Browse Source

Use pathlib instead of os.path where applicable (#112)

* Use pathlib in test_creation.py

* Use pathlib for PROJECT_DIR example in make_dataset.py

* Add pathlib2 for backwards compatibility if project is Python 2
main
Isaac Slavitt 7 years ago committed by Peter Bull
parent
commit
74a2e29fef
  1. 60
      tests/test_creation.py
  2. 5
      {{ cookiecutter.repo_name }}/requirements.txt
  3. 4
      {{ cookiecutter.repo_name }}/src/data/make_dataset.py

60
tests/test_creation.py

@ -1,93 +1,86 @@
import os import os
import pytest
import shutil import shutil
from pathlib import Path
from cookiecutter import main from cookiecutter import main
import pytest
CCDS_ROOT = os.path.abspath( CCDS_ROOT = Path(__file__).parents[1].resolve()
os.path.join(
__file__,
os.pardir,
os.pardir
)
)
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def default_baked_project(tmpdir): def default_baked_project(tmpdir):
out_dir = str(tmpdir.mkdir('data-project')) temp = tmpdir.mkdir('data-project')
out_dir = Path(temp).resolve()
main.cookiecutter( main.cookiecutter(
CCDS_ROOT, str(CCDS_ROOT),
no_input=True, no_input=True,
extra_context={}, extra_context={},
output_dir=out_dir output_dir=out_dir
) )
# default project name is project_name # default project name is project_name
yield os.path.join(out_dir, 'project_name') yield out_dir / 'project_name'
# cleanup after # cleanup after
shutil.rmtree(out_dir) shutil.rmtree(out_dir)
def test_readme(default_baked_project): def test_readme(default_baked_project):
readme_path = os.path.join(default_baked_project, 'README.md') readme_path = default_baked_project / 'README.md'
assert os.path.exists(readme_path) assert readme_path.exists()
assert no_curlies(readme_path) assert no_curlies(readme_path)
def test_license(default_baked_project): def test_license(default_baked_project):
license_path = os.path.join(default_baked_project, 'LICENSE') license_path = default_baked_project / 'LICENSE'
assert os.path.exists(license_path) assert license_path.exists()
assert no_curlies(license_path) assert no_curlies(license_path)
def test_requirements(default_baked_project): def test_requirements(default_baked_project):
reqs_path = os.path.join(default_baked_project, 'requirements.txt') reqs_path = default_baked_project / 'requirements.txt'
assert os.path.exists(reqs_path) assert reqs_path.exists()
assert no_curlies(reqs_path) assert no_curlies(reqs_path)
def test_makefile(default_baked_project): def test_makefile(default_baked_project):
makefile_path = os.path.join(default_baked_project, 'Makefile') makefile_path = default_baked_project / 'Makefile'
assert os.path.exists(makefile_path) assert makefile_path.exists()
assert no_curlies(makefile_path) assert no_curlies(makefile_path)
def test_folders(default_baked_project): def test_folders(default_baked_project):
expected_dirs = [ expected_dirs = [
'data', 'data',
os.path.join('data', 'external'), 'data/external',
os.path.join('data', 'interim'), 'data/interim',
os.path.join('data', 'processed'), 'data/processed',
os.path.join('data', 'raw'), 'data/raw',
'docs', 'docs',
'models', 'models',
'notebooks', 'notebooks',
'references', 'references',
'reports', 'reports',
os.path.join('reports', 'figures'), 'reports/figures',
'src', 'src',
os.path.join('src', 'data'), 'src/data',
os.path.join('src', 'features'), 'src/features',
os.path.join('src', 'models'), 'src/models',
os.path.join('src', 'visualization') 'src/visualization',
] ]
ignored_dirs = [ ignored_dirs = [
default_baked_project str(default_baked_project)
] ]
abs_expected_dirs = [os.path.join(default_baked_project, d) for abs_expected_dirs = [str(default_baked_project / d) for d in expected_dirs]
d in expected_dirs]
abs_dirs, _, _ = list(zip(*os.walk(default_baked_project))) abs_dirs, _, _ = list(zip(*os.walk(default_baked_project)))
assert len(set(abs_expected_dirs + ignored_dirs) - set(abs_dirs)) == 0 assert len(set(abs_expected_dirs + ignored_dirs) - set(abs_dirs)) == 0
@ -106,5 +99,4 @@ def no_curlies(filepath):
] ]
template_strings_in_file = [s in data for s in template_strings] template_strings_in_file = [s in data for s in template_strings]
return not any(template_strings_in_file) return not any(template_strings_in_file)

5
{{ cookiecutter.repo_name }}/requirements.txt

@ -8,3 +8,8 @@ coverage
awscli awscli
flake8 flake8
python-dotenv>=0.5.1 python-dotenv>=0.5.1
{% if cookiecutter.python_interpreter != 'python3' %}
# backwards compatibility
pathlib2
{% endif %}

4
{{ cookiecutter.repo_name }}/src/data/make_dataset.py

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os
import click import click
import logging import logging
from pathlib import Path
from dotenv import find_dotenv, load_dotenv from dotenv import find_dotenv, load_dotenv
@ -21,7 +21,7 @@ if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format=log_fmt) logging.basicConfig(level=logging.INFO, format=log_fmt)
# not used in this stub but often useful for finding various files # not used in this stub but often useful for finding various files
project_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir) project_dir = Path(__file__).resolve().parents[2]
# find .env automagically by walking up directories until it's found, then # find .env automagically by walking up directories until it's found, then
# load up the .env entries as environment variables # load up the .env entries as environment variables

Loading…
Cancel
Save