def gold_reset(self): ''' Mark all of this job's test questions (gold data) as NOT gold. Splitting the /jobs/:job_id/gold API call into gold_reset() and gold_add() is not faithful to the API, but resetting gold marks and adding them should not have the same API endpoint in the first place. ''' params = dict(reset='true') res = self._connection.request('/jobs/%s/gold' % self.id, method='PUT', params=params) # reset cache self._cache.remove(keyfunc(self, 'properties')) self._cache.remove(keyfunc(self, 'units')) return res
def update(self, props): params = rails_params({'job': props}) logger.debug('Updating Job[%d]: %r', self.id, params) res = self._connection.request('/jobs/%s' % self.id, method='PUT', params=params) # reset cached properties self._cache.remove(keyfunc(self, 'properties')) return res
def upload(self, units): headers = {'Content-Type': 'application/json'} data = '\n'.join(json.dumps(unit) for unit in units) logger.debug('Upload data: {}'.format(data)) res = self._connection.request('/jobs/%s/upload' % self.id, method='POST', headers=headers, data=data) # reset cached units self._cache.remove(keyfunc(self, 'units')) return res
def create_job(self, props): ''' Creates an empty job with given `props` attributes. ''' params = rails_params({'job': props}) job_response = self.request('/jobs', method='POST', params=params) job = Job(job_response['id'], self) job._properties = job_response # bust cache of job_ids self._cache.remove(keyfunc(self, 'job_ids')) return job
def gold_add(self, check, check_with=None): ''' Configure the gold labels for a task. * check: the name of the field being checked against - Can call /jobs/{job_id}/legend to see options - And as far as I can tell, the job.properties['gold'] field is a hash with keys that are "check" names, and values that are "with" names. * check_with: the name of the field containing the gold label for check - Crowdflower calls this field "with", which is a Python keyword - defaults to check + '_gold' I'm not sure why convert_units would be anything but true. ''' params = dict(check=check, convert_units='true') if check_with is not None: params['with'] = check_with res = self._connection.request('/jobs/%s/gold' % self.id, method='PUT', params=params) # reset cache self._cache.remove(keyfunc(self, 'properties')) self._cache.remove(keyfunc(self, 'units')) return res
def upload(self, units): ''' TODO: allow setting Job parameters at the same time ''' headers = {'Content-Type': 'application/json'} # N.b.: CF expects newline-separated JSON, not actual JSON # e.g., this would fail with a status 500: kwargs['data'] = json.dumps(data) data = '\n'.join(json.dumps(unit) for unit in units) job_response = self.request('/jobs/upload', method='POST', headers=headers, data=data) job = Job(job_response['id'], self) job._properties = job_response # bust cache of job_ids self._cache.remove(keyfunc(self, 'job_ids')) return job
def _cache_flush(self, func_attr): self._cache.remove(keyfunc(self, func_attr))
def add_tags(self, tags): params = rails_params({'tags': tags}) self._connection.request('/jobs/%s/tags' % self.id, method='POST', params=params) self._cache.remove(keyfunc(self, 'tags'))
def delete_unit(self, unit_id): response = self._connection.request('/jobs/%s/units/%s' % (self.id, unit_id), method='DELETE') # bust cache if the request did not raise any errors self._cache.remove(keyfunc(self, 'units')) return response