https://github.com/casey/starstar
** for the win
https://github.com/casey/starstar
Last synced: 8 months ago
JSON representation
** for the win
- Host: GitHub
- URL: https://github.com/casey/starstar
- Owner: casey
- Created: 2016-09-24T02:26:57.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-09-25T03:06:10.000Z (almost 10 years ago)
- Last Synced: 2025-02-14T01:18:26.538Z (over 1 year ago)
- Language: Python
- Size: 290 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
starstar
--------
This repo contains a simple example of a pattern in python that I've found useful when I need to load a config file and do something with the data it contains.
The pattern is simple:
1. Load the config file off of disk
2. Parse it into python objects using the `**` operator and list comprehensions
3. Do something with the python objects
In this example the config file is a YAML file, although it could just as easily be JSON, TOML, or a CSV, and we use it to instantiate python objects which then serve as the context for a jinja2 markdown template. This might also be useful for parsing a config file that contains program settings.
There are a number of advantages to this approach:
- It is resistant to bugs caused by misspellings in the config file. If you misspell an item in the config file, the corresponding call to `__init__` will fail with a helpful error message.
- If you misspell a property name in a template you'll get an error message which has the class name in it, instead of a generic dictionary key error.
- It is concise and appealing.
Files:
- `main` is the code. Start reading here
- `README.md` is this readme
- `config.yaml` is the YAML config file
- `template.md` is the jinja2 markdown template
- `output.md` is the rendered template
- `yaml` is a copy of the PyYAML YAML library
- `jinja2` is a copy of the Jinja2 template engine