Thursday, 22 February 2018

Python Shelve- Day 12(1 day later)

  • #shelve are same like dictionary,except that shelve can be used while #dealing with extremely large dictionary,dictionary will be handled in memory, #where as shelve will handle in a separate file #key points to note in shelve is that , the file with extension .db(or dit,bak,dat) ## will be created, #in dictionary since it is in memory,even if there is any mistake like type we can #change it immediately by changing the typo ,where as in shelve we have to #delete that key combination
  •   
    import shelve
    
    # with shelve.open("D:/WORK/2018/February/22-02-2018-Thursday/shelvetest1")as fruit:
    #     fruit = {"orange":"its a fruit which is orange in color",
    #               "apple":"good for health"}
    #     print(fruit)
    #     print(fruit["orange"])
    # print("="*40)
    # print(fruit["orange"])
    # print(fruit)
    
    with shelve.open("D:/WORK/2018/February/22-02-2018-Thursday/shelvetest2")as fruit:
        fruit["orange"]="its a fruit which is orange in color"
        fruit["apple"]="good for health"
    print(fruit)
    print("="*40)
    #the below peice of code will throw an error
    # print(fruit["orange"])
    
    
    with shelve.open("D:/WORK/2018/February/22-02-2018-Thursday/shelvebike1")as bike:
        bike["name"]="tvs"
        bike["engine_cc"]="250 cc"
        #even if removed and executes the below code it will be saved
        #in the first run and remains there,which is not the case if
        #it is normal dictionary
        # bike["engin_cc"]="250 cc"
        bike["model"]="victor"
        print(bike)
        for i in bike:
            print(i)
            print(bike[i])
    ###############################################################################
    import shelve
    
    with shelve.open("D:/WORK/2018/February/22-02-2018-Thursday/shelvebike1")as bike:
        bike["name"]="tvs"
        bike["engine_cc"]="250 cc"
        #even if removed and executes the below code it will be saved
        #in the first run and remains there,which is not the case if
        #it is normal dictionary
        # bike["engin_cc"]="250 cc"
        #we can delete that key using,it has to be run one time only
        # del bike["engin_cc"]
        bike["model"]="victor"
        print(bike)
        for i in bike:
            print(i)
            print(bike[i])
    
     
    

    No comments:

    Post a Comment