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

618 lines
13 KiB
Plaintext
Raw Normal View History

{
"metadata": {
"name": "",
2015-01-25 19:02:30 +08:00
"signature": "sha256:13e6414e26c196bc6cbaf0093750187f11c2637e20975d01f8e38416d518abb0"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Basics"
]
},
{
"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": "code",
"collapsed": false,
"input": [
"# list always creates a new list\n",
"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": "code",
"collapsed": false,
"input": [
"# Knowing the type of an object is important, as it's useful to write \n",
"# functions that can handle many types of input.\n",
"type('foo')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"str"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# isinstance can take a type or tuple\n",
"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": "code",
"collapsed": false,
"input": [
"# List all attributes and methods\n",
"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
},
2015-01-25 18:47:14 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Access help\n",
"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": "code",
"collapsed": false,
"input": [
"# Access an attribute or method by name\n",
"# getattr, hasattr, and setattr can be used to write generic, reusable code\n",
"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 Util:\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, else\n",
" 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 Util\n",
"\n",
"\n",
"class TestUtil():\n",
"\n",
" def test_is_iterable(self):\n",
" assert_equal(Util.is_iterable('foo'), True)\n",
" assert_equal(Util.is_iterable(7), False)\n",
"\n",
" def test_convert_to_list(self):\n",
" assert_equal(isinstance(Util.convert_to_list('foo'), list), True)\n",
" assert_equal(isinstance(Util.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
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## range and xrange"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Note: range is no longer avalable in Python 3\n",
"# Generate a list of evenly spaced integers\n",
"range(10)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 16,
"text": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
]
}
],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Range can take start, stop, and step arguments\n",
"range(0, 20, 3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 17,
"text": [
"[0, 3, 6, 9, 12, 15, 18]"
]
}
],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# It is very common to iterate through sequences by index with range\n",
"seq = [1, 2, 3]\n",
"for i in range(len(seq)):\n",
" val = seq[i]\n",
" print(val)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# xrange is recommended for longer ranges and is available in Python 3 as \n",
"# range. It returns an iterator that generates integers one by one rather\n",
"# than all at once and storing them in a large list.\n",
"sum = 0\n",
"for i in xrange(100000):\n",
" if i % 2 == 0:\n",
" sum += 1\n",
"print(sum)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"50000\n"
]
}
],
"prompt_number": 19
2015-01-25 18:48:18 +08:00
},
2015-01-25 19:02:30 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 19
}
],
"metadata": {}
}
]
}