示例#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]
示例#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')
示例#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')
示例#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')
示例#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')
示例#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')
示例#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
示例#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
示例#9
0
		def check_other(self, other):
			if not isinstance(other, Bytes):
				raise UnitError(msg)
			return func(self, other)
示例#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)
示例#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
示例#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')
示例#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')
示例#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')