python

How use __setattr__ & __getattr__ for map INI values?

Hi, I want to map a INI file as a python object. So if the file have: [UserOptions] SampleFile = sample.txt SamplePort = 80 SampleInt = 1 Sample = Aja SampleDate = 10/02/2008 Then I want: c = Configuration('sample.ini') c.UserOptions.SamplePort = 90 I'm looking to setattr but I get a recursion error. This is what I have: class ...

Is python automagically parallelizing IO- and CPU- or memory-bound sections?

This is a follow-up questions on a previous one. Consider this code, which is less toyish than the one in the previous question (but still much simpler than my real one) import sys data=[] for line in open(sys.argv[1]): data.append(line[-1]) print data[-1] Now, I was expecting a longer run time (my benchmark file is 65150224 li...

How to do this join query in Django

In Django, I have two models: class Product(models.Model): name = models.CharField(max_length = 50) categories = models.ManyToManyField(Category) class ProductRank(models.Model): product = models.ForeignKey(Product) rank = models.IntegerField(default = 0) I put the rank into a separate table because every view of a pa...

A class method which behaves differently when called as an instance method?

I'm wondering if it's possible to make a method which behaves differently when called as a class method than when called as an instance method. For example, as a skills-improvement project, I'm writing a Matrix class (yes, I know there are perfectly good matrix classes already out there). I've created a class method for it called identi...

How do capture groups work? (wrt python regular expressions)

While using regex to help solve a problem in the Python Challenge, I came across some behaviour that confused me. from here: (...) Matches whatever regular expression is inside the parentheses. and '+' Causes the resulting RE to match 1 or more repetitions of the preceding RE. So this makes sense: >>>import re >>>re.findall(r"(\d+...

What GUI options are available for Python on Mac OS X?

Is there like an AIR for python? I know I can use Cocoa with Python, is there anything else? How easy is it to get started with Cocoa for Python if you know Python. ...

Ordering a list of dictionaries in python

I've got a python list of dictionaries: mylist = [ {'id':0, 'weight':10, 'factor':1, 'meta':'ABC'}, {'id':1, 'weight':5, 'factor':1, 'meta':'ABC'}, {'id':2, 'weight':5, 'factor':2, 'meta':'ABC'}, {'id':3, 'weight':1, 'factor':1, 'meta':'ABC'} ] Whats the most efficient/cleanest way to order that list by weight then factor (numericaly)...

Use of cycle in django

I have a webpage where I am looping,and using cycle inside the loop. {% for o in something %} {% for c in o %} <div class="{% cycle 'white' 'black'%}"></div> {% endfor %} Now, this means everytime inside the loop, first div tag gets white.But,what I want is to alternate between white and black i.e. start with white, then next time wh...

Is there a "safe" subset of Python for use as an embedded scripting language?

In the many Python applications I've created, I often create simple modules containing nothing but constants to be used as config files. Additionally, because the config file is actually a Python code file, I can add simple logic for changing variables depending on a debug level, etc. While this works great for internal applications, I...

Get a unique list/tuple element given a condition in python

Hi! How do I get a tuple/list element given a condition in python? This occurs pretty often and I am looking for a nice-few-lines-pythonic way of doing this. here could be an example: Consider a tuple containing 2D points coordinates like this: points = [[x1, y1],[x2, y2],[x3, y3], ...] And I would like to get the point that minimi...

How to download a file using python in a 'smarter' way?

I need to download several files via http in Python. The most obvious way to do it is just using urllib2: import urllib2 u = urllib2.urlopen('http://server.com/file.html') localFile = open('file.html', 'w') localFile.write(u.read()) localFile.close() But I'll have to deal with the URLs that are nasty in some way, say like this: http:...

Txt file parse to get a list of .o file names

Hello, I have a txt file like : test.txt Symbols from __ctype_tab.o: Name Value Class Type Size Line Section __ctype |00000000| D | OBJECT|00000004| |.data __ctype_tab |00000000| r | OBJECT|00000101| |.rodata Symbols from _ashldi3.o: N...

Fql needed for fetching status of user friends(django+ facebook)

Hi, I am trying to fetch all status history of the user friends.Basically I am showing up friend list with thr pics and name. so I wanted to put the link next to the name saying how many times user had updated the status. on clicking on the link I can see the whole history of the status updated by that guy. Need help for writing fql. t...

Delete all files/directories except two specific directories

So, there seems to be a few questions asking about removing files/directories matching certain cases, but I'm looking for the exact opposite: Delete EVERYTHING in a folder that DOESN'T match my provided examples. For example, here is an example directory tree: . |-- coke | |-- diet | |-- regular | `-- vanilla |-- icecream | |-...

Is it possible to have multiple statements in a python lambda expression?

I am a python newbie trying to achieve the following: I have a list of lists: lst = [[567,345,234],[253,465,756, 2345],[333,777,111, 555]] I want map lst into another list containing only the second smallest number from each sublist. So the result should be: [345, 465, 333] For example if I were just interested in the smallest num...

Extracting bitmap from a file

Hi all, given a somewhat complex file of unknown specification that among other things contains an uncompressed bitmap file (.BMP), how would you extract it in Python? Scan for the "BM" tag and see if the following bytes "resemble" a BMP header? ...

Pointers in Python on variables with None value

Hi everybody! I have a method that creates a new node in a tree - either left or right. If the value is lower than my current value it is inserted on the left, otherwise on the right side. I want to refactor this code, so that I first see on which side I have to insert my element, and then insert it. Before I implemented this twice: On...

How would you write a @debuggable decorator in python?

When debugging, I like to print out all the inputs and outputs of a function (I know I need a better IDE, but humour me, this could be used for error reporting). So, I'd ideally like to have: @debuggable def myfunc(argA,argB,argC): return argB+1 and use a global variable to switch on or off debugging. No, you don't like globals ei...

url method of ImageField returns a non-Url value - Django

I'm developing using Django on Windows. I have a model with an imagefield, and use a form to fill it. Images get uploaded without problem. The problem occurs when I attempt to show an uploaded image inside a template by coding this: <img src ='{{object.image.url}}'/> (object is an instance of the relevant model, and image is the name ...

Python: adding namespaces in lxml

I'm trying to specify a namespace using lxml similar to this example (taken from here): <TreeInventory xsi:noNamespaceSchemaLocation="Trees.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;"&gt; </TreeInventory> I'm not sure how to add the Schema instance to use and also the Schema location. The documentation got me start...