def determine_indent(str):
    """
    Figure out the character(s) used for indents in a given source code fragement.

    Parameters
    ----------
    str : string
      source code starting at an indent of 0 and containing at least one indented block.

    Returns
    -------
    string
      The character(s) used for indenting.

    Example
    -------
    code = "def do_stuff(x)\n   print(x)\n"
    indent = determine_indent(str)
    print("The code '", code, "' is indented with \n'", indent, "' (size: ", len(indent), ")")
    """
    indent = None
    reg = r"""
      ^(?P<previous_indent>\s*)\S.+?:\n      # line starting a block, i. e. '   for i in x:\n'
      ((\s*)\n)*                             # empty lines
      (?P=previous_indent)(?P<indent>\s+)\S  # first indented line of the new block, i. e. '      d'(..oStuff())
      """

    matches = re.compile(reg, re.MULTILINE | re.VERBOSE).finditer(str)
    for block_start in matches:
        new_indent = block_start.groupdict()['indent']
        if indent and new_indent != indent:
            warnings.warn('Inconsistent indentation detected.'
                          'Found "%s" (length: %i) as well as "%s" (length: %i)' % (
                              indent, len(indent), new_indent, len(new_indent)))
        indent = new_indent
    return indent
def determine_indent ( str ) : indent = None reg = r"""
      ^(?P<previous_indent>\s*)\S.+?:\n      # line starting a block, i. e. '   for i in x:\n'
      ((\s*)\n)*                             # empty lines
      (?P=previous_indent)(?P<indent>\s+)\S  # first indented line of the new block, i. e. '      d'(..oStuff())
      """ matches = re . compile ( reg , re . MULTILINE | re . VERBOSE ) . finditer ( str ) for block_start in matches : new_indent = block_start . groupdict ( ) [ 'indent' ] if indent and new_indent != indent : warnings . warn ( 'Inconsistent indentation detected.' 'Found "%s" (length: %i) as well as "%s" (length: %i)' % ( indent , len ( indent ) , new_indent , len ( new_indent ) ) ) indent = new_indent return indent
Figure out the character(s) used for indents in a given source code fragement.

    Parameters
    ----------
    str : string
      source code starting at an indent of 0 and containing at least one indented block.

    Returns
    -------
    string
      The character(s) used for indenting.

    Example
    -------
    code = "def do_stuff(x)\n   print(x)\n"
    indent = determine_indent(str)
    print("The code '", code, "' is indented with \n'", indent, "' (size: ", len(indent), ")")