Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Metagenomics
metagenedb
Commits
fe154842
Commit
fe154842
authored
Jul 16, 2019
by
Kenzo-Hugo Hillion
♻
Browse files
add operation to extract subdict from dict
parent
170bd11b
Changes
2
Hide whitespace changes
Inline
Side-by-side
backend/metagenedb/utils/dict_operations.py
0 → 100644
View file @
fe154842
import
logging
logging
.
basicConfig
(
level
=
logging
.
INFO
)
_LOGGER
=
logging
.
getLogger
(
__name__
)
def
extract_dict
(
source_dict
,
keys
):
"""
Extract a dict from a given dict based on a given set of keys
"""
extracted_dict
=
{}
for
key
in
keys
:
try
:
extracted_dict
[
key
]
=
source_dict
[
key
]
del
source_dict
[
key
]
except
KeyError
:
_LOGGER
.
warning
(
f
"[
{
key
}
] is not found in the source dict, extraction skipped for this key."
)
return
extracted_dict
backend/metagenedb/utils/test_dict_operations.py
0 → 100644
View file @
fe154842
from
unittest
import
TestCase
from
metagenedb.utils.dict_operations
import
extract_dict
class
TestExtractDict
(
TestCase
):
def
test_extract_dict
(
self
):
source_dict
=
{
'a'
:
1
,
'b'
:
2
}
extract_keys
=
[
'b'
]
expected_source_dict
=
{
'a'
:
1
}
expected_extract_dict
=
{
'b'
:
2
}
test_extract_dict
=
extract_dict
(
source_dict
,
extract_keys
)
self
.
assertDictEqual
(
source_dict
,
expected_source_dict
)
self
.
assertDictEqual
(
test_extract_dict
,
expected_extract_dict
)
def
test_extract_dict_wrong_key
(
self
):
source_dict
=
{
'a'
:
1
,
'b'
:
2
}
extract_keys
=
[
'c'
]
expected_source_dict
=
{
'a'
:
1
,
'b'
:
2
}
expected_extract_dict
=
{}
test_extract_dict
=
extract_dict
(
source_dict
,
extract_keys
)
self
.
assertDictEqual
(
source_dict
,
expected_source_dict
)
self
.
assertDictEqual
(
test_extract_dict
,
expected_extract_dict
)
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment