Ejemplo n.º 1
0
def _sequence_field(checked_class, suffix, item_type, optional, initial):
    """
    Create checked field for either ``PSet`` or ``PVector``.

    :param checked_class: ``CheckedPSet`` or ``CheckedPVector``.
    :param suffix: Suffix for new type name.
    :param item_type: The required type for the items in the set.
    :param bool optional: If true, ``None`` can be used as a value for
        this field.
    :param initial: Initial value to pass to factory.

    :return: A ``field`` containing a checked class.
    """
    class TheType(checked_class):
        __type__ = item_type
    TheType.__name__ = item_type.__name__.capitalize() + suffix

    if optional:
        def factory(argument):
            if argument is None:
                return None
            else:
                return TheType(argument)
    else:
        factory = TheType
    return field(type=optional_type(TheType) if optional else TheType,
                 factory=factory, mandatory=True,
                 initial=factory(initial))
Ejemplo n.º 2
0
def _sequence_field(checked_class, suffix, item_type, optional, initial):
    """
    Create checked field for either ``PSet`` or ``PVector``.

    :param checked_class: ``CheckedPSet`` or ``CheckedPVector``.
    :param suffix: Suffix for new type name.
    :param item_type: The required type for the items in the set.
    :param bool optional: If true, ``None`` can be used as a value for
        this field.
    :param initial: Initial value to pass to factory.

    :return: A ``field`` containing a checked class.
    """
    class TheType(checked_class):
        __type__ = item_type

    TheType.__name__ = item_type.__name__.capitalize() + suffix

    if optional:

        def factory(argument):
            if argument is None:
                return None
            else:
                return TheType(argument)
    else:
        factory = TheType
    return field(type=optional_type(TheType) if optional else TheType,
                 factory=factory,
                 mandatory=True,
                 initial=factory(initial))
Ejemplo n.º 3
0
def pmap_field(key_type, value_type, optional=False, invariant=_valid):
    """
    Create a checked ``PMap`` field.

    :param key: The required type for the keys of the map.
    :param value: The required type for the values of the map.
    :param bool optional: If true, ``None`` can be used as a value for
        this field.
    :param invariant: Pass-through to ``field``.

    :return: A ``field`` containing a ``CheckedPMap``.
    """
    class TheMap(CheckedPMap):
        __key_type__ = key_type
        __value_type__ = value_type
    TheMap.__name__ = (key_type.__name__.capitalize() +
                       value_type.__name__.capitalize() + "PMap")

    if optional:
        def factory(argument):
            if argument is None:
                return None
            else:
                return TheMap(argument)
    else:
        factory = TheMap
    return field(mandatory=True, initial=TheMap(),
                 type=optional_type(TheMap) if optional else TheMap,
                 factory=factory, invariant=invariant)
Ejemplo n.º 4
0
def pset_field(item_type, optional=False):
    """
    Create checked ``PSet`` field.

    :param item_type: The required type for the items in the set.
    :param bool optional: If true, ``None`` can be used as a value for
        this field.

    :return: A ``field`` containing a ``CheckedPSet`` of the given type.
    """
    class TheSet(CheckedPSet):
        __type__ = item_type
    TheSet.__name__ = item_type.__name__.capitalize() + "PSet"

    if optional:
        def factory(argument):
            if argument is None:
                return None
            else:
                return TheSet(argument)
    else:
        factory = TheSet
    return field(type=optional_type(TheSet) if optional else TheSet,
                 factory=factory, mandatory=True,
                 initial=TheSet())
Ejemplo n.º 5
0
def pmap_field(key_type, value_type, optional=False, invariant=_valid):
    """
    Create a checked ``PMap`` field.

    :param key: The required type for the keys of the map.
    :param value: The required type for the values of the map.
    :param bool optional: If true, ``None`` can be used as a value for
        this field.
    :param invariant: Pass-through to ``field``.

    :return: A ``field`` containing a ``CheckedPMap``.
    """
    class TheMap(CheckedPMap):
        __key_type__ = key_type
        __value_type__ = value_type

    TheMap.__name__ = (key_type.__name__.capitalize() +
                       value_type.__name__.capitalize() + "PMap")

    if optional:

        def factory(argument):
            if argument is None:
                return None
            else:
                return TheMap(argument)
    else:
        factory = TheMap
    return field(mandatory=True,
                 initial=TheMap(),
                 type=optional_type(TheMap) if optional else TheMap,
                 factory=factory,
                 invariant=invariant)
Ejemplo n.º 6
0
def pset_field(item_type, optional=False):
    """
    Create checked ``PSet`` field.

    :param item_type: The required type for the items in the set.
    :param bool optional: If true, ``None`` can be used as a value for
        this field.

    :return: A ``field`` containing a ``CheckedPSet`` of the given type.
    """
    class TheSet(CheckedPSet):
        __type__ = item_type

    TheSet.__name__ = item_type.__name__.capitalize() + "PSet"

    if optional:

        def factory(argument):
            if argument is None:
                return None
            else:
                return TheSet(argument)
    else:
        factory = TheSet
    return field(type=optional_type(TheSet) if optional else TheSet,
                 factory=factory,
                 mandatory=True,
                 initial=TheSet())
Ejemplo n.º 7
0
def pmap_field(key_type,
               value_type,
               optional=False,
               invariant=_valid,
               initial=_UNDEFINED,
               factory=None):
    """
    Create a checked ``PMap`` field.

    :param key: The required type for the keys of the map.
    :param value: The required type for the values of the map.
    :param bool optional: If true, ``None`` can be used as a value for this
        field.
    :param invariant: Pass-through to ``field``.
    :param initial: An initial value for the field.  This will first be coerced
        using the field's factory.  If not given, the initial value is an empty
        map.
    :param factory: A factory used to convert input arguments to the stored
        value whenever it is set. Note that this will be composed with the
        constructor for the ``CheckedPMap`` class constructed for this field.

    :return: A ``field`` containing a ``CheckedPMap``.
    """
    input_factory = factory

    class TheMap(CheckedPMap):
        __key_type__ = key_type
        __value_type__ = value_type

    TheMap.__name__ = (key_type.__name__.capitalize() +
                       value_type.__name__.capitalize() + "PMap")

    if optional:

        def mapping_factory(argument):
            if argument is None:
                return None
            else:
                return TheMap(argument)
    else:
        mapping_factory = TheMap

    if input_factory:
        factory = lambda x: mapping_factory(input_factory(x))
    else:
        factory = mapping_factory

    if initial is _UNDEFINED:
        initial = TheMap()
    else:
        initial = factory(initial)

    return field(mandatory=True,
                 initial=initial,
                 type=optional_type(TheMap) if optional else TheMap,
                 factory=factory,
                 invariant=invariant)
Ejemplo n.º 8
0
def pmap_field(
    key_type, value_type, optional=False, invariant=_valid,
    initial=_UNDEFINED, factory=None
):
    """
    Create a checked ``PMap`` field.

    :param key: The required type for the keys of the map.
    :param value: The required type for the values of the map.
    :param bool optional: If true, ``None`` can be used as a value for this
        field.
    :param invariant: Pass-through to ``field``.
    :param initial: An initial value for the field.  This will first be coerced
        using the field's factory.  If not given, the initial value is an empty
        map.
    :param factory: A factory used to convert input arguments to the stored
        value whenever it is set. Note that this will be composed with the
        constructor for the ``CheckedPMap`` class constructed for this field.

    :return: A ``field`` containing a ``CheckedPMap``.
    """
    input_factory = factory

    class TheMap(CheckedPMap):
        __key_type__ = key_type
        __value_type__ = value_type
    TheMap.__name__ = (key_type.__name__.capitalize() +
                       value_type.__name__.capitalize() + "PMap")

    if optional:
        def mapping_factory(argument):
            if argument is None:
                return None
            else:
                return TheMap(argument)
    else:
        mapping_factory = TheMap

    if input_factory:
        factory = lambda x: mapping_factory(input_factory(x))
    else:
        factory = mapping_factory

    if initial is _UNDEFINED:
        initial = TheMap()
    else:
        initial = factory(initial)

    return field(mandatory=True, initial=initial,
                 type=optional_type(TheMap) if optional else TheMap,
                 factory=factory, invariant=invariant)
Ejemplo n.º 9
0
def pmap_field(key_type,
               value_type,
               optional=False,
               invariant=_valid,
               initial=_UNDEFINED):
    """
    Create a checked ``PMap`` field.

    :param key: The required type for the keys of the map.
    :param value: The required type for the values of the map.
    :param bool optional: If true, ``None`` can be used as a value for this
        field.
    :param invariant: Pass-through to ``field``.
    :param initial: An initial value for the field.  This will first be coerced
        using the field's factory.  If not given, the initial value is an empty
        map.

    :return: A ``field`` containing a ``CheckedPMap``.
    """
    class TheMap(CheckedPMap):
        __key_type__ = key_type
        __value_type__ = value_type

    TheMap.__name__ = (key_type.__name__.capitalize() +
                       value_type.__name__.capitalize() + "PMap")

    if optional:

        def factory(argument):
            if argument is None:
                return None
            else:
                return TheMap(argument)
    else:
        factory = TheMap

    if initial is _UNDEFINED:
        initial = TheMap()
    else:
        initial = factory(initial)

    return field(mandatory=True,
                 initial=initial,
                 type=optional_type(TheMap) if optional else TheMap,
                 factory=factory,
                 invariant=invariant)
Ejemplo n.º 10
0
def pmap_field(
    key_type, value_type, optional=False, invariant=_valid,
    initial=_UNDEFINED
):
    """
    Create a checked ``PMap`` field.

    :param key: The required type for the keys of the map.
    :param value: The required type for the values of the map.
    :param bool optional: If true, ``None`` can be used as a value for this
        field.
    :param invariant: Pass-through to ``field``.
    :param initial: An initial value for the field.  This will first be coerced
        using the field's factory.  If not given, the initial value is an empty
        map.

    :return: A ``field`` containing a ``CheckedPMap``.
    """
    class TheMap(CheckedPMap):
        __key_type__ = key_type
        __value_type__ = value_type
    TheMap.__name__ = (key_type.__name__.capitalize() +
                       value_type.__name__.capitalize() + "PMap")

    if optional:
        def factory(argument):
            if argument is None:
                return None
            else:
                return TheMap(argument)
    else:
        factory = TheMap

    if initial is _UNDEFINED:
        initial = TheMap()
    else:
        initial = factory(initial)

    return field(mandatory=True, initial=initial,
                 type=optional_type(TheMap) if optional else TheMap,
                 factory=factory, invariant=invariant)