modelity.loc

class modelity.loc.Loc(*path: Any)

Bases: Sequence

A tuple-like type that stores location of the value (or error) in the model tree.

Examples:

>>> from modelity.loc import Loc
>>> root = Loc("root")
>>> nested = root + Loc("nested")
>>> nested
Loc('root', 'nested')
>>> nested += Loc(0)
>>> nested
Loc('root', 'nested', 0)
>>> str(nested)
'root.nested.0'
>>> nested[0]
'root'
>>> nested[-1]
0
Parameters:

*args – The positional arguments composing location’s path.

__abstractmethods__ = frozenset({})
__add__(other)
__eq__(value: object) bool

Return self==value.

__getitem__(index)
__hash__() int

Return hash(self).

__init__(*path: Any)
__len__() int
__lt__(value: object) bool

Return self<value.

__orig_bases__ = (typing.Sequence,)
__parameters__ = ()
__repr__() str

Return repr(self).

__slots__ = ('_data',)
__str__() str

Return str(self).

classmethod irrelevant() Loc

Return a special location value indicating that the exact location is irrelevant.

This is equivalent to Loc("_") and is typically used in containers like sets or unordered structures, where the concept of position or path does not apply.

For example, when comparing or storing elements where their precise placement is not semantically meaningful, this sentinel location can be used to fulfill API requirements without implying an actual location.

Added in version 0.17.0.

is_empty() bool

Check if this is an empty location object.

is_parent_of(other: Loc) bool

Check if this location is parent (prefix) of given other location.

Parameters:

other – The other location object.

property last: Any

Return last component of the location.

suffix_match(pattern: Loc) bool

Check if suffix of this location matches given pattern.

Examples:

>>> Loc("foo").suffix_match(Loc("foo"))
True
>>> Loc("foo").suffix_match(Loc("foo", "bar"))
False
>>> Loc("foo", "bar").suffix_match(Loc("foo", "bar"))
True
>>> Loc("foo", "bar").suffix_match(Loc("foo", "*"))
True
>>> Loc("foo", 3, "bar").suffix_match(Loc("foo", "*", "bar"))
True
>>> Loc("foo", 3, "bar").suffix_match(Loc("foo", "*", "baz"))
False

Added in version 0.27.0.

to_tuple() tuple

Convert this location into tuple object.

Added in version 0.36.0.