modernize some Python code

Since we now require Python 3.6, we can use f-strings instead of
format() to make the code more readable.

* doc/org/tut01.org, doc/org/tut02.org, doc/org/tut03.org,
doc/org/tut21.org, doc/org/tut24.org, doc/org/tut90.org,
python/spot/__init__.py, python/spot/jupyter.py, tests/python/acc.py,
tests/python/acc_cond.ipynb, tests/python/complement_semidet.py,
tests/python/decompose.ipynb, tests/python/formulas.ipynb,
tests/python/highlighting.ipynb, tests/python/ipnbdoctest.py,
tests/python/ltlf.py, tests/python/parity.ipynb,
tests/python/product.ipynb, tests/python/relabel.py,
tests/python/satmin.ipynb, tests/python/stutter-inv.ipynb,
tests/python/twagraph-internals.ipynb, tests/python/zlktree.ipynb: Use
f-strings.
This commit is contained in:
Alexandre Duret-Lutz 2024-03-26 20:35:10 +01:00
parent df44f7a5c2
commit 7ac570fa3f
23 changed files with 292 additions and 327 deletions

View file

@ -389,7 +389,8 @@ In C++ you can enable lenient using one of the Boolean arguments of
Formulas have a custom format specification language that allows you
to easily change the way a formula should be output when using the
=format()= method of strings.
=format()= method of strings, or using [[https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals][formatted string litterals]].
#+BEGIN_SRC python
import spot

View file

@ -68,8 +68,8 @@ import spot
m = spot.relabeling_map()
g = spot.relabel('"Proc@Here" U ("var > 10" | "var < 4")', spot.Pnn, m)
for newname, oldname in m.items():
print("#define {} ({})".format(newname.to_str(), oldname.to_str('spin', True)))
print(g.to_str('spin', True))
print(f"#define {newname.to_str()} ({oldname.to_str('spin', True)})")
print(g.to_str('spin', True))
#+END_SRC
#+RESULTS:

View file

@ -159,9 +159,9 @@ The Python equivalent is similar:
# kindstr() prints the name of the operator
# size() return the number of operands of the operators
print("{}, {} children".format(f.kindstr(), f.size()))
print(f"{f.kindstr()}, {f.size()} children")
# [] accesses each operand
print("left: {f[0]}, right: {f[1]}".format(f=f))
print(f"left: {f[0]}, right: {f[1]}")
# you can also iterate over all operands using a for loop
for child in f:
print(" *", child)

View file

@ -557,9 +557,9 @@ Here is the very same example, but written in Python:
print("Stutter Invariant:", aut.prop_stutter_invariant())
for s in range(0, aut.num_states()):
print("State {}:".format(s))
print(f"State {s}:")
for t in aut.out(s):
print(" edge({} -> {})".format(t.src, t.dst))
print(f" edge({t.src} -> {t.dst})")
# bdd_print_formula() is designed to print on a std::ostream, and
# is inconvenient to use in Python. Instead we use
# bdd_format_formula() as this simply returns a string.

View file

@ -190,26 +190,26 @@ decide whether to enclose the destinations in braces.
Here is the Python version of this code:
#+BEGIN_SRC python
import spot
import spot
aut = spot.automaton("tut24.hoa")
bdict = aut.get_dict()
init = aut.get_init_state_number()
ui = aut.is_univ_dest(init)
print("Initial states: {}{}{}".format("{ " if ui else "",
" ".join(map(str, aut.univ_dests(init))),
" }" if ui else ""))
for s in range(0, aut.num_states()):
print("State {}:".format(s))
for t in aut.out(s):
ud = aut.is_univ_dest(t)
print(" edge({} -> {}{}{})".format(t.src,
"{ " if ud else "",
" ".join(map(str, aut.univ_dests(t))),
" }" if ud else ""))
print(" label =", spot.bdd_format_formula(bdict, t.cond))
print(" acc sets =", t.acc)
aut = spot.automaton("tut24.hoa")
def format_dest(s):
if not aut.is_univ_dest(s):
return s
else:
return f'{{ {" ".join(map(str, aut.univ_dests(s)))} }}'
bdict = aut.get_dict()
init = aut.get_init_state_number()
print(f"Initial states: {format_dest(init)}")
for s in range(0, aut.num_states()):
print("State {}:".format(s))
for t in aut.out(s):
print(f" edge({t.src} -> {format_dest(t.dst)})")
print(" label =", spot.bdd_format_formula(bdict, t.cond))
print(" acc sets =", t.acc)
#+END_SRC
#+RESULTS:

View file

@ -108,12 +108,12 @@ import spot
aut1 = spot.make_twa_graph()
ap1a = aut1.register_ap("a")
ap1b = aut1.register_ap("b")
print("aut1: a={} b={}".format(ap1a, ap1b))
print(f"aut1: a={ap1a} b={ap1b}")
aut2 = spot.make_twa_graph()
ap2c = aut2.register_ap("c")
ap2b = aut2.register_ap("b")
ap2a = aut2.register_ap("a")
print("aut1: a={} b={} c={}".format(ap2a, ap2b, ap2c))
print(f"aut1: a={ap2a} b={ap2b} c={ap2c}")
#+END_SRC
#+RESULTS:
@ -271,7 +271,7 @@ all transition that belong to a certain acceptance set.
self.set.add((src, guard, dst))
def str_trans(self, src, guard, dst):
f = spot.bdd_format_formula(self.dict, guard)
return "({},{},{})".format(src, f, dst)
return f"({src},{f},{dst})"
def __str__(self):
return '{' + ",".join([ self.str_trans(*t) for t in self.set ]) + '}'
@ -353,7 +353,7 @@ automaton registers its variables.
self.set.add((src, guard, dest))
def str_trans(self, src, guard, dest):
f = spot.bdd_format_formula(self.dict, guard)
return "({},{},{})".format(src, f, dest)
return f"({src},{f},{dest})"
def __str__(self):
return '{' + ",".join([ self.str_trans(*t) for t in self.set ]) + '}'