DEV Community

João M.C. Teixeira
João M.C. Teixeira

Posted on • Originally published at pythonicthoughtssnippets.github.io

Separating business and administrative logic - example 1

Hi,

(I am re-posting here a post I made in 2019 on my blog)

A quick view on business and administrative logic separation.

A dummy example where we use decorators to check if the input path exists before assigning it to the class attribute. Context managers could accomplish the same, but with the decorator, checking takes place before even executing the function.

def check_folder_exists(func):
    """
    Decorator for class property setters
    """
    @wraps(func)
    def wrapper(self, folder):

        if not Path(folder).exists():
            raise FileNotFoundError(f'Path not found: {folder}')

        else:
            return func(self, folder)
    return wrapper


class Parameters:
    def __init__(self, **kwargs):
        self.kwargs = kwargs

    @property
    def db_folder(self):
        return self._db_folder

    @db_folder.setter
    @check_folder_exists
    def db_folder(self, folder):
        self._db_folder = folder
Enter fullscreen mode Exit fullscreen mode

Below is another snippet from my code where I use a combination of three contexts to handle exceptions hierarchically.

for step in config_reader.steps:

    with GCTXT.abort_if_exception(), \
            GCTXT.query_upon_error(XCPTNS.ConfigReadingException), \
            GCTXT.abort_if_critical_exceptions(CONFREADER.critical_errors):

        step()
Enter fullscreen mode Exit fullscreen mode

Do you find this useful for your projects? Let me know.

Cheers,

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more