forked from ucloud/ucloud-sdk-python2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.py
More file actions
27 lines (23 loc) · 834 Bytes
/
encoder.py
File metadata and controls
27 lines (23 loc) · 834 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
# -*- coding: utf-8 -*-
def encode(d):
result = {}
for k, v in d.items():
if isinstance(v, dict):
for ek, ev in encode(v).items():
result["{}.{}".format(k, ek)] = encode_value(ev)
elif isinstance(v, list):
for i, item in enumerate(v):
if isinstance(item, dict):
for ek, ev in encode(item).items():
result["{}.{}.{}".format(k, i, ek)] = encode_value(ev)
else:
result["{}.{}".format(k, i)] = encode_value(item)
else:
result[k] = encode_value(v)
return result
def encode_value(v):
if isinstance(v, bool):
return "true" if v else "false"
if isinstance(v, float):
return str(int(v)) if v % 1 == 0 else str(v)
return str(v)