(val, typ)
| 15 | |
| 16 | |
| 17 | def cast(val, typ): |
| 18 | log.debug((val, typ)) |
| 19 | if " or " in typ: |
| 20 | for t in typ.split(" or "): |
| 21 | try: |
| 22 | return cast(val, t) |
| 23 | except TqdmTypeError: |
| 24 | pass |
| 25 | raise TqdmTypeError(f"{val} : {typ}") |
| 26 | |
| 27 | # sys.stderr.write('\ndebug | `val:type`: `' + val + ':' + typ + '`.\n') |
| 28 | if typ == 'bool': |
| 29 | if (val == 'True') or (val == ''): |
| 30 | return True |
| 31 | if val == 'False': |
| 32 | return False |
| 33 | raise TqdmTypeError(val + ' : ' + typ) |
| 34 | if typ == 'chr': |
| 35 | if len(val) == 1: |
| 36 | return val.encode() |
| 37 | if re.match(r"^\\\w+$", val): |
| 38 | return eval(f'"{val}"').encode() |
| 39 | raise TqdmTypeError(f"{val} : {typ}") |
| 40 | if typ == 'str': |
| 41 | return val |
| 42 | if typ == 'int': |
| 43 | try: |
| 44 | return int(val) |
| 45 | except ValueError as exc: |
| 46 | raise TqdmTypeError(f"{val} : {typ}") from exc |
| 47 | if typ == 'float': |
| 48 | try: |
| 49 | return float(val) |
| 50 | except ValueError as exc: |
| 51 | raise TqdmTypeError(f"{val} : {typ}") from exc |
| 52 | raise TqdmTypeError(f"{val} : {typ}") |
| 53 | |
| 54 | |
| 55 | def posix_pipe(fin, fout, delim=b'\\n', buf_size=256, |
no test coverage detected