Python IPython.core.magic_arguments.parse_argstring() Examples

The following are 30 code examples of IPython.core.magic_arguments.parse_argstring(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module IPython.core.magic_arguments , or try the search function .
Example #1
Source File: shell_magics.py    From coastermelt with MIT License 6 votes vote down vote up
def sc_read(self, line, cell=''):
        """Read blocks from the SCSI device.

        You can specify the LBA and address. With no arguments, goes into
        record-player mode and starts reading in order from the beginning.
        This is good if you just want the drive to read anything for testing.
        """
        args = parse_argstring(self.sc_read, line)
        d = self.shell.user_ns['d']
        lba = args.lba or 0

        while True:
            data = scsi_read(d, lba, args.blockcount or 1)
            if args.f:
                args.f.write(data)
                args.f.flush()

            sys.stdout.write(hexdump(data, address=lba*2048))
            if args.lba is None:
                # sequential mode
                lba += 1
            else:
                # Just one read
                break 
Example #2
Source File: shell_magics.py    From coastermelt with MIT License 6 votes vote down vote up
def watch(self, line):
        """Watch memory for changes, shows the results in an ASCII data table.

        To use the results programmatically, see the watch_scanner() and
        watch_tabulator() functions.

        Keeps running until you kill it with a KeyboardInterrupt.
        """
        args = parse_argstring(self.watch, line)
        d = self.shell.user_ns['d']
        changes = watch_scanner(d, args.address)
        try:
            for line in watch_tabulator(changes):
                sys.stdout.write(line + '\n')
        except KeyboardInterrupt:
            pass 
Example #3
Source File: shell_magics.py    From coastermelt with MIT License 6 votes vote down vote up
def ivt(self, line):
        """Read or modify the Interrupt Vector Table"""
        args = parse_argstring(self.ivt, line)
        d = self.shell.user_ns['d']

        if args.vector is None:
            # Show vector table. This only shows vectors with jumps, not
            # vectors that go directly to code.
            for addr in range(0, args.limit, 4):
                value = ivt_get(d, addr)
                if value is not None:
                    sys.stdout.write("vector %08x = %08x\n" % (addr, value))

        elif args.new_address is None:
            return ivt_get(d, args.vector)

        else:
            ivt_set(d, args.vector, args.new_address) 
Example #4
Source File: jupyternotify.py    From jupyter-notify with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def autonotify(self, line):
        # Record options
        args = parse_argstring(self.autonotify, line)
        self.options["body"] = args.message.lstrip("\'\"").rstrip("\'\"")
        self.options['autonotify_after'] = args.after
        self.options['autonotify_output'] = args.output

        ### Register events
        ip = get_ipython()

        # Remove events if they're already registered
        # This is necessary because jupyter makes a new instance everytime
        pre, post = self.__class__._events
        if pre and pre in ip.events.callbacks['pre_run_cell']:
            ip.events.callbacks['pre_run_cell'].remove(pre)
        if post and post in ip.events.callbacks['post_run_cell']:
            ip.events.callbacks['post_run_cell'].remove(post)

        # Register new events
        ip.events.register('pre_run_cell', self.pre_run_cell)
        ip.events.register('post_run_cell', self.post_run_cell)
        self.__class__._events = self.pre_run_cell, self.post_run_cell 
Example #5
Source File: heat.py    From pyheatmagic with MIT License 6 votes vote down vote up
def heat(self, line, cell):
        """Method to profile the python code in the ipython cell and display it
        as a heatmap using py-heat package.

        :param line: Line value for the ipython line this magic is called from.
        :param cell: Cell value for the ipython cell this magic is called from.
        """
        args = magic_arguments.parse_argstring(self.heat, line)
        filename = args.out
        if filename is not None:
            filename = os.path.expanduser(args.out)

        _, tmp_file = mkstemp()
        with open(tmp_file, "wb") as f:
            f.write(cell.encode())

        pyheat = PyHeat(tmp_file)
        pyheat.create_heatmap()
        pyheat.show_heatmap(output_file=filename)
        pyheat.close_heatmap()

        os.remove(tmp_file) 
Example #6
Source File: shell_magics.py    From coastermelt with MIT License 5 votes vote down vote up
def console(self, line):
        """Read console output until KeyboardInterrupt.
        Optionally append the output to a file also.
        To write to this console from C++, use the functions in console.h
        """
        args = parse_argstring(self.console, line)
        d = self.shell.user_ns['d']
        console_mainloop(d, buffer=args.buffer_address, log_filename=args.f,
            use_fast_read = not args.slow) 
Example #7
Source File: sectools_magics.py    From msticpy with MIT License 5 votes vote down vote up
def b64(self, line: str = "", cell: str = None) -> str:
        """
        Base64 IPython magic extension.

        Parameters
        ----------
        line : str, optional
            Line contents, by default ""
        cell : str, optional
            Cell contents, by default None

        Returns
        -------
        str
            Decoded text

        """
        if cell is None:
            results, df_results = base64.unpack(line)

        else:
            results, df_results = base64.unpack(cell)
        args = magic_arguments.parse_argstring(self.b64, line)

        if args.clean:
            results = re.sub(self._STRIP_TAGS, "", results)
        elif args.pretty:
            if _BS_AVAILABLE:
                xml_str = f"<decoded_string>{results}</decoded_string>"
                b_soup = BeautifulSoup(xml_str, "xml")
                results = b_soup.prettify()
        if args.out is not None:
            self.shell.user_ns[args.out] = (results, df_results)
        return results 
Example #8
Source File: shell_magics.py    From coastermelt with MIT License 5 votes vote down vote up
def find(self, line):
        """Read ARM memory block, and look for all occurrences of a byte sequence"""
        args = parse_argstring(self.find, line)
        d = self.shell.user_ns['d']
        substr = ''.join(map(chr, args.byte))

        results = search_block(d, args.address, args.size, substr, fast=args.fast, addr_space=args.space)

        for address, before, after in results:
            sys.stdout.write("%08x %52s [ %s ] %s\n" %
                (address, hexstr(before), hexstr(substr), hexstr(after))) 
Example #9
Source File: shell_magics.py    From coastermelt with MIT License 5 votes vote down vote up
def bitfuzz(self, line):
        """Scan a small number of words in binary while writing 00000000/ffffffff patterns.
        This can help determine the implementation of bits in an MMIO register.
        """
        args = parse_argstring(self.bitfuzz, line)
        d = self.shell.user_ns['d']
        try:
            for line in bitfuzz_rounds(d, args.address, args.wordcount, args.period, args.delay):
                print(line)
        except KeyboardInterrupt:
            return 
Example #10
Source File: shell_magics.py    From coastermelt with MIT License 5 votes vote down vote up
def ovl(self, line):
        """Position a movable RAM overlay at the indicated virtual address range.
        With no parameters, shows the current location of the RAM.

        It can go anywhere in the first 8MB. So, put it between 20_ and 80_, fill it with
        tasty data, then move it overtop of flash. Or see the wrf / asmf commands to do this
        quickly in one step.
        """
        args = parse_argstring(self.ovl, line)
        d = self.shell.user_ns['d']
        if args.address is None:
            sys.stdout.write("base = %x, wordcount = %x\n" % overlay_get(d))
        else:
            overlay_set(d, args.address, args.wordcount) 
Example #11
Source File: shell_magics.py    From coastermelt with MIT License 5 votes vote down vote up
def dis(self, line):
        """Disassemble ARM instructions"""
        args = parse_argstring(self.dis, line)
        d = self.shell.user_ns['d']
        sys.stdout.write(disassemble(d, args.address, args.size, thumb = not args.arm) + '\n') 
Example #12
Source File: shell_magics.py    From coastermelt with MIT License 5 votes vote down vote up
def asm(self, line, cell=''):
        """Assemble one or more ARM instructions"""
        args = parse_argstring(self.asm, line)
        d = self.shell.user_ns['d']
        code = ' '.join(args.code) + '\n' + cell
        try:
            assemble(d, args.address, code, defines=all_defines())
        except CodeError as e:
            raise UsageError(str(e)) 
Example #13
Source File: shell_magics.py    From coastermelt with MIT License 5 votes vote down vote up
def asmf(self, line, cell='', va=0x500000):
        """Assemble ARM instructions into a patch we instantly overlay onto Flash.
        Combines the 'asm' and 'wrf' commands.
        """
        args = parse_argstring(self.asmf, line)
        d = self.shell.user_ns['d']
        code = ' '.join(args.code) + '\n' + cell
        overlay_assemble(d, args.address, code, defines=all_defines(), va=va) 
Example #14
Source File: ipython.py    From storlets with Apache License 2.0 5 votes vote down vote up
def put(self, line):
        args = magic_arguments.parse_argstring(self.put, line)
        if not args.o:
            raise UsageError('-o option is mandatory for the invocation')
        if not args.o[0].startswith(tuple(string.ascii_letters)):
            raise UsageError('The output variable name must be a valid prefix '
                             'of a python variable, that is, start with a '
                             'letter')
        if not args.storlet:
            raise UsageError('--storlet option is mandatory '
                             'for the invocation')
        if not args.input:
            raise UsageError('--input option is mandatory for the invocation')
        if not args.input.startswith('/'):
            raise UsageError('--input argument must be a full path')

        if not args.output:
            raise UsageError('--output option is mandatory for the invocation')

        dst_container, dst_obj = self._parse_input_path(args.output)

        headers = {'X-Run-Storlet': '%s' % args.storlet}
        # pick -i option and translate the params to
        # X-Storlet-Parameter-x headers
        storlet_headers = self._generate_params_headers(
            self.shell.user_ns[args.i] if args.i else {})
        headers.update(storlet_headers)

        # invoke storlet app on copy
        conn = get_swift_connection()
        response_dict = dict()
        with open(args.input, 'rb') as content:
            conn.put_object(
                dst_container, dst_obj,
                content,
                headers=headers,
                response_dict=response_dict)

        res = Response(int(response_dict['status']),
                       response_dict['headers'])
        self.shell.user_ns[args.o] = res 
Example #15
Source File: ipython.py    From storlets with Apache License 2.0 5 votes vote down vote up
def uploadfile(self, line, cell):
        """Upload the contents of the cell to OpenStack Swift.
        """
        args = magic_arguments.parse_argstring(self.uploadfile, line)
        container, obj = args.container_obj.split('/', 1)
        conn = get_swift_connection()
        conn.put_object(container, obj, cell,
                        {'Content-Type': 'application/python'}) 
Example #16
Source File: shell_magics.py    From coastermelt with MIT License 5 votes vote down vote up
def reset(self, line=''):
        """Reset and reopen the USB interface."""
        args = parse_argstring(self.reset, line)
        d = self.shell.user_ns.get('d_remote')

        if not d:
            # Recover from starting up without a device
            d = remote.Device()
            self.shell.user_ns['d_remote'] = d
            self.shell.user_ns['d'] = d

        d.reset()
        if args.arm:
            reset_arm(d) 
Example #17
Source File: jupyter_slack.py    From jupyter-slack-notify with MIT License 5 votes vote down vote up
def notify(self, line="", cell=None):
        args = magic_arguments.parse_argstring(self.notify, line)
        mess = args.message.replace("\"", "")
        with Monitor(mess, time=args.time):
            self.shell.ex(cell) 
Example #18
Source File: _system_commands.py    From colabtools with Apache License 2.0 5 votes vote down vote up
def _shell_cell_magic(args, cmd):
  """Run the cell via a shell command, allowing input to be provided.

  Also available as a line magic.

  Usage:
    # Returns a ShellResult.
    %%shell
    echo "hello"

  This is similar to Jupyter's `!` magic, but additionally allows input to be
  provided to the subprocess. By default, if the subprocess returns a non-zero
  exit code a `subprocess.CalledProcessError` is raised. The provided command
  is run within a bash shell.

  Args:
    args: Optional arguments.
    cmd: The shell command to execute.

  Returns:
    ShellResult containing the results of the executed command.

  Raises:
    subprocess.CalledProcessError: If the subprocess exited with a non-zero
      exit code and the `ignore_errors` argument wasn't provided.
  """

  parsed_args = magic_arguments.parse_argstring(_shell_cell_magic, args)

  result = _run_command(cmd, clear_streamed_output=False)
  if not parsed_args.ignore_errors:
    result.check_returncode()
  return result 
Example #19
Source File: jupyternotify.py    From jupyter-notify with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def notify(self, line, cell=None):

        # Duplicate options to allow notifications running in the same cell to
        # have separate messages.
        options = dict(self.options)

        # custom message
        args = parse_argstring(self.notify, line)
        options["body"] = args.message.lstrip("\'\"").rstrip("\'\"")

        # generate a uuid so that we only deliver this notification once, not again
        # when the browser reloads (we append a div to check that)
        notification_uuid = uuid.uuid4()

        # Run cell if its cell magic
        if cell is not None:
            output = get_ipython().run_cell(cell)

            # prevent autonotify from firing with notify cell magic
            self.__class__.notification_uuid = None

            # Get cell output and set as message
            if args.output and output.result is not None:
                try:
                    options['body'] = str(output.result)
                except ValueError:
                    pass # can't convert to string. Use default message


        # display our browser notification using javascript
        self.display_notification(options, notification_uuid) 
Example #20
Source File: ipython.py    From storlets with Apache License 2.0 5 votes vote down vote up
def copy(self, line):
        args = magic_arguments.parse_argstring(self.copy, line)
        if not args.o:
            raise UsageError('-o option is mandatory for the invocation')
        if not args.o[0].startswith(tuple(string.ascii_letters)):
            raise UsageError('The output variable name must be a valid prefix '
                             'of a python variable, that is, start with a '
                             'letter')
        if not args.storlet:
            raise UsageError('--storlet option is mandatory '
                             'for the invocation')
        if not args.input:
            raise UsageError('--input option is mandatory for the invocation')

        if not args.output:
            raise UsageError('--output option is mandatory for the invocation')

        src_container, src_obj = self._parse_input_path(args.input)
        dst_container, dst_obj = self._parse_input_path(args.output)
        destination = '/%s/%s' % (dst_container, dst_obj)

        headers = {'X-Run-Storlet': '%s' % args.storlet}
        # pick -i option and translate the params to
        # X-Storlet-Parameter-x headers
        storlet_headers = self._generate_params_headers(
            self.shell.user_ns[args.i] if args.i else {})
        headers.update(storlet_headers)

        # invoke storlet app on copy
        conn = get_swift_connection()

        response_dict = dict()
        conn.copy_object(
            src_container, src_obj,
            destination=destination,
            headers=headers,
            response_dict=response_dict)

        res = Response(int(response_dict['status']),
                       response_dict['headers'])
        self.shell.user_ns[args.o] = res 
Example #21
Source File: ipython.py    From storlets with Apache License 2.0 5 votes vote down vote up
def get(self, line):
        args = magic_arguments.parse_argstring(self.get, line)
        if not args.o:
            raise UsageError('-o option is mandatory for the invocation')
        if not args.o[0].startswith(tuple(string.ascii_letters)):
            raise UsageError('The output variable name must be a valid prefix '
                             'of a python variable, that is, start with a '
                             'letter')
        if not args.storlet:
            raise UsageError('--storlet option is mandatory '
                             'for the invocation')
        if not args.input:
            raise UsageError('--input option is mandatory for the invocation')

        src_container, src_obj = self._parse_input_path(args.input)

        headers = {'X-Run-Storlet': '%s' % args.storlet}
        # pick -i option and translate the params to
        # X-Storlet-Parameter-x headers
        storlet_headers = self._generate_params_headers(
            self.shell.user_ns[args.i] if args.i else {})
        headers.update(storlet_headers)

        # invoke storlet app on get
        conn = get_swift_connection()
        response_dict = dict()
        resp_headers, resp_content_iter = conn.get_object(
            src_container, src_obj,
            resp_chunk_size=64 * 1024,
            headers=headers,
            response_dict=response_dict)

        res = Response(int(response_dict['status']),
                       resp_headers,
                       resp_content_iter)
        self.shell.user_ns[args.o] = res 
Example #22
Source File: blackcellmagic.py    From blackcellmagic with MIT License 5 votes vote down vote up
def black(self, line, cell):
        """Magic command to format the IPython cell."""
        args = magic_arguments.parse_argstring(self.black, line)
        line_length = args.line_length
        if cell:
            try:
                from black import FileMode
                mode = FileMode(line_length=line_length)
                formatted = format_str(src_contents=cell, mode=mode)
            except TypeError:
                formatted = format_str(src_contents=cell, line_length=line_length)
            if formatted and formatted[-1] == "\n":
                    formatted = formatted[:-1]
            self.shell.set_next_input(formatted, replace=True) 
Example #23
Source File: shell_magics.py    From coastermelt with MIT License 5 votes vote down vote up
def bic(self, line, cell=''):
        """Read/modify/write hex words into ARM memory, [mem] &= ~arg"""
        args = parse_argstring(self.bic, line)
        d = self.shell.user_ns['d']
        args.word.extend(map(hexint, cell.split()))
        for i, w in enumerate(args.word):
            poke_bic(d, args.address + i*4, w) 
Example #24
Source File: sectools_magics.py    From msticpy with MIT License 5 votes vote down vote up
def ioc(self, line="", cell=None) -> List[Tuple[str, List[str]]]:
        """
        Ioc Extract IPython magic extension.

        Parameters
        ----------
        line : str, optional
            Line contents, by default ""
        cell : str, optional
            Cell contents, by default None

        Returns
        -------
        List[Tuple[str, List[str]]]
            List of tuples of IoCs found grouped by type.

        """
        args = magic_arguments.parse_argstring(self.ioc, line)
        ioc_types = None
        if args.ioc_types:
            ioc_types = [ioc_type.strip() for ioc_type in args.ioc_types.split(",")]

        if cell is None:
            results = self._ioc_extract.extract(src=line, ioc_types=ioc_types)
        else:
            results = self._ioc_extract.extract(src=cell, ioc_types=ioc_types)
        iocs = [(ioc_type, list(ioc_res)) for ioc_type, ioc_res in results.items()]

        if args.out is not None:
            self.shell.user_ns[args.out] = results
        return iocs 
Example #25
Source File: magics.py    From Computable with MIT License 5 votes vote down vote up
def pxconfig(self, line):
        """configure default targets/blocking for %px magics"""
        args = magic_arguments.parse_argstring(self.pxconfig, line)
        if args.targets:
            self.view.targets = self._eval_target_str(args.targets)
        if args.block is not None:
            self.view.block = args.block
        if args.set_verbose is not None:
            self.verbose = args.set_verbose 
Example #26
Source File: magics.py    From Computable with MIT License 5 votes vote down vote up
def result(self, line=''):
        """Print the result of the last asynchronous %px command.
        
        This lets you recall the results of %px computations after
        asynchronous submission (block=False).

        Examples
        --------
        ::

            In [23]: %px os.getpid()
            Async parallel execution on engine(s): all

            In [24]: %pxresult
            Out[8:10]: 60920
            Out[9:10]: 60921
            Out[10:10]: 60922
            Out[11:10]: 60923
        """
        args = magic_arguments.parse_argstring(self.result, line)
        
        if self.last_result is None:
            raise UsageError(NO_LAST_RESULT)
        
        self.last_result.get()
        self.last_result.display_outputs(groupby=args.groupby) 
Example #27
Source File: execution.py    From Computable with MIT License 5 votes vote down vote up
def debug(self, line='', cell=None):
        """Activate the interactive debugger.

        This magic command support two ways of activating debugger.
        One is to activate debugger before executing code.  This way, you
        can set a break point, to step through the code from the point.
        You can use this mode by giving statements to execute and optionally
        a breakpoint.

        The other one is to activate debugger in post-mortem mode.  You can
        activate this mode simply running %debug without any argument.
        If an exception has just occurred, this lets you inspect its stack
        frames interactively.  Note that this will always work only on the last
        traceback that occurred, so you must call this quickly after an
        exception that you wish to inspect has fired, because if another one
        occurs, it clobbers the previous one.

        If you want IPython to automatically do this on every exception, see
        the %pdb magic for more details.
        """
        args = magic_arguments.parse_argstring(self.debug, line)

        if not (args.breakpoint or args.statement or cell):
            self._debug_post_mortem()
        else:
            code = "\n".join(args.statement)
            if cell:
                code += "\n" + cell
            self._debug_exec(code, args.breakpoint) 
Example #28
Source File: execution.py    From Computable with MIT License 5 votes vote down vote up
def capture(self, line, cell):
        """run the cell, capturing stdout/err"""
        args = magic_arguments.parse_argstring(self.capture, line)
        out = not args.no_stdout
        err = not args.no_stderr
        with capture_output(out, err) as io:
            self.shell.run_cell(cell)
        if args.output:
            self.shell.user_ns[args.output] = io 
Example #29
Source File: pylab.py    From Computable with MIT License 5 votes vote down vote up
def matplotlib(self, line=''):
        """Set up matplotlib to work interactively.
        
        This function lets you activate matplotlib interactive support
        at any point during an IPython session.
        It does not import anything into the interactive namespace.
        
        If you are using the inline matplotlib backend for embedded figures,
        you can adjust its behavior via the %config magic::

            # enable SVG figures, necessary for SVG+XHTML export in the qtconsole
            In [1]: %config InlineBackend.figure_format = 'svg'

            # change the behavior of closing all figures at the end of each
            # execution (cell), or allowing reuse of active figures across
            # cells:
            In [2]: %config InlineBackend.close_figures = False

        Examples
        --------
        In this case, where the MPL default is TkAgg::

            In [2]: %matplotlib
            Using matplotlib backend: TkAgg

        But you can explicitly request a different backend::

            In [3]: %matplotlib qt
        """
        args = magic_arguments.parse_argstring(self.matplotlib, line)
        gui, backend = self.shell.enable_matplotlib(args.gui)
        self._show_matplotlib_backend(args.gui, backend) 
Example #30
Source File: basic.py    From Computable with MIT License 5 votes vote down vote up
def notebook(self, s):
        """Export and convert IPython notebooks.

        This function can export the current IPython history to a notebook file
        or can convert an existing notebook file into a different format. For
        example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
        To export the history to "foo.py" do "%notebook -e foo.py". To convert
        "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible
        formats include (json/ipynb, py).
        """
        args = magic_arguments.parse_argstring(self.notebook, s)

        from IPython.nbformat import current
        args.filename = unquote_filename(args.filename)
        if args.export:
            fname, name, format = current.parse_filename(args.filename)
            cells = []
            hist = list(self.shell.history_manager.get_range())
            for session, prompt_number, input in hist[:-1]:
                cells.append(current.new_code_cell(prompt_number=prompt_number,
                                                   input=input))
            worksheet = current.new_worksheet(cells=cells)
            nb = current.new_notebook(name=name,worksheets=[worksheet])
            with io.open(fname, 'w', encoding='utf-8') as f:
                current.write(nb, f, format);
        elif args.format is not None:
            old_fname, old_name, old_format = current.parse_filename(args.filename)
            new_format = args.format
            if new_format == u'xml':
                raise ValueError('Notebooks cannot be written as xml.')
            elif new_format == u'ipynb' or new_format == u'json':
                new_fname = old_name + u'.ipynb'
                new_format = u'json'
            elif new_format == u'py':
                new_fname = old_name + u'.py'
            else:
                raise ValueError('Invalid notebook format: %s' % new_format)
            with io.open(old_fname, 'r', encoding='utf-8') as f:
                nb = current.read(f, old_format)
            with io.open(new_fname, 'w', encoding='utf-8') as f:
                current.write(nb, f, new_format)