Coverage for src/scrilla/util/helper.py: 98%

37 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-18 18:14 +0000

1from math import trunc, log10, floor 

2from typing import Dict, List, Tuple 

3 

4from scrilla.static import constants 

5 

6 

7def truncate(number: float, digits: int) -> float: 

8 stepper = 10.0 ** digits 

9 return trunc(stepper * number) / stepper 

10 

11 

12def strip_string_array(array: List[str]) -> List[str]: 

13 new_array = [] 

14 for string in array: 

15 new_array.append(string.strip()) 

16 return new_array 

17 

18 

19def split_and_strip(string: str, upper: bool = True, delimiter=",") -> List[str]: 

20 if upper: 

21 return strip_string_array(string.upper().split(delimiter)) 

22 return strip_string_array(string.lower().split(delimiter)) 

23 

24 

25def reorder_dict(this_dict: Dict[str, str], desired_order: List[str]): 

26 ordered_keys = [ 

27 order for order in desired_order if order in list(this_dict.keys())] 

28 return {order: this_dict[order] for order in ordered_keys} 

29 

30 

31def round_array(array: List[float], decimals: int) -> List[float]: 

32 return [round(element, decimals) for element in array] 

33 

34 

35def intersect_dict_keys(dict1: dict, dict2: dict) -> Tuple[dict, dict]: 

36 """ 

37 Generates two new dictionaries from the inputted dictionaries such that the new dictionaries contain the same keys. In other words, this function takes the intersection of the dictionary keys and generates two new dictionaries with *only* those keys. 

38 """ 

39 return {x: dict1[x] for x in dict1.keys() if x in dict2.keys()}, {x: dict2[x] for x in dict2.keys() if x in dict1.keys()} 

40 

41 

42def complement_dict_keys(dict1: dict, dict2: dict) -> dict: 

43 """ 

44 Returns the transformed dictionaries whose keys are a complement relative to the second dictionary keys. In other words, this function takes the complement of the first dictionary keys relative to the second dictionary keys and visa versa, 

45 """ 

46 return {x: dict1[x] for x in dict1.keys() if x not in dict2.keys()}, {x: dict2[x] for x in dict2.keys() if x not in dict1.keys()} 

47 

48 

49def exceeds_accuracy(decimal: float) -> bool: 

50 if decimal == 0: 

51 return True 

52 decimal = abs(decimal) 

53 if (0 < decimal < 10 ** (-constants.constants['ACCURACY'])): 

54 return True 

55 return False 

56 

57 

58def significant_digits(number: float, digits: int) -> float: 

59 return number if number == 0 else round(number, -int(floor(log10(abs(number)))) + (digits - 1)) 

60 

61 

62def get_first_json_key(this_json: dict) -> str: 

63 return list(this_json.keys())[0] 

64 

65 

66def replace_troublesome_chars(msg: str) -> str: 

67 return msg.replace('\u2265', '').replace('\u0142', '')