1

I'm new to OOP, I'm trying to create a class and function that receives names and create the SQL calls for me, like: df_1 and df_2.
However I want that my class manages it for me, so the args will be just to repeat the names, like: ('table1', 'table2', 'n'..)..

So how can I do this in the best way? As I'm learning I appreciate explanations

import pandas as pd
from def_funcs import sup  # I got my data base engine from this func
from typing import Tuple, List, Dict

# NON OOP WAY
df_1 = pd.read_sql("""
    select * from table1
    """, sup.start_engine())
print(df_1.columns)

df_2 = pd.read_sql("""
    select * from table2
    """, sup.start_engine())
print(df_2.columns)

OOP WAY << EXPECTED >>

class ExtractDb:
def __init__(self, tbl_name, tables):
    self.tbl_name = tbl_name
    self.get_tables = tables

def get_tables(*args: str) -> Tuple[str]:
    return args

def create_db(self, tbl):
    tbl = self.tbl_name
    tbl = pd.read_sql(self.get_tables, sup.start_engine())
    return tbl
    # print(ExtractDb.get_tables('', '2', '3', '4'))
    print(ExtractDb.create_db())

The output will be the number of tables passed as args within tuples or dict (whatever).

Maybe my code is really messed up, feel free to modify it!!

  • I don't understand what you expect `get_tables` to do – juanpa.arrivillaga Aug 31 '22 at 20:43
  • @juanpa.arrivillaga I just expect it to replicate the tuple that receives strings, ex: args(tbl1, tbl2).. If not for this function, the strings cannot be replicated.. Buuut as I said, I don't even know if this is the correct way to do this. – automation_m Aug 31 '22 at 20:45
  • I'm sorry, that really doesn't make any sense to me. Currently, the `get_tables` function simply returns `args`, which doesn't *replicatae anything*, and also, that doesn't make sense as a decorator (decorators generally return callables) – juanpa.arrivillaga Aug 31 '22 at 20:48
  • Ok, so it can be inside the class, doesn't even need to be a decorator at all. I saw some example and tried to replicate (probably poorly replicated by myself) – automation_m Aug 31 '22 at 20:57
  • @juanpa.arrivillaga I have editted it, please check if it is better to understand =] – automation_m Aug 31 '22 at 21:27
  • you said you saw some examples, can you give me the link to the example? – Daniel Afriyie Aug 31 '22 at 21:38
  • @DanielAfriyie This example is the most next to mine I think, and the difference is that I already have my engine declared from my libs. https://stackoverflow.com/questions/22946139/python-class-to-convert-all-tables-in-a-database-to-pandas-dataframes – automation_m Aug 31 '22 at 21:42
  • 1
    if i'm getting you right, you want to give the `ExtractDb` class table names, and it will create pandas dataframe from the names and return it? – Daniel Afriyie Aug 31 '22 at 21:46
  • Exactly, all I'm trying to do is create pandas data frame using args, in the most right "OOP" way possible.. – automation_m Aug 31 '22 at 21:49
  • yeah, using oop is good, but i think you can use a simple function for this, give it the table names and it return the dataframes in a list – Daniel Afriyie Aug 31 '22 at 21:53

1 Answers1

1

You can try these two approaches:

  1. Use a simple function
import pandas as pd
from def_funcs import sup  # I got my data base engine from this func
from typing import Tuple, List, Dict

def get_dataframes(tables):
    dataframes = []
    for t in tables:
        sql = f"SELECT * FROM {t}"
        dataframe = pd.read_sql(sql, sup.start_engine())
        dataframes.append(dataframe)
    return dataframes
  1. Use the OOP approach
class ExtractDb:

    def __init__(self, tables):
        self.tables= tables

    def get_tables(*args: str) -> Tuple[str]:
        return args

    def create_db(self, tbl):
        dataframes = []
        for t in self.tables:
            sql = f"SELECT * FROM {t}"
            dataframe = pd.read_sql(sql, sup.start_engine())
            dataframes.append(dataframe)
        return dataframes
Daniel Afriyie
  • 217
  • 4
  • 12