python

OpenCV Python binds incredibly slow iterations through image data

Hey, I recently took some code that tracked an object based on color in opencv c++ and rewrote it in the python bindings. The overall results and method were the same minus syntax obviously. But, when I perform the below code on each frame of a video it takes almost 2-3 seconds to complete where as the c++ variant, also below, is insta...

Python: email.message_from_string performance with large data in email body

I've been playing around with Python's imaplib and email module recently. I tried sending and receiving large emails (with most of the data in the body of the email rather than attachments) using the imaplib/email modules. However, I've noticed a problem when I download large emails (of size greater than 8MB or so) from the email serve...

Benchmarks of scripting languages doing the same task?

This is not a X vs Y question - do not reply as same. Does anyone know where I could find reviews or reports on tasks that people did in two or more high-level scripting languages to see which one was more suited for a certain job? (note that this is different from looking for a language suited for all jobs!) I really want to know which...

How can I get all the attributes of a HTML tag?

How can I get all the attributes of a HTML tag? listinp = soup('input') for input in listinp: # get all attr on this tag in dict ...

Django annotate groupings by month

Hello, I have a very basic model: class Link(models.Model): title = models.CharField(max_length=250, null=False) user = models.ForeignKey(User) url = models.CharField(max_length=250, blank=True, null=True) link_count = models.IntegerField(default=0) pub_date = models.DateField(auto_now_add=True) updated = models...

Accessing later index in array using enumerate(array) Python

hey guys, how would you access an array from array[n] in an array of 100 floats in this for loop (i need the enumerate): for index,value in enumerate(array): #do stuff with array[n] n=n+1 im trying to make it so that it operates in a smaller and smaller space each iteration.. thanks ...

ASCII art in the optparse description

I'm making a shell script with the optparse module, jut for fun, so I wanted to print a nice ascii drawing in place of the description. Turns out that this code: parser = optparse.OptionParser( prog='./spill.py', description=u''' / \ vvvvvvv /|__/| ...

Script won't run in Python3.0

This script will run as expected and pass doctests without any errors in Python 2.6: def num_even_digits(n): """ >>> num_even_digits(123456) 3 >>> num_even_digits(2468) 4 >>> num_even_digits(1357) 0 >>> num_even_digits(2) 1 >>> num_even_digits(20) 2 """ count = 0 ...

python regex match and replace

I need to find, process and remove (one by one) any substrings that match a rather long regex: # p is a compiled regex # s is a string while 1: m = p.match(s) if m is None: break process(m.group(0)) #do something with the matched pattern s = re.sub(m.group(0), '', s) #remove it from string s The code above is...

In Django, how do I allow print statements to work with Apache WSGI?

"print" only works in development server. But what if I want it to work in Apache? Just in case I forget to comment it out...I want to be able to go smoothly without causing errors. (Just print to nothing) ...

adding text to image using python

Is the following function correct, this code is meant to add a phrase to image. Note that i cannot use image.text function or any other but can only use getpixel, putpixel, load, and save. def insertTxtImage(srcImage, phrase): pixel = srcImage.getpixel(30,30); srcImage.putpixel(pixel,phrase); srcImage.save; pass Yes it is ...

Get variable from parent

Hi, I have two files. main.py that has main program logic and functions.py that has additional functions. Lets say main.py has this code import functions some_var = 'some value' I would like to print out value of some_var in my functions.py file. How can I achieve this. ...

What is the Django way to do this?

class Article(models.Model): def user_may_see_full_version(self, user): # do something very sophisticated with the user return [True/False whatever] now i want to create a template like this: {% for article in articles %} {% if article.user_may_see_full_version request.user %}{{ article }}{% else %}{{article.ti...

Accessing the name of an instance in Python (so I can print it)

So as part of problem 17.6 in "Think Like a Computer Scientist", I've written a class called Kangaroo: class Kangaroo(object): def __init__(self, pouch_contents = []): self.pouch_contents = pouch_contents def __str__(self): ''' >>> kanga = Kangaroo() >>> kanga.put_in_pouch('olfactory') >...

How do I clear all variables in the middle of a Python script?

I am looking for something similar to 'clear' in Matlab: A command/function which removes all variables from the workspace, releasing them from system memory. Is there such a thing in Python? EDIT: I want to write a script which at some point clears all the variables. ...

Can't get mod_wsgi and Apache with Django to work

This is the error I get in my Apache error log: [Sun Aug 22 16:52:06 2010] [error] [client 127.0.0.1] ImportError: No module named settings This is my .wsgi file, per this blog post: import sys sys.path.insert(0, '/home/wot/django-projects/aedo') import settings import django.core.management django.core.management.setup_environ(setti...

Need help with a PIL Error -- IOError: decoder zip not available

I am getting the: IOError: decoder zip not available when I try to draw an image and save to a jpeg in PIL. Any thoughts on how to resolve this? PIL has worked fine for me in the past, when it comes to viewing/uploading images. ...

conditional `ctypedef` with Cython

I need access to the uint64_t typedef from stdint.h in some wrapper code that I'm writing and I can't figure out how to get it done. The problem is that from what I can tell from the docs, my ctypedef will have to take the form: ctypedef unsigned long uint64_t or ctypedef unsigned long long uint64_t depending on if WORDSIZE from bi...

Quickly find differences between two large text files

I have two 3GB text files, each file has around 80 million lines. And they share 99.9% identical lines (file A has 60,000 unique lines, file B has 80,000 unique lines). How can I quickly find those unique lines in two files? Is there any ready-to-use command line tools for this? I'm using Python but I guess it's less possible to find a ...

Uninstall python built from source?

I've installed python 2.6 from source, and somehow later mistakenly installed another python 2.6 from a package manager too. I can't find a way to uninstall a python that was built from source, is this possible/easy? Running ubuntu 10.04 Thanks. ...