python

How do I un-escape a backslash-escaped string in python?

Suppose I have a string which is a backslash-escaped version of another string. Is there an easy way, in Python, to unescape the string? I could, for example, do: >>> escaped_str = '"Hello,\\nworld!"' >>> raw_str = eval(escaped_str) >>> print raw_str Hello, world! >>> However that involves passing a (possibly untrusted) string to ev...

How do I tell PyLint "it's a variable, not a constant" to stop message C0103?

I have a module-level variable in my Python 2.6 program named "_log", which PyLint complains about: C0103: Invalid name "_log" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$) Having read this answer I understand why it's doing this: it thinks the variable is a constant and applies the constant regex. However, I beg to differ: I think it...

How slow are Python/django exceptions?

is python exception slow? I'm kind using python exceptions to structure programm follow in my web application, and I'm wondering how throwing exceptions will affect performance of my application. what is your thoughts? which one of the following statements is less expensive in terms of memory and cpu? try: artist = Artist.objects....

How to join lists element-wise in Python?

l1 = [4, 6, 8] l2 = [a, b, c] result = [(4,a),(6,b),(8,c)] How do I do that? ...

Parsing multilevel text list

I need to parse text lists: 1 List name 1 item 2 item 3 item 2 List name 1 item 2 item 3 item 3 List name 1 item 2 item 3 item I was trying to use regular expression to split first level list: import re def re_show(pat, s): print re.compile(pat, re.S).sub("{\g<0>}", s),'\n' s = ''' 1 List name 1 item 2 item 3 item 2 List nam...

Is it possible to keep the column order using the Python csv DictReader

For example, my csv has columns as below: ID, ID2, Date, Job No, Code I need to write the columns back in the same order. The dict jumbles the order immediately, so I believe it's more of a problem with the reader. ...

Need example/help with GtkTextBuffer (of GtkTextView) serialize/deserialize

I am trying to save user's bold/italic/font/etc tags in a GtkTextView. Using GtkTextBuffer.get_text() does not return the tags. The best documentation I have found on this is: http://www.pygtk.org/docs/pygtk/class-gtktextbuffer.html#method-gtktextbuffer--register-serialize-format However, I do not understand the function arguments. It ...

Django: vps or shared hosting?

I am new to web development and everything involved with it. Im finishing my website in django and i will soon have to find a hosting and deploy it. I heard there are vps or shared hosting types. So here are the questions: 1. How many visits/clicks per day make it worth choosing vps? shared? 2. How hard is it to tune and maintain a vps o...

Python 2.6: reading data from a Windows Console application. (os.system?)

I have a Windows console application that returns some text. I want to read that text in a Python script. I have tried reading it by using os.system, but it is not working properly. import os foo = os.system('test.exe') Assuming that test.exe returns "bar", I want the variable foo to be set to "bar". But what happens is, it prints "b...

Pythonic way to ignore for loop control variable

A Python program I'm writing is to read a set number of lines from the top of a file, and the program needs to preserve this header for future use. Currently, I'm doing something similar to the following: header = '' header_len = 4 for i in range(1, header_len): header += file_handle.readline() Pylint complains th...

Python control structure utilisation

This code seems to smell: result = None for item in list: if result is None: result = item.foo(args) else: if ClassFred.objects.get(arg1=result) < ClassFred.objects.get(arg1=item.foo(args)): result = item.foo(args) The smelliest part is the utility of 'result'. Would anyone be kind enough sniff it f...

Return value from thread

How do I get a thread to return a tuple or any value of my choice back to the parent in Python? ...

Should Pylons' development.ini be checked in?

I'm learning about Pylons and I've read a few tutorials, but none of them have addressed collaboration practices. Starting on a practice project. I'd like to keep my code in a revision-control system (Git, specifically) as if it were an open-source project with multiple developers, in order to practice that aspect of Pylons development ...

How to do a string replace in a urlencoded string

I have a string like x = "http://query.yahooapis.com/v1/public/yql?q=select%20owner%2Curls%20from%20flickr.photos.info%20where%20photo_id%3D'%s'&amp;format=json" If I do x % 10 that fails as there are %20f etc which are being treated as format strings, so I have to do a string conactination. How can I use normal string replacements here...

Use pyExcelerator to generate dynamic Excel file with Django. Ensure unique temporary filename.

I'd like to generate a dynamic Excel file on request from Django. The library pyExcelerator does this, but I haven't found any way to use the contents of the Excel file without generating a server-side temporary Excel file, reading it, using its contents and deleting it. The problem is that pyExcelerator only way to extract the contents...

IBoutlet with PyObjC and Interface Builder

I'm writing a simple OSX app using Python and PyObjC. I designed the settings dialog using Interface Builder and I use ibtool to compile it, then load it from Python. The problem is how to access the controls I have in this window from the Python code? I played around with iPhone development a bit before and I remember I need to have an ...

Unicode filenames on python 2.6 under Mac OS X

I'm using os.walk to create a list of all music files under a folder. Some of these filenames are non-ascii, for example: 01 空即是色.mp3 I'm using the mutagen library to parse metadata for this file, and it professes complete unicode support. The filename is being retrieved as unicode, and can be printed as unicode. However, no matte...

IronPython examples

Hi folks: I have heard IronPython for a long time, but never seen a real-life application using it. Do you provide some examples to see its power? Thanks. ...

Get data back from Jython scripts using JSR-223

Hello. I am using Jython 2.5.1 with JSR-223 (i.e. javax.script package) and I expect the last line of the Python script to be returned. For example, after evaluating this script: class Multiplier: def multiply(self, x, y): return x * y Multiplier().multiply(5, 7) I should get back 35, but I get null instead. In other hand it w...

How to compare datetime in Django?

Suppose I have: ds = datetime.datetime.now dd = Entry.objects.get(pk=id).pub_date How to compare 2 objects above? I want to get the time difference between them. Please help me solve this problem. Thank you very much ! ...