How to use the pyrsistent.PMap function in pyrsistent

To help you get started, we’ve selected a few pyrsistent examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github liqd / adhocracy3 / src / adhocracy_core / adhocracy_core / workflows / test_init.py View on Github external
def test_add_meta(self, registry, mock_meta):
        from pyrsistent import PMap
        self.call_fut(registry, 'package:dummy.yaml', 'dummy')
        meta = registry.content.workflows_meta['dummy']
        assert isinstance(meta, PMap)
        assert meta['states']
        assert meta['transitions']
        assert meta['initial_state']
        assert meta['auto_transition'] is False
github Yelp / task_processing / task_processing / plugins / mesos / resource_helpers.py View on Github external
NUMERIC_RESOURCE = field(
    type=float,
    initial=0.0,
    factory=float,
    invariant=lambda x: (x >= 0, 'resource < 0'),
)
_NUMERIC_RESOURCES = frozenset(['cpus', 'mem', 'disk', 'gpus'])


class ResourceSet(PRecord):
    cpus = NUMERIC_RESOURCE
    mem = NUMERIC_RESOURCE
    disk = NUMERIC_RESOURCE
    gpus = NUMERIC_RESOURCE
    ports = field(type=(PVector[PMap] if TYPE_CHECKING else PVector),
                  initial=v(),
                  factory=pvector)


def get_offer_resources(offer: addict.Dict, role: str) -> ResourceSet:
    """ Get the resources from a Mesos offer

    :param offer: the payload from a Mesos resourceOffer call
    :param role: the Mesos role we want to get resources for
    :returns: a mapping from resource name -> available resources for the offer
    """
    res = ResourceSet()
    for resource in offer.resources:
        if resource.role != role:
            continue
github ClusterHQ / flocker / flocker / control / _diffing.py View on Github external
located.  See ``PMap.transform`` for the format of this sort of path.

    :param subobj_a: The desired input sub object.

    :param subobj_b: The desired output sub object.

    :returns: An iterable of ``_IDiffChange`` s that will turn ``subobj_a``
        into ``subobj_b``.
    """
    if subobj_a == subobj_b:
        return pvector([])
    elif isinstance(subobj_a, PClass) and isinstance(subobj_b, PClass):
        a_dict = subobj_a._to_dict()
        b_dict = subobj_b._to_dict()
        return _create_diffs_for_mappings(current_path, a_dict, b_dict)
    elif isinstance(subobj_a, PMap) and isinstance(subobj_b, PMap):
        return _create_diffs_for_mappings(
            current_path, subobj_a, subobj_b)
    elif isinstance(subobj_a, PSet) and isinstance(subobj_b, PSet):
        return _create_diffs_for_sets(
            current_path, subobj_a, subobj_b)
    # If the objects are not equal, and there is no intelligent way to recurse
    # inside the objects to make a smaller diff, simply set the current path
    # to the object in b.
    if len(current_path) > 0:
        return pvector([
            _Set(
                path=current_path[:-1],
                key=current_path[-1],
                value=subobj_b
            )
        ])
github Yelp / task_processing / task_processing / task_processor.py View on Github external
import importlib
import logging

from pyrsistent import field
from pyrsistent import m
from pyrsistent import PMap
from pyrsistent import pmap
from pyrsistent import PRecord

from task_processing.interfaces import TaskExecutor

log = logging.getLogger(__name__)


class Registry(PRecord):
    plugin_modules = field(type=PMap, initial=m(), factory=pmap)
    """
    A map of plugin names (str) to that plugins entry point (a module)

    We use this to allow the registry and plugins to dynamically
    change their behaviors. Currently just the plugins registering executors
    """

    def register_task_executor(self, name, task_executor_cls):
        """Helper method for adding an executor"""
        return self.transform(
            ('task_executors', name), lambda _: task_executor_cls
        )

    def register_deprecated_task_executor(self, name, task_executor_cls):
        """Helper method for adding an deprecated executor"""
        return self.transform(
github rackerlabs / otter / otter / convergence / model.py View on Github external
:var dict json: JSON dict received from Nova from which this server
        is created
    """
    id = attr.ib()
    state = attr.ib(validator=_validate_state)
    created = attr.ib()
    image_id = attr.ib()
    flavor_id = attr.ib()
    # type(pvector()) is pvectorc.PVector, which != pyrsistent.PVector
    links = attr.ib(default=attr.Factory(pvector),
                    validator=instance_of(type(pvector())))
    desired_lbs = attr.ib(default=attr.Factory(pset),
                          validator=instance_of(PSet))
    servicenet_address = attr.ib(default='',
                                 validator=instance_of(string_types))
    json = attr.ib(default=attr.Factory(pmap), validator=instance_of(PMap))

    @classmethod
    def from_server_details_json(cls, server_json):
        """
        Create a :obj:`NovaServer` instance from a server details JSON
        dictionary, although without any 'server' or 'servers' initial resource
        key.

        See
        http://docs.rackspace.com/servers/api/v2/cs-devguide/content/
        Get_Server_Details-d1e2623.html

        :return: :obj:`NovaServer` instance
        """
        try:
            server_state = ServerState.lookupByName(server_json['status'])
github Yelp / task_processing / task_processing / interfaces / event.py View on Github external
        factory=lambda x: pmap(x) if not isinstance(x, PMap) else x,
        initial=m(),
github ClusterHQ / flocker / flocker / control / _model.py View on Github external
:ivar dataset_id: A unique identifier, as ``unicode``.

    :ivar bool deleted: If ``True``, this dataset has been deleted and its
        data is unavailable, or will soon become unavailable.

    :ivar PMap metadata: Mapping between ``unicode`` keys and
        corresponding values. Typically there will be a ``"name"`` key whose
        value is a a human-readable name, e.g. ``"main-postgres"``.

    :ivar int maximum_size: The maximum size in bytes of this dataset, or
        ``None`` if there is no specified limit.
    """
    dataset_id = field(mandatory=True, type=unicode, factory=unicode)
    deleted = field(mandatory=True, initial=False, type=bool)
    maximum_size = field(mandatory=True, initial=None)
    metadata = field(mandatory=True, type=PMap, factory=pmap, initial=pmap(),
                     serializer=lambda f, d: dict(d))


class Manifestation(PClass):
    """
    A dataset that is mounted on a node.

    :ivar Dataset dataset: The dataset being mounted.

    :ivar bool primary: If true, this is a primary, otherwise it is a replica.
    """
    dataset = field(mandatory=True, type=Dataset)
    primary = field(mandatory=True, type=bool)

    @property
    def dataset_id(self):
github rackerlabs / otter / otter / convergence / steps.py View on Github external
return reporter


def _success_reporter(success_reason):
    """
    Return a callable that takes a result and returns a::

        (StepResult.RETRY, [ErrorReason.String(success_reason)])
    """
    def reporter(_):
        return StepResult.RETRY, [ErrorReason.String(success_reason)]
    return reporter


@implementer(IStep)
@attributes([Attribute('server_config', instance_of=PMap)])
class CreateServer(object):
    """
    A server must be created.

    :ivar pmap server_config: Nova launch configuration.
    """

    def as_effect(self):
        """Produce a :obj:`Effect` to create a server."""
        eff = Effect(Func(generate_server_name))

        def got_name(random_name):
            server_config = set_server_name(self.server_config, random_name)
            return create_server(thaw(server_config))

        return eff.on(got_name).on(
github ClusterHQ / flocker / flocker / control / _persistence.py View on Github external
def _is_pyrsistent(obj):
    """
    Boolean check if an object is an instance of a pyrsistent object.
    """
    return isinstance(obj, (PRecord, PClass, PMap, PSet, PVector))
github ClusterHQ / flocker / flocker / node / script.py View on Github external
)
    deployers = field(factory=pmap, initial=_DEFAULT_DEPLOYERS, mandatory=True)
    reactor = field(initial=reactor, mandatory=True)

    get_external_ip = field(initial=(lambda: _get_external_ip), mandatory=True)

    control_service_host = field(type=bytes, mandatory=True)
    control_service_port = field(type=int, mandatory=True)

    # Cannot use type=NodeCredential because one of the tests really wants to
    # set this to None.
    node_credential = field(mandatory=True)
    # Cannot use type=Certificate; pyrsistent rejects classic classes.
    ca_certificate = field(mandatory=True)

    api_args = field(type=PMap, factory=pmap, mandatory=True)

    @classmethod
    def from_configuration(cls, configuration, reactor=None):
        """
        Load configuration from a data structure loaded from the configuration
        file and only minimally processed.

        :param dict configuration: Agent configuration as returned by
            ``get_configuration``.
        :param IReactorCore reactor: A provider of IReactorCore and
            IReactorTime or None to use the global reactor.
        :return: A new instance of ``cls`` with values loaded from the
            configuration.
        """
        if 'logging' in configuration:
            from logging.config import dictConfig