Skip to content

Commit

Permalink
converters, rn, preço médio
Browse files Browse the repository at this point in the history
  • Loading branch information
kl0ck committed Mar 25, 2021
1 parent 6d4685f commit 8560513
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 18 deletions.
21 changes: 21 additions & 0 deletions converters.py
Original file line number Diff line number Diff line change
@@ -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"))
21 changes: 20 additions & 1 deletion database.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# https://docs.python.org/3/library/sqlite3.html
import sqlite3
import converters

class Database:

Expand Down Expand Up @@ -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
35 changes: 21 additions & 14 deletions leitor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import parsers
from rn import RN
from operacao import Operacao
from database import Database

Expand All @@ -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")

Expand All @@ -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
Expand Down Expand Up @@ -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))
11 changes: 9 additions & 2 deletions operacao.py
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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
27 changes: 27 additions & 0 deletions rn.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 3 additions & 1 deletion teste.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8560513

Please sign in to comment.