spytula


Namespytula JSON
Version 0.0.3 PyPI version JSON
download
home_pagehttps://github.com/pirhoo/spytula/
SummaryA Python library that provides a simple and convenient way to build JSON and YAML data structures using a builder pattern.
upload_time2023-06-20 09:15:32
maintainer
docs_urlNone
authorPierre Romera
requires_python>=3.8.1,<4.0.0
licenseMIT
keywords json yaml dsl
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 👩‍🍳 Spytula [![](https://img.shields.io/github/actions/workflow/status/pirhoo/spytula/main.yml)](https://github.com/pirhoo/spytula/actions)

Spytula is a Python library that provides a simple and convenient way to build JSON and YAML data structures using a builder pattern.

## Installation

Use pip to install the Spytula library:

```bash
pip install spytula
```

## Usage    

Import the `SpytulaBuilder` class from the `spytula.builder` module:

```python
from spytula.builder import SpytulaBuilder

# Create an instance of SpytulaBuilder
builder = SpytulaBuilder()

# Add attributes to the JSON structure
builder.attribute('name', 'Ramen')
builder.attribute('origin', 'Japan')

# Create a list of ingredients
for builder.each('ingredients') as add_ingredient:    
    for ingredient in ['Noodles', 'Pork', 'Eggs', 'Miso']:
        with add_ingredient() as ingredient_builder:
            ingredient_builder.attribute('name', ingredient)

# Add optional attributes conditionally
builder.when('spiciness', 'Medium', True)
builder.when('extra_toppings', ['Green Onions', 'Nori', 'Bamboo Shoots'], True)

# Configure the key to use camelcase
builder.key_format(camelize={'uppercase_first_letter': False})

# Convert the JSON structure to JSON-formatted string
json_output = builder.to_json(indent=4)

# Print the JSON output
print(json_output)
```

This will output:

```json
{
    "name": "Ramen",
    "origin": "Japan",
    "ingredients": [
        { "name": "Noodles" },
        { "name": "Pork" },
        { "name": "Eggs" },
        { "name": "Miso" }
    ],
    "spiciness": "Medium",
    "extraToppings": [
        "Green Onions",
        "Nori",
        "Bamboo Shoots"
    ]
}

```

In this example, we create a `SpytulaBuilder` instance and add attributes like name and origin to represent `Ramen`. We use the `nodes()` context manager to create a list of ingredients and add them to the JSON structure. Optional attributes like spiciness and toppings are added conditionally using the `when()` method. Finally, we convert the JSON structure to a JSON-formatted string using `to_json()` with an indentation of 4 spaces.

## Documentation    

Refer for the [documentation](https://pirhoo.github.io/spytula/) for more details.
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/pirhoo/spytula/",
    "name": "spytula",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8.1,<4.0.0",
    "maintainer_email": "",
    "keywords": "JSON,YAML,DSL",
    "author": "Pierre Romera",
    "author_email": "hello@pirhoo.com",
    "download_url": "https://files.pythonhosted.org/packages/43/c5/e98bfc4bb6384e0ebfcca32ab4f1ea1a861e4ce2b6875f1cc4f4f166cc8c/spytula-0.0.3.tar.gz",
    "platform": null,
    "description": "# \ud83d\udc69\u200d\ud83c\udf73 Spytula [![](https://img.shields.io/github/actions/workflow/status/pirhoo/spytula/main.yml)](https://github.com/pirhoo/spytula/actions)\n\nSpytula is a Python library that provides a simple and convenient way to build JSON and YAML data structures using a builder pattern.\n\n## Installation\n\nUse pip to install the Spytula library:\n\n```bash\npip install spytula\n```\n\n## Usage    \n\nImport the `SpytulaBuilder` class from the `spytula.builder` module:\n\n```python\nfrom spytula.builder import SpytulaBuilder\n\n# Create an instance of SpytulaBuilder\nbuilder = SpytulaBuilder()\n\n# Add attributes to the JSON structure\nbuilder.attribute('name', 'Ramen')\nbuilder.attribute('origin', 'Japan')\n\n# Create a list of ingredients\nfor builder.each('ingredients') as add_ingredient:    \n    for ingredient in ['Noodles', 'Pork', 'Eggs', 'Miso']:\n        with add_ingredient() as ingredient_builder:\n            ingredient_builder.attribute('name', ingredient)\n\n# Add optional attributes conditionally\nbuilder.when('spiciness', 'Medium', True)\nbuilder.when('extra_toppings', ['Green Onions', 'Nori', 'Bamboo Shoots'], True)\n\n# Configure the key to use camelcase\nbuilder.key_format(camelize={'uppercase_first_letter': False})\n\n# Convert the JSON structure to JSON-formatted string\njson_output = builder.to_json(indent=4)\n\n# Print the JSON output\nprint(json_output)\n```\n\nThis will output:\n\n```json\n{\n    \"name\": \"Ramen\",\n    \"origin\": \"Japan\",\n    \"ingredients\": [\n        { \"name\": \"Noodles\" },\n        { \"name\": \"Pork\" },\n        { \"name\": \"Eggs\" },\n        { \"name\": \"Miso\" }\n    ],\n    \"spiciness\": \"Medium\",\n    \"extraToppings\": [\n        \"Green Onions\",\n        \"Nori\",\n        \"Bamboo Shoots\"\n    ]\n}\n\n```\n\nIn this example, we create a `SpytulaBuilder` instance and add attributes like name and origin to represent `Ramen`. We use the `nodes()` context manager to create a list of ingredients and add them to the JSON structure. Optional attributes like spiciness and toppings are added conditionally using the `when()` method. Finally, we convert the JSON structure to a JSON-formatted string using `to_json()` with an indentation of 4 spaces.\n\n## Documentation    \n\nRefer for the [documentation](https://pirhoo.github.io/spytula/) for more details.",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Python library that provides a simple and convenient way to build JSON and YAML data structures using a builder pattern.",
    "version": "0.0.3",
    "project_urls": {
        "Documentation": "https://pirhoo.github.io/spytula/",
        "Homepage": "https://github.com/pirhoo/spytula/",
        "Repository": "https://github.com/pirhoo/spytula/"
    },
    "split_keywords": [
        "json",
        "yaml",
        "dsl"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d7e8599a0a449f761f5a2175599ebf5904c7eea4cf98b05b48d3f9b772e18925",
                "md5": "865c860d07b39d32b100fbc9b5f7d9b2",
                "sha256": "1e00aa4df38e348f5b17c15d0265f73e200c739649be43154fa87e51fb9c0344"
            },
            "downloads": -1,
            "filename": "spytula-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "865c860d07b39d32b100fbc9b5f7d9b2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8.1,<4.0.0",
            "size": 6328,
            "upload_time": "2023-06-20T09:15:30",
            "upload_time_iso_8601": "2023-06-20T09:15:30.211651Z",
            "url": "https://files.pythonhosted.org/packages/d7/e8/599a0a449f761f5a2175599ebf5904c7eea4cf98b05b48d3f9b772e18925/spytula-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "43c5e98bfc4bb6384e0ebfcca32ab4f1ea1a861e4ce2b6875f1cc4f4f166cc8c",
                "md5": "8ea4429d15b40ab6b7bf2a57b59e4b20",
                "sha256": "b2ae997a9f7b750779b8d7e2176621637cb2e71063b04adb34df8c5b2084d268"
            },
            "downloads": -1,
            "filename": "spytula-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "8ea4429d15b40ab6b7bf2a57b59e4b20",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8.1,<4.0.0",
            "size": 6161,
            "upload_time": "2023-06-20T09:15:32",
            "upload_time_iso_8601": "2023-06-20T09:15:32.963381Z",
            "url": "https://files.pythonhosted.org/packages/43/c5/e98bfc4bb6384e0ebfcca32ab4f1ea1a861e4ce2b6875f1cc4f4f166cc8c/spytula-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-20 09:15:32",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pirhoo",
    "github_project": "spytula",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "spytula"
}
        
Elapsed time: 0.08099s