Skip to main content

Opinionated library for handling yaml configuration files and jinja2 templates

Project description

Yamja

WARNING: This is pre-release software.

Yamja is an opinionated library for handling yaml configuration files and jinja2 templates - designed for configuration driven development.

It was created after I've realized that I'm repeating the same pattern in many projects. It's not big (around 100 lines of code) but it offers a consistent and ergonomic way to handle configuration files.

example usage:

cfg = yamja.load_config("./game_v1.yaml")
character = cfg.lookup('characters.marcus')
game_prompt = cfg.render('game_prompt', character=character)

Features

  • Load YAML configuration files
  • Use Jinja2 templates within your configuration
  • Support for nested configuration lookups using dot notation
  • Support for environmental variables overriding lookups
  • Support for jinja2 macros

Installation

pip install yamja

Usage

Basic Configuration Loading

from yamja import load_config

# Load a configuration file
config = load_config('config.yaml')

# Access values using dot notation
value = config.lookup('section.subsection.key')

# Access with default value
value = config.lookup('section.subsection.key', default='fallback')

# Access with environmental variable override
value = config.lookup('other_section.key', default=123, env_var='OTHER_KEY', cast=int)

Defaults and precedence

  • Omitting default (or passing ...): raises KeyError when the key is missing.
  • Precedence: environment variable (if present) > config value > default.
  • Casting: cast is applied to the selected source (env/config/default).

Template Rendering

# config.yaml
templates:
  greeting: "Hello {{ name }}!"
# Render a template with variables
greeting = config.render('greeting', name='World')
# more practical example
characters:
  jane:
    name: Jane Doe
    age: 30
    skills:
      - hacking
      - parkour
      - martial arts

templates:
  character_prompt: |
    <CHARACTER>
      <NAME>{{ character.name }}</NAME>
      <AGE>{{ character.age }}</AGE>
      <SKILLS>
        {% for skill in character.skills %}
          <SKILL>{{ skill }}</SKILL>
        {% endfor %}
      </SKILLS>
    </CHARACTER>
character = config.lookup('characters.jane')
character_prompt = config.render('character_prompt', character=character)

Including Other Config Files

# main.yaml
include:
  - common.yaml
  - specific.yaml

additional_settings:
  key: value

Requirements

  • Python >= 3.8
  • Jinja2
  • PyYAML

License

MIT License - see LICENSE for details.

Links

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

yamja-0.3.0.tar.gz (31.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yamja-0.3.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file yamja-0.3.0.tar.gz.

File metadata

  • Download URL: yamja-0.3.0.tar.gz
  • Upload date:
  • Size: 31.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.8

File hashes

Hashes for yamja-0.3.0.tar.gz
Algorithm Hash digest
SHA256 36fec991d6b68194c3902a71cabf585e8fd64c08489074befe27d5bb0087e40c
MD5 613d70bcb5ecef47f6eed8fd2c380df7
BLAKE2b-256 a11141e01c92c0cd5d5f0d38f5eb6d5b3ab19ab93fae25f868ca22fed474d678

See more details on using hashes here.

File details

Details for the file yamja-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: yamja-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.8

File hashes

Hashes for yamja-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe13206a5694603001a0ba5a20d15a7e1036d6b0aa6cf13c5d471092aaf920b5
MD5 fff4155b890b03cf6c60658ad5eeb056
BLAKE2b-256 9ea3c30634bcb43b9a7e3bd68d2a5311644bff24b2f3a31bb1036f7c0791188e

See more details on using hashes here.

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