#!/usr/bin/env python
# encoding:utf8

"""
    unittests for BASIC parser
    ==========================

    :created: 2014 by Jens Diemer - www.jensdiemer.de
    :copyleft: 2014 by the DragonPy team, see AUTHORS for more details.
    :license: GNU GPL v3 or above, see LICENSE for more details.
"""

from __future__ import absolute_import, division, print_function

import logging
import sys
import unittest

from dragonlib.core.basic_parser import BASICParser

log = logging.getLogger(__name__)

class TestBASICParser(unittest.TestCase):
    def setUp(self):
        self.parser = BASICParser()

    def assertParser(self, ascii_listing, reference, print_parsed_lines=False):
        '''
        parse the given ASCII Listing and compare it with the reference.

        Used a speacial representation of the parser result for a human
        readable compare. Force using of """...""" to supress escaping apostrophe
        '''
        parsed_lines = self.parser.parse(ascii_listing)

        string_dict = {}
        for line_no, code_objects in list(parsed_lines.items()):
            string_dict[line_no] = [repr(code_object) for code_object in code_objects]

        if print_parsed_lines:
            print("-" * 79)
            print("parsed lines:", parsed_lines)
            print("-" * 79)
            print("reference:", reference)
            print("-" * 79)

        self.assertEqual(string_dict, reference)

    def test_only_code(self):
        ascii_listing = """
            10 CLS
            20 PRINT
        """
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:CLS>""",
                ],
                20: [
                    """<CODE:PRINT>""",
                ],
            },
            #             print_parsed_lines=True
        )

    def test_string(self):
        ascii_listing = '10 A$="A STRING"'
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:A$=>""",
                    """<STRING:"A STRING">""",
                ],
            },
            #             print_parsed_lines=True
        )

    def test_strings(self):
        ascii_listing = """
            10 A$="1":B=2:C$="4":CLS:PRINT "ONLY CODE"
        """
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:A$=>""",
                    """<STRING:"1">""",
                    """<CODE::B=2:C$=>""",
                    """<STRING:"4">""",
                    """<CODE::CLS:PRINT >""",
                    """<STRING:"ONLY CODE">""",
                ],
            },
            #             print_parsed_lines=True
        )

    def test_string_and_comment(self):
        ascii_listing = """
            10 A$="NO :'REM" ' BUT HERE!
        """
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:A$=>""",
                    """<STRING:"NO :'REM">""",
                    """<CODE: '>""",
                    """<COMMENT: BUT HERE!>""",
                ],
            },
            #             print_parsed_lines=True
        )

    def test_string_not_terminated(self):
        ascii_listing = """
            10 PRINT "NOT TERMINATED STRING
        """
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:PRINT >""",
                    """<STRING:"NOT TERMINATED STRING>""",
                ],
            },
            #             print_parsed_lines=True
        )

    def test_data(self):
        ascii_listing = """
            10 DATA 1,2,A,FOO
        """
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:DATA>""",
                    """<DATA: 1,2,A,FOO>""",
                ],
            },
            #             print_parsed_lines=True
        )

    def test_data_with_string1(self):
        ascii_listing = """
            10 DATA 1,2,"A","FOO BAR",4,5
        """
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:DATA>""",
                    """<DATA: 1,2,"A","FOO BAR",4,5>""",
                ],
            },
            #             print_parsed_lines=True
        )

    def test_data_string_colon(self):
        ascii_listing = """
            10 DATA "FOO : BAR"
        """
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:DATA>""",
                    """<DATA: "FOO : BAR">""",
                ],
            },
            #             print_parsed_lines=True
        )

    def test_code_after_data(self):
        ascii_listing = """
            10 DATA "FOO : BAR":PRINT 123
        """
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:DATA>""",
                    """<DATA: "FOO : BAR">""",
                    """<CODE::PRINT 123>""",
                ],
            },
            #             print_parsed_lines=True
        )

    def test_comment(self):
        ascii_listing = """
            10 REM A COMMENT
        """
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:REM>""",
                    """<COMMENT: A COMMENT>""",
                ],
            },
            #             print_parsed_lines=True
        )

    def test_nothing_after_comment1(self):
        ascii_listing = """
            10 REM
        """
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:REM>""",
                ],
            },
            #             print_parsed_lines=True
        )

    def test_nothing_after_comment2(self):
        ascii_listing = """
            10 '
        """
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:'>""",
                ],
            },
            #             print_parsed_lines=True
        )

    def test_no_code_after_comment(self):
        ascii_listing = """
            10 REM FOR "FOO : BAR":PRINT 123
        """
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:REM>""",
                    """<COMMENT: FOR "FOO : BAR":PRINT 123>""",
                ],
            },
            #             print_parsed_lines=True
        )

    def test_comment2(self):
        ascii_listing = """
            10 A=2 ' FOR "FOO : BAR":PRINT 123
        """
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:A=2 '>""",
                    """<COMMENT: FOR "FOO : BAR":PRINT 123>""",
                ],
            },
            #             print_parsed_lines=True
        )

    def test_no_comment(self):
        ascii_listing = """
            10 B$="'"
        """
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:B$=>""",
                    """<STRING:"'">""",
                ],
            },
            #             print_parsed_lines=True
        )

    def test_spaces_after_line_no(self):
        ascii_listing = """
            10 FOR I=1 TO 3:
            20     PRINT I
            30 NEXT
        """
        self.assertParser(ascii_listing,
            {
                10: [
                    """<CODE:FOR I=1 TO 3:>""",
                ],
                20: [
                    """<CODE:    PRINT I>""",
                ],
                30: [
                    """<CODE:NEXT>""",
                ],
            },
#             print_parsed_lines=True
        )

if __name__ == "__main__":
    from dragonlib.utils.logging_utils import setup_logging

    setup_logging(
#         level=1 # hardcore debug ;)
#         level=10  # DEBUG
#         level=20  # INFO
#         level=30  # WARNING
#         level=40 # ERROR
        level=50 # CRITICAL/FATAL
    )

    unittest.main(
        argv=(
            sys.argv[0],
#             "TestBASICParser.test_spaces_after_line_no",
        ),
        #         verbosity=1,
        verbosity=2,
        #         failfast=True,
    )
    print(" --- END --- ")


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

import numpy as np
import random

from torch.nn.modules.module import T

def default_conv(in_channels, out_channels, kernel_size, bias=True):
    return nn.Conv2d(
        in_channels, out_channels, kernel_size,
        padding=(kernel_size // 2), bias=bias)

class invPixelShuffle(nn.Module):

    def __init__(self, ratio=2):
        super(invPixelShuffle, self).__init__()
        self.ratio = ratio

    def forward(self, tensor):
        ratio = self.ratio
        b = tensor.size(0)
        ch = tensor.size(1)
        y = tensor.size(2)
        x = tensor.size(3)
        assert x % ratio == 0 and y % ratio == 0, 'x, y, ratio : {}, {}, {}'.format(x, y, ratio)

        return tensor.view(b, ch, y // ratio, ratio, x // ratio, ratio).permute(0, 1, 3, 5, 2, 4).contiguous().view(b, -1, y // ratio, x // ratio)

class ExtractFea(torch.nn.Module):
    def __init__(self, channels):
        super(ExtractFea, self).__init__()
        self.channels = channels

        self.conv1 = nn.Conv2d(in_channels=4, out_channels=self.channels, kernel_size=1, padding=0)
        self.conv2 = nn.Conv2d(in_channels=self.channels, out_channels=self.channels, kernel_size=3, padding=1)
        self.conv3 = nn.Conv2d(in_channels=self.channels, out_channels=self.channels, kernel_size=3, padding=1)
        self.conv4 = nn.Conv2d(in_channels=self.channels, out_channels=self.channels, kernel_size=3, padding=1)

    def forward(self, frame):
        f0 = F.relu(self.conv1(frame))
        f1 = F.relu(self.conv2(f0))
        f2 = F.relu(self.conv3(f1))
        out = self.conv4(f2)
        # out = self.conv2(f0)
        return out

class blockNL(torch.nn.Module):
    def __init__(self, channels, fs):
        super(blockNL, self).__init__()
        self.channels = channels
        self.fs = fs
        # self.ExtractFea = ExtractFea(channels=self.channels)
        self.softmax = nn.Softmax(dim=-1)

        self.t = nn.Conv2d(in_channels=self.channels, out_channels=self.channels, kernel_size=1, stride=1, bias=False)
        self.p = nn.Conv2d(in_channels=self.channels, out_channels=self.channels, kernel_size=1, stride=1, bias=False)
        self.g = nn.Conv2d(in_channels=self.channels, out_channels=self.channels, kernel_size=1, stride=1, bias=False)
        self.w = nn.Conv2d(in_channels=self.channels, out_channels=self.channels, kernel_size=1, stride=1, bias=False)

    def forward(self, x):

        # x_fea = self.ExtractFea(x)

        x_fea = x

        theta = self.t(x_fea).permute(0, 2, 3, 1)#.contiguous()#[b, c, h, w]#[b, h, w,c]
        theta = torch.unsqueeze(theta, dim=-2)  # [b, h, w, 1, c]
        # print(theta.size())

        phi = self.p(x_fea)#[b, c, h, w]
        b, c, h, w = phi.size()
        phi_patches = F.unfold(phi, self.fs, padding=self.fs//2)#[b, c*fs*fs, hw]
        phi_patches = phi_patches.view(b, c, self.fs * self.fs, -1)#[b, c, fs*fs, hw]
        phi_patches = phi_patches.view(b, c, self.fs * self.fs, h, w)  #[b, c, fs*fs, h, w]
        phi_patches = phi_patches.permute(0, 3, 4, 1, 2)#.contiguous()#[b, h, w, c, fs*fs]
        # print(phi_patches.size())

        att = torch.matmul(theta, phi_patches)# [b, h, w, 1, fs*fs]
        att = self.softmax(att)# [b, h, w, 1, fs*fs]
        # print(att.size())

        g = self.g(x_fea) #[b, 3, h, w]
        g_patches = F.unfold(g, self.fs, padding=self.fs // 2)#[b, 3*fs*fs, hw]
        g_patches = g_patches.view(b, 4, self.fs * self.fs, -1)#[b, 3, fs*fs, hw]
        g_patches = g_patches.view(b, 4, self.fs * self.fs, h, w)#[b, 3, fs*fs, h, w]
        g_patches = g_patches.permute(0, 3, 4, 2, 1)#.contiguous()#[b, h, w, fs*fs, 3]
        # print(g_patches.size())

        out_x = torch.matmul(att, g_patches)  # [1, h, w, 1, 3]
        out_x = torch.squeeze(out_x, dim=-2)# [1, h, w, 3]
        out_x = out_x.permute(0, 3, 1, 2)#.contiguous()
        # print(alignedframe.size())
        return self.w(out_x) + x

class Conv_up(nn.Module):
    def __init__(self, c_in, mid_c, up_factor):
        super(Conv_up, self).__init__()

        body = [nn.Conv2d(in_channels=c_in, out_channels=mid_c, kernel_size=3, padding=3 // 2), nn.ReLU(),
                # nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=3 // 2), nn.ReLU(),
                # nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=3 // 2), nn.ReLU(),
                ]
        self.body = nn.Sequential(*body)
        conv = default_conv
        ## x3 00
        ## x2 11
        if up_factor == 2:
            modules_tail = [
                # nn.ConvTranspose2d(64, 64, kernel_size=3, stride=up_factor, padding=1, output_padding=1),
                nn.Upsample(scale_factor=2),
                conv(mid_c, c_in,3),
                conv(c_in, c_in, 3)]

        elif up_factor == 3:
            modules_tail = [
                # nn.ConvTranspose2d(64, 64, kernel_size=3, stride=up_factor, padding=0, output_padding=0),
                nn.Upsample(scale_factor=3),
                conv(mid_c, c_in,3),
                conv(c_in, c_in, 3)]

        elif up_factor == 4:
            modules_tail = [
                # nn.ConvTranspose2d(64, 64, kernel_size=3, stride=2, padding=1, output_padding=1),
                # nn.ConvTranspose2d(64, 64, kernel_size=3, stride=2, padding=1, output_padding=1),
                nn.Upsample(scale_factor=4),
                conv(mid_c, c_in,3),
                conv(c_in, c_in, 3)]
        self.tail = nn.Sequential(*modules_tail)

    def forward(self, input):

        out = self.body(input)
        out = self.tail(out)
        return out

class Conv_down(nn.Module):
    def __init__(self, c_in,mid_c, up_factor):
        super(Conv_down, self).__init__()

        body = [nn.Conv2d(in_channels=c_in, out_channels=mid_c, kernel_size=3, padding=3 // 2), nn.ReLU(),
                # nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=3 // 2), nn.ReLU(),
                # nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=3 // 2), nn.ReLU(),
                ]
        self.body = nn.Sequential(*body)
        conv = default_conv
        if up_factor == 4:
            modules_tail = [
                # nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1, stride=2),
                # nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1, stride=2),
                nn.MaxPool2d(4),
                conv(mid_c, c_in,3),
                conv(c_in, c_in, 3)]

        elif up_factor == 3:
            modules_tail = [
                # nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1, stride=up_factor),
                nn.MaxPool2d(3),
                conv(mid_c, c_in,3),
                conv(c_in, c_in, 3)]

        elif up_factor == 2:
            modules_tail = [
                # nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1, stride=up_factor),
                nn.MaxPool2d(2),
                conv(mid_c, c_in,3),
                conv(c_in, c_in, 3)]
        self.tail = nn.Sequential(*modules_tail)

    def forward(self, input):

        out = self.body(input)
        out = self.tail(out)
        return out

class att_spatial(nn.Module):
    def __init__(self, res_num=2):
        super(att_spatial, self).__init__()

        block = [
            ConvBlock(2, 32, 3, 1, 1, activation='prelu', norm=None, bias=False),
        ]
        for i in range(res_num):
            block.append(ResnetBlock(32, 3, 1, 1, 0.1, activation='prelu', norm=None))
        self.block = nn.Sequential(*block)
        self.spatial = ConvBlock(2, 1, 3, 1, 1, activation='prelu', norm=None, bias=False)

    def forward(self, x):
        x = self.block(x)
        x_compress = torch.cat([torch.max(x, 1)[0].unsqueeze(1), torch.mean(x, 1).unsqueeze(1)], dim=1)
        x_out = self.spatial(x_compress)

        scale = torch.sigmoid(x_out)  # broadcasting
        return scale

class ConvBlock(torch.nn.Module):
    def __init__(self, input_size, output_size, kernel_size=3, stride=1, padding=1, bias=True, activation='prelu', norm=None, pad_model=None):
        super(ConvBlock, self).__init__()

        self.pad_model = pad_model
        self.norm = norm
        self.input_size = input_size
        self.output_size = output_size
        self.kernel_size = kernel_size
        self.stride = stride
        self.padding = padding
        self.bias = bias

        if self.norm =='batch':
            self.bn = torch.nn.BatchNorm2d(self.output_size)
        elif self.norm == 'instance':
            self.bn = torch.nn.InstanceNorm2d(self.output_size)

        self.activation = activation
        if self.activation == 'relu':
            self.act = torch.nn.ReLU(True)
        elif self.activation == 'prelu':
            self.act = torch.nn.PReLU(init=0.5)
        elif self.activation == 'lrelu':
            self.act = torch.nn.LeakyReLU(0.2, True)
        elif self.activation == 'tanh':
            self.act = torch.nn.Tanh()
        elif self.activation == 'sigmoid':
            self.act = torch.nn.Sigmoid()

        if self.pad_model == None:
            self.conv = torch.nn.Conv2d(self.input_size, self.output_size, self.kernel_size, self.stride, self.padding, bias=self.bias)
        elif self.pad_model == 'reflection':
            self.padding = nn.Sequential(nn.ReflectionPad2d(self.padding))
            self.conv = torch.nn.Conv2d(self.input_size, self.output_size, self.kernel_size, self.stride, 0, bias=self.bias)

    def forward(self, x):
        out = x
        if self.pad_model is not None:
            out = self.padding(out)

        if self.norm is not None:
            out = self.bn(self.conv(out))
        else:
            out = self.conv(out)

        if self.activation is not None:
            return self.act(out)
        else:
            return out

class ResnetBlock(torch.nn.Module):
    def __init__(self, input_size, kernel_size=3, stride=1, padding=1, bias=True, scale=1, activation='prelu', norm='batch', pad_model=None):
        super().__init__()

        self.norm = norm
        self.pad_model = pad_model
        self.input_size = input_size
        self.kernel_size = kernel_size
        self.stride = stride
        self.padding = padding
        self.bias = bias
        self.scale = scale

        if self.norm =='batch':
            self.normlayer = torch.nn.BatchNorm2d(input_size)
        elif self.norm == 'instance':
            self.normlayer = torch.nn.InstanceNorm2d(input_size)
        else:
            self.normlayer = None

        self.activation = activation
        if self.activation == 'relu':
            self.act = torch.nn.ReLU(True)
        elif self.activation == 'prelu':
            self.act = torch.nn.PReLU(init=0.5)
        elif self.activation == 'lrelu':
            self.act = torch.nn.LeakyReLU(0.2, True)
        elif self.activation == 'tanh':
            self.act = torch.nn.Tanh()
        elif self.activation == 'sigmoid':
            self.act = torch.nn.Sigmoid()
        else:
            self.act = None

        if self.pad_model == None:
            self.conv1 = torch.nn.Conv2d(input_size, input_size, kernel_size, stride, padding, bias=bias)
            self.conv2 = torch.nn.Conv2d(input_size, input_size, kernel_size, stride, padding, bias=bias)
            self.pad = None
        elif self.pad_model == 'reflection':
            self.pad = nn.Sequential(nn.ReflectionPad2d(padding))
            self.conv1 = torch.nn.Conv2d(input_size, input_size, kernel_size, stride, 0, bias=bias)
            self.conv2 = torch.nn.Conv2d(input_size, input_size, kernel_size, stride, 0, bias=bias)

        layers = filter(lambda x: x is not None, [self.pad, self.conv1, self.normlayer, self.act, self.pad, self.conv2, self.normlayer, self.act])
        # layers = filter(lambda x: x is not None, [self.pad, self.conv1, self.normlayer, self.act])
        self.layers = nn.Sequential(*layers)

    def forward(self, x):
        residual = x
        out = x
        out = self.layers(x)
        out = out * self.scale
        out = torch.add(out, residual)
        return out

class Net(nn.Module):
    def __init__(self, base_filter=None, args=None,num_channels=4,mid_channels=64, T = 4):
        super().__init__()

        print("now: pan_unfolding_V4")
        self.up_factor = 4
        G0 = mid_channels
        kSize = 3

        # T = 4
        # todo
        self.conv_u = nn.ModuleList([nn.Sequential(*[
            nn.Conv2d(4*(i+1), 64, kSize, padding=(kSize - 1) // 2, stride=1),
            nn.Conv2d(64, 4, kSize, padding=(kSize - 1) // 2, stride=1)
        ]) for i in range(T)])

        self.u = nn.ParameterList(
            [nn.Parameter(torch.tensor(0.5)) for _ in range(T)])
        self.eta = nn.ParameterList(
            [nn.Parameter(torch.tensor(0.5)) for _ in range(T)])
        self.gama = nn.ParameterList(
            [nn.Parameter(torch.tensor(0.5)) for _ in range(T)])
        self.delta = nn.ParameterList(
            [nn.Parameter(torch.tensor(0.1)) for _ in range(T)])

        # self.gama1 = nn.ParameterList(
        #     [nn.Parameter(torch.tensor(0.5)) for _ in range(T)])
        # self.delta1 = nn.ParameterList(
        #     [nn.Parameter(torch.tensor(0.1)) for _ in range(T)])
        # self.u1 = nn.ParameterList(
        #     [nn.Parameter(torch.tensor(0.5)) for _ in range(T)])

        self.conv_up = Conv_up(4, G0, self.up_factor)
        self.conv_down = Conv_down(4, G0, self.up_factor)

        self.rm1 = att_spatial(res_num=3)
        # self.rm2 = att_spatial(res_num=3)

        # # self.NLBlock = nn.ModuleList([blockNL(4, 15) for _ in range(T)])
        self.NLBlock = blockNL(4, 15)

        self.hf_pan = nn.Conv2d(3, 1, 1, padding=0, stride=1)

    def forward(self, lms, b_ms, pan): #l_ms,bms,pan
        # lms B 4 64 64
        # pan B 1 64 64

        hp_pan_2 = pan - F.interpolate(F.interpolate(pan, scale_factor=1/2, mode='bicubic'), scale_factor=2, mode='bicubic') # B 1 256 256

        hp_pan_4 = pan - F.interpolate(F.interpolate(pan, scale_factor=1/4, mode='bicubic'), scale_factor=4, mode='bicubic') # B 1 256 256

        hp_pan_8 = pan - F.interpolate(F.interpolate(pan, scale_factor=1/8, mode='bicubic'), scale_factor=8, mode='bicubic') # B 1 256 256

        pan_hp = self.hf_pan(torch.cat([hp_pan_2, hp_pan_4, hp_pan_8], dim=1))  # B 1 256 256

        hms = torch.nn.functional.interpolate(lms, scale_factor=self.up_factor, mode='bilinear', align_corners=False)   # B 4 256 256
        x = hms

        uk_list = []
        vk_list = []
        outs_list = []

        # decode_u_list = []

        for i in range(len(self.conv_u)):
            if i!=0:
                uk = self.conv_u[i](torch.cat(uk_list + [x], 1))         # B 4 256 256
            else:
                uk = self.conv_u[i](x)         # B 4 256 256
            # uk_list.append(uk)

            # denoising module
            rm1_s2_0 = pan_hp + self.rm1(torch.cat([torch.unsqueeze(uk[:,0,:,:],1), pan], 1)) * pan_hp  # B 1 256 256
            rm1_s2_1 = pan_hp + self.rm1(torch.cat([torch.unsqueeze(uk[:,1,:,:],1), pan], 1)) * pan_hp  # B 1 256 256
            rm1_s2_2 = pan_hp + self.rm1(torch.cat([torch.unsqueeze(uk[:,2,:,:],1), pan], 1)) * pan_hp  # B 1 256 256
            rm1_s2_3 = pan_hp + self.rm1(torch.cat([torch.unsqueeze(uk[:,3,:,:],1), pan], 1)) * pan_hp  # B 1 256 256
            decode_u = torch.cat([rm1_s2_0, rm1_s2_1, rm1_s2_2, rm1_s2_3], 1)  # B 4 256 256

            decode_u = decode_u + uk  # B 4 256 256
            uk_list.append(decode_u)

            # NARM
            NL = self.NLBlock(x)        # B 4 256 256
            # vk_list.append(NL)
            if i!=0:
                vk = self.conv_u[i](torch.cat(vk_list+[NL], 1))
            else:
                vk = self.conv_u[i](NL)         # B 4 256 256
            # vk = self.conv_u[i](torch.cat(vk_list, 1))         # B 4 256 256
            # denoising module
            rm2_s2_0 = pan_hp + self.rm1(torch.cat([torch.unsqueeze(vk[:,0,:,:],1), pan], 1)) * pan_hp  # B 1 256 256
            rm2_s2_1 = pan_hp + self.rm1(torch.cat([torch.unsqueeze(vk[:,1,:,:],1), pan], 1)) * pan_hp  # B 1 256 256
            rm2_s2_2 = pan_hp + self.rm1(torch.cat([torch.unsqueeze(vk[:,2,:,:],1), pan], 1)) * pan_hp  # B 1 256 256
            rm2_s2_3 = pan_hp + self.rm1(torch.cat([torch.unsqueeze(vk[:,3,:,:],1), pan], 1)) * pan_hp  # B 1 256 256
            decode_v = torch.cat([rm2_s2_0, rm2_s2_1, rm2_s2_2, rm2_s2_3], 1)  # B 4 256 256

            decode_v = decode_v + vk  # B 4 256 256
            vk_list.append(decode_v)

            # iteration
            x = x - self.delta[i]*(self.conv_up(self.conv_down(x)-lms+self.u[i]*(self.conv_down(NL)-lms))+self.eta[i]*(x-decode_u)+self.gama[i]*(NL-decode_v))

            outs_list.append(x)

        return outs_list[-1]# , uk_list[-1], vk_list[-1] # , decoder_list, fea_list

    def test(self, device='cpu'):
        total_params = sum(p.numel() for p in self.parameters())
        print(f'{total_params:,} total parameters.')
        total_trainable_params = sum(
            p.numel() for p in self.parameters() if p.requires_grad)
        print(f'{total_trainable_params:,} training parameters.')

        input_ms = torch.rand(1, 4, 64, 64)    # 196 为在Embeddings中的 n_patches （14 * 14）
        input_pan = torch.rand(1, 1, 256, 256)

        # ideal_out = torch.rand(1, 4, 256, 256)

        # out = self.forward(input_ms, None, input_pan)

        # assert out.shape == ideal_out.shape
        import torchsummaryX
        torchsummaryX.summary(self, input_ms.to(device), None, input_pan.to(device))

        #
        # from thop import profile
        # flops, params = profile(self, inputs=(input_ms,None, input_pan))
        # print("flops:", flops/1e9, "G")
        # print("params:", params/1e6, "M")

if __name__ == "__main__":

    net3 = pan_unfolding(T=3)
    net3.test()


from . import views
from .views import EditCommentView, DeleteCommentView
from django.urls import path

urlpatterns = [
    path('', views.PostList.as_view(), name='home'),
    path('about/', views.AboutPage.as_view(), name='about'),
    path('contact/', views.ContactPage.as_view(), name='contact'),
    path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
    path('like/<slug:slug>', views.PostLike.as_view(), name='post_like'),
    path(
        'edit/<int:comment_id>',
        EditCommentView.as_view(),
        name='edit_comment'
    ),
    path(
        'delete/<int:comment_id>',
        DeleteCommentView.as_view(),
        name='delete_comment'
    ),
]


#!/usr/bin/env python3
from pwn import xor
from random import randint
from os import urandom

output_img = open("flag.png", "wb")
input_img = open("flag.png.enc", "rb").read()

header = b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00"
key = [0]*9
for i in range(9):
    key[i] = int(input_img[i]) ^ int(header[i])
output_img.write(xor(input_img, key))


""" Test saved .json group files for errors.
"""

import sys
from utils import message
from classes.group import Group

def run():
    """Run sanity tests on package files"""
    message.info("Checking for package errors...")
    groups = Group.load_all()
    errors_found = 0

    for group in groups:
        files = group.files
        packages = group.packages

        for package in packages:
            errors_found += package.evaluate()

            for _file in package.files:
                errors_found += _file.evaluate()

        for _file in files:
            errors_found += _file.evaluate()

    if errors_found > 0:
        message.alert(
            f"{errors_found} errors found. It is highly recommended to fix them before proceeding."
        )
        if not message.question("Proceed without fixing?", "boolean"):
            sys.exit(1)
    else:
        message.info("No errors found")

    message.break_line()


import serial
import tkinter as tk
from tkinter import ttk
from serial.tools import list_ports

ser = None

def set_port(port):
    global ser
    # Update the serial object with the specific port
    ser = serial.Serial(port=port, baudrate=9600, timeout=1)

def toggle_pin(pin_number, value):
    global ser
    if ser is not None and ser.is_open:
        # Send the command to the Arduino
        ser.write(f"{str(pin_number)}:{value}".encode())
        print(f"{pin_number}:{value}".encode())
        #print(f"Pin {pin_number}:{value}")

def setup_gui():
    # Create the GUI window
    window = tk.Tk()
    window.title("Arduino Pin-checker")
    window.geometry("400x700")

    # Create a drop-down menu to select the serial port
    port_label = ttk.Label(window, text="Select Serial Port", font=("Arial Bold", 20))
    port_label.pack()
    port_var = tk.StringVar()
    ports = [port.device for port in list_ports.comports()]
    port_menu = ttk.Combobox(window, width=20, textvariable=port_var, values=ports)
    port_menu.pack()

    # Create two frames for the digital and analog pins
    digital_pins_frame = tk.Frame(window)
    digital_pins_frame.pack(side=tk.LEFT, padx=20, pady=20)
    analog_pins_frame = tk.Frame(window)
    analog_pins_frame.pack(side=tk.LEFT, padx=20, pady=20)

    # Create a switch for each digital pin on the Arduino Uno
    for pin_number in range(2, 14):
        pin_switch_var = tk.IntVar()
        pin_switch = tk.Checkbutton(digital_pins_frame, text=f"Pin {pin_number}", font=("Arial Bold", 20), variable=pin_switch_var, onvalue=1, offvalue=0, command=lambda pin_number=pin_number, pin_switch_var=pin_switch_var: toggle_pin(pin_number, pin_switch_var.get()), padx=30, pady=5)
        pin_switch.pack()

    # Create a switch for each analog pin on the Arduino Uno
    for pin_number in range(6):
        pin_switch_var = tk.IntVar()
        pin_switch = tk.Checkbutton(analog_pins_frame, text=f"A{pin_number}", font=("Arial Bold", 20), variable=pin_switch_var, onvalue=1, offvalue=0, command=lambda pin_number=pin_number, pin_switch_var=pin_switch_var: toggle_pin(f"A{pin_number}", pin_switch_var.get()), padx=30, pady=5)
        pin_switch.pack()

    # Configure the serial port
    port_menu.bind("<<ComboboxSelected>>", lambda event: set_port(port_var.get()))

    # Start the GUI event loop
    window.mainloop()

if __name__ == "__main__":
    setup_gui()


"""
	Ejecutamos la la función principal para realizar los thumbnails.
"""
import os
from ..thumbnails.thumbnails import *
from config import Config

URL_VIDEOS = Config.VIDEO_FOLDER
URL_THUMBNAILS = os.getcwd()
URL_THUMBNAILS = URL_THUMBNAILS[:URL_THUMBNAILS.rfind("/")+1]
URL_THUMBNAILS += Config.FOLDER_THUMBNAILS #'views/templates/img/thumbnails'

def make_thumbnails():
	thumbnail = Thumbnails()
	for folderName, subfolders, filenames in os.walk(URL_VIDEOS):
		if folderName[-1:] == "_":
			continue
		print('The current folder is ' + folderName)

		for subfolder in subfolders:
			folder_ubication = folderName.replace(URL_VIDEOS, URL_THUMBNAILS)
			if subfolder[-1:] == "_":
				continue

			if not (os.path.exists(f'{folder_ubication}/{subfolder}')):
				print(thumbnail.make_folder(subfolder, folder_ubication))
				continue
			print(f"Folder already exists ({subfolder})")

		for file in filenames:
			folder_ubication = folderName.replace(URL_VIDEOS, URL_THUMBNAILS)
			if os.path.exists(f'{folder_ubication}/{file[:-4]}.jpg'):
				print(f"File already exists ({file})")
				continue

			image_thumbnail = thumbnail.make_thumbnail(file, folderName)
			image   = thumbnail.make_thumbnail_image(image_thumbnail)
			image   = thumbnail.resize_image(image, width = 350, height = 200)
			message = thumbnail.save_image(image, folder_ubication, file)
			print(message)

		print('')

if __name__ == '__main__':
	#make_thumbnails()
	continue


from __future__ import absolute_import
from __future__ import print_function
import six

__author__ = 'a_medelyan'

import textrank
import rake
import operator
import re
from summa import summarizer

# EXAMPLE ONE - SIMPLE
stoppath = "SmartStoplist.txt"

keyword_shortlist_length = 3

def extractWords(sentence, min_char_len, max_word_length, min_frq, summary_word_length):
    # 1. initialize RAKE by providing a path to a stopwords file
    rake_object = rake.Rake(stoppath, min_char_len, max_word_length, min_frq)

    # # 2. run on RAKE on a given text
    # sample_file = io.open("data/docs/fao_test/06_450.xml", 'r',encoding="iso-8859-1")
    # text = sample_file.read()

    keywords = rake_object.run(sentence)

    summary = summarizer.summarize(sentence, words=summary_word_length)

    #summary2 = textrank.extractSentences(sentence)

    #print(summary)
    print("~~~~ generating Catchphrases ~~~~")
    print("~~~~ generating Summary ~~~~")
    print("############################################")
    #print(summary2)

    # return keywords

    # # 3. print results
    # # keywords.sort(key=lambda x: x[1])
    # # print("Keywords:", keywords)
    #
    # print("----------")
    # # EXAMPLE TWO - BEHIND THE SCENES (from https://github.com/aneesha/RAKE/rake.py)
    #
    # # 1. initialize RAKE by providing a path to a stopwords file
    rake_object = rake.Rake(stoppath)
    #
    # # text = "Compatibility of systems of linear constraints over the set of natural numbers. Criteria of compatibility " \
    # #      "of a system of linear Diophantine equations, strict inequations, and nonstrict inequations are considered. " \
    # #       "Upper bounds for components of a minimal set of solutions and algorithms of construction of minimal generating"\
    # #       " sets of solutions for all types of systems are given. These criteria and the corresponding algorithms " \
    # #       "for constructing a minimal supporting set of solutions can be used in solving all the considered types of " \
    # #       "systems and systems of mixed types."
    #
    #
    #
    # # 1. Split text into sentences
    sentenceList = rake.split_sentences(sentence)
    sentenceList = [re.sub("\s\s+", " ", sentence) for sentence in sentenceList]
    # sentenceList = list(filter(None, sentenceList))
    # print("=========================================================================================")
    # print("=========================================================================================")
    # print("=========================================================================================")
    # for sentence in sentenceList:
    #     sentense = re.sub("\s\s+", " ", sentence)
    # for sentence in sentenceList:
    #     print("Sentence:", sentence)
    # print("=========================================================================================")
    # print("=========================================================================================")
    # print("=========================================================================================")
    # # generate candidate keywords
    stopwordpattern = rake.build_stop_word_regex(stoppath)
    phraseList = rake.generate_candidate_keywords(sentenceList, stopwordpattern)
    # # print("Phrases:", phraseList)
    #
    # # calculate individual word scores
    wordscores = rake.calculate_word_scores(phraseList)
    #
    # # generate candidate keyword scores
    keywordcandidates = rake.generate_candidate_keyword_scores(phraseList, wordscores)
    # # for candidate in keywordcandidates.keys():
    # #    print("Candidate: ", candidate, ", score: ", keywordcandidates.get(candidate))
    #
    # # sort candidates by score to determine top-scoring keywords

    max_key = (max(keywordcandidates, key=keywordcandidates.get))
    max_value = (keywordcandidates[max_key])

    for key, value in keywordcandidates.iteritems():
        if(key in summary):
            keywordcandidates[key] = value + max_value

    sortedKeywords = sorted(six.iteritems(keywordcandidates), key=operator.itemgetter(1), reverse=True)
    totalKeywords = len(sortedKeywords)
    sortedKeywords = sortedKeywords[:15]

    return sortedKeywords, summary
    #
    # for example, you could just take the top third as the final keywords
    # for keyword in sortedKeywords[0:int(totalKeywords / keyword_shortlist_length)]:
    #     print("Keyword: ", keyword[0], ", score: ", keyword[1])


handle = open("data/m_cold.fasta", "r")
handle.readline()

from Bio import SeqIO
for record in SeqIO.parse("data/m_cold.fasta", "fasta"):
    print(record.id, len(record))

from Bio import SeqIO
handle = open("data/m_cold.fasta", "r")
for record in SeqIO.parse(handle, "fasta"):
    print(record.id, len(record))
handle.close()

my_info = 'A string\n with multiple lines.'
print(my_info)

from io import StringIO
my_info_handle = StringIO(my_info)
first_line = my_info_handle.readline()
print(first_line)

second_line = my_info_handle.readline()
print(second_line)

python/23 - Appendix, Useful stuff about Python.py


"""empty message

Revision ID: 303f9c5c8ea4
Revises: 4825b030f4f5
Create Date: 2019-05-10 16:25:12.832231

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '303f9c5c8ea4'
down_revision = '4825b030f4f5'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('theme', sa.Column('cover_image', sa.String(length=512), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('theme', 'cover_image')
    # ### end Alembic commands ###


#!/usr/bin/python

from concurrent import futures
import logging

import grpc
from dwc_pb2 import DwcRequest, DwcResponse
import dwc_pb2_grpc

class Dwc(dwc_pb2_grpc.DistributedWordCounterServicer):
    def count(self, request: DwcRequest, context):
        words = request.words
        word_frequency = {}
        for w in words:
            if w not in word_frequency:
                word_frequency[w] = 1
            else:
                word_frequency[w] += 1

        print(word_frequency)
        return DwcResponse(word_frequency=word_frequency)

def serve():
    PORT = '50051'
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=5))
    dwc_pb2_grpc.add_DistributedWordCounterServicer_to_server(Dwc(), server)
    server.add_insecure_port('[::]:' + PORT)
    server.start()
    print('Server started, listening on ' + PORT)

    server.wait_for_termination()

if __name__ == '__main__':
    logging.basicConfig()
    serve()


from flask import Flask, render_template, make_response
from flask import redirect, request, jsonify, url_for
from flask_sqlalchemy import SQLAlchemy

import os, io
import uuid
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import numpy as np
from base64 import b64encode

from PIL import Image
import requests
from io import BytesIO

import os, io
from google.cloud import vision
from google.cloud.vision_v1 import types
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
from matplotlib.patches import Polygon

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r'visionAPIkey.json'

app = Flask(__name__)
app.debug = True

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///usrData.db'
db = SQLAlchemy(app)

class Todo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    text = db.Column(db.String(200), nullable=False)

@app.route('/')
def index():
    #title = 'Create the input'
    return render_template('index.html')#, title=title)

@app.route('/habitsDrag', methods=['GET', 'POST'])
def habitsDrag():
    #title = 'Create the input'
    return render_template('habitsDrag.html')#, title=title)

@app.route("/f", methods=["GET", "POST"])
def f():
    if request.method == "POST":
        print("ANALYSING")
        if request.files:
            image = request.files["image"]
            img = Image.open(image)
            imgProcessed = Image.open(io.BytesIO(analyse(img)))
            image_io = BytesIO()
            imgProcessed.save(image_io, 'PNG')
            dataurl = 'data:image/png;base64,' + b64encode(image_io.getvalue()).decode('ascii')
            return render_template('f.html', image_data=dataurl)

            return redirect(request.url)

    return render_template("f.html")

@app.route('/habits', methods=['GET', 'POST'])
def habits():
    if request.method == 'GET':
        print("GETTING")
        return render_template('habits.html')
    print("POSTING")
    global data
    data = request.get_data()
    print(type(data))
    #print(request.get_json())
    #print(request.json['imgDataJson'])
    #return render_template('analysis.html', data=data)
    return redirect(url_for('analysis'))

@app.route('/analysis', methods=['GET', 'POST'])
def analysis():
    img = request.files['imgInp']
    img = Image.open(BytesIO(img))
    analyse(img)
    if request.method == 'GET':
        print('rendering "analysis"')
        return render_template('analysis.html', data=data)

@app.route('/login', methods=['GET'])
def login():
    return render_template('login.html')

@app.route('/register', methods=['GET'])
def register():
    return render_template('register.html')

@app.route('/about', methods=['GET'])
def about():
    return render_template('about.html')

@app.route('/ideas', methods=['GET'])
def ideas():
    return render_template('ideas.html')

@app.route('/results/<uuid>', methods=['GET'])
def results(uuid):
    title = 'Result'
    data = get_file_content(uuid)
    return render_template('layouts/results.html',
                           title=title,
                           data=data)

@app.route('/postmethod', methods = ['POST'])
def post_javascript_data():
    jsdata = request.form['canvas_data']
    unique_id = create_csv(jsdata)
    params = { 'uuid' : unique_id }
    return jsonify(params)

def analyse(img):
    client = vision.ImageAnnotatorClient()

    img_byte_arr = io.BytesIO()
    img.save(img_byte_arr, format='PNG')
    img_byte_arr = img_byte_arr.getvalue()

    # construct an iamge instance
    image = types.Image(content=img_byte_arr)
    response = client.text_detection(image=image)  # returns TextAnnotation

    texts = response.text_annotations

    fig = Figure()
    output = io.BytesIO()
    #plt.figure(figsize=(12, 20))
    #plt.imshow(img)
    for text in texts:
        vertices = [[vertex.x, vertex.y] for vertex in text.bounding_poly.vertices]
        vertices.append(vertices[0]) #repeat the first point to create a 'closed loop'

        xs, ys = zip(*vertices) #create lists of x and y values
        plt.plot(xs,ys)
    FigureCanvas(fig).print_png(output)
    return output.getvalue()#, mimetype='image/png')
    #plt.savefig()
    #plt.show() # if you need...

if __name__ == '__main__':
    app.run()


from _data_preprocess2 import DataPrep

from sklearn.model_selection import train_test_split
from sklearn import metrics
import datetime
import lightgbm as lgb

class Model(object):

    def __init__(self, db=None):
        self.db = db
        self.data_pre = DataPrep(db=self.db)

    def get_label(self, data):
        data['post1'] = data.sort_values('trade_date').groupby('ts_code')['daily-close-0'].shift(1)
        data['post3'] = data.sort_values('trade_date').groupby('ts_code')['daily-close-0'].shift(3)
        data['post5'] = data.sort_values('trade_date').groupby('ts_code')['daily-close-0'].shift(5)
        data['y1'] = data['post1'] / data['daily-close-0']
        data['y2'] = data['post3'] / data['daily-close-0']
        data['y3'] = data['post5'] / data['daily-close-0']

        cp_col = ['ts_code']
        data_col = ['trade_date']
        y_cols = ['post1', 'post3', 'post5', 'y1', 'y2', 'y3']
        ft_cols = list(set(data.columns.tolist()) - set(cp_col + data_col + y_cols))

        return data

    def get_train_data(self, start_date=None, end_date=None):
        if (start_date is None) or (end_date is None):
            tmp = datetime.datetime.now()
            end_date = tmp.strftime('%Y%m%d')
            start_date = (tmp-datetime.timedelta(days=30)).strftime('%Y%m%d')
        data = self.data_pre.get_data(start_date=start_date, end_date=end_date)
        data = self.get_label(data)

        cp_col = ['ts_code']
        data_col = ['trade_date']
        y_cols = ['post1', 'post3', 'post5', 'y1', 'y2', 'y3']
        ft_cols = list(set(data.columns.tolist()) - set(cp_col + data_col + y_cols))

        tmp = data[~data['y1'].isna()]
        X = tmp[ft_cols].to_numpy()
        Y = (tmp['y1'] > 1.05).astype('int').to_numpy()

        return X, Y

    def get_predict_data(self):
        date = datetime.date().strftime('%Y%m%d')
        data = self.data_pre.get_data(date=date)

        cp_col = ['ts_code']
        data_col = ['trade_date']
        y_cols = ['post1', 'post3', 'post5', 'y1', 'y2', 'y3']
        ft_cols = list(set(data.columns.tolist()) - set(cp_col + data_col + y_cols))

        X = data[ft_cols].to_numpy()

        return X

    def train(self):
        self.X, self.Y = self.get_train_data()
        x_train, x_test, y_train, y_test = train_test_split(self.X, self.Y, test_size=0.25)
        gbm = lgb.LGBMClassifier()

        gbm.fit(x_train, y_train)

        y_pre = gbm.predict(x_test, num_iteration=gbm.best_iteration_)

        self.acc = metrics.accuracy_score(y_test, y_pre)
        self.precision = metrics.precision_score(y_test, y_pre)
        self.recall = metrics.recall_score(y_test, y_pre)
        self.f1_score = metrics.f1_score(y_test, y_pre)
        self.auc = metrics.roc_auc_score(y_test, y_pre)
        self.total_num = y_test.shape[0]
        self.p_num = y_test.reshape(-1, ).sum()
        self.n_num = self.total_num - self.p_num
        self.pp_num = y_pre.reshape(-1, ).sum()
        self.pn_num = self.total_num - self.pp_num
        self.cf_mt = metrics.confusion_matrix(y_test, y_pre)

        self.model = gbm
        return gbm

    def predict(self, data=None):
        x = self.get_predict_data(data)

        y_pre = self.model.predict(x)
        return y_pre


import errno
import os
import random
import secrets
import shutil
from datetime import datetime, timedelta
from pathlib import Path
from typing import Any, List
from unittest import mock

import pytest
from yarl import URL

from neuro_sdk import (
    AbstractRecursiveFileProgress,
    StorageProgressComplete,
    StorageProgressEnterDir,
    StorageProgressFail,
    StorageProgressLeaveDir,
    StorageProgressStart,
    StorageProgressStep,
)
from neuro_sdk._file_utils import READ_SIZE, FileTransferer, LocalFS, rm

@pytest.fixture()
def src_dir(tmp_path_factory: Any) -> Path:
    return tmp_path_factory.mktemp("src_dir")

@pytest.fixture()
def dst_dir(tmp_path_factory: Any) -> Path:
    return tmp_path_factory.mktemp("dst_dir")

@pytest.fixture()
def transferer() -> FileTransferer[Path, Path]:
    return FileTransferer(LocalFS(), LocalFS())

async def test_transfer_file(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    (src_dir / "test_file").write_bytes(b"testing")
    await transferer.transfer_file(src_dir / "test_file", dst_dir / "test_file")
    res = (dst_dir / "test_file").read_bytes()
    assert res == b"testing"

async def test_transfer_file_source_not_exists(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    with pytest.raises(FileNotFoundError) as e:
        await transferer.transfer_file(src_dir / "test_file", dst_dir / "test_file")
    assert e.value.args[0] == errno.ENOENT

async def test_transfer_file_source_is_dir(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    (src_dir / "test_file").mkdir()
    with pytest.raises(IsADirectoryError) as e:
        await transferer.transfer_file(src_dir / "test_file", dst_dir / "test_file")
    assert e.value.args[0] == errno.EISDIR

async def test_transfer_file_dest_is_dir(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    (src_dir / "test_file").write_bytes(b"testing")
    (dst_dir / "test_file").mkdir()
    with pytest.raises(IsADirectoryError) as e:
        await transferer.transfer_file(src_dir / "test_file", dst_dir / "test_file")
    assert e.value.args[0] == errno.EISDIR

async def test_transfer_file_progress(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    size = 3 * READ_SIZE
    (src_dir / "test_file").write_bytes(b"0" * size)
    progress = mock.Mock()
    await transferer.transfer_file(
        src_dir / "test_file", dst_dir / "test_file", progress=progress
    )
    src_url = URL((src_dir / "test_file").as_uri())
    dst_url = URL((dst_dir / "test_file").as_uri())
    progress.start.assert_called_once_with(StorageProgressStart(src_url, dst_url, size))
    assert [call for call in progress.step.call_args_list] == [
        ((StorageProgressStep(src_url, dst_url, READ_SIZE, size),),),
        ((StorageProgressStep(src_url, dst_url, 2 * READ_SIZE, size),),),
        ((StorageProgressStep(src_url, dst_url, 3 * READ_SIZE, size),),),
    ]
    progress.complete.assert_called_once_with(
        StorageProgressComplete(src_url, dst_url, size)
    )

async def test_transfer_file_update_dst_newer(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    now = datetime.now()
    hour_ago = now - timedelta(hours=1)

    (src_dir / "test_file").write_bytes(b"testing")
    (dst_dir / "test_file").write_bytes(b"newer data")
    os.utime(src_dir / "test_file", (hour_ago.timestamp(), hour_ago.timestamp()))
    os.utime(dst_dir / "test_file", (now.timestamp(), now.timestamp()))
    await transferer.transfer_file(
        src_dir / "test_file", dst_dir / "test_file", update=True
    )
    res = (dst_dir / "test_file").read_bytes()
    assert res == b"newer data"

async def test_transfer_file_update_src_newer(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    now = datetime.now()
    hour_ago = now - timedelta(hours=1)

    (src_dir / "test_file").write_bytes(b"newer data")
    (dst_dir / "test_file").write_bytes(b"tessting")
    os.utime(src_dir / "test_file", (now.timestamp(), now.timestamp()))
    os.utime(dst_dir / "test_file", (hour_ago.timestamp(), hour_ago.timestamp()))
    await transferer.transfer_file(
        src_dir / "test_file", dst_dir / "test_file", update=True
    )
    res = (dst_dir / "test_file").read_bytes()
    assert res == b"newer data"

async def test_transfer_file_continue_src_newer(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    now = datetime.now()
    hour_ago = now - timedelta(hours=1)

    (src_dir / "test_file").write_bytes(b"newer data")
    (dst_dir / "test_file").write_bytes(b"testing")
    os.utime(src_dir / "test_file", (now.timestamp(), now.timestamp()))
    os.utime(dst_dir / "test_file", (hour_ago.timestamp(), hour_ago.timestamp()))
    await transferer.transfer_file(
        src_dir / "test_file", dst_dir / "test_file", continue_=True
    )
    res = (dst_dir / "test_file").read_bytes()
    assert res == b"newer data"

async def test_transfer_file_continue_dst_newer(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    now = datetime.now()
    hour_ago = now - timedelta(hours=1)

    (src_dir / "test_file").write_bytes(b"testing   additional data")
    (dst_dir / "test_file").write_bytes(b"only test ")
    os.utime(src_dir / "test_file", (hour_ago.timestamp(), hour_ago.timestamp()))
    os.utime(dst_dir / "test_file", (now.timestamp(), now.timestamp()))
    await transferer.transfer_file(
        src_dir / "test_file", dst_dir / "test_file", continue_=True
    )
    res = (dst_dir / "test_file").read_bytes()
    assert res == b"only test additional data"

async def gen_file_tree(path: Path, depths: int = 2) -> None:
    if depths > 0:
        for _ in range(10):
            dir_name = secrets.token_hex(10)
            (path / dir_name).mkdir()
            await gen_file_tree(path / dir_name, depths=depths - 1)
    for _ in range(10):
        file_name = secrets.token_hex(10)
        data = bytearray(random.getrandbits(8) for _ in range(1024))
        (path / file_name).write_bytes(data)

async def cmp_dirs(path1: Path, path2: Path) -> bool:
    childs1 = {path.name for path in path1.iterdir()}
    childs2 = {path.name for path in path2.iterdir()}
    if childs1 != childs2:
        return False
    same = True
    for child in childs1:
        if (path1 / child).is_file() and (path2 / child).is_file():
            same = same and (path1 / child).read_bytes() == (path2 / child).read_bytes()
        elif (path1 / child).is_dir() and (path2 / child).is_dir():
            same = same and await cmp_dirs(path1 / child, path2 / child)
        else:
            same = False
    return same

async def test_transfer_dir(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    src = src_dir / "sub_dir"
    src.mkdir()
    await gen_file_tree(src)
    await transferer.transfer_dir(src, dst_dir / "sub_dir")
    assert await cmp_dirs(src, dst_dir / "sub_dir")

async def test_transfer_dir_dest_exists(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    src = src_dir / "sub_dir"
    src.mkdir()
    dst = dst_dir / "sub_dir"
    dst.mkdir()
    await gen_file_tree(src, depths=1)
    await transferer.transfer_dir(src, dst)
    assert await cmp_dirs(src, dst)

async def test_transfer_dir_source_not_exists(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    with pytest.raises(FileNotFoundError) as e:
        await transferer.transfer_file(src_dir / "sub_dir", dst_dir / "sub_dir")
    assert e.value.args[0] == errno.ENOENT

async def test_transfer_dir_source_not_dir(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    (src_dir / "sub_dir").write_bytes(b"bbb")
    with pytest.raises(NotADirectoryError) as e:
        await transferer.transfer_dir(src_dir / "sub_dir", dst_dir / "sub_dir")
    assert e.value.args[0] == errno.ENOTDIR

async def test_transfer_dir_dest_not_dir(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    (src_dir / "sub_dir").mkdir()
    (dst_dir / "sub_dir").write_bytes(b"bbb")
    with pytest.raises(NotADirectoryError) as e:
        await transferer.transfer_dir(src_dir / "sub_dir", dst_dir / "sub_dir")
    assert e.value.args[0] == errno.ENOTDIR

async def test_transfer_dir_progress(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    src = src_dir / "sub_dir"
    src.mkdir()
    await gen_file_tree(src, depths=1)

    class MockProgress(AbstractRecursiveFileProgress):
        def __init__(self) -> None:
            self.entered_dirs: List[StorageProgressEnterDir] = []
            self.left_dirs: List[StorageProgressLeaveDir] = []
            self.failed_dirs: List[StorageProgressFail] = []
            self.started_files: List[StorageProgressStart] = []
            self.file_steps: List[StorageProgressStep] = []
            self.completed_files: List[StorageProgressComplete] = []

        def enter(self, data: StorageProgressEnterDir) -> None:
            self.entered_dirs.append(data)

        def leave(self, data: StorageProgressLeaveDir) -> None:
            self.left_dirs.append(data)

        def fail(self, data: StorageProgressFail) -> None:
            self.failed_dirs.append(data)

        def start(self, data: StorageProgressStart) -> None:
            self.started_files.append(data)

        def complete(self, data: StorageProgressComplete) -> None:
            self.completed_files.append(data)

        def step(self, data: StorageProgressStep) -> None:
            self.file_steps.append(data)

    progress = MockProgress()
    await transferer.transfer_dir(src, dst_dir / "sub_dir", progress=progress)

    def _check_progress(path: Path) -> None:
        for subpath in path.iterdir():
            src_url = URL(subpath.as_uri())
            dst_url = URL((dst_dir / "sub_dir" / (subpath.relative_to(src))).as_uri())
            if subpath.is_file():
                size = subpath.stat().st_size
                assert any(
                    start
                    == StorageProgressStart(
                        src=src_url,
                        dst=dst_url,
                        size=size,
                    )
                    for start in progress.started_files
                )
                assert any(
                    step
                    == StorageProgressStep(
                        src=src_url,
                        dst=dst_url,
                        size=size,
                        current=size,
                    )
                    for step in progress.file_steps
                )
                assert any(
                    finish
                    == StorageProgressComplete(
                        src=src_url,
                        dst=dst_url,
                        size=size,
                    )
                    for finish in progress.completed_files
                )
            if subpath.is_dir():
                assert any(
                    enter
                    == StorageProgressEnterDir(
                        src=src_url,
                        dst=dst_url,
                    )
                    for enter in progress.entered_dirs
                )
                assert any(
                    leave
                    == StorageProgressLeaveDir(
                        src=src_url,
                        dst=dst_url,
                    )
                    for leave in progress.left_dirs
                )
                _check_progress(subpath)

    _check_progress(src)

async def test_transfer_dir_update(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    now = datetime.now()
    hour_ago = now - timedelta(hours=1)

    src = src_dir / "sub_dir"
    src.mkdir()
    dst = dst_dir / "sub_dir"
    dst.mkdir()
    (src / "src_newer").write_bytes(b"newer data")
    (dst / "src_newer").write_bytes(b"testing")
    os.utime(src / "src_newer", (now.timestamp(), now.timestamp()))
    os.utime(dst / "src_newer", (hour_ago.timestamp(), hour_ago.timestamp()))

    (src / "dst_newer").write_bytes(b"testing")
    (dst / "dst_newer").write_bytes(b"newer data")
    os.utime(src / "dst_newer", (now.timestamp(), hour_ago.timestamp()))
    os.utime(dst / "dst_newer", (hour_ago.timestamp(), now.timestamp()))

    await transferer.transfer_dir(src, dst, update=True)
    assert (dst / "src_newer").read_bytes() == b"newer data"
    assert (dst / "dst_newer").read_bytes() == b"newer data"

async def test_transfer_dir_continue(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    now = datetime.now()
    hour_ago = now - timedelta(hours=1)

    src = src_dir / "sub_dir"
    src.mkdir()
    dst = dst_dir / "sub_dir"
    dst.mkdir()
    (src / "src_newer").write_bytes(b"newer data")
    (dst / "src_newer").write_bytes(b"testing")
    os.utime(src / "src_newer", (now.timestamp(), now.timestamp()))
    os.utime(dst / "src_newer", (hour_ago.timestamp(), hour_ago.timestamp()))

    (src / "dst_newer").write_bytes(b"               more data")
    (dst / "dst_newer").write_bytes(b"already copied")
    os.utime(src / "dst_newer", (now.timestamp(), hour_ago.timestamp()))
    os.utime(dst / "dst_newer", (hour_ago.timestamp(), now.timestamp()))

    await transferer.transfer_dir(src, dst, continue_=True)
    assert (dst / "src_newer").read_bytes() == b"newer data"
    assert (dst / "dst_newer").read_bytes() == b"already copied more data"

async def test_transfer_dir_filter(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    src = src_dir / "sub_dir"
    src.mkdir()
    await gen_file_tree(src, depths=1)
    skip_dirs = []
    for path in src.iterdir():
        if path.is_dir():
            skip_dirs.append(path.name)
        if len(skip_dirs) == 3:
            break

    async def _filter(path: str) -> bool:
        return not any(skip_dir in path for skip_dir in skip_dirs)

    await transferer.transfer_dir(src, dst_dir / "sub_dir", filter=_filter)
    for skip_dir in skip_dirs:
        shutil.rmtree(src / skip_dir)
    assert await cmp_dirs(src, dst_dir / "sub_dir")

async def test_transfer_dir_ignore_file_names(
    transferer: FileTransferer[Path, Path], src_dir: Path, dst_dir: Path
) -> None:
    src = src_dir / "sub_dir"
    src.mkdir()
    await gen_file_tree(src, depths=1)
    skip_dirs = []
    for path in src.iterdir():
        if path.is_dir():
            skip_dirs.append(path.name)
        if len(skip_dirs) == 4:
            break
    (src_dir / ".cp_ignore").write_text("\n".join(skip_dirs[:2]))
    (src_dir / "sub_dir" / ".cp_ignore").write_text("\n".join(skip_dirs[2:]))

    await transferer.transfer_dir(
        src, dst_dir / "sub_dir", ignore_file_names={".cp_ignore"}
    )
    for skip_dir in skip_dirs:
        shutil.rmtree(src / skip_dir)
    assert await cmp_dirs(src, dst_dir / "sub_dir")

async def test_rm_file(
    src_dir: Path,
) -> None:
    file_path = src_dir / "file"
    file_path.touch()
    await rm(LocalFS(), file_path, recursive=False)
    assert not file_path.exists()

async def test_rm_dir(
    src_dir: Path,
) -> None:
    dir_path = src_dir / "sub_dir"
    dir_path.mkdir()
    await gen_file_tree(dir_path, depths=1)
    await rm(LocalFS(), dir_path, recursive=True)
    assert not dir_path.exists()

async def test_rm_not_exists(
    src_dir: Path,
) -> None:
    file_path = src_dir / "file"
    with pytest.raises(FileNotFoundError) as e:
        await rm(LocalFS(), file_path, recursive=False)
    assert e.value.args[0] == errno.ENOENT

async def test_rm_dir_not_recursive(
    src_dir: Path,
) -> None:
    dir_path = src_dir / "sub_dir"
    dir_path.mkdir()
    with pytest.raises(IsADirectoryError) as e:
        await rm(LocalFS(), dir_path, recursive=False)
    assert e.value.args[0] == errno.EISDIR


#!/usr/bin/python3

from bs4 import BeautifulSoup
from fake_useragent import UserAgent
from colorama import Fore, Back, Style
import requests, re, string

banner = """

╔╦╗┬ ┬┌─┐  ╦ ╦┌─┐┌─┐┬┌─┌─┐┬─┐  ╔╗╔┌─┐┬ ┬┌─┐
 ║ ├─┤├┤   ╠═╣├─┤│  ├┴┐├┤ ├┬┘  ║║║├┤ │││└─┐
 ╩ ┴ ┴└─┘  ╩ ╩┴ ┴└─┘┴ ┴└─┘┴└─  ╝╚╝└─┘└┴┘└─┘

Author: c0d3Ninja
"""

print (banner)

ua = UserAgent()
header = {'User-Agent':str(ua.chrome)}
link = "https://thehackernews.com/"
response = requests.get(link, timeout=5, headers=header)
if response.status_code == 200:
    print ("-" * 70)
    print("Webite: " + Fore.GREEN + link)
    print(Fore.WHITE + "Status: " + Fore.GREEN + "UP")
else:
    exit()

soup = BeautifulSoup(response.content, "html.parser")
menu = soup.find_all('li',attrs={"class":"show-menu"})

title = soup.title

for titletext in title:
    print(Fore.WHITE + "Title: " + Fore.GREEN + titletext)
    print (Fore.WHITE + "-" * 75)

for links in soup.find_all('a',attrs={"class":"story-link"}):
    seperator = Fore.CYAN + "-" * 75
    print(Fore.WHITE)
    print (''.join(x for x in links.text if x in string.printable).strip() + "\n\n" + Fore.GREEN + links.get('href') + "\n" + seperator + "\n")


import os
import time
import random

import cv2 as cv
import numpy as np
import tensorflow as tf

from cifar_data_load import get_training_dataset, get_test_dataset
from common import get_onehot_label

def AlexNet(input, p=0.5):
    # 定义卷积层conv1
    with tf.name_scope('conv1'):
        # 第一层卷积层 filter 64 3 11 11 步长4
        conv1_kernel = tf.Variable(tf.truncated_normal([11, 11, 3, 64], dtype=tf.float32,
                                   stddev=1e-1, name='weights'))
        conv1 = tf.nn.conv2d(input, conv1_kernel, [1, 4, 4, 1], padding='SAME')
        conv1_biases = tf.Variable(tf.constant(0.0, dtype=tf.float32, shape=[64]),
                                               trainable=True, name='biases')
        conv1_bias = tf.nn.bias_add(conv1, conv1_biases)
        conv1 = tf.nn.relu(conv1_bias, name='conv1')

        # local response normalization LRN局部响应归一化
        lrn1 = tf.nn.lrn(conv1, 4, bias=1.0, alpha=0.001 / 9, beta=0.75, name='lrn1')
        # 最大池化层 filter 3 3 步长2
        pool1 = tf.nn.max_pool(lrn1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
                               padding='VALID', name='pool1')

    # 定义卷积层conv2
    with tf.name_scope('conv2'):
        # 第二层卷积层 filter 192 64 5 5 步长1
        conv2_kernel = tf.Variable(tf.truncated_normal([5, 5, 64, 192], dtype=tf.float32,
                                   stddev=1e-1, name='weights'))
        conv2 = tf.nn.conv2d(pool1, conv2_kernel, [1, 1, 1, 1], padding='SAME')
        conv2_biases = tf.Variable(tf.constant(0.0, dtype=tf.float32, shape=[192]),
                                   trainable=True, name='biases')
        conv2_bias = tf.nn.bias_add(conv2, conv2_biases)
        conv2 = tf.nn.relu(conv2_bias, name='conv2')

        # local response normalization LRN局部响应归一化
        lrn2 = tf.nn.lrn(conv2, 4, bias=1.0, alpha=0.001 / 9, beta=0.75, name='lrn2')
        # 最大池化层 filter 3 3 步长2
        pool2 = tf.nn.max_pool(lrn2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
                               padding='VALID', name='pool2')

    # 定义卷积层conv3
    with tf.name_scope('conv3'):
        # 第二层卷积层 filter 384 192 3 3 步长1
        conv3_kernel = tf.Variable(tf.truncated_normal([3, 3, 192, 384], dtype=tf.float32,
                                   stddev=1e-1, name='weights'))
        conv3 = tf.nn.conv2d(pool2, conv3_kernel, [1, 1, 1, 1], padding='SAME')
        conv3_biases = tf.Variable(tf.constant(0.0, dtype=tf.float32, shape=[384]),
                                               trainable=True, name='biases')
        conv3_bias = tf.nn.bias_add(conv3, conv3_biases)
        conv3 = tf.nn.relu(conv3_bias, name='conv3')

    # 定义卷积层conv4
    with tf.name_scope('conv4'):
        # 第二层卷积层 filter 256 384 3 3 步长1
        conv4_kernel = tf.Variable(tf.truncated_normal([3, 3, 384, 256], dtype=tf.float32,
                                   stddev=1e-1, name='weights'))
        conv4 = tf.nn.conv2d(conv3, conv4_kernel, [1, 1, 1, 1], padding='SAME')
        conv4_biases = tf.Variable(tf.constant(0.0, dtype=tf.float32, shape=[256]),
                                   trainable=True, name='biases')
        conv4_bias = tf.nn.bias_add(conv4, conv4_biases)
        conv4 = tf.nn.relu(conv4_bias, name='conv4')

    # 定义卷积层conv5
    with tf.name_scope('conv5'):
        # 第二层卷积层 filter 256 256 3 3 步长1
        conv5_kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256], dtype=tf.float32,
                                   stddev=1e-1, name='weights'))
        conv5 = tf.nn.conv2d(conv4, conv5_kernel, [1, 1, 1, 1], padding='SAME')
        conv5_biases = tf.Variable(tf.constant(0.0, dtype=tf.float32, shape=[256]),
                                         trainable=True, name='biases')
        conv5_bias = tf.nn.bias_add(conv5, conv5_biases)
        conv5 = tf.nn.relu(conv5_bias, name='conv5')

        # 最大池化层 filter 3 3 步长2
        pool5 = tf.nn.max_pool(conv5, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
                               padding='VALID', name='pool5')

        # 把值展开 reshape送入全连接层
        flatten = tf.reshape(pool5, [-1, 6*6*256])

    # 定义全连接层dense1
    with tf.name_scope('dense1'):
        # 输入9216 输出4096
        fc1_weights = tf.Variable(tf.truncated_normal([6*6*256, 4096], mean=0, stddev=0.01))
        fc1_biases = tf.Variable(tf.constant(0.0, shape=[1024], dtype=tf.float32),
                                 trainable=True, name='biases')
        fc1 = tf.nn.sigmoid(tf.matmul(flatten, fc1_weights) + fc1_biases, name='dense1')
        dense1 = tf.nn.dropout(fc1, p)

    # 定义全连接层dense2
    with tf.name_scope('dense2'):
        # 输入4096 输出4096
        fc2_weights = tf.Variable(tf.truncated_normal([4096, 4096], mean=0, stddev=0.01))
        fc2_biases = tf.Variable(tf.constant(0.0, shape=[1024], dtype=tf.float32),
                                 trainable=True, name='biases')
        fc2 = tf.nn.sigmoid(tf.matmul(dense1, fc2_weights) + fc2_biases, name='dense2')
        dense2 = tf.nn.dropout(fc2, p)

    # 定义全连接层dense3
    with tf.name_scope('output'):
        # 输入4096 输出10
        fc3_weights = tf.Variable(tf.truncated_normal([4096, 10], mean=0, stddev=0.01))
        fc3_biases = tf.Variable(tf.constant(0.0, shape=[10], dtype=tf.float32),
                                 trainable=True, name='biases')
        dense3 = tf.matmul(dense2, fc3_weights) + fc3_biases

    return dense3

# 训练
def train():
    # 加载训练集
    train_sample, train_label = get_training_dataset()

    # 定义tf占位符 输入输出格式
    x = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224 ,3], name='x')
    y = tf.placeholder(dtype=tf.float32, shape=[None, 10], name='y')

    # 建立网络
    output = AlexNet(x, 0.5)
    # 定义训练的loss函数 得到一个batch的平均值
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y))
    # 定义优化器
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.09).minimize(loss)
    # 定义准确率
    val_acc = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(output, 1), tf.argmax(y, 1)), tf.float32))

    # saver持久化 保存模型
    saver = tf.train.Saver()
    init = tf.global_variables_initializer()
    epochs = 40
    with tf.Session() as session:
        # 编译 静态图
        session.run(init)

        # 加载模型
        model = tf.train.get_checkpoint_state("./model/alexnet")
        if model and model.model_checkpoint_path:
            # saver.restore(session, model.model_checkpoint_path)
            print('Successfully Load AlexNet Model!')

        for epoch in range(epochs):
            begin = time.time()
            # 生成并打乱训练集的顺序
            index = np.arange(50000)
            random.shuffle(index)

            # batch size为200 训练集前40000训练 后10000验证
            for i in range(0, 0+40000, 200):
                train_batch_sample = list()
                train_batch_label = list()
                for j in range(0, 200):
                    # train_batch_sample.append(train_sample[index[i + j]] / 255)
                    train_batch_sample.append(cv.resize(train_sample[index[i + j]], (224, 224)) / 255)
                    train_batch_label.append(train_label[index[i + j]])
                train_batch_label = get_onehot_label(train_batch_label, class_num=10)
                # 训练
                a, b = session.run([optimizer, loss], feed_dict={
                    x: train_batch_sample,
                    y: train_batch_label,
                })
                print('train loss: ', b)

            acc = 0
            for i in range(40000, 40000+10000, 200):
                val_batch_sample = list()
                val_batch_label = list()
                for j in range(0, 200):
                    # val_batch_sample.append(train_sample[index[i + j]].astype(np.uint8) / 225)
                    val_batch_sample.append(cv.resize(train_sample[index[i + j]].astype(np.uint8), (224, 224)) / 255)
                    val_batch_label.append(train_label[index[i + j]])
                val_batch_label = get_onehot_label(val_batch_label, class_num=10)
                acc += session.run(val_acc, feed_dict={
                    x: val_batch_sample,
                    y: val_batch_label
                })
            # 输出 一共50次验证数据相加
            print('====================Epoch {}: validation acc: {}, spend time: {}s==================='.format(epoch + 1, acc / 50, time.time() - begin))
            # 每训练5epoch 保存模型
            if (epoch + 1) % 5 == 0:
                pass
                # 保存模型
                saver.save(session, "model/alexnet/alexnet.ckpt")

        # 保存模型
        saver.save(session, "model/alexnet/alexnet.ckpt")

def test():
    # 加载训练集
    test_sample, test_label = get_test_dataset()

    # 定义tf占位符 输入输出格式
    x = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224 ,3], name='x')
    y = tf.placeholder(dtype=tf.float32, shape=[None, 10], name='y')

    # 建立网络
    output = AlexNet(x, 0.5)
    # 预测
    predict_output = tf.argmax(output, 1)
    ground_truth = tf.argmax(y, 1)

    # 定义准确率
    val_acc = tf.reduce_mean(tf.cast(tf.equal(predict_output, ground_truth), tf.float32))
    acc = 0
    # saver持久化 保存模型
    saver = tf.train.Saver()

    with tf.Session() as session:
        # 加载模型
        saver.restore(session, 'model/alexnet/alexnet.ckpt')

        for i in range(0, 10000, 200):
            test_batch_sample = list()
            test_batch_label = list()
            for j in range(200):
                test_batch_sample.append(cv.resize(test_sample[i + j], (224, 224)) / 255)
                test_batch_label.append(test_label[i + j])
            test_batch_label = get_onehot_label(test_batch_label, class_num=10)
            a, b = session.run([predict_output, ground_truth], feed_dict={
                x: test_batch_sample,
                y: test_batch_label
            })
            for k in range(200):
                if a[k] == b[k]:
                    acc += 1
        print('test acc is {}%'.format(acc / 10000 * 100))

        class_list = ['airplane', 'automobile', 'bird', 'cat', 'deer',
                      'dog', 'frog', 'horse', 'ship', 'truck']
        image_path = os.getcwd() + "/../DL_HotNet_Tensorflow/net/"
        for i in range(1, 11):
            name = image_path + str(i) + '.jpg'
            img = cv.imread(name)
            img = cv.resize(img, (32, 32))
            img = cv.resize(img, (224, 224)) / 255
            img = np.array([img])
            predict = session.run(predict_output, feed_dict={x: img})
            print('{} image detect result is : {}'.format(class_list[i - 1], class_list[predict[0]]))

if __name__ == '__main__':
    os.chdir('/home/yipeng/workspace/python/tensorflow_samples')
    train()
    # test()


import numpy as np
from PIL import Image

def rgbTogray(rgb: np.array) -> np.array:
    # standard graysacale conversion
    r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
    return 0.2989 * r + 0.5870 * g + 0.1140 * b

def binaryzation(gray: np.array) -> np.array:
    # map like conditional conversion
    # limiarization with threshold at the half
    return (127 < gray) & (gray <= 255)

def backToRgb(binary: np.array) -> np.array:
    # map like conditional conversion
    # limiarization with threshold at the half
    return (binary != 0) * 255

def imageHandle(image: np.array) -> np.array:
    dimension = len(image.shape)
    image = rgbTogray(image) if dimension == 3 else image
    image = binaryzation(image)
    return image

def erode(image: np.array, kernel: np.array, backToRgb: bool = False) -> np.array:
    original = np.array(image)
    image = imageHandle(image)

    blank_image = np.zeros_like(image)
    output: np.array = blank_image

    image_padded = np.zeros(
        (image.shape[0] + kernel.shape[0] - 1,
            image.shape[1] + kernel.shape[1] - 1)
    )

    # Copy image to padded image
    image_padded[kernel.shape[0] - 2: -1, kernel.shape[1] - 2: -1] = image

    # Iterate over image & apply kernel
    for y in range(image.shape[1]):
        for x in range(image.shape[0]):
            summation = (
                kernel * image_padded[x: x +
                                      kernel.shape[0], y: y + kernel.shape[1]]
            )

            output[x, y] = int(np.count_nonzero(summation)
                               == np.count_nonzero(kernel))

    if backToRgb:
        output = np.array(Image.fromarray(output).convert("RGB"))
        output = np.where(output[:, :, :] == (0, 0, 0), output, original)

    return output


class CoupleHoldingHands(object):

    def minSwapsCouples(self, seatArrangement):
        minSwaps = 0
        positionDict = [-1 for counter in range(len(seatArrangement))]

        for counter in range(len(seatArrangement)):
            positionDict[seatArrangement[counter]] = counter

        for counter in range(len(seatArrangement)):
            innerCounter = self.getPartner(positionDict[self.getPartner(seatArrangement[counter])])
            while (innerCounter != counter):
                seatArrangement[counter], seatArrangement[innerCounter] = seatArrangement[innerCounter], seatArrangement[counter]
                positionDict[seatArrangement[counter]], positionDict[seatArrangement[innerCounter]] = \
                    positionDict[seatArrangement[innerCounter]], positionDict[seatArrangement[counter]]
                minSwaps += 1
                innerCounter = self.getPartner(positionDict[self.getPartner(seatArrangement[counter])])

        return minSwaps

    def getPartner(self, person):
        if person%2 == 0:
            return person+1
        else:
            return person-1

from nose.tools import assert_equals, assert_raises

class TestCoupleHoldingHands(object):

  def testCoupleHoldingHands(self):
    coupleHoldingHands = CoupleHoldingHands()

    assert_equals(coupleHoldingHands.minSwapsCouples([0, 1, 2, 3]), 0)

    assert_equals(coupleHoldingHands.minSwapsCouples([0, 2, 1, 3]), 1)

    assert_equals(coupleHoldingHands.minSwapsCouples([3, 2, 0, 1]), 0)

    assert_equals(coupleHoldingHands.minSwapsCouples([5, 4, 2, 6, 3, 1, 0, 7]), 2)

    print ("All test cases passed!")

def main():
  testCoupleHoldingHands = TestCoupleHoldingHands()
  testCoupleHoldingHands.testCoupleHoldingHands()

if __name__ == '__main__':
  main()


"""
171. Excel Sheet Column Number
Given a string columnTitle that represents the column title as appear in an Excel sheet, return its corresponding column number.

For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...

Example1:
Input: columnTitle = "A"
Output: 1

Example2:
Input: columnTitle = "AB"
Output: 28

Example3:
Input: columnTitle = "ZY"
Output: 701

Example4:
Input: columnTitle = "FXSHRXW"
Output: 2147483647

Constraints:
1 <= columnTitle.length <= 7
columnTitle consists only of uppercase English letters.
columnTitle is in the range ["A", "FXSHRXW"].
"""

"""
Notes:
1. One pass with ord function: O(n) time | O(1) space
2. One line with functools.reduce: O(n) time | O(1) space
"""

from functools import reduce
class Solution(object):
    def titleToNumber(self, columnTitle: str) -> int:
        number = 0
        for char in columnTitle:
            number = number * 26 + (ord(char) - ord("A") + 1)
        return number

    def titleToNumber2(self, columnTitle: str) -> int:
        return reduce(lambda x, y: x * 26 + y, [ord(char) - ord("A") + 1 for char in columnTitle])

# Unit Tests
import unittest
funcs = [Solution().titleToNumber, Solution().titleToNumber2]

class TestTitleToNumber(unittest.TestCase):
    def testTitleToNumber1(self):
        for func in funcs:
            columnTitle = "A"
            self.assertEqual(func(columnTitle=columnTitle), 1)

    def testTitleToNumber2(self):
        for func in funcs:
            columnTitle = "AB"
            self.assertEqual(func(columnTitle=columnTitle), 28)

    def testTitleToNumber3(self):
        for func in funcs:
            columnTitle = "ZY"
            self.assertEqual(func(columnTitle=columnTitle), 701)

    def testTitleToNumber4(self):
        for func in funcs:
            columnTitle = "FXSHRXW"
            self.assertEqual(func(columnTitle=columnTitle), 2147483647)

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


from __future__ import unicode_literals
import django
from django.shortcuts import render, redirect, HttpResponse
from django.db import models
from apps.main.models import *
from datetime import *
import requests
import json

def grab_data():
    response = requests.get("https://api.seatgeek.com/2/events?venue.city=Chicago&client_id=ODI3OTE5M3wxNTAxMDIxODIzLjUy&per_page=3000")
    my_events = response.json()
    event_list = my_events['events']
    for i in range(len(event_list)):
        #performer creation
        event_popularity = event_list[i]['score']
        if event_popularity > 0.5:
            new_performer_name = event_list[i]['performers'][0]['name']
            existing_performers = Performer.objects.filter(name = new_performer_name)
            if len(existing_performers) == 0:
                name = new_performer_name
                category = event_list[i]['performers'][0]['type']
                thumbnail = event_list[i]['performers'][0]['image']
                new_performer = Performer.objects.create(name = name, category = category, thumbnail = thumbnail)
                new_performer.save()
                event_performer = new_performer
            else:
                event_performer = Performer.objects.get(name = new_performer_name)
            new_venue_name = event_list[i]['venue']['name']
            existing_venues = Venue.objects.filter(title = new_venue_name)
            if len(existing_venues) == 0:
                title = new_venue_name
                address = event_list[i]['venue']['address']
                city = event_list[i]['venue']['city']
                state = event_list[i]['venue']['state']
                zip_code = event_list[i]['venue']['postal_code']
                new_venue = Venue.objects.create(title = title, address = address, city = city, state = state, zip_code = zip_code)
                new_venue.save()
                event_venue = new_venue
            else:
                event_venue = Venue.objects.get(title = new_venue_name)

            try:
                event_title = event_list[i]['short_title']
            except:
                event_title = event_list[i]['title']

            if event_title.endswith(' - Chicago'):
                event_title= event_title[:-10]
            venue = event_venue
            performers = event_performer
            time_string = event_list[i]['datetime_local']
            start_time = datetime(year=int(time_string[0:4]), month=int(time_string[5:7]), day=int(time_string[8:10]), hour=int(time_string[11:13]), minute=int(time_string[14:16]))
            popularity = event_popularity
            average_price = event_list[i]['stats']['lowest_price_good_deals']
            if not average_price:
                average_price = 55.00
            new_event = Event.objects.create(title = event_title, venue = venue, performers = performers, start_time = start_time, popularity = popularity, average_price = average_price)
            new_event.save()
    return

grab_data()


from pathlib import Path

import pytest
from pytest import param

import solution

files = [
    ("test_input_1.txt", 39, None),
    ("test_input_2.txt", 590784, None),
    ("test_input_3.txt", 1, None),
    ("test_input_4.txt", 1, None),
]

@pytest.mark.parametrize(
    "file,expected",
    [param(Path(file), expected, id=file) for file, expected, _ in files],
)
def test_part_1(file: Path, expected, capsys):
    lines = file.read_text().splitlines()

    with capsys.disabled():
        result = solution.part_1(solution.parse(lines))
    assert result == expected

@pytest.mark.parametrize(
    "file,expected",
    [param(Path(file), expected, id=file) for file, _, expected in files],
)
def test_part_2(file: Path, expected):
    lines = file.read_text().splitlines()

    result = solution.part_2(solution.parse(lines))
    assert result == expected

def test_intersection():
    c1 = solution.Cube(10, 20, 10, 20, 10, 20, True)
    c2 = solution.Cube(15, 25, 15, 25, 15, 25, True)
    assert c1.intersection(c2) == solution.Cube(15, 20, 15, 20, 15, 20, False)

def test_intersection_non_overlap():
    c1 = solution.Cube(10, 20, 10, 20, 10, 20, True)
    c2 = solution.Cube(21, 25, 21, 25, 21, 25, True)
    assert c1.intersection(c2) is None


#参考:https://blog.csdn.net/Likianta/article/details/90123678?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase

from time import sleep,time
import asyncio

def demo4():

    async def washing1():
        await asyncio.sleep(3)
        print('washer1 finished')
    """
    async def washing2():
        await asyncio.sleep(2)
        print('washer2 finished')

    """
    async def washing3():
        await washing1()
        print('washer3 finished')

    loop = asyncio.get_event_loop()

    tasks = [washing3(),]

    loop.run_until_complete(asyncio.wait(tasks))

    loop.close()

if __name__ == '__main__':
    start = time()
    demo4()
    end = time()
    print('elapsed time = ' + str(end - start))


import binascii

from libvirt import libvirtError
from xml.dom import minidom
from libvirttestapi.src import sharedmod
from libvirttestapi.utils import process

required_params = ('networkname',)
optional_params = {}

VIRSH_NETUUID = "virsh net-uuid"
NWPATH = "/etc/libvirt/qemu/networks/"

def check_network_exists(conn, networkname, logger):
    """ check if the network exists, may or may not be active """
    network_names = conn.listNetworks()
    network_names += conn.listDefinedNetworks()

    if networkname not in network_names:
        logger.error("%s doesn't exist" % networkname)
        return False
    else:
        return True

def check_network_uuid(networkname, UUIDString, logger):
    """ check UUID String of a network """
    ret = process.run(VIRSH_NETUUID + ' %s' % networkname, shell=True, ignore_status=True)
    if ret.exit_status:
        logger.error("executing " + "\"" + VIRSH_NETUUID + ' %s' % networkname +
                     "\"" + " failed")
        logger.error(ret.stderr)
        return False
    else:
        UUIDString_virsh = ret.stdout
        logger.debug("UUIDString from API is %s" % UUIDString)
        logger.debug("UUIDString from " + "\"" + VIRSH_NETUUID + "\"" " is %s"
                     % UUIDString_virsh)
        if UUIDString_virsh == UUIDString:
            return True
        else:
            return False

def checking_uuid(logger, nwname, nwuuid):
    """ compare two UUIDs, one is from API, another is from network XML"""
    global NWPATH
    NWPATH = NWPATH + nwname + ".xml"
    xml = minidom.parse(NWPATH)
    network = xml.getElementsByTagName('network')[0]
    uuid = network.getElementsByTagName('uuid')[0].childNodes[0].data
    if uuid == nwuuid:
        return True
    else:
        return False

def network_uuid(params):
    """ 1.call appropriate API to generate the UUIDStirng
          of a network , then compared to the output of command
          virsh net-uuid
        2.check below 2 new APIs:
           networkLookupByUUIDString
           networkLookupByUUID
    """
    global NWPATH
    logger = params['logger']
    networkname = params['networkname']

    conn = sharedmod.libvirtobj['conn']

    if not check_network_exists(conn, networkname, logger):
        logger.error("need a defined network")
        return 1

    netobj = conn.networkLookupByName(networkname)

    try:
        UUIDString = netobj.UUIDString()

        #For a transient network, set another path
        if not netobj.isPersistent() == 1:
            NWPATH = "/var/run/libvirt/network/"

        logger.info("the UUID string of network \"%s\" is \"%s\""
                    % (networkname, UUIDString))
        #allowing '-' and ' ' anywhere between character pairs, just
        #check one of them.
        UUIDString1 = UUIDString.replace("-", " ")
        network1 = conn.networkLookupByUUIDString(UUIDString1)
        nw_name1 = network1.name()
        logger.debug("The given UUID is \"%s\", the network is \"%s\" using\
 networkLookupByUUIDString" % (UUIDString1, nw_name1))

        UUIDString2 = UUIDString.replace("-", "")
        UUID_ascii = binascii.a2b_hex(UUIDString2)
        network2 = conn.networkLookupByUUID(UUID_ascii)
        nw_name2 = network2.name()
        logger.debug("The given UUID is \"%s\", the network is \"%s\" using \
networkLookupByUUID" % (UUIDString2, nw_name2))

        if nw_name1 == nw_name2 and checking_uuid(logger, nw_name1, UUIDString):
            logger.info("Successed to get network name \"%s\" using \"%s\""
                        % (nw_name1, UUIDString))

        if check_network_uuid(networkname, UUIDString, logger):
            logger.info(VIRSH_NETUUID + " test succeeded.")
            return 0
        else:
            logger.error(VIRSH_NETUUID + " test failed.")
            return 1
    except libvirtError as e:
        logger.error("API error message: %s, error code is %s"
                     % (e.get_error_message(), e.get_error_code()))
        return 1

    return 0