-
Notifications
You must be signed in to change notification settings - Fork 1
/
diagram.py
809 lines (679 loc) · 37.9 KB
/
diagram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
from generallibrary.functions import AutoInitBases, wrapper_transfer, deco_cast_to_self, Recycle
from generallibrary.values import clamp, confineTo
from generallibrary.iterables import get, pivot_list, subtract_list, flatten
import pandas
from itertools import chain
# import pickle
import dill as pickle # Can apparently pickle lambdas: https://stackoverflow.com/questions/25348532/can-python-pickle-lambda-functions
import matplotlib.pyplot as plt
import networkx as nx
from random import uniform
def _skip_node_check(node, order_func, filt, traverse_excluded, _all_nodes):
if node in _all_nodes:
return True
if filt and not traverse_excluded and not filt(node):
return True
if order_func:
for order_node in order_func(node):
if order_node not in _all_nodes:
return True
return False
def _traverse(*nodes, func, depth, flat, filt, traverse_excluded, include_self, order_func, vertical, spawn, _all_nodes):
nodes = [node for node in nodes if not _skip_node_check(node=node, order_func=order_func, filt=filt, traverse_excluded=traverse_excluded, _all_nodes=_all_nodes)]
if not nodes:
return StopIteration
if include_self:
if filt and traverse_excluded:
filtered_nodes = [node for node in nodes if filt(node)] if filt else nodes
else:
filtered_nodes = nodes
if flat:
yield from filtered_nodes
else:
yield filtered_nodes
_all_nodes.extend(nodes)
if depth is not StopIteration:
next_depth = StopIteration if depth == 0 else depth - 1
next_nodes = [node2 for node in nodes for node2 in func(node, spawn=spawn)]
def next_traverse(*nodes2):
return _traverse(*nodes2, func=func, depth=next_depth, flat=flat, filt=filt, traverse_excluded=traverse_excluded, include_self=True, order_func=order_func, vertical=vertical, spawn=spawn, _all_nodes=_all_nodes)
if vertical:
for node in next_nodes:
yield from next_traverse(node)
else:
yield from next_traverse(*next_nodes)
def _traverser(*nodes, func, depth=None, flat=None, filt=None, traverse_excluded=None, include_self=None, gen=None, vertical=None, order_func=None, spawn=None):
if depth is None: depth = 0
if flat is None: flat = True
if traverse_excluded is None: traverse_excluded = False
if include_self is None: include_self = False
if gen is None: gen = False
if vertical is None: vertical = True
if spawn is None: spawn = True
generator = _traverse(*nodes, func=func, depth=depth, flat=flat, filt=filt, traverse_excluded=traverse_excluded, include_self=include_self, order_func=order_func, vertical=vertical, spawn=spawn, _all_nodes=[])
return generator if gen else list(generator)
def _deco_depth(func):
def _wrapper(node, depth=None, flat=None, filt=None, traverse_excluded=None, include_self=None, gen=None, vertical=None, spawn=None):
return _traverser(node, func=func, depth=depth, flat=flat, filt=filt, traverse_excluded=traverse_excluded, include_self=include_self, gen=gen, vertical=vertical, spawn=spawn)
return wrapper_transfer(func, _wrapper)
class _Diagram_Visualize:
def graph(self, name_func=lambda x: x):
""" :param TreeDiagram or NetworkDiagram or Any self:
:param name_func: """
G = nx.DiGraph()
for node in self.get_all():
color = [uniform(0, 0.7) for _ in range(3)]
for dependant in node.get_children():
G.add_edge(name_func(node), name_func(dependant), color=color)
colors = nx.get_edge_attributes(G, 'color').values()
plt.figure(figsize=(8, 8))
nx.draw(G, pos=nx.circular_layout(G), edge_color=colors, width=2, connectionstyle='arc3, rad=0', with_labels=True, node_color="None", node_size=3000, node_shape="s")
plt.show()
class _Diagram_QOL:
""" Quality of life helper methods of Diagram. """
def _get_children_or_parents(self, parent, child):
if (parent, child) == (None, None):
parent = self.get_parent()
if parent:
return parent._children
elif child:
return child._parents
else:
return [self]
def _singular_alternative(self, method, index, depth, filt, traverse_excluded, include_self, spawn):
if index is None:
index = 0
generator = method.__func__(self, depth=depth, flat=True, gen=True, filt=filt, traverse_excluded=traverse_excluded, include_self=include_self, spawn=spawn)
if index < 0:
return get(iterable=tuple(generator), index=index)
else:
for i, node in enumerate(generator):
if i == index:
return node
def get_index(self, parent=None, child=None):
""" Get the index this node has in target node's container. Parent takes precedence if both are defined.
Defaults to returning index of first parent's children. Returns 0 if orphan.
:param TreeDiagram or NetworkDiagram or Any self:
:param TreeDiagram or NetworkDiagram or Any parent:
:param TreeDiagram or NetworkDiagram or Any child:
:raises ValueError: If given node doesn't contain self. """
return self._get_children_or_parents(parent=parent, child=child).index(self)
def set_index(self, index, parent=None, child=None):
""" Set the index this node has in target node's container. Parent takes precedence if both are defined.
Defaults to setting index of first parent's children. Nothing happens if orphan and parent is defined.
:param TreeDiagram or NetworkDiagram or Any self:
:param int index:
:param TreeDiagram or NetworkDiagram or Any parent:
:param TreeDiagram or NetworkDiagram or Any child:
:raises ValueError: If given node doesn't contain self. """
container = self._get_children_or_parents(parent=parent, child=child)
if index != container.index(self):
container.remove(self)
container.insert(index, self)
@deco_cast_to_self(if_not_base="_Diagram")
def add_node(self, child):
""" Add a node as child, either with one arg being of own type or with args to create a new one.
Returns child.
:param TreeDiagram or NetworkDiagram or Any self:
:param TreeDiagram or NetworkDiagram or Any child: """
child.set_parent(parent=self)
return child
def remove_node(self):
""" Remove this node recursively. Use set_parent(None) instead if children should be unaffected.
:param TreeDiagram or NetworkDiagram or Any self: """
self.set_parent(parent=None)
if hasattr(self, "recycle_clear"):
self.recycle_clear()
for child in self.get_children(gen=True, spawn=False):
child.remove_node()
def get_child(self, index=None, depth=None, filt=None, traverse_excluded=None, include_self=None, spawn=None):
""" Singular QOL alternative for get_children().
:param TreeDiagram or NetworkDiagram or Any self:
:param int or None index: Index of node to be returned. Possible filter is applied before.
:param int or None depth: Default depth of 0 will return single direct layer. Get unlimited with -1. Previous layers are included.
:param filt: Optional functional filter, expects 1 node as argument. Applies filter to ALL nodes, including self. See traverse_excluded.
:param bool or None traverse_excluded: False - Whether to traverse a node even though it has been filtered out from result.
:param bool or None include_self: False
:param bool or None spawn: True - Whether to call spawn_* hooks when using get_children or get_parents.
:rtype: TreeDiagram or NetworkDiagram or Any """
return self._singular_alternative(self.get_children, index=index, depth=depth, filt=filt, traverse_excluded=traverse_excluded, include_self=include_self, spawn=spawn)
def get_parent(self, index=None, depth=None, filt=None, traverse_excluded=None, include_self=None, spawn=None):
""" Singular QOL alternative for get_parents().
:param TreeDiagram or NetworkDiagram or Any self:
:param int or None index: Index of node to be returned. Possible filter is applied before.
:param int or None depth: Default depth of 0 will return single direct layer. Get unlimited with -1. Previous layers are included.
:param filt: Optional functional filter, expects 1 node as argument. Applies filter to ALL nodes, including self. See traverse_excluded.
:param bool or None traverse_excluded: False - Whether to traverse a node even though it has been filtered out from result.
:param bool or None include_self: False
:param bool or None spawn: True - Whether to call spawn_* hooks when using get_children or get_parents.
:rtype: TreeDiagram or NetworkDiagram or Any """
return self._singular_alternative(self.get_parents, index=index, depth=depth, filt=filt, traverse_excluded=traverse_excluded, include_self=include_self, spawn=spawn)
def get_node(self, index=None, depth=None, filt=None, traverse_excluded=None, include_self=None, spawn=None):
""" Singular QOL alternative for get_nodes().
:param TreeDiagram or NetworkDiagram or Any self:
:param int or None index: Index of node to be returned. Possible filter is applied before.
:param int or None depth: Default depth of 0 will return single direct layer. Get unlimited with -1. Previous layers are included.
:param filt: Optional functional filter, expects 1 node as argument. Applies filter to ALL nodes, including self. See traverse_excluded.
:param bool or None traverse_excluded: False - Whether to traverse a node even though it has been filtered out from result.
:param bool or None include_self: False
:param bool or None spawn: True - Whether to call spawn_* hooks when using get_children or get_parents.
:rtype: TreeDiagram or NetworkDiagram or Any """
return self._singular_alternative(self.get_nodes, index=index, depth=depth, filt=filt, traverse_excluded=traverse_excluded, include_self=include_self, spawn=spawn)
def get_sibling(self, index=None, depth=None, filt=None, traverse_excluded=None, include_self=None, spawn=None):
""" Singular QOL alternative for get_siblings().
:param TreeDiagram or NetworkDiagram or Any self:
:param int or None index: Index of node to be returned. Possible filter is applied before.
:param int or None depth: Default depth of 0 will return single direct layer. Get unlimited with -1. Previous layers are included.
:param filt: Optional functional filter, expects 1 node as argument. Applies filter to ALL nodes, including self. See traverse_excluded.
:param bool or None traverse_excluded: False - Whether to traverse a node even though it has been filtered out from result.
:param bool or None include_self: False
:param bool or None spawn: True - Whether to call spawn_* hooks when using get_children or get_parents.
:rtype: TreeDiagram or NetworkDiagram or Any """
return self._singular_alternative(self.get_siblings, index=index, depth=depth, filt=filt, traverse_excluded=traverse_excluded, include_self=include_self, spawn=spawn)
def get_ordered_index(self):
""" Get which layer this node is in based on global get_ordered().
:param TreeDiagram or NetworkDiagram or Any self: """
for i, layer in enumerate(self.get_ordered(flat=False, gen=True)):
if self in layer:
return i
def disconnect(self, filt):
""" Iterate all nodes and set nodes passing the filter's parent to None.
:param TreeDiagram or NetworkDiagram or Any self:
:param filt: """
for node in self.get_all(spawn=False, gen=True, filt=filt, traverse_excluded=True):
node.set_parent(None)
class _Diagram_Global:
""" Global methods of a Diagram. """
def get_all(self, depth=None, flat=None, filt=None, traverse_excluded=None, gen=None, spawn=None):
""" QOL, shortcut for get_nodes() with depth being -1 and include_self being True.
Will return/yield all nodes, originating from self.
:param TreeDiagram or NetworkDiagram or Any self:
:param int or None depth: -1 - Depth of 0 will return/yield single direct layer. Get unlimited with -1.
:param bool or None flat: True - Whether to return/yield nodes directly or in lists. Ignored if vertical.
:param filt: Optional functional filter, expects 1 node as argument. Applies filter to ALL nodes, including self. See traverse_excluded.
:param bool or None traverse_excluded: False - Whether to traverse a node even though it has been filtered out from result.
:param bool or None gen: False - Whether to return a generator or list.
:param bool or None spawn: True - Whether to call spawn_* hooks when using get_children or get_parents.
:rtype: list[TreeDiagram or NetworkDiagram or Any] """
if depth is None:
depth = -1
func = self.get_nodes.__func__
return _traverser(self, func=func, depth=depth, flat=flat, filt=filt, traverse_excluded=traverse_excluded, include_self=True, gen=gen, vertical=True, spawn=spawn)
def get_ordered(self, depth=None, flat=None, filt=None, traverse_excluded=None, gen=None, spawn=None):
""" Top to Bottom horizontally.
Starts with orphan nodes and traverses to return/yield children nodes which have had their respective parents already returned/yielded.
:param TreeDiagram or NetworkDiagram or Any self:
:param int or None depth: -1 - Depth of 0 will return/yield single direct layer. Get unlimited with -1.
:param bool or None flat: True - Whether to return/yield nodes directly or in lists. Ignored if vertical.
:param filt: Optional functional filter, expects 1 node as argument. Applies filter to ALL nodes, including self. See traverse_excluded.
:param bool or None traverse_excluded: False - Whether to traverse a node even though it has been filtered out from result.
:param bool or None gen: False - Whether to return a generator or list.
:param bool or None spawn: True - Whether to call spawn_* hooks when using get_children or get_parents.
:rtype: list[TreeDiagram or NetworkDiagram or Any] """
if depth is None:
depth = -1
origins = [node for node in self.get_all() if not node.get_parents()]
if not origins:
raise AttributeError("Could not find any orphan nodes.")
func = self.get_children.__func__
order_func = self.get_parents.__func__
return _traverser(*origins, func=func, depth=depth, flat=flat, filt=filt, traverse_excluded=traverse_excluded, include_self=True, gen=gen, vertical=False, order_func=order_func, spawn=spawn)
class Storable:
""" Core methods to store, save and load attributes. """
def save_node(self):
return pickle.dumps(self)
@staticmethod
def load_node(pickled_bytes):
""" :rtype: TreeDiagram or NetworkDiagram or Any """
return pickle.loads(pickled_bytes)
def copy_node(self):
""" :rtype: TreeDiagram or NetworkDiagram or Any """
return self.load_node(pickled_bytes=self.save_node())
class _Diagram(_Diagram_Global, _Diagram_QOL, _Diagram_Visualize, Storable, metaclass=AutoInitBases):
""" Core methods of a Diagram. """
def __init__(self, parent=None):
# print(hasattr(self, "_children"))
self._children = [] # type: list[_Diagram]
self._parents = [] # type: list[_Diagram]
self.set_parent(parent=parent)
@deco_cast_to_self(if_not_base="_Diagram")
def set_parent(self, parent):
""" Set a new parent for this Node.
Returns parent.
:param TreeDiagram or NetworkDiagram or Any self:
:param TreeDiagram or NetworkDiagram or Any parent: """
if self._single_parent or parent is None:
for old_parent in self._parents:
old_parent._children.remove(self)
self._parents.clear()
# elif parent in self._parents:
# parent._children.remove(self)
# self._parents.remove(parent)
if parent is not None and parent not in self._parents:
self._parents.append(parent)
parent._children.append(self)
# Only for TreeDiagram, cannot import hook in this module
if hasattr(self, "_set_shared"):
self._set_shared(parent=parent)
return parent
def spawn_children(self): pass
def spawn_parents(self): pass
@_deco_depth
def get_children(self, depth=None, flat=None, filt=None, traverse_excluded=None, include_self=None, gen=None, vertical=None, spawn=None):
""" Down.
:param TreeDiagram or NetworkDiagram or Any self:
:param int or None depth: 0 - Depth of 0 will return/yield single direct layer. Get unlimited with -1. Previous layers are included.
:param bool or None flat: True - Whether to return/yield nodes directly or in lists. Ignored if vertical.
:param filt: Optional functional filter, expects 1 node as argument. Applies filter to ALL nodes, including self. See traverse_excluded.
:param bool or None gen: False - Whether to return a generator or list.
:param bool or None traverse_excluded: False - Whether to traverse a node even though it has been filtered out from result.
:param bool or None include_self: False
:param bool or None vertical: True - Whether to traverse one node at a time, or layer by layer.
:param bool or None spawn: True - Whether to call spawn_* hooks when using get_children or get_parents.
:rtype: list[TreeDiagram or NetworkDiagram or Any] """
if spawn:
self.spawn_children()
for child in self._children:
yield child
@_deco_depth
def get_parents(self, depth=None, flat=None, filt=None, traverse_excluded=None, include_self=None, gen=None, vertical=None, spawn=None):
""" Up.
:param TreeDiagram or NetworkDiagram or Any self:
:param int or None depth: 0 - Depth of 0 will return/yield single direct layer. Get unlimited with -1. Previous layers are included.
:param bool or None flat: True - Whether to return/yield nodes directly or in lists. Ignored if vertical.
:param filt: Optional functional filter, expects 1 node as argument. Applies filter to ALL nodes, including self. See traverse_excluded.
:param bool or None gen: False - Whether to return a generator or list.
:param bool or None traverse_excluded: False - Whether to traverse a node even though it has been filtered out from result.
:param bool or None include_self: False
:param bool or None vertical: True - Whether to traverse one node at a time, or layer by layer.
:param bool or None spawn: True - Whether to call spawn_* hooks when using get_children or get_parents.
:rtype: list[TreeDiagram or NetworkDiagram or Any] """
if spawn:
self.spawn_parents()
for parent in self._parents:
yield parent
@_deco_depth
def get_nodes(self, depth=None, flat=None, filt=None, traverse_excluded=None, include_self=None, gen=None, vertical=None, spawn=None):
""" Up + Down.
:param TreeDiagram or NetworkDiagram or Any self:
:param int or None depth: 0 - Depth of 0 will return/yield single direct layer. Get unlimited with -1. Previous layers are included.
:param bool or None flat: True - Whether to return/yield nodes directly or in lists. Ignored if vertical.
:param filt: Optional functional filter, expects 1 node as argument. Applies filter to ALL nodes, including self. See traverse_excluded.
:param bool or None gen: False - Whether to return a generator or list.
:param bool or None traverse_excluded: False - Whether to traverse a node even though it has been filtered out from result.
:param bool or None include_self: False
:param bool or None vertical: True - Whether to traverse one node at a time, or layer by layer.
:param bool or None spawn: True - Whether to call spawn_* hooks when using get_children or get_parents.
:rtype: list[TreeDiagram or NetworkDiagram or Any] """
for node in chain(self.get_parents(gen=True, spawn=spawn), self.get_children(gen=True, spawn=spawn)):
yield node
@_deco_depth
def get_siblings(self, depth=None, flat=None, filt=None, traverse_excluded=None, include_self=None, gen=None, vertical=None, spawn=None):
""" Up -> Down.
:param TreeDiagram or NetworkDiagram or Any self:
:param int or None depth: 0 - Depth of 0 will return/yield single direct layer. Get unlimited with -1. Previous layers are included.
:param bool or None flat: True - Whether to return/yield nodes directly or in lists. Ignored if vertical.
:param filt: Optional functional filter, expects 1 node as argument. Applies filter to ALL nodes, including self. See traverse_excluded.
:param bool or None gen: False - Whether to return a generator or list.
:param bool or None traverse_excluded: False - Whether to traverse a node even though it has been filtered out from result.
:param bool or None include_self: False
:param bool or None vertical: True - Whether to traverse one node at a time, or layer by layer.
:param bool or None spawn: True - Whether to call spawn_* hooks when using get_children or get_parents.
:rtype: list[TreeDiagram or NetworkDiagram or Any] """
yield from self._siblings_and_spouses(self.get_parents, self.get_children, spawn=spawn)
def _siblings_and_spouses(self, method1, method2, spawn):
# for node1 in method1.__func__(self, gen=True, spawn=spawn):
for node1 in method1.__func__(self, gen=True, spawn=True):
node_list = method2.__func__(node1, gen=False, spawn=spawn)
ordered_node_list = pivot_list(list_=node_list, index=node_list.index(self))
ordered_node_list.remove(self, )
for node2 in ordered_node_list:
yield node2
class TreeDiagram(_Diagram):
""" A Diagram where each node cannot have more than one parent. """
_single_parent = True
shared = ... # For type hinting
def __init__(self, parent=None):
pass
def _set_shared(self, parent):
""" A shared dictionary between all connected nodes.
Changing a nodes parent will change its own and recursive children's shared dict to its new parent's shared dict.
If new parent is None then a new shared dict is created.
Shared dicts are never merged, do that explicitly if needed.
Todo: Shared dict for NetworkDiagram, resolve logic with multiple parents.
:param TreeDiagram or NetworkDiagram or Any self: """
if getattr(parent, "shared", object()) is not self.shared:
new_shared = {} if parent is None else parent.shared
for part in self.get_children(depth=-1, gen=True, include_self=True, spawn=False):
part.shared = new_shared
assert self.shared is not Ellipsis
def view(self, indent=1, relative=False, custom_repr=None, spacer=" ", spawn=False, filt=None, traverse_excluded=False, vertical=True, print_out=True):
""" Get a printable string showing a clear view of this TreeDiagram structure.
Hides additional lines of a node's repr. """
if relative:
top = self.copy_node()
top.set_parent(parent=None)
else:
top = self
lines = []
for node in top.get_children(depth=-1, gen=True, include_self=True, spawn=spawn, filt=filt, traverse_excluded=traverse_excluded, vertical=vertical):
lanes = []
all_parents = node.get_parents(depth=-1, spawn=spawn)
if all_parents:
del all_parents[-1]
all_parents.insert(0, node)
for i, parent in enumerate(all_parents): # type: int, TreeDiagram
sibling_index = parent.get_index()
if i == 0:
if len(parent.get_siblings(spawn=spawn)) == sibling_index:
lane = f"└{'─' * indent}{spacer}"
else:
lane = f"├{'─' * indent}{spacer}"
else:
if len(parent.get_siblings(spawn=spawn)) == sibling_index:
lane = f"{spacer}{spacer * indent}{spacer}"
else:
lane = f"│{spacer * indent}{spacer}"
lanes.insert(0, lane)
node_str = str(custom_repr(node)) if custom_repr else repr(node)
if "\n" in node_str:
node_str = f"{node_str.splitlines()[0]} ..."
lines.append(f"{''.join(lanes)}{node_str}")
if relative:
top.remove_node()
view = "\n".join(lines)
if print_out:
print(view)
return view
class NetworkDiagram(_Diagram):
""" A Diagram where each node can have any amount parents. """
_single_parent = False
def __init__(self, parent=None):
pass
@_deco_depth
def get_spouses(self, depth=None, flat=None, filt=None, traverse_excluded=None, include_self=None, gen=None, vertical=None, spawn=None):
""" Down -> Up.
:param TreeDiagram or NetworkDiagram or Any self:
:param int or None depth: 0 - Depth of 0 will return/yield single direct layer. Get unlimited with -1. Previous layers are included.
:param bool or None flat: True - Whether to return/yield nodes directly or in lists. Ignored if vertical.
:param filt: Optional functional filter, expects 1 node as argument. Applies filter to ALL nodes, including self. See traverse_excluded.
:param bool or None gen: False - Whether to return a generator or list.
:param bool or None traverse_excluded: False - Whether to traverse a node even though it has been filtered out from result.
:param bool or None include_self: False
:param bool or None vertical: True - Whether to traverse one node at a time, or layer by layer.
:param bool or None spawn: True - Whether to call spawn_* hooks when using get_children or get_parents.
:rtype: list[TreeDiagram or NetworkDiagram or Any] """
yield from self._siblings_and_spouses(self.get_children, self.get_parents, spawn=spawn)
def get_spouse(self, index=None, depth=None, filt=None, traverse_excluded=None, include_self=None, spawn=None):
""" Singular QOL alternative for get_spouses().
:param TreeDiagram or NetworkDiagram or Any self:
:param int or None index: Index of node to be returned. Possible filter is applied before.
:param int or None depth: Default depth of 0 will return single direct layer. Get unlimited with -1. Previous layers are included.
:param filt: Optional functional filter, expects 1 node as argument. Applies filter to ALL nodes, including self. See traverse_excluded.
:param bool or None traverse_excluded: False - Whether to traverse a node even though it has been filtered out from result.
:param bool or None include_self: False
:param bool or None spawn: True - Whether to call spawn_* hooks when using get_children or get_parents.
:rtype: TreeDiagram or NetworkDiagram or Any """
return self._singular_alternative(self.get_spouses, index=index, depth=depth, filt=filt, traverse_excluded=traverse_excluded, include_self=include_self, spawn=spawn)
class Markdown(TreeDiagram):
""" A section for a markdown file, built on TreeDiagram. """
def __init__(self, *lines, header=None, parent=None):
self.header = header
self.lines = []
self.add_lines(*lines)
def __str__(self):
return '\n'.join(self.get_all_lines())
def __contains__(self, item):
return str(self).__contains__(item)
@staticmethod
def link(text, header=None, url=None, href=False, enabled=True):
""" Return a link to a header or url.
Enable `href` if inside a non-formatting tag such as <pre>. """
if not enabled:
return text
if url is not None:
link = url
else:
if header is None:
header = text
link = f"#{str(header).replace(' ', '-').replace(':', '')}"
if href:
return f"<a href='{link}'>{text}</a>"
return f"[{text}]({link})"
def get_section_lines(self):
""" Get a list of all lines in this section. """
lines = self.lines.copy()
if self.header:
lines.insert(0, f"{'#' * clamp(1 + len(self.get_parents(depth=-1)), 1, 6)} {self.header}")
return lines
def add_lines(self, *lines):
""" Add lines to list, using splitlines. """
for line in lines: # type: str
self.lines.extend(str(line).splitlines())
return self
def get_all_lines(self):
""" Get a list of all lines in this entire Markdown by iterating all children.
:rtype: list[str] """
lines = []
for markdown in self.get_all():
if lines:
lines.append("")
lines.extend(markdown.get_section_lines())
return lines
def add_table_lines(self, *dicts, sort_by=None):
""" Add a table to the lines with packed dicts using pandas `to_markdown`. """
df = pandas.DataFrame(dicts)
if sort_by is not None:
df = df.sort_values(by=sort_by)
self.add_lines(df.to_markdown(index=False))
return self
def add_list_lines(self, *items, indent=0):
""" Add list lines. """
self.add_lines(*[f"{' ' * indent} - {item}" for item in items])
return self
def add_code_lines(self, *lines):
""" Add code lines, wrapped by quotes. """
self.add_lines(*lines)
self.wrap_with_tags("```")
return self
def add_pre_lines(self, *lines):
self.add_lines(*lines)
self.wrap_with_tags("pre")
return self
def wrap_with_tags(self, *tags):
non_tags = "```",
for tag in tags:
if tag in non_tags:
self.lines.insert(0, tag)
self.lines.append(tag)
else:
self.lines.insert(0, f"<{tag}>")
self.lines.append(f"</{tag}>")
return self
# class _Diagram_Graph:
# def __init__(self):
# self.loops = set()
#
# def graph(self):
# """ :param TreeDiagram or NetworkDiagram or Any self: """
# loops = self.get_loops()
#
# self._relate_loops(loops=loops)
# top_loop = loops[0].get_parent(-1, -1, include_self=True) # type: Loop
# top_loop.view()
#
# print(top_loop.available_nodes()) # d shouldn't be available - Maybe try zones instead
# print(top_loop.unavailable_nodes())
#
# return loops
#
# def get_links(self):
# """ Return a set of sets containing two nodes, paired by child and/or parent.
#
# :param TreeDiagram or NetworkDiagram or Any self: """
# links = set()
# for node in self.get_all():
# links.update({frozenset({node, child}) for child in node.get_children()})
# links.update({frozenset({node, parent}) for parent in node.get_parents()})
# return links
#
# def get_loops(self):
# """ Get a list of all unrelated Loops.
#
# :param TreeDiagram or NetworkDiagram or Any self:
# :rtype: list[Loop] """
# loops = self._yield_all_loops()
# loops = self._exclude_mirrored_loops(loops=loops)
# loops = self._extract_smallest_loops(loops=loops)
# self._assign_loops_to_nodes(loops=loops)
# return loops
#
# # def _relate_loops(self, loops):
# # """ :param TreeDiagram or NetworkDiagram or Any self: """
# # related = []
# # for loop in loops:
# # if not related:
# # related.append(loop)
# # else:
# # for related_loop in related:
# # if related_loop.can_contain(loop=loop):
# # related_loop.add_node(child=loop)
# # break
# # else:
# # raise AssertionError("Failed relating loops.")
#
# def _relate_loops(self, loops):
# """ Prioritize making top loops parents by iterating children horizontally.
#
# :param TreeDiagram or NetworkDiagram or Any self: """
# top = Loop()
# for loop in loops:
# for related_loop in top.get_children(depth=-1, include_self=True, vertical=False, gen=True):
# if related_loop.can_contain(loop=loop):
# related_loop.add_node(child=loop)
# break
# else:
# raise AssertionError(f"Failed finding parent for loop {loop}")
#
# def _extract_smallest_loops(self, loops):
# """ :param TreeDiagram or NetworkDiagram or Any self: """
# while True:
# for loop in loops:
# loop_links = loop.get_loop_links()
# other_loops = loops.copy()
# other_loops.remove(loop)
#
# # See if smaller or equal loops have all of the loop_nodes combined
# for loop2 in other_loops:
# if len(loop2.nodes) <= len(loop.nodes):
# loop_links -= loop2.get_loop_links()
#
# if not loop_links:
# loops.remove(loop)
# break
# else:
# break
#
# return loops
#
# def _assign_loops_to_nodes(self, loops):
# """ :param TreeDiagram or NetworkDiagram or Any self: """
# for node in self.get_all():
# node.loops.clear()
# for loop in loops:
# for node in loop.nodes:
# node.loops.add(loop)
#
# def _exclude_mirrored_loops(self, loops):
# """ :param TreeDiagram or NetworkDiagram or Any self: """
# mirrored_loops = []
# for loop in loops:
# if not any([loop.equals(old_loop) for old_loop in mirrored_loops]):
# mirrored_loops.append(loop)
# return mirrored_loops
#
# def _yield_all_loops(self, *nodes):
# """ :param TreeDiagram or NetworkDiagram or Any self: """
# nodes = list(nodes) + [self]
# for node in self.get_nodes():
# if node in nodes:
# if node is not nodes[-2]:
# index = nodes.index(node)
# yield Loop(*nodes[index:])
# else:
# yield from node._yield_all_loops(*nodes)
# class Loop(TreeDiagram):
# def __init__(self, *nodes):
# self.nodes = list(nodes)
#
# def __repr__(self):
# return f"Loop: {self.nodes}"
#
# def get_loop_links(self):
# return {frozenset({node, self.next_node(node=node)}) for node in self.nodes}
#
# @property
# def nodes_set(self):
# return set(self.nodes)
#
# def _next_prev_node(self, node, incr):
# index = confineTo(value=self.nodes.index(node) + incr, minimum=0, maximum=len(self.nodes) - 1, margin=0.5)
# return self.nodes[index]
#
# def next_node(self, node):
# return self._next_prev_node(node=node, incr=1)
#
# def prev_node(self, node):
# return self._next_prev_node(node=node, incr=-1)
#
# def nearby_nodes(self, node):
# return self.next_node(node=node), self.prev_node(node=node)
#
# def all_nodes(self):
# return set.union(*[loop.nodes_set for loop in self.get_all(gen=True)])
#
# def equals(self, loop):
# return len(self.nodes) == len(loop.nodes) and not (self.nodes_set - loop.nodes_set)
# # return len(self.nodes) == len(loop.nodes) and not subtract_list(self.nodes, loop.nodes)
#
# def get_connected_loops(self):
# loops = set.union(*[node.loops for node in self.nodes])
# loops.remove(self)
# return loops
#
# def get_shared_nodes(self, loop):
# """ Symmetrical. """
# return self.nodes_set.intersection(loop.nodes_set)
#
# def get_exclusive_nodes(self, loop):
# """ Not symmetrical. """
# return self.nodes_set - self.get_shared_nodes(loop=loop)
#
# def get_edge_nodes(self, loop):
# """ Symmetrical. """
# exclusive = self.get_exclusive_nodes(loop=loop) # Could be possible that there are 0 exclusive nodes
# return set(flatten(map(self.nearby_nodes, exclusive))) - exclusive
#
# # --- Folding logic below ---
#
# def get_blocked_nodes(self, loop):
# """ Selfs' nodes that loop is blocking.
# Symmetrical. """
# return self.get_shared_nodes(loop=loop) - self.get_edge_nodes(loop=loop)
#
# def available_nodes(self):
# """ Set of nodes that are available inside self. """
# nodes = self.nodes_set
# for loop in self.get_children():
# nodes -= self.get_blocked_nodes(loop=loop)
# nodes.update(loop.get_exclusive_nodes(loop=self))
# return nodes
#
# def unavailable_nodes(self):
# """ Set of nodes that exist but are not available inside self. """
# return self.all_nodes() - self.available_nodes()
#
# def can_contain(self, loop):
# assert loop.get_parent() is None and loop.get_child() is None
#
# return not loop.nodes_set.intersection(self.unavailable_nodes())