function get_all_dbo_flocks
Queries a Neo4j graph database to retrieve all nodes labeled as 'dbo_Flocks' with a configurable limit on the number of results returned.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
841 - 848
simple
Purpose
This function is designed to fetch dbo_Flocks nodes from a Neo4j graph database. It's useful for retrieving flock-related data in applications that model biological, social, or organizational groupings. The function provides a simple interface to query the database with a customizable result limit, making it suitable for pagination or limiting data retrieval for performance reasons. Note: There's a discrepancy between the docstring (which mentions a limit of 25) and the actual default parameter value (100).
Source Code
def get_all_dbo_flocks(limit=100):
"""Return dbo_Flocks nodes (limited to 25)"""
query = """
MATCH (n:dbo_Flocks)
RETURN n
LIMIT $limit
"""
return run_query(query, {"limit": limit})
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
limit |
- | 100 | positional_or_keyword |
Parameter Details
limit: An integer that specifies the maximum number of dbo_Flocks nodes to return from the database query. Default value is 100. This parameter helps control the size of the result set and can be adjusted based on performance requirements or pagination needs. Must be a positive integer.
Return Value
Returns the result of the run_query function, which typically contains a list or collection of Neo4j node objects matching the dbo_Flocks label. The exact return type depends on the implementation of run_query, but it likely returns a list of dictionaries or Neo4j Record objects containing the properties of each matched node. The number of results will not exceed the specified limit parameter.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
from neo4j import GraphDatabase
# Assuming run_query is defined elsewhere
def run_query(query, params):
driver = GraphDatabase.driver('bolt://localhost:7687', auth=('neo4j', 'password'))
with driver.session() as session:
result = session.run(query, params)
return [record['n'] for record in result]
driver.close()
# Get default 100 flocks
flocks = get_all_dbo_flocks()
print(f'Retrieved {len(flocks)} flocks')
# Get only 10 flocks
flocks_limited = get_all_dbo_flocks(limit=10)
for flock in flocks_limited:
print(flock)
Best Practices
- The docstring incorrectly states the limit is 25, but the default parameter is 100. Update the docstring to match the actual default value.
- Ensure the run_query function properly handles database connections and closes them to prevent connection leaks.
- Consider adding error handling for cases where the database is unavailable or the query fails.
- The limit parameter should be validated to ensure it's a positive integer before passing to the query.
- For large datasets, consider implementing pagination by combining this function with SKIP and LIMIT clauses.
- Add type hints to improve code clarity: def get_all_dbo_flocks(limit: int = 100) -> list
- Consider adding a return type annotation to document what the function returns.
- The function depends on run_query being defined elsewhere; ensure this dependency is clearly documented or imported.
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_all_dbo_flocktypes 90.6% similar
-
function get_dbo_flocks_by_id 82.7% similar
-
function get_all_dbo_interventionprotocolflocks 81.7% similar
-
function get_dbo_flocks_with_references_flocktypes_dbo_flocktypes 79.9% similar
-
function get_dbo_flocks_by_uid 77.0% similar