Coverage for src\tempstick_py\_helpers.py: 87%

19 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-09-11 22:38 -0700

1from datetime import datetime 

2import re 

3 

4 

5def format_mac(mac: str) -> str: 

6 """Return canonical MAC address 

7 

8 Taken exactly from https://stackoverflow.com/a/29446103/19251950 

9 

10 :param mac: Common MAC address; may include common delimiters and mixed-case 

11 :type mac: str 

12 :return: Canonical MAC address of form '00:80:41:ae:fd:7e' 

13 :rtype: str 

14 """ 

15 mac = re.sub( 

16 "[.:-]", "", mac 

17 ).lower() # remove delimiters and convert to lower case 

18 mac = "".join(mac.split()) # remove whitespaces 

19 assert len(mac) == 12 # length should be now exactly 12 (eg. 008041aefd7e) 

20 assert mac.isalnum() # should only contain letters and numbers 

21 mac = ":".join(["%s" % (mac[i : i + 2]) for i in range(0, 12, 2)]) 

22 return mac 

23 

24 

25# 2022-09-04 07:00:00Z 

26def format_datetime(dt: str) -> datetime: 

27 obj = datetime.strptime(dt, "%Y-%m-%d %H:%M:%SZ") 

28 return obj 

29 

30 

31def set_attr_from_dict(cls, values: dict): 

32 for key, val in values.items(): 

33 setattr(cls, key, val) 

34 

35 

36def debug_print(var_name, var_value, cls): 

37 name = cls.__class__.__name__ 

38 value = "{parent} | {name}: {value}".format( 

39 parent=name, name=var_name, value=var_value 

40 ) 

41 print(value)