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 + "]: "
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])
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(';')
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):
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):
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:
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:
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: (
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)
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')