我知道如何将hy模块导入python。我所要做的就是创建一个包含something.hy代码的hy文件,然后执行以下操作.
import hy
import something
something.func('args') # assumes there is an hy function called `func`但是,我还没有弄清楚如何计算包含hy代码的python中的字符串。例如..。
hycode = '(print "it works!")'
hy.SOMEHOW_EVALUATE(hycode)
# I'd like this to cause the string `it works!` to print out.或者这个例子..。
hycode = '(+ 39 3)'
result = hy.SOMEHOW_EVALUATE(hycode)
# I'd like result to now contain `42`在python中使用hy时,是否有任何方法以这种方式计算字符串?
发布于 2017-09-21 20:41:51
使用hy.read_str和hy.eval。
>>> import hy
>>> hy.read_str("(+ 39 3)")
HyExpression([
HySymbol('+'),
HyInteger(39),
HyInteger(3)])
>>> hy.eval(_)
42
>>> hycode = hy.read_str('(print "it works!")')
>>> hycode
HyExpression([
HySymbol('print'),
HyString('it works!')])
>>> hy.eval(hycode)
it works!如果您安装Hy从Github母版中安装,则此操作有效。如果您需要让它在Hy的旧版本上工作,您可以看到hy包的__init__.py中的实现很简单
from hy.core.language import read, read_str # NOQA
from hy.importer import hy_eval as eval # NOQAhttps://stackoverflow.com/questions/46351010
复制相似问题