[英]How to change code for different method call of a parent/child class structure while using data from parent object
(sorry if my title isn't concise enough) (对不起,如果我的标题不够简洁)
I read data from binary simulation output, do some calculation and create different plots.我从二进制模拟输出中读取数据,进行一些计算并创建不同的图。 The OO code I came up with works, but I'm not satisfied with the style and could use some advise.
我想出的 OO 代码有效,但我对风格不满意,可以使用一些建议。 Here is my (simplified) code so far (find explanations below):
到目前为止,这是我的(简化)代码(在下面找到解释):
import numpy as np
import matplotlib.pyplot as plt
class TR:
def __init__(self):
self.t = np.linspace(0,10,100) # time
self.x = np.sin(self.t) # x data
self.y = np.cos(self.t) # y data
# Base class
class PostProcessor:
def __init__(self, parent):
self.parent = parent
self.calc()
def calc(self):
# Define x_data and y_data in sub class
pass
def write(self):
# Writes public attributes to file
pass
def read(self):
# Reads class specific file
pass
def plot(self):
# By default plot y_data over x_data
fig = plt.figure()
self.ax = fig.add_subplot(1, 1, 1)
self.ax.plot(self.x_data, self.y_data)
class Orbit(PostProcessor):
def calc(self):
self.x_data = self.parent.x
self.y_data = self.parent.y
class Response(PostProcessor):
def calc(self):
self.x_data = self.parent.t
self.y_data = np.array([self.parent.x, self.parent.y]).transpose()
I created a class TR
which is dedicated to one binary file.我创建了一个专用于一个二进制文件的类
TR
。 In my example I just provided the data ( t
, x
, y
) within the __init__
function.在我的示例中,我只是在
__init__
函数中提供了数据( t
、 x
、 y
)。
For each plot (here: xy-Orbit and Response) I want to write to and read from a new file, because the raw data is quite huge and I just need chunks of it.对于每个图(此处:xy-Orbit 和 Response),我想写入和读取一个新文件,因为原始数据非常庞大,我只需要其中的大块。 To avoid duplicated code I created a base class
PostProcessor
which assigns the parent object (the TR
object), provides a write
, read
and plot
function.为了避免重复代码,我创建了一个基类
PostProcessor
,它分配父对象( TR
对象),提供write
、 read
和plot
函数。
This way the sub classes Orbit
and Response
only have to provide x_data and y_data.这样子类
Orbit
和Response
只需要提供 x_data 和 y_data。
This is how I use my code:这是我如何使用我的代码:
obj = TR()
obj.Orbit(obj).plot()
obj.Response(obj).plot()
Does somebody has an idea how to change the code so that I can use it as follows:?有人知道如何更改代码,以便我可以按如下方式使用它:?
obj = TR()
obj.Orbit.plot()
obj.Response.plot()
The problem is that I need to access data from the TR
object and therefore I need to reference it with initialization.问题是我需要从
TR
对象访问数据,因此我需要在初始化时引用它。
I know it is not good that the plot
function relies on the existance of self.x_data
and self.y_data
, but I got some error handling for it.我知道
plot
函数依赖于self.x_data
和self.y_data
的存在并不好,但是我得到了一些错误处理。
You could utilize properties.你可以利用属性。
class TR:
def __init__(self):
<your code>
self._orbit = None
self._response = None
@property
def orbit(self):
if self._orbit is None:
self._orbit = Orbit(self)
return self._orbit
@property
def response(self):
<same as for orbit>
Define classes Orbit()
and Response()
inline, no need to define them within PR and everything should work.定义类
Orbit()
和Response()
内联,不需要在 PR 中定义它们,一切都应该工作。 No changes to classes themselves needed.不需要对类本身进行更改。
Now you can use it like this:现在你可以像这样使用它:
pr = PR()
pr.orbit.plot()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.