Skip to content

Instantly share code, notes, and snippets.

@anmolgarg
Last active November 11, 2015 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anmolgarg/ef483a22babb353367a9 to your computer and use it in GitHub Desktop.
Save anmolgarg/ef483a22babb353367a9 to your computer and use it in GitHub Desktop.
'''
Creates a new directory for a project consisting of a root folder named `[date_]project`
and a template of folders designed for a typical data science project.
Script first asks for user input for naming and then clones a template folder using git.
The script also writes a README.md file with creation information.
Run the make_project_dir script by typing
'python -c "$(curl -fsSL https://gist.githubusercontent.com/anmolgarg/ef483a22babb353367a9/raw/make_project_dir.py)"'
To use your own template repo, simply provide the url to a repo matching the style of the
default repo as an arg or when asked for user input.
The default repo notebook is best when used in conjunction with a custom
ipython profile startup script for setting default imports and pandas and matplotlib configurations.
Run this curl command to copy my startup script to your ipython profile
'curl -L https://gist.githubusercontent.com/anmolgarg/b92ae9985efc6543f0cc/raw/00-pd_mpl_mag.ipy > ~/.ipython/profile_default/startup/00-pd_plt_magic.ipy'
'''
import os
import shutil
import sys
import datetime as dt
def make_project_dir(name, date, repo):
'''Clones given git repo. Renames repo, deletes .git/ dir,
and renames eda notebook if exists.
Also writes a simple README file with login name and datetime.
'''
# create destination names
datestr = dt.datetime.now().strftime('%m%d%y')
folder_name = name.strip().replace(' ', '_')
if not date:
ipy_name = datestr+'_'+folder_name
else:
folder_name = ipy_name = datestr+'_'+folder_name
dir_dest = './'+folder_name
# clone template repo from given github, rename, and delete git dot folder
os.system("git clone {}".format(repo))
shutil.move(repo.split('/')[-1].split('.')[0], dir_dest)
shutil.rmtree(dir_dest+'/.git/')
# if cloned template has a notebook at notebooks/eda.ipynb, rename to notebooks/[date]_[project_name].ipynb
try:
os.rename(dir_dest+"/notebooks/eda.ipynb", dir_dest+"/notebooks/"+ipy_name+".ipynb")
except:
pass
# write README.md
readthis = '##'+name.strip().upper()+'\n\ncreated by: ' + os.getlogin() + '\n\ncreated at: ' + str(dt.datetime.now())
readme = os.open(dir_dest + "/README.md", os.O_RDWR | os.O_CREAT)
os.write(readme, readthis)
os.close(readme)
print 'created', folder_name
return
def ask_master(repo):
'''Asks for user input on project details'''
name = raw_input("Please enter the name of project: ")
date = raw_input("Do you want to date stamp the folder name? [yes]: ")
if repo is None:
repo = raw_input("Please enter optional custom template repo [https://github.com/anmolgarg/project_template.git]: ")
try:
if date.lower()[0] == 'n':
date = False
else:
date = True
except:
date = True
if repo != '':
print 'using user defined repo:', repo
else:
repo = 'https://github.com/anmolgarg/project_template.git'
print 'using default repo:', repo
return name, date, repo
if __name__ == "__main__":
try:
repo = sys.argv[1]
except:
repo = None
name, date, repo = ask_master(repo)
make_project_dir(name, date, repo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment