diff --git a/converters.py b/converters.py new file mode 100644 index 0000000..23fc2ab --- /dev/null +++ b/converters.py @@ -0,0 +1,21 @@ +from operacao import Operacao +from decimal import Decimal + +def toOperacao(tuple): + return Operacao(tuple[0], tuple[1], tuple[2], tuple[3], tuple[4], tuple[5], tuple[6]) + +def toDateYMD(dataDMY): + a = str(dataDMY).split('/') + y = a[2] + m = a[1] + d = a[0] + return y + '-' + m + '-' + d + +def toDecimal(str): + if (',' in str and '.' in str): + raise Exception("Número inválido: [" + str + "]. Use vírgula para decimais.") + str = str.replace(',', '.') + return Decimal(str) + +#print(toDateYMD("23/01/2021")) +#print(toNumber("1.024,68")) diff --git a/database.py b/database.py index 31d8581..85d56c4 100644 --- a/database.py +++ b/database.py @@ -1,5 +1,6 @@ # https://docs.python.org/3/library/sqlite3.html import sqlite3 +import converters class Database: @@ -30,5 +31,23 @@ def list(self): rows = c.fetchall() + #for r in rows: + # print(r) + + return rows + + def listarPorTicker(self, ticker): + con = self.con + c = con.cursor() + + c.execute("SELECT * FROM operacao WHERE ticker = ? ORDER BY data", (ticker,)) + + rows = c.fetchall() + + i = 0 for r in rows: - print(r) + rows[i] = converters.toOperacao(r) + #print(rows[i]) + i+=1 + + return rows diff --git a/leitor.py b/leitor.py index c085d8d..db46c2c 100644 --- a/leitor.py +++ b/leitor.py @@ -1,5 +1,6 @@ import sys import parsers +from rn import RN from operacao import Operacao from database import Database @@ -15,10 +16,17 @@ print("Lendo arquivo " + arquivo + "...") print() -f = open(arquivo, "r", encoding="utf-8") - -lines = f.readlines() -#print(*lines) +lines = None +try: + f = open(arquivo, "r", encoding="UTF-8") + lines = f.readlines() + #print(*lines) +except UnicodeDecodeError: + f = open(arquivo, "r", encoding="ISO-8859-1") + lines = f.readlines() +except: + print("Não foi possível ler o arquivo [" + arquivo + "].") + sys.exit(0) print("Extraindo operações...\n") @@ -45,7 +53,7 @@ def isEmBranco(txt): line = line.strip() i+=1 - print(str(i).zfill(3), line) + print(str(i).zfill(4), line) if isEmBranco(line): continue @@ -77,21 +85,20 @@ def isEmBranco(txt): print("Preço inválido na linha", str(i) + ":", line) sys.exit(0) - operacoes.append(Operacao(data, cv, qtd, ticker, preco, "")) + operacoes.append(Operacao(None, data, cv, qtd, ticker, preco, "")) print() print("Leitura do arquivo " + arquivo + " concluída.") print() -print("Operações: ") -print() +#print("Operações: ") +#print() -for o in operacoes: - print(o) +#for o in operacoes: +# print(o) print() -db = Database() -for o in operacoes: - db.add(o) - +rn = RN(operacoes) +pm = rn.precoMedio("B3SA3", "C") +print("Preço médio = " + str(pm)) diff --git a/operacao.py b/operacao.py index 35bfd84..5224f31 100644 --- a/operacao.py +++ b/operacao.py @@ -1,6 +1,7 @@ class Operacao: - def __init__(self, data, cv, qtd, ticker, preco, nota): + def __init__(self, id, data, cv, qtd, ticker, preco, nota): + self.id = id self.data = data if isinstance(data, str) else data[0] self.cv = cv if isinstance(cv, str) else cv[0] self.qtd = qtd if isinstance(qtd, str) else qtd[0] @@ -9,4 +10,10 @@ def __init__(self, data, cv, qtd, ticker, preco, nota): self.nota = nota if isinstance(nota, str) else nota[0] def __str__(self): - return str(self.data) + " " + str(self.cv) + " " + str(self.qtd) + " " + str(self.ticker) + " " + str(self.preco) + " " + str(self.nota) + return str(self.id).zfill(4) + " " + str(self.data) + " " + str(self.cv) + " " + str(self.qtd) + " " + str(self.ticker) + " " + str(self.preco) + " " + str(self.nota) + + def isCompra(self): + return True if self.cv.upper() == 'C' else False + + def isVenda(self): + return True if self.cv.upper() == 'V' else False diff --git a/rn.py b/rn.py new file mode 100644 index 0000000..7475e7a --- /dev/null +++ b/rn.py @@ -0,0 +1,27 @@ +from database import Database +import converters + +class RN: + + db = None + + def __init__(self, operacoes): + self.db = Database() + for o in operacoes: + self.db.add(o) + + def precoMedio(self, ticker, cv): + db = self.db + l = db.listarPorTicker(ticker) + + somatorio = 0 + + for o in l: + if (o.isCompra() and cv.upper() == 'C'): + somatorio += converters.toDecimal(o.preco) + print(o) + elif (o.isVenda() and cv.upper() == 'V'): + somatorio += converters.toDecimal(o.preco) + print(o) + + return round(somatorio / l.__len__(), 2) diff --git a/teste.txt b/teste.txt index 1025109..bb66f6a 100644 --- a/teste.txt +++ b/teste.txt @@ -1,5 +1,7 @@ # um comentário + +01/02/2021 C 25 B3SA3 50,00 # Clear +01/03/2021 C 25 B3SA3 51,00 # Clear 17/03/2021 C 50 B3SA3 52,68 # Clear 18/03/2021 V 100 LEVE3 19,24 # Mirae - 18/03/2021 V 100 LEVE3 19,24 # Mirae