8000 Adding 'keys' and 'values' methods to dict object by kellrott · Pull Request #213 · go-python/gpython · GitHub
[go: up one dir, main page]

Skip to content

Adding 'keys' and 'values' methods to dict object #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Adding 'keys' and 'values' methods to dict object
  • Loading branch information
kellrott committed Jan 5, 2023
commit 562d3176b5a34b5cdd46ce6e06b6c5d161e33e82
26 changes: 26 additions & 0 deletions py/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ func init() {
return NewIterator(o), nil
}, 0, "items() -> list of D's (key, value) pairs, as 2-tuples")

StringDictType.Dict["keys"] = MustNewMethod("keys", func(self Object, args Tuple) (Object, error) {
err := UnpackTuple(args, nil, "keys", 0, 0)
if err != nil {
return nil, err
}
sMap := self.(StringDict)
o := make([]Object, 0, len(sMap))
for k := range sMap {
o = append(o, String(k))
}
return NewIterator(o), nil
}, 0, "keys() -> list of D's keys, as a list")

StringDictType.Dict["values"] = MustNewMethod("values", func(self Object, args Tuple) (Object, error) {
err := UnpackTuple(args, nil, "values", 0, 0)
if err != nil {
return nil, err
}
sMap := self.(StringDict)
o := make([]Object, 0, len(sMap))
for _, v := range sMap {
o = append(o, v)
}
return NewIterator(o), nil
}, 0, "values() -> list of D's values, as a list")

StringDictType.Dict["get"] = MustNewMethod("get", func(self Object, args Tuple) (Object, error) {
var length = len(args)
switch {
Expand Down
8 changes: 8 additions & 0 deletions py/tests/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
assert a.get('b',1) == 1
assert a.get('b',True) == True

doc="check keys"
a = {"a":1}
assert a.keys() == ["a"]

doc="check values"
a = {"a":1}
assert a.values() == [1]

doc="check items"
a = {"a":"b","c":5.5}
for k, v in a.items():
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
0