python

How do you serialize a model instance in Django?

There is a lot of documentation on how to serialize a Model QuerySet but how do you just serialize to json the fields of a Model Instance? ...

Converting tree list to hierarchy dict

I have a list of elements with attrs: parent, level, is_leaf_node, is_root_node, is_child_node. I want to convert this list to hierarchy dict. Example of output dict: { 'Technology': { 'Gadgets':{}, 'Gaming':{}, 'Programming': { 'Python':{},...

Difference in regex behavior between Perl and Python?

I have a couple email addresses, '[email protected]' and '[email protected]'. In perl, I could take the To: line of a raw email and find either of the above addresses with /\w+@(tickets\.)?company\.com/i In python, I simply wrote the above regex as '\w+@(tickets\.)?company\.com' expecting the same result. However, support...

Shell: insert a blank/new line two lines above pattern

I have a text file and it requires some formatting. I know that if you want to add a blank line above every line that matches your regexp, you can use: sed '/regexp/{x;p;x;}' But I'd like to add a blank line, not one line above, but two lines above the line which matches my regexp. The pattern I'll be matching is a postal code, in th...

django-paypal setup

Has anyone setup django-paypal? Here is the link to it: [here][1]? I have "myproject" setup, and my folder sturecture looks like this: myproject > paypal > (stdandard and pro folders) to my settins.py file I added INSTALLED_APPS = ( 'myproject.paypal.standard', 'myproject.paypal.pro', ) in my url's file for my account app ...

Whats the easiest and fastest way to measure HD performance using Python?

I need to measure the performance of a hard disk using python. What is the best/fastest/shortest/easiest approach to do it? It doesn't have to be overly accurate, just a ballpark value. My actual goal is to write a small utility which will adjust the postgres settings to the best configuration for the given hardware. My naive approach ...

Why doesn't the regex match when I add groups?

I have this regex code in python : if re.search(r"\{\\fad|fade\(\d{1,4},\d{1,4}\)\}", text): print(re.search(r"\{\\fad|fade\((\d{1,4}),(\d{1,4})\)\}", text).groups()) text is {\fad(200,200)}Épisode 101 : {\i1}The Ghost{\i0}\Nv. 1.03 and read from a file (don't know if that helps). This returns the following: (None, None) When I ...

Problem with Python modules

Hi! I'm uploading my first Django app to my Dreamhost server. My app uses xlwt package and since I can't install it in the default location ( /usr/lib/python2.3/site-packages/xlwt ), I installed it on another location by: python setup.py install --home=$HOME Then xlwt is installed here: /home/myuser/lib/python/xlwt/ After that, I ...

Make function definition in a python file order independant

I use Python CGI. I cannot call a function before it is defined. In Oracle PL/SQL there was this trick of "forward declaration"; naming all the functions on top so the order of defining didn't matter no more. Is there such a trick in Python as well? example: def do_something(ds_parameter): helper_function(ds_parameter) .... ...

how to use pycurl if requested data is sometimes gzipped, sometimes not?

I'm doing this to fetch some data: c = pycurl.Curl() c.setopt(pycurl.ENCODING, 'gzip') c.setopt(pycurl.URL, url) c.setopt(pycurl.TIMEOUT, 10) c.setopt(pycurl.FOLLOWLOCATION, True) xml = StringIO() c.setopt(pycurl.WRITEFUNCTION, xml.write ) c.perform() c.close() My urls are typically of this sort: http://host/path/to/resource-f...

PyQt4 Minimize to Tray

Is there a way to minimize to tray in PyQt4? I've already worked with the QSystemTrayIcon class, but now I would like to minimize or "hide" my app window, and show only the tray icon. Has anybody done this? Any direction would be appreciated. Using Python 2.5.4 and PyQt4 on Window XP Pro ...

Program Control-Flow in Python

I have some data that I have stored in a list and if I print out the list I see the following: . . . 007 A000000 Y 007 B000000 5 007 C010100 1 007 C020100 ACORN FUND 007 C030100 N 007 C010200 2 007 C020200 ACORN INTERNATIONAL 007 C030200 N 007 C010300 3 007 C020300 ACORN USA 007 C030300 N 007 C010400 4 . . . The dots before and a...

suggestions for a daemon that accepts zip files for processing

im looking to write a daemon that: reads a message from a queue (sqs, rabbit-mq, whatever ...) containing a path to a zip file updates a record in the database saying something like "this job is processing" reads the aforementioned archive's contents and inserts a row into a database w/ information culled from file meta data for each f...

Using sphinx/miktex to generate pdf files that displays UTF8 Japanese (CJK) text in windows

I have documentation in ReSt (UTF8) and I'm using sphinx to generate HTML and latex files. (No issues with html conversion) I then want to convert the resulting latex file to PDf. Currently I'm using MiKTeX 2.7's pdflatex.exe command to perfom this conversion. (Converting a source file without Japanese characters produces the expected...

Python Sort Collections.DefaultDict in Descending order

I have this bit of code: visits = defaultdict(int) for t in tweetsSQL: visits[t.user.from_user] += 1 I looked at some examples online that used the sorted method like so: sorted(visits.iteritems, key=operator.itemgetter(1), reverse=True) but it's giving me: "TypeError: 'builtin_function_or_method' object is not ite...

Python: MySQLdb Connection Problems

I'm having trouble with the MySQLdb module. db = MySQLdb.connect( host = 'localhost', user = 'root', passwd = '', db = 'testdb', port = 3000) (I'm using a custom port) the error I get is: Error 2002: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) Which doesn't make much se...

Matplotlib suddenly crashes after reinstalling Xcode?

I was happy in my world of python and matplotlib with a good level of familiarity. I notied Xcode on my Mac wasn't working so I installed the latest version from Apple and it somehow broke my install of matplotlib (or numpy?)! I'm now getting ... /sw/lib/python2.5/site-packages/matplotlib-0.91.1-py2.5-macosx- 10.5-i386.egg/matplotlib/n...

How can I remove a temporary file (image) that is being displayed by CGI?

I've written a python CGI script that converts files into .jpgs and displays them in a simple HTML page. I don't want to clutter up the folders with these .jpg files, so I used tempfile.NamedTemporaryFile to create a file to store the converted .jpg output. Everything works great, but i want to remove this file after the page is displa...

Child process detecting the parent process' death in Python

Is there a way for a child process in Python to detect if the parent process has died? ...

How do I make text wrapping match current indentation level in vim?

Does anyone know of a way to get vim to wrap long lines of text such that the position of the wrapped text is based on the indentation of the current line? I don't want to reformat my code, just for it to be displayed prettily. For instance, if I set my settings so that the line: print 'ProcessorError(%r, %r, %r)' % (self.file, self.in...