Sort the attrs list in-place by _fields and then alphabetically by name
(attrs, object)
| 324 | return results |
| 325 | |
| 326 | def sort_attributes(attrs, object): |
| 327 | 'Sort the attrs list in-place by _fields and then alphabetically by name' |
| 328 | # This allows data descriptors to be ordered according |
| 329 | # to a _fields attribute if present. |
| 330 | fields = getattr(object, '_fields', []) |
| 331 | try: |
| 332 | field_order = {name : i-len(fields) for (i, name) in enumerate(fields)} |
| 333 | except TypeError: |
| 334 | field_order = {} |
| 335 | keyfunc = lambda attr: (field_order.get(attr[0], 0), attr[0]) |
| 336 | attrs.sort(key=keyfunc) |
| 337 | |
| 338 | # ----------------------------------------------------- module manipulation |
| 339 |