# This is a simple python script to look through all the GAP code
# and documentation in the lib/ folder of this repository, find
# all example code snippets, and extract them all into a single
# Jupyter Notebook.
# The resulting notebook can then be loaded and the user can
# manually run one or more of the code snippets. This is useful
# when generating visualizations that will then be included in the
# original manual, to show the results of the code snippets.
# This script is to be run without parameters. It finds all the
# lib/*.gd files and processes them in alphabetical order by
# filename.
import sys
import json
import os
# Declare some global variables to use as output templates:
# The function that gathers snippets from a file and the global
# variable in which it puts them:
snippets = [ ]
def process_file ( gdfile ):
global snippets
print "Processing " + gdfile + "..."
mode = "not in a snippet"
inf = open( gdfile, "r" )
for line in inf.readlines():
if line.strip() == "#! @BeginLog":
mode = "in a snippet"
snippets += [ [ ] ]
elif line.strip() == "#! @EndLog":
mode = "not in a snippet"
elif mode == "in a snippet":
snippets[len(snippets)-1] += [ line[3:] ]
inf.close()
# The function that writes all the snippets into a file:
outfile = "extracted_snippets.ipynb"
def write_all ():
global snippets, outfile
print "Writing " + outfile + "..."
outf = open( outfile, "w" )
outf.write( header )
outf.write( mdcell % ( json.dumps( [ \ "# Snippets extracted from all files in `lib/*.gd`\n" \
] ), "," ) )
outf.write( codecell % ( json.dumps( [ \ "LoadPackage( \"jupyterviz\" );" ] ), "," ) )
comma = ","
for index, snippet in enumerate( snippets ):
snippet[len(snippet)-1] = snippet[len(snippet)-1].strip()
if index == len( snippets ) - 1:
comma = ""
outf.write( codecell % ( json.dumps( snippet ), comma ) )
outf.write( footer )
outf.close()
# Use the above functions on lib/*.gd
for fname in os.listdir( "lib" ):
if fname[-3:] == ".gd":
process_file( "lib/" + fname )
write_all()
print "Done. You can open the resulting file with this command:"
print "jupyter notebook " + outfile
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.