如何遍历包含类对象的元组?

时间:2015-10-24 00:07:20

标签: python object tuples

我目前正在编写一段代码,用于自动化我每天使用的工程软件(Orcaflex)流程。我正在使用python与Orcaflex接口以自动运行链接计算。我的代码下面连同输出以供参考。

我的想法是获取Orcaflex模型中的所有对象并迭代该元组对象以提取行名称以将该数据提供给Orcaflex的行向导工具。对象类型是一个元组,但是当我索引该元组时,我得到一个类型(类'OrcFxAPI.OrcaFlexObject')。

我的问题是如何遍历这个包含对象的元组,以便我可以获取行名称字符串?非常感谢任何帮助。

更新:我能够将对象转换为字符串并执行基本的字符串操作来获取行名称。请参阅下面的更新代码。但是,在我的第二篇文章中可以看到从模型中获取行名称的更有效方法,使用点符号来获取行名称和类型。

我的更新代码:

# Created by: Brian Weber
# Created on: 09/15/2015

# This script will load a base file and then calculate for
# different tensions using the line setup wizard.

# Note that buoys are modeled as clump attachments on the line with a global offset for
# pennant wire.

import OrcFxAPI
import numpy as np

def convert_MT_to_kN(value_in_MT):
    g = 9.80665002864
    value_in_kN = value_in_MT * g
    return value_in_kN

g = 9.80665002864

# Load file name
filename = 'CX15-Anchor Plan.dat'
model = OrcFxAPI.Model(filename)

# Pipe tensions to be solved for; units are in MT then converted to kN for Orcaflex
min_tension = float(raw_input("\n Please enter the minimum tension to calculate catenary in MT: "))
max_tension = float(raw_input("\n Please enter the maximum tension to calculate catenary in MT: "))
tension_increment = float(raw_input("\n Please enter the tension increment to calculate catenary in MT: "))
pipe_tensions = (np.arange(min_tension, max_tension, tension_increment) * g).tolist()

# Grab lines in model
lines = []
objects = model.objects # returns a tuple
# Convert objects to strings
for o in objects:
    o_string = repr(o)
    if "Line:" in o_string:
        string_split = o_string.split("'")
        lines.append(string_split[1])

# Solve all lines in model for all line tensions
print('\nSolving line tensions...')
for tension in pipe_tensions:
    print('\nLine tension being solved for is {:.2f} kN.\n').format(tension)
    for line in lines:
        model.general.LineSetupCalculationMode = 'Calculate Line Lengths'
        model[line].LineSetupIncluded = 'Yes'
        # model.general.LineSetupMinDamping = 5
        # model.general.LineSetupMaxDamping = 20
        # model.general.LineSetupMaxIterations = 50
        model[line].LineSetupTargetVariable = 'Tension'
        model[line].LineSetupTargetValue = tension
        model[line].LineSetupLineEnd = 'End A'
        print('Line {} set.').format(line)
    print('\nInvoking Line Setup Wizard...')
    model.InvokeLineSetupWizard()   
    print('\nLine Setup Wizard done.')
    # Calculate static position of mooring lines and buoys
    model.CalculateStatics()
    # Save static simulation and load into new object
    model.SaveSimulation(('{} - {:.2f} kN.sim').format(filename, tension))

print('\nLine Wizard has completed!')

2 个答案:

答案 0 :(得分:0)

我实际上找到了另一种更有效的方法来从Orcaflex模型中获取线名称。由于对象返回类对象的元组,因此可以使用点表示法来获取类型( line.type ,它返回一个整数)和名称( line.name ,它返回一个string)使用以下代码:

import OrcFxAPI
import numpy as np

g = 9.80665002864

# Load file name
filename = 'CX15-Anchor Plan.dat'
model = OrcFxAPI.Model(filename)

# Pipe tensions to be solved for; units are in MT then converted to kN for Orcaflex
min_tension = float(raw_input("\n Please enter the minimum tension to calculate catenary in MT: "))
max_tension = float(raw_input("\n Please enter the maximum tension to calculate catenary in MT: "))
tension_increment = float(raw_input("\n Please enter the tension increment to calculate catenary in MT: "))
pipe_tensions = (np.arange(min_tension, max_tension, tension_increment) * g).tolist()

# Grab lines in model
objects = model.objects # returns a tuple
lines = [obj for obj in objects if obj.type == OrcFxAPI.otLine] # All line objects in model

# Solve all lines in model for all line tensions
print('\nSolving line tensions...')
for tension in pipe_tensions:
    print('\nLine tension being solved for is {:.2f} kN.\n').format(tension)
    for line in lines:
        model.general.LineSetupCalculationMode = 'Calculate Line Lengths'
        model[line.name].LineSetupIncluded = 'Yes'
        model[line.name].LineSetupTargetVariable = 'Tension'
        model[line.name].LineSetupTargetValue = tension
        model[line.name].LineSetupLineEnd = 'End A'
        print('Line {} set.').format(line.name)
    print('\nInvoking Line Setup Wizard...')
    model.InvokeLineSetupWizard()   
    print('\nLine Setup Wizard done.')
    # Calculate static position of mooring lines and buoys
    model.CalculateStatics()
    # Save static simulation and load into new object
    model.SaveSimulation(('{} - {:.2f} kN.sim').format(filename, tension))

print('\nLine Wizard has completed!')

答案 1 :(得分:-1)

你得到的元组中对象的内容是什么?你知道这个对象在元组中的位置吗?那些对象有你想要的属性吗?举个例子,如果对象是元组中的第二个元素,你可以这样做:

if(window.performance != undefined) {
  if(window.performance.timing != undefined) {
    if(window.performance.timing.toJSON != undefined) {
      /* etc */
    }
  }
}

您可以在示例中提供对象的示例内容变量吗? (通过打印)