osmocom.utils¶
various pyosmocom utilities
osmocom: various utilities
- class osmocom.utils.JsonEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)¶
Extend the standard library JSONEncoder with support for more types.
Constructor for JSONEncoder, with sensible defaults.
If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.
If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.
If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an RecursionError). Otherwise, no such check takes place.
If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.
If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.
If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.
If specified, separators should be an (item_separator, key_separator) tuple. The default is (’, ‘, ‘: ‘) if indent is
None
and (‘,’, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘,’, ‘:’) to eliminate whitespace.If specified, default is a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a
TypeError
.- default(o)¶
Implement this method in a subclass such that it returns a serializable object for
o
, or calls the base implementation (to raise aTypeError
).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
- osmocom.utils.all_subclasses(cls) set ¶
Recursively get all subclasses of a specified class
- osmocom.utils.auto_int(x)¶
Helper function for argparse to accept hexadecimal integers.
- osmocom.utils.b2h(b: bytearray) Hexstr ¶
convert from a sequence of bytes to a string of hex nibbles
- osmocom.utils.h2b(s: Hexstr) bytearray ¶
convert from a string of hex nibbles to a sequence of bytes
- osmocom.utils.h2i(s: Hexstr) List[int] ¶
convert from a string of hex nibbles to a list of integers
- osmocom.utils.h2s(s: Hexstr) str ¶
convert from a string of hex nibbles to an ASCII string
- osmocom.utils.i2h(s: List[int]) Hexstr ¶
convert from a list of integers to a string of hex nibbles
- osmocom.utils.i2s(s: List[int]) str ¶
convert from a list of integers to an ASCII string
- osmocom.utils.is_decimal(instr: str) str ¶
Method that can be used as ‘type’ in argparse.add_argument() to validate the value consists of an even sequence of decimal digits only.
- osmocom.utils.is_hex(string: str, minlen: int = 2, maxlen: int | None = None) bool ¶
Check if a string is a valid hexstring
- osmocom.utils.is_hexstr(instr: str) str ¶
Method that can be used as ‘type’ in argparse.add_argument() to validate the value consists of an even sequence of hexadecimal digits only.
- osmocom.utils.is_hexstr_or_decimal(instr: str) str ¶
Method that can be used as ‘type’ in argparse.add_argument() to validate the value consists of [hexa]decimal digits only.
- osmocom.utils.lpad(s: str, l: int, c='f') str ¶
pad string on the left side. :param s: string to pad :param l: total length to pad to :param c: padding character
- Returns:
String ‘s’ padded with as many ‘c’ as needed to reach total length of ‘l’
- osmocom.utils.rpad(s: str, l: int, c='f') str ¶
pad string on the right side. :param s: string to pad :param l: total length to pad to :param c: padding character
- Returns:
String ‘s’ padded with as many ‘c’ as needed to reach total length of ‘l’
- osmocom.utils.s2h(s: str) Hexstr ¶
convert from an ASCII string to a string of hex nibbles
- osmocom.utils.str_sanitize(s: str) str ¶
replace all non printable chars, line breaks and whitespaces, with ‘ ‘, make sure that there are no whitespaces at the end and at the beginning of the string.
- Parameters:
s – string to sanitize
- Returns:
filtered result of string ‘s’
- osmocom.utils.swap_nibbles(s: Hexstr) Hexstr ¶
swap the nibbles in a hex string