PythonUtil.py 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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. if __debug__:
  11. import traceback
  12. from direct.directutil import Verify
  13. ScalarTypes = (types.FloatType, types.IntType, types.LongType)
  14. # NOTE: ifAbsentPut has been replaced with Python's dictionary's builtin setdefault
  15. # before:
  16. # ifAbsentPut(dict, key, defaultValue)
  17. # after:
  18. # dict.setdefault(key, defaultValue)
  19. # Please use setdefault instead -- Joe
  20. def enumerate(L):
  21. """Returns (0, L[0]), (1, L[1]), etc., allowing this syntax:
  22. for i, item in enumerate(L):
  23. ...
  24. enumerate is a built-in feature in Python 2.3, which implements it
  25. using an iterator. For now, we can use this quick & dirty
  26. implementation that returns a list of tuples that is completely
  27. constructed every time enumerate() is called.
  28. """
  29. return zip(xrange(len(L)), L)
  30. import __builtin__
  31. if hasattr(__builtin__, 'enumerate'):
  32. print 'enumerate is already present in __builtin__'
  33. else:
  34. __builtin__.enumerate = enumerate
  35. def unique(L1, L2):
  36. """Return a list containing all items in 'L1' that are not in 'L2'"""
  37. L2 = dict([(k,None) for k in L2])
  38. return [item for item in L1 if item not in L2]
  39. def indent(stream, numIndents, str):
  40. """
  41. Write str to stream with numIndents in front of it
  42. """
  43. # To match emacs, instead of a tab character we will use 4 spaces
  44. stream.write(' ' * numIndents + str)
  45. def writeFsmTree(instance, indent = 0):
  46. if hasattr(instance, 'parentFSM'):
  47. writeFsmTree(instance.parentFSM, indent-2)
  48. elif hasattr(instance, 'fsm'):
  49. name = ''
  50. if hasattr(instance.fsm, 'state'):
  51. name = instance.fsm.state.name
  52. print "%s: %s"%(instance.fsm.name, name)
  53. if __debug__:
  54. class StackTrace:
  55. def __init__(self, label="", start=0, limit=None):
  56. """
  57. label is a string (or anything that be be a string)
  58. that is printed as part of the trace back.
  59. This is just to make it easier to tell what the
  60. stack trace is referring to.
  61. start is an integer number of stack frames back
  62. from the most recent. (This is automatically
  63. bumped up by one to skip the __init__ call
  64. to the StackTrace).
  65. limit is an integer number of stack frames
  66. to record (or None for unlimited).
  67. """
  68. self.label = label
  69. if limit is not None:
  70. self.trace = traceback.extract_stack(sys._getframe(1+start),
  71. limit=limit)
  72. else:
  73. self.trace = traceback.extract_stack(sys._getframe(1+start))
  74. def __str__(self):
  75. r = "Debug stack trace of %s (back %s frames):\n"%(
  76. self.label, len(self.trace),)
  77. for i in traceback.format_list(self.trace):
  78. r+=i
  79. return r
  80. def traceFunctionCall(frame):
  81. """
  82. return a string that shows the call frame with calling arguments.
  83. e.g.
  84. foo(x=234, y=135)
  85. """
  86. f = frame
  87. co = f.f_code
  88. dict = f.f_locals
  89. n = co.co_argcount
  90. if co.co_flags & 4: n = n+1
  91. if co.co_flags & 8: n = n+1
  92. r=''
  93. if dict.has_key('self'):
  94. r = '%s.'%(dict['self'].__class__.__name__, )
  95. r+="%s("%(f.f_code.co_name, )
  96. comma=0 # formatting, whether we should type a comma.
  97. for i in range(n):
  98. name = co.co_varnames[i]
  99. if name=='self':
  100. continue
  101. if comma:
  102. r+=', '
  103. else:
  104. # ok, we skipped the first one, the rest get commas:
  105. comma=1
  106. r+=name
  107. r+='='
  108. if dict.has_key(name):
  109. v=str(dict[name])
  110. if len(v)>200:
  111. r+="<too big for debug>"
  112. else:
  113. r+=str(dict[name])
  114. else: r+="*** undefined ***"
  115. return r+')'
  116. def traceParentCall():
  117. return traceFunctionCall(sys._getframe(2))
  118. def printThisCall():
  119. print traceFunctionCall(sys._getframe(1))
  120. return 1 # to allow "assert printThisCall()"
  121. def tron():
  122. sys.settrace(trace)
  123. def trace(frame, event, arg):
  124. if event == 'line':
  125. pass
  126. elif event == 'call':
  127. print traceFunctionCall(sys._getframe(1))
  128. elif event == 'return':
  129. print "returning"
  130. elif event == 'exception':
  131. print "exception"
  132. return trace
  133. def troff():
  134. sys.settrace(None)
  135. def apropos(obj, *args):
  136. """
  137. Obsolete, use pdir
  138. """
  139. print 'Use pdir instead'
  140. def getClassLineage(obj):
  141. """
  142. print object inheritance list
  143. """
  144. if type(obj) == types.DictionaryType:
  145. # Just a dictionary, return dictionary
  146. return [obj]
  147. elif type(obj) == types.InstanceType:
  148. # Instance, make a list with the instance and its class interitance
  149. return [obj] + getClassLineage(obj.__class__)
  150. elif type(obj) == types.ClassType:
  151. # Class, see what it derives from
  152. lineage = [obj]
  153. for c in obj.__bases__:
  154. lineage = lineage + getClassLineage(c)
  155. return lineage
  156. else:
  157. # Not what I'm looking for
  158. return []
  159. def pdir(obj, str = None, fOverloaded = 0, width = None,
  160. fTruncate = 1, lineWidth = 75, wantPrivate = 0):
  161. # Remove redundant class entries
  162. uniqueLineage = []
  163. for l in getClassLineage(obj):
  164. if type(l) == types.ClassType:
  165. if l in uniqueLineage:
  166. break
  167. uniqueLineage.append(l)
  168. # Pretty print out directory info
  169. uniqueLineage.reverse()
  170. for obj in uniqueLineage:
  171. _pdir(obj, str, fOverloaded, width, fTruncate, lineWidth, wantPrivate)
  172. print
  173. def _pdir(obj, str = None, fOverloaded = 0, width = None,
  174. fTruncate = 1, lineWidth = 75, wantPrivate = 0):
  175. """
  176. Print out a formatted list of members and methods of an instance or class
  177. """
  178. def printHeader(name):
  179. name = ' ' + name + ' '
  180. length = len(name)
  181. if length < 70:
  182. padBefore = int((70 - length)/2.0)
  183. padAfter = max(0,70 - length - padBefore)
  184. header = '*' * padBefore + name + '*' * padAfter
  185. print header
  186. print
  187. def printInstanceHeader(i, printHeader = printHeader):
  188. printHeader(i.__class__.__name__ + ' INSTANCE INFO')
  189. def printClassHeader(c, printHeader = printHeader):
  190. printHeader(c.__name__ + ' CLASS INFO')
  191. def printDictionaryHeader(d, printHeader = printHeader):
  192. printHeader('DICTIONARY INFO')
  193. # Print Header
  194. if type(obj) == types.InstanceType:
  195. printInstanceHeader(obj)
  196. elif type(obj) == types.ClassType:
  197. printClassHeader(obj)
  198. elif type (obj) == types.DictionaryType:
  199. printDictionaryHeader(obj)
  200. # Get dict
  201. if type(obj) == types.DictionaryType:
  202. dict = obj
  203. else:
  204. dict = obj.__dict__
  205. # Adjust width
  206. if width:
  207. maxWidth = width
  208. else:
  209. maxWidth = 10
  210. keyWidth = 0
  211. aproposKeys = []
  212. privateKeys = []
  213. remainingKeys = []
  214. for key in dict.keys():
  215. if not width:
  216. keyWidth = len(key)
  217. if str:
  218. if re.search(str, key, re.I):
  219. aproposKeys.append(key)
  220. if (not width) and (keyWidth > maxWidth):
  221. maxWidth = keyWidth
  222. else:
  223. if key[:1] == '_':
  224. if wantPrivate:
  225. privateKeys.append(key)
  226. if (not width) and (keyWidth > maxWidth):
  227. maxWidth = keyWidth
  228. else:
  229. remainingKeys.append(key)
  230. if (not width) and (keyWidth > maxWidth):
  231. maxWidth = keyWidth
  232. # Sort appropriate keys
  233. if str:
  234. aproposKeys.sort()
  235. else:
  236. privateKeys.sort()
  237. remainingKeys.sort()
  238. # Print out results
  239. if wantPrivate:
  240. keys = aproposKeys + privateKeys + remainingKeys
  241. else:
  242. keys = aproposKeys + remainingKeys
  243. format = '%-' + `maxWidth` + 's'
  244. for key in keys:
  245. value = dict[key]
  246. if callable(value):
  247. strvalue = `Signature(value)`
  248. else:
  249. strvalue = `value`
  250. if fTruncate:
  251. # Cut off line (keeping at least 1 char)
  252. strvalue = strvalue[:max(1,lineWidth - maxWidth)]
  253. print (format % key)[:maxWidth] + '\t' + strvalue
  254. # Magic numbers: These are the bit masks in func_code.co_flags that
  255. # reveal whether or not the function has a *arg or **kw argument.
  256. _POS_LIST = 4
  257. _KEY_DICT = 8
  258. def _is_variadic(function):
  259. return function.func_code.co_flags & _POS_LIST
  260. def _has_keywordargs(function):
  261. return function.func_code.co_flags & _KEY_DICT
  262. def _varnames(function):
  263. return function.func_code.co_varnames
  264. def _getcode(f):
  265. """
  266. _getcode(f)
  267. This function returns the name and function object of a callable
  268. object.
  269. """
  270. def method_get(f):
  271. return f.__name__, f.im_func
  272. def function_get(f):
  273. return f.__name__, f
  274. def instance_get(f):
  275. if hasattr(f, '__call__'):
  276. method = f.__call__
  277. if (type(method) == types.MethodType):
  278. func = method.im_func
  279. else:
  280. func = method
  281. return ("%s%s" % (f.__class__.__name__, '__call__'), func)
  282. else:
  283. s = ("Instance %s of class %s does not have a __call__ method" %
  284. (f, f.__class__.__name__))
  285. raise TypeError, s
  286. def class_get(f):
  287. if hasattr(f, '__init__'):
  288. return f.__name__, f.__init__.im_func
  289. else:
  290. return f.__name__, lambda: None
  291. codedict = { types.UnboundMethodType: method_get,
  292. types.MethodType : method_get,
  293. types.FunctionType : function_get,
  294. types.InstanceType : instance_get,
  295. types.ClassType : class_get,
  296. }
  297. try:
  298. return codedict[type(f)](f)
  299. except KeyError:
  300. if callable(f): # eg, built-in functions and methods
  301. # raise ValueError, "type %s not supported yet." % type(f)
  302. return f.__name__, None
  303. else:
  304. raise TypeError, ("object %s of type %s is not callable." %
  305. (f, type(f)))
  306. class Signature:
  307. def __init__(self, func):
  308. self.type = type(func)
  309. self.name, self.func = _getcode(func)
  310. def ordinary_args(self):
  311. n = self.func.func_code.co_argcount
  312. return _varnames(self.func)[0:n]
  313. def special_args(self):
  314. n = self.func.func_code.co_argcount
  315. x = {}
  316. #
  317. if _is_variadic(self.func):
  318. x['positional'] = _varnames(self.func)[n]
  319. if _has_keywordargs(self.func):
  320. x['keyword'] = _varnames(self.func)[n+1]
  321. elif _has_keywordargs(self.func):
  322. x['keyword'] = _varnames(self.func)[n]
  323. else:
  324. pass
  325. return x
  326. def full_arglist(self):
  327. base = list(self.ordinary_args())
  328. x = self.special_args()
  329. if x.has_key('positional'):
  330. base.append(x['positional'])
  331. if x.has_key('keyword'):
  332. base.append(x['keyword'])
  333. return base
  334. def defaults(self):
  335. defargs = self.func.func_defaults
  336. args = self.ordinary_args()
  337. mapping = {}
  338. if defargs is not None:
  339. for i in range(-1, -(len(defargs)+1), -1):
  340. mapping[args[i]] = defargs[i]
  341. else:
  342. pass
  343. return mapping
  344. def __repr__(self):
  345. if self.func:
  346. defaults = self.defaults()
  347. specials = self.special_args()
  348. l = []
  349. for arg in self.ordinary_args():
  350. if defaults.has_key(arg):
  351. l.append( arg + '=' + str(defaults[arg]) )
  352. else:
  353. l.append( arg )
  354. if specials.has_key('positional'):
  355. l.append( '*' + specials['positional'] )
  356. if specials.has_key('keyword'):
  357. l.append( '**' + specials['keyword'] )
  358. return "%s(%s)" % (self.name, string.join(l, ', '))
  359. else:
  360. return "%s(?)" % self.name
  361. def aproposAll(obj):
  362. """
  363. Print out a list of all members and methods (including overloaded methods)
  364. of an instance or class
  365. """
  366. apropos(obj, fOverloaded = 1, fTruncate = 0)
  367. def doc(obj):
  368. if (isinstance(obj, types.MethodType)) or \
  369. (isinstance(obj, types.FunctionType)):
  370. print obj.__doc__
  371. def adjust(command = None, dim = 1, parent = None, **kw):
  372. """
  373. adjust(command = None, parent = None, **kw)
  374. Popup and entry scale to adjust a parameter
  375. Accepts any Slider keyword argument. Typical arguments include:
  376. command: The one argument command to execute
  377. min: The min value of the slider
  378. max: The max value of the slider
  379. resolution: The resolution of the slider
  380. text: The label on the slider
  381. These values can be accessed and/or changed after the fact
  382. >>> vg = adjust()
  383. >>> vg['min']
  384. 0.0
  385. >>> vg['min'] = 10.0
  386. >>> vg['min']
  387. 10.0
  388. """
  389. # Make sure we enable Tk
  390. from direct.tkwidgets import Valuator
  391. # Set command if specified
  392. if command:
  393. kw['command'] = lambda x: apply(command, x)
  394. if parent is None:
  395. kw['title'] = command.__name__
  396. kw['dim'] = dim
  397. # Create toplevel if needed
  398. if not parent:
  399. vg = apply(Valuator.ValuatorGroupPanel, (parent,), kw)
  400. else:
  401. vg = apply(Valuator.ValuatorGroup,(parent,), kw)
  402. vg.pack(expand = 1, fill = 'x')
  403. return vg
  404. def intersection(a, b):
  405. """
  406. intersection(list, list):
  407. """
  408. if not a: return []
  409. if not b: return []
  410. d = []
  411. for i in a:
  412. if (i in b) and (i not in d):
  413. d.append(i)
  414. for i in b:
  415. if (i in a) and (i not in d):
  416. d.append(i)
  417. return d
  418. def union(a, b):
  419. """
  420. union(list, list):
  421. """
  422. # Copy a
  423. c = a[:]
  424. for i in b:
  425. if (i not in c):
  426. c.append(i)
  427. return c
  428. def sameElements(a, b):
  429. if len(a) != len(b):
  430. return 0
  431. for elem in a:
  432. if elem not in b:
  433. return 0
  434. for elem in b:
  435. if elem not in a:
  436. return 0
  437. return 1
  438. def list2dict(L, value=None):
  439. """creates dict using elements of list, all assigned to same value"""
  440. return dict([(k,value) for k in L])
  441. def invertDict(D):
  442. """creates a dictionary by 'inverting' D; keys are placed in the new
  443. dictionary under their corresponding value in the old dictionary.
  444. Data will be lost if D contains any duplicate values.
  445. >>> old = {'key1':1, 'key2':2}
  446. >>> invertDict(old)
  447. {1: 'key1', 2: 'key2'}
  448. """
  449. n = {}
  450. for key, value in D.items():
  451. n[value] = key
  452. return n
  453. def invertDictLossless(D):
  454. """similar to invertDict, but values of new dict are lists of keys from
  455. old dict. No information is lost.
  456. >>> old = {'key1':1, 'key2':2, 'keyA':2}
  457. >>> invertDictLossless(old)
  458. {1: ['key1'], 2: ['key2', 'keyA']}
  459. """
  460. n = {}
  461. for key, value in D.items():
  462. n.setdefault(value, [])
  463. n[value].append(key)
  464. return n
  465. def uniqueElements(L):
  466. """are all elements of list unique?"""
  467. return len(L) == len(list2dict(L))
  468. def disjoint(L1, L2):
  469. """returns non-zero if L1 and L2 have no common elements"""
  470. used = dict([(k,None) for k in L1])
  471. for k in L2:
  472. if k in used:
  473. return 0
  474. return 1
  475. def contains(whole, sub):
  476. """
  477. Return 1 if whole contains sub, 0 otherwise
  478. """
  479. if (whole == sub):
  480. return 1
  481. for elem in sub:
  482. # The first item you find not in whole, return 0
  483. if elem not in whole:
  484. return 0
  485. # If you got here, whole must contain sub
  486. return 1
  487. def replace(list, old, new, all=0):
  488. """
  489. replace 'old' with 'new' in 'list'
  490. if all == 0, replace first occurrence
  491. otherwise replace all occurrences
  492. returns the number of items replaced
  493. """
  494. if old not in list:
  495. return 0
  496. if not all:
  497. i = list.index(old)
  498. list[i] = new
  499. return 1
  500. else:
  501. numReplaced = 0
  502. for i in xrange(len(list)):
  503. if list[i] == old:
  504. numReplaced += 1
  505. list[i] = new
  506. return numReplaced
  507. def reduceAngle(deg):
  508. """
  509. Reduces an angle (in degrees) to a value in [-180..180)
  510. """
  511. return (((deg + 180.) % 360.) - 180.)
  512. def fitSrcAngle2Dest(src, dest):
  513. """
  514. given a src and destination angle, returns an equivalent src angle
  515. that is within [-180..180) of dest
  516. examples:
  517. fitSrcAngle2Dest(30,60) == 30
  518. fitSrcAngle2Dest(60,30) == 60
  519. fitSrcAngle2Dest(0,180) == 0
  520. fitSrcAngle2Dest(-1,180) == 359
  521. fitSrcAngle2Dest(-180,180) == 180
  522. """
  523. return dest + reduceAngle(src - dest)
  524. def fitDestAngle2Src(src, dest):
  525. """
  526. given a src and destination angle, returns an equivalent dest angle
  527. that is within [-180..180) of src
  528. examples:
  529. fitDestAngle2Src(30,60) == 60
  530. fitDestAngle2Src(60,30) == 30
  531. fitDestAngle2Src(0,180) == -180
  532. fitDestAngle2Src(1,180) == 180
  533. """
  534. return src + (reduceAngle(dest - src))
  535. def closestDestAngle2(src, dest):
  536. # The function above didn't seem to do what I wanted. So I hacked
  537. # this one together. I can't really say I understand it. It's more
  538. # from impirical observation... GRW
  539. diff = src - dest
  540. if diff > 180:
  541. # if the difference is greater that 180 it's shorter to go the other way
  542. return dest - 360
  543. elif diff < -180:
  544. # or perhaps the OTHER other way...
  545. return dest + 360
  546. else:
  547. # otherwise just go to the original destination
  548. return dest
  549. def closestDestAngle(src, dest):
  550. # The function above didn't seem to do what I wanted. So I hacked
  551. # this one together. I can't really say I understand it. It's more
  552. # from impirical observation... GRW
  553. diff = src - dest
  554. if diff > 180:
  555. # if the difference is greater that 180 it's shorter to go the other way
  556. return src - (diff - 360)
  557. elif diff < -180:
  558. # or perhaps the OTHER other way...
  559. return src - (360 + diff)
  560. else:
  561. # otherwise just go to the original destination
  562. return dest
  563. def binaryRepr(number, max_length = 32):
  564. # This will only work reliably for relatively small numbers.
  565. # Increase the value of max_length if you think you're going
  566. # to use long integers
  567. assert number < 2L << max_length
  568. shifts = map (operator.rshift, max_length * [number], \
  569. range (max_length - 1, -1, -1))
  570. digits = map (operator.mod, shifts, max_length * [2])
  571. if not digits.count (1): return 0
  572. digits = digits [digits.index (1):]
  573. return string.join (map (repr, digits), '')
  574. # constant profile defaults
  575. PyUtilProfileDefaultFilename = 'profiledata'
  576. PyUtilProfileDefaultLines = 80
  577. PyUtilProfileDefaultSorts = ['cumulative', 'time', 'calls']
  578. # call this from the prompt, and break back out to the prompt
  579. # to stop profiling
  580. #
  581. # OR to do inline profiling, you must make a globally-visible
  582. # function to be profiled, i.e. to profile 'self.load()', do
  583. # something like this:
  584. #
  585. # def func(self=self):
  586. # self.load()
  587. # import __builtin__
  588. # __builtin__.func = func
  589. # PythonUtil.startProfile(cmd='func()', filename='profileData')
  590. # del __builtin__.func
  591. #
  592. def startProfile(filename=PyUtilProfileDefaultFilename,
  593. lines=PyUtilProfileDefaultLines,
  594. sorts=PyUtilProfileDefaultSorts,
  595. silent=0,
  596. callInfo=1,
  597. cmd='run()'):
  598. import profile
  599. profile.run(cmd, filename)
  600. if not silent:
  601. printProfile(filename, lines, sorts, callInfo)
  602. # call this to see the results again
  603. def printProfile(filename=PyUtilProfileDefaultFilename,
  604. lines=PyUtilProfileDefaultLines,
  605. sorts=PyUtilProfileDefaultSorts,
  606. callInfo=1):
  607. import pstats
  608. s = pstats.Stats(filename)
  609. s.strip_dirs()
  610. for sort in sorts:
  611. s.sort_stats(sort)
  612. s.print_stats(lines)
  613. if callInfo:
  614. s.print_callees(lines)
  615. s.print_callers(lines)
  616. class Functor:
  617. def __init__(self, function, *args, **kargs):
  618. assert callable(function), "function should be a callable obj"
  619. self._function = function
  620. self._args = args
  621. self._kargs = kargs
  622. self.__name__ = 'Functor: %s' % self._function.__name__
  623. self.__doc__ = self._function.__doc__
  624. def __call__(self, *args, **kargs):
  625. """call function"""
  626. _args = list(self._args)
  627. _args.extend(args)
  628. _kargs = self._kargs.copy()
  629. _kargs.update(kargs)
  630. return apply(self._function,_args,_kargs)
  631. def bound(value, bound1, bound2):
  632. """
  633. returns value if value is between bound1 and bound2
  634. otherwise returns bound that is closer to value
  635. """
  636. if bound1 > bound2:
  637. return min(max(value, bound2), bound1)
  638. else:
  639. return min(max(value, bound1), bound2)
  640. def lerp(v0, v1, t):
  641. """
  642. returns a value lerped between v0 and v1, according to t
  643. t == 0 maps to v0, t == 1 maps to v1
  644. """
  645. return v0 + (t * (v1 - v0))
  646. def average(*args):
  647. """ returns simple average of list of values """
  648. val = 0.
  649. for arg in args:
  650. val += arg
  651. return val / len(args)
  652. def boolEqual(a, b):
  653. """
  654. returns true if a and b are both true or both false.
  655. returns false otherwise
  656. (a.k.a. xnor -- eXclusive Not OR).
  657. """
  658. return (a and b) or not (a or b)
  659. def lineupPos(i, num, spacing):
  660. """
  661. use to line up a series of 'num' objects, in one dimension,
  662. centered around zero
  663. 'i' is the index of the object in the lineup
  664. 'spacing' is the amount of space between objects in the lineup
  665. """
  666. assert num >= 1
  667. assert i >= 0 and i < num
  668. pos = float(i) * spacing
  669. return pos - ((float(spacing) * (num-1))/2.)
  670. def formatElapsedSeconds(seconds):
  671. """
  672. Returns a string of the form "mm:ss" or "hh:mm:ss" or "n days",
  673. representing the indicated elapsed time in seconds.
  674. """
  675. sign = ''
  676. if seconds < 0:
  677. seconds = -seconds
  678. sign = '-'
  679. # We use math.floor() instead of casting to an int, so we avoid
  680. # problems with numbers that are too large to represent as
  681. # type int.
  682. seconds = math.floor(seconds)
  683. hours = math.floor(seconds / (60 * 60))
  684. if hours > 36:
  685. days = math.floor((hours + 12) / 24)
  686. return "%s%d days" % (sign, days)
  687. seconds -= hours * (60 * 60)
  688. minutes = (int)(seconds / 60)
  689. seconds -= minutes * 60
  690. if hours != 0:
  691. return "%s%d:%02d:%02d" % (sign, hours, minutes, seconds)
  692. else:
  693. return "%s%d:%02d" % (sign, minutes, seconds)
  694. def solveQuadratic(a, b, c):
  695. # quadratic equation: ax^2 + bx + c = 0
  696. # quadratic formula: x = [-b +/- sqrt(b^2 - 4ac)] / 2a
  697. # returns None, root, or [root1, root2]
  698. # a cannot be zero.
  699. if a == 0.:
  700. return None
  701. # calculate the determinant (b^2 - 4ac)
  702. D = (b * b) - (4. * a * c)
  703. if D < 0:
  704. # there are no solutions (sqrt(negative number) is undefined)
  705. return None
  706. elif D == 0:
  707. # only one root
  708. return (-b) / (2. * a)
  709. else:
  710. # OK, there are two roots
  711. sqrtD = math.sqrt(D)
  712. twoA = 2. * a
  713. root1 = ((-b) - sqrtD) / twoA
  714. root2 = ((-b) + sqrtD) / twoA
  715. return [root1, root2]
  716. def stackEntryInfo(depth=0, baseFileName=1):
  717. """
  718. returns the sourcefilename, line number, and function name of
  719. an entry in the stack.
  720. 'depth' is how far back to go in the stack; 0 is the caller of this
  721. function, 1 is the function that called the caller of this function, etc.
  722. by default, strips off the path of the filename; override with baseFileName
  723. returns (fileName, lineNum, funcName) --> (string, int, string)
  724. returns (None, None, None) on error
  725. """
  726. try:
  727. stack = None
  728. frame = None
  729. try:
  730. stack = inspect.stack()
  731. # add one to skip the frame associated with this function
  732. frame = stack[depth+1]
  733. filename = frame[1]
  734. if baseFileName:
  735. filename = os.path.basename(filename)
  736. lineNum = frame[2]
  737. funcName = frame[3]
  738. result = (filename, lineNum, funcName)
  739. finally:
  740. del stack
  741. del frame
  742. except:
  743. result = (None, None, None)
  744. return result
  745. def lineInfo(baseFileName=1):
  746. """
  747. returns the sourcefilename, line number, and function name of the
  748. code that called this function
  749. (answers the question: 'hey lineInfo, where am I in the codebase?')
  750. see stackEntryInfo, above, for info on 'baseFileName' and return types
  751. """
  752. return stackEntryInfo(1)
  753. def callerInfo(baseFileName=1):
  754. """
  755. returns the sourcefilename, line number, and function name of the
  756. caller of the function that called this function
  757. (answers the question: 'hey callerInfo, who called me?')
  758. see stackEntryInfo, above, for info on 'baseFileName' and return types
  759. """
  760. return stackEntryInfo(2)
  761. def lineTag(baseFileName=1, verbose=0, separator=':'):
  762. """
  763. returns a string containing the sourcefilename and line number
  764. of the code that called this function
  765. (equivalent to lineInfo, above, with different return type)
  766. see stackEntryInfo, above, for info on 'baseFileName'
  767. if 'verbose' is false, returns a compact string of the form
  768. 'fileName:lineNum:funcName'
  769. if 'verbose' is true, returns a longer string that matches the
  770. format of Python stack trace dumps
  771. returns empty string on error
  772. """
  773. fileName, lineNum, funcName = callerInfo()
  774. if fileName is None:
  775. return ''
  776. if verbose:
  777. return 'File "%s", line %s, in %s' % (fileName, lineNum, funcName)
  778. else:
  779. return '%s%s%s%s%s' % (fileName, separator, lineNum, separator,
  780. funcName)
  781. def findPythonModule(module):
  782. # Look along the python load path for the indicated filename.
  783. # Returns the located pathname, or None if the filename is not
  784. # found.
  785. filename = module + '.py'
  786. for dir in sys.path:
  787. pathname = os.path.join(dir, filename)
  788. if os.path.exists(pathname):
  789. return pathname
  790. return None
  791. def describeException(backTrace = 4):
  792. # When called in an exception handler, returns a string describing
  793. # the current exception.
  794. def byteOffsetToLineno(code, byte):
  795. # Returns the source line number corresponding to the given byte
  796. # offset into the indicated Python code module.
  797. import array
  798. lnotab = array.array('B', code.co_lnotab)
  799. line = code.co_firstlineno
  800. for i in range(0, len(lnotab),2):
  801. byte -= lnotab[i]
  802. if byte <= 0:
  803. return line
  804. line += lnotab[i+1]
  805. return line
  806. infoArr = sys.exc_info()
  807. exception = infoArr[0]
  808. exceptionName = getattr(exception, '__name__', None)
  809. extraInfo = infoArr[1]
  810. trace = infoArr[2]
  811. stack = []
  812. while trace.tb_next:
  813. # We need to call byteOffsetToLineno to determine the true
  814. # line number at which the exception occurred, even though we
  815. # have both trace.tb_lineno and frame.f_lineno, which return
  816. # the correct line number only in non-optimized mode.
  817. frame = trace.tb_frame
  818. module = frame.f_globals.get('__name__', None)
  819. lineno = byteOffsetToLineno(frame.f_code, frame.f_lasti)
  820. stack.append("%s:%s, " % (module, lineno))
  821. trace = trace.tb_next
  822. frame = trace.tb_frame
  823. module = frame.f_globals.get('__name__', None)
  824. lineno = byteOffsetToLineno(frame.f_code, frame.f_lasti)
  825. stack.append("%s:%s, " % (module, lineno))
  826. description = ""
  827. for i in range(len(stack) - 1, max(len(stack) - backTrace, 0) - 1, -1):
  828. description += stack[i]
  829. description += "%s: %s" % (exceptionName, extraInfo)
  830. return description
  831. def mostDerivedLast(classList):
  832. """pass in list of classes. sorts list in-place, with derived classes
  833. appearing after their bases"""
  834. def compare(a,b):
  835. if issubclass(a,b):
  836. result=1
  837. elif issubclass(b,a):
  838. result=-1
  839. else:
  840. result=0
  841. #print a,b,result
  842. return result
  843. classList.sort(compare)
  844. def clampScalar(value, a, b):
  845. # calling this ought to be faster than calling both min and max
  846. if a < b:
  847. if value < a:
  848. return a
  849. elif value > b:
  850. return b
  851. else:
  852. return value
  853. else:
  854. if value < b:
  855. return b
  856. elif value > a:
  857. return a
  858. else:
  859. return value
  860. def weightedChoice(choiceList, rng=random.random, sum=None):
  861. """given a list of (weight,item) pairs, chooses an item based on the
  862. weights. rng must return 0..1. if you happen to have the sum of the
  863. weights, pass it in 'sum'."""
  864. # TODO: add support for dicts
  865. if sum is None:
  866. sum = 0.
  867. for weight, item in choiceList:
  868. sum += weight
  869. rand = rng()
  870. accum = rand * sum
  871. for weight, item in choiceList:
  872. accum -= weight
  873. if accum <= 0.:
  874. return item
  875. # rand is ~1., and floating-point error prevented accum from hitting 0.
  876. # Or you passed in a 'sum' that was was too large.
  877. # Return the last item.
  878. return item
  879. def randFloat(a, b=0., rng=random.random):
  880. """returns a random float in [a,b]
  881. call with single argument to generate random float between arg and zero
  882. """
  883. return lerp(a,b,rng())
  884. def normalDistrib(a, b, gauss=random.gauss):
  885. """
  886. NOTE: assumes a < b
  887. Returns random number between a and b, using gaussian distribution, with
  888. mean=avg(a,b), and a standard deviation that fits ~99.7% of the curve
  889. between a and b. Outlying results are clipped to a and b.
  890. ------------------------------------------------------------------------
  891. http://www-stat.stanford.edu/~naras/jsm/NormalDensity/NormalDensity.html
  892. The 68-95-99.7% Rule
  893. ====================
  894. All normal density curves satisfy the following property which is often
  895. referred to as the Empirical Rule:
  896. 68% of the observations fall within 1 standard deviation of the mean.
  897. 95% of the observations fall within 2 standard deviations of the mean.
  898. 99.7% of the observations fall within 3 standard deviations of the mean.
  899. Thus, for a normal distribution, almost all values lie within 3 standard
  900. deviations of the mean.
  901. ------------------------------------------------------------------------
  902. In calculating our standard deviation, we divide (b-a) by 6, since the
  903. 99.7% figure includes 3 standard deviations _on_either_side_ of the mean.
  904. """
  905. return max(a, min(b, gauss((a+b)*.5, (b-a)/6.)))
  906. def weightedRand(valDict, rng=random.random):
  907. """
  908. pass in a dictionary with a selection -> weight mapping. Eg.
  909. {"Choice 1" : 10,
  910. "Choice 2" : 30,
  911. "bear" : 100}
  912. -Weights need not add up to any particular value.
  913. -The actual selection will be returned.
  914. """
  915. selections = valDict.keys()
  916. weights = valDict.values()
  917. totalWeight = 0
  918. for weight in weights:
  919. totalWeight += weight
  920. # get a random value between 0 and the total of the weights
  921. randomWeight = rng() * totalWeight
  922. # find the index that corresponds with this weight
  923. for i in range(len(weights)):
  924. totalWeight -= weights[i]
  925. if totalWeight <= randomWeight:
  926. return selections[i]
  927. assert(True, "Should never get here")
  928. return selections[-1]
  929. def randUint31(rng=random.random):
  930. """returns a random integer in [0..2^31).
  931. rng must return float in [0..1]"""
  932. return int(rng() * 0x7FFFFFFF)
  933. def randInt32(rng=random.random):
  934. """returns a random integer in [-2147483648..2147483647].
  935. rng must return float in [0..1]
  936. """
  937. i = int(rng() * 0x7FFFFFFF)
  938. if rng() < .5:
  939. i += 0x80000000
  940. return i
  941. class Enum:
  942. """Pass in list of strings or string of comma-separated strings.
  943. Items are accessible as instance.item, and are assigned unique,
  944. increasing integer values. Pass in integer for 'start' to override
  945. starting value.
  946. Example:
  947. >>> colors = Enum('red, green, blue')
  948. >>> colors.red
  949. 0
  950. >>> colors.green
  951. 1
  952. >>> colors.blue
  953. 2
  954. >>> colors.getString(colors.red)
  955. 'red'
  956. """
  957. if __debug__:
  958. # chars that cannot appear within an item string.
  959. InvalidChars = string.whitespace
  960. def _checkValidIdentifier(item):
  961. invalidChars = string.whitespace+string.punctuation
  962. invalidChars = invalidChars.replace('_','')
  963. invalidFirstChars = invalidChars+string.digits
  964. if item[0] in invalidFirstChars:
  965. raise SyntaxError, ("Enum '%s' contains invalid first char" %
  966. item)
  967. if not disjoint(item, invalidChars):
  968. for char in item:
  969. if char in invalidChars:
  970. raise SyntaxError, (
  971. "Enum\n'%s'\ncontains illegal char '%s'" %
  972. (item, char))
  973. return 1
  974. _checkValidIdentifier = staticmethod(_checkValidIdentifier)
  975. def __init__(self, items, start=0):
  976. if type(items) == types.StringType:
  977. items = items.split(',')
  978. self._stringTable = {}
  979. # make sure we don't overwrite an existing element of the class
  980. assert(self._checkExistingMembers(items))
  981. assert(uniqueElements(items))
  982. i = start
  983. for item in items:
  984. # remove leading/trailing whitespace
  985. item = string.strip(item)
  986. # is there anything left?
  987. if len(item) == 0:
  988. continue
  989. # make sure there are no invalid characters
  990. assert(Enum._checkValidIdentifier(item))
  991. self.__dict__[item] = i
  992. self._stringTable[i] = item
  993. i += 1
  994. def getString(self, value):
  995. return self._stringTable[value]
  996. def __contains__(self, value):
  997. return value in self._stringTable
  998. def __len__(self):
  999. return len(self._stringTable)
  1000. if __debug__:
  1001. def _checkExistingMembers(self, items):
  1002. for item in items:
  1003. if hasattr(self, item):
  1004. return 0
  1005. return 1
  1006. ############################################################
  1007. # class: Singleton
  1008. # Purpose: This provides a base metaclass for all classes
  1009. # that require one and only one instance.
  1010. #
  1011. # Example: class mySingleton:
  1012. # __metaclass__ = PythonUtil.Singleton
  1013. # def __init__(self,...):
  1014. # ...
  1015. #
  1016. # Note: This class is based on Python's New-Style Class
  1017. # design. An error will occur if a defined class
  1018. # attemps to inherit from a Classic-Style Class only,
  1019. # ie: class myClassX:
  1020. # def __init__(self, ...):
  1021. # ...
  1022. #
  1023. # class myNewClassX(myClassX):
  1024. # __metaclass__ = PythonUtil.Singleton
  1025. # def __init__(self, ...):
  1026. # myClassX.__init__(self, ...)
  1027. # ...
  1028. #
  1029. # This causes problems because myNewClassX is a
  1030. # New-Style class that inherits from only a
  1031. # Classic-Style base class. There are two ways
  1032. # simple ways to resolve this issue.
  1033. #
  1034. # First, if possible, make myClassX a
  1035. # New-Style class by inheriting from object
  1036. # object. IE: class myClassX(object):
  1037. #
  1038. # If for some reason that is not an option, make
  1039. # myNewClassX inherit from object and myClassX.
  1040. # IE: class myNewClassX(object, myClassX):
  1041. ############################################################
  1042. class Singleton(type):
  1043. def __init__(cls,name,bases,dic):
  1044. super(Singleton,cls).__init__(name,bases,dic)
  1045. cls.instance=None
  1046. def __call__(cls,*args,**kw):
  1047. if cls.instance is None:
  1048. cls.instance=super(Singleton,cls).__call__(*args,**kw)
  1049. return cls.instance
  1050. class SingletonError(ValueError):
  1051. """ Used to indicate an inappropriate value for a Singleton."""