Created
February 13, 2014 05:46
-
-
Save hummus/8970363 to your computer and use it in GitHub Desktop.
This file contains 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
from collections import defaultdict | |
from itertools import imap | |
from boto import dynamodb | |
from magnitude import mg | |
REGION='us-west-1' | |
conn = dynamodb.connect_to_region(REGION) | |
WANT_PROPS = ( | |
('item_count', None), | |
('size_bytes', 'B'), | |
('write_units', 'kB/s'), | |
) | |
def main(): | |
stats = [] | |
totals = defaultdict(int) | |
for table in imap(conn.get_table, conn.list_tables(10000)): | |
if table.status != 'ACTIVE': | |
continue | |
tstat = {} | |
for (k,unit) in WANT_PROPS: | |
v = getattr(table,k) | |
tstat.update({k:v}) | |
totals[k] += v | |
stats.append({table.name:tstat}) | |
return (stats,totals) | |
def fu(value, units, do_units=True): | |
if units is None or not do_units: | |
return mg(value) | |
else: | |
return mg(value,units).auto_si() | |
if __name__ == '__main__': | |
stats,totals = main() | |
print('--totals') | |
for (k,units) in WANT_PROPS: | |
print('%20s %-35s'%(k,fu(totals[k],units))) |
This file contains 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
diff --git a/magnitude.py b/magnitude.py | |
index d6e2ee4..00bf4c9 100644 | |
--- a/magnitude.py | |
+++ b/magnitude.py | |
@@ -213,7 +213,9 @@ _prefix = {'y': 1e-24, # yocto | |
'Pi': 2 ** 50, # Pebi (<- peta, 10^15) | |
'Ei': 2 ** 60 # Exbi (<- exa, 10^18) | |
} | |
- | |
+_r_si_prefix = dict([(math.log10(v),k) for (k,v) in sorted( | |
+ _prefix.items(),key=lambda x:x[1]) if len(k)==1] | |
+) | |
###### Default print formatting options | |
@@ -417,7 +419,10 @@ class Magnitude(): | |
if not _prn_units: | |
return st | |
+ st += self.get_unit_str() | |
+ return st.strip() | |
+ def get_unit_str(self): | |
u = self.unit | |
num = ' ' # numerator | |
for i in range(len(_unames)): | |
@@ -434,10 +439,24 @@ class Magnitude(): | |
if den: | |
if num == ' ': | |
num += '1 ' | |
- st += (num + '/ ' + den) | |
+ return (num + '/ ' + den).strip() | |
elif num != ' ': | |
- st += num | |
- return st.strip() | |
+ return num.strip() | |
+ return '' | |
+ | |
+ def auto_si(self): | |
+ r = self.copy() | |
+ r.to_base_units() | |
+ bu = r.get_unit_str() | |
+ if bu == 'b': bu = 'B' | |
+ assert bu, 'no base unit' | |
+ | |
+ e = int(math.log10(abs(r.val))) | |
+ use_prefix = _r_si_prefix.get((e/3)*3) | |
+ if use_prefix is not None: | |
+ return r.ounit(use_prefix+bu) | |
+ else: | |
+ return r | |
def term2mag(self, s): | |
"""Converts a string with units to a Magnitude. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment