Exemple #1
0
    def test_cut_list_to_limit_list_length_not_mutate_list_when_not_too_long(
            self, target_list):
        actual_output = optimization_util.cut_list_to_limit_list_length(
            target_list, MAX_LIST_LENGTH)

        self.assertEqual(target_list, actual_output)
        self.assertLessEqual(len(actual_output), MAX_LIST_LENGTH)
Exemple #2
0
    def _optimize(self, product_batch: Dict[str, Any], language: str,
                  country: str, currency: str) -> int:
        """Runs the optimization.

    Args:
      product_batch: A batch of product data.
      language: The language to use for this optimizer.
      country: The country to use for this optimizer.
      currency: The currency to use for this optimizer.

    Returns:
      The number of products affected by this optimization.
    """
        num_of_products_optimized = 0
        for entry in product_batch['entries']:
            product = entry['product']
            if 'productTypes' in product:
                product_types = product.get('productTypes', [])

                optimized_product_types = (
                    optimization_util.cut_list_to_limit_list_length(
                        product_types, _MAX_LIST_LENGTH))

                if optimized_product_types != product_types:
                    product['productTypes'] = optimized_product_types
                    logging.info(
                        'Modified item %s: Removing the last items from productTypes: %s',
                        product.get('offerId', ''), product_types)
                    num_of_products_optimized += 1
                    base_optimizer.set_optimization_tracking(
                        product, base_optimizer.SANITIZED)

        return num_of_products_optimized
Exemple #3
0
    def test_cut_list_to_limit_list_length_shorten_list_when_too_long(self):
        target_list = list(range(MAX_LIST_LENGTH + 1))

        actual_output = optimization_util.cut_list_to_limit_list_length(
            target_list, MAX_LIST_LENGTH)

        expected_output = list(range(MAX_LIST_LENGTH))
        self.assertEqual(expected_output, actual_output)
        self.assertLen(actual_output, MAX_LIST_LENGTH)
Exemple #4
0
    def _optimize(self, product_batch: Dict[str, Any], language: str,
                  country: str, currency: str) -> int:
        """Runs the optimization.

    Fixes invalid color fields.
    See above for the definition of an invalid color field.

    Args:
      product_batch: A batch of product data.
      language: The language to use for this optimizer.
      country: The country to use for this optimizer.
      currency: The currency to use for this optimizer.

    Returns:
      The number of products affected by this optimization: int
    """
        num_of_products_optimized = 0

        for entry in product_batch['entries']:
            product = entry['product']
            if 'color' in product:
                original_colors = product.get('color', '')

                colors = original_colors.split(_SEPARATOR)

                optimized_colors = optimization_util.cut_list_elements_over_max_length(
                    colors, constants.MAX_COLOR_STR_LENGTH_FOR_EACH)

                optimized_colors = optimization_util.cut_list_to_limit_list_length(
                    optimized_colors, constants.MAX_COLOR_COUNT)

                optimized_colors = optimization_util.cut_list_to_limit_concatenated_str_length(
                    optimized_colors, _SEPARATOR,
                    constants.MAX_COLOR_STR_LENGTH_IN_TOTAL)

                optimized_colors = _SEPARATOR.join(optimized_colors)

                if original_colors != optimized_colors:
                    product['color'] = optimized_colors
                    num_of_products_optimized += 1
                    base_optimizer.set_optimization_tracking(
                        product, base_optimizer.SANITIZED)

        return num_of_products_optimized
Exemple #5
0
def _clean_up_term_list(term_list: List[str], max_count: int) -> List[str]:
  """Cleans up a list of terms.

  This function removes duplicate elements and returns a list of title-formed
  terms. It also removes colors that would cause the length limit exceed.

  Args:
    term_list: A list of terms.
    max_count: The maximum number of terms returned.

  Returns:
    A list of title-formed terms.
  """
  clean_list = list(dict.fromkeys(term_list))
  clean_list = optimization_util.cut_list_elements_over_max_length(
      clean_list, constants.MAX_COLOR_STR_LENGTH_FOR_EACH)
  clean_list = optimization_util.cut_list_to_limit_concatenated_str_length(
      clean_list, '/', constants.MAX_COLOR_STR_LENGTH_IN_TOTAL)
  clean_list = [term.title() for term in clean_list]
  return optimization_util.cut_list_to_limit_list_length(clean_list, max_count)