Skip to content

Commit 093bdcb

Browse files
author
hhaensel
committed
add support for @py from <module> import
1 parent 0148fd2 commit 093bdcb

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/py_macro.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,10 +793,26 @@ For example:
793793
- `x.foo` is translated to `pygetattr(x, "foo")`
794794
795795
Compound statements such as `begin`, `if`, `while` and `for` are supported.
796+
Import statements are supported, e.g.
797+
- `import foo, bar`
798+
- `from os.path import join as py_joinpath, exists`
796799
797800
See the online documentation for more details.
798801
"""
799802
macro py(ex)
800803
esc(py_macro(ex, __module__, __source__))
801804
end
805+
806+
macro py(keyword, modulename, ex)
807+
keyword == :from || return :( nothing )
808+
809+
d = Dict(isa(a.args[1], Symbol) ? a.args[1] => a.args[1] : a.args[1].args[1] => a.args[2] for a in ex.args)
810+
vars = Expr(:tuple, values(d)...)
811+
imports = Tuple(keys(d))
812+
813+
esc(quote
814+
$vars = pyimport($(string(modulename)) => $(string.(imports)))
815+
end)
816+
end
817+
802818
export @py

0 commit comments

Comments
 (0)