python

Why does Python keep a reference count on False and True?

I was looking at the source code to the hasattr built-in function and noticed a couple of lines that piqued my interest: Py_INCREF(Py_False); return Py_False; ... Py_INCREF(Py_True); return Py_True; Aren't Py_False and Py_True global values? Just out of sheer curiosity, why is Python keeping a reference count for these variables? ...

Verbose list comprehension in Python

I have a list of integers and I want to create a new list with all elements smaller than a given limit. a=range(15) #example list limit=9 #example limit My approach to solve this problem was [i for i in a if i < limit] To me the beginning 'i for i in' looks pretty verbose. Is there a better implementation in Python? ...

How can I get the element of a list that has a minimum/maximum property in Python?

I have the following array in Python: points_list = [point0, point1, point2] where each of points_list is of the type: class point: __init__(self, coord, value): self.coord = numpy.array(coord) self.value = value # etc... And a function: def distance(x,y): return numpy.linalg.norm(x.coord - y.coord) And I...

Charts with proper unicode support

I want to create simple charts like pies and bars in python. I tried out CairoPlot and pycha. Both look amazing, but they seem not to be able to handle unicode characters properly. CairoPlot.pie_plot(name='test.png', width=800, height=600, data={'eins':100, 'zwei':48, 'drei':90, 'vier':98,u'fünf':187}) result in f...

Related to executing Java programs through Python

I am using os.system to execute a java program through a python script. I need to pass a file name to the java program as an argument. I am not able to figure out how to pass the relative file location. What should be my reference for determining the relative location. I tried to use the location of the python script as the reference but...

How to decompile a regex?

Is there any way to decompile a regular expression once compiled? ...

wxpython drag&drop focus problem

Hi, I'd like to implement drag&drop in wxPython that works in similar way that in WordPad/Eclipse etc. I mean the following: when something is being dropped to WordPad, WordPad window is on top with focus and text is added. In Eclipse editor text is pasted, Eclipse window gains focus and is on top. When I implement drag&drop using wxPy...

Installing Mercurial on Mac OS X 10.6 Snow Leopard

Installing Mercurial on Mac OS X 10.6 Snow Leopard I installed Mercurial 1.3.1 on Mac OS X 10.6 Snow Leopard from source using the following: cd ~/src curl -O http://mercurial.selenic.com/release/mercurial-1.3.1.tar.gz tar xzvf mercurial-1.3.1.tar.gz cd mercurial-1.3.1 make ALL sudo make install This installs the site-packages files ...

Is it Pythonic for a function to return an iterable or non-iterable depending on its input?

(Title and contents updated after reading Alex's answer) In general I believe that it's considered bad form (un-Pythonic) for a function to sometimes return an iterable and sometimes a single item depending on its parameters. For example struct.unpack always returns a tuple even if it contains only one item. I'm trying to finalise the...

Jinja2 If Statement

The code below is a sample form I'm using to learn jinja2. As written, it returns an error saying that it doesn't recognize the {% endif %} tag. Why does this happen? <html> Name: {{ name }} Print {{ num }} times Color: {{ color }} {% if convert_to_upper %}Case: Upper {% elif not convert_to_upper %}Case: Lower{% endif %} {% for r...

Command line options with optional arguments in Python

I was wondering if there's a simple way to parse command line options having optional arguments in Python. For example, I'd like to be able to call a script two ways: > script.py --foo > script.py --foo=bar From the Python getopt docs it seems I have to choose one or the other. ...

Django forms: how to display media (javascript) for a DateTimeInput widget ?

Hello (please excuse me for my bad english ;) ), Imagine the classes bellow: models.py from django import models class MyModel(models.Model): content_type = models.ForeignKey(ContentType, verbose_name=_('content type')) object_id = models.PositiveIntegerField(_('object id')) content_object = generic.GenericForeignKey('con...

How much data could be stored into a Google App Engine, application?

Answering this question and searching for references I have this doubt my self: *How much data could be stored into a Google App Engine, application? If I'm reading well this table: Resources | Free daily | Free Max Rate | Daily Billing enable | Max Rate Billing --------------------------------------------------------...

Python 3 and open source: Are there any good projects?

I've been studying Python 3 recently and I have come across a conundrum: I want to expand my abilities by working on an open source project, but I seem to have trouble finding any specifically for Python 3. I know that this question has been asked before: Such as here, And here, Unfortunately these all seem to be using Python <= 2.6 and...

How to improve performance of python cgi that reads a big file and returns it as a download?

I have this python cgi script that checks if it hasn't been accessed to many times from the same IP, and if everything is ok, reads a big file form disk (11MB) and then returns it as a download. It works,but performance sucks. The bottleneck seems to be reading this huge file over and over: def download_demo(): """ Returns the...

Can Python's MiniMock create mock of functions defined in the same file?

I'm using the Python MiniMock library for unit testing. I'd like to mock out a function defined in the same Python file as my doctest. Can MiniMock handle that? The naive approach fails: def foo(): raise ValueError, "Don't call me during testing!" def bar(): """ Returns twice the value of foo() >>> from minimock impor...

Automatic String to Number conversion in Python

Hello, I am trying to compare two lists of string in python. Some of the strings are numbers however I don't want to use it as number, only for string comparison. I read the string from a file and put them on a list like this: def main(): inputFileName = 'BateCarteira.csv' inputFile = open(inputFileName, "r") bankNumbers ...

Do I need multiple cursor objects to loop over a recordset and update at the same time?

So I've got a large database that I can't hold in memory at once. I've got to loop over every item in a table, process it, and put the processed data into another column in the table. While I'm looping over my cursor, if I try to run an update statement it truncates the recordset (I believe because it's re-purposing the cursor object)....

What are sqlite development headers and how to install them?

I am trying to install pysqlite and have troubles with that. I found out that the most probable reason of that is missing sqlite headers and I have to install them. However, I have no ideas what these headers are (where I can find them, what they are doing and how to install them). Can anybody, pleas, help me with that? ...

Print PDF document with python's win32print module?

I'm trying to print a PDF document with the win32print module. Apparently this module can only accept PCL or raw text. Is that correct? If so, is there a module available to convert a PDF document into PCL? I contemplated using ShellExecute; however, this is not an option since it only allows printing to the default printer. I need to...