Suggestions to better understand when and how is convenient to use OPP.
This question is a follow-up question on this post.
I have a script that reads an input file. In time, the script may be modified to account for new elements, that are directly added in the same input file. I would like to better understand how OOP paradigm can help to simplify this updating procedure.
The code is written based on the suggestions from @Peilonrayz.
Input file:
SIMPLY SUPPORTED BEAM NNODES<NNnodes><Node,X,Y> 2 1,0,0 2,1,0 SECTIONS<NSec><Sec,Area,Inertia,Depth,ShearCF> 1 1,100000,1,1,0
from __future__ import annotations from dataclasses import dataclass @dataclass class Mesh: title: str nnode: NNode sections: Section @classmethod def load(cls, file): return cls( file.readline(), NNode.load(file), Section.load(file) ) _NNODE_KEYS = ['ID', 'x', 'y'] @dataclass class NNode: nodes: List[dict] @classmethod def load(cls, file): file.seek(0) while not file.readline().startswith('NNODES'): continue amount = int(file.readline()) values = [] for node in range(amount): values.append(dict(zip( _NNODE_KEYS, file.readline().split(',') ))) return cls(values) _SECTIONS_KEYS = ['Sec', 'Area', 'Inertia','Depth','ShearCF'] @dataclass class Section : sections: List[dict] @classmethod def load(cls, file): file.seek(0) while not file.readline().startswith('SECTIONS'): continue amount = int(file.readline()) values = [] for node in range(amount): values.append(dict(zip( _SECTIONS_KEYS, file.readline().split(',') ))) return cls(values)
Next, if the script is modified to handle more data, one can use inheritance.
SIMPLY SUPPORTED BEAM NNODES<NNnodes><Node,X,Y> 2 1,0,0 2,1,0 SECTIONS<NSec><Sec,Area,Inertia,Depth,ShearCF> 1 1,100000,1,1,0 MATERIALS<NMat><Mat,Young,Poisson,Thermal,Weight> 1 1,30000000,0.3,0,0
from __future__ import annotations from dataclasses import dataclass @dataclass class Mesh: title: str nnode: NNode sections: Section @classmethod def load(cls, file): return cls( file.readline(), NNode.load(file), Section.load(file) ) _NNODE_KEYS = ['ID', 'x', 'y'] @dataclass class NNode: nodes: List[dict] @classmethod def load(cls, file): file.seek(0) while not file.readline().startswith('NNODES'): continue amount = int(file.readline()) values = [] for node in range(amount): values.append(dict(zip( _NNODE_KEYS, file.readline().split(',') ))) return cls(values) _SECTIONS_KEYS = ['Sec', 'Area', 'Inertia','Depth','ShearCF'] @dataclass class Section : sections: List[dict] @classmethod def load(cls, file): file.seek(0) while not file.readline().startswith('SECTIONS'): continue amount = int(file.readline()) values = [] for node in range(amount): values.append(dict(zip( _SECTIONS_KEYS, file.readline().split(',') ))) return cls(values) @dataclass class Mesh_mat(Mesh): materials: Material @classmethod def load(cls, file): return cls( file.readline(), NNode.load(file), Section.load(file), Material.load(file) ) _MATERIAL_KEYS = ['Mat', 'Young', 'Poisson','Thermal','Weight'] @dataclass class Material : materials: List[dict] @classmethod def load(cls, file): file.seek(0) while not file.readline().startswith('MATERIALS'): continue amount = int(file.readline()) values = [] for node in range(amount): values.append(dict(zip( _MATERIAL_KEYS, file.readline().split(',') ))) return cls(values)
Anyway, I don't see any convenience in using this approach instead of simply modify the Mesh
class.
from __future__ import annotations from dataclasses import dataclass @dataclass class Mesh: title: str nnode: NNode sections: Section materials: Material @classmethod def load(cls, file): return cls( file.readline(), NNode.load(file), Section.load(file), Material.load(file) ) _NNODE_KEYS = ['ID', 'x', 'y'] @dataclass class NNode: nodes: List[dict] @classmethod def load(cls, file): file.seek(0) while not file.readline().startswith('NNODES'): continue amount = int(file.readline()) values = [] for node in range(amount): values.append(dict(zip( _NNODE_KEYS, file.readline().split(',') ))) return cls(values) _SECTIONS_KEYS = ['Sec', 'Area', 'Inertia','Depth','ShearCF'] @dataclass class Section : sections: List[dict] @classmethod def load(cls, file): file.seek(0) while not file.readline().startswith('SECTIONS'): continue amount = int(file.readline()) values = [] for node in range(amount): values.append(dict(zip( _SECTIONS_KEYS, file.readline().split(',') ))) return cls(values) _MATERIAL_KEYS = ['Mat', 'Young', 'Poisson','Thermal','Weight'] @dataclass class Material : materials: List[dict] @classmethod def load(cls, file): file.seek(0) while not file.readline().startswith('MATERIALS'): continue amount = int(file.readline()) values = [] for node in range(amount): values.append(dict(zip( _MATERIAL_KEYS, file.readline().split(',') ))) return cls(values)
Since I don't have experience in OPP programming, which of these two approaches is better, if a different person who writes the code is asked to add modifications(like handles more data) ?
The code can be further improved as @Peilonrayz said in the first question (while cycle and controls over the input data) but I would stay focused only on OOP improvements