Last active
November 11, 2018 02:16
-
-
Save mkakh/833b2aed4194eaf4aa80d5be59b224f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import json | |
from optparse import OptionParser | |
# parseArgs :: (IO (), Dict, [String]) | |
def parseArgs(): | |
parser = OptionParser( | |
usage="Usage: %prog input_file.ipynb -o output_file.ipynb") | |
parser.add_option( | |
"-o", | |
type="string", | |
dest="output_file", | |
help="write to OUTPUT_FILE" | |
) | |
options, args = parser.parse_args() | |
return (parser.print_help, vars(options), args) | |
# loadJson :: String -> Dict | |
def loadJson(filePath): | |
with open(filePath, 'r') as f: | |
jsonDict = json.load(f) | |
return jsonDict | |
# existsTag :: String -> [String] -> Bool | |
def existsTag(tag, inputs): | |
if tag in str(inputs): | |
return True | |
else: | |
return False | |
# existsKey :: String -> Dict -> Bool | |
def existsKey(key, inputs): | |
if key in inputs: | |
return True | |
else: | |
return False | |
# writeJson :: String -> Dict -> IO() | |
def writeJson(filePath, jsonDict): | |
with open(filePath, "w") as f: | |
f.write(json.dumps(jsonDict, ensure_ascii=False, indent=1)) | |
f.write('\n') | |
# showJson :: Dict -> IO () | |
def showJson(jsonDict): | |
print(json.dumps(jsonDict, ensure_ascii=False, indent=1)) | |
if __name__ == '__main__': | |
printHelp, options, args = parseArgs() | |
if len(args) != 1: | |
printHelp() | |
exit(1) | |
jsonDict = loadJson(args[0]) | |
for i in range(len(jsonDict['cells'])): | |
if existsTag("#erase", jsonDict['cells'][i]['source']): | |
jsonDict['cells'][i]['source'] = '' | |
if existsKey('outputs', jsonDict['cells'][i]): | |
jsonDict['cells'][i]['outputs'] = [] | |
if existsKey('execution_count', jsonDict['cells'][i]): | |
jsonDict['cells'][i]['execution_count'] = None | |
if options['output_file'] != None: | |
writeJson(options["output_file"], jsonDict) | |
else: | |
showJson(jsonDict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment