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

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

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

Display the JPEG representation of an object .


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

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

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

Display the LaTeX representation of an object .


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

    Note that not many frontends support displaying JSON.

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

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

Display the JSON representation of an object .


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

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

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

Display the Javascript representation of an object .


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


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

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

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

Find the full path to a command using which .


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

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

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

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


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

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

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

Forward read events from an FD over a socket .


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

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


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

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

Return a launcher for a given clsname and kind .


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

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

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


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

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

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


def start(self):
        """Start the app for the engines subcommand."""
        self.log.info("IPython cluster: started")
        # First see if the cluster is already running

        # Now log and daemonize
        self.log.info(
            'Starting engines with [daemon=%r]' % self.daemonize
        )
        # TODO: Get daemonize working on Windows or as a Windows Server.
        if self.daemonize:
            if os.name=='posix':
                daemonize()

        dc = ioloop.DelayedCallback(self.start_engines, 0, self.loop)
        dc.start()
        # Now write the new pid file AFTER our new forked pid is active.
        # self.write_pid_file()
        try:
            self.loop.start()
        except KeyboardInterrupt:
            pass
        except zmq.ZMQError as e:
            if e.errno == errno.EINTR:
                pass
            else:
                raise

Start the app for the engines subcommand .


def start(self):
        """Start the app for the start subcommand."""
        # First see if the cluster is already running
        try:
            pid = self.get_pid_from_file()
        except PIDFileError:
            pass
        else:
            if self.check_pid(pid):
                self.log.critical(
                    'Cluster is already running with [pid=%s]. '
                    'use "ipcluster stop" to stop the cluster.' % pid
                )
                # Here I exit with a unusual exit status that other processes
                # can watch for to learn how I existed.
                self.exit(ALREADY_STARTED)
            else:
                self.remove_pid_file()

        # Now log and daemonize
        self.log.info(
            'Starting ipcluster with [daemon=%r]' % self.daemonize
        )
        # TODO: Get daemonize working on Windows or as a Windows Server.
        if self.daemonize:
            if os.name=='posix':
                daemonize()

        dc = ioloop.DelayedCallback(self.start_controller, 0, self.loop)
        dc.start()
        dc = ioloop.DelayedCallback(self.start_engines, 1000*self.delay, self.loop)
        dc.start()
        # Now write the new pid file AFTER our new forked pid is active.
        self.write_pid_file()
        try:
            self.loop.start()
        except KeyboardInterrupt:
            pass
        except zmq.ZMQError as e:
            if e.errno == errno.EINTR:
                pass
            else:
                raise
        finally:
            self.remove_pid_file()

Start the app for the start subcommand .


def get_app_wx(*args, **kwargs):
    """Create a new wx app or return an exiting one."""
    import wx
    app = wx.GetApp()
    if app is None:
        if not kwargs.has_key('redirect'):
            kwargs['redirect'] = False
        app = wx.PySimpleApp(*args, **kwargs)
    return app

def get_app_wx ( * args , * * kwargs ) : import wx app = wx . GetApp ( ) if app is None : if not kwargs . has_key ( 'redirect' ) : kwargs [ 'redirect' ] = False app = wx . PySimpleApp ( * args , * * kwargs ) return app

Create a new wx app or return an exiting one .


def is_event_loop_running_wx(app=None):
    """Is the wx event loop running."""
    if app is None:
        app = get_app_wx()
    if hasattr(app, '_in_event_loop'):
        return app._in_event_loop
    else:
        return app.IsMainLoopRunning()

def is_event_loop_running_wx ( app = None ) : if app is None : app = get_app_wx ( ) if hasattr ( app , '_in_event_loop' ) : return app . _in_event_loop else : return app . IsMainLoopRunning ( )


def start_event_loop_wx(app=None):
    """Start the wx event loop in a consistent manner."""
    if app is None:
        app = get_app_wx()
    if not is_event_loop_running_wx(app):
        app._in_event_loop = True
        app.MainLoop()
        app._in_event_loop = False
    else:
        app._in_event_loop = True

def start_event_loop_wx ( app = None ) : if app is None : app = get_app_wx ( ) if not is_event_loop_running_wx ( app ) : app . _in_event_loop = True app . MainLoop ( ) app . _in_event_loop = False else : app . _in_event_loop = True

Start the wx event loop in a consistent manner .


def get_app_qt4(*args, **kwargs):
    """Create a new qt4 app or return an existing one."""
    from IPython.external.qt_for_kernel import QtGui
    app = QtGui.QApplication.instance()
    if app is None:
        if not args:
            args = ([''],)
        app = QtGui.QApplication(*args, **kwargs)
    return app

def get_app_qt4 ( * args , * * kwargs ) : from IPython . external . qt_for_kernel import QtGui app = QtGui . QApplication . instance ( ) if app is None : if not args : args = ( [ '' ] , ) app = QtGui . QApplication ( * args , * * kwargs ) return app

Create a new qt4 app or return an existing one .


def is_event_loop_running_qt4(app=None):
    """Is the qt4 event loop running."""
    if app is None:
        app = get_app_qt4([''])
    if hasattr(app, '_in_event_loop'):
        return app._in_event_loop
    else:
        # Does qt4 provide a other way to detect this?
        return False


def start_event_loop_qt4(app=None):
    """Start the qt4 event loop in a consistent manner."""
    if app is None:
        app = get_app_qt4([''])
    if not is_event_loop_running_qt4(app):
        app._in_event_loop = True
        app.exec_()
        app._in_event_loop = False
    else:
        app._in_event_loop = True

def start_event_loop_qt4 ( app = None ) : if app is None : app = get_app_qt4 ( [ '' ] ) if not is_event_loop_running_qt4 ( app ) : app . _in_event_loop = True app . exec_ ( ) app . _in_event_loop = False else : app . _in_event_loop = True

Start the qt4 event loop in a consistent manner .


def blank_canvas(width, height):
        """Return a blank canvas to annotate.

        :param width: xdim (int)
        :param height: ydim (int)
        :returns: :class:`jicbioimage.illustrate.Canvas`
        """
        canvas = np.zeros((height, width, 3), dtype=np.uint8)
        return canvas.view(Canvas)

def blank_canvas ( width , height ) : canvas = np . zeros ( ( height , width , 3 ) , dtype = np . uint8 ) return canvas . view ( Canvas )


def draw_cross(self, position, color=(255, 0, 0), radius=4):
        """Draw a cross on the canvas.

        :param position: (row, col) tuple
        :param color: RGB tuple
        :param radius: radius of the cross (int)
        """
        y, x = position
        for xmod in np.arange(-radius, radius+1, 1):
            xpos = x + xmod
            if xpos < 0:
                continue  # Negative indices will draw on the opposite side.
            if xpos >= self.shape[1]:
                continue  # Out of bounds.
            self[int(y), int(xpos)] = color
        for ymod in np.arange(-radius, radius+1, 1):
            ypos = y + ymod
            if ypos < 0:
                continue  # Negative indices will draw on the opposite side.
            if ypos >= self.shape[0]:
                continue  # Out of bounds.
            self[int(ypos), int(x)] = color


def draw_line(self, pos1, pos2, color=(255, 0, 0)):
        """Draw a line between pos1 and pos2 on the canvas.

        :param pos1: position 1 (row, col) tuple
        :param pos2: position 2 (row, col) tuple
        :param color: RGB tuple
        """
        r1, c1 = tuple([int(round(i, 0)) for i in pos1])
        r2, c2 = tuple([int(round(i, 0)) for i in pos2])
        rr, cc = skimage.draw.line(r1, c1, r2, c2)
        self[rr, cc] = color

def draw_line ( self , pos1 , pos2 , color = ( 255 , 0 , 0 ) ) : r1 , c1 = tuple ( [ int ( round ( i , 0 ) ) for i in pos1 ] ) r2 , c2 = tuple ( [ int ( round ( i , 0 ) ) for i in pos2 ] ) rr , cc = skimage . draw . line ( r1 , c1 , r2 , c2 ) self [ rr , cc ] = color

Draw a line between pos1 and pos2 on the canvas .


def text_at(self, text, position, color=(255, 255, 255),
                size=12, antialias=False, center=False):
        """Write text at x, y top left corner position.

        By default the x and y coordinates represent the top left hand corner
        of the text. The text can be centered vertically and horizontally by
        using setting the ``center`` option to ``True``.

        :param text: text to write
        :param position: (row, col) tuple
        :param color: RGB tuple
        :param size: font size
        :param antialias: whether or not the text should be antialiased
        :param center: whether or not the text should be centered on the
                       input coordinate
        """
        def antialias_value(value, normalisation):
            return int(round(value * normalisation))

        def antialias_rgb(color, normalisation):
            return tuple([antialias_value(v, normalisation) for v in color])

        def set_color(xpos, ypos, color):
            try:
                self[ypos, xpos] = color
            except IndexError:
                pass

        y, x = position
        font = PIL.ImageFont.truetype(DEFAULT_FONT_PATH, size=size)
        mask = font.getmask(text)
        width, height = mask.size
        if center:
            x = x - (width // 2)
            y = y - (height // 2)
        for ystep in range(height):
            for xstep in range(width):
                normalisation = mask[ystep * width + xstep] / 255.
                if antialias:
                    if normalisation != 0:
                        rgb_color = antialias_rgb(color, normalisation)
                        set_color(x + xstep, y+ystep, rgb_color)
                else:
                    if normalisation > .5:
                        set_color(x + xstep, y + ystep, color)

def text_at ( self , text , position , color = ( 255 , 255 , 255 ) , size = 12 , antialias = False , center = False ) : def antialias_value ( value , normalisation ) : return int ( round ( value * normalisation ) ) def antialias_rgb ( color , normalisation ) : return tuple ( [ antialias_value ( v , normalisation ) for v in color ] ) def set_color ( xpos , ypos , color ) : try : self [ ypos , xpos ] = color except IndexError : pass y , x = position font = PIL . ImageFont . truetype ( DEFAULT_FONT_PATH , size = size ) mask = font . getmask ( text ) width , height = mask . size if center : x = x - ( width // 2 ) y = y - ( height // 2 ) for ystep in range ( height ) : for xstep in range ( width ) : normalisation = mask [ ystep * width + xstep ] / 255. if antialias : if normalisation != 0 : rgb_color = antialias_rgb ( color , normalisation ) set_color ( x + xstep , y + ystep , rgb_color ) else : if normalisation > .5 : set_color ( x + xstep , y + ystep , color )

Write text at x y top left corner position .


def from_grayscale(im, channels_on=(True, True, True)):
        """Return a canvas from a grayscale image.

        :param im: single channel image
        :channels_on: channels to populate with input image
        :returns: :class:`jicbioimage.illustrate.Canvas`
        """
        xdim, ydim = im.shape
        canvas = np.zeros((xdim, ydim, 3), dtype=np.uint8)
        for i, include in enumerate(channels_on):
            if include:
                canvas[:, :, i] = im
        return canvas.view(AnnotatedImage)

def from_grayscale ( im , channels_on = ( True , True , True ) ) : xdim , ydim = im . shape canvas = np . zeros ( ( xdim , ydim , 3 ) , dtype = np . uint8 ) for i , include in enumerate ( channels_on ) : if include : canvas [ : , : , i ] = im return canvas . view ( AnnotatedImage )

Return a canvas from a grayscale image .


def get_uuid(length=32, version=1):
    """
    Returns a unique ID of a given length.
    User `version=2` for cross-systems uniqueness.
    """
    if version == 1:
        return uuid.uuid1().hex[:length]
    else:
        return uuid.uuid4().hex[:length]

def get_uuid ( length = 32 , version = 1 ) : if version == 1 : return uuid . uuid1 ( ) . hex [ : length ] else : return uuid . uuid4 ( ) . hex [ : length ]

Returns a unique ID of a given length . User version = 2 for cross - systems uniqueness .


def get_unique_key_from_get(get_dict):
    """
    Build a unique key from get data
    """
    site = Site.objects.get_current()
    key = get_dict_to_encoded_url(get_dict)
    cache_key = '{}_{}'.format(site.domain, key)
    return hashlib.md5(cache_key).hexdigest()

def get_unique_key_from_get ( get_dict ) : site = Site . objects . get_current ( ) key = get_dict_to_encoded_url ( get_dict ) cache_key = '{}_{}' . format ( site . domain , key ) return hashlib . md5 ( cache_key ) . hexdigest ( )


def get_domain(url):
    """ Returns domain name portion of a URL """
    if 'http' not in url.lower():
        url = 'http://{}'.format(url)
    return urllib.parse.urlparse(url).hostname

def get_domain ( url ) : if 'http' not in url . lower ( ) : url = 'http://{}' . format ( url ) return urllib . parse . urlparse ( url ) . hostname


def get_url_args(url):
    """ Returns a dictionary from a URL params """
    url_data = urllib.parse.urlparse(url)
    arg_dict = urllib.parse.parse_qs(url_data.query)
    return arg_dict

def get_url_args ( url ) : url_data = urllib . parse . urlparse ( url ) arg_dict = urllib . parse . parse_qs ( url_data . query ) return arg_dict