python

Python: Does python have an equivalent to 'switch'?

I am trying to check each index in an 8 digit binary string. If it is '0' then it is 'OFF' otherwise its 'ON'. I'm wondering if there's a more concise way to write this code with a switch-like feature. Thanks, ...

How to programmatically set a global (module) variable?

I would like to define globals in a "programmatic" way. Something similar to what I want to do would be: definitions = {'a': 1, 'b': 2, 'c': 123.4} for definition in definitions.items(): exec("%s = %r" % definition) # a = 1, etc. Specifically, I want to create a module fundamentalconstants that contains variables that can be acc...

Configuring Roundup with Apache

I think I just need a bit more guidance than what the documentation gives, and it's quite hard to find anything relating to Roundup and Apache specifically. All i'm trying to do currently is to have Apache display what the stand-alone server does when running roundup-server support=C:/Roundup/ Running windows XP with apache 2.2 and pyt...

Create a temporary FIFO (named pipe) in Python?

How can you create a temporary FIFO (named pipe) in Python? This should work: import tempfile temp_file_name = mktemp() os.mkfifo(temp_file_name) open(temp_file_name, os.O_WRONLY) # ... some process, somewhere, will read it ... However, I'm hesitant because of the big warning in Python Docs 11.6 and potential removal because it's ...

Application to generate installers for Linux, Windows and MacOSX from a single configuration

Here's what I want: Given a set of definitions (preferably in Python) on what files to install where and what post-install script to run, etc.. I would like this program to generate installers for the three major platforms: MSI on Windows dmg on MacOSX Tarball w/ install.sh (and rpm/deb, if possible) on Linux For example, installco...

Kill sub-threads when Django restarts?

I'm running Django, and I'm creating threads that run in parallel while Django runs. Those threads sometimes run external processes that block while waiting for external input. When I restart Django, those threads that are blocking while awaiting external input sometimes persist through the restart, and further they have and keep open P...

Load an existing many-to-many table relation with sqlalchemy

I'm using SqlAlchemy to interact with an existing PostgreSQL database. I need to access data organized in a many-to-many relationship. The documentation describes how to create relationships, but I cannot find an example for neatly loading and query an existing one. ...

Python: Traversing a string, checking its element, and inputting dictionary key-value pairs

I have a function that returns an 8 digit long binary string for given parameter: def rule(x): rule = bin(x)[2:].zfill(8) return rule I want to traverse each index of this string and check if it is a zero or a one. I tried to write a code like this: def rule(x): rule = bin(x)[2:].zfill(8) while i < len(rule(x)): if ru...

Accessing Plist items in a dict

I have a class in a module I that reads a plist (XML) file and returns a dict. This is extremely convenient because I can say something like: Data.ServerNow.Property().DefaultChart This returns a property dictionary, specifically the value for DefaultChart. Very elegant. However, assembling a dictionary this way fails: dict={'Data': ...

What is the keycode for control+z key in python?

I want to send some modem AT commands using python code, and am wondering what is the keycode for the key combination control+z Gath ...

Python bracket convention

What do you think is the convention that is mostly used when writing dictionary literals in the code? I'll write one possible convention as an answer. ...

Is it possible to run pydev connected to a virtualbox instance?

At the moment I'm developing using a simple editor, putty, and a VirtualBox instance of a linux server. I've heard good things about pydev and would like to try it, but I'd like to use the python install & terminal from my VirtualBox guest OS. I'm already using a Shared Folder with VirtualBox so my Guest OS can see my local files. Is i...

Add an "empty" option to a ChoiceField based on model datas

Hello, I'm defining a ChoiceField based on a model's datas. field = forms.ChoiceField(choices=[[r.id, r.name] for r in Model.objects.all()]) However I'd like to prepend my options with an empty one to select "no" objects. But I can't find a nice way to prepend that. All my tests like : field = forms.ChoiceField(choices=[[0, '------...

Casting regex arguments into a list

Greetings, A script is working on one or more files. I want to pass the filenames (with regex in them) as arguments and put them in a list. What is the best way to do it? For example I would accept the following arguments: script.py file[1-3].nc #would create list [file1.nc, file2.nc, file3.nc] that I can work on script.py file*.nc #w...

In which module does the Timer class reside in python?

Am reading the following online Timer manual for python 2.5, but am wondering where is the actual module where the class Timer resides? Gath ...

deleting files with python scripts

Hi, I want to delete some files with python scripts (while using Windows). I have tried the following code: >>>import os >>> os.remove ('D:\new.docx') but I am getting the following error: Traceback (most recent call last): File "<pyshell#1>", line 1, in -toplevel- os.remove ('D:\new.docx') OSError: [Errno 22] Invalid argume...

how to get content of a small ascii file in python ?

Let's say we want to implement an equivalent of the PHP's file_get_content. What is the best practice? (elegant and reliable) Here are some proposition, are they correct? using "with" statement: def file_get_contents(filename): with file(filename) as f: s = f.read() return s using is using standard open safe? def f...

Returning a list

I have the following code: def foo(*args) print len(args) print args now I'd like to know how to return that same args list. I guess it should be simple? Thanks ...

Any way to clear python shell?

I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here. Thanks! ...

Help with Admin forms validation error

Hi Guys, I am quite new to Django, I'm having few problems with validation forms in Admin module, more specifically with raising exceptions in the ModelForm. I can validate and manipulate data in clean methods but cannot seem to raise any errors. Whenever I include any raise statement I get this error "'NoneType' object has no attribute...