Skip to content

Commit 7e4a70d

Browse files
committed
formatting and cleanup
1 parent f3fea9c commit 7e4a70d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2217
-1906
lines changed

coverage.xml

Lines changed: 701 additions & 610 deletions
Large diffs are not rendered by default.

dsg_lib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# -*- coding: utf-8 -*-
22

3-
__version__ = '0.13.3'
3+
__version__ = "0.13.3"

dsg_lib/async_database_functions/__import_sqlalchemy.py

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
11
# -*- coding: utf-8 -*-
2+
"""
3+
This module provides functionality to import and validate SQLAlchemy components, ensuring compatibility with the required version.
4+
5+
The `import_sqlalchemy` function is the primary function in this module. It imports various components from SQLAlchemy, checks the version of SQLAlchemy, and raises an ImportError if the version is below the minimum required version.
6+
7+
Usage example:
8+
```python
9+
from import_sqlalchemy import import_sqlalchemy
10+
11+
sqlalchemy_components = import_sqlalchemy()
12+
sqlalchemy, MetaData, create_engine, text, Column, DateTime, String, IntegrityError, SQLAlchemyError, AsyncSession = sqlalchemy_components
13+
14+
# Example usage of imported components
15+
engine = create_engine('sqlite:///example.db')
16+
metadata = MetaData()
17+
```
18+
19+
Author(s):
20+
Mike Ryan
21+
22+
Date Created:
23+
2024/05/16
24+
Date Updated:
25+
2024/07/26
26+
"""
227
from typing import Tuple
328

429
from loguru import logger
@@ -15,7 +40,7 @@ def import_sqlalchemy() -> Tuple:
1540
and raises an ImportError if the version is less than the minimum required version.
1641
1742
Returns:
18-
tuple: A tuple containing the following SQLAlchemy components:
43+
Tuple: A tuple containing the following SQLAlchemy components:
1944
- sqlalchemy: The SQLAlchemy module.
2045
- MetaData: The MetaData class from SQLAlchemy.
2146
- create_engine: The create_engine function from SQLAlchemy.
@@ -26,23 +51,25 @@ def import_sqlalchemy() -> Tuple:
2651
- IntegrityError: The IntegrityError exception from SQLAlchemy.
2752
- SQLAlchemyError: The SQLAlchemyError exception from SQLAlchemy.
2853
- AsyncSession: The AsyncSession class from SQLAlchemy.
29-
- create_async_engine: The create_async_engine function from SQLAlchemy.
30-
- select: The select function from SQLAlchemy.
31-
- declarative_base: The declarative_base function from SQLAlchemy.
32-
- sessionmaker: The sessionmaker function from SQLAlchemy.
33-
- func: The func object from SQLAlchemy.
34-
- NoResultFound: The NoResultFound exception from SQLAlchemy.
3554
3655
Raises:
37-
ImportError: If the SQLAlchemy version is less than the minimum required version.
56+
ImportError: If SQLAlchemy is not installed or the version is below the minimum required version.
57+
58+
Example:
59+
```python
60+
from import_sqlalchemy import import_sqlalchemy
61+
62+
sqlalchemy_components = import_sqlalchemy()
63+
sqlalchemy, MetaData, create_engine, text, Column, DateTime, String, IntegrityError, SQLAlchemyError, AsyncSession = sqlalchemy_components
3864
39-
Author: Mike Ryan
40-
Date: 2024/05/16
41-
License: MIT
65+
# Example usage of imported components
66+
engine = create_engine('sqlite:///example.db')
67+
metadata = MetaData()
68+
```
4269
"""
43-
min_version = '2.0.0' # Minimum required version of SQLAlchemy
70+
min_version = "2.0.0" # Minimum required version of SQLAlchemy
4471

45-
logger.info('Attempting to import SQLAlchemy...')
72+
logger.info("Attempting to import SQLAlchemy...")
4673

4774
try:
4875
# Import SQLAlchemy and its components
@@ -55,11 +82,11 @@ def import_sqlalchemy() -> Tuple:
5582
from sqlalchemy.orm.exc import NoResultFound
5683
from sqlalchemy.sql import func
5784

58-
logger.info('Successfully imported SQLAlchemy.')
85+
logger.info("Successfully imported SQLAlchemy.")
5986

6087
except ImportError: # pragma: no cover
6188
# Handle the case where SQLAlchemy is not installed
62-
logger.error('Failed to import SQLAlchemy.')
89+
logger.error("Failed to import SQLAlchemy.")
6390
create_engine = text = sqlalchemy = None # pragma: no cover
6491

6592
# Check SQLAlchemy version
@@ -68,13 +95,13 @@ def import_sqlalchemy() -> Tuple:
6895
) < packaging_version.parse(min_version):
6996
# If the installed version is less than the minimum required version, raise an error
7097
logger.error(
71-
f'SQLAlchemy version >= {min_version} required, run `pip install --upgrade sqlalchemy`'
98+
f"SQLAlchemy version >= {min_version} required, run `pip install --upgrade sqlalchemy`"
7299
)
73100
raise ImportError(
74-
f'SQLAlchemy version >= {min_version} required, run `pip install --upgrade sqlalchemy`'
101+
f"SQLAlchemy version >= {min_version} required, run `pip install --upgrade sqlalchemy`"
75102
) # pragma: no cover
76103

77-
logger.info('Returning SQLAlchemy components.')
104+
logger.info("Returning SQLAlchemy components.")
78105

79106
# Return the imported SQLAlchemy components
80107
return (
@@ -118,4 +145,6 @@ def import_sqlalchemy() -> Tuple:
118145
String, # The String class from SQLAlchemy
119146
func, # The func object from SQLAlchemy
120147
NoResultFound, # The NoResultFound exception from SQLAlchemy
121-
) = import_sqlalchemy() # Call the function that imports SQLAlchemy and checks its version
148+
) = (
149+
import_sqlalchemy()
150+
) # Call the function that imports SQLAlchemy and checks its version

dsg_lib/async_database_functions/async_database.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def __init__(self, db_config: DBConfig):
8585
"""
8686
self.db_config = db_config
8787
self.Base = BASE
88-
logger.debug('AsyncDatabase initialized')
88+
logger.debug("AsyncDatabase initialized")
8989

9090
def get_db_session(self):
9191
"""This method returns a context manager that provides a new database
@@ -96,7 +96,7 @@ def get_db_session(self):
9696
Returns: contextlib._GeneratorContextManager: A context manager that
9797
provides a new database session.
9898
"""
99-
logger.debug('Getting database session')
99+
logger.debug("Getting database session")
100100
return self.db_config.get_db_session()
101101

102102
async def create_tables(self):
@@ -106,7 +106,7 @@ async def create_tables(self):
106106
107107
Returns: None
108108
"""
109-
logger.debug('Creating tables')
109+
logger.debug("Creating tables")
110110
try:
111111
# Bind the engine to the metadata of the base class
112112
self.Base.metadata.bind = self.db_config.engine
@@ -115,8 +115,8 @@ async def create_tables(self):
115115
async with self.db_config.engine.begin() as conn:
116116
# Run a function in a synchronous manner
117117
await conn.run_sync(self.Base.metadata.create_all)
118-
logger.info('Tables created successfully')
118+
logger.info("Tables created successfully")
119119
except Exception as ex: # pragma: no cover
120120
# Log the error and raise it
121-
logger.error(f'Error creating tables: {ex}') # pragma: no cover
121+
logger.error(f"Error creating tables: {ex}") # pragma: no cover
122122
raise # pragma: no cover

dsg_lib/async_database_functions/base_schema.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,18 @@ class MyModel(base_schema.SchemaBaseSQLite):
5656
String, # The String class from SQLAlchemy
5757
func, # The func object from SQLAlchemy
5858
NoResultFound, # The NoResultFound exception from SQLAlchemy
59-
) = import_sqlalchemy() # Call the function that imports SQLAlchemy and checks its version
59+
) = (
60+
import_sqlalchemy()
61+
) # Call the function that imports SQLAlchemy and checks its version
6062

6163

6264
# comments
63-
uuid_comment = 'Unique identifier for each record, a string representation of a UUID'
64-
date_created_comment = 'Date and time when a row was inserted, defaults to current UTC time'
65+
uuid_comment = "Unique identifier for each record, a string representation of a UUID"
66+
date_created_comment = (
67+
"Date and time when a row was inserted, defaults to current UTC time"
68+
)
6569
date_updated_comment = (
66-
'Date and time when a row was last updated, defaults to current UTC time on update'
70+
"Date and time when a row was last updated, defaults to current UTC time on update"
6771
)
6872

6973

@@ -228,7 +232,7 @@ class MyModel(base_schema.SchemaBaseMySQL, BASE):
228232
date_created = Column(
229233
DateTime,
230234
index=True,
231-
server_default=text('UTC_TIMESTAMP()'),
235+
server_default=text("UTC_TIMESTAMP()"),
232236
comment=date_created_comment,
233237
)
234238

@@ -237,8 +241,8 @@ class MyModel(base_schema.SchemaBaseMySQL, BASE):
237241
date_updated = Column(
238242
DateTime,
239243
index=True,
240-
server_default=text('UTC_TIMESTAMP()'),
241-
onupdate=text('UTC_TIMESTAMP()'),
244+
server_default=text("UTC_TIMESTAMP()"),
245+
onupdate=text("UTC_TIMESTAMP()"),
242246
comment=date_updated_comment,
243247
)
244248

@@ -287,7 +291,7 @@ class MyModel(base_schema.SchemaBaseOracle, BASE):
287291
date_created = Column(
288292
DateTime,
289293
index=True,
290-
server_default=text('SYS_EXTRACT_UTC(SYSTIMESTAMP)'),
294+
server_default=text("SYS_EXTRACT_UTC(SYSTIMESTAMP)"),
291295
comment=date_created_comment,
292296
)
293297

@@ -296,8 +300,8 @@ class MyModel(base_schema.SchemaBaseOracle, BASE):
296300
date_updated = Column(
297301
DateTime,
298302
index=True,
299-
server_default=text('SYS_EXTRACT_UTC(SYSTIMESTAMP)'),
300-
onupdate=text('SYS_EXTRACT_UTC(SYSTIMESTAMP)'),
303+
server_default=text("SYS_EXTRACT_UTC(SYSTIMESTAMP)"),
304+
onupdate=text("SYS_EXTRACT_UTC(SYSTIMESTAMP)"),
301305
comment=date_updated_comment,
302306
)
303307

@@ -346,7 +350,7 @@ class MyModel(base_schema.SchemaBaseMSSQL, BASE):
346350
date_created = Column(
347351
DateTime,
348352
index=True,
349-
server_default=text('GETUTCDATE()'),
353+
server_default=text("GETUTCDATE()"),
350354
comment=date_created_comment,
351355
)
352356

@@ -355,8 +359,8 @@ class MyModel(base_schema.SchemaBaseMSSQL, BASE):
355359
date_updated = Column(
356360
DateTime,
357361
index=True,
358-
server_default=text('GETUTCDATE()'),
359-
onupdate=text('GETUTCDATE()'),
362+
server_default=text("GETUTCDATE()"),
363+
onupdate=text("GETUTCDATE()"),
360364
comment=date_updated_comment,
361365
)
362366

@@ -405,18 +409,18 @@ class MyModel(base_schema.SchemaBaseFirebird, BASE):
405409
date_created = Column(
406410
DateTime,
407411
index=True,
408-
server_default=text('CURRENT_TIMESTAMP'),
409-
comment='Date and time when a row was inserted, defaults to current time',
412+
server_default=text("CURRENT_TIMESTAMP"),
413+
comment="Date and time when a row was inserted, defaults to current time",
410414
)
411415

412416
# The date and time when a particular row was last updated. It defaults to
413417
# the current time whenever the instance is updated.
414418
date_updated = Column(
415419
DateTime,
416420
index=True,
417-
server_default=text('CURRENT_TIMESTAMP'),
418-
onupdate=text('CURRENT_TIMESTAMP'),
419-
comment='Date and time when a row was last updated, defaults to current time on update',
421+
server_default=text("CURRENT_TIMESTAMP"),
422+
onupdate=text("CURRENT_TIMESTAMP"),
423+
comment="Date and time when a row was last updated, defaults to current time on update",
420424
)
421425

422426

@@ -464,7 +468,7 @@ class MyModel(base_schema.SchemaBaseSybase, BASE):
464468
date_created = Column(
465469
DateTime,
466470
index=True,
467-
server_default=text('GETUTCDATE()'),
471+
server_default=text("GETUTCDATE()"),
468472
comment=date_created_comment,
469473
)
470474

@@ -473,8 +477,8 @@ class MyModel(base_schema.SchemaBaseSybase, BASE):
473477
date_updated = Column(
474478
DateTime,
475479
index=True,
476-
server_default=text('GETUTCDATE()'),
477-
onupdate=text('GETUTCDATE()'),
480+
server_default=text("GETUTCDATE()"),
481+
onupdate=text("GETUTCDATE()"),
478482
comment=date_updated_comment,
479483
)
480484

dsg_lib/async_database_functions/database_config.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@
6767
String, # The String class from SQLAlchemy
6868
func, # The func object from SQLAlchemy
6969
NoResultFound, # The NoResultFound exception from SQLAlchemy
70-
) = import_sqlalchemy() # Call the function that imports SQLAlchemy and checks its version
70+
) = (
71+
import_sqlalchemy()
72+
) # Call the function that imports SQLAlchemy and checks its version
7173

7274

7375
# Now you can use declarative_base at the module level
@@ -118,15 +120,15 @@ class DBConfig:
118120
"""
119121

120122
SUPPORTED_PARAMETERS = {
121-
'sqlite': {'echo', 'future', 'pool_recycle'},
122-
'postgresql': {
123-
'echo',
124-
'future',
125-
'pool_pre_ping',
126-
'pool_size',
127-
'max_overflow',
128-
'pool_recycle',
129-
'pool_timeout',
123+
"sqlite": {"echo", "future", "pool_recycle"},
124+
"postgresql": {
125+
"echo",
126+
"future",
127+
"pool_pre_ping",
128+
"pool_size",
129+
"max_overflow",
130+
"pool_recycle",
131+
"pool_timeout",
130132
},
131133
# Add other engines here...
132134
}
@@ -175,11 +177,15 @@ def __init__(self, config: Dict):
175177
```
176178
"""
177179
self.config = config
178-
engine_type = self.config['database_uri'].split('+')[0]
180+
engine_type = self.config["database_uri"].split("+")[0]
179181
supported_parameters = self.SUPPORTED_PARAMETERS.get(engine_type, set())
180-
unsupported_parameters = set(config.keys()) - supported_parameters - {'database_uri'}
182+
unsupported_parameters = (
183+
set(config.keys()) - supported_parameters - {"database_uri"}
184+
)
181185
if unsupported_parameters:
182-
error_message = f'Unsupported parameters for {engine_type}: {unsupported_parameters}'
186+
error_message = (
187+
f"Unsupported parameters for {engine_type}: {unsupported_parameters}"
188+
)
183189
logger.error(error_message)
184190
raise Exception(error_message)
185191

@@ -188,7 +194,9 @@ def __init__(self, config: Dict):
188194
for param in supported_parameters
189195
if self.config.get(param) is not None
190196
}
191-
self.engine = create_async_engine(self.config['database_uri'], **engine_parameters)
197+
self.engine = create_async_engine(
198+
self.config["database_uri"], **engine_parameters
199+
)
192200
self.metadata = MetaData()
193201

194202
@asynccontextmanager
@@ -229,7 +237,7 @@ async def get_db_session(self):
229237
# Perform your database operations here pass
230238
```
231239
"""
232-
logger.debug('Creating new database session')
240+
logger.debug("Creating new database session")
233241
try:
234242
# Create a new database session
235243
async with sessionmaker(
@@ -239,8 +247,8 @@ async def get_db_session(self):
239247
yield session
240248
except SQLAlchemyError as e: # pragma: no cover
241249
# Log the error and raise it
242-
logger.error(f'Database error occurred: {str(e)}') # pragma: no cover
250+
logger.error(f"Database error occurred: {str(e)}") # pragma: no cover
243251
raise # pragma: no cover
244252
finally: # pragma: no cover
245253
# Log the end of the database session
246-
logger.debug('Database session ended') # pragma: no cover
254+
logger.debug("Database session ended") # pragma: no cover

0 commit comments

Comments
 (0)