Module change_pswd_func.check_similarity

View Source
 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
#!/usr/bin/env python

from Levenshtein import distance  # type: ignore

from .constants import SIMILARITY_THRESHOLD

def similar(string1: str, string2: str) -> bool:

    '''Check two strings similarity

    Calculate distance between two strings and compare distance with threshold

    [Change password requirement]

    3. password is not similar to old password < 80% match.

    Returns:

        bool: True if two strings are similar otherwise False

    '''

    if type(string1) is not str or type(string2) is not str:

        raise TypeError('Args need to be str type.')

    print('\n[string1]: {}'.format(string1))  # '\n' is for pytest output

    print('[string2]: {}'.format(string2))

    normalized_distance = distance(string1, string2) / max(len(string1), len(string2))

    return 1 - normalized_distance > SIMILARITY_THRESHOLD

Variables

SIMILARITY_THRESHOLD

Functions

similar

def similar(
    string1: str,
    string2: str
) -> bool

Check two strings similarity

Calculate distance between two strings and compare distance with threshold

[Change password requirement] 3. password is not similar to old password < 80% match.

Returns: bool: True if two strings are similar otherwise False

View Source
def similar(string1: str, string2: str) -> bool:

    '''Check two strings similarity

    Calculate distance between two strings and compare distance with threshold

    [Change password requirement]

    3. password is not similar to old password < 80% match.

    Returns:

        bool: True if two strings are similar otherwise False

    '''

    if type(string1) is not str or type(string2) is not str:

        raise TypeError('Args need to be str type.')

    print('\n[string1]: {}'.format(string1))  # '\n' is for pytest output

    print('[string2]: {}'.format(string2))

    normalized_distance = distance(string1, string2) / max(len(string1), len(string2))

    return 1 - normalized_distance > SIMILARITY_THRESHOLD