Python中节点图的ASCII可视化

18

我有一个叫做Node的类。

class Node:
   def __init__(self,name, childList, parentList):
      self.name = name
      # a list of all nodes which are children of this node
      # may have length 0 to many
      self.childList = childList 
      # a list of all nodes which are parents of this node
      # may have length 0 to many
      self.parentList = parentList
我有一个节点列表(nodeList)。这些节点可能会在彼此的父节点列表或子节点列表中。我想能够将它们在子节点列表和父节点列表中的关系可视化到stdout上(以ASCII绘图方式呈现)。
例如,下面的名称是节点列表中节点的名称。
                           Classifier
                                |
                                |
                         FeatureCombiner
                          /           \
                         /             \
                        /               \
               FeatureGenerator1     FeatureGenerator2
                      \                     /
                       \                   /
                        \                 /
                         \               /
                          \             /
                           \           /
                            \         /
                            Image Loader
分类器具有一个空的parentList和一个长度为1的childList,其中包含FeatureCombiner。FeatureGenerator1和2具有相同的parent和childList,分别包含FeatureCombiner和Image Loader。Image Loader具有一个空的childList和一个包含FeatureGenerator1和2的parentList。

提前感谢, Matt

3个回答

11
我们在DVC项目中遇到了类似的问题,曾经潜伏并尝试将Graph::Easy移植到Python中,但我们意识到没有跨平台的Python库可以做到这一点,而且不需要像Graphviz那样依赖重。所以我们最终使用了一个名为Grandalf的神奇库来处理布局,然后我们自己以ASCII格式呈现结果,并使用分页器(这就是像git log这样的东西如何使其输出既漂亮又可水平和垂直滚动)。在此处查看整个代码
+-------------------+           +--------------------+
| test_data.csv.dvc |           | train_data.csv.dvc |
+-------------------+           +--------------------+
                  **              **                  
                    ***        ***                    
                       **    **                       
                +-------------------+                 
                | featurization.dvc |                 
                +-------------------+                 
                  ***            ***                  
                **                  ***               
              **                       **             
    +--------------+                     **           
    | training.dvc |                   **             
    +--------------+                ***               
                  ***            ***                  
                     **        **                     
                       **    **                       
                     +---------+                      
                     | Dvcfile |                      
                     +---------+                      

2
这真的很棒 :) - Josh Bode

8

谢谢,这些看起来可以用图像格式做到。我之前只考虑了二进制,因为我认为那会更容易。 - aultimus

5
也许可以从Perl的Graph::Easy中移植ASCII图形布局逻辑?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接