Skip to content

Instantly share code, notes, and snippets.

@skewballfox
Created August 30, 2020 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skewballfox/f389c24f3f3e23d0f64360653a628982 to your computer and use it in GitHub Desktop.
Save skewballfox/f389c24f3f3e23d0f64360653a628982 to your computer and use it in GitHub Desktop.
in progress python support for the debug visualizer
"""
Author: skewballfox
"""
from json import dumps
from typing import Union, Dict, Optional
from abc import ABC, abstractmethod
class DataType(ABC):
"""Abstract class for all dataTypes
Args:
object ([type]): [description]
"""
def __init__(self):
self.kind = {}
super().__init__()
def __repr__(self):
return dumps(self.__dict__)
"""
interface Text {
kind: { text: true };
text: string;
mimeType?: string;
fileName?: string;
}
"""
class Text(DataType):
def __init__(self,
text_data: str,
mimeType: Optional[str] = None,
fileName: Optional[str] = None):
super().__init__()
self.kind["text"] = True
self.text = text_data
if mimeType is None:
self.mimeType = mimeType
if fileName is None:
self.fileName = fileName
"""
interface Svg extends Text {
kind: { text: true; svg: true };
}
"""
class Svg(Text):
def __init__(self,
text_data: str,
mimeType: Optional[str] = None,
fileName: Optional[str] = None):
self.kind["svg"] = True
super().__init__(text_data, mimeType, fileName)
"""
interface DotGraph extends Text {
kind: { text: true; dotGraph: true };
}
"""
class DotGraph(Text):
def __init__(self,
text_data: str,
mimeType: Optional[str] = None,
fileName: Optional[str] = None):
self.kind["dotGraph"] = True
super().__init__(text_data, mimeType, fileName)
"""
export interface Grid {
kind: { array: true };
columnLabels?: { label?: string }[];
rows: {
label?: string;
columns: {
content?: string;
tag?: string;
color?: string;
}[];
}[];
markers?: {
id: string;
row: number;
column: number;
rows?: number;
columns?: number;
label?: string;
color?: string;
}[];
}
"""
"""
interface NodeGraphData {
id: string;
label?: string;
color?: string;
shape?: "ellipse" | "box";
}
"""
"""
interface EdgeGraphData {
from: string;
to: string;
label?: string;
id?: string;
color?: string;
dashes?: boolean;
}
"""
class Edge:
def __init__(self):
pass
class Node:
def __init__(self, id: Union[int, str], lab):
self.id = id
class Graph(DataType):
"""
Args:
DataType (Union[Dict[str,list],Dict[int,list]]):
either expects a dictionary with a list as values or a 2d array
some representation of a basic graph
"""
def __init__(self, graph_data: Union[Dict[str, list], Dict[int, list]]):
super().__init__()
self.kind["graph"] = True
#works for both a dictionary and an nxn array
self.edges = []
if isinstance(graph_data, dict):
self.nodes = [Node(node) for node in graph_data]
"""for node in graph_data:
print("node: ", node)
print("edges: ", graph_data[node])
self.nodes[node_i] = Node()
for edge in graph_data[node]:
print(edge_i)
print("edge: ", graph_data[node][edge_i])
self.edges.append({
"from": node,
"to": graph_data[node][edge_i]
})
edge_i += 1
edge_i = 0
node_i += 1
"""
"""
export interface Plotly {
kind: { plotly: true };
data: Partial<Plotly.Data>[];
}
// See plotly docs for Plotly.Data.
"""
class Plotly(DataType):
def __init__(self, data):
super().__init__()
self.kind["plotly"] = True
if __name__ == "__main__":
graph_data1 = {"A": ["B", "C"], "B": ["A,C"], "C": ["A,D"], "D": ["A"]}
graph_data2 = {1: [2, 3], 2: [1, 3], 3: [1, 4], 4: [1]}
graph = Graph(graph_data1)
print(graph)
@Rdroshan
Copy link

Rdroshan commented Sep 1, 2020

This is a great help for understanding. Thanks.

We can also have a class for LinkedList as well.
As in above, I can see the Node and Edge will be a visualization data.

I'm trying to write a visualization function for a linked list implementation. Have started with the basic implementation of Node and creating a Linked list out of it and then visualizing them currently with direct json_value.

I'm thinking of finding a pattern and then creating a generalized function for this.
Suggestions are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment