dag_cbor.ipld

Types and functions relating to the IPLD data model IPLD data model.

IPLDKind

IPLDKind

Python type alias for kinds in the IPLD data model:

alias of Union[None, bool, int, float, str, bytes, CID, List[IPLDKind], Dict[str, IPLDKind]]

IPLDObjPath

class IPLDObjPath(*segments)[source]

Bases: Sequence[Union[int, str]]

Path within an object of IPLDKind, as a sequence of IPLDObjPathSegment. Paths are immutable and hashable, and a path is a Sequence of the segments that constitute it.

__le__(other)[source]

The < and <= operators can be used to check whether a path is a (strict) sub-path of another path, starting at the same root:

>>> _ = IPLDObjPath()
>>> p = _/0/'red'
>>> q = p/1/2
>>> p == q
False
>>> p <= q
True
>>> p < q
True
Parameters:

other (IPLDObjPath) –

Return type:

bool

__lt__(other)[source]

See IPLDObjPath.__le__.

Parameters:

other (IPLDObjPath) –

Return type:

bool

static __new__(cls, *segments)[source]

Constructor for IPLDObjPath.

Parameters:

segments (IPLDObjPathSegment; variadic positional) –

Return type:

IPLDObjPath

__repr__()[source]
return "/"+"/".join(repr(seg) for seg in self)
Return type:

str

__rshift__(value)[source]

Accesses the sub-value at this path in the given IPLD value:

>>> _ = IPLDObjPath()
>>> _ >> [0, False, {"a": b"hello", "b": "bye"}]
[0, False, {'a': b'hello', 'b': 'bye'}]
>>> _/2 >> [0, False, {"a": b"hello", "b": "bye"}]
{'a': b'hello', 'b': 'bye'}
>>> _/2/'b' >> [0, False, {"a": b"hello", "b": "bye"}]
'bye'
Raises:
  • ValueError – if attempting to access a sub-value in a value of IPLDScalarKind

  • ValueError – if attempting to access a sub-value indexed by a str segment in a value of list IPLDKind (a Python List)

  • ValueError – if attempting to access a sub-value keyed by a int segment in a value of map IPLDKind (a Python Dict)

  • IndexError – if attempting to access a sub-value in a value of list kind, where the int segment is not a valid index for the list

  • KeyError – if attempting to access a sub-value in a value of map kind, where the str segment is not a valid key for the map

  • TypeError – if any of the sub-values along the path is not of IPLD IPLDKind at the top level

Parameters:

value (IPLDKind) –

Return type:

IPLDKind

__rtruediv__(other)[source]

It is possible to prepend a single segment at a time to an existing path using / (a new path is returned):

>>> _ = IPLDObjPath()
>>> p = _/2/'red'
>>> 1/p
/1/2/'red'

Prepending multiple segments requires brackets (because the / operator associates to the left):

>>> 0/(1/p)
/0/1/2/'red'
Parameters:

other (Union[IPLDObjPathSegment, IPLDObjPath]) –

Return type:

IPLDObjPath

__truediv__(other)[source]

The / operator can be used to create paths by concatenating segments. Below we use _ as a suggestive name for an empty path, acting as root:

>>> _ = IPLDObjPath()
>>> p = _/2/'red'
>>> p
/2/'red'

Concatenating an existing path with one or more segments returns a new path, extended by the given segments:

>>> p/3
/2/'red'/3
>>> p/0/'blue'
/2/'red'/0/'blue'

Concatenating two paths yields a new path, where the end of the first path is treated as the root for the second:

>>> q = _/0/'blue'
>>> p/q
/2/'red'/0/'blue'
Parameters:

other (Union[IPLDObjPathSegment, IPLDObjPath]) –

Return type:

IPLDObjPath

access(value)[source]

Accesses the sub-value at this path in the given IPLD value. Can be written more expressively as self >> value, see IPLDObjPath.__rshift__.

Parameters:

value (IPLDKind) –

Return type:

IPLDKind

static parse(path_str)[source]

Parses a IPLDObjPath from a string representation where segments are separated by “/”, such as that returned by IPLDObjPath.__repr__.

Parameters:

path_str (str) –

Return type:

IPLDObjPath

IPLDObjPathSegment

IPLDObjPathSegment

An individual segment in a IPLDObjPath within a IPLD value (see IPLDKind for the ). A segment can be an int or a str:

  • an int segment is a position, indexing an item in a value of List IPLDKind (a List in Python)

  • an str segment is a key, indexing a value in a value of Map IPLDKind (a Dict in Python)

alias of Union[int, str]

IPLDScalarKind

IPLDScalarKind

Python type alias for scalar kinds in the IPLD data model:

alias of Union[None, bool, int, float, str, bytes, CID]