jbedo465's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
24 (0.02 per day)
Joined:
18-February 10
Profile Views:
1,265
Last Active:
User is offline Oct 03 2011 02:32 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   jbedo465 has not set their status

Posts I've Made

  1. In Topic: How do I initialize a class object with a field?

    Posted 2 Oct 2011

    Understood,

    Thanks again,
    JB
  2. In Topic: How do I initialize a class object with a field?

    Posted 2 Oct 2011

    Simown,

    So my defs should look like this?

    #container.py
    class __TupleContainer__(object):
        def __init__(self, container = []):
            self.container = container
        def appendToken(self, tokenLexemeLinenumberTriplet): # METHOD in STUDENT code.
            if (type(tokenLexemeLinenumberTriplet) != tuple
                    or len(tokenLexemeLinenumberTriplet) != 3
                    or type(tokenLexemeLinenumberTriplet[0]) != str
                    or type(tokenLexemeLinenumberTriplet[1]) != str
                    or type(tokenLexemeLinenumberTriplet[2]) != int):
                raise ValueError,(                                  \
                    "argument to appendToken is not a (token,lexeme,lineNumber) " \
                        "3-tuple: " + str(tokenLexemeLinenumberTriplet))
            # Python nested functions cannot modify the binding of a parameter or
            # variable in an enclosing function, but if that binding refers to a
            # mutable data structure such as a list, they can mutate the mutable
            # data structure.
            self.container.append(tokenLexemeLinenumberTriplet)
    
        def getTokens(self):    # METHOD IN STUDENT CLASS
            return tuple(self.container) # constructs and returns an immutable copy
    
    def makeScannedObjectContainer():
        container2 = __TupleContainer__()
        return container2
    
    


    It works, but is this what it should look like conventionally? I'm very new to python and would like to keep with conventions. Simown, thank you for all the help, I appreciate it.

    Thank you,
    JB
  3. In Topic: How do I initialize a class object with a field?

    Posted 2 Oct 2011

    Simown,

    Thanks, that seems to have cleared that up, but now I'm getting this error:

    Traceback (most recent call last):
    File "test_scanner.py", line 45, in <module>
    container.appendToken(triplet)
    TypeError: appendToken() takes exactly 1 argument (2 given)

    I don't see how appendToken() is getting two arguments, I'm only passing it one. Here are my two files that might be at fault:

    #test_scanner.py
    from objects_scanner1.scanner import scan
    from objects_scanner1.container import makeScannedObjectContainer
    from sys import argv, stdin, stderr, exit
    
    if __name__=='__main__':
        filehandle = None
        # Do this only if this module is executed as the main module.
        if (len(argv) == 2):
            filehandle = open(argv[1], "rU") # portable linefeed handling
        else:
            stderr.write(
                "Invalid usage, test driver takes one mandatory file name." \
                + '\n')
            exit(1)
        container = makeScannedObjectContainer()
        linenum = 0
        done = False
        while (not done):
            try:
                tokenlist = scan(filehandle, linenum)
            except ValueError, message:
                stderr.write("ValueError: " + str(message) + "\n")
                exit(1)
            # print "DEBUG type of tokenlist", type(tokenlist), tokenlist
            for triplet in tokenlist:
                container.appendToken(triplet)
                linenum = triplet[2]
                if (triplet[0] == 'EOF'):
                    done = True
        filehandle.close()
        tokenList = container.getTokens()
        for tokenTriplet in tokenList:
            print tokenTriplet
    
    


    And here is the second:

    #container.py
    class __TupleContainer__(object):
        def __init__(self, container = []):
            self.container = container
        def appendToken(tokenLexemeLinenumberTriplet): # METHOD in STUDENT code.
            if (type(tokenLexemeLinenumberTriplet) != list
                    or len(tokenLexemeLinenumberTriplet) != 3
                    or type(tokenLexemeLinenumberTriplet[0]) != str
                    or type(tokenLexemeLinenumberTriplet[1]) != str
                    or type(tokenLexemeLinenumberTriplet[2]) != int):
                raise ValueError,(                                  \
                    "argument to appendToken is not a (token,lexeme,lineNumber) " \
                        "3-tuple: " + str(tokenLexemeLinenumberTriplet))
            # Python nested functions cannot modify the binding of a parameter or
            # variable in an enclosing function, but if that binding refers to a
            # mutable data structure such as a list, they can mutate the mutable
            # data structure.
            container.append(tokenLexemeLinenumberTriplet)
    
        def getTokens():    # METHOD IN STUDENT CLASS
            return tuple(container) # constructs and returns an immutable copy
    
    def makeScannedObjectContainer():
        container2 = __TupleContainer__()
        return container2
    
    


    Am I missing something here?
    There are other files but they aren't at fault.
    The line numbers don't match up because I've cut out the comments. Also, makeScannedObjectContainer() should be a "factory function" to pump out __TupleContainer__ objects, that's why I have it outside the __TupleContainer__ class.


    Thanks,
    JB

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Click here to e-mail me

Friends

Comments

jbedo465 has no profile comments yet. Why not say hello?