python

Can Python's list comprehensions (ideally) do the equivalent of 'count(*)...group by...' in SQL?

I think list comprehensions may give me this, but I'm not sure: any elegant solutions in Python (2.6) in general for selecting unique objects in a list and providing a count? (I've defined an __eq__ to define uniqueness on my object definition). So in RDBMS-land, something like this: CREATE TABLE x(n NUMBER(1)); INSERT INTO x VALUES(1...

scrape html generated by javascript with python

I need to scrape a site with python. I obtain the source html code with the urlib module, but I need to scrape also some html code that is generated by a javascript function (which is included in the html source). What this functions does "in" the site is that when you press a button it outputs some html code. How can I "press" this butt...

How do I change the python_egg_cache?

I am trying to get django up and running in production mode but I get this error that I can't seem to fix: ExtractionError: Can't extract file(s) to egg cache The following error occurred while trying to extract file(s) to the Python egg cache: [Errno 13] Permission denied: '/home/james/.python-eggs' The Python egg cache directory ...

Python: strange numbers being pulled from binary file /confusion with hex and decimals

This might be extremely trivial, and if so I apologise, but I'm getting really confused with the outputs I'm getting: hex? decimal? what? Here's an example, and what it returns: >>> print 'Rx State: ADC Clk=', ADC_Clock_MHz,'MHz DDC Clk=', DDC_Clock_kHz,'kHz Temperature=', Temperature,'C' Rx State: ADC Clk= [1079246848L, 0L] MHz DDC Cl...

How to write a confusion matrix in Python?

I wrote a confusion matrix calculation code in Python: def conf_mat(prob_arr, input_arr): # confusion matrix conf_arr = [[0, 0], [0, 0]] for i in range(len(prob_arr)): if int(input_arr[i]) == 1: if float(prob_arr[i]) < 0.5: conf_arr[0][1] = ...

How do I find the shortest overlapping match using regular expressions?

I'm still relatively new to regex. I'm trying to find the shortest string of text that matches a particular pattern, but am having trouble if the shortest pattern is a substring of a larger match. For example: import re string = "A|B|A|B|C|D|E|F|G" my_pattern = 'a.*?b.*?c' my_regex = re.compile(my_pattern, re.DOTALL|re.IGNORECASE) matc...

Boost::Python, static factories, and inheritance.

So I may have a rather unique use case here, but I'm thinking it should work- But it's not working correctly. Basically, I have a class that uses a static factory method ( create ) that returns a shared_ptr to the newly created instance of the class. This class also has a virtual function that I'd like to override from python and call f...

Python - Trap all signals

In python 2.6 under Linux, I can use the following to handle a TERM signal: import signal def handleSigTERM(): shutdown() signal.signal(signal.SIGTERM, handleSigTERM) Is there any way to setup a handler for all signals received by the process, other than just setting them up one-at-a-time? ...

Running a Django test server under twisted web

As I'm writing an application which uses twisted web for serving async requests and Django for normal content delivery, I thought it would have been nice to have both run under the same twisted reactor through the WSGI interface of Django. I also wanted to test my app using the nice test server facility that Django offers. At first I si...

When running a python script in IDLE, is there a way to pass in command line arguments (args)?

I'm testing some python code that parses command line input. Is there a way to pass this input in through IDLE? Currently I'm saving in the IDLE editor and running from a command prompt. I'm running Windows. ...

Python asyncore & dbus

Is it possible to integrate asyncore with dbus through the same main loop? Usually, DBus integration is done through glib main loop: is it possible to have either asyncore integrate this main loop or have dbus use asyncore's ? ...

Downloading a File Protected by NTLM/SSPI Without Prompting For Credentials Using Python on Win32?

The title says it all, even if it is a mouthful! I need to download a file on a corporate Sharepoint site using CPython. Existing codebase prevents me from using Ironpython without porting the code, so .NET's WebClient library is out. I also want to download the file without prompting the user to save and without prompting the user for ...

How to fix "can't adapt error" when saving binary data using python psycopg2

I ran across this bug three times today in one of our projects. Putting the problem and solution online for future reference. impost psycopg2 con = connect(...) def save(long_blob): cur = con.cursor() long_data = struct.unpack('<L', long_blob) cur.execute('insert into blob_records( blob_data ) values (%s)', [long_data...

What's the difference between OneToOne and Subclassing a model in Django

For example: class Subdomain(Site): #fields here and class Subdomain(models.Model): site = models.OneToOne(Site) #fields here ...

possible in sqlalchemy to join table based on a column value?

I am trying to create a table to hold user actions on my web app. Take a simple case where a user adds a new story, and comments on it. This will add two entries to the user_action table. In the user_action table I would like to store the module name associated with each action and the items id. In this cause I would store the modules as...

django - inlineformset_factory with more than one ForeignKey

Hey people, Im trying to do a formset with the following models (boost is the primary): class boost(models.Model): creator = models.ForeignKey(userInfo) game = models.ForeignKey(gameInfo) name = models.CharField(max_length=200) desc = models.CharField(max_length=500) rules = models.CharField(max_length=500) subsc...

Python-MySQLdb problem: wrong ELF class: ELFCLASS32

As part of trying out django CMS (http://www.django-cms.org/), I'm struggling with getting Python-MySQLdb to work (http://pypi.python.org/pypi/MySQL-python/). I have installed Django CMS and all of its dependencies (Python 2.5, Django, django-south, MySQL server) I'm trying out the example code within Django CMS code with MySQL as chos...

Customary To Inherit Metaclasses From type?

I have been trying to understand python metaclasses, and so have been going through some sample code. As far as I understand it, a Python metaclass can be any callable. So, I can have my metaclass like def metacls(clsName, bases, atts): .... return type(clsName, bases, atts) However, I have seen a lot of people write their met...

"NOTICE AUTH" notifications when connecting to IRC server

As a learning exercise, I'm writing a Python program to connect to a channel on an IRC network, so I can output messages in the channel to stdout. I'm using asynchat and manually sending the protocol messages, rather than using something like Twisted or existing bot code from the net - again, it's a more useful learning experience that w...

Python: How does regex re.compile(r'^[-\w]+$') search? Or, how does regex work in this context?

By reading the documentation here it seems to me that re.compile(r'^[-\w]+$') would just search whether there was any character that is alphanumeric, an underscore, or a hyphen. But really this returns a match only if all the characters fit that description (ie, it fails if there is a space or a dollar sign or asterisk, etc). I don't...