I'm creating a custom class to store information about a CFD simulation results.
Right now the way it is set up is that it instantiates an empty class object, then used a method called load_mesh which calls an external function to read all the information about the mesh, and return a dictionary of all the information. The load_mesh method then assigns a bunch of class attributes from the values in the dictionary.
The problem is that I am planning to store alot more information than just the mesh, and I dont want to have like 1000 attributes to my class object. I want to store then in appropriate containers(?) that each have their own methods.
For example, my code looks like this currently (some stuff omitted that's unnecessary):
class CFD():
def __init__(self, infile=None):
self.file = infile
def load_mesh(self):
mesh = load_cfd_mesh(self) #calls outside function to load mesh info, uses self.file, returns dict
self.proj = mesh['proj']
self.static_items = mesh['static_items']
self.nnodes = mesh['nnodes']
self.node_coords = mesh['node_coords']
self.node_codes = mesh['node_codes']
self.nelements = mesh['nelements']
self.element_types = mesh['element_types_str']
self.node_connectivity = mesh['node_connectivity']
self.element_node_ids = mesh['element_node_ids']
self.element_coords = mesh['element_coords']
self.element_elevs = mesh['element_elevs']
self.horizontal_units = mesh['horizontal_units']
self.vertical_units = mesh['vertical_units']
test = CFD('testfile.txt') #instantiate
test.load_mesh() #load mesh information to attributes
Now, I can access any of the mesh information by doing:
test.proj
self.nnodes
self.coords
etc...
But want I want to do is store all of this information in test.mesh, where test.mesh has all of these attributes but also has the method test.mesh.load().
I THINK I can do something like this:
class CFD():
def __init__(self, infile=None):
self.file = infile
self.mesh = None
def load_mesh(self):
mesh = load_cfd_mesh(self) #calls outside function to load mesh info, uses self.file, returns dict
setattr(self.mesh, 'proj', mesh['proj'])
#etc....
then I'd be able to do:
test = CFD('testfile.txt') #instantiate
test.load_mesh() #load mesh information to attributes
test.mesh.proj
But I can't figure out how to add the load_mesh method to self.mesh?
How is it possible to achieve the following way of doing this:
test = CFD('testfile.txt') #instantiate
test.mesh.load() #load mesh information to attributes
test.mesh.proj
Do I have to define another class within the main class? Like class mesh(self):
Also, if my proposed way of adding attributes to self.mesh doesn't make sense..please help!
mydata.some_keyseems a tiny bit nicer thanmydata['some_key'].self.meshhow can I add theloadmethod to it, the main question is adding a methodclass CFD():is fine, if a little odd looking.self.mesh['proj']if I want, but how can I load the mesh info by callingself.mesh.load()