dirutility.compare   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 33.33 %

Importance

Changes 0
Metric Value
eloc 31
dl 16
loc 48
rs 10
c 0
b 0
f 0
wmc 7

4 Functions

Rating   Name   Duplication   Size   Complexity  
A unique_venn() 0 3 1
A unique() 0 8 1
A main() 16 16 4
A compare_trees() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
from tqdm import tqdm
2
3
from dirutility import DirPaths
4
5
6
def unique(list1, list2):
7
    """
8
    Get unique items in list1 that are not in list2
9
    :return: Unique items only in list 1
10
    """
11
    set2 = set(list2)
12
    list1_unique = [x for x in tqdm(list1, desc='Unique', total=len(list1)) if x not in set2]
13
    return list1_unique
14
15
16
def unique_venn(list1, list2):
17
    """Get unique items that are only in list1 and only in list2"""
18
    return unique(list1, list2), unique(list2, list1)
19
20
21
def compare_trees(dir1, dir2):
22
    """Parse two directories and return lists of unique files"""
23
    paths1 = DirPaths(dir1).walk()
24
    paths2 = DirPaths(dir2).walk()
25
    return unique_venn(paths1, paths2)
26
27
28 View Code Duplication
def main():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
29
    from dirutility.gui import CompareTreesGUI
30
    params = CompareTreesGUI().sources
31
    src = params['source']
32
    dir1_u, dir2_u = compare_trees(src['dir1'], src['dir2'])
33
34
    if params['save']:
35
        from databasetools import CSVExport, DictTools
36
        save = params['save']
37
        if save['csv']:
38
            CSVExport(list(dir1_u), cols=['files'], file_path=save['directory'], file_name='dir1_unique')
39
            CSVExport(list(dir2_u), cols=['files'], file_path=save['directory'], file_name='dir2_unique')
40
        if save['json']:
41
            DictTools(save['directory'], 'dir1_unique').save(list(dir1_u))
42
            DictTools(save['directory'], 'dir2_unique').save(list(dir2_u))
43
    print('Done!')
44
45
46
if __name__ == "__main__":
47
    main()
48