Python operator.itemgetter() Examples

The following are 30 code examples of operator.itemgetter(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module operator , or try the search function .
Example #1
Source File: rdd.py    From pyspark-cassandra with Apache License 2.0 7 votes vote down vote up
def _spanning_iterator(self):
        ''' implements basic spanning on the python side operating on Rows '''
        # TODO implement in Java and support not only Rows

        columns = set(str(c) for c in self.columns)

        def spanning_iterator(partition):
            def key_by(columns):
                for row in partition:
                    k = Row(**{c: row.__getattr__(c) for c in columns})
                    for c in columns:
                        del row[c]

                    yield (k, row)

            for g, l in groupby(key_by(columns), itemgetter(0)):
                yield g, list(_[1] for _ in l)

        return spanning_iterator 
Example #2
Source File: collocations_app.py    From razzy-spinner with GNU General Public License v3.0 6 votes vote down vote up
def run(self):
            try:
                words = self.model.CORPORA[self.name]()
                from operator import itemgetter
                text = [w for w in words if len(w) > 2]
                fd = FreqDist(tuple(text[i:i+2]) for i in range(len(text)-1))
                vocab = FreqDist(text)
                scored = [((w1,w2), fd[(w1,w2)] ** 3 / float(vocab[w1] * vocab[w2])) for w1, w2 in fd]
                scored.sort(key=itemgetter(1), reverse=True)
                self.model.collocations = list(map(itemgetter(0), scored))
                self.model.queue.put(CORPUS_LOADED_EVENT)
            except Exception as e:
                print(e)
                self.model.queue.put(ERROR_LOADING_CORPUS_EVENT)

#def collocations():
#    colloc_strings = [w1 + ' ' + w2 for w1, w2 in self._collocations[:num]] 
Example #3
Source File: estimators.py    From naru with Apache License 2.0 6 votes vote down vote up
def next_partition_candidate(self, partitions, column_number, table,
                                 num_new_partitions, maxdiff_map):
        global_maxdiff = max(sorted(maxdiff_map.keys()))
        partition, cid = maxdiff_map[global_maxdiff][0]
        vals = partition.col_value_list[cid]
        counter = collections.Counter(vals)
        first_key = True
        prev_key = None
        diff = []
        for key in sorted(counter.keys()):
            if first_key:
                first_key = False
                prev_key = key
            else:
                spread = key - prev_key
                diff.append((prev_key, (spread * counter[prev_key])))
                prev_key = key
        diff.append((prev_key, (0 * counter[prev_key])))
        partition_boundaries = sorted(
            list(
                tp[0]
                for tp in sorted(diff, key=operator.itemgetter(
                    1), reverse=True)[:min(num_new_partitions - 1, len(diff))]))
        return (partitions.index(partition), cid, partition_boundaries,
                global_maxdiff) 
Example #4
Source File: stats.py    From rift-python with Apache License 2.0 6 votes vote down vote up
def table(self, exclude_zero, sort_by_description=False):
        rows = []
        for stat in self._stats:
            if not exclude_zero or not stat.is_zero():
                rows.append([
                    stat.description(),
                    stat.value_display_str(),
                    stat.rate_display_str(),
                    stat.last_change_display_str()
                ])
        if sort_by_description:
            rows = sorted(rows, key=operator.itemgetter(0))
        tab = table.Table()
        tab.add_row([
            "Description",
            "Value",
            ["Last Rate", "Over Last {} Changes".format(RATE_HISTORY)],
            ["Last Change"]
        ])
        tab.add_rows(rows)
        return tab 
Example #5
Source File: classifier_node.py    From me-ica with GNU Lesser General Public License v2.1 6 votes vote down vote up
def rank(self, x, threshold=None):
        """Returns ordered list with all labels ordered according to prob(x)
        (e.g., [[3 1 2], [2 1 3], ...]).

        The optional threshold parameter is used to exclude labels having equal
        or less probability. E.g. threshold=0 excludes all labels with zero
        probability.
        """
        all_ranking = []
        prob = self.prob(x)
        for p in prob:
            if threshold is None:
                ranking = p.items()
            else:
                ranking = ((k, v) for k, v in p.items() if v > threshold)
            result = [k for k, v in
                      sorted(ranking, key=operator.itemgetter(1), reverse=True)]
            all_ranking.append(result)
        return all_ranking 
Example #6
Source File: store.py    From Paradrop with Apache License 2.0 6 votes vote down vote up
def list_chutes(ctx):
    """
    List chutes in the store that you own or have access to.
    """
    client = ControllerClient()
    result = client.list_chutes()
    if len(result) > 0:
        click.echo("Name                             Ver Description")
    for chute in sorted(result, key=operator.itemgetter('name')):
        # Show the summary if available. This is intended to be
        # a shorter description than the description field.
        summary = chute.get('summary', None)
        if summary is None:
            summary = chute.get('description', '')
        click.echo("{:32s} {:3d} {}".format(chute['name'],
            chute['current_version'], summary))
    return result 
Example #7
Source File: GithubObject.py    From gist-alfred with MIT License 6 votes vote down vote up
def get__repr__(self, params):
        """
        Converts the object to a nicely printable string.
        """
        def format_params(params):
            if atLeastPython3:
                items = params.items()
            else:
                items = list(params.items())
            for k, v in sorted(items, key=itemgetter(0), reverse=True):
                isText = isinstance(v, (str, unicode))
                if isText and not atLeastPython3:
                    v = v.encode('utf-8')
                yield '{k}="{v}"'.format(k=k, v=v) if isText else '{k}={v}'.format(k=k, v=v)
        return '{class_name}({params})'.format(
            class_name=self.__class__.__name__,
            params=", ".join(list(format_params(params)))
        ) 
Example #8
Source File: wordnet.py    From razzy-spinner with GNU General Public License v3.0 6 votes vote down vote up
def hypernym_distances(self, distance=0, simulate_root=False):
        """
        Get the path(s) from this synset to the root, counting the distance
        of each node from the initial node on the way. A set of
        (synset, distance) tuples is returned.

        :type distance: int
        :param distance: the distance (number of edges) from this hypernym to
            the original hypernym ``Synset`` on which this method was called.
        :return: A set of ``(Synset, int)`` tuples where each ``Synset`` is
           a hypernym of the first ``Synset``.
        """
        distances = set([(self, distance)])
        for hypernym in self._hypernyms() + self._instance_hypernyms():
            distances |= hypernym.hypernym_distances(distance+1, simulate_root=False)
        if simulate_root:
            fake_synset = Synset(None)
            fake_synset._name = '*ROOT*'
            fake_synset_distance = max(distances, key=itemgetter(1))[1]
            distances.add((fake_synset, fake_synset_distance+1))
        return distances 
Example #9
Source File: store.py    From Paradrop with Apache License 2.0 6 votes vote down vote up
def list_versions(ctx, name):
    """
    List versions of a chute in the store.

    NAME must be the name of a chute in the store.
    """
    client = ControllerClient()
    result = client.list_versions(name)
    if len(result) > 0:
        click.echo("Version GitCheckout")
    for version in sorted(result, key=operator.itemgetter('version')):
        try:
            code = version['config']['download']['checkout']
        except:
            code = "N/A"
        click.echo("{:7s} {}".format(str(version['version']), code))
    return result 
Example #10
Source File: cli.py    From tldr.py with MIT License 6 votes vote down vote up
def build_index():
    repo_directory = get_config()['repo_directory']
    index_path = path.join(repo_directory, 'pages', 'index.json')
    page_path = path.join(repo_directory, 'pages')

    tree_generator = os.walk(page_path)
    folders = next(tree_generator)[1]
    commands, new_index = {}, {}
    for folder in folders:
        pages = next(tree_generator)[2]
        for page in pages:
            command_name = path.splitext(page)[0]
            if command_name not in commands:
                commands[command_name] = {'name': command_name,
                                          'platform': [folder]}
            else:
                commands[command_name]['platform'].append(folder)
    command_list = [item[1] for item in
                    sorted(commands.items(), key=itemgetter(0))]
    new_index['commands'] = command_list

    with open(index_path, mode='w') as f:
        json.dump(new_index, f) 
Example #11
Source File: nce.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def update(self, labels, preds):
        label_weight = labels[1].asnumpy()
        preds = preds[0].asnumpy()
        tmp = []
        for i in range(preds.shape[0]):
            for j in range(preds.shape[1]):
                tmp.append((label_weight[i][j], preds[i][j]))
        tmp = sorted(tmp, key=itemgetter(1), reverse=True)
        m = 0.0
        n = 0.0
        z = 0.0
        k = 0
        for a, _ in tmp:
            if a > 0.5:
                m += 1.0
                z += len(tmp) - k
            else:
                n += 1.0
            k += 1
        z -= m * (m + 1.0) / 2.0
        z /= m
        z /= n
        self.sum_metric += z
        self.num_inst += 1 
Example #12
Source File: UpdateWeightTable.py    From python-toolbox-for-rapid with Apache License 2.0 6 votes vote down vote up
def sort_by_column(self, csv_cont, col, reverse=False):
        """
        Sorts CSV contents by column name (if col argument is type <str>)
        or column index (if col argument is type <int>).

        """
        header = csv_cont[0]
        body = csv_cont[1:]
        if isinstance(col, str):
            col_index = header.index(col)
        else:
            col_index = col
        body = sorted(body,
               key=operator.itemgetter(col_index),
               reverse=reverse)
        body.insert(0, header)
        return body 
Example #13
Source File: LogisticRegression.py    From fuku-ml with MIT License 6 votes vote down vote up
def score_function_all_class(self, x, W):

        score_list = {}
        ovo_vote = []

        for class_item in self.class_list:
            score = self.score_function(x, W[class_item])
            if self.decomposition == 'ovo':
                if score >= 0.5:
                    score_list[class_item] = class_item[0]
                else:
                    score_list[class_item] = class_item[1]
                ovo_vote.append(score_list[class_item])
            elif self.decomposition == 'ova':
                score_list[class_item] = score

        if self.decomposition == 'ovo':
            return max(set(ovo_vote), key=ovo_vote.count)
        elif self.decomposition == 'ova':
            return max(score_list.items(), key=operator.itemgetter(1))[0] 
Example #14
Source File: TraceAnalysis.py    From VMAttack with MIT License 6 votes vote down vote up
def address_count(trace):
    """
    Count the diffetent occurences of the addresses in the trace and return a sorted(highest->lowest) occurence list
    :param trace: execution trace of the binary
    :return: sorted list starting with the highest address count and ending with the lowest
    """
    trace = [line.addr for line in trace]
    analysis_result = {}
    for addr in trace:
        # for heuristic analysis the count of address
        count = trace.count(addr)
        if addr not in analysis_result.keys():
            analysis_result[addr] = count
    # sort the analysis result by most common addresses
    sorted_result = sorted(analysis_result.items(), key=operator.itemgetter(1))
    sorted_result.reverse()
    return sorted_result 
Example #15
Source File: calendar.py    From wechatpy with MIT License 6 votes vote down vote up
def add(self, organizer, summary, color, description="", shares=()):
        """
        创建日历
        https://work.weixin.qq.com/api/doc/90000/90135/92618

        :param organizer: 指定的组织者userid。注意该字段指定后不可更新
        :param summary: 日历标题。1 ~ 128 字符
        :param color: 日历在终端上显示的颜色,RGB颜色编码16进制表示,例如:”#0000FF” 表示纯蓝色
        :param description: 日历描述。0 ~ 512 字符
        :param shares: 日历共享成员列表。最多2000人
        :type shares: list[str]

        :return: 日历ID
        :rtype: str
        """
        data = {
            "calendar": {
                "organizer": organizer,
                "summary": summary,
                "color": color,
                "description": description,
                "shares": [{"userid": userid} for userid in shares],
            }
        }
        return self._post("oa/calendar/add", data=data, result_processor=op.itemgetter("cal_id")) 
Example #16
Source File: schedule.py    From wechatpy with MIT License 6 votes vote down vote up
def get_by_calendar(self, calendar_id, offset=0, limit=500):
        """
        获取日历下的日程列表
        https://work.weixin.qq.com/api/doc/90000/90135/92626
        (注意,被取消的日程也可以拉取详情,调用者需要检查status)

        :param calendar_id: 日历ID
        :param offset: 分页,偏移量, 默认为0
        :param limit: 分页,预期请求的数据量,默认为500,取值范围 1 ~ 1000

        :return: 日程列表
        :rtype: list[dict]
        """
        return self._post(
            "oa/schedule/get_by_calendar",
            data={"cal_id": calendar_id, "offset": offset, "limit": limit},
            result_processor=op.itemgetter("schedule_list"),
        ) 
Example #17
Source File: per.py    From asn1tools with MIT License 5 votes vote down vote up
def __init__(self, name, values, numeric):
        super(Enumerated, self).__init__(name, 'ENUMERATED')
        root, additions = enum_values_split(values)
        root = sorted(root, key=itemgetter(1))

        # Root.
        index_to_data, data_to_index = self.create_maps(root,
                                                        numeric)
        self.root_index_to_data = index_to_data
        self.root_data_to_index = data_to_index
        self.root_number_of_bits = integer_as_number_of_bits(len(index_to_data) - 1)

        if numeric:
            self.root_data_to_value = {k: k for k in enum_values_as_dict(root)}
        else:
            self.root_data_to_value = {v: k for k, v in enum_values_as_dict(root).items()}

        # Optional additions.
        if additions is None:
            index_to_data = None
            data_to_index = None
        else:
            index_to_data, data_to_index = self.create_maps(additions,
                                                            numeric)

        self.additions_index_to_data = index_to_data
        self.additions_data_to_index = data_to_index 
Example #18
Source File: entities.py    From plugin.video.kmediatorrent with GNU General Public License v3.0 5 votes vote down vote up
def extract_from_server_meta(self, meta):
        '''
        Will extract information for the "ip_address", "user_agent" and "locale"
        properties from the given WSGI REQUEST META variable or equivalent.
        '''
        if 'REMOTE_ADDR' in meta and meta['REMOTE_ADDR']:
            ip = None
            for key in ('HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR'):
                if key in meta and not ip:
                    ips = meta.get(key, '').split(',')
                    ip = ips[len(ips) - 1].strip()
                    if not utils.is_valid_ip(ip):
                        ip = ''
                    if utils.is_private_ip(ip):
                        ip = ''
            if ip:
                self.ip_address = ip

        if 'HTTP_USER_AGENT' in meta and meta['HTTP_USER_AGENT']:
            self.user_agent = meta['HTTP_USER_AGENT']

        if 'HTTP_ACCEPT_LANGUAGE' in meta and meta['HTTP_ACCEPT_LANGUAGE']:
            user_locals = []
            matched_locales = utils.validate_locale(meta['HTTP_ACCEPT_LANGUAGE'])
            if matched_locales:
                lang_lst = map((lambda x: x.replace('-', '_')), (i[1] for i in matched_locales))
                quality_lst = map((lambda x: x and x or 1), (float(i[4] and i[4] or '0') for i in matched_locales))
                lang_quality_map = map((lambda x, y: (x, y)), lang_lst, quality_lst)
                user_locals = [x[0] for x in sorted(lang_quality_map, key=itemgetter(1), reverse=True)]

            if user_locals:
                self.locale = user_locals[0]

        return self 
Example #19
Source File: oer.py    From asn1tools with MIT License 5 votes vote down vote up
def get_enumerated_values(self, type_):
        return sorted([(canonical(data), value)
                       for data, value in type_.data_to_value.items()],
                      key=itemgetter(1)) 
Example #20
Source File: transfer.py    From monero-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def url_data(url):
    gs = re.compile(
        r'^(?:(?P<user>[a-z0-9_-]+)?(?::(?P<password>[^@]+))?@)?(?P<host>[^:\s]+)(?::(?P<port>[0-9]+))?$'
        ).match(url).groupdict()
    return dict(filter(operator.itemgetter(1), gs.items())) 
Example #21
Source File: walletdump.py    From monero-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def url_data(url):
    gs = re.compile(
        r'^(?:(?P<user>[a-z0-9_-]+)?(?::(?P<password>[^@]+))?@)?(?P<host>[^:\s]+)(?::(?P<port>[0-9]+))?$'
        ).match(url).groupdict()
    return dict(filter(operator.itemgetter(1), gs.items())) 
Example #22
Source File: base.py    From zun with Apache License 2.0 5 votes vote down vote up
def check_for_versions_intersection(func_list):
        """Determines whether function list intersections

        General algorithm:
        https://en.wikipedia.org/wiki/Intersection_algorithm

        :param func_list: list of VersionedMethod objects
        :return: boolean
        """

        pairs = []
        counter = 0

        for f in func_list:
            pairs.append((f.start_version, 1))
            pairs.append((f.end_version, -1))

        pairs.sort(key=operator.itemgetter(1), reverse=True)
        pairs.sort(key=operator.itemgetter(0))

        for p in pairs:
            counter += p[1]

            if counter > 1:
                return True

        return False 
Example #23
Source File: misc.py    From verge3d-blender-addon with GNU General Public License v3.0 5 votes vote down vote up
def most_common(self, n=None):
        '''List the n most common elements and their counts from the most
        common to the least.  If n is None, then list all element counts.

        >>> Counter('abcdeabcdabcaba').most_common(3)
        [('a', 5), ('b', 4), ('c', 3)]

        '''
        # Emulate Bag.sortedByCount from Smalltalk
        if n is None:
            return sorted(self.items(), key=_itemgetter(1), reverse=True)
        return _heapq.nlargest(n, self.items(), key=_itemgetter(1)) 
Example #24
Source File: util.py    From ConvLab with MIT License 5 votes vote down vote up
def batch_get(arr, idxs):
    '''Get multi-idxs from an array depending if it's a python list or np.array'''
    if isinstance(arr, (list, deque)):
        return np.array(operator.itemgetter(*idxs)(arr))
    else:
        return arr[idxs] 
Example #25
Source File: compat.py    From razzy-spinner with GNU General Public License v3.0 5 votes vote down vote up
def most_common(self, n=None):
                '''List the n most common elements and their counts from the most
                common to the least.  If n is None, then list all element counts.

                >>> Counter('abracadabra').most_common(3)
                [('a', 5), ('r', 2), ('b', 2)]

                '''
                if n is None:
                    return sorted(self.iteritems(), key=itemgetter(1), reverse=True)
                return nlargest(n, self.iteritems(), key=itemgetter(1)) 
Example #26
Source File: table.py    From razzy-spinner with GNU General Public License v3.0 5 votes vote down vote up
def sort_by(self, column_index, order='toggle'):
        """
        Sort the rows in this table, using the specified column's
        values as a sort key.

        :param column_index: Specifies which column to sort, using
            either a column index (int) or a column's label name
            (str).

        :param order: Specifies whether to sort the values in
            ascending or descending order:

              - ``'ascending'``: Sort from least to greatest.
              - ``'descending'``: Sort from greatest to least.
              - ``'toggle'``: If the most recent call to ``sort_by()``
                sorted the table by the same column (``column_index``),
                then reverse the rows; otherwise sort in ascending
                order.
        """
        if order not in ('ascending', 'descending', 'toggle'):
            raise ValueError('sort_by(): order should be "ascending", '
                             '"descending", or "toggle".')
        column_index = self.column_index(column_index)
        config_cookie = self._save_config_info(index_by_id=True)

        # Sort the rows.
        if order == 'toggle' and column_index == self._sortkey:
            self._rows.reverse()
        else:
            self._rows.sort(key=operator.itemgetter(column_index),
                            reverse=(order=='descending'))
            self._sortkey = column_index

        # Redraw the table.
        self._fill_table()
        self._restore_config_info(config_cookie, index_by_id=True, see=True)
        if self._DEBUG: self._check_table_vs_mlb() 
Example #27
Source File: agreement.py    From razzy-spinner with GNU General Public License v3.0 5 votes vote down vote up
def _grouped_data(self, field, data=None):
        data = data or self.data
        return groupby(sorted(data, key=itemgetter(field)), itemgetter(field)) 
Example #28
Source File: classify.py    From product-classifier with MIT License 5 votes vote down vote up
def classify(self, name, description=None, keywords=None):
        """
        Classifies the text using the internal classifier. Returns a
        probability distribution of the labels associated with the text.
        """
        features = self.featurizer.featurize(name, description, keywords)
        probdist = self._classifier.prob_classify(features)
        labels   = [(label, probdist.prob(label))
                    for label in probdist.samples()
                    if probdist.prob(label) > 0.01]
        return sorted(labels, key=itemgetter(1), reverse=True) 
Example #29
Source File: tabview.py    From OpenTrader with GNU Lesser General Public License v3.0 5 votes vote down vote up
def sort_by_column_natural_reverse(self):
        xp = self.x + self.win_x
        self.data = self.sorted_nicely(self.data, itemgetter(xp), rev=True) 
Example #30
Source File: tabview.py    From OpenTrader with GNU Lesser General Public License v3.0 5 votes vote down vote up
def sort_by_column_natural(self):
        xp = self.x + self.win_x
        self.data = self.sorted_nicely(self.data, itemgetter(xp))