Skip to main content

Delphai backend common facilities [Pakage]

Development

Github action deploys to pypi when a new tag is created. Also make sure the version in setup.py is equal to the version on Github

NOTE PYPI wont allow version overriding, Means make sure the pakage works well before changing the version in setup.py

Installation

Include the reference to this library into your Pipfile

[packages]
delphai-backend-utils = "*"

The following packages will be installed automatically, and you do not need to include them into Pipfile:

  • omegaconf = "*"
  • memoization = "*"
  • azure-identity = "==1.4.0b3"
  • azure-keyvault = "*"
  • python-dotenv = "*"
  • kubernetes = "*"
  • coloredlogs = "*"

If you are going to use the module db_access, please also include pymongo into Pipfile:

[packages]
delphai-backend-utils = "*"
pymongo = "*"

If you are going to use the module grpc_calls, please also include gRPC dependencies into Pipfile:

[packages]
delphai-backend-utils = "*"
grpcio-reflection = "*"

For full functionality:

[packages]
delphai-backend-utils = "*"
pymongo = "*"
grpcio = "*"
grpcio-tools = "*"
grpcio-reflection = "*"
tornado = "*"

To update this library in your development environment:

pipenv update

Module config

Loads and provides configuration data from config/*.yml files, including resolved values from environment variables stored into .env file, and Kubernetes and Azure secrets.

Configuration files used to compile an actual working configuration:

  • config/default.yml - basic default configuration
  • config/development.yml - redefines default configuration in a development environment
  • config/review.yml - redefines default configuration in the review environment
  • config/staging.yml - redefines default configuration in the staging environment
  • config/production.yml - redefines default configuration in the production environment
  • .env - used only in development environment

Importing:

from delphai_backend_utils.config import get_config

Usage example 1 - retrieving a single value:

    address = get_config('server.host_and_port')

If value is not redefined in a environment-specific config file, it will be extracted from default.yml:

server:
  host_and_port: 0.0.0.0:8080

Usage example 2 - retrieving and using a whole chapter:

    server_config = get_config('server')
    address = server_config['host_and_port']  # Be careful, a KeyError might happen

Usage example 3 - retrieving the whole configuraion as an OmegaConf object:

    config = get_config(None)  # Notice "None"
    db_name = config.db.name  # "MissingMandatoryValue: Missing mandatory value: db.connection_string" can happen, and it might be a desirable behavior

Module db_config

Establishing connections to MongoDB.

Importing:

from delphai_backend_utils.db_config import get_own_db_connection

Please do not forget to include pymongo into your Pipfile (see above in the "Installation" section).

Usage example - printing out a "companies" collection size:

    db_conn = get_own_db_connection()
    print(db_conn.companies.estimated_document_count())

There is one more function chunks in this module which is hugely useful when we need to implement chunked reads and updates.

Module logging

Configurable logging. In addition to standard logging operations implements error_with_traceback function that logs a last occured exception.

Importing:

from delphai_backend_utils import logging

Usage example - printing out a "companies" collection size:

from delphai_backend_utils import logging

def fail_here():
    return 1/0

def calculate_result():
    try:
        return fail_here()
    except Exception:
        logging.error_with_traceback()
        return None

Module grpc_calls

The natural way to call other microservices. Functions:

  • call_grpc - calls other grpc service
  • async_call_grpc - awaitable version of call_grpc. Use if caller is "async def".
  • get_description - to get other grpc's service description
  • async_get_description - awaitable version of get_description. Use if caller is "async def".

Importing:

from delphai_backend_utils import grpc_calls

Please do not forget to include gRPC dependencies into your Pipfile (see above in the "Installatio" section).

Usage example - printing out a "companies" collection size:

    suggestions = grpc_calls.call_grpc('delphai.typeahead.Typeahead.get_suggestions', {'search_query': 'siemens'})
    for idx, suggestion in enumerate(suggestions['matches']):
        print(f'{idx + 1}. {suggestion["value"]} {suggestion.get("url")}')

Module users

The natural way to call other microservices. Functions:

  • call_grpc - calls other grpc service
  • async_call_grpc - awaitable version of call_grpc. Use if caller is "async def".
  • get_description - to get other grpc's service description
  • async_get_description - awaitable version of get_description. Use if caller is "async def".

Importing:

from delphai_backend_utils.user import get_user

Usage example:

    try:
        user = get_user(context)  # context is a gRPC context that usually contains user description in its metadata
        user_id_str = user.get('https://delphai.com/mongo_user_id')
    except Exception:
        raise Exception('Failed get user info')
    if not user_id_str:
        raise Exception('User profile is not properly configured')

Module formatting

Common forms and formatting standards. Functions:

  • clean_url - clean an url

Importing:

from delphai_backend_utils.formatting import clean_url

Usage example:

    url = clean_url(url, keep_www=True)  

Module own_gateway

Implements an individual HTTP1–>gRPC (HTTP2) gateway for gRPC microservices.

Usage example (serve function in server.py module):

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    service_pb2_grpc.add_GreetingServicer_to_server(Greeting(), server)

    # the reflection service will be aware of "Greeter" and "ServerReflection" services.
    service_names = (
        service_pb2.DESCRIPTOR.services_by_name['Greeting'].full_name,
        reflection.SERVICE_NAME,
    )
    reflection.enable_server_reflection(service_names, server)

    # Start own gateway if configured                                  # <<<<
    run_own_gateway = get_config('server.run_own_gateway') or False    # <<<<
    if run_own_gateway:                                                # <<<<
        from delphai_backend_utils.own_gateway import run_own_gateway  # <<<<
        run_own_gateway(service_pb2.DESCRIPTOR, server)                # <<<<

    address = get_config('server.host_and_port') or '0.0.0.0:8080'
    server.add_insecure_port(address)
    server.start()
    logging.info(f'Started server {address}')
    try:
        server.wait_for_termination()
    except KeyboardInterrupt:
        logging.error('Interrupted')

Lines to add marked with "# <<<<".

Additionally:

  1. Include tornado into your Pipfile (see above in the "Installation" section).
  2. Turn on this feature in your configuration file (default.yml or some other configuration file):
server:
  # host_and_port: 0.0.0.0:8080  # "0.0.0.0:8080" is a default value. Uncomment and change if you need another.
  run_own_gateway: true  # Set to true if you need an individual gateway to be runned together with gRPC handler.
  # gateway_host_and_port: 0.0.0.0:7070  # "0.0.0.0:7070" is a default value.

Module api_calls

A wrapper for calling APIs. Contains only a single function for Azure Machine Learning Endpoints right now (call_azure_endpoint).

Importing:

from delphai_backend_utils.api_calls import call_azure_endpoint

Usage example - calling word embedding model to retrieve similar keywords:

    api_url = 'http://51.145.149.205:80/api/v1/service/similar-keywords/score'
    api_key = ''  # has to be set
    keywords_resp = call_azure_endpoint(api_url, api_key, 'urban mobility')
    if keywords_resp['success']:
        print(', '.join([keyword for keyword, _ in keywords_resp['content']]))

Metadata

Release files for delphai-backend-utils 1.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for delphai-backend-utils 1.0.10
File Size Uploaded Provenance Actions
delphai_backend_utils-1.0.10.tar.gz 18.4 kB Without provenance Details for delphai_backend_utils-1.0.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for delphai-backend-utils 1.0.10
File Interpreter ABI Platform Actions
delphai_backend_utils-1.0.10-py3-none-any.whl Python 3 none any Details for delphai_backend_utils-1.0.10-py3-none-any.whl

Total release size: 37.0 kB

Release files / delphai_backend_utils-1.0.10.tar.gz

Download URL delphai_backend_utils-1.0.10.tar.gz
Size 18.4 kB
Tags Source
SHA-256 checksum
How to use checksums
080baa36ffbcf6c9fc96edc7d4c4c04b7574d7afd458ba8b4e85426027044415
BLAKE2b-256 checksum
How to use checksums
d6b764b89ea33d1b38d385a492205049198f538757b75624598b107f930dc3f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Release files / delphai_backend_utils-1.0.10-py3-none-any.whl

Download URL delphai_backend_utils-1.0.10-py3-none-any.whl
Size 18.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ac6553f6477cf602755d5bb5e5fd9e0f4aa3dceffa411cc4e3a0751b7c6e8f47
BLAKE2b-256 checksum
How to use checksums
10902bf9b3da85c7f236f301153e88ba40a5862eb7099fd82f6f30d954649e27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Security for delphai-backend-utils 1.0.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page