Functions inside "for" iteration, Python
I am a beginner and kinda lost with the code i have written. I am writing an add-on for some software, trying to extract some data from its classes.
There is a list of statements that i am going through using operator for. I am interested in two types of statements called VC_STATEMENT_LINMOTION and VC_STATEMENT_PTPMOTION (there are many other types as well). First in my code i see which of these two types of statements go first. If it is LinMotion, then, i get some extract values for this LinMotion statement (by going to that function (def get_initalvalues())) till code "meets" PtpMotion statement. After it does it goes to def get_conf() function where data for PtpMotion is extracted. Also, in get_conf() if later LinMotion is met, data is taken from PtpMotion of last iteration.
But it doesnt work.
I know it sounds heavy ))). Maybe i myself made it too complicated and solution is simpler.
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()
i will try to show in sample:
If LinMotions statement goes first, before PtpMotion then:
LinMotion (some special initial data) LinMotion (still initial data) ... in all other LinMotion statements prog still uses initial data until PtpMotion statement appears:
PtpMotion (its own PtpMotion data)
if LinMotion appears again, data from last PtpMotion statement is used
If PtpMotion statement goes fi开发者_Go百科rst, before LinMotion, then PtpMotion (its own PtpMotion data)
If Linmotion appears, data from last PtpMotion statement is used
(Edit: I've updated the code to reflect new information in your post.)
If I understand your problem corrrectly, a simple description of what you're trying to achieve is this: you have a list of Statement
instances in main_routine.Statements
and you want to perform different code based on statement.Type
. Here is the natural structure this usually (read: almost always) takes in Python code.
# 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)
Honestly, I think everybody, not just yourself, is confused with you've written. Switch to a simple control flow that models your data and you should be fine.
精彩评论