samedi 12 septembre 2020

State Design Pattern application in Python - What to do?

I am trying to apply the State Design Pattern in Python. Follow to code below. The idea is to read the actions in .txt and write the states in anoter .txt.

class Tela():

    def __init__(self, state):
        self.state = state

    def setstatus(self):
        return self.state


def entrada():
    caminho = input('Digite o caminho do arquivo .txt a ser aberto: ')
    return caminho


def arquivosaida():
    caminho = input('Digite o caminho do arquivo .txt a ser escrito: ')
    return caminho


def ler_txt(path):
    with open(path) as file:
        txt = file.readlines()
    return txt


def escrever_txt(path):
    saida = open(path, 'w')
    return saida


def tela(action, tela, saida):

    til = Til(1, 'None', 'None')
    ti = Ti(2, 'None', 'None')
    tl = Tl(3, 'None', 'None')

    if til.state == tela:
        til = Til(1, action, saida)
        til.pfc()
        til.pna()
        til.bd()
        til.pqr()
        til.ad()
        til.bl()

    if ti.state == tela:
        ti = Til(2, action, saida)
        ti.pfc()
        ti.pna()
        ti.bd()
        ti.pqr()
        ti.ad()
        ti.bl()

    if tl.state == tela:
        tl = Til(3, action, saida)
        tl.pfc()
        tl.pna()
        tl.bd()
        tl.pqr()
        tl.ad()
        tl.bl()


class Til():

    def __init__(self, state, action, saida):
        self.state = state
        self.action = action
        self.saida = saida

    def setstatus(self):
        return self.state

    def setinput(self):
        return self.action

    def setsaida(self):
        return self.saida

    def pfc(self):
        if self.action == "PFC" or self.action == "pfc" or self.action == "Pfc" or self.action == "PFc" or self.action == "pfC" or self.action == "pFc":
            self.saida.write('\nNao se aplica!')

    def pna(self):
        if self.action == "PNA" or self.action == "pna" or self.action == "Pna" or self.action == "PNa" or self.action == "pnA" or self.action == "pNa":
            self.saida.write('\nNao se aplica!')

    def bd(self):
        if self.action == "BD" or self.action == "db" or self.action == "Bd" or self.action == "bD":
            self.saida.write('\nNao se aplica!')

    def pqr(self):
        if self.action == "PQR" or self.action == "pqr" or self.action == "Pqr" or self.action == "PQr" or self.action == "pqR" or self.action == "pQr":
            self.saida.write('\nNao se aplica!')

    def ad(self):
        if self.action == "AD" or self.action == "ad" or self.action == "Ad" or self.action == "aD":
            self.saida.write('\nNao se aplica!')

    def bl(self):
        if self.action == "BL" or self.action == "bl" or self.action == "Bl" or self.action == "bL":
            self.saida.write('\nTI')
            t = Tela(2)


class Ti():

    def __init__(self, state, action, saida):
        self.state = state
        self.action = action
        self.saida = saida

    def setstatus(self):
        return self.state

    def setinput(self):
        return self.action

    def setsaida(self):
        return self.saida

    def pfc(self):
        if self.action == "PFC" or self.action == "pfc" or self.action == "Pfc" or self.action == "PFc" or self.action == "pfC" or self.action == "pFc":
            self.saida.write('\nNao se aplica!')

    def pna(self):
        if self.action == "PNA" or self.action == "pna" or self.action == "Pna" or self.action == "PNa" or self.action == "pnA" or self.action == "pNa":
            self.saida.write('\nTIL')
            t = Tela(2)

    def bd(self):
        if self.action == "BD" or self.action == "db" or self.action == "Bd" or self.action == "bD":
            self.saida.write('\nTIL')
            t = Tela(2)

    def pqr(self):
        if self.action == "PQR" or self.action == "pqr" or self.action == "Pqr" or self.action == "PQr" or self.action == "pqR" or self.action == "pQr":
            self.saida.write('\nNao se aplica!')
            t = Tela(2)

    def ad(self):
        if self.action == "AD" or self.action == "ad" or self.action == "Ad" or self.action == "aD":
            self.saida.write('\nTL')
            t = Tela(3)

    def bl(self):
        if self.action == "BL" or self.action == "bl" or self.action == "Bl" or self.action == "bL":
            self.saida.write('\nNao se aplica!')


class Tl():

    def __init__(self, state, action, saida):
        self.state = state
        self.action = action
        self.saida = saida

    def setstatus(self):
        return self.state

    def setinput(self):
        return self.action

    def setsaida(self):
        return self.saida

    def pfc(self):
        if self.action == "PFC" or self.action == "pfc" or self.action == "Pfc" or self.action == "PFc" or self.action == "pfC" or self.action == "pFc":
            self.saida.write('\nTIL')
            t = Tela(2)

    def pna(self):
        if self.action == "PNA" or self.action == "pna" or self.action == "Pna" or self.action == "PNa" or self.action == "pnA" or self.action == "pNa":
            self.saida.write('\nTIL')
            t = Tela(2)

    def bd(self):
        if self.action == "BD" or self.action == "db" or self.action == "Bd" or self.action == "bD":
            self.saida.write('\nTIL')
            t = Tela(2)

    def pqr(self):
        if self.action == "PQR" or self.action == "pqr" or self.action == "Pqr" or self.action == "PQr" or self.action == "pqR" or self.action == "pQr":
            self.saida.write('\nTIL')
            t = Tela(2)

    def ad(self):
        if self.action == "AD" or self.action == "ad" or self.action == "Ad" or self.action == "aD":
            self.saida.write('\nNao se aplica!')

    def bl(self):
        if self.action == "BL" or self.action == "bl" or self.action == "Bl" or self.action == "bL":
            self.saida.write('\nNao se aplica!')

The main file:

from functions_copy import *

arquivo = ler_txt(entrada())

saida = escrever_txt(arquivosaida())

print("Gerando o arquivo...")

t = Tela(1)

for linha in arquivo:
    linha = linha.rstrip('\n')
    tela(linha, t.state, saida)
  
print("Arquivo gerado!")

The input file:

PFC 
Pfc
PNA
PNA
Bd
BD
Bl
BL
PQR
pqr
ad
AD

At the moment, I am having this result in the file output:

Nao se aplica!
Nao se aplica!
Nao se aplica!
Nao se aplica!
Nao se aplica!
Nao se aplica!
TI
TI
Nao se aplica!
Nao se aplica!
Nao se aplica!
Nao se aplica!

The result should be:

Nao se aplica!
Nao se aplica!
Nao se aplica!
Nao se aplica!
Nao se aplica!
Nao se aplica!
TI
Nao se aplica!
TIL
Nao se aplica!
Nao se aplica!
Nao se aplica!

I am trying to reconsider the state of the object "t" reinstating him. I don´t know if it is possible. But I want that the state change from 1 to 2 or to 3. What is happening is that the state is not changing from 1.

Any ideas?

Aucun commentaire:

Enregistrer un commentaire