numpy

Форк
0
/
find_deprecated_escaped_characters.py 
62 строки · 1.9 Кб
1
#!/usr/bin/env python3
2
r"""
3
Look for escape sequences deprecated in Python 3.6.
4

5
Python 3.6 deprecates a number of non-escape sequences starting with '\' that
6
were accepted before. For instance, '\(' was previously accepted but must now
7
be written as '\\(' or r'\('.
8

9
"""
10

11

12
def main(root):
13
    """Find deprecated escape sequences.
14

15
    Checks for deprecated escape sequences in ``*.py files``. If `root` is a
16
    file, that file is checked, if `root` is a directory all ``*.py`` files
17
    found in a recursive descent are checked.
18

19
    If a deprecated escape sequence is found, the file and line where found is
20
    printed. Note that for multiline strings the line where the string ends is
21
    printed and the error(s) are somewhere in the body of the string.
22

23
    Parameters
24
    ----------
25
    root : str
26
        File or directory to check.
27
    Returns
28
    -------
29
    None
30

31
    """
32
    import ast
33
    import tokenize
34
    import warnings
35
    from pathlib import Path
36

37
    count = 0
38
    base = Path(root)
39
    paths = base.rglob("*.py") if base.is_dir() else [base]
40
    for path in paths:
41
        # use tokenize to auto-detect encoding on systems where no
42
        # default encoding is defined (e.g. LANG='C')
43
        with tokenize.open(str(path)) as f:
44
            with warnings.catch_warnings(record=True) as w:
45
                warnings.simplefilter('always')
46
                tree = ast.parse(f.read())
47
            if w:
48
                print("file: ", str(path))
49
                for e in w:
50
                    print('line: ', e.lineno, ': ', e.message)
51
                print()
52
                count += len(w)
53
    print("Errors Found", count)
54

55

56
if __name__ == "__main__":
57
    from argparse import ArgumentParser
58

59
    parser = ArgumentParser(description="Find deprecated escaped characters")
60
    parser.add_argument('root', help='directory or file to be checked')
61
    args = parser.parse_args()
62
    main(args.root)
63

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.