Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pysrc/juliacall/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@ def exception(self):
def backtrace(self):
return self.args[1]

def _init_jlwrap():
# Called by PythonCall once the native ValueBase type is available.
global RawValue, AnyValue, IteratorValue, TypeValue
global ModuleValue, IOValueBase, BinaryIOValue, TextIOValue
global NumberValue, ComplexValue, RealValue, RationalValue
global IntegerValue, ArrayValue, VectorValue, DictValue
global SetValue, CallbackValue
from ._jlwrap import (
RawValue,
AnyValue,
IteratorValue,
TypeValue,
ModuleValue,
IOValueBase,
BinaryIOValue,
TextIOValue,
NumberValue,
ComplexValue,
RealValue,
RationalValue,
IntegerValue,
ArrayValue,
VectorValue,
DictValue,
SetValue,
CallbackValue,
)


CONFIG = {'inited': False}

def init():
Expand Down
35 changes: 35 additions & 0 deletions pysrc/juliacall/_jlwrap/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Python implementations of Julia value wrappers, initialized by PythonCall."""

from .raw import RawValue
from .any import AnyValue
from .iter import IteratorValue
from .type import TypeValue
from .module import ModuleValue
from .io import IOValueBase, BinaryIOValue, TextIOValue
from .number import NumberValue, ComplexValue, RealValue, RationalValue, IntegerValue
from .array import ArrayValue
from .vector import VectorValue
from .dict import DictValue
from .set import SetValue
from .callback import CallbackValue

__all__ = [
"RawValue",
"AnyValue",
"IteratorValue",
"TypeValue",
"ModuleValue",
"IOValueBase",
"BinaryIOValue",
"TextIOValue",
"NumberValue",
"ComplexValue",
"RealValue",
"RationalValue",
"IntegerValue",
"ArrayValue",
"VectorValue",
"DictValue",
"SetValue",
"CallbackValue",
]
152 changes: 152 additions & 0 deletions pysrc/juliacall/_jlwrap/any.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
"""Julia value wrappers from src/JlWrap/any.jl."""

from juliacall import ValueBase

# Populated by Julia during PythonCall initialization.
_jl_methods = {}


class AnyValue(ValueBase):
__module__ = "juliacall"
__slots__ = ()
def __repr__(self):
if self._jl_isnull():
return "<jl NULL>"
else:
return self._jl_callmethod(_jl_methods["__repr__"])
def __str__(self):
if self._jl_isnull():
return "NULL"
else:
return self._jl_callmethod(_jl_methods["__str__"])
def __getattr__(self, k):
if k.startswith("__") and k.endswith("__"):
raise AttributeError(k)
else:
return self._jl_callmethod(_jl_methods["__getattr__"], k)
def __setattr__(self, k, v):
try:
ValueBase.__setattr__(self, k, v)
except AttributeError:
if k.startswith("__") and k.endswith("__"):
raise
else:
return
self._jl_callmethod(_jl_methods["__setattr__"], k, v)
def __dir__(self):
return ValueBase.__dir__(self) + self._jl_callmethod(_jl_methods["__dir__"])
def __call__(self, *args, **kwargs):
return self._jl_callmethod(_jl_methods["__call__"], args, kwargs)
def __bool__(self):
return True
def __len__(self):
return self._jl_callmethod(_jl_methods["__len__"])
def __getitem__(self, k):
return self._jl_callmethod(_jl_methods["__getitem__"], k)
def __setitem__(self, k, v):
self._jl_callmethod(_jl_methods["__setitem__"], k, v)
def __delitem__(self, k):
self._jl_callmethod(_jl_methods["__delitem__"], k)
def __iter__(self):
return self._jl_callmethod(_jl_methods["__iter__"])
def __reversed__(self):
return self._jl_callmethod(_jl_methods["__reversed__"])
def __contains__(self, v):
return self._jl_callmethod(_jl_methods["__contains__"], v)
def __pos__(self):
return self._jl_callmethod(_jl_methods["__pos__"])
def __neg__(self):
return self._jl_callmethod(_jl_methods["__neg__"])
def __abs__(self):
return self._jl_callmethod(_jl_methods["__abs__"])
def __invert__(self):
return self._jl_callmethod(_jl_methods["__invert__"])
def __add__(self, other):
return self._jl_callmethod(_jl_methods["__add__"], other)
def __sub__(self, other):
return self._jl_callmethod(_jl_methods["__sub__"], other)
def __mul__(self, other):
return self._jl_callmethod(_jl_methods["__mul__"], other)
def __truediv__(self, other):
return self._jl_callmethod(_jl_methods["__truediv__"], other)
def __floordiv__(self, other):
return self._jl_callmethod(_jl_methods["__floordiv__"], other)
def __mod__(self, other):
return self._jl_callmethod(_jl_methods["__mod__"], other)
def __pow__(self, other, modulo=None):
if modulo is None:
return self._jl_callmethod(_jl_methods["__pow__"], other)
else:
return self._jl_callmethod(_jl_methods["__pow_modulo__"], other, modulo)
def __lshift__(self, other):
return self._jl_callmethod(_jl_methods["__lshift__"], other)
def __rshift__(self, other):
return self._jl_callmethod(_jl_methods["__rshift__"], other)
def __and__(self, other):
return self._jl_callmethod(_jl_methods["__and__"], other)
def __xor__(self, other):
return self._jl_callmethod(_jl_methods["__xor__"], other)
def __or__(self, other):
return self._jl_callmethod(_jl_methods["__or__"], other)
def __radd__(self, other):
return self._jl_callmethod(_jl_methods["__radd__"], other)
def __rsub__(self, other):
return self._jl_callmethod(_jl_methods["__rsub__"], other)
def __rmul__(self, other):
return self._jl_callmethod(_jl_methods["__rmul__"], other)
def __rtruediv__(self, other):
return self._jl_callmethod(_jl_methods["__rtruediv__"], other)
def __rfloordiv__(self, other):
return self._jl_callmethod(_jl_methods["__rfloordiv__"], other)
def __rmod__(self, other):
return self._jl_callmethod(_jl_methods["__rmod__"], other)
def __rpow__(self, other, modulo=None):
if modulo is None:
return self._jl_callmethod(_jl_methods["__rpow__"], other)
else:
return self._jl_callmethod(_jl_methods["__rpow_modulo__"], other, modulo)
def __rlshift__(self, other):
return self._jl_callmethod(_jl_methods["__rlshift__"], other)
def __rrshift__(self, other):
return self._jl_callmethod(_jl_methods["__rrshift__"], other)
def __rand__(self, other):
return self._jl_callmethod(_jl_methods["__rand__"], other)
def __rxor__(self, other):
return self._jl_callmethod(_jl_methods["__rxor__"], other)
def __ror__(self, other):
return self._jl_callmethod(_jl_methods["__ror__"], other)
def __eq__(self, other):
return self._jl_callmethod(_jl_methods["__eq__"], other)
def __ne__(self, other):
return self._jl_callmethod(_jl_methods["__ne__"], other)
def __le__(self, other):
return self._jl_callmethod(_jl_methods["__le__"], other)
def __lt__(self, other):
return self._jl_callmethod(_jl_methods["__lt__"], other)
def __ge__(self, other):
return self._jl_callmethod(_jl_methods["__ge__"], other)
def __gt__(self, other):
return self._jl_callmethod(_jl_methods["__gt__"], other)
def __hash__(self):
return self._jl_callmethod(_jl_methods["__hash__"])
@property
def __name__(self):
return self._jl_callmethod(_jl_methods["__name__"])
def _jl_raw(self):
'''Convert this to a juliacall.RawValue.'''
return self._jl_callmethod(_jl_methods["_jl_raw"])
def _jl_display(self, mime=None):
'''Display this, optionally specifying the MIME type.'''
return self._jl_callmethod(_jl_methods["_jl_display"], mime)
def _jl_help(self, mime=None):
'''Show help for this Julia object.'''
return self._jl_callmethod(_jl_methods["_jl_help"], mime)
def _jl_call_nogil(self, *args, **kwargs):
'''Call this with the given arguments but with the GIL disabled.

WARNING: This function must not interact with Python at all without re-acquiring
the GIL.
'''
return self._jl_callmethod(_jl_methods["_jl_call_nogil"], args, kwargs)
def _repr_mimebundle_(self, include=None, exclude=None):
return self._jl_callmethod(_jl_methods["_repr_mimebundle_"], include, exclude)
52 changes: 52 additions & 0 deletions pysrc/juliacall/_jlwrap/array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Julia value wrappers from src/JlWrap/array.jl."""

from .any import AnyValue

# Populated by Julia during PythonCall initialization.
_jl_methods = {}


class ArrayValue(AnyValue):
__module__ = "juliacall"
__slots__ = ()
@property
def _jl_buffer_info(self):
return _jl_methods["_jl_buffer_info"]
@property
def ndim(self):
return self._jl_callmethod(_jl_methods["ndim"])
@property
def shape(self):
return self._jl_callmethod(_jl_methods["shape"])
def copy(self):
return self._jl_callmethod(_jl_methods["copy"])
def reshape(self, shape):
return self._jl_callmethod(_jl_methods["reshape"], shape)
def __bool__(self):
return bool(len(self))
def __getitem__(self, k):
return self._jl_callmethod(_jl_methods["__getitem__"], k)
def __setitem__(self, k, v):
self._jl_callmethod(_jl_methods["__setitem__"], k, v)
def __delitem__(self, k):
self._jl_callmethod(_jl_methods["__delitem__"], k)
@property
def __array_interface__(self):
return self._jl_callmethod(_jl_methods["__array_interface__"])
def __array__(self, dtype=None, copy=None):
import numpy
# convert to an array-like object
arr = self
if not (hasattr(arr, "__array_interface__") or hasattr(arr, "__array_struct__")):
if copy is False:
raise ValueError("copy=False is not supported when collecting ArrayValue data")
# the first attempt collects into an Array
arr = self._jl_callmethod(_jl_methods["__array__"])
if not (hasattr(arr, "__array_interface__") or hasattr(arr, "__array_struct__")):
# the second attempt collects into a PyObjectArray
arr = self._jl_callmethod(_jl_methods["__array_pyobjectarray__"])
# convert to a numpy array if numpy is available
return numpy.array(arr, dtype=dtype, copy=copy)
def to_numpy(self, dtype=None, copy=True, order="K"):
import numpy
return numpy.array(self, dtype=dtype, copy=copy, order=order)
27 changes: 27 additions & 0 deletions pysrc/juliacall/_jlwrap/callback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Julia value wrappers from src/JlWrap/callback.jl."""

from juliacall import ValueBase

# Populated by Julia during PythonCall initialization.
_jl_methods = {}


class CallbackValue(ValueBase):
__module__ = "juliacall"
__slots__ = ()
def __repr__(self):
if self._jl_isnull():
return "<jl NULL>"
else:
return self._jl_callmethod(_jl_methods["__repr__"])
def __str__(self):
if self._jl_isnull():
return "NULL"
else:
return self._jl_callmethod(_jl_methods["__str__"])
def __call__(self, *args, **kwargs):
return self._jl_callmethod(_jl_methods["__call__"], args, kwargs)


def wrap_callback(f):
return lambda *args, **kwargs: f(*args, **kwargs)
77 changes: 77 additions & 0 deletions pysrc/juliacall/_jlwrap/dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""Julia value wrappers from src/JlWrap/dict.jl."""

from .any import AnyValue

# Populated by Julia during PythonCall initialization.
_jl_methods = {}


class DictValue(AnyValue):
__module__ = "juliacall"
__slots__ = ()
_jl_undefined_ = object()
def __bool__(self):
return bool(len(self))
def __iter__(self):
return self._jl_callmethod(_jl_methods["__iter__"])
def __contains__(self, key):
return self._jl_callmethod(_jl_methods["__contains__"], key)
def __getitem__(self, key):
if key in self:
return self._jl_callmethod(_jl_methods["__getitem__"], key)
else:
raise KeyError(key)
def __setitem__(self, key, value):
return self._jl_callmethod(_jl_methods["__setitem__"], key, value)
def __delitem__(self, key):
if key in self:
return self._jl_callmethod(_jl_methods["__delitem__"], key)
else:
raise KeyError(key)
def keys(self):
return self._jl_callmethod(_jl_methods["keys"])
def values(self):
return self._jl_callmethod(_jl_methods["values"])
def items(self):
return self._jl_callmethod(_jl_methods["items"])
def get(self, key, default=None):
if key in self:
return self[key]
else:
return default
def setdefault(self, key, default=None):
if key not in self:
self[key] = default
return self[key]
def clear(self):
return self._jl_callmethod(_jl_methods["clear"])
def pop(self, key, default=_jl_undefined_):
if key in self:
ans = self[key]
del self[key]
return ans
elif default is self._jl_undefined_:
raise KeyError(key)
else:
return default
def popitem(self):
if len(self):
return self._jl_callmethod(_jl_methods["popitem"])
else:
raise KeyError()
def update(self, other=_jl_undefined_, **kwargs):
if other is self._jl_undefined_:
pass
else:
if hasattr(other, "keys"):
items = ((k, other[k]) for k in other.keys())
else:
items = other
self._jl_callmethod(_jl_methods["update"], items)
if kwargs:
self.update(kwargs)
def copy(self):
return self._jl_callmethod(_jl_methods["copy"])
import collections.abc
collections.abc.MutableMapping.register(DictValue)
del collections
Loading
Loading