I have the following python code:
a=open("example.nx",'r').read()
b=a.replace("\n","").replace(" ","").split("|")
for i in b:
if (len(i)==2 or len(i)==1) and i.__contains__("}") or i.__contains__(")"):
"""doesn't belong is probably a closing bracket"""
index=b.index(i)
b[index-1]=b[index-1]+b[index]#add the closing brackets to the one before deleting it
del b[index]
"""check for brackets opened that aren't closed"""
if (i.__contains__("(") and not i.__contains__(")")) or (i.__contains__("{") and not i.__contains__("}")):
#these are not closed
index=b.index(i)
if b[index+1].__contains__(")") or b[index+1].__contains__("}"):
#add them together then delete the extra
b[index]=b[index]+b[index+1]
del b[index+1]
del b[len(b)-1]
print(b)
And I have the file example.nx
:
bp myClass(
myClass[arg1]{
set myArg to arg1|
}
)|
def myFunction[arg2,arg3]{
set a to arg2|
set b to arg3|
}|
set var1 to 3|
set var2 to 4|
myFunction(var1,var2)|
I get the output of:
['bp myClass(myClass[arg1]{set myArg to arg1})', 'def myFunction[arg2,arg3]{set a to arg2', 'set b to arg3}', 'set var1 to 3', 'set var2 to 4myFunction(var1,var2)']
I was expecting to get(Look at last elements):
['bp myClass(myClass[arg1]{set myArg to arg1})', 'def myFunction[arg2,arg3]{set a to arg2', 'set b to arg3}', 'set var1 to 3', 'set var2 to 4','myFunction(var1,var2)']
I have added breakpoints and have broken the issue down to this block:
if (len(i)==2 or len(i)==1) and i.__contains__("}") or i.__contains__(")"):
"""doesn't belong is probably a closing bracket"""
index=b.index(i)
b[index-1]=b[index-1]+b[index]#add the closing brackets to the one before deleting it
del b[index]
I don't know why this is happening because at the end of the list where the 2 elements merge they don't fit the length requirements so they shouldn't even set off the if statement.
You are missing parenthesis in the first if:
txt = """
bp myClass(
myClass[arg1]{
set myArg to arg1|
}
)|
def myFunction[arg2,arg3]{
set a to arg2|
set b to arg3|
}|
set var1 to 3|
set var2 to 4|
myFunction(var1,var2)|
"""
tokens = txt.replace('\n', '').replace(" ","").split("|")
for token in tokens:
if (len(token) == 2 or len(token) == 1) and ('}' in token or ')' in token):
# these are missing ^ ^
# doesn't belong is probably a closing bracket
i = tokens.index(token)
tokens[i - 1] = tokens[i-1] + tokens[i] # add the closing brackets to the one before deleting it
del tokens[i]
# check for brackets opened that aren't closed
if ('(' in token and ')' not in token) or ('{' in token and '}' not in token):
# these are not closed
i = tokens.index(token)
if ')' in tokens[i+1] or '}' in tokens[i+1]:
# add them together then delete the extra
tokens[i] = tokens[i] + tokens[i+1]
del tokens[i+1]
del tokens[len(tokens)-1]
print(tokens)
I've also changed your variables around, a
and b
are not good variable names. i
can be a good variable name, if it is used as an integer index into a list.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments