In this video we're going to take a different approach to getting a list of all the songs for a particular
album or artist.
So one problem with the previous approach which relied on the fall and directory names is that you may
not have such a neatly organized false structure and if you get the music from different sources.
So the phone is most well known used to sign Peten from samples.
Some of these are the last couple of days have been great for demonstrating how to work with files and
directories.
That's not necessarily the best way to achieve the original objective that was to populate the music
database from a record collection.
So in this video we're going to rate the IDs three tags from the three files on the disk.
Now in order to do that we're going to need a generator that will return the full path of name of a
pastry file.
So that's a challenge.
All right so the challenge is to create a generator that will return the complete formal name of all
MP 3 far was no the general idea should start at the specified directory the start which will be provided
as a document to the generator function.
Now the four names should include the full path from the specified stat directory.
So it should return a relative path.
Now it's a good idea to make a generator function a bit more generic.
Suddenly it can handle with any extension.
So I passed the extension as a second parameter to that function.
Now it's an optional extra.
You could make the pass absolute so that I specify the phone I'm from the root of the drive or volume.
Check out the documentation for the os module if you want to do that.
Now if you haven't got any real MP files you can use the music directory from the previous videos and
search for a MP three files instead of in revolves.
Right so that's the challenge.
See here you go with it.
Pause the video and come back when you're ready to go through my solution.
All right so the solution to this challenge is it's very similar to what we've already done.
So we're going to use the hours to walk up generator to walk the directories from stop and then we're
going to filter the files that match the extension.
So to create a new file
and I'm going to call this one.
And peace rescanned.
We're going to start by importing I guess they're going to import it in match they're going to stop
first and create a famous function and call this one find a school music print to say he's stopped us
by extension in parentheses in a colon.
I mean the top floor paff come Aspies directory's from space fossils in hours to walk starts in parentheses
Kallen for file in if in match start to files commerce.
The single quite Stara dot.
Then we went through what Kelly Brice's and a single quote winding single quite a suicide dot format
extension Kallen after closing off the two parentheses Ruprecht the CS Monitor yield.
I was taught path to join an imprint the path cum a spice follow
and let's actually try this out something to talk for F alone 11 in foreign on a school music
and singing quiet music singing quite calm spice handpiece every single quotes and then in the print
to see what print to say in print if in parentheses and lean on that lot of course we need a calling.
OK so in the previous video we passed the Asterix the wildcard character as part of the pyramidal string.
Say here what I've done is to saw that this was on Law and seven decided to restrict the format by including
the Asterix in the film to Paten.
Now I guess you could make arguments for and against each approach.
In this case the caller doesn't have to worry about the format of the Peten I just passed the extension
as an argument.
It does mean that fine music isn't as flexible as it could be that it allows the file extension to be
floated.
But you've already seen two different approaches to filtering.
You can choose whichever is more appropriate for your particular application.
All right so that's returning all the foreign items relative to a current working directory so that
it will start with music.
But if we want absolute Pott's would use the stop path IBS path function.
So let's actually just try running that.
If we do that say that we are getting a wrap that is working right so stop that.
So again talking about absolute paths.
How would we deal with that.
Well it's actually a function we can use for that.
So what we do is after the fall of the four following.
If in matched filter alone put laughter should absolute and the score path is equal to.
I would stop path the path.
Absolute Positano stands for path and then I to change and X not at all.
So we're recreating
recreating absolute path.
Although I liked
and then we use it in that unit.
Please
just go ahead and change that sort of path.
Now of course the absolute path missing from this is to say that it does work and going to the top and
you can say I worked quite a long path on that one.
But you can say that obviously that path is working in an absolute path then the tree to each fall.
So to.
All right so we don't get the full path of these matching fall and that could be more useful if we wanted
to store the parts in a database so that our music player program could play the songs for example.
Notice how we didn't explicitly create a generator object either.
You can just use your generators in a for loop and take care of creating the generator and the four
takes care of creating the generator and repeatedly calling next on it.
And if you did create the generator object and loop over it that's also fine.
So you could've done something like this.
So I did my school music files to find other school music
music in single quotes from spice and MP 3.
So if it did it that way that would be entirely acceptable as well.
And obviously he did do that why then you probably changed the leucocytes for him.
In my old school music forms we can run that and obviously we get the same result.
So basically I'm saying that why is also I find we get the same results each way.
All right so that's the challenge completed.
Now the reason for creating that generate it is so that we can loop through all the way MP three files
and get the song details from the tags.
Now in case you are not familiar with tags and music files that actually have music players are able
to display the song title or even an image of the album of the album cover.
Now there's a statement detailing the art of the ID three tags used in peace rifles.
So I'm going for a browser.
Let's just have a quick look.
So this Web site contains everything you need to have that you need if you want it to write your own
programs to read write the tags.
So if you want to do that have a good read of the information on this site to find the format of the
data that's ultimately going to be dealing with.
We are going to delve into the barn reformat of MP 3 files.
But one question we get asked often is what we need to read and write Barnby data in.
We're reading the IDs three tags from the music files is one example where you need to do that.
And in fact if any of your music files were missing some tags such as the artist song to also Tado then
you actually could use the earlier technique and attempt to work out the correct values from the following
times.
Your program could then run in the tags back to the file with the correct files at it.
Now working with a binary file format to do with all these three tags is more fiddly than difficult.
But fortunately that Batchelder has created a pause and too much all that does a pretty good job of
reading.
The most common tags you know there are other parts of the lobbies and most of them will also let you
write tanks as well as reading them now in the case of Netz mojo Lenny read the text but that's a good
thing because there's no chance of destroying your music collection by working with this video.
In other words you Wimpey three files will be open for reading only so they'll be Quartzsite.
Now if you're interested in his parsing program ideas read it up by when to put the link to this well
you can go and grab it posted as you can see the lines up there for using Porth and two point seven.
You can download the pop pop pop from the link on the page and copy it into the project directory.
Now fortunately it doesn't work as part of the three.
So what we've done is we've actually produced a possible 3 version from Ned's original code.
So if you are using Python 3 download to already read.
Sorry ATI's 3 Rita was cool.
Please up pie from the resources section of this video and place that in your project directories itself.
Now using the IDs read is much always easy.
You just really create you a new object and provide it with a path to an NPC file and we have to obviously
import the module first of course.
Let's just get back to Intel edge.
I am going to actually post that cause I didn't want to create any file new path and fall and are going
to call this one which I mentioned already.
Sorry Rita I don't score page 3.
This is the palm tree version of the fall and that didn't work.
Let me copy this code and try again that's better.
Looks like it's actually posted incorrectly this time and again you can grab this from the resources
section.
All right so that's the fall if we can get back to our example here in P3 scan.
We just need to create a new object and provide it with a path to an empty string.
Fall ill let you do the rest but obviously to start off with.
Going to import the module first.
Let's go to do that.
So we're going to import D-3 Rita and Wisco pastry ass ideas for the reader.
The bottom three version then what we do is down here and around half full loop.
What I do is treat is equal to three.
Rita start Rita with a capital WA then F in double quotes as in parentheses rather than to print.
Want to expand the print print type of quites print DIS-EASE artist Colan spice.
That's right curly braces come a spice Abal with a capital.
I threw out Kinlay Bryce's Spice after the coma track call in spice.
What can be Brice's come to spice.
So I want to get pro-West colon the correct price prices after the spice and then quite Dalt format
but the format will start with these three.
Ha.
Thought I was cool value.
The first one is going to be performant in single quotes lower case come up.
This is very hard to get at the school value taught kids in school that the printer single quite Talban
ideas for ya.
Give us Covelli print to season two single quotes track and come on the last one will be Adi's are not
giving us Covelli parentheses interesting requests Tottle
such a be it that's pretty straightforward and generate it takes care of getting the phonons first.
And I mainly can focus on them using the already three read a module to read the tag information now
we're going to get an error with his card when I run it.
And that's because when porting it to an MP 3 fall and that rule in peace add another rule.
MP 3 files so the piece files are just plain text and don't actually contain any tag information which
is three Reedham module doesn't seem to handle too well so I'll run just to confirm but I should get
an error here.
And surprisingly we did get one in our series and see see invented argument.
And again that's not really surprising.
And we shouldn't be trying to make Adi's tags from small text files but we really should handle that
and gracefully move onto the next fall if we get an invalid one in the list.
So that sounds like another good challenge.
All right so the challenge is to modify the program to handle any exceptions that are raised by the
ID 3 read a module.
So if an exception is found record the file name in the list and move on to the next file.
Then once the loop finishes pretty at all the files that caused an error.
So that's your challenge.
So here you go with it.
Post the video and come back when you're ready to see my version of the solution.
Post the video now.
All right so for my solution I'm going to start by creating an empty list.
And this is going to be to store the names of any problem files that we try to process.
So I'm going to come down here and put this on line day and 15 rounds second top era call list.
He goes to right square prices.
So adding an empty list or creating an empty list then took the next one there that actually leaps through.
And what we need to do then is another for loop.
We need to add a Troy Troy colon.
They want to put all this code intended to the next level.
So they're going to add an except except it's going to be
at least pinned.
If the video is an exception handling or did suggest that they shouldn't generally catch all exceptions
nobody should be specific and handle specific exceptions.
You know there are always exceptions.
Pun intended to any rule.
And here we need to record any files that cause a problem.
Whatever the problem may be in cases like this it's valid to just catch every exception.
If something goes wrong we're recording the name of the file so that the user can investigate the problem.
It's eye catching all exceptions is actually acceptable in this case.
All right so the last step now is just to pretend any names were released.
So let's go ahead and do that.
So we're going to do it down the bottom for spice era and school list spice in the air on the school
fall actually in Arundel school.
It's called an imprint Eriphyle.
And if you run this program we should follow that.
We get every eight pastry followed by a printed which isn't surprising everyone involved in peace revolves.
Police have confirmed this you've got a second to finish.
And keep in mind also that you can actually run this with rulin PCF always see how this actually works.
If you have got some of your own NPC files I'd suggest it's a good idea to actually go ahead and test
that.
But Theiss brilliant piece refuses to confirm that this does actually work.
All right so you can say here we've got a list of all the files that are invalid which is basically
every fall again because of the ENP three not being invalid false.
Now the other thing you might notice though is run at the top year a small number of files and see it
returning them for the tags.
And that's because of a false loud enough that the attempt to read the tags doesn't cause an error it
just won't return the valid tag either.
Of course and that's why we are getting them.
All right so that's what it looks like with invalid and PC files.
But let's actually just run this just to make sure it does work with valid in piece revolves.
So I've actually got some of my computer to run as a test that I've purchased.
So you need to have some valid in P3 files yourself if you want to actually do this test just to see
it.
I've actually got a photo here and I've got some of my favorites.
I see Daisy and Addo contrast as you can say we've got Paul I mine uses to chalk a desk top test music.
So what I want to do now is change this to point to that folder.
What I'm going to do is typically that line in Cointet first line out and I'm going to change it to
point to some velvet in place before so it's user uses to Metalka just top test music port to have the
final four slashes.
Well if we run this now I will get some good results.
OK.
And the reason for that is you're getting no errors which is good because I didn't change the four top
to be in place because obviously now we are dealing with ballad in rainfalls.
Let's just go ahead and run that again.
I mean we're getting some data as you can see now which we would expect to get.
So let's say you're a couple of Adele songs in there as well.
Clearly between the artist and the album the track and also the name of the song so obviously you guys
to try this is working nicely.
Or is our just commented out to say you can run this using the P3 format if you want to on your computer
an artist uncomment that you're going to see change that path to whatever is appropriate for your operating
system.
All right so that's it.
That's generators.
And as you've seen I think there are a great way to deal with huge amounts of data without using up
all of your computer's memory.
Now I'm sure we can think of all sorts of applications but generators will be useful streaming data
for example would be could use regenerate.
And when you search for something using Google search engine.
It displays the results one page at a time when you click to get the next page of data that's downloaded
once you request it.
So the Google search engine doesn't download all the thousands of search results to your computer.
Now I don't have access to Google source code so I can't guarantee that they're using pots and generators
for retaining search results but that's part of what's develop or whatever.
Russell was working at Google.
It's a good guess that they actually are using that and it's certainly an application where generally
it would be very appropriate.
Right I'm going to finish this video and discussions are generators here in the next video we're going
to look at this comprehensions.
Now I mentioned a couple of times in the course and it's time to find out what they are and how to use
them.
So I will see you in that next video.