"""A seat of a kaggle_environments game for a bot in its own process: env.run gets a function that passes each observation to the bot's process and its answer back. A late or broken answer is the game's no-op.""" import json, os, sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(os.path.dirname(HERE), 'kit')) from host import Child # noqa: E402 def seat(folder, limit=1.0, idle=None, start=30): main = os.path.join(folder, 'main.py') if not os.path.isfile(main): sys.exit(f'No bot in {folder}: add main.py with agent(obs, config), as on Kaggle') child = Child([sys.executable, '-s', os.path.join(HERE, 'kaggle_child.py'), main], cwd=folder, limit=limit, start=start) def agent(obs, config): answer = child.ask({'obs': json.loads(json.dumps(obs, default=str)), 'config': json.loads(json.dumps(config, default=str))}) return idle if answer is None else answer agent.child = child return agent