python

What is best way to remove duplicate lines matching regex from string using Python?

This is a pretty straight forward attempt. I haven't been using python for too long. Seems to work but I am sure I have much to learn. Someone let me know if I am way off here. Needs to find patterns, write the first line which matches, and then add a summary message for remaining consecutive lines which match pattern and return modified...

Naming conventions in a Python library

Hello, I'm implementing a search algorithm (let's call it MyAlg) in a python package. Since the algorithm is super-duper complicated, the package has to contain an auxiliary class for algorithm options. Currently I'm developing the entire package by myself (and I'm not a programmer), however I expect 1-2 programmers to join the project l...

Django: How do I create a generic url routing to views?

I have a pretty standard django app, and am wondering how to set the url routing so that I don't have to explicitly map each url to a view. For example, let's say that I have the following views: Project, Links, Profile, Contact. I'd rather not have my urlpatterns look like this: (r'^Project/$', 'mysite.app.views.project'), (r'^Links/...

Initializing cherrypy.session early

I love CherryPy's API for sessions, except for one detail. Instead of saying cherrypy.session["spam"] I'd like to be able to just say session["spam"]. Unfortunately, I can't simply have a global from cherrypy import session in one of my modules, because the cherrypy.session object isn't created until the first time a page request is ma...

How do I build and install P4Python for Mac OS X?

I've been unable to build P4Python for an Intel Mac OS X 10.5.5. These are my steps: I downloaded p4python.tgz (from http://filehost.perforce.com/perforce/r07.3/tools/) and expanded it into "P4Python-2007.3". I downloaded p4api.tar (from http://filehost.perforce.com/perforce/r07.3/bin.macosx104x86/) and expanded it into "p4api-2007.3....

How do you get a directory listing sorted by creation date in python?

What is the best way to get a list of all files in a directory, sorted by date [created | modified], using python, on a windows machine? ...

Python - How do I convert "an OS-level handle to an open file" to a file object?

tempfile.mkstemp() returns "a tuple containing an OS-level handle to an open file (as would be returned by os.open()) and the absolute pathname of that file, in that order." How do I convert that OS-level handle to a file object? The documentation for os.open() states: To wrap a file descriptor in a "file object", use fdopen(). ...

Now that Python 2.6 is out, what modules currently in the language should every programmer know about?

A lot of useful features in Python are somewhat "hidden" inside modules. Named tuples (new in Python 2.6), for instance, are found in the collections module. The Library Documentation page will give you all the modules in the language, but newcomers to Python are likely to find themselves saying "Oh, I didn't know I could have done it ...

Python - How do I write a decorator that restores the cwd?

How do I write a decorator that restores the current working directory to what it was before the decorated function was called? In other words, if I use the decorator on a function that does an os.chdir(), the cwd will not be changed after the function is called. ...

How can I compress a folder and email the compressed file in Python?

I would like to compress a folder and all its sub-folders/files, and email the zip file as an attachment. What would be the best way to achieve this with Python? ...

2D animation in Python

I'm writing a simulator in Python, and am curious about options and opinions regarding basic 2D animations. By animation, I'm referring to rendering on the fly, not displaying prerendered images. I'm currently using matplotlib (Wxagg backend), and it's possible that I'll be able to continue using it, but I suspect it won't be able to su...

How to package Twisted program with py2exe?

Hi, I tried to package a Twisted program with py2exe, but once I run the exe file I built, I got a "No module named resource" error. And I found the py2exe said: The following modules appear to be missing ['FCNTL', 'OpenSSL', 'email.Generator', 'email.Iterators', 'email.Utils', 'pkg_resources', 'pywintypes', 'resource', 'win32api',...

What to include in a beginner's programming book?

As a follow-up to my previous question, what sort of information should I make sure to include in a tutorial book and what should I avoid? In other words, what would you have liked to have known when first starting with Python or wxPython? Right now I am just planning on restructing the tutorials I have already created and other editing...

USB Driver Development on a Mac using Python

I would like to write a driver to talk to my Suunto t3 watch in Python on a Mac. My day job is doing basic web work in C# so my familiarity with Python and developing on a Mac is limited. Can you suggest how one would start doing driver development in general and then more specifically on a Mac. I.e. how to easily see what data is being...

Django signals vs. overriding save method

I'm having trouble wrapping my head around this. Right now I have some models that looks kind of like this: def Review(models.Model) ...fields... overall_score = models.FloatField(blank=True) def Score(models.Model) review = models.ForeignKey(Review) question = models.TextField() grade = models.IntegerField() A ...

How do I write to a log from mod_python under apache?

I seem to only be able to write to the Apache error log via stderr. Anyone know of a more structured logging architecture that I could use from my python web project, like commons? ...

Python 3 performance?

No doubt most people have read all about the various incompatible changes that are going into Python 3, so on a question of just performance, how does python 3 compare to python 2.x? ...

When will most libraries be Python 3 compliant?

Does anyone have an idea how long it will take before "almost all" widely used Python libraries work with Python 3.0? I am planning to stay on 2.X for a while because I don't want to start porting stuff over and then find that some library I already rely on or a new library I might start using doesn't work yet with py3k. My understandi...

To learn python 2 then 3, or 3 from the start?

I would like to learn python and currently have access to some good python 2 books. However python 3 is not guaranteed to be backward compatible with python 2. If I were to learn python 2 I have to unlearn and relearn some things when I upgrade to python 3. Also, is there any use/point in learning python 2 or should I just learn python 3...

UNIX shell written in a reasonable language?

Has anyone ever heard of a UNIX shell written in a reasonable language, like Python? ...