Skip to content

Commit

Permalink
Separated genutils.typetools into dedicated submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
timbernat committed Apr 19, 2024
1 parent b3173b6 commit b048c45
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 50 deletions.
50 changes: 0 additions & 50 deletions polymerist/genutils/typetools.py

This file was deleted.

1 change: 1 addition & 0 deletions polymerist/genutils/typetools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'''Additional type-hinting and typechecking not provided by Python builtins'''
22 changes: 22 additions & 0 deletions polymerist/genutils/typetools/categorical.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'''Type-hinting for Union-bound categories of types'''

from typing import Any, Callable, Literal, Type, Union
from numpy import number as np_number
from numpy.typing import ArrayLike

from .parametric import U


def _union_member_factory(union : U) -> Callable[[Any], bool]:
'''Factory for making Uion-membership-checking functions'''
def isinunion(var : Any) -> bool:
'''Check if an object is a member of a Union class'''
return isinstance(var, union.__args__)
return isinunion

Numeric = Union[int, float, complex, np_number]
JSONSerializable = Union[str, bool, int, float, tuple, list, dict]

isnumeric = _union_member_factory(Numeric)
isarraylike = _union_member_factory(ArrayLike)
isjsonserializable = _union_member_factory(JSONSerializable)
19 changes: 19 additions & 0 deletions polymerist/genutils/typetools/numpytypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'''Type aliases specific to numpy arrays'''

from typing import TypeAlias, TypeVar
import numpy as np
import numpy.typing as npt


N = TypeVar('N', bound=int) # for typing arbitrary array dimension
M = TypeVar('M', bound=int) # for typing arbitrary array dimension
DType = TypeVar('DType', bound=np.generic)
Shape : TypeAlias = tuple

Coords2D : TypeAlias = np.ndarray[Shape[M, 2], DType] # an Mx2 array of M 3D coordinates
Coords3D : TypeAlias = np.ndarray[Shape[M, 3], DType] # an Mx3 array of M 3D coordinates
CoordsND : TypeAlias = np.ndarray[Shape[M, N], DType] # an MxN array of M ND coordinates

Vector2D : TypeAlias = np.ndarray[Shape[2], DType] # a 1x2 array of M 3D coordinates
Vector3D : TypeAlias = np.ndarray[Shape[3], DType] # a 1x3 array of M 3D coordinates
VectorND : TypeAlias = np.ndarray[Shape[N], DType] # a 1xN array of M ND coordinates
17 changes: 17 additions & 0 deletions polymerist/genutils/typetools/parametric.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'''Type aliases for Python callable input parameters'''

from typing import TypeVar, ParamSpec, _UnionGenericAlias

T = TypeVar('T') # universal generic type

C = TypeVar('C') # generic type for a class
O = TypeVar('O') # generic type for an object passed to a function
F = TypeVar('F') # generic type for a function
U = TypeVar('U', bound=_UnionGenericAlias) # generic class from representing Literal Unions (since arg-free union is not supported)

P = ParamSpec('P') # for representing (preserved) input parameters
R = TypeVar('R') # for representing generic return values

# TOSELF: replace with typing.ParamSpecArgs/.ParamSpecKWArgs?
Args = TypeVar('Args' ) # generic type for arguments to a function
KWArgs = TypeVar('KWArgs') # generic type for keyword arguments to a function

0 comments on commit b048c45

Please sign in to comment.