Verified Commit db949e6e authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Expand README docs a bit

parent f266c920
Loading
Loading
Loading
Loading
+54 −8
Changes for README.md: 54 added lines, 8 removed lines.
Original line number Diff line number Diff line
Physical Quantities
===================

This package provides types for representing physical quantities, which have a magnitude and 
a unit.

An in-depth usage guide is available in the module's docstring.
This package provides types for representing fixed-precision quantities, which have a scalar 
value and a unit of measurement.  The package uses integer scalars only, with the precision 
defined by the unit value; for example if a distance `QuantityUnit` enum is defined with 
`MILLIMETRE = 10` the precision is tenths of a millimetre, or $1\times10^{-10}$ of a metre.

At runtime the values are nothing more than an integer number of the unit value.  For 
instance a time value in microsecond precision would represent a 1.5 seconds value as the 
integer `1500000000`.  These values can be created by multiplying by a suitable constant, 
i.e. `my_time: Quantity[Time] = 1.5 @ Time.SECONDS`; and converted back to a non-quantity 
`float` or `int` with the appropriate `>>` or `//` division operations[^operators], i.e. 
`my_time_ms: int = my_time // Time.MILLISECONDS`.  Note that the multiplication of a `float` 
with the `Time` constant creates an integer `Quantity[Time]` value, not another `float` as 
Python would normally do.

Type checkers treat variables and arguments typed as `Quantity[...]` as a wholly separate 
type and will not allow them to be passed without conversion to interfaces that expect 
a numeric value of a specific unit of measurement.  For instance, passing `my_time` to the 
`time.sleep()` function would cause a type checker to correctly issue an error, as instead 
of sleeping for 1.5 seconds it would sleep for nearly half a millennium.

This is the beauty of this approach: it enforces the usage of a variable as a quantity of 
a specific type and makes it scaling agnostic. When a boundary is met that naïvely requires 
an untyped numeric value with a specific scaling, an explicit conversion is required by type 
checkers.

[^operators]: See (Choice of Operators)[#choice-of-operators] for information about the `@` 
  and `>>` operators.


Quick Start
-----------

Create units by creating an enum class subclassing `QuantityUnit`.  The value of each item 
is scaled to its siblings, with an appropriate value chosen as the smallest representable 
quantity which forms the base precision when scaling quantities.  For example, units of 
distance where the smallest possible quantity is 0.1mm:
Create scaled units of measurement by creating an enum class subclassing `QuantityUnit`.  
The value of each item is scaled to its siblings, with an appropriate value chosen as the 
smallest representable quantity which forms the base precision when scaling quantities.  For 
example, units of distance where the smallest possible quantity is 0.1mm:

```python
from kodo.quantities import QuantityUnit
@@ -31,8 +54,11 @@ class Distance(QuantityUnit):
    HALF_INCH = 8 * SIXTEENTH_INCH
    INCH = 16 * SIXTEENTH_INCH
    FOOT = 12 * INCH
```

Or units of time were the smallest possible quantity is 1ms:

```python
class Time(QuantityUnit):

    MILLISECONDS = 1
@@ -99,3 +125,23 @@ item itself can be used, matching the '>>' operator:
assert (3.6 @ Time.SECONDS) // (1 @ Time.SECONDS) is 3
assert (3.6 @ Time.SECONDS) // Time.SECONDS is 3
```


Choice of Operators
-------------------

The operators for constructing ("@") and deconstructing (">>") quantities may seem a bit
odd, given that what they actually do is multiply and divide the values. They were chosen to
be visually distinct from other multiplication and division operations on quantities and
scalar units.

The matrix multiplication operator therefore replaces the scalar multiplication operator,
while the shift operator, which looks arrow-like, is used to convert to the indicated unit.
i.e.:

>>> # quantity  (converted to)  units
>>> delay             >>        Time.SECONDS
3.6

See https://code.kodo.org.uk/dom/python-quantities/-/work_items/1 for further information on 
the future use of these operators and to weigh in on whether they should be changed.
−17
Changes for kodo/quantities/__init__.py: 0 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -168,23 +168,6 @@ floats or ints of a particular unit:

>>> assert (3.6 @ Time.SECONDS) % Time.SECONDS >> Time.SECONDS == 0.6
>>> assert (3.6 @ Time.SECONDS) % Time.SECONDS // Time.MILLISECONDS == 600


Choice of Operators
-------------------

The operators for constructing ("@") and deconstructing (">>") quantities may seem a bit
odd, given that what they actually do is multiply and divide the values. They were chosen to
be visually distinct from other multiplication and division operations on quantities and
scalar units.

The matrix multiplication operator therefore replaces the scalar multiplication operator,
while the shift operator, which looks arrow-like, is used to convert to the indicated unit.
i.e.:

>>> # quantity  (converted to)  units
>>> delay             >>        Time.SECONDS
3.6
"""

from __future__ import annotations