python

Memory requirments for large python list

I was doing a foolish thing like from itertools import * rows = combinations(range(0, 1140), 17) all_rows = [] for row in rows: all_rows.append(row) No surprise I run out of memory address space (32 bit python 3.1) My question i, how do I calculate how much memory address space I will need for a large list? In this case the list i...

Gruber’s URL Regular Expression in Python

How do I rewrite this new way to recognise addresses to work in Python? \b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))) ...

Where should I place the one-time operation operation in the Django framework?

I want to perform some one-time operations such as to start a background thread and populate a cache every 30 minutes as initialize action when the Django server is started, so it will not block user from visiting the website. Where should I place all this code in Django? Put them into the setting.py file does not work. It seems it wil...

Why python doesn't have a sign() function?

I can't understand why python has not a sign() function. It has an abs() builtin (which I consider sign()'s sister), but no sign. In python 2.6 there is even a copysign() function (in math), but no sign. Why bother to write a copysign(x,y) when you could just write a sign() and then get the copysign() directly from abs(x) * sign(y)? The...

How to query a contact for its PubSub using Python's xmpp?

I'm writing a XMPP bot in Python (using xmpppy). I want (after user's request) check his PubSub status (mood, tune) and do something with it. How do I do that? I know how to parse a stanza send by user when he changes status but I don't know how to force him to send me such stanza. ...

Holiday Calendars, File Formats, et al.

I'm looking for a way to figure out whether a given date is a "holiday," given some holiday calendar. Specifically, you might say is_holiday (datetime.date, "USA") which would answer whether the given date is a holiday for the calendar named "USA." I recognize that there's no trivial way of doing this for all holidays for all years. F...

Finding all classes that derive from a given base class in python

I'm looking for a way to get a list of all classes that derive from a particular base class in Python. More specifically I am using Django and I have a abstract base Model and then several Models that derive from that base class... class Asset(models.Model): name = models.CharField(max_length=500) last_update = models.DateTime...

Data Structures in Python

All the books I've read on data structures so far seem to use C/C++, and make heavy use of the "manual" pointer control that they offer. Since Python hides that sort of memory management and garbage collection from the user is it even possible to implement efficient data structures in this language, and is there any reason to do so inste...

How to use ipython's IPShellEmbed from within a running doctest

Please help me get an embedded ipython console to run inside a doctest. The example code demonstrates the problem and will hang your terminal. On bash shell I type ctrl-Z and then kill %1 to break out and kill, since ctrl-C won't work. def some_function(): """ >>> some_function() 'someoutput' """ # now try to drop ...

Create a text file in python using values from a sqlite database

Hello, I am trying to build a text file which is a combination of predefined strings and variable values which I would like to take from a pre-existing sqlite database. The general format of each line of the text file is as such: constraint n: value i < value j Where n is an integer which will increase by one every line. Value i is...

cherrypy: accessing the URI/route parameters inside a tool hook?

I have a tool hook setup for 'before_finalize' like so: def before_finalize(): d = cherrypy.response.body location = '%s/views' % cherrypy.request.app.config['/']['application_path'] cherrypy.response.body = Template(file='%s/index.tmpl' % location).respond() What I need to do is find out inside that hook what route (I'm u...

fast folder size calculation in Python on Windows

I am looking for a fast way to calculate the size of a folder in Python on Windows. This is what I have so far: def get_dir_size(path): total_size = 0 if platform.system() == 'Windows': try: items = win32file.FindFilesW(path + '\\*') except Exception, err: return 0 # Add the size or perform recursion on fold...

Determining if root logger is set to DEBUG level in Python?

If I set the logging module to DEBUG with a command line parameter like this: if (opt["log"] == "debug"): logging.basicConfig(level=logging.DEBUG) How can I later tell if the logger was set to DEBUG? I'm writing a decorator that will time a function if True flag is passed to it, and if no flag is given, it defaults to printing timi...

How to get Permissions to inherit from User's Groups?

I'm trying to figure out Django Groups and the documentation is pretty bare on the site. For example, you can use the decorator permission_required() to check the permissions, however, this only checks if you have assigned permissions directly. I have assigned Users to Groups which have Permissions setup. When using Django's permissio...

What does this code mean: "print >> sys.stderr"

print >> sys.stderr, "Error in atexit._run_exitfuncs:" Why print '>>' in front of sys.stderr? Thanks. ...

Sphinx 0.6.3: The languages module cannot be found.

I receive the following error message when I try to use the sphinx-quickstart generated make.bat command: make html Error: The languages module cannot be found. Did you install Sphinx and its dependencies correctly? I tried running the sphinx-build command and received the same error. I am using Python 2.6.4 on Windows Vista. I ...

Python: print the full array

When I print an array i get the following stuff, but I want the full array. Is there any way to do this? Thanks in advance, and everybody a happy, lucky, successfull new year! [[0 0 0 ..., 0 0 0] [0 0 0 ..., 0 0 0] [0 0 0 ..., 0 0 0] ..., [0 0 0 ..., 0 0 0] [0 0 0 ..., 0 0 0] [0 0 0 ..., 0 0 0]] ...

Gracefully handling "MySQL has gone away"

I'm writing a small database adapter in Python, mostly for fun. I'm trying to get the code to gracefully recover from a situation where the MySQL connection "goes away," aka wait_timeout is exceeded. I've set wait_timeout at 10 so I can try this. Here's my code: def select(self, query, params=[]): try: self.cursor...

Any difference between 'b' and 'c'?

class a(object): b = 'bbbb' def __init__(self): self.c = 'cccc' I think they are the same; is there any difference? ...

Convert an image to RGBA mode with python

I tried the code listed at Using PIL to make all white pixels transparent to convert some .ico files to .png images with transparent background. It doesn't work on all .ico images, some time it just outputs an image with black background. Sample .ico files can be located here ...