def get_value(self, key):
     # type: (str) -> Any
     """Get a value from the configuration.
     """
     try:
         return _dictionary[key]
     except KeyError:
         raise ConfigurationError("No such key - {}".format(key))
Exemplo n.º 2
0
def _disassemble_key(name):
    # type: (str) -> List[str]
    if "." not in name:
        error_message = (
            "Key does not contain dot separated section and key. "
            "Perhaps you wanted to use 'global.{}' instead?"
        ).format(name)
        raise ConfigurationError(error_message)
    return name.split(".", 1)
Exemplo n.º 3
0
    def _get_parser_to_modify(self):
        # type: () -> Tuple[str, RawConfigParser]
        # Determine which parser to modify
        parsers = self._parsers[self.load_only]
        if not parsers:
            # This should not happen if everything works correctly.
            raise ConfigurationError(
                "Fatal Internal error [id=2]. Please report as a bug.")

        # Use the highest priority parser.
        return parsers[-1]
Exemplo n.º 4
0
 def get_value(self, key: str) -> Any:
     """Get a value from the configuration."""
     orig_key = key
     key = _normalize_name(key)
     try:
         return self._dictionary[key]
     except KeyError:
         # disassembling triggers a more useful error message than simply
         # "No such key" in the case that the key isn't in the form command.option
         _disassemble_key(key)
         raise ConfigurationError(f"No such key - {orig_key}")
Exemplo n.º 5
0
def block_orthogonal(tensor: torch.Tensor,
                     split_sizes: List[int],
                     gain: float = 1.0) -> None:
    """
    An initializer which allows initializing model parameters in "blocks". This is helpful
    in the case of recurrent models which use multiple gates applied to linear projections,
    which can be computed efficiently if they are concatenated together. However, they are
    separate parameters which should be initialized independently.
    Parameters
    ----------
    tensor : ``torch.Tensor``, required.
        A tensor to initialize.
    split_sizes : List[int], required.
        A list of length ``tensor.ndim()`` specifying the size of the
        blocks along that particular dimension. E.g. ``[10, 20]`` would
        result in the tensor being split into chunks of size 10 along the
        first dimension and 20 along the second.
    gain : float, optional (default = 1.0)
        The gain (scaling) applied to the orthogonal initialization.
    """

    if isinstance(tensor, Variable):
        # in pytorch 4.0, Variable equals Tensor
        #    block_orthogonal(tensor.data, split_sizes, gain)
        # else:
        sizes = list(tensor.size())
        if any([a % b != 0 for a, b in zip(sizes, split_sizes)]):
            raise ConfigurationError(
                "tensor dimensions must be divisible by their respective "
                "split_sizes. Found size: {} and split_sizes: {}".format(
                    sizes, split_sizes))
        indexes = [
            list(range(0, max_size, split))
            for max_size, split in zip(sizes, split_sizes)
        ]
        # Iterate over all possible blocks within the tensor.
        for block_start_indices in itertools.product(*indexes):
            # A list of tuples containing the index to start at for this block
            # and the appropriate step size (i.e split_size[i] for dimension i).
            index_and_step_tuples = zip(block_start_indices, split_sizes)
            # This is a tuple of slices corresponding to:
            # tensor[index: index + step_size, ...]. This is
            # required because we could have an arbitrary number
            # of dimensions. The actual slices we need are the
            # start_index: start_index + step for each dimension in the tensor.
            block_slice = tuple([
                slice(start_index, start_index + step)
                for start_index, step in index_and_step_tuples
            ])
            tensor[block_slice] = torch.nn.init.orthogonal_(
                tensor[block_slice].contiguous(), gain=gain)
Exemplo n.º 6
0
    def unset_value(self, key):
        # type: (str) -> None
        """Unset a value in the configuration.
        """
        self._ensure_have_load_only()

        if key not in self._config[self.load_only]:
            raise ConfigurationError("No such key - {}".format(key))

        fname, parser = self._get_parser_to_modify()

        if parser is not None:
            section, name = _disassemble_key(key)

            # Remove the key in the select_parser
            modified_something = False
            if parser.has_section(section):
                # Returns whether the option was removed or not
                modified_something = parser.remove_option(section, name)

            if modified_something:
                # name removed from select_parser, section may now be empty
                section_iter = iter(parser.items(section))
                try:
                    val = six.next(section_iter)
                except StopIteration:
                    val = None

                if val is None:
                    parser.remove_section(section)

                self._mark_as_modified(fname, parser)
            else:
                raise ConfigurationError(
                    "Fatal Internal error [id=1]. Please report as a bug."
                )

        del self._config[self.load_only][key]
Exemplo n.º 7
0
    def unset_value(self, key: str) -> None:
        """Unset a value in the configuration."""
        self._ensure_have_load_only()

        assert self.load_only
        if key not in self._config[self.load_only]:
            raise ConfigurationError(f"No such key - {key}")

        fname, parser = self._get_parser_to_modify()

        if parser is not None:
            section, name = _disassemble_key(key)
            if not (parser.has_section(section)
                    and parser.remove_option(section, name)):
                # The option was not removed.
                raise ConfigurationError(
                    "Fatal Internal error [id=1]. Please report as a bug.")

            # The section may be empty after the option was removed.
            if not parser.items(section):
                parser.remove_section(section)
            self._mark_as_modified(fname, parser)

        del self._config[self.load_only][key]
Exemplo n.º 8
0
 def _construct_parser(self, fname):
     # type: (str) -> RawConfigParser
     parser = configparser.RawConfigParser()
     # If there is no such file, don't bother reading it but create the
     # parser anyway, to hold the data.
     # Doing this is useful when modifying and saving files, where we don't
     # need to construct a parser.
     if os.path.exists(fname):
         try:
             parser.read(fname)
         except UnicodeDecodeError:
             raise ConfigurationError(
                 ("ERROR: "
                  "Configuration file contains invalid %s characters.\n"
                  "Please fix your configuration, located at %s\n") %
                 (locale.getpreferredencoding(False), fname))
     return parser
    def __init__(self, isolated, load_only=None):
        # type: (bool, Optional[Kind]) -> None
        super(Configuration, self).__init__()

        if load_only is not None and load_only not in VALID_LOAD_ONLY:
            raise ConfigurationError(
                "Got invalid value for load_only - should be one of {}".format(
                    ", ".join(map(repr, VALID_LOAD_ONLY))))
        isolated = isolated
        load_only = load_only

        # Because we keep track of where we got the data from
        _parsers = {variant: []
                    for variant in OVERRIDE_ORDER
                    }  # type: Dict[Kind, List[Tuple[str, RawConfigParser]]]
        _config = {variant: {}
                   for variant in OVERRIDE_ORDER
                   }  # type: Dict[Kind, Dict[str, Any]]
        _modified_parsers = []  # type: List[Tuple[str, RawConfigParser]]
Exemplo n.º 10
0
    def __init__(self, isolated: bool, load_only: Optional[Kind] = None) -> None:
        super().__init__()

        if load_only is not None and load_only not in VALID_LOAD_ONLY:
            raise ConfigurationError(
                "Got invalid value for load_only - should be one of {}".format(
                    ", ".join(map(repr, VALID_LOAD_ONLY))
                )
            )
        self.isolated = isolated
        self.load_only = load_only

        # Because we keep track of where we got the data from
        self._parsers: Dict[Kind, List[Tuple[str, RawConfigParser]]] = {
            variant: [] for variant in OVERRIDE_ORDER
        }
        self._config: Dict[Kind, Dict[str, Any]] = {
            variant: {} for variant in OVERRIDE_ORDER
        }
        self._modified_parsers: List[Tuple[str, RawConfigParser]] = []
Exemplo n.º 11
0
    def __init__(self, isolated, load_only=None):
        super(Configuration, self).__init__()

        _valid_load_only = [kinds.USER, kinds.GLOBAL, kinds.VENV, None]
        if load_only not in _valid_load_only:
            raise ConfigurationError(
                "Got invalid value for load_only - should be one of {}".format(
                    ", ".join(map(repr, _valid_load_only[:-1]))
                )
            )
        self.isolated = isolated
        self.load_only = load_only

        # The order here determines the override order.
        self._override_order = [
            kinds.GLOBAL, kinds.USER, kinds.VENV, kinds.ENV, kinds.ENV_VAR
        ]

        self._ignore_env_names = ["version", "help"]

        # Because we keep track of where we got the data from
        self._parsers = {variant: [] for variant in self._override_order}
        self._config = {variant: {} for variant in self._override_order}
        self._modified_parsers = []
Exemplo n.º 12
0
 def _ensure_have_load_only(self):
     # type: () -> None
     if self.load_only is None:
         raise ConfigurationError("Needed a specific file to be modifying.")
     logger.debug("Will be working with %s variant only", self.load_only)
Exemplo n.º 13
0
    and the data stored is also nice.
    """

    def __init__(self, isolated, load_only=None):
<<<<<<< HEAD
        # type: (bool, Optional[Kind]) -> None
=======
        # type: (bool, Kind) -> None
>>>>>>> b66a76afa15ab74019740676a52a071b85ed8f71
        super(Configuration, self).__init__()

        _valid_load_only = [kinds.USER, kinds.GLOBAL, kinds.SITE, None]
        if load_only not in _valid_load_only:
            raise ConfigurationError(
                "Got invalid value for load_only - should be one of {}".format(
                    ", ".join(map(repr, _valid_load_only[:-1]))
                )
            )
<<<<<<< HEAD
        self.isolated = isolated
        self.load_only = load_only
=======
        self.isolated = isolated  # type: bool
        self.load_only = load_only  # type: Optional[Kind]
>>>>>>> b66a76afa15ab74019740676a52a071b85ed8f71

        # The order here determines the override order.
        self._override_order = [
            kinds.GLOBAL, kinds.USER, kinds.SITE, kinds.ENV, kinds.ENV_VAR
        ]
Exemplo n.º 14
0
 def get_value(self, key: str) -> Any:
     """Get a value from the configuration."""
     try:
         return self._dictionary[key]
     except KeyError:
         raise ConfigurationError(f"No such key - {key}")
Exemplo n.º 15
0
 def _ensure_have_load_only(self):
     # type: () -> None
     if self.load_only is None:
         raise ConfigurationError("Needed a specific file to be modifying.")
Exemplo n.º 16
0
    def items(self):
        # type: () -> Iterable[Tuple[str, Any]]
        """Returns key-value pairs like dict.items() representing the loaded
        configuration
        """
        return self._dictionary.items()

    def get_value(self, key):
        # type: (str) -> Any
        """Get a value from the configuration.
        """
        try:
            return self._dictionary[key]
        except KeyError:
            raise ConfigurationError("No such key - {}".format(key))

    def set_value(self, key, value):
        # type: (str, Any) -> None
        """Modify a value in the configuration.
        """
        self._ensure_have_load_only()

        assert self.load_only
        fname, parser = self._get_parser_to_modify()

        if parser is not None:
            section, name = _disassemble_key(key)

            # Modify the parser and the configuration
            if not parser.has_section(section):