python

Python: For each list element apply a function across the list

Given [1,2,3,4,5], how can i do something like 1/1, 1/2, 1/3,1/4,1/5, ...., 3/1,3/2,3/3,3/4,3/5,.... 5/1,5/2,5/3,5/4,5/5 I would like to store all the results, find the minimum, and return the two numbers used to find the minimum. So in the case i've described above i would like to return (1,5). So basically I would like to do somethin...

How to *printf* in Python?

The question is in the title. I'd like to do in Python what I do in this example in C: #include <stdio.h> int main() { int i; for (i=0; i<10; i++) printf("."); return 0; } Output: .......... In Python: >>> for i in xrange(0,10): print '.' . . . . . . . . . . >>> for i in xrange(0,10): print '.', . . . . . . . . . . ...

What's the best way(error proof / foolproof) to parse a file using python with following format?

######################################## # some comment # other comment ######################################## block1 { value=data some_value=some other kind of data othervalue=032423432 } block2 { value=data some_value=some other kind of data othervalue=032423432 } ...

Outputting to a text file

How to print the following code to a .txt file y = '10.1.1.' # /24 network, for x in range(255): x += 1 print y + str(x) # not happy that it's in string, but how to print it into a.txt There's copy paste, but would rather try something more interesting. ...

Python join, why is it string.join(list) instead of list.join(string)?

This has always confused me. It seems like this would be nicer: my_list = ["Hello", "world"] print my_list.join("-") # Produce: "Hello-world" Than this: my_list = ["Hello", "world"] print "-".join(my_list) # Produce: "Hello-world" Is there a specific reason it does it like this? ...

Which Python module is suitable for data manipulation in a list?

I have a sequence of x, y and z -coordinates, which I need to manipulate. They are in one list of three tuples, like {(x1, y1, z1), (x2, y2, z2), ...}. I need addition, multiplication and logarithm to manipulate my data. I would like to study a module, which is as powerful as Awk -language. ...

Uses for Dynamic Languages

My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent compile-time duck typing...

retrieving current URL from FireFox with python

I want to know what is the current url of active tab in running firefox instance from python module. Does FireFox have any API for this and does python know to work with it? ...

CDN options for image resizing

Background: I working on an application on Google App Engine. Its been going really well until I hit one of their limitations in file size -- 1MB. One of the components of my application resizes images, which have been uploaded by users. The files are directly uploaded to S3 (http://developer.amazonwebservices.com/connect/entry.jspa?ext...

How can I disable quoting in the Python 2.4 CSV reader?

I am writing a Python utility that needs to parse a large, regularly-updated CSV file I don't control. The utility must run on a server with only Python 2.4 available. The CSV file does not quote field values at all, but the Python 2.4 version of the csv library does not seem to give me any way to turn off quoting, it just allows me to...

Simulation Problem with mouse in Pygame

I have the following code: import math import pygame from pygame.locals import * # Initialize PyGame pygame.init() pygame.display.set_mode((800,600)) quit = False roundtime = 20 # ms nextupdate = pygame.time.get_ticks() + roundtime framenum = 0 # Watering radius r = 25 # Time step dt = 1 # min field = [0.0]*80*60 while not quit: ...

How to write the Fibonacci Sequence in Python

Updated: EDIT! I have coded the program wrongly. Instead of returning the Fibonacci numbers between a range (ie. startNumber 1, endNumber 20 should = only those numbers between 1 & 20), I have written for the program to display all Fibonacci numbers between a range (ie. startNumber 1, endNumber 20 displays = First 20 Fibonacci numbers). ...

Create a cross-platform protocol helper that runs a python script

I'd like to register a protocol handler, like "myapp:", across OS X, Linux, and Windows, so that when someone clicks a url like "myapp://some/params" in a web browser, a python script will be called and passed in those params. Obviously this would require something being installed on each machine to enable it, but just trying to figure ...

refactor this dictionary-to-xml converter in python

It's a small thing, really: I have this function that converts dict objects to xml. Here's the function: def dictToXml(d): from xml.sax.saxutils import escape def unicodify(o): if o is None: return u''; return unicode(o) lines = [] def addDict(node, offset): for name, value in node....

File and space in Python

I have a file like: <space> <space> line1 <space> column 1 column 2 column 3 ... . . . <space> <space> How to remove this extra spaces? I need to extract the heading which will be on line1. Also, I need to extract column 1, column 2, column 3 etc. At the end of last column content there is '\n'.How to get rid of it ??? H...

HOWTO: XML-RPC dynamic function registration in python?

I am new to XML-RPC. #client code import xmlrpclib proxy = xmlrpclib.ServerProxy("http://localhost:8000/") x,y = arg1,arg2 print proxy.fun1(x,y) print proxy.fun2(x,y) What server should do: load fun1 register fun1 return result unload fun1 and then do same with fun2. What is the best way to do this? I have figured out a way to d...

Refactor this Python code to iterate over a container

Surely there is a better way to do this? results = [] if not queryset is None: for obj in queryset: results.append((getattr(obj,field.attname),obj.pk)) The problem is that sometimes queryset is None which causes an exception when I try to iterate over it. In this case, I just want result to be set to an empty list. This co...

Confusion about global variables in python

Hi, I'm new to python, so please excuse what is probably a pretty dumb question. Basically, I have a single global variable, called _debug, which is used to determine whether or not the script should output debugging information. My problem is, I can't set it in a different python script than the one that uses it. I have two scripts:...

Get rid of '\n' in Python

How to get rid of '\n' at the end of a line ? ...

Best opensource IDE for building applications on Google App Engine?

Looking to dabble with GAE and python, and I'd like to know what are some of the best tools for this - thanks! ...