python

Feeding a string into Popen

I would like to run a process from Python (2.4/2.5/2.6) using Popen, and I would like to give it a string as its standard input. I'll write an example where the process does a "head -n 1" its input. The following works, but I would like to solve it in a nicer way, without using echo: >>> from subprocess import * >>> p1 = Popen(["echo"...

Nested While loop in Python.

Hi! I am a beginner in python programming. I wrote the following program but it doesn't execute as I want it to. Here is the code: b=0 x=0 while b<=10: print 'here is the outer loop\n',b, while x<=15: k=p[x] print'here is the inner loop\n',x, x=x+1 b=b+1 can somebody help me?? I will be grateful in...

How do I get the operating system name in a friendly manner using Python 2.5?

I tried: print os.name And the output I got was: :nt However, I want output more like "Windows 98", or "Linux". After suggestions in this question, I also tried: import os print os.name import platform print platform.system() print platform.release() And my output was: Traceback (most recent call last): File "C:/Documents an...

SQLAlchemy session management in long-running process

Scenario: A .NET-based application server (Wonderware IAS/System Platform) hosts automation objects that communicate with various equipment on the factory floor. CPython is hosted inside this application server (using Python for .NET). The automation objects have scripting functionality built-in (using a custom, .NET-based language). T...

How to trigger post-build using setuptools/distutils

I am building an application using py2app/setuptools, so once it creates application bundle I want to take some action on dist folder e.g. create a installer/upload it. Is there a way? I have found some post-install solution but no post-build Alternatively I can call 'python setup.py py2app' from my own script and do that, but it would...

Set files to ownership of current directory in Python

I'm working on a Python script that creates text files containing size/space information about the directories that the script is run on. The script needs to be run as root, and as a result, it sets the text files that it creates to root's ownership. I know I can change the ownership with os.fchown, but how do I pass fchown the uid and ...

Which python mpi library to use?

I'm starting work on some simulations using MPI and want to do the programming in Python/scipy. The scipy site lists a number of mpi libraries, but I was hoping to get feedback on quality, ease of use, etc from anyone who has used one. ...

GAE load data into datastore without using CSV

I've used bulkloader.Loader to load stuff into the GAE dev and live datastore, but my next thing to to create objects from non-CSV data and push it into the datastore. So say my object is something like: class CainEvent(db.Model): name =db.StringProperty(required=True) birthdate = db.DateProperty() Can anyone give me a s...

fcntl substitute on Windows

I received a Python project (which happens to be a Django project, if that matters,) that uses the fcntl module from the standard library, which seems to be available only on Linux. When I try to run it on my Windows machine, it stops with an ImportError, because this module does not exist here. Is there any way for me to make a small c...

Django Form Validation Framework on AppEngine: How to strip out HTML etc.?

I'm using the Django Form Validation Framework on AppEngine (http://code.google.com/appengine/articles/djangoforms.html), like this: data = MyForm(data=self.request.POST) if data.is_valid(): entity = data.save(commit=False) entity.put() I wonder if there's a way to preprocess the POST data (strip out malicious code, HTML etc...

How does AMF communication work?

How does Flash communicate with services / scripts on servers via AMF? Regarding the AMF libraries for Python / Perl / PHP which are easier to develop than .NET / Java: do they execute script files, whenever Flash sends an Remote Procedure Call? or do they communicate via sockets, to script classes that are running as services? Rega...

pytz: Why is normalize needed when converting between timezones?

Hi I'm reading the not so complete pytz documentation and I'm stuck on understand one part of it. Converting between timezones also needs special attention. This also needs to use the normalize method to ensure the conversion is correct. >>> utc_dt = utc.localize(datetime.utcfromtimestamp(1143408899)) >>> utc_dt.strftime(fmt) '200...

Python and the built-in heap

At the moment, I am trying to write a priority queue in Python using the built in heapq library. However, I am stuck trying to get a handle on what Python does with the tie-breaking, I want to have a specific condition where I can dictate what happens with the tie-breaking instead of the heapq library that seems to almost pick something ...

web framework compatible with python 3.1 and py-postgresql

I have started learning Python by writing a small application using Python 3.1 and py-postgresql. Now I want to turn it into a web application. But it seems that most frameworks such as web-py, django, zope are still based on Python 2.x. Unfortunately py-postgresql is incompatible with Python 2.x. Do I have to rewrite all my classes an...

What python web frameworks work well with CGI (e.g. on nearlyfreespeech.net)?

From nearlyfreespeech's website, they state that the following don't work well: mod_python Web application frameworks that depend on persistent processes, including: Ruby On Rails, Django, Zope, and others (some of these will run under CGI, but will run slowly and are suitable only for development purposes) Are there any Python web ...

How to save a configuration file / python file IO

I have this python code for opening a .cfg file, writing to it and saving it: import ConfigParser def get_lock_file(): cf = ConfigParser.ConfigParser() cf.read("svn.lock") return cf def save_lock_file(configurationParser): cf = configurationParser config_file = open('svn.lock', 'w') ...

talking between python tcp server and a c++ client

I am having an issue trying to communicate between a python TCP server and a c++ TCP client. After the first call, which works fine, the subsequent calls cause issues. As far as WinSock is concerned, the send() function worked properly, it returns the proper length and WSAGetLastError() does not return anything of significance. Howeve...

Negative lookahead after newline?

I have a CSV-like text file that has about 1000 lines. Between each record in the file is a long series of dashes. The records generally end with a \n, but sometimes there is an extra \n before the end of the record. Simplified example: "1x", "1y", "Hi there" ------------------------------- "2x", "2y", "Hello - I'm lost" ---------------...

What is the best design for polling a modem for incoming data?

I have a GSM modem connected to my computer, i want to receive text messages sent to it using a python program i have written, am just wondering what is the best technique to poll for data. Should i write a program that has a infinite loop that continuously checks for incoming sms's i.e within the loop the program sends the AT commands...

Can I run a Python script as a service?

Is it possible to run a Python script as a background service on a webserver? I want to do this for socket communication. ...