Home > Software design >  Iterate through 2 files at once performing actions back and forth between the two
Iterate through 2 files at once performing actions back and forth between the two

Time:12-03

Im kind of confused on how to implement this in python, I need to be able to iterate through 6 total files, but 2 files at a time, and read a line from one file, do what that line does, then switch over to read a line from the other file and do what it says, and then vice versa iterating down both files, here is what the files would look like:

START,T1        
READ,y,B3
EXECUTE,y 1 
WRITE,y,B3
READ,z,C1
READ,x,A3
EXECUTE,z=z x   
EXECUTE,x 2 
EXECUTE,x 2 
WRITE,x,A3
WRITE,z,C1
READ,y,B3
EXECUTE,y 8 
READ,z,B2
EXECUTE,z 2 
WRITE,z,B2
WRITE,y,B3
READ,x,A1
EXECUTE,x 3 
WRITE,x,A1
COMMIT      
END,T1      

and here is my current code which can parse them:

class Database:
    def __init__(self):
        self.file = self.readFile()
        self.temp_db = None
        self.y = 0
        self.x = 0
        self.z = 0

    def readFile(self):
        df = pd.read_csv('database.txt', sep=',', engine='python')
        df = df.iloc[: , 1:]
        df.index = df.index   1
        return df

    def parseTransaction(self, fileName, num, transaction):
        # if(self.temp_db == None):
        #     self.temp_db = self.file
            
        with open(fileName, 'r') as t1_file:
            for line in t1_file:
                split_line = line.split(',')

                with open('schedule' num '.txt', 'a') as the_file:
                        the_file.write('Transaction '   transaction   ': '   split_line[0]   '\n')

                if(split_line[0] == "START"):
                    continue
                elif(split_line[0] == "READ"):
                    self.read(self.temp_db, split_line[1], split_line[2])
                elif(split_line[0] == "EXECUTE"):
                    self.execute(split_line[1])
                elif(split_line[0] == "WRITE"):
                    self.write(self.temp_db, split_line[1], split_line[2])
                elif(split_line[0] == "COMMIT"): #Doesnt ever hit true?
                    continue 
                elif(split_line[0] == "END"):
                    break
                

    def t1(self, num):
        self.parseTransaction('t1.txt', num, '1')

    def t2(self, num):
        self.parseTransaction('t2.txt', num, '2')

    def t3(self, num):
        self.parseTransaction('t3.txt', num, '3')

    def t4(self, num):
        self.parseTransaction('t4.txt', num, '4')

    def t5(self, num):
        self.parseTransaction('t5.txt', num, '5')

    def t6(self, num):
        self.parseTransaction('t6.txt', num, '6')

    def serializeTransaction(self):
        self.temp_db = self.file
        self.t1('1')
        self.t2('1')
        self.t3('1')
        self.t4('1')
        self.t5('1')
        self.t6('1')
        self.temp_db.to_csv(r'C:\Users\laner\Desktop\program 3\database1.txt', sep=',')
        self.temp_db = None

    def interleave(self):
        self.temp_db = self.file

        self.temp_db.to_csv(r'C:\Users\laner\Desktop\program 3\database2.txt', sep=',')

    def read(self, db, var, indentfier):
        row = indentfier[1]
        col = indentfier[0]
        res = db.at[int(row),col]
        if(var == 'y'):
            self.y = res
        elif(var == 'x'):
            self.x = res
        elif(var == 'z'):
            self.z = res

    def execute(self, expression):
        varCount = expression.count(expression[0])
        var = None
        varName = None
        if(varCount == 1):
            for char in expression:
                if(char == 'y'):
                    var = self.y
                    varName = 'y'
                elif(char == 'x'):
                    var = self.x
                    varName = 'x'
                elif(char == 'z'):
                    var = self.z
                    varName = 'z'
                elif(char == ' '):
                    continue
                else:
                    try:
                        var  = int(char)
                    except:
                        pass
            if(varName == 'y'):
                self.y = int(var)
            elif(varName == 'x'):
                self.x = int(var)
            elif(varName == 'z'):
                self.z = int(var)
        elif(varCount == 2):
            for char in expression:
                if(char == 'y' and varName == None):
                    var = self.y
                    varName = 'y'
                elif(char == 'x' and varName == None):
                    var = self.x
                    varName = 'x'
                elif(char == 'z' and varName == None):
                    var = self.z
                    varName = 'z'
                elif(char == varName or char == ' '):
                    continue
                elif(char == 'y' and varName != None):
                    var  = self.y
                elif(char == 'x' and varName != None):
                    var  = self.x
                elif(char == 'z' and varName != None):
                    var  = self.z 
            if(varName == 'y'):
                self.y = int(var)
            elif(varName == 'x'):
                self.x = int(var)
            elif(varName == 'z'):
                self.z = int(var)

    def write(self, db, var, identifier):
        row = identifier[1]
        col = identifier[0]
        if(var == 'y'):
            db.at[int(row),col] = self.y
            self.y = 0
        elif(var == 'x'):
            db.at[int(row),col] = self.x
            self.x = 0
        elif(var == 'z'):
            db.at[int(row),col] = self.z
            self.z = 0
            

t1-t6 function is for reading the specified files in, so t1 is what i pasted above and t2 looks similar like this:

START,T2        
READ,y,A2
EXECUTE,y 2 
WRITE,y,A2
READ,z,B2
READ,x,A3
EXECUTE,x 5 
WRITE,x,A3
EXECUTE,z 2 
WRITE,z,B2
READ,y,C1
EXECUTE,y 4 
READ,z,A1
EXECUTE,z 10    
WRITE,z,A1
WRITE,y,C1
READ,x,A3
EXECUTE,x=x y   
WRITE,x,A3
COMMIT      
END,T2  

there are 6 of these files in total, so thats where im at, parsing through 2 files at once one file at a time and cycling between the two?

CodePudding user response:

In general, you can interleave two streams using itertools.chain and zip. (A file-like object can be treated as a stream of str values.)

>>> from itertools import chain
>>> list(chain.from_iterable(zip([1,2,3], "abc")))
[1, 'a', 2, 'b', 3, 'c']

This extends to an arbitrary number of iterables in a straight word manner, by simply passing each iterable as a separate argument to zip. You can define a function to wrap this:

def interleave(*args):
    yield from chain.from_iterable(zip(*args))

for x in interleave("abc", "def", "ghi"):
  print(x)

outputs

a
d
g
b
e
h
c
f
i
  • Related