import sys
from PyQt5 import QtCore, QtGui, uic,QtWidgets
import database_commands as db
import makeAHK as ahk

class MyWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        uic.loadUi('replaceit.ui', self)
        self.connections()
        self.load_data()
        self.special_attributes()
        self.show()

    def special_attributes(self):
        self.cancel_button.setEnabled(False)
        self.query_textedit.setReadOnly(True)
        self.description_edit.setReadOnly(True)
        self.hotkey_line_edit.setReadOnly(True)
    def load_data(self):
        self.load_left_list()
        self.load_right_list()

    def load_combo_box(self):
        self.Category_comboBox.addItem("Personal")

    def connections(self):
        self.new_query_btn.clicked.connect(self.add_new_command)
        self.cancel_button.clicked.connect(self.cancel_new_command)
        self.launch_button.clicked.connect(self.launch_hotkeys)
        self.left_to_right_button.clicked.connect(self.left_to_right)
        self.right_to_left_button.clicked.connect(self.right_to_left)
        self.left_list.itemClicked.connect(self.left_load_displays)
        self.right_list.itemClicked.connect(self.right_load_displays)
        self.left_list.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.left_list.customContextMenuRequested.connect(self.left_list_right_click)
        self.right_list.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.right_list.customContextMenuRequested.connect(self.right_list_right_click)
    def add_new_command(self):
        self.new_query_btn.setText("Save")
        self.launch_button.setEnabled(False)
        self.cancel_button.setEnabled(True)
        self.query_textedit.setReadOnly(False)
        self.description_edit.setReadOnly(False)
        self.hotkey_line_edit.setReadOnly(False)
        self.query_textedit.clear()
        self.description_edit.clear()
        self.hotkey_line_edit.clear()
        self.new_query_btn.clicked.disconnect(self.add_new_command)
        self.new_query_btn.clicked.connect(self.save_new_command)
        #self.query_textedit.toPlainText()

    def save_new_command(self):
        #print("Saving...")
        self.new_query_btn.setText("New Command")
        self.new_query_btn.clicked.disconnect(self.save_new_command)
        self.new_query_btn.clicked.connect(self.add_new_command)
        self.launch_button.setEnabled(True)
        self.cancel_button.setEnabled(False)
        command=self.query_textedit.toPlainText()
        hotkey=self.hotkey_line_edit.text()
        description=self.description_edit.toPlainText()
        #print(description)
        #print((conn,command,1,hotkey,description))
        db.insert_command(command,1,hotkey,description)
        self.load_left_list()
    def cancel_new_command(self):
        self.new_query_btn.setText("New Command")
        self.new_query_btn.clicked.disconnect(self.save_new_command)
        self.new_query_btn.clicked.connect(self.add_new_command)
        self.launch_button.setEnabled(True)
        self.cancel_button.setEnabled(False)
        self.query_textedit.setReadOnly(True)
        self.description_edit.setReadOnly(True)
        self.hotkey_line_edit.setReadOnly(True)
        self.query_textedit.clear()
        self.description_edit.clear()
        self.hotkey_line_edit.clear()
    def load_left_list(self):
        self.left_list.clear()
        rows=db.get_unused_commands()
        #print("madeit")
        for row in rows:
            #print("left_list: ",row['hotkey'])
            ''' Current output
            [{'id': 2, 'hotkey': 'new', 'new_text': 'new', 'category_cd': 1, 'description': '', 'in_use_ind': 0}, {'id': 5, 'hotkey': 'wow', 'new_text': 'we can', 'category_cd': 1, 'description': 'do it', 'in_use_ind': 0}]
            '''
            item=QtWidgets.QListWidgetItem(row['hotkey'])
            item.setData(3,row)
            self.left_list.addItem(item)

    def load_right_list(self):
        self.right_list.clear()
        rows=db.get_used_commands()
        for row in rows:
            #print("right_list: ",row['hotkey'])
            ''' Current output
            [{'id': 2, 'hotkey': 'new', 'new_text': 'new', 'category_cd': 1, 'description': '', 'in_use_ind': 0}, {'id': 5, 'hotkey': 'wow', 'new_text': 'we can', 'category_cd': 1, 'description': 'do it', 'in_use_ind': 0}]
            '''
            item=QtWidgets.QListWidgetItem(row['hotkey'])
            item.setData(3,row)
            self.right_list.addItem(item)

    def left_to_right(self):
        modify=[]
        for item in self.left_list.selectedItems():
            it= self.left_list.takeItem(self.left_list.row(item))
            self.right_list.addItem(it)
            modify.append(item.data(3)['id'])
        db.update_in_use_ind(modify,1)
        self.load_left_list()
        self.load_right_list()

    def right_to_left(self):
        modify=[]
        for item in self.right_list.selectedItems():

            it= self.right_list.takeItem(self.right_list.row(item))
            self.left_list.addItem(it)
            modify.append(item.data(3)['id'])
            #print(modify)
        db.update_in_use_ind(modify,0)

        self.load_left_list()
        self.load_right_list()

    def left_load_displays(self):

        self.query_textedit.setPlainText(str(self.left_list.currentItem().data(3)['new_text']))
        self.description_edit.setPlainText(str(self.left_list.currentItem().data(3)['description']))
        self.hotkey_line_edit.setText(str(self.left_list.currentItem().data(3)['hotkey']))
    def right_load_displays(self):

        self.query_textedit.setPlainText(str(self.right_list.currentItem().data(3)['new_text']))
        self.description_edit.setPlainText(str(self.right_list.currentItem().data(3)['description']))
        self.hotkey_line_edit.setText(str(self.right_list.currentItem().data(3)['hotkey']))

    def left_list_right_click(self, QPos):
        #print("left_list_right_click")
        self.listMenu= QtWidgets.QMenu()
        edit_item = self.listMenu.addAction("Edit Item")
        menu_item = self.listMenu.addAction("Remove Item")
        selected_item=self.left_list.currentItem()
        parentPosition = self.left_list.mapToGlobal(QtCore.QPoint(0, 0))
        self.listMenu.move(parentPosition + QPos)
        self.listMenu.show()
    def right_list_right_click(self, QPos):
        #print("left_list_right_click")
        self.listMenu= QtWidgets.QMenu()
        edit_item = self.listMenu.addAction("Edit Item")
        menu_item = self.listMenu.addAction("Remove Item")
        selected_item=self.right_list.currentItem()
        parentPosition = self.right_list.mapToGlobal(QtCore.QPoint(0, 0))
        self.listMenu.move(parentPosition + QPos)
        self.listMenu.show()
    def launch_hotkeys(self):
        rows=db.get_used_commands()
        ahk.make(rows)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    sys.exit(app.exec_())


import os.path
import pickle
import subprocess

from psyrun.scheduler import JobStatus, Scheduler

class MockScheduler(Scheduler):
    def __init__(self, datafile):
        self.datafile = datafile

    @property
    def next_id(self):
        if os.path.exists(self.datafile):
            with open(self.datafile, 'rb') as f:
                return pickle.load(f)['next_id']
        else:
            return 0

    @next_id.setter
    def next_id(self, value):
        self._serialize(next_id=value)

    @property
    def joblist(self):
        if os.path.exists(self.datafile):
            with open(self.datafile, 'rb') as f:
                return tuple(pickle.load(f)['joblist'])
        else:
            return tuple()

    @joblist.setter
    def joblist(self, value):
        self._serialize(joblist=value)

    def _serialize(self, next_id=None, joblist=None):
        if next_id is None:
            next_id = self.next_id
        if joblist is None:
            joblist = self.joblist

        with open(self.datafile, 'wb') as f:
            pickle.dump(
                {'next_id': next_id, 'joblist': joblist},
                f, pickle.HIGHEST_PROTOCOL)

    def submit(
            self, args, output_filename, name=None, depends_on=None,
            scheduler_args=None):
        if depends_on is None:
            depends_on = []
        jobid = self.next_id
        self.next_id += 1
        self.joblist = self.joblist + ({
            'id': jobid,
            'args': args,
            'output_filename': output_filename,
            'name': name or str(jobid),
            'depends_on': depends_on,
            'scheduler_args': scheduler_args,
            'status': '*Q' if len(depends_on) > 0 else 'Q',
        },)
        return jobid

    def mark_running(self):
        updated = []
        for job in self.joblist:
            job.update({'status': 'R'})
            updated.append(job)
        self.joblist = tuple(updated)

    def kill(self, jobid):
        self.joblist = [job for job in self.joblist if job['id'] != jobid]

    def get_status(self, jobid):
        for job in self.joblist:
            if job['id'] == jobid:
                return JobStatus(job['id'], job['status'], job['name'])
        return None

    def consume(self):
        for job in self.joblist:
            with open(job['output_filename'], 'a') as f:
                subprocess.check_call(
                    job['args'], stdout=f, stderr=subprocess.STDOUT)
        self.joblist = []

    def get_jobs(self):
        return [job['id'] for job in self.joblist]


"""
class RNNCell that represents a cell of a simple RNN:

    class constructor def __init__(self, i, h, o):
        i is the dimensionality of the data
        h is the dimensionality of the hidden state
        o is the dimensionality of the outputs
        Creates the public instance attributes Wh, Wy, bh, by that represent the weights and biases of the cell
            Wh and bh are for the concatenated hidden state and input data
            Wy and by are for the output
        The weights should be initialized using a random normal distribution in the order listed above
        The weights will be used on the right side for matrix multiplication
        The biases should be initialized as zeros
    public instance method def forward(self, h_prev, x_t): that performs forward propagation for one time step
        x_t is a numpy.ndarray of shape (m, i) that contains the data input for the cell
            m is the batche size for the data
        h_prev is a numpy.ndarray of shape (m, h) containing the previous hidden state
        The output of the cell should use a softmax activation function
        Returns: h_next, y
            h_next is the next hidden state
            y is the output of the cell
"""
import numpy as np

class RNNCell:
    """class RNNCell"""

    def __init__(self, i, h, o):
        """fn __init__"""
        self.i = i
        self.h = h
        self.o = o
        self.Wh = np.random.normal(size=(self.i + self.h, self.h))
        self.bh = np.zeros((1, self.h))
        self.Wy = np.random.normal(size=(self.h, self.o))
        self.by = np.zeros((1, self.o))

    def forward(self, h_prev, x_t):
        """fn forward"""
        self.hidden_concat = np.concatenate((h_prev.T, x_t.T), axis=0)
        self.h_next = np.tanh((np.matmul(self.hidden_concat.T, self.Wh)) + self.bh)
        self.y = self.softmax(np.matmul(self.h_next, self.Wy) + self.by)
        return self.h_next, self.y

    def softmax(self, y):
        """fn softmax"""
        return np.exp(y) / np.sum(np.exp(y), axis=1, keepdims=True)


"""added lessons_subgroups_members

Revision ID: 2606ce1554df
Revises: 2a96b6b05aec
Create Date: 2021-01-27 02:41:53.862560

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '2606ce1554df'
down_revision = '2a96b6b05aec'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('lessons_subgroups_members',
    sa.Column('lesson_id', sa.Integer(), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['lesson_id'], ['lessons.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.tg_id'], )
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('lessons_subgroups_members')
    # ### end Alembic commands ###


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import uuid

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Product',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=255, verbose_name='name')),
                ('slug', models.SlugField(max_length=255, verbose_name='slug')),
            ],
            options={
                'verbose_name': 'product',
                'verbose_name_plural': 'products',
            },
        ),
        migrations.CreateModel(
            name='Subscription',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('email', models.EmailField(max_length=255, verbose_name='email address')),
                ('email_hash', models.CharField(default=b'', help_text='An MD5 hash of the email address.', max_length=32, editable=False, verbose_name='email hash')),
                ('uuid', models.UUIDField(default=uuid.uuid4, editable=False)),
                ('is_active', models.BooleanField(default=False, verbose_name='is active')),
                ('sub_conf_at', models.DateTimeField(auto_now_add=True, verbose_name='subscription confirmed at', null=True)),
                ('sub_conf_ip', models.GenericIPAddressField(null=True, verbose_name='subscription confirmed ip', blank=True)),
                ('sub_conf_user_agent', models.CharField(default=b'', max_length=200, verbose_name='subscription confirmed user agent', blank=True)),
                ('unsub_conf_at', models.DateTimeField(null=True, verbose_name='unsubscribe confirmed at', blank=True)),
                ('unsub_conf_ip', models.GenericIPAddressField(null=True, verbose_name='unsubscribe confirmed ip', blank=True)),
                ('unsub_conf_user_agent', models.CharField(default=b'', max_length=200, verbose_name='subscribe confirmed user agent', blank=True)),
                ('product', models.ForeignKey(to='subscription.Product')),
            ],
            options={
                'verbose_name': 'subscription',
                'verbose_name_plural': 'subscriptions',
            },
        ),
    ]


import typing
from dataclasses import dataclass

import pytest
from dataclasses_json import dataclass_json
from typing_extensions import Annotated

from flytekit import LaunchPlan, task, workflow
from flytekit.core import context_manager
from flytekit.core.context_manager import CompilationState
from flytekit.core.promise import (
    VoidPromise,
    create_and_link_node,
    create_and_link_node_from_remote,
    translate_inputs_to_literals,
)
from flytekit.exceptions.user import FlyteAssertion
from flytekit.types.pickle.pickle import BatchSize

def test_create_and_link_node():
    @task
    def t1(a: typing.Union[int, typing.List[int]]) -> typing.Union[int, typing.List[int]]:
        return a

    with pytest.raises(FlyteAssertion, match="Cannot create node when not compiling..."):
        ctx = context_manager.FlyteContext.current_context()
        create_and_link_node(ctx, t1, a=3)

    ctx = context_manager.FlyteContext.current_context().with_compilation_state(CompilationState(prefix=""))
    p = create_and_link_node(ctx, t1, a=3)
    assert p.ref.node_id == "n0"
    assert p.ref.var == "o0"
    assert len(p.ref.node.bindings) == 1

    @task
    def t2(a: typing.Optional[int] = None) -> typing.Optional[int]:
        return a

    p = create_and_link_node(ctx, t2)
    assert p.ref.var == "o0"
    assert len(p.ref.node.bindings) == 0

def test_create_and_link_node_from_remote():
    @task
    def t1() -> None:
        ...

    with pytest.raises(FlyteAssertion, match="Cannot create node when not compiling..."):
        ctx = context_manager.FlyteContext.current_context()
        create_and_link_node_from_remote(ctx, t1, a=3)

    ctx = context_manager.FlyteContext.current_context().with_compilation_state(CompilationState(prefix=""))
    p = create_and_link_node_from_remote(ctx, t1)
    assert p.ref.node_id == "n0"
    assert p.ref.var == "placeholder"
    assert len(p.ref.node.bindings) == 0

    @task
    def t2(a: int) -> int:
        return a

    p = create_and_link_node_from_remote(ctx, t2, a=3)
    assert p.ref.var == "o0"
    assert len(p.ref.node.bindings) == 1

def test_create_and_link_node_from_remote_ignore():
    @workflow
    def wf(i: int, j: int):
        ...

    lp = LaunchPlan.get_or_create(wf, name="promise-test", fixed_inputs={"i": 1}, default_inputs={"j": 10})
    ctx = context_manager.FlyteContext.current_context().with_compilation_state(CompilationState(prefix=""))

    # without providing the _inputs_not_allowed or _ignorable_inputs, all inputs to lp become required,
    # which is incorrect
    with pytest.raises(FlyteAssertion, match="Missing input `i` type `<FlyteLiteral simple: INTEGER>`"):
        create_and_link_node_from_remote(ctx, lp)

    # Even if j is not provided it will default
    create_and_link_node_from_remote(ctx, lp, _inputs_not_allowed={"i"}, _ignorable_inputs={"j"})

    # value of `i` cannot be overriden
    with pytest.raises(
        FlyteAssertion, match="ixed inputs cannot be specified. Please remove the following inputs - {'i'}"
    ):
        create_and_link_node_from_remote(ctx, lp, _inputs_not_allowed={"i"}, _ignorable_inputs={"j"}, i=15)

    # It is ok to override `j` which is a default input
    create_and_link_node_from_remote(ctx, lp, _inputs_not_allowed={"i"}, _ignorable_inputs={"j"}, j=15)

@dataclass_json
@dataclass
class MyDataclass(object):
    i: int
    a: typing.List[str]

@pytest.mark.parametrize(
    "input",
    [2.0, MyDataclass(i=1, a=["h", "e"]), [1, 2, 3], ["foo"] * 5],
)
def test_translate_inputs_to_literals(input):
    @task
    def t1(a: typing.Union[float, MyDataclass, Annotated[typing.List[typing.Any], BatchSize(2)]]):
        print(a)

    ctx = context_manager.FlyteContext.current_context()
    translate_inputs_to_literals(ctx, {"a": input}, t1.interface.inputs, t1.python_interface.inputs)

def test_translate_inputs_to_literals_with_wrong_types():
    ctx = context_manager.FlyteContext.current_context()
    with pytest.raises(TypeError, match="Cannot convert"):

        @task
        def t1(a: typing.Union[float, typing.List[int]]):
            print(a)

        translate_inputs_to_literals(ctx, {"a": {"a": 3}}, t1.interface.inputs, t1.python_interface.inputs)

    with pytest.raises(TypeError, match="Cannot convert"):

        @task
        def t1(a: typing.Union[float, typing.Dict[str, int]]):
            print(a)

        translate_inputs_to_literals(ctx, {"a": [1, 2, 3]}, t1.interface.inputs, t1.python_interface.inputs)

    with pytest.raises(
        AssertionError,
        match="Outputs of a non-output producing task n0 cannot be passed to another task",
    ):

        @task
        def t1(a: typing.Union[float, typing.Dict[str, int]]):
            print(a)

        translate_inputs_to_literals(
            ctx,
            {"a": VoidPromise("n0")},
            t1.interface.inputs,
            t1.python_interface.inputs,
        )

def test_optional_task_kwargs():
    from typing import Optional

    from flytekit import Workflow

    @task
    def func(foo: Optional[int] = None):
        pass

    wf = Workflow(name="test")
    wf.add_entity(func, foo=None)

    wf()


#coding: utf-8
import math
import numpy as np
import torch
from torch import nn
from torch.nn import Parameter
from torch.nn.modules.rnn import GRU
from torch.nn.utils.rnn import pad_packed_sequence, pack_padded_sequence

from .cuda_helper import zeros, Tensor, LongTensor, cuda, ones
from .gumbel import gumbel_max, gumbel_max_with_mask
from .storage import Storage

from transformers import GPT2LMHeadModel

class DecoderBase(nn.Module):
	def __init__(self):
		super().__init__()

	def _freerun(self, inp, max_sent_length, nextStep, updateEOS=None, mode='max', top_k=10, temperature=1):
		# inp contains: batch_size, dm, embLayer, max_sent_length, [init_h]
		# nextStep(embedding, flag):  pass embedding to RNN and get gru_h, flag indicates i th sentence is end when flag[i]==1

		# output: w_o emb length

		batch_size = inp.batch_size

		gen = Storage()
		gen.w_pro = []
		gen.w_o = []
		flag = zeros(batch_size).int()
		EOSmet = []

		next_emb = LongTensor([inp.go_id]).repeat(batch_size, 1)

		for i in range(max_sent_length):
			now = next_emb

			w = nextStep(now, flag, i) / temperature

			gen.w_pro.append(w.softmax(dim=-1))
			if mode == "max":
				w = torch.argmax(w, dim=1)
			elif mode == "gumbel" or mode == "sample":
				w_onehot = gumbel_max(w)[0]
				w = torch.argmax(w_onehot, dim=1)
			elif mode == "samplek":
				_, index = w.topk(top_k, dim=-1, largest=True, sorted=True) # batch_size, top_k
				mask = torch.zeros_like(w).scatter_(-1, index, 1.0)
				w_onehot = gumbel_max_with_mask(w, mask)
				w = torch.argmax(w_onehot, dim=1)

			gen.w_o.append(w)
			next_emb = w.unsqueeze(-1)

			EOSmet.append(flag)
			flag = flag | (w == inp.eos_id).int()
			if updateEOS:
				flag = updateEOS(flag)

			if (i+1) % 10 == 0:
				if torch.sum(flag).detach().cpu().numpy() == batch_size:
					break

		EOSmet = 1 - torch.stack(EOSmet)
		gen.length = torch.sum(EOSmet.long(), 0).detach().cpu().numpy()
		seqlen = max(gen.length)
		EOSmet = EOSmet[:seqlen]
		gen.w_o = (torch.stack(gen.w_o[:seqlen]) * EOSmet.long()).transpose(0, 1)

		return gen

	def _beamsearch(self, inp, max_length, nextStep, top_k=10, length_penalty=0.7, temperature=1):

		batch_size = inp.batch_size

		w_o = []
		flag = zeros(batch_size, top_k).byte()
		EOSmet = []
		score = zeros(batch_size, top_k)
		score[:, 1:] = -1e8
		now_length = zeros(batch_size, top_k)
		back_index = []
		regroup = LongTensor([i for i in range(top_k)]).repeat(batch_size, 1)

		next_emb = LongTensor([inp.go_id]).repeat(batch_size, top_k)

		for i in range(max_length):
			now = next_emb

			w, valid_flag = nextStep(now, flag, i, regroup=regroup) # batch_size, top_k, hidden_size
			w = w / temperature
			valid_flag = (1 - flag.float()) * valid_flag.float()

			w = w.log_softmax(dim=-1)

			new_score = (score.unsqueeze(-1) + w * valid_flag.unsqueeze(-1)) / ((now_length.float() + valid_flag + 1e-9).unsqueeze(-1) ** length_penalty)
			new_score[:, :, 1:] = new_score[:, :, 1:] + (1 - valid_flag.float()).unsqueeze(-1) * new_score.min() * 10
			_, index = new_score.reshape(batch_size, -1).topk(top_k, dim=-1, largest=True, sorted=True) # batch_size, top_k

			sum_score = (score.unsqueeze(-1) + w * valid_flag.unsqueeze(-1)).reshape(batch_size, -1)
			# assert (regroup >= new_score.shape[1]).sum().tolist() == 0
			score = torch.gather(sum_score, dim=1, index=index)

			vocab_size = w.shape[-1]
			regroup = index / vocab_size # batch_size, top_k
			back_index.append(regroup)
			w_id = torch.fmod(index, vocab_size) # batch_size, top_k

			# regroup = cuda(torch.LongTensor(np.arange(10))).unsqueeze(0).repeat(32, 1)
			# _, w = w.max(dim=-1)

			# assert (regroup >= flag.shape[1]).sum().tolist() == 0
			flag = torch.gather(flag, dim=1, index=regroup)
			# assert (regroup >= now_length.shape[1]).sum().tolist() == 0
			now_length = torch.gather(now_length, dim=1, index=regroup) + valid_flag

			w_o.append(w_id)
			next_emb = w_id

			EOSmet.append(flag)

			flag = flag | ((w_id == inp.eos_id) * valid_flag).byte()

			if torch.sum(flag).detach().cpu().numpy() == batch_size * top_k:
				break

		#back tracking
		gen = Storage()
		back_EOSmet = []
		gen.w_o = []
		now_index = LongTensor([i for i in range(top_k)]).repeat(batch_size, 1)

		for i, index in reversed(list(enumerate(back_index))):
			gen.w_o.append(torch.gather(w_o[i], dim=1, index=now_index))
			back_EOSmet.append(torch.gather(EOSmet[i], dim=1, index=now_index))
			now_index = torch.gather(index, dim=1, index=now_index)

		back_EOSmet = 1-torch.stack(list(reversed(back_EOSmet)))
		gen.w_o = torch.stack(list(reversed(gen.w_o))) * back_EOSmet.long()
		gen.length = torch.sum(back_EOSmet, 0).detach().cpu().numpy()

		return gen

class GPT2Decoder(DecoderBase):
	def __init__(self, pretrained_model):
		super().__init__()
		self.model = GPT2LMHeadModel.from_pretrained(pretrained_model)

	def forward(self, incoming, attn_mask):
		logits, _ = self.model(incoming, attention_mask=attn_mask)
		return logits

	def init_forward(self):
		past = None

		def nextStep(incoming, stopmask, position):
			nonlocal past
			logits, present = self.model(input_ids=incoming, attention_mask=1-stopmask, past=past)
			past = present
			return logits[:, 0, :]

		return nextStep

	def init_forward_3d(self, batch_size, top_k):
		past = None
		i = 0

		def nextStep(incoming, stopmask, position, regroup=None):
			nonlocal past, i, top_k
			i = position

			new_incoming = incoming.unsqueeze(-1)

			flatten_batch = batch_size * top_k
			new_incoming = new_incoming.reshape(flatten_batch, -1)
			stopmask = stopmask.reshape(flatten_batch)

			if past:
				d1, d2, d3, d4, d5 = past[0].shape
				assert d2 == flatten_batch
				past = tuple(m.reshape(d1, batch_size, top_k, d3, d4, d5).\
						gather(2, index=regroup.unsqueeze(0).unsqueeze(-1).unsqueeze(-1).unsqueeze(-1).repeat(d1, 1, 1, d3, d4, d5)).\
						reshape(d1, flatten_batch, d3, d4, d5) for m in past)

			logits, present = self.model(input_ids=new_incoming, attention_mask=1-stopmask.unsqueeze(-1), \
				past=past)

			past = present

			valid_flag = ones(batch_size, top_k)
			return logits[:, 0, :].reshape(batch_size, top_k, -1), valid_flag

		return nextStep

	def freerun(self, inp, mode='max', top_k=10, temperature=1):
		nextStep = self.init_forward()
		return self._freerun(inp, inp.max_sent_length, nextStep, None, mode, top_k, temperature)

	# def beamsearch(self, inp, top_k, wLinearLayerCallback, input_callback=None, length_penalty=0.7):
	# 	nextStep = self.init_forward_3d(inp.batch_size, top_k, inp.get("init_h", None))
	# 	return self._beamsearch(inp, top_k, nextStep, wLinearLayerCallback, input_callback, length_penalty)

class GPT2Seq2seq(DecoderBase):
	def __init__(self, pretrained_model):
		super().__init__()
		self.model = GPT2LMHeadModel.from_pretrained(pretrained_model)

	def _concat(self, post, post_length, resp, resp_length, go_id):
		batch_size = len(post)
		concat_length = [post_length[i] + resp_length[i] - 1 for i in range(batch_size)]
		indexes = np.zeros((batch_size, max(concat_length)), dtype=int)
		incoming = ones(batch_size, max(concat_length), device=post).long() * go_id
		attn_mask = zeros(batch_size, max(concat_length), device=post)
		type_mask = zeros(batch_size, max(concat_length), device=post).long()

		for i in range(batch_size):
			indexes[i, :post_length[i]] = np.arange(post_length[i])
			indexes[i, post_length[i]:concat_length[i]] = np.arange(resp_length[i] - 1) + post.shape[1] + 1
			attn_mask[i, :concat_length[i]] = 1
			type_mask[i, post_length[i]:concat_length[i]] = 1
		incoming = torch.cat([post, resp], 1).gather(1, LongTensor(indexes))

		return incoming, attn_mask, type_mask

	def _split(self, incoming, post_length, resp_length):
		batch_size = len(incoming)
		indexes = np.zeros((batch_size, max(resp_length)), dtype=int)
		for i in range(batch_size):
			indexes[i, :resp_length[i]-1] = np.arange(resp_length[i] - 1) + post_length[i] - 1
			indexes[i, resp_length[i]-1:] = resp_length[i] - 1 + post_length[i] - 1

		if len(incoming.shape) == 2:
			res = torch.gather(incoming, 1, LongTensor(indexes))
		else:
			res = torch.gather(incoming, 1, LongTensor(indexes).unsqueeze(-1).expand(-1, -1, incoming.shape[-1]))
		return res

	def forward(self, post, post_length, resp, resp_length, go_id):
		incoming, attn_mask, type_mask = self._concat(post, post_length, resp, resp_length, go_id)
		logits, _ = self.model(incoming, attention_mask=attn_mask, token_type_ids=type_mask)
		return self._split(logits, post_length, resp_length)

	def init_forward(self, batch_size, post, post_length):
		past = None
		i = 0
		type_mask = ones(batch_size, post.shape[1], device=post).long()
		for i in range(batch_size):
			type_mask[i, :post_length[i]] = 0

		def nextStep(incoming, stopmask, position):
			nonlocal past, i
			i = position

			if i < post.shape[1]:
				new_incoming = post[:, i:i+1] * (1 - type_mask[:, i:i+1]) + incoming * type_mask[:, i:i+1]
				new_type_mask = type_mask[:, i:i+1]
			else:
				new_incoming = incoming
				new_type_mask = torch.ones_like(type_mask[:, :1])

			logits, present = self.model(input_ids=new_incoming, attention_mask=1-stopmask.unsqueeze(-1), \
				token_type_ids=new_type_mask, past=past)
			past = present
			return logits[:, 0, :]

		def updateStopMask(stopmask):
			nonlocal i
			if i < post.shape[1]:
				return type_mask[:, i].int() * stopmask
			else:
				return stopmask

		return nextStep, updateStopMask

	def init_forward_3d(self, batch_size, post, post_length, top_k):
		past = None
		i = 0
		type_mask = ones(batch_size, post.shape[1], device=post).long()
		for i in range(batch_size):
			type_mask[i, :post_length[i]] = 0
		type_mask = type_mask.unsqueeze(1).expand(batch_size, top_k, post.shape[1])

		def nextStep(incoming, stopmask, position, regroup=None):
			nonlocal past, i, post
			i = position

			if i < post.shape[1]:
				post_3d = post[:, i:i+1].unsqueeze(1).expand(batch_size, top_k, 1)
				new_incoming = post_3d * (1 - type_mask[:, :, i:i+1]) + incoming.unsqueeze(-1) * type_mask[:, :, i:i+1]
				new_type_mask = type_mask[:, :, i:i+1]
			else:
				new_incoming = incoming.unsqueeze(-1)
				new_type_mask = torch.ones_like(type_mask[:, :, :1])

			flatten_batch = batch_size * top_k
			new_incoming = new_incoming.reshape(flatten_batch, -1)
			new_type_mask = new_type_mask.reshape(flatten_batch, -1)
			stopmask = stopmask.reshape(flatten_batch)

			if past:
				d1, d2, d3, d4, d5 = past[0].shape
				assert d2 == flatten_batch
				past = tuple(m.reshape(d1, batch_size, top_k, d3, d4, d5).\
						gather(2, index=regroup.unsqueeze(0).unsqueeze(-1).unsqueeze(-1).unsqueeze(-1).repeat(d1, 1, 1, d3, d4, d5)).\
						reshape(d1, flatten_batch, d3, d4, d5) for m in past)

			logits, present = self.model(input_ids=new_incoming, attention_mask=1-stopmask.unsqueeze(-1), \
				token_type_ids=new_type_mask, past=past)

			past = present

			if i + 1 < post.shape[1]:
				valid_flag = type_mask[:, :, i + 1].int()
			else:
				valid_flag = torch.ones_like(type_mask[:, :, 0])
			return logits[:, 0, :].reshape(batch_size, top_k, -1), valid_flag

		return nextStep

	def freerun(self, inp, mode='max', top_k=10, temperature=1):
		nextStep, updateStopMask = self.init_forward(inp.batch_size, inp.post, inp.post_length)
		gen = self._freerun(inp, inp.max_sent_length + inp.post.shape[1], nextStep, updateStopMask, mode, top_k, temperature)
		res = Storage()
		res.length = np.minimum(gen.length - inp.post_length, np.array([inp.max_sent_length - 1]))
		res.w_o = self._split(gen.w_o, inp.post_length, res.length + 1)
		return res

	def beamsearch(self, inp, top_k, length_penalty=0.7, temperature=1):
		nextStep = self.init_forward_3d(inp.batch_size, inp.post, inp.post_length, top_k)
		gen = self._beamsearch(inp, inp.max_sent_length + inp.post.shape[1], nextStep, top_k, length_penalty, temperature)
		res = Storage()
		res.length = np.minimum(gen.length[:, 0] - inp.post_length, np.array([inp.max_sent_length - 1]))
		res.w_o = self._split(gen.w_o[:, :, 0].transpose(0, 1), inp.post_length, res.length + 1)
		return res


from __future__ import absolute_import

import functools
import logging
import six

from ably.util.unicodemixin import UnicodeMixin

log = logging.getLogger(__name__)

class AblyException(BaseException, UnicodeMixin):
    def __init__(self, reason, status_code, code):
        super(AblyException, self).__init__()
        self.reason = reason
        self.code = code
        self.status_code = status_code

    def __unicode__(self):
        return six.u('%s %s %s') % (self.code, self.status_code, self.reason)

    @staticmethod
    def raise_for_response(response):
        if response.status_code >= 200 and response.status_code < 300:
            # Valid response
            return

        try:
            json_response = response.json()
            if json_response:
                try:
                    raise AblyException(json_response['reason'],
                                        json_response['statusCode'],
                                        json_response['code'])
                except KeyError:
                    msg = "Unexpected exception decoding server response: %s"
                    msg = msg % response.text
                    raise AblyException(msg, 500, 50000)
        except:
            log.debug("Response: %d %s", response.status_code, response.text)
            raise AblyException(
                response.text,
                response.status_code,
                response.status_code * 100)

        raise AblyException("", response.status_code, response.status_code*100)

    @staticmethod
    def from_exception(e):
        if isinstance(e, AblyException):
            return e
        return AblyException("Unexpected exception: %s" % e, 500, 50000)

def catch_all(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            log.exception(e)
            raise AblyException.from_exception(e)

    return wrapper


'''
    Day 13: https://adventofcode.com/2020/day/13
'''

import re

def read_input():
    '''
        Reads the input file and returns a list of all the lines
    '''

    lines = []
    with open('day13input.txt') as f:
        lines = f.read().splitlines()

    return lines

def part_one(lines):
    '''
        Gets the ID of the earliest bus you can take to the airport multiplied by the number of minutes you'll need to wait for that bus.
    '''

    earliest_timestamp = int(lines[0])

    bus_ids = lines[1].split(',')

    highest_mod = 0
    highest_bus_id = 0

    for bus in bus_ids:
        if bus == 'x':
            continue
        bus_id = int(bus)
        mod = earliest_timestamp % bus_id
        if mod > highest_mod:
            highest_mod = mod
            highest_bus_id = bus_id

    return highest_bus_id * (highest_bus_id - highest_mod)

def part_two(lines):
    '''
        Instant version, takes into account that all the numbers are prime, so can adjust the multiplier once we have found the correct time for a bus id
    '''

    lines[1] = lines[1].replace('x', '0')

    bus_ids = list(map(int, lines[1].split(',')))

    new_list = []
    for i, bus in enumerate(bus_ids):
        if bus != 0:
            new_list.append((bus, i))

    multiplier = new_list.pop(0)[0]
    time = multiplier

    for bus, i in new_list:
        while (time + i) % bus != 0:
            time += multiplier
        multiplier *= bus

    return time

def part_two_faster(lines):
    '''
        Doesn't use enumerate and skips all the 0s, uses tuples to hold the offset
    '''

    lines[1] = lines[1].replace('x', '0')

    bus_ids = list(map(int, lines[1].split(',')))

    dict = {}
    new_list = []
    for i, bus in enumerate(bus_ids):
        if bus != 0:
            new_list.append((bus, i))

    start = new_list.pop(0)[0]
    time = start

    found = 1

    while found:
        found = 0
        for bus in new_list:
            if time % bus[0] != bus[0] - bus[1]:
                found = 1
                break
        if found:
            time += start

    return time

def part_two_working(lines):
    '''
        First iteration, will eventually work but will take hours
    '''

    lines[1] = lines[1].replace('x', '0')

    bus_ids = list(map(int, lines[1].split(',')))

    start = bus_ids.pop(0)
    time = start

    found = 1

    while found:
        found = 0
        for i, bus in enumerate(bus_ids):
            if bus != 0:
                if time % bus != bus - (i + 1):
                    found = 1
                    break
        if found:
            time += start

    return time


import unittest
import json
import UserNRank

class Unittest(unittest.TestCase):
    def test(self):
        input = {"name": "First", "point": 10}
        self.assertEqual(UserNRank.ranking(), json.dumps([{"name": "First", "point": 11}, {"name": "hi9", "point": 9}, {"name": "hi8", "point": 8}, {"name": "hi7", "point": 7}, {"name": "hi6", "point": 6}]))
        self.assertEqual(UserNRank.createUser(json.dumps(input)), json.dumps("choose a new name"))

if __name__ == '__main__':
    unittest.main()


import sqlite3
from utils import display
from PyQt5.QtWidgets import QDialog
from PyQt5.QtCore import pyqtSlot
from PyQt5 import uic

# Classe permettant d'afficher la fenêtre de visualisation des données
class AppTablesData(QDialog):

    # Constructeur
    def __init__(self, data:sqlite3.Connection):
        super(QDialog, self).__init__()
        self.ui = uic.loadUi("gui/tablesData.ui", self)
        self.data = data

        # On met à jour l'affichage avec les données actuellement présentes dans la base
        self.refreshAllTables()

    ####################################################################################################################
    # Méthodes permettant de rafraichir les différentes tables
    ####################################################################################################################

    # Fonction de mise à jour de l'affichage d'une seule table
    def refreshTable(self, label, table, query):
        display.refreshLabel(label, "")
        try:
            cursor = self.data.cursor()
            result = cursor.execute(query)
        except Exception as e:
            table.setRowCount(0)
            display.refreshLabel(label, "Impossible d'afficher les données de la table : " + repr(e))
        else:
            display.refreshGenericData(table, result)

    # Fonction permettant de mettre à jour toutes les tables
    @pyqtSlot()
    def refreshAllTables(self):
        self.refreshTable(self.ui.label_equipiers, self.ui.tableEquipiers, "SELECT numEq, numSp  FROM LesEquipiers")
        self.refreshTable(self.ui.label_epreuves, self.ui.tableEpreuves, "SELECT nomDi, numEp, nomEp, formeEp, categorieEp, nbSportifsEp, dateEp FROM LesEpreuves")
        self.refreshTable(self.ui.label_inscriptions, self.ui.tableInscriptions, "SELECT numIn, numEp FROM LesInscriptions")
        self.refreshTable(self.ui.label_resultats, self.ui.tableResultats, "SELECT numEp, gold, silver, bronze FROM LesResultats")
        self.refreshTable(self.ui.label_sportifs, self.ui.tableSportifs, "SELECT numSp, nomSp, prenomSp, pays, categorieSp,age FROM LesSportifs")
        self.refreshTable(self.ui.label_disciplines, self.ui.tableDisciplines,"SELECT nomDi FROM LesDisciplines")
        self.refreshTable(self.ui.label_disciplines, self.ui.tableEquipes, "SELECT numEq,nbEquipiersEq FROM LesEquipes")


'''
1351. Count Negative Numbers in a Sorted Matrix

Given a m * n matrix grid which is sorted in non-increasing order both row-wise and column-wise.

Return the number of negative numbers in grid.

'''
class Solution:
    def countNegatives(self, grid: List[List[int]]) -> int:
        sm = 0
        for outer in grid:
            for inner in outer:
                if inner < 0:
                    sm+=1
        return sm


from django.shortcuts import render, redirect
from district.models import District, Taluka, Village
from .forms import BankForm, BranchForm
from django.contrib.auth.decorators import login_required

# Create your views here.
from .models import Bank, Branch

@login_required(login_url='login')
def add_bank(request):
    forms = BankForm()
    if request.method == 'POST':
        forms = BankForm(request.POST)
        if forms.is_valid():
            forms.save()
            return redirect('bank')
    bank = Bank.objects.all()
    context = {'forms': forms, 'bank': bank}
    return render(request, 'bank/bank.html', context)

@login_required(login_url='login')
def add_branch(request):
    forms = BranchForm()
    if request.method == 'POST':
        forms = BranchForm(request.POST)
        if forms.is_valid():
            forms.save()
            return redirect('branch')
    branch = Branch.objects.all()
    context = {'forms': forms, 'branch': branch}
    return render(request, 'bank/branch.html', context)

@login_required(login_url='login')
def branch_list(request):
    branch = Branch.objects.all()
    context = {'branch': branch}
    return render(request, 'bank/branch_list.html', context)

@login_required(login_url='login')
def load_taluka(request):
    district_id = request.GET.get('district')
    taluka = Taluka.objects.filter(district_id=district_id).order_by('name')

    taluka_id = request.GET.get('taluka')
    village = Village.objects.filter(taluka_id=taluka_id).order_by('name')
    context = {
        'taluka': taluka,
        'village': village
    }
    return render(request, 'others/taluka_dropdown_list_options.html', context)


# https://leetcode.com/problems/custom-sort-string/
from collections import defaultdict

class Solution(object):
    def customSortString(self, S, T):
        """
        :type S: str
        :type T: str
        :rtype: str
        """
        # this holds the number of occurrences of all characters in T
        dictt = dict()
        for i in range(len(T)):
            if T[i] not in dictt.keys():
                dictt[T[i]] = 1
            else:
                dictt[T[i]] += 1

        res =''
        for j in S:
            while j in dictt and dictt[j] > 0:
                res +=j
                dictt[j] -=1
            # remove the dictionary key once you have added all occurrences to res so you dont have to iterate on it again
            dictt.pop(j, None)

        # for the remaining keys that were in T and not in S, just add them to the end of res
        for k in dictt.keys():
            while dictt[k] > 0:
                res +=k
                dictt[k] -=1

        return res


import argparse
import configparser
import re
import sys
from collections import OrderedDict

def ask_choice(prompt, choices):
    if not choices:
        return None
    lowered_choices = [o.lower().strip() for o in choices]
    while True:
        print(":: {prompt}".format(prompt=prompt))
        for i, option in enumerate(choices):
            print("   {i} {option}".format(i=i, option=option))
        answer = input("> ").strip().lower()
        if answer in lowered_choices:
            return answer
        try:
            if 0 <= int(answer) < len(choices):
                return choices[int(answer)]
        except:
            pass

def perform_subsitutions(text, variables):
    """Subsititues any occurances of ${NAME[:DEFAULT]} with the value from dictionary."""
    def evaluate_match(match):
        captures = match.groupdict()
        default = captures.get("default") or ""
        variable_name = captures.get("name") or ""
        if variable_name not in variables:
            return default
        return variables[variable_name]
    return re.sub(r"\$\{(?P<name>\w*):?(?P<default>.*?)\}",
                  evaluate_match,
                  text)

def parse_node_invocation(invocation):
    """Parse a node invocation like `switch_on [(param:value), ...]` into node_id and the variable dict."""
    node_id_and_maybe_variables_text = re.split(
        r"\s+", invocation.strip(), maxsplit=1)
    node_id = node_id_and_maybe_variables_text[0]
    variables = {}
    if len(node_id_and_maybe_variables_text) == 2:
        variables_text = node_id_and_maybe_variables_text[1]
        for variable_text in re.finditer(r"\((?P<variable>[^()]+)\)", variables_text):
            name, value = variable_text.group("variable").split(":")
            variables[name] = value
    return (node_id, variables)

class Node():
    """Represents a node in the decision tree."""
    def __init__(self, node_id, text, root, settings):
        self.node_id = node_id
        self.text = text
        if root is None:
            self.root = False
        elif root.strip().lower() == "true":
            self.root = True
        elif root.strip().lower() == "false":
            self.root = False
        else:
            raise ValueError(
                "node {node_id} had a root value of {root} which could not be parsed".format(
                    node_id=node_id,
                    root=root
                ))
        self.choice_to_node_invocation = OrderedDict()
        for name, value in settings.items():
            branch_match = re.match(r"if_(?P<choice>\w+)", name, re.I)
            if not branch_match:
                continue
            choice = branch_match.groupdict().get("choice")
            if not choice:
                raise ValueError(
                    "A choice text cannot be empty, if_<x> must have a non-empty x")
            self.choice_to_node_invocation[choice] = value

    def run(self, variables):
        """Run the node, returning the next node and invocation variables if any."""
        choices = []
        for choice, _ in self.choice_to_node_invocation.items():
            choices.append(choice)
        choices.append("exit")
        prompt = perform_subsitutions(self.text, variables)
        choice = ask_choice(prompt, choices=choices)
        if choice is None or choice == "exit":
            return None
        node_invocation = self.choice_to_node_invocation[choice]
        node_invocation = perform_subsitutions(node_invocation, variables)
        return parse_node_invocation(node_invocation)

    def __str__(self):
        return "Node(id={node_id}, text={text}, root={root}, choices={choices})".format(
            node_id=self.node_id,
            text=self.text,
            root=self.root,
            choices=self.choice_to_node_invocation
        )

class Scenario():
    """Represents a collection of nodes for a troubleshooting scenario."""
    def __init__(self, config_file_obj):
        config_parser = configparser.ConfigParser()
        config_parser.read_file(config_file_obj)
        self.node_id_to_node = {}
        for node_id, settings in config_parser.items():
            if node_id == configparser.DEFAULTSECT:
                continue
            if settings.get("text") is None:
                raise ValueError("Each node must contain a text setting")
            text = settings.get("text")
            if text is not None:
                del settings["text"]
            root = settings.get("root")
            if root is not None:
                del settings["root"]
            self.node_id_to_node[node_id] = Node(
                node_id,
                text,
                root,
                settings)

    def get_node(self, node_id):
        if not re.match(r"\w+", node_id, re.I):
            raise ValueError("Invalid node id {node_id}".format(node_id=node_id))
        return self.node_id_to_node.get(node_id)

    def get_root_node_id(self):
        for id, node in self.node_id_to_node.items():
            if node.root == True:
                return id
        return None

class Troubleshooter():
    """Create a troubleshooter which uses the given scenario to help users."""
    def __init__(self, scenario: Scenario):
        self.scenario = scenario

    def get_help(self):
        node_id, variables = self.scenario.get_root_node_id(), {}
        if not node_id:
            raise Exception("No root node in scenario")
        while node_id is not None:
            node = self.scenario.get_node(node_id)
            if node is None:
                raise Exception("Node with id {node_id} could not be found".format(node_id=node_id))
            next = node.run(variables)
            if next is None:
                return "OK"
            node_id, variables = next

def main(argv=None):
    argv = argv or sys.argv[1:] or ["--scenario", input("scenario filename: ")]
    parser = argparse.ArgumentParser("Troubleshooter")
    parser.add_argument("--scenario",
                        type=argparse.FileType("r"),
                        required=True)
    result = parser.parse_args(argv)
    troubleshooter = Troubleshooter(Scenario(result.scenario))
    troubleshooter.get_help()

if __name__ == "__main__":
    main()


# -*- coding: utf8 -*-
from flask import request
from flask.globals import _request_ctx_stack
from werkzeug.datastructures import ImmutableMultiDict, CombinedMultiDict, MultiDict

class ServerRequest(object):
    """
    To understand flask.request: https://www.cnblogs.com/MnCu8261/p/10013702.html
        replace current Request object with ServerRequest
        remain the original Request methods and attributes except args/form/json/values
    """

    def __init__(self, data):
        self._raw_request = request._get_current_object()

        self.args = ImmutableMultiDict()
        self.form = ImmutableMultiDict()
        self.json = {}
        if request.method.lower() == "get":
            self.args = ImmutableMultiDict(data)
        elif request.content_type == "application/json":
            self.json = data
        else:
            self.form = ImmutableMultiDict(data)

        setattr(_request_ctx_stack._local.stack[-1], "request", self)

    def __getattr__(self, item):
        return getattr(self._raw_request, item)

    @property
    def values(self):
        args = []
        for d in self.args, self.form:
            if not isinstance(d, MultiDict):
                d = MultiDict(d)
            args.append(d)
        return CombinedMultiDict(args)


from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import ttk, filedialog, messagebox
import pandas as pd
from pandas import DataFrame
from matplotlib import pyplot as plt
import Export as ex
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import gridspec
import numpy as np
from statsmodels.formula.api import ols
import main as mn
import p2main as mn2
import p4main as mn4

df=DataFrame()
scancounter=0
scannum = 0
toggleCI = False

def package3(menuroot):
    menuroot.withdraw()
    root=Toplevel(menuroot)
    root.geometry("733x566")
    root.state('zoomed')

    def OpenFile():
        filename2 = filedialog.askopenfilename(initialdir="/",
                                          title="Select A ZIP",
                                          filetype=(("ZIP files", "*.zip"), ("RAR files","*.rar"),("all files", "*.*")))
        label2_file.configure(text=filename2)

    #code for uploading saved data file
    file_frame = LabelFrame(root, text="Open File")
    file_frame.place(height=800, width=310, x=0, y=0)

    label2_file =ttk.Label(file_frame, text="")
    label2_file.place(x=0, y=10)

    label3_file =ttk.Label(file_frame, text="")
    label3_file.place(x=0, y=50)
    #button for loading saved excel data
    button2 = Button(root, text="Load File", command=lambda:Load_excel_data())
    button2.place(x=100, y=0)

    #tabs
    my_notebook=ttk.Notebook(root)
    my_notebook.place(x=310,y=0, height=785, width=1230)

    #excel data frame 1
    frame1 = LabelFrame(my_notebook,bg='white')
    frame1.pack(fill="both", expand=1)

    my_notebook.add(frame1,text="Discharge")

    #hide tabs before importing
    my_notebook.hide(0)

    # This is the Treeview Widget for frame 1
    tv1 = ttk.Treeview(frame1)
    tv1.place(relheight=1,relwidth=1)

    treescrolly=Scrollbar(frame1,orient="vertical",command=tv1.yview)
    treescrollx=Scrollbar(frame1,orient="horizontal",command=tv1.xview)

    tv1.configure(xscrollcommand=treescrollx.set,yscrollcommand=treescrolly.set)
    treescrollx.pack(side="bottom",fill="x")
    treescrolly.pack(side="right",fill="y")

    #function for loading excel data

    def  Load_excel_data():
    #if your file is valid this will load the file into the treeview
        file_path = label2_file["text"]
        global df
        df=None
        try:

            #if filepath fpr discharge is not empty only load
            if file_path!="":
                excel_filename = r"{}".format(file_path)
                if excel_filename[-4:] == ".csv":
                    if scancounter==0:
                        df = pd.read_csv(excel_filename)
                else:
                    if scancounter==0:
                        df = pd.read_excel(excel_filename)

        except ValueError:
            messagebox.showerror("Information", "The file you have chosen is invalid")
            return None
        except FileNotFoundError:
            messagebox.showerror("Information", f"No such file as {file_path}")
            return None

        clear_data()

        #if Discharge is loaded
        if file_path !="":
            my_notebook.add(frame1,text="Discharge")
            my_notebook.select(0)
            tv1["column"] = list(df.columns)
            tv1["show"] = "headings"
            for column in tv1["columns"]:
                tv1.heading(column, text=column)

            df_rows = df.to_numpy().tolist()
            for row in df_rows:
                tv1.insert("", "end", values=row)

    #################################################
    def clear_data():
        print("data")
        return None

    def ScanFile():

        file_path = label2_file["text"]
        global df
        global scannum
        global scancounter
        if scannum == 1:
            df = pd.read_excel(file_path)
            colname = df.columns[1]
            scancounter = 1

        NewWindow = Toplevel(root)
        NewWindow.title("HyFFlow")
        NewWindow.geometry("500x200")
        NewWindow.resizable(0, 0)

        if df.isnull().values.any():
            label_question = Label(NewWindow, text="Excel file contains NULL values, would you like to remove NULL values?")
            label_question.place(x=100, y=69)
            def RemoveNA():
                df[colname].replace('', np.nan, inplace = True)
                df.dropna(inplace = True)
                messagebox.showinfo("Information","NULL values removed, file is imported successfully")
                NewWindow.destroy()
            b1 = Button(NewWindow, text="Yes", height=1, width=7, bg="lightblue", fg="white", font="bold", command=RemoveNA)
            b1.place(x=130, y=150)

            def NoRemove():
                messagebox.showinfo("Information","Please import another excel file without NULL values")
                NewWindow.destroy()
            b2 = Button(NewWindow, text="No", height=1, width=7, bg="lightblue", fg="white", font="bold", command=NoRemove)
            b2.place(x=280, y=150)
        else:
            messagebox.showinfo("Information", "Excel file contains no error data, file imported successfully")
            NewWindow.destroy()

    def ImportDischarge():
        filename2=None
        filename2 = filedialog.askopenfilename(initialdir="/",
                                           title="Select A File",
                                           filetype=(("xlsx files", "*.xlsx"), ("all files", "*.*")))
        label2_file.configure(text=filename2)
        if filename2!="":
            NewWindow = Toplevel(root)
            NewWindow.title("HyFFlow")
            NewWindow.geometry("500x200")
            NewWindow.resizable(0, 0)

            global scannum
            scannum = 1

            label_question = Label(NewWindow, text="Would you like to scan through the data in the Excel Sheet")
            label_question.place(x=100, y=69)

            b1 = Button(NewWindow, text="Yes", height=1, width=7, bg="lightblue", fg="white", font="bold", command=ScanFile)
            b1.place(x=130, y=150)
            def Continue():
                messagebox.showinfo("Innformation","file is imported successfully ")
                NewWindow.destroy()
            b2 = Button(NewWindow, text="No", height=1, width=7, bg="lightblue", fg="white", font="bold", command=Continue)
            b2.place(x=280, y=150)

    #About
    def About():
        print(".......")

    #Menu

    menu = Menu(root)
    root.config(menu=menu)
    filemenu = Menu(menu, tearoff=0)
    menu.add_cascade(label="File", menu=filemenu)

    Importexcel_menu = Menu(filemenu, tearoff=0)
    Importexcel_menu.add_command(label="Discharge", command=ImportDischarge)

    filemenu.add_cascade(label="Select Excel", menu=Importexcel_menu)

    #submenu for switching Menu
    Switchpackage_menu = Menu(filemenu, tearoff=0)
    Switchpackage_menu.add_command(label="Fundamentals of the Flow Regime", command=lambda:mn.package1(root))
    Switchpackage_menu.add_command(label="Flow Metrics", command=lambda:mn2.package2(root))
    Switchpackage_menu.add_command(label="Long-term Trends on Flow and Rainfall Regimes", command=lambda:mn4.package4(root))
    filemenu.add_cascade(label="Switch to other package", menu=Switchpackage_menu)

    filemenu.add_separator()
    def closepackage():
        root.destroy()
        menuroot.deiconify()
    filemenu.add_command(label="Exit", command=closepackage)

    #Window Menu
    Windowmenu = Menu(menu, tearoff=0)
    menu.add_cascade(label="Window", menu=Windowmenu)
    Windowmenu.add_command(label="About", command=About)

    #Analysis Menu
    Analysismenu = Menu(menu, tearoff=0)
    menu.add_cascade(label="Analysis", menu=Analysismenu)

    #SubMenu for selecting visualization
    Visualization_menu = Menu(Analysismenu, tearoff=0)

    Visualization_menu.add_checkbutton(label="Principal Component Analysis")
    Analysismenu.add_cascade(label="Visualization", menu=Visualization_menu)

    #Help Menu
    helpmenu = Menu(menu, tearoff=0)
    menu.add_cascade(label="Help", menu=helpmenu)
    helpmenu.add_command(label="About", command=About)

    def quit_me():
        root.destroy()
        menuroot.quit()

    root.protocol("WM_DELETE_WINDOW", quit_me)


import numpy as np
import scipy.constants as sp

N = int(1e6)
a = 1e-6

# Product and then sum
aProdSum = 0
for i in range(N):
    aProdSum += a * sp.mu_0
print ("Prod Sum diff: %f\n", aProdSum - sp.mu_0)

# Sum and then product
aSumProd = 0
for i in range(N):
    aSumProd += a
aSumProd *= sp.mu_0
print ("Sum Prod diff: %f\n", aSumProd - sp.mu_0)

# Rounding
for i in range (20):
    print ( i, i/10, i*.1 )

print("END")


import numpy as np
import cv2
from cv_utils import add_text

## Convert COCO keypoints to PoseTrack keypoints
def conver_coco_poseTrack(kps):
    """
        input:  N, 17, 3
            kp_names = [0:'nose', 1:'l_eye', 2:'r_eye', 3:'l_ear', 4:'r_ear', 5:'l_shoulder',
                        6:'r_shoulder', 7:'l_elbow', 8:'r_elbow', 9:'l_wrist', 10:'r_wrist',
                        11:'l_hip', 12:'r_hip', 13:'l_knee', 14:'r_knee', 15:'l_ankle', 16:'r_ankle']
        output: N, 15, 3
            {0: 'right_ankle', 1: 'right_knee', 2: 'right_hip', 3: 'left_hip', 4:'left_knee', 5:'left_ankle',  6:'right_wrist', 7:'right_elbow', 8:'right_shoulder',9:'left_shoulder', 10:'left_elbow', 11:'left_wrist',12:'head_bottom', 13:'nose', 14'head_top']

    """
    MAP ={16:0, 14:1, 12:2, 11:3, 13:4, 15:5, 10:6, 8:7, 6:8, 5:9, 7:10, 9:11, 0:13}

    pose_track_kps = np.zeros((kps.shape[0], 15, 3))

    for k in range(kps.shape[0]):
        pivel = (kps[k][5] + kps[k][6])/2
        head_bottom = (pivel+kps[k][0])/2 # 12
        head_top = 2*kps[k][0] - head_bottom # 14
        head_top[0] = (head_bottom[0] + kps[k][0][0])/2 #改变x

        for coco_i, pt_j in MAP.items():
            pose_track_kps[k][pt_j] = kps[k][coco_i]
        pose_track_kps[k][12] = head_bottom
        pose_track_kps[k][14] = head_top

    return pose_track_kps

def draw_bbox(img, bbox, text=None, change_color=None):
    x1, y1, w, h = bbox
    pt1 = (int(x1), int(y1))
    pt2 = (int(x1+w), int(y1+h))
    if change_color:
        img = cv2.rectangle(img, pt1, pt2, (2,0,2), 3, 2)
    else:
        img = cv2.rectangle(img, pt1, pt2, (255,0,255), 3, 2)

    if text:
        img = add_text(img, str(text), pt1, 1)
    return img


################
# Challenge: ATM
#
# This coding challenge requires you to update the below psuedo code.
# This means the below code has only been partially setup for you, and
# WILL NOT WORK! You will need to make changes to the code to fix it.
#
# Read the requirements and psuedo comments. GOOD LUCK!!!!!!!!!!
#
#
# Requirements:
#	- Create an ATM program
#	- Get user name
#	- Get user withdraw amount
#	- Assume account starts with $1000
#	- Display user name, withdraw amount, remaining total
#
################

# get a name from user
# REMEMBER: the input function
# always returns a string
rename_me = input()

# get a withdraw amount from
# user - this should be a number
# REMEMBER: the input function
# always returns a string
rename_me = input()

# convert the withdraw amount to integer
rename_me = int()

# create an integer variable
# called account_total and
# assign it a value of 1000
rename_me = 0

# subtract withdraw amount
# from account total
rename_me = rename_me - rename_me

# print the user name
print(rename_me)

# print the withdraw amount
print(rename_me)

# print the remaining account total
print(rename_me)


# Copyright 2013 - Mirantis, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

import json

import unittest2

from mistralclient.api.v1 import executions as e
from mistralclient.tests.unit.v1 import base

# TODO(everyone): Later we need additional tests verifying all the errors etc.

EXECS = [
    {
        'id': "123",
        'workbook_name': "my_workbook",
        'task': 'my_task',
        'state': 'RUNNING',
        'context': """
            {
                "person": {
                    "first_name": "John",
                    "last_name": "Doe"
                }
            }
        """
    }
]

URL_TEMPLATE = '/workbooks/%s/executions'
URL_TEMPLATE_ID = '/workbooks/%s/executions/%s'

class TestExecutions(base.BaseClientV1Test):
    def test_create(self):
        mock = self.mock_http_post(content=EXECS[0])
        body = {
            'workbook_name': EXECS[0]['workbook_name'],
            'task': EXECS[0]['task'],
            'context': EXECS[0]['context']
        }

        ex = self.executions.create(EXECS[0]['workbook_name'],
                                    EXECS[0]['task'],
                                    EXECS[0]['context'])

        self.assertIsNotNone(ex)
        self.assertDictEqual(e.Execution(self.executions, EXECS[0]).__dict__,
                             ex.__dict__)

        arg_body = mock.call_args[0][1]
        url = mock.call_args[0][0]

        self.assertEqual(url, URL_TEMPLATE % EXECS[0]['workbook_name'])
        self.assertDictEqual(json.loads(arg_body), body)

    def test_create_with_empty_context(self):
        mock = self.mock_http_post(content=EXECS[0])
        body = {
            'workbook_name': EXECS[0]['workbook_name'],
            'task': EXECS[0]['task']
        }

        self.executions.create(EXECS[0]['workbook_name'],
                               EXECS[0]['task'])

        arg_body = mock.call_args[0][1]
        url = mock.call_args[0][0]

        self.assertEqual(url, URL_TEMPLATE % EXECS[0]['workbook_name'])
        self.assertDictEqual(json.loads(arg_body), body)

    @unittest2.expectedFailure
    def test_create_failure1(self):
        self.mock_http_post(content=EXECS[0])
        self.executions.create(EXECS[0]['workbook_name'],
                               EXECS[0]['task'],
                               "sdfsdf")

    @unittest2.expectedFailure
    def test_create_failure2(self):
        self.mock_http_post(content=EXECS[0])
        self.executions.create(EXECS[0]['workbook_name'],
                               EXECS[0]['task'],
                               list('343', 'sdfsd'))

    def test_update(self):
        mock = self.mock_http_put(content=EXECS[0])
        body = {
            'state': EXECS[0]['state']
        }

        ex = self.executions.update(EXECS[0]['workbook_name'],
                                    EXECS[0]['id'],
                                    EXECS[0]['state'])

        self.assertIsNotNone(ex)
        self.assertEqual(e.Execution(self.executions, EXECS[0]).__dict__,
                         ex.__dict__)
        mock.assert_called_once_with(
            URL_TEMPLATE_ID % (EXECS[0]['workbook_name'], EXECS[0]['id']),
            json.dumps(body))

    def test_list(self):
        mock = self.mock_http_get(content={'executions': EXECS})

        executions = self.executions.list(EXECS[0]['workbook_name'])

        self.assertEqual(1, len(executions))
        ex = executions[0]

        self.assertEqual(e.Execution(self.executions, EXECS[0]).__dict__,
                         ex.__dict__)
        mock.assert_called_once_with(URL_TEMPLATE % EXECS[0]['workbook_name'])

    def test_get(self):
        mock = self.mock_http_get(content=EXECS[0])

        ex = self.executions.get(EXECS[0]['workbook_name'], EXECS[0]['id'])

        self.assertEqual(e.Execution(self.executions, EXECS[0]).__dict__,
                         ex.__dict__)
        mock.assert_called_once_with(
            URL_TEMPLATE_ID % (EXECS[0]['workbook_name'], EXECS[0]['id']))

    def test_delete(self):
        mock = self.mock_http_delete(status_code=204)

        self.executions.delete(EXECS[0]['workbook_name'], EXECS[0]['id'])

        mock.assert_called_once_with(
            URL_TEMPLATE_ID % (EXECS[0]['workbook_name'], EXECS[0]['id']))


import datetime
import os

from smartninja_mongo.odm import Model
from models.database import mongo_db
import smartninja_redis

collection = mongo_db.bitts
redis = smartninja_redis.from_url(os.environ.get("REDIS_URL"))

class Bitt(Model):
    def __init__(self, text, username, **kwargs):
        self.username = username
        self.text = text
        self.created = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')

        if type(self.text) is not str:
            raise TypeError("Bitt text must be a string!")

        super().__init__(**kwargs)

    def insert(self):
        bitt_id = collection.insert_one(self.__dict__).inserted_id
        self._id = bitt_id  # save id from MongoDB into the Bitt object
        self.id = str(bitt_id)  # convert Mongo's ObjectId to string

        # save ID into Redis (last Bitt ID)
        redis.set(name="last-bitt-id", value=str(bitt_id))

        return bitt_id

    @classmethod
    def get_all_bitts(cls):
        bitts = collection.find()

        # some pre-made bitts (you can delete them)
        bitts_dicts = [
            {
                "id": "1e4eabc8654211e99e4eacbc329afba7",
                "username": "Bittman",
                "created": "2019-04-19T07:32:09",
                "text": "I'm fine. Thanks for not asking."
            },
            {
                "id": "9125e4ea654211e99e4eacbc329afba7",
                "username": "bossbabe",
                "created": "2019-04-21T22:05:12",
                "text": "Sometimes you have to unfollow people in real life."
            },
            {
                "id": "6e38adc2654311e99e4eacbc329afba7",
                "username": "karmalicious",
                "created": "2019-04-22T18:40:45",
                "text": "I hope karma slaps you in the face before I do."
            },
        ]

        for bitt in bitts:
            bitt_dict = dict(bitt)

            try:
                bitt_dict["id"] = str(bitt["_id"])  # convert Mongo's ObjectId to string
            except Exception as e:
                print("Error _id to id: {}".format(e))

            bitts_dicts.append(dict(bitt_dict))  # convert bitts from a document type into a dictionary

        # sort by date created (reverse order)
        bitts_dicts.sort(key=lambda x: datetime.datetime.strptime(x['created'], '%Y-%m-%dT%H:%M:%S'), reverse=True)

        return bitts_dicts

    @classmethod
    def get_last_bitt_id(cls):
        last_bitt_id = redis.get(name="last-bitt-id")

        if last_bitt_id:
            last_bitt_id = last_bitt_id.decode()
        else:
            last_bitt = cls.get_all_bitts()[0]
            last_bitt_id = last_bitt["id"]
            redis.set(name="last-bitt-id", value=last_bitt_id)

        return last_bitt_id


from pprint import pprint

with open("recipes.txt", encoding="utf-8") as file:
    cook_book = {}
    for line in file:
        name = line.strip()
        cook_book[name] = []
        ingredients = int(file.readline().strip())
        for ing in range(ingredients):
            new_dict = {}
            split_line = str(file.readline().strip()).split(" | ")
            new_dict.update({"ingredient_name": split_line[0]})
            new_dict.update({"quantity": split_line[1]})
            new_dict.update({"measure": split_line[2]})
            cook_book[name].append(new_dict)
        file.readline()

# pprint(cook_book)

def get_shop_list_by_dishes(dishes, person_count):
    shop_list = {}
    for dish in dishes:
        if dish in cook_book.keys():
            for ingredient in cook_book[dish]:
                new_dict = {}
                key_word = ingredient["ingredient_name"]
                new_dict["measure"] = ingredient["measure"]
                new_dict["quantity"] = int(ingredient["quantity"]) * person_count
                if key_word in shop_list.keys():
                    shop_list[key_word]["quantity"] += int(ingredient["quantity"]) * person_count
                else:
                    shop_list[key_word] = new_dict
    return pprint(shop_list)

# get_shop_list_by_dishes(['Запеченный картофель', 'Омлет'], 2)
# get_shop_list_by_dishes(['Фахитос', 'Омлет'], 2)


import requests
import facebookads.api as fbapi
from facebookads.adobjects.adaccount import AdAccount
from facebookads.adobjects.adsinsights import AdsInsights
from facebookads.adobjects.adset import AdSet
from facebookads.adobjects import user
from facebookads.adobjects.campaign import Campaign
from facebookads.adobjects.ad import Ad
import os
import csv
import const
from datetime import datetime
from datetime import timedelta
import pydash

def get_attribtues_in_class(i_object):
    lt_attributes = []
    lt_values = i_object.__dict__
    for lv_value in lt_values:
        if lv_value[:2] != '__':
            # print(lv_value,'----------',lt_values.get(lv_value))
            lt_attributes.append(lt_values.get(lv_value))
    return lt_attributes

class FbGraph(object):
    def __init__(self, i_access_token, i_api_config):
        # initiate the session and update the headers
        self.client = requests.Session()
        self.client.headers.update({
            "Authorization": "Bearer %s" % i_access_token,
            "Content-Type": "application/json"
        })
        self.access_token = i_access_token
        self.apid = i_api_config.get("client_id")
        self.ap_secret = i_api_config.get("client_secret")
        self.adsapi = fbapi.FacebookAdsApi.init(app_id=self.apid,
                                                app_secret=self.ap_secret,
                                                api_version='v2.10',
                                                access_token=self.access_token)

    def get_user_name(self):
        lv_url = 'https://graph.facebook.com/v2.10/me'
        lv_response = self.client.get(lv_url)
        if lv_response.status_code != 200:
            return {
                "status": 1,
                "errmsg": "Did not receive a valid response "}
        else:
            lt_json = lv_response.json()
            return {"status": 0,
                    "user": lt_json.get('name') + '-(' + lt_json.get(
                        'id') + ')'}

    def get_client(self):
        return self.client

    def get_adaccount(self):
        self.user = user.User('me', self.adsapi)
        lt_fields = [
            'id',
            'name',
            'account_id']

        self.adaccounts = self.user.get_ad_accounts(fields=lt_fields)

        return self.adaccounts

    def get_adsapi(self):
        return self.adsapi

class FbAdaccount(object):
    def __init__(self, adsapi, adaccount_id):
        self.adacc = AdAccount(fbid=adaccount_id, api=adsapi)

    def get_campaign_list(self):
        self.camplist = []
        self.camplist = self.adacc.get_campaigns(fields=['id', 'name'])

    def get_campaigns(self):
        lt_fields = get_attribtues_in_class(Campaign.Field)
        print('Fields for campaign are ------', lt_fields)
        lt_campaign = self.adacc.get_campaigns(fields=lt_fields)
        self.campaigns = {}
        print('result from method ----', lt_campaign)
        for lw_campaign in lt_campaign:
            print('-------', lw_campaign)
            self.campaigns[lw_campaign['id']] = lw_campaign
        print('Campaigns are --------------------', self.campaigns)

    def get_adset_list(self):
        self.adsetlist = []
        self.adsetlist = self.adacc.get_ad_sets(fields=['id', 'name'])

    def get_adsets(self):
        lt_fields = get_attribtues_in_class(AdSet.Field)
        print('Field for Adsets are ------------------------', lt_fields)
        lt_adsets = self.adacc.get_ad_sets(fields=lt_fields)
        self.adsets = {}
        for lw_adset in lt_adsets:
            self.adsets[lw_adset['id']] = lw_adset
        print('Adsets are --------------------------', self.adsets)

    def get_ads_list(self):
        self.adslist = []
        self.adslist = self.adacc.get_ads(fields=['id','name'])

    def get_ads(self):
        lt_fields = get_attribtues_in_class(Ad.Field)
        lt_ads = self.adacc.get_ads(fields=lt_fields)
        self.ads = {}
        for lw_ads in lt_ads:
            self.ads[lw_ads['id']] = lw_ads

    def filter_campaign(self, i_objective=None, i_buyingtype=None):
        lt_filtercampobj = []
        lt_filtercamp = []
        for lw_camp in self.campaigns:
            if lw_camp.get('objective') in i_objective:
                lt_filtercampobj.append(lw_camp)
        for lw_camp in lt_filtercamp:
            if lw_camp.get('buying_type') in i_buyingtype:
                lt_filtercamp.append(lw_camp)
        return lt_filtercamp

class FbObject(object):
    def __init__(self, i_id, adsapi, i_object):
        self.id = id
        self.insights = []
        self.obj = i_object

    def get_params(self, i_params):
        # get the breakdown values
        self.lt_breakdown = []
        self.lt_breakdown = i_params.get(const.CONFIG_FIELDS.FBBREAKDOWN)
        self.lt_parameters = {
            # 'time_range': {'since':i_params.get('since'),'until':i_params.get('until')},
            # 'filtering': [
            #              {'field':'objective','operator':'IN','value':i_params.get(const.CONFIG_FIELDS.FBObjective)},
            #              {'field':'buying_type','operator':'IN','value':i_params.get(const.CONFIG_FIELDS.FBBuyingType)},
            #               ],
            'breakdowns': self.lt_breakdown,

            # 'action_attribution_windows':['1d_view','7d_view','28d_view','default']
        }

    def get_insights(self, i_fields, i_params):

        # self.get_params(i_params)
        # self.daterange = get_date_range(i_since=i_params.get('since'), i_until=i_params.get('until'))
        self.daterange = get_date_range(i_since='2017-07-30',
                                        i_until='2017-08-01')
        print('Date range is ---------------', self.daterange)

        # self.insights = []

        # print('Just before getting insights -----------------------------',lt_parameters)
        # self.insights = self.campaign.get_insights(fields=i_fields, params=lt_parameters)
        # print('Insights for Campaign -------------------------',self.campaignid)
        # print(self.insights)

    def get_insight_formatted(self,i_screenfield):
        lt_fields = []
        lt_output = []
        for lw_insights in self.insights:
            #print('FB insights Raw --------------***************',lw_insights)
            lw_row = {}
            for lw_fields, lv_value in lw_insights.iteritems():
                #print('Field ----------',lw_fields,'Value --------------------',lv_value)
                if self.lt_breakdown != None and lw_fields in self.lt_breakdown:
                    lw_row[lw_fields] = lv_value
                    if not (lw_fields in lt_fields):
                        lt_fields.append(lw_fields)
                else:
                    #####******************************************************************
                    #### This can be improved by using pydash.get
                    #### however for that we need to know the list of all fields which have embedded structure
                    ### parked as of now as the list is unknown
                    for lw_screenfield in i_screenfield:
                        lv_pos = lw_screenfield.find(":")
                        if lv_pos > 0:
                            lv_pre = lw_selfield[:lv_pos]
                            lv_post = lw_selfield[lv_pos+1:]
                            if lv_pre in const.gt_actionfields:
                                lt_actions = lw_insights.get(lv_pre)
                                lv_index = pydash.find_index(lt_actions,['action_type',lv_post])
                                lw_row[lw_screenfield] = lt_actions[lv_index].get("value")
                        else:
                            lv_pre = lw_screenfield
                            lv_post = ""
                            lw_row[lw_screenfield] = lw_insight.get(lw_screenfield)
                        #if lv_pos != -1:
                    # if Type is list then get each field in the list
                    if type(lv_value) == type(lt_fields):
                        # print('Fields are -------',lw_fields)
                        lt_list = lv_value
                        #print('List entries are ----------',lt_list)
                        for lw_list in lt_list:
                            #print('List entry-----------', lw_list)
                            lv_fieldname = lw_fields + ':' + lw_list.get('action_type')
                            # print(lw_fields,'--------Field name in list -----------',lv_fieldname)
                            lv_fieldvalue = lw_list.get('value')
                            lw_row[lv_fieldname] = lv_fieldvalue
                            if not (lv_fieldname in lt_fields):
                                lt_fields.append(lv_fieldname)
                    else:
                        lw_row[lw_fields] = lv_value
                        if not (lw_fields in lt_fields):
                            lt_fields.append(lw_fields)

            lt_output.append(lw_row)
            # print('Row ------------------',lw_row)
        print('Output insights are ----------------', lt_output)
        return lt_output, lt_fields

class FbCampaign(FbObject):
    def __init__(self, i_id, adsapi, i_object):
        FbObject.__init__(self, i_id, adsapi, i_object)
        self.campaign = Campaign(fbid=i_id, api=adsapi)

    def get_params(self, i_params):
        #get the super params
        FbObject.get_params(self, i_params)
        #campaign specific parameters
        self.lt_parameters['filtering'] = [
            {'field': 'objective', 'operator': 'IN',
             'value': i_params.get(const.CONFIG_FIELDS.FBObjective)},
            {'field': 'buying_type', 'operator': 'IN',
             'value': i_params.get(const.CONFIG_FIELDS.FBBuyingType)},
        ]

    def get_insights(self, i_fields, i_params):
        #get the parameters initialized
        self.get_params(i_params)
        #call the super insights method
        FbObject.get_insights(self, i_fields, i_params)
        #for selected date range loop for each day
        for lw_daterange in self.daterange:
            # set the time range as specific day
            self.lt_parameters['time_range'] = {'since': lw_daterange,
                                                'until': lw_daterange}
            lt_insight = []
            #get the insights for that specific day
            lt_insight = self.campaign.get_insights(fields=i_fields,
                                                    params=self.lt_parameters)
            print('Day wise insights are ---------------------------', lw_daterange,'------', lt_insight)
            #for each date wise insights append the date with field "Day"
            for lw_insight in lt_insight:
                lw_insight['Day'] = lw_daterange
                #add the Object details to dictionary --- in this case it will be the feilds specific to the campaign
                lw_insight.update(self.obj)
                self.insights.append(lw_insight)

class FBAdset(FbObject):
    def __init__(self, i_id, adsapi, i_object):
        FbObject.__init__(self, i_id, adsapi, i_object)
        self.Adset = AdSet(fbid=i_id, api=adsapi)

    def get_params(self, i_params):
        #super parameters initialize
        FbObject.get_params(self, i_params)
        #parameter specific to the Adset
        self.lt_parameters['filtering'] = [
            {'field': 'adset.placement.page_types', 'operator': 'ANY',
             'value': i_params.get(const.CONFIG_FIELDS.FBPageType)}
        ]

    def get_insights(self, i_fields, i_params):
        #intialize the parameters before calling the get_insights
        self.get_params(i_params)
        #call super get_insights
        FbObject.get_insights(self, i_fields, i_params)
        # self.insights = self.Adset.get_insights(fields=i_fields, params=self.lt_parameters)
        # print('Insights for the Adset are --------------',self.insights)

        #for each day in selected date range
        for lw_daterange in self.daterange:
            #set the time_range parameter
            self.lt_parameters['time_range'] = {'since': lw_daterange,
                                                'until': lw_daterange}
            lt_insight = []
            #get the insights for that day
            lt_insight = self.Adset.get_insights(fields=i_fields,
                                                 params=self.lt_parameters)
            #print('Adsets Insights for day ----', lw_daterange, '--------')
            #print(lt_insight)

            for lw_insight in lt_insight:
                #add the day field in the output since its fetched daywise
                lw_insight['Day'] = lw_daterange
                #add the Object details to dictionary --- in this case it will be the feilds specific to the Adset
                lw_insight.update(self.obj)
                self.insights.append(lw_insight)

class FBAds(FbObject):
   def __init__(self, i_id, adsapi, i_object):
        FbObject.__init__(self, i_id, adsapi, i_object)
        self.Ads = Ad(fbid=i_id, api=adsapi)

   def get_params(self, i_params):
       #get the super parameter
       FbObject.get_params(self, i_params)
       #Ad specific parameter
       self.lt_parameters['filtering'] = [
           {'field': 'adset.placement.page_types', 'operator': 'ANY',
            'value': i_params.get(const.CONFIG_FIELDS.FBPageType)}
       ]
   def get_insights(self, i_fields, i_params):
       #get the parameter set before calling the insights
       self.get_params(i_params)
       #get the super insights
       FbObject.get_insights(self, i_fields, i_params)
       #for each day in selected date range
       for lw_daterange in self.daterange:
           #set the time_range parameter
           self.lt_parameters['time_range'] = {'since': lw_daterange,
                                               'until': lw_daterange}
           print('Parameters just before calling the method ----------------',
                 self.lt_parameters)
           lt_insight = []
           #get the insights
           lt_insight = self.Ads.get_insights(fields=i_fields,
                                                params=self.lt_parameters)
           print('Ads Insights for day ----', lw_daterange, '--------')
           print(lt_insight)
           #for each insights add the date field since its fetched date wise
           for lw_insight in lt_insight:
               #Day field is added
               lw_insight['Day'] = lw_daterange
               #add the Object details to dictionary --- in this case it will be the feilds specific to the Ads
               lw_insight.update(self.obj)
               self.insights.append(lw_insight)

def get_fields(i_group):
    print('Group --------------------------------', i_group)
    print('Infields -------------------------', const.gt_infields)
    lt_outdisplay = []
    lt_fields = const.gt_infields.get(i_group)
    for lv_key, lw_infields in lt_fields.iteritems():
        if len(lw_infields) == 0:

            lt_outdisplay.append(lv_key)
        else:
            for lw_row in lw_infields:
                lt_outdisplay.append(lv_key + ':' + lw_row)

    return lt_outdisplay

def get_field_description(i_fname):
    return const.gt_fielddesc.get(i_fname)

def get_date_range(i_since, i_until):
    lv_sincedate = datetime.strptime(i_since, '%Y-%m-%d')
    print('Since date - ', lv_sincedate)

    lv_untildate = datetime.strptime(i_until, '%Y-%m-%d')
    print('Until Date - ', lv_untildate)
    lt_dates = []
    while lv_sincedate <= lv_untildate:
        lt_dates.append(lv_sincedate.date().strftime('%Y-%m-%d'))
        lv_sincedate = lv_sincedate + timedelta(days=1)
        # print('After adding a day ----', lv_sincedate)

    # print('List of dates is as follows -------------------', lt_dates)

    return lt_dates


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
from __future__ import absolute_import

broker_url = 'redis://127.0.0.1:6379/3'

#必须开启result_backend才能获取task执行的状态信息
result_backend = 'redis://127.0.0.1:6379/3'

result_expires = 10

timezone = 'Asia/Shanghai'

#imports = ('tasks.my_tasks')
imports = ()

#任务的路由，按需修改'*'的路由到指定的query，celery的启动脚本中的Queue的配置做对应的修改
task_routes = {
        'celery.ping': 'default',
        '*': 'weps_celery'
        }


import random
import turtle

def randColor():
    col = ["red", "yellow", "green", "blue", "purple", "orange"]
    return col[random.randint(0,len(col)-1)]

def mouseInput(x,y):
    print(x,',',y)

class shape():
    def __init__(self,xpos=0,ypos=0,fillcolor='',filled=False):
        self.__x = xpos
        self.__y = ypos
        self.__fill = fillcolor
        self.__isFilled = filled

    def setFillColor(self,color):
        self.__fill = color

    def setFilled(self,filled):
        self.__isFilled = filled

    def isFilled(self):
        return self.__isFilled

    def getFillColor(self):
        return self.__fill

    def getX(self):
        return self.__x
    def getY(self):
        return self.__y

class circle(shape):
    def __init__(self,xpos=0,ypos=0,rad=1):
        self.__x = xpos
        self.__y = ypos
        self.__radius = rad
        super().__init__(xpos,ypos)

    def draw(self,turtle):
        turtle.hideturtle()
        turtle.speed(0)
        turtle.pu()
        turtle.goto(self.__x,self.__y - self.__radius)
        turtle.pd()
        if self.isFilled():
            turtle.fillcolor(self.getFillColor())
            turtle.begin_fill()
            turtle.circle(self.__radius)
            turtle.end_fill()
        else:
            turtle.circle(self.__radius)

    def isIN(self,x=None,y=None):
        if x != None and y != None:
            if ((x-self.__x)**2 + (y-self.__y)**2) <= self.__radius**2:
                return True
        return False

class rectangle(shape):
    def __init__(self,xpos=0,ypos=0,length=1,width=1):
        self.__x = xpos
        self.__y = ypos
        self.__l = length
        self.__w = width
        super().__init__(xpos,ypos)

    def getPos(self):
        return self.__x, self.__y

    def draw(self,turtle):
        turtle.hideturtle()
        turtle.speed(0)
        turtle.pu()
        turtle.goto(self.__x - (self.__l/2), \
                    self.__y + (self.__w/2))
        turtle.seth(0)
        turtle.pd()
        if self.isFilled():
            turtle.fillcolor(self.getFillColor())
            turtle.begin_fill()
            for each in range (0,2):
                turtle.forward(self.__l)
                turtle.right(90)
                turtle.forward(self.__w)
                turtle.right(90)
            turtle.end_fill()
        else:
            for each in range (0,2):
                turtle.forward(self.__l)
                turtle.right(90)
                turtle.forward(self.__w)
                turtle.right(90)

    def isIN(self,x=None,y=None):
        if x != None and y != None:
            if (x <= self.__x + self.__l/2) and \
               (x >= self.__x - self.__l/2) and \
               (y <= self.__y + self.__w/2) and \
               (y >= self.__y - self.__w/2):
                return True
        return False

    def getHeight(self):
        return self.__w
    def getLength(self):
        return self.__l
    def getX(self):
        return self.__x
    def getY(self):
        return self.__y
    def setLength(self,leng):
        self.__l = leng

class Button(rectangle):
    def __init__(self,xpos=0,ypos=0,length=1,width=1,label='',fun=None):
        self.__label= label
        self.__func = fun
        super().__init__(xpos,ypos,length,width)
        if length < int(len(label)*.64*super().getHeight()):
            super().setLength(int(len(label)*.64*super().getHeight()))

    def draw(self,turtle):
        super().draw(turtle)
        turtle.pu()
        turtle.goto(super().getX(),super().getY()-super().getHeight
                    ()/2)
        turtle.write(self.__label,False,'center', \
                     font=('Courier',str(super().getHeight()),'normal'))
    def act(self):
        self.self.__func()

class Display():
    def __init__(self):
        self.__myt = turtle.Turtle()
        self.__scr = turtle.Screen()
        self.__scr.delay(0)
        self.__myt.speed(0)
        self.__myt.hideturtle()
        self.__elements = []
        self.__scr.onscreenclick(self.mouseEvent)
        self.__scr.listen()

    def mouseEvent(self,x=0,y=0):
        __removed = False
        for shape in self.__elements:
            if shape.isIN(x,y):
                self.remove(shape)
                __removed= True
                break
        if not __removed:
            if random.randint(0,1) == 0:
                sh = circle(x,y,random.randint(10,100))
            else:
                sh = rectangle(x,y,random.randint(20,200), \
                               random.randint(20,200))
            sh.setFillColor(randColor())
            sh.setFilled(True)
            self.add(sh)

    def add(self,shape):
        self.__elements.append(shape)
        shape.draw(self.__myt)

    def remove(self,shape):
        self.__elements.remove(shape)
        self.__myt.clear()
        for sh in self.__elements:
            sh.draw(self.__myt)
            if sh == self.__elements[-1]:
                break


# coding: utf-8
"""
1、找到未加密的参数
2、想办法把参数进行加密（必须参考网易逻辑），params，encSecKey
3、请求到网页，抓取评论信息
"""
import requests
# 需要安装pycrypto库，用于AES加密，安装命令：pip install pycryptodome
from Crypto.Cipher import AES
from base64 import b64encode
import json

url = "https://music.163.com/weapi/comment/resource/comments/get?csrf_token="
songId = 1325905146

# 请求方式：POST
# 经过解析发现：params _-> encText, encSecKey --> encSecKey
data = {
    "csrf_token": "",
    "cursor": "-1",
    "offset": "0",
    "orderType": "1",
    "pageNo": "1",
    "pageSize": "20",
    "rid": f"R_SO_4_{songId}",
    "threadId": f"R_SO_4_{songId}"
}

# 这些参数都是服务与加密函数的，window.asrsea，其中i可以在网页中查到，然后写死
e = "010001"
f = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7"
g = "0CoJUm6Qyw8W8jud"
i = "PvLgW8IKGXQDWBsm"

def get_encSecKey():  # 由于i是固定的，所以encSecKey是固定的
    return "5757f6675f9df72e1d15570d55554f85378efb20855b54670e12b10fbd4339d48ec7156d91f5bc8c62082eba565720c7b1b1c0036589802cd1f884d2cf0cfe9a4070dbb203abd22d9f88dfc60b15ba171098d68431b7f54e65e24dd76a34917180025bf942d128ca281223b4eedca94c008deb0e8352576ae50e3e65e3b6387b"

def get_params(data):  # data默认是字符串
    first_enc = enc_params(data, g)
    second_enc = enc_params(first_enc, i)
    return second_enc  # 返回的就是params

def to_16(data):  # 转化成16的倍数，为下发的加密算法服务
    pad = 16 - len(data) % 16
    data += chr(pad) * pad
    return data

def enc_params(data, key):  # 加密过程
    iv = "0102030405060708"
    data = to_16(data)
    aes = AES.new(key=key.encode("utf-8"), IV=iv.encode("utf-8"), mode=AES.MODE_CBC)  # 创建加密器
    bs = aes.encrypt(data.encode("utf-8"))  # 加密，加密的内容长度必须是16的倍数
    return str(b64encode(bs), "utf-8")  # 转化位字符串返回

# 处理加密过程，window.asrsea
"""
    function a(a) {  # a=16，随机的16位字符串
        var d, e, b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c = "";
        for (d = 0; a > d; d += 1)  # 循环a次，即16次
            e = Math.random() * b.length,  # 随机数 * b的长度
            e = Math.floor(e),  # 取e的整数，e = "010001"，即1
            c += b.charAt(e);  # 取字符串中的xxx位置 b
        return c
    }
    # AES加密算法要三个参数：原文、密钥和偏移量，其中iv是偏移量，e是原文，所以反推c是加密的密钥，而c由b得到，所以b是密钥
    function b(a, b) {  # a：需要加密的数据
        var c = CryptoJS.enc.Utf8.parse(b) # b是密钥
          , d = CryptoJS.enc.Utf8.parse("0102030405060708")
          , e = CryptoJS.enc.Utf8.parse(a) # 得到的结果e是通过对a进行加密得到的
          , f = CryptoJS.AES.encrypt(e, c, {  # c是加密的密钥
            iv: d,  # AES加密算法中的偏移量
            mode: CryptoJS.mode.CBC  # 模式是：CBC
        });
        return f.toString()
    }
    function c(a, b, c) {  # 函数c()不产生随机数
        var d, e;
        return setMaxDigits(131),
        d = new RSAKeyPair(b,"",c),
        e = encryptedString(d, a)
    }
    function d(d, e, f, g) {  d:数据，e:010001，f:很长， g:0CoJUm6Qyw8W8jud
        var h = {}
          , i = a(16);  # 往上找a，输出16位的随机值，在浏览器里边打短短的，可以找到i的值，"PvLgW8IKGXQDWBsm"，给它写死
        return h.encText = b(d, g), # d是原文，g是密钥
        h.encText = b(h.encText, i),  # 得到的就是params，加密过程，分两次进行加密，第一次是b(d, g)，之后将该加密得到的
        结果再次和i进行b(第一次加密的结果, i)加密，通过b函数分析，可以得到第一次加密的结果是原文，i是密钥
        h.encSecKey = c(i, e, f),  # 得到的就是encSecKey，其中e和f是定值，
        是写死的，唯一变化的是i，即a(16)，里边有随机值，如果将i固定写死，那么c()函数输出的值，也是定值，得到的encSecKey也是定值，以此来破解
        h
    }
    window.asrsea = d,
    var bKf6Z = window.asrsea(JSON.stringify(i8a), bva3x(["流泪", "强"]), bva3x(Tu8m.md), bva3x(["爱心", "女孩", "惊恐", "大笑"]));

    把bva3x(["流泪", "强"])丢到浏览器控制台，输出：010001
    把bva3x(Tu8m.md)丢到浏览器控制台，输出：
    00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7
    把bva3x(["爱心", "女孩", "惊恐", "大笑"])到浏览器控制台，输出：0CoJUm6Qyw8W8jud
    d: 数据, 010001, 上面那张长的数字和字母组合 , 0CoJUm6Qyw8W8jud
"""

# 发送请求
response = requests.post(url, data={
    "params": get_params(json.dumps(data)),
    "encSecKey": get_encSecKey()
})
print(response.json())

JS破解/网易云音乐热评/网易云音乐热评.py


class Solution:
    def topKFrequent(self, nums,k):
        dict = {}
        for i in nums:
            dict[i] = dict.get(i,0) + 1
        array = sorted(dict.items(),key = lambda x:x[1],reverse=True)
        result = []
        for i in range(k):
            result.append(array[i][0])
        return result


# Modify the program so that the exits is a dictionary rather than a list,
# with the keys being the numbers of the locations and the values being
# dictionaries holding the exits (as they do at present). No change should
# be needed to the actual code.
#
# Once that is working, create another dictionary that contains words that
# players may use. These words will be the keys, and their values will be
# a single letter that the program can use to determine which way to go.

locations = {
    0: "You are sitting in front of a laptop and learning Python",
    1: "Road",
    2: "Hill",
    3: "You are in the, building",
    4: "Valley",
    5: "Forest"
}

exits = {0: {"Q": 0},
         1: {"W": 2, "E": 3, "N": 5, "S": 4, "Q": 0},
         2: {"N": 5, "Q": 0},
         3: {"W": 1, "Q": 0},
         4: {"N": 1, "W": 2, "Q": 0},
         5: {"W": 2, "S": 1, "Q": 0} }

vocab = { "QUIT" : "Q",
          "NORTH": "N",
          "SOUTH": "S",
          "EAST": "E",
          "WEST": "W"  }

#    print(locations[0].split()) # default delimiter is space ' '
#    print(locations[3].split(",")) # , is a delimiter, the string will split from this ,
#    print(' '.join(locations[0].split(' ')))

# starting at location 1: Road
loc = 1

while True:
    #available_exits = ""
    #for direction in exits[loc].keys():
    #   available_exits += direction + ', '

    available_exits = ', '.join(exits[loc].keys())

    print(locations[loc])

    if loc == 0:
        break

    direction = input("Available exits are:  "+ available_exits+' :').upper()
    print()

    # Parse the user input if necessary - Method 1
    #if len(direction) > 1: # if the user input contains more than one characters
    #    for words in vocab: # iterate through all the keys in vocab
    #        if words in direction: # iterate through all the words in the user input and if vocab[words] == direction[words]
    #            direction = vocab[words]

    # Parse the user input if necessary - Method 2
    if len(direction) > 1: # if the user input contains more than one characters
        words = direction.split()
        for word in words:
            if word in vocab:
                direction = vocab[word]
                break

    if direction in exits[loc]:
        loc = exits[loc][direction]
    else:
        print('You cannot go in that direction')

Python Masterclass/Section 9 - Dictionaries and Sets/Challenges/challenge9.py