Skip to content

Commit

Permalink
Enable %f pattern in ${now} resolver (#1287)
Browse files Browse the repository at this point in the history
  • Loading branch information
forcecore authored Jan 10, 2021
1 parent 270d026 commit 4d7c953
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions hydra/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions news/1287.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for %f directive (microseconds) to the ${now:} resolver
18 changes: 18 additions & 0 deletions tests/test_hydra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion website/docs/configure_hydra/Intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-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`.
Expand Down

0 comments on commit 4d7c953

Please sign in to comment.