-
Notifications
You must be signed in to change notification settings - Fork 1
/
recursive_bool.py
41 lines (34 loc) · 950 Bytes
/
recursive_bool.py
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
from collections.abc import Sequence, Sized
from typing import Any
def _rbool(x: Any, *, _S: list[Any] | None = None) -> bool | None:
"""
_rbool(...) -> bool | None
True -> satisfies the conditions of truthiness
False -> a falsy object
None -> an empty object
"""
if not isinstance(x, Sequence):
return bool(x) or (isinstance(x, Sized) and None)
if _S is None:
_S = []
if x in _S:
return True
_S.append(x)
res = True
for o in x:
if _rbool(o, _S=_S) is not None:
break
else:
res = None
_S.pop()
return res
def rbool(x: Any) -> bool:
"""
rbool(x: Any) -> bool
True -> a non-false object
False -> an object that satisfies the conditions of falsiness:
1. falsy non-sized object
2. empty sized object
3. a sequence full of objects satisfying either 2. or 3.
"""
return _rbool(x) or False