Skip to content

Commit

Permalink
Fix up magic operators
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens committed Nov 20, 2024
1 parent 5a36312 commit 439a36e
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions kurbopy/magic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import functools
import sys


# This module allows for polymorphic operator overloading between objects of
Expand All @@ -24,8 +25,41 @@ def do_magic(self, rhs, methodname):
raise TypeError("Cannot %s %s by %s" % (methodname, mytype, other_type))


<<<<<<< HEAD
for method in ["mul", "add", "sub", "isub", "iadd"]:
magic = functools.partial(do_magic, methodname=method)
magic.__name__ = method
magic.__doc__ = "Magic method " + method
globals()["magic_" + method] = magic
=======
def magic_sub(self, rhs):
other_type = get_magic_name(rhs)
mytype = get_magic_name(self)
if hasattr(self, "_sub_" + other_type):
return getattr(self, "_sub_" + other_type)(rhs)
raise TypeError("Cannot subtract %s from %s" % (other_type, mytype))


def magic_isub(self, rhs):
other_type = get_magic_name(rhs)
mytype = get_magic_name(self)
if hasattr(self, "_isub_" + other_type):
return getattr(self, "_isub_" + other_type)(rhs)
raise TypeError("Cannot subtract %s from %s" % (other_type, mytype))


def magic_add(self, rhs):
other_type = get_magic_name(rhs)
mytype = get_magic_name(self)
if hasattr(self, "_add_" + other_type):
return getattr(self, "_add_" + other_type)(rhs)
raise TypeError("Cannot add %s to %s" % (other_type, mytype))


def magic_iadd(self, rhs):
other_type = get_magic_name(rhs)
mytype = get_magic_name(self)
if hasattr(self, "_iadd_" + other_type):
return getattr(self, "_iadd_" + other_type)(rhs)
raise TypeError("Cannot add %s to %s" % (other_type, mytype))
>>>>>>> 729889f (Fix up magic operators)

0 comments on commit 439a36e

Please sign in to comment.