我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用django.utils.six.iterkeys()。
def get_subclass_fields(self, request, obj=None): # Find out how many fields would really be on the form, # if it weren't restricted by declared fields. exclude = list(self.exclude or []) exclude.extend(self.get_readonly_fields(request, obj)) # By not declaring the fields/form in the base class, # get_form() will populate the form with all available fields. form = self.get_form(request, obj, exclude=exclude) subclass_fields = list(six.iterkeys(form.base_fields)) + list(self.get_readonly_fields(request, obj)) # Find which fields are not part of the common fields. for fieldset in self.base_fieldsets: for field in fieldset[1]['fields']: try: subclass_fields.remove(field) except ValueError: pass # field not found in form, Django will raise exception later. return subclass_fields
def evaluate(self, expression, invert=False): """ Evaluates expression for each of the retrieved values from the last call to get_values(). :param expression: A comparison expression to evaluate against the collected data. Example: '>20%'. :type expression: basestring :param invert: Invert the expression logic if True. :returns: A list of (metric, current_value) tuples for metrics whose last retrieved current value matches the expression. """ matcher = self._get_matcher(expression) result = [(metric, self.result[metric]['value']) for metric in iterkeys(self.result) if bool(matcher(metric)) ^ bool(invert)] return result
def get_tax_for_country(country_code, rate_name=None): try: country_vat = VAT.objects.get(country_code=country_code) reduced_rates = country_vat.data['reduced_rates'] standard_rate = country_vat.data['standard_rate'] except (KeyError, ObjectDoesNotExist): return None rate = standard_rate if rate_name and reduced_rates and rate_name in six.iterkeys(reduced_rates): rate = reduced_rates[rate_name] tax_name = '%s - %s' % (country_code, rate_name) return LinearTax(rate/100, tax_name)
def test_iterkeys(self): self.session['x'] = 1 self.session.modified = False self.session.accessed = False i = six.iterkeys(self.session) self.assertTrue(hasattr(i, '__iter__')) self.assertTrue(self.session.accessed) self.assertFalse(self.session.modified) self.assertEqual(list(i), ['x'])
def test_iterkeys(self): self.context['x'] = 1 self.context.modified = False self.context.accessed = False i = six.iterkeys(self.context) self.assertTrue(hasattr(i, '__iter__')) self.assertTrue(self.context.accessed) self.assertFalse(self.context.modified) self.assertEqual(list(i), ['x'])
def get_models(self): """ Get all models in the registry """ return set(iterkeys(self._models))
def get_indices(self, models=None): """ Get all indices in the registry or the indices for a list of models """ if models is not None: return set( indice for indice, docs in iteritems(self._indices) for doc in docs if doc._doc_type.model in models ) return set(iterkeys(self._indices))
def get_cached_multiple_link_load(items, time_interval): """Cached version of get_multiple_link_load()""" item_map = {k: _cache_key(k, time_interval) for k in iterkeys(items)} # cache lookup cached = cache.get_many(item_map.values()) _logger.debug( "get_cached_multiple_link_load: got %d/%d values from cache (%r)", len(cached), len(items), time_interval) # retrieve data for cache misses misses = {k: v for k, v in iteritems(items) if item_map[k] not in cached} if misses: get_multiple_link_load(misses, time_interval) # set data from cache reverse_item_map = {v: k for k, v in iteritems(item_map)} for cache_key, value in iteritems(cached): key = reverse_item_map[cache_key] properties = items[key] properties['load_in'], properties['load_out'] = value # add new data to cache missed_data = {item_map[key]: (properties['load_in'], properties['load_out']) for key, properties in iteritems(misses)} _logger.debug("get_cached_multiple_link_load: caching %d values", len(missed_data)) cache.set_many(missed_data, CACHE_TIMEOUT)
def get_cached_multiple_cpu_load(items, time_interval): """Cached version of get_multiple_link_load()""" item_map = {k: _cache_key(k, time_interval) for k in iterkeys(items)} # cache lookup cached = cache.get_many(item_map.values()) _logger.debug( "get_cached_multiple_cpu_load: got %d/%d values from cache (%r)", len(cached), len(items), time_interval) # retrieve data for cache misses misses = {k: v for k, v in iteritems(items) if item_map[k] not in cached} if misses: get_multiple_cpu_load(misses, time_interval) # set data from cache reverse_item_map = {v: k for k, v in iteritems(item_map)} for cache_key, value in iteritems(cached): key = reverse_item_map[cache_key] properties = items[key] properties['load'] = value # add new data to cache missed_data = {item_map[key]: properties['load'] for key, properties in iteritems(misses)} _logger.debug("get_cached_multiple_cpu_load: caching %d values", len(missed_data)) cache.set_many(missed_data, CACHE_TIMEOUT)
def _to_xml(self, xml, data, tag_name=None): if tag_name: xml.startElement(tag_name, {}) if hasattr(data, 'write_xml'): # Support for the `IWriteXML` protocol. data.write_xml(xml) elif data is True: xml.characters('true') elif data is False: xml.characters('false') elif isinstance(data, (list, tuple)): for item in data: self._to_xml(xml, item, tag_name=(getattr(data, "xml_tag", None) or self.item_tag_name)) elif isinstance(data, dict): key_order = getattr(data, "key_order", ()) for key in sorted(six.iterkeys(data), key=order_by_sort_order(key_order)): self._to_xml(xml, data[key], key) elif data is None: # Don't output any value pass else: xml.characters(smart_text(data)) if tag_name: xml.endElement(tag_name)