错误帮助:'元组'对象没有属性' getx'

时间:2016-03-03 02:40:52

标签: python python-3.x computer-science

我在调试代码时遇到问题。

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def compare(self, other):
        x1 = self.x
        y1 = self.y
        x2 = other.x
        y2 = other.y
        n = 512
        if (x1 == x2) and (y1 == y2):
            return 0
        found = False
        while found == False:
            if ((x1 // n) != (x2 // n)) or ((y1 // n) != (y2 // n)):
                found = True
                c1 = ((x1 // n), (y1 // n))
                c2 = ((x2 // n), (y2 // n))
            else:
                if x1 >= n and x2 >= n:
                    x1 = x1 - n
                    x2 = x2 - n
                if y1 >= n  and y2 >= n:
                    y1 = y1 - n
                    y2 = y2 - n
                n = n / 2
        if c1 == (0, 1):
            e1 = 1
        if c1 == (1, 1):
            e1 = 2
        if c1 == (0, 0):
            e1 = 3
        if c1 == (1, 0):
            e1 = 4
        if c2 == (0, 1):
            e2 = 1
        if c2 == (1, 1):
            e2 = 2
        if c2 == (0, 0):
            e2 = 3
        if c2 == (1, 0):
            e2 = 4
        if e1 > e2:
            return 1
        if e2 > e1:
            return -1

    def dist(self, other):
        x = abs(self.x - other.x)
        y = abs(self.y - other.y)
        if x >= y:
            return x
        else:
            return y

    def getx(self):
        return self.x

    def gety(self):
        return self.y

    def __lt__(self, other):
        return self.compare(other) == -1

    def __eq__(self, other):
        return self.x == other.getx and self.y == other.gety

    def __le__(self, other):
        return self < other or self == other

from Point import Point

class PointSet:

    def __init__(self):
        self.list = []

    def add(self, point):
        count = 0
        found = False
        if self.list == []:
            self.list.append(point)
            found = True
        while not found:
            for x,y in self.getpoints():
                if point.compare(Point(x, y)) == -1:
                    self.list.insert(count, point)
                    found = True
                    return count
                else:
                    if count == len(self.list)-1:
                        self.list.append(point)
                        found = True
                        return count
                    count = count + 1

    def NN(self, query):
        count = 10000
        trace = ()
        if self.list == []:
            return None
        for x,y in self.getpoints():
            if query.dist(Point(x, y)) < count:
                count = query.dist(Point(x, y))
                trace = (x, y)
        return trace

    def ANN(self, query):
        count = 0
        if self.list == []:
            return None
        for x,y in self.getpoints():
            if query.compare(Point(x, y)) == -1:
                return self.list[count], self.list[count+1], self.list[count-1], self.list[count-2]
            else:
                count = count + 1

    def getpoints(self):
        return [(i.x, i.y) for i in self.list]

当我尝试运行我的测试时,他们中的大多数人都会回来时发出一个错误,说“&#39; tuple&#39;对象没有属性&#39; getx&#39;。我不确定为什么这个错误会不断出现。这是我的测试代码。

import unittest
from Point import Point
from PointSet import PointSet

class TestPointSet(unittest.TestCase):
    def setUp(self):
        coords = [ 
            (300, 800),
            (12,720),
            (75,660),
            (150,550),
            (605 , 810),
            (900, 640),
            (100, 390),
            (300, 400),
            (80, 100),
            (260, 30),
            (400, 25),
            (1000, 450),
            (940, 400),
            (990, 410),
            (800, 280)
            ]

        self.pt_list = [Point(x,y) for x,y in coords]

    def test_new_point_set(self):
        pts = PointSet()
        pts.add(Point(0,0))
        pts.add(Point(0,1023))

    def test_pointset_is_ordered(self):
        pointset = PointSet()
        for i in range(10):
            for j in range(10):
                pointset.add(Point(i* 8+1,j * 16 - 1))

        for i in range(100-1):
            assert(pointset.getpoints()[i] < pointset.getpoints()[i+1])

    def test_pointset_is_ordered2(self):
        pts = PointSet()
        pts.add(self.pt_list[3])
        pts.add(self.pt_list[10])
        pts.add(self.pt_list[6])
        pts.add(self.pt_list[11])
        pts.add(self.pt_list[1])
        pts.add(self.pt_list[4])
        pts.add(self.pt_list[7])
        pts.add(self.pt_list[14])
        pts.add(self.pt_list[8])
        pts.add(self.pt_list[5])
        pts.add(self.pt_list[13])
        pts.add(self.pt_list[9])
        pts.add(self.pt_list[12])
        pts.add(self.pt_list[0])
        pts.add(self.pt_list[2])

        for i,p in enumerate(pts.getpoints()):
            self.assertEqual(p, self.pt_list[i])

    def test_NN(self):
        pointset = PointSet()
        for i in range(15):
            for j in range(15):
                pointset.add(Point(i * 64, j * 64))

        for i in range(15):
            for j in range(15):
                nn = pointset.NN(Point(i * 64 - 31, j * 64 + 31))
                assert(nn.getx() == i * 64)
                assert(nn.gety() == j * 64)

    def test_ANN(self):
        ps = PointSet()
        for p in self.pt_list:
            ps.add(p)
        self.assertEqual(ps.ANN(Point(129, 390)), self.pt_list[6])
        self.assertEqual(ps.ANN(Point(1000, 512)), self.pt_list[5])

if __name__ == '__main__':
    unittest.main()

需要缩回以便可读的回溯。

Traceback:

     Traceback (most recent call last):
File "C:\Users\\Desktop\\skeleton (5)\TestPointSet.py", line 60, in test_pointset_is_ordered2
  self.assertEqual(p, self.pt_list[i])
File "C:\Users\\AppData\Local\Programs\Python\Python35-32\lib\unittest\case.py", line 820, in assertEqual
assertion_func(first, second, msg=msg)
File "C:\Users\\AppData\Local\Programs\Python\Python35-32\lib\unittest\case.py", line 810, in _baseAssertEqual
if not first == second:
File "C:\Users\\Desktop\\skeleton (5)\Point.py", line 67, in __eq__
return self.x == other.getx and self.y == other.gety
AttributeError: 'tuple' object has no attribute 'getx'

1 个答案:

答案 0 :(得分:0)

pointset.NN返回一个元组,所以nn是元组,设置在这里:

 for x,y in self.getpoints():
        if query.dist(Point(x, y)) < count:
            count = query.dist(Point(x, y))
            trace = (x, y)  # TRACE IS A TUPLE
 return trace

更改为:

trace = Point(x, y)

pointSet.getpoints返回元组列表:

 return [(i.x, i.y) for i in self.list]

更改为

return self.list

虽然这似乎是故意的,因为pointSet中的self.list已经是一个点列表。你确定要他们成为积分吗?