Kaydet (Commit) 382d74ba authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

Iterables, String/Bytes and new registeration system, linked nodes

üst ac7ef3ca
......@@ -3,28 +3,34 @@ from __future__ import annotations
import collections.abc as abcs
from dataclasses import dataclass, field
from enum import Enum, auto
from io import StringIO
from typing import Any, Optional
class Display(Enum):
COLLECTION = auto()
@dataclass
class Node:
value: Display
display: Display
children: Optional[Node] = field(default_factory=list)
def add_child(self, child):
self.children.append(child)
@dataclass
class ConnectedNode:
class KVNode:
lhs: Any
rhs: Any
@dataclass
class LinkedNode:
prev: Optional[LinkedNode]
value: Any
def __str__(self):
return f"{value} ->"
class Viz:
typeregs = {}
......@@ -36,9 +42,9 @@ class Viz:
return obj
@classmethod
def register(cls, protocol, attr):
def register(cls, protocol, attr = None):
def wrapper(func):
cls.typeregs[getattr(protocol, attr)] = func
cls.typeregs[getattr(protocol, attr) if attr else protocol] = func
return func
return wrapper
......@@ -48,11 +54,36 @@ class Viz:
def mapping(self, obj):
node = Node(Display.COLLECTION)
for key, value in obj.items():
child = ConnectedNode(self.visit(key), self.visit(value))
child = KVNode(self.visit(key), self.visit(value))
node.add_child(child)
return node
@Viz.register((str, bytes))
def string(self, obj):
return obj
@Viz.register(abcs, "Iterable")
def sequence(self, obj):
node = Node(Display.COLLECTION)
prev = None
for item in obj:
child = LinkedNode(prev, item)
node.add_child(child)
prev = child
return node
viz = Viz()
node = viz.visit({1: 1, 2: 2, 3: {1: 2}})
print(node)
node = viz.visit({
'meta': {
'imports': [1, 2, 3],
'forbids': (1, 2),
'test': 3,
'instrs': {'a', 'b'}
},
'teta': {
'x': 'y'
},
(1, 2): 'a'
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment