python

Using __str__ representation for printing objects in containers in Python

I've noticed that when an instance with an overloaded str method is passed to the print() function as an argument, it prints as intended. However, when passing a container that contains one of those instances to print(), it uses the repr method instead. That is to say, print(x) displays the correct string representation of x, and print(x...

Find number of differences in 2 strings

int n = string.numDifferences("noob", "newb"); // 2 ?? ...

Easiest way to automatically download required modules in Python?

I would like to release a python module I wrote which depends on several packages. What's the easiest way to make it so these packages are programmatically downloaded just in case they are not available on the system that's being run? Most of these modules should be available by easy_install or pip or something like that. I simply want ...

extract specific element from nested elements using lxml html

Hi all I am having some problems that I think can be attributed to xpath problems. I am using the html module from the lxml package to try and get at some data. I am providing the most simplified situation below, but keep in mind the html I am working with is much uglier. <table> <tr> <td> <table> <tr><td></t...

google search rankings and trends api

I'm looking to find an api/program/interface to get the following information. a term(s) overall popularity - ala google trends how a website shows up rank wise in for said term(s) - ala googlesearchpositionfinder and how many websites have the term(s) - standard google, e.g. Searching for foobar and urban dictionary shows up at positi...

Extending Python’s int type to accept only values within a given range

I would like to create a custom data type which basically behaves like an ordinary int, but with the value restricted to be within a given range. I guess I need some kind of factory function, but I cannot figure out how to do it. myType = MyCustomInt(minimum=7, maximum=49, default=10) i = myType(16) # OK i = myType(52) # raises V...

mount command pid

Trying to mount a device and get the pid of mount command. cmd="/bin/mount /dev/sda1 /mnt" os.system(cmd) Now how to obtain the pid of mount command? There plenty of mounted device available on my system, something like ps | grep mount won't work. ...

Installing a Python program on Linux

I wrote a Python program. I would like to add to it an installation script that will set up everything necessary - like desktop icon, entry in the menu, home directory file, etc. I'm working on Linux (ubuntu). When a Python program is installed, what needs to happen in general? I know that it probably depends on the nature of the progr...

Running py.test from emacs

What I would like if for C-c C-c to run py.test and display the output in the other buffer if the name of the file being edited begins with test_ and to normally run py-execute-buffer otherwise. How would I do this? I am using emacs 23.1.1 with python-mode and can access py.test from the command line. ...

How do I install python gtkmozembed package or is there a replacement of python GtkMozEmbed in windows?

self.widget = gtkmozembed.MozEmbed() self.widget.set_size_request(x + 18, y) I can't find a binary installer of gtkmozembed, is there an equivalent that can do the above ? ...

Django model manager didn't work with related object when I do aggregated query

Hi, all. I'm having trouble doing an aggregation query on a many-to-many related field. Here are my models: class SortedTagManager(models.Manager): use_for_related_fields = True def get_query_set(self): orig_query_set = super(SortedTagManager, self).get_query_set() # FIXME `used` is wrongly counted ret...

django + xmppy: send a message to two recipients

I'm trying to use xmpppy for sending jabber-messages from a django-website. This works entirely fine. However, the message only gets sent to the -first- of the recipients in the list. This happens when I run the following function from django, and also if I run it from an interactive python-shell. The weird part though, is that if I ext...

How to read pdf, ppt, xl, doc files content into a string in php/python

Pls suggest me any inbuilt command or package? ...

boost python version

I'm trying to use boost.python library in a C++ project (Windows + VS9) but it always tries to link against pyton25.lib. Is it possible to link with version 2.6.x of python? thanks ...

convert a list of booleans to string

How do I convert this: [True, True, False, True, True, False, True] Into this: 'AB DE G' Note: C and F are missing in the output because the corresponding items in the input list are False. ...

Optimizing code using PIL

Firstly sorry for the long piece of code pasted below. This is my first time actually having to worry about performance of an application so I haven't really ever worried about performance. This piece of code pretty much searches for an image inside another image, it takes 30 seconds to run on my computer, converting the images to greysc...

python bind a type to a variable

I am a Python noob. I create a class as follows: class t1: x = '' def __init__(self, x): self.x = x class t2: y = '' z = '' def __init__(self, x, y, z): self.y = t1.__init__(x) self.z = z Now contrary to C++ or Java, I do not bind the data type to y while writing the class definition. It i...

Python3k ctypes printf

printf returns 1 instead of Hello World! which is the desired result. I googled it and think its because of the changes in the way sequences are treated. How do i modify the code to print "Hello World!"? www.mail-archive.com/[email protected]/msg15119.html import ctypes msvcrt=ctypes.cdll.msvcrt string=b"Hello World!" msvcrt.prin...

How to do this in a pythonic way?

Consider this Python snippet: for a in range(10): if a == 7: pass if a == 8: pass if a == 9: pass else: print "yes" How can it be written shorter? #Like this or... if a ?????[7,8,9]: pass ...

Editting ForeignKey from "child" table

I'm programming on py with django. I have models: class Product(mymodels.Base): title = models.CharField() price = models.ForeignKey(Price) promoPrice = models.ForeignKey(Price, related_name="promo_price") class Price(mymodels.Base): value = models.DecimalField(max_digits=10, decimal_places=3) taxValue = models.Deci...