Skip to content

Commit

Permalink
Adding a dict-merging utility
Browse files Browse the repository at this point in the history
  • Loading branch information
eliben committed Jan 15, 2018
1 parent b153ef6 commit 18c99a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 8 additions & 0 deletions elftools/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
from ..construct import ConstructError


def merge_dicts(*dicts):
"Given any number of dicts, merges them into a new one."""
result = {}
for d in dicts:
result.update(d)
return result


def bytelist2string(bytelist):
""" Convert a list of byte values (e.g. [0x10 0x20 0x00]) to a bytes object
(e.g. b'\x10\x20\x00').
Expand Down
12 changes: 11 additions & 1 deletion test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from random import randint

from elftools.common.py3compat import int2byte, BytesIO
from elftools.common.utils import (parse_cstring_from_stream,
from elftools.common.utils import (parse_cstring_from_stream, merge_dicts,
preserve_stream_pos)


Expand Down Expand Up @@ -54,5 +54,15 @@ def test_basic(self):
self.assertEqual(sio.tell(), 5)


class Test_merge_dicts(unittest.TestCase):
def test_basic(self):
md = merge_dicts({10: 20, 20: 30}, {30: 40, 50: 60})
self.assertEqual(md, {10: 20, 20: 30, 30: 40, 50: 60})

def test_keys_resolve(self):
md = merge_dicts({10: 20, 20: 30}, {20: 40, 50: 60})
self.assertEqual(md, {10: 20, 20: 40, 50: 60})


if __name__ == '__main__':
unittest.main()

0 comments on commit 18c99a9

Please sign in to comment.