python

Python recursion and return statements

Hi, I'm fairly new to Python and recursive functions as a whole, so pardon my ignorance. I am trying to implement a binary search tree in Python and have the following insert method (taken out of a class): def insert(self, key, root=None): '''Inserts a node in the tree''' if root == None: root = self.root if root.k...

Keep code from running during syncdb

hello again! I have some code that throws causes syncdb to throw an error (because it tries to access the model before the tables are created). Is there a way to keep the code from running on syncdb? something like: if not syncdb: run_some_code() Thanks :) edit: PS - I thought about using the post_init signal... for the code th...

Simplest problem Invalid syntax

I'm learning Python and can't even write the first example: print 2 ** 100 this gives SyntaxError: invalid syntax pointing at the 2. Why is this? I'm using version 3.1 ...

How Python calculate number?

Possible Duplicate: python - decimal place issues with floats In [4]: 52+121.2 Out[4]: 173.19999999999999 ...

Can you pass a dictionary when replacing strings in Python?

In PHP, you have preg_replace($patterns, $replacements, $string), where you can make all your substitutions at once by passing in an array of patterns and replacements. What is the equivalent in Python? I noticed that the string and re functions replace() and sub() don't take dictionaries... Edited to clarify based on a comment by ric...

Use Django ORM as standalone

Possible Duplicates: Use only some parts of Django? Using only the DB part of Django I want to use the Django ORM as standalone. Despite an hour of searching Google, I'm still left with several questions: Does it require me to set up my Python project with a setting.py, /myApp/ directory, and modules.py file? Can I create a ne...

Python Tkinter Tk/Tcl usage Problem

I am using Tcl from Python Tkinter Module like below from Tkinter import * Tcl = Tcl().eval Tcl("info patchlevel") '8.3.5' You can see Tcl version 8.3 is selected by python. But i also have tcl8.4 in my system. Now,how do i make python select tcl8.4 in Tkinter module. Tcl8.3 does not have Expect package,so i can not use Expect pack...

Recursive Relationship with Google App Engine and BigTable

Hello all, In a classic relational database, I have the following table: CREATE TABLE Person( Id int IDENTITY(1,1) NOT NULL PRIMARY KEY, MotherId int NOT NULL REFERENCES Person(Id), FatherId int NOT NULL REFERENCES Person(Id), FirstName nvarchar(255)) I am trying to convert this table into a Google App Engine table. ...

How to match a string of a certain length with a regex

For a project of mine, I'm trying to implement a small part of the BitTorrent protocol, which can be found here. Specifically, I want to use the "Bencoding" part of it, which is a way to safely encode data for transfer over a socket. The format is as follows: 8:a string => "a string" i1234e => 1234 l1:a1:be => ['a', 'b'] d1:a1:b3:one3:t...

Install CherryPy on Linux hosting provider without command line access

I have a linux based web hosting provider (fatcow.com) that doesn't give any command line access and won't run the setup script for CherryPy (python web server) for me. Is there any way to run get around this limitation so that I have a working install of CherryPy? This might be more or a serverfault.com question, but maybe someone her...

Preprocessing route parameters in Python Routes

Hello Folks, I'm using Routes for doing all the URL mapping job. Here's a typical route in my application: map.routes('route', '/show/{title:[^/]+}', controller='generator', filter_=postprocess_title) Quite often I have to strip some characters (like whitespace and underscore) from the {title} parameter. Currently there's one call pe...

Bad Practice to run code in constructor thats likely to fail?

Hello everyone, my question is rather a design question. In Python, if code in your "constructor" fails, the object ends up not being defined. Thus: someInstance = MyClass("test123") #lets say that constructor throws an exception someInstance.doSomething() # will fail, name someInstance not defined. I do have a situation though, wher...

django Authentication using auth.views

User should be redirected to the Login page after registration and after logout. In both cases there must be a message displayed indicating relevant messages. Using the django.contrib.auth.views.login how do I send these {{ info }} messages. A possible option would be to copy the auth.views to new registration module and include all es...

Scope of python lambda functions and their parameters

I need a callback function that is almost exactly the same for a series of gui events. The function will behave slightly differently depending on which event has called it. Seems like a simple case to me, but I cannot figure out this weird behavior of lambda functions. So I have the following simplified code below: def callback(msg): ...

Python total memory used

Is there a way for a Python program to determine how much memory it's currently using? I've seen discussions about memory usage for a single object, but what I need is total memory usage for the process, so that I can determine when it's necessary to start discarding cached data. ...

Extracting YouTube Video's author using Python and YouTubeAPI

Hi, how do I get the author/username from an object using: GetYouTubeVideoEntry(video_id=youtube_video_id_to_output) I'm using Google's gdata.youtube.service Python library Thanks in advance! :) ...

Python IRC bot and encoding issue

Currently I have a simple IRC bot written in python. Since I migrated it to python 3.0 which differentiates between bytes and unicode strings I started having encoding issues. Specifically, with others not sending UTF-8. Now, I could just tell everyone to send UTF-8 (which they should regardless) but an even better solution would be tr...

Pygtk graphics contexts and allocating colors

I've searched on this, but nothing has what I'm looking for. http://www.mail-archive.com/[email protected]/msg10529.html -- Nobody answered him. This is exactly what I'm experiencing. When I set the foreground on a graphics context, it doesn't seem to actually change. I've been through the tutorial and FAQ, but neither say much. They e...

Printed representation of list

Hello! I want to format a list into a string in this way: [1,2,3] => '1 2 3'. How to do this? Is there any customizable formatter in Python as Common Lisp format? ...

Purpose of @ symbols in Python?

I've noticed in several examples i see things such as this: # Comments explaining code i think @innerclass or: def foo(): """ Basic Doc String """ @classmethod Googling doesn't get me very far, for just a general definition of what this is. Also i cant find anything really in the python documentation. What do these do? ...