def find_try_except_wrapper_node(<EOL>node: astroid.node_classes.NodeNG<EOL>) -> Union[astroid.ExceptHandler, astroid.TryExcept]:

current = node<EOL>ignores = (astroid.ExceptHandler, astroid.TryExcept)<EOL>while current and not isinstance(current.parent, ignores):<EOL><INDENT>current = current.parent<EOL><DEDENT>if current and isinstance(current.parent, ignores):<EOL><INDENT>return current.parent<EOL><DEDENT>return None<EOL>

Return the ExceptHandler or the TryExcept node in which the node is.


def is_from_fallback_block(node: astroid.node_classes.NodeNG) -> bool:

context = find_try_except_wrapper_node(node)<EOL>if not context:<EOL><INDENT>return False<EOL><DEDENT>if isinstance(context, astroid.ExceptHandler):<EOL><INDENT>other_body = context.parent.body<EOL>handlers = context.parent.handlers<EOL><DEDENT>else:<EOL><INDENT>other_body = itertools.chain.from_iterable(<EOL>handler.body for handler in context.handlers<EOL>)<EOL>handlers = context.handlers<EOL><DEDENT>has_fallback_imports = any(<EOL>isinstance(import_node, (astroid.ImportFrom, astroid.Import))<EOL>for import_node in other_body<EOL>)<EOL>ignores_import_error = _except_handlers_ignores_exception(handlers, ImportError)<EOL>return ignores_import_error or has_fallback_imports<EOL>

Check if the given node is from a fallback import block.


def get_exception_handlers(<EOL>node: astroid.node_classes.NodeNG, exception=Exception<EOL>) -> List[astroid.ExceptHandler]:

context = find_try_except_wrapper_node(node)<EOL>if isinstance(context, astroid.TryExcept):<EOL><INDENT>return [<EOL>handler for handler in context.handlers if error_of_type(handler, exception)<EOL>]<EOL><DEDENT>return None<EOL>

Return the collections of handlers handling the exception in arguments.

    Args:
        node (astroid.NodeNG): A node that is potentially wrapped in a try except.
        exception (builtin.Exception or str): exception or name of the exception.

    Returns:
        list: the collection of handlers that are handling the exception or None.


def is_node_inside_try_except(node: astroid.Raise) -> bool:

context = find_try_except_wrapper_node(node)<EOL>return isinstance(context, astroid.TryExcept)<EOL>

Check if the node is directly under a Try/Except statement.
    (but not under an ExceptHandler!)

    Args:
        node (astroid.Raise): the node raising the exception.

    Returns:
        bool: True if the node is inside a try/except statement, False otherwise.


def node_ignores_exception(<EOL>node: astroid.node_classes.NodeNG, exception=Exception<EOL>) -> bool:

managing_handlers = get_exception_handlers(node, exception)<EOL>if not managing_handlers:<EOL><INDENT>return False<EOL><DEDENT>return any(managing_handlers)<EOL>

Check if the node is in a TryExcept which handles the given exception.

    If the exception is not given, the function is going to look for bare
    excepts.


def class_is_abstract(node: astroid.ClassDef) -> bool:

for method in node.methods():<EOL><INDENT>if method.parent.frame() is node:<EOL><INDENT>if method.is_abstract(pass_is_abstract=False):<EOL><INDENT>return True<EOL><DEDENT><DEDENT><DEDENT>return False<EOL>

return true if the given class node should be considered as an abstract
    class


@lru_cache(maxsize=<NUM_LIT>)<EOL>def safe_infer(<EOL>node: astroid.node_classes.NodeNG, context=None<EOL>) -> Optional[astroid.node_classes.NodeNG]:

try:<EOL><INDENT>inferit = node.infer(context=context)<EOL>value = next(inferit)<EOL><DEDENT>except astroid.InferenceError:<EOL><INDENT>return None<EOL><DEDENT>try:<EOL><INDENT>next(inferit)<EOL>return None  <EOL><DEDENT>except astroid.InferenceError:<EOL><INDENT>return None  <EOL><DEDENT>except StopIteration:<EOL><INDENT>return value<EOL><DEDENT>

Return the inferred value for the given node.

    Return None if inference failed or if there is some ambiguity (more than
    one node has been inferred).


def has_known_bases(klass: astroid.ClassDef, context=None) -> bool:

try:<EOL><INDENT>return klass._all_bases_known<EOL><DEDENT>except AttributeError:<EOL><INDENT>pass<EOL><DEDENT>for base in klass.bases:<EOL><INDENT>result = safe_infer(base, context=context)<EOL>if (<EOL>not isinstance(result, astroid.ClassDef)<EOL>or result is klass<EOL>or not has_known_bases(result, context=context)<EOL>):<EOL><INDENT>klass._all_bases_known = False<EOL>return False<EOL><DEDENT><DEDENT>klass._all_bases_known = True<EOL>return True<EOL>

Return true if all base classes of a class could be inferred.


def node_type(node: astroid.node_classes.NodeNG) -> Optional[type]:

<EOL>types = set()<EOL>try:<EOL><INDENT>for var_type in node.infer():<EOL><INDENT>if var_type == astroid.Uninferable or is_none(var_type):<EOL><INDENT>continue<EOL><DEDENT>types.add(var_type)<EOL>if len(types) > <NUM_LIT:1>:<EOL><INDENT>return None<EOL><DEDENT><DEDENT><DEDENT>except astroid.InferenceError:<EOL><INDENT>return None<EOL><DEDENT>return types.pop() if types else None<EOL>

Return the inferred type for `node`

    If there is more than one possible type, or if inferred type is Uninferable or None,
    return None


def is_registered_in_singledispatch_function(node: astroid.FunctionDef) -> bool:

singledispatch_qnames = (<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>)<EOL>if not isinstance(node, astroid.FunctionDef):<EOL><INDENT>return False<EOL><DEDENT>decorators = node.decorators.nodes if node.decorators else []<EOL>for decorator in decorators:<EOL><INDENT>if not isinstance(decorator, astroid.Call):<EOL><INDENT>continue<EOL><DEDENT>func = decorator.func<EOL>if not isinstance(func, astroid.Attribute) or func.attrname != "<STR_LIT>":<EOL><INDENT>continue<EOL><DEDENT>try:<EOL><INDENT>func_def = next(func.expr.infer())<EOL><DEDENT>except astroid.InferenceError:<EOL><INDENT>continue<EOL><DEDENT>if isinstance(func_def, astroid.FunctionDef):<EOL><INDENT>return decorated_with(func_def, singledispatch_qnames)<EOL><DEDENT><DEDENT>return False<EOL>

Check if the given function node is a singledispatch function.


def get_node_last_lineno(node: astroid.node_classes.NodeNG) -> int:

<EOL>if getattr(node, "<STR_LIT>", False):<EOL><INDENT>return get_node_last_lineno(node.finalbody[-<NUM_LIT:1>])<EOL><DEDENT>if getattr(node, "<STR_LIT>", False):<EOL><INDENT>return get_node_last_lineno(node.orelse[-<NUM_LIT:1>])<EOL><DEDENT>if getattr(node, "<STR_LIT>", False):<EOL><INDENT>return get_node_last_lineno(node.handlers[-<NUM_LIT:1>])<EOL><DEDENT>if getattr(node, "<STR_LIT:body>", False):<EOL><INDENT>return get_node_last_lineno(node.body[-<NUM_LIT:1>])<EOL><DEDENT>return node.lineno<EOL>

Get the last lineno of the given node. For a simple statement this will just be node.lineno,
but for a node that has child statements (e.g. a method) this will be the lineno of the last
child statement recursively.


def is_postponed_evaluation_enabled(node: astroid.node_classes.NodeNG) -> bool:

name = "<STR_LIT>"<EOL>module = node.root()<EOL>stmt = module.locals.get(name)<EOL>return (<EOL>stmt<EOL>and isinstance(stmt[<NUM_LIT:0>], astroid.ImportFrom)<EOL>and stmt[<NUM_LIT:0>].modname == "<STR_LIT>"<EOL>)<EOL>

Check if the postponed evaluation of annotations is enabled


def is_subclass_of(child: astroid.ClassDef, parent: astroid.ClassDef) -> bool:

if not all(isinstance(node, astroid.ClassDef) for node in (child, parent)):<EOL><INDENT>return False<EOL><DEDENT>for ancestor in child.ancestors():<EOL><INDENT>try:<EOL><INDENT>if astroid.helpers.is_subtype(ancestor, parent):<EOL><INDENT>return True<EOL><DEDENT><DEDENT>except _NonDeducibleTypeHierarchy:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>return False<EOL>

Check if first node is a subclass of second node.
:param child: Node to check for subclass.
:param parent: Node to check for superclass.
:returns: True if child is derived from parent. False otherwise.


parent = node.parent<EOL>if isinstance(parent, astroid.For):<EOL><INDENT>return True<EOL><DEDENT>if isinstance(parent, astroid.Comprehension):<EOL><INDENT>if parent.iter == node:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>elif isinstance(parent, astroid.Call):<EOL><INDENT>if isinstance(parent.func, astroid.Name):<EOL><INDENT>parent_scope = parent.func.lookup(parent.func.name)[<NUM_LIT:0>]<EOL>if _is_builtin(parent_scope) and parent.func.name in _ACCEPTS_ITERATOR:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>elif isinstance(parent.func, astroid.Attribute):<EOL><INDENT>if parent.func.attrname in ATTRIBUTES_ACCEPTS_ITERATOR:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>inferred = utils.safe_infer(parent.func)<EOL>if inferred:<EOL><INDENT>if inferred.qname() in _BUILTIN_METHOD_ACCEPTS_ITERATOR:<EOL><INDENT>return True<EOL><DEDENT>root = inferred.root()<EOL>if root and root.name == "<STR_LIT>":<EOL><INDENT>return True<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(parent, astroid.Assign) and isinstance(<EOL>parent.targets[<NUM_LIT:0>], (astroid.List, astroid.Tuple)<EOL>):<EOL><INDENT>if len(parent.targets[<NUM_LIT:0>].elts) > <NUM_LIT:1>:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>elif (<EOL>isinstance(parent, astroid.Compare)<EOL>and len(parent.ops) == <NUM_LIT:1><EOL>and parent.ops[<NUM_LIT:0>][<NUM_LIT:0>] == "<STR_LIT>"<EOL>):<EOL><INDENT>return True<EOL><DEDENT>elif isinstance(parent, astroid.YieldFrom):<EOL><INDENT>return True<EOL><DEDENT>if isinstance(parent, astroid.Starred):<EOL><INDENT>return True<EOL><DEDENT>return False<EOL>

Check if the node is being used as an iterator.

    Definition is taken from lib2to3.fixer_util.in_special_context().


parent = node.parent<EOL>return isinstance(<EOL>parent, (astroid.TryExcept, astroid.ExceptHandler, astroid.If, astroid.IfExp)<EOL>)<EOL>

Checks if an import node is in the context of a conditional.


self._future_division = False<EOL>self._future_absolute_import = False<EOL>

Clear checker state after previous module.


found_node, _ = node.lookup(node.name)<EOL>if not _is_builtin(found_node):<EOL><INDENT>return<EOL><DEDENT>if node.name not in self._bad_builtins:<EOL><INDENT>return<EOL><DEDENT>if node_ignores_exception(node) or isinstance(<EOL>find_try_except_wrapper_node(node), astroid.ExceptHandler<EOL>):<EOL><INDENT>return<EOL><DEDENT>message = node.name.lower() + "<STR_LIT>"<EOL>self.add_message(message, node=node)<EOL>

Detect when a "bad" built-in is referenced.


try:<EOL><INDENT>for inferred in node.value.infer():<EOL><INDENT>if not isinstance(inferred, astroid.Instance):<EOL><INDENT>continue<EOL><DEDENT>if utils.inherit_from_std_ex(inferred):<EOL><INDENT>self.add_message("<STR_LIT>", node=node)<EOL><DEDENT><DEDENT><DEDENT>except astroid.InferenceError:<EOL><INDENT>return<EOL><DEDENT>


@utils.check_messages("<STR_LIT>", "<STR_LIT>")<EOL><INDENT>def visit_attribute(self, node):<DEDENT>

if node.attrname == "<STR_LIT>":<EOL><INDENT>self.add_message("<STR_LIT>", node=node)<EOL>return<EOL><DEDENT>exception_message = "<STR_LIT:message>"<EOL>try:<EOL><INDENT>for inferred in node.expr.infer():<EOL><INDENT>if isinstance(inferred, astroid.Instance) and utils.inherit_from_std_ex(<EOL>inferred<EOL>):<EOL><INDENT>if node.attrname == exception_message:<EOL><INDENT>if exception_message in inferred.instance_attrs:<EOL><INDENT>continue<EOL><DEDENT>self.add_message("<STR_LIT>", node=node)<EOL><DEDENT><DEDENT>if isinstance(inferred, astroid.Module):<EOL><INDENT>self._warn_if_deprecated(<EOL>node, inferred.name, {node.attrname}, report_on_modules=False<EOL>)<EOL><DEDENT><DEDENT><DEDENT>except astroid.InferenceError:<EOL><INDENT>return<EOL><DEDENT>


@utils.check_messages("<STR_LIT>", "<STR_LIT>")<EOL><INDENT>def visit_excepthandler(self, node):<DEDENT>

def _is_used_in_except_block(node):<EOL><INDENT>scope = node.scope()<EOL>current = node<EOL>while (<EOL>current<EOL>and current != scope<EOL>and not isinstance(current, astroid.ExceptHandler)<EOL>):<EOL><INDENT>current = current.parent<EOL><DEDENT>return isinstance(current, astroid.ExceptHandler) and current.type != node<EOL><DEDENT>if isinstance(node.name, (astroid.Tuple, astroid.List)):<EOL><INDENT>self.add_message("<STR_LIT>", node=node)<EOL>return<EOL><DEDENT>if not node.name:<EOL><INDENT>return<EOL><DEDENT>scope = node.parent.scope()<EOL>scope_names = scope.nodes_of_class(astroid.Name, skip_klass=astroid.FunctionDef)<EOL>scope_names = list(scope_names)<EOL>potential_leaked_names = [<EOL>scope_name<EOL>for scope_name in scope_names<EOL>if scope_name.name == node.name.name<EOL>and scope_name.lineno > node.lineno<EOL>and not _is_used_in_except_block(scope_name)<EOL>]<EOL>reassignments_for_same_name = {<EOL>assign_name.lineno<EOL>for assign_name in scope.nodes_of_class(<EOL>astroid.AssignName, skip_klass=astroid.FunctionDef<EOL>)<EOL>if assign_name.name == node.name.name<EOL>}<EOL>for leaked_name in potential_leaked_names:<EOL><INDENT>if any(<EOL>node.lineno < elem < leaked_name.lineno<EOL>for elem in reassignments_for_same_name<EOL>):<EOL><INDENT>continue<EOL><DEDENT>self.add_message("<STR_LIT>", node=leaked_name)<EOL><DEDENT>

Visit an except handler block and check for exception unpacking.


@utils.check_messages("<STR_LIT>", "<STR_LIT>")<EOL><INDENT>def visit_raise(self, node):<DEDENT>

<EOL>if node.exc is None:<EOL><INDENT>return<EOL><DEDENT>expr = node.exc<EOL>if self._check_raise_value(node, expr):<EOL><INDENT>return<EOL><DEDENT>try:<EOL><INDENT>value = next(astroid.unpack_infer(expr))<EOL><DEDENT>except astroid.InferenceError:<EOL><INDENT>return<EOL><DEDENT>self._check_raise_value(node, value)<EOL>

Visit a raise statement and check for raising
        strings or old-raise-syntax.


def _is_typing_namedtuple(node: astroid.ClassDef) -> bool:

for base in node.ancestors():<EOL><INDENT>if base.qname() == TYPING_NAMEDTUPLE:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL>

Check if a class node is a typing.NamedTuple class


def _is_enum_class(node: astroid.ClassDef) -> bool:

for base in node.bases:<EOL><INDENT>try:<EOL><INDENT>inferred_bases = base.inferred()<EOL><DEDENT>except astroid.InferenceError:<EOL><INDENT>continue<EOL><DEDENT>for ancestor in inferred_bases:<EOL><INDENT>if not isinstance(ancestor, astroid.ClassDef):<EOL><INDENT>continue<EOL><DEDENT>if ancestor.name == "<STR_LIT>" and ancestor.root().name == "<STR_LIT>":<EOL><INDENT>return True<EOL><DEDENT><DEDENT><DEDENT>return False<EOL>

Check if a class definition defines an Enum class.

    :param node: The class node to check.
    :type node: astroid.ClassDef

    :returns: True if the given node represents an Enum class. False otherwise.
    :rtype: bool


def _is_dataclass(node: astroid.ClassDef) -> bool:

if not node.decorators:<EOL><INDENT>return False<EOL><DEDENT>root_locals = node.root().locals<EOL>for decorator in node.decorators.nodes:<EOL><INDENT>if isinstance(decorator, astroid.Call):<EOL><INDENT>decorator = decorator.func<EOL><DEDENT>if not isinstance(decorator, (astroid.Name, astroid.Attribute)):<EOL><INDENT>continue<EOL><DEDENT>if isinstance(decorator, astroid.Name):<EOL><INDENT>name = decorator.name<EOL><DEDENT>else:<EOL><INDENT>name = decorator.attrname<EOL><DEDENT>if name == DATACLASS_DECORATOR and DATACLASS_DECORATOR in root_locals:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL>

Check if a class definition defines a Python 3.7+ dataclass

    :param node: The class node to check.
    :type node: astroid.ClassDef

    :returns: True if the given node represents a dataclass class. False otherwise.
    :rtype: bool


nb_bool_expr = <NUM_LIT:0><EOL>for bool_expr in bool_op.get_children():<EOL><INDENT>if isinstance(bool_expr, BoolOp):<EOL><INDENT>nb_bool_expr += _count_boolean_expressions(bool_expr)<EOL><DEDENT>else:<EOL><INDENT>nb_bool_expr += <NUM_LIT:1><EOL><DEDENT><DEDENT>return nb_bool_expr<EOL>

Counts the number of boolean expressions in BoolOp `bool_op` (recursive)

    example: a and (b or c or (d and e)) ==> 5 boolean expressions


linter.register_checker(MisdesignChecker(linter))<EOL>

required method to auto register this checker


self.stats = self.linter.add_stats()<EOL>self._returns = []<EOL>self._branches = defaultdict(int)<EOL>self._stmts = []<EOL>


nb_parents = len(list(node.ancestors()))<EOL>if nb_parents > self.config.max_parents:<EOL><INDENT>self.add_message(<EOL>"<STR_LIT>",<EOL>node=node,<EOL>args=(nb_parents, self.config.max_parents),<EOL>)<EOL><DEDENT>if len(node.instance_attrs) > self.config.max_attributes:<EOL><INDENT>self.add_message(<EOL>"<STR_LIT>",<EOL>node=node,<EOL>args=(len(node.instance_attrs), self.config.max_attributes),<EOL>)<EOL><DEDENT>

check size of inheritance hierarchy and number of instance attributes


@check_messages("<STR_LIT>", "<STR_LIT>")<EOL><INDENT>def leave_classdef(self, node):<DEDENT>

my_methods = sum(<EOL><NUM_LIT:1> for method in node.mymethods() if not method.name.startswith("<STR_LIT:_>")<EOL>)<EOL>if my_methods > self.config.max_public_methods:<EOL><INDENT>self.add_message(<EOL>"<STR_LIT>",<EOL>node=node,<EOL>args=(my_methods, self.config.max_public_methods),<EOL>)<EOL><DEDENT>if (<EOL>node.type != "<STR_LIT:class>"<EOL>or _is_enum_class(node)<EOL>or _is_dataclass(node)<EOL>or _is_typing_namedtuple(node)<EOL>):<EOL><INDENT>return<EOL><DEDENT>all_methods = _count_methods_in_class(node)<EOL>if all_methods < self.config.min_public_methods:<EOL><INDENT>self.add_message(<EOL>"<STR_LIT>",<EOL>node=node,<EOL>args=(all_methods, self.config.min_public_methods),<EOL>)<EOL><DEDENT>


<EOL>self._returns.append(<NUM_LIT:0>)<EOL>args = node.args.args<EOL>ignored_argument_names = self._ignored_argument_names<EOL>if args is not None:<EOL><INDENT>ignored_args_num = <NUM_LIT:0><EOL>if ignored_argument_names:<EOL><INDENT>ignored_args_num = sum(<EOL><NUM_LIT:1> for arg in args if ignored_argument_names.match(arg.name)<EOL>)<EOL><DEDENT>argnum = len(args) - ignored_args_num<EOL>if argnum > self.config.max_args:<EOL><INDENT>self.add_message(<EOL>"<STR_LIT>",<EOL>node=node,<EOL>args=(len(args), self.config.max_args),<EOL>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>ignored_args_num = <NUM_LIT:0><EOL><DEDENT>locnum = len(node.locals) - ignored_args_num<EOL>if locnum > self.config.max_locals:<EOL><INDENT>self.add_message(<EOL>"<STR_LIT>", node=node, args=(locnum, self.config.max_locals)<EOL>)<EOL><DEDENT>self._stmts.append(<NUM_LIT:1>)<EOL>

check function name, docstring, arguments, redefinition,
        variable names, max locals


returns = self._returns.pop()<EOL>if returns > self.config.max_returns:<EOL><INDENT>self.add_message(<EOL>"<STR_LIT>",<EOL>node=node,<EOL>args=(returns, self.config.max_returns),<EOL>)<EOL><DEDENT>branches = self._branches[node]<EOL>if branches > self.config.max_branches:<EOL><INDENT>self.add_message(<EOL>"<STR_LIT>",<EOL>node=node,<EOL>args=(branches, self.config.max_branches),<EOL>)<EOL><DEDENT>stmts = self._stmts.pop()<EOL>if stmts > self.config.max_statements:<EOL><INDENT>self.add_message(<EOL>"<STR_LIT>",<EOL>node=node,<EOL>args=(stmts, self.config.max_statements),<EOL>)<EOL><DEDENT>

most of the work is done here on close:
        checks for max returns, branch, return in __init__


if not self._returns:<EOL><INDENT>return  <EOL><DEDENT>self._returns[-<NUM_LIT:1>] += <NUM_LIT:1><EOL>


if node.is_statement:<EOL><INDENT>self._inc_all_stmts(<NUM_LIT:1>)<EOL><DEDENT>

default visit method -> increments the statements counter if
        necessary


branches = len(node.handlers)<EOL>if node.orelse:<EOL><INDENT>branches += <NUM_LIT:1><EOL><DEDENT>self._inc_branch(node, branches)<EOL>self._inc_all_stmts(branches)<EOL>


self._inc_branch(node, <NUM_LIT:2>)<EOL>self._inc_all_stmts(<NUM_LIT:2>)<EOL>


self._check_boolean_expressions(node)<EOL>branches = <NUM_LIT:1><EOL>if node.orelse and (len(node.orelse) > <NUM_LIT:1> or not isinstance(node.orelse[<NUM_LIT:0>], If)):<EOL><INDENT>branches += <NUM_LIT:1><EOL><DEDENT>self._inc_branch(node, branches)<EOL>self._inc_all_stmts(branches)<EOL>

increments the branches counter and checks boolean expressions


condition = node.test<EOL>if not isinstance(condition, BoolOp):<EOL><INDENT>return<EOL><DEDENT>nb_bool_expr = _count_boolean_expressions(condition)<EOL>if nb_bool_expr > self.config.max_bool_expr:<EOL><INDENT>self.add_message(<EOL>"<STR_LIT>",<EOL>node=condition,<EOL>args=(nb_bool_expr, self.config.max_bool_expr),<EOL>)<EOL><DEDENT>

Go through "if" node `node` and counts its boolean expressions

        if the "if" node test is a BoolOp node


branches = <NUM_LIT:1><EOL>if node.orelse:<EOL><INDENT>branches += <NUM_LIT:1><EOL><DEDENT>self._inc_branch(node, branches)<EOL>


def _inc_branch(self, node, branchesnum=<NUM_LIT:1>):

self._branches[node.scope()] += branchesnum<EOL>


total_lines = stats["<STR_LIT>"]<EOL>if not total_lines:<EOL><INDENT>raise EmptyReportError()<EOL><DEDENT>sect.description = "<STR_LIT>" % total_lines<EOL>lines = ("<STR_LIT:type>", "<STR_LIT>", "<STR_LIT:%>", "<STR_LIT>", "<STR_LIT>")<EOL>for node_type in ("<STR_LIT:code>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>"):<EOL><INDENT>key = node_type + "<STR_LIT>"<EOL>total = stats[key]<EOL>percent = float(total * <NUM_LIT:100>) / total_lines<EOL>lines += (node_type, str(total), "<STR_LIT>" % percent, "<STR_LIT>", "<STR_LIT>")<EOL><DEDENT>sect.append(Table(children=lines, cols=<NUM_LIT:5>, rheaders=<NUM_LIT:1>))<EOL>

calculate percentage of code / doc / comment / empty


i = start_index<EOL>tok_type = tokens[i][<NUM_LIT:0>]<EOL>start = tokens[i][<NUM_LIT:2>]<EOL>pos = start<EOL>line_type = None<EOL>while i < len(tokens) and tokens[i][<NUM_LIT:2>][<NUM_LIT:0>] == start[<NUM_LIT:0>]:<EOL><INDENT>tok_type = tokens[i][<NUM_LIT:0>]<EOL>pos = tokens[i][<NUM_LIT:3>]<EOL>if line_type is None:<EOL><INDENT>if tok_type == tokenize.STRING:<EOL><INDENT>line_type = "<STR_LIT>"<EOL><DEDENT>elif tok_type == tokenize.COMMENT:<EOL><INDENT>line_type = "<STR_LIT>"<EOL><DEDENT>elif tok_type in JUNK:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>line_type = "<STR_LIT>"<EOL><DEDENT><DEDENT>i += <NUM_LIT:1><EOL><DEDENT>if line_type is None:<EOL><INDENT>line_type = "<STR_LIT>"<EOL><DEDENT>elif i < len(tokens) and tokens[i][<NUM_LIT:0>] == tokenize.NEWLINE:<EOL><INDENT>i += <NUM_LIT:1><EOL><DEDENT>return i, pos[<NUM_LIT:0>] - start[<NUM_LIT:0>] + <NUM_LIT:1>, line_type<EOL>

return the line type : docstring, comment, code, empty


linter.register_checker(RawMetricsChecker(linter))<EOL>


self.stats = self.linter.add_stats(<EOL>total_lines=<NUM_LIT:0>,<EOL>code_lines=<NUM_LIT:0>,<EOL>empty_lines=<NUM_LIT:0>,<EOL>docstring_lines=<NUM_LIT:0>,<EOL>comment_lines=<NUM_LIT:0>,<EOL>)<EOL>


i = <NUM_LIT:0><EOL>tokens = list(tokens)<EOL>while i < len(tokens):<EOL><INDENT>i, lines_number, line_type = get_type(tokens, i)<EOL>self.stats["<STR_LIT>"] += lines_number<EOL>self.stats[line_type] += lines_number<EOL><DEDENT>


if isinstance(stmt, (astroid.List, astroid.Tuple)):<EOL><INDENT>for elt in stmt.elts:<EOL><INDENT>inferred = utils.safe_infer(elt)<EOL>if inferred and inferred is not astroid.Uninferable:<EOL><INDENT>yield elt, inferred<EOL><DEDENT><DEDENT>return<EOL><DEDENT>for infered in stmt.infer(context):<EOL><INDENT>if infered is astroid.Uninferable:<EOL><INDENT>continue<EOL><DEDENT>yield stmt, infered<EOL><DEDENT>

Recursively generate nodes inferred by the given statement.
If the inferred value is a list or a tuple, recurse on the elements.
Returns an iterator which yields tuples in the format
('original node', 'infered node').


def _is_raising(body: typing.List) -> bool:

for node in body:<EOL><INDENT>if isinstance(node, astroid.Raise):<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL>

Return true if the given statement node raise an exception


linter.register_checker(ExceptionsChecker(linter))<EOL>


Default implementation for all the nodes.


cause = utils.safe_infer(node.cause)<EOL>if cause in (astroid.Uninferable, None):<EOL><INDENT>return<EOL><DEDENT>if isinstance(cause, astroid.Const):<EOL><INDENT>if cause.value is not None:<EOL><INDENT>self.add_message("<STR_LIT>", node=node)<EOL><DEDENT><DEDENT>elif not isinstance(cause, astroid.ClassDef) and not utils.inherit_from_std_ex(<EOL>cause<EOL>):<EOL><INDENT>self.add_message("<STR_LIT>", node=node)<EOL><DEDENT>

Verify that the exception context is properly set.

        An exception context can be only `None` or an exception.


self._check_try_except_raise(node)<EOL>exceptions_classes = []<EOL>nb_handlers = len(node.handlers)<EOL>for index, handler in enumerate(node.handlers):<EOL><INDENT>if handler.type is None:<EOL><INDENT>if not _is_raising(handler.body):<EOL><INDENT>self.add_message("<STR_LIT>", node=handler)<EOL><DEDENT>if index < (nb_handlers - <NUM_LIT:1>):<EOL><INDENT>msg = "<STR_LIT>"<EOL>self.add_message("<STR_LIT>", node=node, args=msg)<EOL><DEDENT><DEDENT>elif isinstance(handler.type, astroid.BoolOp):<EOL><INDENT>self.add_message(<EOL>"<STR_LIT>", node=handler, args=handler.type.op<EOL>)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>excs = list(_annotated_unpack_infer(handler.type))<EOL><DEDENT>except astroid.InferenceError:<EOL><INDENT>continue<EOL><DEDENT>for part, exc in excs:<EOL><INDENT>if exc is astroid.Uninferable:<EOL><INDENT>continue<EOL><DEDENT>if isinstance(exc, astroid.Instance) and utils.inherit_from_std_ex(<EOL>exc<EOL>):<EOL><INDENT>exc = exc._proxied<EOL><DEDENT>self._check_catching_non_exception(handler, exc, part)<EOL>if not isinstance(exc, astroid.ClassDef):<EOL><INDENT>continue<EOL><DEDENT>exc_ancestors = [<EOL>anc<EOL>for anc in exc.ancestors()<EOL>if isinstance(anc, astroid.ClassDef)<EOL>]<EOL>for previous_exc in exceptions_classes:<EOL><INDENT>if previous_exc in exc_ancestors:<EOL><INDENT>msg = "<STR_LIT>" % (<EOL>previous_exc.name,<EOL>exc.name,<EOL>)<EOL>self.add_message(<EOL>"<STR_LIT>", node=handler.type, args=msg<EOL>)<EOL><DEDENT><DEDENT>if (<EOL>exc.name in self.config.overgeneral_exceptions<EOL>and exc.root().name == utils.EXCEPTIONS_MODULE<EOL>and not _is_raising(handler.body)<EOL>):<EOL><INDENT>self.add_message(<EOL>"<STR_LIT>", args=exc.name, node=handler.type<EOL>)<EOL><DEDENT>if exc in exceptions_classes:<EOL><INDENT>self.add_message(<EOL>"<STR_LIT>", args=exc.name, node=handler.type<EOL>)<EOL><DEDENT><DEDENT>exceptions_classes += [exc for _, exc in excs]<EOL><DEDENT><DEDENT>


linter.register_checker(StdlibChecker(linter))<EOL>


try:<EOL><INDENT>infered = next(node.infer())<EOL><DEDENT>except astroid.InferenceError:<EOL><INDENT>return<EOL><DEDENT>if isinstance(infered, Instance) and infered.qname() == "<STR_LIT>":<EOL><INDENT>self.add_message("<STR_LIT>", node=node)<EOL><DEDENT>

Check that a datetime was infered.
        If so, emit boolean-datetime warning.


try:<EOL><INDENT>mode_arg = utils.get_argument_from_call(node, position=<NUM_LIT:1>, keyword="<STR_LIT>")<EOL><DEDENT>except utils.NoSuchArgumentError:<EOL><INDENT>return<EOL><DEDENT>if mode_arg:<EOL><INDENT>mode_arg = utils.safe_infer(mode_arg)<EOL>if isinstance(mode_arg, astroid.Const) and not _check_mode_str(<EOL>mode_arg.value<EOL>):<EOL><INDENT>self.add_message("<STR_LIT>", node=node, args=mode_arg.value)<EOL><DEDENT><DEDENT>

Check that the mode argument of an open or file call is valid.


return (<EOL>isinstance(node, astroid.Call)<EOL>and isinstance(node.func, astroid.Name)<EOL>and node.func.name == "<STR_LIT>"<EOL>)<EOL>


token = tokens[index]<EOL>if token.exact_type != tokenize.COMMA:<EOL><INDENT>return False<EOL><DEDENT>left_tokens = itertools.islice(tokens, index + <NUM_LIT:1>, None)<EOL>same_line_remaining_tokens = list(<EOL>itertools.takewhile(<EOL>lambda other_token, _token=token: other_token.start[<NUM_LIT:0>] == _token.start[<NUM_LIT:0>],<EOL>left_tokens,<EOL>)<EOL>)<EOL>is_last_element = all(<EOL>other_token.type in (tokenize.NEWLINE, tokenize.COMMENT)<EOL>for other_token in same_line_remaining_tokens<EOL>)<EOL>if not same_line_remaining_tokens or not is_last_element:<EOL><INDENT>return False<EOL><DEDENT>def get_curline_index_start():<EOL><INDENT>"""<STR_LIT>"""<EOL>for subindex, token in enumerate(reversed(tokens[:index])):<EOL><INDENT>if token.type in (tokenize.NEWLINE, tokenize.NL):<EOL><INDENT>return index - subindex<EOL><DEDENT><DEDENT>return <NUM_LIT:0><EOL><DEDENT>curline_start = get_curline_index_start()<EOL>expected_tokens = {"<STR_LIT>", "<STR_LIT>"}<EOL>for prevtoken in tokens[curline_start:index]:<EOL><INDENT>if "<STR_LIT:=>" in prevtoken.string or prevtoken.string in expected_tokens:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL>

Check if the given token is a trailing comma

    :param tokens: Sequence of modules tokens
    :type tokens: list[tokenize.TokenInfo]
    :param int index: Index of token under check in tokens
    :returns: True if the token is a comma which trails an expression
    :rtype: bool


Required method to auto register this checker.


if isinstance(node.parent, astroid.If):<EOL><INDENT>orelse = node.parent.orelse<EOL>if orelse and orelse == [node]:<EOL><INDENT>if (node.lineno, node.col_offset) in self._elifs:<EOL><INDENT>return True<EOL><DEDENT><DEDENT><DEDENT>return False<EOL>

Check if the given node is an actual elif

        This is a problem we're having with the builtin ast module,
        which splits `elif` branches into a separate if statement.
        Unfortunately we need to know the exact type in certain
        cases.


if self._is_actual_elif(node):<EOL><INDENT>return<EOL><DEDENT>if len(node.orelse) != <NUM_LIT:1> or len(node.body) != <NUM_LIT:1>:<EOL><INDENT>return<EOL><DEDENT>first_branch = node.body[<NUM_LIT:0>]<EOL>else_branch = node.orelse[<NUM_LIT:0>]<EOL>if isinstance(first_branch, astroid.Return):<EOL><INDENT>if not isinstance(else_branch, astroid.Return):<EOL><INDENT>return<EOL><DEDENT>first_branch_is_bool = self._is_bool_const(first_branch)<EOL>else_branch_is_bool = self._is_bool_const(else_branch)<EOL>reduced_to = "<STR_LIT>"<EOL><DEDENT>elif isinstance(first_branch, astroid.Assign):<EOL><INDENT>if not isinstance(else_branch, astroid.Assign):<EOL><INDENT>return<EOL><DEDENT>first_branch_targets = [<EOL>target.name<EOL>for target in first_branch.targets<EOL>if isinstance(target, astroid.AssignName)<EOL>]<EOL>else_branch_targets = [<EOL>target.name<EOL>for target in else_branch.targets<EOL>if isinstance(target, astroid.AssignName)<EOL>]<EOL>if not first_branch_targets or not else_branch_targets:<EOL><INDENT>return<EOL><DEDENT>if sorted(first_branch_targets) != sorted(else_branch_targets):<EOL><INDENT>return<EOL><DEDENT>first_branch_is_bool = self._is_bool_const(first_branch)<EOL>else_branch_is_bool = self._is_bool_const(else_branch)<EOL>reduced_to = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>return<EOL><DEDENT>if not first_branch_is_bool or not else_branch_is_bool:<EOL><INDENT>return<EOL><DEDENT>if not first_branch.value.value:<EOL><INDENT>return<EOL><DEDENT>self.add_message("<STR_LIT>", node=node, args=(reduced_to,))<EOL>

Check if the given if node can be simplified.

        The if statement can be reduced to a boolean expression
        in some cases. For instance, if there are two branches
        and both of them return a boolean value that depends on
        the result of the statement's test, then this can be reduced
        to `bool(test)` without losing any functionality.


frame = node.frame()<EOL>if not isinstance(frame, astroid.FunctionDef) or not frame.is_generator():<EOL><INDENT>return<EOL><DEDENT>if utils.node_ignores_exception(node, StopIteration):<EOL><INDENT>return<EOL><DEDENT>if not node.exc:<EOL><INDENT>return<EOL><DEDENT>exc = utils.safe_infer(node.exc)<EOL>if exc is None or exc is astroid.Uninferable:<EOL><INDENT>return<EOL><DEDENT>if self._check_exception_inherit_from_stopiteration(exc):<EOL><INDENT>self.add_message("<STR_LIT>", node=node)<EOL><DEDENT>

Check if an exception of type StopIteration is raised inside a generator


stopiteration_qname = "<STR_LIT>".format(utils.EXCEPTIONS_MODULE)<EOL>return any(_class.qname() == stopiteration_qname for _class in exc.mro())<EOL>

Return True if the exception node in argument inherit from StopIteration


def _looks_like_infinite_iterator(param):<EOL><INDENT>inferred = utils.safe_infer(param)<EOL>if inferred:<EOL><INDENT>return inferred.qname() in KNOWN_INFINITE_ITERATORS<EOL><DEDENT>return False<EOL><DEDENT>if isinstance(node.func, astroid.Attribute):<EOL><INDENT>return<EOL><DEDENT>inferred = utils.safe_infer(node.func)<EOL>if getattr(inferred, "<STR_LIT:name>", "<STR_LIT>") == "<STR_LIT>":<EOL><INDENT>frame = node.frame()<EOL>has_sentinel_value = len(node.args) > <NUM_LIT:1><EOL>if (<EOL>isinstance(frame, astroid.FunctionDef)<EOL>and frame.is_generator()<EOL>and not has_sentinel_value<EOL>and not utils.node_ignores_exception(node, StopIteration)<EOL>and not _looks_like_infinite_iterator(node.args[<NUM_LIT:0>])<EOL>):<EOL><INDENT>self.add_message("<STR_LIT>", node=node)<EOL><DEDENT><DEDENT>

Check if a StopIteration exception is raised by the call to next function

        If the next value has a default value, then do not add message.

        :param node: Check to see if this Call node is a next function
        :type node: :class:`astroid.node_classes.Call`


<EOL>if not isinstance(node.scope(), astroid.FunctionDef):<EOL><INDENT>return<EOL><DEDENT>nested_blocks = self._nested_blocks[:]<EOL>if node.parent == node.scope():<EOL><INDENT>self._nested_blocks = [node]<EOL><DEDENT>else:<EOL><INDENT>for ancestor_node in reversed(self._nested_blocks):<EOL><INDENT>if ancestor_node == node.parent:<EOL><INDENT>break<EOL><DEDENT>self._nested_blocks.pop()<EOL><DEDENT>if isinstance(node, astroid.If) and self._is_actual_elif(node):<EOL><INDENT>if self._nested_blocks:<EOL><INDENT>self._nested_blocks.pop()<EOL><DEDENT><DEDENT>self._nested_blocks.append(node)<EOL><DEDENT>if len(nested_blocks) > len(self._nested_blocks):<EOL><INDENT>self._emit_nested_blocks_message_if_needed(nested_blocks)<EOL><DEDENT>

Update and check the number of nested blocks


duplicated_objects = set()<EOL>all_types = collections.defaultdict(set)<EOL>for call in node.values:<EOL><INDENT>if not isinstance(call, astroid.Call) or len(call.args) != <NUM_LIT:2>:<EOL><INDENT>continue<EOL><DEDENT>inferred = utils.safe_infer(call.func)<EOL>if not inferred or not utils.is_builtin_object(inferred):<EOL><INDENT>continue<EOL><DEDENT>if inferred.name != "<STR_LIT>":<EOL><INDENT>continue<EOL><DEDENT>isinstance_object = call.args[<NUM_LIT:0>].as_string()<EOL>isinstance_types = call.args[<NUM_LIT:1>]<EOL>if isinstance_object in all_types:<EOL><INDENT>duplicated_objects.add(isinstance_object)<EOL><DEDENT>if isinstance(isinstance_types, astroid.Tuple):<EOL><INDENT>elems = [<EOL>class_type.as_string() for class_type in isinstance_types.itered()<EOL>]<EOL><DEDENT>else:<EOL><INDENT>elems = [isinstance_types.as_string()]<EOL><DEDENT>all_types[isinstance_object].update(elems)<EOL><DEDENT>return {<EOL>key: value for key, value in all_types.items() if key in duplicated_objects<EOL>}<EOL>

Get the duplicated types from the underlying isinstance calls.

        :param astroid.BoolOp node: Node which should contain a bunch of isinstance calls.
        :returns: Dictionary of the comparison objects from the isinstance calls,
                  to duplicate values from consecutive calls.
        :rtype: dict


if node.op != "<STR_LIT>":<EOL><INDENT>return<EOL><DEDENT>first_args = self._duplicated_isinstance_types(node)<EOL>for duplicated_name, class_names in first_args.items():<EOL><INDENT>names = sorted(name for name in class_names)<EOL>self.add_message(<EOL>"<STR_LIT>",<EOL>node=node,<EOL>args=(duplicated_name, "<STR_LIT:U+002CU+0020>".join(names)),<EOL>)<EOL><DEDENT>

Check isinstance calls which can be merged together.


if node.op != "<STR_LIT>" or len(node.values) < <NUM_LIT:2>:<EOL><INDENT>return<EOL><DEDENT>def _find_lower_upper_bounds(comparison_node, uses):<EOL><INDENT>left_operand = comparison_node.left<EOL>for operator, right_operand in comparison_node.ops:<EOL><INDENT>for operand in (left_operand, right_operand):<EOL><INDENT>value = None<EOL>if isinstance(operand, astroid.Name):<EOL><INDENT>value = operand.name<EOL><DEDENT>elif isinstance(operand, astroid.Const):<EOL><INDENT>value = operand.value<EOL><DEDENT>if value is None:<EOL><INDENT>continue<EOL><DEDENT>if operator in ("<STR_LIT:<>", "<STR_LIT>"):<EOL><INDENT>if operand is left_operand:<EOL><INDENT>uses[value]["<STR_LIT>"].add(comparison_node)<EOL><DEDENT>elif operand is right_operand:<EOL><INDENT>uses[value]["<STR_LIT>"].add(comparison_node)<EOL><DEDENT><DEDENT>elif operator in ("<STR_LIT:>>", "<STR_LIT>"):<EOL><INDENT>if operand is left_operand:<EOL><INDENT>uses[value]["<STR_LIT>"].add(comparison_node)<EOL><DEDENT>elif operand is right_operand:<EOL><INDENT>uses[value]["<STR_LIT>"].add(comparison_node)<EOL><DEDENT><DEDENT><DEDENT>left_operand = right_operand<EOL><DEDENT><DEDENT>uses = collections.defaultdict(<EOL>lambda: {"<STR_LIT>": set(), "<STR_LIT>": set()}<EOL>)<EOL>for comparison_node in node.values:<EOL><INDENT>if isinstance(comparison_node, astroid.Compare):<EOL><INDENT>_find_lower_upper_bounds(comparison_node, uses)<EOL><DEDENT><DEDENT>for _, bounds in uses.items():<EOL><INDENT>num_shared = len(bounds["<STR_LIT>"].intersection(bounds["<STR_LIT>"]))<EOL>num_lower_bounds = len(bounds["<STR_LIT>"])<EOL>num_upper_bounds = len(bounds["<STR_LIT>"])<EOL>if num_shared < num_lower_bounds and num_shared < num_upper_bounds:<EOL><INDENT>self.add_message("<STR_LIT>", node=node)<EOL>break<EOL><DEDENT><DEDENT>

Check if there is any chained comparison in the expression.

        Add a refactoring message if a boolOp contains comparison like a < b and b < c,
        which can be chained as a < b < c.

        Care is taken to avoid simplifying a < b < c and b < d.


for_loop = aug_assign.parent<EOL>if not isinstance(for_loop, astroid.For) or len(for_loop.body) > <NUM_LIT:1>:<EOL><INDENT>return<EOL><DEDENT>assign = for_loop.previous_sibling()<EOL>if not isinstance(assign, astroid.Assign):<EOL><INDENT>return<EOL><DEDENT>result_assign_names = {<EOL>target.name<EOL>for target in assign.targets<EOL>if isinstance(target, astroid.AssignName)<EOL>}<EOL>is_concat_loop = (<EOL>aug_assign.op == "<STR_LIT>"<EOL>and isinstance(aug_assign.target, astroid.AssignName)<EOL>and len(for_loop.body) == <NUM_LIT:1><EOL>and aug_assign.target.name in result_assign_names<EOL>and isinstance(assign.value, astroid.Const)<EOL>and isinstance(assign.value.value, str)<EOL>and isinstance(aug_assign.value, astroid.Name)<EOL>and aug_assign.value.name == for_loop.target.name<EOL>)<EOL>if is_concat_loop:<EOL><INDENT>self.add_message("<STR_LIT>", node=aug_assign)<EOL><DEDENT>

We start with the augmented assignment and work our way upwards.
Names of variables for nodes if match successful:
result = ''  # assign
for number in ['1', '2', '3']  # for_loop
    result += number  # aug_assign


return (<EOL>isinstance(node, astroid.BoolOp)<EOL>and node.op == "<STR_LIT>"<EOL>and len(node.values) == <NUM_LIT:2><EOL>and isinstance(node.values[<NUM_LIT:0>], astroid.BoolOp)<EOL>and not isinstance(node.values[<NUM_LIT:1>], astroid.BoolOp)<EOL>and node.values[<NUM_LIT:0>].op == "<STR_LIT>"<EOL>and not isinstance(node.values[<NUM_LIT:0>].values[<NUM_LIT:1>], astroid.BoolOp)<EOL>and len(node.values[<NUM_LIT:0>].values) == <NUM_LIT:2><EOL>)<EOL>

Returns true if node is 'condition and true_value or false_value' form.

All of: condition, true_value and false_value should not be a complex boolean expression


<EOL>explicit_returns = [<EOL>_node for _node in self._return_nodes[node.name] if _node.value is not None<EOL>]<EOL>if not explicit_returns:<EOL><INDENT>return<EOL><DEDENT>if len(explicit_returns) == len(<EOL>self._return_nodes[node.name]<EOL>) and self._is_node_return_ended(node):<EOL><INDENT>return<EOL><DEDENT>self.add_message("<STR_LIT>", node=node)<EOL>

Check that all return statements inside a function are consistent.

        Return statements are consistent if:
            - all returns are explicit and if there is no implicit return;
            - all returns are empty and if there is, possibly, an implicit return.

        Args:
            node (astroid.FunctionDef): the function holding the return statements.


<EOL>if isinstance(node, astroid.Return):<EOL><INDENT>return True<EOL><DEDENT>if isinstance(node, astroid.Call):<EOL><INDENT>try:<EOL><INDENT>funcdef_node = node.func.inferred()[<NUM_LIT:0>]<EOL>if self._is_function_def_never_returning(funcdef_node):<EOL><INDENT>return True<EOL><DEDENT><DEDENT>except astroid.InferenceError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if isinstance(node, astroid.While):<EOL><INDENT>return True<EOL><DEDENT>if isinstance(node, astroid.Raise):<EOL><INDENT>if not node.exc:<EOL><INDENT>return True<EOL><DEDENT>if not utils.is_node_inside_try_except(node):<EOL><INDENT>return True<EOL><DEDENT>exc = utils.safe_infer(node.exc)<EOL>if exc is None or exc is astroid.Uninferable:<EOL><INDENT>return False<EOL><DEDENT>exc_name = exc.pytype().split("<STR_LIT:.>")[-<NUM_LIT:1>]<EOL>handlers = utils.get_exception_handlers(node, exc_name)<EOL>handlers = list(handlers) if handlers is not None else []<EOL>if handlers:<EOL><INDENT>return any(<EOL>self._is_node_return_ended(_handler) for _handler in handlers<EOL>)<EOL><DEDENT>return True<EOL><DEDENT>if isinstance(node, astroid.If):<EOL><INDENT>is_orelse_returning = any(<EOL>self._is_node_return_ended(_ore)<EOL>for _ore in node.orelse<EOL>if not isinstance(_ore, astroid.FunctionDef)<EOL>)<EOL>is_if_returning = any(<EOL>self._is_node_return_ended(_ifn)<EOL>for _ifn in node.body<EOL>if not isinstance(_ifn, astroid.FunctionDef)<EOL>)<EOL>return is_if_returning and is_orelse_returning<EOL><DEDENT>return any(<EOL>self._is_node_return_ended(_child)<EOL>for _child in node.get_children()<EOL>if not isinstance(_child, astroid.ExceptHandler)<EOL>)<EOL>

Check if the node ends with an explicit return statement.

        Args:
            node (astroid.NodeNG): node to be checked.

        Returns:
            bool: True if the node ends with an explicit statement, False otherwise.


try:<EOL><INDENT>return node.qname() in self._never_returning_functions<EOL><DEDENT>except TypeError:<EOL><INDENT>return False<EOL><DEDENT>

Return True if the function never returns. False otherwise.

        Args:
            node (astroid.FunctionDef): function definition node to be analyzed.

        Returns:
            bool: True if the function never returns, False otherwise.


if len(self._return_nodes[node.name]) > <NUM_LIT:1>:<EOL><INDENT>return<EOL><DEDENT>if len(node.body) <= <NUM_LIT:1>:<EOL><INDENT>return<EOL><DEDENT>last = node.body[-<NUM_LIT:1>]<EOL>if isinstance(last, astroid.Return):<EOL><INDENT>if last.value is None:<EOL><INDENT>self.add_message("<STR_LIT>", node=node)<EOL><DEDENT>elif isinstance(last.value, astroid.Const) and (last.value.value is None):<EOL><INDENT>self.add_message("<STR_LIT>", node=node)<EOL><DEDENT><DEDENT>

Check for presence of a *single* return statement at the end of a
        function. "return" or "return None" are useless because None is the
        default return type if they are missing.

        NOTE: produces a message only if there is a single return statement
        in the function body. Otherwise _check_consistent_returns() is called!
        Per its implementation and PEP8 we can have a "return None" at the end
        of the function body if there are other return statements before that!


<EOL>if not isinstance(node.iter, astroid.Call):<EOL><INDENT>return<EOL><DEDENT>if not self._is_builtin(node.iter.func, "<STR_LIT>"):<EOL><INDENT>return<EOL><DEDENT>if len(node.iter.args) == <NUM_LIT:2> and not _is_constant_zero(node.iter.args[<NUM_LIT:0>]):<EOL><INDENT>return<EOL><DEDENT>if len(node.iter.args) > <NUM_LIT:2>:<EOL><INDENT>return<EOL><DEDENT>if not isinstance(node.iter.args[-<NUM_LIT:1>], astroid.Call):<EOL><INDENT>return<EOL><DEDENT>second_func = node.iter.args[-<NUM_LIT:1>].func<EOL>if not self._is_builtin(second_func, "<STR_LIT>"):<EOL><INDENT>return<EOL><DEDENT>len_args = node.iter.args[-<NUM_LIT:1>].args<EOL>if not len_args or len(len_args) != <NUM_LIT:1>:<EOL><INDENT>return<EOL><DEDENT>iterating_object = len_args[<NUM_LIT:0>]<EOL>if not isinstance(iterating_object, astroid.Name):<EOL><INDENT>return<EOL><DEDENT>scope = node.scope()<EOL>if iterating_object.name == "<STR_LIT>" and scope.name == "<STR_LIT>":<EOL><INDENT>return<EOL><DEDENT>for child in node.body:<EOL><INDENT>for subscript in child.nodes_of_class(astroid.Subscript):<EOL><INDENT>if not isinstance(subscript.value, astroid.Name):<EOL><INDENT>continue<EOL><DEDENT>if not isinstance(subscript.slice, astroid.Index):<EOL><INDENT>continue<EOL><DEDENT>if not isinstance(subscript.slice.value, astroid.Name):<EOL><INDENT>continue<EOL><DEDENT>if subscript.slice.value.name != node.target.name:<EOL><INDENT>continue<EOL><DEDENT>if iterating_object.name != subscript.value.name:<EOL><INDENT>continue<EOL><DEDENT>if subscript.value.scope() != node.scope():<EOL><INDENT>continue<EOL><DEDENT>self.add_message("<STR_LIT>", node=node)<EOL>return<EOL><DEDENT><DEDENT>

Emit a convention whenever range and len are used for indexing.


if (<EOL>isinstance(node, astroid.UnaryOp)<EOL>and node.op == "<STR_LIT>"<EOL>and _is_len_call(node.operand)<EOL>):<EOL><INDENT>self.add_message("<STR_LIT>", node=node)<EOL><DEDENT>

`not len(S)` must become `not S` regardless if the parent block
        is a test condition or something else (boolean expression)
        e.g. `if not len(S):`


def _wrapper(fun):<EOL><INDENT>@functools.wraps(fun)<EOL>def _forward(self):<EOL><INDENT>for key, value in kwargs.items():<EOL><INDENT>setattr(self.checker.config, key, value)<EOL><DEDENT>if isinstance(self, CheckerTestCase):<EOL><INDENT>self.checker.open()<EOL><DEDENT>fun(self)<EOL><DEDENT>return _forward<EOL><DEDENT>return _wrapper<EOL>

Decorator for setting config values on a checker.


<EOL>file_handle, tmp = tempfile.mkstemp()<EOL>if content:<EOL><INDENT>if sys.version_info >= (<NUM_LIT:3>, <NUM_LIT:0>):<EOL><INDENT>write(file_handle, bytes(content, "<STR_LIT:ascii>"))<EOL><DEDENT>else:<EOL><INDENT>write(file_handle, content)<EOL><DEDENT><DEDENT>try:<EOL><INDENT>yield tmp<EOL><DEDENT>finally:<EOL><INDENT>close(file_handle)<EOL>remove(tmp)<EOL><DEDENT>

Create a new temporary file.

    If *content* parameter is given, then it will be written
    in the temporary file, before passing it back.
    This is a context manager and should be used with a *with* statement.


with _create_tempfile() as temp:<EOL><INDENT>module = astroid.parse(code)<EOL>module.file = temp<EOL>yield module<EOL><DEDENT>

Create an astroid module for the given code, backed by a real file.


obj = msg.obj<EOL>line = msg.line<EOL>msg_id = msg.msg_id<EOL>msg = msg.msg<EOL>self.message_ids[msg_id] = <NUM_LIT:1><EOL>if obj:<EOL><INDENT>obj = "<STR_LIT>" % obj<EOL><DEDENT>sigle = msg_id[<NUM_LIT:0>]<EOL>if PY3K and linesep != "<STR_LIT:\n>":<EOL><INDENT>msg = msg.replace("<STR_LIT:\r\n>", "<STR_LIT:\n>")<EOL><DEDENT>self.messages.append("<STR_LIT>" % (sigle, line, obj, msg))<EOL>

manage message of different type and in the context of path


with self.assertAddsMessages():<EOL><INDENT>yield<EOL><DEDENT>

Assert that no messages are added by the given method.


yield<EOL>got = self.linter.release_messages()<EOL>msg = "<STR_LIT>" "<STR_LIT>" % (<EOL>"<STR_LIT:\n>".join(repr(m) for m in messages),<EOL>"<STR_LIT:\n>".join(repr(m) for m in got),<EOL>)<EOL>assert list(messages) == got, msg<EOL>

Assert that exactly the given method adds the given messages.

        The list of messages must exactly match *all* the messages added by the
        method. Additionally, we check to see whether the args in each message can
        actually be substituted into the message string.


walker = ASTWalker(linter)<EOL>walker.add_checker(self.checker)<EOL>walker.walk(node)<EOL>


self._stream.write("<STR_LIT>" % self._indent)<EOL>self._inc_indent()<EOL>self._write_attributes(GRAPH_ATTRS, **args)<EOL>


self._dec_indent()<EOL>self._stream.write("<STR_LIT>" % self._indent)<EOL>


self._stream.write('<STR_LIT>' % (self._indent, title))<EOL>self._write_attributes(NODE_ATTRS, **args)<EOL>self._stream.write("<STR_LIT>")<EOL>


def edge(self, from_node, to_node, edge_type="<STR_LIT>", **args):

self._stream.write(<EOL>'<STR_LIT>'<EOL>% (self._indent, edge_type, from_node, to_node)<EOL>)<EOL>self._write_attributes(EDGE_ATTRS, **args)<EOL>self._stream.write("<STR_LIT>")<EOL>


def _write_attributes(self, attributes_dict, **args):

for key, value in args.items():<EOL><INDENT>try:<EOL><INDENT>_type = attributes_dict[key]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise Exception(<EOL>"""<STR_LIT>"""<EOL>% (key, attributes_dict.keys())<EOL>)<EOL><DEDENT>if not _type:<EOL><INDENT>self._stream.write('<STR_LIT>' % (self._indent, key, value))<EOL><DEDENT>elif _type == <NUM_LIT:1>:<EOL><INDENT>self._stream.write("<STR_LIT>" % (self._indent, key, int(value)))<EOL><DEDENT>elif value in _type:<EOL><INDENT>self._stream.write("<STR_LIT>" % (self._indent, key, value))<EOL><DEDENT>else:<EOL><INDENT>raise Exception(<EOL>"""<STR_LIT>"""<EOL>% (value, key, _type)<EOL>)<EOL><DEDENT><DEDENT>


self._indent = "<STR_LIT>" % self._indent<EOL>


self._indent = self._indent[:-<NUM_LIT:2>]<EOL>


self.config = handler.config<EOL>self._set_default_options()<EOL>self.linker = linker<EOL>self.classdiagram = None<EOL>


title = node.name<EOL>if self.module_names:<EOL><INDENT>title = "<STR_LIT>" % (node.root().name, title)<EOL><DEDENT>return title<EOL>


<EOL>if option is None:<EOL><INDENT>return bool(self.config.classes)<EOL><DEDENT>return option<EOL>

activate some options if not explicitly deactivated


self.module_names = self._set_option(self.config.module_names)<EOL>all_ancestors = self._set_option(self.config.all_ancestors)<EOL>all_associated = self._set_option(self.config.all_associated)<EOL>anc_level, association_level = (<NUM_LIT:0>, <NUM_LIT:0>)<EOL>if all_ancestors:<EOL><INDENT>anc_level = -<NUM_LIT:1><EOL><DEDENT>if all_associated:<EOL><INDENT>association_level = -<NUM_LIT:1><EOL><DEDENT>if self.config.show_ancestors is not None:<EOL><INDENT>anc_level = self.config.show_ancestors<EOL><DEDENT>if self.config.show_associated is not None:<EOL><INDENT>association_level = self.config.show_associated<EOL><DEDENT>self.anc_level, self.association_level = anc_level, association_level<EOL>

set different default options with _default dictionary


return self.anc_level, self.association_level<EOL>


if self.config.show_builtin:<EOL><INDENT>return True<EOL><DEDENT>return node.root().name != BUILTINS_NAME<EOL>


self.linker.visit(node)<EOL>self.classdiagram.add_object(self.get_title(node), node)<EOL>


if level == <NUM_LIT:0>:<EOL><INDENT>return<EOL><DEDENT>for ancestor in node.ancestors(recurs=False):<EOL><INDENT>if not self.show_node(ancestor):<EOL><INDENT>continue<EOL><DEDENT>yield ancestor<EOL><DEDENT>


def get_associated(self, klass_node, level):

if level == <NUM_LIT:0>:<EOL><INDENT>return<EOL><DEDENT>for association_nodes in list(klass_node.instance_attrs_type.values()) + list(<EOL>klass_node.locals_type.values()<EOL>):<EOL><INDENT>for node in association_nodes:<EOL><INDENT>if isinstance(node, astroid.Instance):<EOL><INDENT>node = node._proxied<EOL><DEDENT>if not (isinstance(node, astroid.ClassDef) and self.show_node(node)):<EOL><INDENT>continue<EOL><DEDENT>yield node<EOL><DEDENT><DEDENT>


def extract_classes(self, klass_node, anc_level, association_level):

if self.classdiagram.has_node(klass_node) or not self.show_node(klass_node):<EOL><INDENT>return<EOL><DEDENT>self.add_class(klass_node)<EOL>for ancestor in self.get_ancestors(klass_node, anc_level):<EOL><INDENT>self.extract_classes(ancestor, anc_level - <NUM_LIT:1>, association_level)<EOL><DEDENT>for node in self.get_associated(klass_node, association_level):<EOL><INDENT>self.extract_classes(node, anc_level, association_level - <NUM_LIT:1>)<EOL><DEDENT>

extract recursively classes related to klass_node


mode = self.config.mode<EOL>if len(node.modules) > <NUM_LIT:1>:<EOL><INDENT>self.pkgdiagram = PackageDiagram("<STR_LIT>" % node.name, mode)<EOL><DEDENT>else:<EOL><INDENT>self.pkgdiagram = None<EOL><DEDENT>self.classdiagram = ClassDiagram("<STR_LIT>" % node.name, mode)<EOL>

visit a pyreverse.utils.Project node

        create a diagram definition for packages


if self.pkgdiagram:<EOL><INDENT>return self.pkgdiagram, self.classdiagram<EOL><DEDENT>return (self.classdiagram,)<EOL>

leave the pyreverse.utils.Project node

        return the generated diagram definition


if self.pkgdiagram:<EOL><INDENT>self.linker.visit(node)<EOL>self.pkgdiagram.add_object(node.name, node)<EOL><DEDENT>

visit an astroid.Module node

        add this class to the package diagram definition


anc_level, association_level = self._get_levels()<EOL>self.extract_classes(node, anc_level, association_level)<EOL>

visit an astroid.Class node

        add this class to the class diagram definition


if self.pkgdiagram:<EOL><INDENT>self.pkgdiagram.add_from_depend(node, node.modname)<EOL><DEDENT>

visit astroid.ImportFrom  and catch modules for package diagram


def class_diagram(self, project, klass):

self.classdiagram = ClassDiagram(klass, self.config.mode)<EOL>if len(project.modules) > <NUM_LIT:1>:<EOL><INDENT>module, klass = klass.rsplit("<STR_LIT:.>", <NUM_LIT:1>)<EOL>module = project.get_module(module)<EOL><DEDENT>else:<EOL><INDENT>module = project.modules[<NUM_LIT:0>]<EOL>klass = klass.split("<STR_LIT:.>")[-<NUM_LIT:1>]<EOL><DEDENT>klass = next(module.ilookup(klass))<EOL>anc_level, association_level = self._get_levels()<EOL>self.extract_classes(klass, anc_level, association_level)<EOL>return self.classdiagram<EOL>

return a class diagram definition for the given klass and its
        related klasses


<EOL>diagrams = []<EOL>generator = ClassDiadefGenerator(linker, self)<EOL>for klass in self.config.classes:<EOL><INDENT>diagrams.append(generator.class_diagram(project, klass))<EOL><DEDENT>if not diagrams:<EOL><INDENT>diagrams = DefaultDiadefGenerator(linker, self).visit(project)<EOL><DEDENT>for diagram in diagrams:<EOL><INDENT>diagram.extract_relationships()<EOL><DEDENT>return diagrams<EOL>

Get the diagrams configuration data

        :param project:The pyreverse project
        :type project: pyreverse.utils.Project
        :param linker: The linker
        :type linker: pyreverse.inspector.Linker(IdGeneratorMixIn, LocalsVisitor)

        :returns: The list of diagram definitions
        :rtype: list(:class:`pylint.pyreverse.diagrams.ClassDiagram`)


try:<EOL><INDENT>subprocess.call(["<STR_LIT>", "<STR_LIT>"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<EOL><DEDENT>except OSError:<EOL><INDENT>print(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % output_format<EOL>)<EOL>sys.exit(<NUM_LIT:32>)<EOL><DEDENT>

check if we need graphviz for different output format


if not args:<EOL><INDENT>print(self.help())<EOL>return <NUM_LIT:1><EOL><DEDENT>sys.path.insert(<NUM_LIT:0>, os.getcwd())<EOL>try:<EOL><INDENT>project = project_from_files(<EOL>args,<EOL>project_name=self.config.project,<EOL>black_list=self.config.black_list,<EOL>)<EOL>linker = Linker(project, tag=True)<EOL>handler = DiadefsHandler(self.config)<EOL>diadefs = handler.get_diadefs(project, linker)<EOL><DEDENT>finally:<EOL><INDENT>sys.path.pop(<NUM_LIT:0>)<EOL><DEDENT>if self.config.output_format == "<STR_LIT>":<EOL><INDENT>writer.VCGWriter(self.config).write(diadefs)<EOL><DEDENT>else:<EOL><INDENT>writer.DotWriter(self.config).write(diadefs)<EOL><DEDENT>return <NUM_LIT:0><EOL>


for diagram in diadefs:<EOL><INDENT>basename = diagram.title.strip().replace("<STR_LIT:U+0020>", "<STR_LIT:_>")<EOL>file_name = "<STR_LIT>" % (basename, self.config.output_format)<EOL>self.set_printer(file_name, basename)<EOL>if diagram.TYPE == "<STR_LIT:class>":<EOL><INDENT>self.write_classes(diagram)<EOL><DEDENT>else:<EOL><INDENT>self.write_packages(diagram)<EOL><DEDENT>self.close_graph()<EOL><DEDENT>

write files for <project> according to <diadefs>


<EOL>for i, obj in enumerate(sorted(diagram.modules(), key=lambda x: x.title)):<EOL><INDENT>self.printer.emit_node(i, label=self.get_title(obj), shape="<STR_LIT>")<EOL>obj.fig_id = i<EOL><DEDENT>for rel in diagram.get_relationships("<STR_LIT>"):<EOL><INDENT>self.printer.emit_edge(<EOL>rel.from_object.fig_id, rel.to_object.fig_id, **self.pkg_edges<EOL>)<EOL><DEDENT>


<EOL>for i, obj in enumerate(sorted(diagram.objects, key=lambda x: x.title)):<EOL><INDENT>self.printer.emit_node(i, **self.get_values(obj))<EOL>obj.fig_id = i<EOL><DEDENT>for rel in diagram.get_relationships("<STR_LIT>"):<EOL><INDENT>self.printer.emit_edge(<EOL>rel.from_object.fig_id, rel.to_object.fig_id, **self.inh_edges<EOL>)<EOL><DEDENT>for rel in diagram.get_relationships("<STR_LIT>"):<EOL><INDENT>self.printer.emit_edge(<EOL>rel.from_object.fig_id, rel.to_object.fig_id, **self.imp_edges<EOL>)<EOL><DEDENT>for rel in diagram.get_relationships("<STR_LIT>"):<EOL><INDENT>self.printer.emit_edge(<EOL>rel.from_object.fig_id,<EOL>rel.to_object.fig_id,<EOL>label=rel.name,<EOL>**self.association_edges<EOL>)<EOL><DEDENT>


def set_printer(self, file_name, basename):

raise NotImplementedError<EOL>


raise NotImplementedError<EOL>


layout = dict(rankdir="<STR_LIT>")<EOL>self.printer = DotBackend(basename, additional_param=layout)<EOL>self.file_name = file_name<EOL>

initialize DotWriter and add options for layout.


return obj.title<EOL>


label = obj.title<EOL>if obj.shape == "<STR_LIT>":<EOL><INDENT>label = "<STR_LIT>" % label<EOL><DEDENT>if not self.config.only_classnames:<EOL><INDENT>label = r"<STR_LIT>" % (label, r"<STR_LIT>".join(obj.attrs))<EOL>for func in obj.methods:<EOL><INDENT>args = [arg.name for arg in func.args.args if arg.name != "<STR_LIT>"]<EOL>label = r"<STR_LIT>" % (label, func.name, "<STR_LIT:U+002CU+0020>".join(args))<EOL><DEDENT>label = "<STR_LIT>" % label<EOL><DEDENT>if is_exception(obj.node):<EOL><INDENT>return dict(fontcolor="<STR_LIT>", label=label, shape="<STR_LIT>")<EOL><DEDENT>return dict(label=label, shape="<STR_LIT>")<EOL>

get label and shape for classes.

        The label contains all attributes and methods


self.printer.generate(self.file_name)<EOL>


self.graph_file = open(file_name, "<STR_LIT>")<EOL>self.printer = VCGPrinter(self.graph_file)<EOL>self.printer.open_graph(<EOL>title=basename,<EOL>layoutalgorithm="<STR_LIT>",<EOL>late_edge_labels="<STR_LIT:yes>",<EOL>port_sharing="<STR_LIT>",<EOL>manhattan_edges="<STR_LIT:yes>",<EOL>)<EOL>self.printer.emit_node = self.printer.node<EOL>self.printer.emit_edge = self.printer.edge<EOL>


return r"<STR_LIT>" % obj.title<EOL>


if is_exception(obj.node):<EOL><INDENT>label = r"<STR_LIT>" % obj.title<EOL><DEDENT>else:<EOL><INDENT>label = r"<STR_LIT>" % obj.title<EOL><DEDENT>if obj.shape == "<STR_LIT>":<EOL><INDENT>shape = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>shape = "<STR_LIT>"<EOL><DEDENT>if not self.config.only_classnames:<EOL><INDENT>attrs = obj.attrs<EOL>methods = [func.name for func in obj.methods]<EOL>maxlen = max(len(name) for name in [obj.title] + methods + attrs)<EOL>line = "<STR_LIT:_>" * (maxlen + <NUM_LIT:2>)<EOL>label = r"<STR_LIT>" % (label, line)<EOL>for attr in attrs:<EOL><INDENT>label = r"<STR_LIT>" % (label, attr)<EOL><DEDENT>if attrs:<EOL><INDENT>label = r"<STR_LIT>" % (label, line)<EOL><DEDENT>for func in methods:<EOL><INDENT>label = r"<STR_LIT>" % (label, func)<EOL><DEDENT><DEDENT>return dict(label=label, shape=shape)<EOL>


self.printer.close_graph()<EOL>self.graph_file.close()<EOL>


options = []<EOL>home = os.environ.get("<STR_LIT>", "<STR_LIT>")<EOL>if home:<EOL><INDENT>rcfile = os.path.join(home, RCFILE)<EOL>try:<EOL><INDENT>options = open(rcfile).read().split()<EOL><DEDENT>except IOError:<EOL><INDENT>pass  <EOL><DEDENT><DEDENT>return options<EOL>

Read config file and return list of options


options = get_default_options()<EOL>options.reverse()<EOL>for arg in options:<EOL><INDENT>sys.argv.insert(<NUM_LIT:1>, arg)<EOL><DEDENT>


if SPECIAL.match(name):<EOL><INDENT>visibility = "<STR_LIT>"<EOL><DEDENT>elif PRIVATE.match(name):<EOL><INDENT>visibility = "<STR_LIT>"<EOL><DEDENT>elif PROTECTED.match(name):<EOL><INDENT>visibility = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>visibility = "<STR_LIT>"<EOL><DEDENT>return visibility<EOL>

return the visibility from a name: public, protected, private or special


return ABSTRACT.match(node.name)<EOL>

return true if the given class node correspond to an abstract class
    definition


return FINAL.match(node.name)<EOL>

return true if the given class/function node correspond to final
    definition


__mode = <NUM_LIT:0><EOL>for nummod in mode.split("<STR_LIT:+>"):<EOL><INDENT>try:<EOL><INDENT>__mode += MODES[nummod]<EOL><DEDENT>except KeyError as ex:<EOL><INDENT>print("<STR_LIT>" % ex, file=sys.stderr)<EOL><DEDENT><DEDENT>self.__mode = __mode<EOL>


visibility = get_visibility(getattr(node, "<STR_LIT:name>", node))<EOL>return not self.__mode & VIS_MOD[visibility]<EOL>

return true if the node should be treated


if _done is None:<EOL><INDENT>_done = set()<EOL><DEDENT>if node in _done:<EOL><INDENT>raise AssertionError((id(node), node, node.parent))<EOL><DEDENT>_done.add(node)<EOL>self.visit(node)<EOL>for child_node in node.get_children():<EOL><INDENT>assert child_node is not node<EOL>self.walk(child_node, _done)<EOL><DEDENT>self.leave(node)<EOL>assert node.parent is not node<EOL>

walk on the tree from <node>, getting callbacks from handler


klass = node.__class__<EOL>methods = self._cache.get(klass)<EOL>if methods is None:<EOL><INDENT>handler = self.handler<EOL>kid = klass.__name__.lower()<EOL>e_method = getattr(<EOL>handler, "<STR_LIT>" % kid, getattr(handler, "<STR_LIT>", None)<EOL>)<EOL>l_method = getattr(<EOL>handler, "<STR_LIT>" % kid, getattr(handler, "<STR_LIT>", None)<EOL>)<EOL>self._cache[klass] = (e_method, l_method)<EOL><DEDENT>else:<EOL><INDENT>e_method, l_method = methods<EOL><DEDENT>return e_method, l_method<EOL>

get callbacks from handler for the visited node


method = self.get_callbacks(node)[<NUM_LIT:0>]<EOL>if method is not None:<EOL><INDENT>method(node)<EOL><DEDENT>


method = self.get_callbacks(node)[<NUM_LIT:1>]<EOL>if method is not None:<EOL><INDENT>method(node)<EOL><DEDENT>


if node in self._visited:<EOL><INDENT>return None<EOL><DEDENT>self._visited[node] = <NUM_LIT:1>  <EOL>methods = self.get_callbacks(node)<EOL>if methods[<NUM_LIT:0>] is not None:<EOL><INDENT>methods[<NUM_LIT:0>](node)<EOL><DEDENT>if hasattr(node, "<STR_LIT>"):  <EOL><INDENT>for local_node in node.values():<EOL><INDENT>self.visit(local_node)<EOL><DEDENT><DEDENT>if methods[<NUM_LIT:1>] is not None:<EOL><INDENT>return methods[<NUM_LIT:1>](node)<EOL><DEDENT>return None<EOL>

launch the visit starting from the given node


return True<EOL>

Handler used by interfaces to handle suspicious interface nodes.


def interfaces(node, herited=True, handler_func=_iface_hdlr):

<EOL>try:<EOL><INDENT>implements = bases.Instance(node).getattr("<STR_LIT>")[<NUM_LIT:0>]<EOL><DEDENT>except exceptions.NotFoundError:<EOL><INDENT>return<EOL><DEDENT>if not herited and implements.frame() is not node:<EOL><INDENT>return<EOL><DEDENT>found = set()<EOL>missing = False<EOL>for iface in node_classes.unpack_infer(implements):<EOL><INDENT>if iface is astroid.Uninferable:<EOL><INDENT>missing = True<EOL>continue<EOL><DEDENT>if iface not in found and handler_func(iface):<EOL><INDENT>found.add(iface)<EOL>yield iface<EOL><DEDENT><DEDENT>if missing:<EOL><INDENT>raise exceptions.InferenceError()<EOL><DEDENT>

Return an iterator on interfaces implemented by the given class node.


def project_from_files(<EOL>files, func_wrapper=_astroid_wrapper, project_name="<STR_LIT>", black_list=("<STR_LIT>",)<EOL>):

<EOL>astroid_manager = manager.AstroidManager()<EOL>project = Project(project_name)<EOL>for something in files:<EOL><INDENT>if not os.path.exists(something):<EOL><INDENT>fpath = modutils.file_from_modpath(something.split("<STR_LIT:.>"))<EOL><DEDENT>elif os.path.isdir(something):<EOL><INDENT>fpath = os.path.join(something, "<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>fpath = something<EOL><DEDENT>ast = func_wrapper(astroid_manager.ast_from_file, fpath)<EOL>if ast is None:<EOL><INDENT>continue<EOL><DEDENT>project.path = project.path or ast.file<EOL>project.add_module(ast)<EOL>base_name = ast.name<EOL>if ast.package and something.find("<STR_LIT>") == -<NUM_LIT:1>:<EOL><INDENT>for fpath in modutils.get_module_files(<EOL>os.path.dirname(ast.file), black_list<EOL>):<EOL><INDENT>ast = func_wrapper(astroid_manager.ast_from_file, fpath)<EOL>if ast is None or ast.name == base_name:<EOL><INDENT>continue<EOL><DEDENT>project.add_module(ast)<EOL><DEDENT><DEDENT><DEDENT>return project<EOL>

return a Project from a list of files or modules


self.id_count = start_value<EOL>


self.id_count += <NUM_LIT:1><EOL>return self.id_count<EOL>


if self.tag:<EOL><INDENT>node.uid = self.generate_id()<EOL><DEDENT>for module in node.modules:<EOL><INDENT>self.visit(module)<EOL><DEDENT>

visit a pyreverse.utils.Project node

         * optionally tag the node with a unique id


if self.tag:<EOL><INDENT>node.uid = self.generate_id()<EOL><DEDENT>for subelmt in node.values():<EOL><INDENT>self.visit(subelmt)<EOL><DEDENT>

visit an astroid.Package node

         * optionally tag the node with a unique id


if hasattr(node, "<STR_LIT>"):<EOL><INDENT>return<EOL><DEDENT>node.locals_type = collections.defaultdict(list)<EOL>node.depends = []<EOL>if self.tag:<EOL><INDENT>node.uid = self.generate_id()<EOL><DEDENT>

visit an astroid.Module node

         * set the locals_type mapping
         * set the depends mapping
         * optionally tag the node with a unique id


if hasattr(node, "<STR_LIT>"):<EOL><INDENT>return<EOL><DEDENT>node.locals_type = collections.defaultdict(list)<EOL>if self.tag:<EOL><INDENT>node.uid = self.generate_id()<EOL><DEDENT>for baseobj in node.ancestors(recurs=False):<EOL><INDENT>specializations = getattr(baseobj, "<STR_LIT>", [])<EOL>specializations.append(node)<EOL>baseobj.specializations = specializations<EOL><DEDENT>node.instance_attrs_type = collections.defaultdict(list)<EOL>for assignattrs in node.instance_attrs.values():<EOL><INDENT>for assignattr in assignattrs:<EOL><INDENT>self.handle_assignattr_type(assignattr, node)<EOL><DEDENT><DEDENT>try:<EOL><INDENT>node.implements = list(interfaces(node, self.inherited_interfaces))<EOL><DEDENT>except astroid.InferenceError:<EOL><INDENT>node.implements = ()<EOL><DEDENT>

visit an astroid.Class node

         * set the locals_type and instance_attrs_type mappings
         * set the implements list and build it
         * optionally tag the node with a unique id


if hasattr(node, "<STR_LIT>"):<EOL><INDENT>return<EOL><DEDENT>node.locals_type = collections.defaultdict(list)<EOL>if self.tag:<EOL><INDENT>node.uid = self.generate_id()<EOL><DEDENT>

visit an astroid.Function node

         * set the locals_type mapping
         * optionally tag the node with a unique id


<EOL>if hasattr(node, "<STR_LIT>"):<EOL><INDENT>return<EOL><DEDENT>node._handled = True<EOL>if node.name in node.frame():<EOL><INDENT>frame = node.frame()<EOL><DEDENT>else:<EOL><INDENT>frame = node.root()<EOL><DEDENT>try:<EOL><INDENT>if not hasattr(frame, "<STR_LIT>"):<EOL><INDENT>if isinstance(frame, astroid.ClassDef):<EOL><INDENT>self.visit_classdef(frame)<EOL><DEDENT>elif isinstance(frame, astroid.FunctionDef):<EOL><INDENT>self.visit_functiondef(frame)<EOL><DEDENT>else:<EOL><INDENT>self.visit_module(frame)<EOL><DEDENT><DEDENT>current = frame.locals_type[node.name]<EOL>values = set(node.infer())<EOL>frame.locals_type[node.name] = list(set(current) | values)<EOL><DEDENT>except astroid.InferenceError:<EOL><INDENT>pass<EOL><DEDENT>

visit an astroid.AssignName node

        handle locals_type


try:<EOL><INDENT>values = set(node.infer())<EOL>current = set(parent.instance_attrs_type[node.attrname])<EOL>parent.instance_attrs_type[node.attrname] = list(current | values)<EOL><DEDENT>except astroid.InferenceError:<EOL><INDENT>pass<EOL><DEDENT>

handle an astroid.assignattr node

        handle instance_attrs_type


context_file = node.root().file<EOL>for name in node.names:<EOL><INDENT>relative = modutils.is_relative(name[<NUM_LIT:0>], context_file)<EOL>self._imported_module(node, name[<NUM_LIT:0>], relative)<EOL><DEDENT>

visit an astroid.Import node

        resolve module dependencies


basename = node.modname<EOL>context_file = node.root().file<EOL>if context_file is not None:<EOL><INDENT>relative = modutils.is_relative(basename, context_file)<EOL><DEDENT>else:<EOL><INDENT>relative = False<EOL><DEDENT>for name in node.names:<EOL><INDENT>if name[<NUM_LIT:0>] == "<STR_LIT:*>":<EOL><INDENT>continue<EOL><DEDENT>fullname = "<STR_LIT>" % (basename, name[<NUM_LIT:0>])<EOL>if fullname.find("<STR_LIT:.>") > -<NUM_LIT:1>:<EOL><INDENT>try:<EOL><INDENT>fullname = modutils.get_module_part(fullname, context_file)<EOL><DEDENT>except ImportError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>if fullname != basename:<EOL><INDENT>self._imported_module(node, fullname, relative)<EOL><DEDENT><DEDENT>

visit an astroid.ImportFrom node

        resolve module dependencies


def compute_module(self, context_name, mod_path):

package_dir = os.path.dirname(self.project.path)<EOL>if context_name == mod_path:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>if modutils.is_standard_module(mod_path, (package_dir,)):<EOL><INDENT>return <NUM_LIT:1><EOL><DEDENT>return <NUM_LIT:0><EOL>

return true if the module should be added to dependencies


def _imported_module(self, node, mod_path, relative):

module = node.root()<EOL>context_name = module.name<EOL>if relative:<EOL><INDENT>mod_path = "<STR_LIT>" % ("<STR_LIT:.>".join(context_name.split("<STR_LIT:.>")[:-<NUM_LIT:1>]), mod_path)<EOL><DEDENT>if self.compute_module(context_name, mod_path):<EOL><INDENT>if not hasattr(module, "<STR_LIT>"):<EOL><INDENT>module.depends = []<EOL><DEDENT>mod_paths = module.depends<EOL>if mod_path not in mod_paths:<EOL><INDENT>mod_paths.append(mod_path)<EOL><DEDENT><DEDENT>

Notify an imported module, used to analyze dependencies


def add_relationship(self, from_object, to_object, relation_type, name=None):

rel = Relationship(from_object, to_object, relation_type, name)<EOL>self.relationships.setdefault(relation_type, []).append(rel)<EOL>


def get_relationship(self, from_object, relation_type):

for rel in self.relationships.get(relation_type, ()):<EOL><INDENT>if rel.from_object is from_object:<EOL><INDENT>return rel<EOL><DEDENT><DEDENT>raise KeyError(relation_type)<EOL>


attrs = []<EOL>properties = [<EOL>(n, m)<EOL>for n, m in node.items()<EOL>if isinstance(m, astroid.FunctionDef) and decorated_with_property(m)<EOL>]<EOL>for node_name, associated_nodes in (<EOL>list(node.instance_attrs_type.items())<EOL>+ list(node.locals_type.items())<EOL>+ properties<EOL>):<EOL><INDENT>if not self.show_attr(node_name):<EOL><INDENT>continue<EOL><DEDENT>names = self.class_names(associated_nodes)<EOL>if names:<EOL><INDENT>node_name = "<STR_LIT>" % (node_name, "<STR_LIT:U+002CU+0020>".join(names))<EOL><DEDENT>attrs.append(node_name)<EOL><DEDENT>return sorted(attrs)<EOL>

return visible attributes, possibly with class name


methods = [<EOL>m<EOL>for m in node.values()<EOL>if isinstance(m, astroid.FunctionDef)<EOL>and not decorated_with_property(m)<EOL>and self.show_attr(m.name)<EOL>]<EOL>return sorted(methods, key=lambda n: n.name)<EOL>


assert node not in self._nodes<EOL>ent = DiagramEntity(title, node)<EOL>self._nodes[node] = ent<EOL>self.objects.append(ent)<EOL>


names = []<EOL>for node in nodes:<EOL><INDENT>if isinstance(node, astroid.Instance):<EOL><INDENT>node = node._proxied<EOL><DEDENT>if (<EOL>isinstance(node, astroid.ClassDef)<EOL>and hasattr(node, "<STR_LIT:name>")<EOL>and not self.has_node(node)<EOL>):<EOL><INDENT>if node.name not in names:<EOL><INDENT>node_name = node.name<EOL>names.append(node_name)<EOL><DEDENT><DEDENT><DEDENT>return names<EOL>


return self._nodes.keys()<EOL>


return node in self._nodes<EOL>

return true if the given node is included in the diagram


return self._nodes[node]<EOL>

return the diagram object mapped to node


return [o for o in self.objects if isinstance(o.node, astroid.ClassDef)]<EOL>


for klass in self.classes():<EOL><INDENT>if klass.node.name == name:<EOL><INDENT>return klass<EOL><DEDENT><DEDENT>raise KeyError(name)<EOL>

return a class by its name, raise KeyError if not found


for obj in self.classes():<EOL><INDENT>node = obj.node<EOL>obj.attrs = self.get_attrs(node)<EOL>obj.methods = self.get_methods(node)<EOL>if is_interface(node):<EOL><INDENT>obj.shape = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>obj.shape = "<STR_LIT:class>"<EOL><DEDENT>for par_node in node.ancestors(recurs=False):<EOL><INDENT>try:<EOL><INDENT>par_obj = self.object_from_node(par_node)<EOL>self.add_relationship(obj, par_obj, "<STR_LIT>")<EOL><DEDENT>except KeyError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>for impl_node in node.implements:<EOL><INDENT>try:<EOL><INDENT>impl_obj = self.object_from_node(impl_node)<EOL>self.add_relationship(obj, impl_obj, "<STR_LIT>")<EOL><DEDENT>except KeyError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>for name, values in list(node.instance_attrs_type.items()) + list(<EOL>node.locals_type.items()<EOL>):<EOL><INDENT>for value in values:<EOL><INDENT>if value is astroid.Uninferable:<EOL><INDENT>continue<EOL><DEDENT>if isinstance(value, astroid.Instance):<EOL><INDENT>value = value._proxied<EOL><DEDENT>try:<EOL><INDENT>associated_obj = self.object_from_node(value)<EOL>self.add_relationship(associated_obj, obj, "<STR_LIT>", name)<EOL><DEDENT>except KeyError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT><DEDENT><DEDENT>

extract relation ships between nodes in the diagram


return [o for o in self.objects if isinstance(o.node, astroid.Module)]<EOL>


for mod in self.modules():<EOL><INDENT>if mod.node.name == name:<EOL><INDENT>return mod<EOL><DEDENT><DEDENT>raise KeyError(name)<EOL>

return a module by its name, raise KeyError if not found


for mod in self.modules():<EOL><INDENT>mod_name = mod.node.name<EOL>if mod_name == name:<EOL><INDENT>return mod<EOL><DEDENT>package = node.root().name<EOL>if mod_name == "<STR_LIT>" % (package, name):<EOL><INDENT>return mod<EOL><DEDENT>if mod_name == "<STR_LIT>" % (package.rsplit("<STR_LIT:.>", <NUM_LIT:1>)[<NUM_LIT:0>], name):<EOL><INDENT>return mod<EOL><DEDENT><DEDENT>raise KeyError(name)<EOL>

return a module by its name, looking also for relative imports;
        raise KeyError if not found


def add_from_depend(self, node, from_module):

mod_name = node.root().name<EOL>obj = self.module(mod_name)<EOL>if from_module not in obj.node.depends:<EOL><INDENT>obj.node.depends.append(from_module)<EOL><DEDENT>

add dependencies created by from-imports


ClassDiagram.extract_relationships(self)<EOL>for obj in self.classes():<EOL><INDENT>try:<EOL><INDENT>mod = self.object_from_node(obj.node.root())<EOL>self.add_relationship(obj, mod, "<STR_LIT>")<EOL><DEDENT>except KeyError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>for obj in self.modules():<EOL><INDENT>obj.shape = "<STR_LIT>"<EOL>for dep_name in obj.node.depends:<EOL><INDENT>try:<EOL><INDENT>dep = self.get_module(dep_name, obj.node)<EOL><DEDENT>except KeyError:<EOL><INDENT>continue<EOL><DEDENT>self.add_relationship(obj, dep, "<STR_LIT>")<EOL><DEDENT><DEDENT>


basename = osp.basename(filename)<EOL>storedir = osp.dirname(osp.abspath(filename))<EOL>target = filename.split("<STR_LIT:.>")[-<NUM_LIT:1>]<EOL>return storedir, basename, target<EOL>

Transforms /some/path/foo.png into ('/some/path', 'foo.png', 'png').


return '<STR_LIT>' % nid<EOL>

Returns a suitable DOT node id for `nid`.


if not graph_dict:<EOL><INDENT>return ()<EOL><DEDENT>result = []<EOL>if vertices is None:<EOL><INDENT>vertices = graph_dict.keys()<EOL><DEDENT>for vertice in vertices:<EOL><INDENT>_get_cycles(graph_dict, [], set(), result, vertice)<EOL><DEDENT>return result<EOL>

given a dictionary representing an ordered graph (i.e. key are vertices
    and values is a list of destination vertices representing edges), return a
    list of detected cycles


def _get_cycles(graph_dict, path, visited, result, vertice):

if vertice in path:<EOL><INDENT>cycle = [vertice]<EOL>for node in path[::-<NUM_LIT:1>]:<EOL><INDENT>if node == vertice:<EOL><INDENT>break<EOL><DEDENT>cycle.insert(<NUM_LIT:0>, node)<EOL><DEDENT>start_from = min(cycle)<EOL>index = cycle.index(start_from)<EOL>cycle = cycle[index:] + cycle[<NUM_LIT:0>:index]<EOL>if cycle not in result:<EOL><INDENT>result.append(cycle)<EOL><DEDENT>return<EOL><DEDENT>path.append(vertice)<EOL>try:<EOL><INDENT>for node in graph_dict[vertice]:<EOL><INDENT>if node not in visited:<EOL><INDENT>_get_cycles(graph_dict, path, visited, result, node)<EOL>visited.add(node)<EOL><DEDENT><DEDENT><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>path.pop()<EOL>

recursive function doing the real work for get_cycles


if self._source is None:<EOL><INDENT>self.emit("<STR_LIT>")<EOL>self._source = "<STR_LIT:\n>".join(self.lines)<EOL>del self.lines<EOL><DEDENT>return self._source<EOL>


def generate(self, outputfile=None, dotfile=None, mapfile=None):

import subprocess  <EOL>name = self.graphname<EOL>if not dotfile:<EOL><INDENT>if outputfile and outputfile.endswith("<STR_LIT>"):<EOL><INDENT>dotfile = outputfile<EOL><DEDENT>else:<EOL><INDENT>dotfile = "<STR_LIT>" % name<EOL><DEDENT><DEDENT>if outputfile is not None:<EOL><INDENT>storedir, _, target = target_info_from_filename(outputfile)<EOL>if target != "<STR_LIT>":<EOL><INDENT>pdot, dot_sourcepath = tempfile.mkstemp("<STR_LIT>", name)<EOL>os.close(pdot)<EOL><DEDENT>else:<EOL><INDENT>dot_sourcepath = osp.join(storedir, dotfile)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>target = "<STR_LIT>"<EOL>pdot, dot_sourcepath = tempfile.mkstemp("<STR_LIT>", name)<EOL>ppng, outputfile = tempfile.mkstemp("<STR_LIT>", name)<EOL>os.close(pdot)<EOL>os.close(ppng)<EOL><DEDENT>pdot = codecs.open(dot_sourcepath, "<STR_LIT:w>", encoding="<STR_LIT:utf8>")<EOL>pdot.write(self.source)<EOL>pdot.close()<EOL>if target != "<STR_LIT>":<EOL><INDENT>use_shell = sys.platform == "<STR_LIT:win32>"<EOL>if mapfile:<EOL><INDENT>subprocess.call(<EOL>[<EOL>self.renderer,<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>mapfile,<EOL>"<STR_LIT>",<EOL>target,<EOL>dot_sourcepath,<EOL>"<STR_LIT>",<EOL>outputfile,<EOL>],<EOL>shell=use_shell,<EOL>)<EOL><DEDENT>else:<EOL><INDENT>subprocess.call(<EOL>[self.renderer, "<STR_LIT>", target, dot_sourcepath, "<STR_LIT>", outputfile],<EOL>shell=use_shell,<EOL>)<EOL><DEDENT>os.unlink(dot_sourcepath)<EOL><DEDENT>return outputfile<EOL>

Generates a graph file.

        :param str outputfile: filename and path [defaults to graphname.png]
        :param str dotfile: filename and path [defaults to graphname.dot]
        :param str mapfile: filename and path

        :rtype: str
        :return: a path to the generated file


self.lines.append(line)<EOL>


def emit_edge(self, name1, name2, **props):

attrs = ['<STR_LIT>' % (prop, value) for prop, value in props.items()]<EOL>n_from, n_to = normalize_node_id(name1), normalize_node_id(name2)<EOL>self.emit("<STR_LIT>" % (n_from, n_to, "<STR_LIT:U+002CU+0020>".join(sorted(attrs))))<EOL>

emit an edge from <name1> to <name2>.
        edge properties: see http://www.graphviz.org/doc/info/attrs.html


attrs = ['<STR_LIT>' % (prop, value) for prop, value in props.items()]<EOL>self.emit("<STR_LIT>" % (normalize_node_id(name), "<STR_LIT:U+002CU+0020>".join(sorted(attrs))))<EOL>

emit a node with given properties.
        node properties: see http://www.graphviz.org/doc/info/attrs.html


<EOL>base_path = os.path.dirname(<EOL>os.path.dirname(os.path.dirname(os.path.abspath(__file__)))<EOL>)<EOL>ext_path = os.path.join(base_path, "<STR_LIT>", "<STR_LIT>")<EOL>modules = []<EOL>doc_files = {}<EOL>for filename in os.listdir(ext_path):<EOL><INDENT>name, ext = os.path.splitext(filename)<EOL>if name[<NUM_LIT:0>] == "<STR_LIT:_>" or name in DEPRECATED_MODULES:<EOL><INDENT>continue<EOL><DEDENT>if ext == "<STR_LIT>":<EOL><INDENT>modules.append("<STR_LIT>" % name)<EOL><DEDENT>elif ext == "<STR_LIT>":<EOL><INDENT>doc_files["<STR_LIT>" + name] = os.path.join(ext_path, filename)<EOL><DEDENT><DEDENT>modules.sort()<EOL>if not modules:<EOL><INDENT>sys.exit("<STR_LIT>")<EOL><DEDENT>linter = PyLinter()<EOL>linter.load_plugin_modules(modules)<EOL>extensions_doc = os.path.join(<EOL>base_path, "<STR_LIT>", "<STR_LIT>", "<STR_LIT>"<EOL>)<EOL>with open(extensions_doc, "<STR_LIT:w>") as stream:<EOL><INDENT>stream.write("<STR_LIT>")<EOL>stream.write("<STR_LIT>")<EOL>stream.write("<STR_LIT>")<EOL>for module in modules:<EOL><INDENT>stream.write("<STR_LIT>".format(module))<EOL><DEDENT>stream.write("<STR_LIT:\n>")<EOL>stream.write(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL>stream.write(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL>by_module = get_plugins_info(linter, doc_files)<EOL>for module, info in sorted(by_module.items()):<EOL><INDENT>linter._print_checker_doc(info["<STR_LIT:name>"], info, stream=stream)<EOL><DEDENT><DEDENT>

Output full documentation in ReST format for all extension modules


linter.register_checker(MyAstroidChecker(linter))<EOL>

This required method auto registers the checker.

    :param linter: The linter to register the checker to.
    :type linter: pylint.lint.PyLinter


if not (<EOL>isinstance(node.func, astroid.Attribute)<EOL>and isinstance(node.func.expr, astroid.Name)<EOL>and node.func.expr.name == self.config.store_locals_indicator<EOL>and node.func.attrname == "<STR_LIT>"<EOL>):<EOL><INDENT>return<EOL><DEDENT>in_class = node.frame()<EOL>for param in node.args:<EOL><INDENT>in_class.locals[param.name] = node<EOL><DEDENT>

Called when a :class:`.astroid.node_classes.Call` node is visited.

        See :mod:`astroid` for the description of available nodes.

        :param node: The node to check.
        :type node: astroid.node_classes.Call


linter.register_checker(MyRawChecker(linter))<EOL>

required method to auto register this checker


with node.stream() as stream:<EOL><INDENT>for (lineno, line) in enumerate(stream):<EOL><INDENT>if line.rstrip().endswith("<STR_LIT:\\>"):<EOL><INDENT>self.add_message("<STR_LIT>", line=lineno)<EOL><DEDENT><DEDENT><DEDENT>

process a module

        the module's content is accessible via node.stream() function


from distutils import util<EOL>if util.get_platform()[:<NUM_LIT:3>] == "<STR_LIT>":<EOL><INDENT>return linux_scripts + [script + "<STR_LIT>" for script in linux_scripts]<EOL><DEDENT>return linux_scripts<EOL>

Creates the proper script names required for each platform
    (taken from 4Suite)


result = []<EOL>for package in os.listdir(directory):<EOL><INDENT>absfile = join(directory, package)<EOL>if isdir(absfile):<EOL><INDENT>if exists(join(absfile, "<STR_LIT>")):<EOL><INDENT>if prefix:<EOL><INDENT>result.append("<STR_LIT>" % (prefix, package))<EOL><DEDENT>else:<EOL><INDENT>result.append(package)<EOL><DEDENT>result += get_packages(absfile, result[-<NUM_LIT:1>])<EOL><DEDENT><DEDENT><DEDENT>return result<EOL>

return a list of subpackages for the given directory


if USE_SETUPTOOLS:<EOL><INDENT>if "<STR_LIT>" in sys.argv:<EOL><INDENT>sys.argv.remove("<STR_LIT>")<EOL><DEDENT><DEDENT>packages = [modname] + get_packages(join(base_dir, "<STR_LIT>"), modname)<EOL>if USE_SETUPTOOLS:<EOL><INDENT>if install_requires:<EOL><INDENT>kwargs["<STR_LIT>"] = install_requires<EOL>kwargs["<STR_LIT>"] = dependency_links<EOL><DEDENT>kwargs["<STR_LIT>"] = {<EOL>"<STR_LIT>": [<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>]<EOL>}<EOL><DEDENT>kwargs["<STR_LIT>"] = packages<EOL>cmdclass = {"<STR_LIT>": MyInstallLib, "<STR_LIT>": build_py}<EOL>if easy_install_lib:<EOL><INDENT>cmdclass["<STR_LIT>"] = easy_install<EOL><DEDENT>return setup(<EOL>name=distname,<EOL>version=__pkginfo__["<STR_LIT:version>"],<EOL>license=__pkginfo__["<STR_LIT>"],<EOL>description=__pkginfo__["<STR_LIT:description>"],<EOL>long_description=long_description,<EOL>author=__pkginfo__["<STR_LIT>"],<EOL>author_email=__pkginfo__["<STR_LIT>"],<EOL>url=__pkginfo__["<STR_LIT>"],<EOL>scripts=ensure_scripts(scripts),<EOL>classifiers=__pkginfo__["<STR_LIT>"],<EOL>data_files=data_files,<EOL>ext_modules=ext_modules,<EOL>cmdclass=cmdclass,<EOL>extras_require=extras_require,<EOL>test_suite="<STR_LIT:test>",<EOL>python_requires="<STR_LIT>",<EOL>setup_requires=["<STR_LIT>"],<EOL>tests_require=["<STR_LIT>"],<EOL>**kwargs<EOL>)<EOL>


install_lib.install_lib.run(self)<EOL>if include_dirs:<EOL><INDENT>for directory in include_dirs:<EOL><INDENT>dest = join(self.install_dir, directory)<EOL>if sys.version_info >= (<NUM_LIT:3>, <NUM_LIT:0>):<EOL><INDENT>exclude = {"<STR_LIT>", "<STR_LIT>"}<EOL><DEDENT>else:<EOL><INDENT>exclude = set()<EOL><DEDENT>shutil.rmtree(dest, ignore_errors=True)<EOL>shutil.copytree(<EOL>directory, dest, ignore=shutil.ignore_patterns(*exclude)<EOL>)<EOL><DEDENT><DEDENT>


index_record = index_series_tuple[<NUM_LIT:0>]<EOL>series = index_series_tuple[<NUM_LIT:1>]<EOL>record = Record()<EOL>record.series = series<EOL>record.index_record = index_record<EOL>record.set_attributes(kwargs)<EOL>return record<EOL>

Generates an instance of Record() from a tuple of the form (index, pandas.Series)
with associated parameters kwargs

Paremeters
----------
index_series_tuple : tuple
    tuple consisting of (index, pandas.Series)
kwargs : dict
    aditional arguments

Returns
-------
Record : object


print('<STR_LIT>')<EOL>df['<STR_LIT>'] = df.index<EOL>df.reset_index(drop=True, inplace=True)<EOL>if pd.__version__ >= '<STR_LIT>':<EOL><INDENT>d = df.T.to_dict(orient='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>d = df.T.to_dict(outtype='<STR_LIT>')<EOL><DEDENT>collection = [load_record(item, kwargs) for item in list(d.items())]<EOL>return collection<EOL>

Generates a list of Record objects given a DataFrame.
Each Record instance has a series attribute which is a pandas.Series of the same attributes
in the DataFrame.
Optional data can be passed in through kwargs which will be included by the name of each object.

parameters
----------
df : pandas.DataFrame
kwargs : alternate arguments to be saved by name to the series of each object

Returns
-------
collection : list
    list of Record objects where each Record represents one row from a dataframe

Examples
--------
This is how we generate a Record Collection from a DataFrame.

>>> import pandas as pd
>>> import turntable
>>>
>>> df = pd.DataFrame({'Artist':"""Michael Jackson, Pink Floyd, Whitney Houston, Meat Loaf,
    Eagles, Fleetwood Mac, Bee Gees, AC/DC""".split(', '),
>>> 'Album' :"""Thriller, The Dark Side of the Moon, The Bodyguard, Bat Out of Hell,
    Their Greatest Hits (1971-1975), Rumours, Saturday Night Fever, Back in Black""".split(', ')})
>>> collection = turntable.press.build_collection(df, my_favorite_record = 'nevermind')
>>> record = collection[0]
>>> print record.series


return pd.concat([record.series for record in collection], axis=<NUM_LIT:1>).T<EOL>

Converts a collection back into a pandas DataFrame

parameters
----------
collection : list
    list of Record objects where each Record represents one row from a dataframe

Returns
-------
df : pandas.DataFrame
    DataFrame of length=len(collection) where each row represents one Record


collection = build_collection(df)<EOL>collection = turntable.spin.batch(collection, method)<EOL>return collection_to_df(collection)<EOL>

Runs the full turntable process on a pandas DataFrame

parameters
----------
df : pandas.DataFrame
    each row represents a record
method : def method(record)
    function used to process each row

Returns
-------
df : pandas.DataFrame
    DataFrame processed by method

Example
-------
>>> import pandas as pd
>>> import turntable
>>>
>>> df = pd.DataFrame({'Artist':"""Michael Jackson, Pink Floyd, Whitney Houston, Meat Loaf, Eagles, Fleetwood Mac, Bee Gees, AC/DC""".split(', '), 'Album':"""Thriller, The Dark Side of the Moon, The Bodyguard, Bat Out of Hell, Their Greatest Hits (1971–1975), Rumours, Saturday Night Fever, Back in Black""".split(', ')})
>>>
>>> def method(record):
>>>    record.cost = 40
>>>    return record
>>>
>>> turntable.press.spin_frame(df, method)


def __init__(self, pickle=True, pickle_path='<STR_LIT>'):

self.pickle = pickle  <EOL>self.class_path = turntable.utils.path2filename(<EOL>pickle_path + '<STR_LIT:/>' + self.__class__.__name__)[<NUM_LIT:0>]<EOL>self.pickles = []<EOL>

Parameters
----------
pickle : pickle the class properties
pickle_path : where to pickle


for key in self.pickles:<EOL><INDENT>self.__dict__[key] = None<EOL><DEDENT>

sets all attribute values from the attribute list pickles to None
        -this is the default behaviure so this method is redundent


clean removes all files and folders under the class_path directory


for key, value in list(kwargs.items()):<EOL><INDENT>setattr(self, key, value)<EOL><DEDENT>

Initalizes the given argument structure as properties of the class
to be used by name in specific method execution.

Parameters
----------
kwargs : dictionary
    Dictionary of extra attributes,
    where keys are attributes names and values attributes values.


method_to_call = getattr(self, method)<EOL>result = method_to_call()<EOL>

Calls a specied method by name using run_method


def series(collection, method, prints = <NUM_LIT:15>, *args, **kwargs):

if '<STR_LIT>' in list(kwargs.keys()):<EOL><INDENT>verbose = kwargs['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>verbose = True<EOL><DEDENT>results = []<EOL>timer = turntable.utils.Timer(nLoops=len(collection), numPrints=prints, verbose=verbose)<EOL>for subject in collection:<EOL><INDENT>results.append(method(subject, *args, **kwargs))<EOL>timer.loop()<EOL><DEDENT>timer.fin()<EOL>return results<EOL>

Processes a collection in series

Parameters
----------
collection : list
    list of Record objects
method : method to call on each Record
prints : int
    number of timer prints to the screen

Returns
-------
collection : list
    list of Record objects after going through method called

If more than one collection is given, the function is called with an argument list
consisting of the corresponding item of each collection, substituting None for
missing values when not all collection have the same length.
If the function is None, return the original collection (or a list of tuples if multiple collections).

Example
-------
adding 2 to every number in a range

>>> import turntable
>>> collection = range(100)
>>> method = lambda x: x + 2
>>> collection = turntable.spin.series(collection, method)


def batch(collection, method, processes=None, batch_size=None, quiet=False,<EOL>kwargs_to_dump=None, args=None, **kwargs):

if processes is None:<EOL><INDENT>processes = min(mp.cpu_count(), <NUM_LIT:20>, len(collection))<EOL><DEDENT>if batch_size is None:<EOL><INDENT>batch_size = max(len(collection) // processes, <NUM_LIT:1>)<EOL><DEDENT>print('<STR_LIT>', batch_size)<EOL>mod = len(collection) % processes<EOL>batch_list = [collection[x:x + batch_size]<EOL>for x in range(<NUM_LIT:0>, len(collection) - mod, batch_size)]<EOL>if mod != <NUM_LIT:0>:<EOL><INDENT>batch_list[len(batch_list) - <NUM_LIT:1>] += collection[-mod:]<EOL><DEDENT>print('<STR_LIT>', len(batch_list))<EOL>if args is None:<EOL><INDENT>args = method<EOL><DEDENT>else:<EOL><INDENT>if isinstance(args, tuple) == False:<EOL><INDENT>args = (args,)<EOL><DEDENT>args = (method,) + args<EOL><DEDENT>if kwargs_to_dump is None:<EOL><INDENT>res = parallel(<EOL>batch_list,<EOL>new_function_batch,<EOL>processes=processes,<EOL>args=args,<EOL>**kwargs)<EOL><DEDENT>else:<EOL><INDENT>res = process_dump(<EOL>batch_list,<EOL>new_function_batch,<EOL>kwargs_to_dump,<EOL>processes=processes,<EOL>args=args,<EOL>**kwargs)<EOL><DEDENT>returnList = []<EOL>for l in res:<EOL><INDENT>returnList += l<EOL><DEDENT>return returnList<EOL>

Processes a collection in parallel batches,
    each batch processes in series on a single process.
    Running batches in parallel can be more effficient that splitting a list across cores as in spin.parallel
    because of parallel processing has high IO requirements.

    Parameters
    ----------
    collection : list
        i.e. list of Record objects
    method : method to call on each Record
    processes : int
        number of processes to run on [defaults to number of cores on machine]
    batch_size : int
        lenght of each batch [defaults to number of elements / number of processes]

    Returns
    -------
    collection : list
        list of Record objects after going through method called

    Example
    -------
    adding 2 to every number in a range

    >>> import turntable
    >>> collection = range(100)
    >>> def jam(record):
    >>>     return record + 2
    >>> collection = turntable.spin.batch(collection, jam)

    Note
    ----

    lambda functions do not work in parallel


def thread(function, sequence, cores=None, runSeries=False, quiet=False):

<EOL>if cores is None:<EOL><INDENT>pool = ThreadPool()<EOL><DEDENT>else:<EOL><INDENT>pool = ThreadPool(cores)<EOL><DEDENT>tic = time.time()<EOL>if runSeries is False:<EOL><INDENT>try:<EOL><INDENT>results = pool.map(function, sequence)<EOL>pool.close()<EOL>pool.join()<EOL><DEDENT>except:<EOL><INDENT>print('<STR_LIT>')<EOL>results = series(sequence, function)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>results = series(sequence, function)<EOL><DEDENT>toc = time.time()<EOL>elapsed = toc - tic<EOL>if quiet is False:<EOL><INDENT>if cores is None:<EOL><INDENT>print("<STR_LIT>" % str(elapsed))<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>" % (str(elapsed), str(cores)))<EOL><DEDENT><DEDENT>return results<EOL>

sets up the threadpool with map for parallel processing


def parallel(collection, method, processes=None, args=None, **kwargs):

if processes is None:<EOL><INDENT>processes = min(mp.cpu_count(), <NUM_LIT:20>)<EOL><DEDENT>print("<STR_LIT>" + str(processes) + "<STR_LIT>")<EOL>pool = mp.Pool(processes=processes)<EOL>PROC = []<EOL>tic = time.time()<EOL>for main_arg in collection:<EOL><INDENT>if args is None:<EOL><INDENT>ARGS = (main_arg,)<EOL><DEDENT>else:<EOL><INDENT>if isinstance(args, tuple) == False:<EOL><INDENT>args = (args,)<EOL><DEDENT>ARGS = (main_arg,) + args<EOL><DEDENT>PROC.append(pool.apply_async(method, args=ARGS, kwds=kwargs))<EOL><DEDENT>RES = []<EOL>for p in PROC:<EOL><INDENT>try:<EOL><INDENT>RES.append(p.get())<EOL><DEDENT>except Exception as e:<EOL><INDENT>print("<STR_LIT>")<EOL>print(e)<EOL>RES.append(None)<EOL><DEDENT><DEDENT>pool.close()<EOL>pool.join()<EOL>toc = time.time()<EOL>elapsed = toc - tic<EOL>print("<STR_LIT>" % (str(elapsed), str(processes)))<EOL>return RES<EOL>

Processes a collection in parallel.

    Parameters
    ----------
    collection : list
        i.e. list of Record objects
    method : method to call on each Record
    processes : int
        number of processes to run on [defaults to number of cores on machine]
    batch_size : int
        lenght of each batch [defaults to number of elements / number of processes]

    Returns
    -------
    collection : list
        list of Record objects after going through method called

    Example
    -------
    adding 2 to every number in a range

    >>> import turntable
    >>> collection = range(100)
    >>> def jam(record):
    >>>     return record + 2
    >>> collection = turntable.spin.parallel(collection, jam)

    Note
    ----

    lambda functions do not work in parallel


try:<EOL><INDENT>spit = kwargs.pop('<STR_LIT>')<EOL><DEDENT>except:<EOL><INDENT>spit = None<EOL><DEDENT>try:<EOL><INDENT>results = fcn(*args, **kwargs)<EOL>if results:<EOL><INDENT>return results<EOL><DEDENT><DEDENT>except:<EOL><INDENT>print(traceback.format_exc())<EOL>if spit:<EOL><INDENT>return spit<EOL><DEDENT><DEDENT>

try:
          retrun fcn(*args, **kwargs)
       except:
          print traceback
            if 'spit' in kwargs.keys():
                return kwargs['spit']

    Parameters
    ----------
    fcn : function
    *args : unnamed parameters of fcn
    **kwargs : named parameters of fcn
        spit : returns the parameter named return in the exception

    Returns
    -------
    The expected output of fcn or prints the exception traceback


def batch_list(sequence, batch_size, mod = <NUM_LIT:0>, randomize = False):

if randomize:<EOL><INDENT>sequence = random.sample(sequence, len(sequence))<EOL><DEDENT>return [sequence[x:x + batch_size] for x in range(<NUM_LIT:0>, len(sequence)-mod, batch_size)]<EOL>

Converts a list into a list of lists with equal batch_size.

Parameters
----------
sequence : list
    list of items to be placed in batches
batch_size : int
    length of each sub list
mod : int
    remainder of list length devided by batch_size
    mod = len(sequence) % batch_size
randomize = bool
    should the initial sequence be randomized before being batched


def to_pickle(obj, filename, clean_memory=False):

path, filename = path_to_filename(filename)<EOL>create_dir(path)<EOL>with open(path + filename, "<STR_LIT:wb>") as output:<EOL><INDENT>pickle.dump(obj, output, pickle.HIGHEST_PROTOCOL)<EOL><DEDENT>if clean_memory:<EOL><INDENT>obj = None<EOL><DEDENT>return obj<EOL>


path = pathfile[:pathfile.rfind('<STR_LIT:/>') + <NUM_LIT:1>]<EOL>if path == '<STR_LIT>':<EOL><INDENT>path = '<STR_LIT>'<EOL><DEDENT>filename = pathfile[pathfile.rfind('<STR_LIT:/>') + <NUM_LIT:1>:len(pathfile)]<EOL>if '<STR_LIT:.>' not in filename:<EOL><INDENT>path = pathfile<EOL>filename = '<STR_LIT>'<EOL><DEDENT>if (filename == '<STR_LIT>') and (path[len(path) - <NUM_LIT:1>] != '<STR_LIT:/>'):<EOL><INDENT>path += '<STR_LIT:/>'<EOL><DEDENT>return path, filename<EOL>

Takes a path filename string and returns the split between the path and the filename

if filename is not given, filename = ''
if path is not given, path = './'


folders = path.split('<STR_LIT:/>')<EOL>folders = [i for i in folders if i != '<STR_LIT>']<EOL>rootPath = '<STR_LIT>'<EOL>if folders[<NUM_LIT:0>] == '<STR_LIT>':<EOL><INDENT>folders = folders[<NUM_LIT:1>:]<EOL><DEDENT>count = <NUM_LIT:0><EOL>for directory in folders:<EOL><INDENT>count += <NUM_LIT:1><EOL>if (directory[<NUM_LIT:0>] == '<STR_LIT:.>') & (count == <NUM_LIT:1>):<EOL><INDENT>rootPath = directory<EOL><DEDENT>else:<EOL><INDENT>rootPath = rootPath + '<STR_LIT:/>' + directory<EOL><DEDENT>try:<EOL><INDENT>os.makedirs(rootPath)<EOL><DEDENT>except OSError as exception:<EOL><INDENT>if exception.errno != errno.EEXIST:<EOL><INDENT>raise<EOL><DEDENT><DEDENT><DEDENT>for key in list(dir_dict.keys()):<EOL><INDENT>rootPath = path + "<STR_LIT:/>" + key<EOL>try:<EOL><INDENT>os.makedirs(rootPath)<EOL><DEDENT>except OSError as exception:<EOL><INDENT>if exception.errno != errno.EEXIST:<EOL><INDENT>raise<EOL><DEDENT><DEDENT>if dir_dict[key] is not None:<EOL><INDENT>create_dir(rootPath, dir_dict[key])<EOL><DEDENT><DEDENT>

Tries to create a new directory in the given path.
**create_dir** can also create subfolders according to the dictionnary given as second argument.

Parameters
----------
path : string
    string giving the path of the location to create the directory, either absolute or relative.
dir_dict : dictionary, optional
    Dictionary ordering the creation of subfolders. Keys must be strings, and values either None or path dictionaries.
    the default is {}, which means that no subfolders will be created

Examples
--------

>>> path = './project'
>>> dir_dict = {'dir1':None, 'dir2':{'subdir21':None}}
>>> utils.create_dir(path, dir_dict)

will create:

* *project/dir1*
* *project/dir2/subdir21*

in your parent directory.