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

554 lines
12 KiB
Plaintext
Raw Normal View History

{
"metadata": {
"name": "",
"signature": "sha256:5641b782273b21293cd24c3af10e9e68d593e70c5554c4cc0e37ba2a389837e9"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Basics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* References\n",
"* Types\n",
"* Attributes and Methods\n",
"* Duck Typing Utilities\n",
"* Duck Typing Utilities Unit Tests\n",
"* Exceptions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## References"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = [1, 2, 3]\n",
"b = a\n",
"a == b"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 1,
"text": [
"True"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a is b"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"True"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"list() always creates a new list"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"c = list(a)\n",
"a == c"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"True"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a is c"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"False"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Types"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Knowing the type of an object is important, as it's useful to write functions that can handle many types of input."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"type('foo')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"str"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"isinstance() can take a type or tuple"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"isinstance(7, (int, float))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"True"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Attributes and Methods"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"List all attributes and methods"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dir('foo')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"['__add__',\n",
" '__class__',\n",
" '__contains__',\n",
" '__delattr__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__getitem__',\n",
" '__getnewargs__',\n",
" '__getslice__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__le__',\n",
" '__len__',\n",
" '__lt__',\n",
" '__mod__',\n",
" '__mul__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__rmod__',\n",
" '__rmul__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" '_formatter_field_name_split',\n",
" '_formatter_parser',\n",
" 'capitalize',\n",
" 'center',\n",
" 'count',\n",
" 'decode',\n",
" 'encode',\n",
" 'endswith',\n",
" 'expandtabs',\n",
" 'find',\n",
" 'format',\n",
" 'index',\n",
" 'isalnum',\n",
" 'isalpha',\n",
" 'isdigit',\n",
" 'islower',\n",
" 'isspace',\n",
" 'istitle',\n",
" 'isupper',\n",
" 'join',\n",
" 'ljust',\n",
" 'lower',\n",
" 'lstrip',\n",
" 'partition',\n",
" 'replace',\n",
" 'rfind',\n",
" 'rindex',\n",
" 'rjust',\n",
" 'rpartition',\n",
" 'rsplit',\n",
" 'rstrip',\n",
" 'split',\n",
" 'splitlines',\n",
" 'startswith',\n",
" 'strip',\n",
" 'swapcase',\n",
" 'title',\n",
" 'translate',\n",
" 'upper',\n",
" 'zfill']"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Access help"
]
},
2015-01-25 18:47:14 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"help(str.upper)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Help on method_descriptor:\n",
"\n",
"upper(...)\n",
" S.upper() -> string\n",
" \n",
" Return a copy of the string S converted to uppercase.\n",
"\n"
]
}
],
2015-01-25 19:02:30 +08:00
"prompt_number": 8
2015-01-25 18:47:14 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Access an attribute or method by name getattr, hasattr, and setattr can be used to write generic, reusable code"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"getattr('foo', 'split')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 19:02:30 +08:00
"prompt_number": 9,
"text": [
"<function split>"
]
}
],
2015-01-25 19:02:30 +08:00
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Duck Typing Utilities"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%file type_util.py\n",
"class TypeUtil:\n",
"\n",
" @classmethod\n",
" def is_iterable(cls, obj):\n",
" \"\"\"Determines if obj is iterable.\n",
"\n",
" Useful when writing functions that can accept multiple types of\n",
" input (list, tuple, ndarray, iterator). Pairs well with\n",
" convert_to_list.\n",
" \"\"\"\n",
" try:\n",
" iter(obj)\n",
" return True\n",
" except TypeError:\n",
" return False\n",
"\n",
" @classmethod\n",
" def convert_to_list(cls, obj):\n",
" \"\"\"Converts obj to a list if it is not a list and it is iterable, \n",
" else returns the original obj.\n",
" \"\"\"\n",
" if not isinstance(obj, list) and cls.is_iterable(obj):\n",
" obj = list(obj)\n",
" return obj\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Overwriting type_util.py\n"
]
}
],
2015-01-25 19:02:30 +08:00
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Duck Typing Utilities Unit Tests"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%file tests/test_type_util.py\n",
"from nose.tools import assert_equal\n",
"from pydatasnippets.core.type_util import TypeUtil\n",
"\n",
"\n",
"class TestUtil():\n",
"\n",
" def test_is_iterable(self):\n",
" assert_equal(TypeUtil.is_iterable('foo'), True)\n",
" assert_equal(TypeUtil.is_iterable(7), False)\n",
"\n",
" def test_convert_to_list(self):\n",
" assert_equal(isinstance(TypeUtil.convert_to_list('foo'), list), True)\n",
" assert_equal(isinstance(TypeUtil.convert_to_list(7), list), False)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Overwriting tests/test_type_util.py\n"
]
}
],
2015-01-25 19:02:30 +08:00
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"!nosetests tests/test_type_util.py"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"..\r\n",
"----------------------------------------------------------------------\r\n",
"Ran 2 tests in 0.001s\r\n",
"\r\n",
"OK\r\n"
]
}
],
2015-01-25 18:47:14 +08:00
"prompt_number": 12
},
2015-01-25 18:48:18 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exceptions"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def convert_to_float(x):\n",
" try:\n",
" x = float(x)\n",
" except ValueError:\n",
" print('Failed')\n",
" else: \n",
" # Executes only when try succeeds\n",
" print('Succeeded')\n",
" finally: \n",
" # Always executes\n",
" print('Completed convert_to_float')\n",
" return x"
],
"language": "python",
"metadata": {},
"outputs": [],
2015-01-25 19:02:30 +08:00
"prompt_number": 13
2015-01-25 18:48:18 +08:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = convert_to_float('7')\n",
"x"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Succeeded\n",
"Completed convert_to_float\n"
]
},
{
"metadata": {},
"output_type": "pyout",
2015-01-25 19:02:30 +08:00
"prompt_number": 14,
2015-01-25 18:48:18 +08:00
"text": [
"7.0"
]
}
],
2015-01-25 19:02:30 +08:00
"prompt_number": 14
2015-01-25 18:48:18 +08:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = convert_to_float('a')\n",
"x"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Failed\n",
"Completed convert_to_float\n"
]
},
{
"metadata": {},
"output_type": "pyout",
2015-01-25 19:02:30 +08:00
"prompt_number": 15,
2015-01-25 18:48:18 +08:00
"text": [
"'a'"
]
}
],
2015-01-25 19:02:30 +08:00
"prompt_number": 15
}
],
"metadata": {}
}
]
}