8000 Add keys_to_upper function · localstack/localstack@2992903 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2992903

Browse files
committed
Add keys_to_upper function
1 parent 65310fc commit 2992903

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

localstack/utils/objects.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Any, Callable, Dict, Generic, List, Optional, Set, Type, TypeVar, Union
55

66
from .collections import ensure_list
7-
from .strings import first_char_to_lower
7+
from .strings import first_char_to_lower, first_char_to_upper
88

99
ComplexType = Union[List, Dict, object]
1010

@@ -155,8 +155,10 @@ def recurse_object(obj: ComplexType, func: Callable, path: str = "") -> ComplexT
155155
return obj
156156

157157

158-
def keys_to_lower(obj: ComplexType, skip_children_of: List[str] = None) -> ComplexType:
159-
"""Recursively changes all dict keys to first character lowercase. Skip children
158+
def keys_to(
159+
obj: ComplexType, op: Callable[[str], str], skip_children_of: List[str] = None
160+
) -> ComplexType:
161+
"""Recursively changes all dict keys to apply op. Skip children
160162
of any elements whose names are contained in skip_children_of (e.g., ['Tags'])"""
161163
skip_children_of = ensure_list(skip_children_of or [])
162164

@@ -166,13 +168,21 @@ def fix_keys(o, path="", **kwargs):
166168
if isinstance(o, dict):
167169
for k, v in dict(o).items():
168170
o.pop(k)
169-
o[first_char_to_lower(k)] = v
171+
o[op(k)] = v
170172
return o
171173

172174
result = recurse_object(obj, fix_keys)
173175
return result
174176

175177

178+
def keys_to_lower(obj: ComplexType, skip_children_of: List[str] = None) -> ComplexType:
179+
return keys_to(obj, first_char_to_lower, skip_children_of)
180+
181+
182+
def keys_to_upper(obj: ComplexType, skip_children_of: List[str] = None) -> ComplexType:
183+
return keys_to(obj, first_char_to_upper, skip_children_of)
184+
185+
176186
def singleton_factory(factory: Callable[[], _T]) -> Callable[[], _T]:
177187
"""
178188
Decorator for methods that create a particular value once and then return the same value in a thread safe way.

0 commit comments

Comments
 (0)
0