nodes: 0, module; 1, function_definition; 2, function_name:sort_by_size; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, comment:# sort groups by number of elements; 11, expression_statement; 12, comment:# if group-limit is provided, combine remaining groups; 13, if_statement; 14, comment:# remove others group regardless of limit if requested; 15, if_statement; 16, identifier:group_limit; 17, None; 18, identifier:discard_others; 19, False; 20, identifier:others_label; 21, string; 22, comment:"""
          Sort the groups by the number of elements they contain, descending.
  
          Also has option to limit the number of groups. If this option is
          chosen, the remaining elements are placed into another group with the
          name specified with others_label. if discard_others is True, the others
          group is removed instead.
          """; 23, assignment; 24, comparison_operator:group_limit is not None; 25, comment:# now group together all groups that did not make the limit; 26, block; 27, boolean_operator; 28, block; 29, string_content:others; 30, attribute; 31, call; 32, identifier:group_limit; 33, None; 34, if_statement; 35, comment:# only go to second last (-1), since the 'others' group is now last; 36, for_statement; 37, comment:# remove if empty; 38, if_statement; 39, identifier:discard_others; 40, comparison_operator:others_label in self.groups; 41, delete_statement; 42, identifier:self; 43, identifier:groups; 44, identifier:OrderedDict; 45, argument_list; 46, not_operator; 47, block; 48, else_clause; 49, identifier:g; 50, identifier:group_keys; 51, block; 52, parenthesized_expression; 53, block; 54, identifier:others_label; 55, attribute; 56, subscript; 57, call; 58, identifier:discard_others; 59, expression_statement; 60, expression_statement; 61, block; 62, if_statement; 63, delete_statement; 64, boolean_operator; 65, delete_statement; 66, identifier:self; 67, identifier:groups; 68, attribute; 69, identifier:others_label; 70, identifier:sorted; 71, argument_list; 72, assignment; 73, call; 74, expression_statement; 75, not_operator; 76, block; 77, subscript; 78, comparison_operator:others_label in self.groups; 79, comparison_operator:len(self.groups[others_label]) == 0; 80, subscript; 81, identifier:self; 82, identifier:groups; 83, call; 84, keyword_argument; 85, keyword_argument; 86, identifier:group_keys; 87, subscript; 88, attribute; 89, argument_list; 90, assignment; 91, identifier:discard_others; 92, expression_statement; 93, attribute; 94, identifier:g; 95, identifier:others_label; 96, attribute; 97, call; 98, integer:0; 99, attribute; 100, identifier:others_label; 101, attribute; 102, argument_list; 103, identifier:key; 104, lambda; 105, identifier:reverse; 106, True; 107, call; 108, slice; 109, attribute; 110, identifier:setdefault; 111, identifier:others_label; 112, call; 113, identifier:group_keys; 114, subscript; 115, call; 116, identifier:self; 117, identifier:groups; 118, identifier:self; 119, identifier:groups; 120, identifier:len; 121, argument_list; 122, identifier:self; 123, identifier:groups; 124, identifier:six; 125, identifier:iteritems; 126, attribute; 127, lambda_parameters; 128, call; 129, attribute; 130, argument_list; 131, binary_operator:group_limit - 1; 132, identifier:self; 133, identifier:groups; 134, identifier:list; 135, argument_list; 136, call; 137, slice; 138, attribute; 139, argument_list; 140, subscript; 141, identifier:self; 142, identifier:groups; 143, identifier:x; 144, identifier:len; 145, argument_list; 146, attribute; 147, identifier:keys; 148, identifier:group_limit; 149, integer:1; 150, attribute; 151, argument_list; 152, identifier:group_limit; 153, subscript; 154, identifier:extend; 155, subscript; 156, attribute; 157, identifier:others_label; 158, subscript; 159, identifier:self; 160, identifier:groups; 161, attribute; 162, identifier:keys; 163, attribute; 164, identifier:others_label; 165, attribute; 166, identifier:g; 167, identifier:self; 168, identifier:groups; 169, identifier:x; 170, integer:1; 171, identifier:self; 172, identifier:groups; 173, identifier:self; 174, identifier:groups; 175, identifier:self; 176, identifier:groups
code: def sort_by_size(self, group_limit=None, discard_others=False,
                       others_label='others'):
          """
          Sort the groups by the number of elements they contain, descending.
  
          Also has option to limit the number of groups. If this option is
          chosen, the remaining elements are placed into another group with the
          name specified with others_label. if discard_others is True, the others
          group is removed instead.
          """
          # sort groups by number of elements
          self.groups = OrderedDict(sorted(six.iteritems(self.groups),
                                           key=lambda x: len(x[1]),
                                           reverse=True))
  
          # if group-limit is provided, combine remaining groups
          if group_limit is not None:
  
              # now group together all groups that did not make the limit
              if not discard_others:
                  group_keys = self.groups.keys()[group_limit - 1:]
                  self.groups.setdefault(others_label, list())
              else:
                  group_keys = self.groups.keys()[group_limit:]
  
              # only go to second last (-1), since the 'others' group is now last
              for g in group_keys:
                  if not discard_others:
                      self.groups[others_label].extend(self.groups[g])
                  del self.groups[g]
  
              # remove if empty
              if (others_label in self.groups and
                      len(self.groups[others_label]) == 0):
                  del self.groups[others_label]
  
          # remove others group regardless of limit if requested
          if discard_others and others_label in self.groups:
              del self.groups[others_label]