from __future__ import absolute_import, division, print_function, unicode_literals
import os
import unittest
import uuid

import requests_mock

from canvasapi import Canvas
from canvasapi.group import Group, GroupMembership, GroupCategory
from canvasapi.course import Page
from canvasapi.discussion_topic import DiscussionTopic
from canvasapi.exceptions import RequiredFieldMissing
from canvasapi.external_feed import ExternalFeed
from canvasapi.file import File
from canvasapi.folder import Folder
from canvasapi.tab import Tab
from tests import settings
from tests.util import register_uris

@requests_mock.Mocker()
class TestGroup(unittest.TestCase):

    def setUp(self):
        self.canvas = Canvas(settings.BASE_URL, settings.API_KEY)

        with requests_mock.Mocker() as m:
            register_uris({'course': ['get_by_id'], 'group': ['get_by_id']}, m)

            self.course = self.canvas.get_course(1)
            self.group = self.canvas.get_group(1)

    # __str__()
    def test__str__(self, m):
        string = str(self.group)
        self.assertIsInstance(string, str)

    # show_front_page()
    def test_show_front_page(self, m):
        register_uris({'group': ['show_front_page']}, m)

        front_page = self.group.show_front_page()
        self.assertIsInstance(front_page, Page)
        self.assertTrue(hasattr(front_page, 'url'))
        self.assertTrue(hasattr(front_page, 'title'))

    # create_front_page()
    def test_edit_front_page(self, m):
        register_uris({'group': ['edit_front_page']}, m)

        new_front_page = self.group.edit_front_page()
        self.assertIsInstance(new_front_page, Page)
        self.assertTrue(hasattr(new_front_page, 'url'))
        self.assertTrue(hasattr(new_front_page, 'title'))

    # list_pages()
    def test_get_pages(self, m):
        register_uris({'group': ['get_pages', 'get_pages2']}, m)

        pages = self.group.get_pages()
        page_list = [page for page in pages]
        self.assertEqual(len(page_list), 4)
        self.assertIsInstance(page_list[0], Page)
        self.assertTrue(hasattr(page_list[0], 'id'))
        self.assertEqual(page_list[0].group_id, self.group.id)

    # create_page()
    def test_create_page(self, m):
        register_uris({'group': ['create_page']}, m)

        title = 'New Page'
        new_page = self.group.create_page(wiki_page={'title': title})
        self.assertIsInstance(new_page, Page)
        self.assertTrue(hasattr(new_page, 'title'))
        self.assertEqual(new_page.title, title)
        self.assertTrue(hasattr(new_page, 'id'))
        self.assertEqual(new_page.group_id, self.group.id)

    def test_create_page_fail(self, m):
        with self.assertRaises(RequiredFieldMissing):
            self.group.create_page(settings.INVALID_ID)

    # get_page()
    def test_get_page(self, m):
        register_uris({'group': ['get_page']}, m)

        url = 'my-url'
        page = self.group.get_page(url)
        self.assertIsInstance(page, Page)

    # edit()
    def test_edit(self, m):
        register_uris({'group': ['edit']}, m)

        new_title = "New Group"
        response = self.group.edit(description=new_title)
        self.assertIsInstance(response, Group)
        self.assertTrue(hasattr(response, 'description'))
        self.assertEqual(response.description, new_title)

    # delete()
    def test_delete(self, m):
        register_uris({'group': ['delete']}, m)

        group = self.group.delete()
        self.assertIsInstance(group, Group)
        self.assertTrue(hasattr(group, 'name'))
        self.assertTrue(hasattr(group, 'description'))

    # invite()
    def test_invite(self, m):
        register_uris({'group': ['invite']}, m)

        user_list = ["1", "2"]
        response = self.group.invite(user_list)
        gmembership_list = [groupmembership for groupmembership in response]
        self.assertIsInstance(gmembership_list[0], GroupMembership)
        self.assertEqual(len(gmembership_list), 2)

    # list_users()
    def test_list_users(self, m):
        register_uris({'group': ['list_users', 'list_users_p2']}, m)

        from canvasapi.user import User
        users = self.group.list_users()
        user_list = [user for user in users]
        self.assertIsInstance(user_list[0], User)
        self.assertEqual(len(user_list), 4)

    # remove_user()
    def test_remove_user(self, m):
        register_uris({'group': ['remove_user']}, m)

        from canvasapi.user import User
        response = self.group.remove_user(1)
        self.assertIsInstance(response, User)

    # upload()
    def test_upload(self, m):
        register_uris({'group': ['upload', 'upload_final']}, m)

        filename = 'testfile_group_%s' % uuid.uuid4().hex
        with open(filename, 'w+') as file:
            response = self.group.upload(file)
        self.assertTrue(response[0])
        self.assertIsInstance(response[1], dict)
        self.assertIn('url', response[1])
        # http://stackoverflow.com/a/10840586
        # Not as stupid as it looks.
        try:
            os.remove(filename)
        except OSError:
            pass

    # preview_processed_html()
    def test_preview_processed_html(self, m):
        register_uris({'group': ['preview_processed_html']}, m)

        html_str = "
processed html
"
        response = self.group.preview_html(html_str)
        self.assertEqual(response, html_str)

    # get_activity_stream_summary()
    def test_get_activity_stream_summary(self, m):
        register_uris({'group': ['activity_stream_summary']}, m)

        response = self.group.get_activity_stream_summary()
        self.assertEqual(len(response), 2)
        self.assertIn('type', response[0])

    # list_memberships()
    def test_list_memberships(self, m):
        register_uris({'group': ['list_memberships', 'list_memberships_p2']}, m)

        response = self.group.list_memberships()
        membership_list = [membership for membership in response]
        self.assertEqual(len(membership_list), 4)
        self.assertIsInstance(membership_list[0], GroupMembership)
        self.assertTrue(hasattr(membership_list[0], 'id'))

    # get_membership()
    def test_get_membership(self, m):
        register_uris({'group': ['get_membership']}, m)

        response = self.group.get_membership(1, "users")
        self.assertIsInstance(response, GroupMembership)

    # create_membership()
    def test_create_membership(self, m):
        register_uris({'group': ['create_membership']}, m)

        response = self.group.create_membership(1)
        self.assertIsInstance(response, GroupMembership)

    # update_membership()
    def test_update_membership(self, m):
        register_uris({'group': ['update_membership_user']}, m)

        response = self.group.update_membership(1)
        self.assertIsInstance(response, GroupMembership)

    # get_discussion_topic()
    def test_get_discussion_topic(self, m):
        register_uris({'group': ['get_discussion_topic']}, m)

        group_id = 1
        discussion = self.group.get_discussion_topic(group_id)
        self.assertIsInstance(discussion, DiscussionTopic)
        self.assertTrue(hasattr(discussion, 'group_id'))
        self.assertEqual(group_id, discussion.id)
        self.assertEqual(discussion.group_id, 1)

    # get_file()
    def test_get_file(self, m):
        register_uris({'group': ['get_file']}, m)

        file = self.group.get_file(1)
        self.assertIsInstance(file, File)
        self.assertEqual(file.display_name, 'Group_File.docx')
        self.assertEqual(file.size, 4096)

    # get_full_discussion_topic
    def test_get_full_discussion_topic(self, m):
        register_uris({'group': ['get_full_discussion_topic']}, m)

        topic_id = 1
        discussion = self.group.get_full_discussion_topic(topic_id)
        self.assertIsInstance(discussion, DiscussionTopic)
        self.assertTrue(hasattr(discussion, 'view'))
        self.assertTrue(hasattr(discussion, 'participants'))
        self.assertEqual(discussion.group_id, 1)

    # get_discussion_topics()
    def test_get_discussion_topics(self, m):
        register_uris({'group': ['get_discussion_topics']}, m)

        response = self.group.get_discussion_topics()
        discussion_list = [discussion for discussion in response]
        self.assertIsInstance(discussion_list[0], DiscussionTopic)
        self.assertTrue(hasattr(discussion_list[0], 'group_id'))
        self.assertEqual(2, len(discussion_list))

    # create_discussion_topic()
    def test_create_discussion_topic(self, m):
        register_uris({'group': ['create_discussion_topic']}, m)

        title = "Topic 1"
        discussion = self.group.create_discussion_topic()
        self.assertTrue(hasattr(discussion, 'group_id'))
        self.assertIsInstance(discussion, DiscussionTopic)
        self.assertEqual(discussion.title, title)
        self.assertEqual(discussion.group_id, 1)

    # reorder_pinned_topics()
    def test_reorder_pinned_topics(self, m):
        register_uris({'group': ['reorder_pinned_topics']}, m)

        order = [1, 2, 3]

        discussions = self.group.reorder_pinned_topics(order=order)
        self.assertTrue(discussions)

    def test_reorder_pinned_topics_no_list(self, m):
        register_uris({'group': ['reorder_pinned_topics_no_list']}, m)

        order = "1, 2, 3"

        with self.assertRaises(ValueError):
            self.group.reorder_pinned_topics(order=order)

    # list_external_feeds()
    def test_list_external_feeds(self, m):
        register_uris({'group': ['list_external_feeds']}, m)

        feeds = self.group.list_external_feeds()
        feed_list = [feed for feed in feeds]
        self.assertEqual(len(feed_list), 2)
        self.assertTrue(hasattr(feed_list[0], 'url'))
        self.assertIsInstance(feed_list[0], ExternalFeed)

    # create_external_feed()
    def test_create_external_feed(self, m):
        register_uris({'group': ['create_external_feed']}, m)

        url_str = "http://example.com/myblog.rss"
        response = self.group.create_external_feed(url=url_str)
        self.assertIsInstance(response, ExternalFeed)

    # delete_external_feed()
    def test_delete_external_feed(self, m):
        register_uris({'group': ['delete_external_feed']}, m)

        ef_id = 1
        deleted_ef = self.group.delete_external_feed(ef_id)

        self.assertIsInstance(deleted_ef, ExternalFeed)
        self.assertTrue(hasattr(deleted_ef, 'url'))
        self.assertEqual(deleted_ef.display_name, "My Blog")

    # list_files()
    def test_group_files(self, m):
        register_uris({'group': ['list_group_files', 'list_group_files2']}, m)

        files = self.group.list_files()
        file_list = [file for file in files]
        self.assertEqual(len(file_list), 4)
        self.assertIsInstance(file_list[0], File)

    # get_folder()
    def test_get_folder(self, m):
        register_uris({'group': ['get_folder']}, m)

        folder = self.group.get_folder(1)
        self.assertEqual(folder.name, "Folder 1")
        self.assertIsInstance(folder, Folder)

    # list_folders()
    def test_list_folders(self, m):
        register_uris({'group': ['list_folders']}, m)

        folders = self.group.list_folders()
        folder_list = [folder for folder in folders]
        self.assertEqual(len(folder_list), 2)
        self.assertIsInstance(folder_list[0], Folder)

    # create_folder()
    def test_create_folder(self, m):
        register_uris({'group': ['create_folder']}, m)

        name_str = "Test String"
        response = self.group.create_folder(name=name_str)
        self.assertIsInstance(response, Folder)

    # list_tabs()
    def test_list_tabs(self, m):
        register_uris({'group': ['list_tabs']}, m)

        tabs = self.group.list_tabs()
        tab_list = [tab for tab in tabs]
        self.assertEqual(len(tab_list), 2)
        self.assertIsInstance(tab_list[0], Tab)

@requests_mock.Mocker()
class TestGroupMembership(unittest.TestCase):

    def setUp(self):
        self.canvas = Canvas(settings.BASE_URL, settings.API_KEY)

        with requests_mock.Mocker() as m:
            register_uris({'group': ['get_by_id', 'get_membership']}, m)

            self.group = self.canvas.get_group(1)
            self.membership = self.group.get_membership(1, "users")

    # __str__()
    def test__str__(self, m):
        string = str(self.membership)
        self.assertIsInstance(string, str)

    # update()
    def test_update(self, m):
        register_uris({'group': ['update_membership_membership']}, m)

        response = self.membership.update(mem_id=1, moderator=False)
        self.assertIsInstance(response, GroupMembership)

    # remove_user()
    def test_remove_user(self, m):
        register_uris({'group': ['remove_user']}, m)

        response = self.membership.remove_user(1)

        self.assertIsInstance(response, dict)
        self.assertEqual(len(response), 0)

    # remove_self()
    def test_remove_self(self, m):
        register_uris({'group': ['remove_self']}, m)

        response = self.membership.remove_self()

        self.assertIsInstance(response, dict)
        self.assertEqual(len(response), 0)

@requests_mock.Mocker()
class TestGroupCategory(unittest.TestCase):

    def setUp(self):
        self.canvas = Canvas(settings.BASE_URL, settings.API_KEY)
        with requests_mock.Mocker() as m:
            register_uris({'course': ['get_by_id', 'create_group_category']}, m)

            self.course = self.canvas.get_course(1)
            self.group_category = self.course.create_group_category("Test String")

    # __str__()
    def test__str__(self, m):
        string = str(self.group_category)
        self.assertIsInstance(string, str)

    # create_group()
    def test_create_group(self, m):
        register_uris({'group': ['category_create_group']}, m)

        test_str = "Test Create Group"
        response = self.group_category.create_group(name=test_str)
        self.assertIsInstance(response, Group)
        self.assertTrue(hasattr(response, 'name'))
        self.assertEqual(response.name, test_str)

    # update()
    def test_update(self, m):
        register_uris({'group': ['category_update']}, m)

        new_name = "Test Update Category"
        response = self.group_category.update(name=new_name)
        self.assertIsInstance(response, GroupCategory)

    # delete_category()
    def test_delete_category(self, m):
        register_uris({'group': ['category_delete_category']}, m)

        response = self.group_category.delete()

        self.assertIsInstance(response, dict)
        self.assertEqual(len(response), 0)

    # list_groups()
    def test_list_groups(self, m):
        register_uris({'group': ['category_list_groups']}, m)

        response = self.group_category.list_groups()
        group_list = [group for group in response]
        self.assertEqual(len(group_list), 2)
        self.assertIsInstance(group_list[0], Group)
        self.assertTrue(hasattr(group_list[0], 'id'))

    # list_users()
    def test_list_users(self, m):
        from canvasapi.user import User

        register_uris({'group': ['category_list_users']}, m)

        response = self.group_category.list_users()
        user_list = [user for user in response]
        self.assertEqual(len(user_list), 4)
        self.assertIsInstance(user_list[0], User)
        self.assertTrue(hasattr(user_list[0], 'user_id'))

    # assign_members()
    def test_assign_members(self, m):
        from canvasapi.progress import Progress
        from canvasapi.paginated_list import PaginatedList

        requires = {
            'group': [
                'category_assign_members_true',
                'category_assign_members_false'
            ]
        }
        register_uris(requires, m)

        result_true = self.group_category.assign_members(sync=True)
        return_false = self.group_category.assign_members()

        self.assertIsInstance(result_true, PaginatedList)
        self.assertIsInstance(return_false, Progress)


def compute_bic(centroids, labels, n_clusters, X):
    """
    Computes the BIC metric for a given clusters
    Parameters:
    -----------------------------------------
    kmeans:  List of clustering object from scikit learn
    X     :  multidimension np array of data points
    Returns:
    -----------------------------------------
    BIC value
    """
    import numpy as np
    from scipy.spatial import distance
    # assign centers and labels
    centers = centroids
    # number of clusters
    m = n_clusters
    # size of the clusters
    n = np.bincount(labels)
    # size of data set
    N, d = X.shape

    # compute variance for all clusters beforehand
    cl_var = (1.0 / (N - m) / d) * sum([sum(distance.cdist(X[np.where(labels == i)], [centers[i]],
                                       'euclidean')**2) for i in range(m)])

    const_term = 0.5 * m * np.log(N) * (d + 1)

    BIC = (np.sum([n[i] * np.log(n[i]) -
                n[i] * np.log(N) -
                ((n[i] * d) / 2) * np.log(2 * np.pi * cl_var) -
                ((n[i] - 1) * d / 2) for i in range(m)]) - const_term)

    return BIC

def getValueKey(item):
    return item[1]

def getIdKey(item):
    return item[0]

def doIt(M, values, nmin, nmax, cgap):
    # for debug purpose:
    # print("[Clustering] Python: Input maps:")
    # print(M)
    import importlib
    import math
    import numpy as np
    # import matplotlib.pyplot as plt
    from sklearn.cluster import k_means

    print("[Clustering] Python: Input eigenvalues:")
    print(values)

    print("[Clustering] min: ", nmin)
    print("[Clustering] max: ", nmax)

    kmin = nmin
    kmax = nmax + 1

    k=kmin

    if(kmin!=nmax):
        print("[Clustering] nmin!=nmax")
        maxgap = -1000000
        maxid = 0

        # check if Numpy is installed
        loader = importlib.find_loader('numpy')
        found = loader is not None
        if found:
            print("[Clustering] Python: numpy module found.")
        else:
            print("[Clustering] Python error: numpy module not found.")
            return 0

        # check if scipy is installed
        loader = importlib.find_loader('scipy')
        found = loader is not None
        if found:
            print("[Clustering] Python: scipy module found.")
        else:
            print("[Clustering] Python error: scipy module not found.")
            return 0

        # check if Scikit-learn is installed
        loader = importlib.find_loader('sklearn')
        found = loader is not None
        if found:
            print("[Clustering] Python: sklearn module found.")
        else:
            print("[Clustering] Python error: sklearn module not found.")
            return 0

        from sklearn.cluster import k_means

        eigenvalues = sorted(values[0], reverse=True)
        #x=[]
        #for i in range(0, len(eigenvalues)):
        #    print("value: ", eigenvalues[i])
        #    print("Id: ", i)
        #    x.append(i+1)
        #plt.plot(x, eigenvalues)
        #plt.show(x)

        gapsPairs=[]
        gaps=[]
        x=[]

        if(len(eigenvalues) < kmax):
            kmax = len(eigenvalues)

        averageGap = 0.
        for i in range(2, kmax):
            gap = math.fabs(eigenvalues[i]-eigenvalues[i-1])
            gaps.append(gap)
            x.append(i)
            gapsPairs.append([i, gap])
            #print("Gap: ", gap)
            #print("Id: ", i)
            averageGap = averageGap + gap

        # export to CSV
        # import csv
        # csvfile = open('/tmp/clustering_gaps.csv', 'w')
        # csvwriter = csv.writer(csvfile, delimiter=';')
        # csvwriter.writerow(['eigengaps'])
        # for i in range(len(gaps)):
        #     csvwriter.writerow([gaps[i]])

        averageGap = averageGap/kmax

        sortedGapsPairs=sorted(gapsPairs, key=getValueKey, reverse=True)
        print("[Clustering] Python Sorted: ", sortedGapsPairs)

        maxgap=sortedGapsPairs[0][1]

        significantGapNb = 1
        for i in range(1, len(sortedGapsPairs)):
            currentGap=sortedGapsPairs[i][1]
            #print("percentage: ", currentGap/maxgap)
            if currentGap > 0.00001 and (currentGap/maxgap > 0.1):
                significantGap = i

        if(cgap>significantGap):
            significantGap = cgap
        significantGapsPairs = sortedGapsPairs[0:significantGap+1]
        #print("Sorted cut significant: ", significantGapsPairs)

        significantGapsPairs = sorted(significantGapsPairs, key=getIdKey)
        print("[Clustering] Python Significant Pairs: ", significantGapsPairs)

        # export to CSV
        # import csv
        # csvfile = open('/tmp/clustering_sortedGaps.csv', 'w')
        # csvwriter = csv.writer(csvfile, delimiter=';')
        # csvwriter.writerow(['k', 'gaps'])
        # for i in range(len(significantGapsPairs)):
        #     csvwriter.writerow([significantGapsPairs[i][0], significantGapsPairs[i][1]])

        #significantGapsPairs=sorted(significantGapsPairs, key=getIdKey)
        #print("Significant: ", significantGapsPairs)
        #print("Current gap: ", cgap)

        maxgap = significantGapsPairs[cgap-1][1]
        maxid = significantGapsPairs[cgap-1][0]

        print("[Clustering] Python maxgap: ", maxgap)
        print("[Clustering] Python maxid: ", maxid)
        print("[Clustering] Python Percentage average over max gap: ", averageGap/maxgap)

        k = maxid

        # if(k<0):
        #     k=nmax

        # if( averageGap/maxgap > 0.6 ):
        #     k = nmax
        #     print("[Clustering] Python not significant gap found in range: ", averageGap/maxgap)

        print("[Clustering] Python chosen k: ", k)

        # plt.plot(x, gaps)
        # plt.show(x)

    _, labels, _ = k_means(M, n_clusters=k, random_state=0)

    #for i in range(0, len(labels)):
    #    if(labels[i] < 1):
    #        labels[i]=1

    result = np.asarray(labels)

    # for debug purpose:
    #print("[Clustering] Python: Output assignation:")
    #print(result)

    return result


import requests
import re
import Book_Info
import time
import numpy as np
import csv
import sys

from bs4 import BeautifulSoup

url = 'http://book.douban.com/people/asyanyang/wish?start=1665&sort=time&rating=all&filter=all&mode=grid'
hds=[{'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'},\
{'User-Agent':'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.12 Safari/535.11'},\
{'User-Agent': 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)'}]

bookNo = 1674 - 1
books = []
pattern = re.compile('\d{7,}')
page_num = 0
csvfile  = open('Chuxu_wish.csv', "w")
writer = csv.writer(csvfile, delimiter='\t', quotechar='"', quoting=csv.QUOTE_ALL)
#
# for row in reader:
#     writer.writerow(row)

response = requests.get(url)
print(response)

# response.headers['content-type']
content = response.content

currPage = BeautifulSoup(content, "html.parser")
# spanSet = soup.findAll('a', attrs = {"class": "nbg"})
# nextUrl = 'null'
# nextPageFlag = currPage.find(rel='next')
# print(nextPageFlag['href'])

while True:

    #    print("当前页面:"+url)
    #    print("下一页"+nextPageFlag['href'])
    #time.sleep(np.random.rand()*5)
    page_num += 1
    print ('Downloading Information From Page %d', page_num)
    content = requests.get(url, headers=hds[page_num%len(hds)]).content
    currPage = BeautifulSoup(content, "html.parser")
    bookLinkSet = currPage.find_all('a', attrs={"class": "nbg"})
    wishTimeSet = currPage.find_all('span', attrs={'class': "date"})
    # print(wishTimeSet)
    # print(bookLinkSet)

    for bookLink, wishTime in zip(bookLinkSet, wishTimeSet):
        subjectLink = bookLink['href']
        wish = wishTime.string
        bookID = re.search(pattern, subjectLink).group()
        book = Book_Info.BookInfo(book_id = bookID, wish_time = wish, bookLink = subjectLink)
        writer.writerow([book.bookID, book.title, book.numRaters, book.averageRate, book.numPages, book.bookLink])
        books.append(book)
        print(bookNo + book.bookCount)
        # print(bookID, wish)
        # print(subjectLink)

    if currPage.find(rel='next') is None:
        break
    else:
        url = currPage.find(rel='next')['href']
        #    print(nextPageFlag)
        # print(soup.prettify())

csvfile.close()

# workbook  = xlsxwriter.Workbook('Chuxu_wish.xlsx')
# worksheet = workbook.add_worksheet()
#
# worksheet.write(books)
#
# workbook.close()


from sccd.realtime.eventloop import *
from sccd.realtime.tkinter import TkInterImplementation
from sccd.cd.parser.xml import *
import GUI
import sys

if __name__ == '__main__':
    if len(sys.argv) == 2:
        model_path = sys.argv[1]
    else:
        model_path = "models/model_03_orthogonal.xml"

    cd = load_cd(model_path)

    def send_event(event: str):
        eventloop.add_input_now(port="in", event_name=event)

    GUI.gui.send_event = send_event

    def on_output(event):
        GUI.gui.handle_event(event)

    controller = Controller(cd, output_callback=on_output)
    eventloop = EventLoop(controller, TkInterImplementation(GUI.tk))

    eventloop.start()
    GUI.tk.mainloop()


from util.diceroller import DiceRoller
from util.tokenizer import Tokenizer
from util.validator import Validator
from util.roll import Roll
from util.calc import Calculator as calc

"""This class parses the tokens list and evaluates the symbols."""
class Parser:

    operators_1 = ['*', '/', 'd', 'kh', 'kl', 'cs']
    operators_2 = ['+', '-']

    def __init__(self, exp: str) -> None:
        t = Tokenizer(exp)
        v = Validator(t.tokens)
        self.result = None
        if v.valid:
            self.result = self.evaluate(v.tokens)

    # returns the object represented as a string
    def __repr__(self) -> str:
        return f'Result\t: {self.result}'

    # evaluates the tokenized expression to a number
    def evaluate(self, _tokens: list[str]):
        tokens = _tokens.copy()

        # resolve groups: ()
        while self.__get_group(tokens) is not None:
            i, j = self.__get_group(tokens)
            group = tokens[i+1:j]
            tokens[i:j+1] = [self.evaluate(group)]

        # resolve operators_1: *, /, d, kh, kl, cs
        while self.__get_operator(tokens, self.operators_1) is not None:
            i = self.__get_operator(tokens, self.operators_1)
            operator = tokens[i]
            left, right = self.__get_neighbours(tokens, i)
            tokens[i-1:i+2] = [self.__calculate(left, operator, right)]

        # resolve operators_2: +, -
        while self.__get_operator(tokens, self.operators_2) is not None:
            i = self.__get_operator(tokens, self.operators_2)
            operator = tokens[i]
            left, right = self.__get_neighbours(tokens, i)
            tokens[i-1:i+2] = [self.__calculate(left, operator, right)]

        # return result
        if len(tokens) == 1:
            x = tokens[0]
            if isinstance(x, Roll) or isinstance(x, int):
                return x
        return None

    # calculates a simple operation with 2 arguments
    def __calculate(self, left, operator: str, right):
        if operator in self.operators_1 or operator in self.operators_2:
            if operator == '+':
                if (isinstance(left, int) or isinstance(left, Roll)) and (isinstance(right, int) or isinstance(right, Roll)):
                    return calc.add(left, right)
            elif operator == '-':
                if (isinstance(left, int) or isinstance(left, Roll)) and (isinstance(right, int) or isinstance(right, Roll)):
                    return calc.substract(left, right)
            elif operator == '*':
                if (isinstance(left, int) or isinstance(left, Roll)) and (isinstance(right, int) or isinstance(right, Roll)):
                    return calc.multiply(left, right)
            elif operator == '/':
                if (isinstance(left, int) or isinstance(left, Roll)) and (isinstance(right, int) or isinstance(right, Roll)):
                    return calc.divide(left, right)
            elif operator == 'd':
                if (isinstance(left, int) or isinstance(left, Roll)) and (isinstance(right, int) or isinstance(right, Roll)):
                    if int(left) >= 0 and int(right) >= 0:
                        roll = calc.roll_dice(left, right)
                        print(roll)
                        return roll
            elif operator == 'kh':
                if isinstance(left, Roll) and (isinstance(right, int) or isinstance(right, int)):
                    if int(right) >= 0:
                        return calc.keep_high(left, right)
            elif operator == 'kl':
                if isinstance(left, Roll) and (isinstance(right, int) or isinstance(right, int)):
                    if int(right) >= 0:
                        return calc.keep_low(left, right)
            elif operator == 'cs':
                if isinstance(left, Roll) and isinstance(right, str):
                    return calc.count_success(left, right)
        return None

    # returns the position (start, end) of the first group '(...)' from tokens list
    @staticmethod
    def __get_group(tokens: list[str]):
        for i in range(len(tokens)):
            if tokens[i] == '(':
                count = 1
                for j in range(i+1, len(tokens)):
                    if tokens[j] == '(':
                        count += 1
                    elif tokens[j] == ')':
                        count -= 1
                        if count == 0:
                            return (i, j)

    # returns the position of the first list element that is an operator from a specified operators list
    @staticmethod
    def __get_operator(tokens: list[str], operators: list[str]):
        for i, x in enumerate(tokens):
            if x in operators:
                return i
        return None

    # returns a tuple of 2 list elements: to the left and right of the element given by index
    @staticmethod
    def __get_neighbours(tokens: list[str], idx: int):
        if idx == 0:
            left = None
        else:
            left = tokens[idx-1]
        if idx == len(tokens) - 1:
            right = None
        else:
            right = tokens[idx+1]
        return (left, right)


import packtools

# packtools.stylechecker double

class XMLValidatorDouble(object):
    @classmethod
    def parse(cls, file, dtd=None, no_doctype=False):
        pass

    def validate_all(self, fail_fast=False):
        return True, None

    @property
    def meta(self):
        return {
            'article_title': 'HIV/AIDS knowledge among men who have sex with men: applying the item response theory',
            'issue_year': '2014',
            'journal_title': u'Revista de Sa\xfade P\xfablica',
            'journal_pissn': '0034-8910',
            'journal_eissn': '1518-8787',
            'issue_number': '2',
            'issue_volume': '48'
        }

class XMLValidatorAnnotationsDouble(XMLValidatorDouble):
    def annotate_errors(self, fail_fast=False):
        return "some annotations in xml string"

    def validate_all(self, fail_fast=False):
        error_list = []

        class DummyError(object):
            line = 1
            column = 6
            message = u'Premature end of data in tag xml line 1, line 1, column 6'
            level_name = 'ERROR'

        for x in xrange(0, 6):
            error_list.append(DummyError())

        return False, error_list

# ------------------
# utils.analyze_xml
# ------------------
def make_stub_analyze_xml(type):
    """Factory for utils.analyze_xml stub functions.

    `type` can be: 'valid' or 'invalid'.
    """
    sample_xml = "
foo
"
    sample_meta = {}

    if type == 'valid':
        result = {
            'annotations': sample_xml,
            'validation_errors': None,
            'meta': sample_meta,
        }
        err = None
    elif type == 'invalid':
        result = {
            'annotations': sample_xml,
            'validation_errors': [],
            'meta': sample_meta,
        }
        err = None
    elif type == 'throw_io_error':
        result = None
        err = IOError(u'Error reading file foo.xml')
    elif type == 'syntax_error':
        result = None
        err = Exception(u'Premature end of data in tag unclosed_tag line 5, line 5, column 17')
    else:
        raise ValueError('Unknown type value')

    def stub__analyze_xml(f, extra_schematron=None):
        return result, err

    return stub__analyze_xml


from rest_framework import serializers
from authentication.models import Account

from authentication.serializers import AccountSerializer
from company.models import Costumer, Company, Place

class CostumerSerializer(serializers.ModelSerializer):
    """Serializes the :class:`Costumer` for consumption in the API"""
    account = AccountSerializer(read_only=True, required=False)

    class Meta:
        model = Costumer

        fields = ('id', 'account', 'name', 'rtn', 'created',)
        read_only_fields = ('id', 'created')

    def get_validation_exclusion(self):
        exclusions = super(CostumerSerializer, self).get_validation_exclusions()

        return exclusions + ['account']

class CompanySerializer(serializers.HyperlinkedModelSerializer):
    account = serializers.SlugRelatedField(
        slug_field=Account.USERNAME_FIELD,
        queryset=Account.objects.all()
    )
    place_set = serializers.HyperlinkedRelatedField(
        many=True,
        read_only=True,
        view_name='place-detail'
    )

    class Meta:
        model = Company

        fields = (
            'id', 'account', 'name', 'rtn', 'cai', 'created', 'place_set',
        )
        read_only_fields = ('id', 'created',)

    def get_validation_exclusion(self):
        exclusions = super(CompanySerializer, self).get_validation_exclusions()

        return exclusions + ['account']

class PlaceSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Place

        fields = ('id', 'name', 'company', 'prefix', 'next_receipt_number',
                  'max_emission_date',)
        read_only_fields = ('id', 'created',)

    def get_validation_exclusion(self):
        exclusions = super(PlaceSerializer, self).get_validation_exclusions()

        return exclusions + ['account']


import numpy as np
import time
import matplotlib.pyplot as plt
import math, cv2, os
import copy
from scipy.interpolate import griddata

class marker_flow:
    def __init__(self):
        self.kernel1 = self.make_kernel(3, 'circle')
        self.kernel2 = self.make_kernel(45, 'circle')
        self.scale = 1
        self.refresh1 = False
        self.refresh2 = False
        self.marker_thre = 100

    def rgb2gray(self, rgb):
        return np.dot(rgb[..., :3], [0.33, 0.33, 0.34])

    def make_kernel(self, n, type):
        if type is 'circle':
            kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (n, n))
        else:
            kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (n, n))
        return kernel

    def defect_mask(self, im_cal):
        pad = 60
        var0 = 60  #left up
        var1 = 60  # right up
        var2 = 65  # right down
        var3 = 60  # left down
        im_mask = np.ones((im_cal.shape))
        triangle0 = np.array([[0, 0], [var0, 0], [0, var0]])
        triangle1 = np.array([[im_mask.shape[1] - var1, 0],
                              [im_mask.shape[1], 0], [im_mask.shape[1], var1]])
        triangle2 = np.array([[im_mask.shape[1] - var2, im_mask.shape[0]], [im_mask.shape[1], im_mask.shape[0]], \
            [im_mask.shape[1], im_mask.shape[0]-var2]])
        triangle3 = np.array([[0, im_mask.shape[0]],
                              [0, im_mask.shape[0] - var3],
                              [var3, im_mask.shape[0]]])
        color = [0]  #im_mask
        cv2.fillConvexPoly(im_mask, triangle0, color)
        cv2.fillConvexPoly(im_mask, triangle1, color)
        cv2.fillConvexPoly(im_mask, triangle2, color)
        cv2.fillConvexPoly(im_mask, triangle3, color)
        im_mask[:pad, :] = 0
        im_mask[-pad:, :] = 0
        im_mask[:, :pad] = 0
        im_mask[:, -pad:] = 0
        return im_mask

    def make_thre_mask(self, im_cal):
        thre_image = np.zeros(im_cal.shape, dtype=np.uint8)
        previous_mask = np.zeros(im_cal.shape, dtype=np.uint8)
        for i in range(10, 80, 30):
            _, mask = cv2.threshold(im_cal.astype(np.uint8), i, 255,
                                    cv2.THRESH_BINARY_INV)
            mask_expand = cv2.dilate(mask, self.kernel2, iterations=1)
            mask_erode = cv2.erode(mask_expand, self.kernel2, iterations=1)
            thre_image += (mask_erode - previous_mask) / 255 * i
            previous_mask = mask_erode
            # cv2.imshow('threshold', thre_image)
            # cv2.waitKey(0)
        thre_image += (np.ones(im_cal.shape, dtype=np.uint8) -
                       previous_mask / 255) * 80 + 10

        return thre_image

    def creat_mask_2(self, raw_image, dmask):
        scale = 2
        m, n = raw_image.shape[1], raw_image.shape[0]
        raw_image = cv2.pyrDown(raw_image).astype(np.float32)
        blur = cv2.GaussianBlur(raw_image, (25, 25), 0)
        blur2 = cv2.GaussianBlur(raw_image, (5, 5), 0)
        diff = blur - blur2
        diff *= 16.0
        diff[diff < 0.] = 0.
        diff[diff > 255.] = 255.
        mask = (diff[:, :, 0] > 25) & (diff[:, :, 2] > 25) & (diff[:, :, 1] >
                                                              120)
        mask = cv2.resize(mask.astype(np.uint8), (m, n))
        mask = mask * dmask
        mask = cv2.dilate(mask, self.kernel1, iterations=1)
        return (1 - mask) * 255

    def find_dots(self, binary_image):
        # down_image = cv2.resize(binary_image, None, fx=2, fy=2)
        params = cv2.SimpleBlobDetector_Params()
        # Change thresholds
        params.minThreshold = 1
        params.maxThreshold = 12
        params.minDistBetweenBlobs = 9
        params.filterByArea = True
        params.minArea = 9
        params.filterByCircularity = False
        params.filterByConvexity = False
        params.filterByInertia = False
        params.minInertiaRatio = 0.5
        detector = cv2.SimpleBlobDetector_create(params)
        keypoints = detector.detect(binary_image.astype(np.uint8))
        return keypoints

    def flow_calculate_in_contact(self, keypoints2, x_initial, y_initial,
                                  u_ref, v_ref):
        x2, y2, u, v, x1_paired, y1_paired, x2_paired, y2_paired = [], [], [], [], [], [], [], []

        for i in range(len(keypoints2)):
            x2.append(keypoints2[i].pt[0] / self.scale)
            y2.append(keypoints2[i].pt[1] / self.scale)

        x2 = np.array(x2)
        y2 = np.array(y2)
        index_list = []

        for i in range(x2.shape[0]):

            distance = list(((np.array(x_initial) - x2[i])**2 +
                             (np.array(y_initial) - y2[i])**2))
            if len(distance) == 0:
                break
            min_index = distance.index(min(distance))
            u_temp = x2[i] - x_initial[min_index]
            v_temp = y2[i] - y_initial[min_index]
            shift_length = np.sqrt(u_temp**2 + v_temp**2)
            # print 'length',shift_length

            if shift_length < 12:
                # print xy2.shape,min_index,len(distance)
                x1_paired.append(x_initial[min_index] - u_ref[min_index])
                y1_paired.append(y_initial[min_index] - v_ref[min_index])
                x2_paired.append(x2[i])
                y2_paired.append(y2[i])
                u.append(u_temp + u_ref[min_index])
                v.append(v_temp + v_ref[min_index])
                index_list.append(self.index_list[min_index])

                if shift_length > 7:
                    refresh = True
                else:
                    refresh = False

        return x1_paired, y1_paired, x2_paired, y2_paired, u, v, refresh, index_list

    def flow_calculate_global(self, keypoints2, x_initial, y_initial, u_ref,
                              v_ref):
        x2, y2, u, v, x1_paired, y1_paired, x2_paired, y2_paired  = [], [], [], [], [], [], [], []
        x1_return, y1_return, x2_return, y2_return, u_return, v_return = [],[],[],[],[],[]

        for i in range(len(keypoints2)):
            x2.append(keypoints2[i].pt[0] / self.scale)
            y2.append(keypoints2[i].pt[1] / self.scale)

        x2 = np.array(x2)
        y2 = np.array(y2)
        index_list = []

        for i in range(x2.shape[0]):
            distance = list(((np.array(x_initial) - x2[i])**2 +
                             (np.array(y_initial) - y2[i])**2))
            if len(distance) == 0:
                break
            min_index = distance.index(min(distance))
            u_temp = x2[i] - x_initial[min_index]
            v_temp = y2[i] - y_initial[min_index]
            shift_length = np.sqrt(u_temp**2 + v_temp**2)
            # print 'length',shift_length
            if shift_length < 12:
                x1_paired.append(x_initial[min_index] - u_ref[min_index])
                y1_paired.append(y_initial[min_index] - v_ref[min_index])
                x2_paired.append(x2[i])
                y2_paired.append(y2[i])
                u.append(u_temp + u_ref[min_index])
                v.append(v_temp + v_ref[min_index])
                index_list.append(self.index_list[min_index])

                # del x_initial[min_index], y_initial[min_index], u_ref[
                #     min_index], v_ref[min_index]

        x1_return = np.array(x1_paired)
        y1_return = np.array(y1_paired)
        x2_return = np.array(x2_paired)
        y2_return = np.array(y2_paired)
        u_return = np.array(u)
        v_return = np.array(v)

        return x1_return, y1_return, x2_return, y2_return, u_return, v_return, \
            list(x2_paired), list(y2_paired), np.array(x2_paired), np.array(y2_paired), index_list
        # return x1_paired,y1_paired,x2_paired,y2_paired,u,v

    def dispOpticalFlow(self, im_cal, x, y, u, v, name):
        # mask = np.zeros_like(im_cal)
        mask2 = np.zeros_like(im_cal)
        amf = 3
        x = np.array(x).astype(np.int16)
        y = np.array(y).astype(np.int16)
        for i in range(u.shape[0]):  #self.u_sum

            mask2 = cv2.line(mask2,
                             (int(x[i] + u[i] * amf), int(y[i] + v[i] * amf)),
                             (x[i], y[i]), [0, 120, 120], 2)

        img = cv2.add(im_cal, mask2)

        cv2.imshow(name, img.astype(np.uint8))
        cv2.waitKey(0)

    def main(self, raw_imag, first_image):

        if first_image:

            imgwc = np.array(raw_imag).astype(np.float32)
            self.im_slipsign = np.zeros(imgwc.shape)
            cv2.putText(self.im_slipsign, 'Slip', (210, 30),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2,
                        cv2.LINE_AA)
            im_gray = self.rgb2gray(imgwc)  #.astype(np.uint8)
            self.dmask1 = self.defect_mask(im_gray)
            final_image = self.creat_mask_2(imgwc, self.dmask1)
            keypoints = self.find_dots(final_image)
            self.u_sum1 = np.zeros(len(keypoints))
            self.v_sum1 = np.zeros(len(keypoints))
            self.u_addon1 = list(self.u_sum1)
            self.v_addon1 = list(self.v_sum1)
            self.x1_last1 = []
            self.y1_last1 = []
            for i in range(len(keypoints)):
                self.x1_last1.append(keypoints[i].pt[0] / self.scale)
                self.y1_last1.append(keypoints[i].pt[1] / self.scale)
            self.x_iniref1 = list(self.x1_last1)
            self.y_iniref1 = list(self.y1_last1)
            self.absmotion1 = 0
            self.index_list = range(len(self.x1_last1))
            return np.array(self.x1_last1), np.array(
                self.y1_last1), np.zeros_like(self.x1_last1), np.zeros_like(
                    self.x1_last1), np.array(self.index_list)

        else:
            imgwc = np.array(raw_imag).astype(np.float32)
            final_image = self.creat_mask_2(imgwc, self.dmask1)

            if self.refresh1:
                keypoints = self.find_dots(final_image)
                x1, y1, x2, y2, u, v, self.x_iniref1, self.y_iniref1, self.u_addon1, self.v_addon1, index_list\
                    = self.flow_calculate_global(keypoints, list(self.x_iniref1), list(self.y_iniref1), \
                        list(self.u_addon1), list(self.v_addon1))
                self.refresh1 = False
            else:
                keypoints = self.find_dots(final_image)
                x1, y1, x2, y2, u, v, self.refresh1, index_list = self.flow_calculate_in_contact(
                    keypoints, list(self.x_iniref1), list(self.y_iniref1),
                    list(self.u_addon1), list(self.v_addon1))

            return np.array(x2), np.array(y2), np.array(u), np.array(
                v), np.array(index_list)

if __name__ == "__main__":

    mf = marker_flow()
    path = '/media/mcube/SERVER_HD/siyuan/policy_finetune/'
    ob = 'circle'
    num = 10
    x_min, x_max, y_min, y_max = 62, 366, 63, 256  #marker tracking region
    gap = 10  #pixel gap between each measurement

    for i in range(12):  #read the 12 images during contact period
        img = cv2.imread(path + ob + '/' + str(num) + '/' + str(i) + '.jpg')
        x, y, u, v, index_list = mf.main(img, i == 0)

        if i == 0:
            x_ref, y_ref = copy.deepcopy(x), copy.deepcopy(y)
            x_paired, y_paired = copy.deepcopy(x), copy.deepcopy(y)
            # x_min, x_max = int(np.min(x_ref)), int(np.max(x_ref)) + 1
            # y_min, y_max = int(np.min(y_ref)), int(np.max(y_ref)) + 1
            x_grid, y_grid = np.meshgrid(range(x_min, x_max, 10),
                                         range(y_min, y_max, 10))
            u_grid = np.zeros_like(x_grid)
            v_grid = np.zeros_like(x_grid)
        else:
            x_paired, y_paired = x_ref[index_list], y_ref[index_list]

            points = np.squeeze(np.dstack((x_paired.T, y_paired.T)))
            values = np.squeeze(np.dstack((u.T, v.T)))

            uv = griddata(points,
                          values, (x_grid.flatten(), y_grid.flatten()),
                          method='linear')
            uv[np.isnan(uv)] = 0.
            u_grid = uv[:, 0]
            v_grid = uv[:, 1]

        u_grid_image = np.reshape(u_grid, x_grid.shape)
        v_grid_image = np.reshape(v_grid, x_grid.shape)

        cv2.imshow('u', ((u_grid_image + 10) * 12).astype(np.uint8))
        cv2.imshow('v', ((v_grid_image + 10) * 12).astype(np.uint8))
        mf.dispOpticalFlow(img, x_grid.flatten(), y_grid.flatten(),
                           u_grid.flatten(), v_grid.flatten(), 'flow')


from services.base_service import BaseService
from common.constant.category import Category
from services.non_modify_services.redo_service import RedoService

class UndoService(BaseService):
    undo_list = []
    if False:
        undo_list = [BaseService()]

    def get_code(self):
        return 'u'

    def get_category(self):
        return Category.non_modify

    def process(self, data):  # type: (str) -> None
        if not UndoService.undo_list:
            # print(self.file.content)
            return
        service = UndoService.undo_list.pop()
        service.undo()
        RedoService.add(service)

    @classmethod
    def add(self, service):
        UndoService.undo_list.append(service)


"""
INF-tc1
@author: Arthur

TD 1
"""
# Exercice VIII-2

def convertir(S):
    str1 = ''.join(S)
    if (str1.isdigit()):
        int(str1)
        print('Safe')
    else:
        print('Pas possible. String contient valeurs non acceptees')

string = ['1','2','3']
convertir(string)


# -*- coding: utf-8 -*-
from odoo import api, fields, models
from odoo.tools.sql import drop_view_if_exists

class is_sale_order_line(models.Model):
    _name='is.sale.order.line'
    _description='is.sale.order.line'
    _order='id desc'
    _auto = False

    company_id              = fields.Many2one('res.company', 'Société')
    date_order              = fields.Datetime('Date Commande')
    delivery_date           = fields.Datetime('Date Livraison')
    is_commande_soldee      = fields.Boolean('Commande soldée')
    partner_id              = fields.Many2one('res.partner', 'Client')
    product_id              = fields.Many2one('product.product', 'Article')
    barcode                 = fields.Char('Code barre')
    product_uom             = fields.Many2one('uom.uom', 'Unité')
    description             = fields.Char('Description')
    product_uom_qty         = fields.Float('Qt cde'            , digits=(14,3))
    qty_delivered           = fields.Float('Qt livrée'         , digits=(14,3))
    qty_invoiced            = fields.Float('Qt facturée'       , digits=(14,3))
    order_id                = fields.Many2one('sale.order', 'Commande')
    order_line_id           = fields.Many2one('sale.order.line', 'Ligne de commande')
    is_purchase_line_id     = fields.Many2one('purchase.order.line', 'Ligne cde fournisseur')
    purchase_order_id       = fields.Many2one('purchase.order', 'Cde fournisseur')
    state                   = fields.Selection([
            ('draft', 'Brouillon'),
            ('sent', 'Envoyé'),
            ('sale', 'Confirmé'),
            ('done', 'Bloqué'),
            ('cancel', 'Annulé'),
        ], 'Etat de la commande')

    def init(self):
        drop_view_if_exists(self.env.cr, self._table)
        self.env.cr.execute("""
            CREATE OR REPLACE view is_sale_order_line AS (
                select
                    sol.id,
                    so.company_id,
                    sol.order_id,
                    so.is_commande_soldee,
                    so.date_order,
                    so.delivery_date,
                    so.partner_id,
                    sol.product_id,
                    pp.barcode,
                    sol.product_uom,
                    sol.name as description,
                    sol.product_uom_qty,
                    sol.qty_delivered,
                    sol.qty_invoiced ,
                    sol.id                  as order_line_id,
                    sol.is_purchase_line_id,
                    po.id                   as purchase_order_id,
                    so.state
                from sale_order so    inner join sale_order_line     sol on so.id=sol.order_id
                                      inner join product_product     pp on sol.product_id=pp.id
                                      inner join product_template    pt on pp.product_tmpl_id=pt.id
                                      inner join res_partner         rp on so.partner_id=rp.id
                                      left join purchase_order_line  pol on sol.is_purchase_line_id=pol.id
                                      left join purchase_order       po on pol.order_id=po.id
            )
        """)


import numpy as np
import pandas as pd
import os
import h5py
import pickle
import scipy.io.wavfile
from glob import glob
from natsort import natsorted
from random import shuffle

def get_phone_dict(label_filename_fmt):
	"""
	Arguments
	label_filename_fmt: Filename convention to pass to glob
	---
	Return
	phone_dict: Dictionary mappng all phones to a number (can be used for generating one-hot label)
	"""
	all_lab_files = glob(label_filename_fmt)
	all_lab_files = natsorted(all_lab_files)
	phone_dict = {}
	phone_dict['x'] = 0
	phone_dict['pau'] = 1
	count = 2

	for lab_file in all_lab_files:
		with open(lab_file, 'r') as f:
			for line in f.readlines():
				label = line.strip().split()[-1]
				p1, rest = label.split('^',1)
				p2, rest = rest.split('-',1)
				p3, rest = rest.split('+',1)
				p4, rest = rest.split('=',1)
				p5_p6, rest = rest.split('_',1)
				p5, p6 = p5_p6.rsplit('@',1)

				if p1 not in phone_dict.keys():
					phone_dict[p1] = count
					count += 1
				if p2 not in phone_dict.keys():
					phone_dict[p2] = count
					count += 1
				if p3 not in phone_dict.keys():
					phone_dict[p3] = count
					count += 1
				if p4 not in phone_dict.keys():
					phone_dict[p4] = count
					count += 1
				if p5 not in phone_dict.keys():
					phone_dict[p5] = count
					count += 1

	return phone_dict

def parse_label_files(label_filename_fmt, output_directory):
	"""
	Arguments
	label_filename_fmt: Filename convention to pass to glob
	output_directory: The directory to save the output csv files to
	"""
	all_lab_files = glob(label_filename_fmt)
	all_lab_files = natsorted(all_lab_files)

	for lab_file in all_lab_files:
		df = pd.DataFrame(columns=['t1','t2','p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'b10', 'b11', 'b12', 'b13', 'b14', 'b15', 'b16', 'c1', 'c2', 'c3', 'd1', 'd2', 'e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8', 'f1', 'f2', 'g1', 'g2', 'h1', 'h2', 'h3', 'h4', 'h5', 'i1', 'i2', 'j1', 'j2', 'j3'])
		idx = 0

		with open(lab_file, 'r') as f:
			for line in f.readlines():
				t1, t2, label = line.strip().split()
				p1, rest = label.split('^',1)
				p2, rest = rest.split('-',1)
				p3, rest = rest.split('+',1)
				p4, rest = rest.split('=',1)
				p5_p6, rest = rest.split('_',1)
				p5, p6 = p5_p6.rsplit('@',1)
				p7, rest = rest.split('/',1)

				_, rest = rest.split(':',1)
				a1, rest = rest.split('_',1)
				a2, rest = rest.split('_',1)
				a3, rest = rest.split('/',1)

				_, rest = rest.split(':',1)
				b1, rest = rest.split('-',1)
				b2, rest = rest.split('-',1)
				b3, rest = rest.split('@',1)
				b4, rest = rest.split('-',1)
				b5, rest = rest.split('&',1)
				b6, rest = rest.split('-',1)
				b7, rest = rest.split('#',1)
				b8, rest = rest.split('-',1)
				b9, rest = rest.split('$',1)
				b10, rest = rest.split('-',1)
				b11, rest = rest.split('!',1)
				b12, rest = rest.split('-',1)
				b13, rest = rest.split(';',1)
				b14, rest = rest.split('-',1)
				b15, rest = rest.split('|',1)
				b16, rest = rest.split('/',1)

				_, rest = rest.split(':',1)
				c1, rest = rest.split('+',1)
				c2, rest = rest.split('+',1)
				c3, rest = rest.split('/',1)

				_, rest = rest.split(':',1)
				d1, rest = rest.split('_',1)
				d2, rest = rest.split('/',1)

				_, rest = rest.split(':',1)
				e1, rest = rest.split('+',1)
				e2, rest = rest.split('@',1)
				e3, rest = rest.split('+',1)
				e4, rest = rest.split('&',1)
				e5, rest = rest.split('+',1)
				e6, rest = rest.split('#',1)
				e7, rest = rest.split('+',1)
				e8, rest = rest.split('/',1)

				_, rest = rest.split(':',1)
				f1, rest = rest.split('_',1)
				f2, rest = rest.split('/',1)

				_, rest = rest.split(':',1)
				g1, rest = rest.split('_',1)
				g2, rest = rest.split('/',1)

				_, rest = rest.split(':',1)
				h1, rest = rest.split('=',1)
				h2, rest = rest.split('@',1)	# slightly different from the convention
				h3, rest = rest.split('=',1)
				h4, rest = rest.split('|',1)
				h5, rest = rest.split('/',1)

				_, rest = rest.split(':',1)
				i1, rest = rest.split('=',1)
				i2, rest = rest.split('/',1)

				_, rest = rest.split(':',1)
				j1, rest = rest.split('+',1)
				j2, j3 = rest.split('-',1)

				df_row = pd.DataFrame({'t1':t1,'t2':t2,'p1':p1, 'p2':p2, 'p3':p3, 'p4':p4, 'p5':p5, 'p6':p6, 'p7':p7, 'a1':a1, 'a2':a2, 'a3':a3, 'b1':b1, 'b2':b2, 'b3':b3, 'b4':b4, 'b5':b5, 'b6':b6, 'b7':b7, 'b8':b8, 'b9':b9, 'b10':b10, 'b11':b11, 'b12':b12, 'b13':b13, 'b14':b14, 'b15':b15, 'b16':b16, 'c1':c1, 'c2':c2, 'c3':c3, 'd1':d1, 'd2':d2, 'e1':e1, 'e2':e2, 'e3':e3, 'e4':e4, 'e5':e5, 'e6':e6, 'e7':e7, 'e8':e8, 'f1':f1, 'f2':f2, 'g1':g1, 'g2':g2, 'h1':h1, 'h2':h2, 'h3':h3, 'h4':h4, 'h5':h5, 'i1':i1, 'i2':i2, 'j1':j1, 'j2':j2, 'j3':j3}, index=[idx])
				idx += 1
				df = df.append(df_row)

		save_file = os.path.join(output_directory, os.path.splitext(os.path.basename(lab_file))[0]+'.csv')
		# print 'writing label from:',lab_file,'to:',save_file
		df = df[['t1','t2','p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'b10', 'b11', 'b12', 'b13', 'b14', 'b15', 'b16', 'c1', 'c2', 'c3', 'd1', 'd2', 'e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8', 'f1', 'f2', 'g1', 'g2', 'h1', 'h2', 'h3', 'h4', 'h5', 'i1', 'i2', 'j1', 'j2', 'j3']]
		df.to_csv(save_file, index=False)

def create_data_scp(filename_fmt,wav_folder='rwavs',lab_folder='csv_labs',f0_folder='f0',test_samples=30):
	"""
	Arguments
	filename_fmt: Format for WAV files
	wav_folder: Name of folder containing the WAV files
	lab_folder: Name of folder containing the csv label files
	f0_folder: Name of folder containing the F0 files
	test_samples: Number of samples from each folder to hold out as test set
	---
	Return
	Saves scripts to file train_list.scp and test_list.scp
	"""
	all_wav_files = glob(filename_fmt)
	if test_samples>0:
		shuffle(all_wav_files)
		train_wav_files = all_wav_files[:-test_samples]
		test_wav_files = all_wav_files[-test_samples:]
	else:
		train_wav_files = all_wav_files
	train = open('train_list.scp','w')
	for wav_file in train_wav_files:
		par_dir, part_no = os.path.split(os.path.dirname(wav_file))
		par_dir = os.path.dirname(par_dir)
		lab_file = os.path.join(par_dir, lab_folder, part_no, os.path.splitext(os.path.basename(wav_file))[0]+'.csv')
		f0_file = os.path.join(par_dir, f0_folder, part_no, os.path.splitext(os.path.basename(wav_file))[0]+'.txt')
		train.write('{} {} {}\n'.format(wav_file, lab_file, f0_file))
	train.close()

	if test_samples>0:
		test = open('test_list.scp','w')
		for wav_file in test_wav_files:
			par_dir, part_no = os.path.split(os.path.dirname(wav_file))
			par_dir = os.path.dirname(par_dir)
			lab_file = os.path.join(par_dir, lab_folder, part_no, os.path.splitext(os.path.basename(wav_file))[0]+'.csv')
			f0_file = os.path.join(par_dir, f0_folder, part_no, os.path.splitext(os.path.basename(wav_file))[0]+'.txt')
			test.write('{} {} {}\n'.format(wav_file, lab_file, f0_file))
		test.close()

def preprocess_input(file_scp, phone_map_pkl, pos_map_pkl, output_file, inp_dim=None, padding = 1024, pad_row=None, freq = 16000):
	"""
	Arguments
	file_scp: Line containing (space separated) wav_filenm lab_filenm f0_filenm
	phone_map_pkl: File containing the phone to id map
	pos_map_pkl: File containing the POS to id map
	output_file: The output csv file
	inp_dim: The dimension of the input
	padding: The padding data to add between files
	freq: The frequency at which the samples should be recorded
	"""
	if not inp_dim:
		inp_dim = 406

	wav_scale = max(np.abs(np.iinfo(np.int16).min),np.iinfo(np.int16).max)

	with open(phone_map_pkl,'r') as f:
		phone_map = pickle.load(f)

	with open(pos_map_pkl,'r') as f:
		pos_map = pickle.load(f)

	def expand_row(row, phone_map, pos_map):
		x = np.zeros(inp_dim)
		offset = 0

		x[phone_map[str(row['p1'])]]=1
		offset += len(phone_map)

		x[offset+phone_map[str(row['p2'])]]=1
		offset += len(phone_map)

		x[offset+phone_map[str(row['p3'])]]=1
		offset += len(phone_map)

		x[offset+phone_map[str(row['p4'])]]=1
		offset += len(phone_map)

		x[offset+phone_map[str(row['p5'])]]=1
		offset += len(phone_map)

		x[offset]=0 if row['p6']=='x' else int(row['p6'])
		offset += 1

		x[offset]=0 if row['p6']=='x' else int(row['p7'])
		offset += 1

		x[offset] = 1 if row['a1']==1 else 0
		offset += 1

		x[offset] = 1 if row['a2']==1 else 0
		offset += 1

		x[offset] = row['a3']
		offset += 1

		x[offset] = 0 if row['b1']=='x' else int(row['b1'])
		offset += 1

		x[offset] = 0 if row['b2']=='x' else int(row['b2'])
		offset += 1

		x[offset] = 0 if row['b3']=='x' else int(row['b3'])
		offset += 1

		x[offset] = 0 if row['b4']=='x' else int(row['b4'])
		offset += 1

		x[offset] = 0 if row['b5']=='x' else int(row['b5'])
		offset += 1

		x[offset] = 0 if row['b6']=='x' else int(row['b6'])
		offset += 1

		x[offset] = 0 if row['b7']=='x' else int(row['b7'])
		offset += 1

		x[offset] = 0 if row['b8']=='x' else int(row['b8'])
		offset += 1

		x[offset] = 0 if row['b9']=='x' else int(row['b9'])
		offset += 1

		x[offset] = 0 if row['b10']=='x' else int(row['b10'])
		offset += 1

		x[offset] = 0 if row['b11']=='x' else int(row['b11'])
		offset += 1

		x[offset] = 0 if row['b12']=='x' else int(row['b12'])
		offset += 1

		x[offset] = 0 if row['b13']=='x' else int(row['b13'])
		offset += 1

		x[offset] = 0 if row['b14']=='x' else int(row['b14'])
		offset += 1

		x[offset] = 0 if row['b15']=='x' else int(row['b15'])
		offset += 1

		x[offset + phone_map[str(row['b16'])]] = 1
		offset += len(phone_map)

		x[offset] = 0 if row['c1']=='x' else int(row['c1'])
		offset += 1

		x[offset] = 0 if row['c2']=='x' else int(row['c2'])
		offset += 1

		x[offset] = 0 if row['c3']=='x' else int(row['c3'])
		offset += 1

		x[offset + pos_map[str(row['d1'])]] = 1
		offset += len(pos_map)

		x[offset] = 0 if row['d2']=='x' else int(row['d2'])
		offset += 1

		x[offset + pos_map[str(row['e1'])]] = 1
		offset += len(pos_map)

		x[offset] = 0 if row['e2']=='x' else int(row['e2'])
		offset += 1

		x[offset] = 0 if row['e3']=='x' else int(row['e3'])
		offset += 1

		x[offset] = 0 if row['e4']=='x' else int(row['e4'])
		offset += 1

		x[offset] = 0 if row['e5']=='x' else int(row['e5'])
		offset += 1

		x[offset] = 0 if row['e6']=='x' else int(row['e6'])
		offset += 1

		x[offset] = 0 if row['e7']=='x' else int(row['e7'])
		offset += 1

		x[offset] = 0 if row['e8']=='x' else int(row['e8'])
		offset += 1

		x[offset + pos_map[str(row['f1'])]] = 1
		offset += len(pos_map)

		x[offset] = 0 if row['f2']=='x' else int(row['f2'])
		offset += 1

		x[offset] = 0 if row['g1']=='x' else int(row['g1'])
		offset += 1

		x[offset] = 0 if row['g2']=='x' else int(row['g2'])
		offset += 1

		x[offset] = 0 if row['h1']=='x' else int(row['h1'])
		offset += 1

		x[offset] = 0 if row['h2']=='x' else int(row['h2'])
		offset += 1

		x[offset] = 0 if row['h3']=='x' else int(row['h3'])
		offset += 1

		x[offset] = 0 if row['h4']=='x' else int(row['h4'])
		offset += 1

		x[offset] = 0 if row['i1']=='x' else int(row['i1'])
		offset += 1

		x[offset] = 0 if row['i2']=='x' else int(row['i2'])
		offset += 1

		x[offset] = 0 if row['j1']=='x' else int(row['j1'])
		offset += 1

		x[offset] = 0 if row['j2']=='x' else int(row['j2'])
		offset += 1

		x[offset] = 0 if row['j3']=='x' else int(row['j3'])
		offset += 1

		return x

	if not pad_row:
		pad_row_s = pd.Series(data={'p1':'x', 'p2':'x', 'p3':'x', 'p4':'x', 'p5':'x', 'p6':'x', 'p7':'x', 'a1':0, 'a2':0, 'a3':0, 'b1':'x', 'b2':'x', 'b3':'x', 'b4':'x', 'b5':'x', 'b6':'x', 'b7':'x', 'b8':'x', 'b9':'x', 'b10':'x', 'b11':'x', 'b12':'x', 'b13':'x', 'b14':'x', 'b15':'x', 'b16':'x', 'c1':'x', 'c2':'x', 'c3':'x', 'd1':0, 'd2':0, 'e1':0, 'e2':'x', 'e3':'x', 'e4':'x', 'e5':'x', 'e6':'x', 'e7':'x', 'e8':'x', 'f1':0, 'f2':0, 'g1':0, 'g2':0, 'h1':'x', 'h2':'x', 'h3':1, 'h4':1, 'h5':0, 'i1':0, 'i2':0, 'j1':5, 'j2':5, 'j3':5})
		pad_row = expand_row(pad_row_s, phone_map, pos_map)

	with h5py.File(output_file, 'w') as D:
		dx = D.create_dataset('X', (1, 256+inp_dim+2), dtype=np.dtype('float16'), compression="gzip", maxshape=(None, 256+inp_dim+2))
		dy = D.create_dataset('Y', (1, 256), dtype=np.dtype('float16'), compression="gzip", maxshape=(None, 256))

		wav_filenm, lab_filenm, f0_filenm = file_scp.strip().split()
		rate, wav_data = scipy.io.wavfile.read(wav_filenm)
		rate = float(rate)
		# mu-law transformation
		wav_data = wav_data.astype(np.float32)
		wav_data /= wav_scale
		wav_data = np.sign(wav_data)*np.log(1+255*np.abs(wav_data))/np.log(256)
		wav_data = (wav_data+1)*128
		wav_data = np.floor(wav_data).astype(np.int16)
		W = np.zeros((len(wav_data),256), dtype=np.uint8)
		W[range(len(wav_data)), wav_data] = 1

		# sample label file
		X = np.zeros((len(wav_data),inp_dim),dtype=np.uint8)
		lab_df = pd.read_csv(lab_filenm)
		r = 0
		r_changed = False
		for x in range(len(wav_data)):
			t_x = x*1./rate
			while r<len(lab_df)-1 and t_x > lab_df.loc[r,'t2']:
				r_changed = True
				r += 1
			if r_changed or x==0:
				X[x,:] = expand_row(lab_df.loc[r], phone_map, pos_map)
			else:
				X[x,:] = X[x-1,:]
			r_changed = False

		# sample F0 file
		F = np.zeros((len(wav_data),2),dtype=np.float16)
		f0_df = pd.read_csv(f0_filenm, sep='\t')
		r = 0
		for x in range(len(wav_data)):
			t_x = x*1./rate
			while r<len(f0_df)-1 and t_x > f0_df.loc[r,'time']:
				r += 1
			F[x,0] = 0 if f0_df.loc[r,'pitch']=='--undefined--' else 1
			F[x,1] = np.log(float(f0_df.loc[r,'pitch'])) if F[x,0] else 0

		dx.resize((len(wav_data) + padding, 256+inp_dim+2))
		dy.resize((len(wav_data) + padding, 256))
		dx[:len(wav_data) + padding, :] = 0
		dx[:len(wav_data) + padding, 128] = 1
		dx[:padding, 256:256+inp_dim] = pad_row
		dx[padding:padding+len(wav_data), 0:256] = W
		dx[padding-1:padding+len(wav_data)-1, 256:256+inp_dim] = X
		dx[padding-1:padding+len(wav_data)-1, 256+inp_dim:256+inp_dim+2] = F

		dy[:len(wav_data) + padding, :] = 0
		dy[:len(wav_data) + padding, 128] = 1
		dy[padding-1:padding+len(wav_data)-1, 0:256] = W

def hdf5matrix(file, dset):
	"""
	Arguments
	file: The hdf5 data file
	dset: Name of the dataset inside the file
	---
	Return
	Dataset as a Numpy matrix
	"""
	f = h5py.File(file, 'r')
	data = f[dset].value
	f.close()
	return data


try:
    file = open("a.txt")
    dict = {"key": "value"}
    print(dict["key"])
except FileNotFoundError:
    file = open("a.txt", "w")
    file.write("This is something new.")

except KeyError as errorMessage:
    print(f"The {errorMessage} is not exist in dictionary")

else:
    content = file.read()
    print(content)

finally:
    file.close()
    print("file closed.")


from zombie import ZombieApp
import sys
import os

if __name__ == "__main__":
	argv = sys.argv
	if len(argv) < 2:
		raise Exception("Command Arguments should include input file.")
	filepath = argv[1]
	if not os.path.exists(filepath):
		raise Exception("Input file does not exist.")

	with open(filepath, 'r') as f:
		lines = f.readlines()
	# Process the inputs
	grid = int(lines[0])
	zombie_pos = lines[1].rstrip()
	creatures_pos = lines[2].rstrip().split(':')
	movements = lines[3].rstrip()

	zombieApp = ZombieApp(grid, movements)
	pos = zombie_pos.split(',')
	x = int(pos[0])
	y = int(pos[1])
	zombieApp.add_zombie(x, y)

	for raw_pos in creatures_pos:
		pos = raw_pos.split(',')
		x = int(pos[0])
		y = int(pos[1])
		zombieApp.add_creature(x, y)

	zombieApp.run()
	zombies = zombieApp.result()
	# Print result
	final_positions = []
	total_score = 0
	for zombie in zombies:
		total_score += zombie.score
		final_positions.append((zombie.x, zombie.y))

	print("zombies score: " + str(total_score))
	print("zombies final positions: " + str(final_positions))


from django.test import TestCase
from tastypie.test import ResourceTestCase

from django.db import IntegrityError
from django.utils.encoding import smart_unicode

from scenable.places.models import Place, Location, HoursListing
from scenable.tags.models import Tag
from django.db import transaction

# class LocationModelTest(TestCase):
#     @property
#     def valid_location_base(self):
#         '''
#         property to return a basic Location object that is known to be valid
#         for use in tests
#         '''
#         return Location(country='US',
#                         state='PA',
#                         town='Pittsburgh',
#                         neighborhood=None,
#                         postcode='15213',
#                         address='4620 Henry Street',
#                         latitude=40.446487,
#                         longitude=-79.948524)

#     def test_base_is_valid(self):
#         '''Ensures the location used as the basis of many tests is valid'''
#         self.valid_location_base.save()

#     def test_valid_country_format(self):
#         '''Tests that all country codes have either 0 or 2 characters'''
#         # locations with 1 or >2 letters are no good
#         invalid_countries = ['U', 'USA', 'United States']
#         for invalid_country in invalid_countries:
#             l = self.valid_location_base
#             l.country = invalid_country
#             self.assertRaises(ValidationError, l.save)

#         valid_countries = ['', 'US']
#         for valid_country in valid_countries:
#             l = self.valid_location_base
#             l.country = valid_country
#             l.save()

#     def test_valid_state_format(self):
#         '''Tests that all states have either 0 or 2 characters'''
#         invalid_states = ['P', 'PAX', 'Pennsylvania']
#         for invalid_state in invalid_states:
#             l = self.valid_location_base
#             l.state = invalid_state
#             self.assertRaises(ValidationError, l.save)

#         valid_states = ['', 'PA']
#         for valid_state in valid_states:
#             l = self.valid_location_base
#             l.state = valid_state
#             l.save()

#     def test_geocode_bounds(self):
#         '''
#         Ensures latitude and longitude are within accepted ranges.
#         '''
#         # does an exhaustive 3x3 attempt to set various lat/long values, should only succeed once
#         for lat,lat_valid in ((-90.1,False),(90.1,False),(40.4,True)):
#             for lon,lon_valid in ((-180.1,False),(180.1,False),(-80,True)):
#                 location = Location(latitude=lat,longitude=lon)
#                 if lon_valid and lat_valid:
#                     location.save()    # should be ok
#                 else:
#                     with self.assertRaises(ValidationError):
#                         location.save()

#     def test_complete_geocode(self):
#         '''Ensures any Location with a longitude has a latitude and vice-versa.'''
#         l = self.valid_location_base
#         l.longitude = None
#         self.assertRaises(ValidationError,l.save)
#         l = self.valid_location_base
#         l.latitude = None
#         self.assertRaises(ValidationError,l.save)

class PlaceModelTest(TestCase):
    def test_hours_custom_field(self):
        '''
        Tests cases for custom HoursField. Mostly makes sure everything is
        round-trippable.
        '''
        # create place with no hours or one set to empty list
        Place.objects.create(name='empty', hours=[])
        Place.objects.create(name='implicit_empty')

        # test creation of place with most basic hours content
        firm_hours = [HoursListing('Mon-Fri', '9am - 5pm')]
        Place.objects.create(name='firm', hours=firm_hours)

        # test creation of hours with multiple listings
        bank_hours = [
            HoursListing('Mon-Fri', '9am - 5pm'),
            HoursListing('Sat', '10am - 2pm'),
            HoursListing('Sun', 'Closed')
        ]
        Place.objects.create(name='bank', hours=bank_hours)

        # test creation of hours with commas within hours values
        restaurant_hours = [
            HoursListing('Sun', '11am - 10pm'),
            HoursListing('Mon', 'Closed'),
            HoursListing('Mon-Fri', '11am - 2pm, 4pm - 11pm'),
            HoursListing('Sat', '11am - 10pm')
        ]
        Place.objects.create(name='restaurant', hours=restaurant_hours)

        # assert that hours is non-nullable (empty list means no hours)
        # make a savepoint since we're expecting to trash the transaction
        sid = transaction.savepoint()
        with self.assertRaises(IntegrityError):
            Place.objects.create(name='none', hours=None)
        # IntegrityError must've fired, have to roll back
        transaction.savepoint_rollback(sid)

        # test retrieval of above insertions
        Place.objects.get(name='empty').hours == []
        Place.objects.get(name='implicit_empty').hours == []
        Place.objects.get(name='firm').hours == firm_hours
        Place.objects.get(name='bank').hours == bank_hours
        Place.objects.get(name='restaurant').hours == restaurant_hours

class PlaceResourceTest(ResourceTestCase):
    def setUp(self):
        super(PlaceResourceTest, self).setUp()
        self.detailed_place = Place.objects.create(
            name='detailed',
            description='This is a fun place. So many details!',
            location=Location.objects.create(address='5139 Penn Ave',
                town='Pittsburgh', state='PA', postcode='15224',
                latitude='40.465002', longitude='-79.941352'),
            hours=[HoursListing('Mon-Fri', '9am - 5pm'),
                   HoursListing('Sat', '10pm - 4pm'),
                   HoursListing('Sun', 'Closed')],
            # TODO: add parking
            phone='555-5555',
            url='http://example.com',
            fb_id='1234567890',
            twitter_username='twitter',
            listed=True)
        self.detailed_place.tags.add(Tag.objects.create(name='fun'))
        self.detailed_place.tags.add(Tag.objects.create(name='woo-hoo'))

        self.sparse_listed_place1 = Place.objects.create(
            name='sparse listed 1',
            listed=True)
        self.sparse_listed_place2 = Place.objects.create(
            name='sparse listed 2',
            listed=True)
        self.sparse_unlisted_place = Place.objects.create(
            name='sparse unlisted',
            listed=False)

        self.detail_url = '/api/v1/place/%s' % self.detailed_place.id

    def _test_detailed_equality(self, inst, response_dict):
        '''
        Tests each field of a Place object vs. a PlaceResource dict
        '''
        # these fields can be tested with a simple assertEquals
        simple_equality_keys = ['name', 'phone', 'url', 'fb_id', 'listed',
                                'twitter_username', 'description']
        for k in simple_equality_keys:
            self.assertEquals(getattr(inst, k), response_dict.get(k))

        ### more complex equalities tests
        # test locations
        if inst.location is None:
            self.assertEquals(inst.location, response_dict.get('location'))
        else:
            location_equality_keys = ['address', 'town', 'state', 'postcode']
            for k in location_equality_keys:
                self.assertEquals(getattr(inst.location, k), response_dict['location'].get(k))
            self.assertEquals(smart_unicode(inst.location.latitude), response_dict['location'].get('latitude'))
            self.assertEquals(smart_unicode(inst.location.longitude), response_dict['location'].get('longitude'))

        # test resource uri
        self.assertEquals('/api/v1/place/%s/' % inst.id, response_dict.get('resource_uri'))

        # test hours with special logic
        self.assertEquals(len(inst.hours), len(response_dict.get('hours')))
        for detail_listing, resp_listing in zip(inst.hours, response_dict.get('hours')):
            self.assertEquals(detail_listing.days, resp_listing.get('days'))
            self.assertEquals(detail_listing.hours, resp_listing.get('hours'))

        # ensure all tags are aaccounted for (also check id/name)
        inst_tag_dataset = [{'id': tag.id, 'name': tag.name} for tag in inst.tags.order_by('id')]
        response_dict.get('tags').sort(key=lambda d: d.get('id'))
        self.assertEquals(inst_tag_dataset, response_dict.get('tags'))

    def test_get_list(self):
        '''
        Test basic resource endpoint GET request.
        '''
        resp = self.api_client.get('/api/v1/place/', format='json')
        self.assertValidJSONResponse(resp)
        resp = self.deserialize(resp)

        # test expected number of results, and that each id is unique
        self.assertEquals(len(resp['objects']), 4)
        self.assertEquals(len(set([r['id'] for r in resp['objects']])), 4)

        # sanity check for detail item
        detailed_resp = [r for r in resp['objects'] if r['id'] == self.detailed_place.id][0]
        self.assertEquals(self.detailed_place.name, detailed_resp['name'])

    def test_get_listed_only(self):
        '''
        Double check to ensure listed filter is working: important for
        feeds.
        '''
        resp = self.api_client.get('/api/v1/place/?listed=true', format='json')
        self.assertValidJSONResponse(resp)
        resp = self.deserialize(resp)
        self.assertEquals(len(resp['objects']), 3)

    def test_get_detail(self):
        detail_resp = self.api_client.get('/api/v1/place/%s/' % self.detailed_place.id, format='json')
        self.assertValidJSONResponse(detail_resp)
        detail_resp = self.deserialize(detail_resp)
        self._test_detailed_equality(self.detailed_place, detail_resp)

        # ensure the unlisted object can be retrieved directly
        unlisted_resp = self.api_client.get('/api/v1/place/%s/' % self.sparse_unlisted_place.id, format='json')
        self.assertValidJSONResponse(unlisted_resp)
        unlisted_resp = self.deserialize(unlisted_resp)
        self._test_detailed_equality(self.sparse_unlisted_place, unlisted_resp)

# class CloseLocationManagerTest(TestCase):
#     def setUp(self):
#         Location.objects.create(address='5467 Penn Ave', town='Pittsburgh', state='PA',
#                                 postcode='15206', latitude=40.464751, longitude=-79.93344)
#         Location.objects.create(address='5469 Penn Ave', town='Pittsburgh', state='PA',
#                                 postcode='15206', latitude=40.464598, longitude=-79.933881)
#         Location.objects.create(address='5151 Penn Ave', town='Pittsburgh', state='PA',
#                                 postcode='15206', latitude=40.464948, longitude=-79.940899)

#     def test_get_close(self):
#         lat, lng = 40.464237, -79.932940
#         try:
#             Location.close_manager.get_close(address='5467 Penn Ave', latitude=lat, longitude=lng)
#         except Location.DoesNotExist:
#             self.fail('close_get query failed.')

#         # try two tests with the bounds too narrow to find. these should fail to find objects
#         with self.assertRaises(Location.DoesNotExist):
#             Location.close_manager.get_close(address='5467 Penn Ave', latitude=lat, longitude=lng,
#                                         _close_options={'lat_error': 1e-4})
#         with self.assertRaises(Location.DoesNotExist):
#             Location.close_manager.get_close(address='5467 Penn Ave', latitude=lat, longitude=lng,
#                                         _close_options={'lng_error': 1e-4})

#         with self.assertRaises(Location.MultipleObjectsReturned):
#             Location.close_manager.get_close(postcode='15206', latitude=lat, longitude=lng,
#                                         _close_options={'assert_single_match': True})

#     def test_get_close_or_create(self):
#         lat, lng = 40.464237, -79.932940
#         l, created = Location.close_manager.get_close_or_create(address='5467 Penn Ave', latitude=lat, longitude=lng)
#         self.assertFalse(created)
#         l, created = Location.close_manager.get_close_or_create(latitude=lat, longitude=lng)
#         self.assertFalse(created)

#         l, created = Location.close_manager.get_close_or_create(address='5468 Penn Ave', latitude=lat, longitude=lng)
#         self.assertTrue(created)
#         self.assertEquals(l.latitude, lat)
#         self.assertEquals(l.longitude, lng)


from Node import Node

#import the game mechanics
from gameMechanics import *

class UCS:
    pass

    #UCS algorithm to find the shortest path from initial state to goal state
    def search(initialGame):

        #Initialise the game mechanics
        mechanics = gameMechanics(initialGame)

        #Create the root node
        root = Node(None, "Initial State", mechanics.convertedGame, mechanics.intialFuelLevels, mechanics.findMoves(mechanics.convertedGame, mechanics.intialFuelLevels), 0, 0)

        #Create a queue to store nodes
        queue = []

        #Create a list to store visited nodes
        visited = []
        visitedNodes = []

        #Add the root node to the queue
        queue.append(root)

        #While the queue is not empty
        while queue:

            #Pop the first node in the queue
            node = queue.pop(0)

            #If the node is not visited
            if node.game not in visited:

                # Add the node to the visited list
                visited.append(node.game)
                visitedNodes.append(node)

                #If the node is the goal state return the node
                if node.game[2][5] == 'A':
                    return [node, visitedNodes]

                #Add the children of the node to the queue
                for x, child in enumerate(node.children[0]):
                    queue.append(Node(node, node.children[1][x], child, node.children[2][x], mechanics.findMoves(child, node.children[2][x]), node.depth+1, 0))
        return [None, visitedNodes]


import json
import os

R_FILE = "countries.json"

# Первый вариант, без всяких там классов и итераторов //
# R_FILE = "countries.json"
# W_FILE = "countries.txt"
# URL = 'http://en.wikipedia.org/wiki/'
#
# with open(R_FILE, "r") as read_file:
#     data = json.load(read_file)
#
# url_country = {}
# for country in data:
#     country_name = country['name']['common']
#     url_country[country_name] = URL + country_name.replace(' ', '_')
#
# pprint(url_country)
#
# with open(W_FILE, "w", encoding='utf-8') as write_file:
#     for c, u in url_country.items():
#         write_file.write(c + ' - ' + u + '\n')
#

class CountryUrl:
    URL = 'http://en.wikipedia.org/wiki/'

    def __init__(self, file):
        self.url_country = {}
        self.file = file
        self.save_file = os.path.splitext(self.file)[0] + '.txt'
        with open(self.file, "r") as read_file:
            data = json.load(read_file)
        i = 0
        for country in data:
            country_name = country['name']['common']
            self.url_country[i] = country_name
            i += 1

    def get_url(self, country_name):
        return self.URL + country_name.replace(' ', '_')

    def __iter__(self):
        self.cursor = 0
        return self

    def __next__(self):
        self.cursor += 1
        if self.cursor > len(self.url_country) - 2:
            raise StopIteration
        url = self.get_url(self.url_country[self.cursor])
        with open(self.save_file, "a", encoding='utf-8') as write_file:
            write_file.write(self.url_country[self.cursor] + ' - ' + url + '\n')
        return url

countries = CountryUrl(R_FILE)

for country in countries:
    print(country)


import numpy as np
import sys
if '../' not in sys.path:
    sys.path.append('../')

def policy_evaluation(policy,
                      env,
                      theta=1e-9,
                      discount_factor=1.0,
                      max_iterations=1e9):
    """
    Evaluates a policy given the environments' full dynamics
    Args:
        policy: [S, A] shaped matrix representing the policy.
        env: The OpenAI environment where
            env.P represents the transition probs
            env.P[s][a] represents the list of transition tuples (prob, next_state, reward, terminated)
            env.nS represents no of states
            env.nA represents no of Actions
        theta: threshold. Stop evaluation when change is less than theta
        discount_factor: Gamma
    Returns:
        Value functions. Vector of length env.nS
    """
    # random value function with all zeros
    V = np.zeros(env.nS)

    iterations = 1
    while iterations < max_iterations:
        # stopping criteria
        delta = 0
        # evaluate each state
        for state in range(env.nS):
            v = 0

            # evaluate all possible actions
            for a, action_prob in enumerate(policy[state]):
                for (prob, next_state, reward, _) in env.P[state][a]:
                    # expected value
                    v += action_prob * prob * (reward + discount_factor * V[next_state])

            # update delta
            delta = max(delta, np.abs(v - V[state]))

            V[state] = v
        if delta < theta:
            print("Evaluation iterations: {}".format(iterations))
            break

        iterations += 1

    return np.array(V)

def one_step_lookahead(state, V, env, gamma):
    """
    Helper function to calculate value of all actions in a state
    Args:
        state: current state
        V: value functions. A vector of length env.nS
        env: OpenAI environment of FrozenLake
        gamma: discount factor. Relative weightage of future rewards
    Returns:
        A vector of length env.nA represention the value taking an action
    """
    A = np.zeros((env.nA))

    for a in range(env.nA):
        for prob, next_state, reward, _ in env.P[state][a]:
            A[a] += prob * (reward + gamma * V[next_state])
    return A

def policy_iteration(env, theta=1e-9, discount_factor=1.0, max_iterations=1e9):
    """
    Iterative policy improvement. Iteratively evlauates a policy and improves it
    until an optimal policy is found.
    Args:
        env: The OpenAI environment where
            env.P represents the transition probs
            env.P[s][a] represents the list of transition tuples (prob, next_state, reward, terminated)
            env.nS represents no of states
            env.nA represents no of Actions
        theta: Threshold value. Stop policy evaluation if delta is less than theta ( Early Stopping ).
        discount_factor: Relative weightage of future rewards.
    Returns:
    A tuple (policy, V)
        policy: An optimal Policy to behave in the env of shape [nS, nA].
        V: A vector of length nS denoting how good it is to be in a state.
    """
    # create random policy
    policy = np.ones([env.nS, env.nA]) / env.nA
    iteration = 1
    while iteration < max_iterations:
        # evaluation
        V = policy_evaluation(policy, env)
        policy_stable = True

        # iterate through each state and improve policy
        for state in range(env.nS):
            old_action = np.argmax(policy[state])
            action_values = one_step_lookahead(state, V, env, discount_factor)
            best_action = np.argmax(action_values)

            if old_action != best_action:
                policy_stable = False

            # deterministic
            policy[state] = np.eye(env.nA)[best_action]

            # stochastic policy
            # best_actions = np.argwhere(action_values==np.max(action_values)).flatten()
            # policy[state] = np.sum([np.eye(env.nA)[i] for i in best_actions], axis=0) / len(best_actions)

        if policy_stable:
            print("Evaluated policies {}".format(iteration))
            return policy, V

        iteration += 1

def value_iteration(env, theta=1e-9, discount_factor=1.0, max_iteration=1e9):
    """
    Value iteration to solve MDP
    Args:
        env: Open AI environment already initialized.
        theta: thershold for stopping
        discount factor: relative weightage for future rewards
        max_iterations: number of iterations to evaluate for
    """
    V = np.zeros(env.nS)
    iteration =1
    while iteration < max_iteration:
        delta = 0
        for state in range(env.nS):
            v = 0
            action_values = one_step_lookahead(state, V, env, discount_factor)
            best_value = np.max(action_values)
            delta = max(delta, abs(V[state] - best_value))
            V[state] = best_value
        if delta < theta:
            print("Value iteration converger after {} iterations".format(iteration))
            break
        iteration += 1

    policy = np.zeros([env.nS, env.nA])
    for state in range(env.nS):
        action_values = one_step_lookahead(state, V, env, discount_factor)

        # select best action
        best_action = np.argmax(action_values)
        policy[state][best_action] = 1.0

    return policy, V

# [[0 3 3 3]
#  [0 0 0 0]
#  [3 1 0 0]
#  [0 2 1 0]]

# Value Function
# [[0.8235294  0.82352939 0.82352939 0.82352938]
#  [0.8235294  0.         0.52941175 0.        ]
#  [0.8235294  0.8235294  0.76470587 0.        ]
#  [0.         0.88235293 0.94117647 0.        ]]


"""The Detailed Hello World Push integration."""
import asyncio
import logging

import os
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.core import HomeAssistant

from .const import *

_LOGGER = logging.getLogger(__name__)

# List of platforms to support. There should be a matching .py file for each,
# eg <cover.py> and <sensor.py>
PLATFORMS = ["sensor"]

async def async_setup(hass: HomeAssistant, config: dict):
    """Set up the Hello World component."""
    # Ensure our name space for storing objects is a known type. A dict is
    # common/preferred as it allows a separate instance of your class for each
    # instance that has been created in the UI.
    hass.data.setdefault(DOMAIN, {})

    return True

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up Hello World from a config entry."""
    # Store an instance of the "connecting" class that does the work of speaking
    # with your actual devices.
    _LOGGER.debug("call async_setup_entry")
    #hass.data[DOMAIN][entry.entry_id] = DOMAIN
    hass.data[DOMAIN][entry.entry_id] = {}
    hass.data[DOMAIN][entry.entry_id]["listener"] = []

    entry.async_on_unload(entry.add_update_listener(update_listener))
    # This creates each HA object for each platform your device requires.
    # It's done by calling the `async_setup_entry` function in each platform module.
    for component in PLATFORMS:
        hass.async_create_task(
            hass.config_entries.async_forward_entry_setup(entry, component)
        )

    return True

# async def async_remove_config_entry_device(
#     hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
# ) -> bool:
#     """Remove a config entry from a device."""
#     _LOGGER.debug("async_remove_config_entry_device : " + str(device_entry))

#     device_entry.config_entries.remove(config_entry.entry_id)
#     #config_entry.async_remove(hass)

async def update_listener(hass, entry):
    """Handle options update."""
    await hass.config_entries.async_reload(entry.entry_id)

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
    """Unload a config entry."""
    # This is called when an entry/configured device is to be removed. The class
    # needs to unload itself, and remove callbacks. See the classes for further
    # details
    _LOGGER.debug("call async_unload_entry")

    for listener in hass.data[DOMAIN][entry.entry_id]["listener"]:
        listener()

    unload_ok = all(
        await asyncio.gather(
            *[
                hass.config_entries.async_forward_entry_unload(
                    entry, component)
                for component in PLATFORMS
            ]
        )
    )

    _LOGGER.debug("entry : " + str(entry.options))
    data_dir = entry.options.get(CONF_DATA_DIR)
    if os.path.isdir(data_dir):
        file_list = os.listdir(data_dir)
        if len(file_list) <= 0:
            _LOGGER.debug("remove dir : %s", data_dir)
            os.removedirs(data_dir)

    if unload_ok:
        hass.data[DOMAIN].pop(entry.entry_id)

    return unload_ok


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct  4 13:22:11 2021

@author: mariapalazzi
"""
import random
import numpy as np
from scipy.optimize import root
from required_functions_null_models import avg_degrees_expoRG
#%%
def exponentialRG_proportional_proportional(M):
    '''
    Solve the system of equations of the maximum log-likelihood problem.
    The system of equations is solved using scipy's root function with
    the solver defined by 'method'.
    The solutions correspond to the Lagrange multipliers. We used the Least-squares
    method='lm' for the Levenberg-Marquardt approach.

    It can happen that the solver ``method`` used by ``scipy.root``
    does not converge to a solution.

    Payrato et al (PRX 2019) and F. Saracco et al (Sci Rep 2015)

    Parameters
    ----------
    M : array
        the binary biadjacency matrix.

    Returns
    -------
    Pij : array, floats
        The probability matrix.
        Given the biadjacency matrix M, which describes the probabilities of
        row-node r and column-node c being linked.

    '''
    R,C=M.shape #rows and cols
    #degrees of "real" matrix
    cols_degr = M.sum(axis=0)
    rows_degr = M.sum(axis=1)
    x0=np.random.uniform(0,1,size=(R+C))
    Pij = np.zeros((R,C));

    solution_vector =  root(avg_degrees_expoRG, x0,args=(rows_degr,cols_degr),method='lm')

    print("Solver successful for lagrange multipliers?", solution_vector.success)
    print(solution_vector.message)
    if solution_vector.success==False:
        Pij=Pij*np.nan
        print('Solution did not converge, returning P_ij with NAN')
        return Pij
    x = solution_vector.x[0:R]
    y = solution_vector.x[R:R+C]

    for p_index in range(len(rows_degr)):
        for a_index in range(len(cols_degr)):
            Pij[p_index,a_index] = (x[p_index]*y[a_index])/(1+x[p_index]*y[a_index])

    return Pij
#%%
def bascompte_probabilistic_proportional(M):
    '''
    Bascopmte's probabilistic null model. Bacompte PNAS 2003.

    Parameters
    ----------
    M : array
        the binary biadjacency matrix.

    Returns
    -------
    p_ij : array
        The probability matrix.
        Given the biadjacency matrix M, which describes the probabilities of
        row-node r and column-node c being linked.

    '''
    rows,cols=M.shape
    # dregrees of the rows and cols nodes
    cols_degr = M.sum(axis=0)
    rows_degr = M.sum(axis=1)
    # normalized degrees
    rows_degr_norm = rows_degr/cols
    cols_degr_norm = cols_degr/rows

  #  M_null=np.zeros((rows,cols),dtype=int)
    M_n=np.zeros((rows,cols))

  #  M_rand_ij=np.random.uniform(0,1,size=(rows,cols))

    #obtaining the matrix of probabilities
    for i in range(rows):
        for j in range(cols):
            p_ij=0.5*(cols_degr_norm[j] + rows_degr_norm[i])
            M_n[i,j]=p_ij

    #null matrix
    #M_null=(M_n>=M_rand_ij).astype(int)
    return M_n
#%%
def corrected_probabilistic_proportional(M):
    '''
    corrected probabilistic null model, a variation of Bascompte's model

    Parameters
    ----------
    M : array
        the binary biadjacency matrix.

    Returns
    -------
    p_ij : array
        The probability matrix.
        Given the biadjacency matrix M, which describes the probabilities of
        row-node r and column-node c being linked.

    '''
    rows,cols=M.shape
    # dregrees of the rows and cols nodes
    cols_degr = M.sum(axis=0)
    rows_degr = M.sum(axis=1)
    # normalized dregrees
    rows_degr_norm = rows_degr/cols
    cols_degr_norm = cols_degr/rows
    degr_rows_from_cols_sampling = (cols_degr_norm.sum())/cols
    degr_cols_from_rows_sampling = (rows_degr_norm.sum())/rows

    #M_null=np.zeros((rows,cols),dtype=int)
    M_n=np.zeros((rows,cols))

    #M_rand_ij=np.random.uniform(0,1,size=(rows,cols))

    #obtaining the matrix of probabilities
    for i in range(rows):
        for j in range(cols):
            p_ij=0.5*(cols_degr_norm[j] +
                                    ((1 - cols_degr_norm[j])*(rows_degr_norm[i] - degr_rows_from_cols_sampling)) +
                      rows_degr_norm[i] +
                                    ((1 - rows_degr_norm[i])*(cols_degr_norm[j] - degr_cols_from_rows_sampling)) )
            if p_ij<0:
                p_ij=0
            M_n[i,j]=p_ij

    #null matrix
   # M_null=(M_n>=M_rand_ij).astype(int)
    # if symmetric==True:
    #     np.fill_diagonal(M_null, 0)
    #     M_=np.triu(M_null,k=1)+(np.triu(M_null,k=1)).T
    # else:
    #     M_=M_null
    return M_n
#%%
def equiprobable_equiprobable(matrix):
    '''
    Function to generate randomization with equiprobable degrees.

    Inputs:
    ----------
    input_matrix: array
        the binary biadjacency matrix

    output:
    ----------
    result: array
        randomized version of the original matrix with equiprobable degrees
    '''
    matrix=np.array(matrix)
    R,C=matrix.shape
    occs=matrix.sum()
    fill=occs/float(R*C)
    rm=np.zeros([R,C])
    while rm.sum()<occs:
        rr,rc=random.randrange(R),random.randrange(C)
        if random.random()<=fill:
            rm[rr][rc]=1
    return rm.astype(int)
#%%
def curve_ball(m,presences,num_iterations=-1):
    '''
    Function to generate randomization with fixed degrees.
    FF null model, Curveball (Strona et al. 2014)

    Inputs:
    ----------
    input_matrix: array
        the binary biadjacency matrix
    num_iterations: int
        Number of pair comparisons for each matrix generation, if empty, it takes
        the smallets dimension times five.
    presences: list of lists (int)
        a list of list containing the indices of column (or rows) nodes to
        wich each row (or column) node has a link

    output:
    ----------
    result: array
        randomized version of the original matrix with fixed degrees
    '''
    r_hp=presences.copy()
    num_rows, num_cols = m.shape
    num_iters = 5 * min(num_rows, num_cols) if num_iterations == -1 else num_iterations
    for rep in range(num_iters):
        ab = random.sample(range(len(r_hp)), 2) #randomly select two nodes
        a = ab[0]
        b = ab[1]
        ab = set(r_hp[a]) & set(r_hp[b])# overlap between the two nodes
        a_ba = set(r_hp[a]) - ab #other links of node a
        if len(a_ba) != 0:
            b_aa = set(r_hp[b]) - ab #other links of node b
            if len(b_aa) != 0:
                ab = list(ab)
                a_ba = list(a_ba)
                b_aa = list(b_aa)
                random.shuffle(a_ba)
                random.shuffle(b_aa)
                max_swap_extent=min(len(a_ba), len(b_aa)) #max value for pair extractions
                swap_extent = random.randint(1,max_swap_extent)
                #pair extractions
                r_hp[a] = ab+a_ba[:-swap_extent]+b_aa[-swap_extent:]
                r_hp[b] = ab+b_aa[:-swap_extent]+a_ba[-swap_extent:]
    out_mat = np.zeros([num_rows, num_cols], dtype='int8') if num_cols >= num_rows else np.zeros([num_cols,num_rows], dtype='int8')
    for r in range(min(num_rows, num_cols)):
        out_mat[r, r_hp[r]] = 1
        result = out_mat if num_cols >= num_rows else out_mat.T
    return result


from django.conf.urls import url

from . import views

urlpatterns = [
    # Viewing
    url(r'^$',
        views.PublicResultsIndexView.as_view(),
        name='results-public-index'),
    url(r'^round/(?P<round_seq>\d+)/$',
        views.PublicResultsForRoundView.as_view(),
        name='results-public-round'),
    url(r'^debate/(?P<pk>\d+)/scoresheets/$',
        views.PublicBallotScoresheetsView.as_view(),
        name='results-public-scoresheet-view'),

    # Public Ballots
    url(r'^add/$',
        views.PublicBallotSubmissionIndexView.as_view(),
        name='results-public-ballot-submission-index'),
    url(r'^add/adjudicator/(?P<adj_id>\d+)/$',
        views.PublicNewBallotSetByIdUrlView.as_view(),
        name='results-public-ballotset-new-pk'),

    # Private Ballots
    url(r'^add/a(?P<url_key>\w+)/$',
        views.PublicNewBallotSetByRandomisedUrlView.as_view(),
        name='results-public-ballotset-new-randomised'),

    url(r'^added/$',
        views.PostPublicBallotSetSubmissionURLView.as_view(),
        name='post-results-public-ballotset-new'),
]