def to_json(value: Any, fn: Union[ToJsonFn, None] = None) -> Any: if isinstance(value, BaseNode): return value.to_json(fn) if isinstance(value, list): return list(to_json(item, fn) for item in value) if isinstance(value, tuple): return list(to_json(item, fn) for item in value) else: return value
def from_json(value: Any) -> Any: if isinstance(value, dict):
cls = getattr(sys.modules[__name__], value['type'])
args = {
k: from_json(v) for k, v in value.items() if k != 'type'
} return cls(**args) if isinstance(value, list): return list(map(from_json, value)) else: return value
def scalars_equal(node1: Any, node2: Any, ignored_fields: List[str]) -> bool: """Compare two nodes which are not lists."""
if type(node1) != type(node2): returnFalse
if isinstance(node1, BaseNode): return node1.equals(node2, ignored_fields)
return cast(bool, node1 == node2)
class BaseNode: """Base class for all Fluent AST nodes.
All productions described in the ASDL subclass BaseNode, including Span and
Annotation. Implements __str__, to_json and traverse. """
def clone(self: Node) -> Node: """Create a deep clone of the current node.""" def visit(value: Any) -> Any: """Clone node and its descendants.""" if isinstance(value, BaseNode): return value.clone() if isinstance(value, list): return [visit(child) for child in value] if isinstance(value, tuple): return tuple(visit(child) for child in value) return value
# Use all attributes found on the node as kwargs to the constructor. return self.__class__(
**{name: visit(value) for name, value in vars(self).items()}
)
Nodes are deeply compared on a field by field basis. If possible, False is returned early. When comparing attributes and variants in
SelectExpressions, the order doesn't matter. By default, spans are not
taken into account. """
if ignored_fields: for key in ignored_fields:
self_keys.discard(key)
other_keys.discard(key)
if self_keys != other_keys: returnFalse
for key in self_keys:
field1 = getattr(self, key)
field2 = getattr(other, key)
# List-typed nodes are compared item-by-item. When comparing # attributes and variants, the order of items doesn't matter. if isinstance(field1, list) and isinstance(field2, list): if len(field1) != len(field2): returnFalse
for elem1, elem2 in zip(field1, field2): ifnot scalars_equal(elem1, elem2, ignored_fields): returnFalse
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.