@@ -7,50 +7,14 @@ def newmodule(name):
77 "A new module with the given name."
88 return Base .Module (Base .Symbol (name ))
99
10- def using (globals , module , attrs = None , prefix = 'jl' , rename = None ):
11- """Import a module into globals.
12-
13- Args:
14- globals: A dict to import into (usually 'locals()').
15- module: The name of the module to import.
16- attrs: If given, import these attributes from the module instead of
17- the module itself. A list of strings or a space-separated string.
18- prefix: A prefix added to the name of each imported item.
19- rename: If given, a function mapping names to the name assigned in globals.
20- """
21- # rename
22- if rename is None :
23- rename = lambda name : prefix + name
24- # import the module
25- path = module .split ('.' )
26- mname = path [0 ]
27- if mname == 'Base' :
28- module = Base
29- elif mname == 'Core' :
30- module = Core
31- elif mname == 'Main' :
32- module = Main
33- else :
34- module = Base .require (Base .Main , Base .Symbol (mname ))
35- for attr in path [1 :]:
36- module = Base .getproperty (module , Base .Symbol (attr ))
37- # export
38- if attrs is None :
39- globals [rename (path [- 1 ])] = module
40- else :
41- if isinstance (attrs , str ):
42- attrs = attrs .split ()
43- for attr in attrs :
44- globals [rename (attr )] = getattr (module , attr )
45-
46- class As :
47- "Interpret 'value' as type 'type' when converting to Julia."
48- __slots__ = ("value" , "type" )
49- def __init__ (self , value , type ):
50- self .value = value
51- self .type = type
52- def __repr__ (self ):
53- return "juliacall.As({!r}, {!r})" .format (self .value , self .type )
10+ _convert = None
11+
12+ def convert (T , x ):
13+ "Convert x to a Julia T."
14+ global _convert
15+ if _convert is None :
16+ _convert = PythonCall .seval ("pyjlcallback((T,x)->pyjl(pyconvert(pyjlvalue(T)::Type,x)))" )
17+ return _convert (T , x )
5418
5519class JuliaError (Exception ):
5620 "An error arising in Julia code."
0 commit comments