From 3e9e92d31c9a6b8c124b2f48013a0b54761b183b Mon Sep 17 00:00:00 2001 From: Mauro Scomparin Date: Tue, 10 Jul 2018 21:40:18 +0200 Subject: [PATCH] changed variables and function names --- money/money.py | 146 +++++++++++++++++++------------------- money/tests/test_money.py | 4 +- setup.py | 6 +- 3 files changed, 78 insertions(+), 78 deletions(-) diff --git a/money/money.py b/money/money.py index 5ed8d53..292aeb4 100755 --- a/money/money.py +++ b/money/money.py @@ -15,7 +15,7 @@ DATE_FIELD_NAME = 'date' 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(';') @@ -28,7 +28,7 @@ def scrivi_movimento(path, m): return -def leggi_tipo(): +def read_type(): t = 'n' while not (t == '' or t == '+' or t == '-'): t = input('type (+/-) [-]: ') @@ -39,75 +39,75 @@ def leggi_tipo(): 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): @@ -116,11 +116,11 @@ def inserimento_dati(): 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): @@ -129,8 +129,8 @@ def riassunto_dati(): 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): @@ -152,54 +152,54 @@ def configuration_from_file(file_path): 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]) diff --git a/money/tests/test_money.py b/money/tests/test_money.py index 1cad1f2..063de4d 100644 --- a/money/tests/test_money.py +++ b/money/tests/test_money.py @@ -40,7 +40,7 @@ class MoneyTest(unittest.TestCase): self.assertTrue(money.LAST_MOVEMENT_NUMBER in res) self.assertEqual(res[money.LAST_MOVEMENT_NUMBER], 10) - def test_ordina_ore(self): + def test_sort_movements(self): date = [ { 'data': '18/04/2017', @@ -63,7 +63,7 @@ class MoneyTest(unittest.TestCase): 'prog': 4, }, ] - res = money.ordina(date) + res = money.sort_movements(date) self.assertEqual(res[0]['prog'], 2) self.assertEqual(res[1]['prog'], 1) self.assertEqual(res[2]['prog'], 3) diff --git a/setup.py b/setup.py index 772a0e9..0ddf6af 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ with open(path.join(here, 'README.rst'), encoding = 'utf-8') as f: setup( name = 'money', version = '1.0.1', - description = 'gestisce spese e entrate', + description = 'Money handling program', long_description = long_description, url = 'http://code.scompo.it:3000/scompo/money', author = 'Mauro Scomparin', @@ -19,8 +19,8 @@ setup( packages=find_packages(exclude=['contrib', 'docs', 'tests', 'utils', 'scripts']), entry_points={ 'console_scripts' : [ - 'inserisci=money.money:inserimento_dati', - 'riassunto=money.money:riassunto_dati', + 'money-insert=money.money:read_new_movement', + 'money-latest=money.money:latest_movements', ], }, ) -- 2.25.1