Skip to content

sql_source

mlpype.sklearn.data.sql_source

Provides tools for using SQL as an input data source.

SqlSource

Bases: DataSource[DataFrame]

Reads data from a sql table using Pandas read_sql.

Source code in packages/mlpype-sklearn/src/mlpype/sklearn/data/sql_source.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class SqlSource(DataSource[pd.DataFrame]):
    """Reads data from a sql table using Pandas read_sql."""

    def __init__(
        self,
        sql: str,
        con: str,
        *args: Any,
        **kwargs: Any,
    ):
        """Reads data from a sql table using Pandas read_sql.

        This class only works as a storage for the arguments to call Pandas read_sql.
        For proper documentation on the arguments, check Pandas read_sql.

        Args:
            sql (str): The sql and the `sql` argument to `Pandas read_sql`.
            con (str): The con string and `con` argument to `Pandas read_sql`.
            *args (Any): Positional arguments to be passed to `Pandas read_sql`.
            **kwargs (Any): Keyword arguments to be passed to `Pandas read_sql`.
        """
        self.sql = sql
        self.con = con
        self.args = args
        self.kwargs = kwargs
        super().__init__()

    def read(self) -> pd.DataFrame:
        """Use the provided arguments to call `read_sql`.

        Returns:
            pd.DataFrame: The result of the provided query.
        """
        return pd.read_sql(*self.args, sql=self.sql, con=self.con, **self.kwargs)

__init__(sql, con, *args, **kwargs)

Reads data from a sql table using Pandas read_sql.

This class only works as a storage for the arguments to call Pandas read_sql. For proper documentation on the arguments, check Pandas read_sql.

Parameters:

Name Type Description Default
sql str

The sql and the sql argument to Pandas read_sql.

required
con str

The con string and con argument to Pandas read_sql.

required
*args Any

Positional arguments to be passed to Pandas read_sql.

()
**kwargs Any

Keyword arguments to be passed to Pandas read_sql.

{}
Source code in packages/mlpype-sklearn/src/mlpype/sklearn/data/sql_source.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def __init__(
    self,
    sql: str,
    con: str,
    *args: Any,
    **kwargs: Any,
):
    """Reads data from a sql table using Pandas read_sql.

    This class only works as a storage for the arguments to call Pandas read_sql.
    For proper documentation on the arguments, check Pandas read_sql.

    Args:
        sql (str): The sql and the `sql` argument to `Pandas read_sql`.
        con (str): The con string and `con` argument to `Pandas read_sql`.
        *args (Any): Positional arguments to be passed to `Pandas read_sql`.
        **kwargs (Any): Keyword arguments to be passed to `Pandas read_sql`.
    """
    self.sql = sql
    self.con = con
    self.args = args
    self.kwargs = kwargs
    super().__init__()

read()

Use the provided arguments to call read_sql.

Returns:

Type Description
DataFrame

pd.DataFrame: The result of the provided query.

Source code in packages/mlpype-sklearn/src/mlpype/sklearn/data/sql_source.py
37
38
39
40
41
42
43
def read(self) -> pd.DataFrame:
    """Use the provided arguments to call `read_sql`.

    Returns:
        pd.DataFrame: The result of the provided query.
    """
    return pd.read_sql(*self.args, sql=self.sql, con=self.con, **self.kwargs)