Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

A python library for making ascii-art into network graphs.

Project description

https://travis-ci.org/AnjoMan/asciigraf.svg?branch=master https://coveralls.io/repos/github/AnjoMan/asciigraf/badge.svg?branch=master

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 characters in A-Z, a-z, 0-9, and _, {, }. Edges can be composed of -, /, \ and | (with some restrictions on how corners work — see the tests).

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 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 Point object to each node indicating where on the (x, y) plane each node starts ( 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.

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

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

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---(string)----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"])
>>> string

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
                             """)

Project details


Release history Release notifications

This version
History Node

0.3.0

History Node

0.2.0

History Node

0.1.3

History Node

0.1.2

History Node

0.1.1

History Node

0.1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
asciigraf-0.3.0.tar.gz (4.2 kB) Copy SHA256 hash SHA256 Source None Oct 30, 2017

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page