Ejemplo n.º 1
0
 def __init__(self, parent, depth, main, indict=None, name=None):
     """
     Constructor.
     
     :param _profile: Profile section that has to be wrapped by this class.
     :type _profile: Section
     """
     
     Section.__init__(self, parent, depth + 1, main, indict, name)
Ejemplo n.º 2
0
 def __init__(self, parent, depth, main, indict=None, name=None):
     """
     Constructor.
     
     :param get_process: Process section.
     :type get_process: Section
     """
     
     Section.__init__(self, parent, depth + 1, main, indict, name)
Ejemplo n.º 3
0
    def __init__(self, parent, depth, main, indict=None, name=None):
        """
        Constructor.
        
        :param get_process: Process section.
        :type get_process: Section
        """

        Section.__init__(self, parent, depth + 1, main, indict, name)
Ejemplo n.º 4
0
    def __init__(self, parent, depth, main, indict=None, name=None):
        """
        Constructor.
        
        :param _profile: Profile section that has to be wrapped by this class.
        :type _profile: Section
        """

        Section.__init__(self, parent, depth + 1, main, indict, name)
Ejemplo n.º 5
0
    def from_conf(conf: configobj.Section):

        if 'embedding' in conf:
            log.warning('config: "embedding" is ignored - please remove!')

        M, K = conf.as_int('components'), conf.as_int('codelength')
        fcl1 = (M * K // 2) if 'fcl1' not in conf else conf.as_int('fcl1')

        return Dimensions(components=M, codelength=K, fcl1=fcl1)
Ejemplo n.º 6
0
    def __init__(self, infile=None, options=None, configspec=None, encoding=None,
             interpolation=True, raise_errors=False, list_values=True,
             create_empty=False, file_error=False, stringify=True,
             indent_type=None, default_encoding=None, unrepr=False,
             write_empty_values=False, _inspec=False, parent_config=None):
        """
        Parse a config file or create a config file object.

        ``ConfigObj(infile=None, configspec=None, encoding=None,
                    interpolation=True, raise_errors=False, list_values=True,
                    create_empty=False, file_error=False, stringify=True,
                    indent_type=None, default_encoding=None, unrepr=False,
                    write_empty_values=False, _inspec=False)``
        """
        self._inspec = _inspec
        # init the superclass
        # this is the only change - we pass the parent configobj if
        # available, to have lookup use its values
        Section.__init__(self, parent_config or self, 0, self)


        infile = infile or []
        if options is not None:
            import warnings
            warnings.warn('Passing in an options dictionary to ConfigObj() is ',
                          'deprecated. Use **options instead.',
                          DeprecationWarning, stacklevel=2)

        _options = {'configspec': configspec,
                    'encoding': encoding, 'interpolation': interpolation,
                    'raise_errors': raise_errors, 'list_values': list_values,
                    'create_empty': create_empty, 'file_error': file_error,
                    'stringify': stringify, 'indent_type': indent_type,
                    'default_encoding': default_encoding, 'unrepr': unrepr,
                    'write_empty_values': write_empty_values}

        options = dict(options or {})
        options.update(_options)

        # XXXX this ignores an explicit list_values = True in combination
        # with _inspec. The user should *never* do that anyway, but still...
        if _inspec:
            options['list_values'] = False

        defaults = OPTION_DEFAULTS.copy()
        # TODO: check the values too.
        for entry in options:
            if entry not in defaults:
                raise TypeError('Unrecognised option "%s".' % entry)

        # Add any explicit options to the defaults
        defaults.update(options)
        self._initialise(defaults)
        configspec = defaults['configspec']
        self._original_configspec = configspec
        self._load(infile, configspec)
Ejemplo n.º 7
0
    def __init__(self, infile=None, options=None, configspec=None,
                 encoding=None, interpolation=True, raise_errors=False,
                 list_values=True, create_empty=False, file_error=False,
                 stringify=True, indent_type=None, default_encoding=None,
                 unrepr=False, write_empty_values=False, _inspec=False,
                 parent_config=None):
        """Parse a config file or create a config file object."""
        self._inspec = _inspec
        # init the superclass
        # this is the only change - we pass the parent configobj if
        # available, to have lookup use its values
        Section.__init__(self, parent_config or self, 0, self)

        infile = infile or []
        if options is not None:
            import warnings
            warnings.warn('Passing in an options dictionary to ConfigObj() ',
                          'is deprecated. Use **options instead.',
                          DeprecationWarning, stacklevel=2)

        _options = {'configspec': configspec,
                    'encoding': encoding, 'interpolation': interpolation,
                    'raise_errors': raise_errors, 'list_values': list_values,
                    'create_empty': create_empty, 'file_error': file_error,
                    'stringify': stringify, 'indent_type': indent_type,
                    'default_encoding': default_encoding, 'unrepr': unrepr,
                    'write_empty_values': write_empty_values}

        options = dict(options or {})
        options.update(_options)

        # XXXX this ignores an explicit list_values = True in combination
        # with _inspec. The user should *never* do that anyway, but still...
        if _inspec:
            options['list_values'] = False

        defaults = OPTION_DEFAULTS.copy()
        # TODO: check the values too.
        for entry in options:
            if entry not in defaults:
                raise TypeError('Unrecognised option "%s".' % entry)

        # Add any explicit options to the defaults
        defaults.update(options)
        self._initialise(defaults)
        configspec = defaults['configspec']
        self._original_configspec = configspec
        self._load(infile, configspec)
Ejemplo n.º 8
0
    def from_conf(conf: configobj.Section):

        def _read_param(conf, key: str) -> List[Any]:
            try:
                return [conf.as_float(key), None, None]

            except TypeError:
                mapping = zip([float, float, eval], conf.as_list(key))
                return [f(x) for f, x in mapping]

        # ---

        early_stopping = None
        if 'early stopping' in conf:
            early_stopping = (
                int(conf['early stopping'][0]),
                float(conf['early stopping'][1]), )

        # ---

        encoder_init = (None, )
        if 'encoder init' in conf:
            method, *args = conf['encoder init']
            if method == 'uniform':
                args = float(args[0]), float(args[1])

            encoder_init = (method, ) + tuple(args)

        decoder_init_factor = None
        if 'decoder init factor' in conf:
            decoder_init_factor = float(conf['decoder init factor'])

        # ---

        return Parameters(
            learningrate=_read_param(conf, 'learningrate'),
            momentum=conf.as_float('momentum'),
            gumbel_scale=_read_param(conf, 'gumbel scale'),
            gumbel_temperature=_read_param(conf, 'gumbel temperature'),
            early_stopping=early_stopping,
            encoder_init=encoder_init,
            decoder_init_factor=decoder_init_factor)
Ejemplo n.º 9
0
Archivo: misc.py Proyecto: ayinalh/xoa
def dict_merge(*dd, **kwargs):
    """Merge dictionaries

    First dictionaries have priority over next

    Parameters
    ----------
    dd:
        Argument are interpreted as dictionary to merge.
        Those who are not dictionaries are skipped.
    mergesubdicts: optional
        Also merge dictionary items
        (like in a tree) [default: True].
    mergetuples: optional
        Also merge tuple items [default: False].
    mergelists: optional
        Also merge list items [default: False].
    unique: optional
        Uniquify lists and tuples [default: True].
    skipnones: optional
        Skip Nones [default: True].
    skipempty: optional
        Skip everything is not converted to False
        using bool [default: False].
    cls: optional
        Class to use. Default to the first class found in arguments
        that is not a :class:`dict`, else defaults to :class:`dict`.

    Example
    -------
    .. ipython:: python

        @suppress
        from xoa.misc import dict_merge
        d1 = dict(a=3, b=5, e=[1, 2])
        d2 = dict(a=5, c=7, e=[3, 4])
        print(dict_merge(d1, d2, mergelists=True))

    """
    # Options
    mergesubdicts = kwargs.get("mergesubdicts", True)
    mergelists = kwargs.get("mergelists", False)
    mergetuples = kwargs.get("mergetuples", False)
    unique = kwargs.get("unique", True)
    skipnones = kwargs.get("skipnones", True)
    overwriteempty = kwargs.get("overwriteempty", False)
    cls = kwargs.get("cls")
    dd = [_f for _f in dd if _f]

    # Get the class
    if cls is None:
        cls = dict
        for d in dd:
            if d.__class__ is not dict:
                cls = d.__class__
                break

    # Init
    from configobj import Section, ConfigObj

    if cls is Section:
        for d in dd:
            if isinstance(d, Section):
                break
        else:
            raise XoaError("Can't initialise Section for merging")
        outd = Section(d.parent, d.depth, d.main, name=d.name)
    else:
        outd = cls()

    # Loop
    for d in dd:
        if not isinstance(d, dict):
            continue

        # Content
        for key, val in d.items():
            if skipnones and val is None:
                continue
            # Not set so we set
            if key not in outd or (overwriteempty and is_empty(outd[key])):
                outd[key] = val
            # Merge subdict
            elif (mergesubdicts and isinstance(outd[key], dict)
                  and isinstance(val, dict)):
                outd[key] = dict_merge(outd[key], val, **kwargs)
            # Merge lists
            elif (mergelists and isinstance(outd[key], list)
                  and isinstance(val, list)):
                outd[key] += val
                if unique:
                    outd[key] = list(set((outd[key])))
            # Merge tuples
            elif (mergetuples and isinstance(outd[key], tuple)
                  and isinstance(val, tuple)):
                outd[key] += val
                if unique:
                    outd[key] = tuple(set(outd[key]))

    # Comments for ConfigObj instances
    if cls is ConfigObj:
        if not outd.initial_comment and hasattr(d, "initial_comment"):
            outd.initial_comment = d.initial_comment
        if not outd.final_comment and hasattr(d, "final_comment"):
            outd.final_comment = d.final_comment
        if hasattr(d, "inline_comments") and d.inline_comments:
            outd.inline_comments = dict_merge(outd.inline_comments,
                                              d.inline_comments,
                                              overwriteempty=True)

    return outd
Ejemplo n.º 10
0
 def __init__(self, project):
     """
     Constructor.
     """
     Section.__init__(self, project.parent, project.depth, 
                      project.main, project, project.name)
Ejemplo n.º 11
0
 def __init__(self, project):
     """
     Constructor.
     """
     Section.__init__(self, project.parent, project.depth, project.main,
                      project, project.name)