<commit_before># -*- coding: utf-8 -*-

from pyquery import PyQuery
from riot.virtual_dom import define_tag, mount
from riot.observable import Observable
import re
import types

def fn(self, opts):
    cmd = ('''
def init(self, opts):
    self.items = opts.items

def edit(self, e):
    self.text = e.target.value

def add(self, e):
    if self.text:
        self.items.append(dict(title=self.text, done=False, hidden=False))
        self.text = self.input.value = ''

def remove_all_done(self, e):
    self.items = [item for item in self.items if not item.get('done')]

def not_hidden_items(self):
    return [item for item in self.items if not item.get('hidden')]

def toggle(self, e):
    e.item['done'] = not e.item.get('done')
    return True
    ''')
    exec cmd
    fnnames = re.findall(r'^def ([a-zA-Z0-9_]+)', cmd, re.MULTILINE)
    localfns = locals()
    fns = [localfns[fnname] for fnname in fnnames]
    for fnname in fnnames:
        attribute = types.MethodType(localfns[fnname], self, Observable)
        setattr(self, fnname, attribute)
    if hasattr(self, 'init'):
        self.init(opts)


tag = {
    'name': 'todo',
    'html': '''<todo>
  <text>{ opts.title }</text>
  <listbox>
    <each list="{ filter(not_hidden, self.items) }">
      <checkbox label="{ title }" state="{ completed }" on_state_change="{ parent.toggle }" />
    </each>
  </listbox>
  <edit name="input" />
  <button label="Add #{ len(filter(what_show, self.items) + 1 }" />
  <button label="X{ len(filter(only_done, self.items) }" on_press="{ remove_all_done }" />
  <script>
    def init(self, opts):
        self.items = opts.items

    def edit(self, e):
        self.text = e.target.value

    def add(self, e):
        if self.text:
            self.items.append(dict(title=self.text, done=False, hidden=False))
            self.text = self.input.value = ''

    def remove_all_done(self, e):
        self.items = [item for item in self.items if not item.get('done')]

    def not_hidden_items(self):
        return [item for item in self.items if not item.get('hidden')]

    def toggle(self, e):
        e.item['done'] = not e.item.get('done')
        return True
  </script>
</todo>''',
    'fn': fn,
}

define_tag(**tag)

root = PyQuery('<filler><todo></todo></filler>')

doms = mount(root, 'todo', 'todo', {
    'title': 'I want to behave!',
    'items': [
        {'title': 'Avoid excessive coffeine', 'done': True, 'hidden': False},
        {'title': 'Hidden item', 'done': True, 'hidden': True},
        {'title': 'Be less provocative', 'done': False, 'hidden': False},
        {'title': 'Be nice to people', 'done': False, 'hidden': False}
    ]
})
print vars(doms[0])<commit_msg>fix todo example.<commit_after># -*- coding: utf-8 -*-

from riot.app import quit_app, run_tag
from riot.tags.style import parse_style
from riot.tags.tags import parse_tag_from_node
from riot.tags.utils import convert_string_to_node
from riot.virtual_dom import define_tag, mount

todo = define_tag('todo', '''<todo>
  <pile>
    <text>{ title }</text>
    <edit name="input" />
    <button label="Add #{ next_count() }" onclick="{ add }" />
    <button label="X" onclick="{ remove_all_done }" />
    <button label="Exit" onclick="{ exit }">
  </pile>
  <script>
    def init(self, opts):
        self.items = opts['items']
        self.title = opts['title']

    def edit(self, e):
        self.text = e.target.value

    def add(self, e):
        if self.text:
            self.items.append(dict(title=self.text, done=False, hidden=False))
            self.text = self.input.value = ''

    def remove_all_done(self, e):
        self.items = [item for item in self.items if not item.get('done')]

    def not_hidden_items(self):
        return [item for item in self.items if not item.get('hidden')]

    def count(self):
        return len(self.not_hidden_items())

    def next_count(self):
        return self.count() + 1

    def toggle(self, e):
        e.item['done'] = not e.item.get('done')
        return True
  </script>
</todo>''')


root = convert_string_to_node('<filler><todo /></filler>')

mount(root, 'todo', 'todo', {
    'title': 'I want to behave!',
    'items': [
        {'title': 'Avoid excessive coffeine', 'done': True, 'hidden': False},
        {'title': 'Hidden item', 'done': True, 'hidden': True},
        {'title': 'Be less provocative', 'done': False, 'hidden': False},
        {'title': 'Be nice to people', 'done': False, 'hidden': False}
    ]
})

app = parse_tag_from_node(root)

run_tag(app)
{'fraction_non_alphanumeric': 0.1716120460584588, 'fraction_numerical': 0.001550044286979628, 'mean_word_length': 3.0366398570151922, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, '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': '196504', 'n_tokens_mistral': 1569, 'n_tokens_neox': 1526, 'n_words': 404}