~mapperr/trl

ref: f0199cf618af7c4bcc3737e98948d2f7ceeb02e6 trl/trullo/shortener.py -rw-r--r-- 2.0 KiB
f0199cf6 — mapperr Add labels to board view 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from typing import List
from typing import Set

import attr

from trullo.shortcuttable import Shortcuttable


@attr.s(auto_attribs=True)
class Shortener:
    @staticmethod
    def normalize(string: str) -> str:
        result = ''
        for char in string:
            if char.lower() not in 'qwertyuiopasdfghjklzxcvbnm1234567890':
                continue
            result += char.lower()
        return result

    @staticmethod
    def get_min_symbols_to_uniq(strings: List[str]) -> int:
        symbol_counter = 1
        longest_item_length = Shortener.get_longest_item_length(strings)
        string_list = [
            string_item[0:symbol_counter].lower()
            for string_item in strings
        ]
        while symbol_counter < longest_item_length \
                and Shortener.are_there_duplicates(string_list):
            symbol_counter += 1
            string_list = [
                string_item[0:symbol_counter].lower()
                for string_item in strings
            ]
        return symbol_counter

    @staticmethod
    def are_there_duplicates(list_: List[str]):
        set_: Set[str] = set()
        for elem in list_:
            if elem in set_:
                return True
            else:
                set_.add(elem)
        return False

    @staticmethod
    def get_longest_item_length(list_: List[str]) -> int:
        longest_length = 0
        for string in list_:
            length = len(string)
            if length > longest_length:
                longest_length = length
        return longest_length

    @staticmethod
    def get_matches(
            shortcut: str,
            shortcuttables: List[Shortcuttable]) -> List[any]:
        return [shortcuttable for shortcuttable in shortcuttables
                if Shortener.is_a_match(
                shortcut, shortcuttable.get_normalized_name())
                ]

    @staticmethod
    def is_a_match(shortcut: str, normalized_name: str) -> bool:
        return shortcut in normalized_name