class_docstr: Context manager for tests requiring a live database.
  
  with TempMongo() as foo:
      mj = foo.mongo_jobs('test1')
class_docstr_tok: ["Context","manager","for","tests","requiring","a","live","database",".","with","TempMongo","(",")","as","foo",":","mj","=","foo.mongo_jobs","(","'test1","'",")"]
code: def __enter__(self):
      try:
          open(self.workdir)
          assert 0
      except OSError:
          subprocess.call(["mkdir", "-p", "%s/db" % self.workdir])
          proc_args = [
              "mongod",
              "--dbpath=%s/db" % self.workdir,
              "--noprealloc",
              "--port=22334",
          ]
          print("starting mongod", proc_args)
          self.mongo_proc = subprocess.Popen(
              proc_args,
              stdout=subprocess.PIPE,
              stderr=subprocess.PIPE,
              cwd=self.workdir,  # this prevented mongod assertion fail
          )
          try:
              interval = 0.125
              while interval <= 2:
                  if interval > 0.125:
                      print("Waiting for mongo to come up")
                  time.sleep(interval)
                  interval *= 2
                  if self.db_up():
                      break
              if self.db_up():
                  return self
              else:
                  try:
                      os.kill(self.mongo_proc.pid, signal.SIGTERM)
                  except OSError:
                      pass  # if it crashed there is no such process
                  out, err = self.mongo_proc.communicate()
                  print(out, file=sys.stderr)
                  print(err, file=sys.stderr)
                  raise RuntimeError(
                      "No database connection", proc_args
                  )
          except Exception as e:
              try:
                  os.kill(self.mongo_proc.pid, signal.SIGTERM)
              except OSError:
                  pass  # if it crashed there is no such process
              raise e
  
imports: import pickle
  import os
  import signal
  import subprocess
  import sys
  import traceback
  import threading
  import time
  import unittest
  import numpy
  import nose
  import nose.plugins.skip
  from hyperopt.base import JOB_STATE_DONE, STATUS_OK
  from hyperopt.mongoexp import parse_url
  from hyperopt.mongoexp import MongoTrials
  from hyperopt.mongoexp import MongoWorker
  from hyperopt.mongoexp import ReserveTimeout
  from hyperopt.mongoexp import as_mongo_str
  from hyperopt.mongoexp import main_worker_helper
  from hyperopt.mongoexp import MongoJobs
  from hyperopt.fmin import fmin
  from hyperopt import hp, rand
  import hyperopt.tests.test_base
  from hyperopt.tests.unit.test_domains import gauss_wave2
label_desc: Use image node_id 2 for calling the TempMongo obj's underlying member method code with example usage: obj.__enter__() and returns: self