def pinfo(self,obj,oname='',formatter=None,info=None,detail_level=0):
        """Show detailed information about an object.

        Optional arguments:

        - oname: name of the variable pointing to the object.

        - formatter: special formatter for docstrings (see pdoc)

        - info: a structure with some information fields which may have been
        precomputed already.

        - detail_level: if set to 1, more information is given.
        """
        info = self.info(obj, oname=oname, formatter=formatter,
                            info=info, detail_level=detail_level)
        displayfields = []
        def add_fields(fields):
            for title, key in fields:
                field = info[key]
                if field is not None:
                    displayfields.append((title, field.rstrip()))

        add_fields(self.pinfo_fields1)

        # Base class for old-style instances
        if (not py3compat.PY3) and isinstance(obj, types.InstanceType) and info['base_class']:
            displayfields.append(("Base Class", info['base_class'].rstrip()))

        add_fields(self.pinfo_fields2)

        # Namespace
        if info['namespace'] != 'Interactive':
            displayfields.append(("Namespace", info['namespace'].rstrip()))

        add_fields(self.pinfo_fields3)

        # Source or docstring, depending on detail level and whether
        # source found.
        if detail_level > 0 and info['source'] is not None:
            displayfields.append(("Source", self.format(py3compat.cast_bytes_py2(info['source']))))
        elif info['docstring'] is not None:
            displayfields.append(("Docstring", info["docstring"]))

        # Constructor info for classes
        if info['isclass']:
            if info['init_definition'] or info['init_docstring']:
                displayfields.append(("Constructor information", ""))
                if info['init_definition'] is not None:
                    displayfields.append((" Definition",
                                    info['init_definition'].rstrip()))
                if info['init_docstring'] is not None:
                    displayfields.append((" Docstring",
                                        indent(info['init_docstring'])))

        # Info for objects:
        else:
            add_fields(self.pinfo_fields_obj)

        # Finally send to printer/pager:
        if displayfields:
            page.page(self._format_fields(displayfields))

def
pinfo
(
self
,
obj
,
oname
=
''
,
formatter
=
,
info
=
,
detail_level
=
0
)
:
info
=
self
.
info
(
obj
,
oname
=
oname
,
formatter
=
formatter
,
info
=
info
,
detail_level
=
detail_level
)
displayfields
=
[
]
def
add_fields
(
fields
)
:
for
title
,
key
in
fields
:
field
=
info
[
key
]
if
field
is
not
:
displayfields
.
append
(
(
title
,
field
.
rstrip
(
)
)
)
add_fields
(
self
.
pinfo_fields1
)
if
(
not
py3compat
.
PY3
)
and
isinstance
(
obj
,
types
.
InstanceType
)
and
info
[
'base_class'
]
:
displayfields
.
append
(
(
"Base Class"
,
info
[
'base_class'
]
.
rstrip
(
)
)
)
add_fields
(
self
.
pinfo_fields2
)
if
info
[
'namespace'
]
!=
'Interactive'
:
displayfields
.
append
(
(
"Namespace"
,
info
[
'namespace'
]
.
rstrip
(
)
)
)
add_fields
(
self
.
pinfo_fields3
)
if
detail_level
>
0
and
info
[
'source'
]
is
not
:
displayfields
.
append
(
(
"Source"
,
self
.
format
(
py3compat
.
cast_bytes_py2
(
info
[
'source'
]
)
)
)
)
elif
info
[
'docstring'
]
is
not
:
displayfields
.
append
(
(
"Docstring"
,
info
[
"docstring"
]
)
)
if
info
[
'isclass'
]
:
if
info
[
'init_definition'
]
or
info
[
'init_docstring'
]
:
displayfields
.
append
(
(
"Constructor information"
,
""
)
)
if
info
[
'init_definition'
]
is
not
:
displayfields
.
append
(
(
" Definition"
,
info
[
'init_definition'
]
.
rstrip
(
)
)
)
if
info
[
'init_docstring'
]
is
not
:
displayfields
.
append
(
(
" Docstring"
,
indent
(
info
[
'init_docstring'
]
)
)
)
else
:
add_fields
(
self
.
pinfo_fields_obj
)
if
displayfields
:
page
.
page
(
self
.
_format_fields
(
displayfields
)
)

Show detailed information about an object .


def psearch(self,pattern,ns_table,ns_search=[],
                ignore_case=False,show_all=False):
        """Search namespaces with wildcards for objects.

        Arguments:

        - pattern: string containing shell-like wildcards to use in namespace
        searches and optionally a type specification to narrow the search to
        objects of that type.

        - ns_table: dict of name->namespaces for search.

        Optional arguments:

          - ns_search: list of namespace names to include in search.

          - ignore_case(False): make the search case-insensitive.

          - show_all(False): show all names, including those starting with
          underscores.
        """
        #print 'ps pattern:<%r>' % pattern # dbg

        # defaults
        type_pattern = 'all'
        filter = ''

        cmds = pattern.split()
        len_cmds  =  len(cmds)
        if len_cmds == 1:
            # Only filter pattern given
            filter = cmds[0]
        elif len_cmds == 2:
            # Both filter and type specified
            filter,type_pattern = cmds
        else:
            raise ValueError('invalid argument string for psearch: <%s>' %
                             pattern)

        # filter search namespaces
        for name in ns_search:
            if name not in ns_table:
                raise ValueError('invalid namespace <%s>. Valid names: %s' %
                                 (name,ns_table.keys()))

        #print 'type_pattern:',type_pattern # dbg
        search_result, namespaces_seen = set(), set()
        for ns_name in ns_search:
            ns = ns_table[ns_name]
            # Normally, locals and globals are the same, so we just check one.
            if id(ns) in namespaces_seen:
                continue
            namespaces_seen.add(id(ns))
            tmp_res = list_namespace(ns, type_pattern, filter,
                                    ignore_case=ignore_case, show_all=show_all)
            search_result.update(tmp_res)

        page.page('\n'.join(sorted(search_result)))

Search namespaces with wildcards for objects .


def threaded_reactor():
    """
    Start the Twisted reactor in a separate thread, if not already done.
    Returns the reactor.
    The thread will automatically be destroyed when all the tests are done.
    """
    global _twisted_thread
    try:
        from twisted.internet import reactor
    except ImportError:
        return None, None
    if not _twisted_thread:
        from twisted.python import threadable
        from threading import Thread
        _twisted_thread = Thread(target=lambda: reactor.run( \
                installSignalHandlers=False))
        _twisted_thread.setDaemon(True)
        _twisted_thread.start()
    return reactor, _twisted_thread

def threaded_reactor ( ) : global _twisted_thread try : from twisted . internet import reactor except ImportError : return None , None if not _twisted_thread : from twisted . python import threadable from threading import Thread _twisted_thread = Thread ( target = lambda : reactor . run ( installSignalHandlers = False ) ) _twisted_thread . setDaemon ( True ) _twisted_thread . start ( ) return reactor , _twisted_thread

Start the Twisted reactor in a separate thread if not already done . Returns the reactor . The thread will automatically be destroyed when all the tests are done .


def deferred(timeout=None):
    """
    By wrapping a test function with this decorator, you can return a
    twisted Deferred and the test will wait for the deferred to be triggered.
    The whole test function will run inside the Twisted event loop.

    The optional timeout parameter specifies the maximum duration of the test.
    The difference with timed() is that timed() will still wait for the test
    to end, while deferred() will stop the test when its timeout has expired.
    The latter is more desireable when dealing with network tests, because
    the result may actually never arrive.

    If the callback is triggered, the test has passed.
    If the errback is triggered or the timeout expires, the test has failed.

    Example::

        @deferred(timeout=5.0)
        def test_resolve():
            return reactor.resolve("www.python.org")

    Attention! If you combine this decorator with other decorators (like
    "raises"), deferred() must be called *first*!

    In other words, this is good::

        @raises(DNSLookupError)
        @deferred()
        def test_error():
            return reactor.resolve("xxxjhjhj.biz")

    and this is bad::

        @deferred()
        @raises(DNSLookupError)
        def test_error():
            return reactor.resolve("xxxjhjhj.biz")
    """
    reactor, reactor_thread = threaded_reactor()
    if reactor is None:
        raise ImportError("twisted is not available or could not be imported")
    # Check for common syntax mistake
    # (otherwise, tests can be silently ignored
    # if one writes "@deferred" instead of "@deferred()")
    try:
        timeout is None or timeout + 0
    except TypeError:
        raise TypeError("'timeout' argument must be a number or None")

    def decorate(func):
        def wrapper(*args, **kargs):
            q = Queue()
            def callback(value):
                q.put(None)
            def errback(failure):
                # Retrieve and save full exception info
                try:
                    failure.raiseException()
                except:
                    q.put(sys.exc_info())
            def g():
                try:
                    d = func(*args, **kargs)
                    try:
                        d.addCallbacks(callback, errback)
                    # Check for a common mistake and display a nice error
                    # message
                    except AttributeError:
                        raise TypeError("you must return a twisted Deferred "
                                        "from your test case!")
                # Catch exceptions raised in the test body (from the
                # Twisted thread)
                except:
                    q.put(sys.exc_info())
            reactor.callFromThread(g)
            try:
                error = q.get(timeout=timeout)
            except Empty:
                raise TimeExpired("timeout expired before end of test (%f s.)"
                                  % timeout)
            # Re-raise all exceptions
            if error is not None:
                exc_type, exc_value, tb = error
                raise exc_type, exc_value, tb
        wrapper = make_decorator(func)(wrapper)
        return wrapper
    return decorate

By wrapping a test function with this decorator you can return a twisted Deferred and the test will wait for the deferred to be triggered . The whole test function will run inside the Twisted event loop .


def find_best_string(query,
                     corpus,
                     step=4,
                     flex=3,
                     case_sensitive=False):
    """Return best matching substring of corpus.

    Parameters
    ----------
    query : str
    corpus : str
    step : int
        Step size of first match-value scan through corpus. Can be thought of
        as a sort of "scan resolution". Should not exceed length of query.
    flex : int
        Max. left/right substring position adjustment value. Should not
        exceed length of query / 2.

    Outputs
    -------
    output0 : str
        Best matching substring.
    output1 : float
        Match ratio of best matching substring. 1 is perfect match.

    """

    def ratio(a, b):
        """Compact alias for SequenceMatcher."""
        return SequenceMatcher(None, a, b).ratio()

    def scan_corpus(step):
        """Return list of match values from corpus-wide scan."""
        match_values = []
        m = 0
        while m + qlen - step <= len(corpus):
            match_values.append(ratio(query, corpus[m : m-1+qlen]))
            m += step
        return match_values

    def index_max(v):
        """Return index of max value."""
        return max(range(len(v)), key=v.__getitem__)

    def adjust_left_right_positions():
        """Return left/right positions for best string match."""
        # bp_* is synonym for 'Best Position Left/Right' and are adjusted
        # to optimize bmv_*
        p_l, bp_l = [pos] * 2
        p_r, bp_r = [pos + qlen] * 2

        # bmv_* are declared here in case they are untouched in optimization
        bmv_l = match_values[round_decimal(p_l / step)]
        bmv_r = match_values[round_decimal(p_r / step)]

        for f in range(flex):
            ll = ratio(query, corpus[p_l - f: p_r])
            if ll > bmv_l:
                bmv_l = ll
                bp_l = p_l - f

            lr = ratio(query, corpus[p_l + f: p_r])
            if lr > bmv_l:
                bmv_l = lr
                bp_l = p_l + f

            rl = ratio(query, corpus[p_l: p_r - f])
            if rl > bmv_r:
                bmv_r = rl
                bp_r = p_r - f

            rr = ratio(query, corpus[p_l: p_r + f])
            if rr > bmv_r:
                bmv_r = rr
                bp_r = p_r + f

        return bp_l, bp_r, ratio(query, corpus[bp_l : bp_r])

    if not case_sensitive:
        query = query.lower()
        corpus = corpus.lower()

    qlen = len(query)

    if flex >= qlen/2:
        print("Warning: flex exceeds length of query / 2. Setting to default.")
        flex = 3

    match_values = scan_corpus(step)
    pos = index_max(match_values) * step

    pos_left, pos_right, match_value = adjust_left_right_positions()

    return corpus[pos_left: pos_right].strip(), match_value

Return best matching substring of corpus .


def to_string(self, indent=True, declaration=True):
        """Encodes the stored ``data`` to XML and returns a
        ``string``.

        Setting ``indent`` to ``False`` will forego any pretty-printing
        and return a condensed value.

        Setting ``declaration`` to ``False`` will skip inserting the
        XML declaration.
        """
        return etree.tostring(self.to_xml(),
                              encoding=self.encoding,
                              xml_declaration=declaration,
                              pretty_print=indent
                              )

def to_string ( self , indent = True , declaration = True ) : return etree . tostring ( self . to_xml ( ) , encoding = self . encoding , xml_declaration = declaration , pretty_print = indent )

Encodes the stored data to XML and returns a string .


def to_xml(self):
        """Encodes the stored ``data`` to XML and returns
        an ``lxml.etree`` value.
        """
        if self.data:
            self.document = self._update_document(self.document, self.data)

        return self.document

def to_xml ( self ) : if self . data : self . document = self . _update_document ( self . document , self . data ) return self . document

Encodes the stored data to XML and returns an lxml . etree value .


def load_all_modules_in_packages(package_or_set_of_packages):
    """
    Recursively loads all modules from a package object, or set of package objects

    :param package_or_set_of_packages: package object, or iterable of package objects
    :return: list of all unique modules discovered by the function
    """
    if isinstance(package_or_set_of_packages, types.ModuleType):
        packages = [package_or_set_of_packages]
    elif isinstance(package_or_set_of_packages, Iterable) and not isinstance(package_or_set_of_packages, (dict, str)):
        packages = package_or_set_of_packages
    else:
        raise Exception("This function only accepts a module reference, or an iterable of said objects")

    imported = packages.copy()

    for package in packages:
        if not hasattr(package, '__path__'):
            raise Exception(
                'Package object passed in has no __path__ attribute. '
                'Make sure to pass in imported references to the packages in question.'
            )

        for module_finder, name, ispkg in pkgutil.walk_packages(package.__path__):
            module_name = '{}.{}'.format(package.__name__, name)
            current_module = importlib.import_module(module_name)
            imported.append(current_module)
            if ispkg:
                imported += load_all_modules_in_packages(current_module)

    for module in imported:
        # This is to cover cases where simply importing a module doesn't execute all the code/definitions within
        # I don't totally understand the reasons for this, but I do know enumerating a module's context (like with dir)
        # seems to solve things
        dir(module)

    return list(
        {
            module.__name__: module

            for module in imported
        }.values()
    )

Recursively loads all modules from a package object or set of package objects


def __dict_invert(self, data):
        """Helper function for merge.

        Takes a dictionary whose values are lists and returns a dict with
        the elements of each list as keys and the original keys as values.
        """
        outdict = {}
        for k,lst in data.items():
            if isinstance(lst, str):
                lst = lst.split()
            for entry in lst:
                outdict[entry] = k
        return outdict

def __dict_invert ( self , data ) : outdict = { } for k , lst in data . items ( ) : if isinstance ( lst , str ) : lst = lst . split ( ) for entry in lst : outdict [ entry ] = k return outdict


def merge(self, __loc_data__=None, __conflict_solve=None, **kw):
        """Merge two Structs with customizable conflict resolution.

        This is similar to :meth:`update`, but much more flexible. First, a
        dict is made from data+key=value pairs. When merging this dict with
        the Struct S, the optional dictionary 'conflict' is used to decide
        what to do.

        If conflict is not given, the default behavior is to preserve any keys
        with their current value (the opposite of the :meth:`update` method's
        behavior).

        Parameters
        ----------
        __loc_data : dict, Struct
            The data to merge into self
        __conflict_solve : dict
            The conflict policy dict.  The keys are binary functions used to
            resolve the conflict and the values are lists of strings naming
            the keys the conflict resolution function applies to.  Instead of
            a list of strings a space separated string can be used, like
            'a b c'.
        kw : dict
            Additional key, value pairs to merge in

        Notes
        -----

        The `__conflict_solve` dict is a dictionary of binary functions which will be used to
        solve key conflicts.  Here is an example::

            __conflict_solve = dict(
                func1=['a','b','c'],
                func2=['d','e']
            )

        In this case, the function :func:`func1` will be used to resolve
        keys 'a', 'b' and 'c' and the function :func:`func2` will be used for
        keys 'd' and 'e'.  This could also be written as::

            __conflict_solve = dict(func1='a b c',func2='d e')

        These functions will be called for each key they apply to with the
        form::

            func1(self['a'], other['a'])

        The return value is used as the final merged value.

        As a convenience, merge() provides five (the most commonly needed)
        pre-defined policies: preserve, update, add, add_flip and add_s. The
        easiest explanation is their implementation::

            preserve = lambda old,new: old
            update   = lambda old,new: new
            add      = lambda old,new: old + new
            add_flip = lambda old,new: new + old  # note change of order!
            add_s    = lambda old,new: old + ' ' + new  # only for str!

        You can use those four words (as strings) as keys instead
        of defining them as functions, and the merge method will substitute
        the appropriate functions for you.

        For more complicated conflict resolution policies, you still need to
        construct your own functions.

        Examples
        --------

        This show the default policy:

        >>> s = Struct(a=10,b=30)
        >>> s2 = Struct(a=20,c=40)
        >>> s.merge(s2)
        >>> sorted(s.items())
        [('a', 10), ('b', 30), ('c', 40)]

        Now, show how to specify a conflict dict:

        >>> s = Struct(a=10,b=30)
        >>> s2 = Struct(a=20,b=40)
        >>> conflict = {'update':'a','add':'b'}
        >>> s.merge(s2,conflict)
        >>> sorted(s.items())
        [('a', 20), ('b', 70)]
        """

        data_dict = dict(__loc_data__,**kw)

        # policies for conflict resolution: two argument functions which return
        # the value that will go in the new struct
        preserve = lambda old,new: old
        update   = lambda old,new: new
        add      = lambda old,new: old + new
        add_flip = lambda old,new: new + old  # note change of order!
        add_s    = lambda old,new: old + ' ' + new

        # default policy is to keep current keys when there's a conflict
        conflict_solve = list2dict2(self.keys(), default = preserve)

        # the conflict_solve dictionary is given by the user 'inverted': we
        # need a name-function mapping, it comes as a function -> names
        # dict. Make a local copy (b/c we'll make changes), replace user
        # strings for the three builtin policies and invert it.
        if __conflict_solve:
            inv_conflict_solve_user = __conflict_solve.copy()
            for name, func in [('preserve',preserve), ('update',update),
                               ('add',add), ('add_flip',add_flip),
                               ('add_s',add_s)]:
                if name in inv_conflict_solve_user.keys():
                    inv_conflict_solve_user[func] = inv_conflict_solve_user[name]
                    del inv_conflict_solve_user[name]
            conflict_solve.update(self.__dict_invert(inv_conflict_solve_user))
        for key in data_dict:
            if key not in self:
                self[key] = data_dict[key]
            else:
                self[key] = conflict_solve[key](self[key],data_dict[key])

def
merge
(
self
,
__loc_data__
=
,
__conflict_solve
=
,
**
kw
)
:
data_dict
=
dict
(
__loc_data__
,
**
kw
)
preserve
=
lambda
old
,
new
:
old
update
=
lambda
old
,
new
:
new
add
=
lambda
old
,
new
:
old
+
new
add_flip
=
lambda
old
,
new
:
new
+
old
add_s
=
lambda
old
,
new
:
old
+
' '
+
new
conflict_solve
=
list2dict2
(
self
.
keys
(
)
,
default
=
preserve
)
if
__conflict_solve
:
inv_conflict_solve_user
=
__conflict_solve
.
copy
(
)
for
name
,
func
in
[
(
'preserve'
,
preserve
)
,
(
'update'
,
update
)
,
(
'add'
,
add
)
,
(
'add_flip'
,
add_flip
)
,
(
'add_s'
,
add_s
)
]
:
if
name
in
inv_conflict_solve_user
.
keys
(
)
:
inv_conflict_solve_user
[
func
]
=
inv_conflict_solve_user
[
name
]
del
inv_conflict_solve_user
[
name
]
conflict_solve
.
update
(
self
.
__dict_invert
(
inv_conflict_solve_user
)
)
for
key
in
data_dict
:
if
key
not
in
self
:
self
[
key
]
=
data_dict
[
key
]
else
:
self
[
key
]
=
conflict_solve
[
key
]
(
self
[
key
]
,
data_dict
[
key
]
)

Merge two Structs with customizable conflict resolution .


def object_to_primitive(obj):
    '''
    convert object to primitive type so we can serialize it to data format like python.

    all primitive types: dict, list, int, float, bool, str, None
    '''
    if obj is None:
        return obj

    if isinstance(obj, (int, float, bool, str)):
        return obj

    if isinstance(obj, (list, frozenset, set)):
        return [object_to_primitive(x) for x in obj]

    if isinstance(obj, dict):
        return dict([(object_to_primitive(k), object_to_primitive(v)) for k, v in obj.items()])

    data = vars(obj)
    assert isinstance(data, dict)
    return object_to_primitive(data)

def object_to_primitive ( obj ) : if obj is None : return obj if isinstance ( obj , ( int , float , bool , str ) ) : return obj if isinstance ( obj , ( list , frozenset , set ) ) : return [ object_to_primitive ( x ) for x in obj ] if isinstance ( obj , dict ) : return dict ( [ ( object_to_primitive ( k ) , object_to_primitive ( v ) ) for k , v in obj . items ( ) ] ) data = vars ( obj ) assert isinstance ( data , dict ) return object_to_primitive ( data )

convert object to primitive type so we can serialize it to data format like python .


def format2(self, raw, out = None, scheme = ''):
        """ Parse and send the colored source.

        If out and scheme are not specified, the defaults (given to
        constructor) are used.

        out should be a file-type object. Optionally, out can be given as the
        string 'str' and the parser will automatically return the output in a
        string."""

        string_output = 0
        if out == 'str' or self.out == 'str' or \
           isinstance(self.out,StringIO.StringIO):
            # XXX - I don't really like this state handling logic, but at this
            # point I don't want to make major changes, so adding the
            # isinstance() check is the simplest I can do to ensure correct
            # behavior.
            out_old = self.out
            self.out = StringIO.StringIO()
            string_output = 1
        elif out is not None:
            self.out = out

        # Fast return of the unmodified input for NoColor scheme
        if scheme == 'NoColor':
            error = False
            self.out.write(raw)
            if string_output:
                return raw,error
            else:
                return None,error

        # local shorthands
        colors = self.color_table[scheme].colors
        self.colors = colors # put in object so __call__ sees it

        # Remove trailing whitespace and normalize tabs
        self.raw = raw.expandtabs().rstrip()

        # store line offsets in self.lines
        self.lines = [0, 0]
        pos = 0
        raw_find = self.raw.find
        lines_append = self.lines.append
        while 1:
            pos = raw_find('\n', pos) + 1
            if not pos: break
            lines_append(pos)
        lines_append(len(self.raw))

        # parse the source and write it
        self.pos = 0
        text = StringIO.StringIO(self.raw)

        error = False
        try:
            for atoken in generate_tokens(text.readline):
                self(*atoken)
        except tokenize.TokenError as ex:
            msg = ex.args[0]
            line = ex.args[1][0]
            self.out.write("%s\n\n*** ERROR: %s%s%s\n" %
                           (colors[token.ERRORTOKEN],
                            msg, self.raw[self.lines[line]:],
                            colors.normal)
                           )
            error = True
        self.out.write(colors.normal+'\n')
        if string_output:
            output = self.out.getvalue()
            self.out = out_old
            return (output, error)
        return (None, error)

def
format2
(
self
,
raw
,
out
=
,
scheme
=
''
)
:
string_output
=
0
if
out
==
'str'
or
self
.
out
==
'str'
or
isinstance
(
self
.
out
,
StringIO
.
StringIO
)
:
out_old
=
self
.
out
self
.
out
=
StringIO
.
StringIO
(
)
string_output
=
1
elif
out
is
not
:
self
.
out
=
out
if
scheme
==
'NoColor'
:
error
=
False
self
.
out
.
write
(
raw
)
if
string_output
:
return
raw
,
error
else
:
return
,
error
colors
=
self
.
color_table
[
scheme
]
.
colors
self
.
colors
=
colors
self
.
raw
=
raw
.
expandtabs
(
)
.
rstrip
(
)
self
.
lines
=
[
0
,
0
]
pos
=
0
raw_find
=
self
.
raw
.
find
lines_append
=
self
.
lines
.
append
while
1
:
pos
=
raw_find
(
'\n'
,
pos
)
+
1
if
not
pos
:
break
lines_append
(
pos
)
lines_append
(
len
(
self
.
raw
)
)
self
.
pos
=
0
text
=
StringIO
.
StringIO
(
self
.
raw
)
error
=
False
try
:
for
atoken
in
generate_tokens
(
text
.
readline
)
:
self
(
*
atoken
)
except
tokenize
.
TokenError
as
ex
:
msg
=
ex
.
args
[
0
]
line
=
ex
.
args
[
1
]
[
0
]
self
.
out
.
write
(
"%s\n\n*** ERROR: %s%s%s\n"
%
(
colors
[
token
.
ERRORTOKEN
]
,
msg
,
self
.
raw
[
self
.
lines
[
line
]
:
]
,
colors
.
normal
)
)
error
=
True
self
.
out
.
write
(
colors
.
normal
+
'\n'
)
if
string_output
:
output
=
self
.
out
.
getvalue
(
)
self
.
out
=
out_old
return
(
output
,
error
)
return
(
,
error
)


def getfigs(*fig_nums):
    """Get a list of matplotlib figures by figure numbers.

    If no arguments are given, all available figures are returned.  If the
    argument list contains references to invalid figures, a warning is printed
    but the function continues pasting further figures.

    Parameters
    ----------
    figs : tuple
        A tuple of ints giving the figure numbers of the figures to return.
    """
    from matplotlib._pylab_helpers import Gcf
    if not fig_nums:
        fig_managers = Gcf.get_all_fig_managers()
        return [fm.canvas.figure for fm in fig_managers]
    else:
        figs = []
        for num in fig_nums:
            f = Gcf.figs.get(num)
            if f is None:
                print('Warning: figure %s not available.' % num)
            else:
                figs.append(f.canvas.figure)
        return figs

def
getfigs
(
*
fig_nums
)
:
from
matplotlib
.
_pylab_helpers
import
Gcf
if
not
fig_nums
:
fig_managers
=
Gcf
.
get_all_fig_managers
(
)
return
[
fm
.
canvas
.
figure
for
fm
in
fig_managers
]
else
:
figs
=
[
]
for
num
in
fig_nums
:
f
=
Gcf
.
figs
.
get
(
num
)
if
f
is
:
print
(
'Warning: figure %s not available.'
%
num
)
else
:
figs
.
append
(
f
.
canvas
.
figure
)
return
figs

Get a list of matplotlib figures by figure numbers .


def print_figure(fig, fmt='png'):
    """Convert a figure to svg or png for inline display."""
    # When there's an empty figure, we shouldn't return anything, otherwise we
    # get big blank areas in the qt console.
    if not fig.axes and not fig.lines:
        return

    fc = fig.get_facecolor()
    ec = fig.get_edgecolor()
    fig.set_facecolor('white')
    fig.set_edgecolor('white')
    try:
        bytes_io = BytesIO()
        fig.canvas.print_figure(bytes_io, format=fmt, bbox_inches='tight')
        data = bytes_io.getvalue()
    finally:
        fig.set_facecolor(fc)
        fig.set_edgecolor(ec)
    return data

def print_figure ( fig , fmt = 'png' ) : if not fig . axes and not fig . lines : return fc = fig . get_facecolor ( ) ec = fig . get_edgecolor ( ) fig . set_facecolor ( 'white' ) fig . set_edgecolor ( 'white' ) try : bytes_io = BytesIO ( ) fig . canvas . print_figure ( bytes_io , format = fmt , bbox_inches = 'tight' ) data = bytes_io . getvalue ( ) finally : fig . set_facecolor ( fc ) fig . set_edgecolor ( ec ) return data

Convert a figure to svg or png for inline display .


def mpl_runner(safe_execfile):
    """Factory to return a matplotlib-enabled runner for %run.

    Parameters
    ----------
    safe_execfile : function
      This must be a function with the same interface as the
      :meth:`safe_execfile` method of IPython.

    Returns
    -------
    A function suitable for use as the ``runner`` argument of the %run magic
    function.
    """

    def mpl_execfile(fname,*where,**kw):
        """matplotlib-aware wrapper around safe_execfile.

        Its interface is identical to that of the :func:`execfile` builtin.

        This is ultimately a call to execfile(), but wrapped in safeties to
        properly handle interactive rendering."""

        import matplotlib
        import matplotlib.pylab as pylab

        #print '*** Matplotlib runner ***' # dbg
        # turn off rendering until end of script
        is_interactive = matplotlib.rcParams['interactive']
        matplotlib.interactive(False)
        safe_execfile(fname,*where,**kw)
        matplotlib.interactive(is_interactive)
        # make rendering call now, if the user tried to do it
        if pylab.draw_if_interactive.called:
            pylab.draw()
            pylab.draw_if_interactive.called = False

    return mpl_execfile

def mpl_runner ( safe_execfile ) : def mpl_execfile ( fname , * where , ** kw ) : import matplotlib import matplotlib . pylab as pylab is_interactive = matplotlib . rcParams [ 'interactive' ] matplotlib . interactive ( False ) safe_execfile ( fname , * where , ** kw ) matplotlib . interactive ( is_interactive ) if pylab . draw_if_interactive . called : pylab . draw ( ) pylab . draw_if_interactive . called = False return mpl_execfile

Factory to return a matplotlib - enabled runner for %run .


def select_figure_format(shell, fmt):
    """Select figure format for inline backend, either 'png' or 'svg'.

    Using this method ensures only one figure format is active at a time.
    """
    from matplotlib.figure import Figure
    from IPython.zmq.pylab import backend_inline

    svg_formatter = shell.display_formatter.formatters['image/svg+xml']
    png_formatter = shell.display_formatter.formatters['image/png']

    if fmt=='png':
        svg_formatter.type_printers.pop(Figure, None)
        png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png'))
    elif fmt=='svg':
        png_formatter.type_printers.pop(Figure, None)
        svg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'svg'))
    else:
        raise ValueError("supported formats are: 'png', 'svg', not %r"%fmt)

    # set the format to be used in the backend()
    backend_inline._figure_format = fmt

Select figure format for inline backend either png or svg .


def find_gui_and_backend(gui=None):
    """Given a gui string return the gui and mpl backend.

    Parameters
    ----------
    gui : str
        Can be one of ('tk','gtk','wx','qt','qt4','inline').

    Returns
    -------
    A tuple of (gui, backend) where backend is one of ('TkAgg','GTKAgg',
    'WXAgg','Qt4Agg','module://IPython.zmq.pylab.backend_inline').
    """

    import matplotlib

    if gui and gui != 'auto':
        # select backend based on requested gui
        backend = backends[gui]
    else:
        backend = matplotlib.rcParams['backend']
        # In this case, we need to find what the appropriate gui selection call
        # should be for IPython, so we can activate inputhook accordingly
        gui = backend2gui.get(backend, None)
    return gui, backend

def find_gui_and_backend ( gui = None ) : import matplotlib if gui and gui != 'auto' : backend = backends [ gui ] else : backend = matplotlib . rcParams [ 'backend' ] gui = backend2gui . get ( backend , None ) return gui , backend

Given a gui string return the gui and mpl backend .


def activate_matplotlib(backend):
    """Activate the given backend and set interactive to True."""

    import matplotlib
    if backend.startswith('module://'):
        # Work around bug in matplotlib: matplotlib.use converts the
        # backend_id to lowercase even if a module name is specified!
        matplotlib.rcParams['backend'] = backend
    else:
        matplotlib.use(backend)
    matplotlib.interactive(True)

    # This must be imported last in the matplotlib series, after
    # backend/interactivity choices have been made
    import matplotlib.pylab as pylab

    # XXX For now leave this commented out, but depending on discussions with
    # mpl-dev, we may be able to allow interactive switching...
    #import matplotlib.pyplot
    #matplotlib.pyplot.switch_backend(backend)

    pylab.show._needmain = False
    # We need to detect at runtime whether show() is called by the user.
    # For this, we wrap it into a decorator which adds a 'called' flag.
    pylab.draw_if_interactive = flag_calls(pylab.draw_if_interactive)

def activate_matplotlib ( backend ) : import matplotlib if backend . startswith ( 'module://' ) : matplotlib . rcParams [ 'backend' ] = backend else : matplotlib . use ( backend ) matplotlib . interactive ( True ) import matplotlib . pylab as pylab pylab . show . _needmain = False pylab . draw_if_interactive = flag_calls ( pylab . draw_if_interactive )

Activate the given backend and set interactive to True .


def configure_inline_support(shell, backend, user_ns=None):
    """Configure an IPython shell object for matplotlib use.

    Parameters
    ----------
    shell : InteractiveShell instance

    backend : matplotlib backend

    user_ns : dict
      A namespace where all configured variables will be placed.  If not given,
      the `user_ns` attribute of the shell object is used.
    """
    # If using our svg payload backend, register the post-execution
    # function that will pick up the results for display.  This can only be
    # done with access to the real shell object.

    # Note: if we can't load the inline backend, then there's no point
    # continuing (such as in terminal-only shells in environments without
    # zeromq available).
    try:
        from IPython.zmq.pylab.backend_inline import InlineBackend
    except ImportError:
        return

    user_ns = shell.user_ns if user_ns is None else user_ns

    cfg = InlineBackend.instance(config=shell.config)
    cfg.shell = shell
    if cfg not in shell.configurables:
        shell.configurables.append(cfg)

    if backend == backends['inline']:
        from IPython.zmq.pylab.backend_inline import flush_figures
        from matplotlib import pyplot
        shell.register_post_execute(flush_figures)
        # load inline_rc
        pyplot.rcParams.update(cfg.rc)
        # Add 'figsize' to pyplot and to the user's namespace
        user_ns['figsize'] = pyplot.figsize = figsize

    # Setup the default figure format
    fmt = cfg.figure_format
    select_figure_format(shell, fmt)

    # The old pastefig function has been replaced by display
    from IPython.core.display import display
    # Add display and getfigs to the user's namespace
    user_ns['display'] = display
    user_ns['getfigs'] = getfigs

def configure_inline_support ( shell , backend , user_ns = None ) : try : from IPython . zmq . pylab . backend_inline import InlineBackend except ImportError : return user_ns = shell . user_ns if user_ns is None else user_ns cfg = InlineBackend . instance ( config = shell . config ) cfg . shell = shell if cfg not in shell . configurables : shell . configurables . append ( cfg ) if backend == backends [ 'inline' ] : from IPython . zmq . pylab . backend_inline import flush_figures from matplotlib import pyplot shell . register_post_execute ( flush_figures ) pyplot . rcParams . update ( cfg . rc ) user_ns [ 'figsize' ] = pyplot . figsize = figsize fmt = cfg . figure_format select_figure_format ( shell , fmt ) from IPython . core . display import display user_ns [ 'display' ] = display user_ns [ 'getfigs' ] = getfigs

Configure an IPython shell object for matplotlib use .


def pylab_activate(user_ns, gui=None, import_all=True, shell=None):
    """Activate pylab mode in the user's namespace.

    Loads and initializes numpy, matplotlib and friends for interactive use.

    Parameters
    ----------
    user_ns : dict
      Namespace where the imports will occur.

    gui : optional, string
      A valid gui name following the conventions of the %gui magic.

    import_all : optional, boolean
      If true, an 'import *' is done from numpy and pylab.

    Returns
    -------
    The actual gui used (if not given as input, it was obtained from matplotlib
    itself, and will be needed next to configure IPython's gui integration.
    """
    gui, backend = find_gui_and_backend(gui)
    activate_matplotlib(backend)
    import_pylab(user_ns, import_all)
    if shell is not None:
        configure_inline_support(shell, backend, user_ns)

    print """
Welcome to pylab, a matplotlib-based Python environment [backend: %s].
For more information, type 'help(pylab)'.""" % backend
    # flush stdout, just to be safe
    sys.stdout.flush()

    return gui

def pylab_activate ( user_ns , gui = None , import_all = True , shell = None ) : gui , backend = find_gui_and_backend ( gui ) activate_matplotlib ( backend ) import_pylab ( user_ns , import_all ) if shell is not None : configure_inline_support ( shell , backend , user_ns ) print % backend sys . stdout . flush ( ) return gui

Activate pylab mode in the user s namespace .


def _trace(self, frame, event, arg_unused):
        """The trace function passed to sys.settrace."""

        if self.stopped:
            return

        if 0:
            sys.stderr.write("trace event: %s %r @%d\n" % (
                event, frame.f_code.co_filename, frame.f_lineno
            ))

        if self.last_exc_back:
            if frame == self.last_exc_back:
                # Someone forgot a return event.
                if self.arcs and self.cur_file_data:
                    pair = (self.last_line, -self.last_exc_firstlineno)
                    self.cur_file_data[pair] = None
                self.cur_file_data, self.last_line = self.data_stack.pop()
            self.last_exc_back = None

        if event == 'call':
            # Entering a new function context.  Decide if we should trace
            # in this file.
            self.data_stack.append((self.cur_file_data, self.last_line))
            filename = frame.f_code.co_filename
            if filename not in self.should_trace_cache:
                tracename = self.should_trace(filename, frame)
                self.should_trace_cache[filename] = tracename
            else:
                tracename = self.should_trace_cache[filename]
            #print("called, stack is %d deep, tracename is %r" % (
            #               len(self.data_stack), tracename))
            if tracename:
                if tracename not in self.data:
                    self.data[tracename] = {}
                self.cur_file_data = self.data[tracename]
            else:
                self.cur_file_data = None
            # Set the last_line to -1 because the next arc will be entering a
            # code block, indicated by (-1, n).
            self.last_line = -1
        elif event == 'line':
            # Record an executed line.
            if self.cur_file_data is not None:
                if self.arcs:
                    #print("lin", self.last_line, frame.f_lineno)
                    self.cur_file_data[(self.last_line, frame.f_lineno)] = None
                else:
                    #print("lin", frame.f_lineno)
                    self.cur_file_data[frame.f_lineno] = None
            self.last_line = frame.f_lineno
        elif event == 'return':
            if self.arcs and self.cur_file_data:
                first = frame.f_code.co_firstlineno
                self.cur_file_data[(self.last_line, -first)] = None
            # Leaving this function, pop the filename stack.
            self.cur_file_data, self.last_line = self.data_stack.pop()
            #print("returned, stack is %d deep" % (len(self.data_stack)))
        elif event == 'exception':
            #print("exc", self.last_line, frame.f_lineno)
            self.last_exc_back = frame.f_back
            self.last_exc_firstlineno = frame.f_code.co_firstlineno
        return self._trace

def
_trace
(
self
,
frame
,
event
,
arg_unused
)
:
if
self
.
stopped
:
return
if
0
:
sys
.
stderr
.
write
(
"trace event: %s %r @%d\n"
%
(
event
,
frame
.
f_code
.
co_filename
,
frame
.
f_lineno
)
)
if
self
.
last_exc_back
:
if
frame
==
self
.
last_exc_back
:
if
self
.
arcs
and
self
.
cur_file_data
:
pair
=
(
self
.
last_line
,
self
.
last_exc_firstlineno
)
self
.
cur_file_data
[
pair
]
=
self
.
cur_file_data
,
self
.
last_line
=
self
.
data_stack
.
pop
(
)
self
.
last_exc_back
=
if
event
==
'call'
:
self
.
data_stack
.
append
(
(
self
.
cur_file_data
,
self
.
last_line
)
)
filename
=
frame
.
f_code
.
co_filename
if
filename
not
in
self
.
should_trace_cache
:
tracename
=
self
.
should_trace
(
filename
,
frame
)
self
.
should_trace_cache
[
filename
]
=
tracename
else
:
tracename
=
self
.
should_trace_cache
[
filename
]
if
tracename
:
if
tracename
not
in
self
.
data
:
self
.
data
[
tracename
]
=
{
}
self
.
cur_file_data
=
self
.
data
[
tracename
]
else
:
self
.
cur_file_data
=
self
.
last_line
=
1
elif
event
==
'line'
:
if
self
.
cur_file_data
is
not
:
if
self
.
arcs
:
self
.
cur_file_data
[
(
self
.
last_line
,
frame
.
f_lineno
)
]
=
else
:
self
.
cur_file_data
[
frame
.
f_lineno
]
=
self
.
last_line
=
frame
.
f_lineno
elif
event
==
'return'
:
if
self
.
arcs
and
self
.
cur_file_data
:
first
=
frame
.
f_code
.
co_firstlineno
self
.
cur_file_data
[
(
self
.
last_line
,
first
)
]
=
self
.
cur_file_data
,
self
.
last_line
=
self
.
data_stack
.
pop
(
)
elif
event
==
'exception'
:
self
.
last_exc_back
=
frame
.
f_back
self
.
last_exc_firstlineno
=
frame
.
f_code
.
co_firstlineno
return
self
.
_trace

The trace function passed to sys . settrace .


def start(self):
        """Start this Tracer.

        Return a Python function suitable for use with sys.settrace().

        """
        self.thread = threading.currentThread()
        sys.settrace(self._trace)
        return self._trace

def start ( self ) : self . thread = threading . currentThread ( ) sys . settrace ( self . _trace ) return self . _trace


def stop(self):
        """Stop this Tracer."""
        self.stopped = True
        if self.thread != threading.currentThread():
            # Called on a different thread than started us: we can't unhook
            # ourseves, but we've set the flag that we should stop, so we won't
            # do any more tracing.
            return

        if hasattr(sys, "gettrace") and self.warn:
            if sys.gettrace() != self._trace:
                msg = "Trace function changed, measurement is likely wrong: %r"
                self.warn(msg % (sys.gettrace(),))
        #print("Stopping tracer on %s" % threading.current_thread().ident)
        sys.settrace(None)


def _start_tracer(self):
        """Start a new Tracer object, and store it in self.tracers."""
        tracer = self._trace_class()
        tracer.data = self.data
        tracer.arcs = self.branch
        tracer.should_trace = self.should_trace
        tracer.should_trace_cache = self.should_trace_cache
        tracer.warn = self.warn
        fn = tracer.start()
        self.tracers.append(tracer)
        return fn

def _start_tracer ( self ) : tracer = self . _trace_class ( ) tracer . data = self . data tracer . arcs = self . branch tracer . should_trace = self . should_trace tracer . should_trace_cache = self . should_trace_cache tracer . warn = self . warn fn = tracer . start ( ) self . tracers . append ( tracer ) return fn

Start a new Tracer object and store it in self . tracers .


def _installation_trace(self, frame_unused, event_unused, arg_unused):
        """Called on new threads, installs the real tracer."""
        # Remove ourselves as the trace function
        sys.settrace(None)
        # Install the real tracer.
        fn = self._start_tracer()
        # Invoke the real trace function with the current event, to be sure
        # not to lose an event.
        if fn:
            fn = fn(frame_unused, event_unused, arg_unused)
        # Return the new trace function to continue tracing in this scope.
        return fn

def _installation_trace ( self , frame_unused , event_unused , arg_unused ) : sys . settrace ( None ) fn = self . _start_tracer ( ) if fn : fn = fn ( frame_unused , event_unused , arg_unused ) return fn

Called on new threads installs the real tracer .


def start(self):
        """Start collecting trace information."""
        if self._collectors:
            self._collectors[-1].pause()
        self._collectors.append(self)
        #print("Started: %r" % self._collectors, file=sys.stderr)

        # Check to see whether we had a fullcoverage tracer installed.
        traces0 = []
        if hasattr(sys, "gettrace"):
            fn0 = sys.gettrace()
            if fn0:
                tracer0 = getattr(fn0, '__self__', None)
                if tracer0:
                    traces0 = getattr(tracer0, 'traces', [])

        # Install the tracer on this thread.
        fn = self._start_tracer()

        for args in traces0:
            (frame, event, arg), lineno = args
            try:
                fn(frame, event, arg, lineno=lineno)
            except TypeError:
                raise Exception(
                    "fullcoverage must be run with the C trace function."
                )

        # Install our installation tracer in threading, to jump start other
        # threads.
        threading.settrace(self._installation_trace)


def stop(self):
        """Stop collecting trace information."""
        #print >>sys.stderr, "Stopping: %r" % self._collectors
        assert self._collectors
        assert self._collectors[-1] is self

        self.pause()
        self.tracers = []

        # Remove this Collector from the stack, and resume the one underneath
        # (if any).
        self._collectors.pop()
        if self._collectors:
            self._collectors[-1].resume()

def stop ( self ) : assert self . _collectors assert self . _collectors [ - 1 ] is self self . pause ( ) self . tracers = [ ] self . _collectors . pop ( ) if self . _collectors : self . _collectors [ - 1 ] . resume ( )


def pause(self):
        """Pause tracing, but be prepared to `resume`."""
        for tracer in self.tracers:
            tracer.stop()
            stats = tracer.get_stats()
            if stats:
                print("\nCoverage.py tracer stats:")
                for k in sorted(stats.keys()):
                    print("%16s: %s" % (k, stats[k]))
        threading.settrace(None)

def
pause
(
self
)
:
for
tracer
in
self
.
tracers
:
tracer
.
stop
(
)
stats
=
tracer
.
get_stats
(
)
if
stats
:
print
(
"\nCoverage.py tracer stats:"
)
for
k
in
sorted
(
stats
.
keys
(
)
)
:
print
(
"%16s: %s"
%
(
k
,
stats
[
k
]
)
)
threading
.
settrace
(
)

Pause tracing but be prepared to resume .


def resume(self):
        """Resume tracing after a `pause`."""
        for tracer in self.tracers:
            tracer.start()
        threading.settrace(self._installation_trace)

def resume ( self ) : for tracer in self . tracers : tracer . start ( ) threading . settrace ( self . _installation_trace )


def get_line_data(self):
        """Return the line data collected.

        Data is { filename: { lineno: None, ...}, ...}

        """
        if self.branch:
            # If we were measuring branches, then we have to re-build the dict
            # to show line data.
            line_data = {}
            for f, arcs in self.data.items():
                line_data[f] = ldf = {}
                for l1, _ in list(arcs.keys()):
                    if l1:
                        ldf[l1] = None
            return line_data
        else:
            return self.data

def get_line_data ( self ) : if self . branch : line_data = { } for f , arcs in self . data . items ( ) : line_data [ f ] = ldf = { } for l1 , _ in list ( arcs . keys ( ) ) : if l1 : ldf [ l1 ] = None return line_data else : return self . data


def collect_exceptions(rdict_or_list, method='unspecified'):
    """check a result dict for errors, and raise CompositeError if any exist.
    Passthrough otherwise."""
    elist = []
    if isinstance(rdict_or_list, dict):
        rlist = rdict_or_list.values()
    else:
        rlist = rdict_or_list
    for r in rlist:
        if isinstance(r, RemoteError):
            en, ev, etb, ei = r.ename, r.evalue, r.traceback, r.engine_info
            # Sometimes we could have CompositeError in our list.  Just take
            # the errors out of them and put them in our new list.  This
            # has the effect of flattening lists of CompositeErrors into one
            # CompositeError
            if en=='CompositeError':
                for e in ev.elist:
                    elist.append(e)
            else:
                elist.append((en, ev, etb, ei))
    if len(elist)==0:
        return rdict_or_list
    else:
        msg = "one or more exceptions from call to method: %s" % (method)
        # This silliness is needed so the debugger has access to the exception
        # instance (e in this case)
        try:
            raise CompositeError(msg, elist)
        except CompositeError as e:
            raise e

check a result dict for errors and raise CompositeError if any exist . Passthrough otherwise .


def render_traceback(self, excid=None):
        """render one or all of my tracebacks to a list of lines"""
        lines = []
        if excid is None:
            for (en,ev,etb,ei) in self.elist:
                lines.append(self._get_engine_str(ei))
                lines.extend((etb or 'No traceback available').splitlines())
                lines.append('')
        else:
            try:
                en,ev,etb,ei = self.elist[excid]
            except:
                raise IndexError("an exception with index %i does not exist"%excid)
            else:
                lines.append(self._get_engine_str(ei))
                lines.extend((etb or 'No traceback available').splitlines())

        return lines


def process_startup():
    """Call this at Python startup to perhaps measure coverage.

    If the environment variable COVERAGE_PROCESS_START is defined, coverage
    measurement is started.  The value of the variable is the config file
    to use.

    There are two ways to configure your Python installation to invoke this
    function when Python starts:

    #. Create or append to sitecustomize.py to add these lines::

        import coverage
        coverage.process_startup()

    #. Create a .pth file in your Python installation containing::

        import coverage; coverage.process_startup()

    """
    cps = os.environ.get("COVERAGE_PROCESS_START")
    if cps:
        cov = coverage(config_file=cps, auto_data=True)
        cov.start()
        cov._warn_no_data = False
        cov._warn_unimported_source = False

def process_startup ( ) : cps = os . environ . get ( "COVERAGE_PROCESS_START" ) if cps : cov = coverage ( config_file = cps , auto_data = True ) cov . start ( ) cov . _warn_no_data = False cov . _warn_unimported_source = False

Call this at Python startup to perhaps measure coverage .


def _canonical_dir(self, morf):
        """Return the canonical directory of the module or file `morf`."""
        return os.path.split(CodeUnit(morf, self.file_locator).filename)[0]

def _canonical_dir ( self , morf ) : return os . path . split ( CodeUnit ( morf , self . file_locator ) . filename ) [ 0 ]

Return the canonical directory of the module or file morf .


def _source_for_file(self, filename):
        """Return the source file for `filename`."""
        if not filename.endswith(".py"):
            if filename[-4:-1] == ".py":
                filename = filename[:-1]
            elif filename.endswith("$py.class"): # jython
                filename = filename[:-9] + ".py"
        return filename

def _source_for_file ( self , filename ) : if not filename . endswith ( ".py" ) : if filename [ - 4 : - 1 ] == ".py" : filename = filename [ : - 1 ] elif filename . endswith ( "$py.class" ) : filename = filename [ : - 9 ] + ".py" return filename


def _should_trace_with_reason(self, filename, frame):
        """Decide whether to trace execution in `filename`, with a reason.

        This function is called from the trace function.  As each new file name
        is encountered, this function determines whether it is traced or not.

        Returns a pair of values:  the first indicates whether the file should
        be traced: it's a canonicalized filename if it should be traced, None
        if it should not.  The second value is a string, the resason for the
        decision.

        """
        if not filename:
            # Empty string is pretty useless
            return None, "empty string isn't a filename"

        if filename.startswith('<'):
            # Lots of non-file execution is represented with artificial
            # filenames like "<string>", "<doctest readme.txt[0]>", or
            # "<exec_function>".  Don't ever trace these executions, since we
            # can't do anything with the data later anyway.
            return None, "not a real filename"

        self._check_for_packages()

        # Compiled Python files have two filenames: frame.f_code.co_filename is
        # the filename at the time the .pyc was compiled.  The second name is
        # __file__, which is where the .pyc was actually loaded from.  Since
        # .pyc files can be moved after compilation (for example, by being
        # installed), we look for __file__ in the frame and prefer it to the
        # co_filename value.
        dunder_file = frame.f_globals.get('__file__')
        if dunder_file:
            filename = self._source_for_file(dunder_file)

        # Jython reports the .class file to the tracer, use the source file.
        if filename.endswith("$py.class"):
            filename = filename[:-9] + ".py"

        canonical = self.file_locator.canonical_filename(filename)

        # If the user specified source or include, then that's authoritative
        # about the outer bound of what to measure and we don't have to apply
        # any canned exclusions. If they didn't, then we have to exclude the
        # stdlib and coverage.py directories.
        if self.source_match:
            if not self.source_match.match(canonical):
                return None, "falls outside the --source trees"
        elif self.include_match:
            if not self.include_match.match(canonical):
                return None, "falls outside the --include trees"
        else:
            # If we aren't supposed to trace installed code, then check if this
            # is near the Python standard library and skip it if so.
            if self.pylib_match and self.pylib_match.match(canonical):
                return None, "is in the stdlib"

            # We exclude the coverage code itself, since a little of it will be
            # measured otherwise.
            if self.cover_match and self.cover_match.match(canonical):
                return None, "is part of coverage.py"

        # Check the file against the omit pattern.
        if self.omit_match and self.omit_match.match(canonical):
            return None, "is inside an --omit pattern"

        return canonical, "because we love you"

def
_should_trace_with_reason
(
self
,
filename
,
frame
)
:
if
not
filename
:
return
,
"empty string isn't a filename"
if
filename
.
startswith
(
'<'
)
:
return
,
"not a real filename"
self
.
_check_for_packages
(
)
dunder_file
=
frame
.
f_globals
.
get
(
'__file__'
)
if
dunder_file
:
filename
=
self
.
_source_for_file
(
dunder_file
)
if
filename
.
endswith
(
"$py.class"
)
:
filename
=
filename
[
:
9
]
+
".py"
canonical
=
self
.
file_locator
.
canonical_filename
(
filename
)
if
self
.
source_match
:
if
not
self
.
source_match
.
match
(
canonical
)
:
return
,
"falls outside the --source trees"
elif
self
.
include_match
:
if
not
self
.
include_match
.
match
(
canonical
)
:
return
,
"falls outside the --include trees"
else
:
if
self
.
pylib_match
and
self
.
pylib_match
.
match
(
canonical
)
:
return
,
"is in the stdlib"
if
self
.
cover_match
and
self
.
cover_match
.
match
(
canonical
)
:
return
,
"is part of coverage.py"
if
self
.
omit_match
and
self
.
omit_match
.
match
(
canonical
)
:
return
,
"is inside an --omit pattern"
return
canonical
,
"because we love you"

Decide whether to trace execution in filename with a reason .


def _should_trace(self, filename, frame):
        """Decide whether to trace execution in `filename`.

        Calls `_should_trace_with_reason`, and returns just the decision.

        """
        canonical, reason = self._should_trace_with_reason(filename, frame)
        if self.debug.should('trace'):
            if not canonical:
                msg = "Not tracing %r: %s" % (filename, reason)
            else:
                msg = "Tracing %r" % (filename,)
            self.debug.write(msg)
        return canonical

def
_should_trace
(
self
,
filename
,
frame
)
:
canonical
,
reason
=
self
.
_should_trace_with_reason
(
filename
,
frame
)
if
self
.
debug
.
should
(
'trace'
)
:
if
not
canonical
:
msg
=
"Not tracing %r: %s"
%
(
filename
,
reason
)
else
:
msg
=
"Tracing %r"
%
(
filename
,
)
self
.
debug
.
write
(
msg
)
return
canonical

Decide whether to trace execution in filename .


def _warn(self, msg):
        """Use `msg` as a warning."""
        self._warnings.append(msg)
        sys.stderr.write("Coverage.py warning: %s\n" % msg)

def
_warn
(
self
,
msg
)
:
self
.
_warnings
.
append
(
msg
)
sys
.
stderr
.
write
(
"Coverage.py warning: %s\n"
%
msg
)


def _check_for_packages(self):
        """Update the source_match matcher with latest imported packages."""
        # Our self.source_pkgs attribute is a list of package names we want to
        # measure.  Each time through here, we see if we've imported any of
        # them yet.  If so, we add its file to source_match, and we don't have
        # to look for that package any more.
        if self.source_pkgs:
            found = []
            for pkg in self.source_pkgs:
                try:
                    mod = sys.modules[pkg]
                except KeyError:
                    continue

                found.append(pkg)

                try:
                    pkg_file = mod.__file__
                except AttributeError:
                    pkg_file = None
                else:
                    d, f = os.path.split(pkg_file)
                    if f.startswith('__init__'):
                        # This is actually a package, return the directory.
                        pkg_file = d
                    else:
                        pkg_file = self._source_for_file(pkg_file)
                    pkg_file = self.file_locator.canonical_filename(pkg_file)
                    if not os.path.exists(pkg_file):
                        pkg_file = None

                if pkg_file:
                    self.source.append(pkg_file)
                    self.source_match.add(pkg_file)
                else:
                    self._warn("Module %s has no Python source." % pkg)

            for pkg in found:
                self.source_pkgs.remove(pkg)

def
_check_for_packages
(
self
)
:
if
self
.
source_pkgs
:
found
=
[
]
for
pkg
in
self
.
source_pkgs
:
try
:
mod
=
sys
.
modules
[
pkg
]
except
KeyError
:
continue
found
.
append
(
pkg
)
try
:
pkg_file
=
mod
.
__file__
except
AttributeError
:
pkg_file
=
else
:
d
,
f
=
os
.
path
.
split
(
pkg_file
)
if
f
.
startswith
(
'__init__'
)
:
pkg_file
=
d
else
:
pkg_file
=
self
.
_source_for_file
(
pkg_file
)
pkg_file
=
self
.
file_locator
.
canonical_filename
(
pkg_file
)
if
not
os
.
path
.
exists
(
pkg_file
)
:
pkg_file
=
if
pkg_file
:
self
.
source
.
append
(
pkg_file
)
self
.
source_match
.
add
(
pkg_file
)
else
:
self
.
_warn
(
"Module %s has no Python source."
%
pkg
)
for
pkg
in
found
:
self
.
source_pkgs
.
remove
(
pkg
)

Update the source_match matcher with latest imported packages .


def start(self):
        """Start measuring code coverage.

        Coverage measurement actually occurs in functions called after `start`
        is invoked.  Statements in the same scope as `start` won't be measured.

        Once you invoke `start`, you must also call `stop` eventually, or your
        process might not shut down cleanly.

        """
        if self.run_suffix:
            # Calling start() means we're running code, so use the run_suffix
            # as the data_suffix when we eventually save the data.
            self.data_suffix = self.run_suffix
        if self.auto_data:
            self.load()

        # Create the matchers we need for _should_trace
        if self.source or self.source_pkgs:
            self.source_match = TreeMatcher(self.source)
        else:
            if self.cover_dir:
                self.cover_match = TreeMatcher([self.cover_dir])
            if self.pylib_dirs:
                self.pylib_match = TreeMatcher(self.pylib_dirs)
        if self.include:
            self.include_match = FnmatchMatcher(self.include)
        if self.omit:
            self.omit_match = FnmatchMatcher(self.omit)

        # The user may want to debug things, show info if desired.
        if self.debug.should('config'):
            self.debug.write("Configuration values:")
            config_info = sorted(self.config.__dict__.items())
            self.debug.write_formatted_info(config_info)

        if self.debug.should('sys'):
            self.debug.write("Debugging info:")
            self.debug.write_formatted_info(self.sysinfo())

        self.collector.start()
        self._started = True
        self._measured = True

def
start
(
self
)
:
if
self
.
run_suffix
:
self
.
data_suffix
=
self
.
run_suffix
if
self
.
auto_data
:
self
.
load
(
)
if
self
.
source
or
self
.
source_pkgs
:
self
.
source_match
=
TreeMatcher
(
self
.
source
)
else
:
if
self
.
cover_dir
:
self
.
cover_match
=
TreeMatcher
(
[
self
.
cover_dir
]
)
if
self
.
pylib_dirs
:
self
.
pylib_match
=
TreeMatcher
(
self
.
pylib_dirs
)
if
self
.
include
:
self
.
include_match
=
FnmatchMatcher
(
self
.
include
)
if
self
.
omit
:
self
.
omit_match
=
FnmatchMatcher
(
self
.
omit
)
if
self
.
debug
.
should
(
'config'
)
:
self
.
debug
.
write
(
"Configuration values:"
)
config_info
=
sorted
(
self
.
config
.
__dict__
.
items
(
)
)
self
.
debug
.
write_formatted_info
(
config_info
)
if
self
.
debug
.
should
(
'sys'
)
:
self
.
debug
.
write
(
"Debugging info:"
)
self
.
debug
.
write_formatted_info
(
self
.
sysinfo
(
)
)
self
.
collector
.
start
(
)
self
.
_started
=
True
self
.
_measured
=
True


def _atexit(self):
        """Clean up on process shutdown."""
        if self._started:
            self.stop()
        if self.auto_data:
            self.save()

def _atexit ( self ) : if self . _started : self . stop ( ) if self . auto_data : self . save ( )


def exclude(self, regex, which='exclude'):
        """Exclude source lines from execution consideration.

        A number of lists of regular expressions are maintained.  Each list
        selects lines that are treated differently during reporting.

        `which` determines which list is modified.  The "exclude" list selects
        lines that are not considered executable at all.  The "partial" list
        indicates lines with branches that are not taken.

        `regex` is a regular expression.  The regex is added to the specified
        list.  If any of the regexes in the list is found in a line, the line
        is marked for special treatment during reporting.

        """
        excl_list = getattr(self.config, which + "_list")
        excl_list.append(regex)
        self._exclude_regex_stale()

def exclude ( self , regex , which = 'exclude' ) : excl_list = getattr ( self . config , which + "_list" ) excl_list . append ( regex ) self . _exclude_regex_stale ( )

Exclude source lines from execution consideration .


def _exclude_regex(self, which):
        """Return a compiled regex for the given exclusion list."""
        if which not in self._exclude_re:
            excl_list = getattr(self.config, which + "_list")
            self._exclude_re[which] = join_regex(excl_list)
        return self._exclude_re[which]

def _exclude_regex ( self , which ) : if which not in self . _exclude_re : excl_list = getattr ( self . config , which + "_list" ) self . _exclude_re [ which ] = join_regex ( excl_list ) return self . _exclude_re [ which ]

Return a compiled regex for the given exclusion list .


def save(self):
        """Save the collected coverage data to the data file."""
        data_suffix = self.data_suffix
        if data_suffix is True:
            # If data_suffix was a simple true value, then make a suffix with
            # plenty of distinguishing information.  We do this here in
            # `save()` at the last minute so that the pid will be correct even
            # if the process forks.
            extra = ""
            if _TEST_NAME_FILE:
                f = open(_TEST_NAME_FILE)
                test_name = f.read()
                f.close()
                extra = "." + test_name
            data_suffix = "%s%s.%s.%06d" % (
                socket.gethostname(), extra, os.getpid(),
                random.randint(0, 999999)
                )

        self._harvest_data()
        self.data.write(suffix=data_suffix)

def save ( self ) : data_suffix = self . data_suffix if data_suffix is True : extra = "" if _TEST_NAME_FILE : f = open ( _TEST_NAME_FILE ) test_name = f . read ( ) f . close ( ) extra = "." + test_name data_suffix = "%s%s.%s.%06d" % ( socket . gethostname ( ) , extra , os . getpid ( ) , random . randint ( 0 , 999999 ) ) self . _harvest_data ( ) self . data . write ( suffix = data_suffix )

Save the collected coverage data to the data file .


def combine(self):
        """Combine together a number of similarly-named coverage data files.

        All coverage data files whose name starts with `data_file` (from the
        coverage() constructor) will be read, and combined together into the
        current measurements.

        """
        aliases = None
        if self.config.paths:
            aliases = PathAliases(self.file_locator)
            for paths in self.config.paths.values():
                result = paths[0]
                for pattern in paths[1:]:
                    aliases.add(pattern, result)
        self.data.combine_parallel_data(aliases=aliases)

def combine ( self ) : aliases = None if self . config . paths : aliases = PathAliases ( self . file_locator ) for paths in self . config . paths . values ( ) : result = paths [ 0 ] for pattern in paths [ 1 : ] : aliases . add ( pattern , result ) self . data . combine_parallel_data ( aliases = aliases )

Combine together a number of similarly - named coverage data files .


def _harvest_data(self):
        """Get the collected data and reset the collector.

        Also warn about various problems collecting data.

        """
        if not self._measured:
            return

        self.data.add_line_data(self.collector.get_line_data())
        self.data.add_arc_data(self.collector.get_arc_data())
        self.collector.reset()

        # If there are still entries in the source_pkgs list, then we never
        # encountered those packages.
        if self._warn_unimported_source:
            for pkg in self.source_pkgs:
                self._warn("Module %s was never imported." % pkg)

        # Find out if we got any data.
        summary = self.data.summary()
        if not summary and self._warn_no_data:
            self._warn("No data was collected.")

        # Find files that were never executed at all.
        for src in self.source:
            for py_file in find_python_files(src):
                py_file = self.file_locator.canonical_filename(py_file)

                if self.omit_match and self.omit_match.match(py_file):
                    # Turns out this file was omitted, so don't pull it back
                    # in as unexecuted.
                    continue

                self.data.touch_file(py_file)

        self._measured = False

def
_harvest_data
(
self
)
:
if
not
self
.
_measured
:
return
self
.
data
.
add_line_data
(
self
.
collector
.
get_line_data
(
)
)
self
.
data
.
add_arc_data
(
self
.
collector
.
get_arc_data
(
)
)
self
.
collector
.
reset
(
)
if
self
.
_warn_unimported_source
:
for
pkg
in
self
.
source_pkgs
:
self
.
_warn
(
"Module %s was never imported."
%
pkg
)
summary
=
self
.
data
.
summary
(
)
if
not
summary
and
self
.
_warn_no_data
:
self
.
_warn
(
"No data was collected."
)
for
src
in
self
.
source
:
for
py_file
in
find_python_files
(
src
)
:
py_file
=
self
.
file_locator
.
canonical_filename
(
py_file
)
if
self
.
omit_match
and
self
.
omit_match
.
match
(
py_file
)
:
continue
self
.
data
.
touch_file
(
py_file
)
self
.
_measured
=
False

Get the collected data and reset the collector .


def analysis(self, morf):
        """Like `analysis2` but doesn't return excluded line numbers."""
        f, s, _, m, mf = self.analysis2(morf)
        return f, s, m, mf

def analysis ( self , morf ) : f , s , _ , m , mf = self . analysis2 ( morf ) return f , s , m , mf

Like analysis2 but doesn t return excluded line numbers .


def analysis2(self, morf):
        """Analyze a module.

        `morf` is a module or a filename.  It will be analyzed to determine
        its coverage statistics.  The return value is a 5-tuple:

        * The filename for the module.
        * A list of line numbers of executable statements.
        * A list of line numbers of excluded statements.
        * A list of line numbers of statements not run (missing from
          execution).
        * A readable formatted string of the missing line numbers.

        The analysis uses the source file itself and the current measured
        coverage data.

        """
        analysis = self._analyze(morf)
        return (
            analysis.filename,
            sorted(analysis.statements),
            sorted(analysis.excluded),
            sorted(analysis.missing),
            analysis.missing_formatted(),
            )

def analysis2 ( self , morf ) : analysis = self . _analyze ( morf ) return ( analysis . filename , sorted ( analysis . statements ) , sorted ( analysis . excluded ) , sorted ( analysis . missing ) , analysis . missing_formatted ( ) , )


def _analyze(self, it):
        """Analyze a single morf or code unit.

        Returns an `Analysis` object.

        """
        self._harvest_data()
        if not isinstance(it, CodeUnit):
            it = code_unit_factory(it, self.file_locator)[0]

        return Analysis(self, it)

def _analyze ( self , it ) : self . _harvest_data ( ) if not isinstance ( it , CodeUnit ) : it = code_unit_factory ( it , self . file_locator ) [ 0 ] return Analysis ( self , it )


def report(self, morfs=None, show_missing=True, ignore_errors=None,
                file=None,                          # pylint: disable=W0622
                omit=None, include=None
                ):
        """Write a summary report to `file`.

        Each module in `morfs` is listed, with counts of statements, executed
        statements, missing statements, and a list of lines missed.

        `include` is a list of filename patterns.  Modules whose filenames
        match those patterns will be included in the report. Modules matching
        `omit` will not be included in the report.

        Returns a float, the total percentage covered.

        """
        self._harvest_data()
        self.config.from_args(
            ignore_errors=ignore_errors, omit=omit, include=include,
            show_missing=show_missing,
            )
        reporter = SummaryReporter(self, self.config)
        return reporter.report(morfs, outfile=file)

def report ( self , morfs = None , show_missing = True , ignore_errors = None , file = None , omit = None , include = None ) : self . _harvest_data ( ) self . config . from_args ( ignore_errors = ignore_errors , omit = omit , include = include , show_missing = show_missing , ) reporter = SummaryReporter ( self , self . config ) return reporter . report ( morfs , outfile = file )


def annotate(self, morfs=None, directory=None, ignore_errors=None,
                    omit=None, include=None):
        """Annotate a list of modules.

        Each module in `morfs` is annotated.  The source is written to a new
        file, named with a ",cover" suffix, with each line prefixed with a
        marker to indicate the coverage of the line.  Covered lines have ">",
        excluded lines have "-", and missing lines have "!".

        See `coverage.report()` for other arguments.

        """
        self._harvest_data()
        self.config.from_args(
            ignore_errors=ignore_errors, omit=omit, include=include
            )
        reporter = AnnotateReporter(self, self.config)
        reporter.report(morfs, directory=directory)

def annotate ( self , morfs = None , directory = None , ignore_errors = None , omit = None , include = None ) : self . _harvest_data ( ) self . config . from_args ( ignore_errors = ignore_errors , omit = omit , include = include ) reporter = AnnotateReporter ( self , self . config ) reporter . report ( morfs , directory = directory )


def html_report(self, morfs=None, directory=None, ignore_errors=None,
                    omit=None, include=None, extra_css=None, title=None):
        """Generate an HTML report.

        The HTML is written to `directory`.  The file "index.html" is the
        overview starting point, with links to more detailed pages for
        individual modules.

        `extra_css` is a path to a file of other CSS to apply on the page.
        It will be copied into the HTML directory.

        `title` is a text string (not HTML) to use as the title of the HTML
        report.

        See `coverage.report()` for other arguments.

        Returns a float, the total percentage covered.

        """
        self._harvest_data()
        self.config.from_args(
            ignore_errors=ignore_errors, omit=omit, include=include,
            html_dir=directory, extra_css=extra_css, html_title=title,
            )
        reporter = HtmlReporter(self, self.config)
        return reporter.report(morfs)

def html_report ( self , morfs = None , directory = None , ignore_errors = None , omit = None , include = None , extra_css = None , title = None ) : self . _harvest_data ( ) self . config . from_args ( ignore_errors = ignore_errors , omit = omit , include = include , html_dir = directory , extra_css = extra_css , html_title = title , ) reporter = HtmlReporter ( self , self . config ) return reporter . report ( morfs )


def xml_report(self, morfs=None, outfile=None, ignore_errors=None,
                    omit=None, include=None):
        """Generate an XML report of coverage results.

        The report is compatible with Cobertura reports.

        Each module in `morfs` is included in the report.  `outfile` is the
        path to write the file to, "-" will write to stdout.

        See `coverage.report()` for other arguments.

        Returns a float, the total percentage covered.

        """
        self._harvest_data()
        self.config.from_args(
            ignore_errors=ignore_errors, omit=omit, include=include,
            xml_output=outfile,
            )
        file_to_close = None
        delete_file = False
        if self.config.xml_output:
            if self.config.xml_output == '-':
                outfile = sys.stdout
            else:
                outfile = open(self.config.xml_output, "w")
                file_to_close = outfile
        try:
            try:
                reporter = XmlReporter(self, self.config)
                return reporter.report(morfs, outfile=outfile)
            except CoverageException:
                delete_file = True
                raise
        finally:
            if file_to_close:
                file_to_close.close()
                if delete_file:
                    file_be_gone(self.config.xml_output)

def xml_report ( self , morfs = None , outfile = None , ignore_errors = None , omit = None , include = None ) : self . _harvest_data ( ) self . config . from_args ( ignore_errors = ignore_errors , omit = omit , include = include , xml_output = outfile , ) file_to_close = None delete_file = False if self . config . xml_output : if self . config . xml_output == '-' : outfile = sys . stdout else : outfile = open ( self . config . xml_output , "w" ) file_to_close = outfile try : try : reporter = XmlReporter ( self , self . config ) return reporter . report ( morfs , outfile = outfile ) except CoverageException : delete_file = True raise finally : if file_to_close : file_to_close . close ( ) if delete_file : file_be_gone ( self . config . xml_output )

Generate an XML report of coverage results .


def display(*objs, **kwargs):
    """Display a Python object in all frontends.

    By default all representations will be computed and sent to the frontends.
    Frontends can decide which representation is used and how.

    Parameters
    ----------
    objs : tuple of objects
        The Python objects to display.
    include : list or tuple, optional
        A list of format type strings (MIME types) to include in the
        format data dict. If this is set *only* the format types included
        in this list will be computed.
    exclude : list or tuple, optional
        A list of format type string (MIME types) to exclue in the format
        data dict. If this is set all format types will be computed,
        except for those included in this argument.
    """
    include = kwargs.get('include')
    exclude = kwargs.get('exclude')

    from IPython.core.interactiveshell import InteractiveShell
    inst = InteractiveShell.instance()
    format = inst.display_formatter.format
    publish = inst.display_pub.publish

    for obj in objs:
        format_dict = format(obj, include=include, exclude=exclude)
        publish('IPython.core.display.display', format_dict)

def display ( * objs , ** kwargs ) : include = kwargs . get ( 'include' ) exclude = kwargs . get ( 'exclude' ) from IPython . core . interactiveshell import InteractiveShell inst = InteractiveShell . instance ( ) format = inst . display_formatter . format publish = inst . display_pub . publish for obj in objs : format_dict = format ( obj , include = include , exclude = exclude ) publish ( 'IPython.core.display.display' , format_dict )

Display a Python object in all frontends .


def display_html(*objs, **kwargs):
    """Display the HTML representation of an object.

    Parameters
    ----------
    objs : tuple of objects
        The Python objects to display, or if raw=True raw HTML data to
        display.
    raw : bool
        Are the data objects raw data or Python objects that need to be
        formatted before display? [default: False]
    """
    raw = kwargs.pop('raw',False)
    if raw:
        for obj in objs:
            publish_html(obj)
    else:
        display(*objs, include=['text/plain','text/html'])

def display_html ( * objs , ** kwargs ) : raw = kwargs . pop ( 'raw' , False ) if raw : for obj in objs : publish_html ( obj ) else : display ( * objs , include = [ 'text/plain' , 'text/html' ] )

Display the HTML representation of an object .


def display_svg(*objs, **kwargs):
    """Display the SVG representation of an object.

    Parameters
    ----------
    objs : tuple of objects
        The Python objects to display, or if raw=True raw svg data to
        display.
    raw : bool
        Are the data objects raw data or Python objects that need to be
        formatted before display? [default: False]
    """
    raw = kwargs.pop('raw',False)
    if raw:
        for obj in objs:
            publish_svg(obj)
    else:
        display(*objs, include=['text/plain','image/svg+xml'])

def display_svg ( * objs , ** kwargs ) : raw = kwargs . pop ( 'raw' , False ) if raw : for obj in objs : publish_svg ( obj ) else : display ( * objs , include = [ 'text/plain' , 'image/svg+xml' ] )

Display the SVG representation of an object .


def display_png(*objs, **kwargs):
    """Display the PNG representation of an object.

    Parameters
    ----------
    objs : tuple of objects
        The Python objects to display, or if raw=True raw png data to
        display.
    raw : bool
        Are the data objects raw data or Python objects that need to be
        formatted before display? [default: False]
    """
    raw = kwargs.pop('raw',False)
    if raw:
        for obj in objs:
            publish_png(obj)
    else:
        display(*objs, include=['text/plain','image/png'])

def display_png ( * objs , ** kwargs ) : raw = kwargs . pop ( 'raw' , False ) if raw : for obj in objs : publish_png ( obj ) else : display ( * objs , include = [ 'text/plain' , 'image/png' ] )

Display the PNG representation of an object .


def display_jpeg(*objs, **kwargs):
    """Display the JPEG representation of an object.

    Parameters
    ----------
    objs : tuple of objects
        The Python objects to display, or if raw=True raw JPEG data to
        display.
    raw : bool
        Are the data objects raw data or Python objects that need to be
        formatted before display? [default: False]
    """
    raw = kwargs.pop('raw',False)
    if raw:
        for obj in objs:
            publish_jpeg(obj)
    else:
        display(*objs, include=['text/plain','image/jpeg'])

def display_jpeg ( * objs , ** kwargs ) : raw = kwargs . pop ( 'raw' , False ) if raw : for obj in objs : publish_jpeg ( obj ) else : display ( * objs , include = [ 'text/plain' , 'image/jpeg' ] )

Display the JPEG representation of an object .


def display_latex(*objs, **kwargs):
    """Display the LaTeX representation of an object.

    Parameters
    ----------
    objs : tuple of objects
        The Python objects to display, or if raw=True raw latex data to
        display.
    raw : bool
        Are the data objects raw data or Python objects that need to be
        formatted before display? [default: False]
    """
    raw = kwargs.pop('raw',False)
    if raw:
        for obj in objs:
            publish_latex(obj)
    else:
        display(*objs, include=['text/plain','text/latex'])

def display_latex ( * objs , ** kwargs ) : raw = kwargs . pop ( 'raw' , False ) if raw : for obj in objs : publish_latex ( obj ) else : display ( * objs , include = [ 'text/plain' , 'text/latex' ] )

Display the LaTeX representation of an object .


def display_json(*objs, **kwargs):
    """Display the JSON representation of an object.

    Note that not many frontends support displaying JSON.

    Parameters
    ----------
    objs : tuple of objects
        The Python objects to display, or if raw=True raw json data to
        display.
    raw : bool
        Are the data objects raw data or Python objects that need to be
        formatted before display? [default: False]
    """
    raw = kwargs.pop('raw',False)
    if raw:
        for obj in objs:
            publish_json(obj)
    else:
        display(*objs, include=['text/plain','application/json'])

def display_json ( * objs , ** kwargs ) : raw = kwargs . pop ( 'raw' , False ) if raw : for obj in objs : publish_json ( obj ) else : display ( * objs , include = [ 'text/plain' , 'application/json' ] )

Display the JSON representation of an object .


def display_javascript(*objs, **kwargs):
    """Display the Javascript representation of an object.

    Parameters
    ----------
    objs : tuple of objects
        The Python objects to display, or if raw=True raw javascript data to
        display.
    raw : bool
        Are the data objects raw data or Python objects that need to be
        formatted before display? [default: False]
    """
    raw = kwargs.pop('raw',False)
    if raw:
        for obj in objs:
            publish_javascript(obj)
    else:
        display(*objs, include=['text/plain','application/javascript'])

def display_javascript ( * objs , ** kwargs ) : raw = kwargs . pop ( 'raw' , False ) if raw : for obj in objs : publish_javascript ( obj ) else : display ( * objs , include = [ 'text/plain' , 'application/javascript' ] )

Display the Javascript representation of an object .


def reload(self):
        """Reload the raw data from file or URL."""
        if self.filename is not None:
            with open(self.filename, self._read_flags) as f:
                self.data = f.read()
        elif self.url is not None:
            try:
                import urllib2
                response = urllib2.urlopen(self.url)
                self.data = response.read()
                # extract encoding from header, if there is one:
                encoding = None
                for sub in response.headers['content-type'].split(';'):
                    sub = sub.strip()
                    if sub.startswith('charset'):
                        encoding = sub.split('=')[-1].strip()
                        break
                # decode data, if an encoding was specified
                if encoding:
                    self.data = self.data.decode(encoding, 'replace')
            except:
                self.data = None

def reload ( self ) : if self . filename is not None : with open ( self . filename , self . _read_flags ) as f : self . data = f . read ( ) elif self . url is not None : try : import urllib2 response = urllib2 . urlopen ( self . url ) self . data = response . read ( ) encoding = None for sub in response . headers [ 'content-type' ] . split ( ';' ) : sub = sub . strip ( ) if sub . startswith ( 'charset' ) : encoding = sub . split ( '=' ) [ - 1 ] . strip ( ) break if encoding : self . data = self . data . decode ( encoding , 'replace' ) except : self . data = None


def _find_cmd(cmd):
    """Find the full path to a command using which."""

    path = sp.Popen(['/usr/bin/env', 'which', cmd],
                    stdout=sp.PIPE, stderr=sp.PIPE).communicate()[0]
    return py3compat.bytes_to_str(path)

def _find_cmd ( cmd ) : path = sp . Popen ( [ '/usr/bin/env' , 'which' , cmd ] , stdout = sp . PIPE , stderr = sp . PIPE ) . communicate ( ) [ 0 ] return py3compat . bytes_to_str ( path )

Find the full path to a command using which .


def system(self, cmd):
        """Execute a command in a subshell.

        Parameters
        ----------
        cmd : str
          A command to be executed in the system shell.

        Returns
        -------
        int : child's exitstatus
        """
        # Get likely encoding for the output.
        enc = DEFAULT_ENCODING

        # Patterns to match on the output, for pexpect.  We read input and
        # allow either a short timeout or EOF
        patterns = [pexpect.TIMEOUT, pexpect.EOF]
        # the index of the EOF pattern in the list.
        # even though we know it's 1, this call means we don't have to worry if
        # we change the above list, and forget to change this value:
        EOF_index = patterns.index(pexpect.EOF)
        # The size of the output stored so far in the process output buffer.
        # Since pexpect only appends to this buffer, each time we print we
        # record how far we've printed, so that next time we only print *new*
        # content from the buffer.
        out_size = 0
        try:
            # Since we're not really searching the buffer for text patterns, we
            # can set pexpect's search window to be tiny and it won't matter.
            # We only search for the 'patterns' timeout or EOF, which aren't in
            # the text itself.
            #child = pexpect.spawn(pcmd, searchwindowsize=1)
            if hasattr(pexpect, 'spawnb'):
                child = pexpect.spawnb(self.sh, args=['-c', cmd]) # Pexpect-U
            else:
                child = pexpect.spawn(self.sh, args=['-c', cmd])  # Vanilla Pexpect
            flush = sys.stdout.flush
            while True:
                # res is the index of the pattern that caused the match, so we
                # know whether we've finished (if we matched EOF) or not
                res_idx = child.expect_list(patterns, self.read_timeout)
                print(child.before[out_size:].decode(enc, 'replace'), end='')
                flush()
                if res_idx==EOF_index:
                    break
                # Update the pointer to what we've already printed
                out_size = len(child.before)
        except KeyboardInterrupt:
            # We need to send ^C to the process.  The ascii code for '^C' is 3
            # (the character is known as ETX for 'End of Text', see
            # curses.ascii.ETX).
            child.sendline(chr(3))
            # Read and print any more output the program might produce on its
            # way out.
            try:
                out_size = len(child.before)
                child.expect_list(patterns, self.terminate_timeout)
                print(child.before[out_size:].decode(enc, 'replace'), end='')
                sys.stdout.flush()
            except KeyboardInterrupt:
                # Impatient users tend to type it multiple times
                pass
            finally:
                # Ensure the subprocess really is terminated
                child.terminate(force=True)
        # add isalive check, to ensure exitstatus is set:
        child.isalive()
        return child.exitstatus

def system ( self , cmd ) : enc = DEFAULT_ENCODING patterns = [ pexpect . TIMEOUT , pexpect . EOF ] EOF_index = patterns . index ( pexpect . EOF ) out_size = 0 try : if hasattr ( pexpect , 'spawnb' ) : child = pexpect . spawnb ( self . sh , args = [ '-c' , cmd ] ) else : child = pexpect . spawn ( self . sh , args = [ '-c' , cmd ] ) flush = sys . stdout . flush while True : res_idx = child . expect_list ( patterns , self . read_timeout ) print ( child . before [ out_size : ] . decode ( enc , 'replace' ) , end = '' ) flush ( ) if res_idx == EOF_index : break out_size = len ( child . before ) except KeyboardInterrupt : child . sendline ( chr ( 3 ) ) try : out_size = len ( child . before ) child . expect_list ( patterns , self . terminate_timeout ) print ( child . before [ out_size : ] . decode ( enc , 'replace' ) , end = '' ) sys . stdout . flush ( ) except KeyboardInterrupt : pass finally : child . terminate ( force = True ) child . isalive ( ) return child . exitstatus


def forward_read_events(fd, context=None):
    """Forward read events from an FD over a socket.

    This method wraps a file in a socket pair, so it can
    be polled for read events by select (specifically zmq.eventloop.ioloop)
    """
    if context is None:
        context = zmq.Context.instance()
    push = context.socket(zmq.PUSH)
    push.setsockopt(zmq.LINGER, -1)
    pull = context.socket(zmq.PULL)
    addr='inproc://%s'%uuid.uuid4()
    push.bind(addr)
    pull.connect(addr)
    forwarder = ForwarderThread(push, fd)
    forwarder.start()
    return pull

def forward_read_events ( fd , context = None ) : if context is None : context = zmq . Context . instance ( ) push = context . socket ( zmq . PUSH ) push . setsockopt ( zmq . LINGER , - 1 ) pull = context . socket ( zmq . PULL ) addr = 'inproc://%s' % uuid . uuid4 ( ) push . bind ( addr ) pull . connect ( addr ) forwarder = ForwarderThread ( push , fd ) forwarder . start ( ) return pull

Forward read events from an FD over a socket .


def run(self):
        """Loop through lines in self.fd, and send them over self.sock."""
        line = self.fd.readline()
        # allow for files opened in unicode mode
        if isinstance(line, unicode):
            send = self.sock.send_unicode
        else:
            send = self.sock.send
        while line:
            send(line)
            line = self.fd.readline()
        # line == '' means EOF
        self.fd.close()
        self.sock.close()

def run ( self ) : line = self . fd . readline ( ) if isinstance ( line , unicode ) : send = self . sock . send_unicode else : send = self . sock . send while line : send ( line ) line = self . fd . readline ( ) self . fd . close ( ) self . sock . close ( )

Loop through lines in self . fd and send them over self . sock .


def find_launcher_class(clsname, kind):
    """Return a launcher for a given clsname and kind.

    Parameters
    ==========
    clsname : str
        The full name of the launcher class, either with or without the
        module path, or an abbreviation (MPI, SSH, SGE, PBS, LSF,
        WindowsHPC).
    kind : str
        Either 'EngineSet' or 'Controller'.
    """
    if '.' not in clsname:
        # not a module, presume it's the raw name in apps.launcher
        if kind and kind not in clsname:
            # doesn't match necessary full class name, assume it's
            # just 'PBS' or 'MPI' prefix:
            clsname = clsname + kind + 'Launcher'
        clsname = 'IPython.parallel.apps.launcher.'+clsname
    klass = import_item(clsname)
    return klass

def find_launcher_class ( clsname , kind ) : if '.' not in clsname : if kind and kind not in clsname : clsname = clsname + kind + 'Launcher' clsname = 'IPython.parallel.apps.launcher.' + clsname klass = import_item ( clsname ) return klass

Return a launcher for a given clsname and kind .


def start(self):
        """Start the app for the stop subcommand."""
        try:
            pid = self.get_pid_from_file()
        except PIDFileError:
            self.log.critical(
                'Could not read pid file, cluster is probably not running.'
            )
            # Here I exit with a unusual exit status that other processes
            # can watch for to learn how I existed.
            self.remove_pid_file()
            self.exit(ALREADY_STOPPED)

        if not self.check_pid(pid):
            self.log.critical(
                'Cluster [pid=%r] is not running.' % pid
            )
            self.remove_pid_file()
            # Here I exit with a unusual exit status that other processes
            # can watch for to learn how I existed.
            self.exit(ALREADY_STOPPED)

        elif os.name=='posix':
            sig = self.signal
            self.log.info(
                "Stopping cluster [pid=%r] with [signal=%r]" % (pid, sig)
            )
            try:
                os.kill(pid, sig)
            except OSError:
                self.log.error("Stopping cluster failed, assuming already dead.",
                    exc_info=True)
                self.remove_pid_file()
        elif os.name=='nt':
            try:
                # kill the whole tree
                p = check_call(['taskkill', '-pid', str(pid), '-t', '-f'], stdout=PIPE,stderr=PIPE)
            except (CalledProcessError, OSError):
                self.log.error("Stopping cluster failed, assuming already dead.",
                    exc_info=True)
            self.remove_pid_file()


def build_launcher(self, clsname, kind=None):
        """import and instantiate a Launcher based on importstring"""
        try:
            klass = find_launcher_class(clsname, kind)
        except (ImportError, KeyError):
            self.log.fatal("Could not import launcher class: %r"%clsname)
            self.exit(1)

        launcher = klass(
            work_dir=u'.', config=self.config, log=self.log,
            profile_dir=self.profile_dir.location, cluster_id=self.cluster_id,
        )
        return launcher

def
build_launcher
(
self
,
clsname
,
kind
=
)
:
try
:
klass
=
find_launcher_class
(
clsname
,
kind
)
except
(
ImportError
,
KeyError
)
:
self
.
log
.
fatal
(
"Could not import launcher class: %r"
%
clsname
)
self
.
exit
(
1
)
launcher
=
klass
(
work_dir
=
u'.'
,
config
=
self
.
config
,
log
=
self
.
log
,
profile_dir
=
self
.
profile_dir
.
location
,
cluster_id
=
self
.
cluster_id
,
)
return
launcher