i have following string in report file:
"bunch(conditions=['s1', 's2', 's3', 's4', 's5', 's6'], durations=[[30.0], [30.0], [30.0], [30.0], [30.0], [30.0]], onsets=[[172.77], [322.77], [472.77], [622.77], [772.77], [922.77]])"
i turn bunch()
object or dict
, can access information inside (via either my_var.conditions
or my_var["conditions"]
).
this works eval()
:
eval("bunch(conditions=['s1', 's2', 's3', 's4', 's5', 's6'], durations=[[30.0], [30.0], [30.0], [30.0], [30.0], [30.0]], onsets=[[172.77], [322.77], [472.77], [622.77], [772.77], [922.77]])")
however avoid using that.
i have tried write couple of string substitutions convert dict syntax , parse json.loads()
looks very hackish, , break encounter new fields in future strings; e.g.:
"{"+"bunch(conditions=['s1', 's2', 's3', 's4', 's5', 's6'], durations=[[30.0], [30.0], [30.0], [30.0], [30.0], [30.0]], onsets=[[172.77], [322.77], [472.77], [622.77], [772.77], [922.77]])"[1:-1]+"}".replace("conditions=","'conditions':")
you idea.
do know if there better way parse this?
this pyparsing code define parsing expression bunch declaration.
from pyparsing import (pyparsing_common, suppress, keyword, forward, quotedstring, group, delimitedlist, dict, removequotes, parseresults) # define pyparsing parser bunch declaration lbrack,rbrack,lpar,rpar,eq = map(suppress, "[]()=") integer = pyparsing_common.integer real = pyparsing_common.real ident = pyparsing_common.identifier # define recursive expression nested lists listexpr = forward() listitem = real | integer | quotedstring.setparseaction(removequotes) | group(listexpr) listexpr << lbrack + delimitedlist(listitem) + rbrack # define expression bunch declaration bunch = keyword("bunch") arg_defn = group(ident + eq + listitem) bunch_decl = bunch + lpar + dict(delimitedlist(arg_defn))("args") + rpar
here parser run against sample input:
# run sample input test sample = """bunch(conditions=['s1', 's2', 's3', 's4', 's5', 's6'], durations=[[30.0], [30.0], [30.0], [30.0], [30.0], [30.0]], onsets=[[172.77], [322.77], [472.77], [622.77], [772.77], [922.77]])""" bb = bunch_decl.parsestring(sample) # print parsed output as-is print(bb)
gives:
['bunch', [['conditions', ['s1', 's2', 's3', 's4', 's5', 's6']], ['durations', [[30.0], [30.0], [30.0], [30.0], [30.0], [30.0]]], ['onsets', [[172.77], [322.77], [472.77], [622.77], [772.77], [922.77]]]]]
with pyparsing, can add parse-time callback, pyparsing tokens->bunch conversion you:
# define simple placeholder class bunch class bunch(object): def __init__(self, **kwargs): self.__dict__.update(kwargs) def __repr__(self): return "bunch:(%s)" % ', '.join("%r: %s" % item item in vars(self).items()) # add parse action, , pyparsing autoconvert parsed data bunch bunch_decl.addparseaction(lambda t: bunch(**t.args.asdict()))
now parser give actual bunch instance:
[bunch:('durations': [[30.0], [30.0], [30.0], [30.0], [30.0], [30.0]], 'conditions': ['s1', 's2', 's3', 's4', 's5', 's6'], 'onsets': [[172.77], [322.77], [472.77], [622.77], [772.77], [922.77]])]