Gentoo Archives: gentoo-dev

From: Sean Jensen_Grey <seanj@×××××××××.org>
To: gentoo-dev@g.o
Subject: [gentoo-dev] quasi patch, more like python tips ...
Date: Sat, 24 Feb 2001 20:20:54
Message-Id: Pine.LNX.4.21.0102241900370.6581-100000@grace.speakeasy.org
1 Please take this criticism with a grain of salt.
2
3 I was taking a look at dopython which apparently takes a series of command line
4 args and turns them into a function to call in the portage system. Looks like
5 one can make direct python calls from bash by
6
7 dopython functionName arg1 arg2 dot dot dot argN
8
9 But I noticed it wasn't very python like
10
11 1) the code was bare, and could not be reused by other python modules. By
12 wrapping all of your code in function definitions and checking the name space
13 you are in when your code executes makes it very easy for other people to reuse
14 your python code. I have never seen a language with a higher ability for code
15 reuse than python. python rocks!
16
17 some else could
18
19 from dopython import *
20
21 and call your function.
22
23 2) kinda looked like c with patterns like
24
25 while (x<len(array)):
26
27 when you are indeed looping over the elements in the array. A pattern like
28
29 for element in array:
30
31 is very explicit about looping over all the elements in the array.
32
33 Please take a look at a sample progression of code towards the true path. B^)
34
35 ## start of python code
36
37 array = [ "call", "functions", "at", "midnight",
38 "they", "hate", "to", "wake", "up" ]
39
40 #### version in dopython.py
41
42 mycommand=array[1]+"("
43 x=2
44 while (x<len(array)):
45 if x==(len(array)-1):
46 mycommand=mycommand+'"'+array[x]+'"'
47 else:
48 mycommand=mycommand+'"'+array[x]+'",'
49 x=x+1
50
51 mycommand=mycommand+")"
52
53 print mycommand
54
55 #### slightly condensed version, more python like, requires v2
56
57 mycommand=array[1]+"("
58
59 for x in array[2:len(array)-1]:
60 mycommand += '"' + x + '",'
61 mycommand += '"' + array[-1] + '"'
62
63 mycommand += ")"
64
65 print mycommand
66
67 #### turned into a function, sorta self documenting
68
69 def generateEvalString(array):
70 """takes an array, and turns it into a function(plus,arguments)
71 string that can be sent to eval"""
72
73 # first element is the function name
74 mycommand=array[0]+"("
75 # all of the next elements are arguments
76 for arg in array[1:-1]:
77 mycommand += '"' + arg + '",'
78 mycommand += '"' + array[-1] + '"'
79 mycommand += ")"
80
81 return mycommand
82
83 if __name__ == "__main__":
84 # we were called as a program, not a module
85 print generateEvalString(array[1:])
86 print generateEvalString.__doc__