python

Determine proxy type

I have the following code to download a URL through a proxy: proxy_handler = urllib2.ProxyHandler({'http': p}) opener = urllib2.build_opener(proxy_handler) urllib2.install_opener(opener) req = urllib2.Request(url) sock = urllib2.urlopen(req) How can I use Python to determine the type of proxy it is (transparent, anonymous, etc)? One s...

Sorting disk I/O errors in Python

How do I sort out (distinguish) an error derived from a "disk full condition" from "trying to write to a read-only file system"? I don't want to fill my HD to find out :) What I want is to know who to catch each exception, so my code can say something to the user when he is trying to write to a ReadOnly FS and another message if the user...

Django Model Sync Table

If I change a field in a Django model, how can I synchronize it with the database tables? Do I need to do it manually on the database or is there a tool that does helps with the process? ...

Cost of len() function

What is the cost of len() function for Python built-ins? Is it same for all built-ins? (list/tuple/string/dictionary) ...

Static methods and thread safety

In python with all this idea of "Everything is an object" where is thread-safety? I am developing django website with wsgi. Also it would work in linux, and as I know they use effective process management, so we could not think about thread-safety alot. I am not doubt in how module loads, and there functions are static or not? Every inf...

Google App Engine: how to unescape POST body?

Newbie question... I am using silverlight to POST data to my GAE application class XmlCrud(webapp.RequestHandler): def post(self): body = self.request.body The data comes in fine but it is escaped like this: %3C%3Fxml+version=%221.0%22+encoding%3D%22utf-16%22%3F%3E%0D%0A%3CBosses+xmlns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org...

python windows directory mtime: how to detect package directory new file?

Hi folks. I'm working on an auto-reload feature for WHIFF http://whiff.sourceforge.net (so you have to restart the HTTP server less often, ideally never). I have the following code to reload a package module "location" if a file is added to the package directory. It doesn't work on Windows XP. How can I fix it? I think the problem is...

python program choice

My program is ICAPServer (similar with httpserver), it's main job is to receive data from clients and save the data to DB. There are two main steps and two threads: ICAPServer receives data from clients, puts the data in a queue (50kb <1ms); another thread pops data from the queue, and writes them to DB SO, if 2nd step is too slow, th...

Finding content between two words withou RegEx, BeautifulSoup, lXml ... etc

How to find out the content between two words or two sets of random characters? The scraped page is not guaranteed to be Html only and the important data can be inside a javascript block. So, I can't remove the JavaScript. consider this: <html> <body> <div>StartYYYY "Extract HTML", ENDYYYY </body> Some Java Scripts code STARTXXXX "E...

Retrieve cookie created using javascript in python

I've had a look at many tutorials regarding cookiejar, but my problem is that the webpage that i want to scape creates the cookie using javascript and I can't seem to retrieve the cookie. Does anybody have a solution to this problem? ...

Should I use Unicode string by default?

Is it considered as a good practice to pick Unicode string over regular string when coding in Python? I mainly work on the Windows platform, where most of the string types are Unicode these days (i.e. .NET String, '_UNICODE' turned on by default on a new c++ project, etc ). Therefore, I tend to think that the case where non-Unicode strin...

What's the point of this code pattern?

Hi, I was trying to create a python wrapper for an tk extension, so I looked at Tkinter.py to learn how to do it. While looking at that file, I found the following pattern appears a lot of times: an internal method (hinted by the leading "_" in the method name) is defined, then a public method is defined just to be the internal method...

Calculating formulae in Excel with Python

Hi, I would like to insert a calculation in Excel using Python. Generally it can be done by inserting a formula string into the relevant cell. However, if i need to calculate a formula multiple times for the whole column the formula must be updated for each individual cell. For example, if i need to calculate the sum of two cells, the...

html form submission in python and php is simple, can a novice do it in java?

I've made two versions of a script that submits a (https) web page form and collects the results. One version uses Snoopy.class in php, and the other uses urllib and urllib2 in python. Now I would like to make a java version. Snoopy makes the php version exceedingly easy to write, and it runs fine on my own (OS X) machine. But it alloca...

Django Initialization

I have a big array, that I would like to load into memory only once when django starts up and then treat it as a read only global variable. What is the best place to put the code for the initialization of that array? If I put it in settings.py it will be reinitialized every time the settings module is imported, correct? ...

lucene / python

Can I use lucene directly from python, preferably without using a binary module? I am interested mainly in read access -- being able to perform queries from python over existing lucene indexes. ...

Is there any way to get the repeating decimal section of a fraction in Python?

I'm working with fractions using Python's decimal module and I'd like to get just the repeating part of a certain fraction. For example: if I had 1/3 I'd like to get 3, if I had 1/7 I'd like to get 142857. Is there any standard function to do this? ...

How to package a command line Python script

I've created a python script that's intended to be used from the command line. How do I go about packaging it? This is my first python package and I've read a bit about setuptools, but I'm still not sure the best way to do this. Solution I ended up using setup.py with the key configurations noted below: setup( .... entry_points=...

Error running twisted application

I am trying to run a simple twisted application echo bot that metajack blogged about, everything looks like it is going to load fine, but at the very end I get an error: 2009/07/12 15:46 -0600 [-] ImportError: cannot import name toResponse 2009/07/12 15:46 -0600 [-] Failed to load application: cannot import name toResponse Any ideas o...

does python have conversion operators?

I don't think so, but I thought I'd ask just in case. For example, for use in a class that encapsulates an int: i = IntContainer(3) i + 5 And I'm not just interested in this int example, I was looking for something clean and general, not overriding every int and string method. Thanks, sunqiang. That's just what I wanted. I didn't ...