python

wxPython progress bar

I can't use wx.ProgressDialog because I need to add extra contents to the dialog box (a pause button and information about what is currently being processed). Is there a control for just the progress bar that I can use in my own dialog box? I could of course draw something simple myself, but since the program needs to run on Mac OS X, ...

Reading utf-8 characters from a gzip file in python

Hi, I am trying to read a gunzipped file (.gz) in python and am having some trouble. I used the gzip module to read it but the file is encoded as a utf-8 text file so eventually it reads an invalid character and crashes. Does anyone know how to read gzip files encoded as utf-8 files? I know that there's a codecs module that can help ...

AJAX URLs and GET requests

Ok, a great example of what I am trying to achieve is at Google Translate. The URL: http://translate.google.com/#en|es|this is what I am trying to do makes a GET request using this URL: http://translate.google.com/translate_a/t?client=t&text=this%20is%20what%20I%20am%20trying%20to%20do&sl=en&tl=es&otf=1&pc=0 I'm not ...

Linting Python: what is good?

Are there any good modules that you can run against your code to catch coding errors? I expected pylint to catch mistakes in the use of default arguments to functions like this: >>> def spam(eggs=[]): ... eggs.append("spam") ... return eggs but was disappointed to find them unreported. I am looking for something beyond PEP8 fo...

Find the nth occurrence of substring in a string

This seems like it should be pretty trivial, but I am new at Python and want to do it the most Pythonic way. I want to find the n'th occurrence of a substring in a string. There's got to be something equivalent to what I WANT to do which is mystring.find("substring", 2nd) How can you achieve this in Python? ...

Converting a list of lists to a tuple in Python

I have a list of lists (generated with a simple list comprehension): >>> base_lists = [[a, b] for a in range(1, 3) for b in range(1, 6)] >>> base_lists [[1,1],[1,2],[1,3],[1,4],[1,5],[2,1],[2,2],[2,3],[2,4],[2,5]] I want to turn this entire list into a tuple containing all of the values in the lists, i.e.: resulting_tuple = (1,1,1,2...

Named parameters with Python C API?

How can I simulate the following Python function using the Python C API? def foo(bar, baz="something or other"): print bar, baz (i.e., so that it is possible to call it via: >>> foo("hello") hello something or other >>> foo("hello", baz="world!") hello world! >>> foo("hello", "world!") hello, world! ) ...

CSV to JSON script

I took this script from here: import csv from itertools import izip f = open( '/django/sw2/wkw2/csvtest1.csv', 'r' ) reader = csv.reader( f ) keys = ( "firm_url", "firm_name", "first", "last", "school", "year_graduated" ) out = [] for property in reader: property = iter( property ) data = {} for key in keys: data[ key ...

Handling UTF-16 in a Django uploaded file

In my Django webapp, in one location users can upload a text file where each line contains a string which will be operated on - the file isn't being stored on the server or anything like that. My code looks like this: roFile = request.FILES['uploadFileName'] ros = roFile.read().strip() ros = ros.split('\n') ros = [t.strip() for t in ro...

When are property validations run in Google App Engine (GAE)?

So I was reading the following documentation on defining your own property types in GAE. I noticed that I could also include a .validate() method when extending a new Property. This validate method will be called "when an assignment is made to a property to make sure that it is compatible with your assigned attributes". Fair enough, but ...

An Exercise: map or reduce a map in Python without list comprehensions?

When I started writing this question, I didn't think of the easy solution with nested lists, but now anyway want to find one. Here's an ugly code: fun0( fun1(fun2(fun3(arg1))), fun1(fun2(fun3(arg4))), fun1(fun2(fun3(arg4))), fun1(fun2(fun3(arg4)))) Ouch! Names are given for examples. In the real application, their nam...

How important is it to check return values when using the Python C API?

It seems that everytime I call a function that returns a PyObject*, I have to add four lines of error checking. Example: py_fullname = PyObject_CallMethod(os, "path.join", "ss", folder, filename); if (!py_fullname) { Py_DECREF(pygame); Py_DECREF(os); return NULL; } image = PyObject_CallMethodObjArgs(pygame, "image.load", py_...

How to populate sqlite3 in django?

My plan is to collect data from websites in batches (lawyer bios from each firm's website; since they are all different, I will use a modified spider for each site) and convert each batch into a csv file; then to json; and then load it to database. So I will need to append each new file to the existing database. Please let me know how to...

Use Python's easy_install in intranet

Increasingly I found myself using tools based upon python, particularly that use installation processes involving easy_install. The trouble for me is that I am On an intranet with no internet access On windows (which always complicates things a little!) Any advice on how to setup easy_install on my intranet to make using python bas...

Set django-notification to be opt in rather than the default of opt out.

I'm using django-notification to allow my users to opt out of certain alerts I generate in my web-application. By default when I create a new notice type it is enabled rather than disabled In the users notification interface (checked) I'd like to make some alerts opt-in rather than the default of opt out. I've looked through the docs ...

Django loaddata error

I created a "fixtures" folder in the app directory and put data1.json in there. This is what is in the file: [{"firm_url": "http://www.graychase.com/kadam", "firm_name": "Gray & Chase", "first": " Karin ", "last": "Adam", "school": "Ernst Moritz Arndt University Greifswald", "year_graduated": " 2004"} ] In the command line I cd to t...

Lost connection to MySQL server during query

I have a huge table and I need to process all rows in it. I'm always getting this Lost connection message and I'm not able to reconnect and restore the cursor to the last position it was. This is basically the code I have here: # import MySQLdb class DB: conn = None def connect(self): self.conn = MySQLdb.connect('hostname', 'u...

Killing the child processes with the parent process

I have a program spawning and communicating with cpu heavy, unstable processes, not created by me. If my app crashes or is killed by sigkill, I want the subprocesses to get killed as well, so the user don´t have to track them down and kill them manually. I know this topic has been covered before, but I have tried all methods described, ...

Liteweight CGI Server to use on local machine to serve KML to Google Earth via Python or similar?

Greetings, I want to write a script that handles simple http requests from Google Earth and sends back KML to display map tiles that are stored locally. I would LIKE to use Python but any language is fine. I have not ever done anything with CGI, but I think this is the simplest way to accomplish my task. This is what the Google KML d...

How can I get optparse's OptionParser to ignore invalid arguments?

In python's OptionParser, how can I instruct it to ignore undefined flag arguments supplied to method parse_args? e.g. I've only defined option --foo for my OptionParser instance, but I call parse_args with list [ '--foo', '--bar' ] EDIT: I don't care if it filters them out of the original list. I just want undefined options ignored...