#import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#import dataset
URL="https://raw.githubusercontent.com/AdiPersonalWorks/Random/master/student_scores%20-%20student_scores.csv"
df=pd.read_csv(URL)
print("Data imported successfully")
#inspect the data
print(df.shape)
print(df.head())
print(df.describe())
Scores=df["Scores"]
Hours=df["Hours"]
avg_scores=Scores.mean()
avg_hours=Hours.mean()
print("On Average a student studying:", avg_hours, " will attain an average score of :", avg_scores)
#Correlation between variablesavg_hours
df.corr()
print(" Correlation between hours and scores is 0.976191")

#Graphical Visualization of Data
# Plotting the distribution of scores
df.plot(x='Hours', y='Scores',style='o')
plt.title('Hours vs Percentage Score')
plt.grid(True)
plt.xlabel('Hours Studied')
plt.ylabel('Percentage Score')
plt.show()
#Inference
print(" Positive linear relation between the number of hours studied and percentage of score.")

#Preparing Data for Modelling
X = df.iloc[:, :-1].values
y = df.iloc[:, 1].values
#Training the model
from sklearn.model_selection import train_test_split
##Split the data into train and test model
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.20,random_state=0)

#Fit the model
from sklearn.linear_model import LinearRegression
linreg = LinearRegression()
linreg.fit(X_train, y_train)
print("Training Complete")
print('Intercept: ',linreg.intercept_)

# Plotting the regression line
line = linreg.coef_*X+linreg.intercept_

# Plotting for the test data
plt.scatter(X, y)
plt.plot(X, line,color = 'green')
plt.show()
#Predict the model
print(X_test) # Testing data - In Hours
y_pred = linreg.predict(X_test) # Predicting the scores
# Comparing Actual vs Predicted
df1 = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
print(df1)
# testing with  own data
hours = 9.25
own_pred = linreg.predict([[hours]])
print("No of Hours of Study = {}".format(hours))
print("Predicted Score = {}".format(own_pred[0]))

#Evaluating the model
from sklearn import metrics
print('Mean Absolute Error:',
      metrics.mean_absolute_error(y_test, y_pred))
print("Residual sum of squares(MSE): %.2f" % np.mean((y_pred - y_test) ** 2))
MSE=np.mean((y_pred - y_test) ** 2)
print("Root mean squares error (RMSE): %.2f" % np.sqrt(MSE))
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % linreg.score(X_test, y_test))
print("Mean absolute error: %.2f" % np.mean(np.absolute(y_pred - y_test)))
print("R2-score: %.2f" % metrics.r2_score(y_pred ,y_test))
print(linreg.score(X_test,y_test)*100,'% Prediction Accuracy')


import sys

def solve():

    a = int(input())
    answer = [0] * 10001

    for i in range(a):
        b = int(sys.stdin.readline())
        answer[b] = answer[b] + 1

    for i in range(10001):
        if answer[i] != 0:
            for j in range(answer[i]):
                print(i)

solve()


from rdkit import Chem
from rdkit.Chem import Draw
from rdkit import RDLogger
import numpy as np
import copy
import random
from collections import OrderedDict
from rdkit.Chem import rdMolDescriptors as rdDesc
from .molecular_graph import *
from .molecule_state import MolState
import torch
import warnings
from rdkit.Chem.QED import qed
from rdkit.Chem import AllChem
from rdkit import DataStructs
from scipy.stats import wasserstein_distance
import pickle
from .chemprop_IR.smiles_predict import get_IR_prediction, Forward_IR
import ipdb

lg = RDLogger.logger()
lg.setLevel(RDLogger.CRITICAL)
rdBase.DisableLog('rdApp.error')
warnings.filterwarnings("ignore")

REWARD_THRESHOLD = 0.95

class Env:
    def __init__(self, molForm, targetIRSpectra, targetNMRSpectra):
        self.molForm = molForm # CNOF Molecular Formula
        self.targetSpectra = targetIRSpectra
        self.targetNMR = targetNMRSpectra
        self.state = MolState(molForm, targetNMRSpectra) # Molecular State that has all the loose atoms

    def __str__(self):
        return "Current Env State: " + str(self.state) + " MolForm: " + str(self.molForm)

    def convertStrToList(self, string):
        return [int(i) for i in string.strip('][').split(', ')]

    def reset(self, ir_train_dat, idx=74753):
        try:
            self.targetmol = ir_train_dat[idx][0]
            self.molForm = ir_train_dat[idx][1]
            self.targetSpectra = ir_train_dat[idx][2] # ir-spectra
            self.targetNMR = ir_train_dat[idx][3] # nmr-spectra
        except Exception as e:
            print('Reset error', e)
            time.sleep(20)
        self.state = MolState(self.molForm, self.targetNMR)
        #print('Finished env reset')

    def reward(self, episode_actor, state = None, target = None):
        # Need to change for the testing code
        # Would be same as terminal_reward for our usecase in IR
        try:
            if state is None:
                state = self.state
            if target is None:
                target = self.targetSpectra
            state_mol = Chem.MolToSmiles(self.state.rdmol)
            state_mol = Chem.MolFromSmiles(Chem.MolToSmiles(Chem.MolFromSmiles(state_mol, sanitize=False), kekuleSmiles=True), sanitize=False)
            target_mol = Chem.MolFromSmiles(Chem.MolToSmiles(Chem.MolFromSmiles(self.targetmol, sanitize=False), kekuleSmiles=True), sanitize=False)
            substructmatch = int(target_mol.HasSubstructMatch(state_mol))
            if state.numInRdmol < state.totalNumOfAtoms:
                return substructmatch
            return substructmatch
        except:
            return 0

    def terminal_reward(self, episode_actor, state = None, target = None):
        try:
            if state is None:
                state = self.state
            if target is None:
                target = self.targetSpectra
            mol = self.state.rdmol
            Chem.SanitizeMol(mol,Chem.SanitizeFlags.SANITIZE_FINDRADICALS|Chem.SanitizeFlags.SANITIZE_KEKULIZE|Chem.SanitizeFlags.SANITIZE_SETAROMATICITY|Chem.SanitizeFlags.SANITIZE_SETCONJUGATION|Chem.SanitizeFlags.SANITIZE_SETHYBRIDIZATION|Chem.SanitizeFlags.SANITIZE_SYMMRINGS,catchErrors=True)

            if state.numInRdmol < state.totalNumOfAtoms:
                return 0

            self.forward_model.predict_smiles(Chem.MolToSmiles(mol))
            predicted_IR = np.abs(np.array(predicted_IR))

            reward = 1 - wasserstein_distance(predicted_IR, target)

            return 2*(reward-0.5)

        except Exception as e:
            print('Terminal reward error', e)
            return 0

    def invalidAction(self):
        raise Exception("Invalid Action has been chosen :(")

    def isTerminal(self, episode_actor, state: MolState = None):

        if state is None:
            state = self.state

        if sum(state.valid_actions()) == 0:
            return True

        if state.numInRdmol < state.totalNumOfAtoms:
            return False

        # target_ir = self.targetSpectra

        # mol = deepcopy(self.state.rdmol)
        # Chem.SanitizeMol(mol)

        state_mol = self.state.rdmol
        target_mol = Chem.MolFromSmiles(self.targetmol,sanitize=False)
        substructmatch = int(target_mol.HasSubstructMatch(state_mol))

        # if the present state is the target molecule
        if Chem.MolToSmiles(state_mol, canonical=True) == Chem.MolToSmiles(target_mol, canonical=True):
            return True

        # if the present state is a substructure of the target
        if substructmatch > 0:
            return False
        else:
            return True

        if self.terminal_reward(episode_actor) > REWARD_THRESHOLD:
            return True
        else:
            return False

    def step(self,  actionInt: int, episode_actor, state: MolState = None):
        if state is None:
            state = self.state

        valid_actions = state.action_mask
        if valid_actions[actionInt] == 0:
            action = state._actionIntToList(actionInt)
            #print(state._actionIntToList(actionInt))
            #print(state)

            return self.invalidAction()

        state.doStep(state._actionIntToList(actionInt))
        terminal = self.isTerminal(episode_actor, state)
        _ = state.valid_actions()
        reward = self.reward(episode_actor, state)
        if terminal:
            return state, reward,terminal
        else:
            return state, reward,terminal


"""Rebuilding broken crowdsale contracts."""
import csv
import datetime
from io import StringIO

import pytest
from eth_utils import to_wei
from ethereum.tester import TransactionFailed
from web3.contract import Contract

from ico.tests.utils import time_travel
from ico.state import CrowdsaleState

@pytest.fixture
def token(empty_token):
    """Get a token with 0 initial issuance."""
    return empty_token

@pytest.fixture
def new_token(chain, team_multisig, token_name, token_symbol) -> Contract:
    """Get another token contract with 0 initial issuance."""

    args = [token_name, token_symbol, 0, 0, True]  # Owner set

    tx = {
        "from": team_multisig
    }

    contract, hash = chain.provider.deploy_contract('CrowdsaleToken', deploy_args=args, deploy_transaction=tx)
    return contract

@pytest.fixture
def start_time() -> int:
    """Start Apr 15th"""
    return int((datetime.datetime(2017, 4, 15, 16, 00) - datetime.datetime(1970, 1, 1)).total_seconds())

@pytest.fixture
def end_time(start_time) -> int:
    """Run 4 weeks."""
    return start_time + 4 * 7 * 24 * 3600

@pytest.fixture
def minimum_funding_goal() -> int:
    """What is our minimum funding goal."""
    return to_wei(7500, "ether")

@pytest.fixture
def cap() -> int:
    """What is our maximum tokens sold capacity."""
    return 4000000

@pytest.fixture
def sample_data(customer, customer_2):
    data = """
Address,Payment at,Tx hash,Tx index,Invested ETH,Received tokens
{},2017-04-13T16:01:46+00:00,0xf88780859cfde239e5898d036b685f5358d4b0a0f82e8cce26403c782f8a2e52,1,0.505,561
{},2017-04-13T16:02:38+00:00,0x1385320b9d693afad1dce05cb0f9c8c3c1bc017668d32ee2b69d4039fdaf5983,3,0.1,111
    """.strip().format(customer, customer_2)
    return list(csv.DictReader(StringIO(data)))

@pytest.fixture
def success_sample_data(customer, customer_2):
    """Enough to break the minimum funding goal"""
    data = """
Address,Payment at,Tx hash,Tx index,Invested ETH,Received tokens
{},2017-04-13T16:01:46+00:00,0xf88780859cfde239e5898d036b685f5358d4b0a0f82e8cce26403c782f8a2e52,1,0.505,561
{},2017-04-13T16:02:38+00:00,0x1385320b9d693afad1dce05cb0f9c8c3c1bc017668d32ee2b69d4039fdaf5983,3,0.1,111
{},2017-04-13T16:02:38+00:00,0x1385320b9d693afad1dce05cb0f9c8c3c1bc017668d32ee2b69d4039fdaf5984,4,7500,1111
    """.strip().format(customer, customer_2, customer_2)
    return list(csv.DictReader(StringIO(data)))

@pytest.fixture
def founder_allocation() -> float:
    """How much tokens are allocated to founders, etc."""
    return 0.2

def set_finalizer(chain, token, crowdsale, team_multisig, founder_allocation) -> Contract:
    # Create finalizer contract
    args = [
        token.address,
        crowdsale.address,
        int(founder_allocation * 10000),  # 20% for founders, bounties, etc.
        team_multisig,  # Who gets extra tokens
    ]
    contract, hash = chain.provider.deploy_contract('BonusFinalizeAgent', deploy_args=args)

    # Allow finalzier to do mint()
    token.transact({"from": team_multisig}).setMintAgent(contract.address, True)
    assert token.call().mintAgents(contract.address) == True

    token.transact({"from": team_multisig}).setReleaseAgent(contract.address)
    crowdsale.transact({"from": team_multisig}).setFinalizeAgent(contract.address)
    return contract

@pytest.fixture
def milestone_pricing(chain, start_time, end_time):
    """Milestone pricing, do not set presale collection contract."""

    week = 24*3600 * 7

    args = [
        [
            start_time + 0, to_wei("0.0009", "ether"),
            start_time + week*1, to_wei("0.0009", "ether"),
            end_time, 0,
        ],
    ]

    tx = {
        "gas": 4000000
    }
    contract, hash = chain.provider.deploy_contract('MilestonePricing', deploy_args=args, deploy_transaction=tx)
    return contract

@pytest.fixture
def original_crowdsale(chain, team_multisig, start_time, end_time, milestone_pricing, preico_cap, minimum_funding_goal, cap, token, founder_allocation) -> Contract:
    """Crowdsale that we are going to relaunch.

    This will reserve the release agent for the original crowdsale.
    """

    args = [
        token.address,
        milestone_pricing.address,
        team_multisig,
        start_time,
        end_time,
        minimum_funding_goal,
        cap
    ]

    tx = {
        "from": team_multisig,
    }

    contract, hash = chain.provider.deploy_contract('MintedTokenCappedCrowdsale', deploy_args=args, deploy_transaction=tx)

    assert contract.call().owner() == team_multisig
    assert not token.call().released()
    assert contract.call().maximumSellableTokens() == cap

    # Allow crowdsale contract to do mint()
    token.transact({"from": team_multisig}).setMintAgent(contract.address, True)
    assert token.call().mintAgents(contract.address) == True

    set_finalizer(chain, token, contract, team_multisig, founder_allocation)

    return contract

@pytest.fixture
def relaunched_crowdsale(chain, team_multisig, start_time, end_time, milestone_pricing, preico_cap, minimum_funding_goal, cap, original_crowdsale, new_token, founder_allocation) -> Contract:
    """Create a crowdsale with fixed contracts."""

    args = [
        new_token.address,
        milestone_pricing.address,
        team_multisig,
        start_time,
        end_time,
        minimum_funding_goal,
        cap
    ]

    tx = {
        "from": team_multisig,
    }

    contract, hash = chain.provider.deploy_contract('RelaunchedCrowdsale', deploy_args=args, deploy_transaction=tx)

    assert contract.call().owner() == team_multisig
    assert not new_token.call().released()
    assert contract.call().maximumSellableTokens() == cap

    # Allow crowdsale contract to do mint()
    new_token.transact({"from": team_multisig}).setMintAgent(contract.address, True)
    assert new_token.call().mintAgents(contract.address) == True
    set_finalizer(chain, new_token, contract, team_multisig, founder_allocation)

    return contract

def test_rebuild_failed_crowdsale_with_new_token(chain, original_crowdsale, token, new_token, relaunched_crowdsale, sample_data, team_multisig, customer, customer_2):
    """Rebuild a crowdsale that is not going to reach its minimum goal."""

    time_travel(chain, original_crowdsale.call().startsAt() + 1)
    assert original_crowdsale.call().getState() == CrowdsaleState.Funding
    assert relaunched_crowdsale.call().getState() == CrowdsaleState.Funding

    for data in sample_data:
        addr = data["Address"]
        wei = to_wei(data["Invested ETH"], "ether")
        original_crowdsale.transact({"from": addr, "value": wei}).buy()

    # We have a confirmation hash
    events = original_crowdsale.pastEvents("Invested").get()
    assert len(events) == 2
    e = events[-1]

    # Import old transactions from the multisig contract
    for data in sample_data:
        addr = data["Address"]
        wei = to_wei(data["Invested ETH"], "ether")
        tokens = int(data["Received tokens"])
        orig_txid = int(data["Tx hash"], 16)
        orig_tx_index = int(data["Tx index"])
        relaunched_crowdsale.transact({"from": team_multisig}).setInvestorDataAndIssueNewToken(addr, wei, tokens, orig_txid)

    # No double issuance for the same tx
    data = sample_data[0]
    addr = data["Address"]
    wei = to_wei(data["Invested ETH"], "ether")
    tokens = int(data["Received tokens"])
    orig_txid = int(data["Tx hash"], 16)
    orig_tx_index = int(data["Tx index"])
    with pytest.raises(TransactionFailed):
        relaunched_crowdsale.transact({"from": team_multisig}).setInvestorDataAndIssueNewToken(addr, wei, tokens, orig_txid)

    # Compare that both crowdsales and tokens look the same
    assert original_crowdsale.call().tokensSold() == relaunched_crowdsale.call().tokensSold()
    assert original_crowdsale.call().investedAmountOf(customer) == relaunched_crowdsale.call().investedAmountOf(customer)
    assert original_crowdsale.call().investedAmountOf(customer_2) == relaunched_crowdsale.call().investedAmountOf(customer_2)

    assert token.call().balanceOf(customer) == relaunched_crowdsale.call().tokenAmountOf(customer)
    assert token.call().balanceOf(customer_2) == relaunched_crowdsale.call().tokenAmountOf(customer_2)
    assert token.call().balanceOf(customer) == new_token.call().balanceOf(customer)
    assert token.call().balanceOf(customer_2) == new_token.call().balanceOf(customer_2)

    assert token.call().totalSupply() == new_token.call().totalSupply()

    time_travel(chain, original_crowdsale.call().endsAt() + 1)

    assert original_crowdsale.call().getState() == CrowdsaleState.Failure
    assert relaunched_crowdsale.call().getState() == CrowdsaleState.Failure
    relaunched_crowdsale.transact({"from": team_multisig, "value": to_wei(30, "ether")}).loadRefund()
    assert relaunched_crowdsale.call().getState() == CrowdsaleState.Refunding

    relaunched_crowdsale.transact({"from": customer}).refund()
    relaunched_crowdsale.transact({"from": customer_2}).refund()

    # No double refund
    with pytest.raises(TransactionFailed):
        relaunched_crowdsale.transact({"from": customer}).refund()

    with pytest.raises(TransactionFailed):
        original_crowdsale.transact({"from": customer}).refund()

def test_rebuild_success_crowdsale_with_new_token(chain, new_token, relaunched_crowdsale, success_sample_data, team_multisig, customer, customer_2):
    """Rebuild a crowdsale that reaches its minimum goal."""

    time_travel(chain, relaunched_crowdsale.call().startsAt() + 1)
    assert relaunched_crowdsale.call().getState() == CrowdsaleState.Funding

    # Import old transactions from the multisig contract
    for data in success_sample_data:
        addr = data["Address"]
        wei = to_wei(data["Invested ETH"], "ether")
        tokens = int(data["Received tokens"])
        orig_txid = int(data["Tx hash"], 16)
        # orig_tx_index = int(data["Tx index"])
        relaunched_crowdsale.transact({"from": team_multisig}).setInvestorDataAndIssueNewToken(addr, wei, tokens, orig_txid)

        assert relaunched_crowdsale.call().getRestoredTransactionStatus(orig_txid)

    # Compare that both crowdsales and tokens look the same
    assert relaunched_crowdsale.call().investorCount() == 2

    # We restored 3 events
    events = relaunched_crowdsale.pastEvents("RestoredInvestment").get()
    assert len(events) == 3

    assert new_token.call().balanceOf(customer_2) == 1222
    assert new_token.call().balanceOf(customer) == relaunched_crowdsale.call().tokenAmountOf(customer)
    assert new_token.call().balanceOf(customer_2) == relaunched_crowdsale.call().tokenAmountOf(customer_2)
    assert new_token.call().totalSupply() == relaunched_crowdsale.call().tokensSold()

    time_travel(chain, relaunched_crowdsale.call().endsAt() + 1)

    assert relaunched_crowdsale.call().getState() == CrowdsaleState.Success

    before_final = new_token.call().totalSupply()
    relaunched_crowdsale.transact({"from": team_multisig}).finalize()
    assert new_token.call().totalSupply() == int(before_final * 1.20)

    assert new_token.call().released()


import numpy as np
import pandas as pd

'''导入数据'''

predict_target = pd.read_excel('D:\研究生课程\研究生案例大赛\实验\ecommercedata-预测.xlsx',index_col=0)
data = pd.read_excel('D:\研究生课程\研究生案例大赛\实验\cosinfo\CustomerInfo_3.xlsx', index_col=0)

'''寻找测试集内没有在之前购买过商品的客户'''
predict_target_set = set()
data_set = set()
for i in list(predict_target.index):
    predict_target_set.add(i)
for j in list(data.index):
    data_set.add(j)

predict_difference = predict_target_set.difference(data_set)
print(predict_difference)

predict_target_drop = list(predict_difference)
predict_target = predict_target.drop(list(predict_difference), axis=0)

'''寻找测试集内没有在之前购买过商品的客户'''
predict_target_set = set()
data_set = set()
for i in list(predict_target.index):
    predict_target_set.add(i)
for j in list(data.index):
    data_set.add(j)

predict_difference = predict_target_set.difference(data_set)
print(predict_difference)

'''寻找最近的K个邻居顾客'''
'''定义距离函数'''
# 定义欧氏距离
def euSimilar(a,b):
    dist = np.linalg.norm(a - b)
    return 1 / (dist+0.0001)

# 定义余弦相似度
def cos_sim(a, b):
    a_norm = np.linalg.norm(a)
    b_norm = np.linalg.norm(b)
    cos = np.dot(a,b)/(a_norm * b_norm)
    return cos

# 寻找最相似的K个客户
SimilarCos = dict() # 创建一个字典存储最相似的客户与得分
for i in list(predict_target.index):
    distance_list = [0 for h in range(5)]
    SimilarCos_list = [0 for k in range(5)]
    for j in list(data.index.drop(i)):
        x = np.array(data.loc[i])
        y = np.array(data.loc[j])
        distance = cos_sim(x, y) # 计算余弦相似度
        if distance > min(distance_list):
            a = distance_list.index(min(distance_list))
            distance_list[a] = distance
            SimilarCos_list[a] = j
        else:
            continue

    Total_Simsocre = np.array(distance_list, dtype='float64').sum()  # 对相似度得分进行求和
    SimScore = [k/Total_Simsocre for k in distance_list] # 求得平均相似度得分

    SimilarCos[i] = [SimilarCos_list, SimScore]

print(SimilarCos)

# 将最相似顾客与其得分写入Excel表格中
rowname=list()
for headers in SimilarCos.keys():
    rowname.append(headers)
SimilarCos_Frame = pd.DataFrame(index=rowname, columns=['Customer1', 'Customer2', 'Customer3', 'Customer4', 'Customer5',
                                                        'Score1', 'Score2','Score3', 'Score4', 'Score5' ]).fillna(0)
for keys in SimilarCos.keys():
    list1 = SimilarCos[keys][0]+list(SimilarCos[keys][1])
    SimilarCos_Frame.loc[keys] = list1

print(SimilarCos_Frame)

'''执行推荐算法'''
'''将数据集进行标准化（逐列）'''
for i in list(data.columns):
    Mean = np.mean(data.loc[:, i])
    Std = np.std(data.loc[:, i])
    if Std != 0:
        data.loc[:, i] = (data.loc[:, i]-Mean)/Std
    else:
        continue
print(data)

'''使用推荐算法'''
# 算法思路，将最接近的K个客户所购买的商品数量标准分数，以余弦相似度为权重，计算加权平均数作为指数得分，最终得分最高的n类商品为推荐商品。
# 使用推荐评分算法
CosScore = pd.DataFrame(index=predict_target.index, columns=data.columns).fillna(0)
for i in list(SimilarCos_Frame.index):
    s1 = SimilarCos_Frame.loc[i, list(SimilarCos_Frame.columns)[0:5]] # 用来保存最相似客户的ID
    s2 = SimilarCos_Frame.loc[i, list(SimilarCos_Frame.columns)[5:]] # 用来保存最相似客户对应的相似度得分
    ScoreList = np.array(list(0 for i in range(len(CosScore.columns))), dtype='float64') # 用来保存相似度得分列表
    Total_s2 = 0 # 计算余弦相似度加权使用

    for j in range(len(s1)):
        if s1[j] != 0 and s2[j] != 0:
            c = np.array(data.loc[s1[j]], dtype='float64')*s2[j] # 以余弦相似度作为权重 计算指数评分
        ScoreList += c
        Total_s2 += np.array(s2[j])
    CosScore.loc[i] = ScoreList/Total_s2

# 进行推荐商品
recommend_frame = pd.DataFrame(index=CosScore.index,columns=['Stock{}'.format(i for i in range(1,11))]).fillna(0)
for i in list(CosScore.index):
    Stock_score = CosScore.loc[i]
    Stock_score = Stock_score.sort_values(ascending=False)
    for k in range(1, 11):
        recommend_frame.loc[i, 'Stock{}'.format(k)] = Stock_score.index[k-1]

print(recommend_frame)

recommend_frame.to_csv('推荐商品_Predict.csv')


from sqlalchemy import Column, ForeignKey, Integer, String, Text
from sqlalchemy.orm import relationship

from core.db import Base

class FlashCard(Base):
    __tablename__ = "api_flash_cards"

    id = Column(Integer, primary_key=True, index=True, unique=True)
    title = Column(String(255))
    front = Column(Text, nullable=True)
    front_image_url = Column(String(255), nullable=True)
    back = Column(Text, nullable=True)
    back_image_url = Column(String(255), nullable=True)

    theme = Column(Integer, ForeignKey('api_themes.id'))
    theme_id = relationship("Theme")

    user = Column(Integer, ForeignKey('api_users.id'))
    user_id = relationship("User")


"""
# 推荐系统模型
"""
import os
import logging
import math
import paddle
import paddle.nn as nn
import paddle.nn.functional as F

logger = logging.getLogger(__name__)

class MovieRecommendModel(nn.Layer):
    def __init__(self, use_poster, use_mov_title, use_mov_cat, use_age_job,
                 vocab_sizes={}, fc_sizes=[]):
        super(MovieRecommendModel, self).__init__()

        # 将传入的name信息和bool型参数添加到模型类中
        self.use_mov_poster = use_poster  # 计算电影信息特征时是否使用海报, 现模型不使用
        self.use_mov_title = use_mov_title
        self.use_usr_age_job = use_age_job
        self.use_mov_cat = use_mov_cat
        self.fc_sizes = fc_sizes

        usr_embedding_dim = 32
        gender_embeding_dim = 16
        age_embedding_dim = 16

        job_embedding_dim = 16
        mov_embedding_dim = 16
        category_embedding_dim = 16
        title_embedding_dim = 32

        logger.info("define network layer for embedding usr info")
        USR_ID_NUM = vocab_sizes["max_usr_id"] + 1
        # 对用户ID做映射，并紧接着一个Linear层
        self.usr_emb = nn.Embedding(num_embeddings=USR_ID_NUM, embedding_dim=usr_embedding_dim, sparse=False)
        self.usr_fc = nn.Linear(in_features=usr_embedding_dim, out_features=32)

        # 对用户性别信息做映射，并紧接着一个Linear层
        USR_GENDER_DICT_SIZE = 2
        self.usr_gender_emb = nn.Embedding(num_embeddings=USR_GENDER_DICT_SIZE, embedding_dim=gender_embeding_dim)
        self.usr_gender_fc = nn.Linear(in_features=gender_embeding_dim, out_features=16)

        # 对用户年龄信息做映射，并紧接着一个Linear层
        USR_AGE_DICT_SIZE = vocab_sizes["max_usr_age"] + 1  # +1 考虑0自然数
        self.usr_age_emb = nn.Embedding(num_embeddings=USR_AGE_DICT_SIZE, embedding_dim=age_embedding_dim)
        self.usr_age_fc = nn.Linear(in_features=age_embedding_dim, out_features=16)

        # 对用户职业信息做映射，并紧接着一个Linear层
        USR_JOB_DICT_SIZE = vocab_sizes["max_usr_job"] + 1
        self.usr_job_emb = nn.Embedding(num_embeddings=USR_JOB_DICT_SIZE, embedding_dim=job_embedding_dim)
        self.usr_job_fc = nn.Linear(in_features=job_embedding_dim, out_features=16)

        # 新建一个Linear层，用于整合用户数据信息
        self.usr_combined = nn.Linear(in_features=80, out_features=200)

        logger.info("define network layer for embedding usr info")
        MOV_DICT_SIZE = vocab_sizes["max_mov_id"] + 1
        self.mov_emb = nn.Embedding(num_embeddings=MOV_DICT_SIZE, embedding_dim=mov_embedding_dim)
        self.mov_fc = nn.Linear(in_features=mov_embedding_dim, out_features=32)

        # 对电影类别做映射
        CATEGORY_DICT_SIZE = vocab_sizes["movie_category_size"] + 1  # +1 考虑添加0类别
        self.mov_cat_emb = nn.Embedding(num_embeddings=CATEGORY_DICT_SIZE,
                                        embedding_dim=category_embedding_dim,
                                        sparse=False)
        self.mov_cat_fc = nn.Linear(in_features=category_embedding_dim, out_features=32)

        # 对电影名称做映射
        MOV_TITLE_DICT_SIZE = vocab_sizes["movie_title_size"] + 1
        self.mov_title_emb = nn.Embedding(num_embeddings=MOV_TITLE_DICT_SIZE,
                                          embedding_dim=title_embedding_dim,
                                          sparse=False)
        self.mov_title_conv = nn.Conv2D(in_channels=1, out_channels=1,
                                        kernel_size=(3, 1), stride=(2, 1), padding=0)
        self.mov_title_conv2 = nn.Conv2D(in_channels=1, out_channels=1,
                                         kernel_size=(3, 1), stride=1, padding=0)

        # 新建一个Linear层，用于整合电影特征
        self.mov_concat_embed = nn.Linear(in_features=96, out_features=200)

        # 增加全连接层深度提取特征
        user_sizes = [200] + self.fc_sizes
        acts = ["relu" for _ in range(len(self.fc_sizes))]
        self._user_layers = []
        for i in range(len(self.fc_sizes)):
            linear = nn.Linear(
                in_features=user_sizes[i],
                out_features=user_sizes[i + 1],
                weight_attr=paddle.ParamAttr(
                    initializer=paddle.nn.initializer.Normal(
                        std=1.0 / math.sqrt(user_sizes[i]))))
            self.add_sublayer("linear_user_%d" % i, linear)
            self._user_layers.append(linear)
            if acts[i] == "relu":
                act = nn.ReLU()
                self.add_sublayer("user_act_%d" % i, act)
                self._user_layers.append(act)

        # 电影特征和用户特征使用了不同的全连接层，不共享参数
        movie_sizes = [200] + self.fc_sizes
        acts = ["relu" for _ in range(len(self.fc_sizes))]
        self._movie_layers = []
        for i in range(len(self.fc_sizes)):
            linear = nn.Linear(
                in_features=movie_sizes[i],
                out_features=movie_sizes[i + 1],
                weight_attr=paddle.ParamAttr(
                    initializer=paddle.nn.initializer.Normal(
                        std=1.0 / math.sqrt(movie_sizes[i]))))
            self.add_sublayer("linear_movie_%d" % i, linear)
            self._movie_layers.append(linear)
            if acts[i] == "relu":
                act = nn.ReLU()
                self.add_sublayer("movie_act_%d" % i, act)
                self._movie_layers.append(act)

    def get_usr_feature(self, usr_var):
        """定义计算用户特征的前向运算过程"""
        # 获取到用户数据
        usr_id, usr_gender, usr_age, usr_job = usr_var
        # 将用户的ID数据经过embedding和Linear计算，得到的特征保存在feats_collect中
        feats_collect = []

        usr_id_feat = F.relu(self.usr_fc(self.usr_emb(usr_id)))
        feats_collect.append(usr_id_feat)

        # 计算用户的性别特征，并保存在feats_collect中
        usr_gender_feat = F.relu(self.usr_gender_fc(self.usr_gender_emb(usr_gender)))
        feats_collect.append(usr_gender_feat)

        # 选择是否使用用户的年龄-职业特征
        if self.use_usr_age_job:
            usr_age_feat = F.relu(self.usr_age_fc(self.usr_age_emb(usr_age)))
            feats_collect.append(usr_age_feat)

            usr_job_feat = F.relu(self.usr_job_fc(self.usr_job_emb(usr_job)))
            feats_collect.append(usr_job_feat)

        # 将用户的特征级联，并通过Linear层得到最终的用户特征
        usr_feat = paddle.concat(feats_collect, axis=-1)
        user_features = F.tanh(self.usr_combined(usr_feat))

        # 通过3层全链接层，获得用于计算相似度的用户特征和电影特征
        for n_layer in self._user_layers:
            user_features = n_layer(user_features)

        return user_features

    def get_movie_feature(self, mov_var):
        """定义电影特征的前向计算过程"""
        # 获得电影数据
        mov_id, mov_cat, mov_title, mov_poster = mov_var
        feats_collect = []
        # 获得batchsize的大小
        batch_size = mov_id.shape[0]
        # 计算电影ID的特征，并存在feats_collect中
        mov_id_feat = F.relu(self.mov_fc(self.mov_emb(mov_id)))
        feats_collect.append(mov_id_feat)

        # 如果使用电影的种类数据，计算电影种类特征的映射
        if self.use_mov_cat:
            # 计算电影种类的特征映射，对多个种类的特征求和得到最终特征
            mov_cat_feat = self.mov_cat_emb(mov_cat)
            mov_cat_feat = paddle.sum(mov_cat_feat, axis=1, keepdim=False)  # shape: [btc, feature_size]
            mov_cat_feat = F.relu(self.mov_cat_fc(mov_cat_feat))
            feats_collect.append(mov_cat_feat)

        if self.use_mov_title:
            mov_title_feat = self.mov_title_emb(mov_title)
            mov_title_feat = self.mov_title_conv2(F.relu(self.mov_title_conv(mov_title_feat)))
            mov_title_feat = paddle.sum(mov_title_feat, axis=2, keepdim=False)  # shape: [bat, 1, feature_size]
            mov_title_feat = F.relu(mov_title_feat)
            mov_title_feat = paddle.reshape(mov_title_feat, [batch_size, -1])
            feats_collect.append(mov_title_feat)

        # 使用一个全连接层，整合所有电影特征，映射为一个200维的特征向量
        mov_feat = paddle.concat(feats_collect, axis=-1)
        mov_features = F.tanh(self.mov_concat_embed(mov_feat))
        # 继续使用深度网络提取特征
        for n_layer in self._movie_layers:
            mov_features = n_layer(mov_features)

        return mov_features

    def forward(self, usr_var, mov_var):
        """定义个性化推荐算法的前向计算"""
        # 计算用户特征和电影特征
        user_features = self.get_usr_feature(usr_var)
        mov_features = self.get_movie_feature(mov_var)

        # 根据计算的特征计算相似度, reshape方便loss计算
        sim = F.common.cosine_similarity(user_features, mov_features).reshape([-1, 1])
        # 使用余弦相似度算子，计算用户和电影的相似程度
        # sim = F.cosine_similarity(user_features, mov_features, axis=1).reshape([-1, 1])
        # 将相似度扩大范围到和电影评分相同数据范围
        res = paddle.scale(sim, scale=5)

        return user_features, mov_features, res


def define_spotsignal(self):
        """
        Identify the "expected" flux value at the time of each observation based on the
        Kepler long-cadence data, to ensure variations observed are not the effects of a single
        large starspot. Only works if the target star was targeted for long or short cadence
        observations during the primary mission.
        """
        client = kplr.API()
        star = client.star(self.kic)

        lcs = star.get_light_curves(short_cadence=False)
        time, flux, ferr, qual = [], [], [], []
        for lc in lcs:
            with lc.open() as f:
                hdu_data = f[1].data
                time.append(hdu_data["time"])
                flux.append(hdu_data["pdcsap_flux"])
                ferr.append(hdu_data["pdcsap_flux_err"])
                qual.append(hdu_data["sap_quality"])
            tout = np.array([])
            fout = np.array([])
            eout = np.array([])
            for i in range(len(flux)):
                t = time[i][qual[i] == 0]
                f = flux[i][qual[i] == 0]
                e = ferr[i][qual[i] == 0]

                t = t[np.isfinite(f)]
                e = e[np.isfinite(f)]
                f = f[np.isfinite(f)]

                e /= np.median(f)
                f /= np.median(f)
                tout = np.append(tout, t[50:]+54833)
                fout = np.append(fout, f[50:])
                eout = np.append(eout, e[50:])

            self.spot_signal = np.zeros(52)

            for i in range(len(self.times)):
                if self.times[i] < 55000:
                    self.spot_signal[i] = 1.0
                else:
                    self.spot_signal[i] = fout[np.abs(self.times[i] - tout) == np.min(np.abs(self.times[i] - tout))]


import abc

import torch

class AttackInstance:
    """This class is subclassed by each AttackInstance, abstracing away the underlying attack.

    Parameters
    ----

    model: nn.Module
       The model for which the adversarial examples are optimised

    args: argparse.Namespace
        The arguments passed to the program, which are used to determine the attack parameters
    """

    def __init__(self, model, args):
        self.model = model
        self.args = args

    @abc.abstractmethod
    def generate_attack(self, batch):
        """An abstract function, which when implemented takes in a batch, and applies an adversarial perturbation to the datapoints

        Parameters
        ----
        batch: (Tensor,Tensor)
           Takes in a typical tuple (xs,ys) of datapoints and their labels

        Returns
        ---
        perturbed batch: (Tensor,Tensor)
           This returns (adv(xs),ys) where adv denotes our adversarial examples"""
        return None

def tensor_clamp(x, a_min, a_max):
    """
    like torch.clamp, except with bounds defined as tensors
    """
    out = torch.clamp(x - a_max, max=0) + a_max
    out = torch.clamp(out - a_min, min=0) + a_min
    return out

def normalize_l2(x):
    """
    Expects x.shape == [N, C, H, W]
    """
    norm = torch.norm(x.view(x.size(0), -1), p=2, dim=1)

    for i in range(1, len(x.shape)):
        norm = norm.unsqueeze(-1)

    return x / (norm + 1e-8)

def tensor_clamp_l2(x, center, radius):
    """batched clamp of x into l2 ball around center of given radius"""
    x = x.data
    diff = x - center
    diff_norm = torch.norm(diff.view(diff.size(0), -1), p=2, dim=1)
    project_select = diff_norm > radius
    if project_select.any():
        for _ in range(0, len(x.shape) - 1):
            diff_norm = diff_norm.unsqueeze(-1)

        new_x = x
        new_x[project_select] = (center + (diff / diff_norm) * radius)[project_select]
        return new_x
    else:
        return x

def cwloss(outputs, targets, k=None):
    # This is the CW loss function, which is a maximisation of the margin between the correct class and the incorrect class
    # This is the loss function used in the paper
    # https://arxiv.org/abs/1608.04644
    #

    outputs_sorted = torch.argsort(outputs, dim=1)
    predictions = outputs_sorted[:, -1]
    correct_predictions = targets == predictions
    margin_targets = predictions.clone()
    margin_targets[correct_predictions] = outputs_sorted[:, -2][correct_predictions]

    loss = outputs[:, targets] - outputs[:, margin_targets]

    if k is not None:
        loss = torch.max(loss, -k)

    return torch.mean(loss)


import random

print("HEJ OCH VÄLKOMMEN TILL HÄNGA GUBBE!")
print("-----------------------------------")

answerlist = ["stefan" , "lazar" , "haris" ]

random.shuffle(answerlist)

answer = list(answerlist[0])

display = []

display.extend(answer)

for i in range (len(display)):
    display[i] = "_"

print(" ".join(display))
print()

count = 0

while count < len(answer):
    guess = input("gissa en bokstav: ")
    guess = guess.lower()
    print (count)

    for i in range (len(answer)):

        if answer[i] == guess :
            display[i] = guess
            count = count + 1

    print(' '.join(display))
    print()

print("Bra, du gissade rätt ord")


def apply_update(self, value, index):
        """
            Record an opendnp3 data value (Analog, Binary, etc.) in the outstation's database.

            The data value gets sent to the Master as a side-effect.

        :param value: An instance of Analog, Binary, or another opendnp3 data value.
        :param index: (integer) Index of the data definition in the opendnp3 database.
        """
        _log.debug('Recording {} measurement, index={}, value={}'.format(type(value).__name__, index, value.value))
        builder = asiodnp3.UpdateBuilder()
        builder.Update(value, index)
        update = builder.Build()
        OutstationApplication.get_outstation().Apply(update)


"""
This small stub executes a command to get list of top 5
programs currently running with the longest time .

"""

import os
import sys
import subprocess
import logging

logging.basicConfig(level=logging.INFO, format='%(process)d-%(levelname)s-%(message)s')

try:
    if sys.platform.startswith("win"):
        logging.info("Windows Operating System found..")
        POWERSHELL_PROCESS = subprocess.Popen(["powershell", "Get-Process | select starttime,ProcessName,CPU,PM | Sort starttime -Desc"], stdout=subprocess.PIPE)
        RESULT = POWERSHELL_PROCESS.communicate()[0]
        print(RESULT)

    elif sys.platform.startswith("lin"):
        logging.info("Linux Operating System found..")
        COMMAND = "ps -o pid=,user=,vsz=,command=,%cpu=,%mem,time -e | sort -nrk 3,3 | head -n 7"
        RESULT = os.system(COMMAND)

except Exception as exception:
    logging.error("Error!", exc_info=True)


import re
import inspect
def __avg_consume__(*args,**kwargs):
    fn = "$" + inspect.stack()[1][3]
    if args.__len__() ==1:
        return __create_item__(fn,args[0])
    elif args.__len__()>1:
        ret = {
            fn: []
        }
        for item in args:
            ret[fn].append(__get_field_expr__(item))
        return __create__(ret)
def __get_field_expr__(x):
    import pydocs
    if isinstance(x,pydocs.Fields):
        if x.__tree__==None:
            return "$"+x.__name__
        else:
            return x.__tree__
    else:
        return x
def __create__(expr):
    import pydocs
    ret = pydocs.Fields()
    setattr(ret, "__tree__", expr)
    return ret
def __create_array__(fn,*args):
    ret = {
        fn: []
    }
    for item in args:
        ret[fn].append(__get_field_expr__(item))
    return __create__(ret)
def __comsume_with_array__(*args):
    fn= "$"+inspect.stack()[1][3]

    ret = {
        fn: []
    }
    for item in args:
        ret[fn].append(__get_field_expr__(item))
    return __create__(ret)
def __create_item__(fn,item):
    ret = {
        fn: __get_field_expr__(item)
    }
    return __create__(ret)
def __comsume_with_item__(item):
    fn = "$" + inspect.stack()[1][3]

    ret = {
        fn: __get_field_expr__(item)
    }
    return __create__(ret)

def regex(field, partern, ignore_case=True):
    import pydocs
    if  isinstance(field,pydocs.Fields):
        if ignore_case:
            field.__tree__={
                field.__name__:{
                    "$regex":re.compile(partern,re.IGNORECASE)
                }
            }
            return field
        else:
            field.__tree__ = {
                field.__name__: {
                    "$regex": re.compile(partern)
                }
            }
            return field
def concat(*args,**kwargs):
    import pydocs
    expr = {
        "$concat": []
    }
    for item in args:
        if isinstance(item, pydocs.Fields):
            expr["$concat"].append(__get_field_expr__(item))
        else:
            expr["$concat"].append(item)
    ret = pydocs.Fields()
    setattr(ret, "__tree__", expr)
    return ret
def iif(test,true_case,flase_case):
    """
    { $cond: { if: <boolean-expression>, then: <true-case>, else: <false-case-> } }
    :param test:
    :param true_case:
    :param flase_case:
    :return:
    """
    import pydocs
    expr={
        "$cond":{
            "if":__get_field_expr__(test),
            "then":__get_field_expr__(true_case),
            "else":__get_field_expr__(flase_case)
        }
    }
    ret = pydocs.Fields()
    setattr(ret, "__tree__", expr)
    return ret
def case(test,result):
    """
     { case: <expression>, then: <expression> }
    :param test:
    :param result:
    :return:
    """
    import pydocs
    expr = {
        "case":__get_field_expr__(test),
        "then":__get_field_expr__(result)

    }
    ret = pydocs.Fields()
    setattr(ret, "__tree__", expr)
    return ret
def switch(*args,**kwargs):
    """
    $switch: { branches: [{ case: <expression>, then: <expression> },{ case: <expression>, then: <expression> },..],default: <expression>}
    :param args:
    :param kwargs:
    :return:
    """
    import pydocs
    branches=[]
    default=__get_field_expr__(args[args.__len__()-1])
    # default = __get_field_expr__(args[args.__len__() - 1])
    # list_of_args = [x for x in args if args.index(x) < args.__len__() - 1]
    # for item in list_of_args:
    #     if isinstance(item,pydocs.Fields):
    #         branches.append(item.to_mongodb())
    #     else:
    #         branches.append(item)
    m = args.__len__()
    for i in pyrange(0,m-1,1):
        item  = args[i]
        if isinstance(item,pydocs.Fields):
            branches.append(item.to_mongodb())
        else:
            branches.append(item)
    #     branches.append(__get_field_expr__(getattr(args[i],"__tree__")))

    expr={
        "$switch":{
            "branches":branches,
            "default":default
        }
    }
    ret = pydocs.Fields()
    setattr(ret, "__tree__", expr)
    return ret
def abs(expr):
    return __create__({
        "$abs":__get_field_expr__(expr)
    })
def addToSet(expr):
    return __create__({
        "$addToSet": __get_field_expr__(expr)
    })
def allElementsTrue(*args):
    ret ={
        "$allElementsTrue":[]
    }
    for item in args:
        ret["$allElementsTrue"].append(__get_field_expr__(item))
    return __create__(ret)
def And(*args):
    return __create_array__("$and",*args)
def add(*args):
    """
    { $add: [ <expression1>, <expression2>, ... ] }
    :param args:
    :return:
    """
    return __create_array__("$add", *args)
def anyElementTrue(*args):
    return __create_array__("$anyElementTrue", *args)
def arrayElemAt(*args):
    return __create_array__("$arrayElemAt", *args)
def arrayToObject(expr):
    """
    { $arrayToObject: <expression> }
    :param expr:
    :return:
    """
    return __create_item__("$arrayToObject",expr)
def expr(expr):
    """
    { $expr: <expression> }
    :param expr:
    :return:
    """
    return __create_item__("$expr",expr)
def avg(*args,**kwargs):
    """
        { $avg: <expression> }
        :param expr:
        :return:
        """
    return __avg_consume__(*args,**kwargs)
def ceil(expr):
    """
    { $ceil: <number> }
    :return:
    """
    return __create_item__("$ceil", expr)
def cmp(expr1,expr2):
    """
    { $cmp: [ <expression1>, <expression2> ] }
    :return:
    """
    return __create_array__("$cmp", expr1,expr2)
def concatArrays(*args):
    """
    { $concatArrays: [ <array1>, <array2>, ... ] }
    :return:
    """
    return __comsume_with_array__(*args)
def convert(input,to,onError=None,onNull=None):
    """
    {
       $convert:
          {
             input: <expression>,
             to: <type expression>,
             onError: <expression>,  // Optional.
             onNull: <expression>    // Optional.
          }
    }
    :return:
    """
    ret_data=dict(
        input=__get_field_expr__(input),
        to=__get_field_expr__(to)
    )
    if onError!=None:
        ret_data.update({
            "onError":__get_field_expr__(onError)
        })
    if onNull!=None:
        ret_data.update({
            "onNull":__get_field_expr__(onNull)
        })
    return __create_item__("$convert",ret_data)
def dateFromParts(year,month=None,day=None,hour=None,minute=None,second=None,milliseconds=None,timezone=None):
    """
    { $dateFromParts :
        {'year': <year>, 'month': <month>, 'day': <day>,
        'hour': <hour>, 'minute': <minute>, 'second': <second>,
        'milliseconds': <ms>, 'timezone': <tzExpression>
        }
    }
    :return:
    """
    ret_data={
        "year":year
    }
    optionals=dict(
        month=month,
        day=day,
        hour=hour,
        minute=minute,
        second=second,
        milliseconds=milliseconds,
        timezone=timezone
    )
    for k,v in optionals.items():
        if v!=None:
            ret_data.update({
                k:v
            })
    return __create_item__("$dateFromParts",ret_data)
def dateToParts(date,timezone=None,iso8601=None):
    """
    {
        $dateToParts: {
            'date' : <dateExpression>,
            'timezone' : <timezone>,
            'iso8601' : <boolean>
        }
    }
    :return:
    """
    optionals=dict(
        timezone=timezone,
        iso8601=iso8601
    )
    ret_data={
        "date":date
    }
    for k,v in optionals.items():
        if v!=None:
            ret_data.update({
                k:v
            })
    return __create_item__("$dateToParts",ret_data)
def dateFromString(dateString,format=None,timezone=None,onError=None,onNull=None):
    """
    { $dateFromString: {
         dateString: <dateStringExpression>,
         format: <formatStringExpression>,
         timezone: <tzExpression>,
         onError: <onErrorExpression>,
         onNull: <onNullExpression>
    } }
    :return:
    """
    ret_data={
        "dateString":dateString
    }
    optionals=dict(
        format=format,
        timezone=timezone,
        onNull=onNull,
        onError=onError
    )
    for k, v in optionals.items():
        if v != None:
            ret_data.update({
                k: v
            })
    return __create_item__("$dateFromString", ret_data)
def dateToString(date,format=None,timezone=None,onNull=None):
    """
    { $dateToString: {
        date: <dateExpression>,
        format: <formatString>,
        timezone: <tzExpression>,
        onNull: <expression>
    } }
    :return:
    """
    ret_data = {
        "date": date
    }
    optionals = dict(
        format=format,
        timezone=timezone,
        onNull=onNull
    )
    for k, v in optionals.items():
        if v != None:
            ret_data.update({
                k: v
            })
    return __create_item__("$dateToString", ret_data)
def dayOfMonth(date,timezone=None):
    """
    { date: <dateExpression>, timezone: <tzExpression> }
    :return:
    """
    ret_data = {
        "date": date
    }
    optionals = dict(
        timezone=timezone
    )
    for k, v in optionals.items():
        if v != None:
            ret_data.update({
                k: v
            })
    return __create_item__("$dayOfMonth", ret_data)
def dayOfWeek(date,timezone=None):
    """
    { date: <dateExpression>, timezone: <tzExpression> }
    :return:
    """
    ret_data = {
        "date": date
    }
    optionals = dict(
        timezone=timezone
    )
    for k, v in optionals.items():
        if v != None:
            ret_data.update({
                k: v
            })
    return __create_item__("$dayOfWeek", ret_data)
def dayOfYear(date,timezone=None):
    """
    { date: <dateExpression>, timezone: <tzExpression> }
    :return:
    """
    ret_data = {
        "date": date
    }
    optionals = dict(
        timezone=timezone
    )
    for k, v in optionals.items():
        if v != None:
            ret_data.update({
                k: v
            })
    return __create_item__("$dayOfYear", ret_data)

def filter(field,iter,cond):
    """
    { $filter: { input: <array>, as: <string>, cond: <expression> } }
    :return:
    """
    import pydocs

    return __create_item__("$filter", {
        "input":__get_field_expr__(field),
        "as":pydocs.get_field_expr(iter,True),
        "cond":__get_field_expr__(cond)
    })
def first(*args, **kwargs):
    """
    { $first: <expression> }
    :param field:
    :return:
    """
    return __avg_consume__(*args, **kwargs)
def floor(field):
    """
    { $floor: <number> }
    :param field:
    :return:
    """
    return __comsume_with_item__(field)
def hour(date,timezone=None):
    """
    { date: <dateExpression>, timezone: <tzExpression> }
    :return:
    """
    ret_data = {
        "date": date
    }
    optionals = dict(
        timezone=timezone
    )
    for k, v in optionals.items():
        if v != None:
            ret_data.update({
                k: v
            })
    return __create_item__("$hour", ret_data)
def ifNull(field,replacement):
    """
    { $ifNull: [ <expression>, <replacement-expression-if-null> ] }
    :return:
    """
    return __create_array__("$ifNull",field,replacement)
def In(field,items):
    """
    { $in: [ <expression>, <array expression> ] }
    :return:
    """
    _items=[]
    for item in items:
        _items.append(__get_field_expr__(item))
    return __create__({
        "$in":_items
    })
def indexOfArray(items,field,start=None,end=None):
    """
    { $indexOfArray: [ <array expression>, <search expression>, <start>, <end> ] }
    :return:
    """
    _items=[
        __get_field_expr__(items),
        __get_field_expr__(field)
    ]
    if start!=None:
        _items.append(__get_field_expr__(start))
    if end!=None:
        _items.append(__get_field_expr__(end))
    return __create__({
        "$indexOfArray":_items
    })
def indexOfBytes(field1,field2,start=None,end=None):
    """
    { $indexOfBytes: [ <string expression>, <substring expression>, <start>, <end> ] }
    :return:
    """
    _items = [
        __get_field_expr__(field1),
        __get_field_expr__(field2)
    ]
    if start != None:
        _items.append(__get_field_expr__(start))
    if end != None:
        _items.append(__get_field_expr__(end))
    return __create__({
        "$indexOfArray": _items
    })
def indexOfCP(field1,field2,start=None,end=None):
    """
    { $indexOfCP: [ <string expression>, <substring expression>, <start>, <end> ] }
    :return:
    """
    _items = [
        __get_field_expr__(field1),
        __get_field_expr__(field2)
    ]
    if start != None:
        _items.append(__get_field_expr__(start))
    if end != None:
        _items.append(__get_field_expr__(end))
    return __create__({
        "$indexOfCP": _items
    })
def isArray(field):
    """
    { $isArray: [ <expression> ] }
    :return:
    """
    return __comsume_with_item__(field)
def isoDayOfWeek(date,timezone=None):
    """
    { $isoDayOfWeek: <dateExpression> }
    { date: <dateExpression>, timezone: <tzExpression> }
    :return:
    """
    ret_data = {
        "date": date
    }
    optionals = dict(
        timezone=timezone
    )
    for k, v in optionals.items():
        if v != None:
            ret_data.update({
                k: v
            })
    return __create_item__("$isoDayOfWeek", ret_data)
def isoWeek(date,timezone=None):
    """
    { $isoWeek: <dateExpression> }
    { date: <dateExpression>, timezone: <tzExpression> }
    :return:
    """
    ret_data = {
        "date": date
    }
    optionals = dict(
        timezone=timezone
    )
    for k, v in optionals.items():
        if v != None:
            ret_data.update({
                k: v
            })
    return __create_item__("$isoWeek", ret_data)
def isoWeekYear(date,timezone=None):
    """
    { $isoWeekYear: <dateExpression> }
    { date: <dateExpression>, timezone: <tzExpression> }
    :return:
    """
    ret_data = {
        "date": date
    }
    optionals = dict(
        timezone=timezone
    )
    for k, v in optionals.items():
        if v != None:
            ret_data.update({
                k: v
            })
    return __create_item__("$isoWeekYear", ret_data)
def last(*args, **kwargs):
    """
    { $last: <expression> }
    :return:
    """
    return __avg_consume__(*args, **kwargs)

def let(vars,In):
    """
    {
      $let:
         {
           vars: { <var1>: <expression>, ... },
           in: <expression>
         }
    }
    :return:
    """
    _vars={}
    for k,v in vars.items():
        _vars.update({
            k:__get_field_expr__(v)
        })
    return __create__({
        "$let":{
            "vars":_vars,
            "in":__get_field_expr__(In)
        }
    })
def literal(value):
    """
    { $literal: <value> }
    :return:
    """
    return __comsume_with_item__(value)
def lg(value):
    """
    { $literal: <value> }
    :return:
    """
    return __comsume_with_item__(value)
def log(value):
    """
    { $literal: <value> }
    :return:
    """
    return __comsume_with_item__(value)

def log10(value):
    """
    { $literal: <value> }
    :return:
    """
    return __comsume_with_item__(value)
def ltrim(field,chars=None):
    """
    { $ltrim: { input: <string>,  chars: <string> } }
    :return:
    """
    ret_data = {
        "input": field
    }
    optionals = dict(
        chars=chars
    )
    for k, v in optionals.items():
        if v != None:
            ret_data.update({
                k: __get_field_expr__(v)
            })
    return __create_item__("$trim", ret_data)
def map(field,In,alias):
    from . import pydocs
    from . import pyaggregatebuilders
    """
    { $map: { input: <expression>, as: <string>, in: <expression> } }
    :return:
    """
    _In = pyaggregatebuilders.Project(*In).stage
    ret_data={
        "$map":{
            "input":pydocs.get_field_expr(field),
            "as":pydocs.get_field_expr(alias,True),
            "in":_In
        }
    }
    return __create__(ret_data)
def max(*args,**kwargs):
    """
    { $max: <expression> }
    :return:
    """
    return __avg_consume__(*args,**kwargs)
def mergeObjects(*args):
    """
    { $mergeObjects: [ <document1>, <document2>, ... ] }
    :return:
    """
    return __comsume_with_array__(*args)
def meta(field):
    """
    { $meta: <metaDataKeyword> }
    :return:
    """
    return __comsume_with_item__(field)
def min(*args,**kwargs):
    """
    { $min: <metaDataKeyword> }
    :return:
    """
    return __avg_consume__(*args,**kwargs)
def millisecond(date,timezone):
    """
    { $millisecond: <dateExpression> }
    { date: <dateExpression>, timezone: <tzExpression> }
    :return:
    """
    ret_data = {
        "date": __get_field_expr__(date)
    }
    optionals = dict(
        timezone=__get_field_expr__(timezone)
    )
    for k, v in optionals.items():
        if v != None:
            ret_data.update({
                k: __get_field_expr__(v)
            })
    return __create_item__("$millisecond", ret_data)
def minute(date,timezone):
    """
    { $minute: <dateExpression> }
    { date: <dateExpression>, timezone: <tzExpression> }
    :return:
    """
    ret_data = {
        "date": __get_field_expr__(date)
    }
    optionals = dict(
        timezone=__get_field_expr__(timezone)
    )
    for k, v in optionals.items():
        if v != None:
            ret_data.update({
                k: __get_field_expr__(v)
            })
    return __create_item__("$minute", ret_data)
def month(date,timezone):
    """
    { $month: <dateExpression> }
    { date: <dateExpression>, timezone: <tzExpression> }
    :return:
    """
    ret_data = {
        "date": __get_field_expr__(date)
    }
    optionals = dict(
        timezone=__get_field_expr__(timezone)
    )
    for k, v in optionals.items():
        if v != None:
            ret_data.update({
                k: __get_field_expr__(v)
            })
    return __create_item__("$month", ret_data)
def Not(field):
    return __create__({
        "$not":__get_field_expr__(field)
    })
def objectToArray(field):
    """
    { $objectToArray: <object> }
    :return:
    """
    return __comsume_with_item__(field)
def pow(number,exponent):
    """
    { $pow: [ <number>, <exponent> ] }
    :return:
    """
    return __create__({
        "$pow":[
            __get_field_expr__(number),
            __get_field_expr__(exponent)
        ]
    })
def push(field):
    """
    { $push: <expression> }
    :return:
    """
    return __comsume_with_item__(field)
pyrange = range
def range(start,end,step=None):
    """
    { $range: [ <start>, <end>, <non-zero step> ] }
    :return:
    """
    ret_data=[__get_field_expr__(start),__get_field_expr__(end)]

    if step!=None:
        ret_data.append(__get_field_expr__(step))
    return __create__({
        "$range":ret_data
    })
def reduce(field,initialValue,In):
    """
    {
        $reduce: {
            input: <array>,
            initialValue: <expression>,
            in: <expression>
        }
    }
    :return:
    """
    return __create_item__("$reduce",{
        "input":__get_field_expr__(field),
        "initialValue":__get_field_expr__(initialValue),
        "in":__get_field_expr__(In)
    })
def reverseArray(field):
    """
    { $reverseArray: <array expression> }
    :return:
    """
    return __comsume_with_item__(field)

def rtrim(field,chars=None):
    """
    { $rtrim: { input: <string>,  chars: <string> } }
    :return:
    """
    ret_data = {
        "input": field
    }
    optionals = dict(
        chars=chars
    )
    for k, v in optionals.items():
        if v != None:
            ret_data.update({
                k: __get_field_expr__(v)
            })
    return __create_item__("$rrim", ret_data)
def second(date,timezone):
    """
    { $second: <dateExpression> }
    { date: <dateExpression>, timezone: <tzExpression> }
    :return:
    """
    ret_data = {
        "date": __get_field_expr__(date)
    }
    optionals = dict(
        timezone=__get_field_expr__(timezone)
    )
    for k, v in optionals.items():
        if v != None:
            ret_data.update({
                k: __get_field_expr__(v)
            })
    return __create_item__("$second", ret_data)
def setDifference(*args,**kwargs):
    """
    { $setDifference: [ <expression1>, <expression2> ] }
    :return:
    """
    return __avg_consume__(*args,**kwargs)
def setEquals(*args,**kwargs):
    """
    { $setEquals: [ <expression1>, <expression2>, ... ] }
    :return:
    """
    return __avg_consume__(*args, **kwargs)
def setIntersection(*args, **kwargs):
    """
    { $setIntersection: [ <array1>, <array2>, ... ] }
    :return:
    """
    return __avg_consume__(*args, **kwargs)
def setIsSubset(field1,field2):
    """
    { $setIsSubset: [ <expression1>, <expression2> ] }
    :return:
    """
    return __comsume_with_array__(field1, field2)
def setUnion(*args,**kwargs):
    """{ $setUnion: [ <expression1>, <expression2>, ... ] }"""
    return __avg_consume__(*args,**kwargs)
def size(field):
    """
    { $size: <expression> }
    :return:
    """
    return __comsume_with_item__(field)
def slice(field,n,position=None):
    """
    { $slice: [ <array>, <position>, <n> ] }
    :return:
    """
    if position!=None:
        return __comsume_with_array__(field,
                                      position,
                                      n)
    else:
        return __comsume_with_array__(field,
                                      position)
def split(field,delimiter):
    """
    { $split: [ <string expression>, <delimiter> ] }
    :return:
    """
    return __comsume_with_array__(
        field,
        delimiter
    )
def sqrt(field):
    """
    { $sqrt: <number> }
    :return:
    """
    return __comsume_with_item__(field)
def stdDevPop(*args,**kwargs):
    """
       { $stdDevPop: <number> }
       :return:
       """
    return __avg_consume__(*args,**kwargs)

def stdDevSamp(*args,**kwargs):
    """
           { $stdDevSamp: <number> }
           :return:
           """
    return __avg_consume__(*args, **kwargs)
def strcasecmp(field1,field2):
    """
    { $strcasecmp: [ <expression1>, <expression2> ] }
    :return:
    """
    return __comsume_with_array__(
        field1,
        field2
    )
def strLenBytes(field):
    """
    { $strLenBytes: <string expression> }
    :return:
    """
    return __comsume_with_item__(field)
def strLenCP(field):
    """
    { $strLenCP: <string expression> }
    :return:
    """
    return __comsume_with_item__(field)
def substr(field,start,length):
    """
    { $substr: [ <string>, <start>, <length> ] }
    :return:
    """
    return __comsume_with_array__(
        field,
        start,
        length
    )
def substrBytes(field,index,count):
    """
    { $substrBytes: [ <string expression>, <byte index>, <byte count> ] }
    :return:
    """
    return __comsume_with_array__(
        field,
        index,
        count
    )
def substrCP(field,index,count):
    """
    { $substrCP: [ <string expression>, <code point index>, <code point count> ] }
    :return:
    """
    return __comsume_with_array__(
        field,
        index,
        count
    )
def sum(*args,**kwargs):
    return __avg_consume__(*args,**kwargs)
def stdDevSamp(*args,**kwargs):
    return __avg_consume__(*args, **kwargs)
def stdDevPop(*args,**kwargs):
    return __avg_consume__(*args, **kwargs)
def toBool(field):
    """
    {
       $toBool: <expression>
    }
    :return:
    """
    return __comsume_with_item__(field)
def toDate(field):
    """
    {
       $toBool: <expression>
    }
    :return:
    """
    return __comsume_with_item__(field)
def toDecimal(field):
    return __comsume_with_item__(field)
def toDouble(field):
    return __comsume_with_item__(field)
def toInt(field):
    return __comsume_with_item__(field)
def toLong(field):
    return __comsume_with_item__(field)
def toObjectId(field):
    return __comsume_with_item__(field)
def toString(field):
    return __comsume_with_item__(field)
def toLower(field):
    return __comsume_with_item__(field)
def toUpper(field):
    return __comsume_with_item__(field)
def trim(field):
    return __comsume_with_item__(field)
def trunc(field):
    return __comsume_with_item__(field)
def type(field):
    return __comsume_with_item__(field)
def week(field,timezone=None):
    if timezone:
        return __create_item__("$week",{
            "date": __get_field_expr__(field),
            "timezone":timezone
        })
    else:
        return __comsume_with_item__(field)
def year(field,timezone=None):
    if timezone:
        return __create_item__("$year",{
            "date": __get_field_expr__(field),
            "timezone":timezone
        })
    else:
        return __comsume_with_item__(field)
def zip(field):
    return __comsume_with_item__(field)


import flask
from flask import request, jsonify
import subprocess
from flask_cors import CORS, cross_origin
import json
import os

app = flask.Flask(__name__)
app.config["DEBUG"] = True
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

chapel_frontend_executable = "/root/chapel_frontend/ChapelParseJson"
chapel_frontend_param = "/tmp/program_to_be_parsed.txt"

@app.route('/', methods=['GET'])
def home():
    return "
Home

Here you cant see nothing..
"

@app.route('/api/v1/resources/haskell/chapel_parser/', methods=['POST'])
@cross_origin()
def api_all():
    post_content = request.get_json()
    program = post_content["program"]

    if os.path.exists(chapel_frontend_param):
        os.remove(chapel_frontend_param)

    file = open(chapel_frontend_param,"w+")

    file.write(program)

    file.close()

    temp = subprocess.Popen([chapel_frontend_executable, chapel_frontend_param], stdout = subprocess.PIPE, universal_newlines=True)
    # get the output as a string
    output = str(temp.communicate()[0])

    print (output)

    # response = "asdad"
    # response.headers.add("Access-Control-Allow-Origin", "*")

    # response.headers.add('Access-Control-Allow-Origin', '*')
    # print (output)
    return output

app.run(host='0.0.0.0', port=8080)


# -*- coding: utf-8 -*-

from openfisca_core.parameters import Parameter, ParameterNode, Scale

def build_api_values_history(values_history):
    api_values_history = {}
    for value_at_instant in values_history.values_list:
        api_values_history[value_at_instant.instant_str] = value_at_instant.value

    return api_values_history

def get_value(date, values):
    candidates = sorted(
        [
            (start_date, value)
            for start_date, value in values.items()
            if start_date
            <= date  # dates are lexicographically ordered and can be sorted
        ],
        reverse=True,
    )

    if candidates:
        return candidates[0][1]
    else:
        return None

def build_api_scale(scale, value_key_name):
    # preprocess brackets for a scale with 'rates' or 'amounts'
    brackets = [
        {
            "thresholds": build_api_values_history(bracket.threshold),
            "values": build_api_values_history(getattr(bracket, value_key_name)),
        }
        for bracket in scale.brackets
    ]

    dates = set(
        sum(
            [
                list(bracket["thresholds"].keys()) + list(bracket["values"].keys())
                for bracket in brackets
            ],
            [],
        )
    )  # flatten the dates and remove duplicates

    # We iterate on all dates as we need to build the whole scale for each of them
    api_scale = {}
    for date in dates:
        for bracket in brackets:
            threshold_value = get_value(date, bracket["thresholds"])
            if threshold_value is not None:
                rate_or_amount_value = get_value(date, bracket["values"])
                api_scale[date] = api_scale.get(date) or {}
                api_scale[date][threshold_value] = rate_or_amount_value

    # Handle stopped parameters: a parameter is stopped if its first bracket is stopped
    latest_date_first_threshold = max(brackets[0]["thresholds"].keys())
    latest_value_first_threshold = brackets[0]["thresholds"][
        latest_date_first_threshold
    ]

    if latest_value_first_threshold is None:
        api_scale[latest_date_first_threshold] = None

    return api_scale

def build_source_url(absolute_file_path, country_package_metadata):
    relative_path = absolute_file_path.replace(country_package_metadata["location"], "")
    return "{}/blob/{}{}".format(
        country_package_metadata["repository_url"],
        country_package_metadata["version"],
        relative_path,
    )

def build_api_parameter(parameter, country_package_metadata):
    api_parameter = {
        "description": getattr(parameter, "description", None),
        "id": parameter.name,
        "metadata": parameter.metadata,
    }
    if parameter.file_path:
        api_parameter["source"] = build_source_url(
            parameter.file_path, country_package_metadata
        )
    if isinstance(parameter, Parameter):
        if parameter.documentation:
            api_parameter["documentation"] = parameter.documentation.strip()
        api_parameter["values"] = build_api_values_history(parameter)
    elif isinstance(parameter, Scale):
        if "rate" in parameter.brackets[0].children:
            api_parameter["brackets"] = build_api_scale(parameter, "rate")
        elif "amount" in parameter.brackets[0].children:
            api_parameter["brackets"] = build_api_scale(parameter, "amount")
    elif isinstance(parameter, ParameterNode):
        if parameter.documentation:
            api_parameter["documentation"] = parameter.documentation.strip()
        api_parameter["subparams"] = {
            child_name: {
                "description": child.description,
            }
            for child_name, child in parameter.children.items()
        }
    return api_parameter

def build_parameters(tax_benefit_system, country_package_metadata):
    return {
        parameter.name.replace(".", "/"): build_api_parameter(
            parameter, country_package_metadata
        )
        for parameter in tax_benefit_system.parameters.get_descendants()
    }


from pylab import *
from scipy.optimize import curve_fit

c=3e8
h=6.626e-34
k=1.38e-23

data=loadtxt('firas.txt')
x=data[:,0]*c*100
y=data[:,1]*1e-20
er=data[:,3]*1e-23*1000

def I(f,T):
    return 2*h/c**2*f**3/(exp(h*f/(k*T))-1)

temp,_=curve_fit(I,x,y)
print('Temperature =',temp[0],'K')
errorbar(x,y,yerr=er,label='Data')
plot(x,y,'.')
plot(x,I(x,temp[0]),label='Fitted Plot')
xlabel("$\\nu$ [Hz]")
ylabel("$I(\\nu)$ [W/$m^2$/Hz/sr]")
title('CMB')
legend()
grid()
show()


"""
-------------------------------------------------
   File Name:    __init__.py.py
   Date:         2019/10/17
   Description:
-------------------------------------------------
"""

# function to calculate the Exponential moving averages for the Generator weights
# This function updates the exponential average weights based on the current training
# Copy from: https://github.com/akanimax/pro_gan_pytorch
def update_average(model_tgt, model_src, beta):
    """
    update the model_target using exponential moving averages
    :param model_tgt: target model
    :param model_src: source model
    :param beta: value of decay beta
    :return: None (updates the target model)
    """

    # utility function for toggling the gradient requirements of the models
    def toggle_grad(model, requires_grad):
        for p in model.parameters():
            p.requires_grad_(requires_grad)

    # turn off gradient calculation
    toggle_grad(model_tgt, False)
    toggle_grad(model_src, False)

    param_dict_src = dict(model_src.named_parameters())

    for p_name, p_tgt in model_tgt.named_parameters():
        p_src = param_dict_src[p_name]
        assert (p_src is not p_tgt)
        p_tgt.copy_(beta * p_tgt + (1. - beta) * p_src)

    # turn back on the gradient calculation
    toggle_grad(model_tgt, True)
    toggle_grad(model_src, True)


from django.urls import path
from core import views

urlpatterns = [
    path('contests/', views.contests, name='all_contests'),
    path('contests/<int:contest_id>', views.contests, name='contest'),
    path('contests/<str:contests_type>', views.contests, name='contests'),
    path('attempts/', views.attempts, name='all_attempts'),
    path('attempts/<int:contest_id>', views.attempts, name='attempts'),
    path('teams/', views.teams, name='all_teams'),
    path('teams/<int:team_id>', views.teams, name='team'),
    path('teams/contest-<int:contest_id>', views.teams, name='teams'),
    path('get_permissions/<int:contest_id>', views.get_permissions, name='permissions'),
    path('users/', views.register_user, name='register_user')
]


import re
import time
from datetime import timedelta
from typing import Optional

import numpy as np
from pponnxcr.predict_system import BoxedResult

import module.config.server as server
from module.base.button import ButtonWrapper
from module.base.decorator import cached_property
from module.base.utils import area_pad, corner2area, crop, float2str
from module.exception import ScriptError
from module.logger import logger
from module.ocr.keyword import Keyword
from module.ocr.models import OCR_MODEL, TextSystem
from module.ocr.utils import merge_buttons

class OcrResultButton:
    def __init__(self, boxed_result: BoxedResult, matched_keyword: Optional[Keyword]):
        """
        Args:
            boxed_result: BoxedResult from ppocr-onnx
            matched_keyword: Keyword object or None
        """
        self.area = boxed_result.box
        self.search = area_pad(self.area, pad=-20)
        # self.color =
        self.button = boxed_result.box

        if matched_keyword is not None:
            self.matched_keyword = matched_keyword
            self.name = str(matched_keyword)
        else:
            self.matched_keyword = None
            self.name = boxed_result.ocr_text

        self.text = boxed_result.ocr_text
        self.score = boxed_result.score

    def __str__(self):
        return self.name

    __repr__ = __str__

    def __eq__(self, other):
        return str(self) == str(other)

    def __hash__(self):
        return hash(self.name)

    def __bool__(self):
        return True

    @property
    def is_keyword_matched(self) -> bool:
        return self.matched_keyword is not None

class Ocr:
    # Merge results with box distance <= thres
    merge_thres_x = 0
    merge_thres_y = 0

    def __init__(self, button: ButtonWrapper, lang=None, name=None):
        """
        Args:
            button:
            lang: If None, use in-game language
            name: If None, use button.name
        """
        if lang is None:
            lang = server.lang
        if name is None:
            name = button.name

        self.button: ButtonWrapper = button
        self.lang: str = lang
        self.name: str = name

    @cached_property
    def model(self) -> TextSystem:
        return OCR_MODEL.get_by_lang(self.lang)

    def pre_process(self, image):
        """
        Args:
            image (np.ndarray): Shape (height, width, channel)

        Returns:
            np.ndarray: Shape (width, height)
        """
        return image

    def after_process(self, result):
        """
        Args:
            result (str): '第二行'

        Returns:
            str:
        """
        if result.startswith('UID'):
            result = 'UID'
        return result

    def format_result(self, result):
        """
        Will be overriden.
        """
        return result

    def _log_change(self, attr, func, before):
        after = func(before)
        if after != before:
            logger.attr(f'{self.name} {attr}', f'{before} -> {after}')
        return after

    def ocr_single_line(self, image, direct_ocr=False):
        # pre process
        start_time = time.time()
        if not direct_ocr:
            image = crop(image, self.button.area)
        image = self.pre_process(image)
        # ocr
        result, _ = self.model.ocr_single_line(image)
        # after proces
        result = self._log_change('after', self.after_process, result)
        result = self._log_change('format', self.format_result, result)
        logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
                    text=str(result))
        return result

    def ocr_multi_lines(self, image_list):
        # pre process
        start_time = time.time()
        image_list = [self.pre_process(image) for image in image_list]
        # ocr
        result_list = self.model.ocr_lines(image_list)
        result_list = [(result, score) for result, score in result_list]
        # after process
        result_list = [(self.after_process(result), score) for result, score in result_list]
        result_list = [(self.format_result(result), score) for result, score in result_list]
        logger.attr(name="%s %ss" % (self.name, float2str(time.time() - start_time)),
                    text=str([result for result, _ in result_list]))
        return result_list

    def filter_detected(self, result: BoxedResult) -> bool:
        """
        Return False to drop result.
        """
        return True

    def detect_and_ocr(self, image, direct_ocr=False) -> list[BoxedResult]:
        """
        Args:
            image:
            direct_ocr: True to ignore `button` attribute and feed the image to OCR model without cropping.

        Returns:

        """
        # pre process
        start_time = time.time()
        if not direct_ocr:
            image = crop(image, self.button.area)
        image = self.pre_process(image)
        # ocr
        results: list[BoxedResult] = self.model.detect_and_ocr(image)
        # after proces
        for result in results:
            if not direct_ocr:
                result.box += self.button.area[:2]
            result.box = tuple(corner2area(result.box))

        results = [result for result in results if self.filter_detected(result)]
        results = merge_buttons(results, thres_x=self.merge_thres_x, thres_y=self.merge_thres_y)
        for result in results:
            result.ocr_text = self.after_process(result.ocr_text)

        logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
                    text=str([result.ocr_text for result in results]))
        return results

    def _match_result(
            self,
            result: str,
            keyword_classes,
            lang: str = None,
            ignore_punctuation=True,
            ignore_digit=True):
        """
        Args:
            result (str):
            keyword_classes: A list of `Keyword` class or classes inherited `Keyword`

        Returns:
            If matched, return `Keyword` object or objects inherited `Keyword`
            If not match, return None
        """
        if not isinstance(keyword_classes, list):
            keyword_classes = [keyword_classes]

        # Digits will be considered as the index of keyword
        if ignore_digit:
            if result.isdigit():
                return None

        # Try in current lang
        for keyword_class in keyword_classes:
            try:
                matched = keyword_class.find(
                    result,
                    lang=lang,
                    ignore_punctuation=ignore_punctuation
                )
                return matched
            except ScriptError:
                continue

        return None

    def matched_single_line(
            self,
            image,
            keyword_classes,
            lang: str = None,
            ignore_punctuation=True
    ) -> OcrResultButton:
        """
        Args:
            image: Image to detect
            keyword_classes: `Keyword` class or classes inherited `Keyword`, or a list of them.
            lang:
            ignore_punctuation:

        Returns:
            OcrResultButton: Or None if it didn't matched known keywords.
        """
        result = self.ocr_single_line(image)

        result = self._match_result(
            result,
            keyword_classes=keyword_classes,
            lang=lang,
            ignore_punctuation=ignore_punctuation,
        )

        logger.attr(name=f'{self.name} matched',
                    text=result)
        return result

    def matched_multi_lines(
            self,
            image_list,
            keyword_classes,
            lang: str = None,
            ignore_punctuation=True
    ) -> list[OcrResultButton]:
        """
        Args:
            image_list:
            keyword_classes: `Keyword` class or classes inherited `Keyword`, or a list of them.
            lang:
            ignore_punctuation:

        Returns:
            List of matched OcrResultButton.
            OCR result which didn't matched known keywords will be dropped.
        """
        results = self.ocr_multi_lines(image_list)

        results = [self._match_result(
            result,
            keyword_classes=keyword_classes,
            lang=lang,
            ignore_punctuation=ignore_punctuation,
        ) for result in results]
        results = [result for result in results if result.is_keyword_matched]

        logger.attr(name=f'{self.name} matched',
                    text=results)
        return results

    def _product_button(
            self,
            boxed_result: BoxedResult,
            keyword_classes,
            lang: str = None,
            ignore_punctuation=True,
            ignore_digit=True
    ) -> OcrResultButton:
        if not isinstance(keyword_classes, list):
            keyword_classes = [keyword_classes]

        matched_keyword = self._match_result(
            boxed_result.ocr_text,
            keyword_classes=keyword_classes,
            lang=lang,
            ignore_punctuation=ignore_punctuation,
            ignore_digit=ignore_digit,
        )
        button = OcrResultButton(boxed_result, matched_keyword)
        return button

    def matched_ocr(self, image, keyword_classes, direct_ocr=False) -> list[OcrResultButton]:
        """
        Args:
            image: Screenshot
            keyword_classes: `Keyword` class or classes inherited `Keyword`, or a list of them.
            direct_ocr: True to ignore `button` attribute and feed the image to OCR model without cropping.

        Returns:
            List of matched OcrResultButton.
            OCR result which didn't matched known keywords will be dropped.
        """
        results = self.detect_and_ocr(image, direct_ocr=direct_ocr)

        results = [self._product_button(result, keyword_classes) for result in results]
        results = [result for result in results if result.is_keyword_matched]

        logger.attr(name=f'{self.name} matched',
                    text=results)
        return results

class Digit(Ocr):
    def __init__(self, button: ButtonWrapper, lang='en', name=None):
        super().__init__(button, lang=lang, name=name)

    def format_result(self, result) -> int:
        """
        Returns:
            int:
        """
        result = super().after_process(result)
        logger.attr(name=self.name, text=str(result))

        res = re.search(r'(\d+)', result)
        if res:
            return int(res.group(1))
        else:
            logger.warning(f'No digit found in {result}')
            return 0

class DigitCounter(Ocr):
    def __init__(self, button: ButtonWrapper, lang='en', name=None):
        super().__init__(button, lang=lang, name=name)

    def format_result(self, result) -> tuple[int, int, int]:
        """
        Do OCR on a counter, such as `14/15`, and returns 14, 1, 15

        Returns:
            int:
        """
        result = super().after_process(result)
        logger.attr(name=self.name, text=str(result))

        res = re.search(r'(\d+)/(\d+)', result)
        if res:
            groups = [int(s) for s in res.groups()]
            current, total = int(groups[0]), int(groups[1])
            # current = min(current, total)
            return current, total - current, total
        else:
            logger.warning(f'No digit counter found in {result}')
            return 0, 0, 0

class Duration(Ocr):
    @classmethod
    def timedelta_regex(cls, lang):
        regex_str = {
            'cn': r'^(?P<prefix>.*?)'
                  r'((?P<days>\d{1,2})\s*天\s*)?'
                  r'((?P<hours>\d{1,2})\s*小时\s*)?'
                  r'((?P<minutes>\d{1,2})\s*分钟\s*)?'
                  r'((?P<seconds>\d{1,2})\s*秒)?'
                  r'(?P<suffix>[^天时钟秒]*?)$',
            'en': r'^(?P<prefix>.*?)'
                  r'((?P<days>\d{1,2})\s*d\s*)?'
                  r'((?P<hours>\d{1,2})\s*h\s*)?'
                  r'((?P<minutes>\d{1,2})\s*m\s*)?'
                  r'((?P<seconds>\d{1,2})\s*s)?'
                  r'(?P<suffix>[^dhms]*?)$'
        }[lang]
        return re.compile(regex_str)

    def after_process(self, result):
        result = super().after_process(result)
        result = result.strip('.,。，')
        result = result.replace('Oh', '0h').replace('oh', '0h')
        return result

    def format_result(self, result: str) -> timedelta:
        """
        Do OCR on a duration, such as `18d 2h 13m 30s`, `2h`, `13m 30s`, `9s`

        Returns:
            timedelta:
        """
        matched = self.timedelta_regex(self.lang).search(result)
        if not matched:
            return timedelta()
        days = self._sanitize_number(matched.group('days'))
        hours = self._sanitize_number(matched.group('hours'))
        minutes = self._sanitize_number(matched.group('minutes'))
        seconds = self._sanitize_number(matched.group('seconds'))
        return timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)

    @staticmethod
    def _sanitize_number(number) -> int:
        if number is None:
            return 0
        return int(number)


# loading cash amount from the database
import datetime
import os
import sys
import pandas as pd

# *********** импортируем данные для подключения к базам
scriptpath = r"d:\Prestige\Python\Config"
sys.path.append(os.path.abspath(scriptpath))
from authorize import con_postgres_psycopg2

conpg = con_postgres_psycopg2()

def sql_rest_of_cash():
    return '''
        SELECT currency, kasa, (sum(amount_receipt) + sum(amount_expense)) AS amount
        FROM v_one_cach
        WHERE bank_account_cashier_key IN
            (SELECT DISTINCT
                bank_account_cashier_key
            FROM v_one_cach
            WHERE (doc_date >= '2022-01-01')
                AND (type_of_cash = 'Наличные')
            GROUP BY bank_account_cashier_key)
        GROUP BY currency, kasa
        ORDER BY currency, kasa;
    '''

def create_sql_universal(field, table_name, parameter):
    return '''
        SELECT DISTINCT %s
        FROM %s
        WHERE (type_of_cash = 'Наличные') AND %s
        ORDER BY %s
    ''' % (field, table_name, parameter, field)

def get_currencies(prm):
    # create sql of type currency
    field = "currency"
    parameter = "doc_date = '%s'" % prm
    table_name = 'v_one_cach'
    sql = create_sql_universal(field, table_name, parameter)
    return sql

def get_cash_expenses(date):
    sms = ''
    field = "currency"
    parameter_currency = "doc_date = '%s'" % date
    table_name = 'v_one_cach'
    try:
        conpg = con_postgres_psycopg2()
        sql = create_sql_universal(field, table_name, parameter_currency)
        currencies = pd.read_sql(sql, conpg)
        for idcurrency, currency in currencies.itertuples():
            field = "kasa"
            parameter_kasa = "%s AND currency = '%s'" % (parameter_currency, currency)
            sql = create_sql_universal(field, table_name, parameter_kasa)
            cash_boxs = pd.read_sql(sql, conpg)
            for id_cash_box, cash_box in cash_boxs.itertuples():
                field = "concat(client,': ', a_comment)" \
                        ", sum(amount_receipt + amount_expense) OVER (PARTITION BY currency, concat(client,': ', a_comment))"
                parameter_description = "%s AND kasa = '%s'" % (parameter_kasa, cash_box)
                sql = create_sql_universal(field, table_name, parameter_description)
                df = pd.read_sql(sql, conpg)
                sms_current = create_sms_movement(df)
                sms += '\n\n***** %s *****%s' % (cash_box, sms_current)

    except Exception as e:
        print("ERROR: OneC. %s" % e)

    finally:
        return sms

def create_sms_movement(df, sms=''):
    for row in df.itertuples():
        firm_name = row[1]
        amount = str("{:.2f}".format(row[2]))
        sms += "\n%s\n%s\n" % (firm_name, amount)

    return sms

def create_sms_rest(df):
    sms = ''
    for row in df.itertuples():
        firm_name = row.kasa
        currency = row.currency
        amount = str("{:.2f}".format(row.amount))
        if row.amount < 0:
            amount += ' (EKSI)'

        sms += "\n%s\n%s\n%s\n" % (firm_name, currency, amount)

    return sms

def main_one_c_cash_rest():
    # 1С - rest cash in 1C
    conpg = con_postgres_psycopg2()
    sql = sql_rest_of_cash()
    df = pd.read_sql(sql, conpg)
    sms = create_sms_rest(df)
    return sms

if __name__ == '__main__':
    # main_one_c_cash_rest()
    get_cash_expenses('01.02.2023')


# import the necessary packages
from threading import Thread
from multiprocessing import Process
import cv2
import YoloModel
import ArucoDetect

class WebcamVideoStream:
	def __init__(self, src=0, name="WebcamVideoStream",UseModel = False, UseAruco = False):
		# initialize the video camera stream and read the first frame
		# from the stream
		self.stream = cv2.VideoCapture(0)

		#self.stream = cv2.VideoCapture('1664152920.7081351basicvideo2.avi')
		self.stream.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
		self.stream.set(cv2.CAP_PROP_FRAME_HEIGHT, 320)

		(self.grabbed, self.frame) = self.stream.read()

		# initialize the thread name
		self.name = name

		# initialize the variable used to indicate if the thread should
		# be stopped
		self.stopped = False
		self.UseModel = UseModel
		self.UseAruco = UseAruco
		self.Model = YoloModel.Yolo()
		self.Aruco = ArucoDetect.Aruco()

	def start(self):
		# start the thread to read frames from the video stream
		t = Thread(target=self.update, name=self.name, args=())
		t.daemon = True
		t.start()
		#t = Process(target=self.update, args=())
		#t.start()
		#t.join()
		#self.update()

		return self

	def update(self):
		# keep looping infinitely until the thread is stopped

		while True:
			# if the thread indicator variable is set, stop the thread
			if self.stopped:
				print("stop 4")
				cv2.destroyAllWindows()
				self.stream.release()
				return

			# otherwise, read the next frame from the stream
			(self.grabbed, frame) = self.stream.read()
			self.frame = frame

			#if(self.UseAruco):
			#	self.Aruco.update(frame)

			#if(self.UseModel):
			#	self.frame = self.Model.update(frame)

			#if(self.UseAruco):
			#	self.Aruco.draw(frame)

			#Display image
			#cv2.imshow('WebcamVideo',frame)
			#cv2.waitKey(1)

	def read(self):
		# return the frame most recently read
		return self.frame, self.grabbed

	def stop(self):
		# indicate that the thread should be stopped
		print("stop 3")
		self.stopped = True
		self.close()

Drone Payload Project/Pi Code/WebcamVideoStreamOverride.py


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

from django.db import models, migrations
import django.db.models.deletion
import datetime

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Log',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('message', models.CharField(max_length=255)),
                ('ref_object', models.CharField(max_length=200)),
                ('type', models.CharField(max_length=25, blank=True)),
                ('level', models.IntegerField(choices=[(50, 'CRITICAL'), (40, 'ERROR'), (30, 'WARNING'), (20, 'INFO'), (10, 'DEBUG')])),
                ('spider_name', models.CharField(max_length=200)),
                ('date', models.DateTimeField(default=datetime.datetime.now)),
            ],
            options={
                'ordering': ['-date'],
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='LogMarker',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('message_contains', models.CharField(max_length=255)),
                ('ref_object', models.CharField(max_length=200, blank=True)),
                ('mark_with_type', models.CharField(help_text='Choose "Custom" and enter your own type in the next field for a custom type', max_length=2, choices=[('PE', 'Planned Error'), ('DD', 'Dirty Data'), ('IM', 'Important'), ('IG', 'Ignore'), ('MI', 'Miscellaneous'), ('CU', 'Custom')])),
                ('custom_type', models.CharField(max_length=25, blank=True)),
                ('spider_name', models.CharField(max_length=200, blank=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='SchedulerRuntime',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('runtime_type', models.CharField(default='P', max_length=1, choices=[('S', 'SCRAPER'), ('C', 'CHECKER')])),
                ('next_action_time', models.DateTimeField(default=datetime.datetime.now)),
                ('next_action_factor', models.FloatField(null=True, blank=True)),
                ('num_zero_actions', models.IntegerField(default=0)),
            ],
            options={
                'ordering': ['next_action_time'],
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='ScrapedObjAttr',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=200)),
                ('attr_type', models.CharField(max_length=1, choices=[('S', 'STANDARD'), ('T', 'STANDARD (UPDATE)'), ('B', 'BASE'), ('U', 'DETAIL_PAGE_URL'), ('I', 'IMAGE')])),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='ScrapedObjClass',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=200)),
                ('scraper_scheduler_conf', models.TextField(default='"MIN_TIME": 15,\n"MAX_TIME": 10080,\n"INITIAL_NEXT_ACTION_FACTOR": 10,\n"ZERO_ACTIONS_FACTOR_CHANGE": 20,\n"FACTOR_CHANGE_FACTOR": 1.3,\n')),
                ('checker_scheduler_conf', models.TextField(default='"MIN_TIME": 1440,\n"MAX_TIME": 10080,\n"INITIAL_NEXT_ACTION_FACTOR": 1,\n"ZERO_ACTIONS_FACTOR_CHANGE": 5,\n"FACTOR_CHANGE_FACTOR": 1.3,\n')),
                ('comments', models.TextField(blank=True)),
            ],
            options={
                'ordering': ['name'],
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Scraper',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=200)),
                ('status', models.CharField(default='P', max_length=1, choices=[('A', 'ACTIVE'), ('M', 'MANUAL'), ('P', 'PAUSED'), ('I', 'INACTIVE')])),
                ('content_type', models.CharField(default='H', max_length=1, choices=[('H', 'HTML'), ('X', 'XML')])),
                ('max_items_read', models.IntegerField(help_text='Max number of items to be read (empty: unlimited).', null=True, blank=True)),
                ('max_items_save', models.IntegerField(help_text='Max number of items to be saved (empty: unlimited).', null=True, blank=True)),
                ('pagination_type', models.CharField(default='N', max_length=1, choices=[('N', 'NONE'), ('R', 'RANGE_FUNCT'), ('F', 'FREE_LIST')])),
                ('pagination_on_start', models.BooleanField(default=False)),
                ('pagination_append_str', models.CharField(help_text='Syntax: /somepartofurl/{page}/moreurlstuff.html', max_length=200, blank=True)),
                ('pagination_page_replace', models.TextField(help_text=b"RANGE_FUNCT: uses Python range funct., syntax: [start], stop[, step], FREE_LIST: 'Replace text 1', 'Some other text 2', 'Maybe a number 3', ...", blank=True)),
                ('checker_type', models.CharField(default='N', max_length=1, choices=[('N', 'NONE'), ('4', '404'), ('X', '404_OR_X_PATH')])),
                ('checker_x_path', models.CharField(max_length=200, blank=True)),
                ('checker_x_path_result', models.CharField(max_length=200, blank=True)),
                ('checker_ref_url', models.URLField(max_length=500, blank=True)),
                ('comments', models.TextField(blank=True)),
                ('scraped_obj_class', models.ForeignKey(to='dynamic_scraper.ScrapedObjClass', on_delete=django.db.models.deletion.CASCADE)),
            ],
            options={
                'ordering': ['name', 'scraped_obj_class'],
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='ScraperElem',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('x_path', models.CharField(max_length=200)),
                ('reg_exp', models.CharField(max_length=200, blank=True)),
                ('from_detail_page', models.BooleanField(default=False)),
                ('processors', models.CharField(max_length=200, blank=True)),
                ('proc_ctxt', models.CharField(max_length=200, blank=True)),
                ('mandatory', models.BooleanField(default=True)),
                ('scraped_obj_attr', models.ForeignKey(to='dynamic_scraper.ScrapedObjAttr', on_delete=django.db.models.deletion.CASCADE)),
                ('scraper', models.ForeignKey(to='dynamic_scraper.Scraper', on_delete=django.db.models.deletion.CASCADE)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.AddField(
            model_name='scrapedobjattr',
            name='obj_class',
            field=models.ForeignKey(to='dynamic_scraper.ScrapedObjClass', on_delete=django.db.models.deletion.CASCADE),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='logmarker',
            name='scraper',
            field=models.ForeignKey(blank=True, to='dynamic_scraper.Scraper', null=True, on_delete=django.db.models.deletion.CASCADE),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='log',
            name='scraper',
            field=models.ForeignKey(blank=True, to='dynamic_scraper.Scraper', null=True, on_delete=django.db.models.deletion.CASCADE),
            preserve_default=True,
        ),
    ]