python

How much input validation should I be doing on my python functions/methods?

I'm interested in how much up front validation people do in the Python they write. Here are a few examples of simple functions: def factorial(num): """Computes the factorial of num.""" def isPalindrome(inputStr): """Tests to see if inputStr is the same backwards and forwards.""" def sum(nums): """Same as the built-in sum(...

Ruby timeout for Python?

Does anyone know a good solution for implementing a function similar to Ruby's timeout in Python? I've googled it and didn't really see anything very good. Thanks for the help. Here's a link to the Ruby documentation http://www.ruby-doc.org/stdlib/libdoc/timeout/rdoc/index.html ...

How do I build a numpy array from a generator?

How can I build a numpy array out of a generator object? Let me illustrate the problem: >>> import numpy >>> def gimme(): ... for x in xrange(10): ... yield x ... >>> gimme() <generator object at 0x28a1758> >>> list(gimme()) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> numpy.array(xrange(10)) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> numpy...

Generating random text strings of a given pattern

I need to generate random text strings of a particular format. Would like some ideas so that I can code it up in Python. The format is <8 digit number><15 character string>. ...

Get Data from OpenGL glReadPixels(using Pyglet)

I'm using Pyglet(and OpenGL) in Python on an application, I'm trying to use glReadPixels to get the RGBA values for a set of pixels. It's my understanding that OpenGL returns the data as packed integers, since that's how they are stored on the hardware. However for obvious reasons I'd like to get it into a normal format for working wit...

How can I reorder an mbox file chronologically?

Hello, I have a single spool mbox file that was created with evolution, containing a selection of emails that I wish to print. My problem is that the emails are not placed into the mbox file chronologically. I would like to know the best way to place order the files from first to last using bash, perl or python. I would like to oder by ...

Making a virtual package available via sys.modules

Say I have a package "mylibrary". I want to make "mylibrary.config" available for import, either as a dynamically created module, or a module imported from an entirely different place that would then basically be "mounted" inside the "mylibrary" namespace. I.e., I do: import sys, types sys.modules['mylibrary.config'] = types.ModuleTyp...

How can i stop a While loop?

Hi,i wrote a while loop in a function,but dont know how to stop it...when it doesnt meet its final condition,the loop just go for ever....how can i stop it?thanks in advance. def determine_period(universe_array): period=0 tmp=universe_array while True: tmp=apply_rules(tmp)#aplly_rules is a another function pe...

Questions about Setuptools and alternatives

I've seen a good bit of setuptools bashing on the internets lately. Most recently, I read James Bennett's On packaging post on why no one should be using setuptools. From my time in #python on Freenode, I know that there are a few souls there who absolutely detest it. I would count myself among them, but I do actually use it. I've used ...

Python UnicodeDecodeError - Am I misunderstanding encode?

Any thoughts on why this isn't working? I really thought 'ignore' would do the right thing. >>> 'add \x93Monitoring\x93 to list '.encode('latin-1','ignore') Traceback (most recent call last): File "<interactive input>", line 1, in ? UnicodeDecodeError: 'ascii' codec can't decode byte 0x93 in position 4: ordinal not in range(128) ...

Passing a Python array to a C++ vector using Swig

I have an array of objects in Python [obj1, obj2, obj3] and I want to pass them to off to a C++ function to perform some computation. I'm using SWIG to write my interface. The class type of the passed object is already defined in C++. What's the best way to do this? ...

What is the best way to serialize a ModelForm object in Django?

I am using Django and the Google Web Toolkit (GWT) for my current project. I would like to pass a ModelForm instance to GWT via an Http response so that I can "chop" it up and render it as I please. My goal is to keep the form in sync with changes to my models.py file, yet increase control I have over the look of the form. However, the d...

using existing rrule to generate a further set of occurrences

I have an rrule instance e.g. r = rrule(WEEKLY, byweekday=SA, count=10, dtstart=parse('20081001')) where dtstart and byweekday may change. If I then want to generate the ten dates that follow on from this rrule, what's the best way of doing it? Can I assign a new value to the _dtstart member of r? That seems to work but I'm not ...

Is there anyone who has managed to compile mod_wsgi for apache on Mac OS X Leopard?

I'm working on a Django project that requires debugging on a multithreaded server. I've found mod_wsgi 2.0+ to be the easiest to work with, because of easy workarounds for python module reloading. Problem is can't get it to compile on Leopard. Is there anyone who has managed to do it so far, either for the builtin Apache or MAMP. I'd be ...

Is there an OpenSource BASIC interpreter in Ruby/Python?

I want something simple in order to experiment/hack. I've created a lot interpreters/compilers for c and I just want something simple. A basic BASIC :D If you don't know any (I've done my google search...), yacc/bison is the only way? Thx ...

Python: os.environ.get('SSH_ORIGINAL_COMMAND') returns None

Trying to follow a technique found at bzr and gitosis I did the following: added to ~/.ssh/authorized_keys the command="my_parser" parameter which point to a python script file named 'my_parser' and located in /usr/local/bin (file was chmoded as 777) in that script file '/usr/local/bin/my_parser' I got the following lines: #!/usr/bi...

How to discover the type of the NAT a given interface is behind

I would like to discover the type of the NAT (FullCone, Restricted Cone, Port Restricted cone, Symmetric) a given network interface is behind. I've tested different tools (http://freshmeat.net/projects/jstun/, http://code.google.com/p/boogu/) but they report different results for the same interface. I'm looking for a definitive ans...

IPv6 decoder for pcapy/impacket

I use the pcapy/impacket library to decode network packets in Python. It has an IP decoder which knows about the syntax of IPv4 packets but apparently no IPv6 decoder. Does anyone get one? In a private correspondance, the Impacket maintainers say it may be better to start with Scapy ...

Python dictionary clear

In python, is there a difference between calling clear() and assigning {} to a dictionary? If yes, what is it? Example:d = {"stuff":"things"} d.clear() #this way d = {} #vs this way ...

command-line world clock?

Is there a script to display a simple world clock (time in various places around the world) on a *nix terminal? I was thinking of writing a quick Python script, but I have a feeling that's gonna be more work than I think (e.g. due to config and output format) - not to mention reinventing the wheel... ...