MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / sort_objects

Function sort_objects

diffusers/utils/custom_init_isort.py:143–174  ·  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

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