import torch
from torch import nn
import torch.nn.functional as F

from models.Segformer import mit_b0,mit_b1,mit_b2#,mit_b3,mit_b4,mit_b5
class SK(nn.Module):
    def __init__(self, in_channel, mid_channel, out_channel, fuse, len=32, reduce=16):
        super(SK, self).__init__()
        len = max(mid_channel // reduce, len)
        self.fuse = fuse
        self.conv1 = nn.Sequential(
            nn.Conv2d(in_channel, mid_channel, kernel_size=1, bias=False),
            nn.BatchNorm2d(mid_channel),
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(mid_channel, out_channel,kernel_size=3,stride=1,padding=1,bias=False),
            nn.BatchNorm2d(out_channel),
        )
        if fuse:
            #https://github.com/syt2/SKNet
            self.gap = nn.AdaptiveAvgPool2d((1, 1))
            self.fc = nn.Sequential(
                nn.Conv2d(mid_channel, len, kernel_size=1, stride=1, bias=False),
                nn.BatchNorm2d(len),
                nn.ReLU(inplace=True)
            )
            self.fc1 = nn.Sequential(
                nn.Conv2d(mid_channel, len, kernel_size=1, stride=1, bias=False),
                nn.ReLU(inplace=True)
            )
            self.fcs = nn.ModuleList([])
            for i in range(2):
                self.fcs.append(
                    nn.Conv2d(len, mid_channel, kernel_size=1, stride=1)
                )
            self.softmax = nn.Softmax(dim=1)

        nn.init.kaiming_uniform_(self.conv1[0].weight, a=1)
        nn.init.kaiming_uniform_(self.conv2[0].weight, a=1)

    def forward(self, x, y=None, shape=None):

        x = self.conv1(x)
        if self.fuse:
            shape = x.shape[-2:]
            b = x.shape[0]
            y = F.interpolate(y, shape, mode="nearest")

            feas_U = [x,y]
            feas_U = torch.stack(feas_U,dim=1)
            attention = torch.sum(feas_U, dim=1)
            attention = self.gap(attention)
            if b ==1:
                attention = self.fc1(attention)
            else:
                attention = self.fc(attention)
            attention = [fc(attention) for fc in self.fcs]
            attention = torch.stack(attention, dim=1)
            attention = self.softmax(attention)
            x = torch.sum(feas_U * attention, dim=1)

        # output
        y = self.conv2(x)
        return y, x

class SKF(nn.Module):
    def __init__(
        self,student, in_channels, out_channels, mid_channel, embed
    ):
        super(SKF, self).__init__()
        self.student = student

        skfs = nn.ModuleList()

        for idx, in_channel in enumerate(in_channels):
            skfs.append(SK(in_channel, mid_channel, out_channels[idx], idx < len(in_channels)-1))

        self.skfs = skfs[::-1]
        self.embed = embed
        if self.embed == 5:
            self.embed1_linearproject = nn.Linear(in_channels[0], out_channels[0])
            self.embed2_linearproject = nn.Linear(in_channels[1], out_channels[1])
            self.embed3_linearproject = nn.Linear(in_channels[2], out_channels[2])
            self.embed4_linearproject = nn.Linear(in_channels[3], out_channels[3])
        elif self.embed == 1:
            self.embed1_linearproject = nn.Linear(in_channels[0], out_channels[0])
        elif self.embed == 2:
            self.embed1_linearproject = nn.Linear(in_channels[1], out_channels[1])
        elif self.embed == 3:
            self.embed1_linearproject = nn.Linear(in_channels[2], out_channels[2])
        elif self.embed == 4:
            self.embed1_linearproject = nn.Linear(in_channels[3], out_channels[3])

    def forward(self, x):
        student_features = self.student(x,is_feat=True)
        embed = student_features[2]
        logit = student_features[1]
        x = student_features[0][::-1]
        results = []
        embedproj = []
        out_features, res_features = self.skfs[0](x[0])
        results.append(out_features)
        for features, skf in zip(x[1:], self.skfs[1:]):
            out_features, res_features = skf(features, res_features)
            results.insert(0, out_features)

        if self.embed ==5:
            embedproj = [*embedproj, self.embed1_linearproject(embed[0])]
            embedproj = [*embedproj, self.embed2_linearproject(embed[1])]
            embedproj = [*embedproj, self.embed3_linearproject(embed[2])]
            embedproj = [*embedproj, self.embed4_linearproject(embed[3])]
            return results, logit, embedproj
        elif self.embed == 0:
            return results, logit
        elif self.embed == 1:
            embedproj = [*embedproj, self.embed1_linearproject(embed[0])]
            return results, logit, embedproj
        elif self.embed == 2:
            embedproj = [*embedproj, self.embed1_linearproject(embed[1])]
            return results, logit, embedproj
        elif self.embed == 3:
            embedproj = [*embedproj, self.embed1_linearproject(embed[2])]
            return results, logit, embedproj
        elif self.embed == 4:
            embedproj = [*embedproj, self.embed1_linearproject(embed[3])]
            return results, logit, embedproj
        else:
            assert 'the number of embeddings not supported'

def build_kd_trans(model,embed,in_channels = [32, 64, 160, 256], out_channels = [64, 128, 320, 512]):
    mid_channel = 64
    student = model
    model = SKF(student, in_channels, out_channels, mid_channel,embed)
    return model

def hcl(fstudent, fteacher):
    loss_all = 0.0
    for fs, ft in zip(fstudent, fteacher):
        n,c,h,w = fs.shape
        loss = F.mse_loss(fs, ft, reduction='mean')
        cnt = 1.0
        tot = 1.0
        for l in [4,2,1]:
            if l >=h:
                continue
            tmpfs = F.adaptive_avg_pool2d(fs, (l,l))
            tmpft = F.adaptive_avg_pool2d(ft, (l,l))
            cnt /= 2.0
            loss += F.mse_loss(tmpfs, tmpft, reduction='mean') * cnt
            tot += cnt
        loss = loss / tot
        loss_all = loss_all + loss
    return loss_all

class ChannelNorm(nn.Module):
    def __init__(self):
        super(ChannelNorm, self).__init__()
    def forward(self,featmap):
        n,c,h,w = featmap.shape
        featmap = featmap.reshape((n,c,-1))
        featmap = featmap.softmax(dim=-1)
        return featmap

class CriterionCWD(nn.Module):

    def __init__(self,norm_type='none',divergence='mse',temperature=1.0):

        super(CriterionCWD, self).__init__()

        # define normalize function
        if norm_type == 'channel':
            self.normalize = ChannelNorm()
        elif norm_type =='spatial':
            self.normalize = nn.Softmax(dim=1)
        elif norm_type == 'channel_mean':
            self.normalize = lambda x:x.view(x.size(0),x.size(1),-1).mean(-1)
        else:
            self.normalize = None
        self.norm_type = norm_type

        self.temperature = 1.0

        # define loss function
        if divergence == 'mse':
            self.criterion = nn.MSELoss(reduction='sum')
        elif divergence == 'kl':
            self.criterion = nn.KLDivLoss(reduction='sum')
            self.temperature = temperature
        self.divergence = divergence

    def forward(self,preds_S, preds_T):

        n,c,h,w = preds_S.shape
        #import pdb;pdb.set_trace()
        if self.normalize is not None:
            norm_s = self.normalize(preds_S/self.temperature)
            norm_t = self.normalize(preds_T.detach()/self.temperature)
        else:
            norm_s = preds_S[0]
            norm_t = preds_T[0].detach()

        if self.divergence == 'kl':
            norm_s = norm_s.log()
        loss = self.criterion(norm_s,norm_t)

        #item_loss = [round(self.criterion(norm_t[0][0].log(),norm_t[0][i]).item(),4) for i in range(c)]
        #import pdb;pdb.set_trace()
        if self.norm_type == 'channel' or self.norm_type == 'channel_mean':
            loss /= n * c
            # loss /= n * h * w
        else:
            loss /= n * h * w

        return loss * (self.temperature**2)
######################################################################################################################
class EmbedChannelNorm(nn.Module):
    def __init__(self):
        super(EmbedChannelNorm, self).__init__()
    def forward(self,embed):
        n,c,_ = embed.shape
        embed = embed.softmax(dim=-1)
        return embed

class CriterionEmbedCWD(nn.Module):

    def __init__(self,norm_type='none',divergence='mse',temperature=1.0):

        super(CriterionEmbedCWD, self).__init__()

        # define normalize function
        if norm_type == 'channel':
            self.normalize = EmbedChannelNorm()
        elif norm_type =='spatial':
            self.normalize = nn.Softmax(dim=1)
        elif norm_type == 'channel_mean':
            self.normalize = lambda x:x.view(x.size(0),x.size(1),-1).mean(-1)
        else:
            self.normalize = None
        self.norm_type = norm_type

        self.temperature = 1.0

        # define loss function
        if divergence == 'mse':
            self.criterion = nn.MSELoss(reduction='sum')
        elif divergence == 'kl':
            self.criterion = nn.KLDivLoss(reduction='sum')
            self.temperature = temperature
        self.divergence = divergence

    def forward(self,embed_S, embed_T):
        embed_S = embed_S.transpose(1, 2).contiguous()
        embed_T = embed_T.transpose(1, 2).contiguous()
        n,c,_ = embed_S.shape
        #import pdb;pdb.set_trace()
        if self.normalize is not None:
            norm_s = self.normalize(embed_S/self.temperature)
            norm_t = self.normalize(embed_T.detach()/self.temperature)
        else:
            norm_s = embed_S[0]
            norm_t = embed_T[0].detach()

        if self.divergence == 'kl':
            norm_s = norm_s.log()
        loss = self.criterion(norm_s,norm_t)

        if self.norm_type == 'channel' or self.norm_type == 'channel_mean':
            loss /= n * c

        return loss * (self.temperature**2)

def hcl_feaw(fstudent, fteacher):
    loss_all = 0.0
    fea_weights = [0.1,0.1,0.5,1]
    for fs, ft,fea_w in zip(fstudent, fteacher,fea_weights):
        n,c,h,w = fs.shape
        loss = F.mse_loss(fs, ft, reduction='mean')
        cnt = 1.0
        tot = 1.0
        for l in [4,2,1]:
            if l >=h:
                continue
            tmpfs = F.adaptive_avg_pool2d(fs, (l,l))
            tmpft = F.adaptive_avg_pool2d(ft, (l,l))
            cnt /= 2.0
            loss += F.mse_loss(tmpfs, tmpft, reduction='mean') * cnt
            tot += cnt
        loss = loss / tot
        loss_all = loss_all + fea_w*loss
    return loss_all


#!/usr/bin/env python3

from pylab import *
from numpy import *
import matplotlib.cm as cm
from common import *

idx_vec = range(1, num_k+1)

if with_FVD_solution == True :
    if num_k > 1 :
        fig, ax = plt.subplots(2, num_k, figsize=(9, 5.5))
    else :
        fig, ax = plt.subplots(1, 2, figsize=(9, 5.5))
    fig.suptitle('Eigenfunctions, %s' % task_name)
else :
    fig, ax = plt.subplots(1, num_k, figsize=(9, 5.5))
    fig.suptitle('Eigenfunctions, %s' % task_name)

tot_min = -0.3
tot_max = 0.3

if with_FVD_solution :
    for i in range(len(idx_vec)) :
      if conjugated_eigvec_flag == 1 :
        data_file = open('../%s/data/%s_FVD_%d_conjugated.txt' % (working_dir_name, eig_file_name_prefix, idx_vec[i]), 'r')
      else :
        data_file = open('../%s/data/%s_FVD_%d.txt' % (working_dir_name, eig_file_name_prefix, idx_vec[i]), 'r')

      xmin, xmax, nx = [ float (x) for x in data_file.readline().split() ]
      ymin, ymax, ny = [ float (x) for x in data_file.readline().split() ]

      Z = np.loadtxt(data_file)

      x = np.linspace(xmin, xmax, int(nx))
      y = np.linspace(ymin, ymax, int (ny))

      if num_k > 1 :
          fvd_ax = ax[0, i]
      else :
          fvd_ax = ax[i]

      im = fvd_ax.imshow( Z , cmap=cm.jet, extent = [xmin, xmax, ymin, ymax], vmin=tot_min , vmax=tot_max , origin='lower', interpolation='none' )
      fvd_ax.set_title('FVD, %dth' % (idx_vec[i]))

      if i == 0:
        yticks(np.linspace(xmin, xmax, 5))
      else :
        plt.setp(fvd_ax.get_yticklabels(), visible=False)

sign_list = [1 for i in range(num_k)]
sign_list[0] = 1
if num_k > 1 :
    sign_list[1] = -1
if num_k > 2 :
    sign_list[2] = -1

for i in range(len(idx_vec)) :
  base_name = '../%s/data/%s' % (working_dir_name, eig_file_name_prefix)

  if conjugated_eigvec_flag == 1 :
      data_file = open('%s_%d_conjugated.txt' % (base_name, idx_vec[i]), 'r')
  else :
      data_file = open('%s_%d.txt' % (base_name, idx_vec[i]), 'r')

  xmin, xmax, nx  = [ float (x) for x in data_file.readline().split() ]
  ymin, ymax, ny  = [ float (x) for x in data_file.readline().split() ]

  Z = np.loadtxt(data_file, skiprows=0)

  x = np.linspace(xmin, xmax, int (nx))
  y = np.linspace(ymin, ymax, int (ny))
  X, Y = np.meshgrid(x,y)

#  tot_min = Z.min()
#  tot_max = Z.max()
#  print (tot_min, tot_max)

  if with_FVD_solution :
      if num_k > 1 :
          nn_ax = ax[1, i]
      else :
          nn_ax = ax[num_k+i]
  else :
      if num_k > 1 :
          nn_ax = ax[i]
      else :
          nn_ax = ax
  im = nn_ax.imshow( sign_list[i] * Z , cmap=cm.jet, extent = [xmin, xmax, ymin, ymax], vmin=tot_min , vmax=tot_max , origin='lower', interpolation='none' )

  nn_ax.set_title('NN, %dth' % (idx_vec[i]) )

  if i == 0:
    yticks(np.linspace(xmin, xmax, 5))
  else :
    plt.setp(nn_ax.get_yticklabels(), visible=False)

cax = fig.add_axes([0.92, 0.12, .04, 0.79])
#fig.colorbar(im, cax=cax, orientation='horizontal',cmap=cm.jet)
fig.colorbar(im, cax=cax, cmap=cm.jet)
#cax.tick_params(labelsize=10)

base_name = '../%s/fig/eigvec_nn_and_FVD' % (working_dir_name)

if conjugated_eigvec_flag == 1 :
    fig_name = '%s_%d_conjugated.eps' % (base_name, num_k)
else :
    fig_name = '%s_%d.eps' % (base_name, num_k)

savefig(fig_name)

print ("output figure: %s" % fig_name)


import traceback, re, json, logging

from ..file_utilities.filepath import Filepath
from ..entitlements.entitlement_manager import Entitlement_Manager
from .file_manager import File_Manager
from ..client_config import COLLECTIONS_WITH_BAD_LEVEL_IMAGES, UNLOCK_ALL_BUDDIES

from .. import shared

logger_errors = logging.getLogger('VIM_errors')
logger = logging.getLogger('VIM_main')
logger_inv = logging.getLogger('VIM_inventory')

class Buddy_Manager:

    @staticmethod
    def generate_blank_buddy_database():
        if shared is not None:
            client = shared.client
            weapon_data = client.all_weapon_data

            payload = {}
            File_Manager.update_individual_inventory(payload, "buddies")

    @staticmethod
    async def update_inventory(**kwargs):
        payload = json.loads(kwargs.get("payload"))
        buddy_uuid = payload["buddyUuid"]
        new_data = payload["newData"]

        inventory = File_Manager.fetch_individual_inventory()["buddies"]

        for uuid,buddy in inventory.items():
            if uuid == buddy_uuid:
                inventory[uuid] = new_data
                break

        File_Manager.update_individual_inventory(inventory, "buddies")
        await shared.client.broadcast_loadout()

        return inventory

    @staticmethod
    async def favorite_all(**kwargs):
        payload = json.loads(kwargs.get("payload"))
        favorite = payload["favorite"]

        inventory = File_Manager.fetch_individual_inventory()["buddies"]
        for uuid,buddy in inventory.items():
            for instance_uuid,instance in buddy["instances"].items():
                if not instance["locked"]:
                    instance["favorite"] = favorite

        File_Manager.update_individual_inventory(inventory, "buddies")
        await shared.client.broadcast_loadout()

        return inventory

    @staticmethod
    def refresh_buddy_inventory():
        valclient = shared.client.client
        client = shared.client

        old_data = None

        try:
            old_data = File_Manager.fetch_individual_inventory()["buddies"]
        except KeyError:
            old_data = None
        except Exception as e:
            logger_errors.error(traceback.format_exc())
            logger.debug("making fresh buddy database")
            Buddy_Manager.generate_blank_skin_database()

        buddy_entitlements = Entitlement_Manager.fetch_entitlements(valclient, "buddy")["Entitlements"]

        sanitized_buddy_entitlements = {}
        for entitlement in buddy_entitlements:
            if not entitlement["ItemID"] in sanitized_buddy_entitlements.keys():
                sanitized_buddy_entitlements[entitlement["ItemID"]] = []
            sanitized_buddy_entitlements[entitlement["ItemID"]].append(entitlement["InstanceID"])

        inventory = {}

        # iterate through each buddy
        for buddy in client.all_buddy_data:
            buddy_owned = False
            owned_level_id = ""
            levels = [level["uuid"] for level in buddy["levels"]]

            if UNLOCK_ALL_BUDDIES:
                buddy_owned = True

            for level in levels:
                if level in sanitized_buddy_entitlements.keys():
                    buddy_owned = True
                    owned_level_id = level
                    break

            if buddy_owned:
                buddy_payload = {}
                existing_buddy_data = None

                if old_data is not None:
                    try:
                        existing_buddy_data = old_data[buddy["uuid"]]
                    except:
                        pass

                buddy_payload["display_name"] = buddy["displayName"]
                buddy_payload["uuid"] = buddy["uuid"]
                buddy_payload["display_icon"] = buddy["displayIcon"]
                buddy_payload["level_uuid"] = owned_level_id
                buddy_payload["instance_count"] = len(sanitized_buddy_entitlements[owned_level_id])

                buddy_payload["instances"] = {}

                for instance in sanitized_buddy_entitlements[owned_level_id]:
                    try:
                        buddy_payload["instances"][instance] = {
                            "uuid": instance,
                            "favorite": existing_buddy_data["instances"][instance]["favorite"] if existing_buddy_data is not None else False,
                            "super_favorite": existing_buddy_data["instances"][instance]["super_favorite"] if existing_buddy_data is not None else False,
                            "locked": existing_buddy_data["instances"][instance]["locked"] if existing_buddy_data is not None else False,
                            "locked_weapon_uuid": existing_buddy_data["instances"][instance]["locked_weapon_uuid"] if existing_buddy_data is not None else "",
                            "locked_weapon_display_name": existing_buddy_data["instances"][instance]["locked_weapon_display_name"] if existing_buddy_data is not None else "",
                        }

                    # remove me later
                    except:
                        buddy_payload["instances"][instance] = {
                            "uuid": instance,
                            "favorite": False,
                            "super_favorite": False,
                            "locked": False,
                            "locked_weapon_uuid": "",
                            "locked_weapon_display_name": "",
                        }

                # check for invalid favorite/lock combinations
                for instance in buddy_payload["instances"].values():
                    if instance["locked"]:
                        instance["favorite"] = False
                        if instance["locked_weapon_uuid"] == "" or instance["locked_weapon_display_name"] == "":
                            instance["locked"] = False
                            instance["locked_weapon_uuid"] = ""
                            instance["locked_weapon_display_name"] = ""

                inventory[buddy["uuid"]] = buddy_payload

        sort = sorted(inventory.items(), key=lambda x: x[1]["display_name"].lower())
        inventory = {k: v for k, v in sort}

        logger_inv.debug(f"buddy inventory:\n{json.dumps(inventory)}")
        File_Manager.update_individual_inventory(inventory,"buddies")
        return True


from multiprocessing import Process, Lock, Queue, Semaphore
import time
from random import random

buffer = Queue(10)
empty = Semaphore(2)  # 缓存空余数
full = Semaphore(0)  # 缓存占用数
lock = Lock()

class Consumer(Process):

    def run(self):
        global empty, buffer, full, lock
        while True:
            full.acquire()
            lock.acquire()   # 占用空间先acquire
            num = buffer.get()
            time.sleep(1)
            print(f"Consumer remove an element..{num}")
            lock.release()
            empty.release()

class Producer(Process):

    def run(self):
        global empty, full, buffer, lock
        while True:
            empty.acquire()
            lock.acquire()
            num = random()
            buffer.put(num)

            time.sleep(1)
            print("Producer append an element...  {}".format(num))
            lock.release()
            full.release()

if __name__ == "__main__":
    consumer = Consumer()
    producer = Producer()
    producer.daemon = consumer.daemon = True
    producer.start()
    consumer.start()

    producer.join()
    consumer.join()
    print("Main process ended!!!")


# Import libraries
from requests import get
from json import dumps

# Your own local host's url
URL = "http://127.0.0.1:5000/"

# Names of active pages
mine_block = "mine_block"
get_chain = "get_chain"
is_valid = "is_valid"

# Define function for to check if API works and use the API.
def check_request_and_get_result(url, target_page_name, checked=False, needed_json_dumps=True):
    target_url = url + target_page_name
    request = get(target_url)
    response = request.status_code
    if checked:
        return dumps(request.json(), sort_keys=True, indent=4) if needed_json_dumps else request.json()
    else:
        return "Congratulation, API works!" if response == 200 else "Something went wrong."

print(check_request_and_get_result(URL, get_chain, True))

blockchain_davidcoin/Module 1 - Create a Blockchain/use_your_own_API.py


import sys, os
from turtle import home

myPath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, myPath + '/../')

import time
import pytest
import allure
from allure_commons.types import AttachmentType
from Tests.test_Base import BaseTest
from Locators.Locators import Locators
from Config.config import TestData
from Pages.LoginPage import LoginPage
from Locators.EnumsPackage.Enums import Sort_Productss

class Test_Home(BaseTest):

    @pytest.mark.order()
    def test_verify_home_page_title(self):
        self.loginPage = LoginPage(self.driver)
        homePage = self.loginPage.do_login()
        title = homePage.get_title()
        assert title == TestData.HOME_PAGE_TITLE
        allure.attach(self.driver.get_screenshot_as_png(),attachment_type=AttachmentType.PNG)

    @pytest.mark.order()
    def test_verify_home_page_header(self):
        self.loginPage = LoginPage(self.driver)
        homePage = self.loginPage.do_login()
        header = homePage.get_header_value()
        allure.attach(self.driver.get_screenshot_as_png(), attachment_type=AttachmentType.JPG)
        assert header == TestData.HOME_PAGE_HEADER

    @pytest.mark.order()
    def test_verify_cart_icon_visible(self):
        self.loginPage = LoginPage(self.driver)
        homePage = self.loginPage.do_login()
        notification = homePage.is_cart_icon_exist()
        assert notification
        allure.attach(self.driver.get_screenshot_as_png(),attachment_type=AttachmentType.JPG)

    @pytest.mark.order()
    def test_verify_product_sort_container(self):
        self.loginPage = LoginPage(self.driver)
        homePage = self.loginPage.do_login()
        homePage.product_sort_container()
        for getValue in Sort_Productss:
            sortingNames  = self.driver.find_element_by_xpath(
                     "//*[@class='product_sort_container']//option[contains(text(),'%s')]" % str(getValue.value))
            assert sortingNames.text == getValue.value

    @pytest.mark.order()
    def test_verify_shopping(self):
        self.loginPage = LoginPage(self.driver)
        homePage = self.loginPage.do_login()
        homePage.do_shopping()
        allure.attach(self.driver.get_screenshot_as_png(),attachment_type=AttachmentType.PNG)

    @pytest.mark.order()
    def test_verify_sorting_Zto_A(self):
        self.loginPage = LoginPage(self.driver)
        homePage = self.loginPage.do_login()
        homePage.product_sort_container()
        homePage.sort_product_High_to_Low()
        allure.attach(self.driver.get_screenshot_as_png(),attachment_type=AttachmentType.PNG)

    @pytest.mark.order()
    def test_verify_logout_into_app(self):
        self.loginPage = LoginPage(self.driver)
        homePage = self.loginPage.do_login()
        homePage.do_logout()
        allure.attach(self.driver.get_screenshot_as_png(),attachment_type=AttachmentType.PNG)


# import cv2
#
# filename="imgmirror.jpg"
# img= cv2.imread('image.jpg')
# res= img.copy()
# for i in range(img.shape[0]):
#     for j in range(img.shape[1]):
#         res[i][img.shape[1]-j-1]= img[i][j]
#
# cv2.imshow('image', res)
# cv2.imwrite(filename,res)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# import cv2
#
# img = cv2.imread("no entry.png")
#
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#
# cv2.imshow("image ori", img)
# cv2.imshow("image gray", gray)
# filename="noentrygray.jpg"
# cv2.imwrite(filename,gray)
# cv2.waitKey(0)

from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
from keras.layers import Dropout, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D
import numpy as np
import cv2

#############################################

#frameWidth = 640  # CAMERA RESOLUTION
#frameHeight = 480
#brightness = 180
#threshold = 0.75  # PROBABLITY THRESHOLD
font = cv2.FONT_HERSHEY_SIMPLEX
##############################################

# SETUP THE VIDEO CAMERA
cap = cv2.VideoCapture(0)
#cap.set(3, frameWidth)
#cap.set(4, frameHeight)
#cap.set(10, brightness)

imageDimesions = (32, 32, 3)
noOfClasses = 3

sampleNum=0

no_Of_Filters = 60
size_of_Filter = (5, 5)  # THIS IS THE KERNEL THAT MOVE AROUND THE IMAGE TO GET THE FEATURES.
# THIS WOULD REMOVE 2 PIXELS FROM EACH BORDER WHEN USING 32 32 IMAGE
size_of_Filter2 = (3, 3)
size_of_pool = (2, 2)  # SCALE DOWN ALL FEATURE MAP TO GERNALIZE MORE, TO REDUCE OVERFITTING
no_Of_Nodes = 500  # NO. OF NODES IN HIDDEN LAYERS

model = Sequential()
model.add((Conv2D(no_Of_Filters, size_of_Filter, input_shape=(imageDimesions[0], imageDimesions[1], 1),
                      activation='relu')))  # ADDING MORE CONVOLUTION LAYERS = LESS FEATURES BUT CAN CAUSE ACCURACY TO INCREASE
model.add((Conv2D(no_Of_Filters, size_of_Filter, activation='relu')))
model.add(MaxPooling2D(pool_size=size_of_pool))  # DOES NOT EFFECT THE DEPTH/NO OF FILTERS

model.add((Conv2D(no_Of_Filters // 2, size_of_Filter2, activation='relu')))
model.add((Conv2D(no_Of_Filters // 2, size_of_Filter2, activation='relu')))
model.add(MaxPooling2D(pool_size=size_of_pool))
model.add(Dropout(0.5))

model.add(Flatten())
model.add(Dense(no_Of_Nodes, activation='relu'))
model.add(Dropout(0.5))  # INPUTS NODES TO DROP WITH EACH UPDATE 1 ALL 0 NONE
model.add(Dense(noOfClasses, activation='softmax'))  # OUTPUT LAYER
    # COMPILE MODEL
model.compile(Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])

model.load_weights('91model.h5')

def grayscale(img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    return img

def equalize(img):
    img = cv2.equalizeHist(img)
    return img

def preprocessing(img):
    img = grayscale(img)
    img = equalize(img)
    img = img / 255
    return img

# def getCalssName(classNo):
#     if classNo == 0:
#         return 'No Entry'
#     elif classNo == 1:
#         return 'Turn Right'
#     elif classNo == 2:
#         return 'Turn Left'
#     elif classNo == 3:
#         return 'Go Ahead'

# cascLeft = "all.xml"
# cascRight = "all.xml"
# cascStop = "all.xml"
cascLeft = "turnLeft_ahead.xml"
cascRight = "turnRight_ahead.xml"
cascStop = "stopsign_classifier.xml"
#speedLimit = "lbpCascade.xml"
leftCascade = cv2.CascadeClassifier(cascLeft)
rightCascade = cv2.CascadeClassifier(cascRight)
stopCascade = cv2.CascadeClassifier(cascStop)
#speedCascade = cv2.CascadeClassifier(speedLimit)

video_capture = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    left = leftCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30)
    )
    right = rightCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30)
    )
    stop = stopCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30)
    )
    # speed = speedCascade.detectMultiScale(
    #     gray,
    #     scaleFactor=1.1,
    #     minNeighbors=5,
    #     minSize=(30, 30)
    # )

    # Draw a rectangle around the faces
    for (x, y, w, h) in left:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        roi_gray = gray[y:y + h, x:x + w]
        cropped_img = np.expand_dims(np.expand_dims(cv2.resize(roi_gray, (32, 32)), -1), 0)
        prediction = model.predict(cropped_img)
        #sampleNum = sampleNum + 1
        rambu = ('Stop', 'Turn Right', 'Turn Left')
        maxindex = rambu[int(np.argmax(prediction))]

        cv2.putText(frame, maxindex, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
        #cv2.imwrite("TrainingImage\ " + str(sampleNum) + ".jpg", frame)
        # if probabilityValue > threshold:
        #     cv2.putText(frame, str(tessss) + "%", (x, y + h), cv2.FONT_HERSHEY_SIMPLEX, 1,
        #                 (0, 255, 0), 2, cv2.LINE_AA)

    for (x, y, w, h) in right:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        roi_gray = gray[y:y + h, x:x + w]
        cropped_img = np.expand_dims(np.expand_dims(cv2.resize(roi_gray, (32, 32)), -1), 0)
        prediction = model.predict(cropped_img)
        #sampleNum = sampleNum + 1
        rambu = ('Stop', 'Turn Right', 'Turn Left')
        maxindex = rambu[int(np.argmax(prediction))]

        cv2.putText(frame, maxindex, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
        #cv2.imwrite("TrainingImage\ " + str(sampleNum) + ".jpg", frame)
        #probabilityValue = np.amax(prediction)
        # if probabilityValue > threshold:
        #     cv2.putText(frame, str(round(probabilityValue * 100, 2)) + "%", (x, y+h), cv2.FONT_HERSHEY_SIMPLEX, 1,
        #                 (0, 255, 0), 2, cv2.LINE_AA)

    for (x, y, w, h) in stop:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        roi_gray = gray[y:y + h, x:x + w]
        cropped_img = np.expand_dims(np.expand_dims(cv2.resize(roi_gray, (32, 32)), -1), 0)
        prediction = model.predict(cropped_img)
        #sampleNum = sampleNum + 1
        rambu = ('Stop', 'Turn Right', 'Turn Left')
        maxindex = rambu[int(np.argmax(prediction))]

        cv2.putText(frame, maxindex, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
        #cv2.imwrite("TrainingImage\ " + str(sampleNum) + ".jpg", frame)

    # for (x ,y, w, h) in speed:
    #     cv2.rectangle(frame, (x ,y), (x+w, y+h), (0, 255, 0), 2)
    #     roi_gray = gray[y:y + h, x:x + w]
    #     cropped_img = np.expand_dims(np.expand_dims(cv2.resize(roi_gray, (32, 32)), -1), 0)
    #     prediction = model.predict(cropped_img)
    #
    #     rambu = ('Stop', 'Turn Right', 'Turn Left', 'Max Speed 50')
    #     maxindex = rambu[int(np.argmax(prediction))]
    #
    #     cv2.putText(frame, maxindex, (x,y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)

    # Display the resulting frame
    cv2.imshow('Video', frame)

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

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()


import cv2
import time
import numpy as np
import supervision as sv#this is a Roboflow open source libray
from ultralytics import YOLO
from tqdm import tqdm #this is a tool for visualising progress bars in console.  Remove for production code as might slow things down

COLORS = sv.ColorPalette.default()

#Define entry and exit areas on image (got the cordinates by drawing zones using https://blog.roboflow.com/polygonzone/)
#Zone_in is garden bottom half and front of house bottom half - red colour
ZONE_IN_POLYGONS = [
    np.array([[640, 154],[0, 242],[0, 360],[640, 360]]),
    np.array([[650, 162],[986, 158],[990, 360],[646, 360]]),
]
#Zone_out is garden top half and front of house top half - green colour
ZONE_OUT_POLYGONS = [
    np.array([[642, 0],[978, 0],[982, 142],[654, 146]]),
    np.array([[0, 0],[634, 0],[638, 146],[2, 222]]),
]

def initiate_poylgon_zones(polygons:list[np.ndarray],frame_resolution_wh:tuple[int,int],triggering_position:sv.Position=sv.Position.CENTER)->list[sv.PolygonZone]:
    return[sv.PolygonZone(polygon,frame_resolution_wh,triggering_position)for polygon in polygons]

class DetectionsManager:
    def __init__(self) -> None:
        self.tracker_id_to_zone_id: Dict[int, str] = {}
        self.total_count: int = 5

    #update function takes the list of detections triggered by a zone and maps the tracker ID to either in or out
    def update(self,detections: sv.detection, detections_zone_in: list[sv.detection], detections_zone_out: list[sv.detection]) -> sv.detection:
        for detection in detections_zone_in:
            #print('Zone in detection  ', detection)
            if np.any(detection.tracker_id):#this tests if there are any tracker id's.  If not the for loop below crashes
                for tracker_id in detection.tracker_id:
                    if tracker_id in self.tracker_id_to_zone_id:
                        #print(self.tracker_id_to_zone_id[tracker_id])
                        if self.tracker_id_to_zone_id[tracker_id] == 'out':#if current value is out then this detection has crossed zones
                            self.total_count += 1 #add one to the count as an 'out' has become an 'in'
                            self.tracker_id_to_zone_id[tracker_id] = 'in' # and update zone in dictionary to reflect this
                    else:
                        self.tracker_id_to_zone_id[tracker_id] = 'in' #this means tracker ID is new so add to the dictionary

        for detection in detections_zone_out:
            #print('Zone out detections  ', detection)
            if np.any(detection.tracker_id): #this tests if there are any tracker id's.  If not the for loop below crashes
                for tracker_id in detection.tracker_id:
                    if tracker_id in self.tracker_id_to_zone_id:
                        #print(self.tracker_id_to_zone_id[tracker_id])
                        if self.tracker_id_to_zone_id[tracker_id] == 'in':#if current value is in then this detection has crossed zones
                            self.total_count -= 1 #minus one to the count as an 'in' has become an 'out'
                            self.tracker_id_to_zone_id[tracker_id] = 'out' # and update zone in dictionary to reflect this
                    else:
                        self.tracker_id_to_zone_id[tracker_id] = 'out' #this means tracker ID is new so add to the dictionary

        #Need new statement which filters the detections so it only shows those from within a zone - although not sure that matters for this use case as zones cover whole field of view
        #detections.class_id = np.vectorize(lambda x: self.tracker_id_to_zone_id.get(x, -1))(detections.tracker_id)#i don't understand what this is doing so need to come back to it

        return self.total_count

class VideoProcessor:
    def __init__(self, source_weights_path: str, source_video_path: str, target_video_path: str = None,
        confidence_threshold: float = 0.1, iou_threshold: float = 0.7,) -> None:
        self.source_weights_path = source_weights_path
        self.conf_threshold = confidence_threshold
        self.iou_threshold = iou_threshold
        self.source_video_path = source_video_path
        self.target_video_path = target_video_path

        self.model = YOLO(self.source_weights_path)
        self.tracker = sv.ByteTrack()
        self.box_annotator = sv.BoxAnnotator(color=COLORS)
        self.trace_annotator = sv.TraceAnnotator(color=COLORS, position=sv.Position.CENTER, trace_length=100, thickness=2)
        self.video_info = sv.VideoInfo.from_video_path(source_video_path)
        self.video_info.fps = 25  # setting the frames per second for writing the video to 25 instead of 30 as original cameras are at 25fps
        print(self.video_info)
        self.zone_in = initiate_poylgon_zones(ZONE_IN_POLYGONS,self.video_info.resolution_wh,sv.Position.CENTER)
        self.zone_out = initiate_poylgon_zones(ZONE_OUT_POLYGONS,self.video_info.resolution_wh,sv.Position.CENTER)
        self.detections_manager = DetectionsManager()

    def process_video(self):
        frame_generator = sv.get_video_frames_generator(self.source_video_path)
        if self.target_video_path:
            with sv.VideoSink(self.target_video_path, self.video_info) as f:
                for frame in tqdm(frame_generator, total=self.video_info.total_frames):
                    t1 = cv2.getTickCount()
                    processed_frame = self.process_frame(frame)
                    t2 = cv2.getTickCount()
                    ticks_taken = (t2 - t1) / cv2.getTickFrequency()
                    FPS = 1 / ticks_taken
                    cv2.putText(processed_frame, 'FPS: {0:.2f}'.format(FPS), (30, 50), cv2.FONT_HERSHEY_SIMPLEX, 1,(255, 255, 0), 2, cv2.LINE_AA)
                    f.write_frame(processed_frame)

        else:
            for frame in frame_generator:
                t1 = cv2.getTickCount()
                processed_frame = self.process_frame(frame)
                t2 = cv2.getTickCount()
                ticks_taken = (t2 - t1) / cv2.getTickFrequency()
                FPS = 1 / ticks_taken
                cv2.putText(processed_frame,'FPS: {0:.2f}'.format(FPS), (30, 50), cv2.FONT_HERSHEY_SIMPLEX, 1,(255, 255, 0), 2, cv2.LINE_AA)
                cv2.imshow("Count of Customers Indoors", processed_frame)
                if cv2.waitKey(1) & 0xFF ==ord("q"):
                    break
            cv2.destroyAllWindows()

    def process_frame(self,frame: np.ndarray)-> np.ndarray:
        #consider resizing the frame tp 180x640 for both training and inference to see of this speeds things up
        result = self.model(frame, verbose = False, conf=self.conf_threshold,iou=self.iou_threshold)[0]#add the device parameter to run this on the Mac's GPU which sognificantly speeds up inference
        detections = sv.Detections.from_ultralytics(result)#pass the YOLO8 inference results through supervision to use their detections object which is easier to process
        detections = detections[detections.class_id == 0]#filter the list of detections so it only shows category '0' which is people
        detections = self.tracker.update_with_detections(detections)#pass the detections through the tracker to add tracker ID as additional field to detections object

        #filter out detections not triggered within a zone and add the deteections to lists for zone in and zone out
        detections_zone_in = []
        detections_zone_out = []
        for zone_in, zone_out in zip(self.zone_in,self.zone_out):
            detection_zone_in = detections[zone_in.trigger(detections)]#this is an Supervision function to test if a detection occured within a zone
            detections_zone_in.append(detection_zone_in)
            detection_zone_out = detections[zone_out.trigger(detections)]#this is an Supervision function to test if a detection occured within a zone
            detections_zone_out.append(detection_zone_out)

        total_count = self.detections_manager.update(detections,detections_zone_in,detections_zone_out)#call to the detections manager class 'rules engine' for working out which zone a detection was triggered in

        return self.annotate_frame(frame,detections,total_count)

    def annotate_frame(self,frame: np.ndarray, detections: sv.Detections,total_count:int)-> np.ndarray:
        annotated_frame = frame.copy()
        for i,(zone_in,zone_out) in enumerate(zip(self.zone_in,self.zone_out)):#use enumerate so you get the index [i] automatically
            annotated_frame = sv.draw_polygon(annotated_frame,zone_in.polygon,COLORS.colors[0])#draw zone in polygons
            annotated_frame = sv.draw_polygon(annotated_frame,zone_out.polygon,COLORS.colors[1])#draw zone out polygons

        if detections:#need to check some detections are found before adding annotations, otherwise list comprehension below breaks
            labels = [f"#{tracker_id}" for tracker_id in detections.tracker_id]#list comprehension to return list of tracker_ID's to use in label
            annotated_frame = self.box_annotator.annotate(annotated_frame,detections,skip_label=True)#add in labels = labels if want tracker ID annotated on frame
            annotated_frame = self.trace_annotator.annotate(annotated_frame,detections)

        annotated_frame = sv.draw_text(scene=annotated_frame, text="Count of People Currently In", text_anchor=sv.Point(x=1130, y=150), text_scale=0.6, text_thickness=1,background_color=COLORS.colors[0])
        annotated_frame = sv.draw_text(scene=annotated_frame,text=str(total_count),text_anchor=sv.Point(x=1118, y=226),text_scale=2,text_thickness=5,background_color=COLORS.colors[0],text_padding=40)

        return annotated_frame

processor = VideoProcessor(
        source_weights_path='yolov8nPeopleCounterV2.pt',
        source_video_path='/Users/tobieabel/Desktop/video_frames/Youtube/v3_a demo.mp4',
        #target_video_path='/Users/tobieabel/Desktop/video_frames/Youtube/v3_b demo_annotated.mp4',
        )
processor.process_video()


# -*- coding: utf-8 -*-
import json
import requests
import os
import time
import log21
from kafka import KafkaConsumer

access_token = os.environ.get("ACCESS_TOKEN")
kafka_host = os.environ.get("KAFKA_HOST")
kafka_port = os.environ.get("KAFKA_PORT", "9092")
kafka_topic = os.environ.get("KAFKA_TOPIC")

def dingtalk_robot(text):
    url = "https://oapi.dingtalk.com/robot/send?access_token=" + access_token

    headers = {'Content-Type': 'application/json'}

    data_dict = {
        "msgtype": "markdown",
        "markdown": {
            "title": "日志告警",
            "text": text
        }
    }

    json_data = json.dumps(data_dict)
    response = requests.post(url, data=json_data, headers=headers)
    print(response.text)  # {"errcode":0,"errmsg":"ok"}

def test_to_json(message):
    data = json.loads(message, strict=False)
    return data.get('text').get('content')

def kafka_to_dingtalk():
    if kafka_port == '':
        bootstrap_server = '{}:{}'.format(kafka_host,'9092')
    else:
        bootstrap_server = '{}:{}'.format(kafka_host, kafka_port)
    consumer = KafkaConsumer(
        kafka_topic,
        bootstrap_servers=bootstrap_server,
        auto_offset_reset='latest',
        api_version=(0, 10, 2)
    )
    log21.print(type(consumer))
    for msg in consumer:
        dingtalk_massage = test_to_json(msg.value.decode())
        time.sleep(4)
        dingtalk_robot(dingtalk_massage)

if __name__ == '__main__':
    if access_token == '':
        log21.print(log21.get_color('#FF0000') + '未提供钉钉机器人ACCESS_TOKEN' )
    if kafka_host == '':
        log21.print(log21.get_color('#FF0000') + '未配置Kafka的环境变量KAFKA_HOST' )
    if kafka_host == '':
        log21.print(log21.get_color('#FF0000') + '未配置Kafka的环境变量KAFKA_TOPIC' )
    kafka_to_dingtalk()


#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2022/9/12 2:08 下午
# @Author  : LiangJun
# @Filename    : test_demo2.py

import unittest
from ddt import ddt, data

test_datas = [
    {'id': 1, 'title': '测试用例1'},
    {'id': 2, 'title': '测试用例2'},
    {'id': 3, 'title': '测试用例3'}
]

@ddt
class TestDemo(unittest.TestCase):

    @data(*test_datas)
    def test_demo1(self, i):
        print(i)


import json
from django.http import HttpResponse

__author__ = 'diraven'

class HttpResponseJson(HttpResponse):
    def __init__(self, data=None, is_success=False, message=''):
        response_data = {
            'data': data,
            'message': message,
            'success': is_success
        }
        super(HttpResponseJson, self).__init__(json.dumps(response_data), content_type="application/json")


import time
import picamera
import sqlite3
import signal
import os
import shutil

pidDB = sqlite3.connect('/home/pi/System/PID.db')
pidCursor = pidDB.cursor()
actualPID = os.getpid()
print("I'm PID " + str(actualPID))
pidCursor.execute("""UPDATE PID SET value = ? WHERE name = ?""", (actualPID, "camera"))
pidDB.commit()

"""Function to take timelapse"""
def CameraFootage(signum, stack):
	print("Received:" + str(signum))
	if signum == 10:
		print("Beginning timelapse")
		with picamera.PiCamera() as camera:
			camera.start_preview()
			camera.annotate_text = time.strftime('%Y-%m-%d %H:%M:%S')
			time.sleep(1)
			shutil.rmtree('/home/dev/www/public/media/')
			os.mkdir('/home/dev/www/public/media')
			i = 0
			for filename in camera.capture_continuous('/home/dev/www/public/media/img{counter:03d}.jpg'):
				if i < 20:
					print("Captured %s" %filename)
					time.sleep(1)
					i = i +1
				else:
					i = 0
					break

signal.signal(signal.SIGUSR1, CameraFootage)

while True:
	time.sleep(3)


# -*- coding: utf-8 -*-
import pytest
from nameko.testing.utils import get_extension
from nameko.testing.waiting import wait_for_call

from nameko_grpc.client import Client
from nameko_grpc.entrypoint import GrpcServer

class TestCloseSocketOnClientExit:
    @pytest.fixture(params=["server=nameko"])
    def server_type(self, request):
        return request.param[7:]

    def test_close_socket(self, server, load_stubs, spec_dir, grpc_port, protobufs):
        """Regression test for https://github.com/nameko/nameko-grpc/issues/39"""
        stubs = load_stubs("example")

        client = Client(
            "//localhost:{}".format(grpc_port),
            stubs.exampleStub,
            "none",
            "high",
            False,
        )
        proxy = client.start()

        container = server
        grpc_server = get_extension(container, GrpcServer)
        connection_ref = grpc_server.channel.conn_pool.connections.queue[0]
        connection = connection_ref()

        response = proxy.unary_unary(protobufs.ExampleRequest(value="A"))
        assert response.message == "A"

        with wait_for_call(connection.sock, "close"):
            client.stop()


#! -*- coding=utf-8 -*-
import os
import sys

filepath = os.path.abspath(__file__)
sys.path.append(os.path.dirname(os.path.dirname(filepath)))

import threading
import time

from datetime import datetime
from multiprocessing import Process
from machines.machineVPN import MachineVPN
# from machines.machineWujiVPN import MachineVPN
from machines.machineXposeHook import MachineXHook as Machine008
from appium4droid import webdriver
from bootstrap import setup_boostrap
from TotalMachine import WorkMachine
from appium4droid.support.ui import WebDriverWait
from machines.StateMachine import Machine
import random
import requests
import re

class TotalMachine(WorkMachine):
    def load_task_info(self):
        return []

    def setup_machine(self):
        dr = self.driver
        self.machine008 = Machine008(dr)
        self.machine008.task_schedule = ["record_file", "clear_data", "modify_data_suiji"]  # 007 task list
        self.appname = "testsdk"

    def main_loop(self):
        dr = self.driver
        m008 = self.machine008
        while True:
            try:
                dr.press_keycode(3)
                time.sleep(1)
                dr.press_keycode(3)
                time.sleep(1)
                #清后台
                # dr.press_keycode(82)
                # time.sleep(1)
                # WebDriverWait(dr, 10).until(lambda d: d.find_element_by_id("com.android.systemui:id/clearButton")).click()
                # time.sleep(1)
                MachineVPN(dr).run()
                m008.run()
                # dr.press_keycode(3)
                # time.sleep(1)
                # dr.press_keycode(3)
                # time.sleep(1)
                # WebDriverWait(dr, 30).until(lambda d: d.find_element_by_name(self.appname)).click()
                # time.sleep(5)
                # 开启加速
                # dr.press_keycode(3)
                # time.sleep(1)
                # WebDriverWait(dr, 30).until(lambda d: d.find_element_by_name("GMD Speed Time")).click()
                # time.sleep(1)
                # WebDriverWait(dr, 30).until(lambda d: d.find_element_by_id("com.gmd.speedtime:id/buttonStart")).click()
                # time.sleep(2)
                dr.press_keycode(3)
                time.sleep(1)
                WebDriverWait(dr, 30).until(lambda d: d.find_element_by_name(self.appname)).click()
                time.sleep(15)
                #记录ip
                self.log_ip()
                dr.press_keycode(3)
                time.sleep(5)
                WebDriverWait(dr, 30).until(lambda d: d.find_element_by_name(self.appname)).click()
                time.sleep(1)
                #关闭加速
                # dr.press_keycode(3)
                # time.sleep(1)
                # WebDriverWait(dr, 30).until(lambda d: d.find_element_by_name("嘀嗒拼车")).click()
                # time.sleep(5)
                # WebDriverWait(dr, 30).until(lambda d: d.find_element_by_id("com.gmd.speedtime:id/buttonStop")).click()
                # time.sleep(1)
                # dr.press_keycode(3)
                # time.sleep(1)
            except Exception as e:
                print("somting wrong")
                print(e)
            finally:
                pass
            print("Again\n")
        return self.exit

    def log_ip(self):
        WEB_URL = 'http://ip.chinaz.com/getip.aspx'
        r = requests.get(WEB_URL)
        print(r.text)
        match = re.search(r'ip:\'(.+)\'\,address:\'(.+)\'', r.text)
        if match:
            print(match.group(1))
            print(match.group(2))
            ip = match.group(1)
            addr = match.group(2)
            with open('/sdcard/1/ip.log', 'a') as f:
                f.write('\n%s  %s' % (ip, addr))

if __name__ == "__main__":
        TM = TotalMachine()
        TM.run()


## LESSON 6 Q1: AUDITING - ITERATIVE PARSING/SAX PARSE using ITERPARSE

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Your task is to use the iterative parsing to process the map file and
find out not only what tags are there, but also how many, to get the
feeling on how much of which data you can expect to have in the map.
The output should be a dictionary with the tag name as the key
and number of times this tag can be encountered in the map as value.

Note that your code will be tested with a different data file than the 'example.osm'
"""
import xml.etree.ElementTree as ET
import pprint

def count_tags(filename):
        # YOUR CODE HERE
    tagdict = {}
    for event, elem in ET.iterparse(filename):
        try:
            if elem.tag in tagdict:
                tagdict[elem.tag] += 1
            else:
                tagdict[elem.tag] = 1
            elem.clear()
        except 'NoneType':
            pass
    return tagdict

def test():

    tags = count_tags('examples.osm')
    pprint.pprint(tags)
    assert tags == {'bounds': 1,
                     'member': 3,
                     'nd': 4,
                     'node': 20,
                     'osm': 1,
                     'relation': 1,
                     'tag': 7,
                     'way': 1}

if __name__ == "__main__":
    test()


url = "http://dantri.com.vn/"

output_file_name = "news.xlsx"

#Step 1: Download information on the Dantri website
from urllib.request import urlopen
from bs4 import BeautifulSoup

#1.1: Open a connection
conn = urlopen(url)

#1.2: read
raw_data = conn.read()   #byte

#1.3: Decode
html_content = raw_data.decode('utf-8')

# Faster way
# from urllib.request import urlopen
# html_content = urlopen(url).read().decode('utf-8')
# print(html_content)
# print(html_content)

#How to save html_content as a file (in case internet is weak)
# html_file = open("dantri.html","wb")            #write: byte
# html_file.write(raw_data)
# html_file.close()

#Step 2: Extract ROI (Region of interest)

#Create a soup
soup = BeautifulSoup(html_content, "html.parser")
# print(soup.prettify)
ul = soup.find("ul", "ul1 ulnew")
# find chi dung cho tim 1 cai
# print(ul.prettify())
li_list = ul.find_all("li")
#find_all dung cho tim tat ca
# for li in li_list:
#     print(li)
#     print("***" * 10)

#Step 3: Extract News
news_list = []

for li in li_list:
# li = li_list[0]
# h4 = li.h4      #h4 = li.find("h4")
# a = h4.a

#better way:
# a = li.h4.a   (or li.a)
    a = li.h4.a
    href = url + a["href"]
    title = a.string
    news = {
        "title": title,
        "link": href
    }
    news_list.append(news)

print(news_list)


import xlrd
import xlwt
from featureComp import *
from createmat import *

def findRank(path2):
	for i in range(1,39):
		path3=path2+str(i)+'.xlsx'
		matchday=xlrd.open_workbook(path3)
		sheet1=matchday.sheet_by_index(0)
		#print path3,'\n'
		for j in range(1,21):
			team_rank[sheet1.cell(j,2).value.strip()].append(sheet1.cell(j,0).value)

def resetMatches(matches_played):
    for k in matches_played.keys():
        matches_played[k]=0
teams={}
teamprofile={}
matches_played={}
team_rank={}
train_book=xlwt.Workbook()
sheet1=train_book.add_sheet("sheet 1")
book = xlrd.open_workbook("Season_table.xlsx")
first_sheet = book.sheet_by_index(0)
form_table=([0.75,0.15,20],[0.6,0.25,16],[0.4,0.4,12],[0.15,0.6,10])
for i in range(1,37):
	teams[first_sheet.cell(i,0).value.strip()]=[]
	teamprofile[first_sheet.cell(i,0).value.strip()]=[]
	matches_played[first_sheet.cell(i, 0).value.strip()]=0
	team_rank[first_sheet.cell(i,0).value.strip()]=[]
num=2005
match=1
featureobj=Feature()
for j in range(10):
	path='Fixtures/'+str(num)+'.xlsx'
	path2='Match Days/'+str(num)+'/Match'
	fbook=xlrd.open_workbook(path)
	first_sheet = fbook.sheet_by_index(0)
	findRank(path2)
	AQDQmat(first_sheet,teams)
	FORMmat(first_sheet, team_rank, teams, matches_played, form_table)
	resetMatches(matches_played)
	featureobj.featureCompute(first_sheet,sheet1,teams,matches_played,teamprofile)
	num+=1

train_book.save("training.xls")

rtrain_book=xlrd.open_workbook('training.xlsx')
svmdatasheet=rtrain_book.sheet_by_index(0)
with open('svmdataformat', 'w') as f:
    featureobj.SVMformat(svmdatasheet,f)
f.closed
'''
for k,v in teams.iteritems():
	print k
	print '------------------'
	print v
'''
teamslist=[]
for i in range(1,37):
	for j in (9,10):
		if int(book.sheet_by_index(0).cell(i,j).value)==1:
			teamslist.append(book.sheet_by_index(0).cell(i,0).value.strip())

for names in teamslist:
	train_book=xlwt.Workbook()
	sheet1=train_book.add_sheet("sheet 1")
	for i in range(len(teamprofile[names])):
		for j in range(4):
			sheet1.row(i).write(j,teamprofile[names][i][j])

	train_book.save(str(names)+".xlsx")


from mock import Mock

import pytest

import json

from datetime import date, datetime
from src.smallquery.functions.run_query import app

@pytest.fixture()
def run_query_event():
    """
    Generates Run Query event
    """
    return {
        'query': 'select * from unit/test.parquet',
        'limit': 10,
    }

def test_results_serializer():
    a_date = date(year=2020, month=11, day=10)
    a_date_time = datetime(year=2021, month=6, day=24,
                           hour=13, minute=3, second=12, microsecond=2323)
    a_str = 'ksjdf'
    a_int = 78
    data = {
        'some_date': a_date,
        'some_date_time': a_date_time,
        'some_string': a_str,
        'some_int': a_int,
    }
    expected_json = '{"some_date": "2020-11-10", "some_date_time": "2021-06-24T13:03:12.002323", "some_string": "ksjdf", "some_int": 78}'

    actual_json = json.dumps(data, default=app.results_serializer)

    assert expected_json == actual_json

def test_handler(run_query_event):
    ensure_db_connected_mock = Mock()
    run_query_mock = Mock()
    app.ensure_db_connected = ensure_db_connected_mock
    app.run_query = run_query_mock

    app.lambda_handler(run_query_event, None)

    assert ensure_db_connected_mock.call_count == 1
    assert run_query_mock.call_count == 1