Gestione dei database con la struttura with

This commit is contained in:
Fabio Scotto di Santolo
2021-07-01 19:46:46 +02:00
parent 6db03d7d88
commit b02c6c1252
2 changed files with 42 additions and 0 deletions

0
database/__init__.py Normal file
View File

42
database/main.py Normal file
View File

@@ -0,0 +1,42 @@
class FakeCursor:
pass
class FakeConnection:
def __init__(self, configuration: dict) -> None:
self.__configuration = configuration
def cursor(self):
return FakeCursor()
class UseDatabase:
def __init__(self, config: dict) -> None:
self.__configuration = config
def __enter__(self) -> 'cursor':
print("Initializing DB connection...")
self.__connection = FakeConnection(self.__configuration)
self.__cursor = self.__connection.cursor()
return self.__cursor
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
"""
Python call this method when close resources
:param exc_type:
:param exc_val:
:param exc_tb:
:return:
"""
print("...Close resources")
print(f"{exc_type=}, {exc_val=}, {exc_tb=}")
def main():
with UseDatabase({}) as my_cursor:
print("Execute 'with' body")
print(f"{my_cursor=}")
if __name__ == '__main__':
main()