Skip to content

Instantly share code, notes, and snippets.

@tedsteinmann
Created October 14, 2023 20:07
Show Gist options
  • Save tedsteinmann/d3ee4ea35435af132d9d7dc2399fd408 to your computer and use it in GitHub Desktop.
Save tedsteinmann/d3ee4ea35435af132d9d7dc2399fd408 to your computer and use it in GitHub Desktop.
Notebook Common Setup
import os
import sys
import json
import shutil
import inspect
from datetime import datetime
# Get the current working directory
current_file_directory = os.path.dirname(os.path.abspath(__file__))
# Navigate one directory up
parent_directory = os.path.dirname(current_file_directory)
# Specify the file name you want to access
settings_file_name = "settings.json"
servers_file_name = 'servers.json'
# Create the file path
settings_file_path = os.path.join(parent_directory, settings_file_name)
servers_file_path = os.path.join(parent_directory, servers_file_name)
# load settings if the file exists
if os.path.exists(settings_file_path):
# Load settings from settings.json
with open(settings_file_path, 'r') as settings_file:
settings = json.load(settings_file)
# Extract the paths from the settings
module_path = settings.get("module_path", "")
data_path = settings.get("data_path", "")
scripts_path = settings.get("scripts_path", "")
def append_to_sys_path(path_to_append):
if path_to_append not in sys.path:
sys.path.append(path_to_append)
# Append the paths to sys.path
if module_path:
append_to_sys_path(module_path)
if data_path:
append_to_sys_path(data_path)
if scripts_path:
append_to_sys_path(scripts_path)
#initialize project variables
# Get today's date in YYYY-MM-DD format
today_date = datetime.now().strftime('%Y-%m-%d')
project_name = sys.argv[1].strip("'")
project = f'{today_date}-{project_name}'
project_directory_path = f'{data_path}/{project}'
project_raw_path = f'{project_directory_path}/raw'
project_processed_path = f'{project_directory_path}/processed'
project_external_path = f'{project_directory_path}/external/'
print(f'variables initialized for project: {project} \n')
print(f'project: {project}')
print(f'project_directory_path: {project_directory_path}')
print(f'project_raw_path: {project_raw_path}')
print(f'project_processed_path: {project_processed_path}')
print(f'project_external_path: {project_external_path}')
# List of folder paths
folders_to_reset = [project_directory_path, project_raw_path, project_processed_path,project_external_path]
for folder_path in folders_to_reset:
# Check if the folder exists
if os.path.exists(folder_path):
try:
os.rmdir(folder_path)
print(f'Deleted and recreated project folders: \n {project_directory_path} \n {project_raw_path} \n {project_processed_path} \n {project_external_path} \n')
except OSError:
shutil.rmtree(folder_path)
os.mkdir(folder_path)
#generate readme document
with open(f'{project_directory_path}/readme.txt', 'w') as readme_file:
readme_file.write(f"{project} Generated on: {today_date} ")
print(f'\nreadme.txt generated')
@tedsteinmann
Copy link
Author

tedsteinmann commented Oct 15, 2023

Call this from within notebooks with:

%run ../common_setup.py 'my-project'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment