diff options
Diffstat (limited to 'The_C_Book/Chapter9.ipynb')
-rwxr-xr-x | The_C_Book/Chapter9.ipynb | 322 |
1 files changed, 322 insertions, 0 deletions
diff --git a/The_C_Book/Chapter9.ipynb b/The_C_Book/Chapter9.ipynb new file mode 100755 index 00000000..33989015 --- /dev/null +++ b/The_C_Book/Chapter9.ipynb @@ -0,0 +1,322 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:df11d901f0094ffa94d5329d8948da8b0edcfcfd27852f9f43b01212a792c736" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 9: Libraries " + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.1, page no. 250 " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "This requires python version 2.6 or greater.....\n", + "'''\n", + "\n", + "\n", + "import sys\n", + "\n", + "\n", + "class x:\n", + " a=0\n", + " b=0\n", + " c=0\n", + " \n", + "\n", + "try:\n", + " print sys.getsizeof(x.a) - sys.getsizeof(x.b)\n", + "except AttributeError:\n", + " print \"sys.getsizeof exists in Python 2.6 or greater...\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.2, page no. 252" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "This program wont give any output.\n", + "'''\n", + "\n", + "\n", + "def func():\n", + " c = 0\n", + " assert(c!=0)\n", + " c = raw_input(\"enter value.. :\")" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.3, page no. 264" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#place=jmp_buf() # this is outside class that we don't have\n", + "def func():\n", + " '''\n", + " /*\n", + " * Return to main.\n", + " * Looks like a second return from setjmp,\n", + " * returning 4!\n", + " */\n", + " '''\n", + " #longjmp(place, 4) # This function belongs to outside class file. we don't have it.\n", + " print \"What! longjmp returned!\\n\"\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "retval = 0\n", + "'''\n", + "/*\n", + "* First call returns 0,\n", + "* a later longjmp will return non-zero.\n", + "*/\n", + "'''\n", + "\n", + "#if(setjmp(place) != 0): #setjmp function is imported from outside. we don't have it in this file. so for compiling make comment on this two lines.\n", + "# print \"Returned using longjmp\\n\"\n", + "func()\n", + "print \"What! func returned!\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "What! longjmp returned!\n", + "\n", + "What! func returned!\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.4, page no. 267" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Example 9.4\n", + "This shows misuse of loop. i.e. infinite loop.\n", + "This prints infinitely ready... in console.\n", + "'''\n", + "\n", + "\n", + "temp_file = None\n", + "def leave(sig):\n", + " temp_file.write(\"\\nInterrupted..\\n\")\n", + " temp_file.close()\n", + " \n", + "\n", + "temp_file = open(\"tmp\",\"w\");\n", + "while(True):\n", + " '''\n", + " /*\n", + " * Do things....\n", + " */\n", + " '''\n", + " print \"Ready...\\n\"\n", + " a = raw_input(\"Enter character.. \")\n", + "\n", + " #/* can't get here ... */" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.5, page no. 268" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def f(*args):\n", + " pass\n", + "\n", + "f(1,2,3)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.6, page no. 270" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def maxof(*args):\n", + " max = 0\n", + " a = 0\n", + " for i in args:\n", + " if i > max:\n", + " max = i\n", + " return max\n", + "\n", + "\n", + "def f():\n", + " i = 5\n", + " j = 24\n", + " print \"%d\\n\" %(maxof(3, i, j, 0))\n", + "\n", + "\n", + "\n", + "f()\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "24\n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.7, page no. 287" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Since we do not have 'testfile', this program will give an error\n", + "\n", + "import sys\n", + "class xx:\n", + " xx_int = 0\n", + " xx_float = 0\n", + "\n", + "ar = []\n", + "\n", + "\n", + "fp = open(\"testfile\", \"w\")\n", + "if(fp.write(\"Hello\")):\n", + " print \"Error writing\\n\"\n", + " sys.exit(0)\n", + "\n", + "for i in fp.readlines():\n", + " print i\n", + "\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.8, page no. 290" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "f = open(\"anfile\",\"w\")\n", + "try:\n", + " if(f):\n", + " f.write(\"What - no error!\\n\")\n", + "except Exception:\n", + " print \"Bad File\"" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 7 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |