python

How to know if urllib.urlretrieve succeeds?

urllib.urlretrieve returns silently even if the file doesn't exist on the remote http server, it just saves a html page to the named file. For example: urllib.urlretrieve('http://google.com/abc.jpg', 'abc.jpg') just returns silently, even if abc.jpg doesn't exist on google.com server, the generated abc.jpg is not a valid jpg file, it'...

How do I operate on the actual object, not a copy, in a python for loop?

let's say I have a list a = [1,2,3] I'd like to increment every item of that list in place. I want to do something as syntactically easy as for item in a: item += 1 but in that example python uses just the value of item, not its actual reference, so when I'm finished with that loop a still returns [1,2,3] instead of [2,3,4]. ...

Converting a String to Dictionary?

How can i convert the following: s = "{'muffin' : 'lolz', 'foo' : 'kitty'}" Into a dictionary object? I'd prefer not to use eval() what should i do? The main reason for this, is one of my coworkers classes he wrote, converts all input into strings. I'm not in the mood to go and modify his classes, to deal with this issue. ...

Efficient Tuple List Comparisons

I am kind of hitting a wall on this problem and I was wondering if some fresh brains could help me out. I have a large list of four element tuples in the format: (ID number, Type, Start Index, End Index) Previously in the code, I have searched through thousands of blocks of text for two specific types of substrings. These tuples stor...

Python string find

I want to find a certain substring inside a string. The string is stored in a list of strings. How can i do it? ...

What's the point of os.error?

Why does Python's os module contain error, an alias for OSError? Is there a reason to ever spell it os.error? OSError certainly seems more consistent with all the other built-in exceptions. I hoped os.py would shed some light, but it uses error sometimes and OSError others. It seems goofy to have an extra name for one of the exception...

python string input problem with whitespace!!

my input is something like this 23 + 45 = astart for the exact input when i take it as raw_input() and then try to split it , it gives me an error like this SyntaxError: invalid syntax the code is this k=raw_input() a,b=(str(i) for i in k.split(' + ')) b,c=(str(i) for i in b.split(' = ')) its always number + number = asta...

Getting the first and last item in a python for loop

Is there an elegant and pythonic way to trap the first and last item in a for loop which is iterating over a generator? from calendar import Calendar cal = Calendar(6) month_dates = cal.itermonthdates(year, month) for date in month_dates: if (is first item): # this is fake month_start = date if (is last item): ...

How to write a proxy server in Python?

I want to write a program that changes the HTTP headers in my requests that are sent by my web-browser. I believe it can be done with a proxy server. So, I'd like to write a proxy server. I study programming. How can I do this in Python? ...

Python and psychopg2 - raw sql select from table with integer criteria

It should be simple, bit I've spent the last hour searching for the answer. This is using psychopg2 on python 2.6. I need something like this: special_id = 5 sql = """ select count(*) as ct, from some_table tbl where tbl.id = %(the_id) """ cursor = connection.cursor() cursor.execute(sql, ...

How do I draw out specific data from an opened url in Python using urllib2?

I'm new to Python and am playing around with making a very basic web crawler. For instance, I have made a simple function to load a page that shows the high scores for an online game. So I am able to get the source code of the html page, but I need to draw specific numbers from that page. For instance, the webpage looks like this: http:...

How to find out the arity of a method in Python

I'd like to find out the arity of a method in Python (the number of parameters that it receives). Right now I'm doing this: def arity(obj, method): return getattr(obj.__class__, method).func_code.co_argcount - 1 # remove self class Foo: def bar(self, bla): pass arity(Foo(), "bar") # => 1 I'd like to be able to achieve this...

Python Global Interpreter Lock (GIL) workaround on multi-core systems using taskset on Linux?

So I just finished watching this talk on the Python Global Interpreter Lock (GIL) http://blip.tv/file/2232410. The gist of it is that the GIL is a pretty good design for single core systems (Python essentially leaves the thread handling/scheduling up to the operating system). But that this can seriously backfire on multi-core systems an...

How do convert unicode escape sequences to unicode characters in a python string

When I tried to get the content of a tag using "unicode(head.contents[3])" i get the output similar to this: "Christensen Sk\xf6ld". I want the escape sequence to be returned as string. How to do it in python? ...

Version of python to learn, revisited.

Possible Duplicate: To learn python 2 then 3, or 3 from the start? I started learning python in 2.6 but didn't get as far as I would have liked before getting overloaded with projects at work. Now, I'd like to start fresh again, but should I go back to 2.6 or 3 now that it's been out for a little while. I searched and found th...

How to get a reference to current module's attributes in Python

What I'm trying to do would look like this in the command line: >>> import mymodule >>> names = dir(mymodule) How can I get a reference to all the names defined in mymodule from within mymodule itself? Something like this: # mymodule.py names = dir(__thismodule__) ...

Python LDAP Authentication from remote web server

I have a django application hosted on webfaction which now has a static/private ip. Our network in the office is obviously behind a firewall and the AD server is running behind this firewall. From inside the network i can authenticate using python-ldap with the AD's internal IP address and the port 389 and all works well. When i move t...

how to take a matrix in python?

i want to create a matrix of size 1234*5678 with it being filled with 1 to 5678 in row major order?>..!! ...

Using doctest "result parser" within unit-tests in Python?

I recently faced a problem about combining unit tests and doctests in Python. I worked around this problem in other way, but I still have question about it. Python's doctest module parses docstrings in a module and run commands following ">>> " at the beginning of each line and compare the output of it and those in docstrings. I wonder...

How to write a proxy-server in Python?

(Edited to note: exact duplicate of 989739) Could you give any suggestions or online sourses as to the matter of writing one's own client proxy-server? In particular, this proxy must be able to make one thing: replace the HTTP-headers sent by browsers with my own ones. ...