python

Python - How to use Conch to create a Virtual SSH server.

I'm looking at creating a server in python that I can run, and will work as an SSH server. This will then let different users login, and act as if they'd logged in normally, but only had access to one command. I want to do this so that I can have a system where I can add users to without having to create a system wide account, so that t...

from X import a versus import X; X.a

This is one of those semi-religious Python questions that I suspect has well reasoned responses lurking in the community. I've seen some Python programmers use the following style fairly consistently (we'll call it style 1): import some_module # Use some_module.some_identifier in various places. For support of this style you can cite ...

Splitting a semicolon-separated string to a dictionary, in Python

I have a string that looks like this: "Name1=Value1;Name2=Value2;Name3=Value3" Is there a built-in class/function in Python that will take that string and construct a dictionary, as though I had done this: dict = { "Name1": "Value1", "Name2": "Value2", "Name3": "Value3" } I have looked through the modules available but ...

Configuration file with list of key-value pairs in python

I have a python scripts that analyzes a set of error messages and checks for each message if it matches a certain pattern (regular expression) in order to group these messages. For example "file x does not exist" and "file y does not exist" would match "file .* does not exist" and be accounted as two occurrences of "file not found" categ...

Base-2 (Binary) Representation Using Python

Building on How Do You Express Binary Literals in Python, I was thinking about sensible, intuitive ways to do that Programming 101 chestnut of displaying integers in base-2 form. This is the best I came up with, but I'd like to replace it with a better algorithm, or at least one that should have screaming-fast performance. def num_bi...

How to make parts of a website under SSL and the rest not?

I need to create a cherrypy main page that has a login area. I want the login area to be secure, but the rest of the page should not be so. How can I do this in CherryPy? Ideally, any suggestions will be compatible with http://tools.cherrypy.org/wiki/AuthenticationAndAccessRestrictions Thanks. ...

Counting array elements in Python

How can I count the number of elements in an array, because contrary to logic array.count(string) does not count all the elements in the array, it just searches for the number of occurrences of string. ...

How do I capture an mp3 stream with python

What's the best way of capturing an mp3 stream coming off of http and saving it to disk with python? Thus far I've tried target = open(target_path, "w") conn = urllib.urlopen(stream_url) while True: target.write(conn.read(buf_size)) This gives me data but its garbled or wont play in mp3 players. ...

How to make a python, command-line program autocomplete arbitrary things NOT interpreter

I am aware of how to setup autocompletion of python objects in the python interpreter (on unix). Google shows many hits for explanations on how to do this. Unfortunately, there are so many references to that it is difficult to find what I need to do, which is slightly different. I need to know how to enable, tab/auto completion of ...

How to run included tests on deployed pylons application

I have installed pylons based application from egg, so it sits somewhere under /usr/lib/python2.5/site-packages. I see that the tests are packaged too and I would like to run them (to catch a problem that shows up on deployed application but not on development version). So how do I run them? Doing "nosetests" from directory containing ...

Reading/Writing MS Word files in Python

Is it possible to read and write Word (2003 and 2007) files in Python without using a COM object? I know that I can: f = open('c:\file.doc', "w") f.write(text) f.close() but Word will read it as an HTML file not a native .doc file. ...

Django admin interface inlines placement

I want to be able to place an inline inbetween two different fields in a fieldset. You can already do this with foreignkeys, I figured that inlining the class I wanted and defining it to get extra forms would do the trick, but apparently I get a: "class x" has no ForeignKey to "class y" error. Is this not something that is supported ...

How can I, in python, iterate over multiple 2d lists at once, cleanly?

If I'm making a simple grid based game, for example, I might have a few 2d lists. One might be for terrain, another might be for objects, etc. Unfortunately, when I need to iterate over the lists and have the contents of a square in one list affect part of another list, I have to do something like this. for i in range(len(alist)): f...

How to use Python to login to a webpage and retrieve cookies for later usage?

Hi! I want to download and parse webpage using python, but to access it I need a couple of cookies set. Therefore I need to login over https to the webpage first. The login moment involves sending two POST params (username, password) to /login.php. During the login request I want to retrieve the cookies from the response header and stor...

How to break out of multiple loops in Python?

Given the following code (that doesn't work): while True: #snip: print out current state while True: ok = get_input("Is this ok? (y/n)") if ok == "y" or ok == "Y": break 2 #this doesn't work :( if ok == "n" or ok == "N": break #do more processing with menus and stuff Is there a way to make this work...

Google App Engine and 404 error

I've setup a static website on GAE using hints found elsewhere, but can't figure out how to return a 404 error. My app.yaml file looks like - url: (.*)/ static_files: static\1/index.html upload: static/index.html - url: / static_dir: static with all the static html/jpg files stored under the static directory. The above works fo...

What property returns the regular expression used when re.compile was called?

def foo (): x = re.compile('^abc') foo2(x) def foo2(x): # How do I get x to return '^abc'? logging.info('x is ' + x.???) ...

How can I quantify difference between two images?

Here's what I would like to do: I'm taking pictures with a webcam at regular intervals. Sort of like a time lapse thing. However, if nothing has really changed, that is, the picture pretty much looks the same, I don't want to store the latest snapshot. I imagine there's some way of quantifying the difference, and I would have to empi...

Daemon Threads Explanation

In the Python documentation: http://www.python.org/doc/2.5.2/lib/thread-objects.html it says: "A thread can be flagged as a ``daemon thread''. The significance of this flag is that the entire Python program exits when only daemon threads are left. The initial value is inherited from the creating thread." Does anyone have a clearer exp...

IDLE does't start in python 3.0

If I have no connection to internet, does that mean I can't start IDLE (which comes with python 3.0)? ...