1

(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?

Januz
  • 31
  • 2
  • What are you actually trying to accomplish here? It may make more sense to have one `DB` object that reads through the entire file and creates your Pandas df and dictionary all in an internal method, instead of setting up things like triple-nested classes which are almost certainly more work than you need. – jdaz Jul 22 '20 at 19:37

1 Answers1

0

You coud make the variables global with the global tag:

global var_global
var_global = 'test'

It would look something like this in a class with subclasses:

class test:
    def __init__(self, var):
        global var_global
        var_global = var

    class sub:
        def __init__(self):
            print('Output:', var_global)


tst = test('test')
tst.sub()

Output:

Output: test

Process finished with exit code 0

I think there are much better solutions than this one but i think it fits your idea. :)