Example #1
0
def source_code(s, globs, system='sage'):
    r"""
    Format an object's source code to process and display in the
    Sage notebook.

    INPUT:

    - ``s`` - a string; a name of an object

    - ``globs`` - a string:object dictionary; a context in which to
      evaluate ``s``

    - ``system`` - a string (default: 'sage'); the system to which to
      confine the search

    OUTPUT:

    - a string containing the object's file, starting line number, and
      source code

    AUTHORS:

    - William Stein: partly taken from IPython for use in Sage

    - Nick Alexander: extensions
    """
    if system not in ['sage', 'python']:
        s = system + '.' + s

    try:
        obj = eval(s, globs)
    except NameError:
        return html_markup("No object %s"%s)

    try:
        try:
            return html_markup(obj._sage_src_())
        except:
            pass
        newline = "\n\n"  # blank line to start new paragraph
        indent = "    "   # indent source code to mark it as a code block

        filename = sageinspect.sage_getfile(obj)
        try:
            lines, lineno = sageinspect.sage_getsourcelines(obj)
        except IOError as msg:
            return html_markup(str(msg))
        src = indent.join(lines)
        src = indent + format_src(src)
        if not lineno is None:
            output = "**File:** %s"%filename
            output += newline
            output += "**Source Code** (starting at line %s)::"%lineno
            output += newline
            output += src
        return html_markup(output)

    except (TypeError, IndexError):
        return html_markup("Source code for {} is not available.".format(s) +
                           "\nUse {}? to see the documentation.".format(s))
Example #2
0
def source_code(s, globs, system='sage'):
    r"""
    Format obj's source code for printing in Sage notebook.
    
    AUTHORS:

    - William Stein: partly taken from IPython for use in Sage

    - Nick Alexander: extensions
    """
    if system not in ['sage', 'python']:
        s = system + '.' + s

    try:
        obj = eval(s, globs)
    except NameError:
        return "No object %s"%s
    
    try:
        try:
            return obj._sage_src_()
        except:
            pass
        filename = sageinspect.sage_getfile(obj)
        lines, lineno = sageinspect.sage_getsourcelines(obj, is_binary=False)
        src = ''.join(lines)
        src = sagedoc.format_src(src)
        if not lineno is None:
            src = "File: %s\nSource Code (starting at line %s):\n%s"%(filename, lineno, src)
        return src
    
    except (TypeError, IndexError), msg:
        print msg
        return "Source code for %s not available."%obj
Example #3
0
 def getsource(obj, is_binary):
     # modified from sage.misc.sagedoc.my_getsource
     from sage.misc.sagedoc import sageinspect, format_src
     try:
         s = sageinspect.sage_getsource(obj, is_binary)
         return format_src(str(s))
     except Exception as e:
         print('Error getting source: %s' % e.message)
Example #4
0
 def getsource(obj, is_binary):
     # modified from sage.misc.sagedoc.my_getsource
     from sage.misc.sagedoc import sageinspect, format_src
     try:
         s = sageinspect.sage_getsource(obj, is_binary)
         return format_src(str(s))
     except Exception, msg:
         print 'Error getting source:', msg
         return None
Example #5
0
        def getsource(obj, is_binary):
            # modified from sage.misc.sagedoc.my_getsource
            from sage.misc.sagedoc import sageinspect, format_src

            try:
                s = sageinspect.sage_getsource(obj, is_binary)
                return format_src(str(s))
            except Exception, msg:
                print "Error getting source:", msg
                return None