mirror of
https://github.com/littlefs-project/littlefs.git
synced 2025-12-01 12:20:02 +00:00
scripts: Added support for SI-prefixes as iI punescape modifiers
This adds %i and %I as punescape modifiers for limited printing of integers with SI prefixes: - %(field)i - base-10 SI prefixes - 100 => 100 - 10000 => 10K - 0.01 => 10m - %(field)I - base-2SI prefixes - 128 => 128 - 10240 => 10Ki - 0.125 => 128mi These can also easily include units as a part of the punescape string: - %(field)iops/s => 10Kops/s - %(field)IB => 10KiB This is particularly useful in plotmpl.py for adding explicit x/yticklabels without sacrificing the automatic SI-prefixes.
This commit is contained in:
@ -614,7 +614,7 @@ def punescape(s, attrs=None):
|
||||
'|' '%u....'
|
||||
'|' '%U........'
|
||||
'|' '%\((?P<field>[^)]*)\)'
|
||||
'(?P<format>[+\- #0-9\.]*[sdboxXfFeEgG])')
|
||||
'(?P<format>[+\- #0-9\.]*[siIdboxXfFeEgG])')
|
||||
def unescape(m):
|
||||
if m.group()[1] == '%': return '%'
|
||||
elif m.group()[1] == 'n': return '\n'
|
||||
@ -634,10 +634,16 @@ def punescape(s, attrs=None):
|
||||
if isinstance(v, str):
|
||||
v = dat(v, 0)
|
||||
v = int(v)
|
||||
elif f[-1] in 'fFeEgG':
|
||||
elif f[-1] in 'iIfFeEgG':
|
||||
if isinstance(v, str):
|
||||
v = dat(v, 0)
|
||||
v = float(v)
|
||||
if f[-1] in 'iI':
|
||||
v = (si if 'i' in f[-1] else si2)(v)
|
||||
f = f.replace('i', 's').replace('I', 's')
|
||||
if '+' in f and not v.startswith('-'):
|
||||
v = '+'+v
|
||||
f = f.replace('+', '').replace('-', '')
|
||||
else:
|
||||
f = ('<' if '-' in f else '>') + f.replace('-', '')
|
||||
v = str(v)
|
||||
@ -655,7 +661,7 @@ def psplit(s):
|
||||
'|' '%u....'
|
||||
'|' '%U........'
|
||||
'|' '%\((?P<field>[^)]*)\)'
|
||||
'(?P<format>[+\- #0-9\.]*[sdboxXfFeEgG])')
|
||||
'(?P<format>[+\- #0-9\.]*[siIdboxXfFeEgG])')
|
||||
return [m.group() for m in re.finditer(pattern.pattern + '|.', s)]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user