<jupyter_start><jupyter_text>How How to import code from python filesGiven a `` file in a directory any other python process in the samedirectory can import that file as it would a normal library.````{tip}```import ```````At that stage it is possible to uses any python object (a `function`, a `class`, a`variable`) by referring to the `` as library:```.function.class.variable```See the {ref}`modularisation_tutorial` for examples of this. How to break up code in to modular componentsWhen modularising code aim to identify specific components of the code that canbe isolated from the rest.In practice this means writing multiple functions that use the correct inputsand outputs in chain for an overall goal.Often this allows us to write a more comprehensive docstring that explainsspecific parts of the implemented process.As an example, consider the problem of wanting to pay a shared bill afterapplying a tip, the following function will do this:<jupyter_code>def add_tip_and_get_bill_share(total, tip_proportion, number_of_payers):
    """
    This returns the share of a bill to be paid by `number_of_payers`
    ensuring the total paid includes a tip.
    """
    tip_amount = tip_proportion * total
    total += tip_amount
    return total / number_of_payers<jupyter_output><empty_output><jupyter_text>We can check that this works:<jupyter_code>add_tip_and_get_bill_share(total=100, tip_proportion=0.2, number_of_payers=6)<jupyter_output><empty_output><jupyter_text>An improvement of the above would be:<jupyter_code>def add_tip(total, tip_proportion):
    """
    This adds the given proportion to a bill total.

    Note that tip_proportion is a number between 0 and 1. A tip_proportion of 0
    corresponds to no tip and a tip_proportion of 1 corresponds to paying the
    total twice.
    """
    tip_amount = tip_proportion * total
    return total + tip_amount


def get_bill_share(total, number_of_payers):
    """
    This returns the share of a bill by dividing the total by the number of
    payers.
    """
    return total / number_of_payers<jupyter_output><empty_output><jupyter_text>Then to use the above we would be able to explicitly write out each step whichensures that there is clarity in what is being done:<jupyter_code>total = add_tip(total=100, tip_proportion=0.2)
get_bill_share(total=total, number_of_payers=6)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06573957016434892, 'fraction_numerical': 0.0067425200168563, 'mean_word_length': 5.025380710659898, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '604646', 'license': 'MIT', 'n_tokens_mistral': 713, 'n_tokens_neox': 673, 'n_words': 277, 'path': 'book/building-tools/05-modularisation/how/.main.md.bcp.ipynb', 'repo_name': '11michalis11/pfm', 'size': '2373'}