Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add @jinja and @format casting #704

Merged
merged 20 commits into from Jan 29, 2022
Merged

Feature: add @jinja and @format casting #704

merged 20 commits into from Jan 29, 2022

Conversation

EdwardCuiPeacock
Copy link
Contributor

@EdwardCuiPeacock EdwardCuiPeacock commented Jan 18, 2022

Adding the capability of @jinja and @format templated values to be type-casted

Related to issue: #642

  • Implemented the following casting cases that are compatible with @jinja and @format:
    • @str: casting parsed results to string
    • @int: casting parsed results to integer
    • @float: casting parsed results to float
    • @bool: casting parsed results to boolean
    • @json: casting parsed results to dictionary
  • Updated Lazy class under utils/parse_conf.py
    • Added the casting argument
    • Adding the method set_casting(self, casting)
  • Added the unit test for the above casting.

Examples

Casting to integer from Jinja templated values

NUM_GPUS: 4
# This returns the integer after parsing
FOO: "@int @jinja {{ this.NUM_GPUS }}"
# This returns the string after parsing
FOO: "@jinja {{ this.NUM_GPUS }}"

Casting to a json dict from Jinja templated values

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: "@json @jinja {{ this|attr(this.MODEL_TYPE) }}"

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

@pep8speaks
Copy link

pep8speaks commented Jan 18, 2022

Hello @EdwardCuiPeacock! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2022-01-29 17:05:04 UTC

@EdwardCuiPeacock EdwardCuiPeacock changed the title Feature/add jinja casting Feature: add jinja casting Jan 18, 2022
@EdwardCuiPeacock EdwardCuiPeacock changed the title Feature: add jinja casting Feature: add @jinja and @format casting Jan 21, 2022
@EdwardCuiPeacock
Copy link
Contributor Author

EdwardCuiPeacock commented Jan 24, 2022

@rochacbruno any idea how to fix this error in the CI/CD pipeline? What am I missing?

make: *** [Makefile:192: run-pre-commit] Error 1
Error: Process completed with exit code 2.

Update: used black matching the version and args in the .pre-commit-config.yaml and reformatted parse_conf.py. Not sure if this would fix the problem.

@codecov
Copy link

codecov bot commented Jan 29, 2022

Codecov Report

Merging #704 (4c77cdd) into master (0bca1d0) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #704   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           21        21           
  Lines         1671      1684   +13     
=========================================
+ Hits          1671      1684   +13     
Impacted Files Coverage Δ
dynaconf/utils/parse_conf.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0bca1d0...4c77cdd. Read the comment docs.

@rochacbruno rochacbruno merged commit db1ceab into dynaconf:master Jan 29, 2022
rochacbruno added a commit that referenced this pull request Apr 15, 2022
Shortlog of commits since last release:

    Anderson Sousa (1):
          Document the usage with python -m (#710)

    Andressa Cabistani (2):
          Add unique label when merging lists to fix issue #653 (#661)
          Add new validation to fix issue #585 (#667)

    Armin Berres (1):
          Fix typo in error message

    Bruno Rocha (7):
          Release version 3.1.7
          Found this bug that was duplicating the generated envlist (#663)
          Add support for Python 3.10 (#665)
          Attempt to fix #555 (#669)
          Create update_contributors.yml
          Fixing pre-coomit and docs CI
          Added `dynaconf get` command to cli (#730)

    Caneco (2):
          improvement: add brand new logo to the project (#686)
          improvement: update socialcard to match the python way (#687)

    EdwardCuiPeacock (2):
          Feature: add @Jinja and @Format casting (#704)
          Combo converter doc (#735)

    Eitan Mosenkis (1):
          Fix FlaskConfig.setdefault (#706)

    Enderson Menezes (Mr. Enderson) (2):
          Force PYTHONIOENCODING to utf-8 to fix #664 (#672)
          edit: move discussions to github tab (#682)

    Eugene Triguba (1):
          Fix custom prefix link in envvar documentation (#680)

    Gibran Herrera (1):
          Fix Issue 662 Lazy validation (#675)

    Jitendra Yejare (2):
          Load vault secrets from environment less stores or which are not written by dynaconf (#725)
          Use default value when settings is blank (#729)

    Pavel Alimpiev (1):
          Update docs link (#678)

    Ugo Benassayag (1):
          Added validate_only_current_env to validator (issue #734) (#736)

    Waylon Walker (1):
          Docs Fix Spelling (#696)

    dependabot[bot] (3):
          Bump django from 2.1.5 to 2.2.26 in /example/django_pytest_pure (#711)
          Bump mkdocs from 1.1.2 to 1.2.3 (#715)
          Bump django from 2.2.26 to 2.2.27 in /example/django_pytest_pure (#717)

    github-actions[bot] (2):
          [automated] Update Contributors File (#691)
          [automated] Update Contributors File (#732)

    lowercase00 (1):
          Makes Django/Flask kwargs case insensitive (#721)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants