PythonUtil.py 36 KB

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