一组动态成员的自然命名方案
用户希望实例化一个表示 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对要执行的操作的描述并不完全清楚,但自动命名变量 Something1、Something2 等通常是一个坏主意。请改用列表:
编辑: 好的,我想我明白您的意思,尽管您的解释仍然不太容易阅读。您有一组管道,每个管道都记录了温度和压力的时间序列,对吧?
最简单的方法是使用字典:
或嵌套字典:
或者您可以重写类的
__getattr__
方法,以便它在字典中查找,您可以执行以下操作:编辑 2: 覆盖
__getattr__
看起来有点像这样: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:
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:
Or nested dictionaries:
Or you could override your class'
__getattr__
method, so that it looks in a dictionary, and you can do:Edit 2: Overriding
__getattr__
would look a bit like this: