Registry / devops / conditional

conditional

JSON →
library2.0pypypiunverified

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.

pip install conditional
INSTALL
IMPORT
SIG · CONDITIONAL
C
conditional
devopspythonv2.0
Install
1.5s avg
Import
—
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.10–3.910 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Conditional
✓ from conditional import Conditional
✗ from conditional import Conditional

This example demonstrates how to use `Conditional` to wrap a block of code, including other context managers, based on a boolean condition.

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")
Debug
Known issues
breakingIn `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`.
fix
Ensure the `condition` argument is explicitly a `bool`. If you previously passed a callable, evaluate it *before* passing the result to `Conditional`: `with Conditional(my_callable()):` instead of `with Conditional(my_callable):`.
affects: 2.0+
gotchaThe `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.
fix
Use it as `with Conditional(my_condition): with MyActualContextManager(): ...` if you want to conditionally enter `MyActualContextManager`. For simple conditional execution without another context manager, you might also consider a standard `if my_condition: ...` block.
affects: all
gotchaBe 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.
fix
If certain operations must always occur regardless of the condition, place them outside the `Conditional` block. For complex conditional logic, a standard `if/else` statement might offer more clarity and control over divergent paths.
affects: all
Upgrade
Version history
2.0latest on PyPI · released May 14, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
35 hits · last 30 days
node
32
OpenAI (training)
1
Resources
conditional — pip install conditional · libregistry