Skip to content

Commit 4d855b8

Browse files
authored
Merge pull request #4102 from hozlucas28/Solution-23-Python
#23 - Python
2 parents e24b19d + be4d8c7 commit 4d855b8

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# pylint: disable=pointless-string-statement,missing-class-docstring,missing-function-docstring
2+
3+
"""
4+
Singleton pattern...
5+
"""
6+
7+
from typing import Any, Self
8+
9+
10+
print("Singleton pattern...")
11+
12+
13+
class SingletonMeta(type):
14+
__instances: dict[Any, Any] = {}
15+
16+
def __call__(cls, *args: Any, **kwds: Any) -> Any:
17+
if cls not in cls.__instances:
18+
instance: Any = super().__call__(*args, **kwds)
19+
cls.__instances[cls] = instance
20+
21+
return cls.__instances[cls]
22+
23+
24+
class Counter(metaclass=SingletonMeta):
25+
def __init__(self) -> None:
26+
self.__count: int = 0
27+
28+
def get_count(self) -> int:
29+
return self.__count
30+
31+
def increment(self) -> Self:
32+
self.__count += 1
33+
return self
34+
35+
def decrement(self) -> Self:
36+
self.__count -= 1
37+
return self
38+
39+
40+
counter_01: Counter = Counter()
41+
counter_02: Counter = Counter()
42+
43+
print(
44+
"\nAre `counter_01` and `counter_02` the same instance of `Counter` class?"
45+
+ f" {counter_01 == counter_02}",
46+
)
47+
48+
print("\nMethod call of `counter_01` instance...")
49+
50+
counter_01.increment().increment().increment()
51+
print("\ncounter01.increment().increment().increment()")
52+
53+
print(f"\nCount attribute of `counter_01` instance --> {counter_01.get_count()}")
54+
55+
56+
print("\nMethod call of `counter_02` instance...")
57+
58+
counter_02.decrement()
59+
print("\ncounter02.decrement()")
60+
61+
print(f"\nCount attribute of `counter_02` instance --> {counter_02.get_count()}")
62+
63+
print(
64+
"\n# ---------------------------------------------------------------------------------- #\n"
65+
)
66+
67+
68+
"""
69+
Additional challenge...
70+
"""
71+
72+
print("Additional challenge...")
73+
74+
75+
class UserSession(metaclass=SingletonMeta):
76+
__email: str
77+
__uid: str
78+
__name: str
79+
__user_name: str
80+
81+
def __init__(self, *, email="", uid="", name="", user_name="") -> None:
82+
self.__email = email
83+
self.__uid = uid
84+
self.__name = name
85+
self.__user_name = user_name
86+
87+
def get_email(self) -> str:
88+
return self.__email
89+
90+
def get_uid(self) -> str:
91+
return self.__uid
92+
93+
def get_name(self) -> str:
94+
return self.__name
95+
96+
def get_user_name(self) -> str:
97+
return self.__user_name
98+
99+
def set_email(self, new_email: str) -> Self:
100+
self.__email = new_email
101+
return self
102+
103+
def set_uid(self, new_uid: str) -> Self:
104+
self.__uid = new_uid
105+
return self
106+
107+
def set_name(self, new_name: str) -> Self:
108+
self.__name = new_name
109+
return self
110+
111+
def set_user_name(self, new_user_name: str) -> Self:
112+
self.__user_name = new_user_name
113+
return self
114+
115+
def delete_data(self) -> Self:
116+
self.__email = ""
117+
self.__uid = ""
118+
self.__name = ""
119+
self.__user_name = ""
120+
return self
121+
122+
123+
user_session: UserSession = UserSession(
124+
125+
name="Juan",
126+
uid="15H4G-A5D-1Y7",
127+
user_name="JuanGamer16",
128+
)
129+
130+
print(
131+
"\nUser: "
132+
+ f"{user_session.get_uid()} {user_session.get_name()} "
133+
+ f"{user_session.get_user_name()} {user_session.get_email()}"
134+
)
135+
136+
user_session.delete_data()
137+
print("\nUser data deleted!")
138+
139+
print(
140+
"\nUser: "
141+
+ f"{user_session.get_uid()} {user_session.get_name()} "
142+
+ f"{user_session.get_user_name()} {user_session.get_email()}"
143+
)

0 commit comments

Comments
 (0)