getattr and setattr on nested subobjects / chained properties?

I have an object (Person) that has multiple subobjects (Pet, Residence) as properties. I want to be able to dynamically set the properties of these subobjects like so: class Person(object): def …

Implement MATLAB’s im2col ‘sliding’ in Python

Q: How to speed this up? Below is my implementation of Matlab’s im2col ‘sliding’ with the additional feature of returning every n’th column. The function takes an image (or any 2 dim array) and …

Naturally sorting Pandas DataFrame

I have a pandas DataFrame with indices I want to sort naturally. Natsort doesn’t seem to work. Sorting the indices prior to building the DataFrame doesn’t seem to help because the manipulations I do …

Dynamically creating Django models with `type`

I have 20+ MySQL tables, prm_a, prm_b, … with the same basic structure but different names, and I’d like to associate them with Django model classes without writing each one by hand. So, feeling …

How to get PID by process name?

Is there any way I can get the PID by process name in Python? PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND …

How to print the filename returned by askopenfilename?

As the codes show, I wanted to print the file name. def callback(): file_name = open(askopenfilename(filetypes = ((“Music File”, “*.mp3”) …

query from postgresql using python as dictionary

I’m using Python 2.7 and postgresql 9.1. Trying to get dictionary from query, I’ve tried the code as described here: http://wiki.postgresql.org/wiki/Using_psycopg2_with_PostgreSQL import psycopg2 …

Pip freeze vs. pip list

A comparison of outputs reveals differences: Pip’s documentation states but what is “requirements format,” and why does pip list generate a more comprehensive list than pip freeze? Answer When you are using a virtualenv, you can specify a requirements.txt file to install all the dependencies. A typical usage: The packages need to be in a specific format for pip to understand, which is That is the “requirements format”. Here, django==1.4.2 implies install django version 1.4.2 (even though the latest is 1.6.x). If you do not specify ==1.4.2, the latest version available would be installed. You can read more in “Virtualenv

Print range of numbers on same line

Using python I want to print a range of numbers on the same line. how can I do this using python, I can do it using C by not adding n, but how can I do it using python. for x in xrange(1,10): …

Local variable referenced before assignment?

I am using the PyQt library to take a screenshot of a webpage, then reading through a CSV file of different URLs. I am keeping a variable feed that incremements everytime a URL is processed and …