python

Iterate a format string over a list

In Lisp, you can have something like this: (setf my-stuff '(1 2 "Foo" 34 42 "Ni" 12 14 "Blue")) (format t "~{~d ~r ~s~%~}" my-stuff) What would be the most Pythonic way to iterate over that same list? The first thing that comes to mind is: mystuff = [1, 2, "Foo", 34, 42, "Ni", 12, 14, "Blue"] for x in xrange(0, len(mystuff)-1, 3): ...

What am I doing wrong? Python object instantiation keeping data from previous instantiation?

Can someone point out to me what I'm doing wrong or where my understanding is wrong? To me, it seems like the code below which instantiates two objects should have separate data for each instantiation. class Node: def __init__(self, data = []): self.data = data def main(): a = Node() a.data.append('a-data') #only a...

How can I access another server with Python?

I have two servers, and one updates with a DNSBL of 100k domains every 15 minutes. I want to process these domains through a Python script with information from Safebrowsing, Siteadvisor, and other services. Unfortunately, the server with the DNSBL is rather slow. Is there a way I can transfer the files over from the other server with SS...

How can I fix this bug? BadKeyError: Name must be string type.

Hey everyone. I am using Appengine/Python and I haven't been able to fix a BadKeyError bug for the last 5 hours. I'm wondering if someone can help me figure it out. The part of the app that is causing the bug is a controller that processes votes done by users. Actor_id is the key of the user and object_id is the key of the object that is...

A Faster way of Directory walking instead of os.listdir ?

Hello SO! I am trying to improve performance of elfinder , an ajax based file manager(elRTE.ru) . It uses os.listdir in a recurisve to walk through all directories recursively and having a performance hit (like listing a dir with 3000 + files takes 7 seconds ) .. I am trying to improve performance for it here is it's walking function:...

How do you list all child processes in python?

I'm using a third party library that starts various sub processes. When there's an exception I'd like to kill all the child processes. How can I get a list of child pids? ...

Python's subprocess module returning different results from Unix shell

I'm trying to get a list of the CSV files in a directory with python. This is really easy within unix: ls -l *.csv And, predictably, I get a list of the files that end with .csv in my directory. However, when I attempt the Python equivalent using the Subprocess module: >>> import subprocess as sp >>> sp.Popen(["ls", "-l", "*.csv"], s...

Python: Looping over One Dictionary and Creating Key/Value Pairs in a New Dictionary if Conditions Are Met

I want to compare the values of one dictionary to the values of a second dictionary. If the values meet certain criteria, I want to create a third dictionary with keys and value pairs that will vary depending on the matches. Here is a contrived example that shows my problem. edit: sorry about all the returns, but stack overflow is not...

Python binary file reading problem

I'm trying to read a binary file (which represents a matrix in Matlab) in Python. But I am having trouble reading the file and converting the bytes to the correct values. The binary file consists of a sequence of 4-byte numbers. The first two numbers are the number of rows and columns respectively. My friend gave me a Matlab function he...

Get loop count inside a Python FOR loop

In a Python for loop that iterates over a list we can write: for item in list: print item and it neatly goes through all the elements in the list. Is there a way to know within the loop how many times I've been looping so far? For instance, I want to take a list and after I've processed ten elements I want to do something with the...

Add headers to a file

I have a file containing data like below: 88_NPDJ 565 789 3434 54454 98HGJDN 945 453 3453 23423 ... ... ... whats the best way to add headers to the file? After data has been entered into the file. The data is tab delimited. ...

Unstructured Text to Structured Data

I am looking for references (tutorials, books, academic literature) concerning structuring unstructured text in a manner similar to the google calendar quick add button. I understand this may come under the NLP category, but I am interested only in the process of going from something like "Levi jeans size 32 A0b293" to: Brand: Levi, Si...

Displaying and refreshing my picture every 5 seconds

Ok, I've got the GUI in tkinter working, and I'm trying to grab and image every 5 seconds and display it in a Label named Picturelabel. from Tkinter import * from PIL import ImageGrab import cStringIO, base64, time, threading class PictureThread(threading.Thread): def run(self): print "test" box = (0,0,500,500) #x,x...

Python "List" object is not callable

I'm writing a program that looks through CSVs in a directory and appends the contents of each CSV to a list. Here's a snippet of the offending code: import glob import re c = glob.glob("*.csv") print c archive = [] for element in c: look = open(element, "r").read() open = re.split("\n+", look) for n in open: n = ...

Python search and replace in binary file

I am trying to search and replace some of the text (eg 'Smith, John') in this pdf form file (header.fdf, I presumed this is treated as binary file): '%FDF-1.2\n%\xe2\xe3\xcf\xd3\n1 0 obj\n<</FDF<</Fields[<</V(M)/T(PatientSexLabel)>><</V(24-09-1956 53)/T(PatientDateOfBirth)>><</V(Fisher)/T(PatientLastNameLabel)>><</V(CNSL)/T(PatientCons...

Is there any way to make Tkinter look less windows 95ish?

I was wondering if there was a way to make tkinter more aesthetically pleasing. ...

What does [[]]*2 do in python?

A = [[]]*2 A[0].append("a") A[1].append("b") B = [[], []] B[0].append("a") B[1].append("b") print "A: "+ str(A) print "B: "+ str(B) Yields: A: [['a', 'b'], ['a', 'b']] B: [['a'], ['b']] One would expect that the A list would be the same as the B list, this is not the case, both append statements were applied to A[0] and A[1]. W...

Can someone show me the "hello world" of Paypal IPN?

I'd like to set up a PayPal donation box, and use their IPN protocol to monitor when donations come in. The documentation is enormously complex and full of features I'm not interested in. Is there a short snippet -- ideally in Python -- that shows how to, say, connect to Paypal, loop forever, and print "Just got $5" every time a donation...

Stop evaluation within a module

I've gotten used to writing functions that work like this: def f(): if sunny: return #do non-sunny stuff I'm trying to figure out the equivalent syntax to use within a module. I want to do something like this: if sunny: import tshirt #do something here to skip the rest of the file import raincoat import umbrella #continue...

Python dictionary - binary search for a key?

I want to write a container class that acts like a dictionary (actually derives from a dict), The keys for this structure will be dates. When a key (i.e. date) is used to retrieve a value from the class, if the date does not exist then the next available date that preceeds the key is used to return the value. The following data should ...