1

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

And how best to implement the workflow of such a process? That is, will it be necessary to implement another method for combining these 3 methods? Then the input file will be fed to this method. Or is it better to put the whole process in __init__ a method that will call these 3 methods in a certain sequence? Then the input file will be fed into the instance of the class. How is this best done? What is worth taking into account?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.