function tree_attribute
A predicate function that determines if an identifier string represents a custom attribute in AttrTrees by checking if it starts with a capitalized character or underscore.
/tf/active/vicechatdev/patches/util.py
376 - 389
simple
Purpose
This function is used to filter and identify custom attributes added to AttrTree objects, distinguishing them from methods, properties, and internal attributes. It implements a naming convention where custom attributes should start with a capital letter (when applicable) or an underscore. This is useful for introspection and attribute management in tree-like data structures.
Source Code
def tree_attribute(identifier):
"""
Predicate that returns True for custom attributes added to AttrTrees
that are not methods, properties or internal attributes.
These custom attributes start with a capitalized character when
applicable (not applicable to underscore or certain unicode characters)
"""
if identifier == '':
return True
if identifier[0].upper().isupper() is False and identifier[0] != '_':
return True
else:
return identifier[0].isupper()
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
identifier |
- | - | positional_or_keyword |
Parameter Details
identifier: A string representing an attribute name to be checked. Can be an empty string, start with uppercase/lowercase letters, underscore, or unicode characters. The function evaluates whether this identifier follows the custom attribute naming convention.
Return Value
Returns a boolean value: True if the identifier represents a custom attribute (empty string, starts with capitalized letter, or starts with underscore), False if it starts with a lowercase letter that can be uppercased (indicating it's likely a method or property name).
Usage Example
# Example usage of tree_attribute function
# Custom attributes (returns True)
print(tree_attribute('CustomAttr')) # True - starts with capital
print(tree_attribute('_private')) # True - starts with underscore
print(tree_attribute('')) # True - empty string
print(tree_attribute('123abc')) # True - starts with non-letter
# Non-custom attributes (returns False)
print(tree_attribute('method_name')) # False - starts with lowercase
print(tree_attribute('property')) # False - starts with lowercase
# Use case: filtering attributes
class AttrTree:
CustomData = 'value'
_internal = 'private'
method_name = 'not_custom'
attrs = ['CustomData', '_internal', 'method_name', 'AnotherCustom']
custom_attrs = [attr for attr in attrs if tree_attribute(attr)]
print(custom_attrs) # ['CustomData', '_internal', 'AnotherCustom']
Best Practices
- This function is designed for a specific naming convention where custom attributes start with capital letters or underscores
- Empty strings are considered valid custom attributes by this function
- The function handles unicode characters that may not have uppercase equivalents
- Use this function consistently across an AttrTree implementation to maintain naming conventions
- Be aware that identifiers starting with numbers or special characters (except underscore) will return True
- This is a pure function with no side effects, making it safe for repeated calls
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class sanitize_identifier_fn 51.3% similar
-
function capitalize_unicode_name 45.2% similar
-
function get_node_labels 42.6% similar
-
function sanitize_folders 37.8% similar
-
function validate_and_alternatives 37.6% similar