{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ingredient: Using anonymizers\n",
    "\n",
    "This example shows a shelf with anonymizers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SELECT census.gender AS gender,\n",
      "       sum(census.pop2000) AS population_raw\n",
      "FROM census\n",
      "GROUP BY census.gender\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>gender</th>\n",
       "      <th>population_raw</th>\n",
       "      <th>gender_id</th>\n",
       "      <th>population</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>F</td>\n",
       "      <td>143534804</td>\n",
       "      <td>F</td>\n",
       "      <td>144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>M</td>\n",
       "      <td>137392517</td>\n",
       "      <td>M</td>\n",
       "      <td>137</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  gender  population_raw gender_id  population\n",
       "0      F       143534804         F         144\n",
       "1      M       137392517         M         137"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from examples_base import *\n",
    "\n",
    "shelf = Shelf({\n",
    "    'state': Dimension(Census.state, anonymizer=lambda v: v[::-1]),\n",
    "    'age': WtdAvgMetric(Census.age, Census.pop2000),\n",
    "    'gender': Dimension(Census.gender),\n",
    "    'population': Metric(func.sum(Census.pop2000), formatters=[\n",
    "        lambda value: int(round(value, -6) / 1000000)\n",
    "    ])\n",
    "})\n",
    "\n",
    "recipe = Recipe(shelf=shelf, session=oven.Session(), extension_classes=[Anonymize])\\\n",
    "    .dimensions('state').metrics('population')\n",
    "\n",
    "# Look at the output.\n",
    "print(recipe.to_sql())\n",
    "recipe.dataset.df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Formatters are python code that runs after the row data is retrieved from the database. The original value is available as `ingredient_raw`. The SQL query returns the `ingredient_raw` value and the `ingredient` value is added by calling the formatter."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Edit 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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
