python

Is @measured a standard decorator? What library is it in?

In this blog article they use the construct: @measured def some_func(): #... # Presumably outputs something like "some_func() is finished in 121.333 s" somewhere This @measured directive doesn't seem to work with raw python. What is it? UPDATE: I see from Triptych that @something is valid, but is where can I find @measured,...

Using Python's ctypes to pass/read a parameter declared as "struct_name *** param_name"?

I am trying to use Python's ctypes library to access some methods in the scanning library SANE. This is my first experience with ctypes and the first time I have had to deal with C datatypes in over a year so there is a fair learning curve here, but I think even without that this particular declaration would be troublesome: extern SANE...

Django: How can I use my model classes to interact with my database from outside Django?

I'd like to write a script that interacts with my DB using a Django app's model. However, I would like to be able to run this script from the command line or via cron. What all do I need to import to allow this? ...

JavaScript implementation that allows access to [[Call]]

The ECMA standard defines a hidden, internal property [[Call]], which, if implemented, mean the object is callable / is a function. In Python, something similar takes place, except that you can override it yourself to create your own callable objects: >>> class B: ... def __call__(self, x,y): print x,y ... >>> inst = B() >>> inst(1...

How to iterate over a list repeating each element in Python

I'm using Python to infinitely iterate over a list, repeating each element in the list a number of times. For example given the list: l = [1, 2, 3, 4] I would like to output each element two times and then repeat the cycle: 1, 1, 2, 2, 3, 3, 4, 4, 1, 1, 2, 2 ... I've got an idea of where to start: def cycle(iterable): if not has...

How do you or together all values of a list in Python?

How do you or together all values of a list in Python? I'm thinking something like: or([True, True, False]) or if it was possible: reduce(or, [True, True, False]) ...

104, 'Connection reset by peer' socket error, or When does closing a socket result in a RST rather than FIN?

We're developing a Python web service and a client web site in parallel. When we make an HTTP request from the client to the service, one call consistently raises a socket.error in socket.py, in read: (104, 'Connection reset by peer') When I listen in with wireshark, the "good" and "bad" responses look very similar: Because of the s...

What is a python equivalent of PHP's var_dump()

When debugging in PHP I frequently find it useful to simply stick a var_dump($foo, $bar, ...) in my code to show me the the what a variable is, what is value is, and the same for anything that it contains. What is a good python equivalent for this? I have seen several things in my Google searching that are somewhat equivalent, but noth...

Best way to return the language of a given string

More specifically, I'm trying to check if given string (a sentence) is in Turkish. I can check if the string has Turkish characters such as Ç, Ş, Ü, Ö, Ğ etc. However that's not very reliable as those might be converted to C, S, U, O, G before I receive the string. Another method is to have the 100 most used words in Turkish and check...

How can I make the Python logging output to be colored?

Some time ago I saw a Mono application with colored output, probably because of it's log system, because all the messages were standardized. Now, Python has the logging module, and it let you specify a lot of options or customize it entirely, so I'm imagining that something like that would be possible too with Python, however I could not...

MySQL and Python

I'm having some troubles updating a row in a MySQL db. Here is the code I'm trying to run: import MySQLdb conn=MySQLdb.connect(host="localhost", user="root", passwd="pass", db="dbname") cursor=conn.cursor() cursor.execute("UPDATE compinfo SET Co_num=4 WHERE ID=100") cursor.execute("SELECT Co_num FROM compinfo WHERE ID=100") results = ...

Code refactoring help - how to reorganize validations

We have a web application that takes user inputs or database lookups to form some operations against some physical resources. The design can be simply presented as following diagram: user input <=> model object <=> database storage validations are needed with request coming from user input but NOT when coming from database lookup hits ...

How do you manage your Django applications?

Hi, I just wanted to try to build a project with django. Therefore I have a (basic) question on how to manage such a project. Since I cannot find any guidelines or so on how to split a project into applications. Let's take a kind of SO as an example. Which applications would you use? I'd say there should be the applications "users" and ...

Using Python Web GET data

I'm trying to pass information to a python page via the url. I have the following link text: "<a href='complete?id=%s'>" % (str(r[0])) on the complete page, I have this: import cgi def complete(): form = cgi.FieldStorage() db = MySQLdb.connect(user="", passwd="", db="todo") c = db.cursor() c.execute("delete from tasks...

MySQL-db lib for Python 3.0 ?

So, looking for a mysql-db-lib that is compatible with py3k/py3.0/py3000, any ideas? google turned up nothing. ...

PIL and numpy

Alright, I'm toying around with converting a PIL image object back and forth to a numpy array so I can do some faster pixel by pixel transformations than PIL's PixelAccess object would allow. I've figured out how to place the pixel information in a useful 3D numpy array by way of: pic = Image.open("foo.jpg") pix = numpy.array(pic.getda...

Checking 2-dimensional array (like eight queens puzzle)

My problem is very similar to eight queens puzzle. I've got 2-dimensional array (N x N) that for example, looks like this: 0,0,0,0,1 y 0,0,0,0,0 | 0,0,0,0,0 V 0,0,0,1,0 0,0,0,0,0 x-> I'm checking horizontally, vertically and diagonally for occurrences of 1 \,0,|,0,/ 0,\,|,/,0 -,-,1,-,- 0,/,|,\,0 /,0,|,0,\ I'm thinking about storin...

How do I send a custom header with urllib2 in a HTTP Request?

I want to send a custom "Accept" header in my request when using urllib2.urlopen(..). How do I do that? ...

dropping trailing '.0' from floats

I'm looking for a way to convert numbers to string format, dropping any redundant '.0' The input data is a mix of floats and strings. Desired output: 0 --> '0' 0.0 --> '0' 0.1 --> '0.1' 1.0 --> '1' I've come up with the following generator expression, but I wonder if there's a faster way: (str(i).rstrip('.0') if i else '0' for...

Python 3 porting workflow?

I have a small project I want to try porting to Python 3 - how do I go about this? I have made made the code run without warnings using python2.6 -3 (mostly removing .has_key() calls), but I am not sure of the best way to use the 2to3 tool. Use the 2to3 tool to convert this source code to 3.0 syntax. Do not manually edit the output!...