python

Preferred way of defining properties in Python: property decorator or lambda?

Which is the preferred way of defining class properties in Python and why? Is it Ok to use both in one class? @property def total(self): return self.field_1 + self.field_2 or total = property(lambda self: self.field_1 + self.field_2) ...

Adding an object to another module's globals in python

I know this is very evil, but is it possible to add an object to another module's globals, something like: #module dog.py import cat cat.globals.addVar('name','mittens') and #module cat.py print name #mittens ...

How do I set a default page in Pylons?

I've created a new Pylons application and added a controller ("main.py") with a template ("index.mako"). Now the URL http://myserver/main/index works. How do I make this the default page, ie. the one returned when I browse to http://myserver/ ? I've already added a default route in routing.py: def make_map(): """Create, configure a...

Accented characters in matplotlib

Does anyone know a way to get matplotlib to render accented chars (é,ã,â,etc)? For instance i'm trying to use accented chars on set_yticklabels() and matplot renders squares instead, and when i use unicode() it renders the wrong chars. Is there a way to make this work? Thanks in advance, Jim. Update Turns out you can use u"éã" but f...

Why can you reference an imported module using the importing module in python

I am trying to understand why any import can be referenced using the importing module, e.g #module master.py import slave and then >>>import master >>>print master.slave gives <module 'slave' from 'C:\Documents and Settings....'> What is the purpose of the feature? I can see how it can be helpful in a package's __init__.py file, b...

Javascript equivalent of Python's iterkeys() dictionary method

In Python I can use the iterkeys() method to iterate over the keys of a dictionary. For example: mydict = {'a': [3,5,6,43,3,6,3,], 'b': [87,65,3,45,7,8], 'c': [34,57,8,9,9,2],} for k in mydict.iterkeys(): print k gives me: a c b How can I do something similar in Javascript? ...

Why does concatenating a boolean value return an integer?

In python, you can concatenate boolean values, and it would return an integer. Example: >>> True True >>> True + True 2 >>> True + False 1 >>> True + True + True 3 >>> True + True + False 2 >>> False + False 0 Why? Why does this make sense? I understand that True is often represented as 1, whereas False is represented as 0, but that ...

Python urllib2 Basic Auth Problem

Update: based on Lee's comment I decided to condense my code to a really simple script and run it from the command line: import urllib2 import sys username = sys.argv[1] password = sys.argv[2] url = sys.argv[3] print("calling %s with %s:%s\n" % (url, username, password)) passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_...

Change Sikuli's sensitivity?

I've been using sikuli for awhile, however I have an issue with it... It's not sensitive enough. I'm trying to match something on the screen that is -EXACT-, and there are a few other items on the screen that look similar enough that sikuli is mistaking them for what I'm actually looking for, so I need to make it look for ONLY this item ...

Python - merge items of two lists into a list of tuples

What's the pythonic way of achieving the following? list_a = [1, 2, 3, 4] list_b = [5, 6, 7, 8] #Need to create a of tuples from list_a and list_b list_c = [(1,5), (2,6), (3,7), (4,8)] Each member of list_c is a tuple, whose first member is from list_a and the second is from list_b. ...

What does the term "blocking" mean in programming?

Could someone provide a layman definition and use case? ...

How to separate comma separeted data from csv file?

I have opened a csv file and I want to sort each string which is comma separeted and are in same line: ex:: file : name,sal,dept tom,10000,it o/p :: each string in string variable I have a file which is already open, so I can not use "open" API, I have to use "csv.reader" which have to read one line at a time. ...

Creating a list of lists with consecutive numbers

I am looking for a convenient way to create a list of lists for which the lists within the list have consecutive numbers. So far I only came up with a very unsatisfying brute-typing force solution (yeah right, I just use python for a few weeks now): block0 = [] ... block4 = [] blocks = [block0,block1,block2,block3,block4] I appreciat...

django python - generic views and cookies

Hi, I made in my web a menu using generic_view - simple 'django.views.generic.list_detail.object_list' in urls.py file. I would like to set a cookies each time when user chooses one of element of this list [HttpResponse.set_cookie(...)]. What is the best solution? Should I write function in views.py or have you got more simple solution? ...

Python nonblocking console input

I am (trying) to make a simple IRC client in python (as kind of a project while I learn the language). I have a loop that I use to receive and parse what the IRC server sends me, but if I use raw_input to input stuff, it stops the loop dead in its tracks until I input something (obviously). How can I input something without the loop sto...

Why can't I find `len(list)` in Python?

I'm new to Python. I have a method that begins: def foo(self, list): length = len(list) I've called len() successfully in other cases, but here I get: TypeError: object of type 'type' has no len() How do I convince Python that this object passed in is a list? What am I missing? ...

why does python.subprocess hang after proc.communicate()?

I've got an interactive program called my_own_exe. First, it prints out alive, then you input S\n and then it prints out alive again. Finally you input L\n. It does some processing and exits. However, when I call it from the following python script, the program seemed to hang after printing out the first 'alive'. Can anyone here tell m...

python unicode implementation (using external programs: cygnative plink ssh rsync)

I have a backup applications in python that needs to work on Windows. It needs UTF compatibility (to be able to backup directories that contain UTF characters like italian accents). The problem is it uses external programs (plink, cygwin, ssh and rsync) and I can't get them working. The prototype is 32 lines long, please take a look: # ...

Suggestions required for generating this logging file structure in django project

Hi Can anyone please suggest how to generate log files having following directory-file structure using python logging in django project. logs/2009-03-09 /errors.log /warnings.log /info.log /emails.log /messages.log logs/2009-03-08 /errors.log /warnings.log ...

How can I tell if waiting on Event has timed out?

Hello. >>> import threading >>> event = threading.Event() >>> event.set() >>> print event.wait(1) None >>> event.clear() >>> print event.wait(1) None So it basically returns None both when condition was True and False. How can I distinguish the case of timeouting from the one with no waiting at all? Meanwile, the docs say This met...