- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathprefix_conversions_string.py
121 lines (103 loc) · 3.11 KB
/
prefix_conversions_string.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
"""
* Author: Manuel Di Lullo (https://github.com/manueldilullo)
* Description: Convert a number to use the correct SI or Binary unit prefix.
Inspired by prefix_conversion.py file in this repository by lance-pyles
URL: https://en.wikipedia.org/wiki/Metric_prefix#List_of_SI_prefixes
URL: https://en.wikipedia.org/wiki/Binary_prefix
"""
from __future__ importannotations
fromenumimportEnum, unique
fromtypingimportTypeVar
# Create a generic variable that can be 'Enum', or any subclass.
T=TypeVar("T", bound="Enum")
@unique
classBinaryUnit(Enum):
yotta=80
zetta=70
exa=60
peta=50
tera=40
giga=30
mega=20
kilo=10
@unique
classSIUnit(Enum):
yotta=24
zetta=21
exa=18
peta=15
tera=12
giga=9
mega=6
kilo=3
hecto=2
deca=1
deci=-1
centi=-2
milli=-3
micro=-6
nano=-9
pico=-12
femto=-15
atto=-18
zepto=-21
yocto=-24
@classmethod
defget_positive(cls) ->dict:
"""
Returns a dictionary with only the elements of this enum
that has a positive value
>>> from itertools import islice
>>> positive = SIUnit.get_positive()
>>> inc = iter(positive.items())
>>> dict(islice(inc, len(positive) // 2))
{'yotta': 24, 'zetta': 21, 'exa': 18, 'peta': 15, 'tera': 12}
>>> dict(inc)
{'giga': 9, 'mega': 6, 'kilo': 3, 'hecto': 2, 'deca': 1}
"""
return {unit.name: unit.valueforunitinclsifunit.value>0}
@classmethod
defget_negative(cls) ->dict:
"""
Returns a dictionary with only the elements of this enum
that has a negative value
@example
>>> from itertools import islice
>>> negative = SIUnit.get_negative()
>>> inc = iter(negative.items())
>>> dict(islice(inc, len(negative) // 2))
{'deci': -1, 'centi': -2, 'milli': -3, 'micro': -6, 'nano': -9}
>>> dict(inc)
{'pico': -12, 'femto': -15, 'atto': -18, 'zepto': -21, 'yocto': -24}
"""
return {unit.name: unit.valueforunitinclsifunit.value<0}
defadd_si_prefix(value: float) ->str:
"""
Function that converts a number to his version with SI prefix
@input value (an integer)
@example:
>>> add_si_prefix(10000)
'10.0 kilo'
"""
prefixes=SIUnit.get_positive() ifvalue>0elseSIUnit.get_negative()
forname_prefix, value_prefixinprefixes.items():
numerical_part=value/ (10**value_prefix)
ifnumerical_part>1:
returnf"{numerical_part!s}{name_prefix}"
returnstr(value)
defadd_binary_prefix(value: float) ->str:
"""
Function that converts a number to his version with Binary prefix
@input value (an integer)
@example:
>>> add_binary_prefix(65536)
'64.0 kilo'
"""
forprefixinBinaryUnit:
numerical_part=value/ (2**prefix.value)
ifnumerical_part>1:
returnf"{numerical_part!s}{prefix.name}"
returnstr(value)
if__name__=="__main__":
importdoctest
doctest.testmod()