-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcredicoop_precargadas.py
115 lines (89 loc) · 2.89 KB
/
credicoop_precargadas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
import io
import csv
from datetime import datetime
from decimal import Decimal
def _date(value):
v = value.strip()
return datetime.strptime(v, '%d/%m/%Y').date()
def _string(value):
return value.strip()
def _card_string(value):
return value.strip('-')
def _amount(value):
value = value.replace('.', '')
return Decimal(value.replace(',', '.'))
RECEIVER = {
'receiver': (4, _string),
'card_number': (9, _card_string),
}
PERIOD = {
'date_from': (4, _date),
'date_to': (9, _date),
}
MOVE = {
'date': (1, _date),
'op_number': (2, _string),
'name': (3, _string),
'description1': (4, _string),
'description2': (6, _string),
'debit': (8, _amount),
'credit': (12, _amount),
}
class Precargadas(object):
def __init__(self, name, encoding='windows-1252'):
self.statements = []
if isinstance(name, (bytes, str)):
with io.open(name, encoding=encoding, mode='r') as f:
self._parse(f)
else:
self._parse(name)
def _parse(self, f):
statement = Statement()
self.statements.append(statement)
csv_reader = csv.reader(f, delimiter=',')
line = 0
debit_total = 0
credit_total = 0
for row in csv_reader:
line += 1
if line in range(1, 5):
continue
elif line == 5:
self._parse_statement(row, statement, RECEIVER)
elif line == 6:
self._parse_statement(row, statement, PERIOD)
elif line in range(7, 9):
continue
else:
if row[1] == '':
continue
move = Move()
self._parse_move(row, move, MOVE)
if row[8]:
debit_total += _amount(row[8])
if row[12]:
credit_total += _amount(row[12])
statement.moves.append(move)
statement.debit_total = debit_total
statement.credit_total = credit_total
return
def _parse_statement(self, row, statement, desc):
for name, (col, parser) in desc.items():
value = parser(row[col])
setattr(statement, name, value)
def _parse_move(self, row, move, desc):
for name, (col, parser) in desc.items():
value = parser(row[col])
setattr(move, name, value)
class Statement(object):
__slots__ = list(RECEIVER.keys()) + list(PERIOD.keys()) + [
'debit_total', 'credit_total', 'moves']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.moves = []
class Move(object):
__slots__ = list(MOVE.keys())
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)