From b4cd9bfb7bbe6f36b7909448266e25c6a94afca4 Mon Sep 17 00:00:00 2001 From: Maximilian Pensel Date: Thu, 18 Feb 2021 10:36:06 +0100 Subject: [PATCH 1/3] Add logging example code for when create_cli==no Sensitive to logging option (yaml/hocon) Add cell in example.ipynb and sample code in main.py --- .../notebooks/example.ipynb | 23 ++++++++++++++++++- .../src/{{cookiecutter.module_name}}/main.py | 16 ++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/{{cookiecutter.project_slug}}/notebooks/example.ipynb b/{{cookiecutter.project_slug}}/notebooks/example.ipynb index 1137af1..b1ae923 100644 --- a/{{cookiecutter.project_slug}}/notebooks/example.ipynb +++ b/{{cookiecutter.project_slug}}/notebooks/example.ipynb @@ -10,7 +10,28 @@ "To use your module `{{ cookiecutter.module_name }}` from here, run `pip install -e .` from the root of your project directory." {% endif %} ] - }, + },{% if cookiecutter.config_file != 'none' %} + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + {% if cookiecutter.config_file == 'yaml' %}"import os\n", + {% endif %}"import logging\n", + "from {{ cookiecutter.module_name }} import util\n", + "\n", + "logger = logging.getLogger(\"{{ cookiecutter.module_name }}\")\n", + "\n", + "config = util.load_config({% if cookiecutter.config_file == 'yaml' %}os.path.join(os.path.abspath(os.pardir),\n", + " \"config\",\n", + " \"config.yml\"){% endif %})\n", + "util.logging_setup(config)\n", + "\n", + "logger.info(\"Use this setup to start logging in notebooks or to \"+\n", + " \"get the correct logging format in your {{ cookiecutter.module_name }} module\")" + ] + },{% endif %} { "cell_type": "code", "execution_count": null, diff --git a/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main.py b/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main.py index 4d27bd2..eff7d0c 100644 --- a/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main.py +++ b/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main.py @@ -1,6 +1,20 @@ +{% if cookiecutter.config_file != 'none' %}{% if cookiecutter.config_file == 'yaml' %}import os +{% endif %}import logging +from {{ cookiecutter.module_name }} import util +logger = logging.getLogger("{{ cookiecutter.module_name }}") + +{% endif %} def main(): - # TODO your journey starts here + {% if cookiecutter.config_file != 'none' %}config = util.load_config({% if cookiecutter.config_file == 'yaml' %}os.path.join(os.path.abspath(__file__), + *[os.pardir]*3, + "config", + "config.yml"){% endif %}) + util.logging_setup(config) + + logger.info("Looks like you're all set up. Let's get going!") + + {% endif %}# TODO your journey starts here print("hello :)") From 613cb180660e92fbe224200ed616a4e5c740b2cd Mon Sep 17 00:00:00 2001 From: Sebastian Straub Date: Fri, 26 Feb 2021 16:26:49 +0100 Subject: [PATCH 2/3] exclude python code because of jinja templates --- .pre-commit-config.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4195077..64f6772 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,8 +4,11 @@ repos: hooks: - id: check-added-large-files - id: check-ast + # exclude because of jinja templates + exclude: \{\{cookiecutter.project_slug\}\}/.+\.py - id: check-merge-conflict - id: check-yaml + # exclude because of jinja templates exclude: \{\{cookiecutter.project_slug\}\}/.gitlab-ci.yml - id: end-of-file-fixer - id: mixed-line-ending From 5d63395c715ccf3577e737ef9af35d11f927b622 Mon Sep 17 00:00:00 2001 From: Sebastian Straub Date: Fri, 26 Feb 2021 16:27:31 +0100 Subject: [PATCH 3/3] simplify main.py --- .../src/{{cookiecutter.module_name}}/main.py | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main.py b/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main.py index eff7d0c..c6532ef 100644 --- a/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main.py +++ b/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main.py @@ -1,21 +1,19 @@ -{% if cookiecutter.config_file != 'none' %}{% if cookiecutter.config_file == 'yaml' %}import os -{% endif %}import logging +import logging + from {{ cookiecutter.module_name }} import util -logger = logging.getLogger("{{ cookiecutter.module_name }}") +logger = logging.getLogger('{{ cookiecutter.module_name }}') + -{% endif %} def main(): - {% if cookiecutter.config_file != 'none' %}config = util.load_config({% if cookiecutter.config_file == 'yaml' %}os.path.join(os.path.abspath(__file__), - *[os.pardir]*3, - "config", - "config.yml"){% endif %}) + {% if cookiecutter.config_file != 'none' -%} + config = util.load_config({% if cookiecutter.config_file == 'yaml' %}'config/config.yml'{% else %}None{% endif %}) util.logging_setup(config) - + {% else -%} + logging.basicConfig(level=logging.INFO) + {% endif -%} logger.info("Looks like you're all set up. Let's get going!") - - {% endif %}# TODO your journey starts here - print("hello :)") + # TODO your journey starts here if __name__ == "__main__":