Python operator.itemgetter() Examples

The following are 30 code examples for showing how to use operator.itemgetter(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module operator , or try the search function .

Example 1
Project: pyspark-cassandra   Author: TargetHolding   File: rdd.py    License: 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
Project: wechatpy   Author: wechatpy   File: schedule.py    License: 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 3
Project: wechatpy   Author: wechatpy   File: calendar.py    License: 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 4
Project: VMAttack   Author: anatolikalysch   File: TraceAnalysis.py    License: 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 5
Project: fuku-ml   Author: fukuball   File: LogisticRegression.py    License: 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 6
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: nce.py    License: 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 7
Project: Paradrop   Author: ParadropLabs   File: store.py    License: 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 8
Project: Paradrop   Author: ParadropLabs   File: store.py    License: 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 9
Project: python-toolbox-for-rapid   Author: Esri   File: UpdateWeightTable.py    License: 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 10
Project: tldr.py   Author: lord63   File: cli.py    License: 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
Project: razzy-spinner   Author: rafasashi   File: collocations_app.py    License: 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 12
Project: razzy-spinner   Author: rafasashi   File: wordnet.py    License: 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 13
Project: gist-alfred   Author: danielecook   File: GithubObject.py    License: 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 14
Project: me-ica   Author: ME-ICA   File: classifier_node.py    License: 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 15
Project: naru   Author: naru-project   File: estimators.py    License: 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 16
Project: rift-python   Author: brunorijsman   File: stats.py    License: 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 17
Project: wechatpy   Author: wechatpy   File: schedule.py    License: MIT License 5 votes vote down vote up
def get(self, schedule_ids):
        """
        获取日程
        https://work.weixin.qq.com/api/doc/90000/90135/92624

        :param schedule_ids: 日程ID列表。一次最多可获取1000条
        :type schedule_ids: list[str]

        :return: 日程列表
        :rtype: list[dict]
        """
        return self._post(
            "oa/schedule/get", data={"schedule_id_list": schedule_ids}, result_processor=op.itemgetter("schedule_list"),
        ) 
Example 18
Project: wechatpy   Author: wechatpy   File: calendar.py    License: MIT License 5 votes vote down vote up
def update(self, calendar_id, summary, color, description="", shares=()):
        """
        更新日历
        https://work.weixin.qq.com/api/doc/90000/90135/92619

        :param calendar_id: 日历ID
        :param summary: 日历标题。1 ~ 128 字符
        :param color: 日历在终端上显示的颜色,RGB颜色编码16进制表示,例如:”#0000FF” 表示纯蓝色
        :param description: 日历描述。0 ~ 512 字符
        :param shares: 日历共享成员列表。最多2000人
        :type shares: list[str]
        """
        data = {
            "calendar": {
                "cal_id": calendar_id,
                "summary": summary,
                "color": color,
                "description": description,
                "shares": [{"userid": userid} for userid in shares],
            }
        }
        return self._post("oa/calendar/update", data=data, result_processor=op.itemgetter("cal_id")) 
Example 19
Project: wechatpy   Author: wechatpy   File: calendar.py    License: MIT License 5 votes vote down vote up
def get(self, calendar_ids):
        """
        获取日历
        https://work.weixin.qq.com/api/doc/90000/90135/92621

        :param calendar_ids: 日历ID列表。一次最多可获取1000条
        :type calendar_ids: list[str]

        :return: 日历列表
        :rtype: list[dict]
        """
        return self._post(
            "oa/calendar/get", data={"cal_id_list": calendar_ids}, result_processor=op.itemgetter("calendar_list"),
        ) 
Example 20
Project: cherrypy   Author: cherrypy   File: wspbus.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def publish(self, channel, *args, **kwargs):
        """Return output of all subscribers for the given channel."""
        if channel not in self.listeners:
            return []

        exc = ChannelFailures()
        output = []

        raw_items = (
            (self._priorities[(channel, listener)], listener)
            for listener in self.listeners[channel]
        )
        items = sorted(raw_items, key=operator.itemgetter(0))
        for priority, listener in items:
            try:
                output.append(listener(*args, **kwargs))
            except KeyboardInterrupt:
                raise
            except SystemExit:
                e = sys.exc_info()[1]
                # If we have previous errors ensure the exit code is non-zero
                if exc and e.code == 0:
                    e.code = 1
                raise
            except Exception:
                exc.handle_exception()
                if channel == 'log':
                    # Assume any further messages to 'log' will fail.
                    pass
                else:
                    self.log('Error in %r listener %r' % (channel, listener),
                             level=40, traceback=True)
        if exc:
            raise exc
        return output 
Example 21
Project: fuku-ml   Author: fukuball   File: LogisticRegression.py    License: MIT License 5 votes vote down vote up
def prediction(self, input_data='', mode='test_data'):

        prediction = {}
        prediction_list = {}
        prediction_return = 0.0
        ovo_vote = []

        for class_item in self.class_list:
            self.temp_W = self.W
            self.W = self.temp_W[class_item]
            prediction = super(MultiClassifier, self).prediction(input_data, mode)
            if self.decomposition == 'ovo':
                if prediction['prediction'] >= 0.5:
                    prediction_list[class_item] = class_item[0]
                else:
                    prediction_list[class_item] = class_item[1]
                ovo_vote.append(prediction_list[class_item])
            elif self.decomposition == 'ova':
                prediction_list[class_item] = prediction['prediction']
            self.W = self.temp_W
            self.temp_W = {}

        if self.decomposition == 'ovo':
            # counter = collections.Counter(ovo_vote)
            prediction_return = max(set(ovo_vote), key=ovo_vote.count)
        elif self.decomposition == 'ova':
            prediction_return = max(prediction_list.items(), key=operator.itemgetter(1))[0]

        return {
            "input_data_x": prediction['input_data_x'],
            "input_data_y": prediction['input_data_y'],
            "prediction": prediction_return,
            "prediction_list": prediction_list,
        } 
Example 22
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: metric.py    License: Apache License 2.0 5 votes vote down vote up
def update(self, labels, preds):
        mx.metric.check_label_shapes(labels, preds)
        label_weight = labels[0].asnumpy()
        preds = preds[0].asnumpy()
        tmp = []
        for i in range(preds.shape[0]):
            tmp.append((label_weight[i], preds[i]))
        tmp = sorted(tmp, key=itemgetter(1), reverse=True)
        label_sum = label_weight.sum()
        if label_sum == 0 or label_sum == label_weight.size:
            raise Exception("AUC with one class is undefined")

        label_one_num = np.count_nonzero(label_weight)
        label_zero_num = len(label_weight) - label_one_num
        total_area = label_zero_num * label_one_num
        height = 0
        width = 0
        area = 0
        for a, _ in tmp:
            if a == 1.0:
                height += 1.0
            else:
                width += 1.0
                area += height

        self.sum_metric += area / total_area
        self.num_inst += 1 
Example 23
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: nce.py    License: Apache License 2.0 5 votes vote down vote up
def update(self, labels, preds):
        preds = np.array([x.asnumpy() for x in preds])
        preds = preds.reshape((preds.shape[0] * preds.shape[1], preds.shape[2]))
        label_weight = labels[1].asnumpy()
        label_weight = label_weight.transpose((1, 0, 2))
        label_weight = label_weight.reshape((preds.shape[0], preds.shape[1]))

        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 24
def generate_stats(warnings):
    total_count = sum(warnings.values())
    sorted_warnings = sorted(warnings.items(), key=operator.itemgetter(1), reverse=True)
    return sorted_warnings, total_count 
Example 25
Project: DOTA_models   Author: ringringyi   File: data_utils.py    License: Apache License 2.0 5 votes vote down vote up
def sort_vocab_by_frequency(vocab_freq_map):
  """Sorts vocab_freq_map by count.

  Args:
    vocab_freq_map: dict<str term, int count>, vocabulary terms with counts.

  Returns:
    list<tuple<str term, int count>> sorted by count, descending.
  """
  return sorted(
      vocab_freq_map.items(), key=operator.itemgetter(1), reverse=True) 
Example 26
Project: DOTA_models   Author: ringringyi   File: cifar10_main.py    License: Apache License 2.0 5 votes vote down vote up
def __call__(self, op):
    if op.device:
      return op.device
    if op.type not in ['Variable', 'VariableV2', 'VarHandleOp']:
      return self.worker_device

    device_index, _ = min(enumerate(self.ps_sizes), key=operator.itemgetter(1))
    device_name = self.ps_devices[device_index]
    var_size = op.outputs[0].get_shape().num_elements()
    self.ps_sizes[device_index] += var_size

    return device_name 
Example 27
Project: graph-neural-networks   Author: alelab-upenn   File: visualTools.py    License: GNU General Public License v3.0 5 votes vote down vote up
def text_summary(self, mode, epoch, type, text, vocabulary, gt=True, max_length=20):
        for i, el in enumerate(text):  # text_list
            if not gt:  # we are printing a sample
                idx = el.nonzero().squeeze() + 1
            else:
                idx = el  # we are printing the ground truth

            words_list = itemgetter(*idx)(vocabulary)

            if len(words_list) <= max_length:
                self.writer.add_text('{}/{}_{}_{}'.format(mode, type, i, 'gt' if gt else 'prediction'), ', '.join(filter(lambda x: x != '<pad>', words_list)), epoch)
            else:
                self.writer.add_text('{}/{}_{}_{}'.format(mode, type, i, 'gt' if gt else 'prediction'), 'Number of sampled ingredients is too big: {}'.format(len(words_list)), epoch)

    # losses: dictionary of error labels and values 
Example 28
Project: Paradrop   Author: ParadropLabs   File: device.py    License: Apache License 2.0 5 votes vote down vote up
def print_pdconf(res):
    if res.ok:
        data = res.json()
        data.sort(key=operator.itemgetter("age"))

        print("{:3s} {:12s} {:20s} {:30s} {:5s}".format("Age", "Type", "Name",
            "Comment", "Pass"))
        for item in data:
            print("{age:<3d} {type:12s} {name:20s} {comment:30s} {success}".format(**item)) 
Example 29
Project: Paradrop   Author: ParadropLabs   File: node.py    License: Apache License 2.0 5 votes vote down vote up
def print_pdconf(data):
    data.sort(key=operator.itemgetter("age"))

    print("{:3s} {:12s} {:20s} {:30s} {:5s}".format("Age", "Type", "Name",
        "Comment", "Pass"))
    for item in data:
        print("{age:<3d} {type:12s} {name:20s} {comment:30s} {success}".format(**item)) 
Example 30
Project: fine-lm   Author: akzaidi   File: decoding.py    License: MIT License 5 votes vote down vote up
def _get_sorted_inputs(filename, num_shards=1, delimiter="\n"):
  """Returning inputs sorted according to length.

  Args:
    filename: path to file with inputs, 1 per line.
    num_shards: number of input shards. If > 1, will read from file filename.XX,
      where XX is FLAGS.worker_id.
    delimiter: str, delimits records in the file.

  Returns:
    a sorted list of inputs

  """
  tf.logging.info("Getting sorted inputs")
  # read file and sort inputs according them according to input length.
  if num_shards > 1:
    decode_filename = filename + ("%.2d" % FLAGS.worker_id)
  else:
    decode_filename = filename

  with tf.gfile.Open(decode_filename) as f:
    text = f.read()
    records = text.split(delimiter)
    inputs = [record.strip() for record in records]
    # Strip the last empty line.
    if not inputs[-1]:
      inputs.pop()
  input_lens = [(i, len(line.split())) for i, line in enumerate(inputs)]
  sorted_input_lens = sorted(input_lens, key=operator.itemgetter(1))
  # We'll need the keys to rearrange the inputs back into their original order
  sorted_keys = {}
  sorted_inputs = []
  for i, (index, _) in enumerate(sorted_input_lens):
    sorted_inputs.append(inputs[index])
    sorted_keys[index] = i
  return sorted_inputs, sorted_keys