"""
Things Database Connector
"""
#%% Import packages
import pandas as pd
import sqlite3
import datetime as dt
import plotly.express as px
import numpy as np
import dtale
from dash import html
import csv

import os
import shutil
import seaborn as sns
import time as time
import datetime
import matplotlib.pyplot as plt
import plotly.graph_objects as go

import dash as dash
from dash import dcc
from flask import request
from dash.dependencies import Input, Output, State
from credentials import user

#%% Connect to Database
sql_connect = sqlite3.connect(f'{user}/Library/Group Containers/JLMPQHK86H.com.culturedcode.ThingsMac/Things Database.thingsdatabase/main.sqlite')
cursor = sql_connect.cursor()

#%% SQL-Query
query = 'SELECT uuid, title, notes, area, actionGroup, userModificationDate, creationDate, dueDate, startDate, stopDate, status, project, type FROM TMTask;'
query2 = 'SELECT uuid, userModificationDate, creationDate, title, status, stopDate, "index", task FROM TMChecklistItem;'
query3 = 'SELECT uuid, title FROM TMArea;'
results = cursor.execute(query).fetchall()

#%% Executing Query
data = dt
data = pd.read_sql_query(query, sql_connect)
data2 = pd.read_sql_query(query2, sql_connect)
data3 = pd.read_sql_query(query3, sql_connect)

#%% Close connection
sql_connect.close()

#%% Calculating KPIs
tasks_number = data.groupby('project')['title'].agg('count')
print(tasks_number)

#%% Preparing Data

data_projects = data[['uuid', 'title', 'area']]
data_projects = data_projects[data_projects.area.notnull()]
data_projects = data_projects.sort_values('uuid')
data_projects = data_projects.rename(columns={'uuid':'project_id', 'title':'project_title', 'area':'block_id'})

data_area = data[['uuid', 'title', 'project']]
data_area = data_area[data_area.project.notnull()]
data_area = data_area.sort_values('uuid')
data_area = data_area.rename(columns={'uuid': 'area_id', 'title': 'area_title'})

data3 = data3.rename(columns={'uuid': 'block_id', 'title': 'block_title'})

#%% Joining Data areas

data_joined0 = pd.merge(data, data_area, how='left', left_on='actionGroup', right_on='area_id')
data_joined0['project_id'] = np.where(data_joined0['project_y'].isnull(), data_joined0['project_x'], data_joined0['project_y'])
data_joined0 = data_joined0.rename(columns=({'uuid':'task_id', 'title':'task_title', 'actionGroup':'area_id_x'}))
data_joined0 = data_joined0[['task_id', 'task_title', 'notes', 'area_id', 'area_title', 'project_id', 'userModificationDate', 'creationDate', 'dueDate', 'startDate', 'stopDate', 'status', 'type']]

#%% Joining data projects

data_joined = pd.merge(data_joined0, data_projects, how='left', left_on='project_id', right_on='project_id')
# data_joined = data_joined[data_joined..notnull()]

#%%
data_joined2 = pd.merge(data_joined, data3, how='left', left_on='block_id', right_on='block_id')

#%%
data_arranged = data_joined2[['task_id',
                              'task_title',
                              'notes',
                              'area_title',
                              'area_id',
                              'project_title',
                              'project_id',
                              'block_title',
                              'block_id',
                              'type',
                              'status',
                              'userModificationDate',
                              'creationDate',
                              'dueDate',
                              'startDate',
                              'stopDate']]

#%% Dtale Data Exploration
dtale.show(data_joined2)

#%% Converting Types
convert_dict = {'task_id': str,
                 'task_title': str,
                 'notes': str,
                 'userModificationDate': float,
                 'creationDate': float,
                 'dueDate': float,
                 'startDate': float,
                 'stopDate': float,
                 'status': int,
                 'type': int}

data_labelled = data_arranged.astype(convert_dict)

#%% Converting Timestamps
data_labelled['creationDate'] = pd.to_datetime(round(data_labelled['creationDate']), format=None, unit='s', errors='coerce')
data_labelled['startDate'] = pd.to_datetime(round(data_labelled['startDate']), format=None, unit='s', errors='coerce')
data_labelled['userModificationDate'] = pd.to_datetime(round(data_labelled['userModificationDate']), format=None, unit='s', errors='coerce')
data_labelled['dueDate'] = pd.to_datetime(round(data_labelled['dueDate']), format=None, unit='s', errors='coerce')
data_labelled['stopDate'] = pd.to_datetime(round(data_labelled['stopDate']), format=None, unit='s', errors='coerce')

#%% Mapping Values
data_labelled['status'] = data_labelled['status'].map({3:'gelöscht', 2:'unkown', 1:'unknown2'})
data_labelled['type'] = data_labelled['type'].map({2:'area', 1:'block', 0:'task'})

#%% Calculating Random Column

data_labelled['randCol'] = np.random.randint(1, 5, data_labelled.shape[0])
data_labelled['randCol2'] = np.random.randint(1, 1000, data_labelled.shape[0])

data_labelled['project_title'] = data_labelled['project_title'].fillna(0)
data_labelled['area_title'] = data_labelled['area_title'].fillna(0)
data_labelled['block_title'] = data_labelled['block_title'].fillna(0)
data_labelled['status'] = data_labelled['status'].fillna(0)

#%% Subsetting Data
subset_tasks = data_labelled[data_labelled['type'] == 'task']
subset_tasks = subset_tasks[['task_id', 'task_title', 'notes', 'area_title', 'project_title', 'project_id', 'block_title', 'status', 'type', 'userModificationDate', 'creationDate', 'dueDate', 'startDate', 'stopDate']]

subset_area = data_labelled[data_labelled['type'] == 'area']
subset_area = subset_area[['task_id', 'task_title',  'area_id', 'project_id', 'area_title', 'status', 'type', 'userModificationDate', 'creationDate', 'dueDate', 'startDate', 'stopDate']]

subset_project = data_labelled[data_labelled['type'] == 'block']
subset_project = subset_project[['task_id', 'task_title', 'notes', 'status', 'type', 'userModificationDate', 'creationDate', 'dueDate', 'startDate', 'stopDate']]

#%% Plotting
plotting_data = data_labelled[(data_labelled.status != 'gelöscht')]
TaskScatter = px.scatter(plotting_data,
                         x="creationDate",
                         y="randCol2",
                         color="block_title",
                         size="randCol",
                         hover_name="task_title",
                         hover_data=["project_title", "area_title", "block_title",
                                     "status", "type", "userModificationDate", "creationDate",
                                     "dueDate", "startDate", "stopDate"])

TaskScatter.for_each_trace(
    lambda trace: trace.update(marker_symbol="square") if trace.name == "ARBEIT" else (),
)

# Use date string to set xaxis range
TaskScatter.update_layout(xaxis_range=['2022-01-01', '2022-07-31'], title_text="Manually Set Date Range", uniformtext_minsize=8, uniformtext_mode='hide')

TaskScatter.update_traces(textposition='top right')

# Rangeslider
TaskScatter.update_xaxes(
    rangeslider_visible=True,
    showgrid=False,
    rangeselector=dict(
        buttons=list([
            dict(count=1, label="1m", step="month", stepmode="backward"),
            dict(count=6, label="6m", step="month", stepmode="backward"),
            dict(count=1, label="YTD", step="year", stepmode="todate"),
            dict(count=1, label="1y", step="year", stepmode="backward"),
            dict(step="all")
        ])
    )
)

# Showing the plot
TaskScatter.show()

#%% Dash App
# Create the Dash app
app = dash.Dash()

# Set up the layout with a single graph
app.layout = html.Div(children=[
    html.H1(children="AUFGABENSTEUERUNG"),
    dcc.Dropdown(id='track-dropdown',
                    options=[{'label': i, 'value': i} for i in data_labelled['area'].unique()],
                    multi=True),
    dcc.Graph(id='TaskScatter')])

# Set up callback function
@app.callback(
    Output(component_id='TaskScatter', component_property='figure'),
    Input(component_id='track-dropdown', component_property='value')
)
def update_figure(track):
    filtered_track = data_labelled[data_labelled['area'] == track]
    TaskScatter = px.scatter(data_labelled,
                             title=f'Task Scatter {track}',
                             x="creationDate",
                             y="randCol",
                             color='task_title',
                             size='randCol2',
                             symbol='',
                             text="title",
                             opacity=0.5,
                             hover_data=["block_title", 'project_title'],
                             width=1600,
                             height=800)
    return TaskScatter

# Run local server
if __name__ == '__main__':
    app.run_server(debug=True)


import json
import nltk
from scipy import sparse
from nltk.tokenize import word_tokenize
from pymystem3 import Mystem
from nltk.corpus import stopwords
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer

# nltk.download('stopwords')
sw = stopwords.words('russian')
m = Mystem()

def make_corpus(path):
    with open(path, 'r') as f:
        corpus = list(f)[:50000]
    answers = []
    for doc in corpus:
        doc = json.loads(doc)
        ans = sorted(doc['answers'], key=lambda x: x['author_rating']['value'])
        if len(ans) > 0:
            answers.append(ans[-1]['text'])
    return answers

def preprocess_text(text):
    words = [w.lower() for w in word_tokenize(text) if w.isalpha()]
    words = [w for w in words if w not in sw]
    text = m.lemmatize(' '.join(words))
    return ''.join(text)[:-1]

def index_mtx(docs):
    texts = [preprocess_text(d) for d in docs]
    count_vectorizer = CountVectorizer()
    tf_vectorizer = TfidfVectorizer(use_idf=False, norm='l2')
    tfidf_vectorizer = TfidfVectorizer(use_idf=True, norm='l2')
    x_count_vec = count_vectorizer.fit_transform(texts)
    x_tf_vec = tf_vectorizer.fit_transform(texts)
    x_tfidf_vec = tfidf_vectorizer.fit_transform(texts)
    idf = tfidf_vectorizer.idf_
    tf = x_tf_vec
    k = 2
    b = 0.75
    len_d = x_count_vec.sum(axis=1)
    avdl = len_d.mean()
    B_1 = (k * (1 - b + b * len_d/avdl))
    values = []
    rows = []
    cols = []
    for i, rc in enumerate(zip(*tf.nonzero())):
        val_a = tf.data[i] * idf[rc[1]] * (k+1)
        val_b = tf.data[i] + float(B_1[rc[0]])
        values.append(val_a/val_b)
        rows.append(rc[0])
        cols.append(rc[1])
    mtx = sparse.csr_matrix((values, (rows, cols)))
    return mtx, count_vectorizer

def ind_query(qr, vct):
    return vct.transform([qr])

def calc_distance(ind_qr, ind_mtx):
    return np.dot(ind_mtx, ind_qr.T).toarray()

def main():
    answers = make_corpus('data.jsonl')
    # у меня не хватает памяти для хранения answers в арэе, поэтому каждому элементу соответствует айди (чтобы мотом можно было реализовать сортировку через маску)
    ans_ids = np.arange(0, len(answers)*3, 3)
    ind_mtx, vct = index_mtx(answers)
    qr = input('Введите запрос или нажмите на Enter, если больше не хотите спрашивать: ')
    results = []
    while qr != '':
        qr = preprocess_text(qr)
        ind_q = ind_query(qr, vct)
        sort_ind = calc_distance(ind_q, ind_mtx).argsort(axis=0)[-1::-1]
        res = ans_ids[sort_ind.ravel()]
        results.append(res)
        print('Топ 5:\n', '\n'.join([answers[i//3] for i in res[:5]]))
        qr = input('Введите запрос или нажмите на Enter, если больше не хотите спрашивать: ')
    return results

if __name__ == '__main__':
   results = main()


from itertools import permutations

class Intcode():
	def __init__(self, program, noun=None, verb=None):
		self.program = [int(i) for i in program]

		if noun:
			self.program[1] = noun
		if verb:
			self.program[2] = verb

		self.i = 0
		self._output = None

	def parameter_mode(self, mode, param):
		if mode == '0':
			return self.program[param]
		elif mode == '1':
			return param
		else:
			raise Exception('Invalid mode.')

	def get_params(self, param_mode, n_params):
		params = [None for n in range(n_params)]
		params[0] = self.parameter_mode(param_mode[0], self.program[self.i + 1])
		params[1] = self.parameter_mode(param_mode[1], self.program[self.i + 2])
		if n_params == 3:
			params[2] = self.program[self.i + 3]
		return params

	def run(self, _input):
		while self.i <= len(self.program):
			instruction = self.program[self.i]
			opcode = instruction % 100
			param_mode = str(instruction // 100)
			param_mode = param_mode[::-1] + '0' * (3 - len(param_mode))
			ix_change = False

			if opcode == 1:
				params = self.get_params(param_mode, 3)
				self.program[params[2]] = params[0] + params[1]
			elif opcode == 2:
				params = self.get_params(param_mode, 3)
				self.program[params[2]] = params[0] * params[1]
			elif opcode == 3:
				params = [self.program[self.i + 1]]
				current_input = _input[0]
				_input.pop(0)
				self.program[params[0]] = current_input
			elif opcode == 4:
				params = [self.parameter_mode(param_mode[0], self.program[self.i + 1])]
				self._output = params[0]
				self.i += 1 + len(params) # still need to increment!
				return self
			elif opcode == 5:
				params = self.get_params(param_mode, 2)
				if params[0] != 0:
					self.i = params[1]
					ix_change = True
			elif opcode == 6:
				params = self.get_params(param_mode, 2)
				if params[0] == 0:
					self.i = params[1]
					ix_change = True
			elif opcode == 7:
				params = self.get_params(param_mode, 3)
				self.program[params[2]] = 1 if params[0] < params[1] else 0
			elif opcode == 8:
				params = self.get_params(param_mode, 3)
				self.program[params[2]] = 1 if params[0] == params[1] else 0
			elif opcode == 99:
				self._output = self.program[0]
				return self
			else:
				raise Exception('Unknown opcode.')

			if not ix_change:
				self.i += 1 + len(params)

def amplifier_controller(_input):
	signals = []
	perm = list(permutations([0, 1, 2, 3, 4]))
	for thruster in perm:
		a = Intcode(_input).run([thruster[0], 0])
		b = Intcode(_input).run([thruster[1], a._output])
		c = Intcode(_input).run([thruster[2], b._output])
		d = Intcode(_input).run([thruster[3], c._output])
		e = Intcode(_input).run([thruster[4], d._output])
		signals.append(e._output)

	return signals, perm

def feedback_loop(_input):
	signals = []
	perm = list(permutations([5, 6, 7, 8, 9]))
	for thruster in perm:
		a = Intcode(_input).run([thruster[0], 0])
		b = Intcode(_input).run([thruster[1], a._output])
		c = Intcode(_input).run([thruster[2], b._output])
		d = Intcode(_input).run([thruster[3], c._output])
		e = Intcode(_input).run([thruster[4], d._output])

		while a.program[a.i] != 99:
			a = a.run([e._output])
			b = b.run([a._output])
			c = c.run([b._output])
			d = d.run([c._output])
			e = e.run([d._output])
			e_val = e._output

		signals.append(e_val)

	return signals, perm

def run(_input, part):
	if part == 1:
		signals, perm = amplifier_controller(_input)
	elif part == 2:
		signals, perm = feedback_loop(_input)
	else:
		raise Exception('Only 2 parts to this problem.')

	best_signal = max(signals)  # 4215746
	optimal_thruster = perm[signals.index(best_signal)]
	print(f'PART {part}: highest signal: {best_signal}, best thruster combination: {"".join([str(s) for s in optimal_thruster])}')

if __name__ == '__main__':
	_input = open("2019/aoc7.txt").read().split(',')
	run(_input, part=1)  # 844468
	run(_input, part=2)  # 4215746


#!/usr/bin/env python

def main() -> None:
    """Entry point for the CLI"""
    from .config import config

    config.update_config("cli")
    from .node_deployer import app

    app()

def debug() -> None:
    """Entry point for the debug CLI"""
    from .config import config

    config.update_config("debug")
    from .node_deployer import app

    # Below, we set the default value of the debug flag
    # for the base function of each command to True
    def unwrap(f):  # Not a closure, just here to avoid polluting the namespace
        if hasattr(f, "__wrapped__"):
            return unwrap(f.__wrapped__)
        else:
            return f

    for c in app.registered_commands:
        f = unwrap(c.callback)
        defaults = list(f.__defaults__)
        defaults[-1] = True
        f.__defaults__ = tuple(defaults)

    app()

if __name__ == "__main__":
    main()


from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import permissions

schema_view = get_schema_view(
    openapi.Info(
        title="ECO TAXI",
        default_version='v1',
        description="API for Evion",
        terms_of_service="https://evion.kg",
        contact=openapi.Contact(email="evionteam1@gmail.com"),
        license=openapi.License(name="Evion"),
    ),
    public=True,
    permission_classes=[permissions.AllowAny],
)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', schema_view.with_ui('swagger',
         cache_timeout=0), name='schema-swagger-ui'),
    path('user/', include('djoser.urls')),
    path('login/', include('djoser.urls.jwt')),
    path('', include('apps.users.urls')),
    path('drivers/', include('apps.drivers.urls'))
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL,
                          document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)


import pymongo

if __name__ == "__main__":
    client = pymongo.MongoClient("mongodb://localhost:27017/")

    all_db_name = client.list_database_names()
    print(all_db_name)

    colletn = client["ayush"]
    print(colletn.list_collection_names())


from datetime import timedelta

from django.db import models
from django.utils import timezone

class TOTPRequestQuerySet(models.QuerySet):
    def is_valid(self, receiver, request, code):
        current_time = timezone.now()
        result = self.filter(
            receiver=receiver,
            request_id=request,
            code=code,
            created__lt=current_time,
            created__gt=current_time - timedelta(seconds=300),
        ).exists()
        return result


from django.shortcuts import render
import joblib as jb
import numpy as np
from pathlib import Path

# Create your views here.
def home(request):
    return render (request,'index.html')

def prediction(request):
    if request.method=='POST':
        TSH=request.POST['TSH']
        FTI=request.POST['FTI']
        TT4=request.POST['TT4']
        T3=request.POST['T3']
        query_hypothyroid=request.POST['query_hypothyroid']
        on_thyroxine=request.POST['on_thyroxine']
        sex=request.POST['sex']
        pregnant=request.POST['pregnant']
        psych=request.POST['psych']

        arr=np.array([[TSH,FTI,TT4,T3,query_hypothyroid,on_thyroxine,sex,pregnant,psych]])

        model=jb.load('thyroid/model/model_RandomForest.pkl')
        result= model.predict(arr)
        if result ==1:
            return render(request,'after.html',{'data':'your value are abnormal and are have thyroid'})
        else :
            return render(request,'after.html',{'data':'your value are normal '})


import aio_pika
import backoff
from aio_pika import exceptions

class RabbitConsumer:
    def __init__(self, dsn):
        self.dsn = dsn
        self.connection = None
        self.channel = None

    @backoff.on_exception(backoff.expo, exceptions.CONNECTION_EXCEPTIONS, max_tries=5, max_time=15)
    async def get_connection(self):
        if self.connection is None:
            self.connection = await aio_pika.connect_robust(
                self.dsn,
                timeout=10.0,
            )
        return self.connection

    @backoff.on_exception(backoff.expo, exceptions.CONNECTION_EXCEPTIONS, max_tries=5, max_time=15)
    async def get_channel(self):
        if self.connection is None:
            self.get_connection()
        if self.channel is None:
            self.channel = await self.connection.channel()
        return self.channel


import re

import discord
import asyncio
import random
from datetime import datetime, timedelta
from pytz import timezone
import pytz
import help_commands

from discord.ext import commands

class misc_commands(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.sent_message = False

    @commands.is_owner()
    @commands.command(aliases=["del"])
    async def delete_messages(self, ctx, amount=1):
        async for message in ctx.channel.history(limit = int(amount)+1):
            await message.delete()

    @commands.command()
    async def repeat(self, ctx, *, words):
        await ctx.send(words)

    @commands.Cog.listener()
    async def on_message(self, message):
        if message.author == self.bot.user:
            return
        try:
            prefix = help_commands.get_prefix(message)
            if "prefix" in message.content and not message.content.startswith(
                    f"{prefix}prefix"):
                await message.channel.send(f"My prefix is \"{prefix}\"")
        except Exception as e:
            print("from on_message prefix func: error occured", e)

def setup(bot):
    bot.add_cog(misc_commands(bot))


from keras.datasets import mnist
from keras.optimizers import SGD
from keras.utils import to_categorical

import create_model as cm
import read_data as rd

## Creating the model

data_shape = (28, 28, 1)

model = cm.create_model(data_shape)

model.summary()

## Compiling the model

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(
        loss='categorical_crossentropy',
        optimizer=sgd,
        metrics=['accuracy']
        )

## Training

try:
        (x_train, y_train), (x_test, y_test) = rd.read_data("/data")
        using_floydhub = True
except IOError:
        print("Couldn't open the dataset in /data, loading keras dataset")
        (x_train, y_train), (x_test, y_test) = mnist.load_data()
        using_floydhub = False

y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

model.fit(x_train, y_train,
        epochs=20,
        batch_size=128)

## Evaluation
score = model.evaluate(x_test, y_test, batch_size=128)
print("\n", "Model accuracy: ", score[1], "\n")

## save model
if using_floydhub:
        output_dir = '/output/'
else:
        output_dir = './output/'
        if not os.path.exists(output_dir):
                os.makedirs(output_dir)

model.save(output_dir+'nikulaj_model.hdf5')


import shutil

from jinja2 import Environment, FileSystemLoader

from fixtures import stream_list, topic_list, chat_list
from config import BASE_URL, INSTANCE_TITLE

environment = Environment(loader=FileSystemLoader("templates"), autoescape=True)
stream_list_template = environment.get_template("stream_list.html.jinja2")
topic_list_template = environment.get_template("topic_list.html.jinja2")
topic_chat_template = environment.get_template("topic_chat.html.jinja2")

with open("./rendered/index.html", mode="w+", encoding="utf-8") as f:
    f.write(
        stream_list_template.render(
            base_url=BASE_URL,
            page_title=INSTANCE_TITLE,
            title=INSTANCE_TITLE,
            last_updated_str="Archived on August 17th 2022 at 11.00 AM UTC",
            stream_list=stream_list,
        )
    )
with open("./rendered/stream.html", mode="w+", encoding="utf-8") as f:
    f.write(
        topic_list_template.render(
            base_url=BASE_URL,
            page_title=f"# lorem ipsum | {INSTANCE_TITLE}",
            title=INSTANCE_TITLE,
            last_updated_str="Archived on August 17th 2022 at 11.00 AM UTC",
            topic_list=topic_list,
            stream_name="lorem ipsum",
            stream_url=f"{BASE_URL}/stream.html",
            rendered_description="""
For discussions of the API, integrations, and bots and in Zulip.
""",
        )
    )
with open("./rendered/topic.html", mode="w+", encoding="utf-8") as f:
    f.write(
        topic_chat_template.render(
            base_url=BASE_URL,
            page_title=f"Le fishe swimming in a bathtub - # lorem ipsum | {INSTANCE_TITLE}",
            title=INSTANCE_TITLE,
            last_updated_str="Archived on August 17th 2022 at 11.00 AM UTC",
            chat_list=chat_list,
            stream_name="lorem ipsum",
            stream_url=f"{BASE_URL}/stream.html",
            topic_name="Le fishe swimming in a bathtub",
            num_messages=3,
        )
    )

# Copy needed assets to /rendered
shutil.copytree("templates/assets", "rendered/assets", dirs_exist_ok=True)


# Find Minimum in Rotated Sorted Array
from typing import List

class Solution:
    def findMin(self, nums: List[int]) -> int:
        if len(nums) == 1: return nums[0]
        l, r = 0, len(nums) - 1
        if nums[l] < nums[r]: return nums[l]
        while l < r - 1:
            mid = (l + r) // 2
            if nums[mid] > nums[l]:
                l = mid
            else:
                r = mid
        return min(nums[l], nums[r])

s = Solution()
print(s.findMin([4,5,6,7,0,1,2]))