Skip to main content

SesamUtils

Build Status

Python module to simplify common tasks when developing microservices for the Sesam integration platform.

Usage examples

Environment Variables

You can use the VariablesConfig class to load environment variables into your service. It requires a list of required variables. It also support an optional list of variables as a second argument.

from sesamutils import VariablesConfig
import sys

required_env_vars = ["username", "password", "hostname"]
optional_env_vars = ["debug", ("auth_type", "user")] # Default values can be given to optional environment variables by the use of tuples

config = VariablesConfig(required_env_vars, optional_env_vars=optional_env_vars)

if not config.validate():
    sys.exit(1)


print(config.username)

Dotdictify

A class to help traversing and modifying large nested dictionaries. Lets you navigate by dot notation.

from sesamutils import Dotdictify

example_dict = {
    "test": {
        "my_thing": "hello"
    }
}

dot_dict = Dotdictify(example_dict)

print(dot_dict.test.my_thing)

# hello

Profiler

You can use this (A profiling decorator) to see complete details of execution time taken by a function in your program. Based on that, you can optimize your python code if required.

from sesamutils import profiler

@profiler
def <Name of your method>():
    <your method definition>

# Apply to any function with @profiler
# Profiles the function using cProfile, and prints out a report to screen.
# belwo are few lines for illustration  purpose.
ncalls tottime  percall  cumtime  percall filename:lineno(function)
1    0.000    0.000    0.312    0.312 C:/Work/PycharmProjects/node-notification-handler/service/notification-handler.py:143(get_node_members_and_roles)
2    0.000    0.000    0.311    0.156 C:\Users\ravish.ranjan\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py:537(get)
2    0.000    0.000    0.311    0.156 C:\Users\ravish.ranjan\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py:466(request)
2    0.000    0.000    0.309    0.154 C:\Users\ravish.ranjan\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py:617(send)
2    0.000    0.000    0.308    0.154 C:\Users\ravish.ranjan\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\adapters.py:394(send)
2    0.000    0.000    0.306    0.153 C:\Users\ravish.ranjan\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\connectionpool.py:446(urlopen)
2    0.000    0.000    0.306    0.153 C:\Users\ravish.ranjan\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\connectionpool.py:319(_make_request)
1    0.000    0.000    0.258    0.258 C:\Work\PycharmProjects\node-notification-handler\service\portal.py:34(get_subscription_members)

Sesam Logger

You can use this to save time and lines of code. It provides the standard customization of root level logging- configuration and gives you SESAM uniform standard settings.

from sesamutils import sesam_logger

logger = sesam_logger('<name of your module or logger>')

Default log level is 'INFO'. To set a different level, you need to provide the environment variable 'LOG_LEVEL'. Since the microservice log view in the Sesam portal gets it's own timestamps directly from docker, this logger does not print timestamps by default. However if you want to log timestamps regardless, you can enable timestamps like this:

logger = sesam_logger('<name of your module or logger>', timestamp=True)

If your app is using Flask and cherrypy, you can enable request logging by sending the app instance to sesam_logger like this:

logger = sesam_logger('<name of your module or logger>', app=<Flask app instance>)

Serve Flask app

When writing microservices using Flask, we don't want to use the built-in development web server as this among other things doesn't set content-length or support chunked encoding (streams). To mitigate this we use cherrypy to serve our Flask microservices. To simplify this task, you can use our serve function:

...
from sesamutils.flask import serve

app = Flask(__name__)

...

if __name__ == "__main__":
    serve(app)

To define another port than the default (5000), you can do:

    serve(app, port=<port>)

Installation

pip install sesamutils

Metadata

Release files for sesamutils 0.1.10

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

Built distribution (wheel)

Table of built distributions (wheels) for sesamutils 0.1.10
File Interpreter ABI Platform
sesamutils-0.1.10-py3-none-any.whl Python 3 none any Details

Release files / sesamutils-0.1.10-py3-none-any.whl

Download URL sesamutils-0.1.10-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b58edc36604f91acb47eb424ceb4ea9513eccf2dd37e511b84cda8492498d93a
BLAKE2b-256 checksum
How to use checksums
a5ca856764bdb6bc845ad9879440a1528f92e924684a12506da3d4fc050e017d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

Security for sesamutils 0.1.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