🔍 Code Extractor

function tree_attribute

Maturity: 47

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.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
376 - 389
Complexity:
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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class sanitize_identifier_fn 51.3% similar

    A parameterized function class that sanitizes strings (group/label values) to make them safe for use as Python attribute names in AttrTree structures by converting special characters to their unicode names and applying transformations.

    From: /tf/active/vicechatdev/patches/util.py
  • function capitalize_unicode_name 45.2% similar

    Transforms Unicode character name strings by removing the word 'capital' and capitalizing the following word, converting strings like 'capital delta' to 'Delta'.

    From: /tf/active/vicechatdev/patches/util.py
  • function get_node_labels 42.6% similar

    Retrieves all non-private attributes from the NodeLabels class as a dictionary, filtering out attributes that start with underscore.

    From: /tf/active/vicechatdev/CDocs/db/schema_manager.py
  • function sanitize_folders 37.8% similar

    Recursively traverses a directory tree and sanitizes folder names by removing non-ASCII characters, renaming folders to ASCII-only versions.

    From: /tf/active/vicechatdev/creation_updater.py
  • function validate_and_alternatives 37.6% similar

    Validates whether a given keyword is a valid chemical compound, biochemical concept, or drug-related term using GPT-4, and returns alternative names/synonyms if valid.

    From: /tf/active/vicechatdev/offline_parser_docstore.py
← Back to Browse