RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Customer Renderers
Lecture: Common renderer types via ABCs (abstract base classes)
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
So let's go and adjust our renderers one more time here to make them a common thing.
0:08
So first of all, let's add another type, we'll call this abstract renderer, so let me copy over this type here, renderer abstract base
0:17
and notice its metaclass is set to be an abstract based class meta which means we can do things like say these are abstract methods
0:25
and that means anything that wants to use this type is going to require that it implements these methods,
0:31
that it implements a call, a can serialize value this is not something we've needed yet, but we're going to need,
0:37
and this add adapter, which we have done. So besides this can serialize value, all of our renderers are really already this thing
0:43
so let's go ahead and make them actually derive from that type. So, we've got this, we're going to say implement abstract method
0:54
and it says alright, which one, this can serialize value that's not surprising, we could import this type here
1:00
but it doesn't really matter, and we don't need to worry about that I just put it in the base class, this type annotation
1:05
so you can see what we are expecting but we don't need it here. So, how do I know that I can do this— I'll say whether the type of value
1:11
say return type of value is in self.adapters or which one of the csv type value is list, let's just say is instance, unless, alright,
1:31
so if it's a list, we're pretty sure we can serialize it, we could also look through the items and say yes or no
1:36
but we're just going to go this way and if we could adapt it right so either of these two would work fine,
1:42
and I really don't like having something other than the initializers the first so let's stick this at the bottom.
1:47
Okay, great, so that's our csv one, this image one, let's go over here and make it also— we don't need this anymore
1:54
make it derive from this thing, like that, and again it's going to have to implement this can serialize value
2:05
let me copy a little bit over here, put that down; now, whether this one can serialize a value is if it is a dictionary right
2:13
or it can adapt the type, so that's pretty straightforward and let's go and do this last one here, right same exact thing except for we need the base.
2:28
Lastly, let's go he and run this and see if it still works, okay we were able to create it, that means we were able
2:37
to allocate the things derived from the abstract base types which means they exist, like for example, if we don't have this
2:42
I don't k now if you've played with this before, if you don't implement all these abstract types you can see right here
2:50
if you don't implement the abstract types and try to allocate one so in the dunder init you'll see boom— cannot instantiate abstract class
3:03
because it doesn't implement this right. So this just basically verifies that anything that wants to be used in our system
3:10
is going to be one of these known renderer types. All right, so we've got some structure around our renderers
3:17
we've implemented a variety of them, the last thing to do is we want to have this built in one here also adapt to our functionality,
3:28
to our common base type, so we're going to create a class that derives, basically that just models or derives from this.