Node 클래스
Node 클래스¶
Node 클래스는 YamlDocument의 AST에 대한 대여된 뷰(borrowed view)를 제공하여 트리 탐색, 쿼리 및 변경 작업을 가능하게 합니다. 노드는 doc.node(), doc.find("$.path") 또는 doc.walk()을 통해 생성됩니다.
개요¶
class Node:
"""A node in the YAML AST, backed by a YamlDocument and a path."""
각 Node는 부모 YamlDocument에 대한 참조와 문서의 AST 내에서 대상 노드로 이동하는 경로 튜플을 저장합니다. 문서가 수정되거나 해제되면 노드는 만료(stale)됩니다.
생성자¶
Node.__init__()¶
Node.__init__(document: YamlDocument, path: tuple = ()) -> None
매개변수:
document— 부모YamlDocumentpath— 대상 노드로 이동하는 경로 세그먼트(키/인덱스)의 튜플
속성¶
value¶
이 노드의 스칼라 값을 가져옵니다.
value -> Any | None
스칼라가 아닌 노드(매핑, 시퀀스)에 대해서는 None을 반환합니다.
읽기에 실패하면 예외가 납니다. 경로가 없으면 KeyError, 서로 다른 두 YAML 키가 같은
Python 키로 투영되면 YamlTypeError입니다(to_dict()가 같은 지점에서 거부합니다). 뒤의
거부는 경로가 아니라 문서의 성질이므로, 그런 문서에서는 어떤 경로를 쓰든 Node 읽기가
예외를 냅니다. 쓰기는 이 읽기를 거치지 않습니다. set_value()는 AST를 직접 고치므로,
이름이 겹치면 그 이름으로 접히는 첫 쌍에 반영됩니다.
root_type¶
이 노드의 타입을 가져옵니다.
root_type -> str
"scalar", "mapping", "sequence", "null" 중 하나를 반환합니다.
별칭 노드는 자기 대상의 타입을 알릴 뿐 자기만의 철자는 갖지 않습니다:
defaults: &d\n timeout: 30\nprod: *d\n와 src: &s\n - a\nref: *s\n에서 측정하면
$.prod와 $.ref 노드는 "mapping"과 "sequence"로 답하고, base: &x 1\nuse: *x\n의
$.use에 있는 스칼라 별칭은 "scalar"로 답합니다.
_path¶
문서의 AST 내에서 이 노드로 이동하는 경로 튜플입니다.
_path -> tuple
path¶
Get this node's path segments (tuple of keys and indices). The public accessor for logging, error messages, and re-finding a node after it becomes stale.
path -> tuple
children¶
이 노드의 자식 노드들을 가져옵니다.
children -> list[Node]
스칼라/null 노드에 대해서는 빈 리스트를 반환합니다.
parent¶
부모 Node를 가져오거나, 루트인 경우 None을 반환합니다.
parent -> Node | None
comment¶
이 노드의 주석 텍스트를 가져옵니다. 주석이 없으면 None을 반환합니다.
comment -> str | None
anchor¶
이 노드의 앵커 이름을 가져옵니다. 앵커가 없으면 None을 반환합니다.
anchor -> str | None
tag¶
이 노드의 YAML 태그 문자열(예: !!str)을 가져옵니다. 태그가 없으면 None을 반환합니다.
tag -> str | None
scalar_style¶
스칼라 스타일을 가져옵니다("plain", "single_quoted", "double_quoted", "literal", "folded"). 비스칼라 노드는 None을 반환합니다.
scalar_style -> str | None
flow_style¶
흐름 스타일을 가져옵니다(True = 흐름 {}/[], False = 블록). 비컨테이너 노드는 None을 반환합니다.
flow_style -> bool | None
chomping¶
chomping 표시자를 가져옵니다("strip", "clip", "keep"). 비스칼라 노드는 None을 반환합니다.
chomping -> str | None
메서드¶
find()¶
JSONPath-like 경로로 노드를 찾습니다.
find(path: str) -> Node | list[Node]
지원하는 경로 문법:
| 패턴 | 설명 |
|---|---|
$.key |
루트 키 |
$.key.subkey |
중첩 키 |
$.arr[0] |
시퀀스 인덱스 |
$.arr[*] |
시퀀스의 모든 항목 |
$..key |
모든 깊이에서 키를 검색 |
$..* |
모든 하위 노드 |
반환값: 정확한 경로에 대해서는 단일 Node, 와일드카드/깊이 검색 쿼리에 대해서는 list[Node]를 반환합니다.
find_first()¶
Find the first matching node by JSONPath-like path. Always returns a single Node or None (never a list) — for wildcard paths ([*]), returns the first element.
find_first(path: str) -> Node | None
walk()¶
모든 하위 노드를 순회합니다(깊이 우선 전위 순회).
walk() -> Iterator[Node]
생성(yield): 노드 자신, 그 다음 모든 하위 노드를 재귀적으로 반환합니다.
filter()¶
술어 함수로 하위 노드를 필터링합니다.
filter(predicate: Callable[[Node], bool]) -> list[Node]
매개변수:
predicate—Node를 받아bool을 반환하는 함수
예제:
scalars = root.filter(lambda n: n.root_type == "scalar")
set_value()¶
이 노드의 값을 교체하며, 메타데이터(주석, 앵커, 태그, 스타일)를 보존합니다.
set_value(value: Any, create_missing: bool = False) -> None
create_missing=True를 사용하면 경로상의 누락된 중간 매핑 키가 중첩 매핑으로 생성됩니다. 인덱스 세그먼트가 누락된 경우는 여전히 오류입니다.
측정된 모서리들:
find()가 해석하지 못한 노드 —Node(invalid)객체 — 도 여전히 씁니다:doc.node().find("$.zz").set_value(3)은zz: 3을 추가합니다. 거부되는 쪽은 쓰기가 아니라 그value읽기(KeyError)입니다- 별칭 노드에서도 이 호출은 거부가 아니라 별칭을 제자리에서 다시 씁니다:
parse("base: &x 1\nuse: *x\n").node().find("$.use").set_value(42)는base: &x 1\nuse: 42\n를 발출합니다 - 별칭 을 통한 쓰기는 거부됩니다:
edit failed: cannot-edit-alias - 만료된 노드에서는
RuntimeWarning을 낸 뒤YamlDocumentError: document has been modified; re-find the node를 발생시킵니다 - 작성자가 알지 못하는 Python 값은 편집 오류가 아니라
YamlTypeError(Unsupported Python type for YAML conversion)입니다
append()¶
시퀀스 노드에 값을 추가합니다.
append(value: Any) -> None
insert()¶
시퀀스 노드의 특정 인덱스에 삽입합니다.
insert(index: int, value: Any) -> None
delete()¶
이 노드와 그 주석을 제거합니다. 이후 노드는 만료됩니다.
delete(on_duplicate: str = "one") -> None
측정된 거부들: 루트 노드는 YamlEditError: edit failed: edit-error로 답하고, 별칭이 아직
참조하는 앵커를 가진 노드는 edit failed: anchor-is-referenced로 답하며, 같은 키를 두 짝이
철자하는 문서에서 on_duplicate="error"는 edit failed: duplicate-key-target으로 답합니다.
별칭 짝을 지우는 것은 거부가 아닙니다 —
defaults: &d\n timeout: 30\nprod: *d\n에서 find("$.prod").delete()는
defaults: &d\n timeout: 30\n만 남깁니다. 만료된 노드는 다음 접근에서 YamlDocumentError로
답하지만 is_valid()는 여전히 True로 답합니다.
rename()¶
이 노드의 매핑 키를 변경합니다. 노드는 매핑 값이어야 합니다.
rename(new_key: str) -> None
측정된 거부들: 루트는 YamlEditError: edit failed: cannot-rename-root로 답하고, 키가 없는
시퀀스 요소는 edit failed: cannot-rename-complex-key로 답하며, 매핑이 이미 갖고 있는
new_key는 edit failed: rename-key-exists로 답하고, 키 자체가 복합 노드인 노드는 엔진까지
닿지 않습니다: parse("? [1, 2]\n: value\n").node().children[0].rename("b")는
YamlEditError: ValueError: segment must be str or int로 답합니다. 같은 키로 이름 변경하는
것은 no-op이고, 별칭 짝에서는 이 호출이 거부되지 않고 키의 이름을 바꿉니다 —
defaults: &d\n timeout: 30\nprod: *d\n에서 find("$.prod").rename("prod2")는
defaults: &d\n timeout: 30\nprod2: *d\n를 남깁니다.
set_comment()¶
이 노드의 주석을 설정(또는 교체)합니다. standalone=True(기본)면 주석이 노드 위의 독립된 줄에 출력되고, standalone=False면 노드 뒤에 인라인으로 출력됩니다.
set_comment(text: str, standalone: bool = True) -> None
remove_comment()¶
이 노드의 주석을 제거합니다.
remove_comment() -> None
set_anchor()¶
이 노드의 앵커를 설정(또는 교체)합니다.
set_anchor(name: str) -> None
별칭이 아직 현재 앵커를 가리키는 동안은 YamlEditError: edit failed: anchor-is-referenced로
거부됩니다 — base: &x 1\nuse: *x\n에서 측정하면 find("$.base").set_anchor("renamed")는
실패하고, 별칭을 지운 뒤 같은 호출을 base: &x 1\n에 넘기면 base: &renamed 1을 발출합니다.
별칭 노드 자신에 대해서는 edit failed: cannot-edit-alias로 답합니다.
remove_anchor()¶
이 노드의 앵커를 제거합니다.
remove_anchor() -> None
set_anchor()와 같은 방어막입니다: base: &x 1\nuse: *x\n에서 측정하면
find("$.base").remove_anchor()는 YamlEditError: edit failed: anchor-is-referenced로
답합니다.
set_tag()¶
이 노드의 YAML 태그를 설정(또는 교체)합니다. "!custom"은 로컬 태그, "!!int"는 프라이머리(!!) 태그, "!<tag:yaml.org,2002:str>"는 verbatim 태그가 됩니다.
set_tag(tag: str) -> None
remove_tag()¶
이 노드의 YAML 태그를 제거합니다.
remove_tag() -> None
set_scalar_style()¶
스칼라 스타일을 설정(또는 교체)합니다. 비스칼라 노드에서는 no-op. 값: "plain", "single_quoted", "double_quoted", "literal", "folded".
set_scalar_style(style: str) -> None
set_flow_style()¶
흐름 스타일을 설정(또는 교체)합니다. True는 흐름({}/[]), False는 블록. 비컨테이너 노드에서는 no-op.
set_flow_style(flow: bool) -> None
set_chomping()¶
chomping 표시자를 설정(또는 교체)합니다. 값: "strip"(-), "clip"(기본값), "keep"(+). 비스칼라 노드에서는 no-op.
set_chomping(chomp: str) -> None
sort_keys()¶
Sort the keys of this mapping node in place. No-op on non-mapping nodes.
sort_keys() -> None
move()¶
Move this subtree to a new path in the same document (copies the subtree to new_path, then removes the source). new_path is an absolute JSONPath.
move(new_path: str) -> None
측정된 모서리들: 부모가 존재하는 목적지는 자기 자신이 없어도 됩니다 —
src:\n x: 1\n에서 find("$.src").move("$.nope")는 nope:\n x: 1\n를 남깁니다 — 반면
중간 키가 없는 목적지는 YamlEditError: edit failed: missing-path:deep으로 답합니다. 스칼라
목적지를 교체하는 것은 동작합니다: dst: 2가 dst:\n x: 1\n가 됩니다. 루트를 이동하면
edit failed: edit-error가 답하고, 하위 트리를 자기 경로로 옮기면 문서가 비워집니다
(find("$.src").move("$.src")은 {}\n를 남깁니다), 그리고 값을 참조하는 별칭이 아직
남아 있는 목적지는 edit failed: anchor-is-referenced로 답합니다.
to_yaml()¶
이 서브트리를 YAML 문자열로 직렬화합니다.
to_yaml() -> str
copy()¶
이 하위 트리를 문서에서 분리된 독립 Python 값(dict/list/scalar)으로 깊은 복사합니다. set_value()로 다른 곳에 붙여넣을 때 유용합니다.
copy() -> Any
is_valid()¶
부모 문서가 여전히 살아있는지 확인합니다.
is_valid() -> bool
이 질문은 문서에 대한 것이지 경로에 대한 것이 아닙니다. 경로가 아무것도 해석하지
못하는 노드(키가 없거나, 두 YAML 키가 같은 Python 이름으로 접히거나, 이후 편집으로
노드가 만료된 경우)도 여전히 유효하므로, is_valid()으로는 value가 응답할지를
확인할 수 없습니다. 그 신호는 __repr__이 전달합니다.
release()¶
부모 문서에 대한 참조를 해제하여 이 노드를 만료 상태로 표시합니다.
release() -> None
release()를 호출한 후에는 이 노드에 접근하면 RuntimeWarning이 발생하고 YamlDocumentError가 발생합니다.
던더 메서드¶
__repr__()¶
__repr__() -> str
경로가 해석되면 Node(root_type=<type>, path=<path>), release()를 호출한 뒤에는
Node(released), 해석되지 않으면 Node(invalid)를 반환합니다. 후자는 문서가 수정되었거나,
경로가 아무것도 가리키지 않거나, 값 읽기가 거부된 경우(value 참조)에 나타납니다.
__eq__()¶
__eq__(other: object) -> bool
두 Node 인스턴스는 동일한 문서, 경로 및 활성 상태를 공유하는 경우 동일합니다.
value_eq()¶
Compare this node's resolved value with another node or Python value. Unlike __eq__ (reference identity), this compares the actual YAML value.
value_eq(other: object) -> bool
무효 노드 동작¶
만료된 노드
Node는 생성 시점의 문서 리비전에 연결됩니다. 문서 편집은 리비전을 증가시키므로,
이전에 얻은 노드는 만료됩니다. 문서를 편집한 후에는 항상 노드를 다시 찾아야 합니다.
노드는 다음 경우에 만료됩니다:
- 부모
YamlDocument가 가비지 컬렉션된 경우 release()가 명시적으로 호출된 경우- 노드가 생성된 후 문서가 수정된 경우
만료된 노드에 접근하면 RuntimeWarning이 발생하고 YamlDocumentError가 발생합니다:
>>> node = doc.node()
>>> doc.set("$.key", "new_value")
>>> node.value # (1)!
RuntimeWarning: Node is stale: the document was modified after this node was created
YamlDocumentError: document has been modified; re-find the node # (2)!
- 만료된 노드에 접근하면 먼저
RuntimeWarning이 트리거됩니다. - 그런 다음
YamlDocumentError가 발생합니다.doc.node().find(path)로 노드를 다시 획득하세요.
예제¶
import pyrs_yaml
doc = pyrs_yaml.parse("""
a:
b: 1
c: [2, 3, 4]
d: hello
""")
# Get root node
root = doc.node()
print(root.root_type) # "mapping"
# Navigate
node = root.find("$.a.c[1]")
print(node.value) # 3
# Walk
for n in root.walk():
print(n._path, n.root_type)
# Filter
numbers = root.filter(lambda n: n.root_type == "scalar" and isinstance(n.value, int))
for n in numbers:
print(n._path, n.value) # ('a', 'b') 1, ('a', 'c', 0) 2, ...
# Mutate
root.find("$.a.b").set_value(42)
root.find("$.a.c").append(5)
root.find("$.d").rename("greeting")
root.find("$.a.c[0]").delete()
print(doc.to_yaml())
# a:
# b: 42
# c: [3, 4, 5]
# greeting: hello
자동 생성 API 참조¶
아래 내용은 mkdocstrings가 라이브러리 자체 docstring과 생성된 형식 stub에서 그려 낸 것이므로, 서명은 코드에서 온 것이지 손으로 옮긴 것이 아닙니다.
pyrs_yaml.Node
¶
Node(document, path=())
A node in the YAML AST, backed by a YamlDocument and a path.
Each Node stores a reference to its parent YamlDocument and a path tuple that navigates to the target node within the document's AST.
Source code in python/pyrs_yaml/node.py
33 34 35 36 37 | |
path
property
¶
path
Get this node's path segments (tuple of keys and indices).
The path can be used to reconstruct a JSONPath-like string or to re-find the node after it becomes stale.
value
property
¶
value
Get the scalar value of this node.
Returns None for container nodes (mappings and sequences).
Use the following alternatives for containers:
node.children— iterate over child nodesnode.to_yaml()— serialize the subtree to a YAML stringdoc.get(key)— access a value by literal mapping key from the document
leading_comment
property
¶
leading_comment
Get this node's leading (standalone) comment text.
Returns the note the source carried on its own line above the
node. For YAML documents the receiver historically stored
standalone notes on the comment slot with a standalone
flag; the getter normalises across both representations, so a
parsed YAML document and a parsed TOML / JSONC document both
surface their leading note here.
None when the node has no standalone comment.
leading_comments
property
¶
leading_comments
Every leading (standalone) note above this node, in source order.
A document may stack any number of comment lines directly above one key;
all of them are preserved. :attr:leading_comment returns only the first,
and is unchanged for documents that carry a single note.
An empty list when the node has no standalone comment.
scalar_style
property
¶
scalar_style
Get the scalar style ("plain", "single_quoted",
"double_quoted", "literal", "folded"), or None for
non-scalar nodes.
flow_style
property
¶
flow_style
Get the flow style (True = flow {}/[], False = block),
or None for non-container nodes.
chomping
property
¶
chomping
Get the chomping indicator ("strip", "clip", "keep"),
or None for non-scalar nodes.
is_valid
¶
is_valid()
Check if the parent document is still alive.
Source code in python/pyrs_yaml/node.py
67 68 69 | |
release
¶
release()
Release the reference to the parent document, marking this node as stale.
After calling release(), any access to this node will emit a RuntimeWarning and raise YamlDocumentError.
Source code in python/pyrs_yaml/node.py
80 81 82 83 84 85 86 87 | |
to_yaml
¶
to_yaml()
Serialize this subtree to YAML string.
Source code in python/pyrs_yaml/node.py
116 117 118 119 120 121 122 123 124 125 126 127 | |
copy
¶
copy()
Deep-copy this subtree as a standalone value (dict/list/scalar).
The result is detached from the document — useful for duplicating a
subtree to paste elsewhere via set_value().
Source code in python/pyrs_yaml/node.py
129 130 131 132 133 134 135 136 | |
set_value
¶
set_value(value, create_missing=False)
Replace this node's value, preserving its metadata (comment/anchor/tag/style).
With create_missing=True, missing intermediate mapping keys along
the path are created as nested mappings (e.g. setting a.b.c on
a: 1 creates b and c). Index segments that miss are still
an error.
Source code in python/pyrs_yaml/node.py
138 139 140 141 142 143 144 145 146 | |
append
¶
append(value)
Append to a sequence node.
Source code in python/pyrs_yaml/node.py
148 149 150 | |
insert
¶
insert(index, value)
Insert into a sequence node at index.
Source code in python/pyrs_yaml/node.py
152 153 154 | |
delete
¶
delete(on_duplicate='one')
Remove this node and its comments. Node becomes stale afterwards.
on_duplicate decides what a delete means when the document holds more than one
pair spelling this key, told apart only by the tag or anchor the reader keeps:
"one" (the default) removes the pair the path resolves to, "all" removes
every pair that key could answer with, and "error" refuses while both remain.
Source code in python/pyrs_yaml/node.py
156 157 158 159 160 161 162 163 164 | |
rename
¶
rename(new_key)
Rename this node's mapping key. Node must be a mapping value.
Source code in python/pyrs_yaml/node.py
166 167 168 | |
set_comment
¶
set_comment(text, standalone=True)
Set (or replace) this node's comment.
With standalone=True (default) the comment is emitted on its own
line above the node; with standalone=False it is emitted inline
after the node.
Source code in python/pyrs_yaml/node.py
176 177 178 179 180 181 182 183 | |
remove_comment
¶
remove_comment()
Remove this node's comment.
Source code in python/pyrs_yaml/node.py
185 186 187 | |
set_leading_comment
¶
set_leading_comment(text)
Set (or replace) this node's leading (standalone) comment.
Writes to the dedicated leading_comment slot introduced in
PR #114 and clears any legacy standalone=True note still
living on the comment slot so the two representations never
disagree.
Source code in python/pyrs_yaml/node.py
218 219 220 221 222 223 224 225 226 | |
remove_leading_comment
¶
remove_leading_comment()
Remove this node's leading (standalone) comment.
Clears both the leading_comment slot and any legacy
comment(standalone=True) note on the same node. The inline
trailing note on the comment slot is preserved.
Source code in python/pyrs_yaml/node.py
228 229 230 231 232 233 234 235 | |
set_anchor
¶
set_anchor(name)
Set (or replace) this node's anchor.
Source code in python/pyrs_yaml/node.py
243 244 245 | |
remove_anchor
¶
remove_anchor()
Remove this node's anchor.
Source code in python/pyrs_yaml/node.py
247 248 249 | |
set_tag
¶
set_tag(tag)
Set (or replace) this node's YAML tag.
"!custom" produces a local tag, "!!int" produces a primary
(!!) tag, and "!<tag:yaml.org,2002:str>" produces a verbatim
tag.
Source code in python/pyrs_yaml/node.py
257 258 259 260 261 262 263 264 | |
remove_tag
¶
remove_tag()
Remove this node's YAML tag.
Source code in python/pyrs_yaml/node.py
266 267 268 | |
set_scalar_style
¶
set_scalar_style(style)
Set (or replace) this node's scalar style.
No-op on non-scalar nodes. Recognized values: "plain",
"single_quoted", "double_quoted", "literal", "folded".
Source code in python/pyrs_yaml/node.py
279 280 281 282 283 284 285 | |
set_flow_style
¶
set_flow_style(flow)
Set (or replace) this node's flow style.
True emits flow ({}/[]), False emits block. No-op on
non-container nodes.
Source code in python/pyrs_yaml/node.py
295 296 297 298 299 300 301 | |
set_chomping
¶
set_chomping(chomp)
Set (or replace) this node's chomping indicator.
Recognized values: "strip" (-), "clip" (default),
"keep" (+). No-op on non-scalar nodes.
Source code in python/pyrs_yaml/node.py
311 312 313 314 315 316 317 | |
sort_keys
¶
sort_keys()
Sort the keys of this mapping node in place.
Only meaningful on mapping nodes; no-op on other node types.
Source code in python/pyrs_yaml/node.py
319 320 321 322 323 324 | |
move
¶
move(new_path)
Move this subtree to a new path in the same document.
new_path is an absolute JSONPath ($.dest.key). The source node
is removed after the subtree is copied to the destination.
Source code in python/pyrs_yaml/node.py
326 327 328 329 330 331 332 333 334 335 | |
walk
¶
walk()
Walk all descendant nodes (depth-first pre-order).
Source code in python/pyrs_yaml/node.py
359 360 361 362 363 | |
filter
¶
filter(predicate)
Filter descendant nodes by a predicate function.
Source code in python/pyrs_yaml/node.py
365 366 367 | |
find
¶
find(path)
Find a node by JSONPath-like path.
The path must start with $ (a leading $ is required; a bare
a.b or arr[0] raises ValueError). This is the canonical
path syntax for the Python API; the Rust backend's internal path parser
accepts an optional $ for compatibility.
Supports
$.key - Root key $.key.subkey - Nested key $.arr[0] - Index into sequence $.arr[*] - All items in sequence $..key - Deep search for key at any depth
Source code in python/pyrs_yaml/node.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
find_first
¶
find_first(path)
Find the first matching node by JSONPath-like path.
Unlike find(), this always returns a single Node or None
(never a list). For wildcard paths ([*]), returns the first element.
Source code in python/pyrs_yaml/node.py
407 408 409 410 411 412 413 414 415 416 417 418 419 420 | |
value_eq
¶
value_eq(other)
Compare this node's resolved value with another node or Python value.
Unlike == (which compares reference identity), this compares the
actual YAML value (dict/list/scalar/None) after resolution.
Source code in python/pyrs_yaml/node.py
437 438 439 440 441 442 443 444 445 | |