错误帮助:“元组”对象没有属性“ getx” [英] Error Help: 'tuple' object has no attribute 'getx'

查看:85
本文介绍了错误帮助:“元组”对象没有属性“ getx”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having trouble debugging my code.

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]

当我尝试运行测试时,大多数返回一个错误,提示 tuple对象具有没有属性 getx。我不确定为什么会继续出现此错误。这是我的测试代码。

When I try and run my tests, most of them come back with an error saying 'tuple' object has no attribute 'getx'. I'm not sure why this error keeps coming up. Here is my test code.

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()

回溯必须缩进以便可读。

The traceback needed to indented to be readable.

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'


推荐答案

pointset.NN返回一个元组,因此nn是元组,在此处设置:

pointset.NN returns a tuple, so nn is tuple, set here:

 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)

pointpoint.getpoints返回一个元组列表:

And pointSet.getpoints returns a list of tuples:

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

更改为

return self.list

尽管这似乎是故意的,因为pointSet中的self.list已经是一个点列表。您确定要让它们成为积分吗?

Though this seems intentional, since self.list in pointSet is already a list of points. Are you sure you want them to be points?

这篇关于错误帮助:“元组”对象没有属性“ getx”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆