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)
| 141 | |
| 142 | |
| 143 | def 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 | |
| 177 | def sort_objects_in_import(import_statement: str) -> str: |
no test coverage detected