Sort the attrs list in-place by _fields and then alphabetically by name
(attrs, object)
| 298 | return results |
| 299 | |
| 300 | def sort_attributes(attrs, object): |
| 301 | 'Sort the attrs list in-place by _fields and then alphabetically by name' |
| 302 | # This allows data descriptors to be ordered according |
| 303 | # to a _fields attribute if present. |
| 304 | fields = getattr(object, '_fields', []) |
| 305 | try: |
| 306 | field_order = {name : i-len(fields) for (i, name) in enumerate(fields)} |
| 307 | except TypeError: |
| 308 | field_order = {} |
| 309 | keyfunc = lambda attr: (field_order.get(attr[0], 0), attr[0]) |
| 310 | attrs.sort(key=keyfunc) |
| 311 | |
| 312 | # ----------------------------------------------------- module manipulation |
| 313 |