python

DOCTEST==argv[0] as a convention?

In a bit of Python I'm writing (a command line and filter testing tool: claft) I wanted a simple way to invoke the built-in test suite (doctest) and I decided on the following: if 'DOCTEST' in os.environ and os.environ['DOCTEST']==sys.argv[0]: _runDocTests() sys.exit() Thus if the DOCTEST variable is set for some other program...

How to get the desktop resolution in Mac via Python?

Trying to write a python application that downloads images from an RSS feed, and makes a composite background. How do I get the current desktop resolution on Mac OS X (leopard?) ...

Accessing elements with offsets in Python's for .. in loops

I've been mucking around a bit with Python, and I've gathered that it's usually better (or 'pythonic') to use for x in SomeArray: rather than the more C-style for i in range(0, len(SomeArray)): I do see the benefits in this, mainly cleaner code, and the ability to use the nice map() and related functions. However, I am quite often ...

Making sure that psycopg2 database connection alive.

Hi folks, I have a python application that opens a database connection that can hang online for an hours, but sometimes the database server reboots and while python still have the connection it won't work with OperationalError exception. So I'm looking for any reliable method to "ping" the database and know that connection is alive. I'...

Copying files to directories as specified in a file list with python

I have a bunch of files in a single directory that I would like to organize in sub-directories. This directory structure (which file would go in which directory) is specified in a file list that looks like this: Directory: Music\ -> 01-some_song1.mp3 -> 02-some_song2.mp3 -> 03-some_song3.mp3 Directory: Images\ -> 01-some_i...

Program invoking: at least one command line parameter

I'm trying to check that a user of this code runs this program from command line using only one command line parameter (the name of the file). When I run this code like this (I'm on windows) C:\>python program.py Usage: program.py <file.txt> C:\> Right. But when I run this program using a file I want to manipulate, I get nothing print...

Handling both SSL and non-SSL connections when inheriting from httplib.HTTP(s)Connection

I have a class that inherits from httplib.HTTPSConnection. class MyConnection(httplib.HTTPSConnection): def __init__(self, *args, **kw): httplib.HTTPSConnection.__init__(self,*args, **kw) ... Is it possible to turn off the SSL layer when the class is instantiatied so I can also use it to communicate with non-secure servers?...

Explain socket buffers please.

Hello! I was trying to find examples about socket programming and came upon this script: http://stacklessexamples.googlecode.com/svn/trunk/examples/networking/mud.py When reading through this script i found this line: listenSocket.listen(5) As i understand it - it reads 5 bytes from the buffer and then does stuff with it... but what...

aap - python trouble

Hello, i'm trying to run aap-application. Version is 1.076 (tried higher). All commands send me an error like: > Traceback (most recent call last): > File "/usr/bin/aap", line 10, in > <module> > import Main File "/usr/share/aap/Main.py", line 14, in > <module> > from DoAddDef import doadddef File "/usr/share/aap/DoAddDef...

Using Python to read the screen and controlling keyboard/mouse on OSX.

I'm looking for or trying to write a testing suite in Python which will control the mouse/keyboard and watch the screen for changes. The obvious parts I need are (1) screen watcher, (2) keyboard/mouse control. The latter is explained here, but what is the best way to go about doing the former on OSX? ...

How are nested dictionaries handled by DictWriter?

Using the CSV module in python, I was experimenting with the DictWriter class to convert dictionaries to rows in a csv. Is there any way to handle nested dictionaries? Specifically, I'm exporting Disqus comments that have a structure like this: { u'status': u'approved', u'forum': {u'id': u'', u'': u'', u'shortname': u'', u'name': u'',...

Python integer division yields float

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> 2/2 1.0 Is this intended? I strongly remember earlier versions returning int/int=int? What should I do, is there a new division operator or must I always cast? ...

Python - capture Popen stdout AND display on console?

Hi, I want to capture stdout from a long-ish running process started via subprocess.Popen(...) so I'm using stdout=PIPE as an arg. However, because it's a long running process I also want to send the output to the console (as if I hadn't piped it) to give the user of the script an idea that it's still working. Is this at all possible?...

Programmatically change font color of text in PDF

I'm not familiar with the PDF specification at all. I was wondering if it's possible to directly manipulate a PDF file so that certain blocks of text that I've identified as important are highlighted in colors of my choice. Language of choice would be python. ...

A python based PowerShell ?

I just took a brief look at PowerShell (I knew it as Monad shell). My ignorant eyes see it more or less like a hybrid between regular bash and python. I would consider such integration between the two environments very cool on linux and osx, so I was wondering if it already exists (ipython is not really the same), and if not, why ? ...

Recursive generation + filtering. Better non-recursive ?

I have the following need (in python): generate all possible tuples of length 12 (could be more) containing either 0, 1 or 2 (basically, a ternary number with 12 digits) filter these tuples according to specific criteria, culling those not good, and keeping the ones I need. As I had to deal with small lengths until now, the function...

Python Data Descriptor With Pass-through __set__ command

I'm having a bit of an issue solving a problem I'm looking at. I have a specialized set of functions which are going to be in use across a program, which are basically dynamic callables which can replace functions and methods. Due to the need to have them work properly to emulate the functionality of methods, these functions override _...

Entity Framwework-like ORM NOT for .NET

Hi, What I really like about Entity framework is its drag and drop way of making up the whole model layer of your application. You select the tables, it joins them and you're done. If you update the database scheda, right click -> update and you're done again. This seems to me miles ahead the competiting ORMs, like the mess of XML (n)H...

Python get wrong value for os.environ["ProgramFiles"] on 64bit vista

Python 2.4.3 on a Vista64 machine. The following 2 variables are in the environment: ProgramFiles=C:\Program Files ProgramFiles(x86)=C:\Program Files (x86) But when I run the following import os print os.environ["ProgramFiles"] print os.environ["ProgramFiles(x86)"] I get: C:\Program Files (x86) C:\Program Files (x86) Any idea how ...

URLconfs in Django

I am going through the Django sample application and come across the URLConf. I thought the import statement on the top resolves the url location, but for 'mysite.polls.urls' I couldn't remove the quotes by including in the import statement. Why should I use quotes for 'mysite.polls.urls' and not for admin url? and what should I do if ...