code: def do_pickle_ontology(filename, g=None):
      
      ONTOSPY_LOCAL_MODELS = get_home_location()
      get_or_create_home_repo()  # ensure all the right folders are there
      pickledpath = os.path.join(ONTOSPY_LOCAL_CACHE, filename + ".pickle")
      # pickledpath = ONTOSPY_LOCAL_CACHE + "/" + filename + ".pickle"
      if not g:
          g = Ontospy(os.path.join(ONTOSPY_LOCAL_MODELS, filename))
          # g = Ontospy(ONTOSPY_LOCAL_MODELS + "/" + filename)
  
      if not GLOBAL_DISABLE_CACHE:
          try:
              cPickle.dump(g, open(pickledpath, "wb"))
              printDebug(".. cached <%s>" % filename, "green")
          except Exception as e:
              print(Style.DIM + "\n.. Failed caching <%s>" % filename +
                    Style.RESET_ALL)
              print(str(e) + "\n")
              print(
                  Style.DIM +
                  "... attempting to increase the recursion limit from %d to %d" %
                  (sys.getrecursionlimit(), sys.getrecursionlimit() * 10) +
                  Style.RESET_ALL)
  
              try:
                  sys.setrecursionlimit(sys.getrecursionlimit() * 10)
                  cPickle.dump(g, open(pickledpath, "wb"))
                  printDebug(".. cached <%s>" % filename, "green")
              except Exception as e:
                  printDebug(
                      "\n... Failed caching <%s>... Aborting caching operation..."
                      % filename, "error")
                  print(str(e) + "\n")
              sys.setrecursionlimit(int(sys.getrecursionlimit() / 10))
      return g
docs: from a valid filename, generate the graph instance and pickle it too
      note: option to pass a pre-generated graph instance too
      2015-09-17: added code to increase recursion limit if cPickle fails
          see http://stackoverflow.com/questions/2134706/hitting-maximum-recursion-depth-using-pythons-pickle-cpickle