Types¶
Add custom type¶
If you want add your own type, you need inherit from
BaseType and override the abstract method
convert. It used to convert an raw value from request to the current
type.
Here is an example:
from parameter.types import BaseType
class CVSList(BaseType):
def convert(self, val):
return val.split(",")
The above type receive a string value, and returns a list that split by comma.
Then you can use the type you have defined.
from parameter import Model, Argument
class DemoEntity(Model):
names = Argument("names", CVSList)
If you want some custom options, you can define the constructor method.
from parameter.types import BaseType
class CVSList(BaseType):
def __init__(self, separator=","):
self.separator = separator
def convert(self, val):
return val.split(self.separator)
The you can define a different separator.
from parameter import Model, Argument
class DemoEntity(Model):
names = Argument("names", CVSList(separator="|"))
-
class
parameter.types.BaseType[source]¶ Base class of the types.
-
convert(val)[source]¶ Convert a value to this type.
Raises: parameter.exception.MismatchError
-
-
class
parameter.types.Nested(model_cls)[source]¶ -
convert(adapter)[source]¶ Returns an instance which subclasses
ModelParameters: adapter ( BaseAdapter) – Adapter of the hosted model.
-