From cd3ac0d9ff0cc91d4bb75f56ae927db2cacde47b Mon Sep 17 00:00:00 2001 From: Mauro Scomparin Date: Wed, 19 Apr 2017 02:07:59 +0200 Subject: [PATCH] temporaneamente --- .gitignore | 4 +- README.md => README.rst | 3 +- bilancio.py | 44 ------------ money.py | 78 --------------------- money/__init__.py | 0 money/money.py | 139 ++++++++++++++++++++++++++++++++++++++ money/tests/__init__.py | 0 money/tests/test_money.py | 42 ++++++++++++ scripts/shell.sh | 5 ++ scripts/tests.sh | 5 ++ setup.py | 26 +++++++ 11 files changed, 222 insertions(+), 124 deletions(-) rename README.md => README.rst (81%) delete mode 100755 bilancio.py delete mode 100755 money.py create mode 100644 money/__init__.py create mode 100755 money/money.py create mode 100644 money/tests/__init__.py create mode 100644 money/tests/test_money.py create mode 100755 scripts/shell.sh create mode 100755 scripts/tests.sh create mode 100644 setup.py diff --git a/.gitignore b/.gitignore index 600d2d3..07645f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.vscode \ No newline at end of file +.vscode +__pycache__ +*.egg-info \ No newline at end of file diff --git a/README.md b/README.rst similarity index 81% rename from README.md rename to README.rst index 5de0f20..744401b 100644 --- a/README.md +++ b/README.rst @@ -1,3 +1,4 @@ -# money +money +===== python script I run on my phone to log my expenses. diff --git a/bilancio.py b/bilancio.py deleted file mode 100755 index c237f0b..0000000 --- a/bilancio.py +++ /dev/null @@ -1,44 +0,0 @@ -from time import localtime, strftime -from os.path import expanduser, join -from pprint import pprint -from decimal import * - -def file_output(): - home = expanduser('~') - out_file_name = 'movimenti.dat' - out_file = join(home, 'dati', out_file_name) - return out_file - -def carica_file(f): - dati = [] - with open(f, "r") as df: - for l in df: - spl = l.split(';') - d = { - 'valore' : spl[0], - 'data' : spl[1], - 'ora' : spl[2], - 'descrizione' : spl[3] - } - dati.append(d) - return dati - -print("bilancio 0.0.1") -out_file = file_output() -print('file:', out_file) -dati = carica_file(out_file) -val_attuale = Decimal('0') -spese_tot = Decimal('0') -guadagni_tot = Decimal('0') -for d in dati: - m = Decimal(d['valore']) - val_attuale = val_attuale + m - if m > Decimal('0'): - guadagni_tot = guadagni_tot + m - else: - spese_tot = spese_tot + m -print('valore attuale:', str(val_attuale)) -print('guadagni complessivi:', str(guadagni_tot)) -print('spese complessive:', str(spese_tot)) -print('grazie.') - diff --git a/money.py b/money.py deleted file mode 100755 index 343a4c0..0000000 --- a/money.py +++ /dev/null @@ -1,78 +0,0 @@ -from time import localtime, strftime -from os.path import expanduser, join -from pprint import pprint - -def leggi_tipo(): - t = 'n' - while not (t == '' or t == '+' or t == '-'): - t = input('tipo (+/-) [-]: ') - if t == '': - t='-' - elif t == '+': - t='' - return t - -def leggi_valore(): - v = '' - while v == '': - v = input('valore (#####.##) []: ') - return v - -def leggi_data(): - d = input('data (DD/MM/YYYY) [oggi]: ') - if d == '': - d = strftime("%d/%m/%Y", localtime()) - return d - -def leggi_ora(): - o = input('ora (HH:MM) [adesso]: ') - if o == '': - o = strftime('%H:%M', localtime()) - return o - -def leggi_descrizione(): - d = input('descrizione () []: ') - return d - -def leggi_movimento(): - tipo = leggi_tipo() - valore = leggi_valore() - data = leggi_data() - ora = leggi_ora() - descrizione = leggi_descrizione() - m = { - 'tipo' : tipo, - 'valore' : valore, - 'data' : data, - 'ora' : ora, - 'descrizione': descrizione - } - return m - -def scrivi_movimento(path, m): - with open(path, 'a') as f: - f.write(m['tipo'] + m['valore']) - f.write(';') - f.write(m['data']) - f.write(';') - f.write(m['ora']) - f.write(';') - f.write(m['descrizione']) - f.write('\n') - return - -def file_output(): - home = expanduser('~') - out_file_name = 'movimenti.dat' - out_file = join(home, 'dati', out_file_name) - return out_file - -print("money 0.0.1") -out_file = file_output() -print('file output:', out_file) -m = leggi_movimento() -scrivi_movimento(out_file, m) -print('scritto:') -pprint(m) -print('grazie.') - diff --git a/money/__init__.py b/money/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/money/money.py b/money/money.py new file mode 100755 index 0000000..6a1497a --- /dev/null +++ b/money/money.py @@ -0,0 +1,139 @@ +from time import localtime, gmtime, strftime, strptime +from os.path import expanduser, join +from pprint import pprint +from decimal import * + +def scrivi_movimento(path, m): + with open(path, 'a') as f: + f.write(m['tipo'] + m['valore']) + f.write(';') + f.write(m['data']) + f.write(';') + f.write(m['ora']) + f.write(';') + f.write(m['descrizione']) + f.write('\n') + return + +def leggi_tipo(): + t = 'n' + while not (t == '' or t == '+' or t == '-'): + t = input('tipo (+/-) [-]: ') + if t == '': + t='-' + elif t == '+': + t='' + return t + +def leggi_valore(): + v = '' + while v == '': + v = input('valore (#####.##) []: ') + return v + +def leggi_data(): + d = input('data (DD/MM/YYYY) [oggi]: ') + if d == '': + d = strftime("%d/%m/%Y", localtime()) + return d + +def leggi_ora(): + o = input('ora (HH:MM) [adesso]: ') + if o == '': + o = strftime('%H:%M', localtime()) + return o + +def leggi_descrizione(): + d = input('descrizione () []: ') + return d + +def leggi_movimento(): + tipo = leggi_tipo() + valore = leggi_valore() + data = leggi_data() + ora = leggi_ora() + descrizione = leggi_descrizione() + m = { + 'tipo' : tipo, + 'valore' : valore, + 'data' : data, + 'ora' : ora, + 'descrizione': descrizione + } + return m + +def get_file_dati(): + home = expanduser('~') + nome_file_dati = 'movimenti.dat' + file_dati = join(home, 'dati', nome_file_dati) + print('file dati:', file_dati) + return file_dati + +def carica_file(f): + dati = [] + with open(f, "r") as df: + for l in df: + spl = l.split(';') + d = { + 'valore' : spl[0], + 'data' : spl[1], + 'ora' : spl[2], + 'descrizione' : spl[3] + } + dati.append(d) + return dati + +def inserimento(file_dati): + m = leggi_movimento() + scrivi_movimento(file_dati, m) + +def inserimento_dati(): + file_dati = get_file_dati() + inserimento(file_dati) + +def riassunto_dati(): + file_dati = get_file_dati() + riassunto(file_dati) + +def data_default(data): + try: + return strptime(data, '%d/%m/%Y') + except ValueError: + return gmtime(0) + +def ora_default(ora): + try: + return strptime(ora, '%H:%M') + except ValueError: + return gmtime(0) + +def ordina(dati): + return sorted( + dati, + key = lambda x: ( + data_default(x['data']), + ora_default(x['ora']) + ), + reverse = True + ) + +def riassunto(file_dati): + 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['valore']) + val_attuale = val_attuale + m + if m > Decimal('0'): + guadagni_tot = guadagni_tot + m + else: + spese_tot = spese_tot + m + print('valore attuale:', str(val_attuale)) + print('guadagni complessivi:', str(guadagni_tot)) + print('spese complessive:', str(spese_tot)) + print('ultimi 5 movimenti:') + for i in range(5): + if i < len(dati_ordinati): + print(dati_ordinati[i]) diff --git a/money/tests/__init__.py b/money/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/money/tests/test_money.py b/money/tests/test_money.py new file mode 100644 index 0000000..482a69f --- /dev/null +++ b/money/tests/test_money.py @@ -0,0 +1,42 @@ +import unittest +from money import money + +class UselessTest(unittest.TestCase): + + def test_passa_sempre(self): + self.assertTrue(True, msg='se fallisce son problemi') + + def test_ordina_ore(self): + date = [ + { + 'data' : '18/04/2017', + 'ora' : '00:01', + 'prog' : 1, + }, + { + 'data' : '19/04/2017', + 'ora' : '00:01', + 'prog' : 2, + }, + { + 'data' : '17/04/2017', + 'ora' : '', + 'prog' : 3, + }, + { + 'data' : '', + 'ora' : '00:01', + 'prog' : 4, + }, + ] + res = money.ordina(date) + self.assertEqual(res[0]['prog'], 2) + self.assertEqual(res[1]['prog'], 1) + self.assertEqual(res[2]['prog'], 3) + self.assertEqual(res[3]['prog'], 4) + +def main(): + unittest.main() + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/scripts/shell.sh b/scripts/shell.sh new file mode 100755 index 0000000..ad00272 --- /dev/null +++ b/scripts/shell.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +# shell.sh + +docker run -it --rm -v $(pwd):/money -w /money python:3 /bin/bash \ No newline at end of file diff --git a/scripts/tests.sh b/scripts/tests.sh new file mode 100755 index 0000000..f412227 --- /dev/null +++ b/scripts/tests.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +# tests.sh + +docker run -it --rm -v $(pwd):/money -w /money python:3 python3 -m unittest -v \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..edf3f89 --- /dev/null +++ b/setup.py @@ -0,0 +1,26 @@ +from setuptools import setup, find_packages +from codecs import open +from os import path + +here = path.abspath(path.dirname(__file__)) + +with open(path.join(here, 'README.rst'), encoding = 'utf-8') as f: + long_description = f.read() + +setup( + name = 'money', + version = '0.0.1', + description = 'gestisce spese e entrate', + long_description = long_description, + url = 'https://github.com/scompo/money', + author = 'Mauro Scomparin', + author_email = 'scompo@gmail.com', + license = 'BSD', + packages=find_packages(exclude=['contrib', 'docs', 'tests', 'utils', 'scripts']), + entry_points={ + 'console_scripts' : [ + 'inserisci=money.money:inserimento_dati', + 'riassunto=money.money:riassunto_dati', + ], + }, +) \ No newline at end of file -- 2.25.1