python

Parse dict of dicts to string

I have a dictionary with following structure : {1: {'message': u'test', 'user': u'user1'}, 2: {'message': u'test2', 'user': u'user2'}} I'd like to create a string containing values from the inner dictionary in this form : string = "<span>test1</span><span>user1</span><br /> <span>test2</span>..." I've tried everything fro...

Modelling Data with Google App Engine Datastore

I am currently building a web application on Google App Engine in Python to harvest horse racing data of the form. The basic data structure is Course has many Meetings has many Races has many Horses has one Jockey and had one Trainer. So far I have got the following models (reduced number of fields for sake of brevity). class Course(db....

Why am I getting Name Error when importing a class?

Hello, I am just starting to learn Python, but I have already run into some errors. I have made a file called pythontest.py with the following contents: class Fridge: """This class implements a fridge where ingredients can be added and removed individually or in groups""" def __init__(self, items={}): """Optionall...

Multiple authentication options with Tornado

Just started playing with Tornado and want to offer multiple methods of authentication. Currently my app is working fine with Google's hybrid OpenID/oAuth using tornado.auth.GoogleMixin and the unauthenticated users are automatically sent to Google's auth page. If an unauthenticated user wants to use another option (ie. local auth or ...

Good programming podcasts?

Possible Duplicate: What good technology podcasts are out there? Are there good CS programming and Eletronics podcasts? Itunes U has a many classes turned into podcasts, but most of them are terrible to follow without video. ...

Determine IP address of CONNECTED interface (linux) in python

On my linux machine, 1 of 3 network interfaces may be actually connected to the internet. I need to get the IP address of the currently connected interface, keeping in mind that my other 2 interfaces may be assigned IP addresses, just not be connected. I can just ping a website through each of my interfaces to determine which one has co...

python mp3 meta-tag

Hello, I try to write a script than scan recursive a given directory and if found mp3 get and just print meta tag for it. What ever I passed to getEyeD3Tag I got an exception. Here is my code that i have written so far def getEyeD3Tags(path): try: trackInfo = eyeD3.Mp3AudioFile(path) tag = trackInfo.getTag() ...

More efficient ways of doing this

for i in vr_world.getNodeNames(): if i != "_error_": World[i] = vr_world.getChild(i) vr_world.getNodeNames() returns me a gigantic list, vr_world.getChild(i) returns a specific type of object. This is taking a long time to run, is there anyway to make it more efficient? I have seen one-liners for loops before that are sup...

Python - decorator - trying to access the parent class of a method

This doesn't work: def register_method(name=None): def decorator(method): # The next line assumes the decorated method is bound (which of course it isn't at this point) cls = method.im_class cls.my_attr = 'FOO BAR' def wrapper(*args, **kwargs): method(*args, **kwargs) return wrappe...

Excluding a Django app from being localized using a middleware

I need to localize a django project, but keep one of the applications (the blog) English only. I wrote this middleware in order to achieve this: from django.conf import settings from django.core.urlresolvers import resolve class DelocalizeMiddleware: def process_request(self, request): current_app_name = __name__.split('.'...

Implementing Bi-Directional relationships in MongoEngine

I'm building a Django application that uses MongoDB and MongoEngine to store data. To present a simplified and version of my problem, say I want to have two classes: User and Page. Each page should associate itself with a user and each user a page. from mongoengine import * class Page(Document): pass class User(Document): name...

Test framework allowing tests to depend on other tests

I'm wondering if there is a test framework that allows for tests to be declared as being dependent on other tests. This would imply that they should not be run, or that their results should not be prominently displayed, if the tests that they depend on do not pass. The point of such a setup would be to allow the root cause to be more r...

Is there an easier way to package with Python?

I tried to package a django app today. It's a big baby, and with the setup file, I have to manually write all packages and sub packages in the 'package' parameter. Then I have to find a way to copy fixtures, htmls / Css / image files, documentations, etc. It's a terrible way to work. We are computer scientists, we automatize, doing this...

Is it possible to use a USB flash drive to serve files locally to a browser?

Using just python, is it possible to possible to use a USB flash drive to serve files locally to a browser, and save information off the online web? Ideally I would only need python. Where would I start? ...

Special character use in Python 2.6

I am more than a bit tired, but here goes: I am doing tome HTML scraping in python 2.6.5 with BeautifulSoap on an ubuntubox Reason for python 2.6.5: BeautifulSoap sucks under 3.1 I try to run the following code: # dataretriveal from html files from DETHERM # -*- coding: utf-8 -*- import sys,os,re,csv from BeautifulSoup import Beauti...

Chromosome representation for real numbers?

I am trying to solve a problem using genetic algorithms. The problem is to find the set of integral and real values that optimizes a function. I need to represent the problem using a binary string (simply because I understand the concept of crossover/mutation etc much better when applied to binary string chromosomes). A candidate sol...

Can You Use a Single Regular Expression to Parse Function Parameters?

Problem There is a program file that contains the following code snippet at some point in the file. ... food($apples$ , $oranges$ , $pears$ , $tomato$){ ... } ... This function may contain any number of parameters but they must be strings separated by commas. All the parameter strings are lowercase words. I want to be able to pa...

Segmentation fault in Prime Number generator

I am aware the following isn't the fastest way of generating a list of primes however I posed myself the problem and before googling wrote the following program. It works fine for numbers < ~ 44,000 but then gives a segmentation fault when ran on my 2Ghz Core 2 Duo Macbook. I am not really interested in alternative methods at the moment ...

Calling a file in python

Hello, I'd like to call a .py file from within python. It is in the same directory. Effectivly, I would like the same behavior as calling python foo.py from the command line without using any of the command line tools. How should I do this? ...

Finding the nth prime number using Python

When I run this code, even for just counting to the 10th prime number (instead of 1000) I get a skewed/jacked output--all "not prime" titles for my is_composite variable, my test_num is giving me prime and composite numbers, and my prime_count is off Some of the answers that developers have shared use functions and the math import--that...