+
+class DecodeError(Exception):
+ def __init__(self, e, **kwargs):
+ super().__init__(e)
+ for k, v in kwargs.items():
+ setattr(self, k, v)
+
+def intx(x):
+ if isinstance(x, str):
+ x = int(x, 0)
+ return x
+
+
+def hhmm(x):
+ """Check for the string that represents hours and minutes"""
+ if not isinstance(x, str) or len(x) != 4:
+ raise ValueError(str(x) + " is not a four-character string")
+ hh = int(x[:2])
+ mm = int(x[2:])
+ if hh < 0 or hh > 23 or mm < 0 or mm > 59:
+ raise ValueError(str(x) + " does not contain valid hours and minutes")
+ return x
+
+
+def l3str(x):
+ if isinstance(x, str):
+ x = x.split(",")
+ if len(x) != 3 or not all(isinstance(el, str) for el in x):
+ raise ValueError(str(x) + " is not a list of three strings")
+ return x
+
+
+def l3int(x):
+ if isinstance(x, str):
+ x = x.split(",")
+ x = [int(el) for el in x]
+ if len(x) != 3 or not all(isinstance(el, int) for el in x):
+ raise ValueError(str(x) + " is not a list of three integers")
+ return x
+
+
+class MetaPkt(type):
+ """
+ For each class corresponding to a message, automatically create
+ two nested classes `In` and `Out` that also inherit from their
+ "nest". Class attribute `IN_KWARGS` defined in the "nest" is
+ copied to the `In` nested class under the name `KWARGS`, and
+ likewise, `OUT_KWARGS` of the nest class is copied as `KWARGS`
+ to the nested class `Out`. In addition, method `encode` is
+ defined in both classes equal to `in_encode()` and `out_encode()`
+ respectively.
+ """
+
+ def __new__(cls, name, bases, attrs):
+ newcls = super().__new__(cls, name, bases, attrs)
+ newcls.In = super().__new__(
+ cls,
+ name + ".In",
+ (newcls,) + bases,
+ {
+ "KWARGS": newcls.IN_KWARGS,
+ "decode": newcls.in_decode,
+ "encode": newcls.in_encode,
+ },
+ )
+ newcls.Out = super().__new__(
+ cls,
+ name + ".Out",
+ (newcls,) + bases,
+ {
+ "KWARGS": newcls.OUT_KWARGS,
+ "decode": newcls.out_decode,
+ "encode": newcls.out_encode,
+ },
+ )
+ return newcls