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} | |