-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8df97d6
commit edf06b7
Showing
2 changed files
with
16 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,26 @@ | ||
from typing import Dict, List, Union | ||
from typing import Dict, List, Type, Union | ||
|
||
INT = Type[int] | ||
FLOAT = Type[float] | ||
|
||
def cast_all(o: Union[List, Dict], from_type, to_type) -> Union[List, Dict]: | ||
|
||
def cast_all( | ||
o: Union[List, Dict], from_type: Union[INT, FLOAT], to_type: Union[FLOAT, INT] | ||
) -> Union[List, Dict]: | ||
if isinstance(o, list): | ||
for i, v in enumerate(o): | ||
if type(v) == from_type: | ||
o[i] = to_type(v) | ||
v2 = to_type(v) | ||
if v == v2: | ||
o[i] = v2 | ||
elif isinstance(v, (list, dict)): | ||
cast_all(v, from_type, to_type) | ||
elif isinstance(o, dict): | ||
for k, v in o.items(): | ||
if type(v) == from_type: | ||
o[k] = to_type(v) | ||
v2 = to_type(v) | ||
if v == v2: | ||
o[k] = v2 | ||
elif isinstance(v, (list, dict)): | ||
cast_all(v, from_type, to_type) | ||
return o |
This file contains 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