python

Regex for finding date in Apache access log

I'm writing a python script to extract data out of our 2GB Apache access log. Here's one line from the log. 81.52.143.15 - - [01/Apr/2008:00:07:20 -0600] "GET /robots.txt HTTP/1.1" 200 29 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1) VoilaBot BETA 1.2 (http://www.voila.com/)" I'm trying to get the date portion from that...

Given a date range how to calculate the number of weekends partially or wholly within that range?

Given a date range how to calculate the number of weekends partially or wholly within that range? (A few definitions as requested: take 'weekend' to mean Saturday and Sunday. The date range is inclusive i.e. the end date is part of the range 'wholly or partially' means that any part of the weekend falling within the date range means the...

Optparse: Usage on variable arg callback action does not indicate that extra params are needed

I have implemented in my python code a callback for variable arguments similar to what can be found here: hxxp://docs.python.org/library/optparse.html#callback-example-6-variable-arguments Adding the option like this: parser.add_option("-c", "--callback", dest="vararg_attr", action="callback", callback=vararg_callback) The problem ...

Custom implementation of "tail -f" functionality in C

EDIT: I used, finally, inotify. As stefanB says, inotify is the thing to use. I found a tail clone that uses inotify to implement the -f mode, inotail. Original question text: I'm trying to implement the "tail -f" logic in a C project, for prototyping purposes I developed it in python as follow: # A forever loop, each 5 seconds w...

datetime.now() in Django application goes bad

Hi, I've had some problems with a Django application after I deployed it. I use a Apache + mod-wsgi on a ubuntu server. A while after I reboot the server the time goes foobar, it's wrong by around -10 hours. I made a Django view that looks like: def servertime(): return HttpResponse( datetime.now() ) and after I reboot the server an...

What is an exotic function signature in Python?

I recently saw a reference to "exotic signatures" and the fact they had been deprecated in 2.6 (and removed in 3.0). The example given is def exotic_signature((x, y)=(1,2)): return x+y What makes this an "exotic" signature? ...

How can I create a RSA public key in PEM format from an RSA modulus?

Hi all, I have the modulus of an RSA public key. I want to use this public key with the Python library "M2Crypto", but it requires a public key in PEM format. Thus, I have to convert the RSA modulus to a PEM file. The modulus can be found here. Any ideas? Thanks a lot, hevalbaranov ...

How to return a function value with decorator and thread

have this code import threading def Thread(f): def decorator(*args,**kargs): print(args) thread = threading.Thread(target=f, args=args) thread.start() thread.join() decorator.__name__ = f.__name__ return decorator @Thread def add_item(a, b): return a+b print(add_item(2,2)) but ...

Wrapping a Python Object

Hi Pythonistas, I'd like to serialize Python objects to and from the plist format (this can be done with plistlib). My idea was to write a class PlistObject which wraps other objects: def __init__(self, anObject): self.theObject = anObject and provides a "write" method: def write(self, pathOrFile): plistlib.writeToPlist(se...

Python: Spawn parallel child processes on a multi-processor system - use multiprocessor package, subprocess package, XYZ package?

Hi there, I am a Python newbie so please be gentle :) I have a Python script that I want to use as a controller to another Python script. I have a server with 64 processors, so want to spawn up to 64 child processes of this second Python script. The child script is called: %> python create_graphs.py --name=NAME where NAME is somethi...

How to parse a RFC 2822 date/time into a Python datetime?

I have a date of the form specified by RFC 2822 -- say Fri, 15 May 2009 17:58:28 +0000, as a string. Is there a quick and/or standard way to get it as a datetime object in Python 2.5? I tried to produce a strptime format string, but the +0000 timezone specifier confuses the parser. ...

Django Admin & Model Deletion

I've got a bunch of classes that inherit from a common base class. This common base class does some cleaning up in its delete method. class Base(models.Model): def delete(self): print "foo" class Child(Base): def delete(self): print "bar" super(Child, self).delete() When I call delete on the Child from...

python orm

This is a newbie theory question - I'm just starting to use Python and looking into Django and orm. Question: If I develop my objects and through additional development modify the base object structures, inheritance, etc. - would Django's ORM solution modify the database automatically OR do I need to perform a conversion (if the app is ...

Problems eagerloading a set of object using SQLAlchemy

I'm using Turbogears2 and SQLAlchemy to develop a webapp. I have two mapped tables O1 and O2. O2 has a sorted list of O1s in 'ones'. At some point I want to query all O2's and the referenced O1's. Unfortunately the query below fails because table O2 is aliased in the query and the column referenced by the order_by phrase is no longer kn...

wxPython dialogs: "Enter" keyboard button would not "ok" the dialog

I am creating a custom wxPython dialog by subclassing wx.Dialog. When I press Enter while using it, (and while being focused on one of the form elements,) it just takes the focus to the next form element, while I want it to press the ok button. How do I solve this? ...

Closing and Opening Frames in wxPython

I'm working on writing a very simple client/server application as an excuse to start learning network/gui programming in python. At the moment I'm stuck on transitioning from my login frame to the main frame of the application. The login frame is a subclass of wx.Frame, and basically I just want to close it and open the main frame when...

How do you calculate the greatest number of repetitions in a list?

If I have a list in Python like [1, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 1] how do I calculate the greatest number of repeats for any element? In this case 2 is repeated a maximum of 4 times and 1 is repeated a maximum of 3 times. Is there a way to do this but also record the index at which the longest run began? ...

What is the least resource intense data structure to distribute with a Python Application

I am building an application to distribute to fellow academics. The application will take three parameters that the user submits and output a list of dates and codes related to those events. I have been building this using a dictionary and intended to build the application so that the dictionary loaded from a pickle file when the appli...

dnspython and python objects

I'm trying to use the dnspython library, and am a little confused by their example for querying MX records on this page: www.dnspython.org/examples.html: import dns.resolver answers = dns.resolver.query('dnspython.org', 'MX') for rdata in answers: print 'Host', rdata.exchange, 'has preference', rdata.preference In the python CLI,...

How does MySQL's RENAME TABLE statment work/perform?

MySQL has a RENAME TABLE statemnt that will allow you to change the name of a table. The manual mentions The rename operation is done atomically, which means that no other session can access any of the tables while the rename is running The manual does not (to my knowedge) state how this renaming is accomplished. Is an enti...