python

Python - very confused about method and while loop.

I have this method: def is_active(self): if self.is_current_node(): return True cur_children = self.get_children() while cur_children is not None: for child in cur_children: if child.is_current_node(): return True raise Exception(child.display_name) cur_children...

Check that a function raises a warning with nose tests

I'm writing unit tests using nose, and I'd like to check whether a function raises a warning (the function uses warnings.warn). Is this something that can easily be done? ...

oauth & POSTing JSON

Reading section 9.1 of OAuth Core 1.0, I only see a reference to performing POST requests using content-type of application/x-www-form-urlencoded. How does one go about performing POST requests with JSON data in the request body? How does handle the signing? Is it at all possible? Can this work on AppEngine? ...

Foreignkeyfield - verbose name not shown in form

Hi there, my verbose_name of a foreignkeyfield isn't printed in my forms. (I create the modelforms via modelformset_factory model class MOrders(models.Model): amount = models.IntegerField('Bestellmenge', null=True, blank=True) order_date = models.DateField('Bestelldatum') id = models.AutoField(primary_key=True) m_produ...

Python error, only in the command window. Causes site to hang when loading.

I receive this error when files are loading and sometimes after they have stopped loading. It only happens with this site, not with my other django sites. I can't make any sense of this one. Can anyone tell me what's going wrong here? ...

Number of floats between two floats

Say I have two Python floats a and b, is there an easy way to find out how many representable real numbers are between the two in IEEE-754 representation (or whatever representation the machine used is using)? ...

With PyGTK/win32, how do I place a new window near its parent but not off-screen?

With PyGTK running on Windows, I want to have a new window pop up near a parent window, but never off-screen. I observed the behavior I want in Microsoft Wordpad in both Windows 7 and Windows XP. If you make the window very small and move it to the bottom right of the desktop, right click in the text field, and open the Paragraph menu,...

Pickle vs output to a file in python

I have a program that outputs some lists that I want to store to work with later. For example, suppose it outputs a list of student names and another list of their midterm scores. I can store this output in the following two ways: Standard File Output way: newFile = open('trialWrite1.py','w') newFile.write(str(firstNames)) newFile.wri...

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3'

I have an Excel spreadsheet that I'm reading in that contains some £ signs. When I try to read it in using the xlrd module, I get the following error: x = table.cell_value(row, col) x = x.decode("ISO-8859-1") UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 0: ordinal not in range(128) If I rewrite this to...

How to show a vertical rule at the beginning of the current line?

I'm looking for a way in vim to easily visualize the various indent levels of python code. It would help if there was always a vertical rule at the beginning of the current line. That way I can scan down the code to see where the current block ends. Are there any plugins out there that do this? ...

python: how do replace all occurrences of certain characters

i am reading a csv into a: import csv import collections import pdb import math import urllib def do_work(): a=get_file('c:/pythonwork/cds/cds.csv') a=remove_chars(a) print a[0:10] def get_file(start_file): #opens original file, reads it to array with open(start_file,'rb') as f: data=list(csv.reader(f)) return (data) ...

python: keep char only if it is within this list

i have a list: a = ['a','b','c'.........'A','B','C'.........'Z'] and i have string: string1= 's#$%ERGdfhliisgdfjkskjdfW$JWLI3590823r' i want to keep ONLY those characters in string1 that exist in a what is the most effecient way to do this? perhaps instead of having a be a list, i should just make it a string? like this a='abcdefg...

Best Python text editor on Ubuntu?

I'm new to Ubuntu and know it has a command line version of python already installed. However, I want a full text editor. What python editor would be best? How do I install it? ...

It's there anyway to check if the mouse cursor has changed? [Python]

It's anyway to check if the mouse cursor of Windows has changed? For example, I'm on a website and isn't have complete the loading yet, and some specific place on the site has a link to click, but, if I maintain the mouse above the link when the page doesn't complete the load, the cursor will remain the same, but if the page load comple...

How is __eq__ handled in Python and in what order?

Since Python does not provide left/right versions of its comparison operators, how does it decide which function to call? class A(object): def __eq__(self, other): print "A __eq__ called" return self.value == other class B(object): def __eq__(self, other): print "B __eq__ called" return self.value...

Facebook connect with django/python

I've been stuck on this for a while. I can't seem to get facebook login to pop up when following the google tutorial: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"&gt;&lt;/script&gt; <div id="fb-root"></div> <script src="http://connect.facebook.net/en_US/all.js"&gt;&lt;/script&gt; <script> FB.init({app...

Pluggable Python program

I want to make a PyQt4 program that supports plugins. Basically I want the user to be able to write QWidget subclasses in PyQt4 and add/remove them from the main application window via a GUI. How would I do that, especially the plugin mechanism? ...

python enumeration class for ORM purposes

EDITED QUESTION I'm trying to create a class factory that can generate enumeration-like classes with the following properties: Class is initialized from the list of allowed values (i.e., it's automatically generated!). Class creates one instance of itself for each of the allowed value. Class does not allow the creation of any addition...

urllib2.urlopen throws 404 exception for urls that browser opens

Hi. The following url (and others like it) can be opened in a browser but causes urllib2.urlopen to throw a 404 exception: http://store.ovi.com/#/applications?categoryId=20&amp;fragment=1&amp;page=1 geturl() returns the same url (no redirect). The headers are copied and pasted from firebug. I tried passing in the headers as a dictionar...

Django, database retrieval not working but deleting fields and adding new fields is working

I have been able to get my database queries to work properly for deleting existing entries and also adding new entries to the database but I am completely stumped as to why I am unable to retrieve anything from my database. I am trying a query such as: from web1.polls.models import Poll retquery = Poll.objects.all() print retquery --pri...