Пример #1
0
    def __set__(self, obj, items):
        old_items, items = self.split_old_new(obj, items)

        if old_items is not None:
            items = _operator.__iadd__(old_items, items)

        setattr(obj, self.attr, items)
Пример #2
0
    def __set__(self, obj, items):
        old_items, items = self.split_old_new(obj, items)

        if self.add_meth is not None:
            func = getattr(obj, self.add_meth)

            for item in items:
                func(item)

        if old_items is not None:
            items = _operator.__iadd__(old_items, items)

        setattr(obj, self.attr, items)
Пример #3
0
 def test_inplace(self):
     class C(object):
         def __iadd__     (self, other): return "iadd"
         def __iand__     (self, other): return "iand"
         def __idiv__     (self, other): return "idiv"
         def __ifloordiv__(self, other): return "ifloordiv"
         def __ilshift__  (self, other): return "ilshift"
         def __imod__     (self, other): return "imod"
         def __imul__     (self, other): return "imul"
         def __imatmul__  (self, other): return "imatmul"
         def __ior__      (self, other): return "ior"
         def __ipow__     (self, other): return "ipow"
         def __irshift__  (self, other): return "irshift"
         def __isub__     (self, other): return "isub"
         def __itruediv__ (self, other): return "itruediv"
         def __ixor__     (self, other): return "ixor"
         def __getitem__(self, other): return 5  # so that C is a sequence
     c = C()
     self.assertEqual(operator.iadd     (c, 5), "iadd")
     self.assertEqual(operator.iand     (c, 5), "iand")
     self.assertEqual(operator.idiv     (c, 5), "idiv")
     self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv")
     self.assertEqual(operator.ilshift  (c, 5), "ilshift")
     self.assertEqual(operator.imod     (c, 5), "imod")
     self.assertEqual(operator.imul     (c, 5), "imul")
     self.assertEqual(operator.imatmul  (c, 5), "imatmul")
     self.assertEqual(operator.ior      (c, 5), "ior")
     self.assertEqual(operator.ipow     (c, 5), "ipow")
     self.assertEqual(operator.irshift  (c, 5), "irshift")
     self.assertEqual(operator.isub     (c, 5), "isub")
     self.assertEqual(operator.itruediv (c, 5), "itruediv")
     self.assertEqual(operator.ixor     (c, 5), "ixor")
     self.assertEqual(operator.iconcat  (c, c), "iadd")
     self.assertEqual(operator.irepeat  (c, 5), "imul")
     self.assertEqual(operator.__iadd__     (c, 5), "iadd")
     self.assertEqual(operator.__iand__     (c, 5), "iand")
     self.assertEqual(operator.__idiv__     (c, 5), "idiv")
     self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv")
     self.assertEqual(operator.__ilshift__  (c, 5), "ilshift")
     self.assertEqual(operator.__imod__     (c, 5), "imod")
     self.assertEqual(operator.__imul__     (c, 5), "imul")
     self.assertEqual(operator.__imatmul__  (c, 5), "imatmul")
     self.assertEqual(operator.__ior__      (c, 5), "ior")
     self.assertEqual(operator.__ipow__     (c, 5), "ipow")
     self.assertEqual(operator.__irshift__  (c, 5), "irshift")
     self.assertEqual(operator.__isub__     (c, 5), "isub")
     self.assertEqual(operator.__itruediv__ (c, 5), "itruediv")
     self.assertEqual(operator.__ixor__     (c, 5), "ixor")
     self.assertEqual(operator.__iconcat__  (c, c), "iadd")
     self.assertEqual(operator.__irepeat__  (c, 5), "imul")
Пример #4
0
 def test_inplace(self):
     class C(object):
         def __iadd__     (self, other): return "iadd"
         def __iand__     (self, other): return "iand"
         def __idiv__     (self, other): return "idiv"
         def __ifloordiv__(self, other): return "ifloordiv"
         def __ilshift__  (self, other): return "ilshift"
         def __imod__     (self, other): return "imod"
         def __imul__     (self, other): return "imul"
         def __ior__      (self, other): return "ior"
         def __ipow__     (self, other): return "ipow"
         def __irshift__  (self, other): return "irshift"
         def __isub__     (self, other): return "isub"
         def __itruediv__ (self, other): return "itruediv"
         def __ixor__     (self, other): return "ixor"
         def __getitem__(self, other): return 5  # so that C is a sequence
     c = C()
     self.assertEqual(operator.iadd     (c, 5), "iadd")
     self.assertEqual(operator.iand     (c, 5), "iand")
     self.assertEqual(operator.idiv     (c, 5), "idiv")
     self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv")
     self.assertEqual(operator.ilshift  (c, 5), "ilshift")
     self.assertEqual(operator.imod     (c, 5), "imod")
     self.assertEqual(operator.imul     (c, 5), "imul")
     self.assertEqual(operator.ior      (c, 5), "ior")
     self.assertEqual(operator.ipow     (c, 5), "ipow")
     self.assertEqual(operator.irshift  (c, 5), "irshift")
     self.assertEqual(operator.isub     (c, 5), "isub")
     self.assertEqual(operator.itruediv (c, 5), "itruediv")
     self.assertEqual(operator.ixor     (c, 5), "ixor")
     self.assertEqual(operator.iconcat  (c, c), "iadd")
     self.assertEqual(operator.irepeat  (c, 5), "imul")
     self.assertEqual(operator.__iadd__     (c, 5), "iadd")
     self.assertEqual(operator.__iand__     (c, 5), "iand")
     self.assertEqual(operator.__idiv__     (c, 5), "idiv")
     self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv")
     self.assertEqual(operator.__ilshift__  (c, 5), "ilshift")
     self.assertEqual(operator.__imod__     (c, 5), "imod")
     self.assertEqual(operator.__imul__     (c, 5), "imul")
     self.assertEqual(operator.__ior__      (c, 5), "ior")
     self.assertEqual(operator.__ipow__     (c, 5), "ipow")
     self.assertEqual(operator.__irshift__  (c, 5), "irshift")
     self.assertEqual(operator.__isub__     (c, 5), "isub")
     self.assertEqual(operator.__itruediv__ (c, 5), "itruediv")
     self.assertEqual(operator.__ixor__     (c, 5), "ixor")
     self.assertEqual(operator.__iconcat__  (c, c), "iadd")
     self.assertEqual(operator.__irepeat__  (c, 5), "imul")
Пример #5
0
 def __iadd__(self, other):
     """self += other"""
     return __iadd__(get_wrapped_object(self), get_wrapped_object(other))
Пример #6
0
 def update_event(self, inp=-1):
     self.set_output_val(0, operator.__iadd__(self.input(0), self.input(1)))
Пример #7
0
from operator import __add__, __iadd__
import operator
# Immutable objects
a = 10;b = 20;m = 30;n = 40
p = operator.add(a, b)
q = operator.iadd(m, n)
print(p)
print(q)
sumVal = __add__(a, b)
print("Val for a:%d, b:%d is sum:%d" % (a, b, sumVal))
sumVal2 = __iadd__(a, b)
print("Val for a:%d, b:%d is sum:%d" % (a, b, sumVal2))
# Mutable Objects ==> updation and assignment both are carried out in case of mutable targets.
listEx = [1, 2, 3, 4]
z = operator.add(listEx, [55, 44, 99])
print("Updated list is: ", z)
print("Previously it was: ", listEx)
z2 = operator.iadd(listEx, [55, 88, 99])
print("Updated list is: ", z2)
print("Previously it was: ", listEx)  # works like a+=b principal for mutable objects.