Skip to main content

Provides utilities for creating components

Project description

Classic Components

Этот пакет предоставляет функциональность компонентов. Является частью проекта "Classic".

Главное, что предоставляет пакет - это декоратор component для обертывания классов. Класс, обернутый таким декоратором, называется далее компонентом, и обладает рядом свойств:

  • автоматически сгенерированный конструктор на базе аннотаций.
  • добавляет "дополнительные аннотации" к аннотациям класса (подробности далее)
  • добавляет автоматическую регистрацию инстансов в указанных реестрах

Простой пример:

from classic.components import component


@component
class SomeService:
    prop: int
    
    def action(self):
        print(self.prop)


service = SomeService(prop=1)
service.action()  # prints 1

Генерацию конструктора можно отключить:

from classic.components import component


@component(init=False)
class SomeService:
    prop: int
    
    def action(self):
        print(self.prop)


service = SomeService(prop=1)  # TypeError: SomeService() takes no arguments

Также есть алиасы для часто используемых параметров field из dataclasses:

from dataclasses import field
from classic.components import component, no_init, factory, default


@component
class SomeLogic:
    some_list: field(default_factory=list)
    analog: factory(list)  # то же самое
    
    some_str: str = field(default='Hello')
    analog_str: str = default('Hello')

    _some_field: int = field(default=0, init=False)
    _analog: int = no_init(0)  # то же самое

Реестры

Также пакет предоставляет интерфейс для создания реестров и базовую реализацию реестра со слабыми ссылками. Это нужно не столько для кода приложений, сколько для других библиотек в платформе.

from classic.components import component, Registry, WeakSetRegistry


@component(init=False)
class SomeService:
    registry: Registry
    
    def action(self):
        print(self.prop)


registry = WeakSetRegistry()
service = SomeService(registry=registry)

print(registry.storage)
# {<weakref at 0x109390fe0; to 'SomeService' at 0x10933ec50>}

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_components-1.3.1.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

classic_components-1.3.1-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file classic_components-1.3.1.tar.gz.

File metadata

  • Download URL: classic_components-1.3.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for classic_components-1.3.1.tar.gz
Algorithm Hash digest
SHA256 83acb39d659653b69a55bcf972a6b52b5a0c19fd64a9d015764a549c70ed43b6
MD5 04879b82f52a2c20e0ea449740f651f1
BLAKE2b-256 eecb2bac8af76aa8e406462c62d21b88548561a0f2450719067a388e96bd49a3

See more details on using hashes here.

File details

Details for the file classic_components-1.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for classic_components-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 da7885096bee6d14be846e4b651ca5cf6670eed85b2fed1809b2297ce20a1b5a
MD5 887b1c57bce8cc70bebffea43fc8937f
BLAKE2b-256 3276de8a6d553af17a9829f8ce6bf3277188de8ecf0008d35e97517afacd3c91

See more details on using hashes here.

Supported by

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