code: def _py_expand_short(subsequence, sequence, max_l_dist):
      
      # The following diagram shows the score calculation step.
      #
      # Each new score is the minimum of:
      #  * a OR a + 1 (substitution, if needed)
      #  * b + 1 (deletion, i.e. skipping a sequence character)
      #  * c + 1 (insertion, i.e. skipping a sub-sequence character)
      #
      # a -- +1 -> c
      #
      # |  \       |
      # |   \      |
      # +1  +1?    +1
      # |      \   |
      # v       ⌟  v
      #
      # b -- +1 -> scores[subseq_index]
  
      subseq_len = len(subsequence)
      if subseq_len == 0:
          return (0, 0)
  
      # Initialize the scores array with values for just skipping sub-sequence
      # chars.
      scores = list(range(1, subseq_len + 1))
  
      min_score = subseq_len
      min_score_idx = -1
  
      for seq_index, char in enumerate(sequence):
          # calculate scores, one for each character in the sub-sequence
          a = seq_index
          c = a + 1
          for subseq_index in range(subseq_len):
              b = scores[subseq_index]
              c = scores[subseq_index] = min(
                  a + (char != subsequence[subseq_index]),
                  b + 1,
                  c + 1,
              )
              a = b
  
          # keep the minimum score found for matches of the entire sub-sequence
          if c <= min_score:
              min_score = c
              min_score_idx = seq_index
  
          # bail early when it is impossible to find a better expansion
          elif min(scores) >= min_score:
              break
  
      return (min_score, min_score_idx + 1) if min_score <= max_l_dist else (None, None)
docs: Straightforward implementation of partial match expansion.