python

What errors/exceptions do I need to handle with urllib2.Request / urlopen?

I have the following code to do a postback to a remote URL: request = urllib2.Request('http://www.example.com', postBackData, { 'User-Agent' : 'My User Agent' }) try: response = urllib2.urlopen(request) except urllib2.HTTPError, e: checksLogger.error('HTTPError = ' + str(e.code)) except urllib2.URLError, e: checksLogger.er...

PyObjC + Python 3.0 Questions

By default, a Cocoa-Python application uses the default Python runtime which is version 2.5. How can I configure my Xcode project so that it would use the newer Python 3.0 runtime? I tried replacing the Python.framework included in the project with the newer version but it did not work. And another thing, are PyObjc modules compatible w...

Decorator classes in Python

I want to construct classes for use as decorators with the following principles intact: It should be possible to stack multiple such class decorators on top off 1 function. The resulting function name pointer should be indistinguishable from the same function without a decorator, save maybe for just which type/class it is. Ordering off...

Unable to decode unicode string in Python 2.4

This is in python 2.4. Here is my situation. I pull a string from a database, and it contains an umlauted 'o' (\xf6). At this point if I run type(value) it returns str. I then attempt to run .decode('utf-8'), and I get an error ('utf8' codec can't decode bytes in position 1-4). Really my goal here is just to successfully make type(valu...

Mocking a Urllib2.urlopen that raises an HTTPError 404 using pymox

I am try to mock out an urllib2.urlopen that will raise a HTTPError 404. This is what I have so far: import urllib2 import mox # check that it works req = urllib2.Request('http://www.google.com/') print urllib2.urlopen(req) # OK, let's mock it up m = mox.Mox() m.StubOutWithMock(urllib2, 'urlopen') # We can be verbose if we want to :) ...

qt design issue

i'm trying to design interface like this one http://www.softpedia.com/screenshots/FlashFXP_2.png i'm using the QT design and programming with python well on the left it's a treeWidget but what is on the right side ? as everytime i change the cursor on the tree all widgets replace... thanks :p ...

How to get whole text of an Element in xml.minidom?

I want to get the whole text of an Element to parse some xhtml: <div id='asd'> <pre>skdsk</pre> </div> begin E = div element on the above example, I want to get <pre>skdsk</pre> How? ...

How can I blue-box an image?

I have a scanned image which is basically black print on some weird (non-gray) background, say, green or yellow (think old paper). How can I get rid of the green/yellow and receive a gray picture with as much of the gray structure of the original image intact? I.e. I want to keep the gray around the letters for the anti-aliasing effect ...

Finding the static attributes of a class in Python

This is an unusual question, but I'd like to dynamically generate the slots attribute of the class based on whatever attributes I happened to have added to the class. For example, if I have a class: class A(object): one = 1 two = 2 __slots__ = ['one', 'two'] I'd like to do this dynamically rather than specifying the argu...

Crunching xml with python

I need to remove white spaces between xml tags, e.g. if the original xml looks like: <node1> <node2> <node3>foo</node3> </node2> </node1> I'd like the end-result to be crunched down to single line: <node1><node2><node3>foo</node3></node2></node1> Please note that I will not have control over the xml structure, so th...

How to debug a weird threaded open fifo issue?

A web service is configured to expose some of its data when receiving a USR1 signal. The signal will be sent by a xinetd server when it receives a request from a remote client, e.g. nc myserver 50666. When the web server receives USR1 signal, it opens a dedicated fifo pipe, writes its data to the pipe, and then close the pipe. In the mea...

What's a good rate limiting algorithm?

I could use some pseudo-code, or better, Python. I am trying to implement a rate-limiting queue for a Python IRC bot, and it partially works, but if someone triggers less messages than the limit (e.g., rate limit is 5 messages per 8 seconds, and the person triggers only 4), and the next trigger is over the 8 seconds (e.g., 16 seconds la...

Is there a way to manually register a user with a py-transport server-side?

I'm trying to write some scripts to migrate my users to ejabberd, but the only way that's been suggested for me to register a user with a transport is to have them use their client and discover the service. Certainly there is a way, right? ...

Full command line as it was typed

I want to get the full command line as it was typed. This: " ".join(sys.argv[:]) doesn't work here (deletes double quotes). Also I prefer not to rejoin something that was parsed and splited. Any ideas? Thank you in advance. ...

How to tell if a connection is dead in python

I want my python application to be able to tell when the socket on the other side has been dropped. Is there a method for this? ...

How do I create a D-Bus service that dynamically creates multiple objects?

I'm new to D-Bus (and to Python, double whammy!) and I am trying to figure out the best way to do something that was discussed in the tutorial. However, a text editor application could as easily own multiple bus names (for example, org.kde.KWrite in addition to generic TextEditor), have multiple objects (maybe /org/kde/docu...

Can someone explain Gtk2 packing?

I need to use Gtk2 for a project. I will be using python/ruby for it. The problem is that packing seems kind of mystical to me. I tried using a VBox so that I could have the following widgets in my window ( in the following order ): menubar toolbar text view/editor control I've managed to "guess" my way with pack_start and get the la...

Alternatives to ffmpeg as a cli tools for video still extraction?

I need to extract stills from video files. Currently I am using ffmpeg, but I am looking for a simpler tool and for a tool that my collegues can just install. No need to compile it from a svn checkout. Any hints? A python interface would be nice. ...

Python: simple async download of url content?

I have a web.py server that responds to various user requests. One of these requests involves downloading and analyzing a series of web pages. Is there a simple way to setup an async / callback based url download mechanism in web.py? Low resource usage is particularly important as each user initiated request could result in download of...

Programmatic login and use of non-api-supported Google services

Google provides APIs for a number of their services and bindings for several languages. However, not everything is supported. So this question comes from my incomplete understanding of things like wget, curl, and the various web programming libraries. How can I authenticate programmatically to Google? Is it possible to leverage the exi...