code: def pad_sequences(
      sequences,
      maxlen=None,
      dtype="int32",
      padding="pre",
      truncating="pre",
      value=0.0,
  ):
      """Pads sequences to the same length.
  
      This function transforms a list of
      `num_samples` sequences (lists of integers)
      into a 2D Numpy array of shape `(num_samples, num_timesteps)`.
      `num_timesteps` is either the `maxlen` argument if provided,
      or the length of the longest sequence otherwise.
  
      Sequences that are shorter than `num_timesteps`
      are padded with `value` at the beginning or the end
      if padding='post.
  
      Sequences longer than `num_timesteps` are truncated
      so that they fit the desired length.
      The position where padding or truncation happens is determined by
      the arguments `padding` and `truncating`, respectively.
  
      Pre-padding is the default.
  
      # Arguments
          sequences: List of lists, where each element is a sequence.
          maxlen: Int, maximum length of all sequences.
          dtype: Type of the output sequences.
              To pad sequences with variable length strings, you can use `object`.
          padding: String, 'pre' or 'post':
              pad either before or after each sequence.
          truncating: String, 'pre' or 'post':
              remove values from sequences larger than
              `maxlen`, either at the beginning or at the end of the sequences.
          value: Float or String, padding value.
  
      # Returns
          x: Numpy array with shape `(len(sequences), maxlen)`
  
      # Raises
          ValueError: In case of invalid values for `truncating` or `padding`,
              or in case of invalid shape for a `sequences` entry.
      """
      if not hasattr(sequences, "__len__"):
          raise ValueError("`sequences` must be iterable.")
      num_samples = len(sequences)
  
      lengths = []
      sample_shape = ()
      flag = True
  
      # take the sample shape from the first non empty sequence
      # checking for consistency in the main loop below.
  
      for x in sequences:
          try:
              lengths.append(len(x))
              if flag and len(x):
                  sample_shape = np.asarray(x).shape[1:]
                  flag = False
          except TypeError:
              raise ValueError(
                  "`sequences` must be a list of iterables. "
                  "Found non-iterable: " + str(x)
              )
  
      if maxlen is None:
          maxlen = np.max(lengths)
  
      is_dtype_str = np.issubdtype(dtype, np.str_) or np.issubdtype(
          dtype, np.unicode_
      )
      if (
          isinstance(value, str)
          and dtype != object
          and not is_dtype_str
      ):
          raise ValueError(
              "`dtype` {} is not compatible with `value`'s type: {}\n"
              "You should set `dtype=object` for variable length strings.".format(
                  dtype, type(value)
              )
          )
  
      x = np.full(
          (num_samples, maxlen) + sample_shape, value, dtype=dtype
      )
      for idx, s in enumerate(sequences):
          if not len(s):
              continue  # empty list/array was found
          if truncating == "pre":
              trunc = s[-maxlen:]
          elif truncating == "post":
              trunc = s[:maxlen]
          else:
              raise ValueError(
                  'Truncating type "%s" ' "not understood" % truncating
              )
  
          # check `trunc` has expected shape
          trunc = np.asarray(trunc, dtype=dtype)
          if trunc.shape[1:] != sample_shape:
              raise ValueError(
                  "Shape of sample %s of sequence at position %s "
                  "is different from expected shape %s"
                  % (trunc.shape[1:], idx, sample_shape)
              )
  
          if padding == "post":
              x[idx, : len(trunc)] = trunc
          elif padding == "pre":
              x[idx, -len(trunc) :] = trunc
          else:
              raise ValueError(
                  'Padding type "%s" not understood' % padding
              )
      return x
  
file_path: keras-preprocessing/keras_preprocessing/sequence.py
imports: import json
  import random
  import numpy
label_desc: Use image node_id 1 for calling a global function with example usage: pad_sequences(sequences, maxlen, dtype, padding, truncating, value) and returns: x