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.

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Write tests for your changes.
  4. Ensure that pytest and ruff pass without errors.
  5. Submit a pull request with a detailed description of your changes.

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.1.tar.gz (9.8 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.1-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: std_methods-0.1.1.tar.gz
  • Upload date:
  • Size: 9.8 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.1.tar.gz
Algorithm Hash digest
SHA256 fd46e3f23d12486ae7e3476d6652bd74b96646105ea3afdf80e6946821350912
MD5 114c611362728f00a709ffdbda6f8212
BLAKE2b-256 90712976dc8ecd4532f625c792b89c53ffc9becb0dbd598001f582f9ecf28d01

See more details on using hashes here.

Provenance

The following attestation bundles were made for std_methods-0.1.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: std_methods-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3f0d902b72b503122e85730261cfbbd92288347497593246c7996aec077ff65a
MD5 e91a61fc5d5b0db888e736d9de2a26e2
BLAKE2b-256 9d1599345fe555612e0865294a699682447a2f41af5c7333c1ce86c3e90be9e7

See more details on using hashes here.

Provenance

The following attestation bundles were made for std_methods-0.1.1-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