{ "metadata": { "name": "chapter-15.ipynb" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 15: Miscellaneous Features

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Enumerated Data Type , Page number: 508

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def enum(**enums):\n", " return type('Enum', (), enums)\n", "#Enum declaration\n", "emp_dept = enum(assembly = 0, manufacturing = 1,accounts = 2, stores = 3)\n", "\n", "from collections import namedtuple\n", "#Structure declaration\n", "struct_employee = namedtuple(\"struct_employee\", \"name age bs emp_dept\")\n", "\n", "\n", "#Structure for employee\n", "department = emp_dept.manufacturing\n", "e = struct_employee(\"Lothar Mattheus\",28,5575.50,department)\n", "\n", "#Result\n", "print \"Name = \",e.name\n", "print \"Age = \",e.age\n", "print \"Basic salary = \",e.bs\n", "print \"Dept = \",e.emp_dept\n", "\n", "if(e.emp_dept == 2):\n", " print \"%s is an accountant\" %(e.name)\n", "else:\n", " print \"%s is not an accountant\" %(e.name)\n", " \n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Name = Lothar Mattheus\n", "Age = 28\n", "Basic salary = 5575.5\n", "Dept = 1\n", "Lothar Mattheus is not an accountant\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Are Enums Necessary, Page number: 510

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#macro definition\n", "ASSEMBLY = 0\n", "MANUFACTURING = 1\n", "ACCOUNTS = 2\n", "STORES = 3\n", "\n", "from collections import namedtuple\n", "#Structure declaration\n", "struct_employee = namedtuple(\"struct_employee\", \"name age bs department\")\n", "\n", "#Structure for employee\n", "e = struct_employee(\"Lothar Mattheus\",28,5575.50,MANUFACTURING)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Division Without Typecasting , Page number: 513

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "x=6\n", "y=4\n", "\n", "#Calculation\n", "a = int( x/y) \n", "\n", "#Result\n", "print \"Value of a = %f\" %(a)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Value of a = 1.000000\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Division With Typecasting , Page number: 513

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "x=6\n", "y=4\n", "\n", "#Calculation\n", "a = float( x)/y\n", "\n", "#Result\n", "print \"Value of a = %f\" %(a)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Value of a = 1.500000\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Type Casting , Page number: 514

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "a=6.35\n", "\n", "#Result\n", "print \"Value of a on typecasting = %d\" %(int(a))\n", "print \"Value of a = %f\"%(a)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Value of a on typecasting = 6\n", "Value of a = 6.350000\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Bit Fields , Page number: 516

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "import ctypes\n", "import math\n", "\n", "#macro definition\n", "MALE = 0\n", "FEMALE = 1\n", "SINGLE = 0\n", "MARRIED = 1\n", "DIVORCED = 2\n", "WIDOWED = 3\n", "\n", "#Structure declaration\n", "class employee(Structure):\n", " _fields_ = [(\"gender\", c_int, 1),(\"mar_stat\", c_int, 3),(\"hobby\", c_int, 3),(\"scheme\", c_int, 4)]\n", " _sizeof_ = 2\n", "#Structure for employee\n", "e = employee()\n", "e.gender = MALE\n", "e.mar_stat = DIVORCED\n", "e.hobby = 5\n", "e.scheme =9\n", "\n", "#Result\n", "print \"Gender = \",e.gender\n", "print \"Marital status = \",e.mar_stat\n", "print \"Bytes occupied by e = \",sizeof(e)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Gender = 0\n", "Marital status = 2\n", "Bytes occupied by e = 4\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Address of a Function , Page number: 517

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Function definition\n", "def display():\n", " print \"Long live viruses!!\"\n", "\n", "#Result\n", "print \"Address of function display is \",id(display)\n", "display() #function call\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Address of function display is 133993192\n", "Long live viruses!!\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Invoking a Function Using Pointer, Page number: 518

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Function definition\n", "def display():\n", " print \"Long live viruses!!\"\n", "\n", "func_ptr = id(display) #assigning address of function\n", "print \"Address of function display is \",func_ptr\n", "display() #function call\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Address of function display is 133993080\n", "Long live viruses!!\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Functions Returning Pointers , Page number: 520

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Function definition\n", "def fun():\n", " i = 20\n", " return (i)\n", "\n", "#Result\n", "p =fun() #function call\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

String Copy , Page number: 520

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Function definition\n", "def copy(t,s):\n", " i = 0\n", " while ( s[i] != '\\0' ):\n", " t = t + s[i]\n", " i = i + 1\n", " return t\n", "\n", "#Variable declaration\n", "source = \"Jaded\\0\" \n", "target = ''\n", "string = copy( target, source ) # function call\n", "\n", "#Result\n", "print string\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Jaded\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Findmax Function , Page number: 522

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "to find out max value from a set of values\n", "\n", "#function declaration\n", "def findmax(*arg):\n", " maxi = arg[1]\n", " for count in range(2,arg[0]):\n", " num = arg[count]\n", " if (num > maxi):\n", " maxi = num\n", " return maxi\n", "\n", "maxi = findmax(5,23,15,1,92,50)#function call \n", "print \"maximum = \",maxi\n", "\n", "maxi = findmax(3,100,300,29)#function call \n", "print \"maximum = \",maxi\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "maximum = 92\n", "maximum = 300\n" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Display Function, Page number: 524

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#function declaration\n", "def display(*arg):\n", " if(arg[0] == 1): #case int\n", " for j in range(2,arg[1]+2):\n", " i = arg[j]\n", " print \"%d\"%(i)\n", " elif(arg[0] == 2): #case char\n", " for j in range(2,arg[1]+2):\n", " i = arg[j]\n", " print \"%c\"%(i)\n", " elif(arg[0] == 3): #case double\n", " for j in range(2,arg[1]+2):\n", " i = arg[j]\n", " print \"%lf\"%(i)\n", " \n", "#function calls\n", "display(1,2,5,6)\n", "display(2,4,'A','a','b','c')\n", "display(3,3,2.5,299.3,-1.0)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5\n", "6\n", "A\n", "a\n", "b\n", "c\n", "2.500000\n", "299.300000\n", "-1.000000\n" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Union Demo , Page number: 526

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "\n", "#union declaration\n", "class union_a(Union):\n", " _fields_ = [(\"i\", c_int),\n", " (\"ch\", ((c_char * 1)*2))]\n", "\n", "key = union_a()\n", "key.i = 512\n", "print \"key.i = \",key.i\n", "print \"key.ch[0] = \",(key.ch[0][0])\n", "print \"key.ch[1] = \",(key.ch[1][0])\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Union Example , Page number: 530

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "\n", "#Union declaration\n", "class union_a(Union):\n", " _fields_ = [(\"i\", c_int),\n", " (\"ch\", ((c_char * 1)*2))]\n", "\n", "key = union_a()\n", "key.i = 512\n", "print \"key.i = \",key.i\n", "print \"key.ch[0] = \",(key.ch[0][0])\n", "print \"key.ch[1] = \",(key.ch[1][0])\n", "\n", "key.ch[0][0] = 50 #assign new value to key.ch[0]\n", "print\"key.i = \",key.i\n", "print\"key.ch[0] = \",(key.ch[0][0])\n", "print\"key.ch[1] = \",(key.ch[1][0])\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Union of Structures , Page number: 531

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "\n", "#Structure declarations\n", "class a(Structure):\n", " _fields_ = [(\"i\", c_int),\n", " (\"c\", ((c_char * 1)*2))]\n", "\n", "class b(Structure):\n", " _fields_ = [(\"j\", c_int),\n", " (\"d\", ((c_char * 1)*2))]\n", " \n", "#Union declaration\n", "class union_z(Union):\n", " _fields_ = [(\"key\", a),\n", " (\"data\", b )]\n", "\n", "strange = union_z()\n", "strange.key.i = 512\n", "strange.data.d[0][0] = 0\n", "strange.data.d[1][0] = 32\n", "\n", "print strange.key.i\n", "print strange.data.j\n", "print strange.key.c[0][0]\n", "print strange.data.d[0][0]\n", "print strange.key.c[1][0]\n", "print strange.data.d[1][0]\n" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }