python

gtk.treeviewcolumn not sorting

I've gotten a sortable treeview working. Clicking on the columns makes it sort by ascending order, and clicking it again makes it sort by descending order. However, if I click on the column header a third time, it goes to some "unsorted" state, instead of back to ascending. I connected a function to the clicked signal of the column, and ...

How do I use subprocess to tar & compress a folder?

I have used the below code, it can compress folders. But when I uncompress folders, it will throw an error. I think there is something wrong with my code. Any advice is welcome. def compress_index(): "将/var/solr/core0/data索引目录进行打包压缩至同目录下,以该日时间命名的data.tar.gz.20101013形式的压缩包" parentdirs,folder=os.path.split(g_index_path) tar_fi...

Importing nested modules in Python

I'm trying to import a few libraries into my program (which is a google AppEngine application). Basically, I'm supposed to put all libraries in the root folder, but I've just created another folder called lib and placed them within that folder. (I've created the __init__.py) Imports regularly work fine by using the import lib.module o...

Python appending dictionary, TypeError: unhashable type ??

abc = {} abc[int: anotherint] Then the error came up. TypeError: unhashable type? Why I received this? I've tried str() ...

What does |= (ior) do in Python?

Google won't let me search |= so I'm having trouble finding relevant documentation. Anybody know? ...

Insert multiple tab-delimited text files into MySQL with Python?

Hi everyone, I am pretty new to Python programing and programing in general so bare with me. I am trying to create a program that takes a number of tab delaminated text files, and works through them one at a time entering the data they hold into a MySQL database. There are several text files, like movies.txt which looks like this: 1 ...

UTF-8 compatible compression in python

I'd like to include a large compressed string in a json packet, but am having some difficulty. import json,bz2 myString = "A very large string" zString = bz2.compress(myString) json.dumps({ 'compressedData' : zString }) which will result in a UnicodeDecodeError: 'utf8' codec can't decode bytes in position 10-13: invalid data An...

Is it possible to use reflection to examine a function's decorators in Python 2.5?

This is what i want to do: @MyDecorator def f(): pass for d in f.decorators: print d ...

Python: Parse stream title with mplayer

I'm writing a simple frontend in Python to play and record internet radio channels (e.g. from shoutcast) using mplayer (in a subprocess). When a user clicks a station the following code is run: url = http://77.111.88.131:8010 # only an example cmd = "mplayer %s" % url p = subprocess.Popen(cmd.split(), shell=False) wait = os.waitpid(p....

Subprocess module errors with 'export' in python on linux?

I'm setting up a program to connect my computer to our schools proxy and currently have something like this: import subprocess import sys username = 'fergus.barker' password = '*************' proxy = 'proxy.det.nsw.edu.au:8080' options = '%s:%s@%s' % (username, password, proxy) subprocess.Popen('export http_proxy=' + options) But up...

Python Unicode CSV export (using Django)

Hi All, I'm using a Django app to export a string to a CSV file. The string is a message that was submitted through a front end form. However, I've been getting this error when a unicode single quote is provided in the input. UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 200: ordinal not in rang...

Making Menu options with checkbutton in Tkinter (python

I am making a Menu using Tkinter but i wanted to put "add_checkbutton" instead of "add_command"into the menu options but problem is "How i deselect/select a checkbox." menu = Menu(parent) parent.config(menu=menu) viewMenu = Menu(menu) menu.add_cascade(label="View", menu=viewMenu) viewMenu.add_command(label = "Show All", command=self....

Multiple Static Directories in Python Tornado

I have a directory structure setup like: root/ js/ css/ libs/ index.html From Tornado, I want to serve js, css, and libs as static directories, but I can only find out how to serve one of them. Can this be done? ...

Does Python have an "or equals" function like ||= in Ruby?

If not, what is the best way to do this? Right now I'm doing (for a django project): if not 'thing_for_purpose' in request.session: request.session['thing_for_purpose'] = 5 but its pretty awkward. In Ruby it would be: request.session['thing_for_purpose'] ||= 5 which is much nicer. ...

In python, any elegant way to refer to class method within the classes declaration scope?

The below code works both under Python 2.6 and 3.1, but the third lambda of SomeObject.columns is a bit silly, serving no real purpose but to prevent the reference to SomeObject.helper_function from being looked at before the class declaration finishes. It seems like a hack. If I remove the lambda, and replace it with just SomeObject.hel...

Subclassing file class in Python raises NameError

I have to do a very simple project in python where I add error checking to the built in file class. So far, I've got: class RobustFile(file): def __init__(self,name,mode): file.__init__(self,name,mode) I'm just starting out, but to make sure I hadn't messed anything up, I ran it. Well, right off the bat, I raised a NameE...

Python if vs try-except

I was wondering why the try-except is slower than the if in the program below. def tryway(): try: while True: alist.pop() except IndexError: pass def ifway(): while True: if alist == []: break else: alist.pop() if __name__=='__main__': from timeit imp...

Why an error in nosetests and not in Eclipse?

I'm using a third-party library which needs urlfetch from google.appengine.api. It is imported into the executing tests using this line: from google.appengine.api import urlfetch The google_appengine directory is on my PYTHONPATH, and if I execute my unit tests directly from Eclipse, I see no errors. However, if I use nosetests, I se...

How can you manually render a form field with its initial value set?

I'm trying to render a form's fields manually so that my designer colleagues could manipulate the input elements within the HTML instead of struggling in Python source. ie. Instead of declaring form fields like this... {{ form.first_name }} .. I actually do ... <label for="id_first_name">Your name:</label>...

Twitter Authentication Questions

I have two questions (does that violate etiquette?) surrounding Twitter authentication. The first question is this. I'd like to store the access token that I receive but it is a dictionary object. Do I store the whole dictionary object or just some of the pertinent parts. Secondly I'd like to know how to log the user out. I found this ...