{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n", "#\n", "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# http://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License.\n", "\n", "# Preamble to mimick book environment\n", "import logging\n", "from pprint import pprint\n", "from sys import stdout as STDOUT" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 1\n", "class Field(object):\n", " def __init__(self, name):\n", " self.name = name\n", " self.internal_name = '_' + self.name\n", "\n", " def __get__(self, instance, instance_type):\n", " if instance is None: return self\n", " return getattr(instance, self.internal_name, '')\n", "\n", " def __set__(self, instance, value):\n", " setattr(instance, self.internal_name, value)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 2\n", "class Customer(object):\n", " # Class attributes\n", " first_name = Field('first_name')\n", " last_name = Field('last_name')\n", " prefix = Field('prefix')\n", " suffix = Field('suffix')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Before: '' {}\n", "After: 'Euclid' {'_first_name': 'Euclid'}\n" ] } ], "source": [ "# Example 3\n", "foo = Customer()\n", "print('Before:', repr(foo.first_name), foo.__dict__)\n", "foo.first_name = 'Euclid'\n", "print('After: ', repr(foo.first_name), foo.__dict__)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 4\n", "class Meta(type):\n", " def __new__(meta, name, bases, class_dict):\n", " for key, value in class_dict.items():\n", " if isinstance(value, Field):\n", " value.name = key\n", " value.internal_name = '_' + key\n", " cls = type.__new__(meta, name, bases, class_dict)\n", " return cls" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 5\n", "class DatabaseRow(object, metaclass=Meta):\n", " pass" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 6\n", "class Field(object):\n", " def __init__(self):\n", " # These will be assigned by the metaclass.\n", " self.name = None\n", " self.internal_name = None\n", " def __get__(self, instance, instance_type):\n", " if instance is None: return self\n", " return getattr(instance, self.internal_name, '')\n", "\n", " def __set__(self, instance, value):\n", " setattr(instance, self.internal_name, value)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 7\n", "class BetterCustomer(DatabaseRow):\n", " first_name = Field()\n", " last_name = Field()\n", " prefix = Field()\n", " suffix = Field()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Before: '' {}\n", "After: 'Euler' {'_first_name': 'Euler'}\n" ] } ], "source": [ "# Example 8\n", "foo = BetterCustomer()\n", "print('Before:', repr(foo.first_name), foo.__dict__)\n", "foo.first_name = 'Euler'\n", "print('After: ', repr(foo.first_name), foo.__dict__)" ] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }