Ejemplo n.º 1
0
	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]
Ejemplo n.º 2
0
 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')
Ejemplo n.º 3
0
 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')
Ejemplo n.º 4
0
 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')
Ejemplo n.º 5
0
 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')
Ejemplo n.º 6
0
 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')
Ejemplo n.º 7
0
	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
Ejemplo n.º 8
0
	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
Ejemplo n.º 9
0
		def check_other(self, other):
			if not isinstance(other, Bytes):
				raise UnitError(msg)
			return func(self, other)
Ejemplo n.º 10
0
	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)
Ejemplo n.º 11
0
	def __imul__(self, other):
		if not isinstance(other, (int, long)):
			raise UnitError('Can only multiply Bytes with integers')
		self.qty *= other
		return self
Ejemplo n.º 12
0
 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')
Ejemplo n.º 13
0
 def __imul__(self, other):
     if isinstance(other, (int, long)):
         self.bytes *= other
         return self
     else:
         raise UnitError('Can only multiply sectors with integers')
Ejemplo n.º 14
0
 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')