Skip to content

Commit 73845b5

Browse files
committed
refactor: use early return for better readability
1 parent 6c1b3a3 commit 73845b5

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

database/database.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,24 @@ def __new__(cls):
1919
return Database._instance
2020

2121
def __init__(self):
22-
if not self._initialized:
23-
database_path = os.path.join(self.dir_path, "users.db")
24-
self.logger = logging.getLogger(__name__)
25-
26-
if not os.path.exists(database_path):
27-
self.logger.debug("File '{}' does not exist! Trying to create one.".format(database_path))
28-
try:
29-
self.create_database(database_path)
30-
except Exception:
31-
self.logger.error("An error has occurred while creating the database!")
32-
33-
self.connection = sqlite3.connect(database_path)
34-
self.connection.text_factory = lambda x: str(x, 'utf-8', "ignore")
35-
self.cursor = self.connection.cursor()
36-
37-
self._initialized = True
22+
if self._initialized:
23+
return
24+
25+
database_path = os.path.join(self.dir_path, "users.db")
26+
self.logger = logging.getLogger(__name__)
27+
28+
if not os.path.exists(database_path):
29+
self.logger.debug("File '{}' does not exist! Trying to create one.".format(database_path))
30+
try:
31+
self.create_database(database_path)
32+
except Exception:
33+
self.logger.error("An error has occurred while creating the database!")
34+
35+
self.connection = sqlite3.connect(database_path)
36+
self.connection.text_factory = lambda x: str(x, 'utf-8', "ignore")
37+
self.cursor = self.connection.cursor()
38+
39+
self._initialized = True
3840

3941
@staticmethod
4042
def create_database(database_path):

0 commit comments

Comments
 (0)