def get_required_param(json, param): if json is None: logger.info("Request is not a valid json") raise InvalidUsage("Request is not a valid json") value = json.get(param, None) if (value is None) or (value == '') or (value == []): logger.info("A required request parameter '{}' had value {}".format( param, value)) raise InvalidUsage( "A required request parameter '{}' was not provided".format(param)) return value
def upload(): """ Upload the results of training (as automatically recorded by your env's monitor) to OpenAI Gym. Parameters: - training_dir: A directory containing the results of a training run. - api_key: Your OpenAI API key - algorithm_id (default=None): An arbitrary string indicating the paricular version of the algorithm (including choices of parameters) you are running. """ j = request.get_json() training_dir = get_required_param(j, 'training_dir') api_key = get_required_param(j, 'api_key') algorithm_id = get_optional_param(j, 'algorithm_id', None) try: gym.upload(training_dir, algorithm_id, writeup=None, api_key=api_key, ignore_open_monitors=False) return ('', 204) except gym.error.AuthenticationError: raise InvalidUsage('You must provide an OpenAI Gym API key')
def make_comment_on_article(slug, body, **kwargs): article = Article.query.filter_by(slug=slug).first() if not article: raise InvalidUsage.article_not_found() comment = Comment(article, current_identity.profile, body, **kwargs) comment.save() return comment
def unfollow_user(username): user = User.query.filter_by(username=username).first() if not user: raise InvalidUsage.user_not_found() current_identity.profile.unfollow(user.profile) current_identity.profile.save() return user.profile
def update_article(slug, **kwargs): article = Article.query.filter_by( slug=slug, author_id=current_identity.profile.id).first() if not article: raise InvalidUsage.article_not_found() article.update(updatedAt=dt.datetime.utcnow, **kwargs) article.save() return article
def unfavorite_an_article(slug): profile = current_identity.profile article = Article.query.filter_by(slug=slug).first() if not article: raise InvalidUsage.article_not_found() article.unfavourite(profile) article.save() return article
def register_user(username, password, email, **kwargs): try: userprofile = UserProfile( User(username, email, password=password, **kwargs).save()).save() except IntegrityError: db.session.rollback() raise InvalidUsage.user_already_registered() return userprofile.user
def delete_comment_on_article(slug, cid): article = Article.query.filter_by(slug=slug).first() if not article: raise InvalidUsage.article_not_found() comment = article.comments.filter_by( id=cid, author=current_identity.profile).first() comment.delete() return '', 200
def create(self, env_id): try: env = gym.make(env_id) except gym.error.Error: raise InvalidUsage( "Attempted to look up malformed environment ID '{}'".format( env_id)) instance_id = str(uuid.uuid4().hex)[:self.id_len] self.envs[instance_id] = env return instance_id
def get_optional_param(json, param, default): if json is None: logger.info("Request is not a valid json") raise InvalidUsage("Request is not a valid json") value = json.get(param, None) if (value is None) or (value == '') or (value == []): logger.info( "An optional request parameter '{}' had value {} and was replaced with default value {}" .format(param, value, default)) value = default return value
def _remove_env(self, instance_id): try: del self.envs[instance_id] except KeyError: raise InvalidUsage('Instance_id {} unknown'.format(instance_id))
def _lookup_env(self, instance_id): try: return self.envs[instance_id] except KeyError: raise InvalidUsage('Instance_id {} unknown'.format(instance_id))
def get_article(slug): article = Article.query.filter_by(slug=slug).first() if not article: raise InvalidUsage.article_not_found() return article
def login_user(email, password, **kwargs): user = User.query.filter_by(email=email).first() if user is not None and user.check_password(password): return user else: raise InvalidUsage.user_not_found()
def get_profile(username): user = User.query.filter_by(username=username).first() if not user: raise InvalidUsage.user_not_found() return user.profile