Last active
June 21, 2016 17:36
-
-
Save burkestar/39d8041705b55b7c4fc46969523a756c to your computer and use it in GitHub Desktop.
Extensible API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 20, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "creating Project\n", | |
| "constructing Project data\n", | |
| "creating AdminProject\n", | |
| "creating Project\n", | |
| "calling super construct_data\n", | |
| "constructing Project data\n", | |
| "updating with additional data\n", | |
| "{'version': '1.2', 'name': 'test', 'special': 100}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "class Project(object):\n", | |
| " def __init__(self, data=None):\n", | |
| " if data:\n", | |
| " self.data = data\n", | |
| "\n", | |
| " @classmethod\n", | |
| " def _construct_data(cls, *args, **kwargs):\n", | |
| " print('constructing Project data')\n", | |
| " return {\n", | |
| " 'name': kwargs.get('name'),\n", | |
| " 'version': kwargs.get('version')\n", | |
| " }\n", | |
| "\n", | |
| " @classmethod\n", | |
| " def create(cls, name, version=None):\n", | |
| " print('creating Project')\n", | |
| " data = cls._construct_data(name=name, version=version)\n", | |
| " return cls(data)\n", | |
| " \n", | |
| "import collections\n", | |
| " \n", | |
| "class AdminProject(Project):\n", | |
| "\n", | |
| " @classmethod\n", | |
| " def _construct_data(cls, *args, **kwargs):\n", | |
| " print('calling super construct_data')\n", | |
| " data = super(AdminProject, cls)._construct_data(*args, **kwargs)\n", | |
| " if cls.extra and isinstance(cls.extra, collections.Iterable):\n", | |
| " data.update(cls.extra)\n", | |
| " print('updating with additional data')\n", | |
| " print(data)\n", | |
| " return data\n", | |
| "\n", | |
| " @classmethod\n", | |
| " def create(cls, name, version=None, **kwargs):\n", | |
| " print('creating AdminProject')\n", | |
| " cls.extra = kwargs.get('extra')\n", | |
| " return super(AdminProject, cls).create(name, version)\n", | |
| " \n", | |
| "\n", | |
| "project = Project.create('testProject', version='1.0')\n", | |
| "admin = AdminProject.create('test', version='1.2', extra={'special': 100})" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 38, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'name': 'testProject', 'version': '1.0'}" | |
| ] | |
| }, | |
| "execution_count": 38, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "project.data" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 25, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'name': 'test', 'special': 100, 'version': '1.2'}" | |
| ] | |
| }, | |
| "execution_count": 25, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "admin.data" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 21, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "creating Project\n", | |
| "constructing Project data\n", | |
| "my project\n" | |
| ] | |
| }, | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "__main__.Project" | |
| ] | |
| }, | |
| "execution_count": 21, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "class Multi(object):\n", | |
| " def __init__(self, klass=Project):\n", | |
| " self.klass = klass\n", | |
| " \n", | |
| " def do_it(self, name, **kwargs):\n", | |
| " self.project = self.klass.create(name, version='2.0', **kwargs)\n", | |
| "\n", | |
| "m = Multi()\n", | |
| "m.do_it('my project')\n", | |
| "print(m.project.data['name'])\n", | |
| "type(m.project)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 23, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "creating AdminProject\n", | |
| "creating Project\n", | |
| "calling super construct_data\n", | |
| "constructing Project data\n", | |
| "updating with additional data\n", | |
| "{'version': '2.0', 'name': 'my project 2', 'special': 100}\n", | |
| "my project 2\n", | |
| "{'version': '2.0', 'name': 'my project 2', 'special': 100}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "m2 = Multi(klass=AdminProject)\n", | |
| "m2.do_it('my project 2', extra={'special': 100})\n", | |
| "print(m2.project.data['name'])\n", | |
| "type(m2.project)\n", | |
| "print(m2.project.data)" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 2", | |
| "language": "python", | |
| "name": "python2" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 2 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython2", | |
| "version": "2.7.6" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment