python

Why is this genexp performing worse than a list comprehension?

I was trying to find the quickest way to count the number of items in a list matching a specific filter. In this case, finding how many odd numbers there are in a list. While doing this, I was surprised by the results of comparing a list comprehension vs the equivalent generator expression: python -m timeit -s "L = xrange(1000000)" "su...

How do I get virtualenvwrapper and cygwin to co-operate?

I am trying to get virtualenv[wrapper] to work on my windows machine through cygwin. The install is successful, together with easy_install, from these directions: http://www.doughellmann.com/docs/virtualenvwrapper/. The problem comes in when I use the mkvirtualenv [name_of_vir_env]. I get the following output: Blockquote bash-3.1$ ...

How to install NumPy on Windows 64?

NumPy installer can't find python path in the registry. Cannot install Python version 2.5 required, which was not found in the registry. OK I have to modify the registry? I already modified %PATH% to point to the Python25 installation directory. ...

recursive nested expression in Python

I am using Python 2.6.4. I have a series of select statements in a text file and I need to extract the field names from each select query. This would be easy if some of the fields didn't use nested functions like to_char() etc. Given select statement fields that could have several nested parenthese like "ltrim(rtrim(to_char(base_fiel...

How do you get a responsive GUI if your codebehind is running an infinte loop? PyQT

If you have a function consistently running an infinite loop in the background, how will your GUI ever be responsive? It is waiting for the loop to finish and this renders the interface useless. How is this solved in PyQT? ...

how to use /dev/ptmx for create a virtual serial port?

I have a program, using pyserial, and I want to test it without using a real serial port device. In windows, I use com0com, and in linux, I know there is a method to create virtual serial port pair without using additional program. so I look up the manual, and found pts, /dev/ptmx, but I don't know how to create a pair by following the...

Python rounding issue

I have come across a very strange issue in python. (Using python 2.4.x) In windows: >>> a = 2292.5 >>> print '%.0f' % a 2293 But in Solaris: >>> a = 2292.5 >>> print '%.0f' % a 2292 But this is the same in both windows and solaris: >>> a = 1.5 >>> print '%.0f' % a 2 Can someone explain this behavior? I'm guessing it's platform ...

Python word splitting

Is there a good library can detect and split words from a combined string? Example: "cdimage" -> ["cd", "image"] "filesaveas" -> ["file", "save", "as"] ...

Why do we need tuples in Python (or any immutable data type)?

I've read several python tutorials (Dive Into Python, for one), and the language reference on Python.org - I don't see why the language needs tuples. Tuples have no methods compared to a list or set, and if I must convert a tuple to a set or list to be able to sort them, what's the point of using a tuple in the first place? Immutabilit...

Having trouble with py.test remote

I love py.test and am trying to get the remote test execution feature to work so I can run tests on a remote machine. There is very little doc and I am getting frustrated with it. Any help figuring out what I am doing wrong is appreciated. Here is my command line on the main server: c:\Python26\Scripts\py.test --dist=each --tx socket...

django error ,about django-sphinx

from django.db import models from djangosphinx.models import SphinxSearch class MyModel(models.Model): search = SphinxSearch() # optional: defaults to db_table # If your index name does not match MyModel._meta.db_table # Note: You can only generate automatic configurations from the ./manage.py script # if your index name...

In the Python version of Google App Engine, what is the easiest way to mock or subclass the File class so that software written to access files does not throw an exception?

I would like to run some code on the Python version of Google App Engine that uses the built in File type. I’m looking for the easiest way to stop GAE from throwing errors due to illegal access. Has anyone already sub-classed or mocked File to read and write to memory rather than to the disk? I don’t need persistence, just the ability to...

unpredicted output binary search tree in python

class Node(): def __init__(self,data, left=None, right=None): self.data = data self.left = left self.right = right class BSTree(): def __init__(self): self.root = None def add(self,data): if self.root is None: self.root = Node(data) self.reset() els...

PyGTK: How do I make a custom widget look like a gtk.Notebook tab?

Hi, I'm writing a program and I need some extra functionality from the gtk.Notebook widget, so I have taken to creating my own. My only problem is styling my tabs so that they look like the tabs in gtk.Notebook and will change according to the user's theme. I really don't know where to start so any advice would be much appreciated, th...

Making wxPython GUI launch on a different screen from Eclipse

I currently have a dual-monitor setup with Eclipse on monitor 2. When I run the code that launches the wxPython GUI, I would like for this GUI to appear on monitor 1. Currently, the GUI consistently appears on monitor 2, covering Eclipse, and I have to drag it to monitor 1 every time. Is there a solution to this problem -- either a confi...

Django query filter, passing a date results in an invalid SQL syntax

z = Pokes.objects.filter( pokestiming__when_start__lte=datetime.now(), pokestiming__when_end__gte=datetime.now(), pokestiming__next_poke__lte=datetime.now(), poke_deleted=0, poke_operating=1, ) That's a query I'm using. It selects any Poke object, with a PokesTiming object (foreign key) within the date ranges. It's...

Opening POSTed file with PIL Image

Using WSGI, webob and PIL, I'm trying to use Image.open() on a file directly from the request. However, Image.open() always throws the exception "cannot identify image file". The image is the only field, no other POST or GET variables are used. The file is coming from a standard HTML upload form with enctype="multipart/form-data". im...

Migrating from Javadoc to Python Documentation

So I've gotten somewhat used to Javadoc style documentation. Looking through various examples of Python code, I'm finding that, at first blush, the documentation seems to be missing a lot of information. The good: vary rarely do you see self-evident bits of documentation. Docstrings are usually a paragraph or less of English markup that...

sscanf in Python

I'm looking for an equivalent to sscanf() in Python. I want to parse /proc/net/* files, in C I could do something like this: int matches = sscanf( buffer, "%*d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %*X %*X:%*X %*X:%*X %*X %*d %*d %ld %*512s\n", local_addr, &local_port, rem_addr, &rem_port, &inode); I thought at ...

django error when i use django svn version.

this code: class TribeForm(forms.ModelForm): slug = forms.SlugField(max_length=20, help_text = _("a short version of the name consisting only of letters, numbers, underscores and hyphens."), error_message = _("This value must contain only letters, numbers, underscores and hyphens.") ) def clean_slug(sel...