File: util.py

package info (click to toggle)
python-openapi-core 0.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,104 kB
  • sloc: python: 19,979; makefile: 44
file content (26 lines) | stat: -rw-r--r-- 686 bytes parent folder | download
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
"""OpenAPI core util module"""

from itertools import chain
from typing import Any
from typing import Iterable

BOOLEAN_TRUE_VALUES = ("y", "yes", "t", "true", "on", "1")
BOOLEAN_FALSE_VALUES = ("n", "no", "f", "false", "off", "0")


def forcebool(val: Any) -> bool:
    if isinstance(val, str):
        val = val.lower()
        if val in BOOLEAN_TRUE_VALUES:
            return True
        elif val in BOOLEAN_FALSE_VALUES:
            return False
        else:
            raise ValueError(f"invalid truth value {val!r}")

    return bool(val)


def chainiters(*lists: Iterable[Any]) -> Iterable[Any]:
    iters = map(lambda l: l and iter(l) or [], lists)
    return chain(*iters)