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’…
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 …
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 …
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 …
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
Combine Pool.map with shared memory Array in Python multiprocessing
I have a very large (read only) array of data that I want to be processed by multiple processes in parallel. I like the Pool.map function and would like to use it to calculate functions on that data …
Check if object is file-like in Python
File-like objects are objects in Python that behave like a real file, e.g. have a read() and a write method(), but have a different implementation from file. It is realization of the Duck Typing …