Esempio n. 1
0
    def get_single(self, resource, query_string, params):
        """
        return a single object for the requested resource.
        :params: resource - name of the resource to obtain i.e experiments, projects, models, etc.
        :params: query_string - graphql string to invoke.
        :params: params - variables required to invoke query string in the client.
        """
        query = gql(query_string)
        class_name = lambda class_name: getattr(sys.modules[__name__],
                                                class_name)
        singular = self._client.execute(query, params)[resource][0]
        class_to_init = class_name(resource.title())
        hash_key = extract_id(singular['pk'])
        if resource == "project":
            hash_key = extract_id(singular['sk'])

        if not resource == "job":
            singular_object = class_to_init(self._client, singular,
                                            self._user_id, hash_key)
            return singular_object

        # job object class is slighty different in design
        singular_object = class_to_init(self._upload_client, singular,
                                        hash_key)
        return singular_object
Esempio n. 2
0
    def get_collection(self, resource, query_string, params):
        """
        return a collection of objects for the requested resource.
        :params: resource - name of the resource to obtain i.e experiments, projects, models, etc.
        :params: query_string - graphql string to invoke.
        :params: params - variables required to invoke query string in the client.
        """
        query = gql(query_string)
        singular_resource = lambda resource_name: str(resource_name.title()[
            0:-1])  # format resource name to match one of the existing classes
        class_name = lambda class_name: getattr(sys.modules[
            __name__], class_name)  # convert string to class name
        collection = self._client.execute(query, params)[resource]
        class_to_init = class_name(singular_resource(resource))

        collection_objects = []
        if not resource == "jobs":
            collection_objects = [
                class_to_init(self._client, item, self._user_id,
                              extract_id(item['sk'])) for item in collection
            ]
            return collection_objects
        # jobs resource
        collection_objects = [
            class_to_init(self._upload_client, item, self._user_id,
                          extract_id(item['pk'])) for item in collection
        ]
        return collection_objects
Esempio n. 3
0
 def set_project_id(self):
     """
     return the project associated with the experiment id
     depending if the experiment comes from the get_collection query
     or a get_single query the experiment id can be fromn the
     sort key or primary key
     """
     pk = extract_id(self._attr['pk'])
     sk = extract_id(self._attr['sk'])
     self._project_id = pk
     if pk == self._id:
         self._project_id = sk
     return
Esempio n. 4
0
 def experiments(self):
     """
     retreive experiments that belong to a project
     :params: project_id - a uuid
     """
     query = gql(EXPERIMENTS_QUERY_FRAGMENT)
     params = {
         "id": str(self._id),
     }
     experiments_query = self._client.execute(query, params)['experiments']
     project_experiments = [
         Experiment(self._client, item, self._user_id,
                    extract_id(item['sk'])) for item in experiments_query
     ]
     return project_experiments