From: Jon Speicher Date: Sat, 27 Jul 2013 20:32:24 +0000 (-0400) Subject: Finish up the section on TDD X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=6e68183e012fff892f50fc35ba3873b0abedf0d3;p=swc-testing-nose.git Finish up the section on TDD --- diff --git a/python/sw_engineering/SoftwareEngineering.ipynb b/python/sw_engineering/SoftwareEngineering.ipynb index 6c714f1..bdfd763 100644 --- a/python/sw_engineering/SoftwareEngineering.ipynb +++ b/python/sw_engineering/SoftwareEngineering.ipynb @@ -1890,31 +1890,139 @@ "* A list of all zeros\n", "* Integers with an integral mean\n", "* Integers with a non-integral mean\n", - "* Floating point numbers\n", + "* Floating point numbers" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Results\n", "\n", - "TBD: empty list? exception?" + "You probably wound up with something like this." ] }, { "cell_type": "code", "collapsed": false, "input": [ - "def calc_mean(ilist):\n", - " '''\n", - " returns the mean of input list\n", - " '''\n", - " if len(ilist) == 0:\n", - " raise ValueError('Input is empty.')\n", - " \n", + "def mean(numbers):\n", + " '''Returns the mean of the provided list of numbers.'''\n", " total = 0.0\n", - " for num in ilist:\n", - " total = total + num\n", - " return total/float(len(ilist))" + " for number in numbers:\n", + " total = total + number\n", + " return total/float(len(numbers))" ], "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 27 + "prompt_number": 1 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How good was your testing?" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "mean([0])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "pyout", + "prompt_number": 2, + "text": [ + "0.0" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "mean([1])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "pyout", + "prompt_number": 5, + "text": [ + "1.0" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "mean([1, 2, 3, 4, 5])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "pyout", + "prompt_number": 3, + "text": [ + "3.0" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "mean([2.7, 3.8, 7.2])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "pyout", + "prompt_number": 4, + "text": [ + "4.566666666666666" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "mean([])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "ZeroDivisionError", + "evalue": "float division by zero", + "output_type": "pyerr", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mmean\u001b[0;34m(numbers)\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnumber\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mnumbers\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mtotal\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtotal\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mnumber\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mtotal\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumbers\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mZeroDivisionError\u001b[0m: float division by zero" + ] + } + ], + "prompt_number": 6 }, { "cell_type": "code",