nodes: 0, module; 1, function_definition; 2, function_name:rst_dict_table; 3, parameters; 4, block; 5, identifier:dict_; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, expression_statement; 11, expression_statement; 12, comment:# apply formatting; 13, expression_statement; 14, expression_statement; 15, comment:# work out longest elements in each column; 16, expression_statement; 17, expression_statement; 18, if_statement; 19, comment:# build table header line; 20, expression_statement; 21, function_definition; 22, comment:# build table of lines; 23, expression_statement; 24, if_statement; 25, expression_statement; 26, if_statement; 27, for_statement; 28, expression_statement; 29, return_statement; 30, identifier:key_format; 31, identifier:str; 32, identifier:val_format; 33, identifier:str; 34, identifier:header; 35, None; 36, identifier:sort; 37, True; 38, comment:"""Returns an RST-formatted table of keys and values from a `dict`
  
      Parameters
      ----------
      dict_ : dict
          data to display in table
      key_format : callable
          callable function with which to format keys
      val_format : callable
          callable function with which to format values
      header : None, tuple of str
          a 2-tuple of header for the two columns, or `None` to exclude
          a header line (default)
      sort : bool, optional
          Sort the dictionary keys alphabetically when writing the table.
  
      Examples
      --------
      >>> a = {'key1': 'value1', 'key2': 'value2'}
      >>> print(rst_dict_table(a))
      ====  ======
      key1  value1
      key2  value2
      ====  ======
      >>> print(rst_dict_table(a, key_format='``{}``'.format,
      ...                      val_format=':class:`{}`'.format,
      ...                      header=('Key', 'Value'))
      ========  ===============
      Key       Value
      ========  ===============
      ``key1``  :class:`value1`
      ``key2``  :class:`value2`
      ========  ===============
      """; 39, assignment; 40, assignment; 41, assignment; 42, assignment; 43, assignment; 44, identifier:header; 45, block; 46, assignment; 47, function_name:row; 48, parameters; 49, block; 50, assignment; 51, identifier:header; 52, block; 53, assignment; 54, identifier:sort; 55, block; 56, pattern_list; 57, identifier:params; 58, block; 59, call; 60, call; 61, pattern_list; 62, call; 63, identifier:keys; 64, call; 65, identifier:values; 66, call; 67, identifier:nckey; 68, call; 69, identifier:ncval; 70, call; 71, expression_statement; 72, expression_statement; 73, expression_statement; 74, identifier:divider; 75, call; 76, identifier:key; 77, identifier:val; 78, expression_statement; 79, return_statement; 80, identifier:lines; 81, list; 82, expression_statement; 83, identifier:params; 84, call; 85, expression_statement; 86, identifier:key; 87, identifier:val; 88, expression_statement; 89, expression_statement; 90, attribute; 91, argument_list; 92, attribute; 93, argument_list; 94, identifier:keys; 95, identifier:values; 96, identifier:zip; 97, argument_list; 98, identifier:map; 99, argument_list; 100, identifier:map; 101, argument_list; 102, identifier:max; 103, argument_list; 104, identifier:max; 105, argument_list; 106, assignment; 107, assignment; 108, assignment; 109, attribute; 110, argument_list; 111, assignment; 112, call; 113, identifier:divider; 114, call; 115, identifier:zip; 116, argument_list; 117, assignment; 118, assignment; 119, call; 120, identifier:lines; 121, identifier:append; 122, identifier:divider; 123, string; 124, identifier:join; 125, identifier:lines; 126, list_splat; 127, identifier:key_format; 128, identifier:keys; 129, identifier:val_format; 130, identifier:values; 131, call; 132, call; 133, pattern_list; 134, identifier:header; 135, identifier:nckey; 136, call; 137, identifier:ncval; 138, call; 139, string:"{}  {}"; 140, identifier:format; 141, binary_operator:'='*nckey; 142, binary_operator:'='*ncval; 143, identifier:fmt; 144, call; 145, attribute; 146, argument_list; 147, attribute; 148, argument_list; 149, identifier:keys; 150, identifier:values; 151, identifier:params; 152, call; 153, identifier:fmt; 154, call; 155, attribute; 156, argument_list; 157, string_content; 158, call; 159, identifier:map; 160, argument_list; 161, identifier:map; 162, argument_list; 163, identifier:khead; 164, identifier:vhead; 165, identifier:max; 166, argument_list; 167, identifier:max; 168, argument_list; 169, string; 170, identifier:nckey; 171, string; 172, identifier:ncval; 173, attribute; 174, argument_list; 175, identifier:fmt; 176, identifier:format; 177, identifier:key; 178, identifier:val; 179, identifier:lines; 180, identifier:extend; 181, tuple; 182, identifier:sorted; 183, argument_list; 184, attribute; 185, argument_list; 186, identifier:lines; 187, identifier:append; 188, call; 189, escape_sequence:\n; 190, attribute; 191, argument_list; 192, identifier:len; 193, identifier:keys; 194, identifier:len; 195, identifier:values; 196, identifier:nckey; 197, call; 198, identifier:ncval; 199, call; 200, string_content:=; 201, string_content:=; 202, string; 203, identifier:format; 204, identifier:nckey; 205, identifier:ncval; 206, call; 207, identifier:divider; 208, identifier:params; 209, string; 210, identifier:format; 211, identifier:nckey; 212, identifier:ncval; 213, attribute; 214, argument_list; 215, identifier:dict_; 216, identifier:items; 217, identifier:len; 218, argument_list; 219, identifier:len; 220, argument_list; 221, string_content:{{0:{0}s}}  {{1}}; 222, identifier:row; 223, argument_list; 224, string_content:{{0:{0}s}}  {{1}}; 225, identifier:fmt; 226, identifier:format; 227, identifier:key; 228, identifier:val; 229, identifier:khead; 230, identifier:vhead; 231, list_splat; 232, identifier:header
code: def rst_dict_table(dict_, key_format=str, val_format=str, header=None,
                     sort=True):
      """Returns an RST-formatted table of keys and values from a `dict`
  
      Parameters
      ----------
      dict_ : dict
          data to display in table
      key_format : callable
          callable function with which to format keys
      val_format : callable
          callable function with which to format values
      header : None, tuple of str
          a 2-tuple of header for the two columns, or `None` to exclude
          a header line (default)
      sort : bool, optional
          Sort the dictionary keys alphabetically when writing the table.
  
      Examples
      --------
      >>> a = {'key1': 'value1', 'key2': 'value2'}
      >>> print(rst_dict_table(a))
      ====  ======
      key1  value1
      key2  value2
      ====  ======
      >>> print(rst_dict_table(a, key_format='``{}``'.format,
      ...                      val_format=':class:`{}`'.format,
      ...                      header=('Key', 'Value'))
      ========  ===============
      Key       Value
      ========  ===============
      ``key1``  :class:`value1`
      ``key2``  :class:`value2`
      ========  ===============
      """
      keys, values = zip(*dict_.items())
  
      # apply formatting
      keys = map(key_format, keys)
      values = map(val_format, values)
  
      # work out longest elements in each column
      nckey = max(map(len, keys))
      ncval = max(map(len, values))
      if header:
          khead, vhead = header
          nckey = max(nckey, len(khead))
          ncval = max(ncval, len(vhead))
  
      # build table header line
      divider = "{}  {}".format('='*nckey, '='*ncval)
  
      def row(key, val):
          fmt = '{{0:{0}s}}  {{1}}'.format(nckey, ncval)
          return fmt.format(key, val)
  
      # build table of lines
      lines = [divider]
      if header:
          lines.extend((row(*header), divider))
      params = zip(keys, values)
      if sort:
          params = sorted(params)
      for key, val in params:
          fmt = '{{0:{0}s}}  {{1}}'.format(nckey, ncval)
          lines.append(fmt.format(key, val))
      lines.append(divider)
  
      return '\n'.join(lines)