Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions fiddle/_src/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,23 +338,40 @@ def make_previous_text(entry: history.HistoryEntry) -> str:
return f'{_path_str(path)} = {current}{past}'


def as_dict_flattened(cfg: config.Buildable) -> Dict[str, Any]:
def as_dict_flattened(
cfg: config.Buildable, output_fn_or_cls_name: bool = False
) -> Dict[str, Any]:
"""Returns a flattened dict of cfg's paths (dot syntax) and values.

Default values and tags won't be included in the flattened dict.

Args:
cfg: A buildable to generate a flattened dict representation for.
output_fn_or_cls_name: If true, output the function or class name for
buildables.

Returns: A flattened Dict representation of `cfg`.
"""

def dict_generate(value, state=None) -> Iterator[_LeafSetting]:
state = state or daglish.BasicTraversal.begin(dict_generate, value)

# Rearrange parameters in signature order.
if isinstance(value, config.Buildable):
# Rearrange parameters in signature order.
value = _rearrange_buildable_args(value, insert_unset_sentinels=False)
# Add the function or class name if it exists.
if (
output_fn_or_cls_name
and hasattr(value, '__fn_or_cls__')
and hasattr(value.__fn_or_cls__, '__name__')
):
current_path_with_fn_or_cls_name = state.current_path + (
daglish.Attr('__fn_or_cls__'),
daglish.Attr('__name__'),
)
yield _LeafSetting(
current_path_with_fn_or_cls_name, None, value.__fn_or_cls__.__name__
)

if not _has_nested_builder(value):
yield _LeafSetting(state.current_path, None, value)
Expand Down
31 changes: 31 additions & 0 deletions fiddle/_src/printing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,13 @@ def test_simple_flattened_dict(self):
expected = {'x': 1, 'y': 'abc'}
self.assertEqual(output, expected)

def test_simple_flattened_dict_fn_name(self):
cfg = fdl.Config(fn_x_y, 1, 'abc')
output = printing.as_dict_flattened(cfg, output_fn_or_cls_name=True)

expected = {'__fn_or_cls__.__name__': 'fn_x_y', 'x': 1, 'y': 'abc'}
self.assertEqual(output, expected)

def test_skip_unset_argument(self):
cfg = fdl.Config(fn_x_y, 3.14)
output = printing.as_dict_flattened(cfg)
Expand All @@ -507,13 +514,37 @@ def test_nested(self):
expected = {'x': 'x', 'y.x': 'nest_x', 'y.y': 123}
self.assertEqual(output, expected)

def test_nested_fn_name(self):
cfg = fdl.Config(fn_x_y, 'x', fdl.Config(fn_x_y, 'nest_x', 123))
output = printing.as_dict_flattened(cfg, output_fn_or_cls_name=True)

expected = {
'__fn_or_cls__.__name__': 'fn_x_y',
'x': 'x',
'y.__fn_or_cls__.__name__': 'fn_x_y',
'y.x': 'nest_x',
'y.y': 123,
}
self.assertEqual(output, expected)

def test_class(self):
cfg = fdl.Config(SampleClass, 'a_param', b=123)
output = printing.as_dict_flattened(cfg)

expected = {'a': 'a_param', 'b': 123}
self.assertEqual(output, expected)

def test_class_cls_name(self):
cfg = fdl.Config(SampleClass, 'a_param', b=123)
output = printing.as_dict_flattened(cfg, output_fn_or_cls_name=True)

expected = {
'__fn_or_cls__.__name__': 'SampleClass',
'a': 'a_param',
'b': 123,
}
self.assertEqual(output, expected)

def test_kwargs(self):
cfg = fdl.Config(fn_with_kwargs, 1, abc='extra kwarg value')
output = printing.as_dict_flattened(cfg)
Expand Down