### Install pyyaml-include Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Installs the pyyaml-include package using pip. For remote file support, additional fsspec dependencies need to be installed. ```bash pip install "pyyaml-include" ``` ```bash pip install "pyyaml-include" fsspec[http] ``` ```bash pip install "pyyaml-include" fsspec[s3] ``` -------------------------------- ### Install Dependencies for Documentation Build Source: https://github.com/tanbro/pyyaml-include/blob/main/docs/README.rst Installs the project and its documentation dependencies in editable mode. This step ensures all necessary packages, including Sphinx and its extensions, are available for building the documentation. ```sh pip install -e . --group docs ``` ```sh uv sync --group docs ``` -------------------------------- ### Loading YAML with HTTP Include Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Example of a YAML file using the registered `!http-include` tag. When loaded, pyyaml-include constructs the full URL by combining the base URL, base directory, and the path specified in the tag. ```yaml xyz: !http-include xyz.yml ``` -------------------------------- ### Dumping and Loading YAML with Serialized Includes Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Continues the serialization example by defining YAML with include statements, loading it (which defers actual file loading), dumping it back to a string, and then de-serializing it. The de-serialization process can re-enable auto-loading. ```python yaml_str = """ - !inc include.d/1.yaml - !inc include.d/2.yaml """ d0 = yaml.load(yaml_str, yaml.Loader) s = yaml.dump(d0) print(s) ctor.autoload = True d1 = yaml.load(s, yaml.Loader) d2 = yaml_include.load(d0) ``` -------------------------------- ### YAML include with wildcard and mapping glob/open parameters Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Illustrates using a mapping format for glob and open parameters, specifying keys like 'glob' and 'open' for clarity. ```yaml files: !inc {urlpath: "etc/app/**/*.yml", glob: [!!int "2"], open: {encoding: utf16}} ``` -------------------------------- ### Serve Built Documentation Locally Source: https://github.com/tanbro/pyyaml-include/blob/main/docs/README.rst Serves the generated static HTML documentation using Python's built-in HTTP server. This allows for local previewing of the documentation. The server can be configured to use a different port if the default port 8000 is in use. ```sh python -m http.server -d docs/_build/html ``` ```sh python -m http.server -d docs/_build/html 8080 ``` -------------------------------- ### YAML include with fsspec.open and named args Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Illustrates a YAML snippet using a file scheme and named arguments, which maps to a Python call with keyword arguments for fsspec.open. ```yaml files: !inc {urlpath: file:///foo/baz.yaml, encoding: utf16} ``` -------------------------------- ### YAML include with wildcard and glob/open parameters Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Demonstrates including files with wildcards using sequence parameters for glob and open methods, including maxdepth and encoding. ```yaml files: !inc ["etc/app/**/*.yml", {maxdepth: !!int "2"}, {encoding: utf16}] ``` -------------------------------- ### Generate API Documentation with Sphinx Source: https://github.com/tanbro/pyyaml-include/blob/main/docs/README.rst Generates API documentation for the project. This command is typically run when the source tree changes or if the documentation directory needs to be reset. It creates the necessary files in the 'docs/apidoc' directory. ```sh sphinx-apidoc -H "" -feo docs/apidoc src ``` -------------------------------- ### Advanced Wildcard Parameters and File Opening Modes Source: https://context7.com/tanbro/pyyaml-include/llms.txt Demonstrates using advanced wildcard parameters and file opening modes with PyYAML Include, allowing control over glob search depth and file encoding. ```python import yaml import yaml_include yaml.add_constructor("!inc", yaml_include.Constructor(base_dir="configs")) yaml_string = """ # Positional form: [path, glob_params, open_params] configs1: !inc ["app/**/*.yml", {maxdepth: 2}, {encoding: utf-16}] # Named form with separate glob and open parameters configs2: !inc { urlpath: "app/**/*.yml", glob: {maxdepth: 2}, open: {encoding: utf-16} } # Simple form with just maxdepth configs3: !inc ["services/**/*.yml", 1] """ config = yaml.load(yaml_string, yaml.Loader) # Equivalent to: # for file in fs.glob("app/**/*.yml", maxdepth=2): # with fs.open(file, encoding="utf-16") as f: # yaml.load(f, Loader) ``` -------------------------------- ### Configure Constructor for HTTP Includes (Python) Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Shows the Python code to instantiate a pyyaml-include Constructor with an fsspec HTTP filesystem. This allows the constructor to resolve includes from HTTP locations, specifying a base URL for relative path resolution. ```python import yaml import fsspec import yaml_include http_fs = fsspec.filesystem("http", client_kwargs={"base_url": f"http://{HOST}:{PORT}"}) ctor = yaml_include.Constructor(fs=http_fs, base_dir="/foo/baz") yaml.add_constructor("!inc", ctor, yaml.Loader) ``` -------------------------------- ### YAML include with fsspec.open and positional args Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Shows how a YAML snippet with a file scheme and positional arguments translates to a Python call using fsspec.open. ```yaml files: !inc [file:///foo/baz.yaml, r] ``` -------------------------------- ### Build HTML Documentation with Sphinx Source: https://github.com/tanbro/pyyaml-include/blob/main/docs/README.rst Builds the HTML version of the project documentation. This can be done using the Make tool on Unix-like systems or directly via a command on Windows. The output is placed in the 'docs/_build/html' directory. ```sh make -C docs html ``` ```bat docs\make html ``` -------------------------------- ### Dynamic Base Directory for Runtime Path Resolution Source: https://context7.com/tanbro/pyyaml-include/llms.txt Demonstrates using a callable function as the base directory for PyYAML Include, enabling dynamic path resolution at runtime based on environment variables. ```python import os import yaml import yaml_include # Base directory determined at runtime def get_config_dir(): env = os.getenv("ENVIRONMENT", "development") return f"/etc/app/config/{env}" ctor = yaml_include.Constructor(base_dir=get_config_dir) yaml.add_constructor("!inc", ctor, yaml.Loader) yaml_string = """ database: !inc database.yml """ # Base directory evaluated when loading os.environ["ENVIRONMENT"] = "production" prod_config = yaml.load(yaml_string, yaml.Loader) # Loads from /etc/app/config/production/database.yml os.environ["ENVIRONMENT"] = "staging" stage_config = yaml.load(yaml_string, yaml.Loader) # Loads from /etc/app/config/staging/database.yml ``` -------------------------------- ### Python equivalent for fsspec.open with positional args Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md The Python code generated from the YAML snippet above, demonstrating the use of fsspec.open with a mode argument. ```python with fsspec.open("file:///foo/baz.yaml", "r") as f: yaml.load(f, Loader) ``` -------------------------------- ### Registering HTTP Include Constructor with PyYAML Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Demonstrates how to register a custom constructor for HTTP includes using PyYAML and fsspec. It shows how to specify a base URL and a base directory for resolving relative paths within the included YAML files. Parameters passed to the `!http-include` tag are forwarded to the fsspec `open` method. ```python import yaml import fsspec import yaml_include yaml.add_constructor( "!http-include", yaml_include.Constructor( fsspec.filesystem("http", client_kwargs={"base_url": f"http://{HOST}:{PORT}"}), base_dir="/sub_1/sub_1_1" ) ) ``` -------------------------------- ### Python equivalent for wildcard include with glob/open parameters Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md The Python code generated for the YAML snippet with wildcards and explicit glob/open parameters, showing iteration and file opening. ```python for file in local_fs.glob("etc/app/**/*.yml", maxdepth=2): with local_fs.open(file, encoding="utf16") as f: yaml.load(f, Loader) ``` -------------------------------- ### S3 Cloud Storage Inclusion with pyyaml-include and fsspec Source: https://context7.com/tanbro/pyyaml-include/llms.txt Demonstrates including configuration files stored in AWS S3 buckets using fsspec. Requires AWS credentials and specifies the bucket and path. Supports compressed files and wildcard matching. ```python import yaml import fsspec import yaml_include # Create S3 filesystem s3_fs = fsspec.filesystem( "s3", key="ACCESS_KEY", secret="SECRET_KEY" ) ctor = yaml_include.Constructor(fs=s3_fs, base_dir="my-bucket/configs") yaml.add_constructor("!inc", ctor, yaml.Loader) yaml_string = """ # Include single compressed file from S3 database: !inc {urlpath: s3://my-bucket/db.yml.gz, compression: gzip} # Include multiple files with wildcard services: !inc {urlpath: s3://my-bucket/services/*.yml} """ config = yaml.load(yaml_string, yaml.Loader) ``` -------------------------------- ### Basic Local File Inclusion with pyyaml-include Source: https://context7.com/tanbro/pyyaml-include/llms.txt Demonstrates including a single YAML file from the local filesystem using the `!inc` tag. The Constructor needs to be registered with a base directory. The included file's content replaces the tag in the loaded YAML. ```python import yaml import yaml_include # Register the constructor with base directory yaml.add_constructor("!inc", yaml_include.Constructor(base_dir="/etc/app/config")) # YAML document with include statement yaml_string = """ database: !inc db/postgres.yml cache: !inc cache/redis.yml """ # Load YAML - included files are automatically parsed with open("config.yml") as f: config = yaml.full_load(f) # Result: {'database': {...contents of db/postgres.yml...}, # 'cache': {...contents of cache/redis.yml...}} ``` -------------------------------- ### Include YAML Files using Wildcards Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Shows how to use shell-style wildcards within the '!inc' tag to include multiple YAML files. The content from all matched files will be collected into a sequence (list). ```yaml files: !inc include.d/*.yml ``` -------------------------------- ### Serialization and Lazy Loading with PyYAML Include Source: https://context7.com/tanbro/pyyaml-include/llms.txt Illustrates how to serialize include statements without immediate loading and then load them on demand using PyYAML Include's autoload and representer features. ```python import yaml import yaml_include # Create constructor with autoload disabled ctor = yaml_include.Constructor(base_dir="config", autoload=False) yaml.add_constructor("!inc", ctor) # Add representer for serialization rpr = yaml_include.Representer("inc") yaml.add_representer(yaml_include.Data, rpr) yaml_string = """ database: !inc db.yml cache: !inc cache.yml """ # Load without opening files - returns Data objects data_objects = yaml.load(yaml_string, yaml.Loader) # Result: {'database': Data(urlpath='db.yml'), # 'cache': Data(urlpath='cache.yml')} # Serialize back to YAML string serialized = yaml.dump(data_objects) # Result: "database: !inc 'db.yml'\ncache: !inc 'cache.yml'\n" # Now actually load the files ctor.autoload = True config = yaml.load(serialized, yaml.Loader) # Or use the load function directly config = yaml_include.load(data_objects, yaml.Loader, ctor) ``` -------------------------------- ### YAML include with named parameters Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Illustrates passing parameters as a mapping after the !inc tag, which are then used as keyword arguments (**kwargs) in Python. ```yaml files: !inc {urlpath: /foo/baz.yaml, encoding: utf16} ``` -------------------------------- ### YAML include with positional parameters Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Demonstrates using a sequence of parameters after the !inc tag, which are passed as positional arguments (*args) to the underlying Python function. ```yaml files: !inc [include.d/**/*.yaml, {maxdepth: 1}, {encoding: utf16}] ``` -------------------------------- ### Wildcard Pattern Matching with pyyaml-include Source: https://context7.com/tanbro/pyyaml-include/llms.txt Shows how to include multiple files matching a glob pattern into a sequence. Supports basic glob patterns and recursive glob patterns with an optional maximum depth limit. ```python import yaml import yaml_include yaml.add_constructor("!inc", yaml_include.Constructor(base_dir="config")) yaml_string = """ # Include all YAML files in plugins directory plugins: !inc plugins/*.yml # Include all YAML files recursively with depth limit services: !inc [services/**/*.yml, {maxdepth: 2}] """ config = yaml.load(yaml_string, yaml.Loader) # Result: {'plugins': [{...plugin1...}, {...plugin2...}, {...plugin3...}], # 'services': [{...service1...}, {...service2...}]} ``` -------------------------------- ### YAML include with wildcard and positional glob parameter Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Shows an alternative YAML syntax for wildcards where the glob parameter is provided positionally, without explicit naming. ```yaml files: !inc ["etc/app/**/*.yml", [!!int "2"]] ``` -------------------------------- ### Python equivalent for fsspec.open with named args Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md The Python code corresponding to the YAML snippet above, showing fsspec.open being called with an encoding keyword argument. ```python with fsspec.open("file:///foo/baz.yaml", encoding="utf16") as f: yaml.load(f, Loader) ``` -------------------------------- ### Include HTTP File in YAML Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Demonstrates how to include a YAML file hosted on an HTTP server directly within another YAML document. This requires setting up a pyyaml Constructor with an fsspec HTTP filesystem. ```yaml conf: logging: !inc http://domain/etc/app/conf.d/logging.yml ``` -------------------------------- ### Remote HTTP File Inclusion with pyyaml-include and fsspec Source: https://context7.com/tanbro/pyyaml-include/llms.txt Illustrates including YAML files from remote HTTP servers using fsspec. A Constructor is registered with an HTTP filesystem instance, allowing relative paths from a base URL or absolute URLs. ```python import yaml import fsspec import yaml_include # Create HTTP filesystem with base URL http_fs = fsspec.filesystem( "http", client_kwargs={"base_url": "https://config.example.com"} ) # Register constructor with HTTP filesystem ctor = yaml_include.Constructor(fs=http_fs, base_dir="/v1/configs") yaml.add_constructor("!inc", ctor, yaml.Loader) yaml_string = """ # Relative paths use base_dir production: !inc production/app.yml # Absolute URL paths shared: !inc /shared/common.yml # Full URLs bypass base_dir external: !inc https://another-server.com/config.yml """ config = yaml.load(yaml_string, yaml.Loader) # Files fetched from: # - https://config.example.com/v1/configs/production/app.yml # - https://config.example.com/shared/common.yml # - https://another-server.com/config.yml ``` -------------------------------- ### Managing Autoload Temporarily with a Context Manager Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Demonstrates the use of the `managed_autoload` context manager to temporarily disable or enable the `autoload` feature of the Constructor. This allows for fine-grained control over when included files are loaded within specific blocks of code. ```python ctor = yaml_include.Constructor() with ctor.managed_autoload(False): yaml.full_load(YAML_TEXT) # autoload restores True automatically ``` -------------------------------- ### Register and Use !inc Constructor with PyYAML Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Demonstrates how to register the yaml_include.Constructor with PyYAML's loader and use the '!inc' tag in a YAML file to include other YAML files. This process involves adding the constructor to the loader and then loading the main YAML file. ```python import yaml import yaml_include # add the tag yaml.add_constructor("!inc", yaml_include.Constructor(base_dir='/your/conf/dir')) with open('0.yml') as f: data = yaml.full_load(f) print(data) # (optional) unregister the constructor del yaml.Loader.yaml_constructors["!inc"] del yaml.UnSafeLoader.yaml_constructors["!inc"] del yaml.FullLoader.yaml_constructors["!inc"] ``` -------------------------------- ### Python equivalent for wildcard include with positional glob parameter Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md The Python code generated for the YAML snippet using a positional argument for the glob method, with default open parameters. ```python for file in local_fs.glob("etc/app/**/*.yml", 2): with local_fs.open(file) as f: yaml.load(f, Loader) ``` -------------------------------- ### Loading YAML with HTTP Includes (Python) Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Provides the Python code snippet for loading a YAML string that contains '!inc' tags. The 'yaml.load' function, configured with the custom constructor, will resolve the includes according to the fsspec configuration. ```python yaml.load(yaml_string, yaml.Loader) ``` -------------------------------- ### Nested Include Statements with PyYAML Include Source: https://context7.com/tanbro/pyyaml-include/llms.txt Explains how to handle nested include statements where a file being included also contains its own include directives, using the `nested=True` option. ```python import yaml import yaml_include ctor = yaml_include.Constructor(base_dir="config", autoload=False) yaml.add_constructor("!inc", ctor) # Main config with include main_yaml = """ app: !inc app.yml """ # app.yml also has includes # app.yml content: # name: MyApp # database: !inc database.yml # cache: !inc cache.yml # Load without auto-loading data = yaml.load(main_yaml, yaml.Loader) # Recursively load with nested=True config = yaml_include.load( data, yaml.Loader, ctor, nested=True ) # All nested includes are resolved recursively ``` -------------------------------- ### Default File Inclusion (Python) Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Demonstrates the default behavior of pyyaml-include when no specific fsspec filesystem is provided. It automatically uses the local file system, equivalent to explicitly setting fs=fsspec.filesystem('file'). ```python yaml.add_constructor("!inc", Constructor()) # is equivalent to: yaml.add_constructor("!inc", Constructor(fs=fsspec.filesystem("file"))) ``` -------------------------------- ### Register Include Constructor for Multiple PyYAML Loaders Source: https://context7.com/tanbro/pyyaml-include/llms.txt Register the include constructor (`!inc`) for various PyYAML loader types (Loader, FullLoader, SafeLoader, UnsafeLoader) to ensure consistent handling of included files across different loading strategies. Includes cleanup of the registered constructor. ```python import yaml import yaml_include # Create single constructor instance ctor = yaml_include.Constructor(base_dir="config") # Register with all loader types yaml.add_constructor("!inc", ctor, yaml.Loader) yaml.add_constructor("!inc", ctor, yaml.FullLoader) yaml.add_constructor("!inc", ctor, yaml.SafeLoader) yaml.add_constructor("!inc", ctor, yaml.UnsafeLoader) yaml_string = """ config: !inc app.yml """ # Works with any loader config1 = yaml.load(yaml_string, yaml.Loader) config2 = yaml.load(yaml_string, yaml.FullLoader) config3 = yaml.load(yaml_string, yaml.SafeLoader) # Clean up - unregister constructor del yaml.Loader.yaml_constructors["!inc"] del yaml.FullLoader.yaml_constructors["!inc"] del yaml.SafeLoader.yaml_constructors["!inc"] ``` -------------------------------- ### Default File Inclusion in YAML Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Shows the syntax for including a file using the '!inc' tag when pyyaml-include is configured to use the default local file system. The path is resolved relative to the current working directory or a specified base_dir. ```yaml data: !inc: foo/baz.yaml ``` -------------------------------- ### Flattening Sequences from Multiple Included Files Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Illustrates how to load multiple YAML files matching a glob pattern (`*.yaml`) into a single sequence. The `flatten: true` option merges the top-level sequences from each matched file into a single, flat list. ```yaml items: !include "*.yaml" ``` ```yaml items: !include {urlpath: "*.yaml", flatten: true} ``` -------------------------------- ### Custom Loader for Multiple Formats Source: https://context7.com/tanbro/pyyaml-include/llms.txt Defines a custom loader that can parse different file formats (JSON, TOML, YAML) based on file extension, registering it with PyYAML for the '!inc' tag. ```python import json import tomllib import yaml import yaml_include def multi_format_loader(urlpath, file, Loader): if urlpath.endswith(".json"): return json.load(file) elif urlpath.endswith(".toml"): return tomllib.load(file) else: # Default to YAML return yaml.load(file, Loader) # Register constructor with custom loader ctor = yaml_include.Constructor( base_dir="config", custom_loader=multi_format_loader ) yaml.add_constructor("!inc", ctor, yaml.Loader) yaml_string = """ database: !inc database.json features: !inc features.toml logging: !inc logging.yml """ config = yaml.load(yaml_string, yaml.Loader) # Each file is parsed with appropriate format parser ``` -------------------------------- ### YAML Structure with HTTP Includes (Python) Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Illustrates the structure of a YAML file that uses the '!inc' tag to include content from various locations relative to a configured base directory and an HTTP base URL. Paths can be relative, absolute, or traverse directories. ```yaml key1: !inc doc1.yml # relative path to "base_dir" key2: !inc ./doc2.yml # relative path to "base_dir" also key3: !inc /doc3.yml # absolute path, "base_dir" does not affect key3: !inc ../doc4.yml # relative path one level upper to "base_dir" ``` -------------------------------- ### Serializing YAML with Include Statements Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Shows how to configure pyyaml-include to serialize include statements instead of loading the included content. This involves creating a Constructor with `autoload=False` and adding a corresponding Representer. The included files are parsed into `yaml_include.Data` objects, which are then serialized back into `!inc` tags. ```python import yaml import yaml_include ctor = yaml_include.Constructor(autoload=False) yaml.add_constructor("!inc", ctor) rpr = yaml_include.Representer("inc") yaml.add_representer(yaml_include.Data, rpr) ``` -------------------------------- ### YAML include with explicit integer type for parameter Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md Demonstrates using the !!int tag in YAML to ensure a parameter is interpreted as an integer, preventing potential type errors from PyYAML. ```yaml files: !inc ["etc/app/**/*.yml", open: {intParam: !!int 1}] ``` -------------------------------- ### Sequence Flattening with pyyaml-include Source: https://context7.com/tanbro/pyyaml-include/llms.txt Explains and demonstrates sequence flattening when including multiple files that contain YAML sequences. By default, nested includes result in a 2D array; `flatten: true` creates a single, flat 1D array. ```python import yaml import yaml_include yaml.add_constructor("!inc", yaml_include.Constructor(base_dir="data")) # Without flattening (default) - creates 2D array yaml_2d = """ items: !inc lists/*.yml """ # Assuming each file contains: [item1, item2, item3] # Result: items = [[file1_item1, file1_item2, file1_item3], # [file2_item1, file2_item2, file2_item3]] # With flattening - creates flat 1D array yaml_flat = """ items: !inc {urlpath: "lists/*.yml", flatten: true} """ # Result: items = [file1_item1, file1_item2, file1_item3, # file2_item1, file2_item2, file2_item3] config_2d = yaml.load(yaml_2d, yaml.Loader) config_flat = yaml.load(yaml_flat, yaml.Loader) ``` -------------------------------- ### Lazy Load Large Configurations with Generator Source: https://context7.com/tanbro/pyyaml-include/llms.txt Process large YAML configurations efficiently using a generator to load includes one at a time, reducing memory overhead. This is ideal for very large configuration files. ```python import yaml import yaml_include ctor = yaml_include.Constructor(base_dir="config", autoload=False) yaml.add_constructor("!inc", ctor) yaml_string = """ services: - !inc service1.yml - !inc service2.yml - !inc service3.yml """ # Load structure without files data = yaml.load(yaml_string, yaml.Loader) # Process includes one at a time with generator for service_config in yaml_include.lazy_load(data, yaml.Loader, ctor): print(f"Loaded service: {service_config}") # Process each service as it's loaded # Memory efficient for large configurations ``` -------------------------------- ### Temporary Autoload Context Manager Source: https://context7.com/tanbro/pyyaml-include/llms.txt Shows how to temporarily disable autoloading within a specific code block using the `managed_autoload` context manager provided by PyYAML Include's constructor. ```python import yaml import yaml_include ctor = yaml_include.Constructor(base_dir="config") yaml.add_constructor("!inc", ctor) yaml.add_representer(yaml_include.Data, yaml_include.Representer("inc")) yaml_string = """ settings: !inc settings.yml """ # Normally autoload is True config1 = yaml.load(yaml_string, yaml.Loader) # settings.yml is loaded # Temporarily disable autoloading with ctor.managed_autoload(False): data_objects = yaml.load(yaml_string, yaml.Loader) # Returns Data objects without loading files # Autoload restored to True automatically config2 = yaml.load(yaml_string, yaml.Loader) # settings.yml is loaded again ``` -------------------------------- ### Custom Loader for JSON, TOML, and YAML Inclusion in Python Source: https://github.com/tanbro/pyyaml-include/blob/main/README.md This Python code defines a custom loader function `my_loader` that can parse JSON, TOML, and YAML files. It's then integrated with `pyyaml-include` to allow these file types to be included in a YAML document using the `!inc` tag. This approach enhances flexibility by supporting multiple data serialization formats. ```python import json import tomllib as toml import yaml import yaml_include # Define loader function def my_loader(urlpath, file, Loader): if urlpath.endswith(".json"): return json.load(file) if urlpath.endswith(".toml"): return toml.load(file) return yaml.load(file, Loader) # Create the include constructor, with the custom loader ctor = yaml_include.Constructor(custom_loader=my_loader) # Add the constructor to YAML Loader yaml.add_constructor("!inc", ctor, yaml.Loader) # Then, json files will can be loaded by std-lib's json module, and the same to toml files. s = """ json: !inc "*.json" toml: !inc "*.toml" yaml: !inc "*.yaml" """ yaml.load(s, yaml.Loader) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.