python

How to list the files in a static directory?

Hello, I am playing with Google App Engine and Python and I cannot list the files of a static directory. Below is the code I currently use. app.yaml - url: /data static_dir: data Python code to list the files myFiles = [] for root, dirs, files in os.walk(os.path.join(os.path.dirname(__file__), 'data/') ): for name in files: ...

Generating and submitting a dynamic number of objects in a form with Django

I want to be able to update a dynamic number of objects within a single form using Django and I'm wondering what the best way to do this would be. An example of a similar situation may help. Model: class Customer(Model.models): name = models.CharField(max_length=100) active = models.BooleanField() Form (I know I'm mixing view...

Suggestions for Python debugging tools?

Yesterday I made a simulation using Python. I had a few difficulties with variables and debugging. Is there any software for Python, which provides a decent debugger? Related question: What is the best way to debug my Python code? ...

How do I simulate flip of biased coin in python?

In unbiased coin flip H or T occurs 50% of times. But I want to simulate coin which gives H with probability 'p' and T with probability '(1-p)'. something like this: def flip(p): '''this function return H with probability p''' # do something return result >> [flip(0.8) for i in xrange(10)] [H,H,T,H,H,H,T,H,H,H] ...

Most suitable language(s) for simulations in modeling?

I will participate a modeling competition, which spends three days. I need a language which is fast and designed for modeling, such as to 2D/3D models. I have considered these languages: Python Sage Which languages would you use? ...

Python decimal range() step value

Is there a way to step between 0 and 1 by 0.1? I thought I could do it like the following but it failed: for i in range(0, 1, 0.1): print i Instead, it says that the step argument cannot be zero, which it's not. Thanks. ...

Easy_install of wxpython has "setup script" error.

I have an install of python 2.5 that fink placed in /sw/bin/. I use the easy install command sudo /sw/bin/easy_install wxPython to try to install wxpython and I get an error while trying to process wxPython-src-2.8.9.1.tab.bz2 that there is not setup script. Easy-install has worked for several other installations until this one. Any h...

How to convert XML to JSON in Python

I am importing an XML feed and trying to convert it to JSON for output. I'm getting this error: TypeError: <xml.dom.minidom.Document instance at 0x72787d8> is not JSON serializable Unfortunately I know next to nothing about Python. I'm developing this on the Google App Engine. I could use some help, because my little 2 hour hack that...

What's the idiomatic Python equivalent to Django's 'regroup' template tag?

http://docs.djangoproject.com/en/dev/ref/templates/builtins/#regroup I can think of a few ways of doing it with loops but I'd particularly like to know if there is a neat one-liner. ...

Python and os.chroot

I'm writing a web-server in Python as a hobby project. The code is targeted at *NIX machines. I'm new to developing on Linux and even newer to Python itself. I am worried about people breaking out of the folder that I'm using to serve up the web-site. The most obvious way to do this is to filter requests for documents like /../../etc/pa...

How to send multiple input field values with same name?

I have m2m field, lets say it have name 'relations', so i want to allow user to send as many relations as he wants. I add new input to html with javascript with same name, like so <input type='text' name='relations' value='a' /> <input type='text' name='relations' value='b' /> in cleaned_data i receive only value of second input ('b')...

Python regular expressions with more than 100 groups?

Is there any way to beat the 100-group limit for regular expressions in Python? Also, could someone explain why there is a limit. Thanks. ...

Need to route instance calls inside a python class

The problem is a need to take the arguments into account before choosing the responder. Here is my attempt so far. from responders import A, B, C class RandomResponder(object) def init(self, *args, *kwargs): self.args = args self.kwargs = kwargs def __getattr__(self, name): # pick a responder based on t...

What CMS runs on Google App Engine?

Is it possible to deploy any CMS (Content Management System) using Google App Engine? Wikipedia lists 4 Python CMSes and one of them is Django based. Do you know any way to make any of them to work on App Engine? My votes: Python GuteCMS (revision 11 10/28/2009) Too simple. Barely usable. cpedialog (both 1.0 and 2.0 beta) Too simp...

is there a string method to capitalize acronyms in python?

This is good: import string string.capwords("proper name") 'Proper Name' This is not so good: string.capwords("I.R.S") 'I.r.s' Is there no string method to do capwords so that it accomodates acronyms? ...

How do I simulate biased die in python?

I want to simulate N-dimensional biased die? def roll(N,bias): '''this function rolls N dimensional die with biasing provided''' # do something return result >> N=6 >> bias=( 0.20,0.20,0.15,0.15,0.14,0.16,) >> roll(N,bias) 2 ...

How can I build a recursive function in python?

How can I build a recursive function in python? ...

Parsing XML With Python

I'm building a simple web base rss reader in python, but I'm having trouble parsing the xml. I started out by trying some stuff in the python command line. >>> from xml.dom import minidom >>> import urllib2 >>> url ='http://www.digg.com/rss/index.xml' >>> xmldoc = minidom.parse(urllib2.urlopen(url)) >>> channelnode = xmldoc.getElements...

How do you remove duplicates from a list in Python?

Given a list of strings, I want to sort it alphabetically and remove duplicates. I know I can do this: from sets import Set [...] myHash = Set(myList) but I don't know how to retrieve the list members from the hash in alphabetical order. I'm not married to the hash, so any way to accomplish this will work. Also, performance is not an...

Python - is there a list of decorators somewhere?

I know of @staticmethod, @classmethod, and @property, but only through scattered documentation. Is there a complete list of function decorators that are built into Python? ...