From 2134fa3bb2200bb8fab6a1f85f53f96f6240fb9a Mon Sep 17 00:00:00 2001 From: Luca Soldaini Date: Fri, 16 Sep 2022 07:41:15 -0700 Subject: [PATCH] added utility functions fdict and flist --- pyproject.toml | 2 +- src/springs/__init__.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f273544..a0b3f50 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = "luca@soldaini.net" } diff --git a/src/springs/__init__.py b/src/springs/__init__.py index c98c8e1..57e45c1 100644 --- a/src/springs/__init__.py +++ b/src/springs/__init__.py @@ -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 @@ -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", @@ -68,8 +94,10 @@ def make_flexy(cls_: Any) -> Any: "dataclass", "DictConfig", "edit_list", + "fdict", "field", "flexyclass", + "flist", "from_dataclass", "from_dict", "from_file",