python

Python template help

I'm using App Engine's web-app templating system (similar if not identical to django) Normally I render templates from my static directory /templates/ as follows in my main handler: dirname = os.path.dirname(__file__) template_file = os.path.join(dirname, os.path.join('templates', template_name)) output = template.render(template_f...

How can I interactively explore why a test is failing?

I have a test that is failing with: ====================================================================== FAIL: test_register_should_create_UserProfile (APP.forum.tests.test_views.UserTestCAse) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Us...

Module "duck typing" pitfalls?

I just started experimenting with a new technique I name (for the moment at least) "module duck typing". Example: Main Module import somepackage.req ## module required by all others import abc import Xyz Module abc __all__=[] def getBus(): """ Locates the `req` for this application """ for mod_name in sys.modules: ...

Preferred Python twitter API?

I found at http://apiwiki.twitter.com/Libraries#Python that there are several Twitter APIs for Python. I've been using python-twitter for a while but never needed the API Search. Now I need to do a job that includes Twitter searches and I see they are not supported by this lib. I wonder know which of the listed ones is the appropriated...

What version of Python should I be using to develop web applications?

Currently I have python 3.1.1 installed on my system, same version on my server with WSGI 3.0 running on Apache 2.2. I want to keep using python 3.1, but I'm finding that lots of libraries/frameworks don't support it yet, namely Django, Pylons, MySQLdb, etc. I also checked an account I have on a friend's server, and it is running pytho...

Should a Python generator raise an exception when there are no more elements to yield?

Should a Python generator raise an exception when there are no more elements to yield? Which one? ...

How do I change permissions to a socket?

I am trying to run a simple Python based web server given here. And I get the following error message: Traceback (most recent call last): File "webserver.py", line 63, in <module> main() File "webserver.py", line 55, in main server = HTTPServer(('', 80), MyHandler) File "/usr/lib/python2.5/SocketServer.py", line 330, in _...

proper name for python * operator?

What is the correct name for operator *, as in function(*args)? unpack, unzip, something else? ...

SqlAlchemy optimizations for read-only object models.

I have a complex network of objects being spawned from a sqlite database using sqlalchemy ORM mappings. I have quite a few deeply nested: for parent in owner.collection: for child in parent.collection: for foo in child.collection: do lots of calcs with foo.property My profiling is showing me that the sqlalc...

Index and Slice a Generator in Python

Lets say I have a generator function that looks like this: def fib(): x,y = 1,1 while True: x, y = y, x+y yield x Ideally, I could just use fib()[10] or fib()[2:12:2] to get indexes and slices, but currently I have to use itertools for these things. I can't use a generator for a drop in replacement for lists. ...

Django array or list output?

I'm pulling a set of image urls and their respective titles. I've tried creating a hash or associative array, but the data seems to overwrite so I only end up with the last item in the array. For example; thumbnail_list = [] for file in media: thumbnail_list['url'] = file.url thumbnail_list['title'] = file.title I've even tr...

How do I prevent my Python 2.6 application from automatically closing once reaching the end of code?

Hello. I'm new to programming, especially Python. I'm trying to make an application that convert Fahrenheit to Celsius, but I don't know how to make the program stay open. Whenever it reaches the end of the code, it automatically closes before the user can see his or her results. Help, please? I'm using Python 2.6. ...

How can I disable the webbrowser message in python?

In my python program, when I send the user to create a gmail account by use of the webbrowser module, python displays: "Please enter your Gmail username: Created new window in existing browser session." Is there any way to get rid of "created new window in existing browser session", as it takes up the space where the user types in thei...

Python: Convert string in base64 to image and save on filesystem

Hi, I have a string in base64 format, which represents png image. Is there a way to save this image to the filesystem, as a png file? I encoded the image using flex. Actually this is what I get on server (can't see any image after any of proposed methods :( ) iVBORw0KGgoAAAANSUhEUgAABoIAAAaCCAYAAAABZu+EAAAqOElEQVR42uzBAQEAAACAkP6...

Is there a Python equivalent of Ruby's 'any?' function?

In Ruby, you can call Enumerable#any? on a enumerable object to see if any of its elements satisfies the predicate you pass in the block. Like so: lst.any?{|e| pred(e) } In Python, there's an any function that does something similar, but on a list of booleans. Of course, for a reasonably-sized list, I'd just do: any(map(pred,lst)) ...

Floating Point Modulo Problem

Hello everyone, I've stumbled onto a very strange bug. Read the comments in the code to see what the bug is exactly, but essentially a variable modulo 1 is returning 1 (but it doesn't equal 1!). I'm assuming there is a display problem where the float is extremely close to one but not exactly. However, it should be moduloing to zero. I c...

How to calculate a value to a certain number of decimal places?

Using numpy or python's standard library, either or. How can I take a value with several decimal places and truncate it to 4 decimal places? I only want to compare floating point numbers to their first 4 decimal points. ...

django+flex: Debugging strategies

Hi! I love django, and I like flex. Django for it's cool debugging system (those yellow pages helps a lot to find bugs in my code), and flex for it possibilities. Recently I come across a problem. If I create a form in flex and then communicate with the django server, I can't see any debugging info (when the exception happens in django...

zip() alternative for iterating through two iterables

I have two large (~100 GB) text files that must be iterated through simultaneously. Zip works well for smaller files but I found out that it's actually making a list of lines from my two files. This means that every line gets stored in memory. I don't need to do anything with the lines more than once. handle1 = open('filea', 'r'); ha...

Problem originating SSH tunnels from python

The object is to set up n number of ssh tunnels between satellite servers and a centralized registry database. I have already set up public key authentication between my servers so they just log right in without password prompts. Now what ? I've tried Paramiko. It seems decent but gets pretty complicated just to set up a basic tunnel, al...