Git Product home page Git Product logo

asciigraf's Introduction

asciigraf

Maintainability

Asciigraf is a python library that turns ascii diagrams of networks into network objects. It returns a networkx graph of nodes for each alpha-numeric element in the input text; nodes are connected in the graph to match the edges represented in the diagram by -, /, \ and |.

Installation

Asciigraf can be installed from pypi using pip:

~/$ pip install asciigraf

Usage

Asciigraf expects a string containg a 2-d ascii diagram. Nodes can be an alphanumeric string composed of words, sentences and punctuation (for a look at what is all tested to work, see the node recognition tests). Edges can be composed of -, /, \ and |.

import asciigraf

network = asciigraf.graph_from_ascii("""
          NodeA-----
                   |
                   |---NodeB
                                     """)

print(network)
>>> <networkx.classes.graph.Graph at 0x7f24c3a8b470>

print(network.edges())
>>> [('NodeA', 'NodeB')]

print(network.nodes())
>>> ['NodeA', 'NodeB']

Networkx provides tools to attach data to graphs, nodes and edges, and asciigraf leverages these in a number of ways; in the example below you can see that asciigraf uses this to attach a x, y position tuple to each node indicating the line/col position of each node ( 0,0 is at the top-left). It also attaches a length attribute to each edge which matches the number of characters in that edge, as well as a list of positions for each character an edge. In addition, the input data is attached as a graph attribute ascii_string for reference.

print(network.nodes(data=True))
>>> [('NodeA', {'position': (10, 1)}), ('NodeB', {'position': (23, 3)})]

print(network.edges(data=True))
>>> [('NodeA', 'NodeB', OrderedDict([('length', 10), 'points', [...]))]

print(network.edge['NodeA']['NodeB']['points'])
>>> [(15, 1), (16, 1), (17, 1), (18, 1),
     (19, 1), (19, 2), (19, 3), (20, 3), (21, 3), (22, 3)]

print(network.graph["ascii_string"])
>>>
    NodeA-----
             |
             |---NodeB

Asciigraf also lets you annotate the edges of graphs using in-line labels --- denoted by parentheses. The contents of the label will be attached to the edge on which it is drawn with the attribute name label.

network = asciigraf.graph_from_ascii("""

    A---(nuts)----B----(string)---C
                  |
                  |
                  |
                  D---(pebbles)----E

""")

print(network.get_edge_data("A", "B")["label"])
>>> nuts

print(network.get_edge_data("B", "C")["label"])
>>> string

print(network.get_edge_data("D", "E")["label"])
>>> pebbles

print(hasattr(network.get_edge_data("B", "D"), "label"))
>>> False

Have fun!

import asciigraf


network = asciigraf.graph_from_ascii("""
          s---p----1---nx
         /    |        |
        /     |        0---f
       6l-a   c--
      /   |      \--k
     /   ua         |  9e
    q      \        | /
            \-r7z   jud
                \    |
                 m   y
                  \  |
                   v-ow
                             """)

asciigraf's People

Contributors

anjoman avatar emilyylma avatar etimberg avatar greatestape avatar yangineer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

asciigraf's Issues

Asciigraf should report the length of each branch

Currently, asciigraf returns a networkx graph datastructure where every node has a position relative to the top-left corner of the string in which the network is drawn. It would be great if the graph also came with a length on each edge which indicated the number of characters in the edge. For example:

asciigraf.graph_from_ascii("""
            (13)           (10)
        n0-------------n1----------n2
                       |
                       |  (3)
                       |
                       n4
""").nodes(data=True)
>>> [('n0', {'position': Point(3, 8)}),
        ('n1', {'position': Point(3, 23)}),
        ('n2', {'position': Point(3, 35)}),
        ('n4', {'position': Point(7, 24)})]

If we did this, you could do something like

asciigraf.graph_from_ascii("""
            (13)           (10)
        n0-------------n1----------n2
                       |
                       |  (3)
                       |
                       n4
""").edges(data=True)
>>> [('n0', 'n1', {'length': 13}),
        ('n1', 'n2', {'length': 10}),
        ('n1', 'n4', {'length': 3})]

Support for edge modifiers

It would be valuable to be able to mark an edge with a modifier. E.G. in the below graph, the edge (n2, n3) has an arrow modifier which could be used to make a directed graph, or to modify the significance of the edge in some other way.

     n1---------n2
                ^
                |
                n3

We already support edge labels such as n1---(label_1)---n2 but this change would allow the user to have modifiers and labels on the same edge

``n1<---(edge_3)--->n2`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.