Commit 54a8ce50 authored by Alexandre Duret-Lutz's avatar Alexandre Duret-Lutz
Browse files

python: LRU cache for the dot->svg conversion

* wrap/python/spot.py: Here.
parent 31b3862f
...@@ -20,17 +20,26 @@ ...@@ -20,17 +20,26 @@
from spot_impl import * from spot_impl import *
import subprocess import subprocess
import sys import sys
from functools import lru_cache
# Global BDD dict so that we do not have to create one in user code.
_bdd_dict = make_bdd_dict() _bdd_dict = make_bdd_dict()
def _ostream_to_svg(ostr): # Add a small LRU cache so that when we display automata into a
# interactive widget, we avoid some repeated calls to dot for
# identical inputs.
@lru_cache(maxsize=64)
def _str_to_svg(str):
dotty = subprocess.Popen(['dot', '-Tsvg'], dotty = subprocess.Popen(['dot', '-Tsvg'],
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE) stdout=subprocess.PIPE)
dotty.stdin.write(ostr.str().encode('utf-8')) dotty.stdin.write(str)
res = dotty.communicate() res = dotty.communicate()
return res[0].decode('utf-8') return res[0].decode('utf-8')
def _ostream_to_svg(ostr):
return _str_to_svg(ostr.str().encode('utf-8'))
def _render_automaton_as_svg(a, opt=None): def _render_automaton_as_svg(a, opt=None):
ostr = ostringstream() ostr = ostringstream()
dotty_reachable(ostr, a, opt) dotty_reachable(ostr, a, opt)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment