HOUR_FIELD_NAME = 'hour'
DESCRIPTION_FIELD_NAME = 'description'
-def scrivi_movimento(path, m):
+def write_movement(path, m):
with open(path, 'a') as f:
f.write(m[TYPE_FIELD_NAME] + m[VALUE_FIELD_NAME])
f.write(';')
return
-def leggi_tipo():
+def read_type():
t = 'n'
while not (t == '' or t == '+' or t == '-'):
t = input('type (+/-) [-]: ')
return t
-def leggi_valore():
+def read_value():
v = ''
while v == '':
v = input('value (#####.##) []: ')
return v
-def leggi_data():
- d = input('data (DD/MM/YYYY) [today]: ')
+def read_date():
+ d = input('date (DD/MM/YYYY) [today]: ')
if d == '':
d = strftime("%d/%m/%Y", localtime())
return d
-def leggi_ora():
- o = input('hour (HH:MM) [now]: ')
- if o == '':
- o = strftime('%H:%M', localtime())
- return o
+def read_hour():
+ h = input('hour (HH:MM) [now]: ')
+ if h == '':
+ h = strftime('%H:%M', localtime())
+ return h
-def leggi_descrizione():
+def read_description():
d = input('description () []: ')
return d
-def leggi_movimento():
- tipo = leggi_tipo()
- valore = leggi_valore()
- data = leggi_data()
- ora = leggi_ora()
- descrizione = leggi_descrizione()
+def read_movement():
+ type = read_type()
+ value = read_value()
+ date = read_date()
+ hour = read_hour()
+ description = read_description()
m = {
- TYPE_FIELD_NAME: tipo,
- VALUE_FIELD_NAME: valore,
- DATE_FIELD_NAME: data,
- HOUR_FIELD_NAME: ora,
- DESCRIPTION_FIELD_NAME: descrizione
+ TYPE_FIELD_NAME: type,
+ VALUE_FIELD_NAME: value,
+ DATE_FIELD_NAME: date,
+ HOUR_FIELD_NAME: hour,
+ DESCRIPTION_FIELD_NAME: description
}
return m
-def get_file_dati(config):
- file_dati = abspath(config[DATA_FILE_NAME])
- print('file dati:', file_dati)
- return file_dati
+def get_data_file(config):
+ data_file = abspath(config[DATA_FILE_NAME])
+ print('data file:', data_file)
+ return data_file
-def carica_file(f):
- dati = []
- with open(f, "r") as df:
- for l in df:
- spl = l.split(';')
- d = {
- VALUE_FIELD_NAME: spl[0],
- DATE_FIELD_NAME: spl[1],
- HOUR_FIELD_NAME: spl[2],
- DESCRIPTION_FIELD_NAME: spl[3]
+def load_file(file):
+ movements = []
+ with open(file, "r") as data_file:
+ for line in data_file:
+ split_line = l.split(';')
+ movement = {
+ VALUE_FIELD_NAME: split_line[0],
+ DATE_FIELD_NAME: split_line[1],
+ HOUR_FIELD_NAME: split_line[2],
+ DESCRIPTION_FIELD_NAME: split_line[3]
}
- dati.append(d)
- return dati
+ movements.append(movement)
+ return movements
-def inserimento(file_dati):
- m = leggi_movimento()
- scrivi_movimento(file_dati, m)
+def insert_new_movement(data_file):
+ movement = read_movement()
+ write_movement(data_file, movement)
-def inserimento_dati():
+def read_new_movement():
default_config = default_configuration()
resulting_config = None
if file_exists(CONFIG_FILE_LOCATION):
default_config, config_from_file)
else:
resulting_config = default_config
- file_dati = get_file_dati(resulting_config)
- inserimento(file_dati)
+ data_file = get_data_file(resulting_config)
+ insert_new_movement(data_file)
-def riassunto_dati():
+def latest_movements():
default_config = default_configuration()
resulting_config = None
if file_exists(CONFIG_FILE_LOCATION):
default_config, config_from_file)
else:
resulting_config = default_config
- file_dati = get_file_dati(resulting_config)
- riassunto(file_dati, resulting_config)
+ data_file = get_data_file(resulting_config)
+ write_latest_movements(data_file, resulting_config)
def override_default_configuration(default_conf, other_conf):
return json.load(fp)
-def data_default(data):
+def default_date(date):
try:
- return strptime(data, '%d/%m/%Y')
+ return strptime(date, '%d/%m/%Y')
except ValueError:
return gmtime(0)
-def ora_default(ora):
+def default_hour(hour):
try:
- return strptime(ora, '%H:%M')
+ return strptime(hour, '%H:%M')
except ValueError:
return gmtime(0)
-def ordina(dati):
+def sort_movements(movements):
return sorted(
- dati,
+ movements,
key=lambda x: (
- data_default(x[DATE_FIELD_NAME]),
- ora_default(x[HOUR_FIELD_NAME])
+ default_date(x[DATE_FIELD_NAME]),
+ default_hour(x[HOUR_FIELD_NAME])
),
reverse=True
)
def default_configuration():
- return {DATA_FILE_NAME: 'movimenti.dat', LAST_MOVEMENT_NUMBER: 5}
+ return {DATA_FILE_NAME: 'movements.dat', LAST_MOVEMENT_NUMBER: 5}
def last_movement_number_message(n):
return 'latest {} movements:'.format(n)
-def riassunto(file_dati, conf):
- dati = carica_file(file_dati)
- dati_ordinati = ordina(dati)
- val_attuale = Decimal('0')
- spese_tot = Decimal('0')
- guadagni_tot = Decimal('0')
- for d in dati:
- m = Decimal(d[VALUE_FIELD_NAME])
- val_attuale = val_attuale + m
- if m > Decimal('0'):
- guadagni_tot = guadagni_tot + m
+def write_latest_movements(data_file, conf):
+ movements = load_file(data_file)
+ ordered_movements = sort_movements(movements)
+ actual_value = Decimal('0')
+ total_expenses = Decimal('0')
+ total_incomes = Decimal('0')
+ for movement in ordered_movements:
+ movement_value = Decimal(movement[VALUE_FIELD_NAME])
+ actual_value = actual_value + movement_value
+ if movement_value > Decimal('0'):
+ total_incomes = total_incomes + movement_value
else:
- spese_tot = spese_tot + m
- print('actual value:', str(val_attuale))
- print('total incomes:', str(guadagni_tot))
- print('total expenses:', str(spese_tot))
+ total_expenses = total_expenses + movement_value
+ print('actual value:', str(actual_value))
+ print('total incomes:', str(total_incomes))
+ print('total expenses:', str(total_expenses))
print(last_movement_number_message(conf[LAST_MOVEMENT_NUMBER]))
for i in range(conf[LAST_MOVEMENT_NUMBER]):
- if i < len(dati_ordinati):
- print(dati_ordinati[i])
+ if i < len(ordered_movements):
+ print(ordered_movements[i])