Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
be019ce0
Commit
be019ce0
authored
Apr 11, 2014
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
objdict: Implement construction from iterable of pairs.
Pairs are limited to tuples so far.
parent
12a04392
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
py/objdict.c
+13
-3
13 additions, 3 deletions
py/objdict.c
tests/basics/dict-from-iter.py
+4
-0
4 additions, 0 deletions
tests/basics/dict-from-iter.py
with
17 additions
and
3 deletions
py/objdict.c
+
13
−
3
View file @
be019ce0
...
@@ -7,6 +7,7 @@
...
@@ -7,6 +7,7 @@
#include
"mpconfig.h"
#include
"mpconfig.h"
#include
"qstr.h"
#include
"qstr.h"
#include
"obj.h"
#include
"obj.h"
#include
"objtuple.h"
#include
"runtime0.h"
#include
"runtime0.h"
#include
"runtime.h"
#include
"runtime.h"
...
@@ -39,13 +40,22 @@ STATIC mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp
...
@@ -39,13 +40,22 @@ STATIC mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp
dict
=
mp_obj_new_dict
(
0
);
dict
=
mp_obj_new_dict
(
0
);
break
;
break
;
case
1
:
case
1
:
{
if
(
MP_OBJ_IS_TYPE
(
args
[
0
],
&
mp_type_dict
))
{
if
(
MP_OBJ_IS_TYPE
(
args
[
0
],
&
mp_type_dict
))
{
return
dict_copy
(
args
[
0
]);
return
dict_copy
(
args
[
0
]);
}
}
// TODO create dict from an arbitrary mapping!
// TODO create dict from an arbitrary mapping!
// TODO create dict from an iterable!
assert
(
false
);
// Make dict from iterable of pairs
mp_obj_t
iterable
=
mp_getiter
(
args
[
0
]);
mp_obj_t
dict
=
mp_obj_new_dict
(
0
);
// TODO: support arbitrary seq as a pair
mp_obj_tuple_t
*
item
;
while
((
item
=
mp_iternext
(
iterable
))
!=
MP_OBJ_NULL
)
{
mp_obj_dict_store
(
dict
,
item
->
items
[
0
],
item
->
items
[
1
]);
}
return
dict
;
}
default:
default:
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
"dict takes at most 1 argument"
));
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
"dict takes at most 1 argument"
));
...
...
This diff is collapsed.
Click to expand it.
tests/basics/dict-from-iter.py
0 → 100644
+
4
−
0
View file @
be019ce0
print
(
dict
([(
1
,
"
foo
"
)]))
d
=
dict
([(
"
foo
"
,
"
foo2
"
),
(
"
bar
"
,
"
baz
"
)])
print
(
sorted
(
d
.
keys
()))
print
(
sorted
(
d
.
values
()))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment