“for”迭代中的函数,Python

2024-10-05 12:27:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我是个初学者,有点迷恋我写的代码。我正在为一些软件编写一个插件,试图从它的类中提取一些数据。在

这里有一个语句列表,我正在使用operator for执行。我对两种类型的语句感兴趣,分别是VC_STATEMENT_LINMOTION和VC_STATEMENT_PTPMOTION(还有许多其他类型)。 首先,在我的代码中,我看到这两种类型的语句中哪一种先出现。如果它是LinMotion,那么,我将获得这个LinMotion语句的一些提取值(通过转到那个函数(def get_initalvalues()),直到代码“满足”PtpMotion语句。完成后,它将转到def get_conf()函数,从中提取PtpMotion的数据。另外,在get_conf()中,如果满足后面的LinMotion,则从上一次迭代的PtpMotion获取数据。在

但它不起作用。在

我知道这听起来很重)。也许是我自己把它弄得太复杂了,解决办法也更简单了。在

for statement in main_routine.Statements:
    if statement.Type == VC_STATEMENT_LINMOTION or statement.Type == VC_STATEMENT_PTPMOTION:


        def get_conf(*args):
            global output
            if statement.Type == VC_STATEMENT_PTPMOTION:
                ptp_motion_conf = ""
                Extracted string data is assigned to ptp_motion_conf which is later added to overall output

                output += ptp_motion_conf 
            elif statement.Type == VC_STATEMENT_LINMOTION:
                output += ptp_motion_conf #using data of VC_STATEMENT_PTPMOTION from last iteration

        def get_initialconf(*args):
            global output
            if statement.Type == VC_STATEMENT_LINMOTION:
                ...


            elif statement.Type == VC_STATEMENT_PTPMOTION:
                get_conf()        


        statements = [] # getting list of statements to know which of LinMotion or PtpMotion goes first
        for statement in main_routine.Statements:  
            statements.append(statement.Type) 

        if statements.index("LinMotion") < statements.index("PtpMotion"): #statement LinMotion goes first
            get_initialconf()  
        elif statements.index("LinMotion") > statements.index("PtpMotion"):#statement PtpMotion goes first
            get_conf()

我将尝试在示例中显示:

如果LinMotions语句先执行,则在ptp操作之前:

LinMotion(一些特殊的初始数据) LinMotion(仍然是初始数据) ... 在所有其他LinMotion语句中,prog仍然使用初始数据,直到PtpMotion语句出现:

PtpMotion(它自己的PtpMotion数据)

如果LinMotion再次出现,则使用上一个PtpMotion语句中的数据

如果PtpMotion语句先在LinMotion之前,则 PtpMotion(它自己的PtpMotion数据)

如果出现Linmotion,则使用上一个PtpMotion语句中的数据


Tags: 数据outputgetconftype语句statementvc
1条回答
网友
1楼 · 发布于 2024-10-05 12:27:12

编辑:我已经更新了代码,以反映您帖子中的新信息。)

如果我正确地理解了您的问题,一个简单的描述就是:您在main_routine.Statements中有一个Statement实例的列表,并且您希望基于statement.Type执行不同的代码。这是Python代码中通常采用的自然结构。在

# no idea what this is in your code, but since you want to save it
# to a file, I'll blindy assume it is a string.
output = ""

# current stream of statements. accumulate as long as type is compatible.
stream = []

# process each statement.
for statement in main_routine.Statements:

    if statement.Type == VC_STATEMENT_LINMOTION:
        stream.append(statement)

    elif statement.Type == VC_STATEMENT_PTPMOTION:
        if (len(stream) > 0) and (stream[-1].Type == VC_STATEMENT_LINMOTION):
            output += process_linmotion_stream(stream)
            stream = []
            continue
        stream.append(statement)

    # ignore other types of statements.

# after loop, make sure leftover statements are processed.
output += process_linmotion_stream(stream)

老实说,我认为每个人,不仅仅是你自己,都对你的文章感到困惑。切换到对数据建模的简单控制流,这样就可以了。在

相关问题 更多 >

    热门问题