1. #!/Usr/bin/python
  2. """
  3. general stuff
  4.  
  5. """
  6.  
  7. import re
  8.  
  9.  
  10. #@go
  11. def _rw():
  12. f="sept10"
  13. for line in open(f):
  14. line = re.sub(r"^[ \t\n]*","",line)
  15. line = re.sub(r"[ \t\n]*$","",line)
  16. if line:
  17. print "[" + line + "]"
  18.  
  19. def my2d(m,n) :
  20. out = []
  21. for row in range(m):
  22. tmp =[]
  23. for col in range(n):
  24. tmp += [None]
  25. out += [tmp]
  26. return out
  27.  
  28. def _2d():
  29. x=my2d(5,5)
  30. x[2][3] = 4
  31. print x
  32.  
  33. #print 1
  34. #_2d()
  35.  
  36. #quit();
  37. """
  38. rules of printing
  39.  
  40. - if things contain nested things
  41. - then print the nested things, indented according to the level of nesting
  42. - numbers get printed verbatim
  43. - string get surrounded by rabbit ears
  44. - list get printed one per line
  45. - dictionaries get printed ':key = value'
  46. - sorted on key order
  47. - if the value is simple, print it on the same line
  48. - simple = number or string
  49. - otherwise, find a dictionary containing all
  50. the instance's local variables, then print it
  51. as a dictionary (surrounded by the class name of the instance)
  52. """
  53.  
  54. from go import *
  55.  
  56. import sys
  57. def say(x):
  58. sys.stdout.write(x)
  59.  
  60. #--------------------------------------------------
  61. # here are some structures which will become our
  62. # examples of "stuff to print"
  63.  
  64. class Person(object):
  65. def __init__(i,name,age,shoesize,hobbies=[]):
  66. i.name = name
  67. i.age = age
  68. i.shoesize = shoesize
  69. i.hobbies = hobbies
  70. def bday(i):
  71. i.age += 1
  72.  
  73. class Emp(Person):
  74. def __init__(i,name,age,shoesize,hobbies,salary=0):
  75. super(Emp,i).__init__(name,age,shoesize,hobbies)
  76. i.salary = salary
  77. def promote(i):
  78. i.salary *= 1.1
  79.  
  80. class Point:
  81. def __init__(i,x=0,y=0):
  82. i.x=x
  83. i.y=y
  84. def rprint(i):
  85. return 'Point(%s @ %s)' % (i.x,i.y)
  86.  
  87. x= [Emp("tim",10,10,["reading","writing"],1000),
  88. [
  89. [3,"44"],
  90. 5,
  91. ["66",
  92. [7,8]
  93. ]
  94. ],
  95. Point(1,100)
  96. ]
  97.  
  98. # -----------------------------------------------
  99. # now lets print some stuff
  100.  
  101. def rprint0(x,dpth=-1):
  102. "basic recursive print of lists"
  103. if isinstance(x, list):
  104. for y in x:
  105. rprint0(y,dpth+1)
  106. else:
  107. print ' ' * dpth + str(x)
  108.  
  109. #@go
  110. def _rprint0() : rprint0(x)
  111.  
  112. # -----------------------------------------------
  113.  
  114. def rprint1(x,dpth=-1):
  115. "as above, with rabbit ears around strings"
  116. def listp(x) : return isinstance(x,list)
  117. def stringp(x): return isinstance(x, str)
  118. def quoted(x) : return '\"%s\"' % x if stringp(x) else str(x)
  119. if listp(x):
  120. for y in x:
  121. rprint1(y,dpth+1)
  122. else:
  123. print ' ' * dpth + quoted(x)
  124.  
  125. #@go
  126. def _rprint1() : rprint1(x)
  127.  
  128. # -----------------------------------------------
  129.  
  130. def rprint2(x,dpth=-1):
  131. "as above, but allow instances to control their own printing"
  132. def listp(x) : return isinstance(x,list)
  133. def stringp(x): return isinstance(x, str)
  134. def quoted(x) : return '\"%s\"' % x if stringp(x) else str(x)
  135. def rprintable(x): return hasattr(x,"rprint")
  136. if listp(x):
  137. for y in x:
  138. rprint2(y,dpth+1)
  139. elif rprintable(x):
  140. print ' ' * dpth + ">>" + x.rprint()
  141. else:
  142. print ' ' * dpth + quoted(x)
  143.  
  144. #@go
  145. def _rprint2() : rprint2(x)
  146.  
  147. # -----------------------------------------------
  148.  
  149. def rprint(anything, dpth = 0):
  150. """
  151. As above but if an instance has fields f1,f2,...
  152. then print those fields, but name. also,
  153. if a dictionary has keys k1,k2,.... then print
  154. those keys and values.
  155.  
  156. Also, define some shortcuts to make the code clean.
  157. """
  158. what = lambda x: ':%s' % x
  159. tabs = lambda n: ' ' * n # or 2 or 8 or...
  160. quoted = lambda x: '\"%s\"' % x if stringp(x) else str(x)
  161. equals = lambda x: ' = %s' % quoted(x)
  162. nump = lambda x: isinstance(x, (int, long, float, complex))
  163. dictp = lambda x: isinstance(x, dict)
  164. listp = lambda x: isinstance(x, list)
  165. stringp = lambda x: isinstance(x, str)
  166. rprintable= lambda x: hasattr(x,"rprint")
  167. simple = lambda x: stringp(x) or nump(x)
  168. className = lambda x: x.__class__.__name__
  169. if simple(anything):
  170. print tabs(dpth) + quoted(anything)
  171. elif rprintable(anything):
  172. print tabs(dpth) + anything.rprint()
  173. elif listp(anything):
  174. for something in anything:
  175. rprint(something, dpth+1)
  176. elif dictp(anything):
  177. for key in sorted(anything.keys()):
  178. value = anything[key]
  179. say(tabs(dpth) + what(key))
  180. if simple(value):
  181. print equals(value)
  182. else:
  183. print ""
  184. rprint(value, dpth + 1)
  185. else:
  186. print tabs(dpth) + className(anything) + '('
  187. rprint(anything.__dict__, dpth + 1)
  188. print tabs(dpth) + ')'
  189.  
  190. @go
  191. def _rprint() :
  192. rprint({ 'key1' : list('abcdef'),
  193. 'key2' : 'value2',
  194. 'key3' : { 'key3a': 'value3a' },
  195. 'key4' : { 'key4a': { 'key4aa': list('abcdef'),
  196. 'key4ab': Point(y=100),
  197. 'key4ac': Emp("tim",21,10,
  198. ["flying","swimming"],
  199. salary=10000)},
  200. 'key4b': 100}
  201. })
  202.  
  203.