示例#1
0
文件: parser.py 项目: lingpy/lingpy
    def _add_entries(
            self,
            entry,
            source,
            function,
            override=False,
            **keywords):
        # check for empty entries etc.
        if not entry:
            raise ValueError('Entry was not properly specified.')

        lentry = entry.lower()

        def _apply(key, s, *args, **kwargs):
            try:
                res = function(s, *args, **kwargs)
            except:
                raise ValueError('Could not convert item ID: {0}.'.format(key))
            if override:
                self._data[key][self._header[lentry]] = res
            else:
                self._data[key].append(res)

        # check for override stuff, this causes otherwise an error message
        if entry not in self.header and override:
            return self.add_entries(entry, source, function, override=False)

        # check whether the stuff is already there
        if entry in self._header and not override:
            if confirm(
                "Column <{entry}> already exists, do you want to override?".format(
                    entry=entry)):
                keywords['override'] = True
                return self.add_entries(entry, source, function, **keywords)
            return  # pragma: no cover

        if not override:
            # get the new index into the header
            # add a new alias if this is not specified
            if entry.lower() not in self._alias2:
                self._alias2[entry.lower()] = [entry.lower(), entry.upper()]
                self._alias[entry.lower()] = entry.lower()
                self._alias[entry.upper()] = entry.lower()

            # get the true value
            name = self._alias[entry.lower()]

            # get the new index
            newIdx = max(self._header.values()) + 1

            # change the aliased header for each entry in alias2
            for a in self._alias2[name]:
                self._header[a] = newIdx

            self.header[name] = self._header[name]
            # add the entry to the columns! XXX
            self.columns.append(name)

            # modify the entries attribute
            self.entries = sorted(set(self.entries + [entry.lower()]))

        # check for multiple entries (separated by comma)
        if ',' in source:
            sources = source.split(',')
            idxs = [self._header[s] for s in sources]

            # iterate over the data and create the new entry
            for key in self:
                _apply(key, self[key], idxs)
        # if the source is a dictionary, this dictionary will be directly added to the
        # original data-storage of the wordlist
        elif isinstance(source, dict):
            for key in self:
                _apply(key, source[key])
        else:
            # get the index of the source in self
            idx = self._header[source]
            for key in self:
                _apply(key, self[key][idx], **keywords)
示例#2
0
    def _add_entries(self,
                     entry,
                     source,
                     function,
                     override=False,
                     **keywords):
        # check for empty entries etc.
        if not entry:
            raise ValueError('Entry was not properly specified.')

        lentry = entry.lower()

        def _apply(key, s, *args, **kwargs):
            try:
                res = function(s, *args, **kwargs)
            except:
                raise ValueError('Could not convert item ID: {0}.'.format(key))
            if override:
                self._data[key][self._header[lentry]] = res
            else:
                self._data[key].append(res)

        # check for override stuff, this causes otherwise an error message
        if entry not in self.header and override:
            return self.add_entries(entry, source, function, override=False)

        # check whether the stuff is already there
        if entry in self._header and not override:
            if confirm(
                    "Column <{entry}> already exists, do you want to override?"
                    .format(entry=entry)):
                keywords['override'] = True
                return self.add_entries(entry, source, function, **keywords)
            return  # pragma: no cover

        if not override:
            # get the new index into the header
            # add a new alias if this is not specified
            if entry.lower() not in self._alias2:
                self._alias2[entry.lower()] = [entry.lower(), entry.upper()]
                self._alias[entry.lower()] = entry.lower()
                self._alias[entry.upper()] = entry.lower()

            # get the true value
            name = self._alias[entry.lower()]

            # get the new index
            newIdx = max(self._header.values()) + 1

            # change the aliased header for each entry in alias2
            for a in self._alias2[name]:
                self._header[a] = newIdx

            self.header[name] = self._header[name]
            # add the entry to the columns! XXX
            self.columns.append(name)

            # modify the entries attribute
            self.entries = sorted(set(self.entries + [entry.lower()]))

        # check for multiple entries (separated by comma)
        if ',' in source:
            sources = source.split(',')
            idxs = [self._header[s] for s in sources]

            # iterate over the data and create the new entry
            for key in self:
                _apply(key, self[key], idxs)
        # if the source is a dictionary, this dictionary will be directly added to the
        # original data-storage of the wordlist
        elif isinstance(source, dict):
            for key in self:
                _apply(key, source[key])
        else:
            # get the index of the source in self
            idx = self._header[source]
            for key in self:
                _apply(key, self[key][idx], **keywords)