What is the best way to store nested data in a python class? -


i have simple python program meant operations on c source code. extract header files #include in .c file , store them, take function declarations .h files , store too, , take variables functions.

the problem is, how can nest these class keep them linked? i.e. .c files contain lists of .h files, .h files contain lists of function declarations, function declarations contains lists of variables.

so far i've got is:

class c_file:     file_count = 0      def __init__(self, file_name):         self.file_name = file_name         c_file.file_count += 1      def headerfile(self, header_name, functions):         self.header_name = header_name         self.functions = []         x in functions:             self.variables.append(x) 

i'm not sure if dictionaries better option? or need class @ all? (this first python program, i'm used working in c)

make object, , each object acts container sub-objects. when instantiate class, it's responsible parsing object find children.

so, create c_file, iterates on contents of files , creates h_file objects. when create h_file, iterates on contents , creates funcs. when create func iterates on contents create vars. , on.

for example:

class c_file(object):     def __init__(self, filename):         self.headers = []         header in self._get_headers(filename):             self.headers.append(h_file(header))  class h_file(object):     def __init__(self, header):         self.funcs = []         func in self._get_funcs(header):             self.funcs.append(func(func))  class func(object):     def __init__(self, func):         self.vars = []         var in self._get_vars(func):             self.vars.append(var(var)) 

then, main program has loop on list of filenames, creating c_files:

def main():     c_files = []     file in os.path.listdir(sys.argv[1]):         c_files.append(c_file(file)) 

as each object created starts chain of events find children, grandchildren , on.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -