@@ -48,7 +48,22 @@ def __init__(self, value: float, exponent: int = 0) -> None:
48
48
value: The value of this quantity in a given exponent of the base unit.
49
49
exponent: The exponent of the base unit the given value is in.
50
50
"""
51
- self ._base_value = value * 10 ** exponent
51
+ self ._base_value = value * 10.0 ** exponent
52
+
53
+ @classmethod
54
+ def _new (cls , value : float , * , exponent : int = 0 ) -> Self :
55
+ """Instantiate a new quantity subclass instance.
56
+
57
+ Args:
58
+ value: The value of this quantity in a given exponent of the base unit.
59
+ exponent: The exponent of the base unit the given value is in.
60
+
61
+ Returns:
62
+ A new quantity subclass instance.
63
+ """
64
+ self = cls .__new__ (cls )
65
+ self ._base_value = value * 10.0 ** exponent
66
+ return self
52
67
53
68
def __init_subclass__ (cls , exponent_unit_map : dict [int , str ]) -> None :
54
69
"""Initialize a new subclass of Quantity.
@@ -78,15 +93,15 @@ def __init_subclass__(cls, exponent_unit_map: dict[int, str]) -> None:
78
93
79
94
@classmethod
80
95
def zero (cls ) -> Self :
81
- """Return a quantity with value 0.
96
+ """Return a quantity with value 0.0.
82
97
83
98
Returns:
84
- A quantity with value 0.
99
+ A quantity with value 0.0.
85
100
"""
86
101
_zero = cls ._zero_cache .get (cls , None )
87
102
if _zero is None :
88
103
_zero = cls .__new__ (cls )
89
- _zero ._base_value = 0
104
+ _zero ._base_value = 0.0
90
105
cls ._zero_cache [cls ] = _zero
91
106
assert isinstance (_zero , cls )
92
107
return _zero
@@ -464,9 +479,7 @@ def from_celsius(cls, value: float) -> Self:
464
479
Returns:
465
480
A new temperature quantity.
466
481
"""
467
- power = cls .__new__ (cls )
468
- power ._base_value = value
469
- return power
482
+ return cls ._new (value )
470
483
471
484
def as_celsius (self ) -> float :
472
485
"""Return the temperature in degrees Celsius.
@@ -508,9 +521,7 @@ def from_watts(cls, watts: float) -> Self:
508
521
Returns:
509
522
A new power quantity.
510
523
"""
511
- power = cls .__new__ (cls )
512
- power ._base_value = watts
513
- return power
524
+ return cls ._new (watts )
514
525
515
526
@classmethod
516
527
def from_milliwatts (cls , milliwatts : float ) -> Self :
@@ -522,9 +533,7 @@ def from_milliwatts(cls, milliwatts: float) -> Self:
522
533
Returns:
523
534
A new power quantity.
524
535
"""
525
- power = cls .__new__ (cls )
526
- power ._base_value = milliwatts * 10 ** - 3
527
- return power
536
+ return cls ._new (milliwatts , exponent = - 3 )
528
537
529
538
@classmethod
530
539
def from_kilowatts (cls , kilowatts : float ) -> Self :
@@ -536,9 +545,7 @@ def from_kilowatts(cls, kilowatts: float) -> Self:
536
545
Returns:
537
546
A new power quantity.
538
547
"""
539
- power = cls .__new__ (cls )
540
- power ._base_value = kilowatts * 10 ** 3
541
- return power
548
+ return cls ._new (kilowatts , exponent = 3 )
542
549
543
550
@classmethod
544
551
def from_megawatts (cls , megawatts : float ) -> Self :
@@ -550,9 +557,7 @@ def from_megawatts(cls, megawatts: float) -> Self:
550
557
Returns:
551
558
A new power quantity.
552
559
"""
553
- power = cls .__new__ (cls )
554
- power ._base_value = megawatts * 10 ** 6
555
- return power
560
+ return cls ._new (megawatts , exponent = 6 )
556
561
557
562
def as_watts (self ) -> float :
558
563
"""Return the power in watts.
@@ -690,9 +695,7 @@ def from_amperes(cls, amperes: float) -> Self:
690
695
Returns:
691
696
A new current quantity.
692
697
"""
693
- current = cls .__new__ (cls )
694
- current ._base_value = amperes
695
- return current
698
+ return cls ._new (amperes )
696
699
697
700
@classmethod
698
701
def from_milliamperes (cls , milliamperes : float ) -> Self :
@@ -704,9 +707,7 @@ def from_milliamperes(cls, milliamperes: float) -> Self:
704
707
Returns:
705
708
A new current quantity.
706
709
"""
707
- current = cls .__new__ (cls )
708
- current ._base_value = milliamperes * 10 ** - 3
709
- return current
710
+ return cls ._new (milliamperes , exponent = - 3 )
710
711
711
712
def as_amperes (self ) -> float :
712
713
"""Return the current in amperes.
@@ -789,9 +790,7 @@ def from_volts(cls, volts: float) -> Self:
789
790
Returns:
790
791
A new voltage quantity.
791
792
"""
792
- voltage = cls .__new__ (cls )
793
- voltage ._base_value = volts
794
- return voltage
793
+ return cls ._new (volts )
795
794
796
795
@classmethod
797
796
def from_millivolts (cls , millivolts : float ) -> Self :
@@ -803,9 +802,7 @@ def from_millivolts(cls, millivolts: float) -> Self:
803
802
Returns:
804
803
A new voltage quantity.
805
804
"""
806
- voltage = cls .__new__ (cls )
807
- voltage ._base_value = millivolts * 10 ** - 3
808
- return voltage
805
+ return cls ._new (millivolts , exponent = - 3 )
809
806
810
807
@classmethod
811
808
def from_kilovolts (cls , kilovolts : float ) -> Self :
@@ -817,9 +814,7 @@ def from_kilovolts(cls, kilovolts: float) -> Self:
817
814
Returns:
818
815
A new voltage quantity.
819
816
"""
820
- voltage = cls .__new__ (cls )
821
- voltage ._base_value = kilovolts * 10 ** 3
822
- return voltage
817
+ return cls ._new (kilovolts , exponent = 3 )
823
818
824
819
def as_volts (self ) -> float :
825
820
"""Return the voltage in volts.
@@ -914,9 +909,7 @@ def from_watt_hours(cls, watt_hours: float) -> Self:
914
909
Returns:
915
910
A new energy quantity.
916
911
"""
917
- energy = cls .__new__ (cls )
918
- energy ._base_value = watt_hours
919
- return energy
912
+ return cls ._new (watt_hours )
920
913
921
914
@classmethod
922
915
def from_kilowatt_hours (cls , kilowatt_hours : float ) -> Self :
@@ -928,9 +921,7 @@ def from_kilowatt_hours(cls, kilowatt_hours: float) -> Self:
928
921
Returns:
929
922
A new energy quantity.
930
923
"""
931
- energy = cls .__new__ (cls )
932
- energy ._base_value = kilowatt_hours * 10 ** 3
933
- return energy
924
+ return cls ._new (kilowatt_hours , exponent = 3 )
934
925
935
926
@classmethod
936
927
def from_megawatt_hours (cls , megawatt_hours : float ) -> Self :
@@ -942,9 +933,7 @@ def from_megawatt_hours(cls, megawatt_hours: float) -> Self:
942
933
Returns:
943
934
A new energy quantity.
944
935
"""
945
- energy = cls .__new__ (cls )
946
- energy ._base_value = megawatt_hours * 10 ** 6
947
- return energy
936
+ return cls ._new (megawatt_hours , exponent = 6 )
948
937
949
938
def as_watt_hours (self ) -> float :
950
939
"""Return the energy in watt hours.
@@ -1039,9 +1028,7 @@ def from_hertz(cls, hertz: float) -> Self:
1039
1028
Returns:
1040
1029
A new frequency quantity.
1041
1030
"""
1042
- frequency = cls .__new__ (cls )
1043
- frequency ._base_value = hertz
1044
- return frequency
1031
+ return cls ._new (hertz )
1045
1032
1046
1033
@classmethod
1047
1034
def from_kilohertz (cls , kilohertz : float ) -> Self :
@@ -1053,9 +1040,7 @@ def from_kilohertz(cls, kilohertz: float) -> Self:
1053
1040
Returns:
1054
1041
A new frequency quantity.
1055
1042
"""
1056
- frequency = cls .__new__ (cls )
1057
- frequency ._base_value = kilohertz * 10 ** 3
1058
- return frequency
1043
+ return cls ._new (kilohertz , exponent = 3 )
1059
1044
1060
1045
@classmethod
1061
1046
def from_megahertz (cls , megahertz : float ) -> Self :
@@ -1067,9 +1052,7 @@ def from_megahertz(cls, megahertz: float) -> Self:
1067
1052
Returns:
1068
1053
A new frequency quantity.
1069
1054
"""
1070
- frequency = cls .__new__ (cls )
1071
- frequency ._base_value = megahertz * 10 ** 6
1072
- return frequency
1055
+ return cls ._new (megahertz , exponent = 6 )
1073
1056
1074
1057
@classmethod
1075
1058
def from_gigahertz (cls , gigahertz : float ) -> Self :
@@ -1081,9 +1064,7 @@ def from_gigahertz(cls, gigahertz: float) -> Self:
1081
1064
Returns:
1082
1065
A new frequency quantity.
1083
1066
"""
1084
- frequency = cls .__new__ (cls )
1085
- frequency ._base_value = gigahertz * 10 ** 9
1086
- return frequency
1067
+ return cls ._new (gigahertz , exponent = 9 )
1087
1068
1088
1069
def as_hertz (self ) -> float :
1089
1070
"""Return the frequency in hertz.
@@ -1152,9 +1133,7 @@ def from_percent(cls, percent: float) -> Self:
1152
1133
Returns:
1153
1134
A new percentage quantity.
1154
1135
"""
1155
- percentage = cls .__new__ (cls )
1156
- percentage ._base_value = percent
1157
- return percentage
1136
+ return cls ._new (percent )
1158
1137
1159
1138
@classmethod
1160
1139
def from_fraction (cls , fraction : float ) -> Self :
@@ -1166,9 +1145,7 @@ def from_fraction(cls, fraction: float) -> Self:
1166
1145
Returns:
1167
1146
A new percentage quantity.
1168
1147
"""
1169
- percentage = cls .__new__ (cls )
1170
- percentage ._base_value = fraction * 100
1171
- return percentage
1148
+ return cls ._new (fraction * 100 )
1172
1149
1173
1150
def as_percent (self ) -> float :
1174
1151
"""Return this quantity as a percentage.
0 commit comments