Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何遍历包含类对象的元组?_Python_Object_Tuples - Fatal编程技术网

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

Python 如何遍历包含类对象的元组?,python,object,tuples,Python,Object,Tuples,我目前正在编写一段代码,它将自动化我每天使用的工程软件(Orcaflex)的过程。我使用python与Orcaflex接口,自动运行悬链线计算。下面是我的代码和输出,以供参考 我的想法是获取Orcaflex模型中的所有对象,并迭代该元组对象以提取行名称,从而将数据输入Orcaflex的行向导工具。objects类型是一个元组,但当我索引该元组时,返回的类型是(类'OrcFxAPI.OrcaFlexObject') 我的问题是如何迭代这个包含对象的元组,以便获得行名字符串?任何帮助都将不胜感激 更

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

我的想法是获取Orcaflex模型中的所有对象,并迭代该元组对象以提取行名称,从而将数据输入Orcaflex的行向导工具。objects类型是一个元组,但当我索引该元组时,返回的类型是(类'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!')

返回的元组中对象的内容是什么?你知道这个对象在元组中的位置吗?这些对象是否具有您想要的属性?例如,如果对象是元组中的第二个元素,则可以执行以下操作:

objects[1].<<func_name_you_are_interested_in>>()
or 
objects[1].<<property_name_you_are_interested_in>>
对象[1]。()
或
对象[1]。

您能否在示例中给出对象变量的示例内容?(通过打印)

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

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!')

如果您单击我的原始帖子中的最后一个链接,就会看到对象变量输出的图片和对象变量索引的类型。@JTurk:使用注释提问。您介意解释否决票吗?至少我知道我做错了什么@弗拉门戈:你在说什么?你是在这个问题上否决我的人吗?@JTurk这正是我需要的。我能够将对象转换为字符串,告诉字符串中是否有“Line:”,然后拆分单引号以获取行名。谢谢!!