python

Decorators and in class

Is there any way to write decorators within a class structure that nest well? For example, this works fine without classes: def wrap1(func): def loc(*args,**kwargs): print 1 return func(*args,**kwargs) return loc def wrap2(func): def loc(*args,**kwargs): print 2 return func(*args,**kwargs) return lo...

Python source header comment

What is the line #!/usr/bin/env python in the first line of a python script used for? ...

Create a standalone windows exe which does not require pythonXX.dll

Hello, is there a way to create a standalone .exe from a python script. Executables generated with py2exe can run only with pythonXX.dll. I'd like to obtain a fully standalone .exe which does not require to install the python runtime library. It looks like a linking problem but using static library instead the dynamic one and it would be...

In Python how can I access "static" class variables within class methods

If I have the following python code: class Foo(object): bar = 1 def bah(self): print bar f = Foo() f.bah() It complains NameError: global name 'bar' is not defined How can I access class/static variable 'bar' within method 'bah'? ...

questions re: current state of GUI programming with Python

I recently did some work modifying a Python gui app that was using wxPython widgets. I've experimented with Python in fits and starts over last six or seven years, but this was the first time I did any work with a gui. I was pretty disappointed at what seems to be the current state of gui programming with Python. I like the Python lan...

How to compare type of an object in Python?

Basically I wanna do: obj = 'str' type ( obj ) == string I tried: type ( obj ) == type ( string ) and didn't work. EDIT: Thanks. Also what about the other types? Like there was NoneType that I couldn't replicate. ...

web.py: passing initialization / global variables to handler classes?

I'm attempting to use web.py with Tokyo Cabinet / pytc and need to pass the db handle (the connection to tokyo cabinet) to my handler classes so they can talk to tokyo cabinet. Is there a way to pass the handler to the handler class's init function? Or should I be putting the handle in globals() ? What is globals() and how do you use i...

Python Bayesian text classification modules

A quick Google search reveals that there are a good number of Bayesian classifiers implemented as Python modules. If I want wrapped, high-level functionality similar to dbacl, which of those modules is right for me? Training % dbacl -l one sample1.txt % dbacl -l two sample2.txt Classification % dbacl -c one -c two sample3.txt -v one...

Debugging a running python process

Is there a way to see a stacktrace of what various threads are doing inside a python process? Let's suppose I have a thread which allows me some sort of remote access to the process. ...

Python SAX parser says XML file is not well-formed.

I stripped some tags that I thought were unnecessary from an XML file. Now when I try to parse it, my SAX parser throws an error and says my file is not well-formed. However, I know every start tag has an end tag. The file's opening tag has a link to an XML schema. Could this be causing the trouble? If so, then how do I fix it? Edit: I ...

Python comments: # vs strings

I have a question regarding the "standard" way to put comments inside Python source code: def func(): "Func doc" ... <code> 'TODO: fix this' #badFunc() ... <more code> def func(): "Func doc" ... <code> #TODO: fix this #badFunc() ... <more code> I prefer to write general comments as strings inst...

SQLAlchemy - INSERT OR REPLACE equivalent

Hi, does anybody know what is the equivalent to SQL "INSERT OR REPLACE" clause in SQLAlchemy and its SQL expression language? Many thanks -- honzas ...

How Much Traffic Can Shared Web Hosting (for a Python Django site) support?

Someone in this thread http://stackoverflow.com/questions/685162/how-much-traffic-can-shared-web-hosting-take stated that a $5/mo shared hosting account on Reliablesite.net can support 10,000 - 20,000 unique users/day and 100,000 - 200,000 pageviews/day. That seems awfully high for a $5/mo account. And someone else told me it's far le...

Why do languages like Java use hierarchical package names, while Python does not?

I haven't done enterprise work in Java, but I often see the reverse-domain-name package naming convention. For example, for a Stack Overflow Java package you'd put your code underneath package com.stackoverflow. I just ran across a Python package that uses the Java-like convention, and I wasn't sure what the arguments for and against it...

How to tell the difference between an iterator and an iterable?

In Python the interface of an iterable is a subset of the iterator interface. This has the advantage that in many cases they can be treated in the same way. However, there is an important semantic difference between the two, since for an iterable __iter__ returns a new iterator object and not just self. How can I test that an iterable is...

What is the best way to toggle python prints?

I'm running Python 2.4 in a game engine and I want to be able to turn off all prints if needed. For example I'd like to have the prints on for a debug build, and then turned off for a release build. It's also imperative that it's as transparent as possible. My solution to this in the C code of the engine is having the printf function in...

What's a more elegant rephrasing of this cropping algorithm? (in Python)

I want to crop a thumbnail image in my Django application, so that I get a quadratic image that shows the center of the image. This is not very hard, I agree. I have already written some code that does exactly this, but somehow it lacks a certain ... elegance. I don't want to play code golf, but there must be a way to express this shor...

Import an existing python project to XCode

I've got a python project I've been making in terminal with vim etc.. I've read that XCode supports Python development at that it supports SVN (which I am using) but I can't find documentation on how to start a new XCode project from an existing code repository. Other developers are working on the project not using XCode - They won't mi...

python code convention using pylint

I'm trying out pylint to check my source code for conventions. Somehow some variable names are matched with the regex for constants (const-rgx) instead of the variable name regex (variable-rgx). How to match the variable name with variable-rgx? Or should I extend const-rgx with my variable-rgx stuff? e.g. C0103: 31: Invalid name "settin...

Python server side AJAX library?

I want to have a browser page that updates some information on a timer or events. I'd like to use Python on the server side. It's quite simple, I don't need anything massively complex. I can spend some time figuring out how to do all this the "AJAX way", but I'm sure someone has written a nice Python library to do all the heavy lifting....