Skip to main content

Component definitions for Pip.Services in Python

Project description

Pip.Services Logo
Business Logic Components for Python

This module is a part of the Pip.Services polyglot microservices toolkit.

The Logic module contains standard component definitions to handle complex business transactions.

The module contains the following packages:

  • Cache - distributed cache
  • Lock - distributed lock components
  • State - distributed state management components

Quick links:

Use

Install the Python package as

pip install pip_services4_logic

Example how to use caching and locking. Here we assume that references are passed externally.

from pip_services3_commons.refer import Descriptor, References, IReferences, IReferenceable
from pip_services4_logic.cache import ICache, MemoryCache
from pip_services4_logic.lock.ILock import ILock
from pip_services4_logic.lock.MemoryLock import MemoryLock


class MyComponent(IReferenceable):
    __cache: ICache
    __lock: ILock

    def set_references(self, references: IReferences):
        self.__cache = references.get_one_required(Descriptor("*", "cache", "*", "*", "1.0"))
        self.__lock = references.get_one_required(Descriptor("*", "lock", "*", "*", "1.0"))

    def my_method(self, context, param1):
        # First check cache for result
        result = self.__cache.retrieve(context, 'mykey')

        # Lock..
        self.__lock.acquire_lock(context, "mykey", 1000, 1000, )

        # Do processing
        # ...

        # Store result to cache async
        self.__cache.store(context, 'mykey', result, 3600000)

        # Release lock async
        self.__lock.release_lock(context, 'mykey')

        return result


# Use the component
my_component = MyComponent()
my_component.set_references(References.from_tuples(
    Descriptor("pip-services", "cache", "memory", "default", "1.0"), MemoryCache(),
    Descriptor("pip-services", "lock", "memory", "default", "1.0"), MemoryLock(),
))

result = my_component.my_method(None, param1)

If you need to create components using their locators (descriptors) implement component factories similar to the example below.

from pip_services3_commons.refer import Descriptor
from pip_services4_logic.build import Factory


class MyFactory(Factory):
    my_component_descriptor = Descriptor("myservice", "mycomponent", "default", "*", "1.0")

    def __init__(self):
        super(MyFactory, self).__init__()

        self.register_as_type(MyFactory.my_component_descriptor, MyFactory)


# Using the factory
my_factory = MyFactory()
my_component1 = my_factory.create(Descriptor("myservice", "mycomponent", "default", "myComponent1", "1.0"))
my_component2 = my_factory.create(Descriptor("myservice", "mycomponent", "default", "myComponent2", "1.0"))

...

Develop

For development you shall install the following prerequisites:

  • Python 3.7+
  • Visual Studio Code or another IDE of your choice
  • Docker

Install dependencies:

pip install -r requirements.txt

Run automated tests:

python test.py

Generate API documentation:

./docgen.ps1

Before committing changes run dockerized build and test as:

./build.ps1
./test.ps1
./clear.ps1

Contacts

The initial implementation is done by Sergey Seroukhov. Pip.Services team is looking for volunteers to take ownership over Python implementation in the project.

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

pip_services4_logic-0.0.2.tar.gz (15.1 kB view details)

Uploaded Source

File details

Details for the file pip_services4_logic-0.0.2.tar.gz.

File metadata

  • Download URL: pip_services4_logic-0.0.2.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for pip_services4_logic-0.0.2.tar.gz
Algorithm Hash digest
SHA256 2190ac1b7e44cd4e59a9fdb496be4a79d322b606dd075c2d25bdd64f685b2fc4
MD5 706c4a4c388e31888d5c78c36f18c97e
BLAKE2b-256 060fac839b5305653651155451fc9140e64a6b8849133c1e9e5629f2369716a7

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