diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2017-08-02 22:51:34 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2017-08-02 22:51:34 +0200 |
commit | c2d21a4c36cf9374b708f580af2fd420bb9b1146 (patch) | |
tree | ab04461cb233384f7523d59282968f26615c62bd /src/ee | |
parent | 2384a4e12cb029cbd6c8595fa9f3c5a666a391da (diff) | |
download | ee-python-c2d21a4c36cf9374b708f580af2fd420bb9b1146.tar.gz ee-python-c2d21a4c36cf9374b708f580af2fd420bb9b1146.tar.bz2 ee-python-c2d21a4c36cf9374b708f580af2fd420bb9b1146.tar.xz ee-python-c2d21a4c36cf9374b708f580af2fd420bb9b1146.zip |
o Indenting with two spaces.
o Reworking test to use pytest's parametrize
Diffstat (limited to 'src/ee')
-rw-r--r-- | src/ee/formatting/__init__.py | 47 |
1 files changed, 19 insertions, 28 deletions
diff --git a/src/ee/formatting/__init__.py b/src/ee/formatting/__init__.py index 3c592ef..a1e27a4 100644 --- a/src/ee/formatting/__init__.py +++ b/src/ee/formatting/__init__.py @@ -1,4 +1,3 @@ -#import sympy as sp import math from ee import EeError @@ -7,23 +6,15 @@ __all__ = [ 'eng_str', ] -#class Symbols(object): -# def __init__(self): -# self.tau = sp.symbols('tau'), -# self.r = sp.symbols('r'), -# self.c = sp.symbols('c') -# -#_s = Symbols() - class ESeries(object): - def __init__(self, series): - self.series = series + def __init__(self, series): + self.series = series - def closest(self, value): - e = math.floor(math.log10(value)) - value = float(value/(10**e)) + def closest(self, value): + e = math.floor(math.log10(value)) + value = float(value/(10**e)) - return min(self.series, key=lambda v: abs(v - value)) * 10**e + return min(self.series, key=lambda v: abs(v - value)) * 10**e # https://en.wikipedia.org/wiki/E-series_of_preferred_numbers _e_series_24 = [1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1] @@ -63,23 +54,23 @@ e96 = ESeries(_e_series_96) e192 = ESeries(_e_series_192) def e_series_find_closest(value): - e = math.floor(math.log10(value)) - value = float(value/(10**e)) + e = math.floor(math.log10(value)) + value = float(value/(10**e)) - return min(series, key=lambda v: abs(v - value)) * 10**e + return min(series, key=lambda v: abs(v - value)) * 10**e def eng_str(value): - big = ['', ' k', ' M', ' G', ' T'] - e_floor = math.floor(math.log10(value)) + big = ['', ' k', ' M', ' G', ' T'] + e_floor = math.floor(math.log10(value)) - div3 = int(math.floor(e_floor/3)) - suffix = big[div3] - scale = 10**(div3*3) - scaled = value/scale + div3 = int(math.floor(e_floor/3)) + suffix = big[div3] + scale = 10**(div3*3) + scaled = value/scale - if scaled == int(scaled): - return "{:1.0f}{}".format(scaled, suffix) - else: - return "{:1.1f}{}".format(scaled, suffix) + if scaled == int(scaled): + return "{:1.0f}{}".format(scaled, suffix) + else: + return "{:1.1f}{}".format(scaled, suffix) # return "scaled={:>9}, div3={}, floor(e)={}, scale={}, suffix={}".format(str(scaled), div3, e_floor, scale, suffix) |