1
00:00:05,460 --> 00:00:09,600
so as I mentioned the album classes uses a
list to store the songs and provides a

2
00:00:09,600 --> 00:00:14,750
method the add song method that we've
create for adding more songs or

3
00:00:14,750 --> 00:00:18,880
adding songs to the list and it's also got a field for the artist and we

4
00:00:18,880 --> 00:00:22,080
got an error at the moment and that's because we
haven't created that class so we are going

5
00:00:22,080 --> 00:00:27,700
to be doing that in this video now having an artists object
stored in the album object can cause

6
00:00:27,700 --> 00:00:33,050
problems because the artist class will also hold the
list of the artists publish albums but

7
00:00:33,050 --> 00:00:37,180
I'm gonna talk more about that when we've seen
the artist class now one thing worth

8
00:00:37,180 --> 00:00:41,500
mentioning that before we get to that is
that the error in the doc string and that is up

9
00:00:41,500 --> 00:00:46,640
here you can see that if we flag is it says unresolved reference album_name so this

10
00:00:46,640 --> 00:00:50,260
really highlights the fact that the doc strings
aren't just ordinary strings but they

11
00:00:50,260 --> 00:00:54,690
really do have a special significance
looking at the init method as you can

12
00:00:54,690 --> 00:01:00,120
see here on line 30 or 30 downwards we can see that the data attributes of the

13
00:01:00,120 --> 00:01:06,710
class a name year artist and tracks but I
incorrectly referred to the album_name

14
00:01:06,710 --> 00:01:12,340
attribute in the doc string so IntelliJ has flag a warning unresolved reference album_

15
00:01:12,340 --> 00:01:16,770
name so it's unusual for the
content of strings to be check for

16
00:01:16,770 --> 00:01:21,350
syntax errors but doc strings are used by
a number of tools including IntelliJ

17
00:01:21,350 --> 00:01:26,460
itself now IntelliJ uses the doc strings
of the built-in modules to produce the

18
00:01:26,460 --> 00:01:30,630
intellisense tooltips that appear when
you're using Python built-in

19
00:01:30,630 --> 00:01:34,500
functions and classes but as the
built-in themselves are actually written in

20
00:01:34,500 --> 00:01:40,570
C IntelliJ can't examine the real source built-ins.py is a stub provider so that you

21
00:01:40,570 --> 00:01:44,540
can checked that various declarations and we have
talked about that before now its easy

22
00:01:44,540 --> 00:01:47,930
discussed this with built-in with
built-ins modules on the screens so I add

23
00:01:47,930 --> 00:01:53,160
a call to the code down here on line
54 to do that so we can see what we are talking

24
00:01:53,160 --> 00:01:56,160
about so type...

25
00:01:58,110 --> 00:02:05,080
again we get that same warning here unexpected
argument so we can command or

26
00:02:05,080 --> 00:02:11,009
control-click on album you get access to
the documentation that way but also

27
00:02:11,009 --> 00:02:15,560
going back and closed it down or come
down here again we click on Help

28
00:02:15,560 --> 00:02:20,200
commander or control click on Help we can
see the definitions in the built-in.py

29
00:02:20,200 --> 00:02:24,680
you can see that there's a
comment field next to it so that the

30
00:02:24,680 --> 00:02:28,450
real signatures are unknown and has been
restored from the doc string but the

31
00:02:28,450 --> 00:02:32,320
doc string doesn't list the functions
arguments as a result that's the reason

32
00:02:32,320 --> 00:02:36,200
why IntelliJ doesn't know that it can
take a parameter and to give the warning

33
00:02:36,200 --> 00:02:42,940
as such so close it down and go back to song.py and specifically we'll fix this

34
00:02:42,940 --> 00:02:47,890
error with the album_name
so I'm going to change that to what it should

35
00:02:47,890 --> 00:02:51,710
have been called with name and we should find that error disappears because of course name

36
00:02:51,710 --> 00:02:54,020
is an attribute defined in

37
00:02:54,020 --> 00:02:58,950
our init method you can see the error then goes away
and also delete this help album which

38
00:02:58,950 --> 00:03:03,570
we no longer need and I what will do
is instead will start typing the new

39
00:03:03,570 --> 00:03:06,360
class so lets type the artist class

40
00:03:06,360 --> 00:03:19,120
out so...and I'm going to make a space again so we can see what we are doing here so...

41
00:03:19,120 --> 00:03:23,110
....

42
00:03:28,070 --> 00:03:50,040
....

43
00:03:50,040 --> 00:04:23,170
.....

44
00:04:23,170 --> 00:04:34,910
....ok we also want to document the method so....

45
00:04:34,910 --> 00:04:37,910
.....

46
00:04:39,070 --> 00:05:22,030
.....

47
00:05:22,030 --> 00:05:52,190
....

48
00:05:52,190 --> 00:06:07,250
...again this is a good idea
to put that type of documentation in if

49
00:06:07,250 --> 00:06:12,550
that is the case remember when you do
make the change to go back and revise

50
00:06:12,550 --> 00:06:22,950
the documentation so all we are doing here is.....

51
00:06:22,950 --> 00:06:26,270
so theirs not really any checking their....

52
00:06:26,270 --> 00:06:32,710
so the artist class has got 2 data attributes
the string holding the artist's

53
00:06:32,710 --> 00:06:37,870
name and a list containing the album objects
and its a standard way to organize your

54
00:06:37,870 --> 00:06:41,090
record collection now with the albums by
particular group stored together

55
00:06:41,090 --> 00:06:46,000
compilation albums or the songs by
various artists do complicate things in

56
00:06:46,000 --> 00:06:49,730
this design attempts to deal with that
by allowing an artist to be specified

57
00:06:49,730 --> 00:06:54,480
for each song now the major problem with
this design currently is that an artist

58
00:06:54,480 --> 00:06:59,540
object will have a reference to an album
and that album object will also have a

59
00:06:59,540 --> 00:07:03,380
reference to the artist now the problems
that that can cause are to do with

60
00:07:03,380 --> 00:07:07,890
garbage collection so when objects are
no longer used in your Python code they

61
00:07:07,890 --> 00:07:13,200
still take up memory until they're
ultimately destroyed now many more modern

62
00:07:13,200 --> 00:07:17,310
programming languages deal with this by a
system known as garbage collection

63
00:07:17,310 --> 00:07:21,960
and that means that the runtime system
keeps track of variables and reclaims their

64
00:07:21,960 --> 00:07:26,560
memory when they are no longer being used in the program
anymore and that generally means even

65
00:07:26,560 --> 00:07:30,210
when they go out of scope or when there is no
longer anything else referring to them

66
00:07:30,210 --> 00:07:36,300
as an example now if where to create an
album object for say adells 25

67
00:07:36,300 --> 00:07:41,640
it would have a adell as its artist attribute
however the adellt artist object would have 25

68
00:07:41,640 --> 00:07:46,180
in its list of albums so when the
program is no longer using these the

69
00:07:46,180 --> 00:07:48,560
garbage collector might see that
there's still a reference to both

70
00:07:48,560 --> 00:07:52,850
objects each from the other one and then
ultimately not reclaim the memory

71
00:07:52,850 --> 00:07:57,880
for either so you can see how would then would
stall both of those entries sort of

72
00:07:57,880 --> 00:08:03,230
permanently well until your program
finishes and won't claim

73
00:08:03,230 --> 00:08:07,230
the amount of memory space with that said the
Python 3 garbage collector is quite

74
00:08:07,230 --> 00:08:12,390
advanced and can actually cope quite well
with these situations but circular

75
00:08:12,390 --> 00:08:18,210
object references like the one like this that
I just mentioned are still best avoided and

76
00:08:18,210 --> 00:08:21,780
another problem with circular references
like this is that it can seriously

77
00:08:21,780 --> 00:08:26,640
complicates saving the objects to file
or to a database so sticking with the

78
00:08:26,640 --> 00:08:31,220
adell for a moment when we try to write a
adelts artist object then we will go through all the

79
00:08:31,220 --> 00:08:35,450
attributes and save them into disk when we
get to the album's object the program

80
00:08:35,450 --> 00:08:38,200
will go through the album attributes and save
them to disk

81
00:08:38,200 --> 00:08:41,600
in the process it would find an artist
attribute that would then need to be

82
00:08:41,600 --> 00:08:45,390
saved so it will go through all the
attributes and save them to disk in the

83
00:08:45,390 --> 00:08:49,140
process finding a collection of albums
each containing a reference to an artist

84
00:08:49,140 --> 00:08:55,060
object that contains a collection of
objects and so on so Python pickle module

85
00:08:55,060 --> 00:08:59,870
can often cope with this type
of listing but there are limits and it

86
00:08:59,870 --> 00:09:04,350
can also get quite confused he can get
quite confused and I think you can too as the

87
00:09:04,350 --> 00:09:09,680
program in this scenario and in actual
fact I have made it even worse here and

88
00:09:09,680 --> 00:09:15,880
I've done that by also storing the artist as an attribute of each song in the
album's tracklist so lets just open up

89
00:09:15,880 --> 00:09:21,410
an image to see this in more detail so
let's just have a look at the class

90
00:09:21,410 --> 00:09:26,950
structure for what we've come up with
you can see the diagram on the screen so this is a

91
00:09:26,950 --> 00:09:30,590
little bit complicated and there's
really no good reason for storing the

92
00:09:30,590 --> 00:09:36,630
artist object in the song class for
example the artists name would be enough

93
00:09:36,630 --> 00:09:39,830
to allow the actual artist
object to be retrieved

94
00:09:40,700 --> 00:09:45,990
if more details were required so we are gonna take a
look at that later and just to be clear

95
00:09:45,990 --> 00:09:49,420
I'm not saying that you should never
allow circular references in your code

96
00:09:49,420 --> 00:09:54,840
but if you do allow them then it should
be for you know a really good reason with

97
00:09:54,840 --> 00:09:59,540
that said you should also be aware of
what's going on and also of the potential

98
00:09:59,540 --> 00:10:04,430
problems that may arise as a result of
using them so we're going to remove the

99
00:10:04,430 --> 00:10:10,940
circular references a little later but we are gonna leave them in for now so that we can see the program does work as

100
00:10:10,940 --> 00:10:16,520
its currently set up so we are going to go back to the
code and now that our classes are

101
00:10:16,520 --> 00:10:20,930
defined let's move on and start doing
something with them know rather than

102
00:10:20,930 --> 00:10:26,240
doing loads of typing and also because
using classes to pass data is a very good

103
00:10:26,240 --> 00:10:31,340
way to do it I'm gonna read in the
songs information from a text file now we've got

104
00:10:31,340 --> 00:10:34,830
a file albums.zip that can be downloaded from the Resources section

105
00:10:34,830 --> 00:10:40,270
of this video so download that file then
extract the file and then copy that file

106
00:10:40,270 --> 00:10:43,410
to the same directory as your Python
project

107
00:10:43,410 --> 00:10:47,720
file in IntelliJ and again you can do
that too easily determine where you are by just

108
00:10:47,720 --> 00:10:49,680
right clicking the project here

109
00:10:49,680 --> 00:10:54,510
and coming down to reveal in finder which may be
show files or something like that on a

110
00:10:54,510 --> 00:10:59,640
different platform click on that and it
takes us into the folder so I'm gonna put the

111
00:10:59,640 --> 00:11:05,780
actual files in that folder there where my other py files are and  I'm just gonna navigate

112
00:11:05,780 --> 00:11:07,800
to the file location

113
00:11:07,800 --> 00:11:12,960
where I've already downloaded the files the album.zip and unzip and copy it in their

114
00:11:12,960 --> 00:11:16,280
so I'm gonna go to that folder to my downloads
folder

115
00:11:17,830 --> 00:11:23,950
ok I got albums.zip their so I'm going to unzip that
theirs the albums.txt file that we

116
00:11:23,950 --> 00:11:29,800
are going to import I'm going to take a copy of that
go back to the path where my project files are and

117
00:11:29,800 --> 00:11:33,620
pasted that in there and then when I go back to the code in IntelliJ

118
00:11:34,330 --> 00:11:40,950
we got albums.txt just appeared so
the file if we have a look at it you can see

119
00:11:40,950 --> 00:11:46,960
it's a tab separate list of songs with each
song records starting on a new line now

120
00:11:46,960 --> 00:11:52,080
a record here consists of the artists name
the album name the year it was released as

121
00:11:52,080 --> 00:11:57,650
well as the song title now Python will make  light
work of parsing this kind of data all we

122
00:11:57,650 --> 00:12:02,360
really have to do is to split each line at the tab character and convert the result to a

123
00:12:02,360 --> 00:12:06,930
tuple and it's also necessary to
remove the end of line marker from each

124
00:12:06,930 --> 00:12:11,140
line and fortunately there is again
trivial to do in Python so let's

125
00:12:11,140 --> 00:12:15,830
create a function to load this data up and
call it only if the program is executed

126
00:12:15,830 --> 00:12:22,530
as a script so going back to our code again I want to close this text file so here in our

127
00:12:22,530 --> 00:12:27,270
artists class we are not going to put in
the artist class we are going to start typing it as

128
00:12:27,270 --> 00:12:37,370
a separate function outside of the class so....

129
00:12:37,370 --> 00:12:39,780
.....

130
00:12:39,780 --> 00:12:45,490
....

131
00:12:53,640 --> 00:13:08,010
.....

132
00:13:08,010 --> 00:13:17,370
...

133
00:13:17,370 --> 00:13:21,740
....

134
00:13:24,870 --> 00:13:46,830
....

135
00:13:47,510 --> 00:13:53,890
.....

136
00:13:53,890 --> 00:14:01,700
...so that should give us our 4 fields and as you can see Python makes light work of something that

137
00:14:01,700 --> 00:14:05,890
would take a lot more code in other
languages.....

138
00:14:05,890 --> 00:14:11,940
...

139
00:14:11,940 --> 00:14:26,310
....and that's our method so lets go back

140
00:14:26,310 --> 00:14:31,110
and confirm this works again we are only running this

141
00:14:31,940 --> 00:14:37,630
if the programs executed as scripts so we are going to do a test here so...

142
00:14:37,630 --> 00:14:47,260
....

143
00:14:47,260 --> 00:14:54,010
...alright lets try running the program

144
00:14:54,010 --> 00:14:57,440
and we should see that its displays the details for
all the songs from the

145
00:14:58,030 --> 00:15:04,940
text file so run song and it seems to
have worked ok we've got a list of the

146
00:15:04,940 --> 00:15:10,380
artists field the album the year as
well as the song title itself so that's

147
00:15:10,380 --> 00:15:16,890
good that is working well.....so you can

148
00:15:16,890 --> 00:15:23,590
see there or you could see their that the fields variables seem to be getting the correct values ok so we got

149
00:15:23,590 --> 00:15:26,590
that working....

150
00:15:28,610 --> 00:15:32,420
so now we got this working I'm gonna finish the video
here in the next video we're going to

151
00:15:32,420 --> 00:15:37,890
move on and enhance a little bit more so
if we are gonna start putting the data into the classes

152
00:15:37,890 --> 00:15:41,340
following naturally from the data so in
other words as each rows are read

153
00:15:41,940 --> 00:15:45,550
we are gonna create a song object then add it
to the album and when a new album is

154
00:15:45,550 --> 00:15:51,640
found in the data the current album is going to be store in the artist album list and a new album objects is going to be created

155
00:15:51,640 --> 00:15:54,900
with that current rows details so lets work on that in the next video

