{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0981bae1",
   "metadata": {},
   "source": [
    "# Example: Functions argument types\n",
    "\n",
    "In this example, we are going to see what types of arguments are most popular in Python.\n",
    "\n",
    "First, let's pull in all the code from every module we can import:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "647e97b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We have loaded 5792 modules\n"
     ]
    }
   ],
   "source": [
    "from code_data.module_codes import modules_codes_cached\n",
    "\n",
    "# tuples of name, source, code\n",
    "module_codes = modules_codes_cached()\n",
    "print(f\"We have loaded {len(module_codes)} modules\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c71d9a3",
   "metadata": {},
   "source": [
    "Now we can turn them all into data, and also iterate through them all to have all the recursive\n",
    "ones at the top level:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "29c0cb39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We have loaded 155408 code data objects\n"
     ]
    }
   ],
   "source": [
    "from code_data import CodeData\n",
    "\n",
    "all_code_data = {code_data for _, _, code in module_codes for code_data in CodeData.from_code(code).all_code_data()}\n",
    "print(f\"We have loaded {len(all_code_data)} code data objects\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3bbccea5",
   "metadata": {},
   "source": [
    "Let's filter for functions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "908b1904",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "126792 of them are functions\n"
     ]
    }
   ],
   "source": [
    "from code_data import Function\n",
    "\n",
    "fns = {c for c in all_code_data if isinstance(c.type, Function)}\n",
    "print(f\"{len(fns)} of them are functions\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6c5a11d",
   "metadata": {},
   "source": [
    "We can see how many functions have a docstring:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d8a8d0b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The function has a docstring? Counter({False: 96336, True: 30456})\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "\n",
    "have_docstring = Counter(bool(f.type.docstring) for f in fns)\n",
    "\n",
    "print(f\"The function has a docstring? {have_docstring}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc1292c9",
   "metadata": {},
   "source": [
    "And finally, we can see how many different argument types are used cumulatively:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0ae1a207",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({<_ParameterKind.POSITIONAL_OR_KEYWORD: 1>: 202948,\n",
       "         <_ParameterKind.VAR_KEYWORD: 4>: 5066,\n",
       "         <_ParameterKind.VAR_POSITIONAL: 2>: 3689,\n",
       "         <_ParameterKind.KEYWORD_ONLY: 3>: 3283,\n",
       "         <_ParameterKind.POSITIONAL_ONLY: 0>: 246})"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "param_kinds = Counter(k for c in fns for k in c.type.args.parameters.values())\n",
    "param_kinds"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4941b39",
   "metadata": {},
   "source": [
    "There are an order of magnitude more \"positional or keyword\" arguments then all other\n",
    "types combined!\n",
    "\n",
    "And we can see there is the least number of positional only arguments, which make\n",
    "sense since they were most recently introduced in [PEP 570](https://peps.python.org/pep-0570/)"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "formats": "md:myst",
   "text_representation": {
    "extension": ".md",
    "format_name": "myst",
    "format_version": 0.13,
    "jupytext_version": "1.11.5"
   }
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.4"
  },
  "source_map": [
   14,
   22,
   28,
   33,
   38,
   42,
   47,
   51,
   57,
   61,
   64
  ]
 },
 "nbformat": 4,
 "nbformat_minor": 5
}