Section ยป field

field(name) โ†’ value or None
field(name, options) โ†’ object/value or None
field(name, loader, options) โ†’ value or None

Retrieve a field's value from the section, optionally supplying a loader to validate and/or transform the value, and/or an options object.

enocolor: blue
sound:
python>>> document = enopy.parse(input)

>>> document.field('color')
'blue'
>>> document.field('sound')
None
>>> document.field('sound', required=True)
ValidationError: ...
>>> document.field('sound', enforce_value=True)    
ValidationError: ...
>>> document.field('sound', enforce_element=True)    
None
>>> document.field('texture', enforce_element=True)
ValidationError: ...
>>> document.field('color', lambda name, value: value.upper())  
'BLUE'

>>> def color_validator(name, value):
...   if value != 'green':
...     raise ValueError('Only green is allowed!')
...   return value
>>> document.field('color', color_validator)
ValidationError: Only green is allowed!

>>> document.field('color', with_element=True)
[ <class Field name="color" value="blue">, 'blue' ]

Parameters

name

A string representing the name of the field to return.

loader

A function returning the transformed/validated value or raising an error.

options

enforce_element

Whether the field must be present in the document (True or False, defaults to False)

enforce_value

Whether there must be a value or the field is allowed to be empty (defaults to False)

required

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

with_element

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

Return value

The value of the field, or None if empty, or a list of the form [element, value]when the option with_element is specified.