cornsnake.util_object
Functions for working with object attributes. The get_attributes
function retrieves all non-private attributes of an object. The get_attribute_value
function returns the value of a specific attribute of an object.
1""" 2Functions for working with object attributes. The `get_attributes` function retrieves all non-private attributes of an object. The `get_attribute_value` function returns the value of a specific attribute of an object. 3 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_object.html) 5""" 6 7import typing 8 9 10def get_attributes(object: typing.Any) -> list[str]: 11 """ 12 Dynamically get all non-private attributes of an object (reflection). 13 14 Args: 15 object: The object to retrieve attributes from. 16 17 Returns: 18 list: A list of non-private attributes of the object. 19 """ 20 attributes = [] 21 all_properties = dir(object) 22 for prop in all_properties: 23 if not prop.startswith("_"): # skip built-ins 24 attributes.append(prop) 25 return attributes 26 27 28def get_attribute_value(object: typing.Any, attribute_name: str) -> typing.Any: 29 """ 30 Dynamically get the value of a specific attribute of an object. 31 32 Args: 33 object: The object to retrieve the attribute value from. 34 attribute_name: The name of the attribute to get the value of. 35 36 Returns: 37 The value of the specified attribute of the object. 38 """ 39 return getattr(object, attribute_name) 40 41 42def set_attribute_value( 43 object: typing.Any, attribute_name: str, value: typing.Any 44) -> None: 45 """Dynamically set the value of the given attribute of that object. 46 47 Args: 48 object: The object upon which to set the attribute value. 49 attribute_name: The name of the attribute to set the value of. 50 """ 51 setattr(object, attribute_name, value) 52 53 54def has_attribute(object: typing.Any, attribute_name: str) -> bool: 55 """ 56 Dynamically check if the given object has an attribute with that name (reflection). 57 58 Args: 59 object: The object to retrieve attributes from. 60 attribute_name: The name of the attribute to find. 61 62 Returns: 63 True if that object has an attribute with that name. Else False. 64 """ 65 return attribute_name in get_attributes(object)
11def get_attributes(object: typing.Any) -> list[str]: 12 """ 13 Dynamically get all non-private attributes of an object (reflection). 14 15 Args: 16 object: The object to retrieve attributes from. 17 18 Returns: 19 list: A list of non-private attributes of the object. 20 """ 21 attributes = [] 22 all_properties = dir(object) 23 for prop in all_properties: 24 if not prop.startswith("_"): # skip built-ins 25 attributes.append(prop) 26 return attributes
Dynamically get all non-private attributes of an object (reflection).
Args: object: The object to retrieve attributes from.
Returns: list: A list of non-private attributes of the object.
29def get_attribute_value(object: typing.Any, attribute_name: str) -> typing.Any: 30 """ 31 Dynamically get the value of a specific attribute of an object. 32 33 Args: 34 object: The object to retrieve the attribute value from. 35 attribute_name: The name of the attribute to get the value of. 36 37 Returns: 38 The value of the specified attribute of the object. 39 """ 40 return getattr(object, attribute_name)
Dynamically get the value of a specific attribute of an object.
Args: object: The object to retrieve the attribute value from. attribute_name: The name of the attribute to get the value of.
Returns: The value of the specified attribute of the object.
43def set_attribute_value( 44 object: typing.Any, attribute_name: str, value: typing.Any 45) -> None: 46 """Dynamically set the value of the given attribute of that object. 47 48 Args: 49 object: The object upon which to set the attribute value. 50 attribute_name: The name of the attribute to set the value of. 51 """ 52 setattr(object, attribute_name, value)
Dynamically set the value of the given attribute of that object.
Args: object: The object upon which to set the attribute value. attribute_name: The name of the attribute to set the value of.
55def has_attribute(object: typing.Any, attribute_name: str) -> bool: 56 """ 57 Dynamically check if the given object has an attribute with that name (reflection). 58 59 Args: 60 object: The object to retrieve attributes from. 61 attribute_name: The name of the attribute to find. 62 63 Returns: 64 True if that object has an attribute with that name. Else False. 65 """ 66 return attribute_name in get_attributes(object)
Dynamically check if the given object has an attribute with that name (reflection).
Args: object: The object to retrieve attributes from. attribute_name: The name of the attribute to find.
Returns: True if that object has an attribute with that name. Else False.