py: Add attrtuple object, for space-efficient tuples with attr access.

If you need the functionality of a namedtuple but will only make 1 or a
few instances, then use an attrtuple instead.
This commit is contained in:
Damien George
2015-04-21 14:14:24 +00:00
parent 23a2b11abf
commit 5aa311d330
7 changed files with 120 additions and 9 deletions

View File

@@ -40,4 +40,19 @@ mp_obj_t mp_obj_tuple_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs);
mp_obj_t mp_obj_tuple_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value);
mp_obj_t mp_obj_tuple_getiter(mp_obj_t o_in);
extern const mp_obj_type_t mp_type_attrtuple;
#define MP_DEFINE_ATTRTUPLE(tuple_obj_name, fields, nitems, ...) \
const mp_obj_tuple_t tuple_obj_name = { \
.base = {&mp_type_attrtuple}, \
.len = nitems, \
.items = { __VA_ARGS__ , (void*)fields } \
}
#if MICROPY_PY_COLLECTIONS
void mp_obj_attrtuple_print_helper(const mp_print_t *print, const qstr *fields, mp_obj_tuple_t *o);
#endif
mp_obj_t mp_obj_new_attrtuple(const qstr *fields, mp_uint_t n, const mp_obj_t *items);
#endif // __MICROPY_INCLUDED_PY_OBJTUPLE_H__