Commit cacd9ab0 authored by Tester Jr, Christopher's avatar Tester Jr, Christopher
Browse files

add import errors for mixins, separate out pandas mixin

parent e17fc3f0
Loading
Loading
Loading
Loading
+7 −6
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ def convert_to_update(dictionary):
class CRUDTable(ABC):
    """Defines a database table with standard CRUD operations"""

    def __init__(self, columns, schema, db):
    def __init__(self, columns, schema, db, table_name=None):
        """__init__.

        :param columns: dictionary like {'id': int, 'email': str}
@@ -66,6 +66,7 @@ class CRUDTable(ABC):
        self.columns = columns
        self.db = db
        self.schema = schema
        self.name = table_name if table_name is not None else self.__class__.__name__
        self.logger = create_logger()

    def create(self, **kwargs):
@@ -82,7 +83,7 @@ class CRUDTable(ABC):
        column_names = str(list(kwargs.keys()))[1:-1].replace("'", "")
        # build the query with the class's name and the kwargs that were passed
        query = (
            f"INSERT INTO {self.schema}.{self.__class__.__name__} "
            f"INSERT INTO {self.schema}.{self.name} "
            f"({column_names}) "
            f"VALUES ({', '.join(['%s']*len(kwargs.keys()))})"
        )
@@ -172,7 +173,7 @@ class CRUDTable(ABC):
        if where_clause is None:
            try:
                # make a query to select the columns with no where clause
                query = f"SELECT {select_clause} " + f"FROM {self.schema}.{self.__class__.__name__}"
                query = f"SELECT {select_clause} " + f"FROM {self.schema}.{self.name}"
                # inform the user we are executing the query..
                self.logger.info(f"Executing query: {query}")
                # if the db is not already opened..
@@ -193,7 +194,7 @@ class CRUDTable(ABC):
                # make a query to select the columns with the where clause
                query = (
                    f"SELECT {select_clause} "
                    + f"FROM {self.schema}.{self.__class__.__name__} "
                    + f"FROM {self.schema}.{self.name} "
                    + f"{where_clause[0]}"
                )
                # tell the user we are executing their query
@@ -264,7 +265,7 @@ class CRUDTable(ABC):
            params = tuple([*update_clause[1], *where_clause[1]])
            # build a query to update the specified values in the db
            query = (
                f"UPDATE {self.schema}.{self.__class__.__name__} "
                f"UPDATE {self.schema}.{self.name} "
                + f"SET {update_clause[0]} "
                + f"{where_clause[0]}"
            )
@@ -304,7 +305,7 @@ class CRUDTable(ABC):
            # construct the where clause with the conversion method
            where_clause = convert_to_where(kwargs)
            # build a delete query with the specified values
            query = f"DELETE FROM {self.schema}.{self.__class__.__name__} " + f"{where_clause[0]}"
            query = f"DELETE FROM {self.schema}.{self.name} " + f"{where_clause[0]}"
            # tell the user that we are executing their query
            self.logger.info(f"Executing query: {query}, " + f"params: {where_clause[1]}")
            # if the db is not open..
+12 −1
Original line number Diff line number Diff line
"""Scratch file for showing how the mixins get used."""

try:
    from influxdb import InfluxDBClient
except ImportError:
    import sys

    from common.logz import create_logger

    logger = create_logger()
    logger.warn(
        "Influx extra must be installed to use influx mixin. "
    )
    sys.exit(1)

from common.env import check_environment as ce

+12 −2
Original line number Diff line number Diff line
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Provide a `pandas` mixin for the Database class."""

try:
    import pandas as pd
    import sqlalchemy
except ImportError:
    import sys

    from common.logz import create_logger

    logger = create_logger()
    logger.warn("db-pandas extra must be installed to use mixin. ")
    sys.exit(1)


class PandasMixin:
+13 −1
Original line number Diff line number Diff line
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Allow opening with a sqlite3 connection."""

try:
    import sqlite3
except ImportError:
    import sys

    from common.logz import create_logger

    logger = create_logger()
    logger.warn(
        "Sqlite extra must be installed to use sqlite mixin. "
    )
    sys.exit(1)

from common.env import check_environment as ce
from common.logz import create_logger
+6 −3
Original line number Diff line number Diff line
@@ -30,20 +30,23 @@ classifiers = [
    "Topic :: Software Development :: Build Tools"
]
dependencies = [
    "rich==10.11.0",
    "pandas==2.1.4",
    "SQLAlchemy~=2.0.23"
    "rich==10.11.0"
]

[project.optional-dependencies]
postgres = ["psycopg2-binary==2.9.9"]
mssql = ["pymssql==2.2.11"]
influx = ["influxdb==5.3.1"]
db-pandas = [
    "pandas==2.1.4",
    "SQLAlchemy~=2.0.23"
]
auth = [
    "ldap3==2.9.1",
    "flask>=2.0.2"
]
scrapers = [
    "pandas==2.1.4",
    "urllib3~=1.26.9",
    "selenium~=4.1.3",
    "requests~=2.27.1",