PythonUtil.py 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000
  1. import types
  2. import string
  3. import re
  4. import math
  5. import operator
  6. import inspect
  7. import os
  8. import sys
  9. import random
  10. import time
  11. if __debug__:
  12. import traceback
  13. from direct.directutil import Verify
  14. ScalarTypes = (types.FloatType, types.IntType, types.LongType)
  15. def enumerate(L):
  16. """Returns (0, L[0]), (1, L[1]), etc., allowing this syntax:
  17. for i, item in enumerate(L):
  18. ...
  19. enumerate is a built-in feature in Python 2.3, which implements it
  20. using an iterator. For now, we can use this quick & dirty
  21. implementation that returns a list of tuples that is completely
  22. constructed every time enumerate() is called.
  23. """
  24. return zip(xrange(len(L)), L)
  25. import __builtin__
  26. if not hasattr(__builtin__, 'enumerate'):
  27. __builtin__.enumerate = enumerate
  28. def unique(L1, L2):
  29. """Return a list containing all items in 'L1' that are not in 'L2'"""
  30. L2 = dict([(k, None) for k in L2])
  31. return [item for item in L1 if item not in L2]
  32. def indent(stream, numIndents, str):
  33. """
  34. Write str to stream with numIndents in front of it
  35. """
  36. # To match emacs, instead of a tab character we will use 4 spaces
  37. stream.write(' ' * numIndents + str)
  38. def nonRepeatingRandomList(vals, max):
  39. random.seed(time.time())
  40. #first generate a set of random values
  41. valueList=range(max)
  42. finalVals=[]
  43. for i in range(vals):
  44. index=int(random.random()*len(valueList))
  45. finalVals.append(valueList[index])
  46. valueList.remove(valueList[index])
  47. return finalVals
  48. def writeFsmTree(instance, indent = 0):
  49. if hasattr(instance, 'parentFSM'):
  50. writeFsmTree(instance.parentFSM, indent-2)
  51. elif hasattr(instance, 'fsm'):
  52. name = ''
  53. if hasattr(instance.fsm, 'state'):
  54. name = instance.fsm.state.name
  55. print "%s: %s"%(instance.fsm.name, name)
  56. #if __debug__: #RAU accdg to Darren its's ok that StackTrace is not protected by __debug__
  57. # DCR if somebody ends up using StackTrace in production, either
  58. # A) it will be OK because it hardly ever gets called, or
  59. # B) it will be easy to track it down (grep for StackTrace)
  60. class StackTrace:
  61. def __init__(self, label="", start=0, limit=None):
  62. """
  63. label is a string (or anything that be be a string)
  64. that is printed as part of the trace back.
  65. This is just to make it easier to tell what the
  66. stack trace is referring to.
  67. start is an integer number of stack frames back
  68. from the most recent. (This is automatically
  69. bumped up by one to skip the __init__ call
  70. to the StackTrace).
  71. limit is an integer number of stack frames
  72. to record (or None for unlimited).
  73. """
  74. self.label = label
  75. if limit is not None:
  76. self.trace = traceback.extract_stack(sys._getframe(1+start),
  77. limit=limit)
  78. else:
  79. self.trace = traceback.extract_stack(sys._getframe(1+start))
  80. def __str__(self):
  81. r = "Debug stack trace of %s (back %s frames):\n"%(
  82. self.label, len(self.trace),)
  83. for i in traceback.format_list(self.trace):
  84. r+=i
  85. return r
  86. #-----------------------------------------------------------------------------
  87. def traceFunctionCall(frame):
  88. """
  89. return a string that shows the call frame with calling arguments.
  90. e.g.
  91. foo(x=234, y=135)
  92. """
  93. f = frame
  94. co = f.f_code
  95. dict = f.f_locals
  96. n = co.co_argcount
  97. if co.co_flags & 4: n = n+1
  98. if co.co_flags & 8: n = n+1
  99. r=''
  100. if dict.has_key('self'):
  101. r = '%s.'%(dict['self'].__class__.__name__,)
  102. r+="%s("%(f.f_code.co_name,)
  103. comma=0 # formatting, whether we should type a comma.
  104. for i in range(n):
  105. name = co.co_varnames[i]
  106. if name=='self':
  107. continue
  108. if comma:
  109. r+=', '
  110. else:
  111. # ok, we skipped the first one, the rest get commas:
  112. comma=1
  113. r+=name
  114. r+='='
  115. if dict.has_key(name):
  116. v=str(dict[name])
  117. if len(v)>2000:
  118. # r+="<too big for debug>"
  119. r += (str(dict[name])[:2000] + "...")
  120. else:
  121. r+=str(dict[name])
  122. else: r+="*** undefined ***"
  123. return r+')'
  124. def traceParentCall():
  125. return traceFunctionCall(sys._getframe(2))
  126. def printThisCall():
  127. print traceFunctionCall(sys._getframe(1))
  128. return 1 # to allow "assert printThisCall()"
  129. if __debug__:
  130. def lineage(obj, verbose=0, indent=0):
  131. """
  132. return instance or class name in as a multiline string.
  133. Usage: print lineage(foo)
  134. (Based on getClassLineage())
  135. """
  136. r=""
  137. if type(obj) == types.ListType:
  138. r+=(" "*indent)+"python list\n"
  139. elif type(obj) == types.DictionaryType:
  140. r+=(" "*indent)+"python dictionary\n"
  141. elif type(obj) == types.ModuleType:
  142. r+=(" "*indent)+str(obj)+"\n"
  143. elif type(obj) == types.InstanceType:
  144. r+=lineage(obj.__class__, verbose, indent)
  145. elif type(obj) == types.ClassType:
  146. r+=(" "*indent)
  147. if verbose:
  148. r+=obj.__module__+"."
  149. r+=obj.__name__+"\n"
  150. for c in obj.__bases__:
  151. r+=lineage(c, verbose, indent+2)
  152. return r
  153. def tron():
  154. sys.settrace(trace)
  155. def trace(frame, event, arg):
  156. if event == 'line':
  157. pass
  158. elif event == 'call':
  159. print traceFunctionCall(sys._getframe(1))
  160. elif event == 'return':
  161. print "returning"
  162. elif event == 'exception':
  163. print "exception"
  164. return trace
  165. def troff():
  166. sys.settrace(None)
  167. #-----------------------------------------------------------------------------
  168. def getClassLineage(obj):
  169. """
  170. print object inheritance list
  171. """
  172. if type(obj) == types.DictionaryType:
  173. # Just a dictionary, return dictionary
  174. return [obj]
  175. elif (type(obj) == types.InstanceType):
  176. # Instance, make a list with the instance and its class interitance
  177. return [obj] + getClassLineage(obj.__class__)
  178. elif ((type(obj) == types.ClassType) or
  179. (type(obj) == types.TypeType)):
  180. # Class or type, see what it derives from
  181. lineage = [obj]
  182. for c in obj.__bases__:
  183. lineage = lineage + getClassLineage(c)
  184. return lineage
  185. # New FFI objects are types that are not defined.
  186. # but they still act like classes
  187. elif hasattr(obj, '__class__'):
  188. # Instance, make a list with the instance and its class interitance
  189. return [obj] + getClassLineage(obj.__class__)
  190. else:
  191. # Not what I'm looking for
  192. return []
  193. def pdir(obj, str = None, width = None,
  194. fTruncate = 1, lineWidth = 75, wantPrivate = 0):
  195. # Remove redundant class entries
  196. uniqueLineage = []
  197. for l in getClassLineage(obj):
  198. if type(l) == types.ClassType:
  199. if l in uniqueLineage:
  200. break
  201. uniqueLineage.append(l)
  202. # Pretty print out directory info
  203. uniqueLineage.reverse()
  204. for obj in uniqueLineage:
  205. _pdir(obj, str, width, fTruncate, lineWidth, wantPrivate)
  206. print
  207. def _pdir(obj, str = None, width = None,
  208. fTruncate = 1, lineWidth = 75, wantPrivate = 0):
  209. """
  210. Print out a formatted list of members and methods of an instance or class
  211. """
  212. def printHeader(name):
  213. name = ' ' + name + ' '
  214. length = len(name)
  215. if length < 70:
  216. padBefore = int((70 - length)/2.0)
  217. padAfter = max(0, 70 - length - padBefore)
  218. header = '*' * padBefore + name + '*' * padAfter
  219. print header
  220. print
  221. def printInstanceHeader(i, printHeader = printHeader):
  222. printHeader(i.__class__.__name__ + ' INSTANCE INFO')
  223. def printClassHeader(c, printHeader = printHeader):
  224. printHeader(c.__name__ + ' CLASS INFO')
  225. def printDictionaryHeader(d, printHeader = printHeader):
  226. printHeader('DICTIONARY INFO')
  227. # Print Header
  228. if type(obj) == types.InstanceType:
  229. printInstanceHeader(obj)
  230. elif type(obj) == types.ClassType:
  231. printClassHeader(obj)
  232. elif type (obj) == types.DictionaryType:
  233. printDictionaryHeader(obj)
  234. # Get dict
  235. if type(obj) == types.DictionaryType:
  236. dict = obj
  237. # FFI objects are builtin types, they have no __dict__
  238. elif not hasattr(obj, '__dict__'):
  239. dict = {}
  240. else:
  241. dict = obj.__dict__
  242. # Adjust width
  243. if width:
  244. maxWidth = width
  245. else:
  246. maxWidth = 10
  247. keyWidth = 0
  248. aproposKeys = []
  249. privateKeys = []
  250. remainingKeys = []
  251. for key in dict.keys():
  252. if not width:
  253. keyWidth = len(key)
  254. if str:
  255. if re.search(str, key, re.I):
  256. aproposKeys.append(key)
  257. if (not width) and (keyWidth > maxWidth):
  258. maxWidth = keyWidth
  259. else:
  260. if key[:1] == '_':
  261. if wantPrivate:
  262. privateKeys.append(key)
  263. if (not width) and (keyWidth > maxWidth):
  264. maxWidth = keyWidth
  265. else:
  266. remainingKeys.append(key)
  267. if (not width) and (keyWidth > maxWidth):
  268. maxWidth = keyWidth
  269. # Sort appropriate keys
  270. if str:
  271. aproposKeys.sort()
  272. else:
  273. privateKeys.sort()
  274. remainingKeys.sort()
  275. # Print out results
  276. if wantPrivate:
  277. keys = aproposKeys + privateKeys + remainingKeys
  278. else:
  279. keys = aproposKeys + remainingKeys
  280. format = '%-' + `maxWidth` + 's'
  281. for key in keys:
  282. value = dict[key]
  283. if callable(value):
  284. strvalue = `Signature(value)`
  285. else:
  286. strvalue = `value`
  287. if fTruncate:
  288. # Cut off line (keeping at least 1 char)
  289. strvalue = strvalue[:max(1, lineWidth - maxWidth)]
  290. print (format % key)[:maxWidth] + '\t' + strvalue
  291. # Magic numbers: These are the bit masks in func_code.co_flags that
  292. # reveal whether or not the function has a *arg or **kw argument.
  293. _POS_LIST = 4
  294. _KEY_DICT = 8
  295. def _is_variadic(function):
  296. return function.func_code.co_flags & _POS_LIST
  297. def _has_keywordargs(function):
  298. return function.func_code.co_flags & _KEY_DICT
  299. def _varnames(function):
  300. return function.func_code.co_varnames
  301. def _getcode(f):
  302. """
  303. _getcode(f)
  304. This function returns the name and function object of a callable
  305. object.
  306. """
  307. def method_get(f):
  308. return f.__name__, f.im_func
  309. def function_get(f):
  310. return f.__name__, f
  311. def instance_get(f):
  312. if hasattr(f, '__call__'):
  313. method = f.__call__
  314. if (type(method) == types.MethodType):
  315. func = method.im_func
  316. else:
  317. func = method
  318. return ("%s%s" % (f.__class__.__name__, '__call__'), func)
  319. else:
  320. s = ("Instance %s of class %s does not have a __call__ method" %
  321. (f, f.__class__.__name__))
  322. raise TypeError, s
  323. def class_get(f):
  324. if hasattr(f, '__init__'):
  325. return f.__name__, f.__init__.im_func
  326. else:
  327. return f.__name__, lambda: None
  328. codedict = { types.UnboundMethodType: method_get,
  329. types.MethodType: method_get,
  330. types.FunctionType: function_get,
  331. types.InstanceType: instance_get,
  332. types.ClassType: class_get,
  333. }
  334. try:
  335. return codedict[type(f)](f)
  336. except KeyError:
  337. if callable(f): # eg, built-in functions and methods
  338. # raise ValueError, "type %s not supported yet." % type(f)
  339. return f.__name__, None
  340. else:
  341. raise TypeError, ("object %s of type %s is not callable." %
  342. (f, type(f)))
  343. class Signature:
  344. def __init__(self, func):
  345. self.type = type(func)
  346. self.name, self.func = _getcode(func)
  347. def ordinary_args(self):
  348. n = self.func.func_code.co_argcount
  349. return _varnames(self.func)[0:n]
  350. def special_args(self):
  351. n = self.func.func_code.co_argcount
  352. x = {}
  353. #
  354. if _is_variadic(self.func):
  355. x['positional'] = _varnames(self.func)[n]
  356. if _has_keywordargs(self.func):
  357. x['keyword'] = _varnames(self.func)[n+1]
  358. elif _has_keywordargs(self.func):
  359. x['keyword'] = _varnames(self.func)[n]
  360. else:
  361. pass
  362. return x
  363. def full_arglist(self):
  364. base = list(self.ordinary_args())
  365. x = self.special_args()
  366. if x.has_key('positional'):
  367. base.append(x['positional'])
  368. if x.has_key('keyword'):
  369. base.append(x['keyword'])
  370. return base
  371. def defaults(self):
  372. defargs = self.func.func_defaults
  373. args = self.ordinary_args()
  374. mapping = {}
  375. if defargs is not None:
  376. for i in range(-1, -(len(defargs)+1), -1):
  377. mapping[args[i]] = defargs[i]
  378. else:
  379. pass
  380. return mapping
  381. def __repr__(self):
  382. if self.func:
  383. defaults = self.defaults()
  384. specials = self.special_args()
  385. l = []
  386. for arg in self.ordinary_args():
  387. if defaults.has_key(arg):
  388. l.append(arg + '=' + str(defaults[arg]))
  389. else:
  390. l.append(arg)
  391. if specials.has_key('positional'):
  392. l.append('*' + specials['positional'])
  393. if specials.has_key('keyword'):
  394. l.append('**' + specials['keyword'])
  395. return "%s(%s)" % (self.name, string.join(l, ', '))
  396. else:
  397. return "%s(?)" % self.name
  398. def doc(obj):
  399. if (isinstance(obj, types.MethodType)) or \
  400. (isinstance(obj, types.FunctionType)):
  401. print obj.__doc__
  402. def adjust(command = None, dim = 1, parent = None, **kw):
  403. """
  404. adjust(command = None, parent = None, **kw)
  405. Popup and entry scale to adjust a parameter
  406. Accepts any Slider keyword argument. Typical arguments include:
  407. command: The one argument command to execute
  408. min: The min value of the slider
  409. max: The max value of the slider
  410. resolution: The resolution of the slider
  411. text: The label on the slider
  412. These values can be accessed and/or changed after the fact
  413. >>> vg = adjust()
  414. >>> vg['min']
  415. 0.0
  416. >>> vg['min'] = 10.0
  417. >>> vg['min']
  418. 10.0
  419. """
  420. # Make sure we enable Tk
  421. from direct.tkwidgets import Valuator
  422. # Set command if specified
  423. if command:
  424. kw['command'] = lambda x: apply(command, x)
  425. if parent is None:
  426. kw['title'] = command.__name__
  427. kw['dim'] = dim
  428. # Create toplevel if needed
  429. if not parent:
  430. vg = apply(Valuator.ValuatorGroupPanel, (parent,), kw)
  431. else:
  432. vg = apply(Valuator.ValuatorGroup, (parent,), kw)
  433. vg.pack(expand = 1, fill = 'x')
  434. return vg
  435. def difference(a, b):
  436. """
  437. difference(list, list):
  438. """
  439. if not a: return b
  440. if not b: return a
  441. d = []
  442. for i in a:
  443. if (i not in b) and (i not in d):
  444. d.append(i)
  445. for i in b:
  446. if (i not in a) and (i not in d):
  447. d.append(i)
  448. return d
  449. def intersection(a, b):
  450. """
  451. intersection(list, list):
  452. """
  453. if not a: return []
  454. if not b: return []
  455. d = []
  456. for i in a:
  457. if (i in b) and (i not in d):
  458. d.append(i)
  459. for i in b:
  460. if (i in a) and (i not in d):
  461. d.append(i)
  462. return d
  463. def union(a, b):
  464. """
  465. union(list, list):
  466. """
  467. # Copy a
  468. c = a[:]
  469. for i in b:
  470. if (i not in c):
  471. c.append(i)
  472. return c
  473. def sameElements(a, b):
  474. if len(a) != len(b):
  475. return 0
  476. for elem in a:
  477. if elem not in b:
  478. return 0
  479. for elem in b:
  480. if elem not in a:
  481. return 0
  482. return 1
  483. def makeList(x):
  484. """returns x, converted to a list"""
  485. if type(x) is types.ListType:
  486. return x
  487. elif type(x) is types.TupleType:
  488. return list(x)
  489. else:
  490. return [x,]
  491. def makeTuple(x):
  492. """returns x, converted to a tuple"""
  493. if type(x) is types.ListType:
  494. return tuple(x)
  495. elif type(x) is types.TupleType:
  496. return x
  497. else:
  498. return (x,)
  499. def list2dict(L, value=None):
  500. """creates dict using elements of list, all assigned to same value"""
  501. return dict([(k, value) for k in L])
  502. def invertDict(D):
  503. """creates a dictionary by 'inverting' D; keys are placed in the new
  504. dictionary under their corresponding value in the old dictionary.
  505. Data will be lost if D contains any duplicate values.
  506. >>> old = {'key1':1, 'key2':2}
  507. >>> invertDict(old)
  508. {1: 'key1', 2: 'key2'}
  509. """
  510. n = {}
  511. for key, value in D.items():
  512. n[value] = key
  513. return n
  514. def invertDictLossless(D):
  515. """similar to invertDict, but values of new dict are lists of keys from
  516. old dict. No information is lost.
  517. >>> old = {'key1':1, 'key2':2, 'keyA':2}
  518. >>> invertDictLossless(old)
  519. {1: ['key1'], 2: ['key2', 'keyA']}
  520. """
  521. n = {}
  522. for key, value in D.items():
  523. n.setdefault(value, [])
  524. n[value].append(key)
  525. return n
  526. def uniqueElements(L):
  527. """are all elements of list unique?"""
  528. return len(L) == len(list2dict(L))
  529. def disjoint(L1, L2):
  530. """returns non-zero if L1 and L2 have no common elements"""
  531. used = dict([(k, None) for k in L1])
  532. for k in L2:
  533. if k in used:
  534. return 0
  535. return 1
  536. def contains(whole, sub):
  537. """
  538. Return 1 if whole contains sub, 0 otherwise
  539. """
  540. if (whole == sub):
  541. return 1
  542. for elem in sub:
  543. # The first item you find not in whole, return 0
  544. if elem not in whole:
  545. return 0
  546. # If you got here, whole must contain sub
  547. return 1
  548. def replace(list, old, new, all=0):
  549. """
  550. replace 'old' with 'new' in 'list'
  551. if all == 0, replace first occurrence
  552. otherwise replace all occurrences
  553. returns the number of items replaced
  554. """
  555. if old not in list:
  556. return 0
  557. if not all:
  558. i = list.index(old)
  559. list[i] = new
  560. return 1
  561. else:
  562. numReplaced = 0
  563. for i in xrange(len(list)):
  564. if list[i] == old:
  565. numReplaced += 1
  566. list[i] = new
  567. return numReplaced
  568. def reduceAngle(deg):
  569. """
  570. Reduces an angle (in degrees) to a value in [-180..180)
  571. """
  572. return (((deg + 180.) % 360.) - 180.)
  573. def fitSrcAngle2Dest(src, dest):
  574. """
  575. given a src and destination angle, returns an equivalent src angle
  576. that is within [-180..180) of dest
  577. examples:
  578. fitSrcAngle2Dest(30, 60) == 30
  579. fitSrcAngle2Dest(60, 30) == 60
  580. fitSrcAngle2Dest(0, 180) == 0
  581. fitSrcAngle2Dest(-1, 180) == 359
  582. fitSrcAngle2Dest(-180, 180) == 180
  583. """
  584. return dest + reduceAngle(src - dest)
  585. def fitDestAngle2Src(src, dest):
  586. """
  587. given a src and destination angle, returns an equivalent dest angle
  588. that is within [-180..180) of src
  589. examples:
  590. fitDestAngle2Src(30, 60) == 60
  591. fitDestAngle2Src(60, 30) == 30
  592. fitDestAngle2Src(0, 180) == -180
  593. fitDestAngle2Src(1, 180) == 180
  594. """
  595. return src + (reduceAngle(dest - src))
  596. def closestDestAngle2(src, dest):
  597. # The function above didn't seem to do what I wanted. So I hacked
  598. # this one together. I can't really say I understand it. It's more
  599. # from impirical observation... GRW
  600. diff = src - dest
  601. if diff > 180:
  602. # if the difference is greater that 180 it's shorter to go the other way
  603. return dest - 360
  604. elif diff < -180:
  605. # or perhaps the OTHER other way...
  606. return dest + 360
  607. else:
  608. # otherwise just go to the original destination
  609. return dest
  610. def closestDestAngle(src, dest):
  611. # The function above didn't seem to do what I wanted. So I hacked
  612. # this one together. I can't really say I understand it. It's more
  613. # from impirical observation... GRW
  614. diff = src - dest
  615. if diff > 180:
  616. # if the difference is greater that 180 it's shorter to go the other way
  617. return src - (diff - 360)
  618. elif diff < -180:
  619. # or perhaps the OTHER other way...
  620. return src - (360 + diff)
  621. else:
  622. # otherwise just go to the original destination
  623. return dest
  624. def binaryRepr(number, max_length = 32):
  625. # This will only work reliably for relatively small numbers.
  626. # Increase the value of max_length if you think you're going
  627. # to use long integers
  628. assert number < 2L << max_length
  629. shifts = map (operator.rshift, max_length * [number], \
  630. range (max_length - 1, -1, -1))
  631. digits = map (operator.mod, shifts, max_length * [2])
  632. if not digits.count (1): return 0
  633. digits = digits [digits.index (1):]
  634. return string.join (map (repr, digits), '')
  635. # constant profile defaults
  636. PyUtilProfileDefaultFilename = 'profiledata'
  637. PyUtilProfileDefaultLines = 80
  638. PyUtilProfileDefaultSorts = ['cumulative', 'time', 'calls']
  639. def profile(callback, name):
  640. import __builtin__
  641. if 'globalProfileFunc' in __builtin__.__dict__:
  642. # rats. Python profiler is not re-entrant...
  643. base.notify.warning(
  644. 'PythonUtil.profileStart(%s): aborted, already profiling %s'
  645. #'\nStack Trace:\n%s'
  646. % (name, __builtin__.globalProfileFunc,
  647. #StackTrace()
  648. ))
  649. return
  650. __builtin__.globalProfileFunc = callback
  651. print '***** START PROFILE: %s *****' % name
  652. startProfile(cmd='globalProfileFunc()')
  653. print '***** END PROFILE: %s *****' % name
  654. del __builtin__.__dict__['globalProfileFunc']
  655. def profiled(category):
  656. """ decorator for profiling functions
  657. turn categories on and off via "want-profile-categoryName 1"
  658. e.g.
  659. @profiled('particles')
  660. def loadParticles():
  661. ...
  662. """
  663. assert type(category) is types.StringType, "must provide a category name for @profiled"
  664. if not __dev__:
  665. # if we're not in __dev__, just return the function itself. This
  666. # results in zero runtime overhead, since decorators are evaluated
  667. # at module-load.
  668. def nullDecorator(f):
  669. return f
  670. return nullDecorator
  671. def profileDecorator(f):
  672. def _profiled(*args, **kArgs):
  673. #import pdb;pdb.set_trace()
  674. # must do this in here because we don't have base/simbase
  675. # at the time that PythonUtil is loaded
  676. name = '(%s) %s from %s' % (category, f.func_name, f.__module__)
  677. try:
  678. _base = base
  679. except:
  680. _base = simbase
  681. if _base.config.GetBool('want-profile-%s' % category, 0):
  682. return profile(Functor(f, *args, **kArgs), name)
  683. else:
  684. return f(*args, **kArgs)
  685. #import pdb;pdb.set_trace()
  686. _profiled.__doc__ = f.__doc__
  687. return _profiled
  688. return profileDecorator
  689. # call this from the prompt, and break back out to the prompt
  690. # to stop profiling
  691. #
  692. # OR to do inline profiling, you must make a globally-visible
  693. # function to be profiled, i.e. to profile 'self.load()', do
  694. # something like this:
  695. #
  696. # def func(self=self):
  697. # self.load()
  698. # import __builtin__
  699. # __builtin__.func = func
  700. # PythonUtil.startProfile(cmd='func()', filename='profileData')
  701. # del __builtin__.func
  702. #
  703. def startProfile(filename=PyUtilProfileDefaultFilename,
  704. lines=PyUtilProfileDefaultLines,
  705. sorts=PyUtilProfileDefaultSorts,
  706. silent=0,
  707. callInfo=1,
  708. cmd='run()'):
  709. import profile
  710. profile.run(cmd, filename)
  711. if not silent:
  712. printProfile(filename, lines, sorts, callInfo)
  713. # call this to see the results again
  714. def printProfile(filename=PyUtilProfileDefaultFilename,
  715. lines=PyUtilProfileDefaultLines,
  716. sorts=PyUtilProfileDefaultSorts,
  717. callInfo=1):
  718. import pstats
  719. s = pstats.Stats(filename)
  720. s.strip_dirs()
  721. for sort in sorts:
  722. s.sort_stats(sort)
  723. s.print_stats(lines)
  724. if callInfo:
  725. s.print_callees(lines)
  726. s.print_callers(lines)
  727. def getSetterName(valueName, prefix='set'):
  728. # getSetterName('color') -> 'setColor'
  729. # getSetterName('color', 'get') -> 'getColor'
  730. return '%s%s%s' % (prefix, string.upper(valueName[0]), valueName[1:])
  731. def getSetter(targetObj, valueName, prefix='set'):
  732. # getSetter(smiley, 'pos') -> smiley.setPos
  733. return getattr(targetObj, getSetterName(valueName, prefix))
  734. class Functor:
  735. def __init__(self, function, *args, **kargs):
  736. assert callable(function), "function should be a callable obj"
  737. self._function = function
  738. self._args = args
  739. self._kargs = kargs
  740. self.__name__ = 'Functor: %s' % self._function.__name__
  741. self.__doc__ = self._function.__doc__
  742. def destroy(self):
  743. del self._function
  744. del self._args
  745. del self._kargs
  746. del self.__name__
  747. del self.__doc__
  748. def __call__(self, *args, **kargs):
  749. """call function"""
  750. _args = list(self._args)
  751. _args.extend(args)
  752. _kargs = self._kargs.copy()
  753. _kargs.update(kargs)
  754. return apply(self._function, _args, _kargs)
  755. def __repr__(self):
  756. s = 'Functor(%s' % self._function.__name__
  757. for arg in self._args:
  758. try:
  759. argStr = repr(arg)
  760. except:
  761. argStr = 'bad repr: %s' % arg.__class__
  762. s += ', %s' % argStr
  763. for karg, value in self._kargs.items():
  764. s += ', %s=%s' % (karg, repr(value))
  765. s += ')'
  766. return s
  767. class Stack:
  768. def __init__(self):
  769. self.__list = []
  770. def push(self, item):
  771. self.__list.append(item)
  772. def top(self):
  773. # return the item on the top of the stack without popping it off
  774. return self.__list[-1]
  775. def pop(self):
  776. return self.__list.pop()
  777. def clear(self):
  778. self.__list = []
  779. def isEmpty(self):
  780. return len(self.__list) == 0
  781. def __len__(self):
  782. return len(self.__list)
  783. class Queue:
  784. # FIFO queue
  785. # interface is intentionally identical to Stack (LIFO)
  786. def __init__(self):
  787. self.__list = []
  788. def push(self, item):
  789. self.__list.append(item)
  790. def top(self):
  791. # return the next item at the front of the queue without popping it off
  792. return self.__list[0]
  793. def pop(self):
  794. return self.__list.pop(0)
  795. def clear(self):
  796. self.__list = []
  797. def isEmpty(self):
  798. return len(self.__list) == 0
  799. def __len__(self):
  800. return len(self.__list)
  801. if __debug__:
  802. q = Queue()
  803. assert q.isEmpty()
  804. q.clear()
  805. assert q.isEmpty()
  806. q.push(10)
  807. assert not q.isEmpty()
  808. q.push(20)
  809. assert not q.isEmpty()
  810. assert len(q) == 2
  811. assert q.top() == 10
  812. assert q.top() == 10
  813. assert q.pop() == 10
  814. assert len(q) == 1
  815. assert not q.isEmpty()
  816. assert q.pop() == 20
  817. assert len(q) == 0
  818. assert q.isEmpty()
  819. """
  820. ParamObj/ParamSet
  821. =================
  822. These two classes support you in the definition of a formal set of
  823. parameters for an object type. The parameters may be safely queried/set on
  824. an object instance at any time, and the object will react to newly-set
  825. values immediately.
  826. ParamSet & ParamObj also provide a mechanism for atomically setting
  827. multiple parameter values before allowing the object to react to any of the
  828. new values--useful when two or more parameters are interdependent and there
  829. is risk of setting an illegal combination in the process of applying a new
  830. set of values.
  831. To make use of these classes, derive your object from ParamObj. Then define
  832. a 'ParamSet' subclass that derives from the parent class' 'ParamSet' class,
  833. and define the object's parameters within its ParamSet class. (see examples
  834. below)
  835. The ParamObj base class provides 'get' and 'set' functions for each
  836. parameter if they are not defined. These default implementations
  837. respectively set the parameter value directly on the object, and expect the
  838. value to be available in that location for retrieval.
  839. Classes that derive from ParamObj can optionally declare a 'get' and 'set'
  840. function for each parameter. The setter should simply store the value in a
  841. location where the getter can find it; it should not do any further
  842. processing based on the new parameter value. Further processing should be
  843. implemented in an 'apply' function. The applier function is optional, and
  844. there is no default implementation.
  845. NOTE: the previous value of a parameter is available inside an apply
  846. function as 'self.getPriorValue()'
  847. The ParamSet class declaration lists the parameters and defines a default
  848. value for each. ParamSet instances represent a complete set of parameter
  849. values. A ParamSet instance created with no constructor arguments will
  850. contain the default values for each parameter. The defaults may be
  851. overriden by passing keyword arguments to the ParamSet's constructor. If a
  852. ParamObj instance is passed to the constructor, the ParamSet will extract
  853. the object's current parameter values.
  854. ParamSet.applyTo(obj) sets all of its parameter values on 'obj'.
  855. SETTERS AND APPLIERS
  856. ====================
  857. Under normal conditions, a call to a setter function, i.e.
  858. cam.setFov(90)
  859. will actually result in the following calls being made:
  860. cam.setFov(90)
  861. cam.applyFov()
  862. Calls to several setter functions, i.e.
  863. cam.setFov(90)
  864. cam.setViewType('cutscene')
  865. will result in this call sequence:
  866. cam.setFov(90)
  867. cam.applyFov()
  868. cam.setViewType('cutscene')
  869. cam.applyViewType()
  870. Suppose that you desire the view type to already be set to 'cutscene' at
  871. the time when applyFov() is called. You could reverse the order of the set
  872. calls, but suppose that you also want the fov to be set properly at the
  873. time when applyViewType() is called.
  874. In this case, you can 'lock' the params, i.e.
  875. cam.lockParams()
  876. cam.setFov(90)
  877. cam.setViewType('cutscene')
  878. cam.unlockParams()
  879. This will result in the following call sequence:
  880. cam.setFov(90)
  881. cam.setViewType('cutscene')
  882. cam.applyFov()
  883. cam.applyViewType()
  884. NOTE: Currently the order of the apply calls following an unlock is not
  885. guaranteed.
  886. EXAMPLE CLASSES
  887. ===============
  888. Here is an example of a class that uses ParamSet/ParamObj to manage its
  889. parameters:
  890. class Camera(ParamObj):
  891. class ParamSet(ParamObj.ParamSet):
  892. Params = {
  893. 'viewType': 'normal',
  894. 'fov': 60,
  895. }
  896. ...
  897. def getViewType(self):
  898. return self.viewType
  899. def setViewType(self, viewType):
  900. self.viewType = viewType
  901. def applyViewType(self):
  902. if self.viewType == 'normal':
  903. ...
  904. def getFov(self):
  905. return self.fov
  906. def setFov(self, fov):
  907. self.fov = fov
  908. def applyFov(self):
  909. base.camera.setFov(self.fov)
  910. ...
  911. EXAMPLE USAGE
  912. =============
  913. cam = Camera()
  914. ...
  915. # set up for the cutscene
  916. savedSettings = cam.ParamSet(cam)
  917. cam.setViewType('closeup')
  918. cam.setFov(90)
  919. ...
  920. # cutscene is over, set the camera back
  921. savedSettings.applyTo(cam)
  922. del savedSettings
  923. """
  924. class ParamObj:
  925. # abstract base for classes that want to support a formal parameter
  926. # set whose values may be queried, changed, 'bulk' changed, and
  927. # extracted/stored/applied all at once (see documentation above)
  928. # ParamSet subclass: container of parameter values. Derived class must
  929. # derive a new ParamSet class if they wish to define new params. See
  930. # documentation above.
  931. class ParamSet:
  932. Params = {
  933. # base class does not define any parameters, but they would
  934. # appear here as 'name': value,
  935. }
  936. def __init__(self, *args, **kwArgs):
  937. self.__class__._compileDefaultParams()
  938. if len(args) == 1 and len(kwArgs) == 0:
  939. # extract our params from an existing ParamObj instance
  940. obj = args[0]
  941. self.paramVals = {}
  942. for param in self.getParams():
  943. self.paramVals[param] = getSetter(obj, param, 'get')()
  944. else:
  945. assert len(args) == 0
  946. if __debug__:
  947. for arg in kwArgs.keys():
  948. assert arg in self.getParams()
  949. self.paramVals = dict(kwArgs)
  950. def getValue(self, param):
  951. if param in self.paramVals:
  952. return self.paramVals[param]
  953. return self._Params[param]
  954. def applyTo(self, obj):
  955. # Apply our entire set of params to a ParamObj
  956. obj.lockParams()
  957. for param in self.getParams():
  958. getSetter(obj, param)(self.getValue(param))
  959. obj.unlockParams()
  960. # CLASS METHODS
  961. def getParams(cls):
  962. # returns safely-mutable list of param names
  963. cls._compileDefaultParams()
  964. return cls._Params.keys()
  965. getParams = classmethod(getParams)
  966. def getDefaultValue(cls, param):
  967. cls._compileDefaultParams()
  968. return cls._Params[param]
  969. getDefaultValue = classmethod(getDefaultValue)
  970. def _compileDefaultParams(cls):
  971. if cls.__dict__.has_key('_Params'):
  972. # we've already compiled the defaults for this class
  973. return
  974. bases = list(cls.__bases__)
  975. # bring less-derived classes to the front
  976. mostDerivedLast(bases)
  977. cls._Params = {}
  978. for c in (bases + [cls]):
  979. # make sure this base has its dict of param defaults
  980. c._compileDefaultParams()
  981. if c.__dict__.has_key('Params'):
  982. # apply this class' default param values to our dict
  983. cls._Params.update(c.Params)
  984. _compileDefaultParams = classmethod(_compileDefaultParams)
  985. # END PARAMSET SUBCLASS
  986. def __init__(self, *args, **kwArgs):
  987. assert issubclass(self.ParamSet, ParamObj.ParamSet)
  988. # If you pass in a ParamSet obj, its values will be applied to this
  989. # object in the constructor.
  990. params = None
  991. if len(args) == 1 and len(kwArgs) == 0:
  992. # if there's one argument, assume that it's a ParamSet
  993. params = args[0]
  994. elif len(kwArgs) > 0:
  995. assert len(args) == 0
  996. # if we've got keyword arguments, make a ParamSet out of them
  997. params = self.ParamSet(**kwArgs)
  998. self._paramLockRefCount = 0
  999. # this holds dictionaries of parameter values prior to the set that we
  1000. # are performing
  1001. self._priorValuesStack = Stack()
  1002. # this holds the name of the parameter that we are currently modifying
  1003. # at the top of the stack
  1004. self._curParamStack = Stack()
  1005. def setterStub(param, setterFunc, self,
  1006. value):
  1007. # should we apply the value now or should we wait?
  1008. # if this obj's params are locked, we track which values have
  1009. # been set, and on unlock, we'll call the applyers for those
  1010. # values
  1011. if self._paramLockRefCount > 0:
  1012. setterFunc(value)
  1013. priorValues = self._priorValuesStack.top()
  1014. if param not in priorValues:
  1015. try:
  1016. priorValue = getSetter(self, param, 'get')()
  1017. except:
  1018. priorValue = None
  1019. priorValues[param] = priorValue
  1020. self._paramsSet[param] = None
  1021. else:
  1022. # prepare for call to getPriorValue
  1023. self._priorValuesStack.push({
  1024. param: getSetter(self, param, 'get')()
  1025. })
  1026. setterFunc(value)
  1027. # call the applier, if there is one
  1028. applier = getattr(self, getSetterName(param, 'apply'), None)
  1029. if applier is not None:
  1030. self._curParamStack.push(param)
  1031. applier()
  1032. self._curParamStack.pop()
  1033. self._priorValuesStack.pop()
  1034. if hasattr(self, 'handleParamChange'):
  1035. self.handleParamChange((param,))
  1036. # insert stub funcs for param setters
  1037. for param in self.ParamSet.getParams():
  1038. setterName = getSetterName(param)
  1039. getterName = getSetterName(param, 'get')
  1040. # is there a setter defined?
  1041. if not hasattr(self, setterName):
  1042. # no; provide the default
  1043. def defaultSetter(self, value, param=param):
  1044. setattr(self, param, value)
  1045. self.__class__.__dict__[setterName] = defaultSetter
  1046. # is there a getter defined?
  1047. if not hasattr(self, getterName):
  1048. # no; provide the default. If there is no value set, return
  1049. # the default
  1050. def defaultGetter(self, param=param,
  1051. default=self.ParamSet.getDefaultValue(param)):
  1052. return getattr(self, param, default)
  1053. self.__class__.__dict__[getterName] = defaultGetter
  1054. # grab a reference to the setter
  1055. setterFunc = getattr(self, setterName)
  1056. # if the setter is a direct member of this instance, move the setter
  1057. # aside
  1058. if setterName in self.__dict__:
  1059. self.__dict__[setterName + '_MOVED'] = self.__dict__[setterName]
  1060. setterFunc = self.__dict__[setterName]
  1061. # install a setter stub that will a) call the real setter and
  1062. # then the applier, or b) call the setter and queue the
  1063. # applier, depending on whether our params are locked
  1064. self.__dict__[setterName] = Functor(setterStub, param,
  1065. setterFunc, self)
  1066. if params is not None:
  1067. params.applyTo(self)
  1068. def destroy(self):
  1069. for param in self.ParamSet.getParams():
  1070. setterName = getSetterName(param)
  1071. self.__dict__[setterName].destroy()
  1072. del self.__dict__[setterName]
  1073. def setDefaultParams(self):
  1074. # set all the default parameters on ourself
  1075. self.ParamSet().applyTo(self)
  1076. def lockParams(self):
  1077. self._paramLockRefCount += 1
  1078. if self._paramLockRefCount == 1:
  1079. self._handleLockParams()
  1080. def unlockParams(self):
  1081. if self._paramLockRefCount > 0:
  1082. self._paramLockRefCount -= 1
  1083. if self._paramLockRefCount == 0:
  1084. self._handleUnlockParams()
  1085. def _handleLockParams(self):
  1086. # this will store the names of the parameters that are modified
  1087. self._paramsSet = {}
  1088. # this will store the values of modified params (from prior to
  1089. # the lock).
  1090. self._priorValuesStack.push({})
  1091. def _handleUnlockParams(self):
  1092. for param in self._paramsSet:
  1093. # call the applier, if there is one
  1094. applier = getattr(self, getSetterName(param, 'apply'), None)
  1095. if applier is not None:
  1096. self._curParamStack.push(param)
  1097. applier()
  1098. self._curParamStack.pop()
  1099. self._priorValuesStack.pop()
  1100. if hasattr(self, 'handleParamChange'):
  1101. self.handleParamChange(tuple(self._paramsSet.keys()))
  1102. del self._paramsSet
  1103. def paramsLocked(self):
  1104. return self._paramLockRefCount > 0
  1105. def getPriorValue(self):
  1106. # call this within an apply function to find out what the prior value
  1107. # of the param was
  1108. return self._priorValuesStack.top()[self._curParamStack.top()]
  1109. def __repr__(self):
  1110. argStr = ''
  1111. for param in self.ParamSet.getParams():
  1112. argStr += '%s=%s,' % (param,
  1113. repr(getSetter(self, param, 'get')()))
  1114. return '%s(%s)' % (self.__class__.__name__, argStr)
  1115. """
  1116. POD (Plain Ol' Data)
  1117. Like ParamObj/ParamSet, but without lock/unlock/getPriorValue and without
  1118. appliers. Similar to a C++ struct, but with auto-generated setters and
  1119. getters.
  1120. Use POD when you want the generated getters and setters of ParamObj, but
  1121. efficiency is a concern and you don't need the bells and whistles provided
  1122. by ParamObj.
  1123. POD.__init__ *MUST* be called. You should NOT define your own data getters
  1124. and setters. Data values may be read, set, and modified directly. You will
  1125. see no errors if you define your own getters/setters, but there is no
  1126. guarantee that they will be called--and they will certainly be bypassed by
  1127. POD internally.
  1128. EXAMPLE CLASSES
  1129. ===============
  1130. Here is an example of a class heirarchy that uses POD to manage its data:
  1131. class Enemy(POD):
  1132. DataSet = {
  1133. 'faction': 'navy',
  1134. }
  1135. class Sailor(Enemy):
  1136. DataSet = {
  1137. 'build': HUSKY,
  1138. 'weapon': Cutlass(scale=.9),
  1139. }
  1140. EXAMPLE USAGE
  1141. =============
  1142. s = Sailor(faction='undead', build=SKINNY)
  1143. # make two copies of s
  1144. s2 = s.makeCopy()
  1145. s3 = Sailor(s)
  1146. # example sets
  1147. s2.setWeapon(Musket())
  1148. s3.build = TALL
  1149. # example gets
  1150. faction2 = s2.getFaction()
  1151. faction3 = s3.faction
  1152. """
  1153. class POD:
  1154. DataSet = {
  1155. # base class does not define any data items, but they would
  1156. # appear here as 'name': value,
  1157. }
  1158. def __init__(self, **kwArgs):
  1159. self.__class__._compileDefaultDataSet()
  1160. if __debug__:
  1161. for arg in kwArgs.keys():
  1162. assert arg in self.getDataNames(), (
  1163. "unknown argument for %s: '%s'" % (
  1164. self.__class__, arg))
  1165. for name in self.getDataNames():
  1166. if name in kwArgs:
  1167. getSetter(self, name)(kwArgs[name])
  1168. else:
  1169. getSetter(self, name)(self.getDefaultValue(name))
  1170. def setDefaultValues(self):
  1171. # set all the default data values on ourself
  1172. for name in self.getDataNames():
  1173. getSetter(self, name)(self.getDefaultValue(name))
  1174. # this functionality used to be in the constructor, triggered by a single
  1175. # positional argument; that was conflicting with POD subclasses that wanted
  1176. # to define different behavior for themselves when given a positional
  1177. # constructor argument
  1178. def copyFrom(self, other, strict=False):
  1179. # if 'strict' is true, other must have a value for all of our data items
  1180. # otherwise we'll use the defaults
  1181. for name in self.getDataNames():
  1182. if hasattr(other, getSetterName(name, 'get')):
  1183. setattr(self, name, getSetter(other, name, 'get')())
  1184. else:
  1185. if strict:
  1186. raise "object '%s' doesn't have value '%s'" % (other, name)
  1187. else:
  1188. setattr(self, name, self.getDefaultValue(name))
  1189. # support 'p = POD.POD().copyFrom(other)' syntax
  1190. return self
  1191. def makeCopy(self):
  1192. # returns a duplicate of this object
  1193. return self.__class__().copyFrom(self)
  1194. def applyTo(self, obj):
  1195. # Apply our entire set of data to another POD
  1196. for name in self.getDataNames():
  1197. getSetter(obj, name)(getSetter(self, name, 'get')())
  1198. def getValue(self, name):
  1199. return getSetter(self, name, 'get')()
  1200. # CLASS METHODS
  1201. def getDataNames(cls):
  1202. # returns safely-mutable list of datum names
  1203. cls._compileDefaultDataSet()
  1204. return cls._DataSet.keys()
  1205. getDataNames = classmethod(getDataNames)
  1206. def getDefaultValue(cls, name):
  1207. cls._compileDefaultDataSet()
  1208. return cls._DataSet[name]
  1209. getDefaultValue = classmethod(getDefaultValue)
  1210. def _compileDefaultDataSet(cls):
  1211. if cls.__dict__.has_key('_DataSet'):
  1212. # we've already compiled the defaults for this class
  1213. return
  1214. # create setters & getters for this class
  1215. if cls.__dict__.has_key('DataSet'):
  1216. for name in cls.DataSet:
  1217. setterName = getSetterName(name)
  1218. if not hasattr(cls, setterName):
  1219. def defaultSetter(self, value, name=name):
  1220. setattr(self, name, value)
  1221. cls.__dict__[setterName] = defaultSetter
  1222. getterName = getSetterName(name, 'get')
  1223. if not hasattr(cls, getterName):
  1224. def defaultGetter(self, name=name,
  1225. default=cls.DataSet[name]):
  1226. return getattr(self, name, default)
  1227. cls.__dict__[getterName] = defaultGetter
  1228. # this dict will hold all of the aggregated default data values for
  1229. # this particular class, including values from its base classes
  1230. cls._DataSet = {}
  1231. bases = list(cls.__bases__)
  1232. # bring less-derived classes to the front
  1233. mostDerivedLast(bases)
  1234. for c in (bases + [cls]):
  1235. # skip multiple-inheritance base classes that do not derive from POD
  1236. if issubclass(c, POD):
  1237. # make sure this base has its dict of data defaults
  1238. c._compileDefaultDataSet()
  1239. if c.__dict__.has_key('DataSet'):
  1240. # apply this class' default data values to our dict
  1241. cls._DataSet.update(c.DataSet)
  1242. _compileDefaultDataSet = classmethod(_compileDefaultDataSet)
  1243. # END CLASS METHODS
  1244. def __repr__(self):
  1245. argStr = ''
  1246. for name in self.getDataNames():
  1247. argStr += '%s=%s,' % (name, repr(getSetter(self, name, 'get')()))
  1248. return '%s(%s)' % (self.__class__.__name__, argStr)
  1249. """ TODO
  1250. if __dev__:
  1251. @staticmethod
  1252. def unitTest():
  1253. tColor = 'red'
  1254. tColor2 = 'blue'
  1255. class test(POD):
  1256. DataSet = {
  1257. 'color': tColor,
  1258. }
  1259. t = test()
  1260. assert t.getColor() == tColor
  1261. t.setColor(tColor2)
  1262. assert t.getColor() == tColor2
  1263. t2 = test().makeCopy()
  1264. assert t2.getColor() == t.getColor() == tColor2
  1265. """
  1266. def bound(value, bound1, bound2):
  1267. """
  1268. returns value if value is between bound1 and bound2
  1269. otherwise returns bound that is closer to value
  1270. """
  1271. if bound1 > bound2:
  1272. return min(max(value, bound2), bound1)
  1273. else:
  1274. return min(max(value, bound1), bound2)
  1275. def lerp(v0, v1, t):
  1276. """
  1277. returns a value lerped between v0 and v1, according to t
  1278. t == 0 maps to v0, t == 1 maps to v1
  1279. """
  1280. return v0 + (t * (v1 - v0))
  1281. def average(*args):
  1282. """ returns simple average of list of values """
  1283. val = 0.
  1284. for arg in args:
  1285. val += arg
  1286. return val / len(args)
  1287. def addListsByValue(a, b):
  1288. """
  1289. returns a new array containing the sums of the two array arguments
  1290. (c[0] = a[0 + b[0], etc.)
  1291. """
  1292. c = []
  1293. for x, y in zip(a, b):
  1294. c.append(x + y)
  1295. return c
  1296. def boolEqual(a, b):
  1297. """
  1298. returns true if a and b are both true or both false.
  1299. returns false otherwise
  1300. (a.k.a. xnor -- eXclusive Not OR).
  1301. """
  1302. return (a and b) or not (a or b)
  1303. def lineupPos(i, num, spacing):
  1304. """
  1305. use to line up a series of 'num' objects, in one dimension,
  1306. centered around zero
  1307. 'i' is the index of the object in the lineup
  1308. 'spacing' is the amount of space between objects in the lineup
  1309. """
  1310. assert num >= 1
  1311. assert i >= 0 and i < num
  1312. pos = float(i) * spacing
  1313. return pos - ((float(spacing) * (num-1))/2.)
  1314. def formatElapsedSeconds(seconds):
  1315. """
  1316. Returns a string of the form "mm:ss" or "hh:mm:ss" or "n days",
  1317. representing the indicated elapsed time in seconds.
  1318. """
  1319. sign = ''
  1320. if seconds < 0:
  1321. seconds = -seconds
  1322. sign = '-'
  1323. # We use math.floor() instead of casting to an int, so we avoid
  1324. # problems with numbers that are too large to represent as
  1325. # type int.
  1326. seconds = math.floor(seconds)
  1327. hours = math.floor(seconds / (60 * 60))
  1328. if hours > 36:
  1329. days = math.floor((hours + 12) / 24)
  1330. return "%s%d days" % (sign, days)
  1331. seconds -= hours * (60 * 60)
  1332. minutes = (int)(seconds / 60)
  1333. seconds -= minutes * 60
  1334. if hours != 0:
  1335. return "%s%d:%02d:%02d" % (sign, hours, minutes, seconds)
  1336. else:
  1337. return "%s%d:%02d" % (sign, minutes, seconds)
  1338. def solveQuadratic(a, b, c):
  1339. # quadratic equation: ax^2 + bx + c = 0
  1340. # quadratic formula: x = [-b +/- sqrt(b^2 - 4ac)] / 2a
  1341. # returns None, root, or [root1, root2]
  1342. # a cannot be zero.
  1343. if a == 0.:
  1344. return None
  1345. # calculate the determinant (b^2 - 4ac)
  1346. D = (b * b) - (4. * a * c)
  1347. if D < 0:
  1348. # there are no solutions (sqrt(negative number) is undefined)
  1349. return None
  1350. elif D == 0:
  1351. # only one root
  1352. return (-b) / (2. * a)
  1353. else:
  1354. # OK, there are two roots
  1355. sqrtD = math.sqrt(D)
  1356. twoA = 2. * a
  1357. root1 = ((-b) - sqrtD) / twoA
  1358. root2 = ((-b) + sqrtD) / twoA
  1359. return [root1, root2]
  1360. def stackEntryInfo(depth=0, baseFileName=1):
  1361. """
  1362. returns the sourcefilename, line number, and function name of
  1363. an entry in the stack.
  1364. 'depth' is how far back to go in the stack; 0 is the caller of this
  1365. function, 1 is the function that called the caller of this function, etc.
  1366. by default, strips off the path of the filename; override with baseFileName
  1367. returns (fileName, lineNum, funcName) --> (string, int, string)
  1368. returns (None, None, None) on error
  1369. """
  1370. try:
  1371. stack = None
  1372. frame = None
  1373. try:
  1374. stack = inspect.stack()
  1375. # add one to skip the frame associated with this function
  1376. frame = stack[depth+1]
  1377. filename = frame[1]
  1378. if baseFileName:
  1379. filename = os.path.basename(filename)
  1380. lineNum = frame[2]
  1381. funcName = frame[3]
  1382. result = (filename, lineNum, funcName)
  1383. finally:
  1384. del stack
  1385. del frame
  1386. except:
  1387. result = (None, None, None)
  1388. return result
  1389. def lineInfo(baseFileName=1):
  1390. """
  1391. returns the sourcefilename, line number, and function name of the
  1392. code that called this function
  1393. (answers the question: 'hey lineInfo, where am I in the codebase?')
  1394. see stackEntryInfo, above, for info on 'baseFileName' and return types
  1395. """
  1396. return stackEntryInfo(1)
  1397. def callerInfo(baseFileName=1):
  1398. """
  1399. returns the sourcefilename, line number, and function name of the
  1400. caller of the function that called this function
  1401. (answers the question: 'hey callerInfo, who called me?')
  1402. see stackEntryInfo, above, for info on 'baseFileName' and return types
  1403. """
  1404. return stackEntryInfo(2)
  1405. def lineTag(baseFileName=1, verbose=0, separator=':'):
  1406. """
  1407. returns a string containing the sourcefilename and line number
  1408. of the code that called this function
  1409. (equivalent to lineInfo, above, with different return type)
  1410. see stackEntryInfo, above, for info on 'baseFileName'
  1411. if 'verbose' is false, returns a compact string of the form
  1412. 'fileName:lineNum:funcName'
  1413. if 'verbose' is true, returns a longer string that matches the
  1414. format of Python stack trace dumps
  1415. returns empty string on error
  1416. """
  1417. fileName, lineNum, funcName = callerInfo()
  1418. if fileName is None:
  1419. return ''
  1420. if verbose:
  1421. return 'File "%s", line %s, in %s' % (fileName, lineNum, funcName)
  1422. else:
  1423. return '%s%s%s%s%s' % (fileName, separator, lineNum, separator,
  1424. funcName)
  1425. def findPythonModule(module):
  1426. # Look along the python load path for the indicated filename.
  1427. # Returns the located pathname, or None if the filename is not
  1428. # found.
  1429. filename = module + '.py'
  1430. for dir in sys.path:
  1431. pathname = os.path.join(dir, filename)
  1432. if os.path.exists(pathname):
  1433. return pathname
  1434. return None
  1435. def describeException(backTrace = 4):
  1436. # When called in an exception handler, returns a string describing
  1437. # the current exception.
  1438. def byteOffsetToLineno(code, byte):
  1439. # Returns the source line number corresponding to the given byte
  1440. # offset into the indicated Python code module.
  1441. import array
  1442. lnotab = array.array('B', code.co_lnotab)
  1443. line = code.co_firstlineno
  1444. for i in range(0, len(lnotab), 2):
  1445. byte -= lnotab[i]
  1446. if byte <= 0:
  1447. return line
  1448. line += lnotab[i+1]
  1449. return line
  1450. infoArr = sys.exc_info()
  1451. exception = infoArr[0]
  1452. exceptionName = getattr(exception, '__name__', None)
  1453. extraInfo = infoArr[1]
  1454. trace = infoArr[2]
  1455. stack = []
  1456. while trace.tb_next:
  1457. # We need to call byteOffsetToLineno to determine the true
  1458. # line number at which the exception occurred, even though we
  1459. # have both trace.tb_lineno and frame.f_lineno, which return
  1460. # the correct line number only in non-optimized mode.
  1461. frame = trace.tb_frame
  1462. module = frame.f_globals.get('__name__', None)
  1463. lineno = byteOffsetToLineno(frame.f_code, frame.f_lasti)
  1464. stack.append("%s:%s, " % (module, lineno))
  1465. trace = trace.tb_next
  1466. frame = trace.tb_frame
  1467. module = frame.f_globals.get('__name__', None)
  1468. lineno = byteOffsetToLineno(frame.f_code, frame.f_lasti)
  1469. stack.append("%s:%s, " % (module, lineno))
  1470. description = ""
  1471. for i in range(len(stack) - 1, max(len(stack) - backTrace, 0) - 1, -1):
  1472. description += stack[i]
  1473. description += "%s: %s" % (exceptionName, extraInfo)
  1474. return description
  1475. def mostDerivedLast(classList):
  1476. """pass in list of classes. sorts list in-place, with derived classes
  1477. appearing after their bases"""
  1478. def compare(a, b):
  1479. if issubclass(a, b):
  1480. result=1
  1481. elif issubclass(b, a):
  1482. result=-1
  1483. else:
  1484. result=0
  1485. #print a, b, result
  1486. return result
  1487. classList.sort(compare)
  1488. def clampScalar(value, a, b):
  1489. # calling this ought to be faster than calling both min and max
  1490. if a < b:
  1491. if value < a:
  1492. return a
  1493. elif value > b:
  1494. return b
  1495. else:
  1496. return value
  1497. else:
  1498. if value < b:
  1499. return b
  1500. elif value > a:
  1501. return a
  1502. else:
  1503. return value
  1504. def weightedChoice(choiceList, rng=random.random, sum=None):
  1505. """given a list of (weight, item) pairs, chooses an item based on the
  1506. weights. rng must return 0..1. if you happen to have the sum of the
  1507. weights, pass it in 'sum'."""
  1508. # TODO: add support for dicts
  1509. if sum is None:
  1510. sum = 0.
  1511. for weight, item in choiceList:
  1512. sum += weight
  1513. rand = rng()
  1514. accum = rand * sum
  1515. for weight, item in choiceList:
  1516. accum -= weight
  1517. if accum <= 0.:
  1518. return item
  1519. # rand is ~1., and floating-point error prevented accum from hitting 0.
  1520. # Or you passed in a 'sum' that was was too large.
  1521. # Return the last item.
  1522. return item
  1523. def randFloat(a, b=0., rng=random.random):
  1524. """returns a random float in [a, b]
  1525. call with single argument to generate random float between arg and zero
  1526. """
  1527. return lerp(a, b, rng())
  1528. def normalDistrib(a, b, gauss=random.gauss):
  1529. """
  1530. NOTE: assumes a < b
  1531. Returns random number between a and b, using gaussian distribution, with
  1532. mean=avg(a, b), and a standard deviation that fits ~99.7% of the curve
  1533. between a and b. Outlying results are clipped to a and b.
  1534. ------------------------------------------------------------------------
  1535. http://www-stat.stanford.edu/~naras/jsm/NormalDensity/NormalDensity.html
  1536. The 68-95-99.7% Rule
  1537. ====================
  1538. All normal density curves satisfy the following property which is often
  1539. referred to as the Empirical Rule:
  1540. 68% of the observations fall within 1 standard deviation of the mean.
  1541. 95% of the observations fall within 2 standard deviations of the mean.
  1542. 99.7% of the observations fall within 3 standard deviations of the mean.
  1543. Thus, for a normal distribution, almost all values lie within 3 standard
  1544. deviations of the mean.
  1545. ------------------------------------------------------------------------
  1546. In calculating our standard deviation, we divide (b-a) by 6, since the
  1547. 99.7% figure includes 3 standard deviations _on_either_side_ of the mean.
  1548. """
  1549. return max(a, min(b, gauss((a+b)*.5, (b-a)/6.)))
  1550. def weightedRand(valDict, rng=random.random):
  1551. """
  1552. pass in a dictionary with a selection -> weight mapping. Eg.
  1553. {"Choice 1": 10,
  1554. "Choice 2": 30,
  1555. "bear": 100}
  1556. -Weights need not add up to any particular value.
  1557. -The actual selection will be returned.
  1558. """
  1559. selections = valDict.keys()
  1560. weights = valDict.values()
  1561. totalWeight = 0
  1562. for weight in weights:
  1563. totalWeight += weight
  1564. # get a random value between 0 and the total of the weights
  1565. randomWeight = rng() * totalWeight
  1566. # find the index that corresponds with this weight
  1567. for i in range(len(weights)):
  1568. totalWeight -= weights[i]
  1569. if totalWeight <= randomWeight:
  1570. return selections[i]
  1571. assert True, "Should never get here"
  1572. return selections[-1]
  1573. def randUint31(rng=random.random):
  1574. """returns a random integer in [0..2^31).
  1575. rng must return float in [0..1]"""
  1576. return int(rng() * 0x7FFFFFFF)
  1577. def randInt32(rng=random.random):
  1578. """returns a random integer in [-2147483648..2147483647].
  1579. rng must return float in [0..1]
  1580. """
  1581. i = int(rng() * 0x7FFFFFFF)
  1582. if rng() < .5:
  1583. i *= -1
  1584. return i
  1585. def randUint32(rng=random.random):
  1586. """returns a random integer in [0..2^32).
  1587. rng must return float in [0..1]"""
  1588. return long(rng() * 0xFFFFFFFFL)
  1589. class SerialNum:
  1590. """generates serial numbers"""
  1591. def __init__(self, start=None):
  1592. if start is None:
  1593. start = 0
  1594. self.__counter = start-1
  1595. def next(self):
  1596. self.__counter += 1
  1597. return self.__counter
  1598. _uname = SerialNum()
  1599. def uniqueName(name):
  1600. global _uname
  1601. return '%s-%s' % (name, _uname.next())
  1602. def beginExports(modname):
  1603. """The beginExports/endExports construct is used to bracket
  1604. a set of top-level python declarations. The effect is to
  1605. add those declarations to the module's list of exported
  1606. symbols. In other words, the purpose of this bracketing
  1607. construct is to make it easier to properly initialize the
  1608. export-list for a module. The parameter should be the
  1609. current module's name."""
  1610. if (sys.modules.has_key(modname) == 0):
  1611. raise "beginExports("+modname+"): no such module"
  1612. mod = sys.modules[modname]
  1613. if (mod.__dict__.has_key("__begin_exports__")):
  1614. raise "beginExports("+modname+"): incorrect nesting"
  1615. mod.__begin_exports__ = set(mod.__dict__.keys())
  1616. if (mod.__dict__.has_key("__all__")==0):
  1617. mod.__all__ = []
  1618. def endExports(modname):
  1619. """The beginExports/endExports construct is used to bracket
  1620. a set of top-level python declarations. The effect is to
  1621. add those declarations to the module's list of exported
  1622. symbols. In other words, the purpose of this bracketing
  1623. construct is to make it easier to properly initialize the
  1624. export-list for a module. The parameter should be the
  1625. current module's name."""
  1626. if (sys.modules.has_key(modname) == 0):
  1627. raise "beginExports("+modname+"): no such module"
  1628. mod = sys.modules[modname]
  1629. if (mod.__dict__.has_key("__begin_exports__")==0):
  1630. raise "beginExports("+modname+"): incorrect nesting"
  1631. begin_exports = mod.__begin_exports__
  1632. del mod.__dict__["__begin_exports__"]
  1633. end_exports = set(mod.__dict__.keys())
  1634. added = list(end_exports.difference(begin_exports))
  1635. for x in added:
  1636. if (x[0]!="_"):
  1637. mod.__all__.append(x)
  1638. # __builtins__["beginExports"] = beginExports
  1639. # __builtins__["endExports"] = endExports
  1640. class Enum:
  1641. """Pass in list of strings or string of comma-separated strings.
  1642. Items are accessible as instance.item, and are assigned unique,
  1643. increasing integer values. Pass in integer for 'start' to override
  1644. starting value.
  1645. Example:
  1646. >>> colors = Enum('red, green, blue')
  1647. >>> colors.red
  1648. 0
  1649. >>> colors.green
  1650. 1
  1651. >>> colors.blue
  1652. 2
  1653. >>> colors.getString(colors.red)
  1654. 'red'
  1655. """
  1656. if __debug__:
  1657. # chars that cannot appear within an item string.
  1658. InvalidChars = string.whitespace
  1659. def _checkValidIdentifier(item):
  1660. invalidChars = string.whitespace+string.punctuation
  1661. invalidChars = invalidChars.replace('_','')
  1662. invalidFirstChars = invalidChars+string.digits
  1663. if item[0] in invalidFirstChars:
  1664. raise SyntaxError, ("Enum '%s' contains invalid first char" %
  1665. item)
  1666. if not disjoint(item, invalidChars):
  1667. for char in item:
  1668. if char in invalidChars:
  1669. raise SyntaxError, (
  1670. "Enum\n'%s'\ncontains illegal char '%s'" %
  1671. (item, char))
  1672. return 1
  1673. _checkValidIdentifier = staticmethod(_checkValidIdentifier)
  1674. def __init__(self, items, start=0):
  1675. if type(items) == types.StringType:
  1676. items = items.split(',')
  1677. self._stringTable = {}
  1678. # make sure we don't overwrite an existing element of the class
  1679. assert self._checkExistingMembers(items)
  1680. assert uniqueElements(items)
  1681. i = start
  1682. for item in items:
  1683. # remove leading/trailing whitespace
  1684. item = string.strip(item)
  1685. # is there anything left?
  1686. if len(item) == 0:
  1687. continue
  1688. # make sure there are no invalid characters
  1689. assert Enum._checkValidIdentifier(item)
  1690. self.__dict__[item] = i
  1691. self._stringTable[i] = item
  1692. i += 1
  1693. def getString(self, value):
  1694. return self._stringTable[value]
  1695. def __contains__(self, value):
  1696. return value in self._stringTable
  1697. def __len__(self):
  1698. return len(self._stringTable)
  1699. def copyTo(self, obj):
  1700. # copies all members onto obj
  1701. for name, value in self._stringTable:
  1702. setattr(obj, name, value)
  1703. if __debug__:
  1704. def _checkExistingMembers(self, items):
  1705. for item in items:
  1706. if hasattr(self, item):
  1707. return 0
  1708. return 1
  1709. ############################################################
  1710. # class: Singleton
  1711. # Purpose: This provides a base metaclass for all classes
  1712. # that require one and only one instance.
  1713. #
  1714. # Example: class mySingleton:
  1715. # __metaclass__ = PythonUtil.Singleton
  1716. # def __init__(self, ...):
  1717. # ...
  1718. #
  1719. # Note: This class is based on Python's New-Style Class
  1720. # design. An error will occur if a defined class
  1721. # attemps to inherit from a Classic-Style Class only,
  1722. # ie: class myClassX:
  1723. # def __init__(self, ...):
  1724. # ...
  1725. #
  1726. # class myNewClassX(myClassX):
  1727. # __metaclass__ = PythonUtil.Singleton
  1728. # def __init__(self, ...):
  1729. # myClassX.__init__(self, ...)
  1730. # ...
  1731. #
  1732. # This causes problems because myNewClassX is a
  1733. # New-Style class that inherits from only a
  1734. # Classic-Style base class. There are two ways
  1735. # simple ways to resolve this issue.
  1736. #
  1737. # First, if possible, make myClassX a
  1738. # New-Style class by inheriting from object
  1739. # object. IE: class myClassX(object):
  1740. #
  1741. # If for some reason that is not an option, make
  1742. # myNewClassX inherit from object and myClassX.
  1743. # IE: class myNewClassX(object, myClassX):
  1744. ############################################################
  1745. class Singleton(type):
  1746. def __init__(cls, name, bases, dic):
  1747. super(Singleton, cls).__init__(name, bases, dic)
  1748. cls.instance=None
  1749. def __call__(cls, *args, **kw):
  1750. if cls.instance is None:
  1751. cls.instance=super(Singleton, cls).__call__(*args, **kw)
  1752. return cls.instance
  1753. class SingletonError(ValueError):
  1754. """ Used to indicate an inappropriate value for a Singleton."""
  1755. def printListEnum(l):
  1756. # log each individual item with a number in front of it
  1757. digits = 0
  1758. n = len(l)
  1759. while n > 0:
  1760. digits += 1
  1761. n /= 10
  1762. format = '%0' + '%s' % digits + 'i:%s'
  1763. for i in range(len(l)):
  1764. print format % (i, l[i])
  1765. def gcDebugOn():
  1766. import gc
  1767. return (gc.get_debug() & gc.DEBUG_SAVEALL) == gc.DEBUG_SAVEALL
  1768. class ScratchPad:
  1769. """empty class to stick values onto"""
  1770. pass
  1771. import __builtin__
  1772. __builtin__.Functor = Functor
  1773. __builtin__.Stack = Stack
  1774. __builtin__.Queue = Queue
  1775. __builtin__.SerialNum = SerialNum
  1776. __builtin__.ScratchPad = ScratchPad
  1777. __builtin__.uniqueName = uniqueName
  1778. __builtin__.profiled = profiled