python: support operator rewriting in __format__

Fixes #168.

* python/spot/__init__.py: Implement it.
* tests/python/formulas.ipynb: Test it.
* NEWS: Mention it.
This commit is contained in:
Alexandre Duret-Lutz 2016-05-01 18:58:08 +02:00
parent d9174593c8
commit e91c6ba2f1
3 changed files with 23 additions and 4 deletions

View file

@ -230,6 +230,8 @@ class formula:
quotes are *not* added)
- 'q': quote and escape for shell output, using single
quotes or double quotes depending on the contents.
- '[...]': rewrite away all the operators specified in brackets,
using spot.unabbreviate().
- ':spec': pass the remaining specification to the
formating function for strings.
@ -240,6 +242,8 @@ class formula:
parent = False
escape = None
form = self
while spec:
c, spec = spec[0], spec[1:]
if c in ('f', 's', '8', 'l', 'w', 'x', 'X'):
@ -250,10 +254,16 @@ class formula:
escape = c
elif c == ':':
break
elif c == '[':
pos = spec.find(']')
if pos < 0:
raise ValueError("unclosed bracket: [" + spec)
form = form.unabbreviate(spec[0:pos])
spec = spec[pos+1:]
else:
raise ValueError("unknown format specification: " + c + spec)
s = self.to_str(syntax, parent)
s = form.to_str(syntax, parent)
if escape == 'c':
o = ostringstream()