{ "metadata": { "name": "", "signature": "sha256:0352470b73b74e7c85e4f85e86fee12d65fbbd4a1e28a93edffaf78162a96fe2" }, "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 }, { "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", "prompt_number": 8, "text": [ "" ] } ], "prompt_number": 8 }, { "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" ] } ], "prompt_number": 9 }, { "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" ] } ], "prompt_number": 10 }, { "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" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 } ], "metadata": {} } ] }