python

Is there a way to call a function right before a PyQt application ends?

I am collecting usage stats for my applications which include how much each session lasts. However, I can't seem to be able to save this information because None Of the signals I tried yet actually succeeds to call my report_session function. This are the signals I have already tried: lastWindowClosed() aboutToQuit() destroyed() Ei...

How can I change Menu's Text color?

I want to change Menu's Text color, but not MenuItem. ...

easy_install force a version

Mac os x 10.6. Hello I'm trying to install lxml to solve an inkscape message. I've had a look at the website, and version 2.2.8 looked reasonable to me but when I did easy_install lxml it installed version 2.3.beta1 which is not really what I want I presume. What is the best way to fix this and how can I force easy_install with the versi...

Using a try/catch to retry the same method

I have a class whose methods require that a certain class field exists correctly. That class field is set in the constructor and it's read from a config file, and it may or may not get the correct data from that config file. If the data is incorrect, it will have the wrong data in the class field and the class method will throw an except...

Using a file to find win percentage of teams in file

1) Write a function that reads a file of sports team win/loss records, and computes the win percentage of each. For example, using the file leagueRecords.txt: Lions, 20, 14 Tigers, 31, 0 Bears, 16, 17 Ohmy's, 11, 5 Ferocious Animals, 12, 8 Home Team Name, 15, 22 Screaming Eagles, 22, 13 Yelling Falcons, 16, 14 Loud-t...

Python IMAP call

I am using the imap library to access my unread messages on gmail and to print out the subjects, is there a way to make sure that the messages being read are still tagged as unread. Thanks ...

Python equivalent of Ruby's each_slice(count)

Hey I just wanna know what is pythons equivalent of Ruby's each_slice(count) Like I wanna take 2 elements from list for each iteration. Like for [1,2,3,4,5,6] i wanna handle 1,2 in first iteration then 3,4 then 5,6. Ofcourse there is a roundabout way using index values. But is there a direct function or someway to do this directly? T...

Raise child_exception Errno 2

I'm attempting to convert a C header into a Python library using ctypes and ctypeslib. I'm running Python 2.7, on OSX 10.6.4 (Snow Leopard). The header-file I am converting is mcbcio32.h, located in /header/mcbcio32.h I wish to create an xml output in the same folder, named mcbcio32.xml. I run h2xml.py (which converts the c header into ...

Forcing an interrupt between threads through a singlton object (academic)

So this is a very weird situation, and I'm sure not very pythonic. But I'm not actually using this in any production code, I'm just considering how (if?) this could work. It doesn't have to be python specific, but I'd like a solution that at least WORKS within python framework. Basically, I have a thread safe singleton object that imple...

Python: numpy and matplotlib anomaly

This is the first time I am using matplotlib and numpy. Here goes the problem: If I goto python cli, the intended code works fine. Here is that code >>> from numpy import * >>> y = array([1,2]) >>> y = append(y, y[len(y) - 1]+1) >>> y array([1, 2, 3]) But if I use it with matplotlib in a script I get this error. line 26, in onkeypr...

Python regex to convert non-ascii characters in a string to closest ascii equivalents.

I'm seeking simple Python function that takes a string and returns a similar one but with all non-ascii characters converted to their closest ascii equivalent. For example, diacritics and whatnot should be dropped. I'm imagining there must be a pretty canonical way to do this and there are plenty of related stackoverflow questions but I'...

How to compile python code that uses boto to access S3?

I'm trying to compile a simple Python program, that uploads files to an S3 bucket using the boto package, in to a single, redistributable .exe file. I'm open to any compilation method. So far I've tried both bbfreeze and py2exe and both yield the same results. The code in question that causes trouble looks like this: import boto #...sni...

pyapns - hexlified_token_str

Hi, i try to test pyapns. There is a mention of the hexlified_token_str in the documentation. My token is stored in base64 format. I try to do this >>> notify('myapp', base64.decodestring('Sl96FJtZbZDZECSP3EedQJbsXdtlV+LXWd4+jbzvbHM='), {'aps':{'alert': 'Hello!'}}) But I'm wrong. Traceback (most recent call last): File "<stdin>",...

testing for numeric equality when variable is modified inside loop

I am new to python and I was writing something like: t = 0. while t<4.9: t = t + 0.1 if t == 1.: ... do something ... I noticed that the if statement was never being executed. So I modified the code to look like this: ''' Case a''' t = 0. while t<4.9: t = t + 0.1 print(t) print(t == 5.) When I run t...

Can PHP be more like python?

Possible Duplicate: Does PHP have an equivalent to Python's list comprehension syntax? Does PHP have any equivalent of the simple and awesome list comprehension in python? Specifically, can I do a = [x for x in xrange(1,20)] in PHP w/o annoying loops? ...

Wrong output in Python - as per my logic

Can someone tell me why my program is working weird. I am trying to sort list1 in ascending order. This code is part of my quick sort program I am trying to write. As per my logic which I am applying in this code, and I checked manually too, the output should be [1,2,3,4,5]. However the output is coming out to be [1,2,2,4,5]. Can you tel...

python : using gettext everywhere with __init__.py

Hi there, I would like to use gettext through my application. So, I tried to put the basics into __ init__.py like this : import gettext _ = gettext.gettext gettext.bindtextdomain ( 'brainz', '../datas/translations/' ) gettext.textdomain ( 'brainz' ) And I try simple call in Brainz.py : #!/usr/bin/python from brainz import * ## ...

pygtk: What class should my custom widgets inherit from?

When making a custom widget in pygtk, what class should it inherit from? I want to be able to put the widget inside other widgets, but I don't want other people to put stuff in mine. Usually I make my widgets inherit from gtk.HBox or gtk.VBox, and that works fine, but it is possible then for someone to do a pack_start() on my widget and ...

Python: Compare dict with a static reference?

Hey, I have to check if a dictionary is the same as it was yesterday, if it has changed. In PHP I could have serialized an array and compared the resulting strings from yesterday and today. However, I don't know how to do it in Py. I've read a little about Pickle and maybe it could be done with md5 somehow? So basically I need a way t...

Python - what is the accepted money calculation technique?

Take the following example: >>> from decimal import Decimal >>> nrml_price = Decimal('0.59') >>> discounted = nrml_price / 3 # Taking 2/3 off the price with a coupon Decimal('0.1966666666666666666666666667') # Customers don't have fractions of a penny >>> (nrml_price / 3).quantize(D('0.00')) # So I quantize to get 2 decimal places De...