<commit_before>import os, json

from PySide.QtCore import QAbstractListModel, QModelIndex, Qt
from PySide.QtGui import QIcon

class ImageModel(QAbstractListModel):
    TagRole = 0x101
    
    def __init__(self, parent=None, troll_dir=None):
        QAbstractListModel.__init__(self, parent)

        # load data from disk
        default = os.path.expanduser(os.path.join("~", ".trollbox"))
        self.troll_dir = troll_dir if troll_dir else default
        if not os.path.exists(self.troll_dir):
            os.mkdir(self.troll_dir)
        self.metadata_path = os.path.join(self.troll_dir, "metadata.json")
        if os.path.exists(self.metadata_path):
            metadata = json.load(open(self.metadata_path, "rt"))
        else:
            metadata = []

        self.images = []
        for url, tags, local_path in metadata:
            self.images.append((url, tags, local_path, QIcon(local_path)))

    def image_dir(self):
        return os.path.join(self.troll_dir, "images")

    def image_path(self, filename):
        return os.path.join(self.image_dir(), filename)

    def rowCount(self, parent_index=0):
        return len(self.images[parent_index:])

    def addImage(self, url, tags, local_path):
        '''
        Adds an image with url and tags that has already been downloaded
        to local_path to the model.
        '''
        icon = QIcon(local_path)
        self.images.append((url, tags, local_path, icon))
        self.save()
<commit_msg>fixed a bug in rowCount: i was assuming parent_index was an int when it was actually a QModelIndex instance<commit_after>import os, json

from PySide.QtCore import QAbstractListModel, QModelIndex, Qt
from PySide.QtGui import QIcon

class ImageModel(QAbstractListModel):
    TagRole = 0x101
    
    def __init__(self, parent=None, troll_dir=None):
        QAbstractListModel.__init__(self, parent)

        # load data from disk
        default = os.path.expanduser(os.path.join("~", ".trollbox"))
        self.troll_dir = troll_dir if troll_dir else default
        if not os.path.exists(self.troll_dir):
            os.mkdir(self.troll_dir)
        self.metadata_path = os.path.join(self.troll_dir, "metadata.json")
        if os.path.exists(self.metadata_path):
            metadata = json.load(open(self.metadata_path, "rt"))
        else:
            metadata = []

        self.images = []
        for url, tags, local_path in metadata:
            self.images.append((url, tags, local_path, QIcon(local_path)))

    def image_dir(self):
        return os.path.join(self.troll_dir, "images")

    def image_path(self, filename):
        return os.path.join(self.image_dir(), filename)

    def rowCount(self, parent_index=None):
        if parent_index != None:
            i = parent_index.row()
        else:
            i = 0
        return len(self.images[i:])

    def addImage(self, url, tags, local_path):
        '''
        Adds an image with url and tags that has already been downloaded
        to local_path to the model.
        '''
        icon = QIcon(local_path)
        self.images.append((url, tags, local_path, icon))
        self.save()

{'fraction_non_alphanumeric': 0.1070063694267516, 'fraction_numerical': 0.0031847133757961785, 'mean_word_length': 2.589714285714286, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1038800', 'n_tokens_mistral': 982, 'n_tokens_neox': 952, 'n_words': 238}