Skip to main content

Py-Macaroni

This is a library to help you avoid Spaghetti Code. It helps you express your application logic in a testable way. The whole idea for this lib was based on some concepts from functional programming and rust, mainly the "unwrap" concept from rust.

Why

When creating bussiness logic for your application, sometimes it gets hard to structure complex logic in a easy to understand way. To help with that, this library was created. The main objective is to help you structure your application in a clean and readable way, enabling future maintenance of the code.

For example, if you have a CRUD application, some logic will be shared between the Create and Update functions, the lib, in this case, can help by chaining functions required to implement the main bussiness rule, and makes it easier to reuse functions when required.

import macaroni as mci

class BussinessLogic(mci.Macaroni):
    def validate_request(self, data):
        # ...

    def check_user_permissions(self, data):
        # ...

    def log_operation(self, data):
        # ...

    def create_item(self, data):
        # ...

    def update_item(self, data):
        # ...

    @mci.error
    def handle_error(self, data, error):
        # ...

    @mci.exception(DuplicateKey)
    def handle_duplicate_item(self, data, exception):
        # ...


def create_item(data):
    bl = BussinessLogic(data)
    result = (
        bl.validate_request()
        .check_user_permissions()
        .create_item()
        .log_operation()
        .handle_error()
        .handle_duplicate_item()
        .unwrap()
    )
    return result


def update_item(data):
    bl = BussinessLogic(data)
    result = (
        bl.validate_request()
        .check_user_permissions()
        .update_item()
        .log_operation()
        .handle_error()
        .unwrap()
    )
    return result

It also helps you avoid dealing with error and exception handling on each part of your application logic, letting you handle then only at the end of the application execution, automatically skipping steps in your application logic when an error or exception happens.

Installation

pip install macaroni

Usage

More examples can be found in the ./examples directory.

import macaroni as mci

class Calculator(mci.Macaroni):
    def increment(self, value, increment=None):
        if increment is None:
            increment = 1

        return value + increment

    def divide(self, value, denominator):
        if denominator == 0:
            return mci.Error('invalid denominator {0}'.format(denominator))

        return value / denominator

    def invalid(self, value):
        raise ValueError('invalid data')


base_val = 10
calc = Calcultator(base_val)
result = calc \
    .increment(2) \
    .divide(2) \
    .unwrap() # this returns the final result
assert result == 6

Error handling:

calc = Calculator(10)
result = calc \
    .increment(2) # this is executed \
    .divide(-3) # this causes an error \
    .increment() # this is not executed \
    .unwrap() # returns the error

assert bool(calc.error) == True
assert result == 'invalid denominator 0'

Exception handling:

calc = Calculator(10)
try:
    calc \
        .increment(2) # this is executed \
        .invalid() # this causes an exception \
        .increment() # this is not executed \
        .unwrap() # the exception ValueError is raised on unwrap
except ValueError:
    pass

assert bool(calc.exception) == True

When testing, you can use the parameter magic=False to disable the automatic value passing, chaining and error/exception handling:

def test_increment():
    calc = Calculator(magic=False)
    result = calc.increment(10, 2)
    assert result == 12

def test_error():
    calc = Calculator(magic=False)
    result = calc.divide(10, 0)
    assert calc.error is not None
    assert result = 'invalid denominator 0'

def test_exception()
    calc = Calculator(magic=False)

    raised_error = False
    try:
        calc.invalid(10)
    except ValueError:
        raised_error = True

    assert raised_error

Release files for macaroni 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for macaroni 0.0.3
File Size Uploaded
macaroni-0.0.3.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for macaroni 0.0.3
File Interpreter ABI Platform
macaroni-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 9.7 kB

Release files / macaroni-0.0.3.tar.gz

Download URL macaroni-0.0.3.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
31442aff1a1a5acccea23647d29dac63acf8efea5a2109879156c65ac79b043a
BLAKE2b-256 checksum
How to use checksums
4346661ebe62baba8ba286cea1c2fe66fca86158beca48623804ff01ed7e1d8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

Release files / macaroni-0.0.3-py3-none-any.whl

Download URL macaroni-0.0.3-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e6f45b42e62cef03adf67ca7091ba68f8734025590f7ddd09daf66a06b7344ed
BLAKE2b-256 checksum
How to use checksums
7b6b1a427720633cf48c28d1c94aaae4f5873db11ebd913b259cc8c81e89b4ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page