The original poster is here; here is my approach so far refined:
I can add (or monkey-patch) the bool __nonzero__ operator to objects with objects, returning whether they are included. Then, when the objects are mutually exclusive, I can have:
with res1 or res2 or res3 or res4: ...
When a resource is togglable, I can create an empty one that is nop; wither seems like a nice name for him:
class sither: @classmethod def __enter__(cls): pass @classmethod def __exit__(cls,*args): pass ... with res1 or wither, res2 or wither: ...
I can also use this while retaining the ability to switch from suitable objects:
with res1 if res1enabled else wither, res2 if res2enabled else wither: ..
Finally, those whom I control most, I can integrate the included check into the class itself, so when they are used and not included, they nop:
with res1, res2, res3: ...
The with operator is absolutely adored; it is still a bit fragmented. It will be interesting to see what refinement others have in this regard ...
source share