Skip to content

10. Practical Exercise: Version 2

Image

This new version introduces the following [config.py] file:


def configure():
    import os

    # absolute path to this script's directory
    script_dir = os.path.dirname(os.path.abspath(__file__))

    # root directory from which certain relative paths will be measured
    root_dir = "C:/Data/st-2020/dev/python/cours-2020/python3-flask-2020/impots"

    # application dependencies
    absolute_dependencies = [
        f"{root_dir}/v01/shared",
    ]

    # application configuration
    config = {
        # absolute path to the taxpayers file
        "taxpayersFilename": f"{script_dir}/../data/taxpayersdata.txt",
        # absolute path to the results file
        "resultsFilename": f"{script_dir}/../data/results.txt"
    }

    # update the syspath
    from myutils import set_syspath
    set_syspath(absolute_dependencies)

    # restore the config
    return config

Comments

  • line 5: We retrieve the absolute path of the folder containing the script being executed, in this case the [config.py] script. This gives us the absolute path of the [main] folder. This is also the folder containing the main script [main.py];
  • line 8: when a referenced file does not belong to the application folder, we will not use [script_dir] to locate it but [root_dir]. This line must be changed as soon as the application moves to a different location in the file system;
  • lines 11–13: we list the absolute paths of all the folders that must be in the Python Path for the application to work. On line 12, we reference the [shared] folder from version 1 of the application exercise;
  • lines 16–21: define the application’s configuration in a [config] dictionary. Here, we specify the absolute paths of the text files handled by the application. To do this, we use [script_dir], which, as a reminder, refers to the [main] folder here;
  • Lines 24–25: We set the Python path required by the application;

The main script [main.py] is as follows:


# configure the application
import config

config = config.configure()

# The syspath is configured—we can now import modules
from tax_module_01 import tax_calculation, record_results, get_taxpayers_data

# Taxpayers file
taxpayers_filename = config['taxpayersFilename']
# results file
results_filename = config['resultsFilename']

# code
try:

    # read taxpayer data
    taxpayers = get_taxpayers_data(taxpayers_filename)
    # list of results
    results = []
    # Calculate taxpayers' taxes
    for taxpayer in taxpayers:
        # the tax calculation returns a dictionary of keys
        # ['married', 'children', 'salary', 'tax', 'surcharge', 'rebate', 'deduction', 'rate']
        result = calculate_tax(taxpayer['married'], taxpayer['children'], taxpayer['salary'])
        # the dictionary is added to the list of results
        results.append(result)
    # We save the results
    save_results(results_filename, results)
except BaseException as error:
    # There may be various errors: file not found, incorrect file content
    # display the error and exit the application
    print(f"The following error occurred: {error}]\n")
finally:
    print("Job completed...")

Comments

  • lines 1–4: the application is configured;
  • line 7: we know that after configuration, the Python Path is correct and includes the [shared] folder, which contains the [impôts_module_01] script. From this script, we import the functions we need;
  • lines 9-12: the names of the files used are found in the configuration. These are absolute paths;
  • lines 14–35: this is the code from version 1;

Version 1 did not work in a Python console. In the same console, version 2 produces the following results:

(venv) C:\Data\st-2020\dev\python\cours-2020\python3-flask-2020\impots\v02>python main/main.py
Work completed...

No errors are encountered now.