<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ecological Tutorial"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Getting Started"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before we start to set some environment variables, note than in a real application this would be set outside of your application."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"INTEGER_LIST\"] = \"[1, 2, 3, 4, 5]\"\n",
    "os.environ[\"INTEGER\"] = \"42\"\n",
    "os.environ[\"BOOLEAN\"] = \"False\"\n",
    "os.environ[\"OVERRIDE_DEFAULT\"] = \"This is NOT the default value\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's create a configuration class:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ecological\n",
    "\n",
    "class Configuration(ecological.AutoConfig):\n",
    "    integer_list: list\n",
    "    integer: int\n",
    "    boolean: bool\n",
    "    with_default: str = \"This is the default value\"\n",
    "    override_default: str = \"This is the default value\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Easy right?\n",
    "Now that we created the configuration class. Let's look at what's inside:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "print(repr(Configuration.integer_list))\n",
    "print(type(Configuration.integer_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42\n",
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "print(repr(Configuration.integer))\n",
    "print(type(Configuration.integer))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "<class 'bool'>\n"
     ]
    }
   ],
   "source": [
    "print(repr(Configuration.boolean))\n",
    "print(type(Configuration.boolean))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'This is the default value'\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(repr(Configuration.with_default))\n",
    "print(type(Configuration.with_default))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'This is NOT the default value'\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(repr(Configuration.override_default))\n",
    "print(type(Configuration.override_default))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see all the values where cast from `str` to the expected types, and if a default value is set it will be used if the corresponding environment variable doesn't exist."
   ]
  }
 ],
 "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.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}<commit_msg>Add typing support to the tutorial<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ecological Tutorial"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Getting Started"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before we start to set some environment variables, note than in a real application this would be set outside of your application."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"INTEGER_LIST\"] = \"[1, 2, 3, 4, 5]\"\n",
    "os.environ[\"DICTIONARY\"] = \"{'key': 'value'}\"\n",
    "os.environ[\"INTEGER\"] = \"42\"\n",
    "os.environ[\"BOOLEAN\"] = \"False\"\n",
    "os.environ[\"OVERRIDE_DEFAULT\"] = \"This is NOT the default value\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's create a configuration class:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import ecological\n",
    "\n",
    "class Configuration(ecological.AutoConfig):\n",
    "    integer_list: list\n",
    "    integer: int\n",
    "    dictionary: dict\n",
    "    boolean: bool\n",
    "    with_default: str = \"This is the default value\"\n",
    "    override_default: str = \"This is the default value\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Easy right?\n",
    "Now that we created the configuration class. Let's look at what's inside:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "print(repr(Configuration.integer_list))\n",
    "print(type(Configuration.integer_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42\n",
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "print(repr(Configuration.integer))\n",
    "print(type(Configuration.integer))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'key': 'value'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(repr(Configuration.dictionary))\n",
    "print(type(Configuration.dictionary))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "<class 'bool'>\n"
     ]
    }
   ],
   "source": [
    "print(repr(Configuration.boolean))\n",
    "print(type(Configuration.boolean))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'This is the default value'\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(repr(Configuration.with_default))\n",
    "print(type(Configuration.with_default))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'This is NOT the default value'\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(repr(Configuration.override_default))\n",
    "print(type(Configuration.override_default))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see all the values where cast from `str` to the expected types, and if a default value is set it will be used if the corresponding environment variable doesn't exist."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Typing Support"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Ecological** also supports some of the types defined in [PEP 484](https://www.python.org/dev/peps/pep-0484/), for example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List, Dict\n",
    "\n",
    "class ConfigurationTyping(ecological.AutoConfig):\n",
    "    integer_list: List\n",
    "    dictionary: Dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As expected the variables were converted to the real types:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "print(repr(ConfigurationTyping.integer_list))\n",
    "print(type(ConfigurationTyping.integer_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'key': 'value'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(repr(ConfigurationTyping.dictionary))\n",
    "print(type(ConfigurationTyping.dictionary))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "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.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
{'fraction_non_alphanumeric': 0.21049499057633173, 'fraction_numerical': 0.007836524154349767, 'mean_word_length': 2.474155754651964, 'pattern_counts': {'":': 219, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3341130', 'n_tokens_mistral': 3813, 'n_tokens_neox': 3700, 'n_words': 722}