docs/enterprise/extension_query.py
def Flatten(data, all_columns):
  """Flattens lists inside |data|, one level deep.

  This function will flatten each dictionary key in |data| into a single row
  so that it can be written to a CSV file.

  Args:
    data: the data to be flattened.
    all_columns: set of all columns that are found in the result (this will be
      filled by the function).

  Yields:
    A list of dict objects whose lists or sets have been flattened.
  """
  SEPARATOR = ', '

  # Max length of a cell in Excel is technically 32767 characters but if we get
  # too close to this limit Excel seems to create weird results when we open
  # the CSV file. To protect against this, give a little more buffer to the max
  # characters.
  MAX_CELL_LENGTH = 32700

  for item in data:
    added_item = {}
    for prop, value in item.items():
      # Non-container properties can be added directly.
      if not isinstance(value, (list, set)):
        added_item[prop] = value
        continue

      # Otherwise join the container together into a single cell.
      num_prop = 'num_' + prop
      added_item[num_prop] = len(value)

      # For long lists, the cell contents may go over MAX_CELL_LENGTH, so
      # split the list into chunks that will fit into MAX_CELL_LENGTH.
      flat_list = SEPARATOR.join(sorted(value))
      overflow_prop_index = 0
      while True:
        current_column = prop
        if overflow_prop_index:
          current_column = prop + '_' + str(overflow_prop_index)

        flat_list_len = len(flat_list)
        if flat_list_len > MAX_CELL_LENGTH:
          last_separator = flat_list.rfind(SEPARATOR, 0,
                                           MAX_CELL_LENGTH - flat_list_len)
          if last_separator != -1:
            added_item[current_column] = flat_list[0:last_separator]
            flat_list = flat_list[last_separator + 2:]
            overflow_prop_index = overflow_prop_index + 1
            continue

        # Fall-through case where no more splitting is possible, this is the
        # lass cell to add for this list.
        added_item[current_column] = flat_list
        break

      assert isinstance(added_item[prop],
                        (int, bool, str)), ('unexpected type for item: %s' %
                                            type(added_item[prop]).__name__)

    all_columns.update(added_item.keys())
    yield added_item
Flattens lists inside |data|, one level deep.

  This function will flatten each dictionary key in |data| into a single row
  so that it can be written to a CSV file.

  Args:
    data: the data to be flattened.
    all_columns: set of all columns that are found in the result (this will be
      filled by the function).

  Yields:
    A list of dict objects whose lists or sets have been flattened.
  
Flattens lists inside |data|, one level deep.
This function will flatten each dictionary key in |data| into a single row
so that it can be written to a CSV file.
Flattens lists inside |data| one level deep . This function will flatten each dictionary key in |data| into a single row so that it can be written to a CSV file .
def Flatten(data, all_columns):
  SEPARATOR = ', '
  MAX_CELL_LENGTH = 32700
  for item in data:
    added_item = {}
    for prop, value in item.items():
      if not isinstance(value, (list, set)):
        added_item[prop] = value
        continue
      num_prop = 'num_' + prop
      added_item[num_prop] = len(value)
      flat_list = SEPARATOR.join(sorted(value))
      overflow_prop_index = 0
      while True:
        current_column = prop
        if overflow_prop_index:
          current_column = prop + '_' + str(overflow_prop_index)
        flat_list_len = len(flat_list)
        if flat_list_len > MAX_CELL_LENGTH:
          last_separator = flat_list.rfind(SEPARATOR, 0,
                                           MAX_CELL_LENGTH - flat_list_len)
          if last_separator != -1:
            added_item[current_column] = flat_list[0:last_separator]
            flat_list = flat_list[last_separator + 2:]
            overflow_prop_index = overflow_prop_index + 1
            continue
        added_item[current_column] = flat_list
        break
      assert isinstance(added_item[prop],
                        (int, bool, str)), ('unexpected type for item: %s' %
                                            type(added_item[prop]).__name__)
    all_columns.update(added_item.keys())
    yield added_item
def Flatten ( data , all_columns ) : SEPARATOR = ', ' MAX_CELL_LENGTH = 32700 for item in data : added_item = { } for prop , value in item . items ( ) : if not isinstance ( value , ( list , set ) ) : added_item [ prop ] = value continue num_prop = 'num_' + prop added_item [ num_prop ] = len ( value ) flat_list = SEPARATOR . join ( sorted ( value ) ) overflow_prop_index = 0 while True : current_column = prop if overflow_prop_index : current_column = prop + '_' + str ( overflow_prop_index ) flat_list_len = len ( flat_list ) if flat_list_len > MAX_CELL_LENGTH : last_separator = flat_list . rfind ( SEPARATOR , 0 , MAX_CELL_LENGTH - flat_list_len ) if last_separator != - 1 : added_item [ current_column ] = flat_list [ 0 : last_separator ] flat_list = flat_list [ last_separator + 2 : ] overflow_prop_index = overflow_prop_index + 1 continue added_item [ current_column ] = flat_list break assert isinstance ( added_item [ prop ] , ( int , bool , str ) ) , ( 'unexpected type for item: %s' % type ( added_item [ prop ] ) . __name__ ) all_columns . update ( added_item . keys ( ) ) yield added_item
Flattens lists inside |data|, one level deep.
Flattens lists inside |data| one level deep .
"""Flattens lists inside |data|, one level deep.

  This function will flatten each dictionary key in |data| into a single row
  so that it can be written to a CSV file.

  Args:
    data: the data to be flattened.
    all_columns: set of all columns that are found in the result (this will be
      filled by the function).

  Yields:
    A list of dict objects whose lists or sets have been flattened.
  """ # Max length of a cell in Excel is technically 32767 characters but if we get # too close to this limit Excel seems to create weird results when we open # the CSV file. To protect against this, give a little more buffer to the max # characters. # Non-container properties can be added directly. # Otherwise join the container together into a single cell. # For long lists, the cell contents may go over MAX_CELL_LENGTH, so # split the list into chunks that will fit into MAX_CELL_LENGTH. # Fall-through case where no more splitting is possible, this is the # lass cell to add for this list.
A list of dict objects whose lists or sets have been flattened.
A list of dict objects whose lists or sets have been flattened .
data
the data to be flattened.
the data to be flattened .
all_columns
set of all columns that are found in the result (this will be
filled by the function).
set of all columns that are found in the result ( this will be filled by the function ) .