From e19d139e92992e447eab408903a9c49a81ad9b06 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Wed, 7 Feb 2024 12:04:10 +0100 Subject: [PATCH] leasefilter: use f-strings instead of format() --- PLC/LeaseFilter.py | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/PLC/LeaseFilter.py b/PLC/LeaseFilter.py index 888b18e..4ed6c01 100644 --- a/PLC/LeaseFilter.py +++ b/PLC/LeaseFilter.py @@ -66,10 +66,9 @@ class LeaseFilter(Filter): def sql_time_intersect(f1, u1, f2, u2): # either f2 is in [f1,u1], or u2 is in [f1,u1], or f2<=f1<=u1<=u2 return ( - "(({f1} <= {f2}) AND ({f2} <= {u1})) " - "OR (({f1} <= {u2}) AND ({u2} <= {u1})) " - "OR (({f2}<={f1}) AND ({u1}<={u2}))" - .format(f1=f1, u1=u1, f2=f2, u2=u2)) + f"(({f1} <= {f2}) AND ({f2} <= {u1})) " + f"OR (({f1} <= {u2}) AND ({u2} <= {u1})) " + f"OR (({f2}<={f1}) AND ({u1}<={u2}))") @staticmethod def time_in_range(timestamp, f1, u1): @@ -79,14 +78,12 @@ class LeaseFilter(Filter): @staticmethod def sql_time_in_range(timestamp, f1, u1): # is timestamp in [f1, u1] - return ( - "(({f1} <= {timestamp}) AND ({timestamp} <= {u1}))" - .format(timestamp=timestamp, f1=f1, u1=u1)) + return f"(({f1} <= {timestamp}) AND ({timestamp} <= {u1}))" @staticmethod def sql_timeslot_after(f1, u1, mark): # is the lease alive after mark, i.e. u1 >= mark - return "({u1} >= {mark})".format(u1=u1, mark=mark) + return f"({u1} >= {mark})" # hooks for the local fields def sql_alive(self, alive): @@ -100,8 +97,7 @@ class LeaseFilter(Filter): u = LeaseFilter.quote(u) return LeaseFilter.sql_time_intersect(f, u, 't_from', 't_until') else: - raise PLCInvalidArgument("LeaseFilter: alive field {}" - .format(alive)) + raise PLCInvalidArgument(f"LeaseFilter: alive field {alive}") def sql_clip(self, clip): if isinstance(clip, int) or isinstance(clip, str): @@ -113,8 +109,7 @@ class LeaseFilter(Filter): u = LeaseFilter.quote(u) return LeaseFilter.sql_time_intersect(f, u, 't_from', 't_until') else: - raise PLCInvalidArgument("LeaseFilter: clip field {}" - .format(clip)) + raise PLCInvalidArgument(f"LeaseFilter: clip field {clip}") # the whole key to implementing day is to compute today's beginning def today_start(self): @@ -157,14 +152,14 @@ class LeaseFilter(Filter): try: # locate hook function associated with key method = LeaseFilter.__dict__['sql_' + k] - where_part += " {} {}({})"\ - .format(self.join_with, - self.negation[k], - method(self, self.local[k])) + where_part += ( + f" {self.join_with} {self.negation[k]}" + f"({method(self, self.local[k])})" + ) except Exception as e: raise PLCInvalidArgument( - "LeaseFilter: something wrong with filter" - "key {}, val was {} -- {}".format(k, v, e)) + f"LeaseFilter: something wrong with filter " + f"key {k}, value was {v} -- {e}") return (where_part, clip_part) # xxx not sure where this belongs yet -- 2.43.0