Creating the model

Now, we will create a simple SurfboardMetricModel class that we will use to represent metrics. Remember that we won't be persisting the model in any database or file, and therefore, in this case, our class will just provide the required attributes and no mapping information.

Create a new models subfolder within the metrics/metrics folder. Then, create a new metrics.py file in the metrics/metrics/models subfolder. The following lines show the code that declares the necessary imports that we will require for many classes. This will then create a SurfboardMetricModel class in this file. The code file for the sample is included in the restful_python_2_09_01 folder, in the Pyramid01/metrics/metrics/models/metrics.py file:

from enum import Enum 
from marshmallow import Schema, fields 
from marshmallow_enum import EnumField 
 
 
class SurfboardMetricModel: 
    def __init__(self, status, speed_in_mph, altitude_in_feet, water_temperature_in_f): 
        # We will automatically generate the new id 
        self.id = 0 
        self.status = status 
        self.speed_in_mph = speed_in_mph 
        self.altitude_in_feet = altitude_in_feet 
        self.water_temperature_in_f = water_temperature_in_f 

The SurfboardMetricModel class just declares a constructor; that is, the __init__ method. This method receives many arguments and uses them to initialize the attributes with the same names: status, speed_in_mph, altitude_in_feet, and water_temperature_in_f. The id attribute is set to 0. We will automatically increment the identifier for each new surfboard metric generated with an API call.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.145.114