labapi.util.path.NotebookPath#
- class labapi.util.path.NotebookPath(
- part: NotebookPath | AbstractBaseTreeNode | EscapedSegment,
- *parts: EscapedSegment,
- parent: NotebookPath | AbstractBaseTreeNode | None = None,
Bases:
Sequence[UnescapedSegment]A structured path referencing a location in the notebook tree.
Supports absolute and relative paths, plus
..parent navigation.Empty segments and
.are discarded;..collapses the preceding segment, or is kept at the root of a relative path.If a node name contains a literal
/, write it as\/in a path string, e.g.r"Reports\/2024". Iteration, indexing,name, andpartsreturn node names.str()adds escapes back so the result can be parsed again.Examples:
# From a tree node (always absolute) path = NotebookPath(folder) # e.g. /Experiments/2024 # From a string path = NotebookPath("/Experiments/2024") # absolute path = NotebookPath("2024/Results") # relative # Combine with / path = NotebookPath(notebook) / "Experiments" / "2024"
- __init__(
- part: NotebookPath | AbstractBaseTreeNode | EscapedSegment,
- *parts: EscapedSegment,
- parent: NotebookPath | AbstractBaseTreeNode | None = None,
Construct a
NotebookPath.Additional
partsare appended topart.- Parameters:
part – A tree node creates an absolute path, a
NotebookPathis copied, and a string is parsed as path syntax.parts – Additional path strings appended after
part./separates segments unless it is escaped as\/.parent – Absolute path or node used later to resolve a relative string path. Must be absolute.
- Raises:
PathError – If
parentis not absolute.
Methods
__init__(part, *parts[, parent])Construct a
NotebookPath.count(value)escape(*parts)Add path escapes.
index(value, [start, [stop]])Raises ValueError if the value is not present.
Return whether this path is absolute.
is_relative_to(other)Return whether this path is located inside
other.relative_to(other)Return this path made relative to
other.resolve([parent, recurse])Return an absolute version of this path.
startswith(other)Return whether this path starts with another path's segments.
to_string([escape])Return the path as a slash-separated string.
unescape(part)Remove path escapes.
Attributes
The final node name.
The parent path (all segments except the last).
All node names except the last one.
- to_string(escape=False) str[source]#
Return the path as a slash-separated string.
By default, names are joined as-is. Pass
escape=Trueto matchstr(path).- Returns:
Examples include
"/Experiments/2024"and"2024/Results".
- is_absolute() bool[source]#
Return whether this path is absolute.
An absolute path is rooted at the notebook level and begins with
/in its string form.- Returns:
Trueif the path is absolute,Falseif relative.
- resolve(
- parent: NotebookPath | None = None,
- recurse: bool = False,
Return an absolute version of this path.
If the path is already absolute it is returned unchanged. Otherwise the path is resolved against
parent(if given) or against theparentanchor stored at construction time.- Parameters:
parent – An absolute path to resolve against. Ignored when the path is already absolute or has a stored parent anchor.
recurse – If
True,parentitself is resolved before use.
- Returns:
A new absolute
NotebookPath.- Raises:
PathError – If the path is relative and no parent is available to resolve against.
- startswith(
- other: NotebookPath,
Return whether this path starts with another path’s segments.
Compares raw segments without resolving either path.
- Parameters:
other – The prefix path to test against.
- Returns:
Trueif the leading segments of this path equal all segments ofother.
- is_relative_to(
- other: NotebookPath | AbstractBaseTreeNode,
Return whether this path is located inside
other.Unanchored relative paths are considered to be relative to any absolute path.
- Parameters:
other – The candidate ancestor path or tree node.
- Returns:
Trueif this path is equal to or belowother.
- relative_to(
- other: NotebookPath | AbstractBaseTreeNode,
Return this path made relative to
other.The result is a new relative
NotebookPathwhoseparentanchor is set to the resolved form ofother, so it can be resolved back to an absolute path later.- Parameters:
other – The ancestor path or tree node to relativise against.
- Returns:
A relative
NotebookPathfromotherto this path.- Raises:
PathError – If this path is not located inside
other.
- property name: UnescapedSegment#
The final node name.
Returns
"."for an empty path.
- property parts: Sequence[UnescapedSegment]#
All node names except the last one.
Analogous to the parent directory in a file path.
- property parent: NotebookPath#
The parent path (all segments except the last).
Resolves the path first, then appends
..to obtain the parent.- Returns:
An absolute
NotebookPathpointing to the parent location.
- count(
- value,
- index(
- value[,
- start[,
- stop,]]
Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but recommended.