I need to keep track of units on float and int values in Python, but I don’t want to use an external package like magnitude or others, because I don’t need to perform operations on the values. Instead, all I want is to be able to define floats and ints that have a unit attribute (and I don’t want to
Draw bold/italic text with PIL?
How to draw bold/italic text with PIL? ImageFont.truetype(file, size) has an option to specify font size only. Answer Use the bold/italic version of the font
Truncating unicode so it fits a maximum size when encoded for wire transfer
Given a Unicode string and these requirements: The string be encoded into some byte-sequence format (e.g. UTF-8 or JSON unicode escape) The encoded string has a maximum length For example, the iPhone push service requires JSON encoding with a maximum total packet size of 256 bytes. What is the best way to truncate the string so that it re-encodes to
What is the default content-type/charset?
According to this answer: urllib2 read to Unicode I have to get the content-type in order to change to Unicode. However, some websites don’t have a “charset”. For example, the [‘content-type’] for this page is “text/html”. I can’t convert it to Unicode. Is there a default “encoding” (English, of course)…so that if nothing is found, I can just use that?
Is there a cross-platform way to open a file browser in Python?
I’m thinking something along the lines of the webbrowser module, but for file browsers. In Windows I’d like to open explorer, in GNOME on Linux I want to open nautilus, Konqueror on KDE, etc. I’d prefer not to kludge it up if I can avoid it. ;-) Answer I’d prefer not to kludge it up if I can avoid it.
Creating thumbnails from video files with Python
I need to create thumbnails for a video file once I’ve uploaded to a webapp running python. How would I go about this… I need a library that can basically either do this for me, or that can read the image frames out of video files (of several formats) automatically. Answer A simple combination of PyMedia and PIL would do
Understanding generators in Python
I am reading the Python cookbook at the moment and am currently looking at generators. I’m finding it hard to get my head round. As I come from a Java background, is there a Java equivalent? The book was speaking about ‘Producer / Consumer’, however when I hear that I think of threading. What is a generator and why would
Read from File, or STDIN
I’ve written a command line utility that uses getopt for parsing arguments given on the command line. I would also like to have a filename be an optional argument, such as it is in other utilities like grep, cut etc. So, I would like it to have the following usage How can I implement the following? if a filename is
How do I concatenate two lists in Python?
This question’s answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. How do I concatenate two lists in Python? Example: Expected outcome: Answer Use the + operator to combine the lists: Output:
Python socket receive – incoming packets always have a different size
I’m using the SocketServer module for a TCP server. I’m experiencing some issue here with the recv() function, because the incoming packets always have a different size, so if I specify recv(1024) (I tried with a bigger value, and smaller), it gets stuck after 2 or 3 requests because the packet length will be smaller (I think), and then the