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']
2
  • What is the type of testfile? (And hence, myfile.testfile?) Commented Mar 31, 2019 at 7:52
  • Additionally, why not if myfile.literals: instead of if myfile.literals is not None:? Commented Mar 31, 2019 at 7:57

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

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
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']

2 Comments

What happened to the other two literals after the second ‘ LTORG’ ? I need them as separate commo separated strings please
@A.Sultan edited my answer. Now it should return the correct result.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.