From d99f2dfbc31ecec7b4959dcc3b36d098325bc4b8 Mon Sep 17 00:00:00 2001 From: Alexander Zaft Date: Fri, 14 Apr 2023 10:11:32 +0200 Subject: [PATCH] Add __format__ to EnumMember Make the format specifier 'd' able to be used when formatting them in f-strings as an alternative to an int()-cast. Change-Id: I4083aa0f4b0d8d10e3e11a29591cfbf5e5aca03d Reviewed-on: https://forge.frm2.tum.de/review/c/secop/frappy/+/30902 Tested-by: Jenkins Automated Tests Reviewed-by: Markus Zolliker Reviewed-by: Alexander Zaft --- frappy/lib/enum.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/frappy/lib/enum.py b/frappy/lib/enum.py index 4489b9e..7dd65e4 100644 --- a/frappy/lib/enum.py +++ b/frappy/lib/enum.py @@ -211,6 +211,11 @@ class EnumMember: def __index__(self): return self.value.__index__() + def __format__(self, format_spec): + if format_spec.endswith('d'): + return format(self.value, format_spec) + return super().__format__(format_spec) + # note: we do not implement the __i*__ methods as they modify our value # inplace and we want to have a const def __forbidden__(self, *args):