forked from hydrobasher/entelect-hackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriteFile.py
More file actions
31 lines (25 loc) · 1001 Bytes
/
writeFile.py
File metadata and controls
31 lines (25 loc) · 1001 Bytes
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
import json
from classes import *
from classes2 import *
from readFile import read
import json
def write(filename, out_level_obj):
def serialize(obj):
# 1. If the object has a manual to_dict method, use it
if hasattr(obj, "to_dict"):
return {k: serialize(v) for k, v in obj.to_dict().items()}
# 2. Handle lists (like your 'laps' or 'segments' lists)
if isinstance(obj, list):
return [serialize(i) for i in obj]
# 3. Handle basic types (int, float, str, bool, None)
if not hasattr(obj, "__dict__"):
return obj
# 4. Fallback for objects without a to_dict (standard __dict__ unpacking)
data = {}
for key, value in obj.__dict__.items():
if key == 'level': continue
data[key] = serialize(value)
return data
output_dict = serialize(out_level_obj)
with open(filename, 'w') as f:
json.dump(output_dict, f, indent=4)