<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright 2016 The Simbiose Ventures Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A library that provides a Python client to Slicing Dice API"""
import ujson

import pyslicer.utils.validators as validators
import pyslicer.exceptions as exceptions

from pyslicer.api import SlicingDiceAPI
from pyslicer.url_resources import URLResources


class SlicingDice(SlicingDiceAPI):
    """A python interface to Slicing Dice API

    Example usage:

        To create an object of the SliceDicer:

            >>> from pyslicer.api import SlicingDice
            >>> sd = SlicingDice('my-token')

        To create a field:

            >>> field_json = {
                    'name': 'Pyslicer String Field',
                    'description': 'Pyslicer example description',
                    'type': 'string',
                    'cardinality': 'low'}
            >>> print sd.create_field(field_json)

        To make a query:

            >>> query_json = {
                    'type': 'count',
                    'select': [
                        {
                            "pyslicer-string-field":
                                {
                                    "equal": "test_value_1"
                                }
                        },
                        "or",
                        {
                            "pyslicer-string-field":
                                {
                                    "equal": "test_value_2"
                                }
                        },
                    ]
                }
            >>> print sd.query(query_json)

        To make a index:

            >>> indexing_json = {
                    '<EMAIL>': {
                        'pyslicer-string-field': 'test_value_1',
                        'pyslicer-integer-field': 42,
                    },
                    '<EMAIL>': {
                        'pyslicer-string-field': 'test_value_2',
                        'pyslicer-integer-field': 42,
                    },
                }
            >>> print sd.index(indexing_json)
    """
    def __init__(
        self, write_key=None, read_key=None, master_key=None,
            custom_key=None, use_ssl=False, timeout=60):
        """Instantiate a new SlicingDice object.

        Keyword arguments:
        key(string or SlicerKey obj) -- Key to access API
        timeout(int) -- Define timeout to request,
            defaults 30 secs(default 30).
        use_ssl(bool) -- Define if the request uses verification SSL for
            HTTPS requests. Defaults False.(Optional)
        """
        super(SlicingDice, self).__init__(
            write_key, read_key, master_key, custom_key, use_ssl, timeout)

    def _count_query_wrapper(self, url, query):
        """Validate count query and make request.

        Keyword arguments:
        url(string) -- Url to make request
        query(dict) -- A count query
        """
        sd_count_query = validators.QueryCountValidator(query)
        if sd_count_query.validator():
            return self._make_request(
                url=url,
                json_data=ujson.dumps(query),
                req_type="post",
                key_level=0)

    def _data_extraction_wrapper(self, url, query):
        """Validate data extraction query and make request.

        Keyword arguments:
        url(string) -- Url to make request
        query(dict) -- A data extraction query
        """
        sd_extraction_result = validators.QueryDataExtractionValidator(query)
        if sd_extraction_result.validator():
            return self._make_request(
                url=url,
                json_data=ujson.dumps(query),
                req_type="post",
                key_level=0)

    def _wrapper_test(self, test):
        base_url = SlicingDice.BASE_URL
        if test:
            base_url += "/test"
        return base_url

    def _saved_query_wrapper(self, url, query, update=False):
        """Validate saved query and make request.

        Keyword arguments:
        url(string) -- Url to make request
        query(dict) -- A saved query
        update(bool) -- Indicates with operation is update a
            saved query or not.(default false)
        """<commit_msg>Change class method _wrapper_test to staticmethod<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright 2016 The Simbiose Ventures Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A library that provides a Python client to Slicing Dice API"""
import ujson

import pyslicer.utils.validators as validators
import pyslicer.exceptions as exceptions

from pyslicer.api import SlicingDiceAPI
from pyslicer.url_resources import URLResources


class SlicingDice(SlicingDiceAPI):
    """A python interface to Slicing Dice API

    Example usage:

        To create an object of the SliceDicer:

            >>> from pyslicer.api import SlicingDice
            >>> sd = SlicingDice('my-token')

        To create a field:

            >>> field_json = {
                    'name': 'Pyslicer String Field',
                    'description': 'Pyslicer example description',
                    'type': 'string',
                    'cardinality': 'low'}
            >>> print sd.create_field(field_json)

        To make a query:

            >>> query_json = {
                    'type': 'count',
                    'select': [
                        {
                            "pyslicer-string-field":
                                {
                                    "equal": "test_value_1"
                                }
                        },
                        "or",
                        {
                            "pyslicer-string-field":
                                {
                                    "equal": "test_value_2"
                                }
                        },
                    ]
                }
            >>> print sd.query(query_json)

        To make a index:

            >>> indexing_json = {
                    '<EMAIL>': {
                        'pyslicer-string-field': 'test_value_1',
                        'pyslicer-integer-field': 42,
                    },
                    '<EMAIL>': {
                        'pyslicer-string-field': 'test_value_2',
                        'pyslicer-integer-field': 42,
                    },
                }
            >>> print sd.index(indexing_json)
    """
    def __init__(
        self, write_key=None, read_key=None, master_key=None,
            custom_key=None, use_ssl=False, timeout=60):
        """Instantiate a new SlicingDice object.

        Keyword arguments:
        key(string or SlicerKey obj) -- Key to access API
        timeout(int) -- Define timeout to request,
            defaults 30 secs(default 30).
        use_ssl(bool) -- Define if the request uses verification SSL for
            HTTPS requests. Defaults False.(Optional)
        """
        super(SlicingDice, self).__init__(
            write_key, read_key, master_key, custom_key, use_ssl, timeout)

    def _count_query_wrapper(self, url, query):
        """Validate count query and make request.

        Keyword arguments:
        url(string) -- Url to make request
        query(dict) -- A count query
        """
        sd_count_query = validators.QueryCountValidator(query)
        if sd_count_query.validator():
            return self._make_request(
                url=url,
                json_data=ujson.dumps(query),
                req_type="post",
                key_level=0)

    def _data_extraction_wrapper(self, url, query):
        """Validate data extraction query and make request.

        Keyword arguments:
        url(string) -- Url to make request
        query(dict) -- A data extraction query
        """
        sd_extraction_result = validators.QueryDataExtractionValidator(query)
        if sd_extraction_result.validator():
            return self._make_request(
                url=url,
                json_data=ujson.dumps(query),
                req_type="post",
                key_level=0)

    @staticmethod
    def _wrapper_test(test):
        base_url = SlicingDice.BASE_URL
        if test:
            base_url += "/test"
        return base_url

    def _saved_query_wrapper(self, url, query, update=False):
        """Validate saved query and make request.

        Keyword arguments:
        url(string) -- Url to make request
        query(dict) -- A saved query
        update(bool) -- Indicates with operation is update a
            saved query or not.(default false)
        """
{'fraction_non_alphanumeric': 0.08737864077669903, 'fraction_numerical': 0.00516422226812642, 'mean_word_length': 1.5657127715951245, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5151882', 'n_tokens_mistral': 2611, 'n_tokens_neox': 2480, 'n_words': 719}