{
 "metadata": {
  "name": "",
  "signature": "sha256:aed4cbb1750b5cf45152f9d0ded2a9f36978f3177285c38b159fb79a8b0d35e4"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 20: Namespaces and Other Advanced Topics<h1>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.1, Page Number:474<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "\n",
      "class CounterNameSpace:\n",
      "    upperbound=None\n",
      "    lowerbound=None\n",
      "    class counter:\n",
      "        def __init__(self,n):\n",
      "            if n<=CounterNameSpace.upperbound:\n",
      "                self.count=n\n",
      "            else:\n",
      "                self.count=CounterNameSpace.upperbound\n",
      "        def reset(self,n):\n",
      "            if n<=CounterNameSpace.upperbound:\n",
      "                self.count=n\n",
      "        def run(self):\n",
      "            if self.count>CounterNameSpace.lowerbound:\n",
      "                self.count-=1\n",
      "                return self.count\n",
      "            else:\n",
      "                return CounterNameSpace.lowerbound\n",
      "            \n",
      "CounterNameSpace.upperbound=100\n",
      "CounterNameSpace.lowerbound=0\n",
      "\n",
      "\n",
      "ob1=CounterNameSpace.counter(10)\n",
      "while True:\n",
      "    i=ob1.run()\n",
      "    print i,\n",
      "    if i<=CounterNameSpace.lowerbound:\n",
      "        break        \n",
      "print\n",
      "\n",
      "\n",
      "ob2=CounterNameSpace.counter(20)\n",
      "while True:\n",
      "    i=ob2.run()\n",
      "    print i,\n",
      "    if i<=CounterNameSpace.lowerbound:\n",
      "        break   \n",
      "print\n",
      "\n",
      "\n",
      "ob2.reset(100)\n",
      "CounterNameSpace.lowerbound=90\n",
      "while True:\n",
      "    i=ob2.run()\n",
      "    print i,\n",
      "    if i<=CounterNameSpace.lowerbound:\n",
      "        break   "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "9 8 7 6 5 4 3 2 1 0\n",
        "19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0\n",
        "99 98 97 96 95 94 93 92 91 90\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.2, Page Number:476<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "class CounterNameSpace:\n",
      "    upperbound=None\n",
      "    lowerbound=None\n",
      "    class counter:\n",
      "        def __init__(self,n):\n",
      "            if n<=c.upperbound:\n",
      "                self.count=n\n",
      "            else:\n",
      "                self.count=c.upperbound\n",
      "        def reset(self,n):\n",
      "            if n<=c.upperbound:\n",
      "                self.count=n\n",
      "        def run(self):\n",
      "            if self.count>c.lowerbound:\n",
      "                self.count-=1\n",
      "                return self.count\n",
      "            else:\n",
      "                return c.lowerbound\n",
      "\n",
      "#Use only upperbound using c\n",
      "c=CounterNameSpace()\n",
      "c.upperbound=100\n",
      "CounterNameSpace.lowerbound=0\n",
      "\n",
      "\n",
      "ob1=CounterNameSpace.counter(10)\n",
      "while True:\n",
      "    i=ob1.run()\n",
      "    print i,\n",
      "    if i<=CounterNameSpace.lowerbound:\n",
      "        break        \n",
      "print\n",
      "\n",
      "#Now use entre CounterName Space using c\n",
      "\n",
      "ob2=c.counter(20)\n",
      "while True:\n",
      "    i=ob2.run()\n",
      "    print i,\n",
      "    if i<=c.lowerbound:\n",
      "        break   \n",
      "print\n",
      "\n",
      "\n",
      "ob2.reset(100)\n",
      "c.lowerbound=90\n",
      "while True:\n",
      "    i=ob2.run()\n",
      "    print i,\n",
      "    if i<=c.lowerbound:\n",
      "        break   "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "9 8 7 6 5 4 3 2 1 0\n",
        "19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0\n",
        "99 98 97 96 95 94 93 92 91 90\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.3, Page Number:479<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "#User-input\n",
      "print \"Enter a number:\"\n",
      "val= 10.00\n",
      "\n",
      "#Result\n",
      "print \"This is your number:\",val"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a number:\n",
        "This is your number: 10.0\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.4, Page Number:479<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "import sys\n",
      "\n",
      "#User-input\n",
      "sys.stdout.write(\"Enter a number:\")\n",
      "val= 10.00\n",
      "\n",
      "#Result\n",
      "sys.stdout.write(\"\\nThis is your number: \")\n",
      "sys.stdout.write(str(val))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a number:\n",
        "This is your number: 10.0"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.5, Page Number:479<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from sys import stdout\n",
      "\n",
      "#User-input\n",
      "stdout.write(\"Enter a number:\")\n",
      "val= 10.00\n",
      "\n",
      "#Result\n",
      "stdout.write(\"\\nThis is your number: \")\n",
      "stdout.write(str(val))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a number:\n",
        "This is your number: 10.0"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.6, Page Number:480<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "def vline(i):\n",
      "    for j in xrange(i,0,-1):\n",
      "        print \"|\"\n",
      "def hline(i):\n",
      "    for j in xrange(i,0,-1):\n",
      "        print \"-\",\n",
      "    print \n",
      "\n",
      "p=vline      #p points to vline\n",
      "p(4)         #call vline()\n",
      "\n",
      "p=hline      #p now points to hline\n",
      "p(3)         #call hline"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "|\n",
        "|\n",
        "|\n",
        "|\n",
        "- - -\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.7, Page Number:482<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "import string\n",
      "\n",
      "def qsort(p):\n",
      "    if p == []: \n",
      "        return []\n",
      "    else:\n",
      "        pivot = p[0]\n",
      "        lesser = qsort([x for x in p[1:] if x < pivot])\n",
      "        greater = qsort([x for x in p[1:] if x >= pivot])\n",
      "        return lesser + [pivot] + greater\n",
      " \n",
      "#Variable Declaration        \n",
      "str=\"Function pointers provide flexibility.\" \n",
      "\n",
      "#sorting the string\n",
      "str=qsort(str)\n",
      "str=string.join(str)\n",
      "\n",
      "#Result\n",
      "print \"sorted strng: \",str\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "sorted strng:        . F b c d e e e f i i i i i i l l n n n o o o p p r r s t t t u v x y\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.8, Page Number:482<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import string\n",
      "\n",
      "def qsort(p):\n",
      "    \"\"\"Quicksort using list comprehensions\"\"\"\n",
      "    if p == []: \n",
      "        return []\n",
      "    else:\n",
      "        pivot = p[0]\n",
      "        lesser = qsort([x for x in p[1:] if x < pivot])\n",
      "        greater = qsort([x for x in p[1:] if x >= pivot])\n",
      "        return lesser + [pivot] + greater\n",
      " \n",
      "#Variable Declaration        \n",
      "num=[10,4,3,6,5,7,8]\n",
      "\n",
      "#sorting the string\n",
      "num=qsort(num)\n",
      "\n",
      "#Result\n",
      "for i in range(7):\n",
      "    print num[i],\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3 4 5 6 7 8 10\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.9, Page Number:484<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def space(count,ch=None):\n",
      "    if ch==None:\n",
      "        for i in xrange(count,0,-1):\n",
      "            print '',\n",
      "    else:\n",
      "        for i in xrange(count,0,-1):\n",
      "            print ch,\n",
      "            \n",
      "\n",
      "fp1=space\n",
      "fp2=space\n",
      "\n",
      "fp1(22)           #outputs 20 spaces\n",
      "print \"|\\n\"        \n",
      "\n",
      "fp2(30,'x')       #output 30 xs \n",
      "print \"|\\n\"\n",
      "        "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "                      |\n",
        "\n",
        "x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x |\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.10, Page Number:485<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "num=None  #static variable\n",
      "\n",
      "class ShareVar:\n",
      "    def setnum(i=0):\n",
      "        global num\n",
      "        num=i\n",
      "    def shownum(self):\n",
      "        global num\n",
      "        print num\n",
      "    \n",
      "#Variables declaration\n",
      "a=ShareVar()\n",
      "b=ShareVar()\n",
      "\n",
      "a.shownum()        #prints None\n",
      "b.shownum()        #prints None\n",
      "\n",
      "num=10             #set static num to 10\n",
      "\n",
      "a.shownum()        #prints 10\n",
      "b.shownum()        #prints 10\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "None\n",
        "None\n",
        "10\n",
        "10\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.11, Page Number:487<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class Demo:\n",
      "    i=None\n",
      "    j=None\n",
      "    def geti(self):\n",
      "        return self.i\n",
      "    def seti(self,x):\n",
      "        self.i=x\n",
      "    \n",
      "ob=Demo()\n",
      "\n",
      "ob.seti(1900)\n",
      "print ob.geti()\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1900\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.12, Page Number:488<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class myclass:\n",
      "    def __init__(self,x):\n",
      "        self.__a=x\n",
      "    def geta(self):\n",
      "        return self.__a\n",
      "    \n",
      "#Variable declaration\n",
      "ob=myclass(4)\n",
      "\n",
      "#Result\n",
      "print ob.geta()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4\n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.13, Page Number:489<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "class myclass:\n",
      "    def __init__(self,x):\n",
      "        self.__a=x\n",
      "    def geta():\n",
      "        return self.__a\n",
      "    \n",
      "ob = myclass(110)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.14, Page Number:490<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class myclass:\n",
      "    def __init__(self,i):\n",
      "        self.__num=i\n",
      "    def getnum(self):\n",
      "        return self.__num\n",
      "    \n",
      "#Variable declaration\n",
      "o=myclass(10)\n",
      "\n",
      "print o.getnum()          #display 10\n",
      "\n",
      "o=myclass(1000)\n",
      "\n",
      "print o.getnum()          #display 1000"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "10\n",
        "1000\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.15, Page Number:491<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class myclass:\n",
      "    def __init__(self,x,y):\n",
      "        self.__numA=x\n",
      "        self.__numB=y\n",
      "    def getnumA(self):\n",
      "        return self.__numA\n",
      "    def getnumB(self):\n",
      "        return self.__numB\n",
      "        \n",
      "#Variable declaration\n",
      "ob1=myclass(7,9)\n",
      "ob2=myclass(5,2)\n",
      "\n",
      "#Result\n",
      "print \"Values in ob1 are \",ob1.getnumB(),\"and\",ob1.getnumA()\n",
      "print \"Values in ob2 are \",ob2.getnumB(),\"and\",ob2.getnumA()\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Values in ob1 are  9 and 7\n",
        "Values in ob2 are  2 and 5\n"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.16, Page Number:492<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class myclass:\n",
      "    def __init__(self,x,y):\n",
      "        self.__numA=x\n",
      "        self.__numB=y\n",
      "    def getnumA(self):\n",
      "        return self.__numA\n",
      "    def getnumB(self):\n",
      "        return self.__numB\n",
      "        \n",
      "#Variable declaration\n",
      "ob1=myclass(7,9)\n",
      "ob2=myclass(5,2)\n",
      "\n",
      "#Result\n",
      "print \"Values in ob1 are \",ob1.getnumB(),\"and\",ob1.getnumA()\n",
      "print \"Values in ob2 are \",ob2.getnumB(),\"and\",ob2.getnumA()\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Values in ob1 are  9 and 7\n",
        "Values in ob2 are  2 and 5\n"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.17, Page Number:494<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def myfunc():\n",
      "    print \"This links as a C function.\"\n",
      "    \n",
      "myfunc()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This links as a C function.\n"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.18, Page Number:495<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from ctypes import *\n",
      "\n",
      "class myclass:    \n",
      "    sum=c_int(0)\n",
      "    def sum_it(self,x):\n",
      "        for i in range(x+1):\n",
      "            self.sum.value+=i\n",
      "    fp=sum_it                          #pointer to function\n",
      "\n",
      "#Variable declaration\n",
      "c=myclass()\n",
      "fp=myclass.sum_it                      #get address of function\n",
      "dp=pointer(c.sum)                      #address of data\n",
      "c.fp(7)                                #compute summation of 7\n",
      "\n",
      "\n",
      "#Result\n",
      "print \"summation of 7 is\",dp[0]\n",
      "        "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "summation of 7 is 28\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.19, Page Number:496<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from ctypes import *\n",
      "\n",
      "class myclass:\n",
      "    sum=c_int(0)\n",
      "    def sum_it(self,x):                      \n",
      "        for i in range(x+1):\n",
      "            self.sum.value+=i\n",
      "    fp=sum_it                          #pointer to function\n",
      "\n",
      "#Variable declaration\n",
      "d=myclass()\n",
      "c=[d]                                  #ponter to object\n",
      "fp=myclass.sum_it                      #get address of function\n",
      "dp=pointer(c[0].sum)                   #get address of data\n",
      "\n",
      "c[0].fp(7)                             #compute summation of 7\n",
      "\n",
      "\n",
      "#Result\n",
      "print \"summation of 7 is\",dp[0]\n",
      "        "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " summation of 7 is 28\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 20.20, Page Number:497<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "class three_d:\n",
      "    def __init__(self,a,b,c):   #3D coordinates\n",
      "        self.x=a\n",
      "        self.y=b\n",
      "        self.z=c\n",
      "    #Display x,y,z coordinates - three_d inserter.\n",
      "    def __repr__(self):\n",
      "        return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)+\"\\n\"\n",
      "    def __add__(self,op2):\n",
      "        if isinstance(op2,int):\n",
      "            c=self.x*self.y*self.z+op2\n",
      "            return c\n",
      "        temp=three_d(self.x+op2.x,self.y+op2.y,self.z+op2.z)\n",
      "        return temp\n",
      "    \n",
      "a=three_d(1,2,3)\n",
      "b=three_d(2,3,4)\n",
      "\n",
      "print a,b,\n",
      "\n",
      "print b+100               #displays 124 because of conversion to int\n",
      "\n",
      "a=a+b                     #add two three_d objects - no conversion\n",
      "\n",
      "print a                   #displays 3,5,7"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1, 2, 3\n",
        " 2, 3, 4\n",
        " 124\n",
        "3, 5, 7\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}