summaryrefslogtreecommitdiff
path: root/Let_us_C/chapter-15.ipynb
diff options
context:
space:
mode:
authorJovina Dsouza2014-06-18 12:43:07 +0530
committerJovina Dsouza2014-06-18 12:43:07 +0530
commit206d0358703aa05d5d7315900fe1d054c2817ddc (patch)
treef2403e29f3aded0caf7a2434ea50dd507f6545e2 /Let_us_C/chapter-15.ipynb
parentc6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff)
downloadPython-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.gz
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.bz2
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.zip
adding book
Diffstat (limited to 'Let_us_C/chapter-15.ipynb')
-rw-r--r--Let_us_C/chapter-15.ipynb621
1 files changed, 621 insertions, 0 deletions
diff --git a/Let_us_C/chapter-15.ipynb b/Let_us_C/chapter-15.ipynb
new file mode 100644
index 00000000..bf1ab52b
--- /dev/null
+++ b/Let_us_C/chapter-15.ipynb
@@ -0,0 +1,621 @@
+{
+ "metadata": {
+ "name": "chapter-15.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 15: Miscellaneous Features <h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Enumerated Data Type , Page number: 508<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to print a payroll using enum'''\n",
+ "\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": [
+ "<h3>Are Enums Necessary, Page number: 510<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to achieve the task of enums using macros'''\n",
+ "\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": [
+ "<h3>Division Without Typecasting , Page number: 513<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate no typecasting'''\n",
+ "\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": [
+ "<h3>Division With Typecasting , Page number: 513<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate explicit typecasting'''\n",
+ "\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": [
+ "<h3>Type Casting , Page number: 514<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate typecasting'''\n",
+ "\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": [
+ "<h3>Bit Fields , Page number: 516<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate bit fields in structures'''\n",
+ "\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": [
+ "<h3>Address of a Function , Page number: 517<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate pointers to functions'''\n",
+ "\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": [
+ "<h3>Invoking a Function Using Pointer, Page number: 518<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to invoke a function using a pointer to a function'''\n",
+ "\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": [
+ "<h3>Functions Returning Pointers , Page number: 520<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate functions returning pointers'''\n",
+ "\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": [
+ "<h3>String Copy , Page number: 520<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program which copies one string into another and\n",
+ "returns the pointer to the target string'''\n",
+ "\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": [
+ "<h3>Findmax Function , Page number: 522<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate a findmax() function\n",
+ "to find out max value from a set of values\n",
+ "irrespective of number of values passed to it'''\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": [
+ "<h3>Display Function, Page number: 524<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate a display() function\n",
+ "to print any number of arguments of any type'''\n",
+ "\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": [
+ "<h3>Union Demo , Page number: 526<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate a union at work'''\n",
+ "\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": [
+ "<h3>Union Example , Page number: 530<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate that we cant assign different values to\n",
+ "different union element at the same time'''\n",
+ "\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": [
+ "<h3>Union of Structures , Page number: 531<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate structures nested in a union'''\n",
+ "\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": {}
+ }
+ ]
+} \ No newline at end of file