1#!/usr/bin/env python3
2"""
3
4"""
5# Imports:
6from __future__ import annotations
7
8# ##-- stdlib imports
9import datetime
10import enum
11import functools as ftz
12import itertools as itz
13import logging as logmod
14import pathlib as pl
15import re
16import time
17import tracemalloc
18import types
19from copy import deepcopy
20from uuid import UUID, uuid1
21from weakref import ref
22
23# ##-- end stdlib imports
24
25# ##-- types
26# isort: off
27import abc
28import collections.abc
29from typing import TYPE_CHECKING, cast, assert_type, assert_never
30from typing import Generic, NewType
31# Protocols:
32from typing import Protocol, runtime_checkable
33# Typing Decorators:
34from typing import no_type_check, final, override, overload
35
36if TYPE_CHECKING:
37 from jgdv import Maybe
38 from typing import Final
39 from typing import ClassVar, Any, LiteralString
40 from typing import Never, Self, Literal
41 from typing import TypeGuard
42 from collections.abc import Iterable, Iterator, Callable, Generator
43 from collections.abc import Sequence, Mapping, MutableMapping, Hashable
44
45 from jgdv import DateTime, Seconds
46##--|
47
48# isort: on
49# ##-- end types
50
51##-- logging
52logging = logmod.getLogger(__name__)
53##-- end logging
54
[docs]
55class HumanNumbers_m:
56 """
57 Simple Mixin for human related functions
58 """
59
[docs]
60 @staticmethod
61 def humanize(val:int|float, *, force_sign:bool=True) -> str:
62 """ Format {val} in a human readable way as a size.
63 Uses tracemalloc._format_size.
64 Depending on size, will use on of the units:
65 B, KiB, MiB, GiB, TiB.
66 """
67 return tracemalloc._format_size(val, force_sign) # type:ignore[attr-defined]
68
69
[docs]
70 @staticmethod
71 def round_time(dt:DateTime=None, *, round:Seconds=60) -> DateTime:
72 """Round a datetime object to any time lapse in seconds
73 dt : datetime.datetime object, default now.
74 round : Closest number of seconds to round to, default 1 minute.
75 Author: Thierry Husson 2012 - Use it as you want but don't blame me.
76 from: https://stackoverflow.com/questions/3463930
77 """
78 dt = dt or datetime.datetime.now()
79 seconds = (dt.replace(tzinfo=None) - dt.min).seconds
80 rounding = (seconds+round/2) // round * round
81 return dt + datetime.timedelta(0,rounding-seconds,-dt.microsecond)