{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Generators" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "45" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# a generator that yields items instead of returning a list\n", "def firstn(n):\n", " num = 0\n", " while num < n:\n", " yield num\n", " num += 1\n", " \n", "sum(firstn(10))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[i for i in firstn(10)]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def unique(iterable, key=lambda x: x):\n", " seen = set()\n", " for elem, ekey in ((e, key(e)) for e in iterable):\n", " if ekey not in seen:\n", " yield elem\n", " seen.add(ekey)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[i for i in unique(range(10))]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Example(object):\n", " def __init__(self):\n", " self.my_string = \"hello\"\n", " self.my_set = {1,2,3}\n", "class Example2(object):\n", " def __init__(self):\n", " self.my_list = [4,5,6]\n", " self.my_class_instance = Example()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def inspectObject(obj):\n", " \"\"\"Returns a list of object attributes and their types.\n", "\n", " Parameters\n", " ----------\n", " obj: :obj:\n", " Any kind of object that is not a builtin.\n", "\n", " Returns\n", " -------\n", " obj_ids: list\n", " A list of lists to be specific.\n", " \"\"\"\n", " # Make list of builtins\n", " bi_list = dir(__builtins__)\n", " # Try to make a list of the types of things inside obj.\n", " try:\n", " # Make list all things inside of an object\n", " obj_ids = [[name, type(thing).__name__] for name, thing in obj.__dict__.items()]\n", "\n", " return obj_ids\n", "\n", " except Exception:\n", " pass\n", "\n", "\n", "def objectWalker(obj, desired_type=None, att_list=None):\n", " \"\"\"Recursively walks through an object an genrates a list of its attributes.\n", "\n", " Parameters\n", " ----------\n", " obj: :obj:\n", "\n", " desired_type: str\n", "\n", " Returns\n", " -------\n", " att_list: list\n", "\n", " \"\"\"\n", " # Create an attributes list if none has been asigned.\n", " if att_list is None:\n", " att_list = []\n", "\n", " # Create list of types that we do not want yo serch through\n", " no_list = dir(__builtins__)\n", " no_list.append(\"DataFrame\")\n", " # desired_type = desired_type or \"DataFrame\"\n", "\n", " obj_inspect = inspectObject(obj)\n", "\n", " for i in obj_inspect:\n", "\n", " # Test if object attribute is desired_type.\n", " if re.search(desired_type, i[1], re.I) is not None:\n", "\n", " att_list.append(i)\n", "\n", " # If the object attribute is not the desired_type then...\n", " elif i[1] not in no_list:\n", " # Try to use recursively objectWalker on the object.\n", " try:\n", " objectWalker(obj.__dict__[i[0]], desired_type, att_list)\n", " except:\n", " pass\n", " return att_list" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "ex = Example2()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[['my_set', 'set']]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "objectWalker(ex,\"set\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[['my_string', 'str']]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "objectWalker(ex,\"str\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[['my_class_instance', 'Example']]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "objectWalker(ex,\"Example\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [Root]", "language": "python", "name": "Python [Root]" }, "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.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }