python

Using property() on classmethods

I have a class with two class methods (using the classmethod() function) for getting and setting what is essentially a static variable. I tried to use the property() function with these, but it results in an error. I was able to reproduce the error with the following in the interpreter: >>> class foo(object): ... _var=5 ... de...

Doing CRUD in Turbogears

Are there any good packages or methods for doing extensive CRUD (create-retrieve-update-delete) interfaces in the Turbogears framework. The FastDataGrid widget is too much of a black box to be useful and CRUDTemplate looks like more trouble than rolling my own. Ideas? Suggestions? ...

Does Django support multi-value cookies?

I'd like to set a cookie via Django with that has several different values to it, similar to .NET's HttpCookie.Values property. Looking at the documentation, I can't tell if this is possible. It looks like it just takes a string, so is there another way? I've tried passing it an array ([10, 20, 30]) and dictionary ({'name': 'Scott', 'id...

Perl or Python script to remove user from group

I am putting together a Samba-based server as a Primary Domain Controller, and ran into a cute little problem that should have been solved many times over. But a number of searches did not yield a result. I need to be able to remove an existing user from an existing group with a command line script. It appears that the usermod easily ...

Generic Exception Handling in Python the "Right Way"

Sometimes I find myself in the situation where I want to execute several sequential commands like such: try: foo(a, b) except Exception, e: baz(e) try: bar(c, d) except Exception, e: baz(e) ... This same pattern occurs when exceptions simply need to be ignored. This feels redundant and the excessive syntax causes it t...

How do you test that a Python function throws an exception?

How does one write a test that fails only if a function doesn't throw an expected exception? ...

Getting Python to use the ActiveTcl libraries

Is there any way to get Python to use my ActiveTcl installation instead of having to copy the ActiveTcl libraries into the Python/tcl directory? ...

Book and tutorial recommedations for Django 1.0

Please share your book or tutorial recommendations for the latest release (1.0) of Django. ...

Is there an inverse function for time.gmtime() that parses a UTC tuple to seconds since the epoch ?

python's time module seems a little haphazard. For example, here is a list of methods in there, from the docstring: time() -- return current time in seconds since the Epoch as a float clock() -- return CPU time since process start as a float sleep() -- delay for a number of seconds given as a float gmtime() -- convert seconds since Epo...

How do I efficiently filter computed values within a Python list comprehension?

The Python list comprehension syntax makes it easy to filter values within a comprehension. For example: result = [x**2 for x in mylist if type(x) is int] Will return a list of the squares of integers in mylist. However, what if the test involves some (costly) computation and you want to filter on the result? One option is: result...

How to pass all Visual Studio 2008 "Macros" to Python script?

[NOTE: This questions is similar to but not the same as this one.] Visual Studio defines several dozen "Macros" which are sort of simulated environment variables (completely unrelated to C++ macros) which contain information about the build in progress. Examples: ConfigurationName Release TargetPath D:\work\foo\win\R...

Python Date Comparisons

I would like to find out if a particular python datetime object is older than X hours or minutes. I am trying to do something similar to: if (datetime.now() - self.timestamp) > 100 # Where 100 is either seconds or minutes but this obviously gives a type error. So my question is what is the proper way to do date time comparison in py...

Request UAC elevation from within a Python script?

I want my Python script to copy files on Vista. When I run it from a normal cmd.exe window, no errors are generated, yet the files are NOT copied. If I run cmd.exe "as administator" and then run my script, it works fine. This makes sense since UAC normally prevents many file system actions. Is there a way I can, from within a Python ...

Style - When to serialize a Django model Instance: signals vs model's save method

I plan to serialize a Django model to XML when it's saved or updated. (The XML's going to be imported into a flash movie). Is it better to listen for a post_save() or pre_save() signal and then perform the serialization, or to just handle it in the model's save() methon ...

Do I have to cause an ValueError in Python

I have this code: chars = #some list try: indx = chars.index(chars) except ValueError: #doSomething else: #doSomethingElse I want to be able to do this because I don't like knowfully causing Exceptions: chars = #some list indx = chars.index(chars) if indx == -1: #doSomething else: #doSomethingElse Is there a wa...

Python Reporting

Are there any packages that provide to following: Integration with python application with minimal dependencies (beyond existing depends we use such as wx and matplotlib) Visual design tool to make it easier for less technical users to design their own reports Able to work with MySQL and Oracle databases Cross platform (Linux and Windo...

Getting stack trace from a running Python application

I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that he's running (somekind of stacktrace on-the-fly)? ...

Why do you like Python?

I have to make a presentation at work to convince everyone why they should try coding in Python. So, I thought of taking a poll here... What is it about Python (features, etc) over other languages that you love? The reason I usually give is that in Python you forget about the complexities and frills of programming languages and can jus...

Translate algorithmic C to Python

I would like to translate some C code to Python code or bytecode. The C code in question is what i'd call purely algorithmic: platform independent, no I/O, just algorithms and in-memory data structures. An example would be a regular expression library. Translation tool would process library source code and produce a functionally equival...

Regex to remove conditional comments

I want a regex which can match conditional comments in a HTML source page so I can remove only those. I want to preserve the regular comments. I would also like to avoid using the .*? notation if possible. The text is foo <!--[if IE]> <style type="text/css"> ul.menu ul li{ font-size: 10px; font-weight:normal; padding-...