python

Tower of hanoi, python -> scheme, shows error. What am I missing ?

The python implementation import sys def move(src, dst, tmp, num): if num == 1: print 'Move from', src, 'to', dst else: move(src, tmp, dst, num-1) move(src, dst, tmp, 1) move(tmp, dst, src, num-1) move('left', 'right', 'middle', int(sys.argv[1])) Gives the right solution for tower of hanoi. But my sch...

Why does supplying stdin to subprocess.Popen cause what is written to stdout to change?

I'm using Python's subprocess.Popen to perform some FTP using the binary client of the host operating system. I can't use ftplib or any other library for various reasons. The behavior of the binary seems to change if I attach a stdin handler to the Popen instance. For example, using XP's ftp client, which accepts a text file of commands...

Tell if python is in interactive mode

In a python script, is there any way to tell if the interpreter is in interactive mode? This would be useful, for instance, when you run an interactive python session and import a module, slightly different code is executed (for example, log file writing is turned off, or a figure won't be produced, so you can interactively test your pro...

Problems with nested loops…

Hi! I’m going to explain to you in details of what I want to achieve. I have 2 programs about dictionaries. The code for program 1 is here: import re words = {'i':'jeg','am':'er','happy':'glad'} text = "I am happy.".split() translation = [] for word in text: word_mod = re.sub('[^a-z0-9]', '', word.lower()) punctuation = word[-...

Python: Import Data from Open Office calc with lxml

How can I import data for example for the field A1? When I use etree.parse() I get an error, because I dont have a xml file. ...

Python, OpenOffice: Programatically Manipulating spreadsheets

Hi, I have an spreadsheet-based automated report that needs to be created daily, with some charts, aggregating functions (e.g. SUM and AVERAGE) and formatted cells (Dates, percentage, etc.). I have tried to write these results directly to an Excel file, but Python's xlwt and xlrd don'y support charts and functions. Moreover, trying t...

How do you round UP a number in Python?

This problem is killing me. How does one roundup a number UP in Python? I tried round(number) but it round the number down. Example: round(2.3) = 2.0 and not 3, what I would like The I tried int(number + .5) but it round the number down again! Example: int(2.3 + .5) = 2 Then I tried round(number + .5) but it won't work in edge cas...

Django -- How to filter objects with an "author" from a set of "authors"(users)?

How to filter objects with an "author" from a set of "authors"(Users)? The "objects" are Posts, having an author(ForeignKey to User). I'm pretty much stumped by this, so I'd appreciate help with it. Of course one could go about this the naive way, by manually filtering them, but that would hit the database real hard. Thanks anyway. E...

Python try/except ... function always returns false

I'm trying to figure out the problem in this short paragraph of code. Any help would be appreciated. Regardless of what I specify User.email to be, it always returns false. def add(self): #1 -- VALIDATE EMAIL ADDRESS #Check that e-mail has been completed try: #Validate if e-mail address is in correct format ...

How do you make Python wait so that you can read the output?

I've always been a heavy user of Notepad2, as it is fast, feature-rich, and supports syntax highlighting. Recently I've been using it for Python. My problem: when I finish editing a certain Python source code, and try to launch it, the screen disappears before I can see the output pop up. Is there any way for me to make the results wa...

Reusing module references in Python (Matplotlib)

I think I may have misunderstood something here... But here goes. I'm using the psd method in matplotlib inside a loop, I'm not making it plot anything, I just want the numerical result, so: import pylab as pyl ... psdResults = pyl.psd(inputData, NFFT=512, Fs=sampleRate, window=blackman) But that's being looped 36 times every time I ...

Importing sound files into Python as NumPy arrays (alternatives to audiolab)

I've been using Audiolab to import sound files in the past, and it worked quite well. However: It doesn't support some formats, like mp3, because the underlying libsndfile refuses to support them It doesn't work in Python 2.6 under Windows, and the author is not around to fix it - In [2]: from scikits import audiolab --------------...

How to check whether string might be type-cast to float in Python?

Is there some function like str.isnumeric but applicable to float? '13.37'.isnumeric() #False I still use this: def isFloat(string): try: float(string) return True except ValueError: return False ...

How can manage many user requests to an apache web server using win32com in python?

Hello every body, I work on a Web text-to-speech System, trasforming text documents in audio mp3 files, using python 2.5. I use Apache2.2 as a server and mod_python as module to embed the Python interpreter within the server. The user should submit via web interface (input interface), a file txt or a Word document (.doc or .rtf) and th...

How can I access a function from FORTRAN which is writen in Python?

Hi, Is there any way to use a python function in FORTRAN? I was given a python script that contains some functions and I need to access this function from a FORTRAN code. I've seen 'f2py' which allows a FORTRAN subroutine to be accessed from Python, and py2exe which will compile python script to an executable. Is there anything out th...

What is the proper way to comment functions in python?

Is there a generally accepted way to do this? Is this acceptable: ######################################################### # Create a new user ######################################################### def add(self): ...

Per object permissions in a container

In my Grok application I have a container of objects and I'd like to limit the objects that a particular principal can view based on an attribute of the principal and the object. If the principal is an administrator they're able to view any of the objects. I've got this to work by using a Traverser that raises an Unauthorized exception ...

Creating a global function, accessible from all classes, with Python + Pylons

Using pylons 0.9.7, I'm trying to make a function that connects to a database on demand. I'd like it to be accessible from all functions within all model classes. In model/__init__.py, I have: #Establish an on-demand connection to the central database def connectCentral(): engine = engine_from_config(config, 'sqlalchemy.central.'...

svn history via command line

I am trying to write a utility in python to get me all the files that have been modified for a specific branch....i don't care about the date or who commited. how would I go about doing this? I can handle the python part, I just can't find a command in svn to give me the output. ...

Serial communication. Sending DTR in the right way?

I'm dealing with a gm29 by Sony Ericsson. The datasheet says that plugging the power is not sufficient to switch on the modem. It says: activate the RS232 control line DTR, high for > 0.2s. I'm writing some tests in python, but: #!/usr/bin/env python ...