analog_daddy.units
¶
Pydantic ready quantiphy.Quantity units
Examples:
>>> from pydantic import BaseModel
>>> class RC(BaseModel):
... resistance: Resistance
... time: Time
>>> rc = RC(resistance="4.7 kOhm", time="1n")
>>> rc.resistance
Resistance('4.7 kOhm')
>>> rc.time
Time('1 ns')
>>> rc.model_dump_json()
'{"resistance":4700.0,"time":1e-9}'
>>> rc.model_dump_json(context={"quantity_format": "str"})
'{"resistance":"4.7 kOhm","time":"1 ns"}'
>>> rc.model_dump_json(context={"quantity_format": "as_tuple"})
'{"resistance":[4700.0,"Ohm"],"time":[1e-9,"s"]}'
>>> class SlewRate(Quantity):
... units: str = "V/s"
>>> SlewRate("1.5 MV/s").as_tuple()
(1500000.0, 'V/s')
Notes
The following approaches were considered based on Pydantic docs and discussions with a LLM model for parts which confused me.
Chosen approach: Put the pydantic hook on the type itself. Works everywhere automatically, best autocomplete, but ties Time to pydantic and uses a lower-level API pydantic calls experimental.
- Annotate as str, secretly swap it to Time in a validator. Rejected since: the type hint lie, so autocomplete/type-checker/serialization all get confused.
- model_validator(mode="after"). Only makes sense when one field needs to be derived from another (cross-field logic), not for a single field's own conversion.
- Approach from pydantic_pint seemed unncessary, but might have to consider if there are any issues.
Capacitance
¶
Bases: Quantity
Capacitance quantity with units of Farads.
Attributes:
| Name | Type | Description |
|---|---|---|
units |
str, default="F"
|
Unit symbol. |
Examples:
Source code in src/analog_daddy/units.py
Current
¶
Bases: Quantity
Current quantity with units of Amperes.
Attributes:
| Name | Type | Description |
|---|---|---|
units |
str, default="A"
|
Unit symbol. |
Examples:
Source code in src/analog_daddy/units.py
Frequency
¶
Bases: Quantity
Frequency quantity with units of Hertz.
Attributes:
| Name | Type | Description |
|---|---|---|
units |
str, default="Hz"
|
Unit symbol. |
Examples:
Source code in src/analog_daddy/units.py
Inductance
¶
Bases: Quantity
Inductance quantity with units of Henry.
Attributes:
| Name | Type | Description |
|---|---|---|
units |
str, default="H"
|
Unit symbol. |
Examples:
Source code in src/analog_daddy/units.py
Length
¶
Bases: Quantity
Length quantity with units of meters.
Attributes:
| Name | Type | Description |
|---|---|---|
units |
str, default="m"
|
Unit symbol. |
Examples:
Source code in src/analog_daddy/units.py
Power
¶
Bases: Quantity
Power quantity with units of Watts.
Attributes:
| Name | Type | Description |
|---|---|---|
units |
str, default="W"
|
Unit symbol. |
Examples:
Source code in src/analog_daddy/units.py
Quantity
¶
Bases: Quantity
Wrapper around quantiphy.Quantity to add Pydantic support.
Source code in src/analog_daddy/units.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
__get_pydantic_core_schema__(_source_type, _handler)
classmethod
¶
Generate a Pydantic core schema for the Quantity type.
The schema is union based with the serialization support for as_real, str, and as_tuple serialization options using the context dictionary.
Source code in src/analog_daddy/units.py
__get_pydantic_json_schema__(_core_schema, handler)
classmethod
¶
Generate a JSON schema for the Quantity type.
Source code in src/analog_daddy/units.py
Resistance
¶
Bases: Quantity
Resistance quantity with units of Ohms/ohm/etc.
Attributes:
| Name | Type | Description |
|---|---|---|
units |
str, default="Ohm"
|
Unit symbol. |
Examples:
>>> Resistance("5 ohms")
Resistance('5 Ohm')
>>> Resistance("5 mΩ")
Resistance('5 mOhm')
>>> Resistance(50)
Resistance('50 Ohm')
Source code in src/analog_daddy/units.py
Temperature
¶
Bases: Quantity
Temperature quantity with units of Celsius.
Attributes:
| Name | Type | Description |
|---|---|---|
units |
str, default="C"
|
Unit symbol. |
Examples:
Source code in src/analog_daddy/units.py
Time
¶
Bases: Quantity
Time quantity with units of seconds.
Attributes:
| Name | Type | Description |
|---|---|---|
units |
str, default="s"
|
Unit symbol. |
Examples:
Source code in src/analog_daddy/units.py
Voltage
¶
Bases: Quantity
Voltage quantity with units of Volts.
Attributes:
| Name | Type | Description |
|---|---|---|
units |
str, default="V"
|
Unit symbol. |
Examples: