python

UnboundLocalError: local variable ... referenced before assignment

I get an UnboundLocalError because I use a template value inside an if statement which is not executed. What is the standard way to handle this situation? class Test(webapp.RequestHandler): def get(self): user = users.get_current_user() if user: greeting = ('Hello, ' + user.nickname()) else...

Sharing queues between processes using nested classes (Python)

Hello everyone, I have a question about sharing queues between processes in Python. Below, I have three queues, one main process, and three inner processes. Each inner process will be adding and getting values from the various queues (they need easy access to the queues). I think it works as it is right now, but this code is the found...

I want to convert a matrix to a list python

Hi there I need to convert a matrix to a list as the example below Matrix: [[ 1. 6. 13. 10. 2.] [ 2. 9. 10. 13. 15.] [ 3. 15. 13. 14. 16.] [ 4. 5. 14. 13. 6.] [ 5. 18. 16. 4. 3.] [ 6. 7. 12. 18. 3.] [ 7. 1. 8. 17. 11.] [ 8. 14. 5. 4. 16.] [ 9. 16. 18. 17. 15.] [ 10. 8...

Java: updated class files not used

I'm developing a Java program through Eclipse locally, and debugging on a remote machine. Whenever I make a change to my program, I copy the corresponding class file to the bin directory on the remote machine. I run my program (a simulator) through a python script via the OS.system command. The problem is that my program sometimes does...

How is the second argument of the eval function different from the third argument?

class all_items(dict): def __getitem__(self, key): return 1 >>> eval("undefined",dict(),all_items()) 1 >>> eval("undefined",all_items(),dict()) Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> eval("undefined",all_items(),dict()) File "<string>", line 1, in <module> NameError: name 'undefined...

printing tab-separated values of a list

Here's my current code: print(list[0], list[1], list[2], list[3], list[4], sep = '\t') I'd like to write it better. But print('\t'.join(list)) won't work because list elements may numbers, other lists, etc., so join would complain. ...

Whats the best way to start learning django?

I really want tobuild a web application (something simple, maybe a database of pokemon cards for instance..) I've heard excellent things about django, wheres the best place to start? ...

Analyse source code in Java using Python ?

Hi everyone, i would like to create a source code analyser for Java Project (like FindBugs and other static analysis programs) that would be able to detect certain method calls. I would prefer to do it using Python, but any advice would be great ! I'm going to start by studying the FindBugs source code, but if anyone could explain to...

How to return local vars([object]) from function?

I use vars() function for the first time, and noticed this behaviour: nodes = ['one', 'two', 'three'] for node in nodes: vars()[node + '_'] = 'some calc ' + node vars()[node] = vars()[node + '_'] print one With this snippet Python outputs some calc one as expected, but if I use it inside function like this: def main(): ...

Python/Tkinter: Using custom mouse cursors under Windows?

Python 2.7/Windows: My understanding is that we can load custom mouse cursors using the cursor='@file.cur' syntax: widget = tkinter.Label( ..., cursor='@help.cur' ) Here's the traceback I receive: Traceback (most recent call last): File "<pyshell#82>", line 1, in <module> widget.config( cursor='@help.cur' ) File "C:\Python27\...

how do I advance to the next item in a nested list? Python

Working with a couple of lists, iterating over each. Here's a code segment: self.links = [] self.iter=iter(self.links) for tgt in self.links: for link in self.mal_list: print(link) if tgt == link: print("Found Suspicious Link: {0}".format(tgt)) self.count += 1 else: self.coun...

Python: How to get the sum of timedelta?

Python: How to get the sum of timedelta? Eg. I just got a lot of timedelta object, and now I want the sum. That's it! ...

How do I see the arguments (and types) of a python method?

$ py twitterDump2.py Traceback (most recent call last): File "twitterDump2.py", line 30, in <module> stream=tweepy.Stream(username,password,listener) TypeError: __init__() takes exactly 3 arguments (4 given) My code: username="abc" password="abc" listener = StreamWatcherListener() stream=tweepy.Stream(username,password,listener...

Execute an installed Python package as a script?

Is there a way to enable a package to be executed as a script? For example: [~]# easy_install /path/to/foo.egg ... [~]# python -m foo --name World Hello World I've tried creating a __main__.py file inside my package but it's not being executed (I'm using Python 2.6). The following error is raised: foo is a package and cannot be dire...

How do I write this to a file? I already implemented the Twitter streaming API (Python)...but it just prints to console.

from getpass import getpass from textwrap import TextWrapper import tweepy import time class StreamWatcherListener(tweepy.StreamListener): status_wrapper = TextWrapper(width=60, initial_indent=' ', subsequent_indent=' ') def on_status(self, status): try: print self.status_wrapper.fill(status.text) ...

How do I set max_allowed_packet or equivalent for MySQLdb in python?

It seems as if MySQLdb is restricting the maximum transfer size for SQL statements. I have set the max_allowed_packet to 128M for mysqld. MySQL documentation says that this needs to be done for the client as well. ...

Strange PEP8 recommandation on comparing Boolean values to True or False

At the end of python PEP8 I'm reading: Don't compare boolean values to True or False using == Yes: if greeting: No: if greeting == True: Worse: if greeting is True: I have no problem with that recommandation when the boolean is True, but it sounds strange when checking for False If I want to know if variable greeting is False...

Impossible to create a configuration file for an python application inside Google App Engine

I try to create a configuration file, where I can store constants. Whenever I try with ConfigParser, I get an error Traceback (most recent call last): File "/home/baun/google_appengine/google/appengine/ext/webapp /__init__.py", line 511, in __call__ handler.get(*groups) File "/home/baun/workspace/octopuscloud/s3/S3.py",...

Broken anydbm.py on ubuntu 10.04 with python 2.6.5?

When trying to run django-celery with beat scheduler: bin/django celeryd -B --settings=app.development --loglevel=INFO I got this exception: Process Beat: Traceback (most recent call last): File "/usr/local/lib/python2.6/dist-packages/multiprocessing/process.py", line 237, in _bootstrap self.run() File "/home/user/eggs/celery-2.1.1-p...

How do you generate a random double between these points?

37.807614 to 37.786996 The randomly generated double must have the same precision (num of digits) as those above. For example, 37.792242 would be good, whereas 37.7823423425 would be bad. ...