Skip to content

Commit ad74b20

Browse files
committed
docstrings
1 parent ba8869f commit ad74b20

File tree

6 files changed

+34
-37
lines changed

6 files changed

+34
-37
lines changed

.idea/workspace.xml

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .easy_postgres_engine.postgres_engine import PostgresEngine

easy_postgres_engine/postgres_engine.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@
88

99
class PostgresEngine:
1010

11-
def __init__(self, databaseName, user, password, host='localhost', port=5432):
11+
def __init__(self, databaseName: str, user: str, password: str, host: str = 'localhost', port: int = 5432):
12+
"""
13+
Class for accessing Postgres databases more easily.
14+
15+
:param databaseName (str): the name of the database to connect to
16+
:param user (str): the user to log in as
17+
:param password (str): password of the user
18+
:param host (str): host address to connect to
19+
:param port (int): port where the database is available
20+
"""
1221
self.databaseName = databaseName
1322
self.user = user
1423
self.password = password
@@ -24,7 +33,7 @@ def _get_connection(self):
2433
logging.exception(f'Error connecting to PostgreSQL {ex}')
2534
raise ex
2635

27-
def _get_cursor(self, isInsertionQuery):
36+
def _get_cursor(self, isInsertionQuery: bool):
2837
if isInsertionQuery:
2938
self.cursor = self.connection.cursor()
3039
else:
@@ -42,7 +51,7 @@ def close(self):
4251
if self.cursor is not None:
4352
self._close_cursor()
4453

45-
def create_table(self, schema):
54+
def create_table(self, schema: str):
4655
self._get_connection()
4756
self._get_cursor(isInsertionQuery=True)
4857
self.cursor.execute(schema)
@@ -54,7 +63,7 @@ def create_table(self, schema):
5463
finally:
5564
self.close()
5665

57-
def create_index(self, tableName, column):
66+
def create_index(self, tableName: str, column: str):
5867
self._get_connection()
5968
self._get_cursor(isInsertionQuery=True)
6069
indexQuery = f'CREATE INDEX IF NOT EXISTS {tableName}_{column} ON {tableName}({column});'
@@ -67,21 +76,8 @@ def create_index(self, tableName, column):
6776
finally:
6877
self.close()
6978

70-
def create_new_foreign_key_constraint(self, tableName, constraintName, foreignKeySQL):
71-
FOREIGN_KEY_QUERY = """
72-
ALTER TABLE {tableName}
73-
ADD CONSTRAINT {constraintName} {foreignKeySQL};
74-
"""
75-
self.create_table(
76-
schema=FOREIGN_KEY_QUERY.format(
77-
tableName=tableName,
78-
constraintName=constraintName,
79-
foreignKeySQL=foreignKeySQL
80-
)
81-
)
82-
83-
@retry(numRetries=5, retryDelay=3, backoffScalingFactor=2)
84-
def run_select_query(self, query, parameters=None):
79+
@retry(numRetries=5, retryDelaySeconds=3, backoffScalingFactor=2)
80+
def run_select_query(self, query: str, parameters: dict = None):
8581
self._get_connection()
8682
self._get_cursor(isInsertionQuery=False)
8783
self.cursor.execute(query, parameters)
@@ -90,8 +86,8 @@ def run_select_query(self, query, parameters=None):
9086
outputDataframe = pd.DataFrame(outputs)
9187
return outputDataframe.where(outputDataframe.notnull(), None).dropna(axis=0, how='all')
9288

93-
@retry(numRetries=5, retryDelay=3, backoffScalingFactor=2)
94-
def run_update_query(self, query, parameters=None, returnId=True):
89+
@retry(numRetries=5, retryDelaySeconds=3, backoffScalingFactor=2)
90+
def run_update_query(self, query: str, parameters: dict = None, returnId: bool = True):
9591
self._get_connection()
9692
self._get_cursor(isInsertionQuery=True)
9793
if returnId:

easy_postgres_engine/retry_decorator.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1+
import logging
2+
13
from functools import wraps
24
from time import sleep
35

46

5-
def retry(numRetries=5, retryDelay=3, backoffScalingFactor=2, logger=None):
7+
def retry(numRetries: int = 5, retryDelaySeconds: int = 3, backoffScalingFactor: int = 2):
68

79
def retry_decorator(func):
810
@wraps(func)
911
def retry_function(*args, **kwargs):
10-
numTries, currentDelay = numRetries, retryDelay
12+
numTries, currentDelay = numRetries, retryDelaySeconds
1113
while numTries > 1:
1214
try:
1315
return func(*args, **kwargs)
1416
except Exception as ex:
1517
exceptionMessage = f'{ex}, Retrying in {currentDelay} seconds...'
16-
if logger:
17-
logger.warning(exceptionMessage)
18-
else:
19-
print(exceptionMessage)
18+
logging.warning(exceptionMessage)
2019
sleep(currentDelay)
2120
numTries -= 1
2221
currentDelay *= backoffScalingFactor
File renamed without changes.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
download_url='https://github.com/miksyr/easy_postgres_engine/archive/v_01.tar.gz',
1212
keywords=['postgreSQL', 'postgres'],
1313
install_requires=[
14-
'pandas==0.25.3'
14+
'pandas==0.25.3',
1515
'psycopg2-binary==2.8.4'
1616
],
1717
classifiers=[

0 commit comments

Comments
 (0)