-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve template (see detailed commit message)
Improve the Cookiecutter template in the following ways: - Make hello function echo the input - Add test for echoing the input - Add support for Python Fire - Add workaround for known issue [1] in Python Fire - Add workaround for an issue in the Code Runner extension - Add tasks for Visual Studio Code: - Show usage help (only if Python Fire is enabled) - Run hello - Run hello with an argument - Code Runner: ignore editor selection - Add option to skip installing dependencies [1] google/python-fire#188 (comment)
- Loading branch information
Showing
8 changed files
with
136 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=733558 | ||
// for the documentation about the tasks.json format | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{%- if cookiecutter.use_fire == "y" %} | ||
{ | ||
"label": "{{ cookiecutter.first_module_name }}: Show usage help", | ||
"type": "process", | ||
"command": "pipenv", | ||
"args": [ | ||
"run", | ||
"{{ cookiecutter.first_module_name }}" | ||
], | ||
"problemMatcher": [], | ||
"group": "build", | ||
"presentation": { | ||
"clear": true, | ||
"showReuseMessage": false | ||
} | ||
}, | ||
{%- endif %} | ||
{ | ||
"label": "{{ cookiecutter.first_module_name }}: Run hello", | ||
"type": "process", | ||
"command": "pipenv", | ||
"args": [ | ||
"run", | ||
"{{ cookiecutter.first_module_name }}" | ||
{%- if cookiecutter.use_fire == "y" %}, | ||
"hello" | ||
{%- endif %} | ||
], | ||
"problemMatcher": [], | ||
"group": { | ||
"kind": "build", | ||
"isDefault": true | ||
}, | ||
"presentation": { | ||
"clear": true, | ||
"showReuseMessage": false | ||
} | ||
}, | ||
{ | ||
"label": "{{ cookiecutter.first_module_name }}: Run hello '{{ cookiecutter.author_full_name }}'", | ||
"type": "process", | ||
"command": "pipenv", | ||
"args": [ | ||
"run", | ||
{%- if cookiecutter.use_fire == "y" %} | ||
"{{ cookiecutter.first_module_name }}", | ||
"hello", | ||
"{{ cookiecutter.author_full_name }}" | ||
{%- else %} | ||
"python", | ||
"-c", | ||
"import {{ cookiecutter.first_module_name }}; print({{ cookiecutter.first_module_name }}.hello(\"{{ cookiecutter.author_full_name }}\"))" | ||
{%- endif %} | ||
], | ||
"problemMatcher": [], | ||
"group": "build", | ||
"presentation": { | ||
"clear": true, | ||
"showReuseMessage": false | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Several workarounds for known issues in our dependencies""" | ||
|
||
import colorama | ||
import fire | ||
|
||
|
||
def __fire_suppress_pager(): | ||
"""Make Python Fire not use a pager when it prints a help text. | ||
See also: | ||
https://github.com/google/python-fire/issues/188#issuecomment-631419585 | ||
""" | ||
fire.core.Display = lambda lines, out: print(*lines, file=out) | ||
|
||
|
||
def __vscode_code_runner_fix_colors(): | ||
"""Work around an issue in the Code Runner extension for | ||
Visual Studio Code, which claims to understand ANSI sequences | ||
but doesn’t actually interpret them. | ||
""" | ||
colorama.init() | ||
|
||
|
||
def apply(): | ||
"""Applies all known workarounds.""" | ||
__fire_suppress_pager() | ||
__vscode_code_runner_fix_colors() |
19 changes: 16 additions & 3 deletions
19
{{ cookiecutter.project_slug }}/src/{{ cookiecutter.first_module_name }}.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,22 @@ | ||
"""This is the description of the {{ cookiecutter.first_module_name }} module.""" | ||
{%- if cookiecutter.use_fire == "y" %} | ||
import fire | ||
import fire_workarounds | ||
{%- endif %} | ||
|
||
def hello(): | ||
"""Returns the string `Hello, world!` | ||
|
||
def hello(name='world'): | ||
"""Returns a greeting. | ||
""" | ||
return 'Hello, world!' | ||
return f'Hello, {name}!' | ||
|
||
|
||
if __name__ == '__main__': | ||
{%- if cookiecutter.use_fire == "y" %} | ||
fire_workarounds.apply() | ||
fire.Fire({ | ||
"hello": hello | ||
}) | ||
{%- else %} | ||
print(hello()) | ||
{%- endif %} |
13 changes: 11 additions & 2 deletions
13
{{ cookiecutter.project_slug }}/tests/test_{{ cookiecutter.first_module_name }}.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
import {{ cookiecutter.first_module_name }} | ||
|
||
def test_hello(): | ||
assert {{ cookiecutter.first_module_name }}.hello() == 'Hello, world!' | ||
|
||
def test_hello_with_author_name(): | ||
assert {{ cookiecutter.first_module_name }} \ | ||
.hello('{{ cookiecutter.author_full_name }}') \ | ||
== 'Hello, {{ cookiecutter.author_full_name }}!' | ||
|
||
|
||
def test_hello_with_no_arguments(): | ||
assert {{ cookiecutter.first_module_name }} \ | ||
.hello() \ | ||
== 'Hello, world!' |