def _unit_class_mul(self, other): """Multiply a Unit by an object. If other is another Unit, returns a new composite Unit. Exponents of similar dimensions are added. If self and other share similar BaseDimension, but with different BaseUnits, the resulting BaseUnit for that BaseDimension will be that used in self. If other is a not another Unit, this method returns a new Quantity... UNLESS other is a Quantity and the resulting unit is dimensionless, in which case the underlying value type of the Quantity is returned. """ if is_unit(other): if self in Unit._multiplication_cache: if other in Unit._multiplication_cache[self]: return Unit._multiplication_cache[self][other] else: Unit._multiplication_cache[self] = {} # print "unit * unit" result1 = { } # dictionary of dimensionTuple: (BaseOrScaledUnit, exponent) for unit, exponent in self.iter_base_or_scaled_units(): d = unit.get_dimension_tuple() if d not in result1: result1[d] = {} assert unit not in result1[d] result1[d][unit] = exponent for unit, exponent in other.iter_base_or_scaled_units(): d = unit.get_dimension_tuple() if d not in result1: result1[d] = {} if unit not in result1[d]: result1[d][unit] = 0 result1[d][unit] += exponent result2 = {} # stripped of zero exponents for d in result1: for unit in result1[d]: exponent = result1[d][unit] if exponent != 0: assert unit not in result2 result2[unit] = exponent new_unit = Unit(result2) Unit._multiplication_cache[self][other] = new_unit return new_unit elif is_quantity(other): # print "unit * quantity" value = other._value unit = self * other.unit return Quantity(value, unit).reduce_unit(self) else: # print "scalar * unit" value = other unit = self # Is reduce_unit needed here? I hope not, there is a performance issue... # return Quantity(other, self).reduce_unit(self) return Quantity(other, self)
def _unit_class_mul(self, other): """Multiply a Unit by an object. If other is another Unit, returns a new composite Unit. Exponents of similar dimensions are added. If self and other share similar BaseDimension, but with different BaseUnits, the resulting BaseUnit for that BaseDimension will be that used in self. If other is a not another Unit, this method returns a new Quantity... UNLESS other is a Quantity and the resulting unit is dimensionless, in which case the underlying value type of the Quantity is returned. """ if is_unit(other): if self in Unit._multiplication_cache: if other in Unit._multiplication_cache[self]: return Unit._multiplication_cache[self][other] else: Unit._multiplication_cache[self] = {} # print "unit * unit" result1 = {} # dictionary of dimensionTuple: (BaseOrScaledUnit, exponent) for unit, exponent in self.iter_base_or_scaled_units(): d = unit.get_dimension_tuple() if d not in result1: result1[d] = {} assert unit not in result1[d] result1[d][unit] = exponent for unit, exponent in other.iter_base_or_scaled_units(): d = unit.get_dimension_tuple() if d not in result1: result1[d] = {} if unit not in result1[d]: result1[d][unit] = 0 result1[d][unit] += exponent result2 = {} # stripped of zero exponents for d in result1: for unit in result1[d]: exponent = result1[d][unit] if exponent != 0: assert unit not in result2 result2[unit] = exponent new_unit = Unit(result2) Unit._multiplication_cache[self][other] = new_unit return new_unit elif is_quantity(other): # print "unit * quantity" value = other._value unit = self * other.unit return Quantity(value, unit).reduce_unit(self) else: # print "scalar * unit" value = other unit = self # Is reduce_unit needed here? I hope not, there is a performance issue... # return Quantity(other, self).reduce_unit(self) return Quantity(other, self)
def is_dimensionless(x): """ """ if is_unit(x): return x.is_dimensionless() elif is_quantity(x): return x.unit.is_dimensionless() else: # everything else in the universe is dimensionless return True
def __rtruediv__(self, other): """Divide a scalar by a quantity. Returns a new Quantity. The resulting units are the inverse of the self argument units. """ if is_unit(other): # print "R unit / quantity" raise NotImplementedError('programmer is surprised __rtruediv__ was called instead of __truediv__') elif is_quantity(other): raise NotImplementedError('programmer is surprised __rtruediv__ was called instead of __truediv__') else: # print "R scalar / quantity" return other * pow(self, -1.0)
def _unit_class_rdiv(self, other): """ Divide another object type by a Unit. Returns a new Quantity with a value of other and units of the inverse of self. """ if is_unit(other): raise NotImplementedError('programmer is surprised __rtruediv__ was called instead of __truediv__') else: # print "R scalar / unit" unit = pow(self, -1.0) value = other return Quantity(value, unit).reduce_unit(self)
def __rmul__(self, other): """Multiply a scalar by a Quantity Returns a new Quantity with the same units as self, but with the value multiplied by other. """ if is_unit(other): raise NotImplementedError('programmer is surprised __rmul__ was called instead of __mul__') # print "R unit * quantity" elif is_quantity(other): # print "R quantity * quantity" raise NotImplementedError('programmer is surprised __rmul__ was called instead of __mul__') else: # print "scalar * quantity" return self._change_units_with_factor(self.unit, other, post_multiply=True)
def _unit_class_rdiv(self, other): """ Divide another object type by a Unit. Returns a new Quantity with a value of other and units of the inverse of self. """ if is_unit(other): raise NotImplementedError( 'programmer is surprised __rtruediv__ was called instead of __truediv__' ) else: # print "R scalar / unit" unit = pow(self, -1.0) value = other return Quantity(value, unit).reduce_unit(self)
def __truediv__(self, other): """Divide a Quantity by another object Returns a new Quantity, unless the resulting unit type is dimensionless, in which case the underlying value type is returned. """ if is_unit(other): # print "quantity / unit" return self * pow(other, -1.0) # unit = self.unit / other # return Quantity(self._value, unit).reduce_unit(self.unit) elif is_quantity(other): # print "quantity / quantity" # Delegate quantity/quantity to (quantity/scalar)/unit return (self/other._value) / other.unit else: # print "quantity / scalar" return self * pow(other, -1.0)
def __mul__(self, other): """Multiply a quantity by another object Returns a new Quantity that is the product of the self * other, unless the resulting unit is dimensionless, in which case the underlying value type is returned, instead of a Quantity. """ if is_unit(other): # print "quantity * unit" # Many other mul/div operations delegate to here because I was debugging # a dimensionless unit conversion problem, which I ended up fixing within # the reduce_unit() method. unit = self.unit * other return Quantity(self._value, unit).reduce_unit(self.unit) elif is_quantity(other): # print "quantity * quantity" # Situations where the units cancel can result in scale factors from the unit cancellation. # To simplify things, delegate Quantity * Quantity to (Quantity * scalar) * unit return (self * other._value) * other.unit else: # print "quantity * scalar" return self._change_units_with_factor(self.unit, other, post_multiply=False)
if __name__ == '__main__': process = Process() offset = Offset() base = process.readuint4(offset.ObjectManager.ManagerBase) playerAddress = None curObj = process.readuint4(base + offset.ObjectManager.FirstObj) nextObj = curObj player_guid = process.readlong(base + offset.ObjectManager.PlayerGuid) while curObj != 0 and curObj % 2 == 0: guid = process.readlong(curObj + offset.ObjectManager.CurObjGuid) type = process.readuint4(curObj + offset.ObjectManager.ObjType) descriptor = process.readuint4(curObj + offset.ObjectManager.DescriptorOffset) if playerAddress is not None: distance = unit.distance_ato_b(playerAddress, curObj, process) player = unit.is_player(curObj, process) unit_ = unit.is_unit(curObj, process) if unit: name = unit.name(curObj, process) if player or unit_: print("Distance of " + str(curObj) + " from Player is " + str(distance)) if guid == player_guid: print("Player : " + str(curObj) + " GUID : " + str(guid) + "\nDescriptor : " + str(descriptor)) health = process.readuint4(curObj + offset.GameObject.PosX) print("CorpseX : " + str(health)) playerAddress = curObj nextObj = process.readuint4(curObj + offset.ObjectManager.NextObj) curObj = nextObj process.cont()
def __init__(self, value=None, unit=None): """ Create a new Quantity from a value and a unit. Parameters - value: (any type, usually a number) Measure of this quantity - unit: (Unit) the physical unit, e.g. simtk.unit.meters. """ # When no unit is specified, bend over backwards to handle all one-argument possibilities if unit is None: # one argument version, copied from UList if is_unit(value): # Unit argument creates an empty list with that unit attached unit = value value = [] elif is_quantity(value): # Ulist of a Quantity is just the Quantity itself unit = value.unit value = value._value elif _is_string(value): unit = dimensionless else: # Is value a container? is_container = True try: i = iter(value) except TypeError: is_container = False if is_container: if len(value) < 1: unit = dimensionless else: first_item = iter(value).next() # Avoid infinite recursion for string, because a one-character # string is its own first element if value == first_item: unit = dimensionless else: unit = Quantity(first_item).unit # Notice that tuples, lists, and numpy.arrays can all be initialized with a list new_container = Quantity([], unit) for item in value: new_container.append(Quantity(item)) # Strips off units into list new_container._value # __class__ trick does not work for numpy.arrays try: import numpy if isinstance(value, numpy.ndarray): value = numpy.array(new_container._value) else: # delegate contruction to container class from list value = value.__class__(new_container._value) except ImportError: # delegate contruction to container class from list value = value.__class__(new_container._value) else: # Non-Quantity, non container # Wrap in a dimensionless Quantity unit = dimensionless # Accept simple scalar quantities as units if is_quantity(unit): value = value * unit._value unit = unit.unit # Use empty list for unspecified values if value is None: value = [] self._value = value self.unit = unit
def __init__(self, value=None, unit=None): """ Create a new Quantity from a value and a unit. Parameters - value: (any type, usually a number) Measure of this quantity - unit: (Unit) the physical unit, e.g. simtk.unit.meters. """ # When no unit is specified, bend over backwards to handle all one-argument possibilities if unit == None: # one argument version, copied from UList if is_unit(value): # Unit argument creates an empty list with that unit attached unit = value value = [] elif is_quantity(value): # Ulist of a Quantity is just the Quantity itself unit = value.unit value = value._value elif _is_string(value): unit = dimensionless else: # Is value a container? is_container = True try: i = iter(value) except TypeError: is_container = False if is_container: if len(value) < 1: unit = dimensionless else: first_item = iter(value).next() # Avoid infinite recursion for string, because a one-character # string is its own first element if value == first_item: unit = dimensionless else: unit = Quantity(first_item).unit # Notice that tuples, lists, and numpy.arrays can all be initialized with a list new_container = Quantity([], unit) for item in value: new_container.append(Quantity(item)) # Strips off units into list new_container._value # __class__ trick does not work for numpy.arrays try: import numpy if isinstance(value, numpy.ndarray): value = numpy.array(new_container._value) else: # delegate contruction to container class from list value = value.__class__(new_container._value) except ImportError: # delegate contruction to container class from list value = value.__class__(new_container._value) else: # Non-Quantity, non container # Wrap in a dimensionless Quantity unit = dimensionless # Accept simple scalar quantities as units if is_quantity(unit): value = value * unit._value unit = unit.unit # Use empty list for unspecified values if value == None: value = [] self._value = value self.unit = unit