Skip to main content

A library based on `PyYAML` that promises to parse YAML documents and produce Python objects, for executing Python code within YAML documents and instantiating Python objects based on YAML arguments.

Project description

InYAML

Overview

The library based on PyYAML promises to parse YAML documents and produce Python objects, for executing Python code within YAML documents and instantiating Python objects based on YAML arguments.

Features

  • Executable YAML: This introduces Python code defined within YAML documents when the code is not wrapped in the "" or ''. It supports importing modules, executing arbitrary Python expressions directly from YAML.
  • Instantiable YAML: It is not only Executable but also supports instantiating Python objects as defined by class or function tags in YAML. This allows the YAML to not just carry data but also semantic information about how the data should be instantiated into Python objects.
  • Dynamic Import and Execution: Functionality to dynamically import Python modules and execute functions as specified in the YAML, providing flexibility and powerful integration capabilities.

Key Components

ExecutableConstructor:

  • Executes Python codes or expressions within YAML.
  • Manages import (__import__ as the default key) and run (__run__ as the default key) executions through special YAML keys.
  • Ensures that only designated code is executed.

InstantiableConstructor:

  • Inherits from ExecutableConstructor.
  • Adds the capability to instantiate objects using type specifications provided in YAML tags.
  • Supports advanced features like namespace and argument passing for constructors.

load Function:

  • Parses YAML documents and uses either ExecutableConstructor or InstantiableConstructor based on whether the is_instantiable is True or not.
  • All imported modules or classes will be stores in the imported targets attribute (__import__ as the default identifier) of the outermost Namespace.

Installation

pip install inyaml

Usage Example

import inyaml

class TestClass:
    def __init__(self, *args, **kwargs):
        print(args)
        print(kwargs)
        print('TestClass')

def test_run_function(arg=0):
    print(arg)

input_data = """
add: 1+2
string: "string"
number: 3
outer_namespace: &ref
 inner_namespace:
  first: [9+ 8,{'any_number':99}]
  second: {"any_number":333}
list: [{'any_float':99.1}, 1, 2, 3+4, *ref]
__import__ (np): numpy
dict: {"any_number_in_a_dict":444}
__run__: test_run_function()
instance: !TestClass
 string: "from"
 __args__: [1,2,3]
 __kwargs__:
  first: 1
  second: 2
 number: 12345
 np_array: &ref_2 !np.array ~([1, 20, 300])
 np_array_2: !np.array [[1, 20, 300]]
 np_array_3: *ref_2
 __run__: test_run_function(25)
"""

print(inyaml.load(input_data))

Safety Notice

While this library allows execution of Python code specified in YAML, it should be used with caution to avoid executing untrusted code, especially in applications where YAML documents could come from untrusted sources.

Although it has been processed to avoid many dangerous situations, it is possible to introduce dangerous code when setting types and functions using ![any]. Please ensure that all introduced types and functions are safe.

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

inyaml-0.0.1b6.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

inyaml-0.0.1b6-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file inyaml-0.0.1b6.tar.gz.

File metadata

  • Download URL: inyaml-0.0.1b6.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.0

File hashes

Hashes for inyaml-0.0.1b6.tar.gz
Algorithm Hash digest
SHA256 e15e264e61670d8ca4435d2890eadb426cf5269f2c52cd8e6e1237d0e4a8a15b
MD5 c7bc1707fcb00dc38aaa1055dceae77f
BLAKE2b-256 4e51731348604b3ec9a22abfa232f23032e8ca7d3305a65a75f8bb88bd69d590

See more details on using hashes here.

File details

Details for the file inyaml-0.0.1b6-py3-none-any.whl.

File metadata

  • Download URL: inyaml-0.0.1b6-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.0

File hashes

Hashes for inyaml-0.0.1b6-py3-none-any.whl
Algorithm Hash digest
SHA256 7cee6b8c494f2f55e2ceac668d588e4f816325d61412a1639bad4032608f3742
MD5 c9d9b48ac6705af63909e4199e609b12
BLAKE2b-256 c36321a1d497a41c60ada67b6d2fdd09a3c2a0b4b6c6abcb34372fcedaa7e008

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