2016-03-03 64 views
-1

我無法調試我的代碼。錯誤幫助:'元組'對象沒有屬性'getx'

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] 

當我嘗試運行我的測試中,大部分都帶回來一個錯誤,說「元組」對象有沒有屬性「的getX」。我不知道爲什麼這個錯誤不斷出現。這是我的測試代碼。

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' 
+0

什麼是追溯? – zondo

+0

對不起,你是什麼意思回溯。 – Ecoturtle

+0

那麼,你會得到一個錯誤,但可以推測,它不僅僅說「元組」對象沒有屬性「getx」。回溯是一串線,包含有關該線所在的文件,線是什麼的信息等等。它以「Traceback(最近一次調用最後一次):」開頭,並以上面給出的錯誤結束。 – zondo

回答

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已經是一個點列表。你確定你想讓他們成爲點嗎?

+0

謝謝!改變軌跡似乎得到了pointset.NN測試。非常感謝。另外,getpoints()是有意的。 – Ecoturtle

相關問題