Commons module

Provides a set of tools used in microservices or backend services, and it is designed to facilitate symmetric implementation accross different programming languages.

Packages

The module contains the following packages:

  • Commands - commands and events
  • Config - component configuration
  • Convert - portable value converters
  • Data - data patterns
  • Errors - application errors
  • Random - random data generators
  • Refer - component dependencies (Based on the inversion of control (IoC) pattern)
  • Reflect - portable reflection utilities
  • Run - component life-cycle management
  • Validate - validation rules

Use

Install the Python package as

pip install pip-services3-commons

Then you are ready to start using the Pip.Services patterns to augment your backend code.

For instance, here is how you can implement a component, that receives configuration, get assigned references, can be opened and closed using the patterns from this module.

from pip_services3_commons.config import IConfigurable, ConfigParams
from pip_services3_commons.refer import IReferenceable, IReferences, Descriptor
from pip_services3_commons.run import IOpenable


class MyComponentA(IConfigurable, IReferenceable, IOpenable):
    _param1 = 'ABC'
    _param2 = 123
    _another_component: MyComponentB
    _opened = True

    def configure(self, config):
        self._param1 = config.get_as_string_with_default("param1", self._param1)
        self._param2 = config.get_as_integer_with_default("param2", self._param2)

    def set_references(self, references):
        self._another_component = references.get_one_required(
            Descriptor("myservice", "mycomponent-b", "*", "*", "1.0")
        )

    def is_opened(self):
        return self._opened

    def open(self, correlation_id):
        self._opened = True
        print("MyComponentA has been opened.")

    def close(self, correlation_id):
        self._opened = False
        print("MyComponentA has been closed.")

Then here is how the component can be used in the code

from pip_services3_commons.config import IConfigurable, ConfigParams
from pip_services3_commons.refer import References, Descriptor

my_component_A = MyComponentA()

# Configure the component
my_component_A.configure(ConfigParams.from_tuples(
    'param1', 'XYZ',
    'param2', 987
))

# Set references to the component
my_component_A.set_references(References.from_tuples(
    Descriptor("myservice", "mycomponent-b", "default", "default", "1.0"), my_component_B
))

# Open the component
my_component_A.open("123")