Python operator.attrgetter() Examples

The following are 30 code examples of operator.attrgetter(). 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: test_construction.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_construction_with_alt_tz_localize(self, kwargs, tz_aware_fixture):
        tz = tz_aware_fixture
        i = pd.date_range('20130101', periods=5, freq='H', tz=tz)
        kwargs = {key: attrgetter(val)(i) for key, val in kwargs.items()}

        if str(tz) in ('UTC', 'tzutc()'):
            warn = None
        else:
            warn = FutureWarning

        with tm.assert_produces_warning(warn, check_stacklevel=False):
            result = DatetimeIndex(i.tz_localize(None).asi8, **kwargs)
        expected = DatetimeIndex(i, **kwargs)
        tm.assert_index_equal(result, expected)

        # localize into the provided tz
        i2 = DatetimeIndex(i.tz_localize(None).asi8, tz='UTC')
        expected = i.tz_localize(None).tz_localize('UTC')
        tm.assert_index_equal(i2, expected)

        # incompat tz/dtype
        pytest.raises(ValueError, lambda: DatetimeIndex(
            i.tz_localize(None).asi8, dtype=i.dtype, tz='US/Pacific')) 
Example #2
Source File: util.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _manage_size(self):
        while len(self) > self.capacity + self.capacity * self.threshold:
            bytime = sorted(
                dict.values(self),
                key=operator.attrgetter("timestamp"),
                reverse=True,
            )
            for item in bytime[self.capacity :]:
                try:
                    del self[item.key]
                except KeyError:
                    # if we couldn't find a key, most likely some other thread
                    # broke in on us. loop around and try again
                    break


# Regexp to match python magic encoding line 
Example #3
Source File: file_info.py    From baseband with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, attr, needs=(), default=None, doc=None,
                 missing=None, copy=False):
        needs = tuple(needs) if isinstance(needs, (tuple, list)) else (needs,)
        if callable(attr):
            self.fget = attr
            self.attr = attr.__name__
            if doc is None:
                doc = attr.__doc__
        else:
            self.attr = attr
            full_attr = '.'.join(needs+(attr,))
            self.fget = operator.attrgetter(full_attr) if needs else None
            if doc is None:
                doc = "Link to " + full_attr.replace('_parent', 'parent')

        self.needs = needs if '_parent' in needs else ('_parent',) + needs
        self.default = default
        self.missing = missing
        self.copy = copy
        self.__doc__ = doc 
Example #4
Source File: util.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _manage_size(self):
        while len(self) > self.capacity + self.capacity * self.threshold:
            bytime = sorted(
                dict.values(self),
                key=operator.attrgetter("timestamp"),
                reverse=True,
            )
            for item in bytime[self.capacity :]:
                try:
                    del self[item.key]
                except KeyError:
                    # if we couldn't find a key, most likely some other thread
                    # broke in on us. loop around and try again
                    break


# Regexp to match python magic encoding line 
Example #5
Source File: test_transaction.py    From monero-python with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_sorting(self):
        pmts = [
            IncomingPayment(transaction=Transaction(height=10)),
            IncomingPayment(transaction=Transaction(height=12)),
            IncomingPayment(transaction=Transaction(height=13)),
            IncomingPayment(transaction=Transaction(height=None)),
            IncomingPayment(transaction=Transaction(height=100)),
            IncomingPayment(transaction=Transaction(height=None)),
            IncomingPayment(transaction=Transaction(height=1))
        ]
        for i in range(1680):    # 1/3 of possible permutations
            sorted_pmts = sorted(pmts, key=_ByHeight)
            self.assertEqual(
                list(map(attrgetter('height'), map(attrgetter('transaction'), sorted_pmts))),
                [None, None, 100, 13, 12, 10, 1])
            random.shuffle(pmts) 
Example #6
Source File: compiler.py    From asn1tools with MIT License 6 votes vote down vote up
def compile_members(self,
                        members,
                        module_name,
                        sort_by_tag=False):
        compiled_members = []
        has_extension_marker = False

        for member in members:
            if member == EXTENSION_MARKER:
                has_extension_marker = True
                continue

            if isinstance(member, list):
                group_members, _ = self.compile_members(member,
                                                        module_name)
                compiled_members.extend(group_members)
                continue

            compiled_member = self.compile_member(member, module_name)
            compiled_members.append(compiled_member)

        if sort_by_tag:
            compiled_members = sorted(compiled_members, key=attrgetter('tag'))

        return compiled_members, has_extension_marker 
Example #7
Source File: __init__.py    From custodia with GNU General Public License v3.0 6 votes vote down vote up
def handle_plugins(args):
    result = []
    errmsg = "**ERR** {0} ({1.__class__.__name__}: {1})"
    for plugin in PLUGINS:
        result.append('[{}]'.format(plugin))
        eps = pkg_resources.iter_entry_points(plugin)
        eps = sorted(eps, key=operator.attrgetter('name'))
        for ep in eps:
            try:
                if hasattr(ep, 'resolve'):
                    ep.resolve()
                else:
                    ep.load(require=False)
            except Exception as e:  # pylint: disable=broad-except
                if args.verbose:
                    result.append(errmsg.format(ep, e))
            else:
                result.append(str(ep))
        result.append('')
    return result[:-1] 
Example #8
Source File: wifi.py    From ideascube with GNU Affero General Public License v3.0 6 votes vote down vote up
def all(cls):
        result = []

        for connection in NMSettings.ListConnections():
            try:
                conn_settings = connection.GetSettings()
            except DBusException as e:
                # NM throws an exception if we try to use GetSettings on a
                # connection owned by another user. We just ignore it.
                continue

            if '802-11-wireless' not in conn_settings:
                continue

            result.append(cls(connection))

        return OrderedDict(
            [(c.ssid, c) for c in sorted(result, key=attrgetter('ssid'))]) 
Example #9
Source File: wifi.py    From ideascube with GNU Affero General Public License v3.0 6 votes vote down vote up
def all(cls):
        result = []

        known_connections = KnownWifiConnection.all()

        device = get_wifi_device()
        aps = device.SpecificDevice().GetAllAccessPoints()
        aps.sort(key=attrgetter('Frequency'), reverse=True)
        aps.sort(key=attrgetter('Ssid'))

        for ssid, grouper in groupby(aps, attrgetter('Ssid')):
            if ssid:
                # There often are more than one network for a given SSID (for
                # example at 2.4 and 5GHz), so we keep only the one with the
                # highest frequency.
                ap = next(grouper)
                result.append(cls(ap, device, known_connections))

        return OrderedDict(
            [(n.ssid, n) for n in sorted(
                result, key=attrgetter('strength'), reverse=True)]) 
Example #10
Source File: sqlalchemy.py    From gnocchi with Apache License 2.0 6 votes vote down vote up
def update_archive_policy(self, name, ap_items):
        with self.facade.independent_writer() as session:
            ap = session.query(ArchivePolicy).get(name)
            if not ap:
                raise indexer.NoSuchArchivePolicy(name)
            current = sorted(ap.definition,
                             key=operator.attrgetter('granularity'))
            new = sorted(ap_items, key=operator.attrgetter('granularity'))
            if len(current) != len(new):
                raise indexer.UnsupportedArchivePolicyChange(
                    name, 'Cannot add or drop granularities')
            for c, n in zip(current, new):
                if c.granularity != n.granularity:
                    raise indexer.UnsupportedArchivePolicyChange(
                        name, '%s granularity interval was changed'
                        % utils.timespan_total_seconds(c.granularity))
            # NOTE(gordc): ORM doesn't update JSON column unless new
            ap.definition = ap_items
            return ap 
Example #11
Source File: cloudpickle.py    From pywren-ibm-cloud with Apache License 2.0 6 votes vote down vote up
def save_attrgetter(self, obj):
        """attrgetter serializer"""
        class Dummy(object):
            def __init__(self, attrs, index=None):
                self.attrs = attrs
                self.index = index
            def __getattribute__(self, item):
                attrs = object.__getattribute__(self, "attrs")
                index = object.__getattribute__(self, "index")
                if index is None:
                    index = len(attrs)
                    attrs.append(item)
                else:
                    attrs[index] = ".".join([attrs[index], item])
                return type(self)(attrs, index)
        attrs = []
        obj(Dummy(attrs))
        return self.save_reduce(operator.attrgetter, tuple(attrs)) 
Example #12
Source File: selection.py    From pyshgp with MIT License 6 votes vote down vote up
def select_one(self, population: Population) -> Individual:
        """Return single individual from population.

        Parameters
        ----------
        population
            A Population of Individuals.

        Returns
        -------
        Individual
            The selected Individual.

        """
        tournament = choice(population, self.tournament_size, replace=False)
        return min(tournament, key=attrgetter('total_error')) 
Example #13
Source File: fields.py    From jbox with MIT License 6 votes vote down vote up
def __init__(self, label=None, validators=None, query_factory=None,
                 get_pk=None, get_label=None, allow_blank=False,
                 blank_text='', **kwargs):
        super(QuerySelectField, self).__init__(label, validators, **kwargs)
        self.query_factory = query_factory

        if get_pk is None:
            if not has_identity_key:
                raise Exception('The sqlalchemy identity_key function could not be imported.')
            self.get_pk = get_pk_from_identity
        else:
            self.get_pk = get_pk

        if get_label is None:
            self.get_label = lambda x: x
        elif isinstance(get_label, string_types):
            self.get_label = operator.attrgetter(get_label)
        else:
            self.get_label = get_label

        self.allow_blank = allow_blank
        self.blank_text = blank_text
        self.query = None
        self._object_list = None 
Example #14
Source File: lint.py    From linter-pylama with MIT License 6 votes vote down vote up
def prepare_checkers(self):
        """return checkers needed for activated messages and reports"""
        if not self.config.reports:
            self.disable_reporters()
        # get needed checkers
        neededcheckers = [self]
        for checker in self.get_checkers()[1:]:
            messages = set(msg for msg in checker.msgs
                           if self.is_message_enabled(msg))
            if (messages or
                    any(self.report_is_enabled(r[0]) for r in checker.reports)):
                neededcheckers.append(checker)
        # Sort checkers by priority
        neededcheckers = sorted(neededcheckers,
                                key=operator.attrgetter('priority'),
                                reverse=True)
        return neededcheckers

    # pylint: disable=unused-argument 
Example #15
Source File: __init__.py    From jbox with MIT License 5 votes vote down vote up
def add(self, dist):
        """Add `dist` if we ``can_add()`` it and it has not already been added
        """
        if self.can_add(dist) and dist.has_version():
            dists = self._distmap.setdefault(dist.key, [])
            if dist not in dists:
                dists.append(dist)
                dists.sort(key=operator.attrgetter('hashcmp'), reverse=True) 
Example #16
Source File: group.py    From dephell with MIT License 5 votes vote down vote up
def best_release(self) -> 'Release':
        strategy = max if config['strategy'] == 'max' else min
        best_time = strategy(release.time for release in self.releases)
        best_releases = [release for release in self.releases if release.time == best_time]
        if len(best_releases) == 1:
            return best_releases[0]
        return strategy(self.releases, key=attrgetter('version')) 
Example #17
Source File: test_footprint_tile_occurrence.py    From buzzard with Apache License 2.0 5 votes vote down vote up
def assert_property_tile_size(src, tiles, size, occx, occy, boundary_effect_locus):
    w = np.vectorize(operator.attrgetter('w'))(tiles.flatten())
    assert np.unique(w).size == 1
    h = np.vectorize(operator.attrgetter('h'))(tiles.flatten())
    assert np.unique(h).size == 1 
Example #18
Source File: fields.py    From jbox with MIT License 5 votes vote down vote up
def __init__(self, label=None, validators=None, queryset=None, get_label=None, allow_blank=False, blank_text='', **kwargs):
        super(QuerySetSelectField, self).__init__(label, validators, **kwargs)
        self.allow_blank = allow_blank
        self.blank_text = blank_text
        self._set_data(None)
        if queryset is not None:
            self.queryset = queryset.all()  # Make sure the queryset is fresh

        if get_label is None:
            self.get_label = lambda x: x
        elif isinstance(get_label, string_types):
            self.get_label = operator.attrgetter(get_label)
        else:
            self.get_label = get_label 
Example #19
Source File: fields.py    From jbox with MIT License 5 votes vote down vote up
def __init__(self, label=None, validators=None, reference_class=None,
                 get_label=None, allow_blank=False, blank_text='', **kwargs):
        super(KeyPropertyField, self).__init__(label, validators, **kwargs)
        if get_label is None:
            self.get_label = lambda x: x
        elif isinstance(get_label, basestring):
            self.get_label = operator.attrgetter(get_label)
        else:
            self.get_label = get_label

        self.allow_blank = allow_blank
        self.blank_text = blank_text
        self._set_data(None)
        if reference_class is not None:
            self.query = reference_class.query() 
Example #20
Source File: util.py    From jbox with MIT License 5 votes vote down vote up
def _manage_size(self):
        while len(self) > self.capacity + self.capacity * self.threshold:
            bytime = sorted(dict.values(self),
                            key=operator.attrgetter('timestamp'), reverse=True)
            for item in bytime[self.capacity:]:
                try:
                    del self[item.key]
                except KeyError:
                    # if we couldn't find a key, most likely some other thread
                    # broke in on us. loop around and try again
                    break

# Regexp to match python magic encoding line 
Example #21
Source File: fetch.py    From django-echarts with MIT License 5 votes vote down vote up
def ifetch_single(iterable, key, default=EMPTY, getter=None):
    """
    getter() g(item, key):pass
    """

    def _getter(item):
        if getter:
            custom_getter = partial(getter, key=key)
            return custom_getter(item)
        else:
            try:
                attrgetter = operator.attrgetter(key)
                return attrgetter(item)
            except AttributeError:
                pass

            try:
                itemgetter = operator.itemgetter(key)
                return itemgetter(item)
            except KeyError:
                pass

            if default is not EMPTY:
                return default

            raise ValueError('Item %r has no attr or key for %r' % (item, key))

    return map(_getter, iterable) 
Example #22
Source File: __init__.py    From recruit with Apache License 2.0 5 votes vote down vote up
def add(self, dist):
        """Add `dist` if we ``can_add()`` it and it has not already been added
        """
        if self.can_add(dist) and dist.has_version():
            dists = self._distmap.setdefault(dist.key, [])
            if dist not in dists:
                dists.append(dist)
                dists.sort(key=operator.attrgetter('hashcmp'), reverse=True) 
Example #23
Source File: catalog.py    From ideascube with GNU Affero General Public License v3.0 5 votes vote down vote up
def list_available(self, ids):
        ids = self._expand_package_ids(ids, self._available)
        pkgs = []

        for pkg_id in ids:
            try:
                pkgs.append(self._get_package(pkg_id, self._available))

            except (InvalidPackageType, MissingPackageMetadata, NoSuchPackage):
                continue

        return sorted(pkgs, key=attrgetter('id')) 
Example #24
Source File: test_utils.py    From Watson with MIT License 5 votes vote down vote up
def test_sorted_groupby(watson):
    end = arrow.utcnow()
    watson.add('foo', end.shift(hours=-25), end.shift(hours=-24), ['A'])
    watson.add('bar', end.shift(hours=-1), end, ['A'])

    result = list(sorted_groupby(
        watson.frames,
        operator.attrgetter('day'),
        reverse=False))
    assert result[0][0] < result[1][0] 
Example #25
Source File: catalog.py    From ideascube with GNU Affero General Public License v3.0 5 votes vote down vote up
def list_installed(self, ids):
        ids = self._expand_package_ids(ids, self._installed)
        pkgs = []

        for pkg_id in ids:
            try:
                pkgs.append(self._get_package(pkg_id, self._installed))

            except (InvalidPackageType, MissingPackageMetadata, NoSuchPackage):
                continue

        return sorted(pkgs, key=attrgetter('id')) 
Example #26
Source File: test_utils.py    From Watson with MIT License 5 votes vote down vote up
def test_sorted_groupby_reverse(watson):
    end = arrow.utcnow()
    watson.add('foo', end.shift(hours=-25), end.shift(hours=-24), ['A'])
    watson.add('bar', end.shift(hours=-1), end, ['A'])

    result = list(sorted_groupby(
        watson.frames,
        operator.attrgetter('day'),
        reverse=True))
    assert result[0][0] > result[1][0]


# frames_to_csv 
Example #27
Source File: model.py    From pagure with GNU General Public License v2.0 5 votes vote down vote up
def acls_list_pretty(self):
        """
        Return a list containing the description of each ACLs this token has.
        """
        return [
            acl.description
            for acl in sorted(self.acls, key=operator.attrgetter("name"))
        ] 
Example #28
Source File: test_models.py    From ideascube with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_search_Document_is_case_insensitive():
    # The custom function to parse tag string (and so sanitize tag's names)
    # is used is form only, not if we use `tag.set()` or `tag.add()`.
    # DocumentFactory use `tag.add()` to set the tags of the document and so
    # the tag's names are not sanitized.
    doc1 = DocumentFactory(tags=[sanitize_tag_name("aé")])
    doc2 = DocumentFactory(tags=[sanitize_tag_name("AÉ")])
    doc3 = DocumentFactory(tags=[sanitize_tag_name("Bar")])

    assert sorted(Search.search(tags__match=["aé"]), key=attrgetter('id')) \
        == [doc1, doc2]
    assert sorted(Search.search(tags__match=["AÉ"]), key=attrgetter('id')) \
        == [doc1, doc2]
    assert doc3 in Search.search(tags__match=["baR"]) 
Example #29
Source File: opts.py    From ec2-api with Apache License 2.0 5 votes vote down vote up
def list_auth_opts():
    opt_list = ks_loading.register_session_conf_options(CONF, GROUP_AUTHTOKEN)
    opt_list.insert(0, ks_loading.get_auth_common_conf_options()[0])
    # NOTE(mhickey): There are a lot of auth plugins, we just generate
    # the config options for a few common ones
    plugins = ['password', 'v2password', 'v3password']
    for name in plugins:
        for plugin_option in ks_loading.get_auth_plugin_conf_options(name):
            if all(option.name != plugin_option.name for option in opt_list):
                opt_list.append(plugin_option)
    opt_list.sort(key=operator.attrgetter('name'))
    return [(GROUP_AUTHTOKEN, opt_list)] 
Example #30
Source File: member.py    From discord.py with MIT License 5 votes vote down vote up
def flatten_user(cls):
    for attr, value in itertools.chain(BaseUser.__dict__.items(), User.__dict__.items()):
        # ignore private/special methods
        if attr.startswith('_'):
            continue

        # don't override what we already have
        if attr in cls.__dict__:
            continue

        # if it's a slotted attribute or a property, redirect it
        # slotted members are implemented as member_descriptors in Type.__dict__
        if not hasattr(value, '__annotations__'):
            getter = attrgetter('_user.' + attr)
            setattr(cls, attr, property(getter, doc='Equivalent to :attr:`User.%s`' % attr))
        else:
            # Technically, this can also use attrgetter
            # However I'm not sure how I feel about "functions" returning properties
            # It probably breaks something in Sphinx.
            # probably a member function by now
            def generate_function(x):
                def general(self, *args, **kwargs):
                    return getattr(self._user, x)(*args, **kwargs)

                general.__name__ = x
                return general

            func = generate_function(attr)
            func.__doc__ = value.__doc__
            setattr(cls, attr, func)

    return cls