简体   繁体   中英

Convert python literal_eval string to hy

I would like to convert the following to Hy, but I can't seem to figure out how to do it; is there perhaps a way to convert a string of python code to the hy syntax? I don't know if py would work because the evaluated result of f_back may not be safe.

import ast, inspect
def test(f_back):
    return ast.literal_eval(f"inspect.currentframe(){'.f_back' * f_back}")

You could always evaluate x = x.f_back repeatedly in a loop instead of using metaprogramming for this.

To address the more general question, Hy itself only implements the Hy-to-Python direction, not Python to Hy. There's Hikaru Ikuta's py2hy , which doesn't work with recent releases of Hy, but he's recently talked about updating it , so that may change soon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM