diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2017-09-07 21:44:16 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2017-09-07 21:44:16 +0200 |
commit | 6ea743740ff4524287e5b63a18a45bd19d2451bb (patch) | |
tree | d14ec9c5b580450b8e2db2b115be85aea48afc42 /src/ee/formatting | |
parent | a3da99e22540c026e4acf7bdc735aa221a4dd752 (diff) | |
download | ee-python-6ea743740ff4524287e5b63a18a45bd19d2451bb.tar.gz ee-python-6ea743740ff4524287e5b63a18a45bd19d2451bb.tar.bz2 ee-python-6ea743740ff4524287e5b63a18a45bd19d2451bb.tar.xz ee-python-6ea743740ff4524287e5b63a18a45bd19d2451bb.zip |
o Reformat all.
Diffstat (limited to 'src/ee/formatting')
-rw-r--r-- | src/ee/formatting/__init__.py | 132 |
1 files changed, 69 insertions, 63 deletions
diff --git a/src/ee/formatting/__init__.py b/src/ee/formatting/__init__.py index 42bf48d..32eaf64 100644 --- a/src/ee/formatting/__init__.py +++ b/src/ee/formatting/__init__.py @@ -6,42 +6,45 @@ __all__ = [ 'eng_str', ] + 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] +_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] _e_series_12 = [v for i, v in enumerate(_e_series_24) if i % 2 == 0] _e_series_6 = [v for i, v in enumerate(_e_series_24) if i % 4 == 0] _e_series_192 = [ - 1.00, 1.01, 1.02, 1.04, 1.05, 1.06, 1.07, 1.09, 1.10, 1.11, - 1.13, 1.14, 1.15, 1.17, 1.18, 1.20, 1.21, 1.23, 1.24, 1.26, - 1.27, 1.29, 1.30, 1.32, 1.33, 1.35, 1.37, 1.38, 1.40, 1.42, - 1.43, 1.45, 1.47, 1.49, 1.50, 1.52, 1.54, 1.56, 1.58, 1.60, - 1.62, 1.64, 1.65, 1.67, 1.69, 1.72, 1.74, 1.76, 1.78, 1.80, - 1.82, 1.84, 1.87, 1.89, 1.91, 1.93, 1.96, 1.98, 2.00, 2.03, - 2.05, 2.08, 2.10, 2.13, 2.15, 2.18, 2.21, 2.23, 2.26, 2.29, - 2.32, 2.34, 2.37, 2.40, 2.43, 2.46, 2.49, 2.52, 2.55, 2.58, - 2.61, 2.64, 2.67, 2.71, 2.74, 2.77, 2.80, 2.84, 2.87, 2.91, - 2.94, 2.98, 3.01, 3.05, 3.09, 3.12, 3.16, 3.20, 3.24, 3.28, - 3.32, 3.36, 3.40, 3.44, 3.48, 3.52, 3.57, 3.61, 3.65, 3.70, - 3.74, 3.79, 3.83, 3.88, 3.92, 3.97, 4.02, 4.07, 4.12, 4.17, - 4.22, 4.27, 4.32, 4.37, 4.42, 4.48, 4.53, 4.59, 4.64, 4.70, - 4.75, 4.81, 4.87, 4.93, 4.99, 5.05, 5.11, 5.17, 5.23, 5.30, - 5.36, 5.42, 5.49, 5.56, 5.62, 5.69, 5.76, 5.83, 5.90, 5.97, - 6.04, 6.12, 6.19, 6.26, 6.34, 6.42, 6.49, 6.57, 6.65, 6.73, - 6.81, 6.90, 6.98, 7.06, 7.15, 7.23, 7.32, 7.41, 7.50, 7.59, - 7.68, 7.77, 7.87, 7.96, 8.06, 8.16, 8.25, 8.35, 8.45, 8.56, - 8.66, 8.76, 8.87, 8.98, 9.09, 9.20, 9.31, 9.42, 9.53, 9.65, - 9.76, 9.88, + 1.00, 1.01, 1.02, 1.04, 1.05, 1.06, 1.07, 1.09, 1.10, 1.11, + 1.13, 1.14, 1.15, 1.17, 1.18, 1.20, 1.21, 1.23, 1.24, 1.26, + 1.27, 1.29, 1.30, 1.32, 1.33, 1.35, 1.37, 1.38, 1.40, 1.42, + 1.43, 1.45, 1.47, 1.49, 1.50, 1.52, 1.54, 1.56, 1.58, 1.60, + 1.62, 1.64, 1.65, 1.67, 1.69, 1.72, 1.74, 1.76, 1.78, 1.80, + 1.82, 1.84, 1.87, 1.89, 1.91, 1.93, 1.96, 1.98, 2.00, 2.03, + 2.05, 2.08, 2.10, 2.13, 2.15, 2.18, 2.21, 2.23, 2.26, 2.29, + 2.32, 2.34, 2.37, 2.40, 2.43, 2.46, 2.49, 2.52, 2.55, 2.58, + 2.61, 2.64, 2.67, 2.71, 2.74, 2.77, 2.80, 2.84, 2.87, 2.91, + 2.94, 2.98, 3.01, 3.05, 3.09, 3.12, 3.16, 3.20, 3.24, 3.28, + 3.32, 3.36, 3.40, 3.44, 3.48, 3.52, 3.57, 3.61, 3.65, 3.70, + 3.74, 3.79, 3.83, 3.88, 3.92, 3.97, 4.02, 4.07, 4.12, 4.17, + 4.22, 4.27, 4.32, 4.37, 4.42, 4.48, 4.53, 4.59, 4.64, 4.70, + 4.75, 4.81, 4.87, 4.93, 4.99, 5.05, 5.11, 5.17, 5.23, 5.30, + 5.36, 5.42, 5.49, 5.56, 5.62, 5.69, 5.76, 5.83, 5.90, 5.97, + 6.04, 6.12, 6.19, 6.26, 6.34, 6.42, 6.49, 6.57, 6.65, 6.73, + 6.81, 6.90, 6.98, 7.06, 7.15, 7.23, 7.32, 7.41, 7.50, 7.59, + 7.68, 7.77, 7.87, 7.96, 8.06, 8.16, 8.25, 8.35, 8.45, 8.56, + 8.66, 8.76, 8.87, 8.98, 9.09, 9.20, 9.31, 9.42, 9.53, 9.65, + 9.76, 9.88, ] _e_series_96 = [v for i, v in enumerate(_e_series_192) if i % 2 == 0] _e_series_48 = [v for i, v in enumerate(_e_series_192) if i % 4 == 0] @@ -53,46 +56,49 @@ e48 = ESeries(_e_series_48) 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)) - - return min(series, key=lambda v: abs(v - value)) * 10**e - -import numpy -def eng_str(value, unit = None): - if value == 0: - s = '0' + (' ' + unit if unit is not None else '') - else: - big = ['k', 'M', 'G', 'T'] - small = ['m', 'u', 'p'] +def e_series_find_closest(value): + e = math.floor(math.log10(value)) + value = float(value / (10 ** e)) - if unit is not None: - big = [' ' + unit] + [' ' + x + unit for x in big] - small = [' ' + unit] + [' ' + x + unit for x in small] - else: - big = [''] + [' ' + x for x in big] - small = [''] + [' ' + x for x in small] + return min(series, key=lambda v: abs(v - value)) * 10 ** e - e_floor = math.floor(math.log10(value)) - div3 = math.floor(e_floor/3) - if div3 > 0: - suffixes = big - idx = int(div3) - else: - suffixes = small - idx = int(-div3) +import numpy - suffix = suffixes[idx] - scale = 10**(div3*3) - scaled = value/scale - if (scaled - math.floor(scaled)) < 0.0001: -# if scaled == math.floor(scaled): - s = "{:1.0f}{}".format(scaled, suffix) +def eng_str(value, unit=None): + if value == 0: + s = '0' + (' ' + unit if unit is not None else '') else: - s = "{:1.1f}{}".format(scaled, suffix) - - return s + big = ['k', 'M', 'G', 'T'] + small = ['m', 'u', 'p'] + + if unit is not None: + big = [' ' + unit] + [' ' + x + unit for x in big] + small = [' ' + unit] + [' ' + x + unit for x in small] + else: + big = [''] + [' ' + x for x in big] + small = [''] + [' ' + x for x in small] + + e_floor = math.floor(math.log10(value)) + + div3 = math.floor(e_floor / 3) + if div3 > 0: + suffixes = big + idx = int(div3) + else: + suffixes = small + idx = int(-div3) + + suffix = suffixes[idx] + scale = 10 ** (div3 * 3) + scaled = value / scale + + if (scaled - math.floor(scaled)) < 0.0001: + # if scaled == math.floor(scaled): + s = "{:1.0f}{}".format(scaled, suffix) + else: + s = "{:1.1f}{}".format(scaled, suffix) + + return s |