PKM9_ipython_blocking.py""" ipython_blocking offers a context manager and IPython magic to capture cell execution. That is useful when you want a "blocking widget" or other situations where cell execution is paused until some trigger is met (like widget form validation) """ import sys import time import types from IPython.core import magic_arguments from IPython.core.magic import ( Magics, magics_class, line_magic, ) __version__ = '0.1.0' ### General context manager usage: # ctx = CaptureExecution() # with ctx: # while True: # if : # break # ctx.step() class CaptureExecution: "A context manager to capture execute_request events then replay them after exiting the manager" def __init__(self): self.captured_events = [] self.shell = get_ipython() self.kernel = self.shell.kernel def step(self): self.kernel.do_one_iteration() def capture_event(self, stream, ident, parent): "A 'capture' function to register instead of the default execute_request handling" self.captured_events.append((stream, ident, parent)) def start_capturing(self): "Overwrite the kernel shell handler to capture instead of executing new cell-execution requests" self.kernel.shell_handlers['execute_request'] = self.capture_event def stop_capturing(self): "revert the kernel shell handler to the default execute_request behavior" self.kernel.shell_handlers['execute_request'] = self.kernel.execute_request def replay_captured_events(self): "Called at end of context -- replays all captured events once the default execution handler is in place" # need to flush before replaying so messages show up in current cell not replay cells sys.stdout.flush() sys.stderr.flush() for stream, ident, parent in self.captured_events: # Using kernel.set_parent is the key to getting the output of the replayed events # to show up in the cells that were captured instead of the current cell self.kernel.set_parent(ident, parent) self.kernel.execute_request(stream, ident, parent) self.captured_events = [] def __enter__(self): self.start_capturing() self.shell.execution_count += 1 # increment execution count to avoid collision error def __exit__(self, *args): self.stop_capturing() self.replay_captured_events() @magics_class class CaptureMagic(Magics): def capture(self, breaking_func, timeout=None): start = time.time() ctx = CaptureExecution() with ctx: while True: if breaking_func(): break if timeout: if (time.time() - start) <= timeout: break ctx.step() @line_magic @magic_arguments.magic_arguments() @magic_arguments.argument('break_value', help='Widget object or function that defines a break from the blocking context') @magic_arguments.argument('-t', '--timeout', default=None, help="Timeout in seconds to stop capturing") def block(self, line): line = line.strip() args = magic_arguments.parse_argstring(self.block, line) obj = get_ipython().user_ns[args.break_value] ### Support one of two inputs for break_value ### 1) a callable function that will break when the function returns True ### 2) a single Widget, which will cause the context to break when the value changes if isinstance(obj, (types.FunctionType, types.MethodType)): func = obj else: starting_value = obj.value func = lambda: obj.value != starting_value return self.capture(func, args.timeout) def load_ipython_extensions(): get_ipython().register_magics(CaptureMagic) PKQM3I(ipython_blocking-0.1.0.dist-info/LICENSEBSD 3-Clause License Copyright (c) 2018, Matt Kafonek All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. PK!HJVSa&ipython_blocking-0.1.0.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,rzd&Y)r$[)T&UD"PK!H S)ipython_blocking-0.1.0.dist-info/METADATAUMo6WLmMk7No{ۤ0`9MI#}Cievyo8FEU>hg7vu^zސ9{WWm%|I}ͣ?>!9PbQP)@oc*UѺΜZQ#枛[|3QrjJY?^gzHM?z`̽^&-<FR:ImŔ܍sKxp4l[T*\(`([ @m&<8bpf6/b: lV|z+>u}7])ؤ,"\\M+| {mrEM@X üu/[;i1>#d*ÏQ<3e(^ɬ[˿lCJ7+\|pR_>J4SKݷKMeB.Ld7v.ZK+@UbPKM9_ipython_blocking.pyPKQM3I(ipython_blocking-0.1.0.dist-info/LICENSEPK!HJVSa&:ipython_blocking-0.1.0.dist-info/WHEELPK!H S)ipython_blocking-0.1.0.dist-info/METADATAPK!H~4g'(ipython_blocking-0.1.0.dist-info/RECORDPKi