python

How to use one app to satisfy multiple URLs in Django

I'm trying to use one app to satisfy multiple url paths. That is to say, I want the url /blog/ and /job/ to use the same app, but different views. There are a number of ways to do this I'm sure, but none of them seem very clean. Here's what I'm doing right now # /urls.py urlpatterns = patterns("", (r"^(blog|job)/", include("mypro...

Python re.sub question

Greetings all, I'm not sure if this is possible but I'd like to use matched groups in a regex substitution to call variables. a = 'foo' b = 'bar' text = 'find a replacement for me [[:a:]] and [[:b:]]' desired_output = 'find a replacement for me foo and bar' re.sub('\[\[:(.+):\]\]',group(1),text) #is not valid re.sub('\[\[:(.+):\]\]'...

Reverse loop inside a loop with same list

num = list(str(1234567)) for n1 in num: print n1 for n2 in reversed(num): print '\t', n2 On each iteration, it prints the first digit from the first loop and all 7 from the reverse loop. How can I print not all digits but only the last (i.e first) digit from reverse loop? Thanks ...

Pylons and AuthKit OpenID problem

Hi all! I have troubles setting up the support for openID authentication, using authkit and pylons. I set up everything as described in the cookbook, but still get the following error: File "/usr/lib/python2.6/dist-packages/authkit/authenticate/open_id.py", line 480, in __call__ return self.app(environ, start_response) File "/u...

Django app initalization code (like connecting to signals).

I need a place to run an initialization code that is application specific (like connecting to signals). When I put the code to __init__.py module of an application I've ended up with a circular import of the models. Is there a way to fire a function when the framework is setup and before any request is executed? I use quite old version...

How can I find out why PIL isn't drawing the font correctly?

Here's the code I'm using: from PIL import Image import ImageFont, ImageDraw import sys import pdb img = Image.new("RGBA",(300,300)) draw = ImageDraw.Draw(img) font = ImageFont.truetype(sys.argv[1],30) draw.text((0,100),"world",font=font,fill="red") del draw img.save(sys.argv[2],"PNG") and here's the image that results: ( for some r...

Extended Form silently fails

Hello, SOvians. I have customized one of my forms and now it does not pass the is_valid() test. No form.errors are visible. Any ideas for where I went wrong? Form: class SearchForm(forms.Form): param = forms.CharField(required=False, max_length = 500, label = 'Search for') sets = forms.ModelMultipleChoiceField(queryset=Set.obje...

Python newbie - Input strings, return a value to a web page

Hi, I've got a program I would like to use to input a password and one or multiple strings from a web page. The program takes the strings and outputs them to a time-datestamped text file, but only if the password matches the set MD5 hash. The problems I'm having here are that I don't know how to get this code on the web. I have a serv...

First Python Program - Multiple Errors

I am trying to write a python program that will eventually take a command line argument of a file, determine if its a tar or zip etc file and then exctract it accordingly. I am just trying to get the tar part working now and I am getting multiple errors. The file I am checking for resides in my ~/ directory. Any ideas would be great. ...

Check even/odd for Palindrome?

Is it a good idea to check for odd/even length of a palindrome number/string? Most snippets I came across don't do this basic test. If length is even, it can't be a palindrome, no? if len(var) % 2 != 0: # could be a palindrome, continue... else: break Or is it just better (i.e faster) to start comparing the first and last numbers...

A few questions regarding Pythons 'import' feature.

I just downloaded Beautiful Soup and I've decided I'll make a small library (is that what they call them in Python?) that will return results of a movie given and IMDB movie search. My question is, how exactly does this import thing work? For example, I downloaded BeautifulSoup and all it is, is a .py file. Does that file have to be in...

Python: arguments for using itertools to split a list into groups

This is a question about the relative merits of fast code that uses the standard library but is obscure (at least to me) versus a hand-rolled alternative. In this thread (and others that it duplicates), it seems the "Pythonic" way to split a list into groups is to use itertools, as in the first function in the code example below (modifi...

Get current timepoint from Totem application

I want to find the exact time where a media file is currently paused at (or playing) in a running Totem instance using D-Bus. To be precise, what I want is available from the Totem python console (if the plugin exists and is enabled) by the following command: >>> print totem_object.props.current_time 732616 which I understand is milli...

module level garbage collection in python

Let's say I have a module mod_x like the following: class X: pass x=X() Now, let's say I have another module that just performs import mod_x, and goes about its business. The module variable x will not be referenced further during the lifecycle of the interpreter. Will the class instance x get garbage collected at any point except...

How can I get Geany to show me the methods a library has when I press the '.' key?

In visual studio, I could just press ctrl+spacekey and the methods appeared. In Geany is there a way for me to get this functionality? ...

Closest language to Python's syntax that is more low level language!

Hi! I guess topic says it all! But, I really wan't a syntax similar to Python's! And low-level... like C++ for example. I guess Java and C# is OK too, but I really have a huge problem with the { }, and always ; <-- and each line. I hate it so much... ...

When Does It Make Sense To Rewrite A Python Module in C?

In a game that I am writing, I use a 2D vector class which I have written to handle the speeds of the objects. This is called a large number of times every frame as there are a lot of objects on the screen, so any increase I can make in its speed will be useful. It is pretty simple, consisting mostly of wrappers to the related math func...

TypeError upon merely opening a file with csv.DictReader?

I've just uploaded this CSV file via a form, POSTing it to my Python CGI script. The upload seems to have completed successfully. Permissions on the folder are 777, on the file are 755. >>> import csv >>> csvHandle = open('files/TestData.csv', "rb") >>> csvRawRecordDicts = csv.DictReader(csvHandle) Traceback (most recent call last): ...

Property XXXX is not multi-line exception in python GAE

I have a simple model object with profilename = db.StringProperty() and when I get a string with "Some More" and try to put it in model it throws exception Property profilename is not multi-line Is space equivalent to newline or I have missed something here? It is put ting for single word strings without spaces....

Fetch certain .html files from web server

I would like to fetch certain .html files from a web server. My intention is to fetch .html files from a web site (http://www.thetabworld.com/) that has a word "metallica" on file name. How is that possible using python? I have heard about urllib2 but as a python noob, I don't have a slightest idea how to use it. ...