python

Search backward through a string using a regex (in Python)?

Context I'm parsing some code and want to match the doxygen comments before a function. However, because I want to match for a specific function name, getting only the immediately previous comment is giving me problems. Current Approach import re function_re = re.compile( r"\/\*\*(.+)\*\/\s*void\s+(\w+)\s*::\s*function_name\s*\...

In Python, how do I do a string replacement AND retrive the replaced substring?

In Perl, I would write: $x = "abbbc"; $x =~ s/(b+)/z/; print "Replaced $1 and ended up with $x\n"; # "Replaced bbb and ended up with azc" How do I do this in Python -- do a regular-expression string replacement and record what it was that got replaced? ...

Creating temporary user accounts - Django

Hi folks, I need to setup temporary User models for each visitors, where the visitors are obviously tied by session data. I might not be aware of it, but does Django support attaching data to Anonymous users? The only way, I am currently aware of, is to use the session dictionary part of the request object. Help would be very muc...

Python: Creating directories

I want to create a directory (named 'downloaded') on in my desktop directory; isn't this working?: import os os.mkdir('~/Desktop/downloaded/') ...

Is it inefficient to access a python class member container in a loop statement?

I'm trying to adopt some best practices to keep my python code efficient. I've heard that accessing a member variable inside of a loop can incur a dictionary lookup for every iteration of the loop, so I cache these in local variables to use inside the loop. My question is about the loop statement itself... if I have the following class:...

How do I install boto?

So that I am able to work with it within my python scripts? ...

Why does socket.inet_ntoa returns packed format in python

Why does socket.inet_aton returns packed format in python? If I am storing the IP as integer in Database (mysql), do I have to always extract the integer value or is there any easier way out? ...

Problem importing modul2s from boto

I have installed boto like so: python setup.py install; and then when I launch my python script (that imports moduls from boto) on shell, an error like this shows up: ImportError: No module named boto.s3.connection How to settle the matter? ...

difference between len() and .__len__()?

Just curious, Is there any difference between calling len([1,2,3]) or [1,2,3].__len__() ? If there is no apparent difference, what is done differently behind the scenes? Thanks ...

Mocking ImportError in Python

I'm trying this for almost two hours now, without any luck. I have a module that looks like this: try: from zope.component import queryUtility # and things like this except ImportError: # do some fallback operations <-- how to test this? Later in the code: try: queryUtility(foo) except NameError: # do some fallback ...

Start Python from Twisted

Hi all, I have learnt Python for about a month as a one year's PHPer.And I started from Twisted as I'm working in a corporation supplying webservice.I have finished some simple application such as data transferring service,page images-fetch service etc.But the problem is ,I don't understand the struture of codes I wrote in the programs ...

Is it bad practice to extend the MongoEngine User document?

I'm integrating MongoDB using MongoEngine. It provides auth and session support that a standard pymongo setup would lack. In regular django auth, it's considered bad practice to extend the User model since there's no guarantee it will be used correctly everywhere. Is this the case with mongoengine.django.auth? If it is considered bad...

Aggregation over a few models - Django

Hi folks, I'm trying to compute the average of a field over various subsets of a queryset. Player.objects.order_by('-score').filter(sex='male').aggregate(Avg('level')) This works perfectly! But... if I try to compute it for the top 50 players it does not work. Player.objects.order_by('-score').filter(sex='male')[:50].aggregate(A...

How to create instances of related models in Django

I'm working on a CMSy app for which I've implemented a set of models which allow for creation of custom Template instances, made up of a number of Fields and tied to a specific Customer. The end-goal is that one or more templates with a set of custom fields can be defined through the Admin interface and associated to a customer, so that ...

How to read line by line in pdf file using PyPdf ???

hi, i working with pdf files, and i have some code to read from a pdf file,, but is there a way to read line by line from the pdf file( not pages ) ,, using Pypdf,, Python 2.6 ,, on windows ??? here is the code for reading the pdf pages:: import pyPdf def getPDFContent(path): content = "" num_pages = 10 p = file(path, "rb...

How do I use py2exe with paver?

I'm using paver to build my Python application, and I'd like to create an executable using py2exe. I've got the following in my pavement.py: from paver.setuputils import setup from distutils.core import setup import py2exe import paver paver.setuputils.install_distutils_tasks() ... but when I run paver py2exe I get "Build failed: py...

Convert the mysql table details to an Excel (.xls) or comma separated file (.csv) using Python

Hi, I want to convert the mysql database table contents to an Excel(.xls) or comma separated file(csv) using python script... Is it possible? Any one can help me? Thanks in advance, Nimmy ...

python metaprogramming

I'm trying to archive a task which turns out to be a bit complicated since I'm not very good at Python metaprogramming. I want to have a module locations with function get_location(name), which returns a class defined in a folder locations/ in the file with the name passed to function. Name of a class is something like NameLocation. So...

GetLastInputInfo equivalent in Linux to detect last input time

Is there a GetLastInputInfo() equivalent that can be used in Linux? The intention is to detect the last input time (keyboard or mouse) of the user. Am using python to script the program. ...

Send an email using python script

Hi, Today I needed to send email from a Python script. As always I searched Google and found the following script that fits to my need. import smtplib SERVER = "localhost" FROM = "[email protected]" TO = ["[email protected]"] # must be a list SUBJECT = "Hello!" TEXT = "This message was sent with Python's smtplib." # Prepare actual...