Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit b5b2955

Browse files
committed
df: types: Add subspec parameter to Definition
Signed-off-by: John Andersen <[email protected]>
1 parent a9da2d5 commit b5b2955

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Added
99
- Tensorflow hub NLP models.
1010
- Notes on development dependencies in `setup.py` files to codebase notes.
11+
### Changed
12+
- Definitions with a `spec` can use the `subspec` parameter to declare that they
13+
are a list or a dict where the values are of the `spec` type. Rather than the
14+
list or dict itself being of the `spec` type.
1115

1216
## [0.3.3] - 2020-02-10
1317
### Added

dffml/df/types.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class Definition(NamedTuple):
3737
lock: bool = False
3838
# spec is a NamedTuple which could be populated via a dict
3939
spec: NamedTuple = None
40+
# subspec is when your input is a list or dict of values which conform to
41+
# the spec
42+
subspec: bool = False
4043
# validate property will be a callable (function or lambda) which returns
4144
# the sanitized version of the value
4245
validate: Callable[[Any], Any] = None
@@ -274,8 +277,16 @@ def __init__(
274277
# instance name this Input is intended for.
275278
if parents is None:
276279
parents = []
277-
if isinstance(value, dict) and definition.spec is not None:
278-
value = definition.spec(**value)
280+
if definition.spec is not None:
281+
if definition.subspec:
282+
if isinstance(value, list):
283+
for i, subvalue in enumerate(value):
284+
value[i] = definition.spec(**subvalue)
285+
elif isinstance(value, dict):
286+
for key, subvalue in value.items():
287+
value[key] = definition.spec(**subvalue)
288+
elif isinstance(value, dict):
289+
value = definition.spec(**value)
279290
if definition.validate is not None:
280291
value = definition.validate(value)
281292
self.value = value

0 commit comments

Comments
 (0)