<commit_before>import simplejson as json
import requests


class ErrorRetrievingBambooData(IOError):
    pass


class ErrorParsingBambooData(ValueError):
    pass


class PyBamboo(object):
    BAMBOO_URL = 'http://bamboo.io'
    OK_STATUS_CODES = (200, 201, 202)

    def get_url(self):
        return self.BAMBOO_URL

    def get_dataset_url(self, dataset_id):
        data = {'bamboo_url': self.get_url(),
                'dataset': dataset_id}
        return u'%(bamboo_url)s/datasets/%(dataset)s' % data

    def get_dataset_summary_url(self, dataset_id):
        data = {'dataset_url': self.get_dataset_url(dataset_id),
                'select': 'all'}
        return u'%(dataset_url)s/summary?select=%(select)s' % data

    def get_dataset_info_url(self, dataset_id):
        data = {'dataset_url': self.get_dataset_url(dataset_id)}
        return u'%(dataset_url)s/info' % data

    def get_dataset_calculations_url(self, dataset_id):
        data = {'bamboo_url': self.get_url(),
                'dataset': dataset_id}
        return u'%(bamboo_url)s/calculations/%(dataset)s' % data

    def count_submissions(self, dataset_id, field, method='count'):
        """
        Number of submissions for a given field.

        For measure fields method is one of:
            '25%', '50%', '75%', 'count' (default), 'max', 'mean', 'min', 'std'
        """

        url = self.get_dataset_summary_url(dataset_id)
        req = requests.get(url)
        self._check_response(req, (200, 202))

        response = self._safe_json_loads(req)

        value = response.get(field).get('summary')
        if method in value:
            return float(value.get(method))
        else:
            return sum([int(relval) for relval in value.values()])

    def query(self, dataset_id, select=None, query=None, group=None,
              as_summary=False, first=False, last=False):
        params = {
            'select': select,
            'query': query,
            'group': group
        }

        # remove key with no value
        for key, value in params.items():
            if not value:
                params.pop(key)
            else:
                if isinstance(value, dict):
                    params[key] = json.dumps(value)

        if as_summary:
            url = self.get_dataset_summary_url(dataset_id)
        else:
            url = self.get_dataset_url(dataset_id)

        req = requests.get(url, params=params)

        self._check_response(req, (200, 202))

        response = self._safe_json_loads(req)

        if last:
            return response[-1]
        elif first:
            return response[0]
        else:
            return response

    def store_calculation(self, dataset_id, formula_name, formula):
        url = self.get_dataset_calculations_url(dataset_id)

        data = {'name': formula_name,
                  'formula': formula}

        req = requests.post(url, data=data)

        self._check_response(req)

        return self._safe_json_loads(req)

    def store_csv_file(self, csv_file_str):<commit_msg>made bamboo URL settable<commit_after>import simplejson as json
import requests


class ErrorRetrievingBambooData(IOError):
    pass


class ErrorParsingBambooData(ValueError):
    pass


DEFAULT_BAMBOO_URL = 'http://bamboo.io'


class PyBamboo(object):
    
    OK_STATUS_CODES = (200, 201, 202)

    def __init__(self, bamboo_url=DEFAULT_BAMBOO_URL):
        self._bamboo_url = bamboo_url

    def get_url(self):
        return self._bamboo_url

    def get_dataset_url(self, dataset_id):
        data = {'bamboo_url': self.get_url(),
                'dataset': dataset_id}
        return u'%(bamboo_url)s/datasets/%(dataset)s' % data

    def get_dataset_summary_url(self, dataset_id):
        data = {'dataset_url': self.get_dataset_url(dataset_id),
                'select': 'all'}
        return u'%(dataset_url)s/summary?select=%(select)s' % data

    def get_dataset_info_url(self, dataset_id):
        data = {'dataset_url': self.get_dataset_url(dataset_id)}
        return u'%(dataset_url)s/info' % data

    def get_dataset_calculations_url(self, dataset_id):
        data = {'bamboo_url': self.get_url(),
                'dataset': dataset_id}
        return u'%(bamboo_url)s/calculations/%(dataset)s' % data

    def count_submissions(self, dataset_id, field, method='count'):
        """
        Number of submissions for a given field.

        For measure fields method is one of:
            '25%', '50%', '75%', 'count' (default), 'max', 'mean', 'min', 'std'
        """

        url = self.get_dataset_summary_url(dataset_id)
        req = requests.get(url)
        self._check_response(req, (200, 202))

        response = self._safe_json_loads(req)

        value = response.get(field).get('summary')
        if method in value:
            return float(value.get(method))
        else:
            return sum([int(relval) for relval in value.values()])

    def query(self, dataset_id, select=None, query=None, group=None,
              as_summary=False, first=False, last=False):
        params = {
            'select': select,
            'query': query,
            'group': group
        }

        # remove key with no value
        for key, value in params.items():
            if not value:
                params.pop(key)
            else:
                if isinstance(value, dict):
                    params[key] = json.dumps(value)

        if as_summary:
            url = self.get_dataset_summary_url(dataset_id)
        else:
            url = self.get_dataset_url(dataset_id)

        req = requests.get(url, params=params)

        self._check_response(req, (200, 202))

        response = self._safe_json_loads(req)

        if last:
            return response[-1]
        elif first:
            return response[0]
        else:
            return response

    def store_calculation(self, dataset_id, formula_name, formula):
        url = self.get_dataset_calculations_url(dataset_id)

        data = {'name': formula_name,
                'formula': formula}

        req = requests.post(url, data=data)

        self._check_response(req)

        return self._safe_json_loads(req)

    def store_csv_file(self, csv_file_str):
{'fraction_non_alphanumeric': 0.11225309137626466, 'fraction_numerical': 0.009314276537658584, 'mean_word_length': 2.307488050982475, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6571853', 'n_tokens_mistral': 2017, 'n_tokens_neox': 1879, 'n_words': 409}