示例#1
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
示例#2
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,
                                        self._user_id, hash_key)
        return singular_object
示例#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