import sys
_module = sys.modules[__name__]
del sys
convert_to_cpu = _module
lm = _module
load_from_numpy = _module
models = _module
visualize = _module

from _paritybench_helpers import _mock_config, patch_functional
from unittest.mock import mock_open, MagicMock
from torch.autograd import Function
from torch.nn import Module
import abc, collections, copy, enum, functools, inspect, itertools, logging, math, matplotlib, numbers, numpy, pandas, queue, random, re, scipy, sklearn, string, tensorflow, time, torch, torchaudio, torchtext, torchvision, types, typing, uuid, warnings
import numpy as np
from torch import Tensor
patch_functional()
open = mock_open()
yaml = logging = sys = argparse = MagicMock()
ArgumentParser = argparse.ArgumentParser
_global_config = args = argv = cfg = config = params = _mock_config()
argparse.ArgumentParser.return_value.parse_args.return_value = _global_config
yaml.load.return_value = _global_config
sys.argv = _global_config
__version__ = '1.0.0'
xrange = range
wraps = functools.wraps

import torch

from torch.autograd import Variable

from torch import optim

import torch.nn.functional as F

import torch.nn as nn

import numpy as np

import time

import math

class mLSTM(nn.Module):

    def __init__(self, data_size, hidden_size, n_layers=1):
        super(mLSTM, self).__init__()
        self.hidden_size = hidden_size
        self.data_size = data_size
        self.n_layers = n_layers
        input_size = data_size + hidden_size
        self.wx = nn.Linear(data_size, 4 * hidden_size, bias=False)
        self.wh = nn.Linear(hidden_size, 4 * hidden_size, bias=True)
        self.wmx = nn.Linear(data_size, hidden_size, bias=False)
        self.wmh = nn.Linear(hidden_size, hidden_size, bias=False)

    def forward(self, data, last_hidden):
        hx, cx = last_hidden
        m = self.wmx(data) * self.wmh(hx)
        gates = self.wx(data) + self.wh(m)
        i, f, o, u = gates.chunk(4, 1)
        i = F.sigmoid(i)
        f = F.sigmoid(f)
        u = F.tanh(u)
        o = F.sigmoid(o)
        cy = f * cx + i * u
        hy = o * F.tanh(cy)
        return hy, cy

class StackedLSTM(nn.Module):

    def __init__(self, cell, num_layers, input_size, rnn_size, output_size, dropout):
        super(StackedLSTM, self).__init__()
        self.dropout = nn.Dropout(dropout)
        self.num_layers = num_layers
        self.rnn_size = rnn_size
        self.h2o = nn.Linear(rnn_size, output_size)
        self.layers = []
        for i in range(num_layers):
            layer = cell(input_size, rnn_size)
            self.add_module('layer_%d' % i, layer)
            self.layers += [layer]
            input_size = rnn_size

    def forward(self, input, hidden):
        h_0, c_0 = hidden
        h_1, c_1 = [], []
        for i, layer in enumerate(self.layers):
            h_1_i, c_1_i = layer(input, (h_0[i], c_0[i]))
            if i == 0:
                input = h_1_i
            else:
                input = input + h_1_i
            if i != len(self.layers):
                input = self.dropout(input)
            h_1 += [h_1_i]
            c_1 += [c_1_i]
        h_1 = torch.stack(h_1)
        c_1 = torch.stack(c_1)
        output = self.h2o(input)
        return (h_1, c_1), output

    def state0(self, batch_size):
        h_0 = Variable(torch.zeros(self.num_layers, batch_size, self.rnn_size), requires_grad=False)
        c_0 = Variable(torch.zeros(self.num_layers, batch_size, self.rnn_size), requires_grad=False)
        return h_0, c_0


import _diffcp
import cvxpy as cp
import numpy as np
import pytest
from _diffcp import Cone, ConeType
from scipy import sparse

import diffcp.cone_program as cone_prog
import diffcp.cones as cone_lib
import diffcp.utils as utils

CPP_CONES_TO_SCS = {
    ConeType.ZERO: cone_lib.EQ_DIM,
    ConeType.POS: "l",
    ConeType.SOC: "q",
    ConeType.PSD: "s",
    ConeType.EXP: "ep",
    ConeType.EXP_DUAL: "ed"
}

def test_vec_psd_dim():
    assert cone_lib.vec_psd_dim(10) == (10) * (10 + 1) / 2

def test_psd_dim():
    n = 4096
    assert cone_lib.psd_dim(cone_lib.vec_psd_dim(n)) == n

def test_in_exp():
    assert _diffcp.in_exp(np.array([0, 0, 1]))
    assert _diffcp.in_exp(np.array([-1, 0, 0]))
    assert _diffcp.in_exp(np.array([1, 1, 5]))
    assert not _diffcp.in_exp(np.array([1, 0, 0]))
    assert not _diffcp.in_exp(np.array([-1, -1, 1]))
    assert not _diffcp.in_exp(np.array([-1, 0, -1]))

def test_in_exp_dual():
    assert _diffcp.in_exp_dual(np.array([0, 1, 1]))
    assert _diffcp.in_exp_dual(np.array([-1, 1, 5]))
    assert not _diffcp.in_exp_dual(np.array([0, -1, 1]))
    assert not _diffcp.in_exp_dual(np.array([0, 1, -1]))

def test_unvec_symm():
    np.random.seed(0)
    n = 5
    x = np.random.randn(n, n)
    x = x + x.T
    np.testing.assert_allclose(
        cone_lib.unvec_symm(cone_lib.vec_symm(x), n), x)

def test_vec_symm():
    np.random.seed(0)
    n = 5
    x = np.random.randn(cone_lib.vec_psd_dim(n))
    np.testing.assert_allclose(
        cone_lib.vec_symm(cone_lib.unvec_symm(x, n)), x)

def test_proj_zero():
    np.random.seed(0)
    n = 100
    for _ in range(10):
        x = np.random.randn(n)
        np.testing.assert_allclose(
            cone_lib._proj(x, cone_lib.ZERO, dual=True), x)
        np.testing.assert_allclose(
            cone_lib._proj(x, cone_lib.ZERO, dual=False),
            np.zeros(n))

def test_proj_pos():
    np.random.seed(0)
    n = 100
    for _ in range(15):
        x = np.random.randn(n)
        p = cone_lib._proj(x, cone_lib.POS, dual=False)
        np.testing.assert_allclose(p, np.maximum(x, 0))
        np.testing.assert_allclose(
            p, cone_lib._proj(x, cone_lib.POS, dual=True))

def test_proj_soc():
    np.random.seed(0)
    n = 100
    for _ in range(15):
        x = np.random.randn(n)
        z = cp.Variable(n)
        objective = cp.Minimize(cp.sum_squares(z - x))
        constraints = [cp.norm(z[1:], 2) <= z[0]]
        prob = cp.Problem(objective, constraints)
        prob.solve(solver="SCS", eps=1e-10)
        p = cone_lib._proj(x, cone_lib.SOC, dual=False)
        np.testing.assert_allclose(
            p, np.array(z.value))
        np.testing.assert_allclose(
            p, cone_lib._proj(x, cone_lib.SOC, dual=True))

def test_proj_psd():
    np.random.seed(0)
    n = 10
    for _ in range(15):
        x = np.random.randn(n, n)
        x = x + x.T
        x_vec = cone_lib.vec_symm(x)
        z = cp.Variable((n, n), PSD=True)
        objective = cp.Minimize(cp.sum_squares(z - x))
        prob = cp.Problem(objective)
        prob.solve(solver="SCS", eps=1e-10)
        p = cone_lib.unvec_symm(
            cone_lib._proj(x_vec, cone_lib.PSD, dual=False), n)
        np.testing.assert_allclose(p, z.value, atol=1e-5, rtol=1e-5)
        np.testing.assert_allclose(p, cone_lib.unvec_symm(
            cone_lib._proj(x_vec, cone_lib.PSD, dual=True), n))

def test_proj_exp():
    np.random.seed(0)
    for _ in range(15):
        x = np.random.randn(9)
        var = cp.Variable(9)
        constr = [cp.constraints.ExpCone(var[0], var[1], var[2])]
        constr.append(cp.constraints.ExpCone(var[3], var[4], var[5]))
        constr.append(cp.constraints.ExpCone(var[6], var[7], var[8]))
        obj = cp.Minimize(cp.norm(var[0:3] - x[0:3]) +
                          cp.norm(var[3:6] - x[3:6]) +
                          cp.norm(var[6:9] - x[6:9]))
        prob = cp.Problem(obj, constr)
        prob.solve(solver="SCS", eps=1e-12, max_iters=10_000)
        p = cone_lib._proj(x, cone_lib.EXP, dual=False)
        np.testing.assert_allclose(p, var.value, atol=1e-4, rtol=1e-7)
        # x + Pi_{exp}(-x) = Pi_{exp_dual}(x)
        p_dual = cone_lib._proj(x, cone_lib.EXP_DUAL, dual=False)
        var = cp.Variable(9)
        constr = [cp.constraints.ExpCone(var[0], var[1], var[2])]
        constr.append(cp.constraints.ExpCone(var[3], var[4], var[5]))
        constr.append(cp.constraints.ExpCone(var[6], var[7], var[8]))
        obj = cp.Minimize(cp.norm(var[0:3] + x[0:3]) +
                          cp.norm(var[3:6] + x[3:6]) +
                          cp.norm(var[6:9] + x[6:9]))
        prob = cp.Problem(obj, constr)
        prob.solve(solver="SCS", eps=1e-12)
        np.testing.assert_allclose(
            p_dual, x + var.value, atol=1e-6)

def _test_dproj(cone, dual, n, x=None, tol=1e-8):
    if x is None:
        x = np.random.randn(n)
    dx = 1e-6 * np.random.randn(n)
    proj_x = cone_lib._proj(x, CPP_CONES_TO_SCS[cone.type], dual)
    z = cone_lib._proj(x + dx, CPP_CONES_TO_SCS[cone.type], dual)

    Dpi = _diffcp.dprojection(x, [cone], dual)
    np.testing.assert_allclose(Dpi.matvec(dx), z - proj_x, atol=tol)

    Dpi_dense = _diffcp.dprojection_dense(x, [cone], dual)
    np.testing.assert_allclose(Dpi_dense @ dx, z - proj_x, atol=tol)

    # assure that dense and linear operator are the same.
    for i in range(n):
        ei = np.zeros(n)
        ei[i] = 1.0
        np.testing.assert_allclose(Dpi.matvec(ei), Dpi_dense[:, i])

def test_dproj_zero():
    np.random.seed(0)
    for _ in range(10):
        _test_dproj(Cone(ConeType.ZERO, [55]), True, 55)
        _test_dproj(Cone(ConeType.ZERO, [55]), False, 55)

def test_dproj_pos():
    np.random.seed(0)
    for _ in range(10):
        _test_dproj(Cone(ConeType.POS, [55]), True, 55)
        _test_dproj(Cone(ConeType.POS, [55]), False, 55)

def test_dproj_soc():
    np.random.seed(0)
    for _ in range(10):
        _test_dproj(Cone(ConeType.SOC, [55]), True, 55)
        _test_dproj(Cone(ConeType.SOC, [55]), False, 55)

def test_dproj_psd():
    np.random.seed(0)
    for _ in range(10):
        # n=55 equals k * (k + 1) / 2
        _test_dproj(
            Cone(ConeType.PSD, [cone_lib.psd_dim(55)]), True, 55)
        _test_dproj(
            Cone(ConeType.PSD, [cone_lib.psd_dim(55)]), False, 55)

def test_dproj_exp():
    np.random.seed(0)
    for _ in range(10):
        # dimension must be a multiple of 3
        _test_dproj(Cone(ConeType.EXP, [18]), True, 54, tol=1e-5)
        _test_dproj(Cone(ConeType.EXP, [18]), False, 54, tol=1e-5)

def test_dproj_exp_dual():
    np.random.seed(0)
    for _ in range(10):
        # dimension must be a multiple of 3
        _test_dproj(Cone(ConeType.EXP_DUAL, [18]), True, 54, tol=1e-5)
        _test_dproj(
            Cone(ConeType.EXP_DUAL, [18]), False, 54, tol=1e-5)

def test_pi():
    np.random.seed(0)
    for _ in range(10):
        zero_dim = np.random.randint(1, 10)
        pos_dim = np.random.randint(1, 10)
        soc_dim = [np.random.randint(1, 10) for _ in range(
            np.random.randint(1, 10))]
        psd_dim = [np.random.randint(1, 10) for _ in range(
            np.random.randint(1, 10))]
        exp_dim = np.random.randint(3, 18)
        cones = [(cone_lib.ZERO, zero_dim), (cone_lib.POS, pos_dim),
                 (cone_lib.SOC, soc_dim), (cone_lib.PSD, psd_dim),
                 (cone_lib.EXP, exp_dim), (cone_lib.EXP_DUAL, exp_dim)]
        size = zero_dim + pos_dim + sum(soc_dim) + sum(
            [cone_lib.vec_psd_dim(d) for d in psd_dim]) + 2 * 3 * exp_dim
        x = np.random.randn(size)
        for dual in [False, True]:
            proj = cone_lib.pi(x, cones, dual=dual)

            offset = 0
            np.testing.assert_allclose(proj[:zero_dim],
                                       cone_lib._proj(x[:zero_dim], cone_lib.ZERO, dual=dual))
            offset += zero_dim

            np.testing.assert_allclose(proj[offset:offset + pos_dim],
                                       cone_lib._proj(x[offset:offset + pos_dim], cone_lib.POS,
                                                      dual=dual))
            offset += pos_dim

            for dim in soc_dim:
                np.testing.assert_allclose(proj[offset:offset + dim],
                                           cone_lib._proj(x[offset:offset + dim], cone_lib.SOC,
                                                          dual=dual))
                offset += dim

            for dim in psd_dim:
                dim = cone_lib.vec_psd_dim(dim)
                np.testing.assert_allclose(proj[offset:offset + dim],
                                           cone_lib._proj(x[offset:offset + dim], cone_lib.PSD,
                                                          dual=dual))
                offset += dim

            dim = 3 * exp_dim
            np.testing.assert_allclose(proj[offset:offset + dim],
                                       cone_lib._proj(x[offset:offset + dim], cone_lib.EXP, dual=dual))
            offset += dim

            np.testing.assert_allclose(proj[offset:],
                                       cone_lib._proj(x[offset:], cone_lib.EXP_DUAL, dual=dual))

def test_dpi():
    np.random.seed(0)
    for _ in range(10):
        zero_dim = np.random.randint(1, 10)
        pos_dim = np.random.randint(1, 10)
        soc_dim = [np.random.randint(1, 10) for _ in range(
            np.random.randint(1, 10))]
        psd_dim = [np.random.randint(1, 10) for _ in range(
            np.random.randint(1, 10))]
        exp_dim = np.random.randint(3, 18)
        cones = [(cone_lib.ZERO, zero_dim), (cone_lib.POS, pos_dim),
                 (cone_lib.SOC, soc_dim), (cone_lib.PSD, psd_dim),
                 (cone_lib.EXP, exp_dim), (cone_lib.EXP_DUAL, exp_dim)]
        size = zero_dim + pos_dim + sum(soc_dim) + sum(
            [cone_lib.vec_psd_dim(d) for d in psd_dim]) + 2 * 3 * exp_dim
        x = np.random.randn(size)

        for dual in [False, True]:
            cone_list_cpp = cone_lib.parse_cone_dict_cpp(cones)
            proj_x = cone_lib.pi(x, cones, dual=dual)
            dx = 1e-7 * np.random.randn(size)
            z = cone_lib.pi(x + dx, cones, dual=dual)

            Dpi = _diffcp.dprojection(x, cone_list_cpp, dual)
            np.testing.assert_allclose(
                Dpi.matvec(dx), z - proj_x, atol=1e-6)

            Dpi = _diffcp.dprojection_dense(x, cone_list_cpp, dual)
            np.testing.assert_allclose(Dpi @ dx, z - proj_x, atol=1e-6)

def test_get_random_like():
    np.random.seed(0)
    A = sparse.eye(5)
    B = utils.get_random_like(A,
                              lambda n: np.random.normal(0, 1e-6, size=n))
    A_r, A_c = [list(x) for x in A.nonzero()]
    B_r, B_c = [list(x) for x in B.nonzero()]
    assert A_r == B_r
    assert A_c == B_c

def test_infeasible():
    np.random.seed(0)
    c = np.ones(1)
    b = np.array([1.0, -1.0])
    A = sparse.csc_matrix(np.ones((2, 1)))
    cone_dims = {"f": 2}
    with pytest.raises(cone_prog.SolverError, match=r"Solver scs returned status.*"):
        cone_prog.solve_and_derivative(A, b, c, cone_dims)

def test_lsqr():
    np.random.seed(0)
    A = np.random.randn(20, 10)
    b = np.random.randn(20)

    b_copy = b.copy()
    X = _diffcp.lsqr_sparse(sparse.csc_matrix(A), b)
    np.testing.assert_equal(b_copy, b)

    svx = np.linalg.lstsq(A, b, rcond=None)[0]
    xo = X.solution
    np.testing.assert_allclose(svx, xo, err_msg=(
            "istop: %d, itn: %d" % (X.istop, X.itn)))

def test_get_nonzeros():
    np.random.seed(0)
    A = sparse.csc_matrix(np.random.randn(4, 3))
    assert A.nnz, 4 * 3
    A[1, 1] = 0.0
    assert A.nnz, 4 * 3
    import copy
    A_copy = copy.deepcopy(A)
    A.data[A.data == 0.0] = np.nan
    rows, cols = A.nonzero()
    assert rows.size, 4 * 3
    assert cols.size, 4 * 3
    A.data[np.isnan(A.data)] = 0.0
    np.testing.assert_equal(A.data, A_copy.data)
    assert A.nnz, 4 * 3
    assert A.nonzero()[0].size, 4 * 3 - 1
    A.eliminate_zeros()
    assert A.nnz, 4 * 3 - 1

def test_dprojection_exp():
    # variables
    x_ = cp.Variable()

    # parameters
    _lam = cp.Parameter(1, nonneg=True)
    _lam.value = np.ones(1)

    # objective
    objective = cp.Maximize(x_ + _lam*(cp.log(1+x_) + cp.log(1-x_)))

    problem = cp.Problem(objective)

    A, b, c, cone_dims = utils.scs_data_from_cvxpy_problem(problem)

    x, y, s, D, DT = cone_prog.solve_and_derivative(A,
                                                    b,
                                                    c,
                                                    cone_dims,
                                                    solve_method="ECOS",
                                                    mode="dense")

    dlam = 1e-6
    dA = utils.get_random_like(A, lambda n: np.zeros(n))
    db = np.zeros(b.size)
    dc = np.zeros(c.size)
    dc[1] = dc[1] - dlam  # the minus sign stems from the fact that c =  [-1., -1., -1.]
    dc[2] = dc[2] - dlam  # the minus sign stems from the fact that c =  [-1., -1., -1.]

    dx, dy, ds = D(dA, db, dc)

    analytical = -1+_lam.value/np.sqrt(_lam.value**2+1)

    assert abs(analytical[0]-dx[0]/dlam) < 1e-6


class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

def build_tree_from_pre_in(preorder, inorder):
    if not preorder or not inorder:
        return None

    root_val = preorder[0]
    root = TreeNode(root_val)

    idx = inorder.index(root_val)

    root.left = build_tree_from_pre_in(preorder[1:1+idx], inorder[:idx])
    root.right = build_tree_from_pre_in(preorder[1+idx:], inorder[idx+1:])

    return root

def build_tree_from_post_in(postorder, inorder):
    if not postorder or not inorder:
        return None

    root_val = postorder[-1]
    root = TreeNode(root_val)

    idx = inorder.index(root_val)

    root.left = build_tree_from_post_in(postorder[:idx], inorder[:idx])
    root.right = build_tree_from_post_in(postorder[idx:-1], inorder[idx+1:])

    return root

def are_traversals_same_tree(preorder, inorder, postorder):
    # Build trees from the given traversals
    tree_pre_in = build_tree_from_pre_in(preorder, inorder)
    tree_post_in = build_tree_from_post_in(postorder, inorder)

    # Compare the resulting trees
    return are_trees_equal(tree_pre_in, tree_post_in)

def are_trees_equal(root1, root2):
    if root1 is None and root2 is None:
        return True
    if root1 is None or root2 is None:
        return False

    return (root1.val == root2.val and
            are_trees_equal(root1.left, root2.left) and
            are_trees_equal(root1.right, root2.right))

# Test the function with the given examples
inorder1 = [4, 2, 5, 1, 3]
preorder1 = [1, 2, 4, 5, 3]
postorder1 = [4, 5, 2, 3, 1]

inorder2 = [4, 2, 5, 1, 3]
preorder2 = [1, 5, 4, 2, 3]
postorder2 = [4, 1, 2, 3, 5]

print("Output:")
print("Example 1:", "Yes" if are_traversals_same_tree(preorder1, inorder1, postorder1) else "No")
print("Example 2:", "Yes" if are_traversals_same_tree(preorder2, inorder2, postorder2) else "No")

"""OUTPUT
Output:
Example 1: Yes
Example 2: No
"""


from graphics import *

win = GraphWin("Tic-Tac-Toe", 300, 300)
win.setBackground("yellow")

l1 = Line(Point(100,0), Point(100,300))
l1.setFill("blue")
l1.setWidth(10)
l1.draw(win)

l2 = Line(Point(200,0), Point(200,300))
l2.setFill("blue")
l2.setWidth(10)
l2.draw(win)

l3 = Line(Point(0,100), Point(300,100))
l3.setFill("blue")
l3.setWidth(10)
l3.draw(win)

l4 = Line(Point(0,200), Point(300,200))
l4.setFill("blue")
l4.setWidth(10)
l4.draw(win)

seeds = ['X', 'O']
colors = ['red', 'black']
for i in range(9):
    pt = win.getMouse()
    seed = Text(pt, seeds[i%2])
    seed.setFill(colors[i%2])
    seed.setSize(36)
    seed.draw(win)

win.close()


from datetime import datetime, timezone

from django.contrib.auth.models import User
from django.db import IntegrityError, transaction
from django.test import TestCase

from my_app import models

class TestUser(TestCase):

    def test_create_user(self):
        with self.subTest('It is possible to create a user without parameters'):
            user = User.objects.create()
            self.assertEqual('', user.username, 'By default the username is an empty string')

        with self.subTest('A created user can be fetched with the ORM'):
            self.assertTrue(User.objects.filter(pk=user.pk).exists())

    def test_username_is_unique(self):
        with self.subTest('Creating a user with the same username is'):
            User.objects.create(username='foo')
            with self.assertRaises(IntegrityError), transaction.atomic():
                User.objects.create(username='foo')

        User.objects.create(username='bar')
        self.assertTrue(User.objects.filter(username='bar').exists())

class TestUserFixture(TestCase):
    fixtures = ['test-user']

    def test_user_exists(self):
        self.assertTrue(User.objects.filter(pk=1).exists())

class TestEntryModel(TestCase):

    def test_create_instance(self):
        blog = models.Blog.objects.create()

        entry = models.Entry.objects.create(
            pub_date=datetime.now(timezone.utc),
            mod_date=datetime.now(timezone.utc),
            n_comments=0,
            n_pingbacks=0,
            rating=0,
            blog=blog,
        )
        self.assertTrue(models.Entry.objects.filter(pk=entry.pk).exists())

    def test_create_instance_with_full_clean(self):
        blog = models.Blog.objects.create()

        entry = models.Entry.objects.create(
            pub_date=datetime.now(timezone.utc),
            mod_date=datetime.now(timezone.utc),
            n_comments=0,
            n_pingbacks=0,
            rating=0,
            blog=blog,
            headline='Lorem ipsum',
            body_text='Lorem ipsum',
        )
        entry.full_clean()
        self.assertTrue(models.Entry.objects.filter(pk=entry.pk).exists())

    def test_can_add_to_related(self):
        blog = models.Blog.objects.create()

        entry = models.Entry.objects.create(
            pub_date=datetime.now(timezone.utc),
            mod_date=datetime.now(timezone.utc),
            n_comments=0,
            n_pingbacks=0,
            rating=0,
            blog=blog,
        )
        author = models.Author.objects.create()
        entry.authors.add(author)
        self.assertTrue(entry.authors.exists())


from typing import Optional

# Definition for a Node.
class Node:
    def __init__(self, val, prev, next, child):
        self.val = val
        self.prev = prev
        self.next = next
        self.child = child

class Solution:
    def flatten(self, head: 'Optional[Node]') -> 'Optional[Node]':
        stack = []
        temp = head

        while temp:
            if temp.child:
                if temp.next:
                    stack.append(temp.next)
                temp.next = temp.child
                temp.child.prev = temp
                temp.child = None

            if not temp.next and stack:
                node = stack.pop()
                node.prev = temp
                temp.next = node

            temp = temp.next

        return head


# organise a sequence to sample conversion
import os, re

in_dir = "input_individual_gtf"
out_fp="sequence_to_subject.txt"
sequence_to_subject = {}

all_files=os.listdir(in_dir)

for file in all_files:
    current_file = os.path.join(in_dir, file)
    current_species = file.replace(".gff", "")
    with open(current_file, 'r') as f_in:
        for line in f_in:
            if line.startswith("#"):
                continue
            else:
                line=line.strip()
                if "ID=" in line:
                    vals=line.split("\t")
                    pattern_ID= re.compile(r'ID=(.*?);')
                    pattern_gene= re.compile(r'.*?;product=(.*?)$')
                    match_pattern_ID=re.search(pattern_ID, vals[-1])
                    match_pattern_gene=re.search(pattern_gene, vals[-1])
                    start=int(vals[3])
                    end=int(vals[4])
                    if match_pattern_ID and match_pattern_gene:
                        sequence_to_subject[match_pattern_ID.group(1)]=[vals[0], current_species, match_pattern_gene.group(1),str(end-start)]

f_out = open(out_fp,'w+')
f_out.write("seq_id\tsample\tspecies\tgene\tgeneLength\n")
for seq in sequence_to_subject:
    f_out.write("%s\t%s\n" %(seq, "\t".join(sequence_to_subject[seq])))

f_out.close()


import time
from evolut_full import evolut_algo, evolut_algo_naive
from conv_utils import rand_select_algorithm_doppler, gen_rand_seq, gen_binary_codes, gen_polyphase_codes
from genetic_full import run_evolution, select_parents_part_random, select_parents_random, tournament_selection
from crossover_algs import crossover_halfhalf, crossover_random, crossover_randpoint
from hill_climbing import simple_local_search, stochastic_local_search, hill_climbing_iterative, sdls_local_search
import pandas as pd
from anneal_full import anneal_mult, anneal, anneal_with_tabu, anneal_with_local_search, semi_exponential, lin_add, lin_mult, mult_exponential, log_mult, quad_mult, quad_add, expon_add, trigon_add, quenching
from memetic_tabu_search import run_evolution_memet, ts_local_search
from threshold_accept import threshold_handler
from polyphase import great_deluge_handler, anneal_phases_handler, random_arbitrary_polyphase
from particle_swarm import particle_swarm_polyphase
from alternating_codes import hill_climbing_iterative_alternating, great_deluge_alternating, random_alternating_binary, random_alternating_polyphase
from exhaustive import peak_sidelobes_general, peak_sidelobes_efficient, s_doppler

# BINARY TESTS:
# These tests aim to minimize peak sidelobe values for binary sidelobes for codes of length 2-55.
# This checks how well my algorithms do relative to the optimal possible, as well as compared to each other.
# Note that we also have a randomized test at the end, which aims to check how well we do relative to a random algorithm
# with the same time constraints.

def hill_climbing_tests():
    print('hills')
    f = open('best_known_sidelobes.txt')
    lines = f.readlines()
    steepest_descent = ['NA', 'NA']
    simple_climb = ['NA', 'NA']
    stochastic_climb = ['NA', 'NA']
    for i in range(2, 55):
        print(i)
        x = hill_climbing_iterative(i, sdls_local_search)
        steepest_descent.append(x[1].real - int(lines[i-1]))
        y = hill_climbing_iterative(i, simple_local_search)
        simple_climb.append(y[1].real - int(lines[i-1]))
        z = hill_climbing_iterative(i, stochastic_local_search)
        stochastic_climb.append(z[1].real - int(lines[i-1]))
    df = pd.DataFrame({'Steepest Descent': steepest_descent, 'Simple Climb': simple_climb, 'Stochastic Climb': stochastic_climb})
    writer = pd.ExcelWriter('hill_climb.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def anneal_tests():
    print('anneal')
    f = open('best_known_sidelobes.txt')
    lines = f.readlines()
    anneal_reg = ['NA', 'NA']
    anneal_local = ['NA', 'NA']
    anneal_tabu = ['NA', 'NA']
    for i in range(2, 55):
        print(i)
        x = anneal_mult(i, 1, semi_exponential, anneal)
        anneal_reg.append(x.real - int(lines[i-1]))
        y = anneal_mult(i, 1, semi_exponential, anneal_with_tabu)
        anneal_tabu.append(y.real - int(lines[i-1]))

    print(anneal_reg)
    print(anneal_tabu)
    df = pd.DataFrame({'Regular Anneal': anneal_reg, 'Anneal with tabu': anneal_tabu})
    writer = pd.ExcelWriter('anneal_test_binary.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def evolutionary_tests():
    print('evolution')
    f = open('best_known_sidelobes.txt')
    lines = f.readlines()
    evolut_smart = ['NA', 'NA']
    evolut_naive = ['NA', 'NA']
    for i in range(2, 55):
        print(i)
        x = evolut_algo_naive(i)
        evolut_smart.append(x[1].real - int(lines[i - 1]))
        y = evolut_algo_naive(i)
        evolut_naive.append(y[1].real - int(lines[i - 1]))

    df = pd.DataFrame(
        {'Smart evolutionary': evolut_smart, 'Naive evolutionary': evolut_naive})
    writer = pd.ExcelWriter('evolutionary_test_binary.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def genetic_rand_crossover_tests():
    print('genetic_rand')
    f = open('best_known_sidelobes.txt')
    lines = f.readlines()
    genetic_rand_rand = ['NA', 'NA']
    genetic_rand_partrand = ['NA', 'NA']
    genetic_rand_tourney = ['NA', 'NA']
    for i in range(2, 55):
        print(i)
        x = run_evolution(i, parent_func = select_parents_random, crossover_func = crossover_random)
        genetic_rand_rand.append(x[1].real - int(lines[i - 1]))
        y = run_evolution(i, parent_func = select_parents_part_random, crossover_func = crossover_random)
        genetic_rand_partrand.append(y[1].real - int(lines[i - 1]))
        z = run_evolution(i, parent_func = tournament_selection, crossover_func = crossover_random)
        genetic_rand_tourney.append(z[1].real - int(lines[i - 1]))

    df = pd.DataFrame(
        {'Random crossover with random parent selection': genetic_rand_rand, 'Random crossover with partially random parent selection': genetic_rand_partrand,
         'Random crossover with tournament parent selection': genetic_rand_tourney})
    writer = pd.ExcelWriter('genetic_random_crossover_test_binary.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def genetic_randpoint_crossover_tests():
    print('genetic_randpoint')
    f = open('best_known_sidelobes.txt')
    lines = f.readlines()
    genetic_randpt_rand = ['NA', 'NA']
    genetic_randpt_partrand = ['NA', 'NA']
    genetic_randpt_tourney = ['NA', 'NA']
    for i in range(2, 55):
        print(i)
        x = run_evolution(i, parent_func=select_parents_random, crossover_func=crossover_randpoint)
        genetic_randpt_rand.append(x[1].real - int(lines[i - 1]))
        y = run_evolution(i, parent_func=select_parents_part_random, crossover_func=crossover_randpoint)
        genetic_randpt_partrand.append(y[1].real - int(lines[i - 1]))
        z = run_evolution(i, parent_func=tournament_selection, crossover_func=crossover_randpoint)
        genetic_randpt_tourney.append(z[1].real - int(lines[i - 1]))

    df = pd.DataFrame(
        {'Random point crossover with random parent selection': genetic_randpt_rand, 'Random point crossover with partially random parent selection': genetic_randpt_partrand,
         'Random point crossover with tournament parent selection': genetic_randpt_tourney})
    writer = pd.ExcelWriter('genetic_randpoint_crossover_test_binary.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def genetic_half_crossover_tests():
    print('genetic_half')
    f = open('best_known_sidelobes.txt')
    lines = f.readlines()
    genetic_half_rand = ['NA', 'NA']
    genetic_half_partrand = ['NA', 'NA']
    genetic_half_tourney = ['NA', 'NA']
    for i in range(2, 55):
        print(i)
        x = run_evolution(i, parent_func=select_parents_random, crossover_func=crossover_randpoint)
        genetic_half_rand.append(x[1].real - int(lines[i - 1]))
        y = run_evolution(i, parent_func=select_parents_part_random, crossover_func=crossover_randpoint)
        genetic_half_partrand.append(y[1].real - int(lines[i - 1]))
        z = run_evolution(i, parent_func=tournament_selection, crossover_func=crossover_randpoint)
        genetic_half_tourney.append(z[1].real - int(lines[i - 1]))

    df = pd.DataFrame(
        {'Half crossover with random parent selection': genetic_half_rand, 'Half crossover with partially random parent selection': genetic_half_partrand,
         'Half crossover with tournament parent selection': genetic_half_tourney})
    writer = pd.ExcelWriter('genetic_half_crossover_test_binary.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def threshold_accept_tests():
    print('threshold')
    f = open('best_known_sidelobes.txt')
    lines = f.readlines()
    threshold = ['NA', 'NA']
    for i in range(2, 55):
        print(i)
        x = threshold_handler(i)
        threshold.append(x.real - int(lines[i - 1]))
    df = pd.DataFrame({'Threshold accept': threshold})
    writer = pd.ExcelWriter('threshold_accept_test_binary.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def memetic_tests():
    print('memetic')
    f = open('best_known_sidelobes.txt')
    lines = f.readlines()
    memetic_ts = ['NA', 'NA']
    memetic_sdls= ['NA', 'NA']
    memetic_simple = ['NA', 'NA']
    for i in range(2, 55):
        print(i)
        x = run_evolution_memet(i, ts_local_search)
        memetic_ts.append(x[1].real - int(lines[i - 1]))
        y = run_evolution_memet(i, sdls_local_search)
        memetic_sdls.append(y[1].real - int(lines[i - 1]))
        z = run_evolution_memet(i, simple_local_search)
        memetic_simple.append(z[1].real - int(lines[i - 1]))
    df = pd.DataFrame(
        {'Memetic w/ tabu search': memetic_ts, 'Memetic with sdls': memetic_sdls, 'Memetic with simple': memetic_simple})
    writer = pd.ExcelWriter('memetic_test_binary.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def random_tests():
    print('random')
    f = open('best_known_sidelobes.txt')
    lines = f.readlines()
    random_vals = ['NA', 'NA']
    for i in range(2, 55):
        print(i)
        x = rand_select_algorithm_doppler(i, 2, 1)
        random_vals.append(x[1].real - int(lines[i - 1]))
    df = pd.DataFrame({'Random': random_vals})
    writer = pd.ExcelWriter('random_test_binary.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

# RANDOM POLYPHASE TESTS:
# These are tests, with codes chosen randomly at each step, which aim to minimize the psl values for polyphase codes
# with variable doppler widths.  These serve as a control against other tests we will run with the memetic and
# hill climbing algorithms.

lst = [2, 3, 4, 6, 16]
freqs = [1, 2, 3, 4, 6]

def random_polyphase_N1_tests():
    print('random_N1')
    lengths = []
    phase_unity = []
    random_codes = []
    random_vals = []
    for i in range(5, 41, 5):
        print(i)
        for j in lst:
            print(j)
            lengths.append(i)
            phase_unity.append(j)
            x = rand_select_algorithm_doppler(i, j, 1)
            random_codes.append(x[0])
            random_vals.append(x[1])
            print(x[1])
    df = pd.DataFrame({'Length': lengths, 'Phase Unity': phase_unity, 'Code': random_codes, 'PSL Value': random_vals})
    writer = pd.ExcelWriter('random_test_polyphase_N1.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def random_polyphase_N2_tests():
    print('random_N2')
    lengths = []
    phase_unity = []
    random_codes = []
    random_vals = []
    for i in range(5, 41, 5):
        print(i)
        for j in lst:
            print(j)
            lengths.append(i)
            phase_unity.append(j)
            x = rand_select_algorithm_doppler(i, j, 2)
            random_codes.append(x[0])
            random_vals.append(x[1])
    df = pd.DataFrame({'Length': lengths, 'Phase Unity': phase_unity, 'Code': random_codes, 'PSL Value': random_vals})
    writer = pd.ExcelWriter('random_test_polyphase_N2.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def random_polyphase_N3_tests():
    print('random_N3')
    lengths = []
    phase_unity = []
    random_codes = []
    random_vals = []
    for i in range(5, 41, 5):
        print(i)
        for j in lst:
            print(j)
            lengths.append(i)
            phase_unity.append(j)
            x = rand_select_algorithm_doppler(i, j, 3)
            random_codes.append(x[0])
            random_vals.append(x[1])
    df = pd.DataFrame({'Length': lengths, 'Phase Unity': phase_unity, 'Code': random_codes, 'PSL Value': random_vals})
    writer = pd.ExcelWriter('random_test_polyphase_N3.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def random_polyphase_N4_tests():
    print('random_N4')
    lengths = []
    phase_unity = []
    random_codes = []
    random_vals = []
    for i in range(5, 41, 5):
        print(i)
        for j in lst:
            print(j)
            lengths.append(i)
            phase_unity.append(j)
            x = rand_select_algorithm_doppler(i, j, 4)
            random_codes.append(x[0])
            random_vals.append(x[1])
    df = pd.DataFrame({'Length': lengths, 'Phase Unity': phase_unity, 'Code': random_codes, 'PSL Value': random_vals})
    writer = pd.ExcelWriter('random_test_polyphase_N4.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def random_polyphase_N6_tests():
    print('random_N6')
    lengths = []
    phase_unity = []
    random_codes = []
    random_vals = []
    for i in range(5, 41, 5):
        print(i)
        for j in lst:
            print(j)
            lengths.append(i)
            phase_unity.append(j)
            x = rand_select_algorithm_doppler(i, j, 6)
            random_codes.append(x[0])
            random_vals.append(x[1])
    df = pd.DataFrame({'Length': lengths, 'Phase Unity': phase_unity, 'Code': random_codes, 'PSL Value': random_vals})
    writer = pd.ExcelWriter('random_test_polyphase_N6.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

# TEMPERATURE TESTS:
# This one, long test compares the various temperature schedules, timing them and checking for various levels of optimization.
# If rewritten, this test will need to be a little more stringent about timing constraints to make sure that none of these
# take excessively long inside a loop, thus escaping constraints.

lst = [8, 13, 19, 25, 29, 35]
def temperature_tests():
    print('temperature')
    f = open('best_known_sidelobes.txt')
    lines = f.readlines()
    lengths = []
    anneal_semiexponential = []
    anneal_semiexponential_times = []
    anneal_mult_exponential = []
    anneal_mult_exponential_times = []
    anneal_log_mult = []
    anneal_log_mult_times = []
    anneal_lin_mult = []
    anneal_lin_mult_times = []
    anneal_quad_mult = []
    anneal_quad_mult_times = []
    anneal_lin_add = []
    anneal_lin_add_times = []
    anneal_quad_add = []
    anneal_quad_add_times = []
    anneal_expon_add = []
    anneal_expon_add_times = []
    anneal_trigon_add = []
    anneal_trigon_add_times = []
    anneal_quenching = []
    anneal_quenching_times = []
    for i in lst:
        print(i)
        lengths.append(i)
        start_time = time.time()
        x = anneal_mult(i, 1, semi_exponential, anneal)
        anneal_semiexponential_times.append(time.time()-start_time)
        anneal_semiexponential.append(x.real)
        if i == 35:
            print('checkpoint 1')
        start_time = time.time()
        x = anneal_mult(i, 1, mult_exponential, anneal)
        anneal_mult_exponential_times.append(time.time() - start_time)
        anneal_mult_exponential.append(x.real)
        if i == 35:
            print('checkpoint 2')
        start_time = time.time()
        x = anneal_mult(i, 1, log_mult, anneal)
        anneal_log_mult_times.append(time.time() - start_time)
        anneal_log_mult.append(x.real)
        if i == 35:
            print('checkpoint 3')
        start_time = time.time()
        x = anneal_mult(i, 1, lin_mult, anneal)
        anneal_lin_mult_times.append(time.time() - start_time)
        anneal_lin_mult.append(x.real)
        if i == 35:
            print('checkpoint 4')
        start_time = time.time()
        x = anneal_mult(i, 1, quad_mult, anneal)
        anneal_quad_mult_times.append(time.time() - start_time)
        anneal_quad_mult.append(x.real)
        if i == 35:
            print('checkpoint 5')
        start_time = time.time()
        x = anneal_mult(i, 1, lin_add, anneal)
        print(x)
        anneal_lin_add_times.append(time.time() - start_time)
        anneal_lin_add.append(x.real)
        if i == 35:
            print('checkpoint 6')
        start_time = time.time()
        x = anneal_mult(i, 1, quad_add, anneal)
        anneal_quad_add_times.append(time.time() - start_time)
        anneal_quad_add.append(x.real)
        if i == 35:
            print('checkpoint 7')
        start_time = time.time()
        x = anneal_mult(i, 1, expon_add, anneal)
        anneal_expon_add_times.append(time.time() - start_time)
        anneal_expon_add.append(x.real)
        if i == 35:
            print('checkpoint 8')
        start_time = time.time()
        x = anneal_mult(i, 1, trigon_add, anneal)
        anneal_trigon_add_times.append(time.time() - start_time)
        anneal_trigon_add.append(x.real)
        if i == 35:
            print('checkpoint 9')
        start_time = time.time()
        x = anneal_mult(i, 1, quenching, anneal)
        anneal_quenching_times.append(time.time() - start_time)
        anneal_quenching.append(x.real)

    df = pd.DataFrame(
        {'Length': lengths, 'Semiexponential': anneal_semiexponential, 'Semiexponential times': anneal_semiexponential_times,
         'Mult_exponential': anneal_mult_exponential,'Mult_exponential times': anneal_mult_exponential_times,
         'Log_mult': anneal_log_mult, 'Log_mult times': anneal_log_mult_times,
         'Lin_mult': anneal_lin_mult, 'Lin_mult times': anneal_lin_mult_times,  'Quad_mult': anneal_quad_mult,
         'Quad_mult times': anneal_quad_mult_times,
         'Lin_add': anneal_lin_add, 'Lin_add times': anneal_lin_add_times,  'Quad_add': anneal_quad_add,
         'Quad_add times': anneal_quad_add_times, 'Expon_add': anneal_expon_add,
         'Expon_add times': anneal_expon_add_times, 'Trigon_add': anneal_trigon_add,
         'Trigon_add times': anneal_trigon_add_times, 'Quenching': anneal_quenching,
         'Quenching times': anneal_quenching_times})
    writer = pd.ExcelWriter('temperature_tests.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

# DOPPLER, POLYPHASE TESTS:
# This set of tests runs our optimization algorithms to find minimal psl codes with N>1 doppler width, j>2 phase_unity.
# First, we run a hill_climbing test in which our timing is very short, but we get near-optimal solutions for all
# lengths between 2 and 55, and all doppler widths between 1-5, phase_unity between 2 and 5.
# Then, we run our hill_climbing and memetic algorithms on the exact same tests as those for the random polyphase tests above,
# thus serving as a nice point of comparison.

def hill_climbing_doppler_polyphase_tests():
    print('hill_climbing_doppler_polyphase')
    lengths = []
    freq_bands = []
    phase_unity = []
    hill_climbing_codes = []
    hill_climbing_values = []
    times = []
    for j in range(2, 6):
        for N in range(1, 6):
            if j !=2 or N!=1:
                for length in range(2, 55):
                    print(length)
                    print(j)
                    print(N)
                    start_time = time.time()
                    lengths.append(length)
                    freq_bands.append(N)
                    phase_unity.append(j)
                    x = hill_climbing_iterative(length, sdls_local_search, N, j)
                    hill_climbing_codes.append(x[0])
                    hill_climbing_values.append(x[1])
                    times.append(time.time() - start_time)
                    print(x[1])
    df = pd.DataFrame({'Length': lengths, 'Doppler Width': freq_bands,
                       'Phase Unity': phase_unity, 'Code': hill_climbing_codes, 'PSL Value': hill_climbing_values, 'Time': times})
    writer = pd.ExcelWriter('hill_climbing_doppler_polyphase_tests.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def hill_climbing_doppler_binary_select_tests():
    print('hill_climbing_doppler_polyphase')
    lengths = []
    freq_bands = []
    hill_climbing_codes = []
    hill_climbing_values = []
    times = []
    for N in range(2, 6):
        for length in range(5, 41, 5):
            print(length)
            print(N)
            start_time = time.time()
            lengths.append(length)
            freq_bands.append(N)
            x = hill_climbing_iterative(length, sdls_local_search, N, 2)
            hill_climbing_codes.append(x[0])
            hill_climbing_values.append(x[1])
            times.append(time.time() - start_time)
            print(x[1])
    df = pd.DataFrame({'Length': lengths, 'Doppler Width': freq_bands,
                       'Code': hill_climbing_codes, 'PSL Value': hill_climbing_values, 'Time': times})
    writer = pd.ExcelWriter('hill_climbing_doppler_binary_select_tests.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def memetic_doppler_binary_select_tests():
    print('memetic_doppler_binary_select')
    lengths = []
    freq_bands = []
    memetic_codes = []
    memetic_values = []
    times = []
    for N in range(2, 6):
        for length in range(5, 41, 5):
            print(length)
            print(N)
            start_time = time.time()
            lengths.append(length)
            freq_bands.append(N)
            x = run_evolution_memet(length, ts_local_search, 0.1, N)
            memetic_codes.append(x[0])
            memetic_values.append(x[1])
            times.append(time.time() - start_time)
            print(x[1])
    df = pd.DataFrame({'Length': lengths, 'Doppler Width': freq_bands,
                       'Code': memetic_codes, 'PSL Value': memetic_values, 'Time': times})
    writer = pd.ExcelWriter('memetic_doppler_binary_select_tests.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def hill_climbing_doppler_polyphase_select_tests():
    print('hill_climbing_doppler_polyphase_select')
    lengths = []
    freq_bands = []
    hill_climbing_codes = []
    hill_climbing_values = []
    phase_unity = []
    times = []
    for j in range(3, 7):
        for N in range(1, 3):
            for length in range(5, 41, 5):
                print(length)
                print(N)
                print(j)
                start_time = time.time()
                lengths.append(length)
                freq_bands.append(N)
                phase_unity.append(j)
                x = hill_climbing_iterative(length, sdls_local_search, N, j)
                hill_climbing_codes.append(x[0])
                hill_climbing_values.append(x[1])
                times.append(time.time() - start_time)
                print(x[1])
    df = pd.DataFrame({'Length': lengths, 'Doppler Width': freq_bands, 'Phase Unity': phase_unity,
                       'Code': hill_climbing_codes, 'PSL Value': hill_climbing_values, 'Time': times})
    writer = pd.ExcelWriter('hill_climbing_doppler_polyphase_select_tests.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def memetic_doppler_polyphase_select_tests():
    print('memetic_doppler_polyphase_select')
    lengths = []
    freq_bands = []
    memetic_codes = []
    memetic_values = []
    phase_unity = []
    times = []
    for j in range(3, 7):
        for N in range(1, 3):
            for length in range(5, 41, 5):
                print(length)
                print(N)
                print(j)
                start_time = time.time()
                lengths.append(length)
                freq_bands.append(N)
                phase_unity.append(j)
                x = run_evolution_memet(length, ts_local_search, 0.1, N, j)
                memetic_codes.append(x[0])
                memetic_values.append(x[1])
                times.append(time.time() - start_time)
                print(x[1])
    df = pd.DataFrame({'Length': lengths, 'Doppler Width': freq_bands, 'Phase Unity': phase_unity,
                       'Code': memetic_codes, 'PSL Value': memetic_values, 'Time': times})
    writer = pd.ExcelWriter('memetic_doppler_polyphase_select_tests.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

# ARBITRARY POLYPHASE TESTS
# These tests, for lengths 2-39, aim to find optimal psl arbitrary polyphase codes with N=1 doppler width.
# They also compare the relative success of random, great deluge, simulated annealing, and particle swarm algorithms.
# After this comparison, we then run doppler tests with the best polyphase algorithm found, generating some new
# optimal codes in doppler space.
def random_arbitrary_polyphase_test():
    print('random arbitrary polyphase')
    length = []
    random_codes = []
    random_values = []
    for i in range(2, 39):
        print(i)
        length.append(i)
        x = random_arbitrary_polyphase(i)
        random_codes.append(x[0])
        random_values.append(x[1])
        print(x[1])

    df = pd.DataFrame({'Length': length,
                       'Code': random_codes, 'PSL Value': random_values})
    writer = pd.ExcelWriter('random_arbitrary_polyphase.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def great_deluge_tests():
    print('great deluge polyphase')
    great_deluge = ['NA', 'NA']
    for i in range(2, 39):
        print(i)
        x = great_deluge_handler(i)
        great_deluge.append(max(0, x[1].real - 1))
    df = pd.DataFrame({'Great Deluge': great_deluge})
    writer = pd.ExcelWriter('great_deluge_test_arb_polyphase.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def anneal_phase_tests():
    print('anneal polyphase')
    anneal = ['NA', 'NA']
    for i in range(2, 39):
        print(i)
        x = anneal_phases_handler(i, semi_exponential)
        anneal.append(max(0, x[1].real - 1))
    df = pd.DataFrame({'Anneal': anneal})
    writer = pd.ExcelWriter('anneal_test_arb_polyphase.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def particle_swarm_tests():
    print('particle polyphase')
    particle_swarm = ['NA', 'NA']
    for i in range(2, 39):
        print(i)
        x = particle_swarm_polyphase(i)
        particle_swarm.append(max(0, x[1].real - 1))
    df = pd.DataFrame({'Particle Swarm': particle_swarm})
    writer = pd.ExcelWriter('particle_swarm_test_arb_polyphase.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def best_polyphase_gen_codes_N1_tests():
    print('best polyphase')
    lengths = []
    great_deluge_codes = []
    great_deluge_values = []
    for i in range(5, 46, 5):
        print(i)
        lengths.append(i)
        x = great_deluge_handler(i)
        great_deluge_codes.append(x[0])
        print(x[1])
        great_deluge_values.append(x[1])
    df = pd.DataFrame({'Length': lengths, 'Code': great_deluge_codes, 'Value': great_deluge_values})
    writer = pd.ExcelWriter('best_polyphase_N1.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def best_polyphase_gen_codes_N2_tests():
    print('best polyphase 2')
    lengths = []
    great_deluge_codes = []
    great_deluge_values = []
    for i in range(5, 46, 5):
        print(i)
        lengths.append(i)
        x = great_deluge_handler(i, N=2)
        great_deluge_codes.append(x[0])
        great_deluge_values.append(x[1])
    df = pd.DataFrame({'Length': lengths, 'Code': great_deluge_codes, 'Value': great_deluge_values})
    writer = pd.ExcelWriter('best_polyphase_N2.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def best_polyphase_gen_codes_N3_tests():
    print('best polyphase 3')
    lengths = []
    great_deluge_codes = []
    great_deluge_values = []
    for i in range(5, 46, 5):
        print(i)
        lengths.append(i)
        x = great_deluge_handler(i, N=3)
        great_deluge_codes.append(x[0])
        great_deluge_values.append(x[1])
    df = pd.DataFrame({'Length': lengths, 'Code': great_deluge_codes, 'Value': great_deluge_values})
    writer = pd.ExcelWriter('best_polyphase_N3.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def best_polyphase_gen_codes_N4_tests():
    print('best polyphase 4')
    lengths = []
    great_deluge_codes = []
    great_deluge_values = []
    for i in range(5, 46, 5):
        print(i)
        lengths.append(i)
        x = great_deluge_handler(i, N=4)
        great_deluge_codes.append(x[0])
        great_deluge_values.append(x[1])
    df = pd.DataFrame({'Length': lengths, 'Code': great_deluge_codes, 'Value': great_deluge_values})
    writer = pd.ExcelWriter('best_polyphase_N4.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def best_polyphase_gen_codes_N5_tests():
    print('best polyphase 5')
    lengths = []
    great_deluge_codes = []
    great_deluge_values = []
    for i in range(5, 46, 5):
        print(i)
        lengths.append(i)
        x = great_deluge_handler(i, N=5)
        great_deluge_codes.append(x[0])
        great_deluge_values.append(x[1])
    df = pd.DataFrame({'Length': lengths, 'Code': great_deluge_codes, 'Value': great_deluge_values})
    writer = pd.ExcelWriter('best_polyphase_N5.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def best_polyphase_gen_codes_N6_tests():
    print('best polyphase 6')
    lengths = []
    great_deluge_codes = []
    great_deluge_values = []
    for i in range(5, 46, 5):
        print(i)
        lengths.append(i)
        x = great_deluge_handler(i, N=6)
        great_deluge_codes.append(x[0])
        great_deluge_values.append(x[1])
    df = pd.DataFrame({'Length': lengths, 'Code': great_deluge_codes, 'Value': great_deluge_values})
    writer = pd.ExcelWriter('best_polyphase_N6.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

# EXHAUSTIVE TESTS:
# These tests aim to test our exhaustive algorithms, and in particular demonstrate the efficiency of our efficient exhaustive
# method as compared to naive methods.  This also serves as a (start to) a point of comparison for optimal values for
# polyphase codes with doppler width.
def naive_exhaustive_tests():
    print('naive')
    lengths = []
    freq_bands = []
    phase_unity = []
    naive_codes = []
    naive_values = []
    times = []
    for j in range(2, 6):
        for N in range(1, 6):
            count = 2
            start_time = time.time()
            while time.time() - start_time < 600:
                print(count)
                print(j)
                print(N)
                start_time = time.time()
                lengths.append(count)
                freq_bands.append(N)
                phase_unity.append(j)
                if j == 2:
                    x = peak_sidelobes_general(count, None, N, gen_binary_codes)
                else:
                    x = peak_sidelobes_general(count, None, N, gen_polyphase_codes, optional_limit = j)

                naive_codes.append(x[0])
                naive_values.append(x[1])
                times.append(time.time() - start_time)
                print(x[1])
                count+=1
    df = pd.DataFrame({'Length': lengths, 'Doppler Width': freq_bands,
                       'Phase Unity': phase_unity, 'Code': naive_codes, 'PSL Value': naive_values, 'Time': times})
    writer = pd.ExcelWriter('naive_exhaustive.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def efficient_nondoppler_tests():
    print('efficient')
    lengths = []
    freq_bands = []
    efficient_codes = []
    efficient_values = []
    times = []
    for N in range(1, 6):
        count = 2
        start_time = time.time()
        while time.time() - start_time < 450:
            print(count)
            print(N)
            start_time = time.time()
            lengths.append(count)
            freq_bands.append(N)
            if N==1:
                x = peak_sidelobes_efficient(count)
            else:
                x = peak_sidelobes_efficient(count, s_doppler, 0, float('inf'), N)
            efficient_codes.append(x[0])
            efficient_values.append(x[1])
            times.append(time.time() - start_time)
            print(x[1])
            count += 1
    df = pd.DataFrame({'Length': lengths, 'Doppler Width': freq_bands,
                     'Code': efficient_codes, 'PSL Value': efficient_values, 'Time': times})
    writer = pd.ExcelWriter('efficient_exhaustive.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

# ALTERNATING TESTS:
# ARBITRARY POLYPHASE TESTS
# These tests, for lengths 8-20, aim to find near-alternating codes with the number of codes ranging from 4-10
# For binary codes, we only consider an even number of codes, and compare random and hill climbing algorithms
# For polyphase codes, we consider any number of codes, and compare random and great deluge algorithms.

def random_alternating_binary_test():
    print('random alternating binary')
    length = []
    number_of_codes = []
    random_codes = []
    random_values = []
    for i in range(8, 20):
        print(i)
        for j in range(4, 10, 2):
            print(j)
            length.append(i)
            number_of_codes.append(j)
            x = random_alternating_binary(i, j)
            random_codes.append(x[0])
            random_values.append(x[1])
            print(x[1])

    df = pd.DataFrame({'Length': length, 'Number of codes': number_of_codes,
                       'Code': random_codes, 'PSL Value': random_values})
    writer = pd.ExcelWriter('random_alternating_binary.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def random_alternating_polyphase_test():
    print('random alternating polyphase')
    length = []
    number_of_codes = []
    random_codes = []
    random_values = []
    for i in range(8, 20):
        print(i)
        for j in range(4, 10):
            print(j)
            length.append(i)
            number_of_codes.append(j)
            x = random_alternating_polyphase(i, j)
            random_codes.append(x[0])
            random_values.append(x[1])
            print(x[1])

    df = pd.DataFrame({'Length': length, 'Number of codes': number_of_codes,
                       'Code': random_codes, 'PSL Value': random_values})
    writer = pd.ExcelWriter('random_alternating_polyphase.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def hill_climbing_alternating():
    print('hill_climbing_alternating')
    length = []
    number_of_codes = []
    hill_climbing_codes = []
    hill_climbing_values = []
    for i in range(8, 20):
        print(i)
        for j in range(4, 10, 2):
            print(j)
            length.append(i)
            number_of_codes.append(j)
            x = hill_climbing_iterative_alternating(i, j, sdls_local_search)
            print('hi')
            hill_climbing_codes.append(x[0])
            hill_climbing_values.append(x[1])

    df = pd.DataFrame({'Length': length, 'Number of codes': number_of_codes,
                       'Code': hill_climbing_codes, 'PSL Value': hill_climbing_values})
    writer = pd.ExcelWriter('hill_climb_alternating.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

def great_deluge_alternating_test():
    print('great_deluge_alternating')
    length = []
    number_of_codes = []
    great_deluge_codes = []
    great_deluge_values = []
    for i in range(8, 20):
        print(i)
        for j in range(4, 10):
            print(j)
            length.append(i)
            number_of_codes.append(j)
            x = great_deluge_handler(i, great_deluge_alternating, j)
            great_deluge_codes.append(x[0])
            great_deluge_values.append(x[1])

    df = pd.DataFrame({'Length': length, 'Number of codes': number_of_codes,
                       'Code': great_deluge_codes, 'PSL Value': great_deluge_values})
    writer = pd.ExcelWriter('great_deluge_alternating.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()

"""
def gen_scatter_plot_sidelobes(num_elem, N, op_on_list, gen_codes):
    x = []
    y = []
    for i in range(4, 5):
        x.append(i)
        val = peak_sidelobes_general(i, num_elem, N, op_on_list, gen_codes)
        y.append(val[1])
    plt.scatter(x, y)
    plt.xlabel('Length of code')
    plt.ylabel('Minimum Peak Sidelobe')
    plt.title('Minimum Peak Doppler Sidelobes')
    plt.axis([4, 15, 0, 150])
    plt.grid(True)
    plt.show()

def gen_plot_sidelobes(num_elem, op_on_list, gen_codes):
    x = []
    y = []
    for i in range(2, 18):
        x.append(i)
        val = peak_sidelobes_general(i, num_elem, i, op_on_list, gen_codes)
        y.append(val[1])

    plt.scatter(x, y)
    plt.xlabel('Length of code')
    plt.ylabel('Minimum Peak Sidelobe')
    plt.title('Minimum Peak Doppler Sidelobes')
    plt.axis([0, 20, 0, 150])
    plt.grid(True)
    plt.show()
"""


"""Sermon Table

Revision ID: cba323888411
Revises: 368cd37d6b05
Create Date: 2020-07-15 01:49:41.625331

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'cba323888411'
down_revision = '368cd37d6b05'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('sermon',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('title', sa.String(length=124), nullable=True),
    sa.Column('text', sa.String(length=124), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_sermon_created_at'), 'sermon', ['created_at'], unique=False)
    op.create_index(op.f('ix_sermon_text'), 'sermon', ['text'], unique=False)
    op.create_index(op.f('ix_sermon_title'), 'sermon', ['title'], unique=False)
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_sermon_title'), table_name='sermon')
    op.drop_index(op.f('ix_sermon_text'), table_name='sermon')
    op.drop_index(op.f('ix_sermon_created_at'), table_name='sermon')
    op.drop_table('sermon')
    # ### end Alembic commands ###


# MWM Reader Module
from .mwmfile import MWMFile
from datetime import datetime
import os

# Unprocessed sections: geomN, trgN, idx, sdx (search index),
# addr (search address), offs (feature offsets - succinct)

# TODO:
# - Predictive reading of LineStrings
# - Find why polygon geometry is incorrect in iter_features()

class MWM(MWMFile):
    # indexer/feature_meta.hpp
    metadata = ["0",
                "cuisine", "open_hours", "phone_number", "fax_number", "stars",
                "operator", "url", "website", "internet", "ele",
                "turn_lanes", "turn_lanes_forward", "turn_lanes_backward", "email", "postcode",
                "wikipedia", "maxspeed", "flats", "height", "min_height",
                "denomination", "building_levels", "test_id", "ref:sponsored", "price_rate",
                "rating", "banner_url", "level"]

    regiondata = ["languages", "driving", "timezone", "addr_fmt", "phone_fmt",
                  "postcode_fmt", "holidays", "housenames"]

    def __init__(self, f):
        MWMFile.__init__(self, f)
        self.read_tags()
        self.read_header()
        self.type_mapping = []
        self.read_types(os.path.join(
            os.getcwd(), os.path.dirname(__file__), 'types.txt'))

    def read_types(self, filename):
        if not os.path.exists(filename):
            return
        self.type_mapping = []
        with open(filename, 'r') as ft:
            for line in ft:
                if len(line.strip()) > 0:
                    self.type_mapping.append(line.strip().replace('|', '-'))

    def read_version(self):
        """Reads 'version' section."""
        self.seek_tag('version')
        self.f.read(4)  # skip prolog
        fmt = self.read_varuint() + 1
        version = self.read_varuint()
        if version < 161231:
            vdate = datetime(2000 + int(version / 10000), int(version / 100) % 100, version % 100)
        else:
            vdate = datetime.fromtimestamp(version)
            version = int(vdate.strftime('%y%m%d'))
        return {'fmt': fmt, 'version': version, 'date': vdate}

    def read_header(self):
        """Reads 'header' section."""
        if not self.has_tag('header'):
            # Stub for routing files
            self.coord_size = (1 << 30) - 1
            return {}
        self.seek_tag('header')
        result = {}
        coord_bits = self.read_varuint()
        self.coord_size = (1 << coord_bits) - 1
        self.base_point = self.mwm_bitwise_split(self.read_varuint())
        result['basePoint'] = self.to_4326(self.base_point)
        result['bounds'] = self.read_bounds()
        result['scales'] = self.read_uint_array()
        langs = self.read_uint_array()
        for i in range(len(langs)):
            if i < len(self.languages):
                langs[i] = self.languages[langs[i]]
        result['langs'] = langs
        map_type = self.read_varint()
        if map_type == 0:
            result['mapType'] = 'world'
        elif map_type == 1:
            result['mapType'] = 'worldcoasts'
        elif map_type == 2:
            result['mapType'] = 'country'
        else:
            result['mapType'] = 'unknown: {0}'.format(map_type)
        return result

    # COMPLEX READERS

    def read_region_info(self):
        if not self.has_tag('rgninfo'):
            return {}
        fields = {}
        self.seek_tag('rgninfo')
        sz = self.read_varuint()
        if sz:
            for i in range(sz):
                t = self.read_varuint()
                t = self.regiondata[t] if t < len(self.regiondata) else str(t)
                fields[t] = self.read_string()
                if t == 'languages':
                    fields[t] = [self.languages[ord(x)] for x in fields[t]]
        return fields

    def read_metadata(self):
        """Reads 'meta' and 'metaidx' sections."""
        if not self.has_tag('metaidx'):
            return {}
        # Metadata format is different since v8
        fmt = self.read_version()['fmt']
        # First, read metaidx, to match featureId <-> metadata
        self.seek_tag('metaidx')
        ftid_meta = []
        while self.inside_tag('metaidx'):
            ftid = self.read_uint(4)
            moffs = self.read_uint(4)
            ftid_meta.append((moffs, ftid))
        # Sort ftid_meta array
        ftid_meta.sort(key=lambda x: x[0])
        ftpos = 0
        # Now read metadata
        self.seek_tag('meta')
        metadatar = {}
        while self.inside_tag('meta'):
            tag_pos = self.tag_offset('meta')
            fields = {}
            if fmt >= 8:
                sz = self.read_varuint()
                if sz:
                    for i in range(sz):
                        t = self.read_varuint()
                        t = self.metadata[t] if t < len(self.metadata) else str(t)
                        fields[t] = self.read_string()
                        if t == 'fuel':
                            fields[t] = fields[t].split('\x01')
            else:
                while True:
                    t = self.read_uint(1)
                    is_last = t & 0x80 > 0
                    t = t & 0x7f
                    t = self.metadata[t] if t < len(self.metadata) else str(t)
                    l = self.read_uint(1)
                    fields[t] = self.f.read(l).decode('utf-8')
                    if is_last:
                        break

            if len(fields):
                while ftpos < len(ftid_meta) and ftid_meta[ftpos][0] < tag_pos:
                    ftpos += 1
                if ftpos < len(ftid_meta):
                    if ftid_meta[ftpos][0] == tag_pos:
                        metadatar[ftid_meta[ftpos][1]] = fields
        return metadatar

    def read_crossmwm(self):
        """Reads 'chrysler' section (cross-mwm routing table)."""
        if not self.has_tag('chrysler'):
            return {}
        self.seek_tag('chrysler')
        # Ingoing nodes: array of (nodeId, coord) tuples
        incomingCount = self.read_uint(4)
        incoming = []
        for i in range(incomingCount):
            nodeId = self.read_uint(4)
            point = self.read_coord(False)
            incoming.append((nodeId, point))
        # Outgoing nodes: array of (nodeId, coord, outIndex) tuples
        # outIndex is an index in neighbours array
        outgoingCount = self.read_uint(4)
        outgoing = []
        for i in range(outgoingCount):
            nodeId = self.read_uint(4)
            point = self.read_coord(False)
            outIndex = self.read_uint(1)
            outgoing.append((nodeId, point, outIndex))
        # Adjacency matrix: costs of routes for each (incoming, outgoing) tuple
        matrix = []
        for i in range(incomingCount):
            sub = []
            for j in range(outgoingCount):
                sub.append(self.read_uint(4))
            matrix.append(sub)
        # List of mwms to which leads each outgoing node
        neighboursCount = self.read_uint(4)
        neighbours = []
        for i in range(neighboursCount):
            size = self.read_uint(4)
            neighbours.append(self.f.read(size).decode('utf-8'))
        return {'in': incoming, 'out': outgoing, 'matrix': matrix, 'neighbours': neighbours}

    def iter_features(self, metadata=False):
        """Reads 'dat' section."""
        if not self.has_tag('dat'):
            return
        # TODO: read 'offs'?
        md = {}
        if metadata:
            md = self.read_metadata()
        self.seek_tag('dat')
        ftid = -1
        while self.inside_tag('dat'):
            ftid += 1
            feature = {'id': ftid}
            feature_size = self.read_varuint()
            next_feature = self.f.tell() + feature_size
            feature['size'] = feature_size

            # Header
            header = {}
            header_bits = self.read_uint(1)
            types_count = (header_bits & 0x07) + 1
            has_name = header_bits & 0x08 > 0
            has_layer = header_bits & 0x10 > 0
            has_addinfo = header_bits & 0x80 > 0
            geom_type = header_bits & 0x60
            types = []
            for i in range(types_count):
                type_id = self.read_varuint()
                if type_id < len(self.type_mapping):
                    types.append(self.type_mapping[type_id])
                else:
                    types.append(str(type_id + 1))  # So the numbers match with mapcss-mapping.csv
            header['types'] = types
            if has_name:
                header['name'] = self.read_multilang()
            if has_layer:
                header['layer'] = self.read_uint(1)
            if has_addinfo:
                if geom_type == MWM.GeomType.POINT:
                    header['rank'] = self.read_uint(1)
                elif geom_type == MWM.GeomType.LINE:
                    header['ref'] = self.read_string()
                elif geom_type == MWM.GeomType.AREA or geom_type == MWM.GeomType.POINT_EX:
                    header['house'] = self.read_numeric_string()
            feature['header'] = header

            # Metadata
            if ftid in md:
                feature['metadata'] = md[ftid]

            # Geometry
            geometry = {}
            if geom_type == MWM.GeomType.POINT or geom_type == MWM.GeomType.POINT_EX:
                geometry['type'] = 'Point'
            elif geom_type == MWM.GeomType.LINE:
                geometry['type'] = 'LineString'
            elif geom_type == MWM.GeomType.AREA:
                geometry['type'] = 'Polygon'
            if geom_type == MWM.GeomType.POINT:
                geometry['coordinates'] = list(self.read_coord())

            # (flipping table emoticon)
            feature['geometry'] = geometry
            if False:
                if geom_type != MWM.GeomType.POINT:
                    polygon_count = self.read_varuint()
                    polygons = []
                    for i in range(polygon_count):
                        count = self.read_varuint()
                        buf = self.f.read(count)
                        # TODO: decode
                    geometry['coordinates'] = polygons
                    feature['coastCell'] = self.read_varint()

                # OSM IDs
                count = self.read_varuint()
                osmids = []
                for i in range(count):
                    osmid = self.read_osmid()
                    osmids.append('{0}{1}'.format(osmid[0], osmid[1]))
                feature['osmIds'] = osmids

            if self.f.tell() > next_feature:
                raise Exception('Feature parsing error, read too much')
            yield feature
            self.f.seek(next_feature)


import argparse
import sys
import csv
import os
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

BATCH = 1
NUM_GENES = 4
N1 = 91
FLAGS = None
mut_name = ['wt','bcd-','tsl-',\
            'bcd1X','bcd4X',\
            'hb-','kr-','kni-','gt-']

def main(_):

  if not os.path.exists(repr(FLAGS.run_num)+'/test'):
    os.mkdir(repr(FLAGS.run_num)+'/test')
  if not os.path.exists(repr(FLAGS.run_num)+'/test/wt'):
    os.mkdir(repr(FLAGS.run_num)+'/test/wt')

  # Create the model
  sess = tf.InteractiveSession()

  xs = np.linspace(0.05,0.95,N1)
  bcd1_init = np.reshape(np.float32( np.exp(-xs/0.2) ),[1,N1,1])
  tsl1_init = np.reshape(np.float32( np.exp(-xs/0.05) + np.exp(-(1-xs)/0.05) ),[1,N1,1])

  W1 = tf.Variable(tf.truncated_normal([NUM_GENES+2, 32], stddev=0.1))
  b1 = tf.Variable(tf.truncated_normal([32], stddev=0.1))
  W2 = tf.Variable(tf.truncated_normal([32, 32], stddev=0.1))
  b2 = tf.Variable(tf.truncated_normal([32], stddev=0.1))
  W3 = tf.Variable(tf.truncated_normal([32,NUM_GENES], stddev=0.1))
  b3 = tf.Variable(tf.truncated_normal([NUM_GENES], stddev=0.1))

  TIME_STEP = 0.1
  DECAYp = 1.0

  # wt
  bcd1 = bcd1_init
  tsl1 = tsl1_init

  x1 = 0.0*tf.ones([1,N1,NUM_GENES]) #[1,N1,NUM_GENES]
  X1_tf =[]
  for t11 in range(10):
    for t12 in range(3):
      x1p = x1
      h10 = tf.concat([x1p,bcd1,tsl1], axis=2)
      a11 = tf.tensordot(h10, W1, [[2],[0]])
      h11 = tf.nn.relu(a11 + b1) #[1,N1,32]
      a12 = tf.tensordot(h11, W2, [[2],[0]])
      h12 = tf.nn.relu(a12 + b2) #[1,N1,32]
      a13 = tf.tensordot(h12, W3, [[2],[0]])
      h13 = tf.nn.sigmoid(a13 + b3)
      x1 = x1*(1-TIME_STEP*DECAYp) + TIME_STEP*h13

    X1_tf.append(x1[0,:,:]) #[N1,4]

  # mutants
  Case   = np.array([[1,1,1, 1,1,1,1],[0,1,1, 1,1,1,1],[1,1,0, 1,1,1,1],\
                     [0.5,1,1, 1,1,1,1],[2,1,1, 1,1,1,1],\
                     [1,1,1, 0,1,1,1],[1,1,1, 1,0,1,1],[1,1,1, 1,1,0,1],[1,1,1, 1,1,1,0],\
                    ],'float32')

  bcd3 = tf.tile(bcd1_init, [len(mut_name),1,1])*tf.reshape(Case[:,0], [len(mut_name),1,1])
  tsl3 = tf.tile(tsl1_init, [len(mut_name),1,1])*tf.reshape(Case[:,2], [len(mut_name),1,1])
  x3 = 0.0*tf.ones([len(mut_name),N1,NUM_GENES]) #[len(mut_name),N1,NUM_GENES]

  for t31 in range(30):
    x3p = x3
    h30 = tf.concat([x3p,bcd3,tsl3], axis=2)
    a31 = tf.tensordot(h30, W1, [[2],[0]])
    h31 = tf.nn.relu(a31 + b1) #[1,N1,32]
    a32 = tf.tensordot(h31, W2, [[2],[0]])
    h32 = tf.nn.relu(a32 + b2) #[1,N1,32]
    a33 = tf.tensordot(h32, W3, [[2],[0]])
    h33 = tf.nn.sigmoid(a33 + b3)
    x3 = x3*(1-TIME_STEP*DECAYp) + TIME_STEP*h33*tf.expand_dims(Case[:,3:],1)

  X3_tf = x3[:,:,:] #[7,N1,4]

  tf.global_variables_initializer().run()
  # saving and loading networks
  saver = tf.train.Saver()
  checkpoint = tf.train.get_checkpoint_state(repr(FLAGS.run_num)+'/savenet')
  if checkpoint and checkpoint.model_checkpoint_path:
    saver.restore(sess, checkpoint.model_checkpoint_path)
    print("Successfully loaded:", checkpoint.model_checkpoint_path)
  else:
    print("Could not find old network weights")

  X1_test, X3_test = sess.run([X1_tf, X3_tf])

  for i in range(len(X1_test)):
    plt.plot(np.linspace(0,1,N1), X1_test[i])
    plt.axis([0,1,0,1.2])
    plt.savefig(repr(FLAGS.run_num)+'/test/wt/t='+repr(i))
    plt.close()
    np.savetxt(repr(FLAGS.run_num)+'/test/wt/t='+repr(i)+'.csv', X1_test[i], fmt='%.4g', delimiter=',')

  for i in range(len(mut_name)):
    x_mut_plot = np.reshape(X3_test[i,:,:], [N1,4])
    plt.plot(np.linspace(0,1,N1), x_mut_plot)
    plt.axis([0,1,0,1.2])
    plt.savefig(repr(FLAGS.run_num)+'/test/%d_'%(i+1)+mut_name[i])
    plt.close()
    np.savetxt(repr(FLAGS.run_num)+'/test/%d_'%(i+1)+mut_name[i]+'.csv', x_mut_plot, fmt='%.4g', delimiter=',')

  sess.close()

if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  parser.add_argument('--run_num', type=int, default=1, help='***')
  FLAGS, unparsed = parser.parse_known_args()
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)


import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import sqlite3

def enter_data():
    accepted = accept_var.get()

    if accepted=="Accepted":
        # User info
        firstname = first_name_entry.get()
        lastname = last_name_entry.get()

        if firstname and lastname:
            title = title_combobox.get()
            age = age_spinbox.get()
            nationality = nationality_combobox.get()

            # Course info
            registration_status = reg_status_var.get()
            numcourses = numcourses_spinbox.get()
            numsemesters = numsemesters_spinbox.get()

            print("First name: ", firstname, "Last name: ", lastname)
            print("Title: ", title, "Age: ", age, "Nationality: ", nationality)
            print("# Courses: ", numcourses, "# Semesters: ", numsemesters)
            print("Registration status", registration_status)
            print("------------------------------------------")

            # Create Table
            conn = sqlite3.connect('data.db')
            table_create_query = '''CREATE TABLE IF NOT EXISTS Student_Data
                    (firstname TEXT, lastname TEXT, title TEXT, age INT, nationality TEXT,
                    registration_status TEXT, num_courses INT, num_semesters INT)
            '''
            conn.execute(table_create_query)

            # Insert Data

            data_insert_tuple = (firstname, lastname, title,
                                  age, nationality, registration_status, numcourses, numsemesters)
            cursor = conn.cursor()
            cursor.execute(data_insert_query, data_insert_tuple)
            conn.commit()
            conn.close()

        else:
            tk.messagebox.showwarning(title="Error", message="First name and last name are required.")
    else:
        tk.messagebox.showwarning(title= "Error", message="You have not accepted the terms")

window = tk.Tk()
window.title("Data Entry Form")

frame = tk.Frame(window)
frame.pack()

# Saving User Info
user_info_frame =tk.LabelFrame(frame, text=" User Information")
user_info_frame.grid(row= 0, column=0, padx=20, pady=10)

first_name_label = tk.Label(user_info_frame, text="First Name")
first_name_label.grid(row=0, column=0)
last_name_label = tk.Label(user_info_frame, text="Last Name")
last_name_label.grid(row=0, column=1)

first_name_entry = tk.Entry(user_info_frame)
last_name_entry = tk.Entry(user_info_frame)
first_name_entry.grid(row=1, column=0)
last_name_entry.grid(row=1, column=1)

title_label = tk.Label(user_info_frame, text="Title")
title_combobox = ttk.Combobox(user_info_frame, values=["", "Mr.", "Ms.", "Dr."])
title_label.grid(row=0, column=2)
title_combobox.grid(row=1, column=2)

def get_selected_options():
    selected_options = []
    if option1_var.get() == 1:
        selected_options.append("Appetizer")
    if option2_var.get() == 1:
        selected_options.append("Meal")
    if option3_var.get() == 1:
        selected_options.append("Drinks")
    print(selected_options)

root = tk.Tk()

option1_var = tk.IntVar()
option2_var = tk.IntVar()
option3_var = tk.IntVar()

label = tk.Label(root, text="Select your options:")
label.pack()

option1 = tk.Checkbutton(root, text="Appetizer", variable=option1_var)
option2 = tk.Checkbutton(root, text="Meal", variable=option2_var)
option3 = tk.Checkbutton(root, text="Drinks", variable=option3_var)

option1.pack()
option2.pack()
option3.pack()

age_label = tk.Label(user_info_frame, text="Age")
age_spinbox = tk.Spinbox(user_info_frame, from_=18, to=110)
age_label.grid(row=2, column=0)
age_spinbox.grid(row=3, column=0)

nationality_label = tk.Label(user_info_frame, text="Nationality")
nationality_combobox = ttk.Combobox(user_info_frame, values=["Africa", "Antarctica", "Asia", "Europe", "North America", "Oceania", "South America"])
nationality_label.grid(row=2, column=1)
nationality_combobox.grid(row=3, column=1)

for widget in user_info_frame.winfo_children():
    widget.grid_configure(padx=10, pady=5)

# Saving Course Info
courses_frame = tk.LabelFrame(frame)
courses_frame.grid(row=1, column=0, sticky="news", padx=20, pady=10)

registered_label = tk.Label(courses_frame, text="Registration Status")

reg_status_var = tk.StringVar(value="Not Registered")
registered_check = tk.Checkbutton(courses_frame, text="Currently Registered",
                                       variable=reg_status_var, onvalue="Registered", offvalue="Not registered")

registered_label.grid(row=0, column=0)
registered_check.grid(row=1, column=0)

numcourses_label = tk.Label(courses_frame, text= "# Completed Courses")
numcourses_spinbox = tk.Spinbox(courses_frame, from_=0, to='infinity')
numcourses_label.grid(row=0, column=1)
numcourses_spinbox.grid(row=1, column=1)

numsemesters_label = tk.Label(courses_frame, text="# Semesters")
numsemesters_spinbox = tk.Spinbox(courses_frame, from_=0, to="infinity")
numsemesters_label.grid(row=0, column=2)
numsemesters_spinbox.grid(row=1, column=2)

for widget in courses_frame.winfo_children():
    widget.grid_configure(padx=10, pady=5)

# Accept terms
terms_frame = tk.LabelFrame(frame, text="Terms & Conditions")
terms_frame.grid(row=2, column=0, sticky="news", padx=20, pady=10)

accept_var = tk.StringVar(value="Not Accepted")
terms_check = tk.Checkbutton(terms_frame, text= "I accept the terms and conditions.",
                                  variable=accept_var, onvalue="Accepted", offvalue="Not Accepted")
terms_check.grid(row=0, column=0)

# Button
button = tk.Button(frame, text="Enter data", command= enter_data)
button.grid(row=3, column=0, sticky="news", padx=20, pady=10)

window.mainloop()


import cv2 as cv
import numpy as np

def main():
    print("Lets make a cartoon!")
    print(cv.__version__)

    img = cv.imread("selfie04.png")

    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    gray = cv.medianBlur(gray, 5)

    edges = cv.adaptiveThreshold(
        gray, 255,
        cv.ADAPTIVE_THRESH_MEAN_C,
        cv.THRESH_BINARY, 9, 9)

    color = cv.bilateralFilter(img, 9, 250, 250)
    cartoon = cv.bitwise_and(color, color, mask=edges)

    cv.imshow("image", img)
    cv.imshow("edges", edges)
    cv.imshow("cartoon", cartoon)
    cv.waitKey(0)

    # cv.destroyWindow("image")
    # cv.destroyWindow("edges")

if __name__ == '__main__':
    main()


import uuid
from typing import Dict, Any, List

import pytest
from _pytest.monkeypatch import MonkeyPatch
from assertpy import assert_that
from mongomock.mongo_client import MongoClient

import shotgrid_leecher.repository.shotgrid_user_repo as shotgrid_repo
from shotgrid_leecher.domain import user_domain
from shotgrid_leecher.record.enums import DbName, DbCollection
from shotgrid_leecher.record.leecher_structures import ShotgridCredentials
from shotgrid_leecher.record.shotgrid_structures import ShotgridProjectUserLink
from shotgrid_leecher.utils import connectivity as conn
from utils.funcs import (
    fun,
)

Map = Dict[str, Any]

def _all_links(client: MongoClient) -> List[Map]:
    return list(
        client.get_database(DbName.LEECHER.value)
        .get_collection(DbCollection.SHOTGRID_PROJ_USER_LINKS.value)
        .find({})
    )

def _insert_credentials(client: MongoClient, dic: Map) -> Any:
    return client.get_database(DbName.LEECHER.value).get_collection(
        DbCollection.SHOTGRID_CREDENTIALS.value
    ).insert_one(dic)

def _fetch_linked_projects(size=10) -> List[ShotgridProjectUserLink]:
    return [
        ShotgridProjectUserLink(
            id=str(x+1),
            host_url=f"http://{str(uuid.uuid4())[:5]}.com",
            type=str(uuid.uuid4())[:5],
            user_email=str(uuid.uuid4())[:5],
            user_name=str(uuid.uuid4())[:5],
            user_id=x+1,
            project_id=x+1,
            project_name=str(uuid.uuid4())[:5],
        )
        for x in range(size)
    ]

@pytest.mark.asyncio
async def test_insert_project_user_links(monkeypatch: MonkeyPatch):
    # Arrange
    client = MongoClient()
    url = f"http://{str(uuid.uuid4())[:5]}.com"
    _insert_credentials(client, ShotgridCredentials(url, "", "").to_mongo())
    monkeypatch.setattr(conn, "get_db_client", fun(client))
    monkeypatch.setattr(shotgrid_repo, "find_linked_projects", fun(_fetch_linked_projects(10)))
    # Act
    await user_domain.synchronize_project_user_links()

    # Assert
    assert_that(_all_links(client)).extracting("_id").is_equal_to(
        list(map(str, range(1, 10+1)))
    )
    assert_that(_all_links(client)).extracting("user_id").is_equal_to(
        list(range(1, 10+1))
    )
    assert_that(_all_links(client)).extracting("project_id").is_equal_to(
        list(range(1, 10+1))
    )


import boto3
import requests
from io import BytesIO
import datetime

S3_BUCKET = 'isar-webcam'
IMAGE_URL = 'http://www.webcammuenchen.com/Prolog-PR.com---cam3.jpg'

def lambda_handler(event, context):
    page = requests.get(IMAGE_URL)
    image = page.content

    now = datetime.datetime.utcnow()
    filename = '{}/isar-{}.jpg'.format(now.date().isoformat(), now.isoformat())

    s3 = boto3.client('s3')
    with BytesIO(image) as f:
        s3.upload_fileobj(f, S3_BUCKET, filename)

    return 'stored ' + filename


#
# @lc app=leetcode id=100 lang=python3
#
# [100] Same Tree
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
        # Use DFS, for most tree problems, using DFS with recursion is easier than BFS
        def dfs(p, q):
            # When two binary trees are empty
            if p == None and q == None:
                return True
            # When only one of two binary trees is empty
            elif p == None or q == None:
                return False
            # If the binary tree is not empty, compare the value
            elif p.val == q.val:
                # When the value of the root node is the same, continue to judge the value of the left and right subtrees
                return dfs(p.left, q.left) and dfs(p.right, q.right)
            # If the value is different, false is returned
            return False

        return dfs(p, q)

# @lc code=end


from sys import setrecursionlimit
def sumArray(arr):
    n = len(arr)
    if n == 0:
        return 0
    if n == 1:
        return arr[0]
    return arr[0] + sumArray(arr[1:])

# Main
setrecursionlimit(11000)
n = int(input())
arr = list(int(i) for i in input().strip().split(' '))
print(sumArray(arr))


import os
import numpy as np
import pyworld
import librosa
import tensorflow as tf
from duration_model import Duration_Graph
from acoustic_model import Acoustic_Graph
from MTTS.mandarin_frontend import txt2label
from utils import match_qs, mmn, read_file, demvn, ori_coarse_coding_features, extract_coarse_coding_and_postion_features
from hyperparams import hyperparams
hp = hyperparams()
import argparse

def only_chinese(sent):
    flag = True
    for ch in sent:
        if ch < '\u4e00' or ch > '\u9fff':
            flag = False
    return flag

def get_norm_vec(fpath: str, dimension: int):
    '''
    :param fpath: String. Normalise vector file path.
    :param dimension: An integer. Normalise vector dimension.
    :return: Pair. If min max vector then min_vec, max_vec, else return mean_vec, std_vec.
    '''
    norm_vec = read_file(fpath, dimension)
    f_vec = np.reshape(norm_vec[0], (-1, dimension))
    s_vec = np.reshape(norm_vec[1], (-1, dimension))
    return f_vec, s_vec

def extend_labels(inputs, duration):
    '''
    :param inputs: Numpy.array. [Unit_num, hp.DUR_IN_DIM]
    :param duration: Numpy.array. [Unit_num, ] or [Unit_num, 1]
    :return: [Phone_num, hp.SYN_IN_DIM]
    '''
    duration = np.reshape(duration, (-1, 1))
    if inputs.shape[0] != duration.shape[0]:
        raise Exception('This is a bug which results to labels unit_nums not equal to Duration unit_nums.')
    outputs = np.zeros((1, hp.SYN_IN_DIM))
    for i in range(inputs.shape[0]):
        frame_number = int(duration[i][0])
        # print('%d phone duration frame : %d'%(i, frame_number))
        coarse_coding_features_matrix = extract_coarse_coding_and_postion_features(ori_coarse_coding_features,
                                                                                frame_number)
        # coarse_coding_features_matrix dimension is [frame_number, 4]
        label_frame_level_metrix = np.tile(inputs[i], (frame_number, 1))
        # label_frame_level_metrix dimension is [frame_number, 467]
        features_matrix = np.concatenate((label_frame_level_metrix, coarse_coding_features_matrix), axis=1)
        outputs = np.concatenate((outputs, features_matrix), axis=0)
    outputs = outputs[1:, :]
    return outputs

def handle(sent, fpath):
    labs = txt2label(sent)
    dur_in = match_qs(labs, hp.DUR_LAB_DIM, False)
    dur_min_vec, dur_max_vec = get_norm_vec(os.path.join(hp.DATA_DIR, 'dur_minmax_vec.npy'), hp.DUR_LAB_DIM)
    dur_in_norm = mmn(inputs=dur_in, min_vec=dur_min_vec, max_vec=dur_max_vec, dimension=hp.DUR_LAB_DIM)
    dur_net = Duration_Graph(mode='infer')
    dur_in_norm = np.reshape(dur_in_norm, [1, -1, hp.DUR_IN_DIM])
    duration = dur_net.infer(dur_in_norm)
    duration = np.reshape(duration, [-1, hp.DUR_OUT_DIM])
    dur_mean_vec, dur_std_vec = get_norm_vec(os.path.join(hp.DATA_DIR, 'dur_meanstd_vec.npy'), hp.DURATION_DIM)
    duration_features = demvn(duration, dur_mean_vec, dur_std_vec, dimension=hp.DURATION_DIM)
    syn_in = extend_labels(dur_in, duration_features)
    syn_min_vec, syn_max_vec = get_norm_vec(os.path.join(hp.DATA_DIR, 'syn_minmax_vec.npy'), hp.SYN_LAB_DIM)
    syn_in_norm = mmn(inputs=syn_in, min_vec=syn_min_vec, max_vec=syn_max_vec, dimension=hp.SYN_LAB_DIM)
    syn_net = Acoustic_Graph(mode='infer')
    syn_in_norm = np.reshape(syn_in_norm, [1, -1, hp.SYN_IN_DIM])
    acoustic = syn_net.infer(syn_in_norm)
    acoustic = np.reshape(acoustic, [-1, hp.SYN_OUT_DIM])
    syn_mean_vec, syn_std_vec = get_norm_vec(os.path.join(hp.DATA_DIR, 'syn_meanstd_vec.npy'), hp.ACOUSTIC_DIM)
    acoustic_features = demvn(acoustic, syn_mean_vec, syn_std_vec, dimension=hp.ACOUSTIC_DIM)
    index = 0
    f0_features = acoustic_features[:, index: index + hp.F0_DIM]
    f0_features = np.reshape(f0_features, (-1))
    index += hp.F0_DIM * 3
    coded_sp_features = acoustic_features[:, index: index + hp.CODED_SP_DIM]
    index += hp.CODED_SP_DIM * 3
    coded_ap_features = acoustic_features[:, index: index + hp.CODED_AP_DIM]
    f0_features = np.array(f0_features, dtype=np.float64)
    coded_sp_features = np.array(coded_sp_features, dtype=np.float64)
    coded_ap_features = np.array(coded_ap_features, dtype=np.float64)
    decoded_sp_features = pyworld.decode_spectral_envelope(coded_sp_features, hp.SR, fft_size=hp.N_FFT)
    decoded_ap_features = pyworld.decode_aperiodicity(coded_ap_features, hp.SR, fft_size=hp.N_FFT)
    new_y = pyworld.synthesize(f0_features, decoded_sp_features, decoded_ap_features, hp.SR)
    librosa.output.write_wav(fpath, new_y, hp.SR)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--sentence', '-s', type=str, help='Synthesize content. Only supported chinese.')
    parser.add_argument('--path', '-f', type=str, help='Synthesized file path.')
    parser.set_defaults(sentence=None)
    parser.set_defaults(path=None)
    args = parser.parse_args()
    sent = args.sentence
    path = args.path
    if sent is None or only_chinese(sent) is False:
        raise Exception('Input sentence is illegal. Only supported to chinese. Please check.')
    if path is None:
        raise Exception('Please input file path.')
    handle(sent, path)

if __name__ == '__main__':
    main()


import os
import shutil
import requests
import itertools

import iprir
from iprir.parser import parse_file
from iprir.database import DB
from iprir.utils import get_logger

__all__ = ('update_text_db', 'update_sql_db', 'update', 'initialize',)

def update_text_db(which='all', *, timeout=30):
    if which == 'all':
        items = iprir.TEXT_DB_URLS.items()
    else:
        items = [(which, iprir.TEXT_DB_URLS[which])]

    for name, url in items:
        _update_text_db(url, iprir.TEXT_DB_PATH[name], timeout=timeout)

def _update_text_db(url, file, *, timeout=30):
    # TODO: record last modified date from server
    text = requests.get(url, timeout=timeout).text

    old_text_db_path = file + '.old'
    # backup
    db_exists = os.path.exists(file)
    if db_exists:
        get_logger().info('backup text db to %s', old_text_db_path)
        shutil.copyfile(file, old_text_db_path)

    try:
        with open(file, 'wt') as fp:
            fp.write(text)
    except:
        get_logger().error('update text db failed: %s', url)
        if db_exists:
            get_logger().info('revert to backup: %s', old_text_db_path)
            os.replace(old_text_db_path, file)
        raise
    else:
        get_logger().info('update text db succeeded: %s', url)
        if db_exists:
            os.unlink(old_text_db_path)

def update_sql_db():
    records = itertools.chain.from_iterable(map(parse_file, iprir.TEXT_DB_PATH.values()))
    # XXX: avoid conflicts, see iprir.tests:test_ip_overlap()
    records = filter(lambda r: r.country != 'AP', records)

    db = DB()
    try:
        ret = db.reset_table()
        assert ret
        ret = db.add_records(records)
        assert ret
    except Exception:
        get_logger().error('update sql db failed')
    else:
        get_logger().info('update sql db succeeded')
    finally:
        db.close()

def update(*, timeout=30):
    update_text_db(timeout=timeout)
    update_sql_db()

def initialize(*, timeout=30):
    is_init = all(map(os.path.exists, iprir.TEXT_DB_PATH.values()))
    if not is_init:
        update(timeout=timeout)
    elif not os.path.exists(iprir.SQL_DB_PATH):
        update_sql_db()

def main():
    from argparse import ArgumentParser

    parser = ArgumentParser(description='update RIR databases')
    parser.add_argument(
        'target', choices=['text', 'sql', 'all'], default='all', nargs='?',
        help='update text db, sqlite db or both',
    )
    parser.add_argument('-t', '--timeout', type=int, default=30)

    option = parser.parse_args()
    if option.target == 'text':
        update_text_db(timeout=option.timeout)
    elif option.target == 'sql':
        update_sql_db()
    elif option.target == 'all':
        update(timeout=option.timeout)
    else:
        assert not 'possible'

if __name__ == '__main__':
    main()