Skip to main content

Provides decorator 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

Реестры

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

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.1.2.tar.gz (10.2 kB view hashes)

Uploaded Source

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