python

Get last n lines of a file with Python, similar to tail

I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item on the bottom. So I need a tail() method that can read n lines from the bottom and supports an offset. What I came up with looks like this: def tail(f, n, off...

How do you manage your custom modules?

I write tons of python scripts, and I find myself reusing lots code that I've written for other projects. My solution has been to make sure the code is separated into logical modules/packages (this one's a given). I then make them setuptools-aware and publish them on PyPI. This allows my other scripts to always have the most up-to-date c...

Key Presses in Python

Is it possible to make it appear to a system that a key was pressed, for example I need to make a the 'A' key be pressed thousands of times, and it is much to time consuming to do it manually, I would like to write something to do it for me, and the only thing I know well enough is Python. A better way to put it, I need to emulate a ke...

Python language API

Hello. I'm starting with Python coming from java. I was wondering if there exists something similar to JavaDoc API where I can find the class, its methods and and example of how to use it. I've found very helpul to use help( thing ) from the Python ( command line ) I have found this also: http://docs.python.org/ http://docs.python...

How do you make Python / PostgreSQL faster?

Right now I have a log parser reading through 515mb of plain-text files (a file for each day over the past 4 years). My code currently stands as this: http://gist.github.com/12978. I've used psyco (as seen in the code) and I'm also compiling it and using the compiled version. It's doing about 100 lines every 0.3 seconds. The machine is a...

After C++ - Python or Java?

I'm fast approaching the point in my coding where I would like to quickly write object oriented code in languages other than C++ for a variety of reasons. After a lot of research, my choices have pretty much narrowed down to Python and Java. I'm leaning towards Python because of its relationship to C, but with Java, from what I can see...

How can I perform a HEAD request with the mechanize library?

I know how to do a HEAD request with httplib, but I have to use mechanize for this site. Essentially, what I need to do is grab a value from the header (filename) without actually downloading the file. Any suggestions how I could accomplish this? ...

Get Bound Event Handler in Tkinter

After a bind a method to an event of a Tkinter element is there a way to get the method back? >>> root = Tkinter.Tk() >>> frame = Tkinter.Frame(root, width=100, height=100) >>> frame.bind('<Button-1>', lambda e: pprint('Click')) # function needed >>> frame.pack() >>> bound_event_method = frame.??? ...

Is there something like Python's getattr() in C#?

Is there something like Python's getattr() in C#? I would like to create a window by reading a list which contains the names of controls to put on the window. ...

Read the rgb value of a given pixel in Python, Programaticly

If i open an image with open("image.jpg"), how can i get the rgb values of a pixel, if i have the 'coordinates' ( or co-ordinates?) of the pixel? Then how can i do the reverse of this? Starting with a blank graphic, 'write' a pixel with a certain rgb value? It would be so much better if i didn't have to download any additional librarie...

Dynamic radio button creation

In wxPython, if I create a list of radio buttons and place the list initially, is it possible to change the contents in that list later? For example, I have a panel that uses a boxSizer to place the widgets initially. One of those widgets is a list of radio buttons (I have also tried a normal radiobox). I would like to dynamically chang...

Pure Python XSLT library

Is there an XSLT library that is pure Python? Installing libxml2+libxslt or any similar C libraries is a problem on some of the platforms I need to support. I really only need basic XSLT support, and speed is not a major issue. ...

Is it feasible to compile Python to machine code?

How feasible would it be to compile Python (possibly via an intermediate C representation) into machine code? Presumably it would need to link to a Python runtime library, and any parts of the Python standard library which were Python themselves would need to be compiled (and linked in) too. Also, you would need to bundle the Python in...

Can Regex be used for this particular string manipulation?

I need to replace character (say) x with character (say) P in a string, but only if it is contained in a quoted substring. An example makes it clearer: axbx'cxdxe'fxgh'ixj'k -> axbx'cPdPe'fxgh'iPj'k Let's assume, for the sake of simplicity, that quotes always come in pairs. The obvious way is to just process the string one characte...

Vim extension (via Python)?

Howdy, is it possible to extend vim functionality via custom extension (preferably, written in Python)? What I need ideally is custom command when in command mode. E.g. ESC :do_this :do_that ...

How do I test a django database schema?

I want to write tests that can show whether or not the database is in sync with my models.py file. Actually I have already written them, only to find out that django creates a new database each time the tests are run based on the models.py file. Is there any way I can make the models.py test use the existing database schema? The one tha...

PyQt - QScrollBar

Dear Stacktoverflow, can you show me an example of how to use a QScrollBar? Thanks. ...

listing all functions in a python module

I'm not a pythonista, so I'm not sure if this is really obvious or not. I have a python module installed on my system and I'd like to be able to see what functions/classes/methods are available in it so I can call the doc function on each one. In ruby I can do something like ClassName.methods to get a list of all the methods available on...

Adding Cookie to ZSI Posts

I've added cookie support to SOAPpy by overriding HTTPTransport. I need functionality beyond that of SOAPpy, so I was planning on moving to ZSI, but I can't figure out how to put the Cookies on the ZSI posts made to the service. Without these cookies, the server will think it is an unauthorized request and it will fail. How can I add ...

Why results of map() and list comprehension are different?

The following test fails: #!/usr/bin/env python def f(*args): """ >>> t = 1, -1 >>> f(*map(lambda i: lambda: i, t)) [1, -1] >>> f(*(lambda: i for i in t)) # -> [-1, -1] [1, -1] >>> f(*[lambda: i for i in t]) # -> [-1, -1] [1, -1] """ alist = [a() for a in args] print(alist) if __name__ == '__...