1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Rep Power

    Probably a very noob question

    Hey guys, so I just started programming in python. I've done a few tutorials and taken some online lessons, but now it's time to learn by doing. So I am stuck right now, I need to import a class from a separate file (which i did), have it parse an element in a list, and store that parsed file as a dictionary.

    I need your help with this, as I don't really know what to do, I thought that there would be a return but there is not, so I don't know what I am supposed to do. I assume that it is possible and I just don't know how.

    The code I have so far is this (I honestly don't even know if I am formatting this code correctly, any guidance would be great, and please don't just say go watch tutorials, if you are going to help please give me actual advice to use in the future):

    class GCCCOMPARE():
        def __init__(self, list):
            self.GCCLIST = list 
            self.GCCN = []
            self.GCCPLUS = []
        def compare(self):
            while n<size(self.GCCLIST):
    And the code in the other file I was given is this:
    class GCCFORMAT():
        def __init__(self, file):
            #print"New GCC Load"
            self.pList = [];
            self.dList = [];
            self.file = file;
        def parse(self):
            with open(self.file, "r") as icdhelper:
                for line in icdhelper:
                    sArr = line.split()
                    self.pList.append(PARAM(sArr[0], sArr[4], sArr[2], sArr[3], sArr[6], sArr[7]))
    I doubt this will be difficult for anyone experienced, but even so thanks a lot guys!
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power

    Is difficult

    It's difficult because we don't know what output or changes to the state of the program (that is, what should the variables contain) to expect after creating a GCCFORMAT object. Any file with lines that can split into at least 8 fields should serve as input. You mentioned "return" but your program has no return statements. The compare and parse methods therefor return None .

    The GCCFORMAT class looks as if it might be reasonable if python had a definition for the PARAM callable. I don't see it here. Let's move on to the GCCCOMPARE class code.
    class GCCCOMPARE():
        def __init__(self, list):   # *
            self.GCCLIST = list     # *
            self.compare()          # A
            self.GCCN = []          #  B
            self.GCCPLUS = []       #   C
        def compare(self):
            n=0                     # D
            while n<size(self.GCCLIST): # E
                self.GCCN=self.GCCLIST[n] # F 
                self.GCCPLUS=self.GCCLIST[n+1] # G
                GCCFORMAT.__init__(GCCN)       # H
    ABC Line A calls compare which modifies the object's GCCN and GCCPLUS attributes. If compare returns lines B and C immediately change these attributes to empty lists. Side effects are the only reason to write code like that.

    D On line D you set n to zero. The value of n does not change elsewhere in the compare method.

    E Line E compares two values that don't change. The test is always True or always False. Thus GCCCOMPARE.compare(a_GCCCOMPARE_object) usually won't terminate. Perhaps that's what you meant by "I thought there would be a return but there is not"??

    FG Lines F and G are silly in this context because as already mentioned you overwrite any values they held at the end of GCCCOMPARE.__init__ .

    H. H. H. In line H you've used an unitialized variable, GCCN. Maybe you meant self.GCCN . Python won't guess that for you. To construct a GCCFORMAT object write

    * On the lines marked * you've used list which is good in a way since it sort of means the input should be a list. The bad news: it's terrible in that masking builtin definitions will confuse the next person who reads your code.

    Cannot comment intelligently about your indentation because we don't know what the input and expected output is. Show a test case. "Indentation" can also be thought of as "blocks of code" or "statement groups".
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo