我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用django.utils.six.moves.zip_longest()。
def determine_boosts_weights(boosts=()): if not boosts: boosts = get_boosts() boosts = list(sorted(boosts, reverse=True)) min_boost = boosts[-1] if len(boosts) <= WEIGHTS_COUNT: return list(zip_longest(boosts, WEIGHTS, fillvalue=min(min_boost, 0))) max_boost = boosts[0] boost_step = (max_boost - min_boost) / (WEIGHTS_COUNT - 1) return [(max_boost - (i * boost_step), weight) for i, weight in enumerate(WEIGHTS)]
def create_products_by_class(product_class, schema, placeholder_dir, how_many=10, create_images=True, stdout=None): category_name = schema.get('category') or DEFAULT_CATEGORY category = get_or_create_category(category_name) for dummy in range(how_many): product = create_product(product_class=product_class) set_product_attributes(product, product_class) product.categories.add(category) if create_images: class_placeholders = os.path.join( placeholder_dir, schema['images_dir']) create_product_images( product, random.randrange(1, 5), class_placeholders) variant_combinations = get_variant_combinations(product) prices = get_price_override( schema, len(variant_combinations), product.price) variants_with_prices = moves.zip_longest( variant_combinations, prices) for i, variant_price in enumerate(variants_with_prices, start=1337): attr_combination, price = variant_price sku = '%s-%s' % (product.pk, i) create_variant( product, attributes=attr_combination, sku=sku, price_override=price) if not variant_combinations: # Create min one variant for products without variant level attrs sku = '%s-%s' % (product.pk, fake.random_int(1000, 100000)) create_variant(product, sku=sku) if stdout is not None: stdout.write('Product: %s (%s), %s variant(s)' % ( product, product_class.name, len(variant_combinations) or 1))
def resolve_columns(self, row, fields=()): values = [] index_extra_select = len(self.query.extra_select) bool_fields = ("BooleanField", "NullBooleanField") for value, field in zip_longest(row[index_extra_select:], fields): if (field and field.get_internal_type() in bool_fields and value in (0, 1)): value = bool(value) values.append(value) return row[:index_extra_select] + tuple(values)
def resolve_columns(self, row, fields=()): """ This routine is necessary so that distances and geometries returned from extra selection SQL get resolved appropriately into Python objects. """ values = [] aliases = list(self.query.extra_select) # Have to set a starting row number offset that is used for # determining the correct starting row index -- needed for # doing pagination with Oracle. rn_offset = 0 if self.connection.ops.oracle: if self.query.high_mark is not None or self.query.low_mark: rn_offset = 1 index_start = rn_offset + len(aliases) # Converting any extra selection values (e.g., geometries and # distance objects added by GeoQuerySet methods). values = [self.query.convert_values(v, self.query.extra_select_fields.get(a, None), self.connection) for v, a in zip(row[rn_offset:index_start], aliases)] if self.connection.ops.oracle or getattr(self.query, 'geo_values', False): # We resolve the rest of the columns if we're on Oracle or if # the `geo_values` attribute is defined. for value, field in zip_longest(row[index_start:], fields): values.append(self.query.convert_values(value, field, self.connection)) else: values.extend(row[index_start:]) return tuple(values) #### Routines unique to GeoQuery ####