python

Is windows's setsockopt broken?

I want to be able to reuse some ports, and that's why I'm using setsockopt on my sockets, with the following code: sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) However, this doesn't really work. I'm not getting a bind error either, but the server socket just isn't responding (it seems to start , but if I try to connect to ...

How to query filter in django without multiple occurrences

I have 2 models: ParentModel: 'just' sits there ChildModel: has a foreign key to ParentModel ParentModel.objects.filter(childmodel__in=ChildModel.objects.all()) gives multiple occurrences of ParentModel. How do I query all ParentModels that have at least one ChildModel that's referring to it? And without multiple occurrences... ...

string formatting

I am not getting why the colon shifted left in the second time >>> print '%5s' %':' : >>> print '%5s' %':' '%2s' %':' : : Help me out of this please ...

import statement fails for one module

Ok I found the problem, it was an environmental issue, I had the same modules (minus options.py) on the sys.path and it was importing from there instead. Thanks everyone for your help. I have a series of import statements, the last of which will not work. Any idea why? options.py is sitting in the same directory as everything else. fr...

how can I debug more than one script in pyscripter?

I installed portable python on my USB drive, and I really like pyscripter a lot. The thing is, after I start debugging a script, the IDE kind of freezes ( waiting for the code to reach a breakpoint ). This means I can't do anything with it ( I can't even save files ). It would be very useful to be able to debug more than one script at a ...

Python "protected" attributes

How do I access a private attribute of a parent class from a subclass (without making it public)? ...

Django caching - can it be done pre-emptively?

I have a Django view, which receives part of its data from an external website, which I parse using urllib2/BeautifulSoup. This operation is rather expensive so I cache it using the low-level cache API, for ~5 minutes. However, each user which accesses the site after the cached data expires will receive a significant delay of a few seco...

How to make two python programs interact?

I have a HTTP sever in one program and my basic application in another one. Both of them are loops, so I have no idea how to: Write a script that would start the app and then the HTTP server; Make these programs exchange data in operation. How are these things usually done? I would really appriciate Python solutions because my script...

Algorithm for neatly indenting SQL statements (Python implementation would be nice)

I'd like to reformat some SQL statements that are a single string with newlines in to something that's much easier to read. I don't personally know of a good coding style for indenting SQL - how should nested queries / where clauses / left joins / etc by represented to maximise readability? Has anyone seen a pretty-printing algorithm t...

Python tool that builds a dependency diagram for methods of a class

I'm digging into a huge legacy Python class that has a lot of methods. I eventually break complex ones into smaller pieces so the amount of methods increases even more. I wonder if there is a tool that can scan the Python code and build some kind of dependency diagram for its methods. I define method x() to be a dependency of method y()...

How to call a Perl script from Python, piping input to it?

I'm hacking some support for DomainKeys and DKIM into an open source email marketing program, which uses a python script to send the actual emails via SMTP. I decided to go the quick and dirty route, and just write a perl script that accepts an email message from STDIN, signs it, then returns it signed. What I would like to do, is from ...

What is the correct (or best) way to subclass the Python set class, adding a new instance variable?

I'm implementing an object that is almost identical to a set, but requires an extra instance variable, so I am subclassing the built-in set object. What is the best way to make sure that the value of this variable is copied when one of my objects is copied? Using the old sets module, the following code worked perfectly: import sets cla...

All combinations of a list of lists

I'm basically looking for a python version of Combination of List<List<int>> Given a list of lists, I need a new list that gives all the possible combinations of items between the lists. [[1,2,3],[4,5,6],[7,8,9,10]] -> [[1,4,7],[1,4,8],...,[3,6,10]] The number of lists is unknown, so I need something that works for all cases. Bonus p...

Python exceptions: call same function for any Exception

Notice in the code below that foobar() is called if any Exception is thrown. Is there a way to do this without using the same line in every Exception? try: foo() except(ErrorTypeA): bar() foobar() except(ErrorTypeB): baz() foobar() except(SwineFlu): print 'You have caught Swine Flu!' foobar() except: foobar() ...

Configure Apache to use Python just like CGI PHP

Hi, I think one commonly known way of adding PHP to an Apache webserver is to configure it like this: ScriptAlias /php5.3 /usr/local/php5.3/bin Action application/php5.3 /php5.3/php-cgi AddType application/php5.3 .php Now I tried to write a similar configuration for Python: ScriptAlias /python /usr/bin Action application/python /pyth...

Print long integers in python

If I run this where vote.created_on is a python datetime: import calendar created_on_timestamp = calendar.timegm(vote.created_on.timetuple())*1000 created_on_timestamp = str(created_on_timestamp) created_on_timestamp will be printed with encapsulating tick marks ('). If I do int() or something like that, I'll get something like 12408...

Python html output (first attempt), several questions (code included)

While I have been playing with python for a few months now (just a hobbyist), I know very little about web programming (a little html, zero javascript, etc). That said, I have a current project that is making me look at web programming for the first time. This led me to ask: http://stackoverflow.com/questions/731470/whats-easiest-way-...

Getting name of windows computer running python script?

Basically, I have a couple Windows computers on my network that will be running a python script. A different set of configuration options should be used in the script depending on which computer is running this script. How would I get that computer name in the python script? Let's say the script was running on a computer named DARK-...

How to get last inserted item's key in google app engine.

I am working with google app engine and python. I have a model with Items. Immediately after I insert an item with item.put() I want to get it's key and redirect to a page using this key something like, redirectUrl = "/view/key/%s/" % item.key self.redirect(redirectUrl) ...

keeping same formatting for floating point values

I have a python program that reads floating point values using the following regular expression (-?\d+\.\d+) once I extract the value using float(match.group(1)), I get the actual floating point number. However, I am not able to distinguish if the number was 1.2345678 or 1.234 or 1.2340000. The problem I am facing is to print out th...