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))
def _instancedoc_(self): """ Provide documentation for the wrapped function. TESTS:: sage: from sage.misc.sageinspect import sage_getdoc sage: from sage.sets.set_from_iterator import Decorator sage: d = Decorator() sage: d.f = Integer.is_prime sage: print(sage_getdoc(d)) # indirect doctest Test whether "self" is prime. ... Calls the PARI "isprime" function. """ from sage.misc.sageinspect import sage_getsourcelines, sage_getfile, _extract_embedded_position f = self.f doc = f.__doc__ or '' if _extract_embedded_position(doc) is None: try: sourcelines = sage_getsourcelines(f) from sage.env import SAGE_LIB, SAGE_SRC filename = sage_getfile(f) # The following is a heuristics to get # the file name of the cached function # or method if filename.startswith(SAGE_SRC): filename = filename[len(SAGE_SRC):] elif filename.startswith(SAGE_LIB): filename = filename[len(SAGE_LIB):] file_info = "File: %s (starting at line %d)\n"%(filename,sourcelines[1]) doc = file_info+doc except IOError: pass return doc
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
def _sage_doc_(self): """ Provide documentation for the wrapped function. TESTS:: sage: from sage.misc.sageinspect import sage_getdoc sage: from sage.sets.set_from_iterator import Decorator sage: d = Decorator() sage: d.f = Integer.is_prime sage: print sage_getdoc(d) # indirect doctest Test whether "self" is prime. ... Calls the PARI "isprime" function. """ from sage.misc.sageinspect import sage_getsourcelines, sage_getfile, _extract_embedded_position f = self.f doc = f.__doc__ or '' if _extract_embedded_position(doc) is None: try: sourcelines = sage_getsourcelines(f) from sage.env import SAGE_LIB, SAGE_SRC filename = sage_getfile(f) # The following is a heuristics to get # the file name of the cached function # or method if filename.startswith(SAGE_SRC): filename = filename[len(SAGE_SRC):] elif filename.startswith(SAGE_LIB): filename = filename[len(SAGE_LIB):] file_info = "File: %s (starting at line %d)\n"%(filename,sourcelines[1]) doc = file_info+doc except IOError: pass return doc
def docstring(obj_name, globs, system='sage'): r""" Format ``obj_name``'s docstring 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']: obj_name = system + '.' + obj_name try: obj = eval(obj_name, globs) except (AttributeError, NameError, SyntaxError): return "No object '%s' currently defined."%obj_name s = '' try: filename = sageinspect.sage_getfile(obj) #i = filename.find('site-packages/sage/') #if i == -1: s += 'File: %s\n'%filename #else: # file = filename[i+len('site-packages/sage/'):] # s += 'File: <html><a href="src_browser?%s">%s</a></html>\n'%(file,file) except TypeError: pass s += 'Type: %s\n'%type(obj) s += 'Definition: %s\n'%sageinspect.sage_getdef(obj, obj_name) s += 'Docstring: \n%s\n'%sageinspect.sage_getdoc(obj, obj_name) return s.rstrip()
def file_and_line(obj): r""" Look up source file and line number of obj. If the file lies in the Sage library, the path name of the corresponding file in the current branch (i.e., the file that gets copied into the Sage library upon running 'sage -br'). Note that the first line of a file is considered to be 1 rather than 0 because most editors think that this is the case. AUTHORS: - Nils Bruin (2007-10-03) - Simon King (2011-05): Use :mod:`~sage.misc.sageinspect` to get the file and the line. EXAMPLES:: sage: import sage.misc.edit_module as edit_module sage: edit_module.file_and_line(sage) ('...sage/__init__.py', 0) The following tests against a bug that was fixed in trac ticket #11298:: sage: edit_module.file_and_line(x) ('...sage/symbolic/expression.pyx', ...) """ # d = inspect.getdoc(obj) # ret = sage.misc.sageinspect._extract_embedded_position(d); # if ret is not None: # (_, filename, lineno) = ret # else: # filename = inspect.getsourcefile(obj) # _,lineno = inspect.findsource(obj) # # for sage files, the registered source file is the result of the preparsing # these files end in ".py" and have "*autogenerated*" on the second line # for those files, we replace the extension by ".sage" and we subtract # 3 from the line number to compensate for the 3 lines that were prefixed # in the preparsing process # from sage.misc.sageinspect import sage_getfile, sage_getsourcelines filename = sage_getfile(obj) lineno = sage_getsourcelines(obj)[1] - 1 if filename.endswith(".py"): infile = open(filename, "r") infile.readline() if infile.readline().find("*autogenerated*") >= 0: filename = filename[:-3] + ".sage" lineno = lineno - 3 sageroot = sage.misc.sageinspect.SAGE_ROOT + "/" runpathpattern = "^" + sageroot + "local/lib/python[^/]*/site-packages" develbranch = sageroot + "devel/sage" filename = re.sub(runpathpattern, develbranch, filename) return filename, lineno + 1
def file_and_line(obj): r""" Look up source file and line number of obj. If the file lies in the Sage library, the path name of the corresponding file in the current branch (i.e., the file that gets copied into the Sage library upon running 'sage -br'). Note that the first line of a file is considered to be 1 rather than 0 because most editors think that this is the case. AUTHORS: - Nils Bruin (2007-10-03) - Simon King (2011-05): Use :mod:`~sage.misc.sageinspect` to get the file and the line. EXAMPLES:: sage: import sage.misc.edit_module as edit_module sage: edit_module.file_and_line(sage) ('...sage/__init__.py', 0) The following tests against a bug that was fixed in trac ticket #11298:: sage: edit_module.file_and_line(x) ('...sage/symbolic/expression.pyx', ...) """ #d = inspect.getdoc(obj) #ret = sage.misc.sageinspect._extract_embedded_position(d); #if ret is not None: # (_, filename, lineno) = ret #else: # filename = inspect.getsourcefile(obj) # _,lineno = inspect.findsource(obj) # # for sage files, the registered source file is the result of the preparsing # these files end in ".py" and have "*autogenerated*" on the second line # for those files, we replace the extension by ".sage" and we subtract # 3 from the line number to compensate for the 3 lines that were prefixed # in the preparsing process # from sage.misc.sageinspect import sage_getfile, sage_getsourcelines filename = sage_getfile(obj) lineno = sage_getsourcelines(obj)[1] - 1 if filename.endswith('.py'): infile=open(filename,'r') infile.readline() if infile.readline().find("*autogenerated*") >= 0: filename=filename[:-3]+'.sage' lineno = lineno-3 sageroot = sage.misc.sageinspect.SAGE_ROOT+'/' runpathpattern = '^'+sageroot+'local/lib/python[^/]*/site-packages' develbranch = sageroot+'devel/sage' filename=re.sub(runpathpattern,develbranch,filename) return filename, lineno+1
def docstring(obj_name, globs, system='sage'): r""" Format an object's docstring to process and display in the Sage notebook. INPUT: - ``obj_name`` - a string; a name of an object - ``globs`` - a string:object dictionary; a context in which to evaluate ``obj_name`` - ``system`` - a string (default: 'sage'); the system to which to confine the search OUTPUT: - a string containing the object's file, type, definition, and docstring or a message stating the object is not defined AUTHORS: - William Stein: partly taken from IPython for use in Sage - Nick Alexander: extensions """ if system not in ['sage', 'python']: obj_name = system + '.' + obj_name try: obj = eval(obj_name, globs) except (AttributeError, NameError, SyntaxError): return "No object '%s' currently defined."%obj_name s = '' newline = "\n\n" # blank line to start new paragraph try: filename = sageinspect.sage_getfile(obj) #i = filename.find('site-packages/sage/') #if i == -1: s += '**File:** %s'%filename s += newline #else: # file = filename[i+len('site-packages/sage/'):] # s += 'File: <html><a href="src_browser?%s">%s</a></html>\n'%(file,file) except TypeError: pass s += '**Type:** %s'%type(obj) s += newline s += '**Definition:** %s'%sageinspect.sage_getdef(obj, obj_name) s += newline s += '**Docstring:**' s += newline s += sageinspect.sage_getdoc(obj, obj_name) return s.rstrip()
def _sage_doc_(self): """ Provide documentation for the wrapped function. TESTS:: sage: from sage.misc.sageinspect import sage_getdoc sage: from sage.sets.set_from_iterator import Decorator sage: d = Decorator() sage: d.f = Integer.is_prime sage: print sage_getdoc(d) # indirect doctest Returns "True" if "self" is prime. ... IMPLEMENTATION: Calls the PARI "isprime" function. <BLANKLINE> """ from sage.misc.sageinspect import sage_getsourcelines, sage_getfile f = self.f if hasattr(f, "func_doc"): try: sourcelines = sage_getsourcelines(f) except IOError: sourcelines = None if sourcelines is not None: from sage.env import SAGE_LIB, SAGE_SRC filename = sage_getfile(f) # The following is a heuristics to get # the file name of the cached function # or method if filename.startswith(SAGE_SRC): filename = filename[len(SAGE_SRC):] elif filename.startswith(SAGE_LIB): filename = filename[len(SAGE_LIB):] file_info = "File: %s (starting at line %d)\n" % ( filename, sourcelines[1]) doc = file_info + (f.func_doc or '') else: doc = f.func_doc else: doc = f.__doc__ return doc
def _sage_doc_(self): """ Provide documentation for the wrapped function. TESTS:: sage: from sage.misc.sageinspect import sage_getdoc sage: from sage.sets.set_from_iterator import Decorator sage: d = Decorator() sage: d.f = Integer.is_prime sage: print sage_getdoc(d) # indirect doctest Returns "True" if "self" is prime. ... IMPLEMENTATION: Calls the PARI "isprime" function. <BLANKLINE> """ from sage.misc.sageinspect import sage_getsourcelines, sage_getfile f = self.f if hasattr(f, "func_doc"): try: sourcelines = sage_getsourcelines(f) except IOError: sourcelines = None if sourcelines is not None: from sage.env import SAGE_LIB, SAGE_SRC filename = sage_getfile(f) # The following is a heuristics to get # the file name of the cached function # or method if filename.startswith(SAGE_SRC): filename = filename[len(SAGE_SRC):] elif filename.startswith(SAGE_LIB): filename = filename[len(SAGE_LIB):] file_info = "File: %s (starting at line %d)\n"%(filename,sourcelines[1]) doc = file_info+(f.func_doc or '') else: doc = f.func_doc else: doc = f.__doc__ return doc