# -*- coding: utf-8 -*-
import sys, os

try:
    from setuptools import setup, find_packages
except ImportError:
    from ez_setup import use_setuptools
    use_setuptools()
    from setuptools import setup, find_packages

install_requires=[
    "TurboGears2 >= 2.3.9",
    "tgext.pluggable >= 0.7.1",
    "axf",
    "sprox",
    "tgapp-registration >= 0.9.0",
]

testpkgs = [
    'WebTest >= 1.2.3',
    'nose',
    'coverage',
    'ming >= 0.6.1',
    'sqlalchemy',
    'zope.sqlalchemy',
    'repoze.who',
    'tw2.forms',
    'kajiki',
    'tgext.evolve',
]

here = os.path.abspath(os.path.dirname(__file__))
try:
    README = open(os.path.join(here, 'README.rst')).read()
except IOError:
    README = ''

setup(
    name='tgapp-invitations',
    version='0.2.7',
    description='Provides a way to invite new users into a TurboGears2 application',
    long_description=README,
    author='Axant, Vincenzo Castiglia',
    author_email='vincenzo.castiglia@axant.it',
    url='https://github.com/axant/tgapp-invitations',
    keywords='turbogears2.application',
    packages=find_packages(exclude=['ez_setup']),
    install_requires=install_requires,
    include_package_data=True,
    package_data={'invitations': [
        'i18n/*/LC_MESSAGES/*.mo',
        'templates/*/*',
        'public/*/*'
    ]},
    message_extractors={'invitations': [
            ('**.py', 'python', None),
            ('templates/**.xhtml', 'kajiki', None),
            ('public/**', 'ignore', None)
    ]},
    entry_points="""
    """,
    zip_safe=False,
    extras_require={
        'testing': testpkgs,
    },
)


import telebot

from books.books import create_book
from settings import group_token, book_name
from keyboard import create_keyboard

bot = telebot.TeleBot(group_token)
book = create_book(book_name)

def reply_to_message(message, next_page):
    page_data = book.get_page(next_page)
    keyboard = create_keyboard(page_data)
    bot.send_message(message.chat.id, page_data["text"], reply_markup=keyboard)

@bot.message_handler(commands=["start"])
def start(message):
    reply_to_message(message, 1)

@bot.message_handler(content_types=["text"])
def start(message):
    reply_to_message(message, 1)

@bot.callback_query_handler(lambda query: True)
def callback_query_handler(data):
    next_page = int(data.data)
    reply_to_message(data.message, next_page)
    return True

bot.polling(none_stop=True, interval=0)


# standard

# pypi
import flask
from flask import current_app, abort
from flask.views import MethodView

# home grown
from . import bp
from ...accesscontrol import owner_permission
from ...model import db   # this is ok because this module only runs under flask

# module specific needs
from ... import version

class testException(Exception): pass

class ViewDebug(MethodView):

    def get(self):
        try:
            # only owner can do this
            if not owner_permission.can():
                return abort(403)

            thisversion = version.__version__
            appconfigpath = getattr(current_app,'configpath','<not set>')
            appconfigtime = getattr(current_app,'configtime','<not set>')

            # collect groups of system variables
            sysvars = []

            # collect app.config variables
            configkeys = sorted(list(current_app.config.keys()))
            appconfig = []
            for key in configkeys:
                value = current_app.config[key]
                if not owner_permission.can():
                    if key in ['SQLALCHEMY_DATABASE_URI','SECRET_KEY']:
                        value = '<obscured>'
                appconfig.append({'label':key, 'value':value})
            sysvars.append(['current_app.config',appconfig])

            # collect flask.session variables
            sessionkeys = list(flask.session.keys())
            sessionkeys.sort()
            sessionconfig = []
            for key in sessionkeys:
                value = flask.session[key]
                sessionconfig.append({'label':key, 'value':value})
            sysvars.append(['flask.session',sessionconfig])

            # commit database updates and close transaction
            db.session.commit()
            return flask.render_template('sysinfo.html',pagename='Debug',
                                         version=thisversion,
                                         configpath=appconfigpath,
                                         configtime=appconfigtime,
                                         sysvars=sysvars,
                                         owner=owner_permission.can(),
                                         inhibityear=True,inhibitclub=True)

        except:
            # roll back database updates and close transaction
            db.session.rollback()
            raise
bp.add_url_rule('/_debuginfo',view_func=ViewDebug.as_view('debug'),methods=['GET'])

class TestException(MethodView):

    def get(self):
        try:
            raise testException

        except:
            # roll back database updates and close transaction
            db.session.rollback()
            raise
bp.add_url_rule('/xcauseexception',view_func=TestException.as_view('testexception'),methods=['GET'])


import numpy as np
import pytest
import yaml

import pineko

@pytest.mark.parametrize("theoryid,is_mixed", [(400, False), (208, True)])
def benchmark_produce_fonll_tcards(
    tmp_path, test_files, test_configs, theoryid, is_mixed
):
    tcard = pineko.theory_card.load(theoryid)
    tcard_paths_list = pineko.fonll.dump_tcards(tcard, tmp_path, theoryid)
    # Check they are correct
    theorycards = []
    for path in tcard_paths_list:
        with open(path, encoding="UTF-8") as f:
            theorycards.append(yaml.safe_load(f))
    base_pto = pineko.fonll.FNS_BASE_PTO[tcard["FNS"]]
    for num_fonll_tcard, cfg in zip(
        theorycards,
        pineko.fonll.MIXED_FNS_CONFIG if is_mixed else pineko.fonll.FNS_CONFIG,
    ):
        po = int(base_pto) + (cfg.delta_pto if is_mixed else 0)
        assert num_fonll_tcard["FNS"] == cfg.scheme
        assert num_fonll_tcard["NfFF"] == int(cfg.nf)
        assert num_fonll_tcard["PTO"] == po - 1 if is_mixed and cfg.asy else po
        assert num_fonll_tcard["FONLLParts"] == cfg.parts


import pandas as pd
pd.options.mode.chained_assignment = None

import numpy as np
import xml.etree.ElementTree as ET
import os
import pickle
from pathlib import Path
from joblib import Parallel, delayed

def format(data):
    data_frame = pd.DataFrame()
    data_frame["A_ID"] = list(data["A_ID"])
    data_frame["B_ID"] = list(data["B_ID"])
    data_frame["TD"] = data["TD"]
    data_frame["td_offset"] = data["TD_OFFSET"]
    data_frame["CIR_R"] = list(data["CIR_R"])
    data_frame["CIR_I"] = list(data["CIR_I"])
    data_frame["Ref_x"] = data["POS_X"]
    data_frame["Ref_y"] = data["POS_Y"]
    data_frame["Time_stamp"] = data["TIME_STAMP"]

    return data_frame

def _get_params(config_file_path):
    """
    Gets the parameters of the input data of the given XML object.

    Parameters
    ----------
    config : string
        Path to config file

    Returns
    -------
    cir_time_res : float
        Time resolution of the channel impulse response (CIR)
    anchor_count : int
        Number of anchors.
    td_value_range : [min_td, max_td]
        Value range which defined the area of valid values of TDOA / TOA values.

    """
    config = ET.parse(config_file_path)
    config_root = config.getroot()

    cir_time_res = None
    anchor_count = None

    # Gets the attributes from the xml tree
    for child in config_root:
        if child.tag == "CIR_time_res":
            cir_time_res = float(child.attrib['value'])

        if child.tag == "Anchor_count":
            anchor_count = int(child.attrib['value'])

        if child.tag == "TD_value_range":
            td_value_range = (float(child.attrib['min']), float(child.attrib['max']))

    return cir_time_res, anchor_count, td_value_range

def preprocess_data(data, config_file_path, output_folder):
    """
    Preprocess data and store the preprocessed data in the given output folder.
    The data is stored as pickle file with tuples of (data, labels, record_times) for every dataset.

    Parameters
    ----------
    data : dictonary
        Dictionary of { dataset_name : formatted_dataset }
    config_file_path : String
        Path to the parameters file for the formatted data
    output_folder : String
        Path for the output directory

    Returns
    -------
    None.

    """
    # Make path strings OS independent
    config_file_path = Path(config_file_path)
    output_folder = Path(output_folder)

    # Create a output directory for every evaluation scenario
    if os.path.exists(output_folder) == False:
        os.makedirs(output_folder)

    preprocessed_data = {}

    # Store preprocessed subsets as pickle files with labels
    for name, dataset in data.items():
        pre_sub_data, sub_labels, sub_times = preprocess(dataset, config_file_path)

        preprocessed_data[name] = [pre_sub_data, sub_labels, sub_times]

    # Estimate normalization factor from given subsets
    normalization_factor = -float('inf')
    norm_factor_path = output_folder / Path("normalization_factor")

    if not norm_factor_path.is_file():
        for _,dataset in preprocessed_data.items():

            max_subset = np.amax(np.absolute(dataset[0]))
            if max_subset > normalization_factor:
                normalization_factor = max_subset

        pickle.dump(normalization_factor, open(norm_factor_path, "wb"))
    else:
        normalization_factor = pickle.load(open(norm_factor_path, "rb"))

    for name, preprocessed_subset in preprocessed_data.items():
        preprocessed_subset[0] = preprocessed_subset[0] / normalization_factor

        output_path = output_folder / Path(name)
        output_path.parent.mkdir(parents=True, exist_ok=True)

        with open(output_path, 'wb') as sub_data_file:
            pickle.dump(preprocessed_subset, sub_data_file)

    return preprocessed_data

def preprocess(data, config_file_path):
    """
    Preprocesses the data for DL positioning as 3D tensors for every burst.
    Every burst contains following data: [Anchor, CIR, Real/Imaginary]

    Parameters
    ----------
    data : Pandas dataframe
        Data in the standardized format
    config_file_path : string (Path to *.xml file)
        Path to the configuration file containing the parameters for preprocessing
    use_mag : Boolean
        Determine if real/imaginary part or Magnitude is used
    use_td : Boolean
        Determine if CIR has to be aligned by TOA/TDOA

    Returns
    -------
    processed_data : List of numpy arrays.
        Preprocessed data for DL positioning.

    labels : List [[x,y], ...]
        Labels for positioning. In this case 2D positions of the tag.

    times : List
        Timestamps of recording in seconds

    """

    # Get parameters for preprocessing
    cir_time_res, anchor_count, td_value_range = _get_params(config_file_path)

    first_item = data.iloc[0]
    cir_length = len(first_item.CIR_R)

    # Remove all samples, which are not within the valid range
    valid_data = data[(data.TD >= td_value_range[0]) & (data.TD <= td_value_range[1])]
    valid_data = valid_data[(valid_data.td_offset >= 0) & (valid_data.td_offset <= cir_length * cir_time_res)]

    # Remove incomplete bursts
    burst_groups = valid_data.groupby(by=["B_ID"], as_index=False)

    valid_data = burst_groups.filter(lambda x: len(x) == anchor_count)

    # Get IDs of the anchors
    anchors = valid_data.A_ID.unique()

    # Sort anchors to ensure the same order for every dataset
    anchors = sorted(anchors)

    # Get the window size of the output tensor
    # Window size is defined by the valid value range.
    window_size = int(round((td_value_range[1] - td_value_range[0]) / cir_time_res))

    processed_data = []
    labels = []
    times = []

    burst_groups = valid_data.groupby(by=["B_ID"], as_index=False)

    # Preprocess bursts in parallel
    result = Parallel(n_jobs=-1, verbose=10)(delayed(get_burst_data)(burst, anchors, window_size, td_value_range, cir_time_res) for _,burst in burst_groups)

    processed_data = np.array([item[0] for item in result])
    labels = np.array([item[1] for item in result])
    times = np.array([item[2] for item in result])

    return np.array(processed_data).astype('float32'), np.array(labels).astype('float32'), times

def get_burst_data(burst, anchors, window_size, td_value_range, cir_time_res):
    """
    Gets the burst as 2D or 3D array for fingerprinting. A padding is performed of the CIRs in a input tensor of dimension [2, num_anchors, window_size], wheres 2 is for Real/Imag.
    If the magnitude should be used, the tensor is of size [num_anchors, window_size].

    Parameters
    ----------
    burst : pandas Dataframe
        Anchor information within the burst
    use_td : Boolean
        Determine if CIR has to be aligned by TOA/TDOA
    cir_time_res : float
        Time resolution of the channel impulse response (CIR)
    anchors : list
        Anchor ids
    td_value_range : [min_td, max_td]
        Value range which defined the area of valid values of TDOA / TOA values.
    cir_time_res : float
        Resolution of the input CIR
    window_size : int
        Size of the window the CIR is embedded in the burst tensor

    Returns
    -------

    """

    anchor_count = len(anchors)

    burst_data = np.zeros((anchor_count, window_size))

    for a_idx, anchor in enumerate(anchors):

        # Get data of the current anchor within the burst
        anch_data = burst[burst.A_ID == anchor].iloc[0]

        # Get shift in indices of the target time resolution of the TOA peak within the CIR
        cir_td_offset = int(round((anch_data['td_offset'] * (1/cir_time_res))))

        # Get shift in time indices from TD
        td_offset = int(round((anch_data.TD - td_value_range[0]) / cir_time_res))

        cir_shifted = np.zeros(td_offset + len(anch_data.CIR_R)+window_size)

        cir_mag = np.linalg.norm(np.vstack((np.array(anch_data.CIR_R), np.array(anch_data.CIR_I))), axis=0)
        cir_shifted[td_offset:td_offset+len(anch_data.CIR_R)] = cir_mag

        burst_data[a_idx, :window_size] = cir_shifted[cir_td_offset:window_size+cir_td_offset]

    return (burst_data, np.array([burst.Ref_x.mean(), burst.Ref_y.mean()]), burst.Time_stamp.mean())


#!/usr/bin/python
from PIL import Image
import os, sys
from os.path import isfile, join

path = os.getcwd()+'/original/'
print(path)
dirs = os.listdir(path)

for f in dirs:
    print(f)

    # Opens a image in RGB mode
    im = Image.open(path+f)
    newsize = (540, 360)
    im = im.resize(newsize)
    # Shows the image in image viewer
    im.save('tested.jp2', "JPEG2000")


from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name="index"),
    path('ucitavanje/<str:id>/<str:parameters>/', views.ucitavanje_plugin, name="ucitavanje_plugin"),
    path('ucitavanje/<str:id>/', views.ucitavanje_plugin, name="ucitavanje_plugin_bez_param"),
    path('prikaz/<str:id>', views.prikaz_plugin, name="prikaz_plugin"),

    path('dobavi_podatke', views.dobavi_podatke, name='getGraph'),
    path('prikaz/', views.prikaz_komponenta, name="prikaz_komponenta"),

]


from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware

from fastapi_jwt_auth .exceptions import AuthJWTException

# from .product_photo.endpoint import router as product_photo_router

from basket.endpoint import router as basket_router
from product_category.endpoint import router as category_router
from user.endpoints import router as user_router
from product.endpoint import router as product_router

from db import database

app = FastAPI()

origins = [
    "http://localhost",
    "http://localhost:4200",
    "http://127.0.0.1:4200/login"
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
    allow_credentials=True,
)

app.state.database = database

@app.on_event("startup")
async def startup() -> None:
    database_ = app.state.database

    if not database_.is_connected:
        print("Connecting to database")
        await database_.connect()

@app.on_event("shutdown")
async def shutdown() -> None:
    database_ = app.state.database

    if database_.is_connected:
        print("Disconnecting from  database")
        await database_.disconnect()

@app.exception_handler(AuthJWTException)
def authjwt_exception_handler(request: Request, exc: AuthJWTException):
    return JSONResponse(
        status_code=exc.status_code,
        content={"detail": exc.message}
    )

app.include_router(category_router, tags=["category router"], prefix="/category")
app.include_router(user_router, tags=["user router"], prefix="/user")
app.include_router(basket_router, tags=["basket router"], prefix="/basket")
app.include_router(product_router, tags=["product router"], prefix="/product")
# app.include_router(product_photo_router, tags=["photo"], prefix="/photo")

# app.mount("/static", StaticFiles(directory="static"), name="static")


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

from gridfs import GridFS
from pymongo import MongoClient

from .test_backend_mixin import BackendTestCase

from flask_fs.backends.gridfs import GridFsBackend
from flask_fs.storage import Config

import pytest
import mimetypes
import six

TEST_DB = 'fstest'

class GridFsBackendTest(BackendTestCase):
    hasher = 'md5'

    @pytest.fixture
    def pngimage(self, pngfile):
        with open(pngfile, 'rb') as f:
            yield f

    @pytest.fixture
    def jpgimage(self, jpgfile):
        with open(jpgfile, 'rb') as f:
            yield f

    @pytest.fixture(autouse=True)
    def setup(self):
        self.client = MongoClient()
        self.db = self.client[TEST_DB]
        self.gfs = GridFS(self.db, 'test')

        self.config = Config({
            'mongo_url': 'mongodb://localhost:27017',
            'mongo_db': TEST_DB,
        })
        self.backend = GridFsBackend('test', self.config)
        yield
        self.client.drop_database(TEST_DB)

    def put_file(self, filename, content):
        self.gfs.put(content, filename=filename, encoding='utf-8')

    def get_file(self, filename):
        file = self.gfs.get_last_version(filename)
        assert file is not None
        return file.read()

    def file_exists(self, filename):
        return self.gfs.exists(filename=filename)

    def test_default_bucket(self):
        backend = GridFsBackend('test_bucket', self.config)
        assert backend.fs._GridFS__collection.name == 'test_bucket'

    def test_config(self):
        assert self.backend.client.address == ('localhost', 27017)
        assert self.backend.db.name == TEST_DB

    def test_delete_with_versions(self, faker):
        filename = 'test.txt'
        self.put_file(filename, faker.sentence())
        self.put_file(filename, faker.sentence())
        assert self.gfs.find({'filename': filename}).count() == 2

        self.backend.delete(filename)
        assert not self.file_exists(filename)

    def test_write_pngimage(self, pngimage, utils):
        filename = 'test.png'
        content = six.binary_type(pngimage.read())
        content_type = mimetypes.guess_type(filename)[0]
        f = utils.filestorage(filename, content, content_type)
        self.backend.write(filename, f)

        with self.backend.open(filename, 'rb') as f:
            assert f.content_type == content_type

        self.assert_bin_equal(filename, content)

    def test_write_jpgimage(self, jpgimage, utils):
        filename = 'test.jpg'
        content = six.binary_type(jpgimage.read())
        content_type = mimetypes.guess_type(filename)[0]
        f = utils.filestorage(filename, content, content_type)
        self.backend.write(filename, f)

        with self.backend.open(filename, 'rb') as f:
            assert f.content_type == content_type

        self.assert_bin_equal(filename, content)


import time
import uuid

import pytest

def test_create_delete_plugin(openbis_instance):
    timestamp = time.strftime('%a_%y%m%d_%H%M%S').upper()
    pl_name = 'test_plugin_' + timestamp

    plugin = openbis_instance.new_plugin(
	name= pl_name,
	pluginType='ENTITY_VALIDATION',   # or 'DYNAMIC_PROPERTY' or 'MANAGED_PROPERTY',
	entityKind = None,                # or 'SAMPLE', 'MATERIAL', 'EXPERIMENT', 'DATA_SET'
	script = 'def calculate(): pass'  # JYTHON script
    )

    with pytest.raises(ValueError):
        plugin.pluginType = 'Rubbish'
        assert 1 == 0

    with pytest.raises(ValueError):
        plugin.entityKind = 'even more Rubbish'
        assert 1 == 0

    plugin.save()
    pl_exists = openbis_instance.get_plugin(pl_name)
    assert pl_exists is not None

    plugin.delete("test on {}".format(timestamp))

    with pytest.raises(ValueError):
        pl_not_exists = openbis_instance.get_plugin(plugin.name)
        assert pl_not_exists is None

def test_search_plugins(openbis_instance):
    plugins = openbis_instance.get_plugins(start_with=1, count=1)
    assert len(plugins) == 1
    assert plugins.df.__class__.__name__ == 'DataFrame'
    assert len(plugins) == len(plugins.df)

    plugin = plugins[0]
    assert plugin.__class__.__name__ == 'Plugin'

def test_assign_plugin_to_sample_type(openbis_instance):

    timestamp = time.strftime("%a_%y%m%d_%H%M%S").upper()

    plugin_name = 'TEST.NEW_PLUGIN_' + timestamp + "_" + str(uuid.uuid4())
    pl = openbis_instance.new_plugin(
        name=plugin_name,
        pluginType='DYNAMIC_PROPERTY',
        entityKind='SAMPLE',
        script='def validate(x, True):\n\treturn "OK"'
    )
    pl.save()

    sample_type_code = 'SOME_TEST_SAMPLE_TYPE_' + timestamp + "_" + str(uuid.uuid4())
    sample_type = openbis_instance.new_sample_type(
        code=sample_type_code,
        generatedCodePrefix='STST-',
        autoGeneratedCode=True
    )
    sample_type.save()

    sample_type = openbis_instance.get_sample_type(sample_type_code)
    assignments = sample_type.get_property_assignments().df

    assert len(assignments) == 0

    property_code = 'SOME_TEST_PROPERTY_' + timestamp + "_" + str(uuid.uuid4())
    prop_type = openbis_instance.new_property_type(
        code=property_code,
        label='test property',
        description='test property',
        dataType='VARCHAR',
    )
    prop_type.save()

    sample_type.assign_property(prop_type, section='General info', ordinal=2,
                                initialValueForExistingEntities='', plugin=plugin_name)

    assignments = sample_type.get_property_assignments().df

    assert len(assignments) == 1
    assert assignments.loc[0]['plugin'] == plugin_name

    sample = openbis_instance.new_sample(
        type=sample_type.code,
        space='DEFAULT',
        experiment='/DEFAULT/DEFAULT/DEFAULT',
        props={property_code.lower(): "some_value"}
    )
    sample.save()

    sample_type.revoke_property(property_code, force=True)

    assignments = sample_type.get_property_assignments().df
    assert len(assignments) == 0


import os
from os import path as p
import pandas as pd
import numpy as np
import json
import random
import argparse

def get_mokoron():
    """
    Function to load and prepare mokoron-sentiment dataset

    Returns:
        dict: {"positive": positive sentiments, "negative": negative sentiments}

    """
    mokoron_negative = pd.read_csv(p.join(data_dir, 'mokoron', 'negative.csv'), sep=';', comment='#')
    mokoron_positive = pd.read_csv(p.join(data_dir, 'mokoron', 'positive.csv'), sep=';', comment='#')

    mokoron_negative = mokoron_negative.iloc[:, 3]
    mokoron_positive = mokoron_positive.iloc[:, 3]

    mokoron_negative.columns = ['text']
    mokoron_positive.columns = ['text']

    mokoron_negative = mokoron_negative.values
    mokoron_positive = mokoron_positive.values

    mokoron = {"positive": mokoron_positive, "negative": mokoron_negative}
    return mokoron

def get_rusentiment():
    """
    Function to load and prepare rusentiment dataset

    Returns:
        dict: {"positive": positive sentiments, "negative": negative sentiments, "neutral": neutral sentiments}

    """
    rusentiment_random_posts = pd.read_csv(p.join(data_dir, 'rusentiment', 'rusentiment_random_posts.csv'))
    rusentiment_test = pd.read_csv(p.join(data_dir, 'rusentiment', 'rusentiment_test.csv'))
    rusentiment_preselected_posts = pd.read_csv(p.join(data_dir, 'rusentiment', 'rusentiment_preselected_posts.csv'))

    rusentiment_positive = pd.concat([rusentiment_random_posts[rusentiment_random_posts['label'] == 'positive'], \
                                      rusentiment_test[rusentiment_test['label'] == 'positive'], \
                                      rusentiment_preselected_posts[
                                          rusentiment_preselected_posts['label'] == 'positive']]).drop('label',
                                                                                axis=1).values.squeeze(
        -1)

    rusentiment_negative = pd.concat([rusentiment_random_posts[rusentiment_random_posts['label'] == 'negative'], \
                                      rusentiment_test[rusentiment_test['label'] == 'negative'], \
                                      rusentiment_preselected_posts[
                                          rusentiment_preselected_posts['label'] == 'negative']]).drop('label',
                                                                                axis=1).values.squeeze(
        -1)

    rusentiment_neutral = pd.concat([rusentiment_random_posts[rusentiment_random_posts['label'] == 'neutral'], \
                                     rusentiment_test[rusentiment_test['label'] == 'neutral'], \
                                     rusentiment_preselected_posts[
                                         rusentiment_preselected_posts['label'] == 'neutral']]).drop('label',
                                                                                axis=1).values.squeeze(
        -1)

    rusentiment = {"positive": rusentiment_positive, "negative": rusentiment_negative, "neutral": rusentiment_neutral}
    return rusentiment

def get_rureviews():
    """
    Function to load and prepare rureviews-sentiment dataset

    Returns:
        dict: {"positive": positive sentiments, "negative": negative sentiments, "neutral": neutral sentiments}

    """
    rureviews = pd.read_csv(p.join(data_dir, 'rureviews', 'women-clothing-accessories.3-class.balanced.csv'), sep='\t')

    rureviews_positive = (rureviews[rureviews['sentiment'] == 'positive']).drop(['sentiment'], axis=1).values.squeeze(
        -1)
    rureviews_negative = (rureviews[rureviews['sentiment'] == 'negative']).drop(['sentiment'], axis=1).values.squeeze(
        -1)
    rureviews_neutral = (rureviews[rureviews['sentiment'] == 'neautral']).drop(['sentiment'], axis=1).values.squeeze(-1)

    rureviews = {"positive": rureviews_positive, "negative": rureviews_negative, "neutral": rureviews_neutral}
    return rureviews

def get_medical():
    """
    Function to load and prepare medical-sentiment dataset

    Returns:
        dict: {"positive": positive sentiments, "negative": negative sentiments, "neutral": neutral sentiments}

    """
    medical_comments = pd.read_csv(p.join(data_dir, 'medical_comments', 'medical_comments.csv'))

    medical_positive = (medical_comments[
        (medical_comments['sentiment'] == ' Отлично') | (medical_comments['sentiment'] == ' Хорошо')]).drop(
        ['sentiment'], axis=1).values.squeeze(-1)
    medical_negative = (
        medical_comments[
            (medical_comments['sentiment'] == ' Ужасно') | (medical_comments['sentiment'] == ' Плохо')]).drop(
        ['sentiment'], axis=1).values.squeeze(-1)
    medical_neutral = (medical_comments[(medical_comments['sentiment'] == ' Нормально')]).drop(['sentiment'],
                                                                                axis=1).values.squeeze(
        -1)

    medical = {"positive": medical_positive, "negative": medical_negative, "neutral": medical_neutral}
    return medical

def get_kaggle():
    """
    Function to load and prepare kaggle-sentiment dataset

    Returns:
        dict: {"positive": positive sentiments, "negative": negative sentiments, "neutral": neutral sentiments}

    """
    with open(p.join(data_dir, 'kaggle-sentiment', 'train.json'), 'r', encoding='utf-8') as f:
        data = json.load(f)

    kaggle_positive = []
    kaggle_negative = []
    kaggle_neutral = []

    for line in data:
        if line['sentiment'] == 'positive':
            kaggle_positive.append(line['text'])
        elif line['sentiment'] == 'negative':
            kaggle_negative.append(line['text'])
        else:
            kaggle_neutral.append(line['text'])

    kaggle_positive = np.array(kaggle_positive)
    kaggle_negative = np.array(kaggle_negative)
    kaggle_neutral = np.array(kaggle_neutral)

    kaggle = {"positive": kaggle_positive, "negative": kaggle_negative, "neutral": kaggle_neutral}

    return kaggle

def collect_dataset(chosen_datasets):
    """
    Function to collect all chosen datasets in one
    Args:
        chosen_datasets (list): List of names of choden datasets

    Returns:
        sought_data (list): [{"text": text_data, "label": label of sentiment},]
    """
    datasets_f = {'mokoron': get_mokoron,
                  'rureviews': get_rureviews,
                  'medical': get_medical,
                  'rusentiment': get_rusentiment,
                  'kaggle': get_kaggle}

    sought_data = []

    for dataset_name in chosen_datasets:
        dataset = datasets_f[dataset_name]()
        for sentiment in dataset:
            for sample in dataset[sentiment]:
                sought_data.append({'text': sample, 'label': sentiment})

    random.shuffle(sought_data)
    return sought_data

def split_dataset(data, val_split=0.01):
    """
    Function to split dataset into train and validation
    Args:
        data (list): [{"text": text_data, "label": label of sentiment},]

    Returns:
        train (list): [{"text": text_data, "label": label of sentiment},]
        test (list): [{"text": text_data, "label": label of sentiment},]
    """
    treshold = int(len(data) * val_split)
    return data[treshold:], data[:treshold]

def save_dataset(data, path):
    """
    Function to save dataset
    Args:
        data (list): [{"text": text_data, "label": label of sentiment},]
        path (str): path to saving directiory

    """
    with open(path, 'w', encoding='utf-8') as outfile:
        for entry in data:
            json.dump(entry, outfile, ensure_ascii=False)
            outfile.write('\n')

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Glueing and splitting datasets')
    parser.add_argument('--data_directory', type=str, default='sentiment-datasets', help='Path to data directory')
    parser.add_argument('--val_split', type=float, default=0.01, help='Validation proportion')
    parser.add_argument("--mokoron", type=bool, const='True', nargs='?', default=False, help="Use mokoron dataset")
    parser.add_argument("--rureviews", type=bool, const='True', nargs='?', default=False, help="Use rureviews dataset")
    parser.add_argument("--medical", type=bool, const='True', nargs='?', default=False, help="Use medical dataset")
    parser.add_argument("--rusentiment", type=bool, const='True', nargs='?', default=False,
                        help="Use rusentiment dataset")
    parser.add_argument("--kaggle", type=bool, const='True', nargs='?', default=False, help="Use kaggle dataset")

    args = parser.parse_args()

    data_dir = args.data_directory

    datasets_list = ['mokoron', 'rureviews', 'medical', 'rusentiment', 'kaggle']
    datasets_mask = [args.mokoron, args.rureviews, args.medical, args.rusentiment, args.kaggle]
    chosen_datasets = [datasets_list[i] for i in range(len(datasets_list)) if datasets_mask[i]]

    dataset = collect_dataset(chosen_datasets)
    train, validation = split_dataset(dataset, args.val_split)

    save_dataset(train, p.join(data_dir,'train.jsonl'))
    save_dataset(validation, p.join(data_dir, 'val.jsonl'))


from fastapi import (
    APIRouter,
    Depends,
    status,
    Response,
)

from app.schemas import users, products
from app.crud.auth import RoleChecker
from app.crud.admin import AdminService
from typing import List

router = APIRouter(
    prefix='/admin',
    tags=['admin'],
)
allow_access_resource = RoleChecker(['admin'])

@router.get(
    '/products',
    response_model=List[products.ProductInformation],
    dependencies=[Depends(allow_access_resource)],
)
def get_products(
    admin_service: AdminService = Depends(),
):
    return admin_service.get_all_products()

@router.post(
    '/products',
    response_model=products.Product,
    dependencies=[Depends(allow_access_resource)],
    status_code=status.HTTP_201_CREATED,
)
def create_products(
    product_data: products.ProductCreate,
    admin_service: AdminService = Depends(),
):
    return admin_service.create_product(product_data)

@router.get(
    '/products/{product_id}',
    response_model=products.Product,
    dependencies=[Depends(allow_access_resource)],
)
def get_product(
    product_id: int,
    admin_service: AdminService = Depends(),
):
    return admin_service.get_product(product_id)

@router.put(
    '/products/{product_id}',
    response_model=products.Product,
    dependencies=[Depends(allow_access_resource)],
)
def update_product(
    product_id: int,
    product_data: products.ProductUpdate,
    admin_service: AdminService = Depends(),
):
    return admin_service.update_product(product_id, product_data)

@router.delete(
    '/products/{product_id}',
    dependencies=[Depends(allow_access_resource)],
    status_code=status.HTTP_204_NO_CONTENT,
)
def delete_product(
    product_id: int,
    admin_service: AdminService = Depends(),
):
    admin_service.delete_product(product_id)
    return Response (status_code=status.HTTP_204_NO_CONTENT)

@router.get(
    '/users',
    response_model = List[users.UserAccounts],
    dependencies=[Depends(allow_access_resource)],
)
def get_all_users(
    admin_service: AdminService = Depends(),
):
    return admin_service.get_all_users()

@router.get(
    '/users/{user_id}',
    response_model = users.UserAccounts,
    dependencies=[Depends(allow_access_resource)],
)
def get_users(
    user_id: int,
    admin_service: AdminService = Depends(),
):
    return admin_service.get_user(user_id)

@router.put(
    '/users/{user_id}',
    response_model = users.UserAccounts,
)
def status_users(
        user_id: int,
        status_user: users.UserStatus,
        admin_service: AdminService = Depends(),
):
    return admin_service.status_user(user_id, status_user.is_active)


import cv2 as cv

img = cv.imread('WEEK_1/01.jpg')
# png파일을 오버레이를 입힐때는 IMREAD_UNCHANGED 사용
overlay_img = cv.imread('WEEK_1/dices.png' , cv.IMREAD_UNCHANGED)

# 이미지 크기 변환
overlay_img = cv.resize(overlay_img, dsize=(150, 150))

# 투명도 Aplha 구하기
overlay_alpha = overlay_img[:, :, 3:] / 255.0
background_alpha = 1.0 - overlay_alpha

# 이미지 합성하기
x1 = 100
y1 = 100
x2 = x1 + 150
y2 = y1 + 150

img[y1:y2, x1:x2] = overlay_alpha * overlay_img[:, :, :3] + background_alpha * img[y1:y2, x1:x2]

cv.imshow('img',img)
cv.waitKey(0)


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('meat', '0002_auto_20180304_1118'),
    ]

    operations = [
        migrations.AlterField(
            model_name='order_on_the_site_model',
            name='client',
            field=models.CharField(verbose_name='Кліент - ', default='', max_length=256),
        ),
    ]


from django.urls import path

from .views import (
    ItemListView,
    ItemDetailView,
    ItemUpdateView,
    ItemCreateView
)

app_name = 'restaurants'

urlpatterns = [
    path('', ItemListView.as_view(), name='list'),
    path('create/', ItemCreateView.as_view(), name='create'),
    # path('<int:pk>/', ItemDetailView.as_view(), name="detail"),
    path('<int:pk>/edit', ItemUpdateView.as_view(), name="detail"),
]


import json
from argparse import ArgumentParser, Namespace

from idb.cli.commands.base import TargetCommand
from idb.common.types import IdbClient

class DylibInstallCommand(TargetCommand):
    @property
    def description(self) -> str:
        return "Install an dylib"

    @property
    def name(self) -> str:
        return "install"

    def add_parser_arguments(self, parser: ArgumentParser) -> None:
        parser.add_argument("dylib_path", help="Path to the dylib to install", type=str)
        super().add_parser_arguments(parser)

    async def run_with_client(self, args: Namespace, client: IdbClient) -> None:
        artifact = await client.install_dylib(args.dylib_path)
        if args.json:
            print(json.dumps({"dylib": artifact.name, "uuid": artifact.uuid}))
        else:
            print(f"Installed: {artifact.name} {artifact.uuid}")


# raspberry pi imports
import busio
import board

# environment variables setup
import os

# raspberry pi setup
I2C_BUS = busio.I2C(board.SCL, board.SDA)

# magnetometer import
import adafruit_mlx90393

# magnetometer setup
SENSOR = adafruit_mlx90393.MLX90393(I2C_BUS, gain=adafruit_mlx90393.GAIN_1X)

# time imports
import time
from datetime import datetime
import pytz

# time setup
zoneName = os.environ['TIMEZONE']
timezone = pytz.timezone(zoneName)
currentTime = datetime.now(tz=timezone)

# twilio imports
from twilio.rest import Client

# twilio setup
accountSid = os.environ['TWILIO_ACCOUNT_SID']
authToken = os.environ['TWILIO_AUTH_TOKEN']
twilioPhone = os.environ['TWILIO_PHONE_NUMBER']
recipient01 = os.environ['RECIPIENT_01']
recipient02 = os.environ['RECIPIENT_02']

# laps setup
magnet = False
laps = 0
distance = 0

# when running this script
while True:
    # set up x, y, z axes for the sensor
    MX, MY, MZ = SENSOR.magnetic

    # check for significant changes in axis z
    if (MZ < -500 or MZ > 500):
        print("MAGNET SENSED", MX, "  Y", MY,"  Z", MZ)
        # the magnet is near
        magnet = True
    else:
        print(MX," ",MY,"  ",MZ)
        # the magnet is far
        if magnet == True:
            magnet = False
            laps = laps + 1

   # update recipients every morning at 9am local time
    if currentTime.isoformat()[11:19] == "09:00:00":
        # hedgehog wheel circumference is 37 inches
        distance = (laps*37)/63360
        print (distance, " miles have been run")

        # text recipients the miles ran
        if (distance > 3):
            message = client.messages.create(
                body='Good Morning! Hedgie ran ' + str(distance) + ' miles last night. What a good boy.',
                from_=twilioPhone,
                to=recipient01
            )
            message = client.messages.create(
                body='Good Morning! Heggie ran ' + str(distance) + ' miles last night. What a good boy.',
                from_=twilioPhone,
                to=recipient02
            )

        else:
            message = client.messages.create(
                body='Hedgie ran ' + str(distance) + ' miles last night. We should check on him.',
                from_=twilioPhone,
                to=recipient01
            )
            message = client.messages.create(
                body='Alert! Alert! Heggie ran ' + str(distance) + ' miles last night. Alert!',
                from_=twilioPhone,
                to=recipient02
            )
        # reset laps and distance each morning after sending the text
        laps = 0
        distance = 0

    # sensor status displayed when script is stopped
    if SENSOR.last_status > adafruit_mlx90393.STATUS_OK:
        SENSOR.display_status()

    # check for magnet every quarter second
    time.sleep(0.25)


from aiogram.types import KeyboardButton, ReplyKeyboardMarkup

b1 = KeyboardButton("/Режим_работы")
b2 = KeyboardButton("/Расположение")
b3 = KeyboardButton("/Меню")
#b4 = KeyboardButton('Поделиться номером', request_contact=True)
#b5 = KeyboardButton('Отправить где я', request_location=True)

kb_client = ReplyKeyboardMarkup(resize_keyboard=True)

kb_client.add(b3).row(b2, b1)


import pandas as pd
import numpy as np
from sklearn import tree, metrics
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
from scipy.io import arff

data = arff.loadarff('bank.arff')
df = pd.DataFrame(data[0])
df['subscribed'] = df['subscribed'].apply(lambda x: x.decode('utf-8'))

features = df.drop('subscribed', axis=1)
target = df['subscribed']

categorical_columns = ['job', 'marital', 'education', 'default', 'housing', 'loan', 'contact', 'month', 'poutcome']
for column in categorical_columns:
    features[column] = pd.Categorical(features[column])
    features[column] = features[column].cat.codes

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42)

Arvore = DecisionTreeClassifier(criterion='entropy', max_depth=5, random_state=42)
Arvore.fit(X_train, y_train)

plt.figure(figsize=(25,10))
tree.plot_tree(Arvore, feature_names=list(features.columns), class_names=['yes', 'no'], filled=True, rounded=True)
plt.show()

fig, ax = plt.subplots(figsize=(25, 10))
metrics.plot_confusion_matrix(Arvore, X_test, y_test, display_labels=['yes', 'no'], values_format='d', ax=ax)
plt.show()


from django.urls import path, include
from rest_framework import routers
from . import views
from rest_framework.routers import SimpleRouter, DefaultRouter
from rest_framework_nested import routers

# following uses FUNTIONAL based views
# urlpatterns = [
#     path('products/', views.product_list),
#     path('products/<int:pk>/', views.product_details),
#     path('collections/', views.collection_list),
#     path('collections/<int:pk>/', views.collection_detail, name='collection-detail'),
# ]

# following uses CLASS based views
# urlpatterns = [
#     path('products/', views.ProductList.as_view()),
#     path('products/<int:pk>/', views.ProductDetail.as_view()),
#     path('collections/', views.CollectionList.as_view()),
#     path('collections/<int:pk>/', views.CollectionDetail.as_view(), name='collection-detail'),
# ]

# following uses MIXINS and GenericAPI based views
# router = SimpleRouter()
# router = DefaultRouter()
router = routers.DefaultRouter()
router.register('products', views.ProductViewSet, basename='products')
router.register('collections', views.CollectionViewSet)
router.register('customers', views.CustomerViewSet, basename='customers')
router.register('orders', views.OrderViewSet, basename='orders')
router.register('carts', views.CartViewSet, basename='carts')

# routers.NestedDefaultRouter(parentRouter, parentPrefix, lookups='product')
products_router = routers.NestedDefaultRouter(router, 'products', lookup='product')   # lookup means we will have id like product_id
products_router.register('reviews', views.ReviewViewSet, basename='products-reviews')
# from basename we will have two mores like products-reviews-list and products-reviews-details like we started working from TOP,
# and it will be by default

carts_router = routers.NestedDefaultRouter(router, 'carts', lookup='cart')   # lookup means we will have id like product_id
carts_router.register('items', views.CartItemViewSet, basename='cart-items')

# urlpatterns = router.urls

urlpatterns = [
    path('', include(router.urls)),
    path('', include(products_router.urls)),
    path('', include(carts_router.urls)),
]


import numpy as np
import torch
from torch import nn

class CirculantTemporalEncoding(nn.Module):
    def __init__(self, m, lmbda):
        super(CirculantTemporalEncoding, self).__init__()
        self.m = m
        self.lmbda = lmbda

    def single_fv(self, ts):
        device = ts.device
        b, length, d = ts.size()
        # pad with 0 up to the closest power of 2
        length_power_2 = 2 ** int(np.ceil(np.log2(length)))
        offset = length_power_2 - length
        if offset > 0:
            zero_pad = torch.zeros(b, offset, d, dtype=torch.float32, device=device)
            ts = torch.cat([ts, zero_pad], dim=1)
        ts_ar = ts.permute(0, 2, 1).contiguous()  # shape (b, d, length)
        ts_ai = torch.zeros(ts_ar.size()).to(device)  # imaginary part is zero

        # With pytorch.fft, real and im are in the same tensor
        ts_air = torch.stack((ts_ar, ts_ai), dim=-1)
        Q = torch.fft(ts_air, 1)
        Qir = Q[..., 0]
        Qii = Q[..., 1]

        Qir = Qir[:, :, : self.m]
        Qii = Qii[:, :, : self.m]
        return torch.cat([Qir, Qii], dim=-1)

    def merge(self, fv_a, fv_b, offsets, max_len):
        max_len = offsets.shape[-1] // 2
        device = fv_a.device
        ts_ar = fv_a[:, :, : self.m]
        ts_ai = fv_a[:, :, self.m :]
        ts_br = fv_b[:, :, : self.m]
        ts_bi = fv_b[:, :, self.m :]
        length_power_2 = 2 ** int(np.ceil(np.log2(max_len)))

        Rrs = []
        # s(x, y) contains the scores for y shifted from 0 to max_len-1
        # compute s(a, b) and s(b, a), then concat flip(s(a, b)) and s(b, a) to have all possible time-shifts
        for i, (ts_ar, ts_ai, ts_br, ts_bi) in enumerate(
            [(ts_ar, ts_ai, ts_br, ts_bi), (ts_br, ts_bi, ts_ar, ts_ai)]
        ):
            Qir, Qii = ts_ar, ts_ai
            Bir, Bii = ts_br, ts_bi
            # See equation (10) in the paper
            Qdenr = torch.sum(Qir * Qir + Qii * Qii, dim=1, keepdim=True) + self.lmbda

            Sr = torch.sum((Qir * Bir + Qii * Bii) / Qdenr, dim=1, keepdim=True)
            Si = torch.sum((-Qii * Bir + Qir * Bii) / Qdenr, dim=1, keepdim=True)

            # padding with 0 up to the closest power of 2
            s0, s1, s2 = Sr.size()
            padding = length_power_2 - s2
            if padding > 0:
                zero_pad = torch.zeros(
                    s0, s1, padding, dtype=torch.float32, device=device
                )
                Sr = torch.cat([Sr, zero_pad], dim=2)
                Si = torch.cat([Si, zero_pad], dim=2)

            # With pytorch.fft, real and im are in the same tensor
            Sir = torch.stack((Sr, Si), dim=-1)
            R = torch.ifft(Sir, 1)
            Rr = R[..., 0]
            # Shape [b, 1, length_power_2], remove dim=1 and keep only up to max_len -> new shape [b, max_len]
            Rr = Rr.permute(0, 2, 1).squeeze(2)[:, :max_len]
            Rrs.append(Rr)

        Rrs[0] = Rrs[0].flip(dims=[1])
        outs = torch.cat(Rrs, dim=1)
        return outs

    def forward(self, ts_a, ts_b, offsets):
        fv_a = self.single_fv(ts_a)
        fv_b = self.single_fv(ts_b)
        len_a = ts_a.size()[1]
        len_b = ts_b.size()[2]
        max_len = max(len_a, len_b)
        return self.merge(fv_a, fv_b, offsets, max_len)


import asyncio

import httpx
from fake_useragent import UserAgent

settings = {
    "timeout": 5,
}

headers = {
    "user-agent": UserAgent().random,
    "accept": "text/html"
}

urls = []

result = {
    "success": [],
    "warning": [],
    "error": []
}

def load_urls():
    with open('url.txt', 'r', encoding='utf-8') as file:
        for line in file:
            line = line.strip()
            urls.append(line)

async def check_urls():
    for url in urls:
        async with httpx.AsyncClient() as client:
            try:
                response = await client.get(url, headers=headers, timeout=settings['timeout'])

                if response.status_code == 200 or response.status_code == 301:
                    print('{0:<30} {1:>30}'.format(url, '\033[92mSUCCESS\033[0m'))
                    result['success'].append(url)
                else:
                    print('{0:<30} {1:>30}'.format(url, '\033[93mWARNING\033[0m'))
                    result['warning'].append(url)
            except Exception as e:
                print('{0:<30} {1:>30}'.format(url, '\033[91mERROR\033[0m'))
                result['error'].append(url)

def save_data():
    success = "\n".join(result['success'])
    warning = "\n".join(result['warning'])
    error = "\n".join(result['error'])

    open('data/success.txt', 'w').write(success)
    open('data/warning.txt', 'w').write(warning)
    open('data/error.txt', 'w').write(error)

if __name__ == '__main__':
    load_urls()
    print('{0:<30} {1:>20}'.format('Url', 'Status'))
    asyncio.run(check_urls())
    save_data()


from flask_restful import Resource
import requests
import os
import json
from flask import request
from helper import send_options, send_response

class Orders(Resource):
    def post(self):
        content_type = request.headers.get('Content-Type')
        if (content_type == 'application/json'):
            payload = request.get_json()
            response = save_order_details({"records":[{"fields":payload}]})
            if response.reason == 'OK':
                return send_response({}, 200)
            else:
                return send_response(response.text, 400)
        else:
            return send_response({"error": "invalid content type"}, 400)

    def options(self):
        return send_options("POST")

def save_order_details(payload):
    url = os.environ['AIRTABLE_URL']+'/orders'
    header = {"Authorization":"Bearer " + os.environ['AIRTABLE_KEY'], "Content-Type": "application/json"}

    return requests.post(url, data = json.dumps(payload), headers = header)

# sample airtable payload structure for dev reference
sample_payload = {
  "records": [
    {
      "fields": {
        "your-name": "claudia",
        "your-email": "claudia@sample.com",
        "phone-number": "9021111111",
        "pickup-location": "123 high st, Williamswood, Nova Scotia, Canada",
        "drop-location": "55 bently dr, Williamswood, Nova Scotia, Canada",
        "pickup-date": "2022-12-15",
        "item-description": "A wardrobe 73 inches high, 38 wide.\n2 regular sized doors, 3 closet doors, a few skinny windows, building materials 4 boxes of cedar shingles, 2 boxes...",
        "price": 115,
        "tos": 1,
        "record_date": "2022-12-16T02:46:00.000Z"
      }
    },
    {
      "fields": {
        "your-name": "prajwal",
        "your-email": "app@a.ca",
        "phone-number": "9029898888",
        "pickup-location": "8 Theakston Avenue, Halifax, Nova Scotia, Canada",
        "drop-location": "40 Theakston Avenue, Halifax, Nova Scotia, Canada",
        "pickup-date": "2021-12-10",
        "price": 85,
        "tos": 1,
        "record_date": "2021-12-12T03:21:00.000Z"
      }
    }
  ]
}


#! /usr/bin/python

import os
import time
import smtplib
from datetime import datetime
from distutils import util
from MappingCommon import MappingCommon

#
# Main code begins here.
#
fqaqcIssueCount = 0
nqaqcIssueCount = 0

mapc = MappingCommon()

logFilePath = mapc.projectRoot + "/log"
k = open(logFilePath + "/createHit.log", "a+")

now = str(datetime.today())
k.write("\ncreateHit: Daemon starting up at %s\n" % now)
k.close()

# Execute loop based on polling interval
while True:
    hitPollingInterval = int(mapc.getConfiguration('Hit_PollingInterval'))
    hitStandAlone = mapc.getConfiguration('Hit_StandAlone')
    # Boolean config parameters are returned as string and need to be converted.
    hitStandAlone = bool(util.strtobool(hitStandAlone))
    Hit_AvailTarget = int(mapc.getConfiguration('Hit_AvailTarget'))
    hitReplacementThresholdF = float(mapc.getConfiguration('Hit_ReplacementThreshold_F'))
    hitReplacementThresholdN = float(mapc.getConfiguration('Hit_ReplacementThreshold_N'))
    hitAlertFrequency = int(mapc.getConfiguration('Hit_AlertFrequency')) * 60
    hitNeedFKmlsAlert = mapc.getConfiguration('Hit_NeedFKmlsAlert')
    # Boolean config parameters are returned as string and need to be converted.
    hitNeedFKmlsAlert = bool(util.strtobool(hitNeedFKmlsAlert))
    hitNeedNKmlsAlert = mapc.getConfiguration('Hit_NeedNKmlsAlert')
    # Boolean config parameters are returned as string and need to be converted.
    hitNeedNKmlsAlert = bool(util.strtobool(hitNeedNKmlsAlert))

    k = open(logFilePath + "/createHit.log", "a+")
    now = str(datetime.today())

    # Get serialization lock.
    mapc.getSerializationLock()

    # Get all Assignable HITs  and calculate our needs.
    numQaqcHits = 0
    numFqaqcHits = 0
    numNonQaqcHits = 0
    for hitId, row in mapc.getAssignableHitInfo().iteritems():
        # Calculate the number of assignable QAQC, FQAQC, and non-QAQC HITs
        # currently available. For HITs with multiple assignments, only count HITs
        # where the number of assignments created is less than the configured threshold.
        kmlType = row['kmlType']
        maxAssignments = row['maxAssignments']
        createdAssignments = maxAssignments - row['assignmentsRemaining']
        if kmlType == MappingCommon.KmlQAQC:
            numQaqcHits = numQaqcHits + 1
        elif kmlType == MappingCommon.KmlFQAQC:
            # Must have created less than the threshold number of assignments.
            threshold = max(int(round(hitReplacementThresholdF * maxAssignments)), 1)
            if createdAssignments < threshold:
                numFqaqcHits = numFqaqcHits + 1
        elif kmlType == MappingCommon.KmlNormal:
            # Must have created less than the threshold number of assignments.
            threshold = max(int(round(hitReplacementThresholdN * maxAssignments)), 1)
            if createdAssignments < threshold:
                numNonQaqcHits = numNonQaqcHits + 1

    # Create any needed QAQC HITs.
    kmlType = MappingCommon.KmlQAQC
    numReqdQaqcHits = max(Hit_AvailTarget - numQaqcHits, 0)
    if numReqdQaqcHits > 0:
        k.write("\ncreateHit: datetime = %s\n" % now)
        k.write("createHit: createHit sees %s Q HITs, and needs to create %s HITs\n" %
            (numQaqcHits, numReqdQaqcHits))

    for i in xrange(numReqdQaqcHits):
        # Retrieve the last kml gid used to create a QAQC HIT.
        curQaqcGid = mapc.getSystemData('CurQaqcGid')

        # Select the next kml for which to create a HIT.
        # Look for all kmls of the right type whose gid is greater than the last kml chosen.
        # Exclude any kmls that are currently associated with an active HIT.
        (nextKml, mappedCount, fwts, gid) = mapc.getAvailableKml(kmlType, 1, curQaqcGid)
        # If we have no kmls left, loop back to the beginning of the table.
        if nextKml is None:
            curQaqcGid = 0
            (nextKml, mappedCount, fwts, gid) = mapc.getAvailableKml(kmlType, 1, curQaqcGid)
            # If we still have no kmls left, all kmls are in use as HITs.
            # Try again later.
            if nextKml is None:
                break
        # Save the last kml gid used to create a QAQC HIT.
        mapc.setSystemData('CurQaqcGid', gid)

        # Create the QAQC HIT
        hitId = mapc.createHit(nextKml, fwts=fwts)
        k.write("createHit: Created HIT ID %s for Q KML %s\n" % (hitId, nextKml))

    # Create any needed FQAQC HITs.
    kmlType = MappingCommon.KmlFQAQC
    numReqdFqaqcHits = max(Hit_AvailTarget - numFqaqcHits, 0)
    if numReqdFqaqcHits > 0:
        k.write("\ncreateHit: datetime = %s\n" % now)
        k.write("createHit: createHit sees %s F HITs, and needs to create %s HITs\n" %
            (numFqaqcHits, numReqdFqaqcHits))

    for i in xrange(numReqdFqaqcHits):
        # Select the next kml for which to create a HIT.
        # Look for all kmls of the right type whose mapped count by a trusted worker is less than
        # the number of mappings specified by Hit_MaxAssignmentsF.
        # Exclude any kmls that are currently associated with an active HIT.
        hitMaxAssignmentsF = int(mapc.getConfiguration('Hit_MaxAssignmentsF'))
        (nextKml, mappedCount, fwts, gid) = mapc.getAvailableKml(kmlType, hitMaxAssignmentsF)
        # If we have no kmls left, all kmls in the kml_data table have been
        # successfully processed. Notify Lyndon that more kmls are needed if we are in standalone mode.
        if nextKml is None:
            if (fqaqcIssueCount % (hitAlertFrequency / hitPollingInterval)) == 0:
                k.write("createHit: Alert: all F KMLs in kml_data table have been successfully processed. More KMLs needed to create more HITs of this type.\n")
                if hitStandAlone and hitNeedFKmlsAlert:
                    mapc.createAlertIssue("No F KMLs in kml_data table",
                            "Alert: all F KMLs in kml_data table have been successfully processed. More KMLs needed to create more HITs of this type.")
            fqaqcIssueCount += 1
            break
        else:
            if (fqaqcIssueCount % (hitAlertFrequency / hitPollingInterval)) == 0:
                fqaqcIssueCount = 0
            else:
                fqaqcIssueCount += 1
        remainingAssignments = hitMaxAssignmentsF - mappedCount

        # Create the FQAQC HIT
        hitId = mapc.createHit(nextKml, fwts=fwts, maxAssignments=remainingAssignments)
        k.write("createHit: Created HIT ID %s with %d assignments for F KML %s\n" %
                (hitId, remainingAssignments, nextKml))

    # Create any needed N HITs.
    kmlType = MappingCommon.KmlNormal
    numReqdNonQaqcHits = max(Hit_AvailTarget - numNonQaqcHits, 0)
    if numReqdNonQaqcHits > 0:
        k.write("\ncreateHit: datetime = %s\n" % now)
        k.write("createHit: createHit sees %s N HITs, and needs to create %s HITs\n" %
            (numNonQaqcHits, numReqdNonQaqcHits))

    for i in xrange(numReqdNonQaqcHits):
        # Select the next kml for which to create a HIT.
        # Look for all kmls of the right type whose mapped count by a trusted worker is less than
        # the number of mappings specified by Hit_MaxAssignmentsN.
        # Exclude any kmls that are currently associated with an active HIT.
        hitMaxAssignmentsN = int(mapc.getConfiguration('Hit_MaxAssignmentsN'))
        (nextKml, mappedCount, fwts, gid) = mapc.getAvailableKml(kmlType, hitMaxAssignmentsN)
        # If we have no kmls left, all kmls in the kml_data table have been
        # successfully processed. Notify Lyndon that more kmls are needed.
        if nextKml is None:
            if (nqaqcIssueCount % (hitAlertFrequency / hitPollingInterval)) == 0:
                k.write("createHit: Alert: all N KMLs in kml_data table have been successfully processed. More KMLs needed to create more HITs of this type.\n")
                if hitNeedNKmlsAlert:
                    mapc.createAlertIssue("No N KMLs in kml_data table",
                            "Alert: all N KMLs in kml_data table have been successfully processed. More KMLs needed to create more HITs of this type.")
            nqaqcIssueCount += 1
            break
        else:
            if (nqaqcIssueCount % (hitAlertFrequency / hitPollingInterval)) == 0:
                nqaqcIssueCount = 0
            else:
                nqaqcIssueCount += 1
        remainingAssignments = hitMaxAssignmentsN - mappedCount

        # Create the non-QAQC HIT
        hitId = mapc.createHit(nextKml, fwts=fwts, maxAssignments=remainingAssignments)
        k.write("createHit: Created HIT ID %s with %d assignments for N KML %s\n" %
                (hitId, remainingAssignments, nextKml))

    # Release serialization lock.
    mapc.releaseSerializationLock()

    # Sleep for specified polling interval
    k.close()
    time.sleep(hitPollingInterval)


from abc import ABCMeta, abstractmethod

import torch
from torch import nn
import torch.utils.checkpoint as cp
from mmcv.cnn.bricks import DropPath
from mmcv.cnn import (ConvModule, build_activation_layer, build_conv_layer,
                      build_norm_layer)
from mmengine.model import BaseModel
from mmengine.model.weight_init import constant_init
from mmengine.utils.dl_utils.parrots_wrapper import _BatchNorm
from deep_vital.registry import MODELS

eps = 1.0e-5

class Bottleneck1D(BaseModel):
    """Bottleneck block for ResNet1D.
    """

    def __init__(self,
                 in_channels,
                 out_channels,
                 expansion=4,
                 stride=1,
                 dilation=1,
                 downsample=None,
                 style='pytorch',
                 with_cp=False,
                 conv_cfg=dict(type='Conv1d'),
                 norm_cfg=dict(type='BN1d'),
                 act_cfg=dict(type='ReLU', inplace=True),
                 drop_path_rate=0.0,
                 init_cfg=None):
        super().__init__(init_cfg=init_cfg)

        self.in_channels = in_channels
        self.out_channels = out_channels
        self.expansion = expansion
        assert out_channels % expansion == 0
        self.mid_channels = out_channels // expansion
        self.stride = stride
        self.dilation = dilation
        self.with_cp = with_cp
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg

        self.conv1_stride = 1
        self.conv2_stride = stride

        self.norm1_name, norm1 = build_norm_layer(norm_cfg,
                                                  self.mid_channels,
                                                  postfix=1)
        self.norm2_name, norm2 = build_norm_layer(norm_cfg,
                                                  self.mid_channels,
                                                  postfix=2)
        self.norm3_name, norm3 = build_norm_layer(norm_cfg,
                                                  out_channels,
                                                  postfix=3)

        self.conv1 = build_conv_layer(conv_cfg,
                                      in_channels,
                                      self.mid_channels,
                                      kernel_size=1,
                                      stride=self.conv1_stride,
                                      bias=True)
        self.add_module(self.norm1_name, norm1)
        self.conv2 = build_conv_layer(conv_cfg,
                                      self.mid_channels,
                                      self.mid_channels,
                                      kernel_size=3,
                                      stride=self.conv2_stride,
                                      padding=dilation,
                                      dilation=dilation,
                                      bias=True)

        self.add_module(self.norm2_name, norm2)
        self.conv3 = build_conv_layer(conv_cfg,
                                      self.mid_channels,
                                      out_channels,
                                      kernel_size=1,
                                      bias=True)
        self.add_module(self.norm3_name, norm3)

        self.relu = build_activation_layer(act_cfg)

        self.downsample = downsample
        self.drop_path = DropPath(drop_prob=drop_path_rate
                                  ) if drop_path_rate > eps else nn.Identity()

    @property
    def norm1(self):
        return getattr(self, self.norm1_name)

    @property
    def norm2(self):
        return getattr(self, self.norm2_name)

    @property
    def norm3(self):
        return getattr(self, self.norm3_name)

    def forward(self, x):

        def _inner_forward(x):
            identity = x

            out = self.conv1(x)
            out = self.norm1(out)
            out = self.relu(out)

            out = self.conv2(out)
            out = self.norm2(out)
            out = self.relu(out)

            out = self.conv3(out)
            out = self.norm3(out)

            if self.downsample is not None:
                identity = self.downsample(x)

            out = self.drop_path(out)

            out += identity

            return out

        if self.with_cp and x.requires_grad:
            out = cp.checkpoint(_inner_forward, x)
        else:
            out = _inner_forward(x)

        out = self.relu(out)

        return out

def get_expansion(block, expansion=None):
    """Get the expansion of a residual block.

    The block expansion will be obtained by the following order:

    1. If ``expansion`` is given, just return it.
    2. If ``block`` has the attribute ``expansion``, then return
       ``block.expansion``.
    3. Return the default value according the the block type:
       1 for ``BasicBlock`` and 4 for ``Bottleneck``.

    Args:
        block (class): The block class.
        expansion (int | None): The given expansion ratio.

    Returns:
        int: The expansion of the block.
    """
    if isinstance(expansion, int):
        assert expansion > 0
    elif expansion is None:
        if hasattr(block, 'expansion'):
            expansion = block.expansion
        # elif issubclass(block, BasicBlock):
        #     expansion = 1
        elif issubclass(block, Bottleneck1D):
            expansion = 4
        else:
            raise TypeError(f'expansion is not specified for {block.__name__}')
    else:
        raise TypeError('expansion must be an integer or None')

    return expansion

class ResLayer(nn.Sequential):
    """ResLayer to build ResNet style backbone.

    Args:
        block (nn.Module): Residual block used to build ResLayer.
        num_blocks (int): Number of blocks.
        in_channels (int): Input channels of this block.
        out_channels (int): Output channels of this block.
        expansion (int, optional): The expansion for BasicBlock/Bottleneck.
            If not specified, it will firstly be obtained via
            ``block.expansion``. If the block has no attribute "expansion",
            the following default values will be used: 1 for BasicBlock and
            4 for Bottleneck. Default: None.
        stride (int): stride of the first block. Default: 1.
        avg_down (bool): Use AvgPool instead of stride conv when
            downsampling in the bottleneck. Default: False
        conv_cfg (dict, optional): dictionary to construct and config conv
            layer. Default: None
        norm_cfg (dict): dictionary to construct and config norm layer.
            Default: dict(type='BN')
    """

    def __init__(self,
                 block,
                 num_blocks,
                 in_channels,
                 out_channels,
                 expansion=None,
                 stride=1,
                 avg_down=False,
                 conv_cfg=dict(type='Conv1d'),
                 norm_cfg=dict(type='BN1d'),
                 **kwargs):
        self.block = block
        self.expansion = get_expansion(block, expansion)

        downsample = None
        if stride != 1 or in_channels != out_channels:
            downsample = []
            conv_stride = stride
            if avg_down and stride != 1:
                conv_stride = 1
                downsample.append(
                    nn.AvgPool2d(
                        kernel_size=stride,
                        stride=stride,
                        ceil_mode=True,
                        count_include_pad=False))
            downsample.extend([
                build_conv_layer(
                    conv_cfg,
                    in_channels,
                    out_channels,
                    kernel_size=1,
                    stride=conv_stride,
                    bias=True),
                build_norm_layer(norm_cfg, out_channels)[1]
            ])
            downsample = nn.Sequential(*downsample)

        layers = []
        layers.append(
            block(
                in_channels=in_channels,
                out_channels=out_channels,
                expansion=self.expansion,
                stride=stride,
                downsample=downsample,
                conv_cfg=conv_cfg,
                norm_cfg=norm_cfg,
                **kwargs))
        in_channels = out_channels
        for i in range(1, num_blocks):
            layers.append(
                block(
                    in_channels=in_channels,
                    out_channels=out_channels,
                    expansion=self.expansion,
                    stride=1,
                    conv_cfg=conv_cfg,
                    norm_cfg=norm_cfg,
                    **kwargs))
        super().__init__(*layers)

@MODELS.register_module()
class ResNet1D(BaseModel):
    arch_settings = {
        50: (Bottleneck1D, (3, 4, 6, 3)),
        101: (Bottleneck1D, (3, 4, 23, 3)),
        152: (Bottleneck1D, (3, 8, 36, 3))
    }

    def __init__(self,
                 depth,
                 in_channels=1,
                 stem_channels=64,
                 base_channels=64,
                 expansion=None,
                 num_stages=4,
                 strides=(1, 2, 2, 2),
                 dilations=(1, 1, 1, 1),
                 out_indices=(3, ),
                 deep_stem=False,
                 avg_down=False,
                 frozen_stages=-1,
                 conv_cfg=dict(type='Conv1d'),
                 norm_cfg=dict(type='BN1d', requires_grad=True),
                 norm_eval=False,
                 with_cp=False,
                 zero_init_residual=True,
                 init_cfg=None,
                 drop_path_rate=0.0) -> None:
        super().__init__(init_cfg=init_cfg)
        if depth not in self.arch_settings:
            raise KeyError(f'invalid depth {depth} for resnet')
        self.depth = depth
        self.stem_channels = stem_channels
        self.base_channels = base_channels
        self.num_stages = num_stages
        assert num_stages >= 1 and num_stages <= 4
        self.strides = strides
        self.dilations = dilations
        assert len(strides) == len(dilations) == num_stages
        self.out_indices = out_indices
        assert max(out_indices) < num_stages
        self.style = 'pytorch'
        self.deep_stem = deep_stem
        self.avg_down = avg_down
        self.frozen_stages = frozen_stages
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.with_cp = with_cp
        self.norm_eval = norm_eval
        self.zero_init_residual = zero_init_residual
        self.block, stage_blocks = self.arch_settings[depth]
        self.stage_blocks = stage_blocks[:num_stages]
        self.expansion = get_expansion(self.block, expansion)

        self._make_stem_layer(in_channels, stem_channels)

        self.res_layers = []
        _in_channels = stem_channels
        _out_channels = base_channels * self.expansion
        for i, num_blocks in enumerate(self.stage_blocks):
            stride = strides[i]
            dilation = dilations[i]
            res_layer = self.make_res_layer(
                block=self.block,
                num_blocks=num_blocks,
                in_channels=_in_channels,
                out_channels=_out_channels,
                expansion=self.expansion,
                stride=stride,
                dilation=dilation,
                style=self.style,
                avg_down=self.avg_down,
                with_cp=with_cp,
                conv_cfg=conv_cfg,
                norm_cfg=norm_cfg,
                drop_path_rate=drop_path_rate)
            _in_channels = _out_channels
            _out_channels *= 2
            layer_name = f'layer{i + 1}'
            self.add_module(layer_name, res_layer)
            self.res_layers.append(layer_name)

        self._freeze_stages()

        self.feat_dim = res_layer[-1].out_channels

    def make_res_layer(self, **kwargs):
        return ResLayer(**kwargs)

    @property
    def norm1(self):
        return getattr(self, self.norm1_name)

    def _make_stem_layer(self, in_channels, stem_channels):
        if self.deep_stem:
            self.stem = nn.Sequential(
                ConvModule(
                    in_channels,
                    stem_channels // 2,
                    kernel_size=3,
                    stride=2,
                    padding=1,
                    conv_cfg=self.conv_cfg,
                    norm_cfg=self.norm_cfg,
                    inplace=True),
                ConvModule(
                    stem_channels // 2,
                    stem_channels // 2,
                    kernel_size=3,
                    stride=1,
                    padding=1,
                    conv_cfg=self.conv_cfg,
                    norm_cfg=self.norm_cfg,
                    inplace=True),
                ConvModule(
                    stem_channels // 2,
                    stem_channels,
                    kernel_size=3,
                    stride=1,
                    padding=1,
                    conv_cfg=self.conv_cfg,
                    norm_cfg=self.norm_cfg,
                    inplace=True))
        else:
            self.conv1 = build_conv_layer(
                self.conv_cfg,
                in_channels,
                stem_channels,
                kernel_size=7,
                stride=2,
                padding=3,
                bias=True)
            self.norm1_name, norm1 = build_norm_layer(
                self.norm_cfg, stem_channels, postfix=1)
            self.add_module(self.norm1_name, norm1)
            self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool1d(kernel_size=3, stride=3)

    def _freeze_stages(self):
        if self.frozen_stages >= 0:
            if self.deep_stem:
                self.stem.eval()
                for param in self.stem.parameters():
                    param.requires_grad = False
            else:
                self.norm1.eval()
                for m in [self.conv1, self.norm1]:
                    for param in m.parameters():
                        param.requires_grad = False

        for i in range(1, self.frozen_stages + 1):
            m = getattr(self, f'layer{i}')
            m.eval()
            for param in m.parameters():
                param.requires_grad = False

    def init_weights(self):
        super().init_weights()

        if (isinstance(self.init_cfg, dict)
                and self.init_cfg['type'] == 'Pretrained'):
            # Suppress zero_init_residual if use pretrained model.
            return

        if self.zero_init_residual:
            for m in self.modules():
                if isinstance(m, Bottleneck1D):
                    constant_init(m.norm3, 0)

    def forward(self, x):
        if self.deep_stem:
            x = self.stem(x)
        else:
            x = self.conv1(x)
            x = self.norm1(x)
            x = self.relu(x)
        x = self.maxpool(x)
        outs = []
        for i, layer_name in enumerate(self.res_layers):
            res_layer = getattr(self, layer_name)
            x = res_layer(x)
            if i in self.out_indices:
                outs.append(x)
        return tuple(outs)

    def train(self, mode=True):
        super().train(mode)
        self._freeze_stages()
        if mode and self.norm_eval:
            for m in self.modules():
                # trick: eval have effect on BatchNorm only
                if isinstance(m, _BatchNorm):
                    m.eval()


#! /usr/bin/env python

# Way2SMS Desktop-App
# Author: Akash Shende
# Contact: akash321@gmail.com

from bs4 import BeautifulSoup as bs
from way2sms import __contact_list__

class Contacts():

	def __init__(self,html_data,c_list):
		#creating soup
		Contacts.soup=bs(''.join(html_data))

		Contacts.c_list=c_list

	def extract_name(self):
		names=Contacts.soup.findAll(attrs={"name":"Quckvalue"})
		#print names
		names=str(names[0]['value'])
		name_list=names.split("*")

		#for n in name_list:
		#	print n,"\n"

		return name_list

	def extract_number(self):
		numbers=Contacts.soup.findAll(attrs={"name":"Qucktitle"})
		numbers=str(numbers[0]['value'])
		number_list=numbers.split(',')

		#for n in number_list:
		#	print n,"\n"

		return number_list

	def build_list(self):

		list1=self.extract_name()
		list2=self.extract_number()

		for i in xrange(1,len(list1)):
			Contacts.c_list.append([str(list1[i]),str(list2[i])])


import unittest
from untils.HttpClient import HttpClientRequest
from untils.readYaml import baseConfig
from untils.op_mysql import execute
from log.log import logger
from ddt import ddt,data,file_data
from untils.op_excel import Excel_rw
from untils.tool import *
apiPre=baseConfig.api['test']
#apicase_path=baseConfig.APICASE_YAML_PATH
apicase_path=baseConfig.APICASE_EXCEL_PATH
@ddt
class TestCaseAPi(unittest.TestCase):
    '''
    作业：驱动excel表格用例，所有的sheet表格，所有的用例依次执行，而且打印接口执行个数
    '''
    index=0
    '''几个接口的用例'''
    #Cases_Info=Excel_rw(filename=apicase_path).read_api_cases() #读取excel所有的用例数据
    Cases_Info=get_api_excel_case()
    @classmethod
    def setUpClass(self) -> None:
        '''所有用例的前置'''
        self.request=HttpClientRequest()

    def tearDown(self) -> None:
        '''接口请求后，初始化headers'''
        self.request.init_headers()

    # @file_data(apicase_path)
    # def test01(self,**caseinfo):
    #     '''
    #     登录
    #     :return:
    #     '''
    #     TestCaseAPi.index+=1
    #     casename,method,apiname,data,extract,duanyan,headers=caseinfo['casename'],caseinfo['method'],\
    #                                                  caseinfo['apiname'],caseinfo['data'],\
    #                                                  caseinfo['extract'],caseinfo['duanyan'],caseinfo['headers']
    #     res=self.request.sendRequest(method=method,
    #                                  name=apiname,
    #                                  data=data,
    #                                  tiqu=extract,
    #                                  headers=headers,
    #                                  index=TestCaseAPi.index)#3.发请求过程
    #     #self.request.validate(yuqi=duanyan, shiji=res)
    #     result=self.request.validate(yuqi=duanyan,shiji=res) #返回元组（1布尔值，2验证信息）
    #     logger.info(result[1])
    #     self.assertTrue(result[0])#4.结果验证
    #     '''拿到响应值,写入到excel，输入到报告'''

    @data(*Cases_Info)
    def test01(self, Case_Info):
        '''
        登录
        :return:
        '''
        print(Case_Info)
        TestCaseAPi.index += 1
        casename, method, apiname, data, extract, duanyan, headers ,host= Case_Info['接口用例名称'], Case_Info['请求方式'], \
                                                                     Case_Info['接口url'], Case_Info['请求入参'], \
                                                                     Case_Info['提取变量'], Case_Info['检查点'], \
                                                                     Case_Info['请求头信息'],Case_Info['域名'],
        #excel里面字典类型数据转化
        data=json_loads(data)
        headers=json_loads(headers)
        duanyan=json_loads(duanyan)
        extract=json_loads(extract)
        url=host+apiname
        res = self.request.sendRequest(method=method,
                                       url=url,
                                       data=data,
                                       tiqu=extract,
                                       headers=headers,
                                       index=TestCaseAPi.index)  # 3.发请求过程
        result = self.request.validate(yuqi=duanyan, shiji=res)  # 返回元组（1布尔值，2验证信息）
        logger.info(result[1])
        self.assertTrue(result[0])  # 4.结果验证
        '''拿到响应值,写入到excel，输入到报告'''

if __name__ == '__main__':
    unittest.main()


# -*- mode: python; coding: utf-8; -*-

__author__ = "Kenny Meyer"
__email__ = "knny.myer@gmail.com"

from django.forms import ModelForm, Textarea
from django import forms
from django.core.exceptions import ValidationError

from models import FlashCard

RATINGS = ((0, 'Blackout'),
           (1, 'Barely Know it'),
           (2, 'Needs Work'),
           (3, 'Remembered'),
           (4, 'Solid'))

class RatingForm(forms.Form):
    id = forms.IntegerField(widget=forms.HiddenInput)
    rating = forms.ChoiceField(choices=RATINGS)

SORTING = ((0, 'By alphabet'),
           (1, 'By next practice'))

class SortFlashcardsForm(forms.Form):
    sort_by = forms.ChoiceField(choices=SORTING)

class FlashCardForm(ModelForm):
    class Meta():
        model = FlashCard
        fields = ['front', 'back']
        exclude = ('user')
        widgets = {
            'front': Textarea(attrs={'cols': 70, 'rows': 10}),
            'back': Textarea(attrs={'cols': 70, 'rows': 10}),
        }

# vim: ai ts=4 sts=4 et sw=4


'''
This file reproduce the faro sift use in python
'''
#%% Import section
import numpy as np
import cv2 as cv
#%% Im read
img = cv.imread('ReferenceImages/rearCIC.png')
gray= cv.cvtColor(img, cv.COLOR_BGR2GRAY)
#%% sift creator
sift = cv.xfeatures2d.SIFT_create()
#%% sift detection
kp = sift.detect(gray,None)
img = cv.drawKeypoints(gray, kp, img)

cv.imwrite('sift_keypoints.jpg', img)


import datetime
import os
import jwt
from typing import Optional

def create_jwt(
    username: str,
    secret_key: Optional[str] = None,
    expires_seconds: Optional[int] = None,
):
    if not secret_key:
        secret_key = os.environ["SECRET_KEY"]

    if not expires_seconds:
        expires_seconds = int(os.environ["JWT_ACCESS_TOKEN_EXPIRES_SECONDS"])

    iat = datetime.datetime.now(tz=datetime.timezone.utc)
    exp = iat + datetime.timedelta(seconds=expires_seconds)

    payload = {"sub": username, "iat": iat, "exp": exp}

    return jwt.encode(payload=payload, key=secret_key)


#import Adafruit_BME280
import os
import json
import random

PiWeather = "/var/www/html/cash_json/PiWeather.json"

def readWeather(sensor):
    degrees = "%.1f" % random.uniform(71.2, 73.4)
    hectopascals = "%.2f" % (random.randint(930, 937) * 0.02953)
    humidity = random.randint(12, 15)

    j_obj = {};
    j_obj['module'] = "weather"
    j_obj['temp'] = degrees
    j_obj['pressure'] = hectopascals
    j_obj['humidity'] = humidity

    output = json.dumps(j_obj)

    file_obj = open(PiWeather, "w")
    file_obj.truncate()
    file_obj.write(output)
    file_obj.close()


import requests
from bs4 import BeautifulSoup
import os
import time
import csv

headers = {
	'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.1708.400 QQBrowser/9.5.9635.400'
}

def getHTMLtext(url, code="utf-8"):
	try:
		r =requests.get(url)
		r.raise_for_status()
		r.encoding = code
		# print(r.text)
		return r.text
	except:
		return ""

# parameter
# shareCode/year/season : num ,
def sharesCrawl(shareCode,year,season):
	# shareCodeStr = str(shareCode)
	yearStr = str(year)
	seasonStr = str(season)
	url = 'http://quotes.money.163.com/trade/lsjysj_'+shareCode+'.html?year='+yearStr+'&season='+seasonStr
	print(url)
	data = requests.get(url, headers=headers)
	soup = BeautifulSoup(data.text, 'html.parser')

	table = soup.findAll('table',class_='table_bg001')[0]
	rows = table.findAll('tr')

	return rows[::-1]

def writeCSVWithName(shareCode,name,beginYear,endYear):
	# shareCodeStr = str(shareCode)

	# url = 'http://quotes.money.163.com/trade/lsjysj_' + shareCode + '.html'
	# print(url)
	# data = requests.get(url, headers=headers)
	# soup = BeautifulSoup(data.text, 'html.parser')
	csvpath = './dataWithName/' + name + '.csv'
	if(os.path.exists(csvpath)):
		os.remove(csvpath)
	csvFile = open(csvpath, 'w',encoding='utf8',newline='')
	writer = csv.writer(csvFile)
	writer.writerow(('日期','开盘价','最高价','最低价','收盘价','涨跌额','涨跌幅','成交量','成交金额','振幅','换手率'))

	for i in range(beginYear, endYear + 1):
		try:
			# print (str(i) + ' is going')
			# time.sleep(4)
			for j in range(1, 5):
				rows = sharesCrawl(shareCode,i,j)
				for row in rows:
					csvRow = []
					for cell in row.findAll('td'):
						csvRow.append(cell.get_text().replace(',',''))
					if csvRow != []:
						writer.writerow(csvRow)
				# time.sleep(3)
				# print(str(i) + '年' + str(j)  + '季度is done')
		except Exception as e:
			print(e)
			continue
	csvFile.close()

# 读取csv至字典
csvpath = r'D:\python\paper\sz50.csv' # 上证50股票名单
csvFile = open(csvpath, "r",encoding='utf-8')
reader = csv.reader(csvFile)
# 建立空字典
stocks = []
stockdic = {}
for item in reader:
	# 忽略第一行
	if reader.line_num == 1:
		continue
	stocks.append(str(item[1]))
	stockdic[str(item[1])] = item[0]
	# print(item)
csvFile.close()

i = 0
for k in stocks:
	if(i >= 100):
		break
	writeCSVWithName(k,stockdic[k],2007,2017)
	# print(k+","+stockdic[k])
	i = i + 1


__all__ = ['EstimatorConfig',
           'MultiClassXGBConfig',
           'BinClassXGBConfig',
           'RandomForestConfig',
           'ExtraRandomForestConfig',
           'GBDTConfig',
           'XGBRegressorConfig',
           'LGBMRegressorConfig',
           'Basic4x2']

from ..utils.log_utils import get_logger

class EstimatorConfig(object):
    """
    Estimator Argument is a class describes an estimator and its specific arguments, which is used to create concrete
    estimators in training.
    Every time we create a multi-grain scan layer or a cascade layer, we don't put concrete estimator instances into
    initialization parameters, but put the EstimatorConfig that describes the estimator.
    """
    def __init__(self, est_args=None):
        self.LOGGER = get_logger('estimator.estimator_configs')
        if est_args is None:
            self.est_args = {}
        else:
            self.est_args = est_args.copy()

    def get_est_args(self):
        """
        Get estimator argument.

        :return: estimator argument
        """
        return self.est_args.copy()

    def __str__(self):
        return "{}".format(self.est_args['est_type'])

    def __copy__(self):
        return EstimatorConfig(self.est_args)

class MultiClassXGBConfig(EstimatorConfig):
    """
    Multi-class XGBoost Classifier Argument.
    """
    def __init__(self, n_folds=3, nthread=-1, scale_pos_weight=1, num_class=None, silent=True,
                 objective="multi:softprob", subsample=0.9, n_estimators=60,
                 colsample_bytree=0.85, colsample_bylevel=1, max_depth=4, learning_rate=0.1):
        """
        Multi-class XGBoost Classifier Argument describes arguments of multi-class xgboost classifier.
        Parameters can refer to xgboost document: http://xgboost.readthedocs.io/en/latest/python/python_api.html

        :param n_folds: how many folds to execute in cross validation
        :param nthread: number of threads to execute
        :param scale_pos_weight: used to handle class imbalance
        :param num_class: number of classes to classify
        :param silent:
        :param objective: objective, default is 'multi:softprob'
        :param subsample:
        :param n_estimators:
        :param colsample_bytree:
        :param colsample_bylevel:
        :param max_depth:
        :param learning_rate:
        """
        super(MultiClassXGBConfig, self).__init__()
        assert num_class is not None, 'You must set number of classes!'
        self.est_args = {
            'est_type': 'FLXGB',
            'n_folds': n_folds,
            'nthread': nthread,
            'scale_pos_weight': scale_pos_weight,
            'silent': silent,
            'objective': objective,
            'subsample': subsample,
            'n_estimators': n_estimators,
            'colsample_bytree': colsample_bytree,
            'colsample_bylevel': colsample_bylevel,
            'max_depth': max_depth,
            'learning_rate': learning_rate
        }

class BinClassXGBConfig(EstimatorConfig):
    """
    Binary Class XGBoost Classifier.
    """
    def __init__(self, n_folds=3, nthread=-1, scale_pos_weight=1, num_class=2, silent=True,
                 objective="binary:logistic", subsample=1.0, num_boost_round=100,
                 colsample_bytree=1.0, colsample_bylevel=1.0, max_depth=6, learning_rate=0.1):
        """
        Binary-class XGBoost Classifier Argument describes arguments of multi-class xgboost classifier.
        Parameter can refer to xgboost document: http://xgboost.readthedocs.io/en/latest/python/python_api.html

        :param n_folds: how many folds to execute in cross validation
        :param nthread: number of threads to execute
        :param scale_pos_weight: used to handle class imbalance
        :param num_class: number of classes to classify
        :param silent:
        :param objective: objective, default is 'multi:softprob'
        :param subsample:
        :param num_boost_round:
        :param colsample_bytree:
        :param colsample_bylevel:
        :param max_depth:
        :param learning_rate:
        """
        super(BinClassXGBConfig, self).__init__()
        assert num_class is not None, 'You must set number of classes!'
        self.est_args = {
            'est_type': 'FLXGB',
            'n_folds': n_folds,
            'nthread': nthread,
            'scale_pos_weight': scale_pos_weight,
            'silent': silent,
            'objective': objective,
            'subsample': subsample,
            'n_estimators': num_boost_round,
            'colsample_bytree': colsample_bytree,
            'colsample_bylevel': colsample_bylevel,
            'max_depth': max_depth,
            'learning_rate': learning_rate
        }

class RandomForestConfig(EstimatorConfig):
    """
    Random Forest Argument.
    """
    def __init__(self, n_folds=3, n_estimators=500, max_depth=100, n_jobs=-1, max_features='sqrt',
                 criterion="gini", min_samples_split=2, min_impurity_decrease=0.,
                 min_samples_leaf=1, random_state=None):
        """
        Random Forest Arguments.

        :param n_folds:
        :param n_estimators:
        :param max_depth:
        :param n_jobs:
        :param max_features:
        :param criterion:
        :param min_samples_split:
        :param min_impurity_decrease:
        :param min_samples_leaf:
        :param random_state:
        """
        super(RandomForestConfig, self).__init__()
        self.est_args = {
            'est_type': 'FLRF',
            'n_folds': n_folds,
            'n_estimators': n_estimators,
            'max_depth': max_depth,
            'n_jobs': n_jobs,
            'max_features': max_features,
            'criterion': criterion,
            'min_samples_split': min_samples_split,
            'min_impurity_decrease': min_impurity_decrease,
            'min_samples_leaf': min_samples_leaf,
            'random_state': random_state
        }

class ExtraRandomForestConfig(EstimatorConfig):
    """
    Extremely Random Forest Argument.
    """
    def __init__(self, n_folds=3, n_estimators=500, max_depth=100, n_jobs=-1, max_features=1,
                 criterion="gini", min_samples_split=2, min_impurity_decrease=0.,
                 min_samples_leaf=1, random_state=None):
        """
        Completely Random Forest Arguments, also called Extremely Random Forest.

        :param n_folds:
        :param n_estimators:
        :param max_depth:
        :param n_jobs:
        :param max_features:
        :param criterion:
        :param min_samples_split:
        :param min_impurity_decrease:
        :param min_samples_leaf:
        :param random_state:
        """
        super(ExtraRandomForestConfig, self).__init__()
        self.est_args = {
            'est_type': 'FLCRF',
            'n_folds': n_folds,
            'n_estimators': n_estimators,
            'max_depth': max_depth,
            'n_jobs': n_jobs,
            'max_features': max_features,
            'criterion': criterion,
            'min_samples_split': min_samples_split,
            'min_impurity_decrease': min_impurity_decrease,
            'min_samples_leaf': min_samples_leaf,
            'random_state': random_state
        }

class GBDTConfig(EstimatorConfig):
    """
    Gradient Boosting Decision Tree Argument.
    """
    def __init__(self, n_folds=3, loss='ls', learning_rate=0.1, n_estimators=100,
                 subsample=1.0, criterion='friedman_mse', min_samples_split=2,
                 min_samples_leaf=1, min_weight_fraction_leaf=0.,
                 max_depth=3, min_impurity_decrease=0.,
                 min_impurity_split=None, init=None, random_state=None,
                 max_features=None, alpha=0.9, verbose=0, max_leaf_nodes=None,
                 warm_start=False, presort='auto'):
        """
        Gradient Boosting Decision Tree Argument.

        :param n_folds: how many folds to execute in cross validation
        :param loss:
        :param learning_rate:
        :param n_estimators:
        :param subsample:
        :param criterion:
        :param min_samples_split:
        :param min_samples_leaf:
        :param min_weight_fraction_leaf:
        :param max_depth:
        :param min_impurity_decrease:
        :param min_impurity_split:
        :param init:
        :param random_state:
        :param max_features:
        :param alpha:
        :param verbose:
        :param max_leaf_nodes:
        :param warm_start:
        :param presort:
        """
        super(GBDTConfig, self).__init__()
        self.est_args = {
            'est_type': 'FLGBDT',
            'n_folds': n_folds,
            'loss': loss,
            'learning_rate': learning_rate,
            'n_estimators': n_estimators,
            'criterion': criterion,
            'min_samples_split': min_samples_split,
            'min_samples_leaf': min_samples_leaf,
            'min_weight_fraction_leaf': min_weight_fraction_leaf,
            'max_depth': max_depth,
            'init': init,
            'subsample': subsample,
            'max_features': max_features,
            'min_impurity_decrease': min_impurity_decrease,
            'min_impurity_split': min_impurity_split,
            'random_state': random_state,
            'alpha': alpha,
            'verbose': verbose,
            'max_leaf_nodes': max_leaf_nodes,
            'warm_start': warm_start,
            'presort': presort
        }

class XGBRegressorConfig(EstimatorConfig):
    """
    Sklearn based xgboost regressor.
    """
    def __init__(self, n_folds=3,  max_depth=3, learning_rate=0.1, n_estimators=100,
                 silent=True, objective="reg:linear",
                 nthread=-1, gamma=0, min_child_weight=1, max_delta_step=0,
                 subsample=1, colsample_bytree=1, colsample_bylevel=1,
                 reg_alpha=0, reg_lambda=1, scale_pos_weight=1,
                 base_score=0.5, seed=0, missing=None):
        """
        XGBoost Regressor Argument describes arguments of XGBoost Regressor.
        Parameter can refer to xgboost document: http://xgboost.readthedocs.io/en/latest/python/python_api.html.

        :param n_folds: how many folds to execute in cross validation
        :param nthread: number of threads to execute
        :param scale_pos_weight: used to handle class imbalance
        :param n_estimators:
        :param silent:
        :param reg_lambda:
        :param reg_alpha:
        :param gamma:
        :param min_child_weight: Defines the minimum sum of weights of all observations required in a child.
                                 Used to control over-fitting. Higher values prevent a model from learning relations
                                  which might be highly specific to the particular sample selected for a tree.
                                 Too high values can lead to under-fitting hence, it should be tuned using CV.
        :param base_score:
        :param objective: objective, default is 'reg:linear'
        :param subsample: default=1
        :param colsample_bytree:
        :param colsample_bylevel:
        :param max_depth: default=3, The maximum depth of a tree, same as GBM.
                          Used to control over-fitting as higher depth will allow model to learn relations
                           very specific to a particular sample.
        :param learning_rate: default=0.1
        :param seed:
        """
        super(XGBRegressorConfig, self).__init__()
        self.est_args = {
            'est_type': 'FLXGB',
            'n_folds': n_folds,
            'nthread': nthread,
            'scale_pos_weight': scale_pos_weight,
            'silent': silent,
            'objective': objective,
            'max_delta_step': max_delta_step,
            'n_estimators': n_estimators,
            'reg_lambda': reg_lambda,
            'reg_alpha': reg_alpha,
            'gamma': gamma,
            'min_child_weight': min_child_weight,
            'base_score': base_score,
            'subsample': subsample,
            'colsample_bytree': colsample_bytree,
            'colsample_bylevel': colsample_bylevel,
            'max_depth': max_depth,
            'learning_rate': learning_rate,
            'seed': seed,
            'missing': missing
        }

class LGBMRegressorConfig(EstimatorConfig):
    """
    Sklearn based LightGBM regressor.
    """
    def __init__(self, n_folds=3, boosting_type="gbdt", num_leaves=31, max_depth=-1,
                 learning_rate=0.1, n_estimators=100,
                 subsample_for_bin=200000, objective=None, class_weight=None,
                 min_split_gain=0., min_child_weight=1e-3, min_child_samples=20,
                 subsample=1., subsample_freq=1, colsample_bytree=1.,
                 reg_alpha=0., reg_lambda=0., random_state=None,
                 n_jobs=-1, silent=True):
        """
        Initialize FLLGBMRegressor argument.

        :param n_folds:
        :param boosting_type:
        :param num_leaves: LightGBM use num_leaves to limit tree complexity.
                           Approximately, there are `num_leaves = 2^(max_depth)`.
        :param max_depth:
        :param learning_rate:
        :param n_estimators:
        :param subsample_for_bin:
        :param objective:
        :param class_weight:
        :param min_split_gain:
        :param min_child_weight:
        :param min_child_samples:
        :param subsample:
        :param subsample_freq:
        :param colsample_bytree:
        :param reg_alpha:
        :param reg_lambda:
        :param random_state:
        :param n_jobs:
        :param silent:
        """
        super(LGBMRegressorConfig, self).__init__()
        self.est_args = {
            'est_type': 'FLLGBM',
            'n_folds': n_folds,
            'boosting_type': boosting_type,
            'num_leaves': num_leaves,
            'max_depth': max_depth,
            'learning_rate': learning_rate,
            'n_estimators': n_estimators,
            'subsample_for_bin': subsample_for_bin,
            'objective': objective,
            'class_weight': class_weight,
            'min_split_gain': min_split_gain,
            'min_child_weight': min_child_weight,
            'min_child_samples': min_child_samples,
            'subsample': subsample,
            'subsample_freq': subsample_freq,
            'colsample_bytree': colsample_bytree,
            'reg_alpha': reg_alpha,
            'reg_lambda': reg_lambda,
            'random_state': random_state,
            'n_jobs': n_jobs,
            'silent': silent
        }

def Basic4x2(n_folds=3, n_estimators=500, max_depth=100, n_jobs=-1, min_samples_leaf=1):
    crf = ExtraRandomForestConfig(n_folds=n_folds,
                                  n_estimators=n_estimators,
                                  max_depth=max_depth,
                                  n_jobs=n_jobs,
                                  min_samples_leaf=min_samples_leaf)
    rf = RandomForestConfig(n_folds=n_folds,
                            n_estimators=n_estimators,
                            max_depth=max_depth,
                            n_jobs=n_jobs,
                            min_samples_leaf=min_samples_leaf)
    est_configs = [
        crf, crf, crf, crf,
        rf, rf, rf, rf
    ]
    return est_configs