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.Date(format=u'%Y-%m-%d')[source]
convert(val)[source]
class parameter.types.Datetime(format=u'%Y-%m-%d %H:%M:%S')[source]
convert(val)[source]
class parameter.types.Decimal(context=None)[source]
convert(val)[source]
class parameter.types.Double[source]
convert(val)[source]
class parameter.types.Integer[source]

Integer type.

convert(val)[source]
class parameter.types.Nested(model_cls)[source]
convert(adapter)[source]

Returns an instance which subclasses Model

Parameters:adapter (BaseAdapter) – Adapter of the hosted model.
class parameter.types.String(max_len=None, encoding=u'utf8')[source]

String type. This is str in Python2 and bytes in Python3.

convert(val)[source]
class parameter.types.Unicode(max_len=None, encoding=u'utf8')[source]

Unicode type. This is unicode in Python2 and str in Python3.

convert(val)[source]