unitcorn 0.9.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of unitcorn might be problematic. Click here for more details.
- unitcorn/__init__.py +110 -0
- unitcorn/airQuality/__init__.py +99 -0
- unitcorn/area/__init__.py +3 -0
- unitcorn/area/area.py +103 -0
- unitcorn/base/Decorators.py +72 -0
- unitcorn/base/Registry.py +75 -0
- unitcorn/base/SmartBases.py +26 -0
- unitcorn/base/_Algebra.py +205 -0
- unitcorn/base/_Measurement.py +1122 -0
- unitcorn/base/_ScalingMeasurement.py +398 -0
- unitcorn/base/_SmartFloat.py +1635 -0
- unitcorn/base/__init__.py +6 -0
- unitcorn/config/__init__.py +318 -0
- unitcorn/config/__main__.py +8 -0
- unitcorn/config/si.toml +53 -0
- unitcorn/config/template.toml +146 -0
- unitcorn/config/us.toml +53 -0
- unitcorn/defaults/WeatherFlow/__init__.py +176 -0
- unitcorn/defaults/__init__.py +1 -0
- unitcorn/derived/__init__.py +16 -0
- unitcorn/derived/density.py +26 -0
- unitcorn/derived/partsPer.py +96 -0
- unitcorn/derived/precipitation.py +96 -0
- unitcorn/derived/rate.py +80 -0
- unitcorn/derived/volume.py +122 -0
- unitcorn/derived/wind.py +54 -0
- unitcorn/digital/__init__.py +21 -0
- unitcorn/energy/__init__.py +4 -0
- unitcorn/energy/energy.py +120 -0
- unitcorn/energy/power.py +83 -0
- unitcorn/errors/Conversion.py +12 -0
- unitcorn/errors/Unit.py +4 -0
- unitcorn/errors/__init__.py +7 -0
- unitcorn/length/__init__.py +19 -0
- unitcorn/length/imperial.py +46 -0
- unitcorn/length/length.py +74 -0
- unitcorn/length/metric.py +56 -0
- unitcorn/mass/__init__.py +15 -0
- unitcorn/mass/imperial.py +47 -0
- unitcorn/mass/mass.py +57 -0
- unitcorn/mass/metric.py +47 -0
- unitcorn/others/__init__.py +338 -0
- unitcorn/others/light.py +51 -0
- unitcorn/pressure/__init__.py +3 -0
- unitcorn/pressure/pressure.py +189 -0
- unitcorn/py.typed +0 -0
- unitcorn/temperature/__init__.py +19 -0
- unitcorn/temperature/celsius.py +23 -0
- unitcorn/temperature/change.py +156 -0
- unitcorn/temperature/fahrenheit.py +24 -0
- unitcorn/temperature/kelvin.py +25 -0
- unitcorn/temperature/temperature.py +154 -0
- unitcorn/time_/__init__.py +1 -0
- unitcorn/time_/time.py +387 -0
- unitcorn/utils.py +399 -0
- unitcorn-0.9.0.dist-info/METADATA +536 -0
- unitcorn-0.9.0.dist-info/RECORD +59 -0
- unitcorn-0.9.0.dist-info/WHEEL +4 -0
- unitcorn-0.9.0.dist-info/licenses/LICENSE.txt +17 -0
unitcorn/time_/time.py
ADDED
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from datetime import timedelta, datetime
|
|
3
|
+
from uuid import uuid4
|
|
4
|
+
|
|
5
|
+
from math import inf
|
|
6
|
+
|
|
7
|
+
__all__ = ['Time']
|
|
8
|
+
|
|
9
|
+
from typing import Mapping
|
|
10
|
+
|
|
11
|
+
from ..base.Decorators import UnitType
|
|
12
|
+
from ..base import Dimension, both
|
|
13
|
+
from ..base import ScalingMeasurement, Scale, FormatSpec
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@UnitType
|
|
17
|
+
class Time(ScalingMeasurement, metaclass=Dimension, system=both, symbol='T', baseUnit='Second'):
|
|
18
|
+
_limits = -inf, inf
|
|
19
|
+
Millisecond: type
|
|
20
|
+
Second: type
|
|
21
|
+
Minute: type
|
|
22
|
+
Hour: type
|
|
23
|
+
Day: type
|
|
24
|
+
Week: type
|
|
25
|
+
Month: type
|
|
26
|
+
Year: type
|
|
27
|
+
Decade: type
|
|
28
|
+
Century: type
|
|
29
|
+
Millennia: type
|
|
30
|
+
|
|
31
|
+
_acceptedTypes = (timedelta,)
|
|
32
|
+
|
|
33
|
+
class _Scale(Scale):
|
|
34
|
+
Millisecond = 1/1000
|
|
35
|
+
Second = 1
|
|
36
|
+
Minute = 60
|
|
37
|
+
Hour = 60
|
|
38
|
+
Day = 24
|
|
39
|
+
Year = 365
|
|
40
|
+
Decade = 10
|
|
41
|
+
Century = 10
|
|
42
|
+
Millennia = 10
|
|
43
|
+
Base = 'Second'
|
|
44
|
+
Week = 7.0*float(Day*Hour*Minute*Second)
|
|
45
|
+
Month = 30.436875*float(Day*Hour*Minute*Second)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
common = {'Decade', 'Year', 'Day', 'Hour', 'Minute', 'Second', 'Millisecond'}
|
|
49
|
+
|
|
50
|
+
# Which unit is sensible at which magnitude. Each rung reads "below
|
|
51
|
+
# <amount> <unit>, show <unit>"; the first rung that fits wins and anything
|
|
52
|
+
# past the last rung is shown in `ladder_top`. Used by the `human` and `ago`
|
|
53
|
+
# specs and by `Time.humanized`. This is a meaning-of-the-number table, not
|
|
54
|
+
# a width constraint: `digit_budget` is unrelated and stays one.
|
|
55
|
+
#
|
|
56
|
+
# under 90 s -> seconds (a minute and a half is still "90 s" to a person)
|
|
57
|
+
# under 90 min -> minutes
|
|
58
|
+
# under 30 h -> hours (33 hrs is not a useful reading, 1.4 days is; the
|
|
59
|
+
# first draft said 36 h, which would have left 33 hrs alone)
|
|
60
|
+
# under 14 d -> days
|
|
61
|
+
# under 8 wk -> weeks
|
|
62
|
+
# under 18 mth -> months
|
|
63
|
+
# else -> years
|
|
64
|
+
ladder = (
|
|
65
|
+
(90, 'Second', 'Second'),
|
|
66
|
+
(90, 'Minute', 'Minute'),
|
|
67
|
+
(30, 'Hour', 'Hour'),
|
|
68
|
+
(14, 'Day', 'Day'),
|
|
69
|
+
(8, 'Week', 'Week'),
|
|
70
|
+
(18, 'Month', 'Month'),
|
|
71
|
+
)
|
|
72
|
+
ladder_top = 'Year'
|
|
73
|
+
|
|
74
|
+
def __new__(cls, value: float | datetime | timedelta, scale: Scale = None):
|
|
75
|
+
match value:
|
|
76
|
+
case float(value) | int(value):
|
|
77
|
+
pass
|
|
78
|
+
case datetime():
|
|
79
|
+
value = datetime.now().timestamp() - value.timestamp()
|
|
80
|
+
value = Second(value)
|
|
81
|
+
if cls is type(value):
|
|
82
|
+
return value
|
|
83
|
+
case timedelta():
|
|
84
|
+
value = value.total_seconds()
|
|
85
|
+
value = Second(value)
|
|
86
|
+
if cls is type(value):
|
|
87
|
+
return value
|
|
88
|
+
return super().__new__(cls, value, scale)
|
|
89
|
+
|
|
90
|
+
def __format__(self, format_spec: str, **extras) -> str:
|
|
91
|
+
|
|
92
|
+
original_spec = format_spec
|
|
93
|
+
|
|
94
|
+
if precisionSpec := FormatSpec.precision.search(format_spec):
|
|
95
|
+
format_spec = format_spec[:precisionSpec.start()-1] if format_spec.endswith(precisionSpec.group()) else format_spec[precisionSpec.end()+1:]
|
|
96
|
+
precisionSpec = precisionSpec.groupdict()
|
|
97
|
+
else:
|
|
98
|
+
precisionSpec = {'format_spec': ''}
|
|
99
|
+
|
|
100
|
+
if format_spec == 'timestamp':
|
|
101
|
+
return self._formatTimestamp()
|
|
102
|
+
elif format_spec.startswith('human'):
|
|
103
|
+
if format_spec[-1] == '+':
|
|
104
|
+
extras['unit_type'] = 'name'
|
|
105
|
+
# The sign is put on by hand: the base formatter sends any value
|
|
106
|
+
# that is not above 1 down its sub-1 branch, so -1.375 days comes
|
|
107
|
+
# out as '-1 days' where 1.375 days comes out as '1.4 days'.
|
|
108
|
+
humanized = self.humanized
|
|
109
|
+
sign = '-' if humanized < 0 else ''
|
|
110
|
+
return sign + abs(humanized).__format__('simple', **extras)
|
|
111
|
+
elif format_spec.startswith('simple'):
|
|
112
|
+
extras['unit_spacer'] = ' '
|
|
113
|
+
if format_spec[-1] == '+':
|
|
114
|
+
extras['unit_type'] = 'name'
|
|
115
|
+
if format_spec := precisionSpec['format_spec']:
|
|
116
|
+
format_spec += ':'
|
|
117
|
+
format_spec = "shorten=True, plural=True"
|
|
118
|
+
return super().__format__(format_spec, **extras)
|
|
119
|
+
elif format_spec.startswith('ago'):
|
|
120
|
+
if format_spec[-1] == '+':
|
|
121
|
+
extras['unit_type'] = 'name'
|
|
122
|
+
if self < 0:
|
|
123
|
+
return f'in {abs(self).__format__("human", **extras)}'
|
|
124
|
+
return f'{self.__format__("human", **extras)} ago'
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
units = 'ymwdHMS'
|
|
128
|
+
matches = list(i.groupdict() for i in re.finditer(rf'%(?P<full>(?P<leading>[-#])?(?P<char>[{units}])(?P=char)*)', format_spec))
|
|
129
|
+
params = {}
|
|
130
|
+
trailingID = str(uuid4())
|
|
131
|
+
for group in sorted(matches, key=lambda x: format_spec.index(x['full'])):
|
|
132
|
+
padding = len(group['full'].strip('-#')) if not group['leading'] else 0
|
|
133
|
+
match group['char']:
|
|
134
|
+
case 'y':
|
|
135
|
+
paramID = f'year_{trailingID}'
|
|
136
|
+
newValue = f'{{{paramID}:0{padding}}}'
|
|
137
|
+
params[paramID] = (self.year if not params else abs(self.year)).onlyInt
|
|
138
|
+
case 'm':
|
|
139
|
+
paramID = f'month_{trailingID}'
|
|
140
|
+
newValue = f'{{{paramID}:0{padding}}}'
|
|
141
|
+
params[paramID] = (self.month if not params else abs(self.month)).onlyInt
|
|
142
|
+
case 'w':
|
|
143
|
+
paramID = f'week_{trailingID}'
|
|
144
|
+
newValue = f'{{{paramID}:0{padding}}}'
|
|
145
|
+
params[paramID] = (self.week if not params else abs(self.week)).onlyInt
|
|
146
|
+
case 'd':
|
|
147
|
+
paramID = f'day_{trailingID}'
|
|
148
|
+
newValue = f'{{{paramID}:0{padding}}}'
|
|
149
|
+
params[paramID] = (self.day if not params else abs(self.day)).onlyInt
|
|
150
|
+
case 'H':
|
|
151
|
+
paramID = f'hour_{trailingID}'
|
|
152
|
+
newValue = f'{{{paramID}:0{padding}}}'
|
|
153
|
+
params[paramID] = (self.hour if not params else abs(self.hour)).onlyInt
|
|
154
|
+
case 'M':
|
|
155
|
+
paramID = f'minute_{trailingID}'
|
|
156
|
+
newValue = f'{{{paramID}:0{padding}}}'
|
|
157
|
+
params[paramID] = (self.minute if not params else abs(self.minute)).onlyInt
|
|
158
|
+
case 'S':
|
|
159
|
+
paramID = f'second_{trailingID}'
|
|
160
|
+
newValue = f'{{{paramID}:0{padding}}}'
|
|
161
|
+
params[paramID] = (self.second if not params else abs(self.second)).onlyInt
|
|
162
|
+
case _:
|
|
163
|
+
newValue = f'%{group["full"]}'
|
|
164
|
+
format_spec = format_spec.replace(f'%{group["full"]}', newValue)
|
|
165
|
+
if matches:
|
|
166
|
+
format_spec = {'format': format_spec, **params}
|
|
167
|
+
return super().__format__(format_spec)
|
|
168
|
+
|
|
169
|
+
def _formatTimestamp(self) -> str:
|
|
170
|
+
"""
|
|
171
|
+
Clock form, escalating with magnitude.
|
|
172
|
+
|
|
173
|
+
===================== ==================
|
|
174
|
+
under an hour ``MM:SS``
|
|
175
|
+
an hour up to 100 h ``H:MM:SS``
|
|
176
|
+
over 100 h ``Dd H:MM:SS``
|
|
177
|
+
a year or more ``Yy Dd H:MM:SS``
|
|
178
|
+
===================== ==================
|
|
179
|
+
|
|
180
|
+
Seconds are rounded to the nearest whole second, a negative value keeps
|
|
181
|
+
its sign in front, and a year is 365 days. The year prefix only appears
|
|
182
|
+
once there is a whole year to show, so 101 days reads ``101d 0:00:00``
|
|
183
|
+
and 400 days reads ``1y 35d 0:00:00``.
|
|
184
|
+
|
|
185
|
+
This does the arithmetic itself rather than going through ``onlyInt``:
|
|
186
|
+
``Time.common`` holds ``Scale`` members, which ``splitInto`` then tries to
|
|
187
|
+
call as types.
|
|
188
|
+
"""
|
|
189
|
+
total = float(self.second)
|
|
190
|
+
sign = '-' if total < 0 else ''
|
|
191
|
+
total = round(abs(total))
|
|
192
|
+
days, rest = divmod(total, 86400)
|
|
193
|
+
hours, rest = divmod(rest, 3600)
|
|
194
|
+
minutes, seconds = divmod(rest, 60)
|
|
195
|
+
if total < 3600:
|
|
196
|
+
return f'{sign}{minutes:02}:{seconds:02}'
|
|
197
|
+
if total <= 100*3600:
|
|
198
|
+
return f'{sign}{total//3600}:{minutes:02}:{seconds:02}'
|
|
199
|
+
clock = f'{hours}:{minutes:02}:{seconds:02}'
|
|
200
|
+
if days >= 365:
|
|
201
|
+
years, days = divmod(days, 365)
|
|
202
|
+
return f'{sign}{years}y {days}d {clock}'
|
|
203
|
+
return f'{sign}{days}d {clock}'
|
|
204
|
+
|
|
205
|
+
@property
|
|
206
|
+
def humanized(self) -> 'Time':
|
|
207
|
+
"""
|
|
208
|
+
This duration in the unit :attr:`ladder` says is sensible for its
|
|
209
|
+
magnitude (``Hour(33).humanized`` is ``Day(1.375)``). The sign is kept;
|
|
210
|
+
the choice is made on the magnitude.
|
|
211
|
+
"""
|
|
212
|
+
seconds = abs(float(self.second))
|
|
213
|
+
for amount, thresholdUnit, unit in self.ladder:
|
|
214
|
+
if seconds < amount*getattr(Time, thresholdUnit).scale.absoluteValue:
|
|
215
|
+
return getattr(Time, unit)(self)
|
|
216
|
+
return getattr(Time, self.ladder_top)(self)
|
|
217
|
+
|
|
218
|
+
def __format_value__(self, params: Mapping) -> str:
|
|
219
|
+
|
|
220
|
+
return super().__format_value__(params)
|
|
221
|
+
|
|
222
|
+
def _convert(self, other):
|
|
223
|
+
if isinstance(other, datetime):
|
|
224
|
+
other = Second(other.timestamp() - datetime.now().timestamp())
|
|
225
|
+
if isinstance(other, timedelta):
|
|
226
|
+
other = Second(other.total_seconds())
|
|
227
|
+
return super()._convert(other)
|
|
228
|
+
|
|
229
|
+
def _millisecond(self):
|
|
230
|
+
return self.changeScale(self.scale.Millisecond)
|
|
231
|
+
|
|
232
|
+
def _second(self):
|
|
233
|
+
return self.changeScale(self.scale.Second)
|
|
234
|
+
|
|
235
|
+
def _minute(self):
|
|
236
|
+
return self.changeScale(self.scale.Minute)
|
|
237
|
+
|
|
238
|
+
def _hour(self):
|
|
239
|
+
return self.changeScale(self.scale.Hour)
|
|
240
|
+
|
|
241
|
+
def _day(self):
|
|
242
|
+
return self.changeScale(self.scale.Day)
|
|
243
|
+
|
|
244
|
+
def _year(self):
|
|
245
|
+
return self.changeScale(self.scale.Year)
|
|
246
|
+
|
|
247
|
+
def _month(self):
|
|
248
|
+
return float(Month(self))
|
|
249
|
+
|
|
250
|
+
def _decade(self):
|
|
251
|
+
return self.changeScale(self.scale.Decade)
|
|
252
|
+
|
|
253
|
+
def _century(self):
|
|
254
|
+
return self.changeScale(self.scale.Century)
|
|
255
|
+
|
|
256
|
+
@property
|
|
257
|
+
def timedelta(self) -> timedelta:
|
|
258
|
+
return timedelta(seconds=self.second)
|
|
259
|
+
|
|
260
|
+
def _millennia(self):
|
|
261
|
+
return self.changeScale(self.scale.Millennia)
|
|
262
|
+
|
|
263
|
+
@property
|
|
264
|
+
def millisecond(self):
|
|
265
|
+
return Millisecond(self)
|
|
266
|
+
|
|
267
|
+
@property
|
|
268
|
+
def second(self):
|
|
269
|
+
return Second(self)
|
|
270
|
+
|
|
271
|
+
@property
|
|
272
|
+
def minute(self):
|
|
273
|
+
return Minute(self)
|
|
274
|
+
|
|
275
|
+
@property
|
|
276
|
+
def hour(self):
|
|
277
|
+
return Hour(self)
|
|
278
|
+
|
|
279
|
+
@property
|
|
280
|
+
def day(self):
|
|
281
|
+
return Day(self)
|
|
282
|
+
|
|
283
|
+
@property
|
|
284
|
+
def week(self):
|
|
285
|
+
return Week(self)
|
|
286
|
+
|
|
287
|
+
@property
|
|
288
|
+
def month(self):
|
|
289
|
+
return Month(self)
|
|
290
|
+
|
|
291
|
+
@property
|
|
292
|
+
def year(self):
|
|
293
|
+
return Year(self)
|
|
294
|
+
|
|
295
|
+
@property
|
|
296
|
+
def decade(self):
|
|
297
|
+
return Decade(self)
|
|
298
|
+
|
|
299
|
+
@property
|
|
300
|
+
def century(self):
|
|
301
|
+
return Century(self)
|
|
302
|
+
|
|
303
|
+
@property
|
|
304
|
+
def millennia(self):
|
|
305
|
+
return Millennia(self)
|
|
306
|
+
|
|
307
|
+
## abbreviations
|
|
308
|
+
y = year
|
|
309
|
+
d = day
|
|
310
|
+
min = minute
|
|
311
|
+
m = minute
|
|
312
|
+
hr = hour
|
|
313
|
+
h = hour
|
|
314
|
+
s = second
|
|
315
|
+
sec = second
|
|
316
|
+
ms = millisecond
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
class Millisecond(Time):
|
|
320
|
+
_unit = 'ms'
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
class Second(Time, alias='sec'):
|
|
324
|
+
_unit = 's'
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
class Minute(Time):
|
|
328
|
+
_precision = 1
|
|
329
|
+
_unit = 'min'
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
class Hour(Time):
|
|
333
|
+
_unit = 'hr'
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
class Day(Time, plural_name='Days', plural_unit='days'):
|
|
337
|
+
_unit = 'd'
|
|
338
|
+
_digit_budget = 2
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
class Week(Time):
|
|
342
|
+
_unit = 'wk'
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
class Month(Time):
|
|
346
|
+
_unit = 'mth'
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
class Year(Time):
|
|
350
|
+
_unit = 'yr'
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
class Decade(Time):
|
|
354
|
+
_unit = 'dec'
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
class Century(Time):
|
|
358
|
+
_unit = 'cen'
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
class Millennia(Time):
|
|
362
|
+
_unit = 'mel'
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
Time.Millisecond = Millisecond
|
|
366
|
+
Time.Second = Second
|
|
367
|
+
Time.Minute = Minute
|
|
368
|
+
Time.Hour = Hour
|
|
369
|
+
Time.Day = Day
|
|
370
|
+
Time.Week = Week
|
|
371
|
+
Time.Month = Month
|
|
372
|
+
Time.Year = Year
|
|
373
|
+
Time.Decade = Decade
|
|
374
|
+
Time.Century = Century
|
|
375
|
+
Time.Millennia = Millennia
|
|
376
|
+
|
|
377
|
+
Time.aMillisecond = Time.oneMillisecond = Millisecond.one = Millisecond(1)
|
|
378
|
+
Time.aSecond = Time.oneSecond = Second.one = Second(1)
|
|
379
|
+
Time.aMinute = Time.oneMinute = Minute.one = Minute(1)
|
|
380
|
+
Time.anHour = Time.oneHour = Hour.one = Hour(1)
|
|
381
|
+
Time.aDay = Time.oneDay = Day.one = Day(1)
|
|
382
|
+
Time.aWeek = Time.oneWeek = Week.one = Week(1)
|
|
383
|
+
Time.aMonth = Time.oneMonth = Month.one = Month(1)
|
|
384
|
+
Time.aYear = Time.oneYear = Year.one = Year(1)
|
|
385
|
+
Time.aDecade = Time.oneDecade = Decade.one = Decade(1)
|
|
386
|
+
Time.aCentury = Time.oneCentury = Century.one = Century(1)
|
|
387
|
+
Time.aMillennia = Time.oneMillennia = Millennia.one = Millennia(1)
|