Skip to content

Commit

Permalink
Show example of quoted string as default value for oc.env
Browse files Browse the repository at this point in the history
  • Loading branch information
odelalleau committed Mar 16, 2021
1 parent c6a7c86 commit e63e669
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
30 changes: 18 additions & 12 deletions docs/notebook/Tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,9 @@
"metadata": {},
"source": [
"You can specify a default value to use in case the environment variable is not defined.\n",
"This default value must be a string (with the exception of `${oc.env:VAR,null}` that returns `None` if `VAR` is not defined).\n",
"This default value can be a string or ``null`` (representing Python ``None``). Passing a default with a different type will result in an error.\n",
"\n",
"The following example sets `abc123` as the the default value when `DB_PASSWORD` is not defined:"
"The following example sets default database passwords when ``DB_PASSWORD`` is not defined:"
]
},
{
Expand All @@ -786,20 +786,26 @@
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'abc123'"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"'abc123'\n",
"'12345'\n"
]
}
],
"source": [
"os.environ.pop('DB_PASSWORD', None) # ensure env variable does not exist\n",
"cfg = OmegaConf.create({'database': {'password': '${oc.env:DB_PASSWORD,abc123}'}})\n",
"cfg.database.password"
"cfg = OmegaConf.create(\n",
" {\n",
" \"database\": {\n",
" \"password1\": \"${oc.env:DB_PASSWORD,abc123}\", # the string 'abc123'\n",
" \"password2\": \"${oc.env:DB_PASSWORD,'12345'}\", # the string '12345'\n",
" },\n",
" }\n",
")\n",
"print(repr(cfg.database.password1))\n",
"print(repr(cfg.database.password2))"
]
},
{
Expand Down
19 changes: 13 additions & 6 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -404,16 +404,23 @@ Input YAML file:
'/home/omry'

You can specify a default value to use in case the environment variable is not defined.
This default value can be a string or `null` (representing Python `None`). Passing a default with a different type will result in an error.
The following example sets ``abc123`` as the default value when ``DB_PASSWORD`` is not defined.
This default value can be a string or ``null`` (representing Python ``None``). Passing a default with a different type will result in an error.
The following example sets default database passwords when ``DB_PASSWORD`` is not defined:

.. doctest::

>>> cfg = OmegaConf.create({
... 'database': {'password': '${oc.env:DB_PASSWORD,abc123}'}
... })
>>> cfg.database.password
>>> cfg = OmegaConf.create(
... {
... "database": {
... "password1": "${oc.env:DB_PASSWORD,abc123}",
... "password2": "${oc.env:DB_PASSWORD,'12345'}",
... },
... }
... )
>>> cfg.database.password1 # the string 'abc123'
'abc123'
>>> cfg.database.password2 # the string '12345'
'12345'


Decoding strings with interpolations
Expand Down

0 comments on commit e63e669

Please sign in to comment.