SAS Kernel magic to show the SAS log for the previous submitted code.
        This magic is only available within the SAS Kernel
979024
def line_showLog(self):
        
        if self.kernel.mva is None:
            print("Can't show log because no session exists")
        else:
            return self.kernel.Display(HTML(self.kernel.cachedlog))
9501
def columnInfo(self):
        
        code = "proc contents data=" + self.libref + '.' + self.table + ' ' + self._dsopts() + ";ods select Variables;run;"

        if self.sas.nosub:
            print(code)
            return

        if self.results.upper() == 'PANDAS':
            code = "proc contents data=%s.%s %s ;ods output Variables=work._variables ;run;" % (self.libref, self.table, self._dsopts())
            pd = self._returnPD(code, '_variables')
            pd['Type'] = pd['Type'].str.rstrip()
            return pd

        else:
            ll = self._is_valid()
            if self.HTML:
                if not ll:
                    ll = self.sas._io.submit(code)
                if not self.sas.batch:
                    self.sas.DISPLAY(self.sas.HTML(ll['LST']))
                else:
                    return ll
            else:
                if not ll:
                    ll = self.sas._io.submit(code, "text")
                if not self.sas.batch:
                    print(ll['LST'])
                else:
                    return ll
460930
def print_code_table(self, out=sys.stdout):
        
        out.write(u'bits  code       (value)  symbol\n')
        for symbol, (bitsize, value) in sorted(self._table.items()):
            out.write(u'{b:4d}  {c:10} ({v:5d})  {s!r}\n'.format(
                b=bitsize, v=value, s=symbol, c=bin(value)[2:].rjust(bitsize, '0')
            ))
606567
def _log_diff_memory_data(self, prefix, new_memory_data, old_memory_data):
        
        def _vmem_used(memory_data):
            return memory_data['machine_data'].used

        def _process_mem_percent(memory_data):
            return memory_data['process_data']['memory_percent']

        def _process_rss(memory_data):
            return memory_data['process_data']['memory_info'].rss

        def _process_vms(memory_data):
            return memory_data['process_data']['memory_info'].vms

        if new_memory_data and old_memory_data:
            log.info(
                u"%s Diff Vmem used: %s, Diff percent memory: %s, Diff rss: %s, Diff vms: %s",
                prefix,
                _vmem_used(new_memory_data) - _vmem_used(old_memory_data),
                _process_mem_percent(new_memory_data) - _process_mem_percent(old_memory_data),
                _process_rss(new_memory_data) - _process_rss(old_memory_data),
                _process_vms(new_memory_data) - _process_vms(old_memory_data),
            )
933152
def _log(self, level, msg, args, exc_info=None, extra=None):
        """"""
        if exc_info and not isinstance(exc_info, tuple):
            exc_info = sys.exc_info()

        self.append(HistoryRecord(level, "unknown filename", 0, msg, args, exc_info, func="unknown func"))
511585
def print_code(vm, out=sys.stdout, ops_per_line=8, registers=True):
    """"""
    if registers:
        out.write("IP: %d\n" % vm.instruction_pointer)
        out.write("DS: %s\n" % str(vm.stack))
        out.write("RS: %s\n" % str(vm.return_stack))

    def to_str(op):
        if is_embedded_push(op):
            op = get_embedded_push_value(op)

        if isstring(op):
            return '"%s"' % repr(op)[1:-1]
        elif callable(op):
            return vm.lookup(op)
        else:
            return str(op)

    for addr, op in enumerate(vm.code):
        if (addr % ops_per_line) == 0 and (addr==0 or (addr+1) < len(vm.code)):
            if addr > 0:
                out.write("\n")
            out.write("%0*d  " % (max(4, len(str(len(vm.code)))), addr))
        out.write("%s " % to_str(op))
    out.write("\n")
320038
def info(self):
        
        if self.results.casefold() != 'pandas':
            print("The info method only works with Pandas results")
            return None
        info_code = """
        data work._statsInfo ;
            do rows=0 by 1 while( not last ) ;
                set {0}.{1}{2} end=last;
                array chrs _character_ ;
                array nums _numeric_ ;
                array ccounts(999) _temporary_ ;
                array ncounts(999) _temporary_ ;
                do over chrs;
                    ccounts(_i_) + missing(chrs) ;
                end;
                do over nums;
                    ncounts(_i_) + missing(nums);
                end;   
            end ;
            length Variable $32 type $8. ;
            Do over chrs;
                Type = 'char';
                Variable = vname(chrs) ;
                N = rows;
                Nmiss = ccounts(_i_) ;
                Output ;
            end ;
            Do over nums;
                Type = 'numeric';
                Variable = vname(nums) ;
                N = rows;
                Nmiss = ncounts(_i_) ;
                if variable ^= 'rows' then output;
            end ;
            stop;
            keep Variable N NMISS Type ;
        run;
        """
        if self.sas.nosub:
            print(info_code.format(self.libref, self.table, self._dsopts()))
            return None
        info_pd = self._returnPD(info_code.format(self.libref, self.table, self._dsopts()), '_statsInfo')
        info_pd = info_pd.iloc[:, :]
        info_pd.index.name = None
        info_pd.name = None
        return info_pd
1182101
def log_last_error(fname, context=None):
    """"""
    fd = open(fname, 'a')
    log_time(fd)
    if context:
        print("Context", file=fd)
        print("-------", file=fd)
        print("", file=fd)
        if PY2:
            print(u' '.join(context).encode('utf-8').strip(), file=fd)
        else:
            print(context, file=fd)
        print("", file=fd)
        print("Traceback", file=fd)
        print("---------", file=fd)
        print("", file=fd)
    traceback.print_exc(file=fd)
    print("", file=fd)
    print("", file=fd)
692809
def show(self):
        
        self.show_info()
        self.show_notes()
        if self.code:
            self.each_params_by_register(self.code.get_registers_size(), self.get_descriptor())
            self.code.show()
824235
def logged(level=logging.DEBUG):
    
    def wrap(f):
        _logger = logging.getLogger("{}.{}".format(f.__module__, f.__name__))

        def wrapped_f(*args, **kwargs):
            _logger.log(level, "Called at {} with args = {} and kwargs = {}"
                        .format(datetime.datetime.now(), args, kwargs))
            data = f(*args, **kwargs)
            _logger.log(level, "Done at {} with args = {} and kwargs = {}"
                        .format(datetime.datetime.now(), args, kwargs))
            return data

        return wrapped_f
    return wrap
1305480
def magic_help(self, line):
        
        line = line.strip()
        if not line:
            for magic in self.magics:
                stream_content = {'name': 'stdout', 'text': "%{}\n".format(magic)}
                self.send_response(self.iopub_socket, 'stream', stream_content)
        elif line in self.magics:
            # its a magic
            stream_content = {'name': 'stdout', 'text': "{}\n{}".format(line, self.magics[line].__doc__)}
            self.send_response(self.iopub_socket, 'stream', stream_content)
        elif line in self.interpreter.ops:
            # it's an instruction
            stream_content = {'name': 'stdout', 'text': "{}\n{}".format(line, self.interpreter.ops[line].__doc__)}
            self.send_response(self.iopub_socket, 'stream', stream_content)
        else:
            stream_content = {'name': 'stderr', 'text': "'{}' not a known magic or instruction".format(line)}
            self.send_response(self.iopub_socket, 'stream', stream_content)
927489
def global_backtrace_report
      if Kernel.respond_to?(:caller_for_all_threads)
        all_thread_stacks = caller_for_all_threads
      elsif Thread.respond_to?(:list) && Thread.public_method_defined?(:backtrace)
        all_thread_stacks = {}
        Thread.list.each do |thread|
          all_thread_stacks[thread] = thread.backtrace
        end
      end

      output = "========== Process #{Process.pid}: backtrace dump ==========\n"
      if all_thread_stacks
        all_thread_stacks.each_pair do |thread, stack|
          if thread_name = thread[:name]
            thread_name = "(#{thread_name})"
          end
          stack ||= ["(empty)"]
          output << ("-" * 60) << "\n"
          output << "# Thread: #{thread.inspect}#{thread_name}, "
          if thread == Thread.main
            output << "[main thread], "
          end
          if thread == Thread.current
            output << "[current thread], "
          end
          output << "alive = #{thread.alive?}\n"
          output << ("-" * 60) << "\n"
          output << "    " << stack.join("\n    ")
          output << "\n\n"
        end
      else
        output << ("-" * 60) << "\n"
        output << "# Current thread: #{Thread.current.inspect}\n"
        output << ("-" * 60) << "\n"
        output << "    " << caller.join("\n    ")
      end
      return output
    end
394148
def kernel_restarted_message(self, msg):
        """"""
        if not self.is_error_shown:
            # If there are kernel creation errors, jupyter_client will
            # try to restart the kernel and qtconsole prints a
            # message about it.
            # So we read the kernel's stderr_file and display its
            # contents in the client instead of the usual message shown
            # by qtconsole.
            try:
                stderr = self._read_stderr()
            except Exception:
                stderr = None
            if stderr:
                self.show_kernel_error('<tt>%s</tt>' % stderr)
        else:
            self.shellwidget._append_html("<br>%s<hr><br>" % msg,
                                          before_prompt=False)
347403
def results(context, history_log):
    """"""
    if context.obj is None:
        context.obj = {}
    context.obj['history_log'] = history_log

    if context.invoked_subcommand is None:
        context.invoke(show, item=1)
808933
def proc_exit(self, details):
        
        log = self._params.get('log', self._discard)
        pid = self._key
        exit_code = details
        why = statusfmt(exit_code)
        proc = None
        for p in self._parent._proc_state:
            if pid == p.pid:
                proc = p
        if proc is None:
            log.error("Legion reported exit of unknown pid %s for task %r which %s", pid, self._name, why)
            return

        now = time.time()
        proc.pid = None
        proc.exit_code = exit_code
        proc.exited = now
        proc.pending_sig = None
        proc.next_sig = None
        self._parent._last_status = exit_code
        extant = len(self._parent.get_pids())
        if extant == 0:
            self._parent._started = None
            self._parent._stopping = None
            self._parent._stopped = now
            self._parent.onexit()
        else:
            log.debug("Task '%s' still has %d process%s running", self._name, extant, ses(extant, 'es'))
        if exit_code and not self._parent._terminated:
            log.warning("Task '%s' pid %d %s -- unexpected error exit", self._name, pid, why)
        else:
            log.info("Task '%s' pid %d %s", self._name, pid, why)
776085
def memory_dump():
    
    import cPickle
    dump = open("memory.pickle", 'w')
    for obj in gc.get_objects():
        i = id(obj)
        size = sys.getsizeof(obj, 0)
        #    referrers = [id(o) for o in gc.get_referrers(obj) if hasattr(o, '__class__')]
        referents = [id(o) for o in gc.get_referents(obj) if hasattr(o, '__class__')]
        if hasattr(obj, '__class__'):
            cls = str(obj.__class__)
            cPickle.dump({'id': i, 'class': cls, 'size': size, 'referents': referents}, dump)
446123
def _memory_data(self, log_prefix):
        
        machine_data = psutil.virtual_memory()

        process = psutil.Process()
        process_data = {
            'memory_info': process.get_memory_info(),
            'ext_memory_info': process.get_ext_memory_info(),
            'memory_percent': process.get_memory_percent(),
            'cpu_percent': process.get_cpu_percent(),
        }

        log.info(u"%s Machine memory usage: %s; Process memory usage: %s", log_prefix, machine_data, process_data)
        return {
            'machine_data': machine_data,
            'process_data': process_data,
        }