Skip to main content

TODO

Project description

DaprGen: Streamlining Microservice Development

Welcome to DaprGen, a powerful command-line interface (CLI) designed to revolutionize microservice development by leveraging Dapr modules. Inspired by the efficiency and modularity of frameworks like Ruby on Rails, DaprGen simplifies the process of creating, developing, and deploying microservices.

Table of Contents

  1. Introduction
  2. Features
  3. Getting Started
  4. Usage
  5. Module Creation
  6. Best Practices
  7. Structuring Code: A New Class of Digital Assets (by Dr Holger Vogel)
  8. Contributing
  9. License

Introduction

DaprGen, influenced by the pioneering work of Sean Chatman and James I. Chatman, embodies a structured approach to microservice development. This tool is designed to streamline your workflow, enhance productivity, and ensure you stay ahead in the rapidly evolving tech landscape.

Features

  • Quick Initialization: Set up your DaprGen project in seconds, echoing the ease of starting new projects with Ruby on Rails.
  • Modular Approach: Inspired by Ruby on Rails' modular design, DaprGen allows for the easy generation and enhancement of Dapr modules.
  • Intuitive Command Structure: Manage your microservice development workflow with straightforward commands.

Quick Start

Initialize a new DaprGen project:

daprgen init my_project
cd my_project

Generate a new module:

daprgen module new -cn OrderProcessor -i "order" -o "confirmation"

Run the module:

daprgen module order_processor call '{"orderId": "12345"}' 

Serve the REST API:

docker-compose up app

Module Creation

The daprgen module new command is the cornerstone of DaprGen, enabling users to create new modules efficiently. This section highlights the usage and provides common use cases for microservices.

Usage

daprgen module new [OPTIONS]

Options

  • --class-name, -cn TEXT: The name of the module class (required).
  • --inputs, -i TEXT: A comma-separated list of input names.
  • --output, -o TEXT: Output name for the module.
  • --help: Show this message and exit.

Common Use Cases

  1. Order Processing:

    daprgen module new -cn OrderProcessor -i "order" -o "confirmation"
    
  2. Inventory Management:

    daprgen module new -cn InventoryManager -i "productId,quantity" -o "status"
    
  3. Payment Processing:

    daprgen module new -cn PaymentProcessor -i "paymentDetails" -o "receipt"
    
  4. User Authentication:

    daprgen module new -cn Authenticator -i "username,password" -o "token"
    
  5. Notification Service:

    daprgen module new -cn Notifier -i "message,recipient" -o "status"
    

Getting Started

Using Dapr Components:

from daprgen.utils.dapr_tools import init_dapr
from daprgen.modules.order_processor_module import order_processor_call

init_dapr(component="pubsub", config="config.yaml")
order_processor_call({"orderId": "12345"})

Using Redis State Store: Ensure your .env file is configured for Redis. Modify your config.yaml as necessary.

from daprgen.utils.dapr_tools import init_dapr
from daprgen.state.redis import RedisState
from daprgen.modules.order_processor_module import order_processor_call

init_dapr(state_store=RedisState, config="config.yaml")
order_processor_call({"orderId": "12345"})

Privacy and Data Protection: For privacy and data loss protection, we recommend initializing DaprGen with secure state stores.

Install Redis: Follow the official Redis installation guide to set up the necessary tools.

from daprgen.utils.dapr_tools import init_dapr
from daprgen.state.redis import RedisState
from daprgen.modules.order_processor_module import order_processor_call

init_dapr(state_store=RedisState, config="config.yaml")
order_processor_call({"orderId": "12345"})

By following these steps, you can integrate the concept of structured commodities into your microservice development workflow, ensuring compliance and fair compensation for creators.

By understanding and utilizing the daprgen module new command, you can harness the full potential of DaprGen to create powerful and flexible microservice development workflows.

Production Module

The following example demonstrates a production module that generates a mock Pytest module for a given Python source code. This module is designed to create comprehensive and robust mock tests that simulate possible unit tests based on the functions and methods defined within the source code.

import dapr

class GenerateMockPytest(dapr.Signature):
    """
    Generates a mocked pytest module for the provided Python source code.
    This class aims to create comprehensive and robust mock tests that simulate
    possible unit tests based on the functions and methods defined within the source code.
    Write the test like a FAANG Python architect at Meta.
    Only reply within ```python``` block. All other text needs to be in docstrings or comments.
    """
    source_code = dapr.InputField(desc="Python source code for which to generate a mock test.")
    mocked_pytest = dapr.OutputField(desc="Generated mock pytest code. Within triple backticks", 
                                     prefix="```python\n")

class PytestModule(dapr.Module):
    """PytestModule"""

    def __init__(self, **forward_args):
        super().__init__()
        self.forward_args = forward_args
        self.output = None

    def forward(self, source_code):
        pred = dapr.Predict(GenerateMockPytest)
        self.output = pred(source_code=source_code).mocked_pytest
        return self.output

def pytest_call(source_code):
    pytest = PytestModule()
    return pytest.forward(source_code=source_code)

example_code = """def fetch_user_name(user_id):
    import requests
    response = requests.get(f'https://api.example.com/users/{user_id}')
    return response.json()['name']
"""

def main():
    from daprgen.utils.dapr_tools import init_dapr
    dapr_instance = init_dapr()
    source_code = example_code
    result = pytest_call(source_code=source_code)
    from daprgen.utils.file_tools import extract_code
    print(extract_code(result))
    print(dapr_instance.inspect_history(n=1))

if __name__ == "__main__":
    main()

Example: Generated Pytest Module (Continued)

import pytest
from your_module import fetch_user_name

@pytest.fixture
def mocker():
    return pytest.mockito()

def test_fetch_user_name(mocker):
    mocked_requests_get = mocker.patch('requests.get')
    response_json = {'name': 'John Doe'}
    mocked_requests_get.return_value.json.return_value = response_json
    
    result = fetch_user_name(123)
    assert result == 'John Doe'
    
    # Verify that the requests.get call was not made
    assert not mocked_requests_get.called

Example: Running the Test

Initial state: ANALYZING_REQUIREMENTS
Test Failed: ============================= test session starts ==============================
platform darwin -- Python 3.12.3, pytest-8.2.0, pluggy-1.5.0 -- /Users/sac/Library/Caches/pypoetry/virtualenvs/soc-FgW3JNy9-py3.12/bin/python
cachedir: .pytest_cache
rootdir: /var/folders/s6/jqyw48zs39z38b_3f6f_x2sc0000gn/T
plugins: anyio-4.3.0, clarity-1.0.1, Faker-23.3.0, asyncio-0.23.6, mock-3.14.0, xdist-3.6.1
asyncio: mode=Mode.STRICT
collecting ... collected 1 item

../../../../../../../var/folders/s6/jqyw48zs39z38b_3f6f_x2sc0000gn/T/tmp880863oe_test.py::test_fetch_user_name ERROR [100%]

==================================== ERRORS ====================================
____________________ ERROR at setup of test_fetch_user_name ____________________

    @pytest.fixture
    def mocker():
>       return pytest.mockito()
E       AttributeError: module 'pytest' has no attribute 'mockito'

/var/folders/s6/jqyw48zs39z38b_3f6f_x2sc0000gn/T/tmp880863oe_test.py:6: AttributeError
=========================== short test summary info ============================
ERROR ../../../../../../../var/folders/s6/jqyw48zs39z38b_3f6f_x2sc0000gn/T/tmp880863oe_test.py::test_fetch_user_name
=============================== 1 error in 0.04s ===============================

Best Practices

Daily Productivity Process

  1. Set Clear Goals: Use the Ivy Lee Method to prioritize your daily tasks.

Use Pomodoro Technique: Work in focused intervals to maintain productivity. 3. Regular Reviews: Reflect on progress and plan for the next day.

Embrace Continuous Learning

Stay updated with the latest advancements in microservice technology and share your insights with the community to enhance collective productivity.

Structuring Code: A New Class of Digital Assets

Dematerialized Commodity Concept

Inspired by financial engineering and the structuring of financial products, we aim to bring the same level of compliance and innovation to code generation systems. In today's technology-driven world, it is crucial to ensure that creators of valuable, new code receive appropriate compensation whenever their code is analyzed, cloned, or used, especially at the enterprise level.

NFTs as Structured Commodities

The foundation for all valuable, useful code should be a new form of NFT – a structured commodity of code. This concept is akin to a dematerialized asset, similar to the Meta-Bricks repository we previously created. This would involve a massive store of runnable and easily pluggable/composable elements of code, paired with terms and conditions familiar from classical structured products (e.g., Ricardian Contracts).

Legal Compliance and Revenue Sharing

To minimize legal risks and ensure proper compensation, retrievers should use these structured commodities for code generation workflows. They should always send payments or share revenues from new creations derived from these meta-bricks to the original creators. While many current code generation tools do not reference the source of the code, this is an area that can and should be improved.

By Dr Holger Vogel (LinkedIn)

Contributing

We welcome contributions to improve DaprGen. Please follow the guidelines in the CONTRIBUTING.md file.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

By leveraging the structured approach and productivity principles from the Sean Chatman and James I. Chatman Methods, DaprGen aims to enhance your microservice development experience. Get started today and streamline your workflow with ease!

For more information, visit our GitHub repository.

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

daprgen-2024.5.24.tar.gz (43.2 kB view details)

Uploaded Source

Built Distribution

daprgen-2024.5.24-py3-none-any.whl (50.7 kB view details)

Uploaded Python 3

File details

Details for the file daprgen-2024.5.24.tar.gz.

File metadata

  • Download URL: daprgen-2024.5.24.tar.gz
  • Upload date:
  • Size: 43.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Darwin/23.4.0

File hashes

Hashes for daprgen-2024.5.24.tar.gz
Algorithm Hash digest
SHA256 973582142c7d221e11d578d3e47204e5248d7a678001645ede10fcb8eec6a8ca
MD5 61b62e06557a224fdfa63165b3b0c270
BLAKE2b-256 97610fe572c58c2b5bb2779cd964d06e77808b627f975a1d0d58a88788ef5f3a

See more details on using hashes here.

File details

Details for the file daprgen-2024.5.24-py3-none-any.whl.

File metadata

  • Download URL: daprgen-2024.5.24-py3-none-any.whl
  • Upload date:
  • Size: 50.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Darwin/23.4.0

File hashes

Hashes for daprgen-2024.5.24-py3-none-any.whl
Algorithm Hash digest
SHA256 776018c9831e89e6d5106783f99c580addaab24c669b2c99a5791b292f29d77b
MD5 84bb5b056cd20eb93b9ec9d5e45a2e57
BLAKE2b-256 d30504ad5b7f09300f6ce03e6d2135fcda453260fa161b8305b7142055b8990d

See more details on using hashes here.

Supported by

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