一组动态成员的自然命名方案

发布于 2024-10-15 04:17:52 字数 570 浏览 0 评论 0原文

用户希望实例化一个表示 transeint 的类,并自动访问所表示的每个变量(最多 200 个变量)的成员项。变量类实例集将是基于基于文件的输入数据的动态,并且期望使用文件提供的变量名称来创建可通过自然命名方案访问的这些变量实例的集合。实际上,变量类隐藏了数据存储位置和独立变量(即时间)存储位置的详细信息。以下伪代码表示最终用户可能表达的随机行。在某些情况下,后处理可能会更加广泛。

tran1 = CTransient('TranData', ...)
Padj = tran1.pressPipe1 + 10  # add 10 bar to a pressure for conservatism
Tsat = TsatRoutine( tran1.tempPipe1 )
MyPlotRoutine( tran1.tempPipe1, tran1.tempPipe2 )

其中输入数据文件中定义的 pressPipeX 和 tempPipeX 名称以及相应的 numpy 数据向量在“TranData”文件输入文件中指定,并且是 CVariable 类的实例。

有关如何动态构建表示瞬态变量的实例集以便可以访问它们的帮助将不胜感激。

The desire is for the user to instantiate a class that represents the transeint along with automatic access to a member item for each variable being represented (up to 200 variables). The set of variable class instances would be dynamic based on file based input data and the desire is to use the file provided variable names to create a collection of these variable instances that are accessible with a natural naming scheme. Effectively, the variable class hides the details of where the data is stored and the indepedent variable (ie, time) is stored. The following pseudo code expresses random lines that the end user may express. In some cases, the post processing may be much more extensive.

tran1 = CTransient('TranData', ...)
Padj = tran1.pressPipe1 + 10  # add 10 bar to a pressure for conservatism
Tsat = TsatRoutine( tran1.tempPipe1 )
MyPlotRoutine( tran1.tempPipe1, tran1.tempPipe2 )

where pressPipeX and tempPipeX names defined in the input data files and the corresponding numpy data vectors are specified in the 'TranData' file input file and are instances of a CVariable class.

Help on how to dynamically build the set of instances that represent the transient variables such that they can be accessed would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然#的苍凉 2024-10-22 04:17:52

您对要执行的操作的描述并不完全清楚,但自动命名变量 Something1、Something2 等通常是一个坏主意。请改用列表:

transientvariables = []
transientvariables.append(makenewtransientvariable())
# ...
for tv in transientvariables:
    print tv

编辑: 好的,我想我明白您的意思,尽管您的解释仍然不太容易阅读。您有一组管道,每个管道都记录了温度和压力的时间序列,对吧?

最简单的方法是使用字典:

transients["tempPipe1"]

或嵌套字典:

transients["temp"]["Pipe1"]

或者您可以重写类的 __getattr__ 方法,以便它在字典中查找,您可以执行以下操作:

transients.tempPipe1

编辑 2: 覆盖 __getattr__ 看起来有点像这样:

def __getattr__(self, name):
    if name in self.varMap:
        return self.varMap[name]
    raise AttributeError

Your description of what you're trying to do isn't entirely clear, but automatically naming variables something1, something2, etc. are generally a bad idea. Use a list instead:

transientvariables = []
transientvariables.append(makenewtransientvariable())
# ...
for tv in transientvariables:
    print tv

Edit: OK, I think I see what you're getting at, although your explanation still isn't exactly easy to read. You have a collection of pipes, with a time series of temperature and pressure recorded for each one, right?

The easiest way would be to use a dictionary:

transients["tempPipe1"]

Or nested dictionaries:

transients["temp"]["Pipe1"]

Or you could override your class' __getattr__ method, so that it looks in a dictionary, and you can do:

transients.tempPipe1

Edit 2: Overriding __getattr__ would look a bit like this:

def __getattr__(self, name):
    if name in self.varMap:
        return self.varMap[name]
    raise AttributeError
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文