Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Making Products Flexible (Part 2)

00:00 In the previous lesson, you have made some substantial changes to the product classes to make them more generic. But you might wonder, well, the JSON serializer and the XML serializer, they used to be based on the song structure, but how do they now know that I need an artist and a title and an ID? For that, you will need to change the Song data class.

00:22 So if you scroll up to the top of the code, you will be adding a method called use_ product() to your data class. And this method will be specific to this particular class.

00:34 So in this method, you will be able to say, well, for a song, I will need an ID, I will need a title, and I will need an artist. On line 11, type def use_product().

00:48 And as always, it will take self, but it also takes product as an input parameter, so which product to use. And this product will be either the JSON serializer or the XML serializer, or whichever serializer you might build in the future.

01:06 So these serializers are classes, and these are the product classes that you have made more generic in the previous lessons. So now we can use the methods that you built in the previous lesson.

01:18 So product.start_object().

01:24 Of course, the first thing to do is to actually start the object. And given that you are in the Song class, you will want to create a song object.

01:33 So passing in "song" and then self.song_id.

01:41 So that will create a song. The next step then is to add properties to the song. For that there was the add_property() method,

01:51 and the first property you’ll add is a "title", then you pass in self.title.

02:00 And the second property to add, product.add_property() is the "artist", and that is the attribute self.artist.

02:13 So this use_product() method is specific to the Song class. It’s creating a song, start_object "song", and then it’s adding properties, "title" and "artist".

02:24 But it is using the functionality of the generic product classes because both the JSON serializer and the XML serializer, and all other serializers that you will be building, have the methods called start_ object() and add_property().

02:42 Now the start_object() method in the JSON serializer creates a dictionary, and the one in the XML serializer creates an element tree, but that’s irrelevant for the user.

02:52 For the user, it’s only relevant that there is a method called start_object(), and that method will do whatever it requires for that particular product.

03:04 Same thing for the add_property(). All the user needs to know is that there is a method called add_property(), and that will do in the background whatever it needs to do to add properties to either a dictionary when it comes to a JSON product, or to the element tree when it comes to XML.

03:23 So that was the idea behind generalizing the product code. And I think you’ll agree that that is a very powerful improvement of your code. It is creating a little bit more distance between the client and the creation of the object, the background or the nitty gritty of how the object works, that is now even less relevant to the clients.

03:45 They don’t need to know anything about that. The next step on your journey to making the code more generic is to change the serialize() function in line 16.

03:58 So the first input parameter there is song. You can make that more generic because at the moment we only have songs, but in the near future, you will create a book object.

04:11 The inputs to the serialize() function could be a book as well. So instead of song, you make that a bit more generic by calling it object_to_serialize.

04:22 The next change I’d like to make is currently in row 18 where the serializer_product is instantiated. So just to recap, in line 17 _get_serializer() returns a class object.

04:38 And currently in line 18, the class object is instantiated and then a string representation is returned. But I’m going to need the product instance, as you will see in a second.

04:51 The first thing to do is to create the product instance outside of what is now line 19. I call that my_product.

05:01 And so that will be the instance of the serializer_product.

05:06 That’s my instance. Now why did I do that? That’s because I would like to use the use_product() method as defined in line 11. And as you can see, that takes the product instance as input parameter.

05:21 Let’s do that. Now, where is my Song class? Well, that is the first input parameter into the serialize() function. So object_to_serialize

05:35 .use_product(),

05:39 the input parameter there is the instance of the product, which is my_product.

05:45 So now all you need to do in line 20 is to return the string representation of your product instance, which is my_product.

05:57 So just to recap how the serialize() function works: in line 17, _get_serializer() returns a product class object, I capture that in serializer_product, which I then instantiate in line 18 and capture that in my_product. In line 19 then, the my_product instance gets passed into the use_ product() method of my Song class or my Book class in the near future.

06:29 That then triggers the functionality on lines 12, 13, and 14. So it will create the object and add the properties for that specific object. And then finally in line 20, the string representation of the product gets returned to the client.

06:48 And then typing the code that is familiar by now, no doubt. So from serializer import song and serialize()` and create a song,

06:58 and then serialize that song, for example, with data format JSON, and that works fine. And then also try XML, and that works fine too. And when we use YAML, a ValueError is raised as expected. In the next lesson, you will get to see the power of the code you have created.

07:23 So by making the products more generic, you will now be able to also serialize a book, for example, which clearly has different properties. A book doesn’t have an artist, for example, but it has a publisher and you’ll see that it has more properties than a song.

07:41 So that will all be possible because you’ve made your code so flexible and generic. But we’ll cover that in the next lesson.

Become a Member to join the conversation.