Last active
May 27, 2020 09:33
-
-
Save cftang0827/d561bbef8e01af71546c70c02cb2f2fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import re | |
def to_snake(input_str): | |
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', input_str) | |
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower() | |
def to_camel(input_str): | |
return re.sub(r'_([a-z])', lambda x: x.group(1).upper(), input_str) | |
def transorm_request(req, t_func): | |
output = req | |
if isinstance(req, dict): | |
output = {} | |
for k, v in req.items(): | |
output[t_func(k)] = transorm_request(v, t_func) | |
elif isinstance(req, list): | |
output = [] | |
for item in req: | |
output.append(transorm_request(item, t_func)) | |
elif isinstance(req, str): | |
output = t_func(req) | |
return output | |
t = { | |
"tShirt": [ | |
{ | |
"aBook": 2 | |
}, | |
{ | |
"bBook": "cBook" | |
} | |
], | |
"noteBook": { | |
"yooHoo": 2, | |
"barrrRaaa": "helloWorld" | |
}, | |
"faceBook": 5 | |
} | |
y = transorm_request(t, to_snake) | |
print(y) | |
e = transorm_request(y, to_camel) | |
print(e) | |
print(e == t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment