# 逻辑和之前那个基本一样

def HeapSort(lst):
    def heapadjust(arr, start, end):  # 将以start为根节点的堆调整为大顶堆
        temp = arr[start]
        son = 2 * start + 1
        while son <= end:
            if son < end and arr[son] < arr[son + 1]:  # 找出左右孩子节点较大的
                son += 1
            if temp >= arr[son]:  # 判断是否为大顶堆
                break
            arr[start] = arr[son]  # 子节点上移
            start = son  # 继续向下比较
            son = 2 * son + 1
        arr[start] = temp  # 将原堆顶插入正确位置

    #######
    n = len(lst)
    if n <= 1:
        return lst
    # 建立大顶堆
    root = n // 2 - 1  # 最后一个非叶节点（完全二叉树中）
    while root >= 0:
        heapadjust(lst, root, n - 1)
        root -= 1
    # 掐掉堆顶后调整堆
    i = n - 1
    while i >= 0:
        # 将大顶堆堆顶数放到最后
        (lst[0], lst[i]) = (lst[i], lst[0])
        # 调整剩余数组成的堆
        heapadjust(lst, 0, i - 1)
        i -= 1
    return lst

if __name__ == '__main__':
    lst = [1, 2, 34, 345, 235, 78]

    res = HeapSort(lst)
    print(res)


""" Solution to AOC 2022 - 4 """

import sys

def read_pair(line):
    """Convert string on the format "x-y,z-w" to lower and upper limits."""

    line = line.split(",")
    pair_0 = list(map(int, line[0].split("-")))
    pair_1 = list(map(int, line[1].split("-")))

    return (pair_0[0], pair_1[0]), (pair_0[1], pair_1[1])

def is_full_overlap(lower, upper):
    """Find if there is a full overlap. In that case, return 1 else 0."""

    if (lower[0] >= lower[1] and upper[0] <= upper[1]) or (
        lower[0] <= lower[1] and upper[0] >= upper[1]
    ):
        return 1
    return 0

def is_overlap(lower, upper):
    """Find if there is any overlap. In that case, return 1 else 0."""

    if (upper[0] >= lower[1] and lower[0] <= upper[1]) or (
        lower[0] <= upper[1] and upper[0] >= lower[1]
    ):
        return 1
    return 0

if __name__ == "__main__":
    file_in = sys.argv[1]

    with open(file_in, "r") as fh:
        lines = [line.strip() for line in fh.readlines()]

    overlaps = 0
    full_overlaps = 0
    for line in lines:
        lower, upper = read_pair(line)
        overlaps += is_overlap(lower, upper)
        full_overlaps += is_full_overlap(lower, upper)

    print(f"[Task 1] Number of complete overlaps is {full_overlaps}")
    print(f"[Task 2] Number of overlaps is {overlaps}")


def count(list):

    even=0
    odd=0
    for i in list:
        if i%2==0:
            even=even+1
        else:
            odd=odd+1

    return even,odd
list=[20,14,17,19,28,48,98,47,12,55,67]

even,odd=count(list)

print(even)
print(odd)
print('even : {} and odd : {}'.format(even,odd))

# enter 5 name from users , find and display no of user who has lenth more than five letter
lst=[]
for i in range(5):
    x = input("Enter the name")
    lst.append(x)
def count(lst):
    greater = 0
    lesser = 0
    for i in lst:
        if len(i) >= 5:
            greater += 1
        else:
            lesser += 1
    return greater,lesser
greater,lesser = count(lst)
print("greater : {} and lesser : {}".format(greater,lesser))

def count(lst):
    greater = 0
    lesser = 0
    for i in lst:
        if len(i) >= 5:
            greater += 1
        else:
            lesser += 1
    return greater,lesser
list=['sonu','monu','mudit','rahul','abhishek']
greater,lesser = count(lst)
print("greater : {} and lesser : {}".format(greater,lesser))


import setuptools

with open("README.md", "r") as readme:
    long_description = readme.read()

setuptools.setup(
    name="paillier-python",
    version="0.0.3",
    author="Vinicius E. Martins",
    author_email="vini9x@gmail.com",
    description="Simple python paillier encryption implementation",
    long_description=long_description,
    long_description_content_type="text/markdown",
    packages=setuptools.find_packages(),
    install_requires=["libnum"],
    classifiers=[
        "Programming Language :: Python :: 3.8",
        "Operating System :: GNU/Linux",
    ],
    python_requires=">=3.5",
)


# -*- coding: utf-8 -*-
"""
Created on Tue Jun 20 15:59:54 2017

@author: think
"""

import numpy as np

from pycqed.measurement.waveform_control.element import Element
from pycqed.measurement.waveform_control.pulse import SquarePulse

from Gates import Single_Qubit_Gate, Two_Qubit_Gate

class Readout(Element):

    def __init__(self, name, qubits_name = [], **kw):            ## operation is a set of objects: basic one(two) qubit(s) gates

        super().__init__(name, **kw)

        self.operations = {}

        self.refphase = {}       ##  {'Qubit_1': 0, 'Qubit_2': 0}  this is to keep track of the Z rotation

        self.channel = None

        for qubitname in qubits_name:
             self.refphase[qubitname] = 0

    def Prep_Read(self, qubit):

        self.channel = qubit.plunger_gate['channel_VP']

        self.add(SquarePulse(name='Prep_Read', channel=self.channel, amplitude=0.03, length=10e-6), name='Prep_Read')

        return True

    def shuttle(self, qubit, neighbor):

        self.channel = qubit.plunger_gate['channel_VP']

        self.channel_neighbor = neighbor.plunger_gate['channel_VP']

        self.add(SquarePulse(name='ShuttleOut', channel=self.channel, amplitude=0.03, length=10e-6), name='ShuttleOut')

        self.add(SquarePulse(name='ShuttleIn', channel=self.channel_neighbor, amplitude=0.03, length=10e-6),
                 name='ShuttleIn', refpulse = 'ShuttleIn', refpoint = 'start')

        return True

    def Elzerman(self, qubit):

        self.channel = qubit.plunger_gate['channel_VP']

        self.add(SquarePulse(name='Elzerman', channel=self.channel, amplitude=0.03, length=10e-6), name='Elzerman')

        return True

    def Pauli_Blockade(self, qubit):

        self.channel = qubit.plunger_gate['channel_VP']

        self.add(SquarePulse(name='Pauli_Blockade', channel=self.channel, amplitude=0.03, length=10e-6), name='Pauli_Blockade')

        return True


import os
paths = (os.path.join(root, filename)
        for root, _, filenames in os.walk('/home/shruti/Documents/MFCC/')
        for filename in filenames)

for path in paths:
    # the '#' in the example below will be replaced by the '-' in the filenames in the directory
    newname = path.replace('.wavfeatures', '')
    if newname != path:
        os.rename(path, newname)


path = './slurm-1014.out'

def get_info(path):
    with open(path) as file:
        mAP = []
        for row in file:
            words = row.split(' ')
            words = [x for x in words if x not in ['', '|']]
            # print(words)
            if words[0] == 'mAP':
                mAP.append(words[1])
        return mAP

mAP = get_info(path)
mAP = '\t'.join(mAP)
print(mAP)


# Top-Down DP
class Solution:
    def combinationSum4(self, nums: List[int], target: int) -> int:
        memo = [-1] * (target + 1)
        memo[0] = 1
        def helper(target):
            if memo[target] != -1:
                return memo[target]
            res = 0
            for num in nums:
                if target - num >= 0:
                    res += helper(target - num)
            memo[target] = res
            return res
        return helper(target)

# Bottom-Up DP
class Solution:
    def combinationSum4(self, nums: List[int], target: int) -> int:
        memo = [0] * (target + 1)
        memo[0] = 1
        for i in range(1, target + 1):
            for num in nums:
                if i - num >= 0:
                    memo[i] += memo[i - num]
        return memo[target]


#!/usr/bin/env/python
import re
import yaml

alltext = ''
sections = []
with open('pg1404.txt') as fp:
    for line in fp:
        alltext += line

with open("federalist.yaml", 'r') as stream:
    try:
        config = yaml.load(stream)
    except yaml.YAMLError as exc:
        print(exc)

#print alltext

def small_cap_replace(match):
    if match.start(1) == 0:
        thestring = match.group(1).lower()
        replacement = "\\textsc{" + thestring[0].upper() + thestring[1:] + "}"
    else:
        replacement = "\\textsc{" + match.group(1).lower() + "}"
    return replacement

sections = alltext.split("\n\n\n\n\n")

new_sections = []

for section in sections:
    temp_section = []
    paragraphs = section.split("\n\n")

    for paragraph in paragraphs:
        temp_section.append(paragraph.replace("\n", " "))

    new_sections.append(temp_section)

files = []

for section_index, section in enumerate(new_sections):
    article_no = section_index - 2
    if section_index < 3:
        continue
    if section_index > 87:
        continue

    article_number = section_index - 2
    filename = "federalist_%02d.tex" % (article_number)
    files.append(filename)
    # preprocess paragraphs to find footnotes
    with open(filename, 'w') as fp:
        #fp.write("\\documentclass[10pt]{book}\n")

        #fp.write("\\newcommand\Chapter[2]{\chapter[#1: {\itshape#2}]{#1\\[2ex]\Large\itshape#2}}")
        #fp.write("\\begin{document}\n")
        footnotes = {}
        for paragraph_index, paragraph in enumerate(section):
            matches = re.match(r"^(\d+)\. (.*)", paragraph)
            if matches:
                footnotes[matches.group(1)] = matches.group(2)

        for paragraph_index, paragraph in enumerate(section):
            if paragraph_index == 0:
                pass
                #fp.write("\\section[%s: %s]{%s\\\\ {\\large %s}}\n" % (paragraph, section[1], paragraph, section[1]))
                fp.write("\\chapter[%s: %s]{%s\\\\ {\\small %s}}\n\n" % (paragraph.replace("FEDERALIST ", ""), section[1], paragraph.replace("FEDERALIST ", ""), section[1]))
            elif paragraph_index == 1:
                fp.write("\\textit{%s}\n\n" % (config['article_meta'][article_no]['author']))
                fp.write("\\textit{Original publication date: %s}\n\\vspace{1cm}\n\n" % (config['article_meta'][article_no]['publish_date']))
                pass
                #fp.write("\\section{%s}\n" % (paragraph))
            elif paragraph_index == 2:
                pass
                #fp.write("
%s
\n" % (paragraph))
            elif paragraph_index == 3:
                pass
                #fp.write("
%s
\n" % (paragraph))
            else:
                # replace contents with footnotes
                for key, value in footnotes.iteritems():
                    paragraph = paragraph.replace("(%d)" % (int(key)), "\\footnote{%s}" % (value))

                the_last = False
                if paragraph == "PUBLIUS":
                    the_last = True

                # replace capitalized text with smallcaps
                p = re.compile('([A-Z][A-Z ]{3,})')
                paragraph = p.sub(small_cap_replace, paragraph)

                paragraph = paragraph.replace(" }", "} ")

                # fix quotes, maybe
                p = re.compile('(")(.*?)(")')
                paragraph = p.sub(r"``\2\3", paragraph)

                matches = re.match(r"^(\d+)\. (.*)", paragraph)
                if matches:
                    continue

                if paragraph_index == 4:
                    fp.write("%s\n\\vspace{.4cm}\n\n" % (paragraph))
                elif the_last:
                    fp.write("\\vspace{.5cm}\n%s\n\n\\vspace{1.5cm}\n\n" % (paragraph))
                else:
                    # Split paragraphs into individual sentences in order to make things easier for git
                    # to merge.
                    paragraph = paragraph.replace(". ",". \n").replace("! ","! \n").replace("? ","? \n")
                    fp.write("%s\n\n" % (paragraph))

        #fp.write("\\end{document}\n")

with open('federalist_main.tex', 'w') as fp:
    fp.write("%!TEX TS-program = xelatex\n")
    fp.write("%!TEX encoding = UTF-8 Unicode\n")
    fp.write("\\documentclass[10pt]{book}\n")
    fp.write("\\usepackage[utf8]{inputenc}\n")
    fp.write("\\usepackage{fontspec,xunicode}\n")
    fp.write("\\setmainfont[BoldFont={adobe-caslon-bold.otf}, ItalicFont={adobe-caslon-italic.otf}]{adobe-caslon.otf}\n")
    fp.write("\\title{The Federalist Papers}\n")
    fp.write("\\author{Alexander Hamilton, James Madison and John Jay}\n")
    fp.write("\\renewcommand{\\chaptername}{}\n")
    fp.write("\\renewcommand{\\thechapter}{}\n")

    #fp.write("\\newcommand\Chapter[2]{\chapter[#1: {\itshape#2}]{#1\\[2ex]\Large\itshape#2}}")
    fp.write("\\begin{document}\n")
    fp.write("\\maketitle\n")
    fp.write("\\tableofcontents\n")
    fp.write("\\newpage\n")
    for this_file in files:
        fp.write("\\include{%s}\n" % (this_file.replace(".tex","")))
    fp.write("\\end{document}\n")


import tensorflow as tf

"""
自适应学习并不等价于学习率调整，任何optimizer都不会更改学习率，自适应调整调整的是其它东西而不是用户指定的learn_rate，用户指定的learn_rate除非用户自己变动，否则是不会变化的
"""
loss = tf.Variable(3.0, dtype=tf.float32)
learn_rate = tf.Variable(0.01, dtype=tf.float32)
train_op = tf.train.GradientDescentOptimizer(learn_rate).minimize(loss)
with tf.Session()as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(10000):
        _, rate, lo = sess.run([train_op, learn_rate, loss])
        print(rate, lo)


#!/usr/bin/python
#

# Python 2 & 3 Compatibility
# Compatibility for Python 2.7+ and 3.5+
# Source: https://wiki.python.org/moin/PortingToPy3k/BilingualQuickRef
from __future__ import absolute_import  # default in python3
from __future__ import division  # non-truncating division ( use // for floor divison)
from __future__ import print_function  # print() function (default in python3) instead of builtin
from __future__ import unicode_literals  # unadorned string literals are unicode per default (default in python3)

# Sphinx Docformat Declaration
__docformat__ = 'reStructuredText'

"""
FileArchiver implementation for storing Data in the AFF4 Format.
Note: this packages requires PyAFF4 (0.23+), rdflib (4.2.1+, required by PyAFF4)
"""

# Abstract DataStore Provider
from pyddup.core.abstracts import FileArchiver
from pyddup.core.util import logger

import io
import os

from pyaff4 import aff4_directory
# noinspection PyUnresolvedReferences
from pyaff4 import aff4_utils
from pyaff4 import data_store
from pyaff4 import lexicon
from pyaff4 import rdfvalue

# noinspection PyUnresolvedReferences
from pyaff4 import plugins

# Note: if aff4_utils and/or plugins are NOT imported then the AFF4 Library will fail.
# This pitfall may be caused by some IDE's "Autoimport Optimization".
# For PyCharm the "noinspection PyUnresolvedReferences" comment has been added.
# Make sure to keep those imports.

class AFF4Archiver(FileArchiver):
    def __init__(self):
        self.aff4root = None
        self.aff4urn = None
        pass

    def provide(self, location, filename, **metadata):
        if self.aff4root is None:
            self.aff4root = "{}".format(os.path.join(location, filename, ""))
            with data_store.MemoryDataStore() as resolver:
                aff4urn = rdfvalue.URN.NewURNFromFilename(self.aff4root)
                resolver.Set(aff4urn, lexicon.AFF4_STREAM_WRITE_MODE, rdfvalue.XSDString("truncate"))
                with aff4_directory.AFF4Directory.NewAFF4Directory(resolver, aff4urn) as volume:
                    logger.debug("AFF4Directory created with AFF4ROOT = {} and AFF4URN = {}"
                                 .format(self.aff4root, self.aff4urn))
        pass

    def store_file(self, source, alias=None):
        # type: (str, str) -> None
        if alias is None:
            alias = source
        if self.aff4root is not None:
            with data_store.MemoryDataStore() as resolver:
                aff4urn = rdfvalue.URN.NewURNFromFilename(self.aff4root)
                logger.debug("{}".format(aff4urn))
                with aff4_directory.AFF4Directory.NewAFF4Directory(resolver, aff4urn) as volume:
                    file_urn = volume.urn.Append(alias)
                    logger.debug("URN {} created.".format(file_urn))
                    with volume.CreateMember(file_urn) as member:
                        # noinspection PyBroadException
                        fd_stream = None
                        logger.info("Store: {}".format(source))
                        try:
                            fd_stream = io.open(source, mode="rb")
                            member.WriteStream(fd_stream)
                        except (Exception) as e:
                            logger.error("An error has occurred while writing storing \"{}\": {}".format(alias, e))
                        finally:
                            if fd_stream is not None:
                                fd_stream.close()
                        resolver.Set(member.urn, lexicon.AFF4_STREAM_ORIGINAL_FILENAME, rdfvalue.XSDString(alias))
        else:
            raise RuntimeError("Error writing {} into Archive!".format(alias))

    def close(self):
        if self.aff4root is not None:
            self.aff4root = None


# -*- coding: utf-8 -*-
# @Time : DATE:2021/9/20
# @Author : yan
# @Email : 1792659158@qq.com
# @File : kdTreeDemo.py

import pclpy
from pclpy import pcl
import numpy as np

if __name__ == '__main__':
    # 生成点云数据
    cloud_size = 100
    a = np.random.ranf(cloud_size * 3).reshape(-1, 3) * 1024
    cloud = pcl.PointCloud.PointXYZ.from_array(a)

    kdtree = pcl.kdtree.KdTreeFLANN.PointXYZ()
    kdtree.setInputCloud(cloud)
    searchPoint = pcl.point_types.PointXYZ()
    searchPoint.x = np.random.ranf(1) * 1024
    searchPoint.y = np.random.ranf(1) * 1024
    searchPoint.z = np.random.ranf(1) * 1024
    # k最近邻搜索
    k = 8
    pointIdxNKNSearch = pclpy.pcl.vectors.Int([0] * k)
    pointNKNSquaredDistance = pclpy.pcl.vectors.Float([0] * k)
    print('K nearest neighbor search at (', searchPoint.x,
          '', searchPoint.y,
          '', searchPoint.z,
          ') with k =', k, '\n')
    if kdtree.nearestKSearch(searchPoint, k, pointIdxNKNSearch, pointNKNSquaredDistance) > 0:
        for i in range(len(pointIdxNKNSearch)):
            print("  ", cloud.x[pointIdxNKNSearch[i]],
                  " ", cloud.y[pointIdxNKNSearch[i]],
                  " ", cloud.z[pointIdxNKNSearch[i]],
                  " (squared distance: ", pointNKNSquaredDistance[i], ")", "\n")

    # 使用半径最近邻搜索
    pointIdxRadiusSearch = pclpy.pcl.vectors.Int()
    pointRadiusSquaredDistance = pclpy.pcl.vectors.Float()

    radius = np.random.ranf(1) * 256.0
    print("Neighbors within radius search at (", searchPoint.x,
          " ", searchPoint.y, " ", searchPoint.z, ") with radius=",
          radius, '\n')
    if kdtree.radiusSearch(searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0:
        for i in range(len(pointIdxRadiusSearch)):
            print("  ", cloud.x[pointIdxRadiusSearch[i]],
                  " ", cloud.y[pointIdxRadiusSearch[i]],
                  " ", cloud.z[pointIdxRadiusSearch[i]],
                  " (squared distance: ", pointRadiusSquaredDistance[i], ")", "\n")


import pdb
import os
from pathlib import Path
from safe_subprocess import run

def eval_script(path: Path):
    # script = open(path).read()
    # script = script.replace("def test_check():\n    check(", "check(")
    # open(path, "w").write(script)
    # pdb.set_trace()
    r = run(["python3", str(path)])
    if r.timeout:
        status = "Timeout"
    elif r.exit_code == 0:
        status = "OK"
    elif "SyntaxError" in r.stderr:
        status = "SyntaxError"
    else:
        status = "Exception"
    return {
        "status" : status,
        "exit_code": r.exit_code,
        "stdout": r.stdout,
        "stderr": r.stderr,
    }


from bs4 import BeautifulSoup as bs
import requests
import re

originalurl = "https://www.indeed.com/jobs?q=web+developer&l=fort+lee+nj"
results = {'Javascript': 0, 'Python': 0, 'PHP': 0, 'C#': 0, 'Java': 0}

def get_html_parser(url) :
	response = requests.get(url)
	soup = bs(response.content, "html.parser")
	return (soup)

def extract_keywords(arg_list):
	for page in arg_list:
		newurl = ('https://www.indeed.com' + page)
		parsed_single = get_html_parser(newurl)
		keyword_haystack = parsed_single

		if re.search(r'javascript[\W]', keyword_haystack.get_text(), re.IGNORECASE):
			results['Javascript'] += 1

		if re.search(r'Python[\W]', keyword_haystack.get_text(), re.IGNORECASE):
			results['Python'] += 1

		if re.search(r'PHP[\W]', keyword_haystack.get_text(), re.IGNORECASE):
			results['PHP'] += 1

		if re.search(r'.net[\W]', keyword_haystack.get_text(), re.IGNORECASE):
			results['C#'] += 1

		if re.search(r'java[\W]', keyword_haystack.get_text(), re.IGNORECASE):
			results['Java'] += 1

def get_next_page(url):
	data = url.findAll('div',attrs={'class':'pagination'})

	for page_link in data:
		print (page_link.a['href'])

def get_job_links(url):
	parse = get_html_parser(url)
	data = parse.findAll('a',attrs={'class':'turnstileLink'})
	link_list = []

	for link in data:
		link_list.append(link['href'])

	extract_keywords(link_list)
	get_next_page(parse)

get_job_links(originalurl)
print(results)


# -*- coding: utf-8 -*-
from aiohttp.web import Request

from typing import Union, ForwardRef, List, Optional, Annotated, Any
from dataclasses import dataclass, field

import pytest

from mtpylon.exceptions import (
    InvalidCombinator,
    InvalidConstructor,
    InvalidFunction,
)
from mtpylon.utils import (
    is_valid_combinator,
    is_valid_constructor,
    is_valid_function,
    is_good_for_combinator,
    build_combinator_description,
    get_combinator_number,
    is_optional_type,
    build_function_description,
    get_function_number,
    get_fields_map,
)
from mtpylon import long, int128

@dataclass
class BoolTrue:
    class Meta:
        name = 'boolTrue'

@dataclass
class BoolFalse:
    class Meta:
        name = 'boolFalse'

Bool = Annotated[
    Union[BoolTrue, BoolFalse],
    'Bool'
]

@dataclass
class IncorrectNoNameCombinator:
    class Meta:
        pass

@dataclass
class IncorrectEmptyNameCombinator:
    class Meta:
        name = ''

@dataclass
class IncorrectNameCombinator:
    class Meta:
        name = 323

@dataclass
class User:
    id: int
    name: str

    class Meta:
        name = 'user'
        order = ('id', 'name')

@dataclass
class NotInOrder:
    id: int
    value: int

    class Meta:
        name = 'notInOrder'
        order = ('id',)

@dataclass
class MissedAttribute:
    id = int

    class Meta:
        name = 'attributedDidNotDescribed'
        order = ('id', 'value')

@dataclass
class TreeNode:
    value: int
    left_node: 'Tree'
    right_node: 'Tree'

    class Meta:
        name = 'treeNode'
        order = ('value', 'left_node', 'right_node')

@dataclass
class LeafNode:
    value: int

    class Meta:
        name = 'leafNode'
        order = ('value', )

Tree = Annotated[
    Union[TreeNode, LeafNode],
    'Tree'
]

@dataclass
class InputPeerEmpty:
    class Meta:
        name = 'inputPeerEmpty'

@dataclass
class InputPeerSelf:
    class Meta:
        name = 'inputPeerSelf'

@dataclass
class InputPeerChat:
    chat_id: int

    class Meta:
        name = 'inputPeerChat'
        order = ('chat_id', )

@dataclass
class InputPeerUser:
    user_id: int
    access_hash: long

    class Meta:
        name = 'inputPeerUser'
        order = ('user_id', 'access_hash', )

@dataclass
class InputPeerChannel:
    channel_id: int
    access_hash: long

    class Meta:
        name = 'inputPeerChannel'
        order = ('channel_id', 'access_hash', )

@dataclass
class InputPeerUserFromMessage:
    peer: 'InputPeer'
    msg_id: int
    user_id: int

    class Meta:
        name = 'inputPeerUserFromMessage'
        order = ('peer', 'msg_id', 'user_id', )

@dataclass
class InputPeerChannelFromMessage:
    peer: 'InputPeer'
    msg_id: int
    channel_id: int

    class Meta:
        name = 'inputPeerChannelFromMessage'
        order = ('peer', 'msg_id', 'channel_id', )

InputPeer = Annotated[
    Union[
        InputPeerEmpty,
        InputPeerSelf,
        InputPeerChat,
        InputPeerUser,
        InputPeerChannel,
        InputPeerUserFromMessage,
        InputPeerChannelFromMessage
    ],
    'InputPeer'
]

@dataclass
class Task:
    content: str
    finished: Bool

    class Meta:
        name = 'task'
        order = ('content', 'finished')

@dataclass
class TaggedTask:
    content: str
    finished: Bool
    tags: Optional[List[str]] = field(metadata={'flag': 1})

    class Meta:
        name = 'taggedTask'
        order = ('content', 'finished', 'tags')

@dataclass
class ResPQ:
    nonce: int128
    server_nonce: int128
    pq: bytes
    server_public_key_fingerprints: List[long]

    class Meta:
        name = 'resPQ'
        order = (
            'nonce',
            'server_nonce',
            'pq',
            'server_public_key_fingerprints'
        )

@dataclass
class RpcResult:
    req_msg_id: long
    result: Any

    class Meta:
        name = 'rpc_result'
        order = ('req_msg_id', 'result')

class AnotherClass:

    def __init__(self, a, b):
        self.a = a
        self.b = b

@dataclass
class WrongAttrType:
    id: int
    value: AnotherClass

    class Meta:
        name = 'wrongAttrType'
        order = ('id', 'value')

@dataclass
class MessageActionEmpty:
    class Meta:
        name = 'messageAction'

@dataclass
class MessageActionChatAddUser:
    users: List[int]

    class Meta:
        name = 'messageActionChatAddUser'
        order = ('users', )

MessageAction = Annotated[
    Union[MessageActionEmpty, MessageActionChatAddUser],
    'MessageAction'
]

@dataclass
class ChatParticipantCombinator:
    user_id: int
    inviter_id: int
    date: int

    class Meta:
        name = 'chatParticipant'
        order = ('user_id', 'inviter_id', 'date')

@dataclass
class ChatParticipantCreator:
    user_id: int

    class Meta:
        name = 'chatParticipantCreator'
        order = ('user_id', )

@dataclass
class ChatParticipantAdmin:
    user_id: int
    inviter_id: int
    date: int

    class Meta:
        meta = 'chatParticipantAdmin'

ChatParticipant = Annotated[
    Union[
        ChatParticipantCombinator,
        ChatParticipantCreator,
        ChatParticipantAdmin
    ],
    'ChatParticipant'
]

@dataclass
class ChatParticipants:
    chat_id: int
    participants: List[ChatParticipant]
    version: int

    class Meta:
        name = 'chatParticipants'
        order = ('chat_id', 'participants', 'version')

@dataclass
class InputMediaPhotoExternal:
    url: str
    ttl_seconds: Optional[int] = field(metadata={'flag': 0})

    class Meta:
        name = 'inputMediaPhotoExternal'
        order = ('url', 'ttl_seconds')

@dataclass
class InputMediaGifExternal:
    url: str
    q: str

    class Meta:
        name = 'inputMediaGifExternal'

InputMedia = Annotated[
    Union[InputMediaPhotoExternal, InputMediaGifExternal],
    'InputMedia'
]

@dataclass
class DialogFilter:
    class Meta:
        name = 'dialogFilter'

@dataclass
class UpdateDialogFilter:
    id: int
    filter: Optional[DialogFilter] = field(metadata={'flag': 0})

    class Meta:
        name = 'updateDialogFilter'
        order = ('id', 'filter')

@dataclass
class UpdateConfig:
    class Meta:
        name = 'updateConfig'

Update = Annotated[
    Union[UpdateConfig, UpdateDialogFilter],
    'Update'
]

@dataclass
class ExtendedTree:
    val: int
    left: Optional['ExtendedTree'] = field(metadata={'flag': 0})
    right: Optional['ExtendedTree'] = field(metadata={'flag': 1})

    class Meta:
        name = 'extendedTree'
        order = ('val', 'left', 'right')

@dataclass
class WithoutFlags:
    val: Optional[int]

    class Meta:
        name = 'withoutFlags'
        order = ('val', )

@dataclass
class WrongConstructorUsed:
    val: Optional[AnotherClass] = field(metadata={'flag': 0})

    class Meta:
        name = 'withoutFlags'
        order = ('val', )

@dataclass
class IncorrectNoMetaCombinator:
    pass

@dataclass
class Content:
    content: str

@dataclass
class ContentWrapper:
    content: Content = field(metadata={
        'bare': '%',
    })

    class Meta:
        name = 'contentWrapper'
        order = ('content', )

@dataclass
class ContentWrapperLower:
    content: Content = field(metadata={
        'bare': 'lower'
    })

    class Meta:
        name = 'contentWrapperLower'
        order = ('content', )

@dataclass
class ContentList:
    content_list: List[Content] = field(metadata={
        'bare': 'lower',
        'item_meta': {
            'bare': '%'
        }
    })

    class Meta:
        name = 'contentList'
        order = ('content_list', )

@dataclass
class ContentLowerList:
    content_list: List[Content] = field(metadata={
        'bare': 'lower',
        'item_meta': {
            'bare': 'lower'
        }
    })

    class Meta:
        name = 'contentLowerList'
        order = ('content_list', )

@dataclass
class Message:
    msg_id: long
    seqno: int
    bytes: int
    body: Any

    class Meta:
        name = 'message'
        order = (
            'msg_id',
            'seqno',
            'bytes',
            'body',
        )

@dataclass
class MessageContainer:
    messages: List[Message] = field(
        metadata={
            'bare': 'lower',
            'item_meta': {
                'bare': '%'
            }
        }
    )

    class Meta:
        name = 'msg_container'
        order = ('messages',)

async def equals(request: Request, a: int, b: int) -> Bool:  # pragma: nocover
    if a == b:
        return BoolTrue()
    return BoolFalse()

async def get_task_content(
    request: Request,
    task: Task
) -> str:  # pragma: nocover
    return task.content

async def has_tasks(
    request: Request,
    tasks: List[Task]
) -> Bool:  # pragma: nocover
    if len(tasks) > 0:
        return BoolTrue()
    return BoolFalse()

def not_async_func(
    request: Request,
    a: int,
    b: int
) -> Bool:  # pragma: nocover
    if a == b:
        return BoolTrue()
    return BoolFalse()

async def invalid_param(
    request: Request,
    a: AnotherClass,
    b: int
) -> Bool:  # pragma: nocover
    if str(a) == str(b):
        return BoolTrue()
    return BoolFalse()

async def invalid_return_type(
    request: Request,
    a: int,
    b: int
) -> AnotherClass:  # pragma: nocover
    return AnotherClass(a, b)

async def invalid_not_annotated_params(
    request: Request,
    a,
    b
) -> Bool:  # pragma: nocover
    if a == b:
        return BoolTrue()
    return BoolFalse()

async def invalid_args(
    request: Request,
    *args: List[Task]
) -> Bool:  # pragma: nocover
    if len(args) > 0:
        return BoolTrue()
    return BoolFalse()

async def invalid_kwargs(
    request: Request,
    **kwargs
) -> Bool:  # pragma: nocover
    if 'value' in kwargs:
        return BoolTrue()
    return BoolFalse()

async def func_no_request(task: Task) -> Bool:  # pragma: nocover
    return BoolTrue()

class TestIsOptionalType:

    def test_base_type(self):
        assert not is_optional_type(int)

    def test_named_tupele(self):
        assert not is_optional_type(ChatParticipantAdmin)

    def test_optional_base_type(self):
        assert is_optional_type(Optional[int])

    def test_optional_constructor_type(self):
        assert is_optional_type(Optional[MessageAction])

class TestIsGoodForCombinator:

    def test_basic_type(self):
        assert is_good_for_combinator(str)

    def test_constructor_type(self):
        assert is_good_for_combinator(Bool, [Bool, Tree])

    def test_constructor_forward_ref(self):
        assert is_good_for_combinator(ForwardRef('Tree'), [Bool, Tree])

    def test_constructor_annotation(self):
        left_node_type = TreeNode.__dataclass_fields__['left_node'].type
        assert is_good_for_combinator(left_node_type, [Bool, Tree])

    def test_wrong_value(self):
        assert not is_good_for_combinator(AnotherClass)

    def test_wrong_forward_ref(self):
        assert not is_good_for_combinator(ForwardRef('Tree'), [Bool])

    def test_options_list(self):
        assert is_good_for_combinator(Optional[List[str]])
        assert is_good_for_combinator(
            Optional[List[Task]],
            constructors=[Task]
        )

    def test_any_combinator(self):
        assert is_good_for_combinator(Any)

    def test_combinator_with_bare_type(self):
        assert is_good_for_combinator(
            ContentWrapper,
            [
                ContentWrapper,
                Content
            ]
        )

class TestIsValidCombinator:

    def test_empty_correct_combinator(self):
        is_valid_combinator(BoolTrue)
        is_valid_combinator(BoolFalse)

    def test_no_meta_in_combinator(self):
        with pytest.raises(InvalidCombinator):
            is_valid_combinator(IncorrectNoMetaCombinator)

    def test_no_name_in_combinator(self):
        with pytest.raises(InvalidCombinator):
            is_valid_combinator(IncorrectNoNameCombinator)

    def test_wrong_name_in_combinator(self):
        with pytest.raises(InvalidCombinator):
            is_valid_combinator(IncorrectEmptyNameCombinator)

        with pytest.raises(InvalidCombinator):
            is_valid_combinator(IncorrectNameCombinator)

    def test_combinator_with_attributes(self):
        is_valid_combinator(User)

    def test_attribute_is_not_in_order(self):
        with pytest.raises(InvalidCombinator):
            is_valid_combinator(NotInOrder)

    def test_missed_attribute(self):
        with pytest.raises(InvalidCombinator):
            is_valid_combinator(MissedAttribute)

    def test_wrong_attribute(self):
        with pytest.raises(InvalidCombinator):
            is_valid_combinator(WrongAttrType)

    def test_recursive_combinator(self):
        is_valid_combinator(TreeNode, [Bool, Tree])

    def test_list_combinator(self):
        is_valid_combinator(MessageActionChatAddUser)
        is_valid_combinator(ChatParticipants, [ChatParticipant])

    def test_valid_optional_base_type_field(self):
        is_valid_combinator(InputMediaPhotoExternal)

    def test_invalid_declared_optional_field(self):
        with pytest.raises(InvalidCombinator):
            is_valid_combinator(WithoutFlags)

    def test_valid_optional_constructor_field(self):
        is_valid_combinator(UpdateDialogFilter, [Update, DialogFilter])
        is_valid_combinator(ExtendedTree, [ExtendedTree])

    def test_valid_optional_list_field(self):
        is_valid_combinator(TaggedTask, [Bool, TaggedTask])

    def test_invalid_not_constructor_optional_field(self):
        with pytest.raises(InvalidCombinator):
            is_valid_combinator(WrongConstructorUsed)

    def test_valid_with_any_field(self):
        is_valid_combinator(RpcResult, [RpcResult])

class TestIsValidConstructor:

    def test_single_combinator(self):
        is_valid_constructor(User)

    def test_union_of_combinators(self):
        is_valid_constructor(Bool, [Bool, Tree])
        is_valid_constructor(Tree, [Bool, Tree])
        is_valid_constructor(ExtendedTree, [ExtendedTree])
        is_valid_constructor(RpcResult, [RpcResult])
        is_valid_constructor(ContentWrapper, [Content])

    def test_wrong_value(self):
        with pytest.raises(InvalidConstructor):
            is_valid_constructor(int)

class TestIsValidFunction:

    def test_valid_with_basic_params(self):
        is_valid_function(equals, [Bool])

    def test_valid_with_constructor_params(self):
        is_valid_function(get_task_content, [Task])

    def test_valid_list_params(self):
        is_valid_function(has_tasks, [Task, Bool])

    def test_not_async_function(self):
        with pytest.raises(InvalidFunction):
            is_valid_function(not_async_func, [Bool])

    def test_invalid_param(self):
        with pytest.raises(InvalidFunction):
            is_valid_function(invalid_param, [Bool])

    def test_invalid_no_constructor(self):
        with pytest.raises(InvalidFunction):
            is_valid_function(equals)

    def test_invalid_return_type(self):
        with pytest.raises(InvalidFunction):
            is_valid_function(invalid_return_type)

    def test_not_annotaed_param(self):
        with pytest.raises(InvalidFunction):
            is_valid_function(invalid_not_annotated_params, [Bool])

    def test_invalid_args(self):
        with pytest.raises(InvalidFunction):
            is_valid_function(invalid_args, [Bool, Task])

    def test_invalid_kwargs(self):
        with pytest.raises(InvalidFunction):
            is_valid_function(invalid_kwargs)

    def test_request_object_does_not_passed(self):
        with pytest.raises(InvalidFunction):
            is_valid_function(func_no_request)

class TestBuildCombinatorDescription:

    def test_empty_combinator(self):
        assert build_combinator_description(BoolTrue, Bool) == (
            'boolTrue = Bool'
        )
        assert build_combinator_description(BoolFalse, Bool) == (
            'boolFalse = Bool'
        )

    def test_simple_combinator(self):
        assert build_combinator_description(LeafNode, Tree) == (
            'leafNode value:int = Tree'
        )
        assert build_combinator_description(User, User) == (
            'user id:int name:string = User'
        )

    def test_recursive_combinator(self):
        assert build_combinator_description(Task, Task) == (
            'task content:string finished:Bool = Task'
        )
        assert build_combinator_description(TreeNode, Tree) == (
            'treeNode value:int left_node:Tree right_node:Tree = Tree'
        )

    def test_list_combinator_message_action(self):
        assert build_combinator_description(
            MessageActionChatAddUser,
            MessageAction
        ) == 'messageActionChatAddUser users:Vector<int> = MessageAction'
        assert build_combinator_description(
            MessageActionChatAddUser,
            MessageAction,
            for_type_number=True
        ) == 'messageActionChatAddUser users:Vector int = MessageAction'

    def test_list_chat_participants(self):
        assert build_combinator_description(
            ChatParticipants,
            ChatParticipants
        ) == 'chatParticipants chat_id:int participants:Vector<ChatParticipant> version:int = ChatParticipants'  # noqa
        assert build_combinator_description(
            ChatParticipants,
            ChatParticipants,
            for_type_number=True
        ) == 'chatParticipants chat_id:int participants:Vector ChatParticipant version:int = ChatParticipants'  # noqa

    def test_optional_basic_type(self):
        assert build_combinator_description(
            InputMediaPhotoExternal,
            InputMedia
        ) == 'inputMediaPhotoExternal flags:# url:string ttl_seconds:flags.0?int = InputMedia'  # noqa

    def test_optional_constructor_type(self):
        assert build_combinator_description(
            UpdateDialogFilter,
            Update
        ) == 'updateDialogFilter flags:# id:int filter:flags.0?DialogFilter = Update'  # noqa

    def test_several_optional_constructor_type(self):
        assert build_combinator_description(
            ExtendedTree,
            ExtendedTree
        ) == 'extendedTree flags:# val:int left:flags.0?ExtendedTree right:flags.1?ExtendedTree = ExtendedTree'  # noqa

    def test_optional_list_field(self):
        assert build_combinator_description(
            TaggedTask,
            TaggedTask
        ) == 'taggedTask flags:# content:string finished:Bool tags:flags.1?Vector<string> = TaggedTask'  # noqa

        assert build_combinator_description(
            TaggedTask,
            TaggedTask,
            for_type_number=True,
        ) == 'taggedTask flags:# content:string finished:Bool tags:flags.1?Vector string = TaggedTask'  # noqa

    def test_res_pq_with_bytes(self):
        assert build_combinator_description(
            ResPQ,
            ResPQ
        ) == 'resPQ nonce:int128 server_nonce:int128 pq:bytes server_public_key_fingerprints:Vector<long> = ResPQ'  # noqa
        assert build_combinator_description(
            ResPQ,
            ResPQ,
            for_type_number=True
        ) == 'resPQ nonce:int128 server_nonce:int128 pq:string server_public_key_fingerprints:Vector long = ResPQ'  # noqa

    def test_rpc_result(self):
        assert build_combinator_description(
            RpcResult, RpcResult
        ) == 'rpc_result req_msg_id:long result:Object = RpcResult'

    def test_bare_constructor_type_content_wrapper(self):
        assert build_combinator_description(
            ContentWrapper,
            ContentWrapper
        ) == 'contentWrapper content:%Content = ContentWrapper'

    def test_bare_constructor_type_content_lower_wrapper(self):
        assert build_combinator_description(
            ContentWrapperLower,
            ContentWrapperLower
        ) == 'contentWrapperLower content:content = ContentWrapperLower'

    def test_bare_constructor_type_content_list(self):
        assert build_combinator_description(
            ContentList,
            ContentList
        ) == 'contentList content_list:vector<%Content> = ContentList'

    def test_bare_constructor_type_content_list_lower(self):
        assert build_combinator_description(
            ContentLowerList,
            ContentLowerList
        ) == 'contentLowerList content_list:vector<content> = ContentLowerList'

    def test_message_container(self):
        assert build_combinator_description(
            MessageContainer,
            MessageContainer
        ) == 'msg_container messages:vector<%Message> = MessageContainer'

class TestCombinatorNumber:

    def test_bool(self):
        assert get_combinator_number(BoolTrue, Bool) == -1720552011
        assert get_combinator_number(BoolFalse, Bool) == -1132882121

    def test_input_peer(self):
        assertion_list = [
            (InputPeerEmpty, 2134579434),
            (InputPeerSelf, 2107670217),
            (InputPeerChat, 396093539),
            (InputPeerUser, 2072935910),
            (InputPeerChannel, 548253432),
            (InputPeerUserFromMessage, 398123750),
            (InputPeerChannelFromMessage, -1667893317),
        ]

        for combinator, value in assertion_list:
            assert get_combinator_number(combinator, InputPeer) == value

    def test_message_action(self):
        assert get_combinator_number(
            MessageActionChatAddUser,
            MessageAction
        ) == 0x488a7337

    def test_chart_participants(self):
        assert get_combinator_number(
            ChatParticipants,
            ChatParticipants
        ) == 0x3f460fed

    def test_input_media(self):
        assert get_combinator_number(
            InputMediaPhotoExternal,
            InputMedia
        ) == -440664550

    def test_update(self):
        assert get_combinator_number(
            UpdateDialogFilter,
            Update
        ) == 0x26ffde7d

    def test_rpc_result(self):
        assert get_combinator_number(RpcResult, RpcResult) == -212046591

    def test_msg_container(self):
        assert get_combinator_number(
            MessageContainer,
            MessageContainer
        ) == 1945237724

class TestBuildFunctionDescription:

    def test_equals(self):
        assert build_function_description(equals) == (
            'equals a:int b:int = Bool'
        )

    def test_get_task_content(self):
        assert build_function_description(get_task_content) == (
            'get_task_content task:Task = string'
        )

    def test_has_tasks(self):
        assert build_function_description(has_tasks) == (
            'has_tasks tasks:Vector<Task> = Bool'
        )
        assert build_function_description(
            has_tasks,
            for_type_number=True
        ) == 'has_tasks tasks:Vector Task = Bool'

class TestGetFunctionNumber:

    def test_equals(self):
        assert get_function_number(equals) == -1241515178

    def test_get_task_content(self):
        assert get_function_number(get_task_content) == 0x2e08ac72

    def test_has_tasks(self):
        assert get_function_number(has_tasks) == 0x20e13fab

class TestGetFieldsMap:

    def test_fields_map(self):
        fields_map = get_fields_map(User)

        assert 'id' in fields_map
        assert 'name' in fields_map

    def test_fields_map_typeerror(self):
        with pytest.raises(TypeError):
            get_fields_map(44)


# Agasha Ratam
# Version Aug 7, 2020

from bs4 import BeautifulSoup

def edit_html(html_s):
	soup = BeautifulSoup(html_s, 'html.parser')

	# Delete navigation bar
	nav = soup.body.find('div', attrs={'role': 'navigation'})
	if nav:
		nav.extract()

	# Delete R source code
	r_code = soup.body.find(attrs={'id': 'flexdashboard-source-code'})
	if r_code:
		r_code.extract()

	# Add our own navigation bar by adding lines to the start of <body>
	template_f = open('dashboard_template.html', 'r')
	template_soup = BeautifulSoup(template_f.read(), 'html.parser')

	if soup.body.contents == []:
		soup.body.contents.extend(template_soup.body.contents)
	else:
		first = soup.body.contents[0]
		while template_soup.body.contents != []:
			first.insert_before(template_soup.body.contents[0])

	# Add stylesheet by adding lines to the end of <body>
	style = soup.new_tag('link', rel='stylesheet', href='style.css')
	soup.body.append(style)

	return nice_prettify(soup)

def nice_prettify(bs_tag):
	s = bs_tag.prettify()

	new = []
	for line in s.split('\n'):
		count = 0
		while len(line) > 0 and line[0] == ' ':
			count += 1
			line = line[1:]
		for i in range(count):
			line = '\t' + line
		new.append(line)

	return '\n'.join(new)


#!/bin/python3

import os
import subprocess
import shutil

#https://www.pythoncentral.io/how-to-recursively-copy-a-directory-folder-in-python/

path = '/data/app_data/'

def is_file_open(file_name):
    """This function will take a path as input and will check if there any files open(being used) within that folder,
    and will return True in case the file is open, or else return False"""
    print("Checking ", file_name, " - ",)
    process = subprocess.Popen(["/sbin/lsof", file_name], stdout=subprocess.PIPE)
    process.communicate()[0]
    if process.returncode == 0:
        print("Open!")
        return True
    else:
        print("Not open")
        return False

def move_closed_files(path):
    """This function will take a path as input and will go down the tree listing all the files one by one and then call is_file_open() function to check if it is open.
    In case the file is not open, it will move the file to some desired path by calling move() function from source to a destination dir"""
    file_list = []
    for (root,dirs,files) in os.walk(path):
        for file_name in files:
            full_file_name = os.path.join(root, file_name)
            if not is_file_open(full_file_name): # Checking if the file is not open under if condition and if not open, move the file and if open skip and continue to the next file.
                file_list.append(full_file_name)
                print("File can be moved as not open ", file_name)
                source = full_file_name
                destdir = root.replace('fileStore','transferred')
                destination = source.replace('fileStore','transferred')
                if not os.path.exists(destdir):
                    os.makedirs(destdir)
                move(source,destination)
            else:
                continue

def move(src, dest):
    """This function is using shutil module to move source files to specified destination path"""
    shutil.move(src, dest)

# main function to start the process
move_closed_files(path)


#!/usr/bin/env python3
# encoding: utf-8

import socket
from multiprocessing import Process
from threading import Thread
from concurrent.futures import ThreadPoolExecutor
from time import sleep
from collections import deque
from common.bencode import bencode, bdecode
from common.utils import *
from config import Config
from common.metadata import download_metadata
from common.database import RedisClient2

logger = get_logger("logger_dht_main")

# 接收和发送find_node请求的数据结构
class KNode(object):
    def __init__(self, nid, ip, port):
        # nodeId 为DHT中node的key
        self.nid = nid
        # node对应的ip
        self.ip = ip
        # node 对应的port
        self.port = port

# 封装UDP协议和KRPC协议类
class DHT(Thread):
    def __init__(self,bind_ip,bind_port):
        Thread.__init__(self)
        # socket UDP协议封装
        self.ufd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        # 绑定对应线程ip 和 port
        self.ufd.bind((bind_ip, bind_port))

    def send_krpc(self, msg, address):
        # noinspection PyBroadException
        try:
            # KRPC协议需要使用bencode进行传送信息的封装
            self.ufd.sendto(bencode(msg), address)
        except Exception:
            print('krpc发送失败')
            pass

# DHT客户端
class DHTClient(DHT):
    def __init__(self,bind_ip,bind_port):
        DHT.__init__(self,bind_ip,bind_port)
        self.bind_ip = bind_ip
        self.bind_port = bind_port
        # 线程守护进程
        self.setDaemon(True)
        # 随机生成nodeID
        self.nid = random_id()
        # 双端队列，用于存储接收到的nodes信息
        self.nodes = deque(maxlen=Config.MAX_NODE_SIZE)

    # KRPC中的find_node请求
    def send_find_node(self, address, nid=None):
        # 判断进行路由扩散还是加入DHT网络
        nid = get_neighbor(nid, self.nid) if nid else self.nid
        tid = entropy(2)  # 随机生成长度为2的TransactionID,
        msg = {
            't': tid,
            'y': 'q',
            'q': 'find_node',
            'a': {
                'id': nid,
                'target': random_id()
            }
        }
        #print('发送krpc')
        print('发送find_node msg: {0} to address: {1}'.format(msg, address))
        self.send_krpc(msg, address)

    # 初始化DHT网络，加入到DHT网络中
    def join_dht(self):
        """
        通过BOOTSTRAP_NODES引路从而加入DHT网络中
        """
        for address in Config.BOOTSTRAP_NODES:
            self.send_find_node(address)

    def rejoin_dht(self):
        '''
        每隔一段检查nodes是否没有了，没有则重新加入DHT网络
        '''
        if len(self.nodes) == 0:
            #print('nodes耗尽，从DHT网络获取nodes')
            self.join_dht()
        timer(Config.REJOIN_DHT_INTERVAL, self.rejoin_dht)

    def auto_send_find_node(self):
        '''
        从获取到的nodes中选出一个，并向其发送find_node请求
        '''
        while True:
            try:
                node = self.nodes.popleft()
                self.send_find_node((node.ip, node.port), node.nid)
            except IndexError:
                pass
            # 设置发送请求的时间间隔
            sleep(1.0 / Config.MAX_NODE_SIZE)

    def process_find_node_response(self, msg):
        nodes = decode_nodes(msg['r']['nodes'])
        for node in nodes:
            (nid, ip, port) = node
            #print('回复find_node')
            #print('回复来自' + node + '的find_node请求: ')
            if len(nid) != 20:
                print('node格式出错')
                continue
            if ip == self.bind_ip:
                print('find_node请求来自自身请求')
                continue
            n = KNode(nid, ip, port)
            # 将接收到的nodes加入的双端队列中
            print('收到node: {0} 加入双端队列中 from address {1}'.format(node, (ip, port)))
            self.nodes.append(n)
            #RedisClient2.set_keyinfo(nid, (ip, port))

# DHT服务类
class DHTServer(DHTClient):
    def __init__(self, bind_ip, bind_port, process_id):
        DHTClient.__init__(self, bind_ip, bind_port)
        # 客户端绑定ip和端口
        self.bind_ip = bind_ip
        self.bind_port = bind_port
        # 进程id
        self.process_id = process_id
        # 线程池最大下载线程数
        self.pool = ThreadPoolExecutor(Config.DOWNLOAD_THREAD)
        # get_peers请求和announce_peer请求
        self.process_request_actions = {
            'get_peers': self.on_get_peers_request,
            'announce_peer': self.on_announce_peer_request,
        }

        # 定时器，定时重新加入DHT网络
        timer(Config.REJOIN_DHT_INTERVAL, self.rejoin_dht)
    def run(self):
        # 加入DHT网络
        self.rejoin_dht()
        # 监听套接字数据
        while True:
            #print('监听中....')
            # noinspection PyBroadException
            try:
                # 从socket上获取data和address
                (data, address) = self.ufd.recvfrom(65536)
                # 将数据解码
                msg = bdecode(data)
                # 监听信息，并回复
                self.on_message(msg, address)
            except Exception:
                pass

    # 处理request请求
    def on_message(self, msg, address):
        try:
            # 收到的信息为response
            if msg['y'] == 'r':
                # 收到对方发来的nodes
                if 'nodes' in msg['r']:
                    # 向对方发来的nodes发送find_node请求，进一步扩大路由表
                    self.process_find_node_response(msg)
            # 收到的信息为quest
            elif msg['y'] == 'q':
                try:
                    # 判断收到的信息是get_peers还是announce_peer请求，并回复相应报文
                    self.process_request_actions[msg['q']](msg, address)
                except KeyError:
                    # 回复出错，则装死。。
                    self.play_dead(msg, address)
        except KeyError:
            pass

    # 处理get_peers的request请求
    def on_get_peers_request(self, msg, address):
        #print('回复get_peers请求')
        print('收到get_peers请求 msg: {0} from address: {1}'.format(msg, address))
        try:
            h = msg['a']['info_hash']
            tid = msg['t']
            token = h[:2]
            msg = {
                't': tid,
                'y': 'r',
                'r': {
                    'id': get_neighbor(h, self.nid),
                    'nodes': '',
                    'token': token
                }
            }
            self.send_krpc(msg, address)
        except KeyError:
            pass

    def on_announce_peer_request(self, msg, address):
        #print('回复announce_peer请求')
        # noinspection PyBroadException
        print('收到announce_peer请求 msg: {0} from address: {1}'.format(msg, address))
        try:
            h = msg['a']['info_hash']
            token = msg['a']['token']
            if h[:2] == token:
                if 'implied_port ' in msg['a'] and msg['a']['implied_port '] != 0:
                    port = address[1]
                else:
                    port = msg['a']['port']
                # 下载metadata
                self.pool.submit(download_metadata, (address[0], port), h, self.process_id)
        except Exception:
            return
        finally:
            self.ok(msg, address)

    # 装死。。
    def play_dead(self, msg, address):
        try:
            tid = msg['t']
            msg = {
                't': tid,
                'y': 'e',
                'e': [202, 'Server Error']
            }
            self.send_krpc(msg, address)
        except KeyError:
            pass
    # 回复成功消息
    def ok(self, msg, address):
        try:
            tid = msg['t']
            nid = msg['a']['id']
            msg = {
                't': tid,
                'y': 'r',
                'r': {
                    'id': get_neighbor(nid, self.nid)
                }
            }
            self.send_krpc(msg, address)
        except KeyError:
            pass

def _start_thread(offset):
    """
    启动线程

    :param offset: 端口偏移值
    """
    logger.info("DHT网络进程 运行 成功 ! {0} ->>>> {1}:{2}".format(offset,Config.BIND_IP,Config.BIND_PORT + offset))
    dht = DHTServer(Config.BIND_IP, Config.BIND_PORT + offset, offset)
    dht.start()
    dht.auto_send_find_node()
    # t = Thread(target=dht.auto_send_find_node)
    # t.start()
    # t.join()
    # dht.join()

def start_server():
    """
    多进程启动服务
    """
    processes = []
    # Config.MAX_PROCESSES
    for i in range(Config.MAX_PROCESSES):
        processes.append(Process(target=_start_thread, args=(i,)))

    for p in processes:
        p.start()

    for p in processes:
        p.join()


from pytorch_tabnet.tab_model import TabNetClassifier
from tabben.datasets import OpenTabularDataset
from itertools import compress
import torch
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import roc_auc_score
import pandas as pd
import numpy as np
import os
import wget
from pathlib import Path
from matplotlib import pyplot as plt
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--dset_id', required=True, type=str, choices=['arcene', 'poker', 'higgs', 'sarcos' ,'covertype', 'cifar10', 'adult', 'parkinsons', 'adult', 'amazon'])
parser.add_argument('--target_task', default=1, type=int)
parser.add_argument('--valid_split', default=.2, type=float)
parser.add_argument('--task', required=True, type=str,choices = ['binary','multiclass','regression'])
parser.add_argument('--embedding_size', default=32, type=int)
parser.add_argument('--transformer_depth', default=6, type=int)
parser.add_argument('--n_d', default=64, type=int)
parser.add_argument('--n_a', default=64, type=int)
parser.add_argument('--n_steps', default=5, type=int)
parser.add_argument('--gamma', default=1.5, type=float)
parser.add_argument('--n_independent', default=2, type=int)
parser.add_argument('--n_shared', default=2, type=int)
parser.add_argument('--cat_emb_dim', default=1, type=int)
parser.add_argument('--lambda_sparse', default=1e-4, type=float)
parser.add_argument('--momentum', default=.7, type=float)
parser.add_argument('--lr', default=2e-2, type=float)
parser.add_argument('--gamma_step', default=.95, type=float)
parser.add_argument('--step_size', default=20, type=int)
parser.add_argument('--clip_value', default=2., type=float)
parser.add_argument('--max_epochs', default=1000, type=int)
parser.add_argument('--patience', default=100, type=int)
parser.add_argument('--batch_size', default=256, type=int)
parser.add_argument('--virtual_batch_size', default=128, type=int)
opt = parser.parse_args()
ds = OpenTabularDataset('./', opt.dset_id)
test_ds = OpenTabularDataset('./', opt.dset_id, split='test')
train = ds.dataframe()
test = test_ds.dataframe()

train["Set"] = np.random.choice(["train", "valid"], p =[1-opt.valid_split, opt.valid_split], size=(train.shape[0],))
test['Set'] = 'test'

n_total = len(train)

train = pd.concat([train, test])
train = train.reset_index(drop=True)

columns = list(train.columns)
unused_features = []

train_indices = train[train.Set=="train"].index
valid_indices = train[train.Set=="valid"].index
test_indices = train[train.Set=="test"].index

dataset_name = opt.dset_id
if dataset_name ==  "covertype":
    categorical_indicator = ([False] * 10) + ([True]*44)
    target = 'Cover_Type'
elif dataset_name == "arcene":
    target = "label"
    categorical_indicator = ([False] * 10000)
elif dataset_name == "higgs":
    target = "label"
    categorical_indicator = ([False] * 28)
elif dataset_name == "poker":
    target = 'label'
    categorical_indicator = ([True] * 10)
elif dataset_name == "sarcos":
    target = "T" + str(opt.target_task)
    unused_features = ["T" + str(i) for i in range(1,8)]
    categorical_indicator = ([False] * 21)
    test = test.iloc[:,target_task - 1]
    y_test = y_test.iloc[:,target_task-1]
elif dataset_name == "cifar10":
    categorical_indicator = ([False] * 3072)
elif dataset_name == "adult":
    target = 'income'
    categorical_indicator = [False, True, False, True , False, True, True, True, True, True, False, False, False, True]
elif dataset_name == "parkinsons":
    target = "motor_UPDRS" if opt.target_task == 1 else "total_UPDRS"
    unused_features = ['motor_UPDRS', 'total_UPDRS']
    categorical_indicator = [False] * 16
elif dataset_name == 'amazon':
    target = "ACTION"
    categorical_indicator = [True] * 9

cat_columns = list(compress(train.columns, categorical_indicator))

nunique = train.nunique()
types = train.dtypes

categorical_columns = []
categorical_dims =  {}
for col in train.columns:
    if types[col] == 'object' or nunique[col] < 200:
        print(col, train[col].nunique())
        l_enc = LabelEncoder()
        train[col] = train[col].fillna("VV_likely")
        train[col] = l_enc.fit_transform(train[col].values)
        categorical_columns.append(col)
        categorical_dims[col] = len(l_enc.classes_)
    else:
        train.fillna(train.loc[train_indices, col].mean(), inplace=True)

unused_feat = [target] + unused_features

features = [ col for col in train.columns if col not in unused_feat+[target]]

cat_idxs = [ i for i, f in enumerate(features) if f in categorical_columns]

cat_dims = [ categorical_dims[f] for i, f in enumerate(features) if f in categorical_columns]

X_train = train[features].values[train_indices]
y_train = train[target].values[train_indices]

X_valid = train[features].values[valid_indices]
y_valid = train[target].values[valid_indices]

X_test = train[features].values[test_indices]
y_test = train[target].values[test_indices]

if opt.task == 'regression':
    eval_metric = ['rmse']
    clf = TabNetRegressor(
        n_d=opt.n_d, n_a=opt.n_a, n_steps=opt.n_steps,
        gamma=opt.gamma, n_independent=opt.n_independent, n_shared=opt.n_shared,
        cat_idxs=cat_idxs,
        cat_dims=cat_dims,
        cat_emb_dim=opt.cat_emb_dim,
        lambda_sparse=opt.lambda_sparse, momentum=opt.momentum, clip_value=opt.clip_value,
        optimizer_fn=torch.optim.Adam,
        optimizer_params=dict(lr=opt.lr),
        scheduler_params = {"gamma": opt.gamma,
                         "step_size": opt.step_size},
        scheduler_fn=torch.optim.lr_scheduler.StepLR, epsilon=1e-15
    )
else:
    clf = TabNetClassifier(
        n_d=opt.n_d, n_a=opt.n_a, n_steps=opt.n_steps,
        gamma=opt.gamma, n_independent=opt.n_independent, n_shared=opt.n_shared,
        cat_idxs=cat_idxs,
        cat_dims=cat_dims,
        cat_emb_dim=opt.cat_emb_dim,
        lambda_sparse=opt.lambda_sparse, momentum=opt.momentum, clip_value=opt.clip_value,
        optimizer_fn=torch.optim.Adam,
        optimizer_params=dict(lr=opt.lr),
        scheduler_params = {"gamma": opt.gamma_step,
                         "step_size": opt.step_size},
        scheduler_fn=torch.optim.lr_scheduler.StepLR, epsilon=1e-15
    )

    if opt.task == 'binary':
        eval_metric = ['accuracy', 'auc']
    else:
        eval_metric = ['accuracy']

clf.fit(
    X_train=X_train, y_train=y_train,
    eval_set=[(X_train, y_train),(X_test, y_test),(X_valid, y_valid)],
    eval_name=['train', 'test', 'valid'],
    eval_metric=eval_metric,
    max_epochs=opt.max_epochs , patience=opt.patience,
    batch_size=opt.batch_size, virtual_batch_size=opt.virtual_batch_size,
)

preds = clf.predict_proba(X_test)
if opt.task == 'binary':
    test_auc = roc_auc_score(y_score=preds[:,1], y_true=y_test)

    print(f"FINAL TEST SCORE FOR {opt.dset_id} : {test_auc}")
elif opt.task =='regression':
    y_true = y_test
    test_score = mean_squared_error(y_pred=preds, y_true=y_true)
    print(f"FINAL TEST SCORE FOR {opt.dset_id} : {test_score}")
else:
    preds = clf.predict_proba(X_train)
    y_pred = np.vectorize(preds_mapper.get)(np.argmax(preds, axis=1))

    test_acc = accuracy_score(y_pred=y_pred, y_true=y_train)

    print(f"FINAL TEST SCORE FOR {opt.dset_id} : {test_acc}")


from sys import stdin
from collections import deque

def bfs(s):
    queue = deque()
    queue.append(s)
    visited[s] = 1
    while queue:
        c = queue.popleft()
        for i in info[c]:
            if not visited[i]:
                queue.append(i)
                visited[i] = -visited[c]
            elif visited[i] == visited[c]:
                return True
    return False

k = int(stdin.readline())
for _ in range(k):
    v, e = map(int, stdin.readline().split())
    info = [[] for _ in range(v+1)]
    visited = [0]*(v+1)
    ans = 'YES'
    for _ in range(e):
        a, b = map(int, stdin.readline().split())
        info[a].append(b)
        info[b].append(a)
    for i in range(1,v+1):
        if not visited[i]:
            if bfs(i):
                ans = 'NO'
                break
    print(ans)


from django.contrib.auth.tokens import default_token_generator
from django.core.mail import send_mail
from django.shortcuts import get_object_or_404
from rest_framework import filters, status, viewsets
from rest_framework.decorators import action, api_view, permission_classes
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
from rest_framework_simplejwt.tokens import RefreshToken

from api_yamdb.settings import ADMIN_EMAIL
from reviews.models import Category, Genre, Review, Title, User

from .filters import TitleFilter
from .permissions import (IsAuthorOrReadOnly, IsRoleAdmin, IsRoleModerator,
                          ReadOnly)
from .serializers import (AdminUserSerializer, CategorySerializer,
                          CommentSerializer, GenreSerializer, ReviewSerializer,
                          SignupSerializer, TitleCreateSerializer,
                          TitleSerializer, TokenSerializer, UserSerializer)

class TitleViewSet(viewsets.ModelViewSet):
    """
    Admin can manage titles, other can only read

    titles/ - get all titles
    titles/{id}/ - get title with id
    titles/?gerne,category,year,name - filter titles
    """
    queryset = Title.objects.all()
    serializer_class = TitleSerializer
    permission_classes = (IsRoleAdmin | ReadOnly,)
    filterset_class = TitleFilter

    def get_serializer_class(self):
        if self.request.method in ('POST', 'PATCH',):
            return TitleCreateSerializer
        return TitleSerializer

class ReviewViewSet(viewsets.ModelViewSet):
    """
    Admin, Moderator can manage reviews
    User can manage self reviews

    /titles/{title_id}/reviews/ - get all reviews on title
    /titles/{title_id}/reviews/{id}/ - get title with id
    """
    serializer_class = ReviewSerializer
    permission_classes = (
        IsRoleAdmin | IsRoleModerator | IsAuthorOrReadOnly,
    )

    def get_queryset(self):
        title_id = self.kwargs.get('title_id')
        title = get_object_or_404(Title, id=title_id)
        return title.reviews.all()

    def perform_create(self, serializer):
        title_id = self.kwargs.get('title_id')
        title = get_object_or_404(Title, id=title_id)
        serializer.save(author=self.request.user, title=title)

class CommentViewSet(viewsets.ModelViewSet):
    """
    Admin, Moderator can manage comments
    User can manage self comments

    /titles/{title_id}/reviews/{review_id}/comments/
    get all comments and review with id
    /titles/{title_id}/reviews/{review_id}/comments/{id}/
    git comment with id
    """
    serializer_class = CommentSerializer
    permission_classes = (
        IsRoleAdmin | IsRoleModerator | IsAuthorOrReadOnly,
    )

    def get_queryset(self):
        review_id = self.kwargs.get('review_id')
        review = get_object_or_404(Review, id=review_id)
        return review.comments.all()

    def perform_create(self, serializer):
        review_id = self.kwargs.get('review_id')
        review = get_object_or_404(Review, id=review_id)
        serializer.save(author=self.request.user, review=review)

class GenreViewSet(viewsets.ModelViewSet):
    """
    Admin can manage genres, other can only read

    /genres/ - get all genres
    /genres/{id}/ - get genre with id
    /genres/{slug}/ - delete genre with slug
    /genres/?search=name - search genre with name
    """
    queryset = Genre.objects.all()
    serializer_class = GenreSerializer
    permission_classes = (IsRoleAdmin | ReadOnly,)
    filter_backends = (filters.SearchFilter,)
    search_fields = ('name',)

    @action(
        detail=False, methods=['delete'],
        url_path=r'(?P<slug>\w+)',
        lookup_field='slug', url_name='category_slug'
    )
    def get_genre(self, request, slug):
        category = self.get_object()
        serializer = CategorySerializer(category)
        category.delete()
        return Response(serializer.data, status=status.HTTP_204_NO_CONTENT)

class CategoryViewSet(viewsets.ModelViewSet):
    """
    Admin can manage genres, other can only read

    /categories/ - get all categories
    /categories/{id}/ - get category with id
    /categories/{slug}/ - delete category with slug
    /genres/?search=name - search category with name
    """
    queryset = Category.objects.all()
    serializer_class = CategorySerializer
    permission_classes = (IsRoleAdmin | ReadOnly,)
    filter_backends = (filters.SearchFilter,)
    search_fields = ('name',)

    @action(
        detail=False, methods=['delete'],
        url_path=r'(?P<slug>\w+)',
        lookup_field='slug', url_name='category_slug'
    )
    def get_category(self, request, slug):
        category = self.get_object()
        serializer = CategorySerializer(category)
        category.delete()
        return Response(serializer.data, status=status.HTTP_204_NO_CONTENT)

class UserViewSet(viewsets.ModelViewSet):
    """
    Access have only Admin
    /users/ - get all users
    /users/{username}/ - manage user with username
    /users/?search=username - search user with username

    Allow user namege self, excludes role
    /users/me/ - show current user
    """
    queryset = User.objects.all()
    serializer_class = AdminUserSerializer
    permission_classes = (IsRoleAdmin,)
    filter_backends = (filters.SearchFilter,)
    lookup_field = 'username'
    lookup_value_regex = r'[\w\@\.\+\-]+'
    search_fields = ('username',)

    @action(
        detail=False, methods=['get', 'patch'],
        url_path='me', url_name='me',
        permission_classes=(IsAuthenticated,)
    )
    def about_me(self, request):
        serializer = UserSerializer(request.user)
        if request.method == 'PATCH':
            serializer = UserSerializer(
                request.user, data=request.data, partial=True
            )
            serializer.is_valid(raise_exception=True)
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.data, status=status.HTTP_200_OK)

@api_view(['POST'])
@permission_classes([AllowAny])
def signup(request):
    """
    Create user with unique username and email
    then send confirmation code to email
    """
    serializer = SignupSerializer(data=request.data)
    if serializer.is_valid():
        user = serializer.save()
        send_confirmation_code(user)
        return Response(serializer.data, status=status.HTTP_200_OK)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['POST'])
@permission_classes([AllowAny])
def token(request):
    serializer = TokenSerializer(data=request.data)
    if not serializer.is_valid():
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    username = serializer.data['username']
    user = get_object_or_404(User, username=username)
    confirmation_code = serializer.data['confirmation_code']
    if not default_token_generator.check_token(user, confirmation_code):
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    token = RefreshToken.for_user(user)
    return Response(
        {'token': str(token.access_token)}, status=status.HTTP_200_OK
    )

@api_view(['POST'])
@permission_classes([AllowAny])
def code(request):
    """
    if lost the confirmation code send username and email.
    And u got code again
    """
    serializer = UserSerializer(data=request.data)
    if serializer.is_valid():
        username = serializer.data['username']
        email = serializer.data['email']
        user = get_object_or_404(User, username=username, email=email)
        send_confirmation_code(user)
        return Response(serializer.data, status=status.HTTP_200_OK)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def send_confirmation_code(user):
    """
    Send confirmation code on email
    """
    confirmation_code = default_token_generator.make_token(user)
    subject = 'Код подтверждения YaMDb'
    message = f'{confirmation_code} - ваш код для авторизации на YaMDb'
    admin_email = ADMIN_EMAIL
    user_email = [user.email]
    return send_mail(subject, message, admin_email, user_email)


import os
import logging
from logging.config import dictConfig
from datetime import datetime

def configure_logger(name):
    log_path = (
        os.getcwd() + "/log/" + datetime.now().strftime("sr_log_%H_%M_%d_%m_%Y.log")
    )
    logging.config.dictConfig(
        {
            "version": 1,
            "formatters": {
                "default": {
                    "format": "%(asctime)s - %(levelname)s %(filename)s:%(lineno)s - %(message)s",
                    "datefmt": "%Y-%m-%d %H:%M:%S",
                }
            },
            "handlers": {
                "console": {
                    "level": "DEBUG",
                    "class": "logging.StreamHandler",
                    "formatter": "default",
                    "stream": "ext://sys.stdout",
                },
                "file": {
                    "level": "DEBUG",
                    "class": "logging.handlers.RotatingFileHandler",
                    "formatter": "default",
                    "filename": log_path,
                    "maxBytes": 15 * 1024 * 1024,
                    "backupCount": 3,
                },
            },
            "loggers": {"default": {"level": "DEBUG", "handlers": ["console", "file"]}},
            "disable_existing_loggers": False,
        }
    )
    return logging.getLogger(name)


from function1 import calculatePrice1
from function2 import calculatePrice2

def calculatePrice3(options):
    if options['drink_type'] != 'hot':
        raise ValueError("Chocolate sauce can only be added to hot drinks.")

    base_price = calculatePrice1(options)

    num_pumps = int(input("How many pumps of chocolate sauce would you like? (max 6): "))
    if num_pumps < 0 or num_pumps > 6:
        raise ValueError("Invalid number of pumps selected. Please choose a value between 0 and 6.")

    # First 2 pumps are free
    extra_pumps = num_pumps - 2
    if extra_pumps > 0:
        # $0.5 for each extra pump
        base_price += 0.5 * extra_pumps

    return base_price

options = {
    'drink_type': 'hot',
    'size': input("Select size (S, M, or L): "),
    'whipped_cream': input("Select whipped cream topping (with or without): "),
    'availability_L_size': (input("Is L size available for this drink type? (y/n): ").lower() == 'y'),
}

try:
    price = calculatePrice3(options)
    print("Price: $", price)
except ValueError as e:
    print("Error:", str(e))


import asyncio
from typing import List

import pytest

# write the summary of xfailed tests at the end of the test session
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_terminal_summary(terminalreporter):
    yield

    xfailed: List[pytest.TestReport] = terminalreporter.stats.get("xfailed", [])
    if xfailed:
        terminalreporter.write_sep("=", "XFAIL summary info", cyan=True, bold=True)

    for rep in xfailed:
        if not rep.failed:
            reason = getattr(rep, "wasxfail", "")
            terminalreporter.write("XFAIL", yellow=True)
            terminalreporter.write(f" {rep.nodeid} - {reason}\n")

            rep.longrepr.toterminal(terminalreporter._tw)
            terminalreporter.line("")

# write the arguments of the test after the test name
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_protocol(item, nextitem):  # pylint: disable=unused-argument
    yield

    terminalreporter = item.config.pluginmanager.get_plugin("terminalreporter")
    if terminalreporter is not None and hasattr(item, "callspec"):
        callspec = item.callspec
        if callspec:
            params = {f"{name}={value}" for name, value in callspec.params.items()}
            terminalreporter.write(f"\nArguments: {', '.join(params)}")

# cannot put in boilerplate because pytest is a mess
@pytest.fixture(scope="session")
def event_loop():
    loop = asyncio.get_event_loop()
    yield loop

    # clean any remaining task to avoid the warning about pending tasks
    tasks = asyncio.all_tasks(loop)
    for task in tasks:
        # print(f"Cancelling task {task}")
        task.cancel()

    loop.close()


#encoding: utf-8
#!/usr/bin/env python3.5
#import smbus
from fnmatch import fnmatch
from operations.mqtt import MosquittoServer, Mosquitto
import config #configurações dos controladores rgb
import linecache
import time
import threading

import signal
import serial
import serial.tools.list_ports
global serial_data
import json
from omxplayer.player import OMXPlayer
from instance.config import Config
#from operations.telegram import Bot
from pathlib import Path
from time import sleep
import logging
import subprocess
import datetime
import os
import filecmp
import sys
import termios, tty
TOPICO = "capela/crato"
from check_internet import have_internet
volume = 1
root = os.path.dirname(os.path.abspath(__file__))
pattern = "*.py"
inicio_chuva_petalas = 218
acende_scenario = 365
inicio_pulsa_cruz = 340
fim_pulsa_cruz = 350
inicio_sequencia = inicio_chuva_petalas - 5
inicio_fumaca = inicio_chuva_petalas - 5 - 5
for path, subdirs, files in os.walk(root):
	for name in files:
		if fnmatch(name, pattern):
			if not ".debris" in path:
				try:
					#os.system("rm " + os.path.join(path, name+"_")) #remover todos
					if not(filecmp.cmp (os.path.join(path, name) ,os.path.join(path, "."+name+"_"))):
						os.system("sudo cp " + os.path.join(path, name)+" "+os.path.join(path, "." + name + "_"))

				except Exception as e:
					os.system("sudo cp " + os.path.join(path, name)+" "+os.path.join(path, "." + name + "_"))
					print("Novo arquivo criado: ",os.path.join(path, name))

def printException():
	exc_type, exc_obj, tb = sys.exc_info()
	f = tb.tb_frame
	lineno = tb.tb_lineno
	filename = f.f_code.co_filename
	linecache.checkcache(filename)
	line = linecache.getline(filename, lineno, f.f_globals)
	print ('EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj))

def buttonPressed(status):
	'''ATRIBUI A CERIMONIA AO BOTAO PRESSIONADO NO PAINEL'''
	global CONFIGURACAO
	global videoDuration

	if status == 1:
		CONFIGURACAO = config.CERIMONIA_1
		try:
			player.load(config.video_1,  pause=False)
			time.sleep(1)
			player.pause()
			player.set_aspect_mode('stretch')

			#player.set_video_pos(200, 0, 680, 480)
			print(player.video_pos())
		except Exception as ex:
			print(ex)

	if status == 2:
		CONFIGURACAO = config.CERIMONIA_2
		print(CONFIGURACAO)
		try:
			player.load(config.video_2,  pause=False)
			time.sleep(1)
			player.pause()
			player.set_aspect_mode('stretch')
		except Exception as ex:
			print(ex)

	if status == 3:
		CONFIGURACAO = config.CERIMONIA_3
		print(CONFIGURACAO)
		try:
			player.load(config.video_3,  pause=False)
			time.sleep(1)
			player.pause()
			player.set_aspect_mode('stretch')
		except Exception as ex:
			print(ex)

	if status == 4:
		CONFIGURACAO = config.CERIMONIA_4
		print(CONFIGURACAO)
		try:
			player.load(config.video_4,  pause=False)
			time.sleep(1)
			#player.pause()
			player.set_aspect_mode('stretch')
		except Exception as ex:
			print(ex)

	if status == 5:
		CONFIGURACAO = config.CERIMONIA_5
		print(CONFIGURACAO)
		try:
			player.load(config.video_5,  pause=False)
			time.sleep(1)
			player.pause()
			player.set_aspect_mode('stretch')
		except Exception as ex:
			print(ex)

	videoDuration = player.duration()
	#~ while player.playback_status() != "Paused":
		#~ player.pause()
		#~ time.sleep(.01)
	time.sleep(.1)
	#~ tempo = time.time()
	player.set_volume(volume)
	print("Volume " + str(player.volume()))
	print("O vídeo %d tem %.2f segundos de duração "%(status, player.duration()))

def process_serial(dados):
	if dados != "":
		global CONFIGURACAO
		print("Dados serial " +str(dados))
		global tempo
		global intervalo
		global lastIntervalo

		if dados == 'GUARDAMESA':
			print("player position", player.position())
			pass

		if dados == 'ABRINDO A PORTA':
			# ~ print("Player position abre", player.position())
			pass

		if dados == 'FECHA PORTA':
			'''ACENDE A CAPELA APÓS O TEMPO config.ACENDE_SCENARIO'''
			print("Player position inicio fechamento", player.position())
			pass

		if dados == 'PORTA FECHADA':
			'''ACENDE A CAPELA APÓS O TEMPO config.ACENDE_SCENARIO'''
			print("Player position final fechamento", player.position())
			pass

		if dados == 'TIME':
			print(tempo - time.time())
			tempo = time.time()
			pass
		if dados == 'APAGA_CABINE':
			#~ J2=Job('change', "SANCA_CABINE", ' -C gradual 90 "(0,0,0)"')
			#~ J2.start()

			pass
		if dados != 'ONLINE':
			pass

		if dados == 'button_counter = 2':
			serial_mega.write(b'GPIO,42,1\n')

		if dados == 'ESTEIRA DESLIGADA':
			serial_mega.write(b'LED_ESTEIRA,0\n')

		if dados == 'CERIMONIA_COMPLETA':
			# ~ try:
				# ~ telegram_bot.envia_telegram_single("334240998", "CERIMONIA INICIADA")
			# ~ except:
				# ~ print("ERRO TELEGRAM")
			global iniciada
			iniciada = True
			passedScenario1 = False
			passedScenario2 = False
			serial_mega.write(b'COMPLETA\n')
			serial_mega.write(b'<0,0,0,0,0,8,0,0,0,0>\n')
			time.sleep(.1)

			cmd = {"display_off":"display_off"}
			cmd = json.dumps(cmd)
			mqttLocal.mqttc.publish("capela/ihm", cmd, qos=2)
			serial_mega.write(b'B_C\n')
			# ~ for a in range(22):
				# ~ print("aguardando", 22 - a, "segundos", end = "\r")
				# ~ time.sleep(1)

			#time.sleep(25) #delay da cortina

			#
			player.set_position(1.)

			# ~ player.set_position(200)
			# ~ player.seek(280)
			player.play()
			#player.load(source, pause=False)

class Liga_manual (threading.Thread):
##  '''VERIFICAR SE A THREAD RODA SEM O PCF8574 INSTALADO'''
	def __init__(self):
		threading.Thread.__init__(self)
		# The shutdown_flag is a threading.Event object that
		# indicates whether the thread should be terminated.
		self.shutdown_flag = threading.Event()

	def run(self):
		serial_mega.write(b'DIMER,1,100\n')
		time.sleep(2)
		serial_mega.write(b'DIMER,2,100\n')
		time.sleep(2)
		serial_mega.write(b'DIMER,3,100\n')
		time.sleep(2)
		serial_mega.write(b'DIMER,4,100\n')
		time.sleep(2)
		serial_mega.write(b'DIMER,5,100\n')
		time.sleep(2)
		serial_mega.write(b'DIMER,6,100\n')
		time.sleep(2)
		serial_mega.write(b'DIMER,8,100\n')
		time.sleep(2)
		serial_mega.write(b'DIMER,9,100\n')
		time.sleep(2)
		serial_mega.write(b'DIMER,10,100\n')
		time.sleep(2)
		serial_mega.write(b'GPIO,42,0\n')
		time.sleep(10)
		serial_mega.write(b'T_DIM,50\n')

class readSerial(threading.Thread):
##  '''VERIFICAR SE A THREAD RODA SEM O PCF8574 INSTALADO'''
	def __init__(self):
		threading.Thread.__init__(self)
		# The shutdown_flag is a threading.Event object that
		# indicates whether the thread should be terminated.
		self.shutdown_flag = threading.Event()

	def run(self):
			#liga_todos()
			while not self.shutdown_flag.is_set():
				try:
					global serial_mega
					if serial_mega.isOpen():
						reading = serial_mega.readline().decode("utf-8").rstrip()
						process_serial(reading)

				except:
					pass

class ServiceExit(Exception):
	"""
	Custom exception which is used to trigger the clean exit
	of all running threads and the main program.
	"""
	pass

def service_shutdown(signum, frame):
	print('Caught signal %d' % signum)
	raise ServiceExit

def main():
	global iniciada
	global videoDuration
	global intervalo
	global lastIntervalo
	global CONFIGURACAO
	global passedScenario1
	global passedScenario2
	# Register the signal handlers
	signal.signal(signal.SIGTERM, service_shutdown)
	signal.signal(signal.SIGINT, service_shutdown)

	print('Starting main program')

	# Start the job threads
	buttonPressed(1)
	try:

		pass
		time_video = player.position()
		ler_serial = readSerial()
		ler_serial.start()

		looping = time.time()
		lastLooping = time.time()
		time_cortinas = time.time()

		while True:
			if mqttLocal.desligar == True:
				time.sleep(2)
				os.system("sudo poweroff")

			if player.playback_status() == "Playing" and looping < time.time():
				time_video = player.position()
				looping = time.time() + .5

				if time_video >= inicio_fumaca and time_video <= inicio_fumaca + 1:
					serial_mega.write(b'fum,12000\n')

				if time_video >= inicio_chuva_petalas and time_video <= inicio_chuva_petalas +1:
					print("chuva_petalas")
					serial_mega.write(b'CHUVAPETALAS,128\n')
					serial_mega.write(b'LED_ESTEIRA,255\n')
					#serial_mega.write(b'<0,0,0,0,0,100,0,0,0,100>\n')

				if time_video >= inicio_sequencia and time_video <= inicio_sequencia + 1:
					print(" sequencia")
					serial_mega.write(b'sequencia\n')
					time.sleep(1)
					serial_mega.write(b'LED_CABINE,255,255,0\n')

				if time_video >= inicio_pulsa_cruz and time_video <= inicio_pulsa_cruz + 1:
					print("pulsa cruz")
					serial_mega.write(b'EN_RGB_GERAL,1\n')# habilita o loop pulsante vermelho
					time.sleep(1)
					serial_mega.write(b'[100,0,0,100,0,0,5,0,0,5,0,0]\n')

				if time_video >= fim_pulsa_cruz and time_video <= fim_pulsa_cruz + 1:
					print(" desabilita pulsa cruz")
					serial_mega.write(b'EN_RGB_GERAL,0\n')
					time.sleep(1)
					serial_mega.write(b'(0,0,0,0,0,0,0,0,0)\n')

				if time_video >= acende_scenario and time_video <= acende_scenario + 1:
					print(" fim de cerimonia")
					serial_mega.write(b'S_C\n')
					serial_mega.write(b'T_DIM,100\n')
					# ~ serial_mega.write(b'<100,100,100,100,100,100,100,100,100,100>\n')#luz no fim da cerimonia
					time.sleep(1)
					serial_mega.write(b'(255,255,255,0,0,0,255,255,255)\n')
					iluminacao_manual = Liga_manual()
					iluminacao_manual.start()

				print(time_video, end = '\r')

			if player.can_control() and time_video > videoDuration - 2:
				while player.playback_status() != "Paused":
					print("pausando video")
					player.pause()

				#videoDuration = player.duration()
				time.sleep(.5)

				iniciada = False

	except ServiceExit:
		pass
		# Terminate the running threads.
		# Set the shutdown flag on each thread to trigger a clean shutdown of each thread.
##        j1.shutdown_flag.set()

		# Wait for the threads to close...
##        j1.join()

	print('Exiting main program')

try:
	os.system("sudo killall omxplayer.bin")
except:
	pass

logging.basicConfig(level=logging.INFO)

source = "/usr/share/APP/videos/video_1.mp4"
try:
	player_log = logging.getLogger("Player 1")
	player = OMXPlayer(source, args=[  '-o', 'local', '--no-osd','-b'], dbus_name='org.mpris.MediaPlayer2.omxplayer1')
	player.set_aspect_mode('stretch')
except:# NameError:# dbus.exceptions.DBusException:
	print("Reiniciando App")
	os.execv(sys.executable, ['python3'] + sys.argv)
else:
	print("Exception")

#~ player.set_aspect_mode('fill')

#time.sleep(20)
while player.playback_status() != "Paused":
	player.pause()
tempo = time.time()
print("video pausado")
player.set_volume(volume)
#player.mute()
print("Player mute")
print("Volume " + str(player.volume()))
print("O vídeo tem %.2f segundos de duração "%player.duration())

serial_mega = None
serial_nano = None

def config_serial():
	global serial_mega
	global serial_nano
	print("function")
	lista_porta=[]
	portas_abertas=[]
	ports = serial.tools.list_ports.comports()

	for port, desc, hwid in sorted(ports):
	        if "USB" in port:
	            lista_porta.append(port)

	for portas in lista_porta:
		'''
		Abre todas as portas
		'''
		try:
			portas_abertas.append(serial.Serial(portas, 9600, timeout=1))
		except:
			print(portas, "Acesso negado")

	try:

		while serial_mega == None or serial_nano == None:
			for channel in portas_abertas:
				if channel != serial_mega and channel != serial_nano:
					channel.write(b'MODEL\n')
					time.sleep(.1)
					string = channel.readline().decode("utf-8", "ignore")
					if serial_nano == None:
						if "NANO" in string:
							#print("Porta NANO", lista_porta[portas_abertas.index(channel)])
							serial_nano = channel
					if serial_mega == None:
						if "MEGA" in string:
							#print("Porta MEGA", lista_porta[portas_abertas.index(channel)])
							serial_mega = channel
		print("Porta serial MEGA = {}\nPorta serial NANO = {}".format(lista_porta[portas_abertas.index(serial_mega)], lista_porta[portas_abertas.index(serial_nano)]))
		mqttServer.serial_nano = serial_nano
		mqttServer.serial_mega = serial_mega
		mqttLocal.serial_nano = serial_nano
		mqttLocal.serial_mega = serial_mega
	except Exception as e:
		print(e)
		print("Porta Serial não aberta")

thread = threading.Thread(target=config_serial)
thread.start()
mqttLocal = Mosquitto(TOPICO)
#mqttLocal.player = player
mqttServer = MosquittoServer(TOPICO)
#mqttServer.player = player

if __name__ == '__main__':
	main()

# ~ Traceback (most recent call last):
  # ~ File "video_popen_4_comando_rasp.py", line 430, in <module>
    # ~ main()
  # ~ File "video_popen_4_comando_rasp.py", line 353, in main
    # ~ if player.can_control() and time_video > videoDuration - 2:
  # ~ File "</usr/local/lib/python3.7/dist-packages/decorator.py:decorator-gen-22>", line 2, in can_control
# ~ Dados serial B_C
  # ~ File "/usr/local/lib/python3.7/dist-packages/omxplayer/player.py", line 48, in wrapped
    # ~ return fn(self, *args, **kwargs)
  # ~ File "</usr/local/lib/python3.7/dist-packages/decorator.py:decorator-gen-21>", line 2, in can_control
  # ~ File "/usr/local/lib/python3.7/dist-packages/omxplayer/player.py", line 85, in wrapped
    # ~ return from_dbus_type(fn(self, *args, **kwargs))
  # ~ File "/usr/local/lib/python3.7/dist-packages/omxplayer/player.py", line 342, in can_control
    # ~ return self._player_interface_property('CanControl')
  # ~ File "/usr/local/lib/python3.7/dist-packages/omxplayer/player.py", line 829, in _player_interface_property
    # ~ return self._interface_property(self._player_interface.dbus_interface, prop, val)
  # ~ File "/usr/local/lib/python3.7/dist-packages/omxplayer/player.py", line 823, in _interface_property
    # ~ return self._properties_interface.Get(interface, prop)
  # ~ File "/usr/local/lib/python3.7/dist-packages/dbus/proxies.py", line 147, in __call__
    # ~ **keywords)
  # ~ File "/usr/local/lib/python3.7/dist-packages/dbus/connection.py", line 653, in call_blocking
    # ~ message, timeout)
# ~ dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NoReply: Message recipient disconnected from message bus without replying
# ~ INFO:omxplayer.player:OMXPlayer process is dead, all DBus calls from here will fail

#Player position inicio fechamento 291.791414
#Player position final fechamento 332.850824


from os import system
from math import factorial
import math

class VALORES (object):
    resultado = ""
    valor = 0.0
    k = 0.0

def menu():
    system("cls")
    print("Bienvenido al laboratorio 2")
    print("Integrantes: Wilson Caxaj y Nicol Macario")
    print("opciones:")
    print("1) Poisson")
    print("2) Binomial")
    print("3) Normal")
    seleccion = int(input("Seleccion: "))
    if seleccion == 1:
        poisson()

    if seleccion == 2:
        binomial()

    if seleccion == 3:
        normal()

def poisson():
    resultadoslist = []
    system("cls")
    print("POISSON")
    print("a continuación deberas proporcionar los siguientes datos")
    E = float(input("e: "))
    U = float(input("U: "))
    K = float(input("K: "))
    resultadofloat = ((U**K)*(E**(U*-1)))/factorial(K)
    resultadoString = "P(K = "+str(K)+") = "+str(resultadofloat)

    valor =  VALORES()
    valor.valor = resultadofloat
    valor.resultado = resultadoString
    valor.k = K

    resultadoslist.append(valor)

    print(resultadoString)
    seleccion = 1
    while seleccion == 1:
        print("¿deseas agregar otro dato K?")
        print("1) SI")
        print("2) NO")
        seleccion = int(input("continuar: "))
        if seleccion == 1:
            K = float(input("K: "))
            resultadofloat = ((U**K)*E**(U*-1))/factorial(K)
            resultadoString = "P(K = "+str(K)+") = "+str(resultadofloat)

            valor =  VALORES()
            valor.valor = resultadofloat
            valor.resultado = resultadoString
            valor.k = K

            resultadoslist.append(valor)

            print(resultadoString)

        if seleccion == 2:
            print("---Resultados---")
            porct = 0
            for x in range(len(resultadoslist)):
                porct+= resultadoslist[x].valor
                print(resultadoslist[x].resultado+" = "+"{:^10.8f}".format(resultadoslist[x].valor*100)+" %")
            print("en porcentaje: "+str("{:^10.8f}".format(float(porct*100)))+" %")
            print("1) SI\n2)NO, SALIR")
            seleccionmenu = int(input("quieres volver al menu?: "))
            if seleccionmenu == 1:
                menu()

def binomial():
    n = int(input("Digite número de ensayos: "))
    k = int(input("Digite número de exitos: "))
    p = float(input("Digite número probabilidad de exitos: "))

    q=1-p
    res=(factorial(n))/(factorial(k)*(factorial(n-k)))
    res2=pow(p,k)*(pow(q,(n-k)))
    resultado= (res*res2)*100
    print("El resultado es: " +(format(resultado,'.3f')+ "%"))
    print("1) SI\n2)NO, SALIR")
    seleccionmenu = int(input("¿quieres volver al menu?: "))
    if seleccionmenu == 1:
        menu()

def normal():
    system("cls")
    print("NORMAL")
    print("a continuación deberas proporcionar los siguientes datos")
    X = float(input("X: "))
    U = float(input("U: "))
    O = float(input("O: "))
    resultadofloat = (X-U)/O
    resultadoString = "Z = "+str(resultadofloat)

    print(resultadoString)
    print("1) SI\n2)NO, SALIR")
    seleccionmenu = int(input("¿quieres volver al menu?: "))
    if seleccionmenu == 1:
        menu()

menu()