Safety
SafetyDecorators
dataclass
¶
Dataclass that contains all safety decorators.
Attributes:
| Name | Type | Description |
|---|---|---|
is_changing |
Decorator to check if a property's value is changing. |
|
is_negative |
Decorator to check if a property's value is negative. |
|
is_positive |
Decorator to check if a property's value is positive. |
|
is_zero |
Decorator to check if a property's value is zero. |
|
is_within_range |
Decorator to check if a property's value is within a given range. |
|
is_greater_than |
Decorator to check if a property's value is greater than a given value. |
|
is_less_than |
Decorator to check if a property's value is less than a given value. |
|
custom_criteria |
Decorator to check if a property's value meets a custom criteria. |
Source code in opensourceleg/extras/safety/safety.py
SafetyManager
¶
The SafetyManager class enables the addition of safety decorators to an object's properties, specifically to their getters. When the 'start' method is invoked, these decorators are applied to the properties of the objects stored in the 'safe_objects' dictionary. The original objects are then replaced with subclasses that incorporate the decorated properties. Invoking the 'update' method accesses the properties of the objects in the 'safe_objects' dictionary, thereby triggering the decorators.
Source code in opensourceleg/extras/safety/safety.py
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
SafetyContext
¶
Context manager for enabling/disabling safety checks using 'with' context.
Source code in opensourceleg/extras/safety/safety.py
add_safety(instance, attribute, decorator)
¶
Adds a safety decorator to the given object's attribute. The decorator will be applied to the property's getter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
object
|
Object that contains the attribute. |
required |
attribute
|
str
|
Name of the attribute. |
required |
decorator
|
Callable
|
Safety decorator to be applied to the attribute. |
required |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the attribute does not exist in the given object. |
Warning
|
If the attribute is not a property. The SafetyManager only works on properties. |
Source code in opensourceleg/extras/safety/safety.py
start()
¶
Applies all decorators to the properties of the objects in the safe_objects dictionary.
Example:
safety_manager = SafetyManager() safety_manager.add_safety(sensor, "value", SafetyDecorators.is_changing("value")) safety_manager.add_safety(sensor, "a", SafetyDecorators.is_positive()) safety_manager.start()
Source code in opensourceleg/extras/safety/safety.py
stop()
¶
Restores the original classes of the objects, thereby removing the applied safety decorators.
Source code in opensourceleg/extras/safety/safety.py
update()
¶
Accesses the properties of the objects in the safe_objects dictionary, thereby triggering the decorators.
Example: TODO: Add example
Source code in opensourceleg/extras/safety/safety.py
with_safety()
¶
Context manager for enabling safety on entry, and disabling on exit. Usage: with safety_manager.with_safety(): # safety checks active ... # safety checks stopped
Source code in opensourceleg/extras/safety/safety.py
custom_criteria(criteria)
¶
Creates a decorator to check if a property's value meets a custom criteria. The criteria is a function that takes the property's value as an argument and returns a boolean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
criteria
|
Callable
|
Custom criteria function. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
Decorator function. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the property's value does not meet the custom criteria. |
Source code in opensourceleg/extras/safety/safety.py
is_changing(attribute_name, max_points=10, threshold=1e-06, proxy_attribute_name=None)
¶
Creates a decorator to check if a property's value is changing. If the standard deviation of the last 'max_points' values is less than 'threshold', the decorator will raise an error or return a proxy attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attribute_name
|
str
|
Name of the attribute. |
required |
max_points
|
int
|
Number of points to consider. Defaults to 10. |
10
|
threshold
|
float
|
Threshold for the standard deviation. Defaults to 1e-6. |
1e-06
|
proxy_attribute_name
|
Optional[str]
|
Name of the proxy attribute to return if the property is not changing. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
Decorator function. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the property is not changing and no proxy attribute is provided. |
Source code in opensourceleg/extras/safety/safety.py
is_greater_than(min_value, clamp=False, equality=False)
¶
Creates a decorator to check if a property's value is greater than a given value. Gives user choice to implement is_greater_than_or_equal_to with equality bool
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_value
|
float
|
Minimum value to check against. |
required |
clamp
|
bool
|
If True, the decorator will return the clamped value instead of raising an error. Defaults to False. |
False
|
equality
|
bool
|
If True, the decorator will check for is greater than or equal to, instead of is greater than. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
Decorator function. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the property's value is less than or equal to the minimum value |
Source code in opensourceleg/extras/safety/safety.py
is_less_than(max_value, clamp=False, equality=False)
¶
Creates a decorator to check if a property's value is less than a given value. Gives user choice to implement is_less_than_or_equal_to with equality bool
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_value
|
float
|
Maximum value to check against. |
required |
clamp
|
bool
|
If True, the decorator will return the clamped value instead of raising an error. Defaults to False. |
False
|
equality
|
bool
|
If True, the decorator will check for is less than or equal to, instead of is less than. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
Decorator function. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the property's value is greater than or equal to the maximum value |
Source code in opensourceleg/extras/safety/safety.py
is_negative(clamp=False)
¶
Creates a decorator to check if a property's value is negative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clamp
|
bool
|
If True, the decorator will return 0 instead of raising an error. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
Decorator function. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the property's value is not negative. |
Source code in opensourceleg/extras/safety/safety.py
is_positive(clamp=False)
¶
Creates a decorator to check if a property's value is positive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clamp
|
bool
|
If True, the decorator will return 0 instead of raising an error. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
Decorator function. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the property's value is not positive. |
Source code in opensourceleg/extras/safety/safety.py
is_within_range(min_value, max_value, clamp=False)
¶
Creates a decorator to check if a property's value is within a given range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_value
|
float
|
Minimum value of the range. |
required |
max_value
|
float
|
Maximum value of the range. |
required |
clamp
|
bool
|
If True, the decorator will return the clamped value instead of raising an error. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the maximum value is less than or equal to the minimum value. |
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
Decorator function. |
Source code in opensourceleg/extras/safety/safety.py
is_zero(clamp=False)
¶
Creates a decorator to check if a property's value is zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clamp
|
bool
|
If True, the decorator will return 0 instead of raising an error. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
Decorator function. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the property's value is not zero. |