Skip to content

Instantly share code, notes, and snippets.

@qingfengxia
Last active November 1, 2019 10:22
Show Gist options
  • Save qingfengxia/68b1d29837dd43d8493a38112a428c8e to your computer and use it in GitHub Desktop.
Save qingfengxia/68b1d29837dd43d8493a38112a428c8e to your computer and use it in GitHub Desktop.
python_defensive_programming.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "python_defensive_programming.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/qingfengxia/68b1d29837dd43d8493a38112a428c8e/untitled0.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tReK9XUZSbb8",
"colab_type": "text"
},
"source": [
"## Defensive programming\n",
"- make your code safer and more reliable\n",
"- your model more robust"
]
},
{
"cell_type": "code",
"metadata": {
"id": "7gSpn94-SpoD",
"colab_type": "code",
"colab": {}
},
"source": [
"import math\n",
"def mySqrt(X):\n",
" sum = 0.0\n",
" for x in X:\n",
" sum = sum + x\n",
" return math.sqrt(sum)\n",
"\n",
"mySqrt([1, 3])"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "g6RmZoevTY4W",
"colab_type": "text"
},
"source": [
"In this function, the develper has several assumpitons\n",
"+ the input data is iterable\n",
"+ the sum is greater or equal to zero\n",
"\n",
"but the users of this functions do not know this assumptions\n",
"\n",
"NOTE: *do not expect user will read the code before using it*"
]
},
{
"cell_type": "code",
"metadata": {
"id": "bURn5fgEUHjJ",
"colab_type": "code",
"colab": {}
},
"source": [
"mySqrt(10)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "Bk3J2SlkUWFM",
"colab_type": "text"
},
"source": [
"another user's input"
]
},
{
"cell_type": "code",
"metadata": {
"id": "WOZ0rkL3Uhvs",
"colab_type": "code",
"colab": {}
},
"source": [
"mySqrt([-5, 1])"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "HFb8k2L4Uxf1",
"colab_type": "text"
},
"source": [
"+ What does that error mean? \n",
"+ What is wrong with it? a bug in the function?\n",
"\n",
"What the function develope can do for better user experience?"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ByOo_oYXVLZZ",
"colab_type": "text"
},
"source": [
"Does this improved version better?"
]
},
{
"cell_type": "code",
"metadata": {
"id": "ic8r4J4wVPFo",
"colab_type": "code",
"colab": {}
},
"source": [
"def mySqrt1(X):\n",
" # accept an iterable sequence: tuple, list, 1D numpy array of numeric\n",
" sum = 0.0\n",
" for x in X:\n",
" sum = sum + x\n",
" return math.sqrt(sum)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "H1uI2BqMWpnP",
"colab_type": "text"
},
"source": [
"Is there any better approach? Yes\n",
"\n",
"Note: good library developers will do their best to prevent misusage!"
]
},
{
"cell_type": "code",
"metadata": {
"id": "xjyXh4pBW9tO",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 36
},
"outputId": "8422da0d-f962-46c7-bcba-e2d9c6a3f611"
},
"source": [
"import math\n",
"def mySqrt2(X):\n",
" # accept an iterable sequence: tuple, list, 1D numpy array of numeric\n",
" assert hasattr(X, '__getitem__') # pre-condition check\n",
" sum = 0.0\n",
" for x in X:\n",
" sum = sum + x\n",
" assert sum>=0.0 # post condition check\n",
" return math.sqrt(sum)\n",
"\n",
"\n",
"mySqrt2([81])\n",
"mySqrt2([]) # reasonable good response"
],
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0.0"
]
},
"metadata": {
"tags": []
},
"execution_count": 5
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment