Skip to content

Commit d685006

Browse files
author
Christopher Doris
committed
remove using and As and add convert
1 parent c860623 commit d685006

File tree

8 files changed

+28
-98
lines changed

8 files changed

+28
-98
lines changed

docs/src/conversion-to-julia.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ From Python, the arguments to a Julia function will be converted according to th
1212
| :----------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------- |
1313
| **Top priority (wrapped values).** | |
1414
| `juliacall.AnyValue` | `Any` |
15-
| `juliacall.As` | `Any` |
1615
| **Very high priority (arrays).** | |
1716
| Objects satisfying the buffer or array interface (inc. `bytes`, `bytearray`, `array.array`, `numpy.ndarray`) | `PyArray` |
1817
| **High priority (canonical conversions).** | |

docs/src/juliacall.md

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,15 @@ See [JuliaPkg](https://github.com/cjdoris/PyJuliaPkg) for more details.
8989
## Utilities
9090

9191
`````@customdoc
92-
juliacall.using - Function
92+
juliacall.convert - Function
9393
9494
```python
95-
using(globals, module, attrs=None, prefix='jl', rename=None)
95+
convert(T, x)
9696
```
9797
98-
Import the Julia `module` into `globals`.
98+
Convert `x` to a Julia object of type `T`.
9999
100-
If `attrs` is given, the given attributes are imported from the module instead of the
101-
module itself. It may be a list of strings or a space-separated string.
102-
103-
Each item imported is renamed before being added to `globals`. By default a `prefix` is
104-
added. You more generally supply a `rename` function which maps a string to a string.
105-
106-
In the following example we import some items from `Base` to do some vector operations:
107-
```python
108-
juliacall.using(locals(), 'Base', 'Vector Int push! pop!', rename=lambda x:'jl'+x.replace('!',''))
109-
x = jlVector[jlInt]()
110-
jlpush(x, 1, 2, 3)
111-
jlpop(x) # 3
112-
```
100+
You can use this to pass an argument to a Julia function of a specific type.
113101
`````
114102

115103
`````@customdoc
@@ -121,13 +109,3 @@ newmodule(name)
121109
122110
A new module with the given name.
123111
`````
124-
125-
`````@customdoc
126-
juliacall.As - Class
127-
128-
```python
129-
As(x, T)
130-
```
131-
132-
When passed as an argument to a Julia function, is interpreted as `x` converted to Julia type `T`.
133-
`````

docs/src/releasenotes.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# Release Notes
22

3-
## v0.7.0 (2022-03-17)
3+
## v0.7.1 (2022-03-17)
44
* **Breaking:** Removes `pymethod` and `pyclass`. In the future, `pyclass` may become sugar
55
for `types.new_class` (namely you can specify a metaclass).
66
* Adds `pyfunc`, `pyclassmethod`, `pystaticmethod` and `pyproperty`.
77
* `pyconvert_add_rule` is now documented. Its semantics have changed, including the
88
separator of the first argument from `/` to `:`.
99
* A pandas `<NA>` value is now converted to `missing`.
1010
* A `NaN` in a `PyPandasDataFrame` is converted to `missing`.
11+
* **Breaking:** Removes `using` and `As` from JuliaCall.
12+
* Adds `convert` to JuliaCall (replacing `As`).
1113
* Bug fixes.
1214

1315
## v0.6.1 (2022-02-21)

python/juliacall/__init__.py

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5519
class JuliaError(Exception):
5620
"An error arising in Julia code."

python/juliacall/juliapkg.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"packages": {
44
"PythonCall": {
55
"uuid": "6099a3de-0909-46bc-b1f4-468b9a2dfc0d",
6-
"version": "0.7.0"
6+
"version": "0.7.0",
7+
"dev": true,
8+
"path": "../.."
79
}
810
}
911
}

src/convert.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ function init_pyconvert()
403403
push!(PYCONVERT_EXTRATYPES, pyimport("collections.abc" => ("Iterable", "Sequence", "Set", "Mapping"))...)
404404

405405
priority = PYCONVERT_PRIORITY_WRAP
406-
pyconvert_add_rule("juliacall:As", Any, pyconvert_rule_jlas, priority)
407406
pyconvert_add_rule("juliacall:ValueBase", Any, pyconvert_rule_jlvalue, priority)
408407

409408
priority = PYCONVERT_PRIORITY_ARRAY

src/jlwrap/callback.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ function pyjlcallback_call(self, args_::Py, kwargs_::Py)
1212
Py(self(args...; kwargs...))
1313
elseif pylen(args_) > 0
1414
args = pyconvert(Vector{Py}, args_)
15-
Py(self(args...))
15+
if length(args) == 1
16+
Py(self(args[1]))
17+
elseif length(args) == 2
18+
Py(self(args[1], args[2]))
19+
elseif length(args) == 3
20+
Py(self(args[1], args[2], args[3]))
21+
else
22+
Py(self(args...))
23+
end
1624
else
1725
Py(self())
1826
end

src/juliacall.jl

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,7 @@ function init_juliacall_2()
3636
jl.Core = Core
3737
jl.Base = Base
3838
jl.Pkg = Pkg
39+
jl.PythonCall = PythonCall
3940
pycopy!(pyJuliaError, jl.JuliaError)
4041
C.POINTERS.PyExc_JuliaError = incref(getptr(pyJuliaError))
4142
end
42-
43-
function pyconvert_rule_jlas(::Type{T}, x::Py) where {T}
44-
# get the type
45-
t = x.type
46-
if !pyisjl(t)
47-
pydel!(t)
48-
return pyconvert_unconverted()
49-
end
50-
S = _pyjl_getvalue(t)
51-
pydel!(t)
52-
S isa Type || return pyconvert_unconverted()
53-
# convert x.value to S, then to T
54-
v = x.value
55-
r = pytryconvert(S, v)
56-
pydel!(v)
57-
if pyconvert_isunconverted(r)
58-
return pyconvert_unconverted()
59-
elseif T == Any || S <: T
60-
return r
61-
else
62-
return pyconvert_tryconvert(T, pyconvert_result(r))
63-
end
64-
end

0 commit comments

Comments
 (0)