🔍 Code Extractor

function get_node_labels

Maturity: 37

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

File:
/tf/active/vicechatdev/CDocs/db/schema_manager.py
Lines:
505 - 508
Complexity:
simple

Purpose

This function provides a convenient way to access all node label constants defined in the NodeLabels class. It's typically used in graph database operations to dynamically retrieve available node types without hardcoding them. The function filters out private/magic methods (those starting with '_') to return only the meaningful label constants. This is useful for validation, documentation generation, or dynamic query construction in Neo4j graph database contexts.

Source Code

def get_node_labels() -> Dict[str, str]:
    """Get dictionary of node label constants."""
    return {key: value for key, value in vars(NodeLabels).items() 
            if not key.startswith('_')}

Return Value

Type: Dict[str, str]

Returns a dictionary where keys are the attribute names from the NodeLabels class and values are their corresponding string values. Only includes public attributes (those not starting with underscore). The dictionary maps node label constant names to their actual label values used in the graph database. Example: {'USER': 'User', 'DOCUMENT': 'Document'}

Dependencies

  • typing

Required Imports

from typing import Dict

Usage Example

# Assuming NodeLabels class is defined:
# class NodeLabels:
#     USER = 'User'
#     DOCUMENT = 'Document'
#     PROJECT = 'Project'

from typing import Dict

# Get all node labels
labels = get_node_labels()
print(labels)
# Output: {'USER': 'User', 'DOCUMENT': 'Document', 'PROJECT': 'Project'}

# Use in validation
if 'USER' in labels:
    print(f"User label exists: {labels['USER']}")

# Iterate over all labels
for label_name, label_value in labels.items():
    print(f"Label constant {label_name} maps to {label_value}")

Best Practices

  • Ensure the NodeLabels class is properly defined and in scope before calling this function
  • The NodeLabels class should contain only string constants representing node types
  • This function uses reflection (vars()) which assumes NodeLabels is a class with class-level attributes
  • The returned dictionary is a snapshot; changes to NodeLabels after calling this function won't be reflected
  • Consider caching the result if called frequently, as reflection operations have some overhead
  • Private attributes (starting with '_') are intentionally excluded to avoid exposing internal implementation details

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class NodeLabels 71.3% similar

    A constants class that defines string labels for different node types in a Neo4j graph database schema for a document management system (CDocs).

    From: /tf/active/vicechatdev/CDocs/db/schema_manager.py
  • function get_nodes_by_label 58.9% similar

    Retrieves nodes from a Neo4j graph database by label with optional property filtering, pagination, and sorting capabilities.

    From: /tf/active/vicechatdev/CDocs/db/db_operations.py
  • function get_database_statistics 55.0% similar

    Retrieves statistical information about a Neo4j graph database, including counts of nodes grouped by label and counts of relationships grouped by type.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function get_relationship_types 51.7% similar

    Retrieves a dictionary of relationship type constants from the RelTypes class, filtering out private attributes that start with underscore.

    From: /tf/active/vicechatdev/CDocs/db/schema_manager.py
  • function create_node 51.5% similar

    Creates a node in a Neo4j graph database with a specified label and properties, automatically generating a unique ID and timestamp if not provided.

    From: /tf/active/vicechatdev/CDocs/db/db_operations.py
← Back to Browse