python

How do I wrangle python lookups: make.up.a.dot.separated.name.and.use.it.until.destroyed = 777

I'm a Python newbie with a very particular itch to experiment with Python's dot-name-lookup process. How do I code either a class or function in "make.py" so that these assignment statements work succesfully? import make make.a.dot.separated.name = 666 make.something.else.up = 123 make.anything.i.want = 777 ...

Python Sphinx Documentation

How can you use Sphinx for Python 3.x documentation? http://doc.python.org/3.1 seem to have managed it. ...

Python problem executing popen in cron

I use popen to execute commands in a Python script, and I call it via cron. Cron calls out this script but the behavior isn't the same if I call it by hand. Source: from subprocess import Popen, PIPE pp = Popen('/usr/bin/which iptables', shell=True, stdout=PIPE) data = '' for ln in pp.stdout: data = data+ln if data == '': pr...

Setting value for a node in XML document in Python

I have a XML document "abc.xml": I need to write a function replace(name, newvalue) which can replace the value node with tag 'name' with the new value and write it back to the disk. Is this possible in python? How should I do this? ...

What is a good way to test if a Key exists in Python Dictionary

I wanted to test if a key exists in a dictionary before updating the value for the key. I wrote the following code: if 'key1' in dict.keys(): print "blah" else: print "boo" I think this is not the best way to accomplish this task. Is there a better way to test for a key in the dictionary? ...

How to trim the longest match from beginning of a string (using python)

In recent bash versions, I can do this: $ string="Universe.World.Country.State.City.Street" $ echo $string Universe.World.Country.State.City.Street $ newString="${string##*.}" $ echo $newString Street Using Python, what is a succinct way to doing the same? I am interested in the last substring after the last period. Thank you! ...

How to make a python script run like a service or daemon in linux

I have written a python script that checks a certain e-mail address and passes new e-mails to an external program. How can I get this script to execute 24/7, such as turning it into daemon or service in linux. Anyone that answers this, would I also need a loop that never ends in the program, or can it be done by just having the code re...

Why can I not import the Python module 'signal' using Jython, in Linux?

I can't find any reference to the 'signal' class being left out in Jython. Using Jython 2.1. Thanks ...

Recommend codebase to read and hone Python skills

Possible Duplicates: A good open source Python project to read code? How to learn Python: Good Example Code? I've been reading through Coders at Work and there is a repeated theme: hackers tend to read a lot of other's code and learn from it. I program in Python, there are plenty of open source code in it. Which codebase you...

Wait for an event, but don't take it off the queue

Is there any way to make the program sleep until an event occurs, but to not take it off the queue? Similarly to http://www.pygame.org/docs/ref/event.html#pygame.event.wait Or will I need to use pygame.event.wait, and then put that event back onto the queue? Just to clarify, I do not need to know what that event is when it occurs, jus...

Is there a good diagramming library for Python?

I should say I'm looking for something interactive, equivalent to what Nevron offers in it's .NET Diagram product, where a user can create nodes, interact with them by dragging them around, etc. I know there's GraphViz, but as far as I know it's static, and just renders a graph/diagram, there is no interaction with it. I have a bad feel...

Python idiom for 'Try until no exception is raised'

I want my code to automatically try multiple ways to create a database connection. As soon as one works, the code needs to move on (i.e. it shouldn't try to other ways anymore). If they all fail well, then the script can just blow up. So in - what I thought was, but most likely isn't - a stroke of genius I tried this: import psycopg2 f...

kill subprocess when python process is killed?

I am writing a python program that lauches a subprocess (using Popen). I am reading stdout of the subprocess, doing some filtering, and writing to stdout of main process. When I kill the main process (cntl-C) the subprocess keeps running. How do I kill the subprocess too? The subprocess is likey to run a long time. Context: I'm launchi...

CherryPy, load image from matplotlib, or in general

I am not sure what I am doing wrong, It would be great if you could point me toward what to read. I have taken the first CherryPy tutorial "hello world" added a little matplotlib plot. Question 1: how do I know where the file will be saved? It happens to be where I am running the file. Question 2: I don't seem to be get the image to open...

python image recognition

hi, what I want to do is a image recognition for a simple app: given image (500 x 500) pxs ( 1 color background ) the image will have only 1 geometric figure (triangle or square or smaleyface :) ) of (50x50) pxs. python will do the recognition of the figure and display what geometric figure is. any links? any hints? any API? thxs :) ...

Why is this simple python class not working?

Hi, I'm trying to make a class that will get a list of numbers then print them out when I need. I need to be able to make 2 objects from the class to get two different lists. Here's what I have so far class getlist: def newlist(self,*number): lst=[] self.number=number lst.append(number) def printlis...

What is the right way to design an adventure game with PyGame?

I have started development on a small 2d adventure side view game together with a couple of people. The game will consist of the regular elements: A room, a main character, an inventory, npcs, items and puzzles. We've chosen PyGame since we all are familiar with python from before. My question is quite theoretical, but how would we desig...

How can I modify a Python traceback object when raising an exception?

Hi all. I'm working on a Python library used by third-party developers to write extensions for our core application. I'd like to know if it's possible to modify the traceback when raising exceptions, so the last stack frame is the call to the library function in the developer's code, rather than the line in the library that raised the e...

How can I work out the class hierarchy given an object instance in Python?

Is there anyway to discover the base class of a class in Python? For given the following class definitions: class A: def speak(self): print "Hi" class B(A): def getName(self): return "Bob" If I received an instance of an object I can easily work out that it is a B by doing the following: instance = B() print B.__c...

Python SSH / SFTP Module?

Hi all, I've been looking around for a module which allows me to do SSH / SFTP functions in python without using POPEN to do it manually. Is there anything like this? I haven't found any real information on this, thanks! ...