#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 30 16:02:42 2019

@author: mikesung
"""
import numpy as np
import pandas as pd
import polyglot
from polyglot.text import Text, Word
from polyglot.downloader import downloader
import json
from stanfordcorenlp import StanfordCoreNLP
import os
from google.cloud import translate
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/Users/mikesung/Downloads/My First Project-daebfec3c5a1.json"

firstDf = pd.read_csv("/Users/mikesung/Downloads/wiki-sentiment/multi-lang_articles_pandas_fixed.csv", header=None, names = ['de', 'en', 'es', 'ja', 'ru', 'Title', 'zh'])
secondDf = pd.read_csv("/Users/mikesung/Downloads/wiki-sentiment/multi-lang_articles_pandas_fixed_3.csv", header=None, names = ['de', 'en', 'es', 'ja', 'ru', 'Title', 'zh'])

df = pd.concat([firstDf, secondDf], ignore_index=True)

ja = df['ja']
ja = ja.drop(labels=[10, 11])
ja = ja.reset_index(drop=True)

ru = df['ru']
ru = ru.drop(labels = [10])
ru = ru.reset_index(drop=True)
# get EN series
en = df['en']

# get DE series
de = df['de']
de = de.drop(labels = [5, 11])
de = de.reset_index(drop=True)

# get es series
es = df['es']

# get zh series
zh = df['zh']
zh = zh.drop(labels = [11])
zh = zh.reset_index(drop=True)

a = json.load()

with open('/Users/mikesung/Downloads/wiki-sentiment/mergeDict_filtered.json', 'r') as f:
    mergeDictFilter = json.load(f)

engSet = []
for key, subDict in nerDict.items():
    engSet.extend(list(subDict.keys()))

engSet = list(set(engSet))
engSet.append("United Nations")
jaNerDict = {}

jaDictKeys = list(jaDict.keys())
jaDictValues = list(jaDict.values())
jaDictCount = np.array([len(elem) for elem in jaDictValues])
argSort = np.argsort(ruDictCount,axis=None)[::-1]
jaDictCount = jaDictCount.tolist()

topDict = {}
for i in range(len(argSort)):
    topDict[ruDictKeys[argSort[i]]] = ruDictCount[argSort[i]]

jsonDict = {}
jsonDict['ru'] = topDict

with open('/Users/mikesung/Downloads/jaSent.json', 'w') as fp:
    json.dump(jaSent, fp)

def getNERMappings(top150Key):
    translator = Translator()
    retDict = {}
    for elem in top150Key:
        try:
            trans = translator.translate(elem, dest='en').text
            retDict[trans] = elem
        except:
            pass
    return retDict

# get sentiment scores for one country
def getSentimentScores(mergeDict, laDict, countryCode):
    nlp = StanfordCoreNLP('/Users/mikesung/Downloads/wiki-sentiment/corenlp/stanford-corenlp-full-2018-10-05', lang='en', memory='8g', port=9000)
    translate_client = translate.Client()

    scoreDict = {}

    # sentence -> translate -> vector
    sentence_hashes = {}
    length = str(len(mergeDict))
    counter = 1

    for enNer in mergeDict.keys():
#        if counter >= 1:
#            return scoreDict
        print("Working on " + enNer + " " + str(counter) + "/" + length)

        if countryCode in mergeDict[enNer]:
            laNer = mergeDict[enNer][countryCode]
            scoreList = []
            for sentence in laDict[laNer]:

                if  sentence in sentence_hashes:
                    sentiVec = sentence_hashes[sentence]
                    scoreList.append(sentiVec)
                else:
                    if countryCode == 'en':
                        try:
                            trans = sentence
                            results = nlp.sentiment(trans)
                            sentiVec = results['sentences'][0]['sentimentDistribution']
                            scoreList.append(sentiVec)
                            sentence_hashes[sentence] = sentiVec
                        except:
                            print("Failed on one sentence for " + enNer + " " + laNer)
                            pass
                    else:
                        try:
                            trans = translate_client.translate(sentence,target_language='en')['translatedText']
                            results = nlp.sentiment(trans)
                            sentiVec = results['sentences'][0]['sentimentDistribution']
                            scoreList.append(sentiVec)
                            sentence_hashes[sentence] = sentiVec
                        except:
                            print("Failed on one sentence for " + enNer + " " + laNer)
                            pass
            scoreDict[enNer] = scoreList

        else:
            scoreDict[enNer] = []
        counter += 1
#    nlp.close()
    return scoreDict

def getNamedEntities(series, countryCode='en', threshold=5):
    retDict = {}
    for i in range(len(series)):
        text = Text(series[i], hint_language_code=countryCode)

        for sentence in text.sentences:
#            print(sentence)
            for ent in sentence.entities:
                word = ent[0].title()
                if word not in retDict:
                    retDict[word] = []
                    retDict[word].append(sentence.string)
                else:
                    retDict[word].append(sentence.string)

    toBeDel = []
    for key, valueList in retDict.items():
        if len(valueList) < threshold:
            toBeDel.append(key)

    for key in toBeDel:
        del retDict[key]

    return retDict

def getNamedEntitiesStanford(series, countryCode='en', threshold=5):
    nlp = StanfordCoreNLP('/Users/mikesung/Downloads/wiki-sentiment/corenlp/stanford-corenlp-full-2018-10-05', lang=countryCode, memory='8g', port=9000)
    desired_classes = ['PERSON', 'LOCATION', 'ORGANIZATION', 'CITY', 'STATE_OR_PROVINCE', 'COUNTRY', 'NATIONALITY', 'RELIGION', 'IDEOLOGY']
    retDict = {}
    for i in range(len(series)):
        print("Working on article " + str(i))
        text = Text(series[i], hint_language_code=countryCode)
        for sentence in text.sentences:
            try:
                NERList = nlp.ner(sentence.string)
                for tup in NERList:
                    word = tup[0]
                    if tup[1] in desired_classes:
                        if word not in retDict:
                            retDict[word] = []
                            retDict[word].append(sentence.string)
                        else:
                            retDict[word].append(sentence.string)
            except:
                print("Article " + str(i) + "failed to load.")
                pass

    toBeDel = []
    for key, valueList in retDict.items():
        if len(valueList) < threshold:
            toBeDel.append(key)

    for key in toBeDel:
        del retDict[key]

    nlp.close()
    return retDict

def mergeLaDict(enDict, listOfOther, codeList = ['de', 'es', 'ja', 'ru', 'zh']):
    translate_client = translate.Client()
    retDict = {}
    for enKey in enDict.keys():
        retDict[enKey] = {}
        retDict[enKey]['en'] = enKey
    for i in range(len(listOfOther)):
        dic = listOfOther[i]
        countryCode = codeList[i]
        print("Working on " + countryCode)

        for laKey in dic.keys():
            transKey = translate_client.translate(laKey, target_language='en')['translatedText']
            if transKey in retDict:
                retDict[transKey][countryCode] = laKey
            else:
                retDict[transKey] = {}
                retDict[transKey][countryCode] = laKey
    return retDict


#!/usr/bin/env python3

# Выводит на экран первые 'n' чётные числа
# Автор Вазинский Виталий

def even(number):

	even = number % 2

	return  even == 0

def main():
	number = int(input('Введите число: '))
	num = count = 0
	while count < number:
		num += 1
		if even(num):
			count += 1
			print(num, end=' ')
	print()

main()


import json
import os
import requests
import sqlite3

os.system("rm ../database.db")
os.system("./build_database.sh")

url = 'https://api.warframe.market/v1/items'
headers = {'accept': 'application', 'Language': 'zh-hans'}
s = requests.get(url=url, headers=headers).text

data = json.loads(s)

data = data['payload']['items']

for i in range(len(data)):
    data[i]['item_name'] = data[i]['item_name'].replace(' ', '')

conn = sqlite3.connect("../database.db")
curs = conn.cursor()

# nick_names = yaml.load(open('nick_names.yaml', 'r', encoding='utf-8').read(), Loader=yaml.FullLoader)

for i in data:
    item_name = i['item_name']
    # nick_name = ''
    # if 'Prime' in item_name and item_name[:item_name.find('Prime')] in nick_names:
    #     nick_name = nick_names[item_name[:item_name.find('Prime')]]

    curs.execute("INSERT INTO WM_ITEMS(ID, NAME, URL_NAME) VALUES(?, ?, ?)",
                 (i['id'], i['item_name'], i['url_name']))

curs.execute("SELECT * FROM WM_ITEMS")
curs.execute("INSERT INTO WF_MISC VALUES(?, ?)", ("Tenet", "None"))

conn.commit()


from django.urls import path
from . import views

urlpatterns = [
    path("", views.home, name="home"),
    path("cryptids/", views.cryptids, name='cryptids'),
    path("cryptids/<int:cryptid_id>/", views.cryptid, name="cryptid"),
    path("locations/<int:location_id>/", views.location, name='location'),
    path("locations/", views.locations, name='locations'),
    path("all_cryptids_here/", views.all_cryptids_here, name='all_cryptids_here'),
    path("cryptid_disc_date/", views.cryptid_disc_date, name='cryptid_disc_date'),
]


import sklearn.feature_selection as fs  # feature selection library in scikit-learn
import matplotlib.pyplot as plt
import numpy as np

def Feature_Ranking(X_train,y_train):
    '''Mutual Information Regression Metric for Feature Ranking'''
    mir_result = fs.mutual_info_regression(
        X_train,y_train
    )  # mutual information regression feature ordering
    feature_scores = []
    for i in np.arange(len(X_train.columns)):
        feature_scores.append([X_train.columns[i], mir_result[i]])
    sorted_scores = sorted(np.array(feature_scores), key=lambda s: float(s[1]), reverse=True)
    print(np.array(sorted_scores))
    # and plot...
    fig = plt.figure(figsize=(13, 6))
    ax = fig.add_subplot(111)
    ind = np.arange(len(X_train.columns))
    plt.bar(ind, [float(i) for i in np.array(sorted_scores)[:, 1]])
    ax.axes.set_xticks(ind)
    plt.title("Feature Importances (Mutual Information Regression)")
    plt.ylabel("Importance")
    # plt.xlabel('Trees in Forest')
    # plt.grid()
    plt.show()
    return sorted_scores


data = [] # 建立一个data变数他是一个空清单
count = 0 # 宣告一个变数 count 资料为 0
with open('reviews.txt', 'r') as f: # 用读取模式打开reviews档案并当作 f
    for line in f: # line 从 f 里
        data.append(line) # 新增line至 data的清单
        count += 1 # count = count +1
        if count % 1000 == 0: # 如果 count /1000 取余数等于 0的话就
            print(count) # 印出 每一千笔的 count
print('共有', len(data), '笔资料') # 印出 执行上列程式后所装的 data长度

print(data[0])

sum_len = 0 # 宣告 sum_len 变数资料为 0,纪录留言长度的总数
for d in data: # d 从 data里
    sum_len += len(d)
print('平均留言长度是', sum_len/len(data)) # 印出 sum_len总数 / data资料数 = 平均数

new = [] # 建立一个新的空清单为 new
for d in data: # d 从 data里
    if len(d) < 100: # 如果 d的长度 小于 100就
        new.append(d) # 新增 d到 new的清单
print('共有', len(new), '笔留言小于100个字') # 印出 小于100个字的清单总数

good = [] # 新增good空清单
for d in data: # d 从 data里
    if 'good' in d: #　如果字串 good有在 d里面就
        good.append(d) # 将 d新增至 good清单里
print('共有',len(good), '笔留言提到good') # 印出 提到good的总数

# good = [d for d in data if 'good' in d] # output = [(number-1)运算 for number变数 in reference清单 if number %筛选条件 2 == 0]

print(good[0])

# 文字记数
wc = {} # 建立 变数wc它是一个字典
for d in data: # 将每一个 d从 data中逐一取出
    words = d.split() # words 等于 每一次 d遇到的空行切割出来
    for word in words: # 将 word 从 words中逐一取出
        if word in wc: # 如果 word 有再 wc 里就
            wc[word] += 1 # 将wc里的 key值 +1
        else: # 否则
            wc[word] = 1 # 新增新的key进 wc字典

for word in wc: # 将 word 从 wc 里逐一取出
    if wc[word] > 1000000: # 如果 wc的key值 大于 一百万
        print(word, wc[word]) # 印出 key和 wc字典里的值

print(len(wc)) # 印出 wc字典长度
print(wc['Allen']) # 印出 wc字典 key'Allen'的值

while True:
    word = input('请输入查询字元: ') # 让使用者输入想查询的字元 存到变数 word
    if word == 'q': # 如果 word 等于 q
        break # 强制停止
    if word in wc: # 如果 word 再 wc字典里
        print(word, '出现的次数为', wc[word]) # 印出 key和wc字典出现的次数值
    else: # 否则
        print('这个字元没有出现') # 印出这个字元没有出现

print('感谢使用查询功能') # 强制停止后 印出感谢使用查询功能


#!/usr/bin/env python3.6
import yaml
from pprint import pprint

yaml_file3a = 'excercise3a.yml'
with open(yaml_file3a) as f:
    yaml_file3a = f.read()

int_list = yaml.load(yaml_file3a)
print()
print(int_list)

yaml_file3b = 'excercise3b.yml'
with open(yaml_file3b) as f:
    yaml_file3b = f.read()

int_vars = yaml.load(yaml_file3b)

print()
print('-' * 120)
pprint(int_vars)


# *********************************************************
# CLASS APQ

# COLIN KELLEHER 117303363
# ASSIGNMENT 2 - CS2516
# *********************************************************

# *********************************************************
# Import everything from the Element class
# *********************************************************
from Element import *

# *********************************************************
# CLASS APQ
# Adaptable Priority Queue using a Binary Heap
# *********************************************************

class APQ:
# *********************************************************
#  Initialise the binary heap
# *********************************************************
    def __init__(self):
        self._binaryheap = [] # Creating the binary heap

# *********************************************************
#  __str__
# Return a string representation of the Key, Value & Index
# *********************************************************
    def __str__(self):
        result = ""
        for item in self._binaryheap:
            result += ("Key: %i, Value: %s, Index: %i \t" % (item._key, item._value, item._index))
        return result

# *********************************************************
# length
# return the length of the binary heap
# *********************************************************
    def length(self):
        return len(self._binaryheap)

# *********************************************************
# addToHeap
# add Key & Item to binary heap

    # note to self
    # from CS2515 - bubble up when adding element to binary heap
    # pseudocode on lecture 17 - the heap
    # add item in last position
    # bubbleup heap
    # update last position
# *********************************************************
    def addToHeap(self, key, item):
        if APQ.length(self) == 0: # if the length of the heap is zero
            index = 0 # initialise i to be zero
        else: # otherwise if the length is greater than 0
            index = APQ.length(self) # i is initialised to length of heap
        element = Element(key, item, index) # create the element
        self._binaryheap.append(element) # add this to the binary heap - append as it is a list
        if APQ.length(self) > 1: # if the size of the binary heap is greater than 1, run the bubble up heap sort
            self.bubbleUpHeap(element)
        return element
# *********************************************************
# swap
# Swap elements within the heap - used within bubble up and bubble down
# used for swapping elements when bubbling up and down
# *********************************************************

    def swap(self, element, other):
        index = element._index # get the index of the element
        other_index = other._index # get the index of the other element
        self._binaryheap[index] = other # index is equal to other index
        self._binaryheap[other_index] = element # other index is equal to element
        element._index = other_index # element index is equal to other index
        other._index = index #other index is equal to index
    # performing overall swap of element to other place and
    # other place to element place

# *********************************************************
# bubbleUpHeap
# needed when adding a value to the heap to restore property
# *********************************************************
    def bubbleUpHeap(self, element):
        index = (element._index - 1) // 2 # get the index - index -1 divided by 2
        if index >= 0 and index < APQ.length(self) - 1:
            parent = self._binaryheap[index] # get the parent
            if element._key < parent._key: # if the key of the element is less than the key of the parent
                self.swap(element, parent) # swap the parent and the element
                self.bubbleUpHeap(element) # recursively call the method
        return self._binaryheap # return the restored binary heap
# *********************************************************
# bubbleDownHeap
# recursive
# Needed when removing an element from the heap
# *********************************************************
    def bubbleDownHeap(self, element):
        index = element._index # get the index
        left = 2 * index + 1 # get the left index
        right = 2 * index + 2 # get the rifht index
        if left < APQ.length(self) and right < APQ.length(self): # left and right less than length
            if self._binaryheap[left]._key < self._binaryheap[right]._key: # left key less than right key
                if element._key > self._binaryheap[left]._key: # element greater than left index key
                    self.swap(element, self._binaryheap[left]) # swap element and left
                    self.bubbleDownHeap(element) # bubble the element down the heap
            elif element._key > self._binaryheap[right]._key: # key greater than right key
                    self.swap(element, self._binaryheap[right]) # swap element and right
                    self.bubbleDownHeap(element) # bubble the element down the heap
        elif left < APQ.length(self): #if left index is less than heap length
            if element._key > self._binaryheap[left]._key: # element key greatet than lefy key
                self.swap(element, self._binaryheap[left]) # swap left and element
                self.bubbleDownHeap(element) #bubble the element down the heap
        elif right < APQ.length(self): # right less than the heap length
            if element._key > self._binaryheap[right]._key: # element key greater than right key
                self.swap(element, self._binaryheap[right]) # swap the element and right
                self.bubbleDownHeap(element) # bubble the element down the heap
        return self._binaryheap # return the final heap

# *********************************************************
# Remove
# Remove an element from the binary heap
# *********************************************************
    def remove(self, element):
        if element == self._binaryheap[0]: # if the element is as the top of the binary heap
            self.removemin() # call remove min
        elif element == self._binaryheap[-1]: # if the element is at index minus 1
            self._binaryheap.pop() # pop this element off the binary heap
        else:
            index = element._index # get index
            left = 2 * index + 1 #left index
            right = 2 * index + 2 # right index
            parent = (index - 1)//2 # parent index
            lastindex = APQ.length(self)-1 #last index in the heap
            self._binaryheap[index],self._binaryheap[lastindex] = self._binaryheap[lastindex], self._binaryheap[index]
            self._binaryheap[index]._index = index
            self._binaryheap.pop(lastindex) # pop the last element
            if self._binaryheap[index]._key < self._binaryheap[parent]._key:
                # if the index is less than the parent
                self.bubbleUpHeap(self._binaryheap[index]) # bubble up the heap passing index
            elif (left < APQ.length(self)) or (right < APQ.length(self)):
                # if the  left index is less than length of heap  right index is less than length
                self.bubbleDownHeap(self._binaryheap[index])
            # bubble down the heap passing tbe index
# *********************************************************
# min
# Return the minimum value in the binary heap
# *********************************************************

    def min(self):
        return self._binaryheap[0]._value

# *********************************************************
# removemin
# Return the minimum value (top) in the binary heap

    # note to self
    # from CS2515 - bubble down element when removing from top
    # pseudocode on lecture 17 - the heap
    # root node is at index 0
    # last item is at index size-1
# *********************************************************
    def removemin(self):
        element = self._binaryheap[0] # get the minimum element
        if APQ.length(self) == 1: # if there is only one element in the binary heap
            self._binaryheap.pop(0)  # pop the minimum
        else:
            self._binaryheap[0] = self._binaryheap[-1] #update index zero
            self._binaryheap[0]._index = 0 # update the index of the minimum to be zero
            self._binaryheap.pop(-1) # pop it off the heap
            self.bubbleDownHeap(self._binaryheap[0]) # run bubble down on index zero of the binary heap
        return element # return

# *********************************************************
# balance
# ensure the heap is still a binary heap - and fix if not after performing specific operations
# *********************************************************
    def balance(self, element):
        index = element._index # get the index of the element
        parent = (-1)//2 # get the parent
        left = 2 * index + 1 # get the left of the index
        if index <= APQ.length(self)-1: # ift he length -1 is less than the index
            if parent >= 0: # check if the parent is less than or equal to zero
                if self._binaryheap[index]._key < self._binaryheap[parent]._key:
                    self.bubbleUpHeap(self._binaryheap[index])
            elif left < APQ.length(self): # if left index is less than
                self.bubbleDownHeap(self._binaryheap[index])
            return self._binaryheap # return the balanced binary heap
# *********************************************************
# is_empty
# Check to see if the binary heap is empty
# *********************************************************

    def is_empty(self):
        return self._binaryheap == []

# *********************************************************
# update_key
# Used within Dijkstra algorithm to update the key of an element
# *********************************************************
    def update_key(self, element, new_key):
        element._key = new_key
        self.balance(element)
# *********************************************************
# get_key
# Return the key of an element within the heap
# *********************************************************
    def get_key(self, element):
        return self._binaryheap[element._index]._key


import sys
n,m = map(int,sys.stdin.readline().split())
a,b,direction = map(int,sys.stdin.readline().split())
d = [[0] * m for _ in range(n)]
d[a][b] = 1
game_map = []
for i in range(n):
    game_map.append(list(map(int,sys.stdin.readline().split())))

# x,y가 는 각각 이동할 값
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]

def turn_left():
    global direction
    direction -= 1
    if direction == -1:
        direction = 3
count = 1
turn_time = 0
while 1:
    turn_left()
    next_x = a+dx[direction]
    next_y = b+dy[direction]
    if game_map[next_x][next_y] == 0 and d[next_x][next_y] == 0:
        d[next_x][next_y] = 1
        a = next_x
        b = next_y
        count += 1
        turn_time=0
        continue
    else:
        turn_time += 1
    if turn_time==4:
        next_x = a-dx[direction]
        next_y = b-dy[direction]
        if game_map[next_x][next_y] == 0:
            a = next_x
            b = next_y
        else:
            break
        turn_time=0
print(count)

게임 개발(답안 예시).py


from typing import List, Optional
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

import server.schemas as schemas
from server.models.student import Student
from server.CRUD.utils import add_to_db
from server.validation.checks import student_check

async def create_student(db: AsyncSession, student: schemas.UserIn) -> Student:
    db_student = Student(
        first_name=student.first_name,
        last_name=student.last_name,
        email=student.email,
    )
    return await add_to_db(db_student, db=db)

async def get_students_by_group(db: AsyncSession, group_id: int) -> List[Student]:
    stmt = select(Student).where(Student.group_id == group_id)
    result = await db.execute(stmt)
    return result.scalars().all()

async def get_student_by_id(db: AsyncSession, user_id: int) -> Optional[Student]:
    stmt = select(Student).where(Student.id == user_id)
    result = await db.execute(stmt)
    return result.scalar_one_or_none()

async def get_student_checked(db: AsyncSession, id: int) -> Student:
    student = await db.get(Student, id)
    student_check(student)
    return student


import multiprocessing as mp
#
#定义一个被多线程调用的函数，q 就像一个队列， 用来保存每次函数运行的结果
#该函数 没有返回值
def job(q):
    res = 0
    for i in range(1000):
        res += i+i**2+i**3
    q.put(res)   #queue

#定义一个多线程队列，用来存储结果
if __name__ == '__main__':
    q = mp.Queue()
    #定义两个线程函数，用来处理同一个任务
    p1 = mp.Process(target=job,args=(q,))#args 的参数只要一个值的时候，参数后面需要加一个逗号，表示args是可迭代的，后面可能还有别的参数，不加逗号会出错
    p2 = mp.Process(target=job,args=(q,))
    #分别启动、连接两个线程
    p1.start()
    p2.start()
    p1.join()
    p2.join()
    #上面是分批处理的，所以这里分两批输出，将结果分别保存
    res1 = q.get()
    res2 = q.get()
    #打印最后的运算结果
    print(res1+res2)


from collections.abc import Sequence

from structlog.stdlib import get_logger

log = get_logger()

CONSENSUS_PREFIX = "Below are some answers to the question '{question}'"
CLUSTER_DIRECTIONS = """Cluster these answers by how they answer the question '{question}'. Write your answer in the form "Cluster N) [Answer]":

Cluster 1)"""

FINAL_DIRECTIONS = """Based on these clusters, write a single, detailed answer based on a consensus of the proposed answers, that is most likely to be correct.

Detailed, consensus answer:"""

def candidates(
    answers: Sequence[str], prefix: str = "Answer", separator: str = "\n"
) -> str:
    return separator.join(
        (f"{prefix} {idx}) {answer.strip()}" for idx, answer in enumerate(answers, 1))
    )

def build_cluster_prompt(question: str, answers: Sequence[str]) -> str:
    prefix = CONSENSUS_PREFIX.format(question=question)
    return "\n\n".join(
        (prefix, candidates(answers), CLUSTER_DIRECTIONS.format(question=question))
    )

def build_final_prompt(question: str, answers: Sequence[str], clusters: str) -> str:
    cluster_prompt = build_cluster_prompt(question, answers)
    return "\n\n".join((cluster_prompt + clusters.rstrip(), FINAL_DIRECTIONS))


# -*- coding: utf-8 -*-
# @Time    : 2022/8/19 21:58
# @Author  : lzc
# @Email   : hybpjx@163.com
# @File    : 20行列转换.py
# @Software: PyCharm
import pandas as pd

# 显示不全所作的操作
pd.options.display.max_columns = 999
# 把月份当成Month
videos = pd.read_excel("source/20Videos.xlsx", index_col='Month')
# 转置
table = videos.transpose()
print(table)


import tensorflow as tf

class Encoder(tf.keras.Model): # <-- Needed to make parameters trainable and to be callable
    def __init__(self, embedding_size):
        super(Encoder, self).__init__()
        self.layer_list = [

            tf.keras.layers.Conv2D(32, kernel_size=(3, 3), strides=2, padding='same', activation='relu'),
            tf.keras.layers.Conv2D(64, kernel_size=(3, 3), strides=2, padding='same', activation='relu'),

            tf.keras.layers.Flatten(),

            tf.keras.layers.Dense(7*7*16),

            tf.keras.layers.Dense(embedding_size, activation='relu')
        ]

    @tf.function
    def call(self, x):
        for layer in self.layer_list:
            x = layer(x)
        return x

    #@tf.function
    def getShapes(self, x):

        shapeAfterLastConv = None
        denseLayerSize = None
        for layer in self.layer_list:

            x = layer(x)
            if isinstance(layer, tf.keras.layers.Conv2D):
                shapeAfterLastConv = x.shape[1:] # ignore batch dim
            elif isinstance(layer, tf.keras.layers.Flatten):
                denseLayerSize = x.shape[1] # ignore batch dim

        return shapeAfterLastConv, denseLayerSize


import contextlib
import os
import time

import sqlalchemy.exc
from pytest import raises as assert_raises
from pytest import skip

import tangos
import tangos as db
import tangos.blocking
import tangos.core.halo
import tangos.core.simulation
import tangos.core.timestep
import tangos.parallel_tasks as pt
from tangos import log, testing
from tangos.config import testing_db_backend

def setup_module():
    if testing_db_backend != "sqlite":
        skip("This test is only relevant for sqlite databases")

    pt.use("multiprocessing")
    testing.init_blank_db_for_testing(timeout=0.1, verbose=False)

    session = db.core.get_default_session()

    sim = tangos.core.simulation.Simulation("sim")
    session.add(sim)

    ts1 = tangos.core.timestep.TimeStep(sim, "ts1")
    session.add(ts1)

    halo_1 = tangos.core.halo.Halo(ts1, 1, 1, 1, 0, 0, 0, 0)
    session.add_all([halo_1])

    session.commit()

def teardown_module():
    tangos.core.close_db()
    try:
        os.remove("test.db")
    except OSError:
        pass

def _multiprocess_block():

    session = db.core.get_default_session()

    ts = tangos.get_timestep("sim/ts1")
    tangos.core.creator.get_creator(session) # this stops a warning being issued if the creator object is created during creation of Halo
    new_halo = tangos.core.halo.Halo(ts, 5, 5, 5, 0, 0, 0, 0)

    session.add(new_halo)
    session.flush()

    time.sleep(1.0)

    session.commit()

def _multiprocess_test():
    session = db.core.get_default_session()

    time.sleep(0.5)

    ts = tangos.get_timestep("sim/ts1")

    tangos.core.creator.get_creator(session) # this stops a warning being issued if the creator object is created during creation of Halo
    new_halo = tangos.core.halo.Halo(ts, 6, 6, 6, 0, 0, 0, 0)

    session.add(new_halo)

    session.commit()

def _perform_test(use_blocking=True):
    if pt.backend.rank()==1:
        db.init_db("sqlite:///test_dbs/test_blocking_session.db", timeout=0.1, verbose=False)
        pt.barrier()
    else:
        pt.barrier()
        db.init_db("sqlite:///test_dbs/test_blocking_session.db", timeout=0.1, verbose=False)
    if use_blocking:
        db.blocking.make_engine_blocking()
    if pt.backend.rank()==1:
        _multiprocess_block()
    elif pt.backend.rank()==2:
        _multiprocess_test()

@contextlib.contextmanager
def _suppress_exception_report():
    import tangos.parallel_tasks.backends.multiprocessing as backend
    backend._print_exceptions = False  # to prevent confusing error appearing in stdout
    yield
    backend._print_exceptions = True

def test_non_blocking_exception():
    pt.use("multiprocessing-3")
    if testing_db_backend != "sqlite":
        skip("This test is only relevant for sqlite databases")

    with _suppress_exception_report():
        with assert_raises(sqlalchemy.exc.OperationalError):
            with log.LogCapturer():
                pt.launch(_perform_test, (False,))

def test_blocking_avoids_exception():
    pt.use("multiprocessing-3")
    if testing_db_backend != "sqlite":
        skip("This test is only relevant for sqlite databases")

    assert tangos.get_halo("sim/ts1/6") is None
    db.core.get_default_session().commit()
    with log.LogCapturer():
        pt.launch(_perform_test, (True,))

    assert tangos.get_halo("sim/ts1/6") is not None


import os
from datetime import datetime
from flask import Flask
from flask import render_template, request, redirect, send_from_directory
from flaskext import mysql

app = Flask(__name__)
mysql = mysql.MySQL()

app.config['MYSQL_DATABASE_HOST'] = 'b6phvfwbvhi1c3mha7h9-mysql.services.clever-cloud.com'
app.config['MYSQL_DATABASE_USER'] = 'uo4gy1b7480dgnb4'
app.config['MYSQL_DATABASE_PASSWORD'] = 'UXmaMucXeSvvoQXxvcTJ'
app.config['MYSQL_DATABASE_DB'] = 'b6phvfwbvhi1c3mha7h9'

UPLOADS = os.path.join('src/uploads')
app.config['UPLOADS'] = UPLOADS # Guardo ruta como un valor en la app

mysql.init_app(app)
conn = mysql.connect()
cursor = conn.cursor()

def query_mysql(query, data):
    """Function for mysql queries"""
    if len(data) > 0:
        cursor.execute(query, data)
    else:
        cursor.execute(query)
    conn.commit()

@app.route('/')
def index():
    """Index template. Will show teams in database"""
    conn = mysql.connect()
    cursor = conn.cursor()
    sql = "SELECT * FROM rankingteams;"
    cursor.execute(sql)
    equipos = cursor.fetchall()
    print(equipos)
    conn.commit()
    return render_template('equipos/index.html', equipos = equipos)

@app.route('/create')
def create():
    return render_template('equipos/create.html')

@app.route('/store', methods=['POST'])
def storage():
    """Stores new team in database"""
    _name = request.form['txtName']
    _country = request.form['txtCountry']
    _manager = request.form['txtManager']
    _logo = request.form['teamLogo']

    now = datetime.now()
    print(now)
    tiempo = now.strftime("%Y%H%M%S")
    print(tiempo)

    sql = "INSERT INTO rankingteams (name, country, manager, logo_url) values (%s, %s, %s, %s)"
    data = (_name, _country, _manager, _logo)
    query_mysql(sql, data)
    return redirect('/create')

@app.route('/delete/<int:id>')
def delete(id):
    """Deletes team from database"""
    sql = f'DELETE FROM rankingteams WHERE id="{id}"'
    cursor.execute(sql)
    conn.commit()
    return redirect('/')

@app.route('/modify/<int:id>')
def modify(id):
    """Modifies teams in database"""
    sql = "SELECT * FROM rankingteams WHERE id = %s"
    conn = mysql.connect()
    cursor = conn.cursor()
    cursor.execute(sql, id)
    equipo = cursor.fetchone()
    conn.commit()
    return render_template('equipos/edit.html', equipo = equipo)

@app.route('/update', methods=['POST'])
def update():
    """Updates team from database"""
    _id = request.form['txtId']
    _name = request.form['txtName']
    _country = request.form['txtCountry']
    _manager = request.form['txtManager']
    _logo = request.form['teamLogo']

    conn = mysql.connect()
    cursor = conn.cursor()

    sql = f'UPDATE rankingteams SET name = "{_name}", country = "{_country}", manager = "{_manager}", logo_url = "{_logo}" WHERE id = "{_id}"'
    cursor.execute(sql)
    conn.commit()
    return redirect('/')

@app.route('/reset-ids')
def reset():
    """Resets auto increment of IDs from database"""
    data = []
    sql = f"ALTER TABLE b6phvfwbvhi1c3mha7h9.rankingteams auto_increment = 0;"
    query_mysql(sql, data)
    return redirect('/')

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


def solution(maps):
    answer = []
    N = len(maps)
    M = len(maps[0])
    visited = [[0 for _ in range(M)] for _ in range(N)]
    for n in range(N):
        for m in range(M):
            if not visited[n][m] and maps[n][m] != 'X':
                q = [(n, m)]
                visited[n][m] = 1
                cnt = int(maps[n][m])
                while q:
                    x, y = q.pop(0)
                    for dx, dy in (1, 0), (-1, 0), (0, 1), (0, -1):
                        nx, ny = x + dx, y + dy
                        if N > nx >= 0 and M > ny >= 0 and not visited[nx][ny] and maps[nx][ny] in '123456789':
                            q.append((nx, ny))
                            visited[nx][ny] = 1
                            cnt += int(maps[nx][ny])
                answer.append(cnt)
    return sorted(answer) if answer else [-1]


'''
Обычный сапер. На вход подается размеры поля, количество мин и их координаты

'''

x, y, amount = [int(i) for i in input().split()]
field = [[0]*x for _ in range(y)]
for _ in range(amount):
    i, j = [int(k) - 1 for k in input().split()]
    field[i][j] = '*'

    for ver in [-1, 0, 1]:
        for hor in [-1, 0, 1]:
            if 0 <= (i + ver) < x and 0 <= (j + hor) < y:
                if field[i + ver][j + hor] != '*':
                    field[i + ver][j + hor] += 1
'''
alternative option
            if (i + ver) == -1 or (j + hor) == -1 or (i + ver) == x or (j + hor) == y:
                continue
            elif field[i + ver][j + hor] != '*':
                field[i + ver][j + hor] += 1
                '''

for i in range(x):
    for j in range(y):
        if field[i][j] == 0:
            field[i][j]= '.'

for line in field:
    print(*line)

stepik/Programing on Python/Other solutions/Сапер.py


# Ticket deleting plugins

from trac import __version__ as TRAC_VERSION
from trac.core import *
from trac.ticket.model import Ticket
from trac.web.chrome import ITemplateProvider
from webadmin.web_ui import IAdminPageProvider
import re, traceback
from time import strftime, localtime

__all__ = ['TicketDeletePlugin']

class TicketDeletePlugin(Component):
    """A small ticket deletion plugin."""

    implements(ITemplateProvider, IAdminPageProvider)

    # IAdminPageProvider methods
    def get_admin_pages(self, req):
        if req.perm.has_permission('TICKET_ADMIN'):
            yield ('ticket', 'Ticket System', 'delete', 'Delete')
            yield ('ticket', 'Ticket System', 'comments', 'Delete Changes')

    def process_admin_request(self, req, cat, page, path_info):
        assert req.perm.has_permission('TICKET_ADMIN')

        req.hdf['ticketdelete.href'] = self.env.href('admin', cat, page)
        req.hdf['ticketdelete.page'] = page
        req.hdf['ticketdelete.redir'] = 1

        if req.method == 'POST':
            if page == 'delete':
                if 'ticketid' in req.args and 'ticketid2' in req.args:
                    if req.args.get('ticketid') == req.args.get('ticketid2'):
                        t = self._validate(req, req.args.get('ticketid'))
                        if t:
                            self._delete_ticket(t.id)
                            req.hdf['ticketdelete.message'] = "Ticket #%s has been deleted." % t.id

                    else:
                        req.hdf['ticketdelete.message'] = "The two IDs did not match. Please try again."
            elif page == 'comments':
                if 'ticketid' in req.args:
                    req.redirect(self.env.href.admin(cat, page, req.args.get('ticketid')))
                else:
                    t = self._validate(req, path_info)
                    if t:
                        req.hdf['ticketdelete.href'] = self.env.href('admin', cat, page, path_info)
                        try:
                            deletions = None
                            if "multidelete" in req.args:
                                deletions = [x.split('_') for x in req.args.getlist('delete')]
                                deletions.sort(lambda a,b: cmp(b[1],a[1]))
                            else:
                                buttons = [x[6:] for x in req.args.keys() if x.startswith('delete')]
                                deletions = [buttons[0].split('_')]
                            if deletions:
                                for field, ts in deletions:
                                    #field, ts = button.split('_')
                                    ts = int(ts)
                                    self.log.debug('TicketDelete: Deleting change to ticket %s at %s (%s)'%(t.id,ts,field))
                                    self._delete_change(t.id, ts, field)
                                    req.hdf['ticketdelete.message'] = "Change to ticket #%s at %s has been modified" % (t.id, strftime('%a, %d %b %Y %H:%M:%S',localtime(ts)))
                                    req.hdf['ticketdelete.redir'] = 0
                        except ValueError:
                            req.hdf['ticketdelete.message'] = "Timestamp '%s' not valid" % req.args.get('ts')

        if page == 'comments':
            if path_info:
                t = self._validate(req, path_info)
                if t:
                    for time, author, field, oldvalue, newvalue in t.get_changelog():
                        req.hdf['ticketdelete.changes.%s.fields.%s'%(time,field)] = {'old': oldvalue, 'new': newvalue}
                        req.hdf['ticketdelete.changes.%s.author'%time] = author
                        req.hdf['ticketdelete.changes.%s.prettytime'%time] = strftime('%a, %d %b %Y %H:%M:%S',localtime(time))

        return 'ticketdelete_admin.cs', None

    # ITemplateProvider methods
    def get_templates_dirs(self):
        """
        Return the absolute path of the directory containing the provided
        ClearSilver templates.
        """
        from pkg_resources import resource_filename
        return [resource_filename(__name__, 'templates')]

    def get_htdocs_dirs(self):
        """
        Return a list of directories with static resources (such as style
        sheets, images, etc.)

        Each item in the list must be a `(prefix, abspath)` tuple. The
        `prefix` part defines the path in the URL that requests to these
        resources are prefixed with.

        The `abspath` is the absolute path to the directory containing the
        resources on the local file system.
        """
        from pkg_resources import resource_filename
        #return [('ticketdelete', resource_filename(__name__, 'htdocs'))]
        return []

    # Internal methods
    def _get_trac_version(self):
        md = re.match('(\d+)\.(\d+)',TRAC_VERSION)
        if md:
            return (int(md.group(1)),int(md.group(2)))
        else:
            return (0,0)

    def _validate(self, req, arg):
        """Validate that arg is a string containing a valid ticket ID."""
        try:
            id = int(arg)
            t = Ticket(self.env, id)
            return t
        except TracError:
            req.hdf['ticketdelete.message'] = "Ticket #%s not found. Please try again." % id
        except ValueError:
            req.hdf['ticketdelete.message'] = "Ticket ID '%s' is not valid. Please try again." % arg
        return False

    def _delete_ticket(self, id):
        """Delete the given ticket ID."""
        major, minor = self._get_trac_version()
        if major > 0 or minor >= 10:
            ticket = Ticket(self.env,id)
            ticket.delete()
        else:
            db = self.env.get_db_cnx()
            cursor = db.cursor()
            cursor.execute("DELETE FROM ticket WHERE id=%s", (id,))
            cursor.execute("DELETE FROM ticket_change WHERE ticket=%s", (id,))
            cursor.execute("DELETE FROM attachment WHERE type='ticket' and id=%s", (id,))
            cursor.execute("DELETE FROM ticket_custom WHERE ticket=%s", (id,))
            db.commit()

    def _delete_change(self, id, ts, field=None):
        """Delete the change on the given ticket at the given timestamp."""
        db = self.env.get_db_cnx()
        cursor = db.cursor()
        ticket = Ticket(self.env,id)
        if field:
            if field == 'attachment':
                pass # Better handling still pending
            else:
                custom_fields = [f['name'] for f in ticket.fields if f.get('custom')]
                if field != "comment" and not [1 for time, author, field2, oldval, newval in ticket.get_changelog() if time > ts and field == field2]:
                    oldval = [old for _, _, field2, old, _ in ticket.get_changelog(ts) if field2 == field][0]
                    if field in custom_fields:
                        cursor.execute("UPDATE ticket_custom SET value=%s WHERE ticket=%s AND name=%s", (oldval, id, field))
                    else:
                        cursor.execute("UPDATE ticket SET %s=%%s WHERE id=%%s" % field, (oldval, id))
                cursor.execute("DELETE FROM ticket_change WHERE ticket = %s AND time = %s AND field = %s", (id, ts, field))
        else:
            for _, _, field, _, _ in ticket.get_changelog(ts):
                self._delete_change(id, ts, field)

        db.commit()


import re

class Solution:
    def isPalindrome(self, s: str) -> bool:
        s = re.sub('[^a-z0-9]', '', s.lower())
        return s == s[::-1]

input_str = "A man, a plan, a canal: Panama"
solution = Solution()
answer = solution.isPalindrome(input_str)
print(answer)


__all__ = (
    'PrometheusConfigOptions',
    'PrometheusConfig'
)

from ideadatamodel import SocaBaseModel, exceptions, constants
from ideasdk.utils import Utils, Jinja2Utils
from ideasdk.config.cluster_config import ClusterConfig

from typing import Dict, Optional, List

class PrometheusConfigOptions(SocaBaseModel):
    module_id: str
    base_os: str
    namespace: Optional[str]
    # if set to True, node exporter scrape_config will be added to scrape_configs
    node_exporter: Optional[bool]
    # if set to True, IDEA application server scrape_config will be added to scrape_configs
    app_exporter: Optional[bool]
    # prometheus scrape configurations
    # if node_exporter and app_exporter is None or False, scrape_configs is required.
    # refer to: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config
    scrape_configs: Optional[List[Dict]]

class PrometheusConfig:

    def __init__(self, cluster_config: ClusterConfig, options: PrometheusConfigOptions):
        self.config = cluster_config
        if Utils.is_empty(options.module_id):
            raise exceptions.invalid_params('module_id is required')
        if Utils.is_empty(options.base_os):
            raise exceptions.invalid_params('base_os is required')
        if Utils.is_false(options.node_exporter, True) and Utils.is_false(options.app_exporter, True):
            if Utils.is_empty(options.scrape_configs):
                raise exceptions.invalid_params('scrape_configs is required')

        # platform
        self.platform = Utils.get_platform(base_os=options.base_os)
        if Utils.is_true(options.app_exporter, False) and self.platform == constants.PLATFORM_WINDOWS:
            raise exceptions.invalid_params(f'app_exporter is not supported for platform: {self.platform}')

        # global scrape config
        self.scrape_interval = self.config.get_string('metrics.prometheus.scrape_interval', default='60s')
        self.scrape_timeout = self.config.get_string('metrics.prometheus.scrape_timeout', default='10s')

        # module id
        self.module_id = options.module_id

        # cluster name
        self.cluster_name = self.config.get_string('cluster.cluster_name', required=True)

        # aws region
        self.aws_region = self.config.get_string('cluster.aws.region', required=True)

        # namespace
        if Utils.is_empty(options.namespace):
            self.metrics_namespace = f'{self.cluster_name}/{self.module_id}'
        else:
            self.metrics_namespace = options.namespace

        # external labels
        external_labels = self.config.get_config('metrics.prometheus.external_labels')
        if external_labels is not None:
            external_labels = external_labels.as_plain_ordered_dict()
        else:
            external_labels = {}

        namespace_tokens = self.metrics_namespace.split('/')
        for index, token in enumerate(namespace_tokens):
            if index == 0:
                external_labels['cluster_name'] = token
                continue
            if index == 1:
                external_labels['module_id'] = token
                continue
            if index == 2:
                external_labels['component'] = token

        self.external_labels = external_labels

        # remote write
        remote_write_config = self.config.get_config('metrics.prometheus.remote_write', required=True)
        self.remote_write = remote_write_config.as_plain_ordered_dict()
        provider = self.config.get_string('metrics.provider', required=True)
        if provider == constants.METRICS_PROVIDER_AMAZON_MANAGED_PROMETHEUS:
            self.remote_write['sigv4'] = {
                'region': self.aws_region
            }

        # scrape configs
        scrape_configs = Utils.get_as_list(options.scrape_configs, [])
        if Utils.is_true(options.node_exporter, False):
            scrape_configs.append(self.get_default_node_exporter_scrape_config())
        if Utils.is_true(options.app_exporter, False):
            scrape_configs.append(self.get_default_app_exporter_scrape_config())
        self.scrape_configs = scrape_configs

    @staticmethod
    def get_default_node_exporter_scrape_config() -> Dict:
        return {
            'job_name': 'node_exporter',
            'static_configs': [
                {
                    'targets': [
                        'localhost:9100'
                    ]
                }
            ]
        }

    def get_default_app_exporter_scrape_config(self) -> Dict:
        api_context_path = self.config.get_string(f'{self.module_id}.server.api_context_path', required=True)
        server_port = self.config.get_int(f'{self.module_id}.server.port', required=True)
        config = {
            'job_name': 'app_exporter',
            'metrics_path': f'{api_context_path}/metrics',
            'scheme': 'http',
            'authorization': {
                'type': 'Bearer',
                'credentials_file': '/root/metrics_api_token.txt'
            },
            'static_configs': [
                {
                    'targets': [
                        f'localhost:{server_port}'
                    ]
                }
            ]
        }
        enable_tls = self.config.get_bool(f'{self.module_id}.server.enable_tls', default=False)
        if enable_tls:
            config['scheme'] = 'https'
            config['tls_config'] = {
                'insecure_skip_verify': True
            }
        return config

    def build(self) -> Dict:

        context = {
            'utils': Utils,
            'scrape_interval': self.scrape_interval,
            'scrape_timeout': self.scrape_timeout,
            'external_labels': self.external_labels,
            'remote_write': self.remote_write,
            'scrape_configs': self.scrape_configs
        }

        env = Jinja2Utils.env_using_package_loader(
            package_name='ideasdk.metrics.prometheus',
            package_path='templates'
        )

        template = env.get_template(f'prometheus-{self.platform}.yml')
        content = template.render(**context)
        return Utils.from_yaml(content)


from build_in_functions import iterate_mapping
from write_to_csv import seperator_to_csv

def create_region_file(regions_data, cwd):
    region_list = []
    region_list.append(['#blockwise'])
    unique_regions = iterate_mapping(regions_data,"map(&join(``,['/include(./regions/',@,'.csv)']),unique(timeseries[? timeindex_start== `{}`].region[]))".format('2016-01-01T00:00:00'))
    for region in unique_regions:
        region_list.append([region])

    seperator_to_csv(region_list, cwd + '/Region.csv')


# from bandits.data.mnist import get_raw_features, get_vae_features, construct_dataset_from_features
from bandits.algorithms.neural_linear_sampling import NeuralLinearPosteriorSampling
from bandits.core.hyperparams import HyperParams
from bandits.core.contextual_bandit import run_contextual_bandit
from bandits.data.data_sampler import sample_mushroom_data
import numpy as np
import torch
import time

file_name = "bandits/data/mushroom.data"

def display_results(algos, opt_rewards, opt_actions, h_rewards, t_init, name):
  """Displays summary statistics of the performance of each algorithm."""

  print('---------------------------------------------------')
  print('---------------------------------------------------')
  print('{} bandit completed after {} seconds.'.format(
    name, time.time() - t_init))
  print('---------------------------------------------------')

  performance_pairs = []
  for j, a in enumerate(algos):
    performance_pairs.append((a.name, np.sum(h_rewards[:, j])))
  performance_pairs = sorted(performance_pairs,
                             key=lambda elt: elt[1],
                             reverse=True)
  for i, (name, reward) in enumerate(performance_pairs):
    print('{:3}) {:20}| \t \t total reward = {:10}.'.format(i, name, reward))

  print('---------------------------------------------------')
  print('Optimal total reward = {}.'.format(np.sum(opt_rewards)))
  print('Frequency of optimal actions (action, frequency):')
  print([[elt, list(opt_actions).count(elt)] for elt in set(opt_actions)])
  print('---------------------------------------------------')
  print('---------------------------------------------------')

def main():
    data_type = 'mushroom'

    # vae_data = get_vae_features()
    # features, rewards, opt_vals = construct_dataset_from_features(vae_data)
    # dataset = np.hstack((features, rewards))

    num_contexts = 2000
    dataset, opt_mushroom = sample_mushroom_data(file_name, num_contexts)
    opt_rewards, opt_actions = opt_mushroom

    context_dim=117
    num_actions = 2

    # dataset, opt_rewards, opt_actions, num_actions, context_dim

    # hyperparams
    hp_nlinear = HyperParams(num_actions=num_actions,
                             context_dim=context_dim,
                             init_scale=0.3,
                             layer_sizes=[50],
                             batch_size=512,
                             activate_decay=True,
                             initial_lr=0.005,
                             max_grad_norm=5.0,
                             show_training=False,
                             freq_summary=1000,
                             buffer_s=-1,
                             initial_pulls=2,
                             reset_lr=True,
                             lr_decay_rate=0.5,
                             training_freq=1,
                             training_freq_network=50,
                             training_epochs=100,
                             a0=6,
                             b0=6,
                             lambda_prior=0.25,
                             keep_prob=1.0,
                             global_step=50)

    algos = [NeuralLinearPosteriorSampling('NeuralLinear', hp_nlinear)]

    t_init = time.time()

    # run contextual bandit experiment
    print(context_dim, num_actions)
    results = run_contextual_bandit(context_dim, num_actions, dataset, algos)
    _, h_rewards = results
    np.save("mushroom_rewards.npy", h_rewards)

    display_results(algos, opt_rewards, opt_actions, h_rewards, t_init, data_type)

if __name__ == '__main__':
    main()


import re
import requests

def format_number( number):
    try:
        number = number.replace(" ", "")  # Remove whitespace
        number = re.search(r"[0-9]{1,14}", number).group()  # Extract number with regex
        number = "+" + number
        return number
    except:
        return 0

def to_absolute(url, response):
    if not url:
        return None
    if bool(urlparse(url).netloc):
        return url
    else:
        return response.urljoin(url)
print(format_number("+34 604385313  dd"))

print(requests.get("https://tesms.net/").text)


from functools import wraps

from github import GithubException

from floxcore.exceptions import PluginException

def handle_exceptions(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        try:
            return f(*args, **kwargs)
        except GithubException as e:
            if e.data:
                message = e.data.get("message")
                documentation = e.data.get("documentation_url", e.data.get("errors"))
                raise PluginException(f'[Github API] [{e.status}] "{message}" check "{documentation}".')
            else:
                raise PluginException(f"[Github API] [HTTP Status: {e.status}]")

    return wrapper

def authenticate_url(url: str, github_api):
    if "github.com" not in url or not url.startswith("https://"):
        return url

    return url.replace("https://", f"https://{github_api.flox.secrets.getone('github_token')}@")


__author__ = 'Administrator'
# 동전계산 프로그램 함수와 리스트를 사용해서 만들어보기.
# 리스트에 1000원권을 추가해도 코드의 수정이 필요 없을 것
# 100원짜리만 있을 때 다른 주화는 물어보지 않는 것. 입금할 주화를 선택. 총계.

"""
c10=10
c50=50
c100=100
c500=500
"""

# calcValue (10)
total = 0
coinValue = 0
coinTable = [10, 50, 100, 500,1000]
length = len(coinTable)

def multipleVal (coinValue, coinNum):
    return coinValue*coinNum

def choiceCoin ():
    choice = int(input("입금할 주화의 번호를 선택해주세요 \n 1. 10원 2. 50원 3. 100원 4. 500원 5. 1000원 \n"))
    if choice < 0:
        print("값을 잘못입력하셨습니다. 프로그램을 종료합니다.")

    elif choice > length:
        print("값을 잘못입력하셨습니다. 프로그램을 종료합니다.")

    else:
        coinNum = int(input("갯수를 입력해주세요: "))
        return multipleVal(coinTable[choice-1],coinNum)

def myExit ():
    print("종료하시겠습니까? 종료는 0번 계속은 1번을 입력하세요")
    power = int(input())
    if power == 0:
        print("종료하겠습니다 정산금액은 아래와 같습니다.")
        print(total)
        return 1
    else :
        print("계속하겠습니다")

def main():
    while True:
        global total
        total = total+ choiceCoin()
        if myExit() == 1:
            break

if __name__ == '__main__':
    main()

"""
while i<length

    total = total+ multipleVal(coinTable[i],coinNum)
    i += 1
    print (total)

choice = int(input("입금할 주화를 선택해주세요: "))
"""

"""
while True:
    total = total+ 1
    print("종료하시겠습니까? 종료는 0번을 입력하세요")
    power = int(input())
    if power == 0:
        print("종료하겠습니다")

        break
    else :
        print("계속하겠습니다")

# coin = [c10Num,c50Num,c100Num,c500Num]

c10Num=int(input("10원짜리의 개수를 입력"))
c50Num=int(input("50원짜리의 개수를 입력"))
c100Num=int(input("100원짜리의 개수를 입력"))
c500Num=int(input("500원짜리의 개수를 입력"))

total=((coinValue[0]*c10Num)+(coinValue[1]*c50Num)+(coinValue[2]*c100Num)+(coinValue[3]*c500Num))
print(total)
"""


import math
import statistics
import random
import numpy as np

def sign(x):
    if x > 0:
        return 1
    else:
        return -1

def x():
    return random.random() * 2 - 1

def y(x):
    if x > 0:
        y = 1
    elif x < 0:
        y = -1
    prob = random.random()
    if prob <= 0.2:
        y = -y

    return y

def minimize_Ein(X, Y):
    # 升序排列
    X, Y = list(zip(*sorted(
        zip(X, Y),
        key=lambda x_y_pair: x_y_pair[0]
    )))

    Ein = None
    best_s_list = None
    best_theta_list = None
    N = len(X)
    for i in range(N):
        if i < N - 1:
            r = random.random()
            interval = X[i + 1] - X[i]
            theta = X[i] + r * interval
        else:
            r = random.random()
            interval = 1 - X[i]
            theta = X[i] + r * interval

        s = 1
        Ein_current = calculate_Ein(X, Y, s, theta)
        if Ein is None or Ein_current < Ein:
            Ein = Ein_current
            best_s_list = [s]
            best_theta_list = [theta]
        elif Ein_current == Ein:
            best_s_list.append(s)
            best_theta_list.append(theta)

        s = -1
        Ein_current = calculate_Ein(X, Y, s, theta)
        if Ein is None or Ein_current < Ein:
            Ein = Ein_current
            best_s_list = [s]
            best_theta_list = [theta]
        elif Ein_current == Ein:
            best_s_list.append(s)
            best_theta_list.append(theta)

    best_index = random.randrange(0, len(best_s_list), 1)
    best_s = best_s_list[best_index]
    best_theta = best_theta_list[best_index]

    return Ein, best_s, best_theta

def calculate_Ein(X, Y, s, theta):
    Ein = 0
    for index, x in enumerate(X):
        y = Y[index]
        if y == s * sign(x - theta):
            pass
        else:
            Ein = Ein + 1
    # Average
    return Ein / len(X)

def single_dimension():
    N = 20
    iter = 5000
    Ein_list = []
    Eout_list = []
    for _ in range(iter):
        X = [
            x()
            for i in range(N)
        ]
        Y = [
            y(x)
            for x in X
        ]
        Ein, s, theta = minimize_Ein(X, Y)
        Eout = 0.5 + 0.3 * s * (abs(theta) - 1)
        Ein_list.append(Ein)
        Eout_list.append(Eout)
    print(
        statistics.mean(Ein_list),
        statistics.mean(Eout_list)
    )

def multi_dimension():
    X_train = np.loadtxt('https://www.csie.ntu.edu.tw/~htlin/mooc/datasets/mlfound_math/hw2_train.dat')
    Y_train = X_train[:, -1]
    X_train = X_train[:, :9]
    X_test = np.loadtxt('https://www.csie.ntu.edu.tw/~htlin/mooc/datasets/mlfound_math/hw2_test.dat')
    Y_test = X_test[:, -1]
    X_test = X_test[:, :9]

    Ein_multidimension = []
    s_multidimension = []
    theta_multidimension = []
    for dimension_index in range(X_train.shape[1]):
        X = X_train[:, dimension_index]
        Y = Y_train
        Ein, s, theta = minimize_Ein(X, Y)
        Ein_multidimension.append(Ein)
        s_multidimension.append(s)
        theta_multidimension.append(theta)
    best_dimension = np.argmin(Ein_multidimension)
    Ein = Ein_multidimension[best_dimension]
    s = s_multidimension[best_dimension]
    theta = theta_multidimension[best_dimension]

    Eout = calculate_Ein(X_test[:, best_dimension], Y_test, s, theta)

    print(Ein, Eout)

if __name__ == '__main__':
    single_dimension()

    multi_dimension()


# Optimized stacks
from collections import deque

"""
ADVENT OF CODE 2022

DAY 5 PUZZLE 1

"""

def genStacks(rawInput):
    """
    Parse the raw input.

    """

    # Split the file.
    bottomLine = rawInput[-1]
    contents   = rawInput[:-1]

    stackIdx = []
    stacks   = []

    # - Loop over the bottom line, which contains the number of stacks.
    for i, char in enumerate(bottomLine):
        # - Save the string index in which a number is found, and create an empty stack
        # object.
        if(char != " "):
            stackIdx.append(i)
            stacks.append(deque())

    # print(stackIdx)

    # For each row of the contents...
    for row in contents:

        # Loop over the stack indices...
        for sIdx, rowIdx in enumerate(stackIdx):

            # - If the string index from the bottom row matches with something
            # that's not a space or out of bounds, then it's part of that stack.
            # - Add it to the corresponding stack, the i-th stack of the list.
            try:
                if (row[rowIdx] != " "):
                    stacks[sIdx].appendleft(row[rowIdx])
            except Exception:
                pass

    # print(stacks)
    return stacks

#%% MAIN

# Load input
# PATH = "input_sample.txt"
PATH = "input.txt"

file = open(PATH, 'r')

#%%

stackConfig = []
stackMoves  = []

headerFound = False
# Loop the input...
for line in file:

    # If before the line break, it's part of the stack configuration.
    # - Save it with spaces but not the ending line break.
    # - Save the rest in another list, remove the strings since those don't
    # add any important info. Save only the numbers.
    if not headerFound:
        if(line == "\n"):
            headerFound = True
        else:
            stackConfig.append(line[:-1])
    else:
        stackMoves.append(line[:-1].split()[1::2])

#%%

# Generate stacks based on the raw input.
stacks = genStacks(stackConfig)

#%%

# For each instruction in the list...
for move in stackMoves:

    # Get the crates to move, the stack of origin and destination.
    cratesToMove = int(move[0])
    origStack    = int(move[1]) - 1
    destStack    = int(move[2]) - 1

    # Move the number of crates from the origin to destination.
    for i in range(0, cratesToMove):
        crate = stacks[origStack].pop()
        stacks[destStack].append(crate)

# Save the tops of the stacks and show to screen.
top = ""
for stck in stacks:
    top += stck.pop()

print("After rearrangement, the top of the stacks are {}".format(top))

#%%
file.close()


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

"""
Some metadata plotting functions:
- 1D histograms
- 2D scatter plots
"""

import numpy as np

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import seaborn as sns

from osmdq import utils

def md_scatter(metadata_x, metadata_y):
    """Draw a scatter plot from metadata features

    Parameters
    ----------
    metadata_x: pd.Series
        metadata that has to be plotted in abscissa
    metadata_x: pd.Series
        metadata that has to be plotted in ordinates

    """
    plt.plot(metadata_x, metadata_y, 'o')
    plt.xlabel(metadata_x.name)
    plt.ylabel(metadata_y.name)
    plt.tight_layout()
    plt.show()

def md_scatter_set(metadata, pattern, nb_subplot_col=2):
    """Draw 2D scatter plots from metadata features

    Parameters
    ----------
    metadata: pd.DataFrame
        metadata to plot
    pattern: object
        string designing the names of the features that have to be plotted
    nb_subplot_col: integer
        number of plots that must be draw horizontally

    """
    md_scatter = utils.extract_features(metadata, pattern)
    nb_components = len(md_scatter.columns)
    f, ax = plt.subplots(len(md_scatter.columns), len(md_scatter.columns),
                         figsize=(16, 12))
    for column in md_scatter:
        i = np.where(md_scatter.columns == column)[0][0]
        for column2 in md_scatter:
            if column != column2:
                j = np.where(md_scatter.columns == column2)[0][0]
                ax_ = ax[i][j]
                ax_.plot(md_scatter[column], md_scatter[column2], 'o')
                ax_.set_xlabel(column)
                ax_.set_ylabel(column2)
    f.tight_layout()
    f.show()

def md_hist(metadata_x, bins=np.linspace(0,1,21)):
    """Draw an histogram of the metadata_x feature

    Parameters
    ----------
    metadata_x: pd.Series
        metadata that has to be plotted in histogram
    bins: np.array
        array describing each histogram bin; the i-th bin is between the i-th
    and the (i+1)-th

    """
    plt.hist(metadata_x, bins=bins, normed=1)
    plt.xlabel("Histogram of "+metadata_x.name)
    plt.ylabel("Frequency (%)")
    plt.tight_layout()
    plt.show()

def md_hist_set(metadata, pattern, bins=np.linspace(0,1,51), nb_subplot_col=2):
    """Draw a set of histogram for each features of metadata that corresponds
    to the pattern

    Parameters
    ----------
    metadata: pd.DataFrame
        metadata that has to be plotted in histograms
    pattern: object
        string that designs the feature to plot (through regular expressions)
    bins: np.array
        array describing each histogram bin; the i-th bin is between the i-th
    and the (i+1)-th
    nb_subplot_col: integer
        number of plots that must be draw horizontally

    """
    md_hist = utils.extract_features(metadata, pattern)
    nb_components = len(md_hist.columns)
    nb_vertical_plots = int(nb_components/nb_subplot_col)
    if nb_components%nb_subplot_col > 0:
        nb_vertical_plots = nb_vertical_plots + 1
    f, ax = plt.subplots(nb_vertical_plots, nb_subplot_col, figsize=(16, 12))
    for column in md_hist:
        i = np.where(md_hist.columns == column)[0][0]
        data = md_hist[column]
        ax_ = ax[int(i/nb_subplot_col)][i%nb_subplot_col]
        ax_.hist(data, bins=bins, normed=1)
        ax_.set_ylim(0,50)
        ax_.set_title(data.name)
    f.tight_layout()
    f.show()

def md_multiplot(metadata, pattern):
    """Draw a generic plot of metadata features with 1D histogram and 2D
    scatter plots; use regex to identify features to plot

    Parameters
    ----------
    metadata: pd.DataFrame
        metadata that has to be plotted
    pattern: object
        string designing the feature names that has to be plotted

    """
    plotted_metadata = utils.extract_features(metadata, pattern)
    sns.pairplot(plotted_metadata)

def md_corplot(metadata, pattern):
    """ Draw a correlation plot of metadata features; consider only features
    that correspond to pattern

    Parameters
    ----------
    metadata: pd.DataFrame
        metadata that has to be plotted
    pattern: object
        string designing the feature names that has to be plotted

    """
    md_corplot = utils.extract_features(metadata, pattern)
    f, ax = plt.subplots(figsize=(12,10))
    sns.heatmap(md_corplot.astype(float).corr(),
                vmax=1,
                square=True,
                annot=True,
                annot_kws={'size':10},
                fmt='.2f')
    plt.xticks(rotation=90)
    plt.yticks(rotation=0)
    f.tight_layout()
    f.show()


#exercise 4\n",
# Item counter\n",
def main():
    #initialize a counter
    count = 0
    #open the file
    #i use friends.txt coz i have that file
    friends_count = open('Friends.txt', 'r')
    # read everyline and increment the counter

    for line in friends_count:
        friends_name = line
        count = count + 1
    friends_count.close()
    print('You have a total of ', count, ' friends.')
#call the main function
main()


import logging
import string
import re
import praw
import pandas as pd
from collections import Counter

logger = logging.getLogger(__name__)

def sanitize_comment_body(body: str) -> str:
    """Sanitize comment body.

    Delete newline characters and quotes in comments (lines beginning with >).
    Escape [ and ] characters.
    """
    result = " ".join(
        [
            x
            for x in body.replace("[", "\\[").replace("]", "\\]").split("\n")
            if not x.startswith(">")
        ]
    ).strip()
    return result

def sanitize_long_text(text: str, max_length: int = 150) -> str:
    """Sanitize long text.

    Return 150 characters or less from any text.
    If the text is longer than 150 characters long,
    cut it at 149 characters and add a … at the end of it.
    """
    text = text.replace("\n", "")
    if len(text) >= max_length:
        return text[0 : max_length - 1].rsplit(" ", 1)[0] + "…"
    return text

def sanitize_username(username: str) -> str:
    """Sanitize an username."""
    if username in ["None", "/u/None"]:
        return "un inconnu"
    return username

def sanitize_link(link: str) -> str:
    """Sanitize link."""
    return f"https://reddit.com{link}?context=2"

def get_best_post(df_posts: pd.DataFrame) -> dict[str, str]:
    """Post with the best score."""
    best_post = df_posts.loc[df_posts["score"].idxmax()]
    return {
        "best_post_author": best_post["author"],
        "best_post_score": best_post["score"],
        "best_post_title": best_post["title"],
        "best_post_link": best_post["permalink"],
        "best_post_id": best_post["id"],
    }

def get_commented_post(df_posts: pd.DataFrame) -> dict[str, str]:
    """Most commented post.

    Forum Libre posts are filtered out as they generally have a lot of comments.
    """
    subset = df_posts[~df_posts.title.str.startswith("Forum Libre")]
    commented_post = subset.loc[subset["num_comments"].idxmax()]
    return {
        "commented_post_author": commented_post["author"],
        "commented_post_comments": commented_post["num_comments"],
        "commented_post_title": commented_post["title"],
        "commented_post_link": commented_post["permalink"],
        "commented_post_id": commented_post["id"],
    }

def get_best_comment(df_comments: pd.DataFrame) -> dict[str, str]:
    """Comment with the best score."""
    best_comment = df_comments.loc[df_comments["score"].idxmax()]
    return {
        "best_comment_author": best_comment["author"],
        "best_comment_score": best_comment["score"],
        "best_comment_body": sanitize_long_text(best_comment["body"]),
        "best_comment_link": best_comment["permalink"],
        "best_comment_id": best_comment["id"],
    }

def get_worst_comment(df_comments: pd.DataFrame) -> dict[str, str]:
    """Comment with the worst score."""
    worst_comment = df_comments.loc[df_comments["score"].idxmin()]
    return {
        "worst_comment_author": worst_comment["author"],
        "worst_comment_score": worst_comment["score"],
        "worst_comment_body": sanitize_long_text(worst_comment["body"]),
        "worst_comment_link": worst_comment["permalink"],
        "worst_comment_id": worst_comment["id"],
    }

def get_discussed_comment(
    reddit: praw.Reddit, df_comments: pd.DataFrame
) -> dict[str, str]:
    """Comment with the most answers.

    The most discussed comment might not be in df_comments
    so we extract it separately.
    """
    subset = df_comments[df_comments.parent.str.startswith("t1_")][
        "parent"
    ].value_counts()
    discussed_comment_answers = subset[0]
    discussed_parent_id = subset.idxmax().split("_")[-1]
    discussed_comment = df_comments[df_comments["id"] == discussed_parent_id]
    if not discussed_comment.empty:
        return {
            "discussed_comment_author": discussed_comment.author.to_string(index=False),
            "discussed_comment_answers": discussed_comment_answers,
            "discussed_comment_body": sanitize_long_text(
                discussed_comment.body.to_string(index=False)
            ),
            "discussed_comment_link": discussed_comment.permalink.to_string(
                index=False
            ),
            "discussed_comment_id": discussed_comment.id.to_string(index=False),
        }
    else:
        logger.warning(
            "Most discussed comment is not in df_comments. Extracting it separately."
        )
        comment = reddit.comment(discussed_parent_id)
        comment.refresh()
        return {
            "discussed_comment_author": comment.author,
            "discussed_comment_answers": str(len(comment.replies)),
            "discussed_comment_body": sanitize_long_text(comment.body),
            "discussed_comment_link": f"https://reddit.com{comment.permalink}",
            "discussed_comment_id": discussed_parent_id,
        }

def get_amoureux(df_comments: pd.DataFrame) -> dict[str, str]:
    """Two users that interacted with each other the most."""
    # filter comments answering to another comment
    subset = df_comments[df_comments.parent.str.startswith("t1_")]
    subset.loc[:, "parent_id"] = subset.parent.str.split("_").str[-1]
    subset2 = subset.set_index("id").join(subset.set_index("parent_id"), rsuffix="_new")
    subset3 = (
        subset2[subset2.author_new.notnull()][["author", "author_new"]]
        .apply(Counter, axis="columns")
        .value_counts()
    )
    score = subset3[0]
    authors = [x for x, y in subset3.index[0].items()]
    return {
        "amoureux_author1": str(authors[0]),
        "amoureux_author2": str(authors[1]),
        "amoureux_score": score,
    }

def get_qualite(df_comments: pd.DataFrame) -> dict[str, str]:
    """From : https://www.reddit.com/r/BestOfFrance/wiki/index

    Le prix qualité récompense le participant qui a le meilleur rapport "karma par caractère tapés".
    Pour prétendre à ce titre, il faut avoir contribué au moins 140 caractères dans la journée.
    Le score est mesuré en milliSPHKS en l'honneur de /u/sphks qui a suggéré cette fonctionnalité (1 SPHKS = 1 point de karma par caractère).
    """
    subset = df_comments.groupby(["author"]).sum()
    subset2 = subset[subset.length > 140].copy()
    subset2.loc[:, "milliSPHKS"] = subset2["score"] / subset2["length"] * 1000
    qualite_author = subset2.loc[subset2["milliSPHKS"].idxmax()]
    return {
        "qualite_author": qualite_author.name,
        "qualite_score": round(qualite_author["milliSPHKS"], 2),
    }

def get_poc(df_comments: pd.DataFrame) -> dict[str, str]:
    """User that posted the most comments."""
    poc = df_comments["author"].value_counts()
    return {
        "poc_author": str(poc.idxmax()),
        "poc_score": poc[0],
    }

def get_tartine(df_comments: pd.DataFrame) -> dict[str, str]:
    """User that typed the most characters."""
    subset = df_comments.groupby(["author"]).sum()["length"]
    return {
        "tartine_author": str(subset.idxmax()),
        "tartine_score": subset[subset.idxmax()],
    }

def get_capslock(df_comments: pd.DataFrame) -> dict[str, str]:
    """User that typed the most uppercase characters.

    Only the characters from full uppercase words are taken into account.
    """
    subset = df_comments
    # replace punctuation with space so they don't count as characters in words
    subset["body2"] = subset["body"].str.replace(
        "[{}]".format(string.punctuation), " ", regex=True
    )
    subset["capslock"] = subset["body2"].apply(
        lambda row: sum([len(x) for x in row.split() if x.isupper()])
    )
    subset2 = subset.groupby("author").sum()["capslock"]
    return {
        "capslock_author": str(subset2.idxmax()),
        "capslock_score": subset2[subset2.idxmax()],
    }

def get_indecision(df_comments: pd.DataFrame) -> dict[str, str]:
    """User that asked the most questions.

    A question is defined as a string containing at least
    one alphanumerical character and ending with at least one question mark.
    """
    subset = df_comments
    # delete all non-question marks characters and remove repeated question marks
    subset["body2"] = (
        subset["body"]
        .str.replace(
            "[{}]".format(string.punctuation.replace("?", "")), ".", regex=True
        )
        .str.replace(r"\?+", "?", regex=True)
        .str.replace(r"\.+", ".", regex=True)
    )
    subset["question"] = subset["body2"].apply(
        lambda row: len(
            [
                x
                for x in row.rsplit("?", 1)[0].split("?")
                if (x and bool(re.search(r"\w", x)))
            ]
        )
        if "?" in row
        else 0
    )
    subset2 = subset.groupby("author").sum()["question"]
    return {
        "indecision_author": str(subset2.idxmax()),
        "indecision_score": subset2[subset2.idxmax()],
    }

def get_jackpot(df_comments: pd.DataFrame) -> dict[str, str]:
    """User that gained the most karma."""
    subset = df_comments.groupby(["author"]).sum()["score"]
    return {
        "jackpot_author": str(subset.idxmax()),
        "jackpot_score": subset[subset.idxmax()],
    }

def get_krach(df_comments: pd.DataFrame) -> dict[str, str]:
    """User that lost the most karma."""
    subset = df_comments.groupby(["author"]).sum()["score"]
    return {
        "krach_author": str(subset.idxmin()),
        "krach_score": subset[subset.idxmin()],
    }


#!/usr/bin/python3
""" Return top ten titles """
import requests

def top_ten(subreddit):
    """ Query Reddit API to return first 10 hot posts """
    header = {'User-Agent': 'my_user_agent'}
    url = "https://www.reddit.com/r/{}/hot.json?limit=10".format(subreddit)
    try:
        response = requests.get(
            url, headers=header, allow_redirects=False).json().get("data")
        for children in response.get("children"):
            print(children.get("data").get("title"))
    except Exception:
        print(None)


from sklearn import tree
from sklearn.neighbors import NearestNeighbors
from sklearn.neighbors.nearest_centroid import NearestCentroid
import pickle
import numpy as np
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt

features = [[20,11],[20,5],[20,12],[17,7],[16,7],[18,7],[19,7],[20,4],[20,9],[20,10]]
r_fea = [[a[1],a[0] ]for a in features]
#labels = [[0],[1],[1],[0],[1],[0],[1],[0],[0],[1]]
labels = [0,1,1,0,1,0,1,0,0,1]
r_lab = [(a-1)*(a-1) for a in labels]
X = np.array(features+r_fea)
y = np.array(labels + r_lab)
clf = NearestCentroid()
clf.fit(X, y)
print(clf.centroids_)
print(clf.score(X,y))
print(clf.predict([[20, 7]]))

print(clf.predict([[7, 20]]))

list_pickle = open('lr.pkl', 'wb')
pickle.dump(clf, list_pickle)

cmap_light = ListedColormap(['#FFAAAA',  '#AAAAFF'])
h = .02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.figure()
plt.pcolormesh(xx, yy, Z, cmap=cmap_light)
loss = X[np.where(y == 0)]
win = X[np.where(y == 1)]
plt.axis([0, 22, 0, 22])
plt.plot([a[0] for a in loss], [a[1] for a in loss] , "o")
plt.plot([a[0] for a in win], [a[1] for a in win] , ".")
#plt.plot([a[0] for a in clf.centroids_], [a[1] for a in clf.centroids_], "^")
plt.plot([20], [7], "^")
plt.show()


#coding=utf-8
import csv

class CsvHandler(object):
    def __init__(self):
        self.path = './result/csv/master_directory.csv'
        self.name = 'csvHandler'

    #  csv中写入数据
    def import_data(self, item, path=None):
        if path is None:
            path = self.path
        file = open(path, 'a+', encoding='utf-8')
        writer = csv.writer(file)
        writer.writerow(
            (item["University"],item["ExamType"],item["College"],item["Major"],item["ResearchInterests"],item["StudyType"],item["Teacher"],item["StudentNo"],item["Subject1"],item["Subject2"],item["Subject3"],item["Subject4"],item["Content"],item["Link"]))
        return item

if __name__=='__main__':
    test = CsvHandler()
    test.import_data({'University':'nj','Subject1':'jj'}, 'a.csv')
    test.import_data({'University':'bj','Subject1':'wl'}, 'a.csv')
    test.import_data({'University':'qh','Subject1':'zz'}, 'a.csv')


from django.urls import path
from django.views.generic import TemplateView

from main import views

app_name = 'main'
urlpatterns = [
    path("", views.home, name='home'),
    path("about-us/", views.about_us, name='about_us'),
    path("contact-us/", views.ContactUsView.as_view(), name="contact_us"),
    path("team/", views.team, name="team"),
    path("case-studies/", views.case_studies, name="case_studies"),
    path("careers/", views.careers, name="careers"),
    path("events/", views.events, name="events"),
]


from error_functions import ErrorFunction
from learning_rate_decay_techniques import LearningRate
from metrics import Metric
from regularizations import Regularizations

import numpy as np
import tqdm as tqdm
import math

class Training:

    def __init__(self, net, error_func, metr, lr, momentum, reg_type, lambda_, nesterov = True, lr_decay=None, limit_step=None, decay_rate=None, decay_steps=None):
        self._net = net
        self._error_func = ErrorFunction.init_error_function(error_func) # tuple composed by (error_function, error_function_der, name)
        self._metric_name = metr
        self._metric = Metric.init_metric(metr)
        self.curr_lr = lr
        self.init_lr = self.curr_lr # self.init_lr is lr_0
        self.fixed_lr = self.init_lr / 100.0 # self.fixed_lr is lr_tau (~ 1% of lr_0)
        self.lr_decay = None
        if lr_decay is not None:
            self.lr_decay = LearningRate.init_decay_technique(lr_decay) # decay function for learning rate
        self.tau_step = limit_step
        self.decay_rate = decay_rate
        self.decay_steps = decay_steps
        self.momentum = momentum
        self.nesterov = nesterov
        self.lambda_ = lambda_
        self.reg = Regularizations.init_regularization(reg_type) # tuple composed by (reg_function, reg_function_der, name)

    @property
    def lr_params(self):
        """ Returns all the parameters related to the learning rate. Used for the decay """
        return {'init_lr': self.init_lr, 'fixed_lr': self.fixed_lr, 'lr_decay': self.lr_decay,
                'tau_step': self.tau_step, 'decay_rate': self.decay_rate, 'decay_steps': self.decay_steps,'curr_lr': self.curr_lr}

    @property
    def net(self):
        return self._net

    @property
    def error_func(self):
        return self._error_func

    @property
    def metr(self):
        return self._metric

    def gradient_descent(self, tr_x, tr_y, val_x, val_y, epochs, batch_size, strip_early_stopping, baseline_early_stopping, disable_tqdm=True, **kwargs):
        """
        Gradient descent algorithm for training the network

        Args:
            tr_x (np.ndarray): input training set
            tr_y (np.ndarray): targets for each input training pattern
            val_x (np.ndarray): input validation set
            val_y (np.ndarray): targets for each input validation pattern
            epochs (int): number of training epochs
            batch_size (int): number of patterns per single batch
            strip_early_stopping (int): maximum number of consecutive epochs in which the validation error increases (0 means no early stopping)
            baseline_early_stopping (dict): dict with number of epoch (first element) to check if error is below a certain threshold (second element)
            disable_tqdm (bool, optional): to disable progress bar. Defaults to True.

        Returns:
            tuple of lists: return training error (for loss), training metric error, validation error and validation error metric for each epoch
        """

        # early stopping
        if strip_early_stopping > 0 and val_x is not None:
            counter = 0
            val_err = math.inf

        # initialize some variables
        tr_error_values, tr_metric_values, val_error_values, val_metric_values = [], [], [], []
        net = self.net  # just to be shorter
        momentum_net = net.get_empty_struct()
        step = 0

        # cycle through epochs
        for epoch in tqdm.tqdm(range(epochs), desc="Iterating over epochs", disable=disable_tqdm):

            # np.ndarray related to the number of neurons of the last layer of the network (1 for Monk and 2 for ML-CUP)
            epoch_tr_error = np.zeros(net.layers[-1].n_units)
            epoch_tr_metric = np.zeros(net.layers[-1].n_units)

            # shuffle the training set if using mini-batches (or stochastic)
            if batch_size <= tr_x.shape[0]:
                indexes = list(range(len(tr_x)))
                np.random.shuffle(indexes)
                tr_x = tr_x[indexes]
                tr_y = tr_y[indexes]
            # cycle through batches
            for batch_index in range(math.ceil(len(tr_x) / batch_size)):
                start = batch_index * batch_size
                end = start + batch_size
                train_batch = tr_x[start: end]
                targets_batch = tr_y[start: end]
                # gradient is saved in a matricial form
                grad_net = net.get_empty_struct()

                # Nesterov Momentum
                if (self.nesterov == True):
                    # nesterov momentum (interim points)
                    for layer_index in range(len(net.layers)):
                        # alpha * delta_w_old
                        momentum_net[layer_index]['weights'] *= self.momentum
                        momentum_net[layer_index]['biases'] *= self.momentum
                        # w_new1 = w_old + alpha *  delta_w_old
                        net.layers[layer_index].weights = np.add(net.layers[layer_index].weights, momentum_net[layer_index]['weights'])
                        net.layers[layer_index].biases = np.add(net.layers[layer_index].biases,momentum_net[layer_index]['biases'])

                # cycle through patterns and targets within a batch and accumulate the gradients
                for pattern, target in zip(train_batch, targets_batch):
                    net_outputs = net.forward(inp=pattern)

                    epoch_tr_error = np.add(epoch_tr_error, self.error_func[0](prediction=net_outputs, target=target))
                    epoch_tr_metric = np.add(epoch_tr_metric, self.metr(prediction=net_outputs, target=target))

                    # set the layers' gradients and add them into grad_net
                    # (emulate pass by reference of grad_net using return and reassign)
                    # compute the layers gradients in a "matrix" way
                    dErr_dOut = self.error_func[1](prediction=net_outputs, target=target)
                    grad_net = net.backprop(dErr_dOut, grad_net)

                # learning rate decays
                if self.lr_decay is not None:
                    step += 1
                    self.curr_lr = self.lr_decay(curr_step=step, **self.lr_params)

                # weights update
                for layer_index in range(len(net.layers)):
                    # grad_net contains the gradients of all the layers (and units) in the network
                    grad_net[layer_index]['weights'] /= batch_size
                    grad_net[layer_index]['biases'] /= batch_size
                    # delta_w is equivalent to lrn_rate * local_grad * input_on_that_connection (local_grad = delta)
                    delta_w = self.curr_lr * grad_net[layer_index]['weights'] # first step
                    delta_b = self.curr_lr * grad_net[layer_index]['biases']

                    if (self.nesterov == True):
                        momentum_net[layer_index]['weights'] = np.add(momentum_net[layer_index]['weights'], delta_w)
                        momentum_net[layer_index]['biases'] = np.add(momentum_net[layer_index]['biases'], delta_b)
                        net.layers[layer_index].weights = np.subtract(
                            np.add(net.layers[layer_index].weights, delta_w),
                            self.reg[1](w=net.layers[layer_index].weights, lambda_=self.lambda_),
                        )
                        net.layers[layer_index].biases = np.add(
                            net.layers[layer_index].biases,
                            delta_b
                        )
                    else:
                        # momentum_net[layer_index]['weights'] is the new delta_w --> it adds the momentum
                        # Since it acts as delta_w, it multiplies itself by the momentum constant and then adds
                        # lrn_rate * local_grad * input_on_that_connection (i.e. "delta_w")
                        momentum_net[layer_index]['weights'] *= self.momentum # second step
                        momentum_net[layer_index]['biases'] *= self.momentum
                        momentum_net[layer_index]['weights'] = np.add(momentum_net[layer_index]['weights'], delta_w)
                        momentum_net[layer_index]['biases'] = np.add(momentum_net[layer_index]['biases'], delta_b)
                        # keep separation of the 3 hyperparameters
                        # third step
                        net.layers[layer_index].weights = np.subtract(
                            np.add(net.layers[layer_index].weights, momentum_net[layer_index]['weights']),
                            self.reg[1](w=net.layers[layer_index].weights, lambda_=self.lambda_),
                        )
                        net.layers[layer_index].biases = np.add(
                            net.layers[layer_index].biases,
                            momentum_net[layer_index]['biases']
                        )

            # validation (maybe we just want to train our network)
            if val_x is not None:
                epoch_val_error, epoch_val_metric = net.evaluate(inp=val_x, targets=val_y, metr=self._metric_name, error_func=self._error_func[2])
                val_error_values.append(epoch_val_error)
                val_metric_values.append(epoch_val_metric)
            epoch_tr_error = np.sum(epoch_tr_error) / len(epoch_tr_error)
            tr_error_values.append(epoch_tr_error / len(tr_x))
            epoch_tr_metric = np.sum(epoch_tr_metric) / len(epoch_tr_metric)
            tr_metric_values.append(epoch_tr_metric / len(tr_x))

            # early stopping for training
            if strip_early_stopping > 0 and val_x is not None:
                if epoch_val_error < val_err:
                    counter = 0
                    val_err = epoch_val_error
                else:
                    counter+=1
                if counter >= strip_early_stopping:
                    break

            # early stopping for fine tuning validation
            if baseline_early_stopping is not None and val_x is not None:
                if math.isnan(epoch_val_error):
                    raise Exception("Validation error at epoch {} is NaN".format(epoch))
                if epoch == baseline_early_stopping['epoch'] and (epoch_val_error > baseline_early_stopping['threshold'] or math.isnan(epoch_val_error)):
                    raise Exception("Validation error at epoch {} is above threshold {}".format(epoch,baseline_early_stopping['threshold']))
        return tr_error_values, tr_metric_values, val_error_values, val_metric_values

    def print_training_parameters(self):
        """ Prints the training parameters """
        print("---------------- TRAINING PARAMETERS ----------------")
        print("Error Function: {}".format(self.error_func))
        print("Metric: {}".format(self.metr))
        print("Learning Rate Parameters: {}".format(self.lr_params))
        print("Momentum: {}".format(self.momentum))
        print("Nesterov: {}".format(self.nesterov))
        print("Lambda Regularization Rate: {}".format(self.lambda_))
        print("Regularization: {}".format(self.reg))


import scipy
from scipy.io import wavfile
from scipy import signal
import numpy as np
import matplotlib.pyplot as plt

# read wav files
rateLL, f = wavfile.read('audio/iphone_recording/6_left/LL.wav', mmap=True)
rateLU, g = wavfile.read('audio/iphone_recording/6_left/LU.wav', mmap=True)

print('Rate LL: %s' % rateLL)
print('Rate LU: %s' % rateLU)

x = np.arange(0, f.shape[0])

cross = signal.correlate(f, g, mode='full')

pltCount = 3

plt.subplot(pltCount, 1, 1)
plt.title('Lower Left')
plt.plot(x, f)

plt.subplot(pltCount, 1, 2)
plt.title('Upper Left')
plt.plot(x, g)

plt.subplot(pltCount, 1, 3)
plt.title('Cross Correlation')
plt.plot(cross)

plt.show()


from azure.storage.blob import BlockBlobService
import pymongo as py
import os

def create_file_metadata(f, c):
    """Produce file metadata.

    Convert blob store file to metadata dictionary
    Input:
        f: blob filename
        c: container name
    Output:
        d: metadata dictionary
    """
    folder, filename = f.split('/')
    name, filetype = filename.split('.')
    cn, ty, dt = name.split('_')
    d = {
            'azure_folder': folder, 'filename': filename, 'name': name,
            'filetype': filetype, 'company_number': cn, 'doctype':ty,
            'submit_date': dt, 'blob_file': f, 'container': c
        }
    return d

class AzureBlob():
    """Wrapper around blob object."""
    def __init__(self, name, key):
        self.blob = BlockBlobService(
            account_name = name,
            account_key = key
        )
        self.containers = self.blob.list_containers()

    def container_files(self, c):
        return self.blob.list_blobs(c.name)

def main():
    client = py.MongoClient(os.environ['MONGO_URI'])
    db = client.ch
    docs = db.docs

    included_folders = [
        'administratorsprogress',
        'companieshouseapi',
        'liquidatorsstatement'
        ]

    ch = AzureBlob(os.environ['BLOB_NAME'], os.environ['BLOB_KEY'])

    for container in ch.containers:
        if container.name in included_folders:
            for fl in ch.container_files(container):
                docs.insert_one(create_file_metadata(fl.name, container.name))

if __name__ == "__main__":
    main()


import re

name = input("Enter file:")
if len(name) < 1:
    name = "regex_sum.txt"
handle = open(name)

digits = list()
for line in handle:
    x = re.findall("[0-9]+", line)
    if len(x) < 1:
        continue
    else:
        for i in x:
            digits.append(int(i))
total = 0
for d in digits:
    total += d
print(total)