Skip to content
Advertisement

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[, header]) which converts a block of quoted-printable data back to binary and returns the binary data.

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 bytearray type is a mutable sequence of integers in the range 0 <=

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 want to do: The class Points won’t work since num_points isn’t defined yet. I could redefine the _fields_ variable later once num_points

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 operators so you don’t have to write any of them

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 its efficiency. But, as you may have guessed, since

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 different

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 b variables through self[0] and self[1], which

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 {} like so: {C:UsersBVCAPFile1.txt} {C:UsersBVCAPFile2.txt} This is what I get if I

Advertisement