0

I have a text file with numbers stored in the following format:

93 407 77 400 94 365 109 372 
135 312 180 328
100 120 140 160

I want to place these values into two list. One for polygons and one for rectangles.

If a line has 8 numbers in it, then gets stored to the polygon list. If a line has 4 numbers in it, then it get stored in the rectangle list. Like so

polygon=[ [93, 407, 77, 400, 94, 365, 109, 372] ]

rectangle=[ [135, 312, 180, 328,], [100, 120, 140, 160] ]

I would then uses these values to draw either a rectangle or a polygon on a canvas.

Here is what my code is so far:

class getXYCoords:

    def __init__(self, textFile):

        self.textFile = textFile
        polygon = []
        rectangle = []

        with open(self.textFile) as f:

            for line in f:
                line = line.split() #strip EOL
                if line: #lines (ie skip them)

                    # Stores values into polygon list
                    if len(line) == 8:
                        line = [int(i) for i in line]
                        polygon.append(line)

                    # Stores values into rectangle list
                    elif len(line) == 4:
                        line = [int(i) for i in line]
                        rectangle.append(line)

        return polygon, rectangle

# Example program

if __name__ == '__main__':

    #polygon = []
    #rectangle = []

    # Get XY coordinates
    polygon, rectangle = getXYCoords('HAMH HUTC XY.txt')

    print(polygon,'\n')
    print(rectangle)

When I run the program I get this error message:

line 46 in module
polygon, rectangle = getXYCoords('HAMH HUTC XY.txt')
TypeError: init() should return None, not 'tuple'

goofenhour
  • 159
  • 1
  • 2
  • 13
  • Error message is pretty clear... what don't you understand about this? Is there a reason you're using a class instead of a function? – David Zemens Nov 11 '15 at 16:09
  • I wanted to place this code in a separate python file, and thought I needed to do it this way. – goofenhour Nov 11 '15 at 16:35

2 Answers2

2

If you want to represent your coordinate sets in a class, set the polygon and rectangle lists as instance members and refer to them through self.

Note that the __init__ constructor shouldn't return anything and if you want to access your polygon and rectangle lists you need to create an object of getXYCoords (here called coords) and access its members with the "dot" syntax: coords.rectangle, etc:

class getXYCoords:

    def __init__(self, textFile):

        self.textFile = textFile
        self.polygon = []
        self.rectangle = []

        with open(self.textFile) as f:

            for line in f:
                line = line.split() #strip EOL
                if line: #lines (ie skip them)

                    # Stores values into polygon list
                    if len(line) == 8:
                        line = [int(i) for i in line]
                        self.polygon.append(line)

                    # Stores values into rectangle list
                    elif len(line) == 4:
                        line = [int(i) for i in line]
                        self.rectangle.append(line)


# Example program

if __name__ == '__main__':

    # Get XY coordinates
    coords = getXYCoords('test.txt')

    print(coords.polygon,'\n')
    print(coords.rectangle)
xnx
  • 24,509
  • 11
  • 70
  • 109
1

In Python, __init__() doesn't return anything except for None. It wouldn't make sense for the constructor of a class to return anything else. In your code, it seems like you need Polygon and Rectangle as objects to be referenced later. If so, then you'd simply need to add self.rectangle and self.polygon as attributes to your class, and get rid of the return line:

def __init__(self, textFile):

    self.textFile = textFile
    self.polygon = []
    self.rectangle = []

    with open(self.textFile) as f:

        for line in f:
            line = line.split() #strip EOL
            if line: #lines (ie skip them)

                # Stores values into polygon list
                if len(line) == 8:
                    line = [int(i) for i in line]
                    self.polygon.append(line)

                # Stores values into rectangle list
                elif len(line) == 4:
                    line = [int(i) for i in line]
                    self.rectangle.append(line)

If you must return something when a class is called, you can use __new__() instead of __init__(). See this answer for more details.

Community
  • 1
  • 1
AdmiralWen
  • 701
  • 6
  • 16