-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
58 lines (49 loc) · 1.59 KB
/
Copy pathutils.py
File metadata and controls
58 lines (49 loc) · 1.59 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
import json
import logging
# Setting up logging configuration
logging.basicConfig(level=logging.INFO)
class InvalidInputError(Exception):
pass
class ProcessingError(Exception):
pass
def safe_divide(a, b):
try:
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
raise InvalidInputError('Both inputs must be integers or floats.')
result = a / b
except ZeroDivisionError:
logging.error('Division by zero. Returning None.')
return None
except InvalidInputError as e:
logging.error(f'Invalid input: {e}')
return None
except Exception as e:
logging.critical(f'Unexpected error: {e}')
raise ProcessingError('An unexpected error occurred.')
else:
logging.info(f'Division successful: {result}')
return result
def read_json_file(file_path):
try:
with open(file_path, 'r') as file:
data = json.load(file)
except FileNotFoundError:
logging.error(f'File not found: {file_path}')
return None
except json.JSONDecodeError:
logging.error(f'Error decoding JSON from file: {file_path}')
return None
else:
logging.info(f'File read successfully: {file_path}')
return data
def process_data(data):
if not isinstance(data, list):
logging.error('Invalid data type, expected a list.')
return None
processed = []
for item in data:
if not isinstance(item, dict):
logging.warning(f'Skipped item: {item}')
continue
processed.append(item)
return processed