Exemplo n.º 1
0
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
Exemplo n.º 2
0
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')
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
    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
Exemplo n.º 10
0
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
Exemplo n.º 11
0
 def _remove_env(self, instance_id):
     try:
         del self.envs[instance_id]
     except KeyError:
         raise InvalidUsage('Instance_id {} unknown'.format(instance_id))
Exemplo n.º 12
0
 def _lookup_env(self, instance_id):
     try:
         return self.envs[instance_id]
     except KeyError:
         raise InvalidUsage('Instance_id {} unknown'.format(instance_id))
Exemplo n.º 13
0
def get_article(slug):
    article = Article.query.filter_by(slug=slug).first()
    if not article:
        raise InvalidUsage.article_not_found()
    return article
Exemplo n.º 14
0
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()
Exemplo n.º 15
0
def get_profile(username):
    user = User.query.filter_by(username=username).first()
    if not user:
        raise InvalidUsage.user_not_found()
    return user.profile