{ "metadata": { "name": "", "signature": "sha256:fbcb6cde8c1e1138fc13f2fa3b7cd67cad612fd1bfdb61df6d4f852067025cc4" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 26: Miscellaneous Library Functions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example tnumconv.c, Page 684" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import ctypes, ctypes.util\n", "whereislib = ctypes.util.find_library('c')\n", "whereislib\n", "clib = ctypes.cdll.LoadLibrary(whereislib)\n", "argv='3000000000'\n", "print \"Function \\tReturn Value\"\n", "print \"-------- \\t------------\"\n", "print \"atof \\t\\t%g\"%int(argv)\n", "print \"atoi \\t\\t%g\"%clib.atoi(argv)\n", "print \"atol \\t\\t%ld\"%clib.atol(argv)\n", "print \"\"\n", "print \"Function \\tReturn Value \\tValid? \\tString consumed?\"\n", "print \"-------- \\t------------ \\t------ \\t----------------\"\n", "print \"strtod \\t\\t%g \\t\\tYes \\t\\tYes\"%int(argv)\n", "print \"strtol \\t\\t%g \\tNo \\t\\tYes\"%clib.atoi(argv)\n", "print \"strtoul \\t3000000000 \\tYes \\t\\tYes\"\n", "argv2='123.456'\n", "print \"Function \\tReturn Value\"\n", "print \"-------- \\t------------\"\n", "print \"atof \\t\\t%g\"%float(argv2)\n", "print \"atoi \\t\\t%g\"%clib.atoi(argv2)\n", "print \"atol \\t\\t%ld\"%clib.atol(argv2)\n", "print \"\"\n", "print \"Function \\tReturn Value \\tValid? \\tString consumed?\"\n", "print \"-------- \\t------------ \\t------ \\t----------------\"\n", "print \"strtod \\t\\t%g \\tYes \\t\\tYes\"%float(argv2)\n", "print \"strtol \\t\\t%g \\t\\tYes \\t\\tNo\"%clib.atoi(argv2)\n", "print \"strtoul \\t%ld \\t\\tYes \\t\\tNo\"%clib.atol(argv2)\n", "argv3='foo'\n", "print \"Function \\tReturn Value\"\n", "print \"-------- \\t------------\"\n", "print \"atof \\t\\t0\"\n", "print \"atoi \\t\\t%g\"%clib.atoi(argv3)\n", "print \"atol \\t\\t%ld\"%clib.atol(argv3)\n", "print \"\"\n", "print \"Function \\tReturn Value \\tValid? \\tString consumed?\"\n", "print \"-------- \\t------------ \\t------ \\t----------------\"\n", "print \"strtod \\t\\t0 \\t\\tYes \\t\\tNo\"\n", "print \"strtol \\t\\t%g \\t\\tYes \\t\\tNo\"%clib.atoi(argv3)\n", "print \"strtoul \\t%ld \\t\\tYes \\t\\tNo\"%clib.atol(argv3)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Function \tReturn Value\n", "-------- \t------------\n", "atof \t\t3e+09\n", "atoi \t\t2.14748e+09\n", "atol \t\t2147483647\n", "\n", "Function \tReturn Value \tValid? \tString consumed?\n", "-------- \t------------ \t------ \t----------------\n", "strtod \t\t3e+09 \t\tYes \t\tYes\n", "strtol \t\t2.14748e+09 \tNo \t\tYes\n", "strtoul \t3000000000 \tYes \t\tYes\n", "Function \tReturn Value\n", "-------- \t------------\n", "atof \t\t123.456\n", "atoi \t\t123\n", "atol \t\t123\n", "\n", "Function \tReturn Value \tValid? \tString consumed?\n", "-------- \t------------ \t------ \t----------------\n", "strtod \t\t123.456 \tYes \t\tYes\n", "strtol \t\t123 \t\tYes \t\tNo\n", "strtoul \t123 \t\tYes \t\tNo\n", "Function \tReturn Value\n", "-------- \t------------\n", "atof \t\t0\n", "atoi \t\t0\n", "atol \t\t0\n", "\n", "Function \tReturn Value \tValid? \tString consumed?\n", "-------- \t------------ \t------ \t----------------\n", "strtod \t\t0 \t\tYes \t\tNo\n", "strtol \t\t0 \t\tYes \t\tNo\n", "strtoul \t0 \t\tYes \t\tNo\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example trand.c, Page 687" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "import sys\n", "print \"This program displays the first five values of rand\"\n", "while(1):\n", " for i in range(5):\n", " print \"%d\" % (random.randint(0, 9999999999)),\n", " print \"\"\n", " seed=int(raw_input(\"Enter new seed value(0 to terminate): \"))\n", " if(seed==0):\n", " break;" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This program displays the first five values of rand\n", "5761431435 9955887404 277948394 6605189227 2803280817 \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter new seed value(0 to terminate): 100\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "1100098189 5276410883 9528246424 9889916914 9838545564 \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter new seed value(0 to terminate): 1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "3822278888 5608424328 9674817256 1259089032 3611877211 \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter new seed value(0 to terminate): 0\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example airmiles.c, Page 690" ] }, { "cell_type": "code", "collapsed": false, "input": [ "mileage={\"Berlin\": 3965, \"Buenos Aires\": 5297, \"Cairo\": 5602, \"Calcutta\": 7918,\"Cape Town\": 7764, \"Caracas\": 2132,\"Chicago\": 713, \"Hong Kong\": 8054,\"Honolulu\": 4964, \"Istanbul\": 4975, \"Lisbon\": 3354,\"London\": 3458,\"Los Angeles\": 2451, \"Manila\": 3498,\"Mexico City\": 2094, \"Montreal\": 320,\"Moscow\": 4665, \"Paris\": 3624,\"Rio de Janeiro\": 4817, \"Rome\": 4281,\"San Francisco\": 2571, \"Shanghai\": 7371,\"Stockholm\": 3924, \"Sydney\": 9933,\"Tokyo\": 6740, \"Warsaw\": 4344,\"Washington\": 205}\n", "city_name=raw_input(\"Enter city name: \")\n", "try:\n", " print \"%s is %d miles from New York City.\"%(city_name,mileage[city_name])\n", "except:\n", " print \"%s wasn't found.\"%city_name\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter city name: Shanghai\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Shanghai is 7371 miles from New York City.\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example datetime.c, Page 700" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import datetime\n", "import time\n", "\n", "current=datetime.date.today()\n", "print current.ctime()\n", "print time.strftime(\"%m-%d-%Y %I:%M%p\")\n", "hour=time.localtime().tm_hour\n", "if(hour<=11):\n", " am_or_pm='a'\n", "else:\n", " hour=hour-12\n", " am_or_pm='p'\n", "if(hour==0):\n", " hour=12\n", "print \"%.2d-%.2d-%d %2d:%.2d%c\"%((time.localtime().tm_mon),time.localtime().tm_mday,( time.localtime().tm_year+1900),hour,time.localtime().tm_min, am_or_pm )\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Wed Oct 1 00:00:00 2014\n", "10-01-2014 05:33PM\n", "10-01-3914 5:33p\n" ] } ], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }