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:
boolfor the Boolean kindintfor the Integer kindfloatfor the Float kindstrfor the String kindbytesfor the Bytes kind
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 ofIPLDObjPathSegment. Paths are immutable and hashable, and a path is aSequenceof 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:
- __lt__(other)[source]
See
IPLDObjPath.__le__.- Parameters:
other (
IPLDObjPath) –- Return type:
- static __new__(cls, *segments)[source]
Constructor for
IPLDObjPath.- Parameters:
segments (
IPLDObjPathSegment; variadic positional) –- Return type:
- __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
IPLDScalarKindValueError – if attempting to access a sub-value indexed by a
strsegment in a value of listIPLDKind(a PythonList)ValueError – if attempting to access a sub-value keyed by a
intsegment in a value of mapIPLDKind(a PythonDict)IndexError – if attempting to access a sub-value in a value of list kind, where the
intsegment is not a valid index for the listKeyError – if attempting to access a sub-value in a value of map kind, where the
strsegment is not a valid key for the mapTypeError – if any of the sub-values along the path is not of IPLD
IPLDKindat the top level
- Parameters:
value (
IPLDKind) –- Return type:
- __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:
- __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:
- 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__.
- static parse(path_str)[source]
Parses a
IPLDObjPathfrom a string representation where segments are separated by “/”, such as that returned byIPLDObjPath.__repr__.- Parameters:
path_str (
str) –- Return type:
IPLDObjPathSegment
- IPLDObjPathSegment
An individual segment in a
IPLDObjPathwithin a IPLD value (seeIPLDKindfor the ). A segment can be anintor astr:
IPLDScalarKind
- IPLDScalarKind
Python type alias for scalar kinds in the IPLD data model:
boolfor the Boolean kindintfor the Integer kindfloatfor the Float kindstrfor the String kindbytesfor the Bytes kind