From 634cecdaadc6aa70bebd92dc7e33b0b9034160c7 Mon Sep 17 00:00:00 2001 From: ForceCore Date: Sun, 10 Jan 2021 10:40:52 +0900 Subject: [PATCH 1/3] Enable %f strftime directive in now resolver --- hydra/core/utils.py | 4 ++-- news/1287.bugfix | 1 + tests/test_hydra.py | 18 ++++++++++++++++++ website/docs/configure_hydra/Intro.md | 2 +- 4 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 news/1287.bugfix diff --git a/hydra/core/utils.py b/hydra/core/utils.py index 4c58938b963..f9d572a6e7f 100644 --- a/hydra/core/utils.py +++ b/hydra/core/utils.py @@ -7,9 +7,9 @@ import warnings from contextlib import contextmanager from dataclasses import dataclass +from datetime import datetime from os.path import basename, dirname, splitext from pathlib import Path -from time import localtime, strftime from typing import Any, Dict, Optional, Sequence, Tuple, Union, cast from omegaconf import DictConfig, OmegaConf, open_dict, read_write @@ -151,7 +151,7 @@ def register(name: str, f: Any) -> None: pass # please add documentation when you add a new resolver - register("now", lambda pattern: strftime(pattern, localtime())) + register("now", lambda pattern: datetime.now().strftime(pattern)) register( "hydra", lambda path: OmegaConf.select(cast(DictConfig, HydraConfig.get()), path), diff --git a/news/1287.bugfix b/news/1287.bugfix new file mode 100644 index 00000000000..2216eca147e --- /dev/null +++ b/news/1287.bugfix @@ -0,0 +1 @@ +Bug fix to make %f strftime directive to work for ${now:} resolver diff --git a/tests/test_hydra.py b/tests/test_hydra.py index 597cd3bd90b..691805e15a1 100644 --- a/tests/test_hydra.py +++ b/tests/test_hydra.py @@ -730,6 +730,24 @@ def test_local_run_workdir( ) +@pytest.mark.parametrize( + "task_config", + [ + ({"hydra": {"run": {"dir": "${now:%Y%m%d_%H%M%S_%f}"}}}), + ], +) +def test_run_dir_microseconds(tmpdir: Path, task_config: DictConfig) -> None: + cfg = OmegaConf.create(task_config) + assert isinstance(cfg, DictConfig) + integration_test( + tmpdir=tmpdir, + task_config=cfg, + overrides=[], + prints="str('%f' not in os.getcwd())", + expected_outputs="True", + ) + + def test_hydra_env_set_with_config(tmpdir: Path) -> None: cfg = OmegaConf.create({"hydra": {"job": {"env_set": {"foo": "bar"}}}}) integration_test( diff --git a/website/docs/configure_hydra/Intro.md b/website/docs/configure_hydra/Intro.md index 57c2eb68bdc..270aa4e9299 100644 --- a/website/docs/configure_hydra/Intro.md +++ b/website/docs/configure_hydra/Intro.md @@ -92,7 +92,7 @@ Hydra supports several [OmegaConf resolvers](https://github.com/facebookresearch **hydra**: Interpolates into the `hydra` config node. e.g. Use `${hydra:job.name}` to get the Hydra job name. **now**: Creates a string representing the current time using -[strftime](https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior). +[strftime](https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior). e.g. for formatting the time you can use something like`${now:%H-%M-%S}`. **python_version**: Return a string representing the runtime python version by calling `sys.version_info`. From 9c4e72c4aab86f2560cb8e2edde0df1a255bf019 Mon Sep 17 00:00:00 2001 From: Omry Yadan Date: Sun, 10 Jan 2021 02:10:41 -0800 Subject: [PATCH 2/3] Update 1287.bugfix --- news/1287.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/news/1287.bugfix b/news/1287.bugfix index 2216eca147e..8c4a2d9b2f3 100644 --- a/news/1287.bugfix +++ b/news/1287.bugfix @@ -1 +1 @@ -Bug fix to make %f strftime directive to work for ${now:} resolver +Add support for %f directive (microseconds) to the ${now:} resolver From 87d3a433812b3e8b37996da01c642b9d9356bade Mon Sep 17 00:00:00 2001 From: ForceCore Date: Sun, 10 Jan 2021 19:42:14 +0900 Subject: [PATCH 3/3] Update strftime reference link in doc Intro.md modified: website/docs/configure_hydra/Intro.md --- website/docs/configure_hydra/Intro.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/configure_hydra/Intro.md b/website/docs/configure_hydra/Intro.md index 270aa4e9299..cefca7fb0fb 100644 --- a/website/docs/configure_hydra/Intro.md +++ b/website/docs/configure_hydra/Intro.md @@ -92,7 +92,7 @@ Hydra supports several [OmegaConf resolvers](https://github.com/facebookresearch **hydra**: Interpolates into the `hydra` config node. e.g. Use `${hydra:job.name}` to get the Hydra job name. **now**: Creates a string representing the current time using -[strftime](https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior). +[strftime](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior). e.g. for formatting the time you can use something like`${now:%H-%M-%S}`. **python_version**: Return a string representing the runtime python version by calling `sys.version_info`.