import sys, os
import requests
from bs4 import BeautifulSoup # scraper library

import pandas as pd # tables
from collections import OrderedDict

# Config
base_page_url = 'https://www.teamrankings.com/ncaa-basketball/stat/'
date_range = pd.date_range(pd.datetime(2018, 1, 1), periods=59).tolist()

# dictionary: output_name: url
stat_types = {
        'pts_per_game': 'points-per-game',
        'pos_per_game': 'possessions-per-game',
        'field_goals_attempted': 'field-goals-attempted-per-game',
        'field_goals_made': 'field-goals-made-per-game',
        '3pt_attempted': 'three-pointers-attempted-per-game',
        '3pt_made': 'three-pointers-made-per-game',
        'ft_per_100_pos': 'ftm-per-100-possessions',
        'off_rebounds': 'offensive-rebounds-per-game',
        'ast_per_game': 'assists-per-game',
        'to_per_game': 'turnovers-per-game',
        'fouls_per_game': 'personal-fouls-per-game',
        'opp_pts_per_game': 'opponent-points-per-game',
        'opp_pts_from_3pt': 'opponent-points-from-3-pointers',
        'opp_pts_from_2pt': 'opponent-points-from-2-pointers',
        'def_rebounds': 'defensive-rebounds-per-game',
        'blocks_per_game': 'blocks-per-game',
        'steals_per_game': 'steals-per-game',
        'opp_to_per_game': 'opponent-turnovers-per-game',
        'opp_ast_per_game': 'opponent-assists-per-game',
        }

def scrape_stats(page_url, output_name):
    stats_df = None
    stats = {}

    for date_i, date in enumerate(date_range):
        date = str(date.date())
        url = page_url + '?date=' + date

        page = requests.get(url) # load page
        soup = BeautifulSoup(page.text, 'html5lib') # parse
        table = soup.find('table', class_='datatable').find('tbody')
        rows = table.find_all('tr')

        # Go through rows
        for i in range(351):
            row = rows[i].find_all('td')
            team_name = row[1].get_text()
            stat_val = row[2].get_text()

            # Add to stats
            if team_name not in stats:
                stats[team_name] = {}

            stats[team_name][date] = stat_val

        print(f"{output_name}: Fetching date: {date} [{date_i+1}/{len(date_range)}]", end='\r')

    print()

    # Convert to pandas dataframe
    stats_df_data = [ [ team_name, *v.values() ] for team_name,v in stats.items() ]
    stats_df_columns = ['Team Name'] + list(stats[list(stats.keys())[0]].keys())
    stats_df = pd.DataFrame(data = stats_df_data, columns = stats_df_columns)

    return stats_df

# def main():
#     scrape_stats('https://www.teamrankings.com/ncaa-basketball/stat/points-per-game', 'pts_per_game')

def main():
    for (output_name, stat_url) in stat_types.items():
        # Check if file exists so we don't have to reparse the data
        if os.path.isfile(output_name + '.csv'):
            print(f"{output_name}: File exists. Skipping...")
            continue

        page_url = base_page_url + stat_url

        print(f"{output_name}: Parsing from `{page_url}`...")

        stat = scrape_stats(page_url, output_name)
        stat.to_csv(output_name + '.csv')
        print(f"{output_name}: Done.")
        print()

if __name__ == '__main__':
    main()


from django.shortcuts import render
from social.apps.django_app.middleware import SocialAuthExceptionMiddleware
from social import exceptions as social_exceptions

class SocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
    def process_exception(self, request, exception):
        if hasattr(social_exceptions, exception.__class__.__name__):
            return render(request, 'error.html', {
                'errorMessage': 'There was an authentication error.',
                'errorDetails': str(exception)
            })
            # else:
            #     raise exception


from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from .serializers import UserRegistrationSerializer

class UserRegistrationView(APIView):
    """
    API endpoint for user registration.
    """

    def post(self, request):
        """
        Handle user registration POST request.
        """
        serializer = UserRegistrationSerializer(data=request.data)
        if serializer.is_valid():
            user = serializer.save()  # Save the new user
            response_data = {
                "message": "User registered successfully",
                "user": {
                    "id": user.id,
                    "username": user.username,
                    "email": user.email,
                }
            }
            return Response(response_data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


import cv2
import numpy as py

#检测拐角
#与边缘检测不同，拐角的检测的过程稍稍有些复杂
# 。但原理相同，所不同的是先用十字形的结构元素膨胀像素，
# 这种情况下只会在边缘处“扩张”，角点不发生变化。
# 接着用菱形的结构元素腐蚀原图像，
# 导致只有在拐角处才会“收缩”，而直线边缘都未发生变化。

image = cv2.imread('img\\building.jpg', 0)
origin = cv2.imread('img\\building')

#构造5 * 5的结构元素，分别为十字形/菱形/方形/X形
cross = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))
#菱形结构元素的定义
diamond = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
diamond[0, 0] = 0
diamond[0, 1] = 0
diamond[1, 0] = 0
diamond[4, 4] = 0
diamond[4, 3] = 0
diamond[3, 4] = 0
diamond[4, 0] = 0
diamond[4, 1] = 0
diamond[3, 0] = 0
diamond[0, 3] = 0
diamond[0, 4] = 0
diamond[1, 4] = 0

square = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
x = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))
#使用cross膨胀图像
result1 = cv2.dilate(image, cross)
#使用菱形腐蚀图像
result1 = cv2.erode(image, diamond)

#使用X膨胀原图像
result2 = cv2.dilate(image, x)
#使用方形腐蚀图像
result2 = cv2.erode(image, square)

#将两幅闭运算的图像相减获得角
result = cv2.absdiff(result2, result1)
#使用阈值获得二值图
retval, result = cv2.threshold(result, 40, 255, cv2.THRESH_BINARY)

#在原图上用半径为5的圆圈将点标出
for j in range(result.size):
    y = int(j / result.shape[0])
    x = j % result.shape[0]

    if result[x, int(y)] == 255:
        cv2.circle(image, (int(y), x), 5, (255, 0, 0))

cv2.imshow("result", image)
cv2.waitKey(0)
cv2.destroyAllWindows()


from typing import Dict

import executorch.backends.qualcomm.python.PyQnnWrapperAdaptor as PyQnnWrapper

import torch

from .node_visitor import NodeVisitor, register_node_visitor
from .qnn_constants import OpDequantize, QNN_OP_PACKAGE_NAME_QTI_AISW

class DequantizeOpBase(NodeVisitor):
    def __init__(self, *args) -> None:
        super().__init__(*args)

    def define_node(
        self,
        node: torch.fx.Node,
        nodes_to_wrappers: Dict[torch.fx.Node, PyQnnWrapper.TensorWrapper],
    ) -> PyQnnWrapper.PyQnnOpWrapper:
        dequant_input_tensors = []
        input_node = node.args[0]
        input_tensor = self.get_tensor(input_node, node)
        inp_tensor_wrapper = self.define_tensor(
            input_node,
            input_tensor,
            PyQnnWrapper.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE,
            nodes_to_wrappers,
        )
        dequant_input_tensors.append(inp_tensor_wrapper)

        output_tensor = self.get_tensor(node, node)
        output_tensor_wrapper = self.define_tensor(
            node,
            output_tensor,
            PyQnnWrapper.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE,
            nodes_to_wrappers,
        )
        dequant_output_tensors = [output_tensor_wrapper]

        dequant_op = PyQnnWrapper.PyQnnOpWrapper(
            node.target.__name__,
            QNN_OP_PACKAGE_NAME_QTI_AISW,
            OpDequantize.op_name,
        )
        dequant_op.AddInputTensors(dequant_input_tensors)
        dequant_op.AddOutputTensors(dequant_output_tensors)

        return dequant_op

@register_node_visitor
class PerTensorDequantizeDefault(DequantizeOpBase):
    target = "quantized_decomposed.dequantize_per_tensor.default"

@register_node_visitor
class PerTensorDequantizeTensor(DequantizeOpBase):
    target = "quantized_decomposed.dequantize_per_tensor.tensor"

@register_node_visitor
class PerChannelDequantizeDefault(DequantizeOpBase):
    target = "quantized_decomposed.dequantize_per_channel.default"

@register_node_visitor
class PerChannelDequantizeTensor(DequantizeOpBase):
    target = "quantized_decomposed.dequantize_per_channel.tensor"


from pymavlink import mavutil
from contextlib import nullcontext

CONNECTION_STRING = "udpin:0.0.0.0:14550"
DRONE_IDS = [3, 4]

def wait_heartbeats_multi(connection):
    heartbeats = {id: False for id in DRONE_IDS}
    while not all(heartbeats.values()):
        msg = connection.recv_match(type="HEARTBEAT")
        if msg:
            heartbeats[msg.get_srcSystem()] = True

def connect():
    connection = mavutil.mavlink_connection(CONNECTION_STRING)
    connection.wait_heartbeat()
    wait_heartbeats_multi(connection)

    return connection

def recv_ack(connection):
    while True:
        msg = connection.recv_match(type="COMMAND_ACK", blocking=True)
        if msg.get_srcSystem() == connection.target_system:
            break
    print("Received ACK:", msg)

def for_all_drones(f):
    def wrapped(connection, *args, **kwargs):
        for drone in DRONE_IDS:
            connection.target_system = drone
            f(connection, *args, **kwargs)

    return wrapped

def send_command(connection, cmd, confirm, p1=0, p2=0, p3=0, p4=0, p5=0, p6=0, p7=0, lock=nullcontext(), ack=True):
    if type(cmd) == str:
        try:
            cmd = getattr(mavutil.mavlink, cmd)
        except AttributeError:
            raise AttributeError(f"Unknown command `{cmd}`")
    with lock:
        connection.mav.command_long_send(
            connection.target_system,
            connection.target_component,
            cmd,
            confirm, p1, p2, p3, p4, p5, p6, p7
        )
        if ack:
            recv_ack(connection)


# -*- coding: utf-8 -*-
import re
import sys
from setuptools import setup
from setuptools.command.test import test as TestCommand

REQUIRES = [
    'docopt',
    'argparse==1.2.1',
    'requests==2.8.1',
    'trello==0.9.1',
    'wsgiref==0.1.2',
]

class PyTest(TestCommand):
    def finalize_options(self):
        TestCommand.finalize_options(self)
        self.test_args = []
        self.test_suite = True

    def run_tests(self):
        import pytest
        errcode = pytest.main(self.test_args)
        sys.exit(errcode)

def find_version(fname):
    '''Attempts to find the version number in the file names fname.
    Raises RuntimeError if not found.
    '''
    version = ''
    with open(fname, 'r') as fp:
        reg = re.compile(r'__version__ = [\'"]([^\'"]*)[\'"]')
        for line in fp:
            m = reg.match(line)
            if m:
                version = m.group(1)
                break
    if not version:
        raise RuntimeError('Cannot find version information')
    return version

__version__ = find_version("trello2text.py")

def read(fname):
    with open(fname) as fp:
        content = fp.read()
    return content

setup(
    name='trello2text',
    version="0.1.1",
    description='Parses trello board and outputs text',
    long_description=read("README.md"),
    author='Alejandro Cirino',
    author_email='alejandro.cirino@devecoop.com',
    url='https://github.com/cirinoalejando/trello2text',
    install_requires=REQUIRES,
    license=read("LICENSE"),
    zip_safe=False,
    keywords='trello2text',
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        "Programming Language :: Python :: 2",
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: Implementation :: CPython',
        'Programming Language :: Python :: Implementation :: PyPy'
    ],
    py_modules=["trello2text", ".utils"],
    entry_points={
        'console_scripts': [
            "trello2text = trello2text:main"
        ]
    },
    tests_require=['pytest'],
    cmdclass={'test': PyTest}
)


import pandas as pd
import numpy as np
import time
import sys
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
np.set_printoptions(threshold=sys.maxsize)

df = pd.read_csv('contourQ3data.csv')
Z = df.pivot_table(index='p1', columns='p3', values='vari').T.values
X_unique = np.sort(df.p1.unique())
Y_unique = np.sort(df.p3.unique())
X, Y = np.meshgrid(X_unique, Y_unique)

fig, ax = plt.subplots()
CS = ax.contourf(X, Y, Z, cmap='RdGy')
ax.set_title('Contour plot of the behavour of avg I')
ax.set_xlabel('p1')
ax.set_ylabel('p3')
ax.set_aspect('equal')
fig.colorbar(CS, format="%.2f")
plt.show()
plt.savefig('contour-plot-of-vari-0.05.png', dpi=300)


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

class LotkaVolterra:
    """This class defines the Lotka--Voltera prey-predator
    system. There are 4 parameters in this class which
    define the evoluion of the system.

    Attributes:
        k_a     reproduction rate of the antelopes
        k_ca    death rate of antelopes when the meet cheetahs
        k_c     death rate of cheetahs
        k_a     reproduction rate of the cheetahs when they meet antelopes
    """
    def __init__(self,k_a,k_ca,k_c,k_ac):
        self.k_a = k_a
        self.k_ca = k_ca
        self.k_c = k_c
        self.k_ac = k_ac

    def __call__(self,x,t):
        y = np.zeros(len(x))
        y[0] = self.k_a*x[0]-self.k_ca*x[0]*x[1]
        y[1] = -self.k_c*x[1]+self.k_ac*x[0]*x[1]

        return y

class Logistic:
    """This class defines the Logistic population
    growth of a population which has a limited size C
    and a growth rate of nu.

    Attributes:
        nu    Growth rate of the population
        C     Limit sizeof the population
    """
    def __init__(self,nu,C):
        self.nu = nu
        self.C = C

    def __call__(self,x,t):
        return self.nu*(1-x/self.C)*x

class ExplicitEuler:
    """This class defines the Explicit Euler
    scheme for the numerical resolution of
    a differentiel equation.
    """
    def __init__(self,f):
        self.f = f

    def iterate(self,x0,t,dt):
        return x0+dt*self.f(x0,t)

class RK2:
    """This class defines the Runge-Kutta 2
    scheme for the numerical resolution of
    a differentiel equation.
    """
    def __init__(self,f):
        self.f = f

    def iterate(self,x0,t,dt):
        return x0+dt*self.f(x0+dt/2*self.f(x0,t),t+dt/2)

class Integrator:
    """This class defines the Integration
    of a differential equation between tMin and tMax
    with N discretization steps and x0 as an initial condition
    """
    def __init__(self,method,x0,tMin,tMax,N):
        self.x0   = x0
        self.tMin = tMin
        self.tMax = tMax
        self.dt   = (tMax - tMin)/(N-1)

        self.f = method

    def getIntegrationTime(self):
        return np.arange(self.tMin,self.tMax+self.dt,self.dt)

    def integrate(self):
        x = np.array([self.x0])
        for t in np.arange(self.tMin,self.tMax,self.dt):
            x = np.append( x, [self.f.iterate(x[-1,:],t,self.dt)],axis=0)
        return x

# Plots the data in a 2d plot
def plotData(x,y,color,legend):
    plt.rc('text', usetex=True)
    plt.rc('font', family='serif')

    plt.rc('xtick', labelsize=20)
    plt.rc('ytick', labelsize=20)
    plt.ylabel('$a(t),c(t)$',fontsize=20)
    plt.xlabel('$t$', fontsize=20)

    plt.plot(x,y,color,linewidth=2.0,label=legend)
    plt.legend(loc=2,prop={'size':20})

# Parametric plot of x vs y
def parametricPlotData(x,y,color,xAxis,yAxis,legend):
    plt.rc('text', usetex=True)
    plt.rc('font', family='serif')

    plt.rc('xtick', labelsize=20)
    plt.rc('ytick', labelsize=20)
    plt.xlabel('$'+xAxis+'$',fontsize=20)
    plt.ylabel('$'+yAxis+'$',fontsize=20)

    plt.plot(x,y,color,linewidth=2.0,label=legend)
    plt.legend(loc=2,prop={'size':20})

# Plot the population of the antelope and the cheetah
x0 = np.array([2, 4])
tmin = 0
tmax = 100

rk2 = Integrator(RK2(LotkaVolterra(1,1,0.5,0.5)),x0,tmin,tmax,2000)
eul = Integrator(ExplicitEuler(LotkaVolterra(1,1,0.5,0.5)),x0,tmin,tmax,2000)

plotData(rk2.getIntegrationTime(),rk2.integrate()[:,0],'r-',"antelope (RK)")
plotData(rk2.getIntegrationTime(),rk2.integrate()[:,1],'b-',"cheetah (RK)")
plotData(eul.getIntegrationTime(),eul.integrate()[:,0],'g-',"antelope (E)")
plotData(eul.getIntegrationTime(),eul.integrate()[:,1],'m-',"cheetah (E)")

plt.show()

parametricPlotData(rk2.integrate()[:,0], rk2.integrate()[:,1],'r-','a(t)','c(t)',"6 ini (RK)")
parametricPlotData(eul.integrate()[:,0], eul.integrate()[:,1],'b-','a(t)','c(t)',"6 ini (E)")

plt.show()

# Compues the errror between 2 solutions with a given ratio
# in term of resolution points
def computeError(x,xRef,ratio):
    iMax = np.size(xRef,axis=0)
    totError = 0
    for i in np.arange(0,np.size(xRef,axis=1)):
        totError += math.sqrt(np.sum(np.square(x[:,i]-xRef[0:iMax:ratio,i])))/np.size(x[:,i])

    return totError

n_rk = np.array([1000, 2000, 4000, 8000])
n_e = np.array([1000, 2000, 4000, 8000])

n_ref = 16000
tmin = 0
tmax = 13

rk2 = Integrator(RK2(LotkaVolterra(1,1,0.5,0.5)),x0,tmin,tmax,n_ref)
solRefRK = rk2.integrate()

eul = Integrator(ExplicitEuler(LotkaVolterra(1,1,0.5,0.5)),x0,tmin,tmax,n_ref)
solRefE = eul.integrate()

errRK = []
for i in n_rk:
	rk = Integrator(RK2(LotkaVolterra(1,1,0.5,0.5)),x0,tmin,tmax,i)
	r_rk = n_ref//i
	errRK.append(computeError(rk.integrate(),solRefRK,r_rk))
	print(computeError(rk.integrate(),solRefRK,r_rk))

plt.loglog(n_rk,errRK,'ro',linewidth=2.0,label="RK2 error")
plt.loglog(n_rk,np.power(n_rk/10,-2),'k-',linewidth=2.0,label="-2 slope")
plt.legend(loc=3)
plt.show()

errE = []
for i in n_rk:
	e = Integrator(ExplicitEuler(LotkaVolterra(1,1,0.5,0.5)),x0,tmin,tmax,i)
	r_rk = n_ref//i
	errE.append(computeError(e.integrate(),solRefRK,r_rk))
	print(computeError(e.integrate(),solRefRK,r_rk))

plt.loglog(n_rk,errE,'ro',linewidth=2.0,label="Euler error")
plt.loglog(n_rk,np.power(n_e/100,-2),'k-',linewidth=2.0,label="-1 slope")
plt.legend(loc=3)
plt.show()


import tkinter
from tkinter import *
from PIL import ImageTk, Image

# configure window
root = Tk()
windowColor = "#F2F2F2"
root.geometry("827x1500")
root.configure(bg = windowColor)
root.title("Train Build")

# create a container for canvas so window is scrollable
# window is a frame inside canvas that is a container for rest of application
container = Frame(root, bg = windowColor)
canvas = Canvas(container, bg = windowColor, bd=0, highlightthickness=0)
scrollbar = Scrollbar(container, orient="vertical", command=canvas.yview)
window = Frame(canvas, bg = windowColor)

# make canvas and window scrollable
window.bind(
    "<Configure>",
    lambda e: canvas.configure(
        scrollregion=canvas.bbox("all")
    )
)

canvas.create_window((0, 0), window=window, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)

# pack containers into root
container.pack(side="left", fill="both", expand=True)
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")

image1 = Image.open("Asset 4.png")
test = ImageTk.PhotoImage(image1)
label1 = tkinter.Label(image=test, width = 753, height = 355, bg=windowColor)
label1.place(x=150, y=25)

# span information frame
framea = LabelFrame(
    root,
    bg="#FFFFFF",
    bd=0,
    highlightthickness=0

)
framea.place(
    x=60,
    y=48,
    width=334,
    height=390
)

infoSpan = Label(root, text="Span Information", bg="#FFFFFF", font='Helvetica 22 bold')
infoSpan.place(x = 72, y = 64)

# start span name and label
startSpanEntry = Entry(
    bd=0,
    bg="#E6E6E6",
    fg="#000716",
    highlightthickness=0
)
startSpanEntry.configure(highlightbackground="black", highlightcolor="black")
startSpanEntry.place(
    x=292,
    y=104,
    width=87,
    height=22
)
startSpanLabel = Label(root, text="Start Span", bg="#FFFFFF")
startSpanLabel.place(x = 100, y = 108)

# end span name and label
endSpanEntry = Entry(
    bd=0,
    bg="#E6E6E6",
    fg="#000716",
    highlightthickness=0
)
endSpanEntry.configure(highlightbackground="black", highlightcolor="black")
endSpanEntry.place(
    x=292,
    y=139,
    width=87,
    height=22
)
endSpanLabel = Label(root, text="End Span", bg="#FFFFFF")
endSpanLabel.place(x = 100, y = 142)

# span increment name and label
incrementSpanEntry = Entry(
    bd=0,
    bg="#E6E6E6",
    fg="#000716",
    highlightthickness=0
)
incrementSpanEntry.configure(highlightbackground="black", highlightcolor="black")
incrementSpanEntry.place(
    x=292,
    y=174,
    width=87,
    height=22
)
incrementSpanLabel = Label(root, text="Span Increment", bg="#FFFFFF")
incrementSpanLabel.place(x = 100, y = 178)

llTypeOptionList = ["Custom", "Option", "Option", "Option"]

lltext = StringVar()
lltext.set(llTypeOptionList[0])
lloption = OptionMenu(root, lltext, *llTypeOptionList)
lloption.configure(highlightbackground="#FFFFFF")
lloption.place(x = 279, y = 215)

llTypeLabel = Label(root, text="LL Type", bg="#FFFFFF")
llTypeLabel.place(x = 100, y = 213)

infoSpan = Label(root, text="Moment/Stress Calculation Interval Locations", bg="#FFFFFF", font='Helvetica 13 bold')
infoSpan.place(x = 77, y = 248)

# start location name and label
startLocationEntry = Entry(
    bd=0,
    bg="#E6E6E6",
    fg="#000716",
    highlightthickness=0
)
startLocationEntry.configure(highlightbackground="black", highlightcolor="black")
startLocationEntry.place(
    x=292,
    y=279,
    width=87,
    height=22
)
startLocationLabel = Label(root, text="Start Location", bg="#FFFFFF")
startLocationLabel.place(x = 100, y = 283)

# end location name and label
endLocationEntry = Entry(
    bd=0,
    bg="#E6E6E6",
    fg="#000716",
    highlightthickness=0
)
endLocationEntry.configure(highlightbackground="black", highlightcolor="black")
endLocationEntry.place(
    x=292,
    y=314,
    width=87,
    height=22
)
endLocationLabel = Label(root, text="End Location", bg="#FFFFFF")
endLocationLabel.place(x = 100, y = 318)

# interval name and label
intervalEntry = Entry(
    bd=0,
    bg="#E6E6E6",
    fg="#000716",
    highlightthickness=0
)
intervalEntry.configure(highlightbackground="black", highlightcolor="black")
intervalEntry.place(
    x=292,
    y=349,
    width=87,
    height=22
)
intervalLabel = Label(root, text="Interval", bg="#FFFFFF")
intervalLabel.place(x = 100, y = 353)

# step size name and label
stepSizeEntry = Entry(
    bd=0,
    bg="#E6E6E6",
    fg="#000716",
    highlightthickness=0
)
stepSizeEntry.configure(highlightbackground="black", highlightcolor="black")
stepSizeEntry.place(
    x=292,
    y=384,
    width=87,
    height=22
)
stepSizeLabel = Label(root, text="Step Size", bg="#FFFFFF")
stepSizeLabel.place(x = 100, y = 388)

# loads frame
framec = LabelFrame(
    root,
    bg="#FFFFFF",
    bd=0,
    highlightthickness=0

)
framec.place(
    x=60,
    y=473,
    width=334,
    height=286
)

loadsDead = Label(root, text="Dead Loads", bg="#FFFFFF", font='Helvetica 22 bold')
loadsDead.place(x = 75, y = 489)

# girder type name and label

girderTypeOptionList = ["Custom", "Option", "Option", "Option"]

girdertext = StringVar()
girdertext.set(girderTypeOptionList[0])
girderoption = OptionMenu(root, girdertext, *girderTypeOptionList)
girderoption.configure(highlightbackground="#FFFFFF")
girderoption.place(x = 292, y = 529)

typeGirderLabel = Label(root, text="Girder Type", bg="#FFFFFF")
typeGirderLabel.place(x = 100, y = 533)

# deck type name and label
deckTypeOptionList = ["Custom", "Option", "Option", "Option"]

decktext = StringVar()
decktext.set(deckTypeOptionList[0])
deckoption = OptionMenu(root, decktext, *deckTypeOptionList)
deckoption.configure(highlightbackground="#FFFFFF")
deckoption.place(x = 292, y = 564)

typeDeckLabel = Label(root, text="Deck Type", bg="#FFFFFF")
typeDeckLabel.place(x = 100, y = 568)

loadsDead = Label(root, text="Cooper Loads", bg="#FFFFFF", font='Helvetica 22 bold')
loadsDead.place(x = 75, y = 597)

# cooper type name and label

cooperTypeOptionList = ["Custom", "Option", "Option", "Option"]

coopertext = StringVar()
coopertext.set(cooperTypeOptionList[0])
cooperoption = OptionMenu(root, coopertext, *cooperTypeOptionList)
cooperoption.configure(highlightbackground="#FFFFFF")
cooperoption.place(x = 292, y = 635)

typeCooperLabel = Label(root, text="Cooper Type", bg="#FFFFFF")
typeCooperLabel.place(x = 100, y = 639)

# cooper year name and label
cooperYearTypeOptionList = ["Custom", "Option", "Option", "Option"]

cooperYeartext = StringVar()
cooperYeartext.set(cooperYearTypeOptionList[0])
cooperYearoption = OptionMenu(root, cooperYeartext, *cooperYearTypeOptionList)
cooperYearoption.configure(highlightbackground="#FFFFFF")
cooperYearoption.place(x = 292, y = 670)

yearCooperLabel = Label(root, text="Cooper Year", bg="#FFFFFF")
yearCooperLabel.place(x = 100, y = 674)

# girder connection name and label
girderTypeOptionList = ["Custom", "Option", "Option", "Option"]

girdertext = StringVar()
girdertext.set(girderTypeOptionList[0])
girderoption = OptionMenu(root, girdertext, *girderTypeOptionList)
girderoption.configure(highlightbackground="#FFFFFF")
girderoption.place(x = 292, y = 705)

connectionGirderLabel = Label(root, text="Girder Connection", bg="#FFFFFF")
connectionGirderLabel.place(x = 100, y = 709)

valueImpact = Label(root, text="Impact Value", bg=windowColor, font='Helvetica 22 bold')
valueImpact.place(x = 448, y = 64)

# impact option and bridge
impactTypeOptionList = ["Unknown", "Option", "Option", "Option"]

impacttext = StringVar()
impacttext.set(impactTypeOptionList[0])
impactoption = OptionMenu(root, impacttext, *impactTypeOptionList)
impactoption.configure(highlightbackground="#FFFFFF")
impactoption.place(x = 633, y = 103)

impactTypeLabel = Label(root, text="Design Impact and Year", bg=windowColor)
impactTypeLabel.place(x = 474, y = 104)

# section modulus frame
frameb = LabelFrame(
    root,
    bg="#FFFFFF",
    bd=0,
    highlightthickness=0

)
frameb.place(
    x=433,
    y=282,
    width=334,
    height=477
)

# column labels
modulusSection = Label(root, text="Section Modulus", bg="#FFFFFF", font='Helvetica 22 bold')
modulusSection.place(x = 448, y = 300)

# section area load name and label
areaCheck1Value = IntVar()
areaCheck1 = Checkbutton(
    root,
    text = "Gross",
    variable = areaCheck1Value,
    onvalue = 1,
    offvalue = 0,
    bg = "#ffffff"
)
areaCheck1.place(
    x = 665,
    y = 341
)
areaCheck2Value = IntVar()
areaCheck2 = Checkbutton(
    root,
    text = "Net",
    variable = areaCheck2Value,
    onvalue = 1,
    offvalue = 0,
    bg = "#ffffff"
)
areaCheck2.place(
    x = 665,
    y = 368
)

areaSectionLabel = Label(root, text="Section Area", bg="#FFFFFF")
areaSectionLabel.place(x = 473, y = 340)

# number of girders name and label
girderSectionEntry = Entry(
    bd=0,
    bg="#E6E6E6",
    fg="#000716",
    highlightthickness=0
)
girderSectionEntry.configure(highlightbackground="black", highlightcolor="black")
girderSectionEntry.place(
    x=665,
    y=406,
    width=87,
    height=22
)
girderSectionLabel = Label(root, text="Number of Girders", bg="#FFFFFF")
girderSectionLabel.place(x = 473, y = 410)

# S value name and label
sValueEntry = Entry(
    bd=0,
    bg="#E6E6E6",
    fg="#000716",
    highlightthickness=0
)
sValueEntry.configure(highlightbackground="black", highlightcolor="black")
sValueEntry.place(
    x=665,
    y=445,
    width=87,
    height=22
)
sValueLabel = Label(root, text="S Value", bg="#FFFFFF")
sValueLabel.place(x = 473, y = 449)

# Hammer Blow name and label
blowCheck1Value = IntVar()
blowCheck1 = Checkbutton(
    root,
    text = "Yes",
    variable = blowCheck1Value,
    onvalue = 1,
    offvalue = 0,
    bg = "#ffffff"
)
blowCheck1.place(
    x = 665,
    y = 489
)
blowCheck2Value = IntVar()
blowCheck2 = Checkbutton(
    root,
    text = "No",
    variable = blowCheck2Value,
    onvalue = 1,
    offvalue = 0,
    bg = "#ffffff"
)
blowCheck2.place(
    x = 665,
    y = 516
)

blowHammerLabel = Label(root, text="Hammer Blow", bg="#FFFFFF")
blowHammerLabel.place(x = 473, y = 488)

modulusSection = Label(root, text="Fatigue", bg="#FFFFFF", font='Helvetica 22 bold')
modulusSection.place(x = 448, y = 547)

# mean impact load name and label
impactTypeOptionList = ["Custom", "Option", "Option", "Option"]

impacttext = StringVar()
impacttext.set(impactTypeOptionList[0])
impactoption = OptionMenu(root, impacttext, *impactTypeOptionList)
impactoption.configure(highlightbackground="#FFFFFF")
impactoption.place(x = 665, y = 587)

meanImpactLoadLabel = Label(root, text="Mean Impact Load", bg="#FFFFFF")
meanImpactLoadLabel.place(x = 473, y = 591)

# fatigue category name and label
fatigueTypeOptionList = ["Custom", "Option", "Option", "Option"]

fatiguetext = StringVar()
fatiguetext.set(fatigueTypeOptionList[0])
fatigueoption = OptionMenu(root, fatiguetext, *fatigueTypeOptionList)
fatigueoption.configure(highlightbackground="#FFFFFF")
fatigueoption.place(x = 665, y = 622)

categoryFatigueLabel = Label(root, text="Fatigue Category", bg="#FFFFFF")
categoryFatigueLabel.place(x = 473, y = 626)

# ignore stress name and label
ignoreStressEntry = Entry(
    bd=0,
    bg="#E6E6E6",
    fg="#000716",
    highlightthickness=0
)
ignoreStressEntry.configure(highlightbackground="black", highlightcolor="black")
ignoreStressEntry.place(
    x=665,
    y=657,
    width=87,
    height=22
)
ignoreStressLabel = Label(root, text="Ignore Stress", bg="#FFFFFF")
ignoreStressLabel.place(x = 473, y = 661)

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

# train frame
framet = LabelFrame(
    root,
    bg="#FFFFFF",
    bd=0,
    highlightthickness=0

)
framet.place(
    x=60,
    y=794,
    width=707,
    height=211
)

chooseTrain = Label(root, text="Choose Train", bg="#FFFFFF", font='Helvetica 22 bold')
chooseTrain.place(x = 92, y = 810)

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

# save button

# close button

##########################  tk mainloop ##########################
root.resizable(True, True)
root.mainloop()


import ConfigParser, logging, datetime, os, json

from flask import Flask, render_template, request

import mediacloud

CONFIG_FILE = 'settings.config'
basedir = os.path.dirname(os.path.realpath(__file__))

# load the settings file
config = ConfigParser.ConfigParser()
config.read(os.path.join(basedir, 'settings.config'))

# set up logging
logging.basicConfig(level=logging.DEBUG)
logging.info("Starting the MediaCloud example Flask app!")

# clean a mediacloud api client
mc = mediacloud.api.MediaCloud( config.get('mediacloud','api_key') )

app = Flask(__name__)

@app.route("/")
def home():
    return render_template("search-form.html")

@app.route("/search",methods=['POST'])
def search_results():
    keywords = request.form['keywords']
    start_date = request.form['start_date']
    end_date = request.form['end_date']

    # now = datetime.datetime.now()
    results = mc.sentenceCount(keywords,
                                solr_filter=[mc.publish_date_query(datetime.datetime.strptime(start_date, "%Y-%m-%d"),
                                                                   datetime.datetime.strptime(end_date, "%Y-%m-%d")),
                                            'tags_id_media:9139487'],
                                split=True,
                                split_start_date=start_date,
                                split_end_date=end_date)
    print(json.dumps(results['split'], indent=4, separators=(',', ': ')))
    clean_data = {}
    for key in results['split']:
        # if a date, append to clean_data dict
        if len(key.encode('utf-8')) > 5:
            clean_data[key.encode('utf-8')] = results['split'][key]

    # print(type(key.encode('utf-8')))
    # print(json.dumps(clean_data))
    # print(type(clean_data))
    # print(type(json.dumps(clean_data)))
    # print(type(json.loads(json.dumps(clean_data))))
    return render_template("search-results.html",
                           keywords=keywords,
                           sentenceCount=results['count'],
                           weeklyResults=json.dumps(clean_data))

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


from __future__ import annotations

import pickle  # nosec
import struct
from typing import Final, Optional

from ..packet import Packet
from ..sign import Signatures

__all__ = ['UdpPack']

_prefix = struct.Struct('!BBI')

class UdpPack:
    """Packs and unpacks SWIM protocol :class:`~swimprotocol.packet.Packet`
    objects from raw UDP packets or TCP connections. The :mod:`pickle` module
    is used for serialization, so :class:`~swimprotocol.sign.Signatures` is
    used to sign the payloads.

    Args:
        signatures: Generates and verifies cluster packet signatures.
        pickle_protocol: The :mod:`pickle` protocol version number.
        prefix_xor: A 4-byte string used to XOR the packet prefix, as a sanity
            check to detect malformed or incomplete UDP packets.

    """

    def __init__(self, signatures: Signatures, *,
                 pickle_protocol: int = pickle.HIGHEST_PROTOCOL,
                 prefix_xor: bytes = b'SWIM?!') -> None:
        super().__init__()
        if len(prefix_xor) != _prefix.size:
            raise ValueError(f'{prefix_xor!r} must be {_prefix.size} bytes')
        self.signatures: Final = signatures
        self.pickle_protocol: Final = pickle_protocol
        self.prefix_xor: Final = prefix_xor

    def _xor_prefix(self, prefix: bytes) -> bytes:
        zipped = zip(prefix, self.prefix_xor, strict=True)
        return bytes([left ^ right for left, right in zipped])

    def pack(self, packet: Packet) -> bytes:
        """Uses :mod:`pickle` to serialize *packet*, generates a digital
        signature of the pickled data, and returns a byte-string that can be
        sent as a raw UDP packet.

        The resulting byte-string starts with a 4-byte :mod:`struct` prefix
        (XOR'ed with *prefix_xor*) with the `struct format
        <https://docs.python.org/3/library/struct.html#format-strings>`_
        ``!BBH``. The first byte is the length of the salt, the second byte is
        the length of the signature, and the final two bytes are the length of
        the pickled payload. After the prefix, the salt, digest, and pickled
        payload byte-strings are concatenated.

        Args:
            packet: The SWIM protocol packet to serialize.

        """
        pickled = pickle.dumps(packet, self.pickle_protocol)
        salt, digest = self.signatures.sign(pickled)
        salt_start = _prefix.size
        digest_start = salt_start + len(salt)
        data_start = digest_start + len(digest)
        prefix = _prefix.pack(len(salt), len(digest), len(pickled))
        packed = bytearray(data_start + len(pickled))
        packed[0:salt_start] = self._xor_prefix(prefix)
        packed[salt_start:digest_start] = salt
        packed[digest_start:data_start] = digest
        packed[data_start:] = pickled
        return packed

    def unpack(self, data: bytes) -> Optional[Packet]:
        """Deserializes a byte-string that was created using :meth:`.pack` into
        a SWIM protocol packet. If any assumptions about the serialized data
        are not met, including an invalid signature, ``None`` is returned to
        indicate that *data* was malformed or incomplete.

        Args:
            data: The serialized byte-string of the SWIM protocol packet.

        """
        data_view = memoryview(data)
        salt_start = _prefix.size
        prefix = self._xor_prefix(data_view[0:salt_start])
        try:
            salt_len, digest_len, data_len = _prefix.unpack(prefix)
        except struct.error:
            return None
        digest_start = salt_start + salt_len
        data_start = digest_start + digest_len
        data_end = data_start + data_len
        salt = data_view[salt_start:digest_start]
        digest = data_view[digest_start:data_start]
        pickled = data_view[data_start:data_end]
        signatures = self.signatures
        if len(digest) != signatures.digest_size or len(pickled) != data_len:
            return None
        if signatures.verify(pickled, (salt, digest)):
            packet = pickle.loads(pickled)  # noqa: S301
            assert isinstance(packet, Packet)
            return packet
        else:
            return None


from collections import Counter
from datetime import datetime

# 示例数据
# 10折 1200 (0/1) 自己
# 10折 1200 VDMzZFF1T0hKdTRjaEJRMkV0N2xiZz09 (0/3) 舞***影(15***33)

def extract_discount(share_str: str) -> int:
    return int(share_str.split(" ")[0][:-1])

def extract_price(share_str: str) -> int:
    return int(share_str.split(" ")[1])

def extract_suin(share_str: str) -> str:
    return share_str.split(" ")[2]

def extract_remaining_times(share_str: str) -> int:
    # (0/3)
    temp = share_str.split(" ")[3][1:-1]
    remaing_times = int(temp.split("/")[0])

    return remaing_times

# 清洗并去重
with open(".cached/my_home.csv", encoding="utf-8") as f:
    suin_to_share_str: dict[str, str] = {}

    f.readline()
    for line in f:
        line = line.strip()

        if line == "":
            continue
        if line.endswith("自己"):
            continue

        suin = extract_suin(line)
        if suin in suin_to_share_str:
            last_info = suin_to_share_str[suin]
            if extract_remaining_times(line) <= extract_remaining_times(last_info):
                # 之前记录的是新一点的数据
                continue

        suin_to_share_str[suin] = line

# 排序
share_str_list = []
for s in suin_to_share_str.values():
    share_str_list.append(s)
share_str_list.sort(key=lambda s: extract_price(s))

# 统计各个折扣对应数目
discount_to_count: Counter = Counter()
for s in reversed(share_str_list):
    discount = extract_discount(s)
    discount_to_count[discount] += 1

# 导出

with open(".cached/my_home_processed.csv", "w", encoding="utf-8") as f:
    # 导出统计数据
    f.write(f"{datetime.now()}\n")
    f.write(f"总计: {len(share_str_list)}\n")
    for discount in sorted(discount_to_count.keys()):
        count = discount_to_count[discount]
        f.write(f"{discount:2d} 折: {count}\n")
    f.write("-----\n")

    # 导出实际数据
    for share_str in share_str_list:
        f.write(share_str + "\n")


# MenuTitle: SVG Pen
from fontTools.pens.basePen import BasePen

# (C) 2016 by Jens Kutilek
# https://raw.githubusercontent.com/jenskutilek/TypoLabs2016/master/penCollection/svgPen.py

# See also:
# http://www.w3.org/TR/SVG/paths.html#PathDataBNF
# https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths

# SVG path parsing code from:
# http://codereview.stackexchange.com/questions/28502/svg-path-parsing

def parse_svg_path(path_data):
    digit_exp = "0123456789eE"
    comma_wsp = ", \t\n\r\f\v"
    drawto_command = "MmZzLlHhVvCcSsQqTtAa"
    sign = "+-"
    exponent = "eE"
    float = False
    entity = ""
    for char in path_data:
        if char in digit_exp:
            entity += char
        elif char in comma_wsp and entity:
            yield entity
            float = False
            entity = ""
        elif char in drawto_command:
            if entity:
                yield entity
                float = False
                entity = ""
            yield char
        elif char == ".":
            if float:
                yield entity
                entity = "."
            else:
                entity += "."
                float = True
        elif char in sign:
            if entity and entity[-1] not in exponent:
                yield entity
                float = False
                entity = char
            else:
                entity += char
    if entity:
        yield entity

def drawSVGPath(pen, path=""):
    """
    Draw an SVG path that is supplied as a string. This is limited to SVG paths
    that contain only elements that can be matched to the usual path elements
    found in a glyph.
    """
    path_data = list(parse_svg_path(path))
    # print(path_data)
    i = 0
    prev_x: int | float = 0
    prev_y: int | float = 0
    while i < len(path_data):
        # print(i, path_data[i])
        v = path_data[i]
        if v in "Cc":
            # Cubic curve segment
            x1, y1, x2, y2, x3, y3 = path_data[i + 1 : i + 7]
            # print("    ", x1, y1, x2, y2, x3, y3)
            x1 = float(x1)
            y1 = float(y1)
            x2 = float(x2)
            y2 = float(y2)
            x3 = float(x3)
            y3 = float(y3)
            if v == "c":
                x1 += prev_x
                y1 += prev_y
                x2 += prev_x
                y2 += prev_y
                x3 += prev_x
                y3 += prev_y
            pen.curveTo(
                (x1, y1),
                (x2, y2),
                (x3, y3),
            )
            prev_x = x3
            prev_y = y3
            i += 7
        elif v in "Hh":
            # Horizontal line segment
            x = path_data[i + 1]
            # print("    ", x)
            x = float(x)
            if v == "h":
                x += prev_x
            pen.lineTo((x, prev_y))
            prev_x = x
            i += 2
        elif v in "LlMm":
            # Move or Line segment
            x, y = path_data[i + 1 : i + 3]
            # print("    ", x, y)
            x = float(x)
            y = float(y)
            if v in "lm":
                x += prev_x
                y += prev_y
            if v in "Ll":
                pen.lineTo((x, y))
            else:
                pen.moveTo((x, y))
            prev_x = x
            prev_y = y
            i += 3
        elif v in "Qq":
            # Quadratic curve segment
            x1, y1, x2, y2 = path_data[i + 1 : i + 5]
            # print("    ", x1, y1, x2, y2)
            x1 = float(x1)
            y1 = float(y1)
            x2 = float(x2)
            y2 = float(y2)
            if v == "q":
                x1 += prev_x
                y1 += prev_y
                x2 += prev_x
                y2 += prev_y
            pen.qCurveTo(
                (x1, y1),
                (x2, y2),
            )
            prev_x = x2
            prev_y = y2
            i += 5
        elif v in "Vv":
            # Vertical line segment
            y = path_data[i + 1]
            # print(y)
            y = float(y)
            if v == "v":
                y += prev_y
            pen.lineTo((prev_x, y))
            prev_y = y
            i += 2
        elif v in "Zz":
            pen.closePath()
            i += 1
        else:
            print(
                "SVG path element '%s' is not supported for glyph paths."
                % path_data[i]
            )
            break

class SVGpen(BasePen):
    def __init__(
        self,
        glyphSet,
        round_coordinates=False,
        force_relative_coordinates=False,
        optimize_output=False,
    ):
        """
        A pen that converts a glyph outline to an SVG path. After drawing,
        SVGPen.d contains the path as string. This corresponds to the SVG path
        element attribute "d".

        :param glyphSet: The font object
        :type glyphSet: :py:class:`fontParts.RFont`

        :param round_coordinates: Round all coordinates to integer. Default is
            False.
        :type round_coordinates: bool

        :param force_relative_coordinates: Store all coordinates as relative.
            Default is False, i.e. choose whichever notation (absolute or
            relative) produces shorter output for each individual segment.
        :type force_relative_coordinates: bool

        :param optimize_output: Make the output path string as short as
            possible. Default is True. Setting this to False also overrides the
            relative_coordinates option.
        :type optimize_output: bool
        """
        self._rnd = round_coordinates
        self._rel = force_relative_coordinates
        self._opt = optimize_output
        BasePen.__init__(self, glyphSet)
        self.reset()

    def reset(self):
        self.prev_x: int | float = 0  # previous point
        self.prev_y: int | float = 0
        self._set_first_point((0, 0))
        self._set_previous_point((0, 0))
        self._set_previous_cubic_control(None)
        self._set_previous_quadratic_control(None)
        self._prev_cmd = None
        self.relative = False
        self.d = ""

    def _append_shorter(self, absolute, relative):
        # Check if relative output is smaller
        if not self._rel and len(absolute) <= len(relative) or not self._opt:
            cmd_str = absolute
            self.relative = False
        else:
            cmd_str = relative
            self.relative = True

        if cmd_str[0] == self._prev_cmd:
            rest = cmd_str[1:]
            if rest.startswith("-"):
                self.d += rest
            else:
                self.d += " " + rest
        else:
            self.d += cmd_str

    def _get_shorter_sign(self, value):
        if value < 0 and self._opt:
            return "%g" % value
        else:
            return " %g" % value

    def _round_pt(self, pt):
        # Round the point based on the current rounding settings
        if self._rnd:
            x, y = pt
            return (int(round(x)), int(round(y)))
        return pt

    def _set_first_point(self, pt):
        self.first_x, self.first_y = pt

    def _set_previous_point(self, pt):
        self.prev_x, self.prev_y = pt

    def _set_previous_cubic_control(self, pt):
        if pt is None:
            self.prev_cx = None
            self.prev_cy = None
        else:
            self._set_previous_quadratic_control(None)
            self.prev_cx, self.prev_cy = pt

    def _set_previous_quadratic_control(self, pt):
        if pt is None:
            self.prev_qx = None
            self.prev_qy = None
        else:
            self._set_previous_cubic_control(None)
            self.prev_qx, self.prev_qy = pt

    def _reset_previous_controls(self):
        self._set_previous_cubic_control(None)
        self._set_previous_quadratic_control(None)

    def _moveTo(self, pt):
        x, y = self._round_pt(pt)
        cmd = "Mm"
        a = "M%g" % x
        a += self._get_shorter_sign(y)
        r = "m%g" % (x - self.prev_x)
        r += self._get_shorter_sign(y - self.prev_y)
        self._append_shorter(a, r)
        self._set_first_point((x, y))
        self._set_previous_point((x, y))
        self._reset_previous_controls()
        self._prev_cmd = cmd[self.relative]

    def _lineTo(self, pt):
        x, y = self._round_pt(pt)
        if y == self.prev_y:
            cmd = "Hh"
            a = "H%g" % x
            r = "h%g" % (x - self.prev_x)
        elif x == self.prev_x:
            cmd = "Vv"
            a = "V%g" % y
            r = "v%g" % (y - self.prev_y)
        else:
            cmd = "Ll"
            a = "L%g" % x
            a += self._get_shorter_sign(y)
            r = "l%g" % (x - self.prev_x)
            r += self._get_shorter_sign(y - self.prev_y)
        self._append_shorter(a, r)
        self._set_previous_point((x, y))
        self._reset_previous_controls()
        self._prev_cmd = cmd[self.relative]

    def _curveToOne(self, p1, p2, pt):
        x1, y1 = self._round_pt(p1)
        x2, y2 = self._round_pt(p2)
        x3, y3 = self._round_pt(pt)
        if self.prev_cx is None:
            self._set_previous_cubic_control((self.prev_x, self.prev_x))
        if (
            self.prev_y - y1 + self.prev_y == self.prev_cy
            and self.prev_x - x1 + self.prev_x == self.prev_cx
        ):
            # Control point p1 is mirrored, use S command and omit p1
            cmd = "Ss"
            a = "S%g" % x2
            for coord in [y2, x3, y3]:
                a += self._get_shorter_sign(coord)
            r = "s%g" % (x2 - self.prev_x)
            for coord in [
                y2 - self.prev_y,
                x3 - self.prev_x,
                y3 - self.prev_y,
            ]:
                r += self._get_shorter_sign(coord)
        else:
            cmd = "Cc"
            a = "C%g" % x1
            for coord in [y1, x2, y2, x3, y3]:
                a += self._get_shorter_sign(coord)
            r = "c%g" % (x1 - self.prev_x)
            for coord in [
                y1 - self.prev_y,
                x2 - self.prev_x,
                y2 - self.prev_y,
                x3 - self.prev_x,
                y3 - self.prev_y,
            ]:
                r += self._get_shorter_sign(coord)
        self._append_shorter(a, r)
        self._set_previous_point((x3, y3))
        self._set_previous_cubic_control((x2, y2))
        self._prev_cmd = cmd[self.relative]

    def _qCurveToOne(self, p1, p2):
        x1, y1 = self._round_pt(p1)
        x2, y2 = self._round_pt(p2)
        if self.prev_qx is None:
            self._set_previous_quadratic_control((self.prev_x, self.prev_x))
        if (
            self.prev_y - y1 + self.prev_y == self.prev_qy
            and self.prev_x - x1 + self.prev_x == self.prev_qx
        ):
            # Control point p1 is mirrored, use T command and omit p1
            cmd = "Tt"
            a = "T%g" % x2
            a += self._get_shorter_sign(y2)
            r = "t%g" % (x2 - self.prev_x)
            r += self._get_shorter_sign(y2 - self.prev_y)
        else:
            cmd = "Qq"
            a = "Q%g" % x1
            for coord in [y1, x2, y2]:
                a += self._get_shorter_sign(coord)
            r = "q%g" % (x1 - self.prev_x)
            for coord in [
                y1 - self.prev_y,
                x2 - self.prev_x,
                y2 - self.prev_y,
            ]:
                r += self._get_shorter_sign(coord)
        self._append_shorter(a, r)
        self._set_previous_point((x2, y2))
        self._set_previous_quadratic_control((x1, y1))
        self._prev_cmd = cmd[self.relative]

    def _closePath(self):
        cmd = "z" if self._rel else "Z"
        self.d += cmd
        self._set_previous_point((self.first_x, self.first_y))
        self._reset_previous_controls()
        self._prev_cmd = cmd


from typing import List, Dict, Tuple, Any

import numpy as np
import pandas as pd

import spacy
import en_core_web_sm
from spacy.matcher import Matcher

from concept_processing.asp.asp_generator import ASPGenerator
from concept_processing.asp.asp_solver import clingo_solve
from concept_processing.asp.clingo_out_parsers import ClingoAnsParser
from concept_processing.enums import ProblemType
from concept_processing.nlp.spacy_wrapper import SpacyWrapper
from concept_processing.nlp.nlp_parser import NLPParser
from concept_processing.nlp.nlp_utils import add_punctuation, truecase, merge_not

from concept_processing.pam import count_datapoints_in_each_feature

# ILASP solution paths
base_dir = {
    ProblemType.ATOMISATION: '/Users/Cherry0904/Desktop/roko-for-charlize/ilasp/atomisation',
    ProblemType.GENERALISATION: '/Users/Cherry0904/Desktop/roko-for-charlize/ilasp/generalisation',
    # ProblemType.ATOMISATION: '/vol/bitbucket/yy3219/roko-for-charlize/ilasp/atomisation',
    # ProblemType.GENERALISATION: '/vol/bitbucket/yy3219/roko-for-charlize/ilasp/generalisation',
}

background_knowledge_file_temp = '{}/background.ilasp'
solution_file_temp = '{}/solutions/best_sol.lp'
clingo_out_file_temp = '{}/clingo_out.tmp'

class ConceptsState:
    def __init__(self, ids: List[str], label_indices: np.ndarray, label_categories: List[str], concept_pam: np.ndarray,
                 concept_strings: List[str]):
        assert len(ids) == len(label_indices) and len(label_indices) == concept_pam.shape[0] and \
               concept_pam.shape[1] == len(concept_strings)
        self.ids = ids
        self.label_indices = label_indices
        self.label_categories = label_categories
        self.concept_pam = concept_pam
        self.concept_strings = concept_strings

    def get_labels(self) -> List[str]:
        return [self.label_categories[i] for i in self.label_indices]

    def to_dict(self) -> Dict[str, List[Any]]:
        return dict(id=self.ids, label=self.get_labels(), concepts=list(self.concept_pam),
                    explanations=self.concept_strings)

# Replaces the old [row_id, [concept_ids]]
class ConceptBag:
    def __init__(self):
        self.store = {}
        self.ids = []
        self.labels = []

    def append(self, row_id: str, concept_ids: List[str], label: str):
        # Two explanations for a video may exist sometimes.
        if row_id not in self.store:
            self.store[row_id] = concept_ids
            self.ids.append(row_id)
            # There is one odd label that needs to be fixed
            if label == ' it could be called a strike because the pitch landed in the strike zone before being hit':
                label = 'strike'

            self.labels.append(label)
        else:
            self.store[row_id] = list(set(self.store[row_id]).union(concept_ids))

    def to_rawbagofconcepts(self) -> List[Tuple[str, List[int]]]:
        return [(id, self.store[id]) for id in self.ids]

    def to_pam(self) -> np.ndarray:
        """
        Creates binary presence-absence matrix (PAM)
        """

        N = len(self.ids)
        C = 0
        for id in self.ids:
            curr_max = np.max(self.store[id], initial=0)
            C = max(C, curr_max)
        C += 1
        data = np.zeros((N, C))
        for i, id_ in enumerate(self.ids):
            data[i, self.store[id_]] = 1

        # Remove extraneous columns
        cols_to_remove = count_datapoints_in_each_feature(data) == 0
        data = data[:, ~cols_to_remove]
        return data

# Applies generalisation/atomisation procedure to extract the concepts
class ConceptExtractor:
    def __init__(self, nlp: NLPParser):
        self.nlp = nlp
        self.concept_dict = {}
        self.next_concept_id = 0
        self.concept_bag = ConceptBag()

    def parse(self, row_id: str, premise_sents: str, label: str):
        # Non need to include errors
        if label != 'none':
            premise_sents = self.nlp(premise_sents)
            premise_sents = [str(sent) for sent in premise_sents.sentences()]

            # Post-process atomic sentences to remove super short ones
            atomic_sents = self.split(premise_sents, ProblemType.ATOMISATION)
            # atomic_sents = self.remove_short_concepts(atomic_sents)
            # atomic_sents = self.post_process_short_concepts(atomic_sents)
            generalised_sents = self.split(atomic_sents, ProblemType.GENERALISATION)
            # generalised_sents = self.post_process_short_concepts(generalised_sents)

            # concept_ids = [self._get_id(sent) for sent in atomic_sents]
            concept_ids = [self._get_id(sent) for sent in generalised_sents]
            self.concept_bag.append(row_id, concept_ids, label)

    def _get_id(self, sent: str):
        if sent not in self.concept_dict:
            self.concept_dict[sent] = self.next_concept_id
            self.next_concept_id += 1
        return self.concept_dict[sent]

    def get(self) -> Tuple[ConceptBag, List[str]]:
        return self.concept_bag, concept_dict_to_list(self.concept_dict)

    @staticmethod
    def _write(clingo_out_file: str, program: List[str]):
        with open(clingo_out_file, 'w') as f:
            for elem in program:
                f.write(elem + '\n')

    def split(self, sents: List[str], problem_type: ProblemType) -> List[str]:

        sols = []
        for sent in sents:
            b_dir = base_dir[problem_type]

            asp_generator = ASPGenerator(self.nlp, problem_type)
            asp_generator.parse(str(sent))
            # Exactly 1 element since we do not have concepts texts
            program = asp_generator.get_programs()[0]

            clingo_out_file = clingo_out_file_temp.format(b_dir)
            solution_file = solution_file_temp.format(b_dir)
            background_file = background_knowledge_file_temp.format(b_dir)

            self._write(clingo_out_file, program)

            atoms = clingo_solve(clingo_out_file, background_file, solution_file)
            asp_parser = ClingoAnsParser(problem_type)

            sents = asp_parser.get_sentences(atoms)
            atomic_sents = [add_punctuation(merge_not(truecase(sent, self.nlp))) for sent in sents]
            sols += atomic_sents
        return sols

    # Remove concepts with only one to three tokens
    def remove_short_concepts(self, sents: List[str]) -> List[str]:
        num_of_words = [len(sent.split()) for sent in sents]
        index_of_short_concepts = [i for i, j in enumerate(num_of_words) if j == 1 or j == 2 or j==3]
        index_of_all_concepts = [i for i, j in enumerate(num_of_words)]
        index_of_long_concepts = [x for x in index_of_all_concepts if x not in index_of_short_concepts]
        sents = [sents[i] for i in index_of_long_concepts]

        return sents

    # Remove short concepts that satisfy some rules defined on POS tags
    def post_process_short_concepts(self, sents: List[str]) -> List[str]:
        # nlp = SpacyWrapper()
        nlp = en_core_web_sm.load()
        excmatcher = self.add_exc_matcher(nlp)

        num_of_words = [len(sent.split()) for sent in sents]
        index_of_short_concepts = [i for i, j in enumerate(num_of_words) if j == 2 or j==3]
        index_of_all_concepts = [i for i, j in enumerate(num_of_words)]
        index_of_long_concepts = [x for x in index_of_all_concepts if x not in index_of_short_concepts]
        # index_of_long_concepts = [i for i, j in enumerate(num_of_words) if j != 2 and j !=3]
        # index_of_3_token_concepts = [i for i, j in enumerate(num_of_words) if j == 3]

        index_to_keep = []
        for i in index_of_short_concepts:
            doc = nlp(sents[i])
            match = excmatcher(doc)

            if match == []: # If there is no match
                index_to_keep.append(i)

        sents = [sents[i] for i in index_to_keep + index_of_long_concepts]
        return sents

    def add_exc_matcher(self, nlp):
        # create exclusion matcher for our concepts
        excpattern1 = [{"POS": {"IN": ["NOUN", "PRON", "DET"]}}, {"POS": "VERB"}] # such as "it looks", "he's looking"
        # excpattern2 = [{"POS": "DET"}, {"POS": "NOUN"}, {"POS": "VERB"}] # such as "the woman looks"

        # Dirty way of using SpacyWrapper, kept because this code is not maintained
        # excmatcher = Matcher(nlp._nlp.vocab)
        excmatcher = Matcher(nlp.vocab)

        excmatcher.add("meaningless_short_concept", [excpattern1])
        # excmatcher.add("short_concept_3_tokens", [excpattern2])
        # nlp._nlp.add_pipe("benepar", config={"model": "benepar_en3"})

        return excmatcher

def concept_dict_to_list(concept_dict: Dict[str, int]) -> List[str]:
    """
    parameters
    ----------
    concept_dict - dictionary mapping from concept (e.g. strs) to index (int)
        where indices are contiguous and starting from zero.

    returns
    -------
    concepts - a list of concepts where concepts[i] is key k such that
        concept_dict[k] = i
    """
    reverse_dict = {i: s for s, i in concept_dict.items()}
    concepts = [reverse_dict[i] for i in range(len(concept_dict))]
    return concepts


import sys, getopt
import collections

def main(argv):
    inputFile = ''

    try:
        opts, args = getopt.getopt(argv, 'hi:')
    except getopt.GetoptError:
        print('test.py -i <inputfile>')
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            print('test.py -i <inputfile>')
            sys.exit()
        elif opt == '-i':
            inputFile = arg

    displaySignals = []
    displayVals = []
    with open(inputFile, "r") as fileIn:
        for line in fileIn:
            signals, values = line.split(" | ")
            displaySignals.append(signals.split())
            displayVals.append(values.split())

#part 1: Considerd how many times the digits 1, 4, 7, 8 appear accros all displays
#part A
    valLengthSort = lenSort(displayVals)
    for key in valLengthSort:
        print(key, ":", len(valLengthSort[key]))

#part 2: (PER DISPLAY) create map...
    totalVals = []
    for signals, values in zip(displaySignals, displayVals):
        #sort by length
        signalLengthSort = {}
        for signal in signals:
            if not len(signal) in signalLengthSort:
                signalLengthSort[len(signal)]=[]

            signalLengthSort[len(signal)].append(signal)
        #only lengths 2 (=1), 3 (=7), and 4 (=4) are useful.
        infoKeys = [2, 3, 4]
        digitMap = {8: "abcdefg"}
        for info in infoKeys:
            if info in signalLengthSort:
                if info == 2:
                    digitMap[1] = signalLengthSort[info][0]
                if info == 3:
                    digitMap[7] = signalLengthSort[info][0]
                if info == 4:
                    digitMap[4] = signalLengthSort[info][0]

        deductKeys = [5,6]
        for deduct in deductKeys:
                for signal in signalLengthSort[deduct]:
                    overlap147 = 0
                    for char in list(signal):
                        if char in digitMap[1]: overlap147 += 1
                        if char in digitMap[4]: overlap147 += 1
                        if char in digitMap[7]: overlap147 += 1

                    overlap147 += deduct

                    if not 0 in digitMap and overlap147 == 14:
                        digitMap[0] = signal

                    if not 2 in digitMap and overlap147 == 10:
                        digitMap[2] = signal

                    if not 3 in digitMap and overlap147 == 13:
                        digitMap[3] = signal

                    if not 5 in digitMap and overlap147 == 11:
                        digitMap[5] = signal

                    if not 6 in digitMap and overlap147 == 12:
                        digitMap[6] = signal

                    if not 9 in digitMap and overlap147 == 15:
                        digitMap[9] = signal

        digits = []
        for value in values:
            for key in digitMap:
                if collections.Counter(value) == collections.Counter(digitMap[key]):
                    digits.append(str(key))

        totalVals.append(int(("".join(digits))))

    print(totalVals)
    print(sum(totalVals))

def lenSort(outVals):
    sortedLengths={}
    for entry in outVals:
        for val in entry:
            if not len(val) in sortedLengths:
                sortedLengths[len(val)]=[]

            sortedLengths[len(val)].append(val)

    return sortedLengths

def decompose(sorted):
    decomposition={}
    for key in sorted:
        decomposition[key]={}
        for entry in sorted[key]:
            for char in list(entry):
                if not char in decomposition[key]:
                    decomposition[key][char]=0

                decomposition[key][char] += 1

    return decomposition

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


import scrapy
from scrapy import Request

class TrilhasTDC(scrapy.Spider):
    name = "trilhas_tdc"

    start_urls = [
        "http://www.thedevelopersconference.com.br/tdc/2018/saopaulo/trilhas"
    ]

    def parse(self, response):
        colunas = response.xpath('//div[contains(@class, "col-sp")]')

        for coluna in colunas:
            dia = coluna.xpath('./h4/text()').extract_first()
            links_trilhas = coluna.xpath('./a/@href').extract()
            for link_trilha in links_trilhas:
                yield Request(
                    url=response.urljoin(link_trilha),
                    callback=self.parse_trilha,
                    meta={
                        'dia' : dia,
                    }
                )
    def parse_trilha(self,response):
        yield{
            'dia' : response.meta.get('dia'),
            'titulo' : response.xpath('//h1[@class="titulo-trilha"]/text()').extract_first(),
            'subtitulo': response.xpath('//h1[@class="titulo-trilha"]/small/text()').extract_first(),
            'descricao': response.xpath('//div[@class="lead"]//p/text()').extract(),
            'link' : response.url,
        }


import string
from os.path import exists
import pandas as pd
from datetime import datetime
from random import shuffle
import numpy as np

if exists('D:\GRE\my word list\words.csv'):
    df = pd.read_csv('D:\GRE\my word list\words.csv')
    wordcount = df.shape[0]
else:
    df = pd.DataFrame(columns = ['word', 'meaning', 'date', 'times_correct', 'times_incorrect'])
    wordcount = 0

print("*****WELCOME*****")

def get_game():
    if exists('D:\GRE\my word list\high_score.csv'):
        high_df = pd.read_csv('D:\GRE\my word list\high_score.csv')
    else:
        high_df = pd.DataFrame(columns = ['score', 'date', 'time'])

    if exists('D:\GRE\my word list\words.csv'):
        df = pd.read_csv('D:\GRE\my word list\words.csv')
        wordcount = df.shape[0]
        if wordcount < 10:
            print('Sorry, the word list should atleast contain 10 words')
            return
    else:
        print('File doesnt exist!')
        return

    lives = 3
    score = 0
    datentime = datetime.now()
    new_date = datentime.strftime('%d-%m-%Y')
    new_time = datentime.strftime('%H-%M-%S')
    while(lives > 0):
        print('You have %d lives left!'%lives)
        word_index = np.random.randint(low = 0, high = wordcount)
        selected_word = df.iloc[word_index, 0]
        selected_word_meaning = df.iloc[word_index, 1]
        random_meanings = []
        random_meanings_index = np.random.randint(low = 0, high = wordcount, size = (4))
        for x in random_meanings_index:
            random_meanings.append(df.iloc[x, 1])
        random_meanings.append(selected_word_meaning)
        shuffle(random_meanings)
        print('\n', selected_word)
        for i in range(5):
            print('\n%d) %s'%(i, random_meanings[i]))
        while True:
            choice = int(input("\nEnter your choice!"))
            if choice in list(range(5)):
                break
            else:
                print('Wrong choice')
        if random_meanings[choice] == selected_word_meaning:
            score += 1
            print('Correct! Your score now is:', score)
            df.loc[word_index, 'times_correct'] += 1
        else:
            print('Sorry! Wrong answer')
            print('\n%s means %s'%(selected_word, selected_word_meaning))
            lives -= 1
            df.loc[word_index, 'times_incorrect'] += 1
    df.to_csv('D:\GRE\my word list\words.csv', index = False, columns = ['word', 'meaning', 'date', 'times_correct', 'times_incorrect'])
    print('Sorry, you just went out of lives, your highscore for %s at %s was %d'%(new_date, new_time, score))
    high_df.loc[high_df.shape[0]+1, :] = [score, new_date, new_time]
    high_df.sort_values(by = 'score', ascending = False)
    print(high_df)
    high_df.to_csv('D:\GRE\my word list\high_score.csv', index = False, columns = ['score', 'date', 'time'])
    return

def get_stats():
    print('Statistics')
    return

def get_meaning(get_word_meaning):
    if exists('D:\GRE\my word list\words.csv'):
        df = pd.read_csv('D:\GRE\my word list\words.csv')
        wordcount = df.shape[0]
    else:
        print('File doesnt exist!')
        return
    found = False
    for i in range(wordcount):
        if df.iloc[i, 0].lower == get_word_meaning.lower:
            print('\n%s means %s'%(get_word_meaning, df.iloc[i, 1]))
            found = True
            break
    if found == False:
        print('\nSorry, word was not found in your list')
    return

if __name__ == '__main__':

    choice = 1
    while(choice != '*'):
        print("1. Add new word\n2. Play word game\n3. Get word meaning\n4. Get Statistics\n*. TO EXIT!")
        print("\nEnter your choice!")
        choice = input()
        if choice == str(1):
            print("\nAdding new word!")
            new_word = input('\nPlease enter the word: ')
            word_meaning = input('\nPlease enter the meaning: ')
            date = datetime.now()
            date = date.strftime('%d-%m-%Y')
            corr = 0
            incorr = 0
            print('Number of words in list', wordcount+1)
            df.loc[wordcount, :] = [new_word, word_meaning, date, corr, incorr]
            wordcount += 1
            df.to_csv('D:\GRE\my word list\words.csv', index = False, columns = ['word', 'meaning', 'date', 'times_correct', 'times_incorrect'])
        elif choice == str(2):
            print("\nLets play word game!")
            get_game()
        elif choice == str(3):
            get_word_meaning = input('\nGetting word meaning, so please enter the word: ')
            get_meaning(get_word_meaning)
        elif choice == str(4):
            get_stats()
        elif choice == str('*'):
            df.to_csv('D:\GRE\my word list\words.csv', index = False, columns = ['word', 'meaning', 'date', 'times_correct', 'times_incorrect'])
            break
        else :
            print('\nWrong choice, Please try again')


"""
Main launching point of the Top Patch Server
"""
import base64
import uuid
import os
import logging
import logging.config

import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.options

from redis import StrictRedis
from rq import Connection, Queue

from server.handlers import RootHandler, RvlLoginHandler, RvlLogoutHandler
from server.handlers import WebSocketHandler, AdminHandler
from receiver.api.core.newagent import NewAgentV1
from receiver.api.core.checkin import CheckInV1
from receiver.api.core.startup import StartUpV1
from receiver.api.rv.results import *
from receiver.api.core.results import *
from receiver.api.rv.updateapplications import UpdateApplicationsV1
from receiver.api.ra.results import RemoteDesktopResults
from receiver.api.monitoring.monitoringdata import UpdateMonitoringStatsV1

from db.client import *
from scheduler.jobManager import start_scheduler

from tornado.options import define, options

#import newrelic.agent
#newrelic.agent.initialize('/opt/TopPatch/conf/newrelic.ini')

define("port", default=9001, help="run on port", type=int)
define("debug", default=True, help="enable debugging features", type=bool)

class Application(tornado.web.Application):
    def __init__(self, debug):
        handlers = [

            #Operations for the Monitoring Plugin
            (r"/rvl/v1/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12})/monitoring/monitordata/?", UpdateMonitoringStatsV1),

            #RA plugin
            (r"/rvl/ra/rd/results/?", RemoteDesktopResults),

            #Login and Logout Operations
            (r"/rvl/?", RootHandler),
            (r"/rvl/login/?", RvlLoginHandler),
            (r"/rvl/logout/?", RvlLogoutHandler),

            #Operations for the New Core Plugin
            (r"/rvl/v1/core/newagent/?", NewAgentV1),
            (r"/rvl/v1/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12})/core/startup/?", StartUpV1),
            (r"/rvl/v1/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12})/core/checkin/?", CheckInV1),
            (r"/rvl/v1/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12})/rv/updatesapplications/?", UpdateApplicationsV1),
            (r"/rvl/v1/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12})/core/results/reboot/?", RebootResultsV1),
            (r"/rvl/v1/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12})/core/results/shutdown/?", ShutdownResultsV1),

            #New Operations for the New RV Plugin
            (r"/rvl/v1/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12})/rv/results/install/apps/os?",
                InstallOsAppsResults),
            (r"/rvl/v1/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12})/rv/results/install/apps/custom?",
                InstallCustomAppsResults),
            (r"/rvl/v1/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12})/rv/results/install/apps/supported?",
                InstallSupportedAppsResults),
            (r"/rvl/v1/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12})/rv/results/install/apps/agent?",
                InstallAgentAppsResults),
            (r"/rvl/v1/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12})/rv/results/uninstall?",
                UnInstallAppsResults),

        ]

        template_path = "/opt/TopPatch/tp/templates"
        settings = {
            "cookie_secret": "patching-0.7",
            "login_url": "/rvl/login",
        }
        tornado.web.Application.__init__(self, handlers,
                                         template_path=template_path,
                                         debug=True, **settings)

    def log_request(self, handler):
        logging.config.fileConfig('/opt/TopPatch/conf/logging.config')
        log = logging.getLogger('rvweb')
        log_method = log.debug
        if handler.get_status() <= 299:
            log_method = log.info
        elif handler.get_status() <= 399 and \
                handler.get_status() >= 300:
            log_method = log.warn
        elif handler.get_status() <= 499 and \
                handler.get_status() >= 400:
            log_method = log.error
        elif handler.get_status() <= 599 and \
                handler.get_status() >= 500:
            log_method = log.error
        request_time = 1000.0 * handler.request.request_time()
        real_ip = handler.request.headers.get('X-Real-Ip', None)
        #remote_ip = handler.request.remote_ip
        #uri = handler.request.remote_ip
        forwarded_ip = handler.request.headers.get('X-Forwarded-For', None)
        user_agent = handler.request.headers.get('User-Agent')
        log_message = '%d %s %s, %.2fms' % (handler.get_status(), handler._request_summary(), user_agent, request_time)
        if real_ip:
            log_message = (
                '%d %s %s %s %s, %.2fms' %
                (
                    handler.get_status(), handler._request_summary(),
                    real_ip, forwarded_ip, user_agent, request_time
                )
            )
        log_method(log_message)

if __name__ == '__main__':
    tornado.options.parse_command_line()
    https_server = tornado.httpserver.HTTPServer(
        Application(options.debug),
        ssl_options={
            "certfile": os.path.join(
                "/opt/TopPatch/tp/data/ssl/",
                "server.crt"),
            "keyfile": os.path.join(
                "/opt/TopPatch/tp/data/ssl/",
                "server.key"),
        }
    )
    https_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()


import gc
import itertools as it
import os.path as osp
from typing import List
import warnings
from collections import deque, namedtuple

import numpy as np
import torch
from examples.speech_recognition.data.replabels import unpack_replabels
from fairseq import tasks
from fairseq.utils import apply_to_sample
from omegaconf import open_dict
from fairseq.dataclass.utils import convert_namespace_to_omegaconf

# try:
#     from flashlight.lib.text.dictionary import create_word_dict, load_words
#     from flashlight.lib.sequence.criterion import CpuViterbiPath, get_data_ptr_as_bytes
#     from flashlight.lib.text.decoder import (
#         CriterionType,
#         LexiconDecoderOptions,
#         KenLM,
#         LM,
#         LMState,
#         SmearingMode,
#         Trie,
#         LexiconDecoder,
#     )
# except:
#     warnings.warn(
#         "flashlight python bindings are required to use this functionality. Please install from https://github.com/facebookresearch/flashlight/tree/master/bindings/python"
#     )
#     LM = object
#     LMState = object

class W2lDecoder(object):
    def __init__(self, args, tgt_dict):
        self.tgt_dict = tgt_dict
        self.vocab_size = len(tgt_dict)
        self.nbest = args.nbest

        self.blank = (
            tgt_dict.index("<ctc_blank>")
            if "<ctc_blank>" in tgt_dict.indices
            else tgt_dict.bos()
        )
        if "<sep>" in tgt_dict.indices:
            self.silence = tgt_dict.index("<sep>")
        elif "|" in tgt_dict.indices:
            self.silence = tgt_dict.index("|")
        else:
            self.silence = tgt_dict.eos()
        self.asg_transitions = None

    def generate(self, models, sample, **unused):
        """Generate a batch of inferences."""
        # model.forward normally channels prev_output_tokens into the decoder
        # separately, but SequenceGenerator directly calls model.encoder
        encoder_input = {
            k: v for k, v in sample["net_input"].items() if k != "prev_output_tokens"
        }
        emissions = self.get_emissions(models, encoder_input)
        return self.decode(emissions)

    def get_emissions(self, models, encoder_input):
        """Run encoder and normalize emissions"""
        model = models[0]
        encoder_out = model(**encoder_input)
        if hasattr(model, "get_logits"):
            emissions = model.get_logits(encoder_out) # no need to normalize emissions
        else:
            emissions = model.get_normalized_probs(encoder_out, log_probs=True)
        return emissions.transpose(0, 1).float().cpu().contiguous()

    def get_tokens(self, idxs):
        """Normalize tokens by handling CTC blank, ASG replabels, etc."""
        idxs = (g[0] for g in it.groupby(idxs))
        idxs = filter(lambda x: x != self.blank, idxs)
        return torch.LongTensor(list(idxs))

class W2lViterbiDecoder(W2lDecoder):
    def __init__(self, args, tgt_dict):
        super().__init__(args, tgt_dict)


#using a shallow net(2 layers)
#not tested
import numpy as np
import matplotlib.pyplot as plt
import sklearn
import sklearn.datasets
import sklearn.linear_model
from utils import load_dataset

#loading the dataset using utils
x,y = load_dataset()
shape_x = x.shape
shape_y = y.shape
m = shape_x[1]

#first trying to fit the data using sime LR
clf = sklearn.linear_model.LogisticRegressionCV()
clf.fit(x.T, y.T)
print(clf.predict(x.T))

#now trying with multi layer nn model

#helper functions
def layer_sizes(x,y):
	nx = x.shape[0]
	nh = 4
	ny = y.shape[0]
	return(nx,nh,ny)

def init_weights(nx,nh,ny):
	np.random.seed(9)
	w1 = np.random.randn(nh,nx)*0.01
	b1 = np.zeros((nh,1))
	w2 = np.random.randn(ny,nh)*0.01
	b2 = np.zeros((ny,1))
	params = {"w1":w1, "b1":b1,"w2":w2,"b2":b2}
	return params

#forward propagation
def propagate(x,y,params):
	m = x.shape[1]
	w1 = params["w1"]
	b1 = params["b1"]
	w2 = params["w2"]
	b2 = params["b2"]
	z1 = np.dot(w1,x)+b1
	a1 = np.tanh(z1)
	z2 = np.dot(w2,a1)+b2
	a2 = 1/(1+np.exp(-z2))

	logprobs = np.multiply(np.log(a2),y)+np.multiply(np.log(1-a2),(1-y))
	cost =-np.sum(logprobs)/m
	cost = np.squeeze(cost)
	cache = {"z1":z1,"a1":a1,"z2":z2,"a2":a2}
	return cache, cost

#backprop for optimization

def optimize(params, cache, x,y, alpha = 1.2):
	m = x.shape[1]
	a1 = cache["a1"]
	a2 = cache["a2"]
	dz2 = a2-y
	dw2 = np.dot(dz2,a1.T)/m
	db2 = np.sum(dz2)/m
	dz1 = np.dot(w2.T, dz2)*(1-np.power(a1,2))
	dw1 = np.dot(dz1,x.T)/m
	db1 = np.sum(dz1)/m
	w1 = params["w1"]
	w2 = params["w2"]
	b1 = params["b1"]
	b2 = params["b2"]
	w1 -= alpha*dw1
	b1 -= alpha*db1
	w2 -= alpha*dw2
	b2 -= alpha*db2
	params = {"w1":w1,"b1":b1,"w2":w2,"b2":b2}
	grads = {"dw1":dw1,"db1":db1,"dw2":dw2,"db2":db2}
	return params, grads

#final model
def model(x,y,iterations):
	np.random.seed(9)
	nx = layer_sizes(x,y)[0]
	ny = layer_sizes(x,y)[2]
	nh = 4
	params = init_weights(nx,nh,ny)
	for i in range(iterations):
		cost, cache = propagate(x,y, params)
		params,grads = optimize(params, cache,x,y)
		if(i%1000 == 0):
			print(i,cost)

	return params

def predict(params, x):
	m = x.shape[1]
	w1 = params["w1"]
	b1 = params["b1"]
	w2 = params["w2"]
	b2 = params["b2"]
	z1 = np.dot(w1,x)+b1
	a1 = np.tanh(z1)
	z2 = np.dot(w2,a1)+b2
	a2 = 1/(1+np.exp(-z2))
	predictions = (a2>0.5)
	return predictions

params = model(x,y,10000)
predictions = predict(params, x)
print((np.dot(y,predictions.T)+np.dot(1-y, 1-predictions.T))/y.size)


#!/usr/bin/python3
"""
    Python script that takes GitHub credentials
    (username and password) and uses the GitHub API to display id
"""
import requests
import sys

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print("Usage: ./10-my_github.py <username> <token>")
        sys.exit(1)

    username, token = sys.argv[1], sys.argv[2]

    # Make the request with Basic Authentication using your token
    response = requests.get('https://api.github.com/user', auth=(username, token))

    if response.status_code == 200:
        try:
            user_data = response.json()
            user_id = user_data.get('id')
            if user_id is not None:
                print(user_id)
            else:
                print("None")
        except ValueError:
            print("Invalid JSON response")
    else:
        print("None")


from matplotlib import pyplot as plt

if __name__ == '__main__':
    slope = 0.0008588
    y_intercept = -0.1702
    rainfall_values = [50 * x for x in range(0, 18)]

    y = [max(slope * x + y_intercept, 0) for x in rainfall_values]

    plt.title('Bifurcation diagram of Scanlon model')
    plt.xlabel('Rainfall (mm/year)')
    plt.ylabel('Steady state density')
    plt.plot(rainfall_values, y)
    plt.plot(rainfall_values[4:], [0 for _ in range(4, 18)], linestyle='dashed')
    plt.show()


import abc
import typing as tp
from librarius.domain.messages import (
    TAbstractMessage,
    TAbstractCommand,
    TAbstractQuery,
    TAbstractEvent,
)

if tp.TYPE_CHECKING:
    from librarius.domain.messages import AbstractCommand, AbstractQuery, AbstractEvent
    from librarius.domain.models import Entity
    from librarius.service.uow import TAbstractUnitOfWork

TAbstractHandler = tp.TypeVar("TAbstractHandler", bound="AbstractHandler")

class AbstractHandler(tp.Generic[TAbstractHandler, TAbstractMessage], abc.ABC):
    def __init__(self, uow: "TAbstractUnitOfWork"):
        self.uow = uow

    @abc.abstractmethod
    def __call__(self, message: "TAbstractMessage"):
        raise NotImplementedError

class AbstractCommandHandler(
    AbstractHandler["AbstractCommandHandler", TAbstractCommand],
    tp.Generic[TAbstractCommand],
    abc.ABC,
):
    @abc.abstractmethod
    def __call__(self, cmd: "TAbstractCommand") -> None:
        raise NotImplementedError

class AbstractEventHandler(
    AbstractHandler["AbstractEventHandler", TAbstractEvent],
    tp.Generic[TAbstractEvent],
    abc.ABC,
):
    @abc.abstractmethod
    def __call__(self, event: "TAbstractEvent") -> None:
        raise NotImplementedError

class AbstractQueryHandler(
    AbstractHandler["AbstractQueryHandler", TAbstractQuery],
    tp.Generic[TAbstractQuery],
    abc.ABC,
):
    @abc.abstractmethod
    def __call__(
        self, query: "TAbstractQuery"
    ) -> tp.Union[tp.Iterable["Entity"], "Entity"]:
        raise NotImplementedError


from typing import List, Dict
import csv

def get_unique_industries(path: str) -> List[str]:
    with open(path, mode="r") as file:
        data_file = csv.DictReader(file)
        list_data_file = []
        for data in data_file:
            list_data_file.append(data)

    industries = set([industry['industry']
                      for industry in list_data_file
                      if industry['industry'] != ''])
    return industries

def filter_by_industry(jobs: List[Dict], industry: str) -> List[Dict]:
    filtered_industries = []
    for job in jobs:
        if job['industry'] == industry:
            filtered_industries.append(job)
    return filtered_industries


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

import os, json, subprocess
from tempfile import NamedTemporaryFile

cur_dir = os.path.dirname(os.path.abspath(__file__))
file_path = cur_dir + "/../eval_video.py"

def run_and_check_result(cmd):
    cmd_result = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf8")

    data = json.loads(cmd_result.stdout)
    assert "video" in data
    assert type(data["video"]) == float
    assert data["video"] >= 0 and data["video"] <= 100

    # check output file
    with NamedTemporaryFile('w+t') as output:
        cmd.extend(["--output", output.name])
        cmd_result = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf8")

        data = json.loads(output.read())
        assert "video" in data
        assert type(data["video"]) == float
        assert data["video"] >= 0 and data["video"] <= 100

def check_video_vmaf(src_video, dst_video):
    cmd = ["python3", file_path, "--video_eval_method", "vmaf", "--src_video", src_video, "--dst_video", dst_video]

    run_and_check_result(cmd)

def check_yuv_video_vmaf(src_video, dst_video, video_size, pixel_format, bitdepth):
    cmd = ["python3", file_path, "--video_eval_method", "vmaf", "--src_video", src_video, "--dst_video", dst_video, \
                                 "--video_size", video_size, "--pixel_format", pixel_format, "--bitdepth", bitdepth]
    run_and_check_result(cmd)

def check_align_video_vmaf(src_video, dst_video, align_method):
    cmd = ["python3", file_path, "--video_eval_method", "vmaf", "--src_video", src_video, "--dst_video", dst_video]
    if align_method:
        cmd.extend(["--frame_align_method", align_method])

    run_and_check_result(cmd)

def test_y4m_yuv_compare(y4m_video, yuv_video):
    src_video = y4m_video["path"]
    dst_video = yuv_video["path"]
    check_video_vmaf(src_video, dst_video)
    check_video_vmaf(dst_video, src_video)

def test_yuv_yuv_compare(yuv_video):
    src_video = yuv_video["path"]
    dst_video = yuv_video["path"]
    video_size, pixel_format, bitdepth = yuv_video["video_size"], yuv_video["pixel_format"], yuv_video["bitdepth"]
    check_yuv_video_vmaf(src_video, dst_video, video_size=video_size, pixel_format=pixel_format, bitdepth=bitdepth)

def test_y4m_align_compare(y4m_video, align_method):
    src_video = y4m_video["path"]
    dst_video = y4m_video["path"]
    check_align_video_vmaf(src_video, dst_video, align_method)


import requests, os, json
from flask import Flask, render_template, redirect, url_for, request
from dotenv import load_dotenv
from anvil import Anvil, User

load_dotenv()

app = Flask(__name__)
anvil = Anvil()
user = anvil.load_user()
worlds = anvil.load_worlds(user)
anvil.current_world = worlds[0]

@app.route('/', methods=['POST', 'GET'])
def index():
    if request.method == 'POST':
        anvil.current_world = anvil.get_world(worlds, request.form["world"])

    return render_template('index.html', name=user.name, worlds=worlds, current_world=anvil.current_world)

@app.route('/new_article', methods=['POST', 'GET'])
def new_article():
    if request.method == 'POST':
        return redirect(url_for("/"))

    return render_template('new_article.html', name=user.name, worlds=worlds, current_world=anvil.current_world)

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


import smtplib
import re
import sys
import getpass
import random
import math
import time
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header    import Header
import socket
socket.setdefaulttimeout(10)

"""
created by sayansree paria
verson<1.0>

"""
userId=[]
password=[]
print("\n\n\n\temail spammer v1.0\n\tdevlovper sayansree paria\n\tinitiating dummy Ids import.... ")
path=os.path.dirname(os.path.realpath(__file__))
try:
    file=open(path+'\\Attribute.dat','r')
    lines=file.readlines()
    for i in lines:
        userId.append(i.split('|')[0])
        password.append(i.split('|')[1])
    del lines
except FileNotFoundError:
    print ("please define attributes.dat")
    #raise
    sys.exit(0)
except:
    print("unexpected fatal error encountered while accessing Attributes\nmake sure code has access permition")
    #raise
    sys.exit(0)
else:
    print ("\tdummy IDs successfully imported")
finally:
    file.close()

def check(email):
    if not re.match(r"[\w\.-_]+@[\w]+\.com",email):
        print('\tinvalid email format')
        #raise TypeError('userids not in format')
        sys.exit(0)

print('\tprechecking ID format...\n')
if(len(userId)==0):
    print('\nno IDs detected\nplease redefine Attribute file')
    #raise TypeError('userids not in format')
    sys.exit(0)

for i in userId:
    check(i)
    print(i+'\tvalid')

print('\tprecheck succesful')
print('\n\t{num} dummies will be used '.format(num=len(userId)))
print('\tInitiating authentication process\n\tthis may take several minutes')

module=[]
for i in range(len(userId)):

    try:
        server =smtplib.SMTP('smtp.gmail.com',587)
    except :
        print('\ncheck your internet connection')
        sys.exit(0)
    else:
        print('connection established\t\t' + userId[i])
    try:
        server.starttls()
        server.login(userId[i],password[i])
        module.append(server)
        del server
    except smtplib.SMTPConnectError:
        print('\nconnection error')
        server.quit()
    except smtplib.SMTPAuthenticationError:
        print('\nauthentication failed'+userId[i]+'*'*5+password[i][-3:])
        server.quit()
    except:
        print('\nunexpected error')
        server.quit()
        raise
    else:
        print('succesfully authinticated\t\t'+userId[i])

##needs sighting
target=input('enter target username:\t')
print('\t checking email')
check(target)
print(target+'\tvalid')
itr=input('enter no of attacks:\t')

print('\timporting payload')
payload=[]
try:
    file=open(path+'\\payload.txt','r')
    lines=file.readlines()
    for i in lines:
        payload.append(i)
    del lines
except FileNotFoundError:
    print ("please define payload.txt")
    sys.exit(0)
except:
    print("unexpected fatal error encountered while accessing payload\nmake sure code has access permition")
    sys.exit(0)
else:
    print ("\tpayload successfully imported")

finally:
    file.close()
tim=3.5*int(itr)
print('\tinitiating payload injection\n\t expected time {0}min,{1}sec'.format(tim%60,tim//60))
sublist=payload[0].split('|')
for i in range(int(itr)):
    rand=math.floor(random.random()*len(userId))

    msg= MIMEMultipart()
    msg['From'] = userId[rand]
    msg['To']=  target
    msg['Subject']= sublist[math.floor(random.random()*len(sublist))]
    body= payload[ 1+math.floor(random.random()*(len(payload)-1))]
    msg.attach(MIMEText(body,'html'))
    module[rand].sendmail(msg['From'],msg['To'],msg.as_string())
    del msg
    print('payload <{0}> using {1} successful '.format(i+1,userId[rand]))
    time.sleep(2+random.random()*3)# some improvement required

print('\t terminating server connections')

for i in module:
    i.quit()
print('\t payload successful\n\t devloper sayansree paria\n\t build v<1.0>')


# Train model
import sys
import time
import numpy as np
import torch.optim as optim
import pickle
import os
import torch.utils.data
import model as m
import argparse

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--doc_len", type=int, default=300)

    parser.add_argument("--lr", type=float, default=0.001)
    parser.add_argument("--batchSize", type=int, default=32)
    parser.add_argument("--num_workers", type=int, default=4)
    parser.add_argument("--flg_cuda", action='store_true')

    parser.add_argument("--optType", default='Adam')  # Vocabulary size

    parser.add_argument("--logInterval", type=int, default=1)  # Print test accuracy every n epochs
    parser.add_argument("--flgSave", action='store_true')
    parser.add_argument("--savePath", default='./')
    parser.add_argument("--randSeed", type=int, default=42)
    parser.add_argument("--inputPath", default="../aclImdb/df07f20K_stopEng_W_1gram/")
    parser.add_argument("--modelPath")

    args = parser.parse_args()

    torch.manual_seed(args.randSeed)  # For reproducible results

    if not os.path.isdir(args.savePath):
        os.mkdir(args.savePath)

    print('General parameters: ', args)

    print("Loading Data")
    # if args.modelName in ['Enc_SumLSTM', 'Enc_CNN_LSTM']:
    #trainset = m.MovieDataset(args.inputPath, 'train.json', transform=m.padToTensor(args.doc_len))
    testset = m.MovieDataset(args.inputPath, 'test.json', transform=m.padToTensor(args.doc_len))

    print('To Loader')
    if args.flg_cuda:
        #train_loader = torch.utils.data.DataLoader(trainset, batch_size=args.batchSize, shuffle=True, pin_memory=True)
        test_loader = torch.utils.data.DataLoader(testset, batch_size=args.batchSize, shuffle=False, pin_memory=True)
    else:
        #train_loader = torch.utils.data.DataLoader(trainset, batch_size=args.batchSize, shuffle=True, pin_memory=False)
        test_loader = torch.utils.data.DataLoader(testset, batch_size=args.batchSize, shuffle=False, pin_memory=False)

    print("Loading model")
    if args.flg_cuda:
        model = torch.load(args.modelPath + '_model.pt')
        model = model.cuda()
    else:
        model = torch.load(args.modelPath + '_model.pt', map_location=lambda storage, loc: storage)

    print(model)

    if args.optType == 'Adam':
        opt = optim.Adam(model.params, lr=args.lr)
    elif args.optType == 'SGD':
        opt = optim.SGD(model.params, lr=args.lr)

    print("Beginning Training")
    train_paras = {'log_interval': [args.logInterval, 1000], 'flg_cuda': args.flg_cuda,
                   'flgSave': args.flgSave, 'savePath': args.savePath}

    m = m.trainModel(train_paras, None, test_loader, model, opt)
    m._test(0)


import re
import requests
from bs4 import BeautifulSoup

def scrape_page_title(soup):
    """ Function to extract the title of an article from the scrapped code """
    title = soup.find('h1', class_='content__headline').get_text()
    title = re.sub('\n', '', title)
    return title

def scrape_page_topic(soup):
    """ Function to extract the topic name of an article from the scrapped code """
    try:
        label = soup.find('span', class_='label__link-wrapper').get_text()
    except AttributeError:
        return "-"
    label = re.sub('\n', '', label)
    return label

def scrape_page_authors(soup):
    """ Function to extract the names of authors of an article from the scrapped code """
    authors = [re.sub('\n', '', a.get_text()) for a in soup.find_all('span', itemprop="name")]
    authors = ' & '.join([str(a) for a in authors])
    return authors

def scrape_page_datetime(soup):
    """ Function to extract the date of publication of an article from the scrapped code """
    datetime = [re.sub('\n', '', d.get_text()) for d in soup.find_all('time', itemprop="datePublished")]
    datetime = re.sub('\xa0', ' ', datetime[0])
    return datetime

def scrape_page_description(soup):
    """ Function to extract the description of the article from the scrapped code """
    try:
        descr = soup.find('div', class_="content__standfirst").get_text()
        descr = re.sub('\n', '', descr)
    except AttributeError:
        return "-"
    return descr

def scrape_page_text(soup):
    """ Function to extract the text of the article from the scrapped code """
    text = soup.find('div', class_="content__article-body")
    text = [t.get_text() for t in text.find_all('p')]
    text = ' '.join(t for t in text)
    return text

def scrape_page_related_topics(soup):
    """ Function to extract the links of the related topics of a given article from the scrapped code """
    links = [a['href'] for a in soup.find_all('a', class_='submeta__link')]
    return links

def scrape_page(link):
    """ Function returns the different information extracted from the page of an article"""
    page = requests.get(link)
    soup = BeautifulSoup(page.content, 'html.parser')
    title = scrape_page_title(soup)
    link = link
    topic = scrape_page_topic(soup)
    related_topics_links = scrape_page_related_topics(soup)
    authors = scrape_page_authors(soup)
    datetime = scrape_page_datetime(soup)
    description = scrape_page_description(soup)
    text = scrape_page_text(soup)
    return title, link, topic, related_topics_links, authors, datetime, description, text

def home_page(link):
    """ Function returns the links to other articles from a given topic page """
    next_pages = []
    page = requests.get(link)
    soup = BeautifulSoup(page.content, 'html.parser')
    for n in soup.find_all('a', class_='fc-item__link'):
        if n['href'] not in next_pages:
            next_pages.append(n['href'])
    return next_pages


from django.contrib.auth.models import AbstractUser
from django.db import models

class User(AbstractUser):
    ADMIN = 'admin'
    MODERATOR = 'moderator'
    USER = 'user'
    USER_ROLE_CHOICES = [
        (ADMIN, 'admin'),
        (MODERATOR, 'moderator'),
        (USER, 'user'),
    ]

    confirmation_code = models.CharField(
        max_length=100,
        blank=True
    )
    username = models.CharField(
        max_length=150,
        unique=True,
        db_index=True
    )
    email = models.EmailField(
        verbose_name='Email',
        help_text='Введите адрес эл.почты',
        unique=True
    )
    bio = models.TextField(
        verbose_name='О пользователе',
        help_text='Расскажите о себе',
        blank=True,
        null=True
    )

    role = models.CharField(
        'Роль пользователя',
        max_length=20,
        choices=USER_ROLE_CHOICES,
        default=USER,
        blank=True,
    )

    @property
    def is_user(self):
        return self.role == self.USER

    @property
    def is_moderator(self):
        return self.role == self.MODERATOR

    @property
    def is_admin(self):
        return self.role == self.ADMIN or self.is_staff

    class Meta:
        verbose_name = 'Пользователь'
        verbose_name_plural = 'Пользователи'

    def __str__(self):
        return self.name


from typing import List, Optional, Union

def _make_mrkdown_block(mrkdown: str):
    """
    Wraps the mrkdown in a block kit block.
    """
    return {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": mrkdown,
        },
    }

def _make_header_block(heading: str):
    """
    Wraps the heading in a block kit block.
    """
    return {
        "type": "header",
        "text": {
            "type": "plain_text",
            "text": heading,
        },
    }

def _make_block_message(
    blocks: Union[None, str, dict, List[Union[str, dict]]],
    header: Optional[str] = None,
    visible_in_channel: bool = True,
):
    """
    Generates slack block kit messages from a variety of input types.

    str -> Wrap the str in a mrkdown section and in a top level response.

    dict -> Wrap the dict in a top level response.

    list -> Wrap the altered contents in a top level response.

        str -> Wrap the str in a mrkdown section

        dict -> add to top level response as is
    """
    output_blocks: List[dict] = []
    if blocks is None or blocks == "":
        return {}

    if isinstance(blocks, dict):
        output_blocks = [blocks]

    elif isinstance(blocks, list):
        formatted_blocks = []
        for block in blocks:
            if isinstance(block, str):
                formatted_blocks.append(_make_mrkdown_block(block))
            if isinstance(block, dict):
                formatted_blocks.append(block)
        output_blocks = formatted_blocks
    else:
        output_blocks = [_make_mrkdown_block(str(blocks))]

    if header:
        output_blocks = [_make_header_block(header)] + output_blocks
    return {
        "blocks": output_blocks,
        "response_type": "in_channel" if visible_in_channel else "ephemeral",
    }