Sort the attrs list in-place by _fields and then alphabetically by name
(attrs, object)
| 363 | return results |
| 364 | |
| 365 | def sort_attributes(attrs, object): |
| 366 | 'Sort the attrs list in-place by _fields and then alphabetically by name' |
| 367 | # This allows data descriptors to be ordered according |
| 368 | # to a _fields attribute if present. |
| 369 | fields = getattr(object, '_fields', []) |
| 370 | try: |
| 371 | field_order = {name : i-len(fields) for (i, name) in enumerate(fields)} |
| 372 | except TypeError: |
| 373 | field_order = {} |
| 374 | keyfunc = lambda attr: (field_order.get(attr[0], 0), attr[0]) |
| 375 | attrs.sort(key=keyfunc) |
| 376 | |
| 377 | # ----------------------------------------------------- module manipulation |
| 378 |