8000 uploading first draft of sample code for python-circular-import · realpython/materials@e5fb093 · GitHub
[go: up one dir, main page]

Skip to content

Commit e5fb093

Browse files
committed
uploading first draft of sample code for python-circular-import
1 parent 66d67b3 commit e5fb093

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+377
-0
lines changed

python-circular-import/entity-relations/people/__init__.py

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from people import club, person
2+
3+
4+
def main():
5+
p1 = person.Person("John", 30)
6+
p2 = person.Person("Jane", 29)
7+
c = club.Club("Tennis Club", [p1, p2])
8+
print(c)
9+
10+
11+
if __name__ == "__main__":
12+
main()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# from __future__ import annotations
2+
3+
# from typing import TYPE_CHECKING
4+
5+
# if TYPE_CHECKING:
6+
# from people.person import Person
7+
8+
from people.person import Person
9+
10+
11+
class Club:
12+
def __init__(self, name, members: list[Person] = []):
13+
self.name = name
14+
self.members = members
15+
16+
def __str__(self):
17+
return f"{self.name} ({len(self.members)} members)"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# from __future__ import annotations
2+
3+
# from typing import TYPE_CHECKING
4+
5+
# if TYPE_CHECKING:
6+
# from people.club import Club
7+
8+
from people.club import Club
9+
10+
11+
class Person:
12+
def __init__(self, name, age, clubs: list[Club] = []):
13+
self.name = name
14+
self.age = age
15+
self.clubs = clubs
16+
17+
def __str__(self):
18+
return f"{self.name} ({self.age})"

python-circular-import/entity-relations/people_runtime/__init__.py

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from people_runtime import club, person
2+
3+
4+
def main():
5+
p1 = person.Person("John", 30)
6+
p2 = person.Person("Jane", 29)
7+
c = club.Club("Tennis Club")
8+
c.assign_member(p1)
9+
c.assign_member(p2)
10+
print(c)
11+
12+
13+
if __name__ == "__main__":
14+
main()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# from people_runtime.person import Person
2+
3+
4+
class Club:
5+
def __init__(self, name):
6+
self.name = name
7+
self.members = set()
8+
9+
def __str__(self):
10+
return f"{self.name} ({len(self.members)} members)"
11+
12+
def assign_member(self, person):
13+
from people_runtime.person import Person
14+
15+
if isinstance(person, Person):
16+
self.members.add(person)
17+
18+
if self not in person.clubs:
19+
person.join_club(self)
20+
else:
21+
raise ValueError("Can only assign instances of Person")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# from people_runtime.club import Club
2+
3+
4+
class Person:
5+
def __init__(self, name, age):
6+
self.name = name
7+
self.age = age
8+
self.clubs = set()
9+
10+
def __str__(self):
11+
return f"{self.name} ({self.age})"
12+
13+
def join_club(self, club):
14+
from people_runtime.club import Club
15+
16+
if isinstance(club, Club):
17+
self.clubs.add(club)
18+
if self not in club.members:
19+
club.assign_member(self)
20+
else:
21+
raise ValueError("Can only join instances of Club")

python-circular-import/entity-relations/pyproject.toml

Whitespace-only changes.

python-circular-import/layered-architecture/pyproject.toml

Whitespace-only changes.

0 commit comments

Comments
 (0)
0