Skip to content

Instantly share code, notes, and snippets.

@tuturto
Created December 8, 2016 13:43
Show Gist options
  • Save tuturto/a3dc1bb38da3aed6a8eff338cbc5e8ac to your computer and use it in GitHub Desktop.
Save tuturto/a3dc1bb38da3aed6a8eff338cbc5e8ac to your computer and use it in GitHub Desktop.
;;;
;;; 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