Skip to content

Instantly share code, notes, and snippets.

@saulshanabrook
Created November 25, 2019 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saulshanabrook/cb17a191ce4a8f4196e0a1a63c2b4e96 to your computer and use it in GitHub Desktop.
Save saulshanabrook/cb17a191ce4a8f4196e0a1a63c2b4e96 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"import typing"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"T = typing.TypeVar(\"T\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"L = typing.List[typing.List[T]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def f() -> L[int]:\n",
" return [[1, 2, 3]]"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class Column:\n",
" def mean(self) -> int:\n",
" return 0\n",
"\n",
"\n",
"class GeoColumn(Column):\n",
" def length(self) -> int:\n",
" return 0"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"class Dataframe:\n",
" def __init__(self, cols):\n",
" self.cols = cols\n",
"\n",
" def __getattr__(self, name):\n",
" return self.cols[name]\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"d = Dataframe({\"name\": Column(), \"location\": GeoColumn()})"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.location.length()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"T = TypeVar(\"T\", bound=Dict[str, Column])\n",
"\n",
"K = TypeVar(\"K\", bound=KeyOf[T])\n",
"\n",
"\n",
"class Dataframe(Generic[T]):\n",
" def __init__(self, cols: T):\n",
" self.cols = cols\n",
"\n",
" def __getattr__(self, name: K) -> GetItem[T, K]:\n",
" return self.cols[name]"
]
}
],
"metadata": {
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment