<commit_before>#!/usr/bin/env python
from kiwi.ui.delegates import ProxyDelegate
from kiwi.ui.objectlist import Column, ObjectList

class DiaryEntry:
    title = ''
    text = ''
    period = 'morning'

    def get_words(self):
        return len(self.text.split())

    def get_chars(self):
        return len(self.text)

class Diary(ProxyDelegate):
    def __init__(self):
        self.entries = ObjectList([Column("title", width=120, sorted=True),
                                   Column("period", width=80),
                                   Column("text", expand=True, visible=False)])
        ProxyDelegate.__init__(self, DiaryEntry(),
                               ['title', 'period', 'text', 'chars',
                                'words'],
                               gladefile="diary2",
                               delete_handler=self.quit_if_last)
        self.hbox.pack_start(self.entries)
        self.entries.show()
        self.entries.grab_focus()
        self.set_editable(False)

    def proxy_updated(self, *args):
        self.entries.update(self.model)

    def on_add__clicked(self, button):
        entry = DiaryEntry()
        entry.title = 'New title'

        self.set_model(entry)
        self.entries.append(entry)
        self.title.grab_focus()
        self.set_editable(True)

    def on_remove__clicked(self, button):
        entry = self.entries.get_selected()
        if entry:
            self.entries.remove(entry)

        if len(self.entries) < 1:
            editable = False
        else:
            editable = True
        self.set_editable(editable)

    def on_text__content_changed(self, *args):
        self.update("chars")
        self.update("words")
        self.entries.update(self.model)

    def on_entries__selection_changed(self, entries, instance):
        if instance:
            self.set_model(instance)

    def set_editable(self, editable):
        self.leftbox.set_sensitive(editable)
        self.remove.set_sensitive(editable)

proxy = Diary()
proxy.show_and_loop()<commit_msg>Use settable, remove class variables, handle selection when removing better<commit_after>#!/usr/bin/env python
from kiwi.python import Settable
from kiwi.ui.delegates import ProxyDelegate
from kiwi.ui.objectlist import Column, ObjectList

class DiaryEntry(Settable):
    def __init__(self):
        Settable.__init__(self, title='Untitled', period='morning', text='')

    def get_words(self):
        return len(self.text.split())

    def get_chars(self):
        return len(self.text)

class Diary(ProxyDelegate):
    def __init__(self):
        self.entries = ObjectList([Column("title", width=120, sorted=True),
                                   Column("period", width=80),
                                   Column("text", expand=True, visible=False)])
        ProxyDelegate.__init__(self, DiaryEntry(),
                               ['title', 'period', 'text', 'chars', 'words'],
                               gladefile="diary2",
                               delete_handler=self.quit_if_last)
        self.hbox.pack_start(self.entries)
        self.entries.show()
        self.entries.grab_focus()
        self.set_editable(False)

    def proxy_updated(self, *args):
        self.entries.update(self.model)

    def on_add__clicked(self, button):
        entry = DiaryEntry()
        self.entries.append(entry)
        self.entries.select(entry)
        self.set_editable(True)
        self.set_model(entry)
        self.title.grab_focus()

    def on_remove__clicked(self, button):
        entry = self.entries.get_selected()
        if entry:
            prev = self.entries.get_previous(entry)
            self.entries.remove(entry)
            if prev != entry:
                self.entries.select(prev)

        if len(self.entries) < 1:
            editable = False
        else:
            editable = True
        self.set_editable(editable)

    def on_text__content_changed(self, *args):
        self.update("chars")
        self.update("words")
        self.entries.update(self.model)

    def on_entries__selection_changed(self, entries, instance):
        if instance:
            self.set_model(instance)

    def set_editable(self, editable):
        self.leftbox.set_sensitive(editable)
        self.remove.set_sensitive(editable)

proxy = Diary()
proxy.show_and_loop()
{'fraction_non_alphanumeric': 0.1103448275862069, 'fraction_numerical': 0.003218390804597701, 'mean_word_length': 2.14378612716763, 'pattern_counts': {'":': 0, '<': 5, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1864284', 'n_tokens_mistral': 1281, 'n_tokens_neox': 1237, 'n_words': 228}