{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext hamilton.plugins.jupyter_magic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.43.0 (0)\n",
       " -->\n",
       "<!-- Title: %3 Pages: 1 -->\n",
       "<svg width=\"300pt\" height=\"308pt\"\n",
       " viewBox=\"0.00 0.00 300.00 308.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 304)\">\n",
       "<title>%3</title>\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-304 296,-304 296,4 -4,4\"/>\n",
       "<g id=\"clust1\" class=\"cluster\">\n",
       "<title>cluster__legend</title>\n",
       "<polygon fill=\"#ffffff\" stroke=\"black\" points=\"56.5,-160 56.5,-292 152.5,-292 152.5,-160 56.5,-160\"/>\n",
       "<text text-anchor=\"middle\" x=\"104.5\" y=\"-276.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">Legend</text>\n",
       "</g>\n",
       "<!-- A -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>A</title>\n",
       "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M280,-65C280,-65 250,-65 250,-65 244,-65 238,-59 238,-53 238,-53 238,-13 238,-13 238,-7 244,-1 250,-1 250,-1 280,-1 280,-1 286,-1 292,-7 292,-13 292,-13 292,-53 292,-53 292,-59 286,-65 280,-65\"/>\n",
       "<text text-anchor=\"start\" x=\"259.5\" y=\"-43.8\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">A</text>\n",
       "<text text-anchor=\"start\" x=\"255.5\" y=\"-15.8\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">int</text>\n",
       "</g>\n",
       "<!-- B -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>B</title>\n",
       "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M280,-149C280,-149 250,-149 250,-149 244,-149 238,-143 238,-137 238,-137 238,-97 238,-97 238,-91 244,-85 250,-85 250,-85 280,-85 280,-85 286,-85 292,-91 292,-97 292,-97 292,-137 292,-137 292,-143 286,-149 280,-149\"/>\n",
       "<text text-anchor=\"start\" x=\"259.5\" y=\"-127.8\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">B</text>\n",
       "<text text-anchor=\"start\" x=\"255.5\" y=\"-99.8\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">int</text>\n",
       "</g>\n",
       "<!-- _A_inputs -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>_A_inputs</title>\n",
       "<polygon fill=\"#ffffff\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"209,-66 0,-66 0,0 209,0 209,-66\"/>\n",
       "<text text-anchor=\"start\" x=\"15.5\" y=\"-39.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">some_value</text>\n",
       "<text text-anchor=\"start\" x=\"139.5\" y=\"-39.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">int</text>\n",
       "<text text-anchor=\"start\" x=\"52.5\" y=\"-18.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">c</text>\n",
       "<text text-anchor=\"start\" x=\"103.5\" y=\"-18.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">CustomClass</text>\n",
       "</g>\n",
       "<!-- _A_inputs&#45;&gt;A -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>_A_inputs&#45;&gt;A</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M209.01,-33C215.62,-33 221.98,-33 227.88,-33\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"227.96,-36.5 237.96,-33 227.96,-29.5 227.96,-36.5\"/>\n",
       "</g>\n",
       "<!-- _B_inputs -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>_B_inputs</title>\n",
       "<polygon fill=\"#ffffff\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"209,-150 0,-150 0,-84 209,-84 209,-150\"/>\n",
       "<text text-anchor=\"start\" x=\"15.5\" y=\"-123.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">some_value</text>\n",
       "<text text-anchor=\"start\" x=\"139.5\" y=\"-123.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">int</text>\n",
       "<text text-anchor=\"start\" x=\"52.5\" y=\"-102.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">c</text>\n",
       "<text text-anchor=\"start\" x=\"103.5\" y=\"-102.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">CustomClass</text>\n",
       "</g>\n",
       "<!-- _B_inputs&#45;&gt;B -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>_B_inputs&#45;&gt;B</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M209.01,-117C215.62,-117 221.98,-117 227.88,-117\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"227.96,-120.5 237.96,-117 227.96,-113.5 227.96,-120.5\"/>\n",
       "</g>\n",
       "<!-- input -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>input</title>\n",
       "<polygon fill=\"#ffffff\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"134,-260.5 75,-260.5 75,-223.5 134,-223.5 134,-260.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"104.5\" y=\"-238.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">input</text>\n",
       "</g>\n",
       "<!-- function -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>function</title>\n",
       "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M132.5,-205.5C132.5,-205.5 76.5,-205.5 76.5,-205.5 70.5,-205.5 64.5,-199.5 64.5,-193.5 64.5,-193.5 64.5,-180.5 64.5,-180.5 64.5,-174.5 70.5,-168.5 76.5,-168.5 76.5,-168.5 132.5,-168.5 132.5,-168.5 138.5,-168.5 144.5,-174.5 144.5,-180.5 144.5,-180.5 144.5,-193.5 144.5,-193.5 144.5,-199.5 138.5,-205.5 132.5,-205.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"104.5\" y=\"-183.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">function</text>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x758cc9baf2c0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%cell_to_module -m test_module --display --rebuild-drivers\n",
    "\n",
    "from hamilton.function_modifiers import cache\n",
    "import pandas as pd\n",
    "from hamilton.caching import fingerprinting\n",
    "from typing import Any\n",
    "class CustomClass:\n",
    "    data = 3\n",
    "\n",
    "@fingerprinting.hash_value.register(CustomClass)  # type: ignore[misc]\n",
    "def hash_db_client_object(obj: CustomClass, *args: Any, **kwargs: Any) -> str:\n",
    "    \"\"\"Trick hamilton into caching data loading stage and avoid DB access.\"\"\"\n",
    "    return \"constant\"\n",
    "\n",
    "def A(c: CustomClass, some_value: int) -> int:\n",
    "    return some_value + 10 + c.data\n",
    "\n",
    "def B(c: CustomClass, some_value: int) -> int:\n",
    "    return some_value + 20 + c.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from hamilton import driver\n",
    "import test_module\n",
    "\n",
    "\n",
    "def get_driver(new_value):\n",
    "\n",
    "    dr = (\n",
    "        driver.Builder()\n",
    "        .with_config({\"some_value\": new_value})\n",
    "        .with_modules(test_module)\n",
    "        .with_cache(path=\"./another\")\n",
    "        .build()\n",
    "    )\n",
    "    return dr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Node `c` has unhashable result of type `<class 'test_module.CustomClass'>`. Set `CachingBehavior.IGNORE` or register a versioning function to silence warning. Learn more: https://hamilton.dagworks.io/en/latest/concepts/caching/#caching-behavior\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'A': 113, 'B': 123}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class_inst = test_module.CustomClass()\n",
    "get_driver(100).execute([\"A\", \"B\"], inputs={\"c\": class_inst})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Node `c` has unhashable result of type `<class 'test_module.CustomClass'>`. Set `CachingBehavior.IGNORE` or register a versioning function to silence warning. Learn more: https://hamilton.dagworks.io/en/latest/concepts/caching/#caching-behavior\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'A': 213, 'B': 223}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class_inst = test_module.CustomClass()\n",
    "get_driver(200).execute([\"A\", \"B\"], inputs={\"c\": class_inst})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
