import pandas as pd

import datetime as dt

from lifetimes import BetaGeoFitter
from lifetimes import GammaGammaFitter
from lifetimes.plotting import plot_period_transactions

pd.set_option('display.max_columns', None)
pd.set_option('display.float_format', lambda x: '%3f' % x)

df_ = pd.read_csv("datasets/flo_data_20k.csv")
df = df_.copy()

def outlier_thresholds(dataframe, variable):
    quartile1 = dataframe[variable].quantile(0.01)
    quartile3 = dataframe[variable].quantile(0.99)
    interquantile_range = quartile3 - quartile1
    up_limit = quartile3 + 1.5 * interquantile_range
    low_limit = quartile1 - 1.5 * interquantile_range

    return round(low_limit), round(up_limit)

def replace_with_threshold(dataframe, variable):
    low_limit, up_limit = outlier_thresholds(dataframe, variable)
    dataframe.loc[(dataframe[variable] > up_limit), variable] = up_limit

replace_with_threshold(df, "order_num_total_ever_online")
replace_with_threshold(df, "order_num_total_ever_offline")
replace_with_threshold(df, "customer_value_total_ever_offline")
replace_with_threshold(df, "customer_value_total_ever_online")

df["total_order_count"] = df["order_num_total_ever_online"] + df["order_num_total_ever_offline"]
df["total_price"] = df["customer_value_total_ever_offline"] + df["customer_value_total_ever_online"]

def change_date(dataframe, variable):
    dataframe[variable] = dataframe[variable].astype("datetime64[ns]")

change_date(df, "first_order_date")
change_date(df, "last_order_date")
change_date(df, "last_order_date_online")
change_date(df, "last_order_date_offline")

last_date = df["last_order_date"].max() # 2021-05-30

analyze_date = dt.datetime(2021, 6, 1)

cltv = df[["master_id", "total_order_count"]]

cltv["recency"] = (df["last_order_date"] - df["first_order_date"]) \
    .apply(lambda date: date.days)

cltv["T"] = (analyze_date - df["first_order_date"]) \
    .apply(lambda date: date.days)

cltv["monetary"] = df["total_price"] / df["total_order_count"]

cltv.columns = ["master_id", "frequency", "recency_cltv_weekly", "T_weekly", "monetary_cltv_avg"]

cltv = cltv[cltv["frequency"] > 1]

cltv["frequency"] = cltv["frequency"].astype("int")

cltv["recency_cltv_weekly"] = cltv["recency_cltv_weekly"] / 7
cltv["T_weekly"] = cltv["T_weekly"] / 7

bgf = BetaGeoFitter(penalizer_coef=0.001)

bgf.fit(cltv["frequency"],
        cltv["recency_cltv_weekly"],
        cltv["T_weekly"])

cltv["exp_sales_3_month"] = bgf \
    .conditional_expected_number_of_purchases_up_to_time(12, cltv["frequency"],
                                                         cltv["recency_cltv_weekly"],
                                                         cltv["T_weekly"])

cltv["exp_sales_6_month"] = bgf \
    .conditional_expected_number_of_purchases_up_to_time(24, cltv["frequency"],
                                                         cltv["recency_cltv_weekly"],
                                                         cltv["T_weekly"])
plot_period_transactions(bgf)

ggf = GammaGammaFitter(penalizer_coef=0.01)

ggf.fit(cltv["frequency"], cltv["monetary_cltv_avg"])

cltv["exp_average_value"] = ggf \
    .conditional_expected_average_profit(cltv["frequency"], cltv["monetary_cltv_avg"])

exp_6_month_end = ggf.customer_lifetime_value(bgf,
                                          cltv['frequency'],
                                          cltv['recency_cltv_weekly'],
                                          cltv['T_weekly'],
                                          cltv['monetary_cltv_avg'],
                                          time=6, # Month
                                          freq="W",
                                          discount_rate=0.01)
cltv["cltv"] = exp_6_month_end

cltv["segment"] = pd.qcut(cltv["cltv"], 4, labels=["D", "C", "B", "A"])

analyze = cltv.groupby("segment").agg({"count", "mean", "sum"})


"""setup.py"""
from os.path import dirname, join, abspath
from setuptools import setup, find_packages

__DESCRIPTION = """Unofficial Claude2 API supporting direct HTTP chat creation/deletion/retrieval, \
multiple message attachments, proxies and auto session gathering using Firefox with geckodriver. \
"""

with open(
    join(abspath(dirname(__file__)), "README.md"),
    "r",
    encoding="utf-8",
    errors="ignore",
) as fp:
    __LONG_DESCRIPTION = fp.read().lstrip().rstrip()

setup(
    name="unofficial-claude2-api",
    version="0.2.4",
    author="st1vms",
    author_email="stefano.maria.salvatore@gmail.com",
    description=__DESCRIPTION,
    long_description=__LONG_DESCRIPTION,
    long_description_content_type="text/markdown",
    url="https://github.com/st1vms/unofficial-claude2-api",
    packages=find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3.9",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires=">=3.9",
    install_requires=[
        "selgym",
        "curl_cffi",
        "tzlocal",
        "brotli",
    ],
)


# -*- coding:utf-8 -*-
import os
import random
import string

if __name__ == '__main__':
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_add_rbac.settings")
    import django
    django.setup()

    # 插入数据
    from blog import models

    category_choices = [4,2,3]
    dates = ['2011-12-12','2012-3-5','2018-12-5']
    blog_lst = [1,2,3]

    lst = []
    # 插入5个数据
    for i in range(7):

        random_title = ''.join(random.choices(string.ascii_letters, k=5))

        random_category = random.choice(category_choices)

        random_content = ''.join(random.choices(string.ascii_letters, k=9))

        random_date = random.choice(dates)

        random_blog = random.choice(blog_lst)

        customer_obj = models.Article(title=random_title,category=random_category,content=random_content,create_at=random_date,blog_id=random_blog)
        lst.append(customer_obj)

    models.Article.objects.bulk_create(lst)


import os
import re
import nltk
from collections import Counter
# Define paths
malepath = '/Users/dongyuewu/Desktop/Male'
femalepath = '/Users/dongyuewu/Desktop/Female'
# Extract all the text names in a list
Malenameslist = os.listdir(malepath)
Femalenameslist = os.listdir(femalepath)
# Remove an unrelated file name produced by MacOS.
Malenameslist.remove(".DS_Store")
Femalenameslist.remove(".DS_Store")
# Combine all the text files in one txt file.
with open("Maleresult.txt", "w") as outfile:
    for f in Malenameslist:
        with open(malepath+'/'+f) as infile:
            outfile.write(infile.read())
with open("Femaleresult.txt", "w") as outfile:
    for f in Femalenameslist:
        with open(femalepath+'/'+f) as infile:
            outfile.write(infile.read())
# Clean and organize text into a list.
 # Male
maletxt = open("Maleresult.txt").read()
puremaletxt = re.sub(r'\d+','',maletxt)
puremaletxt = re.sub(r"[^\w\d'\s]+",'',puremaletxt)
malelist1 = puremaletxt.split("He")
malelist1.remove('')
finalmalelist = []
for sentence in malelist1:
    he = str("He" + sentence.replace("\n", ""))
    finalmalelist.append(he)
print(finalmalelist)
 # Female
femaletxt = open("Femaleresult.txt").read()
purefemaletxt = re.sub(r'\d+','',femaletxt)
purefemaletxt = re.sub(r"[^\w\d'\s]+",'',purefemaletxt)
femalelist1 = purefemaletxt.split("She")
femalelist1.remove('')
finalfemalelist = []
for sentence in femalelist1:
    she = str("She" + sentence.replace("\n", ""))
    finalfemalelist.append(she)
print(finalfemalelist)
# Sentiment analysis
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
sia = SentimentIntensityAnalyzer()
def get_sentiment(text):
    return sia.polarity_scores(text)
# Create a dictionary containing the characters and corresponding scores.
 # Male
Malescore = {}
for male in finalmalelist:
    Malescore[male] = get_sentiment(male)['compound']
Malescoreordered = sorted(Malescore.items(),key=lambda x: x[1])
Toptenmale = Malescoreordered[-11:-1]
Bottomtenmale = Malescoreordered[0:10]
Toptenmalelist = []
for l in Toptenmale:
    Toptenmalelist.append(l[0])
Bottomtenmalelist = []
for b in Bottomtenmale:
    Bottomtenmalelist.append(b[0])
# Write Malelists into .txt files
with open('/Users/dongyuewu/Desktop/Top10Male.txt', mode='wt', encoding='utf-8') as file1:
    file1.write('\n'.join(Toptenmalelist))
with open('/Users/dongyuewu/Desktop/Bottom10Male.txt', mode='wt', encoding='utf-8') as file2:
    file2.write('\n'.join(Bottomtenmalelist))
 # Female
Femalescore = {}
for female in finalfemalelist:
    Femalescore[female] = get_sentiment(female)['compound']
Femalescoreordered = sorted(Femalescore.items(),key=lambda x: x[1])
Toptenfemale = Femalescoreordered[-11:-1]
Bottomtenfemale = Femalescoreordered[0:10]
Toptenfemalelist = []
for l in Toptenfemale:
    Toptenfemalelist.append(l[0])
Bottomtenfemalelist = []
for b in Bottomtenfemale:
    Bottomtenfemalelist.append(b[0])
# Write Malelists into .txt files
with open('/Users/dongyuewu/Desktop/Top10Female.txt', mode='wt', encoding='utf-8') as file3:
    file3.write('\n'.join(Toptenfemalelist))
with open('/Users/dongyuewu/Desktop/Bottom10Female.txt', mode='wt', encoding='utf-8') as file4:
    file4.write('\n'.join(Bottomtenfemalelist))
# Find 10 most common descriptors
 #Male
Malewords = re.findall(r'\w+',puremaletxt)
Malecap_words = [word.upper() for word in Malewords]
Maleword_count = Counter(Malecap_words)
# Remove the pronouns, articles and prepositions.
dellist = ['A','SHE','HE','S','THE','AN','WITH','HIS','ON','FOR','OF','FROM','WHO','IN','TO','BY','AND','HER']
for n in dellist:
    del Maleword_count[n]
Maleword_count = sorted(Maleword_count.items(),key=lambda x: x[1])
Mtencommon = []
for i in range(-11,-1):
    Mtencommon.append(str(Maleword_count[i][0])+':'+ str(Maleword_count[i][1]))

 #Female
Femalewords = re.findall(r'\w+',purefemaletxt)
Femalecap_words = [word.upper() for word in Femalewords]
Femaleword_count = Counter(Femalecap_words)
for n in dellist:
    del Femaleword_count[n]
Femaleword_count = sorted(Femaleword_count.items(),key=lambda x: x[1])
Ftencommon = []
for i in range(-11,-1):
    Ftencommon.append(str(Femaleword_count[i][0])+':'+str(Femaleword_count[i][1]))

# Write 10 most common descriptors into .txt files.
with open('/Users/dongyuewu/Desktop/10commonMale.txt', mode='wt', encoding='utf-8') as file5:
    file5.write('\n'.join(Mtencommon))
with open('/Users/dongyuewu/Desktop/10commonFemale.txt', mode='wt', encoding='utf-8') as file6:
    file6.write('\n'.join(Ftencommon))


from typing import Any, Optional, List
from app.dependencies.auth import TokenUser

from fastapi import APIRouter, Depends
from pydantic import BaseModel
from sqlalchemy import String, bindparam
from sqlalchemy.orm import Session
from app.db.deps import get_db
from app.db.utils import save_model
from app.dependencies.auth import JWTService, credential_check
from app.models.user import User, UserIn, UserOut

router = APIRouter()

@router.get("/user", response_model=UserOut)
async def get_user(db: Session = Depends(get_db),user : TokenUser = Depends(credential_check)):
    user = db.query(User).filter(User.id == user.id).one_or_none()
    return user

@router.get("/", response_model=List[UserOut])
async def get_users(
    search: Optional[str] = None,
    limit: Optional[int] = 100,
    skip: Optional[int] = 0,
    db: Session = Depends(get_db),
):
    db_result = db.query(User)

    if search:
        db_result = db_result.filter(User.name.like(bindparam("search", type_=String))).params(search=f"%{search}%")

    return db_result.limit(limit).offset(skip).all()

class JwtResponse(BaseModel):
    jwt: str

@router.post(
    "/login",
    response_model=JwtResponse,
)
def post_user(user: UserIn, db: Session = Depends(get_db)):

    model = db.query(User).filter(User.username == user.username).one_or_none()
    if model == None:
        model = save_model(db, User(username=user.username))

    token = JWTService().encode({"id": str(model.id)})

    return {"jwt": str(token)}


from .predictor import Predictor
from dacapo.experiments import Model
from dacapo.experiments.arraytypes import EmbeddingArray
from dacapo.experiments.datasplits.datasets.arrays import NumpyArray

import numpy as np
import torch

class DummyPredictor(Predictor):
    def __init__(self, embedding_dims):
        self.embedding_dims = embedding_dims

    def create_model(self, architecture):
        head = torch.nn.Conv3d(
            architecture.num_out_channels, self.embedding_dims, kernel_size=3
        )

        return Model(architecture, head)

    def create_target(self, gt):
        # zeros
        return NumpyArray.from_np_array(
            np.zeros((self.embedding_dims,) + gt.data.shape[-gt.dims :]),
            gt.roi,
            gt.voxel_size,
            ["c"] + gt.axes,
        )

    def create_weight(self, gt, target, mask, moving_class_counts=None):
        # ones
        return (
            NumpyArray.from_np_array(
                np.ones(target.data.shape),
                target.roi,
                target.voxel_size,
                target.axes,
            ),
            None,
        )

    @property
    def output_array_type(self):
        return EmbeddingArray(self.embedding_dims)


import numpy as np
import torch
from torch import nn

# Function to apply the solver method
def euler(f, x0, t0, tf, dt, u, *kargs):

    """
    Solve a system of differential equations using the Euler method.

    Parameters:
    f (function): The derivative function.
    x0 (ndarray): The initial state.
    t0 (float): The initial time.
    tf (float): The final time.
    dt (float): The time step.
    u (function): The input function.

    Returns:
    ndarray: The state at each time step.
    """
    t = np.arange(t0, tf, dt)
    x = np.zeros((len(t), len(x0)))
    x[0, :] = x0
    for i in range(1, len(t)):
        # print(x[i,:])
        x[i, :] = x[i-1, :] + dt * f(x[i-1, :], t[i-1], u(t[i-1]), *kargs)
    return x

#@title solver
class SolverTemplate(nn.Module):
    def __init__(self, order, min_factor: float = 0.2, max_factor: float = 10, safety: float = 0.9):
        super().__init__()
        self.order = order
        self.min_factor = torch.tensor([min_factor])
        self.max_factor = torch.tensor([max_factor])
        self.safety = torch.tensor([safety])
        self.tableau = None

    def sync_device_dtype(self, x, t_span):
        "Ensures `x`, `t_span`, `tableau` and other solver tensors are on the same device with compatible dtypes"

        if isinstance(x, dict):
            proto_arr = x[list(x.keys())[0]]
        elif isinstance(x, torch.Tensor):
            proto_arr = x
        else:
            raise NotImplementedError(
                f"{type(x)} is not supported as the state variable")

        device = proto_arr.device

        if self.tableau is not None:
            c, a, bsol, berr = self.tableau
            self.tableau = c.to(proto_arr), [a.to(proto_arr) for a in a], bsol.to(
                proto_arr), berr.to(proto_arr)
        t_span = t_span.to(device)
        self.safety = self.safety.to(device)
        self.min_factor = self.min_factor.to(device)
        self.max_factor = self.max_factor.to(device)
        return x, t_span

    def step(self, f, x, t, dt, k1=None, args=None):
        pass

class DirectSolver(SolverTemplate):
    def __init__(self, dtype=torch.float32):
        super().__init__(order=1)
        self.dtype = dtype
        self.stepping_class = "fixed"

    def step(self, f, x, t, dt, k1=None, args=None):

        x_sol = f(t, x)
        return None, x_sol, None

class ResnetSolver(SolverTemplate):
    def __init__(self, step_size=None, dtype=torch.float32):
        super().__init__(order=1)
        self.dtype = dtype
        self.stepping_class = "fixed"

        self.step_size = 1 if step_size is None else step_size

    def step(self, f, x, t, dt, k1=None, args=None):

        x_sol = x + f(t, x) * self.step_size
        return None, x_sol, None


'''This class is built to handle the concept of a "network." If the requested
scan is just a ping sweep then main calls the ping sweep method, and if a port
scan is requested then this class created a Host object for each address in the
provided network, then calls upon the methods in host to conduct a scan on each
host.'''

import time
import ipaddress
from host import Host
from scan_threader import ScanThreader

class Network:
    '''Host ips is parsed using the clean up function, and hosts is initialized as
    an empty list, to be later populated as a list of Host objects. Similarly, up_hosts
    is initialized to represent the hosts that responded to pings so that Host objects
    aren't created for nonexistent hosts.'''
    def __init__(self, args):
        self.args = args
        self.host_ips = clean_up_ips(args.hosts)
        self.hosts = []
        self.up_hosts = set()

    def scan_hosts(self):
        '''Resets hosts and up_hosts so if the scanning is continuous, repeats
        aren't created, then conducts a ping sweep (if user didn't request to skip
        it) and creates a Host object for all alive hosts, then calls the function
        to conduct port scans, and reports various pieces of useful information
        about the status of the scans'''
        self.hosts = []
        self.up_hosts = set()
        if not self.args.skip:
            print('Initiating ping sweep...')
            if self.args.verbosity >= 1:
                print('Using ping sweep to reduce hosts for port scan.'
                    + 'Use -s or --skip to force port scans on all provided hosts.\n')
            self.ping_sweep()
        else:
            self.up_hosts = self.host_ips
            print('Skipping ping sweep...')

        for addr in self.up_hosts:
            self.hosts.append(Host(addr, self.args))

        if self.up_hosts:
            print(f'Initiating port scan on {len(self.up_hosts)} host(s)...')
            print('-' * 30 + '\n')
            start_time = time.time()

            for host in self.hosts:
                host.scan_host()

            elapsed = time.time() - start_time
            print(f'Scan on {len(self.hosts)} host(s) complete in {elapsed:.2f} seconds\n')

    def ping_sweep(self):
        '''This makes use of the ScanThreader class to push out pings to the hosts
        provided by the user, then reports on status and send the results back'''
        start_time = time.time()
        if self.args.verbosity >= 1 or not self.args.ports:
            print(f'Beginning ping sweep on {len(self.host_ips)} hosts...\n')

        scan = ScanThreader('Ping Sweep', self.host_ips, self.args.num_threads, self.args.verbosity)
        self.up_hosts = scan.scan()

        elapsed = time.time() - start_time
        return_str = f'\nPing sweep complete on {len(self.host_ips)} host\n'
        if self.up_hosts:
            for host in self.up_hosts:
                return_str += f'[+] Host {host} is up\n'
        else:
            return_str += 'All hosts scanned are either down or not responding to pings\n'

        if self.args.verbosity >= 1 and self.args.ports:
            print(f'Scanned {len(self.host_ips)} host(s) in {elapsed:.2f} seconds\n {"-" * 30}\n')
            print('-' * 30 + '\n')
            print(return_str)

        return return_str

    def __str__(self):
        '''Turn information about the network into pretty formatting'''
        return_str = ''
        if self.up_hosts:
            return_str += f'{len(self.host_ips)} host(s) scanned:\n'
        else:
            return_str += 'No hosts detected, cancelling port scan...\n'
        return_str += '-' * 30 + '\n'
        for each_host in self.hosts:
            return_str += str(each_host)
        return return_str

def clean_up_ips(input_ips):
    '''This function is used recursively to parse user input for ips into a usable format.
    A set is used so that duplicates can't be added to the final list and therefore unecessarily
    scanned twice. On each loop the item in the set is searched for a comma, dash, or backslash,
    and if any are found then it's split on the comma, or the range is expanded, or
    a CIDR network is provided using the ipaddress libary. IF any of these indicators are found,
    then a subset is created and recursively sent through the function until finally no items in
    the list flag on any of these indicators and the recursion folds into itself back up to the
    original function call'''
    if isinstance(input_ips, str):
        input_ips = set([input_ips])

    output_ips = set()
    for host in input_ips:
        if ',' in host:
            for item in host.split(','):
                output_ips.update(clean_up_ips(item))
        elif '-' in host:
            octets = host.split('.')
            subnet_request = False
            for octet in octets:
                if '-' in octet:
                    index = octets.index(octet)
                    start, end = octet.split('-')
                    if '/' in end:
                        end, subnet = end.split('/')
                        subnet_request = True
                    octet_range = range(int(start), int(end)+1)
            for octet in octet_range:
                octets[index] = (str(octet) + '/' + subnet) if subnet_request else octet
                output_ips.update(clean_up_ips(f'{octets[0]}.{octets[1]}.{octets[2]}.{octets[3]}'))
        elif '/' in host:
            for addr in ipaddress.IPv4Network(host, strict=False):
                output_ips.add(str(addr))
        else:
            output_ips.add(host)
    return output_ips


import serial
import requests
from bs4 import BeautifulSoup

form_url = str(input('Enter URL to Google Form: '))
serial_port = str(input('Enter your Arduino serial port address: '))
data_rate_baud = str(input('Enter your data rate from Arduino in baud: '))

def get_entry():
    page = requests.get(form_url)
    soup = BeautifulSoup(page.content, 'html.parser')

    return (soup.input['name'])

def get_form_response():
    page = requests.get(form_url)
    soup = BeautifulSoup(page.content, 'html.parser')

    return (soup.form['action'])

# print(get_formResponse())
# print(get_entry())

entry = get_entry()
formResponse_url = get_form_response()

url = formResponse_url
form_data = {entry: '', 'draftResponse':[], 'pageHistory':0}
user_agent = {'Referer': form_url, 'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:10.0) Gecko/20100101 Firefox/10.0"}

ser = serial.Serial(serial_port, data_rate_baud)

n = 0
while n < 10:
    data = float(ser.readline())
    form_data[entry] = str(data)
    print("add datapoint", data, "to google forms")
    r = requests.post(url, data=form_data, headers=user_agent)
    n = n+1


"""Platform mock upload module."""
import base64
import datetime
import hashlib
import json
import os
import signal
import uuid

from insights.core import archives, dr
from insights.core.archives import InvalidContentType
from insights.core.context import HostArchiveContext
from insights.core.hydration import create_context
from insights.specs import Specs
from insights.parsers.dnf_modules import DnfModules
from insights.parsers.installed_rpms import Installed
from insights.parsers.yum_repos_d import YumReposD
from tornado.ioloop import IOLoop
from tornado.web import Application, RequestHandler

from common.logging import init_logging, get_logger
from common import mqueue
from common.database_handler import DatabasePool, DatabasePoolConnection

LOGGER = get_logger(__name__)
STORAGE_PATH = "/tmp/storage"

class BaseHandler(RequestHandler):
    """Base Handler."""

    def _get_rh_account(self):
        if "x-rh-identity" not in self.request.headers:
            return "0"
        encoded_value = self.request.headers["x-rh-identity"]
        decoded_value = base64.b64decode(encoded_value).decode("utf-8")
        identity = json.loads(decoded_value)
        return identity.get("identity", {}).get("account_number", "0")

class UploadHandler(BaseHandler):
    """Upload Handler."""

    def data_received(self, chunk):
        pass

    @staticmethod
    def _get_system_profile(archive_path):
        profile = {}
        default_packages = (
            "insights.specs.default",
            "insights.specs.insights_archive",
            "insights.combiners",
            "insights.parsers"
        )
        for pkg in default_packages:
            dr.load_components(pkg)
        broker = dr.Broker()
        try:
            with archives.extract(archive_path) as ex:
                ctx = create_context(ex.tmp_dir, HostArchiveContext)
                broker[ctx.__class__] = ctx
                broker = dr.run(components=[Specs.machine_id, Installed, DnfModules, YumReposD],
                                broker=broker)
                if Specs.machine_id in broker:
                    profile["id"] = broker[Specs.machine_id].content[0].strip()
                profile["installed_packages"] = []
                if Installed in broker:
                    pkglist = broker[Installed]
                    for pkg_name in pkglist.packages:
                        pkg = pkglist.get_max(pkg_name)
                        profile["installed_packages"].append(pkg.nevra)

                profile["yum_repos"] = []
                if YumReposD in broker:
                    repolist = broker[YumReposD]
                    for repo_file in repolist:
                        if repo_file.file_name == 'redhat.repo':
                            for repo in repo_file:
                                if repo_file[repo].get('enabled', '1').lower() in ('1', 'true', 'enabled', 'yes', 'on'):
                                    profile["yum_repos"].append(repo)
                            break

                profile["dnf_modules"] = []
                if DnfModules in broker:
                    for module in broker[DnfModules]:
                        for module_name in module.sections():
                            profile["dnf_modules"].append({'name': module_name,
                                                           'stream': module.get(module_name, 'stream')})
                LOGGER.info(profile)
        except InvalidContentType:
            LOGGER.error("Unable to parse archive.")
        return profile

    def _get_upload_multiplier(self):
        if "x-upload-multiplier" not in self.request.headers:
            return 1
        multiplier = self.request.headers["x-upload-multiplier"]
        return int(multiplier) if multiplier.isdigit() else 1

    def post(self):
        """Answer POST request.
           curl -X POST -F "file=@./file.tar.gz" http://localhost:8100/api/v1/upload
           curl -X POST -F "file=@./file.tar.gz" -H "x-upload-multiplier: 10" http://localhost:8100/api/v1/upload
        """
        if self.request.files and "file" in self.request.files:
            sha1 = hashlib.sha1(self.request.files["file"][0]["body"]).hexdigest()
            file_name = "%s.tar.gz" % sha1
            file_path = os.path.join(STORAGE_PATH, file_name)
            if not os.path.exists(file_path):
                with open(file_path, "wb") as open_file:
                    open_file.write(self.request.files["file"][0]["body"])
            if sha1 in self.application.archive_to_profile_cache:
                profile = self.application.archive_to_profile_cache[sha1]
            else:
                profile = self._get_system_profile(file_path)
                self.application.archive_to_profile_cache[sha1] = profile
                self.application.inventory_id_to_profile_cache[profile.get("id", None)] = profile
            download_url = "http://platform_mock:8000/api/v1/download/%s" % file_name
            rh_account = self._get_rh_account()
            timestamp = datetime.datetime.utcnow().isoformat()
            upload_message = {"host": {"id": profile.get("id", None), "account": rh_account,
                                       "display_name": sha1 + ".example.com",
                                       "system_profile": {
                                           "installed_packages": profile["installed_packages"],
                                           "yum_repos": [{"id": r, "name": r, "enabled": True} for r in profile["yum_repos"]],
                                           "dnf_modules": profile["dnf_modules"],
                                           "insights_client_version": "3.0.13-1"},
                                       "insights_id": "0"
                                       },
                              "platform_metadata": {"request_id": str(uuid.uuid1()), "url": download_url,
                                                    "b64_identity": self.request.headers.get("x-rh-identity", "")},
                              "timestamp": timestamp,
                              "type": "created"
                              }
            for _ in range(self._get_upload_multiplier()):
                self.application.queue.send(upload_message)
                with DatabasePoolConnection() as conn:
                    with conn.cursor() as cur:
                        cur.execute("""
                                        insert into inventory.hosts_v1_1 values (%s, 0, %s, '[]', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '2030-07-21 05:35:53.682554+00', '{}')
                                        on conflict (id) do update set display_name = EXCLUDED.display_name, updated = CURRENT_TIMESTAMP
                                     """, (upload_message["host"]["id"],
                                           upload_message["host"]["display_name"]))
                    conn.commit()
            LOGGER.info("New upload: %s", upload_message)
        else:
            self.set_status(400)
        self.finish()

class DownloadHandler(BaseHandler):
    """Download Handler."""

    def data_received(self, chunk):
        pass

    def get(self, path):
        """Answer GET request."""
        file_path = os.path.join(STORAGE_PATH, path)
        if not os.path.isfile(file_path):
            self.set_status(404)
        else:
            self.set_header("Content-Type", "application/octet-stream")
            with open(file_path, "rb") as open_file:
                self.write(open_file.read())
        self.finish()

class DeleteHandler(BaseHandler):
    """Delete Handler."""

    def data_received(self, chunk):
        pass

    def delete(self, inventory_id):
        """Answer DELETE request."""
        rh_account = self._get_rh_account()
        delete_message = {"type": "delete", "id": inventory_id, "account": rh_account}
        self.application.queue.send(delete_message)
        LOGGER.info("Delete: %s", delete_message)
        self.finish()

class RbacHandler(BaseHandler):
    """RBAC mock"""

    def data_received(self, chunk):
        pass

    def get(self):
        """Answer GET request"""
        self.write(json.dumps({'meta': {'count': 1, 'limit': 10, 'offset': 0},
                               'links': {'first': '/api/rbac/v1/access/?application=vulnerability&limit=1000&offset=0',
                                         'next': None, 'previous': None, 'last': '/api/rbac/v1/access/?application=vulnerability&limit=1000&offset=0'},
                               'data': [{'permission': 'vulnerability:*:*', 'resourceDefinitions': []}]}))

class InsightsRulesHandler(BaseHandler):
    """Insights rule API mock"""

    def data_received(self, chunk):
        pass

    def get(self, rule_id):  # pylint: disable=unused-argument
        """Answer GET request"""
        self.write("""{
            "rule_id": "CVE_2017_14491_dnsmasq|CVE_2017_14491_ERROR",
            "created_at": "2019-02-07T19:02:34.162678Z",
            "updated_at": "2020-02-21T13:39:30.779402Z",
            "ruleset": {
                "created_at": "2018-12-21T01:33:00Z",
                "updated_at": "2018-12-21T01:33:00Z",
                "rule_source": "https://gitlab.cee.redhat.com/insights-open-source/insights-security",
                "description": "Security"
            },
            "description": "CVE-2017-14491: dnsmasq code execution with listening processes",
            "active": true,
            "category": {
                "id": 2,
                "name": "Security"
            },
            "impact": {
                "name": "Remote Code Execution",
                "impact": 4
            },
            "likelihood": 4,
            "node_id": "3199382",
            "tags": "cve security",
            "playbook_count": 1,
            "reboot_required": false,
            "publish_date": "2017-10-02T13:00:00Z",
            "summary": "A buffer overflow vulnerability was found in `Dnsmasq`, a popular lightweight DNS and DHCP server. This can lead to remote code execution. Dnsmasq is used either standalone or directly by applications including `libvirt`.\n",
            "generic": "A vulnerability was discovered in Dnsmasq which allows an attacker to overflow a heap buffer and crash or take control of Dnsmasq. This is accomplished through DNS requests to Dnsmasq querying a domain controlled by the attacker. \n\nDnsmasq is a popular lightweight DNS and DHCP server, often used in home networks and cloud environments as a caching DNS\nstub resolver and to manage DHCP leases. It is used either standalone or directly by applications including libvirt, and\nin a number of layered products. libvirt is a management tool for managing Linux containers and virtual machines.\n\nThis vulnerability can lead to remote code execution and could be triggered by a malicious user on the network.\n\nRed Hat recommends that you update Dnsmasq packages to include the [CVE-2017-14491](http://access.redhat.com/security/cve/CVE-2017-14491) security release.",
            "reason": "This system is vulnerable because:\n\n* It is running a vulnerable package",
            "more_info": "* For more information about this specific flaw, see its [knowledge base article](https://access.redhat.com/security/vulnerabilities/3199382).\n*",
            "impacted_systems_count": 0,
            "reports_shown": false,
            "resolution_set": [
                {
                "system_type": 105,
                "resolution": "Red Hat recommends that you update the `dnsmasq` package",
                "resolution_risk": {
                    "name": "Update Package",
                    "risk": 1
                },
                "has_playbook": true
                }
            ],
            "total_risk": 4,
            "hosts_acked_count": 0,
            "rating": 0
            }""".replace('\n', ''))

class InventoryHandler(BaseHandler):
    """Inventory mock"""

    def data_received(self, chunk):
        pass

    def get(self, inventory_id):
        """Answer GET request"""
        if inventory_id not in self.application.inventory_id_to_profile_cache:
            self.set_status(404)
        else:
            prof = self.application.inventory_id_to_profile_cache[inventory_id]
            self.set_header("Content-Type", "application/json")
            self.write(
                json.dumps({"results": [{
                    "id": inventory_id,
                    "system_profile": {
                        "installed_packages": prof["installed_packages"],
                        "yum_repos": [{"id": r, "name": r, "enabled": True} for r in prof["yum_repos"]],
                        "dnf_modules": prof["dnf_modules"]}
                }]}))
        self.finish()

class PatchAdvisoriesHandler(BaseHandler):
    """Patchman Advisories mock"""

    def data_received(self, chunk):
        pass

    def get(self, system_id):  # pylint: disable=unused-argument
        """Answer GET request"""
        self.write(json.dumps({"data": [
            {
                "attributes": {
                    "advisory_type": 1,
                    "applicable_systems": 10,
                    "description": "The bash packages provide Bash...",
                    "public_date": "2020-09-01T15:22:07Z",
                    "severity": 2,
                    "synopsis": "Moderate: bash security update"},
                "id": "RHSA-2020:3592",
                "type": "advisory"}],
            'links': {},
            'meta': {}}))
        self.finish()

class PatchSystemsHandler(BaseHandler):
    """Patchman Systems mock"""

    def data_received(self, chunk):
        pass

    def get(self, advisory_id):  # pylint: disable=unused-argument
        """Answer GET request"""
        self.write(json.dumps({"data": [
            {
                "attributes": {
                    "display_name": "inv-3.example.com",
                    "last_evaluation": "2020-10-05T16:56:12.810595Z",
                    "last_upload": "2020-10-05T16:56:12.612509Z",
                    "rhsa_count": 1,
                    "rhba_count": 5,
                    "rhea_count": 0,
                    "stale": False,
                    "packages_installed": 1,
                    "packages_updatable": 1
                },
                "id": "00000000-0000-0000-0000-000000000003",
                "type": "system"}],
            'links': {},
            'meta': {}}))
        self.finish()

class PatchViewsSystemsAdvHandler(BaseHandler):
    """Patchman /views/systems/advisories mock"""

    def data_received(self, chunk):
        pass

    def post(self):
        """Answer POST request"""
        data = json.loads(self.request.body)

        systems = data.get('systems', [])
        advisories = data.get('advisories', [])

        if len(systems) >= 1 and len(advisories) >= 1:
            self.write(json.dumps({"data": {
                '00000000-0000-0000-0000-000000000005': ['RHSA-2014:1999'],
            }}))
        else:
            self.write(json.dumps({"data": {}}))
        self.finish()

class ExploitsHandler(BaseHandler):
    """Backoffice-proxy for prod sec /v1/exploits mock"""

    def data_received(self, chunk):
        pass

    def get(self):
        """Answer GET request"""
        self.write(json.dumps([
            {
                "cves": [
                    "CVE-2016-1"
                ],
                "exploits": [
                    {
                        "cvss3_temporal_metrics": "E:F/RL:U/RC:R",
                        "disclosure_date": "2010-10-18T00:00:00Z",
                        "first_seen": "2020-09-18T14:40:52Z",
                        "last_updated": "2020-09-22T02:56:51Z",
                        "name": "exploit/linux/local/name",
                        "source": "metasploit",
                        "summary": "some summary"
                    },
                ],
                "id": 1
            },
            {
                "cves": [
                    "CVE-2013-1"
                ],
                "exploits": [
                    {
                        "cvss3_temporal_metrics": "E:F/RL:U/RC:R",
                        "disclosure_date": "2010-10-18T00:00:00Z",
                        "first_seen": "2020-09-18T14:40:52Z",
                        "last_updated": "2020-09-22T02:56:51Z",
                        "name": "exploit/linux/local/name",
                        "source": "metasploit",
                        "summary": "some summary"
                    },
                ],
                "id": 2
            },
        ]))
        self.finish()

class ServerApplication(Application):
    """Platform mock application."""

    def __init__(self):
        handlers = [
            (r"/api/v1/upload/?", UploadHandler),
            (r"/api/v1/download/(.+)", DownloadHandler),
            (r"/api/v1/delete/(.+)", DeleteHandler),
            (r"/api/rbac/v1/access.+", RbacHandler),
            (r"/api/inventory/v1/hosts/(.+)/system_profile", InventoryHandler),
            (r"/api/insights/v1/rule/(.+)", InsightsRulesHandler),
            (r"/api/patch/v1/systems/(.+)/advisories", PatchAdvisoriesHandler),
            (r"/api/patch/v1/advisories/(.+)/systems", PatchSystemsHandler),
            (r"/api/patch/v1/views/systems/advisories", PatchViewsSystemsAdvHandler),
            (r"/v1/exploits", ExploitsHandler),
        ]
        Application.__init__(self, handlers)
        self.instance = IOLoop.instance()
        self.queue = mqueue.MQWriter(mqueue.EVENTS_TOPIC, bootstrap_servers="localhost:9092")
        self.archive_to_profile_cache = {}
        self.inventory_id_to_profile_cache = {}

    def start(self):
        """Start platform mock server."""
        self.instance.start()

    async def stop(self):
        """Stop platform mock server."""
        await self.queue.stop()
        self.instance.stop()

def main():
    """Main platform mock entrypoint."""
    init_logging()
    if not os.path.exists(STORAGE_PATH):
        os.makedirs(STORAGE_PATH)
    DatabasePool(1)
    LOGGER.info("Starting platform mock.")
    app = ServerApplication()
    app.listen(8000)

    def terminate(*_):
        """Trigger shutdown."""
        LOGGER.info("Signal received, stopping application.")
        IOLoop.instance().add_callback_from_signal(app.stop)

    signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT)
    for sig in signals:
        signal.signal(sig, terminate)

    app.start()
    LOGGER.info("Shutting down.")

if __name__ == '__main__':
    main()


import logging
import socket
import threading
import time
logger = logging.getLogger(__name__)
from . import onedrone as mavlink
#from pymavlink.dialects.v20 import ardupilotmega as mavlink2

class UDPListener(threading.Thread):
    def __init__(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        #self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1),
        # own_hostname = socket.gethostname()
        # own_ip = socket.gethostbyname(own_hostname)
        # own_ip = ''
        self.sock.setblocking(0)

        self.sock.bind(('',10777))

        self.rx_buf = bytearray()
        self.rx_lock = threading.Lock()
        self.bytes_rcvd = 0
        self.data_available = False

        self.max_buf_size = 0xffff
        threading.Thread.__init__(self)

    def __del__(self):
        pass

    def run(self):
        self.keep_running = True
        logger.info('UDP Listener | Running UDP Listener')
        while self.keep_running:
            data_received = self._receive()
            if not data_received:
                time.sleep(0.02)

        logger.info('UDP Listener |  Run function ended')

    def stop(self):
        logger.info('UDP Listener | Stopping UDP Listener')
        self.keep_running = False
        self.join()
        logger.info('UDP Listener | Stopped UDP Listener')

    def _receive(self):
        data = b''
        try:
            data, address = self.sock.recvfrom(4096)
        except BlockingIOError:
            pass
            # logger.info('Excepted BlockingIOError')
        if data:
            self.rx_lock.acquire()
            self.rx_buf +=bytearray(data)
            self.data_available = True
            self.rx_lock.release()
            return True
        else:
            return False


import colorlog
from colorlog import ColoredFormatter

handler = colorlog.StreamHandler()

formatter = ColoredFormatter(
    "%(log_color)s%(asctime)s (%(module)s:%(lineno)d) %(levelname)-8s%(reset)s %(message_log_color)s%(message)s ",
    datefmt="%y/%m/%d %H:%M:%S",
    reset=True,
    log_colors={
        'DEBUG': 'white',
        'INFO': 'white',
        'WARNING': 'white',
        'ERROR': 'white',
        'CRITICAL': 'black,bg_white',
    },
    secondary_log_colors={
        'message': {
            'DEBUG': 'blue',
            'INFO': 'cyan',
            'WARNING': 'yellow',
            'ERROR': 'red',
            'CRITICAL': 'red'
        }
    },
    style='%'
)
handler.setFormatter(formatter)

log = colorlog.getLogger('example')
log.addHandler(handler)
log.setLevel('DEBUG')


import requests
import json

def take_url(in_data: str) -> str:
    """
        Функция возвращающая URL для запроса JSON файла.
    """
    id = in_data.split('/')[-2]
    url = f'https://card.wb.ru/cards/detail?&nm={id}'
    return url

def search_json(url: str):
    """
        Функция запрашивает JSON файл и записывает его в файл.
    """
    headers = {'Accept': "*/*", 'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
    response = requests.get(url, headers=headers)
    # opener = AppURLopener()
    # response = opener.open(url)

    data = response.json()
    with open('wb_catalogs_data.json', 'w', encoding='UTF-8') as file:
        json.dump(data, file, indent=2, ensure_ascii=False)
        # print(f'Данные сохранены в wb_catalogs_data.json')

def take_data(url: str) -> (str, str, str, int, str):
    """
        Функция вытаскивающая из JSON файла необходимые нам данные.
        :return = брэнд, наименование, артикул, цена и URL для дальнейших запросов.
    """
    with open('wb_catalogs_data.json', 'r', encoding='UTF-8') as file:
        data_file = file.read()
        my_loads = json.loads(data_file)
        data_brand = my_loads["data"]['products'][0]["brand"]
        data_name = my_loads["data"]['products'][0]['name']
        data_id = my_loads["data"]['products'][0]['id']
        data_price = int(my_loads["data"]['products'][0]['salePriceU']) // 100
        data_url = url
        return data_brand, data_name, data_id, data_price, data_url

if __name__ == "main":
    take_url()
    search_json()
    take_data()
else:
    print(f'Импортируется {__name__}')


# testing files from murtaza
import cv2
import numpy as np

cap = cv2.VideoCapture(1)

while True:
    _, img = cap.read()
    print(img.shape)
    imgRes1 = cv2.resize(img, (500, 200))
    imgRes2 = cv2.resize(img, (0, 0), None, 0.5, 0.5)
    #print(imgRes2.shape)
    imgCrop = img[100:200, 200:300]

    cv2.imshow("Image1",imgRes1)
    cv2.imshow("Image2",imgRes2)
    cv2.imshow("Crop", imgCrop)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()


import os
import time
import json
import datetime
from slackclient import SlackClient
import pyowm
import nltk
from nltk import word_tokenize, pos_tag, ne_chunk

BOT_NAME = 'weatherbot'

SLACK_BOT_TOKEN = 'SLACK TOKEN'  # got off of slack page
owm = pyowm.OWM('Open Weather Map Token') # available after creating account at https://home.openweathermap.org

slack_client = SlackClient(SLACK_BOT_TOKEN)

api_call = slack_client.api_call("users.list")
if api_call.get('ok'):
    # retrieve all users so we can find our bot
    users = api_call.get('members')
    for user in users:
        if 'name' in user and user.get('name') == BOT_NAME:
            print("Bot ID for '" + user['name'] + "' is " + user.get('id'))
            BOT_ID = user.get('id')
    print('loop completed')
else:
    print("could not find bot user with the name " + BOT_NAME)

# starterbot's ID as an environment variable
#BOT_ID = os.environ.get("BOT_ID")

# constants
AT_BOT = "<@" + BOT_ID + ">"
EXAMPLE_COMMAND = "do"

# instantiate Slack & Twilio clients
slack_client = SlackClient(SLACK_BOT_TOKEN)

def parse_slack_output(slack_rtm_output):
    """
        The Slack Real Time Messaging API is an events firehose.
        this parsing function returns None unless a message is
        directed at the Bot, based on its ID.
    """
    output_list = slack_rtm_output
    if output_list and len(output_list) > 0:
        for output in output_list:
            if output and 'text' in output and AT_BOT in output['text']:
                print('>>>' + str(output))
                return output['text'].split(AT_BOT)[1].strip(), \
                       output['channel']
    return None, None

class StateMachine:

    def __init__(self):
        self.guess_history = []
        self.current_state = InitialState()
        self.READ_WEBSOCKET_DELAY = 1 # 1 second delay

    def run(self):
        if slack_client.rtm_connect():
            print("WeatherBot connected and running!")
            while True:
                command, channel = parse_slack_output(slack_client.rtm_read())
                if command and channel:
                    print('com:' + str(command))
                    print('chan:' + str(channel))
                    self.guess_history.append(command)
                    self.current_state = self.current_state.handle_command(command, channel)
                    if self.current_state == None:
                        print('Quitting')
                        break
                time.sleep(self.READ_WEBSOCKET_DELAY)
        else:
            print("Connection failed. Invalid Slack token or bot ID?")

class State:
    observation = ""
    city = ""
    resp_dict = ""
    def handle_command(self, command, channel):
        assert 0, 'handle command not implemented'

class InitialState(State):

    def __init__(self):
        print('creating InitalState')

    def handle_command(self, command, channel):
        print('InitialState receiving ' + command)

        for chunk in nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(command))):
            print(chunk)
            if hasattr(chunk, 'label'):
                State.city = ' '.join(c[0] for c in chunk.leaves()) # Find the city and return it

        try:
            State.observation = owm.weather_at_place(State.city) # observation is for the current weather only
            return basicWeather.handle_command(self, command, channel) # Go to next state with the weather observation
        except:
            response = ("Sorry, I can't find a city in this sentence: "
                        + command
                        + ". Please rephrase it!")
            slack_client.api_call('chat.postMessage', channel=channel, text=response, as_user=True)
            return InitialState() # Wasn't possible to get a city, return to initial state, offer second try

class basicWeather(State):
    def handle_command(self, command, channel):
        print('BasicWeatherState receiving ' + State.city)

        weather = State.observation.get_weather() # Get the current weather
        weather_beautify = weather.to_JSON()
        State.resp_dict = json.loads(weather_beautify) # Translate JSON object into python diction
        print(State.resp_dict)

        ###
        # Get fields and add description
        ###

        # Basic information
        reference_timestamp = State.resp_dict['reference_time']
        # Temperatures are returned in Kelvin
        status = "Overall status: " + State.resp_dict['status']
        temperature_min_k = float(State.resp_dict['temperature']['temp_min'])
        temperature_avg_k = float(State.resp_dict['temperature']['temp'])
        temperature_max_k = float(State.resp_dict['temperature']['temp_max'])

        # Temperatures in Celsius, rounded to 1/10
        temperature_min_c = round((temperature_min_k - 273.15),1)
        temperature_avg_c = round((temperature_avg_k - 273.15),1)
        temperature_max_c = round((temperature_max_k - 273.15),1)

        # Temperatures in Fahrenheit, rounded to 1/10
        temperature_min_f = round((temperature_min_k * (9/5) - 459.67),1)
        temperature_avg_f = round((temperature_avg_k * (9/5) - 459.67),1)
        temperature_max_f = round((temperature_max_k * (9/5) - 459.67),1)

        # Temperatures Final Output
        temperature_min_out = ("The minimum temperature is: "
                               + str(temperature_min_f)
                               + "F ("
                               + str(temperature_min_c)
                               + "C)")
        temperature_avg_out = ("The average temperature is: "
                               + str(temperature_avg_f)
                               + "F ("
                               + str(temperature_avg_c)
                               + "C)")
        temperature_max_out = ("The maximum temperature is: "
                               + str(temperature_max_f)
                               + "F ("
                               + str(temperature_max_c)
                               +"C)")

        # Turn reference timestamp into readable time
        reference_datetime = (datetime.datetime.fromtimestamp(int(reference_timestamp)))
        current_time = datetime.datetime.now()
        difference = new_time = current_time - reference_datetime
        time_diff_seconds = difference.seconds
        time_out = int(time_diff_seconds / 60)

        #Build response out of given information
        response = ("The current weather in "
                    + State.city
                    + " :\n"
                    + status
                    + "\n"
                    + temperature_min_out
                    + "\n"
                    + temperature_avg_out
                    + "\n"
                    + temperature_max_out
                    + "\n"
                    + "The last time we checked the weather here was "
                    + str(time_out)
                    + " minutes ago. \n"
                    + "Do you want more detailed information? Type @weatherbot yes for more, or @weatherbot no to return!")

        slack_client.api_call('chat.postMessage', channel=channel, text=response, as_user=True) # Respond with current weather
        return followUp() # Return followUp state, give choice of more info or return to InitialState

class advancedWeather(State):
    def handle_command(self, command, channel):
        print('advancedWeather receiving ' + State.city)

        ###
        # Get fields and add description
        ###

        # Advanced Information
        sunset_timestamp = State.resp_dict['sunset_time']
        sunrise_timestamp = State.resp_dict['sunrise_time']
        reference_timestamp = State.resp_dict['reference_time']
        visibility = State.resp_dict['visibility_distance']
        status_detail = State.resp_dict['detailed_status']
        humidity = State.resp_dict['humidity']
        wind_degree = State.resp_dict['wind']['deg']
        wind_speed = State.resp_dict['wind']['speed']
        pressure = State.resp_dict['pressure']['press']

        # Turn timestamps into readable time
        sunset_datetime = (" sets tonight at: "
                          + datetime.datetime.fromtimestamp(int(sunset_timestamp)).strftime('%H:%M:%S'))
        sunrise_datetime = ("Sun rises today at: "
                           + datetime.datetime.fromtimestamp(int(sunrise_timestamp)).strftime('%H:%M:%S'))
        reference_datetime = (datetime.datetime.fromtimestamp(int(reference_timestamp)))
        current_time = datetime.datetime.now()
        difference = new_time = current_time - reference_datetime
        time_diff_seconds = difference.seconds
        time_out = int(time_diff_seconds / 60)

        # Convert visibility distance to km and mi
        visibility_km = round((visibility / 1000),1)
        visibility_mi = round((visibility * 0.00062137),1)
        visibility_out = ("The current visibility distance is: "
                         + str(visibility_mi)
                         + " miles ("
                         + str(visibility_km)
                         + " km)")
        # Build answer
        response = ("The sun in "
                    + State.city
                    + sunset_datetime
                    + "\n"
                    + sunrise_datetime
                    + "\n"
                    + visibility_out
                    + ". \n"
                    + "The humidity is: "
                    + str(humidity)
                    + "%. \n"
                    + "The wind blows with a speed of: "
                    + str(wind_speed)
                    + " m/s. \n"
                    + "Currently the air pressure is: "
                    + str(pressure)
                    + " hPa. \n"
                    + "Overall: "
                    + status_detail
                    + ". \n"
                    + "The last time we checked was "
                    + str(time_out)
                    + " minutes ago. \n"
                    + "If you want weather information for another city, you can ask me right away!")

        slack_client.api_call('chat.postMessage', channel=channel, text=response, as_user=True)

        State.city = ""
        State.obswervation = ""
        State.resp_dict = ""

        return InitialState() # All done, go back to InitialState and wait for new input

class followUp(State):
    def handle_command(self, command, channel):
        print('FollowUpState receiving ' + command)
        print(State.observation)

        answerYes = ['Yes', 'yes', 'yep', 'yup', 'yea', 'y', 'Yep']
        answerNo = ['No', 'no', 'nope', 'naw', 'Nope', 'n']
        if command in answerYes:
            return advancedWeather()
        if command in answerNo:
            response_no = 'Okay! Do you want to check the weather somewhere else?'
            slack_client.api_call('chat.postMessage', channel=channel, text=response_no, as_user=True)
            return InitialState()
        if (command not in answerYes or command not in answerNo):
            response_unknown = 'Sorry, I did not understand that answer. Can you rephrase that?'
            slack_client.api_call('chat.postMessage', channel=channel, text=response_unknown, as_user=True)
            return followUp()

if __name__ == "__main__":
    weather = StateMachine()
    weather.run()


from libcommon.octavo_api_client import (
    OctavoEccoClient
    )
from libcommon.metadata_readers import (
    load_good_metadata
    )
# from nltk.tag.stanford import StanfordPOSTagger
from nltk.tokenize import word_tokenize
# from nltk.stem.porter import PorterStemmer
# from nltk.stem import LancasterStemmer
from nltk.stem import SnowballStemmer
import random
# import sys
import csv
import re
from collections import OrderedDict

def get_doctext_tokens(doctext):
    text_tokens = word_tokenize(doctext)
    return text_tokens

def get_stemmed_tokens(doctext_tokens, stemmer):
    stemmed_tokens = []
    for token in doctext_tokens:
        stemmed_tokens.append(stemmer.stem(token))
    return stemmed_tokens

def get_stemmed_token(token, stemmer):
    stemmed_token = (stemmer.stem(token))
    return stemmed_token

def get_stemmed_baseline(wordfile):
    with open(wordfile) as word_file:
        english_words = set(word.strip().lower() for word in word_file)
    stemmed_baseline = set()
    for word in english_words:
        stemmed_baseline.add(stemmer.stem(word))
    return stemmed_baseline

def get_stemmed_baseline2(wordfile):
    with open(wordfile) as word_file:
        english_words = (
            set(word.split("\t")[0].strip().lower() for word in word_file))
    stemmed_baseline = set()
    for word in english_words:
        stemmed_baseline.add(stemmer.stem(word))
    return stemmed_baseline

def evaluate_text_ocr(doctext, baseline_set, stemmer, min_length=3):
    retdict = {'recognised': [], 'unrecognised': []}
    tokens = get_doctext_tokens(doctext)
    for i in range(0, len(tokens)):
        token = tokens[i]
        if len(token) < min_length:
            continue
        if re.search('[a-zA-Z]', token) is None:
            continue
        stemmed_token = get_stemmed_token(token, stemmer)
        if stemmed_token in baseline_set:
            retdict['recognised'].append(token)
        else:
            retdict['unrecognised'].append(token)
    return retdict

def get_metadata_yearsubset(good_metadata, year):
    results_metadata = {}
    for key, value in good_metadata.items():
        if value.get('estc_publication_year') == str(year):
            results_metadata[key] = value
    return results_metadata

def get_metadata_langsubset(good_metadata, lang):
    results_metadata = {}
    for key, value in good_metadata.items():
        if value.get('estc_language') == lang:
            results_metadata[key] = value
    return results_metadata

def get_metadata_sample(metadata, sample_size):
    metadata_list = list(metadata.values())
    if len(metadata_list) < sample_size:
        sample_size = len(metadata)
    random_sample = random.sample(metadata_list, sample_size)
    return random_sample

def write_wordset(words, outputfile):
    output = set(words)
    outdict = {}
    for word in output:
        outdict[word] = words.count(word)
    sorteddict = OrderedDict(
        sorted(outdict.items(), key=lambda t: t[1], reverse=True))
    with open(outputfile, 'w') as outfile:
        csvwriter = csv.writer(outfile)
        for key, value in sorteddict.items():
            csvwriter.writerow([key, value])
    # with open(outputfile, 'w') as outfile:
    #     for word in output:
    #         outfile.write(word + "\n")

def get_lensums(wordlist):
    words = wordlist
    lens = []
    for word in words:
        lens.append(len(word))
    max_len = max(lens)
    lensums = {}
    for word_length in range(2, max_len + 1):
        lensums[word_length] = lens.count(word_length)
    return lensums

def get_yearly_results(good_metadata, year, sample_size, eccoclient):
    year_metadata_subset = get_metadata_yearsubset(
        good_metadata, year)
    year_metadata_subset = get_metadata_langsubset(
        year_metadata_subset, "English")
    year_metadata_sample = get_metadata_sample(
        year_metadata_subset, sample_size)

    yearly_sample_eccoids = []
    for item in year_metadata_sample:
        yearly_sample_eccoids.append(item.get('ecco_id'))

    print("Fetching n eccoids: " + str(len(yearly_sample_eccoids)))

    yearly_results_by_doc = []
    for docid in yearly_sample_eccoids:
        doctext = eccoclient.get_text_for_document_id(docid).get('text')
        text_evaluated = evaluate_text_ocr(doctext, stemmed_baseline, stemmer)
        good_words = text_evaluated.get('recognised')
        bad_words = text_evaluated.get('unrecognised')
        ratio = len(good_words) / (len(good_words) + len(bad_words))
        yearly_results_by_doc.append({'rec_words': good_words,
                                      'unk_words': bad_words,
                                      'good_ratio': ratio})

    rec_words = []
    unk_words = []
    print("Combining yearly results.")
    for yearly_result in yearly_results_by_doc:
        rec_words.extend(yearly_result.get('rec_words'))
        unk_words.extend(yearly_result.get('unk_words'))
    ratio = len(rec_words) / (len(rec_words) + len(unk_words))

    rec_words_lensum = get_lensums(rec_words)
    unk_words_lensum = get_lensums(unk_words)

    yearly_results_all = {'rec_words': rec_words,
                          'unk_words': unk_words,
                          'ratio': ratio,
                          'rec_sums': rec_words_lensum,
                          'unk_sums': unk_words_lensum}
    return yearly_results_all

def write_yeartable_header(yeartable_csvfile):
    with open(yeartable_csvfile, 'w') as csvfile:
        csvwriter = csv.writer(csvfile)
        header_row = ['year', 'ratio', 'rec', 'unk', ]
        rec_len_list = []
        unk_len_list = []
        for i in range(2, 51):
            rec_len_list.append("rec-" + str(i))
            unk_len_list.append("unk-" + str(i))
        header_row.extend(rec_len_list)
        header_row.extend(unk_len_list)
        csvwriter.writerow(header_row)

# script start

eccoclient = OctavoEccoClient()
# stemmer = PorterStemmer()
# stemmer = LancasterStemmer()
stemmer = SnowballStemmer('english')
# wordfile = "data/wordsEn.txt"
# wordfile = "data/words.txt"
wordfile = "data/count_1w.txt"
# http://norvig.com/ngrams/
stemmed_baseline = get_stemmed_baseline2(wordfile)
good_metadata = load_good_metadata(
    "../data-own-common/good_metadata.json")
yeartable_csvfile = "output/allyears_newest.csv"
start_new = True
yearly_sample_size = 1000000
years_to_evaluate = (list(range(1700, 1799)))

if start_new:
    write_yeartable_header(yeartable_csvfile)

for year in years_to_evaluate:
    print("processing year: " + str(year))
    yearly_results = get_yearly_results(
        good_metadata, year, yearly_sample_size, eccoclient)

    with open(yeartable_csvfile, 'a') as csvfile:
        csvwriter = csv.writer(csvfile)
        outrow = [year,
                  yearly_results.get('ratio'),
                  len(yearly_results.get('rec_words')),
                  len(yearly_results.get('unk_words')),
                  ]
        rec_len_list = []
        unk_len_list = []
        for i in range(2, 51):
            rec_len = yearly_results.get('rec_sums').get(i)
            if rec_len is None:
                rec_len = 0
            rec_len_list.append(rec_len)
            unk_len = yearly_results.get('unk_sums').get(i)
            if unk_len is None:
                unk_len = 0
            unk_len_list.append(unk_len)
        outrow.extend(rec_len_list)
        outrow.extend(unk_len_list)
        csvwriter.writerow(outrow)

    print("finished with year: " + str(year))
    # print("Writing word totals for year " + str(year))
    outfile = "output/" + str(year)
    # write_wordset(yearly_results['rec_words'], (outfile + "-rec.txt"))
    # write_wordset(yearly_results['unk_words'], (outfile + "-unk.txt"))

# rec_words_outfile = "output/recset.txt"
# write_wordset(yearly_results.get('rec_words'), rec_words_outfile)
# unk_words_outfile = "output/unkset.txt"
# write_wordset(yearly_results.get('unk_words'), unk_words_outfile)

# docid = "0145100106"
# doctext = eccoclient.get_text_for_document_id(docid).get('text')

# print("good: " + str(len(good_words)))
# print("bad:  " + str(len(bad_words)))


""" Module to parse DocumentRevisions LibraryStatus """
import codecs
import logging
import os
import plistlib
from riplib.plugin import Plugin

__author__ = 'osxripper'
__version__ = '0.1'
__license__ = 'GPLv3'

class LibraryStatus(Plugin):
    """
    Plugin to parse /.DocumentRevisions-V100/LibraryStatus
    """
    def __init__(self):
        """
        Initialise the class.
        """
        super().__init__()
        self.set_name("Document Revisions Library Status")
        self.set_description("Parse data from /.DocumentRevisions-V100/LibraryStatus")
        self.set_data_file("LibraryStatus")
        self.set_output_file("DocumentRevisions.txt")
        self.set_type("plist")

    def parse(self):
        """
        Parse /.DocumentRevisions-V100/LibraryStatus
        """
        with codecs.open(os.path.join(self._output_dir, self._output_file), "a", encoding="utf-8") as output_file:
            output_file.write("="*10 + " " + self._name + " " + "="*10 + "\r\n")
            if self._os_version in ["big_sur", "catalina"]:
                logging.info("This version of OSX is not supported by this plugin.")
                print("[INFO] This version of OSX is not supported by this plugin.")
                output_file.write("[INFO] This version of OSX is not supported by this plugin.\r\n")
            elif self._os_version in ["mojave", "high_sierra", "sierra", "el_capitan", "yosemite"]:
                plist_file = os.path.join(self._input_dir, ".DocumentRevisions-V100", self._data_file)
                output_file.write("Source File: {0}\r\n\r\n".format(plist_file))
                if os.path.isfile(plist_file):
                    with open(plist_file, "rb") as plist_to_load:
                        plist = plistlib.load(plist_to_load)
                    try:
                        if "databaseStateIsTrustable" in plist:
                            output_file.write("Database State Is Trustable: {0}\r\n".format(plist["databaseStateIsTrustable"]))
                    except KeyError:
                        pass
                else:
                    logging.warning("File: %s does not exist or cannot be found.\r\n", plist_file)
                    output_file.write("[WARNING] File: {0} does not exist or cannot be found.\r\n".format(plist_file))
                    print("[WARNING] File: {0} does not exist or cannot be found.".format(plist_file))
            elif self._os_version in ["mavericks", "mountain_lion", "lion", "snow_leopard"]:
                logging.info("This version of OSX is not supported by this plugin.")
                print("[INFO] This version of OSX is not supported by this plugin.")
                output_file.write("[INFO] This version of OSX is not supported by this plugin.\r\n")
            else:
                logging.warning("Not a known OSX version.")
                print("[WARNING] Not a known OSX version.")
            output_file.write("="*40 + "\r\n\r\n")
        output_file.close()


# Program to digest make depend files
import os
import re
import sys
from typing import Dict, List, Set
from collections import defaultdict

DirectoryName = str
FileName = str

def usage():
    print(""" Usage:
    python factoroptions.py input-options-filename
    """)
    sys.exit(1)

def get_includes(line: str) -> Set[str]:
    """ Return the set of Include options from the -options-for-sources line. """
    parts = line.split(";")
    # parts[0] has the -options-for-sources and the source file.
    includes = {part for part in parts
                if (part.startswith("-I ") and not part.startswith("-I /usr"))}
    return includes

def get_usr_includes(line: str) -> Set[str]:
    """ Return the set of Include options from the -options-for-sources line. """
    parts = line.split(";")
    # parts[0] has the -options-for-sources and the source file.
    includes = {part for part in parts if part.startswith("-I /usr")}
    return includes

def get_non_includes(line: str) -> Set[str]:
    """ Return the set of non-Include options from the -options-for-sources line. """
    parts = line.split(";")
    # parts[0] has the -options-for-sources and the source file.
    others = {part for part in parts[1:] if not part.startswith("-I ")}
    return others

class FactorOptions:
    """ Digest .depend files"""

    def __init__(self, input_filename):
        self.input_filename = input_filename
        self.output_filename = input_filename + ".factored"
        self.includes_filename = input_filename + ".includes"
        self.defines_filename = input_filename + ".defines"
        self.lines = self.read_input
        # Collect the -options-for-sources lines.
        self.ofs_lines = [line for line in self.lines if line.startswith("-options-for-sources ")]
        common_includes = get_includes(self.ofs_lines[0])
        common_usr_includes = get_usr_includes(self.ofs_lines[0])
        common_other = get_non_includes(self.ofs_lines[0])
        print(f"len(common_includes) = {len(common_includes)}")
        print(f"len(common_usr_includes) = {len(common_usr_includes)}")
        print(f"len(common_other) = {len(common_other)}")
        for i in range(1, len(self.ofs_lines)):
            this_includes = get_includes(self.ofs_lines[i])
            common_includes = common_includes.union(this_includes)
            print(f"len(common_includes) = {len(common_includes)}")
            this_usr_includes = get_usr_includes(self.ofs_lines[i])
            common_usr_includes = common_usr_includes.union(this_usr_includes)
            print(f"len(common_usr_includes) = {len(common_usr_includes)}")
            this_other = get_non_includes((self.ofs_lines[i]))
            common_other = common_other.union(this_other)
            print(f"len(common_other) = {len(common_other)}")

        self.common_includes = common_includes
        self.common_usr_includes = common_usr_includes
        self.common_other = common_other
        self.factored = self.compute_output()
        pass

    @property
    def read_input(self) -> List[str]:
        with open(self.input_filename, 'r') as f:
            lines = [line.strip("\\\n") for line in f]
            return lines

    def is_common(self, arg):
        if arg in self.common_includes:
            return True
        if arg in self.common_usr_includes:
            return True
        return arg in self.common_other

    def compute_new_ofs(self, ofs_index) -> str:
        line = self.ofs_lines[ofs_index]
        parts = line.split(";")
        args = []
        for i in range(1, len(parts)):
            arg = parts[i]
            if not self.is_common(arg):
                args.append(arg)
        if args:
            argstring = ';'.join(args)
            result = ':'.join([parts[0], argstring])
            return result
        return ''

    def get_common_usr_include_list(self) -> List[str]:
        # Preserve the order from the first.
        line = self.ofs_lines[0]
        parts = line.split(";")
        result = []
        for i in range(1, len(parts)):
            part = parts[i]
            if part in self.common_usr_includes:
                if os.path.isdir(part[3:]):
                    result.append(part)
                else:
                    print(part + " is not a directory")
        return result

    def compute_output(self) -> List[str]:
        factored_lines = []
        ofs_index = 0
        for line in self.lines:
            if line.startswith("-options-for-sources "):
                factored = self.compute_new_ofs(ofs_index)
                if factored:
                    factored_lines.append(factored)
                ofs_index += 1
                pass
            else:
                if ofs_index == len(self.ofs_lines):
                    # We have processed the last ofs line. Output common.
                    factored_lines += ["# Start of factored options"]
                    # First the non-/usr includes.
                    sorted_includes = [include for include in self.common_includes
                                       if os.path.isdir(include[3:])]
                    sorted_includes.sort()
                    factored_lines += sorted_includes
                    # Next the /usr includes.
                    factored_lines += self.get_common_usr_include_list()
                    # Now the others. Let's sort them too.
                    sorted_others = [other for other in self.common_other]
                    sorted_others.sort()
                    factored_lines += sorted_others
                    # increment ofs_index so we don't do this again.
                    ofs_index += 1
                    factored_lines += ["# End of factored options"]

                # One of the original lines.
                factored_lines.append(line)
                pass
        return factored_lines

    def output_factored(self):
        with open(self.output_filename, "w") as w:
            for line in self.factored:
                w.write(line + '\n')
        pass

    def output_defines(self):
        with open(self.defines_filename, "w") as w:
            for line in self.common_other:
                w.write(line + '\n')
        pass

    def output_includes(self):
        with open(self.includes_filename, "w") as w:
            for line in self.common_usr_includes:
                w.write(line + '\n')
            for line in self.common_includes:
                w.write(line + '\n')
        pass

if __name__ == u'__main__':
    print('Factoring PolySpace Code Prover options\n')
    options = sys.argv
    if len(sys.argv) != 2:
        usage()

    factor = FactorOptions(options[1])
    factor.output_factored()
    factor.output_defines()
    factor.output_includes()
    print('\ndone.\n')


from argparse import ArgumentParser

import torch
import skimage.exposure

import data
from model import JointRegressor

if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('--model', type=str)
    parser.add_argument('--data', type=str)
    parser.add_argument('--atlas', type=str)

    args = parser.parse_args()

    dataset = data.TiffTestImages(args.data)
    atlas = data.CCFv3(args.atlas, dorsoventral_rotation=(-1, 1), mediolateral_rotation=(-1, 1))
    model = JointRegressor.load_from_checkpoint(args.model)

    with torch.no_grad():
        predictions = []
        for image, label in dataset:
            fixed = atlas[label * 10][0]
            image = torch.from_numpy(
                skimage.exposure.equalize_adapthist(
                    image.numpy(), nbins=128, clip_limit=0.05
                )
            )
            prediction = model((fixed.unsqueeze(0), image.unsqueeze(0)))
            # prediction = model(torch.stack((fixed, image.expand(3, *image.shape))).unsqueeze(0))
            predictions.append(([p[0] for p in prediction], label))

    # for (z, d, m), label in predictions:
    #     print(round(z / 10), round(abs(z / 10 - label)), round(abs(d), 1), round(abs(m), 1), label)
    for (x, y, r), _ in predictions:
        print(x, y, r)

        print()


#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Author : Trsdy

https://modenc.renegadeprojects.com/CSF_File_Format
'''

from io import TextIOWrapper
import struct
import json
import sys
import argparse

def make_header(csf: TextIOWrapper, num_labels: int, num_strings: int, locale=0):
    header_info = {
        "FSC": b" FSC",
        "CSF Version": 3,
        "NumLabels": num_labels,
        "NumStrings": num_strings,
        "unused": 0,
        "Language": locale
    }
    csf.write(struct.pack("<4sLLLLL"
            , header_info["FSC"]
            , header_info["CSF Version"]
            , header_info["NumLabels"]
            , header_info["NumStrings"]
            , header_info["unused"]
            , header_info["Language"]
            ))
    return csf

def string2bytes(content: str) -> bytes:
    raw_bytes = bytearray(content.encode("UTF-16LE"))
    for idx in range(len(raw_bytes)-1, -1, -1):
        raw_bytes[idx] = raw_bytes[idx] ^ 0xff
    return bytes(raw_bytes)

def main_coversion(csf_file: TextIOWrapper, name_str_map: dict):
    csf_file = make_header(csf_file, len(name_str_map), len(name_str_map))
    for label, ui_content in name_str_map.items():
        csf_file.write(struct.pack("<4sL", b" LBL", 1))
        uiname_bytes = label.encode('utf-8')
        csf_file.write(struct.pack(f"<L{len(uiname_bytes)}s4s", len(
            uiname_bytes), uiname_bytes, b' RTS'))
        value = string2bytes(ui_content)
        value_len = len(value)//2
        csf_file.write(struct.pack("<L", value_len))
        csf_file.write(struct.pack(f"<{value_len*2}s", value))

def parse_filenames():
    parser = argparse.ArgumentParser(
        description="Convert a JSON file to a CSF file")
    parser.add_argument('-i', '--json', type=str, required=True, help="input JSON file")
    parser.add_argument('-o', '--csf', type=str, required=True, help="output CSF file")
    args = parser.parse_args()
    return args.json, args.csf

if __name__ == "__main__":
    jsn_filename, csf_filename = parse_filenames()
    with open(jsn_filename, mode="r", encoding="GB2312") as jsn_file:
        name_str_dict = json.load(jsn_file)

        with open(csf_filename, mode="wb") as CSF_File:
            main_coversion(CSF_File, name_str_dict)


from __future__ import annotations

from unittest import mock
from unittest.mock import MagicMock

import pytest

from airflow.providers.amazon.aws.hooks.lambda_function import LambdaHook

FUNCTION_NAME = "test_function"
PAYLOAD = '{"hello": "airflow"}'
BYTES_PAYLOAD = b'{"hello": "airflow"}'
RUNTIME = "python3.9"
ROLE = "role"
HANDLER = "handler"
CODE = {}

class LambdaHookForTests(LambdaHook):
    conn = MagicMock(name="conn")

@pytest.fixture
def hook():
    return LambdaHookForTests()

class TestLambdaHook:
    def test_get_conn_returns_a_boto3_connection(self, hook):
        assert hook.conn is not None

    @mock.patch(
        "airflow.providers.amazon.aws.hooks.lambda_function.LambdaHook.conn", new_callable=mock.PropertyMock
    )
    @pytest.mark.parametrize(
        "payload, invoke_payload",
        [(PAYLOAD, BYTES_PAYLOAD), (BYTES_PAYLOAD, BYTES_PAYLOAD)],
    )
    def test_invoke_lambda(self, mock_conn, payload, invoke_payload):
        hook = LambdaHook()
        hook.invoke_lambda(function_name=FUNCTION_NAME, payload=payload)

        mock_conn().invoke.assert_called_once_with(
            FunctionName=FUNCTION_NAME,
            Payload=invoke_payload,
        )

    @pytest.mark.parametrize(
        "hook_params, boto3_params",
        [
            pytest.param(
                {
                    "function_name": FUNCTION_NAME,
                    "runtime": RUNTIME,
                    "role": ROLE,
                    "handler": HANDLER,
                    "code": CODE,
                    "package_type": "Zip",
                },
                {
                    "FunctionName": FUNCTION_NAME,
                    "Runtime": RUNTIME,
                    "Role": ROLE,
                    "Handler": HANDLER,
                    "Code": CODE,
                    "PackageType": "Zip",
                },
                id="'Zip' as 'package_type'",
            ),
            pytest.param(
                {
                    "function_name": FUNCTION_NAME,
                    "role": ROLE,
                    "code": CODE,
                    "package_type": "Image",
                },
                {
                    "FunctionName": FUNCTION_NAME,
                    "Role": ROLE,
                    "Code": CODE,
                    "PackageType": "Image",
                },
                id="'Image' as 'package_type'",
            ),
        ],
    )
    def test_create_lambda(self, hook_params, boto3_params, hook):
        hook.conn.create_function.reset_mock()
        hook.conn.create_function.return_value = {}
        hook.create_lambda(**hook_params)

        hook.conn.create_function.assert_called_once_with(**boto3_params)

    @pytest.mark.parametrize(
        "params",
        [
            pytest.param(
                {
                    "handler": HANDLER,
                },
                id="'runtime' not provided",
            ),
            pytest.param(
                {
                    "runtime": RUNTIME,
                },
                id="'handler' not provided",
            ),
        ],
    )
    def test_create_lambda_with_zip_package_type_and_missing_args(self, params, hook):
        hook.conn.create_function.return_value = {}

        with pytest.raises(TypeError):
            hook.create_lambda(
                function_name=FUNCTION_NAME,
                role=ROLE,
                code=CODE,
                package_type="Zip",
                **params,
            )


from django.http import HttpResponseRedirect
from django.http import JsonResponse
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
import newgen.models as mo
from django.conf import settings
from django.contrib import messages
from django.urls import reverse
from django.forms.models import model_to_dict
from django.contrib import admin

#from top.objects import *
from newgen.tools import *
from newgen.functions import *
from newgen.background_tasks import *
#from top.open_func import *
from django.contrib.auth.models import Group

from django.template.loader import render_to_string
from django.db.models import Q

from cryptography.fernet import Fernet
cipher_key = Fernet.generate_key()
cipher = Fernet(cipher_key)

@login_required
def mainworker(request):

	if request.user.is_superuser:
		return HttpResponseRedirect(reverse('admin:index'))
	if str(request.user) == 'tools':
		return HttpResponseRedirect(reverse('tools'))
	if request.user.groups.first().id == 3:
		return HttpResponseRedirect(reverse('it'))

	user = request.user
	objects = []
	orig_notes =[]
	shedule_list = []

	#print(request.user.groups.first().id)
	#1 - ГРУППА БРИГАДИРОВ

	workers = engineers = GetGroupMembers("Монтажники")
	worker = GetWorker(request)
	history = GetWorkersResults(worker.worker_id)
	#print(request)
	#TOOL NOTE
	for tool_note in mo.ToolsReport.objects.filter(~Q(worker_id = worker.worker_id)):
		orig_notes.append(tool_note)

	#OBJECTS
	for objectx in mo.Object.objects.filter(is_active = True):
		objects.append(objectx.name)

	#SHEDULES
	shedules = mo.Shedule.objects.filter(date__gte = datetime.date.today())

	if shedules.exists():
		for shedule in shedules:
			if str(shedule.leader) == str(worker.fullname):
				shedule_list.append(shedule)
				continue
			if worker in shedule.workers.all():
				shedule_list.append(shedule)

	return render(request,"mainworker.html",{'objects': objects,'history':history, 'worker':worker,
		'user': user, 'workers': workers, 'orig_notes':orig_notes,
	  'shedule':shedule_list})

def notice_work(request):
	workers=[]

	#3 fields + token + object
	workers_to_notice = round((len(dict(request.POST.lists()))-2)/3)

	object_name = GetObject(request,"object")

	for i in range(workers_to_notice):
		worker_short_name = GetObject(request,"worker_" + str(i+1))
		time = GetObject(request,"time_" + str(i+1))
		comment = GetObject(request,"comment_" + str(i+1))
		worker = GetWorkerByName(worker_short_name)
		obj = GetFirstFilteredNote(mo.Object,"name",object_name)

		AddWorkNote(worker, obj, time, comment)
		AddFullReport(worker.fullname, obj.name, time)

	#bg_task()
	messages.success(request, "Всё записано, спасибо")
	return HttpResponseRedirect("../")

def tool_transfers(request):

	user = GetFirstFilteredNote (User, "username",request.user)
	workername = Worker.objects.filter(user = user.id).first().fullname

	try:
		temp_transfers_id = request.POST.getlist(workername)
		for note_id in temp_transfers_id:
			temp_note = GetFirstFilteredNote(mo.TempToolTransfers,"note_id",note_id)
			if request.POST.get(note_id) == "+":
				try:
					RefreshToolReportNote(temp_note.tool_name,temp_note.worker_id)
					AddToolForward(temp_note.tool_name,temp_note.worker_id,"")
				except Exception as e:
					print(e)
			temp_note.delete()
	except Exception as e:
		messages.warning(request,"Гавнина с обновой записей")
		return HttpResponseRedirect("../")
	messages.success(request, 'Данные по оборудованию обновлены')
	return HttpResponseRedirect("../")

def edit_tool(request):
	tool = GetTool(request.POST.get("tool"))
	if tool is None:
		messages.warning(request, "Такого инструмента нет")
		return HttpResponseRedirect("../")
	new_tool_name = request.POST.get("new_tool_name")
	try:
		tool.name = request.POST.get("new_tool_name")
		tool.save()
		messages.success(request, "Название изменено")
	except Exception as e:
		print(e)
		messages.warning(request,e)
		return HttpResponseRedirect("../")
	return HttpResponseRedirect("../")

def delete_tool(request):
	if DeleteTool(request.POST.get("tool")):
		messages.success(request, "Инструмент удалён из БД")
	else:
		messages.warning(request, "Что-то не то")
	return HttpResponseRedirect("../")

def tools(request):
	worker = GetWorker(request)
	tools =[]
	workers = []
	tools_notes = []
	transfers = []
	temp_transfers = []
	orig_notes =[]
	#TOOLS
	for tool in mo.Tools.objects.all():
		if  not mo.TempToolTransfers.objects.filter(tool_id = tool.tool_id).exists():
			tools.append(tool)

	#WORKER
	for another_worker in mo.Worker.objects.all():
		workers.append(another_worker)

	#TOOL NOTE
	for tool_note in mo.ToolsReport.objects.all():
		if str(tool_note.worker_id) != str(worker.fullname):
			orig_notes.append(tool_note)
		tools_notes.append(tool_note)
	#TRANSFERS
	for transfer in mo.ToolsForwarding.objects.order_by('-note_id'):
		transfers.append(transfer)

	#TEMP TRANSFERS
	for temp_transfer in mo.TempToolTransfers.objects.all():
		temp_transfers.append(temp_transfer)

	return render(request,"tools/tools.html",{'tools_notes': tools_notes, 'tools': tools, 'workers': workers,
	 'transfers':transfers, 'temp_transfers':temp_transfers, 'orig_notes':orig_notes})

def add_tool(request):
	if 'submit' in request.POST:
		print(111)
	if AddTool(request.POST.get("tool_name")):
		AddToolReportNote(request.POST.get("tool_name"))
		messages.success(request, "Новый инструмент добавлен")
	else:
		messages.warning(request,"Инструмент с таким названием уже есть")
	return HttpResponseRedirect("../")

def tool_forwarding(request):
	try:
		user = User.objects.filter(username = request.user).first()
		worker = Worker.objects.filter(user = user.id).first()
		tool = Tools.objects.filter(name = request.POST.get("tool")).first()
		comments = request.POST.get("comments")
		AddToolForward(tool, worker, comments)
		RefreshToolReportNote(tool, worker)
		messages.success(request, "Вы взяли инструмент")
	except Exception as e:
		print(e)
		messages.warning(request, "Инструмент не был оформлен на Вас")
	if str(request.user) == "tools":
		AddTempToolTransfer(tool, worker, comments)
	return HttpResponseRedirect("../")

def delete_temp_note(request):
	user = User.objects.filter(username = request.user).first()
	tool = mo.Tools.objects.filter(name = request.POST.get("tool_name")).first()
	worker = mo.Worker.objects.filter(fullname = request.POST.get("worker_name")).first()
	print(request)
	try:
		mo.ToolsForwarding.objects.filter(worker_id = worker.worker_id, tool_name = tool.tool_id).first().delete()
		mo.TempToolTransfers.objects.filter(worker_id = worker.worker_id, tool_id = tool.tool_id).first().delete()
		messages.success(request,"Передача отменена")
	except Exception as e:
		messages.error(request,str(e) +" temp")

	#ВЕРНУТЬ ПРОШЛОГО ХОЗЯИНА
	old_transfer = mo.ToolsForwarding.objects.filter(tool_name = tool.tool_id).first()
	old_worker = mo.Worker.objects.filter(fullname = old_transfer.worker_id).first()
	RefreshToolReportNote(tool,old_worker)
	return HttpResponseRedirect("../")

####################################################################################################################
def organisations_requests(request):
	if not request.session or not request.session.session_key:
		request.session.save()
	statuses = []
	for status in mo.UrgencyStatus.objects.all().order_by('-status_id'):
		statuses.append(status.status)
	return render(request,"organisations_requests.html",{'statuses':statuses})

def new_request(request):
	AddNewRequestNote(request)
	return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

def confirm_email(request):
	code = GetObject(request,"code")
	verification_obj =  GetFirstFilteredNote(EmailVerification,"activation_code",code)

	if verification_obj == None:
		messages.error(request, "Недействительный ключ активации")
	else:
		if datetime.date.today() > verification_obj.expiry_date:
			messages.error(request, "Данный ключ больше не активен")
		else:

			#ACTIVATE REQUEST
			related_request = mo.OpenRequest.objects.filter(note_id = verification_obj.open_request.note_id).first()
			related_request.status =  GetFirstFilteredNote(RequestStatus,"status_id",1)
			related_request.save()

			#ACTIVATE EMAIL
			related_email =GetFirstFilteredNote(mo.Email,"email",verification_obj.email)
			related_email.is_active = True
			related_email.save()

			#DELETE CODE
			GetFirstFilteredNote(EmailVerification,"activation_code",code).delete()
			messages.success(request, "Ваш Email подтверждён")

	return render(request,"inserting_auth_code.html")

def inserting_auth_code(request):
	return render(request,"inserting_auth_code.html")

def it(request):

	engineers = GetGroupMembers("Инженеры")
	active_requests = []
	accepted_requests = []

	for closed_request in ClosedRequest.objects.filter(is_accepted = True):
		accepted_requests.append(closed_request)

	for active_request in mo.OpenRequest.objects.filter(status = 1):
		if not len(ClosedRequest.objects.filter(open_request = active_request.note_id)):
			active_requests.append(active_request)

	return render(request,"it/it.html",{'active_requests':active_requests, 'engineers':engineers,'accepted_requests':accepted_requests})

def add_temp_close_request(request):
	open_request_id = GetObject(request,"request_id")
	photos = GetPhotos(request)
	responsible_list = request.POST.getlist("responsible")
	comment = GetObject(request,"comment")
	close_request_id = CloseRequest(open_request_id,responsible_list,photos,comment)
	messages.success(request, "Всё отправлено, спасибо")
	#encode id -> str -> bytes -> str -> remove "==" -> send email
	encrypted_text = cipher.encrypt(f'{close_request_id}'.encode('utf-8'))
	request_is_closed(close_request_id,encrypted_text.decode('utf-8').split("==")[0])

	return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

def job_evaluation(request, encrypted_text):
	#get decrypted request id from request url^ ****'/****/content' and add '=='
	text = str(request.build_absolute_uri).split("'")[1].split("/")[2]
	text = text +"=="
	try:
		decoded_id = cipher.decrypt(text.encode('utf-8')).decode('utf-8')
	except:
		return HttpResponseRedirect(reverse('unknown_page'))
	#ЕСЛИ ЗАЯВКА УЖЕ ОБРАБОТАНА
	closed_request_note = GetFirstFilteredNote(ClosedRequest,"note_id",decoded_id)
	try:
		if closed_request_note.is_accepted is False and not (closed_request_note is None):
			open_request_note = closed_request_note.open_request
			return render(request,"it/job_evaluation.html",{"closed_note":closed_request_note,"opened_note":open_request_note})
		else:
			return HttpResponseRedirect(reverse('organisations_requests'))
	except:
		return HttpResponseRedirect(reverse('unknown_page'))

def evaluation(request):

	result = GetObject(request,"result")
	client_comment = GetObject(request,"client_comment")
	request_id = GetObject(request,"close_request_id")
	close_note = GetFirstFilteredNote(ClosedRequest,"note_id",request_id)
	close_note.client_comment = client_comment
	if result == "Принять":
		close_note.is_accepted = True
		close_note.save()
	else:
		close_note.delete()
		AddDeclinedRequest(close_note.open_request,close_note.responsible, close_note.client_comment)
	messages.success(request, "Всё отправлено, спасибо")
	return HttpResponseRedirect(reverse('organisations_requests'))

def unknown_page(request):
	return render(request,"404/404.html")