File size: 821 Bytes
28c2a3d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
from typing import Set
def get_filtered_keys_from_object(obj: object, *keys: str) -> Set[str]:
"""
Get filtered list of object variable names.
:param keys: List of keys to include. If the first key is "not", the remaining keys will be removed from the class keys.
:return: List of class keys.
"""
class_keys = obj.__dict__.keys()
if not keys:
return class_keys
# Remove the passed keys from the class keys.
if keys[0] == "not":
return {key for key in class_keys if key not in keys[1:]}
# Check if all passed keys are valid
if invalid_keys := set(keys) - class_keys:
raise ValueError(
f"Invalid keys: {invalid_keys}",
)
# Only return specified keys that are in class_keys
return {key for key in keys if key in class_keys}
|