MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / cast

Function cast

pager_lib/tqdm/cli.py:17–52  ·  view source on GitHub ↗
(val, typ)

Source from the content-addressed store, hash-verified

15
16
17def 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
55def posix_pipe(fin, fout, delim=b'\\n', buf_size=256,

Callers 2

capFunction · 0.85
mainFunction · 0.85

Calls 3

TqdmTypeErrorClass · 0.85
debugMethod · 0.80
encodeMethod · 0.45

Tested by

no test coverage detected