python

Using a global dictionary with threads in Python

Is accessing/changing dictionary values thread-safe? I have a global dictionary foo and multiple threads with ids id1, id2, ... , idn. Is it OK to access and change foo's values without allocating a lock for it if it's known that each thread will only work with its id-related value, say thread with id1 will only work with foo[id1]? ...

Help with Python while loop behaviour

I have a script that uses a simple while loop to display a progress bar but it doesn't seem to be working as I expected: count = 1 maxrecords = len(international) p = ProgressBar("Blue") t = time while count < maxrecords: print 'Processing %d of %d' % (count, maxrecords) percent = float(count) / float(maxrecords) * 100 p.ren...

display a QMessageBox PyQT when a different combobox /list box item is selected

I have a combo box cbLayer and a function do_stuff of the following form: def do_stuff(item_selected_from_cbLayer): new_list = [] # do stuff based on item_selected_from_combobox and put the items in new_list return new_list How can I get a QMessageBox to pop up whenever a different item is selected in the following form: ...

Turbogears 2 Tutorials?

Anyone know of a good beginners tutorial for Turbogears 2? I'm particularly interested in one that would have some details on working with existing database schema rather than creating from scratch. (and some pointers on MS SQL server setup would help too!) ...

Python - ambiguity with decorators receiving a single arg

I am trying to write a decorator that gets a single arg, i.e @Printer(1) def f(): print 3 So, naively, I tried: class Printer: def __init__(self,num): self.__num=num def __call__(self,func): def wrapped(*args,**kargs): print self.__num return func(*args,**kargs**) ret...

Python: what kind of literal delimiter is "better" to use?

Hello everyone. What is the best literal delimiter in Python adn why? Single ' or double "? And most important, why? I'm a begginer in Python and i'm trying to stick with just one. I know that in PHP, for example " is preferred, because PHP does not try to search for the 'string' variable. Is the same case in Python? ...

Inserting python tuple in a MySQL database

Hi, I need to insert a python tuple (of floats) into a MySQL database. In principle I could pickle it and insert it as a string, but that would grant me the chance only to retrieve it through python. Alternative is to serialize the tuple to XML and store the XML string. What solutions do you think would be also possible, with an eye to...

porting python app to mobile platforms

Hello, I have a python app running fine on Windows, Linux and Mac which I would like to port to multiple mobile platforms such as Blackberry, Windows Mobile, Palm, Android and iPhone. I have couple of ideas: - port app to platform supporting some kind of Python like Andoid and Windows Mobile - port app to Java to target most platforms ...

Django without additional tables ?

Hi, is it possible to write Django apps, for example for internal/personal use with existing databases, without having the 'overhead' of Djangos own tables that are usually installed when starting a project ? I would like to use existing tables via models, but not have all the other stuff that is surely useful on normal webpages. The rea...

Where is the best place to put cache-evicting logic in an AppEngine application?

I've written an application for Google AppEngine, and I'd like to make use of the memcache API to cut down on per-request CPU time. I've profiled the application and found that a large chunk of the CPU time is in template rendering and API calls to the datastore, and after chatting with a co-worker I jumped (perhaps a bit early?) to the ...

Parsing FLV header (duration) of remote file in Java

I'm looking for an example of parsing an FLV header for duration specifically in Java. Given the URL of an FLV file I want to download the header only and parse out the duration. I have the FLV spec but I want an example. Python or PHP would be OK too but Java is preferred. ...

Simple noob python style question.

Which is preferred def method(self): or def method( self ): With spaces in the parenthesis ...

What is this construct called in python: ( x, y )

What is this called in python: [('/', MainPage)] Is that an array .. of ... erhm one dictionary? Is that () A tuple? ( or whatever they call it? ) ...

Raise exception vs. return None in Python functions

What's better practice in a user-defined function in Python: raise an exception or return None? For example, I have a function that finds the most recent file in a folder. def latestpdf(folder): # list the files and sort them try: latest = files[-1] except IndexError: # Folder is empty. return None ...

If I have the contents of a zipfile in a Python string, can I decompress it without writing it to a file?

I've written some Python code that fetches a zip file from the web and into a string: In [1]: zip_contents[0:5] Out[1]: 'PK\x03\x04\x14' I see there's a zipfile library, but I'm having trouble finding a function in it that I can just pass a bunch of raw zip data. It seems to want to read it from a file. Do I really need to dump this ...

How can Django projects be deployed with minimal installation work?

To deploy a site with Python/Django/MySQL I had to do these on the server (RedHat Linux): Install MySQLPython Install ModPython Install Django (using python setup.py install) Add some directives on httpd.conf file (or use .htaccess) But, when I deployed another site with PHP (using CodeIgniter) I had to do nothing. I faced some probl...

Using PyUNO on Windows and CentOS

Is there any way to use OpenOffice's PyUNO without using the version of Python that comes with OpenOffice? I mean, can I install a package (on Windows and CentOS) that uses the version of Python that's already on the server? I'm trying to use OpenOffice in headless mode so that I can do document conversion with a script (ultimately...

How can I scrape this frame?

If you visit this link right now, you will probably get a VBScript error. On the other hand, if you visit this link first and then the above link (in the same session), the page comes through. The way this application is set up, the first page is meant to serve as a frame in the second (main) page. If you click around a bit, you'll see...

Is there any way to get vim to auto wrap python strings at 79 chars?

I found this answer about wrapping strings using parens extremely useful, but is there a way in Vim to make this happen automatically? I want to be within a string, typing away, and have Vim just put parens around my string and wrap it as necessary. For me, this would be a gigantic time saver as I spend so much time just wrapping long ...

How can I filter items from a list in Python?

I have data naively collected from package dependency lists. Depends: foo bar baz >= 5.2 I end up with d = set(['foo','bar','baz','>=','5.2']) I don't want the numerics and the operands. In Perl I would @new = grep {/^[a-z]+$/} @old but I can't find a way to e.g. pass remove() a lambda, or something. The closest I've come is...