function get_node_labels
Retrieves all non-private attributes from the NodeLabels class as a dictionary, filtering out attributes that start with underscore.
/tf/active/vicechatdev/CDocs/db/schema_manager.py
505 - 508
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class NodeLabels 71.3% similar
-
function get_nodes_by_label 58.9% similar
-
function get_database_statistics 55.0% similar
-
function get_relationship_types 51.7% similar
-
function create_node 51.5% similar