# HG changeset patch # User Paul Boddie # Date 1275246548 -7200 # Node ID 7fa0f8a72b494aa2dd55df02cf3ed39f54eea977 # Parent 10506f500308ffad12c04f34650b5245ddc76236 Added a missing operator definition to micropython.common.augassign_methods. Added the operator module which could eventually be used to support all usage of operators in a program. diff -r 10506f500308 -r 7fa0f8a72b49 lib/operator.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/operator.py Sun May 30 21:09:08 2010 +0200 @@ -0,0 +1,95 @@ +#!/usr/bin/env python + +""" +Operator support. + +Copyright (C) 2010 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +def binary_op(a, b, left_name, right_name): + + """ + A single parameterised function providing the binary operator mechanism for + arguments 'a' and 'b' using methods of the given 'left_name' and + 'right_name'. + """ + + # First, try and get a method for the left argument, and then call it with + # the right argument. + + try: + fn = getattr(a, left_name) + except AttributeError: + pass + else: + result = fn(b) + if result is not NotImplemented: + return result + + # Otherwise, try and get a method for the right argument, and then call it + # with the left argument. + + try: + fn = getattr(b, right_name) + except AttributeError: + pass + else: + result = fn(a) + if result is not NotImplemented: + return result + + # Where no methods were available, or if neither method could support the + # operation, raise an exception. + + raise TypeError + +def add(a, b): + return binary_op(a, b, "__add__", "__radd__") + +def and_(a, b): + return binary_op(a, b, "__and__", "__rand__") + +def div(a, b): + return binary_op(a, b, "__div__", "__rdiv__") + +def floordiv(a, b): + return binary_op(a, b, "__floordiv__", "__rfloordiv__") + +def lshift(a, b): + return binary_op(a, b, "__lshift__", "__rlshift__") + +def mod(a, b): + return binary_op(a, b, "__mod__", "__rmod__") + +def mul(a, b): + return binary_op(a, b, "__mul__", "__rmul__") + +def or(a, b): + return binary_op(a, b, "__or__", "__ror__") + +def pow(a, b): + return binary_op(a, b, "__pow__", "__rpow__") + +def rshift(a, b): + return binary_op(a, b, "__rshift__", "__rrshift__") + +def sub(a, b): + return binary_op(a, b, "__sub__", "__rsub__") + +def xor(a, b): + return binary_op(a, b, "__xor__", "__rxor__") + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 10506f500308 -r 7fa0f8a72b49 micropython/common.py --- a/micropython/common.py Wed Apr 21 00:58:29 2010 +0200 +++ b/micropython/common.py Sun May 30 21:09:08 2010 +0200 @@ -97,6 +97,7 @@ "-=" : ("__isub__", ("__sub__", "__rsub__")), "*=" : ("__imul__", ("__mul__", "__rmul__")), "/=" : ("__idiv__", ("__div__", "__rdiv__")), + "//=" : ("__ifloordiv__", ("__floordiv__", "__rfloordiv__")), "%=" : ("__imod__", ("__mod__", "__rmod__")), "**=" : ("__ipow__", ("__pow__", "__rpow__")), "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")),