def abs_file(filename):
    """Return the absolute normalized form of `filename`."""
    path = os.path.expandvars(os.path.expanduser(filename))
    path = os.path.abspath(os.path.realpath(path))
    path = actual_path(path)
    return path

def abs_file ( filename ) : path = os . path . expandvars ( os . path . expanduser ( filename ) ) path = os . path . abspath ( os . path . realpath ( path ) ) path = actual_path ( path ) return path

Return the absolute normalized form of filename .


def prep_patterns(patterns):
    """Prepare the file patterns for use in a `FnmatchMatcher`.

    If a pattern starts with a wildcard, it is used as a pattern
    as-is.  If it does not start with a wildcard, then it is made
    absolute with the current directory.

    If `patterns` is None, an empty list is returned.

    """
    prepped = []
    for p in patterns or []:
        if p.startswith("*") or p.startswith("?"):
            prepped.append(p)
        else:
            prepped.append(abs_file(p))
    return prepped

def prep_patterns ( patterns ) : prepped = [ ] for p in patterns or [ ] : if p . startswith ( "*" ) or p . startswith ( "?" ) : prepped . append ( p ) else : prepped . append ( abs_file ( p ) ) return prepped

Prepare the file patterns for use in a FnmatchMatcher .


def sep(s):
    """Find the path separator used in this string, or os.sep if none."""
    sep_match = re.search(r"[\\/]", s)
    if sep_match:
        the_sep = sep_match.group(0)
    else:
        the_sep = os.sep
    return the_sep

def sep ( s ) : sep_match = re . search ( r"[\\/]" , s ) if sep_match : the_sep = sep_match . group ( 0 ) else : the_sep = os . sep return the_sep

Find the path separator used in this string or os . sep if none .


def find_python_files(dirname):
    """Yield all of the importable Python files in `dirname`, recursively.

    To be importable, the files have to be in a directory with a __init__.py,
    except for `dirname` itself, which isn't required to have one.  The
    assumption is that `dirname` was specified directly, so the user knows
    best, but subdirectories are checked for a __init__.py to be sure we only
    find the importable files.

    """
    for i, (dirpath, dirnames, filenames) in enumerate(os.walk(dirname)):
        if i > 0 and '__init__.py' not in filenames:
            # If a directory doesn't have __init__.py, then it isn't
            # importable and neither are its files
            del dirnames[:]
            continue
        for filename in filenames:
            # We're only interested in files that look like reasonable Python
            # files: Must end with .py or .pyw, and must not have certain funny
            # characters that probably mean they are editor junk.
            if re.match(r"^[^.#~!$@%^&*()+=,]+\.pyw?$", filename):
                yield os.path.join(dirpath, filename)

def find_python_files ( dirname ) : for i , ( dirpath , dirnames , filenames ) in enumerate ( os . walk ( dirname ) ) : if i > 0 and '__init__.py' not in filenames : del dirnames [ : ] continue for filename in filenames : if re . match ( r"^[^.#~!$@%^&*()+=,]+\.pyw?$" , filename ) : yield os . path . join ( dirpath , filename )

Yield all of the importable Python files in dirname recursively .


def relative_filename(self, filename):
        """Return the relative form of `filename`.

        The filename will be relative to the current directory when the
        `FileLocator` was constructed.

        """
        fnorm = os.path.normcase(filename)
        if fnorm.startswith(self.relative_dir):
            filename = filename[len(self.relative_dir):]
        return filename

def relative_filename ( self , filename ) : fnorm = os . path . normcase ( filename ) if fnorm . startswith ( self . relative_dir ) : filename = filename [ len ( self . relative_dir ) : ] return filename


def canonical_filename(self, filename):
        """Return a canonical filename for `filename`.

        An absolute path with no redundant components and normalized case.

        """
        if filename not in self.canonical_filename_cache:
            if not os.path.isabs(filename):
                for path in [os.curdir] + sys.path:
                    if path is None:
                        continue
                    f = os.path.join(path, filename)
                    if os.path.exists(f):
                        filename = f
                        break
            cf = abs_file(filename)
            self.canonical_filename_cache[filename] = cf
        return self.canonical_filename_cache[filename]

def canonical_filename ( self , filename ) : if filename not in self . canonical_filename_cache : if not os . path . isabs ( filename ) : for path in [ os . curdir ] + sys . path : if path is None : continue f = os . path . join ( path , filename ) if os . path . exists ( f ) : filename = f break cf = abs_file ( filename ) self . canonical_filename_cache [ filename ] = cf return self . canonical_filename_cache [ filename ]

Return a canonical filename for filename .


def get_zip_data(self, filename):
        """Get data from `filename` if it is a zip file path.

        Returns the string data read from the zip file, or None if no zip file
        could be found or `filename` isn't in it.  The data returned will be
        an empty string if the file is empty.

        """
        import zipimport
        markers = ['.zip'+os.sep, '.egg'+os.sep]
        for marker in markers:
            if marker in filename:
                parts = filename.split(marker)
                try:
                    zi = zipimport.zipimporter(parts[0]+marker[:-1])
                except zipimport.ZipImportError:
                    continue
                try:
                    data = zi.get_data(parts[1])
                except IOError:
                    continue
                return to_string(data)
        return None

def get_zip_data ( self , filename ) : import zipimport markers = [ '.zip' + os . sep , '.egg' + os . sep ] for marker in markers : if marker in filename : parts = filename . split ( marker ) try : zi = zipimport . zipimporter ( parts [ 0 ] + marker [ : - 1 ] ) except zipimport . ZipImportError : continue try : data = zi . get_data ( parts [ 1 ] ) except IOError : continue return to_string ( data ) return None

Get data from filename if it is a zip file path .


def match(self, fpath):
        """Does `fpath` indicate a file in one of our trees?"""
        for d in self.dirs:
            if fpath.startswith(d):
                if fpath == d:
                    # This is the same file!
                    return True
                if fpath[len(d)] == os.sep:
                    # This is a file in the directory
                    return True
        return False

def match ( self , fpath ) : for d in self . dirs : if fpath . startswith ( d ) : if fpath == d : return True if fpath [ len ( d ) ] == os . sep : return True return False

Does fpath indicate a file in one of our trees?


def match(self, fpath):
        """Does `fpath` match one of our filename patterns?"""
        for pat in self.pats:
            if fnmatch.fnmatch(fpath, pat):
                return True
        return False

def match ( self , fpath ) : for pat in self . pats : if fnmatch . fnmatch ( fpath , pat ) : return True return False

Does fpath match one of our filename patterns?


def map(self, path):
        """Map `path` through the aliases.

        `path` is checked against all of the patterns.  The first pattern to
        match is used to replace the root of the path with the result root.
        Only one pattern is ever used.  If no patterns match, `path` is
        returned unchanged.

        The separator style in the result is made to match that of the result
        in the alias.

        """
        for regex, result, pattern_sep, result_sep in self.aliases:
            m = regex.match(path)
            if m:
                new = path.replace(m.group(0), result)
                if pattern_sep != result_sep:
                    new = new.replace(pattern_sep, result_sep)
                if self.locator:
                    new = self.locator.canonical_filename(new)
                return new
        return path

def map ( self , path ) : for regex , result , pattern_sep , result_sep in self . aliases : m = regex . match ( path ) if m : new = path . replace ( m . group ( 0 ) , result ) if pattern_sep != result_sep : new = new . replace ( pattern_sep , result_sep ) if self . locator : new = self . locator . canonical_filename ( new ) return new return path


def loop_qt4(kernel):
    """Start a kernel with PyQt4 event loop integration."""

    from IPython.external.qt_for_kernel import QtCore
    from IPython.lib.guisupport import get_app_qt4, start_event_loop_qt4

    kernel.app = get_app_qt4([" "])
    kernel.app.setQuitOnLastWindowClosed(False)
    kernel.timer = QtCore.QTimer()
    kernel.timer.timeout.connect(kernel.do_one_iteration)
    # Units for the timer are in milliseconds
    kernel.timer.start(1000*kernel._poll_interval)
    start_event_loop_qt4(kernel.app)

def
loop_qt4
(
kernel
)
:
from
IPython
.
external
.
qt_for_kernel
import
QtCore
from
IPython
.
lib
.
guisupport
import
get_app_qt4
,
start_event_loop_qt4
kernel
.
app
=
get_app_qt4
(
[
" "
]
)
kernel
.
app
.
setQuitOnLastWindowClosed
(
False
)
kernel
.
timer
=
QtCore
.
QTimer
(
)
kernel
.
timer
.
timeout
.
connect
(
kernel
.
do_one_iteration
)
kernel
.
timer
.
start
(
1000
*
kernel
.
_poll_interval
)
start_event_loop_qt4
(
kernel
.
app
)

Start a kernel with PyQt4 event loop integration .


def loop_wx(kernel):
    """Start a kernel with wx event loop support."""

    import wx
    from IPython.lib.guisupport import start_event_loop_wx

    doi = kernel.do_one_iteration
     # Wx uses milliseconds
    poll_interval = int(1000*kernel._poll_interval)

    # We have to put the wx.Timer in a wx.Frame for it to fire properly.
    # We make the Frame hidden when we create it in the main app below.
    class TimerFrame(wx.Frame):
        def __init__(self, func):
            wx.Frame.__init__(self, None, -1)
            self.timer = wx.Timer(self)
            # Units for the timer are in milliseconds
            self.timer.Start(poll_interval)
            self.Bind(wx.EVT_TIMER, self.on_timer)
            self.func = func

        def on_timer(self, event):
            self.func()

    # We need a custom wx.App to create our Frame subclass that has the
    # wx.Timer to drive the ZMQ event loop.
    class IPWxApp(wx.App):
        def OnInit(self):
            self.frame = TimerFrame(doi)
            self.frame.Show(False)
            return True

    # The redirect=False here makes sure that wx doesn't replace
    # sys.stdout/stderr with its own classes.
    kernel.app = IPWxApp(redirect=False)

    # The import of wx on Linux sets the handler for signal.SIGINT
    # to 0.  This is a bug in wx or gtk.  We fix by just setting it
    # back to the Python default.
    import signal
    if not callable(signal.getsignal(signal.SIGINT)):
        signal.signal(signal.SIGINT, signal.default_int_handler)

    start_event_loop_wx(kernel.app)

def loop_wx ( kernel ) : import wx from IPython . lib . guisupport import start_event_loop_wx doi = kernel . do_one_iteration poll_interval = int ( 1000 * kernel . _poll_interval ) class TimerFrame ( wx . Frame ) : def __init__ ( self , func ) : wx . Frame . __init__ ( self , None , - 1 ) self . timer = wx . Timer ( self ) self . timer . Start ( poll_interval ) self . Bind ( wx . EVT_TIMER , self . on_timer ) self . func = func def on_timer ( self , event ) : self . func ( ) class IPWxApp ( wx . App ) : def OnInit ( self ) : self . frame = TimerFrame ( doi ) self . frame . Show ( False ) return True kernel . app = IPWxApp ( redirect = False ) import signal if not callable ( signal . getsignal ( signal . SIGINT ) ) : signal . signal ( signal . SIGINT , signal . default_int_handler ) start_event_loop_wx ( kernel . app )

Start a kernel with wx event loop support .


def loop_tk(kernel):
    """Start a kernel with the Tk event loop."""

    import Tkinter
    doi = kernel.do_one_iteration
    # Tk uses milliseconds
    poll_interval = int(1000*kernel._poll_interval)
    # For Tkinter, we create a Tk object and call its withdraw method.
    class Timer(object):
        def __init__(self, func):
            self.app = Tkinter.Tk()
            self.app.withdraw()
            self.func = func

        def on_timer(self):
            self.func()
            self.app.after(poll_interval, self.on_timer)

        def start(self):
            self.on_timer()  # Call it once to get things going.
            self.app.mainloop()

    kernel.timer = Timer(doi)
    kernel.timer.start()

def loop_tk ( kernel ) : import Tkinter doi = kernel . do_one_iteration poll_interval = int ( 1000 * kernel . _poll_interval ) class Timer ( object ) : def __init__ ( self , func ) : self . app = Tkinter . Tk ( ) self . app . withdraw ( ) self . func = func def on_timer ( self ) : self . func ( ) self . app . after ( poll_interval , self . on_timer ) def start ( self ) : self . on_timer ( ) self . app . mainloop ( ) kernel . timer = Timer ( doi ) kernel . timer . start ( )


def loop_gtk(kernel):
    """Start the kernel, coordinating with the GTK event loop"""
    from .gui.gtkembed import GTKEmbed

    gtk_kernel = GTKEmbed(kernel)
    gtk_kernel.start()

def loop_gtk ( kernel ) : from . gui . gtkembed import GTKEmbed gtk_kernel = GTKEmbed ( kernel ) gtk_kernel . start ( )

Start the kernel coordinating with the GTK event loop


def loop_cocoa(kernel):
    """Start the kernel, coordinating with the Cocoa CFRunLoop event loop
    via the matplotlib MacOSX backend.
    """
    import matplotlib
    if matplotlib.__version__ < '1.1.0':
        kernel.log.warn(
        "MacOSX backend in matplotlib %s doesn't have a Timer, "
        "falling back on Tk for CFRunLoop integration.  Note that "
        "even this won't work if Tk is linked against X11 instead of "
        "Cocoa (e.g. EPD).  To use the MacOSX backend in the kernel, "
        "you must use matplotlib >= 1.1.0, or a native libtk."
        )
        return loop_tk(kernel)

    from matplotlib.backends.backend_macosx import TimerMac, show

    # scale interval for sec->ms
    poll_interval = int(1000*kernel._poll_interval)

    real_excepthook = sys.excepthook
    def handle_int(etype, value, tb):
        """don't let KeyboardInterrupts look like crashes"""
        if etype is KeyboardInterrupt:
            io.raw_print("KeyboardInterrupt caught in CFRunLoop")
        else:
            real_excepthook(etype, value, tb)

    # add doi() as a Timer to the CFRunLoop
    def doi():
        # restore excepthook during IPython code
        sys.excepthook = real_excepthook
        kernel.do_one_iteration()
        # and back:
        sys.excepthook = handle_int

    t = TimerMac(poll_interval)
    t.add_callback(doi)
    t.start()

    # but still need a Poller for when there are no active windows,
    # during which time mainloop() returns immediately
    poller = zmq.Poller()
    if kernel.control_stream:
        poller.register(kernel.control_stream.socket, zmq.POLLIN)
    for stream in kernel.shell_streams:
        poller.register(stream.socket, zmq.POLLIN)

    while True:
        try:
            # double nested try/except, to properly catch KeyboardInterrupt
            # due to pyzmq Issue #130
            try:
                # don't let interrupts during mainloop invoke crash_handler:
                sys.excepthook = handle_int
                show.mainloop()
                sys.excepthook = real_excepthook
                # use poller if mainloop returned (no windows)
                # scale by extra factor of 10, since it's a real poll
                poller.poll(10*poll_interval)
                kernel.do_one_iteration()
            except:
                raise
        except KeyboardInterrupt:
            # Ctrl-C shouldn't crash the kernel
            io.raw_print("KeyboardInterrupt caught in kernel")
        finally:
            # ensure excepthook is restored
            sys.excepthook = real_excepthook

Start the kernel coordinating with the Cocoa CFRunLoop event loop via the matplotlib MacOSX backend .


def enable_gui(gui, kernel=None):
    """Enable integration with a given GUI"""
    if gui not in loop_map:
        raise ValueError("GUI %r not supported" % gui)
    if kernel is None:
        if Application.initialized():
            kernel = getattr(Application.instance(), 'kernel', None)
        if kernel is None:
            raise RuntimeError("You didn't specify a kernel,"
                " and no IPython Application with a kernel appears to be running."
            )
    loop = loop_map[gui]
    if kernel.eventloop is not None and kernel.eventloop is not loop:
        raise RuntimeError("Cannot activate multiple GUI eventloops")
    kernel.eventloop = loop


def GOE(N):
    """Creates an NxN element of the Gaussian Orthogonal Ensemble"""
    m = ra.standard_normal((N,N))
    m += m.T
    return m/2

def GOE ( N ) : m = ra . standard_normal ( ( N , N ) ) m += m . T return m / 2


def center_eigenvalue_diff(mat):
    """Compute the eigvals of mat and then find the center eigval difference."""
    N = len(mat)
    evals = np.sort(la.eigvals(mat))
    diff = np.abs(evals[N/2] - evals[N/2-1])
    return diff

def center_eigenvalue_diff ( mat ) : N = len ( mat ) evals = np . sort ( la . eigvals ( mat ) ) diff = np . abs ( evals [ N / 2 ] - evals [ N / 2 - 1 ] ) return diff

Compute the eigvals of mat and then find the center eigval difference .


def ensemble_diffs(num, N):
    """Return num eigenvalue diffs for the NxN GOE ensemble."""
    diffs = np.empty(num)
    for i in xrange(num):
        mat = GOE(N)
        diffs[i] = center_eigenvalue_diff(mat)
    return diffs

def ensemble_diffs ( num , N ) : diffs = np . empty ( num ) for i in xrange ( num ) : mat = GOE ( N ) diffs [ i ] = center_eigenvalue_diff ( mat ) return diffs

Return num eigenvalue diffs for the NxN GOE ensemble .


def init(self, ctxt, step_addr):
        """
        Initialize the item.  This calls the class constructor with the
        appropriate arguments and returns the initialized object.

        :param ctxt: The context object.
        :param step_addr: The address of the step in the test
                          configuration.
        """

        return self.cls(ctxt, self.name, self.conf, step_addr)

def init ( self , ctxt , step_addr ) : return self . cls ( ctxt , self . name , self . conf , step_addr )

Initialize the item . This calls the class constructor with the appropriate arguments and returns the initialized object .


def parse_file(cls, ctxt, fname, key=None, step_addr=None):
        """
        Parse a YAML file containing test steps.

        :param ctxt: The context object.
        :param fname: The name of the file to parse.
        :param key: An optional dictionary key.  If specified, the
                    file must be a YAML dictionary, and the referenced
                    value will be interpreted as a list of steps.  If
                    not provided, the file must be a YAML list, which
                    will be interpreted as the list of steps.
        :param step_addr: The address of the step in the test
                          configuration.  This may be used in the case
                          of includes, for instance.

        :returns: A list of ``Step`` objects.
        """

        # Load the YAML file
        try:
            with open(fname) as f:
                step_data = yaml.load(f)
        except Exception as exc:
            raise ConfigError(
                'Failed to read file "%s": %s' % (fname, exc),
                step_addr,
            )

        # Do we have a key?
        if key is not None:
            if (not isinstance(step_data, collections.Mapping) or
                    key not in step_data):
                raise ConfigError(
                    'Bad step configuration file "%s": expecting dictionary '
                    'with key "%s"' % (fname, key),
                    step_addr,
                )

            # Extract just the step data
            step_data = step_data[key]

        # Validate that it's a sequence
        if not isinstance(step_data, collections.Sequence):
            addr = ('%s[%s]' % (fname, key)) if key is not None else fname
            raise ConfigError(
                'Bad step configuration sequence at %s: expecting list, '
                'not "%s"' % (addr, step_data.__class__.__name__),
                step_addr,
            )

        # OK, assemble the step list and return it
        steps = []
        for idx, step_conf in enumerate(step_data):
            steps.extend(cls.parse_step(
                ctxt, StepAddress(fname, idx, key), step_conf))

        return steps

Parse a YAML file containing test steps .


def parse_step(cls, ctxt, step_addr, step_conf):
        """
        Parse a step dictionary.

        :param ctxt: The context object.
        :param step_addr: The address of the step in the test
                          configuration.
        :param step_conf: The description of the step.  This may be a
                          scalar string or a dictionary.

        :returns: A list of steps.
        """

        # Make sure the step makes sense
        if isinstance(step_conf, six.string_types):
            # Convert string to a dict for uniformity of processing
            step_conf = {step_conf: None}
        elif not isinstance(step_conf, collections.Mapping):
            raise ConfigError(
                'Unable to parse step configuration: expecting string or '
                'dictionary, not "%s"' % step_conf.__class__.__name__,
                step_addr,
            )

        # Parse the configuration into the action and modifier classes
        # and the configuration to apply to each
        action_item = None
        mod_items = {}
        kwargs = {}  # extra args for Step.__init__()
        for key, key_conf in step_conf.items():
            # Handle special keys first
            if key in cls.schemas:
                # Validate the key
                utils.schema_validate(key_conf, cls.schemas[key], ConfigError,
                                      key, step_addr=step_addr)

                # Save the value
                kwargs[key] = key_conf

            # Is it an action?
            elif key in entry.points[NAMESPACE_ACTION]:
                if action_item is not None:
                    raise ConfigError(
                        'Bad step configuration: action "%s" specified, '
                        'but action "%s" already processed' %
                        (key, action_item.name),
                        step_addr,
                    )

                action_item = StepItem(
                    entry.points[NAMESPACE_ACTION][key], key, key_conf)

            # OK, is it a modifier?
            elif key in entry.points[NAMESPACE_MODIFIER]:
                mod_class = entry.points[NAMESPACE_MODIFIER][key]

                # Store it in priority order
                mod_items.setdefault(mod_class.priority, [])
                mod_items[mod_class.priority].append(StepItem(
                    mod_class, key, key_conf))

            # Couldn't resolve it
            else:
                raise ConfigError(
                    'Bad step configuration: unable to resolve action '
                    '"%s"' % key,
                    step_addr,
                )

        # Make sure we have an action
        if action_item is None:
            raise ConfigError(
                'Bad step configuration: no action specified',
                step_addr,
            )

        # What is the action type?
        action_type = (Modifier.STEP if action_item.cls.step_action
                       else Modifier.NORMAL)

        # OK, build our modifiers list and preprocess the action
        # configuration
        modifiers = []
        for mod_item in utils.iter_prio_dict(mod_items):
            # Verify that the modifier is compatible with the
            # action
            if mod_item.cls.restriction & action_type == 0:
                raise ConfigError(
                    'Bad step configuration: modifier "%s" is '
                    'incompatible with the action "%s"' %
                    (mod_item.name, action_item.name),
                    step_addr,
                )

            # Initialize the modifier
            modifier = mod_item.init(ctxt, step_addr)

            # Add it to the list of modifiers
            modifiers.append(modifier)

            # Apply the modifier's configuration processing
            action_item.conf = modifier.action_conf(
                ctxt, action_item.cls, action_item.name, action_item.conf,
                step_addr)

        # Now we can initialize the action
        action = action_item.init(ctxt, step_addr)

        # Create the step
        step = cls(step_addr, action, modifiers, **kwargs)

        # If the final_action is a StepAction, invoke it now and
        # return the list of steps.  We do this after creating the
        # Step object so that we can take advantage of its handling of
        # modifiers.
        if action_item.cls.step_action:
            return step(ctxt)

        # Not a step action, return the step as a list of one element
        return [step]


def init_crash_handler(self):
        """Create a crash handler, typically setting sys.excepthook to it."""
        self.crash_handler = self.crash_handler_class(self)
        sys.excepthook = self.excepthook
        def unset_crashhandler():
            sys.excepthook = sys.__excepthook__
        atexit.register(unset_crashhandler)

def init_crash_handler ( self ) : self . crash_handler = self . crash_handler_class ( self ) sys . excepthook = self . excepthook def unset_crashhandler ( ) : sys . excepthook = sys . __excepthook__ atexit . register ( unset_crashhandler )

Create a crash handler typically setting sys . excepthook to it .


def load_config_file(self, suppress_errors=True):
        """Load the config file.

        By default, errors in loading config are handled, and a warning
        printed on screen. For testing, the suppress_errors option is set
        to False, so errors will make tests fail.
        """
        self.log.debug("Searching path %s for config files", self.config_file_paths)
        base_config = 'ipython_config.py'
        self.log.debug("Attempting to load config file: %s" %
                       base_config)
        try:
            Application.load_config_file(
                self,
                base_config,
                path=self.config_file_paths
            )
        except ConfigFileNotFound:
            # ignore errors loading parent
            self.log.debug("Config file %s not found", base_config)
            pass
        if self.config_file_name == base_config:
            # don't load secondary config
            return
        self.log.debug("Attempting to load config file: %s" %
                       self.config_file_name)
        try:
            Application.load_config_file(
                self,
                self.config_file_name,
                path=self.config_file_paths
            )
        except ConfigFileNotFound:
            # Only warn if the default config file was NOT being used.
            if self.config_file_specified:
                msg = self.log.warn
            else:
                msg = self.log.debug
            msg("Config file not found, skipping: %s", self.config_file_name)
        except:
            # For testing purposes.
            if not suppress_errors:
                raise
            self.log.warn("Error loading config file: %s" %
                          self.config_file_name, exc_info=True)

def
load_config_file
(
self
,
suppress_errors
=
True
)
:
self
.
log
.
debug
(
"Searching path %s for config files"
,
self
.
config_file_paths
)
base_config
=
'ipython_config.py'
self
.
log
.
debug
(
"Attempting to load config file: %s"
%
base_config
)
try
:
Application
.
load_config_file
(
self
,
base_config
,
path
=
self
.
config_file_paths
)
except
ConfigFileNotFound
:
self
.
log
.
debug
(
"Config file %s not found"
,
base_config
)
pass
if
self
.
config_file_name
==
base_config
:
return
self
.
log
.
debug
(
"Attempting to load config file: %s"
%
self
.
config_file_name
)
try
:
Application
.
load_config_file
(
self
,
self
.
config_file_name
,
path
=
self
.
config_file_paths
)
except
ConfigFileNotFound
:
if
self
.
config_file_specified
:
msg
=
self
.
log
.
warn
else
:
msg
=
self
.
log
.
debug
msg
(
"Config file not found, skipping: %s"
,
self
.
config_file_name
)
except
:
if
not
suppress_errors
:
raise
self
.
log
.
warn
(
"Error loading config file: %s"
%
self
.
config_file_name
,
exc_info
=
True
)


def init_profile_dir(self):
        """initialize the profile dir"""
        try:
            # location explicitly specified:
            location = self.config.ProfileDir.location
        except AttributeError:
            # location not specified, find by profile name
            try:
                p = ProfileDir.find_profile_dir_by_name(self.ipython_dir, self.profile, self.config)
            except ProfileDirError:
                # not found, maybe create it (always create default profile)
                if self.auto_create or self.profile=='default':
                    try:
                        p = ProfileDir.create_profile_dir_by_name(self.ipython_dir, self.profile, self.config)
                    except ProfileDirError:
                        self.log.fatal("Could not create profile: %r"%self.profile)
                        self.exit(1)
                    else:
                        self.log.info("Created profile dir: %r"%p.location)
                else:
                    self.log.fatal("Profile %r not found."%self.profile)
                    self.exit(1)
            else:
                self.log.info("Using existing profile dir: %r"%p.location)
        else:
            # location is fully specified
            try:
                p = ProfileDir.find_profile_dir(location, self.config)
            except ProfileDirError:
                # not found, maybe create it
                if self.auto_create:
                    try:
                        p = ProfileDir.create_profile_dir(location, self.config)
                    except ProfileDirError:
                        self.log.fatal("Could not create profile directory: %r"%location)
                        self.exit(1)
                    else:
                        self.log.info("Creating new profile dir: %r"%location)
                else:
                    self.log.fatal("Profile directory %r not found."%location)
                    self.exit(1)
            else:
                self.log.info("Using existing profile dir: %r"%location)

        self.profile_dir = p
        self.config_file_paths.append(p.location)


def stage_default_config_file(self):
        """auto generate default config file, and stage it into the profile."""
        s = self.generate_config_file()
        fname = os.path.join(self.profile_dir.location, self.config_file_name)
        if self.overwrite or not os.path.exists(fname):
            self.log.warn("Generating default config file: %r"%(fname))
            with open(fname, 'w') as f:
                f.write(s)

def
stage_default_config_file
(
self
)
:
s
=
self
.
generate_config_file
(
)
fname
=
os
.
path
.
join
(
self
.
profile_dir
.
location
,
self
.
config_file_name
)
if
self
.
overwrite
or
not
os
.
path
.
exists
(
fname
)
:
self
.
log
.
warn
(
"Generating default config file: %r"
%
(
fname
)
)
with
open
(
fname
,
'w'
)
as
f
:
f
.
write
(
s
)

auto generate default config file and stage it into the profile .


def write(self, suffix=None):
        """Write the collected coverage data to a file.

        `suffix` is a suffix to append to the base file name. This can be used
        for multiple or parallel execution, so that many coverage data files
        can exist simultaneously.  A dot will be used to join the base name and
        the suffix.

        """
        if self.use_file:
            filename = self.filename
            if suffix:
                filename += "." + suffix
            self.write_file(filename)

def write ( self , suffix = None ) : if self . use_file : filename = self . filename if suffix : filename += "." + suffix self . write_file ( filename )

Write the collected coverage data to a file .


def erase(self):
        """Erase the data, both in this object, and from its file storage."""
        if self.use_file:
            if self.filename:
                file_be_gone(self.filename)
        self.lines = {}
        self.arcs = {}

def erase ( self ) : if self . use_file : if self . filename : file_be_gone ( self . filename ) self . lines = { } self . arcs = { }

Erase the data both in this object and from its file storage .


def line_data(self):
        """Return the map from filenames to lists of line numbers executed."""
        return dict(
            [(f, sorted(lmap.keys())) for f, lmap in iitems(self.lines)]
            )

def line_data ( self ) : return dict ( [ ( f , sorted ( lmap . keys ( ) ) ) for f , lmap in iitems ( self . lines ) ] )

Return the map from filenames to lists of line numbers executed .


def arc_data(self):
        """Return the map from filenames to lists of line number pairs."""
        return dict(
            [(f, sorted(amap.keys())) for f, amap in iitems(self.arcs)]
            )

def arc_data ( self ) : return dict ( [ ( f , sorted ( amap . keys ( ) ) ) for f , amap in iitems ( self . arcs ) ] )

Return the map from filenames to lists of line number pairs .


def write_file(self, filename):
        """Write the coverage data to `filename`."""

        # Create the file data.
        data = {}

        data['lines'] = self.line_data()
        arcs = self.arc_data()
        if arcs:
            data['arcs'] = arcs

        if self.collector:
            data['collector'] = self.collector

        if self.debug and self.debug.should('dataio'):
            self.debug.write("Writing data to %r" % (filename,))

        # Write the pickle to the file.
        fdata = open(filename, 'wb')
        try:
            pickle.dump(data, fdata, 2)
        finally:
            fdata.close()

def
write_file
(
self
,
filename
)
:
data
=
{
}
data
[
'lines'
]
=
self
.
line_data
(
)
arcs
=
self
.
arc_data
(
)
if
arcs
:
data
[
'arcs'
]
=
arcs
if
self
.
collector
:
data
[
'collector'
]
=
self
.
collector
if
self
.
debug
and
self
.
debug
.
should
(
'dataio'
)
:
self
.
debug
.
write
(
"Writing data to %r"
%
(
filename
,
)
)
fdata
=
open
(
filename
,
'wb'
)
try
:
pickle
.
dump
(
data
,
fdata
,
2
)
finally
:
fdata
.
close
(
)


def read_file(self, filename):
        """Read the coverage data from `filename`."""
        self.lines, self.arcs = self._read_file(filename)

def read_file ( self , filename ) : self . lines , self . arcs = self . _read_file ( filename )


def raw_data(self, filename):
        """Return the raw pickled data from `filename`."""
        if self.debug and self.debug.should('dataio'):
            self.debug.write("Reading data from %r" % (filename,))
        fdata = open(filename, 'rb')
        try:
            data = pickle.load(fdata)
        finally:
            fdata.close()
        return data

def
raw_data
(
self
,
filename
)
:
if
self
.
debug
and
self
.
debug
.
should
(
'dataio'
)
:
self
.
debug
.
write
(
"Reading data from %r"
%
(
filename
,
)
)
fdata
=
open
(
filename
,
'rb'
)
try
:
data
=
pickle
.
load
(
fdata
)
finally
:
fdata
.
close
(
)
return
data

Return the raw pickled data from filename .


def _read_file(self, filename):
        """Return the stored coverage data from the given file.

        Returns two values, suitable for assigning to `self.lines` and
        `self.arcs`.

        """
        lines = {}
        arcs = {}
        try:
            data = self.raw_data(filename)
            if isinstance(data, dict):
                # Unpack the 'lines' item.
                lines = dict([
                    (f, dict.fromkeys(linenos, None))
                        for f, linenos in iitems(data.get('lines', {}))
                    ])
                # Unpack the 'arcs' item.
                arcs = dict([
                    (f, dict.fromkeys(arcpairs, None))
                        for f, arcpairs in iitems(data.get('arcs', {}))
                    ])
        except Exception:
            pass
        return lines, arcs

def _read_file ( self , filename ) : lines = { } arcs = { } try : data = self . raw_data ( filename ) if isinstance ( data , dict ) : lines = dict ( [ ( f , dict . fromkeys ( linenos , None ) ) for f , linenos in iitems ( data . get ( 'lines' , { } ) ) ] ) arcs = dict ( [ ( f , dict . fromkeys ( arcpairs , None ) ) for f , arcpairs in iitems ( data . get ( 'arcs' , { } ) ) ] ) except Exception : pass return lines , arcs

Return the stored coverage data from the given file .


def combine_parallel_data(self, aliases=None):
        """Combine a number of data files together.

        Treat `self.filename` as a file prefix, and combine the data from all
        of the data files starting with that prefix plus a dot.

        If `aliases` is provided, it's a `PathAliases` object that is used to
        re-map paths to match the local machine's.

        """
        aliases = aliases or PathAliases()
        data_dir, local = os.path.split(self.filename)
        localdot = local + '.'
        for f in os.listdir(data_dir or '.'):
            if f.startswith(localdot):
                full_path = os.path.join(data_dir, f)
                new_lines, new_arcs = self._read_file(full_path)
                for filename, file_data in iitems(new_lines):
                    filename = aliases.map(filename)
                    self.lines.setdefault(filename, {}).update(file_data)
                for filename, file_data in iitems(new_arcs):
                    filename = aliases.map(filename)
                    self.arcs.setdefault(filename, {}).update(file_data)
                if f != local:
                    os.remove(full_path)

def combine_parallel_data ( self , aliases = None ) : aliases = aliases or PathAliases ( ) data_dir , local = os . path . split ( self . filename ) localdot = local + '.' for f in os . listdir ( data_dir or '.' ) : if f . startswith ( localdot ) : full_path = os . path . join ( data_dir , f ) new_lines , new_arcs = self . _read_file ( full_path ) for filename , file_data in iitems ( new_lines ) : filename = aliases . map ( filename ) self . lines . setdefault ( filename , { } ) . update ( file_data ) for filename , file_data in iitems ( new_arcs ) : filename = aliases . map ( filename ) self . arcs . setdefault ( filename , { } ) . update ( file_data ) if f != local : os . remove ( full_path )

Combine a number of data files together .


def add_line_data(self, line_data):
        """Add executed line data.

        `line_data` is { filename: { lineno: None, ... }, ...}

        """
        for filename, linenos in iitems(line_data):
            self.lines.setdefault(filename, {}).update(linenos)

def add_line_data ( self , line_data ) : for filename , linenos in iitems ( line_data ) : self . lines . setdefault ( filename , { } ) . update ( linenos )


def add_arc_data(self, arc_data):
        """Add measured arc data.

        `arc_data` is { filename: { (l1,l2): None, ... }, ...}

        """
        for filename, arcs in iitems(arc_data):
            self.arcs.setdefault(filename, {}).update(arcs)

def add_arc_data ( self , arc_data ) : for filename , arcs in iitems ( arc_data ) : self . arcs . setdefault ( filename , { } ) . update ( arcs )


def add_to_hash(self, filename, hasher):
        """Contribute `filename`'s data to the Md5Hash `hasher`."""
        hasher.update(self.executed_lines(filename))
        hasher.update(self.executed_arcs(filename))

def add_to_hash ( self , filename , hasher ) : hasher . update ( self . executed_lines ( filename ) ) hasher . update ( self . executed_arcs ( filename ) )

Contribute filename s data to the Md5Hash hasher .


def summary(self, fullpath=False):
        """Return a dict summarizing the coverage data.

        Keys are based on the filenames, and values are the number of executed
        lines.  If `fullpath` is true, then the keys are the full pathnames of
        the files, otherwise they are the basenames of the files.

        """
        summ = {}
        if fullpath:
            filename_fn = lambda f: f
        else:
            filename_fn = os.path.basename
        for filename, lines in iitems(self.lines):
            summ[filename_fn(filename)] = len(lines)
        return summ

def summary ( self , fullpath = False ) : summ = { } if fullpath : filename_fn = lambda f : f else : filename_fn = os . path . basename for filename , lines in iitems ( self . lines ) : summ [ filename_fn ( filename ) ] = len ( lines ) return summ

Return a dict summarizing the coverage data .


def get_pasted_lines(sentinel, l_input=py3compat.input):
    """ Yield pasted lines until the user enters the given sentinel value.
    """
    print "Pasting code; enter '%s' alone on the line to stop or use Ctrl-D." \
          % sentinel
    while True:
        try:
            l = l_input(':')
            if l == sentinel:
                return
            else:
                yield l
        except EOFError:
            print '<EOF>'
            return

Yield pasted lines until the user enters the given sentinel value .


def mainloop(self, display_banner=None):
        """Start the mainloop.

        If an optional banner argument is given, it will override the
        internally created default banner.
        """

        with nested(self.builtin_trap, self.display_trap):

            while 1:
                try:
                    self.interact(display_banner=display_banner)
                    #self.interact_with_readline()
                    # XXX for testing of a readline-decoupled repl loop, call
                    # interact_with_readline above
                    break
                except KeyboardInterrupt:
                    # this should not be necessary, but KeyboardInterrupt
                    # handling seems rather unpredictable...
                    self.write("\nKeyboardInterrupt in interact()\n")

def
mainloop
(
self
,
display_banner
=
)
:
with
nested
(
self
.
builtin_trap
,
self
.
display_trap
)
:
while
1
:
try
:
self
.
interact
(
display_banner
=
display_banner
)
break
except
KeyboardInterrupt
:
self
.
write
(
"\nKeyboardInterrupt in interact()\n"
)


def _replace_rlhist_multiline(self, source_raw, hlen_before_cell):
        """Store multiple lines as a single entry in history"""

        # do nothing without readline or disabled multiline
        if not self.has_readline or not self.multiline_history:
            return hlen_before_cell

        # windows rl has no remove_history_item
        if not hasattr(self.readline, "remove_history_item"):
            return hlen_before_cell

        # skip empty cells
        if not source_raw.rstrip():
            return hlen_before_cell

        # nothing changed do nothing, e.g. when rl removes consecutive dups
        hlen = self.readline.get_current_history_length()
        if hlen == hlen_before_cell:
            return hlen_before_cell

        for i in range(hlen - hlen_before_cell):
            self.readline.remove_history_item(hlen - i - 1)
        stdin_encoding = get_stream_enc(sys.stdin, 'utf-8')
        self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(),
                                    stdin_encoding))
        return self.readline.get_current_history_length()

def _replace_rlhist_multiline ( self , source_raw , hlen_before_cell ) : if not self . has_readline or not self . multiline_history : return hlen_before_cell if not hasattr ( self . readline , "remove_history_item" ) : return hlen_before_cell if not source_raw . rstrip ( ) : return hlen_before_cell hlen = self . readline . get_current_history_length ( ) if hlen == hlen_before_cell : return hlen_before_cell for i in range ( hlen - hlen_before_cell ) : self . readline . remove_history_item ( hlen - i - 1 ) stdin_encoding = get_stream_enc ( sys . stdin , 'utf-8' ) self . readline . add_history ( py3compat . unicode_to_str ( source_raw . rstrip ( ) , stdin_encoding ) ) return self . readline . get_current_history_length ( )


def raw_input(self, prompt=''):
        """Write a prompt and read a line.

        The returned line does not include the trailing newline.
        When the user enters the EOF key sequence, EOFError is raised.

        Optional inputs:

          - prompt(''): a string to be printed to prompt the user.

          - continue_prompt(False): whether this line is the first one or a
          continuation in a sequence of inputs.
        """
        # Code run by the user may have modified the readline completer state.
        # We must ensure that our completer is back in place.

        if self.has_readline:
            self.set_readline_completer()

        # raw_input expects str, but we pass it unicode sometimes
        prompt = py3compat.cast_bytes_py2(prompt)

        try:
            line = py3compat.str_to_unicode(self.raw_input_original(prompt))
        except ValueError:
            warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
                 " or sys.stdout.close()!\nExiting IPython!\n")
            self.ask_exit()
            return ""

        # Try to be reasonably smart about not re-indenting pasted input more
        # than necessary.  We do this by trimming out the auto-indent initial
        # spaces, if the user's actual input started itself with whitespace.
        if self.autoindent:
            if num_ini_spaces(line) > self.indent_current_nsp:
                line = line[self.indent_current_nsp:]
                self.indent_current_nsp = 0

        return line


def edit_syntax_error(self):
        """The bottom half of the syntax error handler called in the main loop.

        Loop until syntax error is fixed or user cancels.
        """

        while self.SyntaxTB.last_syntax_error:
            # copy and clear last_syntax_error
            err = self.SyntaxTB.clear_err_state()
            if not self._should_recompile(err):
                return
            try:
                # may set last_syntax_error again if a SyntaxError is raised
                self.safe_execfile(err.filename,self.user_ns)
            except:
                self.showtraceback()
            else:
                try:
                    f = open(err.filename)
                    try:
                        # This should be inside a display_trap block and I
                        # think it is.
                        sys.displayhook(f.read())
                    finally:
                        f.close()
                except:
                    self.showtraceback()

def edit_syntax_error ( self ) : while self . SyntaxTB . last_syntax_error : err = self . SyntaxTB . clear_err_state ( ) if not self . _should_recompile ( err ) : return try : self . safe_execfile ( err . filename , self . user_ns ) except : self . showtraceback ( ) else : try : f = open ( err . filename ) try : sys . displayhook ( f . read ( ) ) finally : f . close ( ) except : self . showtraceback ( )

The bottom half of the syntax error handler called in the main loop .


def _should_recompile(self,e):
        """Utility routine for edit_syntax_error"""

        if e.filename in ('<ipython console>','<input>','<string>',
                          '<console>','<BackgroundJob compilation>',
                          None):

            return False
        try:
            if (self.autoedit_syntax and
                not self.ask_yes_no('Return to editor to correct syntax error? '
                              '[Y/n] ','y')):
                return False
        except EOFError:
            return False

        def int0(x):
            try:
                return int(x)
            except TypeError:
                return 0
        # always pass integer line and offset values to editor hook
        try:
            self.hooks.fix_error_editor(e.filename,
                int0(e.lineno),int0(e.offset),e.msg)
        except TryNext:
            warn('Could not open editor')
            return False
        return True


def exit(self):
        """Handle interactive exit.

        This method calls the ask_exit callback."""
        if self.confirm_exit:
            if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
                self.ask_exit()
        else:
            self.ask_exit()

def
exit
(
self
)
:
if
self
.
confirm_exit
:
if
self
.
ask_yes_no
(
'Do you really want to exit ([y]/n)?'
,
'y'
)
:
self
.
ask_exit
(
)
else
:
self
.
ask_exit
(
)


def get_url_rev(self):
        """
        Returns the correct repository URL and revision by parsing the given
        repository URL
        """
        error_message= (
           "Sorry, '%s' is a malformed VCS url. "
           "Ihe format is <vcs>+<protocol>://<url>, "
           "e.g. svn+http://myrepo/svn/MyApp#egg=MyApp")
        assert '+' in self.url, error_message % self.url
        url = self.url.split('+', 1)[1]
        scheme, netloc, path, query, frag = urlparse.urlsplit(url)
        rev = None
        if '@' in path:
            path, rev = path.rsplit('@', 1)
        url = urlparse.urlunsplit((scheme, netloc, path, query, ''))
        return url, rev


def new_frontend_master(self):
        """ Create and return new frontend attached to new kernel, launched on localhost.
        """
        ip = self.ip if self.ip in LOCAL_IPS else LOCALHOST
        kernel_manager = self.kernel_manager_class(
                                ip=ip,
                                connection_file=self._new_connection_file(),
                                config=self.config,
        )
        # start the kernel
        kwargs = dict()
        kwargs['extra_arguments'] = self.kernel_argv
        kernel_manager.start_kernel(**kwargs)
        kernel_manager.start_channels()
        widget = self.widget_factory(config=self.config,
                                   local_kernel=True)
        self.init_colors(widget)
        widget.kernel_manager = kernel_manager
        widget._existing = False
        widget._may_close = True
        widget._confirm_exit = self.confirm_exit
        return widget

def new_frontend_master ( self ) : ip = self . ip if self . ip in LOCAL_IPS else LOCALHOST kernel_manager = self . kernel_manager_class ( ip = ip , connection_file = self . _new_connection_file ( ) , config = self . config , ) kwargs = dict ( ) kwargs [ 'extra_arguments' ] = self . kernel_argv kernel_manager . start_kernel ( ** kwargs ) kernel_manager . start_channels ( ) widget = self . widget_factory ( config = self . config , local_kernel = True ) self . init_colors ( widget ) widget . kernel_manager = kernel_manager widget . _existing = False widget . _may_close = True widget . _confirm_exit = self . confirm_exit return widget

Create and return new frontend attached to new kernel launched on localhost .


def init_colors(self, widget):
        """Configure the coloring of the widget"""
        # Note: This will be dramatically simplified when colors
        # are removed from the backend.

        # parse the colors arg down to current known labels
        try:
            colors = self.config.ZMQInteractiveShell.colors
        except AttributeError:
            colors = None
        try:
            style = self.config.IPythonWidget.syntax_style
        except AttributeError:
            style = None
        try:
            sheet = self.config.IPythonWidget.style_sheet
        except AttributeError:
            sheet = None

        # find the value for colors:
        if colors:
            colors=colors.lower()
            if colors in ('lightbg', 'light'):
                colors='lightbg'
            elif colors in ('dark', 'linux'):
                colors='linux'
            else:
                colors='nocolor'
        elif style:
            if style=='bw':
                colors='nocolor'
            elif styles.dark_style(style):
                colors='linux'
            else:
                colors='lightbg'
        else:
            colors=None

        # Configure the style
        if style:
            widget.style_sheet = styles.sheet_from_template(style, colors)
            widget.syntax_style = style
            widget._syntax_style_changed()
            widget._style_sheet_changed()
        elif colors:
            # use a default dark/light/bw style
            widget.set_default_style(colors=colors)

        if self.stylesheet:
            # we got an explicit stylesheet
            if os.path.isfile(self.stylesheet):
                with open(self.stylesheet) as f:
                    sheet = f.read()
            else:
                raise IOError("Stylesheet %r not found." % self.stylesheet)
        if sheet:
            widget.style_sheet = sheet
            widget._style_sheet_changed()

def
init_colors
(
self
,
widget
)
:
try
:
colors
=
self
.
config
.
ZMQInteractiveShell
.
colors
except
AttributeError
:
colors
=
try
:
style
=
self
.
config
.
IPythonWidget
.
syntax_style
except
AttributeError
:
style
=
try
:
sheet
=
self
.
config
.
IPythonWidget
.
style_sheet
except
AttributeError
:
sheet
=
if
colors
:
colors
=
colors
.
lower
(
)
if
colors
in
(
'lightbg'
,
'light'
)
:
colors
=
'lightbg'
elif
colors
in
(
'dark'
,
'linux'
)
:
colors
=
'linux'
else
:
colors
=
'nocolor'
elif
style
:
if
style
==
'bw'
:
colors
=
'nocolor'
elif
styles
.
dark_style
(
style
)
:
colors
=
'linux'
else
:
colors
=
'lightbg'
else
:
colors
=
if
style
:
widget
.
style_sheet
=
styles
.
sheet_from_template
(
style
,
colors
)
widget
.
syntax_style
=
style
widget
.
_syntax_style_changed
(
)
widget
.
_style_sheet_changed
(
)
elif
colors
:
widget
.
set_default_style
(
colors
=
colors
)
if
self
.
stylesheet
:
if
os
.
path
.
isfile
(
self
.
stylesheet
)
:
with
open
(
self
.
stylesheet
)
as
f
:
sheet
=
f
.
read
(
)
else
:
raise
IOError
(
"Stylesheet %r not found."
%
self
.
stylesheet
)
if
sheet
:
widget
.
style_sheet
=
sheet
widget
.
_style_sheet_changed
(
)


def info(self):
        """return the connection info for this object's sockets."""
        return (self.identity, self.url, self.pub_url, self.location)

def info ( self ) : return ( self . identity , self . url , self . pub_url , self . location )

return the connection info for this object s sockets .


def Rconverter(Robj, dataframe=False):
    """
    Convert an object in R's namespace to one suitable
    for ipython's namespace.

    For a data.frame, it tries to return a structured array.
    It first checks for colnames, then names.
    If all are NULL, it returns np.asarray(Robj), else
    it tries to construct a recarray

    Parameters
    ----------

    Robj: an R object returned from rpy2
    """
    is_data_frame = ro.r('is.data.frame')
    colnames = ro.r('colnames')
    rownames = ro.r('rownames') # with pandas, these could be used for the index
    names = ro.r('names')

    if dataframe:
        as_data_frame = ro.r('as.data.frame')
        cols = colnames(Robj)
        _names = names(Robj)
        if cols != ri.NULL:
            Robj = as_data_frame(Robj)
            names = tuple(np.array(cols))
        elif _names != ri.NULL:
            names = tuple(np.array(_names))
        else: # failed to find names
            return np.asarray(Robj)
        Robj = np.rec.fromarrays(Robj, names = names)
    return np.asarray(Robj)

def Rconverter ( Robj , dataframe = False ) : is_data_frame = ro . r ( 'is.data.frame' ) colnames = ro . r ( 'colnames' ) rownames = ro . r ( 'rownames' ) names = ro . r ( 'names' ) if dataframe : as_data_frame = ro . r ( 'as.data.frame' ) cols = colnames ( Robj ) _names = names ( Robj ) if cols != ri . NULL : Robj = as_data_frame ( Robj ) names = tuple ( np . array ( cols ) ) elif _names != ri . NULL : names = tuple ( np . array ( _names ) ) else : return np . asarray ( Robj ) Robj = np . rec . fromarrays ( Robj , names = names ) return np . asarray ( Robj )

Convert an object in R s namespace to one suitable for ipython s namespace .


def findsource(object):
    """Return the entire source file and starting line number for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a list of all the lines
    in the file and the line number indexes a line in that list.  An IOError
    is raised if the source code cannot be retrieved.

    FIXED version with which we monkeypatch the stdlib to work around a bug."""

    file = getsourcefile(object) or getfile(object)
    # If the object is a frame, then trying to get the globals dict from its
    # module won't work. Instead, the frame object itself has the globals
    # dictionary.
    globals_dict = None
    if inspect.isframe(object):
        # XXX: can this ever be false?
        globals_dict = object.f_globals
    else:
        module = getmodule(object, file)
        if module:
            globals_dict = module.__dict__
    lines = linecache.getlines(file, globals_dict)
    if not lines:
        raise IOError('could not get source code')

    if ismodule(object):
        return lines, 0

    if isclass(object):
        name = object.__name__
        pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
        # make some effort to find the best matching class definition:
        # use the one with the least indentation, which is the one
        # that's most probably not inside a function definition.
        candidates = []
        for i in range(len(lines)):
            match = pat.match(lines[i])
            if match:
                # if it's at toplevel, it's already the best one
                if lines[i][0] == 'c':
                    return lines, i
                # else add whitespace to candidate list
                candidates.append((match.group(1), i))
        if candidates:
            # this will sort by whitespace, and by line number,
            # less whitespace first
            candidates.sort()
            return lines, candidates[0][1]
        else:
            raise IOError('could not find class definition')

    if ismethod(object):
        object = object.im_func
    if isfunction(object):
        object = object.func_code
    if istraceback(object):
        object = object.tb_frame
    if isframe(object):
        object = object.f_code
    if iscode(object):
        if not hasattr(object, 'co_firstlineno'):
            raise IOError('could not find function definition')
        pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
        pmatch = pat.match
        # fperez - fix: sometimes, co_firstlineno can give a number larger than
        # the length of lines, which causes an error.  Safeguard against that.
        lnum = min(object.co_firstlineno,len(lines))-1
        while lnum > 0:
            if pmatch(lines[lnum]): break
            lnum -= 1

        return lines, lnum
    raise IOError('could not find code object')

Return the entire source file and starting line number for an object .


def set_colors(self,*args,**kw):
        """Shorthand access to the color table scheme selector method."""

        # Set own color table
        self.color_scheme_table.set_active_scheme(*args,**kw)
        # for convenience, set Colors to the active scheme
        self.Colors = self.color_scheme_table.active_colors
        # Also set colors of debugger
        if hasattr(self,'pdb') and self.pdb is not None:
            self.pdb.set_colors(*args,**kw)

def set_colors ( self , * args , ** kw ) : self . color_scheme_table . set_active_scheme ( * args , ** kw ) self . Colors = self . color_scheme_table . active_colors if hasattr ( self , 'pdb' ) and self . pdb is not None : self . pdb . set_colors ( * args , ** kw )

Shorthand access to the color table scheme selector method .


def color_toggle(self):
        """Toggle between the currently active color scheme and NoColor."""

        if self.color_scheme_table.active_scheme_name == 'NoColor':
            self.color_scheme_table.set_active_scheme(self.old_scheme)
            self.Colors = self.color_scheme_table.active_colors
        else:
            self.old_scheme = self.color_scheme_table.active_scheme_name
            self.color_scheme_table.set_active_scheme('NoColor')
            self.Colors = self.color_scheme_table.active_colors

def color_toggle ( self ) : if self . color_scheme_table . active_scheme_name == 'NoColor' : self . color_scheme_table . set_active_scheme ( self . old_scheme ) self . Colors = self . color_scheme_table . active_colors else : self . old_scheme = self . color_scheme_table . active_scheme_name self . color_scheme_table . set_active_scheme ( 'NoColor' ) self . Colors = self . color_scheme_table . active_colors

Toggle between the currently active color scheme and NoColor .


def text(self, etype, value, tb, tb_offset=None, context=5):
        """Return formatted traceback.

        Subclasses may override this if they add extra arguments.
        """
        tb_list = self.structured_traceback(etype, value, tb,
                                            tb_offset, context)
        return self.stb2text(tb_list)

def text ( self , etype , value , tb , tb_offset = None , context = 5 ) : tb_list = self . structured_traceback ( etype , value , tb , tb_offset , context ) return self . stb2text ( tb_list )


def structured_traceback(self, etype, value, elist, tb_offset=None,
                             context=5):
        """Return a color formatted string with the traceback info.

        Parameters
        ----------
        etype : exception type
          Type of the exception raised.

        value : object
          Data stored in the exception

        elist : list
          List of frames, see class docstring for details.

        tb_offset : int, optional
          Number of frames in the traceback to skip.  If not given, the
          instance value is used (set in constructor).

        context : int, optional
          Number of lines of context information to print.

        Returns
        -------
        String with formatted exception.
        """
        tb_offset = self.tb_offset if tb_offset is None else tb_offset
        Colors = self.Colors
        out_list = []
        if elist:

            if tb_offset and len(elist) > tb_offset:
                elist = elist[tb_offset:]

            out_list.append('Traceback %s(most recent call last)%s:' %
                                (Colors.normalEm, Colors.Normal) + '\n')
            out_list.extend(self._format_list(elist))
        # The exception info should be a single entry in the list.
        lines = ''.join(self._format_exception_only(etype, value))
        out_list.append(lines)

        # Note: this code originally read:

        ## for line in lines[:-1]:
        ##     out_list.append(" "+line)
        ## out_list.append(lines[-1])

        # This means it was indenting everything but the last line by a little
        # bit.  I've disabled this for now, but if we see ugliness somewhre we
        # can restore it.

        return out_list

Return a color formatted string with the traceback info .


def _format_list(self, extracted_list):
        """Format a list of traceback entry tuples for printing.

        Given a list of tuples as returned by extract_tb() or
        extract_stack(), return a list of strings ready for printing.
        Each string in the resulting list corresponds to the item with the
        same index in the argument list.  Each string ends in a newline;
        the strings may contain internal newlines as well, for those items
        whose source text line is not None.

        Lifted almost verbatim from traceback.py
        """

        Colors = self.Colors
        list = []
        for filename, lineno, name, line in extracted_list[:-1]:
            item = '  File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
                    (Colors.filename, filename, Colors.Normal,
                     Colors.lineno, lineno, Colors.Normal,
                     Colors.name, name, Colors.Normal)
            if line:
                item += '    %s\n' % line.strip()
            list.append(item)
        # Emphasize the last entry
        filename, lineno, name, line = extracted_list[-1]
        item = '%s  File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
                (Colors.normalEm,
                 Colors.filenameEm, filename, Colors.normalEm,
                 Colors.linenoEm, lineno, Colors.normalEm,
                 Colors.nameEm, name, Colors.normalEm,
                 Colors.Normal)
        if line:
            item += '%s    %s%s\n' % (Colors.line, line.strip(),
                                            Colors.Normal)
        list.append(item)
        #from pprint import pformat; print 'LISTTB', pformat(list) # dbg
        return list

Format a list of traceback entry tuples for printing .


def _format_exception_only(self, etype, value):
        """Format the exception part of a traceback.

        The arguments are the exception type and value such as given by
        sys.exc_info()[:2]. The return value is a list of strings, each ending
        in a newline.  Normally, the list contains a single string; however,
        for SyntaxError exceptions, it contains several lines that (when
        printed) display detailed information about where the syntax error
        occurred.  The message indicating which exception occurred is the
        always last string in the list.

        Also lifted nearly verbatim from traceback.py
        """

        have_filedata = False
        Colors = self.Colors
        list = []
        stype = Colors.excName + etype.__name__ + Colors.Normal
        if value is None:
            # Not sure if this can still happen in Python 2.6 and above
            list.append( str(stype) + '\n')
        else:
            if etype is SyntaxError:
                have_filedata = True
                #print 'filename is',filename  # dbg
                if not value.filename: value.filename = "<string>"
                list.append('%s  File %s"%s"%s, line %s%d%s\n' % \
                        (Colors.normalEm,
                         Colors.filenameEm, value.filename, Colors.normalEm,
                         Colors.linenoEm, value.lineno, Colors.Normal  ))
                if value.text is not None:
                    i = 0
                    while i < len(value.text) and value.text[i].isspace():
                        i += 1
                    list.append('%s    %s%s\n' % (Colors.line,
                                                  value.text.strip(),
                                                  Colors.Normal))
                    if value.offset is not None:
                        s = '    '
                        for c in value.text[i:value.offset-1]:
                            if c.isspace():
                                s += c
                            else:
                                s += ' '
                        list.append('%s%s^%s\n' % (Colors.caret, s,
                                                   Colors.Normal) )

            try:
                s = value.msg
            except Exception:
                s = self._some_str(value)
            if s:
                list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
                                              Colors.Normal, s))
            else:
                list.append('%s\n' % str(stype))

        # sync with user hooks
        if have_filedata:
            ipinst = ipapi.get()
            if ipinst is not None:
                ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)

        return list

def
_format_exception_only
(
self
,
etype
,
value
)
:
have_filedata
=
False
Colors
=
self
.
Colors
list
=
[
]
stype
=
Colors
.
excName
+
etype
.
__name__
+
Colors
.
Normal
if
value
is
:
list
.
append
(
str
(
stype
)
+
'\n'
)
else
:
if
etype
is
SyntaxError
:
have_filedata
=
True
if
not
value
.
filename
:
value
.
filename
=
"<string>"
list
.
append
(
'%s  File %s"%s"%s, line %s%d%s\n'
%
(
Colors
.
normalEm
,
Colors
.
filenameEm
,
value
.
filename
,
Colors
.
normalEm
,
Colors
.
linenoEm
,
value
.
lineno
,
Colors
.
Normal
)
)
if
value
.
text
is
not
:
i
=
0
while
i
<
len
(
value
.
text
)
and
value
.
text
[
i
]
.
isspace
(
)
:
i
+=
1
list
.
append
(
'%s    %s%s\n'
%
(
Colors
.
line
,
value
.
text
.
strip
(
)
,
Colors
.
Normal
)
)
if
value
.
offset
is
not
:
s
=
'    '
for
c
in
value
.
text
[
i
:
value
.
offset
1
]
:
if
c
.
isspace
(
)
:
s
+=
c
else
:
s
+=
' '
list
.
append
(
'%s%s^%s\n'
%
(
Colors
.
caret
,
s
,
Colors
.
Normal
)
)
try
:
s
=
value
.
msg
except
Exception
:
s
=
self
.
_some_str
(
value
)
if
s
:
list
.
append
(
'%s%s:%s %s\n'
%
(
str
(
stype
)
,
Colors
.
excName
,
Colors
.
Normal
,
s
)
)
else
:
list
.
append
(
'%s\n'
%
str
(
stype
)
)
if
have_filedata
:
ipinst
=
ipapi
.
get
(
)
if
ipinst
is
not
:
ipinst
.
hooks
.
synchronize_with_editor
(
value
.
filename
,
value
.
lineno
,
0
)
return
list

Format the exception part of a traceback .


def show_exception_only(self, etype, evalue):
        """Only print the exception type and message, without a traceback.

        Parameters
        ----------
        etype : exception type
        value : exception value
        """
        # This method needs to use __call__ from *this* class, not the one from
        # a subclass whose signature or behavior may be different
        ostream = self.ostream
        ostream.flush()
        ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
        ostream.flush()

def show_exception_only ( self , etype , evalue ) : ostream = self . ostream ostream . flush ( ) ostream . write ( '\n' . join ( self . get_exception_only ( etype , evalue ) ) ) ostream . flush ( )

Only print the exception type and message without a traceback .


def debugger(self,force=False):
        """Call up the pdb debugger if desired, always clean up the tb
        reference.

        Keywords:

          - force(False): by default, this routine checks the instance call_pdb
          flag and does not actually invoke the debugger if the flag is false.
          The 'force' option forces the debugger to activate even if the flag
          is false.

        If the call_pdb flag is set, the pdb interactive debugger is
        invoked. In all cases, the self.tb reference to the current traceback
        is deleted to prevent lingering references which hamper memory
        management.

        Note that each call to pdb() does an 'import readline', so if your app
        requires a special setup for the readline completers, you'll have to
        fix that by hand after invoking the exception handler."""

        if force or self.call_pdb:
            if self.pdb is None:
                self.pdb = debugger.Pdb(
                    self.color_scheme_table.active_scheme_name)
            # the system displayhook may have changed, restore the original
            # for pdb
            display_trap = DisplayTrap(hook=sys.__displayhook__)
            with display_trap:
                self.pdb.reset()
                # Find the right frame so we don't pop up inside ipython itself
                if hasattr(self,'tb') and self.tb is not None:
                    etb = self.tb
                else:
                    etb = self.tb = sys.last_traceback
                while self.tb is not None and self.tb.tb_next is not None:
                    self.tb = self.tb.tb_next
                if etb and etb.tb_next:
                    etb = etb.tb_next
                self.pdb.botframe = etb.tb_frame
                self.pdb.interaction(self.tb.tb_frame, self.tb)

        if hasattr(self,'tb'):
            del self.tb

def debugger ( self , force = False ) : if force or self . call_pdb : if self . pdb is None : self . pdb = debugger . Pdb ( self . color_scheme_table . active_scheme_name ) display_trap = DisplayTrap ( hook = sys . __displayhook__ ) with display_trap : self . pdb . reset ( ) if hasattr ( self , 'tb' ) and self . tb is not None : etb = self . tb else : etb = self . tb = sys . last_traceback while self . tb is not None and self . tb . tb_next is not None : self . tb = self . tb . tb_next if etb and etb . tb_next : etb = etb . tb_next self . pdb . botframe = etb . tb_frame self . pdb . interaction ( self . tb . tb_frame , self . tb ) if hasattr ( self , 'tb' ) : del self . tb

Call up the pdb debugger if desired always clean up the tb reference .


def set_mode(self,mode=None):
        """Switch to the desired mode.

        If mode is not specified, cycles through the available modes."""

        if not mode:
            new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \
                      len(self.valid_modes)
            self.mode = self.valid_modes[new_idx]
        elif mode not in self.valid_modes:
            raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\
                  'Valid modes: '+str(self.valid_modes)
        else:
            self.mode = mode
        # include variable details only in 'Verbose' mode
        self.include_vars = (self.mode == self.valid_modes[2])
        # Set the join character for generating text tracebacks
        self.tb_join_char = self._join_chars[self.mode]

def
set_mode
(
self
,
mode
=
)
:
if
not
mode
:
new_idx
=
(
self
.
valid_modes
.
index
(
self
.
mode
)
+
1
)
%
len
(
self
.
valid_modes
)
self
.
mode
=
self
.
valid_modes
[
new_idx
]
elif
mode
not
in
self
.
valid_modes
:
raise
ValueError
,
'Unrecognized mode in FormattedTB: <'
+
mode
+
'>\n'
'Valid modes: '
+
str
(
self
.
valid_modes
)
else
:
self
.
mode
=
mode
self
.
include_vars
=
(
self
.
mode
==
self
.
valid_modes
[
2
]
)
self
.
tb_join_char
=
self
.
_join_chars
[
self
.
mode
]


def group_required(group,
                   login_url=None,
                   redirect_field_name=REDIRECT_FIELD_NAME,
                   skip_superuser=True):
    """
    View decorator for requiring a user group.
    """
    def decorator(view_func):
        @login_required(redirect_field_name=redirect_field_name,
                        login_url=login_url)
        def _wrapped_view(request, *args, **kwargs):

            if not (request.user.is_superuser and skip_superuser):
                if request.user.groups.filter(name=group).count() == 0:
                    raise PermissionDenied

            return view_func(request, *args, **kwargs)
        return _wrapped_view

    return decorator

def group_required ( group , login_url = None , redirect_field_name = REDIRECT_FIELD_NAME , skip_superuser = True ) : def decorator ( view_func ) : @ login_required ( redirect_field_name = redirect_field_name , login_url = login_url ) def _wrapped_view ( request , * args , ** kwargs ) : if not ( request . user . is_superuser and skip_superuser ) : if request . user . groups . filter ( name = group ) . count ( ) == 0 : raise PermissionDenied return view_func ( request , * args , ** kwargs ) return _wrapped_view return decorator

View decorator for requiring a user group .


def ensure_fromlist(mod, fromlist, buf, recursive):
    """Handle 'from module import a, b, c' imports."""
    if not hasattr(mod, '__path__'):
        return
    for item in fromlist:
        if not hasattr(item, 'rindex'):
            raise TypeError("Item in ``from list'' not a string")
        if item == '*':
            if recursive:
                continue # avoid endless recursion
            try:
                all = mod.__all__
            except AttributeError:
                pass
            else:
                ret = ensure_fromlist(mod, all, buf, 1)
                if not ret:
                    return 0
        elif not hasattr(mod, item):
            import_submodule(mod, item, buf + '.' + item)

def
ensure_fromlist
(
mod
,
fromlist
,
buf
,
recursive
)
:
if
not
hasattr
(
mod
,
'__path__'
)
:
return
for
item
in
fromlist
:
if
not
hasattr
(
item
,
'rindex'
)
:
raise
TypeError
(
"Item in ``from list'' not a string"
)
if
item
==
'*'
:
if
recursive
:
continue
try
:
all
=
mod
.
__all__
except
AttributeError
:
pass
else
:
ret
=
ensure_fromlist
(
mod
,
all
,
buf
,
1
)
if
not
ret
:
return
0
elif
not
hasattr
(
mod
,
item
)
:
import_submodule
(
mod
,
item
,
buf
+
'.'
+
item
)

Handle from module import a b c imports .


def add_line(self, line):
        """Add a line of source to the code.

        Don't include indentations or newlines.

        """
        self.code.append(" " * self.indent_amount)
        self.code.append(line)
        self.code.append("\n")

def
add_line
(
self
,
line
)
:
self
.
code
.
append
(
" "
*
self
.
indent_amount
)
self
.
code
.
append
(
line
)
self
.
code
.
append
(
"\n"
)


def add_section(self):
        """Add a section, a sub-CodeBuilder."""
        sect = CodeBuilder(self.indent_amount)
        self.code.append(sect)
        return sect

def add_section ( self ) : sect = CodeBuilder ( self . indent_amount ) self . code . append ( sect ) return sect


def get_function(self, fn_name):
        """Compile the code, and return the function `fn_name`."""
        assert self.indent_amount == 0
        g = {}
        code_text = str(self)
        exec(code_text, g)
        return g[fn_name]

def get_function ( self , fn_name ) : assert self . indent_amount == 0 g = { } code_text = str ( self ) exec ( code_text , g ) return g [ fn_name ]

Compile the code and return the function fn_name .


def expr_code(self, expr):
        """Generate a Python expression for `expr`."""
        if "|" in expr:
            pipes = expr.split("|")
            code = self.expr_code(pipes[0])
            for func in pipes[1:]:
                self.all_vars.add(func)
                code = "c_%s(%s)" % (func, code)
        elif "." in expr:
            dots = expr.split(".")
            code = self.expr_code(dots[0])
            args = [repr(d) for d in dots[1:]]
            code = "dot(%s, %s)" % (code, ", ".join(args))
        else:
            self.all_vars.add(expr)
            code = "c_%s" % expr
        return code

def
expr_code
(
self
,
expr
)
:
if
"|"
in
expr
:
pipes
=
expr
.
split
(
"|"
)
code
=
self
.
expr_code
(
pipes
[
0
]
)
for
func
in
pipes
[
1
:
]
:
self
.
all_vars
.
add
(
func
)
code
=
"c_%s(%s)"
%
(
func
,
code
)
elif
"."
in
expr
:
dots
=
expr
.
split
(
"."
)
code
=
self
.
expr_code
(
dots
[
0
]
)
args
=
[
repr
(
d
)
for
d
in
dots
[
1
:
]
]
code
=
"dot(%s, %s)"
%
(
code
,
", "
.
join
(
args
)
)
else
:
self
.
all_vars
.
add
(
expr
)
code
=
"c_%s"
%
expr
return
code


def render(self, context=None):
        """Render this template by applying it to `context`.

        `context` is a dictionary of values to use in this rendering.

        """
        # Make the complete context we'll use.
        ctx = dict(self.context)
        if context:
            ctx.update(context)
        return self.render_function(ctx, self.do_dots)

def render ( self , context = None ) : ctx = dict ( self . context ) if context : ctx . update ( context ) return self . render_function ( ctx , self . do_dots )

Render this template by applying it to context .


def do_dots(self, value, *dots):
        """Evaluate dotted expressions at runtime."""
        for dot in dots:
            try:
                value = getattr(value, dot)
            except AttributeError:
                value = value[dot]
            if hasattr(value, '__call__'):
                value = value()
        return value

def do_dots ( self , value , * dots ) : for dot in dots : try : value = getattr ( value , dot ) except AttributeError : value = value [ dot ] if hasattr ( value , '__call__' ) : value = value ( ) return value

Evaluate dotted expressions at runtime .


def render_template(tpl, context):
    '''
    A shortcut function to render a partial template with context and return
    the output.
    '''

    templates = [tpl] if type(tpl) != list else tpl
    tpl_instance = None

    for tpl in templates:
        try:
            tpl_instance = template.loader.get_template(tpl)
            break
        except template.TemplateDoesNotExist:
            pass

    if not tpl_instance:
        raise Exception('Template does not exist: ' + templates[-1])

    return tpl_instance.render(template.Context(context))

def
render_template
(
tpl
,
context
)
:
templates
=
[
tpl
]
if
type
(
tpl
)
!=
list
else
tpl
tpl_instance
=
for
tpl
in
templates
:
try
:
tpl_instance
=
template
.
loader
.
get_template
(
tpl
)
break
except
template
.
TemplateDoesNotExist
:
pass
if
not
tpl_instance
:
raise
Exception
(
'Template does not exist: '
+
templates
[
1
]
)
return
tpl_instance
.
render
(
template
.
Context
(
context
)
)

A shortcut function to render a partial template with context and return the output .


def _formatters_default(self):
        """Activate the default formatters."""
        formatter_classes = [
            PlainTextFormatter,
            HTMLFormatter,
            SVGFormatter,
            PNGFormatter,
            JPEGFormatter,
            LatexFormatter,
            JSONFormatter,
            JavascriptFormatter
        ]
        d = {}
        for cls in formatter_classes:
            f = cls(config=self.config)
            d[f.format_type] = f
        return d

def _formatters_default ( self ) : formatter_classes = [ PlainTextFormatter , HTMLFormatter , SVGFormatter , PNGFormatter , JPEGFormatter , LatexFormatter , JSONFormatter , JavascriptFormatter ] d = { } for cls in formatter_classes : f = cls ( config = self . config ) d [ f . format_type ] = f return d


def for_type(self, typ, func):
        """Add a format function for a given type.

        Parameters
        -----------
        typ : class
            The class of the object that will be formatted using `func`.
        func : callable
            The callable that will be called to compute the format data. The
            call signature of this function is simple, it must take the
            object to be formatted and return the raw data for the given
            format. Subclasses may use a different call signature for the
            `func` argument.
        """
        oldfunc = self.type_printers.get(typ, None)
        if func is not None:
            # To support easy restoration of old printers, we need to ignore
            # Nones.
            self.type_printers[typ] = func
        return oldfunc

def for_type ( self , typ , func ) : oldfunc = self . type_printers . get ( typ , None ) if func is not None : self . type_printers [ typ ] = func return oldfunc

Add a format function for a given type .


def for_type_by_name(self, type_module, type_name, func):
        """Add a format function for a type specified by the full dotted
        module and name of the type, rather than the type of the object.

        Parameters
        ----------
        type_module : str
            The full dotted name of the module the type is defined in, like
            ``numpy``.
        type_name : str
            The name of the type (the class name), like ``dtype``
        func : callable
            The callable that will be called to compute the format data. The
            call signature of this function is simple, it must take the
            object to be formatted and return the raw data for the given
            format. Subclasses may use a different call signature for the
            `func` argument.
        """
        key = (type_module, type_name)
        oldfunc = self.deferred_printers.get(key, None)
        if func is not None:
            # To support easy restoration of old printers, we need to ignore
            # Nones.
            self.deferred_printers[key] = func
        return oldfunc

def for_type_by_name ( self , type_module , type_name , func ) : key = ( type_module , type_name ) oldfunc = self . deferred_printers . get ( key , None ) if func is not None : self . deferred_printers [ key ] = func return oldfunc

Add a format function for a type specified by the full dotted module and name of the type rather than the type of the object .


def _float_precision_changed(self, name, old, new):
        """float_precision changed, set float_format accordingly.

        float_precision can be set by int or str.
        This will set float_format, after interpreting input.
        If numpy has been imported, numpy print precision will also be set.

        integer `n` sets format to '%.nf', otherwise, format set directly.

        An empty string returns to defaults (repr for float, 8 for numpy).

        This parameter can be set via the '%precision' magic.
        """

        if '%' in new:
            # got explicit format string
            fmt = new
            try:
                fmt%3.14159
            except Exception:
                raise ValueError("Precision must be int or format string, not %r"%new)
        elif new:
            # otherwise, should be an int
            try:
                i = int(new)
                assert i >= 0
            except ValueError:
                raise ValueError("Precision must be int or format string, not %r"%new)
            except AssertionError:
                raise ValueError("int precision must be non-negative, not %r"%i)

            fmt = '%%.%if'%i
            if 'numpy' in sys.modules:
                # set numpy precision if it has been imported
                import numpy
                numpy.set_printoptions(precision=i)
        else:
            # default back to repr
            fmt = '%r'
            if 'numpy' in sys.modules:
                import numpy
                # numpy default is 8
                numpy.set_printoptions(precision=8)
        self.float_format = fmt

float_precision changed set float_format accordingly .


def user_config_files():
    """Return path to any existing user config files
    """
    return filter(os.path.exists,
                  map(os.path.expanduser, config_files))

def user_config_files ( ) : return filter ( os . path . exists , map ( os . path . expanduser , config_files ) )


def configure(self, argv=None, doc=None):
        """Configure the nose running environment. Execute configure before
        collecting tests with nose.TestCollector to enable output capture and
        other features.
        """
        env = self.env
        if argv is None:
            argv = sys.argv

        cfg_files = getattr(self, 'files', [])
        options, args = self._parseArgs(argv, cfg_files)
        # If -c --config has been specified on command line,
        # load those config files and reparse
        if getattr(options, 'files', []):
            options, args = self._parseArgs(argv, options.files)

        self.options = options
        if args:
            self.testNames = args
        if options.testNames is not None:
            self.testNames.extend(tolist(options.testNames))

        if options.py3where is not None:
            if sys.version_info >= (3,):
                options.where = options.py3where

        # `where` is an append action, so it can't have a default value
        # in the parser, or that default will always be in the list
        if not options.where:
            options.where = env.get('NOSE_WHERE', None)

        # include and exclude also
        if not options.ignoreFiles:
            options.ignoreFiles = env.get('NOSE_IGNORE_FILES', [])
        if not options.include:
            options.include = env.get('NOSE_INCLUDE', [])
        if not options.exclude:
            options.exclude = env.get('NOSE_EXCLUDE', [])

        self.addPaths = options.addPaths
        self.stopOnError = options.stopOnError
        self.verbosity = options.verbosity
        self.includeExe = options.includeExe
        self.traverseNamespace = options.traverseNamespace
        self.debug = options.debug
        self.debugLog = options.debugLog
        self.loggingConfig = options.loggingConfig
        self.firstPackageWins = options.firstPackageWins
        self.configureLogging()

        if options.where is not None:
            self.configureWhere(options.where)

        if options.testMatch:
            self.testMatch = re.compile(options.testMatch)

        if options.ignoreFiles:
            self.ignoreFiles = map(re.compile, tolist(options.ignoreFiles))
            log.info("Ignoring files matching %s", options.ignoreFiles)
        else:
            log.info("Ignoring files matching %s", self.ignoreFilesDefaultStrings)

        if options.include:
            self.include = map(re.compile, tolist(options.include))
            log.info("Including tests matching %s", options.include)

        if options.exclude:
            self.exclude = map(re.compile, tolist(options.exclude))
            log.info("Excluding tests matching %s", options.exclude)

        # When listing plugins we don't want to run them
        if not options.showPlugins:
            self.plugins.configure(options, self)
            self.plugins.begin()

def
configure
(
self
,
argv
=
,
doc
=
)
:
env
=
self
.
env
if
argv
is
:
argv
=
sys
.
argv
cfg_files
=
getattr
(
self
,
'files'
,
[
]
)
options
,
args
=
self
.
_parseArgs
(
argv
,
cfg_files
)
if
getattr
(
options
,
'files'
,
[
]
)
:
options
,
args
=
self
.
_parseArgs
(
argv
,
options
.
files
)
self
.
options
=
options
if
args
:
self
.
testNames
=
args
if
options
.
testNames
is
not
:
self
.
testNames
.
extend
(
tolist
(
options
.
testNames
)
)
if
options
.
py3where
is
not
:
if
sys
.
version_info
>=
(
3
,
)
:
options
.
where
=
options
.
py3where
if
not
options
.
where
:
options
.
where
=
env
.
get
(
'NOSE_WHERE'
,
)
if
not
options
.
ignoreFiles
:
options
.
ignoreFiles
=
env
.
get
(
'NOSE_IGNORE_FILES'
,
[
]
)
if
not
options
.
include
:
options
.
include
=
env
.
get
(
'NOSE_INCLUDE'
,
[
]
)
if
not
options
.
exclude
:
options
.
exclude
=
env
.
get
(
'NOSE_EXCLUDE'
,
[
]
)
self
.
addPaths
=
options
.
addPaths
self
.
stopOnError
=
options
.
stopOnError
self
.
verbosity
=
options
.
verbosity
self
.
includeExe
=
options
.
includeExe
self
.
traverseNamespace
=
options
.
traverseNamespace
self
.
debug
=
options
.
debug
self
.
debugLog
=
options
.
debugLog
self
.
loggingConfig
=
options
.
loggingConfig
self
.
firstPackageWins
=
options
.
firstPackageWins
self
.
configureLogging
(
)
if
options
.
where
is
not
:
self
.
configureWhere
(
options
.
where
)
if
options
.
testMatch
:
self
.
testMatch
=
re
.
compile
(
options
.
testMatch
)
if
options
.
ignoreFiles
:
self
.
ignoreFiles
=
map
(
re
.
compile
,
tolist
(
options
.
ignoreFiles
)
)
log
.
info
(
"Ignoring files matching %s"
,
options
.
ignoreFiles
)
else
:
log
.
info
(
"Ignoring files matching %s"
,
self
.
ignoreFilesDefaultStrings
)
if
options
.
include
:
self
.
include
=
map
(
re
.
compile
,
tolist
(
options
.
include
)
)
log
.
info
(
"Including tests matching %s"
,
options
.
include
)
if
options
.
exclude
:
self
.
exclude
=
map
(
re
.
compile
,
tolist
(
options
.
exclude
)
)
log
.
info
(
"Excluding tests matching %s"
,
options
.
exclude
)
if
not
options
.
showPlugins
:
self
.
plugins
.
configure
(
options
,
self
)
self
.
plugins
.
begin
(
)

Configure the nose running environment . Execute configure before collecting tests with nose . TestCollector to enable output capture and other features .


def configureLogging(self):
        """Configure logging for nose, or optionally other packages. Any logger
        name may be set with the debug option, and that logger will be set to
        debug level and be assigned the same handler as the nose loggers, unless
        it already has a handler.
        """
        if self.loggingConfig:
            from logging.config import fileConfig
            fileConfig(self.loggingConfig)
            return

        format = logging.Formatter('%(name)s: %(levelname)s: %(message)s')
        if self.debugLog:
            handler = logging.FileHandler(self.debugLog)
        else:
            handler = logging.StreamHandler(self.logStream)
        handler.setFormatter(format)

        logger = logging.getLogger('nose')
        logger.propagate = 0

        # only add our default handler if there isn't already one there
        # this avoids annoying duplicate log messages.
        if handler not in logger.handlers:
            logger.addHandler(handler)

        # default level
        lvl = logging.WARNING
        if self.verbosity >= 5:
            lvl = 0
        elif self.verbosity >= 4:
            lvl = logging.DEBUG
        elif self.verbosity >= 3:
            lvl = logging.INFO
        logger.setLevel(lvl)

        # individual overrides
        if self.debug:
            # no blanks
            debug_loggers = [ name for name in self.debug.split(',')
                              if name ]
            for logger_name in debug_loggers:
                l = logging.getLogger(logger_name)
                l.setLevel(logging.DEBUG)
                if not l.handlers and not logger_name.startswith('nose'):
                    l.addHandler(handler)

def
configureLogging
(
self
)
:
if
self
.
loggingConfig
:
from
logging
.
config
import
fileConfig
fileConfig
(
self
.
loggingConfig
)
return
format
=
logging
.
Formatter
(
'%(name)s: %(levelname)s: %(message)s'
)
if
self
.
debugLog
:
handler
=
logging
.
FileHandler
(
self
.
debugLog
)
else
:
handler
=
logging
.
StreamHandler
(
self
.
logStream
)
handler
.
setFormatter
(
format
)
logger
=
logging
.
getLogger
(
'nose'
)
logger
.
propagate
=
0
if
handler
not
in
logger
.
handlers
:
logger
.
addHandler
(
handler
)
lvl
=
logging
.
WARNING
if
self
.
verbosity
>=
5
:
lvl
=
0
elif
self
.
verbosity
>=
4
:
lvl
=
logging
.
DEBUG
elif
self
.
verbosity
>=
3
:
lvl
=
logging
.
INFO
logger
.
setLevel
(
lvl
)
if
self
.
debug
:
debug_loggers
=
[
name
for
name
in
self
.
debug
.
split
(
','
)
if
name
]
for
logger_name
in
debug_loggers
:
l
=
logging
.
getLogger
(
logger_name
)
l
.
setLevel
(
logging
.
DEBUG
)
if
not
l
.
handlers
and
not
logger_name
.
startswith
(
'nose'
)
:
l
.
addHandler
(
handler
)

Configure logging for nose or optionally other packages . Any logger name may be set with the debug option and that logger will be set to debug level and be assigned the same handler as the nose loggers unless it already has a handler .


def configureWhere(self, where):
        """Configure the working directory or directories for the test run.
        """
        from nose.importer import add_path
        self.workingDir = None
        where = tolist(where)
        warned = False
        for path in where:
            if not self.workingDir:
                abs_path = absdir(path)
                if abs_path is None:
                    raise ValueError("Working directory %s not found, or "
                                     "not a directory" % path)
                log.info("Set working dir to %s", abs_path)
                self.workingDir = abs_path
                if self.addPaths and \
                       os.path.exists(os.path.join(abs_path, '__init__.py')):
                    log.info("Working directory %s is a package; "
                             "adding to sys.path" % abs_path)
                    add_path(abs_path)
                continue
            if not warned:
                warn("Use of multiple -w arguments is deprecated and "
                     "support may be removed in a future release. You can "
                     "get the same behavior by passing directories without "
                     "the -w argument on the command line, or by using the "
                     "--tests argument in a configuration file.",
                     DeprecationWarning)
            self.testNames.append(path)

Configure the working directory or directories for the test run .


def page_dumb(strng, start=0, screen_lines=25):
    """Very dumb 'pager' in Python, for when nothing else works.

    Only moves forward, same interface as page(), except for pager_cmd and
    mode."""

    out_ln  = strng.splitlines()[start:]
    screens = chop(out_ln,screen_lines-1)
    if len(screens) == 1:
        print >>io.stdout, os.linesep.join(screens[0])
    else:
        last_escape = ""
        for scr in screens[0:-1]:
            hunk = os.linesep.join(scr)
            print >>io.stdout, last_escape + hunk
            if not page_more():
                return
            esc_list = esc_re.findall(hunk)
            if len(esc_list) > 0:
                last_escape = esc_list[-1]
        print >>io.stdout, last_escape + os.linesep.join(screens[-1])

def page_dumb ( strng , start = 0 , screen_lines = 25 ) : out_ln = strng . splitlines ( ) [ start : ] screens = chop ( out_ln , screen_lines - 1 ) if len ( screens ) == 1 : print >> io . stdout , os . linesep . join ( screens [ 0 ] ) else : last_escape = "" for scr in screens [ 0 : - 1 ] : hunk = os . linesep . join ( scr ) print >> io . stdout , last_escape + hunk if not page_more ( ) : return esc_list = esc_re . findall ( hunk ) if len ( esc_list ) > 0 : last_escape = esc_list [ - 1 ] print >> io . stdout , last_escape + os . linesep . join ( screens [ - 1 ] )

Very dumb pager in Python for when nothing else works .