Skip to content

Instantly share code, notes, and snippets.

@mpkocher
Last active October 30, 2018 22:48
Show Gist options
  • Save mpkocher/9cc93a634f027d15c230a66b3ebe8632 to your computer and use it in GitHub Desktop.
Save mpkocher/9cc93a634f027d15c230a66b3ebe8632 to your computer and use it in GitHub Desktop.
Almond Example Notebook
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example Using Almond"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[36malpha\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m1234\u001b[39m\n",
"\u001b[36mbeta\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"a-value\"\u001b[39m\n",
"\u001b[36mgamma\u001b[39m: \u001b[32mRange\u001b[39m = \u001b[33mRange\u001b[39m(\u001b[32m0\u001b[39m, \u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m, \u001b[32m4\u001b[39m, \u001b[32m5\u001b[39m, \u001b[32m6\u001b[39m, \u001b[32m7\u001b[39m, \u001b[32m8\u001b[39m, \u001b[32m9\u001b[39m)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"val alpha = 1234\n",
"val beta = \"a-value\"\n",
"val gamma = (0 until 10).toSeq"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[36mres1\u001b[39m: \u001b[32mRange\u001b[39m = \u001b[33mRange\u001b[39m(\u001b[32m0\u001b[39m, \u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m, \u001b[32m4\u001b[39m, \u001b[32m5\u001b[39m, \u001b[32m6\u001b[39m, \u001b[32m7\u001b[39m, \u001b[32m8\u001b[39m, \u001b[32m9\u001b[39m)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gamma"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"defined \u001b[32mclass\u001b[39m \u001b[36mRecord\u001b[39m"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"case class Record(alpha: Int, beta: Int, gamma: Int)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[36mrecords\u001b[39m: \u001b[32mcollection\u001b[39m.\u001b[32mimmutable\u001b[39m.\u001b[32mIndexedSeq\u001b[39m[\u001b[32mRecord\u001b[39m] = \u001b[33mVector\u001b[39m(\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m0\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m2\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m3\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m4\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m5\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m6\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m7\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m8\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m9\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m)\n",
")"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"val records = (0 until 10).map(i => Record(i, 2, 3))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[36mevenRecords\u001b[39m: \u001b[32mcollection\u001b[39m.\u001b[32mimmutable\u001b[39m.\u001b[32mIndexedSeq\u001b[39m[\u001b[32mRecord\u001b[39m] = \u001b[33mVector\u001b[39m(\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m0\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m2\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m4\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m6\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m8\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m)\n",
")\n",
"\u001b[36msomeRecords\u001b[39m: \u001b[32mcollection\u001b[39m.\u001b[32mimmutable\u001b[39m.\u001b[32mIndexedSeq\u001b[39m[\u001b[32mRecord\u001b[39m] = \u001b[33mVector\u001b[39m(\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m0\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m)\n",
")"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"val evenRecords = records.filter(r => (r.alpha % 2 == 0))\n",
"val someRecords = records.slice(0,2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[36mres5\u001b[39m: \u001b[32mcollection\u001b[39m.\u001b[32mimmutable\u001b[39m.\u001b[32mIndexedSeq\u001b[39m[\u001b[32mRecord\u001b[39m] = \u001b[33mVector\u001b[39m(\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m0\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m2\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m3\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m4\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m5\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m6\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m7\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m8\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m),\n",
" \u001b[33mRecord\u001b[39m(\u001b[32m9\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m)\n",
")"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"records // Does Almond have utils for generating a Html view of these common cases (e.g., Seq[T])?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Session Info\n",
"\n",
"Given there are a bunch of moving pieces, having a session info dump would be useful for filing bug reports. \n",
"\n",
"Inspired by [sessionInfo in R](https://stat.ethz.ch/R-manual/R-devel/library/utils/html/sessionInfo.html)."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[32mimport \u001b[39m\u001b[36malmond.api.Properties\n",
"\u001b[39m\n",
"\u001b[32mimport \u001b[39m\u001b[36mammonite.ops._\n",
"\u001b[39m\n",
"\u001b[32mimport \u001b[39m\u001b[36mammonite.ops.ImplicitWd._\n",
"\u001b[39m\n",
"\u001b[32mimport \u001b[39m\u001b[36mjava.time.LocalDate\u001b[39m"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import almond.api.Properties\n",
"import ammonite.ops._\n",
"import ammonite.ops.ImplicitWd._\n",
"import java.time.LocalDate"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"defined \u001b[32mfunction\u001b[39m \u001b[36mgetPythonVersion\u001b[39m\n",
"defined \u001b[32mfunction\u001b[39m \u001b[36mgetJupyterCoreVersion\u001b[39m"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def getPythonVersion(): Option[String] = {\n",
" val result = %%python(\"--version\")\n",
" result.err.lines.headOption.map(_.split(\"::\")).flatMap(_.headOption).map(_.trim)\n",
"}\n",
"\n",
"def getJupyterCoreVersion(): Option[String] = {\n",
" val result = %%python(\"-c\", \"import jupyter_core as J; print J.__version__\")\n",
" result.out.lines.headOption\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[36mnow\u001b[39m: \u001b[32mLocalDate\u001b[39m = 2018-10-30\n",
"\u001b[36mscalaVersion\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"2.12.7\"\u001b[39m\n",
"\u001b[36mjavaVersion\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"1.8.0_144\"\u001b[39m\n",
"\u001b[36malmondVersion\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"0.1.10+2c71b6f\"\u001b[39m\n",
"\u001b[36mpythonVersion\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"Python 2.7.14\"\u001b[39m\n",
"\u001b[36mjupyterCoreVersion\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"4.4.0\"\u001b[39m"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"val now = LocalDate.now()\n",
"val scalaVersion = scala.util.Properties.scalaPropOrElse(\"version.number\", \"unknown\")\n",
"val javaVersion = System.getProperty(\"java.version\") \n",
"val almondVersion = s\"${Properties.version}+${Properties.commitHash.take(7)}\"\n",
"val pythonVersion = getPythonVersion().getOrElse(\"unknown\")\n",
"val jupyterCoreVersion = getJupyterCoreVersion().getOrElse(\"unknown\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Scala",
"language": "scala",
"name": "scala"
},
"language_info": {
"codemirror_mode": "text/x-scala",
"file_extension": ".scala",
"mimetype": "text/x-scala",
"name": "scala",
"nbconvert_exporter": "script",
"version": "2.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment