Skip to content

Instantly share code, notes, and snippets.

@dr-dror
Created November 10, 2024 20:28
Show Gist options
  • Save dr-dror/4564c0b38beeeb0249d4c02dabe3307f to your computer and use it in GitHub Desktop.
Save dr-dror/4564c0b38beeeb0249d4c02dabe3307f to your computer and use it in GitHub Desktop.

Revisions

  1. dr-dror created this gist Nov 10, 2024.
    277 changes: 277 additions & 0 deletions demo2.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,277 @@
    {
    "cells": [
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "from collections.abc import Sequence\n",
    "# Note that the following is deprecated:\n",
    "# https://docs.python.org/3.11/library/typing.html#typing.Sequence\n",
    "# from typing import Sequence"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "# Basic Generic type for functions"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "def get_first_element[T: (int, str)](items: Sequence[T]) -> T:\n",
    " return items[0]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "e_int = get_first_element([15, 2, 3])\n",
    "e_int"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "type(e_int)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "# Tab completion\n",
    "e_int.as_integer_ratio()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "e_str = get_first_element(( 'a ', 'b', 'c'))\n",
    "e_str"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "type(e_str)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "# Tab completion\n",
    "e_str.strip()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "(\n",
    " get_first_element([1.1, 2.2]),\n",
    " get_first_element([1.1, 2]),\n",
    " get_first_element([1, \"1\"]),\n",
    ")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "res = get_first_element([1, \"b\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "res.as_integer_ratio()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "def func2(some_input):\n",
    " return get_first_element(some_input)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "func2([1,2,3])"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "# Basic generic types for classes"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "from collections import deque\n",
    "class Queue[T]:\n",
    " def __init__(self) -> None:\n",
    " self.elements: deque[T] = deque()\n",
    "\n",
    " def push(self, element: T) -> None:\n",
    " self.elements.append(element)\n",
    "\n",
    " def pop(self) -> T:\n",
    " return self.elements.popleft()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "q_int = Queue[int]()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "q_int.push(1)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "q_int.push(2)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "q_int.pop().as_integer_ratio()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "q_str = Queue[str]()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "q_str.push(\"foo\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "q_str.push(\"1\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "q_str.push(1)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "q_str.pop()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "q_str.pop().capitalize()"
    ]
    }
    ],
    "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
    }