File tree Expand file tree Collapse file tree 2 files changed +30
-7
lines changed Expand file tree Collapse file tree 2 files changed +30
-7
lines changed Original file line number Diff line number Diff line change 5
5
import mock
6
6
7
7
from heudiconv .utils import (
8
- get_known_heuristics_with_descriptions ,
8
+ create_tree ,
9
+ get_datetime ,
10
+ get_dicts_intersection ,
9
11
get_heuristic_description ,
10
- load_heuristic ,
12
+ get_known_heuristics_with_descriptions ,
11
13
json_dumps_pretty ,
14
+ JSONDecodeError ,
15
+ load_heuristic ,
12
16
load_json ,
13
- create_tree ,
17
+ remove_prefix ,
18
+ remove_suffix ,
14
19
save_json ,
15
20
update_json ,
16
- get_datetime ,
17
- remove_suffix ,
18
- remove_prefix ,
19
- JSONDecodeError )
21
+ )
20
22
21
23
import pytest
22
24
from .utils import HEURISTICS_PATH
@@ -170,3 +172,10 @@ def test_remove_prefix():
170
172
assert remove_prefix (s , '' ) == s
171
173
assert remove_prefix (s , 'foo' ) == s
172
174
assert remove_prefix (s , 'jason' ) == '.bourne'
175
+
176
+
177
+ def test_get_dicts_intersection ():
178
+ assert get_dicts_intersection ([]) == {}
179
+ assert get_dicts_intersection ([{"a" : 1 }]) == {"a" : 1 }
180
+ assert get_dicts_intersection ([{"a" : 1 }, {"b" : 2 }]) == {}
181
+ assert get_dicts_intersection ([{"a" : 1 }, {"a" : 1 , "b" : 2 }]) == {"a" : 1 }
Original file line number Diff line number Diff line change @@ -645,3 +645,17 @@ def remove_prefix(s, pre):
645
645
if pre and s .startswith (pre ):
646
646
return s [len (pre ):]
647
647
return s
648
+
649
+
650
+ def get_dicts_intersection (recs : list [dict ]) -> dict :
651
+ """Given a list of dictionaries, return a dict of key:values which are the same
652
+ across all entries
653
+ """
654
+ if not recs :
655
+ return {}
656
+ common_keys = recs [0 ].copy ()
657
+ for r in recs [1 :]:
658
+ for k , v in common_keys .copy ().items ():
659
+ if k not in r or r [k ] != v :
660
+ common_keys .pop (k )
661
+ return common_keys
You can’t perform that action at this time.
0 commit comments