import json
import sys
from pprint import pprint

def main(args):
    try:
        with open(args[0]) as f:
            result = json.load(f)
            if "summary" in result and "failure" in result["summary"]:
                failures = result["summary"]["failure"]
                if failures == 0:
                    print("App Inspect Passed!")
                    if "warning" in result["summary"] and result["summary"]["warning"]:
                        print("Warning List:")
                        for group in result["reports"][0]["groups"]:
                            for check in group["checks"]:
                                if check["result"]=="warning":
                                    for msg in check["messages"]:
                                        print(msg["message"])
                    pprint(result["summary"])
                    print("::set-output name=status::pass")
                else:
                    print(f"App Inspect returned {failures} failures.")
                    print("::set-output name=status::fail")
                    pprint(result["summary"])
                    print("Failure List:")
                    for group in result["reports"][0]["groups"]:
                        for check in group["checks"]:
                            if check["result"]=="failure":
                                for msg in check["messages"]:
                                    print(msg["message"])
                    sys.exit(1)
            else:
                print("Unexpected JSON format")
                print("::set-output name=status::fail")
                sys.exit(1)
    except Exception as e:
        print(f"An error occurred {str(e)}")
        sys.exit(1)

if __name__ == "__main__":
    print(sys.argv[1:])
    main(sys.argv[1:])


import random
import signal
import dns.resolver as dr
import os
from dnslib.server import DNSServer
import logging
from dnslib import *
import dnslib
from dnslib.proxy import ProxyResolver
from time import sleep
from scapy.all import DNS,DNSQR,DNSRR,dnsqtypes
from dnslib import DNSLabel, QTYPE, RR, dns

handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
handler.setFormatter(logging.Formatter('%(asctime)s: %(message)s', datefmt='%H:%M:%S'))

logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

TYPE_LOOKUP = {
    'A': (dns.A, QTYPE.A),
    'AAAA': (dns.AAAA, QTYPE.AAAA),
    'CAA': (dns.CAA, QTYPE.CAA),
    'CNAME': (dns.CNAME, QTYPE.CNAME),
    'DNSKEY': (dns.DNSKEY, QTYPE.DNSKEY),
    'MX': (dns.MX, QTYPE.MX),
    'NAPTR': (dns.NAPTR, QTYPE.NAPTR),
    'NS': (dns.NS, QTYPE.NS),
    'PTR': (dns.PTR, QTYPE.PTR),
    'RRSIG': (dns.RRSIG, QTYPE.RRSIG),
    'SOA': (dns.SOA, QTYPE.SOA),
    'SRV': (dns.SRV, QTYPE.SRV),
    'TXT': (dns.TXT, QTYPE.TXT),
    'SPF': (dns.TXT, QTYPE.TXT),
}

class Record:
    def __init__(self, rname, rtype, args):
        self._rname = DNSLabel(rname)

        rd_cls, self._rtype = TYPE_LOOKUP[rtype]

        if self._rtype == QTYPE.SOA and len(args) == 2:
            # add sensible times to SOA
            args += (SERIAL_NO, 3600, 3600 * 3, 3600 * 24, 3600),

        if self._rtype == QTYPE.TXT and len(args) == 1 and isinstance(args[0], str) and len(args[0]) > 255:
            # wrap long TXT records as per dnslib's docs.
            args = wrap(args[0], 255),

        if self._rtype in (QTYPE.NS, QTYPE.SOA):
            ttl = 3600 * 24
        else:
            ttl = 300

        self.rr = RR(
            rname=self._rname,
            rtype=self._rtype,
            rdata=rd_cls(str(*args)),
            ttl=ttl,
        )

class Resolver(ProxyResolver):

    def __init__(self):
        super().__init__(53, 5)

    def read_user_request(self,request, path):
        my_resolver = dr.Resolver()
        dict = {}
        maxvotes = 0
        final_answer = ""
        F = open(path, 'r')

        ips = F.readlines()  #go trough all the dns ips and  saving them in a list
        totalIOTnum = len(ips)
        IOTnum2ask = max(int(totalIOTnum*0.01),7)
        Xcount = 0
        #sending request to rendem dns servers
        i=1
        while i<=IOTnum2ask and Xcount<totalIOTnum:
            random_index = random.randint(0,totalIOTnum-1)

        if ips[random_index]!= "X" : # so we woun't get the same ip twice
                i=i+1
                my_resolver.nameservers = [ips[random_index].strip()] #defing the dns to be a random ip from the list of ips
                print("ask " + ips[random_index].strip() + " which answers ")
                ips[random_index] = "X" # so we woun't get the same ip twice
                Xcount=Xcount+1
                try:
                   answer = my_resolver.query(request) #making a request to the dns above to get the ip of the "request"
                except:
                   answer = "no result"
                   i = i - 1

                if answer!="no result":
                    for rdata in answer:
                        print(rdata)
                        if not rdata in dict: #if this ip doesn't exists in the dict add it with count one
                            dict[rdata] = 1
                        else: dict[rdata] = dict[rdata]+1  #if it does exists count++
                        # updating the max count value and the most common answer
                        if maxvotes < dict[rdata]:
                            maxvotes = dict[rdata]
                            final_answer = rdata

                        for key,val in dict.items():
                            print(key , "=>" , val)
                        print("\n")

                else:
                    print(answer+"\n")

        if final_answer == '':
           final_answer = '204.74.99.100'
        return final_answer

    def resolve(self, request, handler):
        path = '/home/rita/Downloads/ips.txt'
        rname = str(request.questions[0].get_qname())[:-1]
        args = self.read_user_request(rname, path)
        args = (args,)

        #assert d.opencode == 0,d.opencode
        #assert dnsqtypes[d[DNSQR].qtype] == 'A',d[DNSQR].qtype
        rtype='A'
        record = Record(rname, rtype, args)
        reply = request.reply()
        reply.add_answer(record.rr)

        #type_name = QTYPE[request.q.qtype]
        #reply = request.reply()
        #reply.add_answer(answer)

        if reply.rr:
            #logger.info('found zone for %s[%s], %d replies', request.q.qname, type_name, len(reply.rr))
            return reply

        return super().resolve(request, handler)

def handle_sig(signum, frame):
    logger.info('pid=%d, got signal: %s, stopping...', os.getpid(), signal.Signals(signum).name)
    exit(0)

if __name__ == "__main__":

    signal.signal(signal.SIGTERM, handle_sig)
    port = int(os.getenv('PORT', 53))
    resolver = Resolver()
    udp_server = DNSServer(resolver, port=port)
    udp_server.start_thread()

    try:
        while udp_server.isAlive():
            sleep(1)
    except KeyboardInterrupt:
        pass


from sklearn import tree
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
iris=load_iris()

x = iris.data
y = iris.target

xTrain, xTest, yTrain, yTest = train_test_split(x, y, test_size = 1/3, random_state = 0)

clf = tree.DecisionTreeClassifier()
clf.fit(xTest, yTest)

def Iris(flower):
		if flower==0:
			print("setosa")
		elif flower==1:
			print("versicolor")
		else:
			print("virginica")

clf = tree.DecisionTreeClassifier()
clf.fit(xTrain, yTrain)

d1 = float(input("Enter sepal length : "))
d2 = float(input("Enter sepal width : "))
d3 = float(input("Enter petal length : "))
d4 = float(input("Enter petal width : "))

data = [[d1, d2, d3, d4]]

num = clf.predict(data)

Iris(num)

print (clf.score(xTest,yTest))

#6.3,3.3,4.7,1.6


from selenium import webdriver
from time import sleep
from random import randint

DRIVER_PATH = '/Users/hainguyen/Documents/Python/ds_salary_project/chromedriver'
driver = webdriver.Chrome(executable_path = DRIVER_PATH)

driver.get('https://au.indeed.com')

search_job = driver.find_element_by_xpath('//input[@id="text-input-what"]')
search_job.send_keys(['data science'])

search_location = driver.find_element_by_xpath('//*[@id="text-input-where"]')
search_location.send_keys(['australia'])

initial_search_button = driver.find_element_by_xpath('//*[@id="whatWhereFormId"]/div[3]/button')
initial_search_button.click()

driver.implicitly_wait(3)

titles=[]
companies=[]
locations=[]
links =[]
reviews=[]
salaries = []
descriptions=[]

for i in range(0,145):

    job_card = driver.find_elements_by_xpath('//div[contains(@class,"clickcard")]')

    for job in job_card:

        try:
            title  = job.find_element_by_xpath('.//h2[@class="title"]//a').text
        except:
            title = job.find_element_by_xpath('.//h2[@class="title"]//a').get_attribute(name="title")
        titles.append(title)

        try:
            company = job.find_element_by_xpath('.//span[@class="company"]').text
        except:
            company = "None"
        companies.append(company)

        try:
            location = job.find_element_by_xpath('.//span[contains(@class,"location")]').text
        except:
            location = "None"
        locations.append(location)

        try:
            link = job.find_element_by_xpath('.//h2[@class="title"]//a').get_attribute(name="href")
        except:
            link = "None"
        links.append(link)

        try:
            review = job.find_element_by_xpath('.//span[@class="ratingsContent"]').text
        except:
            review = "None"
        reviews.append(review)

        try:
            salary = job.find_element_by_xpath('.//span[@class="salaryText"]').text
        except:
            salary = "None"
        salaries.append(salary)

    try:
        next_page = driver.find_element_by_xpath('//a[@aria-label={}]//span[@class="pn"]'.format(i+2))
        next_page.click()

    except:
        next_page = driver.find_element_by_xpath('//a[@aria-label="Next"]//span[@class="np"]')
        next_page.click()

    print("Page: {}".format(str(i+2)))
    sleep(randint(1, 5))

import pandas as pd
df=pd.DataFrame()
df['Title']=titles
df['Company']=companies
df['Location']=locations
df['Link']=links
df['Review']=reviews
df['Salary']=salaries

df = df.drop_duplicates()

links = df['Link']
for link in links:
    driver.get(link)
    description = driver.find_element_by_xpath('//div[@id="jobDescriptionText"]').text
    descriptions.append(description)
    sleep(randint(1, 5))

df['Description']=descriptions

df.to_csv(r'indeed_ds_jobs.csv', index=False, header=True)


from typing import AsyncIterable, AsyncIterator, Iterable, TypeVar, Union

__all__ = ["async_chain", "async_chain_iterator"]

_T = TypeVar("_T")

async def async_chain(*iterables: Union[Iterable[_T], AsyncIterable[_T]]) -> AsyncIterator[_T]:
    for iterable in iterables:
        if isinstance(iterable, AsyncIterable):
            async for v in iterable:
                yield v
        else:
            for v in iterable:
                yield v

async def async_chain_iterator(iterator: AsyncIterator[Union[Iterable[_T], AsyncIterable[_T]]]) -> AsyncIterator[_T]:
    async for e in iterator:
        async for v in async_chain(e):
            yield v


from PySide2 import QtWidgets, QtCore, QtGui
import maya.cmds as cmds
import pymel.core as pm
import maya.mel as mel

from master_rigger import Splitter
from master_rigger import sanityChecker  # rename this later
from maya_tools.contexts.undoContext import UndoContext
reload(sanityChecker)

class GlobalToolWidget(QtWidgets.QFrame):

    def __init__(self):
        QtWidgets.QFrame.__init__(self)

        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)

        self.setLayout(QtWidgets.QVBoxLayout())
        self.layout().setContentsMargins(1, 1, 1, 1)
        self.layout().setSpacing(0)
        self.layout().setAlignment(QtCore.Qt.AlignTop)

        global_widget = QtWidgets.QWidget()
        global_widget.setLayout(QtWidgets.QVBoxLayout())
        global_widget.layout().setContentsMargins(2, 2, 2, 2)
        global_widget.layout().setSpacing(5)
        global_widget.setSizePolicy(QtWidgets.QSizePolicy.Minimum,
                                    QtWidgets.QSizePolicy.Fixed)

        publish_widget = QtWidgets.QWidget()
        publish_widget.setLayout(QtWidgets.QVBoxLayout())
        publish_widget.layout().setContentsMargins(2, 2, 2, 2)
        publish_widget.layout().setSpacing(5)
        publish_widget.setSizePolicy(QtWidgets.QSizePolicy.Minimum,
                                     QtWidgets.QSizePolicy.Fixed)

        self.layout().addWidget(global_widget)
        self.layout().addWidget(Splitter.Splitter('Rig Mode'))
        self.layout().addWidget(publish_widget)

        # Layouts
        button_layout = QtWidgets.QHBoxLayout()
        publish_layout = QtWidgets.QHBoxLayout()

        # Add layouts to widget
        global_widget.layout().addLayout(button_layout)
        publish_widget.layout().addLayout(publish_layout)

        # Buttons
        self.flip_selection_button = QtWidgets.QPushButton('Flip Selection')
        self.add_iso_button = QtWidgets.QPushButton('Add to Isolate View')
        self.remove_iso_button = QtWidgets.QPushButton('Remove from Isolate View')

        button_layout.addWidget(self.flip_selection_button)
        button_layout.addWidget(self.add_iso_button)
        button_layout.addWidget(self.remove_iso_button)

        self.publish_mode_button = QtWidgets.QPushButton('Publish Mode')
        self.edit_mode_button = QtWidgets.QPushButton('Edit Mode')

        publish_layout.addWidget(self.publish_mode_button)
        publish_layout.addWidget(self.edit_mode_button)

        self.flip_selection_button.clicked.connect(self.flip_selection)
        self.add_iso_button.clicked.connect(self.add_to_isolate)
        self.remove_iso_button.clicked.connect(self.remove_from_isolate)

        self.publish_mode_button.clicked.connect(self.publish_mode)
        self.edit_mode_button.clicked.connect(self.edit_mode)

    def flip_selection(self):
        sel = cmds.ls(selection=True, recursive=True)
        flip = []

        namespace = None
        if ':' in sel[0]:
            sel = [obj.split(':')[1] for obj in sel]
            namespace = obj.split(':')[0]

        for item in sel:
            if '_L_' in item:
                flip.append(item.replace('_L_', '_R_'))
            elif '_R_' in item:
                flip.append(item.replace('_R_', '_L_'))
            elif item.startswith('R_'):
                flip.append('L_' + item[2:])
            elif item.startswith('L_'):
                flip.append('R_' + item[2:])
            elif item.endswith('_R'):
                flip.append(item[:-2] + '_L')
            elif item.endswith('_L'):
                flip.append(item[:-2] + '_R')
            else:
                cmds.warning('Nothing done.')

        if namespace:
            flip = [':'.join([namespace, item]) for item in flip]

        cmds.select(flip, replace=True)

    def add_to_isolate(self):
        mel.eval('isolateSelect -addSelected modelPanel4;')

    def remove_from_isolate(self):
        mel.eval('isolateSelect -removeSelected modelPanel4;')

    def publish_mode(self):
        with UndoContext():
            sanityChecker.publishMode()

    def edit_mode(self):
        with UndoContext():
            sanityChecker.editMode()


import numpy as np
import matplotlib.pyplot as plt

def animate():

    m, n = len(problems), no_snapshots

    fig, axes = plt.subplots(
        m, n, figsize=(16, 4.5*m), squeeze=False, sharey='row', sharex=True
    )
    for i in range(m):
        for j in range(n):
            ax = axes[i, j]
        axes[i, 0].legend()

for i in range(m):
    row = axes[i]
    for j in range(len(methods)):
        ts, x, Us, sol = ex07(N, CFL, method=methods[j], equation=problems[i])
        snapshot_ts = np.linspace(ts[0], ts[-1], no_snapshots)
        for k in range(n):
            ax = row[k]
            t = snapshot_ts[k]
            if j == 0:
                ax.plot(x, sol(x, t), label=f'$u(x)$', linestyle=':')
            ax.plot(x, u_t(t, ts, Us), label=f'$u_{{h,{methods[j]}}}(x)$')
            ax.set(title=f't={t:.2f}')
    row[0].legend()

plt.show()


_requests = {
    {
        'name': 'pan for pancakes',
        'condition': 'clean, non-stick',
        'time': '2 weeks',
        'benefits': '10 pancakes'
    },
    {
        'name': 'hair dryer',
        'condition': 'three speeds, cold and hot airflow',
        'time': 'until 25 november',
        'benefits': '2 sweets milka'
    },
    {
        'name': 'laptop',
        'condition': '--',
        'time': '2 weeks',
        'benefits': '10 pancakes',
    },
    {
        'name': 'nude shoes, heels ',
        'condition': 'new, 38size',
        'time': '27 nov',
        'benefits': '500 rub',
    },
    {
        'name': 'shoe dryer',
        'condition': 'clean',
        'time': '1 week',
        'benefits': 'lays and orange juice',
    },
    {
        'name': 'mirror',
        'condition': 'around 1 meters, clean',
        'time': '3 weeks',
        'benefits': '700rub',
    },
    {
        'name': 'Bakeware',
        'condition': 'cute',
        'time': '1 week',
        'benefits': '5 cakes',
    },
    {
        'name': 'charging on iphone',
        'condition': '--',
        'time': 'until 22 nov',
        'benefits': '100rub and chocolate',
    },
    {
        'name': 'outlet extension',
        'condition': '--',
        'time': 'until friday',
        'benefits': 'chocolate',
    },
    {
        'name': 'fash drive',
        'condition': 'free 1gb',
        'time': '1 weeks',
        'benefits': '200rub'
    }
}

_request_list = []

for name, request_data in _requests.items():
    _new_element = {'name': name}
    _new_element.update(request_data)
    _request_list.append(_new_element)

# Get request filtered by name
def get_requests_by_name(q):
    results = []
    # SEARCH

    for request in _request_list:
       if request['name'].lower() .find(q.lower()) >=0:
            results.append(request)

    return results

def get_request(itemname):
    return _requests.get(itemname)


"""
Django视图函数库。
"""
import os
import json
import urllib
from functools import wraps
import requests
from django.http import HttpResponse
from django.http import JsonResponse
from django.http import Http404
from django.http import FileResponse
from django.conf import settings
from django.urls import reverse
from django.urls import NoReverseMatch
from zencore import errors
from zencore.utils.magic import call_with_parameters_injection
from zencore.utils.magic import get_parameter_defaults
from zencore.utils.compress import InMemoryZipFile

def json_response(function):
    """
    将数据内容转化为Django视图响应。
    """
    @wraps(function)
    def handler(*args, **kwargs):
        """
        真实处理函数。
        """
        result = function(*args, **kwargs)
        return JsonResponse(result, safe=False)
    return handler

def catch_exception(function):
    """
    捕获所有异常，并转化成错误信息。
    """
    @wraps(function)
    def handler(*args, **kwargs):
        """
        真实处理函数。
        """
        try:
            result = function(*args, **kwargs)
        except errors.BaseError as error:
            result = {
                "error": error.to_object(),
            }
        except Exception as error:
            result = {
                "error": {
                    "code": errors.UNKNOWN_EXCEPTION,
                    "message": "Unknown exception: {}".format(str(error)),
                }
            }
        return result
    return handler

def aceept_methods(*methods):
    """
    设定可处理的请求类型。
    """
    methods = [method.upper() for method in methods]
    def wrapper(function):
        """
        包装函数。
        """
        @wraps(function)
        def handler(request, *args, **kwargs):
            """
            真实处理函数。
            """
            if not request.method in methods:
                raise errors.HttpMethodNotAllowedError(request.method, methods)
            return function(request, *args, **kwargs)
        return handler
    return wrapper

def payload_json_decode(function):
    """
    以json形式解压request.body内容，并保存在request.PAYLOAD中。
    """
    @wraps(function)
    def handler(request, *args, **kwargs):
        """
        真实函数。
        """
        body = request.read()
        try:
            payload = json.loads(body)
        except Exception as error:
            raise errors.PayloadDecodeError("json", body, error)
        setattr(request, "PAYLOAD", payload)
        return function(request, *args, **kwargs)
    return handler

def call_with_payload_injection(function):
    """
    从PAYLOAD中提取参数进行函数注入。
    """
    @wraps(function)
    def handler(request, *args, **kwargs):
        """
        真实处理函数。
        """
        return call_with_parameters_injection(
            function,
            [request.PAYLOAD, kwargs, {"request": request}],
            args
            )
    return handler

def call_with_post_injection(function):
    """
    从POST中提取参数进入函数注入。
    """
    @wraps(function)
    def handler(request, *args, **kwargs):
        """
        真实处理函数。
        """
        return call_with_parameters_injection(
            function,
            [request.POST, kwargs, {"request": request}],
            args
            )
    return handler

def call_with_get_injection(function):
    """
    从GET中提取参数进入函数注入。
    """
    @wraps(function)
    def handler(request, *args, **kwargs):
        """
        真实处理函数。
        """
        return call_with_parameters_injection(
            function,
            [request.GET, kwargs, {"request": request}],
            args
            )
    return handler

def cast(name, transform):
    """
    参数类型转化。
    """
    def wrapper(function):
        """
        包装函数。
        """
        @wraps(function)
        def handler(**kwargs):
            """
            真实处理函数。
            """
            if name in kwargs:
                kwargs[name] = transform(kwargs[name])
            return function(**kwargs)
        return handler
    return wrapper

def payload_service(function):
    """
    利用PAYLOAD进行接口服务。
    """
    @json_response
    @catch_exception
    @payload_json_decode
    @call_with_payload_injection
    @wraps(function)
    def handler(*args, **kwargs):
        """
        真实处理函数。
        """
        return function(*args, **kwargs)
    return handler

def post_service(function):
    """
    利用POST进行接口服务。
    """
    @json_response
    @catch_exception
    @call_with_post_injection
    @wraps(function)
    def handler(*args, **kwargs):
        """
        真实处理函数。
        """
        return function(*args, **kwargs)
    return handler

def get_service(function):
    """
    利用GET进行接口服务。
    """
    @json_response
    @catch_exception
    @call_with_get_injection
    @wraps(function)
    def handler(*args, **kwargs):
        """
        真实处理函数。
        """
        return function(*args, **kwargs)
    return handler

def ignore_null_parameters(function):
    """
    当处理函数有参数默认值时，而传递的参数为空时，则忽略传递的空值，而使用参数默认值。
    """
    @wraps(function)
    def handler(*args, **kwargs):
        """
        真实处理函数。
        """
        defaults = get_parameter_defaults(function)
        keys = list(kwargs.keys())
        for key in keys:
            if (kwargs[key] is None) and (key in defaults):
                del kwargs[key]
        return function(*args, **kwargs)
    return handler

def get_request_headers(request):
    """
    获取request的headers数据。
    """
    headers = {}
    if "CONTENT_TYPE" in request.META:
        headers["Content-Type"] = request.META.get("CONTENT_TYPE")
    if "CONTENT_LENGTH" in request.META:
        headers["Content-Length"] = str(request.META.get("CONTENT_LENGTH"))
    for key, value in request.META.items():
        if key.startswith("HTTP_"):
            key = "-".join([k.capitalize() for k in key[5:].split("_")])
            headers[key] = value
    return headers

def static_files_view(request, path, root):
    """
    静态资源文件夹服务视图。option keys: root.
    """
    root = os.path.abspath(root)
    file = os.path.abspath(os.path.join(root, path))
    if not file.startswith(root):
        raise Http404()
    if not os.path.isfile(file):
        raise Http404()
    return FileResponse(open(file, "rb"))

def static_file_view(request, file):
    """
    静态文件视图。option keys: file.
    """
    if not os.path.isfile(file):
        raise Http404()
    return FileResponse(open(file, "rb"))

def proxy_pass_view(request, path, target):
    """
    反向代理视图。option keys: target.

    japro配置:
        urls:
          - name: callback.site
            regex: ^callback/(.*)$
            view: zencore.views.proxy_pass_view
            options:
              target: http://callback.biz.site/

    django urls配置:
        url(
            "^callback/(.*)$",
            proxy_pass_view,
            {"target": "http://callback.biz.site/"},
            name="callback.site"
            ),

    """
    parameters = dict(request.GET)
    headers = get_request_headers(request)
    response = requests.request(
        request.method,
        urllib.parse.urljoin(target, path),
        params=parameters,
        data=request.read(),
        headers=headers,
        )
    result = HttpResponse(
        content=response.content,
        content_type=response.headers.get("Content-Type"),
        status=response.status_code,
        reason=response.reason,
        charset=response.encoding,
        )
    for key, value in response.headers.items():
        result.setdefault(key, value)
    return result

def zipfile_view(function):
    """
    生成zip下载文件。

    要求处理函数返回：
        {
            "name": "example.zip",
            "files": [
                {
                    "filename": "a.txt",
                    "content": b"hello world",
                },{
                    "filename": "b.txt",
                    "filepath": "/tmp/b.txt",
                }
            ]
        }
    """
    def make_zip_file(files):
        """
        获取zip文件字节流。
        """
        zfile = InMemoryZipFile()
        for file in files:
            filename = file["filename"]
            if "content" in file:
                zfile.append(filename, file["content"])
            elif "filepath" in file:
                with open(file["filepath"], "rb") as fobj:
                    zfile.append(filename, fobj.read())
        return zfile.getvalue()
    @wraps(function)
    def handler(*args, **kwargs):
        """
        真实处理函数。
        """
        info = function(*args, **kwargs)
        name = info["name"]
        files = info["files"]
        data = make_zip_file(files)
        response = HttpResponse(data, content_type="application/force-download")
        response["Content-Disposition"] = "attachment; filename={name}".format(name=name)
        response["Content-Length"] = len(data)
        return response
    return handler

def get_site_home_link(app_label=None):
    """获取首页/APP首页的链接。

    获取优先次序
    ----------

    1. APP_XXX_INDEX_LINK

    2. APP_INDEX_LINK

    3. reverse("site.index")

    4. reverse("index")

    5. /
    """
    names = []
    if app_label:
        names.append("APP_{}_INDEX_LINK".format(app_label))
    names += [
        "APP_INDEX_LINK",
    ]
    for name in names:
        if hasattr(settings, name):
            link =  getattr(settings, name)
            if link.startswith("link:"):
                return link[5:]
            else:
                return reverse(link[5:])
    names = [
        "site.index",
        "index",
    ]
    for name in names:
        try:
            return reverse(name)
        except NoReverseMatch:
            pass
    return "/"

def empty(requests):
    """
    返回一个空白页面。
    """
    return HttpResponse("")


import numpy as np
import pandas as pd
import os
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

from sklearn.metrics import accuracy_score
data = pd.read_csv('data.csv')
data = pd.DataFrame(data)
# Check is NULL
data = data.fillna(0)
data.isnull().sum()

y = data['SalePrice']
data = data.drop(columns=['SalePrice'], axis=1)
print(y.shape)
# # Check type
data.dtypes
for col in data.columns:
    if(data[col].dtypes=='object'):
        data = pd.concat([data.drop(columns=[col], axis=1), pd.get_dummies(data[col], prefix=col)], axis=1)
print(data.shape)
X = data
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)

lr = LinearRegression()
lr.fit(X_train, y_train)

print("MSE of Validation: ", mean_squared_error(y_val, lr.predict(X_val)))


import torch
import torch.nn as nn
from ..losses import build_loss
from ..utils import (Anchors, BBoxTransform,
                     iou_cpu, bias_init_with_prob,
                     kaiming_init, normal_init)

# from models.losses.debug import FocalLoss

class ClassificationModel(nn.Module):
    def __init__(self, num_features_in, num_anchors=9, num_classes=80, feature_size=256):
        super(ClassificationModel, self).__init__()

        self.num_classes = num_classes
        self.num_anchors = num_anchors

        self.conv1 = nn.Conv2d(num_features_in, feature_size, kernel_size=3, padding=1)
        self.act1 = nn.ReLU()

        self.conv2 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1)
        self.act2 = nn.ReLU()

        self.conv3 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1)
        self.act3 = nn.ReLU()

        self.conv4 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1)
        self.act4 = nn.ReLU()

        self.output = nn.Conv2d(feature_size, num_anchors*num_classes, kernel_size=3, padding=1)

        self.init_weights()

    def init_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                normal_init(m, std=0.01)
        bias_cls = bias_init_with_prob(0.01)
        normal_init(self.output, std=0.01, bias=bias_cls)

    def forward(self, x):

        out = self.conv1(x)
        out = self.act1(out)

        out = self.conv2(out)
        out = self.act2(out)

        out = self.conv3(out)
        out = self.act3(out)

        out = self.conv4(out)
        out = self.act4(out)

        out = self.output(out)

        out1 = out.permute(0, 2, 3, 1)

        batch_size, height, width, channels = out1.shape

        out2 = out1.view(batch_size, height, width, self.num_anchors, self.num_classes)

        return out2.contiguous().view(x.shape[0], -1, self.num_classes)

class RegressionModel(nn.Module):
    def __init__(self, num_features_in, num_anchors=9, feature_size=256):
        super(RegressionModel, self).__init__()

        self.conv1 = nn.Conv2d(num_features_in, feature_size, kernel_size=3, padding=1)
        self.act1 = nn.ReLU()

        self.conv2 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1)
        self.act2 = nn.ReLU()

        self.conv3 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1)
        self.act3 = nn.ReLU()

        self.conv4 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1)
        self.act4 = nn.ReLU()

        self.output = nn.Conv2d(feature_size, num_anchors*4, kernel_size=3, padding=1)

        self.init_weights()

    def init_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                normal_init(m, std=0.01)

    def forward(self, x):

        out = self.conv1(x)
        out = self.act1(out)

        out = self.conv2(out)
        out = self.act2(out)

        out = self.conv3(out)
        out = self.act3(out)

        out = self.conv4(out)
        out = self.act4(out)

        out = self.output(out)

        out = out.permute(0, 2, 3, 1)

        return out.contiguous().view(out.shape[0], -1, 4)

class RetinaHead(nn.Module):
    def __init__(self,
                 strides=[8, 16, 32, 64, 128],
                 loss_cls=dict(
                    type='FocalLoss',
                    use_sigmoid=True,
                    gamma=2.0,
                    alpha=0.25,
                    loss_weight=1.0),
                 loss_bbox=dict(type='IoULoss', loss_weight=1.0),
                 in_channels=256,
                 feat_channels=256,
                 nclass=80):
        super(RetinaHead, self).__init__()
        self.nclass = nclass
        self.anchors = Anchors(strides=strides)
        self.nanchor = len(self.anchors)
        self.regressBoxes = BBoxTransform()

        self.retina_cls = ClassificationModel(num_features_in=256,
                                              num_anchors=self.nanchor,
                                              num_classes=self.nclass)
        self.retina_reg = RegressionModel(num_features_in=256, num_anchors=9)
        self.loss_cls = build_loss(loss_cls)
        self.loss_bbox = build_loss(loss_bbox)

    def forward(self, inputs, annotations=None):
        device = inputs[0].device
        num_imgs = inputs[0].size(0)
        tensor_zero = torch.tensor(0).float().to(device)
        featmap_sizes = [featmap.size()[-2:] for featmap in inputs]
        anchors, resticts = self.anchors(
            featmap_sizes, dtype=inputs[0].dtype, device=device)

        pred_cls = torch.cat(
            [self.retina_cls(featmap) for featmap in inputs], dim=1)
        pred_reg = torch.cat(
            [self.retina_reg(featmap) for featmap in inputs], dim=1)

        if self.training:
            # return self.loss(pred_cls, pred_reg, anchors, annotations)
            loss_cls, loss_reg = [], []
            annotations = annotations.type_as(pred_reg)
            for bi in range(num_imgs):
                annotation = annotations[bi]
                annotation = annotation[annotation[:, 4] != -1]
                if annotation.shape[0] == 0:
                    loss_cls.append(tensor_zero)
                    loss_reg.append(tensor_zero)
                    continue

                target_cls, target_bbox, pst_idx = self._encode(anchors,
                                                                annotation,
                                                                resticts[0])
                if pst_idx.sum() == 0:
                    loss_cls.append(tensor_zero)
                    loss_reg.append(tensor_zero)
                    continue

                loss_cls_bi = self.loss_cls(pred_cls[bi],
                                            target_cls,
                                            avg_factor=pst_idx.sum())
                loss_reg_bi = self.loss_bbox(pred_reg[bi, pst_idx],
                                             target_bbox,
                                             anchors[pst_idx])
                loss_cls.append(loss_cls_bi)
                loss_reg.append(loss_reg_bi)

            return dict(
                loss_cls=torch.stack(loss_cls).mean(),
                loss_reg=torch.stack(loss_reg).mean()
            )

        else:
            transformed_anchors = self.regressBoxes(
                anchors, pred_reg)

            scores, class_id = torch.max(
                pred_cls.sigmoid(), dim=2, keepdim=True)

            return scores.squeeze(2), class_id.squeeze(2), transformed_anchors

    def _encode(self, anchors, annotation, resticts):
        device = anchors.device
        targets = torch.ones(anchors.shape[0], self.nclass) * -1
        targets = targets.to(device)

        # num_anchors x num_annotations
        IoU = iou_cpu(anchors, annotation[:, :4])
        IoU_max, IoU_argmax = torch.max(IoU, dim=1)  # num_anchors x 1

        assigned_annotations = annotation[IoU_argmax, :]
        # agd_ann_wh = assigned_annotations[:, 2:4] - assigned_annotations[:, :2]
        # scale_indices = (agd_ann_wh.max(dim=1)[0] > resticts[:, 0]) \
        #     & (agd_ann_wh.max(dim=1)[0] < resticts[:, 1])

        positive_indices = torch.ge(IoU_max, 0.5)
        # print(positive_indices.sum())
        targets[torch.lt(IoU_max, 0.4), :] = 0
        targets[positive_indices, :] = 0
        targets[positive_indices,
                assigned_annotations[positive_indices, 4].long()] = 1

        return targets, \
            assigned_annotations[positive_indices, :4], \
            positive_indices


# Copyright 2019 Yelp Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import socket
import struct
import time
from collections import defaultdict
from typing import Callable
from typing import Dict
from typing import Mapping
from typing import Optional
from typing import Tuple

import arrow
import colorlog
import simplejson as json
import staticconf
from clusterman_metrics import APP_METRICS
from clusterman_metrics import ClustermanMetricsBotoClient
from clusterman_metrics import MetricsValuesDict
from clusterman_metrics import SYSTEM_METRICS
from mypy_extensions import TypedDict
from retry import retry
from simplejson.errors import JSONDecodeError
from staticconf.errors import ConfigurationError

from clusterman.config import POOL_NAMESPACE
from clusterman.exceptions import ClustermanSignalError
from clusterman.exceptions import MetricsError
from clusterman.exceptions import NoSignalConfiguredException
from clusterman.exceptions import SignalConnectionError
from clusterman.exceptions import SignalValidationError
from clusterman.util import get_cluster_dimensions

logger = colorlog.getLogger(__name__)

ACK = bytes([1])
DEFAULT_SIGNALS_BUCKET = 'yelp-clusterman-signals'
SOCKET_MESG_SIZE = 4096
SOCKET_TIMEOUT_SECONDS = 300
SIGNAL_LOGGERS: Mapping[
    str,
    Tuple[
        Callable[[str], None],
        Callable[[str], None],
    ]
] = {}
SignalResponseDict = Dict[str, Optional[float]]

class MetricsConfigDict(TypedDict):
    name: str
    type: str
    minute_range: int
    regex: bool

class Signal:
    def __init__(
        self,
        cluster: str,
        pool: str,
        scheduler: str,
        app: str,
        config_namespace: str,
        metrics_client: ClustermanMetricsBotoClient,
        signal_namespace: str,
    ) -> None:
        """ Create an encapsulation of the Unix sockets via which we communicate with signals

        :param cluster: the name of the cluster this signal is for
        :param pool: the name of the pool this signal is for
        :param app: the name of the application this signal is for
        :param config_namespace: the staticconf namespace we can find the signal config in
        :param metrics_client: the metrics client to use to populate signal metrics
        :param signal_namespace: the namespace in the signals repo to find the signal class
            (if this is None, we default to the app name)
        """
        reader = staticconf.NamespaceReaders(config_namespace)

        try:
            self.name: str = reader.read_string('autoscale_signal.name')
        except ConfigurationError:
            raise NoSignalConfiguredException(f'No signal was configured in {config_namespace}')

        self.cluster: str = cluster
        self.pool: str = pool
        self.scheduler: str = scheduler
        self.app: str = app

        self.period_minutes: int = reader.read_int('autoscale_signal.period_minutes')
        if self.period_minutes <= 0:
            raise SignalValidationError(f'Length of signal period must be positive, got {self.period_minutes}')

        self.parameters: Dict = {
            key: value
            for param_dict in reader.read_list('autoscale_signal.parameters', default=[])
            for (key, value) in param_dict.items()
        }
        # Even if cluster and pool were set in parameters, we override them here
        # as we want to preserve a single source of truth
        self.parameters.update(dict(
            cluster=self.cluster,
            pool=self.pool,
        ))

        self.required_metrics: list = reader.read_list('autoscale_signal.required_metrics', default=[])

        self.metrics_client: ClustermanMetricsBotoClient = metrics_client
        self.signal_namespace = signal_namespace
        self._signal_conn: socket.socket = self._connect_to_signal_process()

    def evaluate(self, timestamp: arrow.Arrow, retry_on_broken_pipe: bool = True) -> SignalResponseDict:
        """ Communicate over a Unix socket with the signal to evaluate its result

        :param timestamp: a Unix timestamp to pass to the signal as the "current time"
        :param retry_on_broken_pipe: if the signal socket pipe is broken, restart the signal process and try again
        :returns: a dict of resource_name -> requested resources from the signal
        :raises SignalConnectionError: if the signal connection fails for some reason
        """
        # Get the required metrics for the signal
        metrics = self._get_metrics(timestamp)

        try:
            # First send the length of the metrics data
            metric_bytes = json.dumps({'metrics': metrics, 'timestamp': timestamp.timestamp}).encode()
            len_metrics = struct.pack('>I', len(metric_bytes))  # bytes representation of the length, packed big-endian
            self._signal_conn.send(len_metrics)
            response = self._signal_conn.recv(SOCKET_MESG_SIZE)
            if response != ACK:
                raise SignalConnectionError(f'Error occurred sending metric length to signal (response={response})')

            # Then send the actual metrics data, broken up into chunks
            for i in range(0, len(metric_bytes), SOCKET_MESG_SIZE):
                self._signal_conn.send(metric_bytes[i:i + SOCKET_MESG_SIZE])
            response = self._signal_conn.recv(SOCKET_MESG_SIZE)
            ack_bit = response[:1]
            if ack_bit != ACK:
                raise SignalConnectionError(f'Error occurred sending metric data to signal (response={response})')

            # Sometimes the signal sends the ack and the reponse "too quickly" so when we call
            # recv above it gets both values.  This should handle that case, or call recv again
            # if there's no more data in the previous message
            response = response[1:] or self._signal_conn.recv(SOCKET_MESG_SIZE)
            logger.info(response)

            return json.loads(response)['Resources']

        except JSONDecodeError as e:
            raise ClustermanSignalError('Signal evaluation failed') from e
        except BrokenPipeError as e:
            if retry_on_broken_pipe:
                logger.error('Signal connection failed; reloading the signal and trying again')
                time.sleep(5)  # give supervisord some time to restart the signal
                self._signal_conn = self._connect_to_signal_process()
                return self.evaluate(timestamp, retry_on_broken_pipe=False)
            else:
                raise ClustermanSignalError('Signal evaluation failed') from e

    @retry(exceptions=ConnectionRefusedError, tries=3, delay=5)  # retry signal connection in case it's slow to start
    def _connect_to_signal_process(self) -> socket.socket:
        """ Create a connection to the specified signal over a unix socket

        :returns: a socket connection which can read/write data to the specified signal
        """
        # this creates an abstract namespace socket which is auto-cleaned on program exit
        signal_conn = socket.socket(socket.AF_UNIX)
        signal_conn.connect(f'\0{self.signal_namespace}-{self.name}-{self.app}-socket')

        signal_kwargs = json.dumps({'parameters': self.parameters})
        signal_conn.send(signal_kwargs.encode())
        logger.info(f'Connected to signal {self.name} from {self.signal_namespace}')

        return signal_conn

    def _get_metrics(self, end_time: arrow.Arrow) -> MetricsValuesDict:
        """ Get the metrics required for a signal """

        metrics: MetricsValuesDict = defaultdict(list)
        metric_dict: MetricsConfigDict
        for metric_dict in self.required_metrics:
            if metric_dict['type'] not in (SYSTEM_METRICS, APP_METRICS):
                raise MetricsError(f"Metrics of type {metric_dict['type']} cannot be queried by signals.")

            # Need to add the cluster/pool to get the right system metrics
            # TODO (CLUSTERMAN-126) this should probably be cluster/pool/app eventually
            # TODO (CLUSTERMAN-446) if a mesos pool and a k8s pool share the same app_name,
            #      APP_METRICS will be used for both
            if metric_dict['type'] == SYSTEM_METRICS:
                dims_list = [get_cluster_dimensions(self.cluster, self.pool, self.scheduler)]
                if self.scheduler == 'mesos':  # handle old (non-scheduler-aware) metrics
                    dims_list.insert(0, get_cluster_dimensions(self.cluster, self.pool, None))
            else:
                dims_list = [{}]

            # We only support regex expressions for APP_METRICS
            if 'regex' not in metric_dict:
                metric_dict['regex'] = False

            start_time = end_time.shift(minutes=-metric_dict['minute_range'])
            for dims in dims_list:
                query_results = self.metrics_client.get_metric_values(
                    metric_dict['name'],
                    metric_dict['type'],
                    start_time.timestamp,
                    end_time.timestamp,
                    is_regex=metric_dict['regex'],
                    extra_dimensions=dims,
                    app_identifier=self.app,
                )
                for metric_name, ts in query_results.items():
                    metrics[metric_name].extend(ts)
                    # safeguard; the metrics _should_ already be sorted since we inserted the old
                    # (non-scheduler-aware) metrics before the new metrics above, so this should be fast
                    metrics[metric_name].sort()
        return metrics

def setup_signals_environment(pool: str, scheduler: str) -> Tuple[int, int]:
    app_namespace = POOL_NAMESPACE.format(pool=pool, scheduler=scheduler)
    default_signal_version = staticconf.read_string('autoscale_signal.branch_or_tag')
    signal_versions = [default_signal_version]
    signal_namespaces = [staticconf.read_string('autoscaling.default_signal_role')]
    signal_names = [staticconf.read_string('autoscale_signal.name')]
    app_names = ['__default__']

    app_signal_name = staticconf.read_string(
        'autoscale_signal.name',
        namespace=app_namespace,
        default=None,
    )
    if app_signal_name:
        signal_names.append(app_signal_name)
        signal_versions.append(staticconf.read_string(
            'autoscale_signal.branch_or_tag',
            namespace=app_namespace,
            default=pool,
        ))
        signal_namespaces.append(
            staticconf.read_string('autoscale_signal.namespace', namespace=app_namespace, default=pool),
        )
        app_names.append(pool)

    versions_to_fetch = set(signal_versions)
    os.environ['CMAN_VERSIONS_TO_FETCH'] = ' '.join(versions_to_fetch)
    os.environ['CMAN_SIGNAL_VERSIONS'] = ' '.join(signal_versions)
    os.environ['CMAN_SIGNAL_NAMESPACES'] = ' '.join(signal_namespaces)
    os.environ['CMAN_SIGNAL_NAMES'] = ' '.join(signal_names)
    os.environ['CMAN_SIGNAL_APPS'] = ' '.join(app_names)
    os.environ['CMAN_NUM_VERSIONS'] = str(len(versions_to_fetch))
    os.environ['CMAN_NUM_SIGNALS'] = str(len(signal_versions))
    os.environ['CMAN_SIGNALS_BUCKET'] = staticconf.read_string('aws.signals_bucket', default=DEFAULT_SIGNALS_BUCKET)

    return len(versions_to_fetch), len(signal_versions)


# -*- coding: utf-8 -*-
import logging
import lethingaccesssdk
import time
import os
import json

# User need to implement this class
class Temperature_device(lethingaccesssdk.ThingCallback):
  def __init__(self):
    self.temperature = 41

  def callService(self, name, input_value):
    return -1, {}

  def getProperties(self, input_value):
    if input_value[0] == "temperature":
      return 0, {input_value[0]: self.temperature}
    else:
      return -1

  def setProperties(self, input_value):
    if "temperature" in input_value:
      self.temperature = input_value["temperature"]
      return 0, {}

# User define device behavior
def device_behavior(client, app_callback):
  while True:
    time.sleep(2)
    if app_callback.temperature > 40:
      client.reportEvent('high_temperature', {'temperature': app_callback.temperature})
      client.reportProperties({'temperature': app_callback.temperature})

device_obj_dict = {}
try:
  driver_conf = json.loads(os.environ.get("FC_DRIVER_CONFIG"))
  if "deviceList" in driver_conf and len(driver_conf["deviceList"]) > 0:
    device_list_conf = driver_conf["deviceList"]
    config = device_list_conf[0]
    app_callback = Temperature_device()
    client = lethingaccesssdk.ThingAccessClient(config)
    client.registerAndonline(app_callback)
    device_behavior(client, app_callback)
except Exception as e:
  logging.error(e)

#don't remove this function
def handler(event, context):
  logger = logging.getLogger()


import pygame
pygame.init()
import math
import random
import classes
from classes import *

width = 1280
height = 1024

    #Variable Colors

black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
blue = (0,0,255)
yellow = (140, 0,140)

#font setup

pygame.font.init()
myfont = pygame.font.Font('Sprites/Font/Arcade.ttf', 60)

#Screen setup
line = 0

Level = []
value = 0
blockvalue = 0
linenumber = 0
levelname = True

screen = pygame.display.set_mode((width,height))
screen_rect=screen.get_rect()
pygame.display.set_caption('Level reader')
clock = pygame.time.Clock()

background = pygame.image.load('Sprites/Extra/Background.png').convert()

#everything = pygame.sprite.Group()

floor = Floor()
wall = Wall(0) #left wall
wall = Wall(1275) #right wall

with open('Levels.txt', 'r') as lines:
    lines = lines.read()
    lines = [line.rstrip('\n') for line in open('Levels.txt')]

xline = int(len(lines) / 22)

'''class parent(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.xcord = 0
        self.ycord = 0
        self.xspeed = 0 #horizontal speed
        self.yspeed = 0 #vertical speed
        everything.add(self)

class Block(parent):
    def __init__(self, row, colum, xsize, ysize, color):
        super().__init__()
        self.row = row
        self.xsize = xsize
        self.ysize = ysize
        self.colum = colum
        self.image = pygame.Surface([40 * xsize,40 * ysize])
        self.image.fill(green)
        self.rect = self.image.get_rect()

    def update(self):

        self.xcord = self.colum * 40
        self.ycord = self.row * 40

        self.rect.y = self.ycord
        self.rect.x = self.xcord'''

def spawnlevel(Level):
    for y in range(21):
        value = lines[y + 1 + (linenumber * 22)]
        print(value)
        for x in range(32):
            blockvalue = value[x]
            spawnitems(y,x,blockvalue)

def spawnitems(y,x, typ):

    y = y * 40
    x = x * 40

    if typ == '1':                                                              #player
        player = Player(x,y)
    elif typ == '2':                                                            #ladder
        block = Block(y,x,1,1,black)
    elif typ == '3':                                                            #Static platform
        block = Block(y,x,1,1,blue)
    elif typ == '4':                                                            #Breakable platform
        block = Block(y,x,1,1,blue)
    elif typ == '5':                                                            #vertical wall
        block = Block(y,x,1,1,green)
    elif typ == '6':                                                            #moving platform
        block = Block(y,x,1,1,white)
    elif typ == '7':                                                            #small slime
        ball = Ball(4,x,y, True)
    elif typ == '8':                                                            #medium slime
        ball = Ball(2,x,y, True)
    elif typ == '9':                                                            #Big slime
        ball = Ball(1,x,y, True)

while True:

    for event in pygame.event.get(): #handles closing the window
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                print(Level)
                spawnlevel(Level)
                levelname = False
            elif event.key == pygame.K_UP:
                linenumber += 1
            elif event.key == pygame.K_DOWN:
                linenumber -= 1

    if linenumber >= xline:
        linenumber = 0
    elif linenumber < 0:
        linenumber = xline - 1

    if levelname == True:

        name0 = myfont.render( lines[((linenumber - 2) % xline) * 22], False, black)
        name1 = myfont.render( lines[((linenumber - 1) % xline) * 22], False, black)
        name2 = myfont.render( lines[((linenumber - 0) % xline) * 22], False, green)
        name3 = myfont.render( lines[((linenumber + 1) % xline) * 22], False, black)
        name4 = myfont.render( lines[((linenumber + 2) % xline) * 22], False, black)

    print(xline)

    everything.update()

    screen.blit(background,(0,0))

    everything.draw(screen)

    if levelname == True:
        screen.blit(name0, (500, 100))
        screen.blit(name1, (500, 200))
        screen.blit(name2, (500, 300))
        screen.blit(name3, (500, 400))
        screen.blit(name4, (500, 500))

    pygame.display.flip()

    clock.tick(60)


largest = None
smallest = None
while True:
    num = input("Enter an integer number (or 'done' when done): ") # python 3 has a working input() function
    if num == "done" : break
    # attempt to convert to an integer
    try:
        inum = int(num)

    except:
        print ("Invalid input") # print is a proper function in p3
    else: # not sure if this is a style thing or a p3 vs p2 change but for the 5.2 structure, try/except/else is preferred (only the dangerous test itself should be in the try block, all the instructions to be executed after a successful try should be in else)
        if largest is None:
            largest = inum
        if inum > largest :
            largest = inum
        if smallest is None:
            smallest = inum
        if inum < smallest :
            smallest = inum

print ("Maximum is", largest) # print is a proper function in p3
print ("Minimum is", smallest) # print is a proper function in p3


class Solution(object):
    def minSwapsCouples(self, row):
        """
        :type row: List[int]
        :rtype: int
        """
        ret = 0
        place = [0] * len(row)
        for seat, people in enumerate(row):
            place[people] = seat
        for i in range(len(row)):
            x = row[i]
            if x % 2:
                y = x - 1
            else:
                y = x +1
            j = place[y]
            if abs(i-j) > 1:
                row[i+1], row[j] = row[j], row[i+1]
                place[row[i+1]], place[row[j]] = place[row[j]], place[row[i+1]]
                ret += 1
        return ret


# coding:utf-8

'''
Spider 目录是我们爬虫程序爬取网站以及提取信息的模块。我们首先在目录下新建一个名为 VmoiveSpider 的文件。
同时，该类继承scrapy.Spider。

这里我们用到的scrapy.spider.Spider 是 Scrapy 中最简单的内置 spider。
继承 spider 的类需要定义父类中的属性以及实现重要的方法。
'''
import scrapy
from scrapy_demo.items import ScrapyDemoItem

class VmoiveSpider(scrapy.Spider):
	# 用于区别Spider，必须是唯一的,它是 String 类型.
	name = 'vmoive'
	'''
	name
    这个属性是非常重要的，所以必须定义它。定义 name 目的是为爬虫程序命名。
    因此，还要保持 name 属性是唯一的。它是 String 类型，
    我们在 VmoiveSpider 可以定义：
    '''

	allowed_domains = ['vmovier.com']
	'''
	可选字段(可选定义）。包含了spider允许爬取的域名(domain)列表(list)。 当 OffsiteMiddleware 启用时，
	域名不在列表中的URL不会被跟进。根据 V 电影的 url 地址，我们可以这样定义：
	'''

	# 启动时爬取入口的URL列表，后续的URL则从初始的URL的响应中主动提取,
	start_urls = ['https://www.vmovier.com/']
	#start_urls 是 Url 列表，也是必须被定义。可以把它理解为存放爬虫程序的主入口 url 地址的容器。

	def parse(self, response):
		self.log('item page url is ==== ' + response.url)

		moivelist = response.xpath("//li[@class='clearfix']")

		for m in moivelist:
			item = ScrapyDemoItem()
			item['cover'] = m.xpath('./a/img/@src')[0].extract()
			item['title'] = m.xpath('./a/@title')[0].extract()
			item['dec'] = m.xpath("./div/div[@class='index-intro']/a/text()").extract()[0]
			#print(item)
			#yield item
			'''
			添加个[0], 因为 xpath() 返回的结果是列表类型。
			如果不添加，运行结果会返回一个列表，而不是文本信息。
			'''

		    #item['playUrl'] = response.xpath("//*[@id='iframeId']/@src")[0].extract()
		    #item['playUrl'] = 'https://www.vmovier.com/' + response.xpath("//*[@id='post-list']/li/@data-id")[0] + '?from=index_new_img'

		    #为什么要在 [0] 后面添加 extract()方法，这里涉及到内建选择器 Selecter 的知识。
		    # extract()方法的作用是串行化并将匹配到的节点返回一个unicode字符串列表。把html 标签去掉

			# 提取电影详细页面 url 地址
			urlitem = m.xpath('./a/@href')[0].extract()
			url = response.urljoin(urlitem)

			yield scrapy.Request(url, callback=self.parse_moive, meta={
				'cover': item['cover'],
				'title': item['title'],
				'dec': item['dec'],
			})

	def parse_moive(self, response):
		item = ScrapyDemoItem()
		item['cover'] = response.meta['cover']
		item['title'] = response.meta['title']
		item['dec'] = response.meta['dec']
		item['playUrl'] = response.xpath(".//div[@class='p00b204e980']/p/iframe/@src")[0].extract()
		yield item

		    #'''
		    #parse(response)

            #parser 方法是Scrapy处理下载的response的默认方法。它同样必须被实现。
		    #parse 主要负责处理 response 并返回处理的数据以及跟进的URL。
		    #该方法及其他的Request回调函数必须返回一个包含 Request 及(或) Item 的可迭代的对象。
            #'''


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

from webargs.flaskparser import use_kwargs
from flask_restful import Resource
from flask import Blueprint
from raiden.api.v1.encoding import (
    ChannelRequestSchema,
    EventRequestSchema,
    TokenSwapsSchema,
    TransferSchema,
)

def create_blueprint():
    # Take a look at this SO question on hints how to organize versioned
    # API with flask:
    # http://stackoverflow.com/questions/28795561/support-multiple-api-versions-in-flask#28797512
    return Blueprint('v1_resources', __name__)

class BaseResource(Resource):
    def __init__(self, **kwargs):
        super(BaseResource, self).__init__()
        self.rest_api = kwargs['rest_api_object']

class ChannelsResource(BaseResource):

    put_schema = ChannelRequestSchema(
        exclude=('channel_address', 'state'),
    )

    def __init__(self, **kwargs):
        super(ChannelsResource, self).__init__(**kwargs)

    def get(self):
        """
        this translates to 'get all channels the node is connected with'
        """
        return self.rest_api.get_channel_list()

    @use_kwargs(put_schema, locations=('json',))
    def put(self, **kwargs):
        return self.rest_api.open(**kwargs)

class ChannelsResourceByChannelAddress(BaseResource):

    patch_schema = ChannelRequestSchema(
        exclude=('token_address', 'partner_address', 'settle_timeout', 'channel_address'),
    )

    def __init__(self, **kwargs):
        super(ChannelsResourceByChannelAddress, self).__init__(**kwargs)

    @use_kwargs(patch_schema, locations=('json',))
    def patch(self, **kwargs):
        return self.rest_api.patch_channel(**kwargs)

    def get(self, **kwargs):
        return self.rest_api.get_channel(**kwargs)

class TokensResource(BaseResource):

    def __init__(self, **kwargs):
        super(TokensResource, self).__init__(**kwargs)

    def get(self):
        """
        this translates to 'get all token addresses we have channels open for'
        """
        return self.rest_api.get_tokens_list()

class PartnersResourceByTokenAddress(BaseResource):

    def __init__(self, **kwargs):
        super(PartnersResourceByTokenAddress, self).__init__(**kwargs)

    def get(self, **kwargs):
        return self.rest_api.get_partners_by_token(**kwargs)

class NetworkEventsResource(BaseResource):

    get_schema = EventRequestSchema()

    def __init__(self, **kwargs):
        super(NetworkEventsResource, self).__init__(**kwargs)

    @use_kwargs(get_schema, locations=('query',))
    def get(self, **kwargs):
        return self.rest_api.get_network_events(kwargs['from_block'], kwargs['to_block'])

class TokenEventsResource(BaseResource):

    get_schema = EventRequestSchema()

    def __init__(self, **kwargs):
        super(TokenEventsResource, self).__init__(**kwargs)

    @use_kwargs(get_schema, locations=('query',))
    def get(self, **kwargs):
        return self.rest_api.get_token_network_events(
            kwargs['token_address'],
            kwargs['from_block'],
            kwargs['to_block']
        )

class ChannelEventsResource(BaseResource):

    get_schema = EventRequestSchema()

    def __init__(self, **kwargs):
        super(ChannelEventsResource, self).__init__(**kwargs)

    @use_kwargs(get_schema, locations=('query',))
    def get(self, **kwargs):
        return self.rest_api.get_channel_events(
            kwargs['channel_address'],
            kwargs['from_block'],
            kwargs['to_block']
        )

class TokenSwapsResource(BaseResource):

    put_schema = TokenSwapsSchema()

    def __init__(self, **kwargs):
        super(TokenSwapsResource, self).__init__(**kwargs)

    @use_kwargs(put_schema)
    def put(
            self,
            target_address,
            identifier,
            role,
            sending_token,
            sending_amount,
            receiving_token,
            receiving_amount):
        return self.rest_api.token_swap(
            target_address=target_address,
            identifier=identifier,
            role=role,
            sending_token=sending_token,
            sending_amount=sending_amount,
            receiving_token=receiving_token,
            receiving_amount=receiving_amount
        )

class TransferToTargetResource(BaseResource):

    post_schema = TransferSchema(
        exclude=('initiator_address', 'target_address', 'token_address')
    )

    def __init__(self, **kwargs):
        super(TransferToTargetResource, self).__init__(**kwargs)

    @use_kwargs(post_schema, locations=('json',))
    def post(self, token_address, target_address, amount, identifier):
        return self.rest_api.initiate_transfer(
            token_address=token_address,
            target_address=target_address,
            amount=amount,
            identifier=identifier,
        )


class WindowTypes(object):
    QUERY = 'whylog_query_output'
    TEACHER = 'whylog_teacher'
    INPUT = 'whylog_input'
    MESSAGE = 'whylog_message'
    CASE = 'whylog_case'

class WindowSizes(object):
    QUERY_WINDOW = 10

class Messages(object):
    COMMENT_PREFIX = '# %s'
    TEACHER_HEADER = 'You are using whylog teacher.'
    INPUT_HEADER = 'This is whylog input window.'
    ENDING = 'Do not change the commented text above.'
    INPUT_INFO = 'Enter the content below the commented block and press <F3>.'
    CASE_INFO = 'Select an option and press <F3>'
    CONVERTER = 'This is the match of the group: %s'
    REGEX = 'This is the content of the line:'
    LOGTYPE = 'This is the content and path of the line:'
    PRIMARY_KEY = 'This is the content and path of the line:'
    ADDED_EFFECT = '### WHYLOG ### You added line as effect. Select cause and press <F4>.'

class EditorStates(object):
    EDITOR_NORMAL = 'editor normal'
    LOG_READER = 'query'
    TEACHER = 'teacher menu'
    ADD_CAUSE = 'add cause'
    TEACHER_INPUT = 'input window'
    EFFECT_ADDED = 'added effect'

class ActionTypes(object):
    STANDARD = 'standard action'
    TEACHER = 'teacher action'

class ParserOutputs(object):
    GROUP_CONVERTER = 'group %s: %s, match: %s'
    COPY_BUTTON = '[copy_line]'
    DELETE_BUTTON = '[delete_line]'
    LOG_TYPE = 'log type: %s'
    MESSAGE_CONTENT = '=== %s: %s'
    META = 'file: %s, offset: %s'
    OTHERS_HEADER = '--- Other:'
    PRIMARY_KEY = 'primary key groups: %s'
    GUESS_BUTTON = '[guess_regex]'
    REGEX_HEADER = '--- Regex: %s'
    LINE_CONTENT = '--- Line %s: %s'

class ConstraintsOutputs(object):
    ADD_BUTTON = '[add_constraint]'
    DELETE_BUTTON = '[delete_constraint]'
    GROUP = 'line: %s, group: %s'
    HEADER = '=== Rule Constraints:'
    PARAM = '%s: %s'
    PARAMS_HEADER = 'params:'
    TYPE = '--- Constraint: %s'

class Constraint(object):
    ADD_BUTTON = 'add_constraint'
    DELETE_BUTTON = 'delete_constraint'
    GROUP = 'line: %s, group: %s'
    HEADER = '=== Rule Constraints:'
    PARAM = '%s: %s'
    PARAMS_HEADER = 'params:'
    TYPE = '--- Constraint: %s'

class LogType(object):
    NAME = '--- name: %s'
    HOST_PATTERN = 'host pattern: %s'
    PATH_PATTERN = ' path pattern: %s'
    FILE_NAME_MATCHER = 'file name matcher: %s'
    ADD_BUTTON = 'add_log_type'
    CANCEL_BUTTON = 'cancel'

class LineNames(object):
    EFFECT = 'effect'
    CAUSE = 'cause_%s'
    CAUSE_PREFIX = 6

class Templates(object):
    READ_ERROR = '! Error ! %s'

class ReadMessages(object):
    TOO_MANY_LINES = Templates.READ_ERROR % 'You can put only single line as content of parser.'


import tensorflow as tf

from finetune.base import BaseModel, DROPOUT_OFF
from finetune.target_encoders import MultilabelClassificationEncoder
from finetune.network_modules import multi_classifier

import warnings

class MultiLabelClassifier(BaseModel):
    """
    Classifies a single document into upto N of N categories.

    :param config: A :py:class:`finetune.config.Settings` object or None (for default config).
    :param \**kwargs: key-value pairs of config items to override.
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.threshold_placeholder = None

    def featurize(self, X, max_length=None):
        """
        Embeds inputs in learned feature space. Can be called before or after calling :meth:`finetune`.

        :param X: list or array of text to embed.
        :param max_length: the number of tokens to be included in the document representation.
                           Providing more than `max_length` tokens as input will result in truncation.
        :returns: np.array of features of shape (n_examples, embedding_size).
        """
        return super().featurize(X, max_length=max_length)

    def predict(self, X, threshold=None, max_length=None):
        """
        Produces a list of most likely class labels as determined by the fine-tuned model.

        :param X: list or array of text to embed.
        :param max_length: the number of tokens to be included in the document representation.
                           Providing more than `max_length` tokens as input will result in truncation.
        :returns: list of class labels.
        """
        threshold = threshold or self.config.multi_label_threshold
        return self._predict(X, threshold=threshold, max_length=max_length)

    def predict_proba(self, X, max_length=None):
        """
        Produces a probability distribution over classes for each example in X.

        :param X: list or array of text to embed.
        :param max_length: the number of tokens to be included in the document representation.
                           Providing more than `max_length` tokens as input will result in truncation.
        :returns: list of dictionaries.  Each dictionary maps from a class label to its assigned class probability.
        """
        return super().predict_proba(X, max_length=max_length)

    def finetune(self, X, Y=None, batch_size=None):
        """
        :param X: list or array of text.
        :param Y: A list of lists containing labels for the corresponding X
        :param batch_size: integer number of examples per batch. When N_GPUS > 1, this number
                           corresponds to the number of training examples provided to each GPU.
        """
        return super().finetune(X, Y=Y, batch_size=batch_size)

    def _target_encoder(self):
        return MultilabelClassificationEncoder()

    def _target_model(self, featurizer_state, targets, n_outputs, train=False, reuse=None, **kwargs):
        return multi_classifier(
            hidden=featurizer_state['features'],
            targets=targets,
            n_targets=n_outputs,
            dropout_placeholder=self.do_dropout,
            config=self.config,
            train=train,
            reuse=reuse,
            **kwargs
        )

    def _predict_op(self, logits, **kwargs):
        self.threshold_placeholder = tf.placeholder(tf.float32)
        return tf.cast(tf.nn.sigmoid(logits) > self.threshold_placeholder, tf.int32)

    def _predict_proba_op(self, logits, **kwargs):
        return tf.nn.sigmoid(logits)

    def _predict(self, X, threshold, max_length=None):
        predictions = []
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore")
            max_length = max_length or self.config.max_length
            for xmb, mmb in self._infer_prep(X, max_length=max_length):
                output = self._eval(
                    self.predict_op,
                    feed_dict={
                        self.X: xmb,
                        self.M: mmb,
                        self.do_dropout: DROPOUT_OFF,
                        self.threshold_placeholder: threshold
                    }
                )
                prediction = output.get(self.predict_op)
                formatted_predictions = self.label_encoder.inverse_transform(prediction)
                predictions.extend(formatted_predictions)
        return predictions


import time
import _thread as thread

from . import ws2wh, wh2ws, ws2api, api2ws, file2wh, file2ws, file2api, wh2file, ws2file, api2file

from sectors.common import error

from db.models import (
    TBLBridge
)

class BridgeQueue:
    """
    BridgeQueue to manage all Data Bridges
    """

    def __init__(self):
        self.bridges_info = []
        self.bridges_obj = []

    def fetch_all_bridges(self):
        self.bridges_info = list(TBLBridge.objects.filter(is_active=True).values())

    def open_bridge(self, bridge_info):
        if bridge_info['type'] == 1:  # ws2wh
            b_obj = ws2wh.Bridge(bridge_info)
            b_obj.open()
        elif bridge_info['type'] == 2:  # wh2ws
            b_obj = wh2ws.Bridge(bridge_info)
            b_obj.open()
        elif bridge_info['type'] == 3:  # ws2api
            b_obj = ws2api.Bridge(bridge_info)
            b_obj.open()
        elif bridge_info['type'] == 4:  # api2ws
            b_obj = api2ws.Bridge(bridge_info)
            b_obj.open()
        elif bridge_info['type'] == 5:  # file2wh
            b_obj = file2wh.Bridge(bridge_info)
            b_obj.open()
        elif bridge_info['type'] == 6:  # file2ws
            b_obj = file2ws.Bridge(bridge_info)
            b_obj.open()
        elif bridge_info['type'] == 7:  # file2ws
            b_obj = file2api.Bridge(bridge_info)
            b_obj.open()
        elif bridge_info['type'] == 8:  # wh2file
            b_obj = wh2file.Bridge(bridge_info)
            b_obj.open()
        elif bridge_info['type'] == 9:  # ws2file
            b_obj = ws2file.Bridge(bridge_info)
            b_obj.open()
        elif bridge_info['type'] == 10: # api2file
            b_obj = api2file.Bridge(bridge_info)
            b_obj.open()
        else:
            return False, error.UNKNOWN_BRIDGE_TYPE

        self.bridges_obj.append({
            'id': bridge_info['id'],
            'obj': b_obj,
            'status': False
        })

        return True, error.SUCCESS

    def start_all(self):
        for bridge_info in self.bridges_info:
            self.open_bridge(bridge_info)

        thread.start_new_thread(self.update_connection_status, ())

    def update_connection_status(self):
        while True:
            for b_obj in self.bridges_obj:
                b_obj['status'] = b_obj['obj'].is_connected()

            time.sleep(1)

    def start_bridge_by_id(self, bridge_id):
        new_bridge = True
        for b_obj in self.bridges_obj:
            if b_obj['id'] == bridge_id:
                b_obj['obj'].open()
                new_bridge = False
                break

        if new_bridge:
            bridge_info = list(TBLBridge.objects.filter(id=bridge_id).values())[0]
            return self.open_bridge(bridge_info)

        return True, error.SUCCESS

    def restart_bridge_by_id(self, bridge_id):
        self.remove_bridge_by_id(bridge_id)
        bridge_info = list(TBLBridge.objects.filter(id=bridge_id).values())[0]
        return self.open_bridge(bridge_info)

    def stop_bridge_by_id(self, bridge_id):
        for b_obj in self.bridges_obj:
            if b_obj['id'] == bridge_id:
                b_obj['obj'].close()
                b_obj['status'] = False
                return True, error.SUCCESS

        return False, error.UNKNOWN_BRIDGE_ID

    def remove_bridge_by_id(self, bridge_id):
        for b_obj in self.bridges_obj:
            if b_obj['id'] == bridge_id:
                if b_obj['status']:
                    b_obj['obj'].close()
                b_obj['obj'].close_log()
                self.bridges_obj.remove(b_obj)
                return True, error.SUCCESS

        return False, error.UNKNOWN_BRIDGE_ID

    def get_bridge_cache(self, bridge_id):
        cache = []
        for b_obj in self.bridges_obj:
            if b_obj['id'] == bridge_id:
                cache = b_obj['obj'].get_cache()
                break

        return cache

    def send_message(self, bridge_id, message):
        for b_obj in self.bridges_obj:
            if b_obj['id'] == bridge_id and b_obj['status']:
                b_obj['obj'].send_message(message)
                return True

        return False

    def notify_event(self, event):
        bridge_id = event['bridge_id']
        for b_obj in self.bridges_obj:
            if b_obj['id'] == bridge_id:
                b_obj['obj'].notify_event(event)
                break


# Requests Example
import requests
import json
import logging

# Decorator for control response code
def client_method(func):
    def wrapper(*args, **kwargs):
        response = func(*args, **kwargs)
        return response.json()['results'] if response.status_code == 200 else None
    return wrapper

@client_method
def make_request(count):
    return requests.get(f'https://randomuser.me/api/?results={count}')

def get_users(count):
    data = make_request(count)
    users = []
    for item in data:
        user = {}
        user['title'] = item['name']['title']
        user['first'] = item['name']['first']
        user['last'] = item['name']['last']
        user['username'] = item['login']['username']
        user['password'] = item['login']['password']
        # append user info in users array
        users.append(user)
    # return users in json format
    return json.dumps({'users': users}, indent=4)

if __name__ == "__main__":
    result = get_users(200)
    # write users to json file
    with open('users.json', 'w') as f:
        f.write(result)

    # logging
    logging.basicConfig(
        filename='response.log',
        filemode='w',
        format='%(asctime)s - %(message)s',
        level=logging.INFO)
    logging.info(result)

# Alperen Cubuk


import requests

ENDPOINT = 'https://en.wikipedia.org/w/api.php'

parameters = { 'action' : 'query',
               'prop' : 'revisions',
               'titles' : 'Panama_Papers',
               'format' : 'json',
               'rvdir' : 'newer',
               'rvstart': '2016-04-03T17:59:05Z',
               'rvend' : '2016-04-04T17:59:05Z',
               'continue' : ''}

num_revisions = 0

wp_call = requests.get(ENDPOINT, params=parameters)
response = wp_call.json()
print(response)

# look at the response: see 'continue'.
# this means there are more revisions than what we got in the response.

parameters['continue'] = response['continue']['continue']
parameters['rvcontinue'] = response['continue']['rvcontinue']

# do another call

wp_call = requests.get(ENDPOINT, params=parameters)
response = wp_call.json()
print(response)

# we can get all the revisions by looping until we don't see continue in the response any more
# introduce while loop
done = False
while not done:
    wp_call = requests.get(ENDPOINT, params=parameters)
    response = wp_call.json()
    print(response)
#
    # we need to copy the values the API gave us to continue
    if 'continue' in response:
        parameters['continue'] = response['continue']['continue']
        parameters['rvcontinue'] = response['continue']['rvcontinue']
    else:
        done = True


import json
import operator
from collections import defaultdict
from functools import reduce

from django.core import serializers
from project.users.models import Room
from project.users.services.base_service import BaseService

from django.db.models import Q

class RoomService(BaseService):
    """
    This class is used to retrieve and serialize room models
    for use by the public API
    """
    @staticmethod
    def _additional_filter_queryset(queryset, search_params):

        filter_fields = [
            'behaviour', 'breathing', 'states_of_consciousness',
            'symptom_control', 'cognition', 'mobility', 'tissue_viability',
            'nutrition', 'communication', 'psychological_emotional', 'continence'
        ]

        predicates_exclude = []
        predicated_include = []

        for field in filter_fields:
            if field in search_params:
                # include fields where only support for the need is required
                predicates_exclude.append(('support_' + field, 0))

            elif field + '_severity' in search_params:
                # search for fields with specific levels of need required
                predicated_include.append(('support_' + field + '__gte', search_params[field + '_severity']))

        q_list = [~Q(x) for x in predicates_exclude] + [Q(x) for x in predicated_include]

        if q_list:
            queryset = queryset.filter(reduce(operator.and_, q_list))

        return queryset

    def serialize_list_as_ordered_dict(self, rooms):
        rooms_meeting_criteria_dict = defaultdict(list)
        for room in rooms:
            rooms_meeting_criteria_dict[str(room.home.unique_id)].append(self.serialize_detail(room))
        return rooms_meeting_criteria_dict

    def get(self, filters):
        try:
            room = Room.objects.get(**filters)
        except (Room.DoesNotExist, Room.MultipleObjectsExist):
            return None
        return room

    def filter(self, filters, **kwargs):
        available_rooms = Room.objects.filter(**filters).order_by('home_internal_id')
        additional_filters = kwargs.get('additional_filters', dict())
        return self._additional_filter_queryset(available_rooms, additional_filters)

    def serialize_detail(self, obj, **kwargs):
        data = serializers.serialize('json', [obj,])
        struct = json.loads(data)
        serialized_data = struct[0]['fields']
        serialized_data['la_funding_available'], serialized_data['chc_funding_available'] = (
            obj.nhs_funding_available
        )
        return serialized_data

    def serialize_list(self, queryset, **kwargs):
        response = list()
        for obj in queryset:
            serialized_obj = self.serialize_detail(obj)
            response.append(serialized_obj)
        return response


import math

import numpy as np
from collections import Counter
from keras_preprocessing.sequence import pad_sequences
from keras_preprocessing.text import Tokenizer
import re
from tensorflow.keras.models import load_model
from konlpy.tag import Okt
import datetime as dt
import requests
import pandas as pd
import pathlib
import os

class News:

    global client_id
    global client_secret
    global vocab_size
    client_id = "vKllKxKbERYB7fpkEQO4"
    client_secret = "K4jATtYNtv"
    global days_2ago
    days_2ago = (dt.datetime.today() - dt.timedelta(days=2)).strftime('%Y-%m-%d')

    # def __init__(self):
    #     filename = os.getcwd() + "app/service/news_model.h5"
    #     score_model = load_model(filename)

    def getNaverSearchNews(self, search_word, page_start, display):
        headers = {'X-Naver-Client-Id' : client_id,
                   'X-Naver-Client-Secret':client_secret
                   }
        search_word = search_word
        encode_type = 'json'
        max_display = display
        sort = 'date' #결과값의 정렬기준 시간순 date, 관련도 순 sim
        start = page_start
        url = f"https://openapi.naver.com/v1/search/news.{encode_type}?query={search_word}&display={str(int(max_display))}&start={str(int(start))}&sort={sort}"

        try:
            #HTTP요청 보내기
            r = requests.get(url, headers=headers)
            #요청 결과 보기 200 이면 정상적으로 요청 완료
            if (r == None):
                return None

            new_df = pd.DataFrame(r.json()['items'])
            # new_df= new_df[new_df['title'].str.contains(search_word)]
            new_df['pubDate'] = [ dt.datetime.strptime(x[5:16], "%d %b %Y").strftime('%Y-%m-%d') for x in new_df['pubDate'] ]
        except:
            return None
        return new_df

    def DFconcat(self, df, df_add):
        df = pd.concat([df, df_add])
        df = df.drop_duplicates(['title'], keep='first').reset_index(drop=True)
        return df

    def checkDate(self, df):
        if (df.iloc[-1, -1] < days_2ago) or (len(df) > 100):
            return False
        else: return True

    def morphs_nlp(self, df):
        # 불용어
        stopwords = ['부터','로부터','에는','개월','월','어제','오늘','어떻다','년도','만에','오전', '오후','까지', '이후', '분기', '오다', '가다', '기업','지수','대한','최근','대비','지난','연구원','지난','투자','통해','주가','만원','거래','거래','따르다','조억원','억원','되다','다소','약간','하고','했고','매우','많이','가장','돼다','이고','였다','였고','이다','이라고','이라는','있다','있었다','의','가','이','은','을','들','는','좀','잘','걍','과','도','를','으로','자','에','에서','와','한','하다',
                    '주식','가격','시장','증권','한국','금융','부터','br', '/><', '/>', '.<', '', '']
        morphs = []
        okt = Okt()
        data = df['description']
        data = [re.sub('[^A-Za-z가-힣 ]', '', sentence) for sentence in data] #한글 영어만

        for row in data:
            morphs_x = okt.morphs(row, stem=True) # 형태소 단위 토큰화
            morphs_x = [word for word in morphs_x if not word in stopwords] # 불용어 제거
            morphs_x = [word for word in morphs_x if not len(word)< 2 ] # 한글자는 필요없을거같아서 삭제
            morphs.append(morphs_x)

        # 원래 df에 컬럼 추가
        df['tokenized'] = morphs
        return df

    def pos_neg(self, df):
        word_cloud = []
        pos = len(df[df.score > 0.57])
        neg = len(df[df.score < 0.52])
        positive = df[df.score > 0.57][:10]
        negative = df[df.score < 0.52][:10]

        pos_link = list(np.array(positive['link'].tolist()))
        pos_title = [sentence.replace('','').replace('','').replace('&quot','').replace('&amp','').replace(';','') for sentence in list(np.array(positive['title'].tolist()))]
        neg_link = list(np.array(negative['link'].tolist()))
        neg_title = [sentence.replace('','').replace('','').replace('&quot','').replace('&amp','').replace(';','') for sentence in list(np.array(negative['title'].tolist()))]

        word_count = Counter(np.hstack(df['tokenized'].values)).most_common(80)
        for i in word_count:
            word_cloud.append({"name": i[0], "value": i[1]})
        return {'pos_count': pos, 'neg_count': neg, 'word_cloud': word_cloud,
                'pos_link': pos_link, 'pos_title': pos_title, 'neg_link': neg_link, 'neg_title': neg_title}

    def predict_score(self, df):
        test_x = df['tokenized'].values
        tokenizer = Tokenizer()
        tokenizer.fit_on_texts(test_x)
        threshold = 2

        total_cnt = len(tokenizer.word_index)
        vocab_size = total_cnt

        # 단어 집합 수 맞추기
        while vocab_size > 2600:
            rare_cnt = 0 # 빈도수가 threshold보다 작은 단어의 개수를 카운트
            total_freq = 0 # 총 단어 빈도수 총 합
            rare_freq = 0 # 등장 빈도수가 threshold보다 작은 단어의 등장 빈도수의 총 합

            for key, value in tokenizer.word_counts.items():
                total_freq = total_freq + value

                # 단어의 등장 빈도수가 threshold보다 작으면
                if value < threshold:
                    rare_cnt += 1
                    rare_freq = rare_freq + value
            threshold += 1

            vocab_size = total_cnt - rare_cnt + 2
        print('단어 집합의 크기 :', vocab_size)

        tokenizer = Tokenizer(vocab_size, oov_token = 'OOV')
        tokenizer.fit_on_texts(test_x)
        test_x = tokenizer.texts_to_sequences(test_x)

        # 크롤링 페이지의 기사 길이 확인하고 수정하기!!!!!!!!!!!!!!!!!!!!!!!111
        max_len = max(len(l) for l in test_x)
        test_x = pad_sequences(test_x, maxlen = max_len)

        filename = os.path.join(pathlib.Path(__file__).parent.absolute(), 'news_model.h5')
        # filename = "C:/Users/multicampus/Documents/S05P21A602/backend/root/app/service/news_model.h5"
        score_model = load_model(filename)

        score = score_model.predict(test_x)
        score = score.tolist()
        score = sum(score, [])
        df['score'] = score

        return {'df': df, 'vocab_size': vocab_size}

    def today_score(self, df):
        score_mean = round(np.mean(df['score']), 2)*100
        return score_mean

    def date_score(self, df):
        date_mean = df[['pubDate', 'score']].groupby('pubDate').mean().reset_index(drop=False)
        return date_mean

    def ratio(self, df):
        positive_ratio = round(len(df[df['score'] > 0.57])/len(df)*100, 2)
        negaitive_ratio = round(len(df[df['score'] < 0.52])/len(df)*100, 2)

        print('긍정 기사 비율 : ', positive_ratio, "% ")
        print('부정 기사 비율 : ', negaitive_ratio, "% ")
        return {'positive_ratio': positive_ratio, 'negaitive_ratio': negaitive_ratio}

news_service = News()


# -*- coding: utf-8 -*-
"""
Created on Feb 27 2017
Original Author: Weiping Song
Edited/Modified by: Benjamin Kwapong
"""
import pandas as pd
import numpy as np

MRR = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
RECALL = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
BATCHSIZE = [128]
EVALPOINTCOUNT = [0]

def cut_off():
    ''' Cut-off value (i.e. the length of the recommendation list '''
    cut_off1 = 1
    cut_off2 = 2
    cut_off3 = 5
    cut_off4 = 10
    cut_off5 = 20
    cut_off6 = 50
    cut_off7 = 100
    return (cut_off1, cut_off2, cut_off3, cut_off4, cut_off5, cut_off6, cut_off7)

def itemidmapx(train_data, test_data):
    ''' Build itemidmap from train data '''
    itemids = train_data['movieId'].unique()
    itemidmap = pd.Series(data=np.arange(len(itemids)), index=itemids)
    test_data.sort_values(['userId', 'timestamp'], inplace=True)
    offset_sessions = np.zeros(test_data['userId'].nunique()+1, dtype=np.int32)
    offset_sessions[1:] = test_data.groupby('userId').size().cumsum()
    #evalutation_point_count = 0
    itemx = (itemidmap, offset_sessions)
    return itemx

def evaluate_sessions_batch(model, train_data, test_data):
    '''
    Evaluates the GRU4Rec network wrt. recommendation accuracy measured by recall@N and MRR@N.

    Parameters
    --------
    model : A trained GRU4Rec model.
    train_data : Contains the transactions of the train set.
    In  evaluation phrase, this is used to build item-to-id map.
    test_data : It contains the transactions of the test set.
    It has one  column for session IDs, one for item IDs
    and one for the timestamp of  the events (unix timestamps).
    cut-off : int
        Cut-off value (i.e. the length of the recommendation list;
        N for recall@N and MRR@N). Defauld value is 20.
    BATCHSIZE[0] : int
        Number of events bundled into a batch during evaluation.
        Speeds up evaluation.
        If it is set high, the memory consumption increases. Default value is 100.
    session_key : string
        Header of the session ID column in the input file (default: 'SessionId')
    item_key : string
        Header of the item ID column in the input file (default: 'ItemId')
    time_key : string
        Header of the timestamp column in the input file (default: 'Time')
    Returns
    --------
    out : tuple
        (Recall@N, MRR@N)
    '''
    # (cut_off1, cut_off2, cut_off3, cut_off4, cut_off5, cut_off6, cut_off7) = \
    #(1, 2, 5, 10, 20, 50, 100)
    #(session_key, item_key, time_key) = ('userId', 'movieId', 'timestamp')
    model.predict = False
    # Build itemidmap from train data.
    #itemids = train_data['movieId'].unique()
    #itemidmap = pd.Series(data=np.arange(len(itemids)), index=itemids)
    #test_data.sort_values(['userId', 'timestamp'], inplace=True)
    #offset_sessions = np.zeros(test_data['userId'].nunique()+1, dtype=np.int32)
    #offset_sessions[1:] = test_data.groupby('userId').size().cumsum()
    #evalutation_point_count = 0
    itemx = itemidmapx(train_data, test_data)
    #mrr, recall, precision = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], \
    # [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    #mrr, recall = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], \
    #[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    # precision_counter = [0, 0, 0, 0, 0, 0, 0]
    if len(itemx[1]) - 1 < BATCHSIZE[0]:
        BATCHSIZE[0] = len(itemx[1]) - 1
    iters = np.arange(BATCHSIZE[0]).astype(np.int32)
    maxiter = iters.max()
    start = itemx[1][iters]
    end = itemx[1][iters+1]
    in_idx = np.zeros(BATCHSIZE[0], dtype=np.int32)
    #np.random.seed(42)
    while True:
        valid_mask = iters >= 0
        if valid_mask.sum() == 0:
            break
        start_valid = start[valid_mask]
        #minlen = (end[valid_mask]-start_valid).min()
        in_idx[valid_mask] = test_data['movieId'].values[start_valid]
        for i in range((end[valid_mask]-start_valid).min()-1):
            #out_idx = test_data['movieId'].values[start_valid+i+1]
            preds = model.predict_next_batch(iters, in_idx, itemx[0], BATCHSIZE[0])
            preds.fillna(0, inplace=True)
            in_idx[valid_mask] = test_data['movieId'].values[start_valid+i+1]
            ranks = (preds.values.T[valid_mask].T > \
            np.diag(preds.ix[in_idx].values)[valid_mask]).sum(axis=0) + 1
            #rank1_ok = ranks <= cut_off()[0]
            RECALL[0] += (ranks <= cut_off()[0]).sum()
            #precision[0] += rank1_ok.sum()
            #precision_counter[0] += cut_off1
            MRR[0] += (1.0 / ranks[(ranks <= cut_off()[0])]).sum()
            #rank2_ok = ranks <= cut_off()[1]
            RECALL[1] += (ranks <= cut_off()[1]).sum()
            #precision[1] += rank2_ok.sum()
            #precision_counter[1] += cut_off2
            MRR[1] += (1.0 / ranks[ranks <= cut_off()[1]]).sum()
            #rank3_ok = ranks <= cut_off()[2]
            RECALL[2] += (ranks <= cut_off()[2]).sum()
            #precision[2] += rank3_ok.sum()
            #precision_counter[2] += cut_off3
            MRR[2] += (1.0 / ranks[ranks <= cut_off()[2]]).sum()
            #rank4_ok = ranks <= cut_off()[3]
            RECALL[3] += (ranks <= cut_off()[3]).sum()
            #precision[3] += rank4_ok.sum()
            #precision_counter[3] += cut_off4
            MRR[3] += (1.0 / ranks[ranks <= cut_off()[3]]).sum()
            #rank5_ok = ranks <= cut_off()[4]
            RECALL[4] += (ranks <= cut_off()[4]).sum()
            #precision[4] += rank5_ok.sum()
            #precision_counter[4] += cut_off5
            MRR[4] += (1.0 / ranks[ranks <= cut_off()[4]]).sum()
            #rank6_ok = ranks <= cut_off()[5]
            RECALL[5] += (ranks <= cut_off()[5]).sum()
            #precision[5] += rank6_ok.sum()
            #precision_counter[5] += cut_off6
            MRR[5] += (1.0 / ranks[ranks <= cut_off()[5]]).sum()
            #rank7_ok = ranks <= cut_off()[6]
            RECALL[6] += (ranks <= cut_off()[6]).sum()
            #precision[6] += rank7_ok.sum()
            #precision_counter[6] += cut_off7
            MRR[6] += (1.0 / ranks[ranks <= cut_off()[6]]).sum()
            EVALPOINTCOUNT[0] += len(ranks)
        start = start + (end[valid_mask]-start_valid).min() - 1
        #mask = np.arange(len(iters))[(valid_mask) & (end-start <= 1)]
        for idx in np.arange(len(iters))[(valid_mask) & (end-start <= 1)]:
            maxiter += 1
            if maxiter >= len(itemx[1])-1:
                iters[idx] = -1
            else:
                iters[idx] = maxiter
                start[idx] = itemx[1][maxiter]
                end[idx] = itemx[1][maxiter+1]
    #for i in range(len(precision)):
        #precision[i] = precision[i]/precision_counter[i]
    return [x/EVALPOINTCOUNT[0] for x in RECALL], \
    [y/EVALPOINTCOUNT[0] for y in MRR]#, precision


from Descriptive_Statistics.Mean import Mean
from Descriptive_Statistics.Standard_Deviation import Standard_Deviation
from MathOperations.Addition import Addition

try:
    from itertools import imap
except ImportError:
    # Python 3...
    imap=map

class Sample_Correlation:

    @staticmethod
    def correlation(x, y):
        if(len(x) == len(y)):
            n = len(x)
            sum_x = float(Addition.sumList(x))
            sum_y = float(Addition.sumList(y))
            sum_x_sq = sum(map(lambda x: pow(x, 2), x))
            sum_y_sq = sum(map(lambda x: pow(x, 2), y))
            psum = sum(imap(lambda x, y: x * y, x, y))
            num = psum - (sum_x * sum_y / n)
            den = pow((sum_x_sq - pow(sum_x, 2) / n) * (sum_y_sq - pow(sum_y, 2) / n), 0.5)
            if den == 0: return 0
            return num / den
        else:
            return -1


from sklearn.pipeline import make_union

from suricate.dftransformers import VectorizerConnector, ExactConnector
from suricate.data.base import ix_names
from suricate.data.companies import getsource, gettarget,  getXst, getytrue
left = getsource(nrows=100)
right = gettarget(nrows=100)
X_lr = getXst(nrows=100)
y_true = getytrue(Xst=X_lr)

def test_loaddata():
    print(ix_names['ixname'])
    print(left.shape[0])
    print(right.shape[0])
    assert True

def test_tfidf():
    expected_shape = left.shape[0] * right.shape[0]
    stages = [
        VectorizerConnector(on='name', analyzer='char', pruning=False),
        VectorizerConnector(on='street', analyzer='char', pruning=False),
        ExactConnector(on='duns', pruning=False)

    ]
    scorer = make_union(*stages)
    X_score = scorer.transform(X=X_lr)
    assert X_score.shape[0] == expected_shape
    pass


import json
import socket
import struct

from flask import Flask, request, jsonify

from common_constants import CommonConstants
from distributed_lru.lru_cache import LRUCache

cache = LRUCache(CommonConstants.MAXIMUM_CACHE_ELEMENT_CAPACITY, CommonConstants.CACHE_EXPIRY_IN_SECONDS)

app = Flask('LRU_CACHE_SERVER_ONE')

# def set_up(app):

# set_up(app)

@app.route('/heartbeat', methods=['GET'])
def get_heartbeat():
    return jsonify({'heartbeat': 'success'})

@app.route('/getCacheItem', methods=['GET'])
def get_cache_item():
    item_key = request.args['item_key']
    key, value, expires_at = cache.get_element_from_cache(item_key)
    return jsonify({'item_key': key, 'value': value, 'expires_at': expires_at})

@app.route('/setCacheItem', methods=['POST'])
def set_cache_item():
    content = request.json
    item_key = content['item_key']
    item_value = content['item_value']
    expires_at = content['expires_at']
    cache.set_element_in_cache(item_key, item_value, expires_at)
    multicast_sender({'key': item_key, 'value': item_value, 'expires_at': expires_at, 'action': 'set'})
    return jsonify({item_key: item_value})

@app.route('/deleteCacheItem', methods=['DELETE'])
def delete_cache_item():
    item_key = request.args['item_key']
    flag_value = cache.delete_item_from_cache(item_key)
    multicast_sender({'key': item_key, 'action': 'delete'})
    return jsonify({item_key: flag_value})

# # def invoke_cache_update():
# def broadcast_update(item_key, item_value):
#     input_dict = {'item_key': item_key, 'item_value': item_value}
#     headers = {'Content-type': 'application/json', 'relay-update': False}
#     #server_string = f'http://{sync_server[0][0]}:{sync_server[0][1]}/updateCaches'
#     r = requests.post(server_string, headers=headers,
#                       json=input_dict)
#     return r.status_code

'''
    This function is responsible for sending out json message to the multicast group members
'''

def multicast_sender(data):

    multicast_group = (CommonConstants.MULTICAST_GROUP_IP, CommonConstants.MULTICAST_PORT_VALUE)

    # Create the datagram socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # Set a timeout so the socket does not block indefinitely when trying
    # to receive data.
    sock.settimeout(CommonConstants.SENDER_SOCKET_TIME_OUT)

    # Set the time-to-live for messages to 1 so they do not go past the
    # local network segment.
    ttl = struct.pack('b', CommonConstants.TIME_TO_LIVE_FOR_MESSAGE)
    sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)

    try:

        # Send data to the multicast group
        print(f'sending{data}')
        sent = sock.sendto(json.dumps(data).encode(), multicast_group)

        # Look for responses from all recipients
        while True:
            print('waiting to receive')
            try:
                data, server = sock.recvfrom(CommonConstants.SENDER_SOCKET_BUFFER_SIZE)
            except socket.timeout:
                print('timed out, no more responses')
                break
            else:
                print('received ---', data, server)

    finally:
        print('closing socket')
        sock.close()

@app.route('/updateCacheItem', methods=['PUT'])
def update_cache_item():
    content = request.json
    item_key = content['key']
    item_value = content['value']
    item_action = content['action']
    expires_at = content['expires_at']
    if item_action is 'delete':
        cache.delete_item_from_cache(item_key)
    else:
        cache.set_element_in_cache(item_key, item_value, expires_at)
    return jsonify({item_key: item_value})

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=CommonConstants.SERVER_ONE_PORT, debug=True)