Utils

Collection of utilities for working with Odin as well as generic data manipulation.

Resources

odin.utils.getmeta(resource_or_instance)[source]

Get metaobject from a resource or resource instance.

Parameters:

resource_or_instance (Type[odin.resources.ResourceBase] | odin.resources.ResourceBase) – Resource or instance of a resource.

Returns:

Meta options class

Return type:

odin.resources.ResourceOptions

odin.utils.field_iter(resource, include_virtual: bool = True) Iterator[source]

Return an iterator that yields fields from a resource.

Parameters:
  • resource – Resource to iterate over.

  • include_virtual – Include virtual fields.

Returns:

an iterator that returns fields.

odin.utils.field_iter_items(resource, fields: Sequence | None = None) Iterator[Tuple[str, Any]][source]

Return an iterator that yields fields and their values from a resource.

Parameters:
  • resource – Resource to iterate over.

  • fields – Fields to use; if None defaults to all the resources fields.

Returns:

an iterator that returns (field, value) tuples.

odin.utils.virtual_field_iter_items(resource) Iterator[Tuple[str, Any]][source]

Return an iterator that yields virtual fields and their values from a resource.

Parameters:

resource – Resource to iterate over.

Returns:

an iterator that returns (field, value) tuples.

odin.utils.attribute_field_iter_items(resource) Iterator[Tuple[str, Any]][source]

Return an iterator that yields fields and their values from a resource that have the attribute flag set.

Parameters:

resource – Resource to iterate over.

Returns:

an iterator that returns (field, value) tuples.

Note::

This iterator is designed for codecs that have a distinction between attribute and element fields (eg XML).

odin.utils.element_field_iter_items(resource) Iterator[Tuple[str, Any]][source]

Return an iterator that yields fields and their values from a resource that do not have the attribute flag set.

Parameters:

resource – Resource to iterate over.

Returns:

an iterator that returns (field, value) tuples.

Note::

This iterator is designed for codecs that have a distinction between attribute and element fields (eg XML).

odin.utils.extract_fields_from_dict(d: Dict[str, Any], resource) Dict[str, Any][source]

Extract values from a dict that are defined on a resource.

Fields that are not found will not be included in the output dict.

Parameters:
  • d – the source dictionary.

  • resource – the resource that provides the fields.

Returns:

a dictionary of the resource fields that where found in the dict.

Name Manipulation

odin.utils.camel_to_lower_separated(s: str, sep: str) str[source]

Convert camel case representation into lower separated case ie:

backgroundColor -> background_color

Note any separator at the start or end is stripped.

odin.utils.camel_to_lower_underscore(s: str) str[source]

Convert camel case to lower underscore case.

backgroundColor -> background_color

odin.utils.camel_to_lower_dash(s: str) str[source]

Convert camel case to lower dash case.

backgroundColor -> background-color

odin.utils.lower_underscore_to_camel(value: str) str[source]

Convert lower underscore case to camel case

background_color -> backgroundColor

odin.utils.lower_dash_to_camel(value: str) str[source]

Convert lower dash case to camel case

background-color -> backgroundColor

Choice Generation

odin.utils.value_in_choices(value: Any, choices: List[Tuple[Any, str]]) bool[source]

Check if the value appears in the choices list (an iterable of tuples, the first value of which is the choice value).

Parameters:
  • value – Value to search for

  • choices – List of choices

Returns:

True if value is in the choices iterable.

odin.utils.iter_to_choices(i: Iterable[_T]) Sequence[Tuple[_T, str]][source]

Convert an iterator of strings (or types that can be converted to strings) and convert these into choice value pairs.

Iterables

odin.utils.chunk(iterable: Iterable[_T], n: int) Iterable[Iterable[_T]][source]

Return iterable of n items from an iterable.

Parameters:
  • iterable – Iterable of items

  • n – Size of iterable chunks to return.

Returns:

Iterable chunk of input iterable