python

Run certain code every n seconds

Is there a way to, for example, print Hello World! every n seconds? For example, the program would go through whatever code I had, then once it had been 5 seconds (with time.sleep()) it would execute that code. I would be using this to update a file though, not print Hello World. For example: startrepeat('print('Hello World'), .01) #re...

getting following error while scanning constantly database table using reactor in twisted

Hi, I am getting following error,after few few hours of successful run. Traceback (most recent call last): File "/usr/lib/python2.6/threading.py", line 484, in run self.__target(*self.__args, **self.__kwargs) File "/usr/lib/python2.6/dist-packages/twisted/python/threadpool.py", line 210, in _worker result = context.call(...

How to sync up random generation of strings in Python

How could I ensure that a randomizing algorithm would produce the same random number for two different programs. I am trying to make a chat program that utilizes a shared password, or key and then uses that key to generate a random string that is only predictable to both programs. For example Person A: asd4sa5d8s5s5d5sd Person B: asd4s...

Proper way to process html form in BaseHTTPHandler

I know that I am supposed to use cgi.FieldStorage for that. But what do I initialize it with? def do_GET(self): form = cgi.FieldStorage(WHAT SHOULD BE HERE?!) thanks! I did search, but didn't find an answer :( ...

File Downloader with GUI progress display?

I am trying to write a file downloader that has a GUI and displays the progress of the file being downloaded. I would like it to either display a text percentage, a progress bar or both. I am sure this can be done in Python, but I'm just not sure how. I am using Python 2.6 on MS Windows XP. ...

Python check first and last index of a list

Assuming I have object_list list which contains objects. I want to check if my current iteration is is at the first or the last. for object in object_list: do_something if first_indexed_element: do_something_else if last_indexed_element: do_another_thing How can this be achieved? I know that I can use rang...

What are good programming questions to exercise the use of "if ... else" in Python?

What would be a good set of programming exercises that would help Python newbies to learn the use of the "if ... else" construct? I could cook up the following, do you know of any more? Find the largest/smallest of three numbers. Given a date (year, month, day), find the next date. Most of the intended audience have not had much of a...

*args and **kwargs ?

So I have difficulty with the concept of *args and **kwargs. So far I have learned that: *args = list of arguments -as positional arguments **kwargs = dictionary - whose keys become separate keyword arguments and the values become values of these arguments. ?? To be honest I don't understand and don't get for what programming task ...

sending colored text to a TextCtrl in wxpython

I'm trying to send colored text to a TextCtrl widget, but don't know how style = wx.TE_MULTILINE|wx.BORDER_SUNKEN|wx.TE_READONLY|wx.TE_RICH2 self.status_area = wx.TextCtrl(self.panel, -1, pos=(10, 270),style=style, size=(380,150)) basically that snippet defines a status box...

Using multiple arguments for string formatting in Python (e.g., '%s ... %s')

I have a string that looks like '%s in %s' and I want to know how to seperate the arguments so that they are two different %s. My mind coming from Java came up with this: '%s in %s' % unicode(self.author), unicode(self.publication) But this doesn't work so how does it look in Python? ...

Aggregating save()s in Django?

I'm using Django with an sqlite backend, and write performance is a problem. I may graduate to a "proper" db at some stage, but for the moment I'm stuck with sqlite. I think that my write performance problems are probably related to the fact that I'm creating a large number of rows, and presumably each time I save() one it's locking, u...

How can I combine the awesomness of SQLAlchemy and EAV DB schemas?

Aight ya'll, I gotta lay a Python question on you... I've been doing some work with Pylons recently and quite like the SQLAlchemy model for database interaction. There's one section of my website though which I think could benefit from an EAV schema. Using this as my table example: id | userid | type | value ---+--------+--------|--...

using c# inside an apache python script

Hi fellow programmers, I have a c# application that defines a membership provider used in a Asp.Net MVC application. And i have an apache httpd server that does authentication with mod_wsgi. The objective is to share the membership provider between the two, so that the authentication information be the same. How can i achieve this beh...

In Django, searching and filter by searchbox and categories in one go?

Hello. I wonder if you could help me. I have a list of data that will be displayed on one page. There is a simple search box, a list of categories and a list of tags that can all be used to filter the list of data. I'm trying to built it from the ground up (so it doesn't require JavaScript) but eventually it will submit the search crite...

Python: hexadecimal regular expression question

I want to parse the output of a serial monitoring program called Docklight (I highly recommend it) It outputs 'hexadecimal' strings: or a sequence of (two capital hex digits followed by a space). the corresponding regular expression is: ([0-9A-F]{2} )+ for example: '05 03 DA 4B 3F ' When program detects particular sequences of character...

Recursive (pedigree) tree building from (child, parent) list in Python

Hi All, I have as input a list of tuples (child, parent). Given that a child only have one parent. I would like for every child to build an ordered ancestors list. Any tip ? input would be like : [('3', '4'), ('1', '2'), ('2', '3')] output would be like: 1, [2, 3, 4] 2, [3, 4] 3, [4] 4, [None] ...

enumerate()-ing a generator in Python

I'd like to know what happens when I pass the result of a generator function to python's enumerate(). Example: def veryBigHello(): i = 0 while i < 10000000: i += 1 yield "hello" numbered = enumerate(veryBigHello()) for i, word in numbered: print i, word Is the enumeration iterated lazily, or does it slurp ...

Python Fuse calling 'readlink' 6 times in a row

I am implementing a filesystem using Python Fuse. A directory contains only symlinks and as such I return S_IFLNK | 0777 on the getattr method. Now, when I do an ls on the directory, I notice that Linux calls readlink method 6 times in a row for each entry in the directory. Is it a bug on my side or a normal behavior? ...

How to manage subprocess in Python when the output is huge?

I'm controlling long running simulations (hours, days, even weeks) using a bash script that iterates over all wanted parameters. If only one simulation runs concurrently, the output is piped to "tee", else the output is plainly piped ">" to an output file. All output are huge: some log files are ~2GB and could be even bigger. The script...

process_file(sys.argv[1]) IndexError: list index out of range

This is the code I am working with that comes from Practical Programming: import sys def process_file(filename): '''Open, read, and print a file.''' input_file = open(filename, "r") for line in input_file: line = line.strip() print line input_file.close() if __name__ == "__main__": process_file(sys.argv[1]) After import ...