python

Python urllib3 and how to handle cookie support?

So I'm looking into urllib3 because it has connection pooling and is thread safe (so performance is better, especially for crawling), but the documentation is... minimal to say the least. urllib2 has build_opener so something like: #!/usr/bin/python import cookielib, urllib2 cj = cookielib.CookieJar() opener = urllib2.build_opener(urlli...

Best practices for logging in django project.

Hi It is always good to utilize existing patterns for solving the given problem rather then reinventing the wheel. This time it is about doing logging stuffs in django based project. Can you please share your ideas with me and other about how do you prefer to implement logging in django based project in the following context? What f...

how to call python code in javascript to display bar chart

i have written python code which gives me i/p to chart(json data) but i am not getting how to get these data in my javascript. i want to display it in google app engine i want to do it in same way as we can do it with PHP. ...

In Python, is this a good practice?

try: spam.foo except AttributeError: do_somthing() (Is it wise to check an attribute like that without using it?) ...

The data format for post in urllib2.Request

What should data look like before data encoding in: urllib2.Request(someurl,data) I tried [('name1','value1'),('name2','value2'),...]but not work.:( EDIT: I made a log in the program and recorded the value of urllib.urlencode(data): content=%E5%8F%91%E5%B8%83%E4%BA%86%E4%B8%80%E4%B8%AA%E6%96%B0%E4%B8%BB%E9%A2%98%EF%BC%9A%3Ca+href%3D%2...

creating a frame to put pictures in it and saving back to database

Hello, I'm working on a mobile site and what I have to do is 1.create a picture frame 2.add to it pictures posted by users individually, then save each picture back to the database. Does anyone know how to go about this ...

Uneditable file and Unreadable(for further processing) file( WHY? ) after processing it through C++ Program

Hi...:) This might look to be a very long question to you I understand, but trust me on this its not long. I am not able to identify why after processing this text is not being able to be read and edited. I tried using the ord() function in python to check if the text contains any Unicode characters( non ascii characters) apart from the ...

detect the most used colour in an image using python

Hi, I want to find the most used colour in an image using python. for example detect the colour of the object in the following image http://www.shopcrazy.com.ph/wp-content/images/2007/02/shiny-bags-01.jpg. how to detect the base colour from the RGB codes(example - red in the above image). ...

Python decompressing gzip chunk-by-chunk

I've a memory- and disk-limited environment where I need to decompress the contents of a gzip file sent to me in string-based chunks (over xmlrpc binary transfer). However, using the zlib.decompress() or zlib.decompressobj()/decompress() both barf over the gzip header. I've tried offsetting past the gzip header (documented here), but sti...

Read and overwrite a file in Python

Currently I'm using this: f = open(filename, 'r+') text = f.read() text = re.sub('foobar', 'bar', text) f.seek(0) f.write(text) f.close() But the problem is that the old file is larger than the new file. So I end up with a new file that has a part of the old file on the end of it. ...

Python: installing multiprocessing

I need to import the multiprocessing module in Python 2.5. I've followed the instructions here exactly: http://code.google.com/p/python-multiprocessing/wiki/Install make and make test run without errors. I've also edited $PYTHONPATH to include the directory where the package is installed. But 'import multiprocessing' still says: "Imp...

manipulating list items python

line = "english: while french: pendant que spanish: mientras german: whrend " words = line.split('\t') for each in words: each = each.rstrip() print words the string in 'line' is tab delimited but also features a single white space character after each translated word, so while split returns the list I'm after, each word annoyin...

Append previous lines of a file based on a condition

Hi, I have a text file with a few 1000 lines of text in it. A sample is given below: person1 person2 person3 person4 have paid --------- person5 person6 person7 person9 person10 person11 have paid --------- Each line starts with either "p" or "h" or "-". When "have paid" is encountered while reading the file, I want to append the ...

What is the easiest way to convert list with str into list with int? =)

Hello. What is the easiest way to convert list with str into list with int in python? =) For example, we have to convert ['1','2','3'] to [1,2,3]. Of course, we can use "for:", but it's too easy =) ...

About Python class and instance variables

I've been trying to understand Python's handling of class and instance variables. In particular, I found this answer quite helpful. Basically it says that if you declare a class variable, and then you do an assignment to [instance].property, you will be assigning to a different variable altogether -- one in a different namespace from the...

Regular expression to match C's multiline preprocessor statements

Hello, what I need is to match multiline preprocessor's statements such as: #define max(a,b) \ ({ typeof (a) _a = (a); \ typeof (b) _b = (b); \ _a > _b ? _a : _b; }) The point is to match everything between #define and last }), but I still can't figure out how to write the regexp. I need it to make it work i...

Does the MySQLdb module support prepared statements ?

Does MySQLdb support server-side prepared statements? I can't figure this out from its manual. ...

Getting registry information using Python

I am trying to pull registry info from many servers and put them all into one txt file. I got the code working fine in a .bat file. I hear that there is a way simpler way to do this in Python. I am intrigued and delighted to hear this. Can anyone help finish my code: My working bat file: echo rfsqlcl01app >> foo.txt reg query "\\rfsqlc...

How to get a list with elements that are contained in two other lists?

We have two lists: a=['1','2','3','4'] b=['2','3','4','5'] How to get a list with elements that are contained in both lists: a_and_b=['2','3','4'] and a list with elements that are contained only in one list, but not the other: only_a=['1'] only_b=['5'] Yes, I can use cycles, but it's lame =) ...

Python vs Bash - In which kind of tasks each one outruns the other performance-wise ?

Obviously Python is more user friendly, a quick search on google shows many results that say that, as Python is byte-compiled is usually faster. I even found this that claims that you can see an improvement of over 2000% on dictionary-based operations. What is your experience on this matter? In which kind of task each one is a clear win...