-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
152 lines (124 loc) · 4.15 KB
/
helpers.py
File metadata and controls
152 lines (124 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
source : https://github.com/sixfab/pico_lte_micropython-sdk/blob/master/pico_lte/utils/helpers.py
Module for storing helper functions
"""
import json
from pico_lte.common import config
from pico_lte.utils.status import Status
def read_json_file(file_path):
"""
Function for reading json file
"""
try:
with open(file_path, "r") as file:
data = json.load(file)
except:
return None
else:
return data
def write_json_file(file_path, data):
"""
Function for writing json file
"""
try:
with open(file_path, "w") as file:
json.dump(data, file)
except:
return None
else:
return data
def deep_copy_of_dictionary(dict_instance):
"""Create a deepcopy of the dictionary given.
Parameters
----------
dict_instance : dict
It is the dictionary to be copied.
"""
if isinstance(dict_instance, dict):
dictionary_to_return = {}
for key, value in dict_instance.items():
dictionary_to_return[key] = value
return dictionary_to_return
else:
return None
def get_desired_data(result, prefix, separator=",", data_index=0):
"""Function for getting actual data from response"""
result_to_return = deep_copy_of_dictionary(result)
valuable_lines = None
if result.get("status") != Status.SUCCESS:
result["value"] = None
return result
response = result_to_return.get("response")
for index, value in enumerate(response):
if value == "OK" and index > 0:
valuable_lines = [response[i] for i in range(0, index)]
if valuable_lines:
for line in valuable_lines:
prefix_index = line.find(prefix)
if prefix_index != -1:
index = prefix_index + len(prefix) # Find index of meaningful data
data_array = line[index:].split(separator)
if isinstance(data_index, list): # If desired multiple data
data_index = data_index[: len(data_array)] # Truncate data_index
result_to_return["value"] = [
simplify(data_array[i]) for i in data_index
] # Return list
elif isinstance(data_index, int):
# If data_index is out of range, return first element
data_index = data_index if data_index < len(data_array) else 0
result_to_return["value"] = simplify(
data_array[data_index]
) # Return single data
elif data_index == "all":
result_to_return["value"] = [simplify(data) for data in data_array]
else:
# If data_index is unknown type, return first element
data_index = 0
result_to_return["value"] = simplify(
data_array[data_index]
) # Return single data
return result_to_return
# if no valuable data found
result_to_return["value"] = None
return result_to_return
def simplify(text):
"""Function for simplifying strings"""
if isinstance(text, str):
return text.replace('"', "").replace("'", "")
return text
def read_file(file_path, file_type="t"):
"""
Function for reading file
"""
try:
with open(file_path, "r" + file_type) as file:
data = file.read()
except:
return None
else:
return data
def write_file(file_path, data, file_type="t"):
"""
Function for writing file
"""
try:
with open(file_path, "w" + file_type) as file:
file.write(data)
except:
return None
else:
return data
def get_parameter(path, default=None):
"""
Function for getting parameters for SDK methods from global config dictionary.
"""
desired = config.get("params", None)
if isinstance(desired, dict):
for element in path:
if desired:
desired = desired.get(element, None)
if desired:
return desired
if default:
return default
return None