PythonUtil.py 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684
  1. """Contains miscellaneous utility functions and classes."""
  2. __all__ = [
  3. 'indent', 'doc', 'adjust', 'difference', 'intersection', 'union',
  4. 'sameElements', 'makeList', 'makeTuple', 'list2dict', 'invertDict',
  5. 'invertDictLossless', 'uniqueElements', 'disjoint', 'contains', 'replace',
  6. 'reduceAngle', 'fitSrcAngle2Dest', 'fitDestAngle2Src', 'closestDestAngle2',
  7. 'closestDestAngle', 'getSetterName', 'getSetter', 'Functor', 'Stack',
  8. 'Queue', 'bound', 'clamp', 'lerp', 'average', 'addListsByValue',
  9. 'boolEqual', 'lineupPos', 'formatElapsedSeconds', 'solveQuadratic',
  10. 'findPythonModule', 'mostDerivedLast', 'clampScalar', 'weightedChoice',
  11. 'randFloat', 'normalDistrib', 'weightedRand', 'randUint31', 'randInt32',
  12. 'SerialNumGen', 'serialNum', 'uniqueName', 'Enum', 'Singleton',
  13. 'SingletonError', 'printListEnum', 'safeRepr', 'fastRepr',
  14. 'isDefaultValue', 'ScratchPad', 'Sync', 'itype', 'getNumberedTypedString',
  15. 'getNumberedTypedSortedString', 'printNumberedTyped', 'DelayedCall',
  16. 'DelayedFunctor', 'FrameDelayedCall', 'SubframeCall', 'getBase',
  17. 'GoldenRatio', 'GoldenRectangle', 'rad90', 'rad180', 'rad270', 'rad360',
  18. 'nullGen', 'loopGen', 'makeFlywheelGen', 'flywheel', 'listToIndex2item',
  19. 'listToItem2index', 'formatTimeCompact', 'deeptype', 'StdoutCapture',
  20. 'StdoutPassthrough', 'Averager', 'getRepository', 'formatTimeExact',
  21. 'startSuperLog', 'endSuperLog', 'typeName', 'safeTypeName',
  22. 'histogramDict', 'unescapeHtmlString',
  23. ]
  24. if __debug__:
  25. __all__ += ['StackTrace', 'traceFunctionCall', 'traceParentCall',
  26. 'printThisCall', 'stackEntryInfo', 'lineInfo', 'callerInfo',
  27. 'lineTag', 'profileFunc', 'profiled', 'startProfile',
  28. 'printProfile', 'getProfileResultString', 'printStack',
  29. 'printReverseStack']
  30. import types
  31. import math
  32. import os
  33. import sys
  34. import random
  35. import time
  36. import builtins
  37. import importlib
  38. import functools
  39. __report_indent = 3
  40. from panda3d.core import ConfigVariableBool, ClockObject
  41. ## with one integer positional arg, this uses about 4/5 of the memory of the Functor class below
  42. #def Functor(function, *args, **kArgs):
  43. # argsCopy = args[:]
  44. # def functor(*cArgs, **ckArgs):
  45. # kArgs.update(ckArgs)
  46. # return function(*(argsCopy + cArgs), **kArgs)
  47. # return functor
  48. class Functor:
  49. def __init__(self, function, *args, **kargs):
  50. assert callable(function), "function should be a callable obj"
  51. self._function = function
  52. self._args = args
  53. self._kargs = kargs
  54. if hasattr(self._function, '__name__'):
  55. self.__name__ = self._function.__name__
  56. else:
  57. self.__name__ = str(itype(self._function))
  58. if hasattr(self._function, '__doc__'):
  59. self.__doc__ = self._function.__doc__
  60. else:
  61. self.__doc__ = self.__name__
  62. def destroy(self):
  63. del self._function
  64. del self._args
  65. del self._kargs
  66. del self.__name__
  67. del self.__doc__
  68. def _do__call__(self, *args, **kargs):
  69. _kargs = self._kargs.copy()
  70. _kargs.update(kargs)
  71. return self._function(*(self._args + args), **_kargs)
  72. __call__ = _do__call__
  73. def __repr__(self):
  74. s = 'Functor(%s' % self._function.__name__
  75. for arg in self._args:
  76. try:
  77. argStr = repr(arg)
  78. except:
  79. argStr = 'bad repr: %s' % arg.__class__
  80. s += ', %s' % argStr
  81. for karg, value in list(self._kargs.items()):
  82. s += ', %s=%s' % (karg, repr(value))
  83. s += ')'
  84. return s
  85. class Stack:
  86. def __init__(self):
  87. self.__list = []
  88. def push(self, item):
  89. self.__list.append(item)
  90. def top(self):
  91. # return the item on the top of the stack without popping it off
  92. return self.__list[-1]
  93. def pop(self):
  94. return self.__list.pop()
  95. def clear(self):
  96. self.__list = []
  97. def isEmpty(self):
  98. return len(self.__list) == 0
  99. def __len__(self):
  100. return len(self.__list)
  101. class Queue:
  102. # FIFO queue
  103. # interface is intentionally identical to Stack (LIFO)
  104. def __init__(self):
  105. self.__list = []
  106. def push(self, item):
  107. self.__list.append(item)
  108. def top(self):
  109. # return the next item at the front of the queue without popping it off
  110. return self.__list[0]
  111. def front(self):
  112. return self.__list[0]
  113. def back(self):
  114. return self.__list[-1]
  115. def pop(self):
  116. return self.__list.pop(0)
  117. def clear(self):
  118. self.__list = []
  119. def isEmpty(self):
  120. return len(self.__list) == 0
  121. def __len__(self):
  122. return len(self.__list)
  123. def indent(stream, numIndents, str):
  124. """
  125. Write str to stream with numIndents in front of it
  126. """
  127. # To match emacs, instead of a tab character we will use 4 spaces
  128. stream.write(' ' * numIndents + str)
  129. if __debug__:
  130. import traceback
  131. import marshal
  132. class StackTrace:
  133. def __init__(self, label="", start=0, limit=None):
  134. """
  135. label is a string (or anything that be be a string)
  136. that is printed as part of the trace back.
  137. This is just to make it easier to tell what the
  138. stack trace is referring to.
  139. start is an integer number of stack frames back
  140. from the most recent. (This is automatically
  141. bumped up by one to skip the __init__ call
  142. to the StackTrace).
  143. limit is an integer number of stack frames
  144. to record (or None for unlimited).
  145. """
  146. self.label = label
  147. if limit is not None:
  148. self.trace = traceback.extract_stack(sys._getframe(1+start),
  149. limit=limit)
  150. else:
  151. self.trace = traceback.extract_stack(sys._getframe(1+start))
  152. def compact(self):
  153. r = ''
  154. comma = ','
  155. for filename, lineNum, funcName, text in self.trace:
  156. r += '%s.%s:%s%s' % (filename[:filename.rfind('.py')][filename.rfind('\\')+1:], funcName, lineNum, comma)
  157. if len(r) > 0:
  158. r = r[:-len(comma)]
  159. return r
  160. def reverseCompact(self):
  161. r = ''
  162. comma = ','
  163. for filename, lineNum, funcName, text in self.trace:
  164. r = '%s.%s:%s%s%s' % (filename[:filename.rfind('.py')][filename.rfind('\\')+1:], funcName, lineNum, comma, r)
  165. if len(r) > 0:
  166. r = r[:-len(comma)]
  167. return r
  168. def __str__(self):
  169. r = "Debug stack trace of %s (back %s frames):\n"%(
  170. self.label, len(self.trace),)
  171. for i in traceback.format_list(self.trace):
  172. r+=i
  173. r+="***** NOTE: This is not a crash. This is a debug stack trace. *****"
  174. return r
  175. def printStack():
  176. print(StackTrace(start=1).compact())
  177. return True
  178. def printReverseStack():
  179. print(StackTrace(start=1).reverseCompact())
  180. return True
  181. def printVerboseStack():
  182. print(StackTrace(start=1))
  183. return True
  184. #-----------------------------------------------------------------------------
  185. def traceFunctionCall(frame):
  186. """
  187. return a string that shows the call frame with calling arguments.
  188. e.g.
  189. foo(x=234, y=135)
  190. """
  191. f = frame
  192. co = f.f_code
  193. dict = f.f_locals
  194. n = co.co_argcount
  195. if co.co_flags & 4:
  196. n = n + 1
  197. if co.co_flags & 8:
  198. n = n + 1
  199. r = ''
  200. if 'self' in dict:
  201. r = '%s.' % (dict['self'].__class__.__name__,)
  202. r += "%s(" % (f.f_code.co_name,)
  203. comma=0 # formatting, whether we should type a comma.
  204. for i in range(n):
  205. name = co.co_varnames[i]
  206. if name == 'self':
  207. continue
  208. if comma:
  209. r+=', '
  210. else:
  211. # ok, we skipped the first one, the rest get commas:
  212. comma=1
  213. r+=name
  214. r+='='
  215. if name in dict:
  216. v=safeRepr(dict[name])
  217. if len(v)>2000:
  218. # r+="<too big for debug>"
  219. r += (v[:2000] + "...")
  220. else:
  221. r+=v
  222. else: r+="*** undefined ***"
  223. return r+')'
  224. def traceParentCall():
  225. return traceFunctionCall(sys._getframe(2))
  226. def printThisCall():
  227. print(traceFunctionCall(sys._getframe(1)))
  228. return 1 # to allow "assert printThisCall()"
  229. # Magic numbers: These are the bit masks in func_code.co_flags that
  230. # reveal whether or not the function has a *arg or **kw argument.
  231. _POS_LIST = 4
  232. _KEY_DICT = 8
  233. def doc(obj):
  234. if (isinstance(obj, types.MethodType)) or \
  235. (isinstance(obj, types.FunctionType)):
  236. print(obj.__doc__)
  237. def adjust(command = None, dim = 1, parent = None, **kw):
  238. """
  239. adjust(command = None, parent = None, **kw)
  240. Popup and entry scale to adjust a parameter
  241. Accepts any Slider keyword argument. Typical arguments include:
  242. command: The one argument command to execute
  243. min: The min value of the slider
  244. max: The max value of the slider
  245. resolution: The resolution of the slider
  246. text: The label on the slider
  247. These values can be accessed and/or changed after the fact
  248. >>> vg = adjust()
  249. >>> vg['min']
  250. 0.0
  251. >>> vg['min'] = 10.0
  252. >>> vg['min']
  253. 10.0
  254. """
  255. # Make sure we enable Tk
  256. # Don't use a regular import, to prevent ModuleFinder from picking
  257. # it up as a dependency when building a .p3d package.
  258. Valuator = importlib.import_module('direct.tkwidgets.Valuator')
  259. # Set command if specified
  260. if command:
  261. kw['command'] = lambda x: command(*x)
  262. if parent is None:
  263. kw['title'] = command.__name__
  264. kw['dim'] = dim
  265. # Create toplevel if needed
  266. if not parent:
  267. vg = Valuator.ValuatorGroupPanel(parent, **kw)
  268. else:
  269. vg = Valuator.ValuatorGroup(parent, **kw)
  270. vg.pack(expand = 1, fill = 'x')
  271. return vg
  272. def difference(a, b):
  273. """
  274. difference(list, list):
  275. """
  276. if not a: return b
  277. if not b: return a
  278. d = []
  279. for i in a:
  280. if (i not in b) and (i not in d):
  281. d.append(i)
  282. for i in b:
  283. if (i not in a) and (i not in d):
  284. d.append(i)
  285. return d
  286. def intersection(a, b):
  287. """
  288. intersection(list, list):
  289. """
  290. if not a or not b:
  291. return []
  292. d = []
  293. for i in a:
  294. if (i in b) and (i not in d):
  295. d.append(i)
  296. for i in b:
  297. if (i in a) and (i not in d):
  298. d.append(i)
  299. return d
  300. def union(a, b):
  301. """
  302. union(list, list):
  303. """
  304. # Copy a
  305. c = a[:]
  306. for i in b:
  307. if i not in c:
  308. c.append(i)
  309. return c
  310. def sameElements(a, b):
  311. if len(a) != len(b):
  312. return 0
  313. for elem in a:
  314. if elem not in b:
  315. return 0
  316. for elem in b:
  317. if elem not in a:
  318. return 0
  319. return 1
  320. def makeList(x):
  321. """returns x, converted to a list"""
  322. if isinstance(x, list):
  323. return x
  324. elif isinstance(x, tuple):
  325. return list(x)
  326. else:
  327. return [x,]
  328. def makeTuple(x):
  329. """returns x, converted to a tuple"""
  330. if isinstance(x, list):
  331. return tuple(x)
  332. elif isinstance(x, tuple):
  333. return x
  334. else:
  335. return (x,)
  336. def list2dict(L, value=None):
  337. """creates dict using elements of list, all assigned to same value"""
  338. return dict([(k, value) for k in L])
  339. def listToIndex2item(L):
  340. """converts list to dict of list index->list item"""
  341. d = {}
  342. for i, item in enumerate(L):
  343. d[i] = item
  344. return d
  345. assert listToIndex2item(['a','b']) == {0: 'a', 1: 'b',}
  346. def listToItem2index(L):
  347. """converts list to dict of list item->list index
  348. This is lossy if there are duplicate list items"""
  349. d = {}
  350. for i, item in enumerate(L):
  351. d[item] = i
  352. return d
  353. assert listToItem2index(['a','b']) == {'a': 0, 'b': 1,}
  354. def invertDict(D, lossy=False):
  355. """creates a dictionary by 'inverting' D; keys are placed in the new
  356. dictionary under their corresponding value in the old dictionary.
  357. It is an error if D contains any duplicate values.
  358. >>> old = {'key1':1, 'key2':2}
  359. >>> invertDict(old)
  360. {1: 'key1', 2: 'key2'}
  361. """
  362. n = {}
  363. for key, value in D.items():
  364. if not lossy and value in n:
  365. raise Exception('duplicate key in invertDict: %s' % value)
  366. n[value] = key
  367. return n
  368. def invertDictLossless(D):
  369. """similar to invertDict, but values of new dict are lists of keys from
  370. old dict. No information is lost.
  371. >>> old = {'key1':1, 'key2':2, 'keyA':2}
  372. >>> invertDictLossless(old)
  373. {1: ['key1'], 2: ['key2', 'keyA']}
  374. """
  375. n = {}
  376. for key, value in D.items():
  377. n.setdefault(value, [])
  378. n[value].append(key)
  379. return n
  380. def uniqueElements(L):
  381. """are all elements of list unique?"""
  382. return len(L) == len(list2dict(L))
  383. def disjoint(L1, L2):
  384. """returns non-zero if L1 and L2 have no common elements"""
  385. used = dict([(k, None) for k in L1])
  386. for k in L2:
  387. if k in used:
  388. return 0
  389. return 1
  390. def contains(whole, sub):
  391. """
  392. Return 1 if whole contains sub, 0 otherwise
  393. """
  394. if whole == sub:
  395. return 1
  396. for elem in sub:
  397. # The first item you find not in whole, return 0
  398. if elem not in whole:
  399. return 0
  400. # If you got here, whole must contain sub
  401. return 1
  402. def replace(list, old, new, all=0):
  403. """
  404. replace 'old' with 'new' in 'list'
  405. if all == 0, replace first occurrence
  406. otherwise replace all occurrences
  407. returns the number of items replaced
  408. """
  409. if old not in list:
  410. return 0
  411. if not all:
  412. i = list.index(old)
  413. list[i] = new
  414. return 1
  415. else:
  416. numReplaced = 0
  417. for i in range(len(list)):
  418. if list[i] == old:
  419. numReplaced += 1
  420. list[i] = new
  421. return numReplaced
  422. rad90 = math.pi / 2.
  423. rad180 = math.pi
  424. rad270 = 1.5 * math.pi
  425. rad360 = 2. * math.pi
  426. def reduceAngle(deg):
  427. """
  428. Reduces an angle (in degrees) to a value in [-180..180)
  429. """
  430. return ((deg + 180.) % 360.) - 180.
  431. def fitSrcAngle2Dest(src, dest):
  432. """
  433. given a src and destination angle, returns an equivalent src angle
  434. that is within [-180..180) of dest
  435. examples:
  436. fitSrcAngle2Dest(30, 60) == 30
  437. fitSrcAngle2Dest(60, 30) == 60
  438. fitSrcAngle2Dest(0, 180) == 0
  439. fitSrcAngle2Dest(-1, 180) == 359
  440. fitSrcAngle2Dest(-180, 180) == 180
  441. """
  442. return dest + reduceAngle(src - dest)
  443. def fitDestAngle2Src(src, dest):
  444. """
  445. given a src and destination angle, returns an equivalent dest angle
  446. that is within [-180..180) of src
  447. examples:
  448. fitDestAngle2Src(30, 60) == 60
  449. fitDestAngle2Src(60, 30) == 30
  450. fitDestAngle2Src(0, 180) == -180
  451. fitDestAngle2Src(1, 180) == 180
  452. """
  453. return src + (reduceAngle(dest - src))
  454. def closestDestAngle2(src, dest):
  455. # The function above didn't seem to do what I wanted. So I hacked
  456. # this one together. I can't really say I understand it. It's more
  457. # from impirical observation... GRW
  458. diff = src - dest
  459. if diff > 180:
  460. # if the difference is greater that 180 it's shorter to go the other way
  461. return dest - 360
  462. elif diff < -180:
  463. # or perhaps the OTHER other way...
  464. return dest + 360
  465. else:
  466. # otherwise just go to the original destination
  467. return dest
  468. def closestDestAngle(src, dest):
  469. # The function above didn't seem to do what I wanted. So I hacked
  470. # this one together. I can't really say I understand it. It's more
  471. # from impirical observation... GRW
  472. diff = src - dest
  473. if diff > 180:
  474. # if the difference is greater that 180 it's shorter to go the other way
  475. return src - (diff - 360)
  476. elif diff < -180:
  477. # or perhaps the OTHER other way...
  478. return src - (360 + diff)
  479. else:
  480. # otherwise just go to the original destination
  481. return dest
  482. class StdoutCapture:
  483. # redirects stdout to a string
  484. def __init__(self):
  485. self._oldStdout = sys.stdout
  486. sys.stdout = self
  487. self._string = ''
  488. def destroy(self):
  489. sys.stdout = self._oldStdout
  490. del self._oldStdout
  491. def getString(self):
  492. return self._string
  493. # internal
  494. def write(self, string):
  495. self._string = ''.join([self._string, string])
  496. class StdoutPassthrough(StdoutCapture):
  497. # like StdoutCapture but also allows output to go through to the OS as normal
  498. # internal
  499. def write(self, string):
  500. self._string = ''.join([self._string, string])
  501. self._oldStdout.write(string)
  502. # constant profile defaults
  503. if __debug__:
  504. from io import StringIO
  505. PyUtilProfileDefaultFilename = 'profiledata'
  506. PyUtilProfileDefaultLines = 80
  507. PyUtilProfileDefaultSorts = ['cumulative', 'time', 'calls']
  508. _ProfileResultStr = ''
  509. def getProfileResultString():
  510. # if you called profile with 'log' not set to True,
  511. # you can call this function to get the results as
  512. # a string
  513. global _ProfileResultStr
  514. return _ProfileResultStr
  515. def profileFunc(callback, name, terse, log=True):
  516. global _ProfileResultStr
  517. if 'globalProfileFunc' in builtins.__dict__:
  518. # rats. Python profiler is not re-entrant...
  519. base.notify.warning(
  520. 'PythonUtil.profileStart(%s): aborted, already profiling %s'
  521. #'\nStack Trace:\n%s'
  522. % (name, builtins.globalProfileFunc,
  523. #StackTrace()
  524. ))
  525. return
  526. builtins.globalProfileFunc = callback
  527. builtins.globalProfileResult = [None]
  528. prefix = '***** START PROFILE: %s *****' % name
  529. if log:
  530. print(prefix)
  531. startProfile(cmd='globalProfileResult[0]=globalProfileFunc()', callInfo=(not terse), silent=not log)
  532. suffix = '***** END PROFILE: %s *****' % name
  533. if log:
  534. print(suffix)
  535. else:
  536. _ProfileResultStr = '%s\n%s\n%s' % (prefix, _ProfileResultStr, suffix)
  537. result = globalProfileResult[0]
  538. del builtins.__dict__['globalProfileFunc']
  539. del builtins.__dict__['globalProfileResult']
  540. return result
  541. def profiled(category=None, terse=False):
  542. """ decorator for profiling functions
  543. turn categories on and off via "want-profile-categoryName 1"
  544. e.g.::
  545. @profiled('particles')
  546. def loadParticles():
  547. ...
  548. ::
  549. want-profile-particles 1
  550. """
  551. assert type(category) in (str, type(None)), "must provide a category name for @profiled"
  552. # allow profiling in published versions
  553. #try:
  554. # null = not __dev__
  555. #except:
  556. # null = not __debug__
  557. #if null:
  558. # # if we're not in __dev__, just return the function itself. This
  559. # # results in zero runtime overhead, since decorators are evaluated
  560. # # at module-load.
  561. # def nullDecorator(f):
  562. # return f
  563. # return nullDecorator
  564. def profileDecorator(f):
  565. def _profiled(*args, **kArgs):
  566. name = '(%s) %s from %s' % (category, f.__name__, f.__module__)
  567. if category is None or ConfigVariableBool('want-profile-%s' % category, False).value:
  568. return profileFunc(Functor(f, *args, **kArgs), name, terse)
  569. else:
  570. return f(*args, **kArgs)
  571. _profiled.__doc__ = f.__doc__
  572. return _profiled
  573. return profileDecorator
  574. # intercept profile-related file operations to avoid disk access
  575. movedOpenFuncs = []
  576. movedDumpFuncs = []
  577. movedLoadFuncs = []
  578. profileFilenames = set()
  579. profileFilenameList = Stack()
  580. profileFilename2file = {}
  581. profileFilename2marshalData = {}
  582. def _profileOpen(filename, *args, **kArgs):
  583. # this is a replacement for the file open() builtin function
  584. # for use during profiling, to intercept the file open
  585. # operation used by the Python profiler and profile stats
  586. # systems
  587. if filename in profileFilenames:
  588. # if this is a file related to profiling, create an
  589. # in-RAM file object
  590. if filename not in profileFilename2file:
  591. file = StringIO()
  592. file._profFilename = filename
  593. profileFilename2file[filename] = file
  594. else:
  595. file = profileFilename2file[filename]
  596. else:
  597. file = movedOpenFuncs[-1](filename, *args, **kArgs)
  598. return file
  599. def _profileMarshalDump(data, file):
  600. # marshal.dump doesn't work with StringIO objects
  601. # simulate it
  602. if isinstance(file, StringIO) and hasattr(file, '_profFilename'):
  603. if file._profFilename in profileFilenames:
  604. profileFilename2marshalData[file._profFilename] = data
  605. return None
  606. return movedDumpFuncs[-1](data, file)
  607. def _profileMarshalLoad(file):
  608. # marshal.load doesn't work with StringIO objects
  609. # simulate it
  610. if isinstance(file, StringIO) and hasattr(file, '_profFilename'):
  611. if file._profFilename in profileFilenames:
  612. return profileFilename2marshalData[file._profFilename]
  613. return movedLoadFuncs[-1](file)
  614. def _installProfileCustomFuncs(filename):
  615. assert filename not in profileFilenames
  616. profileFilenames.add(filename)
  617. profileFilenameList.push(filename)
  618. movedOpenFuncs.append(builtins.open)
  619. builtins.open = _profileOpen
  620. movedDumpFuncs.append(marshal.dump)
  621. marshal.dump = _profileMarshalDump
  622. movedLoadFuncs.append(marshal.load)
  623. marshal.load = _profileMarshalLoad
  624. def _getProfileResultFileInfo(filename):
  625. return (profileFilename2file.get(filename, None),
  626. profileFilename2marshalData.get(filename, None))
  627. def _setProfileResultsFileInfo(filename, info):
  628. f, m = info
  629. if f:
  630. profileFilename2file[filename] = f
  631. if m:
  632. profileFilename2marshalData[filename] = m
  633. def _clearProfileResultFileInfo(filename):
  634. profileFilename2file.pop(filename, None)
  635. profileFilename2marshalData.pop(filename, None)
  636. def _removeProfileCustomFuncs(filename):
  637. assert profileFilenameList.top() == filename
  638. marshal.load = movedLoadFuncs.pop()
  639. marshal.dump = movedDumpFuncs.pop()
  640. builtins.open = movedOpenFuncs.pop()
  641. profileFilenames.remove(filename)
  642. profileFilenameList.pop()
  643. profileFilename2file.pop(filename, None)
  644. # don't let marshalled data pile up
  645. profileFilename2marshalData.pop(filename, None)
  646. # call this from the prompt, and break back out to the prompt
  647. # to stop profiling
  648. #
  649. # OR to do inline profiling, you must make a globally-visible
  650. # function to be profiled, i.e. to profile 'self.load()', do
  651. # something like this:
  652. #
  653. # def func(self=self):
  654. # self.load()
  655. # import builtins
  656. # builtins.func = func
  657. # PythonUtil.startProfile(cmd='func()', filename='profileData')
  658. # del builtins.func
  659. #
  660. def _profileWithoutGarbageLeak(cmd, filename):
  661. # The profile module isn't necessarily installed on every Python
  662. # installation, so we import it here, instead of in the module
  663. # scope.
  664. import profile
  665. # this is necessary because the profile module creates a memory leak
  666. Profile = profile.Profile
  667. statement = cmd
  668. sort = -1
  669. #### COPIED FROM profile.run ####
  670. prof = Profile()
  671. try:
  672. prof = prof.run(statement)
  673. except SystemExit:
  674. pass
  675. if filename is not None:
  676. prof.dump_stats(filename)
  677. else:
  678. #return prof.print_stats(sort) #DCR
  679. prof.print_stats(sort) #DCR
  680. #################################
  681. # eliminate the garbage leak
  682. del prof.dispatcher
  683. def startProfile(filename=PyUtilProfileDefaultFilename,
  684. lines=PyUtilProfileDefaultLines,
  685. sorts=PyUtilProfileDefaultSorts,
  686. silent=0,
  687. callInfo=1,
  688. useDisk=False,
  689. cmd='run()'):
  690. # uniquify the filename to allow multiple processes to profile simultaneously
  691. filename = '%s.%s%s' % (filename, randUint31(), randUint31())
  692. if not useDisk:
  693. # use a RAM file
  694. _installProfileCustomFuncs(filename)
  695. _profileWithoutGarbageLeak(cmd, filename)
  696. if silent:
  697. extractProfile(filename, lines, sorts, callInfo)
  698. else:
  699. printProfile(filename, lines, sorts, callInfo)
  700. if not useDisk:
  701. # discard the RAM file
  702. _removeProfileCustomFuncs(filename)
  703. else:
  704. os.remove(filename)
  705. # call these to see the results again, as a string or in the log
  706. def printProfile(filename=PyUtilProfileDefaultFilename,
  707. lines=PyUtilProfileDefaultLines,
  708. sorts=PyUtilProfileDefaultSorts,
  709. callInfo=1):
  710. import pstats
  711. s = pstats.Stats(filename)
  712. s.strip_dirs()
  713. for sort in sorts:
  714. s.sort_stats(sort)
  715. s.print_stats(lines)
  716. if callInfo:
  717. s.print_callees(lines)
  718. s.print_callers(lines)
  719. # same args as printProfile
  720. def extractProfile(*args, **kArgs):
  721. global _ProfileResultStr
  722. # capture print output
  723. sc = StdoutCapture()
  724. # print the profile output, redirected to the result string
  725. printProfile(*args, **kArgs)
  726. # make a copy of the print output
  727. _ProfileResultStr = sc.getString()
  728. # restore stdout to what it was before
  729. sc.destroy()
  730. def getSetterName(valueName, prefix='set'):
  731. # getSetterName('color') -> 'setColor'
  732. # getSetterName('color', 'get') -> 'getColor'
  733. return '%s%s%s' % (prefix, valueName[0].upper(), valueName[1:])
  734. def getSetter(targetObj, valueName, prefix='set'):
  735. # getSetter(smiley, 'pos') -> smiley.setPos
  736. return getattr(targetObj, getSetterName(valueName, prefix))
  737. def mostDerivedLast(classList):
  738. """pass in list of classes. sorts list in-place, with derived classes
  739. appearing after their bases"""
  740. class ClassSortKey(object):
  741. __slots__ = 'classobj',
  742. def __init__(self, classobj):
  743. self.classobj = classobj
  744. def __lt__(self, other):
  745. return issubclass(other.classobj, self.classobj)
  746. classList.sort(key=ClassSortKey)
  747. def bound(value, bound1, bound2):
  748. """
  749. returns value if value is between bound1 and bound2
  750. otherwise returns bound that is closer to value
  751. """
  752. if bound1 > bound2:
  753. return min(max(value, bound2), bound1)
  754. else:
  755. return min(max(value, bound1), bound2)
  756. clamp = bound
  757. def lerp(v0, v1, t):
  758. """
  759. returns a value lerped between v0 and v1, according to t
  760. t == 0 maps to v0, t == 1 maps to v1
  761. """
  762. return v0 + ((v1 - v0) * t)
  763. def getShortestRotation(start, end):
  764. """
  765. Given two heading values, return a tuple describing
  766. the shortest interval from 'start' to 'end'. This tuple
  767. can be used to lerp a camera between two rotations
  768. while avoiding the 'spin' problem.
  769. """
  770. start, end = start % 360, end % 360
  771. if abs(end - start) > 180:
  772. if end < start:
  773. end += 360
  774. else:
  775. start += 360
  776. return (start, end)
  777. def average(*args):
  778. """ returns simple average of list of values """
  779. val = 0.
  780. for arg in args:
  781. val += arg
  782. return val / len(args)
  783. class Averager:
  784. def __init__(self, name):
  785. self._name = name
  786. self.reset()
  787. def reset(self):
  788. self._total = 0.
  789. self._count = 0
  790. def addValue(self, value):
  791. self._total += value
  792. self._count += 1
  793. def getAverage(self):
  794. return self._total / self._count
  795. def getCount(self):
  796. return self._count
  797. def addListsByValue(a, b):
  798. """
  799. returns a new array containing the sums of the two array arguments
  800. (c[0] = a[0 + b[0], etc.)
  801. """
  802. c = []
  803. for x, y in zip(a, b):
  804. c.append(x + y)
  805. return c
  806. def boolEqual(a, b):
  807. """
  808. returns true if a and b are both true or both false.
  809. returns false otherwise
  810. (a.k.a. xnor -- eXclusive Not OR).
  811. """
  812. return (a and b) or not (a or b)
  813. def lineupPos(i, num, spacing):
  814. """
  815. use to line up a series of 'num' objects, in one dimension,
  816. centered around zero
  817. 'i' is the index of the object in the lineup
  818. 'spacing' is the amount of space between objects in the lineup
  819. """
  820. assert num >= 1
  821. assert i >= 0 and i < num
  822. pos = float(i) * spacing
  823. return pos - ((float(spacing) * (num-1))/2.)
  824. def formatElapsedSeconds(seconds):
  825. """
  826. Returns a string of the form "mm:ss" or "hh:mm:ss" or "n days",
  827. representing the indicated elapsed time in seconds.
  828. """
  829. sign = ''
  830. if seconds < 0:
  831. seconds = -seconds
  832. sign = '-'
  833. # We use math.floor() instead of casting to an int, so we avoid
  834. # problems with numbers that are too large to represent as
  835. # type int.
  836. seconds = math.floor(seconds)
  837. hours = math.floor(seconds / (60 * 60))
  838. if hours > 36:
  839. days = math.floor((hours + 12) / 24)
  840. return "%s%d days" % (sign, days)
  841. seconds -= hours * (60 * 60)
  842. minutes = (int)(seconds / 60)
  843. seconds -= minutes * 60
  844. if hours != 0:
  845. return "%s%d:%02d:%02d" % (sign, hours, minutes, seconds)
  846. else:
  847. return "%s%d:%02d" % (sign, minutes, seconds)
  848. def solveQuadratic(a, b, c):
  849. # quadratic equation: ax^2 + bx + c = 0
  850. # quadratic formula: x = [-b +/- sqrt(b^2 - 4ac)] / 2a
  851. # returns None, root, or [root1, root2]
  852. # a cannot be zero.
  853. if a == 0.:
  854. return None
  855. # calculate the determinant (b^2 - 4ac)
  856. D = (b * b) - (4. * a * c)
  857. if D < 0:
  858. # there are no solutions (sqrt(negative number) is undefined)
  859. return None
  860. elif D == 0:
  861. # only one root
  862. return (-b) / (2. * a)
  863. else:
  864. # OK, there are two roots
  865. sqrtD = math.sqrt(D)
  866. twoA = 2. * a
  867. root1 = ((-b) - sqrtD) / twoA
  868. root2 = ((-b) + sqrtD) / twoA
  869. return [root1, root2]
  870. if __debug__:
  871. def stackEntryInfo(depth=0, baseFileName=1):
  872. """
  873. returns the sourcefilename, line number, and function name of
  874. an entry in the stack.
  875. 'depth' is how far back to go in the stack; 0 is the caller of this
  876. function, 1 is the function that called the caller of this function, etc.
  877. by default, strips off the path of the filename; override with baseFileName
  878. returns (fileName, lineNum, funcName) --> (string, int, string)
  879. returns (None, None, None) on error
  880. """
  881. import inspect
  882. try:
  883. stack = None
  884. frame = None
  885. try:
  886. stack = inspect.stack()
  887. # add one to skip the frame associated with this function
  888. frame = stack[depth+1]
  889. filename = frame[1]
  890. if baseFileName:
  891. filename = os.path.basename(filename)
  892. lineNum = frame[2]
  893. funcName = frame[3]
  894. result = (filename, lineNum, funcName)
  895. finally:
  896. del stack
  897. del frame
  898. except:
  899. result = (None, None, None)
  900. return result
  901. def lineInfo(baseFileName=1):
  902. """
  903. returns the sourcefilename, line number, and function name of the
  904. code that called this function
  905. (answers the question: 'hey lineInfo, where am I in the codebase?')
  906. see stackEntryInfo, above, for info on 'baseFileName' and return types
  907. """
  908. return stackEntryInfo(1, baseFileName)
  909. def callerInfo(baseFileName=1, howFarBack=0):
  910. """
  911. returns the sourcefilename, line number, and function name of the
  912. caller of the function that called this function
  913. (answers the question: 'hey callerInfo, who called me?')
  914. see stackEntryInfo, above, for info on 'baseFileName' and return types
  915. """
  916. return stackEntryInfo(2+howFarBack, baseFileName)
  917. def lineTag(baseFileName=1, verbose=0, separator=':'):
  918. """
  919. returns a string containing the sourcefilename and line number
  920. of the code that called this function
  921. (equivalent to lineInfo, above, with different return type)
  922. see stackEntryInfo, above, for info on 'baseFileName'
  923. if 'verbose' is false, returns a compact string of the form
  924. 'fileName:lineNum:funcName'
  925. if 'verbose' is true, returns a longer string that matches the
  926. format of Python stack trace dumps
  927. returns empty string on error
  928. """
  929. fileName, lineNum, funcName = callerInfo(baseFileName)
  930. if fileName is None:
  931. return ''
  932. if verbose:
  933. return 'File "%s", line %s, in %s' % (fileName, lineNum, funcName)
  934. else:
  935. return '%s%s%s%s%s' % (fileName, separator, lineNum, separator,
  936. funcName)
  937. def findPythonModule(module):
  938. # Look along the python load path for the indicated filename.
  939. # Returns the located pathname, or None if the filename is not
  940. # found.
  941. filename = module + '.py'
  942. for dir in sys.path:
  943. pathname = os.path.join(dir, filename)
  944. if os.path.exists(pathname):
  945. return pathname
  946. return None
  947. def clampScalar(value, a, b):
  948. # calling this ought to be faster than calling both min and max
  949. if a < b:
  950. if value < a:
  951. return a
  952. elif value > b:
  953. return b
  954. else:
  955. return value
  956. else:
  957. if value < b:
  958. return b
  959. elif value > a:
  960. return a
  961. else:
  962. return value
  963. def weightedChoice(choiceList, rng=random.random, sum=None):
  964. """given a list of (weight, item) pairs, chooses an item based on the
  965. weights. rng must return 0..1. if you happen to have the sum of the
  966. weights, pass it in 'sum'."""
  967. # Throw an IndexError if we got an empty list.
  968. if not choiceList:
  969. raise IndexError('Cannot choose from an empty sequence')
  970. # TODO: add support for dicts
  971. if sum is None:
  972. sum = 0.
  973. for weight, item in choiceList:
  974. sum += weight
  975. rand = rng()
  976. accum = rand * sum
  977. item = None
  978. for weight, item in choiceList:
  979. accum -= weight
  980. if accum <= 0.:
  981. return item
  982. # rand is ~1., and floating-point error prevented accum from hitting 0.
  983. # Or you passed in a 'sum' that was was too large.
  984. # Return the last item.
  985. return item
  986. def randFloat(a, b=0., rng=random.random):
  987. """returns a random float in [a, b]
  988. call with single argument to generate random float between arg and zero
  989. """
  990. return lerp(a, b, rng())
  991. def normalDistrib(a, b, gauss=random.gauss):
  992. """
  993. NOTE: assumes a < b
  994. Returns random number between a and b, using gaussian distribution, with
  995. mean=avg(a, b), and a standard deviation that fits ~99.7% of the curve
  996. between a and b.
  997. For ease of use, outlying results are re-computed until result is in [a, b]
  998. This should fit the remaining .3% of the curve that lies outside [a, b]
  999. uniformly onto the curve inside [a, b]
  1000. ------------------------------------------------------------------------
  1001. The 68-95-99.7% Rule
  1002. ====================
  1003. All normal density curves satisfy the following property which is often
  1004. referred to as the Empirical Rule:
  1005. 68% of the observations fall within 1 standard deviation of the mean.
  1006. 95% of the observations fall within 2 standard deviations of the mean.
  1007. 99.7% of the observations fall within 3 standard deviations of the mean.
  1008. Thus, for a normal distribution, almost all values lie within 3 standard
  1009. deviations of the mean.
  1010. ------------------------------------------------------------------------
  1011. In calculating our standard deviation, we divide (b-a) by 6, since the
  1012. 99.7% figure includes 3 standard deviations _on_either_side_ of the mean.
  1013. """
  1014. while True:
  1015. r = gauss((a+b)*.5, (b-a)/6.)
  1016. if (r >= a) and (r <= b):
  1017. return r
  1018. def weightedRand(valDict, rng=random.random):
  1019. """
  1020. pass in a dictionary with a selection -> weight mapping. E.g.::
  1021. {"Choice 1": 10,
  1022. "Choice 2": 30,
  1023. "bear": 100}
  1024. - Weights need not add up to any particular value.
  1025. - The actual selection will be returned.
  1026. """
  1027. selections = list(valDict.keys())
  1028. weights = list(valDict.values())
  1029. totalWeight = 0
  1030. for weight in weights:
  1031. totalWeight += weight
  1032. # get a random value between 0 and the total of the weights
  1033. randomWeight = rng() * totalWeight
  1034. # find the index that corresponds with this weight
  1035. for i in range(len(weights)):
  1036. totalWeight -= weights[i]
  1037. if totalWeight <= randomWeight:
  1038. return selections[i]
  1039. assert True, "Should never get here"
  1040. return selections[-1]
  1041. def randUint31(rng=random.random):
  1042. """returns a random integer in [0..2^31).
  1043. rng must return float in [0..1]"""
  1044. return int(rng() * 0x7FFFFFFF)
  1045. def randInt32(rng=random.random):
  1046. """returns a random integer in [-2147483648..2147483647].
  1047. rng must return float in [0..1]
  1048. """
  1049. i = int(rng() * 0x7FFFFFFF)
  1050. if rng() < .5:
  1051. i *= -1
  1052. return i
  1053. class SerialNumGen:
  1054. """generates serial numbers"""
  1055. def __init__(self, start=None):
  1056. if start is None:
  1057. start = 0
  1058. self.__counter = start-1
  1059. def next(self):
  1060. self.__counter += 1
  1061. return self.__counter
  1062. __next__ = next
  1063. class SerialMaskedGen(SerialNumGen):
  1064. def __init__(self, mask, start=None):
  1065. self._mask = mask
  1066. SerialNumGen.__init__(self, start)
  1067. def next(self):
  1068. v = SerialNumGen.next(self)
  1069. return v & self._mask
  1070. __next__ = next
  1071. _serialGen = SerialNumGen()
  1072. def serialNum():
  1073. global _serialGen
  1074. return _serialGen.next()
  1075. def uniqueName(name):
  1076. global _serialGen
  1077. return '%s-%s' % (name, _serialGen.next())
  1078. class EnumIter:
  1079. def __init__(self, enum):
  1080. self._values = tuple(enum._stringTable.keys())
  1081. self._index = 0
  1082. def __iter__(self):
  1083. return self
  1084. def __next__(self):
  1085. if self._index >= len(self._values):
  1086. raise StopIteration
  1087. self._index += 1
  1088. return self._values[self._index-1]
  1089. class Enum:
  1090. """Pass in list of strings or string of comma-separated strings.
  1091. Items are accessible as instance.item, and are assigned unique,
  1092. increasing integer values. Pass in integer for 'start' to override
  1093. starting value.
  1094. Example:
  1095. >>> colors = Enum('red, green, blue')
  1096. >>> colors.red
  1097. 0
  1098. >>> colors.green
  1099. 1
  1100. >>> colors.blue
  1101. 2
  1102. >>> colors.getString(colors.red)
  1103. 'red'
  1104. """
  1105. if __debug__:
  1106. # chars that cannot appear within an item string.
  1107. def _checkValidIdentifier(item):
  1108. import string
  1109. invalidChars = string.whitespace + string.punctuation
  1110. invalidChars = invalidChars.replace('_', '')
  1111. invalidFirstChars = invalidChars+string.digits
  1112. if item[0] in invalidFirstChars:
  1113. raise SyntaxError("Enum '%s' contains invalid first char" %
  1114. item)
  1115. if not disjoint(item, invalidChars):
  1116. for char in item:
  1117. if char in invalidChars:
  1118. raise SyntaxError(
  1119. "Enum\n'%s'\ncontains illegal char '%s'" %
  1120. (item, char))
  1121. return 1
  1122. _checkValidIdentifier = staticmethod(_checkValidIdentifier)
  1123. def __init__(self, items, start=0):
  1124. if isinstance(items, str):
  1125. items = items.split(',')
  1126. self._stringTable = {}
  1127. # make sure we don't overwrite an existing element of the class
  1128. assert self._checkExistingMembers(items)
  1129. assert uniqueElements(items)
  1130. i = start
  1131. for item in items:
  1132. # remove leading/trailing whitespace
  1133. item = item.strip()
  1134. # is there anything left?
  1135. if len(item) == 0:
  1136. continue
  1137. # make sure there are no invalid characters
  1138. assert Enum._checkValidIdentifier(item)
  1139. self.__dict__[item] = i
  1140. self._stringTable[i] = item
  1141. i += 1
  1142. def __iter__(self):
  1143. return EnumIter(self)
  1144. def hasString(self, string):
  1145. return string in set(self._stringTable.values())
  1146. def fromString(self, string):
  1147. if self.hasString(string):
  1148. return self.__dict__[string]
  1149. # throw an error
  1150. {}[string]
  1151. def getString(self, value):
  1152. return self._stringTable[value]
  1153. def __contains__(self, value):
  1154. return value in self._stringTable
  1155. def __len__(self):
  1156. return len(self._stringTable)
  1157. def copyTo(self, obj):
  1158. # copies all members onto obj
  1159. for name, value in self._stringTable:
  1160. setattr(obj, name, value)
  1161. if __debug__:
  1162. def _checkExistingMembers(self, items):
  1163. for item in items:
  1164. if hasattr(self, item):
  1165. return 0
  1166. return 1
  1167. ############################################################
  1168. # class: Singleton
  1169. # Purpose: This provides a base metaclass for all classes
  1170. # that require one and only one instance.
  1171. #
  1172. # Example: class mySingleton:
  1173. # __metaclass__ = PythonUtil.Singleton
  1174. # def __init__(self, ...):
  1175. # ...
  1176. #
  1177. # Note: This class is based on Python's New-Style Class
  1178. # design. An error will occur if a defined class
  1179. # attemps to inherit from a Classic-Style Class only,
  1180. # ie: class myClassX:
  1181. # def __init__(self, ...):
  1182. # ...
  1183. #
  1184. # class myNewClassX(myClassX):
  1185. # __metaclass__ = PythonUtil.Singleton
  1186. # def __init__(self, ...):
  1187. # myClassX.__init__(self, ...)
  1188. # ...
  1189. #
  1190. # This causes problems because myNewClassX is a
  1191. # New-Style class that inherits from only a
  1192. # Classic-Style base class. There are two ways
  1193. # simple ways to resolve this issue.
  1194. #
  1195. # First, if possible, make myClassX a
  1196. # New-Style class by inheriting from object
  1197. # object. IE: class myClassX(object):
  1198. #
  1199. # If for some reason that is not an option, make
  1200. # myNewClassX inherit from object and myClassX.
  1201. # IE: class myNewClassX(object, myClassX):
  1202. ############################################################
  1203. class Singleton(type):
  1204. def __init__(cls, name, bases, dic):
  1205. super(Singleton, cls).__init__(name, bases, dic)
  1206. cls.instance=None
  1207. def __call__(cls, *args, **kw):
  1208. if cls.instance is None:
  1209. cls.instance=super(Singleton, cls).__call__(*args, **kw)
  1210. return cls.instance
  1211. class SingletonError(ValueError):
  1212. """ Used to indicate an inappropriate value for a Singleton."""
  1213. def printListEnumGen(l):
  1214. # log each individual item with a number in front of it
  1215. digits = 0
  1216. n = len(l)
  1217. while n > 0:
  1218. digits += 1
  1219. n //= 10
  1220. format = '%0' + '%s' % digits + 'i:%s'
  1221. for i in range(len(l)):
  1222. print(format % (i, l[i]))
  1223. yield None
  1224. def printListEnum(l):
  1225. for result in printListEnumGen(l):
  1226. pass
  1227. # base class for all Panda C++ objects
  1228. # libdtoolconfig doesn't seem to have this, grab it off of TypedObject
  1229. dtoolSuperBase = None
  1230. def _getDtoolSuperBase():
  1231. global dtoolSuperBase
  1232. from panda3d.core import TypedObject
  1233. dtoolSuperBase = TypedObject.__bases__[0]
  1234. assert dtoolSuperBase.__name__ == 'DTOOL_SUPER_BASE'
  1235. safeReprNotify = None
  1236. def _getSafeReprNotify():
  1237. global safeReprNotify
  1238. from direct.directnotify.DirectNotifyGlobal import directNotify
  1239. safeReprNotify = directNotify.newCategory("safeRepr")
  1240. return safeReprNotify
  1241. def safeRepr(obj):
  1242. global dtoolSuperBase
  1243. if dtoolSuperBase is None:
  1244. _getDtoolSuperBase()
  1245. global safeReprNotify
  1246. if safeReprNotify is None:
  1247. _getSafeReprNotify()
  1248. if isinstance(obj, dtoolSuperBase):
  1249. # repr of C++ object could crash, particularly if the object has been deleted
  1250. # log that we're calling repr
  1251. safeReprNotify.info('calling repr on instance of %s.%s' % (obj.__class__.__module__, obj.__class__.__name__))
  1252. sys.stdout.flush()
  1253. try:
  1254. return repr(obj)
  1255. except:
  1256. return '<** FAILED REPR OF %s instance at %s **>' % (obj.__class__.__name__, hex(id(obj)))
  1257. def safeReprTypeOnFail(obj):
  1258. global dtoolSuperBase
  1259. if dtoolSuperBase is None:
  1260. _getDtoolSuperBase()
  1261. global safeReprNotify
  1262. if safeReprNotify is None:
  1263. _getSafeReprNotify()
  1264. if isinstance(obj, dtoolSuperBase):
  1265. return type(obj)
  1266. try:
  1267. return repr(obj)
  1268. except:
  1269. return '<** FAILED REPR OF %s instance at %s **>' % (obj.__class__.__name__, hex(id(obj)))
  1270. def fastRepr(obj, maxLen=200, strFactor=10, _visitedIds=None):
  1271. """ caps the length of iterable types, so very large objects will print faster.
  1272. also prevents infinite recursion """
  1273. try:
  1274. if _visitedIds is None:
  1275. _visitedIds = set()
  1276. if id(obj) in _visitedIds:
  1277. return '<ALREADY-VISITED %s>' % itype(obj)
  1278. if type(obj) in (tuple, list):
  1279. s = ''
  1280. s += {tuple: '(',
  1281. list: '[',}[type(obj)]
  1282. if maxLen is not None and len(obj) > maxLen:
  1283. o = obj[:maxLen]
  1284. ellips = '...'
  1285. else:
  1286. o = obj
  1287. ellips = ''
  1288. _visitedIds.add(id(obj))
  1289. for item in o:
  1290. s += fastRepr(item, maxLen, _visitedIds=_visitedIds)
  1291. s += ', '
  1292. _visitedIds.remove(id(obj))
  1293. s += ellips
  1294. s += {tuple: ')',
  1295. list: ']',}[type(obj)]
  1296. return s
  1297. elif type(obj) is dict:
  1298. s = '{'
  1299. if maxLen is not None and len(obj) > maxLen:
  1300. o = list(obj.keys())[:maxLen]
  1301. ellips = '...'
  1302. else:
  1303. o = list(obj.keys())
  1304. ellips = ''
  1305. _visitedIds.add(id(obj))
  1306. for key in o:
  1307. value = obj[key]
  1308. s += '%s: %s, ' % (fastRepr(key, maxLen, _visitedIds=_visitedIds),
  1309. fastRepr(value, maxLen, _visitedIds=_visitedIds))
  1310. _visitedIds.remove(id(obj))
  1311. s += ellips
  1312. s += '}'
  1313. return s
  1314. elif type(obj) is str:
  1315. if maxLen is not None:
  1316. maxLen *= strFactor
  1317. if maxLen is not None and len(obj) > maxLen:
  1318. return safeRepr(obj[:maxLen])
  1319. else:
  1320. return safeRepr(obj)
  1321. else:
  1322. r = safeRepr(obj)
  1323. maxLen *= strFactor
  1324. if len(r) > maxLen:
  1325. r = r[:maxLen]
  1326. return r
  1327. except:
  1328. return '<** FAILED REPR OF %s **>' % obj.__class__.__name__
  1329. def convertTree(objTree, idList):
  1330. newTree = {}
  1331. for key in list(objTree.keys()):
  1332. obj = (idList[key],)
  1333. newTree[obj] = {}
  1334. r_convertTree(objTree[key], newTree[obj], idList)
  1335. return newTree
  1336. def r_convertTree(oldTree, newTree, idList):
  1337. for key in list(oldTree.keys()):
  1338. obj = idList.get(key)
  1339. if not obj:
  1340. continue
  1341. obj = str(obj)[:100]
  1342. newTree[obj] = {}
  1343. r_convertTree(oldTree[key], newTree[obj], idList)
  1344. def pretty_print(tree):
  1345. for name in tree.keys():
  1346. print(name)
  1347. r_pretty_print(tree[name], 0)
  1348. def r_pretty_print(tree, num):
  1349. num += 1
  1350. for name in tree.keys():
  1351. print(" " * num, name)
  1352. r_pretty_print(tree[name], num)
  1353. def isDefaultValue(x):
  1354. return x == type(x)()
  1355. def appendStr(obj, st):
  1356. """adds a string onto the __str__ output of an instance"""
  1357. def appendedStr(oldStr, st, self):
  1358. return oldStr() + st
  1359. oldStr = getattr(obj, '__str__', None)
  1360. if oldStr is None:
  1361. def stringer(s):
  1362. return s
  1363. oldStr = Functor(stringer, str(obj))
  1364. stringer = None
  1365. obj.__str__ = types.MethodType(Functor(appendedStr, oldStr, st), obj)
  1366. appendedStr = None
  1367. return obj
  1368. class ScratchPad:
  1369. """empty class to stick values onto"""
  1370. def __init__(self, **kArgs):
  1371. for key, value in kArgs.items():
  1372. setattr(self, key, value)
  1373. self._keys = set(kArgs.keys())
  1374. def add(self, **kArgs):
  1375. for key, value in kArgs.items():
  1376. setattr(self, key, value)
  1377. self._keys.update(list(kArgs.keys()))
  1378. def destroy(self):
  1379. for key in self._keys:
  1380. delattr(self, key)
  1381. # allow dict [] syntax
  1382. def __getitem__(self, itemName):
  1383. return getattr(self, itemName)
  1384. def get(self, itemName, default=None):
  1385. return getattr(self, itemName, default)
  1386. # allow 'in'
  1387. def __contains__(self, itemName):
  1388. return itemName in self._keys
  1389. class Sync:
  1390. _SeriesGen = SerialNumGen()
  1391. def __init__(self, name, other=None):
  1392. self._name = name
  1393. if other is None:
  1394. self._series = self._SeriesGen.next()
  1395. self._value = 0
  1396. else:
  1397. self._series = other._series
  1398. self._value = other._value
  1399. def invalidate(self):
  1400. self._value = None
  1401. def change(self):
  1402. self._value += 1
  1403. def sync(self, other):
  1404. if (self._series != other._series) or (self._value != other._value):
  1405. self._series = other._series
  1406. self._value = other._value
  1407. return True
  1408. else:
  1409. return False
  1410. def isSynced(self, other):
  1411. return ((self._series == other._series) and
  1412. (self._value == other._value))
  1413. def __repr__(self):
  1414. return '%s(%s)<family=%s,value=%s>' % (self.__class__.__name__,
  1415. self._name, self._series, self._value)
  1416. def itype(obj):
  1417. # version of type that gives more complete information about instance types
  1418. global dtoolSuperBase
  1419. t = type(obj)
  1420. # C++ object instances appear to be types via type()
  1421. # check if this is a C++ object
  1422. if dtoolSuperBase is None:
  1423. _getDtoolSuperBase()
  1424. if isinstance(obj, dtoolSuperBase):
  1425. return "<type 'instance' of %s>" % (obj.__class__)
  1426. return t
  1427. def deeptype(obj, maxLen=100, _visitedIds=None):
  1428. if _visitedIds is None:
  1429. _visitedIds = set()
  1430. if id(obj) in _visitedIds:
  1431. return '<ALREADY-VISITED %s>' % itype(obj)
  1432. t = type(obj)
  1433. if t in (tuple, list):
  1434. s = ''
  1435. s += {tuple: '(',
  1436. list: '[',}[type(obj)]
  1437. if maxLen is not None and len(obj) > maxLen:
  1438. o = obj[:maxLen]
  1439. ellips = '...'
  1440. else:
  1441. o = obj
  1442. ellips = ''
  1443. _visitedIds.add(id(obj))
  1444. for item in o:
  1445. s += deeptype(item, maxLen, _visitedIds=_visitedIds)
  1446. s += ', '
  1447. _visitedIds.remove(id(obj))
  1448. s += ellips
  1449. s += {tuple: ')',
  1450. list: ']',}[type(obj)]
  1451. return s
  1452. elif type(obj) is dict:
  1453. s = '{'
  1454. if maxLen is not None and len(obj) > maxLen:
  1455. o = list(obj.keys())[:maxLen]
  1456. ellips = '...'
  1457. else:
  1458. o = list(obj.keys())
  1459. ellips = ''
  1460. _visitedIds.add(id(obj))
  1461. for key in o:
  1462. value = obj[key]
  1463. s += '%s: %s, ' % (deeptype(key, maxLen, _visitedIds=_visitedIds),
  1464. deeptype(value, maxLen, _visitedIds=_visitedIds))
  1465. _visitedIds.remove(id(obj))
  1466. s += ellips
  1467. s += '}'
  1468. return s
  1469. else:
  1470. return str(itype(obj))
  1471. def getNumberedTypedString(items, maxLen=5000, numPrefix=''):
  1472. """get a string that has each item of the list on its own line,
  1473. and each item is numbered on the left from zero"""
  1474. digits = 0
  1475. n = len(items)
  1476. while n > 0:
  1477. digits += 1
  1478. n //= 10
  1479. format = numPrefix + '%0' + '%s' % digits + 'i:%s \t%s'
  1480. first = True
  1481. s = ''
  1482. snip = '<SNIP>'
  1483. for i in range(len(items)):
  1484. if not first:
  1485. s += '\n'
  1486. first = False
  1487. objStr = fastRepr(items[i])
  1488. if len(objStr) > maxLen:
  1489. objStr = '%s%s' % (objStr[:(maxLen-len(snip))], snip)
  1490. s += format % (i, itype(items[i]), objStr)
  1491. return s
  1492. def getNumberedTypedSortedString(items, maxLen=5000, numPrefix=''):
  1493. """get a string that has each item of the list on its own line,
  1494. the items are stringwise-sorted, and each item is numbered on
  1495. the left from zero"""
  1496. digits = 0
  1497. n = len(items)
  1498. while n > 0:
  1499. digits += 1
  1500. n //= 10
  1501. format = numPrefix + '%0' + '%s' % digits + 'i:%s \t%s'
  1502. snip = '<SNIP>'
  1503. strs = []
  1504. for item in items:
  1505. objStr = fastRepr(item)
  1506. if len(objStr) > maxLen:
  1507. objStr = '%s%s' % (objStr[:(maxLen-len(snip))], snip)
  1508. strs.append(objStr)
  1509. first = True
  1510. s = ''
  1511. strs.sort()
  1512. for i in range(len(strs)):
  1513. if not first:
  1514. s += '\n'
  1515. first = False
  1516. objStr = strs[i]
  1517. s += format % (i, itype(items[i]), strs[i])
  1518. return s
  1519. def printNumberedTyped(items, maxLen=5000):
  1520. """print out each item of the list on its own line,
  1521. with each item numbered on the left from zero"""
  1522. digits = 0
  1523. n = len(items)
  1524. while n > 0:
  1525. digits += 1
  1526. n //= 10
  1527. format = '%0' + '%s' % digits + 'i:%s \t%s'
  1528. for i in range(len(items)):
  1529. objStr = fastRepr(items[i])
  1530. if len(objStr) > maxLen:
  1531. snip = '<SNIP>'
  1532. objStr = '%s%s' % (objStr[:(maxLen-len(snip))], snip)
  1533. print(format % (i, itype(items[i]), objStr))
  1534. def printNumberedTypesGen(items, maxLen=5000):
  1535. digits = 0
  1536. n = len(items)
  1537. while n > 0:
  1538. digits += 1
  1539. n //= 10
  1540. format = '%0' + '%s' % digits + 'i:%s'
  1541. for i in range(len(items)):
  1542. print(format % (i, itype(items[i])))
  1543. yield None
  1544. def printNumberedTypes(items, maxLen=5000):
  1545. """print out the type of each item of the list on its own line,
  1546. with each item numbered on the left from zero"""
  1547. for result in printNumberedTypesGen(items, maxLen):
  1548. yield result
  1549. class DelayedCall:
  1550. """ calls a func after a specified delay """
  1551. def __init__(self, func, name=None, delay=None):
  1552. if name is None:
  1553. name = 'anonymous'
  1554. if delay is None:
  1555. delay = .01
  1556. self._func = func
  1557. self._taskName = 'DelayedCallback-%s' % name
  1558. self._delay = delay
  1559. self._finished = False
  1560. self._addDoLater()
  1561. def destroy(self):
  1562. self._finished = True
  1563. self._removeDoLater()
  1564. def finish(self):
  1565. if not self._finished:
  1566. self._doCallback()
  1567. self.destroy()
  1568. def _addDoLater(self):
  1569. taskMgr.doMethodLater(self._delay, self._doCallback, self._taskName)
  1570. def _removeDoLater(self):
  1571. taskMgr.remove(self._taskName)
  1572. def _doCallback(self, task):
  1573. self._finished = True
  1574. func = self._func
  1575. del self._func
  1576. func()
  1577. class FrameDelayedCall:
  1578. """ calls a func after N frames """
  1579. def __init__(self, name, callback, frames=None, cancelFunc=None):
  1580. # checkFunc is optional; called every frame, if returns True, FrameDelay is cancelled
  1581. # and callback is not called
  1582. if frames is None:
  1583. frames = 1
  1584. self._name = name
  1585. self._frames = frames
  1586. self._callback = callback
  1587. self._cancelFunc = cancelFunc
  1588. self._taskName = uniqueName('%s-%s' % (self.__class__.__name__, self._name))
  1589. self._finished = False
  1590. self._startTask()
  1591. def destroy(self):
  1592. self._finished = True
  1593. self._stopTask()
  1594. def finish(self):
  1595. if not self._finished:
  1596. self._finished = True
  1597. self._callback()
  1598. self.destroy()
  1599. def _startTask(self):
  1600. taskMgr.add(self._frameTask, self._taskName)
  1601. self._counter = 0
  1602. def _stopTask(self):
  1603. taskMgr.remove(self._taskName)
  1604. def _frameTask(self, task):
  1605. if self._cancelFunc and self._cancelFunc():
  1606. self.destroy()
  1607. return task.done
  1608. self._counter += 1
  1609. if self._counter >= self._frames:
  1610. self.finish()
  1611. return task.done
  1612. return task.cont
  1613. class DelayedFunctor:
  1614. """ Waits for this object to be called, then calls supplied functor after a delay.
  1615. Effectively inserts a time delay between the caller and the functor. """
  1616. def __init__(self, functor, name=None, delay=None):
  1617. self._functor = functor
  1618. self._name = name
  1619. # FunctionInterval requires __name__
  1620. self.__name__ = self._name
  1621. self._delay = delay
  1622. def _callFunctor(self):
  1623. cb = Functor(self._functor, *self._args, **self._kwArgs)
  1624. del self._functor
  1625. del self._name
  1626. del self._delay
  1627. del self._args
  1628. del self._kwArgs
  1629. del self._delayedCall
  1630. del self.__name__
  1631. cb()
  1632. def __call__(self, *args, **kwArgs):
  1633. self._args = args
  1634. self._kwArgs = kwArgs
  1635. self._delayedCall = DelayedCall(self._callFunctor, self._name, self._delay)
  1636. class SubframeCall:
  1637. """Calls a callback at a specific time during the frame using the
  1638. task system"""
  1639. def __init__(self, functor, taskPriority, name=None):
  1640. self._functor = functor
  1641. self._name = name
  1642. self._taskName = uniqueName('SubframeCall-%s' % self._name)
  1643. taskMgr.add(self._doCallback,
  1644. self._taskName,
  1645. priority=taskPriority)
  1646. def _doCallback(self, task):
  1647. functor = self._functor
  1648. del self._functor
  1649. functor()
  1650. del self._name
  1651. self._taskName = None
  1652. return task.done
  1653. def cleanup(self):
  1654. if self._taskName:
  1655. taskMgr.remove(self._taskName)
  1656. self._taskName = None
  1657. class PStatScope:
  1658. collectors = {}
  1659. def __init__(self, level = None):
  1660. self.levels = []
  1661. if level:
  1662. self.levels.append(level)
  1663. def copy(self, push = None):
  1664. c = PStatScope()
  1665. c.levels = self.levels[:]
  1666. if push:
  1667. c.push(push)
  1668. return c
  1669. def __repr__(self):
  1670. return 'PStatScope - \'%s\'' % (self,)
  1671. def __str__(self):
  1672. return ':'.join(self.levels)
  1673. def push(self, level):
  1674. self.levels.append(level.replace('_',''))
  1675. def pop(self):
  1676. return self.levels.pop()
  1677. def start(self, push = None):
  1678. if push:
  1679. self.push(push)
  1680. self.getCollector().start()
  1681. def stop(self, pop = False):
  1682. self.getCollector().stop()
  1683. if pop:
  1684. self.pop()
  1685. def getCollector(self):
  1686. label = str(self)
  1687. if label not in self.collectors:
  1688. from panda3d.core import PStatCollector
  1689. self.collectors[label] = PStatCollector(label)
  1690. # print ' ',self.collectors[label]
  1691. return self.collectors[label]
  1692. def pstatcollect(scope, level = None):
  1693. def decorator(f):
  1694. return f
  1695. try:
  1696. if not (__dev__ or ConfigVariableBool('force-pstatcollect', False)) or \
  1697. not scope:
  1698. return decorator
  1699. def decorator(f):
  1700. def wrap(*args, **kw):
  1701. scope.start(push = (level or f.__name__))
  1702. val = f(*args, **kw)
  1703. scope.stop(pop = True)
  1704. return val
  1705. return wrap
  1706. except:
  1707. pass
  1708. return decorator
  1709. __report_indent = 0
  1710. def report(types = [], prefix = '', xform = None, notifyFunc = None, dConfigParam = []):
  1711. """
  1712. This is a decorator generating function. Use is similar to
  1713. a @decorator, except you must be sure to call it as a function.
  1714. It actually returns the decorator which is then used to transform
  1715. your decorated function. Confusing at first, I know.
  1716. Decoration occurs at function definition time.
  1717. If __dev__ is not defined, or resolves to False, this function
  1718. has no effect and no wrapping/transform occurs. So in production,
  1719. it's as if the report has been asserted out.
  1720. Parameters:
  1721. types: A subset list of ['timeStamp', 'frameCount', 'avLocation']
  1722. This allows you to specify certain useful bits of info:
  1723. - *module*: Prints the module that this report statement
  1724. can be found in.
  1725. - *args*: Prints the arguments as they were passed to this
  1726. function.
  1727. - *timeStamp*: Adds the current frame time to the output.
  1728. - *deltaStamp*: Adds the current AI synched frame time to
  1729. the output
  1730. - *frameCount*: Adds the current frame count to the output.
  1731. Usually cleaner than the timeStamp output.
  1732. - *avLocation*: Adds the localAvatar's network location to
  1733. the output. Useful for interest debugging.
  1734. - *interests*: Prints the current interest state after the
  1735. report.
  1736. - *stackTrace*: Prints a stack trace after the report.
  1737. prefix: Optional string to prepend to output, just before the
  1738. function. Allows for easy grepping and is useful when
  1739. merging AI/Client reports into a single file.
  1740. xform: Optional callback that accepts a single parameter:
  1741. argument 0 to the decorated function. (assumed to be 'self')
  1742. It should return a value to be inserted into the report
  1743. output string.
  1744. notifyFunc: A notify function such as info, debug, warning, etc.
  1745. By default the report will be printed to stdout. This will
  1746. allow you send the report to a designated 'notify' output.
  1747. dConfigParam: A list of Config.prc string variables.
  1748. By default the report will always print. If you specify
  1749. this param, it will only print if one of the specified
  1750. config strings resolve to True.
  1751. """
  1752. def indent(str):
  1753. global __report_indent
  1754. return ' '*__report_indent+str
  1755. def decorator(f):
  1756. return f
  1757. try:
  1758. if not __dev__ and not ConfigVariableBool('force-reports', False):
  1759. return decorator
  1760. # determine whether we should use the decorator
  1761. # based on the value of dConfigParam.
  1762. dConfigParamList = []
  1763. doPrint = False
  1764. if not dConfigParam:
  1765. doPrint = True
  1766. else:
  1767. if not isinstance(dConfigParam, (list,tuple)):
  1768. dConfigParams = (dConfigParam,)
  1769. else:
  1770. dConfigParams = dConfigParam
  1771. dConfigParamList = [param for param in dConfigParams \
  1772. if ConfigVariableBool('want-%s-report' % (param,), False)]
  1773. doPrint = bool(dConfigParamList)
  1774. if not doPrint:
  1775. return decorator
  1776. # Determine any prefixes defined in our Config.prc.
  1777. if prefix:
  1778. prefixes = set([prefix])
  1779. else:
  1780. prefixes = set()
  1781. for param in dConfigParamList:
  1782. prefix = config.GetString('prefix-%s-report' % (param,), '')
  1783. if prefix:
  1784. prefixes.add(prefix)
  1785. except NameError as e:
  1786. return decorator
  1787. globalClockDelta = importlib.import_module("direct.distributed.ClockDelta").globalClockDelta
  1788. def decorator(f):
  1789. def wrap(*args,**kwargs):
  1790. if args:
  1791. rArgs = [args[0].__class__.__name__ + ', ']
  1792. else:
  1793. rArgs = []
  1794. if 'args' in types:
  1795. rArgs += [repr(x)+', ' for x in args[1:]] + \
  1796. [ x + ' = ' + '%s, ' % repr(y) for x,y in kwargs.items()]
  1797. if not rArgs:
  1798. rArgs = '()'
  1799. else:
  1800. rArgs = '(' + functools.reduce(str.__add__,rArgs)[:-2] + ')'
  1801. outStr = '%s%s' % (f.__name__, rArgs)
  1802. # Insert prefix place holder, if needed
  1803. if prefixes:
  1804. outStr = '%%s %s' % (outStr,)
  1805. globalClock = ClockObject.getGlobalClock()
  1806. if 'module' in types:
  1807. outStr = '%s {M:%s}' % (outStr, f.__module__.split('.')[-1])
  1808. if 'frameCount' in types:
  1809. outStr = '%-8d : %s' % (globalClock.getFrameCount(), outStr)
  1810. if 'timeStamp' in types:
  1811. outStr = '%-8.3f : %s' % (globalClock.getFrameTime(), outStr)
  1812. if 'deltaStamp' in types:
  1813. outStr = '%-8.2f : %s' % (globalClock.getRealTime() - \
  1814. globalClockDelta.delta, outStr)
  1815. if 'avLocation' in types:
  1816. outStr = '%s : %s' % (outStr, str(localAvatar.getLocation()))
  1817. if xform:
  1818. outStr = '%s : %s' % (outStr, xform(args[0]))
  1819. if prefixes:
  1820. # This will print the same report once for each prefix
  1821. for prefix in prefixes:
  1822. if notifyFunc:
  1823. notifyFunc(outStr % (prefix,))
  1824. else:
  1825. print(indent(outStr % (prefix,)))
  1826. else:
  1827. if notifyFunc:
  1828. notifyFunc(outStr)
  1829. else:
  1830. print(indent(outStr))
  1831. if 'interests' in types:
  1832. base.cr.printInterestSets()
  1833. if 'stackTrace' in types:
  1834. print(StackTrace())
  1835. global __report_indent
  1836. rVal = None
  1837. try:
  1838. __report_indent += 1
  1839. rVal = f(*args,**kwargs)
  1840. finally:
  1841. __report_indent -= 1
  1842. if rVal is not None:
  1843. print(indent(' -> '+repr(rVal)))
  1844. return rVal
  1845. wrap.__name__ = f.__name__
  1846. wrap.__dict__ = f.__dict__
  1847. wrap.__doc__ = f.__doc__
  1848. wrap.__module__ = f.__module__
  1849. return wrap
  1850. return decorator
  1851. def getBase():
  1852. try:
  1853. return base
  1854. except:
  1855. return simbase
  1856. def getRepository():
  1857. try:
  1858. return base.cr
  1859. except:
  1860. return simbase.air
  1861. exceptionLoggedNotify = None
  1862. if __debug__:
  1863. def exceptionLogged(append=True):
  1864. """decorator that outputs the function name and all arguments
  1865. if an exception passes back through the stack frame
  1866. if append is true, string is appended to the __str__ output of
  1867. the exception. if append is false, string is printed to the log
  1868. directly. If the output will take up many lines, it's recommended
  1869. to set append to False so that the exception stack is not hidden
  1870. by the output of this decorator.
  1871. """
  1872. try:
  1873. null = not __dev__
  1874. except:
  1875. null = not __debug__
  1876. if null:
  1877. # if we're not in __dev__, just return the function itself. This
  1878. # results in zero runtime overhead, since decorators are evaluated
  1879. # at module-load.
  1880. def nullDecorator(f):
  1881. return f
  1882. return nullDecorator
  1883. def _decoratorFunc(f, append=append):
  1884. global exceptionLoggedNotify
  1885. if exceptionLoggedNotify is None:
  1886. from direct.directnotify.DirectNotifyGlobal import directNotify
  1887. exceptionLoggedNotify = directNotify.newCategory("ExceptionLogged")
  1888. def _exceptionLogged(*args, **kArgs):
  1889. try:
  1890. return f(*args, **kArgs)
  1891. except Exception as e:
  1892. try:
  1893. s = '%s(' % f.__name__
  1894. for arg in args:
  1895. s += '%s, ' % arg
  1896. for key, value in list(kArgs.items()):
  1897. s += '%s=%s, ' % (key, value)
  1898. if len(args) > 0 or len(kArgs) > 0:
  1899. s = s[:-2]
  1900. s += ')'
  1901. if append:
  1902. appendStr(e, '\n%s' % s)
  1903. else:
  1904. exceptionLoggedNotify.info(s)
  1905. except:
  1906. exceptionLoggedNotify.info(
  1907. '%s: ERROR IN PRINTING' % f.__name__)
  1908. raise
  1909. _exceptionLogged.__doc__ = f.__doc__
  1910. return _exceptionLogged
  1911. return _decoratorFunc
  1912. # http://en.wikipedia.org/wiki/Golden_ratio
  1913. GoldenRatio = (1. + math.sqrt(5.)) / 2.
  1914. class GoldenRectangle:
  1915. @staticmethod
  1916. def getLongerEdge(shorter):
  1917. return shorter * GoldenRatio
  1918. @staticmethod
  1919. def getShorterEdge(longer):
  1920. return longer / GoldenRatio
  1921. def nullGen():
  1922. # generator that ends immediately
  1923. if False:
  1924. # yield that never runs but still exists, making this func a generator
  1925. yield None
  1926. def loopGen(l):
  1927. # generator that yields the items of an iterable object forever
  1928. def _gen(l):
  1929. while True:
  1930. for item in l:
  1931. yield item
  1932. gen = _gen(l)
  1933. # don't leak
  1934. _gen = None
  1935. return gen
  1936. def makeFlywheelGen(objects, countList=None, countFunc=None, scale=None):
  1937. # iterates and finally yields a flywheel generator object
  1938. # the number of appearances for each object is controlled by passing in
  1939. # a list of counts, or a functor that returns a count when called with
  1940. # an object from the 'objects' list.
  1941. # if scale is provided, all counts are scaled by the scale value and then int()'ed.
  1942. def flywheel(index2objectAndCount):
  1943. # generator to produce a sequence whose elements appear a specific number of times
  1944. while len(index2objectAndCount) > 0:
  1945. keyList = list(index2objectAndCount.keys())
  1946. for key in keyList:
  1947. if index2objectAndCount[key][1] > 0:
  1948. yield index2objectAndCount[key][0]
  1949. index2objectAndCount[key][1] -= 1
  1950. if index2objectAndCount[key][1] <= 0:
  1951. del index2objectAndCount[key]
  1952. # if we were not given a list of counts, create it by calling countFunc
  1953. if countList is None:
  1954. countList = []
  1955. for object in objects:
  1956. yield None
  1957. countList.append(countFunc(object))
  1958. if scale is not None:
  1959. # scale the counts if we've got a scale factor
  1960. for i in range(len(countList)):
  1961. yield None
  1962. if countList[i] > 0:
  1963. countList[i] = max(1, int(countList[i] * scale))
  1964. # create a dict for the flywheel to use during its iteration to efficiently select
  1965. # the objects for the sequence
  1966. index2objectAndCount = {}
  1967. for i in range(len(countList)):
  1968. yield None
  1969. index2objectAndCount[i] = [objects[i], countList[i]]
  1970. # create the flywheel generator
  1971. yield flywheel(index2objectAndCount)
  1972. def flywheel(*args, **kArgs):
  1973. # create a flywheel generator
  1974. # see arguments and comments in flywheelGen above
  1975. # example usage:
  1976. """
  1977. >>> for i in flywheel([1,2,3], countList=[10, 5, 1]):
  1978. ... print i,
  1979. ...
  1980. 1 2 3 1 2 1 2 1 2 1 2 1 1 1 1 1
  1981. """
  1982. for flywheel in makeFlywheelGen(*args, **kArgs):
  1983. pass
  1984. return flywheel
  1985. if __debug__:
  1986. def quickProfile(name="unnamed"):
  1987. import pstats
  1988. def profileDecorator(f):
  1989. if not ConfigVariableBool("use-profiler", False):
  1990. return f
  1991. def _profiled(*args, **kArgs):
  1992. # must do this in here because we don't have base/simbase
  1993. # at the time that PythonUtil is loaded
  1994. if not ConfigVariableBool("profile-debug", False):
  1995. #dumb timings
  1996. clock = ClockObject.getGlobalClock()
  1997. st = clock.getRealTime()
  1998. f(*args, **kArgs)
  1999. s = clock.getRealTime() - st
  2000. print("Function %s.%s took %s seconds"%(f.__module__, f.__name__,s))
  2001. else:
  2002. import profile as prof, pstats
  2003. #detailed profile, stored in base.stats under (
  2004. if not hasattr(base, "stats"):
  2005. base.stats = {}
  2006. if not base.stats.get(name):
  2007. base.stats[name] = []
  2008. prof.runctx('f(*args, **kArgs)', {'f':f,'args':args,'kArgs':kArgs},None,"t.prof")
  2009. s=pstats.Stats("t.prof")
  2010. #p=hotshot.Profile("t.prof")
  2011. #p.runctx('f(*args, **kArgs)', {'f':f,'args':args,'kArgs':kArgs},None)
  2012. #s = hotshot.stats.load("t.prof")
  2013. s.strip_dirs()
  2014. s.sort_stats("cumulative")
  2015. base.stats[name].append(s)
  2016. _profiled.__doc__ = f.__doc__
  2017. return _profiled
  2018. return profileDecorator
  2019. def getTotalAnnounceTime():
  2020. td=0
  2021. for objs in base.stats.values():
  2022. for stat in objs:
  2023. td+=getAnnounceGenerateTime(stat)
  2024. return td
  2025. def getAnnounceGenerateTime(stat):
  2026. val=0
  2027. stats=stat.stats
  2028. for i in list(stats.keys()):
  2029. if i[2] == "announceGenerate":
  2030. newVal = stats[i][3]
  2031. if newVal > val:
  2032. val = newVal
  2033. return val
  2034. class MiniLog:
  2035. def __init__(self, name):
  2036. self.indent = 1
  2037. self.name = name
  2038. self.lines = []
  2039. def __str__(self):
  2040. return '%s\nMiniLog: %s\n%s\n%s\n%s' % \
  2041. ('*'*50, self.name, '-'*50, '\n'.join(self.lines), '*'*50)
  2042. def enterFunction(self, funcName, *args, **kw):
  2043. rArgs = [repr(x)+', ' for x in args] + \
  2044. [ x + ' = ' + '%s, ' % repr(y) for x,y in kw.items()]
  2045. if not rArgs:
  2046. rArgs = '()'
  2047. else:
  2048. rArgs = '(' + functools.reduce(str.__add__,rArgs)[:-2] + ')'
  2049. line = '%s%s' % (funcName, rArgs)
  2050. self.appendFunctionCall(line)
  2051. self.indent += 1
  2052. return line
  2053. def exitFunction(self):
  2054. self.indent -= 1
  2055. return self.indent
  2056. def appendFunctionCall(self, line):
  2057. self.lines.append(' '*(self.indent*2) + line)
  2058. return line
  2059. def appendLine(self, line):
  2060. self.lines.append(' '*(self.indent*2) + '<< ' + line + ' >>')
  2061. return line
  2062. def flush(self):
  2063. outStr = str(self)
  2064. self.indent = 0
  2065. self.lines = []
  2066. return outStr
  2067. class MiniLogSentry:
  2068. def __init__(self, log, funcName, *args, **kw):
  2069. self.log = log
  2070. if self.log:
  2071. self.log.enterFunction(funcName, *args, **kw)
  2072. def __del__(self):
  2073. if self.log:
  2074. self.log.exitFunction()
  2075. del self.log
  2076. def logBlock(id, msg):
  2077. print('<< LOGBLOCK(%03d)' % id)
  2078. print(str(msg))
  2079. print('/LOGBLOCK(%03d) >>' % id)
  2080. class HierarchyException(Exception):
  2081. JOSWILSO = 0
  2082. def __init__(self, owner, description):
  2083. self.owner = owner
  2084. self.desc = description
  2085. def __str__(self):
  2086. return '(%s): %s' % (self.owner, self.desc)
  2087. def __repr__(self):
  2088. return 'HierarchyException(%s)' % (self.owner, )
  2089. def formatTimeCompact(seconds):
  2090. # returns string in format '1d3h22m43s'
  2091. result = ''
  2092. a = int(seconds)
  2093. seconds = a % 60
  2094. a //= 60
  2095. if a > 0:
  2096. minutes = a % 60
  2097. a //= 60
  2098. if a > 0:
  2099. hours = a % 24
  2100. a //= 24
  2101. if a > 0:
  2102. days = a
  2103. result += '%sd' % days
  2104. result += '%sh' % hours
  2105. result += '%sm' % minutes
  2106. result += '%ss' % seconds
  2107. return result
  2108. if __debug__ and __name__ == '__main__':
  2109. ftc = formatTimeCompact
  2110. assert ftc(0) == '0s'
  2111. assert ftc(1) == '1s'
  2112. assert ftc(60) == '1m0s'
  2113. assert ftc(64) == '1m4s'
  2114. assert ftc(60*60) == '1h0m0s'
  2115. assert ftc(24*60*60) == '1d0h0m0s'
  2116. assert ftc(24*60*60 + 2*60*60 + 34*60 + 12) == '1d2h34m12s'
  2117. del ftc
  2118. def formatTimeExact(seconds):
  2119. # like formatTimeCompact but leaves off '0 seconds', '0 minutes' etc. for
  2120. # times that are e.g. 1 hour, 3 days etc.
  2121. # returns string in format '1d3h22m43s'
  2122. result = ''
  2123. a = int(seconds)
  2124. seconds = a % 60
  2125. a //= 60
  2126. if a > 0:
  2127. minutes = a % 60
  2128. a //= 60
  2129. if a > 0:
  2130. hours = a % 24
  2131. a //= 24
  2132. if a > 0:
  2133. days = a
  2134. result += '%sd' % days
  2135. if hours or minutes or seconds:
  2136. result += '%sh' % hours
  2137. if minutes or seconds:
  2138. result += '%sm' % minutes
  2139. if seconds or result == '':
  2140. result += '%ss' % seconds
  2141. return result
  2142. if __debug__ and __name__ == '__main__':
  2143. fte = formatTimeExact
  2144. assert fte(0) == '0s'
  2145. assert fte(1) == '1s'
  2146. assert fte(2) == '2s'
  2147. assert fte(61) == '1m1s'
  2148. assert fte(60) == '1m'
  2149. assert fte(60*60) == '1h'
  2150. assert fte(24*60*60) == '1d'
  2151. assert fte((24*60*60) + (2 * 60)) == '1d0h2m'
  2152. del fte
  2153. class AlphabetCounter:
  2154. # object that produces 'A', 'B', 'C', ... 'AA', 'AB', etc.
  2155. def __init__(self):
  2156. self._curCounter = ['A']
  2157. def next(self):
  2158. result = ''.join([c for c in self._curCounter])
  2159. index = -1
  2160. while True:
  2161. curChar = self._curCounter[index]
  2162. if curChar == 'Z':
  2163. nextChar = 'A'
  2164. carry = True
  2165. else:
  2166. nextChar = chr(ord(self._curCounter[index])+1)
  2167. carry = False
  2168. self._curCounter[index] = nextChar
  2169. if carry:
  2170. if (-index) == len(self._curCounter):
  2171. self._curCounter = ['A',] + self._curCounter
  2172. break
  2173. else:
  2174. index -= 1
  2175. carry = False
  2176. else:
  2177. break
  2178. return result
  2179. __next__ = next
  2180. if __debug__ and __name__ == '__main__':
  2181. def testAlphabetCounter():
  2182. tempList = []
  2183. ac = AlphabetCounter()
  2184. for i in range(26*3):
  2185. tempList.append(ac.next())
  2186. assert tempList == [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  2187. 'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ',
  2188. 'BA','BB','BC','BD','BE','BF','BG','BH','BI','BJ','BK','BL','BM','BN','BO','BP','BQ','BR','BS','BT','BU','BV','BW','BX','BY','BZ',]
  2189. ac = AlphabetCounter()
  2190. num = 26 # A-Z
  2191. num += (26*26) # AA-ZZ
  2192. num += 26 # AAZ
  2193. num += 1 # ABA
  2194. num += 2 # ABC
  2195. for i in range(num):
  2196. x = ac.next()
  2197. assert x == 'ABC'
  2198. testAlphabetCounter()
  2199. del testAlphabetCounter
  2200. class Default:
  2201. # represents 'use the default value'
  2202. # useful for keyword arguments to virtual methods
  2203. pass
  2204. superLogFile = None
  2205. def startSuperLog(customFunction = None):
  2206. global superLogFile
  2207. if not superLogFile:
  2208. superLogFile = open("c:\\temp\\superLog.txt", "w")
  2209. def trace_dispatch(a,b,c):
  2210. if b == 'call' and a.f_code.co_name != '?' and a.f_code.co_name.find("safeRepr") < 0:
  2211. vars = dict(a.f_locals)
  2212. if 'self' in vars:
  2213. del vars['self']
  2214. if '__builtins__' in vars:
  2215. del vars['__builtins__']
  2216. for i in vars:
  2217. vars[i] = safeReprTypeOnFail(vars[i])
  2218. if customFunction:
  2219. superLogFile.write( "before = %s\n"%customFunction())
  2220. superLogFile.write( "%s(%s):%s:%s\n"%(a.f_code.co_filename.split("\\")[-1],a.f_code.co_firstlineno, a.f_code.co_name, vars))
  2221. if customFunction:
  2222. superLogFile.write( "after = %s\n"%customFunction())
  2223. return trace_dispatch
  2224. sys.settrace(trace_dispatch)
  2225. def endSuperLog():
  2226. global superLogFile
  2227. if superLogFile:
  2228. sys.settrace(None)
  2229. superLogFile.close()
  2230. superLogFile = None
  2231. def configIsToday(configName):
  2232. # TODO: replace usage of strptime with something else
  2233. # returns true if config string is a valid representation of today's date
  2234. today = time.localtime()
  2235. confStr = config.GetString(configName, '')
  2236. for format in ('%m/%d/%Y', '%m-%d-%Y', '%m.%d.%Y'):
  2237. try:
  2238. confDate = time.strptime(confStr, format)
  2239. except ValueError:
  2240. pass
  2241. else:
  2242. if (confDate.tm_year == today.tm_year and
  2243. confDate.tm_mon == today.tm_mon and
  2244. confDate.tm_mday == today.tm_mday):
  2245. return True
  2246. return False
  2247. def typeName(o):
  2248. if hasattr(o, '__class__'):
  2249. return o.__class__.__name__
  2250. else:
  2251. return o.__name__
  2252. def safeTypeName(o):
  2253. try:
  2254. return typeName(o)
  2255. except:
  2256. pass
  2257. try:
  2258. return type(o)
  2259. except:
  2260. pass
  2261. return '<failed safeTypeName()>'
  2262. def histogramDict(l):
  2263. d = {}
  2264. for e in l:
  2265. d.setdefault(e, 0)
  2266. d[e] += 1
  2267. return d
  2268. def unescapeHtmlString(s):
  2269. # converts %## to corresponding character
  2270. # replaces '+' with ' '
  2271. result = ''
  2272. i = 0
  2273. while i < len(s):
  2274. char = s[i]
  2275. if char == '+':
  2276. char = ' '
  2277. elif char == '%':
  2278. if i < (len(s)-2):
  2279. num = int(s[i+1:i+3], 16)
  2280. char = chr(num)
  2281. i += 2
  2282. i += 1
  2283. result += char
  2284. return result
  2285. class PriorityCallbacks:
  2286. """ manage a set of prioritized callbacks, and allow them to be invoked in order of priority """
  2287. def __init__(self):
  2288. self._callbacks = []
  2289. def clear(self):
  2290. del self._callbacks[:]
  2291. def add(self, callback, priority=None):
  2292. if priority is None:
  2293. priority = 0
  2294. callbacks = self._callbacks
  2295. lo = 0
  2296. hi = len(callbacks)
  2297. while lo < hi:
  2298. mid = (lo + hi) // 2
  2299. if priority < callbacks[mid][0]:
  2300. hi = mid
  2301. else:
  2302. lo = mid + 1
  2303. item = (priority, callback)
  2304. callbacks.insert(lo, item)
  2305. return item
  2306. def remove(self, item):
  2307. self._callbacks.remove(item)
  2308. def __call__(self):
  2309. for priority, callback in self._callbacks:
  2310. callback()
  2311. builtins.Functor = Functor
  2312. builtins.Stack = Stack
  2313. builtins.Queue = Queue
  2314. builtins.Enum = Enum
  2315. builtins.SerialNumGen = SerialNumGen
  2316. builtins.SerialMaskedGen = SerialMaskedGen
  2317. builtins.ScratchPad = ScratchPad
  2318. builtins.uniqueName = uniqueName
  2319. builtins.serialNum = serialNum
  2320. if __debug__:
  2321. builtins.profiled = profiled
  2322. builtins.exceptionLogged = exceptionLogged
  2323. builtins.itype = itype
  2324. builtins.appendStr = appendStr
  2325. builtins.bound = bound
  2326. builtins.clamp = clamp
  2327. builtins.lerp = lerp
  2328. builtins.makeList = makeList
  2329. builtins.makeTuple = makeTuple
  2330. if __debug__:
  2331. builtins.printStack = printStack
  2332. builtins.printReverseStack = printReverseStack
  2333. builtins.printVerboseStack = printVerboseStack
  2334. builtins.DelayedCall = DelayedCall
  2335. builtins.DelayedFunctor = DelayedFunctor
  2336. builtins.FrameDelayedCall = FrameDelayedCall
  2337. builtins.SubframeCall = SubframeCall
  2338. builtins.invertDict = invertDict
  2339. builtins.invertDictLossless = invertDictLossless
  2340. builtins.getBase = getBase
  2341. builtins.getRepository = getRepository
  2342. builtins.safeRepr = safeRepr
  2343. builtins.fastRepr = fastRepr
  2344. builtins.nullGen = nullGen
  2345. builtins.flywheel = flywheel
  2346. builtins.loopGen = loopGen
  2347. if __debug__:
  2348. builtins.StackTrace = StackTrace
  2349. builtins.report = report
  2350. builtins.pstatcollect = pstatcollect
  2351. builtins.MiniLog = MiniLog
  2352. builtins.MiniLogSentry = MiniLogSentry
  2353. builtins.logBlock = logBlock
  2354. builtins.HierarchyException = HierarchyException
  2355. builtins.deeptype = deeptype
  2356. builtins.Default = Default
  2357. builtins.configIsToday = configIsToday
  2358. builtins.typeName = typeName
  2359. builtins.safeTypeName = safeTypeName
  2360. builtins.histogramDict = histogramDict