Created
December 8, 2016 13:43
-
-
Save tuturto/a3dc1bb38da3aed6a8eff338cbc5e8ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; | |
;;; how to tokenize an arbitrary string and evaluate it in local context (quick and dirty) | |
;;; you can replace call to locals with a dictionary that specifies variables (as strings) | |
;;; and their values. | |
;;; | |
from hy.lex import tokenize | |
from hy.importer import hy_eval | |
a = 10 | |
b = 5 | |
; tokenize returns a list of HyExpressions, so we simply pick the first one to evaluate. | |
; In case you already have a HyExpression instead of string, skip the tokenize part | |
hy_eval(tokenize("(+ a b)")[0], locals(), "") | |
;;; | |
;;; How to import and call Hy code in python | |
;;; Lets assume we have function "my-function" in "example" package | |
;;; | |
import hy | |
from example import my_function | |
my_function("foo") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment