blob: 5d41b6a78a0a5e387a33e87d2c74c0d50021bee5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
### Problems With Python/Julia Syntax Mapping with dot ('.') Operator
*aka, why the annoying underscore syntax is needed*
Using PyCall, we can map python modules to Julia modules with the same syntax:
python> import os
python> os.uname()
python> os.O_RDONLY
julia> using PyCall
julia> os = pywrap(pyimport("os"))
julia> os.uname()
julia> os.O_RDONLY
And using a recursive version of pywrap (`pyrecwrap`, see below), we can get
nested modules to map also:
python> import os
python> os.path.genericpath.os.uname()
julia> os = pyrecwrap(pyimport("os"))
julia> os.path.genericpath.os.uname()
We can also map python classes to Julia modules:
python> import pyx.style.linewidth as psl
python> psl.THICK
julia> ps = pywrap(pyimport("pyx.style"))
julia> psl = pywrap(ps.linewidth)
julia> psl.THICK
However, in Python a class/object can both have members and be called as an
initializer:
python> psl.THIN
python> psl(0.1)
But in Julia a Module can't act as a function.
Thus, a one-to-one syntax mapping just isn't going to work until (unless?) the
dot ('.') operator can be overridden (as of Julia 0.4 it may not).
The work around used in this project (PyX.jl) is to use underscores in
object/function names and dots in object/module names. Eg,
`style.linewidth(0.2)` vs. `style_linewidth.THICK`.
Note: pyrecwrap can be found in the "pyrecwrap" branch of this repo, or at
https://gist.github.com/bnewbold/b9a701e06b9da319a58dc9526b09c4a5
|