Skip to main content

Provides primitives for application layer

Project description

Classic App Layer

This package provides primitives for application layer. Part of project "Classic".

Usage for validation:

from dataclasses import dataclass

from classic.app import DTO, validate_with_dto
from pydantic import validate_arguments


class SomeDTO(DTO):
    """Based on pydantic.BaseModel. Used for validating input"""
    
    some_field: int
    another_field: str


@dataclass
class SomeAppCls:
    """Some class with app logic. May be mapped on DB."""
    some_field: int
    another_field: str


class SomeService:
    
    @validate_arguments
    def some_method(self, arg: int):
        assert isinstance(arg, int)
    
    @validate_with_dto
    def another_method(self, params: SomeDTO):
        instance = params.create_obj(SomeAppCls)
        print(instance)

Usage for errors:

from classic.app import AppError, ErrorsList


# Describe errors, possible in application
class IncorrectState(AppError):
    msg_template = 'Incorrect app state - "{text}"'
    code = 'app.incorrect_state'


class ServiceNotReady(AppError):
    msg_template = 'Service not ready yet'
    code = 'app.service_not_ready'


# In another file with services:
class SomeService:
    
    def __init__(self):
        self.ready_to_serve = False

    def is_ready(self):
        """Demonstrates simple usage"""
        if not self.ready_to_serve:
            raise ServiceNotReady()

    def mark_as_ready(self):
        """Demonstrates usage of error message templates"""
        if self.ready_to_serve:
            raise IncorrectState(text='Service are ready')
        self.ready_to_serve = True

    def just_give_errors(self):
        """Demonstrates method, what may have more than 1 error"""
        errors = [IncorrectState(text='error 1'), 
                  IncorrectState(text='error 2')]
        raise ErrorsList(errors)


# Somewhere in adapters:

service = SomeService()

try:
    service.is_ready()
except AppError as error:
    print(f'Application responses with error code "{error.code}", '
          f'message is "{error.message}"')

try:
    service.mark_as_ready()
    service.mark_as_ready()
except AppError as error:
    print(f'Application responses with error code "{error.code}", '
          f'message is "{error.message}"')
    
try:
    service.just_give_errors()
except ErrorsList as errors_list:
    for error in errors_list.errors:
        print(f'Application responses with error code "{error.code}", '
              f'message is "{error.message}"')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

classic-app-layer-0.0.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

classic_app_layer-0.0.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file classic-app-layer-0.0.1.tar.gz.

File metadata

  • Download URL: classic-app-layer-0.0.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.7.3

File hashes

Hashes for classic-app-layer-0.0.1.tar.gz
Algorithm Hash digest
SHA256 2a2ff4a0654221cd94b1b24842027887a07a08b096ac0f3fb3d55dafdef2e1d4
MD5 1133ce5aeeea93b22597c8455d11e2e8
BLAKE2b-256 f920e3166334a5bb89a13ca789020eeb2a005d15b6a9a68c51475fa0bc2759ab

See more details on using hashes here.

File details

Details for the file classic_app_layer-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: classic_app_layer-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.7.3

File hashes

Hashes for classic_app_layer-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9ab8f8b39627df7d9ad17b1770672f0ddbdf4b792a17b89ec0c897743bbc715e
MD5 959878ef8e7757b8c82b786451f59ddc
BLAKE2b-256 86d0c94f5b0deaf1a8146bb846949b7301bc1c041c96494918112826c5040caf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page