Code Duplication    Length = 60-61 lines in 2 locations

hansel/operations.py 2 locations

@@ 118-178 (lines=61) @@
115
    return sorted(list(intersect))
116
117
118
def difference(crumb1: 'hansel.Crumb', crumb2: 'hansel.Crumb', on: Iterator[str] = None) -> List[str]:
119
    """Return the difference `crumb1` - `crumb2`, i.e., will return a list of
120
    Crumbs that are in `crumb1` but not in `crumb2`.
121
122
    If `on` is None, will use all the common arguments names of both crumbs.
123
    Otherwise will use only the elements of `on`. All its items must be in
124
    both crumbs.
125
126
    Returns
127
    -------
128
    arg_names: list
129
        The matching items.
130
131
    Parameters
132
    ----------
133
    crumb1: hansel.Crumb
134
135
    crumb2: hansel.Crumb
136
137
    on: str or list of str
138
        Crumb argument names common to both input crumbs.
139
140
    Raises
141
    ------
142
    ValueError:
143
        If an element of `on` does not exists in either `list1` or `list2`.
144
145
    KeyError:
146
        If the result is empty.
147
148
    Returns
149
    -------
150
    inner_join: list[hansel.Crumb]
151
152
    Notes
153
    -----
154
    Use with care, ideally the argument matches should be in the same order in
155
    both crumbs.
156
157
    Both crumbs must have at least one matching identifier argument and one
158
    of those must be the one in `id_colname`.
159
    """
160
    if isinstance(on, str):
161
        on = [on]
162
163
    arg_names = list(_get_matching_items(list(crumb1.all_args()),
164
                                         list(crumb2.all_args()),
165
                                         items=on))
166
167
    if not arg_names:
168
        raise KeyError("Could not find matching arguments between "
169
                       "{} and  {} limited by {}.".format(list(crumb1.all_args()),
170
                                                          list(crumb2.all_args()),
171
                                                          on))
172
173
    maps1 = joint_value_map(crumb1, arg_names, check_exists=True)
174
    maps2 = joint_value_map(crumb2, arg_names, check_exists=True)
175
176
    diff = set(maps1).difference(set(maps2))
177
178
    return sorted(list(diff))
179
180
181
def valuesmap_to_dict(values_map: CrumbArgsMap) -> Dict[str, List[str]]:
@@ 56-115 (lines=60) @@
53
    return sorted(values_map_checked)
54
55
56
def intersection(crumb1: hansel.Crumb, crumb2: hansel.Crumb, on: Iterator[str]=None) -> List[str]:
57
    """Return an 'inner join' of both given Crumbs, i.e., will return a list of
58
    Crumbs with common values for the common arguments of both crumbs.
59
60
    If `on` is None, will use all the common arguments names of both crumbs.
61
    Otherwise will use only the elements of `on`. All its items must be in
62
    both crumbs.
63
64
    Returns
65
    -------
66
    arg_names: list
67
        The matching items.
68
69
    Parameters
70
    ----------
71
    crumb1: hansel.Crumb
72
73
    crumb2: hansel.Crumb
74
75
    on: str or list of str
76
        Crumb argument names common to both input crumbs.
77
78
    Raises
79
    ------
80
    ValueError:
81
        If an element of `on` does not exists in either `list1` or `list2`.
82
83
    KeyError:
84
        If the result is empty.
85
86
    Returns
87
    -------
88
    inner_join: list[hansel.Crumb]
89
90
    Notes
91
    -----
92
    Use with care, ideally the argument matches should be in the same order in
93
    both crumbs.
94
95
    Both crumbs must have at least one matching identifier argument and one
96
    of those must be the one in `on`.
97
    """
98
    if isinstance(on, str):
99
        on = [on]
100
101
    arg_names = list(_get_matching_items(list(crumb1.all_args()), list(crumb2.all_args()), items=on))
102
103
    if not arg_names:
104
        raise KeyError("Could not find matching arguments between {} and  {} limited by {}.".format(
105
            list(crumb1.all_args()),
106
            list(crumb2.all_args()),
107
            on)
108
        )
109
110
    maps1 = joint_value_map(crumb1, arg_names, check_exists=True)
111
    maps2 = joint_value_map(crumb2, arg_names, check_exists=True)
112
113
    intersect = set(maps1) & (set(maps2))
114
115
    return sorted(list(intersect))
116
117
118
def difference(crumb1: 'hansel.Crumb', crumb2: 'hansel.Crumb', on: Iterator[str] = None) -> List[str]: