-
-
Save DimaK415/f8b27ffad4952c7e92e33d97ad52694c to your computer and use it in GitHub Desktop.
Complete Paramerator.ipynb with cells run
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from Paramerator import Parameters" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Instantiate the Class\n", | |
"#### This loads the DEFAULT dict for mapping your parameter files." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"p = Parameters()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Load a file's parameters\n", | |
"#### (Use either bit or boolean responses for Y/N)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\"dat/params1.test\" not found. Generate from default?1\n", | |
"Found Default \"dat/params1.test\". Generate?1\n" | |
] | |
} | |
], | |
"source": [ | |
"p.loader('dat/params1.test', 'name1')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### The loader knew that the file was required\n", | |
"### Now lets try another file" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\"dat/params2.test\" not found. Generate from default?1\n", | |
"Found Default \"dat/params2.test\". Generate?1\n", | |
"Required parameter \"param 1 required\" is missing: .01234\n", | |
"Would you like to save your input to the default file?1\n" | |
] | |
} | |
], | |
"source": [ | |
"p.loader('dat/params2.test', 'name2')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Lets look at the files loader generated" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"dat/params1.test\n", | |
"##Section 1\n", | |
"param 1 int = 1\n", | |
"param 2 float = 0.25\n", | |
"param 3 string = string\n", | |
"param 4 list = can, be, list\n", | |
"param 5 dict = {\"or\":\"dictionary\"}\n", | |
"\n", | |
"dat/params2.test\n", | |
"##Section1\n", | |
"param1required = 0.01234\n", | |
"param2 = [1, 2, 3, 4, 5]\n", | |
"param3 = 20\n", | |
"\n", | |
"##Section2\n", | |
"param1 = True\n", | |
"param2 = False\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"with open('dat/params1.test', 'r') as fileObj1: #Load dat/params1.test\n", | |
" x = fileObj1.read()\n", | |
" print('dat/params1.test' + x)\n", | |
" \n", | |
"with open('dat/params2.test', 'r') as fileObj2: #Load dat/params2.test\n", | |
" x = fileObj2.read()\n", | |
" print('dat/params2.test' + x)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Compare to the attributes attached to params" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"section(Section1=param(param1int=1, param2float=0.25, param3string='string', param4list=['can', 'be', 'list'], param5dict={'or': 'dictionary'})) section(Section1=param(param1required=0.01234, param2=[1, 2, 3, 4, 5], param3=20), Section2=param(param1=True, param2=False))\n" | |
] | |
} | |
], | |
"source": [ | |
"print(p.name1, p.name2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## And call the values as you would attributes using tab. (automatic lookup)\n", | |
"Try it!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"scrolled": false | |
}, | |
"outputs": [], | |
"source": [ | |
"p.name1.Section1.param1int.real ## Tab complete" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Unlike with named tuples, param values are mutable" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'string'" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"p.name1.Section1.param3string" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'this is a new string'" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"p.name1.Section1.param3string = \"this is a new string\"\n", | |
"p.name1.Section1.param3string" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The param keys and sections are not mutable to prevent missing params." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "AttributeError", | |
"evalue": "can't set attribute", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-9-0d047f789692>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSection1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m\"this\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"wont\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"work\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mAttributeError\u001b[0m: can't set attribute" | |
] | |
} | |
], | |
"source": [ | |
"p.name1.Section1 = [\"this\", \"wont\", \"work\"]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## params.loader is object type aware." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dict" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"p.name1.Section1.param5dict = {\"Pretty Cool\":\"ey?\"}\n", | |
"type(p.name1.Section1.param5dict)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(True, bool)" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"p.name2.Section2.param1, type(p.name2.Section2.param1)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## param_writer can save your currently loaded params\n", | |
"In this example we make \"new.test\" in our \"dat\" folder" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"p.writer('dat/new.test', p.name2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## And will prompt on overwrites, asking for new file location if desired." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"dat/new.test already exists. Overwrite? :1\n" | |
] | |
} | |
], | |
"source": [ | |
"p.writer('dat/new.test', p.name2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## You can also generate fresh copies of parameter files using generate_defaults() \n", | |
"This is a work in progress" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\"dat/mongo.info\" not found. Generate from default?1\n", | |
"Found Default \"dat/mongo.info\". Generate?1\n", | |
"Required parameter \"IP\" is missing: 127.0.0.1\n", | |
"Required parameter \"Host\" is missing: 27017\n", | |
"Would you like to save your input to the default file?1\n" | |
] | |
} | |
], | |
"source": [ | |
"p.generate_defaults()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"p.loader('dat/mongo.info', 'mongo')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"section(Location=param(IP='127.0.0.1', Host=27017), Info=param(DataBase='2 Fast', Collection='2 Furious'))" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"p.mongo" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(param(IP='127.0.0.1', Host=27017),\n", | |
" param(DataBase='2 Fast', Collection='2 Furious'))" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"p.mongo.Location, p.mongo.Info" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This can be useful if you are sharing a repo and need to generate files with API keys or other sensitive data.\n", | |
"In the future, I intend to make use of encryption for sensitive data, but until then, be aware that the files generated and their locations are easily visible." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Lastly, here is the DEFAULTS.py dict which built our param files. " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'PARAMS1': {'Default Path': 'dat/params1.test',\n", | |
" 'DAT': {'Section 1': {'param 1 int': '1',\n", | |
" 'param 2 float': '0.25',\n", | |
" 'param 3 string': 'string',\n", | |
" 'param 4 list': 'can, be, list',\n", | |
" 'param 5 dict': '{\"or\":\"dictionary\"}'}}},\n", | |
" 'PARAMS2': {'Default Path': 'dat/params2.test',\n", | |
" 'DAT': {'Section 1': {'param 1 required': '*',\n", | |
" 'param 2': '1, 2, 3, 4, 5',\n", | |
" 'param 3': '20'},\n", | |
" 'Section 2': {'param 1': 'True', 'param 2': 'False'}}},\n", | |
" 'MONGO': {'Default Path': 'dat/mongo.info',\n", | |
" 'DAT': {'Location': {'IP': '*', 'Host': '*'},\n", | |
" 'Info': {'DataBase': '2 Fast', 'Collection': '2 Furious'}}}}" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"p._def_dict" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Use your favorite text editor to create your DEFAULT.py the way you need it. Enjoy! " | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment