import json

import pytest
from click.testing import CliRunner

from sqlite_utils import Database, cli

def test_memory_basic():
    result = CliRunner().invoke(cli.cli, ["memory", "select 1 + 1"])
    assert result.exit_code == 0
    assert result.output.strip() == '[{"1 + 1": 2}]'

@pytest.mark.parametrize("sql_from", ("test", "t", "t1"))
@pytest.mark.parametrize("use_stdin", (True, False))
def test_memory_csv(tmpdir, sql_from, use_stdin):
    content = "id,name\n1,Cleo\n2,Bants"
    input = None
    if use_stdin:
        input = content
        csv_path = "-"
        if sql_from == "test":
            sql_from = "stdin"
    else:
        csv_path = str(tmpdir / "test.csv")
        with open(csv_path, "w") as fp:
            fp.write(content)
    result = CliRunner().invoke(
        cli.cli,
        ["memory", csv_path, "select * from {}".format(sql_from), "--nl"],
        input=input,
    )
    assert result.exit_code == 0
    assert (
        result.output.strip() == '{"id": 1, "name": "Cleo"}\n{"id": 2, "name": "Bants"}'
    )

@pytest.mark.parametrize("use_stdin", (True, False))
def test_memory_tsv(tmpdir, use_stdin):
    data = "id\tname\n1\tCleo\n2\tBants"
    if use_stdin:
        input = data
        path = "stdin:tsv"
        sql_from = "stdin"
    else:
        input = None
        path = str(tmpdir / "chickens.tsv")
        with open(path, "w") as fp:
            fp.write(data)
        path = path + ":tsv"
        sql_from = "chickens"
    result = CliRunner().invoke(
        cli.cli,
        ["memory", path, "select * from {}".format(sql_from)],
        input=input,
    )
    assert result.exit_code == 0, result.output
    assert json.loads(result.output.strip()) == [
        {"id": 1, "name": "Cleo"},
        {"id": 2, "name": "Bants"},
    ]

@pytest.mark.parametrize("use_stdin", (True, False))
def test_memory_json(tmpdir, use_stdin):
    data = '[{"name": "Bants"}, {"name": "Dori", "age": 1, "nested": {"nest": 1}}]'
    if use_stdin:
        input = data
        path = "stdin:json"
        sql_from = "stdin"
    else:
        input = None
        path = str(tmpdir / "chickens.json")
        with open(path, "w") as fp:
            fp.write(data)
        path = path + ":json"
        sql_from = "chickens"
    result = CliRunner().invoke(
        cli.cli,
        ["memory", path, "select * from {}".format(sql_from)],
        input=input,
    )
    assert result.exit_code == 0, result.output
    assert json.loads(result.output.strip()) == [
        {"name": "Bants", "age": None, "nested": None},
        {"name": "Dori", "age": 1, "nested": '{"nest": 1}'},
    ]

@pytest.mark.parametrize("use_stdin", (True, False))
def test_memory_json_nl(tmpdir, use_stdin):
    data = '{"name": "Bants"}\n\n{"name": "Dori"}'
    if use_stdin:
        input = data
        path = "stdin:nl"
        sql_from = "stdin"
    else:
        input = None
        path = str(tmpdir / "chickens.json")
        with open(path, "w") as fp:
            fp.write(data)
        path = path + ":nl"
        sql_from = "chickens"
    result = CliRunner().invoke(
        cli.cli,
        ["memory", path, "select * from {}".format(sql_from)],
        input=input,
    )
    assert result.exit_code == 0, result.output
    assert json.loads(result.output.strip()) == [
        {"name": "Bants"},
        {"name": "Dori"},
    ]

@pytest.mark.parametrize("use_stdin", (True, False))
def test_memory_csv_encoding(tmpdir, use_stdin):
    latin1_csv = (
        b"date,name,latitude,longitude\n" b"2020-03-04,S\xe3o Paulo,-23.561,-46.645\n"
    )
    input = None
    if use_stdin:
        input = latin1_csv
        csv_path = "-"
        sql_from = "stdin"
    else:
        csv_path = str(tmpdir / "test.csv")
        with open(csv_path, "wb") as fp:
            fp.write(latin1_csv)
        sql_from = "test"
    # Without --encoding should error:
    assert (
        CliRunner()
        .invoke(
            cli.cli,
            ["memory", csv_path, "select * from {}".format(sql_from), "--nl"],
            input=input,
        )
        .exit_code
        == 1
    )
    # With --encoding should work:
    result = CliRunner().invoke(
        cli.cli,
        ["memory", "-", "select * from stdin", "--encoding", "latin-1", "--nl"],
        input=latin1_csv,
    )
    assert result.exit_code == 0, result.output
    assert json.loads(result.output.strip()) == {
        "date": "2020-03-04",
        "name": "São Paulo",
        "latitude": -23.561,
        "longitude": -46.645,
    }

@pytest.mark.parametrize("extra_args", ([], ["select 1"]))
def test_memory_dump(extra_args):
    result = CliRunner().invoke(
        cli.cli,
        ["memory", "-"] + extra_args + ["--dump"],
        input="id,name\n1,Cleo\n2,Bants",
    )
    assert result.exit_code == 0
    expected = (
        "BEGIN TRANSACTION;\n"
        'CREATE TABLE IF NOT EXISTS "stdin" (\n'
        "   [id] INTEGER,\n"
        "   [name] TEXT\n"
        ");\n"
        "INSERT INTO \"stdin\" VALUES(1,'Cleo');\n"
        "INSERT INTO \"stdin\" VALUES(2,'Bants');\n"
        "CREATE VIEW t1 AS select * from [stdin];\n"
        "CREATE VIEW t AS select * from [stdin];\n"
        "COMMIT;"
    )
    # Using sqlite-dump it won't have IF NOT EXISTS
    expected_alternative = expected.replace("IF NOT EXISTS ", "")
    assert result.output.strip() in (expected, expected_alternative)

@pytest.mark.parametrize("extra_args", ([], ["select 1"]))
def test_memory_schema(extra_args):
    result = CliRunner().invoke(
        cli.cli,
        ["memory", "-"] + extra_args + ["--schema"],
        input="id,name\n1,Cleo\n2,Bants",
    )
    assert result.exit_code == 0
    assert result.output.strip() == (
        'CREATE TABLE "stdin" (\n'
        "   [id] INTEGER,\n"
        "   [name] TEXT\n"
        ");\n"
        "CREATE VIEW t1 AS select * from [stdin];\n"
        "CREATE VIEW t AS select * from [stdin];"
    )

@pytest.mark.parametrize("extra_args", ([], ["select 1"]))
def test_memory_save(tmpdir, extra_args):
    save_to = str(tmpdir / "save.db")
    result = CliRunner().invoke(
        cli.cli,
        ["memory", "-"] + extra_args + ["--save", save_to],
        input="id,name\n1,Cleo\n2,Bants",
    )
    assert result.exit_code == 0
    db = Database(save_to)
    assert list(db["stdin"].rows) == [
        {"id": 1, "name": "Cleo"},
        {"id": 2, "name": "Bants"},
    ]

@pytest.mark.parametrize("option", ("-n", "--no-detect-types"))
def test_memory_no_detect_types(option):
    result = CliRunner().invoke(
        cli.cli,
        ["memory", "-", "select * from stdin"] + [option],
        input="id,name,weight\n1,Cleo,45.5\n2,Bants,3.5",
    )
    assert result.exit_code == 0, result.output
    assert json.loads(result.output.strip()) == [
        {"id": "1", "name": "Cleo", "weight": "45.5"},
        {"id": "2", "name": "Bants", "weight": "3.5"},
    ]

def test_memory_flatten():
    result = CliRunner().invoke(
        cli.cli,
        ["memory", "-", "select * from stdin", "--flatten"],
        input=json.dumps(
            {
                "httpRequest": {
                    "latency": "0.112114537s",
                    "requestMethod": "GET",
                },
                "insertId": "6111722f000b5b4c4d4071e2",
            }
        ),
    )
    assert result.exit_code == 0, result.output
    assert json.loads(result.output.strip()) == [
        {
            "httpRequest_latency": "0.112114537s",
            "httpRequest_requestMethod": "GET",
            "insertId": "6111722f000b5b4c4d4071e2",
        }
    ]

def test_memory_analyze():
    result = CliRunner().invoke(
        cli.cli,
        ["memory", "-", "--analyze"],
        input="id,name\n1,Cleo\n2,Bants",
    )
    assert result.exit_code == 0
    assert result.output == (
        "stdin.id: (1/2)\n\n"
        "  Total rows: 2\n"
        "  Null rows: 0\n"
        "  Blank rows: 0\n\n"
        "  Distinct values: 2\n\n"
        "stdin.name: (2/2)\n\n"
        "  Total rows: 2\n"
        "  Null rows: 0\n"
        "  Blank rows: 0\n\n"
        "  Distinct values: 2\n\n"
    )

def test_memory_two_files_with_same_stem(tmpdir):
    (tmpdir / "one").mkdir()
    (tmpdir / "two").mkdir()
    one = tmpdir / "one" / "data.csv"
    two = tmpdir / "two" / "data.csv"
    one.write_text("id,name\n1,Cleo\n2,Bants", encoding="utf-8")
    two.write_text("id,name\n3,Blue\n4,Lila", encoding="utf-8")
    result = CliRunner().invoke(cli.cli, ["memory", str(one), str(two), "", "--schema"])
    assert result.exit_code == 0
    assert result.output == (
        'CREATE TABLE "data" (\n'
        "   [id] INTEGER,\n"
        "   [name] TEXT\n"
        ");\n"
        "CREATE VIEW t1 AS select * from [data];\n"
        "CREATE VIEW t AS select * from [data];\n"
        'CREATE TABLE "data_2" (\n'
        "   [id] INTEGER,\n"
        "   [name] TEXT\n"
        ");\n"
        "CREATE VIEW t2 AS select * from [data_2];\n"
    )

def test_memory_functions():
    result = CliRunner().invoke(
        cli.cli,
        ["memory", "select hello()", "--functions", "hello = lambda: 'Hello'"],
    )
    assert result.exit_code == 0
    assert result.output.strip() == '[{"hello()": "Hello"}]'


import os
import random
from utils import normalize_text
from os import path
import sys

def remove_dir_files(path):
    """
    Removes all files in the directory
    """
    for file in os.listdir(path):
        os.remove(os.path.join(path, file))

def update_dir(path, data, msg=""):
    """
    Updates the directory with the data saved in individual files
    """
    remove_dir_files(path)
    for i in range(len(data)):
        file = open(os.path.join(path, str(i) + ".txt"), "w")
        file.write(data[i])
        file.close()
        if (i == 100):
            print(f"{msg} : First 100 files done")
        if (i % 1000 == 0):
            print(f"{msg} {i}/{len(data)}")

def open_file(file_path):
    file = open(file_path, 'r', encoding="utf8")
    lines = file.readlines()
    file.close()
    return lines

def main_process_data(args):
    remove = True if "--remove-stopwords" in args else False
    lemmatize = True if "--lemmatize" in args else False
    if remove:
        print("Removing stopwords")
    if lemmatize:
        print("Lemmatizing")
    process(remove, lemmatize)

def process(remove_stopwords=False, lemmatize=False):
    cwd_path = path.dirname(path.realpath(__file__))
    lines = open_file(path.join(cwd_path, "data.csv"))
    bot = []
    human = []
    print("Total lines: ", len(lines))
    max = len(lines)
    i = 0
    # Add normalized text to bot and human lists
    for raw_line in lines:
        line = raw_line.strip().split(",")
        line = line
        if (len(line) <= 1):
            continue
        if line[-1] == "1":
            bot.append(normalize_text(" ".join(line[:-1]), remove_stopwords, lemmatize))
        elif line[-1] == "0":
            human.append(normalize_text(" ".join(line[:-1]), remove_stopwords, lemmatize))
        else:
            pass
        i += 1
        if (i % 1000 == 0):
            print(f"{i}/{max}")

    print("Bot: ", len(bot))
    print("Human: ", len(human))
    print("Total: ", len(bot) + len(human))
    print("Unlabeled: ", len(lines) - len(bot) - len(human))

    TRAINING_PERCENT = 0.7
    VALIDATION_PERCENT = 0.2
    TESTING_PERCENT = 0.1

    # Set random seed for reproducibility
    random.seed(42)

    # Shuffle data
    random.shuffle(bot)
    random.shuffle(human)

    # Split data
    bot_train = bot[:int(len(bot) * TRAINING_PERCENT)]
    bot_test = bot[int(len(bot) * (TRAINING_PERCENT + VALIDATION_PERCENT)):]
    human_train = human[:int(len(human) * TRAINING_PERCENT)]
    human_test = human[int(len(human) * (TRAINING_PERCENT + VALIDATION_PERCENT)):]
    print("Bot train: ", len(bot_train))
    print("Bot test: ", len(bot_test))
    print("Human train: ", len(human_train))
    print("Human test: ", len(human_test))

    # Clear bot and human directories

    data_path = path.join(cwd_path, "data")
    test_dir = os.path.join(data_path, "test")
    train_dir = os.path.join(data_path, "train")

    bot_train_path = os.path.join(train_dir, "bot")
    bot_test_path = os.path.join(test_dir, "bot")
    human_train_path = os.path.join(train_dir, "human")
    human_test_path = os.path.join(test_dir, "human")

    update_dir(bot_train_path, bot_train, "bot train")
    update_dir(bot_test_path, bot_test, "bot test")
    update_dir(human_train_path, human_train, "human train")
    update_dir(human_test_path, human_test, "human test")

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


import cv2
import numpy as np

# 读取图片，彩色模式
img_color = cv2.imread('faces.jpg',cv2.IMREAD_COLOR)
# 读取图片，灰度模式
img_gray = cv2.imread('faces.jpg',cv2.IMREAD_GRAYSCALE)
# 读取人脸模板图片，灰度模式
template = cv2.imread('face_template1.jpg',cv2.IMREAD_GRAYSCALE)
# 获取模板尺寸
w, h = template.shape[::-1]
# 模板匹配方法数组
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
# 遍历匹配方法
for meth in methods:
    # 拷贝图片
    img_color2 = img_color.copy()
    img_gray2 = img_gray.copy()
    # 把字符串转换成代码
    method = eval(meth)
    # 模板匹配
    res = cv2.matchTemplate(img_gray2,template,method)
    # 获取匹配结果的最大、最小值，及其位置
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    # TM_SQDIFF 和 TM_SQDIFF_NORMED匹配方法：值越小，越相似
    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
        # 取最小值位置，作为矩形框左上角位置
        top_left = min_loc
    else:
        # 取最大值位置，作为矩形框左上角位置
        top_left = max_loc
    # 根据模板尺寸计算出：矩形框右下角位置
    bottom_right = (top_left[0] + w, top_left[1] + h)
    # 画矩形框
    cv2.rectangle(img_color2,top_left, bottom_right, 255, 2)
    # 显示画好矩形框的图片
    cv2.namedWindow(meth, cv2.WINDOW_AUTOSIZE)
    cv2.imshow(meth,img_color2)
    # 等待退出键
    cv2.waitKey(0)
# 销毁显示窗口
cv2.destroyAllWindows()


# COMPLETE EXPLAINATION AT :- https://youtu.be/CuR0DVzToi4
'''
author: cYpHeR
github: cypher-nullbyte
'''
import math
import numpy
import sympy

def modInverse(a, m) :
    a = a % m
    for x in range(1, m) :
        if ((a * x) % m == 1) :
            return x
    return 1
def KeyMatrix(Key,n):
    Matrix=[]
    for i in range(n):
        temp=list()
        for j in range(n):
            temp.append(ord(Key[i*n+j])-97)
        Matrix.append(temp)
    d=math.floor(numpy.linalg.det(Matrix))
    if d==0:
        print("Invalid Key!")
        exit(None)
    A=sympy.Matrix(Matrix)
    A=(A.adjugate()*modInverse(d,26))%26
    for i in range(n):
        for j in range(n):
            Matrix[i][j]=A[i,j]
    return Matrix

def Multiply(X,Y):
    result = numpy.zeros([len(X),1], dtype = int)
    Y=list([ord(x)-97] for x in Y)
    result=numpy.dot(X,Y)
    return result
def Hill_decrypt(Matrix,Cipher,n):
    Plain=""
    for i in range(0,len(Cipher),n):
        temp=Multiply(Matrix,Cipher[i:i+n])
        for x in range(n):
            Plain+=chr(temp[x][0]%26+97)
    return Plain
if __name__=="__main__":
    Key=''.join(input("Enter Key: ").lower().split())
    Cipher=input("Enter CipherText: ")
    n=math.sqrt(len(Key))
    if n!=math.trunc(n) and n!=0:
        print("Invalid key!")
        exit(None)
    n=math.floor(n)
    Matrix=KeyMatrix(Key,n)
    print("PlainText: ",Hill_decrypt(Matrix,Cipher,n))


from flask import Blueprint, g, current_app, jsonify
from flask_restful import Api, Resource, inputs
from flask_restful.reqparse import RequestParser

from models import *
from lib.decorators import api_login_required

bp = Blueprint('my', __name__)

api = Api(bp)

class HistoryResource(Resource):
    """
    我的浏览记录
    """
    method_decorators = {
        'get': [api_login_required],
        'delete': [api_login_required]
    }

    def get(self):
        """
        获取我的浏览记录
        :return:
        """
        rp = RequestParser()
        rp.add_argument('page', type=inputs.positive, default=1, location='args')
        rp.add_argument('pagesize', type=inputs.positive, default=10, location='args')
        req = rp.parse_args()

        user = User.query.get(g.user_id)
        paginate = BrowseHistory.query.filter_by(user_id=g.user_id).paginate(req.page, req.pagesize, False)
        items = []
        for item in paginate.items:
            items.append({
                'id': item.book.book_id,
                'title': item.book.book_name,
                'author': item.book.author_name,
                'state': item.book.status,
                'imgURL': 'http://{}/{}'.format(current_app.config['QINIU_SETTINGS']['host'], item.book.cover),
                'lastTime': item.updated.strftime('%Y-%m-%d %H:%M:%S')
            })
        data = {
            'counts': paginate.total,
            'pagesize': req.pagesize,
            'pages': paginate.pages,
            'page': paginate.page,
            'items': items
        }
        return jsonify(data)

    def delete(self):
        """
        清空浏览记录
        :return:
        """
        historys = BrowseHistory.query.filter_by(user_id=g.user_id).all()
        for h in historys:
            db.session.delete(h)
        db.session.commit()
        return {'msg': 'ok'}

api.add_resource(HistoryResource, '/historys')


import cv2
import tkinter as tk
from tkinter import Entry, Canvas
from pyzbar.pyzbar import decode
from PIL import Image, ImageTk

import tkinter as tk
from tkinter import Entry, Canvas
from PIL import Image, ImageTk
import cv2

def create_qr_scanner_app(root):
    root.title("QR Scanner")

    entry_var = tk.StringVar()
    entry = Entry(root, textvariable=entry_var, width=40)
    entry.pack(pady=10)

    start_scanning_button = tk.Button(root, text="Start Scanning", command=lambda: start_scanning(root, entry_var, canvas, cap))
    start_scanning_button.pack(pady=10)

    canvas = Canvas(root, width=640, height=480)
    canvas.pack()

    cap = cv2.VideoCapture(0)

    root.protocol("WM_DELETE_WINDOW", lambda: on_closing(cap, root))

    return root, entry_var, entry, canvas, cap

def start_scanning(root, entry_var, canvas, cap):
    scanning = True
    root.after(10, lambda: scan(root, entry_var, canvas, cap, scanning))

def scan(root, entry_var, canvas, cap, scanning):
    if scanning:
        _, frame = cap.read()
        decoded_objects = decode(frame)

        for obj in decoded_objects:
            data = obj.data.decode("utf-8")
            entry_var.set(data)

        if _:
            image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            image = Image.fromarray(image)
            photo = ImageTk.PhotoImage(image=image)
            canvas.create_image(0, 0, anchor=tk.NW, image=photo)
            canvas.image = photo

        root.after(10, lambda: scan(root, entry_var, canvas, cap, scanning))

def on_closing(cap, root):
    cap.release()
    root.destroy()

# Ví dụ cách sử dụng
root = tk.Tk()
create_qr_scanner_app(root)
root.mainloop()


import time
from typing import List

import volatility.framework.interfaces.plugins as plugins
from volatility.framework import constants, interfaces, layers
from volatility.framework.configuration import requirements
from volatility.framework.renderers import TreeGrid
from volatility.framework.symbols.windows.kdbg import KdbgIntermedSymbols
from volatility.framework.symbols.windows.pe import PEIntermedSymbols

class Info(plugins.PluginInterface):
    """Show OS & kernel details of the memory sample being analyzed"""

    @classmethod
    def get_requirements(cls) -> List[interfaces.configuration.RequirementInterface]:
        return [
            requirements.TranslationLayerRequirement(
                name = 'primary', description = 'Memory layer for the kernel', architectures = ["Intel32", "Intel64"]),
            requirements.SymbolTableRequirement(name = "nt_symbols", description = "Windows kernel symbols")
        ]

    def get_depends(self, layer_name: str, index: int = 0):
        """List the dependencies of a given layer.

        Args:
            layer_name: the name of the starting layer
            index: the index/order of the layer
        """
        layer = self.context.memory[layer_name]
        yield index, layer
        try:
            for depends in layer.dependencies:
                for j, dep in self.get_depends(depends, index + 1):
                    yield j, self.context.memory[dep.name]
        except AttributeError:
            # FileLayer won't have dependencies
            pass

    def _generator(self):

        virtual_layer_name = self.config["primary"]
        virtual_layer = self.context.memory[virtual_layer_name]
        if not isinstance(virtual_layer, layers.intel.Intel):
            raise TypeError("Virtual Layer is not an intel layer")

        native_types = self.context.symbol_space[self.config["nt_symbols"]].natives

        kdbg_table_name = KdbgIntermedSymbols.create(
            self.context, self.config_path, "windows", "kdbg", native_types = native_types)

        pe_table_name = PEIntermedSymbols.create(self.context, self.config_path, "windows", "pe")

        kvo = virtual_layer.config["kernel_virtual_offset"]

        ntkrnlmp = self.context.module(self.config["nt_symbols"], layer_name = virtual_layer_name, offset = kvo)

        kdbg_offset = ntkrnlmp.get_symbol("KdDebuggerDataBlock").address

        kdbg = self.context.object(
            kdbg_table_name + constants.BANG + "_KDDEBUGGER_DATA64",
            offset = kvo + kdbg_offset,
            layer_name = virtual_layer_name)

        yield (0, ("Memory Location", self.config["primary.memory_layer.location"]))
        yield (0, ("Kernel Base", hex(self.config["primary.kernel_virtual_offset"])))
        yield (0, ("DTB", hex(self.config["primary.page_map_offset"])))
        yield (0, ("Symbols", self.config["nt_symbols.isf_url"]))

        for i, layer in self.get_depends("primary"):
            yield (0, (layer.name, "{} {}".format(i, layer.__class__.__name__)))

        if kdbg.Header.OwnerTag == 0x4742444B:

            yield (0, ("KdDebuggerDataBlock", hex(kdbg.vol.offset)))
            yield (0, ("NTBuildLab", kdbg.get_build_lab()))
            yield (0, ("CSDVersion", str(kdbg.get_csdversion())))

        vers_offset = ntkrnlmp.get_symbol("KdVersionBlock").address

        vers = ntkrnlmp.object(
            type_name = "_DBGKD_GET_VERSION64", layer_name = virtual_layer_name, offset = kvo + vers_offset)

        yield (0, ("KdVersionBlock", hex(vers.vol.offset)))
        yield (0, ("Major/Minor", "{0}.{1}".format(vers.MajorVersion, vers.MinorVersion)))
        yield (0, ("MachineType", str(vers.MachineType)))

        cpu_count_offset = ntkrnlmp.get_symbol("KeNumberProcessors").address

        cpu_count = ntkrnlmp.object(
            type_name = "unsigned int", layer_name = virtual_layer_name, offset = kvo + cpu_count_offset)

        yield (0, ("KeNumberProcessors", str(cpu_count)))

        # this is a hard-coded address in the Windows OS
        if virtual_layer.bits_per_register == 32:
            kuser_addr = 0xFFDF0000
        else:
            kuser_addr = 0xFFFFF78000000000

        kuser = ntkrnlmp.object(type_name = "_KUSER_SHARED_DATA", layer_name = virtual_layer_name, offset = kuser_addr)

        yield (0, ("SystemTime", str(kuser.SystemTime.get_time())))
        yield (0, ("NtSystemRoot",
                   str(kuser.NtSystemRoot.cast("string", encoding = "utf-16", errors = "replace", max_length = 260))))
        yield (0, ("NtProductType", str(kuser.NtProductType.description)))
        yield (0, ("NtMajorVersion", str(kuser.NtMajorVersion)))
        yield (0, ("NtMinorVersion", str(kuser.NtMinorVersion)))
        # yield (0, ("KdDebuggerEnabled", "True" if kuser.KdDebuggerEnabled else "False"))
        # yield (0, ("SafeBootMode", "True" if kuser.SafeBootMode else "False"))

        dos_header = self.context.object(
            pe_table_name + constants.BANG + "_IMAGE_DOS_HEADER", offset = kvo, layer_name = virtual_layer_name)

        nt_header = dos_header.get_nt_header()

        yield (0, ("PE MajorOperatingSystemVersion", str(nt_header.OptionalHeader.MajorOperatingSystemVersion)))
        yield (0, ("PE MinorOperatingSystemVersion", str(nt_header.OptionalHeader.MinorOperatingSystemVersion)))

        yield (0, ("PE Machine", str(nt_header.FileHeader.Machine)))
        yield (0, ("PE TimeDateStamp", time.asctime(time.gmtime(nt_header.FileHeader.TimeDateStamp))))

    def run(self):

        return TreeGrid([("Variable", str), ("Value", str)], self._generator())


import functools

import time
from flask import request, Response, make_response, jsonify, current_app
from flask_classy import FlaskView
from tigereye.helper.code import Code

# 我们自己写aipview
class ApiView(FlaskView):
    # 使用钩子函数来测试接口的响应时间
    def before_request(self):
        self.request_start_time = time.time()
    def after_request(self):
        current_app.logger.info('%s response time: %s' %(request))
    #     ——————————————————————————————————

    # 重写方法，这个方法是一个装饰器，装饰的是我们再view里面写的方法比如 get index play hosts
    @classmethod
    def make_proxy_method(cls, name):
        """Creates a proxy function that can be used by Flasks routing. The
        proxy instantiates the FlaskView subclass and calls the appropriate
        method.

        :param name: the name of the method to create a proxy for
        """

        i = cls()
        view = getattr(i, name)

        if cls.decorators:
            for decorator in cls.decorators:
                view = decorator(view)

        @functools.wraps(view)
        def proxy(**forgettable_view_args):
            # Always use the global request object's view_args, because they
            # can be modified by intervening function before an endpoint or
            # wrapper gets called. This matches Flask's behavior.
            del forgettable_view_args

            if hasattr(i, "before_request"):
                response = i.before_request(name, **request.view_args)
                if response is not None:
                    return response

            before_view_name = "before_" + name
            if hasattr(i, before_view_name):
                before_view = getattr(i, before_view_name)
                response = before_view(**request.view_args)
                if response is not None:
                    return response

            # 这里的view就是我们要装饰的方法，这里还不算一种response类型。
            response = view(**request.view_args)

            #判断是否是一个Response对象
            if not isinstance(response,Response):
                # 如果不是，则先获取它的类型
                response_type = type(response)
                #如果是tuple类型
                if  response_type == tuple and len(response)> 1:
                    # 不成功的话，我们走这里，我们api里面返回的两个参数就是请求失败
                    rc,_data = response
                    #     加上我们的自己APi接口的信息
                    #  jsonify源码逻辑是先dumps然后再makeresponse
                    # return jsonify(rc = rc.value,msg = rc.name,data = _data)
                    response =  jsonify(rc=rc.value, msg=rc.name, data=_data)
                else:
                    # 这些Code.succ.name 都是再helper里面定义一个继承eum类的方法
                    # 写死的代码，出现rc = 1 就是硬编码
                    # 其实即使我们再view里面调用的jsonify 统一到这里了。我们就不用再每个方法里加了
                    # return jsonify(rc=Code.succ.value,msg=Code.succ.name,data = response)
                    response =  jsonify(rc=Code.succ.value,msg=Code.succ.name,data = response)

            # if not isinstance(response, Response):
            #     response = make_response(response)

            after_view_name = "after_" + name
            if hasattr(i, after_view_name):
                after_view = getattr(i, after_view_name)
                response = after_view(response)

            if hasattr(i, "after_request"):
                response = i.after_request(name, response)

            return response

        return proxy


import numpy as np

import pandas as pd

import spacy

from spacy import displacy

nlp = spacy.load('en_core_web_sm')

import nltk

from sklearn import *

test = pd.read_csv('../input/test_stage_1.tsv', delimiter='\t').rename(columns={'A': 'A_Noun', 'B': 'B_Noun'})

sub = pd.read_csv('../input/sample_submission_stage_1.csv')

test.shape, sub.shape
test['mask'] = test['Text'].map(lambda x: '*' * len(x))

test['mask'] = test.apply(lambda r: r['mask'][: r['Pronoun-offset']] + 'P' * len(str(r['Pronoun'])) + r['mask'][r['Pronoun-offset'] + len(str(r['Pronoun'])): ], axis=1)

test['mask'] = test.apply(lambda r: r['mask'][: r['A-offset']] + 'A' * len(str(r['A_Noun'])) + r['mask'][r['A-offset'] + len(str(r['A_Noun'])): ], axis=1)

test['mask'] = test.apply(lambda r: r['mask'][: r['B-offset']] + 'B' * len(str(r['B_Noun'])) + r['mask'][r['B-offset'] + len(str(r['B_Noun'])): ], axis=1)

test['section_min'] = test[['Pronoun-offset', 'A-offset', 'B-offset']].min(axis=1)

test['Pronoun-offset2'] = test['Pronoun-offset'] + test['Pronoun'].map(len)

test['A-offset2'] = test['A-offset'] + test['A_Noun'].map(len)

test['B-offset2'] = test['B-offset'] + test['B_Noun'].map(len)

test['section_max'] = test[['Pronoun-offset2', 'A-offset2', 'B-offset2']].max(axis=1)

print(test['Text'][0][test['section_min'][0]:test['section_max'][0]])

print(test['mask'][0][test['section_min'][0]:test['section_max'][0]])
doc = nlp(test['Text'][0][test['section_min'][0]:test['section_max'][0]])

tokens = pd.DataFrame(

    [[token.text, token.lemma_, token.pos_, token.tag_, token.dep_, token.shape_, token.is_alpha, token.is_stop, [child for child in token.children]] for token in doc],

    columns=['text', 'lemma', 'pos', 'tag', 'dep', 'shape', 'is_alpha', 'is_stop', 'child'])

tokens
test['A'] = test.apply(lambda r: 0.45 if r['A-offset'] == r['section_min'] else 0.4, axis=1)

test['B'] = test.apply(lambda r: 0.3 if r['B-offset'] == r['section_min'] else 0.45, axis=1)

test['NEITHER'] = 1.0 - (test['A'] + test['B'])
#metrics.log_loss()
test[['ID', 'A', 'B', 'NEITHER']].to_csv('submission.csv', index=False)


__author__ = 'RicksLab'
__copyright__ = 'Copyright (C) 2020 RicksLab'
__license__ = 'GNU General Public License'
__program_name__ = 'gpu-utils'
__maintainer__ = 'RicksLab'
__docformat__ = 'reStructuredText'
# pylint: disable=multiple-statements
# pylint: disable=line-too-long
# pylint: disable=consider-using-f-string

from typing import Tuple, Dict
import sys
import re
import logging
import warnings

try:
    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk, Gdk
except ModuleNotFoundError as error:
    print('gi import error: {}'.format(error))
    print('gi is required for {}'.format(__program_name__))
    print('   In a venv, first install vext:  pip install --no-cache-dir vext')
    print('   Then install vext.gi:  pip install --no-cache-dir vext.gi')
    sys.exit(0)

from GPUmodules import env

ColorDict = Dict[str, str]
LOGGER = logging.getLogger('gpu-utils')
PATTERNS = env.GutConst.PATTERNS

def get_color(value: str) -> str:
    """
    Get the rgb hex string for the provided color name.

    :param value: A valid project color name.
    :return: rrb value as a hex string.
    """
    return GuiProps.color_name_to_hex(value)

class GuiProps:
    """
    Class to manage style properties of Gtk widgets.
    """
    _colors: ColorDict = {'white':     '#FFFFFF',
                          'white_off': '#FCFCFC',
                          'white_pp':  '#F0E5D3',
                          'cream':     '#FFFDD1',
                          'gray20':    '#CCCCCC',
                          'gray50':    '#7F7F7F',
                          'gray60':    '#666666',
                          'gray70':    '#4D4D4D',
                          'gray80':    '#333333',
                          'gray95':    '#0D0D0D',
                          'gray_dk':   '#6A686E',
                          'black':     '#000000',
                          # Colors Low Contrast - For table fields
                          'green':     '#8EC3A7',
                          'green_dk':  '#6A907C',
                          'teal':      '#218C8D',
                          'olive':     '#6C9040',
                          'red':       '#B73743',
                          'orange':    '#E86850',
                          'yellow':    '#C9A100',
                          'blue':      '#587498',
                          'purple':    '#6264A7',
                          # Colors Bright - For plot lines
                          'br_red':    '#FF2D2D',
                          'br_orange': '#FF6316',
                          'br_blue':   '#66CCFF',
                          'br_pink':   '#CC00FF',
                          'br_green':  '#99FF99',
                          'br_yellow': '#FFFF66',
                          # Slate - For table fields
                          'slate_lt':  '#A0A0AA',
                          'slate_md':  '#80808d',
                          'slate_dk':  '#5D5D67',
                          'slate_vdk': '#3A3A40'}

    @staticmethod
    def color_name_to_hex(value: str) -> str:
        """
        Return the hex code for the given string.  The specified string must exist in the project color list.

        :param value: Color name
        :return: Color hex code
        """
        if value not in GuiProps._colors:
            raise ValueError('Invalid color name {} not in {}'.format(value, GuiProps._colors))
        return GuiProps._colors[value]

    @staticmethod
    def color_name_to_rgba(value: str) -> Tuple[float, ...]:
        """
        Convert the given color name to a color tuple.  The given color string mus exist in the project
        color list.

        :param value:  Color name
        :return: Color tuple
        """
        if value not in GuiProps._colors:
            raise ValueError('Invalid color name {} not in {}'.format(value, GuiProps._colors))
        return GuiProps.hex_to_rgba(GuiProps._colors[value])

    @staticmethod
    def hex_to_rgba(value: str) -> Tuple[float, ...]:
        """
        Return rgba tuple for give hex color name.

        :param value: hex color value as string
        :return:  rgba tuple
        .. note:: Code copied from Stack Overflow
        """
        if not re.fullmatch(PATTERNS['HEXRGB'], value):
            raise ValueError('Invalid hex color format in {}'.format(value))
        value = value.lstrip('#')
        if len(value) != 6:
            raise ValueError('Invalid hex color format in {}'.format(value))
        (r_1, g_1, b_1, a_1) = tuple(int(value[i:i + 2], 16) for i in range(0, 6, 2)) + (1,)
        (r_1, g_1, b_1, a_1) = (r_1 / 255.0, g_1 / 255.0, b_1 / 255.0, a_1)
        return tuple([r_1, g_1, b_1, a_1])

    @staticmethod
    def set_gtk_prop(gui_item, top: int = None, bottom: int = None, right: int = None,
                     left: int = None, width: int = None, width_chars: int = None, width_max: int = None,
                     max_length: int = None, align: tuple = None, xalign: float = None) -> None:
        """
        Set properties of Gtk objects.

        :param gui_item: Gtk object
        :param top: Top margin
        :param bottom: Bottom margin
        :param right: Right margin
        :param left: Left margin
        :param width: Width of request field
        :param width_chars: Width of label
        :param width_max: Max Width of object
        :param max_length: max length of entry
        :param align: Alignment parameters
        :param xalign: X Alignment parameter
        """
        if top:
            gui_item.set_property('margin-top', top)
        if bottom:
            gui_item.set_property('margin-bottom', bottom)
        if right:
            gui_item.set_property('margin-right', right)
        if left:
            gui_item.set_property('margin-left', left)
        if width:
            gui_item.set_property('width-request', width)
        if width_max:
            gui_item.set_max_width_chars(width_max)
        if width_chars:
            gui_item.set_width_chars(width_chars)
        if max_length:
            gui_item.set_max_length(max_length)
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', category=DeprecationWarning)
            if xalign:
                # FIXME - This is deprecated in latest Gtk, need to use halign
                gui_item.set_alignment(xalign=xalign)
            if align:
                # FIXME - This is deprecated in latest Gtk, need to use halign
                gui_item.set_alignment(*align)

    @classmethod
    def set_style(cls, css_str=None) -> None:
        """
        Set the specified css style, or set default styles if no css string is specified.

        :param css_str: A valid css format string.
        """
        css_list = []
        if css_str is None:
            # Initialize formatting colors.
            css_list.append("grid { background-image: image(%s); }" % cls._colors['gray80'])
            css_list.append("#light_grid { background-image: image(%s); }" % cls._colors['gray20'])
            css_list.append("#dark_grid { background-image: image(%s); }" % cls._colors['gray70'])
            css_list.append("#dark_box { background-image: image(%s); }" % cls._colors['slate_dk'])
            css_list.append("#med_box { background-image: image(%s); }" % cls._colors['slate_md'])
            css_list.append("#light_box { background-image: image(%s); }" % cls._colors['slate_lt'])
            css_list.append("#head_box { background-image: image(%s); }" % cls._colors['blue'])
            css_list.append("#warn_box { background-image: image(%s); }" % cls._colors['red'])
            css_list.append("#button_box { background-image: image(%s); }" % cls._colors['slate_dk'])
            css_list.append("#message_box { background-image: image(%s); }" % cls._colors['gray50'])
            css_list.append("#message_label { color: %s; }" % cls._colors['white_off'])
            css_list.append("#warn_label { color: %s; }" % cls._colors['white_pp'])
            css_list.append("#white_label { color: %s; }" % cls._colors['white_off'])
            css_list.append("#black_label { color: %s; }" % cls._colors['gray95'])
            css_list.append("#ppm_combo { background-image: image(%s); color: %s; }" %
                            (cls._colors['green'], cls._colors['black']))
            css_list.append("button { background-image: image(%s); color: %s; }" %
                            (cls._colors['slate_lt'], cls._colors['black']))
            css_list.append("entry { background-image: image(%s); color: %s; }" %
                            (cls._colors['green'], cls._colors['gray95']))
            # Below format does not work.
            css_list.append("entry:selected { background-image: image(%s); color: %s; }" %
                            (cls._colors['yellow'], cls._colors['white']))
        else:
            css_list.append(css_str)
        LOGGER.info('css %s', css_list)

        screen = Gdk.Screen.get_default()

        for css_item in css_list:
            provider = Gtk.CssProvider()
            css = css_item.encode('utf-8')
            provider.load_from_data(css)
            style_context = Gtk.StyleContext()
            style_context.add_provider_for_screen(screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)


from __future__ import print_function
import numpy as np
import tensorflow as tf
from six.moves import cPickle as pickle
from six.moves import range

image_size = 28
num_labels = 10
train_subset = 10000

batch_size = 128
num_steps = 3001

def accuracy(predictions, labels):
	return (100.0*np.sum(np.argmax(predictions, 1) == np.argmax(labels,1))/predictions.shape[0])

def graphRun(graph, optimizer, loss, tf_train_dataset, tf_train_labels, train_dataset, train_labels, train_prediction, valid_prediction, valid_labels, test_prediction, test_labels):
	with tf.Session(graph = graph) as session:
		tf.global_variables_initializer().run()
		print("Initialized")
		for step in range(num_steps):
			# Pick an offset within the training data, which has been randomized.
			# Note: we could use better randomization across epochs.
			offset = (step*batch_size) % (train_labels.shape[0] - batch_size)
			#offset is not in ascending order. You move around the whole train_dataset. Adv: add more variation in a train data. Remark: it is even better if offset is chosen randomly.

			# Generate a minibatch.
			batch_data = train_dataset[offset:(offset + batch_size), :]
			batch_labels = train_labels[offset:(offset + batch_size),:]

			# Prepare a dictionary telling the session where to feed the minibatch
			# The key of the dictionary is the placeholder node of the graph to be fed,
			# and the value is the numpy array to feed to it.

			feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}

			_,l,predictions = session.run([optimizer, loss, train_prediction], feed_dict = feed_dict)
			if ( step % 500 == 0):
				print("offset %d" % offset);
				print("Minibatch loss at step %d: %f" % (step, l))
				print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels))
				print("Validation accuracy: %.1f%%" % accuracy(valid_prediction.eval(), valid_labels))

		print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels))


from itertools import product

from binary_values import binary_values

def main():
    vals = range(- 2 ** 7, 2 ** 7)
    binvals = binary_values(vals, 8)
    ops = list(product(vals, vals))
    binops = list(product(binvals, binvals))
    products = []
    for o in ops:
        products.append(o[0] * o[1])
    binproducts = binary_values(products, 16)
    with open('rx4boothmult_in.txt', 'w') as file:
        file.writelines([f"{o[0]} {o[1]}\n" for o in binops])
    with open('rx4boothmult_out.txt', 'w') as file:
        file.writelines([f"{p}\n" for p in binproducts])

if __name__ == '__main__':
    main()


import random
import strings_manager as sm
import classifier as cl
from data import knowledge_base as kb
from data import keywords as kw
FLAG = "fast"

# given a key, it returns randomly one of its value
def find_value(key):
    values = kb.dictionary[key]
    return values[random.randint(0, (len(values)-1))]

# given a value, it returns its key
def get_key(val):
    for key, values in kw.keywords_use.items():
        for value in values:
            if val == value:
                return key
    return "key doesn't exist"

# given a sentence, it finds the keywords in it
def find_keywords(sentence):
    keywords_list = []
    for keyword in kb.keywords:
        if keyword[0] in sentence:
            keywords_list.append(keyword)
    return keywords_list

# given a sentence, it finds the rate in it
def find_rate(sentence):
    final_rate = None
    for rate in kb.rates:
        if rate in sentence.lower():
            final_rate = rate
            break  # if the user writes 9 out of 10, LEO saves 9
    return final_rate

# it asks the user to give a rate and it saves it
def ask_for_rate(reaction):
    question = sm.choose_sentence("rating")
    if "*" in question:
        question = sm.replace_a_star(question, reaction)
    sm.my_print_string(question, FLAG)
    rate_answer = input()
    rate = find_rate(rate_answer)
    while not rate:
        output = find_value("wrong rating")
        sm.my_print_string(output, FLAG)
        rate_answer = input()
        rate = find_rate(rate_answer)
    return rate

# it finds the typology of the keyword (the number) to know which particle use
def find_typology(keyword):
    typology = None
    for keyword_tuple in kb.keywords:
        if keyword == keyword_tuple[0]:
            typology = keyword_tuple[1]
            break
    return typology

# it saves all the keywords in the argument messages
def update_messages(messages):
    messages.clear()
    messages_list = list(kw.keywords_use.values())
    # message_list is a list of list of values from the dictionary
    for mes_list in messages_list:
        for elem in mes_list:
            messages.append(elem)
    return messages

# it prints what has been saved from user's answers
def print_db(concerns, situations):
    print("Concern: ", concerns[0].get_concern())
    print("Situation: ", situations[0].get_situation())
    print("Thought: ", situations[0].get_thought_tuples())
    print("Physical symptoms: ", situations[0].get_phy_sym_tuples())
    print("Safety behaviours: ", situations[0].get_safety_behaviours())
    print("Self focus: ", situations[0].get_self_focus())


import requests

from http import HTTPStatus
from pprint import pprint
from urllib.parse import urljoin

from django.conf import settings

if getattr(settings, 'DISABLE_URLLIB3_WARNINGS', False):
    import urllib3
    urllib3.disable_warnings()

class BearerAuth(requests.auth.AuthBase):

    def __init__(self, token):
        self.token = token

    def __call__(self, r):
        r.headers["authorization"] = "Bearer " + self.token
        return r

class HTTPAPIClient(object):
    base_url = None

    def __init__(self, base_url):
        self.base_url = base_url

    def build_url(self, path):
        # return urljoin(self.base_url, path)
        url = ''.join([self.base_url, path])  # quick fix for base urls with relative paths
        return url

    def get_auth(self):
        return None

    def get_default_headers(self):
        return None

    def post(self, path, data=None, json=None,  headers=None, **kwargs):
        r = requests.post(
            self.build_url(path),
            json=json,
            data=data,
            headers={
                **(self.get_default_headers() or {}),
                **(headers or {})
            } or None,
            auth=self.get_auth(),
            **kwargs
        )
        return r

    def get(self, path, data=None, headers=None, **kwargs):
        r = requests.get(
            self.build_url(path),
            data=data,
            headers={
                **(self.get_default_headers() or {}),
                **(headers or {})
            } or None,
            auth=self.get_auth(),
            **kwargs
        )
        return r


# -*- coding: UTF-8 -*-
import wx

from CommandInterface import CommandInterface

_metaclass_ = type
class ChoseFileCmd(CommandInterface,object):
    def __init__(self,view):
        super(ChoseFileCmd,self).__init__(view)

    def Excute(self):
        dlg = wx.FileDialog(None,
                            message="请选择一个文件",
                            wildcard="*" ,
                            style=wx.OPEN
                            )
        if dlg.ShowModal() == wx.ID_OK:
            self.view.filename = dlg.GetPath()
            from GlobalData import CommonData
            showmsg = "选择文件：" + self.view.filename.encode("utf8")
            self.sendViewMsg(CommonData.ViewPublisherc.MAINFRAME_APPENDTEXT,showmsg,True)
        dlg.Destroy()


# coding: UTF-8
import torch
import torch.nn as nn
from pytorch_pretrained_bert import BertModel, BertTokenizer
import json

class FGM():
    def __init__(self, model):
        self.model = model
        self.backup = {}

    def attack(self, epsilon=1., emb_name='bert.embedding'):
        # emb_name这个参数要换成你模型中embedding的参数名
        for name, param in self.model.named_parameters():
            if param.requires_grad and emb_name in name:
                self.backup[name] = param.data.clone()
                norm = torch.norm(param.grad)
                if norm != 0:
                    r_at = epsilon * param.grad / norm
                    param.data.add_(r_at)

    def restore(self, emb_name='bert.embedding'):
        # emb_name这个参数要换成你模型中embedding的参数名
        for name, param in self.model.named_parameters():
            if param.requires_grad and emb_name in name:
                assert name in self.backup
                param.data = self.backup[name]
        self.backup = {}

class bert_RNN(nn.Module):

    def __init__(self, config):
        super(bert_RNN, self).__init__()
        self.bert = BertModel.from_pretrained(config.bert_path)
        for param in self.parameters():
            param.requires_grad = True
        self.lstm = nn.LSTM(config.hidden_size, config.rnn_hidden, config.num_layers,
                            bidirectional=True, batch_first=True, dropout=config.dropout)
        self.dropout = nn.Dropout(config.dropout)
        self.fc_rnn = nn.Linear(config.rnn_hidden * 2, config.num_classes)  # 这里是rnn_hidden * 2是因为使用了BiLSTM

    def forward(self, x):
        context = x[0]  # 输入句子
        mask = x[2]  # 对padding部分进行mask，和句子一个size，padding部分用0表示，如：[1, 1, 1, 1, 0, 0]
        # 因为我选择了参数output_all_encoded_layers=True，12层Transformer的结果全返回了，存在第一个列表中，
        # 每个encoder_output的大小为[batch_size, sequence_length, hidden_size]
        context = torch.squeeze(context, dim=1)
        mask = torch.squeeze(mask, dim=1)
        # output_all_encoded_layers 取 bert 最后一层
        encoder_out, text_cls = self.bert(context, attention_mask=mask, output_all_encoded_layers=False)
        out, _ = self.lstm(encoder_out)
        out = self.dropout(out)
        logits = self.fc_rnn(out[:, -1, :])  # 句子最后时刻的 hidden state
        return logits


import requests
from requests.exceptions import RequestException
import re
import json
import time

def get_page(url):
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0'
        }
        rsp = requests.get(url=url, headers=headers)
        if rsp.status_code == requests.codes.ok:
            return rsp.text
    except RequestException:
        return None

def parse_one_page(html):
    pattern = re.compile(
        '
.*?board-index.*?>(.*?).*?data-src="(.*?)".*?name.*?a.*?>(.*?).*?star.*?>(.*?)
.'
        '*?releasetime.*?>(.*?)
.*?integer.*?>(.*?).*?fraction.*?>(.*?).*?
',
        re.S)
    items = re.findall(pattern, html)
    for item in items:
        yield {
            'index': item[0],
            'image': item[1],
            'title': item[2],
            'actor': item[3].strip()[3:],
            'time': item[4].strip()[5:],
            'score': item[5] + item[6],
        }

def main(offset=0):
    url = 'http://maoyan.com/board/4?offset=' + str(offset)
    html = get_page(url)
    for item in parse_one_page(html):
        print(item)

if __name__ == '__main__':
    for i in range(5):
        main(offset=i * 10)
        time.sleep(1)


# coding: utf-8
import codecs
import sys

from django.core.management.base import BaseCommand
from django.template.defaultfilters import timesince
from twiggy_goodies.django import LogMixin

from allmychanges.models import User

class Command(LogMixin, BaseCommand):
    help = u"""Drops user and all his packages."""

    def handle(self, *args, **options):
        writer = codecs.getwriter('utf-8')(sys.stdout)
        def prn(text):
            writer.write(text)
            writer.write('\n')

        for user in User.objects.order_by('-id'):
            prn(user.username)
            prn(u'\tJoined: {0} ago'.format(timesince(user.date_joined)))
            prn(u'\tEmail: {0}'.format(user.email))
            packages = list(user.changelogs.all())

            if packages:
                prn('\tPackages:')
                for changelog in user.changelogs.all():
                    prn(u'\t\t{0} {1} (https://allmychanges.com{2})'.format(changelog.namespace,
                                             changelog.name,
                                             changelog.get_absolute_url()))
            else:
                prn('\tPackages: NO')

            prn('')


"""
Created on Fri Nov  4 12:53:02 2022
"""

import pandas as pd
df = pd.read_csv("Sales.csv")
df.shape
df.head()

df.dtypes
# Label encode
from sklearn.preprocessing import LabelEncoder
LE = LabelEncoder()
df['ShelveLoc'] = LE.fit_transform(df['ShelveLoc'])
df['Urban'] = LE.fit_transform(df['Urban'])
df['US'] = LE.fit_transform(df['US'])

df.head()

X = df.iloc[:,1:11]
Y = df['high']

# data partition
from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size=0.30)

# model fitting #
#from sklearn.tree import DecisionTreeRegressor

from sklearn.tree import DecisionTreeClassifier
classifier = DecisionTreeClassifier(criterion='entropy', max_depth=5)
classifier.fit(X_train, Y_train)

print("Node counts:",classifier.tree_.node_count)
print("max depth:",classifier.tree_.max_depth)

Y_pred_train = classifier.predict(X_train)
Y_pred_test = classifier.predict(X_test)

from sklearn.metrics import accuracy_score
Training_acc = accuracy_score(Y_train,Y_pred_train).round(2)
Test_acc = accuracy_score(Y_test,Y_pred_test).round(2)

print("Training_accuracy: ",Training_acc.round(2))
print("Test_accuracy: ",Test_acc.round(2))

# 10  -->100,69
# 9   -->99,72
# 8   -->98,71
# 7   -->96,72
# 6   -->93,72
# 5   -->89,72
# 4   -->81,61


#!/usr/bin/env python3
"""
Primary module for generating release notes from Jira

@author Martin Proffitt <martin.proffitt@hpe.com>
@package pyccata.core

"""
from datetime import datetime
from pyccata.core.document import DocumentController

class ReleaseNote(object):
    """
    Contains the structure for generating release notes
    by collating the issue ID, release note text and business
    representative for all issues assigned to the current
    fix version which are in state done but not in state
    rejected.
    """

    CONFIGURATION = 'releasenote.json'
    _document_controller = None
    _document_ready = False
    _document_title = 'MSS Platform Release'

    def __init__(self):
        """ initialise the ReleaseNote object """
        self._document_controller = DocumentController(self.CONFIGURATION)

    def build_document(self):
        """ Creates the document structure """
        try:
            self._document_controller.build()
            self._document_ready = True
        except:
            self._document_ready = False
            raise

    def publish(self):
        """ publish the document """
        if not self._document_ready:
            self.build_document()

        date = datetime.strftime(datetime.now(), '%Y-%m-%d')
        self._document_controller.format_for_email()
        self._document_controller.save(self._document_title + ' ' + date + '.docx')

if __name__ == "__main__":
    release_note = ReleaseNote()
    release_note.build_document()
    release_note.publish()