python

Appending turns my list to NoneType

In Python Shell, I entered: aList = ['a', 'b', 'c', 'd'] for i in aList: print(i) and got a b c d but when I tried: aList = ['a', 'b', 'c', 'd'] aList = aList.append('e') for i in aList: print(i) and got Traceback (most recent call last): File "<pyshell#22>", line 1, in <module> for ...

How to downcase the first character of a string in Python ?

Hello, There is a function to capitalize a string, I would like to be able to change the first character of a string to be sure it will be lowercase. How can I do that in Python ? ...

IPython threading server execfile()

My problem is the following: I would like to open an instance of ipython in a terminal of mine by simply typing, as usual: $ ipython -pylab and be able to have other processes order this ipython instance to execute a file as the execfile() would. This could be any other process, for instance, vim could ask ipython to run the currently...

Optimize two simple nested loops

I have been trying to optimize the two following nested loops: def startbars(query_name, commodity_name): global h_list nc, s, h_list = [], {}, {} query = """ SELECT wbcode, Year, """+query_name+""" FROM innovotable WHERE commodity='"""+commodity_name+"""' and """+query_name+""" != 'NU...

PyMongo: group with 2d geospatial index in conditions returns an error

The error returned is: exception: manual matcher config not allowed Here's my code: cond = {'id': id, 'date': {'$gte': start_date}, 'date': {'$lte': end_date}, 'location': {'$within': {'$box': box }}} reduce = 'function(obj, prev) { prev.count++; }' rows = collection.group({'location': True}, cond, {'count': 0}, reduce) When I remove...

Getting %def references in mako python

is there a way to use %def references somehow, basic idea being: % if condition_a: % func = %def_a % elif condition_b: % func = %def_b ... etc ... ${func( params )} ...

Compare Python Web Frameworks and their respective HTML5 APIs Implementations

If you are familiar with a specific python web framework that has implementations for HTML5 API(s) ie.WebSockets, Forms, WebWorkers, WebStorage, Communication, Geolocation, Canvas, etc. Then please list the name of the framework and its HTML5 capabilities. ...

Is there a way to make the Tkinter text widget read only?

It doesn't look like it has that attribute, but it'd be really useful to me. ...

Python - Most efficient way to find how often each possible pair of words occurs in the same line in a text file?

Hi all, This particular problem is easy to solve, but I'm not so sure that the solution I'd arrive at would be computationally efficient. So I'm asking the experts! What would be the best way to go through a large file, collecting stats (for the entire file) on how often two words occur in the same line? For instance, if the text cont...

MakeTkinter take focus

I have a script that uses Tkinter to pop up a window with a message. How do I make sure it takes focus so the user doesn't miss it and explicitly has to dismis the window. the code is : root = Tk() to_read = "Stuff" w = Label(root, text=to_read) w.pack() root.mainloop() Thanks. ...

Parallel Processing in python

Whats a simple code that does parallel processing in python 2.7? All the examples Ive found online are convoluted and include unnecessary codes. how would i do a simple brute force integer factoring program where I can factor 1 integer on each core (4)? my real program probably only needs 2 cores, and need to share information. I know ...

How to diagnose reason for slow access to sqlite database?

As I have stated in this question some time ago, I am having performance problems when accessing an sqlite database from Python. To make that clear once more, the identical code runs more than 20 times faster using apsw. I recently installed a different version of Python in parallel and installed a new version of apsw for that. This vers...

Python: callback when accessing the value of a variable?

Suppose a function f() returns a value of arbitrary type, perhaps an object but possibly even a builtin type like int or list. Is there a way to assign a variable to that return value, and have a function of my choosing be called the first time the variable is used? I suppose this is similar to lazy evaluation except that a reference to...

Is there a way to cleanly exit out of a thread which is processing data from a (never-ending) generator?

Hello all, Here's the issue: I have a thread which runs a for-loop reading from a generator, doing some processing on that data, etc.. The generator always has data coming in, so no StopIteration exception is ever raised by it. I would like to stop this thread (cleanly) from the main thread (i.e., exit out of the for-loop which is pro...

Python lookup function

I want to set up a lookup function with mako. on top of the template, i have <%! lookup = { 'key': function } %> <%def name="function()"> Output </%def> so i can use it later <%def name="body()"> ${lookup['key']()} </%def> this gives me a function is not a defined error. can i get around this? i know why it doesn'...

How do I get a size of an UTF-8 string in Bytes with Python

Having an UTF-8 string like this: mystring = "işğüı" is it possible to get its (in memory) size in Bytes with Python (2.5)? ...

Why does Tkinter frame resize when text box is added to it?

With this code, the window is 500 by 500, which is what I'm going for: from tkinter import * root = Tk() frame = Frame(root, width=500, height=500) frame.pack() root.mainloop() When I add a text box to the frame, though, it shrinks to just the size of the text box: from tkinter import * root = Tk() frame = Frame(root, width=500, h...

PyQT and threads

I am developing an application that uses multiple threads to gather data from a variety of network devices. I'm using PyQT to display the collected data on a GUI. I am using regular python threads (from thread, threading) in my app (instead of QThread). In order to update the GUI on the different threads, I use a lock (thread.allocate_l...

gtk: why does my treeview sort func receive a row with None in it?

I've set up a gtk.TreeView with a gtk.TreeStore. One column contains formatted dollar amounts, and I've set up sorting by that column as follows: def sortmon(model, i1, i2): v1 = model[i1][COL_MONEY] v2 = model[i2][COL_MONEY] return cmp(float(v1.replace("$","").replace(",","")), float(v2.re...

Organizing Python classes in modules and/or packages

I like the Java convention of having one public class per file, even if there are sometimes good reasons to put more than one public class into a single file. In my case I have alternative implementations of the same interface. But if I would place them into separate files, I'd have redundant names in the import statements (or misleading...