Karana.KUtils.Ktyping#
Module containing utilities for modeling and simulation.
- These utilities include:
DataStruct - Used to validate data and catch errors early.
Kclick - Used to easily generate high-quality CLI applications.
Kquantities - Used to check and convert quantities to the units system used by the simulation.
Submodules#
Attributes#
Functions#
|
Determine if the given value is one of the known quantities. |
|
Ensure that the numpy array is the given shape. |
|
Ensure that if the value is a quantity, that it is the specific provided quantity. |
|
Ensure the given value is within prec of norm. |
|
Validate an incoming object. |
|
Serialize an object using its ID rather than the object itself. |
|
Create pydantic functions to serialize object as ID. |
Package Contents#
- Karana.KUtils.Ktyping.isQuantity(v: quantities.Quantity | numpy.typing.NDArray, quantity: quantities.Quantity)[source]#
Determine if the given value is one of the known quantities.
- Karana.KUtils.Ktyping.mass: quantities.Quantity#
- Karana.KUtils.Ktyping.inertia: quantities.Quantity#
- Karana.KUtils.Ktyping.length: quantities.Quantity#
- Karana.KUtils.Ktyping.velocity: quantities.Quantity#
- Karana.KUtils.Ktyping.acceleration: quantities.Quantity#
- Karana.KUtils.Ktyping.force: quantities.Quantity#
- Karana.KUtils.Ktyping.torque: quantities.Quantity#
- Karana.KUtils.Ktyping.angular_velocity: quantities.Quantity#
- Karana.KUtils.Ktyping.gravitational_parameter: quantities.Quantity#
- Karana.KUtils.Ktyping.MATH_EPSILON: float = 1e-12#
- Karana.KUtils.Ktyping.npSizeCheck(shape: tuple[int, Ellipsis]) pydantic.AfterValidator[source]#
Ensure that the numpy array is the given shape.
- Parameters:
shape (tuple[int, ...]) – Desired shape of the array. A value of -1 indicates that, that particular axis can be any size. For example, a shape of (3, -1, 2) indicates the first axis must be size 3, the second axis can be any size, and the third axis must be size 2.
- Karana.KUtils.Ktyping.optionallyWithUnits(quantity: quantities.Quantity) pydantic.AfterValidator[source]#
Ensure that if the value is a quantity, that it is the specific provided quantity.
If it has no units, then apply the default units for that quantity to it.
- Parameters:
quantity (pq.Quantity) – The quantity we want the value to be if it is a Quantity. If it is not a Quantity, then apply the defaults units for this Quantity to the value.
- Karana.KUtils.Ktyping.normCheck(norm: float, prec: float = MATH_EPSILON) pydantic.AfterValidator[source]#
Ensure the given value is within prec of norm.
- Parameters:
norm (float) – The value we want the norm to be.
prec (float) – The tolerance for the value to be within that norm.
- Karana.KUtils.Ktyping.Vec#
- Karana.KUtils.Ktyping.Mat#
- Karana.KUtils.Ktyping.Vec3#
- Karana.KUtils.Ktyping.Mat33#
- Karana.KUtils.Ktyping.Mat66#
- Karana.KUtils.Ktyping.Mat6n#
- Karana.KUtils.Ktyping.FloatOrQuantity#
- Karana.KUtils.Ktyping.Mass#
- Karana.KUtils.Ktyping.Length3#
- Karana.KUtils.Ktyping.Velocity3#
- Karana.KUtils.Ktyping.AngularVelocity3#
- Karana.KUtils.Ktyping.Inertia#
- Karana.KUtils.Ktyping.Acceleration3#
- Karana.KUtils.Ktyping.Force3#
- Karana.KUtils.Ktyping.Torque3#
- Karana.KUtils.Ktyping.GravitationalParameter#
- Karana.KUtils.Ktyping.beforeVal(val: int | Any, allow_None: bool = True) Any[source]#
Validate an incoming object.
If this is an integer, then us IdMixin to look up the object that corresponds to that integer.
- Parameters:
val (int | Any) – The value to validate.
allow_None (bool) – If True, then if the val does not correspond to an object that has been created, then return None. If False, then throw an error val does not correspond to an object that has been created.
- Returns:
The value.
- Return type:
Any
- Karana.KUtils.Ktyping.ser(val: Any) int | None[source]#
Serialize an object using its ID rather than the object itself.
- Parameters:
val (Any) – Use the id() method to get the value to serialize.
- Returns:
This returns an integer if Value is none None, and None otherwise.
- Return type:
int | None
- Karana.KUtils.Ktyping.SerializeAsId(allow_None: bool = True) tuple[pydantic.BeforeValidator, pydantic.PlainSerializer][source]#
Create pydantic functions to serialize object as ID.
This is typically used like:
- class MyDataStruct(DataStruct):
body: Annotated[PhysicalBody, *SerializeAsId()]
- Parameters:
allow_None (bool) – This affects deserialization. If True, then if the ID does not correspond to an object that has been created, then return None. If False, then throw an error if val does not correspond to an object that has been created.
- Returns:
A validator function and serializer function for the pydantic field to serialize as an ID.
- Return type:
tuple[BeforeValidator, PlainSerializer]