{ "cells": [ { "cell_type": "markdown", "id": "1273fa5f-9ab5-4a4c-857a-0f212e759f12", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Configuration environment variables" ] }, { "cell_type": "code", "execution_count": 1, "id": "75bffc4a-24e8-4d2d-948d-2cb84bc11020", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "from earthkit.regrid import config" ] }, { "cell_type": "raw", "id": "022b7ed2-29f2-49fb-a619-ff86f45d7a48", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "For the rest of this notebook we disable the :ref:`config` autosave so the changes will not be written into our configuration file." ] }, { "cell_type": "code", "execution_count": 2, "id": "2072ae67-13df-4e8a-a1a4-d54a56639569", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "config.autosave = False" ] }, { "cell_type": "raw", "id": "0c497859-6d2a-4fb7-bfb0-0979eb0d2279", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Each :ref:`config` parameter has a corresponding environment variable (see the full list :ref:`here `). When an environment variable is set, it takes precedence over the settings parameter as the following example demonstrates it." ] }, { "cell_type": "markdown", "id": "2888d0b6-beea-4178-9c01-5a08a5be7ad2", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Assuming no environmental variable is set the value is read form the config file." ] }, { "cell_type": "code", "execution_count": 3, "id": "21f853d3-c961-4ad2-b200-c24f7b03a54e", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "30" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.get(\"url-download-timeout\")" ] }, { "cell_type": "raw", "id": "95036166-0eec-437b-9988-771e522ada2e", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "When the environment variable is set :func:`get ` returns its value." ] }, { "cell_type": "code", "execution_count": 4, "id": "d4a8c3bd-19ca-454a-8f39-07de7a2d318c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "env: EARTHKIT_REGRID_URL_DOWNLOAD_TIMEOUT=26\n" ] } ], "source": [ "%env EARTHKIT_REGRID_URL_DOWNLOAD_TIMEOUT=26" ] }, { "cell_type": "code", "execution_count": 5, "id": "dbc54f27-7b5e-467b-bcba-28daea0e3c46", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "26" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.get(\"url-download-timeout\")" ] }, { "cell_type": "raw", "id": "4d784930-1a1e-482c-ae2d-7491a4066b47", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Setting the value generates a warning. The new value is saved into the config file, but :func:`get ` still returns the value of the environment variable." ] }, { "cell_type": "code", "execution_count": 6, "id": "8ee6f835-ae42-4eae-bc96-a53c47f3ce06", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/cgr/git/earthkit-regrid/src/earthkit/regrid/utils/config.py:362: UserWarning: Config option 'url-download-timeout' is also set by environment variable 'EARTHKIT_REGRID_URL_DOWNLOAD_TIMEOUT'.The environment variable takes precedence and its value is returned when calling get().\n", " warnings.warn(msg)\n" ] }, { "data": { "text/plain": [ "26" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.set(\"url-download-timeout\", 10)\n", "config.get(\"url-download-timeout\")" ] }, { "cell_type": "raw", "id": "0a24b839-a4d1-4340-bd87-7d690f0511f7", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "The :func:`env` method gives details about the set environment variables." ] }, { "cell_type": "code", "execution_count": 7, "id": "9c56e84d-ff1e-4415-b924-b461d62ddbbd", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{'url-download-timeout': ('EARTHKIT_REGRID_URL_DOWNLOAD_TIMEOUT', '26')}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.env()" ] }, { "cell_type": "markdown", "id": "d3e74276-cf79-42f0-9b66-b59ac15829d3", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "When we dump the configuration the values set via environment variables are clearly indicated." ] }, { "cell_type": "code", "execution_count": 8, "id": "89a92e24-f7f5-4f04-bc9a-1d0d00f7c8cf", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
NameValueDefault
cache-policy'user''user'
check-out-of-date-urlsFalseFalse
download-out-of-date-urlsFalseFalse
matrix-memory-cache-policy'largest''largest'
matrix-memory-cache-strict-modeFalseFalse
maximum-cache-disk-usageNoneNone
maximum-cache-size53687091205368709120
maximum-matrix-memory-cache-size524288000524288000
temporary-cache-directory-rootNoneNone
temporary-directory-rootNoneNone
url-download-timeoutEARTHKIT_REGRID_URL_DOWNLOAD_TIMEOUT='26'
(10)
'30s'
user-cache-directory'/Users/cgr/.cache/earthkit-regrid''/Users/cgr/.cache/earthkit-regrid'
version'0.4.1.dev2+g389301b.d20250120'''
" ], "text/plain": [ "cache-policy: (user, user)\n", "check-out-of-date-urls: (False, False)\n", "download-out-of-date-urls: (False, False)\n", "matrix-memory-cache-policy: (largest, largest)\n", "matrix-memory-cache-strict-mode: (False, False)\n", "maximum-cache-disk-usage: (None, None)\n", "maximum-cache-size: (5368709120, 5368709120)\n", "maximum-matrix-memory-cache-size: (524288000, 524288000)\n", "temporary-cache-directory-root: (None, None)\n", "temporary-directory-root: (None, None)\n", "url-download-timeout: (EARTHKIT_REGRID_URL_DOWNLOAD_TIMEOUT=26, 10, 30s)\n", "user-cache-directory: (/Users/cgr/.cache/earthkit-regrid, /Users/cgr/.cache/earthkit-regrid)\n", "version: (0.4.1.dev2+g389301b.d20250120, )" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config" ] }, { "cell_type": "code", "execution_count": null, "id": "943ec49a-b11f-490f-9c58-7b942eaaeaa9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "dev_ecc", "language": "python", "name": "dev_ecc" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }