🔍 Code Extractor

function get_manual_relationship_manager

Maturity: 41

Returns a singleton instance of the ManualRelationshipManager class, creating it on first access using lazy initialization.

File:
/tf/active/vicechatdev/full_smartstat/manual_relationships.py
Lines:
158 - 163
Complexity:
simple

Purpose

This function implements the singleton pattern to provide global access to a single ManualRelationshipManager instance throughout the application. It ensures that only one manager exists, which is useful for maintaining consistent state when managing manual relationships between entities. The lazy initialization approach defers object creation until first use, optimizing resource usage.

Source Code

def get_manual_relationship_manager() -> ManualRelationshipManager:
    """Get the global manual relationship manager instance."""
    global _manager
    if _manager is None:
        _manager = ManualRelationshipManager()
    return _manager

Return Value

Type: ManualRelationshipManager

Returns a ManualRelationshipManager instance. This is always the same singleton instance across all calls to this function. The ManualRelationshipManager is responsible for managing manual relationships, likely between data entities or objects in the system.

Required Imports

from typing import Optional

Usage Example

# Assuming ManualRelationshipManager class is defined
# and global _manager variable exists

# Get the singleton instance
manager = get_manual_relationship_manager()

# Use the manager (example methods depend on ManualRelationshipManager implementation)
# manager.add_relationship(entity1, entity2)
# manager.get_relationships(entity1)

# Subsequent calls return the same instance
manager2 = get_manual_relationship_manager()
assert manager is manager2  # True - same instance

Best Practices

  • This function relies on a module-level global variable '_manager' that must be initialized (typically as None) before this function is called
  • The singleton pattern can make testing difficult; consider dependency injection for better testability
  • Thread safety is not guaranteed in this implementation; if used in multi-threaded contexts, consider adding locks
  • The global state makes the code harder to reason about; use sparingly and document where the manager is accessed
  • Ensure ManualRelationshipManager class is properly defined before calling this function

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class ManualRelationshipManager 63.9% similar

    A class that manages manually defined database relationships with persistent JSON storage, allowing users to add, retrieve, update, and remove relationship definitions between database tables.

    From: /tf/active/vicechatdev/full_smartstat/manual_relationships.py
  • function get_driver 51.9% similar

    Singleton function that initializes and returns a Neo4j database driver instance with connection verification.

    From: /tf/active/vicechatdev/CDocs/db/__init__.py
  • function add_manual_relationship 51.3% similar

    Flask route handler that adds a manually specified database relationship to the schema and clears the schema cache to force reload with the new relationship.

    From: /tf/active/vicechatdev/full_smartstat/app.py
  • function create_training_management 49.5% similar

    Factory function that instantiates and returns a TrainingManagement object with optional parent application and document UID parameters.

    From: /tf/active/vicechatdev/CDocs/ui/training_management.py
  • class Neo4jManager 45.9% similar

    A manager class that provides a high-level interface for interacting with Neo4j graph databases, handling connections, queries, node creation, and relationship management.

    From: /tf/active/vicechatdev/QA_updater/knowledge_store/neo4j_manager.py
← Back to Browse