0

I have a code error that I need to debug but I'm not sure where I went wrong. When I run my code I got:

Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    c3_plus_0i = create_complex(create_ordinary(3), create_ordinary(0))
  File "<pyshell#30>", line 2, in create_complex
    return get("make","complex")(x,y)
TypeError: 'NoneType' object is not callable

This is the code that I have:

from generic_arith_min import *

def install_complex_package():
    def make_com(x,y):
        return tag(repcom(x,y))
    def repcom(x,y):
        return (x,y)
    def real(x):
        return x[0]
    def imag(x):
        return x[1]
    def tag(x):
        return attach_tag("complex",x)
    # add,sub,mul,div: (RepCom, RepCom) -> Generic-Com
    def add_com(x,y):
        return make_com( add(real(x), real(y)),
                         add(imag(x), imag(y)) )
    def sub_com(x,y):
        return make_com( sub(real(x), real(y)),
                         sub(imag(x), imag(y)) )
    def mul_com(x,y):
         return make_com(sub(mul(real(x), real(y)),
                             mul(imag(x), imag(y))),
                         add(mul(real(x), imag(y)),
                             mul(real(y), imag(x))))
    def div_com(x,y):
        com_conj = complex_conjugate(y)
        x_times_com_conj = content(mul_com(x, com_conj))
        y_times_com_conj = content(mul_com(y, com_conj))
        return make_com(div(real(x_times_com_conj), real(y_times_com_conj)),
                        div(imag(x_times_com_conj), real(y_times_com_conj)));
    def complex_conjugate(x):
        return (real(x),negate(imag(x)))

    def negate_com(x): # (RepCom) -> Generic-Com
        return make_com(negate(real(x)), negate(imag(x)))

    def is_zero_com(x): # (RepCom) -> Py-Bool
        if is_zero(real(x)) and is_zero(imag(x)):
            return True
        else:
            return False

    def is_eq_com(x,y): # (RepCom, RepCom) -> Py-Bool
        if is_equal(real(x),real(y)) and is_equal(imag(x),imag(y)):
            return True
        else:
            return False

    put("make","complex", make_com)
    put("add",("complex","complex"), add_com)
    put("sub",("complex","complex"), sub_com)
    put("mul",("complex","complex"), mul_com)
    put("div",("complex","complex"), div_com)
    put("negate",("complex",), negate_com) 
    put("is_zero",("complex",), is_zero_com) 
    put("is_equal",("complex","complex"), is_eq_com)

    def repord_to_repcom(x):
        return repcom(create_ordinary(x),create_ordinary(0))

    def CCmethod_to_OCmethod(method):
        return lambda ord, com: method(repord_to_repcom(ord), com)


    def CCmethod_to_COmethod(method):
        return lambda com, ord: method(com, repord_to_repcom(ord))


    put("add",("ordinary","complex"), CCmethod_to_OCmethod(add_com))
    put("add",("complex","ordinary"), CCmethod_to_COmethod(add_com))

    put("sub",("ordinary","complex"), CCmethod_to_OCmethod(sub_com))
    put("sub",("complex","ordinary"), CCmethod_to_COmethod(sub_com))

    put("mul",("ordinary","complex"), CCmethod_to_OCmethod(mul_com))
    put("mul",("complex","ordinary"), CCmethod_to_COmethod(mul_com))

    put("div",("ordinary","complex"), CCmethod_to_OCmethod(div_com))
    put("div",("complex","ordinary"), CCmethod_to_COmethod(div_com))
    put("is_equal",("ordinary","complex"), CCmethod_to_OCmethod(is_eq_com))
    put("is_equal",("complex","ordinary"), CCmethod_to_COmethod(is_eq_com))



def create_complex(x,y):
    return get("make","complex")(x,y)


#################
# Do not change #
#################


n3 = create_ordinary(3)
c3_plus_0i = create_complex(create_ordinary(3), create_ordinary(0))
c2_plus_7i = create_complex(create_ordinary(2), create_ordinary(7))

def gradeThis_complex_package():
    complexA = is_equal(n3, c3_plus_0i)
    complexB = is_equal(sub(add(n3, c2_plus_7i), c2_plus_7i), n3)
    if complexA and complexB:
        print("Well done! Your install_complex_package is complete!")
    else:
        print("Please check your solution for install_complex_package.")


gradeThis_complex_package()

Below is the supported file that it is imported from:

    _operation_table = {} #variables with prefixed with an _ are by convention, for internal use and should not be touched.

def attach_tag(tag, content):
    return (tag, content)


def type_tag(datum):
    if type(datum) == tuple and len(datum) == 2:
        return datum[0]
    raise Exception('Bad tagged datum -- type_tag ', datum)


def content(datum):
    if type(datum) == tuple and len(datum) == 2:
        return datum[1]
    raise Exception('Bad tagged datum -- content ', datum)


def put(op, types, value):
    if op not in _operation_table:
        _operation_table[op] = {}
    _operation_table[op][types] = value


def get(op, types):
    if op in _operation_table and types in _operation_table[op]:
        return _operation_table[op][types]
    else:
        return None

def apply_generic(op, *args):
    type_tags = tuple(map(type_tag, args))
    proc = get(op, type_tags)
    if proc:
        return proc(*map(content, args))
    raise Exception('No method for these types -- apply_generic', (op, type_tags))

##########################
# Generic Number Package #
##########################

#generic operators we want to support
def add(x,y):
    return apply_generic("add", x, y)

def sub(x,y):
    return apply_generic("sub", x, y)

def mul(x,y):
    return apply_generic("mul", x, y)

def div(x,y):
    return apply_generic("div", x, y)

def negate(x):
    return apply_generic("negate", x)

def is_zero(x):
    return apply_generic("is_zero", x)

def is_equal(x, y):
    return apply_generic("is_equal", x, y)

#composite generic operators

def square(x):
    return mul(x,x)

#Generic ordinary number package

def install_ordinary_package():
    def make_ord(x):
        return tag(x)
    def tag(x):
        return attach_tag("ordinary", x)
    # add,sub,mul,div: (RepOrd, RepOrd) -> Generic-OrdNum
    def add_ord(x,y):
        return make_ord(x+y)
    def sub_ord(x,y):
        return make_ord(x-y)
    def mul_ord(x,y):
        return make_ord(x*y)
    def div_ord(x,y):
        return make_ord(x/y)
    def negate_ord(x): # (RepOrd) -> Generic-Ord
        return make_ord(-x)
    def is_zero_ord(x): # (RepOrd) -> Py-Bool
        return x == 0
    def is_equal_ord(x,y): # (RepOrd, RepOrd) -> Py-Bool
        return x == y
    put("make","ordinary", make_ord)
    put("negate",("ordinary",), negate_ord)
    put("is_zero",("ordinary",), is_zero_ord)
    put("add",("ordinary","ordinary"), add_ord)
    put("sub",("ordinary","ordinary"), sub_ord)
    put("mul",("ordinary","ordinary"), mul_ord)
    put("div",("ordinary","ordinary"), div_ord)
    put("is_equal",("ordinary","ordinary"), is_equal_ord)

install_ordinary_package()

def create_ordinary(x):
    return get("make", "ordinary")(x)

Where exactly did the above code cause the rise to this error? If anyone could help I would really appreciate it. Thank you!

2
  • 2
    def get(op, types): if op in _operation_table and types in _operation_table[op]: return _operation_table[op][types] else: return None: sometimes get returns None ... Commented Mar 27, 2017 at 12:07
  • Be wary of defining something with the property decorator when it should be a regular function call. That throws this too. It confused me for a good 15 min until I spotted the @property! Commented Mar 9, 2024 at 6:55

1 Answer 1

1

Your _operation_table dictionary does not contain an entry with make and complex therefore your get() function returns None which in fact is not callable.

 print("complex" in _operation_table["make"])
 False

Perhaps you've forgotten to call install_complex_package (in the same way you called install_ordinary_package) which adds the required function to _operation_table. Once called, your code works fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.