From ff15a1483b3b823bb35866436e084ba07a408377 Mon Sep 17 00:00:00 2001 From: keewis Date: Wed, 23 Feb 2022 23:06:21 +0100 Subject: properly deprecate the current behavior of the `default_format` option (#1419) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- pint/formatting.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'pint/formatting.py') 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 -- cgit v1.2.1