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

Enable %f pattern in now resolver #1287

Merged
merged 3 commits into from Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions hydra/core/utils.py
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
@@ -0,0 +1 @@
Add support for %f directive (microseconds) to the ${now:} resolver
18 changes: 18 additions & 0 deletions tests/test_hydra.py
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())",
omry marked this conversation as resolved.
Show resolved Hide resolved
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
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