<commit_before># -*- coding: utf-8 -*-
from FoodScan.items import *
import wunderpy2
from pysimplelog import Logger


class WuList:
    def __init__(self, client_id, token, list_id):
        self.logger = Logger('Wunderlist')
        self.client = wunderpy2.WunderApi().get_client(token, client_id)
        self.list_id = list_id

    def create_web_hook(self, url, port):
        hooks = self.client.get_webhooks(self.list_id)
        for hook in hooks:
            self.client.delete_webhook(hook['id'], 0)

        self.client.create_webhook(self.list_id, url + ":" + str(port), "generic")

    def item_from_task(self, task, with_selects=True, unmark=False):
        notes = self.client.get_task_notes(task['id'])
        if len(notes) > 0:
            notes = notes[0]['content']
        else:
            notes = u""

        sub_tasks = []
        if with_selects:
            sub_tasks = self.client.get_task_subtasks(task['id'])

        item = Item.parse(task['title'], notes, sub_tasks)
        self.split_items(item, sub_tasks, unmark)
        return item

    def create_item(self, item):
        task = self.client.create_task(self.list_id, title=item.title())
        self.client.create_note(task['id'], item.note())
        for shop_item in item.shop_items:
            self.client.create_subtask(task['id'], unicode(shop_item), completed=shop_item.selected)

    def split_items(self, item, sub_tasks, unmark):
        selected_shop_items = []
        for shop_item in item.shop_items:
            if shop_item.selected:
                selected_shop_items.append(shop_item)

        if len(selected_shop_items) > 0:
            while len(selected_shop_items) > 1:
                shop_item = selected_shop_items.pop()
                new_item = Item(name=shop_item.name, price=shop_item.price, url=shop_item.link)
                new_item.select_shop_item(shop_item)
                self.create_item(new_item)
                shop_item.selected = False
                if unmark:
                    for sub in sub_tasks:
                        if shop_item == ShopItem.parse(sub['title']):
                            self.client.update_subtask(sub['id'], sub['revision'], completed=False)
            item.select_shop_item(selected_shop_items.pop())<commit_msg>more checks before splitting<commit_after># -*- coding: utf-8 -*-
from FoodScan.items import *
import wunderpy2
from pysimplelog import Logger


class WuList:
    def __init__(self, client_id, token, list_id):
        self.logger = Logger('Wunderlist')
        self.client = wunderpy2.WunderApi().get_client(token, client_id)
        self.list_id = list_id

    def create_web_hook(self, url, port):
        hooks = self.client.get_webhooks(self.list_id)
        for hook in hooks:
            self.client.delete_webhook(hook['id'], 0)

        self.client.create_webhook(self.list_id, url + ":" + str(port), "generic")

    def item_from_task(self, task, with_selects=True, unmark=False):
        notes = self.client.get_task_notes(task['id'])
        if len(notes) > 0:
            notes = notes[0]['content']
        else:
            notes = u""

        sub_tasks = []
        if with_selects:
            sub_tasks = self.client.get_task_subtasks(task['id'])

        item = Item.parse(task['title'], notes, sub_tasks)
        self.split_items(item, task['id'], sub_tasks, unmark)
        return item

    def create_item(self, item):
        task = self.client.create_task(self.list_id, title=item.title())
        self.client.create_note(task['id'], item.note())
        for shop_item in item.shop_items:
            self.client.create_subtask(task['id'], unicode(shop_item), completed=shop_item.selected)

    def split_items(self, item, iid, sub_tasks, unmark):
        task = self.client.get_task(iid)
        if task['completed']:
            self.logger.info("task got completed before split: " + item.name.encode('utf-8'))
            return

        selected_shop_items = []
        for shop_item in item.shop_items:
            if shop_item.selected:
                selected_shop_items.append(shop_item)

        if len(selected_shop_items) > 0:
            completed = True
            for t in sub_tasks:
                completed = completed and t['completed']
            if completed:
                self.logger.info("all subtasks completed before split: " + item.name.encode('utf-8'))
                return

            while 1 < len(selected_shop_items):
                shop_item = selected_shop_items.pop()
                self.logger.info("splitting of: " + shop_item.name.encode('utf-8'))
                new_item = Item(name=shop_item.name, price=shop_item.price, url=shop_item.link)
                new_item.select_shop_item(shop_item)
                self.create_item(new_item)
                shop_item.selected = False
                if unmark:
                    for sub in sub_tasks:
                        if shop_item == ShopItem.parse(sub['title']):
                            self.client.update_subtask(sub['id'], sub['revision'], completed=False)
            item.select_shop_item(selected_shop_items.pop())
{'fraction_non_alphanumeric': 0.11392405063291139, 'fraction_numerical': 0.003700097370983447, 'mean_word_length': 2.2986512524084777, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 8, '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': '77916', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1528, 'n_words': 293}