Skip to content

Commit

Permalink
Revert "Implement nested interpolations (#308)"
Browse files Browse the repository at this point in the history
This reverts commit 7d5f8fc.
This functionality is being re-implemented with a proper parser generator
and will come in OmegaConf 2.1
  • Loading branch information
omry committed Aug 15, 2020
1 parent 368504d commit ff7e411
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 356 deletions.
89 changes: 12 additions & 77 deletions docs/notebook/Tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@
"\n",
"OmegaConf support variable interpolation, Interpolations are evaluated lazily on access.\n",
"\n",
"## Config node interpolation"
"### Config node interpolation"
]
},
{
Expand Down Expand Up @@ -618,46 +618,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Interpolations may be nested, enabling more advanced behavior like dynamically selecting a sub-config:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Default: cfg.plan = plan A\n",
"After selecting plan B: cfg.plan = plan B\n"
]
}
],
"source": [
"cfg = OmegaConf.create(\"\"\"\n",
"plans:\n",
" A: plan A\n",
" B: plan B\n",
"selected_plan: A\n",
"plan: ${plans.${selected_plan}}\n",
"\"\"\")\n",
"print(f\"Default: cfg.plan = {cfg.plan}\")\n",
"cfg.selected_plan = \"B\"\n",
"print(f\"After selecting plan B: cfg.plan = {cfg.plan}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Environment variable interpolation\n",
"### Environment variable interpolation\n",
"\n",
"Environment variable interpolation is also supported.\n",
"\n",
Expand All @@ -666,7 +627,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -684,7 +645,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 21,
"metadata": {
"pycharm": {
"name": "#%%\n"
Expand All @@ -709,7 +670,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 22,
"metadata": {
"pycharm": {
"name": "#%%\n"
Expand Down Expand Up @@ -741,7 +702,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 23,
"metadata": {},
"outputs": [
{
Expand All @@ -768,12 +729,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"You can add additional interpolation types using custom resolvers, which take strings as inputs. This example creates a resolver that adds 10 to the given value (note the need to cast it to `int`)."
"You can add additional interpolation types using custom resolvers. This example creates a resolver that adds 10 the the given value."
]
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 24,
"metadata": {
"pycharm": {
"name": "#%%\n"
Expand All @@ -794,32 +755,6 @@
"print(conf.key)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can take advantage of nested interpolations to perform operations over variables:"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"OmegaConf.register_resolver(\"plus_int\", lambda x, y: int(x) + int(y))\n",
"conf = OmegaConf.create({\"a\": 1, \"b\": 2, \"a_plus_b\": \"${plus_int:${a},${b}}\"})\n",
"print(conf.a_plus_b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -843,7 +778,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 25,
"metadata": {
"pycharm": {
"name": "#%%\n"
Expand All @@ -870,7 +805,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 26,
"metadata": {
"pycharm": {
"name": "#%%\n"
Expand All @@ -894,7 +829,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 27,
"metadata": {
"pycharm": {
"name": "#%%\n"
Expand Down Expand Up @@ -954,7 +889,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
"version": "3.8.1"
},
"pycharm": {
"stem_cell": {
Expand Down
33 changes: 2 additions & 31 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -286,23 +286,6 @@ Example:
>>> print(type(conf.client.url).__name__)
str

Interpolations may be nested, enabling more advanced behavior like dynamically selecting a sub-config:

.. doctest::

>>> cfg = OmegaConf.create("""
... plans:
... A: plan A
... B: plan B
... selected_plan: A
... plan: ${plans.${selected_plan}}
... """)
>>> print(cfg.plan) # default plan
plan A
>>> cfg.selected_plan = "B"
>>> print(cfg.plan) # new plan
plan B


Environment variable interpolation
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -339,8 +322,8 @@ The following example sets `12345` as the the default value for the `DB_PASSWORD

Custom interpolations
^^^^^^^^^^^^^^^^^^^^^
You can add additional interpolation types using custom resolvers, which take strings as inputs.
This example creates a resolver that adds 10 to the given value (note the need to cast it to `int`).
You can add additional interpolation types using custom resolvers.
This example creates a resolver that adds 10 the the given value.

.. doctest::

Expand All @@ -353,7 +336,6 @@ This example creates a resolver that adds 10 to the given value (note the need t
Custom resolvers support variadic argument lists in the form of a comma separated list of zero or more values.
Whitespaces are stripped from both ends of each value ("foo,bar" is the same as "foo, bar ").
You can use literal commas and spaces anywhere by escaping (:code:`\,` and :code:`\ `).

.. doctest::

>>> OmegaConf.register_resolver("concat", lambda x,y: x+y)
Expand All @@ -369,17 +351,6 @@ You can use literal commas and spaces anywhere by escaping (:code:`\,` and :code
>>> c.escape_whitespace
'Hello World'

You can take advantage of nested interpolations to perform operations over variables:

.. doctest::

>>> OmegaConf.register_resolver("plus_int",
... lambda x, y: int(x) + int(y))
>>> c = OmegaConf.create({"a": 1,
... "b": 2,
... "a_plus_b": "${plus_int:${a},${b}}"})
>>> c.a_plus_b
3


Merging configurations
Expand Down
1 change: 0 additions & 1 deletion news/308.feature

This file was deleted.

2 changes: 0 additions & 2 deletions omegaconf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from .base import Container, Node
from .dictconfig import DictConfig
from .errors import (
InterpolationParseError,
KeyValidationError,
MissingMandatoryValue,
ReadonlyConfigError,
Expand Down Expand Up @@ -37,7 +36,6 @@
"ReadonlyConfigError",
"UnsupportedValueType",
"KeyValidationError",
"InterpolationParseError",
"Container",
"ListConfig",
"DictConfig",
Expand Down
9 changes: 2 additions & 7 deletions omegaconf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,10 @@ def get_value_kind(value: Any, return_match_list: bool = False) -> Any:
"""
Determine the kind of a value
Examples:
MANDATORY_MISSING : "???"
MANDATORY_MISSING : "???
VALUE : "10", "20", True,
INTERPOLATION: "${foo}", "${foo.bar}"
STR_INTERPOLATION: "ftp://${host}/path", "${foo.${bar}}"
Note in particular that in the current implementation, "${foo.${bar}}" is
identified as a string interpolation (`STR_INTERPOLATION`) while it is
actually a (nested) simple interpolation. This discrepancy w.r.t naming
conventions will be addressed in a future update.
STR_INTERPOLATION: "ftp://${host}/path"
:param value: input string to classify
:param return_match_list: True to return the match list as well
Expand Down
Loading

0 comments on commit ff7e411

Please sign in to comment.