Skip to content

Tag: python-3.x

How to convert a binary (string) into a float value?

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…

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…

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…

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…

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…