[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gentoo-dev
Subject:    [gentoo-dev] quasi patch, more like python tips ...
From:       Sean Jensen_Grey <seanj () speakeasy ! org>
Date:       2001-02-24 20:21:01
[Download RAW message or body]

Please take this criticism with a grain of salt.

I was taking a look at dopython which apparently takes a series of command line
args and turns them into a function to call in the portage system. Looks like
one can make direct python calls from bash by

dopython functionName arg1 arg2 dot dot dot argN

But I noticed it wasn't very python like

1) the code was bare, and could not be reused by other python modules. By
wrapping all of your code in function definitions and checking the name space
you are in when your code executes makes it very easy for other people to reuse
your python code. I have never seen a language with a higher ability for code
reuse than python. python rocks!

some else could

from dopython import *

and call your function.

2)	kinda looked like c with patterns like

while (x<len(array)):

when you are indeed looping over the elements in the array. A pattern like

for element in array:

is very explicit about looping over all the elements in the array. 

Please take a look at a sample progression of code towards the true path. B^)

## start of python code

array = [ "call", "functions", "at", "midnight", 
	"they", "hate", "to", "wake", "up" ]

#### version in dopython.py

mycommand=array[1]+"("
x=2
while (x<len(array)):
        if x==(len(array)-1):
                mycommand=mycommand+'"'+array[x]+'"'
        else:
                mycommand=mycommand+'"'+array[x]+'",'
        x=x+1

mycommand=mycommand+")"

print mycommand

#### slightly condensed version, more python like, requires v2

mycommand=array[1]+"("

for x in array[2:len(array)-1]:
	mycommand += '"' + x + '",'
mycommand += '"' + array[-1] + '"'

mycommand += ")"

print mycommand

#### turned into a function, sorta self documenting

def generateEvalString(array):
	"""takes an array, and turns it into a function(plus,arguments)
	string that can be sent to eval"""

	# first element is the function name
	mycommand=array[0]+"("
	# all of the next elements are arguments
	for arg in array[1:-1]:
		mycommand += '"' + arg + '",'
	mycommand += '"' + array[-1] + '"'
	mycommand += ")"
	
	return mycommand
	
if __name__ == "__main__":
	# we were called as a program, not a module
	print generateEvalString(array[1:])
	print generateEvalString.__doc__





[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic