Data conversion

pyaltt2.converters.merge_dict(*args, add_keys=True)

Safely merge two dictionaries

Parameters:
  • dct0...n – dicts to merge
  • add_keys – merge dict keys (default: True)
Returns:

merged dict

pyaltt2.converters.mq_topic_match(topic, mask)

Checks if topic matches mqtt-style mask

Parameters:
  • topic – topic (string)
  • mask – mask to check
Returns:

True if matches, False if don’t

pyaltt2.converters.parse_date(val=None, return_timestamp=True, ms=False)

Parse date from string or float/integer

Input date can be either timestamp or date-time string

If input value is integer and greater than 3000, it’s considered as a timestamp, otherwise - as a year

Parameters:
  • val – value to parse
  • return_timestamp – return UNIX timestamp (default) or datetime object
  • ms – parse date from milliseconds
Returns:

UNIX timestamp (float) or datetime object. If input value is None, returns current date/time

pyaltt2.converters.parse_number(val)

Tries to parse number from any value

Valid values are:

  • any float / integer
  • 123.45
  • 123 456.899
  • 123,456.899
  • 123 456,899
  • 123.456,82
Parameters:val – value to parse
Returns:val as-is if val is integer, float or None, otherwise parsed value
Raises:ValueError – if input val can not be parsed
pyaltt2.converters.safe_int(val)

Convert string/float to integer

If input value is integer - return as-is If input value is a hexadecimal (0x00): converts hex to decimal

Parameters:val – value to convert
Raises:ValueError – if input value can not be converted
pyaltt2.converters.val_to_boolean(val)

Convert any value to boolean

Boolean: return as-is

  • Integer: 1 = True, 0 = False
  • Strings (case-insensitive): ‘1’, ‘true’, ‘t’, ‘yes’, ‘on’, ‘y’ = True
  • ‘0’, ‘false’, ‘f’, ‘no’, ‘off’, ‘n’ = False
Parameters:val – value to convert
Returns:boolean converted value, None if val is None
Raises:ValueError – if value can not be converted