File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change 14
14
15
15
from IPython .core .magic import Magics , magics_class , line_cell_magic
16
16
from . import Main , Base , PythonCall
17
+ import __main__
18
+
19
+ _set_var = Main .seval ("(k, v) -> @eval $(Symbol(k)) = $v" )
20
+ _get_var = Main .seval ("k -> @eval $(Symbol(k))" )
17
21
18
22
@magics_class
19
23
class JuliaMagics (Magics ):
20
24
21
25
@line_cell_magic
22
26
def julia (self , line , cell = None ):
23
- code = line if cell is None else cell
27
+ invars = []
28
+ outvars = []
29
+ if cell is None :
30
+ code = line
31
+ else :
32
+ code = cell
33
+ for k in line .split ():
34
+ if k .startswith ('<' ):
35
+ invars .append (k [1 :])
36
+ elif k .startswith ('>' ):
37
+ outvars .append (k [1 :])
38
+ else :
39
+ invars .append (k )
40
+ outvars .append (k )
41
+ for k in invars :
42
+ if k in __main__ .__dict__ :
43
+ _set_var (k , __main__ .__dict__ [k ])
24
44
ans = Main .seval ('begin\n ' + code + '\n end' )
25
45
PythonCall ._flush_stdio ()
46
+ for k in outvars :
47
+ __main__ .__dict__ [k ] = _get_var (k )
26
48
if not code .strip ().endswith (';' ):
27
49
return ans
28
50
You can’t perform that action at this time.
0 commit comments