Package: sesamutils
SesamUtilsPython module to simplify common tasks when developing microservices for the Sesam integration platform.Usage examplesEnvironment VariablesYou 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.fromsesamutilsimportVariablesConfigimportsysrequired_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 tuplesconfig=VariablesConfig(required_env_vars,optional_env_vars=optional_env_vars)ifnotconfig.validate():sys.exit(1)print(config.username)DotdictifyA class to help traversing and modifying large nested dictionaries. Lets you navigate by dot notation.fromsesamutilsimportDotdictifyexample_dict={"test":{"my_thing":"hello"}}dot_dict=Dotdictify(example_dict)print(dot_dict.test.my_thing)# helloProfilerYou 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.fromsesamutilsimportprofiler@profilerdef<Nameofyourmethod>():<yourmethoddefinition># 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.ncallstottimepercallcumtimepercallfilename:lineno(function)10.0000.0000.3120.312C:/Work/PycharmProjects/node-notification-handler/service/notification-handler.py:143(get_node_members_and_roles)20.0000.0000.3110.156C:\Users\ravish.ranjan\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py:537(get)20.0000.0000.3110.156C:\Users\ravish.ranjan\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py:466(request)20.0000.0000.3090.154C:\Users\ravish.ranjan\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py:617(send)20.0000.0000.3080.154C:\Users\ravish.ranjan\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\adapters.py:394(send)20.0000.0000.3060.153C:\Users\ravish.ranjan\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\connectionpool.py:446(urlopen)20.0000.0000.3060.153C:\Users\ravish.ranjan\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\connectionpool.py:319(_make_request)10.0000.0000.2580.258C:\Work\PycharmProjects\node-notification-handler\service\portal.py:34(get_subscription_members)Sesam LoggerYou 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.fromsesamutilsimportsesam_loggerlogger=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 tosesam_loggerlike this:logger=sesam_logger('<name of your module or logger>',app=<Flaskappinstance>)Serve Flask appWhen writing microservices usingFlask, 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 ourservefunction:...fromsesamutils.flaskimportserveapp=Flask(__name__)...if__name__=="__main__":serve(app)To define another port than the default (5000), you can do:serve(app,port=<port>)Installationpipinstallsesamutils