def bubbleSort(array):
    n = len(array)
    for i in range(len(array)):
        swapped = False

        for j in range(0, n-i-1):
            if array[j] > array[j+1]:
                array[j], array[j+1] = array[j+1], array[j]
                swapped = True

        if swapped == False:
            break

    return array

print('This is from bubble sort')
arr = [64, 34, 25, 12, 22, 11, 90]

print(bubbleSort(arr))

def insertionSort(array):
    for i in range(1, len(array)):
        key = array[i]

        j = i - 1

        while j >= 0 and key < array[j]:
            array[j+1] = array[j]
            j -= 1
        array[j+1] = key
    return array

print('This is from insertion sort')
arr = [64, 34, 25, 12, 22, 11, 90]
print(insertionSort(arr))


#Done by Carlos Amaral in 12/06/2020

"""
If you could invite anyone, living or deceased, to dinner, who
would you invite? Make a list that includes at least three people you’d like to
invite to dinner. Then use your list to print a message to each person, inviting
them to dinner.
"""

guests = ['Rita', 'Sofia', 'Patrícia']
print(guests)

message = "Dear " + guests[0].title() + \
          " I would like to invite you to my dinner"
print(message)

message = "Dear " + guests[1].title() + \
          " I would like to invite you to my dinner"
print(message)

message = "Dear " + guests[2].title() + \
        " I would like to invite you to my dinner"
print (message)


import pytest
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QDialogButtonBox

from pyweight.wmprofile import Profile, ProfileWindow
from pyweight.wmutils import kg_to_lbs, m_to_in

@pytest.fixture
def pw(tmp_path):
    profile_path = str(tmp_path / "settings.ini")
    profile = Profile(profile_path)
    return ProfileWindow(profile, profile.save, None)

def test_init_profilewindow(qtbot, pw):
    for key, val in Profile.defaults.items():
        res = pw.config.__getattr__(key)
        assert res._raw == val
    assert not pw.kg_radio.isChecked()
    assert pw.lbs_radio.isChecked()
    assert pw.wcrate_spin_box.value() == -1.0
    assert pw.wcrate_spin_box.suffix() == " lbs/wk"
    assert pw.cycle_spinbox.value() == 14
    assert pw.show_adjust_cbox.isChecked()
    assert pw.age_spinbox.value() == 25
    assert pw.height_spinbox.value() == 65
    assert pw.height_spinbox.suffix() == " in"
    assert pw.bfp_info_button.isEnabled()

def test_apply_button(qtbot, pw):
    test_settings = {
        "wcrate": -0.226796185 / 7,
        "cycle": 20,
        "always_show_adj": False,
        "body_fat_method": "manual",
        "age": 30,
        "height": 1.524,
        "gender_selection": "female",
        "gender_prop": 0.7,
        "manual_body_fat": 0.3,
    }
    pw.show()
    qtbot.addWidget(pw)
    pw.wcrate_spin_box.clear()
    qtbot.keyClicks(pw.wcrate_spin_box, str(kg_to_lbs(test_settings["wcrate"]) * 7))
    pw.cycle_spinbox.clear()
    qtbot.keyClicks(pw.cycle_spinbox, str(test_settings["cycle"]))
    qtbot.mouseClick(pw.show_adjust_cbox, Qt.LeftButton)
    pw.age_spinbox.clear()
    qtbot.keyClicks(pw.age_spinbox, str(test_settings["age"]))
    pw.height_spinbox.clear()
    qtbot.keyClicks(pw.height_spinbox, str(m_to_in(test_settings["height"])))
    qtbot.mouseClick(pw.bfp_othergender_radio, Qt.LeftButton)
    # hacky, but qtbot doesn't seem to have a clean way to do this
    pw.sex_slider.setValue(70)
    pw.changed_sex_prop(70)
    qtbot.mouseClick(pw.bfp_female_radio, Qt.LeftButton)
    qtbot.mouseClick(pw.bfp_manual_radio, Qt.LeftButton)
    pw.manual_bfp_spinbox.clear()
    qtbot.keyClicks(pw.manual_bfp_spinbox, str(test_settings["manual_body_fat"] * 100))
    qtbot.mouseClick(pw.config_buttons.button(QDialogButtonBox.Apply), Qt.LeftButton)

    for key, val in test_settings.items():
        assert pw.config.__getattr__(key)._raw == val

def test_changed_gender(qtbot, pw):
    nonbinary_items = (
        pw.sex_label_1,
        pw.sex_label_2,
        pw.sex_slider,
        pw.usage_advice_label,
    )
    pw.show()
    qtbot.addWidget(pw)
    for item in nonbinary_items:
        assert not item.isVisible()
    qtbot.mouseClick(pw.bfp_male_radio, Qt.LeftButton)
    for item in nonbinary_items:
        assert not item.isVisible()
    qtbot.mouseClick(pw.bfp_othergender_radio, Qt.LeftButton)
    for item in nonbinary_items:
        assert item.isVisible()
    qtbot.mouseClick(pw.bfp_female_radio, Qt.LeftButton)
    for item in nonbinary_items:
        assert not item.isVisible()

def test_change_unit(qtbot, pw):
    pw.show()
    qtbot.addWidget(pw)
    qtbot.mouseClick(pw.kg_radio, Qt.LeftButton)
    assert pw.wcrate_spin_box.value() == -0.45
    assert pw.wcrate_spin_box.suffix() == " kg/wk"
    assert pw.height_spinbox.value() == 165.10
    assert pw.height_spinbox.suffix() == " cm"
    pw.save()
    assert pw.config.units == "metric"
    assert pw.config.wcrate == -0.4536 / 7
    assert pw.config.height == 1.651
    qtbot.mouseClick(pw.lbs_radio, Qt.LeftButton)
    assert pw.wcrate_spin_box.value() == -1.0
    assert pw.wcrate_spin_box.suffix() == " lbs/wk"
    assert pw.height_spinbox.value() == 165.10 / 2.54
    assert pw.height_spinbox.suffix() == " in"
    pw.save()
    assert pw.config.units == "imperial"
    assert pw.config.wcrate == -0.4536 / 7
    assert pw.config.height == 1.651

def test_change_bfp_mode(qtbot, pw):
    automatic_items = (
        pw.bfp_info_button,
        pw.age_label,
        pw.age_spinbox,
        pw.height_label,
        pw.height_spinbox,
        pw.gender_selection_gbox,
    )
    manual_items = (pw.manual_bfp_label, pw.manual_bfp_spinbox)
    pw.show()
    qtbot.addWidget(pw)
    qtbot.mouseClick(pw.bfp_manual_radio, Qt.LeftButton)
    for item in automatic_items:
        assert not item.isEnabled()
    for item in manual_items:
        assert item.isEnabled()
    qtbot.mouseClick(pw.bfp_automatic_radio, Qt.LeftButton)
    for item in automatic_items:
        assert item.isEnabled()
    for item in manual_items:
        assert not item.isEnabled()


#!usr/bin/env python

# Linear regression with l2-regularizer
#
# Author: Fangtong Liu
# Date: 06/13/2020
#

import numpy as np
import matplotlib.pyplot as plt

def basis(x1, x2):
    """compute basis (design) matrix"""
    s = 1.75  # bandwidth (length-scale)
    return np.exp(-np.linalg.norm(x1 - x2) ** 2 / (2 * s ** 2))

if __name__ == "__main__":
    # create a dataset
    z = np.arange(0, 8.1, 0.1)
    y = np.exp(-0.1 * z) * np.cos(z)

    # pick some training points rasndomly
    N = 15  # number of training points
    idx = np.unique(np.floor(np.random.rand(N, 1) * len(z)))
    # temp = np.array([0.1, 0.5, 0.3, 0.1, 0.8])
    # u, idx = np.unique(1 + np.floor(temp * len(z)), return_index=True)
    N = len(idx)
    idx = idx.astype(int)
    x = z[idx]  # training input
    t = y[idx] + 0.05 * np.random.randn(N, 1).reshape(-1)  # noisy target

    # here we solve for w in a linear model y = w^T * phi
    # compute basis (design) matrix
    Phi = np.zeros([len(x), len(x) + 1])
    Phi[:, 0] = 1  # bias basis
    for i in range(Phi.shape[0]):
        for j in range(Phi.shape[1]-1):
            Phi[i, j+1] = basis(x[i], x[j])

    # solve for w using least squares with l_2 regularizer
    w = np.dot(Phi.T, Phi) + 0.1 * np.eye(Phi.shape[1])
    w = np.dot(np.linalg.inv(w), np.dot(Phi.T, t))

    # predict at arbitrary inputs
    Phi_test = np.zeros([len(z), len(w)])
    Phi_test[:, 0] = 1  # bias basis
    for i in range(Phi_test.shape[0]):
        for j in range(Phi_test.shape[1]-1):
            Phi_test[i, j+1] = basis(z[i], x[j])

    y_test = np.dot(Phi_test, w)  # predict all at once

    ##################### plotting ##################
    # plot the true process and training points
    fig1 = plt.figure()
    plt.plot(z, y, linewidth=3, label='true process')
    plt.plot(x, t, '.', markersize=10, label='training points')
    plt.plot(z, y_test, '--', color='k', linewidth=3, label='prediction')
    plt.legend(loc='best')
    plt.xlabel(r'$x$')
    plt.ylabel(r'$y(x)$')
    plt.xlim([0, 8])
    plt.ylim([-1.25, 1.25])
    # fig1.savefig('lin_reg.png')
    plt.show()

    fig2 = plt.figure()
    plt.stem(w, use_line_collection=True)
    plt.xlabel('basis')
    plt.ylabel('w')
    # fig2.savefig('lin_reg_w.png')
    plt.show()


import random

def play():

    num = random.randint(1, 9)

    while True:
        guess = int(input("Enter the number:"))
        if num < guess:
            print("lesser")
        elif num > guess:
            print("Bigger")
        else:
            print("Bingo. Number is: ", num)
            break

if __name__ == "__main__":
    play()


import cv2 as cv
import numpy as np

def affine_transform(img: np.ndarray, T: np.ndarray):
    rows, cols, channels = img.shape
    res = np.zeros((rows, cols, channels), np.uint8)
    for i in range(rows):
        for j in range(cols):
            pos_vec = np.array([i, j, 1])
            pos_vec = np.dot(T, pos_vec)

            ni = int(pos_vec[0])
            nj = int(pos_vec[1])

            if ni < rows and nj < cols:
                res[i, j] = img[ni, nj]

    return res

def get_tranformation_matrix(params: list, op_str: str):
    ...

if __name__ == '__main__':
    img = cv.imread("../images/sample1.jpg")
    T = np.eye(3)
    T[0, 2] = 5
    T[1, 2] = 5

    print(T)
    res = affine_transform(img, T)

    cv.imshow("ORIG", img)
    cv.imshow("TRANS", res)

    cv.waitKey(0)
    cv.destroyAllWindows()


from __future__ import print_function
from stitcher import Stitcher
from imutils.video import VideoStream
from imutils.object_detection import non_max_suppression
import numpy as np
import datetime
from imutils import paths
import time
import cv2
import dlib

# initialize the video streams and allow them to warmup
print("[INFO] starting cameras...")

#Initializing Objects
leftStream = VideoStream(src=1).start()
rightStream = VideoStream(src=0).start()
time.sleep(2.0)
stitcher = Stitcher()
predictor = None
faceCascade = cv2.CascadeClassifier('face_cascade.xml')
# initialize the HOG descriptor/person detector
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

def cleanup():
    print("[INFO] cleaning up...")
    cv2.destroyAllWindows()
    leftStream.stop()
    rightStream.stop()

def detect_faces(image):
    gray =  cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags = cv2.CASCADE_SCALE_IMAGE)
    print("Found faces! ", str(len(faces)))

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

def detect_people(image):
    (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),padding=(8, 8), scale=1.05)
    # draw the original bounding boxes
    for (x, y, w, h) in rects:
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
    # apply non-maxima suppression to the bounding boxes using a
    # fairly large overlap threshold to try to maintain overlapping
    # boxes that are still people
    rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
    pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)
    # draw the final bounding boxes
    for (xA, yA, xB, yB) in pick:
        cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)

def rect2bb(rect):
    x = rect.left()
    y = rect.top()
    w = rect.right() - x
    h = rect.bottom() - y
    return x, y, w, h

while True:
    left = leftStream.read()
    right = rightStream.read()
    print(np.shape(left))
    result = stitcher.stitch([left,right])

    # no homograpy could be computed
    if result is None:
        print("[INFO] homography could not be computed")
        break

    # show the output images
    gray = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
    detect_people(gray)
    cv2.imshow("Result", result)

    cv2.imshow("Left Frame", left)
    cv2.imshow("Right Frame", right)
    key = cv2.waitKey(1) & 0xFF

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

#Cleaning Up
cleanup()


from django.contrib import admin

from apps.quests.models import Quest, QuestComment, QuestSubscription, MetroStation, QuestImage

class QuestAdmin(admin.ModelAdmin):
    """Class that represents admin part of the quests"""

    ordering = [
        'name',
        'location',
        'rating',
        'phone',
    ]
    list_display = [
        'name',
        'location',
        'rating',
        'x_coordinate',
        'y_coordinate',
        'phone',
    ]
    search_fields = [
        'name',
        'description',
        'location',
        'phone',
    ]
    list_per_page = 50

class QuestImageAdmin(admin.ModelAdmin):
    """Class that represents admin part of the quest image"""

    ordering = [
        'quest',
        'uploading_timespan',
    ]
    list_display = [
        'quest',
        'uploading_timespan',
    ]
    search_fields = [
        'quest',
        'uploading_timespan',
    ]
    list_per_page = 50

class QuestCommentAdmin(admin.ModelAdmin):
    """Class that represents admin part of the quests comment"""

    ordering = [
        'quest',
        'user',
        'scores',
    ]
    list_display = [
        'quest',
        'user',
        'scores',
    ]
    search_fields = [
        'quest',
        'user',
        'text',
        'scores',
    ]
    list_filter = [
        'timestamp',
    ]
    list_per_page = 50

class QuestSubscriptionAdmin(admin.ModelAdmin):
    """Class that represents admin part of the quests subscriptions"""

    list_display = [
        'user',
        'quest',
    ]
    ordering = [
        'user',
        'quest',
    ]
    search_fields = [
        'user',
        'quest',
    ]
    list_per_page = 50

class MetroStationAdmin(admin.ModelAdmin):
    """Class that represents admin part of the metro station"""

    list_display = [
        'name',
        'color',
        'quest',
    ]
    ordering = [
        'name',
        'color',
        'quest',
    ]
    search_fields = [
        'name',
        'color',
        'quest',
    ]
    list_per_page = 50

admin.site.register(Quest, QuestAdmin)
admin.site.register(QuestImage, QuestImageAdmin)
admin.site.register(QuestComment, QuestCommentAdmin)
admin.site.register(QuestSubscription, QuestSubscriptionAdmin)
admin.site.register(MetroStation, MetroStationAdmin)


"""废弃
#画布
canvas = tk.Canvas(mian_program, height=200, width=500)#创建画布
image_file = tk.PhotoImage(file='D:\代码\PyProject\数据储存点\logo.png')#加载图片文件
image = canvas.create_image(0,0, anchor='nw', image=image_file)#将图片置于画布上
canvas.grid(row=0,rowspan=1)
"""
 #进入到日记_第一个输入框.insert(0,"剑锋所指，意志所向！")
'''
def 自定义保存():
    var.set("")
'''
'''循环添加菜单(废弃)
for each in ['视图','编辑']:
    菜单.add_command(label=each)
'''
"""废弃
进入到日记_按钮=tk.Button(
    mian_program,
    text="别找了，这儿！\n点我进入",
    command=打开日记
    );
进入到日记_按钮.grid(row=1,column=0);
"""
"""已经弃用
自定保存日记_按钮=tk.Button(
    mian_program,
    text="自定义保存",
    command=自定义保存
)
"""

"""
    从头开始吧!
    准备设计一个GUI来介绍Python，就用最具代表性的名字“Holle World!”
"""
"""准备与预设"""
import time
import tkinter as tk
import tkinter.filedialog
import tkinter.messagebox

"""相关函数"""
def 打开日记():
    tkinter.messagebox.showinfo(title='看来你是真的要记', message='Let’s go!');
    mian_program.title("我现在是日记了┗|｀O′|┛ 嗷~~");
    a.grid_forget()
    写日记_输入框.grid(row=1,column=0,ipadx=True,sticky=tk.N+tk.W+tk.E)

    保存日记_按钮.grid(row=2,column=0,sticky=tk.N+tk.W)
    var.set('请写在下面')

def 保存(): #实现获取保存的路径;读取用户输入的文字;保存为txt文件;并提示用户
    with open("D:\代码\PyProject\数据储存点\保存路径.txt","r") as 工具人:
        获取路径=工具人.read()

    日记内容=写日记_输入框.get('0.0','end')
    时间=time.strftime('%Y-%m-%d',time.localtime(time.time()))
    if 获取路径 is not None:
        with open(file=获取路径+"/"+时间+"日记.txt", mode='a+', encoding='utf-8') as filea:
            filea.write(日记内容)
    var.set("保存成功！q(≧▽≦q)")
    time.sleep(2)
    var.set("当然还可以继续写")

def 个人简介():
    个人信息="姓名：李闻焌\n邮箱：1670350532deyerong@gmail.com"
    tkinter.messagebox.showinfo(message=个人信息)

def 选择保存路径():
    global 保存路径
    保存路径=tk.filedialog.askdirectory()
    with open(file="D:\代码\PyProject\数据储存点\保存路径.txt",mode='w+') as 路径文件:
        路径文件.write(str(保存路径))

def 按钮(文本,命令,x,y,ipadx=True,sticky=None):
    global a
    a=tk.Button(mian_program,text=文本,command=命令)
    a.grid(row=x,column=y,ipadx=ipadx,sticky=sticky)
    return a

"""主窗口"""
mian_program=tk.Tk();
mian_program.title("Holle World");
mian_program.geometry("200x200");
var=tk.StringVar()

#主菜单
菜单 = tk.Menu(mian_program)

菜单.add_command(label='关于',command=个人简介)

#分菜单
文件菜单下=tk.Menu(菜单)
文件菜单下.add_command(label="更改保存路径",command=选择保存路径)
菜单.add_cascade(label="文件",menu=文件菜单下)
#实现菜单
mian_program['menu']=菜单

""""进入日记的功能的实现"""
var.set("什么？你要写日记，好吧！")
切换到日记_文字显示=tk.Label(
    mian_program,
    textvariable=var,
    background='pink',
    font=(12),
    );
切换到日记_文字显示.grid(row=0,column=0);

进入到日记=按钮("别找了，这儿！\n点我进入",打开日记,1,0)

保存日记_按钮=tk.Button(
    mian_program,
    text="千万别忘记保存( •̀ ω •́ )y",
    command=保存
);

写日记_输入框=tk.Text(
    mian_program,
    show=None,

    height=6,
    width=20
    );  #第一窗口

mian_program.mainloop()
"""后记:
……做了有两三天了，可以说算是勉强完成，
实在是有些生疏，写了不少废代码（都放至到了最上面）
勉强还算可以
还需要练习
    """
print(保存路径)

PyProject/关于我又想练一练编程这件事.py

关于我又想练一练编程这件事.py


import numpy as np
import matplotlib.pyplot as plt
#3D axes plot
fig = plt.figure()
dim = plt.axes(projection ='3d')
#vertices
A=np.array([3,6,9])
A.resize(3,1)
B=np.array([10,20,30])
B.resize(3,1)
C=np.array([25,-41,5])
C.resize(3,1)
#checking the right angle
if ((A-C).T@(B-C)==0):
	print("ABC is a right triangle and right angle at c")
elif ((A-B).T@(C-B)==0):
	print("ABC is a right triangle and right angle at B")
elif ((B-A).T@(C-A)==0):
	print("ABC is a right triangle and right angle at A")
else:
	print("ABC is a not right triangle")
#plot of points
dim.scatter(A[0],A[1],A[2],marker="x")
dim.scatter(B[0],B[1],B[2],marker="x")
dim.scatter(C[0],C[1],C[2],marker="x")
dim.text(3,6,9, "A", color='black')
dim.text(10,20,30, "B", color='black')
dim.text(25,-41,5, "C", color='black')
#joing points A,B
x1 = np.linspace(3, 10, 10)
y1 = np.linspace(6, 20, 10)
z1 = np.linspace(9, 30, 10)
dim.plot3D(x1, y1, z1, 'blue',label="AB")
#joing points B,C
x2 = np.linspace(10, 25, 10)
y2 = np.linspace(20, -41, 10)
z2 = np.linspace(30, 5, 10)
dim.plot3D(x2, y2, z2, 'red',label="BC")
#joing points A,C
x3 = np.linspace(3, 25, 10)
y3 = np.linspace(6, -41, 10)
z3 = np.linspace(9, 5, 10)
dim.plot3D(x3, y3, z3, 'green',label="AC")
#plotting
dim.set_xlabel('$X$')
dim.set_ylabel('$Y$')
dim.set_zlabel('$Z$')
plt.title('Triangle')
plt.legend(bbox_to_anchor=(1, 1), loc='lower center')
plt.show()


from __future__ import print_function
from __future__ import division
from __future__ import absolute_import

import copy
import datetime

from oslo_context import context as common_context
from oslo_log import log as logging

from congress.common import policy

LOG = logging.getLogger(__name__)

class RequestContext(common_context.RequestContext):
    """Security context and request information.

    Represents the user taking a given action within the system.

    """
    FROM_DICT_EXTRA_KEYS = [
        'user_id', 'tenant_id', 'project_id', 'read_deleted', 'timestamp',
        'tenant_name', 'project_name', 'user_name',
        ]

    def __init__(self, user_id, tenant_id, is_admin=None, read_deleted="no",
                 roles=None, timestamp=None, load_admin_roles=True,
                 request_id=None, tenant_name=None, user_name=None,
                 overwrite=True, **kwargs):
        """Object initialization.

        :param: read_deleted: 'no' indicates deleted records are hidden, 'yes'
            indicates deleted records are visible, 'only' indicates that
            *only* deleted records are visible.

        :param: overwrite: Set to False to ensure that the greenthread local
            copy of the index is not overwritten.

        :param: kwargs: Extra arguments that might be present, but we ignore
            because they possibly came in from older rpc messages.
        """
        super(RequestContext, self).__init__(user=user_id, tenant=tenant_id,
                                             is_admin=is_admin,
                                             request_id=request_id,
                                             overwrite=overwrite,
                                             roles=roles)
        self.user_name = user_name
        self.tenant_name = tenant_name

        self.read_deleted = read_deleted
        if not timestamp:
            timestamp = datetime.datetime.utcnow()
        self.timestamp = timestamp
        self._session = None
        if self.is_admin is None:
            self.is_admin = policy.check_is_admin(self)

        # Log only once the context has been configured to prevent
        # format errors.
        if kwargs:
            LOG.debug(('Arguments dropped when creating '
                       'context: %s'), kwargs)

    @property
    def project_id(self):
        return self.tenant

    @property
    def tenant_id(self):
        return self.tenant

    @tenant_id.setter
    def tenant_id(self, tenant_id):
        self.tenant = tenant_id

    @property
    def user_id(self):
        return self.user

    @user_id.setter
    def user_id(self, user_id):
        self.user = user_id

    def _get_read_deleted(self):
        return self._read_deleted

    def _set_read_deleted(self, read_deleted):
        if read_deleted not in ('no', 'yes', 'only'):
            raise ValueError(_("read_deleted can only be one of 'no', "
                               "'yes' or 'only', not %r") % read_deleted)
        self._read_deleted = read_deleted

    def _del_read_deleted(self):
        del self._read_deleted

    read_deleted = property(_get_read_deleted, _set_read_deleted,
                            _del_read_deleted)

    def to_dict(self):
        ret = super(RequestContext, self).to_dict()
        ret.update({'user_id': self.user_id,
                    'tenant_id': self.tenant_id,
                    'project_id': self.project_id,
                    'read_deleted': self.read_deleted,
                    'timestamp': str(self.timestamp),
                    'tenant_name': self.tenant_name,
                    'project_name': self.tenant_name,
                    'user_name': self.user_name})
        return ret

    def elevated(self, read_deleted=None):
        """Return a version of this context with admin flag set."""
        context = copy.copy(self)
        context.is_admin = True

        if 'admin' not in [x.lower() for x in context.roles]:
            context.roles.append('admin')

        if read_deleted is not None:
            context.read_deleted = read_deleted

        return context

def get_admin_context(read_deleted="no", load_admin_roles=True):
    return RequestContext(user_id=None,
                          tenant_id=None,
                          is_admin=True,
                          read_deleted=read_deleted,
                          load_admin_roles=load_admin_roles,
                          overwrite=False)


class CaesarsCipher:

    @staticmethod
    def cipher(key, message, mode):
        message = message.lower()
        SYMBOLS = 'абвгдежзийклмнопрстуфхцчшщыьэюя '
        translated = ''
        for symbol in message:
            translatedIndex = None
            if symbol in SYMBOLS:
                symbolIndex = SYMBOLS.find(symbol)
                if mode == 'encrypt':
                    translatedIndex = symbolIndex + key
                elif mode == 'decrypt':
                    translatedIndex = symbolIndex - key
                if translatedIndex >= len(SYMBOLS):
                    translatedIndex = translatedIndex - len(SYMBOLS)
                elif translatedIndex < 0:
                    translatedIndex = translatedIndex + len(SYMBOLS)
                translated = translated + SYMBOLS[translatedIndex]
            else:
                translated = translated + symbol

        return translated


import os
import pickle
from operator import itemgetter
from collections import OrderedDict

directory_path = {
    'mp3': "/Users/barber/Data/Research/File-type-identification/scraped-data/train_data/mp3",
    'hwp': "/Users/barber/Data/Research/File-type-identification/scraped-data/train_data/hwp",
    'pdf': "/Users/barber/Data/Research/File-type-identification/scraped-data/train_data/pdf",
    'jpg': "/Users/barber/Data/Research/File-type-identification/scraped-data/train_data/jpg",
    'png': "/Users/barber/Data/Research/File-type-identification/scraped-data/train_data/png"
}

file_size_in_bytes = 4096
max_process_size_in_bytes = 10 * 1024 * 1024  # 100 MB

def compute_gram_frequency(gram, limit):
    print("Processing {}-gram...".format(gram))
    separator_frequency = dict()
    for file_type, directory in directory_path.items():

        if not os.path.exists(directory):
            exit(-1)

        if not directory.endswith("/"):
            directory += "/"

        fragments_processed = 0
        fragments_to_process = int(max_process_size_in_bytes / file_size_in_bytes)

        next_type_flag = False

        for file_name in os.listdir(directory):
            if next_type_flag:
                break

            if file_name.startswith("."):
                continue

            file_path = directory + file_name

            with open(file_path, "rb") as file:
                fragment = file.read(file_size_in_bytes)
                while len(fragment) == file_size_in_bytes:

                    for index in range(0, file_size_in_bytes - gram + 1):
                        separator = int.from_bytes(fragment[index:(index + gram)], byteorder='big')
                        if separator in separator_frequency:
                            separator_frequency[separator] += 1
                        else:
                            separator_frequency[separator] = 1

                    fragments_processed += 1
                    if fragments_processed >= fragments_to_process:
                        print("\t- Processed {}.".format(file_type))

                        next_type_flag = True
                        break

                    if next_type_flag:
                        break

                    fragment = file.read(file_size_in_bytes)

    ordered_frequency = OrderedDict(
        sorted(separator_frequency.items(), key=itemgetter(1), reverse=True))
    separators = list(sorted(list(ordered_frequency.keys())[:limit]))
    result_dict = dict()
    index = 0
    for separator in separators:
        result_dict[separator] = index
        index += 1
    return result_dict

def main():
    result_separators = list()
    result_separators.append(compute_gram_frequency(gram=2, limit=2048))
    result_separators.append(compute_gram_frequency(gram=3, limit=1024))
    result_separators.append(compute_gram_frequency(gram=4, limit=512))
    result_separators.append(compute_gram_frequency(gram=5, limit=256))
    with open("./separators.pickle", "wb") as object_file:
        pickle.dump(result_separators, object_file)
    print("Done. Result: ")
    print(result_separators)

if __name__ == "__main__":
    main()


# Problem 25 ||||||||    Simple string expansion
'''

Given a string, return the expansion of that string.

Input will consist of only lowercase letters and numbers (1 to 9) in valid parenthesis. There will be no letters or numbers after the last closing parenthesis.

Example:
solve("3(ab)") = "ababab"     -- because "ab" repeats 3 times
solve("2(a3(b))") = "abbbabbb" -- because "a3(b)" == "abbb", which repeats twice.

'''
def solve(st):
    stack = [i for i in st]
    ans = ""
    for i in stack[::-1]:
        if i != "(" and i != ")":
            if i in "123456789":
                ans = ans*int(i)
            else:
                ans = ans + i

    return ans[::-1]

print(solve("3(ab)"))

# Done


import intcode

def get_input_str():
    return open("day07.in").readlines()[0]

def get_inputs(input_str):
    return [int(y) for y in input_str.split(',')]

def part1():
    # inputs = get_inputs("3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0")
    max_power = 0
    for a in range(0, 5):
        for b in range(0, 5):
            if b == a: continue
            for c in range(0, 5):
                if c == b or c == a: continue
                for d in range(0, 5):
                    if d == c or d == b or d == a: continue
                    for e in range(0, 5):
                        if e == d or e == c or e == b or e == a: continue

                        inputs = get_inputs(get_input_str())

                        ampA = intcode.Intcode()
                        ampB = intcode.Intcode()
                        ampC = intcode.Intcode()
                        ampD = intcode.Intcode()
                        ampE = intcode.Intcode()

                        ampA.receive_signal(a)
                        ampA.receive_signal(0)
                        out = ampA.run(inputs, False, True)
                        ampB.receive_signal(b)
                        ampB.receive_signal(out)
                        out = ampB.run(inputs, False, True)
                        ampC.receive_signal(c)
                        ampC.receive_signal(out)
                        out = ampC.run(inputs, False, True)
                        ampD.receive_signal(d)
                        ampD.receive_signal(out)
                        out = ampD.run(inputs, False, True)
                        ampE.receive_signal(e)
                        ampE.receive_signal(out)
                        out = ampE.run(inputs, False, True)
                        if out > max_power:
                            max_power = out

    return max_power

def part2():
    max_power = 0
    for a in range(5, 10):
        for b in range(5, 10):
            if b == a: continue
            for c in range(5, 10):
                if c == b or c == a: continue
                for d in range(5, 10):
                    if d == c or d == b or d == a: continue
                    for e in range(5, 10):
                        if e == d or e == c or e == b or e == a: continue

                        inputs = get_inputs(get_input_str())

                        ampA = intcode.Intcode()
                        ampB = intcode.Intcode()
                        ampC = intcode.Intcode()
                        ampD = intcode.Intcode()
                        ampE = intcode.Intcode()

                        ampA.receive_signal(a)
                        ampA.receive_signal(0)
                        ampB.receive_signal(b)
                        ampC.receive_signal(c)
                        ampD.receive_signal(d)
                        ampE.receive_signal(e)

                        some_running = True
                        final_out = 0
                        while some_running:
                            out = ampA.run(inputs, False, True)
                            ampB.receive_signal(out)
                            out = ampB.run(inputs, False, True)
                            ampC.receive_signal(out)
                            out = ampC.run(inputs, False, True)
                            ampD.receive_signal(out)
                            out = ampD.run(inputs, False, True)
                            ampE.receive_signal(out)
                            out = ampE.run(inputs, False, True)

                            if not ampA.complete or not ampB.complete or not ampC.complete or not ampD.complete or not ampE.complete:
                                ampA.receive_signal(out)
                            else:
                                final_out = out
                                some_running = False

                        if final_out > max_power:
                            max_power = final_out

    return max_power


class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        i = 0
        size = len(nums)
        sum_ = (size*(size+1))//2
        sumOrginal = 0
        for val in nums:
            sumOrginal += val

        return sum_ - sumOrginal


import json
from collections import defaultdict
from dataclasses import dataclass
from datetime import date, datetime, timedelta, timezone
from pprint import pprint
from typing import (
    TYPE_CHECKING,
    Any,
    Dict,
    Iterable,
    Iterator,
    List,
    Mapping,
    Optional,
    Sequence,
    Tuple,
    Union,
)
from uuid import uuid4 as uuid

import requests
import yaml
from dagster import (
    AssetCheckResult,
    AssetExecutionContext,
    AssetKey,
    AssetObservation,
    AssetsDefinition,
    AssetSelection,
    ConfigurableResource,
    HourlyPartitionsDefinition,
    Output,
    ScheduleDefinition,
    asset,
    build_schedule_from_partitioned_job,
    define_asset_job,
)
from pydantic import Field

if TYPE_CHECKING:
    from dagster_dbt import DbtCliInvocation
    from dagster_snowflake import SnowflakeConnection

try:
    pass
except ImportError:
    pass

@dataclass(frozen=True, eq=True)
class AssetMaterializationId:
    run_id: str
    asset_key: AssetKey
    partition: Optional[str]
    step_key: str

OPAQUE_ID_METADATA_KEY_PREFIX = "dagster_snowflake_opaque_id:"
OPAQUE_ID_SQL_SIGIL = "snowflake_dagster_dbt_v1_opaque_id"

def add_asset_context_to_sql_query(
    context: AssetExecutionContext,
    sql: str,
    comment_factory=lambda comment: f"\n-- {comment}\n",
    opaque_id=None,
):
    if opaque_id is None:
        opaque_id = str(uuid())
    context.add_output_metadata({f"{OPAQUE_ID_METADATA_KEY_PREFIX}{opaque_id}": True})
    return sql + comment_factory(f"{OPAQUE_ID_SQL_SIGIL}[[[{opaque_id}]]]")

def dbt_with_snowflake_insights(
    context: AssetExecutionContext,
    dbt_cli_invocation: "DbtCliInvocation",
    dagster_events: Optional[Iterable[Union[Output, AssetObservation, AssetCheckResult]]] = None,
    skip_config_check=False,
) -> Iterator[Union[Output, AssetObservation, AssetCheckResult]]:
    if not skip_config_check:
        is_snowflake = dbt_cli_invocation.manifest["metadata"]["adapter_type"] == "snowflake"
        dbt_project_config = yaml.safe_load(
            (dbt_cli_invocation.project_dir / "dbt_project.yml").open("r")
        )
        # sanity check that the sigil is present somewhere in the query comment
        query_comment = dbt_project_config.get("query-comment")
        if query_comment is None:
            raise RuntimeError("query-comment is required in dbt_project.yml but it was missing")
        comment = query_comment.get("comment")
        if comment is None:
            raise RuntimeError(
                "query-comment.comment is required in dbt_project.yml but it was missing"
            )
        if OPAQUE_ID_SQL_SIGIL not in comment:
            raise RuntimeError(
                "query-comment.comment in dbt_project.yml must contain the string"
                f" '{OPAQUE_ID_SQL_SIGIL}'. Read the Dagster Insights docs for more info."
            )
        if is_snowflake:
            # snowflake throws away prepended comments
            if not query_comment.get("append"):
                raise RuntimeError(
                    "query-comment.append must be true in dbt_project.yml when using the Snowflake"
                    " adapter"
                )

    if dagster_events is None:
        dagster_events = dbt_cli_invocation.stream()

    asset_and_partition_key_to_unique_id = {}
    for dagster_event in dagster_events:
        if isinstance(dagster_event, Output):
            unique_id = dagster_event.metadata["unique_id"].value
            asset_key = context.asset_key_for_output(dagster_event.output_name)
            if context._step_execution_context.has_asset_partitions_for_output(  # noqa: SLF001
                dagster_event.output_name
            ):
                partition_key = context.asset_partition_key_for_output(dagster_event.output_name)
            else:
                partition_key = None
            asset_and_partition_key_to_unique_id[(asset_key, partition_key)] = unique_id
        elif isinstance(dagster_event, AssetObservation):
            unique_id = dagster_event.metadata["unique_id"].value
            asset_key = dagster_event.asset_key
            partition_key = dagster_event.partition
            asset_and_partition_key_to_unique_id[(asset_key, partition_key)] = unique_id

        yield dagster_event

    run_results_json = dbt_cli_invocation.get_artifact("run_results.json")
    invocation_id = run_results_json["metadata"]["invocation_id"]

    for (asset_key, partition), unique_id in asset_and_partition_key_to_unique_id.items():
        # must match the query-comment in dbt_project.yml
        opaque_id = f"{unique_id}:{invocation_id}"
        context.log_event(
            AssetObservation(
                asset_key=asset_key,
                partition=partition,
                metadata={f"{OPAQUE_ID_METADATA_KEY_PREFIX}{opaque_id}": True},
            )
        )

class GraphqlClientResource(ConfigurableResource):
    url: str = Field(
        description=(
            "The URL of the Dagster Cloud GraphQL endpoint for your organization. For example,"
            " https://hooli.dagster.cloud/prod/graphql"
        )
    )
    cloud_token: Optional[str] = Field(
        description="A Dagster Cloud agent token to submit metrics to the Dagster Cloud API."
    )

    def execute(self, query_text: str, variables=None) -> Dict[str, Any]:
        headers = {}
        if self.cloud_token is not None:
            headers["Dagster-Cloud-API-Token"] = self.cloud_token
        return requests.post(
            self.url,
            json={"operationName": None, "variables": variables or dict(), "query": query_text},
            headers=headers,
        ).json()

def get_opaque_ids_to_assets(
    graphql_client: GraphqlClientResource, min_datetime: datetime, max_datetime: datetime
) -> Iterable[Tuple[AssetMaterializationId, str]]:
    query = """
{
	assetsOrError {
    ... on AssetConnection {
      nodes {
        key {
            path
        }
        assetObservations(afterTimestampMillis:"%d", beforeTimestampMillis:"%d") {
          runId
          partition
          stepKey
          metadataEntries {
            ... on BoolMetadataEntry {
              label
            }
          }
        }
      }
    }
  }
}
    """ % (
        min_datetime.timestamp() * 1000,
        max_datetime.timestamp() * 1000,
    )

    response = graphql_client.execute(
        #        "http://localhost:3000/graphql",
        query,
        variables={},
    )

    for node in response["data"]["assetsOrError"]["nodes"]:
        asset_key = AssetKey.from_graphql_input(node["key"])
        if asset_key is None:
            # should never happen, but we must appease the type checker
            raise RuntimeError("asset_key is None, which should never happen")
        for observation in node["assetObservations"]:
            run_id = observation["runId"]
            partition = observation["partition"]
            step_key = observation["stepKey"]
            opaque_id = None
            for metadata in observation["metadataEntries"]:
                if metadata.get("label", "").startswith(OPAQUE_ID_METADATA_KEY_PREFIX):
                    opaque_id = metadata["label"][len(OPAQUE_ID_METADATA_KEY_PREFIX) :]
                    break
            if opaque_id is not None:
                yield (
                    AssetMaterializationId(
                        run_id=run_id, asset_key=asset_key, partition=partition, step_key=step_key
                    ),
                    opaque_id,
                )

SNOWFLAKE_QUERY_HISTORY_LATENCY_SLA = timedelta(minutes=45)
QUERY_HISTORY_TIME_PADDING = timedelta(hours=1)  # deal with desynchronized clocks

@dataclass
class SnowflakeInsightsDefinitions:
    assets: Sequence[AssetsDefinition]
    schedule: ScheduleDefinition

def get_cost_data_for_hour(
    context: AssetExecutionContext,
    snowflake: "SnowflakeConnection",
    graphql_client: GraphqlClientResource,
    start_hour: datetime,
    end_hour: datetime,
) -> List[Tuple[AssetMaterializationId, Any]]:
    queries = list(get_opaque_ids_to_assets(graphql_client, start_hour, end_hour))
    print(
        f"Found {len(queries)} queries to check from {start_hour.isoformat()} to"
        f" {end_hour.isoformat()}"
    )
    for am, opaque_id in queries:
        print(opaque_id, am.run_id)
    if len(queries) == 0:
        return []

    opaque_id_to_asset_materialization_id: Dict[str, AssetMaterializationId] = {}
    for asset_materialization_id, opaque_id in queries:
        opaque_id_to_asset_materialization_id[opaque_id] = asset_materialization_id

    opaque_ids_sql = rf"""
    regexp_substr_all(query_text, '{OPAQUE_ID_SQL_SIGIL}\\[\\[\\[(.*?)\\]\\]\\]', 1, 1, 'ce', 1)
    """.strip()

    sql = f"""
WITH
warehouse_sizes AS (
    SELECT 'X-Small' AS warehouse_size, 1 AS credits_per_hour UNION ALL
    SELECT 'Small' AS warehouse_size, 2 AS credits_per_hour UNION ALL
    SELECT 'Medium'  AS warehouse_size, 4 AS credits_per_hour UNION ALL
    SELECT 'Large' AS warehouse_size, 8 AS credits_per_hour UNION ALL
    SELECT 'X-Large' AS warehouse_size, 16 AS credits_per_hour UNION ALL
    SELECT '2X-Large' AS warehouse_size, 32 AS credits_per_hour UNION ALL
    SELECT '3X-Large' AS warehouse_size, 64 AS credits_per_hour UNION ALL
    SELECT '4X-Large' AS warehouse_size, 128 AS credits_per_hour UNION ALL
    SELECT 'missing' as warehouse_size, 0 as credits_per_hour
)
SELECT
    {opaque_ids_sql} as opaque_ids,
    qh.execution_time/(1000*60*60)*wh.credits_per_hour AS query_cost,
    query_id
FROM snowflake.account_usage.query_history AS qh
INNER JOIN warehouse_sizes AS wh
    ON ifnull(qh.warehouse_size, 'missing')=wh.warehouse_size
WHERE
    start_time >= '{start_hour - QUERY_HISTORY_TIME_PADDING}'
    AND start_time <= '{end_hour + QUERY_HISTORY_TIME_PADDING}'
HAVING ARRAY_SIZE(opaque_ids) > 0
        """

    with snowflake.get_connection() as conn:
        with conn.cursor() as cursor:
            result = cursor.execute(sql)
            assert result
            results = result.fetchall()

    costs: List[Tuple[AssetMaterializationId, Any]] = []

    print(
        f"{len(results) if results else 0} annotated queries returned from snowflake query_history"
    )

    if not results:
        return []

    total = 0
    for result_opaque_ids, result_cost, query_id in results:
        opaque_ids = json.loads(result_opaque_ids)
        # TODO: is this cost splitting logic correct?
        cost = result_cost / len(opaque_ids)
        total += len(opaque_ids)
        for opaque_id in opaque_ids:
            print(opaque_id, query_id)
            if opaque_id in opaque_id_to_asset_materialization_id:
                costs.append((opaque_id_to_asset_materialization_id[opaque_id], cost))

    print(
        f"Reported costs for {len(costs)} of {total} asset materializations found in the"
        " query_history."
    )
    return costs

def create_snowflake_insights_asset_and_schedule(
    start_date: date,
    code_location_name: str,
    repository_name: str = "__repository__",
    name: Optional[str] = None,
    group_name: Optional[str] = None,
    job_name: str = "snowflake_insights_import",
    dry_run=True,
    allow_partial_partitions=False,
    snowflake_resource_key: str = "snowflake",
    graphql_client_resource_key: str = "graphql_client",
) -> SnowflakeInsightsDefinitions:
    @asset(
        name=name,
        group_name=group_name,
        partitions_def=HourlyPartitionsDefinition(start_date=start_date.strftime("%Y-%m-%d-%H:%M")),
        required_resource_keys={snowflake_resource_key, graphql_client_resource_key},
    )
    def poll_snowflake_query_history_hour(
        context: AssetExecutionContext,
    ) -> None:
        snowflake: "SnowflakeConnection" = getattr(context.resources, snowflake_resource_key)
        graphql_client: GraphqlClientResource = getattr(
            context.resources, graphql_client_resource_key
        )

        start_hour = context.partition_time_window.start
        end_hour = context.partition_time_window.end

        if not allow_partial_partitions:
            now = datetime.now().astimezone(timezone.utc)
            earliest_call_time = end_hour + SNOWFLAKE_QUERY_HISTORY_LATENCY_SLA
            if now < earliest_call_time:
                raise RuntimeError(
                    "This function was called before the Snowflake query_history table may be"
                    f" available. For hour starting {start_hour.isoformat()} you can call it"
                    f" starting at {earliest_call_time.isoformat()} (it is currently"
                    f" {now.isoformat()})"
                )

        costs = (
            get_cost_data_for_hour(context, snowflake, graphql_client, start_hour, end_hour) or []
        )
        if dry_run:
            pprint(costs)
        else:
            metrics_to_submit = []
            mat_by_step: Mapping[Tuple[str, str], List[Tuple[AssetMaterializationId, Any]]] = (
                defaultdict(list)
            )
            for mat, cost in costs:
                mat_by_step[(mat.run_id, mat.step_key)].append((mat, cost))

            for run_id, step_key in mat_by_step.keys():
                materializations = mat_by_step[(run_id, step_key)]
                for i in range(0, len(materializations), 5):
                    metrics_to_submit.append(
                        {
                            "repositoryName": repository_name,
                            "codeLocationName": code_location_name,
                            "stepKey": step_key,
                            "runId": run_id,
                            "assetMetricDefinitions": [
                                {
                                    "assetKey": mat.asset_key.to_string(),
                                    "assetGroup": "default",
                                    "metricValues": [
                                        {
                                            "metricName": "snowflake_credits",
                                            "metricValue": float(cost),
                                        }
                                    ],
                                }
                                for mat, cost in materializations[i : i + 5]
                            ],
                        }
                    )
                metrics_to_submit.append(
                    {
                        "repositoryName": repository_name,
                        "codeLocationName": code_location_name,
                        "stepKey": step_key,
                        "runId": run_id,
                        "jobMetricDefinitions": [
                            {
                                "metricValues": [
                                    {
                                        "metricName": "snowflake_credits",
                                        "metricValue": sum(
                                            [float(cost) for mat, cost in materializations]
                                        ),
                                    }
                                ],
                            }
                        ],
                    }
                )

            mutation = """
                mutation AddMetrics($metrics: [MetricInputs]) {
                    createOrUpdateMetrics(
                        metrics: $metrics
                    ) {
                        __typename
                        ... on CreateOrUpdateMetricsFailed {
                            message
                        }
                    }
                }
            """

            result = graphql_client.execute(
                query_text=mutation, variables={"metrics": metrics_to_submit}
            )

            assert (
                result["data"]["createOrUpdateMetrics"]["__typename"]
                == "CreateOrUpdateMetricsSuccess"
            ), result

    schedule = build_schedule_from_partitioned_job(
        job=define_asset_job(job_name, AssetSelection.assets(poll_snowflake_query_history_hour)),
        minute_of_hour=59,
    )
    # schedule may be a UnresolvedPartitionedAssetScheduleDefinition so we ignore the type check
    return SnowflakeInsightsDefinitions(assets=[poll_snowflake_query_history_hour], schedule=schedule)  # type: ignore


import PyPDF2
import re
import csv
import os

# Diretório onde os arquivos PDF estão localizados
diretorio = 'E:/Geral/Temp/Thay/newpdfs/'

# Lista para armazenar os dados extraídos de todos os arquivos PDF
dados_totais = []
output_file = open("errados.txt", "a")

# Loop pelos arquivos PDF no diretório
for arquivo_pdf in os.listdir(diretorio):
    if arquivo_pdf.endswith(".pdf"): # == 'SP2012228_AROUCH_Invest.pdf': #.endswith(".pdf"):
        pdf_path = os.path.join(diretorio, arquivo_pdf)

        name_archive = arquivo_pdf

        # Abre o arquivo PDF em modo de leitura binária
        try:
            pdf_file = open(pdf_path, 'rb')
            print(f"Lendo: {arquivo_pdf}")
            pdf_reader = PyPDF2.PdfReader(pdf_file)

            # Inicializa uma string vazia para armazenar o conteúdo do PDF
            pdf_text = ''

            # Loop através das páginas do PDF
            for page_num in range(len(pdf_reader.pages)):
                page = pdf_reader.pages[page_num]
                pdf_text += page.extract_text()
                if page_num == 2:
                    break

            # Fecha o arquivo PDF
            pdf_file.close()
        except:
            print(f"Arquivo corrompido ->  {arquivo_pdf} ")

        #print(pdf_text)

        # Extrair os dados desejados
        numero_processo_match = re.search(r"(\d+\.\d+/\d+-\d+|nº\s*\d+/\d+|[a-z][a-z]\s*\d+/\d+)", pdf_text , flags=re.MULTILINE|re.DOTALL|re.IGNORECASE )

        numero_processo = numero_processo_match.group(1) if numero_processo_match else "null"

        data_julgamento_match = re.search(r"Data do julgamento:\s*(\d{2}/\d{2}/\d{4})", pdf_text)
        data_julgamento = data_julgamento_match.group(1) if data_julgamento_match else "null"

        relatora_match = re.search(r"Relatora:\s*(.+)", pdf_text)
        relatora = relatora_match.group(1) if relatora_match else "null"

        acusado_match = re.search(r"ACUSADO*A*S*\s*:*\s*(.*)EMENTA", pdf_text,  flags=re.MULTILINE|re.DOTALL|re.IGNORECASE )
        acusado = acusado_match.group(1) if acusado_match else "null"

        ementa_match = re.search(r"EMENTAS*\s*:\s*(.*)DECIS.O", pdf_text,  flags=re.MULTILINE|re.DOTALL|re.IGNORECASE )
        ementa = ementa_match.group(1).strip() if ementa_match else "null"

        decisao_match = re.search(r"Decisão:(.*?)(Documento assinado eletronicamente|\Z)", pdf_text, re.DOTALL)
        decisao_completa = decisao_match.group(1).strip() if decisao_match else "null"

        # print(f"Numero_processo : {str(numero_processo)} ")
        # print(f"data_julgamento : {str(data_julgamento)} ")
        # print(f"relatora : {str(relatora)} ")
        #print(f"acusado : {str(acusado)}  ")
        nomes = re.split(r'\n', str(acusado))
        nomes = [nome.strip() for nome in nomes if nome.strip()]  # Remove linhas em branco
        # print(f"ementa : {str(ementa)}")
        # print(f"decisao_completa : {str(decisao_completa)}")

        if acusado != "null":
            if(len(nomes) <= 5 and nomes[0] != 'null'):
                # Adiciona os dados extraídos à lista de dados totais
                dados_totais.append([name_archive,numero_processo, data_julgamento, relatora, acusado, ementa, decisao_completa])

                if "null" in dados_totais[-1]:
                    with open('output.txt', 'a', encoding='utf-8', errors='ignore') as output_file:
                        output_file.write(str(arquivo_pdf) + '\n')
                        if str(numero_processo) == 'null':
                            output_file.write(f"Numero_processo : {str(numero_processo)} \n")
                        if str(data_julgamento) == 'null':
                            output_file.write(f"data_julgamento : {str(data_julgamento)} \n")
                        if str(relatora) == 'null':
                            output_file.write(f"relatora : {str(relatora)} \n")
                        if str(acusado) == 'null':
                            output_file.write(f"acusado : {str(acusado)} \n ")
                        if str(ementa) == 'null':
                            output_file.write(f"ementa : {str(ementa)} \n")
                        output_file.write('\n\n\n-------\n\n\n')

# Nomes das colunas
colunas = ["nome_arquivo","numero_processo", "Data do Julgamento", "Relatora", "Acusado", "Ementa", "Decisão"]

#Escreve os dados no arquivo CSV
with open('resultado.csv', 'w', newline='', encoding='utf-8') as csv_file:
    csv_writer = csv.writer(csv_file)
    csv_writer.writerow(colunas)  # Escreve os nomes das colunas
    csv_writer.writerows(dados_totais)  # Escreve os dados

print("Dados salvos com sucesso no arquivo resultado.csv")


"""
Autor: GAÑAN, Tomas // CERIONI, Enrique
Ejercicio 8a: Cartas
"""

# Importacion de librerias/modulos

import numpy as np
import random

# Funcion adaptación

def adaptacion(sum_valor,sum_peso,table):

    peso_mochila = (sum_peso/8) + sum_valor
    return peso_mochila

# Funcion Ruleta

def ruleta(poblacion, probabilidad, num):

    seleccionados = []

    while(len(seleccionados) < num):
        r = random.random()*0.1

        for (i,individuo) in enumerate(poblacion):
            if (r <= probabilidad[i]):
                seleccionados.append(list(individuo))
                break

    return seleccionados

def cruzaUniforme(padre1,padre2):

    hijo1 = []
    hijo2 = []

    ran = random.randint(0,1)

    for i in range(2):
        if(ran == 1):
            hijo1.append(padre1[i])
            hijo2.append(padre2[i])
        else:
            hijo1.append(padre2[i])
            hijo2.append(padre1[i])

    for i in range(5):
        newPoblacion.append(hijo1)
        newPoblacion.append(hijo2)

    return newPoblacion

def mutacionPermu(pob):

    ra1 = random.randint(0,1)
    ra2 = random.randint(0,1)

    while(ra1 == ra2):
        ra2 = random.randint(0,1)

    for i in range(50):

        a = pob[i][ra2]
        b = pob[i][ra1]

        pob[i][ra1] = a
        pob[i][ra2] = b

        mutacionPob.append(pob[i])

    return mutacionPob

# Desarrollo

table = [
        [1,15,1],
        [2,10,5],
        [3,9,3],
        [4,5,4],
        [5,2,1],
        [6,4,4],
        [7,5,5],
        ]

i = 0
nPoblacion = 100
total_ganancias = 0

individuo = []
poblacion = []
ganancias = []
probabilidad = []

for i in range(nPoblacion):

    sum_peso = 10

    while(sum_peso > 8):

        i = 0
        obj_sel = []
        individuo = []
        objetos = [1,2,3,4,5,6,7]

        while(i < 3):

            r = random.randint(0,6-i)
            obj_sel.append(objetos[r])
            objetos.remove(objetos[r])
            i += 1

        a = obj_sel[0]-1 # objeto 1
        b = obj_sel[1]-1 # objeto 2
        c = obj_sel[2]-1 # objeto 3

        individuo.append(b)
        individuo.append(c)

        # print("ingresaron los objetos a:",table[a][0],"b:",table[b][0],"y c:",table[c][0])

        sum_valor = table[a][1] + table[b][1] + table[c][1]
        sum_peso = table[a][2] + table[b][2] + table[c][2]

    gan_mochila = adaptacion(sum_valor,sum_peso,table)
    ganancias.append(gan_mochila)
    total_ganancias = total_ganancias + gan_mochila

    poblacion.append(individuo)

for i in range(nPoblacion):

    prob = ganancias[i]/total_ganancias
    probabilidad.append(prob)

num = 20
selec = ruleta(poblacion,probabilidad,num)

# Cruza

newPoblacion = []

for i in range(10):

    randomPadre1 = random.randint(0,len(selec)-1)
    randomPadre2 = random.randint(0,len(selec)-1)

    while(randomPadre1 == randomPadre2):
        randomPadre2 = random.randint(0,len(selec)-1)

    cruzaUniforme(selec[randomPadre1],selec[randomPadre2]) # Ejecuto la cruza

    if(randomPadre1 >= randomPadre2):
        selec.remove(selec[randomPadre1])
        selec.remove(selec[randomPadre2])
    else:
        selec.remove(selec[randomPadre2])
        selec.remove(selec[randomPadre1])

# Mutacion

mutacionPob = []
mutacionPermu(newPoblacion)


import torch
import torch.nn.functional as func
import torch.nn as nn
import settings
from torch.utils.data import DataLoader
from torchvision import transforms
import pytorch_lightning as pl
from argparse import ArgumentParser
from dataset_loader import MazeDataset, CollateFn, ToTensor, Normalise

def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1):
    """3x3 convolution with padding"""
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                     padding=dilation, groups=groups, bias=False, dilation=dilation)

def conv1x1(in_planes, out_planes, stride=1):
    """1x1 convolution"""
    return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)

class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
                 base_width=64, dilation=1, norm_layer=None):
        super(BasicBlock, self).__init__()
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        if groups != 1 or base_width != 64:
            raise ValueError('BasicBlock only supports groups=1 and base_width=64')
        if dilation > 1:
            raise NotImplementedError("Dilation > 1 not supported in BasicBlock")
        # Both self.conv1 and self.downsample layers downsample the input when stride != 1
        self.conv1 = conv3x3(inplanes, planes, stride)
        self.bn1 = norm_layer(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(planes, planes)
        self.bn2 = norm_layer(planes)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        identity = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)

        if self.downsample is not None:
            identity = self.downsample(x)

        out += identity
        out = self.relu(out)

        return out

class Bottleneck(nn.Module):
    # Bottleneck in torchvision places the stride for downsampling at 3x3 convolution(self.conv2)
    # while original implementation places the stride at the first 1x1 convolution(self.conv1)
    # according to "Deep residual learning for image recognition"https://arxiv.org/abs/1512.03385.
    # This variant is also known as ResNet V1.5 and improves accuracy according to
    # https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch.

    expansion = 4

    def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
                 base_width=64, dilation=1, norm_layer=None):
        super(Bottleneck, self).__init__()
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        width = int(planes * (base_width / 64.)) * groups
        # Both self.conv2 and self.downsample layers downsample the input when stride != 1
        self.conv1 = conv1x1(inplanes, width)
        self.bn1 = norm_layer(width)
        self.conv2 = conv3x3(width, width, stride, groups, dilation)
        self.bn2 = norm_layer(width)
        self.conv3 = conv1x1(width, planes * self.expansion)
        self.bn3 = norm_layer(planes * self.expansion)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        identity = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample is not None:
            identity = self.downsample(x)

        out += identity
        out = self.relu(out)

        return out

class ResNet(pl.LightningModule):

    def __init__(self, hparams, block=BasicBlock, layers=[2, 2, 2, 2], num_classes=20, zero_init_residual=False,
                 groups=1, width_per_group=64, replace_stride_with_dilation=None,
                 norm_layer=None):
        super(ResNet, self).__init__()
        self.hparams = hparams
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        self._norm_layer = norm_layer

        self.inplanes = 64
        self.dilation = 1
        if replace_stride_with_dilation is None:
            # each element in the tuple indicates if we should replace
            # the 2x2 stride with a dilated convolution instead
            replace_stride_with_dilation = [False, False, False]
        if len(replace_stride_with_dilation) != 3:
            raise ValueError("replace_stride_with_dilation should be None "
                             "or a 3-element tuple, got {}".format(replace_stride_with_dilation))
        self.groups = groups
        self.base_width = width_per_group
        self.conv1 = nn.Conv2d(1, self.inplanes, kernel_size=7, stride=2, padding=3,
                               bias=False)
        self.bn1 = norm_layer(self.inplanes)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2,
                                       dilate=replace_stride_with_dilation[0])
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
                                       dilate=replace_stride_with_dilation[1])
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
                                       dilate=replace_stride_with_dilation[2])
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = nn.Linear(512 * block.expansion, num_classes * settings.NUM_POSE_DIMS)
        if self.hparams.dropout > 0:
            self.fc = nn.Sequential(nn.Dropout(self.hparams.dropout),
                                    nn.Linear(512 * block.expansion, num_classes * settings.NUM_POSE_DIMS))

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
            elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)

        # Zero-initialize the last BN in each residual branch,
        # so that the residual branch starts with zeros, and each residual block behaves like an identity.
        # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
        if zero_init_residual:
            for m in self.modules():
                if isinstance(m, Bottleneck):
                    nn.init.constant_(m.bn3.weight, 0)
                elif isinstance(m, BasicBlock):
                    nn.init.constant_(m.bn2.weight, 0)

    def _make_layer(self, block, planes, blocks, stride=1, dilate=False):
        norm_layer = self._norm_layer
        downsample = None
        previous_dilation = self.dilation
        if dilate:
            self.dilation *= stride
            stride = 1
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                conv1x1(self.inplanes, planes * block.expansion, stride),
                norm_layer(planes * block.expansion),
            )

        layers = []
        layers.append(block(self.inplanes, planes, stride, downsample, self.groups,
                            self.base_width, previous_dilation, norm_layer))
        self.inplanes = planes * block.expansion
        for _ in range(1, blocks):
            layers.append(block(self.inplanes, planes, groups=self.groups,
                                base_width=self.base_width, dilation=self.dilation,
                                norm_layer=norm_layer))

        return nn.Sequential(*layers)

    def _forward_impl(self, x):
        x = x.unsqueeze_(1)

        # See note [TorchScript super()]
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.fc(x)
        # attempt at making this work for more than just dx (so dy, dth)
        x = x.view(-1, settings.NUM_POSE_DIMS, settings.MAX_ITERATIONS)

        return x

    def forward(self, x):
        return self._forward_impl(x.float())

    def training_step(self, batch, batch_nb):
        x, y = batch
        y_hat = self(x)
        loss = func.mse_loss(y_hat, y.float())
        tensorboard_logs = {'train_loss': loss}
        return {'loss': loss, 'log': tensorboard_logs}

    def validation_step(self, batch, batch_nb):
        x, y = batch
        y_hat = self(x)
        return {'val_loss': func.mse_loss(y_hat, y.float())}

    def validation_epoch_end(self, outputs):
        avg_loss = torch.stack([x['val_loss'] for x in outputs]).mean()
        tensorboard_logs = {'val_loss': avg_loss}
        return {'val_loss': avg_loss, 'log': tensorboard_logs}

    def test_step(self, batch, batch_nb):
        x, y = batch
        y_hat = self(x)
        return {'test_loss': func.mse_loss(y_hat, y.float())}

    def test_epoch_end(self, outputs):
        avg_loss = torch.stack([x['test_loss'] for x in outputs]).mean()
        logs = {'test_loss': avg_loss}
        return {'test_loss': avg_loss, 'log': logs, 'progress_bar': logs}

    def configure_optimizers(self):
        return torch.optim.Adam(self.parameters(), lr=self.hparams.learning_rate)

    def train_dataloader(self):
        data_transform_for_training = transforms.Compose([ToTensor(), Normalise()])
        train_dataset = MazeDataset(root_dir=settings.MAZE_IMAGE_DIR,
                                    data_subset_type=settings.TRAIN_SUBSET,
                                    transform=data_transform_for_training)
        return DataLoader(train_dataset, batch_size=self.hparams.batch_size,
                          shuffle=True, num_workers=settings.NUM_CPUS, collate_fn=CollateFn)

    def val_dataloader(self):
        data_transform_for_evaluation = transforms.Compose([ToTensor(), Normalise()])
        val_dataset = MazeDataset(root_dir=settings.MAZE_IMAGE_DIR,
                                  data_subset_type=settings.VAL_SUBSET,
                                  transform=data_transform_for_evaluation)
        return DataLoader(val_dataset, batch_size=self.hparams.batch_size,
                          shuffle=False, num_workers=settings.NUM_CPUS, collate_fn=CollateFn)

    def test_dataloader(self):
        data_transform_for_evaluation = transforms.Compose([ToTensor(), Normalise()])
        test_dataset = MazeDataset(root_dir=settings.MAZE_IMAGE_DIR,
                                   data_subset_type=settings.TEST_SUBSET,
                                   transform=data_transform_for_evaluation)
        return DataLoader(test_dataset, batch_size=self.hparams.batch_size,
                          shuffle=False, num_workers=settings.NUM_CPUS, collate_fn=CollateFn)

    @staticmethod
    def add_model_specific_args(parent_parser):
        """
        Specify the hyperparams for this LightningModule
        """
        # MODEL specific
        parser = ArgumentParser(parents=[parent_parser], add_help=False)
        parser.add_argument('--learning_rate', default=settings.LEARNING_RATE, type=float)
        parser.add_argument('--batch_size', default=settings.BATCH_SIZE, type=int)
        parser.add_argument('--dropout', default=0, type=float)

        # training specific (for this model)
        parser.add_argument('--max_num_epochs', default=settings.MAX_EPOCHS, type=int)

        # program specific
        # parser.add_argument('--default_root_dir', default=settings.MAZE_MODEL_DIR, type=str)

        return parser


# -*- coding: utf-8 -*-
"""
Created on Sat Dec 29 15:42:24 2018
@author: 李萱
"""
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
#from pprint import pprint
import requests
import concurrent
import re
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import numpy as np
import os

url='https://www.etax.nat.gov.tw/etw-main/web/ETW183W1/'
html=requests.get(url).content.decode('utf-8')
sp=BeautifulSoup(html,'html.parser')

table=sp.find('table',{'class':"footable table default" })
rows=table.find_all('a',href=True,title=True)

title_array=[]
link_array=[]
for title in rows:
    title_array.append(title['title'])
    link_array.append(title['href'])

link_dict = dict(zip(title_array, link_array))
link_dict ={i:link_dict[i] for i in link_dict if len(i)==28 and link_dict[i]!='/etw-main/web/ETW183W3_10111'}    #len可以改为regex

font = FontProperties(fname=os.environ['WINDIR']+'\\Fonts\\kaiu.ttf', size=16)

def plot(cal_dict,category,mon,flag):
    plt.figure(figsize=(20,5))
    plt.bar(range(len(cal_dict)), list(cal_dict.values()), align='center')
    plt.xticks(range(len(cal_dict)), list(cal_dict.keys()),fontproperties=font, rotation=45)
    if flag == 0: plt.yticks(np.arange(0, 11, 1))
    if flag == 1: plt.yticks(np.arange(0,101,5))
    plt.title('%s年 发票得奖 %i万 统计图'%(mon,category),fontproperties=font)
    plt.xlabel('县市',fontproperties=font)
    plt.ylabel('发票张数',fontproperties=font)
    plt.show()

sum_dict_1={'基隆市':0,'台北市':0,'新北市':0,'桃园市':0, '新竹市':0, '新竹县':0, '苗栗县':0,'台中市':0,'彰化县':0,'南投县':0,'云林县':0,'嘉义市':0,'嘉义县':0,'台南市':0,'高雄市':0,'屏东县':0,'台东县':0,'花莲县':0,'宜兰县':0,'金门县':0,'澎湖县':0}
sum_dict_2={'基隆市':0,'台北市':0,'新北市':0,'桃园市':0, '新竹市':0, '新竹县':0, '苗栗县':0,'台中市':0,'彰化县':0,'南投县':0,'云林县':0,'嘉义市':0,'嘉义县':0,'台南市':0,'高雄市':0,'屏东县':0,'台东县':0,'花莲县':0,'宜兰县':0,'金门县':0,'澎湖县':0}
tmp={'基隆市':0,'台北市':0,'新北市':0,'桃园市':0, '新竹市':0, '新竹县':0, '苗栗县':0,'台中市':0,'彰化县':0,'南投县':0,'云林县':0,'嘉义市':0,'嘉义县':0,'台南市':0,'高雄市':0,'屏东县':0,'台东县':0,'花莲县':0,'宜兰县':0,'金门县':0,'澎湖县':0}

def calculate(address,category,mon,cal_dict):
    for i in address:
        for state in re.findall(r'">([\u4e00-\u9fa5]{3})',str(i)):
            state = state.replace('桃园县','桃园市')
            if category==1000:
                cal_dict[state]+=1
                sum_dict_1[state]+=1
            if category==200:
                cal_dict[state]+=1
                sum_dict_2[state]+=1
    #plot(cal_dict,category,mon,0)    #算个月的张数
    if mon == '10305':               #指定年月份
        plot(cal_dict,category,mon,0)

def find_winners(sp,mon):
    add1=[]
    add2=[]
    table=mon_sp.find('tbody')
    rows=table.find_all('tr')
    for row in rows:
        address1=row.find('td',{'headers':'companyAddress'})
        add1.append(address1)
        cal_dict_1=tmp.copy()
    calculate(add1,1000,mon,cal_dict_1)
    del add1[:]

    table2=table.find_next('tbody')
    rows2=table2.find_all('tr')
    for row2 in rows2:
        address2=row2.find('td',{'headers':'companyAddress2'})
        add2.append(address2)
        cal_dict_2=tmp.copy()
    calculate(add2,200,mon,cal_dict_2)
    del add2[:]

if __name__=="__main__":
    with concurrent.futures.ThreadPoolExecutor(max_workers=35) as excutor:
        for key, value in link_dict.items() :
            mon_url='https://www.etax.nat.gov.tw'+value
            mon=re.findall(r'https://www.etax.nat.gov.tw/etw-main/web/ETW183W3_([0-9]{5})',mon_url)
            mon_html=requests.get(mon_url)
            mon_html.encoding='big-5'
            mon_sp=BeautifulSoup(mon_html.text,'html.parser')
            find_winners(mon_sp,mon[0])
            if mon[0] == '10201':
                plot(sum_dict_1,1000,'102-107',1)
                plot(sum_dict_2,200,'102-107',1)


from __future__ import absolute_import, division, print_function, unicode_literals

import logging
import time

import numpy as np

from art.config import ART_NUMPY_DTYPE
from art.defences.trainer.adversarial_trainer_FBF import AdversarialTrainerFBF
from art.utils import random_sphere

logger = logging.getLogger(__name__)

class AdversarialTrainerFBFPyTorch(AdversarialTrainerFBF):
    """
    Class performing adversarial training following Fast is Better Than Free protocol.

    | Paper link: https://openreview.net/forum?id=BJx040EFvH

    | The effectiveness of this protoocl is found to be sensitive to the use of techniques like
        data augmentation, gradient clipping and learning rate schedules. Optionally, the use of
        mixed precision arithmetic operation via apex library can significantly reduce the training
        time making this one of the fastest adversarial training protocol.
    """

    def __init__(self, classifier, eps=8, use_amp=False, **kwargs):
        """
        Create an :class:`.AdversarialTrainerFBFPyTorch` instance.

        :param classifier: Model to train adversarially.
        :type classifier: :class:`.Classifier`
        :param eps: Maximum perturbation that the attacker can introduce.
        :type eps: `float`
        :param use_amp: Boolean that decides if apex should be used for mixed precision arithmantic during training
        :type use_amp: `bool`

        """
        super().__init__(classifier, eps, **kwargs)
        self._use_amp = use_amp

    def fit(self, x, y, validation_data=None, batch_size=128, nb_epochs=20, **kwargs):
        """
        Train a model adversarially with FBF protocol.
        See class documentation for more information on the exact procedure.

        :param x: Training set.
        :type x: `np.ndarray`
        :param y: Labels for the training set.
        :type y: `np.ndarray`
        :param validation_data: Tuple consisting of validation data
        :type validation_data: `np.ndarray`
        :param batch_size: Size of batches.
        :type batch_size: `int`
        :param nb_epochs: Number of epochs to use for trainings.
        :type nb_epochs: `int`
        :param kwargs: Dictionary of framework-specific arguments. These will be passed as such to the `fit` function of
                                  the target classifier.
        :type kwargs: `dict`
        :return: `None`
        """
        logger.info("Performing adversarial training with Fast is better than Free protocol")

        nb_batches = int(np.ceil(len(x) / batch_size))
        ind = np.arange(len(x))

        def lr_schedule(t):
            return np.interp([t], [0, nb_epochs * 2 // 5, nb_epochs], [0, 0.21, 0])[0]

        logger.info("Adversarial training FBF")

        for i_epoch in range(nb_epochs):
            # Shuffle the examples
            np.random.shuffle(ind)
            start_time = time.time()
            train_loss = 0
            train_acc = 0
            train_n = 0

            for batch_id in range(nb_batches):
                lr = lr_schedule(i_epoch + (batch_id + 1) / nb_batches)

                # Create batch data
                x_batch = x[ind[batch_id * batch_size : min((batch_id + 1) * batch_size, x.shape[0])]].copy()
                y_batch = y[ind[batch_id * batch_size : min((batch_id + 1) * batch_size, x.shape[0])]]

                _train_loss, _train_acc, _train_n = self._batch_process(x_batch, y_batch, lr)

                train_loss += _train_loss
                train_acc += _train_acc
                train_n += _train_n

            train_time = time.time()

            # compute accuracy
            if validation_data is not None:
                (x_test, y_test) = validation_data
                output = np.argmax(self.predict(x_test), axis=1)
                nb_correct_pred = np.sum(output == np.argmax(y_test, axis=1))
                logger.info(
                    "epoch {}\ttime(s) {:.1f}\tlr {:.4f}\tloss {:.4f}\tacc(tr) {:.4f}\tacc(val) {:.4f}".format(
                        i_epoch,
                        train_time - start_time,
                        lr,
                        train_loss / train_n,
                        train_acc / train_n,
                        nb_correct_pred / x_test.shape[0],
                    )
                )
            else:
                logger.info(
                    "epoch {}\t time(s) {:.1f}\t lr {:.4f}\t loss {:.4f}\t acc {:.4f}".format(
                        i_epoch, train_time - start_time, lr, train_loss / train_n, train_acc / train_n
                    )
                )

    def fit_generator(self, generator, nb_epochs=20, **kwargs):
        """
        Train a model adversarially with FBF protocol using a data generator.
        See class documentation for more information on the exact procedure.

        :param generator: Data generator.
        :type generator: :class:`.DataGenerator`
        :param nb_epochs: Number of epochs to use for trainings.
        :type nb_epochs: `int`
        :param kwargs: Dictionary of framework-specific arguments. These will be passed as such to the `fit` function of
                                  the target classifier.
        :type kwargs: `dict`
        :return: `None`
        """
        logger.info("Performing adversarial training with Fast is better than Free protocol")
        size = generator.size
        batch_size = generator.batch_size
        nb_batches = int(np.ceil(size / batch_size))

        def lr_schedule(t):
            return np.interp([t], [0, nb_epochs * 2 // 5, nb_epochs], [0, 0.21, 0])[0]

        logger.info("Adversarial training FBF")

        for i_epoch in range(nb_epochs):
            start_time = time.time()
            train_loss = 0
            train_acc = 0
            train_n = 0

            for batch_id in range(nb_batches):
                lr = lr_schedule(i_epoch + (batch_id + 1) / nb_batches)

                # Create batch data
                x_batch, y_batch = generator.get_batch()
                x_batch = x_batch.copy()

                _train_loss, _train_acc, _train_n = self._batch_process(x_batch, y_batch, lr)

                train_loss += _train_loss
                train_acc += _train_acc
                train_n += _train_n

            train_time = time.time()
            logger.info(
                "epoch {}\t time(s) {:.1f}\t lr {:.4f}\t loss {:.4f}\t acc {:.4f}".format(
                    i_epoch, train_time - start_time, lr, train_loss / train_n, train_acc / train_n
                )
            )

    def _batch_process(self, x_batch, y_batch, lr):
        """
        Perform the operations of FBF for a batch of data.
        See class documentation for more information on the exact procedure.

        :param x_batch: batch of x.
        :type x_batch: `np.ndarray`
        :param y_batch: batch of y.
        :type y_batch: `np.ndarray`
        :param lr: learning rate for the optimisation step.
        :type lr: `float`
        :return: `(float, float, float)`
        """
        import torch

        n = x_batch.shape[0]
        m = np.prod(x_batch.shape[1:])
        delta = random_sphere(n, m, self._eps, np.inf).reshape(x_batch.shape).astype(ART_NUMPY_DTYPE)
        delta_grad = self._classifier.loss_gradient(x_batch + delta, y_batch)
        delta = np.clip(delta + 1.25 * self._eps * np.sign(delta_grad), -self._eps, +self._eps)
        x_batch_pert = np.clip(x_batch + delta, self._classifier.clip_values[0], self._classifier.clip_values[1])

        # Apply preprocessing
        x_preprocessed, y_preprocessed = self._classifier._apply_preprocessing(x_batch_pert, y_batch, fit=True)

        # Check label shape
        if self._classifier._reduce_labels:
            y_preprocessed = np.argmax(y_preprocessed, axis=1)

        i_batch = torch.from_numpy(x_preprocessed).to(self._classifier._device)
        o_batch = torch.from_numpy(y_preprocessed).to(self._classifier._device)

        # Zero the parameter gradients
        self._classifier._optimizer.zero_grad()

        # Perform prediction
        model_outputs = self._classifier._model(i_batch)

        # Form the loss function
        loss = self._classifier._loss(model_outputs[-1], o_batch)

        self._classifier._optimizer.param_groups[0].update(lr=lr)

        # Actual training
        if self._use_amp:
            import apex.amp as amp

            with amp.scale_loss(loss, self._classifier._optimizer) as scaled_loss:
                scaled_loss.backward()
        else:
            loss.backward()

        # clip the gradients
        torch.nn.utils.clip_grad_norm_(self._classifier._model.parameters(), 0.5)
        self._classifier._optimizer.step()

        train_loss = loss.item() * o_batch.size(0)
        train_acc = (model_outputs[0].max(1)[1] == o_batch).sum().item()
        train_n = o_batch.size(0)

        return train_loss, train_acc, train_n


from copy import deepcopy
from pathlib import Path
from typing import Dict

from framework.cli.job import InteractiveCombinedJob, InteractiveCombinedJobSpec

logger_format = '%(asctime)s | %(levelname)s | %(module)s | %(message)s'
logger_dateformat = "%Y-%m-%d %H:%M:%S"

class Job_2_7(InteractiveCombinedJob):

    # max_sphere_radius (i.e. pruning distance) -> RAM/G
    def SM_RAM(self, distance: float) -> int:
        if distance <= 1:
            return 5
        elif distance <= 1.5:
            return 30
        # 198 is the largest min edge length, so the threshold below which the graph becomes disconnected
        elif distance <= 1.98:
            return 55
        elif distance <= 2:
            return 60
        else:
            # Max
            return 120

    LING_RAM: Dict[str, Dict[int, int]] = {
        "pmi_ngram": {
            1_000: 2,
            3_000: 3,
            10_000: 7,
            30_000: 11,
            40_000: 15,
            60_000: 20,
        },
        "ppmi_ngram": {
            1_000: 2,
            3_000: 3,
            10_000: 5,
            30_000: 7,
            40_000: 9,
            60_000: 11,
        }
    }

    def __init__(self, spec: InteractiveCombinedJobSpec):
        super().__init__(
            script_number="2_7",
            script_name="2_7_interactive_combined.py",
            spec=spec)

    @property
    def _ram_requirement_g(self):
        assert isinstance(self.spec, InteractiveCombinedJobSpec)
        return self.SM_RAM(self.spec.sensorimotor_spec.max_radius) \
               + self.LING_RAM[self.spec.linguistic_spec.model_name][self.spec.linguistic_spec.n_words]

if __name__ == '__main__':

    # Testing everything with a range of CCAs
    ccas = [0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1]

    jobs = []
    s: InteractiveCombinedJobSpec
    for s in InteractiveCombinedJobSpec.load_multiple(
            Path(Path(__file__).parent, "job_specifications/2022-05-24 longer runs more ccas.yaml")):
        for cca in ccas:
            spec = deepcopy(s)
            spec.cross_component_attenuation = cca
            jobs.append(Job_2_7(spec))

    for job in jobs:
        job.run_locally(extra_arguments=["--filter_events accessible_set"])

    print(f"Submitted {len(jobs)} jobs.")


import string

import telebot
import requests
from bs4 import BeautifulSoup

# FINALS
URL = "http://www.consultant.ru/document/cons_doc_LAW_10699/"
titles_and_hrefs = {}

# print(requests.get(URL).status_code) #check if it's work
# print(requests.get(URL).text) #get full html text

# ---------------------------------------------------------------------------
# скрипт для обновления бд на локалке
def update_data_base():
    q = requests.get(URL)
    result = q.content

    soup = BeautifulSoup(result, 'lxml')
    titles = soup.find(class_='document-page__toc').find_all('a')

    title_href = []

    for title in titles:
        href = title.get('href')
        text = title.text
        titles_and_hrefs[text] = href
        title_href.append(f'{href} --- {text}')

    with open('titles_url_list.txt', 'w') as file:
        for line in title_href:
            file.write(f'{line}\n')

# ---------------------------------------------------------------------------

# запись из файла в словарь
def create_dictationary():
    with open('titles_url_list.txt', 'r') as file:
        for line in file:
            splited_line = line.split('---')
            titles_and_hrefs[splited_line[1]] = (splited_line[0])

# поиск сообщения пользователя по ключам
def search_for(text, source):
    text = text.replace('/', '')
    list = []
    keys = source.keys()
    for key in keys:
        if key.find(text) != -1:
            list.append(key)
            print(list)
    return list

def telegram_bot():

    API_TOKEN = '5662360252:AAFuHDxBwgGPL0QobOmK3fHVcvt_S7W-Gqc'

    bot = telebot.TeleBot(API_TOKEN)

    # @bot.message_handler(commands=["start"])
    # def start_bot(message):
    #     bot.send_message(message.chat.id, 'Привет, я бот Юрик. Напиши мне номер статьи или ключевые слова и я попробую найти её')

    @bot.message_handler(commands=["46"])
    def find_title(message):
        list = search_for(message.text, titles_and_hrefs)
        bot.send_message(message.chat.id, "Searching...")
        print(list)
        for item in list:
            bot.send_message(message.chat.id, f'Вот что мне удалось найти: {item}\n{titles_and_hrefs.get(item)}\n')

    bot.polling()

def main():
    create_dictationary()
    print(search_for("45", titles_and_hrefs))

if __name__ == '__main__':
    # main()
    telegram_bot()


# @Date    : 16:03 08/01/2021
# @Author  : ClassicalPi
# @FileName: 983.py
# @Software: PyCharm

import sys
class Solution:
    def mincostTickets(self, days: [int], costs: [int]) -> int:
        res = [0 for i in range(0, days[-1]+1)]
        daySet = set(days)
        for day in range(1,days[-1]+1):
            if day in daySet:
                res[day] = min(res[max(day-1),0]+costs[0],res[max(day-7),0]+costs[1],res[max(day-30),0]+costs[2])
            else:
                res[day] = res[day-1]
        return res[-1]
if __name__ == '__main__':
    S=Solution()
    print(S.mincostTickets([1,4,6,7,8,20],[2,7,15]))


from random import randint
randomNumber = randint(1,100)
print('---PLEASE SELECT A NUMBER BETWEEN 1 AND 100----')

userGuessList = [0]

while True:
	userGuessNumber = int(input('Enter a your number Guess'))

	if (userGuessNumber < 1)  or (userGuessNumber > 100):
		print('OUT OF BOUNDS')
		continue

	if userGuessNumber == randomNumber:
		print('YOU WON')
		break

	##if the guess is incorret, add guess to the list
	userGuessList.append[userGuessNumber]

	if userGuessList[-2]:

		if (abs(randomNumber - userGuessNumber)  <abs(randomNumber - userGuessList[-2])):
			print('WARMER')
		else:
			print('COLDER')

	else:
		if abs(randomNumber - userGuessNumber) <= 10:
			print('WARM')
		else:
			print('COLD')

	break


import nltk
import inflect

from nltk.tokenize import word_tokenize

inflect = inflect.engine()

def is_noun(tag):
    return tag in ['NN', 'NNS', 'NNP', 'NNPS']

def is_verb(tag):
    return tag in ['VB', 'VBD', 'VBG', 'VBN', 'VBP', 'VBZ']

def is_adverb(tag):
    return tag in ['RB', 'RBR', 'RBS']

def is_adjective(tag):
    return tag in ['JJ', 'JJR', 'JJS']

def is_pronoun(tag):
    return tag in ['PRP$', 'PRP', 'WP', 'WP$']

def is_numeral(tag):
    return tag in ['JJ', 'CD']

def get_tags(query):
    return nltk.pos_tag(word_tokenize(query))

def is_singular_noun(tag):
    return (is_noun(tag[1])) and (not inflect.singular_noun(tag[0]))

def is_plural_noun(tag):
    return (is_noun(tag[1])) and (inflect.singular_noun(tag[0]))

def check_to_be(word, tags, i):
    after_word = None
    after_word2 = None
    before_word = None
    after_tag = None
    if i > 0:
        before_word = tags[i - 1][0]
    if i + 1 < len(tags):
        after_word = tags[i + 1][0]
        after_tag = tags[i + 1][1]
    if i + 2 < len(tags):
        after_word2 = tags[i + 2][1]
    if word.lower() == 'i':
        if after_word in ['am', 'was', 'wasn\'t']:
            if is_verb(after_word2):
                return False
            else:
                return True
        elif is_verb(after_tag):
            return True
        else:
            if before_word in ['am', 'was', 'wasn\'t', 'not']:
                return True
            else:
                return False
    elif word.lower() in ['you', 'we', 'they']:
        if after_word in ['are', 'were', 'weren\'t']:
            if is_verb(after_word2):
                return False
            else:
                return True
        else:
            if before_word in ['are', 'were', 'weren\'t', 'not']:
                return True
            else:
                return False
    elif word.lower() in ['he', 'she', 'it']:
        if after_word in ['is', 'was', 'wasn\'t']:
            if is_verb(after_word2):
                return False
            else:
                return True
        else:
            if before_word in ['is', 'was', 'wasn\'t', 'not']:
                return True
            else:
                return False
    else:
        return True

def check_adverbs_frequency(i, tags):
    if is_adverb(tags[i][1]):
        if (tags[i][0] in ['always', 'usually', 'sometimes', 'never']) and (i == 0):
            return False
        elif tags[i][0] in ['always', 'usually', 'sometimes']:
            if (not is_noun(tags[i-1][1])) and (not is_pronoun(tags[i-1][1])):
                return False
            else:
                return is_verb(tags[i+1])
        elif tags[i][0] == 'never':
            if(not is_noun(tags[i - 1][1])) and (not is_pronoun(tags[i - 1][1])):
                return False
            else:
                if is_verb(tags[i+1][1]):
                    f = True
                    for x in tags:
                        if ('not' in x) or ('n\'t' in x):
                            f = False
                            break
                    return f
                else:
                    return False
    return True

def check_articles(i, tags):
    if is_plural_noun(tags[i]):
        if tags[i-1][0].lower() in ['a', 'an']:
            return False
        elif tags[i-1][0].lower() == 'the':
            return True
        elif is_adjective(tags[i-1][1]):
            if tags[i-2][0].lower() in ['a', 'an']:
                return False
            if tags[i-2][0].lower() == 'the':
                return True
        else:
            return False
    if is_singular_noun(tags[i]):
        if (not tags[i-1][0] in ['a', 'an', 'the']) and (not is_adjective(tags[i-1][1])):
            return False
        else:
            return True
    return True

def check_there(i, tags):
    if tags[i][0].lower() == 'there':
        if tags[i + 1][0] in ['is', 'isn\'t']:
            if (tags[i + 2][0] in ['a', 'an']) and (is_singular_noun(tags[i + 3])):
                return True
            elif (tags[i + 2][0] in ['a', 'an']) and (is_adjective(tags[i + 3][1])) and (is_singular_noun(tags[i + 4])):
                return True
            elif (tags[i + 2][0] in ['one']) and (is_singular_noun(tags[i + 3])):
                return True
            else:
                return False
        elif tags[i + 1][0] in ['are']:
            if is_plural_noun(tags[i + 2]):
                return True
            elif (tags[i + 2][0] == 'some') and (is_plural_noun(tags[i + 3])):
                return True
            elif (tags[i + 2][0] != 'one') and (is_numeral(tags[i + 2][1])) and (is_plural_noun(tags[i + 3])):
                return True
            else:
                return False
        elif tags[i + 1][0] in ['aren\'t']:
            if is_plural_noun(tags[i + 2]):
                return True
            elif (tags[i + 2][0] == 'any') and (is_plural_noun(tags[i + 3])):
                return True
            elif (tags[i + 2][0] != 'one') and (is_numeral(tags[i + 2][1])) and (is_plural_noun(tags[i + 3])):
                return True
            else:
                return False
        elif tags[i - 1][0].lower() == 'is':
            if (tags[i + 1][0] in ['a', 'an']) and (is_singular_noun(tags[i + 2])):
                return True
            elif (tags[i + 1][0] in ['a', 'an']) and (is_adjective(tags[i + 2][1])) and (is_singular_noun(tags[i + 3])):
                return True
            else:
                return False
        elif tags[i - 1][0].lower() == 'are':
            if (tags[i + 1][0] == 'any') and (is_plural_noun(tags[i + 2])):
                return True
            else:
                return False
    elif tags[i][0].lower() == 'there\'s':
        if (tags[i + 1][0] in ['a', 'an']) and (is_singular_noun(tags[i + 2])):
            return True
        elif (tags[i + 1][0] in ['a', 'an']) and (is_adjective(tags[i + 2][1])) and (is_singular_noun(tags[i + 3])):
            return True
        elif (tags[i + 1][0] != 'one') and (is_numeral(tags[i + 1][1])) and (is_plural_noun(tags[i + 2])):
            return True
        else:
            return False
    else:
        return True

def check_object_pronouns(word, before_word, before_prep):
    if (word.lower() in ['you', 'it']) and (before_word is None):
        return True
    if word in ['me', 'him', 'her', 'us', 'them', 'you', 'it']:
        if before_word[1] == 'IN':
            if is_verb(before_prep[1]):
                return True
            else:
                return False
        elif is_verb(before_word[1]):
            return True
        else:
            return False
    else:
        return True

def check_plural_form_noun(word):
    if inflect.singular_noun(word) is False:
        return True
    else:
        sing_word = inflect.singular_noun(word)
        plural_word = inflect.plural_noun(sing_word)
        if plural_word == word:
            return True
        else:
            return plural_word

def check_can_permission(before_word, after_word):
    if (before_word is None) or (after_word is None):
        return True
    elif after_word in ['can\'t', 'can', 'cannot']:
        return True
    elif before_word not in ['can\'t', 'can', 'cannot', 'not']:
        return False
    else:
        return True

def get_errors_plural_forn_nouns(message):
    errors = []
    position = -1
    tags = nltk.pos_tag(word_tokenize(message))
    print(tags)
    for i, tag in enumerate(tags):
        position = message.find(tag[0], position + 1)
        answer = check_to_be(tag[0], tags, i)
        if not answer:
            errors.append({'ErrorType': 'error4 (to be)', 'Position': position, 'Correct': ''})
        if is_noun(tag[1]):
            answer = check_plural_form_noun(tag[0])
            if answer != True:
                errors.append({'ErrorType': 'error1', 'Position': position, 'Correct': answer})
        if is_pronoun(tag[1]):
            before_word = None
            after_word = None
            if i > 0:
                before_word = tags[i - 1][0]
            if i + 1 < len(tags):
                after_word = tags[i + 1][0]
            answer = check_can_permission(before_word, after_word)
            if not answer:
                errors.append({'ErrorType': 'error2 (can/cant - permission)', 'Position': position, 'Correct': ''})
            before_word = None
            before_prep = None
            if i > 0:
                before_word = tags[i - 1]
            if i - 1 > 0:
                before_prep = tags[i - 2]
            answer = check_object_pronouns(tag[0], before_word, before_prep)
            if not answer:
                errors.append({'ErrorType': 'error3 (Object Pronouns)', 'Position': position, 'Correct': ''})
        try:
            answer = check_there(i, tags)
            if not answer:
                errors.append({'ErrorType': 'error5 (there is/are)', 'Position': position, 'Correct': ''})
        except:
            print('error5')

        try:
            answer = check_articles(i, tags)
            if not answer:
                errors.append({'ErrorType': 'error6 (articles)', 'Position': position, 'Correct': ''})
        except:
            print('error6')
        try:
            answer = check_adverbs_frequency(i, tags)
            if not answer:
                errors.append({'ErrorType': 'error7 (adverb of frequency)', 'Position': position, 'Correct': ''})
        except:
            print('error7')
    return errors


class Solution:
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        digits[-1] = digits[-1] + 1
        i = len(digits) - 1
        while digits[i] == 10:
            digits[i] = 0
            if i != 0:
                digits[i - 1] += 1
            else:
                digits.insert(0, 1)
            i -= 1
        else:
            return digits


import requests
import json
from bs4 import BeautifulSoup
URL = 'https://www.indiabix.com/aptitude/permutation-and-combination/065002'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
#job_elems = soup.find_all('td', class_='bix-td-qtxt')
questions = []
job_elems = soup.find_all('div', class_='bix-div-container')
i = 0
for job_elem in job_elems:
    #print((job_elem.find('p')).text)
    questions.append({})
    #print(job_elem)
    q = job_elem.find('td',class_='bix-td-qtxt')
    questions[i]["question"] = (((q.find('p')).text).strip()).encode('ascii')
    options = job_elem.find_all('td',class_ = 'bix-td-option')
    #print(options)
    even = True
    opval = ""
    answer = job_elem.find('div',class_='bix-div-answer')
    #print(answer)
    ans = ((((answer.find('p')).find('span',class_='jq-hdnakqb')).text).strip()).encode('ascii')
    print(ans)
    first = 1
    '''for a in two :
        if first == 0:
            ans = ((a.text).strip()).encode('ascii')
        first = 0'''
    for option in options :
        if even :
            opt = (((option.text)[:-1]).strip()).encode('ascii')
            opval = "option" + opt
            even = False
        else :
            value = ((option.text).strip()).encode('ascii')
            questions[i][opval] = value
            if opt == ans :
                questions[i]["answer"] = value
            even = True

    i += 1

print(questions)
with open('data.txt', 'w') as outfile:
    json.dump(questions, outfile)


import sys
from bisect import bisect_left

input = sys.stdin.readline

# 1965 상자넣기
# 앞에있는 상자를 뒤에있는 더 큰 상자에 넣을 수 있을 때
# 하나의 상자에 넣을 수 있는 상자의 갯수의 최댓값을 구해야한다
# 최장 증가 부분 수열의 길이를 구하는 문제와 동일
def sol1965():
    # 상자의 갯수
    n = int(input())

    # lis 알고리즘을 사용하여 최장 증가 부분 수열의 길이를 구하여 반환
    lis = [0]
    for s in map(int, input().split()):
        if s > lis[-1]:
            lis.append(s)
        else:
            lis[bisect_left(lis, s)] = s

    return len(lis) - 1