python

How should I unit test a code-generator?

This is a difficult and open-ended question I know, but I thought I'd throw it to the floor and see if anyone had any interesting suggestions. I have developed a code-generator that takes our python interface to our C++ code (generated via SWIG) and generates code needed to expose this as WebServices. When I developed this code I did i...

Using an XML catalog with Python's lxml?

Is there a way, when I parse an XML document using lxml, to validate that document against its DTD using an external catalog file? I need to be able to work the fixed attributes defined in a document’s DTD. ...

Can you check that an exception is thrown with doctest in Python?

Is it possible to write a doctest unit test that will check that an exception is raised? For example, if I have a function foo(x) that is supposed to raise an exception if x<0, how would I write the doctest for that? ...

What's a good starting development environment for teaching/learning Python?

I'm a seasoned .Net developer with a good chunk of experience on the MS stack and in related areas. I'm about to learn Python and was curious about the ideal development environment on Windows - IDE, tools etc. I'd normally be happy to go out there and have a play with what's available, adapting as I go along. I'd also likely switch bet...

Replacement for for... if array iteration

I love list comprehensions in Python, because they concisely represent a transformation of a list. However, in other languages, I frequently find myself writing something along the lines of: foreach (int x in intArray) if (x > 3) //generic condition on x x++ //do other processing This example is in C#, where I'm under the ...

Python version of PHP's stripslashes

I'm trying to condense this code: cleaned = stringwithslashes cleaned = cleaned.replace("\\n", "\n) cleaned = cleaned.replace("\\r", "\n) cleaned = cleaned.replace("\\", "") ...

Python Regular Expressions

I'm trying to implement string unescaping with Python regex and backreferences, and it doesn't seem to want to work very well. I'm sure it's something I'm doing wrong but I can't figure out what... >>> import re >>> mystring = r"This is \n a test \r" >>> p = re.compile( "\\\\(\\S)" ) >>> p.sub( "\\1", mystring ) 'This is n a test r' >>>...

Can you explain closures (as they relate to Python)?

I've been reading a lot about closures and I think I understand them, but without clouding the picture for myself and others, I was wondering if anyone can explain closures as succinctly and clearly as possible to me and others? I'm looking for a simple explanation that might help me wrap my head around why I should be using them in cer...

Python Sound ("Bell")

I'd like to have a python program alert me when it has completed its task by making a beep noise. Currently, I use import os and then use a command line speech program to say "Process complete." I much rather it be a simple "bell." I know that there's a function that can be used in Cocoa apps, NSBeep, but I don't think that has much ...

Is there a python module for regex match in zip files

I have over a million text files compressed into 40 zip files. I also have a list of about 500 model names of phones. I want to find out the number of times a particular model was mentioned in the text files. Is there any python module which can do a regex match on the files without unzipping it. Is there a simple way to solve this pro...

Regex and unicode

I have a script that parses the filenames of TV episodes (show.name.s01e02.avi for example), grabs the episode name (from the www.thetvdb.com API) and automatically renames them into something nicer (Show Name - [01x02].avi) The script works fine, that is until you try and use it on files that have Unicode show-names (something I never ...

Why is the PyObjC documentation so bad?

For example, http://developer.apple.com/cocoa/pyobjc.html is still for OS X 10.4 Tiger, not 10.5 Leopard.. And that's the official Apple documentation for it.. The official PyObjC page is equally bad, http://pyobjc.sourceforge.net/ It's so bad it's baffling.. I'm considering learning Ruby primarily because the RubyCocoa stuff is so muc...

How do I validate xml against a DTD file in Python

I need to validate an XML string (and not a file) against a DTD description file. How can that be done in python? ...

Useful code which uses reduce() in python

Do anyone here have any useful code which uses reduce() function of python? Is there any code other than the usual + and * that we see in the examples? Refer Fate of reduce() in Python 3000 by GvR ...

Prototyping hybrid Python code

I have been mulling over writing a peak fitting library for a while. I know Python fairly well and plan on implementing everything in Python to begin with but envisage that I may have to re-implement some core routines in a compiled language eventually. IIRC, one of Python's original remits was as a prototyping language, however Python ...

Sanitising user input using Python

What's the best way to sanitise user input for a Python-based web application? Is there a single function to remove HTML characters and any other necessary characters combinations to ensure that an XSS or SQL injection attack isn't possible? ...

Create an encrypted ZIP file in Python

I'm creating an ZIP file with ZipFile in Python 2.5, it works ok so far: import zipfile, os locfile = "test.txt" loczip = os.path.splitext (locfile)[0] + ".zip" zip = zipfile.ZipFile (loczip, "w") zip.write (locfile) zip.close() but I couldn't find how to encrypt the files in the ZIP file. I could use system and call PKZIP -s, but I ...

Best environment for Python on Windows?

So I've got plain python downloaded, so I can run .py files from the command line. Now I want to step it up, have a debugger, be able to call .net or other Windows things, etc... What's my next step? What's a good Python environment for Windows? ...

What's the best way to distribute python command-line tools?

This is my current setup.py script. It works okay, but it installs tvnamer.py (the tool) as "tvnamer.py" into site-packages or somewhere similar.. Can I make setup.py install tvnamer.py as tvnamer, and/or is there a better way of installing command-line applications? ...

How to learn Python?

For a beginner's perspective, can you answer the following? Best tool(s) for Python development (for example, Notepad and Vim) Best book to get started Best website for the Python beginner Best problem domain to do a sample project (what are Python-based solutions best suited for?) ...