<commit_before>"""
api.py

Python library to ease communication with the TinEye API server.

Copyright (c) 2012 Idee Inc. All rights reserved worldwide.
"""

from datetime import datetime
import httplib
import simplejson
import time

from api_request import APIRequest
from exceptions import TinEyeAPIError

import urllib3

class TinEyeResponse(object):
    """
    Represents a response from the API.

    Attributes:

    - `matches`, a list of Match objects.
    """

    def __init__(self, matches):
        self.matches = matches

    def __repr__(self):
        return '%s(matches="%s")' % \
               (self.__class__.__name__, self.matches)

    @staticmethod
    def _from_dict(result_json):
        """
        Takes parsed JSON from the API server and turns it into a
        TinEyeResponse object.

        - `result_json`, the parsed JSON object.

        Returns: a TinEyeResponse object.
        """

        if not isinstance(result_json, dict):
            raise TinEyeAPIError("500", ["Please pass in a dictionary to _from_dict()"])

        matches = []
        if 'results' in result_json:
            results = result_json['results']
            if 'matches' in results:
                for m in results.get('matches'):
                    match = Match._from_dict(m)
                    matches.append(match)
        return TinEyeResponse(matches)

class Match(object):
    """
    Represents an image match.

    Attributes:

    - `image_url`, link to the result image.
    - `width`, image width in pixels.
    - `height`, image height in pixels.
    - `size`, image area in pixels.
    - `format`, image format.
    - `filesize`, image size in bytes.
    - `overlay`, overlay URL.
    - `backlinks`, a list of Backlink objects pointing to
      the original websites and image URLs.
    - `contributor`, whether this is a TinEye contributor image.
    """
<commit_msg>Add total_results value in TinEyeResponse class.<commit_after>"""
api.py

Python library to ease communication with the TinEye API server.

Copyright (c) 2012 Idee Inc. All rights reserved worldwide.
"""

from datetime import datetime
import httplib
import simplejson
import time

from api_request import APIRequest
from exceptions import TinEyeAPIError

import urllib3

class TinEyeResponse(object):
    """
    Represents a response from the API.

    Attributes:

    - `matches`, a list of Match objects.
    """

    def __init__(self, matches, total_results=None):
        self.matches = matches
        self.total_results = total_results or 0

    def __repr__(self):
        return '%s(matches="%s")' % \
               (self.__class__.__name__, self.matches)

    @staticmethod
    def _from_dict(result_json):
        """
        Takes parsed JSON from the API server and turns it into a
        TinEyeResponse object.

        - `result_json`, the parsed JSON object.

        Returns: a TinEyeResponse object.
        """

        if not isinstance(result_json, dict):
            raise TinEyeAPIError("500", ["Please pass in a dictionary to _from_dict()"])

        matches = []
        if 'results' in result_json:
            results = result_json['results']
            if 'matches' in results:
                for m in results.get('matches'):
                    match = Match._from_dict(m)
                    matches.append(match)
        total_results = results.get('total_results')
        return TinEyeResponse(matches, total_results)

class Match(object):
    """
    Represents an image match.

    Attributes:

    - `image_url`, link to the result image.
    - `width`, image width in pixels.
    - `height`, image height in pixels.
    - `size`, image area in pixels.
    - `format`, image format.
    - `filesize`, image size in bytes.
    - `overlay`, overlay URL.
    - `backlinks`, a list of Backlink objects pointing to
      the original websites and image URLs.
    - `contributor`, whether this is a TinEye contributor image.
    """

{'fraction_non_alphanumeric': 0.09125283589614318, 'fraction_numerical': 0.004285354171918326, 'mean_word_length': 2.406008583690987, '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': '5582639', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1054, 'n_words': 387}