python

Is it ok to use dashes in Python files when trying to import them?

Basically when I have a python file like: python-code.py and use: import (python-code) the interpreter gives me syntax error. Any ideas on how to fix it? Are dashes illegal in python file names? ...

Overriding 'to boolean' operator in python?

I'm using a class that is inherited from list as a data structure: class CItem( list ) : pass oItem = CItem() oItem.m_something = 10 oItem += [ 1, 2, 3 ] All is perfect, but if I use my object of my class inside of an 'if', python evaluates it to False if underlying the list has no elements. Since my class is not just list, I reall...

If you were to clone Monopoly Tycoon in Python, what libraries would you use?

Ever played the game Monopoly Tycoon? I think it's great. I would love to remake it. Unfortunately, I have no experience when it comes to 3D programming. I imagine there's a relatively steep learning curve when it comes to openGL stuff, figuring out what is being clicked on and so on... If you were to undertake this task, what librari...

How to get the label of a choice in a Django ChoiceField?

I have a ChoiceField, now how do I get the "label" when I need it? class ContactForm(forms.Form): reason = forms.ChoiceField(choices=[("feature", "A feature"), ("order", "An order")], widget=forms.RadioSelect) form.cleaned_data["reason"] would only give me "...

Trimming a string in Python

I need to write a function in python that gets a string- If the first or last characters in the string are spaces, then they should be removed (both). If not than nothing should be done. " Hello " ----> "Hello" " Hello" -----> "Hello" "Hello " -----> "Hello" "Bob has a cat" ----> "Bob has a cat" (none of the spaces in the middle are...

Active Directory - Django/Rails

I'm thinking about re-writing a web app in Django or Rails and wondering about authenticating against AD. Is one ecosystem better suited for this (libraries, etc) or is it a toss-up? (The app will be hosted on Linux) I have lots of reasons for the re-write, one them is to make myself more marketable. Anyone care to comment on the whic...

Python : How to convert markdown formatted text to text

I need to convert markdown text to plain text format to display summary in my website. I want the code in python. ...

Send headers along in python

I have the following python script and I would like to send "fake" header information along so that my application acts as if it is firefox. How could I do that? import urllib, urllib2, cookielib username = '****' password = '****' login_user = urllib.urlencode({'password' : password, 'username' : username}) jar = cookielib.FileCoo...

Importing In Python

Is it possible to import modules based on location? (eg. do all modules i import have to be in /usr/lib64/python2.5/ or a similar dir?) I'd like to import a module that's local to the current script. ...

Django Template: Comparing Dictionary Length in IF Statement

I am trying to compare the length of a dictionary inside a django template For example, I would like to know the correct syntax to do the following: {% if error_messages %} <div class="error"> {% if length(error_messages) > 1 %} Please fix the following errors: <div class="erroritem"> {% for key, value in error_messa...

Overriding class member variables in Python (Django/Satchmo)

I'm using Satchmo and Django and am trying to extend Satchmo's Product model. I'd like to make one of the fields in Satchmo's Product model have a default value in the admin without changing Satchmo's source code. Here is an abbreviated version of Satchmo's Product model: class Product(models.Model): site = models.ForeignKey(Site,...

Matplotlib Build Problem: Error C1083: Cannot open include file: 'ft2build.h'

ft2build.h is located here: C:\Program Files\GnuWin32\include Initially, I made the same mistake as here: http://stackoverflow.com/questions/160938/fatal-error-c1083-cannot-open-include-file-tiffio-h-no-such-file-or-director but since then, I've corrected that particular error (I've added the above directory to the "include" list, ra...

Python: Is this thread safe?

If I do somthing like this.. import time import threading class test(threading.Thread): def __init__ (self): threading.Thread.__init__(self) self.doSkip = False self.count = 0 def run(self): while self.count<9: self.work() def skip(self): self.doSkip = True def work(self): ...

Python Regexp problem

I'm trying to regexp a line from a webpage. The line is as follows: <tr><td width=60 bgcolor='#ffffcc'><b>random Value</b></td><td align=center width=80> This is what I tried, but it doesn't seem to work, can anyone help me out? 'htmlbody' contains the html page and no, I did not forget to import 're'. reg = re.compile("<tr><td width...

Defining a table with sqlalchemy with a mysql unix timestamp

Background, there are several ways to store dates in MySQ. As a string e.g. "09/09/2009". As integer using the function UNIX_TIMESTAMP() this is supposedly the traditional unix time representation (you know seconds since the epoch plus/minus leap seconds). As a MySQL TIMESTAMP, a mysql specific data type not the same than unix timestam...

How do you store an app engine Image object in the db???

I'm a bit stuck with my code: def setVenueImage(img): img = images.Image(img.read()) x, y = photo_utils.getIdealResolution(img.width, img.height) img.resize(x, y) img.execute_transforms() venue_obj = getVenueSingletonObject() if venue_obj is None: venue_obj = Venue(images = [img]) else: venue_obj.images.append(...

How do i convert WMD markdown syntax to HTML on my site?

Am using django and am implementing WMD on my site, am just wondering how do i convert the markdown syntax to HTML for display purposes, is there some sort of function i should call to do this conversion? What is the best way to handle markdown ie. do i save the markdown as is to the database then parse it when displaying it or should i...

Auto GET to argument of view

some_view?param1=10&param2=20 def some_view(request, param1, param2): Is such possible in Django? ...

How to interact through vim?

I am writing an editor which has lot of parameters that could be easily interacted with through text. I find it inconvenient to implement a separate text-editor or lots of UI code for every little parameter. Usual buttons, boxes and gadgets would be burdensome and clumsy. I'd much rather let user interact with those parameters through vi...

Python Beginner: How to Prevent 'finally' from executing?

The function code: # Connect to the DB try: dbi = MySQLdb.connect(host='localhost', \ user='user', \ passwd='pass', \ db='dbname', \ port=3309) print "Connected to DB ..." except MySQLdb.Error, e: apiErr = 2 apiErrMs...