Package: runtime-config-py
runtime-config-pyThis library allows you to update project settings at runtime. In its basic use case, it is just a client for theserver, but if necessary, you can implement your adapter for the
desired source and get settings from them.runtime-config-py supports Python 3.8+.Examples of using:Create feature flags to control which features are enabled for users. Feature flags are especially useful when the
service is based on a microservice architecture and the addition of a new feature affects multiple services.Quick response to problems in project infrastructure. For example, if one of consumers sends too many requests to
another service, and you need to reduce its performance.Table of contents:InstallationUsageBackendDevelopmentTestsStyle codeInstallationYou can install the library like this:from pypipip install "runtime-config-py[aiohttp]"orpoetry add runtime-config-py -E aiohttpfrom git:pip install git+https://github.com/runtime-config/runtime-config-py.git#egg="runtime-config-py[aiohttp]"Source dependencies have been moved to extras to give you more control over which libraries are installed. If you
have a project dependency on a certain version of aiohttp you can install the library without specifying extras.pip install runtime-config-pyUsageExamples of using the library can be foundhere.Let's see a simple example of using this library together with aiohttp application.fromaiohttpimportwebfromruntime_configimportRuntimeConfigfromruntime_config.sourcesimportConfigServerSrcasyncdefhello(request):name=request.app['config'].namereturnweb.Response(text=f'Hello world{name}!')asyncdefinit(application):source=ConfigServerSrc(host='http://127.0.0.1:8080',service_name='hello_world')config=awaitRuntimeConfig.create(init_settings={'name':'Alex'},source=source)application['config']=configasyncdefshutdown(application):awaitapplication['config'].close()app=web.Application()app.on_startup.append(init)app.on_shutdown.append(shutdown)app.add_routes([web.get('/',hello)])web.run_app(app,port=5000)Before running this code, you need to runserverfrom which this
library can take new values for your variables.
If you don't do this, nothing bad will not happen. You simply cannot change the value of the name variable at runtime :)Automatic source initializationYou can simplify library initialization by automatically creating a source instance. Simply define the following
environment variables and the source instance will be created automatically:RUNTIME_CONFIG_HOSTRUNTIME_CONFIG_SERVICE_NAMEWays to access settingsThis library supports several ways to access variables. All of them are shown below:print(config.name)print(config['name'])print(config.get('name',default='Dima'))BackendCurrently, only 1backendis supported. Later, support for other
backends, such as redis, will probably be added to the library, but this is not in the nearest plans.If you need support for another settings storage source right now, you can write your own source. Implementing this is
very simple. You need to create a class that will be able to retrieve data from the desired source and will inherit
fromruntime_config.sources.base.BaseSource. After that, an instance of the class you created must be passed to
theRuntimeConfig.createmethod.your_source=YourSource(...)config=awaitRuntimeConfig.create(...,source=your_source)DevelopmentInstall depspoetry install --all-extrasTestsCheck the work of the library on several versions of Python at once using the command below:make test-multi-versionsThe simple test run is available through the command below:make testStyle codeFor automatic code formatting and code verification, you need to use the command below:make lint