python

List comprehension for series of deltas

How would you write a list comprehension in python to generate a series of n-1 deltas between n items in an ordered list? Example: L = [5,9,2,1,7] RES = [5-9,9-2,2-1,1-7] = [4,7,1,6] # absolute values ...

Python: User-Defined Exception That Proves The Rule

Python documentations states: Exceptions should typically be derived from the Exception class, either directly or indirectly. the word 'typically' leaves me in an ambiguous state. consider the code: class good(Exception): pass class bad(object): pass Heaven = good() Hell = bad() >>> raise Heaven Traceback (most recent call last): ...

create destination path for shutil.copy files

if b/c/ does not exist in ./a/b/c , shutil.copy("./blah.txt", "./a/b/c/blah.txt") will complain that the destination does not exist. how can i create the path at the same time? ...

Should Python 2.6 on OS X deal with multiple easy-install.pth files in $PYTHONPATH?

I am running ipython from sage and also am using some packages that aren't in sage (lxml, argparse) which are installed in my home directory. I have therefore ended up with a $PYTHONPATH of $HOME/sage/local/lib/python:$HOME/lib/python Python is reading and processing the first easy-install.pth it finds ($HOME/sage/local/lib/python...

trouble setting up TreeViews in pygtk

I've got some code in a class that extends gtk.TreeView, and this is the init method. I want to create a tree view that has 3 columns. A toggle button, a label, and a drop down box that the user can type stuff into. The code below works, except that the toggle button doesn't react to mouse clicks and the label and the ComboEntry aren't d...

Does python have a session variable concept???

I have a datetime.date variable in python.I need to pass it to a function do operations according to the date given and then increment the date for the next set of operations.The problem is I have to do the operations in diff pages and hence I need the date as a variable which can go from page to page. Can we do this in python....... Thi...

python and paramiko: how to check if end of ssh tunnel is still alive and reestablish if not?

Hello all, I'm trying to achieve the following: I have two hosts A and B. A establishes a remote port forwarding tunnel on B, i.e. B is the one a port is forwarded on to some where else and A is the one that sets up the tunnel. I tried the script rforward.py that ships with paramiko and it works very well so far. (I'm running rforward....

How do I assign functions in a dictionary?

hi, I'm having a problem with a simple program I wrote, I want to perform a certain function according to the users input. I've already used a dictionary as a replacement for a switch to do assignment but when I try to assign functions to the dictionary it doesn't execute them... The code: def PrintValuesArea(): ## do this def Print...

Python turtle module confusion

Hi, I'm trying to to add more lines to the triangle, so instead of 3 leading off there will be 5 depending on the parameter given but I really have no idea what to do at this stage and any help would be very welcome. Thanks in advance!:) def draw_sierpinski_triangle(tracer_on, colour, initial_modulus, line_width, initial_heading,initial...

Indent guide plugin for gedit (python).

See the indent guides? They're damn helpful when writing Python code. Any chance I could get something similar for gedit? I wouldn't mind having to write my own plugin, as long as it's in Python... So: Is there a plugin for this which works with gedit? If not, would it be possible to write one in Python. ...

Python PLY zero or more occurrences of a parsing item

I am using Python with PLY to parse LISP-like S-Expressions and when parsing a function call there can be zero or more arguments. How can I put this into the yacc code. This is my function so far: def p_EXPR(p): '''EXPR : NUMBER | STRING | LPAREN funcname [EXPR] RPAREN''' if len(p) == 2: p[0] = p[...

Are there any concerns I should have about storing a Python Lock object in a Beaker session?

There is a certain page on my website where I want to prevent the same user from visiting it twice in a row. To prevent this, I plan to create a Lock object (from Python's threading library). However, I would need to store that across sessions. Is there anything I should watch out for when trying to store a Lock object in a session (s...

Can't iterate over nestled dict in django

Hi, Im trying to iterate over a nestled dict list. The first level works fine. But the second level is treated like a string not dict. In my template I have this: {% for product in Products %} <li> <p>{{ product }}</p> {% for partType in product.parts %} <p>{{ partType }}</p> {% for part in partType %} ...

Bulk get of child entities on Google app engine?

On Google App Engine in Python, I have a Visit entity that has a parent of Patient. A Patient may have multiple visits. I need to set the most_recent_visit (and some auxiliary visit data) somewhere for later querying, probably in another child entity that Brett Slatkin might call a "relationship index entity." I wish to do so in a bul...

Python: replace urls with title names from a string

Hello I would like to remove urls from a string replace them with their titles of the original contents. For example: mystring = "Ah I like this site: http://www.stackoverflow.com. Also I must say I like http://www.digg.com" sanitize(mystring) # it becomes "Ah I like this site: Stack Overflow. Also I must say I like Digg - The Latest ...

RabbitMQ serializing messages from queue with multiple consumers

Hi there, I'm having a problem where I have a queue set up in shared mode and multiple consumers bound to it. The issue is that it appears that rabbitmq is serializing the messages, that is, only one consumer at a time is able to run. I need this to be parallel, however, I can't seem to figure out how. Each consumer is running in its...

How to generate random html document

I'd like to generate completely random piece of html source, possibly from a grammar. I want to do this in python but I'm not sure how to proceed -- is there a library that takes a grammar and just randomly follows its rules, printing the path? Ideas? ...

Best way to detect IronPython

I need to write a module which will be used from both CPython and IronPython. What's the best way to detect IronPython, since I need a slightly different behaviour in that case? I noticed that sys.platform is "win32" on CPython, but "cli" on IronPython. Is there another preferred/standard way of detecting it? ...

Python download without supplying a filename

How do I download a file with progress report using python but without supplying a filename. I have tried urllib.urlretrieve but I seem to have to supply a filename for the downloaded file to save as. So for example: I don't want to supply this: urllib.urlretrieve("http://www.mozilla.com/products/download.html?product=firefox-3.6.3&a...

How to see Microsoft Speech Recognition language and if it's active using Python?

Hello, I'm using windows 7 english and I want to know how to see the microsoft speech language and to see if the speech recognition is active. How can I do it using python? Solved with: x=_winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER) try: y= _winreg.OpenKey(x, r"Software\Microsoft\Speech\Preferences") if _winreg.Que...