{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This details how the `requires` decorator can be used."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from opt import requires"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The function takes an arbitrary number of strings that describe the dependencies introduced by the class or function. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "@requires('pandas')\n",
    "def test():\n",
    "    import pandas\n",
    "    print('yay pandas version {}'.format(pandas.__version__))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "yay pandas version 0.16.2\n"
     ]
    }
   ],
   "source": [
    "test()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So, neat. What if we didn't have the module needed?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "@requires(\"notarealmodule\")\n",
    "def test2():\n",
    "    print(\"you shouldn't see this\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "missing dependencies: ['notarealmodule']\n",
      "not running test2\n"
     ]
    }
   ],
   "source": [
    "test2()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So, if the requirements aren't met, your original function is replaced with a new function.\n",
    "\n",
    "it looks kind of like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def passer():\n",
    "    if verbose:\n",
    "        missing = [arg for i,arg in enumerate(args) if not available[i]]\n",
    "        print(\"missing dependencies: {d}\".format(d=missing))\n",
    "        print(\"not running {}\".format(function.__name__))\n",
    "    else:\n",
    "        pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By deafult, the function is replaced with a verbose version. If you pass `verbose=False`, then the function gets replaced with a nonprinting version. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "@requires(\"notarealmodule\", verbose=False)\n",
    "def test3():\n",
    "    print(\"you shouldn't see this, either\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "test3()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The cool thing is, this works on class definitions as well:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "@requires(\"notarealmodule\")\n",
    "class OLS_mock(object):\n",
    "    def __init__(self, *args, **kwargs):\n",
    "        for arg in args:\n",
    "            print(arg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "missing dependencies: ['notarealmodule']\n",
      "not running OLS_mock\n"
     ]
    }
   ],
   "source": [
    "OLS_mock(1,2,3,4,5, w='Tom')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "@requires(\"pymc3\")\n",
    "class BayesianHLM(object):\n",
    "    def __init__(self, *args, **kwargs):\n",
    "                    print(arg)for arg in args[0:]:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<__main__.BayesianHLM at 0x7fe3a32f9e90>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BayesianHLM(1,2,3,4,5, w='Tom')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I think this could make more sense for `spreg` dependencies, where the extra stuff might be reasonable at a function or class level, rather than at the module level. \n",
    "\n",
    "In addition, it could be useful for any plans for a viz setup, where maybe a projected map would require `cartopy`, but an unprojected map could just use `matplotlib`. This would let us do this function-by-function and print or raise reasonable fail states. "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
