Skip to content

Instantly share code, notes, and snippets.

@ltiao
Created December 15, 2022 17:45
Show Gist options
  • Save ltiao/78b1001c608a5ce9120f9b1936980d69 to your computer and use it in GitHub Desktop.
Save ltiao/78b1001c608a5ce9120f9b1936980d69 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "05dc8c3d",
"metadata": {},
"outputs": [],
"source": [
"from dataclasses import dataclass\n",
"from functools import total_ordering\n",
"\n",
"@total_ordering\n",
"@dataclass(order=False)\n",
"class Token:\n",
" word: str\n",
" count: int\n",
"\n",
" def __lt__(self, other):\n",
" return self.count > other.count or self.count == other.count and self.word < other.word"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c38bf16e",
"metadata": {},
"outputs": [],
"source": [
"x = Token('foo', 3)\n",
"y = Token('bar', 2)\n",
"z = Token('baz', 2)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1b6af471",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x < y"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c4a1bc3b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y < z"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d283e31d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"z < y"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ca5512b2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x < z"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "1d4efa1e",
"metadata": {},
"outputs": [],
"source": [
"a = Token('baz', 3)\n",
"b = Token('baz', 2)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d4535844",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a < b"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "695b1b3c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b < a"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment