code: def main():
      import optparse
      import os
      import sys
  
      reports = ("summary", "popular", "aggregate", "individual")
  
      parser = optparse.OptionParser(
          usage="%prog [options] pythonscript.py [pythonscriptoptions]",
          description="This script runs a Python program that uses APSW "
          "and reports on SQL queries without modifying the program.  This is "
          "done by using connection_hooks and registering row and execution "
          "tracers.  See APSW documentation for more details on the output.",
      )
      parser.add_option(
          "-o",
          "--output",
          dest="output",
          default="stdout",
          help="Where to send the output.  Use a filename, a single dash for stdout, or the words stdout and stderr. [%default]",
      )
      parser.add_option(
          "-s",
          "--sql",
          dest="sql",
          default=False,
          action="store_true",
          help="Log SQL statements as they are executed. [%default]",
      )
      parser.add_option(
          "-r",
          "--rows",
          dest="rows",
          default=False,
          action="store_true",
          help="Log returned rows as they are returned (turns on sql). [%default]",
      )
      parser.add_option(
          "-t",
          "--timestamps",
          dest="timestamps",
          default=False,
          action="store_true",
          help="Include timestamps in logging",
      )
      parser.add_option(
          "-i",
          "--thread",
          dest="thread",
          default=False,
          action="store_true",
          help="Include thread id in logging",
      )
      parser.add_option(
          "-l",
          "--length",
          dest="length",
          default=30,
          type="int",
          help="Max amount of a string to print [%default]",
      )
      parser.add_option(
          "--no-report",
          dest="report",
          default=True,
          action="store_false",
          help="A summary report is normally generated at program exit.  This turns off the report and saves memory.",
      )
      parser.add_option(
          "--report-items",
          dest="reportn",
          metavar="N",
          default=15,
          type="int",
          help="How many items to report in top lists [%default]",
      )
      parser.add_option(
          "--reports",
          dest="reports",
          default=",".join(reports),
          help="Which reports to show [%default]",
      )
  
      parser.disable_interspersed_args()
      options, args = parser.parse_args()
  
      options.reports = [
          x.strip() for x in options.reports.split(",") if x.strip()
      ]
      for r in options.reports:
          if r not in reports:
              parser.error(
                  r
                  + " is not a valid report.  You should supply one or more of "
                  + ", ".join(reports)
              )
  
      if options.rows:
          options.sql = True
  
      if not args:
          parser.error("You must specify a python script to execute")
  
      if not os.path.exists(args[0]):
          parser.error("Unable to find script %r\n" % (args[0],))
  
      sys.argv = args
      sys.path[0] = os.path.split(os.path.abspath(sys.argv[0]))[0]
  
      t = APSWTracer(options)
  
      try:
          t.run()
      finally:
          t.report()
  
imports: import time
  import sys
  import weakref
label_desc: Use image node_id 2 for calling a global function with example usage: main() without return types