Skip to content

Instantly share code, notes, and snippets.

@dr-dror
Created September 26, 2024 14:27
Show Gist options
  • Save dr-dror/9e29288e97f90ea6d2ccbdb71641470d to your computer and use it in GitHub Desktop.
Save dr-dror/9e29288e97f90ea6d2ccbdb71641470d to your computer and use it in GitHub Desktop.

Revisions

  1. dr-dror created this gist Sep 26, 2024.
    147 changes: 147 additions & 0 deletions demo.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,147 @@
    {
    "cells": [
    {
    "cell_type": "code",
    "execution_count": 1,
    "metadata": {},
    "outputs": [],
    "source": [
    "from dataclasses import dataclass"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 9,
    "metadata": {},
    "outputs": [],
    "source": [
    "@dataclass(order=True)\n",
    "class MyModelDC:\n",
    " name: str\n",
    " age: int\n",
    " email: str"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "Create two instances with the same attributes"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 12,
    "metadata": {},
    "outputs": [],
    "source": [
    "dc1 = MyModelDC(name=\"Peter Pen\", age=\"Unknown\", email=\"[email protected]\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 4,
    "metadata": {},
    "outputs": [],
    "source": [
    "dc1a = MyModelDC(name=\"Peter Pen\", age=\"Unknown\", email=\"[email protected]\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "dc1 == dc1a"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "Create some more instances"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 13,
    "metadata": {},
    "outputs": [],
    "source": [
    "dc2 = MyModelDC(name=\"Capt. Hook\", age=67, email=\"[email protected]\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 14,
    "metadata": {},
    "outputs": [],
    "source": [
    "dc2a = MyModelDC(name=\"Capt. Hook\", age=7, email=\"[email protected]\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 15,
    "metadata": {},
    "outputs": [],
    "source": [
    "dc3 = MyModelDC(name=\"Wendy Darling \", age=12, email=\"[email protected]\")"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "Sort the instances"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "sorted([dc1, dc3, dc2a, dc2])"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "Note the type!"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "type(dc1.age), type(dc3.age)"
    ]
    }
    ],
    "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.2"
    }
    },
    "nbformat": 4,
    "nbformat_minor": 2
    }