1
00:00:05,420 --> 00:00:11,640
let's go back to our initial fruit
Python file which I called shelve for example but it was

2
00:00:11,640 --> 00:00:15,430
dealing with fruit as you can see here I want to
show you what happens when we assign a new

3
00:00:15,430 --> 00:00:21,060
key a new value I should say to a key for
example we've got this code here come

4
00:00:21,060 --> 00:00:26,330
down here after or before the close after
we printed out these items here we could

5
00:00:26,330 --> 00:00:39,780
do something like fruit...
and just to

6
00:00:39,780 --> 00:00:51,170
confirm it's been updated we can put...
and we can do print....

7
00:00:51,170 --> 00:01:02,080
in double quotes should say....
and grab the value using the key, and we are gonna leave fruit.close() in as

8
00:01:02,080 --> 00:01:06,240
well and leave the final printing so lets do
that and see what happens to lime bearing

9
00:01:06,240 --> 00:01:10,990
in mind on line 9 we defined it as a
sour green citrus fruit then on 14 we

10
00:01:10,990 --> 00:01:15,740
changed the definition and will run that and again
need to make sure to select the right program

11
00:01:15,740 --> 00:01:21,060
because initially when I first created the
class of you recall I called it shelve and

12
00:01:21,060 --> 00:01:25,140
this is the configuration and not the
actual class name which can be confusing so

13
00:01:25,140 --> 00:01:30,000
we go in there if we want to change that probably a
good idea to do that changed the name

14
00:01:30,000 --> 00:01:34,110
we should be able to change that now, will name it fruit
which makes sense to us and click on apply

15
00:01:34,110 --> 00:01:38,920
and save and now we have fruit their which
makes a lot more sense so know if we run

16
00:01:38,920 --> 00:01:45,170
that way or selecting up here you can see
we've got lime is now great with

17
00:01:45,170 --> 00:01:48,740
tequila as you can see there so
obviously it was updated and we saw in

18
00:01:48,740 --> 00:01:54,280
the previous video that the when we misspelled the word

19
00:01:54,280 --> 00:01:58,280
engine and removed an e at the end when we
attempted to access the key that

20
00:01:58,280 --> 00:02:03,650
doesn't exist we got an error and we can
use the get method to avoid that so we

21
00:02:03,650 --> 00:02:11,370
could do something like, will comment some of this code now, so comment the code here and leave the close

22
00:02:11,370 --> 00:02:14,810
line and remove the print as well

23
00:02:14,810 --> 00:02:22,520
we don't need it anymore, the code above that we will put a while...

24
00:02:22,520 --> 00:02:44,450
...will ask for some input....and will put if....thats the case we

25
00:02:44,450 --> 00:02:45,340
are gonna break

26
00:02:45,340 --> 00:02:48,790
otherwise we gonna do a look up so...

27
00:02:48,790 --> 00:02:55,600
and use the key that is
entered by the user

28
00:02:55,600 --> 00:03:01,670
who type that in and trumpeted it out, description... and also leave line 27 and then we

29
00:03:01,670 --> 00:03:05,810
broken out of the loop we close the
shelve down because we finished

30
00:03:05,810 --> 00:03:13,310
processing so we try running that now
so type a valid one, lime a sour green

31
00:03:13,310 --> 00:03:18,280
citrus fruit we could write grape that work
lets try typing that is not

32
00:03:18,280 --> 00:03:25,400
in there Strawberry we get none and I can quit out of that as well, we

33
00:03:25,400 --> 00:03:29,400
can also specify the default value to return
if the key is not present and dare I say it

34
00:03:29,400 --> 00:03:34,450
again just like a dictionary and if you
remember how to do that we come down here and we

35
00:03:34,450 --> 00:03:48,530
put in the fruit.get..if we run that...enter a valid one like lemon to check if its working

36
00:03:48,530 --> 00:03:51,660
try something like melon

37
00:03:51,660 --> 00:03:57,530
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:57,530 --> 00:04:00,530
the same effect can be obtained by
checking if the key itself is in the

39
00:04:00,530 --> 00:04:05,320
shelve before attempting to retrieve and print
the value using in and again it's just like a

40
00:04:05,320 --> 00:04:12,810
dictionary so we could modify a program
they so lets just changed that shelf_key lets just

41
00:04:12,810 --> 00:04:21,170
refactor that so right click refactor  and rename
that we raname that to dict_key and refactor it

42
00:04:21,170 --> 00:04:25,830
a few other changes there instead of us having
to rename it 4 5 places, please enter a

43
00:04:25,830 --> 00:04:26,550
fruit

44
00:04:26,550 --> 00:04:32,930
if the dictionary key equals quit we still
break as we do before, what we can do is

45
00:04:32,930 --> 00:04:48,970
if....only then we do description is fruit.get, will change that description = fruit

46
00:04:48,970 --> 00:04:56,250
dictionary key because we don't need to do that anymore and put...and obviously we don't need that

47
00:04:56,250 --> 00:05:03,900
anymore because we determined that this dictionary key does
exist in our shelve and we put the print

48
00:05:03,900 --> 00:05:23,270
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:23,270 --> 00:05:29,330
enter lemon , apple, put melon

50
00:05:29,330 --> 00:05:33,240
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:33,240 --> 00:05:35,150
beforehand

52
00:05:35,150 --> 00:05:39,080
instead of only printing it out putting
it out afterward we retrieve it

53
00:05:39,080 --> 00:05:43,750
so just a different way of doing it now the lecture on dictionaries

54
00:05:43,750 --> 00:05:47,930
we pointed out that the keys themselves are
unsorted and the actual order is

55
00:05:47,930 --> 00:05:52,990
undefined for dictionary now the same is true for a shelve
the good thing though is that if

56
00:05:52,990 --> 00:05:57,960
iterate through the keys of our fruit and print
them they may  be in alphabetical order

57
00:05:57,960 --> 00:06:02,740
and that's not surprising as the shelf is
implemented using a data base and just

58
00:06:02,740 --> 00:06:09,560
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:09,560 --> 00:06:16,330
something like....

60
00:06:16,330 --> 00:06:25,210
print...

61
00:06:25,210 --> 00:06:31,440
so we are printing them out like so
if we run it, in this case its not showing

62
00:06:31,440 --> 00:06:35,990
in alphabetical order but it does on some systems so keep that
in mind again because it's coming from

63
00:06:35,990 --> 00:06:39,850
a database but you can't rely on that or
assuming it does in this case you can see

64
00:06:39,850 --> 00:06:44,930
base on the keys in the values they aren't
sorted in alphabetical order so what you

65
00:06:44,930 --> 00:06:50,870
really want to do is ensure that you get a
sorted list first so in other words if

66
00:06:50,870 --> 00:06:54,960
you need to ensure that you want to sorted
list to be outputted you need to sort

67
00:06:54,960 --> 00:06:58,960
those keys first so how would you go
about doing well we got our fruit open

68
00:06:58,960 --> 00:07:04,810
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:04,810 --> 00:07:16,310
need to do so comment those out and
I'm gonna put here ordered keys...

70
00:07:16,310 --> 00:07:29,280
...and you've seen this before..

71
00:07:29,280 --> 00:07:38,220
this its going to be...because we wanted to use
these sorted keys and we go through and print them out

72
00:07:38,220 --> 00:07:44,650
noticed that with ordered keys we
still using the look up key to

73
00:07:44,650 --> 00:07:48,650
grab the value-added because the keys are now sorted we should

74
00:07:48,650 --> 00:07:54,280
get the output showing in alphabetical
order so run that now you can see now,

75
00:07:54,280 --> 00:07:58,370
I've done that way we are getting the output in
alphabetical order which is great even

76
00:07:58,370 --> 00:08:02,100
if the shelf appears to return
the keys in order on your implementation

77
00:08:02,100 --> 00:08:07,030
of python which i believe it doesn't Linux
but not necessarily you can't rely on it

78
00:08:07,030 --> 00:08:12,370
your clearly saw when I was running in
a MAC without the sort code in it didn't

79
00:08:12,370 --> 00:08:15,770
sorted alphabetically for us
automatically so you really want to do

80
00:08:15,770 --> 00:08:16,470
something like this

81
00:08:16,470 --> 00:08:21,000
you want guarantee it can be sorted in that order
now the values method

82
00:08:21,000 --> 00:08:25,520
values items methods of shelve behave as do for a dictionary as well interesting

83
00:08:25,520 --> 00:08:27,350
enough though the actual objects
returned

84
00:08:27,350 --> 00:08:32,200
are not the same type and which type
would depend upon the implementation now

85
00:08:32,200 --> 00:08:37,840
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:37,840 --> 00:08:41,190
on shelve and then the items
which is just a sequence of

87
00:08:41,190 --> 00:09:06,510
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:06,510 --> 00:09:22,920
...and the last line we obviously

89
00:09:22,920 --> 00:09:25,300
leave the fruit.close()

90
00:09:25,300 --> 00:09:33,980
so if we run that, a typo there fruit.values it should be
run that again and there you go

91
00:09:33,980 --> 00:09:38,490
instead of dict_values and dict_items are objects these

92
00:09:38,490 --> 00:09:42,840
methods that were using when called on
the shelve returned values of view as you

93
00:09:42,840 --> 00:09:48,910
can see, values hear about this line output there and also return an item view as well

94
00:09:48,910 --> 00:09:53,640
names of these objects really makes it
clear that they view objects which what they are

95
00:09:53,640 --> 00:09:58,800
can't modify them and that changes to
the underlined lines shelve which will be reflected in them

96
00:09:58,800 --> 00:10:02,700
like the dict_keys the dict_values
and of course the dict_items

97
00:10:02,700 --> 00:10:07,580
objects that we've already
seen in previous lectures now in terms of

98
00:10:07,580 --> 00:10:11,270
behavior though code that works when
using a dictionary and its view objects

99
00:10:11,270 --> 00:10:15,350
will almost always continue to work if
you replace the diction with a shelve so

100
00:10:15,350 --> 00:10:20,240
even if you get a different objects back
essentially the code is still going to work on

101
00:10:20,240 --> 00:10:24,900
most cases without any modification the
initialization will be different and the

102
00:10:24,900 --> 00:10:30,020
shelve has to be close but most of the
work up to that point will work having

103
00:10:30,020 --> 00:10:32,820
said that is one really important
difference between dictionaries and

104
00:10:32,820 --> 00:10:37,940
shelves a shelve key must be a string and
dictionaries themselves can accept any

105
00:10:37,940 --> 00:10:40,460
immutable object as a key as we talked
about

106
00:10:40,460 --> 00:10:45,170
but again a shelve will only accept a string
ok so that is it for now on the next video

107
00:10:45,170 --> 00:10:49,130
we're going to continue our discussion
and look at updating items stored in a

108
00:10:49,130 --> 00:10:50,460
shelve so see you in the next video

