Skip to main content

A central repository for shared logic and utilities across the tool's microservices.

Project description

std-methods

std-methods is a utility library that provides a standardised set of methods to build domain-specific payloads and interact with Neo4j databases. The package includes:

Features

Factories:

  • DiagramFactory: Easily create diagram payloads with default values (or use custom builder functions).
  • NodeFactory: Build standardised payloads for various node types (such as "function" or "element").
  • RelationshipFactory: Generate relationship payloads for connecting nodes (or create custom relationship models).

Repository Base:

  • BaseRepository: A foundational class for interacting with a Neo4j database. It handles connection initialisation, simple query execution, and proper closure of connections.

Utility Functions:

  • generate_id: A helper function that creates unique IDs with a specified prefix using Python’s uuid module.

Installation

You can install the package directly from PyPI:

pip install std-methods

Usage

Once installed, you can import and use the provided components in your Python code.

Importing the Package

from std_methods import (
    DiagramFactory,
    NodeFactory,
    RelationshipFactory,
    BaseRepository,
    generate_id
)

Creating Diagrams

The DiagramFactory helps you create diagram payloads. It can use default values or a custom builder if registered.

# Create a generic diagram payload:
diagram = DiagramFactory.create_diagram("flow", name="My Flow Diagram")
print(diagram)

Example Output:

{
  "id": "Dgm-<uuid>",
  "name": "My Flow Diagram",
  "description": "No description provided",
  "project_id": "Unassigned Project",
  "placements": [],
  "extra_info": "",
  "type": "FLOW"
}

Register a Custom Builder

def custom_diagram_builder(id, name, description, project_id, placements, extra_info, type):
    return {
        "diagram_id": id,
        "title": name,
        "details": description,
        "project": project_id,
        "data": placements,
        "extra": extra_info,
        "diagram_type": type,
    }

DiagramFactory.register_builder("custom", custom_diagram_builder)
custom_diagram = DiagramFactory.create_diagram("custom", name="Custom Diagram")
print(custom_diagram)

Creating Nodes

Use NodeFactory to create node payloads for different types.

# Create a function node payload:
function_node = NodeFactory.create_node(
    "function",
    name="Process Data",
    description="Processes input data and produces outputs."
)
print(function_node)
# Create an element node payload:
element_node = NodeFactory.create_node("element", name="Element 1", document_ids=["doc1", "doc2"])
print(element_node)

Creating Relationships

RelationshipFactory standardizes the creation of relationships between nodes.

# Create a relationship between two nodes:
relationship = RelationshipFactory.create_relationship(
    "depends_on",
    source_id="N-123",
    target_id="N-456"
)
print(relationship)

Using the BaseRepository

BaseRepository provides a simple way to interact with a Neo4j database.

from std_methods import BaseRepository

# Initialize the repository (connection details can be overridden via parameters or environment variables)
repo = BaseRepository(uri="bolt://localhost:7687", user="neo4j", password="secret")

# Execute a query:
try:
    result = repo.execute_query("MATCH (n) RETURN n LIMIT 5")
    print("Query results:", result)
finally:
    repo.close()

Utility Function: generate_id

Generate unique IDs for your objects with a specific prefix:

unique_id = generate_id("N")
print("Generated ID:", unique_id)
graph TD;
    A[DiagramFactory] -->|Creates| B[Diagram Payload];
    C[NodeFactory] -->|Creates| D[Node Payload];
    E[RelationshipFactory] -->|Creates| F[Relationship Payload];
    G[BaseRepository] -->|Queries| H[Neo4j Database];

Linting and Testing

This package is fully tested using pytest and linted with ruff. To run tests and check linting locally:

Run Tests:

pytest

Run Linter (Ruff):

ruff .

Note: If you encounter any false positives (for instance, with your re-exported modules in __init__.py), see the linter configuration section in our documentation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

std_methods-0.1.2.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

std_methods-0.1.2-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file std_methods-0.1.2.tar.gz.

File metadata

  • Download URL: std_methods-0.1.2.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for std_methods-0.1.2.tar.gz
Algorithm Hash digest
SHA256 eebee4cace6ef172904d7d2cdd118a2022562afc41093529490bc6a42de14e66
MD5 5150866b39d19512ee968fa77bf5499a
BLAKE2b-256 c7e893c00d1cd02033cc29d3bfd8094228cb95b77e30fd73dfd8a429568b146f

See more details on using hashes here.

Provenance

The following attestation bundles were made for std_methods-0.1.2.tar.gz:

Publisher: publish.yml on OpenForte/common_library

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file std_methods-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: std_methods-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for std_methods-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 166d58c9c1b0f5b226f36060569e2392a08fcb389f5f6391b562106b595a153f
MD5 4984273cb299fd2c485eae18b2fa0c33
BLAKE2b-256 12c2bdc5b41a3b44395457e7f529523d5cbc21dc7b9e3aa481405504e910360b

See more details on using hashes here.

Provenance

The following attestation bundles were made for std_methods-0.1.2-py3-none-any.whl:

Publisher: publish.yml on OpenForte/common_library

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page