Skip to main content

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

Project description

https://travis-ci.org/opusonesolutions/asciigraf.svg?branch=master https://coveralls.io/repos/github/opusonesolutions/asciigraf/badge.svg?branch=master https://img.shields.io/badge/License-MIT-yellow.svg https://badge.fury.io/py/asciigraf.svg https://img.shields.io/pypi/pyversions/asciigraf.svg 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 characters in A-Z, a-z, 0-9, and _, {, }. 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 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 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, as well as a list of positions for each character an edge

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

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


Download files

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

Source Distribution

asciigraf-0.9.1a9.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asciigraf-0.9.1a9-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file asciigraf-0.9.1a9.tar.gz.

File metadata

  • Download URL: asciigraf-0.9.1a9.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for asciigraf-0.9.1a9.tar.gz
Algorithm Hash digest
SHA256 44e5c4f4fc5c5e0a173141e629506f072de22aa2f77dd9d163b4a22afd46e18c
MD5 58988116c3f2e20024e56c9b066c5b5d
BLAKE2b-256 042a9751b9a64e9f8020953df4458468a10b49f40241ee1fc2fc8dae9c4682b5

See more details on using hashes here.

File details

Details for the file asciigraf-0.9.1a9-py3-none-any.whl.

File metadata

  • Download URL: asciigraf-0.9.1a9-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for asciigraf-0.9.1a9-py3-none-any.whl
Algorithm Hash digest
SHA256 bb21811731ae9f2a1d28b313cdff4a78a030cd23d303466e40b6ffd969922eb9
MD5 0b1f3bbe9bb2489a5422cf5f28ce8f26
BLAKE2b-256 461cc29b8efa694166b5c7e85109f1305a30d0fba4429d2099b653ef409ebac3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page