mirror of
https://github.com/panda3d/panda3d.git
synced 2025-10-05 03:15:07 -04:00
non-lossy invertDict, fixed fastRepr already-visited logic
This commit is contained in:
parent
8a712d1d2d
commit
ca66ed7e36
@ -549,10 +549,10 @@ def list2dict(L, value=None):
|
|||||||
"""creates dict using elements of list, all assigned to same value"""
|
"""creates dict using elements of list, all assigned to same value"""
|
||||||
return dict([(k, value) for k in L])
|
return dict([(k, value) for k in L])
|
||||||
|
|
||||||
def invertDict(D):
|
def invertDict(D, lossy=False):
|
||||||
"""creates a dictionary by 'inverting' D; keys are placed in the new
|
"""creates a dictionary by 'inverting' D; keys are placed in the new
|
||||||
dictionary under their corresponding value in the old dictionary.
|
dictionary under their corresponding value in the old dictionary.
|
||||||
Data will be lost if D contains any duplicate values.
|
It is an error if D contains any duplicate values.
|
||||||
|
|
||||||
>>> old = {'key1':1, 'key2':2}
|
>>> old = {'key1':1, 'key2':2}
|
||||||
>>> invertDict(old)
|
>>> invertDict(old)
|
||||||
@ -560,6 +560,8 @@ def invertDict(D):
|
|||||||
"""
|
"""
|
||||||
n = {}
|
n = {}
|
||||||
for key, value in D.items():
|
for key, value in D.items():
|
||||||
|
if not lossy and value in n:
|
||||||
|
raise 'duplicate key in invertDict: %s' % value
|
||||||
n[value] = key
|
n[value] = key
|
||||||
return n
|
return n
|
||||||
|
|
||||||
@ -2006,7 +2008,6 @@ def fastRepr(obj, maxLen=200, strFactor=10, _visitedIds=None):
|
|||||||
_visitedIds = set()
|
_visitedIds = set()
|
||||||
if id(obj) in _visitedIds:
|
if id(obj) in _visitedIds:
|
||||||
return '<ALREADY-VISITED %s>' % itype(obj)
|
return '<ALREADY-VISITED %s>' % itype(obj)
|
||||||
_visitedIds.add(id(obj))
|
|
||||||
if type(obj) in (types.TupleType, types.ListType):
|
if type(obj) in (types.TupleType, types.ListType):
|
||||||
s = ''
|
s = ''
|
||||||
s += {types.TupleType: '(',
|
s += {types.TupleType: '(',
|
||||||
@ -2017,9 +2018,11 @@ def fastRepr(obj, maxLen=200, strFactor=10, _visitedIds=None):
|
|||||||
else:
|
else:
|
||||||
o = obj
|
o = obj
|
||||||
ellips = ''
|
ellips = ''
|
||||||
|
_visitedIds.add(id(obj))
|
||||||
for item in o:
|
for item in o:
|
||||||
s += fastRepr(item, maxLen, _visitedIds=_visitedIds)
|
s += fastRepr(item, maxLen, _visitedIds=_visitedIds)
|
||||||
s += ', '
|
s += ', '
|
||||||
|
_visitedIds.remove(id(obj))
|
||||||
s += ellips
|
s += ellips
|
||||||
s += {types.TupleType: ')',
|
s += {types.TupleType: ')',
|
||||||
types.ListType: ']',}[type(obj)]
|
types.ListType: ']',}[type(obj)]
|
||||||
@ -2028,13 +2031,18 @@ def fastRepr(obj, maxLen=200, strFactor=10, _visitedIds=None):
|
|||||||
s = '{'
|
s = '{'
|
||||||
if len(obj) > maxLen:
|
if len(obj) > maxLen:
|
||||||
o = obj.keys()[:maxLen]
|
o = obj.keys()[:maxLen]
|
||||||
|
ellips = '...'
|
||||||
else:
|
else:
|
||||||
o = obj.keys()
|
o = obj.keys()
|
||||||
|
ellips = ''
|
||||||
|
_visitedIds.add(id(obj))
|
||||||
for key in o:
|
for key in o:
|
||||||
value = obj[key]
|
value = obj[key]
|
||||||
s += '%s: %s, ' % (fastRepr(key, maxLen, _visitedIds=_visitedIds),
|
s += '%s: %s, ' % (fastRepr(key, maxLen, _visitedIds=_visitedIds),
|
||||||
fastRepr(value, maxLen, _visitedIds=_visitedIds))
|
fastRepr(value, maxLen, _visitedIds=_visitedIds))
|
||||||
s += '...}'
|
_visitedIds.remove(id(obj))
|
||||||
|
s += ellips
|
||||||
|
s += '}'
|
||||||
return s
|
return s
|
||||||
elif type(obj) is types.StringType:
|
elif type(obj) is types.StringType:
|
||||||
maxLen *= strFactor
|
maxLen *= strFactor
|
||||||
|
Loading…
x
Reference in New Issue
Block a user