Core
fieldz_kb.lpg.core
Core pylpg integration for fieldz_kb.
This module provides: - PylpgTypePlugin: abstract base class for type conversion plugins - PylpgContext: plugin registry and cache - Module-level dispatch functions for converting objects to/from nodes - make_context(): factory for creating fresh contexts with built-in plugins
This module is backend-agnostic — it works with any pylpg backend (Neo4j, FalkorDB, FalkorDBLite). Use with fieldz_kb.lpg.session.Session and a pylpg backend (e.g., pylpg.backend.neo4j.Neo4jBackend).
Classes:
| Name | Description |
|---|---|
PylpgContext |
Plugin registry and cache for pylpg type conversion. |
PylpgTypePlugin |
Abstract base class for pylpg type conversion plugins. |
Functions:
| Name | Description |
|---|---|
get_default_context |
Return the shared default context, creating it on first access. |
get_or_make_node_class_from_type |
Get or create a pylpg node class for a given Python type. |
make_context |
Create a fresh PylpgContext with all built-in plugins registered. |
make_nodes_from_object |
Convert a Python object to pylpg nodes and relationships. |
make_object_from_node |
Convert a pylpg node back to a Python object. |
PylpgContext
Plugin registry and cache for pylpg type conversion.
Stores registered plugin classes and caches for type-to-node-class mappings.
Initialize an empty context with no plugins registered.
Methods:
| Name | Description |
|---|---|
get_plugin_for_node_class |
Look up the plugin class for a pylpg node class. |
get_plugin_for_type |
Look up the plugin class for a Python type. |
register |
Register a type plugin class with this context. |
Source code in src/fieldz_kb/lpg/core.py
get_plugin_for_node_class
get_plugin_for_node_class(node_class: type[BaseNode]) -> type[PylpgTypePlugin]
Look up the plugin class for a pylpg node class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_class
|
type[BaseNode]
|
The node class to look up. |
required |
Returns:
| Type | Description |
|---|---|
type[PylpgTypePlugin]
|
The matching PylpgTypePlugin subclass. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no plugin can handle the node class. |
Source code in src/fieldz_kb/lpg/core.py
get_plugin_for_type
get_plugin_for_type(type_: type) -> type[PylpgTypePlugin]
Look up the plugin class for a Python type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
type
|
The Python type to look up. |
required |
Returns:
| Type | Description |
|---|---|
type[PylpgTypePlugin]
|
The matching PylpgTypePlugin subclass. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no plugin can handle the type. |
Source code in src/fieldz_kb/lpg/core.py
register
register(plugin: type[PylpgTypePlugin]) -> None
Register a type plugin class with this context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plugin
|
type[PylpgTypePlugin]
|
A PylpgTypePlugin subclass (not an instance). |
required |
PylpgTypePlugin
Bases: ABC
Abstract base class for pylpg type conversion plugins.
Plugins are used as classes, not instances — all methods are classmethods. Each plugin handles one or more Python types, providing: - Type/node class matching - Node class creation (for dynamically generated types) - Object-to-node conversion - Node-to-object conversion
Args (for subclass methods): ctx: The PylpgContext providing access to caches and other plugins.
Methods:
| Name | Description |
|---|---|
can_handle_node_class |
Return True if this plugin can handle the given node class. |
can_handle_type |
Return True if this plugin can handle the given Python type. |
make_node_class_from_type |
Create a pylpg node class for the given Python type. |
make_nodes_from_object |
Convert a Python object to pylpg nodes and relationships. |
make_object_from_node |
Convert a pylpg node back to a Python object. |
can_handle_node_class
abstractmethod
classmethod
can_handle_node_class(node_class: type, ctx: PylpgContext) -> bool
Return True if this plugin can handle the given node class.
Used for both direct lookup caching (via _handled_node_classes) and predicate-based fallback dispatch.
Source code in src/fieldz_kb/lpg/core.py
can_handle_type
abstractmethod
classmethod
Return True if this plugin can handle the given Python type.
Used for both direct lookup caching (via _handled_types) and predicate-based fallback dispatch (e.g., for fieldz classes, enums).
Source code in src/fieldz_kb/lpg/core.py
make_node_class_from_type
abstractmethod
classmethod
make_node_class_from_type(type_: type, ctx: PylpgContext, make_node_classes_recursively: bool = True, guard: set[type] | None = None) -> type[BaseNode] | None
Create a pylpg node class for the given Python type.
Returns:
| Type | Description |
|---|---|
type[BaseNode] | None
|
A Node subclass, or None if the type uses a pre-built node class. |
Source code in src/fieldz_kb/lpg/core.py
make_nodes_from_object
abstractmethod
classmethod
make_nodes_from_object(obj: object, ctx: PylpgContext, integration_mode: Literal['hash', 'id'], exclude_from_integration: tuple[type, ...], object_to_node: dict) -> tuple[list[BaseNode], list[Relationship]]
Convert a Python object to pylpg nodes and relationships.
Returns:
| Type | Description |
|---|---|
list[BaseNode]
|
A tuple of (nodes, relationships) where nodes is a list of node instances |
list[Relationship]
|
and relationships is a list of Relationship instances. |
Source code in src/fieldz_kb/lpg/core.py
make_object_from_node
abstractmethod
classmethod
make_object_from_node(node: BaseNode, ctx: PylpgContext, node_id_to_object: dict) -> object
Convert a pylpg node back to a Python object.
Returns:
| Type | Description |
|---|---|
object
|
The reconstructed Python object. |
Source code in src/fieldz_kb/lpg/core.py
get_default_context
get_default_context() -> PylpgContext
Return the shared default context, creating it on first access.
get_or_make_node_class_from_type
get_or_make_node_class_from_type(ctx: PylpgContext, type_: type, make_node_classes_recursively: bool = True, guard: set[type] | None = None) -> type[BaseNode] | None
Get or create a pylpg node class for a given Python type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PylpgContext
|
The plugin registry and cache. |
required |
type_
|
type
|
The Python type to get or create a node class for. |
required |
make_node_classes_recursively
|
bool
|
Whether to create node classes for nested types. |
True
|
guard
|
set[type] | None
|
Set of types currently being processed (prevents infinite recursion). |
None
|
Returns:
| Type | Description |
|---|---|
type[BaseNode] | None
|
The node class (a subclass of BaseNode), or None. |
Source code in src/fieldz_kb/lpg/core.py
make_context
make_context() -> PylpgContext
Create a fresh PylpgContext with all built-in plugins registered.
Source code in src/fieldz_kb/lpg/core.py
make_nodes_from_object
make_nodes_from_object(ctx: PylpgContext, object_: object, integration_mode: Literal['hash', 'id'] = 'id', exclude_from_integration: tuple[type, ...] | None = None, object_to_node: dict | None = None) -> tuple[list[BaseNode], list[Relationship]]
Convert a Python object to pylpg nodes and relationships.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PylpgContext
|
The plugin registry and cache. |
required |
object_
|
object
|
The object to convert. |
required |
integration_mode
|
Literal['hash', 'id']
|
How to handle duplicate objects ("hash" or "id"). |
'id'
|
exclude_from_integration
|
tuple[type, ...] | None
|
Types to exclude from integration logic. |
None
|
object_to_node
|
dict | None
|
Cache mapping objects to their nodes for deduplication. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[list[BaseNode], list[Relationship]]
|
A tuple of (nodes, relationships). |
Source code in src/fieldz_kb/lpg/core.py
make_object_from_node
make_object_from_node(ctx: PylpgContext, node: BaseNode, node_id_to_object: dict | None = None) -> object
Convert a pylpg node back to a Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
PylpgContext
|
The plugin registry and cache. |
required |
node
|
BaseNode
|
The pylpg node to convert. |
required |
node_id_to_object
|
dict | None
|
Optional cache mapping node database IDs to objects. |
None
|
Returns:
| Type | Description |
|---|---|
object
|
The reconstructed Python object. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the node type cannot be mapped to a Python class. |