decorative_secrets.defaults
ApplyConditionalDefaultsOptions
dataclass
This class contains options governing the behavior of the apply_conditional_defaults decorator.
Attributes:
-
filter_parameter_defaults
(tuple[typing.Any, ...] | dict[str, tuple[typing.Any, ...]]
) –This may either be:
- A tuple of values which should be filtered out of keyword arguments when both the passed keyword argument value and the parameter default value in the function signature are one of these values.
- A dictionary mapping parameter names to tuples of values applying the filtering logic described above on a per-parameter basis.
Defaults to
()
—indicating no filtering should occur.
Examples:
from decorative_secrets.defaults import (
apply_conditional_defaults,
ApplyConditionalDefaultsOptions,
)
@apply_conditional_defaults(
lambda environment: environment == "prod",
ApplyConditionalDefaultsOptions(filter_parameter_defaults=(None,)),
source_directory="/in/prod",
target_directory="/out/prod",
)
@apply_conditional_defaults(
lambda environment: environment == "dev",
ApplyConditionalDefaultsOptions(filter_parameter_defaults=(None,)),
source_directory="/in/dev",
target_directory="/out/dev",
)
@apply_conditional_defaults(
lambda environment: environment == "stage",
ApplyConditionalDefaultsOptions(filter_parameter_defaults=(None,)),
source_directory="/in/stage",
target_directory="/out/stage",
)
def get_environment_source_target(
environment: str = "dev",
source_directory: str | None = None,
target_directory: str | None = None,
) -> tuple[str, str | None, str | None]:
return (environment, source_directory, target_directory)
get_environment_source_target("stage", None, None)
# ('stage', '/in/stage', '/out/stage')
get_environment_source_target(environment="prod", None, None)
# ('prod', '/in/prod', '/out/prod')
get_environment_source_target(None, None, None)
# ('dev', '/in/dev', '/out/dev')
Source code in src/decorative_secrets/defaults.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
apply_conditional_defaults
apply_conditional_defaults(
condition: collections.abc.Callable[..., bool],
*default_args: typing.Any,
**default_kwargs: typing.Any
) -> collections.abc.Callable[
..., collections.abc.Callable[..., typing.Any]
]
This function decorates another function in order to apply a set of
default keyword or positional/keyword argument values dependent on the
outcome of passing an applicable subset of those arguments to the
condition
function.
Parameters:
-
condition
(collections.abc.Callable[..., bool]
) –A function which accepts a subset of the decorated function's arguments and returns a boolean value indicating whether or not to apply the default argument values.
-
*default_args
(typing.Any
, default:()
) –A set of positional argument values to apply as defaults if the condition is met. Note: If any of these arguments are an instance of ApplyConditionalDefaultsOptions , the options will be used to determine decorator behavior, and will be redacted from the arguments passed to the decorated function.
-
**default_kwargs
(typing.Any
, default:{}
) –A set of keyword argument values to apply as defaults if the condition is met.
Examples:
from decorative_secrets.defaults import apply_conditional_defaults
@apply_conditional_defaults(
lambda environment: environment == "prod",
source_directory="/in/prod",
target_directory="/out/prod",
)
@apply_conditional_defaults(
lambda environment: environment == "dev",
source_directory="/in/dev",
target_directory="/out/dev",
)
@apply_conditional_defaults(
lambda environment: environment == "stage",
source_directory="/in/stage",
target_directory="/out/stage",
)
def get_environment_source_target(
environment: str = "dev",
source_directory: str = "/dev/null",
target_directory: str = "/dev/null",
) -> tuple[str, str, str]:
return (environment, source_directory, target_directory)
get_environment_source_target("stage")
# ('stage', '/in/stage', '/out/stage')
get_environment_source_target(environment="prod")
# ('prod', '/in/prod', '/out/prod')
get_environment_source_target()
# ('dev', '/in/dev', '/out/dev')
Source code in src/decorative_secrets/defaults.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|