tools/pyboard.py: Add parse kwarg to eval.
This is useful when using pyboard.py as a library rather than at the command line. pyb.eval("1+1") --> b"2" pyb.eval("{'a': '\x00'}") --> b"{'a': '\\x00'}" Now you can also do pyb.eval("1+1", parse=True) --> 2 pyb.eval("{'a': '\x00'}", parse=True) --> {'a': '\x00'} This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
parent
77002a92bf
commit
6013d27dd5
|
@ -457,10 +457,15 @@ class Pyboard:
|
||||||
self.exec_raw_no_follow(command)
|
self.exec_raw_no_follow(command)
|
||||||
return self.follow(timeout, data_consumer)
|
return self.follow(timeout, data_consumer)
|
||||||
|
|
||||||
def eval(self, expression):
|
def eval(self, expression, parse=False):
|
||||||
ret = self.exec_("print({})".format(expression))
|
if parse:
|
||||||
ret = ret.strip()
|
ret = self.exec_("print(repr({}))".format(expression))
|
||||||
return ret
|
ret = ret.strip()
|
||||||
|
return ast.literal_eval(ret.decode())
|
||||||
|
else:
|
||||||
|
ret = self.exec_("print({})".format(expression))
|
||||||
|
ret = ret.strip()
|
||||||
|
return ret
|
||||||
|
|
||||||
def exec_(self, command, data_consumer=None):
|
def exec_(self, command, data_consumer=None):
|
||||||
ret, ret_err = self.exec_raw(command, data_consumer=data_consumer)
|
ret, ret_err = self.exec_raw(command, data_consumer=data_consumer)
|
||||||
|
|
Loading…
Reference in New Issue