Skip to content

Instantly share code, notes, and snippets.

@j6k4m8
Last active February 3, 2022 22:00
Show Gist options
  • Save j6k4m8/ff011e090b6c1c22f91cc9a52adad159 to your computer and use it in GitHub Desktop.
Save j6k4m8/ff011e090b6c1c22f91cc9a52adad159 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,
"metadata": {},
"outputs": [],
"source": [
"import subprocess\n",
"class FrogError(ValueError):\n",
" ...\n",
"\n",
"class Frog:\n",
"\n",
" def __init__(self, n=1):\n",
" self.n = n\n",
" \n",
" def talk(self):\n",
" try:\n",
" subprocess.check_output(\"say {}\".format(str(self)), shell=True)\n",
" except:\n",
" return str(self)\n",
"\n",
" def __add__(self, other):\n",
" return Frog(n=self.n + other.n)\n",
" \n",
" def __sub__(self, other):\n",
" return Frog(n=self.n - other.n)\n",
"\n",
" def __str__(self):\n",
" return \"RIBBIT \" * (self.n // 5) + \"ribit \" * (self.n % 5)\n",
" \n",
" def __repr__(self):\n",
" return \"FROG\" * (self.n // 5) + \"frog\" * (self.n % 5)\n",
" \n",
" def __mul__(self, other):\n",
" return Frog(n=self.n * other.n)\n",
" \n",
" def __truediv__(self, other):\n",
" raise FrogError(\"You may 🚨 NOT 🚨 hurt the frog\")\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"frog = Frog()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"frogfrog"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"frog + frog"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"frogfrogfrog"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"frog + frog + frog"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'RIBBIT ribit '"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"str(frog + frog * (frog + frog + frog + frog) + frog)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"megafrog = frog + frog + frog + frog + frog + frog"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FROGFROGFROGFROGFROGFROGFROGfrog"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"megafrog * megafrog"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"ename": "FrogError",
"evalue": "You may 🚨 NOT 🚨 hurt the frog",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mFrogError\u001b[0m Traceback (most recent call last)",
"Input \u001b[0;32mIn [14]\u001b[0m, in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmegafrog\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mfrog\u001b[49m\n",
"Input \u001b[0;32mIn [1]\u001b[0m, in \u001b[0;36mFrog.__truediv__\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 31\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__truediv__\u001b[39m(\u001b[38;5;28mself\u001b[39m, other):\n\u001b[0;32m---> 32\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m FrogError(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mYou may 🚨 NOT 🚨 hurt the frog\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[0;31mFrogError\u001b[0m: You may 🚨 NOT 🚨 hurt the frog"
]
}
],
"source": [
"megafrog / frog"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"megafrog.talk()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"interpreter": {
"hash": "410f6db90cc89b666adbd1b755ae7555dd227a2d7c11822f3d377845b87672a4"
},
"kernelspec": {
"display_name": "Python 3.9.7 64-bit ('scripting')",
"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.9.7"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment