<commit_before><commit_msg>Added metaclass topic<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Metaklasy\n",
    "* Wszystko jest obiektem, klasa też\n",
    "* Klasę można kopiować, modyfikować, mieć wiele referencji do niej\n",
    "* słowo `class` tworzy obiekt\n",
    "* obiekty \"klasy\", potrafią tworzyć obiekty opisane na podstawie swojej definicji\n",
    "* `type` jest klasą\n",
    "* `type(name, bases, dict)`\n",
    "  * name - nazwa klasy\n",
    "  * bases - klasy nadrzędne\n",
    "  * dict - słownik z atrybutami klasy  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "* metaklasa to klasa dla klasy\n",
    "* `type` jest metaklasą zdefiniowaną przez Python'a\n",
    "* `__metaclass__` atrybut metaklasy dla klasy w Python2\n",
    "* `class A(metaclass=metaklasa)` Python3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "class Punkt():\n",
    "    x = 1\n",
    "    y = 2\n",
    "    def dodaj(self):\n",
    "        return self.x + self.y\n",
    "    \n",
    "p = Punkt()    \n",
    "print(p)\n",
    "print(p.x)\n",
    "print(p.y)\n",
    "print(p.dodaj())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "def dodaj(self):\n",
    "    return self.x + self.y\n",
    "\n",
    "KlasaPunkt = type(\"Punkt\", (), {\n",
    "    \"x\": 1,\n",
    "    \"y\": 2,\n",
    "    \"dodaj\": dodaj\n",
    "})\n",
    "\n",
    "p = KlasaPunkt()\n",
    "print(p)\n",
    "print(p.x)\n",
    "print(p.y)\n",
    "print(p.dodaj())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "def capitalize_metaclass_as_function(name, bases, classdict):\n",
    "        d = {}\n",
    "        for attr_key, attr in classdict.items():\n",
    "            print(attr_key, attr)\n",
    "            if callable(attr) and not attr_key.startswith(\"__\".format(name)):\n",
    "                d[attr_key.capitalize()] = attr\n",
    "            else:\n",
    "                d[attr_key] = attr\n",
    "        return type(name, bases, d)\n",
    "\n",
    "class Punkt(metaclass=capitalize_metaclass_as_function):\n",
    "\n",
    "    def __init__(self, x, y):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "        \n",
    "    def dodaj(self):\n",
    "        return self.x + self.y\n",
    "    \n",
    "p = Punkt(1, 2)\n",
    "print(p.Dodaj)\n",
    "print(p.Dodaj())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "class CapitalizeMetaclass(type):\n",
    "    \n",
    "    def __new__(cls, name, bases, classdict):\n",
    "        print(cls)\n",
    "        print(name)\n",
    "        print(bases)\n",
    "        print(classdict)\n",
    "        d = {}\n",
    "        for attr_key, attr in classdict.items():\n",
    "            if callable(attr) and not attr_key.startswith(\"__\".format(name)):\n",
    "                d[attr_key.capitalize()] = attr\n",
    "            else:\n",
    "                d[attr_key] = attr\n",
    "        return super().__new__(cls, name, bases, d)\n",
    "\n",
    "class Punkt(metaclass=CapitalizeMetaclass):\n",
    "\n",
    "    def __init__(self, x, y):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "        \n",
    "    def dodaj(self):\n",
    "        return self.x + self.y\n",
    "    \n",
    "p = Punkt(1, 2)\n",
    "print(p.Dodaj)\n",
    "print(p.Dodaj())"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "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.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
{'fraction_non_alphanumeric': 0.2135706340378198, 'fraction_numerical': 0.004226918798665183, 'mean_word_length': 1.9540078843626807, 'pattern_counts': {'":': 60, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5292675', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1869, 'n_words': 309}