🔍 Code Extractor

function get_all_reference_municipalities

Maturity: 42

Queries a Neo4j graph database to retrieve Reference_Municipalities nodes with a configurable limit on the number of results returned.

File:
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
Lines:
607 - 614
Complexity:
simple

Purpose

This function provides a way to fetch municipality reference data from a Neo4j graph database. It's designed for retrieving a subset of Reference_Municipalities nodes, useful for data exploration, testing, or populating UI elements with municipality options. The function uses Cypher query language to match nodes with the Reference_Municipalities label and returns them through a query execution helper function.

Source Code

def get_all_reference_municipalities(limit=100):
    """Return Reference_Municipalities nodes (limited to 25)"""
    query = """
    MATCH (n:Reference_Municipalities)
    RETURN n
    LIMIT $limit
    """
    return run_query(query, {"limit": limit})

Parameters

Name Type Default Kind
limit - 100 positional_or_keyword

Parameter Details

limit: Integer value that controls the maximum number of Reference_Municipalities nodes to return from the database. Default is 100. Note: The docstring incorrectly states the limit is 25, but the actual default parameter value is 100. This parameter is passed to the Cypher query to prevent returning excessively large result sets.

Return Value

Returns the result of run_query() function execution. The exact return type depends on the implementation of run_query(), but typically would be a list of dictionaries or Neo4j Record objects containing the properties of matched Reference_Municipalities nodes. Each element represents one municipality node with its associated properties. Returns an empty collection if no nodes match or if the database is empty.

Dependencies

  • neo4j

Required Imports

from neo4j import GraphDatabase

Usage Example

# Assuming run_query() is defined and Neo4j connection is configured
# Example run_query implementation:
from neo4j import GraphDatabase

driver = GraphDatabase.driver('bolt://localhost:7687', auth=('neo4j', 'password'))

def run_query(query, params=None):
    with driver.session() as session:
        result = session.run(query, params or {})
        return [record['n'] for record in result]

# Using the function
municipalities = get_all_reference_municipalities(limit=50)
print(f'Retrieved {len(municipalities)} municipalities')

# Get default 100 municipalities
all_municipalities = get_all_reference_municipalities()

# Process results
for municipality in municipalities:
    print(municipality)

Best Practices

  • The docstring contains incorrect information (states limit is 25 but default is 100) - this should be corrected to avoid confusion
  • Always specify an appropriate limit value based on your use case to avoid performance issues with large datasets
  • Ensure the run_query() function properly handles database connection errors and exceptions
  • Consider adding error handling for cases where the database connection fails or the query returns unexpected results
  • The function assumes run_query() is available in scope - ensure proper import or definition of this dependency
  • Consider adding type hints for better code documentation: def get_all_reference_municipalities(limit: int = 100) -> list
  • For production use, consider adding pagination support instead of just a simple limit
  • Validate that the limit parameter is a positive integer to prevent invalid queries

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_reference_municipalities_by_id 84.4% similar

    Retrieves a single Reference_Municipalities node from a Neo4j graph database by matching its unique ID property.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function get_reference_municipalities_by_uid 81.5% similar

    Retrieves a single Reference_Municipalities node from a Neo4j graph database by matching its unique identifier (UID).

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function get_dbo_establishment_with_references_municipalities_reference_municipalities 77.5% similar

    Retrieves Reference_Municipalities nodes from a Neo4j graph database that are connected to a specific dbo_Establishment node via a REFERENCES_MUNICIPALITIES relationship.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_reference_municipalities 73.8% similar

    Creates a new Reference_Municipalities node in a Neo4j graph database with the specified properties and returns the created node.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_municipalities_relationship 64.6% similar

    Creates a REFERENCES_MUNICIPALITIES relationship in a Neo4j graph database between a dbo_Establishment node and a Reference_Municipalities node, with optional properties on the relationship.

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