Skip to content

Instantly share code, notes, and snippets.

@ischurov
Created July 8, 2020 21:11
Show Gist options
  • Save ischurov/790cf62eaed546cf1ab0ad1c0456b34c to your computer and use it in GitHub Desktop.
Save ischurov/790cf62eaed546cf1ab0ad1c0456b34c to your computer and use it in GitHub Desktop.
minimalistic-pipes-in-python.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "## Minimalistic pipes in Python\nResembles `dplyr`'s `%>%`.\n\nBecause we can."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "class StopPipeException(Exception):\n pass\n\nclass Pipe(object):\n def __init__(self, x):\n self.x = x\n def __rshift__(self, f):\n try:\n return Pipe(f(self.x))\n except StopPipeException:\n return self.x\n\ndef stop(x):\n raise StopPipeException",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "from math import sin, sqrt",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Pipe(3) >> sin >> sqrt >> stop",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "0.3756594309475901"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "py3.6",
"display_name": "Python 3.6",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.1",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "minimalistic-pipes-in-python.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment