{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from datashape import dshape, discover\n", "from datashape.dispatch import dispatch\n", "from odo import convert, append, odo" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "class TypedList(list):\n", " \"\"\"A list with type checked values.\n", " \n", " Paramaters\n", " ----------\n", " dtype : dshape\n", " The type of the valus in the list.\n", " data : iterable of dtype, optional\n", " The values to initialize the list with.\n", " \"\"\"\n", " def __init__(self, dtype, data=None):\n", " super().__init__(())\n", " self.dtype = dshape(dtype).measure\n", " if data:\n", " self.extend(data)\n", "\n", " def _checktype(self, value):\n", " if discover(value) != self.dtype:\n", " raise TypeError(\n", " \"value '%s' is of type '%s', not type '%s'\" % (\n", " value,\n", " discover(value).measure,\n", " self.dtype,\n", " ),\n", " )\n", "\n", " def __setitem__(self, idx, value):\n", " self._checktype(value)\n", " super().__setitem__(idx, value)\n", "\n", " def append(self, value):\n", " self._checktype(value)\n", " super().append(value)\n", "\n", " def extend(self, vs):\n", " for v in vs:\n", " self.append(v)\n", "\n", " def __repr__(self):\n", " return '%s::%s' % (super().__repr__(), self.dtype)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]::int64" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tl = TypedList('int64', [1, 2, 3])\n", "tl" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "value 'a' is of type 'string', not type 'int64'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\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[0mtl\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'a'\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;36mappend\u001b[0;34m(self, value)\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 31\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 32\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_checktype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 33\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 34\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36m_checktype\u001b[0;34m(self, value)\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0mdiscover\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmeasure\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 23\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 24\u001b[0m ),\n\u001b[1;32m 25\u001b[0m )\n", "\u001b[0;31mTypeError\u001b[0m: value 'a' is of type 'string', not type 'int64'" ] } ], "source": [ "tl.append('a')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 3]::int64" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tl.append(3)\n", "tl" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "@dispatch(TypedList)\n", "def discover(tl):\n", " return len(tl) * tl.dtype" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dshape(\"4 * int64\")" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "discover(tl)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "@convert.register(TypedList, list)\n", "def list_to_typed_list(ds, dshape=None, **kwargs):\n", " if dshape is None:\n", " dshape = discover(ds).measure.measure\n", " return TypedList(dshape, ds)\n", "\n", "\n", "@convert.register(list, TypedList)\n", "def list_to_typed_list(ds, **kwargs):\n", " return list(ds)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 3]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "odo(tl, list)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3}" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "odo(tl, set)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3, 3)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "odo(tl, tuple)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 3])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "odo(tl, np.ndarray)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.2, 1.3, 1.4])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "odo(TypedList('float64', [1.2, 1.3, 1.4]), np.ndarray)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]::int64" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "odo([1, 2, 3], TypedList, dtype='int64')" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]::int64" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "odo(np.array([1, 2, 3]), TypedList('int64'))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@append.register(list, TypedList)\n", "def append_list_to_typed_list(ds, tl, **kwargs):\n", " tl.extend(ds)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 3, 1, 2, 3]::int64" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "odo([1, 2, 3], tl)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 3, 1, 2, 3]::int64" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tl" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }