sparkConf = create_spark_conf().setMaster("<STR_LIT>").setAppName("<STR_LIT>")<EOL>self.sc = get_spark_context(sparkConf)<EOL>init_engine()<EOL>resource_path = os.path.join(os.path.split(__file__)[<NUM_LIT:0>], "<STR_LIT>")<EOL>self.image_path = os.path.join(resource_path, "<STR_LIT>")<EOL>

setup any state tied to the execution of the given method in a
class.  setup_method is invoked for every test method of a class.


self.sc.stop()<EOL>

teardown any state that was previously setup with a setup_method
call.


sparkConf = create_spark_conf().setMaster("<STR_LIT>").setAppName("<STR_LIT>")<EOL>self.sc = get_spark_context(sparkConf)<EOL>self.sqlContext = SQLContext(self.sc)<EOL>init_engine()<EOL>

setup any state tied to the execution of the given method in a
        class.  setup_method is invoked for every test method of a class.


self.sc.stop()<EOL>

teardown any state that was previously setup with a setup_method
        call.


JavaCreator.add_creator_class("<STR_LIT>")<EOL>sparkConf = create_spark_conf().setMaster("<STR_LIT>").setAppName("<STR_LIT>")<EOL>self.sc = get_spark_context(sparkConf)<EOL>init_engine()<EOL>


self.sc.stop()<EOL>


keras.backend.set_image_dim_ordering("<STR_LIT>")<EOL>sparkConf = create_spark_conf().setMaster("<STR_LIT>").setAppName("<STR_LIT>")<EOL>self.sc = get_spark_context(sparkConf)<EOL>self.sqlContext = SQLContext(self.sc)<EOL>init_engine()<EOL>


keras.backend.set_image_dim_ordering("<STR_LIT>")<EOL>self.sc.stop()<EOL>


indent = INDENT_REGEX.match(physical_line).group(<NUM_LIT:1>)<EOL>for offset, char in enumerate(indent):<EOL><INDENT>if char != indent_char:<EOL><INDENT>return offset, "<STR_LIT>"<EOL><DEDENT><DEDENT>

r"""Never mix tabs and spaces.

    The most popular way of indenting Python is with spaces only.  The
    second-most popular way is with tabs only.  Code indented with a mixture
    of tabs and spaces should be converted to using spaces exclusively.  When
    invoking the Python command line interpreter with the -t option, it issues
    warnings about code that illegally mixes tabs and spaces.  When using -tt
    these warnings become errors.  These options are highly recommended!

    Okay: if a == 0:\n        a = 1\n        b = 1
    E101: if a == 0:\n        a = 1\n\tb = 1


indent = INDENT_REGEX.match(physical_line).group(<NUM_LIT:1>)<EOL>if '<STR_LIT:\t>' in indent:<EOL><INDENT>return indent.index('<STR_LIT:\t>'), "<STR_LIT>"<EOL><DEDENT>

r"""For new projects, spaces-only are strongly recommended over tabs.

    Okay: if True:\n    return
    W191: if True:\n\treturn


physical_line = physical_line.rstrip('<STR_LIT:\n>')    <EOL>physical_line = physical_line.rstrip('<STR_LIT:\r>')    <EOL>physical_line = physical_line.rstrip('<STR_LIT>')  <EOL>stripped = physical_line.rstrip('<STR_LIT>')<EOL>if physical_line != stripped:<EOL><INDENT>if stripped:<EOL><INDENT>return len(stripped), "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>return <NUM_LIT:0>, "<STR_LIT>"<EOL><DEDENT><DEDENT>

r"""Trailing whitespace is superfluous.

    The warning returned varies on whether the line itself is blank, for easier
    filtering for those who want to indent their blank lines.

    Okay: spam(1)\n#
    W291: spam(1) \n#
    W293: class Foo(object):\n    \n    bang = 12


def trailing_blank_lines(physical_line, lines, line_number, total_lines):

if line_number == total_lines:<EOL><INDENT>stripped_last_line = physical_line.rstrip()<EOL>if not stripped_last_line:<EOL><INDENT>return <NUM_LIT:0>, "<STR_LIT>"<EOL><DEDENT>if stripped_last_line == physical_line:<EOL><INDENT>return len(physical_line), "<STR_LIT>"<EOL><DEDENT><DEDENT>

r"""Trailing blank lines are superfluous.

    Okay: spam(1)
    W391: spam(1)\n

    However the last line should end with a new line (warning W292).


def maximum_line_length(physical_line, max_line_length, multiline):

line = physical_line.rstrip()<EOL>length = len(line)<EOL>if length > max_line_length and not noqa(line):<EOL><INDENT>chunks = line.split()<EOL>if ((len(chunks) == <NUM_LIT:1> and multiline) or<EOL>(len(chunks) == <NUM_LIT:2> and chunks[<NUM_LIT:0>] == '<STR_LIT:#>')) andlen(line) - len(chunks[-<NUM_LIT:1>]) < max_line_length - <NUM_LIT:7>:<EOL><INDENT>return<EOL><DEDENT>if hasattr(line, '<STR_LIT>'):   <EOL><INDENT>try:<EOL><INDENT>length = len(line.decode('<STR_LIT:utf-8>'))<EOL><DEDENT>except UnicodeError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if length > max_line_length:<EOL><INDENT>return (max_line_length, "<STR_LIT>"<EOL>"<STR_LIT>" % (length, max_line_length))<EOL><DEDENT><DEDENT>

r"""Limit all lines to a maximum of 79 characters.

    There are still many devices around that are limited to 80 character
    lines; plus, limiting windows to 80 characters makes it possible to have
    several windows side-by-side.  The default wrapping on such devices looks
    ugly.  Therefore, please limit all lines to a maximum of 79 characters.
    For flowing long blocks of text (docstrings or comments), limiting the
    length to 72 characters is recommended.

    Reports error E501.


def blank_lines(logical_line, blank_lines, indent_level, line_number,<EOL>blank_before, previous_logical, previous_indent_level):

if line_number < <NUM_LIT:3> and not previous_logical:<EOL><INDENT>return  <EOL><DEDENT>if previous_logical.startswith('<STR_LIT:@>'):<EOL><INDENT>if blank_lines:<EOL><INDENT>yield <NUM_LIT:0>, "<STR_LIT>"<EOL><DEDENT><DEDENT>elif blank_lines > <NUM_LIT:2> or (indent_level and blank_lines == <NUM_LIT:2>):<EOL><INDENT>yield <NUM_LIT:0>, "<STR_LIT>" % blank_lines<EOL><DEDENT>elif logical_line.startswith(('<STR_LIT>', '<STR_LIT>', '<STR_LIT:@>')):<EOL><INDENT>if indent_level:<EOL><INDENT>if not (blank_before or previous_indent_level < indent_level or<EOL>DOCSTRING_REGEX.match(previous_logical)):<EOL><INDENT>yield <NUM_LIT:0>, "<STR_LIT>"<EOL><DEDENT><DEDENT>elif blank_before != <NUM_LIT:2>:<EOL><INDENT>yield <NUM_LIT:0>, "<STR_LIT>" % blank_before<EOL><DEDENT><DEDENT>

r"""Separate top-level function and class definitions with two blank lines.

    Method definitions inside a class are separated by a single blank line.

    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\ndef 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


line = logical_line<EOL>for match in EXTRANEOUS_WHITESPACE_REGEX.finditer(line):<EOL><INDENT>text = match.group()<EOL>char = text.strip()<EOL>found = match.start()<EOL>if text == char + '<STR_LIT:U+0020>':<EOL><INDENT>yield found + <NUM_LIT:1>, "<STR_LIT>" % char<EOL><DEDENT>elif line[found - <NUM_LIT:1>] != '<STR_LIT:U+002C>':<EOL><INDENT>code = ('<STR_LIT>' if char in '<STR_LIT>' else '<STR_LIT>')  <EOL>yield found, "<STR_LIT>" % (code, char)<EOL><DEDENT><DEDENT>

r"""Avoid extraneous whitespace.

    Avoid extraneous whitespace in these situations:
    - Immediately inside parentheses, brackets or braces.
    - Immediately before a comma, semicolon, or colon.

    Okay: spam(ham[1], {eggs: 2})
    E201: spam( ham[1], {eggs: 2})
    E201: spam(ham[ 1], {eggs: 2})
    E201: spam(ham[1], { eggs: 2})
    E202: spam(ham[1], {eggs: 2} )
    E202: spam(ham[1 ], {eggs: 2})
    E202: spam(ham[1], {eggs: 2 })

    E203: if x == 4: print x, y; x, y = y , x
    E203: if x == 4: print x, y ; x, y = y, x
    E203: if x == 4 : print x, y; x, y = y, x


for match in KEYWORD_REGEX.finditer(logical_line):<EOL><INDENT>before, after = match.groups()<EOL>if '<STR_LIT:\t>' in before:<EOL><INDENT>yield match.start(<NUM_LIT:1>), "<STR_LIT>"<EOL><DEDENT>elif len(before) > <NUM_LIT:1>:<EOL><INDENT>yield match.start(<NUM_LIT:1>), "<STR_LIT>"<EOL><DEDENT>if '<STR_LIT:\t>' in after:<EOL><INDENT>yield match.start(<NUM_LIT:2>), "<STR_LIT>"<EOL><DEDENT>elif len(after) > <NUM_LIT:1>:<EOL><INDENT>yield match.start(<NUM_LIT:2>), "<STR_LIT>"<EOL><DEDENT><DEDENT>

r"""Avoid extraneous whitespace around keywords.

    Okay: True and False
    E271: True and  False
    E272: True  and False
    E273: True and\tFalse
    E274: True\tand False


line = logical_line<EOL>for index in range(len(line) - <NUM_LIT:1>):<EOL><INDENT>char = line[index]<EOL>if char in '<STR_LIT>' and line[index + <NUM_LIT:1>] not in WHITESPACE:<EOL><INDENT>before = line[:index]<EOL>if char == '<STR_LIT::>' and before.count('<STR_LIT:[>') > before.count('<STR_LIT:]>') andbefore.rfind('<STR_LIT:{>') < before.rfind('<STR_LIT:[>'):<EOL><INDENT>continue  <EOL><DEDENT>if char == '<STR_LIT:U+002C>' and line[index + <NUM_LIT:1>] == '<STR_LIT:)>':<EOL><INDENT>continue  <EOL><DEDENT>yield index, "<STR_LIT>" % char<EOL><DEDENT><DEDENT>

r"""Each comma, semicolon or colon should be followed by whitespace.

    Okay: [a, b]
    Okay: (3,)
    Okay: a[1:4]
    Okay: a[:4]
    Okay: a[1:]
    Okay: a[1:4:2]
    E231: ['a','b']
    E231: foo(bar,baz)
    E231: [{'a':'b'}]


def indentation(logical_line, previous_logical, indent_char,<EOL>indent_level, previous_indent_level):

c = <NUM_LIT:0> if logical_line else <NUM_LIT:3><EOL>tmpl = "<STR_LIT>" if logical_line else "<STR_LIT>"<EOL>if indent_level % <NUM_LIT:4>:<EOL><INDENT>yield <NUM_LIT:0>, tmpl % (<NUM_LIT:1> + c, "<STR_LIT>")<EOL><DEDENT>indent_expect = previous_logical.endswith('<STR_LIT::>')<EOL>if indent_expect and indent_level <= previous_indent_level:<EOL><INDENT>yield <NUM_LIT:0>, tmpl % (<NUM_LIT:2> + c, "<STR_LIT>")<EOL><DEDENT>elif not indent_expect and indent_level > previous_indent_level:<EOL><INDENT>yield <NUM_LIT:0>, tmpl % (<NUM_LIT:3> + c, "<STR_LIT>")<EOL><DEDENT>

r"""Use 4 spaces per indentation level.

    For really old code that you don't want to mess up, you can continue to
    use 8-space tabs.

    Okay: a = 1
    Okay: if a == 0:\n    a = 1
    E111:   a = 1
    E114:   # a = 1

    Okay: for item in items:\n    pass
    E112: for item in items:\npass
    E115: for item in items:\n# Hi\n    pass

    Okay: a = 1\nb = 2
    E113: a = 1\n    b = 2
    E116: a = 1\n    # b = 2


def continued_indentation(logical_line, tokens, indent_level, hang_closing,<EOL>indent_char, noqa, verbose):

first_row = tokens[<NUM_LIT:0>][<NUM_LIT:2>][<NUM_LIT:0>]<EOL>nrows = <NUM_LIT:1> + tokens[-<NUM_LIT:1>][<NUM_LIT:2>][<NUM_LIT:0>] - first_row<EOL>if noqa or nrows == <NUM_LIT:1>:<EOL><INDENT>return<EOL><DEDENT>indent_next = logical_line.endswith('<STR_LIT::>')<EOL>row = depth = <NUM_LIT:0><EOL>valid_hangs = (<NUM_LIT:4>,) if indent_char != '<STR_LIT:\t>' else (<NUM_LIT:4>, <NUM_LIT:8>)<EOL>parens = [<NUM_LIT:0>] * nrows<EOL>rel_indent = [<NUM_LIT:0>] * nrows<EOL>open_rows = [[<NUM_LIT:0>]]<EOL>hangs = [None]<EOL>indent_chances = {}<EOL>last_indent = tokens[<NUM_LIT:0>][<NUM_LIT:2>]<EOL>visual_indent = None<EOL>last_token_multiline = False<EOL>indent = [last_indent[<NUM_LIT:1>]]<EOL>if verbose >= <NUM_LIT:3>:<EOL><INDENT>print("<STR_LIT>" + tokens[<NUM_LIT:0>][<NUM_LIT:4>].rstrip())<EOL><DEDENT>for token_type, text, start, end, line in tokens:<EOL><INDENT>newline = row < start[<NUM_LIT:0>] - first_row<EOL>if newline:<EOL><INDENT>row = start[<NUM_LIT:0>] - first_row<EOL>newline = not last_token_multiline and token_type not in NEWLINE<EOL><DEDENT>if newline:<EOL><INDENT>last_indent = start<EOL>if verbose >= <NUM_LIT:3>:<EOL><INDENT>print("<STR_LIT>" + line.rstrip())<EOL><DEDENT>rel_indent[row] = expand_indent(line) - indent_level<EOL>close_bracket = (token_type == tokenize.OP and text in '<STR_LIT>')<EOL>for open_row in reversed(open_rows[depth]):<EOL><INDENT>hang = rel_indent[row] - rel_indent[open_row]<EOL>hanging_indent = hang in valid_hangs<EOL>if hanging_indent:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if hangs[depth]:<EOL><INDENT>hanging_indent = (hang == hangs[depth])<EOL><DEDENT>visual_indent = (not close_bracket and hang > <NUM_LIT:0> and<EOL>indent_chances.get(start[<NUM_LIT:1>]))<EOL>if close_bracket and indent[depth]:<EOL><INDENT>if start[<NUM_LIT:1>] != indent[depth]:<EOL><INDENT>yield (start, "<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>elif close_bracket and not hang:<EOL><INDENT>if hang_closing:<EOL><INDENT>yield start, "<STR_LIT>"<EOL><DEDENT><DEDENT>elif indent[depth] and start[<NUM_LIT:1>] < indent[depth]:<EOL><INDENT>if visual_indent is not True:<EOL><INDENT>yield (start, "<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>elif hanging_indent or (indent_next and rel_indent[row] == <NUM_LIT:8>):<EOL><INDENT>if close_bracket and not hang_closing:<EOL><INDENT>yield (start, "<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>hangs[depth] = hang<EOL><DEDENT>elif visual_indent is True:<EOL><INDENT>indent[depth] = start[<NUM_LIT:1>]<EOL><DEDENT>elif visual_indent in (text, str):<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if hang <= <NUM_LIT:0>:<EOL><INDENT>error = "<STR_LIT>", "<STR_LIT>"<EOL><DEDENT>elif indent[depth]:<EOL><INDENT>error = "<STR_LIT>", "<STR_LIT>"<EOL><DEDENT>elif not close_bracket and hangs[depth]:<EOL><INDENT>error = "<STR_LIT>", "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>hangs[depth] = hang<EOL>if hang > <NUM_LIT:4>:<EOL><INDENT>error = "<STR_LIT>", "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>error = "<STR_LIT>", "<STR_LIT>"<EOL><DEDENT><DEDENT>yield start, "<STR_LIT>" % error<EOL><DEDENT><DEDENT>if (parens[row] and<EOL>token_type not in (tokenize.NL, tokenize.COMMENT) and<EOL>not indent[depth]):<EOL><INDENT>indent[depth] = start[<NUM_LIT:1>]<EOL>indent_chances[start[<NUM_LIT:1>]] = True<EOL>if verbose >= <NUM_LIT:4>:<EOL><INDENT>print("<STR_LIT>" % (depth, start[<NUM_LIT:1>]))<EOL><DEDENT><DEDENT>elif (token_type in (tokenize.STRING, tokenize.COMMENT) or<EOL>text in ('<STR_LIT:u>', '<STR_LIT>', '<STR_LIT:b>', '<STR_LIT>')):<EOL><INDENT>indent_chances[start[<NUM_LIT:1>]] = str<EOL><DEDENT>elif not indent_chances and not row and not depth and text == '<STR_LIT>':<EOL><INDENT>indent_chances[end[<NUM_LIT:1>] + <NUM_LIT:1>] = True<EOL><DEDENT>elif text == '<STR_LIT::>' and line[end[<NUM_LIT:1>]:].isspace():<EOL><INDENT>open_rows[depth].append(row)<EOL><DEDENT>if token_type == tokenize.OP:<EOL><INDENT>if text in '<STR_LIT>':<EOL><INDENT>depth += <NUM_LIT:1><EOL>indent.append(<NUM_LIT:0>)<EOL>hangs.append(None)<EOL>if len(open_rows) == depth:<EOL><INDENT>open_rows.append([])<EOL><DEDENT>open_rows[depth].append(row)<EOL>parens[row] += <NUM_LIT:1><EOL>if verbose >= <NUM_LIT:4>:<EOL><INDENT>print("<STR_LIT>" %<EOL>(depth, start[<NUM_LIT:1>], indent[depth]))<EOL><DEDENT><DEDENT>elif text in '<STR_LIT>' and depth > <NUM_LIT:0>:<EOL><INDENT>prev_indent = indent.pop() or last_indent[<NUM_LIT:1>]<EOL>hangs.pop()<EOL>for d in range(depth):<EOL><INDENT>if indent[d] > prev_indent:<EOL><INDENT>indent[d] = <NUM_LIT:0><EOL><DEDENT><DEDENT>for ind in list(indent_chances):<EOL><INDENT>if ind >= prev_indent:<EOL><INDENT>del indent_chances[ind]<EOL><DEDENT><DEDENT>del open_rows[depth + <NUM_LIT:1>:]<EOL>depth -= <NUM_LIT:1><EOL>if depth:<EOL><INDENT>indent_chances[indent[depth]] = True<EOL><DEDENT>for idx in range(row, -<NUM_LIT:1>, -<NUM_LIT:1>):<EOL><INDENT>if parens[idx]:<EOL><INDENT>parens[idx] -= <NUM_LIT:1><EOL>break<EOL><DEDENT><DEDENT><DEDENT>assert len(indent) == depth + <NUM_LIT:1><EOL>if start[<NUM_LIT:1>] not in indent_chances:<EOL><INDENT>indent_chances[start[<NUM_LIT:1>]] = text<EOL><DEDENT><DEDENT>last_token_multiline = (start[<NUM_LIT:0>] != end[<NUM_LIT:0>])<EOL>if last_token_multiline:<EOL><INDENT>rel_indent[end[<NUM_LIT:0>] - first_row] = rel_indent[row]<EOL><DEDENT><DEDENT>if indent_next and expand_indent(line) == indent_level + <NUM_LIT:4>:<EOL><INDENT>pos = (start[<NUM_LIT:0>], indent[<NUM_LIT:0>] + <NUM_LIT:4>)<EOL>if visual_indent:<EOL><INDENT>code = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>code = "<STR_LIT>"<EOL><DEDENT>yield pos, "<STR_LIT>" % code<EOL><DEDENT>

r"""Continuation lines indentation.

    Continuation lines should align wrapped elements either vertically
    using Python's implicit line joining inside parentheses, brackets
    and braces, or using a hanging indent.

    When using a hanging indent these considerations should be applied:
    - there should be no arguments on the first line, and
    - further indentation should be used to clearly distinguish itself as a
      continuation line.

    Okay: a = (\n)
    E123: a = (\n    )

    Okay: a = (\n    42)
    E121: a = (\n   42)
    E122: a = (\n42)
    E123: a = (\n    42\n    )
    E124: a = (24,\n     42\n)
    E125: if (\n    b):\n    pass
    E126: a = (\n        42)
    E127: a = (24,\n      42)
    E128: a = (24,\n    42)
    E129: if (a or\n    b):\n    pass
    E131: a = (\n    42\n 24)


prev_type, prev_text, __, prev_end, __ = tokens[<NUM_LIT:0>]<EOL>for index in range(<NUM_LIT:1>, len(tokens)):<EOL><INDENT>token_type, text, start, end, __ = tokens[index]<EOL>if (token_type == tokenize.OP and<EOL>text in '<STR_LIT>' and<EOL>start != prev_end and<EOL>(prev_type == tokenize.NAME or prev_text in '<STR_LIT>') and<EOL>(index < <NUM_LIT:2> or tokens[index - <NUM_LIT:2>][<NUM_LIT:1>] != '<STR_LIT:class>') and<EOL>not keyword.iskeyword(prev_text)):<EOL><INDENT>yield prev_end, "<STR_LIT>" % text<EOL><DEDENT>prev_type = token_type<EOL>prev_text = text<EOL>prev_end = end<EOL><DEDENT>

r"""Avoid extraneous whitespace.

    Avoid extraneous whitespace in the following situations:
    - before the open parenthesis that starts the argument list of a
      function call.
    - before the open parenthesis that starts an indexing or slicing.

    Okay: spam(1)
    E211: spam (1)

    Okay: dict['key'] = list[index]
    E211: dict ['key'] = list[index]
    E211: dict['key'] = list [index]


for match in OPERATOR_REGEX.finditer(logical_line):<EOL><INDENT>before, after = match.groups()<EOL>if '<STR_LIT:\t>' in before:<EOL><INDENT>yield match.start(<NUM_LIT:1>), "<STR_LIT>"<EOL><DEDENT>elif len(before) > <NUM_LIT:1>:<EOL><INDENT>yield match.start(<NUM_LIT:1>), "<STR_LIT>"<EOL><DEDENT>if '<STR_LIT:\t>' in after:<EOL><INDENT>yield match.start(<NUM_LIT:2>), "<STR_LIT>"<EOL><DEDENT>elif len(after) > <NUM_LIT:1>:<EOL><INDENT>yield match.start(<NUM_LIT:2>), "<STR_LIT>"<EOL><DEDENT><DEDENT>

r"""Avoid extraneous whitespace around an operator.

    Okay: a = 12 + 3
    E221: a = 4  + 5
    E222: a = 4 +  5
    E223: a = 4\t+ 5
    E224: a = 4 +\t5


parens = <NUM_LIT:0><EOL>need_space = False<EOL>prev_type = tokenize.OP<EOL>prev_text = prev_end = None<EOL>for token_type, text, start, end, line in tokens:<EOL><INDENT>if token_type in SKIP_COMMENTS:<EOL><INDENT>continue<EOL><DEDENT>if text in ('<STR_LIT:(>', '<STR_LIT>'):<EOL><INDENT>parens += <NUM_LIT:1><EOL><DEDENT>elif text == '<STR_LIT:)>':<EOL><INDENT>parens -= <NUM_LIT:1><EOL><DEDENT>if need_space:<EOL><INDENT>if start != prev_end:<EOL><INDENT>if need_space is not True and not need_space[<NUM_LIT:1>]:<EOL><INDENT>yield (need_space[<NUM_LIT:0>],<EOL>"<STR_LIT>")<EOL><DEDENT>need_space = False<EOL><DEDENT>elif text == '<STR_LIT:>>' and prev_text in ('<STR_LIT:<>', '<STR_LIT:->'):<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if need_space is True or need_space[<NUM_LIT:1>]:<EOL><INDENT>yield prev_end, "<STR_LIT>"<EOL><DEDENT>elif prev_text != '<STR_LIT>':<EOL><INDENT>code, optype = '<STR_LIT>', '<STR_LIT>'<EOL>if prev_text == '<STR_LIT:%>':<EOL><INDENT>code, optype = '<STR_LIT>', '<STR_LIT>'<EOL><DEDENT>elif prev_text not in ARITHMETIC_OP:<EOL><INDENT>code, optype = '<STR_LIT>', '<STR_LIT>'<EOL><DEDENT>yield (need_space[<NUM_LIT:0>], "<STR_LIT>"<EOL>"<STR_LIT>" % (code, optype))<EOL><DEDENT>need_space = False<EOL><DEDENT><DEDENT>elif token_type == tokenize.OP and prev_end is not None:<EOL><INDENT>if text == '<STR_LIT:=>' and parens:<EOL><INDENT>pass<EOL><DEDENT>elif text in WS_NEEDED_OPERATORS:<EOL><INDENT>need_space = True<EOL><DEDENT>elif text in UNARY_OPERATORS:<EOL><INDENT>if (prev_text in '<STR_LIT>' if prev_type == tokenize.OP<EOL>else prev_text not in KEYWORDS):<EOL><INDENT>need_space = None<EOL><DEDENT><DEDENT>elif text in WS_OPTIONAL_OPERATORS:<EOL><INDENT>need_space = None<EOL><DEDENT>if need_space is None:<EOL><INDENT>need_space = (prev_end, start != prev_end)<EOL><DEDENT>elif need_space and start == prev_end:<EOL><INDENT>yield prev_end, "<STR_LIT>"<EOL>need_space = False<EOL><DEDENT><DEDENT>prev_type = token_type<EOL>prev_text = text<EOL>prev_end = end<EOL><DEDENT>

r"""Surround operators with a single space on either side.

    - Always surround these binary operators with a single space on
      either side: assignment (=), augmented assignment (+=, -= etc.),
      comparisons (==, <, >, !=, <=, >=, in, not in, is, is not),
      Booleans (and, or, not).

    - If operators with different priorities are used, consider adding
      whitespace around the operators with the lowest priorities.

    Okay: i = i + 1
    Okay: submitted += 1
    Okay: x = x * 2 - 1
    Okay: hypot2 = x * x + y * y
    Okay: c = (a + b) * (a - b)
    Okay: foo(bar, key='word', *args, **kwargs)
    Okay: alpha[:-i]

    E225: i=i+1
    E225: submitted +=1
    E225: x = x /2 - 1
    E225: z = x **y
    E226: c = (a+b) * (a-b)
    E226: hypot2 = x*x + y*y
    E227: c = a|b
    E228: msg = fmt%(errno, errmsg)


line = logical_line<EOL>for m in WHITESPACE_AFTER_COMMA_REGEX.finditer(line):<EOL><INDENT>found = m.start() + <NUM_LIT:1><EOL>if '<STR_LIT:\t>' in m.group():<EOL><INDENT>yield found, "<STR_LIT>" % m.group()[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>yield found, "<STR_LIT>" % m.group()[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>

r"""Avoid extraneous whitespace after a comma or a colon.

    Note: these checks are disabled by default

    Okay: a = (1, 2)
    E241: a = (1,  2)
    E242: a = (1,\t2)


parens = <NUM_LIT:0><EOL>no_space = False<EOL>prev_end = None<EOL>annotated_func_arg = False<EOL>in_def = logical_line.startswith('<STR_LIT>')<EOL>message = "<STR_LIT>"<EOL>for token_type, text, start, end, line in tokens:<EOL><INDENT>if token_type == tokenize.NL:<EOL><INDENT>continue<EOL><DEDENT>if no_space:<EOL><INDENT>no_space = False<EOL>if start != prev_end:<EOL><INDENT>yield (prev_end, message)<EOL><DEDENT><DEDENT>if token_type == tokenize.OP:<EOL><INDENT>if text == '<STR_LIT:(>':<EOL><INDENT>parens += <NUM_LIT:1><EOL><DEDENT>elif text == '<STR_LIT:)>':<EOL><INDENT>parens -= <NUM_LIT:1><EOL><DEDENT>elif in_def and text == '<STR_LIT::>' and parens == <NUM_LIT:1>:<EOL><INDENT>annotated_func_arg = True<EOL><DEDENT>elif parens and text == '<STR_LIT:U+002C>' and parens == <NUM_LIT:1>:<EOL><INDENT>annotated_func_arg = False<EOL><DEDENT>elif parens and text == '<STR_LIT:=>' and not annotated_func_arg:<EOL><INDENT>no_space = True<EOL>if start != prev_end:<EOL><INDENT>yield (prev_end, message)<EOL><DEDENT><DEDENT>if not parens:<EOL><INDENT>annotated_func_arg = False<EOL><DEDENT><DEDENT>prev_end = end<EOL><DEDENT>

r"""Don't use spaces around the '=' sign in function arguments.

    Don't use spaces around the '=' sign when used to indicate a
    keyword argument or a default parameter value.

    Okay: def complex(real, imag=0.0):
    Okay: return magic(r=real, i=imag)
    Okay: boolean(a == b)
    Okay: boolean(a != b)
    Okay: boolean(a <= b)
    Okay: boolean(a >= b)
    Okay: def foo(arg: int = 42):

    E251: def complex(real, imag = 0.0):
    E251: return magic(r = real, i = imag)


prev_end = (<NUM_LIT:0>, <NUM_LIT:0>)<EOL>for token_type, text, start, end, line in tokens:<EOL><INDENT>if token_type == tokenize.COMMENT:<EOL><INDENT>inline_comment = line[:start[<NUM_LIT:1>]].strip()<EOL>if inline_comment:<EOL><INDENT>if prev_end[<NUM_LIT:0>] == start[<NUM_LIT:0>] and start[<NUM_LIT:1>] < prev_end[<NUM_LIT:1>] + <NUM_LIT:2>:<EOL><INDENT>yield (prev_end,<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>symbol, sp, comment = text.partition('<STR_LIT:U+0020>')<EOL>bad_prefix = symbol not in '<STR_LIT>' and (symbol.lstrip('<STR_LIT:#>')[:<NUM_LIT:1>] or '<STR_LIT:#>')<EOL>if inline_comment:<EOL><INDENT>if bad_prefix or comment[:<NUM_LIT:1>] in WHITESPACE:<EOL><INDENT>yield start, "<STR_LIT>"<EOL><DEDENT><DEDENT>elif bad_prefix and (bad_prefix != '<STR_LIT:!>' or start[<NUM_LIT:0>] > <NUM_LIT:1>):<EOL><INDENT>if bad_prefix != '<STR_LIT:#>':<EOL><INDENT>yield start, "<STR_LIT>"<EOL><DEDENT>elif comment:<EOL><INDENT>yield start, "<STR_LIT>"<EOL><DEDENT><DEDENT><DEDENT>elif token_type != tokenize.NL:<EOL><INDENT>prev_end = end<EOL><DEDENT><DEDENT>

r"""Separate inline comments by at least two spaces.

    An inline comment is a comment on the same line as a statement.  Inline
    comments should be separated by at least two spaces from the statement.
    They should start with a # and a single space.

    Each line of a block comment starts with a # and a single space
    (unless it is indented text inside the comment).

    Okay: x = x + 1  # Increment x
    Okay: x = x + 1    # Increment x
    Okay: # Block comment
    E261: x = x + 1 # Increment x
    E262: x = x + 1  #Increment x
    E262: x = x + 1  #  Increment x
    E265: #Block comment
    E266: ### Block comment


line = logical_line<EOL>if line.startswith('<STR_LIT>'):<EOL><INDENT>found = line.find('<STR_LIT:U+002C>')<EOL>if -<NUM_LIT:1> < found and '<STR_LIT:;>' not in line[:found]:<EOL><INDENT>yield found, "<STR_LIT>"<EOL><DEDENT><DEDENT>

r"""Imports should usually be on separate lines.

    Okay: import os\nimport sys
    E401: import sys, os

    Okay: from subprocess import Popen, PIPE
    Okay: from myclas import MyClass
    Okay: from foo.bar.yourclass import YourClass
    Okay: import myclass
    Okay: import foo.bar.yourclass


def module_imports_on_top_of_file(<EOL>logical_line, indent_level, checker_state, noqa):

def is_string_literal(line):<EOL><INDENT>if line[<NUM_LIT:0>] in '<STR_LIT>':<EOL><INDENT>line = line[<NUM_LIT:1>:]<EOL><DEDENT>if line and line[<NUM_LIT:0>] in '<STR_LIT>':<EOL><INDENT>line = line[<NUM_LIT:1>:]<EOL><DEDENT>return line and (line[<NUM_LIT:0>] == '<STR_LIT:">' or line[<NUM_LIT:0>] == "<STR_LIT:'>")<EOL><DEDENT>allowed_try_keywords = ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>if indent_level:  <EOL><INDENT>return<EOL><DEDENT>if not logical_line:  <EOL><INDENT>return<EOL><DEDENT>if noqa:<EOL><INDENT>return<EOL><DEDENT>line = logical_line<EOL>if line.startswith('<STR_LIT>') or line.startswith('<STR_LIT>'):<EOL><INDENT>if checker_state.get('<STR_LIT>', False):<EOL><INDENT>yield <NUM_LIT:0>, "<STR_LIT>"<EOL><DEDENT><DEDENT>elif any(line.startswith(kw) for kw in allowed_try_keywords):<EOL><INDENT>return<EOL><DEDENT>elif is_string_literal(line):<EOL><INDENT>if checker_state.get('<STR_LIT>', False):<EOL><INDENT>checker_state['<STR_LIT>'] = True<EOL><DEDENT>else:<EOL><INDENT>checker_state['<STR_LIT>'] = True<EOL><DEDENT><DEDENT>else:<EOL><INDENT>checker_state['<STR_LIT>'] = True<EOL><DEDENT>

r"""Imports are always put at the top of the file, just after any module
    comments and docstrings, and before module globals and constants.

    Okay: import os
    Okay: # this is a comment\nimport os
    Okay: '''this is a module docstring'''\nimport os
    Okay: r'''this is a module docstring'''\nimport os
    Okay: try:\n    import x\nexcept:\n    pass\nelse:\n    pass\nimport y
    Okay: try:\n    import x\nexcept:\n    pass\nfinally:\n    pass\nimport y
    E402: a=1\nimport os
    E402: 'One string'\n"Two string"\nimport os
    E402: a=1\nfrom sys import x

    Okay: if x:\n    import os


line = logical_line<EOL>last_char = len(line) - <NUM_LIT:1><EOL>found = line.find('<STR_LIT::>')<EOL>while -<NUM_LIT:1> < found < last_char:<EOL><INDENT>before = line[:found]<EOL>if ((before.count('<STR_LIT:{>') <= before.count('<STR_LIT:}>') and   <EOL>before.count('<STR_LIT:[>') <= before.count('<STR_LIT:]>') and   <EOL>before.count('<STR_LIT:(>') <= before.count('<STR_LIT:)>'))):    <EOL><INDENT>lambda_kw = LAMBDA_REGEX.search(before)<EOL>if lambda_kw:<EOL><INDENT>before = line[:lambda_kw.start()].rstrip()<EOL>if before[-<NUM_LIT:1>:] == '<STR_LIT:=>' and isidentifier(before[:-<NUM_LIT:1>].strip()):<EOL><INDENT>yield <NUM_LIT:0>, ("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>break<EOL><DEDENT>if before.startswith('<STR_LIT>'):<EOL><INDENT>yield <NUM_LIT:0>, "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>yield found, "<STR_LIT>"<EOL><DEDENT><DEDENT>found = line.find('<STR_LIT::>', found + <NUM_LIT:1>)<EOL><DEDENT>found = line.find('<STR_LIT:;>')<EOL>while -<NUM_LIT:1> < found:<EOL><INDENT>if found < last_char:<EOL><INDENT>yield found, "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>yield found, "<STR_LIT>"<EOL><DEDENT>found = line.find('<STR_LIT:;>', found + <NUM_LIT:1>)<EOL><DEDENT>

r"""Compound statements (on the same line) are generally discouraged.

    While sometimes it's okay to put an if/for/while with a small body
    on the same line, never do this for multi-clause statements.
    Also avoid folding such long lines!

    Always use a def statement instead of an assignment statement that
    binds a lambda expression directly to a name.

    Okay: if foo == 'blah':\n    do_blah_thing()
    Okay: do_one()
    Okay: do_two()
    Okay: do_three()

    E701: if foo == 'blah': do_blah_thing()
    E701: for x in lst: total += x
    E701: while t < 10: t = delay()
    E701: if foo == 'blah': do_blah_thing()
    E701: else: do_non_blah_thing()
    E701: try: something()
    E701: finally: cleanup()
    E701: if foo == 'blah': one(); two(); three()
    E702: do_one(); do_two(); do_three()
    E703: do_four();  # useless semicolon
    E704: def f(x): return 2*x
    E731: f = lambda x: 2*x


prev_start = prev_end = parens = <NUM_LIT:0><EOL>comment = False<EOL>backslash = None<EOL>for token_type, text, start, end, line in tokens:<EOL><INDENT>if token_type == tokenize.COMMENT:<EOL><INDENT>comment = True<EOL><DEDENT>if start[<NUM_LIT:0>] != prev_start and parens and backslash and not comment:<EOL><INDENT>yield backslash, "<STR_LIT>"<EOL><DEDENT>if end[<NUM_LIT:0>] != prev_end:<EOL><INDENT>if line.rstrip('<STR_LIT:\r\n>').endswith('<STR_LIT:\\>'):<EOL><INDENT>backslash = (end[<NUM_LIT:0>], len(line.splitlines()[-<NUM_LIT:1>]) - <NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>backslash = None<EOL><DEDENT>prev_start = prev_end = end[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>prev_start = start[<NUM_LIT:0>]<EOL><DEDENT>if token_type == tokenize.OP:<EOL><INDENT>if text in '<STR_LIT>':<EOL><INDENT>parens += <NUM_LIT:1><EOL><DEDENT>elif text in '<STR_LIT>':<EOL><INDENT>parens -= <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>

r"""Avoid explicit line join between brackets.

    The preferred way of wrapping long lines is by using Python's implied line
    continuation inside parentheses, brackets and braces.  Long lines can be
    broken over multiple lines by wrapping expressions in parentheses.  These
    should be used in preference to using a backslash for line continuation.

    E502: aaa = [123, \\n       123]
    E502: aaa = ("bbb " \\n       "ccc")

    Okay: aaa = [123,\n       123]
    Okay: aaa = ("bbb "\n       "ccc")
    Okay: aaa = "bbb " \\n    "ccc"
    Okay: aaa = 123  # \\


def is_binary_operator(token_type, text):<EOL><INDENT>return ((token_type == tokenize.OP or text in ['<STR_LIT>', '<STR_LIT>']) and<EOL>text not in "<STR_LIT>")<EOL><DEDENT>line_break = False<EOL>unary_context = True<EOL>for token_type, text, start, end, line in tokens:<EOL><INDENT>if token_type == tokenize.COMMENT:<EOL><INDENT>continue<EOL><DEDENT>if ('<STR_LIT:\n>' in text or '<STR_LIT:\r>' in text) and token_type != tokenize.STRING:<EOL><INDENT>line_break = True<EOL><DEDENT>else:<EOL><INDENT>if (is_binary_operator(token_type, text) and line_break and<EOL>not unary_context):<EOL><INDENT>yield start, "<STR_LIT>"<EOL><DEDENT>unary_context = text in '<STR_LIT>'<EOL>line_break = False<EOL><DEDENT><DEDENT>

r"""
    Avoid breaks before binary operators.

    The preferred place to break around a binary operator is after the
    operator, not before it.

    W503: (width == 0\n + height == 0)
    W503: (width == 0\n and height == 0)

    Okay: (width == 0 +\n height == 0)
    Okay: foo(\n    -x)
    Okay: foo(x\n    [])
    Okay: x = '''\n''' + ''
    Okay: foo(x,\n    -y)
    Okay: foo(x,  # comment\n    -y)


match = not noqa and COMPARE_SINGLETON_REGEX.search(logical_line)<EOL>if match:<EOL><INDENT>singleton = match.group(<NUM_LIT:1>) or match.group(<NUM_LIT:3>)<EOL>same = (match.group(<NUM_LIT:2>) == '<STR_LIT>')<EOL>msg = "<STR_LIT>" % (('<STR_LIT>' if same else '<STR_LIT>') + singleton)<EOL>if singleton in ('<STR_LIT:None>',):<EOL><INDENT>code = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>code = '<STR_LIT>'<EOL>nonzero = ((singleton == '<STR_LIT:True>' and same) or<EOL>(singleton == '<STR_LIT:False>' and not same))<EOL>msg += "<STR_LIT>" % ('<STR_LIT>' if nonzero else '<STR_LIT>')<EOL><DEDENT>yield match.start(<NUM_LIT:2>), ("<STR_LIT>" %<EOL>(code, singleton, msg))<EOL><DEDENT>

r"""Comparison to singletons should use "is" or "is not".

    Comparisons to singletons like None should always be done
    with "is" or "is not", never the equality operators.

    Okay: if arg is not None:
    E711: if arg != None:
    E711: if None == arg:
    E712: if arg == True:
    E712: if False == arg:

    Also, beware of writing if x when you really mean if x is not None --
    e.g. when testing whether a variable or argument that defaults to None was
    set to some other value.  The other value might have a type (such as a
    container) that could be false in a boolean context!


match = COMPARE_NEGATIVE_REGEX.search(logical_line)<EOL>if match:<EOL><INDENT>pos = match.start(<NUM_LIT:1>)<EOL>if match.group(<NUM_LIT:2>) == '<STR_LIT>':<EOL><INDENT>yield pos, "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>yield pos, "<STR_LIT>"<EOL><DEDENT><DEDENT>

r"""Negative comparison should be done using "not in" and "is not".

    Okay: if x not in y:\n    pass
    Okay: assert (X in Y or X is Z)
    Okay: if not (X in Y):\n    pass
    Okay: zz = x is not y
    E713: Z = not X in Y
    E713: if not X.B in Y:\n    pass
    E714: if not X is Y:\n    pass
    E714: Z = not X.B is Y


match = COMPARE_TYPE_REGEX.search(logical_line)<EOL>if match and not noqa:<EOL><INDENT>inst = match.group(<NUM_LIT:1>)<EOL>if inst and isidentifier(inst) and inst not in SINGLETONS:<EOL><INDENT>return  <EOL><DEDENT>yield match.start(), "<STR_LIT>"<EOL><DEDENT>

r"""Object type comparisons should always use isinstance().

    Do not compare types directly.

    Okay: if isinstance(obj, int):
    E721: if type(obj) is type(1):

    When checking if an object is a string, keep in mind that it might be a
    unicode string too! In Python 2.3, str and unicode have a common base
    class, basestring, so you can do:

    Okay: if isinstance(obj, basestring):
    Okay: if type(a1) is type(b1):


pos = logical_line.find('<STR_LIT>')<EOL>if pos > -<NUM_LIT:1> and not noqa:<EOL><INDENT>yield pos, "<STR_LIT>"<EOL><DEDENT>

r"""The {}.has_key() method is removed in Python 3: use the 'in' operator.

    Okay: if "alph" in d:\n    print d["alph"]
    W601: assert d.has_key('alph')


match = RAISE_COMMA_REGEX.match(logical_line)<EOL>if match and not RERAISE_COMMA_REGEX.match(logical_line):<EOL><INDENT>yield match.end() - <NUM_LIT:1>, "<STR_LIT>"<EOL><DEDENT>

r"""When raising an exception, use "raise ValueError('message')".

    The older form is removed in Python 3.

    Okay: raise DummyError("Message")
    W602: raise DummyError, "Message"


pos = logical_line.find('<STR_LIT>')<EOL>if pos > -<NUM_LIT:1>:<EOL><INDENT>yield pos, "<STR_LIT>"<EOL><DEDENT>

r"""New code should always use != instead of <>.

    The older syntax is removed in Python 3.

    Okay: if a != 'no':
    W603: if a <> 'no':


r"""Backticks are removed in Python 3: use repr() instead.

    Okay: val = repr(1 + 2)
    W604: val = `1 + 2`


if '<STR_LIT:\t>' not in line:<EOL><INDENT>return len(line) - len(line.lstrip())<EOL><DEDENT>result = <NUM_LIT:0><EOL>for char in line:<EOL><INDENT>if char == '<STR_LIT:\t>':<EOL><INDENT>result = result // <NUM_LIT:8> * <NUM_LIT:8> + <NUM_LIT:8><EOL><DEDENT>elif char == '<STR_LIT:U+0020>':<EOL><INDENT>result += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>return result<EOL>

r"""Return the amount of indentation.

    Tabs are expanded to the next multiple of 8.

    >>> expand_indent('    ')
    4
    >>> expand_indent('\t')
    8
    >>> expand_indent('       \t')
    8
    >>> expand_indent('        \t')
    16


<EOL>start = text.index(text[-<NUM_LIT:1>]) + <NUM_LIT:1><EOL>end = len(text) - <NUM_LIT:1><EOL>if text[-<NUM_LIT:3>:] in ('<STR_LIT>', "<STR_LIT>"):<EOL><INDENT>start += <NUM_LIT:2><EOL>end -= <NUM_LIT:2><EOL><DEDENT>return text[:start] + '<STR_LIT:x>' * (end - start) + text[end:]<EOL>

Replace contents with 'xxx' to prevent syntax matching.

    >>> mute_string('"abc"')
    '"xxx"'
    >>> mute_string("'''abc'''")
    "'''xxx'''"
    >>> mute_string("r'abc'")
    "r'xxx'"


def parse_udiff(diff, patterns=None, parent='<STR_LIT:.>'):

<EOL>rv = {}<EOL>path = nrows = None<EOL>for line in diff.splitlines():<EOL><INDENT>if nrows:<EOL><INDENT>if line[:<NUM_LIT:1>] != '<STR_LIT:->':<EOL><INDENT>nrows -= <NUM_LIT:1><EOL><DEDENT>continue<EOL><DEDENT>if line[:<NUM_LIT:3>] == '<STR_LIT>':<EOL><INDENT>hunk_match = HUNK_REGEX.match(line)<EOL>(row, nrows) = [int(g or '<STR_LIT:1>') for g in hunk_match.groups()]<EOL>rv[path].update(range(row, row + nrows))<EOL><DEDENT>elif line[:<NUM_LIT:3>] == '<STR_LIT>':<EOL><INDENT>path = line[<NUM_LIT:4>:].split('<STR_LIT:\t>', <NUM_LIT:1>)[<NUM_LIT:0>]<EOL>if path[:<NUM_LIT:2>] == '<STR_LIT>':<EOL><INDENT>path = path[<NUM_LIT:2>:]<EOL><DEDENT>rv[path] = set()<EOL><DEDENT><DEDENT>return dict([(os.path.join(parent, path), rows)<EOL>for (path, rows) in rv.items()<EOL>if rows and filename_match(path, patterns)])<EOL>


if not value:<EOL><INDENT>return []<EOL><DEDENT>if isinstance(value, list):<EOL><INDENT>return value<EOL><DEDENT>paths = []<EOL>for path in value.split('<STR_LIT:U+002C>'):<EOL><INDENT>path = path.strip()<EOL>if '<STR_LIT:/>' in path:<EOL><INDENT>path = os.path.abspath(os.path.join(parent, path))<EOL><DEDENT>paths.append(path.rstrip('<STR_LIT:/>'))<EOL><DEDENT>return paths<EOL>

Parse a comma-separated list of paths.

    Return a list of absolute paths.


def filename_match(filename, patterns, default=True):

if not patterns:<EOL><INDENT>return default<EOL><DEDENT>return any(fnmatch(filename, pattern) for pattern in patterns)<EOL>

Check if patterns contains a pattern that matches filename.

    If patterns is unspecified, this always returns True.


def _add_check(check, kind, codes, args):<EOL><INDENT>if check in _checks[kind]:<EOL><INDENT>_checks[kind][check][<NUM_LIT:0>].extend(codes or [])<EOL><DEDENT>else:<EOL><INDENT>_checks[kind][check] = (codes or ['<STR_LIT>'], args)<EOL><DEDENT><DEDENT>if inspect.isfunction(check):<EOL><INDENT>args = _get_parameters(check)<EOL>if args and args[<NUM_LIT:0>] in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>if codes is None:<EOL><INDENT>codes = ERRORCODE_REGEX.findall(check.__doc__ or '<STR_LIT>')<EOL><DEDENT>_add_check(check, args[<NUM_LIT:0>], codes, args)<EOL><DEDENT><DEDENT>elif inspect.isclass(check):<EOL><INDENT>if _get_parameters(check.__init__)[:<NUM_LIT:2>] == ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>_add_check(check, '<STR_LIT>', codes, None)<EOL><DEDENT><DEDENT>


mod = inspect.getmodule(register_check)<EOL>for (name, function) in inspect.getmembers(mod, inspect.isfunction):<EOL><INDENT>register_check(function)<EOL><DEDENT>

Register all globally visible functions.

    The first argument name is either 'physical_line' or 'logical_line'.


def read_config(options, args, arglist, parser):

config = RawConfigParser()<EOL>cli_conf = options.config<EOL>local_dir = os.curdir<EOL>if USER_CONFIG and os.path.isfile(USER_CONFIG):<EOL><INDENT>if options.verbose:<EOL><INDENT>print('<STR_LIT>' % USER_CONFIG)<EOL><DEDENT>config.read(USER_CONFIG)<EOL><DEDENT>parent = tail = args and os.path.abspath(os.path.commonprefix(args))<EOL>while tail:<EOL><INDENT>if config.read(os.path.join(parent, fn) for fn in PROJECT_CONFIG):<EOL><INDENT>local_dir = parent<EOL>if options.verbose:<EOL><INDENT>print('<STR_LIT>' % parent)<EOL><DEDENT>break<EOL><DEDENT>(parent, tail) = os.path.split(parent)<EOL><DEDENT>if cli_conf and os.path.isfile(cli_conf):<EOL><INDENT>if options.verbose:<EOL><INDENT>print('<STR_LIT>' % cli_conf)<EOL><DEDENT>config.read(cli_conf)<EOL><DEDENT>pep8_section = parser.prog<EOL>if config.has_section(pep8_section):<EOL><INDENT>option_list = dict([(o.dest, o.type or o.action)<EOL>for o in parser.option_list])<EOL>(new_options, __) = parser.parse_args([])<EOL>for opt in config.options(pep8_section):<EOL><INDENT>if opt.replace('<STR_LIT:_>', '<STR_LIT:->') not in parser.config_options:<EOL><INDENT>print("<STR_LIT>" % opt)<EOL>continue<EOL><DEDENT>if options.verbose > <NUM_LIT:1>:<EOL><INDENT>print("<STR_LIT>" % (opt, config.get(pep8_section, opt)))<EOL><DEDENT>normalized_opt = opt.replace('<STR_LIT:->', '<STR_LIT:_>')<EOL>opt_type = option_list[normalized_opt]<EOL>if opt_type in ('<STR_LIT:int>', '<STR_LIT:count>'):<EOL><INDENT>value = config.getint(pep8_section, opt)<EOL><DEDENT>elif opt_type == '<STR_LIT:string>':<EOL><INDENT>value = config.get(pep8_section, opt)<EOL>if normalized_opt == '<STR_LIT>':<EOL><INDENT>value = normalize_paths(value, local_dir)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>assert opt_type in ('<STR_LIT:store_true>', '<STR_LIT>')<EOL>value = config.getboolean(pep8_section, opt)<EOL><DEDENT>setattr(new_options, normalized_opt, value)<EOL><DEDENT>(options, __) = parser.parse_args(arglist, values=new_options)<EOL><DEDENT>options.doctest = options.testsuite = False<EOL>return options<EOL>

Read and parse configurations

    If a config file is specified on the command line with the "--config"
    option, then only it is used for configuration.

    Otherwise, the user configuration (~/.config/pep8) and any local
    configurations in the current directory or above will be merged together
    (in that order) using the read method of ConfigParser.


def process_options(arglist=None, parse_argv=False, config_file=None,<EOL>parser=None):

if not parser:<EOL><INDENT>parser = get_parser()<EOL><DEDENT>if not parser.has_option('<STR_LIT>'):<EOL><INDENT>group = parser.add_option_group("<STR_LIT>", description=(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" %<EOL>(parser.prog, '<STR_LIT:U+002CU+0020>'.join(parser.config_options))))<EOL>group.add_option('<STR_LIT>', metavar='<STR_LIT:path>', default=config_file,<EOL>help="<STR_LIT>")<EOL><DEDENT>if not arglist and not parse_argv:<EOL><INDENT>arglist = []<EOL><DEDENT>(options, args) = parser.parse_args(arglist)<EOL>options.reporter = None<EOL>if options.ensure_value('<STR_LIT>', False):<EOL><INDENT>args.append(options.testsuite)<EOL><DEDENT>elif not options.ensure_value('<STR_LIT>', False):<EOL><INDENT>if parse_argv and not args:<EOL><INDENT>if options.diff or any(os.path.exists(name)<EOL>for name in PROJECT_CONFIG):<EOL><INDENT>args = ['<STR_LIT:.>']<EOL><DEDENT>else:<EOL><INDENT>parser.error('<STR_LIT>')<EOL><DEDENT><DEDENT>options = read_config(options, args, arglist, parser)<EOL>options.reporter = parse_argv and options.quiet == <NUM_LIT:1> and FileReport<EOL><DEDENT>options.filename = _parse_multi_options(options.filename)<EOL>options.exclude = normalize_paths(options.exclude)<EOL>options.select = _parse_multi_options(options.select)<EOL>options.ignore = _parse_multi_options(options.ignore)<EOL>if options.diff:<EOL><INDENT>options.reporter = DiffReport<EOL>stdin = stdin_get_value()<EOL>options.selected_lines = parse_udiff(stdin, options.filename, args[<NUM_LIT:0>])<EOL>args = sorted(options.selected_lines)<EOL><DEDENT>return options, args<EOL>

Process options passed either via arglist or via command line args.

    Passing in the ``config_file`` parameter allows other tools, such as flake8
    to specify their own options to be processed in pep8.


if options:<EOL><INDENT>return [o.strip() for o in options.split(split_token) if o.strip()]<EOL><DEDENT>else:<EOL><INDENT>return options<EOL><DEDENT>

r"""Split and strip and discard empties.

    Turns the following:

    A,
    B,

    into ["A", "B"]


import signal<EOL>try:<EOL><INDENT>signal.signal(signal.SIGPIPE, lambda signum, frame: sys.exit(<NUM_LIT:1>))<EOL><DEDENT>except AttributeError:<EOL><INDENT>pass    <EOL><DEDENT>pep8style = StyleGuide(parse_argv=True)<EOL>options = pep8style.options<EOL>if options.doctest or options.testsuite:<EOL><INDENT>from testsuite.support import run_tests<EOL>report = run_tests(pep8style)<EOL><DEDENT>else:<EOL><INDENT>report = pep8style.check_files()<EOL><DEDENT>if options.statistics:<EOL><INDENT>report.print_statistics()<EOL><DEDENT>if options.benchmark:<EOL><INDENT>report.print_benchmark()<EOL><DEDENT>if options.testsuite and not options.quiet:<EOL><INDENT>report.print_results()<EOL><DEDENT>if report.total_errors:<EOL><INDENT>if options.count:<EOL><INDENT>sys.stderr.write(str(report.total_errors) + '<STR_LIT:\n>')<EOL><DEDENT>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>

Parse options and run checks on Python source.


(exc_type, exc) = sys.exc_info()[:<NUM_LIT:2>]<EOL>if len(exc.args) > <NUM_LIT:1>:<EOL><INDENT>offset = exc.args[<NUM_LIT:1>]<EOL>if len(offset) > <NUM_LIT:2>:<EOL><INDENT>offset = offset[<NUM_LIT:1>:<NUM_LIT:3>]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>offset = (<NUM_LIT:1>, <NUM_LIT:0>)<EOL><DEDENT>self.report_error(offset[<NUM_LIT:0>], offset[<NUM_LIT:1>] or <NUM_LIT:0>,<EOL>'<STR_LIT>' % (exc_type.__name__, exc.args[<NUM_LIT:0>]),<EOL>self.report_invalid_syntax)<EOL>


if self.line_number >= self.total_lines:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>line = self.lines[self.line_number]<EOL>self.line_number += <NUM_LIT:1><EOL>if self.indent_char is None and line[:<NUM_LIT:1>] in WHITESPACE:<EOL><INDENT>self.indent_char = line[<NUM_LIT:0>]<EOL><DEDENT>return line<EOL>

Get the next line from the input buffer.


def run_check(self, check, argument_names):

arguments = []<EOL>for name in argument_names:<EOL><INDENT>arguments.append(getattr(self, name))<EOL><DEDENT>return check(*arguments)<EOL>


def init_checker_state(self, name, argument_names):

if '<STR_LIT>' in argument_names:<EOL><INDENT>self.checker_state = self._checker_states.setdefault(name, {})<EOL><DEDENT>

Prepares a custom state for the specific checker plugin.


self.physical_line = line<EOL>for name, check, argument_names in self._physical_checks:<EOL><INDENT>self.init_checker_state(name, argument_names)<EOL>result = self.run_check(check, argument_names)<EOL>if result is not None:<EOL><INDENT>(offset, text) = result<EOL>self.report_error(self.line_number, offset, text, check)<EOL>if text[:<NUM_LIT:4>] == '<STR_LIT>':<EOL><INDENT>self.indent_char = line[<NUM_LIT:0>]<EOL><DEDENT><DEDENT><DEDENT>

Run all physical checks on a raw input line.


logical = []<EOL>comments = []<EOL>length = <NUM_LIT:0><EOL>prev_row = prev_col = mapping = None<EOL>for token_type, text, start, end, line in self.tokens:<EOL><INDENT>if token_type in SKIP_TOKENS:<EOL><INDENT>continue<EOL><DEDENT>if not mapping:<EOL><INDENT>mapping = [(<NUM_LIT:0>, start)]<EOL><DEDENT>if token_type == tokenize.COMMENT:<EOL><INDENT>comments.append(text)<EOL>continue<EOL><DEDENT>if token_type == tokenize.STRING:<EOL><INDENT>text = mute_string(text)<EOL><DEDENT>if prev_row:<EOL><INDENT>(start_row, start_col) = start<EOL>if prev_row != start_row:    <EOL><INDENT>prev_text = self.lines[prev_row - <NUM_LIT:1>][prev_col - <NUM_LIT:1>]<EOL>if prev_text == '<STR_LIT:U+002C>' or (prev_text not in '<STR_LIT>' and<EOL>text not in '<STR_LIT>'):<EOL><INDENT>text = '<STR_LIT:U+0020>' + text<EOL><DEDENT><DEDENT>elif prev_col != start_col:  <EOL><INDENT>text = line[prev_col:start_col] + text<EOL><DEDENT><DEDENT>logical.append(text)<EOL>length += len(text)<EOL>mapping.append((length, end))<EOL>(prev_row, prev_col) = end<EOL><DEDENT>self.logical_line = '<STR_LIT>'.join(logical)<EOL>self.noqa = comments and noqa('<STR_LIT>'.join(comments))<EOL>return mapping<EOL>


self.report.increment_logical_line()<EOL>mapping = self.build_tokens_line()<EOL>if not mapping:<EOL><INDENT>return<EOL><DEDENT>(start_row, start_col) = mapping[<NUM_LIT:0>][<NUM_LIT:1>]<EOL>start_line = self.lines[start_row - <NUM_LIT:1>]<EOL>self.indent_level = expand_indent(start_line[:start_col])<EOL>if self.blank_before < self.blank_lines:<EOL><INDENT>self.blank_before = self.blank_lines<EOL><DEDENT>if self.verbose >= <NUM_LIT:2>:<EOL><INDENT>print(self.logical_line[:<NUM_LIT>].rstrip())<EOL><DEDENT>for name, check, argument_names in self._logical_checks:<EOL><INDENT>if self.verbose >= <NUM_LIT:4>:<EOL><INDENT>print('<STR_LIT:U+0020>' + name)<EOL><DEDENT>self.init_checker_state(name, argument_names)<EOL>for offset, text in self.run_check(check, argument_names) or ():<EOL><INDENT>if not isinstance(offset, tuple):<EOL><INDENT>for token_offset, pos in mapping:<EOL><INDENT>if offset <= token_offset:<EOL><INDENT>break<EOL><DEDENT><DEDENT>offset = (pos[<NUM_LIT:0>], pos[<NUM_LIT:1>] + offset - token_offset)<EOL><DEDENT>self.report_error(offset[<NUM_LIT:0>], offset[<NUM_LIT:1>], text, check)<EOL><DEDENT><DEDENT>if self.logical_line:<EOL><INDENT>self.previous_indent_level = self.indent_level<EOL>self.previous_logical = self.logical_line<EOL><DEDENT>self.blank_lines = <NUM_LIT:0><EOL>self.tokens = []<EOL>

Build a line from tokens and run all logical checks on it.


try:<EOL><INDENT>tree = compile('<STR_LIT>'.join(self.lines), '<STR_LIT>', '<STR_LIT>', PyCF_ONLY_AST)<EOL><DEDENT>except (ValueError, SyntaxError, TypeError):<EOL><INDENT>return self.report_invalid_syntax()<EOL><DEDENT>for name, cls, __ in self._ast_checks:<EOL><INDENT>checker = cls(tree, self.filename)<EOL>for lineno, offset, text, check in checker.run():<EOL><INDENT>if not self.lines or not noqa(self.lines[lineno - <NUM_LIT:1>]):<EOL><INDENT>self.report_error(lineno, offset, text, check)<EOL><DEDENT><DEDENT><DEDENT>

Build the file's AST and run all AST checks.


if self._io_error:<EOL><INDENT>self.report_error(<NUM_LIT:1>, <NUM_LIT:0>, '<STR_LIT>' % self._io_error, readlines)<EOL><DEDENT>tokengen = tokenize.generate_tokens(self.readline)<EOL>try:<EOL><INDENT>for token in tokengen:<EOL><INDENT>if token[<NUM_LIT:2>][<NUM_LIT:0>] > self.total_lines:<EOL><INDENT>return<EOL><DEDENT>self.maybe_check_physical(token)<EOL>yield token<EOL><DEDENT><DEDENT>except (SyntaxError, tokenize.TokenError):<EOL><INDENT>self.report_invalid_syntax()<EOL><DEDENT>

Tokenize the file, run physical line checks and yield tokens.


<EOL>if _is_eol_token(token):<EOL><INDENT>self.check_physical(token[<NUM_LIT:4>])<EOL><DEDENT>elif token[<NUM_LIT:0>] == tokenize.STRING and '<STR_LIT:\n>' in token[<NUM_LIT:1>]:<EOL><INDENT>if noqa(token[<NUM_LIT:4>]):<EOL><INDENT>return<EOL><DEDENT>self.multiline = True<EOL>self.line_number = token[<NUM_LIT:2>][<NUM_LIT:0>]<EOL>for line in token[<NUM_LIT:1>].split('<STR_LIT:\n>')[:-<NUM_LIT:1>]:<EOL><INDENT>self.check_physical(line + '<STR_LIT:\n>')<EOL>self.line_number += <NUM_LIT:1><EOL><DEDENT>self.multiline = False<EOL><DEDENT>

If appropriate (based on token), check current physical line(s).


def check_all(self, expected=None, line_offset=<NUM_LIT:0>):

self.report.init_file(self.filename, self.lines, expected, line_offset)<EOL>self.total_lines = len(self.lines)<EOL>if self._ast_checks:<EOL><INDENT>self.check_ast()<EOL><DEDENT>self.line_number = <NUM_LIT:0><EOL>self.indent_char = None<EOL>self.indent_level = self.previous_indent_level = <NUM_LIT:0><EOL>self.previous_logical = '<STR_LIT>'<EOL>self.tokens = []<EOL>self.blank_lines = self.blank_before = <NUM_LIT:0><EOL>parens = <NUM_LIT:0><EOL>for token in self.generate_tokens():<EOL><INDENT>self.tokens.append(token)<EOL>token_type, text = token[<NUM_LIT:0>:<NUM_LIT:2>]<EOL>if self.verbose >= <NUM_LIT:3>:<EOL><INDENT>if token[<NUM_LIT:2>][<NUM_LIT:0>] == token[<NUM_LIT:3>][<NUM_LIT:0>]:<EOL><INDENT>pos = '<STR_LIT>' % (token[<NUM_LIT:2>][<NUM_LIT:1>] or '<STR_LIT>', token[<NUM_LIT:3>][<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>pos = '<STR_LIT>' % token[<NUM_LIT:3>][<NUM_LIT:0>]<EOL><DEDENT>print('<STR_LIT>' %<EOL>(token[<NUM_LIT:2>][<NUM_LIT:0>], pos, tokenize.tok_name[token[<NUM_LIT:0>]], text))<EOL><DEDENT>if token_type == tokenize.OP:<EOL><INDENT>if text in '<STR_LIT>':<EOL><INDENT>parens += <NUM_LIT:1><EOL><DEDENT>elif text in '<STR_LIT>':<EOL><INDENT>parens -= <NUM_LIT:1><EOL><DEDENT><DEDENT>elif not parens:<EOL><INDENT>if token_type in NEWLINE:<EOL><INDENT>if token_type == tokenize.NEWLINE:<EOL><INDENT>self.check_logical()<EOL>self.blank_before = <NUM_LIT:0><EOL><DEDENT>elif len(self.tokens) == <NUM_LIT:1>:<EOL><INDENT>self.blank_lines += <NUM_LIT:1><EOL>del self.tokens[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>self.check_logical()<EOL><DEDENT><DEDENT>elif COMMENT_WITH_NL and token_type == tokenize.COMMENT:<EOL><INDENT>if len(self.tokens) == <NUM_LIT:1>:<EOL><INDENT>token = list(token)<EOL>token[<NUM_LIT:1>] = text.rstrip('<STR_LIT:\r\n>')<EOL>token[<NUM_LIT:3>] = (token[<NUM_LIT:2>][<NUM_LIT:0>], token[<NUM_LIT:2>][<NUM_LIT:1>] + len(token[<NUM_LIT:1>]))<EOL>self.tokens = [tuple(token)]<EOL>self.check_logical()<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if self.tokens:<EOL><INDENT>self.check_physical(self.lines[-<NUM_LIT:1>])<EOL>self.check_logical()<EOL><DEDENT>return self.report.get_file_results()<EOL>


self._start_time = time.time()<EOL>


self.elapsed = time.time() - self._start_time<EOL>


def init_file(self, filename, lines, expected, line_offset):

self.filename = filename<EOL>self.lines = lines<EOL>self.expected = expected or ()<EOL>self.line_offset = line_offset<EOL>self.file_errors = <NUM_LIT:0><EOL>self.counters['<STR_LIT>'] += <NUM_LIT:1><EOL>self.counters['<STR_LIT>'] += len(lines)<EOL>


self.counters['<STR_LIT>'] += <NUM_LIT:1><EOL>


def error(self, line_number, offset, text, check):

code = text[:<NUM_LIT:4>]<EOL>if self._ignore_code(code):<EOL><INDENT>return<EOL><DEDENT>if code in self.counters:<EOL><INDENT>self.counters[code] += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>self.counters[code] = <NUM_LIT:1><EOL>self.messages[code] = text[<NUM_LIT:5>:]<EOL><DEDENT>if code in self.expected:<EOL><INDENT>return<EOL><DEDENT>if self.print_filename and not self.file_errors:<EOL><INDENT>print(self.filename)<EOL><DEDENT>self.file_errors += <NUM_LIT:1><EOL>self.total_errors += <NUM_LIT:1><EOL>return code<EOL>


return self.file_errors<EOL>

Return the count of errors and warnings for this file.


return sum([self.counters[key]<EOL>for key in self.messages if key.startswith(prefix)])<EOL>

Return the total count of errors and warnings.


return ['<STR_LIT>' % (self.counters[key], key, self.messages[key])<EOL>for key in sorted(self.messages) if key.startswith(prefix)]<EOL>

Get statistics for message codes that start with the prefix.

        prefix='' matches all errors and warnings
        prefix='E' matches all errors
        prefix='W' matches all warnings
        prefix='E4' matches all errors that have to do with imports


for line in self.get_statistics(prefix):<EOL><INDENT>print(line)<EOL><DEDENT>

Print overall statistics (number of errors and warnings).


print('<STR_LIT>' % (self.elapsed, '<STR_LIT>'))<EOL>if self.elapsed:<EOL><INDENT>for key in self._benchmark_keys:<EOL><INDENT>print('<STR_LIT>' %<EOL>(self.counters[key] / self.elapsed, key,<EOL>self.counters[key]))<EOL><DEDENT><DEDENT>


self._deferred_print = []<EOL>return super(StandardReport, self).init_file(<EOL>filename, lines, expected, line_offset)<EOL>


code = super(StandardReport, self).error(line_number, offset,<EOL>text, check)<EOL>if code and (self.counters[code] == <NUM_LIT:1> or self._repeat):<EOL><INDENT>self._deferred_print.append(<EOL>(line_number, offset, code, text[<NUM_LIT:5>:], check.__doc__))<EOL><DEDENT>return code<EOL>


self._deferred_print.sort()<EOL>for line_number, offset, code, text, doc in self._deferred_print:<EOL><INDENT>print(self._fmt % {<EOL>'<STR_LIT:path>': self.filename,<EOL>'<STR_LIT>': self.line_offset + line_number, '<STR_LIT>': offset + <NUM_LIT:1>,<EOL>'<STR_LIT:code>': code, '<STR_LIT:text>': text,<EOL>})<EOL>if self._show_source:<EOL><INDENT>if line_number > len(self.lines):<EOL><INDENT>line = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>line = self.lines[line_number - <NUM_LIT:1>]<EOL><DEDENT>print(line.rstrip())<EOL>print(re.sub(r'<STR_LIT>', '<STR_LIT:U+0020>', line[:offset]) + '<STR_LIT>')<EOL><DEDENT>if self._show_pep8 and doc:<EOL><INDENT>print('<STR_LIT:U+0020>' + doc.strip())<EOL><DEDENT>sys.stdout.flush()<EOL><DEDENT>return self.file_errors<EOL>

Print the result and return the overall count for this file.


self.options.report = (reporter or self.options.reporter)(self.options)<EOL>return self.options.report<EOL>


if paths is None:<EOL><INDENT>paths = self.paths<EOL><DEDENT>report = self.options.report<EOL>runner = self.runner<EOL>report.start()<EOL>try:<EOL><INDENT>for path in paths:<EOL><INDENT>if os.path.isdir(path):<EOL><INDENT>self.input_dir(path)<EOL><DEDENT>elif not self.excluded(path):<EOL><INDENT>runner(path)<EOL><DEDENT><DEDENT><DEDENT>except KeyboardInterrupt:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT>report.stop()<EOL>return report<EOL>


def input_file(self, filename, lines=None, expected=None, line_offset=<NUM_LIT:0>):

if self.options.verbose:<EOL><INDENT>print('<STR_LIT>' % filename)<EOL><DEDENT>fchecker = self.checker_class(<EOL>filename, lines=lines, options=self.options)<EOL>return fchecker.check_all(expected=expected, line_offset=line_offset)<EOL>


dirname = dirname.rstrip('<STR_LIT:/>')<EOL>if self.excluded(dirname):<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>counters = self.options.report.counters<EOL>verbose = self.options.verbose<EOL>filepatterns = self.options.filename<EOL>runner = self.runner<EOL>for root, dirs, files in os.walk(dirname):<EOL><INDENT>if verbose:<EOL><INDENT>print('<STR_LIT>' + root)<EOL><DEDENT>counters['<STR_LIT>'] += <NUM_LIT:1><EOL>for subdir in sorted(dirs):<EOL><INDENT>if self.excluded(subdir, root):<EOL><INDENT>dirs.remove(subdir)<EOL><DEDENT><DEDENT>for filename in sorted(files):<EOL><INDENT>if ((filename_match(filename, filepatterns) and<EOL>not self.excluded(filename, root))):<EOL><INDENT>runner(os.path.join(root, filename))<EOL><DEDENT><DEDENT><DEDENT>

Check all files in this directory and all subdirectories.


def excluded(self, filename, parent=None):

if not self.options.exclude:<EOL><INDENT>return False<EOL><DEDENT>basename = os.path.basename(filename)<EOL>if filename_match(basename, self.options.exclude):<EOL><INDENT>return True<EOL><DEDENT>if parent:<EOL><INDENT>filename = os.path.join(parent, filename)<EOL><DEDENT>filename = os.path.abspath(filename)<EOL>return filename_match(filename, self.options.exclude)<EOL>

Check if the file should be excluded.

        Check if 'options.exclude' contains a pattern that matches filename.


if len(code) < <NUM_LIT:4> and any(s.startswith(code)<EOL>for s in self.options.select):<EOL><INDENT>return False<EOL><DEDENT>return (code.startswith(self.options.ignore) and<EOL>not code.startswith(self.options.select))<EOL>

Check if the error code should be ignored.

        If 'options.select' contains a prefix of the error code,
        return False.  Else, if 'options.ignore' contains a prefix of
        the error code, return True.


checks = []<EOL>for check, attrs in _checks[argument_name].items():<EOL><INDENT>(codes, args) = attrs<EOL>if any(not (code and self.ignore_code(code)) for code in codes):<EOL><INDENT>checks.append((check.__name__, check, args))<EOL><DEDENT><DEDENT>return sorted(checks)<EOL>

Get all the checks for this category.

        Find all globally visible functions where the first argument name
        starts with argument_name and which contain selected tests.


return self[iso3166_code]<EOL>


zone_name = tz_info.zone<EOL>utc_transition_times_list_raw = getattr(tz_info, <EOL>'<STR_LIT>', <EOL>None)<EOL>utc_transition_times_list = [tuple(utt.timetuple()) <EOL>for utt <EOL>in utc_transition_times_list_raw]if utc_transition_times_list_raw is not Noneelse None<EOL>transition_info_list_raw = getattr(tz_info, <EOL>'<STR_LIT>', <EOL>None)<EOL>transition_info_list = [(utcoffset_td.total_seconds(), <EOL>dst_td.total_seconds(), <EOL>tzname)<EOL>for (utcoffset_td, dst_td, tzname)<EOL>in transition_info_list_raw]if transition_info_list_raw is not Noneelse None<EOL>try:<EOL><INDENT>utcoffset_dt = tz_info._utcoffset<EOL><DEDENT>except AttributeError:<EOL><INDENT>utcoffset = None<EOL><DEDENT>else:<EOL><INDENT>utcoffset = utcoffset_dt.total_seconds()<EOL><DEDENT>tzname = getattr(tz_info, '<STR_LIT>', None)<EOL>parent_class_name = getmro(tz_info.__class__)[<NUM_LIT:1>].__name__<EOL>return cls(zone_name, parent_class_name, utc_transition_times_list, <EOL>transition_info_list, utcoffset, tzname)<EOL>

Create an instance using the result of the timezone() call in
        "pytz".


return s.encode('<STR_LIT>')<EOL>

Cast a string or byte string to an ASCII byte string.


return str(s.decode('<STR_LIT>'))<EOL>

Cast a string or byte string to an ASCII string.


if zone.upper() == '<STR_LIT>':<EOL><INDENT>return utc<EOL><DEDENT>try:<EOL><INDENT>zone = ascii(zone)<EOL><DEDENT>except UnicodeEncodeError:<EOL><INDENT>raise UnknownTimeZoneError(zone)<EOL><DEDENT>zone = _unmunge_zone(zone)<EOL>if zone not in _tzinfo_cache:<EOL><INDENT>if zone in all_timezones_set:<EOL><INDENT>fp = open_resource(zone)<EOL>try:<EOL>

r''' Return a datetime.tzinfo implementation for the given timezone

    >>> from datetime import datetime, timedelta
    >>> utc = timezone('UTC')
    >>> eastern = timezone('US/Eastern')
    >>> eastern.zone
    'US/Eastern'
    >>> timezone(unicode('US/Eastern')) is eastern
    True
    >>> utc_dt = datetime(2002, 10, 27, 6, 0, 0, tzinfo=utc)
    >>> loc_dt = utc_dt.astimezone(eastern)
    >>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'
    >>> loc_dt.strftime(fmt)
    '2002-10-27 01:00:00 EST (-0500)'
    >>> (loc_dt - timedelta(minutes=10)).strftime(fmt)
    '2002-10-27 00:50:00 EST (-0500)'
    >>> eastern.normalize(loc_dt - timedelta(minutes=10)).strftime(fmt)
    '2002-10-27 01:50:00 EDT (-0400)'
    >>> (loc_dt + timedelta(minutes=10)).strftime(fmt)
    '2002-10-27 01:10:00 EST (-0500)'

    Raises UnknownTimeZoneError if passed an unknown zone.

    >>> try:
    ...     timezone('Asia/Shangri-La')
    ... except UnknownTimeZoneError:
    ...     print('Unknown')
    Unknown

    >>> try:
    ...     timezone(unicode('\N{TRADE MARK SIGN}'))
    ... except UnknownTimeZoneError:
    ...     print('Unknown')
    Unknown


return zone.replace('<STR_LIT>', '<STR_LIT:+>').replace('<STR_LIT>', '<STR_LIT:->')<EOL>

Undo the time zone name munging done by older versions of pytz.


return utc<EOL>

Factory function for utc unpickling.

    Makes sure that unpickling a utc instance always returns the same
    module global.

    These examples belong in the UTC class above, but it is obscured; or in
    the README.txt, but we are not depending on Python 2.4 so integrating
    the README.txt examples with the unit tests is not trivial.

    >>> import datetime, pickle
    >>> dt = datetime.datetime(2005, 3, 1, 14, 13, 21, tzinfo=utc)
    >>> naive = dt.replace(tzinfo=None)
    >>> p = pickle.dumps(dt, 1)
    >>> naive_p = pickle.dumps(naive, 1)
    >>> len(p) - len(naive_p)
    17
    >>> new = pickle.loads(p)
    >>> new == dt
    True
    >>> new is dt
    False
    >>> new.tzinfo is dt.tzinfo
    True
    >>> utc is UTC is timezone('UTC')
    True
    >>> utc is timezone('GMT')
    False


return unpickler(*args)<EOL>

Factory function for unpickling pytz tzinfo instances.

    Just a wrapper around tzinfo.unpickler to save a few bytes in each pickle
    by shortening the path.


if offset == <NUM_LIT:0>:<EOL><INDENT>return UTC<EOL><DEDENT>info = _tzinfos.get(offset)<EOL>if info is None:<EOL><INDENT>info = _tzinfos.setdefault(offset, _FixedOffset(offset))<EOL><DEDENT>return info<EOL>

return a fixed-offset timezone based off a number of minutes.

        >>> one = FixedOffset(-330)
        >>> one
        pytz.FixedOffset(-330)
        >>> one.utcoffset(datetime.datetime.now())
        datetime.timedelta(-1, 66600)
        >>> one.dst(datetime.datetime.now())
        datetime.timedelta(0)

        >>> two = FixedOffset(1380)
        >>> two
        pytz.FixedOffset(1380)
        >>> two.utcoffset(datetime.datetime.now())
        datetime.timedelta(0, 82800)
        >>> two.dst(datetime.datetime.now())
        datetime.timedelta(0)

    The datetime.timedelta must be between the range of -1 and 1 day,
    non-inclusive.

        >>> FixedOffset(1440)
        Traceback (most recent call last):
        ...
        ValueError: ('absolute offset is too large', 1440)

        >>> FixedOffset(-1440)
        Traceback (most recent call last):
        ...
        ValueError: ('absolute offset is too large', -1440)

    An offset of 0 is special-cased to return UTC.

        >>> FixedOffset(0) is UTC
        True

    There should always be only one instance of a FixedOffset per timedelta.
    This should be true for multiple creation calls.

        >>> FixedOffset(-330) is one
        True
        >>> FixedOffset(1380) is two
        True

    It should also be true for pickling.

        >>> import pickle
        >>> pickle.loads(pickle.dumps(one)) is one
        True
        >>> pickle.loads(pickle.dumps(two)) is two
        True


if dt.tzinfo is not None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return dt.replace(tzinfo=self)<EOL>


if dt.tzinfo is self:<EOL><INDENT>return dt<EOL><DEDENT>if dt.tzinfo is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return dt.astimezone(self)<EOL>

Correct the timezone information on the given datetime


if dt.tzinfo is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return dt.replace(tzinfo=self)<EOL>


try:<EOL><INDENT>return _timedelta_cache[seconds]<EOL><DEDENT>except KeyError:<EOL><INDENT>delta = timedelta(seconds=seconds)<EOL>_timedelta_cache[seconds] = delta<EOL>return delta<EOL><DEDENT>

Create only one instance of each distinct timedelta


try:<EOL><INDENT>return _datetime_cache[seconds]<EOL><DEDENT>except KeyError:<EOL><INDENT>dt = _epoch + timedelta(seconds=seconds)<EOL>_datetime_cache[seconds] = dt<EOL>return dt<EOL><DEDENT>

Create only one instance of each distinct datetime


try:<EOL><INDENT>return _ttinfo_cache[args]<EOL><DEDENT>except KeyError:<EOL><INDENT>ttinfo = (<EOL>memorized_timedelta(args[<NUM_LIT:0>]),<EOL>memorized_timedelta(args[<NUM_LIT:1>]),<EOL>args[<NUM_LIT:2>]<EOL>)<EOL>_ttinfo_cache[args] = ttinfo<EOL>return ttinfo<EOL><DEDENT>

Create only one instance of each distinct tuple


return td.seconds + td.days * <NUM_LIT> * <NUM_LIT> * <NUM_LIT><EOL>


def unpickler(zone, utcoffset=None, dstoffset=None, tzname=None):

<EOL>tz = pytzpure.timezone(zone)<EOL>if utcoffset is None:<EOL><INDENT>return tz<EOL><DEDENT>utcoffset = memorized_timedelta(utcoffset)<EOL>dstoffset = memorized_timedelta(dstoffset)<EOL>try:<EOL><INDENT>return tz._tzinfos[(utcoffset, dstoffset, tzname)]<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>for localized_tz in tz._tzinfos.values():<EOL><INDENT>if (localized_tz._utcoffset == utcoffset<EOL>and localized_tz._dst == dstoffset):<EOL><INDENT>return localized_tz<EOL><DEDENT><DEDENT>inf = (utcoffset, dstoffset, tzname)<EOL>tz._tzinfos[inf] = tz.__class__(inf, tz._tzinfos)<EOL>return tz._tzinfos[inf]<EOL>

Factory function for unpickling pytz tzinfo instances.

    This is shared for both StaticTzInfo and DstTzInfo instances, because
    database changes could cause a zones implementation to switch between
    these two base classes and we can't break pickles on a pytz version
    upgrade.


if dt.tzinfo is not None and dt.tzinfo is not self:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return (dt + self._utcoffset).replace(tzinfo=self)<EOL>


return self._utcoffset<EOL>

See datetime.tzinfo.utcoffset

        is_dst is ignored for StaticTzInfo, and exists only to
        retain compatibility with DstTzInfo.


return _notime<EOL>

See datetime.tzinfo.dst

        is_dst is ignored for StaticTzInfo, and exists only to
        retain compatibility with DstTzInfo.


return self._tzname<EOL>

See datetime.tzinfo.tzname

        is_dst is ignored for StaticTzInfo, and exists only to
        retain compatibility with DstTzInfo.


Correct the timezone information on the given datetime.

        This is normally a no-op, as StaticTzInfo timezones never have
        ambiguous cases to correct:

        >>> from pytz import timezone
        >>> gmt = timezone('GMT')
        >>> isinstance(gmt, StaticTzInfo)
        True
        >>> dt = datetime(2011, 5, 8, 1, 2, 3, tzinfo=gmt)
        >>> gmt.normalize(dt) is dt
        True

        The supported method of converting between timezones is to use
        datetime.astimezone(). Currently normalize() also works:

        >>> la = timezone('America/Los_Angeles')
        >>> dt = la.localize(datetime(2011, 5, 7, 1, 2, 3))
        >>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'
        >>> gmt.normalize(dt).strftime(fmt)
        '2011-05-07 08:02:03 GMT (+0000)'


if (dt.tzinfo is not None<EOL>and getattr(dt.tzinfo, '<STR_LIT>', None) is not self._tzinfos):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>dt = dt.replace(tzinfo=None)<EOL>idx = max(<NUM_LIT:0>, bisect_right(self._utc_transition_times, dt) - <NUM_LIT:1>)<EOL>inf = self._transition_info[idx]<EOL>return (dt + inf[<NUM_LIT:0>]).replace(tzinfo=self._tzinfos[inf])<EOL>


if dt.tzinfo is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>offset = dt.tzinfo._utcoffset<EOL>dt = dt.replace(tzinfo=None)<EOL>dt = dt - offset<EOL>return self.fromutc(dt)<EOL>

Correct the timezone information on the given datetime

        If date arithmetic crosses DST boundaries, the tzinfo
        is not magically adjusted. This method normalizes the
        tzinfo to the correct one.

        To test, first we need to do some setup

        >>> from pytz import timezone
        >>> utc = timezone('UTC')
        >>> eastern = timezone('US/Eastern')
        >>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'

        We next create a datetime right on an end-of-DST transition point,
        the instant when the wallclocks are wound back one hour.

        >>> utc_dt = datetime(2002, 10, 27, 6, 0, 0, tzinfo=utc)
        >>> loc_dt = utc_dt.astimezone(eastern)
        >>> loc_dt.strftime(fmt)
        '2002-10-27 01:00:00 EST (-0500)'

        Now, if we subtract a few minutes from it, note that the timezone
        information has not changed.

        >>> before = loc_dt - timedelta(minutes=10)
        >>> before.strftime(fmt)
        '2002-10-27 00:50:00 EST (-0500)'

        But we can fix that by calling the normalize method

        >>> before = eastern.normalize(before)
        >>> before.strftime(fmt)
        '2002-10-27 01:50:00 EDT (-0400)'

        The supported method of converting between timezones is to use
        datetime.astimezone(). Currently, normalize() also works:

        >>> th = timezone('Asia/Bangkok')
        >>> am = timezone('Europe/Amsterdam')
        >>> dt = th.localize(datetime(2011, 5, 7, 1, 2, 3))
        >>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'
        >>> am.normalize(dt).strftime(fmt)
        '2011-05-06 20:02:03 CEST (+0200)'


if dt.tzinfo is not None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>possible_loc_dt = set()<EOL>for delta in [timedelta(days=-<NUM_LIT:1>), timedelta(days=<NUM_LIT:1>)]:<EOL><INDENT>loc_dt = dt + delta<EOL>idx = max(<NUM_LIT:0>, bisect_right(<EOL>self._utc_transition_times, loc_dt) - <NUM_LIT:1>)<EOL>inf = self._transition_info[idx]<EOL>tzinfo = self._tzinfos[inf]<EOL>loc_dt = tzinfo.normalize(dt.replace(tzinfo=tzinfo))<EOL>if loc_dt.replace(tzinfo=None) == dt:<EOL><INDENT>possible_loc_dt.add(loc_dt)<EOL><DEDENT><DEDENT>if len(possible_loc_dt) == <NUM_LIT:1>:<EOL><INDENT>return possible_loc_dt.pop()<EOL><DEDENT>if len(possible_loc_dt) == <NUM_LIT:0>:<EOL><INDENT>if is_dst is None:<EOL><INDENT>raise NonExistentTimeError(dt)<EOL><DEDENT>elif is_dst:<EOL><INDENT>return self.localize(<EOL>dt + timedelta(hours=<NUM_LIT:6>), is_dst=True) - timedelta(hours=<NUM_LIT:6>)<EOL><DEDENT>else:<EOL><INDENT>return self.localize(<EOL>dt - timedelta(hours=<NUM_LIT:6>), is_dst=False) + timedelta(hours=<NUM_LIT:6>)<EOL><DEDENT><DEDENT>if is_dst is None:<EOL><INDENT>raise AmbiguousTimeError(dt)<EOL><DEDENT>filtered_possible_loc_dt = [<EOL>p for p in possible_loc_dt<EOL>if bool(p.tzinfo._dst) == is_dst<EOL>]<EOL>if len(filtered_possible_loc_dt) == <NUM_LIT:1>:<EOL><INDENT>return filtered_possible_loc_dt[<NUM_LIT:0>]<EOL><DEDENT>if len(filtered_possible_loc_dt) == <NUM_LIT:0>:<EOL><INDENT>filtered_possible_loc_dt = list(possible_loc_dt)<EOL><DEDENT>sorting_keys = {}<EOL>for local_dt in filtered_possible_loc_dt:<EOL><INDENT>key = local_dt.replace(tzinfo=None) - local_dt.tzinfo._utcoffset<EOL>sorting_keys[key] = local_dt<EOL><DEDENT>first_key = sorted(sorting_keys)[<NUM_LIT:0>]<EOL>return sorting_keys[first_key]<EOL>

Convert naive time to local time.

        This method should be used to construct localtimes, rather
        than passing a tzinfo argument to a datetime constructor.

        is_dst is used to determine the correct timezone in the ambigous
        period at the end of daylight savings time.

        >>> from pytz import timezone
        >>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'
        >>> amdam = timezone('Europe/Amsterdam')
        >>> dt  = datetime(2004, 10, 31, 2, 0, 0)
        >>> loc_dt1 = amdam.localize(dt, is_dst=True)
        >>> loc_dt2 = amdam.localize(dt, is_dst=False)
        >>> loc_dt1.strftime(fmt)
        '2004-10-31 02:00:00 CEST (+0200)'
        >>> loc_dt2.strftime(fmt)
        '2004-10-31 02:00:00 CET (+0100)'
        >>> str(loc_dt2 - loc_dt1)
        '1:00:00'

        Use is_dst=None to raise an AmbiguousTimeError for ambiguous
        times at the end of daylight savings

        >>> try:
        ...     loc_dt1 = amdam.localize(dt, is_dst=None)
        ... except AmbiguousTimeError:
        ...     print('Ambiguous')
        Ambiguous

        is_dst defaults to False

        >>> amdam.localize(dt) == amdam.localize(dt, False)
        True

        is_dst is also used to determine the correct timezone in the
        wallclock times jumped over at the start of daylight savings time.

        >>> pacific = timezone('US/Pacific')
        >>> dt = datetime(2008, 3, 9, 2, 0, 0)
        >>> ploc_dt1 = pacific.localize(dt, is_dst=True)
        >>> ploc_dt2 = pacific.localize(dt, is_dst=False)
        >>> ploc_dt1.strftime(fmt)
        '2008-03-09 02:00:00 PDT (-0700)'
        >>> ploc_dt2.strftime(fmt)
        '2008-03-09 02:00:00 PST (-0800)'
        >>> str(ploc_dt2 - ploc_dt1)
        '1:00:00'

        Use is_dst=None to raise a NonExistentTimeError for these skipped
        times.

        >>> try:
        ...     loc_dt1 = pacific.localize(dt, is_dst=None)
        ... except NonExistentTimeError:
        ...     print('Non-existent')
        Non-existent


if dt is None:<EOL><INDENT>return None<EOL><DEDENT>elif dt.tzinfo is not self:<EOL><INDENT>dt = self.localize(dt, is_dst)<EOL>return dt.tzinfo._utcoffset<EOL><DEDENT>else:<EOL><INDENT>return self._utcoffset<EOL><DEDENT>

See datetime.tzinfo.utcoffset

        The is_dst parameter may be used to remove ambiguity during DST
        transitions.

        >>> from pytz import timezone
        >>> tz = timezone('America/St_Johns')
        >>> ambiguous = datetime(2009, 10, 31, 23, 30)

        >>> tz.utcoffset(ambiguous, is_dst=False)
        datetime.timedelta(-1, 73800)

        >>> tz.utcoffset(ambiguous, is_dst=True)
        datetime.timedelta(-1, 77400)

        >>> try:
        ...     tz.utcoffset(ambiguous)
        ... except AmbiguousTimeError:
        ...     print('Ambiguous')
        Ambiguous


if dt is None:<EOL><INDENT>return None<EOL><DEDENT>elif dt.tzinfo is not self:<EOL><INDENT>dt = self.localize(dt, is_dst)<EOL>return dt.tzinfo._dst<EOL><DEDENT>else:<EOL><INDENT>return self._dst<EOL><DEDENT>

See datetime.tzinfo.dst

        The is_dst parameter may be used to remove ambiguity during DST
        transitions.

        >>> from pytz import timezone
        >>> tz = timezone('America/St_Johns')

        >>> normal = datetime(2009, 9, 1)

        >>> tz.dst(normal)
        datetime.timedelta(0, 3600)
        >>> tz.dst(normal, is_dst=False)
        datetime.timedelta(0, 3600)
        >>> tz.dst(normal, is_dst=True)
        datetime.timedelta(0, 3600)

        >>> ambiguous = datetime(2009, 10, 31, 23, 30)

        >>> tz.dst(ambiguous, is_dst=False)
        datetime.timedelta(0)
        >>> tz.dst(ambiguous, is_dst=True)
        datetime.timedelta(0, 3600)
        >>> try:
        ...     tz.dst(ambiguous)
        ... except AmbiguousTimeError:
        ...     print('Ambiguous')
        Ambiguous


if dt is None:<EOL><INDENT>return self.zone<EOL><DEDENT>elif dt.tzinfo is not self:<EOL><INDENT>dt = self.localize(dt, is_dst)<EOL>return dt.tzinfo._tzname<EOL><DEDENT>else:<EOL><INDENT>return self._tzname<EOL><DEDENT>

See datetime.tzinfo.tzname

        The is_dst parameter may be used to remove ambiguity during DST
        transitions.

        >>> from pytz import timezone
        >>> tz = timezone('America/St_Johns')

        >>> normal = datetime(2009, 9, 1)

        >>> tz.tzname(normal)
        'NDT'
        >>> tz.tzname(normal, is_dst=False)
        'NDT'
        >>> tz.tzname(normal, is_dst=True)
        'NDT'

        >>> ambiguous = datetime(2009, 10, 31, 23, 30)

        >>> tz.tzname(ambiguous, is_dst=False)
        'NST'
        >>> tz.tzname(ambiguous, is_dst=True)
        'NDT'
        >>> try:
        ...     tz.tzname(ambiguous)
        ... except AmbiguousTimeError:
        ...     print('Ambiguous')
        Ambiguous


pos = param.find('<STR_LIT:->')<EOL>lower, upper = (None, None)<EOL>if pos == -<NUM_LIT:1>:<EOL><INDENT>lower, upper = (param, param)<EOL><DEDENT>else:<EOL><INDENT>lower, upper = param.split('<STR_LIT:->')<EOL><DEDENT>ret = (expand_date_param(lower, '<STR_LIT>'), expand_date_param(upper, '<STR_LIT>'))<EOL>return ret<EOL>


year = datetime.MINYEAR<EOL>month = <NUM_LIT:1><EOL>day = <NUM_LIT:1><EOL>hour = <NUM_LIT:0><EOL>minute = <NUM_LIT:0><EOL>second = <NUM_LIT:0><EOL>if lower_upper == '<STR_LIT>':<EOL><INDENT>year = datetime.MAXYEAR<EOL>month = <NUM_LIT:12><EOL>day = <NUM_LIT><EOL>hour = <NUM_LIT><EOL>minute = <NUM_LIT><EOL>second = <NUM_LIT><EOL><DEDENT>if len(param) == <NUM_LIT:0>:<EOL><INDENT>pass<EOL><DEDENT>elif len(param) == <NUM_LIT:4>:<EOL><INDENT>year = int(param)<EOL>if lower_upper == '<STR_LIT>':<EOL><INDENT>month = <NUM_LIT:1><EOL>day = <NUM_LIT:1><EOL>hour = <NUM_LIT:0><EOL>minute = <NUM_LIT:0><EOL>second = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>month = <NUM_LIT:12><EOL>day = <NUM_LIT><EOL>hour = <NUM_LIT><EOL>minute = <NUM_LIT><EOL>second = <NUM_LIT><EOL><DEDENT><DEDENT>elif len(param) == <NUM_LIT:6>:<EOL><INDENT>year = int(param[<NUM_LIT:0>:<NUM_LIT:4>])<EOL>month = int(param[<NUM_LIT:4>:<NUM_LIT:6>])<EOL>if lower_upper == '<STR_LIT>':<EOL><INDENT>day = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>(firstday, dayspermonth) = monthrange(year, month)<EOL>day = dayspermonth<EOL><DEDENT><DEDENT>elif len(param) == <NUM_LIT:8>:<EOL><INDENT>year = int(param[<NUM_LIT:0>:<NUM_LIT:4>])<EOL>month = int(param[<NUM_LIT:4>:<NUM_LIT:6>])<EOL>day = int(param[<NUM_LIT:6>:<NUM_LIT:8>])<EOL><DEDENT>elif len(param) == <NUM_LIT:10>:<EOL><INDENT>year = int(param[<NUM_LIT:0>:<NUM_LIT:4>])<EOL>month = int(param[<NUM_LIT:4>:<NUM_LIT:6>])<EOL>day = int(param[<NUM_LIT:6>:<NUM_LIT:8>])<EOL>hour = int(param[<NUM_LIT:8>:<NUM_LIT:10>])<EOL><DEDENT>elif len(param) == <NUM_LIT:12>:<EOL><INDENT>year = int(param[<NUM_LIT:0>:<NUM_LIT:4>])<EOL>month = int(param[<NUM_LIT:4>:<NUM_LIT:6>])<EOL>day = int(param[<NUM_LIT:6>:<NUM_LIT:8>])<EOL>hour = int(param[<NUM_LIT:8>:<NUM_LIT:10>])<EOL>minute = int(param[<NUM_LIT:10>:<NUM_LIT:12>])<EOL><DEDENT>elif len(param) == <NUM_LIT>:<EOL><INDENT>year = int(param[<NUM_LIT:0>:<NUM_LIT:4>])<EOL>month = int(param[<NUM_LIT:4>:<NUM_LIT:6>])<EOL>day = int(param[<NUM_LIT:6>:<NUM_LIT:8>])<EOL>hour = int(param[<NUM_LIT:8>:<NUM_LIT:10>])<EOL>minute = int(param[<NUM_LIT:10>:<NUM_LIT:12>])<EOL>second = int(param[<NUM_LIT:12>:<NUM_LIT>])<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>year = min(datetime.MAXYEAR, max(datetime.MINYEAR, year))<EOL>return datetime.datetime(year=year, month=month, day=day,<EOL>hour=hour, minute=minute, second=second)<EOL>

Expands a (possibly) incomplete date string to either the lowest
or highest possible contained date and returns
datetime.datetime for that string.

0753 (lower) => 0753-01-01
2012 (upper) => 2012-12-31
2012 (lower) => 2012-01-01
201208 (upper) => 2012-08-31
etc.


def alpha_(xmin,x=x):<EOL><INDENT>"""<STR_LIT>"""<EOL>gexmin = x>=xmin<EOL>n = np.count_nonzero(gexmin)<EOL>if n < <NUM_LIT:2>:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>x = x[gexmin]<EOL>a = <NUM_LIT:1> + float(n) / sum(log(x/xmin))<EOL>return a<EOL><DEDENT>return alpha_<EOL>

Create a mappable function alpha to apply to each xmin in a list of xmins.
    This is essentially the slow version of fplfit/cplfit, though I bet it could
    be speeded up with a clever use of parellel_map.  Not intended to be used by users.

    Docstring for the generated alpha function::

        Given a sorted data set and a minimum, returns power law MLE fit
        data is passed as a keyword parameter so that it can be vectorized

        If there is only one element, return alpha=0


return (alpha-<NUM_LIT:1.>) / n**<NUM_LIT:0.5><EOL>

Clauset et al 2007 equation 3.2:
    sigma = (alpha-1)/sqrt(n)


n = len(x)<EOL>btop = n * (log(x)*log(y)).sum() - (log(x)).sum()*(log(y)).sum()<EOL>bbottom = n*(log(x)**<NUM_LIT:2>).sum() - (log(x).sum())**<NUM_LIT:2><EOL>b = btop / bbottom<EOL>a = ( log(y).sum() - b * log(x).sum() ) / n<EOL>A = exp(a)<EOL>return A,b<EOL>

Returns A and B in y=Ax^B
http://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html


def plexp_cdf(x,xmin=<NUM_LIT:1>,alpha=<NUM_LIT>, pl_only=False, exp_only=False):

x = np.array(x)<EOL>C = <NUM_LIT:1>/(-xmin/(<NUM_LIT:1> - alpha) - xmin/alpha + exp(alpha)*xmin/alpha)<EOL>Ppl = lambda X: <NUM_LIT:1>+C*(xmin/(<NUM_LIT:1>-alpha)*(X/xmin)**(<NUM_LIT:1>-alpha))<EOL>Pexp = lambda X: C*xmin/alpha*exp(alpha)-C*(xmin/alpha)*exp(-alpha*(X/xmin-<NUM_LIT:1>))<EOL>if exp_only:<EOL><INDENT>return Pexp(x)<EOL><DEDENT>elif pl_only:<EOL><INDENT>return Ppl(x)<EOL><DEDENT>d=Ppl(x)<EOL>d[x<xmin]=Pexp(x)[x<xmin]<EOL>return d<EOL>

CDF(x) for the piecewise distribution exponential x<xmin, powerlaw x>=xmin
This is the CDF version of the distributions drawn in fig 3.4a of Clauset et al.
The constant "C" normalizes the PDF


def plexp_inv(P, xmin, alpha, guess=<NUM_LIT:1.>):

def equation(x,prob):<EOL><INDENT>return plexp_cdf(x, xmin, alpha)-prob<EOL><DEDENT>def solver(y, x0=guess):<EOL><INDENT>return scipy.optimize.fsolve(equation, guess, args=(y,))<EOL><DEDENT>f = np.vectorize(solver)<EOL>return f(P)<EOL>

Inverse CDF for a piecewise PDF as defined in eqn. 3.10
of Clauset et al.

(previous version was incorrect and lead to weird discontinuities in the
distribution function)


x = (<NUM_LIT:1>-P)**(<NUM_LIT:1>/(<NUM_LIT:1>-a)) * xm<EOL>return x<EOL>


def discrete_likelihood(data, xmin, alpha):

if not scipyOK:<EOL><INDENT>raise ImportError("<STR_LIT>")<EOL><DEDENT>from scipy.special import zeta as zeta<EOL>zz = data[data>=xmin]<EOL>nn = len(zz)<EOL>sum_log_data = np.log(zz).sum()<EOL>zeta = zeta(alpha, xmin)<EOL>L_of_alpha = -<NUM_LIT:1>*nn*log(zeta) - alpha * sum_log_data<EOL>return L_of_alpha<EOL>

Equation B.8 in Clauset

Given a data set, an xmin value, and an alpha "scaling parameter", computes
the log-likelihood (the value to be maximized)


def discrete_likelihood_vector(data, xmin, alpharange=(<NUM_LIT>,<NUM_LIT>), n_alpha=<NUM_LIT>):

from scipy.special import zeta as zeta<EOL>zz = data[data>=xmin]<EOL>nn = len(zz)<EOL>alpha_vector = np.linspace(alpharange[<NUM_LIT:0>],alpharange[<NUM_LIT:1>],n_alpha)<EOL>sum_log_data = np.log(zz).sum()<EOL>zeta_vector = zeta(alpha_vector, xmin)<EOL>L_of_alpha = -<NUM_LIT:1>*nn*log(zeta_vector) - alpha_vector * sum_log_data<EOL>return L_of_alpha<EOL>

Compute the likelihood for all "scaling parameters" in the range (alpharange)
for a given xmin.  This is only part of the discrete value likelihood
maximization problem as described in Clauset et al
(Equation B.8)

*alpharange* [ 2-tuple ]
    Two floats specifying the upper and lower limits of the power law alpha to test


def discrete_max_likelihood_arg(data, xmin, alpharange=(<NUM_LIT>,<NUM_LIT>), n_alpha=<NUM_LIT>):

likelihoods = discrete_likelihood_vector(data, xmin, alpharange=alpharange, n_alpha=n_alpha)<EOL>Largmax = np.argmax(likelihoods)<EOL>return Largmax<EOL>

Returns the *argument* of the max of the likelihood of the data given an input xmin


def discrete_max_likelihood(data, xmin, alpharange=(<NUM_LIT>,<NUM_LIT>), n_alpha=<NUM_LIT>):

likelihoods = discrete_likelihood_vector(data, xmin, alpharange=alpharange, n_alpha=n_alpha)<EOL>Lmax = np.max(likelihoods)<EOL>return Lmax<EOL>


def most_likely_alpha(data, xmin, alpharange=(<NUM_LIT>,<NUM_LIT>), n_alpha=<NUM_LIT>):

alpha_vector = np.linspace(alpharange[<NUM_LIT:0>],alpharange[<NUM_LIT:1>],n_alpha)<EOL>return alpha_vector[discrete_max_likelihood_arg(data, xmin,<EOL>alpharange=alpharange,<EOL>n_alpha=n_alpha)]<EOL>

Return the most likely alpha for the data given an xmin


<EOL>gexmin = (data>=xmin)<EOL>nn = gexmin.sum()<EOL>if nn < <NUM_LIT:2>:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>xx = data[gexmin]<EOL>alpha = <NUM_LIT:1.0> + float(nn) * (sum(log(xx/(float(xmin)-<NUM_LIT:0.5>))))**-<NUM_LIT:1><EOL>return alpha<EOL>

Equation B.17 of Clauset et al 2009

The Maximum Likelihood Estimator of the "scaling parameter" alpha in the
discrete case is similar to that in the continuous case


def discrete_best_alpha(data, alpharangemults=(<NUM_LIT>,<NUM_LIT>), n_alpha=<NUM_LIT>, approximate=True, verbose=True):

xmins = np.unique(data)<EOL>if approximate:<EOL><INDENT>alpha_of_xmin = [ discrete_alpha_mle(data,xmin) for xmin in xmins ]<EOL><DEDENT>else:<EOL><INDENT>alpha_approx = [ discrete_alpha_mle(data,xmin) for xmin in xmins ]<EOL>alpharanges = [(<NUM_LIT>*a,<NUM_LIT>*a) for a in alpha_approx]<EOL>alpha_of_xmin = [ most_likely_alpha(data,xmin,alpharange=ar,n_alpha=n_alpha) for xmin,ar in zip(xmins,alpharanges) ]<EOL><DEDENT>ksvalues = [ discrete_ksD(data, xmin, alpha) for xmin,alpha in zip(xmins,alpha_of_xmin) ]<EOL>best_index = argmin(ksvalues)<EOL>best_alpha = alpha_of_xmin[best_index]<EOL>best_xmin = xmins[best_index]<EOL>best_ks = ksvalues[best_index]<EOL>best_likelihood = discrete_likelihood(data, best_xmin, best_alpha)<EOL>if verbose:<EOL><INDENT>print("<STR_LIT>" % (<EOL>best_alpha, best_xmin, best_ks, best_likelihood,<EOL>(data<best_xmin).sum(), (data>=best_xmin).sum()))<EOL><DEDENT>return best_alpha,best_xmin,best_ks,best_likelihood<EOL>

Use the maximum L to determine the most likely value of alpha

*alpharangemults* [ 2-tuple ]
    Pair of values indicating multiplicative factors above and below the
    approximate alpha from the MLE alpha to use when determining the
    "exact" alpha (by directly maximizing the likelihood function)


zz = np.sort(data[data>=xmin])<EOL>nn = float(len(zz))<EOL>if nn < <NUM_LIT:2>:<EOL><INDENT>return np.inf<EOL><DEDENT>model_cdf = <NUM_LIT:1.0>-(zz.astype('<STR_LIT:float>')/float(xmin))**(<NUM_LIT:1.0>-alpha)<EOL>data_cdf = np.searchsorted(zz,zz,side='<STR_LIT:left>')/(float(nn))<EOL>ks = max(abs(model_cdf-data_cdf))<EOL>return ks<EOL>

given a sorted data set, a minimum, and an alpha, returns the power law ks-test
D value w/data

The returned value is the "D" parameter in the ks test

(this is implemented differently from the continuous version because there
are potentially multiple identical points that need comparison to the power
law)


x = np.array(x) <EOL>if (x<<NUM_LIT:0>).sum() > <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>" % ((x<<NUM_LIT:0>).sum()))<EOL>x = x[x><NUM_LIT:0>]<EOL><DEDENT>self.data = x<EOL>self.plfit(**kwargs)<EOL>

Initializes and fits the power law.  Can pass "quiet" to turn off
output (except for warnings; "silent" turns off warnings)


def plfit(self, nosmall=True, finite=False, quiet=False, silent=False,<EOL>usefortran=False, usecy=False, xmin=None, verbose=False,<EOL>discrete=None, discrete_approx=True, discrete_n_alpha=<NUM_LIT:1000>,<EOL>skip_consistency_check=False):

x = self.data<EOL>if any(x < <NUM_LIT:0>):<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>z = np.sort(x)<EOL>xmins,argxmins = np.unique(z,return_index=True)<EOL>self._nunique = len(xmins)<EOL>if self._nunique == len(x) and discrete is None:<EOL><INDENT>if verbose:<EOL><INDENT>print("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>discrete = False<EOL><DEDENT>elif self._nunique < len(x) and discrete is None:<EOL><INDENT>if verbose:<EOL><INDENT>print("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>discrete = True<EOL><DEDENT>t = time.time()<EOL>if xmin is None:<EOL><INDENT>if discrete:<EOL><INDENT>self.discrete_best_alpha(approximate=discrete_approx,<EOL>n_alpha=discrete_n_alpha,<EOL>verbose=verbose,<EOL>finite=finite)<EOL>return self._xmin,self._alpha<EOL><DEDENT>elif usefortran and fortranOK:<EOL><INDENT>kstest_values,alpha_values = fplfit.plfit(z, <NUM_LIT:0>)<EOL>if not quiet:<EOL><INDENT>print(("<STR_LIT>" % (time.time()-t)))<EOL><DEDENT><DEDENT>elif usecy and cyOK:<EOL><INDENT>kstest_values,alpha_values = cplfit.plfit_loop(z,<EOL>nosmall=False,<EOL>zunique=xmins,<EOL>argunique=argxmins)<EOL>if not quiet:<EOL><INDENT>print(("<STR_LIT>" % (time.time()-t)))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>f_alpha = alpha_gen(z)<EOL>f_kstest = kstest_gen(z)<EOL>alpha_values = np.asarray(list(map(f_alpha,xmins)),<EOL>dtype='<STR_LIT:float>')<EOL>kstest_values = np.asarray(list(map(f_kstest,xmins)),<EOL>dtype='<STR_LIT:float>')<EOL>if not quiet:<EOL><INDENT>print(("<STR_LIT>" % (time.time()-t)))<EOL><DEDENT><DEDENT>if not quiet:<EOL><INDENT>if usefortran and not fortranOK:<EOL><INDENT>raise ImportError("<STR_LIT>")<EOL><DEDENT>if usecy and not cyOK:<EOL><INDENT>raise ImportError("<STR_LIT>")<EOL><DEDENT><DEDENT>sigma = (alpha_values-<NUM_LIT:1>)/np.sqrt(len(z)-argxmins)<EOL>if nosmall:<EOL><INDENT>goodvals = sigma<<NUM_LIT:0.1><EOL>nmax = argmin(goodvals)<EOL>if nmax <= <NUM_LIT:0>:<EOL><INDENT>nmax = len(xmins) - <NUM_LIT:1><EOL>if not silent:<EOL><INDENT>print("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>nmax = len(xmins)-<NUM_LIT:1><EOL><DEDENT>best_ks_index = argmin(kstest_values[:nmax])<EOL>xmin = xmins[best_ks_index]<EOL>self._alpha_values = alpha_values<EOL>self._xmin_kstest = kstest_values<EOL>if scipyOK:<EOL><INDENT>self._ks_prob_all = np.array([scipy.stats.ksone.sf(D_stat,<EOL>len(kstest_values)-ii)<EOL>for ii,D_stat in<EOL>enumerate(kstest_values)])<EOL><DEDENT>self._sigma = sigma<EOL>n = np.count_nonzero(z>=xmin)<EOL>alpha = <NUM_LIT:1.> + float(n)/sum(log(z[z>=xmin]/xmin))<EOL>try:<EOL><INDENT>if not skip_consistency_check:<EOL><INDENT>np.testing.assert_almost_equal(alpha,<EOL>alpha_values[best_ks_index],<EOL>decimal=<NUM_LIT:4>)<EOL><DEDENT><DEDENT>except AssertionError:<EOL><INDENT>raise AssertionError("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>.format(alpha,<EOL>alpha_values[best_ks_index]))<EOL><DEDENT><DEDENT>z = z[z>=xmin]<EOL>n = len(z)<EOL>alpha = <NUM_LIT:1.> + float(n) / sum(log(z/xmin))<EOL>if finite:<EOL><INDENT>alpha = alpha*(n-<NUM_LIT:1.>)/n+<NUM_LIT:1.>/n<EOL><DEDENT>if n < <NUM_LIT:50> and not finite and not silent:<EOL><INDENT>print(('<STR_LIT>' % n))<EOL><DEDENT>ks = max(abs( np.arange(n)/float(n) - (<NUM_LIT:1>-(xmin/z)**(alpha-<NUM_LIT:1>)) ))<EOL>L = n*log((alpha-<NUM_LIT:1>)/xmin) - alpha*sum(log(z/xmin))<EOL>self._likelihood = L<EOL>self._xmin = xmin<EOL>self._xmins = xmins<EOL>self._alpha= alpha<EOL>self._alphaerr = (alpha-<NUM_LIT:1>)/np.sqrt(n)<EOL>self._ks = ks<EOL>if scipyOK:<EOL><INDENT>self._ks_prob = scipy.stats.ksone.sf(ks, n)<EOL><DEDENT>self._ngtx = n<EOL>if n == <NUM_LIT:1>:<EOL><INDENT>if not silent:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>self._alpha = alpha = <NUM_LIT:0><EOL>self._alphaerr = <NUM_LIT:0><EOL>self._likelihood = L = <NUM_LIT:0><EOL>self._ks = <NUM_LIT:0><EOL>self._ks_prob = <NUM_LIT:0><EOL>self._xmin = xmin<EOL>return xmin,<NUM_LIT:0><EOL><DEDENT>if np.isnan(L) or np.isnan(xmin) or np.isnan(alpha):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not quiet:<EOL><INDENT>if verbose: print("<STR_LIT>", end='<STR_LIT:U+0020>')<EOL>print("<STR_LIT>" % xmin, end='<STR_LIT:U+0020>')<EOL>if verbose: print("<STR_LIT>", end='<STR_LIT:U+0020>')<EOL>print("<STR_LIT>" % n, end='<STR_LIT:U+0020>')<EOL>if verbose: print("<STR_LIT>", end='<STR_LIT:U+0020>')<EOL>print("<STR_LIT>" % (alpha,self._alphaerr), end='<STR_LIT:U+0020>')<EOL>if verbose: print("<STR_LIT>", end='<STR_LIT:U+0020>')<EOL>print("<STR_LIT>" % L, end='<STR_LIT:U+0020>')<EOL>if verbose: print("<STR_LIT>", end='<STR_LIT:U+0020>')<EOL>print("<STR_LIT>" % (ks), end='<STR_LIT:U+0020>')<EOL>if scipyOK:<EOL><INDENT>if verbose: print("<STR_LIT>", end='<STR_LIT:U+0020>')<EOL>print("<STR_LIT>" % (self._ks_prob))<EOL><DEDENT>else:<EOL><INDENT>print()<EOL><DEDENT><DEDENT>return xmin,alpha<EOL>

A Python implementation of the Matlab code
http://www.santafe.edu/~aaronc/powerlaws/plfit.m
from http://www.santafe.edu/~aaronc/powerlaws/

See A. Clauset, C.R. Shalizi, and M.E.J. Newman, "Power-law distributions
in empirical data" SIAM Review, 51, 661-703 (2009). (arXiv:0706.1062)
http://arxiv.org/abs/0706.1062

There are 3 implementations of xmin estimation.  The fortran version is
fastest, the C (cython) version is ~10% slower, and the python version
is ~3x slower than the fortran version.  Also, the cython code suffers
~2% numerical error relative to the fortran and python for unknown
reasons.

There is also a discrete version implemented in python - it is
different from the continous version!

Parameters
----------
discrete : bool or None
    If *discrete* is None, the code will try to determine whether the
    data set is discrete or continous based on the uniqueness of the
    data; if your data set is continuous but you have any non-unique
    data points (e.g., flagged "bad" data), the "automatic"
    determination will fail.  If *discrete* is True or False, the
    discrete or continuous fitter will be used, respectively.
xmin : float or int
    If you specify xmin, the fitter will only determine alpha assuming
    the given xmin; the rest of the code (and most of the complexity)
    is determining an estimate for xmin and alpha.
nosmall : bool
    When on, the code rejects low s/n points.  WARNING: This option,
    which is on by default, may result in different answers than the
    original Matlab code and the "powerlaw" python package
finite : bool
    There is a 'finite-size bias' to the estimator.  The "alpha" the
    code measures is "alpha-hat" s.t. ᾶ = (nα-1)/(n-1), or α = (1 + ᾶ
    (n-1)) / n
quiet : bool
    If False, delivers messages about what fitter is used and the fit
    results
verbose : bool
    Deliver descriptive messages about the fit parameters (only if
    `quiet==False`)
silent : bool
    If True, will print NO messages
skip_consistency_check : bool
    The code will normally perform a consistency check to make sure the
    alpha value computed by the fitter matches the alpha value computed
    directly in python.  It is possible for numerical differences to
    creep in, usually at the 10^-6 or less level.  If you see an
    exception reporting this type of error, skipping the check can be
    the appropriate next step.

Returns
-------
(xmin, alpha)
The best-fit xmin and alpha values


def discrete_best_alpha(self, alpharangemults=(<NUM_LIT>,<NUM_LIT>), n_alpha=<NUM_LIT>,<EOL>approximate=True, verbose=True, finite=True):

data = self.data<EOL>self._xmins = xmins = np.unique(data)<EOL>if approximate:<EOL><INDENT>alpha_of_xmin = [ discrete_alpha_mle(data,xmin) for xmin in xmins ]<EOL><DEDENT>else:<EOL><INDENT>alpha_approx = [ discrete_alpha_mle(data,xmin) for xmin in xmins ]<EOL>alpharanges = [(<NUM_LIT>*a,<NUM_LIT>*a) for a in alpha_approx]<EOL>alpha_of_xmin = [ most_likely_alpha(data,xmin,alpharange=ar,n_alpha=n_alpha)<EOL>for xmin,ar in zip(xmins,alpharanges) ]<EOL><DEDENT>ksvalues = np.array([discrete_ksD(data, xmin, alpha)<EOL>for xmin,alpha in zip(xmins,alpha_of_xmin)<EOL>])<EOL>self._alpha_values = np.array(alpha_of_xmin)<EOL>self._xmin_kstest = ksvalues<EOL>ksvalues[np.isnan(ksvalues)] = np.inf<EOL>best_index = argmin(ksvalues)<EOL>self._alpha = best_alpha = alpha_of_xmin[best_index]<EOL>self._xmin = best_xmin = xmins[best_index]<EOL>self._ks = best_ks = ksvalues[best_index]<EOL>self._likelihood = best_likelihood = discrete_likelihood(data, best_xmin, best_alpha)<EOL>if finite:<EOL><INDENT>self._alpha = self._alpha*(n-<NUM_LIT:1.>)/n+<NUM_LIT:1.>/n<EOL><DEDENT>if verbose:<EOL><INDENT>print("<STR_LIT>" % (<EOL>best_alpha, best_xmin, best_ks, best_likelihood,<EOL>(data<best_xmin).sum(), (data>=best_xmin).sum()))<EOL><DEDENT>self._ngtx = n = (self.data>=self._xmin).sum()<EOL>self._alphaerr = (self._alpha-<NUM_LIT:1.0>)/np.sqrt(n)<EOL>if scipyOK:<EOL><INDENT>self._ks_prob = scipy.stats.ksone.sf(self._ks, n)<EOL><DEDENT>return best_alpha,best_xmin,best_ks,best_likelihood<EOL>

Use the maximum likelihood to determine the most likely value of alpha

*alpharangemults* [ 2-tuple ]
    Pair of values indicating multiplicative factors above and below the
    approximate alpha from the MLE alpha to use when determining the
    "exact" alpha (by directly maximizing the likelihood function)
*n_alpha* [ int ]
    Number of alpha values to use when measuring.  Larger number is more accurate.
*approximate* [ bool ]
    If False, try to "zoom-in" around the MLE alpha and get the exact
    best alpha value within some range around the approximate best
*vebose* [ bool ]
*finite* [ bool ]
    Correction for finite data?


Plot xmin versus the ks value for derived alpha.  This plot can be used
as a diagnostic of whether you have derived the 'best' fit: if there are
multiple local minima, your data set may be well suited to a broken
powerlaw or a different function.


Plot alpha versus the ks value for derived alpha.  This plot can be used
as a diagnostic of whether you have derived the 'best' fit: if there are
multiple local minima, your data set may be well suited to a broken
powerlaw or a different function.