Package: python-service-tools
python-service-toolsUtilities for working with python services.Usagelogging_configDefault configuration for structlog.Configure json logging at the INFO level:fromservicetools.logging_configimportdefault_logging,LogFormat,Verbositydefault_logging(Verbosity.INFO,LogFormat.JSON)Configure text logging at the DEBUG level:fromservicetools.logging_configimportdefault_logging,LogFormat,Verbositydefault_logging(Verbosity.DEBUG,LogFormat.TEXT)Configure text logging at the DEBUG level and filter out external loggers:fromservicetools.logging_configimportdefault_logging,LogFormat,Verbositydefault_logging(Verbosity.DEBUG,LogFormat.TEXT,["extern_logger_1"])Log timing information for a functionDecorator to add timing information to the logs:fromservicetools.timerimporttimerimportstructlog@timer(structlog.get_logger(__name__))defsome_function():passCreate a namespace relative patchCreate namespace relative patches:importsome_package.sub_package.another_packageasunder_testfromservicetools.testingimportrelative_patch_makerpatch=relative_patch_maker(under_test.__name__)classTestStuff:#equivalent to @unittest.mock.patch("some_package.sub_package.another_package.something_to_patch")@patch("something_to_patch")deftest_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")deftest_something(self,patched):under_test.something()patched.assert_called_once()Starlette Structlog middlewareMiddleware forStarletteframework 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.fromservicetools.middlewareimportStructlogRequestMiddlewareimportstructlogapp.add_middleware(StructlogRequestMiddleware(app,logger=structlog.get_logger(__name__)))There are options to customize the logging:importloggingimportstructlogfromservicetools.middlewareimportStructlogRequestMiddlewareapp.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 specificationSpecification fordramatiqactors that allows them to connect a broker
explicitly through theinit_actorfunction 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 soimportdramatiqfromservicetools.lazyactorimportLazyActor@dramatiq.actor(actor_class=LazyActor)deftest_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 sofromdramatiq.brokers.rabbitmqimportRabbitmqBrokerfrompikaimportPlainCredentialsimportdramatiqbroker=RabbitmqBroker(host="localhost",credentials=PlainCredentials(username="user",password="password"),)dramatiq.set_broker(broker)test_func.init_actor(broker=broker)Development GuideThis project usespoetry:$ pip install poetry
$ cd to/project/root
$ poetry installTestingTesting is done via pytest.$ poetry run pytest