<commit_before>#!/usr/bin/env python
"""
_database_

API class representing a cloudant database

"""
import json
import contextlib
import posixpath
import urllib

from .document import CloudantDocument
from .views import DesignDocument
from .errors import CloudantException
from .index import python_to_couch, Index
from .changes import Feed


class CouchDatabase(dict):
    """
    _CouchDatabase_


    """
    def __init__(self, cloudant, database_name, fetch_limit=100):
        super(CouchDatabase, self).__init__()
        self._cloudant_account = cloudant
        self._database_host = cloudant._cloudant_url
        self._database_name = database_name
        self._r_session = cloudant._r_session
        self._fetch_limit = fetch_limit
        self.index = Index(self.all_docs)

    @property
    def database_url(self):
        return posixpath.join(
            self._database_host,
            urllib.quote_plus(self._database_name)
        )

    @property
    def creds(self):
        """
        _creds_

        Return a dict of useful strings to use to authenicate against
        this database, using various methods.

        """
        return {
            "basic_auth": self._cloudant_account.basic_auth_str(),
            "user_ctx": self._cloudant_account.session()['userCtx']
        }

    def exists(self):
        resp = self._r_session.get(self.database_url)
        return resp.status_code == 200

    def metadata(self):
        resp = self._r_session.get(self.database_url)
        resp.raise_for_status()
        return resp.json()

    def doc_count(self):
        return self._metadata().get('doc_count')

    def create_document(self, data, throw_on_exists=False):
        doc = CloudantDocument(self, data.get('_id'))
        doc.update(data)
        doc.create()
        super(CouchDatabase, self).__setitem__(doc['_id'], doc)
        return doc

    def new_document(self):
        """
        _new_document_

        Creates new, empty document
        """
        doc = CloudantDocument(self, None)
        doc.create()
        super(CouchDatabase, self).__setitem__(doc['_id'], doc)
        return doc<commit_msg>bugfix<commit_after>#!/usr/bin/env python
"""
_database_

API class representing a cloudant database

"""
import json
import contextlib
import posixpath
import urllib

from .document import CloudantDocument
from .views import DesignDocument
from .errors import CloudantException
from .index import python_to_couch, Index
from .changes import Feed


class CouchDatabase(dict):
    """
    _CouchDatabase_


    """
    def __init__(self, cloudant, database_name, fetch_limit=100):
        super(CouchDatabase, self).__init__()
        self._cloudant_account = cloudant
        self._database_host = cloudant._cloudant_url
        self._database_name = database_name
        self._r_session = cloudant._r_session
        self._fetch_limit = fetch_limit
        self.index = Index(self.all_docs)

    @property
    def database_url(self):
        return posixpath.join(
            self._database_host,
            urllib.quote_plus(self._database_name)
        )

    @property
    def creds(self):
        """
        _creds_

        Return a dict of useful strings to use to authenicate against
        this database, using various methods.

        """
        return {
            "basic_auth": self._cloudant_account.basic_auth_str(),
            "user_ctx": self._cloudant_account.session()['userCtx']
        }

    def exists(self):
        resp = self._r_session.get(self.database_url)
        return resp.status_code == 200

    def metadata(self):
        resp = self._r_session.get(self.database_url)
        resp.raise_for_status()
        return resp.json()

    def doc_count(self):
        return self.metadata().get('doc_count')

    def create_document(self, data, throw_on_exists=False):
        doc = CloudantDocument(self, data.get('_id'))
        doc.update(data)
        doc.create()
        super(CouchDatabase, self).__setitem__(doc['_id'], doc)
        return doc

    def new_document(self):
        """
        _new_document_

        Creates new, empty document
        """
        doc = CloudantDocument(self, None)
        doc.create()
        super(CouchDatabase, self).__setitem__(doc['_id'], doc)
        return doc
{'fraction_non_alphanumeric': 0.09433085501858736, 'fraction_numerical': 0.0027881040892193307, 'mean_word_length': 2.5726141078838176, 'pattern_counts': {'":': 4, '<': 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': '5291395', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1239, 'n_words': 279}