summaryrefslogtreecommitdiff
path: root/pint/formatting.py
diff options
context:
space:
mode:
authorkeewis <keewis@users.noreply.github.com>2022-02-23 23:06:21 +0100
committerGitHub <noreply@github.com>2022-02-23 23:06:21 +0100
commitff15a1483b3b823bb35866436e084ba07a408377 (patch)
treef6b1a3ecda77dc2407ca9e78073d652d461ee8b7 /pint/formatting.py
parent43287b365e647673978dac6e4f9f8f11c6ef9469 (diff)
downloadpint-ff15a1483b3b823bb35866436e084ba07a408377.tar.gz
properly deprecate the current behavior of the `default_format` option (#1419)
* ignore magnitude formats for units * convert non-truthy to empty string * match longer flags first i.e. match `cfunits` before `cf`. There might be a better way to do this, though. * add a test to check for the deprecation * consistently split between mspec and uspec which allows emitting a DeprecationWarning * correct the explanation of where the default format is coming from * use `reverse=True` instead of sorting with `-len(x)` Co-authored-by: Jules Chéron <43635101+jules-ch@users.noreply.github.com> * make sure the warning is raised correctly
Diffstat (limited to 'pint/formatting.py')
-rw-r--r--pint/formatting.py10
1 files changed, 8 insertions, 2 deletions
diff --git a/pint/formatting.py b/pint/formatting.py
index a04205d..5a458db 100644
--- a/pint/formatting.py
+++ b/pint/formatting.py
@@ -455,14 +455,20 @@ def siunitx_format_unit(units, registry):
def extract_custom_flags(spec):
import re
- flag_re = re.compile("(" + "|".join(list(_FORMATTERS.keys()) + ["~"]) + ")")
+ if not spec:
+ return ""
+
+ # sort by length, with longer items first
+ known_flags = sorted(_FORMATTERS.keys(), key=len, reverse=True)
+
+ flag_re = re.compile("(" + "|".join(known_flags + ["~"]) + ")")
custom_flags = flag_re.findall(spec)
return "".join(custom_flags)
def remove_custom_flags(spec):
- for flag in list(_FORMATTERS.keys()) + ["~"]:
+ for flag in sorted(_FORMATTERS.keys(), key=len, reverse=True) + ["~"]:
if flag:
spec = spec.replace(flag, "")
return spec