Skip to content

Commit

Permalink
Update README.rst
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-morel committed May 24, 2024
1 parent 9dc9c50 commit 123ae97
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,60 +26,59 @@ pyoload.annotate
----------------

Is used as a decorator on the function.

.. code-block:: python
from pyoload import annotate
@annotate
def twice(a:int) -> int:
return a * 2
b = twice(4)
The annotate creates a wrapper over the decorated function which checks in for argument types over each function call using :python`pyoload.matchType(val, spec)`.
The original function is kept in the `.__pyod_annotate__` attribute.
The annotate creates a wrapper over the decorated function which checks in for argument types over each function call using :python:`pyoload.matchType(val, spec)`.
The original function is kept in the :python:`.__pyod_annotate__` attribute.

# pyoload.overload
----------------
pyoload.overload
----------------

Implements function overloading in python via a simple decorator

```python
from pyoload import overload
import math
cache = {}
.. code-block:: python
from pyoload import overload
import math
cache = {}
tan_is_real = lambda v: not (v + 90) % 180 == 0
tan_is_real = lambda v: not (v + 90) % 180 == 0
@overload
def tan(num:Validator(tan_is_real, opposite=True)):
raise ValueError(num)
@overload
def tan(num:Validator(tan_is_real, opposite=True)):
raise ValueError(num)
@overload
def tan(num:int|float) -> float:
return math.tan(num(
@overload
def tan(num:int|float) -> float:
return math.tan(num(
tan(6)
tan(6)
```
When an overload is registerred, the function name in the form `functionModuleName.functionQualName` is goten using `pyoload.get_name(funcOrClass)` an annotate is gotten using `pyoload.annotate(func, True)`
and a new list of overloads is created and stored in `pyoload.__overloads__` dictionarry under it's name. A reference to the list of annotated overloads is stored in the functions `.__pyod_overloads__`.
When the function is called, the wrapper tries all the functions registerred to that name to catch a `pyoload.InternalAnnotationError`. If none ran succesfully, it raises an `pyoload.OverloadError`.
# Casting
-------
Casting
-------
All `pyoload.annotate` and `pyoload.overload` both support Cast objects
instances of `pyoloas.Cast`.
It uses recursive casting with integrated support for dictionaries, e.g:
```python
dict[
int,
tuple[list[float] | float]
]
```
:python:`dict[int,tuple[list[float] | float]]`
for a dictionarry mapping of integers to list of floats or floats.
> Note:
.. note::
When a union, e.g `int | str` is passed to Cast, it tries to cast in each of
the specified types in the listed order, that is
Expand All @@ -90,16 +89,16 @@ for a dictionarry mapping of integers to list of floats or floats.
Will print `'3.0'` as `3.0` can not be converted to a complex

# Accepted annotations

-----------------
Accepted annotations
--------------------
In addition to supporting normal plain types,
pyoload includes support for generic aliasses of iterable types and some other classes:
- `pyoload.Values(iterable)`
- :python:`pyoload.Values(iterable)`
e.g `Values("+-*/")` or `Values(range(6))`
- `pyoload.Cast(type)`
- :python:`pyoload.Cast(type)`
Instructs pyoload to cast to the specified type
- A string
The string contents will be evaluated as soon as first function call.

0 comments on commit 123ae97

Please sign in to comment.