jazzis18
jazzis18

Reputation: 213

Python OOP Coding Style

For example, I want to write a program to import data from a DBF file into a database. I compiled the algorithm. First I convert a DBF file to a CSV file, then a CSV file into the Pandas Dataframe. Then I import the data from Pandas Dataframe into the database. Thus, each step of the algorithm is a separate subroutine that can work independently of the other steps.

import csv
import dbf
import pandas as pd

class Dbf3ToCsv(object):
    """Process a DBF III w/o memo file to a CSV file"""

    def __init__(self):
        pass


class CsvToDataframe(object):
    """Process a CSV file to Pandas Dataframe"""

    def __init__(self):
        pass


class DataframeToDatabase(object):
    """Process a Pandas Dataframe to a Database"""

    def __init__(self):
        pass

But I have a separate class for the basis in which all 3 subroutines are assembled into one common program.

class ImportDbfToDatabase(object):
    """Import a DBF data to a database"""

    def __init__(self):
        pass

Am I writing the code correctly? Or it is necessary to write a class of the basic program somehow in another way?

Upvotes: 1

Views: 143

Answers (1)

Moses Koledoye
Moses Koledoye

Reputation: 78546

each step of the algorithm is a separate subroutine that can work independently of the other steps

It appears you only need subroutines and don't need to maintain states across any of the conversions. Then, you should be using functions and not classes:

def dbf3_to_csv(dbf3_file):
    """Process a DBF III w/o memo file to a CSV file"""
    ...

def csv_to_dataframe(csv_file):
    """Process a CSV file to Pandas Dataframe"""
    ...

def dataframe_to_database(df):
    """Process a Pandas Dataframe to a Database"""
    ...

In this way, you don't need to setup class instances and you can easily pass the return value from one function directly to the next function in your workflow.

Upvotes: 3

Related Questions