pyderive
pyderive
provides derive macros for Python spacial methods and a class attributes for PyO3.
// Enable `multiple-pymethods` feature of PyO3
use *;
use *;
// Place #[derive(PyNew, ...)] before #[pyclass]
# Derives __new__()
=
# Derives __match_args__ (supports Pattern Matching by positional arguments)
:
:
assert ==
assert == 1
assert is None
:
# Derives __repr__()
assert ==
assert ==
# Derives __eq__() that depends on PartialEq/Eq trait
assert ==
# Derives __hash__() that depends on Hash trait
assert == 3289857268557676066
This provides deriving following special methods and attributes;
Derive Macro | Python Method/Attribute |
---|---|
PyNew |
__new__() |
PyMatchArgs |
__match_args__ |
PyRepr |
__repr__() |
PyStr |
__str__() |
PyEq |
__eq__() and __ne__() |
PyOrd |
__lt__() , __le__() , __gt__() and __ge__() |
PyRichCmp |
== , != , > , >= , < and <= |
PyHash |
__hash__() |
PyIter |
__iter__() |
PyReversed |
__reversed__() |
PyLen |
__len__() |
PyDataclassFields |
__dataclass_fields__ |
PyNumeric |
Numeric op methods (__add__() etc.) |
PyBitwise |
Bitwise op methods (__and__() etc.) |
The field attributes #[pyderive(..)]
is used to customize the implementation,
like dataclasses.field()
of Python.
Module pyderive::ops
and pyderive::convert
provides
derive macros that implement individual method that enumerating numeric type (__add__()
etc.) and
called by builtin functions (__int__()
etc.).
In addition, this provides a helper derive macro:
Derive Macro | Impl |
---|---|
ToPyObject |
ToPyObject trait by IntoPy<PyObject> trait for pyclass |
It requires to enable multiple-pymethods
feature of PyO3 because this may produce multiple #[pymethods]
.
License
MIT or Apache-2.0