python

django - convert a list back to a queryset

I have a handful of records that I would like to sort based on a computed value. Got the answer over here... like so: sorted (Profile.objects.all (), key = lambda p: p.reputation) on a Profile class like this: class Profile(models.Model): ... @property def reputation(self): ... Unfortunately the generic view i...

What should I call this function composition?

What should 'foo' be called, given the following? x.items is a set, y.values is a set. function a(key) returns an ordered list of x.items function b(x.item) returns a single y.value Define foo(a, b), which returns a function, d, such that d(key) returns a list of y.values defined by: map(b, a(key)). This feels like a fairly common a...

How to add hooks in twisted.web (or twisted.web2)?

How can I add a hook before and after processing a request on twisted.web (twisted.web2 is fine too)? The equivalent of webpy's: app = web.application(urls, globals()) app.add_processor(web.loadhook(my_attach_callback)) app.add_processor(web.unloadhook(my_detach_callback)) Thanks! ...

Unicode problems with web pages in Python's urllib

I seem to have the all-familiar problem of correctly reading and viewing a web page. It looks like Python reads the page in UTF-8 but when I try to convert it to something more viewable (iso-8859-1) I get this error: UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 2: ordinal not in range(128) The code look...

Coding a coroutine in Python to display "odd" and "even" numbers inifinitely

Hello, I have scratchy ideas of Generators, Iterators and Coroutines. (from PEPs and other tutorials). I want to implement a coroutine- in which routine1 will print odd and routine2 will print even numbers infinitely in a fashion such as: routine1: print odd yield to routine2 routune2: print even yield to routine1 Hav...

Flags in Python

I'm working with a large matrix (250x250x30 = 1,875,000 cells), and I'd like a way to set an arbitrary number of flags for each cell in this matrix, in some manner that's easy to use and reasonably space efficient. My original plan was a 250x250x30 list array, where each element was something like: ["FLAG1","FLAG8","FLAG12"]. I then cha...

How to get a nested element in beautiful soup

I am struggling with the syntax required to grab some hrefs in a td. The table, tr and td elements dont have any class's or id's. If I wanted to grab the anchor in this example, what would I need? < tr > < td > < a >... Thanks ...

How do I select a random element from an array in Python?

The first examples that I googled didn't work. This should be trivial, right? ...

How to append two strings in Python?

I have done this operation millions of times, just using the + operator! I have no idea why it is not working this time, it is overwriting the first part of the string with the new one! I have a list of strings and just want to concatenate them in one single string! If I run the program from Eclipse it works, from the command-line it do...

Mule vs ActiveMQ for Python

I need to manged several servers, network services, appalication server (Apache, Tomcat) and manage them (start stop, install software). I would like to use Python, since C++ seems to complex and less productive for thing task. In am not sure which middleware to use. ActiveMQ and Mule seem to be a good choice, although written in Java. ...

wxPython RichTextCtrl much slower than tkInter Text?

I've made a small tool that parses a chunk of text, does some simple processing (retrieves values from a dictionary, a few regex, etc.) and then spits the results. In order to make easier to read the results, I made two graphic ports, one with tkInter and other with wxPython, so the output is nicely displayed in a Text Area with some wo...

Searching across multiple tables (best practices)

I have property management application consisting of tables: tenants landlords units properties vendors-contacts Basically I want one search field to search them all rather than having to select which category I am searching. Would this be an acceptable solution (technology wise?) Will searching across 5 tables be OK in the long run...

Python strings split with multiple separators

Weird - I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want an array of the words. i.e - "Hey, you - what are you doing here!?" should be ['hey', 'you', 'what', 'are', 'you', 'doing', 'here']. But python's split() only works with one argument... so I have all...

How do I install with distutils to a specific Python installation?

I have a Windows machine with Python 2.3, 2.6 and 3.0 installed and 2.5 installed with Cygwin. I've downloaded the pexpect package but when I run "python setup.py install" it installs to the 2.6 installation. How could I have it install to the Cygwin Python installation, or any other installation? ...

Python Memory Model

I have a very large list Suppose I do that (yeah, I know the code is very unpythonic, but for the example's sake..): n = (2**32)**2 for i in xrange(10**7) li[i] = n works fine. however: for i in xrange(10**7) li[i] = i**2 consumes a significantly larger amount of memory. I don't understand why that is - storing the big number t...

Strange behavior with ModelForm and saving

This problem is very strange and I'm hoping someone can help me. For the sake of argument, I have a Author model with ForeignKey relationship to the Book model. When I display an author, I would like to have a ChoiceField that ONLY displays the books associated with that author. As such, I override the AuthorForm.init() method and I c...

How to delete all the items of an expecific key in a list of dicts?

I'm trying to remove some items of a dict based on their key, here is my code: d1 = {'a': 1, 'b': 2} d2 = {'a': 1} l = [d1, d2, d1, d2, d1, d2] for i in range(len(l)): if l[i].has_key('b'): del l[i]['b'] print l The output will be: [{'a': 1}, {'a': 1}, {'a': 1}, {'a': 1}, {'a': 1}, {'a': 1}] Is there a better way to ...

using pyodbc on ubuntu to insert a image field on SQL Server

I am using Ubuntu 9.04 I have installed the following package versions: unixodbc and unixodbc-dev: 2.2.11-16build3 tdsodbc: 0.82-4 libsybdb5: 0.82-4 freetds-common and freetds-dev: 0.82-4 python2.6-dev I have configured /etc/unixodbc.ini like this: [FreeTDS] Description = TDS driver (Sybase/MS SQL) Driver = /usr...

Changing variable names with Python for loops

Hi, I was just wondering if anyone knew of a way to change variable names based off of a for loop for something like this: for i in range(3) group+i=self.getGroup(selected, header+i) so that the names of the variables change to accomodate the data. Thanks! ~Sam ...

Python Decorator 3.0 and arguments to the decorator

I'm excited to see the latest version of the decorator python module (3.0). It looks a lot cleaner (e.g. the syntax is more sugary than ever) than previous iterations. However, it seems to have lousy support (e.g. "sour" syntax, to horribly stretch the metaphor) for decorators that take arguments themselves. Does anyone have a good ex...