From 4802e7863d852acf9a2645435c4e1dc4c1b45672 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Fri, 18 Jan 2008 20:17:24 -0500 Subject: [PATCH] Support truediv and floordiv in ArithmeticList. --- src/arithmetic_container.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/arithmetic_container.py b/src/arithmetic_container.py index aace477..e81d805 100644 --- a/src/arithmetic_container.py +++ b/src/arithmetic_container.py @@ -1,3 +1,4 @@ +from __future__ import division from decorator import decorator import operator @@ -36,6 +37,8 @@ class ArithmeticList(list): def __sub__(self, other): return self.binary_operator(other, operator.sub) def __mul__(self, other): return self.binary_operator(other, operator.mul) def __div__(self, other): return self.binary_operator(other, operator.div) + def __truediv__(self, other): return self.binary_operator(other, operator.truediv) + def __floordiv__(self, other): return self.binary_operator(other, operator.floordiv) def __mod__(self, other): return self.binary_operator(other, operator.mod) def __pow__(self, other): return self.binary_operator(other, operator.pow) def __lshift__(self, other): return self.binary_operator(other, operator.lshift) @@ -48,6 +51,8 @@ class ArithmeticList(list): def __rsub__(self, other): return self.reverse_binary_operator(other, operator.sub) def __rmul__(self, other): return self.reverse_binary_operator(other, operator.mul) def __rdiv__(self, other): return self.reverse_binary_operator(other, operator.div) + def __rtruediv__(self, other): return self.reverse_binary_operator(other, operator.truediv) + def __rfloordiv__(self, other): return self.reverse_binary_operator(other, operator.floordiv) def __rmod__(self, other): return self.reverse_binary_operator(other, operator.mod) def __rpow__(self, other): return self.reverse_binary_operator(other, operator.pow) def __rlshift__(self, other): return self.reverse_binary_operator(other, operator.lshift) @@ -75,11 +80,24 @@ class ArithmeticList(list): return self def __idiv__(self, other): + from operator import div + self.assert_same_length(other) + for i in range(len(self)): + self[i] = div(self[i], other[i]) + return self + + def __itruediv__(self, other): self.assert_same_length(other) for i in range(len(self)): self[i] /= other[i] return self + def __ifloordiv__(self, other): + self.assert_same_length(other) + for i in range(len(self)): + self[i] //= other[i] + return self + def __imod__(self, other): self.assert_same_length(other) for i in range(len(self)): -- GitLab