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: selection.py From pyshgp with MIT License | 6 votes |
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 #2
Source File: cloudpickle.py From pywren-ibm-cloud with Apache License 2.0 | 6 votes |
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 #3
Source File: sqlalchemy.py From gnocchi with Apache License 2.0 | 6 votes |
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 #4
Source File: __init__.py From custodia with GNU General Public License v3.0 | 6 votes |
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 #5
Source File: test_transaction.py From monero-python with BSD 3-Clause "New" or "Revised" License | 6 votes |
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: file_info.py From baseband with GNU General Public License v3.0 | 6 votes |
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 #7
Source File: util.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
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 #8
Source File: util.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
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 #9
Source File: compiler.py From asn1tools with MIT License | 6 votes |
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 #10
Source File: lint.py From linter-pylama with MIT License | 6 votes |
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 #11
Source File: test_construction.py From recruit with Apache License 2.0 | 6 votes |
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 #12
Source File: wifi.py From ideascube with GNU Affero General Public License v3.0 | 6 votes |
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 #13
Source File: wifi.py From ideascube with GNU Affero General Public License v3.0 | 6 votes |
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 #14
Source File: fields.py From jbox with MIT License | 6 votes |
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 #15
Source File: conftest.py From mutatest with MIT License | 5 votes |
def sorted_binop_expected_locs(binop_expected_locs): """Sorted expected locs when used in tests for sample generation.""" sort_by = attrgetter("lineno", "col_offset", "end_lineno", "end_col_offset") return sorted(binop_expected_locs, key=sort_by)
Example #16
Source File: _cprequest.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def run_hooks(cls, hooks): """Execute the indicated hooks, trapping errors. Hooks with ``.failsafe == True`` are guaranteed to run even if others at the same hookpoint fail. In this case, log the failure and proceed on to the next hook. The only way to stop all processing from one of these hooks is to raise a BaseException like SystemExit or KeyboardInterrupt and stop the whole server. """ assert isinstance(hooks, collections.abc.Iterator) quiet_errors = ( cherrypy.HTTPError, cherrypy.HTTPRedirect, cherrypy.InternalRedirect, ) safe = filter(operator.attrgetter('failsafe'), hooks) for hook in hooks: try: hook() except quiet_errors: cls.run_hooks(safe) raise except Exception: cherrypy.log(traceback=True, severity=40) cls.run_hooks(safe) raise
Example #17
Source File: test_tools.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_priorities(self): """ Hooks should sort by priority order. """ Hook = cherrypy._cprequest.Hook hooks = [ Hook(None, priority=48), Hook(None), Hook(None, priority=49), ] hooks.sort() by_priority = operator.attrgetter('priority') priorities = list(map(by_priority, hooks)) assert priorities == [48, 49, 50]
Example #18
Source File: managers.py From pinax-documents with MIT License | 5 votes |
def members(self, folder, **kwargs): direct = kwargs.get("direct", True) user = kwargs.get("user") Document = apps.get_model("documents", "Document") folders = self.filter(parent=folder) documents = Document.objects.filter(folder=folder) if user: folders = folders.for_user(user) documents = documents.for_user(user) M = sorted(itertools.chain(folders, documents), key=operator.attrgetter("name")) if direct: return M for child in folders: M.extend(self.members(child, **kwargs)) return M
Example #19
Source File: adventure.py From Dumb-Cogs with MIT License | 5 votes |
def parse(data, datafile): """Read the Adventure data file and return a ``Data`` object.""" data._last_travel = [0, [0]] # x and verbs used by section 3 while True: section_number = int(datafile.readline()) if not section_number: # no further sections break store = globals().get('section%d' % section_number) while True: fields = [ (int(field) if field.lstrip('-').isdigit() else field) for field in datafile.readline().strip().split('\t') ] if fields[0] == -1: # end-of-section marker break store(data, *fields) del data._last_travel # state used by section 3 del data._object # state used by section 5 data.object_list = sorted(set(data.objects.values()), key=attrgetter('n')) #data.room_list = sorted(set(data.rooms.values()), key=attrgetter('n')) for obj in data.object_list: name = obj.names[0] if hasattr(data, name): name = name + '2' # create identifiers like ROD2, PLANT2 setattr(data, name, obj) return data #game.py
Example #20
Source File: archive_policy.py From gnocchi with Apache License 2.0 | 5 votes |
def max_block_size(self): # The biggest block size is the coarse grained archive definition return sorted(self.definition, key=operator.attrgetter("granularity"))[-1].granularity
Example #21
Source File: cgi.py From jawfish with MIT License | 5 votes |
def getvalue(self, key, default=None): """Dictionary style get() method, including 'value' lookup.""" if key in self: value = self[key] if type(value) is type([]): return map(attrgetter('value'), value) else: return value.value else: return default
Example #22
Source File: cgi.py From jawfish with MIT License | 5 votes |
def getlist(self, key): """ Return list of received values.""" if key in self: value = self[key] if type(value) is type([]): return map(attrgetter('value'), value) else: return [value.value] else: return []
Example #23
Source File: cgi.py From jawfish with MIT License | 5 votes |
def getvalue(self, key, default=None): """Dictionary style get() method, including 'value' lookup.""" if key in self: value = self[key] if type(value) is type([]): return map(attrgetter('value'), value) else: return value.value else: return default
Example #24
Source File: cgi.py From jawfish with MIT License | 5 votes |
def getlist(self, key): """ Return list of received values.""" if key in self: value = self[key] if type(value) is type([]): return map(attrgetter('value'), value) else: return [value.value] else: return []
Example #25
Source File: inspect.py From jawfish with MIT License | 5 votes |
def walktree(classes, children, parent): """Recursive helper function for getclasstree().""" results = [] classes.sort(key=attrgetter('__module__', '__name__')) for c in classes: results.append((c, c.__bases__)) if c in children: results.append(walktree(children[c], children, c)) return results
Example #26
Source File: block.py From monero-python with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __contains__(self, tx): if isinstance(tx, six.string_types): txid = tx elif isinstance(tx, Transaction): txid = tx.hash else: raise ValueError( "Only Transaction or hash strings may be used to test existence in Blocks, " "got '{:s}'".format(tx)) return txid in map(operator.attrgetter('hash'), self.transactions)
Example #27
Source File: sequencer.py From leaguedirector with Apache License 2.0 | 5 votes |
def selectNextKeyframe(self): selectionSorted = sorted(self.selectedKeyframes(), key=attrgetter('time')) trackSelection = {key.track : key for key in selectionSorted} for track, selected in trackSelection.items(): for child in sorted(track.childItems(), key=attrgetter('time')): if child.time > selected.time: trackSelection[track] = child break self.scene.clearSelection() for item in trackSelection.values(): item.setSelected(True)
Example #28
Source File: sequencer.py From leaguedirector with Apache License 2.0 | 5 votes |
def selectPrevKeyframe(self): selectionSorted = sorted(self.selectedKeyframes(), key=attrgetter('time'), reverse=True) trackSelection = {key.track : key for key in selectionSorted} for track, selected in trackSelection.items(): for child in sorted(track.childItems(), key=attrgetter('time'), reverse=True): if child.time < selected.time: trackSelection[track] = child break self.scene.clearSelection() for item in trackSelection.values(): item.setSelected(True)
Example #29
Source File: refactor.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def get_fixers(self): """Inspects the options to load the requested patterns and handlers. Returns: (pre_order, post_order), where pre_order is the list of fixers that want a pre-order AST traversal, and post_order is the list that want post-order traversal. """ pre_order_fixers = [] post_order_fixers = [] for fix_mod_path in self.fixers: mod = __import__(fix_mod_path, {}, {}, ["*"]) fix_name = fix_mod_path.rsplit(".", 1)[-1] if fix_name.startswith(self.FILE_PREFIX): fix_name = fix_name[len(self.FILE_PREFIX):] parts = fix_name.split("_") class_name = self.CLASS_PREFIX + "".join([p.title() for p in parts]) try: fix_class = getattr(mod, class_name) except AttributeError: raise FixerError("Can't find %s.%s" % (fix_name, class_name)) from None fixer = fix_class(self.options, self.fixer_log) if fixer.explicit and self.explicit is not True and \ fix_mod_path not in self.explicit: self.log_message("Skipping optional fixer: %s", fix_name) continue self.log_debug("Adding transformation: %s", fix_name) if fixer.order == "pre": pre_order_fixers.append(fixer) elif fixer.order == "post": post_order_fixers.append(fixer) else: raise FixerError("Illegal fixer order: %r" % fixer.order) key_func = operator.attrgetter("run_order") pre_order_fixers.sort(key=key_func) post_order_fixers.sort(key=key_func) return (pre_order_fixers, post_order_fixers)
Example #30
Source File: refactor.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def get_fixers(self): """Inspects the options to load the requested patterns and handlers. Returns: (pre_order, post_order), where pre_order is the list of fixers that want a pre-order AST traversal, and post_order is the list that want post-order traversal. """ pre_order_fixers = [] post_order_fixers = [] for fix_mod_path in self.fixers: mod = __import__(fix_mod_path, {}, {}, ["*"]) fix_name = fix_mod_path.rsplit(".", 1)[-1] if fix_name.startswith(self.FILE_PREFIX): fix_name = fix_name[len(self.FILE_PREFIX):] parts = fix_name.split("_") class_name = self.CLASS_PREFIX + "".join([p.title() for p in parts]) try: fix_class = getattr(mod, class_name) except AttributeError: raise FixerError("Can't find %s.%s" % (fix_name, class_name)) fixer = fix_class(self.options, self.fixer_log) if fixer.explicit and self.explicit is not True and \ fix_mod_path not in self.explicit: self.log_message("Skipping optional fixer: %s", fix_name) continue self.log_debug("Adding transformation: %s", fix_name) if fixer.order == "pre": pre_order_fixers.append(fixer) elif fixer.order == "post": post_order_fixers.append(fixer) else: raise FixerError("Illegal fixer order: %r" % fixer.order) key_func = operator.attrgetter("run_order") pre_order_fixers.sort(key=key_func) post_order_fixers.sort(key=key_func) return (pre_order_fixers, post_order_fixers)