From a1c8312f453d8cf9013ebeb49c42d6aaf8ee4f3c Mon Sep 17 00:00:00 2001 From: manueldilullo Date: Tue, 19 Oct 2021 20:38:41 +0200 Subject: [PATCH 1/5] First commit for add_prefix_conversion --- conversions/add_prefix_conversion.py | 113 +++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 conversions/add_prefix_conversion.py diff --git a/conversions/add_prefix_conversion.py b/conversions/add_prefix_conversion.py new file mode 100644 index 000000000000..664b515aaeed --- /dev/null +++ b/conversions/add_prefix_conversion.py @@ -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): + 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) + + +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) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 9059dfd34e7ff9676f2c875c1af1fe3775859712 Mon Sep 17 00:00:00 2001 From: manueldilullo Date: Tue, 19 Oct 2021 22:28:49 +0200 Subject: [PATCH 2/5] Class names in CamelCase, str.format() to f-string --- conversions/add_prefix_conversion.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/conversions/add_prefix_conversion.py b/conversions/add_prefix_conversion.py index 664b515aaeed..99d67171be24 100644 --- a/conversions/add_prefix_conversion.py +++ b/conversions/add_prefix_conversion.py @@ -13,7 +13,7 @@ from enum import Enum, unique @unique -class Binary_Unit(Enum): +class BinaryUnit(Enum): yotta = 80 zetta = 70 exa = 60 @@ -24,7 +24,7 @@ class Binary_Unit(Enum): kilo = 10 @unique -class SI_Unit(Enum): +class SIUnit(Enum): yotta = 24 zetta = 21 exa = 18 @@ -52,7 +52,7 @@ 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() + >>> positive = SIUnit.get_positive() >>> inc = iter(positive.items()) >>> dict(islice(inc, len(positive) // 2)) {'yotta': 24, 'zetta': 21, 'exa': 18, 'peta': 15, 'tera': 12} @@ -67,7 +67,7 @@ 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() + >>> negative = SIUnit.get_negative() >>> inc = iter(negative.items()) >>> dict(islice(inc, len(negative) // 2)) {'deci': -1, 'centi': -2, 'milli': -3, 'micro': -6, 'nano': -9} @@ -86,11 +86,11 @@ def add_si_prefix(value: float) -> str: >>> add_si_prefix(10000) '10.0 kilo' """ - prefixes = SI_Unit.get_positive() if value > 0 else SI_Unit.get_negative() + prefixes = SIUnit.get_positive() if value > 0 else SIUnit.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) + return f"{str(numerical_part)} {name_prefix}" def add_binary_prefix(value: float) -> str: @@ -101,10 +101,10 @@ def add_binary_prefix(value: float) -> str: >>> add_binary_prefix(65536) '64.0 kilo' """ - for prefix in Binary_Unit: + for prefix in BinaryUnit: numerical_part = value / (2 ** prefix.value) if numerical_part > 1: - return "{} {}".format(str(numerical_part), prefix.name) + return f"{str(numerical_part)} {prefix.name}" if __name__ == "__main__": From af6f5dfc7e433186a00794abcd80bbe9af5f1635 Mon Sep 17 00:00:00 2001 From: manueldilullo Date: Tue, 19 Oct 2021 22:35:03 +0200 Subject: [PATCH 3/5] Fixed following pre-commit guidelines --- conversions/add_prefix_conversion.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/conversions/add_prefix_conversion.py b/conversions/add_prefix_conversion.py index 99d67171be24..c5a58b1584d4 100644 --- a/conversions/add_prefix_conversion.py +++ b/conversions/add_prefix_conversion.py @@ -12,6 +12,7 @@ from enum import Enum, unique + @unique class BinaryUnit(Enum): yotta = 80 @@ -23,6 +24,7 @@ class BinaryUnit(Enum): mega = 20 kilo = 10 + @unique class SIUnit(Enum): yotta = 24 @@ -47,7 +49,7 @@ class SIUnit(Enum): yocto = -24 @classmethod - def get_positive(cls: SI_Unit) -> dict: + def get_positive(cls: SIUnit) -> dict: """ Returns a dictionary with only the elements of this enum that has a positive value @@ -59,23 +61,23 @@ def get_positive(cls: SI_Unit) -> dict: >>> 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} + return {unit.name: unit.value for unit in cls if unit.value > 0} @classmethod - def get_negative(cls: SI_Unit) -> dict: + def get_negative(cls: SIUnit) -> 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)) + >>> 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} - + return {unit.name: unit.value for unit in cls if unit.value < 0} def add_si_prefix(value: float) -> str: @@ -97,7 +99,7 @@ def add_binary_prefix(value: float) -> str: """ Function that converts a number to his version with Binary prefix @input value (an integer) - @example: + @example: >>> add_binary_prefix(65536) '64.0 kilo' """ From 55e8b9a7cc289b7989f595a55f1d758e81bd83cb Mon Sep 17 00:00:00 2001 From: manueldilullo Date: Tue, 19 Oct 2021 23:16:03 +0200 Subject: [PATCH 4/5] solved issues with mypy and enum.Enum --- conversions/add_prefix_conversion.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/conversions/add_prefix_conversion.py b/conversions/add_prefix_conversion.py index c5a58b1584d4..3255eae6ff45 100644 --- a/conversions/add_prefix_conversion.py +++ b/conversions/add_prefix_conversion.py @@ -11,6 +11,10 @@ from __future__ import annotations from enum import Enum, unique +from typing import Type, TypeVar + +# Create a generic variable that can be 'Enum', or any subclass. +T = TypeVar("T", bound="Enum") @unique @@ -49,7 +53,7 @@ class SIUnit(Enum): yocto = -24 @classmethod - def get_positive(cls: SIUnit) -> dict: + def get_positive(cls: Type[T]) -> dict: """ Returns a dictionary with only the elements of this enum that has a positive value @@ -64,7 +68,7 @@ def get_positive(cls: SIUnit) -> dict: return {unit.name: unit.value for unit in cls if unit.value > 0} @classmethod - def get_negative(cls: SIUnit) -> dict: + def get_negative(cls: Type[T]) -> dict: """ Returns a dictionary with only the elements of this enum that has a negative value @@ -93,6 +97,7 @@ def add_si_prefix(value: float) -> str: numerical_part = value / (10 ** value_prefix) if numerical_part > 1: return f"{str(numerical_part)} {name_prefix}" + return str(value) def add_binary_prefix(value: float) -> str: @@ -107,6 +112,7 @@ def add_binary_prefix(value: float) -> str: numerical_part = value / (2 ** prefix.value) if numerical_part > 1: return f"{str(numerical_part)} {prefix.name}" + return str(value) if __name__ == "__main__": From fda8c894c8bc0f7843740788daae9e87de0a96e4 Mon Sep 17 00:00:00 2001 From: John Law Date: Sun, 1 May 2022 17:42:57 +0800 Subject: [PATCH 5/5] Rename add_prefix_conversion.py to prefix_conversions_string.py --- .../{add_prefix_conversion.py => prefix_conversions_string.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename conversions/{add_prefix_conversion.py => prefix_conversions_string.py} (100%) diff --git a/conversions/add_prefix_conversion.py b/conversions/prefix_conversions_string.py similarity index 100% rename from conversions/add_prefix_conversion.py rename to conversions/prefix_conversions_string.py