python

Validate (X)HTML in Python

What's the best way to go about validating that a document follows some version of HTML (prefereably that I can specify)? I'd like to be able to know where the failures occur, as in a web-based validator, except in a native Python app. ...

Why does Python's iter() on a mapping return iterkeys() instead of iteritems()?

It seems like if you want to get the keys of a mapping, you ask for them; otherwise, give me the whole mapping (constituted by a set of key-value pairs). Is there a historical reason for this? ...

Ruby "is" equivalent

Is there a Ruby equivalent for Python's "is"? It tests whether two objects are identical (i.e. have the same memory location). ...

Is Python good for big software projects (not web based)?

Right now I'm developing mostly in C/C++, but I wrote some small utilities in Python to automatize some tasks and I really love it as language (especially the productivity). Except for the performances (a problem that could be sometimes solved thanks to the ease of interfacing Python with C modules), do you think it is proper for produ...

Why is my instance variable not in __dict__?

If I create a class A as follows: class A: def _ _init_ _(self): self.name = 'A' Inspecting the _ _dict_ _ member looks like {'name': 'A'} If however I create a class B: class B: name = 'B' _ _dict_ _ is empty. What is the difference between the two, and why doesn't name show up in B's _ _dict_ _? ...

How to escape os.system() calls in Python?

When using os.system() it's often necessary to escape filenames and other arguments passed as parameters to commands. How can I do this? Preferably something that would work on multiple operating systems/shells but in particular for bash. I'm currently doing the following, but am sure there must be a library function for this, or at l...

Are there any "nice to program" GUI toolkits for Python?

I've played around with GTK, TK, wxPython, Cocoa, curses and others. They are are fairly horrible to use.. GTK/TK/wx/curses all seem to basically be direct-ports of the appropriate C libraries, and Cocoa basically mandates using both PyObjC and Interface Builder, both of which I dislike.. The Shoes GUI library for Ruby is great.. It's v...

Django Templates and variable attributes.

I'm using Google App Engine and Django Templates. I have a table that I want to display the objects look something like: Object Result: Items = [item1,item2] Users = [{name='username',item1=3,item2=4},..] The django template is: <table> <tr align="center"> <th>user</th> {% for item in result.items %} <th>{{item}}</th> {%...

C-like structures in Python

Is there a way to conveniently define a C-like structure in Python? I'm tired of writing stuff like: class MyStruct(): def __init__(self, field1, field2, field3) self.field1 = field1 self.field2 = field2 self.field3 = field3 (Interesting, it appears I can't type underscores following non-whitespace in pre-f...

How do I sort a list of strings in Python?

What is the best way of creating an alphabetically sorted list in Python? ...

"The system cannot find the file specified" when invoking subprocess.Popen in python

I'm trying to use svnmerge.py to merge some files. Under the hood it uses python, and when I use it I get an error - "The system cannot find the file specified". Colleagues at work are running the same version of svnmerge.py, and of python (2.5.2, specifically r252:60911) without an issue. I found this link, which describes my problem...

Unit tests in Python

Does Python have a unit testing framework compatible with the standard xUnit style of test framework? If so, what is it, where is it, and is it any good? ...

Setup django with WSGI and apache

I have been sold on mod_wsgi and apache rather than mod_python. I have all the parts installed (django, apache, mod_wsgi) but have run into a problem deploying. I am on osx 10.5 with apache 2.2 and django 1.0b2, mod_wsgi-2.3 My application is called tred. Here are the relevant files: httpd-vhosts (included in httpd-conf) NameVirtual...

How do I add data to an existing model in Django?

Currently, I am writing up a bit of a product-based CMS as my first project. Here is my question. How can I add additional data (products) to my Product model? I have added '/admin/products/add' to my urls.py, but I don't really know where to go from there. How would i build both my view and my template? Please keep in mind that I don'...

What does ** and * do for python parameters?

In the following method, what does the * and ** do for param2? I'm new to Python...I assume it's a reference or pointer. Answers? def foo(param1, *param2): def bar(param1, **param2): ...

What's the best way to implement an 'enum' in Python?

I'm mainly a C# developer, but I'm currently working on a project in Python. What's the best way to implement the equivalent of an enum in Python? ...

Resources for lexing, tokenising and parsing in python

Can people point me to resources on lexing, parsing and tokenising with Python? I'm doing a little hacking on an open source project (hotwire) and wanted to do a few changes to the code that lexes, parses and tokenises the commands entered into it. As it is real working code it is fairly complex and a bit hard to work out. I haven't w...

How to make Ruby or Python web sites to use multiple cores?

Even though Python and Ruby have one kernel thread per interpreter thread, they have a global interpreter lock (GIL) that is used to protect potentially shared data structures, so this inhibits multi-processor execution. Even though the portions in those languajes that are written in C or C++ can be free-threaded, that's not possible wit...

Best GUI Python Interpreter

When I'm trying to hack out an idea I find the Python interpreter is an invaluable to tool to quickly get a working prototype and iterate on it. I've been using IDLE bundled with Python on Windows and that's been working pretty well. I'm currently on a different system running Ubuntu and IDLE doesn't seem to work as well. The tooltips d...

How can I simply inherit methods from an existing instance?

Hi, below I have a very simple example of what I'm trying to do. I want to be able to use HTMLDecorator with any other class. Ignore the fact it's called decorator, it's just a name. import cgi class ClassX(object): pass # ... with own __repr__ class ClassY(object): pass # ... with own __repr__ inst_x=ClassX() inst_y=ClassY() ...