Conditional Context Manager
The `conditional` library provides a `Conditional` context manager that executes code within a `with` block only when a specified boolean condition is true. It simplifies conditional execution of code sections, particularly useful when dealing with other context managers or resource allocation that should only happen under certain circumstances. As of version 2.0, it is actively maintained with a low release cadence.
Common errors
-
TypeError: 'int' object is not callable
cause In `conditional` v2.0+, the `condition` argument must be a boolean. You likely passed an integer (or other non-boolean type) expecting it to be treated as a callable or implicitly converted.fixExplicitly convert your condition to a boolean: `with Conditional(bool(0)):` or `with Conditional(my_condition == 5):`. -
TypeError: __init__ missing 1 required positional argument: 'condition'
cause You are attempting to instantiate `Conditional` without providing the required `condition` argument.fixAlways provide a boolean condition when instantiating `Conditional`: `with Conditional(True): ...` or `my_condition = False; with Conditional(my_condition): ...`. -
AttributeError: 'Conditional' object has no attribute '__enter__'
cause You are trying to use `Conditional` as a regular class instance or function call, rather than as a context manager with a `with` statement.fixEnsure you are using `Conditional` correctly within a `with` statement: `with Conditional(True): # your code here`.
Warnings
- breaking In `conditional` version 2.0 and later, the `condition` argument to `Conditional` MUST be a boolean. In versions prior to 2.0, it accepted a callable, which it would then execute to determine the condition. Passing a non-boolean (e.g., an integer, string, or callable) in v2.0+ will raise a `TypeError`.
- gotcha The `Conditional` context manager takes a *boolean condition*, not another context manager. It serves to conditionally enter its `with` block, where you would then place your actual context-managed resource or code. It does not make an existing context manager conditional itself.
- gotcha Be aware that if the `condition` evaluates to `False`, the code within the `with Conditional(False):` block will not execute at all. This means any side effects (e.g., file writes, database connections, object instantiations) within that block will not occur. Ensure that any necessary setup or cleanup logic is handled appropriately outside or around the conditional block.
Install
-
pip install conditional
Imports
- Conditional
from conditional import Conditional
Quickstart
from conditional import Conditional
# Example 1: Condition is True, block executes
print("Before true conditional block")
with Conditional(True):
print("Inside true conditional block")
with open("temp_file.txt", "w") as f:
f.write("This was written conditionally.\n")
print("After true conditional block")
# Example 2: Condition is False, block does not execute
print("\nBefore false conditional block")
with Conditional(False):
print("Inside false conditional block (should not see this)")
# The file will not be opened or written to
with open("another_temp_file.txt", "w") as f:
f.write("This was NOT written conditionally.\n")
print("After false conditional block")
# Cleanup (optional)
import os
if os.path.exists("temp_file.txt"):
os.remove("temp_file.txt")
if os.path.exists("another_temp_file.txt"):
os.remove("another_temp_file.txt")