Skip to content

Commit c6a4b6d

Browse files
committed
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)
1 parent 2ced26f commit c6a4b6d

File tree

8 files changed

+136
-6
lines changed

8 files changed

+136
-6
lines changed

cookiecutter.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
"python_version": "3.9",
88
"author_full_name": "",
99
"copyright_holder_full_name": "{{ cookiecutter.author_full_name }}",
10-
"use_pandas": "n"
10+
"use_fire": "n",
11+
"use_pandas": "n",
12+
"install_dependencies_now": "y"
1113
}

hooks/post_gen_project.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
os.unlink('.venv/.keep')
66
shutil.rmtree('licenses')
77

8+
{%- if cookiecutter.use_fire != "y" %}
9+
os.unlink('src/fire_workarounds.py')
10+
{%- endif %}
11+
12+
{%- if cookiecutter.install_dependencies_now == "y" %}
813
subprocess.run(
914
'pipenv install -d',
1015
env={
@@ -15,3 +20,4 @@
1520
check=True,
1621
shell=True,
1722
)
23+
{%- endif %}

{{ cookiecutter.project_slug }}/.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"python.linting.pylintEnabled": true,
44
"python.linting.pylintPath": "./.venv/bin/pylint",
55
"python.linting.enabled": true,
6+
"code-runner.ignoreSelection": true,
67
"code-runner.executorMap": {
78
"python": "pipenv run python",
89
},
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{%- if cookiecutter.use_fire == "y" %}
7+
{
8+
"label": "{{ cookiecutter.first_module_name }}: Show usage help",
9+
"type": "process",
10+
"command": "pipenv",
11+
"args": [
12+
"run",
13+
"{{ cookiecutter.first_module_name }}"
14+
],
15+
"problemMatcher": [],
16+
"group": "build",
17+
"presentation": {
18+
"clear": true,
19+
"showReuseMessage": false
20+
}
21+
},
22+
{%- endif %}
23+
{
24+
"label": "{{ cookiecutter.first_module_name }}: Run hello",
25+
"type": "process",
26+
"command": "pipenv",
27+
"args": [
28+
"run",
29+
"{{ cookiecutter.first_module_name }}"
30+
{%- if cookiecutter.use_fire == "y" %},
31+
"hello"
32+
{%- endif %}
33+
],
34+
"problemMatcher": [],
35+
"group": {
36+
"kind": "build",
37+
"isDefault": true
38+
},
39+
"presentation": {
40+
"clear": true,
41+
"showReuseMessage": false
42+
}
43+
},
44+
{
45+
"label": "{{ cookiecutter.first_module_name }}: Run hello '{{ cookiecutter.author_full_name }}'",
46+
"type": "process",
47+
"command": "pipenv",
48+
"args": [
49+
"run",
50+
{%- if cookiecutter.use_fire == "y" %}
51+
"{{ cookiecutter.first_module_name }}",
52+
"hello",
53+
"{{ cookiecutter.author_full_name }}"
54+
{%- else %}
55+
"python",
56+
"-c",
57+
"import {{ cookiecutter.first_module_name }}; print({{ cookiecutter.first_module_name }}.hello(\"{{ cookiecutter.author_full_name }}\"))"
58+
{%- endif %}
59+
],
60+
"problemMatcher": [],
61+
"group": "build",
62+
"presentation": {
63+
"clear": true,
64+
"showReuseMessage": false
65+
}
66+
}
67+
]
68+
}

{{ cookiecutter.project_slug }}/Pipfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ name = "pypi"
55

66
[packages]
77
{{ cookiecutter.project_slug }} = {editable = true, path = "./src"}
8+
{%- if cookiecutter.use_fire == "y" %}
9+
colorama = "*"
10+
fire = "*"
11+
{%- endif %}
812
{%- if cookiecutter.use_pandas == "y" %}
913
pandas = "*"
1014
{%- endif %}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Several workarounds for known issues in our dependencies"""
2+
3+
import colorama
4+
import fire
5+
6+
7+
def __fire_suppress_pager():
8+
"""Make Python Fire not use a pager when it prints a help text.
9+
10+
See also:
11+
https://github.com/google/python-fire/issues/188#issuecomment-631419585
12+
"""
13+
fire.core.Display = lambda lines, out: print(*lines, file=out)
14+
15+
16+
def __vscode_code_runner_fix_colors():
17+
"""Work around an issue in the Code Runner extension for
18+
Visual Studio Code, which claims to understand ANSI sequences
19+
but doesn’t actually interpret them.
20+
"""
21+
colorama.init()
22+
23+
24+
def apply():
25+
"""Applies all known workarounds."""
26+
__fire_suppress_pager()
27+
__vscode_code_runner_fix_colors()
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
"""This is the description of the {{ cookiecutter.first_module_name }} module."""
2+
{%- if cookiecutter.use_fire == "y" %}
3+
import fire
4+
import fire_workarounds
5+
{%- endif %}
26

3-
def hello():
4-
"""Returns the string `Hello, world!`
7+
8+
def hello(name='world'):
9+
"""Returns a greeting.
510
"""
6-
return 'Hello, world!'
11+
return f'Hello, {name}!'
12+
713

814
if __name__ == '__main__':
15+
{%- if cookiecutter.use_fire == "y" %}
16+
fire_workarounds.apply()
17+
fire.Fire({
18+
"hello": hello
19+
})
20+
{%- else %}
921
print(hello())
22+
{%- endif %}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
import {{ cookiecutter.first_module_name }}
22

3-
def test_hello():
4-
assert {{ cookiecutter.first_module_name }}.hello() == 'Hello, world!'
3+
4+
def test_hello_with_author_name():
5+
assert {{ cookiecutter.first_module_name }} \
6+
.hello('{{ cookiecutter.author_full_name }}') \
7+
== 'Hello, {{ cookiecutter.author_full_name }}!'
8+
9+
10+
def test_hello_with_no_arguments():
11+
assert {{ cookiecutter.first_module_name }} \
12+
.hello() \
13+
== 'Hello, world!'

0 commit comments

Comments
 (0)