python

Python state-machine design

Related to this SO question (C state-machine design), could you SO folks share with me (and the community!) your Python state-machine design techniques? Update3: At the moment, I am going for an engine based on the following: class TrackInfoHandler(object): def __init__(self): self._state="begin" self._acc="" #...

Is it possible to make user input invisible as a 'sudo' password input?

I'm using raw_input() to receive password from user in interactive mode, but I want to make input symbols invisible for security reasons, as it is when you're typing your password using sudo or connecting to a database. How I should do it? ...

How can multiple calculations be launched in parallel, while stopping them all when the first one returns? [Python]

How can multiple calculations be launched in parallel, while stopping them all when the first one returns? The application I have in mind is the following: there are multiple ways of calculating a certain value; each method takes a different amount of time depending on the function parameters; by launching calculations in parallel, the ...

trapping a MySql warning

Hi all In my python script I would like to trap a "Data truncated for column 'xxx'" warning durnig my query using MySql. I saw some posts suggesting the code below, but it doesn' work. Do you know if some specific module must be imported or if some option/flag should be called before using this code? Thanks all Afeg import MySQLdb ...

Json communication between flash and javascript

Hi, note: using django/python/javascript/flash So its been two days since I'm stuck at the error. I did the things you told me to and found a couple of ways around it but nothing worked. These are the results. Javascript does not receive the normal string it has to be a json object so. in views.py somestring = json.dumps("HELLO WORL...

Why do I need the DJANGO_SETTINGS_MODULE set?

Every time I log on to my server through SSH I need to type the following: export DJANGO_SETTINGS_MODULE=settings if I do not any usage of the manage.py module fails My manage.py has the following added code: if "notification" in settings.INSTALLED_APPS: from notification import models as notification def create_notice_type...

Why python multiprocessing module cause CPU completely run out?

I am trying python 2.6 multiprocessing module with this simple code snippet. from multiprocessing import Pool p = Pool(5) def f(x): return x*x print p.map(f, [1,2,3]) But this code cause my OS stopped responding. It looks like the CPU is too busy. What's wrong with my code? BTW : it seems that multiprocessing module is a bit da...

why this dos command does not work inside python?

I try to move some dos command from my batch file into python but get this error, The filename, directory name, or volume label syntax is incorrect, for the following statement. subprocess.Popen('rd /s /q .\ProcessControlSimulator\bin', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) if I just copy tha...

determine the type of a value which is represented as string in python

When I read a comma seperated file or string with the csv parser in python all items are represented as a string. see example below. import csv a = "1,2,3,4,5" r = csv.reader([a]) for row in r: d = row d ['1', '2', '3', '4', '5'] type(d[0]) <type 'str'> I want to determine for each value if it is a string, float, integer or date....

Prepare a string for Google Ajax Search?

I have strings such as ["Tabula Rasa", "façade", "DJ Tiësto"] I'm accessing Google Ajax API in Python using the base url: base = 'http://ajax.googleapis.com/ajax/services/search/web' '?v=1.0&q=%s' I'm having issues using these strings plain and noticed I have to transform certain characters, eg. "Tabula Rasa" --> "Tabula%20Ra...

Python newbie class design question.

I'm trying to figure out the best way to design a couple of classes. I'm pretty new to Python (and OOP in general) and just want to make sure that I'm doing this right. I have two classes: "Users" and "User". class User(object): def __init__(self): pass class Users(object): def __init__(self): self.users = [] ...

Selecting An Embedded Language

I'm making an application that analyses one or more series of data using several different algorithms (agents). I came to the idea that each of these agents could be implemented as separate Python scripts which I run using either the Python C API or Boost.Python in my app. I'm a little worried about runtime overhead TBH, as I'm doing so...

Two forms in django templates without conflict

hi, I'm creating a template with two different forms but I have the following problem: when I submit the first one the second is also validated and get validation errors. What can I do to avoid this conflict? Thanks and sorry for my english. ...

Help creating model for Django app

Hello, I'm trying to make a childcare administration app with Django but I've some problems with the payments code. Each kid has to pay monthly 10 times a year. These payments have some particularities: Some kids could pay a different amount of money depending on the economical situation of the parents. The amount of the payments coul...

how to check file size in python?

I am writing a python script in windows. I want to do something based on the file size. For example, if size greater than 0, i will send email to somebody, otherwise continue to other things. so how to check file size? thanks ...

Efficiently search two-tuple

What's the best one-liner replacement for the code below? I'm sure there's a smarter way. choices = ((1, 'ONE'), (2, 'TWO'), (3, 'THREE')) some_int = 2 for choice in choices: if choice[0] == some_int: label = choice[1] break; # label == 'TWO' ...

Django/Python mailing list implementation

Hello everyone My site requires sending periodic update emails to all our registered clients. To keep our lists clear, I want to track all failed deliveries and purge the mailing lists accordingly. I am assuming I am not the first nor the last to do this. Can anyone recommend an existing app/library that I can use to accomplish this ...

Python tool to balance parentheses, quotes, and brackets

Does anyone know of an already-written Python script, tool, or editor that will check for unbalanced multi-line tokens? (parentheses, quotes, {}, [], etc.) I've been writing Python code in IDLE, and every so often I'll get "EOF token in multi-line statement" and start swearing, because it means that somewhere in about 200 lines of code ...

Django/Python: How do you start a new process in Python?

After a customer uploads an image I want to re-size an image to 8 different sizes and then send them up to S3 and save them to the file system. Obviously this could take a long time (seconds) and I would want to do it in a "multithreaded" way (I quote that because I don't care if it's actually multithreaded or if it's some other function...

Python: Elements not in a list

So heres my code: item = [0,1,2,3,4,5,6,7,8,9] for item in z: if item not in z: print item Z contains a list of integers. I want to compare item to Z and print out the numbers that are not in Z when compared to item. I can print the elemtens that are in Z when compared not items, but when i try and do the opposite using t...