Skip to content

Commit

Permalink
Cleanup the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Pattio committed Apr 29, 2019
1 parent 880caa5 commit 543fb73
Show file tree
Hide file tree
Showing 11 changed files with 449 additions and 178 deletions.
25 changes: 15 additions & 10 deletions deepswarm/__init__.py
Expand Up @@ -2,46 +2,51 @@
# Licensed under MIT License

import argparse
import os
import operator
import os
import sys
from yaml import load, Loader

from pathlib import Path
from shutil import copyfile
from yaml import load, Loader

# Create argument parser which allows users to pass a custom script name
# If user didn't pass a custom script name then use sys.argv[0]

# Create argument parser which allows users to pass a custom settings file name
# If the user didn't pass a custom script name then use sys.argv[0]
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--settings_file_name', default=os.path.basename(sys.argv[0]),
help='Settings file name. The default value is the name of invoked script without the .py extenstion')
args, _ = parser.parse_known_args()

# Retrieve name without the extension
# Retrieve filename without the extension
filename = os.path.splitext(args.settings_file_name)[0]

# If mnist yaml doesn't exist it means package was installed via pip in which
# case we should use current working directory as the base path
# If mnist.yaml doesn't exist it means that the package was installed via pip in
# which case we should use the current working directory as the base path
base_path = Path(os.path.dirname(os.path.dirname(__file__)))
if not (base_path / 'settings' / 'mnist.yaml').exists():
module_path = base_path
# Change the base path to current working directory

# Change the base path to the current working directory
base_path = Path(os.getcwd())
settings_directory = (base_path / 'settings')

# Create settings directory if it doesn't exist
if not settings_directory.exists():
settings_directory.mkdir()

# If default settings file doesn't exist, copy one from the module directory
module_default_config = module_path / 'settings/default.yaml'
settings_default_config = settings_directory / 'default.yaml'
if not settings_default_config.exists() and module_default_config.exists():
copyfile(module_default_config, settings_default_config)

# As the base path is now configured we try to load configuration file
# associated with the filename
# associated with the filename
settings_directory = base_path / 'settings'
settings_file_path = Path(settings_directory, filename).with_suffix('.yaml')

# If file doesn't exist fallback to default settings file
# If the file doesn't exist fallback to the default settings file
if not settings_file_path.exists():
settings_file_path = Path(settings_directory, 'default').with_suffix('.yaml')

Expand Down

0 comments on commit 543fb73

Please sign in to comment.