# ======================================================================
# Pyroclastic Flow
#   Advent of Code 2022 Day 17 -- Eric Wastl -- https://adventofcode.com
#
# Python implementation by Dr. Dean Earl Wright III
# ======================================================================

# ======================================================================
#                         s i m u l a t i o n . p y
# ======================================================================
"Simulation for the Advent of Code 2022 Day 17 puzzle"

# ----------------------------------------------------------------------
#                                                                 import
# ----------------------------------------------------------------------
from collections import namedtuple

import chamber
import rocks

# ----------------------------------------------------------------------
#                                                                  types
# ----------------------------------------------------------------------
State = namedtuple('State', 'rock, wind, heights')

# ----------------------------------------------------------------------
#                                                              constants
# ----------------------------------------------------------------------
DEQUE_SIZE = 30

# ======================================================================
#                                                             Simulation
# ======================================================================

class Simulation(object):   # pylint: disable=R0902, R0903, R0205
    "Object for Pyroclastic Flow"

    def __init__(self, text=None, part2=False):

        # 1. Set the initial values
        self.part2 = part2
        self.text = text
        self.chamber = None
        self.rocks = None
        self.winds = None
        self.next_wind = 0

        # 2. Process text (if any)
        if text is not None and len(text) > 0:
            self.winds = text[0]
            self.chamber = chamber.Chamber(text="7", part2=part2)
            self.rocks = rocks.Rocks(text=rocks.ROCKS, part2=part2)

    def one_step(self):
        "Execute one step of the simulation"

        # 0. I need something to work with
        if not self.chamber:
            return

        # 1. If there are no falling rocks, set one falling
        if self.chamber.falling is None:
            self.chamber.add_rock(self.rocks.next_rock())

        # 2. Push the rock sideways
        self.chamber.push_rock(self.winds[self.next_wind])
        self.next_wind = (self.next_wind + 1) % len(self.winds)

        # 3. Let the rock fall
        self.chamber.move_down()

    def one_rock(self):
        "Execure the simulation until a rock comes to rest"

        # 1. Get the current number of rocks
        num_rocks = self.chamber.rocks

        # 2. Loop until we the number of rocks changes
        while num_rocks == self.chamber.rocks:

            # 3. Execute a single step of the simulation
            self.one_step()

        # 4. Return the number of fallen rocks
        return self.chamber.rocks

    def run_for_n_rocks(self, number, delta=0, verbose=False):
        "Run the simulation until the specific number of rocks come to rest"

        # 0. I need something to work with
        if not self.chamber:
            return None

        # 1. Loop until we get enough rocks
        while self.chamber.rocks < number:

            # 2. Execute the simulation until the number of rocks increase
            rocks = self.one_rock()
            if verbose and 0 == rocks % 100:
                print(f"run_for_n_rocks, {rocks} down, {number - rocks} more to go")

        # 3. Return the height of the tower of rocks
        return self.chamber.get_highest(delta=delta)

    def calculate_n_rocks(self, number, verbose=False):
        "Run the simulation until the specific number of rocks come to rest"

        # 0. I need something to work with
        if not self.chamber:
            return None

        # 1. Set up for finding a cycle
        seen = {}
        last_height = 0

        # 2. Loop until we get enough rocks (or we find a cycle)
        while self.chamber.rocks < number:

            # 3. Execute a single simulation step (side/down)
            rocks = self.one_rock()
            if 0 == rocks % 100:
                print(f"look for cycle, {rocks} down, {number - rocks} more to go")

            # 4. Generate the current state
            heights = self.chamber.relative_heights()
            next_rock = self.rocks.next_rock
            next_wind = self.next_wind
            cur_rocks = self.chamber.rocks
            cur_height = self.chamber.get_highest()
            state = State(rock=next_rock, wind=next_wind, heights=heights)

            # 5. If this is a new state, add it and continue
            if state not in seen:
                seen[state] = (cur_rocks, cur_height)
                continue

            # 6. Found a cycle
            if verbose:
                print(f"found a cycle after {self.chamber.rocks} rocks")
            start_rocks, start_height = seen[state]
            cycle_rocks = cur_rocks - start_rocks
            cycle_height = cur_height - start_height
            if verbose:
                print(f"At start of cycle: rocks={start_rocks}, height = {start_height}")
                print(f"At end of cycle: rocks={cur_rocks}, height = {cur_height}")
                print(f"  difference in rocks={cycle_rocks}, height={cycle_height}")
            cycles_to_do = (number - cur_rocks) // cycle_rocks
            add_rocks = cycles_to_do * cycle_rocks
            add_height = cycles_to_do * cycle_height
            if verbose:
                print(f"Cycles left={cycles_to_do}")
                print(f"  additional rocks={add_rocks}, height={add_height}")

            # 7. Finish off the similation
            self.chamber.rocks += add_rocks
            return self.run_for_n_rocks(number, delta=add_height)

            #
        # 3. Return the height of the tower of rocks
        return self.chamber.get_highest()

# ----------------------------------------------------------------------
#                                                  module initialization
# ----------------------------------------------------------------------
if __name__ == '__main__':
    pass

# ======================================================================
# end                    s i m u l a t i o n . p y                   end
# ======================================================================


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

# In[4]:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import csv

# In[5]:

pwd

# In[6]:

df = pd.read_csv('C:\\Users\\thinkpad\\desktop\\case_time_series.csv')
df.head()

# In[7]:

array_2d = np.linspace(1,5,12).reshape(4,3) # create numpy 2D array

print(array_2d) # print numpy array

# In[8]:

sns.heatmap(array_2d) # create heatmap

# In[14]:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import csv

# In[47]:

# Load dataset

Case_time_series_df = pd.read_csv("C:\\Users\\thinkpad\\desktop\\Case_time_series.csv")
Case_time_series_df.head()

# In[75]:

# Load dataset
case_time_series_df = pd.read_csv("C:\\Users\\thinkpad\\desktop\\case_time_series.csv")
case_time_series_df.head()

# In[76]:

array_2d = np.linspace(1,5,12).reshape(4,3) # create numpy 2D array

print(array_2d) # print numpy array

# In[77]:

sns.heatmap(array_2d) # create heatmap

# In[78]:

# set country name as index and drop Country Code, Indicator Name and Indicator Code

case_time_series_df  = case_time_series_df.drop(columns=['Total Confirmed', 'Total Recovered', 'Total Deceased'], axis=1).set_index('Date')
case_time_series_df

# In[79]:

# Create heatmap

plt.figure(figsize=(16,9))
sns.heatmap(case_time_series_df)

# In[81]:

# vmin and vmax parameter set limit of colormap

plt.figure(figsize=(16,9))

sns.heatmap(case_time_series_df, vmin = 0, vmax = 21)

# In[83]:

# change heatmap color using cmap

plt.figure(figsize=(16,9))

sns.heatmap(case_time_series_df, cmap="coolwarm")

# In[84]:

# center parameter

plt.figure(figsize=(16,9))

sns.heatmap(case_time_series_df, cmap="coolwarm", center = 10.0)

# In[85]:

# robust

plt.figure(figsize=(16,9))

sns.heatmap(case_time_series_df, robust = True)

# In[86]:

# annot (annotate) parameter

plt.figure(figsize=(16,9))

sns.heatmap(case_time_series_df, annot = True)

# In[61]:

# pass 2D Numpy array to annot parameter

annot_arr = np.arange(1,13).reshape(4,3) # create 2D numpy array with 4 rows and 3 columns

sns.heatmap(array_2d, annot= annot_arr)

# In[62]:

# fmp parameter - add text on heatmap cell

annot_arr = np.array([['a00','a01','a02'],
                      ['a10','a11','a12'],
                      ['a20','a21','a22'],
                      ['a30','a31','a32']], dtype = str)

sns.heatmap(array_2d, annot = annot_arr, fmt="s") # s -string, d - decimal

# In[87]:

# annot_kws parameter

plt.figure(figsize=(16,9))

annot_kws={'fontsize':10,
           'fontstyle':'italic',
           'color':"k",
           'alpha':0.6,
           'rotation':"vertical",
           'verticalalignment':'center',
           'backgroundcolor':'w'}

sns.heatmap(case_time_series_df, annot = True, annot_kws= annot_kws)

# In[88]:

# linewidths parameter - divede each cell of heatmap

plt.figure(figsize=(16,9))

sns.heatmap(case_time_series_df, linewidths=4)

# In[89]:

# linecolor parameter - change the color of heatmap line

plt.figure(figsize=(16,9))

sns.heatmap(case_time_series_df, linewidths=4, linecolor="k")

# In[90]:

# hide color bar with cbar parameter

plt.figure(figsize=(16,9))

sns.heatmap(case_time_series_df, cbar = False)

# In[91]:

# change style and format of color bar with cbar_kws parameter

plt.figure(figsize=(14,14))

cbar_kws = {"orientation":"horizontal",
            "shrink":1,
            'extend':'min',
            'extendfrac':0.1,
            "ticks":np.arange(0,22),
            "drawedges":True,
           }

sns.heatmap(case_time_series_df, cbar_kws=cbar_kws)

# In[92]:

# multiple heatmaps using subplots

plt.figure(figsize=(30,10))

plt.subplot(1,3,1) # first heatmap
sns.heatmap(case_time_series_df,  cbar=False, linecolor="w", linewidths=1)

plt.subplot(1,3,2) # second heatmap
sns.heatmap(case_time_series_df,  cbar=False, linecolor="k", linewidths=1)

plt.subplot(1,3,3) # third heatmap
sns.heatmap(case_time_series_df,  cbar=False, linecolor="y", linewidths=1)

plt.show()

# In[93]:

# set seaborn heatmap title, x-axis, y-axis label and font size

plt.figure(figsize=(16,9))

ax = sns.heatmap(case_time_series_df)

ax.set(title="Heatmap",
      xlabel="Condition",
      ylabel="Date",)

sns.set(font_scale=2) # set fontsize 2

# In[94]:

# Heatmap with keyword arguments (kwargs) parameter

plt.figure(figsize = (16,9))

kwargs = {'alpha':.9,'linewidth':10, 'linestyle':'--', 'rasterized':False, 'edgecolor':'w',  "capstyle":'projecting',}

ax = sns.heatmap(case_time_series_df,**kwargs)

# In[95]:

# sns heatmap correlation

plt.figure(figsize=(16,9))

sns.heatmap(case_time_series_df.corr(), annot = True)

# In[96]:

# Upper triangle heatmap

plt.figure(figsize=(16,9))

corr_mx = case_time_series_df.corr() # correlation matrix

matrix = np.tril(corr_mx) # take lower correlation matrix

sns.heatmap(corr_mx, mask=matrix)

# In[97]:

# Lower triangle heatmap

plt.figure(figsize=(16,9))

corr_mx = case_time_series_df.corr() # correlation matrix

matrix = np.triu(corr_mx) # take upper correlation matrix

sns.heatmap(corr_mx, mask=matrix)

# In[ ]:


"""
    百度贴吧爬虫
        翻页获取标题及url

    ctrl+f 响应内容中搜索
    html中绿色字体表示被注释了，xpath不会识别:
        解决方法:
           1 需要修改user-Agent换低端一些的浏览器高端浏览器有时会将html信息注释，但是他
           靠引擎仍可以玩出信息，低端浏览器不行，所以不会注释，换个低端浏览器请求头就好了

           2 将data数据的注释符号去掉data.decode().replace("<--!","").replace("-->","")

    找翻页url时尽量不要使用索引，容易变化
"""

import requests
from lxml import etree

class Tieba:
    def __init__(self, name):
        self.url = "https://tieba.baidu.com/f?ie=utf-8&kw={}".format(name)
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKi'
            't/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'

            # 'User-Agent': "Mozilla/4.0 (compatible; MSIE 5.01; windows NT 5.0; DigExt)"
        }

    def get_data(self, url):
        response = requests.get(url, headers=self.headers)
        return response.content

    def parse_data(self, data):
        # 创建elements对象
        data = data.decode().replace("<!--", "").replace("-->", "")
        html = etree.HTML(data)

        el_list = html.xpath('//li/div/div[2]/div[1]/div[1]/a')
        # print(len(el_list))

        data_list = []

        for el in el_list:
            temp = {}
            temp['title'] = el.xpath("./text()")[0]
            temp['link'] = 'http://tieba.baidu.com' + el.xpath("./@href")[0]
            data_list.append(temp)

        # 获取下一页url
        try:
            next_url = 'https:' + html.xpath('//a[contains(text(), "下一页>")]/@href')[0]
        except:
            next_url = None

        return data_list, next_url

    def save_data(self, data_list):
        # 假装保存
        for data in data_list:
            print(data)

    def run(self):
        # url
        # headers
        next_url = self.url
        while True:
            # 发送请求获取响应
            data = self.get_data(next_url)
            # 从响应中提取数据(数据和翻页用的url)
            data_list, next_url = self.parse_data(data)

            self.save_data(data_list)

            print(next_url)
            # 判断是否终结
            if next_url == None:
                break

if __name__ == '__main__':
    a = Tieba("星际争霸")
    a.run()

python/python学习小分队打卡/课程代码/爬虫/爬虫课程1/3/13 百度贴吧.py


"""
    Toolbar giving access to experimental MXS -> Python translation
"""
#pylint: disable= import-error, invalid-name, too-few-public-methods
from PySide2.QtWidgets import (QApplication, QWidget, QDockWidget,
        QVBoxLayout, QLabel, QTextEdit, QStyle, QToolBar, QCommonStyle)
from PySide2.QtGui import (QSyntaxHighlighter, QTextCharFormat,
        QFont, Qt, QBrush, QColor, QKeyEvent)
from PySide2 import QtCore
from PySide2.QtCore import QMimeData, QEvent
from pygments.lexers.python import PythonLexer
from pygments.token import Token
from mxs2py import topy
from qtmax import GetQMaxMainWindow

def makeFormat(weight, color):
    """Create a format from a font weight and color"""
    f = QTextCharFormat()
    f.setFontWeight(weight)
    f.setForeground(color)
    return f

# Color scheme for the syntax highlighted QTextEdit

COMMENT_FORMAT=makeFormat(QFont.Normal, QBrush(QColor(0xd8, 0x98, 0x68)))
OPERATOR_FORMAT=makeFormat(QFont.Bold, QBrush(QColor(0x66, 0x87, 0x99)))
KEYWORD_FORMAT=makeFormat(QFont.Normal, QBrush(QColor(0x8b,0xe5,0x7a)))
LITERAL_FORMAT=makeFormat(QFont.Normal, QBrush(QColor(0xcf,0x6a,0xa0)))
IDENTIFIER_FORMAT=makeFormat(QFont.Normal, QBrush(QColor(220, 220, 220)))
BUILTIN_FORMAT=makeFormat(QFont.Normal, QBrush(QColor(200, 100, 150)))
DEFAULT_FORMAT = makeFormat(QFont.Normal, QBrush(QColor(220, 0, 0)))

COLOR_SCHEME = {
    Token.Text: IDENTIFIER_FORMAT,
    Token.Text.Whitespace: IDENTIFIER_FORMAT,
    Token.Error: IDENTIFIER_FORMAT,
    Token.Other: IDENTIFIER_FORMAT,
    Token.Keyword: KEYWORD_FORMAT,
    Token.Keyword.Namespace: KEYWORD_FORMAT,
    Token.Keyword.Constant: LITERAL_FORMAT,
    Token.Name: IDENTIFIER_FORMAT,
    Token.Name.Function: IDENTIFIER_FORMAT,
    Token.Name.Builtin: BUILTIN_FORMAT,
    Token.Name.Builtin.Pseudo: BUILTIN_FORMAT,
    Token.Name.Namespace: LITERAL_FORMAT,
    Token.Literal: LITERAL_FORMAT,
    Token.Literal.String: LITERAL_FORMAT,
    Token.Literal.String.Double: LITERAL_FORMAT,
    Token.Literal.String.Single: LITERAL_FORMAT,
    Token.Literal.Number: LITERAL_FORMAT,
    Token.Literal.Number.Integer: LITERAL_FORMAT,
    Token.Literal.Number.Float: LITERAL_FORMAT,
    Token.Operator: OPERATOR_FORMAT,
    Token.Operator.Word: OPERATOR_FORMAT,
    Token.Punctuation: OPERATOR_FORMAT,
    Token.Comment: COMMENT_FORMAT,
    Token.Comment.Single: COMMENT_FORMAT,
    Token.Generic: IDENTIFIER_FORMAT
}

class GenericHighlighter(QSyntaxHighlighter):
    """Initialize a syntax highlighter"""
    def highlightBlock(self, text):
        """higlight a block of text"""
        results = self.lexer.get_tokens_unprocessed(text)
        for index, tokentype,value in results:
            style = COLOR_SCHEME[tokentype] if tokentype in COLOR_SCHEME else DEFAULT_FORMAT
            if tokentype not in COLOR_SCHEME:
                print(tokentype)
            self.setFormat(index, len(value),style)

class PythonHighlighter(GenericHighlighter):
    """Intialize a python syntax highlighter"""
    lexer = PythonLexer()

class CustomEditor(QTextEdit):
    """Custom editor for showing python code and
    converting mxs code to python when dropped or pasted."""
    def insertFromMimeData(self, source):
        """Insert code from mime data"""
        if source.hasText():
            # get the text
            snippet = source.text()
            # try to convert it to python
            QApplication.setOverrideCursor(Qt.WaitCursor)
            try:
                (output, err) = topy(snippet)
                pysnippet = output + "\n"
                source = QMimeData()
                source.setText(pysnippet)
                if err:
                    print(err)
            except Exception as e: #pylint: disable=broad-exception-caught
                print(e)
            QApplication.restoreOverrideCursor()
        super().insertFromMimeData(source)

    def keyPressEvent (self, event):
        """Simulate a tab"""
        if event.key() == Qt.Key_Tab:
            event = QKeyEvent (QEvent.KeyPress, Qt.Key_Space,
                Qt.KeyboardModifiers(event.nativeModifiers()),
                "    ")
        super().keyPressEvent(event)

class ScriptEditor(QWidget):
    """
    Custom dialog attached to the 3ds Max main window
    Message label and action push button to create a cylinder in the 3ds Max scene graph
    """
    def __init__(self, parent=None):
        """Initialize the script editor"""
        super().__init__(parent)
        self.init_ui()
    def init_ui(self):
        """ Prepare Qt UI layout for custom dialog """
        main_layout = QVBoxLayout()
        # Add some explanation of what to do
        main_layout.addWidget(QLabel("Drop or Paste MXS Snippets to get them translated to Python"))
        # Add a toolbar
        toolbar = QToolBar()

        def clear_it():
            """Clear editor contents"""
            self.pyEdit.clear()
        def run_it():
            """Run editor contents"""
            exec(self.pyEdit.toPlainText()) #pylint: disable=exec-used
        main_layout.addWidget(toolbar)
        self.common_style=QCommonStyle()
        print(self.common_style)
        self.runAction = toolbar.addAction(
                self.common_style.standardIcon(QStyle.SP_ArrowRight), "Run Snippet", run_it)
        self.clearAction = toolbar.addAction(
                self.common_style.standardIcon(QStyle.SP_DialogCancelButton), "Clear", clear_it)

        self.pyEdit=CustomEditor()
        self.pyEdit.setStyleSheet("background-color: #333333;")
        def enable_text_actions():
            empty = self.pyEdit.document().isEmpty()
            self.runAction.setEnabled(not empty)
            self.clearAction.setEnabled(not empty)
        self.pyEdit.textChanged.connect(enable_text_actions)

        main_layout.addWidget(self.pyEdit)
        self.setLayout(main_layout)
        # syntax highlighting
        self.pyHighlighter = PythonHighlighter(self.pyEdit.document())
        enable_text_actions()

def new_editor(tabto=None, floating=False, dockingarea=QtCore.Qt.RightDockWidgetArea):
    """Create an editor dock widget."""
    main_window = GetQMaxMainWindow()
    # create and setup a console
    recorder = ScriptEditor()
    dock_widget = QDockWidget(main_window)
    dock_widget.setWidget(recorder)
    dock_widget.setObjectName("py_editor")
    dock_widget.setWindowTitle("Experimental MXS -> Python Translator")
    main_window.addDockWidget(dockingarea, dock_widget)
    if not tabto is None:
        tabw = main_window.findChild(QWidget, tabto)
        main_window.tabifyDockWidget(tabw, dock_widget)
    dock_widget.setFloating(floating)
    dock_widget.show()
    return recorder

if __name__ == "__main__":
    theEditor = new_editor()


#!/usr/bin/env python3
import dendropy
import argparse
import sys

def parse_args(args=None):
    parser = argparse.ArgumentParser(description='Load & prune trees.')
    parser.add_argument('File', metavar='FILE', help='File containing trees')
    parser.add_argument('Burnin', type=int, help='How many trees to discard as burnin')
    parser.add_argument('Outf', help='Output file name')
    return parser.parse_args(args)

def main():
    retain_list, tl = get_names(file)
    newtl = tree_ops(tl, burnin, retain_list)
    print("Writing out pruned trees")
    newtl.write_to_path(outf, schema='nexus')

def get_names(file):
    '''Opens trees using Dendropy, and determines which taxa are extinct'''
    print("Reading in trees")
    tl = dendropy.TreeList.get_from_path(file, schema="nexus", preserve_underscores=True)
    print(tl)
    retain_list = []
    print("Getting list of taxa to keep")
    for name in tl.taxon_namespace:
        if str(name).endswith('X\'', 2) is False:
            retain_list.append(name)
    print("To retain:", retain_list)
    return(retain_list, tl)

def tree_ops(tl, burnin, retain_list):
    '''Extract subtrees of extant taxa'''
    print("Disgaurding burnin")
    newtl = tl[burnin:]
    newTrees = dendropy.TreeList()
    print("Extracting good taxa")
    for t in newtl:
        t = t.extract_tree_with_taxa(retain_list)
        newTrees.append(t)
    return(newTrees)

if __name__ == "__main__":
    args = parse_args()
    file = args.File
    burnin = args.Burnin
    outf = args.Outf

    main()


class Solution:
    def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int:
        q=deque()
        visited = set()
        directions = [(-1,0),(1,0),(0,-1),(0,1)]
        def isValid(row,col):

            return 0 <= row < len(maze) and 0 <= col < len(maze[0]) and maze[row][col]=="."

        def isEnd(row,col):
            return (0 == col or col == len(maze[0])-1 or row == 0 or row == len(maze)-1) and (row,col) != tuple(entrance)
        q.append((entrance[0],entrance[1]))
        ans = 0
        while(q):
            x = len(q)
            for _ in range(x):
                r,c = q.popleft()
                if (r,c) in visited:
                     continue
                visited.add((r,c))
                if isEnd(r,c):

                    return ans
                for i,j in directions:
                    if isValid(i+r,c+j):
                        q.append((r+i,c+j))
            ans+=1
        return -1


import time
from datetime import datetime

import pandas
import six
import ujson
import validators
from traitlets import HasTraits

from ..enums import CompetitionMetric, CompetitionType, DatasetFormat
from ..exceptions import (
    MalformedCompetition,
    MalformedDataset,
    MalformedMetric,
    MalformedTargets,
)

class CompetitionSpec(HasTraits):
    def __init__(
        self,
        title,
        type,
        expiration,
        prize,
        metric,
        dataset,
        subtitle="",
        targets=None,
        dataset_type=DatasetFormat.NONE,
        dataset_kwargs=None,
        dataset_key=None,
        num_classes=2,
        when=None,
        answer=None,
        answer_type=DatasetFormat.NONE,
        answer_delay=0,
    ):
        CompetitionSpec.validate(
            title,
            subtitle,
            type,
            expiration,
            prize,
            metric,
            dataset,
            targets,
            dataset_type,
            dataset_kwargs,
            dataset_key,
            num_classes,
            when,
            answer,
            answer_type,
            answer_delay,
        )
        self.title = title
        self.subtitle = subtitle
        self.type = type
        self.expiration = expiration
        self.prize = prize
        self.metric = metric

        # provide dataset if available
        self.dataset = dataset

        if isinstance(dataset, six.string_types) and validators.url(dataset):
            self.dataset_type = dataset_type
        else:
            self.dataset_type = DatasetFormat.NONE

        self.dataset_kwargs = dataset_kwargs if dataset_kwargs else {}
        self.dataset_key = dataset_key

        self.num_classes = num_classes

        if type == CompetitionType.PREDICT:
            self.targets = targets
        else:
            self.targets = None

        self.when = when

        self.answer = answer
        if isinstance(answer, six.string_types) and validators.url(answer):
            self.answer_type = (
                self.dataset_type if answer_type == DatasetFormat.NONE else answer_type
            )
        else:
            self.answer_type = DatasetFormat.NONE

        self.answer_delay = answer_delay

    def to_dict(self):
        ret = {}
        ret["title"] = self.title
        ret["subtitle"] = self.subtitle
        ret["type"] = self.type.value
        ret["expiration"] = (
            self.expiration.timestamp()
            if hasattr(self.expiration, "timestamp")
            else float(
                (
                    time.mktime(self.expiration.timetuple())
                    + self.expiration.microsecond / 1000000.0
                )
            )
        )
        ret["prize"] = self.prize
        ret["metric"] = self.metric.value
        ret["dataset"] = (
            self.dataset
            if isinstance(self.dataset, six.string_types)
            else self.dataset.to_json()
        )
        ret["dataset_type"] = self.dataset_type.value
        ret["dataset_kwargs"] = self.dataset_kwargs
        ret["dataset_key"] = self.dataset_key
        ret["num_classes"] = self.num_classes
        ret["targets"] = list(self.targets) if self.targets is not None else None
        ret["when"] = (
            ""
            if self.when is None
            else self.when.timestamp()
            if hasattr(self.when, "timestamp")
            else float(
                (time.mktime(self.when.timetuple()) + self.when.microsecond / 1000000.0)
            )
        )
        ret["answer"] = (
            self.answer
            if isinstance(self.answer, six.string_types)
            else self.answer.to_json()
            if isinstance(self.answer, pandas.DataFrame)
            else self.dataset
        )
        ret["answer_delay"] = self.answer_delay
        return ret

    def to_json(self):
        return ujson.dumps(self.to_dict())

    @classmethod
    def from_dict(cls, val):
        d = {}
        for k, v in val.items():
            if k == "type":
                d[k] = CompetitionType(v)
            elif k == "metric":
                d[k] = CompetitionMetric(v)
            elif k == "dataset_type" or k == "answer_type":
                d[k] = DatasetFormat(v)
            elif k == "dataset" or k == "answer":
                if isinstance(v, six.string_types):
                    if v in ("", "hidden") or validators.url(v):
                        d[k] = v
                    else:
                        v = ujson.loads(v)

                if isinstance(v, dict) or isinstance(v, list):
                    d[k] = pandas.DataFrame(v)
                else:
                    d[k] = v

            elif k == "expiration" or k == "when":
                if isinstance(v, six.string_types):
                    if not v:
                        d[k] = None
                    else:
                        d[k] = datetime.strptime(v, "%Y-%m-%d %H:%M:%S")
                else:
                    if v:
                        d[k] = datetime.fromtimestamp(float(v))
            elif k in (
                "title",
                "subtitle",
                "prize",
                "dataset_kwargs",
                "dataset_key",
                "num_classes",
                "targets",
                "answer_delay",
            ):
                d[k] = v
        return cls(**d)

    @classmethod
    def from_json(cls, json):
        val = ujson.loads(json)
        return CompetitionSpec.from_dict(val)

    @staticmethod
    def validate(
        title,
        subtitle,
        type,
        expiration,
        prize,
        metric,
        dataset,
        targets,
        dataset_type,
        dataset_kwargs,
        dataset_key,
        num_classes,
        when,
        answer,
        answer_type,
        answer_delay,
    ):
        if not isinstance(type, CompetitionType):
            raise MalformedCompetition()

        if not isinstance(metric, CompetitionMetric):
            raise MalformedMetric()

        if isinstance(dataset, six.string_types) and validators.url(dataset):
            if not isinstance(dataset_type, DatasetFormat):
                raise MalformedDataset()

        if type == CompetitionType.PREDICT:
            if targets is None:
                raise MalformedTargets()

    def __repr__(self):
        return (
            "<title- "
            + str(self.title)
            + ", type-"
            + str(self.type.value)
            + ", expiration-"
            + str(self.expiration)
            + ", prize-"
            + str(self.prize)
            + ", dataset-"
            + str(self.dataset)
            + ", metric-"
            + str(self.metric.value)
            + ", num_classes-"
            + str(self.num_classes)
            + ", targets-"
            + str(self.targets)
            + ">"
        )


import turtle

numSides = int(input("Enter the number of sides."))
lenSides = int(input("Enter the length of sides."))

internalAngle = 360/numSides

thomas = turtle.Turtle()
thomas.color("green")

for i in range(numSides):
    thomas.forward(lenSides)
    thomas.right(internalAngle)

thomas.screen.exitonclick()


import os
import sys
import subprocess
import datetime
import pickle
import pandas as pd
from django.core.wsgi import get_wsgi_application
import json
from dateutil import parser

def find_latest_csvname(specific_date='<YYYYMMDD>'):
    """Finds the newest csv name in the drive.  Can pull a specific date
       by filling in the specific date field.

    Keyword arguments:
        specific date: [string in YYYYMMDD date format ] This is optional.  Standard it to leave blank and it will
                        pull the latest.

    Returns:
        returns a csv filename to pull from google storage bucket.
    """
    command = ['gsutil','ls','gs://rooftop-data/properties_data/']
    out = subprocess.check_output(command)
    csv_list = str(out).split('\\n')
    parsed_csv_list = [x[x.find('data_')+5:-4] for x in csv_list]
    date_csv_list = [x for x in parsed_csv_list if len(x)==8]
    dates = [parser.parse(x) for x in date_csv_list]
    now = datetime.datetime.now()
    newest = max(dt for dt in dates if dt < now)
    csv_filename = 'data_{}.csv'.format(newest.strftime('%Y%m%d'))
    return csv_filename

def fetch_from_google_storage(google_storage_bucket, path_to_file, filename, destination_folder):
    """Pulls file from google cloud storage bucket

    Keyword arguments:
        google_storage_bucket: [string] name of google storage bucket e.g. django-uploads
        path_to_file: [string] path to the folder of the file you want to pull from
        google storage
        filename: [string] name of the file you want to pull e.g. data_<YYYYMMDD>.csv
        destination_folder: [string] path to the folder you want to save the pulled file in

    Returns:
    returns success status if successful
    """
    pull_path = 'gs://{}/{}/{}'.format(google_storage_bucket, path_to_file, filename)
    print('pull path is {}'.format(pull_path))
    destination_path = os.getcwd() + '/{}/{}'.format(destination_folder, filename)
    print('destination path is {}'.format(destination_path))
    gsutil_command = ['gsutil', 'cp', pull_path,destination_path]
    try:
        subprocess.call(gsutil_command)
        print('pulled file {} successfully from the {} google storage bucket'.format(filename,google_storage_bucket))
    except Exception as e:
        print('pull failed. error message: {}'.format(e))

def pull_last_run_date(pickle_path):
    """Pulls the last run date from a pickle file.

    Keyword arguments:
        pickle_path: [string] path to the pickle file that has the last_run_date

    Returns:
    last_run_date_str: [string] path to
    """
    pickle_path = os.path.join(os.getcwd(), pickle_path)
    last_run_date_file= open(pickle_path, 'rb')
    last_run_date = pickle.load(last_run_date_file)
    last_run_date_str = last_run_date.strftime('%Y%m%d')
    return last_run_date_str

def generate_csv_filename(last_run_date_str):
    """Generates name of csv file given a date

    Keyword arguments:
        last_run_date_str: [string] the last run date for the cron job, as a string

    Returns:
        csv_filename: [string] the filename for the csv file with the data stored in it
    """
    csv_filename = 'data_{}.csv'.format(last_run_date_str)
    return csv_filename

def create_property_object(df, idx):
    """Creates django property model object when given a dataframe of new
       listing data and index number

    Keyword arguments:
        df: [pandas dataframe object] dataframe object of home listing data
        idx: index in the dataframe of the house we are turning into a property object

    Returns:
        home: [django model] property object generated from the data in the row
        of the listing dataframe
    """
    home, created = ListProperty.objects.update_or_create(

            # Address
            street_address_1 = df.iloc[idx]['address_address_line1'],
            street_address_2 = df.iloc[idx]['address_unit'],
            city = df.iloc[idx]['address_city'],
            state = df.iloc[idx]['address_state'],
            zip_code = df.iloc[idx]['address_zipcode'],
            latitude = df.iloc[idx]['address_lat'],
            longitude = df.iloc[idx]['address_lon'],

            # Address keys
            hc_slug=df.iloc[idx]['Unnamed: 0'],
            lowercase_slug = df.iloc[idx]['Unnamed: 0'].lower(),

            # Listing data
            list_price = df.iloc[idx]['listing_data_list_price'],
            num_bedrooms = df.iloc[idx]['listing_data_num_bedrooms'],
            num_bathrooms = df.iloc[idx]['listing_data_num_bathrooms'],
            building_area_sq_ft = df.iloc[idx]['listing_data_building_area_sq_ft'],
            home_type = df.iloc[idx]['listing_data_home_type'],

            # Features
            num_floors = df.iloc[idx]['features_floors'],
            year_built = df.iloc[idx]['features_year_built'],
            listing_status = df.iloc[idx]['features_listing_status'],
            interior_features = df.iloc[idx]['features_interior_features'],
            flooring = df.iloc[idx]['features_flooring'],
            is_foreclosure = df.iloc[idx]['features_is_foreclosure'],
            local_school_quality = df.iloc[idx]['features_school_score'],
            remax_url = df.iloc[idx]['features_remax_url'],
            rx_num_half_bath = df.iloc[idx]['features_num_half_bath'],
            has_septic = df.iloc[idx]['features_has_septic'],
            has_pool = df.iloc[idx]['features_has_pool'],
            rx_date_listed = df.iloc[idx]['features_start_date_on_site'],
            subdivision = df.iloc[idx]['features_subdivision'],
            has_established_subdivision = df.iloc[idx]['features_has_established_subdivision'],
            has_well = df.iloc[idx]['features_has_well'],
            has_garage = df.iloc[idx]['features_has_garage'],
            no_pool_well_septic = df.iloc[idx]['features_no_pool_well_septic'],
            garage_size = df.iloc[idx]['features_garage_detail'],
            rx_num_full_bath = df.iloc[idx]['features_num_full_bath'],
            mls_listing_id = df.iloc[idx]['features_MLS'],
            description = df.iloc[idx]['features_desc'],

            # Images
            img_path_header = '/img/'+df.iloc[idx]['Unnamed: 0']+'/img1.jpg',
            img_paths_gallery = url_string_to_json(';'.join(['/img/' + df.iloc[idx]['Unnamed: 0'] + '/img'
                                     + str(x) + '.jpg' for x, y in enumerate(df.iloc[idx]
                                     ['images_img_gallery'].split(';'))]))
        )
    return home, created

def cleaned_dataframe_from_csv(csv_file_path):
    """Takes a csv, turns it into a dataframe, and cleans with ribbon buy box

    Keyword arguments:
        csv_file_path: [string] location of the csv dataframe

    Returns:
        cleaned_and_filtered_df: [pandas dataframe object] dataframe object
    """
    csv_path = os.path.join(os.getcwd(), csv_file_path)
    df = pd.read_csv(csv_path)
    df = df.where((pd.notnull(df)), None)
    df = df.drop_duplicates(subset='Unnamed: 0')
    cleaned_and_filtered_df = df[
        (df['listing_data_num_bathrooms']>=2)
        & (df['listing_data_num_bedrooms']>=3)
        & (df['listing_data_home_type']=='Single Family')
        & (df['listing_data_list_price']>=150000)
        & (df['listing_data_list_price']<=500000)
        & (df['features_year_built']>=1990)
        ]
    return cleaned_and_filtered_df

def iterate_through_properties_and_create_models(df):
    """Iterates through homes in a dataframe, creates a property object, and saves to db

    Keyword arguments:
        df: [django dataframe object] this is the cleaned and filtered dataframe of
        the houses

    Returns:
        There is no output but this simply loops through the dataframe, creates a property
        object of each home which is represented by a row in the dataframe, and then
        attempts to save the created property object to the database.

        If a property object with the same slug already exists in the dataframe,
        this will fail.
    """
    print('Loading list properties to db:')
    save_count = 0
    fail_count = 0
    for idx in range(len(df)):
        try:
            property_object, created = create_property_object(df, idx)
            save_count += created
        except Exception as e:
            fail_count += 1
            print('saving home failed, exception: {}'.format(e))
    print('saved: {} failed: {}  success_rate: {}%'.format(save_count,fail_count,
                                                            str(100*float(save_count/(save_count
                                                                      +fail_count)))))
def setup_environment():
    """Setup the environment"""
    sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ribbonsemble.settings")
    application = get_wsgi_application()
    print('import properties...')

def check_currentdir_location():
    """Checks if you are running script from the correct directory (one with manage.py)

    Returns:
        True if you are current in current working directory, else raise exception
        and abort.
    """
    managepy_exists = os.path.isfile('manage.py')
    README_exists = os.path.isfile('README.md')
    if managepy_exists and README_exists:
        return True
    else:
        sys.exit('You must run this script from the main working directory (with manage.py)')

def url_string_to_json(url_string):
    """This take is string of urls and returns a JSON of image gallery paths

    Keyword arguments:
        url_string: [string] string of image gallery paths

    Returns:
        image_gallery_json: [JSON] a json of the image gallery paths
    """
    json_list = url_string.split(';')
    image_gallery_json = json.dumps(json_list)
    return image_gallery_json

if __name__ == '__main__':
    # Check current location
    check_currentdir_location()
    # Setup django environment.  Note must run from the rooftop
    setup_environment()
    # Need to import Property here and not at top of script because need env setup first.
    from hedonic_model.models import ListProperty
    # Get the name of the more recently updated csv file
    csv_filename = find_latest_csvname()
    # Pull csv from google cloud storage
    fetch_from_google_storage('rooftop-data','properties_data',csv_filename,
                              'scripts/csv_data')
    properties_df = cleaned_dataframe_from_csv('scripts/csv_data/{}'.format(csv_filename))
    iterate_through_properties_and_create_models(properties_df)


from dagster import get_dagster_logger, job, op
from dagster.utils import file_relative_path
import config as cfg
from dagster_airbyte import airbyte_resource, airbyte_sync_op
from dagster_dbt import dbt_cli_resource, dbt_run_op
from dagster_graphql import DagsterGraphQLClient

sync_requestor = airbyte_sync_op.configured(
    {"connection_id": cfg.db_source_requestor["connection_id"]}, name="sync_requestor"
)

sync_provider1_base_consents = airbyte_sync_op.configured(
    {"connection_id": cfg.db_source_provider1["connection_id"]},
    name="sync_provider1_base_consents",
)

DBT_PROJECT_DIR = file_relative_path(__file__, cfg.dbt["project_dir"])

base_consents = dbt_run_op.alias(name="base_consents")

@op
def exec_provider1_base_consents_job(something):
    client = DagsterGraphQLClient(
        hostname=cfg.dagster_provider1["hostname"],
        port_number=cfg.dagster_provider1["port_number"],
    )
    client.submit_job_execution("crossing_datasets_job")

@job(
    resource_defs={
        "airbyte": airbyte_resource.configured(
            {
                "host": cfg.airbyte["host"],
                "port": cfg.airbyte["port"],
            }
        ),
        "dbt": dbt_cli_resource.configured(
            {
                "project_dir": DBT_PROJECT_DIR,
                "profiles_dir": DBT_PROJECT_DIR,
                "models": ["base_consents"],
            }
        ),
    }
)
def base_consents_job():
    logger = get_dagster_logger()
    logger.debug("Starting Job...")

    exec_provider1_base_consents_job(
        sync_provider1_base_consents(
            start_after=base_consents(start_after=sync_requestor())
        )
    )


'''
** qsi
| This module finds the equations for each
| interval based on the data set using QSI.
'''
import numpy as np
from api.scripts import elimination as el

'''
** poly_qsi
| This function creates polynomials per intervals.
- - -
** params
| - xv: A vector containing the x values
| - yv: A vector containing the y values
| - x: Value to be evaluated using the appropriate polynomial
- - -
** returns
| - polynomials: All polynomials per interval
| - intervals: All intervals for each polynomial
| - intindex: The index of the chosen equation in the list
| - y: Approximate of f_n(x) using the appropriate function of an interval (based on x)
'''
def poly_qsi(xv, yv, x):
    # ** Declaration
    m, n = [], len(xv) - 1 # Store the matrix and no. of intervals.
    polynomials, intervals = [], [] # Store the polynomials and its corresponding interval.
    intindex, y = 0, 0 # Store the equation's index in the list and approximate of f_n(x).

    # Sort xv with their corresponding values in yv.
    modified_merge_sort(xv, yv, 0, len(xv) - 1)

    # Add rows to the matrix for condition 1.
    for _ in range(1, n):
        coefficients = [xv[_] ** 2, xv[_], 1, yv[_]] # Store the coefficients.
        m.append(get_row(coefficients, n, 3 * (_ - 1)))
        m.append(get_row(coefficients, n, 3 * _))

    # Add rows to the matrix for condition 2.
    m.append(get_row([xv[0] ** 2, xv[0], 1, yv[0]], n))
    m.append(get_row([xv[n] ** 2, xv[n], 1, yv[n]], n, 3 * (n - 1)))

    # Add rows to the matrix for condition 3.
    for _ in range(1, n):
        # ** Declaration
        coefficients = [2 * xv[_], 1] # Store the coefficients.
        a1, a2 = 3 * (_ - 1), 3 * _ # Store the adders.

        # Add a row to the matrix for condition 3.
        m.append([coefficients[i - a1] if i >= a1 and i <= a1 + 1 else -coefficients[i - a2] if i >= a2 and i <= a2 + 1 else 0 for i in range(3 * n + 1)])
    m = np.delete(np.array(m), 0, axis = 1) # Delete the first column since a1 = 0.
    result = el.gauss_jordan(m) # Get the result including the solution.

    # Return None if the result is None.
    if result == None:
        return None
    solution = [np.round(num, 4) for num in np.concatenate(([0], result['solution']))]

    # Create the polynomials per interval.
    for _ in range(len(solution) // 3):
        # Append the polynomial to the list.
        a = 3 * (_) # Get the adder for morphing.
        polynomials.append(str(solution[a]) + 'x^2 + ' + str(solution[a + 1]) + 'x + ' + str(solution[a + 2]))
        intervals.append([xv[_], xv[_ + 1]]) # Append the intervals.

        # Get the approximate value if it is within the interval.
        if x >= xv[_] and x <= xv[_ + 1]:
            y, intindex = (solution[a] * (x ** 2)) + (solution[a + 1] * x) + solution[a + 2], _
    # Return a dictionary (polynomials and y).
    return { 'polynomials' : polynomials, 'intervals' : intervals, 'intindex' : intindex, 'y' : np.round(y, 4) if x >= xv[0] and x <= xv[len(xv) - 1] else None }

'''
** modified_merge_sort
| This is a helper function, which sorts the x-values along
| with its corresponding y-values via merge sort.
|
| @ref https://www.geeksforgeeks.org/python-program-for-merge-sort/
- - -
** params
| - xv: A vector containing the x values
| - yv: A vector containing the y values
| - l: The left index of the vector
| - r: The right index of the vector
'''
def modified_merge_sort(xv, yv, l, r):
    if l < r:
        # -> Compute the middle index.
        # -> Similar to (l + r) // 2, but it avoids overflow for large l and h.
        m = l + (r - l) // 2

        # Sort the first and second vectors.
        modified_merge_sort(xv, yv, l, m)
        modified_merge_sort(xv, yv, m+1, r)
        merge(xv, yv, l, m, r)

'''
** merge
| This is a helper function, which sorts the x-values along
| with its corresponding y-values via merge sort.
|
| @ref https://www.geeksforgeeks.org/python-program-for-merge-sort/
- - -
** params
| - xv: A vector containing the x values
| - yv: A vector containing the y values
| - l: The left index of the vector
| - m: The middle index
| - r: The right index of the vector
'''
def merge(xv, yv, l, m, r):
    # Compute for the vector sizes.
    n1, n2 = m - l + 1, r - m

    # Create temporary arrays for x and y vectors.
    xvl = [0] * n1
    yvl = [0] * n1

    xvr = [0] * n2
    yvr = [0] * n2

    # Copy data to the temporary arrays.
    for _ in range(0, n1):
        xvl[_], yvl[_] = xv[l + _], yv[l + _]
    for _ in range(0, n2):
        xvr[_], yvr[_] = xv[m + 1 + _], yv[m + 1 + _]

    # Merge the temporary arrays back into xv[l .. r].
    i = j = 0 # Initial indices of the 1st and 2nd subarrays
    k = l # Initial index of the merged subarray

    while i < n1 and j < n2:
        if xvl[i] <= xvr[j]:
            xv[k], yv[k] = xvl[i], yvl[i]
            i += 1
        else:
            xv[k], yv[k] = xvr[j], yvr[j]
            j += 1
        k += 1

    # Copy the remaining elements of xvl and yvl if possible.
    while i < n1:
        xv[k], yv[k] = xvl[i], yvl[i]
        i += 1
        k += 1
    # Copy the remaining elements of xvr and yvr if possible.
    while j < n2:
        xv[k], yv[k] = xvr[j], yvr[j]
        j += 1
        k += 1

'''
** get_row
| This is a helper function, which returns a row based on the condition.
- - -
** params
| - coefficients: A list of coefficients
| - n: Number of intervals
| - a: Adder (Optional)
- - -
** returns
| The vector form of the equation without a1.
'''
def get_row(coefficients, n, a = 0):
    return [coefficients[i - a] if i >= a and i <= a + 2 else coefficients[3] if i == 3 * n else 0 for i in range(3 * n + 1)]

'''
** clean_input
| This is a helper function to clean the data
| from the form.
- - -
** params
| - xv: A vector containing the x values
| - yv: A vector containing the y values
| - x: Value to be evaluated using the appropriate polynomial
- - -
** returns
| All parameters in the correct data type.
'''
def clean_input(xv, yv, x):
    return { 'xv' :  np.array([float(num) for num in xv.split(',')], dtype = float), 'yv' : np.array([float(num) for num in yv.split(',')], dtype = float), 'x' : float(x) }


from collections.abc import Iterator, Generator, Iterable
import unittest

def generator_count_down(num):
    print("Starting")
    while num > 0:
        yield num
        num -= 1

class test_iterator_generator(unittest.TestCase):

    def test_iterator_generator(self):
        ## generator is subclass of iterator, iterator is subclass of iterable
        self.assertTrue(issubclass(Generator, Iterator))
        self.assertFalse(issubclass(Iterator, Generator))
        self.assertTrue(issubclass(Generator, Iterable))

    def test_generator(self):
        ## both are ways to construct generators
        g = generator_count_down(5)
        self.assertTrue(isinstance(g, Generator))
        g2 = (x for x in range(5) if x % 2 == 0)
        self.assertTrue(isinstance(g2, Generator))

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


import sys, time,os, random,copy
import collections
import numpy as np
import pandas as pd

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import ModuleDict
from torch_geometric.loader import DataLoader

from utils import *
from data import *
from model_utils import GVP, GVPConvLayer, LayerNorm

class GVPMSA(object):
    def __init__(self,
            output_dir,
            dataset_names,
            train_dfs_dict,
            val_dfs_dict,
            test_dfs_dict,
            dataset_config,
            pdb_path_prefix = '',
            device='cuda:0',
            node_in_dim = (6, 3),
            node_h_dim = (100,16),
            edge_in_dim = (32, 1),
            edge_h_dim =(32,1),
            lr = 1e-4,
            batch_size=128,
            top_k=15,data_category=False,
             multi_train=False,out_dim=1,
            n_ensembles=3,load_model_path = None,
            esm_msa_linear_hidden=128, num_layers=2,msa_in=True,
             drop_rate=0.1):
        if data_category:
            assert out_dim == 3
        else:
            assert out_dim ==1
        self.batch_size = batch_size
        self.dataset_names = dataset_names
        self.top_k = top_k
        self.data_category = data_category
        self.output_dir = output_dir
        self.device = device
        self.msa_in = msa_in

        self.coords_dict = self.get_coords_dict(pdb_path_prefix,dataset_config)
        self.data_loader_dict = self.get_dataloader(train_dfs_dict = train_dfs_dict,
                                              val_dfs_dict = val_dfs_dict,
                                              test_dfs_dict = test_dfs_dict)

        self.logger = Logger(output_dir)
        if load_model_path:
            model_dict = torch.load(load_model_path,map_location=self.device)
            model = model_dict['model']
            model.load_state_dict(model_dict['model_para'])
            model.multi_train = False
            self.models = [model]

        else:
            self.models = [
            VEPModel(node_in_dim=node_in_dim, node_h_dim=node_h_dim,
                     edge_in_dim=edge_in_dim, edge_h_dim=edge_h_dim,dataset_names=dataset_names,
                     multi_train = multi_train,
                    esm_msa_linear_hidden = esm_msa_linear_hidden,
                 seq_in=True, num_layers=num_layers, drop_rate=drop_rate,
                 out_dim = out_dim,seq_esm_msa_in=msa_in)
            .to(self.device) for _ in range(n_ensembles)]

        weight = torch.tensor([1,100],dtype=torch.float,device=device)
        self.Loss_c = nn.CrossEntropyLoss(weight = weight)
        self.Loss_mse = nn.MSELoss()
        self.batch_size = batch_size
        self.optimizers = [torch.optim.Adam(model.parameters(),lr=lr) for model in self.models]
        self._test_pack = None
    def get_coords_dict(self,pdb_path_prefix,dataset_config):
        coords_dict = {}
        for dataset_ in self.dataset_names:
            pdbfile = os.path.join(pdb_path_prefix,'{}/{}.pdb'.format(dataset_,dataset_))
            coords_binds_pad,seq_bind_pad = get_coords_seq(pdbfile,dataset_config[dataset_],ifbindchain=True,ifbetac=False)
            coords_dict[dataset_] = (coords_binds_pad,seq_bind_pad)
        return coords_dict
    def get_dataloader(self, train_dfs_dict,val_dfs_dict,test_dfs_dict):
        train_loader_dict = {}
        val_loader_dict = {}
        test_loader_dict = {}
        for dataset_name in train_dfs_dict.keys():
            train_dataset = ProteinGraphDataset(train_dfs_dict[dataset_name],self.coords_dict[dataset_name][0],
                 self.coords_dict[dataset_name][1],
                 dataset_name,get_msa_info = self.msa_in,top_k=self.top_k,if_category=self.data_category,device=self.device)
            train_loader = DataLoader(train_dataset, batch_size=self.batch_size, shuffle=True,)
            train_loader_dict[dataset_name] = train_loader

        for dataset_name in val_dfs_dict.keys():
            val_dataset = ProteinGraphDataset(val_dfs_dict[dataset_name],self.coords_dict[dataset_name][0],
                 self.coords_dict[dataset_name][1],
                 dataset_name,get_msa_info = self.msa_in,top_k=self.top_k,if_category=self.data_category,device=self.device)
            val_loader = DataLoader(val_dataset, batch_size=self.batch_size, shuffle=False)
            val_loader_dict[dataset_name] = val_loader

        for dataset_name in test_dfs_dict.keys():
            test_dataset = ProteinGraphDataset(test_dfs_dict[dataset_name],self.coords_dict[dataset_name][0],
                 self.coords_dict[dataset_name][1],
                 dataset_name,get_msa_info = self.msa_in,top_k=self.top_k,if_category=self.data_category,device=self.device)
            test_loader = DataLoader(test_dataset, batch_size=self.batch_size, shuffle=False)
            test_loader_dict[dataset_name] = test_loader
        return {'train':train_loader_dict,'val':val_loader_dict,'test':test_loader_dict}

    def train_onefold(self, fold_idx,epochs=1000,patience=150, save_checkpoint=False, save_prediction=True):
        pred_list_ensemble = 0
        for midx, (model, optimizer) in enumerate(zip(self.models, self.optimizers)):
            stopper = EarlyStopping(patience=patience,higher_better=True)

            for epoch in range(epochs):
                # Training
                losses_all,(pred_list,target_list),spearman_v_train = self.runModel(model,optimizer,mode='train')
                self.logger.write('Epoch{},train total loss: {},  Spearman:{}\n'.format(epoch,
                     losses_all,spearman_v_train))

                # Validation
                losses_all,(pred_list,target_list),spearman_v_val = self.runModel(model,optimizer,mode='val')
                self.logger.write('Epoch{},val total loss: {},  Spearman:{}\n'.format(epoch,
                     losses_all,spearman_v_val))

                if epoch% 20 == 19:
                    self.logger.flush()

                spearman_v_val_mean = sum(spearman_v_val)/len(spearman_v_val)
                is_best = stopper.update(spearman_v_val_mean)

                if is_best:
                    best_model_para = model.state_dict()
                    best_epoch = epoch
                    # test
                    losses_all,(pred_list,target_list),spearman_v_test = self.runModel(model,optimizer,mode='test')
                    self.logger.write('Epoch{},test total loss: {},  Spearman is {},\n'.format(epoch,
                         losses_all,spearman_v_test))
                    best_pred_target_test = (pred_list,target_list)
                    best_val_spearman = spearman_v_val_mean
                    best_test_spearman = spearman_v_test

                if stopper.early_stop or epoch == epochs-1:
                    self.logger.write('Early stop at epoch {}\n'.format(epoch))
                    self.logger.write(
                        'ensemble idx {}, best epoch {}, best validation spearman is {},best test spearman is {},\n\n'.format(
                            midx,best_epoch, best_val_spearman,best_test_spearman))
                    break

            pred_list_ensemble += np.array(best_pred_target_test[0])
            if save_checkpoint:
                best_stat = {'data_fold':fold_idx,'model_para':best_model_para,
                'model':model,
                'epoch':best_epoch,'test_pred_target':best_pred_target_test,
                'best_test_metrics':best_test_spearman,
                'best_val_metrics':best_val_spearman,
                }
                torch.save(best_stat, os.path.join(self.output_dir,'model_fold{}_ensemble{}.pt'.format(fold_idx,midx))
                )
        if save_prediction:
            dataframe = pd.DataFrame({'pred':pred_list_ensemble,'target':best_pred_target_test[1]})
            dataframe.to_csv(os.path.join(self.output_dir,'pred_fold{}.csv'.format(fold_idx)))
        ensemble_metrics_spearman = spearman(pred_list_ensemble,best_pred_target_test[1])
        ensemble_metrics_ndcg = ndcg(pred_list_ensemble,best_pred_target_test[1])
        self.logger.write('ensemble {} models, fold {}, spearman is {}, ndcg is {}\n'.format(
                   len(self.models),fold_idx,ensemble_metrics_spearman,ensemble_metrics_ndcg))
        return dataframe

    def runModel(self, model, optimizer,mode='test'):
        device = self.device

        losses_all = 0
        if mode == 'train':
            data_loader_dict = self.data_loader_dict[mode]
            datasets_list = data_loader_dict.keys()

            spearman_list = []
            count = 0
            target_list = []
            pred_list = []
            data_loader_dict_iter = {}
            for dataset in datasets_list:
                data_loader_dict_iter[dataset] = iter(data_loader_dict[dataset])
            while True:
                dataset = random.sample(datasets_list,1)[0]
                try:
                    (graph,wt_graph) = next(data_loader_dict_iter[dataset])
                except StopIteration:
                    break

                count +=1

                model.train()
                out = model(graph.to(device),wt_graph.to(device))
                target = graph.target.float().to(device)

                if self.data_category:
                    out_classfy,out_reg = out
                    target_category = torch.tensor(graph.target_category,dtype=torch.long,device=device)
                    loss_classfy = self.Loss_c(out_classfy,target_category)

                else:
                    out_reg = out
                    loss_classfy = 0
                loss_reg = self.Loss_mse(out_reg,target)
                loss = loss_classfy + loss_reg
                loss.backward()

                losses_all += loss.item()
                optimizer.step()
                optimizer.zero_grad()
                target_list.extend(target.cpu().detach().numpy())
                pred_list.extend(out_reg.cpu().detach().numpy())
            pred_list = np.vstack(pred_list)[:,0]
            target_list = np.vstack(target_list)[:,0]
            spearman_v = spearman(pred_list,target_list)

            return losses_all/count,(pred_list,target_list),spearman_v
        elif mode == 'test' or 'val':

            data_loader_dict = self.data_loader_dict[mode]
            datasets_list = data_loader_dict.keys()

            count = 0
            losses_all = 0
            spearman_list = []
            outall_reg_all = []
            target_all_all = []
            for dataset in datasets_list:
                target_list = []
                pred_list = []

                for (graph,wt_graph) in data_loader_dict[dataset]:
                    with torch.no_grad():
                        model.eval()
                        count +=1
                        out = model(graph.to(device),wt_graph.to(device))
                        target = graph.target.float().to(device)
                        if self.data_category:
                            out_classfy,out_reg = out
                            target_category = torch.tensor(graph.target_category,dtype=torch.long,device=device)
                            loss_classfy = self.Loss_c(out_classfy,target_category)

                        else:
                            out_reg = out
                            loss_classfy = 0
                        target_list.extend(target.cpu().detach().numpy())
                        pred_list.extend(out_reg.cpu().detach().numpy())
                        loss_reg = self.Loss_mse(out_reg,target)
                        loss = loss_classfy + loss_reg

                        losses_all += loss.item()
                pred_list = np.vstack(pred_list)
                target_list = np.vstack(target_list)
                spearman_list.append(spearman(pred_list,target_list))

                outall_reg_all.append(pred_list)
                target_all_all.append(target_list)

            return_out = (losses_all/count),(np.vstack(outall_reg_all)[:,0],np.vstack(target_all_all)[:,0]),spearman_list
        return return_out

def clones(module, N):
    "Produce N identical layers."
    return nn.ModuleList([copy.deepcopy(module) for _ in range(N)])

class VEPModel(nn.Module):

    def __init__(self, node_in_dim, node_h_dim,
                 edge_in_dim, edge_h_dim,dataset_names,
                 multi_train = False,
                 esm_msa_linear_hidden = 128,
                 seq_in=True, num_layers=2, drop_rate=0.1,seq_esm_msa_in=True,
                 out_dim = 3):

        super(VEPModel, self).__init__()
        self.node_h_dim = node_h_dim
        self.seq_esm_msa_in = seq_esm_msa_in
        self.out_dim = out_dim
        self.esm_msa_linear = nn.Linear(768,esm_msa_linear_hidden)
        self.multi_train = multi_train

        if seq_esm_msa_in:
            node_in_dim = (node_in_dim[0] + esm_msa_linear_hidden, node_in_dim[1])

        if seq_in:
            self.W_s = nn.Embedding(21, 20)
            node_in_dim = (node_in_dim[0] + 20*3, node_in_dim[1])

        self.W_v = nn.Sequential(
            LayerNorm(node_in_dim),
            GVP(node_in_dim, node_h_dim, activations=(None, None))
        )
        self.W_e = nn.Sequential(
            LayerNorm(edge_in_dim),
            GVP(edge_in_dim, edge_h_dim, activations=(None, None))
        )

        self.layers = nn.ModuleList(
                GVPConvLayer(node_h_dim, edge_h_dim, drop_rate=drop_rate)
            for _ in range(num_layers))

        ns, _ = node_h_dim
        self.W_out = nn.Sequential(
            LayerNorm(node_h_dim),
            GVP(node_h_dim, (ns, 0)))

        self.dense = nn.Sequential(
            nn.Linear(ns, ns//2), nn.ReLU(inplace=True),
            nn.Dropout(p=drop_rate),
            nn.Linear(ns//2, ns*2)
        )

        self.readout = nn.Sequential(
            AggregateLayer(d_model = ns*2),
            GlobalPredictor(d_model = ns*2,
                                d_h=128, d_out=out_dim)
        )
        if multi_train:
            readout_list = [copy.deepcopy(self.readout) for i in range(len(dataset_names))]
            self.readout_dict = ModuleDict(dict(zip(dataset_names,readout_list)))

    def forward(self,graph,wt_graph):
        out = self.forward1(graph,wt_graph)
        out = self.dense(out)
        if self.multi_train:
            out = self.readout_dict[graph.dataset_name[0]](out)
        else:
            out = self.readout(out)

        if self.out_dim ==3:
            return out[:,:2],out[:,2]
        elif self.out_dim ==1:
            return out[:,0]
        elif self.out_dim ==4:
            return out[:,:3],out[:,3]
        else:
            print('out dim not in [0,3], not implement')

    def forward1(self, graph,graph_wt):
        '''
        :param h_V: tuple (s, V) of node embeddings
        :param edge_index: `torch.Tensor` of shape [2, num_edges]
        :param h_E: tuple (s, V) of edge embeddings
        :param seq: if not `None`, int `torch.Tensor` of shape [num_nodes]
                    to be embedded and appended to `h_V`
        '''
        batch_num = graph_wt.batch[-1]+1
        h_V = (graph_wt.node_s,graph_wt.node_v)
        h_E = (graph_wt.edge_s,graph_wt.edge_v)
        seq = graph.seq
        seq_wt = graph_wt.seq
        edge_index = graph_wt.edge_index
        if seq is not None:
            seq = self.W_s(seq.long())
            seq_wt = self.W_s(seq_wt.long())
            h_V = (torch.cat([h_V[0], seq,seq_wt,seq-seq_wt], dim=-1), h_V[1])

        if self.seq_esm_msa_in: #[h_V[0].shape = (bs*seqlen,dim)
            h_V = (torch.cat([h_V[0], self.esm_msa_linear(graph_wt.msa_rep[0])], dim=-1), h_V[1])

        h_V = self.W_v(h_V)
        h_E = self.W_e(h_E)
        for layerid,layer in enumerate(self.layers):
            h_V = layer(h_V, edge_index, h_E)

        out = self.W_out(h_V)
        hidden_dim = out.shape[-1]
        out = out.reshape(batch_num,-1,hidden_dim)

        return out

class AggregateLayer(nn.Module):
    def __init__(self, d_model=None, dropout=0.1):
        super(AggregateLayer, self).__init__()
        self.attn = nn.Sequential(collections.OrderedDict([
            ('layernorm', nn.LayerNorm(d_model)),
            ('fc', nn.Linear(d_model, 1, bias=False)),
            ('dropout', nn.Dropout(dropout)),
            ('softmax', nn.Softmax(dim=1))
        ]))

    def forward(self, context):

        weight = self.attn(context)
        output = torch.bmm(context.transpose(-1, -2), weight)
        output = output.squeeze(-1)
        return output

class GlobalPredictor(nn.Module):
    def __init__(self, d_model=None, d_h=None, d_out=None, dropout=0.5):
        super(GlobalPredictor, self).__init__()
        self.batchnorm = nn.BatchNorm1d(d_model)
        self.predict_layer = nn.Sequential(collections.OrderedDict([
            # ('batchnorm', nn.BatchNorm1d(d_model)),
            ('fc1', nn.Linear(d_model, d_h)),
            ('tanh', nn.Tanh()),
            ('dropout', nn.Dropout(dropout)),
            ('fc2', nn.Linear(d_h, d_out))
        ]))

    def forward(self, x):
        if x.shape[0] !=1:
            x = self.batchnorm(x)
        x = self.predict_layer(x)
        return x


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

import random
import sys
import time

import mido

onames = mido.get_output_names()
fluid = list(filter(lambda x: 'FP-30' in x, onames))

if len(fluid) == 0:
    print("Fluid synth not started")
    sys.exit(1)

fport = mido.open_output(fluid[0])
print(f'Connected to {fluid[0]}')

int_names = [
        'Прима',            # 0
        'малка Секунда',    # 1
        'голяма Секунда',   # 2
        'малка Терца',      # 3
        'голяма Терца',     # 4
        'Кварта',           # 5
        'увеличена Кварта', # 6
        'Квинта',           # 7
        'малка Секста',     # 8
        'голяма Секста',    # 9
        'малка Септима',    # 10
        'голяма Септима',   # 11
        'Октава',     # 12
        ]

def play_notes(notes, delay=0, duration=1.0, arp=False):
    for note in notes:
        msg=mido.Message('note_on', note=note)
        fport.send(msg)
        time.sleep(delay)
        if arp:
            msg=mido.Message('note_off', note=note)
            fport.send(msg)
    if not arp:
        time.sleep(duration)
        for note in notes:
            msg=mido.Message('note_off', note=note)
            fport.send(msg)

def play_random_intervals(n=10, choice=list(range(13)), base=None):
    for _ in range(n):
        if base is None:
            base_note = 48 + random.randrange(25)  # C3 - C5
        else:
            case_note = base
        interval = random.choice(choice)
        resp = ''
        while resp == '':
            play_notes((base_note, base_note+interval), delay=1.0, arp=True)
            resp = input(f"Press Enter for repeat, or {choice}: ")
        try:
            resp = int(resp)
            if resp == interval:
                print(f"Correct: {int_names[interval]}")
            else:
                print(f"                 ******* Wrong: {int_names[interval]}, not {int_names[resp]}")
        except ValueError:
            break
        time.sleep(1)

def print_intervals():
    for i, n in enumerate(int_names):
        print(i, n)

if __name__ == '__main__':
    print_intervals()
    play_random_intervals(100,[3,4])


import os
import numpy as np
import pandas as pd
from ics import Calendar, Event
import pytz
from datetime import date, datetime, timedelta
from yaml import safe_load

with open('config.yml',encoding='utf8') as _yaml_file:
    _config = safe_load(_yaml_file)

semester_date_str = _config['semester_date_str']
kouqiangxi_teacher_list = _config['kouqiangxi_teacher_list']
timezone_shanghai = pytz.timezone('Asia/Shanghai')

def generate_ics_file_wrapper(teacher_name):
    xl_path = os.path.join('..','output',semester_date_str,f'{teacher_name}负责课程.xlsx')
    ics_out_path = os.path.join('..','output',semester_date_str,f'{teacher_name}教学日历.ics')
    xlFile = pd.ExcelFile(xl_path)

    dfs = xlFile.sheet_names
    if dfs[0] == 'Sheet1':
        # data_cleaning输出的空白excel文件（当某教师无授课任务的时候会出现）
        print(f'{teacher_name}在{semester_date_str}学期无教学任务，请查证')
    else:
        dfs = [xlFile.parse(s) for s in dfs]
        df = pd.concat(dfs)
        c = Calendar()
        df.apply(lambda row: c.events.add(event_gen(row)), axis=1)

        with open(ics_out_path, 'w', encoding='utf-8') as f:
            f.writelines(c)

def event_gen(para_row):
    para_row.fillna('',inplace=True)
    begin_time = timezone_shanghai.localize(para_row['开始时间'])
    end_time = timezone_shanghai.localize(para_row['结束时间'])
    e = Event(
        name = para_row['课程名称'],
        begin=begin_time,
        end=end_time,
        attendees=para_row['负责人'].split(','),
        location=para_row['地点'],
        description=para_row['备注'],
    )
    return e

for teacher in kouqiangxi_teacher_list:
    generate_ics_file_wrapper(teacher)


# Import of the libraries and the databases we need to analyze
import sys
sys.path.insert(0, '/home/apprenant/Documents/Brief-Emotion-Analysis-Text/')
import pandas as pd
from src.functions import separator

emotion_final = pd.read_csv("/home/apprenant/Documents/Brief-Emotion-Analysis-Text/data/01_raw/Emotion_final.csv")
text_emotion = pd.read_csv("/home/apprenant/Documents/Brief-Emotion-Analysis-Text/data/01_raw/text_emotion.csv")

# 1. Deleting of the useless columns

text_emotion = text_emotion.drop('author', axis=1)
print(text_emotion)
separator()

# 2. Handling of the duplicated rows

print(emotion_final.duplicated().value_counts())
emotion_final = emotion_final.drop_duplicates()
separator()
print(text_emotion.duplicated().value_counts())
separator()

# 3. Handling of the missing values

print(emotion_final.isna().value_counts())
separator()
print(text_emotion.isna().value_counts())
separator()

# 4. Checking of the value types

print(emotion_final.dtypes)
separator()
print(text_emotion.dtypes)
separator()

# 5. Change name of columns

emotion_final = emotion_final.rename(columns={'Text': 'text', 'Emotion': 'label'})
text_emotion = text_emotion.rename(columns={'sentiment':'label', 'content': 'text'})

# 5. Creation of the cleaned data

emotion_final.to_csv('/home/apprenant/Documents/Brief-Emotion-Analysis-Text/data/02_cleaned/cleaned_emotion_final.csv', index=False)
text_emotion.to_csv('/home/apprenant/Documents/Brief-Emotion-Analysis-Text/data/02_cleaned/cleaned_text_emotion.csv', index=False)


from django.db.models.query import QuerySet
from django.shortcuts import render
from rest_framework import generics, serializers
from Base.models import Post, Response
from Base.serializers import GetQuestionsSerializer
from Base.models import Question
from Base.models import Category
from Base.serializers import GetPostsSerializer
from Base.models import SubscribedPosts
from Base.serializers import SubscribedPostSerializer
from Base.serializers import ResponseSerializer
from Base.serializers import FinalResponseSerializer
from Base.models import FinalResponse

class QuestionList(generics.ListCreateAPIView):
   # queryset=Question.objects.all()
    # ssociatedPost
    serializer_class = GetQuestionsSerializer

    def get_queryset(self):
        PostId = self.kwargs['AssociatedPost']
        return Question.objects.filter(AssociatedPost=PostId)

class PostByCategory(generics.ListAPIView):

    serializer_class = GetPostsSerializer

    def get_queryset(self):
        CategoryId = self.kwargs['Category']
        return Post.objects.filter(category=CategoryId,isPublished=False)

class SinglePost(generics.RetrieveUpdateAPIView):
    queryset = Post.objects.all()
    serializer_class = GetPostsSerializer

class AllPosts(generics.ListAPIView):
    queryset = Post.objects.all()
    serializer_class = GetPostsSerializer

class PostListByUser(generics.ListAPIView):
    serializer_class = GetPostsSerializer

    def get_queryset(self):

        userId = self.request.user.id
        # print(userId)
       # print('BUN', userId)
        return Post.objects.filter(author=userId)

class SubscribedPostsByUser(generics.ListAPIView):
    serializer_class = SubscribedPostSerializer

    def get_queryset(self):

        userId = self.request.user.id
        # print(userId)
        #print('BUN', userId)
        return SubscribedPosts.objects.filter(User=userId)

class AddPost(generics.CreateAPIView):
    serializer_class = GetPostsSerializer

class AddSubscription(generics.CreateAPIView):
    serializer_class = SubscribedPostSerializer

class getSubscriptions(generics.ListAPIView):
    serializer_class = SubscribedPostSerializer

    def get_queryset(self):
        PostId = self.kwargs['PostId']
        return SubscribedPosts.objects.filter(AssociatedPost=PostId)

class getAproovedSubscriptions(generics.ListAPIView):
    serializer_class = SubscribedPostSerializer

    def get_queryset(self):
        #PostId = self.kwargs['PostId']
        UserId=self.request.user.id
        return SubscribedPosts.objects.filter( isAprooved=True,User=UserId)

class getSingleSubscription(generics.RetrieveUpdateAPIView):
    queryset = SubscribedPosts.objects.all()

    serializer_class = SubscribedPostSerializer

class PostResponses(generics.ListCreateAPIView):
    queryset = Response.objects.all()
    serializer_class = ResponseSerializer

class PostFinalResponse(generics.ListCreateAPIView):
    queryset = FinalResponse.objects.all()
    serializer_class = FinalResponseSerializer

class FinalResponseByPost(generics.ListCreateAPIView):
    #queryset = FinalResponse.objects.all()
    serializer_class = FinalResponseSerializer
    def get_queryset(self):
        PostId=self.kwargs['AssociatedPost']
        return FinalResponse.objects.filter(AssociatedPost=PostId)

class MyFinalAnswers(generics.ListAPIView):
    serializer_class = FinalResponseSerializer

    def get_queryset(self):
        return FinalResponse.objects.filter(User=self.request.user.id)

class AnswerByQuestion(generics.ListAPIView):
    serializer_class = ResponseSerializer

    def get_queryset(self):
        QuestionId = self.kwargs['questionId']
        return Response.objects.filter(question=QuestionId)


from flask import Flask, render_template, request, session
import datetime
from flask_session import Session

# set FLASK_APP=application.py
app = Flask(__name__)

@app.route("/")
def index():
    headline = "Nazgulz rock!"
    return render_template("index.html", headline=headline)

@app.route("/hello", methods=["GET", "POST"])
def hello():
    if request.method == "GET":
        return "You should submit the post to get here"
    else:
        name = request.form.get("name")
        return render_template("hello.html", jname=name)

@app.route("/more")
def more():
    return render_template("more.html")

notes_list = []

@app.route("/notes", methods=["GET", "POST"])
def notes():
    if request.method == "POST":
        note = request.form.get("note")
        notes_list.append(note)
    return render_template("notes.html", notes=notes_list)

app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)

@app.route("/session_notes_", methods=["GET", "POST"])
def session_notes_():
    if session.get("notes") is None:
        session["notes"] = []
    if request.method == "POST":
        note = request.form.get("note")
        session["notes"].append(note)
    return render_template("session_notes_.html", notes=session["notes"])

@app.route("/names")
def forloop():
    names = ["Alice", "Józsibácsi", "Nazgul"]
    return render_template("forloop.html", names=names)

@app.route("/new")
def n_y():
    now = datetime.datetime.now()
    new_year = now.month == 1 and now.day == 1
    #new_year = True
    return render_template("newyear.html", new_year=new_year, date_=str(now))

@app.route("/KZ")
def zottya():
    return "
Hello, MelaKZ!!!
"

@app.route("/<string:name>")
def valami(shhhh):
    return f"Hello, {shhhh}!"


import pytest
import allure
import logging
from util.assert_util import assert_keyword
from util.request_util import api_request
from util.global_var import *
from util.excel_util import excel_util

register_test_data = excel_util.get_sheet_data("注册")
login_test_data = excel_util.get_sheet_data("登录")

@allure.feature("登录模块")
@pytest.mark.dependency(name="TestLoginModule")
class TestLoginModule:

    @allure.story("注册功能")
    @allure.title('用户注册')  # 指定测试用例标题，默认是函数名
    @allure.description('通过接口进行用户注册')  # 添加测试用例描述
    @allure.severity(allure.severity_level.BLOCKER)  # 阻塞级别
    @pytest.mark.run(order=1)
    @pytest.mark.parametrize('case_data', register_test_data)
    def test_register(self, case_data):
        with allure.step("读取请求数据，调用接口"):
            logging.info("接口用例数据：%s" % case_data)
            response = api_request(case_data[API_IP], case_data[API_URI], case_data[REQUEST_METHOD],
                                   case_data[API_REQUEST_DATA], case_data[RESPONSE_EXTRACT_VAR],
                                   case_data[REQUEST_HEADER], case_data[REQUEST_COOKIE])
        with allure.step("获取响应数据，进行断言"):
            assert_keyword(response, case_data[RESPONSE_ASSERT_KEYWORD])

    @allure.story("登录功能")
    @allure.title('用户登录')  # 指定测试用例标题，默认是函数名
    @allure.description('通过接口进行用户登录')  # 添加测试用例描述
    @allure.severity(allure.severity_level.BLOCKER)  # 阻塞级别
    @pytest.mark.run(order=2)
    @pytest.mark.parametrize('case_data', login_test_data)
    def test_login(self, case_data):
        with allure.step("读取请求数据，调用接口"):
            logging.info("接口用例数据：%s" % case_data)
            response = api_request(case_data[API_IP], case_data[API_URI], case_data[REQUEST_METHOD],
                                   case_data[API_REQUEST_DATA], case_data[RESPONSE_EXTRACT_VAR],
                                   case_data[REQUEST_HEADER], case_data[REQUEST_COOKIE])
        with allure.step("获取响应数据，进行断言"):
            assert_keyword(response, case_data[RESPONSE_ASSERT_KEYWORD])

if __name__ == "__main__":
    test_dir = os.path.dirname(__file__)
    pytest.main(['-s', '-q', test_dir, '--alluredir', '../test_result/', "--clean-alluredir"])
    os.system('allure generate ../test_result/ -o ../test_report/ --clean')
    os.system('allure open -h 127.0.0.1 -p 8881 ../test_report/')


# -*- coding: utf-8 -*-
"""
@author: Zheng Zhihan
@file: ASTNode.py
@time: 2021/06/03
@update: 2021/07/05
@description: 对树进行分析
"""

from SymbolTable import SymbolTable

proc_var = []
l4 = []
var_l4_dict = {}
err = []

func_list = {}  # type, name, l4
func_list['f0'] = {'type': None, 'table': None, 'sys_data':None, 'func_name': 'START'}

asm = []
asm_sys_data = []
asm_usr_data = []
asm_jp_flag = []

class ASTNode(object):
    pass

class ASTNode:
    def __init__(self, ast_type: str, father: ASTNode = None):
        self.atype = ast_type
        self.father = father
        self.child = []
        self.table = None
        self.current_func = None

        if ast_type == 'compoundStatement':
            self.table = SymbolTable()

        if ast_type == 'functionDefinition':

            self.current_func = f'f{len(func_list)}'
            func_list[self.current_func] = {}
        elif father is None:
            self.current_func = 'f1'
        else:
            self.current_func = father.current_func

    def getTableNode(self):
        if self.father is None:
            return None
        node = self.father
        while node.atype != 'compoundStatement':
            node = node.father
        return node

    def getTableDict(self):
        node = self.getTableNode()
        table = {}
        table.update(node.table.table)
        while node.getTableNode() is not None:
            node = node.getTableNode()
            table.update(node.table.table)
        return table

def translate(node: ASTNode):
    if node.atype == 'typeSpecifier':
        return node.child[0][0]
    elif node.atype == 'initDeclarator':
        var_1 = translate(node.child[0])
        var_2 = translate(node.child[2])
        l4.append(['=', var_2, None, var_1])
        # print(f'(=, {var_2}, , {var_1})')

        if type(var_1) is str and var_1[-1] == ']':
            index = var_1.split('[')[-1][:-1]
            asm.append(['MOV', 'EDI', f'{index}'])
            var_1 = var_1.split('[')[0] + '[EDI*4]'

        if type(var_2) is str and var_2[-1] == ']':
            index = var_2.split('[')[-1][:-1]
            asm.append(['MOV', 'ESI', f'{index}'])
            var_2 = var_2.split('[')[0] + '[ESI*4]'

        if type(var_2) is int:
            asm.append(['MOV', f'{var_1}', var_2])
        else:
            asm.append(['MOV', 'EAX', f'{var_2}'])
            asm.append(['MOV', f'{var_1}', 'EAX'])

        return var_1
    elif node.atype == 'declarator':
        sign = node.child[-1]
        if sign[0] == 'Identifier':
            return sign[-1]
        elif sign[0] == 'RBRACK':   # 数组
            l_name = translate(node.child[0])
            l_length = translate(node.child[2])
            return f'{l_name}[{l_length}]'

    elif node.atype == 'initDeclaratorList':
        vl = [translate(node.child[0])]
        vl_ex = translate(node.child[-1])
        if type(vl_ex) is str:
            vl.append(vl_ex)
        else:
            vl.extend(vl_ex)
        return vl
    elif node.atype == 'declaration':  # 声明
        # 变量定义
        child_0 = translate(node.child[0])  # typeSpecifier type

        var = []
        child_1 = node.child[1]
        if node.child[1].atype == 'declarator':  # 单个变量定义
            var.append(translate(child_1))
        elif node.child[1].atype == 'initDeclaratorList':
            var.extend(translate(child_1))
        elif node.child[1].atype == 'initDeclarator':
            var.append(translate(child_1))

        for i in var:
            # 检查是否重复定义
            """if i in node.getTableDict():
                print(f'ERR: {i} Defined Again')
                err.append(f'ERR: {i} Defined Again')
            else:
                func_list[node.current_func]['table'].add(child_0, i)
                node.getTableNode().table.add(child_0, i)"""
            func_list[node.current_func]['table'].add(child_0, i)
            node.getTableNode().table.add(child_0, i)

    elif node.atype == 'primaryExpression':
        if len(node.child) == 1:
            if node.child[0][0] == 'Identifier':
                return node.child[0][-1]
            elif node.child[0][0] == 'IntegerConstant':
                return int(node.child[0][-1])
            elif node.child[0][0] == 'String':
                asm_usr_data.append((f'u{len(asm_usr_data)}', 'String', node.child[0][-1]))
                return f'offset u{len(asm_usr_data)-1}'

        else:
            return translate(node.child[1])  # 括号
    elif node.atype == 'unaryExpression':
        if type(node.child[-1]) is list:
            if node.child[-1][-1] in ['++', '--']:
                var = translate(node.child[0])
                op = node.child[-1][-1]
                if op == '++':
                    asm.append(['INC', f'{var}'])
                elif op == '--':
                    asm.append(['DEC', f'{var}'])

            elif node.child[-1][-1] == ']':     # 数组
                l_name = translate(node.child[0])

                current_func = node.current_func
                sys_data = func_list[current_func]['sys_data']
                var = f'v{len(sys_data)}'
                func_list[current_func]['sys_data'].append(var)
                l_index = translate(node.child[2])
                return f'{l_name}[{l_index}]'

            elif node.child[-1][-1] == ')':  # 函数返回值

                operator = '='
                current_func = node.current_func
                sys_data = func_list[current_func]['sys_data']
                var = f'v{len(sys_data)}'
                func_list[current_func]['sys_data'].append(var)
                func = translate(node.child[0])
                asm.append(['PUSH', 'EAX'])
                if type(node.child[2]) is not list:
                    para = translate(node.child[2])

                    if type(para) is str and para[-1] == ']':
                        index = para.split('[')[-1][:-1]
                        asm.append(['MOV', 'EDI', f'{index}'])
                        para = para.split('[')[0] + '[EDI*4]'

                    asm.append(['MOV', 'EAX', f'{para}'])
                asm.append(['CALL', f'{func}'])
                asm.append(['MOV', f'{var}', 'EAX'])
                asm.append(['POP', 'EAX'])
                l4.append([operator, var, None, func])
        else:
            operator = node.child[1][-1]
            var = translate(node.child[0])
            l4.append([operator, var, None, var])
        # print(f'({operator}, {var}, , {var})')
        return var

    elif node.atype in ['additiveExpression', 'multiplicativeExpression']:
        var_1 = translate(node.child[0])
        """if var_1 not in node.getTableDict() and type(var_1) is str and var_1[0] != '%':
            print(f'ERR: {var_1} is Not Defined')
            err.append(f'ERR: {var_1} is Not Defined')"""

        operator = node.child[1][-1]

        var_2 = translate(node.child[2])
        """if var_2 not in node.getTableDict() and type(var_2) is str and var_2[0] != '%':
            print(f'ERR: {var_2} is Not Defined')
            err.append(f'ERR: {var_2} is Not Defined')"""

        var_base = f'%{len(proc_var) + 1}'
        proc_var.append(var_base)
        l4.append([operator, var_1, var_2, var_base])
        var_l4_dict[var_base] = len(l4) - 1
        # print(f'({operator}, {var_1}, {var_2}, {var_base})')
        current_func= node.current_func
        sys_data = func_list[current_func]['sys_data']
        var = f'v{len(sys_data)}'
        func_list[current_func]['sys_data'].append(var)

        if type(var_1) is str and var_1[-1] == ']':
            index = var_1.split('[')[-1][:-1]
            asm.append(['MOV', 'EDI', f'{index}'])
            var_1 = var_1.split('[')[0] + '[EDI*4]'

        asm.append(['MOV', 'EAX', f'{var_1}'])
        if operator == '+':
            asm.append(['ADD', 'EAX', f'{var_2}'])
        elif operator == '-':
            asm.append(['SUB', 'EAX', f'{var_2}'])
        elif operator == '*':
            asm.append(['MUL', f'{var_2}'])
        elif operator == '/':
            asm.append(['DIV', f'{var_2}'])
        elif operator == '%':
            asm.append(['DIV', f'{var_2}'])
            asm.append(['MOV', 'EAX', 'EDX'])

        asm.append(['MOV', f'{var}', 'EAX'])

        return var
    elif node.atype == 'assignmentExpression':
        var_1 = translate(node.child[0])
        # 检查是否未定义
        """if var_1 not in node.getTableDict() and type(var_1) is str and var_1[0] != '%':
            print(f'ERR: {var_1} is Not Defined')
            err.append(f'ERR: {var_1} is Not Defined')"""

        operator = node.child[1].child[0][-1]
        var_2 = translate(node.child[2])
        # 检查是否未定义
        """if var_2 not in node.getTableDict() and type(var_2) is str and var_2[0] != '%':
            print(f'ERR: {var_2} is Not Defined')
            err.append(f'ERR: {var_2} is Not Defined')"""

        l4.append([operator, var_2, None, var_1])

        if operator == '=':
            if type(var_1) is str and var_1[-1] == ']':
                index = var_1.split('[')[-1][:-1]
                asm.append(['MOV', 'EDI', f'{index}'])
                var_1 = var_1.split('[')[0] + '[EDI*4]'
            if type(var_2) is str and var_2[-1] == ']':
                index = var_2.split('[')[-1][:-1]
                asm.append(['MOV', 'ESI', f'{index}'])
                var_2 = var_2.split('[')[0] + '[ESI*4]'

            if type(var_2) is int:
                asm.append(['MOV', f'{var_1}', var_2])
            else:
                asm.append(['MOV', 'ECX', f'{var_2}'])

                asm.append(['MOV', f'{var_1}', 'ECX'])

        # print(f'({operator}, {var_2}, , {var_1})')
        return var_1
    elif node.atype == 'relationalExpression':
        var_1 = translate(node.child[0])

        if type(var_1) is str and var_1[-1] == ']':
            index = var_1.split('[')[-1][:-1]
            asm.append(['MOV', 'EDI', f'{index}'])
            var_1 = var_1.split('[')[0] + '[EDI*4]'

        asm.append(['MOV', 'EBX', f'{var_1}'])
        var_2 = translate(node.child[2])
        operator = node.child[1][-1]

        if type(var_2) is str and var_2[-1] == ']':
            index = var_2.split('[')[-1][:-1]
            asm.append(['MOV', 'ESI', f'{index}'])
            var_2 = var_2.split('[')[0] + '[ESI*4]'

        asm.append(['CMP', 'EBX', f'{var_2}'])
        jcc = 'JMP'
        if operator == '<':
            jcc = 'JNL'
        elif operator == '>':
            jcc = 'JNG'
        elif operator == '<=':
            jcc = 'JNLE'
        elif operator == '>=':
            jcc = 'JNGE'
        elif operator == '==':
            jcc = 'JNZ'
        elif operator == '!=':
            jcc = 'JZ'

        asm.append([f'{jcc}', len(asm)+1])

        var_base = f'%{len(proc_var) + 1}'
        proc_var.append(var_base)
        l4.append([operator, var_1, var_2, var_base])
        var_l4_dict[var_base] = len(l4) - 1
        return var_base

    elif node.atype == 'jumpStatement':
        jump_type = node.child[0][0]
        loop_node = node.father
        if jump_type == 'BREAK':
            asm.append(['JMP', 'BREAK'])
            if loop_node.atype == 'compoundStatement':  # 加了大括号的循环
                loop_node = loop_node.father
            """if loop_node.atype != 'loopStatement':
                print(f'ERR: break Not in Loop')
                err.append(f'ERR: break Not in Loop')"""
        elif jump_type == 'RETURN':
            rt_var = translate(node.child[1])
            asm.append(['MOV', 'EAX', f'{rt_var}'])

    elif node.atype == 'loopStatement':
        loop_type = node.child[0][0]

        if loop_type == 'FOR':
            i = 2
            c = None
            s = None
            e = None
            back_c = None
            back_s = None
            back_e = None
            if type(node.child[i]) is not list:
                # 第一个expression
                translate(node.child[i])

                # back_c
                asm.append(['JMP', 'c'])
                back_c = len(asm) - 1

                i += 1
            i += 1  # ;
            jump_no = 0

            # 这里设置回调点s
            s = f'J{len(asm_jp_flag)}'
            asm_jp_flag.append(s)
            asm.append([f'{s}:'])

            if type(node.child[-3]) is not list:
                # 第三个expression
                translate(node.child[-3])

            # 这里设置回调点c
            c = f'J{len(asm_jp_flag)}'
            asm_jp_flag.append(c)
            asm.append([f'{c}:'])

            if type(node.child[i]) is not list:
                # 第二个expression
                rt1_var = translate(node.child[i])
                jump_no = var_l4_dict[rt1_var]
                i += 1
                # back_e
                back_e = len(asm)-1
            else:
                l4.append(['pass', None, None, None])
                jump_no = len(l4) - 1

            # 进入循环

            translate(node.child[-1])

            # back_s
            asm.append(['JMP', 's'])
            back_s = len(asm) - 1

            # 这里设置回调点e
            e = f'J{len(asm_jp_flag)}'
            asm_jp_flag.append(e)
            asm.append([f'{e}:'])

            if back_s is not None:
                asm[back_s][-1] = s
            if back_c is not None:
                asm[back_c][-1] = c
            if back_e is not None:
                asm[back_e][-1] = e

            for i in range(len(asm)):
                if asm[i][-1] == 'BREAK':
                    asm[i][-1] = e

    elif node.atype == 'selectionStatement':
        select_type = node.child[0][0]

        if select_type == 'IF':
            translate(node.child[2])

            jump_not_index = len(asm) - 1

            l4.append(['jt', None, None, len(l4) + 2])
            l4.append(['jf', None, None, 0])
            jf_no = len(l4) - 1

            translate(node.child[4])
            jend = f'J{len(asm_jp_flag)}'
            asm_jp_flag.append(jend)
            asm.append(['JMP', f'{jend}'])

            jf = f'J{len(asm_jp_flag)}'
            asm_jp_flag.append(jf)
            asm.append([f'{jf}:'])
            asm[jump_not_index][-1] = jf

            l4.append(['j', None, None, 0])
            j_no = len(l4) - 1
            l4[jf_no][-1] = len(l4)
            if len(node.child) == 7:
                translate(node.child[6])
            l4[j_no][-1] = len(l4)
            asm.append([f'{jend}:'])

    elif node.atype == 'programList':
        for i in node.child:
            translate(i)

    elif node.atype == 'functionDefinition':
        f = {'type': translate(node.child[0])}
        fname = translate(node.child[1].child[0])
        asm.append([f'{fname}', 'PROC'])

        f['table'] = SymbolTable()
        f['sys_data'] = []
        f['func_name'] = fname
        para = node.child[1].child[2]
        if type(para) is not list:
            # 有参数传递
            ptype = translate(para.child[0])
            pname = translate(para.child[1])
            f['table'].add(ptype, pname)
            asm.append(['MOV', f'{pname}', 'EAX'])

        current_func = node.current_func
        func_list[current_func] = f

        l4.append(['DEF', None, None, fname])

        translate(node.child[-1])

        asm.append(['RET'])
        asm.append([f'{fname}', 'ENDP'])

    elif node.atype == 'fordeclaration':

        ntype = translate(node.child[0])
        name = translate(node.child[-1].child[0])
        func_list[node.current_func]['table'].add(ntype, name)
        translate(node.child[-1])

    else:
        for i in node.child:
            if type(i) is not list:
                translate(i)


d1={
    "name":"dhruv",
    "surname":  "panchal",
    1:10100,
    "friend":"chintan",
    "marks":[1,2,4,5,7,8],
    "class":{
        2:3245,
        4:4534,
        6:45435
    }
}

print(d1)
print(d1.keys())
print(d1.values())
print(type(d1))
print(list(d1.items()))

print(d1["name"])
d1["marks"]=[45,6,78,8,43]
print(d1["marks"])
print(d1["class"][2])

d2={
    "pravin": "friend",
    "chintan":"friend",
    "abc":"becdcs"
}

d1.update(d2)

print(d1)

# print(list(d1.items()))
# d1.update({"dhruv":"it"})
# print(list(d1))
# print(list(d1.items()))

# print(d1["dhruv"])
# print(d1.get("dhruv"))

# print(d1.get("it"))   # it returns none if we can not get key value pair
# print(d1["it"])      # throw an error

#  sets

# s ={1,3,5,6,8}
# print(s)
# print(type(s))

# s={}  # empty dictionary
# print(type(s))

# s=set()  # empty set
# print(type(s))

# method

# s= set()
# s.add(4)
# s.add(45)     # add an element
# s.add(345)
# s.add("dhruv")
# print(s)
# print(type(s))

# s.remove("dhruv")     # remove an element
# # s.remove("panchal")   # throw a error
# print(s)

# s.pop()     # remove random element from set

# print(s)

# s.clear()    # empty set  (clear set)

# print(s)

# s = {1,2,4,5}

# s1=s.union({5,6,8,9})
# print(s1)

# s1=s.intersection({4,5,6,7,8})
# print(s1)

# pr

# mydic ={
#     "pankha" : "fan",
#     "vastu" : "item",
#     "dabba" : "box",
# }

# print("options are : " , mydic.keys())
# a = input("enter your choise " )
# print("meaning of your word is :" , mydic.get(a))

# n1 = int(input("enter a number 1 :" ))
# n2 = int(input("enter a number 2 :" ))
# n3 = int(input("enter a number 3 :" ))
# n4 = int(input("enter a number 4 :" ))
# n5 = int(input("enter a number 5 :" ))
# n6 = int(input("enter a number 6 :" ))
# n7 = int(input("enter a number 7 :" ))
# n8 = int(input("enter a number 8 :" ))

# s ={n1,n2,n3,n4,n5,n6,n7,n8}
# print(s)

 # take 3 differant element

# s = {11,"11", 11.454}
# print(type(s))
# print(s)
# print(len(s))

# s = set()
# s.add(20)
# s.add(20.0)   # take as same  20 = 20.0
# s.add("20")
# print(s)
# print(len(s))

# s ={}      # it is a dictionary
# print(type(s))

# s={}

# dhruv = input("dhruv enter your favourate language : ")
# ankita = input("ankita enter your favourate language : ")
# vidhi = input("vidhi enter your favourate language : ")
# ani = input("ani enter your favourate language : ")

# s["Dhruv"]=dhruv
# s["Ankita"]=ankita
# s["Vidhi"]=vidhi
# s["Ani"]=ani

# print(s)

# s={}

# dhruv = input("dhruv enter your favourate language : ")
# ankita = input("ankita enter your favourate language : ")
# vidhi = input("vidhi enter your favourate language : ")
# ani = input("ani enter your favourate language : ")

# s["Dhruv"]=dhruv
# s["Vidhi"]=vidhi
# s["Vidhi"]=ankita
# s["Ani"]=ani

# print(s)


class Employee:

    def __init__(self,id_number,lastname,firstname,birthdate):
        self.id_number = id_number
        self.lastname = lastname
        self.firstname = firstname
        self.birthdate = birthdate

    def __str__(self):
        return f'Employee: #{self.id_number} {self.firstname} {self.lastname} {self.birthdate}'

employee1 = Employee(10,'John','Doe','01/01/70')

print(employee1)


# E<= 15, S <= 28, M <= 19

from sys import stdin

E, S, M = map(int, stdin.readline().split())

# 입력받은 E 는 X % 15 S 는 X % 28 M 은 X % 19
count = 0
while True:
    year = 28 * count + S
    if year % 19 == M or (year % 19 == 0 and M == 19):
        if year % 15 == E or (year % 15 == 0 and E == 15):
            print(28*count + S)
            break

    count += 1

4.브루트포스/날짜계산.py