import tkinter as tk
from tkinter import ttk
from frontend.ErgebnisFrame import ErgebnisFrame
from frontend.custom_widgets.CanvasLogo import LogoCanvas

UEBERSCHRIFT_DESIGN = {
    'bg': '#E2E2E2',
    'font': ('Arial', 24, 'bold')
}

class MeineSuchenFrame(tk.Frame):

    def __init__(self, parent, controller, *args, **kwargs):
        super().__init__(parent)
        self.parent = parent
        self.controller = controller

        self.meine_suchen_df = self.controller.backend.Suche.load_data_from_user_as_df(
            self.controller.active_user).sort_values('zeitpunkt', ascending=False)

        self.view_df = self.meine_suchen_df[['zeitpunkt', 'eingaben_brand', 'eingaben_model', 'eingaben_vehicletype',
                                             'eingaben_erstzulassung', 'eingaben_kilometerstand', 'eingaben_leistung_ps', 'prognose_preis']].copy()

        self.view_df.columns = ['Zeitpunkt', 'Marke', 'Modell', 'Typ',
                                'Erstzulassung', 'Kilometer', 'Leistung [PS]', 'Prognose [€]']
        self.view_df['Marke'] = self.view_df['Marke'].map(lambda val: val.upper())
        self.view_df['Modell'] = self.view_df['Modell'].map(lambda val: val.upper())
        self.view_df['Typ'] = self.view_df['Typ'].map(lambda val: val.upper())
        self.view_df['Erstzulassung'] = self.view_df['Erstzulassung'].map(lambda val: int(val))
        self.view_df['Kilometer'] = self.view_df['Kilometer'].map(lambda val: int(val))
        self.view_df['Leistung [PS]'] = self.view_df['Leistung [PS]'].map(lambda val: int(val))
        self.view_df['Prognose [€]'] = self.view_df['Prognose [€]'].map(lambda val: int(val))

        self.layout()

    def layout(self):
        self.config(bg='#E2E2E2')

    ########################################
    ########################################
        header_frame = tk.Frame(self, bg='#E2E2E2', height=200)
        header_frame.pack(side=tk.TOP, fill=tk.X)
        header_frame.pack_propagate(False)
        # Logo und Überschrift oben links
        logo = LogoCanvas(header_frame, self.controller, size=(50, 50), image_name='lupe_logo', bg='#E2E2E2')
        logo.pack(side=tk.LEFT, anchor=tk.NW, padx=20, pady=20)
        self.logo = logo
        tk.Label(header_frame, text='Deine Suchen', **
                 UEBERSCHRIFT_DESIGN).pack(side=tk.LEFT, padx=(0, 20), pady=20, anchor=tk.NW)

        ########################################
        ########################################
        tree_view_frame = tk.Frame(self, bg='#E2E2E2')
        tree_view_frame.pack(side=tk.TOP, fill=tk.X, expand=False, padx=70)
        style = ttk.Style()
        # style.theme_use("clam")
        style.configure("Treeview.Heading", background="gray", foreground="black", font=('Arial', 12, 'bold'))

        style.configure("Treeview",
                        background="black",
                        foreground="black",
                        rowheight=25,
                        fieldbackground="black",
                        )
        style.map('Treeview', background=[('selected', '#ABDB79')])

        treeview = ttk.Treeview(tree_view_frame, selectmode='browse')
        treeview.tag_configure("evenrow", background='#DEE1E4', foreground='white', font=('Arial', 9))
        treeview.tag_configure("oddrow", background='white', foreground='white', font=('Arial', 9))

        self.treeview = treeview

        treeview['column'] = list(self.view_df.columns)
        treeview["show"] = "headings"

        widhts = [100] + [100]*(len(list(self.view_df.columns))-1)
        i = 0
        for column in treeview["columns"]:
            treeview.column(column, anchor=tk.CENTER, width=widhts[i])
            treeview.heading(column, text=column)
            i += 1

        # command means update the yaxis view of the widget
        treescrolly = tk.Scrollbar(tree_view_frame, orient="vertical", command=treeview.yview)
        # command means update the xaxis view of the widget
        treescrollx = tk.Scrollbar(tree_view_frame, orient="horizontal", command=treeview.xview)
        # assign the scrollbars to the Treeview Widget
        treeview.configure(xscrollcommand=treescrollx.set, yscrollcommand=treescrolly.set)
        # make the scrollbar fill the x axis of the Treeview widget
        treescrollx.pack(side="bottom", fill="x")
        # make the scrollbar fill the y axis of the Treeview widget
        treescrolly.pack(side="right", fill="y")

        treeview.pack(fill=tk.BOTH, expand=True, padx=0, pady=0)

        # Liste in treeview ablegen
        self.treeview.delete(*self.treeview.get_children())
        df_rows = self.view_df.to_numpy().tolist()
        i = 1
        for row in df_rows:
            if i % 2 == 0:
                self.treeview.insert("", "end", values=row, tags=('evenrow',))
            else:
                self.treeview.insert("", "end", values=row, tags=('oddrow',))
            i += 1

        self.treeview.bind('<Double-1>', self.command_ergebnis)

        ########################################
        ########################################
        bottom_frame = tk.Frame(self, bg='#E2E2E2', height=100)
        bottom_frame.pack(side=tk.BOTTOM, fill=tk.X)
        btn = tk.Button(bottom_frame, text='Suche öffnen', command=self.command_ergebnis,
                        bg='#92D050', font=('Arial', 22, 'bold'), cursor='hand2')
        btn.pack(side=tk.RIGHT, padx=80, pady=50)
        btn.bind('<Enter>', lambda *args: btn.config(bg='#C7E7A6', fg='#394240'))
        btn.bind('<Leave>', lambda *args: btn.config(bg='#92D050', fg='black'))

    def command_ergebnis(self, *args):
        curItem = self.treeview.focus()

        if len(self.treeview.item(curItem)['values']) == 0:
            tk.messagebox.showinfo('Achtung', 'Bitte wählen sie eine Datenreihe aus.')
            return
        value = self.treeview.item(curItem)['values']
        zeitpunkt = value[0]
        print(zeitpunkt)
        subdf = self.meine_suchen_df[self.meine_suchen_df['zeitpunkt'] == zeitpunkt].iloc[0]
        print(subdf.to_dict())

        dic = subdf.to_dict()

        for widgets in self.winfo_children():
            widgets.forget()
        ErgebnisFrame(self, self.controller, regression_daten_dic={
            'brand': dic['eingaben_brand'],
            'model': dic['eingaben_model'],
            'vehicletype': dic['eingaben_vehicletype'],
            'auto_alter': 2016 - int(dic['eingaben_erstzulassung']),
            'auto_leistung_ps': int(dic['eingaben_leistung_ps']),
            'auto_kilometerstand': int(dic['eingaben_kilometerstand']),
            'getriebe': dic['eingaben_getriebe'],
            'antriebsart': dic['eingaben_fueltype'],
            'schaden_vorhanden': dic['eingaben_schaden_vorhanden'],
            'erstzulassung': int(dic['eingaben_erstzulassung'])
        }, save=False).pack(fill=tk.BOTH, expand=True)


from __future__ import unicode_literals, print_function, division
from io import open
import unicodedata
import re
import torch
import nltk
import json

from nltk.tokenize import stanford
import csv
from nltk.stem import WordNetLemmatizer
from copy import deepcopy
from collections import Counter
import parameters as p
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

class_dict = {
        "typical": 0,
        "good": 1,
        "great": 2
    }
documents = {"great" : [],
             "good" : [],
             "typical" : []}
word_count = Counter()

base = '/home/paulwu/research/processed_nyt/'
lemmatizer = WordNetLemmatizer()
sentence_tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
work_tokenizer = stanford.StanfordTokenizer(path_to_jar="/home/paulwu/nltk_data/stanford-postagger-2018-02-27/stanford-postagger-3.9.1.jar")

class Lang:
    def __init__(self, name):
        self.name = name
        self.word2index = {}
        self.index2word = {1: "UNK"}
        self.n_words = 1  # Count UNK

    def addSentence(self, sentence):
        for word in sentence.split(' '):
            if(word_count[word] > 5):
                self.addWord(word)

    def addWord(self, word):
        if word not in self.word2index:
            self.n_words += 1
            self.word2index[word] = self.n_words
            self.index2word[self.n_words] = word

eng = Lang("eng")

def unicodeToAscii(s):
    return ''.join(
        c for c in unicodedata.normalize('NFD', s)
        if unicodedata.category(c) != 'Mn'
    )

# Lowercase, trim, and remove non-letter characters

def normalizeString(s):
    s = unicodeToAscii(s.lower().strip())
    s = re.sub(r"([.!?])", r" ", s)
    s = re.sub(r"[^a-zA-Z.!?]+", r" ", s)
    return s

def indexes_from_sentence(sentence):
    indexes = []
    for word in sentence.split(' '):
        try:
            indexes.append(eng.word2index[word])
        except KeyError:
            indexes.append(1)
    return indexes

def preprocess_and_construct_vocab(class_label):
    with open(class_label + '.csv', "r") as f:
        reader = csv.reader(f)
        count = 0
        for row in reader:
            # count += 1
            # if count == p.num_docs_per_class:break
            full_text = row[0]
            full_text = full_text.replace("won't ", "will not ").replace("can't ", "can not ")
            full_text = ' '.join(work_tokenizer.tokenize(full_text))
            sentences = sentence_tokenizer.tokenize(full_text)

            preprocessed = []
            for sentence in sentences:
                # print(sentence)
                sentence = sentence.replace("'re ", "are ").replace("'ll ", "will ").replace("n't ", "not ").replace("'m ","am ").replace("'ve", "have")
                sentence = [word for word in normalizeString(sentence).split() if len(word) >= 2]
                sentence = [lemmatizer.lemmatize(word) for word in sentence.split(' ') if len(word) > 1]
                for word in sentence:
                    word_count[word] += 1
                # print(sentence)
                sentence = ' '.join(sentence)
                # eng.addSentence(sentence)
                preprocessed.append(sentence)
            # print(preprocessed)
            documents[class_label].append(deepcopy(preprocessed))

def data_to_tensor():
    # for label in ["great", "good", "typical"]:
    doc_list = []
    label_list = []
    for label in ["great", "good", "typical"]:
        lable_count = 0
        docs = documents[label]
        cur_label = class_dict[label]

        for doc in docs:
            # print(doc)
            # print(len(doc))
            lable_count += 1
            sentence_list = []
            for sentence in doc:
                indexes = indexes_from_sentence(sentence)
                sentence_list.append(deepcopy(indexes))
                # print(indexes)
            # a list of sentence indices
            doc_list.append(deepcopy(sentence_list))
            # print(doc_list)

        label_list += [cur_label] * lable_count
        # print(label_list)
        # a list of doc indices
    # doc_array = np.array(doc_list, dtype=object)

    result_dict = {"tokens": doc_list, "label": label_list}
    return json.dumps(result_dict)

def main():
    preprocess_and_construct_vocab("great")
    preprocess_and_construct_vocab("good")
    preprocess_and_construct_vocab("typical")
    # total = reduce((lambda x, y: x + y), (reduce((lambda x, y: x + y), documents.values())))
    [eng.addSentence(sent) for docs in documents.values() for doc in docs for sent in doc]

    # print(documents.values())
    # print(word_count)
    # print([(k,v) for (k,v) in word_count.items() if v > 5])
    # print(eng.word2index)
    # final_json = data_to_tensor()
    # print(final_json)
    # with open(p.out_filename + ".json", "w")as f:
    #     f.write(final_json)

main()


# Read file

import pandas as pd

day3 = pd.read_fwf('E:/Projects/advent2021/data/input_3.txt', header=None, widths=[1]*12)

# Part 1

gamma = day3.mode(axis=0)
epsilon = 1-gamma

def series_to_int(s):

    return int(s.iloc[0].astype(str).str.cat(), base=2)

print(series_to_int(gamma) * series_to_int(epsilon))

# Part 2
oxygen = co2 = 1 - day3.copy()

for i in range(0, 12):
    oxygen = oxygen[oxygen[i] == oxygen.mode(axis=0).values[0, i]]
    if len(oxygen.index) == 1:
        break

for i in range(0, 12):
    co2 = co2[co2[i] == (1 - co2.mode(axis=0).values[0, i])]
    if len(co2.index) == 1:
        break

print(series_to_int(1 - co2) * series_to_int(1 - oxygen))


# https://leetcode.com/problems/asteroid-collision/submissions/

class Solution:
    def asteroidCollision(self, asteroids: List[int]) -> List[int]:
        output = [] # Output is a stack

        for i, val in enumerate(asteroids):

            # If going left => crush all the left untill you cant move
            if val < 0:
                while output and output[-1] > 0 and output[-1] < abs(val):
                    output.pop()

                if not output or output[-1] < 0:
                    output.append(val)
                elif output[-1] == abs(val):
                    output.pop()

            # If going right always add
            else: output.append(val)

        return output


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

# # The Median of Price(THB)/Area(sqm.) in Types of Buildings from 30000 data

# # import library

# In[1]:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import requests
import matplotlib.pyplot as plt2

# # import data

# In[2]:

url='https://drive.google.com/file/d/1TNDpbbBAUShvpqzTzXqWdZ7qljn1119i/view?usp=sharing'
url='https://drive.google.com/uc?id=' + url.split('/')[-2]
estimate = pd.read_csv(url)
data=estimate.loc[:, ['propertytype_name_en','area_usable_min','price_min']]
data=data.sort_values('propertytype_name_en',ascending=True).dropna()

# # resetting index

# In[3]:

reset=data.reset_index()

# # list head data

# In[4]:

_type=data.loc[:,['propertytype_name_en']]
_type=_type.values.tolist()
new_type = []
for New_type in _type:
    if New_type not in new_type:
        new_type.append(New_type)

# # selecting data

# In[5]:

def median_founder(data001):
    data001['area/thai bath']=data001['price_min']//data001['area_usable_min']
    data001['median'] = data001['area/thai bath'].median(axis=0)
    data001=data001.reset_index()
    data001.drop('index',axis=1)

# # reset index name

# In[6]:

reset=reset.drop_duplicates(subset=['propertytype_name_en'])

# # drop column

# In[7]:

reset=reset.reset_index().drop('level_0',axis= True).drop('index',axis= True)

# # appending dataframe

# In[8]:

i=0
data001 = data.query('propertytype_name_en == "'+str(reset['propertytype_name_en'][i])+'"')
median_founder(data001)
data001=data001.reset_index()
data001.drop('index',axis=1)
data001 = pd.DataFrame({'Building_type':data001['propertytype_name_en'][0],'Median':data001['median'][0]},index=[int(i)])
data001=pd.DataFrame(data001)

# In[9]:

n=1
for n in range(1,len(new_type)):
    data002 = data.query('propertytype_name_en == "'+str(reset['propertytype_name_en'][n])+'"')
    median_founder(data002)
    data002=data002.reset_index()
    data002.drop('index',axis=1)
    data002 = pd.DataFrame({'Building_type':data002['propertytype_name_en'][0],'Median':data002['median'][0]},index=[int(n)])
    data001=pd.concat([data001,data002])

# # sort value data

# In[10]:

data001 = data001.sort_values('Median',ascending=True)
data001 = data001.reset_index().drop('index',axis=1)

# # ploting graph

# In[11]:

data001.plot(x='Building_type',y='Median',figsize=(20,5),color='gold',linewidth=10,marker='o',ms=15,mec='dimgrey',mfc='black')
plt.xticks(data001.index,data001['Building_type'])
plt.title('The Median of Price(THB)/Area(sqm.) in Types of Buildings',fontsize=16)
plt.xlabel('Type of Buildings',fontsize=12)
plt.ylabel('The Median of Price(THB)/Area(sqm.)',fontsize=12)
plt.show()

# In[12]:

data001.plot.bar(x='Building_type',y='Median',color ='maroon',figsize=(20,5))
plt.xticks(data001.index,data001['Building_type'])
plt.title('The Median of Price(THB)/Area(sqm.) in Types of Buildings',fontsize=16)
plt.xlabel('Type of Buildings',fontsize=12)
plt.ylabel('The Median of Price(THB)/Area(sqm.)',fontsize=12)
plt.show()

# In[15]:

median_seaborn=sns.barplot(x='Building_type',y='Median',data=data001,palette="rocket")
median_seaborn.set_xticklabels(median_seaborn.get_xticklabels(), rotation=90)
plt.show

# In[14]:

data001


from decorator import timer

@timer
def fact(n):
    if n == 0:
        return 1
    else:
        return n * fact(n-1)

@timer
def fact_loop(n):
    fact = 1
    x = 1
    while x <= n:
        fact = fact * x
        x = x + 1
    return fact

print(fact(10))
print(fact_loop(10))


# Import stuff
# and libraries

from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import MultiLabelBinarizer
import pandas as pd

# Data Generation
data_list = {'id':[1,2,3,4,5,6,7,8,9,10],
             'name':['Jon','Ada','Steve','Dan','Joe','Marry','Bill','Jim','Milan','Martin'],
             'surname':['Queen','Jobs','Weehaa','Ng','Gates','Buffet','Musk','Ng','Gates','Musk'],
             'age':[10,12,50,47,61,21,45,14,65,15],
             'state':['USA','USA','USA','GB','CZ','GE','USA','CA','USA','USA']
             }

df = pd.DataFrame(data_list)
o = df.dtypes.values[0]

# Encode data so that we can feed it into the NN
label_encoder_logger = {}
for col in ['name','surname','state']:
    label_encoder_logger[col] = MultiLabelBinarizer()
    df['{}_enc'.format(col)] = label_encoder_logger[col].fit_transform(df[col])

output_columns = ['name_enc', 'surname_enc', 'state_enc', 'age']
output_columns = ['name','surname','state','age']
input_column = 'id'

mlb = MultiLabelBinarizer()
target = mlb.fit_transform(df[output_columns])

# Fit the neural net
clf = MLPClassifier()
clf.fit(X=df[[input_column]],y=target)


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np

from scipy.optimize import linear_sum_assignment

from lib.core.inference import get_final_preds

def calc_dists(preds, target):
    preds = preds.astype(np.float32)[:, :2]
    target = target.astype(np.float32)

    dists = np.sqrt(((preds.reshape(preds.shape[0], 1, preds.shape[1]) - \
        target.reshape(1, target.shape[0], target.shape[1])) ** 2) \
        .sum(axis=-1))

    return dists

def calc_tp_fp_fn(preds, target, hit_thr=2):

    dists = calc_dists(preds, target)

    row_ind, col_ind = linear_sum_assignment(dists)

    tp = dists[row_ind, col_ind] <= hit_thr
    fp = np.logical_not(tp)
    fn = dists.shape[1] - np.sum(tp)

    return tp, fp, fn


print("Welcome to Math Calculator 2.0 ")

num_1 = int(input("Enter your first Number: "))
num_2 = int(input("Enter your second Number: "))

def remainder(a, b):
    if b == 0:
        return print("Undefined")
    return a % b

def exponent(a, b):
    return a ** b

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

def divide(a, b):
    if b == 0:
        return print("Undefined")
    return a / b

def multiply(a, b):
    return a * b

print("First NUM: ", num_1)
print("Second NUM: ", num_2)
print("Subtract             (s)")
print("Add                  (a)")
print("Divide               (d)")
print("Exponent             (e)")
print("Multiply             (m)")
print("Remainder            (r)")
menu_drop = str(input("Now what would you like to do?"))

print("Answer: ")

if menu_drop == "s":
    subtract(num_1, num_2)

elif menu_drop == "a":
    add(num_1, num_2)

elif menu_drop == "d":
    divide(num_1, num_2)

elif menu_drop == "e":
    exponent(num_1, num_2)

elif menu_drop == "m":
    multiply(num_1, num_2)

elif menu_drop == "r":
    remainder(num_1, num_2)

else:
    print("Error 475: Error within INPUT")


from typing import Any, Dict
import pandas as pd
from openbb_terminal.api import openbb

from src.base import pred_type, probabalistic, get_items

def load_data(ticker: str) -> pd.DataFrame:
    df = openbb.stocks.load("AAPL")
    df.columns = [x.lower() for x in df.columns]
    df = df.reset_index()
    return df

def run_prediction(df: pd.DataFrame, num_days: int, info: Dict[str, Any]) -> pred_type:
    the_model, _, temp_dict = get_items(info)
    (ticker_series, historical_fcast, predicted_values, precision, _model,) = getattr(
        openbb.forecast, the_model
    )(df, **temp_dict)
    if the_model in probabalistic:
        preds_df = predicted_values.quantile_df().head(num_days)
    else:
        preds_df = predicted_values.head(num_days)
    vals = [x[0] for x in preds_df.values]
    return vals, precision


from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
from matplotlib.artist import Artist
import pandas as pd
from tinytag import TinyTag
from music_db_ import *

class Music_Add(QObject):
    def __init__(self):
        QObject.__init__(self)
        self.musicpath_ = ''
        self.picturepath_ = ''
        self.genre_ = ''
        self.title_ = ''
        self.album_ = ''
        self.artistname_ = ''
        self.year_ = ''

    musicpath = pyqtSignal(str, arguments=['text_'])
    picturepath = pyqtSignal(str, arguments=['text_'])
    genre = pyqtSignal(str, arguments=['text_'])
    title = pyqtSignal(str, arguments=['text_'])
    album = pyqtSignal(str, arguments=['text_'])
    artistname = pyqtSignal(str, arguments=['text_'])
    year = pyqtSignal(str, arguments=['text_'])

    picture = pyqtSignal(str, arguments=['text_'])

    @pyqtSlot(str)
    def get_file(self,x):

        self.musicpath_ = x.split('///')[1]
        tag = TinyTag.get(self.musicpath_)
        # print('file path: ',self.musicpath_)

        self.musicpath.emit(self.musicpath_)

        self.genre_ = tag.genre
        self.title_ = tag.title
        self.album_ = tag.album
        self.artistname_ = tag.artist
        self.duration_ = tag.duration
        self.year_ = tag.year

        self.genre.emit(self.genre_)
        self.title.emit(self.title_)
        self.album.emit(self.album_)
        self.artistname.emit(self.artistname_)
        # print('music duration: ', self.duration_)

    @pyqtSlot(str)
    def get_picture(self,x):
        self.picturepath_ = x
        # print('file path: ',self.picturepath_)
        self.picturepath.emit(self.picturepath_)
        self.picture.emit(self.picturepath_)

    @pyqtSlot(str)
    def upd_info(self, x):
        """
        """
        # print('x: ', x)
        if '|music_path|#' in x:
            self.musicpath_ = x.split('|#')[1]
            # print(self.musicpath_)
        elif '|picture_path|#' in x:
            self.picturepath_ = x.split('|#')[1]
            # print(self.picturepath_)
        elif '|artist_name|#' in x:
            self.artistname_ = x.split('|#')[1]
            # print(self.artistname_)
        elif '|genre|#' in x:
            self.genre_ = x.split('|#')[1]
            # print(self.genre_)
        elif '|year|#' in x:
            self.year_ = x.split('|#')[1]
            # print(self.year_)
        elif '|title|#' in x:
            self.title_ = x.split('|#')[1]
            # print(self.title_)
        elif '|Album|#' in x:
            self.album_ = x.split('|#')[1]
            # print(self.album_)

    @pyqtSlot()
    def save(self):
        """
        """
        add_new_song(Song,self.musicpath_,self.picturepath_,self.genre_,self.title_,self.album_,self.artistname_,self.duration_,self.year_)
        # print('save')

    @pyqtSlot()
    def clear(self):
        """
        """
        # print('clear')
        self.musicpath_ = ''
        self.picturepath_ = ''
        self.genre_ = ''
        self.title_ = ''
        self.album_ = ''
        self.artistname_ = ''
        self.year_ = ''
        self.genre.emit(self.genre_)
        self.title.emit(self.title_)
        self.album.emit(self.album_)
        self.artistname.emit(self.artistname_)
        self.picturepath.emit(self.picturepath_)
        self.musicpath.emit(self.musicpath_)
        self.year.emit(self.year_)
        self.picture.emit("test.jpg")

if  __name__ == "__main__":
    import sys

    # создаём экземпляр приложения
    app = QGuiApplication(sys.argv)
    # создаём QML движок
    engine = QQmlApplicationEngine()
    # создаём объект
    music_add = Music_Add()
    # и регистрируем его в контексте QML
    engine.rootContext().setContextProperty("music_add", music_add)
    # загружаем файл qml в движок
    engine.load("test_2/add_file.qml")

    engine.quit.connect(app.quit)
    sys.exit(app.exec_())


# import cv2
# import numpy as np
# import matplotlib.pyplot as plt
#
#
# def nothing(x):
#     pass
#
#
# cv2.namedWindow('image')
#
# img = cv2.imread('D:\\experiment\\pic\\q\\tran41004_2.jpg')
# cv2.namedWindow('image')
# cv2.createTrackbar('Er/Di', 'image', 0, 1, nothing)
# # 创建腐蚀或膨胀选择滚动条，只有两个值
# cv2.createTrackbar('size', 'image', 0, 21, nothing)
# # 创建卷积核大小滚动条
#
#
# while (1):
#     s = cv2.getTrackbarPos('Er/Di', 'image')
#     si = cv2.getTrackbarPos('size', 'image')
#     # 分别接收两个滚动条的数据
#     k = cv2.waitKey(1)
#
#     kernel = np.ones((si, si), np.uint8)
#     # 根据滚动条数据确定卷积核大小
#     erroding = cv2.erode(img, kernel)
#     dilation = cv2.dilate(img, kernel)
#     if k == 27:
#         break
#     # esc键退出
#     if s == 0:
#         cv2.imshow('image', erroding)
#     else:
#         cv2.imshow('image', dilation)
#         # 判断是腐蚀还是膨胀

import cv2
import numpy as np
import matplotlib.pyplot as plt

def nothing(x):
    pass

# src = "blur1041004ththth5noisenum1"
# src = "blur541004ththth10noisenum1"
src = "blur541004ththth10noisenum1"
# src = "41004ththth5noisenum1"
# src = "blur541004"
# img = cv2.imread("D:\\experiment\\pic\\q\\"+src+".jpg")

# src = "blur1041004ththth5noisenum1"
img = cv2.imread("D:\\out\\tranrgb\\"+src+".jpg")
raw2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print(type(raw2))
print(type(raw2[0,0]))
# raw2 = np.loadtxt("D:\\out\\try\\guodu1____L0.csv", dtype=np.int, delimiter=",", encoding='utf-8')
raw2 = np.loadtxt("D:\\out\\try\\guodu1____L0.csv",dtype=np.uint8, delimiter=",", encoding='utf-8')
print(type(raw2))
print(type(raw2[0,0]))

img = raw2

si = 2

kernel = np.ones((si, si), np.uint8)
# 根据滚动条数据确定卷积核大小
erroding = cv2.erode(img, kernel)
dilation = cv2.dilate(img, kernel)
# 膨胀之后腐蚀

si = 0
kernel = np.ones((si, si), np.uint8)
dilationerroding = cv2.erode(dilation, kernel)

si = 0
kernel = np.ones((si, si), np.uint8)
errodingdilation = cv2.dilate(erroding, kernel)

cv2.imwrite("D:\\out\\errod\\"+src+".jpg", erroding)
cv2.imwrite("D:\\out\\dilaterrod\\"+src+".jpg", dilationerroding)
cv2.imwrite("D:\\out\\erroddiate\\"+src+".jpg", errodingdilation)
cv2.imwrite("D:\\out\\dilat\\"+src+".jpg", dilation)
np.savetxt("D:\\out\\errod\\"+src+".csv", erroding,fmt="%d", delimiter=',')
np.savetxt("D:\\out\\dilaterrod\\"+src+".csv", dilationerroding,fmt="%d", delimiter=',')
np.savetxt("D:\\out\\erroddiate\\"+src+".csv", errodingdilation,fmt="%d", delimiter=',')
np.savetxt("D:\\out\\dilat\\"+src+".csv", dilation,fmt="%d", delimiter=',')


class CSVFile():
    def __init__(self, name):
        if type(name) is not str:
            raise Exception("Percorso file (name) non di tipo stringa")
        else:
            self.name = name

    def get_data(self, start=None, end=None):

        if start == None:
            start = 0;
        if start < 0:
            start = 0

        lista_righe = []

        try:
            if end is not None:
                my_file = open(self.name, 'r').readlines()[start:end+1] #end+1 perchè ignoro la riga header
            else:
                my_file = open(self.name, 'r').readlines()[start:]

            for item in my_file:
                riga = item.split(',')

                if(riga[0] != 'Date'):
                    riga[1] = riga[1].strip()
                    lista_righe.append(riga)
            #my_file.close()
        except OSError:
            print('Impossibile aprire il file "{}"'.format(self.name))

        return lista_righe

file_shampoo = CSVFile('shampoo.txt')
# print(file_shampoo.get_data(-3,3))
# print(file_shampoo.get_data())
# print(file_shampoo.get_data(30))


#This program create a person group (in which you can add multiple new people
#should only run it when you want to create a whole new person group
#Authors: Nancy Xiao & Ayumi Mizuno
import cognitive_face as CF
import httplib, urllib, base64

# Enter a valid subscription key (keeping the quotes in place).
KEY = ''

CF.Key.set(KEY)

uri_base = 'westus.api.cognitive.microsoft.com'

headers = {
    # Request headers.
    'Content-Type': 'application/json',

    'Ocp-Apim-Subscription-Key':KEY,
}

# Enter an ID you haven't used for creating a group before.
# The valid characters for the ID include numbers, English letters in lower case, '-' and '_'.
# The maximum length of the ID is 64.
personGroupId = ''

# The userData field is optional. The size limit for it is 16KB.
body = "{ 'name':''}"

try:
    # NOTE: You must use the same region in your REST call as you used to obtain your subscription keys.
    #   For example, if you obtained your subscription keys from westus, replace "westcentralus" in the
    #   URL below with "westus".
    conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com')
    conn.request("PUT", "/face/v1.0/persongroups/%s" % personGroupId, body, headers)
    response = conn.getresponse()

    # 'OK' indicates success. 'Conflict' means a group with this ID already exists.
    # If you get 'Conflict', change the value of personGroupId above and try again.
    # If you get 'Access Denied', verify the validity of the subscription key above and try again.
    print(response.reason)

    conn.close()
except Exception as e:
    print("[Errno {0}] {1}".format(e.errno, e.strerror))


import cv2
import numpy as np
from PIL import Image

def remove_background(image):

    image = np.array(image)

    mask = np.zeros(image.shape[:2], np.uint8)

    bgdModel = np.zeros((1, 65), np.float64)

    fgdModel = np.zeros((1, 65), np.float64)

    rect = (50, 50, image.shape[1] - 50, image.shape[0] - 50)

    cv2.grabCut(image, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)

    fg_mask = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')

    result = image * fg_mask[:, :, np.newaxis]

    result = Image.fromarray(result)

    return result


from django.urls import path
from . import views

urlpatterns = [
    # path('', views.getRoutes, name='routes'),

    path('get1000words/', views.get1000WordsFromHTMLTable, name='get_1000_words'),

    # path('add_countries/', views.uploadCountryNames, name='add_countries'),

    path('', views.getLanguages, name='languages'),

    path('countries/', views.getCountries, name='countries'),

    # NOT IN USE - considering creating a language home/about page...
    path('<str:lang>/', views.getLanguage, name='language'),

    path('<str:language>/words/', views.getWordsByLanguage, name='language-words' ),

    path('<str:language>/words/random/', views.getRandomWordLanguage, name='random-word-langauge'),

    path('<str:language>/words/<str:pk>/', views.getWordByLanguage, name='word_by_langauge'),

    path('<str:language>/words/<str:pk>/score/', views.wordScore, name='word_score'),

]


"""

Step1: Import package/Module
Step2: Store plotting points
Step3: Plot graph

"""

import matplotlib.pyplot as py

x = [5,4]
y = [8,30]

py.plot(x,y)
py.xlabel("Money in INR")
py.ylabel("Working no of hrs")
#py.bar(x,y)
py.pie(x)
py.show()


import sys
import os
import cocotb

PATH_TO_ROOT = '../../../'

# disable some pylint things since this runs in an unusual environment
#pylint: disable=import-error,wrong-import-position,no-value-for-parameter

sys.path.append(f'{PATH_TO_ROOT}/python/')
import rohd_port_connector

sys.path.append(f'{PATH_TO_ROOT}/test/port_stuff/tmp_output/')
import cosim_test_module

dart_fail = os.getenv('DART_FAIL')
dart_fail_async = os.getenv('DART_FAIL_ASYNC')
dart_hang = os.getenv('DART_HANG')

@cocotb.test()
async def custom_test(dut):
    """
    A custom cocotb test that runs launch_on_port for port testing.
    """
    print('Starting custom_test')

    def dart_command(port):
        cmd = [
            "dart",
            f"{PATH_TO_ROOT}/test/port_stuff/port_launch.dart",
            "--port", str(port)
        ]
        if dart_fail is not None:
            cmd += ["--fail"]
        if dart_fail_async is not None:
            cmd += ["--failAsync"]
        if dart_hang is not None:
            cmd += ["--hang"]
        return cmd

    await rohd_port_connector.launch_on_port(
        cosim_test_module=cosim_test_module,
        dut=dut,
        # dart_command= None,
        dart_command = dart_command,
        log_name='custom_test',
        enable_logging=False,
        dart_connect_timeout=5,
    )


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import json
import os
import posixpath
from os import PathLike

# `typing.Literal` not available until Python 3.8;
# `typing_extensions` not generally available here
from typing import Iterable, Set

FIRST_LINE = "// This file was generated by {}. DO NOT EDIT.".format(
    # `posixpath` for forward slashes, for presentation purposes
    posixpath.relpath(__file__, os.getenv("TOPSRCDIR", "/"))
)

def generate_allowed_items(
    which: str,  # should be: Literal["files", "names"],
    paths: Iterable[PathLike],
) -> str:
    def remove_trailing_comment(s: str) -> str:
        return s[0 : s.find("#")]

    def read_items_from_path(path: PathLike) -> Set[str]:
        out = set()
        with open(path) as file:
            for line in file.readlines():
                line = remove_trailing_comment(line).strip()
                if not line:
                    continue  # comment or empty line; discard
                out.add(line)
        return out

    allowed = set().union(*(read_items_from_path(path) for path in paths))
    # BUG: `json.dumps` may not correctly handle use of the quote character in
    #   thread names
    allowed_list_s = ",\n  ".join(json.dumps(elem) for elem in sorted(allowed))

    return f"""\
static const char *allow_thread_{which}[] = {{
  {allowed_list_s}
}};"""

def generate_allows(
    *, allowed_names: Iterable[PathLike], allowed_files: Iterable[PathLike]
) -> str:
    """
    This function reads in the specified sets of files -- ordinarily,
    ["ThreadAllows.txt"] and ["ThreadFileAllows.txt"] -- and generates the text
    of a header file containing two arrays with their contents, for inclusion by
    the thread-name checker.

    The checker will reject the creation of any thread via NS_NewNamedThread
    unless either:
      - the thread's name is a literal string which is found in the set of
        allowed thread names; or
      - the thread's creation occurs within a file which is found in the set of
        unchecked files.

    The latter condition exists mostly for the definition of NS_NewNamedThread,
    but there also exist a few cases where the thread name is dynamically
    computed (and so can't be checked).
    """
    output_string = (
        FIRST_LINE
        + "\n\n"
        + generate_allowed_items("files", allowed_files)
        + "\n\n"
        + generate_allowed_items("names", allowed_names)
        + "\n"
    )
    return output_string

# Entry point used by build/clang-plugin/moz.build (q.v.).
def generate_file(output, allowed_names, allowed_files):
    output.write(
        generate_allows(allowed_names=[allowed_names], allowed_files=[allowed_files])
    )


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

# Author: Nixawk

# sudo pip install keystone-engine

"""
$ cat /tmp/shellcode.asm
mov rax,0x1
add rax,0x2
$ python2.7 shellcode_x86_64.py /tmp/shellcode.asm
[*] shellcode_x86_64_byAsmFile - generate shellcode with asm in /tmp/shellcode.asm
[*] shellcode_x86_64_byAsmCode - remove comments in asmcode
[*] shellcode_x86_64_byAsmCode - generate shellcode
[*] shellcode_x86_64_byAsmCode - output c shellcode
\x48\xc7\xc0\x01\x00\x00\x00\x48\x83\xc0\x02
"""

from keystone import *

import logging
import sys

logging.basicConfig(level=logging.DEBUG, format="[*] %(funcName)s - %(message)s")
log = logging.getLogger(__name__)

def shellcode_x86_64_byAsmCode(asmcode):
    ks = Ks(KS_ARCH_X86, KS_MODE_64)  # x86_64 shellcodes

    log.debug("remove comments in asmcode")
    asmcode = [_ for _ in asmcode.splitlines() if _ and (not _.startswith(';'))]
    asmcode = ';'.join(asmcode)

    log.debug("generate shellcode")
    shellcode, length = ks.asm(asmcode)

    log.debug("output c shellcode")
    shellcode = ["\\x%02x" % _ for _ in shellcode]
    shellcode = "".join(shellcode)

    return shellcode

def shellcode_x86_64_byAsmFile(file):
    shellcode = ''

    with open(sys.argv[1]) as asmfile:
        log.info("generate shellcode with asm in %s", file)
        asmcode = asmfile.read()
        shellcode = shellcode_x86_64_byAsmCode(asmcode)

    return shellcode

if __name__ == '__main__':
    # asmcode = 'mov rax, 0x1;add rax, 0x2'

    if len(sys.argv) != 2:
        log.info("[*] python {} <shellcode.asm>")
        sys.exit(0)

    shellcode = shellcode_x86_64_byAsmFile(sys.argv[1])
    print(shellcode)


# coding=utf-8
import cv2
import pytesseract
import os
import textdistance
import numpy as np
import pandas as pd
from operator import itemgetter, attrgetter

# 路径配置
ROOT_PATH = os.getcwd()
IMAGE_PATH = os.path.join(ROOT_PATH, 'images/ktp6.png')
LINE_REC_PATH = os.path.join(ROOT_PATH, 'data/ID_CARD_KEYWORD.csv')
CITIES_REC_PATH = os.path.join(ROOT_PATH, 'data/CITIES.csv')
RELIGION_REC_PATH = os.path.join(ROOT_PATH, 'data/RELIGION.csv')
MARRIAGE_REC_PATH = os.path.join(ROOT_PATH, 'data/MARRIAGE.csv')
NEED_COLON = [3, 4, 6, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 21]
NEXT_LINE = 9
ID_NUMBER = 3

# 对身份证上的信息进行初步OCR
def ocr_raw(image_path):
    # (1) Read
    img_raw = cv2.imread(image_path)
    image = cv2.resize(img_raw, (50 * 16, 500))
    img_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    id_number = return_id_number(image, img_gray)
    # img_gray = cv2.cvtColor(img_raw, cv2.COLOR_BGR2GRAY)
    # (2) Threshold
    cv2.fillPoly(img_gray, pts=[np.asarray([(540, 150), (540, 499), (798, 499), (798, 150)])], color=(255, 255, 255))
    th, threshed = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TRUNC)
    # (3) Detect
    result_raw = pytesseract.image_to_string(threshed, lang="ind")
    return result_raw, id_number

# 去除result_raw中的无效信息
def strip_op(result_raw):
    result_list = result_raw.split('\n')
    new_result_list = []
    for tmp_result in result_list:
        if tmp_result.strip(' '):
            new_result_list.append(tmp_result)
    return new_result_list

# KTP_ number
# NIK_NUMBER模版匹配识别
# 从左到右进行排列识别
def sort_contours(cnts, method="left-to-right"):
    reverse = False
    i = 0
    if method == "right-to-left" or method == "bottom-to-top":
        reverse = True
    if method == "top-to-bottom" or method == "bottom-to-top":
        i = 1
    boundingBoxes = [cv2.boundingRect(c) for c in cnts]  # 用一个最小的矩形，把找到的形状包起来x,y,h,w
    (cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
                                        key=lambda b: b[1][i], reverse=reverse))
    return cnts, boundingBoxes

def return_id_number(image, gray):
    # ret, thresh1 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)#二值处理
    # 定义核函数
    rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 15))  # 返回矩形核函数
    # sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 15))
    tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, rectKernel)  # 顶帽
    gradX = cv2.Sobel(tophat, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1)
    # ksize=-1相当于用3*3的 sobel过滤器，由于 Sobel 函数求导数后会有负值，还会有大于 255 的值，而原图像是 uint8 ，
    # 所以 Sobel 建立的图像位数不够，会有截断。因此要使用 16 位有符号的数据类型，即 cv2.CV_16S
    gradX = np.absolute(gradX)
    (minVal, maxVal) = (np.min(gradX), np.max(gradX))
    gradX = (255 * ((gradX - minVal) / (maxVal - minVal)))
    gradX = gradX.astype("uint8")  # Sobel之后无法显示，将其转回原来的 uint8 形式，否则无法显示图像
    # print (np.array(gradX).shape)
    # 通过闭操作（先膨胀，再腐蚀）将数字连在一起
    gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel)
    # THRESH_OTSU会自动寻找合适的阈值，适合双峰，需把阈值参数设置为0
    thresh = cv2.threshold(gradX, 0, 255,
                           cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    # 再来一个闭操作
    thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, rectKernel)  # 再来一个闭操作
    threshCnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  # 寻找轮廓
    cnts = threshCnts
    cur_img = image.copy()
    cv2.drawContours(cur_img, cnts, -1, (0, 0, 255), 3)
    copy = image.copy()
    locs = []
    # 遍历轮廓
    for (i, c) in enumerate(cnts):
        # 计算矩形
        (x, y, w, h) = cv2.boundingRect(c)
        ar = w / float(h)
        # 选择合适的区域，根据实际任务来，这里的基本都是四个数字一组
        # if ar >10:
        # if (w > 40 ) and (h > 10 and h < 20):
        # 符合的留下来
        if h > 10 and w > 100 and x < 300:
            img = cv2.rectangle(copy, (x, y), (x + w, y + h), (0, 255, 0), 2)
            locs.append((x, y, w, h, w * h))
    # 将符合的轮廓按照面积从大到小排序
    locs = sorted(locs, key=itemgetter(1), reverse=False)

    # print(locs[1][0])
    nik = image[locs[1][1] - 10:locs[1][1] + locs[1][3] + 10, locs[1][0] - 10:locs[1][0] + locs[1][2] + 10]
    # cv_show('nik', nik)

    # print(nik)
    text = image[locs[2][1] - 10:locs[2][1] + locs[2][3] + 10, locs[2][0] - 10:locs[2][0] + locs[2][2] + 10]

    # 读取一个模板图像
    img = cv2.imread("images/module.jpeg")
    # 灰度图
    ref = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 二值图像
    ref = cv2.threshold(ref, 66, 255, cv2.THRESH_BINARY_INV)[1]
    # 计算轮廓
    # cv2.findContours()函数接受的参数为二值图，即黑白的（不是灰度图）,cv2.RETR_EXTERNAL只检测外轮廓，cv2.CHAIN_APPROX_SIMPLE只保留终点坐标
    # 返回的list中每个元素都是图像中的一个轮廓
    refCnts, hierarchy = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    # cv2.drawContours(img, refCnts, -1, (0, 0, 255), 3)#上颜色
    # cv2.imshow('888', img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()

    refCnts = sort_contours(refCnts, method="left-to-right")[0]  # 排序，从左到右，从上到下
    digits = {}

    # 遍历每一个轮廓
    for (i, c) in enumerate(refCnts):
        # 计算外接矩形并且resize成合适大小
        (x, y, w, h) = cv2.boundingRect(c)
        # if
        roi = ref[y:y + h, x:x + w]
        roi = cv2.resize(roi, (57, 88))
        # 每一个数字对应每一个模板
        digits[i] = roi
    # cv_show('digits[i]', digits[i])
    # nik = np.clip(nik, 0, 255)
    # nik = np.array(nik,np.uint8)
    # NIK识别
    gray_nik = cv2.cvtColor(nik, cv2.COLOR_BGR2GRAY)
    # gray_nik = cv2.GaussianBlur(gray_nik, (3, 3), 0)
    # ret_nik, thresh_nik = cv2.threshold(gray_nik, 127, 255, cv2.THRESH_BINARY)
    # cv2.imshow('8', gray_nik)
    # cv2.waitKey(0)
    group = cv2.threshold(gray_nik, 127, 255, cv2.THRESH_BINARY_INV)[1]
    # cv2.imshow('9', group)

    # 计算每一组的轮廓
    digitCnts, hierarchy_nik = cv2.findContours(group.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    nik_r = nik.copy()

    cv2.drawContours(nik_r, digitCnts, -1, (0, 0, 255), 3)

    # 定位NIK的位置
    gX = locs[1][0]
    gY = locs[1][1]
    gW = locs[1][2]
    gH = locs[1][3]

    ctx = sort_contours(digitCnts, method="left-to-right")[0]

    locs_x = []
    # 遍历轮廓
    for (i, c) in enumerate(ctx):
        # 计算矩形
        (x, y, w, h) = cv2.boundingRect(c)

        # 选择合适的区域，根据实际任务来，这里的基本都是四个数字一组

        if h > 10 and w > 10:
            img = cv2.rectangle(nik_r, (x, y), (x + w, y + h), (0, 255, 0), 2)
            locs_x.append((x, y, w, h))

    # digitCnts = sort_contours(digitCnts, method="left-to-right")[0]
    output = []
    groupOutput = []
    # 计算每一组中的每一个数值
    for c in locs_x:
        # 找到当前数值的轮廓，resize成合适的的大小
        (x, y, w, h) = c
        roi = group[y:y + h, x:x + w]
        roi = cv2.resize(roi, (57, 88))
        # cv_show('roi',roi)

        # 计算匹配得分
        scores = []

        # 在模板中计算每一个得分
        for (digit, digitROI) in digits.items():
            # 模板匹配
            result = cv2.matchTemplate(roi, digitROI,
                                       cv2.TM_CCOEFF)
            (_, score, _, _) = cv2.minMaxLoc(result)
            scores.append(score)

        # 得到最合适的数字

        groupOutput.append(str(np.argmax(scores)))

    # 画出来
    cv2.rectangle(image, (gX - 5, gY - 5),
                  (gX + gW + 5, gY + gH + 5), (0, 0, 255), 1)
    cv2.putText(image, "".join(groupOutput), (gX, gY - 15),
                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
    # 图像，文字内容， 坐标 ，字体，大小，颜色，字体厚度

    # 得到结果
    output.extend(groupOutput)
    return ''.join(output)

def main():
    raw_df = pd.read_csv(LINE_REC_PATH, header=None)
    # 读入城市字典
    cities_df = pd.read_csv(CITIES_REC_PATH, header=None)
    # 读入宗教字典
    religion_df = pd.read_csv(RELIGION_REC_PATH, header=None)
    # 读入婚姻字典
    marriage_df = pd.read_csv(MARRIAGE_REC_PATH, header=None)
    result_raw, id_number = ocr_raw(IMAGE_PATH)
    result_list = strip_op(result_raw)

    # test
    # result_list = ['—————— ————— —&——— ——————', 'PROVINSI DAEGAH ISTIMAWA YOGYEKARTA', 'KABUPATEN SLEMQN', 'NIK : 347LL40209790001', 'Nama :RIYANTO. SE', 'Tempat/Tgi Lahir : GROBOGAN. 02-09-1979', 'Jenis Kelamin : LAKI-LAKI Gol Darah :O', 'Alamat PRM PURI DOMAS D-3. SEMPU', 'RTARW 001: 024', 'Kel/Desa : WEDOMARTANI', 'Kecamatan : NGEMPLAK', 'Agama :ISPAM', 'Status Perkawman: KAWAN PE', 'Pekerjaan : PEDAGANG 05-06-2012', 'Kewarganegaraan: WNI SA—', 'Berlaku Hingga  :02-09-2017 N EA']
    print('-------origin rec-------')
    print(result_list)
    loc2index = dict()
    for i, tmp_line in enumerate(result_list):
        for j, tmp_word in enumerate(tmp_line.split(' ')):
            tmp_sim_list = [textdistance.damerau_levenshtein.normalized_similarity(tmp_word_, tmp_word.strip(':')) for
                            tmp_word_ in
                            raw_df[0].values]
            tmp_sim_np = np.asarray(tmp_sim_list)
            arg_max = np.argmax(tmp_sim_np)
            if tmp_sim_np[arg_max] >= 0.6:
                loc2index[(i, j)] = arg_max  ##最高相似度

    print('--------processed------------')
    # 数据处理
    last_result_list = []
    useful_info = False
    for i, tmp_line in enumerate(result_list):
        tmp_list = []
        for j, tmp_word in enumerate(tmp_line.split(' ')):
            tmp_word = tmp_word.strip(':')
            if (i, j) in loc2index:
                useful_info = True
                if loc2index[(i, j)] == NEXT_LINE:  ##换行
                    last_result_list.append(tmp_list)
                    tmp_list = []
                tmp_list.append(raw_df[0].values[loc2index[(i, j)]])
                if loc2index[(i, j)] in NEED_COLON:
                    tmp_list.append(':')
            elif tmp_word == ':' or tmp_word == '':
                continue
            else:
                tmp_list.append(tmp_word)
        if useful_info:
            if len(last_result_list) > 2 and ':' not in tmp_list:
                last_result_list[-1].extend(tmp_list)
            else:
                last_result_list.append(tmp_list)
    # print(last_result_list)

    for tmp_data in last_result_list:
        if '—' in tmp_data:
            tmp_data.remove('—')
        # 对前两行进行处理
        if 'PROVINSI' in tmp_data or 'KABUPATEN' in tmp_data or 'KOTA' in tmp_data:
            for tmp_index, tmp_word in enumerate(tmp_data[1:]):
                # 计算相似度
                tmp_sim_list = [textdistance.damerau_levenshtein.normalized_similarity(tmp_word, tmp_word_) for
                                tmp_word_ in cities_df[0].values]
                tmp_sim_np = np.asarray(tmp_sim_list)
                # 获得相似度最大单词索引
                arg_max = np.argmax(tmp_sim_np)
                if tmp_sim_np[arg_max] >= 0.6:
                    tmp_data[tmp_index + 1] = cities_df[0].values[arg_max]

        # 对身份证一行进行处理
        if 'NIK' in tmp_data:
            if len(id_number) != 16:
                id_number = tmp_data[2]
                if "D" in id_number:
                    id_number = id_number.replace("D", "0")
                if "?" in id_number:
                    id_number = id_number.replace("?", "7")
                if "L" in id_number:
                    id_number = id_number.replace("L", "1")
                while len(tmp_data) > 2:
                    tmp_data.pop()
                tmp_data.append(id_number)
            else:
                while len(tmp_data) > 3:
                    tmp_data.pop()
                tmp_data[2] = id_number

        # 对宗教一行进行处理
        if 'Agama' in tmp_data:
            for tmp_index, tmp_word in enumerate(tmp_data[1:]):
                # 计算相似度
                tmp_sim_list = [textdistance.damerau_levenshtein.normalized_similarity(tmp_word, tmp_word_) for
                                tmp_word_ in religion_df[0].values]
                tmp_sim_np = np.asarray(tmp_sim_list)
                # 获得相似度最大单词索引
                arg_max = np.argmax(tmp_sim_np)
                if tmp_sim_np[arg_max] >= 0.6:
                    tmp_data[tmp_index + 1] = religion_df[0].values[arg_max]

        # 对婚姻一行进行处理
        if 'Status' in tmp_data or 'Perkawinan' in tmp_data:
            for tmp_index, tmp_word in enumerate(tmp_data[2:]):
                # 计算相似度
                tmp_sim_list = [textdistance.damerau_levenshtein.normalized_similarity(tmp_word, tmp_word_) for
                                tmp_word_ in marriage_df[0].values]
                tmp_sim_np = np.asarray(tmp_sim_list)
                # 获得相似度最大单词索引
                arg_max = np.argmax(tmp_sim_np)
                if tmp_sim_np[arg_max] >= 0.6:
                    tmp_data[tmp_index + 2] = marriage_df[0].values[arg_max]
        if 'Alamat' in tmp_data:
            for tmp_index in range(len(tmp_data)):
                if "!" in tmp_data[tmp_index]:
                    tmp_data[tmp_index] = tmp_data[tmp_index].replace("!", "I")
                if "1" in tmp_data[tmp_index]:
                    tmp_data[tmp_index] = tmp_data[tmp_index].replace("1", "I")
                if "i" in tmp_data[tmp_index]:
                    tmp_data[tmp_index] = tmp_data[tmp_index].replace("i", "I")
    for tmp_data in last_result_list:
        print(' '.join(tmp_data))

if __name__ == '__main__':
    main()


# External Resources
import datetime

# Flask Resources
from flask import Response, request, jsonify
from flask_restful import Resource
from flask_jwt_extended import create_access_token, create_refresh_token
from flask_jwt_extended import jwt_required, get_jwt_identity
from .errors import forbidden_request

# Local Resources
from models.users import Users
from .errors import unauthorized_user

class RegisterUserApi(Resource):
    """
    Flask-resftul resource for creating new user.
    :Example:
    >>> from flask import Flask
    >>> from flask_restful import Api
    >>> from flask_back_end.app import default_config

    # Create the flask app and resftul api
    >>> app = Flask(__name__)
    >>> app.config.update(default_config)
    >>> api = Api(app=app)
    >>> api.add_resource(RegisterUserApi, '/authentication/signup')
    """
    @staticmethod
    def post() -> Response:
        """
        POST response method for creating user.
        :return: JSON object
        """
        # request.get_json()
        # data = request.get_data()
        data = request.get_json()
        post_user = Users(**data)
        post_user.save()
        user = Users.objects.get(email=data.get('email'))
        auth_success = user.check_pw_hash(data.get('password'))
        if not auth_success:
            return unauthorized_user()
        else:
            expiry = datetime.timedelta(days=365)
            access_token = create_access_token(identity=str(user.id), expires_delta=expiry)
            refresh_token = create_refresh_token(identity=str(user.id))
            return jsonify({'result': {'access_token': access_token,
                                       'refresh_token': refresh_token,
                                       'logged_in_as': f"{user.email}"}})

    @jwt_required()
    def delete(self):
        """
        DELETE:  Removes user account
        """
        user = Users.objects.get(id=get_jwt_identity()).delete()
        if user is None:
            truth = True
        else:
            truth = False
        return jsonify({'result': truth})

class LoginApi(Resource):
    """
    Flask-resftul resource for retrieving user web token.
    :Example:
    >>> from flask import Flask
    >>> from flask_restful import Api
    >>> from flask_back_end.app import default_config
    >>> app = Flask(__name__)
    >>> app.config.update(default_config)
    >>> api = Api(app=app)
    >>> api.add_resource(LoginApi, '/authentication/login')
    """
    @staticmethod
    def post() -> Response:
        """
        POST response method for retrieving user web token.
        :return: JSON object
        """
        data = request.get_json()
        user = Users.objects.get(email=data.get('email'))
        auth_success = user.check_pw_hash(data.get('password'))
        if not auth_success:
            return unauthorized_user()
        else:
            expiry = datetime.timedelta(days=365)
            access_token = create_access_token(identity=str(user.id), expires_delta=expiry)
            refresh_token = create_refresh_token(identity=str(user.id))
            return jsonify({'result': {'access_token': access_token,
                                       'refresh_token': refresh_token,
                                       'logged_in_as': f"{user.email}"}})


import asyncio
from collections import defaultdict
from decimal import Decimal
from itertools import chain
from typing import Any, Dict, List, Optional, Tuple, Union

import pandas as pd

from hummingbot.client.config.config_helpers import ClientConfigAdapter
from hummingbot.connector.gateway.clob_spot.data_sources.dexalot import dexalot_constants as CONSTANTS
from hummingbot.connector.gateway.clob_spot.data_sources.dexalot.dexalot_auth import DexalotAuth, WalletSigner
from hummingbot.connector.gateway.clob_spot.data_sources.dexalot.dexalot_constants import (
    CONNECTOR_NAME,
    DEXALOT_TO_HB_NUMERIC_STATUS_MAP,
    DEXALOT_TO_HB_STATUS_MAP,
    ORDER_SIDE_MAP,
)
from hummingbot.connector.gateway.clob_spot.data_sources.dexalot.dexalot_web_utils import build_api_factory
from hummingbot.connector.gateway.clob_spot.data_sources.gateway_clob_api_data_source_base import (
    GatewayCLOBAPIDataSourceBase,
)
from hummingbot.connector.gateway.common_types import CancelOrderResult, PlaceOrderResult
from hummingbot.connector.gateway.gateway_in_flight_order import GatewayInFlightOrder
from hummingbot.connector.trading_rule import TradingRule
from hummingbot.connector.utils import combine_to_hb_trading_pair, get_new_numeric_client_order_id
from hummingbot.core.api_throttler.async_throttler import AsyncThrottler
from hummingbot.core.data_type.common import OrderType
from hummingbot.core.data_type.in_flight_order import InFlightOrder, OrderState, OrderUpdate, TradeUpdate
from hummingbot.core.data_type.order_book_message import OrderBookMessage, OrderBookMessageType
from hummingbot.core.data_type.trade_fee import MakerTakerExchangeFeeRates, TokenAmount, TradeFeeBase, TradeFeeSchema
from hummingbot.core.event.events import MarketEvent, OrderBookDataSourceEvent
from hummingbot.core.utils.async_utils import safe_ensure_future, safe_gather
from hummingbot.core.utils.tracking_nonce import NonceCreator
from hummingbot.core.web_assistant.connections.data_types import RESTMethod, WSJSONRequest
from hummingbot.core.web_assistant.web_assistants_factory import WebAssistantsFactory
from hummingbot.core.web_assistant.ws_assistant import WSAssistant
from hummingbot.logger import HummingbotLogger

class DexalotAPIDataSource(GatewayCLOBAPIDataSourceBase):
    _logger: Optional[HummingbotLogger] = None

    def __init__(
        self,
        trading_pairs: List[str],
        connector_spec: Dict[str, Any],
        client_config_map: ClientConfigAdapter,
    ):
        super().__init__(
            trading_pairs=trading_pairs, connector_spec=connector_spec, client_config_map=client_config_map
        )
        self._api_key = connector_spec["additional_prompt_values"]["api_key"]
        self._api_factory: Optional[WebAssistantsFactory] = None
        self._stream_listener: Optional[asyncio.Task] = None
        self._client_order_id_nonce_provider = NonceCreator.for_microseconds()
        self._last_traded_price_map = defaultdict(lambda: Decimal("0"))
        self._snapshots_id_nonce_provider = NonceCreator.for_milliseconds()

    @property
    def connector_name(self) -> str:
        return CONNECTOR_NAME

    @property
    def events_are_streamed(self) -> bool:
        return False

    async def start(self):
        signer = WalletSigner(
            chain=self._chain,
            network=self._network,
            address=self._account_id,
            gateway_instance=self._get_gateway_instance(),
        )
        auth = DexalotAuth(signer=signer, address=self._account_id)
        throttler = AsyncThrottler(CONSTANTS.RATE_LIMITS)
        self._api_factory = build_api_factory(throttler=throttler, api_key=self._api_key, auth=auth)
        self._stream_listener = safe_ensure_future(self._listen_to_streams())
        self._gateway_order_tracker.lost_order_count_limit = CONSTANTS.LOST_ORDER_COUNT_LIMIT
        await super().start()

    async def stop(self):
        await super().stop()
        self._stream_listener is not None and self._stream_listener.cancel()

    def get_supported_order_types(self) -> List[OrderType]:
        return [OrderType.LIMIT, OrderType.LIMIT_MAKER]

    async def place_order(
        self, order: GatewayInFlightOrder, **kwargs
    ) -> Tuple[Optional[str], Optional[Dict[str, Any]]]:
        place_order_results = await super().batch_order_create(orders_to_create=[order])
        result = place_order_results[0]
        if result.exception is not None:
            raise result.exception
        self.logger().debug(
            f"Order creation transaction hash for {order.client_order_id}:"
            f" {result.misc_updates['creation_transaction_hash']}"
        )
        return result.exchange_order_id, result.misc_updates

    async def batch_order_create(self, orders_to_create: List[GatewayInFlightOrder]) -> List[PlaceOrderResult]:
        super_batch_order_create = super().batch_order_create  # https://stackoverflow.com/a/31895448/6793798
        tasks = [
            super_batch_order_create(
                orders_to_create=orders_to_create[i: i + CONSTANTS.MAX_ORDER_CREATIONS_PER_BATCH]
            )
            for i in range(0, len(orders_to_create), CONSTANTS.MAX_ORDER_CREATIONS_PER_BATCH)
        ]
        results = await safe_gather(*tasks)
        flattened_results = list(chain(*results))
        self.logger().debug(
            f"Order creation transaction hashes for {', '.join([o.client_order_id for o in orders_to_create])}"
        )
        for result in flattened_results:
            self.logger().debug(f"Transaction hash: {result.misc_updates['creation_transaction_hash']}")
        return flattened_results

    async def cancel_order(self, order: GatewayInFlightOrder) -> Tuple[bool, Optional[Dict[str, Any]]]:
        cancel_order_results = await super().batch_order_cancel(orders_to_cancel=[order])
        self.logger().debug(
            f"cancel order transaction hash for {order.client_order_id}:"
            f" {cancel_order_results[0].misc_updates['cancelation_transaction_hash']}"
        )
        misc_updates = {}
        canceled = False
        if len(cancel_order_results) != 0:
            result = cancel_order_results[0]
            if result.exception is not None:
                raise result.exception
            misc_updates = result.misc_updates
            canceled = True
        return canceled, misc_updates

    async def batch_order_cancel(self, orders_to_cancel: List[GatewayInFlightOrder]) -> List[CancelOrderResult]:
        super_batch_order_cancel = super().batch_order_cancel  # https://stackoverflow.com/a/31895448/6793798
        tasks = [
            super_batch_order_cancel(
                orders_to_cancel=orders_to_cancel[i: i + CONSTANTS.MAX_ORDER_CANCELATIONS_PER_BATCH]
            )
            for i in range(0, len(orders_to_cancel), CONSTANTS.MAX_ORDER_CANCELATIONS_PER_BATCH)
        ]
        results = await safe_gather(*tasks)
        flattened_results = list(chain(*results))
        self.logger().debug(
            f"Order cancelation transaction hashes for {', '.join([o.client_order_id for o in orders_to_cancel])}"
        )
        for result in flattened_results:
            self.logger().debug(f"Transaction hash: {result.misc_updates['cancelation_transaction_hash']}")
        return flattened_results

    def get_client_order_id(
        self, is_buy: bool, trading_pair: str, hbot_order_id_prefix: str, max_id_len: Optional[int]
    ) -> str:
        decimal_id = get_new_numeric_client_order_id(
            nonce_creator=self._client_order_id_nonce_provider,
            max_id_bit_count=CONSTANTS.MAX_ID_BIT_COUNT,
        )
        return "{0:#0{1}x}".format(  # https://stackoverflow.com/a/12638477/6793798
            decimal_id, CONSTANTS.MAX_ID_HEX_DIGITS + 2
        )

    async def get_account_balances(self) -> Dict[str, Dict[str, Decimal]]:
        self._check_markets_initialized() or await self._update_markets()

        result = await self._get_gateway_instance().get_balances(
            chain=self.chain,
            network=self._network,
            address=self._account_id,
            token_symbols=list(self._hb_to_exchange_tokens_map.values()),
            connector=self.connector_name,
        )
        balances = defaultdict(dict)
        for exchange_token, total_balance in result["balances"]["total"].items():
            token = self._hb_to_exchange_tokens_map.inverse[exchange_token]
            balance_value = Decimal(total_balance)
            if balance_value != 0:
                balances[token]["total_balance"] = balance_value
        for exchange_token, available_balance in result["balances"]["available"].items():
            token = self._hb_to_exchange_tokens_map.inverse[exchange_token]
            if token in balances:
                balances[token]["available_balance"] = Decimal(available_balance)
        return balances

    async def get_order_status_update(self, in_flight_order: GatewayInFlightOrder) -> OrderUpdate:
        await in_flight_order.get_creation_transaction_hash()
        status_update = None

        if in_flight_order.exchange_order_id is None:
            status_update = await self._get_order_status_update_from_transaction_status(in_flight_order=in_flight_order)
            if status_update is not None:
                in_flight_order.exchange_order_id = status_update.exchange_order_id
                self._publisher.trigger_event(event_tag=MarketEvent.OrderUpdate, message=status_update)

        if (
            in_flight_order.exchange_order_id is not None
            or (
                status_update is not None
                and status_update.new_state not in [OrderState.FAILED, OrderState.PENDING_CREATE]
            )
        ):
            status_update = await self._get_order_status_update_with_order_id(in_flight_order=in_flight_order)
            self._publisher.trigger_event(event_tag=MarketEvent.OrderUpdate, message=status_update)

        if status_update is None:
            raise ValueError(f"No update found for order {in_flight_order.client_order_id}.")

        return status_update

    async def get_all_order_fills(self, in_flight_order: GatewayInFlightOrder) -> List[TradeUpdate]:
        self._check_markets_initialized() or await self._update_markets()

        if in_flight_order.exchange_order_id is None:  # we still haven't received an order status update
            await self.get_order_status_update(in_flight_order=in_flight_order)

        request_params = {
            "orderid": await in_flight_order.get_exchange_order_id(),
        }

        resp = await self._api_get(
            path_url=CONSTANTS.EXECUTIONS_PATH,
            throttler_limit_id=CONSTANTS.EXECUTIONS_RATE_LIMIT_ID,
            params=request_params,
            is_auth_required=True,
        )

        trade_updates = []
        for fill_data in resp:
            fill_price = Decimal(fill_data["price"])
            fill_size = Decimal(fill_data["quantity"])
            fee_token = self._hb_to_exchange_tokens_map.inverse[fill_data["feeunit"]]
            fee = TradeFeeBase.new_spot_fee(
                fee_schema=TradeFeeSchema(),
                trade_type=ORDER_SIDE_MAP[fill_data["side"]],
                flat_fees=[TokenAmount(token=fee_token, amount=Decimal(fill_data["fee"]))]
            )
            trade_update = TradeUpdate(
                trade_id=fill_data["execid"],
                client_order_id=in_flight_order.client_order_id,
                exchange_order_id=fill_data["orderid"],
                trading_pair=in_flight_order.trading_pair,
                fill_timestamp=self._dexalot_timestamp_to_timestamp(period_str=fill_data["ts"]),
                fill_price=fill_price,
                fill_base_amount=fill_size,
                fill_quote_amount=fill_price * fill_size,
                fee=fee,
                is_taker=fill_data["type"] == "T",
            )
            trade_updates.append(trade_update)

        return trade_updates

    async def get_last_traded_price(self, trading_pair: str) -> Decimal:
        return self._last_traded_price_map[trading_pair]

    def is_order_not_found_during_status_update_error(self, status_update_exception: Exception) -> bool:
        return (
            str(status_update_exception).endswith("not found")  # transaction not found
            or str(status_update_exception).startswith("No update found for order")  # status update not found
        )

    def is_order_not_found_during_cancelation_error(self, cancelation_exception: Exception) -> bool:
        return False

    async def _update_snapshots_loop(self):
        pass  # Dexalot streams the snapshots via websocket

    async def _get_order_status_update_from_transaction_status(
        self, in_flight_order: GatewayInFlightOrder
    ) -> Optional[OrderUpdate]:
        """
        Transaction not yet listed
        {
          "network": "dexalot",
          "currentBlock": 1005813,
          "timestamp": 1678431621633,
          "txHash": "0xadaef9c4540192e45c991ffe6f12cc86be9c07b80b43487e5778d95c964405c7",  # noqa: documentation
          "txBlock": -1,
          "txStatus": -1,
          "txData": null,
          "txReceipt": null
        }

        ======================

        Transaction failed (abbreviated)
        {
          "timestamp": 1678431839032,
          "txHash": "0x34494d6c88e36415bbf3d1f44e648ed11bd952380743395f950c242cded147d1",  # noqa: documentation
          "txStatus": 1,
          "txData": {
            ...
          },
          "txReceipt": {
            "status": 0,
          }
        }

        ======================

        Transaction success (abbreviated)
        {
          "txStatus": 1,
          "timestamp": 1678440295386,
          "txHash": "0x6aca797aa30636c3285c5ecaee76fe85a9ee2ac60d74af64b8d6660a6f4f0f27",  # noqa: documentation
          "txData": {
            ...
          },
          "txReceipt": {
            "transactionHash": "...",
            "logs": [
              {                                                   # SUCCESS
                "name": "OrderStatusChanged",
                "events": [
                  { ... },
                  { ... },
                  { ... },
                  {
                    "name": "orderId",
                    "type": "bytes32",
                    "value": "0x0000000000000000000000000000000000000000000000000000000063d84143"  # noqa: documentation
                  },
                  {
                    "name": "clientOrderId",
                    "type": "bytes32",
                    "value": "0xb71309b1a16b7903ffc817f256d1f4d42602d402bd81001feb29fd067e69013b"  # noqa: documentation
                  },
                  {
                    "name": "price",
                    "type": "uint256",
                    "value": "15000000"
                  },
                  { ... },
                  {
                    "name": "quantity",
                    "type": "uint256",
                    "value": "500000000000000000"
                  },
                  { ... },
                  { ... },
                  { ... },
                  {
                    "name": "status",
                    "type": "uint8",
                    "value": "0"
                  },
                  { ... },
                  { ... },
                  { ... },
                ],
                "address": "0x09383137C1eEe3E1A8bc781228E4199f6b4A9bbf"
              },
               {                                                   # FAILED
                "name": "OrderStatusChanged",
                "events": [
                  { ... },
                  { ... },
                  { ... },
                  {
                    "name": "orderId",
                    "type": "bytes32",
                    "value": "0x0000000000000000000000000000000000000000000000000000000000000000"  # noqa: documentation
                  },
                  {
                    "name": "clientOrderId",
                    "type": "bytes32",
                    "value": "0x936f0db5ba15ceaf9d6ee8fb6c1aeca01e67dfb3d061680a4ac3ce8ccb153072"  # noqa: documentation
                  },
                  {
                    "name": "price",
                    "type": "uint256",
                    "value": "15000000"
                  },
                  { ... },
                  {
                    "name": "quantity",
                    "type": "uint256",
                    "value": "0"
                  },
                  { ... },
                  { ... },
                  { ... },
                  {
                    "name": "status",
                    "type": "uint8",
                    "value": "1"
                  },
                  { ... },
                  { ... },
                  { ... },
                ],
               },
            ],
            "status": 1,
          },
        }
        """
        transaction_data = await self._get_gateway_instance().get_transaction_status(
            chain=self._chain,
            network=self._network,
            transaction_hash=in_flight_order.creation_transaction_hash,
            connector="dexalot",
        )
        if (
            transaction_data is not None
            and transaction_data["txStatus"] == 1
            and transaction_data.get("txReceipt", {}).get("status") == 1
        ):
            order_data = self._find_order_data_from_transaction_data(
                transaction_data=transaction_data, in_flight_order=in_flight_order
            )
            new_dexalot_numeric_state_event = next(filter(lambda event: event["name"] == "status", order_data))
            new_dexalot_numeric_state = new_dexalot_numeric_state_event["value"]
            exchange_order_id_event = next(filter(lambda event: event["name"] == "orderId", order_data))
            exchange_order_id = exchange_order_id_event["value"]
            timestamp = transaction_data["timestamp"] * 1e-3
            order_update = OrderUpdate(
                trading_pair=in_flight_order.trading_pair,
                update_timestamp=timestamp,
                new_state=DEXALOT_TO_HB_NUMERIC_STATUS_MAP[int(new_dexalot_numeric_state)],
                client_order_id=in_flight_order.client_order_id,
                exchange_order_id=exchange_order_id,
            )
        elif (
            transaction_data is not None
            and (
                transaction_data["txStatus"] == -1
                or transaction_data.get("txReceipt", {}).get("status") == 0
            )
        ):
            order_update = None  # transaction data not found
        else:  # transaction is still being processed
            order_update = OrderUpdate(
                trading_pair=in_flight_order.trading_pair,
                update_timestamp=self._time(),
                new_state=OrderState.PENDING_CREATE,
                client_order_id=in_flight_order.client_order_id,
            )

        return order_update

    @staticmethod
    def _find_order_data_from_transaction_data(
        transaction_data: Dict[str, Any], in_flight_order: GatewayInFlightOrder
    ) -> List[Dict[str, str]]:
        log_items = transaction_data["txReceipt"]["logs"]

        def log_items_filter(log_event_: Dict[str, Any]) -> bool:
            target_hit = False
            if log_event_["name"] == "OrderStatusChanged":
                client_order_id_event = next(
                    filter(
                        lambda event: event["name"] == "clientOrderId",
                        log_event_["events"],
                    ),
                )
                client_order_id = client_order_id_event["value"]
                if client_order_id == in_flight_order.client_order_id:
                    target_hit = True
            return target_hit

        log_item = next(filter(log_items_filter, log_items))
        log_event = log_item["events"]

        return log_event

    async def _get_order_status_update_with_order_id(self, in_flight_order: InFlightOrder) -> Optional[OrderUpdate]:
        url = f"{CONSTANTS.ORDERS_PATH}/{in_flight_order.exchange_order_id}"

        try:
            resp = await self._api_get(
                path_url=url, throttler_limit_id=CONSTANTS.ORDERS_RATE_LIMIT_ID, is_auth_required=True
            )
        except OSError as e:
            if "HTTP status is 404" in str(e):
                raise ValueError(f"No update found for order {in_flight_order.exchange_order_id}.")
            raise e

        if resp.get("message") == "":
            raise ValueError(f"No update found for order {in_flight_order.exchange_order_id}.")
        else:
            status_update = OrderUpdate(
                trading_pair=in_flight_order.trading_pair,
                update_timestamp=pd.Timestamp(resp["updateTs"]).timestamp(),
                new_state=DEXALOT_TO_HB_STATUS_MAP[resp["status"]],
                client_order_id=in_flight_order.client_order_id,
                exchange_order_id=resp["id"],
            )

        return status_update

    def _parse_trading_rule(self, trading_pair: str, market_info: Dict[str, Any]) -> TradingRule:
        base = market_info["baseSymbol"].upper()
        quote = market_info["quoteSymbol"].upper()
        quote_scaler = Decimal(f"1e-{market_info['quoteDecimals']}")
        return TradingRule(
            trading_pair=combine_to_hb_trading_pair(base=base, quote=quote),
            min_order_size=Decimal(f"1e-{market_info['baseDisplayDecimals']}"),
            min_price_increment=Decimal(f"1e-{market_info['quoteDisplayDecimals']}"),
            min_quote_amount_increment=Decimal(f"1e-{market_info['quoteDisplayDecimals']}"),
            min_base_amount_increment=Decimal(f"1e-{market_info['baseDisplayDecimals']}"),
            min_notional_size=Decimal(market_info["minTradeAmount"]) * quote_scaler,
            min_order_value=Decimal(market_info["minTradeAmount"]) * quote_scaler,
        )

    def _get_trading_pair_from_market_info(self, market_info: Dict[str, Any]) -> str:
        base = market_info["baseSymbol"].upper()
        quote = market_info["quoteSymbol"].upper()
        trading_pair = combine_to_hb_trading_pair(base=base, quote=quote)
        return trading_pair

    def _get_exchange_base_quote_tokens_from_market_info(self, market_info: Dict[str, Any]) -> Tuple[str, str]:
        base = market_info["baseSymbol"]
        quote = market_info["quoteSymbol"]
        return base, quote

    def _get_exchange_trading_pair_from_market_info(self, market_info: Dict[str, Any]) -> str:
        exchange_trading_pair = f"{market_info['baseSymbol']}/{market_info['quoteSymbol']}"
        return exchange_trading_pair

    def _get_last_trade_price_from_ticker_data(self, ticker_data: List[Dict[str, Any]]) -> Decimal:
        raise NotImplementedError

    def _get_maker_taker_exchange_fee_rates_from_market_info(
        self, market_info: Dict[str, Any]
    ) -> MakerTakerExchangeFeeRates:
        maker_taker_exchange_fee_rates = MakerTakerExchangeFeeRates(
            maker=Decimal(str(market_info["makerRate"])),
            taker=Decimal(str(market_info["takerRate"])),
            maker_flat_fees=[],
            taker_flat_fees=[],
        )
        return maker_taker_exchange_fee_rates

    @staticmethod
    def _timestamp_to_dexalot_timestamp(timestamp: float) -> str:
        dt_str = str(pd.Timestamp.utcfromtimestamp(ts=timestamp).tz_localize("UTC"))
        return dt_str

    @staticmethod
    def _dexalot_timestamp_to_timestamp(period_str: str) -> float:
        ts = pd.Timestamp(period_str).timestamp()
        return ts

    # REST methods

    async def _api_get(self, *args, **kwargs) -> Union[Dict[str, Any], List[Dict[str, Any]]]:
        kwargs["method"] = RESTMethod.GET
        return await self._api_request(*args, **kwargs)

    async def _api_post(self, *args, **kwargs) -> Union[Dict[str, Any], List[Dict[str, Any]]]:
        kwargs["method"] = RESTMethod.POST
        return await self._api_request(*args, **kwargs)

    async def _api_put(self, *args, **kwargs) -> Union[Dict[str, Any], List[Dict[str, Any]]]:
        kwargs["method"] = RESTMethod.PUT
        return await self._api_request(*args, **kwargs)

    async def _api_delete(self, *args, **kwargs) -> Union[Dict[str, Any], List[Dict[str, Any]]]:
        kwargs["method"] = RESTMethod.DELETE
        return await self._api_request(*args, **kwargs)

    async def _api_request(
        self,
        path_url,
        throttler_limit_id: str,
        is_auth_required: bool = False,
        method: RESTMethod = RESTMethod.GET,
        params: Optional[Dict[str, Any]] = None,
        data: Optional[Dict[str, Any]] = None,
    ) -> Union[Dict[str, Any], List[Dict[str, Any]]]:
        rest_assistant = await self._api_factory.get_rest_assistant()
        url = CONSTANTS.BASE_PATH_URL[self._network] + path_url

        request_result = await rest_assistant.execute_request(
            url=url,
            params=params,
            data=data,
            method=method,
            is_auth_required=is_auth_required,
            throttler_limit_id=throttler_limit_id,
        )

        return request_result

    async def _listen_to_streams(self):
        while True:
            try:
                ws_assistant = await self._connected_websocket_assistant()
                await self._subscribe_to_pairs(ws_assistant)
                await self._process_websocket_messages(ws_assistant)
            except asyncio.CancelledError:
                raise
            except Exception:
                self.logger().exception("Unexpected error while listening to user stream. Reconnecting...")
                await self._sleep(1.0)

    async def _connected_websocket_assistant(self) -> WSAssistant:
        ws_url = CONSTANTS.WS_PATH_URL[self._network]
        if self._api_key is not None and len(self._api_key) > 0:
            auth_response = await self._api_get(
                path_url=CONSTANTS.WS_AUTH_PATH,
                throttler_limit_id=CONSTANTS.WS_AUTH_RATE_LIMIT_ID,
                is_auth_required=True,
            )
            token = auth_response["token"]
            ws_url = f"{CONSTANTS.WS_PATH_URL[self._network]}?wstoken={token}"
        ws: WSAssistant = await self._api_factory.get_ws_assistant()
        await ws.connect(ws_url=ws_url, ping_timeout=CONSTANTS.HEARTBEAT_TIME_INTERVAL)
        return ws

    async def _subscribe_to_pairs(self, ws_assistant: WSAssistant):
        trading_pairs_map = await self.get_symbol_map()
        for trading_pair in self._trading_pairs:
            exchange_trading_pair = trading_pairs_map.inverse[trading_pair]
            market_info = self._markets_info[trading_pair]
            pair_sub_request = {
                "data": exchange_trading_pair,
                "pair": exchange_trading_pair,
                "type": "subscribe",
                "decimal": market_info["quoteDisplayDecimals"],
            }
            request = WSJSONRequest(payload=pair_sub_request, throttler_limit_id=CONSTANTS.WS_SUB_RATE_LIMIT_ID)
            await ws_assistant.send(request)

    async def _process_websocket_messages(self, websocket_assistant: WSAssistant):
        async for ws_response in websocket_assistant.iter_messages():
            data = ws_response.data
            await self._process_event_message(event_message=data)

    async def _process_event_message(self, event_message: Dict[str, Any]):
        if event_message["type"] == "lastTrade":
            await self._process_last_traded_price(price_message=event_message)
        elif event_message["type"] == "orderBooks":
            await self._process_snapshot_message(snapshot_message=event_message)

    async def _process_last_traded_price(self, price_message: Dict[str, Any]):
        data = price_message["data"]
        last_price_message = data[-1]
        price = Decimal(last_price_message["price"])
        trading_pairs_map = await self.get_symbol_map()
        exchange_pair = price_message["pair"]
        trading_pair = trading_pairs_map[exchange_pair]
        self._last_traded_price_map[trading_pair] = price

    async def _process_snapshot_message(self, snapshot_message: Dict[str, Any]):
        data = snapshot_message["data"]
        trading_pairs_map = await self.get_symbol_map()
        exchange_trading_pair = snapshot_message["pair"]
        trading_pair = trading_pairs_map[exchange_trading_pair]
        market_info = self._markets_info[trading_pair]
        price_scaler = Decimal(f"1e-{market_info['quoteDecimals']}")
        size_scaler = Decimal(f"1e-{market_info['baseDecimals']}")

        bid_price_strings = data["buyBook"][0]["prices"].split(",")
        bid_size_strings = data["buyBook"][0]["quantities"].split(",")
        bids = [
            (Decimal(price) * price_scaler, Decimal(size) * size_scaler)
            for price, size in zip(bid_price_strings, bid_size_strings)
        ]

        ask_price_strings = data["sellBook"][0]["prices"].split(",")
        ask_size_strings = data["sellBook"][0]["quantities"].split(",")
        asks = [
            (Decimal(price) * price_scaler, Decimal(size) * size_scaler)
            for price, size in zip(ask_price_strings, ask_size_strings)
        ]

        timestamp = self._time()
        update_id = self._snapshots_id_nonce_provider.get_tracking_nonce(timestamp=timestamp)
        snapshot_msg = OrderBookMessage(
            message_type=OrderBookMessageType.SNAPSHOT,
            content={
                "trading_pair": trading_pair,
                "update_id": update_id,
                "bids": bids,
                "asks": asks,
            },
            timestamp=timestamp,
        )
        self._publisher.trigger_event(event_tag=OrderBookDataSourceEvent.SNAPSHOT_EVENT, message=snapshot_msg)


"""add user display_name

Revision ID: 99a3d2eb08f9
Revises: a9627f63fbed
Create Date: 2022-09-13 07:30:18.747880

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "99a3d2eb08f9"
down_revision = "a9627f63fbed"
branch_labels = None
depends_on = None

def upgrade():
    op.add_column(
        "user", sa.Column("display_name", sa.String, nullable=True), schema="public"
    )

def downgrade():
    op.drop_column("user", "display_name", schema="public")


#ojinal resmin çözünürlüğüne göre bencere boyutlandırma

import cv2

resim=cv2.imread('doga.jpg')#resmi okumak için ,resmi resim değişkenine atıyorum

cozunurluk=480,720

resim_genislik_olcegi = cozunurluk[0] / resim.shape[1]
resim_yukseklik_olcegi = cozunurluk[1] / resim.shape[0]
yeni_olcek_degeri=min(resim_genislik_olcegi,resim_yukseklik_olcegi)
pencere_genislik= int(resim.shape[1]*yeni_olcek_degeri)
pencere_yukseklik=int(resim.shape[0]*yeni_olcek_degeri)

cv2.namedWindow('doga',cv2.WINDOW_NORMAL)
cv2.resizeWindow('doga',pencere_genislik,pencere_yukseklik)

#resmi ekranda görüntüle pencereyi hemen kapama bekle ve herhangi bir tuşa basınca pencereyi kapama işlemi
cv2.imshow('doga',resim)
cv2.waitKey(0)
cv2.destroyAllWindows()


import sys
from functools import reduce

def part1_functional(groups):
    # x is total set of unique answers for the group, and y is the
    # list of answers for an individual person
    return sum(len(reduce(lambda x,y: x.union(set(y)), group, set()))
               for group in groups)

def part1(groups):
    '''
    For each group count the total number of questions for which ANYONE
    answered yes.
    '''
    total_answers = 0
    for group in groups:
        group_answers = set()
        for answers in group:
            group_answers.update(set(answers))
        total_answers += len(group_answers)
    return total_answers

def part2(groups):
    '''
    For each group count the total number of questions for which EVERYONE
    answered yes.

    For this question we will take a different approach and use bit logic.
    Since we know all answers will be from a-z, we can represent all answers
    from an individual using 26 bits. Then, to find which answers _all_ members
    had in common, we can simply 'AND' together (bitwise) all the answers and
    sum the bits which are remaining.

    E.g. 'abc', 'bcf' and 'bc' would be

    (fedcba)
    '000111'
    '100110'
    '000110'
    -------- (bitwise &)
    '000110'

    thus all answers had 2 in common, b and c.

    '''
    def string_to_bits(s):
        bits = 0
        for letter in s:
            bits |= 1 << (ord(letter) - ord('a'))
        return bits

    def total_set_bits(n):
        # could just use bin(n).count('1') but lets keep things consistent
        return sum((n >> i) & 1 for i in range(26))

    total = 0

    for group in groups:
        # initialize with all bits turned on
        common_answers = (1 << 26) - 1

        for answers in group:
            # initialize this person's answers with all bits off
            answers_as_bits = 0
            for answer in answers:
                answers_as_bits |= string_to_bits(answer)

            # Now for this group, keep only the bits (answers) which we already
            # have seen
            common_answers &= answers_as_bits

        total_in_common_for_group = total_set_bits(common_answers)
        total += total_in_common_for_group

    return total

def main(input_file):
    with open(input_file, 'r') as f:
        groups = [group.rstrip().split('\n')
                  for group in f.read().split('\n\n')]

    total_1 = part1(groups)
    print("Part 1:", total_1)

    total_2 = part2(groups)
    print("Part 2:", total_2)

if __name__ == '__main__':
    input_file = sys.argv[-1] if len(sys.argv) > 1 else 'input.txt'
    main(input_file)


# tweetutilities.py
"""Utility functions for interacting with Tweepy objects."""
from better_profanity import profanity
from bs4 import BeautifulSoup
import deepl
from geopy import ArcGIS
import keys_mastodon
import preprocessor as p
import requests
import socket
import time
from urllib.parse import urlparse
from IPython.core.display import display, HTML

# load censored words list
profanity.load_censor_words()

# translator to autodetect source language and return English
translator = deepl.Translator(keys_mastodon.deepL_key)

# tweet-preprocessor remove @ mentions, emojis, hashtags, URLs
p.set_options(p.OPT.MENTION, p.OPT.EMOJI, p.OPT.HASHTAG, p.OPT.URL)

def print_toot(toot):
    """Prints one toot's username and content (translated, if not English)."""

    # display the username of the sender
    print(f'{toot.account.username}:')

    # display toot text
    if toot.language and toot.language.startswith('en'):
        # render HTML version of toot in an HTML div indented 0.5 inches
        text = profanity.censor(toot.content)
        display(HTML(f'
{text}
'))

    # if the language is not English, display original & translated text
    else:
        # render HTML version of toot in an HTML div indented 0.5 inches
        print(f'ORIGINAL: ')
        display(HTML(f'
{toot.content}
'))

        # remove HTML
        soup = BeautifulSoup(toot.content, 'html.parser')
        content = soup.get_text() # remove all HTML/CSS tags and commands

        # translate content
        try:
            result = translator.translate_text(content, target_lang='en-us')
            text = profanity.censor(result.text)
        except:
            text = 'toot was empty'

        # render HTML version of toot in an HTML div indented 0.5 inches
        print(f'TRANSLATED: ')
        display(HTML(f'
{text}
'))

def print_toots(toots):
    """For each toot in toots, call print_toot to display
       the username of the sender and toot text."""
    for toot in toots:
        print_toot(toot)

def get_domain_location_from_url(url):
    """Parse the domain from url, then look up the IP address
       and get its city.

       Get a key at for ipgeolocation.io web service at:
       https://ipgeolocation.io/signup.html
    """

    # get domain from url argument using Python
    # urllib.parse module's urlparse function
    domain = urlparse(url).netloc

    # get IP address for hostname using Python
    # socket module's gethostbyname function
    ip_address = socket.gethostbyname(domain)

    # URL to access ipgeolocation.io web services
    ipgeolocation_url = ('https://api.ipgeolocation.io/ipgeo?apiKey=' +
       keys_mastodon.ipgeolocation_key + f'&ip={ip_address}')

    # use Python requests module to invoke web service
    response = requests.get(ipgeolocation_url)

    # if successful get the location
    if response.status_code == 200:
        data = response.json() # convert JSON to dictionary
        location = (f"{data['city']}, {data['state_prov']}, " +
            f"{data['country_code2']}")
        return location
    else:
        print('Error getting location\n',
              f'{response.status_code}: {response.text}')
        return None

def get_toot_content(toot):
    """Return dictionary with username of toot sender, toot content
       translated to English (if necessary) and the sender account's
       Mastodon server domain."""

    fields = {}
    fields['username'] = toot.account.username

    # possibly translate plain_text
    if toot.language and not toot.language.startswith('en'):
        try:
            result = translator.translate_text(
                toot.content, target_lang='en-us')

            # save translated text
            fields['text'] = profanity.censor(result.text)
        except:
            fields['text'] = None
    else:
        fields['text'] = profanity.censor(toot.content)

    # look up server location
    fields['location'] = get_domain_location_from_url(toot.url)

    return fields

def get_geocodes(toot_list):
    """Get the latitude and longitude for each tweet's location.
    Returns the number of tweets with invalid location data."""

    print('Getting coordinates for mastodon server locations...')
    geo = ArcGIS()  # geocoder
    bad_locations = 0

    for toot in toot_list:
        processed = False
        delay = .1  # used if OpenMapQuest times out to delay next call
        while not processed:
            try:  # get coordinates for tweet['location']
                geo_location = geo.geocode(toot['location'])
                processed = True
            except:  # timed out, so wait before trying again
                print('Service timed out. Waiting.')
                time.sleep(delay)
                delay += .1

        if geo_location:
            toot['latitude'] = geo_location.latitude
            toot['longitude'] = geo_location.longitude
        else:
            bad_locations += 1  # toot['location'] was invalid

    print('Done geocoding')
    return bad_locations

##########################################################################
# (C) Copyright 2022 by Deitel & Associates, Inc. and                    #
# Pearson Education, Inc. All Rights Reserved.                           #
#                                                                        #
# DISCLAIMER: The authors and publisher of this book have used their     #
# best efforts in preparing the book. These efforts include the          #
# development, research, and testing of the theories and programs        #
# to determine their effectiveness. The authors and publisher make       #
# no warranty of any kind, expressed or implied, with regard to these    #
# programs or to the documentation contained in these books. The authors #
# and publisher shall not be liable in any event for incidental or       #
# consequential damages in connection with, or arising out of, the       #
# furnishing, performance, or use of these programs.                     #
##########################################################################


from flask import Flask, render_template, url_for, request, redirect, flash, jsonify
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Restaurant, Base, MenuItem

app = Flask(__name__)

#set up to read the data base
engine = create_engine('sqlite:///restaurantmenu.db', connect_args={'check_same_thread': False}, echo=True)
Base.metadata.bind = engine
DBsession = sessionmaker(bind = engine)
session = DBsession()

#home page, list of menu
@app.route('/', methods = ['GET', 'POST'])
def HomePage():
	if request.method == 'POST':
		option = request.form['option']
		if option == 'restaurants':
			return redirect(url_for('ShowRestaurants'))
		else:
			return redirect(url_for('ShowMenu'))

	else:
		return render_template('homePage.html')

@app.route('/restaurants')
def ShowRestaurants():
	restaurants = session.query(Restaurant).all()
	return render_template('restaurants.html', restaurants = restaurants)

@app.route('/menus')
def ShowMenu():
	menus = session.query(MenuItem).all()
	return render_template('menu.html', menus = menus)

#show menu of a restaurant
@app.route('/restaurants/<int:restaurantID>/menu')
def ShowRestaurantMenu(restaurantID):
	restaurant = session.query(Restaurant).filter_by(id = restaurantID).one()
	menu_items = session.query(MenuItem).filter_by(restaurant_id = restaurantID).all()
	return render_template('restaurant_menu.html',
		restaurant = restaurant, menus = menu_items)

#add new menu item
@app.route('/menu/new/<int:restaurant_id>', methods = ['GET', 'POST'])
def NewMenu(restaurant_id):
	restaurant = session.query(Restaurant).filter_by(id = restaurant_id).one()
	if request.method == 'POST':
		menu_name = request.form['name']
		new_menu = MenuItem(name = menu_name, restaurant = restaurant)
		session.add(new_menu)
		session.commit()
		flash('New menu item is created. Add more?')
		return redirect(url_for('ShowRestaurantMenu', restaurantID = restaurant.id))

	# GET request
	else:
		return render_template('addMenu.html', restaurant = restaurant)

#edit menu item
@app.route('/menu/edit/<int:restaurant_id>/<int:menu_id>', methods = ['GET', 'POST'])
def EditMenu(restaurant_id, menu_id):
	restaurant = session.query(Restaurant).filter_by(id = restaurant_id).one()
	menu = session.query(MenuItem).filter_by(id = menu_id).one()
	if request.method == 'POST':
		new_name = request.form['new_name']
		menu.name = new_name
		session.add(menu)
		session.commit()
		return redirect(url_for('ShowRestaurantMenu', restaurantID = restaurant.id))

	else:
		return render_template('edit.html', restaurant = restaurant, menu = menu)

#delete menu item
@app.route('/menu/delete/<int:restaurant_id>/<int:menu_id>', methods = ['POST', 'GET'])
def DeleteMenu(restaurant_id, menu_id):
	restaurant = session.query(Restaurant).filter_by(id = restaurant_id).one()
	menu = session.query(MenuItem).filter_by(id = menu_id).one()
	if request.method == 'POST':
		option = request.form['option']
		if option == 'yes':
			session.delete(menu)
			session.commit()
			flash('Item has been deleted!')
		return redirect(url_for('ShowRestaurantMenu', restaurantID = restaurant.id))

	else:
		return render_template('delete.html', restaurant = restaurant, menu = menu)

#making API endpoint for GET request
@app.route('/restaurants/<int:restaurant_id>/menu/JSON')
def menuJSON(restaurant_id):
	restaurant = session.query(Restaurant).filter_by(id = restaurant_id).one()
	menu_items = session.query(MenuItem).filter_by(restaurant_id = restaurant_id).all()
	return jsonify(MenuItems = [i.serialize for i in menu_items])

@app.route('/restaurants/<int:restaurant_id>/menu/<int:menu_id>/JSON')
def itemJSON(restaurant_id, menu_id):
	restaurant = session.query(Restaurant).filter_by(id = restaurant_id).one()
	item = session.query(MenuItem).filter_by(id = menu_id).one()
	return jsonify(Item = item.serialize)

if __name__ == '__main__':
	app.secret_key = 'strong_password'
	app.debug = True
	app.run(host = '', port = 6969)