WEBVTT 1 00:00:01.950 --> 00:00:08.170 let's go back to our initial fruit Python file which I called shelve for example but it was 2 00:00:08.170 --> 00:00:11.969 dealing with fruit as you can see here I want to show you what happens when we assign a new 3 00:00:11.969 --> 00:00:17.590 key a new value I should say to a key for example we've got this code here come 4 00:00:17.590 --> 00:00:22.860 down here after or before the close after we printed out these items here we could 5 00:00:22.860 --> 00:00:36.310 do something like fruit... and just to 6 00:00:36.310 --> 00:00:47.700 confirm it's been updated we can put... and we can do print.... 7 00:00:47.700 --> 00:00:58.610 in double quotes should say.... and grab the value using the key, and we are gonna leave fruit.close() in as 8 00:00:58.610 --> 00:01:02.770 well and leave the final printing so lets do that and see what happens to lime bearing 9 00:01:02.770 --> 00:01:07.520 in mind on line 9 we defined it as a sour green citrus fruit then on 14 we 10 00:01:07.520 --> 00:01:12.270 changed the definition and will run that and again need to make sure to select the right program 11 00:01:12.270 --> 00:01:17.590 because initially when I first created the class of you recall I called it shelve and 12 00:01:17.590 --> 00:01:21.679 this is the configuration and not the actual class name which can be confusing so 13 00:01:21.679 --> 00:01:26.530 we go in there if we want to change that probably a good idea to do that changed the name 14 00:01:26.530 --> 00:01:30.649 we should be able to change that now, will name it fruit which makes sense to us and click on apply 15 00:01:30.649 --> 00:01:35.459 and save and now we have fruit their which makes a lot more sense so know if we run 16 00:01:35.459 --> 00:01:41.700 that way or selecting up here you can see we've got lime is now great with 17 00:01:41.700 --> 00:01:45.279 tequila as you can see there so obviously it was updated and we saw in 18 00:01:45.279 --> 00:01:50.810 the previous video that the when we misspelled the word 19 00:01:50.810 --> 00:01:54.819 engine and removed an e at the end when we attempted to access the key that 20 00:01:54.819 --> 00:02:00.189 doesn't exist we got an error and we can use the get method to avoid that so we 21 00:02:00.189 --> 00:02:07.909 could do something like, will comment some of this code now, so comment the code here and leave the close 22 00:02:07.909 --> 00:02:11.340 line and remove the print as well 23 00:02:11.340 --> 00:02:19.050 we don't need it anymore, the code above that we will put a while... 24 00:02:19.050 --> 00:02:40.980 ...will ask for some input....and will put if....thats the case we 25 00:02:40.980 --> 00:02:41.870 are gonna break 26 00:02:41.870 --> 00:02:45.329 otherwise we gonna do a look up so... 27 00:02:45.329 --> 00:02:52.130 and use the key that is entered by the user 28 00:02:52.130 --> 00:02:58.209 who type that in and trumpeted it out, description... and also leave line 27 and then we 29 00:02:58.209 --> 00:03:02.349 broken out of the loop we close the shelve down because we finished 30 00:03:02.349 --> 00:03:09.840 processing so we try running that now so type a valid one, lime a sour green 31 00:03:09.840 --> 00:03:14.819 citrus fruit we could write grape that work lets try typing that is not 32 00:03:14.819 --> 00:03:21.930 in there Strawberry we get none and I can quit out of that as well, we 33 00:03:21.930 --> 00:03:25.930 can also specify the default value to return if the key is not present and dare I say it 34 00:03:25.930 --> 00:03:30.980 again just like a dictionary and if you remember how to do that we come down here and we 35 00:03:30.980 --> 00:03:45.069 put in the fruit.get..if we run that...enter a valid one like lemon to check if its working 36 00:03:45.069 --> 00:03:48.190 try something like melon 37 00:03:48.190 --> 00:03:54.069 and I don't have a melon instead of saying none which obviously is a lot more user-friendly, and ill quit out of that 38 00:03:54.069 --> 00:03:57.060 the same effect can be obtained by checking if the key itself is in the 39 00:03:57.060 --> 00:04:01.859 shelve before attempting to retrieve and print the value using in and again it's just like a 40 00:04:01.859 --> 00:04:09.349 dictionary so we could modify a program they so lets just changed that shelf_key lets just 41 00:04:09.349 --> 00:04:17.709 refactor that so right click refactor and rename that we raname that to dict_key and refactor it 42 00:04:17.709 --> 00:04:22.360 a few other changes there instead of us having to rename it 4 5 places, please enter a 43 00:04:22.360 --> 00:04:23.080 fruit 44 00:04:23.080 --> 00:04:29.460 if the dictionary key equals quit we still break as we do before, what we can do is 45 00:04:29.460 --> 00:04:45.509 if....only then we do description is fruit.get, will change that description = fruit 46 00:04:45.509 --> 00:04:52.789 dictionary key because we don't need to do that anymore and put...and obviously we don't need that 47 00:04:52.789 --> 00:05:00.430 anymore because we determined that this dictionary key does exist in our shelve and we put the print 48 00:05:00.430 --> 00:05:19.800 description on that line as well, also put the else and that's where we print the item, so we....we leave the fruit.close() alone, so we run that again 49 00:05:19.800 --> 00:05:25.860 enter lemon , apple, put melon 50 00:05:25.860 --> 00:05:29.770 we don't have a melon, so it still work in the same as it was before we've just done the test this time 51 00:05:29.770 --> 00:05:31.680 beforehand 52 00:05:31.680 --> 00:05:35.610 instead of only printing it out putting it out afterward we retrieve it 53 00:05:35.610 --> 00:05:40.289 so just a different way of doing it now the lecture on dictionaries 54 00:05:40.289 --> 00:05:44.469 we pointed out that the keys themselves are unsorted and the actual order is 55 00:05:44.469 --> 00:05:49.520 undefined for dictionary now the same is true for a shelve the good thing though is that if 56 00:05:49.520 --> 00:05:54.490 iterate through the keys of our fruit and print them they may be in alphabetical order 57 00:05:54.490 --> 00:05:59.279 and that's not surprising as the shelf is implemented using a data base and just 58 00:05:59.279 --> 00:06:06.099 as an example of that ill comment out the code the while loop because we don't need it any more we could do 59 00:06:06.099 --> 00:06:12.860 something like.... 60 00:06:12.860 --> 00:06:21.740 print... 61 00:06:21.740 --> 00:06:27.979 so we are printing them out like so if we run it, in this case its not showing 62 00:06:27.979 --> 00:06:32.520 in alphabetical order but it does on some systems so keep that in mind again because it's coming from 63 00:06:32.520 --> 00:06:36.389 a database but you can't rely on that or assuming it does in this case you can see 64 00:06:36.389 --> 00:06:41.460 base on the keys in the values they aren't sorted in alphabetical order so what you 65 00:06:41.460 --> 00:06:47.409 really want to do is ensure that you get a sorted list first so in other words if 66 00:06:47.409 --> 00:06:51.490 you need to ensure that you want to sorted list to be outputted you need to sort 67 00:06:51.490 --> 00:06:55.490 those keys first so how would you go about doing well we got our fruit open 68 00:06:55.490 --> 00:07:01.340 I'm going to comment this, because obviously we written to it and each time we are running it we save it again which we don't 69 00:07:01.340 --> 00:07:12.849 need to do so comment those out and I'm gonna put here ordered keys... 70 00:07:12.849 --> 00:07:25.819 ...and you've seen this before.. 71 00:07:25.819 --> 00:07:34.759 this its going to be...because we wanted to use these sorted keys and we go through and print them out 72 00:07:34.759 --> 00:07:41.180 noticed that with ordered keys we still using the look up key to 73 00:07:41.180 --> 00:07:45.180 grab the value-added because the keys are now sorted we should 74 00:07:45.180 --> 00:07:50.819 get the output showing in alphabetical order so run that now you can see now, 75 00:07:50.819 --> 00:07:54.900 I've done that way we are getting the output in alphabetical order which is great even 76 00:07:54.900 --> 00:07:58.639 if the shelf appears to return the keys in order on your implementation 77 00:07:58.639 --> 00:08:03.560 of python which i believe it doesn't Linux but not necessarily you can't rely on it 78 00:08:03.560 --> 00:08:08.909 your clearly saw when I was running in a MAC without the sort code in it didn't 79 00:08:08.909 --> 00:08:12.300 sorted alphabetically for us automatically so you really want to do 80 00:08:12.300 --> 00:08:13.000 something like this 81 00:08:13.000 --> 00:08:17.539 you want guarantee it can be sorted in that order now the values method 82 00:08:17.539 --> 00:08:22.050 values items methods of shelve behave as do for a dictionary as well interesting 83 00:08:22.050 --> 00:08:23.880 enough though the actual objects returned 84 00:08:23.880 --> 00:08:28.730 are not the same type and which type would depend upon the implementation now 85 00:08:28.730 --> 00:08:34.370 what I'm gonna do is show another example of that we're gonna type some code just gonna print the values 86 00:08:34.370 --> 00:08:37.729 on shelve and then the items which is just a sequence of 87 00:08:37.729 --> 00:09:03.040 tuples to show yo what I mean, I'm gonna comment that code out, gonna go here and start and we're gonna do.... 88 00:09:03.040 --> 00:09:19.450 ...and the last line we obviously 89 00:09:19.450 --> 00:09:21.830 leave the fruit.close() 90 00:09:21.830 --> 00:09:30.510 so if we run that, a typo there fruit.values it should be run that again and there you go 91 00:09:30.510 --> 00:09:35.020 instead of dict_values and dict_items are objects these 92 00:09:35.020 --> 00:09:39.370 methods that were using when called on the shelve returned values of view as you 93 00:09:39.370 --> 00:09:45.440 can see, values hear about this line output there and also return an item view as well 94 00:09:45.440 --> 00:09:50.170 names of these objects really makes it clear that they view objects which what they are 95 00:09:50.170 --> 00:09:55.330 can't modify them and that changes to the underlined lines shelve which will be reflected in them 96 00:09:55.330 --> 00:09:59.230 like the dict_keys the dict_values and of course the dict_items 97 00:09:59.230 --> 00:10:04.110 objects that we've already seen in previous lectures now in terms of 98 00:10:04.110 --> 00:10:07.800 behavior though code that works when using a dictionary and its view objects 99 00:10:07.800 --> 00:10:11.880 will almost always continue to work if you replace the diction with a shelve so 100 00:10:11.880 --> 00:10:16.770 even if you get a different objects back essentially the code is still going to work on 101 00:10:16.770 --> 00:10:21.430 most cases without any modification the initialization will be different and the 102 00:10:21.430 --> 00:10:26.550 shelve has to be close but most of the work up to that point will work having 103 00:10:26.550 --> 00:10:29.350 said that is one really important difference between dictionaries and 104 00:10:29.350 --> 00:10:34.470 shelves a shelve key must be a string and dictionaries themselves can accept any 105 00:10:34.470 --> 00:10:36.990 immutable object as a key as we talked about 106 00:10:36.990 --> 00:10:41.700 but again a shelve will only accept a string ok so that is it for now on the next video 107 00:10:41.700 --> 00:10:45.660 we're going to continue our discussion and look at updating items stored in a 108 00:10:45.660 --> 00:10:46.990 shelve so see you in the next video