-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Materials for python-circular-import
Article
#593
Open
iansedano
wants to merge
4
commits into
master
Choose a base branch
from
python-circular-import
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Materials for `python-circular-import` |
Empty file.
12 changes: 12 additions & 0 deletions
12
python-circular-import/entity-relations/people/__main__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from people import club, person | ||
|
||
|
||
def main(): | ||
p1 = person.Person("John", 30) | ||
p2 = person.Person("Jane", 29) | ||
c = club.Club("Tennis Club", [p1, p2]) | ||
print(c) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# from __future__ import annotations | ||
|
||
# from typing import TYPE_CHECKING | ||
|
||
# if TYPE_CHECKING: | ||
# from people.person import Person | ||
|
||
from people.person import Person | ||
|
||
|
||
class Club: | ||
def __init__(self, name, members: list[Person] = []): | ||
self.name = name | ||
self.members = members | ||
|
||
def __str__(self): | ||
return f"{self.name} ({len(self.members)} members)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# from __future__ import annotations | ||
|
||
# from typing import TYPE_CHECKING | ||
|
||
# if TYPE_CHECKING: | ||
# from people.club import Club | ||
|
||
from people.club import Club | ||
|
||
|
||
class Person: | ||
def __init__(self, name, age, clubs: list[Club] = []): | ||
self.name = name | ||
self.age = age | ||
self.clubs = clubs | ||
|
||
def __str__(self): | ||
return f"{self.name} ({self.age})" |
Empty file.
14 changes: 14 additions & 0 deletions
14
python-circular-import/entity-relations/people_runtime/__main__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from people_runtime import club, person | ||
|
||
|
||
def main(): | ||
p1 = person.Person("John", 30) | ||
p2 = person.Person("Jane", 29) | ||
c = club.Club("Tennis Club") | ||
c.assign_member(p1) | ||
c.assign_member(p2) | ||
print(c) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
21 changes: 21 additions & 0 deletions
21
python-circular-import/entity-relations/people_runtime/club.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# from people_runtime.person import Person | ||
|
||
|
||
class Club: | ||
def __init__(self, name): | ||
self.name = name | ||
self.members = set() | ||
|
||
def __str__(self): | ||
return f"{self.name} ({len(self.members)} members)" | ||
|
||
def assign_member(self, person): | ||
from people_runtime.person import Person | ||
|
||
if isinstance(person, Person): | ||
self.members.add(person) | ||
|
||
if self not in person.clubs: | ||
person.join_club(self) | ||
else: | ||
raise ValueError("Can only assign instances of Person") |
21 changes: 21 additions & 0 deletions
21
python-circular-import/entity-relations/people_runtime/person.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# from people_runtime.club import Club | ||
|
||
|
||
class Person: | ||
def __init__(self, name, age): | ||
self.name = name | ||
self.age = age | ||
self.clubs = set() | ||
|
||
def __str__(self): | ||
return f"{self.name} ({self.age})" | ||
|
||
def join_club(self, club): | ||
from people_runtime.club import Club | ||
|
||
if isinstance(club, Club): | ||
self.clubs.add(club) | ||
if self not in club.members: | ||
club.assign_member(self) | ||
else: | ||
raise ValueError("Can only join instances of Club") |
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from shop.controllers.shop_controller import ShopController | ||
|
||
if __name__ == "__main__": | ||
controller = ShopController() | ||
controller.handle_request() |
Empty file.
20 changes: 20 additions & 0 deletions
20
python-circular-import/layered-architecture/shop/controllers/shop_controller.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from shop.services.shop_service import ShopService | ||
|
||
|
||
class ShopController: | ||
_instance = None | ||
|
||
def __new__(cls, *args, **kwargs): | ||
if not cls._instance: | ||
cls._instance = super(ShopController, cls).__new__( | ||
cls, *args, **kwargs | ||
) | ||
return cls._instance | ||
|
||
def __init__(self): | ||
if not hasattr(self, "service"): | ||
self.service = ShopService() | ||
|
||
def handle_request(self): | ||
print("Handling request") | ||
self.service.perform_service_task() |
Empty file.
20 changes: 20 additions & 0 deletions
20
python-circular-import/layered-architecture/shop/services/shop_service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from shop.controllers.shop_controller import ShopController | ||
|
||
|
||
class ShopService: | ||
_instance = None | ||
|
||
def __new__(cls, *args, **kwargs): | ||
if not cls._instance: | ||
cls._instance = super(ShopService, cls).__new__( | ||
cls, *args, **kwargs | ||
) | ||
return cls._instance | ||
|
||
def __init__(self): | ||
if not hasattr(self, "controller"): | ||
self.controller = ShopController() | ||
|
||
def perform_service_task(self): | ||
print("Service task performed") | ||
self.controller.handle_request() |
Empty file.
7 changes: 7 additions & 0 deletions
7
python-circular-import/layered-architecture/shop_di/__main__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from shop_di.controllers.shop_controller import ShopController | ||
from shop_di.services.shop_service import ShopService | ||
|
||
if __name__ == "__main__": | ||
service = ShopService() | ||
controller = ShopController(service) | ||
controller.handle_request() |
Empty file.
15 changes: 15 additions & 0 deletions
15
python-circular-import/layered-architecture/shop_di/controllers/shop_controller.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class ShopController: | ||
_instance = None | ||
|
||
def __new__(cls, *args, **kwargs): | ||
if not cls._instance: | ||
cls._instance = object.__new__(cls) | ||
return cls._instance | ||
|
||
def __init__(self, service): | ||
if not hasattr(self, "service"): | ||
self.service = service | ||
|
||
def handle_request(self): | ||
print("Handling request") | ||
self.service.perform_service_task() |
Empty file.
13 changes: 13 additions & 0 deletions
13
python-circular-import/layered-architecture/shop_di/services/shop_service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class ShopService: | ||
_instance = None | ||
|
||
def __new__(cls, *args, **kwargs): | ||
if not cls._instance: | ||
cls._instance = object.__new__(cls) | ||
return cls._instance | ||
|
||
def __init__(self): | ||
pass | ||
|
||
def perform_service_task(self): | ||
print("Service task performed") |
Empty file.
5 changes: 5 additions & 0 deletions
5
python-circular-import/layered-architecture/shop_import_recursion/__main__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from shop_import_recursion.controllers.shop_controller import ShopController | ||
|
||
if __name__ == "__main__": | ||
controller = ShopController() | ||
controller.handle_request() |
Empty file.
25 changes: 25 additions & 0 deletions
25
...circular-import/layered-architecture/shop_import_recursion/controllers/shop_controller.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# from shop_import_recursion.services import shop_service | ||
import shop_import_recursion | ||
|
||
|
||
class ShopController: | ||
_instance = None | ||
|
||
def __new__(cls, *args, **kwargs): | ||
if not cls._instance: | ||
cls._instance = super(ShopController, cls).__new__( | ||
cls, *args, **kwargs | ||
) | ||
return cls._instance | ||
|
||
def __init__(self): | ||
if not hasattr(self, "service"): | ||
# self.service = shop_service.ShopService() | ||
print(dir(shop_import_recursion)) | ||
self.service = ( | ||
shop_import_recursion.services.shop_service.ShopService() | ||
) | ||
|
||
def handle_request(self): | ||
print("Handling request") | ||
self.service.perform_service_task() |
Empty file.
24 changes: 24 additions & 0 deletions
24
python-circular-import/layered-architecture/shop_import_recursion/services/shop_service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# from shop_import_recursion.controllers import shop_controller | ||
import shop_import_recursion | ||
|
||
|
||
class ShopService: | ||
_instance = None | ||
|
||
def __new__(cls, *args, **kwargs): | ||
if not cls._instance: | ||
cls._instance = super(ShopService, cls).__new__( | ||
cls, *args, **kwargs | ||
) | ||
return cls._instance | ||
|
||
def __init__(self): | ||
if not hasattr(self, "controller"): | ||
# self.controller = shop_controller.ShopController() | ||
self.controller = ( | ||
shop_import_recursion.controllers.shop_controller.ShopController() | ||
) | ||
|
||
def perform_service_task(self): | ||
print("Service task performed") | ||
self.controller.handle_request() |
Empty file.
Empty file.
5 changes: 5 additions & 0 deletions
5
python-circular-import/state-machine/state_machine/__main__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from state_machine.state_a import StateA | ||
|
||
if __name__ == "__main__": | ||
current_state = StateA() | ||
current_state = current_state.on_event("to_b") |
9 changes: 9 additions & 0 deletions
9
python-circular-import/state-machine/state_machine/state_a.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from state_machine.state_b import StateB | ||
|
||
|
||
class StateA: | ||
def on_event(self, event): | ||
if event == "to_b": | ||
return StateB() | ||
else: | ||
return self |
9 changes: 9 additions & 0 deletions
9
python-circular-import/state-machine/state_machine/state_b.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from state_machine.state_a import StateA | ||
|
||
|
||
class StateB: | ||
def on_event(self, event): | ||
if event == "to_a": | ||
return StateA() | ||
else: | ||
return self |
Empty file.
5 changes: 5 additions & 0 deletions
5
python-circular-import/state-machine/state_machine_base/__main__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from state_machine_base.base_state import BaseState | ||
|
||
if __name__ == "__main__": | ||
current_state = BaseState() | ||
current_state = current_state.on_event("to_b") |
12 changes: 12 additions & 0 deletions
12
python-circular-import/state-machine/state_machine_base/base_state.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class BaseState: | ||
state_registry = {} | ||
|
||
@classmethod | ||
def register_state(cls, name, state_cls): | ||
cls.state_registry[name] = state_cls | ||
|
||
def on_event(self, event): | ||
next_state_cls = self.state_registry.get(event) | ||
if next_state_cls: | ||
return next_state_cls() | ||
return self |
8 changes: 8 additions & 0 deletions
8
python-circular-import/state-machine/state_machine_base/state_a.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from state_machine_base.base_state import BaseState | ||
|
||
|
||
class StateA(BaseState): | ||
pass | ||
|
||
|
||
BaseState.register_state("to_b", "StateB") |
8 changes: 8 additions & 0 deletions
8
python-circular-import/state-machine/state_machine_base/state_b.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from state_machine_base.base_state import BaseState | ||
|
||
|
||
class StateB(BaseState): | ||
pass | ||
|
||
|
||
BaseState.register_state("to_a", "StateA") |
Empty file.
9 changes: 9 additions & 0 deletions
9
python-circular-import/state-machine/state_machine_controller/__main__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from state_machine_controller.controller import Controller | ||
|
||
if __name__ == "__main__": | ||
controller = Controller() | ||
print(controller.state) | ||
controller.on_event("to_b") | ||
print(controller.state) | ||
controller.on_event("to_a") | ||
print(controller.state) |
16 changes: 16 additions & 0 deletions
16
python-circular-import/state-machine/state_machine_controller/controller.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from state_machine_controller.state_a import StateA | ||
from state_machine_controller.state_b import StateB | ||
|
||
|
||
class Controller: | ||
def __init__(self): | ||
self.state = StateA() | ||
|
||
def on_event(self, event): | ||
match event: | ||
case "to_a": | ||
self.state = StateA() | ||
case "to_b": | ||
self.state = StateB() | ||
case _: | ||
self.state = self.state |
2 changes: 2 additions & 0 deletions
2
python-circular-import/state-machine/state_machine_controller/state_a.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class StateA: | ||
pass |
2 changes: 2 additions & 0 deletions
2
python-circular-import/state-machine/state_machine_controller/state_b.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class StateB: | ||
pass |
Empty file.
5 changes: 5 additions & 0 deletions
5
python-circular-import/state-machine/state_machine_module/__main__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from state_machine_module.states import StateA | ||
|
||
if __name__ == "__main__": | ||
current_state = StateA() | ||
current_state = current_state.on_event("to_b") |
14 changes: 14 additions & 0 deletions
14
python-circular-import/state-machine/state_machine_module/states.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class StateA: | ||
def on_event(self, event): | ||
if event == "to_b": | ||
return StateB() | ||
else: | ||
return self | ||
|
||
|
||
class StateB: | ||
def on_event(self, event): | ||
if event == "to_a": | ||
return StateA() | ||
else: | ||
return self |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from interface import create_instance | ||
|
||
|
||
def main(): | ||
# Create an instance of ImplementationA | ||
implementation = create_instance("A") | ||
implementation.do_something() # Output: Implementation A is doing something. | ||
|
||
# Create an instance of ImplementationB | ||
implementation = create_instance("B") | ||
implementation.do_something() # Output: Implementation B is doing something. | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
8 changes: 8 additions & 0 deletions
8
python-circular-import/user-code/code_with_me/implementation_a.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from interface import BaseInterface | ||
|
||
|
||
class ImplementationA(BaseInterface): | ||
"""Concrete implementation A.""" | ||
|
||
def do_something(self): | ||
print("Implementation A is doing something.") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This package,
shop_import_recursion
, can be ignored for now. It's an interesting example of a circular import not getting caught as anImportError
but as aRecursionError
. However, my feeling is that it's a bit too contrived to include in a short "How To" article.