Skip to content

Instantly share code, notes, and snippets.

@ruxi
Last active March 14, 2016 21:53
Show Gist options
  • Save ruxi/46aa6e3c14e7467af372 to your computer and use it in GitHub Desktop.
Save ruxi/46aa6e3c14e7467af372 to your computer and use it in GitHub Desktop.
notebooks/gists/lazy_make_init.py.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "2016-02-08 18:00:24 "
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2016-03-08T17:19:08.903396",
"start_time": "2016-03-08T17:19:08.888034"
},
"variables": {
"import datetime; datetime.datetime.now().ctime()": "Mon Mar 14 16:53:04 2016"
}
},
"cell_type": "markdown",
"source": "**last updated**: &nbsp;&nbsp; {{import datetime; datetime.datetime.now().ctime()}} &nbsp;&nbsp;&nbsp;&nbsp; (req. [nbextensions](/nbextensions) python-markdown)<br>\n**author**:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; https://github.com/ruxi\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "**Desc**: Populates **\\_\\_init\\_\\_.py** based on *.py files in module directory\n\n```python\n\n#!/usr/bin/env python\nfrom . import script1\nfrom . import script2\nfrom . import scriptn\n\n```\n\n**Usage**:\n\n > lazy_make_init(path, [\"__author__ = 'your name'\"])"
},
{
"metadata": {
"collapsed": true,
"trusted": true,
"ExecuteTime": {
"end_time": "2016-03-08T17:56:46.771703",
"start_time": "2016-03-08T17:56:46.739621"
}
},
"cell_type": "code",
"source": "#!/usr/bin/env python\nimport os.path\ndef lazy_make_init(path, verbose=True, **kwargs):\n \"\"\" desc: adds import statements to all *.py files in path/to/module\n args: path (req): path/to/module\n header (opt.): for adding docstrings. \n example: ['__author__ = bob']\"\"\"\n\n # get import statements based on *.py\n modules = ['from . import '+ os.path.splitext(file)[0] \n for file in os.listdir(path) \n if '.py' in file and file != '__init__.py']\n\n # write to file \n file = os.path.join(path,'__init__.py')\n with open(file, 'w') as fp:\n # shebang & user vanity and docstrings\n fp.writelines('#!/usr/bin/env python\\n') #shebang\n [fp.writelines(line+'\\n') for line in kwargs['header']] if 'header' in kwargs else None\n # write import script statements\n [fp.writelines(line+'\\n') for line in modules]\n # read file\n if verbose:\n print('\\nwrote to file:\\n{}...\\n'.format(file))\n with open(file,'r') as f:\n print(f.read())",
"execution_count": 2,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "**Background**:\n\n- modules are folders\n- scripts are file.py\n- definitions are functions in file.py\n\nIf **module/\\_\\_init\\_\\_.py** is **blank**, then verbosely, we need\n\n> from module import script <br>\n> script.definition(args)\n\nIf **module/\\_\\_init\\_\\_.py** includes **import script**, then we can chain\n\n> import module <br>\n> module.script.function(args)\n\nThis is useful when there are multiple scripts in a package. \n\n**Motivation**\n\n- \\_\\_init\\_\\_.py must be modified each time a script is added.\n- lazy\\_make\\_init.py basically looks for all \\*.py files and adds import statements in \\_\\_init\\_\\_.py"
}
],
"metadata": {
"language_info": {
"version": "3.5.1",
"name": "python",
"file_extension": ".py",
"nbconvert_exporter": "python",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"mimetype": "text/x-python",
"pygments_lexer": "ipython3"
},
"gist": {
"id": "46aa6e3c14e7467af372",
"data": {
"description": "notebooks/gists/lazy_make_init.py.ipynb",
"public": true
}
},
"kernelspec": {
"name": "python3",
"display_name": "IPython (Python 3)",
"language": "python"
},
"latex_envs": {
"cite_by": "apalike",
"eqNumInitial": 0,
"bibliofile": "biblio.bib",
"current_citInitial": 1,
"eqLabelWithNumbers": true
},
"_draft": {
"nbviewer_url": "https://gist.github.com/46aa6e3c14e7467af372"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment