Adding Functionality to Packages

Software Engineering Principles in Python

Adam Spannbauer

Machine Learning Engineer at Eastman

Package structure

Package Structure

Software Engineering Principles in Python

Adding functionality

working in work_dir/my_package/utils.py

def we_need_to_talk(break_up=False):
    """Helper for communicating with significant other"""
    if break_up:
        print("It's not you, it's me...")
    else:
        print('I <3 U!')

working in work_dir/my_script.py

# Import utils submodule
import my_package.utils

# Decide to start seeing other people
my_package.utils.we_need_to_talk(break_up=True)
It's not you, it's me...
Software Engineering Principles in Python

Importing functionality with __init__.py

working in work_dir/my_package/__init__.py

from .utils import we_need_to_talk

working in work_dir/my_script.py

# Import custom package
import my_package

# Realize you're with your soulmate
my_package.we_need_to_talk(break_up=False)
I <3 U!
Software Engineering Principles in Python

Extending package structure

Extended Package Structure

Software Engineering Principles in Python

Extending package structure

SubPackage Structure

Software Engineering Principles in Python

Let's Practice

Software Engineering Principles in Python

Preparing Video For Download...