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

Continuing the Register

00:00 In this lesson, you will continue working on your register. So in the previous lesson, you created an internal dictionary in your Creator class, and you also implemented a method to populate that internal dictionary.

00:14 In this lesson, the first thing to do is to have the factory_method read from that internal dictionary. The second step then will be to actually populate the internal dictionary, and then you can test your code.

00:29 You’ve learned that the whole objective of using a register was to move away from the if elif else construction here on lines 70 to 77.

00:41 Now before you delete that, please note that this construction returns or, so the factory_method returns instances of the product classes. So an instance of the _JSONSerializer or the _XMLSerializer, or the _YAMLSerializer.

00:57 So the factory_method is still going to have to return instances of products, right? With that in mind, select lines 70 to 77 and just delete those.

01:09 You’re going to replace those with the following line of code: product = So this variable will capture the product. And where do you find the product? Well, the product sits in that internal dictionary.

01:22 So that is self.products.

01:26 And then use .get(data_format).

01:32 So you might be more familiar potentially with the use of the square brackets as you did in row 66 to retrieve data out of a dictionary. But line 70, you are using get(), because when you are using the .get() dictionary method and passing in the data format, this will either return the product or None.

01:55 And that means that in the next line of code, I can do some error trapping. So if not product, in other words, if the dictionary doesn’t return anything, if that product doesn’t exist or if the product is not in the dictionary, then raise a ValueError.

02:19 But if everything is fine, then return as before an instance of the product. So that will be product() and then the open and close parentheses, and that’s it for the factory method, which is now more readable, I would argue.

02:35 And I hope you agree. So the next step is to actually populate the internal dictionary as in use the register. For that please scroll all the way down to the bottom of the code,

02:49 and this is where the register goes. So register, of course, that’s optional. That’s just a comment. Your first step will be to create an instance of the Creator class, call that creator.

03:02 So that’s an instance of the SerializerCreator.

03:07 So again, open and close parentheses.

03:11 So now that I have the instance of the creator class, I now have access to that register_format() method that you implemented in the previous lesson.

03:20 So creator.

03:24 register_format( ) And the first value to go in there was the data format, for example, JSON, and then the product that goes with JSON, so that will be the _JSONSerializer.

03:42 I’m going to be cheeky and copy that because I have to do this twice more. Copy-paste on line 141. Instead of JSON, you will implement XML. And then that will be the XML or the _XMLSerializer.

04:00 And on the line below that, again paste. Instead of JSON, type YAML. And so you will be using the _YAMLSerializer. So now that you have created your register, there is one final change to make. On line 139, you are creating an instance of the Creator class.

04:25 So what you will need to do is you need to make a change to the serialize() function. So if you scroll all the way up to the serialize() function in row 40, you will see that you are in fact already creating an instance of the Creator class there.

04:42 You don’t need to do that in the serialize() function because you are doing it in the register below. So delete that line, and now you are ready to test your code.

04:53 So please save the code and restart the REPL.

04:58 So then type from serializer import Song, Book, and serialize. Create a song or a book. So use your up arrows to find my_song, for example, and then serialize my_song to JSON.

05:14 There you go. So you get exactly the same output. It’s confirming it is JSON and indeed this is the JSON format. For good measure, let’s try XML. So instead of JSON, type XML, and that works fine, confirm that it is XML as you can also see here from the output.

05:33 And then as a final test, let’s try YAML.

05:39 Again, that works. YAML confirmed here, and this is YAML output.

05:47 So your code works. Well done. You have removed the if elif construction, and you have made your Creator class and your factory_method far more readable.

05:56 In the next lesson, you will study something Pythonic called protocols.

Become a Member to join the conversation.