Stringtemplate With Dict Data
Posted by sky | Tags: Python
There seems to be a bug if the dict has a value of an empty string.def test_templateEmpStr():
import stringtemplate
st = stringtemplate.StringTemplate("empty:$s$ok")
st['s'] = '' #None,'' ok
print str(st)
st = stringtemplate.StringTemplate("empty:$m.s$ok")
st['m'] = {'s': ''}
print str(st) # throw exception!
File "..\\stringtemplate\\language\\ASTExpr.py", line 276, in getObjectProperty
value = o[ASTExpr.DEFAULT_MAP_VALUE_NAME]
KeyError: '_default_'
I change the line if not value: to if value is None: to fix this so as to allow an empty string in a dict.
if isinstance(o, stringtemplate.Aggregate) or isinstance(o, dict):
#sys.stderr.write("[getObjectProperty] o = " + str(o) + '\n')
try:
value = o[propertyName]
except:
value = None
if value is None: #if not value: use if value is None instead to fix !?
# no property defined; if a map in this group
# then there may be a default value
value = o[ASTExpr.DEFAULT_MAP_VALUE_NAME]
Previous Post
Next Post