Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OmegaConf.to_yaml #313

Merged
merged 9 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 58 additions & 44 deletions docs/notebook/Tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
],
"source": [
"conf = OmegaConf.create(dict(k='v',list=[1,dict(a='1',b='2')]))\n",
"print(conf.pretty())"
"print(OmegaConf.to_yaml(conf))"
]
},
{
Expand Down Expand Up @@ -108,7 +108,7 @@
],
"source": [
"conf = OmegaConf.create([1, dict(a=10, b=dict(a=10))])\n",
"print(conf.pretty())"
"print(OmegaConf.to_yaml(conf))"
]
},
{
Expand Down Expand Up @@ -145,7 +145,7 @@
],
"source": [
"conf = OmegaConf.load('../source/example.yaml')\n",
"print(conf.pretty())"
"print(OmegaConf.to_yaml(conf))"
]
},
{
Expand Down Expand Up @@ -186,7 +186,7 @@
"- item2\n",
"\"\"\"\n",
"conf = OmegaConf.create(yaml)\n",
"print(conf.pretty())"
"print(OmegaConf.to_yaml(conf))"
]
},
{
Expand Down Expand Up @@ -223,7 +223,7 @@
"source": [
"dot_list = [\"a.aa.aaa=1\", \"a.aa.bbb=2\", \"a.bb.aaa=3\", \"a.bb.bbb=4\"]\n",
"conf = OmegaConf.from_dotlist(dot_list)\n",
"print(conf.pretty())"
"print(OmegaConf.to_yaml(conf))"
]
},
{
Expand Down Expand Up @@ -261,7 +261,7 @@
"import sys\n",
"sys.argv = ['your-program.py', 'server.port=82', 'log.file=log2.txt']\n",
"conf = OmegaConf.from_cli()\n",
"print(conf.pretty())"
"print(OmegaConf.to_yaml(conf))"
]
},
{
Expand Down Expand Up @@ -299,7 +299,7 @@
],
"source": [
"conf = OmegaConf.load('../source/example.yaml')\n",
"print(conf.pretty())"
"print(OmegaConf.to_yaml(conf))"
]
},
{
Expand Down Expand Up @@ -528,9 +528,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Interpolations are evaluated lazily on field access.<br/>\n",
"Note below that when printed the interpolations are not resolved.<br/>\n",
"They get resolved once you access them."
"Interpolations are evaluated lazily on field access. OmegaConf.to_yaml() is eagerly resolving the interpolations by default."
]
},
{
Expand All @@ -550,47 +548,52 @@
" host: localhost\n",
" port: 80\n",
"client:\n",
" url: http://${server.host}:${server.port}/\n",
" server_port: ${server.port}\n",
" url: http://localhost:80/\n",
" server_port: 80\n",
"\n"
]
}
],
"source": [
"conf = OmegaConf.load('../source/config_interpolation.yaml')\n",
"print(conf.pretty())"
"print(OmegaConf.to_yaml(conf))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can change it by passing `resolve=False`."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"conf.client.server_port: 80 int\n",
"conf.client.url: http://localhost:80/ str\n"
"server:\n",
" host: localhost\n",
" port: 80\n",
"client:\n",
" url: http://${server.host}:${server.port}/\n",
" server_port: ${server.port}\n",
"\n"
]
}
],
"source": [
"# Primitive interpolation types are inherited from the referenced value\n",
"print(\"conf.client.server_port: \", conf.client.server_port, type(conf.client.server_port).__name__)\n",
"# Composite interpolation types are always string\n",
"print(\"conf.client.url: \", conf.client.url, type(conf.client.url).__name__)"
"print(OmegaConf.to_yaml(conf, resolve=False))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can resolve interpolation while you are printing using `resolve=True`."
"Notice that the url and server_port changes with the port."
]
},
{
Expand All @@ -602,18 +605,17 @@
"name": "stdout",
"output_type": "stream",
"text": [
"server:\n",
" host: localhost\n",
" port: 80\n",
"client:\n",
" url: http://localhost:80/\n",
" server_port: 80\n",
"\n"
"conf.client.server_port: 90 int\n",
"conf.client.url: http://localhost:90/ str\n"
]
}
],
"source": [
"print(conf.pretty(resolve=True))"
"conf.server.port = 90\n",
"# Primitive interpolation types are inherited from the referenced value\n",
"print(\"conf.client.server_port: \", conf.client.server_port, type(conf.client.server_port).__name__)\n",
"# Composite interpolation types are always string\n",
"print(\"conf.client.url: \", conf.client.url, type(conf.client.url).__name__)"
]
},
{
Expand All @@ -626,7 +628,12 @@
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"scrolled": true
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -693,15 +700,15 @@
"output_type": "stream",
"text": [
"user:\n",
" name: ${env:USER}\n",
" home: /home/${env:USER}\n",
" name: omry\n",
" home: /home/omry\n",
"\n"
]
}
],
"source": [
"conf = OmegaConf.load('../source/env_interpolation.yaml')\n",
"print(conf.pretty())"
"print(OmegaConf.to_yaml(conf))"
]
},
{
Expand All @@ -718,15 +725,15 @@
"output_type": "stream",
"text": [
"user:\n",
" name: omry\n",
" home: /home/omry\n",
" name: ${env:USER}\n",
" home: /home/${env:USER}\n",
"\n"
]
}
],
"source": [
"conf = OmegaConf.load('../source/env_interpolation.yaml')\n",
"print(conf.pretty(resolve=True))"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example was making a point of showing the effect of resolve.
you removed resolve, but now it's not making any point because resolve is True by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in the last commit, now the next cell shows wihout resolving.

"print(OmegaConf.to_yaml(conf, resolve=False))"
]
},
{
Expand Down Expand Up @@ -862,7 +869,7 @@
],
"source": [
"base_conf = OmegaConf.load('../source/example2.yaml')\n",
"print(base_conf.pretty())"
"print(OmegaConf.to_yaml(base_conf))"
]
},
{
Expand All @@ -886,7 +893,7 @@
],
"source": [
"second_conf = OmegaConf.load('../source/example3.yaml')\n",
"print(second_conf.pretty())"
"print(OmegaConf.to_yaml(second_conf))"
]
},
{
Expand Down Expand Up @@ -924,8 +931,15 @@
"sys.argv = ['program.py', 'server.port=82']\n",
"# Merge with cli arguments\n",
"conf.merge_with_cli()\n",
"print(conf.pretty())"
"print(OmegaConf.to_yaml(conf))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -944,7 +958,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
"version": "3.6.10"
},
"pycharm": {
"stem_cell": {
Expand Down
4 changes: 2 additions & 2 deletions docs/source/structured_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fields during construction.
>>> conf3 = OmegaConf.structured(
... SimpleTypes(num=20,
... height=Height.TALL))
>>> print(conf3.pretty())
>>> print(OmegaConf.to_yaml(conf3))
num: 20
pi: 3.1415
is_awesome: true
Expand Down Expand Up @@ -176,7 +176,7 @@ Structured configs can be nested.
... manager: User = User(name="manager", height=Height.TALL)

>>> conf : Group = OmegaConf.structured(Group)
>>> print(conf.pretty())
>>> print(OmegaConf.to_yaml(conf))
name: ???
admin:
name: ???
Expand Down
24 changes: 12 additions & 12 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Empty

>>> from omegaconf import OmegaConf
>>> conf = OmegaConf.create()
>>> print(conf.pretty())
>>> print(OmegaConf.to_yaml(conf))
{}
<BLANKLINE>

Expand All @@ -40,7 +40,7 @@ From a dictionary
.. doctest::

>>> conf = OmegaConf.create({"k" : "v", "list" : [1, {"a": "1", "b": "2"}]})
>>> print(conf.pretty())
>>> print(OmegaConf.to_yaml(conf))
k: v
list:
- 1
Expand All @@ -54,7 +54,7 @@ From a list
.. doctest::

>>> conf = OmegaConf.create([1, {"a":10, "b": {"a":10}}])
>>> print(conf.pretty())
>>> print(OmegaConf.to_yaml(conf))
- 1
- a: 10
b:
Expand All @@ -70,7 +70,7 @@ From a yaml file

>>> conf = OmegaConf.load('source/example.yaml')
>>> # Output is identical to the yaml file
>>> print(conf.pretty())
>>> print(OmegaConf.to_yaml(conf))
server:
port: 80
log:
Expand All @@ -95,7 +95,7 @@ From a yaml string
... - item2
... """
>>> conf = OmegaConf.create(s)
>>> print(conf.pretty())
>>> print(OmegaConf.to_yaml(conf))
a: b
b: c
list:
Expand All @@ -110,7 +110,7 @@ From a dot-list

>>> dot_list = ["a.aa.aaa=1", "a.aa.bbb=2", "a.bb.aaa=3", "a.bb.bbb=4"]
>>> conf = OmegaConf.from_dotlist(dot_list)
>>> print(conf.pretty())
>>> print(OmegaConf.to_yaml(conf))
a:
aa:
aaa: 1
Expand All @@ -130,7 +130,7 @@ To parse the content of sys.arg:
>>> # Simulating command line arguments
>>> sys.argv = ['your-program.py', 'server.port=82', 'log.file=log2.txt']
>>> conf = OmegaConf.from_cli()
>>> print(conf.pretty())
>>> print(OmegaConf.to_yaml(conf))
server:
port: 82
log:
Expand All @@ -153,7 +153,7 @@ See :doc:`structured_config` for more details, or keep reading for a minimal exa
... host: str = "localhost"
>>> # For strict typing purposes, prefer OmegaConf.structured() when creating structured configs
>>> conf = OmegaConf.structured(MyConfig)
>>> print(conf.pretty())
>>> print(OmegaConf.to_yaml(conf))
port: 80
host: localhost
<BLANKLINE>
Expand All @@ -163,7 +163,7 @@ You can use an object to initialize the config as well:
.. doctest::

>>> conf = OmegaConf.structured(MyConfig(port=443))
>>> print(conf.pretty())
>>> print(OmegaConf.to_yaml(conf))
port: 443
host: localhost
<BLANKLINE>
Expand Down Expand Up @@ -427,7 +427,7 @@ Note how the port changes to 82, and how the users lists are combined.
>>>
>>> # merge them all
>>> conf = OmegaConf.merge(base_conf, second_conf, cli_conf)
>>> print(conf.pretty())
>>> print(OmegaConf.to_yaml(conf))
server:
port: 82
users:
Expand Down Expand Up @@ -589,13 +589,13 @@ Creates a copy of a DictConfig that contains only specific keys.
.. doctest:: loaded

>>> conf = OmegaConf.create({"a": {"b": 10}, "c":20})
>>> print(conf.pretty())
>>> print(OmegaConf.to_yaml(conf))
a:
b: 10
c: 20
<BLANKLINE>
>>> c = OmegaConf.masked_copy(conf, ["a"])
>>> print(c.pretty())
>>> print(OmegaConf.to_yaml(c))
a:
b: 10
<BLANKLINE>
Loading