<commit_before># -*- coding: utf-8 -*-
"""
python client for influxdb
"""
import json

from six.moves.urllib.parse import urlencode

import requests


class InfluxDBClient(object):
    """
    InfluxDB Client
    """

    def __init__(self, host, port, username, password, database):
        """
        Initialize client
        """
        self._host = host
        self._port = port
        self._username = username
        self._password = password
        self._database = database
        self._baseurl = "http://{0}:{1}".format(self._host, self._port)

        self._headers = {
            'Content-type': 'application/json',
            'Accept': 'text/plain'}

    # Change member variables

    def switch_db(self, database):
        """
        Change client database

        Parameters
        ----------
        database : string
        """
        self._database = database

    def switch_user(self, username, password):
        """
        Change client username

        Parameters
        ----------
        username : string
        password : string
        """
        self._username = username
        self._password = password

    # Writing Data
    #
    # Assuming you have a database named foo_production you can write data
    # by doing a POST to /db/foo_production/series?u=some_user&p=some_password
    # with a JSON body of points.

    def write_points(self, data):
        """
        Write to multiple time series names
        """
        response = requests.post(
            "{0}/db/{1}/series?u={2}&p={3}".format(
                self._baseurl,
                self._database,
                self._username,
                self._password),
            data=json.dumps(data),
            headers=self._headers)

        if response.status_code == 200:
            return True
        else:
            raise Exception(
                "{0}: {1}".format(response.status_code, response.content))

    def write_points_with_precision(self, data, time_precision='s'):
        """
        Write to multiple time series names
        """
        if time_precision not in ['s', 'm', 'u']:
            raise Exception(
                "Invalid time precision is given. (use 's','m' or 'u')")

        url_format = "{0}/db/{1}/series?u={2}&p={3}&time_precision={4}"

        response = requests.post(url_format.format(
            self._baseurl,
            self._database,
            self._username,
            self._password,
            time_precision),
            data=json.dumps(data),
            headers=self._headers)

        if response.status_code == 200:
            return True
        else:
            raise Exception(
                "{0}: {1}".format(response.status_code, response.content))

    # One Time Deletes

    def delete_points(self, name,
                      regex=None, start_epoch=None, end_epoch=None):
        """
        TODO: Delete a range of data

        2013-11-08: This endpoint has not been implemented yet in ver0.0.8,
        but it is documented in http://influxdb.org/docs/api/http.html.
        See also: src/api/http/api.go:l57
        """
        raise NotImplementedError()

    # Regularly Scheduled Deletes

    def create_scheduled_delete(self, json_body):
        """
        TODO: Create scheduled delete

        2013-11-08: This endpoint has not been implemented yet in ver0.0.8,
        but it is documented in http://influxdb.org/docs/api/http.html.
        See also: src/api/http/api.go:l57
        """
        raise NotImplementedError()

    # get list of deletes
    # curl http://localhost:8086/db/site_dev/scheduled_deletes
    #
    # remove a regularly scheduled delete
    # curl -X DELETE http://localhost:8086/db/site_dev/scheduled_deletes/:id

    def get_list_scheduled_delete(self):
        """
        TODO: Get list of scheduled deletes

        2013-11-08: This endpoint has not been implemented yet in ver0.0.8,
        but it is documented in http://influxdb.org/docs/api/http.html.
        See also: src/api/http/api.go:l57
        """
        raise NotImplementedError()

    def remove_scheduled_delete(self, delete_id):
        """
        TODO: Remove scheduled delete

        2013-11-08: This endpoint has not been implemented yet in ver0.0.8,
        but it is documented in http://influxdb.org/docs/api/http.html.
        See also: src/api/http/api.go:l57
        """
        raise NotImplementedError()

    # Querying Data
    #
    # GET db/:name/series. It takes five parameters
    def query(self, query, time_precision='s', chunked=False):
        """
        Quering data
        """
        if time_precision not in ['s', 'm', 'u']:
            raise Exception(
                "Invalid time precision is given. (use 's','m' or 'u')")

        if chunked is True:
            chunked_param = 'true'
        else:
            chunked_param = 'false'

        encoded_query = urlencode({
            'q': query})

        url_format = "{0}/db/{1}/series?{2}&u={3}&p={4}"
        url_format += "&time_precision={5}&chunked={6}"

        response = requests.get(url_format.format(
            self._baseurl,
            self._database,
            encoded_query,
            self._username,
            self._password,
            time_precision,
            chunked_param))

        if response.status_code == 200:
            return response.json()
        else:
            raise Exception(
                "{0}: {1}".format(response.status_code, response.content))

    # Creating and Dropping Databases
    #
    # ### create a database
    # curl -X POST http://localhost:8086/db -d '{"name": "site_development"}'
    #
    # ### drop a database
    # curl -X DELETE http://localhost:8086/db/site_development<commit_msg>fix the test on py33. Seems like .json() is generating byte array, fall back to json.loads()<commit_after># -*- coding: utf-8 -*-
"""
python client for influxdb
"""
import json

from six.moves.urllib.parse import urlencode

import requests


class InfluxDBClient(object):
    """
    InfluxDB Client
    """

    def __init__(self, host, port, username, password, database):
        """
        Initialize client
        """
        self._host = host
        self._port = port
        self._username = username
        self._password = password
        self._database = database
        self._baseurl = "http://{0}:{1}".format(self._host, self._port)

        self._headers = {
            'Content-type': 'application/json',
            'Accept': 'text/plain'}

    # Change member variables

    def switch_db(self, database):
        """
        Change client database

        Parameters
        ----------
        database : string
        """
        self._database = database

    def switch_user(self, username, password):
        """
        Change client username

        Parameters
        ----------
        username : string
        password : string
        """
        self._username = username
        self._password = password

    # Writing Data
    #
    # Assuming you have a database named foo_production you can write data
    # by doing a POST to /db/foo_production/series?u=some_user&p=some_password
    # with a JSON body of points.

    def write_points(self, data):
        """
        Write to multiple time series names
        """
        response = requests.post(
            "{0}/db/{1}/series?u={2}&p={3}".format(
                self._baseurl,
                self._database,
                self._username,
                self._password),
            data=json.dumps(data),
            headers=self._headers)

        if response.status_code == 200:
            return True
        else:
            raise Exception(
                "{0}: {1}".format(response.status_code, response.content))

    def write_points_with_precision(self, data, time_precision='s'):
        """
        Write to multiple time series names
        """
        if time_precision not in ['s', 'm', 'u']:
            raise Exception(
                "Invalid time precision is given. (use 's','m' or 'u')")

        url_format = "{0}/db/{1}/series?u={2}&p={3}&time_precision={4}"

        response = requests.post(url_format.format(
            self._baseurl,
            self._database,
            self._username,
            self._password,
            time_precision),
            data=json.dumps(data),
            headers=self._headers)

        if response.status_code == 200:
            return True
        else:
            raise Exception(
                "{0}: {1}".format(response.status_code, response.content))

    # One Time Deletes

    def delete_points(self, name,
                      regex=None, start_epoch=None, end_epoch=None):
        """
        TODO: Delete a range of data

        2013-11-08: This endpoint has not been implemented yet in ver0.0.8,
        but it is documented in http://influxdb.org/docs/api/http.html.
        See also: src/api/http/api.go:l57
        """
        raise NotImplementedError()

    # Regularly Scheduled Deletes

    def create_scheduled_delete(self, json_body):
        """
        TODO: Create scheduled delete

        2013-11-08: This endpoint has not been implemented yet in ver0.0.8,
        but it is documented in http://influxdb.org/docs/api/http.html.
        See also: src/api/http/api.go:l57
        """
        raise NotImplementedError()

    # get list of deletes
    # curl http://localhost:8086/db/site_dev/scheduled_deletes
    #
    # remove a regularly scheduled delete
    # curl -X DELETE http://localhost:8086/db/site_dev/scheduled_deletes/:id

    def get_list_scheduled_delete(self):
        """
        TODO: Get list of scheduled deletes

        2013-11-08: This endpoint has not been implemented yet in ver0.0.8,
        but it is documented in http://influxdb.org/docs/api/http.html.
        See also: src/api/http/api.go:l57
        """
        raise NotImplementedError()

    def remove_scheduled_delete(self, delete_id):
        """
        TODO: Remove scheduled delete

        2013-11-08: This endpoint has not been implemented yet in ver0.0.8,
        but it is documented in http://influxdb.org/docs/api/http.html.
        See also: src/api/http/api.go:l57
        """
        raise NotImplementedError()

    # Querying Data
    #
    # GET db/:name/series. It takes five parameters
    def query(self, query, time_precision='s', chunked=False):
        """
        Quering data
        """
        if time_precision not in ['s', 'm', 'u']:
            raise Exception(
                "Invalid time precision is given. (use 's','m' or 'u')")

        if chunked is True:
            chunked_param = 'true'
        else:
            chunked_param = 'false'

        encoded_query = urlencode({
            'q': query})

        url_format = "{0}/db/{1}/series?{2}&u={3}&p={4}"
        url_format += "&time_precision={5}&chunked={6}"

        response = requests.get(url_format.format(
            self._baseurl,
            self._database,
            encoded_query,
            self._username,
            self._password,
            time_precision,
            chunked_param))

        if response.status_code == 200:
            return json.loads(response.content)
        else:
            raise Exception(
                "{0}: {1}".format(response.status_code, response.content))

    # Creating and Dropping Databases
    #
    # ### create a database
    # curl -X POST http://localhost:8086/db -d '{"name": "site_development"}'
    #
    # ### drop a database
    # curl -X DELETE http://localhost:8086/db/site_development
{'fraction_non_alphanumeric': 0.11539124439848328, 'fraction_numerical': 0.017752499138228198, 'mean_word_length': 1.8597831444061115, 'pattern_counts': {'":': 2, '<': 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': '6392950', 'n_tokens_mistral': 3418, 'n_tokens_neox': 3233, 'n_words': 834}