from django.shortcuts import render

from django.contrib.auth.models import User

from chat.models import Message, Contact, Chat

from django.http import HttpResponse, JsonResponse

from django.shortcuts import get_object_or_404

from django.db.models import Q

# from django.contrib.auth.decorators import login_required
from .models import Chat

from django.views.decorators.csrf import csrf_exempt

# Create your views here.
def index(request):
    return render(request, 'chat/index.html')

# @login_required
def room(request, room_name):
	users = User.objects.all().exclude(username = request.user)
	users_length = User.objects.all().count()

	context = {
		'username': request.user.username,
		'room_name': room_name,
		'users': users,
		'users_length': users_length
	}

	template = 'chat/chat.html'

	return render(request, template, context)

def last_10_messages(chatId):
	chat = get_object_or_404(Chat, id=chatId)
	return chat.messages.all().order_by('-timestamp')[:10][::-1]

def get_one_msg(request):
	chat = get_object_or_404(Chat, id=chatId)
	return chat.messages.all().order_by('-timestamp')[:1]

def get_msg_contact(username):
	author = User.objects.get(username= username)
	contact_user = get_object_or_404(Contact, user= author)
	return contact_user

def get_chat(chatId):
	return get_object_or_404(Chat, id = chatId)

@csrf_exempt
def ajaxify(request):
	if request.is_ajax():
		user1 = request.user
		param = request.POST['id']
		user2 = User.objects.get(id=param)
		contact_user1 = Contact.objects.get(user=user1)
		contact_user2 = Contact.objects.get(user=user2)
		print("user", contact_user2.user.username)
		print(type(user2))

		chatId = Chat.objects.filter(participants=contact_user1).filter(participants=contact_user2).first()

		if chatId == None:
			chat = Chat()
			chat.save()
			chat.participants.add(contact_user1)
			chat.participants.add(contact_user2)

			chatId = Chat.objects.filter(participants=contact_user1).filter(participants=contact_user2).first()

		context = {
			'chatId': chatId.id,
			'contact_user2': contact_user2.user.username
		}
		return JsonResponse(context)


import math

documents = ['Гипотеза – предположение; тезис, предполагающий доказательство.',
             'Гуманитарные науки – группа научных дисциплин, направленных на изучение человека и его деятельности.',
             'Концепция – предтеория; совокупность обоснованных суждений о предмете (событии, явлении).',
             'Методика – группа исследовательских методов и приемов, связанных общей целью применения (например: методика анализа статистических источников).',
             'Методология – учение о принципах, методах и средствах познания.',
             'Научная теория – высшая форма систематизации знания; система знаний, описывающая и объясняющая определённую совокупность явлений.',
             'Общественные науки – группа научных дисциплин, направленных на всестороннее изучение социума (общества).']

#---Calculate term frequency --

#First: tokenize words
dictOfWords = {}

for index, sentence in enumerate(documents):
    tokenizedWords = sentence.split(' ')
    dictOfWords[index] = [(word,tokenizedWords.count(word)) for word in tokenizedWords]

#print(dictOfWords)

#second: remove duplicates
termFrequency = {}

for i in range(0, len(documents)):
    listOfNoDuplicates = []
    for wordFreq in dictOfWords[i]:
        if wordFreq not in listOfNoDuplicates:
            listOfNoDuplicates.append(wordFreq)
        termFrequency[i] = listOfNoDuplicates
#print(termFrequency)

#Third: normalized term frequency
normalizedTermFrequency = {}
for i in range(0, len(documents)):
    sentence = dictOfWords[i]
    lenOfSentence = len(sentence)
    listOfNormalized = []
    for wordFreq in termFrequency[i]:
        normalizedFreq = wordFreq[1]/lenOfSentence
        listOfNormalized.append((wordFreq[0],normalizedFreq))
    normalizedTermFrequency[i] = listOfNormalized

#print(normalizedTermFrequency)

#---Calculate IDF

#First: put al sentences together and tokenze words

allDocuments = ''
for sentence in documents:
    allDocuments += sentence + ' '
allDocumentsTokenized = allDocuments.split(' ')

#print(allDocumentsTokenized)

allDocumentsNoDuplicates = []

for word in allDocumentsTokenized:
    if word not in allDocumentsNoDuplicates:
        allDocumentsNoDuplicates.append(word)

#print(allDocumentsNoDuplicates)

#Second calculate the number of documents where the term t appears

dictOfNumberOfDocumentsWithTermInside = {}

for index, voc in enumerate(allDocumentsNoDuplicates):
    count = 0
    for sentence in documents:
        if voc in sentence:
            count += 1
    dictOfNumberOfDocumentsWithTermInside[index] = (voc, count)

#print(dictOfNumberOfDocumentsWithTermInside)

#calculate IDF

dictOFIDFNoDuplicates = {}

for i in range(0, len(normalizedTermFrequency)):
    listOfIDFCalcs = []
    for word in normalizedTermFrequency[i]:
        for x in range(0, len(dictOfNumberOfDocumentsWithTermInside)):
            if word[0] == dictOfNumberOfDocumentsWithTermInside[x][0]:
                listOfIDFCalcs.append((word[0],math.log(len(documents)/dictOfNumberOfDocumentsWithTermInside[x][1])))
    dictOFIDFNoDuplicates[i] = listOfIDFCalcs

#print(dictOFIDFNoDuplicates)

#Multiply tf by idf for tf-idf

dictOFTF_IDF = {}
for i in range(0,len(normalizedTermFrequency)):
    listOFTF_IDF = []
    TFsentence = normalizedTermFrequency[i]
    IDFsentence = dictOFIDFNoDuplicates[i]
    for x in range(0, len(TFsentence)):
        listOFTF_IDF.append((TFsentence[x][0],TFsentence[x][1]*IDFsentence[x][1]))
    dictOFTF_IDF[i] = listOFTF_IDF

print(dictOFTF_IDF)

def test_tf(term, text):
    newText = text.split(' ')
    print(text.count(term))
    print(len(newText))


from hashlib import sha256

from Cryptodome import Random
from Cryptodome.Cipher import AES
from Cryptodome.Cipher import PKCS1_v1_5
from Cryptodome.PublicKey import RSA

def _pad(s, bs):
    return s + (bs - len(s.encode('utf-8')) % bs) * chr(bs - len(s.encode('utf-8')) % bs)  # 필요한 바이트 만큼 붙여줌

def _unpad(s):
    return s[ : -ord( s[ len(s)-1 : ] ) ]  # pad 함수로 padding 된 s를 원상복귀 시킴

def aes(origin):
    print("\ncipher type(DES/DES3/AES/ARS4): AES")

    while True:
        key = input("key(16/24/32): ").encode('utf-8')
        if len(key) not in (16, 24, 32):
            print("check key's length, input key's length should be (16,24,32)")
        else:
            break

    bs = AES.block_size  # bs(block size) : 16 byte
    iv = Random.new().read(bs)  # initial vector : 16 byte
    string = _pad(origin, bs)  #padding 함

    # encrypt
    cipher = AES.new(key, AES.MODE_CBC, IV=iv)
    cipher_text = iv + cipher.encrypt(string.encode('utf-8'))
    print(f"encrypted: {cipher_text}")

    # decrypt
    iv = cipher_text[:AES.block_size]

    cipher = AES.new(key, AES.MODE_CBC, IV=iv)
    plain_text = _unpad(cipher.decrypt(cipher_text[AES.block_size:])).decode('utf-8')
    print(f"decrypted: {plain_text}")

def sha(origin):
    print("\nhash type(SHA/SHA256/SHA384/SHA512/HMAC): SHA256")
    result = sha256(origin.encode('utf-8')).hexdigest()
    print(result)

def rsa(origin):
    print("\nRSA")

    while True:
        key_length: int = int(input("key length(x256, >=1024): "))
        if key_length < 1024 or (key_length % 256) != 0:
            print("key length should longer than 1024 and should divisible with 256")
        else:
            break

    key = RSA.generate(key_length)  # random key generate

    private_key = key.exportKey('DER')
    public_key = key.publickey().exportKey('DER')

    private_key = RSA.importKey(private_key)
    public_key = RSA.importKey(public_key)

    encrypt = PKCS1_v1_5.new(public_key)
    cipher_text = encrypt.encrypt(origin.encode('utf-8'))
    print(f"encrypted: {cipher_text}")

    decrypt = PKCS1_v1_5.new(private_key)
    plaintext = decrypt.decrypt(cipher_text, sentinel=None).decode()
    print(f"decrypted: {plaintext}")

origin = input("original data: ")

aes(origin)
sha(origin)
rsa(origin)


import os
import time

import numpy as np
import cv2
import vtk
import argparse

from scale_recovery import project_to_3d
from visual.vtk_wrapper.vtk_point_cloud_glyph import VtkPointCloudGlyph
from visual.vtk_wrapper import vtk_utils

def setup_vtk_renderer():
    # Setup renderer
    vtk_renderer = vtk.vtkRenderer()
    vtk_renderer.SetBackground(0.2, 0.3, 0.4)

    return vtk_renderer

def setup_vtk_render_window(window_name, window_size, vtk_renderer):
    # Setup Render Window
    vtk_render_window = vtk.vtkRenderWindow()
    vtk_render_window.SetWindowName(window_name)
    vtk_render_window.SetSize(*window_size)
    vtk_render_window.AddRenderer(vtk_renderer)
    return vtk_render_window

def get_velo_points(raw_data, frame_idx):
    velo_points = raw_data.get_velo(frame_idx)

    # Filter points to certain area
    points = velo_points[:, 0:3]
    area_extents = np.asarray([[0, 100], [-50, 50], [-5, 1]], dtype=np.float32)
    area_filter = \
        (points[:, 0] > area_extents[0, 0]) & \
        (points[:, 0] < area_extents[0, 1]) & \
        (points[:, 1] > area_extents[1, 0]) & \
        (points[:, 1] < area_extents[1, 1]) & \
        (points[:, 2] > area_extents[2, 0]) & \
        (points[:, 2] < area_extents[2, 1])
    points = points[area_filter]

    return points

def visual_depth_real_scale(depth_dir, img_dir, out_dir, width=736, height=416):

    scale_x, scale_y = 736/1920, 416/1080
    cx,cy,fx,fy = 851.8287*scale_x, 424.0041*scale_y, 1998.7356*scale_x, 1991.8909*scale_y

    vtk_window_size = (1280, 720)
    max_fps = 10000.0
    file_list = sorted(os.listdir(depth_dir))
    frame_range = (0, len(file_list))
    ##########################################
    min_loop_time = 1.0 / max_fps
    vtk_renderer = setup_vtk_renderer()
    vtk_render_window = setup_vtk_render_window('Overlaid Point Cloud', vtk_window_size, vtk_renderer)
    current_cam = vtk_renderer.GetActiveCamera()
    current_cam.SetViewUp(0, 0, 1)    # 0, -1, 0
    current_cam.SetPosition(0, -10, -20)  #  0.0, 0, 0
    current_cam.SetFocalPoint(0.0, 0.0, 20)
    current_cam.SetViewAngle(50)
    current_cam.Zoom(0.9)

    # # Create VtkAxes
    vtk_axes = vtk.vtkAxesActor()
    vtk_axes.SetTotalLength(1, 1, 1)

    # Setup interactor
    vtk_interactor = vtk.vtkRenderWindowInteractor()
    vtk_interactor.SetRenderWindow(vtk_render_window)
    vtk_interactor.SetInteractorStyle(vtk_utils.ToggleActorsInteractorStyle(None, vtk_renderer, current_cam, vtk_axes))
    vtk_interactor.Initialize()

    # Point cloud
    vtk_pc = VtkPointCloudGlyph()

    # # Add actors
    vtk_renderer.AddActor(vtk_pc.vtk_actor)

   ###########################################
    for frame_idx in range(*frame_range):
        loop_start_time = time.time()
        print('{} / {}'.format(frame_idx, len(file_list) - 1))

        # Load next frame data
        load_start_time = time.time()

        frame = file_list[frame_idx]
        depth_map_path = os.path.join(depth_dir, frame)
        depth_map = np.load(depth_map_path)  # 736*416

        img_name = frame.split('.')[0]
        img_path = img_name +'.jpg'
        raw_image_path = os.path.join(img_dir, img_path)
        bgr_image = cv2.imread(raw_image_path)
        bgr_image = cv2.resize(bgr_image, (width,height))
        # print(raw_image_path)
        cam0_curr_pc = project_to_3d(depth_map, cx, cy, fx, fy, width, height)
        point_colours = bgr_image.reshape(-1, 3)

        print('load\t\t', time.time() - load_start_time)

        # VtkPointCloud
        vtk_pc_start_time = time.time()
        vtk_pc.set_points(cam0_curr_pc, point_colours)
        print('vtk_pc\t\t', time.time() - vtk_pc_start_time)

        # Reset the clipping range to show all points
        vtk_renderer.ResetCameraClippingRange()

        out_depth_map_path = os.path.join(out_dir, img_name + '.png')
        grabber = vtk.vtkWindowToImageFilter()
        grabber.SetInput(vtk_render_window)
        grabber.Update()
        writer = vtk.vtkPNGWriter()
        writer.SetInputData(grabber.GetOutput())
        writer.SetFileName(out_depth_map_path)
        writer.Write()

        # Render
        render_start_time = time.time()
        vtk_render_window.Render()
        print('render\t\t', time.time() - render_start_time)

        # Pause to keep frame rate under max
        loop_run_time = time.time() - loop_start_time
        print('loop\t\t', loop_run_time)
        if loop_run_time < min_loop_time:
            time.sleep(min_loop_time - loop_run_time)

        print('---')

    print('Done')

    # Keep window open
    vtk_interactor.Start()

def main():
    depth_dir = '../dataset/hs5_0831/depth_map/hs5_35k_3_east_park'
    img_dir = '../dataset/hs5_0831/image_undistort'
    out_dir = './output/hs5_35k_3_east_park'
    if not os.path.exists(out_dir):
        os.mkdir(out_dir)
    width = 736
    height = 416
    visual_depth_real_scale(depth_dir, img_dir, out_dir, width, height)

if __name__ == '__main__':
    main()


from django.shortcuts import render
from blog.models import Post, Comment
from .forms import CommentForm

def blog_index(request):
    # query all, "-" = descending order
    posts = Post.objects.all().order_by('-created_on')
    context = {
        "posts": posts,
    }
    return render(request, "blog_index.html", context)

def blog_category(request, category):
    # django queryset filter
    posts = Post.objects.filter(
        categories__name__contains=category
    ).order_by(
        '-created_on'
    )
    context = {
        "category": category,
        "posts": posts
    }
    return render(request, "blog_category.html", context)

def blog_detail(request, pk):
    post = Post.objects.get(pk=pk)
    comments = Comment.objects.filter(post=post)

    # imported form from forms.py
    form = CommentForm()
    # check if request was recieved
    if request.method == 'POST':
        form = CommentForm(request.POST)
        # validated
        if form.is_valid():
            # new instance created
            comment = Comment(
                # form.cleaned_data = dictionary and information can be accessed this way. Keys will correspond to form fields
                author=form.cleaned_data["author"],
                body=form.cleaned_data["body"],
                post=post
            )
            comment.save()

    context = {
        "post": post,
        "comments": comments,
        "form": form,
    }

    return render(request, "blog_detail.html", context)

# form lifecycle
# When a user visits a page containing a form, they send a GET request to the server. In this case, there’s no data entered in the form, so we just want to render the form and display it.

# When a user enters information and clicks the Submit button, a POST request, containing the data submitted with the form, is sent to the server. At this point, the data must be processed, and two things can happen:

# The form is valid, and the user is redirected to the next page.
# The form is invalid, and empty form is once again displayed. The user is back at step 1, and the process repeats.


#!/usr/bin/python3
import sys
import math
from collections import defaultdict

counts = defaultdict(int)

def get_ent(counts, tot):
    ent = 0
    for w in counts:
        p = float(counts[w]) / tot
        ent += p * math.log2(p)

    return -ent

tot = 0
for line in open(sys.argv[1]):
    items = line.split()
    tot = tot + len(items) - 1
    for w in items[1:]:
        counts[w] = counts[w] + 1

print (get_ent(counts, tot))


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

# In[1]:

import pandas as pd
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
from sklearn.neighbors import KNeighborsClassifier

# In[2]:

from sklearn.datasets import load_iris
iris_data = load_iris()

# In[3]:

print(iris_data.DESCR)

# In[4]:

df = pd.DataFrame(iris_data.data)
df.columns = iris_data.feature_names
df.head()

# In[5]:

df['Species'] = iris_data.target
df.head()

# In[6]:

df['Species'].unique()

# In[7]:

print("Species name:", iris_data['target_names'])

# In[8]:

df.shape

# In[9]:

df.info()

# In[10]:

df.isnull().sum()

# In[11]:

df.describe()

# In[12]:

df['Species'].value_counts()

# In[13]:

sns.pairplot(df, hue='Species', markers='+', palette='colorblind')
plt.show()

# In[14]:

plt.figure(figsize=(10,7))
sns.heatmap(df.corr(), annot=True)
plt.show()

# In[15]:

df.plot(kind='scatter', x="sepal length (cm)", y="sepal width (cm)")
plt.show()

# In[16]:

fig=df[df.Species==0].plot(kind='scatter', x="sepal length (cm)", y="sepal width (cm)",
                           color='orange', label='Setosa')
df[df.Species==1].plot(kind='scatter', x="sepal length (cm)", y="sepal width (cm)",
                           color='blue', label='Versicolor', ax=fig)
df[df.Species==2].plot(kind='scatter', x="sepal length (cm)", y="sepal width (cm)",
                           color='green', label='Virginica', ax=fig)
fig.set_xlabel("Sepal Length")
fig.set_ylabel("Sepal Width")
fig.set_title("Sepal Length vs width")
plt.show()

# In[17]:

sns.violinplot(x="Species", y='sepal length (cm)', data=df)

# In[18]:

plt.figure(figsize=(12,7))
plt.subplot(2,2,1)
sns.violinplot(x="Species", y='sepal length (cm)', data=df)
plt.subplot(2,2,2)
sns.violinplot(x="Species", y='sepal width (cm)', data=df)
plt.subplot(2,2,3)
sns.violinplot(x="Species", y='petal length (cm)', data=df)
plt.subplot(2,2,4)
sns.violinplot(x="Species", y='petal width (cm)', data=df)
plt.show()

# In[19]:

X = df.drop('Species', axis=1)
y = df['Species']

# In[20]:

X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=42)

# In[21]:

print("X_train:", X_train.shape)
print("X_test:", X_test.shape)
print("y_train:", y_train.shape)
print("y_test:", y_test.shape)

# In[22]:

dtree = DecisionTreeClassifier()
dtree.fit(X_train, y_train)
dtree_pred = dtree.predict(X_test)
dtree_acc = accuracy_score(dtree_pred, y_test)
print("Test accuracy: {:.2f}%".format(dtree_acc*100))

# In[23]:

print(classification_report(y_test, dtree_pred))

# In[24]:

print(confusion_matrix(y_test, dtree_pred))

# In[26]:

plt.figure(figsize=(15,10))
plot_tree(dtree, feature_names=iris_data.feature_names, class_names = iris_data['target_names'])

# In[27]:

knn_model = KNeighborsClassifier(n_neighbors=3)
knn_model.fit(X_train, y_train)
knn_pred = knn_model.predict(X_test)
knn_acc = accuracy_score(knn_pred, y_test)
print("Test accuracy: {:.2f}%".format(knn_acc*100))

# In[28]:

print(confusion_matrix(y_test, knn_pred))

# In[29]:

df.head()

# In[30]:

df.columns

# In[31]:

data = {'sepal length (cm)':5.0, 'sepal width (cm)':3.4, 'petal length (cm)':1.4,
       'petal width (cm)':0.2}
index = [0]
new_df = pd.DataFrame(data, index)
new_df

# In[32]:

value_pred = dtree.predict(new_df)
value_pred

Supervised machine learning on iris dataset LIVE.py


#*-* coding: utf-8 *-*
import codecs

fileToFindWord = '/home/eliezerpires/mgca/main_ctrl.O'
binaryFileOpen = open(fileToFindWord, 'rb')
binaryFileOpen = binaryFileOpen.read()
new_binaryDecode = codecs.decode(binaryFileOpen, encoding='utf-8', errors='ignore')
print(new_binaryDecode)

# newFileUtf8 = codecs.decode(binaryFileOpen, charset['encoding'], 'strict')
# print(newFileUtf8)
# binaryFileOpen.close()


"""
235. 二叉搜索树的最近公共祖先
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为：“对于有根树 T 的两个结点 p、q，
最近公共祖先表示为一个结点 x，满足 x 是 p、q 的祖先且 x 的深度尽可能大（一个节点也可以是它自己的祖先）。”
示例 1:
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
输出: 6
解释: 节点 2 和节点 8 的最近公共祖先是 6。
说明:
所有节点的值都是唯一的。
p、q 为不同节点且均存在于给定的二叉搜索树中。
"""
class TreeNode:
    def __init__(self,x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def lowestCommonAncestor(self, root:TreeNode, p: TreeNode, q:TreeNode)->TreeNode:
        if root.val > p.val and root.val > q.val:
            return self.lowestCommonAncestor(root.left, p, q)
        elif root.val < p.val and root.val < q.val:
            return self.lowestCommonAncestor(root.right, p, q)
        return root

    def lowestCommonAncestor(self, root:TreeNode, p:TreeNode,q:TreeNode)-> TreeNode:
        while root:
            if root.val < p.val and root.val < q.val:
                root = root.right
            elif root.val > p.val and root.val > q.val:
                root = root.left
            else:
                break
        return root

二叉树/problem235_二叉搜索树的最近公共祖先.py


import xarray as xr
import rasterio

def _get_transform_from_xr(data, x_coord='longitude', y_coord='latitude'):
    """Create a geotransform from an xarray.Dataset or xarray.DataArray.
    """

    from rasterio.transform import from_bounds
    geotransform = from_bounds(data[x_coord][0], data[y_coord][-1],
                               data[x_coord][-1], data[y_coord][0],
                               len(data[x_coord]), len(data[y_coord]))
    return geotransform

def export_xarray_to_geotiff(data, tif_path, bands=None, no_data=-9999, crs="EPSG:4326",
                             x_coord='longitude', y_coord='latitude'):
    """
    Export a GeoTIFF from a 2D `xarray.Dataset`.
    Parameters
    ----------
    data: xarray.Dataset or xarray.DataArray
        An xarray with 2 dimensions to be exported as a GeoTIFF.
    tif_path: string
        The path to write the GeoTIFF file to. You should include the file extension.
    bands: list of string
        The bands to write - in the order they should be written.
        Ignored if `data` is an `xarray.DataArray`.
    no_data: int
        The nodata value.
    crs: string
        The CRS of the output.
    x_coord, y_coord: string
        The string names of the x and y dimensions.
    """
    if isinstance(data, xr.DataArray):
        height, width = data.sizes[y_coord], data.sizes[x_coord]
        count, dtype = 1, data.dtype
    else:
        if bands is None:
            bands = list(data.data_vars.keys())
        else:
            assrt_msg_begin = "The `data` parameter is an `xarray.Dataset`. "
            assert isinstance(bands, list), assrt_msg_begin + "Bands must be a list of strings."
            assert len(bands) > 0 and isinstance(bands[0], str), assrt_msg_begin + "You must supply at least one band."
        height, width = data.dims[y_coord], data.dims[x_coord]
        count, dtype = len(bands), data[bands[0]].dtype
    with rasterio.open(
            tif_path,
            'w',
            driver='GTiff',
            height=height,
            width=width,
            count=count,
            dtype=dtype,
            crs=crs,
            transform=_get_transform_from_xr(data, x_coord=x_coord, y_coord=y_coord),
            nodata=no_data) as dst:
        if isinstance(data, xr.DataArray):
            dst.write(data.values, 1)
        else:
            for index, band in enumerate(bands):
                dst.write(data[band].values.astype(dtype), index + 1)
    dst.close()


from typing import List

from rich import box
from rich.panel import Panel
from rich.table import Table

from vaccibot.models import AppointmentMatch

# ----- Data ----- #

COLUMNS_SETTINGS = {
    "CENTER": dict(
        justify="left",
        header_style="bold",
        style="bold",
    ),  # no_wrap=True),
    "CITY": dict(justify="center", header_style="magenta", style="magenta", no_wrap=True),
    "DISTANCE (KM)": dict(
        justify="center", header_style="medium_turquoise", style="medium_turquoise", no_wrap=True
    ),
    "NEXT APPOINTMENT": dict(justify="right", header_style="bold green3", style="bold green3", no_wrap=True),
    "AVAILABLE VACCINES": dict(
        justify="right", header_style="bold dark_orange3", style="bold dark_orange3", no_wrap=True
    ),
    "URL": dict(
        justify="right", header_style="bold cornflower_blue", style="bold cornflower_blue", no_wrap=True
    ),
}

# ----- Helpers ----- #

def _default_table() -> Table:
    """Create the default structure for the Tasks Table, hard coded columns and no rows added."""
    table = Table(box=box.SIMPLE_HEAVY)
    for header, header_col_settings in COLUMNS_SETTINGS.items():
        table.add_column(header, **header_col_settings)
    return table

def make_department_table(appointments: List[AppointmentMatch]) -> Table:
    table = _default_table()
    for appointment in appointments:
        table.add_row(
            appointment.center_name,
            appointment.center_city,
            str(appointment.distance_km),
            appointment.next_appointment_time,
            ",".join(v for v in appointment.vaccines),
            str(appointment.url),
        )
    return table


import unittest
from days.one import sort_by_heaviest, split_to_elves

class TestDayOne(unittest.TestCase):
    def test_sorting_by_heaviest_is_correct(self):
        set_of_numbers = [[1, 74, 22], [94, 0, 1], [1000, 43, 2], [-1, 0]]
        response = sort_by_heaviest(set_of_numbers)
        self.assertEqual(response, [[1000, 43, 2], [1, 74, 22], [94, 0, 1], [-1, 0]])

    def test_splitting_returns_correct_number_of_elves(self):
        input_elves = ["43", "43", "43", "43", "", "27", "", "43", "43"]
        response = split_to_elves(input_elves)
        self.assertEqual(len(response), 3)


# -*- coding: gb18030 -*-
#
# $Id: ItemResearch.py, fangpengjun Exp $

"""
implement item Research window class

"""
from guis import *
import ShareTexts
from LabelGather import labelGather
from guis.common.Window import Window
from guis.common.PyGUI import PyGUI
from guis.controls.BaseObjectItem import BaseObjectItem as BOItem
from guis.controls.ProgressBar import HFProgressBar
from guis.controls.Button import Button
from guis.controls.StaticText import StaticText
from guis.tooluis.CSRichText import CSRichText
from guis.controls.Icon import Icon
from ModelRender import GodRender
from guis.controls.Icon import Icon
from guis.tooluis.richtext_plugins.PL_Font import PL_Font
from guis.tooluis.richtext_plugins.PL_NewLine import PL_NewLine
from guis.tooluis.richtext_plugins.PL_Space import PL_Space
from guis.tooluis.richtext_plugins.PL_Image import PL_Image
from ItemsFactory import ObjectItem as ItemInfo
from guis.controls.SelectorGroup import SelectorGroup
from TongBeastData import TongBeastData
tongBeastData = TongBeastData.instance()
from NPCModelLoader import NPCModelLoader
modelLoader = NPCModelLoader.instance()
from config.client.msgboxtexts import Datas as mbmsgs
import items
import GUIFacade
import csconst
import csdefine
import random
import Language
from config.client import tongBeastDsp

TONG_ACTIONVAL_LIMIT = {
	0 : ( ShareTexts.NUM_0, 0 ),
	1 : ( ShareTexts.NUM_1, 100 ),
	2 : ( ShareTexts.NUM_2, 200 ),
	3 : ( ShareTexts.NUM_3, 300 ),
	4 : ( ShareTexts.NUM_4, 400 ),
	5 : ( ShareTexts.NUM_5, 500 )
}

BEAS_INDEX_MAP = { 1: "10111139",
		2: "10111140",
		3: "10111141",
		4: "10111142"
		}

class ShenShouBeckon( Window ):

	def __init__( self ):
		wnd = GUI.load( "guis/general/tongabout/shenshoubeckon/window.gui" )
		uiFixer.firstLoadFix( wnd )
		Window.__init__( self, wnd )
		self.posZSegment = ZSegs.L4
		self.activable_ = True
		self.escHide_ 	 = True
		self.selIndex = 0 #当前选择的神兽索引
		self.beckonIndex = 0 #当前召唤出的神兽
		self.__trapID = 0
		self.shenshouLevel = 0 #召唤出的神兽等级
		self.__turnModelCBID = 0
		self.__defaultColor = 255, 255, 255, 255
		self.__triggers = {}
		self.__registerTriggers()
		self.__initialize( wnd )

	def __initialize( self, wnd ):
		self.__pyBeckonBtn = Button( wnd.btnBeckon )
		self.__pyBeckonBtn.setStatesMapping( UIState.MODE_R4C1 )
		self.__pyBeckonBtn.onLClick.bind( self.__beckonShenShou )
		labelGather.setPyBgLabel( self.__pyBeckonBtn, "TongAbout:ShenShouBeckon", "btnBeckon" )

		self.__pyStAction = StaticText( wnd.actionValue )
		self.__pyStAction.text = ""

		self.__pyStAcadeLevel = StaticText( wnd.stAcadLevel )#帮会神兽殿等级
		self.__pyStAcadeLevel.text = ""

		self.__pyRtUseMoney = CSRichText( wnd.rtUseMoney )
		self.__pyRtUseMoney.align = "L"
		self.__pyRtUseMoney.text = ""

		self.__pyStSelGod = StaticText( wnd.selGodAnimal ) #当前召唤出的神兽
		self.__pyStSelGod.text = ""

		self.__pyBtnLeft = Button( wnd.btnLeft )
		self.__pyBtnLeft.setStatesMapping( UIState.MODE_R2C2 )
		self.__pyBtnLeft.onLMouseDown.bind( self.__turnLeft )

		self.__pyBtnRight = Button( wnd.btnRight )
		self.__pyBtnRight.setStatesMapping( UIState.MODE_R2C2 )
		self.__pyBtnRight.onLMouseDown.bind( self.__turnRight )

		self.__pyGodRender = GodRender( wnd.modelRender )

		self.__pyRtResReq = CSRichText( wnd.rtReshReq )
		self.__pyRtResReq.align = "L"
		self.__pyRtResReq.lineFlat = "T"
		self.__pyRtResReq.text = ""

		self.__pyGodBeckon = Icon( wnd.godBeckon.icon )
		self.__pyGodBeckon.icon = ""
		self.__godsGroup = SelectorGroup()
		self.beastDatas = tongBeastData.getDatas()
		self.__pyGodIcons = {}
		self.__pyStAttrs = {}
		self.__pyAttrBar = {}
		for name, item in wnd.children:
			if name.startswith( "godAnimal_" ): #神兽图标
				index = int( name.split( "_" )[1])
				pyGodIcon = GodIcon( item, index )
				self.__pyGodIcons[index] = pyGodIcon
				self.__godsGroup.addSelector( pyGodIcon )
				beaClassName = BEAS_INDEX_MAP[index]
				if self.beastDatas.has_key( beaClassName ): #设置神兽头像
					modelNums = self.beastDatas[beaClassName][1]["modelNums"]
					if len( modelNums ) <= 0:return
					modelNumber = modelNums[random.randint(0, len( modelNums ) - 1 )]
					pyGodIcon.godTexture = modelLoader.getHeadTexture( modelNumber )
					pyGodIcon.modelNumber = modelNumber
			if name.startswith( "attr_" ): #神兽战斗属性
				attrStr = name[5:]
				pyStattr = AttrItem( item )
				pyStattr.title = labelGather.getText( "TongAbout:ShenShouBeckon", attrStr )
				pyStattr.text = ""
				self.__pyStAttrs[attrStr] = pyStattr
			if name.endswith( "_bar" ): #神兽血条和蓝条
				attrbar = name.split( "_" )[0]
				pyAttrBar = HFProgressBar( item.bar )
				pyAttrBar.clipMode = "RIGHT"
				pyAttrBar.value = 0.0
				self.__pyAttrBar[attrbar] = pyAttrBar

		labelGather.setLabel( wnd.lbTitle, "TongAbout:ShenShouBeckon", "lbTitle" )
		labelGather.setLabel( wnd.useMoneyText, "TongAbout:BuildReSearch", "useMoneyText" )
		labelGather.setLabel( wnd.actionText, "TongAbout:BuildReSearch", "actionText" )
		labelGather.setLabel( wnd.rendPanel.stTitle, "TongAbout:BuildReSearch", "resList" )
		labelGather.setLabel( wnd.reqPanel.bgTitle.stTitle, "TongAbout:ShenShouBeckon", "beckonReq" )
		labelGather.setLabel( wnd.statusPanel.bgTitle.stTitle, "TongAbout:ShenShouBeckon", "tongStatus" )
	# --------------------------------------------------------
	def __registerTriggers( self ) :
		self.__triggers["EVT_ON_TOGGLE_TONG_SHENSHOU_BRECKON"] = self.__toggleShows #弹出窗口
		self.__triggers["EVT_ON_TOGGLE_TONG_SET_SHENSHOU_INFO"] = self.__onCurShenshouChange #当前选择神兽改变通知
		self.__triggers["EVT_ON_TOGGLE_TONG_MONEY_CHANGE"] = self.__onTongMoneyChange #帮会资金改变
		self.__triggers["EVT_ON_TONG_SHENSHOU_SLECTED"] = self.__onSelectedShenshou #选择某一个神兽
		self.__triggers["EVT_ON_TONG_SHENSHOU_BRECKON_SUCCEED"] = self.__onBreckonSucc #神兽召唤成功后的回调
		self.__triggers["EVT_ON_TOGGLE_TONG_BUILD_INFO"] = self.__onReciveBuildInfo	#获取建筑信息
		for key in self.__triggers.iterkeys() :
			ECenter.registerEvent( key, self )

	def __deregisterTriggers( self ) :
		for key in self.__triggers.iterkeys() :
			ECenter.unregisterEvent( key, self )

	# -------------------------------------------------------
	def __toggleShows( self, shenshouLevel, shenshouType ):
		player = BigWorld.player()
		distance = csconst.COMMUNICATE_DISTANCE
		if hasattr( GUIFacade.getGossipTarget(), "getRoleAndNpcSpeakDistance" ):
			distance = GUIFacade.getGossipTarget().getRoleAndNpcSpeakDistance()
		self.__trapID = player.addTrapExt( distance, self.__onEntitiesTrapThrough )#打开窗口后为玩家添加对话陷阱
		self.__pyStAcadeLevel.text = labelGather.getText( "TongAbout:ShenShouBeckon", "ssLevel" )%shenshouLevel
		beaClassName = ""
		if shenshouType > 0: #没有召唤出来的神兽
			beaClassName = BEAS_INDEX_MAP[shenshouType]
		self.beckonIndex = shenshouType
		self.shenshouLevel = shenshouLevel
		if self.beastDatas.has_key( beaClassName ): #设置召唤出的神兽头像
			modelNums = self.beastDatas[beaClassName][1]["modelNums"]
			modelNum = ""
			if len( modelNums ) > 0:
				modelNum = modelNums[random.randint(0, len( modelNums ) - 1 )]
			self.__pyGodBeckon.icon = modelLoader.getHeadTexture( modelNum )
			self.__pyGodRender.resetModel( beaClassName )
			for attr, pyStattr in self.__pyStAttrs.iteritems():
				pyStattr.text = self.beastDatas[beaClassName][shenshouLevel][attr]
			for attrbar, pyAttrBar in self.__pyAttrBar.items():
				attrVal = self.beastDatas[beaClassName][shenshouLevel][attrbar]
				if attrVal <= 0:
					pyAttrBar.value = 0.0
				else:
					pyAttrBar.value = float( attrVal/attrVal )
			self.__pyStSelGod.text = self.beastDatas[beaClassName][shenshouLevel]["uname"]
		self.show()

	def __delTrap( self ) :
		player = BigWorld.player()
		if self.__trapID :
			player.delTrap( self.__trapID )									#删除玩家的对话陷阱
			self.__trapID = 0

	def __onEntitiesTrapThrough( self, entitiesInTrap ):
		gossiptarget = GUIFacade.getGossipTarget()							#获取当前对话NPC
		if gossiptarget and gossiptarget not in entitiesInTrap:				#如果NPC离开玩家对话陷阱
			self.__delTrap()
			self.hide()														#隐藏当前交易窗口

	def __onCurShenshouChange( self, shenshouLevel, shenshouType ):
		"""
		当前神兽改变通知
		"""
		self.__pyStAcadeLevel.text = labelGather.getText( "TongAbout:ShenShouBeckon", "ssLevel" )%shenshouLevel
		beaClassName = ""
		if shenshouType > 0: #没有召唤出来的神兽
			beaClassName = BEAS_INDEX_MAP[shenshouType]
		self.beckonIndex = shenshouType
		self.shenshouLevel = shenshouLevel
		if self.beastDatas.has_key( beaClassName ): #设置召唤出的神兽头像
			modelNums = self.beastDatas[beaClassName][1]["modelNums"]
			modelNum = ""
			if len( modelNums ) > 0:
				modelNum = modelNums[random.randint(0, len( modelNums ) - 1 )]
			self.__pyGodBeckon.icon = modelLoader.getHeadTexture( modelNum )
			self.__pyGodRender.resetModel( beaClassName )
			for attr, pyStattr in self.__pyStAttrs.iteritems():
				pyStattr.text = self.beastDatas[beaClassName][shenshouLevel][attr]
			for attrbar, pyAttrBar in self.__pyAttrBar.items():
				attrVal = self.beastDatas[beaClassName][shenshouLevel][attrbar]
				if attrVal <= 0:
					pyAttrBar.value = 0.0
				else:
					pyAttrBar.value = float( attrVal/attrVal )
			self.__pyStSelGod.text = self.beastDatas[beaClassName][shenshouLevel]["uname"]

	def __onSelectedShenshou( self, selIndex ):
		self.selIndex = selIndex
		for index, pyGodIcon in self.__pyGodIcons.items():
			pyGodIcon.selected = index == selIndex
		if self.__pyGodIcons.has_key( selIndex ):
			selPyIcon = self.__pyGodIcons[selIndex]
			modelNumber = selPyIcon.modelNumber
			self.__pyGodRender.resetModel( BEAS_INDEX_MAP[selIndex] )
		if BEAS_INDEX_MAP.has_key( selIndex ):
			className = BEAS_INDEX_MAP[selIndex]
			for attr, pyStattr in self.__pyStAttrs.iteritems():
				pyStattr.text = self.beastDatas[className][self.shenshouLevel][attr]
			for attrbar, pyAttrBar in self.__pyAttrBar.items():
				attrVal = self.beastDatas[className][self.shenshouLevel][attrbar]
				if attrVal <= 0:
					pyAttrBar.value = 0.0
				else:
					pyAttrBar.value = float( attrVal/attrVal )
		if BigWorld.player().tong_getCanUseMoney() >= 500000:
			reqmoney_color = ( 230, 227, 185 )
		else:
			reqmoney_color = ( 255, 0, 0 )
		priceText = utils.currencyToViewText( 500000 )
		req_Expense = labelGather.getText( "TongAbout:ItemReSearch", "consumMoney" )%( PL_Space.getSource( 4 ), priceText )
		req_Expense = PL_Font.getSource( req_Expense, fc = ( 230, 227, 185, 255 ) )
		req_Expense += PL_Font.getSource( fc = self.__defaultColor )
		self.__pyRtResReq.text = req_Expense
		self.__pyBeckonBtn.enable = selIndex != self.beckonIndex

	def __onBreckonSucc( self, index ):
		pass

	def __onTongMoneyChange( self, money ):
		"""
		帮会资金发生变化
		"""
		if not BigWorld.player().inWorld or not self.visible:
			return
		self.__showTongCanUseMoney()

	def __onReciveBuildInfo( self, buildData ):
		if buildData["type"] == csdefine.TONG_BUILDING_TYPE_JK: #金库信息
			self.__showTongCanUseMoney()

	def __showTongCanUseMoney( self ):
		"""
		显示帮会可用资金
		"""
		money = BigWorld.player().tong_getCanUseMoney()
		self.__pyRtUseMoney.text = utils.currencyToViewText( money )

	def __turnLeft( self ):
		BigWorld.cancelCallback( self.__turnModelCBID )
		self.__turnModel( False )
		LastKeyUpEvent.attach( self.__onLastKeyUpEvent )
		return True

	def __turnRight( self ):
		BigWorld.cancelCallback( self.__turnModelCBID )
		self.__turnModel( True )
		LastKeyUpEvent.attach( self.__onLastKeyUpEvent )
		return True

	def __turnModel( self, isRTurn ) :
		"""
		turning model on the mirror
		"""
		self.__pyGodRender.yaw += ( isRTurn and -0.1 or 0.1 )
		if BigWorld.isKeyDown( KEY_LEFTMOUSE ) :
			self.__turnModelCBID = BigWorld.callback( 0.1, Functor( self.__turnModel, isRTurn ) )

	def __onLastKeyUpEvent( self, key, mods ) :
		if key != KEY_LEFTMOUSE : return
		BigWorld.cancelCallback( self.__turnModelCBID )
		LastKeyUpEvent.detach( self.__onLastKeyUpEvent )

	def __beckonShenShou( self ):
		player = BigWorld.player()
		if self.selIndex == 0: return #第一个神兽为出战
		if self.selIndex == self.beckonIndex:
			# "该神兽已经存在!"
			showAutoHideMessage( 3.0, 0x09a1, mbmsgs[0x0c22] )
			return
		elif not player.tong_checkDutyRights( player.tong_grade, csdefine.TONG_RIGHT_PET_SELECT ):
			# "你没有选择神兽的权限!"
			showAutoHideMessage( 3.0, 0x09a2, mbmsgs[0x0c22] )
			return
		elif player.tong_getCanUseMoney() < 500000:
			# "帮会资金不足,选择或者更换神兽需要%d金帮会资金。"
			showAutoHideMessage( 3.0, mbmsgs[0x09a3] % 50, mbmsgs[0x0c22] )
			return

		player.tong_shenShouSelect( self.selIndex )

	# -----------------------------------------------------------
	# public
	# -----------------------------------------------------------
	def onEvent( self, macroName, *args ) :
		self.__triggers[macroName]( *args )

	def show( self ):
		self.__showTongCanUseMoney()
		self.__onSelectedShenshou( 0 )
		self.__pyGodRender.enableDrawModel()
		Window.show( self )

	def hide( self ):
		Window.hide( self )
		GUIFacade.cancelTurnCB( GUIFacade.getGossipTarget() )

	def onLeaveWorld( self ) :
		self.selIndex = 0
		self.beckonIndex = 0
		self.__pyGodRender.clearModel()
		self.__pyGodRender.disableDrawModel()
		for pyGodIcon in self.__pyGodIcons.itervalues():
			pyGodIcon.selected = False
		self.hide()

# -----------------------------------------------
from guis.common.PyGUI import PyGUI
from guis.controls.SelectableButton import SelectableButton

class GodIcon( SelectableButton ):
	def __init__( self, godIcon, index ):
		SelectableButton.__init__( self, godIcon )
		self.__pyIconBg = PyGUI( godIcon.iconBg )
		self.__pyGodIcon = Icon( godIcon.icon )
		self.__pyGodIcon.focus = True
		self.__pyGodIcon.crossFocus = True
		self.__pyGodIcon.onLClick.bind( self.__onGodSelected )
		self.__pyGodIcon.onMouseEnter.bind( self.__onGodEnter )
		self.__pyGodIcon.onMouseLeave.bind( self.__onGodLeave )
		if hasattr( godIcon,"cover" ):
			self.__pyCover = PyGUI( godIcon.cover )
			self.__pyCover.visible = False
		self.__selected = False
		self.index = index
		self.__modelNumber = ""

	def __onGodSelected( self, pyGodIcon ):
		if pyGodIcon is None:return
		ECenter.fireEvent( "EVT_ON_TONG_SHENSHOU_SLECTED", self.index )
		toolbox.infoTip.hide()

	def __onGodEnter( self, pyIcon ): #显示神兽信息
		beaDsp = self.getTongBeaDsp( self.index )
		toolbox.infoTip.showItemTips( self, beaDsp )

	def __onGodLeave( self, pyIcon ):
		toolbox.infoTip.hide()

	def getTongBeaDsp( self, selIndex ): #读取帮会建筑描述
		return tongBeastDsp.Datas[ selIndex ]
	# ----------------------------------------------------
	def __select( self ):
		if self.__pyCover:
			self.__pyCover.visible = True

	def __deselect( self ):
		if self.__pyCover:
			self.__pyCover.visible = False

	def _getSelected( self ):
		return self.__selected

	def _setSelected( self, selected ):
#		self.__pyReItem.selected = selected
		if selected:
			self.__select()
		else:
			self.__deselect()
		self.__selected = selected

	def _getGodTexture( self ):
		return self.__pyGodIcon.icon

	def _setGodTexure( self, texture ):
		self.__pyGodIcon.icon = texture

	def _getModelNumber( self ):
		return self.__modelNumber

	def _setModelNumber( self, modelNumber ):
		self.__modelNumber = modelNumber

	selected = property( _getSelected, _setSelected )
	godTexture = property( _getGodTexture, _setGodTexure )
	modelNumber = property( _getModelNumber, _setModelNumber )

class AttrItem( PyGUI ):
	def __init__( self, item ):
		PyGUI.__init__( self, item )
		self.__pyAttrText = StaticText( item.attrText )
		self.__pyStAttr = StaticText( item.attrValue )
		self.__pyStAttr.color = 255, 255, 255
		self.__pyStAttr.text = ""

	def _getText( self ):
		return self.__pyStAttr.text

	def _setText( self, text ):
		self.__pyStAttr.text = text

	def _getTitle( self ):
		return self.__pyAttrText.text

	def _setTitle( self, title ):
		self.__pyAttrText.text = title

	text = property( _getText, _setText )
	title = property( _getTitle, _setTitle )


supported_services = ["deezer"]
class Song:
    def __init__(self, id, service='Unknown',
                 title='Unkown',artist='Unknown',
                 length='Unknown', date='Unknown',
                 added = '', album='Unknown', cover = 'Unknown',
                 file = ''):
        self.id = id
        if service in supported_services:
            self.service = service

        self.title = title
        self.service = service
        self.artist = artist
        self.length = length
        self.date = date
        self.added = added
        self.album = album
        self.file = file

class Playlist:
    def __init__(self, id, service='', name='', tracks = [], link = ''):
        self.id = id
        if service in supported_services:
            self.service = service
        else:
            if service:
                raise ValueError(service + ' is not a valid or compatible service.')
        self.name = name
        self.tracks = tracks
        self.link = link

if __name__ == '__main__':
    pass


# Databricks notebook source
import os
import pandas as pd
import numpy as np
import base64
from tensorflow import keras
import seaborn as sns

from keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.resnet_v2 import ResNet50V2
from sklearn.preprocessing import minmax_scale
from sklearn.metrics import classification_report
from IPython.display import HTML

# COMMAND ----------

container ="data1"
data_path = f"abfss://{container}@capstone2023cuhk.dfs.core.windows.net/"
df_2021 = spark.read.csv(os.path.join(data_path, "AML-data", "Actual_Data_2021-2022", "ITEM_MATCHING_2021_BOOKS.csv"), header=True)
df_2022 = spark.read.csv(os.path.join(data_path, "AML-data", "Actual_Data_2021-2022", "ITEM_MATCHING_2022_BOOKS.csv"), header=True)
df_2021 = df_2021.toPandas()
df_2022 = df_2022.toPandas()

# Img
df_img_2021 = pd.DataFrame(dbutils.fs.ls("/FileStore/Bookimages"))
df_img_2021["PRODUCT_ID"] = df_img_2021["name"].apply(lambda x: x.replace(".jpg", ""))
df_img_2022 = pd.DataFrame(dbutils.fs.ls("/FileStore/Bookimages2022"))
df_img_2022["PRODUCT_ID"] = df_img_2022["name"].apply(lambda x: x.replace(".jpg", ""))

# COMMAND ----------

# Get only usable img: size != 807
df_img_2021 = df_img_2021[df_img_2021["size"] != 807]
df_img_2022 = df_img_2022[df_img_2022["size"] != 807]

df_img_2021 = df_img_2021[["PRODUCT_ID", "name"]]
df_img_2022 = df_img_2022[["PRODUCT_ID", "name"]]

df_2021 = df_2021[df_2021['PRICE']!='0']
df_2021 = df_2021[~df_2021['QTY_SALES'].isin([None, 'MBI       '])]
df_2022 = df_2022[df_2022['PRICE']!='0']
df_2022 = df_2022[~df_2022['QTY_SALES'].isin([None, 'MBI       '])]

df_2021["QTY_SALES"] = df_2021["QTY_SALES"].astype("int64")
df_2022["QTY_SALES"] = df_2022["QTY_SALES"].astype("int64")
df_2021["STD_SALES"] = minmax_scale(df_2021["QTY_SALES"], feature_range=(0, 1), axis=0, copy=True)
df_2022["STD_SALES"] = minmax_scale(df_2022["QTY_SALES"], feature_range=(0, 1), axis=0, copy=True)
# Remove outlier
# df_2021 = df_2021[df_2021['STD_SALES'] < df_2021["STD_SALES"].quantile(0.85)]

# COMMAND ----------

# Pre-processing
df_2021["PRODUCT_ID"] = df_2021["PRODUCT_ID"].apply(lambda x: x.strip())
df_2022["PRODUCT_ID"] = df_2022["PRODUCT_ID"].apply(lambda x: x.strip())

# COMMAND ----------

df_train = df_2021[["PRODUCT_ID", "QTY_SALES", "STD_SALES"]].merge(df_img_2021, on="PRODUCT_ID")
df_test = df_2022[["PRODUCT_ID", "QTY_SALES", "STD_SALES"]].merge(df_img_2022, on="PRODUCT_ID")

# COMMAND ----------

def categorizeSales(threshold, n):
    return "1" if n >= threshold else "0"

df_train["QTY_SALES"] = df_train["STD_SALES"].apply(lambda x: categorizeSales(0.1, x))
df_test["QTY_SALES"] = df_test["STD_SALES"].apply(lambda x: categorizeSales(0.1, x))

# COMMAND ----------

# ResNet50V2
predict_datagen = keras.preprocessing.image.ImageDataGenerator(rescale=1./255)

pred_train_generator=predict_datagen.flow_from_dataframe(
    dataframe=df_train,
    directory="/dbfs/FileStore/Bookimages/",
    x_col="name",
    y_col="QTY_SALES",
    batch_size=32,
    class_mode="binary",
    target_size=(224,224))

pred_test_generator=predict_datagen.flow_from_dataframe(
    dataframe=df_test,
    directory="/dbfs/FileStore/Bookimages2022/",
    x_col="name",
    y_col="QTY_SALES",
    batch_size=32,
    class_mode="binary",
    target_size=(224,224))

# COMMAND ----------

base_model = ResNet50V2(weights='imagenet')

# COMMAND ----------

train_pred = base_model.predict(pred_train_generator,pred_train_generator.n//pred_train_generator.batch_size)
test_pred = base_model.predict(pred_test_generator,pred_test_generator.n//pred_test_generator.batch_size)

# COMMAND ----------

df_train_output = df_train.copy(deep=True)
df_train_output["pred"] = keras.applications.resnet50.decode_predictions(
    train_pred, top=1
)
df_train_output = df_train_output[["PRODUCT_ID", "pred"]]

df_test_output = df_test.copy(deep=True)
df_test_output["pred"] = keras.applications.resnet50.decode_predictions(
    test_pred, top=1
)
df_test_output = df_test_output[["PRODUCT_ID", "pred"]]

# COMMAND ----------

train_cls_table_csv = "resnet50v2_train_output.csv"
test_cls_table_csv = "resnet50v2_test_output.csv"

df_train_output.to_csv("/tmp/{}".format(train_cls_table_csv), index=False)
df_test_output.to_csv("/tmp/{}".format(test_cls_table_csv), index=False)

# COMMAND ----------

dbutils.fs.cp("file:/tmp/{}".format(train_cls_table_csv), "dbfs:/FileStore/output/{}".format(train_cls_table_csv))
dbutils.fs.cp("file:/tmp/{}".format(test_cls_table_csv), "dbfs:/FileStore/output/{}".format(test_cls_table_csv))

# COMMAND ----------

def csv_download_button(df, fname):
    csv = df.to_csv(index=False) #if no filename is given, a string is returned
    b64 = base64.b64encode(csv.encode()).decode()
    href = f'Download CSV File: {fname}'
    display(HTML(href))

# COMMAND ----------

csv_download_button(df_train_output, train_cls_table_csv)
csv_download_button(df_test_output, test_cls_table_csv)

# COMMAND ----------

display(dbutils.fs.ls("/FileStore/output"))

# COMMAND ----------

csv_download_button(df_2021, "df_2021.csv")
csv_download_button(df_2022, "df_2022.csv")

# COMMAND ----------


import json
import pickle
import re

from bs4 import BeautifulSoup, SoupStrainer
from six.moves import urllib_parse
from resources.lib.ui import control, database
from resources.lib.ui.jscrypto import jscrypto
from resources.lib.ui.BrowserBase import BrowserBase
from resources.lib.indexers.malsync import MALSYNC

class sources(BrowserBase):
    _BASE_URL = 'https://aniwatch.to/'
    keyurl = 'https://raw.githubusercontent.com/Claudemirovsky/keys/e1/key'
    keyhints = [[106, 7], [109, 6], [137, 7], [147, 7]]

    def get_sources(self, anilist_id, episode, get_backup):
        show = database.get_show(anilist_id)
        kodi_meta = pickle.loads(show.get('kodi_meta'))
        title = kodi_meta.get('name')
        title = self._clean_title(title)
        keyword = title

        all_results = []
        srcs = ['sub', 'dub']
        if control.getSetting('general.source') == 'Sub':
            srcs.remove('dub')
        elif control.getSetting('general.source') == 'Dub':
            srcs.remove('sub')

        items = MALSYNC().get_slugs(anilist_id=anilist_id, site='Zoro')
        if not items:
            if kodi_meta.get('start_date'):
                year = kodi_meta.get('start_date').split('-')[0]
                keyword += ' {0}'.format(year)

            headers = {'Referer': self._BASE_URL}
            params = {'keyword': keyword}
            res = database.get(
                self._get_request,
                8,
                self._BASE_URL + 'search',
                data=params,
                headers=headers
            )

            mlink = SoupStrainer('div', {'class': 'flw-item'})
            mdiv = BeautifulSoup(res, "html.parser", parse_only=mlink)
            sdivs = mdiv.find_all('h3')
            sitems = []
            for sdiv in sdivs:
                try:
                    slug = sdiv.find('a').get('href').split('?')[0]
                    stitle = sdiv.find('a').get('data-jname')
                    sitems.append({'title': stitle, 'slug': slug})
                except AttributeError:
                    pass

            if sitems:
                if title[-1].isdigit():
                    items = [x.get('slug') for x in sitems if title.lower() in x.get('title').lower()]
                else:
                    items = [x.get('slug') for x in sitems if (title.lower() + '  ') in (x.get('title').lower() + '  ')]
                if not items and ':' in title:
                    title = title.split(':')[0]
                    items = [x.get('slug') for x in sitems if (title.lower() + '  ') in (x.get('title').lower() + '  ')]

        if items:
            slug = items[0]
            all_results = self._process_aw(slug, title=title, episode=episode, langs=srcs)

        return all_results

    def _process_aw(self, slug, title, episode, langs):
        sources = []
        headers = {'Referer': self._BASE_URL}
        r = database.get(
            self._get_request,
            8,
            self._BASE_URL + 'ajax/v2/episode/list/' + slug.split('-')[-1],
            headers=headers,
            XHR=True
        )
        res = json.loads(r).get('html')
        elink = SoupStrainer('div', {'class': re.compile('^ss-list')})
        ediv = BeautifulSoup(res, "html.parser", parse_only=elink)
        items = ediv.find_all('a')
        e_id = [x.get('data-id') for x in items if x.get('data-number') == episode]
        if e_id:
            params = {'episodeId': e_id[0]}
            r = database.get(
                self._get_request,
                8,
                self._BASE_URL + 'ajax/v2/episode/servers',
                data=params,
                headers=headers,
                XHR=True
            )
            eres = json.loads(r).get('html')
            for lang in langs:
                elink = SoupStrainer('div', {'class': re.compile('servers-{0}$'.format(lang))})
                sdiv = BeautifulSoup(eres, "html.parser", parse_only=elink)
                srcs = sdiv.find_all('div', {'class': 'item'})
                for src in srcs:
                    edata_id = src.get('data-id')
                    edata_name = src.text.strip().lower()
                    eserver_id = src.get('data-server-id')
                    if edata_name in ['megacloud', 'vidstreaming', 'streamtape']:
                        params = {'id': edata_id}
                        r = self._get_request(
                            self._BASE_URL + 'ajax/v2/episode/sources',
                            data=params,
                            headers=headers,
                            XHR=True
                        )
                        slink = json.loads(r).get('link')
                        if edata_name == 'streamtape':
                            source = {
                                'release_title': '{0} - Ep {1}'.format(title, episode),
                                'hash': slink,
                                'type': 'embed',
                                'quality': 'EQ',
                                'debrid_provider': '',
                                'provider': 'aniwatch',
                                'size': 'NA',
                                'info': ['DUB' if lang == 'dub' else 'SUB', edata_name],
                                'lang': 2 if lang == 'dub' else 0,
                            }
                            sources.append(source)
                        else:
                            headers = {'Referer': slink}
                            sl = urllib_parse.urlparse(slink)
                            spath = sl.path.split('/')
                            spath.insert(2, 'ajax')
                            sid = spath.pop(-1)
                            eurl = '{}://{}{}/getSources'.format(sl.scheme, sl.netloc, '/'.join(spath))
                            params = {'id': sid}
                            res = self._get_request(
                                eurl,
                                data=params,
                                headers=headers,
                                XHR=True
                            )
                            res = json.loads(res)
                            subs = res.get('tracks')
                            if subs:
                                subs = [{'url': x.get('file'), 'lang': x.get('label')} for x in subs if x.get('kind') == 'captions']
                            if res.get('encrypted'):
                                slink = self._process_link(res.get('sources'))
                            else:
                                slink = res.get('sources')[0].get('file')
                            if not slink:
                                continue
                            res = self._get_request(slink, headers=headers)
                            quals = re.findall(r'#EXT.+?RESOLUTION=\d+x(\d+).+\n(?!#)(.+)', res)

                            for qual, qlink in quals:
                                qual = int(qual)
                                if qual < 577:
                                    quality = 'NA'
                                elif qual < 721:
                                    quality = '720p'
                                elif qual < 1081:
                                    quality = '1080p'
                                else:
                                    quality = '4K'

                                source = {
                                    'release_title': '{0} - Ep {1}'.format(title, episode),
                                    'hash': urllib_parse.urljoin(slink, qlink) + '|User-Agent=iPad',
                                    'type': 'direct',
                                    'quality': quality,
                                    'debrid_provider': '',
                                    'provider': 'aniwatch',
                                    'size': 'NA',
                                    'info': ['DUB' if lang == 'dub' else 'SUB', 'Server ' + eserver_id],
                                    'lang': 2 if lang == 'dub' else 0,
                                    'subs': subs
                                }
                                sources.append(source)
        return sources

    def _process_link(self, sources):
        r = database.get(
            self._get_request,
            0.2,
            self.keyurl
        )
        keyhints = json.loads(r) or self.keyhints
        try:
            key = ''
            orig_src = sources
            y = 0
            for m, p in keyhints:
                f = m + y
                x = f + p
                key += orig_src[f:x]
                sources = sources.replace(orig_src[f:x], '')
                y += p
            sources = json.loads(jscrypto.decode(sources, key))
            return sources[0].get('file')
        except:
            database.remove(
                self._get_request,
                self.keyurl
            )
            control.log('decryption key not working')
            return ''


#!/usr/bin/python3
"""
This module includes the base class
"""
import json
import csv
import turtle

class Base:
    """
    The base class
    """
    __nb_objects = 0

    def __init__(self, id=None):
        """
        initialization function
        assign the id of the object if provided
        otherwise assign with the next object number
        """

        if id:
            self.id = id
        else:
            Base.nb_objects = self.nb_objects + 1
            self.id = self.nb_objects

    @property
    def nb_objects(self):
        """
        getter function of nb_objects
        private attribute (number of objects)
        """

        return self.__nb_objects

    @nb_objects.setter
    def nb_objects(self, value):
        """
        number of objects setter
        """

        if not isinstance(value, int):
            raise TypeError("Error: value must be an integer")
        else:
            self.__nb_objects = value

    @staticmethod
    def to_json_string(list_dictionaries):
        """
        return json representation
        of list_dictionaries
        """

        if list_dictionaries and len(list_dictionaries):
            return json.dumps(list_dictionaries)
        else:
            return "[]"

    @classmethod
    def save_to_file(cls, list_objs):
        """
        write json representation of instances
        to file
        """

        file_name = f"{cls.__name__}.json"
        list_dictionaries = []
        if list_objs is not None:
            list_dictionaries = [obj.to_dictionary() for obj in list_objs]

        list_json = Base.to_json_string(list_dictionaries)
        with open(file_name, 'w', encoding='utf-8') as file:
            file.write(list_json)

    @staticmethod
    def from_json_string(json_string):
        """
        return json representation of
        json string
        """

        if json_string and len(json_string):
            return json.loads(json_string)
        else:
            return []

    @classmethod
    def create(cls, **dictionary):
        """
        return Rectangle instance
        from dictionary
        """

        instance = None
        if cls.__name__ == "Rectangle":
            instance = cls(2, 1)
        else:
            instance = cls(1)

        instance.update(**dictionary)
        return instance

    @classmethod
    def load_from_file(cls):
        """
        return an instances of cls
        from json file
        """

        file_string = None
        file_name = f"{cls.__name__}.json"
        try:
            with open(file_name, 'r', encoding='utf-8') as file:
                file_string = file.read()
        except FileNotFoundError:
            return []

        list_objs = cls.from_json_string(file_string)

        list_instances = list()
        for obj in list_objs:
            list_instances.append(cls.create(**obj))

        return list_instances

    @classmethod
    def save_to_file_csv(cls, list_objs):
        """
        saves list of objects
        to csv file
        """

        file_name = f"{cls.__name__}.csv"

        list_dictionaries = []
        if list_objs is not None:
            list_dictionaries = [obj.to_dictionary() for obj in list_objs]

        keys = list_dictionaries[0].keys()
        with open(file_name, 'w', encoding="utf-8", newline='\n') as file:
            writer = csv.DictWriter(file, fieldnames=keys)
            writer.writeheader()
            writer.writerows(list_dictionaries)

    @classmethod
    def load_from_file_csv(cls):
        """
        load objects from csv file
        """

        file_name = f"{cls.__name__}.csv"
        list_instances = []
        try:
            with open(file_name, 'r', encoding='utf-8', newline='\n') as file:
                reader = csv.DictReader(file)
                for row in reader:
                    for key, value in row.items():
                        row[key] = int(value)
                    list_instances.append(cls.create(**row))
        except FileNotFoundError:
            return list_instances

        return list_instances

    @staticmethod
    def draw(list_rectangles, list_squares):
        """
        draw a list of rectangles and squares
        """

        turtle.penup()
        for shape in list_rectangles + list_squares:
            turtle.goto(shape.x, shape.y)
            turtle.pendown()
            for _ in range(2):
                turtle.forward(shape.width)
                turtle.right(90)
                turtle.forward(shape.height)
                turtle.right(90)
            turtle.penup()

        turtle.done()


'''
@desc    The Symbios middleware manager classes.
@author  arthuchaut <arthuchaut@gmail.com>
@version 0.1.0
@date    2019-09-22
@note    0.1.0 (2019-09-22): Writed the first drafts.
'''

from typing import Union, List
from enum import Enum
from abc import ABC, abstractmethod

from .message import IncomingMessage, SendingMessage

class Event(Enum):
    '''The Event enumeration declaration.

    Allows to define the ownership of the event of a middleware.

    Attributes:
        ON_EMIT (int): Refer to the emitter.
        ON_LISTEN (int): Refer to the listener.
    '''

    ON_EMIT: int = 0
    ON_LISTEN: int = 1

class MiddlewareABC(ABC):
    '''The MiddlewareABC declaration.

    Abstract class for defining middleware.

    Attributes:
        event (int): The event associated with middleware.
    '''

    def __init__(self, event: int):
        '''The MiddlewareABC initializer.

        Args:
            event (int): The event associated with middleware.
        '''

        self.event: int = event

    @abstractmethod
    async def execute(
        self, symbios: object, message: Union[IncomingMessage, SendingMessage]
    ) -> None:
        '''Abstract method that contains the middleware processes.

        Args:
            symbios (Symbios): The Symbios instance.
            message (Union[IncomingMessage, SendingMessage]):
                The received or sending message.
        '''

        pass

class MiddlewareLibrary:
    '''The MiddlewareLibrary class declaration.

    Allows to register the defined middlewares.

    Attributes:
        _library (Dict[int, List[MiddlewareABC]]): The library that
            contains all declared middlewares.
    '''

    def __init__(self):
        '''The MiddlewareLibrary initializer.
        '''

        self._library: Dict[int, List[MiddlewareABC]] = {
            Event.ON_EMIT: [],
            Event.ON_LISTEN: [],
        }

    def append(self, midd: MiddlewareABC) -> None:
        '''Register a middleware to the library.

        Args:
            midd (MiddlewareABC): The middleware instance to register.

        Raises:
            MiddlewareLibraryError: If the midd argument is not a subclass
                of MiddlewareABC.
        '''

        if not isinstance(midd, MiddlewareABC):
            raise MiddlewareLibraryError(
                f'Expected an instance of MiddlewareABC, {type(midd)} given.'
            )

        self._library[midd.event].append(midd)

    async def run_until_end(
        self, symbios: object, message: SendingMessage, event: int
    ) -> None:
        '''Call all defined middlewares associated the event specified.

        Args:
            symbios (Symbios): The Symbios instance.
            message (Union[IncomingMessage, SendingMessage]): The
                listener/emiter message.
        '''

        for midd in self._library[event]:
            await midd.execute(symbios, message)

class MiddlewareLibraryError(Exception):
    '''The MiddlewareLibraryError exception class.
    '''

    def __init__(self, message: str):
        '''The MiddlewareLibraryError initializer.

        Args:
            message (str): The message to raise.
        '''

        super().__init__(message)


from django.shortcuts import render

from django.core.paginator import Paginator

# Create your views here.
from .models import patients
from .filters import patientsfilter

def show_all_patients_page(request):

    context = {}

    filtered_patients = patientsfilter(
        request.GET,
        queryset=patients.objects.all()

    )

    context['filtered_patients'] = filtered_patients

    paginated_filtered_patients = Paginator(filtered_patients.qs, 5)
    page_number = request.GET.get('page')
    patients_page_obj = paginated_filtered_patients.get_page(page_number)

    context['patients_page_obj'] = patients_page_obj

    return render(request, 'myapp/show_all_patients_page.html', context=context)


lst = [
    {
        'name': 'Nished',
        'occupation': 'Doctor',
        'number': 123123,
        'salary': 500000,
    },
    {
        'name': 'Suyog',
        'occupation': 'Engineer',
        'number': 1123143123,
        'salary': 400000,
    },
    {
        'name': 'Kedar',
        'occupation': 'Engineer',
        'number': 11231412412,
        'salary': 600000,
    }
]

def getItemName(obj):
    if obj['occupation'] == 'Engineer':
        return 'Er. ' + obj['name']
    else:
        return obj['name']

engineers = [
    getItemName(obj)
    for obj in lst
]
print(engineers)


#!/usr/bin/env python

from typing import Dict, Any, List, Set
from math import ceil
from itertools import chain

import aiohttp
import asyncio
import re
import time

THREADS = ['qlfg', 'ocfc', 'qwjo', 'qxmo', 'bszzx', 'buacb', 'bvaty']

PATTERN = r'(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])'

EXCLUDE_EXT = ['.png', '.jpg', '.jpeg', '.mp4', '.webm', '.avi', '.bmp', '.htm', '.gif', '.dll', '.bms', '.aspx']
EXCLUDE_DOM = ['imgur.com', 'youtube.com', 'steamusercontent.com', 'wikia.com', 'gyazo.com', 'tinypic.com',
               'howtogeek.com', 'facepunch', 'imgbox.com', 'youtu.be', 'discord.gg']
EXLUCDE_PAT = [r'http://www$', r'^https://t\.co']

async def thread_count(session: aiohttp.ClientSession, id: str) -> int:
  """Get the number of pages in a thread."""
  async with session.get(f'https://forum.facepunch.com/dev/{id}/1/?json=1') as response:
    json = await response.json()
    name = json['Thread']['Name']
    count = ceil(json['Page']['Total'] / 30)

    print(f'Found {count} pages in {name} ({id})')

    return count

async def fetch_page(session: aiohttp.ClientSession, id: str, page: int) -> Dict[str, Any]:
  """Fetch a specific page of a thread."""
  try:
    async with session.get(f'https://forum.facepunch.com/dev/{id}/{page}/?json=1') as response:
      return await response.json()
  except aiohttp.client_exceptions.ContentTypeError:
    return dict(Posts=list())

async def fetch_thread(session: aiohttp.ClientSession, id: str) -> List[Dict[str, Any]]:
  """Fetch all the pages of a Facepunch thread by its ID."""
  count = await thread_count(session, id)

  pages = await asyncio.gather(*[fetch_page(session, id, page) for page in range(1, count + 1)])

  return pages

def parse_post(post: Dict[str, Any]) -> List[str]:
  """Find all URLs within a post."""
  matches: List[str] = re.findall(PATTERN, post['Message'], re.I | re.M)

  return matches

async def main():
  async with aiohttp.ClientSession() as session:
    threads = await asyncio.gather(*[fetch_thread(session, id) for id in THREADS])
    parsed = [parse_post(post) for thread in threads for page in thread for post in page['Posts']]
    count = 0

    with open('links.txt', 'w') as f:
      used: Set[str] = set()
      for thread in parsed:
        if thread is None:
          continue
        for link in thread:
          lower = link.lower()
          if lower in used:
            continue
          if any(ext in lower for ext in EXCLUDE_EXT):
            continue
          if any(domain in lower for domain in EXCLUDE_DOM):
            continue
          if any(re.match(pattern, link, re.I) for pattern in EXLUCDE_PAT):
            continue

          f.write(f'{link}\n')
          used.add(lower)
          count += 1

    print(f'Wrote {count} links to \'links.txt\'')

if __name__ == '__main__':
  loop = asyncio.get_event_loop()

  try:
    loop.run_until_complete(main())
  except Exception as error:
    loop.close()
    raise error


# 1. 나의 초견
t = int(input())
for _ in range(t):
    n, m = map(int, input().split())
    temp = list(map(int, input().split()))
    data = []
    for i in range(0, len(temp), m):
        data.append(temp[i:i + m])

    for j in range(1, m):
        for i in range(n):
            prev_max = data[i][j - 1]
            if i > 0:
                prev_max = max(prev_max, data[i - 1][j - 1])
            if i < n - 1:
                prev_max = max(prev_max, data[i + 1][j - 1])
            data[i][j] += prev_max
    result = 0
    for i in range(n):
        result = max(result, data[i][m - 1])
    print(result)

# 2. 책의 풀이
# 테스트 케이스(Test Case) 입력
for tc in range(int(input())):
    # 금광 정보 입력
    n, m = map(int, input().split())
    array = list(map(int, input().split()))

    # 다이나믹 프로그래밍을 위한 2차원 DP 테이블 초기화
    dp = []
    index = 0
    for i in range(n):
        dp.append(array[index:index + m])
        index += m

    # 다이나믹 프로그래밍 진행
    for j in range(1, m):
        for i in range(n):
            # 왼쪽 위에서 오는 경우
            if i == 0:
                left_up = 0
            else:
                left_up = dp[i - 1][j - 1]
            # 왼쪽 아래에서 오는 경우
            if i == n - 1:
                left_down = 0
            else:
                left_down = dp[i + 1][j - 1]
            # 왼쪽에서 오는 경우
            left = dp[i][j - 1]
            dp[i][j] = dp[i][j] + max(left_up, left_down, left)

    result = 0
    for i in range(n):
        result = max(result, dp[i][m - 1])

    print(result)

# 3. 얻어갈 점
'''
전형적인 점화식 유형
'''

이코테/다이나믹 프로그래밍/금광.py


from betamax import Betamax
from github_contents import GithubContents
import pathlib
import os
import json

TOKEN = os.environ.get("TEST_GITHUB_TOKEN") or "XXX"

# When recording new tests, run the test suite with
# VCR_RECORD=new_episodes TEST_GITHUB_TOKEN=... pytest -k name_of_test
# https://betamax.readthedocs.io/en/latest/record_modes.html
VCR_RECORD = os.environ.get("VCR_RECORD") or "none"

with Betamax.configure() as config:
    config.cassette_library_dir = "cassettes"

def test_read_small_file():
    github = GithubContents("simonw", "github-contents-demo", TOKEN, branch="master")
    with Betamax(github.session) as vcr:
        vcr.use_cassette("get-file")
        content, sha = github.read("hello.txt")
    assert b"hello world 3" == content
    assert "3c840b722385abe67a2cfadac6a8eaab8429a45c" == sha

def test_write_small_file():
    github = GithubContents("simonw", "github-contents-demo", TOKEN, branch="master")
    with Betamax(github.session) as vcr:
        vcr.use_cassette("write-file")
        assert (
            "3c840b722385abe67a2cfadac6a8eaab8429a45c",
            "ba288b9c0a35409bf655bc85b5b6b04db7bb7742",
        ) == github.write(
            "hello.txt",
            b"hello world 3",
            commit_message="updated by test",
            committer={"name": "Test", "email": "test"},
        )

def test_write_large():
    github = GithubContents("simonw", "github-contents-demo", TOKEN, branch="master")
    with Betamax(github.session) as vcr:
        vcr.use_cassette("write-file-large")
        assert (
            "659974e29c1fd07322c9adf7de6636c6c0c8f9d8",
            "16b273de7090b618be4924e05b5763a9f4c4c69b",
        ) == github.write_large(
            "write_large.txt",
            b"not actually large but I did use the .write_large() method",
            commit_message="written by test",
            committer={"name": "Test", "email": "test"},
        )

def test_branch_exists_returns_false_for_nonexistant_branch():
    github = GithubContents(
        "simonw", "github-contents-demo", TOKEN, branch="not-a-branch"
    )
    with Betamax(github.session) as vcr:
        vcr.use_cassette("branch-exists-false.json", record=VCR_RECORD)
        assert not github.branch_exists()

def test_branch_exists_returns_true_for_existing_branch():
    github = GithubContents(
        "simonw", "github-contents-demo", TOKEN, branch="demo-branch"
    )
    with Betamax(github.session) as vcr:
        vcr.use_cassette("branch-exists-true.json", record=VCR_RECORD)
        assert github.branch_exists()

def test_no_accidental_oauth_token_in_cassettes():
    # Need to make sure I never accidentally check my OAuth token into GitHub
    for filepath in pathlib.Path("cassettes").glob("*.json"):
        data = json.loads(filepath.read_text())
        for interaction in data["http_interactions"]:
            for header in interaction["request"]["headers"].get("Authorization") or []:
                assert "token XXX" == header


import numpy as np

class Image_Classifier():

    def __init__(self):
        self.weights = np.load('data/coefs.npy')
        self.bias_vector = np.load('data/bias.npy')

    def softmax_function(self,X_array):
        """Softmax function for activation"""
        z_exp = [np.exp(i) for i in X_array]
        sum_z_exp = sum(z_exp)
        return [i / sum_z_exp for i in z_exp]

    def predict(self, X_test,):
        """Predict the class of a image."""
        prediction_array = self.softmax_function( np.dot(X_test,self.weights) + self.bias_vector )
        y_prediction = []

        for i in prediction_array:
            label = np.where(i == np.amax(i))[0][0]
            print(label)
            if label == 0:
                y_prediction.append('cat')
            if label == 1:
                y_prediction.append('dog')
            if label == 2:
                y_prediction.append('frog')
            if label == 3:
                y_prediction.append('horse')

        return np.array(y_prediction)


import os
import pandas as pd
import time
import datetime
import pyodbc

files_uploaded = []
n_files = 0

print('starting script....')

while True:
    directory = os.listdir('C:/Users/tmansour/crons')
    print(len(directory))
    if n_files<len(directory):
        n_files = len(directory)
        for file in directory:
            if file not in files_uploaded:
                print('new update found....')
                df = pd.read_csv(f'C:/Users/tmansour/crons/{file}', sep='\t',  lineterminator='\n', names=None)
                item = df.iloc[0, 0]
                qty = df.iloc[0, 2]
                #lot = df.iloc[0, 1]
                Current_Date = datetime.datetime.today()
                conn = pyodbc.connect(r'Driver={SQL Server};Server=pgi-m2m;Database=M2Mdata25;Trusted_Connection=yes;')
                cursor = conn.cursor()
                #cursor.execute("SELECT fpartno, fqty, ftype, fctime_ts, FFROMLOC, FFROMFAC, FMATL, FUSERINFO, FTOBIN FROM intran")
                cursor.execute("SELECT fpartno, fonhand, fbinno, flocation, flot, fac FROM inonhd")
                cursor.execute(f"update inonhd SET fonhand='{qty}' WHERE fpartno = '{item}' AND fbinno = 'JUKI' AND flocation = 'JUKI' AND flot = 'JUKI'")
                conn.commit()
                conn.close()
                files_uploaded.append(file)
                print('finished updating ' + file)

    else:
        time.sleep(2)


"""
File: class_reviews.py
Name: Rosy Huang
-------------------------------
At the beginning of this program, the user is asked to input
the class name (either SC001 or SC101).
Attention: your input should be case-insensitive.
If the user input "-1" for class name, your program would show
the maximum, minimum, and average among all the inputs.
"""

def main():
    """
    This program gives the results of max, min, avg score of SC001 and SC101 classes.
    """
    a_max = 0
    a_min = 0
    a = 0
    a_total = 0
    b_max = 0
    b_min = 0
    b = 0
    b_total = 0

    while True:
        original_x = input("Which class? ")
        x = original_x.upper()
        if original_x == "-1":
            break
        if x == "SC101":
            score = int(input("Score: "))
            a_total += score
            a += 1
            a_avg = a_total / a
            if a_max == 0:
                a_max = score
                a_min = score
            if score > a_max:
                a_max = score
            if score < a_min:
                a_min = score
        if x == "SC001":
            score = int(input("Score: "))
            b_total += score
            b += 1
            b_avg = b_total / b
            if b_max == 0:
                b_max = score
                b_min = score
            if score > b_max:
                b_max = score
            if score < b_min:
                b_min = score
    if b_max == 0 and a_max == 0:
        print("No class scores were entered")
    else:
        print("=============SC001==============")
        if b_max != 0:
            print("Max (001): " + str(b_max))
            print("Min (001): " + str(b_min))
            print("Avg (001): " + str(b_avg))
        else:
            print("No score for SC001")
        print("=============SC101==============")
        if a_max != 0:
            print("Max (101): " + str(a_max))
            print("Min (101): " + str(a_min))
            print("Avg (101): " + str(a_avg))
        else:
            print("No score for SC101")

# ---- DO NOT EDIT CODE BELOW THIS LINE ---- #

if __name__ == '__main__':
    main()


import random

import numpy as np
from torch.nn import MSELoss
from ignite.engine import Events
from ignite.handlers import ModelCheckpoint, EarlyStopping
from transformers import AdamW, get_linear_schedule_with_warmup

from handler import *
from param import model_param, train_param
from model import Model
from metric import metric
from dataset import train_loader, dev_loader

def train():
    set_seed(train_param.seed)
    model = Model(model_param)
    optimizer = AdamW(model.parameters(), lr=train_param.lr, eps=1e-8)
    update_steps = train_param.epoch * len(train_loader)
    scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=0, num_training_steps=update_steps)
    loss_fn = [translate, MSELoss()]
    device = torch.device(f'cuda:{train_param.device}')
    trainer = create_trainer(model, optimizer, scheduler, loss_fn, train_param.grad_norm, device)
    train_evaluator = create_evaluator(model, metric, device)
    dev_evaluator = create_evaluator(model, metric, device)
    trainer.add_event_handler(Events.ITERATION_COMPLETED(every=train_param.interval), log_training_loss)
    trainer.add_event_handler(Events.EPOCH_COMPLETED, log_results, *(train_evaluator, train_loader, 'Train'))
    trainer.add_event_handler(Events.EPOCH_COMPLETED, log_results, *(dev_evaluator, dev_loader, 'Dev'))
    es_handler = EarlyStopping(patience=train_param.patience, score_function=score_fn, trainer=trainer)
    dev_evaluator.add_event_handler(Events.COMPLETED, es_handler)
    ckpt_handler = ModelCheckpoint(train_param.save_path, '', score_function=score_fn,
                                   score_name='score', require_empty=False)
    dev_evaluator.add_event_handler(Events.COMPLETED, ckpt_handler, {'model': model, 'param': model_param})
    print(f'Start running {train_param.save_path.split("/")[-1]} at device: {train_param.device}\t'
          f'lr: {train_param.lr}')
    trainer.run(train_loader, max_epochs=train_param.epoch)

def set_seed(seed):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.device_count() > 0:
        torch.cuda.manual_seed_all(seed)

def translate(t_inputs, masks, d_outputs):
    t_inputs = t_inputs[:, 1:].unsqueeze(2)
    masks = masks[:, 1:]
    loss = -torch.log(torch.gather(d_outputs.transpose(0, 1), 2, t_inputs)).squeeze(2)
    loss = torch.sum(loss * masks) / torch.sum(masks)
    return loss

if __name__ == '__main__':
    train()


from functools import reduce

def read_input(file):
    with open(file) as f:
        return [ int(line.strip()) for line in f.readlines() ]

def diffs(adapters):
    adapters.sort()
    diffs = {}
    curr = 0
    for adapter in adapters:
        diff = adapter - curr
        if diff in diffs:
            diffs[diff] = diffs[diff] + 1
        else:
            diffs[diff] = 1
        curr = adapter
    return diffs

def get_items_in_runs(adapters):
    curr = 0
    items_in_run=1
    runs = []
    for adapter in adapters:
        if adapter == curr + 1:
            items_in_run = items_in_run + 1
        else:
            runs.append(items_in_run)
            items_in_run = 1
        curr = adapter
    runs.append(items_in_run)
    return runs

def get_combos(n):
    if n > 2:
        return sum(range(n-1))+1
    else:
        return 1

def count_combinations(adapters):
    acc = 1
    runs = get_items_in_runs(adapters)
    for run in runs:
        if run > 2:
            acc = acc * (sum(range(run-1))+1)
    return acc

adapters = read_input('input.txt')
adapters.sort()
results = diffs(adapters)
part1 =  results[1] * (results[3] + 1)
print(f"Part 1 - {part1}")
part2 = count_combinations(adapters)
print(f"Part 2 - {part2}")


#from django.contrib import admin
from django.conf.urls import url, include
from django.conf import settings
from django.conf.urls.static import serve

urlpatterns = [
    #path('admin/', admin.site.urls),
    url(r'^', include('modulos.inicio.urls', namespace='inicio')),

    url(r'^static/(?P<path>.*)$', serve, {
        'document_root': settings.STATIC_ROOT } ),

    url(r'^media/(?P<path>.*)$', serve, {
    	'document_root': settings.MEDIA_ROOT, }),
]


n = int(input())
board = [0] * n
answer = 0

def is_promising(x):
    for i in range(x):
        if board[x] == board[i] or abs(board[x] - board[i]) == abs(x - i):
            return False
    return True

def n_queens(x):
    global answer
    if x == n:
        answer += 1
        return
    else:
        for i in range(n):
            board[x] = i
            if is_promising(x):
                n_queens(x+1)

n_queens(0)
print(answer)


# parameters are index of input array
def solve(start, end):
    global input_arr
    left_idx = start
    mid_idx = (start + end) // 2
    right_idx = end
    #print('left_idx: {}, right_idx: {}'.format(left_idx, right_idx))
    if start == end:
        #print('returned: {}'.format(input_arr[start]))
        return input_arr[start]
    area = max(solve(start, mid_idx), solve(mid_idx + 1, end)) # to be determined

    #if left_idx == 0 and right_idx == len(input_arr)-1:
    #    print('this is root case')

    left_idx = right_idx = mid_idx
    minH = input_arr[mid_idx]
    lborder_flag = False
    rborder_flag = False

    # There's only one idx displacement per loop
    while lborder_flag == False or rborder_flag == False:
        if left_idx == start:
            lborder_flag = True
        if right_idx == end:
            rborder_flag = True

        # case 1 : if mid_idx is surrounded with higher heights -> proceed both idx's
        if lborder_flag == False:
            if input_arr[left_idx - 1] >= input_arr[left_idx]:
                left_idx -= 1
                area = max(area, minH * (right_idx - left_idx + 1))
                continue
        elif rborder_flag == False:
            if input_arr[right_idx + 1] >= input_arr[right_idx]:
                right_idx += 1
                area = max(area, minH * (right_idx - left_idx + 1))
                continue

        # case 2 : if mid_idx is surrounded with lower heights
        # -> compare left_idx and right_idx and proceed the higher idx
        if lborder_flag == True and rborder_flag == False:
            right_idx += 1
            minH = min(minH, input_arr[right_idx])
            area = max(area, (right_idx - left_idx + 1) * minH)
            continue
        elif lborder_flag == False and rborder_flag == True:
            left_idx -= 1
            minH = min(minH, input_arr[left_idx])
            area = max(area, (right_idx - left_idx + 1) * minH)
            continue
        elif lborder_flag == False and rborder_flag == False:
            if input_arr[left_idx - 1] <= input_arr[right_idx + 1]:
                right_idx += 1
                minH = min(minH, input_arr[right_idx])
                area = max(area, (right_idx - left_idx + 1) * minH)
                continue
            elif input_arr[left_idx - 1] >= input_arr[right_idx + 1]:
                left_idx -= 1
                minH = min(minH, input_arr[left_idx])
                area = max(area, (right_idx - left_idx + 1) * minH)
                continue
    #print('returned: {}'.format(area))
    return area

input_arr = []
while True:
    arr = list(map(int, input().split()))
    input_arr = arr[1:]
    if arr[0] == 0:
        break
    ans = solve(0, len(input_arr)-1)
    print(ans)

baekjoon_prob_step/020.09 - 6549 (DnC).py


#!/usr/bin/env python
import sys
import pandas
import numpy as np

import matplotlib.pyplot as plt

import sklearn.cluster as cluster
import sklearn.decomposition as decomposition

MANUALLY_SELECTED_ATT = [
    'Entropy_Domain',
    'argPathRatio',
    'ArgUrlRatio',
    'argDomanRatio',
    'pathurlRatio',
    'CharacterContinuityRate',
    'NumberRate_FileName',
    'domainUrlRatio',
    'NumberRate_URL',
    'pathDomainRatio',
    'NumberRate_AfterPath',
    'avgpathtokenlen',
    'URL_Type_obf_Type'
]

#More info in https://scikit-learn.org/stable/auto_examples/cluster/plot_kmeans_digits.html
def kmeans_cluster_graph(csv_file, class_column_name, n_clusters=None, selected_att=None):
    """
    Creates a kmeans cluster graph for the a dataset, using Principal component analysis (PCA) to reduce dimensions

    :param str csv_file: path to the csv file
    :param str class_column_name: the name of the csv column with the class labels
    :param int n_clusters: Number of clusters to use. Defaults to number of unique class labels of the class column
    :param list[str] selected_att: The attributes (column names) from the CSV file to
    use
    """

    # Reads CSV file with data
    dataset = pandas.read_csv(csv_file)

    # Splits CSV file by chosen atributes
    if selected_att:
        df = dataset[ selected_att ]
    else:
        df = dataset

    #Remove valuens with 'NaN'
    df.dropna(inplace=True)

    #Counts unique class labels
    if not n_clusters:
        n_clusters = len(df[class_column_name].unique())
    print(f"Using {n_clusters} clusters")

    #PDA to project the data in two dimensions for the graph
    df_data = df.drop(columns=[class_column_name])
    reduced_data = decomposition.PCA(n_components=2).fit_transform(df_data)

    #Kmeans
    kmeans = cluster.KMeans(init="k-means++", n_clusters=n_clusters)
    kmeans.fit(reduced_data)

    # Step size of the mesh. Decrease to increase the quality of the VQ.
    h = 0.02  # point in the mesh [x_min, x_max]x[y_min, y_max].

    # Plot the decision boundary. For that, we will assign a color to each
    x_min, x_max = reduced_data[:, 0].min() - 1, reduced_data[:, 0].max() + 1
    y_min, y_max = reduced_data[:, 1].min() - 1, reduced_data[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

    # Obtain labels for each point in mesh. Use last trained model.
    Z = kmeans.predict(np.c_[xx.ravel(), yy.ravel()])

    # Put the result into a color plot
    Z = Z.reshape(xx.shape)
    plt.figure(1)
    plt.clf()
    plt.imshow(
        Z,
        interpolation="nearest",
        extent=(xx.min(), xx.max(), yy.min(), yy.max()),
        cmap=plt.cm.Paired,
        aspect="auto",
        origin="lower",
    )

    plt.plot(reduced_data[:, 0], reduced_data[:, 1], "k.", markersize=2)
    # Plot the centroids as a white X
    centroids = kmeans.cluster_centers_
    plt.scatter(
        centroids[:, 0],
        centroids[:, 1],
        marker="x",
        s=169,
        linewidths=3,
        color="w",
        zorder=10,
    )
    plt.title(
        "K-means clustering on the dataset (PCA-reduced data)\n"
        "Centroids are marked with white cross"
    )
    plt.xlim(x_min, x_max)
    plt.ylim(y_min, y_max)
    plt.xticks(())
    plt.yticks(())
    plt.show()

if __name__ == '__main__':

    if len(sys.argv) < 3:
        exit("Usage: kmeans_cluster_graph.py <csv_file> <class_column_name> (opt) <n_clusters>")

    kmeans_cluster_graph(
        csv_file=sys.argv[1],
        class_column_name=sys.argv[2],
    )


from color_variables import *

calendar_view_style = {
                'color': grid_view_text,
                'background-color': grid_view_bg_background,
                'font-style': font_style,
                'height': '80px', 'width': '80px',
                'fontSize': 14,
                'border-radius': 5, #  'border':'1px solid',
                'margin-right': '10px',
                'margin-left': '2px',
                }

# Style for calender header i.e the days.
calendar_view_header_style = {
                            'font-style': font_style,
                            'color': 'black',
                            'height': 'auto',
                            'width': '80px',
                            'fontSize': 14,
                            'border-radius': 5,
                            'margin-right': '10px',
                            'margin-left': '2px',}

rows_style = {'margin-bottom': '10px' } # Give sapce at the bottom of rach row in the calendar view.

drop_down_styles = {'width': '200px','margin-bottom':'10px'}


import unittest
from music_library import Song

class TestSong(unittest.TestCase):

	def test_if_length_has_seconds_minutes_and_or_hours_only(self):

		length = '1:05:21:33'
		exc = None

		try:
			Song('title', 'artist', 'album', length)
		except Exception as err:
			exc = err

		self.assertIsNotNone(exc)
		self.assertEqual(str(exc), 'Invalid input.')

	def test_if_seconds_are_valid(self):

		length = '5:32:111'
		exc = None

		try:
			Song('title', 'artist', 'album', length)
		except Exception as err:
			exc = err

		self.assertIsNotNone(exc)
		self.assertEqual(str(exc), 'Invalid input.')

	def test_if_minutes_are_valid(self):

		length = '88:02'
		exc = None

		try:
			Song('title', 'artist', 'album', length)
		except Exception as err:
			exc = err

		self.assertIsNotNone(exc)
		self.assertEqual(str(exc), 'Invalid input.')

	def test_string_representation(self):

		song = Song('title', 'artist', 'album', '02:55')

		result = str(song)
		expected = 'title - artist from album - 02:55'

		self.assertEqual(result, expected)

	def test_equality(self):

		song1 = Song('title', 'artist', 'album', '02:55')
		song2 = Song('title', 'artist', 'album', '02:55')

		self.assertEqual(song1, song2)

	# def test_validate_sec_min_hour(self):

	# 	time1 = '05'
	# 	time2 = '23'
	# 	time3 = '8'

	# 	result1 = validate_sec_min_hour(time1)
	# 	result2 = validate_sec_min_hour(time2)
	# 	result3 = validate_sec_min_hour(time3)

	# 	self.assertEqual(result1, 5)
	# 	self.assertEqual(result2, 23)
	# 	self.assertEqual(result3, 8)

	def test_length_in_seconds(self):

		song1 = Song('title', 'artist', 'album', '02:55')
		song2 = Song('title2', 'artist2', 'album2', '02:03:55')
		song3 = Song('title3', 'artist3', 'album3', '00:58')

		secs1 = song1.length1(seconds=True)
		secs2 = song2.length1(seconds=True)
		secs3 = song3.length1(seconds=True)

		self.assertEqual(secs1, 175)
		self.assertEqual(secs2, 955)
		self.assertEqual(secs3, 58)

	def test_length_in_minutes(self):

		song1 = Song('title', 'artist', 'album', '2:55')
		song2 = Song('title2', 'artist2', 'album2', '02:03:55')
		song3 = Song('title3', 'artist3', 'album3', '00:58')

		mins1 = song1.length1(minutes=True)
		mins2 = song2.length1(minutes=True)
		mins3 = song3.length1(minutes=True)

		self.assertEqual(mins1, 2)
		self.assertEqual(mins2, 123)
		self.assertEqual(mins3, 0)

	def test_length_in_hours(self):

		song1 = Song('title', 'artist', 'album', '02:55')
		song2 = Song('title2', 'artist2', 'album2', '02:03:55')
		song3 = Song('title3', 'artist3', 'album3', '00:58')

		hours1 = song1.length1(hours=True)
		hours2 = song2.length1(hours=True)
		hours3 = song3.length1(hours=True)

		self.assertEqual(hours1, 0)
		self.assertEqual(hours2, 2)
		self.assertEqual(hours3, 0)

	def test_string_representation_of_length(self):

		song = Song('title', 'artist', 'album', '02:55')

		result = song.length1()
		expected = '02:55'

		self.assertEqual(result, expected)

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


from oidc_component import login_button as oidc_button
from oauth_component import login_button as oauth_button
import streamlit as st
from dotenv import load_dotenv
import os
load_dotenv()

#provider=os.environ['provider']
authority = os.environ['authority'] # 'https://oidc.io/oauth',
clientId = os.environ['clientId']
clientSecret = os.environ['clientSecret']
scope = os.environ['scope']

st.title('Welcome to OIDC-Streamlit')

with st.echo():
    user_info = oidc_button(clientId=clientId , clientSecret=clientSecret, authority = authority ,scope=scope)
    #user_info = oauth_button(clientId=clientId, clientSecret=clientSecret, provider=provider, scope=scope)
    if user_info:
        st.write(f'Hi {user_info["nickname"]}')
        # st.write(user_info) # some private information here

if not user_info:
    st.write("Please login to continue")


#python
from collections import namedtuple

# apetools
from apetools.baseclass import BaseClass
from apetools.commons import enumerations, errors
from parametertree import ParameterTree

AffectorTypes = enumerations.AffectorTypes
IperfDirection = enumerations.IperfDirection
ConfigurationError = errors.ConfigurationError

parameters = ("test_id repetition repetitions output_folder " +
              " receiver sender recovery_time affector").split()

class TestParameter(namedtuple('TestParameter', parameters)):
    """
    A TestParameter holds the settings for a single test-iteration
    """
    __slots__ = ()

    def __str__(self):
        return (self.__class__.__name__ + ":" +
                ','.join(("{f}:{v}".format(f=f, v=getattr(self,f))
                          for f in self._fields)))

# end class TestParameter

class ParameterGenerator(BaseClass):
    """
    A ParameterGenerator is an iterator that generates test-parameters.
    """
    def __init__(self, parameters, *args, **kwargs):
        """
        :param:

         - `parameters`: A list of parameter (namedtuple) lists
        """
        super(ParameterGenerator, self).__init__(*args, **kwargs)
        self.parameters = parameters
        self._tree = None
        return

    @property
    def tree(self):
        """
        :return: parameter-tree populated with parameters (possibly)
        """
        if self._tree is None:
            self._tree = ParameterTree(self.parameters)
        return self._tree

    def __iter__(self):
        """
        :yield: the next namedtuple of parameters
        """
        for parameters in self.tree.paths:
            yield parameters
        return
# end class ParameterGenerator


import pathlib

from collections import defaultdict

root_folder = "/home/nvaessen/data/transfer/paper_n4/aam/ablation/"
experiment_name = "speaker_wav2vec2_aam"
tag_prefix = "ablation"
test_run = False
test_sets_to_use = [
    # "voxceleb2",
    "voxceleb2_test_everyone",
    # "voxceleb2_test_hard"
]

postfix_map = {
    "voxceleb2": "o",
    "voxceleb2_test_hard": "h",
    "voxceleb2_test_everyone": "e",
}

path_dict = defaultdict(set)
for ckpt in sorted(pathlib.Path(root_folder).glob("*.ckpt")):
    first_underscore = ckpt.stem.find("_")
    first_dot = ckpt.stem.find(".")

    ablation_name = ckpt.stem[first_underscore+1:first_dot]

    path_dict[ablation_name].add(ckpt.absolute())

assert len(path_dict) == 10
for key, v in path_dict.items():
    assert len(v) == 3

for test_set in test_sets_to_use:
    for ablation_name, ckpt_set in path_dict.items():
        for ckpt in ckpt_set:
            command_template = (
                f"python run.py -m +experiment={experiment_name} "
                f"data/module={test_set} "
                f"fit_model=False "
                f"network.stat_pooling_type=first+cls "
                f"tag={ablation_name}_eval_{postfix_map[test_set]} "
                f"load_network_from_checkpoint={ckpt} "
                f"network.explicit_num_speakers=5994 "
                f"hydra/launcher=slurm "
            )

            print(f"{command_template} & ")

            if test_run:
                exit()