Package Products :: Package ZenReports :: Module Utils |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenReports.Utils

     1  ##############################################################################
     2  #
     3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
     4  #
     5  # This content is made available according to terms specified in
     6  # License.zenoss under the directory where your Zenoss product is installed.
     7  #
     8  ##############################################################################
     9
    10
    11  import Globals
    12  import zope.interface
    13
    14  from collections import defaultdict
    15  from Products.ZenUtils.Utils import convToUnits
    16
    17  UNAVAILABLE = 'N/A'
    18
    19 -def percent(partial, total):
    20      if partial is None or total is None:
    21          return None
    22      if not total:
    23          return None
    24      return partial * 100 / total
    25
    26
    27 -def percentString(n, decimals=0):
    28      if n is None:
    29          return UNAVAILABLE
    30      return '%*.*f' % (2, decimals, n)
    31
    32
    33 -class Record( object ):
    34      zope.interface.implements( zope.interface.Interface )
    35      __allow_access_to_unprotected_subobjects__ = 1
    36
    37 -    def __init__(self, **kw):
    38          self.values = kw.copy()
    39
    40 -    def __str__(self):
    41          return str(self.values)
    42
    43
    44 -    def __repr__(self):
    45          return repr(self.values)
    46
    47
    48 -    def __getitem__(self, name):
    49          return self.values[name]
    50
    51 -    def __getattr__(self, name):
    52          return self.values.get(name)
    53
    54 -    def percent(self, partial, total):
    55          return percent(partial, total)
    56
    57 -    def percentString(self, n, decimals=0):
    58          return percentString(n, decimals)
    59
    60 -    def humanBytes(self, value, scale=1, unitstr="B"):
    61          if value is None:
    62              return UNAVAILABLE
    63          return convToUnits(value * scale, unitstr=unitstr)
    64
    65 -    def humanBits(self, value, scale=1, unitstr="b"):
    66          if value is None:
    67              return UNAVAILABLE
    68          return convToUnits(value * scale, 1000, unitstr=unitstr)
    69
    70 -    def fmt(self, fmt, value):
    71          if value is None:
    72              return UNAVAILABLE
    73          return fmt % value
    74
    75
    76 -def nested_defaultdict(n,typ):
    77      """create a nested defaultdict n-levels deep, with type typ leaf values"""
    78      fact = typ
    79      i = 1
    80      while i < n:
    81          fact = lambda f=fact: defaultdict(f)
    82          i += 1
    83      return defaultdict(fact)
    84