GASP/GASP_Code
6
1
Fork
You've already forked GASP_Code
4

NameError for Line attribute 'rise' #3

Closed
opened 2020年07月22日 01:25:22 +02:00 by mdnabiltech · 2 comments

Error:

Python 3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gasp import *
>>> begin_graphics()
>>> Line((200,200),(150,350))
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/home/mdnabiltech/Projects/GASP/GASP_Tkinter/gasp/gasp.py", line 162, in __repr__
 f"<Line object from ({self.pos[0]}, {self.pos[1]})>"
NameError: name 'rise' is not defined

Gasp.py File:

class Line:
 """Draw a line segment between p1 and p2."""
 def __init__(self, p1, p2, color=color.BLACK, thickness=1):
 self.pos = p1
 self.rise = p2[0] - p1[0]
 self.run = p2[1] - p1[1]
 self.coord_list = [
 p1[0],
 _canvas_ys - p1[1],
 p2[0],
 _canvas_ys - p2[1]
 ]
 self.id = _canvas.create_line(*self.coord_list, fill=color, width=thickness)
 _canvas.update()
 def __repr__(self):
 return (
 f"<Line object from ({self.pos[0]}, {self.pos[1]})>"
 f" to ({self.pos[0] + rise}, {self.pos[1] - run})>"
 )
**Error:** ``` Python 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from gasp import * >>> begin_graphics() >>> Line((200,200),(150,350)) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/mdnabiltech/Projects/GASP/GASP_Tkinter/gasp/gasp.py", line 162, in __repr__ f"<Line object from ({self.pos[0]}, {self.pos[1]})>" NameError: name 'rise' is not defined ``` **Gasp.py File:** ``` class Line: """Draw a line segment between p1 and p2.""" def __init__(self, p1, p2, color=color.BLACK, thickness=1): self.pos = p1 self.rise = p2[0] - p1[0] self.run = p2[1] - p1[1] self.coord_list = [ p1[0], _canvas_ys - p1[1], p2[0], _canvas_ys - p2[1] ] self.id = _canvas.create_line(*self.coord_list, fill=color, width=thickness) _canvas.update() def __repr__(self): return ( f"<Line object from ({self.pos[0]}, {self.pos[1]})>" f" to ({self.pos[0] + rise}, {self.pos[1] - run})>" ) ```

Turns out the problem was in both 'rise' and 'run'. As I got this too:

Python 3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gasp import *
>>> begin_graphics()
>>> Line((100,100),(200,200))
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/home/mdnabiltech/Projects/GASP/GASP_Tkinter/gasp/gasp.py", line 162, in __repr__
 f"<Line object from ({self.pos[0]}, {self.pos[1]})>"
NameError: name 'run' is not defined

I looked in the gasp.py file and I fixed it with a quick fix of just adding 'self.' to the start of the attributes.

The correct code for line 163 is:
f" to ({self.pos[0] + self.rise}, {self.pos[1] - self.run})>"

as opposed to:
f" to ({self.pos[0] + rise}, {self.pos[1] - run})>"

Turns out the problem was in both 'rise' and 'run'. As I got this too: ``` Python 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from gasp import * >>> begin_graphics() >>> Line((100,100),(200,200)) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/mdnabiltech/Projects/GASP/GASP_Tkinter/gasp/gasp.py", line 162, in __repr__ f"<Line object from ({self.pos[0]}, {self.pos[1]})>" NameError: name 'run' is not defined ``` I looked in the gasp.py file and I fixed it with a quick fix of just adding 'self.' to the start of the attributes. The correct code for line 163 is: `f" to ({self.pos[0] + self.rise}, {self.pos[1] - self.run})>"` as opposed to: `f" to ({self.pos[0] + rise}, {self.pos[1] - run})>"`

Nice work, Nabil! First I added a test for this to test_api.py, then I applied your fix. Actually, we need self.pos[1] - self.run. I'm only adding high level, "functional tests". We should also add lower level, programmer oriented "unit tests" to test the math that is being done to transform the coordinate system.

Anyway, for now the fix has been commited.

Nice work, Nabil! First I added a test for this to test_api.py, then I applied your fix. Actually, we need self.pos[1] - self.run. I'm only adding high level, "functional tests". We should also add lower level, programmer oriented "unit tests" to test the math that is being done to transform the coordinate system. Anyway, for now the fix has been commited.
Sign in to join this conversation.
No Branch/Tag specified
main
No results found.
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
GASP/GASP_Code#3
Reference in a new issue
GASP/GASP_Code
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?