python

Django: apply "same parent" constraint to ManyToManyField mapping to self

I have a model where tasks are pieces of work that each may depend on some number of other tasks to complete before it can start. Tasks are grouped into jobs, and I want to disallow dependencies between jobs. This is the relevant subset of my model: class Job(models.Model): name = models.CharField(max_length=60, unique=True) class ...

For list unless empty in python

I've been writing a lot of constructs like this the past couple of days: list = get_list() if list: for i in list: pass # do something with the list else: pass # do something if the list was empty Lot of junk and I assign the list to a real variable (keeping it in memory longer than needed). Python has simplified a lot...

JSON serialization of Google App Engine models

I've been search for quite a while with no success. My project isn't using Django, is there a simple way to serialize App Engine models (google.appengine.ext.db.Model) into JSON or do I need to write my own serializer? My model class is fairly simple. For instance: class Photo(db.Model): filename = db.StringProperty() title = d...

How to yank entire block in vim?

Is it possible to yank an entire block of python code in vim? be it a def, for, if, etc. block.. ...

Double import in grok

This is a normal case of mutual import. Suppose you have the following layout ./test.py ./one ./one/__init__.py ./one/two ./one/two/__init__.py ./one/two/m.py ./one/two/three ./one/two/three/__init__.py ./one/two/three/four ./one/two/three/four/__init__.py ./one/two/three/four/e.py ./one/two/u.py And you have test.py from one.two.t...

What are some useful non-built-in Django tags?

I'm relatively new to Django and I'm trying to build up my toolbox for future projects. In my last project, when a built-in template tag didn't do quite what I needed, I would make a mangled mess of the template to shoe-horn in the feature. I later would find a template tag that would have saved me time and ugly code. So what are some...

Reading HKEY CURRENT USER from the registry in Python, specifying the user

In my application I run subprocesses under several different user accounts. I need to be able to read some of the information written to the registry by these subprocesses. Each one is writing to HKEY_CURRENT_USER, and I know the user account name that they are running under. In Python, how can I read values from HKEY_CURRENT_USER for a...

Why does my function "hangs"

def retCursor(): host = "localhost" user = "disappearedng" db = "gupan_crawling3" conn = MySQLdb.connect( host=host, user=user, passwd=passwd, db=db) cursor = conn.cursor() return cursor singleCur = retCursor() def checkTemplateBuilt(netlocH): """Used by crawler specifically, this check directly whether tem...

Supporting Multiple Python Versions In Your Code?

Today I tried using pyPdf 1.12 in a script I was writing that targets Python 2.6. When running my script, and even importing pyPdf, I get complaints about deprecated functionality (md5->hashsum, sets). I'd like to contribute a patch to make this work cleanly in 2.6, but I imagine the author does not want to break compatibility for older ...

Given a string, how do I know if it needs decoding

I'm using python's base64 module and I get a string that can be encoded or not encoded. I would like to do something like: if isEncoded(s): output = base64.decodestring(s) else: output = s ideas? ...

the fastest way to create checksum for large files in python

hi, i need to transfer large files across network and need to create checksum for them on hourly basis. so the speed for generating checksum is critical for me. somehow i can't make zlib.crc32 and zlib.adler32 working with files larger than 4GB on Windows XP Pro 64bit machine. i suspect i've hit the 32bit limitation here? using hashlib...

Is there anyway to persuade python's getopt to handle optional paramters to options?

According to the documentation on python's getopt (I think) the options fields should behave as the getopt() function. However I can't seem to enable optional parameters to my code: #!/usr/bin/python import sys,getopt if __name__ == "__main__": try: opts, args = getopt.gnu_getopt(sys.argv[1:], "v::", ["verbose="]) excep...

Good high-level python ftp/http lib?

I'm looking for a good, high-level python ftp client/server library. I'm working on a project that has "evolved" a small http/ftp library on top of ftplib/urllib/urllib2 from what was originally one function, and almost none of it was designed to be built upon. So now it's time to refactor kind of seriously, and I'd like to just switch t...

How to read lines from a file into a multidimensional array (or an array of lists) in python

I have a file with a format similar to this: a,3,4,2,1 3,2,1,a,2 I want to read the file and create an array of lists in a way that: array[0] = ['a','3','4','2','1'] array[1] = ['3','2','1','a','2'] How can I do that? So far I am stuck with: f = open('./urls-eu.csv', 'r') for line in f: arr = line.split(',') print arr But ...

Python: Open IE Browser Window

The webbrowser library provides a convenient way to launch a URL with a browser window through the webbrowser.open() method. Numerous browser types are available, but there does not appear to be an explicit way to launch Internet Explorer when running python on windows. WindowsDefault only works if Internet Explorer is set as the defaul...

Can a WIN32 program authenticate into Django authentication system, using MYSQL?

I have a web service with Django Framework. My friend's project is a WIN32 program and also a MS-sql server. The Win32 program currently has a login system that talks to a MS-sql for authentication. However, we would like to INTEGRATE this login system as one. Please answer the 2 things: I want scrap the MS-SQL to use only the Djang...

Turbogears: Can not start paster after updateing to mac osx 10.6

After updateing to mac osx 10.6 I had to switch back to python 2.5 in order to make virtual env work. But still I can not start my turbogears project. Paster is giving this : Traceback (most recent call last): File ".../tg2env/bin/paster", line 5, in <module> from pkg_resources import load_entry_point File ".../tg2env/lib/python...

Access is Denied loading a dll with ctypes on Vista

I'm having issues with using ctypes. I'm trying to get the following project running on Vista. http://sourceforge.net/projects/fractalfrost/ I've used the project before on Vista and had no problems. I don't see any think changed in svn that cause this I'm thinking it's something local to this machine. In fact I'm not able to load d...

Python: how to show results on a web page?

Most likely it's a dumb question for those who knows the answer, but I'm a beginner, and here it goes: I have a Python script which I run in a command-line with some parameter, and it prints me some results. Let's say results are some HTML code. I never done any Python programming for web, and couldn't figure it out... I need to have a...

Use different Python version with virtualenv

I have a Debian system currently running with python 2.5.4. I got virtualenv properly installed, everything is working fine. Is there a possibility that I can use a virtualenv with a different version of Python? I compiled Python 2.6.2 and would like to use it with some virtualenv. Is it enough to overwrite the binary file? Or do I have...