-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
First commit for add_prefix_conversion #5453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
poyea
merged 5 commits into
TheAlgorithms:master
from
manueldilullo:add_prefix_conversion_branch
May 1, 2022
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a1c8312
First commit for add_prefix_conversion
manueldilullo 9059dfd
Class names in CamelCase, str.format() to f-string
manueldilullo af6f5df
Fixed following pre-commit guidelines
manueldilullo 55e8b9a
solved issues with mypy and enum.Enum
manueldilullo fda8c89
Rename add_prefix_conversion.py to prefix_conversions_string.py
poyea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
""" | ||
* 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__ import annotations | ||
|
||
from enum import Enum, unique | ||
|
||
@unique | ||
class Binary_Unit(Enum): | ||
yotta = 80 | ||
zetta = 70 | ||
exa = 60 | ||
peta = 50 | ||
tera = 40 | ||
giga = 30 | ||
mega = 20 | ||
kilo = 10 | ||
|
||
@unique | ||
class SI_Unit(Enum): | ||
poyea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
def get_positive(cls: SI_Unit) -> dict: | ||
""" | ||
Returns a dictionary with only the elements of this enum | ||
that has a positive value | ||
>>> from itertools import islice | ||
>>> positive = SI_Unit.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.value for unit in cls if unit.value > 0} | ||
|
||
@classmethod | ||
def get_negative(cls: SI_Unit) -> dict: | ||
""" | ||
Returns a dictionary with only the elements of this enum | ||
that has a negative value | ||
>>> from itertools import islice | ||
>>> negative = SI_Unit.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.value for unit in cls if unit.value < 0} | ||
|
||
|
||
|
||
def add_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 = SI_Unit.get_positive() if value > 0 else SI_Unit.get_negative() | ||
for name_prefix, value_prefix in prefixes.items(): | ||
numerical_part = value / (10 ** value_prefix) | ||
if numerical_part > 1: | ||
return "{} {}".format(str(numerical_part), name_prefix) | ||
poyea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def add_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' | ||
""" | ||
for prefix in Binary_Unit: | ||
numerical_part = value / (2 ** prefix.value) | ||
if numerical_part > 1: | ||
return "{} {}".format(str(numerical_part), prefix.name) | ||
poyea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.