python

Python: user friendly time format

Python: I need to show file modification times in "1 day ago", "two hours ago", format. Is there something ready to do that? It should be in english ...

Using Python locale or equivalent in web applications?

Python's locale implementation seems to want to either read the locale from system settings or have it be set via a setlocale call. Neither of these work for me since I'd like to use the capabilities in a web application, where the desired locale is the user's locale. And there are warnings in the locale docs that make the whole thing s...

python: parse HTTP POST request w/file upload and additional params

Hello all, The task is simple: on the server side (python) accept an HTTP POST which contains an uploaded file and more form parameters. I am trying to implement upload progress indicator, and therefore I need to be able to read the file content chunk-by-chunk. All methods I found are based on cgi.FieldStorage, which somehow only allo...

How can 2 Python dictionaries become 1?

Possible Duplicate: Python extend for a dictionary I know that Python list can be appended or extended. Is there an easy way to combine two Python dictionaries with unique keys, for instance: basket_one = {'fruit': 'watermelon', 'veggie': 'pumpkin'} basket_two = {'dairy': 'cheese', 'meat': 'turkey'} I then want one big baske...

How to perform this RegExp in Python ?

Hello, I would like to transform a phone number of this form +33.300000000 in 03.00.00.00.00 +33 is the indicatif it could be 2 or 3 digits length. Digits after the . are the phone number. It could be 9 or 10 digits length. I try like this : p = re.compile( "\+[0-9]+\.([0-9]+)", re.VERBOSE) number = "+33.300000000" p.sub("0\1", numb...

Rules of thumb for when to use operator overloading in python

From what I remember from my C++ class, the professor said that operator overloading is cool, but since it takes relatively a lot of thought and code to cover all end-cases (e.g. when overloading + you probably also want to overload ++ and +=, and also make sure to handle end cases like adding an object to itself etc.), you should only c...

Is there a cleaner way to chain empty list checks in Python?

I have a fairly complex object (deserialized json, so I don't have too much control over it) that I need to check for the existence of and iterate over a fairly deep elements, so right now I have something like this: if a.get("key") and a["key"][0] and a["key"][0][0] : for b in a["key"][0][0] : #Do something which works, b...

SMTP and XMPP deployment/workflow

I'm developing a website that incorporates an XMPP bot and a custom SMTP server (mainly these services process commands and reply). I'd like to set up a system where I can develop locally, push changes to a staging server, and finally to a production system. (Essentially I'm developing on the live server currently.) I'm using python, ...

Python 3.X ready... when?

When will be Python 3.x ready for serious production? I'm thinking mainly in web apps. I have been programming with the 3.x using only the "batteries included" and all works just fine, but I am forced to go back to 2.x when I have to make any serious work in the web (eg, django and/or google app engine). I am primarily interested in thes...

python adding gibberish when reading from a .rtf file?

I have a .rtf file that contains nothing but an integer, say 15. I wish to read this integer in through python and manipulate that integer in some way. However, it seems that python is reading in much of the metadata associated with .rtf files. Why is that? How can I avoid it? For example, trying to read in this file, I get.. {\...

IPython tab completes only some modules

I'm using the EPD version of python and IPython. After installing some modules using easy_install I notice that, although they can be imported, they cannot be tab completed. They exist on the path but, while included modules (pylab, readline, math) can be completed, these new modules cannot. Anyone know what I should look into to find t...

Generate random directories/files given number of files and depth

I'd like to profile some VCS software, and to do so I want to generate a set of random files, in randomly arranged directories. I'm writing the script in Python, but my question is briefly: how do I generate a random directory tree with an average number of sub-directories per directory and some broad distribution of files per directory?...

How to generate a list of 150 cases initialised with a dict in Python ?

I would like to create a list like this list = [] for i in range(150): list.append({'open': False, 'serve': False}) But is there a better way in Python to do it ? ...

Python sqlite3 version

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import sqlite3 >>> sqlite3.version '2.4.1' Questions: Why is the version of the sqlite**3** module '2.4.1' Whats the reason behind bundling such an old sqlite with Python? Th...

How to strip a list of tuple with python ?

I have an array with some flag for each case. In order to use print the array in HTML and use colspan, I need to convert this : [{'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': F...

% operator in python over string

What does the following Python statement mean? send_data="" str_len = "%#04d" % (len(send_data)/2) ...

Most efficient way to add new keys or append to old keys in a dictionary during iteration in Python?

Here's a common situation when compiling data in dictionaries from different sources: Say you have a dictionary that stores lists of things, such as things I like: likes = { 'colors': ['blue','red','purple'], 'foods': ['apples', 'oranges'] } and a second dictionary with some related values in it: favorites = { 'colors':...

Extracting info from html using PHP(XPath), PHP/Python(Regexp) or Python(XPath)

I have approx. 40k+ html documents where I need to extract information from. I have tried to do so using PHP+Tidy(because most files are not well-formed)+DOMDocument+XPath but it is extremely slow.... I am advised to use regexp but the html files are not marked up semantically (table based layout, with meaning-less tag/classes used every...

After submitting form, the value is different due to encoding? (Python)

I am using Django. In a regular form, the user enters "Gerry & Pacemakers". (Notice the Ampersand sign.) When I go views.py... def myview(request): q = request.GET.get('q','').strip() print q q is "Gerry"...but it's supposed to be "Gerry & Pacemakers"...encoded Is the correct way doing this by using urllib?? How do I encod...

How to upload html documentation generated from sphinx to github?

I just documented loads of my code and learnt how to use sphinx to generate the documentation. I want to include that into my github project page but I do not know how to. Does anyone know existing tutorial or simple step to do so? Thanks. ...