From a25d328499e5e5f78894dbeed52e2b76a38d235f Mon Sep 17 00:00:00 2001 From: Mauro Scomparin Date: Wed, 18 Jul 2018 21:59:25 +0200 Subject: [PATCH] added some comments --- money/money.py | 144 +++++++++++++++++++++++++++++++++++++- money/tests/test_money.py | 2 +- 2 files changed, 142 insertions(+), 4 deletions(-) diff --git a/money/money.py b/money/money.py index 4cd3727..55ebfc9 100755 --- a/money/money.py +++ b/money/money.py @@ -25,6 +25,24 @@ DESCRIPTION_FIELD_NAME = _('description') def create_input_message(field_name, field_format, field_default_value): + """Returns a message to request user input. + + Parameters + ---------- + field_name : str + The name of the field to read. + field_format : + The format to show as an example to the user. + field_default_value : + The default value used if the user does not specify a value. + + Returns + ------- + + str + The formatted string to use for user input. + + """ return field_name + " (" + field_format + ") [" + field_default_value + "]: " @@ -42,6 +60,16 @@ def write_movement_to_file(path, m): def write_movement_to_stream(fileStream, movement): + """Formats and writes a movement to a stream. + + Parameters + ---------- + fileStream + An output stream to write the formatted movement to. + movement : type + The movement to write. + + """ fileStream.write(movement[TYPE_FIELD_NAME] + movement[VALUE_FIELD_NAME]) fileStream.write(';') fileStream.write(movement[DATE_FIELD_NAME]) @@ -113,6 +141,19 @@ def get_data_file(config): def load_movements_from_stream(data_file): + """Returns an array of movements read from a stream. + + Parameters + ---------- + data_file + A stream to read the data from. + + Returns + ------- + array + An array of movements read from the stream. + + """ movements = [] for line in data_file: split_line = line.split(';') @@ -139,6 +180,11 @@ def insert_new_movement(data_file): def read_new_movement(): + """Reads and writes a new movement from the userr to the dataFile. + + Called from money-insert. + + """ default_config = default_configuration() resulting_config = None if file_exists(CONFIG_FILE_LOCATION): @@ -152,6 +198,11 @@ def read_new_movement(): def latest_movements(): + """Writes the latest movements formatted. + + Called by money-latest + + """ default_config = default_configuration() resulting_config = None if file_exists(CONFIG_FILE_LOCATION): @@ -184,6 +235,21 @@ def configuration_from_file(file_path): def default_date(date): + """Returns the passed date as a formatted string. + + Otherwise it returns the current date. + + Parameters + ---------- + date + The date to format. + + Returns + ------- + str + The formatted string of the date. + + """ try: return strptime(date, '%d/%m/%Y') except ValueError: @@ -191,6 +257,21 @@ def default_date(date): def default_hour(hour): + """Returs the passed hour as a formatted string. + + Otherwise returns the current time. + + Parameters + ---------- + hour + The hour to format. + + Returns + ------- + str + A string with the hour correctly formatted. + + """ try: return strptime(hour, '%H:%M') except ValueError: @@ -198,6 +279,19 @@ def default_hour(hour): def sort_movements(movements): + """Sorts the movements passed by date and hour. + + Parameters + ---------- + movements : array + movements to order. + + Returns + ------- + array + An array of sorted movements. + + """ return sorted( movements, key=lambda x: ( @@ -209,10 +303,31 @@ def sort_movements(movements): def default_configuration(): + """Returns the default configuration. + + Returns + ------- + object + The default configuration. + + """ return {DATA_FILE_NAME: 'movements.dat', LAST_MOVEMENT_NUMBER: 5} def last_movement_number_message(n): + """Formats the number of movements to show to the user. + + Parameters + ---------- + n : int + The number to show to the user. + + Returns + ------- + str + A string to show to the user with the number of movements shown. + + """ return _('latest {} movements:').format(n) @@ -221,14 +336,37 @@ def value_message(str, value): def format_movement(movement): - return '|{0: >10}|{1: <10}|{2: <5}|{3: <18}|'.format( - movement[VALUE_FIELD_NAME][:10], + """Formats a movement to be visualized. + + Parameters + ---------- + movement : object + The movement to format. + + Returns + ------- + str + A movement formatted for output. + + """ + return '|{0: >8}|{1: <10}|{2: <5}|{3: <20}|'.format( + movement[VALUE_FIELD_NAME][:8], movement[DATE_FIELD_NAME][:10], movement[HOUR_FIELD_NAME][:5], - movement[DESCRIPTION_FIELD_NAME][:18]) + movement[DESCRIPTION_FIELD_NAME][:20]) def write_latest_movements(data_file, conf): + """Helper function to write the latest formatted movements. + + Parameters + ---------- + data_file + input file. + conf + program configuration. + + """ movements = load_file(data_file) ordered_movements = sort_movements(movements) actual_value = Decimal('0') diff --git a/money/tests/test_money.py b/money/tests/test_money.py index 909c703..f96a609 100644 --- a/money/tests/test_money.py +++ b/money/tests/test_money.py @@ -21,7 +21,7 @@ class MoneyTest(unittest.TestCase): 10), "latest 10 movements:") def test_format_movement(self): - self.assertEqual('| -100.00|18/04/2017|00:01|testings stuffffff|', + self.assertEqual('| -100.00|18/04/2017|00:01|testings stuffffff s|', money.format_movement({ money.VALUE_FIELD_NAME: '-100.00', money.DATE_FIELD_NAME: '18/04/2017', -- 2.25.1