<commit_before>import logging
from collection import *
from zotero import getKey, responseIsError, updateObjectsFromWriteResponse


class Collections(object):
    def __init__(self):
        self.orderedArray = []
        self.collectionObjects = {}
        self.dirty = False
        self.loaded = False
        self.owningLibrary = None
        self.collectionsVersion = 0

    def __len__(self):
        return len(self.collectionObjects)

    def __getitem__(self, collectionKey):
        if collectionKey in self.collectionObjects:
            return self.collectionObjects[collectionKey]
        else:
            raise KeyError

    def __setitem__(self, collectionKey, item):
        self.collectionObjects[collectionKey] = item

    def __delitem__(self, collectionKey):
        del self.collectionObjects[collectionKey]

    def __iter__(self):
        return self.collectionObjects.__iter__()

    def __reversed__(self):
        ritems = Items()
        ritems.collectionObjects = self.collectionObjects.reversed()
        return ritems

    def __contains__(self, collectionKey):
        return collectionKey in self.collectionObjects

    def addCollection(self, collection):
        self.collectionObjects[collection.collectionKey] = collection
        self.orderedArray.append(collection)

    def getCollection(self, collectionKey):
        if collectionKey in self.collectionObjects:
            return self.collectionObjects[collectionKey]
        else:
            return False

    def addCollectionsFromFeed(self, feed):
        addedCollections = []
        for entry in feed.entries:
            collection = Collection(entry)
            self.addCollection(collection)
            addedCollections.append(collection)

        return addedCollections

    def writeCollection(self, collection):
        cols = self.writeCollections([collection])
        if cols == False:
            return False
        return cols[0]

    def writeCollections(self, collections):
        collectionsArray = []
        for collection in collections:
            collectionKey = collection.get('collectionKey')
            if collectionKey == '' or collectionKey == None:
                newCollectionKey = getKey()
                collection.set('collectionKey', newCollectionKey)
                collection.set('collectionVersion', 0)
            collectionsArray.append(collection.writeApiObject())

        aparams = {'target': 'collections', 'content': 'json'}
        reqUrl = self.owningLibrary.apiRequestString(aparams)

        chunks = [collectionsArray[i:i + 50] for i in range(0, len(collectionsArray), 50)]
        for chunk in chunks:
            writeArray = []
            for collection in chunk:
                writeArray.append(collection.writeApiObject())
            requestData = json.dumps({'collections': writeArray})

            writeResponse = self.owningLibrary._request(reqUrl, 'POST', requestData, {'Content-Type': 'application/json'})
            if responseIsError(writeResponse):
                logging.info('writeCollections Error')
                logging.info(writeResponse.status_code)
                #entire request failed but we get no per-item write failure messages
                #so update all items with writeFailure manually
                for collection in chunk:
                    collection.writeFailure = {'code': writeResponse.status_code, 'message': writeResponse.text}
            else:<commit_msg>fix createcollection<commit_after>import logging
from collection import *
from zotero import getKey, responseIsError, updateObjectsFromWriteResponse


class Collections(object):
    def __init__(self):
        self.orderedArray = []
        self.collectionObjects = {}
        self.dirty = False
        self.loaded = False
        self.owningLibrary = None
        self.collectionsVersion = 0

    def __len__(self):
        return len(self.collectionObjects)

    def __getitem__(self, collectionKey):
        if collectionKey in self.collectionObjects:
            return self.collectionObjects[collectionKey]
        else:
            raise KeyError

    def __setitem__(self, collectionKey, item):
        self.collectionObjects[collectionKey] = item

    def __delitem__(self, collectionKey):
        del self.collectionObjects[collectionKey]

    def __iter__(self):
        return self.collectionObjects.__iter__()

    def __reversed__(self):
        ritems = Items()
        ritems.collectionObjects = self.collectionObjects.reversed()
        return ritems

    def __contains__(self, collectionKey):
        return collectionKey in self.collectionObjects

    def addCollection(self, collection):
        self.collectionObjects[collection.collectionKey] = collection
        self.orderedArray.append(collection)

    def getCollection(self, collectionKey):
        if collectionKey in self.collectionObjects:
            return self.collectionObjects[collectionKey]
        else:
            return False

    def addCollectionsFromFeed(self, feed):
        addedCollections = []
        for entry in feed.entries:
            collection = Collection(entry)
            self.addCollection(collection)
            addedCollections.append(collection)

        return addedCollections

    def writeCollection(self, collection):
        cols = self.writeCollections([collection])
        if cols == False:
            return False
        return cols[0]

    def writeCollections(self, collections):
        collectionsArray = []
        for collection in collections:
            collectionKey = collection.get('collectionKey')
            if collectionKey == '' or collectionKey == None:
                newCollectionKey = getKey()
                collection.set('collectionKey', newCollectionKey)
                collection.set('collectionVersion', 0)
            collectionsArray.append(collection)

        aparams = {'target': 'collections', 'content': 'json'}
        reqUrl = self.owningLibrary.apiRequestString(aparams)

        chunks = [collectionsArray[i:i + 50] for i in range(0, len(collectionsArray), 50)]
        for chunk in chunks:
            writeArray = []
            for collection in chunk:
                writeArray.append(collection.writeApiObject())
            requestData = json.dumps({'collections': writeArray})

            writeResponse = self.owningLibrary._request(reqUrl, 'POST', requestData, {'Content-Type': 'application/json'})
            if responseIsError(writeResponse):
                logging.info('writeCollections Error')
                logging.info(writeResponse.status_code)
                #entire request failed but we get no per-item write failure messages
                #so update all items with writeFailure manually
                for collection in chunk:
                    collection.writeFailure = {'code': writeResponse.status_code, 'message': writeResponse.text}
            else:
{'fraction_non_alphanumeric': 0.08135838150289017, 'fraction_numerical': 0.0023121387283236996, 'mean_word_length': 2.5293217746047936, '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': '797052', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1604, 'n_words': 410}