py/objnamedtuple: Add _asdict function if OrderedDict is supported
This commit is contained in:
parent
cada971113
commit
79ed58f87b
|
@ -44,3 +44,4 @@
|
|||
#undef MICROPY_VFS_FAT
|
||||
#define MICROPY_VFS_FAT (1)
|
||||
#define MICROPY_PY_FRAMEBUF (1)
|
||||
#define MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT (1)
|
||||
|
|
|
@ -894,6 +894,11 @@ typedef double mp_float_t;
|
|||
#define MICROPY_PY_COLLECTIONS_ORDEREDDICT (0)
|
||||
#endif
|
||||
|
||||
// Whether to provide the _asdict function for namedtuple
|
||||
#ifndef MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT
|
||||
#define MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT (0)
|
||||
#endif
|
||||
|
||||
// Whether to provide "math" module
|
||||
#ifndef MICROPY_PY_MATH
|
||||
#define MICROPY_PY_MATH (1)
|
||||
|
|
|
@ -52,6 +52,23 @@ STATIC size_t namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qstr n
|
|||
return (size_t)-1;
|
||||
}
|
||||
|
||||
#if MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT
|
||||
STATIC mp_obj_t namedtuple_asdict(mp_obj_t self_in) {
|
||||
mp_obj_namedtuple_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
const qstr *fields = ((mp_obj_namedtuple_type_t*)self->tuple.base.type)->fields;
|
||||
mp_obj_t dict = mp_obj_new_dict(self->tuple.len);
|
||||
//make it an OrderedDict
|
||||
mp_obj_dict_t *dictObj = MP_OBJ_TO_PTR(dict);
|
||||
dictObj->base.type = &mp_type_ordereddict;
|
||||
dictObj->map.is_ordered = 1;
|
||||
for (size_t i = 0; i < self->tuple.len; ++i) {
|
||||
mp_obj_dict_store(dict, MP_OBJ_NEW_QSTR(fields[i]), self->tuple.items[i]);
|
||||
}
|
||||
return dict;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(namedtuple_asdict_obj, namedtuple_asdict);
|
||||
#endif
|
||||
|
||||
STATIC void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
|
||||
(void)kind;
|
||||
mp_obj_namedtuple_t *o = MP_OBJ_TO_PTR(o_in);
|
||||
|
@ -64,6 +81,13 @@ STATIC void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
|||
if (dest[0] == MP_OBJ_NULL) {
|
||||
// load attribute
|
||||
mp_obj_namedtuple_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
#if MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT
|
||||
if (attr == MP_QSTR__asdict) {
|
||||
dest[0] = MP_OBJ_FROM_PTR(&namedtuple_asdict_obj);
|
||||
dest[1] = self_in;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
size_t id = namedtuple_find_field((mp_obj_namedtuple_type_t*)self->tuple.base.type, attr);
|
||||
if (id == (size_t)-1) {
|
||||
return;
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
try:
|
||||
try:
|
||||
from collections import namedtuple
|
||||
except ImportError:
|
||||
from ucollections import namedtuple
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
t = namedtuple("Tup", ["baz", "foo", "bar"])(3, 2, 5)
|
||||
|
||||
try:
|
||||
t._asdict
|
||||
except AttributeError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
d = t._asdict()
|
||||
print(list(d.keys()))
|
||||
print(list(d.values()))
|
Loading…
Reference in New Issue