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 7 8def get_attributes(object): 9 """ 10 Dynamically get all non-private attributes of an object (reflection). 11 12 Args: 13 object: The object to retrieve attributes from. 14 15 Returns: 16 list: A list of non-private attributes of the object. 17 """ 18 attributes = [] 19 all_properties = dir(object) 20 for prop in all_properties: 21 if not prop.startswith("_"): # skip built-ins 22 attributes.append(prop) 23 return attributes 24 25 26def get_attribute_value(object, attribute_name): 27 """ 28 Dynamically get the value of a specific attribute of an object. 29 30 Args: 31 object: The object to retrieve the attribute value from. 32 attribute_name: The name of the attribute to get the value of. 33 34 Returns: 35 The value of the specified attribute of the object. 36 """ 37 return getattr(object, attribute_name) 38 39 40def set_attribute_value(object, attribute_name, value): 41 """Dynamically set the value of the given attribute of that object. 42 43 Args: 44 object: The object upon which to set the attribute value. 45 attribute_name: The name of the attribute to set the value of. 46 """ 47 setattr(object, attribute_name, value) 48 49 50def has_attribute(object, attribute_name): 51 """ 52 Dynamically check if the given object has an attribute with that name (reflection). 53 54 Args: 55 object: The object to retrieve attributes from. 56 attribute_name: The name of the attribute to find. 57 58 Returns: 59 True if that object has an attribute with that name. Else False. 60 """ 61 return attribute_name in get_attributes(object)
9def get_attributes(object): 10 """ 11 Dynamically get all non-private attributes of an object (reflection). 12 13 Args: 14 object: The object to retrieve attributes from. 15 16 Returns: 17 list: A list of non-private attributes of the object. 18 """ 19 attributes = [] 20 all_properties = dir(object) 21 for prop in all_properties: 22 if not prop.startswith("_"): # skip built-ins 23 attributes.append(prop) 24 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.
27def get_attribute_value(object, attribute_name): 28 """ 29 Dynamically get the value of a specific attribute of an object. 30 31 Args: 32 object: The object to retrieve the attribute value from. 33 attribute_name: The name of the attribute to get the value of. 34 35 Returns: 36 The value of the specified attribute of the object. 37 """ 38 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.
41def set_attribute_value(object, attribute_name, value): 42 """Dynamically set the value of the given attribute of that object. 43 44 Args: 45 object: The object upon which to set the attribute value. 46 attribute_name: The name of the attribute to set the value of. 47 """ 48 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.
51def has_attribute(object, attribute_name): 52 """ 53 Dynamically check if the given object has an attribute with that name (reflection). 54 55 Args: 56 object: The object to retrieve attributes from. 57 attribute_name: The name of the attribute to find. 58 59 Returns: 60 True if that object has an attribute with that name. Else False. 61 """ 62 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.