Skip to main content

Coeur

A simple service framework for python applications.

Introduction

When creating python based applications, there is typically some core business logic. There are countless ways to organise this logic, including by using functions, or by building the business logic into other frameworks. For example, in a Flask application one may put the business logic directly into a View, or in Django Rest Framework, one might put the business logic into the Serializer.

For smaller applications, coupling the business logic with the main framework you are using isn't problematic, but in larger applications it can become a limitation. For example, if you have a single interface to the business logic that is a REST API, then having the business logic in the views/serializers isn't problematic, but if your application grows and you end up having CLIs, or celery workers that also need to interface with the business logic, then the business logic can't be coupled with the framework used by one of the interfaces.

The purpose of this library is to a create a service framework that allows the different interfaces to consume the core business logic.

Practically speaking, the Flask and DRF examples would be as follows:

  • Flask: View will call the Service
  • DRF: ViewSet will either call the Service, or will call a Serializer that calls the Service
  • Celery: Task will call the service directly

ServiceAction

Action

A service action is a method on a service that can have any number of validations.

A service action is defined as a class attribute. The action is then used as a decorator to set the permissions, validators, and the method that will be called.

When the service is called, the permissions will be checked, then each validator will be called in order of definition, and if all of those pass, the action method will be called.

class MyService:
    @action
    def create_something(self, data):
        # The method that will be called from service.action()
        ...

    # Optionally specify a validator context factory
    @action.validator_context
    def make_context(self, data):
        # The return value will be passed to each validator
        return MyContext()

    @create_something.validate
    def validate_creation(self, context, data):
        # Perform some validation
        ...

The action can be initialised with validators directly, or any combination thereof.

class MyService:
    @action(
        validator_context_factory=make_context,
        validators=[validate_something]
    )
    def create_something(self, data):
        # The method that will be called from service.action()
        ...

Validators

A service action can have multiple validators, defined using:

@my_action.validate
def my_validation_function(self, data):
    # Perform the validation
    ...

The validators are called in the order of definition, and the validation function shouldn't mutate the data (any returned value is be discarded). The framework can work in conjunction with more powerful validation specific libraries such as Marshmallow, for example:

class OrderService:
    @action
    def create(self, data: dict):
        # Do something

    @create.validate
    def validate_order_creation(self, data: dict):
        OrderMarshmallowSchema().load(data)

Metadata

Release files for coeur 2.1.2

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

Source distribution (sdist)

Source distribution for coeur 2.1.2
File Size Uploaded
coeur-2.1.2.tar.gz 6.6 kB Details

Release files / coeur-2.1.2.tar.gz

Download URL coeur-2.1.2.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e592b2a3de0c8b76ac92bb30dcf0721689cc72ed79347464ec7e38d099ffff94
BLAKE2b-256 checksum
How to use checksums
07f2b9b65cfbfa3da45788f59328473212c5136427dae0182ddf05f6a86816fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.12

Release history Release notifications | RSS feed

2.1.3

1 release file

This release

2.1.2 This release

1 release file

2.1.1

1 release file

2.1.0

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.0

1 release file

0.0.1

1 release file

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