(Not a profi programmer here, mine is probably a dumb question)
I've a database stored in several ascii files (one file per table) within the same directory. I'm used to read all files and load each of them in Pandas DataFrame. Eventually I bundle all the dataframes in a single dict variable. Reading the files from disk takes about 1s.
The DB has several sections describing different groups of objects, each one with their configuration parameters:
DB
|
+-TC
| +-PAR
+-TM
| +-PAR
+-SEQ
+-PAR
I want to define a module having classes and methods to load, browse and manipulate the data and I'm wondering which is the best approach with the constrain to read the db from disc only once.
Option 1 [preferred]: have a class/subclass structure mimic the DB organisation.
The desired usage would be something like:
db = DB()
dbdata= db.load(dir)
print( db.version())
tcName = DB.TC('tcName')
print(tcName.description)
tcName.PAR('param1').setvalue(value1)
module:
class DB:
def __init__(self):
def load(self, dbDir):
self.db = read_all_db_files_from_dbDir
return self.db
def version(self):
class TC:
def __init__(self, tcName):
self.tcName = tcName
def description(self)
def parametes(self)
class PAR:
def __init__(self, tcParName):
self.parName = tcParName
def description(self)
def setvalue(self, value)
[...]
class SEQ:
[...definitions similar to TC...]
class PAR
def __init__(self, seqParName):
self.parName = seqParName
def description(self)
def setvalue(self, value)
Option 2: flat class structure.
class DB:
def load(self, dbDir):
self.db = read_all_db_files_from_dbDir
return self.db
def version(self):
class TC:
def __init__(self, tcName, dbData):
self.tcName = tcName
self.dbData = dbData
def description(self)
def parametes(self)
class TCPAR:
def __init__(self, tcParName, dbData):
self.tcParName = tcParName
self.dbData = dbData
def description(self)
def setvalue(self, value)
[... and so on also for the SEQ and SEQPAR classes]
In this case I suppose I need to pass the db variable when calling each method:
db = DB()
dbData = db.load(dir)
tcName = TC('tcName',dbData)
print(tcName.description)
param1 = TCPAR('param1',dbData)
param1.setvalue(value1)
Option 1 question: how can I pass the variable self.db to the subclasses without reloading the db every time I use a subclass/method?
Option 2 question: any other way to share the variable dbData among the classes?