Skip to content

Commit

Permalink
Combo converter doc (#735)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwardCuiPeacock committed Apr 12, 2022
1 parent 00ef8a3 commit 68636ee
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions docs/dynamic.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ from dynaconf import settings
settings.DB_PATH == '~/DEVELOPMENT/calculator/mydb.db'
```

`@format` token can be used together with tokens like `@str`, `@int`, `@float`, `@bool`, and `@json` to cast the results parsed by `@format`.

Example:

Casting to integer from `@format` templated values

```yaml
NUM_GPUS: 4
# This returns the integer after parsing
FOO_INT: "@int @format {this.NUM_GPUS}"
# This returns the string after parsing
FOO_STR: "@format {this.NUM_GPUS}"
```

### @jinja token

If `jinja2` package is installed then dynaconf will also allow the use jinja to render string values.
Expand Down Expand Up @@ -75,3 +89,44 @@ settings.DB_PATH == '~/development/calculator/mydb.db'
The main difference is that Jinja allows some Python expressions to be evaluated such as `{% for, if, while %}` and also supports calling methods and has lots of filters like `| lower`.

Jinja supports its built-in filters listed in [Builtin Filters Page](http://jinja.palletsprojects.com/en/master/templates/#builtin-filters) and Dynaconf includes additional filters for `os.path` module: `abspath`. `realpath`, `relpath`, `basename` and `dirname` and usage is like: `VALUE = "@jinja {{this.FOO | abspath}}"`

`@jinja` token can be used together with tokens like `@str`, `@int`, `@float`, `@bool`, and `@json` to cast the results parsed by `@jinja`.

Example:

Casting to integer from `@jinja` templated values

```yaml
NUM_GPUS: 4
# This returns the integer after parsing
FOO_INT: "@int @jinja {{ this.NUM_GPUS }}"
# This returns the string after parsing
FOO_STR: "@jinja {{ this.NUM_GPUS }}"
```

Example:

Casting to a `json` dict from `@jinja` templated values

```yaml
MODEL_1:
MODEL_PATH: "src.main.models.CNNModel"
GPU_COUNT: 4
OPTIMIZER_KWARGS:
learning_rate: 0.002

MODEL_2:
MODEL_PATH: "src.main.models.VGGModel"
GPU_COUNT: 2
OPTIMIZER_KWARGS:
learning_rate: 0.001

# Flag to choose which model to use
MODEL_TYPE: "MODEL_2"

# This returns the dict loaded from the resulting json
MODEL_SETTINGS_DICT: "@json @jinja {{ this|attr(this.MODEL_TYPE) }}"

# This returns the raw json string
MODEL_SETTINGS_STR: "@jinja {{ this|attr(this.MODEL_TYPE) }}"
```

0 comments on commit 68636ee

Please sign in to comment.