{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Playing with files, strings and dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Problem\n", "Write a code which creates a phone book. The phone book is stored in a file with the number in the beginning and rest being the name. This makes it easy to separate the number from the name using a split command.\n", "\n", "When you start the program it drops to a prompt. Say \"phone > \". The enter command creates a new entry. name or part of the name of a person return the number. If the part matches multiple names it returns all the matches." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Using dictionary" ] }, { "cell_type": "code", "collapsed": false, "input": [ "phonedir = {}\n", "storefile = \"phone.txt\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "def load_directory() :\n", " f = open(storefile, 'r')\n", " for line in f :\n", " words = line.split()\n", " phno = words[0]\n", " name = ' '.join(words[1:])\n", " phonedir[name] = int(phno)\n", " f.close()\n", " return None" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "if __name__ == '__main__' :\n", " print \"Checking load_directory.\"\n", " fw = open(storefile, 'w')\n", " fw.write(\"9840336105 Abc Def\\n\")\n", " fw.write(\"9869369319 Ghi Jkl\\n\")\n", " fw.close()\n", " \n", " load_directory()\n", " print phonedir" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Checking load_directory.\n", "{'Abc Def': 9840336105, 'Ghi Jkl': 9869369319}\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "def enter_name(name, number) :\n", " phonedir[name] = number\n", " return None" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "def save_phone_dir() :\n", " f = open(storefile, 'w')\n", " for e in sorted(phonedir) :\n", " write_string = '%d %s\\n' % (phonedir[e], e)\n", " f.write(write_string)\n", " f.close()\n", " return None" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "if __name__ == '__main__' :\n", " print \"Checking enter_name and save_phone_dir\"\n", " enter_name(\"Mno Pqr\", 681990341)\n", " save_phone_dir()\n", " load_directory()\n", " print phonedir\n", " f = open(storefile, 'r')\n", " for line in f :\n", " print line,\n", " print\n", " f.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Checking enter_name and save_phone_dir\n", "{'Abc Def': 9840336105, 'Mno Pqr': 681990341, 'Ghi Jkl': 9869369319}\n", "9840336105 Abc Def\n", "9869369319 Ghi Jkl\n", "681990341 Mno Pqr\n", "\n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "def list_names(name) :\n", " print \"Name |Number\"\n", " print \"-----------------------------------------\"\n", " for e in phonedir :\n", " if name in e :\n", " print \"%19s |%d\" % (e, phonedir[e]) " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "if __name__ == '__main__' :\n", " print \"Testing list_names\"\n", " list_names('bc')\n", " print 10*'-'\n", " list_names(' ')\n", " print 10*'-'\n", " list_names('zx')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Testing list_names\n", "Name |Number\n", "-----------------------------------------\n", " Abc Def |9840336105\n", "----------\n", "Name |Number\n", "-----------------------------------------\n", " Abc Def |9840336105\n", " Mno Pqr |681990341\n", " Ghi Jkl |9869369319\n", "----------\n", "Name |Number\n", "-----------------------------------------\n" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "def delete_entry(name) :\n", " dellist = []\n", " for e in phonedir :\n", " if name in e :\n", " query_string = \"Delete %s(%d)? (y/n) \" % (e, phonedir[e])\n", " ans = raw_input(query_string)\n", " if ans == 'y' :\n", " dellist.append(e)\n", " for e in dellist :\n", " del phonedir[e]\n", " return None" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "if __name__ == '__main__' :\n", " print \"Testing delete_entry\"\n", " delete_entry('bc')\n", " print phonedir" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Testing delete_entry\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Delete Abc Def(9840336105)? (y/n) y\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "{'Mno Pqr': 681990341, 'Ghi Jkl': 9869369319}\n" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "def user_interface() :\n", " exit = False\n", " error = False\n", " load_directory()\n", " while not exit :\n", " user_input = raw_input(\"Phone > \")\n", " if user_input.strip() == 'exit' or user_input.strip() == 'quit' :\n", " save_phone_dir()\n", " exit = True\n", " elif user_input.strip() == 'enter' or user_input.strip() == 'e' :\n", " name = raw_input(\"\\tName : \")\n", " number = raw_input(\"\\tNumber : \")\n", " try :\n", " no = float(number)\n", " except ValueError :\n", " print \"Something is wrong with the number.\"\n", " error = True\n", " if not error :\n", " enter_name(name, no)\n", " error = False\n", " elif user_input.strip() == 'delete' or user_input.strip() == 'd' :\n", " name = raw_input(\"\\tName : \")\n", " delete_entry(name)\n", " else :\n", " list_names(user_input)\n", " save_phone_dir()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "if __name__ == '__main__' :\n", " print \"Testing user interface\"\n", " user_interface()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Testing user interface\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Phone > \n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Name |Number\n", "-----------------------------------------\n", " Abc Def |9840336105\n", " Mno Pqr |681990341\n", " Ghi Jkl |9869369319\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Phone > d\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\tName : Mno\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Delete Mno Pqr(681990341)? (y/n) y\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Phone > quit\n" ] } ], "prompt_number": 12 } ], "metadata": {} } ] }