import matplotlib.pyplot as plt
import math
import numpy as np
from matplotlib import animation

delta_x, c ,r , k = 0.001, 300 , 1 , 1000
delta_t=delta_x/c
T=0.04
N=int(T*c/delta_x)
t=np.linspace(0,T,N+1)

def next_step(y_previous,y_current):
    y_next=[0]
    c1,c2=2*(1-r**2),r**2
    for i in range(1,len(y_current)-1):
        y_next.append(c1*y_current[i]-y_previous[i]+c2*(y_current[i-1]+y_current[i+1]))
    y_next.append(0)
    return y_next

def initial_Gaussian(x_excite):#initialize or excite the string in some point in the gaussian way
    y_initial=[]
    for i in range(1+int(1./delta_x)):
        y_initial.append(math.exp(-k*(i*delta_x-x_excite)**2))
    return y_initial

def initial_real(x_excite):#initialize or excite the string in some point in the real way
    y_initial=[]
    for i in range(int(x_excite/delta_x)):
        y_initial.append(i*delta_x/x_excite)
    for i in range(int(x_excite/delta_x),1+int(1./delta_x)):
        y_initial.append((i*delta_x-1.)/(x_excite-1))
    return y_initial

def motion_of_a_point(x_observe,x_excite):
    y_initial=initial_real(x_excite) #this can be changed accrodingly
    y_observe=[]                  #calculate the displacement at x=0.05 or y(i=I,n)
    I=int(x_observe/delta_x)
    y0=y_initial
    y1=y_initial
    y_observe.append(y1[I])
    for i in range(N):
        y2=next_step(y0,y1)
        y0,y1=y1,y2
        y_observe.append(y1[I])
    return y_observe

#plot
x_observe_list,x_ex=[0.05,0.1,0.2,0.3],0.45

x_ob=x_observe_list[0]
yx=motion_of_a_point(x_ob,x_ex)
ps=abs(np.fft.rfft(yx))**2
freq= np.linspace(0, c/delta_x/2, len(ps))

plt.subplot(1,2,1)
plt.plot(t,yx)
plt.title('String Signal versus Time')
plt.xlabel('time(s)')
plt.ylabel('Signal(arbitrary units)')
#plt.text(0.,0.4,r'R Wavepacket,$y_0(x)=exp[-1000\times(x-%s)^2]$'%x_ex+'\n$x_{observe}=%s$'%x_ob)
plt.text(0,0.12,'Real Wavepacket of Two Straight Lines')
plt.xlim(0,T)

plt.subplot(1,2,2)
plt.plot(freq,ps)
plt.xlim(0,3000)
plt.xlabel('Frequency(Hz)')
plt.ylabel('Power')
plt.title('Power spectrum')
#plt.text(1500,200000,r'Gaussian Wavepacket'+'\n$y_0(x)=exp[-1000*(x-%s)^2]$'%x_ex+'\n$x_{observe}=%s$'%x_ob)
plt.text(1500,200000,r'Real Wavepacket'+'\n$x_{excite}=$%s'%x_ex+'\n$x_{observe}=%s$'%x_ob)

plt.show()


"""recreating deleted revision

Revision ID: fdc1cbf4bbde
Revises: 0b86175e066f
Create Date: 2023-09-06 12:51:46.232440

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'fdc1cbf4bbde'
down_revision = '0b86175e066f'
branch_labels = None
depends_on = None

def upgrade():
    op.drop_column('users', 'site_visits')
    op.add_column('users', sa.Column('ip', sa.String(), nullable=True))
    op.add_column('games', sa.Column('ip', sa.String(), nullable=True))

def downgrade():
    op.add_column('users', sa.Column('site_visits', sa.Integer(), nullable=True))
    op.drop_column('users', 'ip')
    op.drop_column('games', 'ip')


from uno_env import UnoEnv

from stable_baselines3 import PPO
from stable_baselines3.common.callbacks import EvalCallback

import os
import datetime

timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
model_name = "PPO_" + timestamp

# Directory Setup
model_dir = "models/" + model_name
logdir = "logs"

if not os.path.exists(model_dir):
    os.makedirs(model_dir)
if not os.path.exists(logdir):
    os.makedirs(logdir)

# Environment Setup
env = UnoEnv()
env.reset()

eval_env = UnoEnv()
eval_env.reset()

# Model Setup

eval_callback = EvalCallback(eval_env, best_model_save_path=model_dir,
                             log_path=logdir, eval_freq=1000,
                             deterministic=True, render=False)

model = PPO('MlpPolicy', env, verbose=1, tensorboard_log=logdir)
model.learn(total_timesteps=10000000, tb_log_name=model_name)

env.close()


import io
import itertools
import struct

from .constants import *

def bytes_to_ints(b):
    return [
        struct.unpack('I', bytes(x))[0]
        for x in itertools.zip_longest(*([iter(b)] * 4))]

def read_at(fp, offset, n):
    if fp.seek(offset) != offset:
        raise RuntimeError(f'seek({offset}) operation failed')
    b = fp.read(n)
    if len(b) < n:
        raise RuntimeError(f'read({n}) operation failed')
    return b

class SectorReader:
    def __init__(self, src, sector_size, sectors, offset_resolver, size=None):
        self.src = src
        self.sector_size = sector_size
        self.sectors = sectors
        self.offset_resolver = offset_resolver

        self.offset = 0
        self.max_offset = min(sector_size*len(sectors), size or float('inf'))

    def tell(self):
        return self.offset

    def seek(self, offset, whence=0):
        if whence == 0:
            new_offset = offset
        elif whence == 1:
            new_offset = self.offset + offset
        elif whence == 2:
            new_offset = self.max_offset + offset
        else:
            raise ValueError(f'whence value {whence} unsupported')

        if new_offset < 0:
            raise ValueError(f'invalid offset {new_offset}')

        self.offset = new_offset
        return self.offset

    def read(self, size=-1):
        if self.offset >= self.max_offset:
            return b''

        if size == -1 or size > self.max_offset - self.offset:
            size = self.max_offset - self.offset

        chunks = []
        read = 0
        while read < size:
            offset = self.offset_resolver(self.sectors[self.offset // self.sector_size])
            sector_offset = self.offset % self.sector_size

            to_read = min(size - read, self.sector_size - sector_offset)
            chunks.append(read_at(self.src, offset + sector_offset, to_read))
            read += to_read
            self.offset += to_read

        return b''.join(chunks)

def sector_chain(fat, starting_sector):
    sectors = []
    sector = starting_sector
    while sector != ENDOFCHAIN:
        sectors.append(sector)
        sector = fat[sector]
    return sectors


from typing import List

# LeetCode / 15. 3Sum
class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        result = []
        result_dict = {}
        nums_dict = {}
        nums.sort()

        for i in range(len(nums)):
            if nums[i] in nums_dict:
                nums_dict[nums[i]] += 1
            else:
                nums_dict[nums[i]] = 1

        for i in range(len(nums)):
            for j in range(i+1, len(nums)):
                tempSum = (nums[i] + nums[j]) * -1
                if tempSum in nums_dict:
                    count = 1
                    if tempSum == nums[i]:
                        count += 1
                    if tempSum == nums[j]:
                        count += 1
                    if nums_dict[tempSum] < count:
                        continue

                    temp = [nums[i], nums[j], tempSum]
                    temp.sort()
                    if not tuple(temp) in result_dict:
                        result_dict[tuple(temp)] = True
                        result.append(temp)

        return result

nums = [0,0,0]
print(Solution().threeSum(nums))


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pandas as pd

pd.set_option("display.max_rows", 5)
pd.set_option("display.max_columns", 6)
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.backends.backend_pdf import PdfPages

pp = PdfPages("plots.pdf")
print("Setup Complete")

penguins = sns.load_dataset("penguins")
print(penguins.head())
plot = sns.histplot(data=penguins, x="flipper_length_mm", hue="species", multiple="stack")
plot.set_title("Fig 1: Histogram")
pp.savefig()
plot.figure.clear()

plot = sns.kdeplot(data=penguins, x="flipper_length_mm", hue="species", multiple="stack")
plot.set_title("Fig 2: KDE")
pp.savefig()
plot.figure.clear()

plot = sns.displot(data=penguins, x="flipper_length_mm", hue="species", multiple="stack")
plot.figure.suptitle("Fig 3: Displot stacked histogram")
pp.savefig()
plot.figure.clear()

plot = sns.displot(
    data=penguins, x="flipper_length_mm", hue="species", multiple="stack", kind="kde"
)
plot.figure.suptitle("Fig 4: Displot KDE")
pp.savefig()
plot.figure.clear()

plot = sns.displot(data=penguins, x="flipper_length_mm", hue="species", col="species")
plot.figure.suptitle("")
pp.savefig()
plot.figure.clear()

f, axs = plt.subplots(1, 2, figsize=(8, 4), gridspec_kw=dict(width_ratios=[4, 3]))
sns.scatterplot(data=penguins, x="flipper_length_mm", y="bill_length_mm", hue="species", ax=axs[0])
sns.histplot(
    data=penguins, x="species", hue="species", shrink=0.8, alpha=0.8, legend=False, ax=axs[1]
)
f.tight_layout()
pp.savefig()
f.figure.clear()

tips = sns.load_dataset("tips")
g = sns.relplot(data=tips, x="total_bill", y="tip")
g.ax.axline(xy1=(10, 2), slope=0.2, color="b", dashes=(5, 2))
pp.savefig()
g.figure.clear()

g = sns.relplot(data=penguins, x="flipper_length_mm", y="bill_length_mm", col="sex")
g.set_axis_labels("Flipper length (mm)", "Bill length (mm)")
pp.savefig()
g.figure.clear()

sns.jointplot(data=penguins, x="flipper_length_mm", y="bill_length_mm", hue="species")
pp.savefig()

sns.pairplot(data=penguins, hue="species")
pp.savefig()

sns.jointplot(data=penguins, x="flipper_length_mm", y="bill_length_mm", hue="species", kind="hist")
pp.savefig()

pp.close()


from django.shortcuts import get_object_or_404, render, redirect

from .forms import TodoListForm
from .models import TodoList

def home(request):
    if request.user.is_authenticated():
        return redirect('todolists:show_lists')
    return render(request, 'todolists/home.html')

def new_list(request):
    if request.method == 'POST':
        form = TodoListForm(request.POST)
        if form.is_valid():
            new_list = TodoList()
            new_list.title = form.cleaned_data['title']
            new_list.description = form.cleaned_data['description']
            new_list.is_active = True
            new_list.user = request.user
            new_list.save()
            return redirect('todolists:show_lists')
    else:
        form = TodoListForm()
    return render(request, 'todolists/new_list.html', {'form': form})

def show_lists(request, category=None):
    todolists = TodoList.objects.filter(is_active=True, user=request.user)
    return render(request, 'todolists/show_lists.html', {'todolists': todolists})

def list_detail(request, pk):
    list = get_object_or_404(TodoList, pk=pk)
    return render(request, 'todolists/list_detail.html', {'list': list})


import imutils
import cv2
import numpy as np
from skimage import exposure
import matplotlib.pyplot as plt
import math
import scipy.spatial.distance
#import the necessary library :)

def show_image(image):
    """
    Function to plot an image
    Keyword arguments: image
    Return: None
    """
    cv2.imshow("Immagine",image)
    cv2.waitKey(0)
    return None

def find_4corners(approx_corners):
    """
    Function able to find the 4 external corners in a set of points.
    Keyword arguments: list of points
    Return: list of 4 more distant corners by each other
    """
    if approx_corners is None:
        print("\n ERROR 2: corners not found \n")
        return None
    #  find the corner points of the contours and print it
    approx_corners = sorted(np.concatenate(approx_corners).tolist())
    # find the most extreme corners, only 4 corner points
    x = [a[0] for a in approx_corners]
    y = [a[1] for a in approx_corners]
    # the top-left point has the smallest sum whereas the
    # bottom-right has the largest and compute the difference between
    # the points. the top-right will have the minumum difference and
    # the bottom-left will have the maximum difference
    diff = np.array(x) - np.array(y)
    addition = np.array(x) + np.array(y)
    tl = approx_corners[np.argmin(addition)] # top left point
    br = approx_corners[np.argmax(addition)] # bottom right point
    bl = approx_corners[np.argmin(diff)] # bottom left point
    tr = approx_corners[np.argmax(diff)] # top right point

    #print('\nAll the corner points are: ' + str([tl,bl,br,tr]))
    return [tl,bl,br,tr]

def find_corners(image):

    """
    Function to find all the corners in an image, not used in this case
    Keyword arguments: image
    Return: list of apporximated corners
    """
    if image is None:
        print("\n ERROR 1: image not found or missing \n")
        return None
    # grayscale image and blurring
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    # threshold the image, then perform a series of
    # dilations to remove any small regions of noise
    thresh = cv2.threshold(gray, 180, 255, cv2.THRESH_OTSU+cv2.THRESH_BINARY_INV)[1] #aleatory param
    thresh = cv2.dilate(thresh, None, iterations=8)
    # find contours in thresholded image, then grab the largest one
    cnt = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnt = imutils.grab_contours(cnt)
    cnt = max(cnt, key=cv2.contourArea)
    epsilon = 0.015*cv2.arcLength(cnt, True)
    approx_corners = cv2.approxPolyDP(cnt, epsilon, True)
    return approx_corners

def find_edges(image):

    """
    Function to find the edges of an image
    Keyword arguments: image
    Return: list of approximated corners
    """

    if image is None:
        print("\n ERROR 1: image not found or missing \n")
        return None
    # grayscale image and blurring
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    #blur = cv2.bilateralFilter(gray,10,17,17) # try with bilateral filter
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    # threshold the image, then perform a series of erosions +
    # dilations to remove any small regions of noise
    thresh = cv2.Canny(image,0,50) # try with canny edge detection, not always the best solution
    thresh = cv2.dilate(thresh, None, iterations=2)
    # find contours in thresholded image, then grab the largest one
    cnt = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cnt = imutils.grab_contours(cnt)
    cnt = max(cnt, key=cv2.contourArea)
    epsilon = 0.015*cv2.arcLength(cnt, True)
    approx_corners = cv2.approxPolyDP(cnt, epsilon, True)

    return approx_corners

def rectification_db(image, image_db):

    """
    Function to implement the rectification in an image prevously matched with an images db
    Keyword arguments: image, image_db
    Return: original image, rectified image
    """
    approx_corners = find_edges(image)
    approx_corners_db = find_edges(image_db)
    #take the source points and destination points
    point_src = np.float32(find_4corners(approx_corners))
    point_dst = np.float32(find_4corners(approx_corners_db))
    # homography process and warp prespective images
    H, mask = cv2.findHomography(point_src, point_dst, cv2.RANSAC, 5.0) # mask unusued
    final_width = image_db.shape[0]
    final_high = image_db.shape[1]
    rectified_image = cv2.warpPerspective(image, np.float32(H), (image_db.shape[1],image_db.shape[0]))
    rectified_image = exposure.rescale_intensity(rectified_image, out_range = (0, 255))

    return image, rectified_image

def perspective_correction(img):
    """
    Function to implement the rectification of an image. The formula was taken by a paper
    on the web. Link: https://www.microsoft.com/en-us/research/publication/whiteboard-scanning-image-enhancement/?from=http%3A%2F%2
    Fresearch.microsoft.com%2Fen-us%2Fum%2Fpeople%2Fzhang%2Fpapers%2Ftr03-39.pdf
    Keyword arguments: image
    Return: rectified image
    """
    (rows, cols, _) = img.shape
    u0 = (cols)/2.0
    v0 = (rows)/2.0

    #detected corners on the original image
    approx_corners = find_edges(img)
    tl, bl, br, tr = find_4corners(approx_corners)
    print("CORNERS:" + str([tl,bl,br,tr]))
    """
    # TESTING: drawing all the corner points in the image
    cv2.putText(img, "LT", tuple(tl), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1, cv2.LINE_AA)
    cv2.putText(img, "LD", tuple(bl), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1, cv2.LINE_AA)
    cv2.putText(img, "RT", tuple(tr), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
    cv2.putText(img, "RD", tuple(br), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 255), 1, cv2.LINE_AA)
    show_image(img)
    """
    #widths and heights of the projected image
    w1 = scipy.spatial.distance.euclidean(tl, tr)
    w2 = scipy.spatial.distance.euclidean(bl, br)
    h1 = scipy.spatial.distance.euclidean(tl, bl)
    h2 = scipy.spatial.distance.euclidean(tr, br)
    w = max(w1,w2)
    h = max(h1,h2)

    #visible aspect ratio
    ar_vis = float(w)/float(h)

    #make numpy arrays and append 1 for linear algebra
    m1 = np.array((tl[0], tl[1],1)).astype('float32')
    m2 = np.array((tr[0], tr[1],1)).astype('float32')
    m3 = np.array((bl[0], bl[1],1)).astype('float32')
    m4 = np.array((br[0], br[1],1)).astype('float32')

    #calculate the focal distance
    k2 = np.dot(np.cross(m1,m4),m3) / np.dot(np.cross(m2,m4),m3)
    k3 = np.dot(np.cross(m1,m4),m2) / np.dot(np.cross(m3,m4),m2)
    n2 = k2 * m2 - m1
    n3 = k3 * m3 - m1
    n21 = n2[0]
    n22 = n2[1]
    n23 = n2[2]
    n31 = n3[0]
    n32 = n3[1]
    n33 = n3[2]
    try:
        f = math.sqrt(np.abs((1.0/(n23*n33)) *
        ((n21*n31 - (n21*n33 + n23*n31)*u0 + n23*n33*u0*u0)
        + (n22*n32 - (n22*n33+n23*n32)*v0 + n23*n33*v0*v0))))

        A = np.array([[f,0,u0], [0,f,v0], [0,0,1]]).astype('float32')

        At = np.transpose(A)
        Ati = np.linalg.inv(At)
        Ai = np.linalg.inv(A)
        #calculate the real aspect ratio
        ar_real = math.sqrt(np.dot(np.dot(np.dot(n2,Ati),Ai),n2)
            /np.dot(np.dot(np.dot(n3,Ati),Ai),n3))
        if ar_real < ar_vis:
            W = int(w)
            H = int(float(W / ar_real))
        else:
            H = int(h)
            W = int(ar_real * H)
    except Exception:
        ar_real = ar_vis
        H = int(h)
        W = int(ar_real * H)

    pts1 = np.array([tl, tr, bl, br]).astype('float32')
    pts2 = np.float32([[0,0],[W,0],[0,H],[W,H]])
    #project the image with the new w/h
    M = cv2.getPerspectiveTransform(pts1,pts2)
    dst = cv2.warpPerspective(img, M, (W,H))
    rectified_image = dst
    rectified_image = exposure.rescale_intensity(dst, out_range = (0, 255))
    return rectified_image


from sqlalchemy import delete, select

from app.core.issue.models import Issue
from app.core.issue.vault import AbstractIssueVault
from app.service.postgres.issue.entity import IssueEntity

class IssuePostgresVault(AbstractIssueVault):
    """Implementation of abstract vault."""

    def __init__(self, db_session) -> None:
        self.db_session = db_session

    async def create(self, model: Issue):
        """Implementation of abstract method."""
        entity_to_create = IssueEntity(**model.dict())
        async with self.db_session() as session:
            session.add(entity_to_create)
            await session.commit()

    async def read(self, uid: int) -> Issue:
        """Implementation of abstract method."""
        async with self.db_session() as session:
            entity = (
                await session.select(IssueEntity).where(IssueEntity.uid == uid).first()
            )
            return Issue(entity)

    async def read_all(self, project_uid: int) -> list[Issue]:
        """Implementation of abstract method."""
        async with self.db_session() as session:
            query = select(IssueEntity).where(IssueEntity.project_id == project_uid)
            commit_list = await session.execute(query)
            return commit_list.scalars().unique().all()

    async def update(self, model: Issue):
        """Implementation of abstract method."""
        async with self.db_session() as session:
            await session.update(IssueEntity).where(
                IssueEntity.uid == model.uid
            ).values(model.dict())
            await session.commit()

    async def delete(self, uid: int):
        """Implementation of abstract method."""
        async with self.db_session() as session:
            await session.delete(IssueEntity).where(IssueEntity.uid == uid)
            await session.commit()

    async def delete_by_project(self, project_uid: int):
        """Implementation of abstract method."""
        async with self.db_session() as session:
            query = delete(IssueEntity).where(IssueEntity.project_id == project_uid)
            await session.execute(query)
            await session.commit()


import pytest

from meltano.core.plugin import PluginRef, PluginType, PluginInstall
from meltano.core.plugin.setting import PluginSetting
from meltano.core.plugin.settings_service import (
    PluginSettingValueSource,
    REDACTED_VALUE,
)

def test_create(session):
    setting = PluginSetting(
        name="api_key.test.test", namespace="gitlab", value="C4F3C4F3", enabled=True
    )

    session.add(setting)
    session.commit()

    fetched = session.query(PluginSetting).first()
    assert setting == fetched

@pytest.fixture(scope="class")
def env_var(plugin_discovery_service, plugin_settings_service):
    def _wrapper(plugin: PluginRef, setting_name):
        plugin_def = plugin_discovery_service.find_plugin(plugin.type, plugin.name)
        setting_def = plugin_settings_service.find_setting(plugin, setting_name)

        return plugin_settings_service.setting_env(setting_def, plugin_def)

    return _wrapper

@pytest.fixture
def subject(session, project_add_service, tap, plugin_settings_service):
    plugin = project_add_service.add("extractors", tap.name)

    return plugin_settings_service

class TestPluginSettingsService:
    def test_get_value(
        self, session, subject, project, tap, env_var, monkeypatch, config_service
    ):
        profile = tap.add_profile("profile", label="Profile")
        config_service.update_plugin(tap)
        tap_with_profile = config_service.find_plugin(tap.name)
        tap_with_profile.use_profile(profile)

        # returns the default value when unset
        assert subject.get_value(session, tap, "test") == (
            "mock",
            PluginSettingValueSource.DEFAULT,
        )
        assert subject.get_value(session, tap_with_profile, "test") == (
            "mock",
            PluginSettingValueSource.DEFAULT,
        )

        # overriden by an PluginSetting db value when set
        setting = subject.set(session, tap, "test", "THIS_IS_FROM_DB")
        setting_with_profile = subject.set(
            session, tap_with_profile, "test", "THIS_IS_FROM_DB_WITH_PROFILE"
        )

        assert subject.get_value(session, tap, "test") == (
            "THIS_IS_FROM_DB",
            PluginSettingValueSource.DB,
        )
        assert subject.get_value(session, tap_with_profile, "test") == (
            "THIS_IS_FROM_DB_WITH_PROFILE",
            PluginSettingValueSource.DB,
        )

        # but only if enabled
        setting.enabled = False
        setting_with_profile.enabled = False
        session.merge(setting)
        session.merge(setting_with_profile)
        session.commit()

        assert subject.get_value(session, tap, "test") == (
            "mock",
            PluginSettingValueSource.DEFAULT,
        )
        assert subject.get_value(session, tap_with_profile, "test") == (
            "mock",
            PluginSettingValueSource.DEFAULT,
        )

        # overriden via the `meltano.yml` configuration
        with project.meltano_update() as meltano:
            extractor = meltano.plugins.extractors[0]
            extractor.config = {"test": 42}
            extractor.get_profile("profile").config = {"test": 43}

        assert subject.get_value(session, tap, "test") == (
            42,
            PluginSettingValueSource.MELTANO_YML,
        )
        assert subject.get_value(session, tap_with_profile, "test") == (
            43,
            PluginSettingValueSource.MELTANO_YML,
        )

        # revert back to the original
        with project.meltano_update() as meltano:
            extractor = meltano.plugins.extractors[0]
            extractor.config = {}
            extractor.get_profile("profile").config = {}

        # overriden via ENV
        subject = subject.with_env_override({env_var(tap, "test"): "N33DC0F33"})

        assert subject.get_value(session, tap, "test") == (
            "N33DC0F33",
            PluginSettingValueSource.ENV,
        )
        assert subject.get_value(session, tap_with_profile, "test") == (
            "N33DC0F33",
            PluginSettingValueSource.ENV,
        )

        # overridden via config override
        subject = subject.with_config_override({"test": "foo"})

        assert subject.get_value(session, tap, "test") == (
            "foo",
            PluginSettingValueSource.CONFIG_OVERRIDE,
        )
        assert subject.get_value(session, tap_with_profile, "test") == (
            "foo",
            PluginSettingValueSource.CONFIG_OVERRIDE,
        )

        # Verify that boolean settings set in env are cast correctly
        subject = subject.with_env_override({env_var(tap, "boolean"): "on"})

        assert subject.get_value(session, tap, "boolean") == (
            True,
            PluginSettingValueSource.ENV,
        )

        subject = subject.with_env_override({env_var(tap, "boolean"): "0"})

        assert subject.get_value(session, tap, "boolean") == (
            False,
            PluginSettingValueSource.ENV,
        )

    def test_as_config_custom(self, subject, session, config_service):
        EXPECTED = {"test": "custom", "start_date": None, "secure": None}
        tap = PluginInstall(
            PluginType.EXTRACTORS,
            name="tap-custom",
            namespace="tap_custom",
            config=EXPECTED,
        )
        config_service.add_to_file(tap)

        assert subject.as_config(session, tap) == EXPECTED

    def test_as_config(self, subject, session, tap):
        EXPECTED = {"test": "mock", "start_date": None, "secure": None}
        full_config = subject.as_config(session, tap)
        redacted_config = subject.as_config(session, tap, redacted=True)

        for k, v in EXPECTED.items():
            assert full_config.get(k) == v
            assert redacted_config.get(k) == v

    def test_as_config_redacted(self, subject, session, tap):
        # ensure values are redacted when they are set
        subject.set(session, tap, "secure", "thisisatest")
        config = subject.as_config(session, tap, redacted=True)

        assert config["secure"] == REDACTED_VALUE

        # although setting the REDACTED_VALUE does nothing
        subject.set(session, tap, "secure", REDACTED_VALUE)
        config = subject.as_config(session, tap)
        assert config["secure"] == "thisisatest"

    def test_as_env(self, subject, session, tap, env_var):
        config = subject.as_env(session, tap)

        assert config.get(env_var(tap, "test")) == "mock"
        assert config.get(env_var(tap, "start_date")) == None
        assert config.get(env_var(tap, "secure")) == None

    def test_unset(self, session, subject, tap):
        # overriden by an PluginSetting db value when set
        setting = subject.set(session, tap, "test", "THIS_IS_FROM_DB")
        assert session.query(PluginSetting).count() == 1

        subject.unset(session, tap, "test")
        assert session.query(PluginSetting).count() == 0

    def test_env_var_substitution(self, session, subject, project, tap):
        with project.meltano_update() as meltano:
            extractor = meltano.plugins.extractors[0]
            extractor.config = {
                "var": "$VAR",
                "foo": "${FOO}",
                "missing": "$MISSING",
                "multiple": "$A ${B} $C",
            }

        env = {
            "VAR": "hello world!",
            "FOO": 42,
            "A": "rock",
            "B": "paper",
            "C": "scissors",
        }

        subject = subject.with_env_override(env)

        config = subject.as_config(session, tap)
        assert config["var"] == env["VAR"]
        assert config["foo"] == str(env["FOO"])
        assert config["missing"] == None
        assert config["multiple"] == "rock paper scissors"


import sys
import os
import csv
import h5py
import numpy as np
sys.path.append('../')
from data_parser import DataParser

class UnitTest:
    _data_path = 'unit_test/data'
    _baseline_path = 'unit_test/baseline'
    _result_path = 'unit_test/results'

    def __init__(self, link):
        self._link = link

    def test_DataParser_parse_sample(self):
        data_parser = DataParser(' ')
        for line in self._link:
            try:
                """ generate results """
                dicom_file_path = os.path.join(self._data_path, line)
                contour_file_path = os.path.join(self._data_path, self._link[line])
                shape, img, mask, contour = data_parser.parse_sample(dicom_file_path, contour_file_path)
                np.savez(os.path.join(self._result_path, os.path.splitext(line)[0]) +'.npz', shape=shape, img=img, mask=mask, contour=contour)

                """ Load baseline and do the comparison """
                baseline = np.load(os.path.join(self._baseline_path, os.path.splitext(line)[0]) +'.npz')
                equal1 = np.array_equal(baseline['shape'], shape)
                equal2 = np.array_equal(baseline['img'], img)
                equal3 = np.array_equal(baseline['mask'], mask)
                equal4 = np.array_equal(baseline['contour'], contour)
                if(not(equal1 and equal2 and equal3 and equal4)):
                    return False
            except:
                return False

        return True

    def test_DataParser_save_sample(self):
        data_parser = DataParser(' ')
        data_parser.set_result_path(self._result_path)
        for line in self._link:
            try:
                """ generate results """
                dicom_file_path = os.path.join(self._data_path, line)
                contour_file_path = os.path.join(self._data_path, self._link[line])
                shape, img, mask, contour = data_parser.parse_sample(dicom_file_path, contour_file_path)
                data_parser.save_sample(os.path.splitext(line)[0], shape, img, mask, contour)

                """ Load baseline and do the comparison """
                with h5py.File(os.path.join(self._baseline_path, 'data.h5'), 'r') as hf:
                    sample = hf[os.path.splitext(line)[0]]
                    equal1 = np.array_equal(np.array(sample['shape']), shape)
                    equal2 = np.array_equal(np.array(sample['img']), img)
                    equal3 = np.array_equal(np.array(sample['mask']), mask)
                if(not(equal1 and equal2 and equal3)):
                    return False
            except:
                return False

        return True

def parseLink(link_file):
    with open(link_file) as f:
        reader = csv.reader(f, delimiter=',')
        link_pairs = list(reader)
        link = {}
        for line in link_pairs:
            if len(line) == 2:
                link[line[0]] = line[1]
        return link

if __name__ == '__main__':
    link = parseLink('./unit_test/link.csv')

    unit_test = UnitTest(link);

    if unit_test.test_DataParser_parse_sample():
        print('Succeed    DataParser::parse_sample()')
    else:
        print('Failed     DataParser::parse_sample()')

    if unit_test.test_DataParser_save_sample():
        print('Succeed    DataParser::save_sample()')
    else:
        print('Failed     DataParser::save_sample()')


import time
import requests

url = "http://www.momoshop.com.tw/search/"

fh = {"user-agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"}

r = requests.get(url + "searchShop.jsp?keyword=HTC", headers=fh)

if r.status_code == requests.codes.ok :
    r.encoding = "big5"
    print (r.text)
else :
    print ("HTTP請求錯誤 :"+ url)


import os
import numpy as np
import torch
import torchvision

import augmentations
from skimage import io

def gpu_check():
    return torch.device('cuda' if torch.cuda.is_available() else 'cpu')

def count_files(dir):
    counter = 0
    for content in os.walk(dir):
        files = content[2]
        counter += len(files)
    return counter

def random_img_split(img):
    h, w = img.shape[0], img.shape[1]
    random_point = np.random.uniform(low=0, high=1)
    h_split, w_split = round(h * random_point), round(w * random_point)

    if h_split - 47 <= 0:
        h_split = h_split + abs(h_split - 48)

    if w_split - 27 <= 0:
        w_split = w_split + abs(w_split - 28)

    new_img = img[h_split - 47:h_split, w_split - 27:w_split]
    return new_img

def combine_images(bg, symb):
    bg_crop = random_img_split(bg)
    augmented_symb = augmentations.symbol_transform(symb).reshape((47, 27))
    augmented_bg_small = augmentations.background_transform(bg_crop).reshape((47, 27))

    bg_small_avg_color = torch.mean(bg_crop)

    if torch.round(bg_small_avg_color) == 0:
        tone_mult = 1

    else:
        tone_mult = 0

    # Actually this one somehow combines these two images
    main_img = (augmented_symb * augmented_bg_small - 0.5) + augmented_symb * (-tone_mult)
    augmented_main_image = augmentations.final_transform(main_img)

    return augmented_main_image

def load_image(path):
    img = torchvision.transforms.ToTensor()(io.imread(path))
    return img


from torchvision import transforms, datasets
from modules import lenet
import torch
from tqdm import tqdm
from torch.utils.tensorboard import SummaryWriter

writer = SummaryWriter()
data_path = '/mnt'
batch_size = 256
learning_rate = 1e-3

data_transform = transforms.Compose([
    transforms.Resize((32,32)),
    transforms.ToTensor(),
])

train_data = datasets.MNIST(data_path, train=True, download=True, transform=data_transform)
val_data = datasets.MNIST(data_path, train=False, download=True, transform=data_transform)

train_loader = torch.utils.data.DataLoader(
    train_data,
    batch_size = batch_size,
    shuffle = True,
    num_workers = 4,
)

val_loader = torch.utils.data.DataLoader(
    val_data,
    batch_size = batch_size,
    num_workers = 4,
)

model = lenet.LeNet().cuda()
criterion = torch.nn.CrossEntropyLoss(reduction='sum')
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=2, eta_min=1e-5)

def accuracy(output, target, topk=(1,)):
    """Computes the accuracy over the k top predictions for the specified values of k"""
    maxk = max(topk)
    batch_size = target.size(0)
    _, pred = output.topk(maxk, 1, True, True)
    pred = pred.t()
    correct = pred.eq(target.reshape(1, -1).expand_as(pred))

    return [correct[:k].reshape(-1).float().sum(0) * 100. / batch_size for k in topk]

def train(epoch, log_rate, loader, model, criterion, optimizer, scheduler):
    model.train()
    step = len(loader) // log_rate
    for it, (input, target) in enumerate(tqdm(loader, leave=False)):
        it = len(loader) * epoch + it
        input = input.cuda()
        target = target.cuda()

        output = model(input)

        loss = criterion(output, target)

        optimizer.zero_grad()
        loss.backward()

        optimizer.step()
        scheduler.step()

        lr = optimizer.param_groups[0]["lr"]
        wd = optimizer.param_groups[0]["weight_decay"]
        if it % step == 0:
            writer.add_scalar("train/loss", loss.item(), it)
            writer.add_scalar("train/lr", lr, it)
            writer.add_scalar("train/wd", wd, it)

    print(f"Epoch: [{epoch}] loss: {loss.item()}, lr: {lr}, wd: {wd}")

def validate(epoch, log_rate, loader, model, criterion):
    model.eval()
    step = len(loader) // log_rate

    acc1_global = 0
    acc5_global = 0
    for it, (input, target) in enumerate(tqdm(loader, leave=False)):
        it = len(loader) * epoch + it
        with torch.no_grad():
            input = input.cuda()
            target = target.cuda()

            output = model(input)

        loss = criterion(output, target)
        acc1, acc5 = accuracy(output, target, topk=(1,5))

        if it % step == 0:
            writer.add_scalar("val/loss", loss.item(), it)
            writer.add_scalar("val/acc1", acc1.item(), it)
            writer.add_scalar("val/acc5", acc5.item(), it)

        acc1_global += acc1.item() / len(loader)
        acc5_global += acc5.item() / len(loader)
    print(f"Epoch: [{epoch}] loss: {loss.item()}, acc1: {acc1.item()}, acc5: {acc5.item()}")

    return acc1_global, acc5_global

def main(epoch, train_loader, val_loader, model, criterion, optimizer, scheduler):
    print(f"Total epoch: {epoch}")
    _acc1 = 0.
    _acc5 = 0.
    validate(-1, 1, val_loader, model, criterion)
    for it in range(epoch):
        train(it, 10, train_loader, model,criterion, optimizer, scheduler)
        acc1, acc5 = validate(it, 10, val_loader, model, criterion)
        if it%1 == 0:
            torch.save(model.state_dict(), f'lenet_mnist_{it}.pth')
            print(f"Save ckpt file: lenet_mnist_{it}.pth")

        if acc1 >= _acc1:
            _acc1 = acc1
            _acc5 = acc5

main(100, train_loader, val_loader, model, criterion, optimizer, scheduler)


from os import listdir
from os.path import isfile,join
import cv2

path = '/home/limon/workspace/datasets/trafficLabels/'
Read_path = path + 'labels/'
img_path = path + 'images/'

for f in listdir(Read_path):
    if isfile(join(Read_path, f)):
        name,ext = f.split('.')
        img = cv2.imread(img_path + name + '.jpg')
        h,w,_ = img.shape
        print(name)
        f = open(Read_path+f, "r")
        lines = f.readlines()
        for line in lines:
            data = line.split(",")
            obj = data[0]
            x1 = int(data[1])
            y1 = int(data[2])
            x2 = int(data[3])
            y2 = int(data[4])
            cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),3)

        f.close()
        cv2.imshow('image',img)
        k = cv2.waitKey(0)
        if k == 27:         # wait for ESC key to exit
            exit()
        elif k == ord('s'):
            cv2.destroyAllWindows()


import scrapy
#import requests
import re

class EG(scrapy.Spider):
    name = "eg1"
    start_urls = ['https://www.shoppersstop.com/haute-curry-womens-tie-up-neck-printed-churidar-suit/p-203566837']

    def parse(self, response):
        import pdb;pdb.set_trace()
        cookies = response.headers.get('Set-Cookie','')
        j_session = ''.join(re.findall('JSESSIONID=(.*?) \Path',cookies))
        j_session = j_session.replace(';','')
        csrf_token = ''.join(response.xpath('//div[@class="user-container user-icons log-member"]/input[@id="ajaxCSRF"]/@value').extract())
        cookies={}
        cookie = literal_eval(json.loads(json.dumps(str(headers)))).get('Set-Cookie', [])
        for i in cookie:
            data_key  = i.split(';')[0]
            if data_key:
                try: key, val = data_key.split('=', 1)
                except : continue
                cookies.update({key.strip():val.strip()})
        headers = {
            'origin': 'https://www.shoppersstop.com',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'en-US,en;q=0.9,te;q=0.8',
            'x-requested-with': 'XMLHttpRequest',
            'save-data': 'on',
            'pragma': 'no-cache',
            'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
            'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'accept': '*/*',
            'cache-control': 'no-cache',
            'authority': 'www.shoppersstop.com',
            'referer': 'https://www.shoppersstop.com/haute-curry-womens-tie-up-neck-printed-churidar-suit/p-203566837',
        }

        data = {
          'qty': '1',
          'baseProductCode': '203566837',
          'productCodePost': '203566842',
          'CSRFToken': csrf_token,
        }
        response = requests.post('https://www.shoppersstop.com/cart/add', headers=headers, data=data)


import torch
import torch.nn as nn
from math import sqrt

'''
Pytorch 使用nn.Module定义模型的方法
1.必须继承nn.Module这个类，要让PyTorch知道这个类是一个Module
2.在init(self)中设置好需要的"组件"(如conv,pooling,Linear,BatchNorm等)
3.最后，在forward(self,x)中定义好的“组件”进行组装，就像搭积木，把网络结构搭建出来，这样一个模型就定义好了
'''

# 中间层
class Conv_ReLU_Block(nn.Module):
    def __init__(self):
        super(Conv_ReLU_Block, self).__init__()
        self.conv = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1, bias=False)
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        return self.relu(self.conv(x))

class VDSR(nn.Module):
    def __init__(self):
        super(VDSR, self).__init__()  # 找到Net的父类，并将其初始化。这里继承的是nn.Module类

        # 创建中间层 中间层有18个block,每个block为 Conv + Relu
        self.residual_layer = self.make_layer(Conv_ReLU_Block, 18)

        # 输入的第一个卷积层。 不使用bias,并且先扩大一圈保证输出还是原图大小。  输入为灰度图
        self.input_conv = nn.Conv2d(in_channels=1, out_channels=64, kernel_size=3, stride=1, padding=1, bias=False)

        # 输出卷积层
        self.output_conv = nn.Conv2d(in_channels=64, out_channels=1, kernel_size=3, stride=1, padding=1, bias=False)
        self.relu = nn.ReLU(inplace=True)  # inplace为True，将会改变输入的数据 ，否则不会改变原输入，只会产生新的输出

        for m in self.modules():  # self.modules(), 他会返回该网络中的所有modules.
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels  # k^2 * c
                m.weight.data.normal_(0, sqrt(2. / n))  # He的方法，权值初始化

    def make_layer(self, block, num_of_layer):
        layers = []
        for _ in range(num_of_layer):
            layers.append(block())
        return nn.Sequential(*layers)  # 模块将按照构造函数中传递的顺序添加到模块中

    def forward(self, x):
        ILR = x  # 这里的residual应该是原图
        hidden = self.relu(self.input_conv(x))  # 第一层
        hidden = self.residual_layer(hidden)  # 中间层
        out = self.output_conv(hidden)  # 输出层，无激活函数
        out = torch.add(out, ILR)  # 预测的残差结果和原图相加
        return out


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

class MAtt(nn.Module):
    def __init__(self, N=5, K=1):
        super(MAtt, self).__init__()
        self.N = N
        self.K = K

    def forward(self, fspt, fqry):
        # fspt: [B, N*K, C, H, W]
        # fqry: [B, N*num_query, C, H, W]

        b, nk, c, h, w = fspt.size()
        nq = fqry.size(1)
        n = self.N

        prototype = fspt.view(b, self.N, self.K, c, h, w).mean(dim=2) # [B, N, C, H, W]
        prototype = prototype.view(b, n, c, -1).unsqueeze(1) # [B, 1, N, C, H*W]
        fqry = fqry.view(b, nq, c, -1).unsqueeze(2) # [B, N*num_query, 1, C, H*W]

        prototype_norm = F.normalize(prototype, p=2, dim=3).transpose(3, 4) # [B, 1, N, H*W, C]
        fqry_norm = F.normalize(fqry, p=2, dim=3) # [B, N*num_query, 1, C, H*W]

        correlation_mat = torch.matmul(prototype_norm, fqry_norm) # [B, N*num_query, N, H*W, H*W]

        ap = correlation_mat.mean(dim=4, keepdim=True).transpose(3, 4) # [B, N*num_query, N, 1, H*W]
        ap = F.softmax(ap/0.25, dim=4) + 1
        aq = correlation_mat.mean(dim=3, keepdim=True) # [B, N*num_query, N, 1, H*W]
        aq = F.softmax(aq/0.25, dim=4) + 1

        prototype_adp = (prototype*ap).view(b, nq, n, c, h, w)
        fqry_adp = (fqry*aq).view(b, nq, n, c, h, w)

        return prototype_adp, fqry_adp


import sys
import os
import re
from threading import Thread
try:
    from IPython.kernel.zmq.kernelapp import IPKernelApp
    ZMQ = True
except:
    # had trouble finding kernel.zmq.  Try:
    #   $ pip install -U ipython pyzmq
    ZMQ = False
try:
    import gdb
except:
    print("Either your gdb is not > gdb 7")
    print("Or you are trying to run this without gdb")
    print("Exiting . . .")
    sys.exit(1)

if not "ATP" in os.environ:
    print(os.environ)
    print("Must define ATP breakpoint locations. Exiting . . .")
    sys.exit(1)
else:
    # define dua and atp break point locations
    # must be in format of gdb break points (ie, file : line, *address, symbol_name)
    atp_loc = os.environ['ATP']

BUG_EFFECT_COUNT = 0
SIG_EVENT_COUNT = None
record_regex = re.compile(".*?Log contains ([0-9]+) instructions\..*",
                          re.MULTILINE)
event_regex = re.compile(".*?Current event: ([0-9]+).*", re.MULTILINE)

def get_instr_count():
    data = gdb.execute("info record", to_string=True)
    m = re.search(record_regex, data)
    if m == None:
        print("coulnd't find instruction count in [info record] command")
        print(data)
    return int(m.groups()[0])

def get_event_count():
    data = gdb.execute("when", to_string=True)
    m = re.search(event_regex, data)
    if m == None:
        print("coulnd't find event count in when command")
        print(data)
        assert 0
    return int(m.groups()[0])

# bp_num is int
def launch_debug_using_ipython():
    # run this from a gdb session in order to create a
    # ipython session one could connect to for gdb_python symbol
    # symbol support
    import IPython
    if not ZMQ:
        IPython.embed()
    else:
        IPython.embed_kernel()
        """
        After this you will see:
            NOTE: When using the `ipython kernel` entry point, Ctrl-C will not work.

            To exit, you will have to explicitly quit this process, by either
            sending
            "quit" from a client, or using Ctrl-\ in UNIX-like environments.

            To read more about this, see
            https://github.com/ipython/ipython/issues/2049

            To connect another client to this kernel, use:
                    --existing kernel-138767.json
        To connect to this ipython kernel, from another terminal on same machine
        type:
            $ ipython console --existing kernel-138767.json
        Note that the kernel number will change with each run of the debugging
        application
        To exit, type quit in the ipython terminal, which will give you control
        of the gdb session
        """

def get_bp_hits(bp_num):
    data = gdb.execute("info b {}".format(bp_num), to_string=True)
    hit_str = "breakpoint already hit "
    if not hit_str in data:
        return 0
    else:
        return int(data.split(hit_str)[1].split()[0])

EXIT_LOC = "exit"

class ATP_Breakpoint(gdb.Breakpoint):
    def stop(self):
        global SIG_EVENT_COUNT
        # if (gdb.execute("info record", to_string=True) ==
            # "No record target is currently active.\n"):
            # gdb.write("Starting recording process")
        # else:
            # gdb.write("Hit ATP again.  Restarting . . .")
            # gdb.execute("record stop")
        # gdb.execute("record full")
        gdb.execute("when")
        print("!! Hit ATP !!")
        # print "Instruction Count =", SIG_EVENT_COUNT - get_event_count()
        print("Events =", SIG_EVENT_COUNT, get_event_count())
        gdb.execute("q")
        sys.exit(0)
        return True

class Exit_Breakpoint(gdb.Breakpoint):
    def stop(self):
        ret_data = gdb.execute("info arg", to_string=True)
        ret_code = int(ret_data.split(" = ")[1])
        print("At program exit normal with status: {}".format(ret_code))
        print("Instruction Count = {}".format(get_instr_count()))
        gdb.execute("when")
        # print "Instruction Count = {}".format(get_instr_count())
        ATP_Breakpoint(atp_loc)
        # gdb.execute("set scheduler-locking on")
        gdb.post_event(lambda s: gdb.execute("reverse-continue"))
        gdb.post_event(lambda s: gdb.execute("reverse-continue"))
        # gdb.execute("set scheduler-locking off")
        # gdb.execute("reverse-continue")
        # gdb.execute("q")
        # sys.exit(1)

# class GdbCommand():
    # # def __init__(self, cmd):
        # # self.cmd = cmd
    # def (self):
        # cmd = self.args[0]
        # # gdb.execute(self.cmd)
        # # gdb.write("GDB MESSAGE")
        # print "HERE HERE"
        # print "About to execute: [{}]".format(self.cmd)
        # gdb.execute(self.cmd)

def event_handler (event):
    def handle_sig_event (event):
        if isinstance(event, gdb.SignalEvent):
            if event.stop_signal in ["SIGSEGV", "SIGABRT"]:
                print("Found a SIG {}".format(event.stop_signal))
                print(gdb.execute("p $_siginfo._sifields._sigfault.si_addr",
                            to_string=True))
                # print gdb.execute("info proc mappings", to_string=True)
                gdb.execute("when")
                # print "Instruction Count = {}".format(get_instr_count())
                ATP_Breakpoint(atp_loc)
                def print_fn(s):
                    print(s)
                    return True
                # gdb.post_event(lambda s: print_fn("hello") and gdb.execute("reverse-continue"))
                # gdb.post_event(lambda s: print_fn("hello2") and gdb.execute("reverse-continue"))
                # gdb.post_event(lambda s: print_fn("hello2") and gdb.execute("reverse-continue"))
                # gdb.execute("set scheduler-locking on")
                # gdb.post_event(lambda s: gdb.execute("reverse-continue"))
                # gdb.post_event(lambda s: gdb.execute("reverse-continue"))
                # gdb.execute("set scheduler-locking off")
                try:
                    global SIG_EVENT_COUNT
                    if SIG_EVENT_COUNT == None:
                        SIG_EVENT_COUNT = get_event_count()
                        print("SIG_EVENT_COUNT: {}".format(SIG_EVENT_COUNT))
                    gdb.execute("reverse-continue")
                    gdb.execute("reverse-continue")
                except gdb.error:
                    pass
                # gdb.execute("reverse-continue")
                # gdb.execute("q")
                # sys.exit(1)
            else:
                # print "Instruction Count = {}".format(get_instr_count())
                print("Reached unhandled signal event: {}".format(event.stop_signal))
                print("Exiting . . .")
                gdb.execute("q")
                sys.exit(1)

    # generic StopEvent handler.  We will assume that we only get here from gdb
    # def handle_stop_event (event):
        # if isinstance(event, gdb.StopEvent):
            # global BUG_EFFECT_COUNT
            # BUG_EFFECT_COUNT += 1
            # print "HANDLING STOP EVENT: STEP COUNT {}".format(BUG_EFFECT_COUNT)
            # if BUG_EFFECT_COUNT > 100000:
                # print "Instruction Count = {}".format(BUG_EFFECT_COUNT)
                # gdb.execute("q")
            # else:
                # # gdb.post_event(run_gdb_si)
                # print "posting an event a si thread from stop event"
                # run_gdb_si()
                # # thread = Thread(target=run_gdb_si)
                # # thread.start()

    if isinstance(event, gdb.SignalEvent):
        handle_sig_event(event)
    # assume we get here from beginning of rr thread stop point
    elif isinstance(event, gdb.StopEvent):
        gdb.execute("c")
        # pass
        # handle_stop_event(event)

gdb.execute("set breakpoint pending on", to_string=True)
gdb.execute("set pagination off", to_string=True)
# gdb.execute("set logging on", to_string=True)
# gdb.execute("set logging file /nas/ulrich/mygdb.log", to_string=True)
# gdb.execute("set verbose off", to_string=True)
gdb.execute("set confirm off", to_string=True)
gdb.execute("set width 0", to_string=True)
gdb.execute("set height 0", to_string=True)
# maybe this will redirect program output to dev/null
gdb.execute("tty /dev/null", to_string=True)

# gdb.execute("break " + atp_loc, to_string=True)
# ATP_Breakpoint(atp_loc)
# set breakpoints on normal exit of program and SEGFAULTS
Exit_Breakpoint(EXIT_LOC)
# gdb.execute("break " + EXIT_LOC, to_string=True)
# gdb.execute("handle SIGSEGV stop")
gdb.execute("handle all stop", to_string=True)

# establish callback on breakpoints
gdb.events.stop.connect(event_handler)

# uncomment this line of code to trigger an ipython kernel session that you can
# console into.  See launch_debug_using_ipython() for more info
# gdb.execute("c")
# import IPython; IPython.embed_kernel()
# gdb.execute("si")


'''
    plot some Twitter Places on a Folium map
'''
import csv
import math
import pandas as pd
import os.path

# Map related:
import folium

import branca.colormap as cm

from folium.plugins import FloatImage
url = 'img/accu-snow-weather-sm.jpg'

# data set-up:
locs = 'places_grouped.csv' # lats longs and names
dfin = pd.read_csv(locs)
df = dfin.set_index(['_id'])
all = df.index.tolist()
# print(df.head(3))
# print(df.columns)
# print(df.describe())
with_states = df['place.0.name'].str.contains('\s[A-Z][A-Z]$', na=False, regex=True)
num_states = with_states.sum()

# states = df[df[1].str.extract('\s[A-Z][A-Z]$')].sum()

# MAPS and their params
filename = os.path.basename(__file__)
output_osm = filename.split('.')[0] + '.html'
zoom_start = 7
centre = [df['place.0.bounding_box.coordinates.0.0.1'].mean(), df['place.0.bounding_box.coordinates.0.1.0'].mean()] # Pennsylvania-ish
df = df.sort_values(['place.0.place_type'], ascending=[True])

# Setup the map:
fmap = folium.Map(location=centre, max_zoom=30, zoom_start=zoom_start, control_scale=True, tiles="stamenterrain")

# box styles
styles = {'opacity' : 0.2, 'color' : '#323232', 'fill_color':'red' }
threshold = 5000 # max no of count we will allow
adjust_scale_color = 100
linear = cm.LinearColormap(['green', 'yellow', 'red'], vmin=1, vmax=round(threshold/adjust_scale_color)) # 'yellow',
linear.caption = 'Tweet intensity (scaled)'

# for point marker size
radius = 10

# different place_types for Places.
colors = {'admin' : 'black', 'city' : 'purple' , 'poi' : '#318633' , 'neighborhood' : 'blue', 'country' : 'white'}
idx = 0
remove = ['country']

feature_groups = colors.copy()
# make this have a group for each place_type so they can all be toggled.
for key, value in feature_groups.items():
    if key not in remove:
        feature_groups[key] = folium.FeatureGroup(name='enable place_type: ' + key)

marker_cluster = folium.MarkerCluster("enable place_type: point-of-interest").add_to(fmap)

# fixme - will calculate the color based on the count (tweet intensity)
for row in df.itertuples():
    idx += 1

    # popup text
    index, count, name, place_type = row[:4]

    # skip some values that just screw up the map
    if (not isinstance(place_type, str)) or (not isinstance(name, str)) or\
     (place_type in remove) or (name[-6:] == 'Canada') or (count > threshold):
        continue # i.e. omit all these ones

    # bounding box corners for every Place
    bottom_left = list()
    top_right = list()

    # boxes coords:
    bottom_left.append(row[5])
    bottom_left.append(row[4])
    top_right.append(row[9])
    top_right.append(row[8])

    # now build markers for map and their popups:
    string = '%s: %d tweets. Type:%s' % (name, count, place_type)
    if bottom_left[0] != top_right[0]:
        marker = folium.features.RectangleMarker(
            bounds=[bottom_left, top_right],
            color=colors[place_type],
            fill_color=linear(count),
            fill_opacity=styles['opacity'],
            popup=string)
    else:
        # icon=folium.Icon(color='red', icon='info-sign')
        # sometimes the Places are point coordinates!
        marker = folium.RegularPolygonMarker(
            location=bottom_left,
            radius=radius,
            color=colors[place_type],
            weight=0,
            fill_color=linear(count),
            number_of_sides=6,
            popup=string)

    if(place_type == 'poi'):
        marker.add_to(marker_cluster)
    else:
        feature_groups[place_type].add_child(marker)

fmap.add_child(linear)
for key, feature_group in feature_groups.items():
    if (key not in remove) and (key is not 'poi'):
        fmap.add_child(feature_group)
# folium.TileLayer('openstreetmap').add_to(fmap)
folium.TileLayer('Cartodb Positron').add_to(fmap)
folium.LayerControl(collapsed=False).add_to(fmap)

# todo: add in choro layer with states totals as per:
# https://nbviewer.jupyter.org/github/python-visualization/folium/blob/master/examples/Quickstart.ipynb
# need to make a df from States from full_name last letters to do this.
# output both maps
FloatImage(url, bottom=3, right=1).add_to(fmap)
fmap.save(output_osm)


from collections import namedtuple

from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.db.models import Q
from django.dispatch import receiver
from morango.models.core import Store

from kolibri.core.auth.models import AbstractFacilityDataModel

ContentAssignment = namedtuple(
    "ContentAssignment", ["contentnode_id", "source_model", "source_id", "metadata"]
)

DeletedAssignment = namedtuple("ContentAssignment", ["source_model", "source_id"])

# a global registry of usages of the `ContentAssignmentManager` so we can find models where it
# was implemented and gather their assignments
CONTENT_ASSIGNMENT_MANAGER_REGISTRY = {}

class ContentAssignmentManager(object):
    """
    The ContentAssignmentManager is attached to a model, similar to a Django manager, but it's
    purpose is to allow introspection of changes to the model that occur during a sync, and to
    generate 'assignments' that are translated into `ContentRequest`s
    """

    __slots__ = (
        "model",
        "name",
        "one_to_many",
        "filters",
        "lookup_field",
        "lookup_func",
    )

    def __init__(
        self, one_to_many=False, filters=None, lookup_field=None, lookup_func=None
    ):
        """
        :param one_to_many: indicating that the associated model maps to multiple content nodes
        :type one_to_many: bool
        :param filters: dict of QuerySet filters to apply for determining validity of assignments
        :type filters: dict
        :param lookup_field: the model field which maps the content node
        :type lookup_field: str
        :param lookup_func: a function which receives the value of `lookup_field` to return nodes
            and metadata
        :type lookup_func: callable<tuple[contentnode_id: str, metadata: None|dict]>
        """
        self.model = None
        self.name = None
        self.one_to_many = one_to_many
        self.filters = filters
        self.lookup_field = lookup_field
        self.lookup_func = lookup_func

    def contribute_to_class(self, model, attribute_name):
        """
        This is a special method associated with Django. When an instance of this class is
        set on a Django model, it will call this method, which gives the ability to make the
        association with the model

        :type model: AbstractFacilityDataModel
        :type attribute_name: str
        """
        if not issubclass(model, AbstractFacilityDataModel):
            raise ImproperlyConfigured(
                "{} is only valid on AbstractFacilityDataModels".format(
                    self.__class__.__name__
                )
            )

        if self.one_to_many and self.lookup_func is None:
            raise ImproperlyConfigured(
                "One-to-many assignment models need a lookup function"
            )

        self.model = model
        self.name = attribute_name
        setattr(model, attribute_name, self)
        CONTENT_ASSIGNMENT_MANAGER_REGISTRY.update({id(model): self})

    @classmethod
    def on_any_downloadable_assignment(cls, callable_func):
        """
        Connects the provided callable to the post_save signal of all the associated models.
        Executes the callable with any downloadable assignments from the model instances
        if they match the filters (if defined).

        :param callable_func: The callable function to be executed with the new assignments.
        :type callable_func: callable
        """
        for manager in CONTENT_ASSIGNMENT_MANAGER_REGISTRY.values():
            manager.on_downloadable_assignment(callable_func)

    @classmethod
    def on_any_removable_assignment(cls, callable_func):
        """
        Connects the provided callable to the post_save signal of all associated models.
        Executes the callable with any removable assignments from the model instances
        if they match the filters (if defined).

        :param callable_func: The callable function to be executed with the new assignments.
        :type callable_func: callable
        """
        for manager in CONTENT_ASSIGNMENT_MANAGER_REGISTRY.values():
            manager.on_removable_assignment(callable_func)

    @classmethod
    def find_all_downloadable_assignments(
        cls, dataset_id=None, transfer_session_id=None
    ):
        """
        :param dataset_id: optional argument to filter assignments by dataset
        :param transfer_session_id:
        :rtype: list of ContentAssignment
        """
        if (dataset_id is None) == (transfer_session_id is None):
            raise ValueError(
                "One parameter needs specified: dataset_id and transfer_session_id"
            )

        for manager in CONTENT_ASSIGNMENT_MANAGER_REGISTRY.values():
            for assignment in manager.find_downloadable_assignments(
                dataset_id, transfer_session_id
            ):
                yield assignment

    @classmethod
    def find_all_removable_assignments(cls, dataset_id=None, transfer_session_id=None):
        """
        :param dataset_id: optional argument to filter assignments by dataset_id
        :param transfer_session_id:optional argument to filter assignments by transfer_session_id
        :rtype: list of ContentAssignment or DeletedAssignment
        """
        if (dataset_id is None) == (transfer_session_id is None):
            raise ValueError(
                "One parameter needs specified: dataset_id and transfer_session_id"
            )

        for manager in CONTENT_ASSIGNMENT_MANAGER_REGISTRY.values():
            for assignment in manager.find_removable_assignments(
                dataset_id, transfer_session_id
            ):
                yield assignment

    def _get_modified_store(self, transfer_session_id):
        """
        Queryset for finding the Store records affected by a sync
        """
        return Store.objects.filter(
            model_name=self.model.morango_model_name,
            last_transfer_session_id=transfer_session_id,
        )

    def _get_assignments(self, model_qs):
        """
        Shared method that iterates of a model queryset, finding `lookup_field` and calling
        `lookup_func` if necessary for each result

        :param model_qs: The queryset to find `lookup_field` and sources
        :type model_qs: django.db.models.QuerySet
        :rtype: list of ContentAssignment
        """
        for source_id, lookup_field_value in model_qs.values_list(
            "id", self.lookup_field
        ):
            # avoid duplicate contentnode_ids for same source
            contentnode_ids = set()
            assignments = []

            # when one_to_many, we could get more than one contentnode assignment per source
            if self.one_to_many:
                assignments.extend(self.lookup_func(lookup_field_value))
            elif self.lookup_func is not None:
                assignments.append(self.lookup_func(lookup_field_value))
            else:
                assignments.append((lookup_field_value, None))

            for contentnode_id, metadata in assignments:
                if contentnode_id not in contentnode_ids:
                    yield ContentAssignment(
                        contentnode_id,
                        self.model.morango_model_name,
                        source_id,
                        metadata,
                    )
                    contentnode_ids.update([contentnode_id])

    def on_downloadable_assignment(self, callable_func):
        """
        Connects the provided callable to the post_save signal of the attached model.
        Executes the callable with the downloadable assignments from the model instance
        if it matches the filters (if defined).

        :param callable_func: The callable function to be executed with the new assignments.
        :type callable_func: callable
        """
        # since this is a local function, we need use `weak=False` to prevent garbage collection
        @receiver(models.signals.post_save, sender=self.model, weak=False)
        def on_save(sender, instance, **kwargs):
            queryset = self.model.objects.filter(pk=instance.pk)
            if self.filters:
                queryset = queryset.filter(**self.filters)
            if queryset.exists():
                assignments = self._get_assignments(queryset)
                callable_func(instance.dataset_id, assignments)

    def on_removable_assignment(self, callable_func):
        """
        Connects the provided callable to the post_save signal of the associated models.
        Executes the callable with the removable assignments from the model instance
        if it matches the filters (if defined).

        :param callable_func: The callable function to be executed with the new assignments.
        :type callable_func: callable
        """
        # since these are local functions, we need use `weak=False` to prevent garbage collection
        @receiver(models.signals.post_save, sender=self.model, weak=False)
        def on_save(sender, instance, **kwargs):
            queryset = self.model.objects.filter(pk=instance.pk)
            if self.filters:
                queryset = queryset.exclude(**self.filters)
            if queryset.exists():
                assignments = self._get_assignments(queryset)
                callable_func(instance.dataset_id, assignments)

        @receiver(models.signals.post_delete, sender=self.model, weak=False)
        def on_delete(sender, instance, **kwargs):
            callable_func(
                instance.dataset_id,
                [DeletedAssignment(self.model.morango_model_name, instance.pk)],
            )

    def find_downloadable_assignments(self, dataset_id=None, transfer_session_id=None):
        """
        :param dataset_id: optional dataset_id to filter records by
        :param transfer_session_id: optional transfer_session_id to filter records by transfer_session_id
        :return: yields ContentAssignment tuples
        :rtype: list of ContentAssignment
        """
        if (dataset_id is None) == (transfer_session_id is None):
            raise ValueError(
                "One parameter needs specified: dataset_id and transfer_session_id"
            )

        model_qs = self.model.objects.all()
        if transfer_session_id:
            modified_store = self._get_modified_store(transfer_session_id).exclude(
                Q(deleted=True) | Q(hard_deleted=True) | ~Q(deserialization_error="")
            )
            model_qs = model_qs.filter(
                pk__in=modified_store.values_list("id", flat=True)
            )
        elif dataset_id:
            model_qs = model_qs.filter(dataset_id=dataset_id)

        if self.filters:
            model_qs = model_qs.filter(**self.filters)

        for assignment in self._get_assignments(model_qs):
            yield assignment

    def find_removable_assignments(self, dataset_id=None, transfer_session_id=None):
        """
        :param dataset_id: the ID of the dataset to filter records by dataset_id
        :param transfer_session_id: the ID of the transfer session to filter records by transfer_session_id
        :return: yields ContentAssigment or DeletedAssignment tuples
        :rtype: list of ContentAssignment or DeletedAssignment
        """
        if (dataset_id is None) == (transfer_session_id is None):
            raise ValueError(
                "One parameter needs specified: dataset_id and transfer_session_id"
            )

        model_qs = self.model.objects.all()
        if transfer_session_id:
            modified_store = self._get_modified_store(transfer_session_id)
            model_qs = model_qs.filter(
                pk__in=modified_store.values_list("id", flat=True)
            )

            # models that were deleted
            deleted_qs = modified_store.filter(Q(deleted=True) | Q(hard_deleted=True))
            for source_id in deleted_qs.values_list("id", flat=True):
                yield DeletedAssignment(self.model.morango_model_name, source_id)
        elif dataset_id:
            model_qs = model_qs.filter(dataset_id=dataset_id)

        if self.filters:
            # modified models that do not match filters
            model_qs = model_qs.exclude(**self.filters)

        for assignment in self._get_assignments(model_qs):
            yield assignment


"""BazaFilmowa3 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

from filmy import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index),
    path('osoby/', views.show_osoby),
    path('osoby/<int:pk>/', views.osoba_detail),
    path('add_osoba/', views.add_osoba),
    path('filmy/', views.show_filmy),
    path('add_filmy/', views.add_filmy),
    path('film/<int:pk>/', views.film_detail),
    path('film/<int:film_pk>/add_actor/', views.AddActorToMovie.as_view()),
    path('add_genre/', views.add_genre ),
    path('genre/', views.genre),
    path('delete_osoby/<int:pk>/', views.delete_osoby ),
    path('set_session/', views.SessionFunView.as_view()),
    path('show_session/', views.ShowSession.as_view()),
    path('cookiemonster/', views.CookieeMonsterFunView.as_view()),
    path("delete_cookie/",views.DeleteCookiee.as_view())
]


import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import os

def get_Current_Path(file_target):
    current_directory = os.getcwd()
    port_path = current_directory.replace("\\", "/")
    real_path = port_path+file_target
    #real_path = file_path.replace("\\", "/")
    return real_path

# Set up
# cred = credentials.Certificate("C:/Users/Supakij/Documents/GitHub/testbase_back-end/backend/serviceAccountKey.json")
cred = credentials.Certificate(get_Current_Path("/backend/database/serviceAccountKey.json"))
firebase_admin.initialize_app(cred)

db = firestore.client()

# Add a document to the collection
# Replace "my-collection" with the desired collection name
data = ({'basicsubject':'test','coursecode':533433,'coursename':'null','credit':'null','teachername':'null','test':'test'})
db.collection('Subject test').add(data)

# Close the Firebase Admin SDK when done
firebase_admin.delete_app(firebase_admin.get_app())


import os
import asyncio
from gear import Database, transaction

async def main():
    scope = os.environ['HAIL_SCOPE']

    if scope in ('deploy', 'dev'):
        standing_worker_max_idle_time_secs = 7200  # 2 hours
    else:
        standing_worker_max_idle_time_secs = 300  # 5 minutes

    db = Database()
    await db.async_init()

    columns = [
        ('inst_colls', 'max_new_instances_per_autoscaler_loop', 'INT', 10),  # n * 16 cores / 15s = excess_scheduling_rate/s = 10/s => n ~= 10
        ('inst_colls', 'autoscaler_loop_period_secs', 'INT', 15),
        ('inst_colls', 'worker_max_idle_time_secs', 'INT', 30),
        ('pools', 'standing_worker_max_idle_time_secs', 'INT', standing_worker_max_idle_time_secs),
        ('pools', 'job_queue_scheduling_window_secs', 'INT', 150),  # 2.5 minutes is approximately worker start up time
    ]

    @transaction(db)
    async def insert_and_update(tx):
        for table, col, typ, value in columns:
            await tx.just_execute(f'ALTER TABLE {table} ADD COLUMN {col} {typ}, ALGORITHM=INSTANT;')
            await tx.execute_update(f'UPDATE {table} SET {col} = {value};')
            await tx.just_execute(f'ALTER TABLE {table} MODIFY COLUMN {col} {typ} NOT NULL;')

    await insert_and_update()  # pylint: disable=no-value-for-parameter

    await db.async_close()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())


#!/usr/bin/env python3
""" Plotting Task 6-bars"""
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(5)
fruit = np.random.randint(0, 20, (4, 3))

plt.bar(["Farrah", "Fred", "Felicia"],
        fruit[:][0], color='red',
        label='apples', width=0.5)
plt.bar(["Farrah", "Fred", "Felicia"],
        fruit[:][1], bottom=fruit[0],
        color='yellow', label='bananas', width=0.5)
plt.bar(["Farrah", "Fred", "Felicia"],
        fruit[:][2],
        bottom=fruit[0] + fruit[1],
        color='#ff8000', label='oranges', width=0.5)
plt.bar(["Farrah", "Fred", "Felicia"],
        fruit[:][3],
        bottom=fruit[0] + fruit[1] + fruit[2],
        color='#ffe5b4', label='peaches', width=0.5)
plt.title("Number of Fruit per Person")
plt.yticks(range(0, 81, 10))
plt.ylabel("Quantity of Fruit")
plt.legend()
plt.show()


import requests
from pathlib import Path
from bs4 import BeautifulSoup
import bs4
from docx import Document
from docx.shared import RGBColor
from docx.shared import Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from ebooklib import epub
import re

# def saveToTxt(contents:list):
#     fileName='唐诗三百首.txt'
#     with open(fileName,'a',encoding='utf-8') as f:
#         print('写入中...'+contents[0])
#         for rowContent in contents:
#             f.write(rowContent+'\n')

def getHtmlText(url:str)->str:
    try:
        simBrowser={
            'User-Agent':'Chrome/114.0.5735.106',
            'Accept-Encoding': 'gzip, deflate',
            'Accept': '*/*',
            'Connection': 'keep-alive'
        }
        requests.url
        r=requests.get(url,headers=simBrowser,timeout=30)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except:
        return str()

def getPoetryContent(soup:BeautifulSoup,contentList:list,string:str):
    for br_tag in soup.find_all('br'):
        br_tag.replace_with('\n')
    for tag in soup.find_all(id='contson'+string):
        content=tag.text.strip()
        content=re.sub(r'(\(|（).*?(\)|）)','',content)
        contentList.append(content)
    return contentList

def clearDocx(fileName:str):
    path=Path(fileName)
    if path.exists():
        doc=Document(fileName)
        for element in doc.element.body:
            doc.element.body.remove(element)
        doc.save(fileName)

def addEmptyLine(doc:Document):
    text=doc.add_paragraph()
    format=text.paragraph_format
    #format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    format.line_spacing = 1.0  # 设置为1.0表示单倍行距
    format.space_before = 0
    format.space_after = format.space_before

def paragraphCommonFormat(run,format):
    run.font.name = 'SimSun'
    run.font.color.rgb = RGBColor(0, 0, 0)
    format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    format.line_spacing = 1.0  # 设置为1.0表示单倍行距
    format.space_before = 0
    format.space_after = format.space_before

def setDocxTitle(fileName:str,string:str,rank:int):
    path=Path(fileName)
    doc:Document=Document()
    if path.exists():
        doc=Document(fileName)
    title=doc.add_paragraph(string,'Heading'+str(rank))
    titleRun=title.runs[0]
    titleParagraph_format = title.paragraph_format
    paragraphCommonFormat(titleRun,titleParagraph_format)
    #addEmptyLine(doc)
    doc.save(fileName)

def writeToDocx(fileName:str,contents:list):
    path=Path(fileName)
    doc:Document=Document()
    if path.exists():
        doc=Document(fileName)
    pos=0
    for content in contents:
        paragraph=None
        if pos==0:
            paragraph=doc.add_paragraph(content,'Heading3')
        # elif pos==1:
        #     paragraph=doc.add_paragraph(content,'Heading4')
        else:
            paragraph=doc.add_paragraph(content)
        textRun=paragraph.runs[0]
        Paragraph_format = paragraph.paragraph_format
        paragraphCommonFormat(textRun,Paragraph_format)
        pos+=1
    #addEmptyLine(doc)
    doc.save(fileName)

def saveToDocx(htmlText:str):
    bookName='唐诗三百首'
    saveFileName='/home/linux/Documents/books/{}.docx'.format(bookName)
    clearDocx(saveFileName)
    #setDocxTitle(saveFileName,bookName,1)
    subTitlePos=0
    regex = re.compile(r'(\(|（).*?(\)|）)')
    for div in BeautifulSoup(htmlText,'html.parser').find_all(attrs='typecont'):
        subTitlePos+=1
        setDocxTitle(saveFileName,str(subTitlePos)+'、'+div.div.text,1)
        for tag in div(name='span'):
            poetryName=tag.text.strip()
            author=regex.search(poetryName)
            poetryName=regex.sub('',poetryName)
            contentList=[]
            contentList.append(poetryName)
            if(author):
                contentList.append(author.group(0))
            newUrl='https://so.gushiwen.cn'+tag.a.get('href')
            getPoetryContent(BeautifulSoup(getHtmlText(newUrl),'html.parser'),contentList,newUrl[-17:-5])
            writeToDocx(saveFileName,contentList)
            print('爬取中...'+tag.text.strip())

def saveToEpub(htmlText:str):
    bookName='唐诗三百首'
    saveFileName='/home/linux/Documents/books/{}.epub'.format(bookName)
    book = epub.EpubBook()
    book.set_title(bookName)
    book.set_language('zh')
    subTitlePos=0
    regex = re.compile(r'(\(|（).*?(\)|）)')
    for div in BeautifulSoup(htmlText,'html.parser').find_all(attrs='typecont'):
        subTitlePos+=1
        mainTitle=str(subTitlePos)+'、'+div.div.text
        mainHtmlName='{}.xhtml'.format(mainTitle)
        mainHtmlContent="
{}
".format(mainTitle)
        mainChapter=epub.EpubHtml(title=mainTitle,file_name=mainHtmlName,content=mainHtmlContent)
        book.add_item(mainChapter)
        chapterList:list[epub.EpubHtml]=[]
        for titleTag in div(name='span'):
            subTitle=titleTag.text.strip()
            poetryName=subTitle
            author=regex.search(poetryName)
            poetryName=regex.sub('',subTitle)
            subHtmlName='{}.xhtml'.format(subTitle)
            suHtmlContent="
{}
".format(poetryName)
            suHtmlContent+='
'
            if(author):
                suHtmlContent+=author.group(0)+'
'
            newUrl='https://so.gushiwen.cn'+titleTag.a.get('href')
            for contentTag in BeautifulSoup(getHtmlText(newUrl),'html.parser').find_all(id='contson'+newUrl[-17:-5]):
                for br_tag in contentTag.find_all('br'):
                    br_tag.replace_with('@')
                content=re.sub(r'@', "
",contentTag.text.strip())
                content=regex.sub('',content)
                suHtmlContent+=content
            suHtmlContent+='
'
            subChapter=epub.EpubHtml(title=subTitle,file_name=subHtmlName,content=suHtmlContent)
            book.add_item(subChapter)
            chapterList.append(subChapter)
            print('爬取中...'+subTitle)
        book.toc.append((epub.Section(mainTitle,mainHtmlName),tuple(chapterList)))
        book.add_item(epub.EpubNcx())
        book.add_item(epub.EpubNav())
    epub.write_epub(saveFileName, book,{})

def CrawlTangPoetry(htmlText:str):
    #saveToDocx(htmlText)
    saveToEpub(htmlText)

def main():
    print('开始爬取！')
    url='https://so.gushiwen.cn/gushi/tangshi.aspx'
    htmlText=getHtmlText(url)
    if len(htmlText)>0:
        print('爬取成功！')
        CrawlTangPoetry(htmlText)
    else:
        print('爬取失败！')

main()


"""
Advent of Code 2020: Day 1
https://adventofcode.com/2020/day/1
"""

from typing import List, Tuple
from collections import Counter
from itertools import product

def find_pair_sums_to_N(nums: List[int], N: int) -> Tuple[int, int]:
    if (N % 2 == 0) and nums.count(N // 2) > 1:
        return (N // 2, N // 2)
    nums_set = set(nums)
    for n in nums_set:
        if N - n in nums_set and (n != N - n):
            return (n, N - n)
    raise ValueError("No pair found.")

def overcounted(arr: List, limits: Counter) -> bool:
    """Determines whether any item in arr appears more times than allowed by count_limits."""
    return any(arr.count(x) > limits[x] for x in set(arr))

def find_triplet_sums_to_N(nums: List[int], N: int) -> Tuple[int, int, int]:
    nums_counter = Counter(nums)
    if (N % 3 == 0) and not overcounted([N // 3] * 3, nums_counter):
        return (N // 3, N // 3, N // 3)
    nums_set = set(nums)
    for (m, n) in product(nums_set, nums_set):
        if N - m - n in nums_set and not overcounted([m, n, N - m - n], nums_counter):
            return (m, n, N - m - n)
    raise ValueError("No triplet found.")

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

with open("input_1.txt") as f:
    x_input = [int(x) for x in f]

if __name__ == "__main__":
    x, y = find_pair_sums_to_N(x_input, 2020)
    print(f"Part 1: {x * y}")
    x, y, z = find_triplet_sums_to_N(x_input, 2020)
    print(f"Part 2: {x * y* z}")


from typing import List, Optional

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
        def helper(start: int, end: int) -> TreeNode:
            if start > end:
                return None
            root = TreeNode(val=postorder.pop())

            i = indexes[root.val]
            root.right = helper(i + 1, end)
            root.left = helper(start, i - 1)
            return root

        indexes = {v: i for i, v in enumerate(inorder)}
        return helper(0, len(inorder) - 1)

a = Solution().buildTree(inorder=[9, 3, 15, 20, 7], postorder=[9, 15, 7, 20, 3])


from flask import Flask,render_template,url_for
from forms import RegistrationForm,LoginForm
app = Flask(__name__)

app.config['SECRET_KEY'] = '15fddb6af534010669ebb40f9a4a6b37'

posts = [
    {
'author':'Corey Schafer',
'title':'Blog Post 1',
'content':'First post content',
'date_posted':'April 20,2018'
    },
    {
'author':'Jane Doe',
'title':'Blog Post 2',
'content':'Second post content',
'date_posted':'April 21,2018'
    }

]

@app.route("/")
@app.route("/home")
def home():
    return render_template('home.html',posts=posts)

@app.route("/about")
def about():
    return render_template('about.html',title='About')

@app.route("/register")
def register():
    form = RegistrationForm()
    return render_template('register.html',title='Register',form = form)

@app.route("/login")
def login():
    form = LoginForm()
    return render_template('login.html',title='Login',form = form)

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


import logging
from typing import Dict
import json

class IDP:
    def __init__(self):
        self.userInfoUrl = None          # type: str | None
        self.tokenUrl = None             # type: str | None
        self.jwksUrl = None              # type: str | None
        self.authorizationUrl = None     # type: str | None
        self.domain = None               # type: str | None
        self.clientId = None              # type: str | None

class Authenticator:
    def __init__(self):
        self.realm = None                # type: str | None
        self.domain = None               # type: str | None
        self.authorizationUrl = None     # type: str | None

class SSOModel:
    def __init__(self):
        self.idp = IDP()
        self.authenticator = Authenticator()

    def describe(self):
        return {'idp': self.idp.__dict__, 'authenticator': self.authenticator.__dict__}

    def load_from_json(self, data):
        try:
            self.idp.userInfoUrl = data['idp']['userInfoUrl']
            self.idp.tokenUrl = data['idp']['tokenUrl']
            self.idp.jwksUrl = data['idp']['jwksUrl']
            self.idp.authorizationUrl = data['idp']['authorizationUrl']
            self.idp.domain = data['idp']['domain']
            self.idp.clientId = data['idp']['clientId']
            self.authenticator.realm = data['authenticator']['realm']
            self.authenticator.domain = data['authenticator']['domain']
            self.authenticator.authorizationUrl = data['authenticator']['authorizationUrl']
        except Exception as e:
            logging.critical(e)


"""
11. Container With Most Water
Medium

Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.

Notice that you may not slant the container.

Example 1:

Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example 2:

Input: height = [1,1]
Output: 1
Example 3:

Input: height = [4,3,2,1,4]
Output: 16
Example 4:

Input: height = [1,2,1]
Output: 2

Constraints:

n == height.length
2 <= n <= 3 * 104
0 <= height[i] <= 3 * 104

"""
from typing import List

"""
two pointers (brutal force)

TLE

time O(N^2)
"""

class Solution0:
    def maxArea(self, height: List[int]) -> int:
        n = len(height)

        water = -float('inf')
        for i in range(n):
            for j in range(n - 1, i, -1):
                water = max(water, (j - i) * min(height[i], height[j]))

        return water

"""
two pointers

since if we move pointers towards the middle, the width of area will reduce, but we want to find maximum water hold, so it is always better to move the shorter height of the two pointer, as that might find a higher height that might hold more water (the height of water hold is bounded by the shorter of the two end pointer height)

time O(N)
"""

class Solution:
    def maxArea(self, height: List[int]) -> int:
        n = len(height)

        water = -float('inf')
        cur_height = 0
        i, j = 0, n - 1
        while i < j:
            if height[i] < height[j]:
                water = max(water, (j - i) * height[i])
                i += 1
            else:
                water = max(water, (j - i) * height[j])
                j -= 1

        return water

def main():
    sol = Solution()
    assert sol.maxArea([1,8,6,2,5,4,8,3,7]) == 49, 'fails'

    assert sol.maxArea([1,1]) == 1, 'fails'

    assert sol.maxArea([4,3,2,1,4]) == 16, 'fails'

    assert sol.maxArea([1,2,1]) == 2, 'fails'

if __name__ == '__main__':
   main()


import concurrent
import concurrent.futures
import contextlib
import itertools
import json
import logging
import time
from dataclasses import dataclass
from typing import Dict, List, Mapping, Optional, Tuple, Union

from paho.mqtt.client import MQTTMessage

from tavern._core import exceptions
from tavern._core.dict_util import check_keys_match_recursive
from tavern._core.loader import ANYTHING
from tavern._core.pytest.newhooks import call_hook
from tavern._core.report import attach_yaml
from tavern._core.strict_util import StrictSetting
from tavern.response import BaseResponse

from .client import MQTTClient

logger = logging.getLogger(__name__)

_default_timeout = 1

class MQTTResponse(BaseResponse):
    def __init__(self, client: MQTTClient, name, expected, test_block_config) -> None:
        super().__init__(name, expected, test_block_config)

        self._client = client

        self.received_messages = []  # type: ignore

    def __str__(self):
        if self.response:
            return self.response.payload
        else:
            return "<Not run yet>"

    def verify(self, response) -> dict:
        """Ensure mqtt message has arrived

        Args:
            response: not used except for debug printing
        """

        self.response = response

        try:
            return self._await_response()
        finally:
            self._client.unsubscribe_all()

    def _await_response(self) -> dict:
        """Actually wait for response

        Returns:
            dict: things to save to variables for the rest of this test
        """

        # Get into class with metadata attached
        expected = self.expected["mqtt_responses"]

        by_topic = {
            m: list(v) for m, v in itertools.groupby(expected, lambda x: x["topic"])
        }

        correct_messages: List["_ReturnedMessage"] = []
        warnings: List[str] = []

        with concurrent.futures.ThreadPoolExecutor() as executor:
            futures = []

            for topic, expected_for_topic in by_topic.items():
                logger.debug("Starting thread for messages on topic '%s'", topic)
                futures.append(
                    executor.submit(
                        self._await_messages_on_topic, topic, expected_for_topic
                    )
                )

            for future in concurrent.futures.as_completed(futures):
                # for future in futures:
                try:
                    messages, warnings = future.result()
                except Exception as e:
                    raise exceptions.ConcurrentError(
                        "unexpected error getting result from future"
                    ) from e
                else:
                    warnings.extend(warnings)
                    correct_messages.extend(messages)

        if self.errors:
            if warnings:
                self._adderr("\n".join(warnings))

            raise exceptions.TestFailError(
                "Test '{:s}' failed:\n{:s}".format(self.name, self._str_errors()),
                failures=self.errors,
            )

        saved = {}

        for msg in correct_messages:
            # Check saving things from the payload and from json
            saved.update(
                self.maybe_get_save_values_from_save_block(
                    "payload",
                    msg.msg.payload,
                    outer_save_block=msg.expected,
                )
            )
            saved.update(
                self.maybe_get_save_values_from_save_block(
                    "json",
                    msg.msg.payload,
                    outer_save_block=msg.expected,
                )
            )

            saved.update(self.maybe_get_save_values_from_ext(msg.msg, msg.expected))

        # Trying to save might have introduced errors, so check again
        if self.errors:
            raise exceptions.TestFailError(
                "Saving results from test '{:s}' failed:\n{:s}".format(
                    self.name, self._str_errors()
                ),
                failures=self.errors,
            )

        return saved

    def _await_messages_on_topic(
        self, topic: str, expected: List[Dict]
    ) -> Tuple[List["_ReturnedMessage"], List[str]]:
        """
        Waits for the specific message

        Args:
            topic: topic to listen on
            expected: expected response for this block

        Returns:
            tuple(msg, list): The correct message (if any) and warnings from processing the message
        """

        timeout = max(m.get("timeout", _default_timeout) for m in expected)

        # A list of verifiers that can be used to validate messages for this topic
        verifiers = [_MessageVerifier(self.test_block_config, v) for v in expected]

        correct_messages = []
        warnings = []

        time_spent = 0.0
        while (time_spent < timeout) and verifiers:
            t0 = time.time()

            msg = self._client.message_received(topic, timeout - time_spent)

            if not msg:
                # timed out
                break

            logger.debug("Seeing if message '%s' matched expected", msg)

            call_hook(
                self.test_block_config,
                "pytest_tavern_beta_after_every_response",
                expected=expected,
                response=msg,
            )

            self.received_messages.append(msg)

            with contextlib.suppress(AttributeError):
                msg.payload = msg.payload.decode("utf8")

            attach_yaml(
                {
                    "topic": msg.topic,
                    "payload": msg.payload,
                    "timestamp": msg.timestamp,
                },
                name="rest_response",
            )

            found: List[int] = []
            for i, v in enumerate(verifiers):
                if v.is_valid(msg):
                    correct_messages.append(_ReturnedMessage(v.expected, msg))
                    if found:
                        logger.warning(
                            "Message was matched by multiple mqtt_response blocks"
                        )
                    found.append(i)
                warnings.extend(v.popwarnings())
            verifiers = [v for (i, v) in enumerate(verifiers) if i not in found]

            time_spent += time.time() - t0

        if verifiers:
            for v in verifiers:
                self._adderr(
                    "Expected '%s' on topic '%s' but no such message received",
                    v.expected_payload,
                    topic,
                )

        for msg in correct_messages:
            if msg.expected.get("unexpected"):
                self._adderr(
                    "Got '%s' on topic '%s' marked as unexpected",
                    msg.expected["payload"],
                    topic,
                )

            self._maybe_run_validate_functions(msg)

        return correct_messages, warnings

@dataclass
class _ReturnedMessage:
    """An actual message returned from the API and it's matching 'expected' block."""

    expected: dict
    msg: MQTTMessage

class _MessageVerifier:
    def __init__(self, test_block_config, expected) -> None:
        self.expires = time.time() + expected.get("timeout", _default_timeout)

        self.expected = expected
        self.expected_payload, self.expect_json_payload = self._get_payload_vals(
            expected
        )

        test_strictness = test_block_config.strict
        self.block_strictness: StrictSetting = test_strictness.option_for("json")

        # Any warnings to do with the request
        # eg, if a message was received but it didn't match, message had payload, etc.
        self.warnings: List[str] = []

    def is_valid(self, msg: MQTTMessage) -> bool:
        if time.time() > self.expires:
            return False

        topic = self.expected["topic"]

        def addwarning(w, *args, **kwargs):
            logger.warning(w, *args, **kwargs)
            self.warnings.append(w % args)

        if self.expect_json_payload:
            try:
                msg.payload = json.loads(msg.payload)
            except json.decoder.JSONDecodeError:
                addwarning(
                    "Expected a json payload but got '%s'",
                    msg.payload,
                    exc_info=True,
                )
                return False

        if self.expected_payload is None:
            if msg.payload is None or msg.payload == "":
                logger.info("Got message with no payload (as expected) on '%s'", topic)
                return True
            else:
                addwarning(
                    "Message had payload '%s' but we expected no payload",
                    msg.payload,
                )
        elif self.expected_payload is ANYTHING:
            logger.info("Got message on %s matching !anything token", topic)
            return True
        elif msg.payload != self.expected_payload:
            if self.expect_json_payload:
                try:
                    check_keys_match_recursive(
                        self.expected_payload,
                        msg.payload,
                        [],
                        strict=self.block_strictness,
                    )
                except exceptions.KeyMismatchError:
                    # Just want to log the mismatch
                    pass
                else:
                    logger.info(
                        "Got expected message in '%s' with expected payload",
                        msg.topic,
                    )
                    logger.debug("Matched payload was '%s", msg.payload)
                    return True

            addwarning(
                "Got unexpected payload on topic '%s': '%s' (expected '%s')",
                msg.topic,
                msg.payload,
                self.expected_payload,
            )
        else:
            logger.info(
                "Got expected message in '%s' with expected payload",
                msg.topic,
            )
            logger.debug("Matched payload was '%s", msg.payload)
            return True

        return False

    @staticmethod
    def _get_payload_vals(expected: Mapping) -> Tuple[Optional[Union[str, dict]], bool]:
        """Gets the payload from the 'expected' block

        Returns:
            tuple: First element is the expected payload, second element is whether it's
                expected to be json or not
        """
        # TODO move this check to initialisation/schema checking
        if "json" in expected:
            if "payload" in expected:
                raise exceptions.BadSchemaError(
                    "Can only specify one of 'payload' or 'json' in MQTT response"
                )

            payload = expected["json"]
            json_payload = True

            if payload.pop("$ext", None):
                raise exceptions.MisplacedExtBlockException(
                    "json",
                )
        elif "payload" in expected:
            payload = expected["payload"]
            json_payload = False
        else:
            payload = None
            json_payload = False

        return payload, json_payload

    def popwarnings(self) -> List[str]:
        popped = []
        while self.warnings:
            popped.append(self.warnings.pop(0))
        return popped