Skip to content

Instantly share code, notes, and snippets.

@alexwhittemore
Created August 27, 2018 21:13
Show Gist options
  • Save alexwhittemore/29e9ee51025efcf549528941ff104c9d to your computer and use it in GitHub Desktop.
Save alexwhittemore/29e9ee51025efcf549528941ff104c9d to your computer and use it in GitHub Desktop.
Prototyping a new class using a Jupyter notebook
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# AdderClass\n",
"## A dumb class to demonstrate how one might prototype using Jupyter"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We don't actually need this cell for this example, but I want to demnonstrate how you might separate tasks into different cells to avoid re-running code unnecessarily while monkeying around."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we'll define a new class. It's kind of stupid, but its complexity isn't the point. It stores two numbers, both 0 if not specified on init. When you call an instance's \"add_nums()\" method, it'll return the sum of them."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"class AdderClass:\n",
" def __init__(self, a=0, b=0):\n",
" self.a = a\n",
" self.b = b\n",
" \n",
" def add_nums(self):\n",
" return self.a+self.b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we'll try using this class."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n"
]
}
],
"source": [
"my_nums = AdderClass()\n",
"# This should return 0:\n",
"print my_nums.add_nums()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
}
],
"source": [
"my_nums = AdderClass(1,3)\n",
"# This time, it should return 4:\n",
"print my_nums.add_nums()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"At this point, we can re-work any cell individually and re-run that one bit rather than re-running the whole thing each time. For such a simple example as this, there's not a huge advantage. But maybe your script downloads tens of megabytes of data at one point - that bit works, but takes time and resources you don't want to waste constantly re-running known-good code. Rather than building infrastructure to mock that part out with local data, notebook prototyping might be an easier, faster way to move quickly, focusing only on what doesn't work yet. "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment