def solve(self): """Solves the ideal gas equation for the unknown value. Returns ------- The value (with the default units) for the corresponding unknown. """ left, right = None, None # Solve for the side without any unknowns first. if self.P and self.V: left = self.conv_P * self.V elif self.n and self.T: right = self.n * Ratm * self.T if right: # Then, solve for the single unknown on the left side. if self.unknown == "P": # Pressure calculations. setattr(self, 'P', operator.__truediv__(right, self.V)) return self.P if self.unknown == "V": # Volume calculations. setattr(self, 'V', operator.__truediv__(right, self.conv_P)) return self.V if left: # Otherwise, solve for the single unknown on the right side. if self.unknown == "n": # Moles calculations. setattr( self, 'n', operator.__truediv__(left, operator.__mul__(Ratm, self.conv_T))) return self.n if self.unknown == "T": # Temperature calculations. setattr( self, 'T', operator.__truediv__(left, operator.__mul__(Ratm, self.n))) return self.T
def __call__(self, **kwargs): # Update the class calculation quantities for more calculations. if "moles" in kwargs: self.mole_amount = kwargs["moles"] self.gram_amount = round(operator.mul(self.mole_amount, self.mass), 4) self.molecules = round(operator.mul(self.mole_amount, AVOGADRO), 4) if "grams" in kwargs: self.gram_amount = kwargs["grams"] self.mole_amount = round( operator.__truediv__(self.gram_amount, self.mass), 4) self.molecules = round(operator.mul(self.mole_amount, AVOGADRO), 4) if "molecules" in kwargs: self.molecules = kwargs["molecules"] self.mole_amount = round( operator.__truediv__(self.molecules, AVOGADRO), 4) self.gram_amount = round(operator.mul(self.mass, self.mole_amount)) if "percent" in kwargs: # Primarily if you are setting up elements for Compound.from_formula. if float(kwargs["percent"]) >= 1: raise TypeError("That is not a valid input for the percent " "field. Enter a decimal percent value.") self.percent_of = kwargs["percent"] self.mole_amount = False self.gram_amount = False if all(x in ["moles", "grams", "kwargs"] for x in [kwargs]): raise ValueError("You cannot provide multiple quantities " "of the element at a single time.")
def __truediv__(self, other): assert isinstance(other, float) or isinstance( other, int) or isinstance(other, bool) or isinstance(other, Point) if isinstance(other, Point): return Point(__truediv__(self.x, other.x), __truediv__(self.y, other.y)) else: return Point(__truediv__(self.x, other), __truediv__(self.y, other))
def __init__(self, element_symbol, **kwargs): # Initialize class properties from PeriodicTable object. self._properties = PeriodicTable().get_properties(element_symbol) # Element Symbol/Name. self.element_symbol = element_symbol self.element_name = self.get_element_name() # Atomic Mass and Number. self.mass = self._properties['AtomicMass'] self.number = self._properties['AtomicNumber'] # Electron Configurations. self.electron_configuration = self._properties['ElectronConfiguration'] self.full_electron_configuration = self._get_full_electron_configuration( ) # Miscellaneous Properties. self.radius = self._properties['AtomicRadius'] self.electronegativity = self._properties['Electronegativity'] self.ionization = self._properties['IonizationEnergy'] self.electron_affinity = self._properties['ElectronAffinity'] # Class Value Calculations. if "moles" in kwargs: self.mole_amount = kwargs["moles"] self.gram_amount = round(operator.mul(self.mole_amount, self.mass), 4) self.molecules = round(operator.mul(self.mole_amount, AVOGADRO), 4) if "grams" in kwargs: self.gram_amount = kwargs["grams"] self.mole_amount = round( operator.__truediv__(self.gram_amount, self.mass), 4) self.molecules = round(operator.mul(self.mole_amount, AVOGADRO), 4) if "molecules" in kwargs: self.molecules = kwargs["molecules"] self.mole_amount = round( operator.__truediv__(self.molecules, AVOGADRO), 4) self.gram_amount = round(operator.mul(self.mass, self.mole_amount)) if "percent" in kwargs: # Primarily if you are setting up elements for Compound.from_formula. if float(kwargs["percent"]) >= 1: raise TypeError( "That is not a valid input for the percent field. Enter a percent as a decimal." ) self.percent_of = kwargs["percent"] self.mole_amount = False self.gram_amount = False if all(x in ["moles", "grams", "kwargs"] for x in [kwargs]): raise Exception("You cannot provide multiple different quantities " "of the element at a single time.")
def molarity(compound, setting=None, moles=None, volume=None): """ Calculations involving the molarity of a compound. Returns a value based on the setting. The compound must be the Compound class. The moles/volume setting will be gathered from the compound itself if defined. **Volume is assumed to be in milliliters. Setting --> Molarity: Returns the molarity of the compound from moles and volume. Setting --> Moles: Returns the moles of the compound from molarity and volume. Setting --> Volume: Returns the volume of the compound from moles and volume. """ # Initialize settings: if setting not in ["molarity", "moles", "volume"]: raise ValueError("You must choose a setting: molarity, moles volume.") if not isinstance(compound, Compound): raise AttributeError( "You must include a Compound class as the main argument") if compound.volume and not volume: volume = compound.volume if not compound.volume and not volume and setting in ["molarity", "moles"]: raise AttributeError( "You must define volume either through the Compound class or through the method." ) if compound.mole_amount and not moles: moles = compound.mole_amount if not compound.mole_amount and not moles and setting in [ "molarity", "volume" ]: raise AttributeError( "You must define the mole amount either through the Compound class or through the method." ) if not compound.molarity and setting in ["moles", "volume"]: raise AttributeError( "You must define the molarity of the solution if you want to calculate molarity." ) # Calculations if setting == "molarity": return operator.__truediv__(moles, volume) if setting == "moles": return operator.__mul__(volume, compound.molarity) if setting == "volume": return operator.__truediv__(moles, compound.molarity) else: return None
def __truediv__(self, other): """ This function is used for division of two intervals """ div = [] p1, q1 = self.leftendpoint, self.rightendpoint p2, q2 = other.leftendpoint, other.rightendpoint if (0 >= p2 and 0 <= q2): raise ZeroDivisionError("You tried to divide by zero!") ac = operator.__truediv__(p1, p2) ad = operator.__truediv__(p1, q2) bc = operator.__truediv__(q1, p2) bd = operator.__truediv__(q1, q2) div.append(min(ac, ad, bc, bd)) div.append(max(ac, ad, bc, bd)) for i in np.isfinite(div): if i != True: raise Exception('Is infinity') return Interval(div[0], div[1])
def calculateDeviations(self): now = datetime.datetime.now() for player in self.players : td = now - player.lastGame # python 2.6.5 woes # total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 total_seconds = operator.__truediv__((td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6), 10**6) hours = total_seconds / 3600.0 hours = min( hours, 24.0*DEFAULT_PERIOD_DAYS ) # Ca = Ca * (1.0/0.1)^(Ta/(24*30)) player.deviation = player.deviation * math.pow( MAX_DEVIATION / MIN_DEVIATION, hours / (24.0*DEFAULT_PERIOD_DAYS) ) player.deviation = min( MAX_DEVIATION, max( MIN_DEVIATION, player.deviation ) )
def __init__(self, compound, charge = None, state = "aq", **kwargs): super().__init__(compound, bypass = True) if charge: # Charge can also not be provided. if charge not in range(-5, 6): raise ValueError(f"The value for charge as initiated, {charge}, is too high or too low.") self.charge = charge if state not in ["aq", "s", "l", "g"]: raise ValueError("The state of the compound must be either aqueous, solid, liquid, or gaseous.") self.state = state if 'molarity' in kwargs and 'volume' in kwargs: self.molarity = kwargs['molarity'] self.volume = kwargs['volume'] self.mole_amount = operator.__mul__(self.molarity, self.volume) if 'molarity' in kwargs and 'moles' in kwargs: self.molarity = kwargs['molarity'] self.mole_amount = kwargs['mole_amount'] self.volume = operator.__truediv__(self.mole_amount, self.molarity) if 'moles' in kwargs and 'volume' in kwargs: self.mole_amount = kwargs['moles'] self.volume = kwargs['volume'] self.molarity = operator.__truediv__(self.mole_amount, self.volume)
def calculateDeviations(self): now = datetime.datetime.now() for player in self.players: td = now - player.lastGame # python 2.6.5 woes # total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 total_seconds = operator.__truediv__( (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6), 10**6) hours = total_seconds / 3600.0 hours = min(hours, 24.0 * DEFAULT_PERIOD_DAYS) # Ca = Ca * (1.0/0.1)^(Ta/(24*30)) player.deviation = player.deviation * math.pow( MAX_DEVIATION / MIN_DEVIATION, hours / (24.0 * DEFAULT_PERIOD_DAYS)) player.deviation = min(MAX_DEVIATION, max(MIN_DEVIATION, player.deviation))
def binary_compute(self, l, r): return operator.__truediv__(l, r)
def update_event(self, inp=-1): self.set_output_val(0, operator.__truediv__(self.input(0), self.input(1)))
def __truediv__(self, other): other = to_quantity(other) return new_quantity_nonone(operator.__truediv__(self.number,other.number), (self.unit / other.unit).to_simple_form())
def test_sanity(): ''' Performs a set of simple sanity checks on most operators. ''' #__abs__ AreEqual(operator.__abs__(0), 0) AreEqual(operator.__abs__(1), 1) AreEqual(operator.__abs__(-1), 1) AreEqual(operator.__abs__(0.0), 0.0) AreEqual(operator.__abs__(1.1), 1.1) AreEqual(operator.__abs__(-1.1), 1.1) AreEqual(operator.__abs__(0L), 0L) AreEqual(operator.__abs__(1L), 1L) AreEqual(operator.__abs__(-1L), 1L) #__neg__ AreEqual(operator.__neg__(0), 0) AreEqual(operator.__neg__(1), -1) AreEqual(operator.__neg__(-1), 1) AreEqual(operator.__neg__(0.0), 0.0) AreEqual(operator.__neg__(1.1), -1.1) AreEqual(operator.__neg__(-1.1), 1.1) AreEqual(operator.__neg__(0L), 0L) AreEqual(operator.__neg__(1L), -1L) AreEqual(operator.__neg__(-1L), 1L) #__pos__ AreEqual(operator.__pos__(0), 0) AreEqual(operator.__pos__(1), 1) AreEqual(operator.__pos__(-1), -1) AreEqual(operator.__pos__(0.0), 0.0) AreEqual(operator.__pos__(1.1), 1.1) AreEqual(operator.__pos__(-1.1), -1.1) AreEqual(operator.__pos__(0L), 0L) AreEqual(operator.__pos__(1L), 1L) AreEqual(operator.__pos__(-1L), -1L) #__add__ AreEqual(operator.__add__(0, 0), 0) AreEqual(operator.__add__(1, 2), 3) AreEqual(operator.__add__(-1, 2), 1) AreEqual(operator.__add__(0.0, 0.0), 0.0) AreEqual(operator.__add__(1.1, 2.1), 3.2) AreEqual(operator.__add__(-1.1, 2.1), 1.0) AreEqual(operator.__add__(0L, 0L), 0L) AreEqual(operator.__add__(1L, 2L), 3L) AreEqual(operator.__add__(-1L, 2L), 1L) #__sub__ AreEqual(operator.__sub__(0, 0), 0) AreEqual(operator.__sub__(1, 2), -1) AreEqual(operator.__sub__(-1, 2), -3) AreEqual(operator.__sub__(0.0, 0.0), 0.0) AreEqual(operator.__sub__(1.1, 2.1), -1.0) AreEqual(operator.__sub__(-1.1, 2.1), -3.2) AreEqual(operator.__sub__(0L, 0L), 0L) AreEqual(operator.__sub__(1L, 2L), -1L) AreEqual(operator.__sub__(-1L, 2L), -3L) #__mul__ AreEqual(operator.__mul__(0, 0), 0) AreEqual(operator.__mul__(1, 2), 2) AreEqual(operator.__mul__(-1, 2), -2) AreEqual(operator.__mul__(0.0, 0.0), 0.0) AreEqual(operator.__mul__(2.0, 3.0), 6.0) AreEqual(operator.__mul__(-2.0, 3.0), -6.0) AreEqual(operator.__mul__(0L, 0L), 0L) AreEqual(operator.__mul__(1L, 2L), 2L) AreEqual(operator.__mul__(-1L, 2L), -2L) #__div__ AreEqual(operator.__div__(0, 1), 0) AreEqual(operator.__div__(4, 2), 2) AreEqual(operator.__div__(-1, 2), -1) AreEqual(operator.__div__(0.0, 1.0), 0.0) AreEqual(operator.__div__(4.0, 2.0), 2.0) AreEqual(operator.__div__(-4.0, 2.0), -2.0) AreEqual(operator.__div__(0L, 1L), 0L) AreEqual(operator.__div__(4L, 2L), 2L) AreEqual(operator.__div__(-4L, 2L), -2L) #__floordiv__ AreEqual(operator.__floordiv__(0, 1), 0) AreEqual(operator.__floordiv__(4, 2), 2) AreEqual(operator.__floordiv__(-1, 2), -1) AreEqual(operator.__floordiv__(0.0, 1.0), 0.0) AreEqual(operator.__floordiv__(4.0, 2.0), 2.0) AreEqual(operator.__floordiv__(-4.0, 2.0), -2.0) AreEqual(operator.__floordiv__(0L, 1L), 0L) AreEqual(operator.__floordiv__(4L, 2L), 2L) AreEqual(operator.__floordiv__(-4L, 2L), -2L) #__truediv__ AreEqual(operator.__truediv__(0, 1), 0) AreEqual(operator.__truediv__(4, 2), 2) AreEqual(operator.__truediv__(-1, 2), -0.5) AreEqual(operator.__truediv__(0.0, 1.0), 0.0) AreEqual(operator.__truediv__(4.0, 2.0), 2.0) AreEqual(operator.__truediv__(-1.0, 2.0), -0.5) AreEqual(operator.__truediv__(0L, 1L), 0L) AreEqual(operator.__truediv__(4L, 2L), 2L) AreEqual(operator.__truediv__(-4L, 2L), -2L) #__mod__ AreEqual(operator.__mod__(0, 1), 0) AreEqual(operator.__mod__(4, 2), 0) AreEqual(operator.__mod__(-1, 2), 1) AreEqual(operator.__mod__(0.0, 1.0), 0.0) AreEqual(operator.__mod__(4.0, 2.0), 0.0) AreEqual(operator.__mod__(-1.0, 2.0), 1.0) AreEqual(operator.__mod__(0L, 1L), 0L) AreEqual(operator.__mod__(4L, 2L), 0L) AreEqual(operator.__mod__(-4L, 2L), 0L) #__inv__ AreEqual(operator.__inv__(0), -1) AreEqual(operator.__inv__(1), -2) AreEqual(operator.__inv__(-1), 0) AreEqual(operator.__inv__(0L), -1L) AreEqual(operator.__inv__(1L), -2L) AreEqual(operator.__inv__(-1L), 0L) #__invert__ AreEqual(operator.__invert__(0), -1) AreEqual(operator.__invert__(1), -2) AreEqual(operator.__invert__(-1), 0) AreEqual(operator.__invert__(0L), -1L) AreEqual(operator.__invert__(1L), -2L) AreEqual(operator.__invert__(-1L), 0L) #__lshift__ AreEqual(operator.__lshift__(0, 1), 0) AreEqual(operator.__lshift__(1, 1), 2) AreEqual(operator.__lshift__(-1, 1), -2) AreEqual(operator.__lshift__(0L, 1), 0L) AreEqual(operator.__lshift__(1L, 1), 2L) AreEqual(operator.__lshift__(-1L, 1), -2L) #__rshift__ AreEqual(operator.__rshift__(1, 1), 0) AreEqual(operator.__rshift__(2, 1), 1) AreEqual(operator.__rshift__(-1, 1), -1) AreEqual(operator.__rshift__(1L, 1), 0L) AreEqual(operator.__rshift__(2L, 1), 1L) AreEqual(operator.__rshift__(-1L, 1), -1L) #__not__ AreEqual(operator.__not__(0), 1) AreEqual(operator.__not__(1), 0) AreEqual(operator.__not__(-1), 0) AreEqual(operator.__not__(0L), 1) AreEqual(operator.__not__(1L), 0) AreEqual(operator.__not__(-1L), 0) #__and__ AreEqual(operator.__and__(0, 0), 0) AreEqual(operator.__and__(1, 1), 1) AreEqual(operator.__and__(0, 1), 0) AreEqual(operator.__and__(1, 0), 0) #__xor__ AreEqual(operator.__xor__(0, 0), 0) AreEqual(operator.__xor__(1, 1), 0) AreEqual(operator.__xor__(0, 1), 1) AreEqual(operator.__xor__(1, 0), 1) #__or__ AreEqual(operator.__or__(0, 0), 0) AreEqual(operator.__or__(1, 1), 1) AreEqual(operator.__or__(0, 1), 1) AreEqual(operator.__or__(1, 0), 1) #__concat__ AreEqual(operator.__concat__([0], [1]), [0,1]) AreEqual(operator.__concat__([2], [1]), [2,1]) AreEqual(operator.__concat__([-1], [1]), [-1,1]) #__contains__ Assert(operator.__contains__("abc", "c")) Assert(not operator.__contains__("abc", "d")) Assert(operator.__contains__("abc", "")) Assert(not operator.__contains__("", "c")) Assert(operator.__contains__([1,2,3], 1)) Assert(not operator.__contains__([1,2,3], 4)) #__getitem__ AreEqual(operator.__getitem__("abc", 2), "c") AssertError(IndexError, operator.__getitem__, "abc", 3) AreEqual(operator.__getitem__([1,2,3], 2), 3) AssertError(IndexError, operator.__getitem__, [1,2,3], 3) #__setitem__ AssertError(TypeError, operator.__setitem__, "abc", 2, "d") t_list = [1,2,3] operator.__setitem__(t_list, 2, 4) AreEqual(t_list, [1,2,4]) AssertError(IndexError, operator.__setitem__, [1,2,3], 4, 9) #__delitem__ #UNIMPLEMENTED #AssertError(TypeError, operator.__delitem__, "abc", 2) t_list = [1,2,3] operator.__delitem__(t_list, 2) AreEqual(t_list, [1,2]) AssertError(IndexError, operator.__delitem__, [1,2,3], 4) #__repeat__ AreEqual(operator.__repeat__("abc", 2), "abcabc") AreEqual(operator.__repeat__("", 2), "") AreEqual(operator.__repeat__([1,2,3], 2), [1,2,3,1,2,3]) #__getslice__ AreEqual(operator.__getslice__("abc", 1, 2), "b") AreEqual(operator.__getslice__("abc", 0, 3), "abc") AreEqual(operator.__getslice__("", 0, 0), "") AreEqual(operator.__getslice__([1,2,3], 1, 2), [2]) AreEqual(operator.__getslice__([1,2,3], 0, 3), [1,2,3]) AreEqual(operator.__getslice__([], 0, 0), []) #__delslice__ t_list = [1,2,3] operator.__delslice__(t_list, 1, 2) AreEqual(t_list, [1,3]) t_list = [1,2,3] operator.__delslice__(t_list, 0, 3) AreEqual(t_list, []) t_list = [1,2,3] operator.__delslice__(t_list, 0, 0) AreEqual(t_list, [1,2,3]) #__setslice__ t_list = [1,2,3] operator.__setslice__(t_list, 1, 2, [9]) AreEqual(t_list, [1,9,3]) t_list = [1,2,3] operator.__setslice__(t_list, 0, 3, [9, 8]) AreEqual(t_list, [9, 8]) t_list = [1,2,3] operator.__setslice__(t_list, 0, 0, [9]) AreEqual(t_list, [9,1, 2,3])
def __truediv__(self, value): return operator.__truediv__(self._tensor, value)
#reversed pow = spice(lambda x, y: operator.pow(y, x), name='pow') __pow__ = spice(lambda x, y: operator.__pow__(y, x), name='__pow__') # reversed rshift = spice(lambda x, y: operator.rshift(y, x), name='rshift') __rshift__ = spice(lambda x, y: operator.__rshift__(y, x), name='__rshift__') # reversed sub = spice(lambda x, y: operator.sub(y, x), name='sub') __sub__ = spice(lambda x, y: operator.__sub__(y, x), name='__sub__') # reversed truediv = spice(lambda x, y: operator.truediv(y, x), name='truediv') __truediv__ = spice(lambda x, y: operator.__truediv__(y, x), name='__truediv__') xor = spice(lambda x, y: operator.xor(x, y), name='xor', doc=operator.xor.__doc__) __xor__ = spice(lambda x, y: operator.__xor__(x, y), name='__xor__', doc=operator.xor.__doc__) ################################################# neg = spice(lambda x: operator.neg(x), name='neg', doc=operator.neg.__doc__) __neg__ = spice(lambda x: operator.__neg__(x), name='__neg__', doc=operator.neg.__doc__)
def __rtruediv__(self, other): """other // self""" return __truediv__(other, get_wrapped_object(self))
def __truediv__(self, other): """self / other""" return __truediv__(get_wrapped_object(self), get_wrapped_object(other))
def __truediv__(x, y): if isinstance(x, callproxy): x = x.p_obj if isinstance(y, callproxy): y = y.p_obj return operator.__truediv__(x, y)
def calculate_moles(self): """Calculates the class mole quantity from grams.""" return round(operator.__truediv__(self.gram_amount, self.mass), 3)
def determine_main_compound(self, sample_mass, hydrocarbon=True, othercompound=False): ''' Determines the main compound in the combustion reaction. ''' mole_val = [] __hold = [] other = None # POTENTIAL: Add functionality for giving the other compound but not its mass, and having to calculate that. # other_calc = False if not hydrocarbon and othercompound: raise ValueError( "You cannot have a hydrocarbon that also contains another element." ) for index, compound in enumerate(self.__product_store): if 'O' not in compound.__repr__() and not isinstance( compound, (Element, SpecialElement)): raise TypeError( "The CombustionTrain class only takes in oxide compounds or elements." ) total = operator.__truediv__(compound.gram_amount, compound.mass) if compound.__repr__() == 'CO2': mole_val.append(total * compound.moles_in_compound('C')) __hold.append('C') elif compound.__repr__() == 'H2O': mole_val.append(total * compound.moles_in_compound('H')) __hold.append('H') else: if 'O' in compound.__repr__(): other = str(compound.__repr__()[0]) mole_val.append(total * compound.moles_in_compound(other)) else: other = str(compound.__repr__()) mole_val.append(compound.mole_amount) __hold.append(other) if hydrocarbon: if other is None: reactant = FormulaCompound(SpecialElement('C', moles = mole_val[0]), SpecialElement('H', moles = mole_val[1]))\ .empirical.__repr__() self.main_reactant = reactant else: reactant = FormulaCompound( SpecialElement('C', moles=mole_val[0]), SpecialElement('H', moles=mole_val[1]), SpecialElement(other, moles=mole_val[2])).empirical.__repr__() self.main_reactant = reactant if not hydrocarbon: e1 = mole_val[0] * Element('C').mass e2 = mole_val[1] * Element('H').mass reactant = None if other is None: e3 = sample_mass - e1 - e2 mole_val.append(operator.truediv(e3, Element('O').mass)) reactant = FormulaCompound( SpecialElement('C', moles=mole_val[0]), SpecialElement('H', moles=mole_val[1]), SpecialElement('O', moles=mole_val[2])).empirical.__repr__() else: e3 = mole_val[2] * Element(other).mass e4 = sample_mass - e1 - e2 - e3 mole_val.append(operator.truediv(e4, Element('O').mass)) reactant = FormulaCompound( SpecialElement('C', moles=mole_val[0]), SpecialElement('H', moles=mole_val[1]), SpecialElement(other, moles=mole_val[2]), SpecialElement('O', moles=mole_val[3])).empirical.__repr__() self.main_reactant = reactant return self.main_reactant
def __rtruediv__(self, other: tp.Any) -> tp.Any: operator = lambda rhs, lhs: operator_mod.__truediv__(lhs, rhs) operator.__name__ = 'r' + operator_mod.__truediv__.__name__ return self._ufunc_binary_operator(operator=operator, other=other)
# -*- coding: utf-8 -*- """ Created on Mon Sep 22 15:50:01 2014 @author: Flora Vincent """ import operator print(operator.__truediv__(245850922, 78256779)) print(operator.__truediv__(operator.add(1, operator.pow(5, 0.5)), 2))
def determine_main_compound(product_store, sample_mass, hydrocarbon=True, othercompound=False): ''' Determines the main compound in the combustion reaction. ''' global main_reactant mole_val = [] __hold = [] other = None for index, compound in enumerate(product_store): if 'O' not in compound.__repr__() and not isinstance( compound, (Element, SpecialElement)): raise TypeError( "The CombustionTrain class only takes in oxide compounds or elements." ) total = operator.__truediv__(compound.gram_amount, compound.mass) if compound.__repr__() == 'CO2': mole_val.append(total * compound.moles_in_compound('C')) __hold.append('C') elif compound.__repr__() == 'H2O': mole_val.append(total * compound.moles_in_compound('H')) __hold.append('H') else: if 'O' in compound.__repr__(): other = str(compound.__repr__()[0]) mole_val.append(total * compound.moles_in_compound(other)) else: other = str(compound.__repr__()) mole_val.append(compound.mole_amount) __hold.append(other) if hydrocarbon == True: if other == None: main_reactant = Compound.fromFormula(SpecialElement('C', moles=mole_val[0]), SpecialElement('H', moles=mole_val[1])) \ .store_comp else: main_reactant = Compound.fromFormula( SpecialElement('C', moles=mole_val[0]), SpecialElement('H', moles=mole_val[1]), SpecialElement(other, moles=mole_val[2])).store_comp if hydrocarbon == False: e1 = mole_val[0] * Element('C').mass e2 = mole_val[1] * Element('H').mass if other == None: e3 = sample_mass - e1 - e2 mole_val.append(operator.truediv(e3, Element('O').mass)) main_reactant = Compound.fromFormula( SpecialElement('C', moles=mole_val[0]), SpecialElement('H', moles=mole_val[1]), SpecialElement('O', moles=mole_val[2])).store_comp else: e3 = mole_val[2] * Element(other).mass e4 = sample_mass - e1 - e2 - e3 mole_val.append(operator.truediv(e4, Element('O').mass)) main_reactant = Compound.fromFormula( SpecialElement('C', moles=mole_val[0]), SpecialElement('H', moles=mole_val[1]), SpecialElement(other, moles=mole_val[2]), SpecialElement('O', moles=mole_val[3])).store_comp return main_reactant
def __truediv__(self, other): with self._lock: return operator.__truediv__(self.__wrapped__, other)
def eval(self, env): # The inputs could be expressions so we need to evaluate them first inp1 = self.left.eval(env) inp2 = self.right.eval(env) # Divide the result return operator.__truediv__(inp1, inp2)
def compute_corner_response(img): ## a) apply Sobel filters # Sobel filtering function is implemented in filtering_by_yoseob.py # sobel_img_x, sobel_img_y is derivatives along x and y direction respectively. sobel_img_x, sobel_img_y = my_sobel_filtering(img) # padded with 0 value to operate corner response sobel_img_x = image_padding_2d(sobel_img_x, 2, 1) # (img, padd_width, type=1) sobel_img_y = image_padding_2d(sobel_img_y, 2, 1) # type=1 means just padd with zeros. ## b) compute second moment matrix M uni_window = np.ones((5, 5)) patch_img_x = np.zeros((5, 5)) patch_img_y = np.zeros((5, 5)) size0 = img.shape[0] size1 = img.shape[1] ## c) variables for computing corner responses R = np.zeros((size0, size1)) _k = 0.04 _max_val = 0 elapsed_ = list(range(0, size0, size0 // 20)) for x in range(size0): for y in range(size1): # i. subtract mean of each image patch _sum_x = 0. _sum_y = 0. for i in range(x, x + 5): for j in range(y, y + 5): patch_img_x[i - x][j - y] = sobel_img_x[i][j] _sum_x = operator.__add__(_sum_x, sobel_img_x[i][j]) patch_img_y[i - x][j - y] = sobel_img_y[i][j] _sum_y = operator.__add__(_sum_y, sobel_img_y[i][j]) _avg_x = operator.__truediv__(_sum_x, 25) _avg_y = operator.__truediv__(_sum_y, 25) sum_of_ix_ix = 0. sum_of_ix_iy = 0. sum_of_iy_iy = 0. for i in range(5): for j in range(5): patch_img_x[i][j] = operator.__sub__( patch_img_x[i][j], _avg_x) patch_img_y[i][j] = operator.__sub__( patch_img_y[i][j], _avg_y) sum_of_ix_ix = operator.__add__( sum_of_ix_ix, operator.__mul__(patch_img_x[i][j], patch_img_x[i][j])) sum_of_ix_iy = operator.__add__( sum_of_ix_iy, operator.__mul__(patch_img_x[i][j], patch_img_y[i][j])) sum_of_iy_iy = operator.__add__( sum_of_iy_iy, operator.__mul__(patch_img_y[i][j], patch_img_y[i][j])) # ii. get second moment matrix # since we use uniform window, just calculated the summation of ix_ix, ix_iy, iy_iy respectively (above). M = np.array([[sum_of_ix_ix, sum_of_ix_iy], [sum_of_ix_iy, sum_of_iy_iy]]) eigenvalues, _ = np.linalg.eig(M) #print(eigen_values) e1 = eigenvalues[0] e2 = eigenvalues[1] R[x][y] = e1 * e2 - _k * ((e1 + e2)**2) ## d) normalize responses # i. negative values to 0, otherwise normalize to range [0, 1] if R[x][y] < 0: R[x][y] = 0 if R[x][y] > _max_val: _max_val = R[x][y] if x in elapsed_: print('.', end='') #print("_max_val:", _max_val) #normalizer = 1. #if _max_val != 0: # normalizer = 1 / _max_val #normalizer = 1 / 255 #normalizer = 1 / np.linalg.norm(R) for x in range(size0): for y in range(size1): R[x][y] = operator.__truediv__(R[x][y], _max_val) return R
def __truediv__(self, other): if isinstance(other, Number) or len(other) == 1: return Vector(map(lambda x: operator.__truediv__(x, other), self)) else: return Vector(map(operator.__truediv__, self, other))
def test_sanity(self): """Performs a set of simple sanity checks on most operators.""" #__abs__ self.assertEqual(operator.__abs__(0), 0) self.assertEqual(operator.__abs__(1), 1) self.assertEqual(operator.__abs__(-1), 1) self.assertEqual(operator.__abs__(0.0), 0.0) self.assertEqual(operator.__abs__(1.1), 1.1) self.assertEqual(operator.__abs__(-1.1), 1.1) self.assertEqual(operator.__abs__(long(0)), long(0)) self.assertEqual(operator.__abs__(long(1)), long(1)) self.assertEqual(operator.__abs__(-long(1)), long(1)) #__neg__ self.assertEqual(operator.__neg__(0), 0) self.assertEqual(operator.__neg__(1), -1) self.assertEqual(operator.__neg__(-1), 1) self.assertEqual(operator.__neg__(0.0), 0.0) self.assertEqual(operator.__neg__(1.1), -1.1) self.assertEqual(operator.__neg__(-1.1), 1.1) self.assertEqual(operator.__neg__(long(0)), long(0)) self.assertEqual(operator.__neg__(long(1)), -long(1)) self.assertEqual(operator.__neg__(-long(1)), long(1)) #__pos__ self.assertEqual(operator.__pos__(0), 0) self.assertEqual(operator.__pos__(1), 1) self.assertEqual(operator.__pos__(-1), -1) self.assertEqual(operator.__pos__(0.0), 0.0) self.assertEqual(operator.__pos__(1.1), 1.1) self.assertEqual(operator.__pos__(-1.1), -1.1) self.assertEqual(operator.__pos__(long(0)), long(0)) self.assertEqual(operator.__pos__(long(1)), long(1)) self.assertEqual(operator.__pos__(-long(1)), -long(1)) #__add__ self.assertEqual(operator.__add__(0, 0), 0) self.assertEqual(operator.__add__(1, 2), 3) self.assertEqual(operator.__add__(-1, 2), 1) self.assertEqual(operator.__add__(0.0, 0.0), 0.0) self.assertEqual(operator.__add__(1.1, 2.1), 3.2) self.assertEqual(operator.__add__(-1.1, 2.1), 1.0) self.assertEqual(operator.__add__(long(0), long(0)), long(0)) self.assertEqual(operator.__add__(long(1), long(2)), long(3)) self.assertEqual(operator.__add__(-long(1), long(2)), long(1)) #__sub__ self.assertEqual(operator.__sub__(0, 0), 0) self.assertEqual(operator.__sub__(1, 2), -1) self.assertEqual(operator.__sub__(-1, 2), -3) self.assertEqual(operator.__sub__(0.0, 0.0), 0.0) self.assertEqual(operator.__sub__(1.1, 2.1), -1.0) self.assertEqual(operator.__sub__(-1.1, 2.1), -3.2) self.assertEqual(operator.__sub__(long(0), long(0)), long(0)) self.assertEqual(operator.__sub__(long(1), long(2)), -long(1)) self.assertEqual(operator.__sub__(-long(1), long(2)), -long(3)) #__mul__ self.assertEqual(operator.__mul__(0, 0), 0) self.assertEqual(operator.__mul__(1, 2), 2) self.assertEqual(operator.__mul__(-1, 2), -2) self.assertEqual(operator.__mul__(0.0, 0.0), 0.0) self.assertEqual(operator.__mul__(2.0, 3.0), 6.0) self.assertEqual(operator.__mul__(-2.0, 3.0), -6.0) self.assertEqual(operator.__mul__(long(0), long(0)), long(0)) self.assertEqual(operator.__mul__(long(1), long(2)), long(2)) self.assertEqual(operator.__mul__(-long(1), long(2)), -long(2)) #__div__ self.assertEqual(operator.__div__(0, 1), 0) self.assertEqual(operator.__div__(4, 2), 2) self.assertEqual(operator.__div__(-1, 2), -1) self.assertEqual(operator.__div__(0.0, 1.0), 0.0) self.assertEqual(operator.__div__(4.0, 2.0), 2.0) self.assertEqual(operator.__div__(-4.0, 2.0), -2.0) self.assertEqual(operator.__div__(long(0), long(1)), long(0)) self.assertEqual(operator.__div__(long(4), long(2)), long(2)) self.assertEqual(operator.__div__(-long(4), long(2)), -long(2)) #__floordiv__ self.assertEqual(operator.__floordiv__(0, 1), 0) self.assertEqual(operator.__floordiv__(4, 2), 2) self.assertEqual(operator.__floordiv__(-1, 2), -1) self.assertEqual(operator.__floordiv__(0.0, 1.0), 0.0) self.assertEqual(operator.__floordiv__(4.0, 2.0), 2.0) self.assertEqual(operator.__floordiv__(-4.0, 2.0), -2.0) self.assertEqual(operator.__floordiv__(long(0), long(1)), long(0)) self.assertEqual(operator.__floordiv__(long(4), long(2)), long(2)) self.assertEqual(operator.__floordiv__(-long(4), long(2)), -long(2)) #__truediv__ self.assertEqual(operator.__truediv__(0, 1), 0) self.assertEqual(operator.__truediv__(4, 2), 2) self.assertEqual(operator.__truediv__(-1, 2), -0.5) self.assertEqual(operator.__truediv__(0.0, 1.0), 0.0) self.assertEqual(operator.__truediv__(4.0, 2.0), 2.0) self.assertEqual(operator.__truediv__(-1.0, 2.0), -0.5) self.assertEqual(operator.__truediv__(long(0), long(1)), long(0)) self.assertEqual(operator.__truediv__(long(4), long(2)), long(2)) self.assertEqual(operator.__truediv__(-long(4), long(2)), -long(2)) #__mod__ self.assertEqual(operator.__mod__(0, 1), 0) self.assertEqual(operator.__mod__(4, 2), 0) self.assertEqual(operator.__mod__(-1, 2), 1) self.assertEqual(operator.__mod__(0.0, 1.0), 0.0) self.assertEqual(operator.__mod__(4.0, 2.0), 0.0) self.assertEqual(operator.__mod__(-1.0, 2.0), 1.0) self.assertEqual(operator.__mod__(long(0), long(1)), long(0)) self.assertEqual(operator.__mod__(long(4), long(2)), long(0)) self.assertEqual(operator.__mod__(-long(4), long(2)), long(0)) #__inv__ self.assertEqual(operator.__inv__(0), -1) self.assertEqual(operator.__inv__(1), -2) self.assertEqual(operator.__inv__(-1), 0) self.assertEqual(operator.__inv__(long(0)), -long(1)) self.assertEqual(operator.__inv__(long(1)), -long(2)) self.assertEqual(operator.__inv__(-long(1)), long(0)) #__invert__ self.assertEqual(operator.__invert__(0), -1) self.assertEqual(operator.__invert__(1), -2) self.assertEqual(operator.__invert__(-1), 0) self.assertEqual(operator.__invert__(long(0)), -long(1)) self.assertEqual(operator.__invert__(long(1)), -long(2)) self.assertEqual(operator.__invert__(-long(1)), long(0)) #__lshift__ self.assertEqual(operator.__lshift__(0, 1), 0) self.assertEqual(operator.__lshift__(1, 1), 2) self.assertEqual(operator.__lshift__(-1, 1), -2) self.assertEqual(operator.__lshift__(long(0), 1), long(0)) self.assertEqual(operator.__lshift__(long(1), 1), long(2)) self.assertEqual(operator.__lshift__(-long(1), 1), -long(2)) #__rshift__ self.assertEqual(operator.__rshift__(1, 1), 0) self.assertEqual(operator.__rshift__(2, 1), 1) self.assertEqual(operator.__rshift__(-1, 1), -1) self.assertEqual(operator.__rshift__(long(1), 1), long(0)) self.assertEqual(operator.__rshift__(long(2), 1), long(1)) self.assertEqual(operator.__rshift__(-long(1), 1), -long(1)) #__not__ self.assertEqual(operator.__not__(0), 1) self.assertEqual(operator.__not__(1), 0) self.assertEqual(operator.__not__(-1), 0) self.assertEqual(operator.__not__(long(0)), 1) self.assertEqual(operator.__not__(long(1)), 0) self.assertEqual(operator.__not__(-long(1)), 0) #__and__ self.assertEqual(operator.__and__(0, 0), 0) self.assertEqual(operator.__and__(1, 1), 1) self.assertEqual(operator.__and__(0, 1), 0) self.assertEqual(operator.__and__(1, 0), 0) #__xor__ self.assertEqual(operator.__xor__(0, 0), 0) self.assertEqual(operator.__xor__(1, 1), 0) self.assertEqual(operator.__xor__(0, 1), 1) self.assertEqual(operator.__xor__(1, 0), 1) #__or__ self.assertEqual(operator.__or__(0, 0), 0) self.assertEqual(operator.__or__(1, 1), 1) self.assertEqual(operator.__or__(0, 1), 1) self.assertEqual(operator.__or__(1, 0), 1) #__concat__ self.assertEqual(operator.__concat__([0], [1]), [0,1]) self.assertEqual(operator.__concat__([2], [1]), [2,1]) self.assertEqual(operator.__concat__([-1], [1]), [-1,1]) #__contains__ self.assertTrue(operator.__contains__("abc", "c")) self.assertTrue(not operator.__contains__("abc", "d")) self.assertTrue(operator.__contains__("abc", "")) self.assertTrue(not operator.__contains__("", "c")) self.assertTrue(operator.__contains__([1,2,3], 1)) self.assertTrue(not operator.__contains__([1,2,3], 4)) #__getitem__ self.assertEqual(operator.__getitem__("abc", 2), "c") self.assertRaises(IndexError, operator.__getitem__, "abc", 3) self.assertEqual(operator.__getitem__([1,2,3], 2), 3) self.assertRaises(IndexError, operator.__getitem__, [1,2,3], 3) #__setitem__ self.assertRaises(TypeError, operator.__setitem__, "abc", 2, "d") t_list = [1,2,3] operator.__setitem__(t_list, 2, 4) self.assertEqual(t_list, [1,2,4]) self.assertRaises(IndexError, operator.__setitem__, [1,2,3], 4, 9) #__delitem__ #UNIMPLEMENTED #self.assertRaises(TypeError, operator.__delitem__, "abc", 2) t_list = [1,2,3] operator.__delitem__(t_list, 2) self.assertEqual(t_list, [1,2]) self.assertRaises(IndexError, operator.__delitem__, [1,2,3], 4) #__repeat__ self.assertEqual(operator.__repeat__("abc", 2), "abcabc") self.assertEqual(operator.__repeat__("", 2), "") self.assertEqual(operator.__repeat__([1,2,3], 2), [1,2,3,1,2,3]) #__getslice__ self.assertEqual(operator.__getslice__("abc", 1, 2), "b") self.assertEqual(operator.__getslice__("abc", 0, 3), "abc") self.assertEqual(operator.__getslice__("", 0, 0), "") self.assertEqual(operator.__getslice__([1,2,3], 1, 2), [2]) self.assertEqual(operator.__getslice__([1,2,3], 0, 3), [1,2,3]) self.assertEqual(operator.__getslice__([], 0, 0), []) #__delslice__ t_list = [1,2,3] operator.__delslice__(t_list, 1, 2) self.assertEqual(t_list, [1,3]) t_list = [1,2,3] operator.__delslice__(t_list, 0, 3) self.assertEqual(t_list, []) t_list = [1,2,3] operator.__delslice__(t_list, 0, 0) self.assertEqual(t_list, [1,2,3]) #__setslice__ t_list = [1,2,3] operator.__setslice__(t_list, 1, 2, [9]) self.assertEqual(t_list, [1,9,3]) t_list = [1,2,3] operator.__setslice__(t_list, 0, 3, [9, 8]) self.assertEqual(t_list, [9, 8]) t_list = [1,2,3] operator.__setslice__(t_list, 0, 0, [9]) self.assertEqual(t_list, [9,1, 2,3])
def __rtruediv__(self, other): proxiee = _get_proxiee(self) _logger.debug("__rtruediv__ on proxiee (%r)", proxiee) return operator.__truediv__(other, proxiee)
def __rtruediv__(self, other): return new_quantity_nonone(operator.__truediv__(other,self.number), (1.0 / self.unit).to_simple_form())
def process_image(PATH): my_img = cv2.imread(PATH, cv2.IMREAD_GRAYSCALE) my_img = cv2.resize(my_img, (28, 28)) ret, bw_img = cv2.threshold(my_img, 127, 255, cv2.THRESH_BINARY) bw_img = 255 - bw_img tensorflow.image.convert_image_dtype(bw_img, dtype = tensorflow.float32) bw_img = np.array([bw_img]).astype('float32') / 255.0 return bw_img LABELS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] # Dataset and training/test sets. set = keras.datasets.mnist (train_imgs, train_labels), (test_imgs, test_labels) = set.load_data() train_imgs = op.__truediv__(train_imgs, 255.0); test_imgs = op.__truediv__(test_imgs, 255.0) def show_example(): # Shows the first 25 images (and labels) in the training sample. # Call the function below if you want to see it. plt.figure(figsize = (10, 10)) for i in range(25): plt.subplot(5, 5, i + 1) plt.grid(False); plt.xticks([]); plt.yticks([]) plt.imshow(train_imgs[i], cmap = plt.cm.binary) plt.xlabel(LABELS[train_labels[i]]) plt.show() # The Network model = keras.models.Sequential([ keras.layers.Flatten(input_shape = (28, 28)),
def test_sanity(self): """Performs a set of simple sanity checks on most operators.""" #__abs__ self.assertEqual(operator.__abs__(0), 0) self.assertEqual(operator.__abs__(1), 1) self.assertEqual(operator.__abs__(-1), 1) self.assertEqual(operator.__abs__(0.0), 0.0) self.assertEqual(operator.__abs__(1.1), 1.1) self.assertEqual(operator.__abs__(-1.1), 1.1) self.assertEqual(operator.__abs__(big(0)), big(0)) self.assertEqual(operator.__abs__(big(1)), big(1)) self.assertEqual(operator.__abs__(-big(1)), big(1)) #__neg__ self.assertEqual(operator.__neg__(0), 0) self.assertEqual(operator.__neg__(1), -1) self.assertEqual(operator.__neg__(-1), 1) self.assertEqual(operator.__neg__(0.0), 0.0) self.assertEqual(operator.__neg__(1.1), -1.1) self.assertEqual(operator.__neg__(-1.1), 1.1) self.assertEqual(operator.__neg__(big(0)), big(0)) self.assertEqual(operator.__neg__(big(1)), -big(1)) self.assertEqual(operator.__neg__(-big(1)), big(1)) #__pos__ self.assertEqual(operator.__pos__(0), 0) self.assertEqual(operator.__pos__(1), 1) self.assertEqual(operator.__pos__(-1), -1) self.assertEqual(operator.__pos__(0.0), 0.0) self.assertEqual(operator.__pos__(1.1), 1.1) self.assertEqual(operator.__pos__(-1.1), -1.1) self.assertEqual(operator.__pos__(big(0)), big(0)) self.assertEqual(operator.__pos__(big(1)), big(1)) self.assertEqual(operator.__pos__(-big(1)), -big(1)) #__add__ self.assertEqual(operator.__add__(0, 0), 0) self.assertEqual(operator.__add__(1, 2), 3) self.assertEqual(operator.__add__(-1, 2), 1) self.assertEqual(operator.__add__(0.0, 0.0), 0.0) self.assertEqual(operator.__add__(1.1, 2.1), 3.2) self.assertEqual(operator.__add__(-1.1, 2.1), 1.0) self.assertEqual(operator.__add__(big(0), big(0)), big(0)) self.assertEqual(operator.__add__(big(1), big(2)), big(3)) self.assertEqual(operator.__add__(-big(1), big(2)), big(1)) #__sub__ self.assertEqual(operator.__sub__(0, 0), 0) self.assertEqual(operator.__sub__(1, 2), -1) self.assertEqual(operator.__sub__(-1, 2), -3) self.assertEqual(operator.__sub__(0.0, 0.0), 0.0) self.assertEqual(operator.__sub__(1.1, 2.1), -1.0) self.assertEqual(operator.__sub__(-1.1, 2.1), -3.2) self.assertEqual(operator.__sub__(big(0), big(0)), big(0)) self.assertEqual(operator.__sub__(big(1), big(2)), -big(1)) self.assertEqual(operator.__sub__(-big(1), big(2)), -big(3)) #__mul__ self.assertEqual(operator.__mul__(0, 0), 0) self.assertEqual(operator.__mul__(1, 2), 2) self.assertEqual(operator.__mul__(-1, 2), -2) self.assertEqual(operator.__mul__(0.0, 0.0), 0.0) self.assertEqual(operator.__mul__(2.0, 3.0), 6.0) self.assertEqual(operator.__mul__(-2.0, 3.0), -6.0) self.assertEqual(operator.__mul__(big(0), big(0)), big(0)) self.assertEqual(operator.__mul__(big(1), big(2)), big(2)) self.assertEqual(operator.__mul__(-big(1), big(2)), -big(2)) #__div__ self.assertEqual(operator.__div__(0, 1), 0) self.assertEqual(operator.__div__(4, 2), 2) self.assertEqual(operator.__div__(-1, 2), -1) self.assertEqual(operator.__div__(0.0, 1.0), 0.0) self.assertEqual(operator.__div__(4.0, 2.0), 2.0) self.assertEqual(operator.__div__(-4.0, 2.0), -2.0) self.assertEqual(operator.__div__(big(0), big(1)), big(0)) self.assertEqual(operator.__div__(big(4), big(2)), big(2)) self.assertEqual(operator.__div__(-big(4), big(2)), -big(2)) #__floordiv__ self.assertEqual(operator.__floordiv__(0, 1), 0) self.assertEqual(operator.__floordiv__(4, 2), 2) self.assertEqual(operator.__floordiv__(-1, 2), -1) self.assertEqual(operator.__floordiv__(0.0, 1.0), 0.0) self.assertEqual(operator.__floordiv__(4.0, 2.0), 2.0) self.assertEqual(operator.__floordiv__(-4.0, 2.0), -2.0) self.assertEqual(operator.__floordiv__(big(0), big(1)), big(0)) self.assertEqual(operator.__floordiv__(big(4), big(2)), big(2)) self.assertEqual(operator.__floordiv__(-big(4), big(2)), -big(2)) #__truediv__ self.assertEqual(operator.__truediv__(0, 1), 0) self.assertEqual(operator.__truediv__(4, 2), 2) self.assertEqual(operator.__truediv__(-1, 2), -0.5) self.assertEqual(operator.__truediv__(0.0, 1.0), 0.0) self.assertEqual(operator.__truediv__(4.0, 2.0), 2.0) self.assertEqual(operator.__truediv__(-1.0, 2.0), -0.5) self.assertEqual(operator.__truediv__(big(0), big(1)), big(0)) self.assertEqual(operator.__truediv__(big(4), big(2)), big(2)) self.assertEqual(operator.__truediv__(-big(4), big(2)), -big(2)) #__mod__ self.assertEqual(operator.__mod__(0, 1), 0) self.assertEqual(operator.__mod__(4, 2), 0) self.assertEqual(operator.__mod__(-1, 2), 1) self.assertEqual(operator.__mod__(0.0, 1.0), 0.0) self.assertEqual(operator.__mod__(4.0, 2.0), 0.0) self.assertEqual(operator.__mod__(-1.0, 2.0), 1.0) self.assertEqual(operator.__mod__(big(0), big(1)), big(0)) self.assertEqual(operator.__mod__(big(4), big(2)), big(0)) self.assertEqual(operator.__mod__(-big(4), big(2)), big(0)) #__inv__ self.assertEqual(operator.__inv__(0), -1) self.assertEqual(operator.__inv__(1), -2) self.assertEqual(operator.__inv__(-1), 0) self.assertEqual(operator.__inv__(big(0)), -big(1)) self.assertEqual(operator.__inv__(big(1)), -big(2)) self.assertEqual(operator.__inv__(-big(1)), big(0)) #__invert__ self.assertEqual(operator.__invert__(0), -1) self.assertEqual(operator.__invert__(1), -2) self.assertEqual(operator.__invert__(-1), 0) self.assertEqual(operator.__invert__(big(0)), -big(1)) self.assertEqual(operator.__invert__(big(1)), -big(2)) self.assertEqual(operator.__invert__(-big(1)), big(0)) #__lshift__ self.assertEqual(operator.__lshift__(0, 1), 0) self.assertEqual(operator.__lshift__(1, 1), 2) self.assertEqual(operator.__lshift__(-1, 1), -2) self.assertEqual(operator.__lshift__(big(0), 1), big(0)) self.assertEqual(operator.__lshift__(big(1), 1), big(2)) self.assertEqual(operator.__lshift__(-big(1), 1), -big(2)) #__rshift__ self.assertEqual(operator.__rshift__(1, 1), 0) self.assertEqual(operator.__rshift__(2, 1), 1) self.assertEqual(operator.__rshift__(-1, 1), -1) self.assertEqual(operator.__rshift__(big(1), 1), big(0)) self.assertEqual(operator.__rshift__(big(2), 1), big(1)) self.assertEqual(operator.__rshift__(-big(1), 1), -big(1)) #__not__ self.assertEqual(operator.__not__(0), 1) self.assertEqual(operator.__not__(1), 0) self.assertEqual(operator.__not__(-1), 0) self.assertEqual(operator.__not__(big(0)), 1) self.assertEqual(operator.__not__(big(1)), 0) self.assertEqual(operator.__not__(-big(1)), 0) #__and__ self.assertEqual(operator.__and__(0, 0), 0) self.assertEqual(operator.__and__(1, 1), 1) self.assertEqual(operator.__and__(0, 1), 0) self.assertEqual(operator.__and__(1, 0), 0) #__xor__ self.assertEqual(operator.__xor__(0, 0), 0) self.assertEqual(operator.__xor__(1, 1), 0) self.assertEqual(operator.__xor__(0, 1), 1) self.assertEqual(operator.__xor__(1, 0), 1) #__or__ self.assertEqual(operator.__or__(0, 0), 0) self.assertEqual(operator.__or__(1, 1), 1) self.assertEqual(operator.__or__(0, 1), 1) self.assertEqual(operator.__or__(1, 0), 1) #__concat__ self.assertEqual(operator.__concat__([0], [1]), [0, 1]) self.assertEqual(operator.__concat__([2], [1]), [2, 1]) self.assertEqual(operator.__concat__([-1], [1]), [-1, 1]) #__contains__ self.assertTrue(operator.__contains__("abc", "c")) self.assertTrue(not operator.__contains__("abc", "d")) self.assertTrue(operator.__contains__("abc", "")) self.assertTrue(not operator.__contains__("", "c")) self.assertTrue(operator.__contains__([1, 2, 3], 1)) self.assertTrue(not operator.__contains__([1, 2, 3], 4)) #__getitem__ self.assertEqual(operator.__getitem__("abc", 2), "c") self.assertRaises(IndexError, operator.__getitem__, "abc", 3) self.assertEqual(operator.__getitem__([1, 2, 3], 2), 3) self.assertRaises(IndexError, operator.__getitem__, [1, 2, 3], 3) #__setitem__ self.assertRaises(TypeError, operator.__setitem__, "abc", 2, "d") t_list = [1, 2, 3] operator.__setitem__(t_list, 2, 4) self.assertEqual(t_list, [1, 2, 4]) self.assertRaises(IndexError, operator.__setitem__, [1, 2, 3], 4, 9) #__delitem__ #UNIMPLEMENTED #self.assertRaises(TypeError, operator.__delitem__, "abc", 2) t_list = [1, 2, 3] operator.__delitem__(t_list, 2) self.assertEqual(t_list, [1, 2]) self.assertRaises(IndexError, operator.__delitem__, [1, 2, 3], 4) #__repeat__ self.assertEqual(operator.__repeat__("abc", 2), "abcabc") self.assertEqual(operator.__repeat__("", 2), "") self.assertEqual(operator.__repeat__([1, 2, 3], 2), [1, 2, 3, 1, 2, 3]) #__getslice__ self.assertEqual(operator.__getslice__("abc", 1, 2), "b") self.assertEqual(operator.__getslice__("abc", 0, 3), "abc") self.assertEqual(operator.__getslice__("", 0, 0), "") self.assertEqual(operator.__getslice__([1, 2, 3], 1, 2), [2]) self.assertEqual(operator.__getslice__([1, 2, 3], 0, 3), [1, 2, 3]) self.assertEqual(operator.__getslice__([], 0, 0), []) #__delslice__ t_list = [1, 2, 3] operator.__delslice__(t_list, 1, 2) self.assertEqual(t_list, [1, 3]) t_list = [1, 2, 3] operator.__delslice__(t_list, 0, 3) self.assertEqual(t_list, []) t_list = [1, 2, 3] operator.__delslice__(t_list, 0, 0) self.assertEqual(t_list, [1, 2, 3]) #__setslice__ t_list = [1, 2, 3] operator.__setslice__(t_list, 1, 2, [9]) self.assertEqual(t_list, [1, 9, 3]) t_list = [1, 2, 3] operator.__setslice__(t_list, 0, 3, [9, 8]) self.assertEqual(t_list, [9, 8]) t_list = [1, 2, 3] operator.__setslice__(t_list, 0, 0, [9]) self.assertEqual(t_list, [9, 1, 2, 3])
def __rtruediv__(self, other): assert isinstance(other, float) or isinstance( other, int) or isinstance(other, bool) return Point(__truediv__(other, self.x), __truediv__(other, self.y))