Section ยป list

list(name) โ†’ list
list(name, options) โ†’ list
list(name, loader, options) โ†’ list

Retrieve the items of a list, optionally supplying a loader and options.

enocolors:
- pink
- peach

textures:
-
python>>> document = enopy.parse(input)

>>> document.list('colors')
[ 'pink', 'peach' ]
>>> document.list('sounds')
[]
>>> document.list('textures')                         
ValidationError: ...
>>> document.list('textures', enforce_values=False)
[ None ]
>>> document.list('sounds', required=True)         
ValidationError: ...
>>> document.list('sounds', enforce_element=True)  
ValidationError: ...
>>> document.list('colors', lambda name, value: value.upper())
[ 'PINK', 'PEACH' ]

>>> def color_validator(name, value):
...   if value == 'peach':
...     raise ValueError('Peach may not be on the list!')
...   return f"I like {value}"
>>> document.list('colors', color_validator)
ValidationError: Peach may not be on the list!

>>> document.list('colors', with_elements=True)
[[ <class Field value="pink">, 'pink' ],
 [ <class Field value="peach">, 'peach' ]]
  
>>> document.list('colors', min_count=3)
ValidationError: ...

Parameters

name

The name of the list to return as a string.

loader

A function returning the transformed/validated value or raising an error.(The function is applied to each list item on its own, and for the parametersignature you can choose between just value as single parameter or name, value- in that order - if you need both)

options

enforce_element

Whether the list must be present in the document (defaults to False)

enforce_values

Whether empty list items (- in the document, mapping to None) are disallowed (defaults to True)

exact_count

Validate that there are exactly n items in the list (takes a number, defaults to None)

min_count

Validate that there are at least n items in the list (takes a number, defaults to None)

max_count

Validate that there are at most n items in the list (takes a number, defaults to None)

required

Alias for enforce_element (this exists on many methods and depending on context refers to either element or value)

with_elements

Whether to return a list of lists of the form [element, value] instead of just the values (defaults to False)

Return value

The (optionally transformed/validated) values of the items of this list as a list.With with_elements set to True it returns a list of lists of the form [element, value] instead.