{ "metadata": { "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.8.2-final" }, "orig_nbformat": 2, "kernelspec": { "name": "Python 3.8.2 64-bit ('stackoverflow': conda)", "display_name": "Python 3.8.2 64-bit ('stackoverflow': conda)", "metadata": { "interpreter": { "hash": "f3131ab0c53afd587e929ab5f3e0081bb3b1675889ab2c259292a9bd8773e05a" } } } }, "nbformat": 4, "nbformat_minor": 2, "cells": [ { "source": [ "# Named tuples and dataclasses" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "A quick review of named tuples (standad and typed) and dataclasses.\n", "\n", "From the [Data Science from Scratch book](https://www.oreilly.com/library/view/data-science-from/9781492041122/)." ], "cell_type": "markdown", "metadata": {} }, { "source": [ "## `namedtuple`" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "Similar to dictionaries but\n", "\n", "- fields are named\n", "- values are immutable" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from collections import namedtuple\n", "import datetime" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "StockPrice = namedtuple('StockPrice', ['symbol', 'date', 'closing_price'])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "type" ] }, "metadata": {}, "execution_count": 3 } ], "source": [ "type(StockPrice)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "StockPrice(symbol='MSFT', date=datetime.date(2018, 12, 14), closing_price=106.03)" ] }, "metadata": {}, "execution_count": 4 } ], "source": [ "price = StockPrice('MSFT', datetime.date(2018, 12, 14), 106.03)\n", "price" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "__main__.StockPrice" ] }, "metadata": {}, "execution_count": 5 } ], "source": [ "type(price)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "('MSFT', datetime.date(2018, 12, 14), 106.03)" ] }, "metadata": {}, "execution_count": 6 } ], "source": [ "price.symbol, price.date, price.closing_price" ] }, { "source": [ "## `NamedTuple`" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "Simlar to `namedtuple` but\n", "\n", "- it is a class\n", "- fields have to be typed\n", "- as it is a class, it can have methods" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from typing import NamedTuple" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "class StockPrice(NamedTuple):\n", " symbol: str\n", " date: datetime.date\n", " closing_price: float\n", "\n", " def is_high_tech(self) -> bool:\n", " return self.symbol in ['MSFT', 'GOOG', 'FB', 'AMZN', 'AAPL']" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "StockPrice(symbol='MSFT', date=datetime.date(2019, 12, 14), closing_price=106.83)" ] }, "metadata": {}, "execution_count": 9 } ], "source": [ "stockprice = StockPrice('MSFT', datetime.date(2019, 12, 14), 106.83)\n", "stockprice" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 10 } ], "source": [ "stockprice.is_high_tech()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 11 } ], "source": [ "StockPrice('TOOT', 12, 12).is_high_tech()" ] }, { "source": [ "## Dataclasses" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "Similar to `NamedTuple` but\n", "\n", "- fields are mutable" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "from dataclasses import dataclass" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "@dataclass\n", "class StockPrice2:\n", " symbol: str\n", " date: datetime.date\n", " closing_price: float\n", "\n", " def is_high_tech(self) -> bool:\n", " return self.symbol in ['MSFT', 'GOOG', 'FB', 'AMZN', 'AAPL']" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "StockPrice2(symbol='MSFt', date=datetime.date(2018, 12, 14), closing_price=106.03)" ] }, "metadata": {}, "execution_count": 14 } ], "source": [ "stockprice2 = StockPrice2('MSFt', datetime.date(2018, 12, 14), 106.03)\n", "stockprice2" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "53.015" ] }, "metadata": {}, "execution_count": 15 } ], "source": [ "stockprice2.closing_price /= 2\n", "stockprice2.closing_price" ] }, { "source": [ "For `NamedTuple` this does not work" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "output_type": "error", "ename": "AttributeError", "evalue": "can't set attribute", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mstockprice\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclosing_price\u001b[0m \u001b[0;34m/=\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: can't set attribute" ] } ], "source": [ "stockprice.closing_price /= 2" ] } ] }