MCPcopy Create free account
hub / github.com/TencentARC/BrushNet / sort_objects

Function sort_objects

utils/custom_init_isort.py:142–173  ·  view source on GitHub ↗

Sort a list of objects following the rules of isort (all uppercased first, camel-cased second and lower-cased last). Args: objects (`List[Any]`): The list of objects to sort. key (`Callable[[Any], str]`, *optional*): A function taking an object a

(objects: List[Any], key: Optional[Callable[[Any], str]] = None)

Source from the content-addressed store, hash-verified

140
141
142def sort_objects(objects: List[Any], key: Optional[Callable[[Any], str]] = None) -> List[Any]:
143 """
144 Sort a list of objects following the rules of isort (all uppercased first, camel-cased second and lower-cased
145 last).
146
147 Args:
148 objects (`List[Any]`):
149 The list of objects to sort.
150 key (`Callable[[Any], str]`, *optional*):
151 A function taking an object as input and returning a string, used to sort them by alphabetical order.
152 If not provided, will default to noop (so a `key` must be provided if the `objects` are not of type string).
153
154 Returns:
155 `List[Any]`: The sorted list with the same elements as in the inputs
156 """
157
158 # If no key is provided, we use a noop.
159 def noop(x):
160 return x
161
162 if key is None:
163 key = noop
164 # Constants are all uppercase, they go first.
165 constants = [obj for obj in objects if key(obj).isupper()]
166 # Classes are not all uppercase but start with a capital, they go second.
167 classes = [obj for obj in objects if key(obj)[0].isupper() and not key(obj).isupper()]
168 # Functions begin with a lowercase, they go last.
169 functions = [obj for obj in objects if not key(obj)[0].isupper()]
170
171 # Then we sort each group.
172 key1 = ignore_underscore_and_lowercase(key)
173 return sorted(constants, key=key1) + sorted(classes, key=key1) + sorted(functions, key=key1)
174
175
176def sort_objects_in_import(import_statement: str) -> str:

Callers 2

_replaceFunction · 0.85
sort_objects_in_importFunction · 0.85

Calls 1

Tested by

no test coverage detected