Skip to content

Commit b3055a0

Browse files
authored
Merge pull request #521 from realpython/python-sequences
python-sequences
2 parents 27b2cce + 4368ea6 commit b3055a0

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

python-sequences/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Python Sequences: A Comprehensive Guide
2+
3+
This folder contains source code for the Real Python tutorial [Python Sequences: A Comprehensive Guide](https://realpython.com/python-sequences-comprehensive-guide/).
4+
5+
For more code, intermediate steps, and additional explanation, check out the associated tutorial.

python-sequences/draw_shape.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import turtle
2+
3+
from shape import ShapePoints
4+
5+
triangle = ShapePoints([(100, 100), (-200, 100), (-200, -200)])
6+
7+
# Draw the shape using turtle graphics
8+
turtle.penup()
9+
# Move to the first point
10+
turtle.setposition(triangle[0])
11+
turtle.pendown()
12+
# Draw lines to the other points
13+
for point in triangle[1:]:
14+
turtle.setposition(point)
15+
16+
turtle.done()

python-sequences/shape.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class ShapePoints:
2+
"""
3+
A ShapePoints object represents a collection of points
4+
5+
Attributes:
6+
- points: sequence of points, where each point is a
7+
tuple (x, y)
8+
"""
9+
10+
def __init__(self, points):
11+
self.points = list(points)
12+
if points and self.points[0] != self.points[-1]:
13+
self.points.append(self.points[0])
14+
15+
def __repr__(self):
16+
return f"ShapePoints({self.points})"
17+
18+
def __getitem__(self, index):
19+
return self.points[index]
20+
21+
def __len__(self):
22+
if self.points:
23+
return len(self.points) - 1
24+
return 0
25+
26+
def __iter__(self):
27+
return iter(self.points)

python-sequences/shape_abc.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from collections.abc import MutableSequence
2+
3+
4+
class ShapePoints(MutableSequence):
5+
MIN_POINTS = 3
6+
7+
def __init__(self, points):
8+
self.points = list(points)
9+
if len(self.points) < self.MIN_POINTS:
10+
raise ValueError(
11+
f"Shape must have at least {self.MIN_POINTS} points"
12+
)
13+
if points and self.points[0] != self.points[-1]:
14+
self.points.append(self.points[0])
15+
16+
def __repr__(self):
17+
return f"ShapePoints({self.points})"
18+
19+
def __getitem__(self, index):
20+
return self.points[index]
21+
22+
def __len__(self):
23+
if self.points:
24+
return len(self.points) - 1
25+
return 0
26+
27+
def __iter__(self):
28+
return iter(self.points)
29+
30+
def __contains__(self, item):
31+
print("Checking if item is in ShapePoints")
32+
return item in self.points
33+
34+
def __delitem__(self, index):
35+
if len(self) < self.MIN_POINTS + 1:
36+
raise ValueError(
37+
f"Shape must have at least {self.MIN_POINTS} points"
38+
)
39+
if index in (0, len(self.points) - 1, -1):
40+
del self.points[0]
41+
self.points[-1] = self.points[0]
42+
else:
43+
del self.points[index]
44+
45+
def __setitem__(self, index, value):
46+
if index in (0, len(self.points) - 1, -1):
47+
self.points[0] = value
48+
self.points[-1] = value
49+
else:
50+
self.points[index] = value
51+
52+
def insert(self, index, value):
53+
if index in (0, len(self.points) - 1, -1):
54+
self.points.insert(0, value)
55+
self.points[-1] = value
56+
else:
57+
self.points.insert(index, value)
58+
59+
def count(self, value):
60+
return self.points[:-1].count(value)
61+
62+
def append(self, value):
63+
self.points.append(self.points[0])
64+
self.points[-2] = value

0 commit comments

Comments
 (0)