Skip to content

Commit

Permalink
added utility functions fdict and flist
Browse files Browse the repository at this point in the history
  • Loading branch information
soldni committed Sep 16, 2022
1 parent ee28035 commit 2134fa3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "springs"
version = "1.4.1.1"
version = "1.4.2"
description = "A set of utilities to create and manage typed configuration files effectively, built on top of OmegaConf."
authors = [
{name = "Luca Soldaini", email = "[email protected]" }
Expand Down
30 changes: 29 additions & 1 deletion src/springs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass, field
from typing import Any, Callable, Optional, Type, TypeVar
from typing import Any, Callable, Dict, List, Optional, Type, TypeVar

from omegaconf import MISSING, DictConfig, ListConfig

Expand Down Expand Up @@ -60,6 +60,32 @@ def make_flexy(cls_: Any) -> Any:
return flexyclass(cls_)


def fdict(**kwargs: Any) -> Dict[str, Any]:
"""Shortcut for creating a Field with a default_factory that returns
a dictionary.
Args:
**kwargs: values for the dictionary returned by default factory"""

def _factory_fn() -> Dict[str, Any]:
return {**kwargs}

return field(default_factory=_factory_fn)


def flist(*args: Any) -> List[Any]:
"""Shortcut for creating a Field with a default_factory that returns
a list.
Args:
*args: values for the list returned by default factory"""

def _factory_fn() -> List[Any]:
return [*args]

return field(default_factory=_factory_fn)


__all__ = [
"all_resolvers",
"cast",
Expand All @@ -68,8 +94,10 @@ def make_flexy(cls_: Any) -> Any:
"dataclass",
"DictConfig",
"edit_list",
"fdict",
"field",
"flexyclass",
"flist",
"from_dataclass",
"from_dict",
"from_file",
Expand Down

0 comments on commit 2134fa3

Please sign in to comment.