data-science-ipython-notebooks/core/functions.ipynb

165 lines
4.5 KiB
Plaintext
Raw Normal View History

{
"metadata": {
"name": "",
2015-01-27 03:07:37 +08:00
"signature": "sha256:93f08d982c73b7b50d2c679984dc25a00cc198f32a1a82269c2c119da5d0e26e"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Functions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Functions as Objects\n",
"* Lambdas\n",
"* Closures\n",
"* \\*args, \\*\\*kwargs\n",
"* Currying\n",
"* Generators\n",
"* Generator Expressions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Functions as Objects"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python treats functions as objects which can simplify data cleaning"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%file transform_util.py\n",
"import re\n",
"\n",
"\n",
"class TransformUtil:\n",
"\n",
" @classmethod\n",
" def remove_punctuation(cls, value):\n",
" \"\"\"Removes !, #, and ?.\n",
" \"\"\" \n",
" return re.sub('[!#?]', '', value) \n",
"\n",
" @classmethod\n",
" def clean_strings(cls, strings, ops): \n",
" \"\"\"General purpose method to clean strings.\n",
"\n",
" Pass in a sequence of strings and the operations to perform.\n",
" \"\"\" \n",
" result = [] \n",
" for value in strings: \n",
" for function in ops: \n",
" value = function(value) \n",
" result.append(value) \n",
" return result"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Overwriting transform_util.py\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%file tests/test_transform_util.py\n",
"from nose.tools import assert_equal\n",
"from ..transform_util import TransformUtil\n",
"\n",
"\n",
"class TestTransformUtil():\n",
"\n",
" states = [' Alabama ', 'Georgia!', 'Georgia', 'georgia', \\\n",
" 'FlOrIda', 'south carolina##', 'West virginia?']\n",
" \n",
" expected_output = ['Alabama',\n",
" 'Georgia',\n",
" 'Georgia',\n",
" 'Georgia',\n",
" 'Florida',\n",
" 'South Carolina',\n",
" 'West Virginia']\n",
" \n",
" def test_remove_punctuation(self):\n",
" assert_equal(TransformUtil.remove_punctuation('!#?'), '')\n",
2015-01-27 03:07:37 +08:00
" \n",
" def test_map_remove_punctuation(self):\n",
" # Map applies a function to a collection\n",
" output = map(TransformUtil.remove_punctuation, self.states)\n",
" assert_equal('!#?' not in output, True)\n",
"\n",
" def test_clean_strings(self):\n",
" clean_ops = [str.strip, TransformUtil.remove_punctuation, str.title] \n",
" output = TransformUtil.clean_strings(self.states, clean_ops)\n",
" assert_equal(output, self.expected_output)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Overwriting tests/test_transform_util.py\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"!nosetests tests/test_transform_util.py -v"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"core.tests.test_transform_util.TestTransformUtil.test_clean_strings ... ok\r\n",
2015-01-27 03:07:37 +08:00
"core.tests.test_transform_util.TestTransformUtil.test_map_remove_punctuation ... ok\r\n",
"core.tests.test_transform_util.TestTransformUtil.test_remove_punctuation ... ok\r\n",
"\r\n",
"----------------------------------------------------------------------\r\n",
2015-01-27 03:07:37 +08:00
"Ran 3 tests in 0.002s\r\n",
"\r\n",
"OK\r\n"
]
}
],
"prompt_number": 3
}
],
"metadata": {}
}
]
}