~dricottone/filters

ref: f62c909a49e4a579a0b74b9b6632c25e127b3668 filters/filter/internals.py -rw-r--r-- 2.9 KiB
f62c909aDominic Ricottone Version 1.0.1; Cleaned up documentation 4 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3

import sys
from typing import *

VERSION = (1,0,1,)

def _try_get_float(
    mapping: Dict,
    key: str,
    *,
    default: Optional[float] = None,
) -> Optional[float]:
    if key in mapping:
        return float(mapping[key])
    else:
        return default

def _try_get_list_float(
    mapping: Dict,
    key: str,
    *,
    default: Optional[List[float]] = None,
) -> Optional[List[float]]:
    if key in mapping:
        _list = list()
        for value in mapping[key]:
            try:
                _list.append(float(value))
            except:
                _print_invalid_data(value)
        return _list
    else:
        return default

def _read_stdin() -> Iterator[float]:
    try:
        for line in sys.stdin.readlines():
            line = line.strip()
            if len(line) == 0:
                continue
            try:
                yield float(line.strip())
            except:
                _print_invalid_data(line)
    except KeyboardInterrupt:
        sys.stdout.write("\n")

def _read_file(filename) -> Iterator[float]:
    try:
        with open(filename, 'r') as f:
            for line in f.readlines():
                line = line.strip()
                if len(line) == 0:
                    continue
                try:
                    yield float(line)
                except:
                    _print_invalid_data(line)
    except OSError:
        _print_invalid_file(filename)

def _get_raw_data(filenames: List[str]) -> List[float]:
    raw_data: List[float] = list()

    if len(filenames) == 0:
        raw_data.extend(list(_read_stdin()))
    else:
        for filename in filenames:
            if filename == '-':
                raw_data.extend(list(_read_stdin()))
            else:
                raw_data.extend(list(_read_file(filename)))

    return raw_data

def _print_help() -> None:
    _msg = "Usage: filter METHOD [OPTIONS] DATA\n"
    sys.stdout.write(_msg)

def _print_version() -> None:
    _msg = "filter {0}\n".format(".".join(str(v) for v in VERSION))
    sys.stdout.write(_msg)

def _print_methodologies(*method: str) -> None:
    _msg = "Valid methodologies: {0}\n".format(", ".join(method))
    sys.stdout.write(_msg)

def _print_usage() -> None:
    _msg = (
        "Usage: filter METHOD [OPTIONS] DATA",
        "Try `filter --list-methodologies` and `filter METHOD --help`",
    )
    sys.stderr.write("\n".join(_msg) + "\n")

def _print_invalid_methodology(method: str) -> None:
    _msg = (
        "{0}: Invalid methodology '{1}'\n".format(sys.argv[0], method),
        "Try `filter --list-methodologies`",
    )
    sys.stderr.write("\n".join(_msg) + "\n")

def _print_invalid_file(filename: str) -> None:
    _msg = "{0}: Invalid file '{1}'\n".format(sys.argv[0], filename)
    sys.stderr.write(_msg)

def _print_invalid_data(line: str) -> None:
    _msg = "{0}: Cannot convert '{1}' into numeric value\n".format(
        sys.argv[0], line,
    )
    sys.stderr.write(_msg)