Skip to main content

python-service-tools

Utilities for working with python services.

PyPI - Python Version PyPI

Usage

logging_config

Default configuration for structlog.

Configure json logging at the INFO level:

from servicetools.logging_config import default_logging, LogFormat, Verbosity

default_logging(Verbosity.INFO, LogFormat.JSON)

Configure text logging at the DEBUG level:

from servicetools.logging_config import default_logging, LogFormat, Verbosity

default_logging(Verbosity.DEBUG, LogFormat.TEXT)

Configure text logging at the DEBUG level and filter out external loggers:

from servicetools.logging_config import default_logging, LogFormat, Verbosity

default_logging(Verbosity.DEBUG, LogFormat.TEXT, ["extern_logger_1"])

Log timing information for a function

Decorator to add timing information to the logs:

from servicetools.timer import timer

import structlog

@timer(structlog.get_logger(__name__))
def some_function():
    pass

Create a namespace relative patch

Create namespace relative patches:

import some_package.sub_package.another_package as under_test
from servicetools.testing import relative_patch_maker

patch = relative_patch_maker(under_test.__name__)

class TestStuff:
    #equivalent to @unittest.mock.patch("some_package.sub_package.another_package.something_to_patch")
    @patch("something_to_patch")
    def test_something(self, patched):
        under_test.something()
        patched.assert_called_once()

    #equivalent to @unittest.mock.patch("some_package.sub_package.another_package.something_else_to_patch")
    @patch("something_else_to_patch")
    def test_something(self, patched):
        under_test.something()
        patched.assert_called_once()

Starlette Structlog middleware

Middleware for Starlette framework to log HTTP requests to structlog. Log entries will be made at the start and end of each request. Error requests (400s and 500s) will also be logged. Any calls that throw exceptions will be converted 500 responses.

from servicetools.middleware import StructlogRequestMiddleware
import structlog

app.add_middleware(StructlogRequestMiddleware(app, logger=structlog.get_logger(__name__)))

There are options to customize the logging:

import logging

import structlog
from servicetools.middleware import StructlogRequestMiddleware

app.add_middleware(StructlogRequestMiddleware(
    app,
    logger=structlog.get_logger(__name__),
    log_level=logging.DEBUG,  # Log at the DEBUG level.
    ignored_status_codes={404},  # Do not log 404 errors.
))

Dramatiq Lazy Actor specification

Specification for dramatiq actors that allows them to connect a broker explicitly through the init_actor function rather than implicitly when they are created. This allows you to defer setting up your Rabbitmq broker to a time of your choosing. To create a new actor that uses this behavior, set up your dramatiq actors like so

import dramatiq

from servicetools.lazyactor import LazyActor

@dramatiq.actor(actor_class=LazyActor)
def test_func(data: str) -> None:
    print(data)

Now, whenever you have set up your Rabbitmq instance and connected to it, tell your actors to connect to it like so

from dramatiq.brokers.rabbitmq import RabbitmqBroker
from pika import PlainCredentials
import dramatiq

broker = RabbitmqBroker(
    host="localhost",
    credentials=PlainCredentials(username="user", password="password"),
)
dramatiq.set_broker(broker)

test_func.init_actor(broker=broker)

Development Guide

This project uses poetry:

$ pip install poetry
$ cd to/project/root
$ poetry install

Testing

Testing is done via pytest.

$ poetry run pytest

Metadata

Release files for python-service-tools 0.5.2

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

Source distribution (sdist)

Source distribution for python-service-tools 0.5.2
File Size Uploaded
python_service_tools-0.5.2.tar.gz 10.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-service-tools 0.5.2
File Interpreter ABI Platform
python_service_tools-0.5.2-py3-none-any.whl Python 3 none any Details

Total release size: 22.6 kB

Release files / python_service_tools-0.5.2.tar.gz

Download URL python_service_tools-0.5.2.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6bd703b3dd64d6d909a923b1ce0934da96c657090ceea210cd33739f198d119f
BLAKE2b-256 checksum
How to use checksums
4c6e98016152eec326825fe00abd0ae5114220674f5cb919feaf1fe85c7ecea3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.11.3 Linux/5.15.0-1037-azure

Release files / python_service_tools-0.5.2-py3-none-any.whl

Download URL python_service_tools-0.5.2-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
76c395fef48d13e9ca37625a20192a28f5f7cb996800242d1314cd703a0cb610
BLAKE2b-256 checksum
How to use checksums
980b68803f14e055146df3cc22a02e6d0e8b757178c2eb2966a4c2b69bbe27dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.11.3 Linux/5.15.0-1037-azure

Release history Release notifications | RSS feed

This release

0.5.2 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.7

2 release files

0.4.6

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

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