Loading video player…

Attempting to Serialize

00:00 In this lesson, you will start working your way towards basic implementation of the Factory Method pattern. Before you do though, let’s study an example that isn’t designed using the Factory Method pattern, and let’s see how you can improve it.

00:17 Well, firstly in the later examples, you will need the YAML format. Please install PyYAML using the instruction on the screen, python -m pip install PyYAML with the y in py as lowercase, and everything else uppercase.

00:40 And secondly, I would recommend that you download the code because there’s a lot of it. Of course, feel free to type along, but that will just take a little bit longer.

00:50 So please open serializer.py.

00:55 And here’s the first attempt of building a serializer solution. We will need JSON and XML because those are the two formats that we are intending to work with.

01:07 And then we will need a class called Song because we want to create a song object. So an instance of this class would be our song object. You have the .__init__() method and as parameters, you have song_id, title and artist, which you just store in the instance attributes.

01:29 Now let me scroll down a little bit to show you the rest of the code.

01:33 Secondly, we have a function called serialize(). Now this is a function. This is not a method. This is not a method of the Song class.

01:41 This sits outside of the Song class, as you can see based on the indentation. So this is a function and it takes as inputs, a song and a data_format.

01:53 And based on the data_format, it is going to make a decision using an if-elif-else construction. So if the data_format is JSON, it will create the song_info here as a dictionary, and then apply json.dumps() to that and then return that.

02:11 If the format is XML, it will have to do a little bit more work because it will create song_info as an element of the element tree. It will then create a sub-element called title, and it will then assign some text to this title, which comes from the song title.

02:31 It does the same thing for artist. So artist is a sub-element of song_info, and then the text allocated to artist is a song’s artist.

02:42 It then returns the element tree but converted to string, based on song_info object. And then it uses specific encoding.

02:52 If the data_format is neither JSON nor XMLs So the else leg here, it will raise a ValueError.

03:02 So if you move to the subfolder where you saved the serializer.py file and from there open the REPL. And then from serializer import Song and serialize. Song is with a capital because that’s the class, and serialize is in lowercase because that is a function.

03:23 Then create a song object. I’m going to call that my_song. That is an instance of the Song class. So again, Song with a capital, it needs an identifier.

03:34 I’m going to give that 1. I’m going to pick what in my opinion is the best song of all time, which is “A Forest” by the artists called The Cure.

03:47 So that is my song, serialize it, just type serialize() pass in my_song.

03:56 And then the required format. Let’s start with XML, and there you have it. There is the song_id, the title, and the artist represented in XML format.

04:09 If you press the up arrow to go back to the previous line and instead of XML, type JSON, press Enter, and then you have the same information represented as a dictionary.

04:21 So in JSON format, press the up arrow again to try something that doesn’t exist or doesn’t exist yet. YAML you will implement that later at the moment that hasn’t been implemented yet.

04:34 So that indeed raises a ValueError, which is the else leg of the if-elif-else construction. Though your code does exactly what it’s supposed to do, when you create your object and then serialize your object given a certain data format, it will work for XML and for JSON because those have been implemented but not yet for YAML. You now have code that works.

05:05 Is this code using the Factory Method pattern? Well, you’ll uncover that in the next lesson.

Become a Member to join the conversation.