Exemplo n.º 1
0
    def get_state(self):
        """Returns the object state dictionary.

        Returns
        -------
        dict
            Dictionary containing the state of the object.

        """
        # We extract the PUBLIC (pub), READ/WRITE (rw) and READ ONLY (r)
        # attributes from the class dictionary, than we build a new dictionary
        # using as keys the attributes names without the accessibility prefix
        state = dict((as_public(k), getattr(self, as_public(k)))
                     for k in extract_attr(self, 'pub+rw+r'))

        # Get the state of the deeper objects
        # Use list(state) as state size will change during iteration
        for attr in list(state):
            if isinstance(state[attr], CCreator):
                state_deep = state[attr].get_state()
                # Replace `attr` with its attributes's state
                for attr_deep in state_deep:
                    attr_full_key = attr + '.' + attr_deep
                    state[attr_full_key] = state_deep[attr_deep]
                del state[attr]

        return dict(state)
Exemplo n.º 2
0
    def get_params(self):
        """Returns the dictionary of class parameters.

        A parameter is a PUBLIC or READ/WRITE attribute.

        """
        # We extract the PUBLIC (pub) and the READ/WRITE (rw) attributes
        # from the class dictionary, than we build a new dictionary using
        # as keys the attributes names without the accessibility prefix
        return SubLevelsDict((as_public(k), getattr(self, as_public(k)))
                             for k in extract_attr(self, 'pub+rw'))
Exemplo n.º 3
0
 def __repr__(self):
     """Defines print behaviour."""
     out_repr = self.__class__.__name__ + "{"
     for k in extract_attr(self, 'pub+rw+r'):
         pub_attr_name = as_public(k)
         out_repr += "'{:}': ".format(pub_attr_name)
         out_repr += repr(getattr(self, pub_attr_name))
         out_repr += ", "
     return out_repr.rstrip(', ') + "}"
Exemplo n.º 4
0
    def get_params(self):
        """Returns the dictionary of class hyperparameters.

        A hyperparameter is a PUBLIC or READ/WRITE attribute.

        """
        # We extract the PUBLIC (pub) and the READ/WRITE (rw) attributes
        # from the class dictionary, than we build a new dictionary using
        # as keys the attributes names without the accessibility prefix
        params = SubLevelsDict((as_public(k), getattr(self, as_public(k)))
                               for k in extract_attr(self, 'pub+rw'))

        # Now look for any parameter inside the accessible attributes
        for k in extract_attr(self, 'r'):
            # Extract the contained object (if any)
            k_attr = getattr(self, as_public(k))
            if hasattr(k_attr, 'get_params') and len(k_attr.get_params()) > 0:
                # as k_attr has one or more parameters, it's a parameter itself
                params[as_public(k)] = k_attr

        return params