|
12 | 12 |
|
13 | 13 |
|
14 | 14 | # various object type check functions
|
15 |
| -__all__ += ["is_bool", "is_dict", "is_int", "is_int_range", "is_list", "is_neg_int", "is_percentage", "is_pos_int", |
16 |
| - "is_prime"] |
| 15 | +__all__ += ["is_bool", "is_dict", "is_float", "is_int", "is_int_range", "is_list", "is_neg_float", "is_neg_int", |
| 16 | + "is_percentage", "is_pos_float", "is_pos_int", "is_prime"] |
17 | 17 | is_bool = lambda b: isinstance(b, bool)
|
18 | 18 | is_dict = lambda d: isinstance(d, dict)
|
| 19 | +is_float = lambda f: isinstance(f, float) |
19 | 20 | is_int = lambda i: isinstance(i, int)
|
20 | 21 | is_int_range = lambda i, i1, i2=None: all(is_int(x) for x in [i, i1, i2 or 0]) and i in (range(i1+1) if i2 is None \
|
21 | 22 | else range(i1, i2+1))
|
22 | 23 | is_list = lambda l: isinstance(l, (list, set, tuple))
|
| 24 | +is_neg_float = lambda f, zero=False: is_float(f) and (f <= 0. if zero else f < 0.) |
23 | 25 | is_neg_int = lambda i, zero=False: is_int(i) and (i <= 0 if zero else i < 0)
|
24 | 26 | is_percentage = lambda f: isinstance(f, (int, float)) and 0. <= float(f) <= 1.
|
| 27 | +is_pos_float = lambda f, zero=False: is_float(f) and (f >= 0. if zero else f > 0.) |
25 | 28 | is_pos_int = lambda i, zero=True: is_int(i) and (i >= 0 if zero else i > 0)
|
26 | 29 | is_prime = lambda i: __prime_number(i)
|
27 | 30 |
|
|
43 | 46 |
|
44 | 47 |
|
45 | 48 | # -------------------- DATA FORMAT ARGUMENT TYPES --------------------
|
46 |
| -__all__ += ["int_range", "neg_int", "negative_int", "pos_int", "positive_int", "ints", "ints_range", "neg_ints", |
47 |
| - "negative_ints", "pos_ints", "positive_ints", "prime_number", "values_list"] |
48 |
| - |
49 |
| - |
50 |
| -def __ints(l, check_func=lambda x: False, idescr=None, shouldbe=None, **kwargs): |
51 |
| - """ Parses a comma-separated list of ints. """ |
52 |
| - l = _str2list(l) |
53 |
| - msg = "{} {}integer{}".format(["Bad list of", "Not a"][len(l) == 1], "" if idescr is None else idescr + " ", |
54 |
| - ["s", ""][len(l) == 1]) |
55 |
| - if shouldbe is not None: |
56 |
| - msg += " (should be %s)" % shouldbe |
57 |
| - if not all(check_func(_, **kwargs) for _ in l): |
58 |
| - raise ValueError(msg) |
59 |
| - return l |
60 |
| -ints = lambda l: __ints(l, is_int) |
61 |
| -int_range = lambda i, i1, i2=None: __ints(i, is_int_range, "valid", "in range [%d,%d]" % \ |
62 |
| - (0 if i2 is None else i1, i1 if i2 is None else i2), i1=i1, i2=i2)[0] |
63 |
| -negative_int = neg_int = lambda i, zero=False: __ints(i, is_neg_int, "negative", zero=zero)[0] |
64 |
| -positive_int = pos_int = lambda i, zero=True: __ints(i, is_pos_int, "positive", zero=zero)[0] |
65 |
| -ints_range = lambda l, i1, i2=None: __ints(l, is_int_range, "valid", "in range [%d,%d]" % \ |
66 |
| - (0 if i2 is None else i1, i1 if i2 is None else i2), i1=i1, i2=i2) |
67 |
| -negative_ints = neg_ints = lambda l, zero=False: __ints(l, is_neg_int, "negative", zero=zero) |
68 |
| -positive_ints = pos_ints = lambda l, zero=True: __ints(l, is_pos_int, "positive", zero=zero) |
| 49 | +__all__ += ["floats", "int_range", "ints", "ints_range", "neg_float", "neg_floats", "negative_float", "negative_floats", |
| 50 | + "neg_int", "neg_ints", "negative_int", "negative_ints", "pos_float", "pos_floats", "positive_float", |
| 51 | + "positive_floats", "pos_int", "positive_int", "pos_ints", "positive_ints", "prime_number", "values_list"] |
| 52 | + |
| 53 | + |
| 54 | +def __n(ntype): |
| 55 | + def _wrapper(l, check_func=lambda x: False, idescr=None, shouldbe=None, **kwargs): |
| 56 | + """ Parses a comma-separated list of ints. """ |
| 57 | + l = _str2list(l) |
| 58 | + if not all(check_func(x, **kwargs) for x in l): |
| 59 | + msg = f"{['Bad list of', 'Not a'][len(l) == 1]} {'' if idescr is None else idescr + ' '}{ntype}" \ |
| 60 | + f"{['s', ''][len(l) == 1]}" |
| 61 | + if shouldbe is not None: |
| 62 | + msg += f" (should be {shouldbe})" |
| 63 | + raise ValueError(msg) |
| 64 | + return l |
| 65 | + return _wrapper |
| 66 | + |
| 67 | +floats = lambda l: __n("float")(l, is_float) |
| 68 | +negative_float = neg_float = lambda i, zero=False: __n("float")(i, is_neg_float, "negative", zero=zero)[0] |
| 69 | +positive_float = pos_float = lambda i, zero=True: __n("float")(i, is_pos_float, "positive", zero=zero)[0] |
| 70 | +negative_floats = neg_floats = lambda l, zero=False: __n("float")(l, is_neg_float, "negative", zero=zero) |
| 71 | +positive_floats = pos_floats = lambda l, zero=True: __n("float")(l, is_pos_float, "positive", zero=zero) |
| 72 | +floats.__name__ = "floats" |
| 73 | +negative_float.__name__ = neg_float.__name__ = "negative float" |
| 74 | +negative_floats.__name__ = neg_floats.__name__ = "negative floats list" |
| 75 | +positive_float.__name__ = pos_float.__name__ = "positive float" |
| 76 | +positive_floats.__name__ = pos_floats.__name__ = "positive floats list" |
| 77 | + |
| 78 | +ints = lambda l: __n("integer")(l, is_int) |
| 79 | +int_range = lambda i, i1, i2=None: __n("integer")(i, is_int_range, "valid", "in range [%d,%d]" % \ |
| 80 | + (0 if i2 is None else i1, i1 if i2 is None else i2), i1=i1, i2=i2)[0] |
| 81 | +negative_int = neg_int = lambda i, zero=False: __n("integer")(i, is_neg_int, "negative", zero=zero)[0] |
| 82 | +positive_int = pos_int = lambda i, zero=True: __n("integer")(i, is_pos_int, "positive", zero=zero)[0] |
| 83 | +ints_range = lambda l, i1, i2=None: __n("integer")(l, is_int_range, "valid", "in range [%d,%d]" % \ |
| 84 | + (0 if i2 is None else i1, i1 if i2 is None else i2), i1=i1, i2=i2) |
| 85 | +negative_ints = neg_ints = lambda l, zero=False: __n("integer")(l, is_neg_int, "negative", zero=zero) |
| 86 | +positive_ints = pos_ints = lambda l, zero=True: __n("integer")(l, is_pos_int, "positive", zero=zero) |
69 | 87 | ints.__name__ = "integers"
|
70 | 88 | int_range.__name__ = "integer (from range)"
|
71 | 89 | ints_range.__name__ = "integers list (from range)"
|
|
0 commit comments