Exemple #1
0
    def set(self, key, value=None):
        """
        Set node attribute values.

        :param key:   node attribute key
        :param value: node attribute value
        """

        self.nodes[self.nid][to_unicode(key)] = to_unicode(value)
Exemple #2
0
    def set(self, key, value, **kwargs):
        """
        Set edge attribute values.
        The method may be overloaded in custom classes to pre-process data
        before setting.

        :param key:   edge attribute key
        :param value: edge attribute value
        """

        self.edges[self.nid][to_unicode(key)] = to_unicode(value)
    def set(self, key, value):
        """
        Implement dictionary setter
        
        ..  note::  Do not use this method directly to add new nodes or edges
            to the graph. Use the graph add_node or add_edge methods for this
            purpose instead.
        
        :param key:   dictionary key to add or update
        :param value: key value
        """

        self._storage[key] = to_unicode(value)
        if self.is_view:
            self._view.add(to_unicode(key))
Exemple #4
0
def flatten_nested_dict(config, parent_key='', sep='.'):
    """
    Flatten a nested dictionary by concatenating all
    nested keys.
    Keys are converted to a string representation if
    needed.
    
    :param config:     dictionary to flatten
    :type config:      :py:dict
    :param parent_key: leading string in concatenated keys
    :type parent_key:  :py:str
    :param sep:        concatenation separator
    :type sep:         :py:str
    :return:           flattened dictionary
    :rtype:            :py:dict
    """

    items = []
    for key, value in config.items():

        new_key = to_unicode(
            '{0}{1}{2}'.format(parent_key, sep, key) if parent_key else key)
        if isinstance(value, collections.MutableMapping):
            items.extend(flatten_nested_dict(value, new_key, sep=sep).items())
        else:
            items.append((new_key, value))

    return dict(items)
    def set_view(self, keys):
        """
        Register keys to represent a selective view on the dictionary

        :param keys: keys to set
        :type keys:  list or tuple
        """

        keys = [to_unicode(key) for key in keys if key in self]
        self._view = keys
    def setdefault(self, key, default=None):
        """
        Dictionary like set default method
        
        If key is not defined set to default value.
        
        :param key:     key to set
        :param default: default value to set
        
        :return:        value
        """

        key = to_unicode(key)
        default = to_unicode(default)
        try:
            return self[key]
        except KeyError:
            self[key] = default
        
        return default
Exemple #7
0
    def __setitem__(self, key, value):
        """
        Implement class abstract method __setitem__

        If the storage class defines a data 'view' on the parent, add the new
        key to the view.
        Resolve data references defined using the self._data_pointer_key and
        set new value on the source attribute.

        :param key:     attribute to set value for
        :param value:   value to set
        """

        key = to_unicode(key)

        if key in self:
            key = self.get_data_reference(key, default=key)
        else:
            if self.is_view:
                self._view.append(key)

        self._storage[key] = to_unicode(value)
Exemple #8
0
    def connect_task(self, task1, task2, *args, **kwargs):
        """
        Connect tasks by task ID (graph nid)

        Creates the directed graph edge connecting two tasks (nodes) together.
        An edge also defines which parameters in the output of one task serve
        as input for another task and how they are named.

        Parameter selection is defined by all additional arguments and keyword
        arguments provided to `connect_task`. Keyword arguments also define the
        name translation of the argument.

        :param task1:         first task of two tasks to connect
        :type task1:          :py:int
        :param task2:         second task of two tasks to connect
        :type task2:          :py:int

        :return:              edge identifier
        :rtype:               :py:tuple
        """

        for task in (task1, task2):
            if task not in self.workflow.nodes:
                raise WorkflowError('Task {0} not in workflow'.format(task))
            if self.workflow.nodes[task].get('format') != 'task':
                raise WorkflowError(
                    'Node {0} not of format "task"'.format(task))

        if task1 == task2:
            raise WorkflowError('Connection to self not allowed')

        edge_data = {'key': u'task_link'}
        data_mapping = prepaire_data_dict(kwargs)
        if data_mapping:
            edge_data['data_mapping'] = data_mapping
        if len(args):
            data_select = [to_unicode(arg) for arg in args]

            # If data_select and data_mapping, the mapping keys should be in data_select
            for key in data_mapping:
                if key not in data_select:
                    data_select.append(key)
                    logging.debug(
                        'Added {0} data key to data selection list'.format(
                            key))

            edge_data['data_select'] = data_select

        eid = self.workflow.add_edge(task1, task2, **edge_data)

        return eid
 def set(self, key, value=None):
     """
     Validate and set IP6 address according to RFC 4291
     """
     
     if key == self.key_tag:
         try:
             ip = ipaddress.ip_address(to_unicode(value))
             if ip.version != 6:
                 logger.error('{0} is of IP protocol {1} not 6'.format(value, ip.version))
         except ipaddress.AddressValueError:
             logger.error('{0} not a valid IP6 address'.format(value))
     
     self.nodes[self.nid][key] = value
    def set_view(self, keys):
        """
        Register keys to represent a selective view on the dictionary

        :param keys: keys to set
        :type keys:  list or tuple
        """

        # If the view covers the dictionary do not set it
        if len(keys) == len(self):
            return

        keys = [to_unicode(key) for key in keys if key in self]
        self._view = keys
 def set(self, key, value=None):
     """
     Validate and set IP4 address according to the "dotted-quad" ABNF
     syntax as defined in RFC 2673
     """
     
     if key == self.key_tag:
         try:
             ip = ipaddress.ip_address(to_unicode(value))
             if ip.version != 4:
                 logger.error('{0} is of IP protocol {1} not 4'.format(value, ip.version))
         except ipaddress.AddressValueError:
             logger.error('{0} not a valid IP4 address'.format(value))
     
     self.nodes[self.nid][key] = value
Exemple #12
0
    def to_string(value):

        return to_unicode(value)