I want to convert a binary number into a float number. Here’s an example of a possibility: gives me the correct output: Unfortunately, I am working with binary strings, i.e., I need something like float(‘-0b1110’). However, this doesn’t work: I tried to use binascii.a2b_qp(string[, hea…
Tag: python-3.x
Best way to convert string to bytes in Python 3?
TypeError: ‘str’ does not support the buffer interface suggests two possible methods to convert a string to bytes: Which method is more Pythonic? Answer If you look at the docs for bytes, it points you to bytearray: bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearr…
ctypes variable length structures
Ever since I read Dave Beazley’s post on binary I/O handling (http://dabeaz.blogspot.com/2009/08/python-binary-io-handling.html) I’ve wanted to create a Python library for a certain wire protocol. However, I can’t find the best solution for variable length structures. Here’s what I wan…
How to pass arguments to a Button command in Tkinter?
Suppose I have the following Button made with Tkinter in Python: The method action is called when I press the button, but what if I wanted to pass some arguments to the method action? I have tried with the following code: This just invokes the method immediately, and pressing the button does nothing. See Pyth…
Comparable classes in Python 3
What is the standard way of making a class comparable in Python 3? (For example, by id.) Answer To make classes comparable, you only need to implement __lt__ and decorate the class with functools.total_ordering. You should also provide an __eq__ method if possible. This provides the rest of the comparison ope…
Using @functools.lru_cache with dictionary arguments
I have a method that takes (among others) a dictionary as an argument. The method is parsing strings and the dictionary provides replacements for some substrings, so it doesn’t have to be mutable. This function is called quite often, and on redundant elements so I figured that caching it would improve i…
Python SocketServer error upon connection
I am running a Python server using the socketserver module in Python 3.1. Every time I get a connection from the client (which succeeds client side), my server receives an error. Here is my code: And here is my error: The odd thing I noticed about the error is that the port it gives on the second line is diff…
Regular expression to return text between parenthesis
All I need is the contents inside the parenthesis. Answer If your problem is really just this simple, you don’t need regex:
How to make an immutable object in Python?
Although I have never needed this, it just struck me that making an immutable object in Python could be slightly tricky. You can’t just override __setattr__, because then you can’t even set attributes in the __init__. Subclassing a tuple is a trick that works: But then you have access to the a and…
Parsing the results of askopenfilenames()?
I’m trying to get a list of filenames from tkinter.filedialog.askopenfilenames() in Python 3.2. I was expecting the output to be a tuple (or maybe a list) with each element containing a filename. As far as I can tell, it’s returning a string with each element contained within curly-brackets {} lik…