Source code for qcelemental.models.types
from typing import Any, Dict
import numpy as np
class TypedArray(np.ndarray):
@classmethod
def __get_validators__(cls):
yield cls.validate
@classmethod
def validate(cls, v):
try:
v = np.asarray(v, dtype=cls._dtype)
except ValueError:
raise ValueError("Could not cast {} to NumPy Array!".format(v))
return v
@classmethod
def __modify_schema__(cls, field_schema: Dict[str, Any]) -> None:
dt = cls._dtype
if dt is int or np.issubdtype(dt, np.integer):
items = {"type": "number", "multipleOf": 1.0}
elif dt is float or np.issubdtype(dt, np.floating):
items = {"type": "number"}
elif dt is str or np.issubdtype(dt, np.string_):
items = {"type": "string"}
elif dt is bool or np.issubdtype(dt, np.bool_):
items = {"type": "boolean"}
field_schema.update(type="array", items=items)
class ArrayMeta(type):
def __getitem__(self, dtype):
return type("Array", (TypedArray,), {"_dtype": dtype})
[docs]class Array(np.ndarray, metaclass=ArrayMeta):
pass