This function is copied from a modified pycodestyle checker for Twisted.
    See https://github.com/cyli/TwistySublime/blob/master/twisted_pycodestyle.py
    Twisted Coding Standard:

    Separate top-level function and class definitions with three blank lines.
    Method definitions inside a class are separated by two blank lines.

    Extra blank lines may be used (sparingly) to separate groups of related
    functions.  Blank lines may be omitted between a bunch of related
    one-liners (e.g. a set of dummy implementations).

    Use blank lines in functions, sparingly, to indicate logical sections.

    Okay: def a():\n    pass\n\n\n\ndef b():\n    pass
    Okay: class A():\n    pass\n\n\n\nclass B():\n    pass
    Okay: def a():\n    pass\n\n\n# Foo\n# Bar\n\ndef b():\n    pass

    E301: class Foo:\n    b = 0\n    def bar():\n        pass
    E302: def a():\n    pass\n\ndef b(n):\n    pass
    E303: def a():\n    pass\n\n\n\ndef b(n):\n    pass
    E303: def a():\n\n\n\n    pass
    E304: @decorator\n\ndef a():\n    pass
    E305: "comment"\n\n\ndef a():\n    pass
    E306: variable="value"\ndef a():   pass

    @param logical_line: Supplied by PyCodeStyle. The content of the line it is dealing with.
    @param blank_lines: Supplied by PyCodeStyle.
    @param indent_level: Supplied by PyCodeStyle. The current indent level.
    @param line_number: Supplied by PyCodeStyle. The current line number.
    @param blank_before: Supplied by PyCodeStyle. The number of blank lines before this one.
    @param previous_logical: Supplied by PyCodeStyle. The previous logical line.
    @param previous_indent_level: Supplied by PyCodeStyle. The indent level of the previous line.

def modifiedBlankLines(logical_line, blank_lines, indent_level, line_number,
                       blank_before, previous_logical, previous_indent_level):
    """
    This function is copied from a modified pycodestyle checker for Twisted.
    See https://github.com/cyli/TwistySublime/blob/master/twisted_pycodestyle.py
    Twisted Coding Standard:

    Separate top-level function and class definitions with three blank lines.
    Method definitions inside a class are separated by two blank lines.

    Extra blank lines may be used (sparingly) to separate groups of related
    functions.  Blank lines may be omitted between a bunch of related
    one-liners (e.g. a set of dummy implementations).

    Use blank lines in functions, sparingly, to indicate logical sections.

    Okay: def a():\n    pass\n\n\n\ndef b():\n    pass
    Okay: class A():\n    pass\n\n\n\nclass B():\n    pass
    Okay: def a():\n    pass\n\n\n# Foo\n# Bar\n\ndef b():\n    pass

    E301: class Foo:\n    b = 0\n    def bar():\n        pass
    E302: def a():\n    pass\n\ndef b(n):\n    pass
    E303: def a():\n    pass\n\n\n\ndef b(n):\n    pass
    E303: def a():\n\n\n\n    pass
    E304: @decorator\n\ndef a():\n    pass
    E305: "comment"\n\n\ndef a():\n    pass
    E306: variable="value"\ndef a():   pass

    @param logical_line: Supplied by PyCodeStyle. The content of the line it is dealing with.
    @param blank_lines: Supplied by PyCodeStyle.
    @param indent_level: Supplied by PyCodeStyle. The current indent level.
    @param line_number: Supplied by PyCodeStyle. The current line number.
    @param blank_before: Supplied by PyCodeStyle. The number of blank lines before this one.
    @param previous_logical: Supplied by PyCodeStyle. The previous logical line.
    @param previous_indent_level: Supplied by PyCodeStyle. The indent level of the previous line.
    """
    def isClassDefDecorator(thing):
        return (thing.startswith('def ') or
                thing.startswith('class ') or
                thing.startswith('@'))

    # Don't expect blank lines before the first line
    if line_number == 1:
        return

    previous_is_comment = pycodestyle.DOCSTRING_REGEX.match(previous_logical)

    # Check blank lines after a decorator,
    if previous_logical.startswith('@'):
        if blank_before:
            yield 0, "E304 blank lines found after function decorator"

    if isClassDefDecorator(logical_line):
        if indent_level:
            # There should only be 1 line or less between docstrings and
            # the next function
            if previous_is_comment:
                # Previous is a comment so it has one extra indentation.
                at_same_indent = previous_indent_level - 4 == indent_level
                if (
                    at_same_indent and
                    logical_line.startswith('def ') and
                    blank_before == 2
                        ):
                    # This look like a previous method with a docstring
                    # and empty body.
                    return
                if blank_before > 1:
                    yield 0, (
                        "E305 too many blank lines after docstring "
                        "(%d)" % (blank_before,))

            # Between first level functions, there should be 2 blank lines.
            # any further indended functions can have one or zero lines
            else:
                if not (blank_before == 2 or
                        indent_level > 4 or
                        previous_indent_level <= indent_level):
                    yield 0, ("E301 expected 2 blank lines, "
                              "found %d" % (blank_before,))

        # Top level, there should be 3 blank lines between class/function
        # definitions (but not necessarily after variable declarations)
        elif previous_indent_level and blank_before != 3:
            yield 0, ("E302 expected 3 blank lines, "
                      "found %d" % (blank_before,))

    elif blank_before > 1 and indent_level:
        yield 0, "E303 too many blank lines (%d)" % (blank_before,)