GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — PythonConceptExamples/DependencyInversion.py at master · ha04259/PythonConceptExamples

Expand file tree

/

DependencyInversion.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
59 lines (41 loc) · 1.52 KB
/

DependencyInversion.py

Copy path

File metadata and controls

59 lines (41 loc) · 1.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from abc import abstractmethod
from enum import Enum
class Relationship(Enum):
PARENT = 0
CHILD = 1
SIBLING = 2
class Person:
def __init__(self, name):
self.name = name
class RelationshipBrowser:
@abstractmethod
def find_all_children_of(self, name): pass
class Relationships(RelationshipBrowser): # low-level
relations = []
def add_parent_and_child(self, parent, child):
self.relations.append((parent, Relationship.PARENT, child))
self.relations.append((child, Relationship.PARENT, parent))
def find_all_children_of(self, name):
for r in self.relations:
if r[0].name == name and r[1] == Relationship.PARENT:
yield r[2].name
class Research:
# dependency on a low-level module directly
# bad because strongly dependent on e.g. storage type
# def __init__(self, relationships):
# # high-level: find all of john's children
# relations = relationships.relations
# for r in relations:
# if r[0].name == 'John' and r[1] == Relationship.PARENT:
# print(f'John has a child called {r[2].name}.')
def __init__(self, browser):
for p in browser.find_all_children_of("John"):
print(f'John has a child called {p}')
parent = Person('John')
child1 = Person('Chris')
child2 = Person('Matt')
# low-level module
relationships = Relationships()
relationships.add_parent_and_child(parent, child1)
relationships.add_parent_and_child(parent, child2)
Research(relationships)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.