7

I am writing a program that manipulates strings in a file. I want to simply add the literals (strings, such as SUB =X'1D' that assemble into =X'1D' BYTE X'1D') above an ' LTORG' to my testfile.

The problem is I collected the literals above each LTORG as a list inserted them as a list. I would like to insert literals one at a time.

I have this output that is :

[' START 100', " SUB =X'1D'", ' LTORG', '["=X\'1D\' BYTE X\'1D\'"]', ' RESW 
   20', " SUB =X'0259'", " ADD =C'12345'", " MUL =X'4356'", " SUB =X'69'", ' 
   LTORG', '["=X\'0259\' BYTE X\'0259\'", "=C\'12345\' BYTE C\'12345\'", 
   "=X\'4356\' BYTE X\'4356\'", "=X\'69\' BYTE X\'69\'"]', " ADD =C'05'", ' 
   END EXA']
def handle_LTORG(self, testfile):

    myfile.testfile = testfile

    for index, line in enumerate(myfile.testfile):
        line = line.split(" ", 3)
        if len(line) > 2:
            if line[2].startswith("=X") or line[2].startswith("=C"):
                raw_literal = line[2]
                instruction = 'BYTE'
                operand = line[2][1:]
                literal = [raw_literal, instruction, operand]
                literal = ' '.join(literal)
                myfile.literals.append(literal)
        if line[1] == 'LTORG':
            if myfile.literals is not None:
                myfile.testfile.insert(index + 1, str(myfile.literals))
                myfile.literals.pop(0)

The second-last line is mainly producing the issue. It adds the literals collected in a list and inserts them as a packed list rather than one string per line.

I want it to look like this:

[' START 100', " SUB =X'1D'", ' LTORG', '"=X'1D' BYTE X'1D'"', ' RESW 20', " SUB =X'0259'", " ADD =C'12345'", " MUL =X'4356'", " SUB =X'69'", ' LTORG', '"=X'0259' BYTE X'0259'", "=C'12345' BYTE C'12345'", "=X'4356' BYTE X'4356'", "=X'69' BYTE X'69'", " ADD =C'05'", ' END EXA']
petezurich
  • 9,280
  • 9
  • 43
  • 57
asultan904
  • 189
  • 7

3 Answers3

1

i'd try to use something like the top comment here How to make a flat list out of list of lists?

list = ['whatever',['1','2','3'],'3er']
flat_list = []
for member in list:
    if type(member) is list:
        for item in member:
            flat_list.append(item)
    else:
        flat_list.append(member)
ConscriptMR
  • 656
  • 1
  • 7
  • 16
  • This does not answer either of the two problems I have. I don’t want a purely flat list, I need it like the output – asultan904 Mar 31 '19 at 11:38
1

What you're trying to do is a combination of two actions:

  • First, you need to translate all list that are in string literals to actual lists using literal_eval from ast module.

  • Then, you need to flatten the list.

Below is the code demostrating the process:

from ast import literal_eval
inlist = [' START 100', " SUB =X'1D'", ' LTORG', '["=X\'1D\' BYTE X\'1D\'"]', ' RESW    20', " SUB =X'0259'", " ADD =C'12345'", " MUL =X'4356'", " SUB =X'69'", '    LTORG', '["=X\'0259\' BYTE X\'0259\'", "=C\'12345\' BYTE C\'12345\'",    "=X\'4356\' BYTE X\'4356\'", "=X\'69\' BYTE X\'69\'"]', " ADD =C'05'", '    END EXA']
inlist = [literal_eval(elem) if elem[0] == '[' and elem[-1] == ']' else elem for elem in inlist]
outlist = []
for elem in inlist:
     if isinstance(elem,list):
          for item in elem:
               outlist.append(item)
     else:
          outlist.append(elem)
print(outlist)

Output:

[' START 100', " SUB =X'1D'", ' LTORG', "=X'1D' BYTE X'1D'", ' RESW    20', " SUB =X'0259'", " ADD =C'12345'", " MUL =X'4356'", " SUB =X'69'", '    LTORG', "=X'0259' BYTE X'0259'", "=C'12345' BYTE C'12345'", "=X'4356' BYTE X'4356'", "=X'69' BYTE X'69'", " ADD =C'05'", '    END EXA']
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
0

UPDATE: Fixed the issue with a while loop. Feel free to post suggestions!

    def handle_LTORG(self, testfile):

    myfile.testfile = testfile

    for index, line in enumerate(myfile.testfile):
        line = line.split(" ", 3)
        if len(line) > 2:
            if line[2].startswith("=X") or line[2].startswith("=C"):
                raw_literal = line[2]
                instruction = 'BYTE'
                operand = line[2][1:]
                literal = [raw_literal, instruction, operand]
                literal = ' '.join(literal)
                myfile.literals.append(literal)
        if line[1] == 'LTORG':
            if myfile.literals:
                i = 'hi'
                while len(i) > 0:
                    i = myfile.literals[-1]
                    myfile.testfile.insert(index+1, str(i))
                    myfile.literals.pop()
                    if len(myfile.literals) == 0:
                        break

    return myfile.testfile
asultan904
  • 189
  • 7