Skip to content

Instantly share code, notes, and snippets.

@asdf8601
Last active March 29, 2024 12:37
Show Gist options
  • Select an option

  • Save asdf8601/2d3bc321405b1991277fd6001060df0d to your computer and use it in GitHub Desktop.

Select an option

Save asdf8601/2d3bc321405b1991277fd6001060df0d to your computer and use it in GitHub Desktop.

Revisions

  1. @mmngreco mmngreco revised this gist Mar 29, 2024. 1 changed file with 8 additions and 19 deletions.
    27 changes: 8 additions & 19 deletions dot2ascii.py
    Original file line number Diff line number Diff line change
    @@ -2,24 +2,9 @@
    """
    Examples
    --------
    >>> pipx run main.py help
    ... text = '''
    ... graph {
    ... rankdir=LR
    ... A [label="Christmas"]
    ... B [label="Go shopping"]
    ... C [label="Let me think"]
    ... D [label="Laptop"]
    ... E [label="iPhone"]
    ... F [label="Car"]
    ... A -> B [label="Get money"]
    ... B -> C
    ... C -> D [label="One"]
    ... C -> E [label="Two"]
    ... C -> F [label="Three"]
    ... }
    ... '''
    $ pipx run https://gist.githubusercontent.com/mmngreco/2d3bc321405b1991277fd6001060df0d/raw/dot2ascii.py help
    $ pipx run https://gist.githubusercontent.com/mmngreco/2d3bc321405b1991277fd6001060df0d/raw/dot2ascii.py "graph {a -- b -- c}"
    $ echo "graph {rankdir=LR; a -- b -- c }" | pipx run https://gist.githubusercontent.com/mmngreco/2d3bc321405b1991277fd6001060df0d/raw/dot2ascii.py
    """
    # /// script
    # requires-python = ">=3.10"
    @@ -46,7 +31,7 @@ def dot_to_ascii(dot: str, fancy: bool = True):
    return response


    if __name__ == '__main__':
    def app():
    import sys
    if len(sys.argv) > 1:
    text = sys.argv[1]
    @@ -57,3 +42,7 @@ def dot_to_ascii(dot: str, fancy: bool = True):
    text = sys.stdin.read()
    ascii = dot_to_ascii(text)
    print(ascii)


    if __name__ == '__main__':
    app()
  2. @mmngreco mmngreco created this gist Mar 29, 2024.
    59 changes: 59 additions & 0 deletions dot2ascii.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    #!python3
    """
    Examples
    --------
    >>> pipx run main.py help
    ... text = '''
    ... graph {
    ... rankdir=LR
    ... A [label="Christmas"]
    ... B [label="Go shopping"]
    ... C [label="Let me think"]
    ... D [label="Laptop"]
    ... E [label="iPhone"]
    ... F [label="Car"]
    ... A -> B [label="Get money"]
    ... B -> C
    ... C -> D [label="One"]
    ... C -> E [label="Two"]
    ... C -> F [label="Three"]
    ... }
    ... '''
    """
    # /// script
    # requires-python = ">=3.10"
    # dependencies = [
    # "requests",
    # ]
    # ///

    import requests

    def dot_to_ascii(dot: str, fancy: bool = True):
    url = 'https://dot-to-ascii.ggerganov.com/dot-to-ascii.php'
    boxart = 0
    if fancy:
    # use nice box drawing char instead of + , | , -
    boxart = 1
    params = {
    'boxart': boxart,
    'src': dot,
    }
    response = requests.get(url, params=params).text
    if response == '':
    raise SyntaxError('DOT string is not formatted correctly')
    return response


    if __name__ == '__main__':
    import sys
    if len(sys.argv) > 1:
    text = sys.argv[1]
    if text == "help":
    print("Usage: python main.py [dot_string]")
    sys.exit(0)
    else:
    text = sys.stdin.read()
    ascii = dot_to_ascii(text)
    print(ascii)