python

Most pythonic form for mapping a series of statements?

This is something that has bugged me for some time. I learnt Haskell before I learnt Python, so I've always been fond of thinking of many computations as a mapping onto a list. This is beautifully expressed by a list comprehension (I'm giving the pythonic version here): result = [ f(x) for x in list ] In many cases though, we want to ...

How to adding middleware to Appengine's webapp framework?

Im using the appengine webapp framework (link). Is it possible to add Django middleware? I cant find any examples. Im currently trying to get the firepython middleware to work (link). ...

Is Python 3 a good starting point when you want to learn Python ?

Hello, I'm a programmer with a heavy background in web application development (mostly PHP but also ASP), and some C/C++ and Java knowledge as well. My interest in Python has been growing a lot lately, and I'm wondering if I should start with the recently released Python 3, or with the older but really Python 2.x ? Thank you. Nicolas ...

Return file from python module

Edit: How to return/serve a file from a python controller (back end) over a web server, with the file_name? as suggested by @JV ...

Capitalize a string

Hi, Does anyone know of a really simple way of capitalizing just the first letter of a string, regardless of the capitalization of the rest of the string? For example: asimpletest -> Asimpletest aSimpleTest -> ASimpleTest I would like to be able to do all string lengths as well. Thanks, Dan ...

Extending builtin classes in python

How can I extend a builtin class in python? I would like to add a method to the str class. I've done some searching but all I'm finding is older posts, I'm hoping someone knows of something newer. ...

Weighted random selection with and without replacement

Recently I needed to do weighted random selection of elements from a list, both with and without replacement. While there are well known and good algorithms for unweighted selection, and some for weighted selection without replacement (such as modifications of the resevoir algorithm), I couldn't find any good algorithms for weighted sele...

How to add file extensions based on file type on Linux/Unix?

This is a question regarding Unix shell scripting (any shell), but any other "standard" scripting language solution would also be appreciated: I have a directory full of files where the filenames are hash values like this: fd73d0cf8ee68073dce270cf7e770b97 fec8047a9186fdcc98fdbfc0ea6075ee These files have different original file types...

Making Python default to another version installed on a shared host.

I am on a shared host and can not change the symbolic link to Python2.4, it defaults to 2.3. I tried creating a sym link in the director I would be working on to 2.4, but it seems the the 'global' python interpreter under /usr/bin/python take presedence unless I run it as ./python. What alternative ways are there to override this behavio...

How to work with very long strings in Python?

I'm tackling project euler's problem 220 (looked easy, in comparison to some of the others - thought I'd try a higher numbered one for a change!) So far I have: D = "Fa" def iterate(D,num): for i in range (0,num): D = D.replace("a","A") D = D.replace("b","B") D = D.replace("A","aRbFR") D = D.replace("B","LFaLb...

How to blend drawn circles with pygame

I am trying to create an application like the one here: http://www.eigenfaces.com/ Basically lots of overlapping circles drawn with pygame. I cannot figure out how the blend the circles to make them translucent. That is to have overlapping colors show through. My code so far is this: import sys, random, time import pygame from pygame....

Cleaner way to query on a dynamic number of columns in Django?

In my case, I have a number of column names coming from a form. I want to filter to make sure they're all true. Here's how I currently do it: for op in self.cleaned_data['options']: cars = cars.filter((op, True)) Now it works but there are are a possible ~40 columns to be tested and it therefore doesn't appear very efficient to ke...

Beginner: Trying to understand how apps interact in Django

I just got done working through the Djano tutorials for the second time, and am understanding things much more clearly now. However, I'm still unclear how apps inside a site interact with one another. For example, lets say I'm writing a blog application (a rather popular activity, apparently). Blog posts and comments tend to go together...

How do I check if a string is a number in Python?

What is the best possible way to check if a string can be represented as a number in Python? The function I currently have right now is: def is_number(s): try: float(s) return True except ValueError: return False This seems clunky, but I haven't found a better method because calling float in the main ...

How to configure vim to not put comments at the beginning of lines while editing python files

When I add a # in insert mode on an empty line in Vim while editing python files, vim moves the # to the beginning of the line, but I would like the # to be inserted at the tab level where I entered it. For example, when writing this in vim for i in range(10): # the # does not stay there where I entered it. It is moved like so, ...

Are there statistical studies that indicates that Python is "more productive"?

If I do a google search with the string "python productive" the first results is a page http://www.ferg.org/projects/python_java_side-by-side.html claiming that "python is more productive of Java". Many Python programmers that I have talked with claim that Python is "more productive", and most of them report the arguments listed in the ...

Adding a SOAP header to a SOAPpy request

Does anyone know how to do this? I need to add a header of the form: value1 value2 ...

How is ** implemented in Python?

I'm wondering where I find the source to show how the operator ** is implemented in Python. Can someone point me in the right direction? ...

How can I get the number of records that reference a particular foreign key in Django?

I'm working on a blog application in Django. Naturally, I have models set up such that there are Posts and Comments, and a particular Post may have many Comments; thus, Post is a ForeignKey in the Comments model. Given a Post object, is there an easy way (ideally, through a method call) to find out how many Comments belong to the Post? ...

How do you return multiple values in Python?

The canonical way to return multiple values in languages that support it is often tupling. Consider this trivial example: def f(x): y0 = x + 1 y1 = x * 3 y2 = y0 ** y3 return (y0,y1,y2) However, this quickly gets problematic as the number of values returned increases. What if you want to return four or five values? Sure, you...