def get_qty_in(self, unit): if unit[0] in 'KMGT': unit = unit[0] + 'iB' if unit not in Bytes.units: raise UnitError('Unrecognized unit: ' + unit) if self.qty % Bytes.units[unit] != 0: msg = 'Unable to convert {qty} bytes to a whole number in {unit}'.format(qty=self.qty, unit=unit) raise UnitError(msg) return self.qty / Bytes.units[unit]
def __div__(self, other): if isinstance(other, (int, long)): return Sectors(self.bytes / other, self.sector_size) if isinstance(other, Sectors): if self.sector_size == other.sector_size: return self.bytes / other.bytes else: raise UnitError( 'Cannot divide sectors with different sector sizes') raise UnitError('Can only divide sectors with integers or sectors')
def __add__(self, other): if isinstance(other, (int, long)): return Sectors(self.bytes + self.sector_size * other, self.sector_size) if isinstance(other, Bytes): return Sectors(self.bytes + other, self.sector_size) if isinstance(other, Sectors): if self.sector_size != other.sector_size: raise UnitError( 'Cannot sum sectors with different sector sizes') return Sectors(self.bytes + other.bytes, self.sector_size) raise UnitError('Can only add sectors, bytes or integers to sectors')
def __sub__(self, other): if isinstance(other, (int, long)): return Sectors(self.bytes - self.sector_size * other, self.sector_size) if isinstance(other, Bytes): return Sectors(self.bytes - other, self.sector_size) if isinstance(other, Sectors): if self.sector_size != other.sector_size: raise UnitError( 'Cannot subtract sectors with different sector sizes') return Sectors(self.bytes - other.bytes, self.sector_size) raise UnitError( 'Can only subtract sectors, bytes or integers from sectors')
def __iadd__(self, other): if isinstance(other, (int, long)): self.bytes += self.sector_size * other return self if isinstance(other, Bytes): self.bytes += other return self if isinstance(other, Sectors): if self.sector_size != other.sector_size: raise UnitError( 'Cannot sum sectors with different sector sizes') self.bytes += other.bytes return self raise UnitError('Can only add sectors, bytes or integers to sectors')
def __imod__(self, other): if self.sector_size == other.sector_size: self.bytes %= other.bytes return self else: raise UnitError( 'Cannot take modulus of sectors with different sector sizes')
def __idiv__(self, other): if isinstance(other, Bytes): self.qty /= other.qty else: if not isinstance(other, (int, long)): raise UnitError('Can only divide Bytes with integers or Bytes') self.qty /= other return self
def parse(qty_str): import re regex = re.compile('^(?P<qty>\d+)(?P<unit>[KMGT]i?B|B)$') parsed = regex.match(qty_str) if parsed is None: raise UnitError('Unable to parse ' + qty_str) qty = int(parsed.group('qty')) unit = parsed.group('unit') if unit[0] in 'KMGT': unit = unit[0] + 'iB' byte_qty = qty * Bytes.units[unit] return byte_qty
def check_other(self, other): if not isinstance(other, Bytes): raise UnitError(msg) return func(self, other)
def __div__(self, other): if isinstance(other, Bytes): return self.qty / other.qty if not isinstance(other, (int, long)): raise UnitError('Can only divide Bytes with integers or Bytes') return Bytes(self.qty / other)
def __imul__(self, other): if not isinstance(other, (int, long)): raise UnitError('Can only multiply Bytes with integers') self.qty *= other return self
def __mod__(self, other): if self.sector_size == other.sector_size: return Sectors(self.bytes % other.bytes, self.sector_size) else: raise UnitError( 'Cannot take modulus of sectors with different sector sizes')
def __imul__(self, other): if isinstance(other, (int, long)): self.bytes *= other return self else: raise UnitError('Can only multiply sectors with integers')
def __mul__(self, other): if isinstance(other, (int, long)): return Sectors(self.bytes * other, self.sector_size) else: raise UnitError('Can only multiply sectors with integers')