0
import re, traceback, keyword

def pnamedtuple(type_name, field_names, mutable=False):
    def show_listing(s):
        for i, l in enumerate(s.split('\n'), 1):
            print('{num: >1} {txt}'.format(num= i, txt= l.rstrip()))

    # put your code here
    # bind class_definition (used below) to the string constructed for the     class
    field_names = field_names.replace(',', ' ').split()
    if type(type_name) == int or type(field_names) == set or type_name[0].isalpha() == False:
        raise SyntaxError

    class_template = '''\
class {type_name}:
    def __init__(self, {fields}):
        self._fields = [{fields}]
        self._mutable = False 
        self.a = self._fields[0]
        self.b = self._fields[1]
        self.c = self._fields[2]

    def __repr__(self):
        return 'Triple1' + '(a='+self.a+',b='+self.b+',c=self.c)'

    def get_a(self):
        return self.a

    def get_b(self):
        return self.b

    def get_c(self):
        return self.c

    def __getitem__(self,i):
        if type(i) == int:
            if i > 2:
                raise IndexError

        if type(i) == str:
            if i not in 'abc':
                raise IndexError

        if i == 0 or i == 'a':
            return self.a
        if i == 1 or i == 'b':
            return self.b
        if i == 2 or i == 'c':
            return self.c

    def _replace(self, **kwargs):
        if self._mutable:
            for key, value in kwargs.iteritems():
                setattr(self, key, value)
        else:
            for field in self._fields:
                if field not in kwargs:
                    kwargs[field] = getattr(self, field)

   '''

    class_definition = class_template.format(
    type_name = type_name,
    fields = ','.join(name for name in field_names)
    )

    # For initial debugging, always show the source code of the class
    #show_listing(class_definition)

    # Execute the class_definition string in a local namespace, binding the
    #   name source_code in its dictionary to the class_defintion; return the
    #   class object created; if there is a syntax error, list the class and
    #   also show the error

    name_space = dict(__name__='pnamedtuple_{type_name}'.format(type_name= type_name))
    print(name_space)
    try:
        exec(class_definition,name_space)
        name_space[type_name].source_code = class_definition
    except(SyntaxError, TypeError):
        show_listing(class_definition)
        traceback.print_exc()
    return name_space[type_name]

an example call to this function is Point = pnamedtuple('Point', ['x','y'], mutable=False), which is equivalent to writing Point = pnamedtuple('Point', 'x y') or Point = pnamedtuple('Point', 'x,y') A legal name for the type and fields must start with a letter which can be followed by 0 or more letters, digits, or underscore characters

I am working on the repr method. it returns a string. an example will be: For Point, if we defined origin = Point(0,0) then calling repr(origin) would return 'Point(x=0,y=0)'. I try to call

t1 = Triple1(1,2,3)
repr(t1)

it give me the following error:

18 # Test init/repr
20 *Error: repr(t1) -> <pnamedtuple_Triple1.Triple1 object at 0x015AF7B0> but should -> Triple1(a=1,b=2,c=3)
22 *Error: repr(t2) -> <pnamedtuple_Triple2.Triple2 object at 0x015AF9D0> but should -> Triple2(a=1,b=2,c=3)
23 *Error: t3 = Triple3(1,2,3) raised exception NameError: name 'Triple3' is not defined
24 *Error: repr(t3) raised exception NameError: name 't3' is not defined
25 *Error: t3 = Triple3(c=3,b=2,a=1) raised exception NameError: name 'Triple3' is not defined
26 *Error: repr(t3) raised exception NameError: name 't3' is not defined
28 *Error: repr(tok) -> <pnamedtuple_Triple_OK.Triple_OK object at 0x015AF850> but should -> Triple_OK(a17=1,b__1=2,c__2=3)

this is the bsc.txt code for repr:

# Test init/repr
c-->t1 = Triple1(1,2,3)
e-->repr(t1)-->Triple1(a=1,b=2,c=3)
c-->t2 = Triple2(1,2,3)
e-->repr(t2)-->Triple2(a=1,b=2,c=3)
c-->t3 = Triple3(1,2,3)
e-->repr(t3)-->Triple3(a=1,b=2,c=3)
c-->t3 = Triple3(c=3,b=2,a=1)
e-->repr(t3)-->Triple3(a=1,b=2,c=3)
c-->tok= Triple_OK(c__2=3,b__1=2,a17=1)
e-->repr(tok)-->Triple_OK(a17=1,b__1=2,c__2=3)
e-->t1.a-->1
e-->t1.b-->2
e-->t1.c-->3

can someone help me to fix my repr method? I asked all of my TAs but they cannot figure out why I got the error in line 20.

Stephan
  • 365
  • 1
  • 5
  • 18
  • You have no ``__repr__`` method in your class! What you have is called ``_repr_``, which has no particular meaning. – jasonharper Nov 09 '16 at 03:47
  • I think there is something wrong in my __repr__ function or in my __init__ function. I am just not where did I do wrong. – Stephan Nov 09 '16 at 03:55
  • Possible duplicate of [python 3: class "template" (function that returns a parameterized class)](http://stackoverflow.com/questions/4858298/python-3-class-template-function-that-returns-a-parameterized-class) – ivan_pozdeev Nov 09 '16 at 04:02
  • Class name can be set without parsing, too: http://stackoverflow.com/questions/5352781/how-to-set-class-names-dynamically – ivan_pozdeev Nov 09 '16 at 04:03
  • @ ivan_pozdeev thanks, but I don't think that solves my problem. – Stephan Nov 09 '16 at 04:22
  • does anyone know? – Stephan Nov 10 '16 at 01:05

0 Answers0