class  binaryTree:
    def __init__(self,size):
        self.customList = size *[None]
        self.lastUsedIndex = 0
        self.maxSize = size

    def insertNode(self,value):
        if self.lastUsedIndex +1 == self.maxSize:
            return "The Binary  tree is full"
        self.customList[self.lastUsedIndex +1] = value
        self.lastUsedIndex +=1
        return "The Value has been successfully inserted"

    def searchNode(self,value):
        for i in range(len(self.customList)):
            if self.customList[i] == value:
                return 'Success'
        return 'Not Found'

    def preOrderTraversal(self, index):
        if index > self.lastUsedIndex:
            return
        print(self.customList[index])
        self.preOrderTraversal(index*2)
        self.preOrderTraversal(index*2 + 1)

    def inOrderTraversal(self, index):
        if index > self.lastUsedIndex:
            return
        self.inOrderTraversal(index*2)
        print(self.customList[index])
        self.inOrderTraversal(index*2+1)

    def postOrderTraversal(self, index):
        if index > self.lastUsedIndex:
            return
        self.postOrderTraversal(index*2)
        self.postOrderTraversal(index*2+1)
        print(self.customList[index])

    def levelOrderTraversal(self, index):
        for i in range(index, self.lastUsedIndex+1):
            print(self.customList[i])

    def deleteNode(self,value):
        if self.lastUsedIndex  == 0:
            return 'There is no element in Binary tree to delete'
        else:
            for i in range(1,self.lastUsedIndex +1):
                if self.customList[i] == value:
                    self.customList[i] = self.customList[self.lastUsedIndex]
                    self.customList[self.lastUsedIndex]
                    return "The Node has been successfully deleted"

    def delete(self):
        self.customList = None
        return "Binary has been deleted"

newBt = binaryTree(8)

print('--------Binary Tree using python list ------')
print('--------Inserting ------')
print(newBt.insertNode("Drinks"))
print(newBt.insertNode("Hot"))
print(newBt.insertNode("Cold"))

print('--------Search node ------')
print(newBt.searchNode("Hot"))

print('--------PreOrder Traversal ------')
newBt.preOrderTraversal(1)

print('--------InOrder Traversal ------')
newBt.inOrderTraversal(1)

print('--------postOrder Traversal ------')
newBt.postOrderTraversal(1)

print('--------LevelOrder Traversal ------')
newBt.levelOrderTraversal(1)

print('--------Delete Node ------')
print(newBt.deleteNode('Hot'))

print('--------Delete Binary Tree ------')
print(newBt.delete())


# copy, sorted, produtos.sort
# Exercícios
# Aumente os preços dos produtos a seguir em 10%
# Gere novos_produtos por deep copy (cópia profunda)
produtos = [
    {'nome': 'Produto 5', 'preco': 10.00},
    {'nome': 'Produto 1', 'preco': 22.32},
    {'nome': 'Produto 3', 'preco': 10.11},
    {'nome': 'Produto 2', 'preco': 105.87},
    {'nome': 'Produto 4', 'preco': 69.90},
]

# Ordene os produtos por nome decrescente (do maior para menor)
# Gere produtos_ordenados_por_nome por deep copy (cópia profunda)

# Ordene os produtos por preco crescente (do menor para maior)
# Gere produtos_ordenados_por_preco por deep copy (cópia profunda)

print(produtos[0]['preco'])

for i in range(len(produtos)):

    novo_valor = round((float(produtos[i]['preco'])*1.10),2)
    produtos[i].update(preco=novo_valor)

    # produtos[i].values()

# for i in range(len(produtos)):
    # print(produtos[i])

def ordena_nome(item):
    return item['nome']

def ordena_preco(item):
    return item['preco']

produtos.sort(key=ordena_nome, reverse=True)
# print(produtos)

import copy

produtos_ordenados_nome = copy.deepcopy(produtos)

produtos.sort(key=ordena_preco)
# print(produtos)

produtos_ordenados_preco = copy.deepcopy(produtos)

for i in range(len(produtos)):
    print(produtos_ordenados_nome[i])

print()
print('--------------------------')
print()

for i in range(len(produtos)):
    print(produtos_ordenados_preco[i])


import os
import nltk
from nltk import sent_tokenize
from nltk import word_tokenize
from nltk.probability import FreqDist
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np

# nltk.download('punkt')

# 1 - Importando o texto
with open(os.path.join('automacoes/PLN_IA/data', 'texto.txt', ), 'r', encoding='utf-8') as file:
    texto = file.read()
    # print(texto)

# 2 - Tokenizando o texto (separando por palavras, ou setença (ponto final))
sent_tokens = sent_tokenize(texto)
print(sent_tokens)
print(len(sent_tokens))

word_tokens = word_tokenize(texto)
print(word_tokens)
print(len(word_tokens))

# 3 - Frequencia de distribuição
fdist = FreqDist(word_tokens)
print(fdist)
print(fdist.most_common(10))
fdist.plot(10)

# 4 - WordCloud / WordCloud Customizado
def plot_cloud(wordcloud):
    plt.figure(figsize=(40,30))
    plt.imshow(wordcloud)
    plt.axis('off')
    plt.show()

wordcloud = WordCloud(
    width = 3000,
    height = 2000,
    random_state = 1,
    background_color= 'salmon',
    colormap='Pastel1',
    collocations=False,
    stopwords = STOPWORDS
).generate(texto)

plot_cloud(wordcloud)

mask = np.array(
    Image.open('automacoes/PLN_IA/data/upvote.png')
)

print(mask)
wordcloud = WordCloud(
    width = 3000,
    height = 2000,
    random_state = 1,
    background_color= 'salmon',
    colormap='Pastel1',
    collocations=False,
    stopwords = STOPWORDS,
    mask = mask
).generate(texto)

# plot_cloud(wordcloud)


import os
import re
from nonebot import on_command,on_message,on_keyword
from nonebot.rule import to_me
from nonebot.typing import T_State
from nonebot.adapters import Bot, Event, Message

weather = on_command("天气", priority=1)
path = 're\msyh.ttc'
file = open('re\citycid.txt','r',encoding='utf-8')
cityids = file.readlines()
cityids = str(cityids)
@weather.handle()
async def handle_first_receive(bot: Bot, event: Event, state: T_State):
    args = str(event.get_message()).strip()  # 首次发送命令时跟随的参数，例：/天气 上海，则args为上海
    if args:
        state["city"] = args  # 如果用户发送了参数则直接赋值

@weather.got("city", prompt="你想查询哪个城市的天气呢？")
async def handle_city(bot: Bot, event: Event, state: T_State):
    gid = event.group_id
    city = state["city"]
    city_weather = await weathercn(await findcityid(city))
    msg = '[CQ:image,file=file:///C:\\Users\德兰李维奇\.cache\weatherCN\weather.png]'
    await bot.send_group_msg(group_id=gid,message=msg)

async def findcityid(cityname):
    cityid = re.findall("(\d{9})=%s" % cityname, cityids)[0]
    return cityid

async def weathercn(cityid):
    str = 'python -m weathercn %s D:\KSBot\weather\pic\msyh.ttc'%cityid
    os.system(str)
    print("打印成功!")


"""main URL Configuration

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', IndexView.as_view(), name='index'),
    path('product/<int:pk>/', ProductView.as_view(), name='product_view'),
    path('product/<int:pk>/update/', ProductUpdateView.as_view(), name='product_update'),
    path('product/create/', ProductCreateView.as_view(), name='product_create'),
    path('product/<int:pk>/delete/', ProductDeleteView.as_view(), name='product_delete'),
    path('product/category/<int:pk>/', CategoryView.as_view(), name='product_category'),

    path('basket/product/<int:pk>/', BasketCountView.as_view(), name='basket_count'),
    path('basket/', BasketOrderView.as_view(), name='basket_view'),
    path('basket/<int:pk>/', BasketDeleteView.as_view(), name='basket_delete')
]


class Solution:
    def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
        ans = []
        compare = x if (x < y or y == 1) and x != 1 else y
        max_power = 0
        while compare**max_power < bound:
            if x == 1 and y == 1:
                compare = 2
            max_power += 1
        for i in range(max_power):
            for j in range(max_power):
                v = x**i + y**j
                if v <= bound and not v in ans:
                    ans.append(v)
        return ans


# Laod all the libraries
from argparse import ArgumentParser

def read(path, filename):
    # read file
    with open(path + filename, 'r') as f:
        score = [line.rstrip('\n') for line in f]
        print(score)

if __name__ == "__main__":
    # Read the filepath for csv files from command line
    parser = ArgumentParser()
    # First input path where files are stored
    parser.add_argument("-p",
                        "--path",
                        help="input the filepath",
                        type = str)
    # Second input filename
    parser.add_argument("-f",
                        "--file",
                        help="input the filename",
                        type = str)
    args = parser.parse_args()
    path = args.path
    filename = args.file
    # Execute main
    read(path, filename)


import cv2
import randomname
import datetime
import time
from VideoGet import VideoGet
from VideoShow import VideoShow
from RgbTest import RgbTest

class LifeCycle:
    def __init__(self):
        self.first_run = True
        self.name = ""
        self.born_dt = None
        self.born_text = ""
        self.death_text = ""
        self.age = ""
        self.dead = False

    def delay(self):
        time.sleep(5)

    def born(self):
        self.dead = False
        self.death_text = ""
        self.age = ""
        self.name = randomname.get_name()
        self.name = self.name.replace("-", " ")
        self.name = self.name.title()
        self.born_dt = datetime.datetime.now()
        self.born_text =  self.name + " Born " + self.born_dt.strftime("%d.%m.%Y At %H:%M:%S")

    def death(self):
        self.dead = True
        self.age = datetime.datetime.now() - self.born_dt
        minutes = divmod(self.age.seconds, 60)
        self.death_text = "Died " + datetime.datetime.now().strftime("%d.%m.%Y At %H:%M:%S")
        if minutes[0] == 0:
            self.age ="Aged " + str(minutes[1]) + " seconds"
        elif minutes[0] == 1:
            self.age = "Aged " + str(minutes[0]) + " minute " + str(minutes[1]) + " seconds"
        else:
            self.age = "Aged " + str(minutes[0]) + " minutes " + str(minutes[1]) +" seconds"

def roi_test(roi_test, bubble):
    if roi_test:
        #print("Do servo stuff here")
        if not bubble.first_run:
            bubble.first_run = True
            bubble.death()
            return bubble
        return bubble
    else:
        #print("Bubble still visible")
        if bubble.first_run:
            bubble.first_run = False
            bubble.born()
            return bubble
        return bubble

def draw_text(frame, bubble_life):
    cv2.putText(frame, bubble_life.born_text,
                (10, 30), cv2.FONT_HERSHEY_DUPLEX, 1.0, (255, 255, 255))
    cv2.putText(frame, bubble_life.death_text,
                (10, 60), cv2.FONT_HERSHEY_DUPLEX, 1.0, (255, 255, 255))
    cv2.putText(frame, bubble_life.age,
                (10, 90), cv2.FONT_HERSHEY_DUPLEX, 1.0, (255, 255, 255))

def thread_handler(source=0):
    video_getter = VideoGet(source).start()
    video_shower = VideoShow(video_getter.frame).start()
    rgb_test = RgbTest(video_getter.frame).start()
    bubble_life = LifeCycle()

    while True:
        if video_getter.stopped or video_shower.stopped:
            video_shower.stop()
            video_getter.stop()
            rgb_test.stop()
            break

        frame = video_getter.frame
        rgb_test.frame = frame
        bubble_life = roi_test(rgb_test.roi_test, bubble_life)
        draw_text(frame, bubble_life)
        if bubble_life.dead:
            bubble_life.delay()
        video_shower.frame = frame

def main():
    thread_handler(0)

if __name__ == "__main__":
    main()


# coding: utf-8

from __future__ import absolute_import

from swagger_server.models.error import Error
from swagger_server.models.generic_object import GenericObject
from swagger_server.models.uid_info import UidInfo
from . import BaseTestCase
from six import BytesIO
from flask import json

class TestUidController(BaseTestCase):
    """ UidController integration test stubs """

    def test_root_get(self):
        """
        Test case for root_get

        Returns the current versions information
        """
        response = self.client.open('/v1/',
                                    method='GET')
        self.assert200(response, "Response body is : " + response.data.decode('utf-8'))

    def test_root_post(self):
        """
        Test case for root_post

        creates a new uid
        """
        response = self.client.open('/v1/',
                                    method='POST')
        self.assert200(response, "Response body is : " + response.data.decode('utf-8'))

    def test_uid_delete(self):
        """
        Test case for uid_delete

        delete a uid
        """
        response = self.client.open('/v1/{uid}'.format(uid=789),
                                    method='DELETE',
                                    content_type='application/json')
        self.assert200(response, "Response body is : " + response.data.decode('utf-8'))

    def test_uid_get(self):
        """
        Test case for uid_get

        get the particular uid for the version
        """
        response = self.client.open('/v1/{uid}'.format(uid=789),
                                    method='GET',
                                    content_type='application/json')
        self.assert200(response, "Response body is : " + response.data.decode('utf-8'))

    def test_uid_post(self):
        """
        Test case for uid_post

        update an existing uid
        """
        body = GenericObject()
        response = self.client.open('/v1/{uid}'.format(uid=789),
                                    method='POST',
                                    data=json.dumps(body),
                                    content_type='application/json')
        self.assert200(response, "Response body is : " + response.data.decode('utf-8'))

    def test_uid_put(self):
        """
        Test case for uid_put

        update an existing uid
        """
        body = GenericObject()
        response = self.client.open('/v1/{uid}'.format(uid=789),
                                    method='PUT',
                                    data=json.dumps(body),
                                    content_type='application/json')
        self.assert200(response, "Response body is : " + response.data.decode('utf-8'))

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


import json
import os
import copy

from .ApiBase import ApiBase
from package.mylib import mylib

class admin(ApiBase):
    '''
    后台管理相关
    '''

    def __init__(self, handler):
        super().__init__(handler)

        self.pluginpath = r'./plugin'
        self.default_admin = []
        admin_path = self.config['ADMIN']['config']
        with open(admin_path) as f:
            self.default_admin = json.load(f)
        del admin_path

    # 加载插件配置插件，所有插件只能有一个插件可以配置后台管理
    def __load_plugin_menu(self):
        admin_mneu = []

        for filedir in os.listdir(self.pluginpath):
            if os.path.isdir(os.path.join(self.pluginpath, filedir)) and filedir != '__pycache__':
                json_path = os.path.join(self.pluginpath, filedir, 'adminconfig.json')
                if os.path.isfile(json_path):
                    with open(json_path, 'r') as f:
                        admin_mneu = json.load(f)
                        return admin_mneu
        return admin_mneu

    # 加载后台管理菜单
    def __load_admin_menu(self):
        # admin_mneu = self.__load_plugin_menu()
        # if len(admin_mneu)>0:
        #     self.default_admin = admin_mneu

        return json.dumps(self.default_admin)

    def main(self):
        # 获取后台管理菜单
        if self.query['get']=='menu':
            return self.__load_admin_menu()


from helpers.General import add_json_key

class ChatGPTClient:
    def __init__ (self, **kwargs) -> None:
        identity = kwargs.get('identity', None)
        self.users: dict = {}
        self.user_ids: list = []
        self.identity: str = identity

    @classmethod
    async def create(cls, **kwargs) -> 'ChatGPTClient':
        server_users: dict = kwargs.get('server_users', {})
        client_users: list = kwargs.get('client_users', [])
        client_id: str = kwargs.get('client_id', None)
        self = cls(identity = client_id)
        await self.__load_client_users(client_users = client_users, server_users = server_users)
        return self

    async def __load_client_users(self, **kwargs) -> None:

        client_users = kwargs.get('client_users', [])
        server_users = kwargs.get('server_users', {})

        if len(client_users) == 0:
            return
        else:
            self.users = {}
            for user in client_users:
                self.users[user] = server_users[user]
                self.user_ids.append(server_users[user]['user_id'])


import numpy as np
from sklearn.decomposition import PCA

def get_fg_bg_epochs(rec):
    """
    Get fg / bg epoch names
    """

    fgs = np.unique([e.split('STIM_null_')[1] for e in rec.epochs.name.unique() if e.startswith('STIM_null_')]).tolist()
    bgs = np.unique([e.split('_null')[0].split('STIM_')[1] for e in rec.epochs.name.unique() if e.endswith('_null')]).tolist()

    return fgs, bgs

def get_fg_bg_transitions(rec):
    """
    get epochs that transitioned from only fg to fg + bg
    """
    trans_epochs = [e for e in rec.epochs.name.unique() if ('null' not in e) & ('STIM_' in e)]
    fgs, bgs = get_fg_bg_epochs(rec)
    lfgs = [f for f in fgs if '-0-1' in f]
    sbgs = [b for b in bgs if '-0.5-1' in b]
    fg_bg = [e for e in trans_epochs if any(ele in e for ele in lfgs) & any(ele in e for ele in sbgs)]
    return fg_bg

def get_bg_fg_transitions(rec):
    """
    get epochs that transitioned from only bg to fg + bg
    """
    trans_epochs = [e for e in rec.epochs.name.unique() if ('null' not in e) & ('STIM_' in e)]
    fgs, bgs = get_fg_bg_epochs(rec)
    sfgs = [f for f in fgs if '-0.5-1' in f]
    lbgs = [b for b in bgs if '-0-1' in b]
    bg_fg = [e for e in trans_epochs if any(ele in e for ele in sfgs) & any(ele in e for ele in lbgs)]
    return bg_fg

def get_fg_bg_full_overlap(rec):
    """
    get epochs that overlapped for the full period
    """
    trans_epochs = [e for e in rec.epochs.name.unique() if ('null' not in e) & ('STIM_' in e)]
    fgs, bgs = get_fg_bg_epochs(rec)
    lfgs = [f for f in fgs if '-0-1' in f]
    lbgs = [b for b in bgs if '-0-1' in b]
    bg_fg = [e for e in trans_epochs if any(ele in e for ele in lfgs) & any(ele in e for ele in lbgs)]
    return bg_fg

def get_fg_pcs(rec, collapse=False):
    """
    Get top pc axes of FG evoked responses
    """
    prestim = rec['resp'].extract_epoch('PreStimSilence').shape[-1]
    poststim = rec['resp'].extract_epoch('PostStimSilence').shape[-1]
    nCells = rec['resp'].shape[0]

    fgs, bgs = get_fg_bg_epochs(rec)
    fgs1 = [f for f in fgs if f.endswith('0-1')]
    if collapse:
        fg_resp = np.zeros((len(fgs1), nCells))
    else:
        fg_resp = []
    for i, fg in enumerate(fgs1):
        if collapse:
            resp = rec['resp'].extract_epoch('STIM_null_'+fg)[:, :, prestim:-poststim].sum(axis=-1).mean(axis=0)
            prespont = rec['resp'].extract_epoch('STIM_null_'+fg)[:, :, 0:prestim].sum(axis=-1).mean(axis=0)
            fg_resp[i, :] = resp - prespont
        else:
            resp = rec['resp'].extract_epoch('STIM_null_'+fg)[:, :, prestim:-poststim].mean(axis=0)
            prespont = rec['resp'].extract_epoch('STIM_null_'+fg)[:, :, 0:prestim].mean(axis=(0, -1))
            fg_resp.append((resp.T - prespont))

    if type(fg_resp) == list:
        fg_resp = np.concatenate(fg_resp, axis=0)
    fg_pca = PCA()
    fg_pca.fit(fg_resp)
    return fg_pca.components_

def get_bg_pcs(rec, collapse=False):
    """
    Get top pc axes of BG evoked responses
    """
    prestim = rec['resp'].extract_epoch('PreStimSilence').shape[-1]
    poststim = rec['resp'].extract_epoch('PostStimSilence').shape[-1]
    nCells = rec['resp'].shape[0]

    fgs, bgs = get_fg_bg_epochs(rec)
    bgs1 = [b for b in bgs if b.endswith('0-1')]
    if collapse:
        bg_resp = np.zeros((len(bgs1), nCells))
    else:
        bg_resp = []
    for i, bg in enumerate(bgs1):
        if collapse:
            resp = rec['resp'].extract_epoch('STIM_' + bg + '_null')[:, :, prestim:-poststim].sum(axis=-1).mean(axis=0)
            prespont = rec['resp'].extract_epoch('STIM_' + bg + '_null')[:, :, 0:prestim].sum(axis=-1).mean(axis=0)
            bg_resp[i, :] = resp - prespont
        else:
            resp = rec['resp'].extract_epoch('STIM_' + bg + '_null')[:, :, prestim:-poststim].mean(axis=0)
            prespont = rec['resp'].extract_epoch('STIM_' + bg + '_null')[:, :, 0:prestim].mean(axis=(0, -1))
            bg_resp.append((resp.T - prespont))

    if type(bg_resp) == list:
        bg_resp = np.concatenate(bg_resp, axis=0)
    bg_pca = PCA()
    bg_pca.fit(bg_resp)
    return bg_pca.components_

def get_decoder(rec, fg_epoch, bg_epoch, collapse=False):
    """
    Get decoding axis for each time bin for the given epoch pair
    """

    prestim = rec['resp'].extract_epoch('PreStimSilence').shape[-1]
    poststim = rec['resp'].extract_epoch('PostStimSilence').shape[-1]
    tbins = rec['resp'].extract_epoch('REFERENCE').shape[-1]
    nCells = rec['resp'].shape[0]

    fg_resp = rec['resp'].extract_epoch('STIM_null_'+fg_epoch)
    bg_resp = rec['resp'].extract_epoch('STIM_'+bg_epoch+'_null')

    decoding_axes = np.zeros((tbins, nCells))
    if collapse:
        # single decoding axis, repped out for all tbins
        fr = fg_resp[:, :, prestim:-poststim].transpose([0, 2, 1]).reshape(-1, nCells)
        br = bg_resp[:, :, prestim:-poststim].transpose([0, 2, 1]).reshape(-1, nCells)
        axis = fr.mean(axis=0) - br.mean(axis=0)
        axis /= np.linalg.norm(axis)
        decoding_axes = np.tile(axis, [tbins, 1])
    else:
        # different decoding axis for each time point
        for t in range(fg_resp.shape[-1]):
            fr = fg_resp[:, :, t]
            br = bg_resp[:, :, t]
            axis = fr.mean(axis=0) - br.mean(axis=0)
            axis /= np.linalg.norm(axis)
            decoding_axes[t, :] = axis

    return decoding_axes


# I have created this file
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
    return HttpResponse('''
gopal jha is here

Learning 1

Learn boostrap

    ''')
def learning1(request):
    # return HttpResponse('''
gopal jha is here

hello everyone here
 gopajha
    #
vitality

    # ''');

     #render are use for trnasfer the data from main python to html file on other side we use {{}} this symbol
     #rendor have three parameter: 1. request 2. path 3. variable

    a={'name1':"gopal",'name2':"ravi"}
    return render(request,'index.html',a);

def removepunc(request):

    #get data from html page by using request.GET.get() function in python
    #post data from html page by using request.POST.get() function in python when ever we use post we have to write {% csrf_token %} on form section on html

    t1=request.POST.get('text','default')
    t2=request.POST.get('removepunch','off')
    t3=request.POST.get('upperCase','off')
    t4=request.POST.get('nlr','off')
    print(t1);
    out=t1
    if t2=="on":
        specil_key=["!","@","#","$","%","^","&","*","(",")","_","+","{","}",":","<",">",">","<","?","|",",",".",";","'","[","]","/","-","=",]
        result=""
        for i in t1:
            if i not in specil_key:
                result+=i;
        out=result;

    if(t3=="on"):
        out=out.upper()
    if(t4=="on"):
        result=""
        for i in out:
            if(i !="\n"):
                result+=i;
            else:
                break;
        out=result;
    if((t2=="off") and (t3=="off") and t4=="off"):
        test1={
        "word":t1,
        "result":"Select checkbox"
        }
        return render(request,'removePunch.html',test1)
    test1={
        "word":t1,
        "result":out
        }
    return render(request,'removePunch.html',test1)

def contact(request):
    return HttpResponse('''8745977703
    back

    ''');

#start boostrap
def boostrap(request):
    return render(request,'learn_boostrap.html');
# end boostrap


from flask import Flask, jsonify, send_file
from flask_cors import CORS

import db
from ubuntuarchive import get_all_package_lists, get_all_packages_in_list

app = Flask(__name__)
CORS(app)

# /search/<pkg_name>
# /package/<pkg_name>/<version*>/<architecture**>
# /install (json with package id list)
@app.route("/search/<pkg_name>", methods=["GET"])
def find_packages_by_name(pkg_name):
    try:
        return jsonify(db.find_packages_by_name(pkg_name)), 200
    except Exception as e:
        return jsonify({"errormsg": str(e)}), 404

@app.route("/package/<pkg_name>", methods=["GET"])
def get_packages_by_name(pkg_name):
    try:
        return jsonify(db.get_packages_by_name(pkg_name)), 200
    except Exception as e:
        return jsonify({"errormsg": str(e)}), 404

@app.route("/package/<pkg_name>/<pkg_version>", methods=["GET"])
def get_packages_by_name_version(pkg_name, pkg_version):
    try:
        return jsonify(db.get_packages_by_name_version(pkg_name, pkg_version)), 200
    except Exception as e:
        return jsonify({"errormsg": str(e)}), 404

@app.route("/package/<pkg_name>/<pkg_version>/<pkg_architecture>", methods=["GET"])
def get_package_by_name_version_arch(pkg_name, pkg_version, pkg_architecture):
    try:
        return jsonify(db.get_package_by_name_version_arch(pkg_name, pkg_version, pkg_architecture)), 200
    except Exception as e:
        return jsonify({"errormsg": str(e)}), 404

@app.route("/package_id/<pkg_id>", methods=["GET"])
def get_package_by_id(pkg_id):
    try:
        return jsonify(db.get_package_by_id(pkg_id)), 200
    except Exception as e:
        return jsonify({"errormsg": str(e)}), 404

@app.route("/package/<pkg_name>/<pkg_version>/<pkg_architecture>/download", methods=["GET"])
def download_package_by_name_version_arch(pkg_name, pkg_version, pkg_architecture):
    try:
        return send_file(
            db.download_package_by_name_version_arch(pkg_name, pkg_version, pkg_architecture),
            "application/vnd.debian.binary-package",
            as_attachment=True,
            attachment_filename="{}_{}_{}.deb".format(pkg_name, pkg_version, pkg_architecture)
        ), 200
    except Exception as e:
        return jsonify({"errormsg": str(e)}), 404

@app.route("/rebuild_db", methods=["GET"])
def rebuild_db():
    try:
        pkg_lists = get_all_package_lists()
        for pkg_list in pkg_lists:
            packages = get_all_packages_in_list(pkg_list)
            db.update_packages_database(packages)
    except Exception as e:
        return jsonify({"errormsg": str(e)}), 500
    return jsonify({}), 200

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5122)

# http://localhost:5000/search/nano
# http://localhost:5000/package/nano
# http://localhost:5000/package/nano/2.9.8-1
# http://localhost:5000/package/nano/2.9.8-1/amd64
# http://localhost:5000/package/nano/2.9.8-1/amd64/download
# http://localhost:5000/install


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

import os
import sys
import time
import tty, termios
import roslib
import rospy
from time import sleep
from geometry_msgs.msg import Twist
from std_msgs.msg import String

def tele_key():
    thread_stop = False
    cmd = Twist()
    # roslib.load_manifest('smartcar_teleop')
    pub = rospy.Publisher('/cmd_vel', Twist)
    rospy.init_node('key_rc_test_node')
    turn = 0
    sidewalk = 0
    speed = 0
    while (thread_stop == False):
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(fd)
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

        if ch == 'w':
            speed = speed + 0.5
        elif ch == 's':
            speed = speed - 0.5
        elif ch == 'a':
            turn = turn + 0.785
        elif ch == 'd':
            turn = turn - 0.785
        elif ch == 'q':
            sidewalk = sidewalk + 0.5
        elif ch == 'e':
            sidewalk = sidewalk - 0.5
        elif ch == 'p':
            turn = 0
            speed = 0
            thread_stop = True
        else:
            turn = 0
            sidewalk = 0
            speed = 0

        cmd.linear.x = speed
        cmd.linear.y = sidewalk
        cmd.angular.z = turn
        print('rc speed:', cmd.linear.x, cmd.linear.y, cmd.angular.z)
        pub.publish(cmd)
        time.sleep(0.05)
        pub.publish(cmd)
        time.sleep(0.05)
        pub.publish(cmd)
        time.sleep(0.05)
        pub.publish(cmd)
        time.sleep(0.05)

if __name__ == '__main__':
    tele_key()


import numpy as np
import csv
import time
import datetime

from sklearn import cross_validation
import xgboost as xgb

trainTestIDs = []
trainTestLabels = []
validationIDs = []
with open('stage1_labels.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        trainTestIDs.append(row['id'])
        trainTestLabels.append(row['cancer'])

with open('stage1_sample_submission.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        validationIDs.append(row['id'])

numGivenFeat=4096
numConcatFeats = numGivenFeat*3

def getFeatureData(id):
    fileName = '/home/zdestefa/data/segFilesResizedVGG19to4096/resnetFeats_' + id + '.npy'
    featData = np.load(fileName)
    outVec = np.zeros((1,numConcatFeats))
    outVec[0, 0:numGivenFeat] = np.mean(featData, axis=0)
    outVec[0, numGivenFeat:numGivenFeat * 2] = np.max(featData, axis=0)  # this causes potential overfit. should remove
    outVec[0, numGivenFeat * 2:numGivenFeat * 3] = np.min(featData, axis=0)  # this causes potential overfit. should remove
    return outVec

def train_xgboost():

    print('Train/Validation Data being obtained')
    #x = np.array([np.mean(getVolData(id), axis=0) for id in trainTestIDs.tolist()])

    x = np.zeros((len(trainTestIDs),numConcatFeats))
    ind = 0
    for id in trainTestIDs:
        x[ind,:] = getFeatureData(id)
        ind = ind+1
        #x.append(np.array([np.mean(featData,axis=0)]))
    print('Finished getting train/test data')
    #y = np_utils.to_categorical(trainTestLabels, 2)
    y = [float(lab) for lab in trainTestLabels]
    trn_x, val_x, trn_y, val_y = cross_validation.train_test_split(x, y, random_state=42, stratify=y,
                                                                   test_size=0.20)

    # clf = xgb.XGBRegressor(max_depth=30,
    #                        n_estimators=1500,
    #                        min_child_weight=9,
    #                        learning_rate=0.05,
    #                        nthread=8,
    #                        subsample=0.80,
    #                        colsample_bytree=0.80,
    #                        seed=4242)

    #change the max depth
    clf = xgb.XGBRegressor(max_depth=10,
                           n_estimators=1500,
                           min_child_weight=9,
                           learning_rate=0.05,
                           nthread=8,
                           subsample=0.80,
                           colsample_bytree=0.80,
                           seed=4242)

    clf.fit(trn_x, trn_y, eval_set=[(val_x, val_y)], verbose=True,
            eval_metric='logloss', early_stopping_rounds=100)
    return clf

def make_submit():
    clf = train_xgboost()

    print('Kaggle Test Data being obtained')
    x2 = np.zeros((len(validationIDs), numConcatFeats))
    ind = 0
    for id in validationIDs:
        x2[ind, :] = getFeatureData(id)
        ind = ind + 1
    print('Finished getting kaggle test data')

    pred = clf.predict(x2)

    ts = time.time()
    st = datetime.datetime.fromtimestamp(ts).strftime('%Y_%m_%d__%H_%M_%S')
    fileName = 'submissions/VGG19PlusXGBoost_' + st + '.csv'

    with open(fileName, 'w') as csvfile:
        fieldnames = ['id', 'cancer']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

        writer.writeheader()
        for ind in range(len(validationIDs)):
            writer.writerow({'id': validationIDs[ind], 'cancer': str(pred[ind])})

if __name__ == '__main__':
    #calc_featuresA()
    make_submit()


import json

def ask_for_input():
	# symptomsRaw = input("Please enter your symptoms separated by a comma and space: ")
	# symptomList = symptomsRaw.split(", ")
	filepath = "Symptoms.json"
	with open(filepath) as fp:
		symptomData = json.load(fp)
		symptomList = symptomData["symptoms"]
	return symptomList	#list of symptoms from user

def retrieve_ontology():
	filepath = "DOSFinal.json"
	with open(filepath) as fp:
		ontologyData = json.load(fp)
	return ontologyData	#ontology JSON containing keywords

def search_for_candidates(ontologyData, symptomList):
	candidateList = []
	for disease in ontologyData["diseases"]:
		totalKeywordCounter = len(disease["name"].split())
		diagnosis = {}
		totalRecorded = False
		unmatchedKeywords = disease["keywords"]
		for symptom in symptomList:
			keywordFoundCounter = 0	#take record of number of matches for each symptom
			nameIsMatch = disease["name"].lower().find(symptom)
			if (nameIsMatch > -1):	#check symptom against disease name
				keywordFoundCounter += 1
				if ("name" not in diagnosis):	#initialize dictionary entry
					diagnosis["name"] = disease["name"]
					diagnosis["search terms"] = []

			if (len(disease["keywords"]) > 0):	#only run code on ontology entries with keywords
				if (totalRecorded == False): #prevents duplication of number of total keywords for each symptom in symptomList
					totalKeywordCounter += len(disease["keywords"])
					totalRecorded = True
				for keyword in disease["keywords"]:
					keywordIsMatch = keyword.lower().find(symptom)
					if (keywordIsMatch > -1):	#check symptom against keywords
						unmatchedKeywords = list(filter(lambda a: a != keyword, unmatchedKeywords))
						keywordFoundCounter += 1
						if ("name" not in diagnosis):	#initialize dictionary entry
							diagnosis["name"] = disease["name"]
							diagnosis["search terms"] = []

			if keywordFoundCounter != 0:	#only run code if the ontology entry contained the keyword
				diagnosis["search terms"].append({"# of '" + symptom + "' found": keywordFoundCounter})
		if (bool(diagnosis) == True):	#check diagnosis is not empty because there was no match
			diagnosis["unmatched keywords"] = unmatchedKeywords
			diagnosis["total # of keywords"] = totalKeywordCounter #have to divide by number of symptoms because total keywords amount is duplicated for each loop of a symptom
			candidateList.append(diagnosis)
	return candidateList	#candidates have any of the symptoms but may not have all the symptoms the user provided

def filter_candidates(candidateList, symptomList):
	diagnosesList = []
	for diagnosis in candidateList:
		if (len(diagnosis["search terms"]) == len(symptomList)):	#filter only for candidates that have all the user-provided symptoms
			totalSearchTermFoundCounter = 0	#find sum of all keywords found for ranking purposes
			for term in diagnosis["search terms"]:	#check each "# of __ found" entries
				for key in term:
					totalSearchTermFoundCounter += term[key]	#add number of each symptom found to total counter
			diagnosis["sum of search terms found"] = totalSearchTermFoundCounter
			diagnosesList.append(diagnosis)
	return diagnosesList

def create_JSON(symptomList, rankedDiagnosesList):
	data = {}
	data["patient"] = {"symptoms": symptomList,
					   "diagnoses": rankedDiagnosesList
					  }
	return data

def write_to_json(data):
	filename = "Diagnosis.json"
	with open(filename, "w", encoding = "utf-8") as file:
		json.dump(data, file, indent = 4)

symptomList = ask_for_input()
ontologyData = retrieve_ontology()
candidateList = search_for_candidates(ontologyData, symptomList);
diagnosesList = filter_candidates(candidateList, symptomList)
rankedDiagnosesList = sorted(diagnosesList, key = lambda x: list(x.values())[2], reverse = True)	#sort diagnosesList according to the 2nd index entry which is the total # of keywords
data = create_JSON(symptomList, rankedDiagnosesList)
write_to_json(data)


"""datetime_2_date

Revision ID: f1fda4d52e24
Revises: b3ee7d1a3f0a
Create Date: 2022-03-28 10:30:29.498461

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'f1fda4d52e24'
down_revision = 'b3ee7d1a3f0a'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('customers', 'date_of_birth')
    op.drop_column('price_list', 'date_of_end')
    op.drop_column('review', 'date_of_creation_payment')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('review', sa.Column('date_of_creation_payment', postgresql.TIMESTAMP(), autoincrement=False, nullable=True))
    op.add_column('price_list', sa.Column('date_of_end', postgresql.TIMESTAMP(), autoincrement=False, nullable=True))
    op.add_column('customers', sa.Column('date_of_birth', postgresql.TIMESTAMP(), autoincrement=False, nullable=False))
    # ### end Alembic commands ###


class Solution(object):
    def longestIncreasingPath(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: int
        """
        if not matrix:
            return 0

        self.moves = [(0,1), (0,-1), (1,0), (-1,0)]
        global_max = 0

        for row in range(len(matrix)):
            for col in range(len(matrix[0])):
                path = self.dfs(matrix, row, col, set())
                global_max = max(global_max, path)

        return global_max

    def dfs(self, matrix, row, col, seen):
        stack = [(row, col, 1)]
        curr_max_length = 1
        seen.add((row,col))

        while stack:
            x, y, length = stack.pop()
            curr_max_length = max(curr_max_length, length)

            for r, c in self.moves:
                if not self.is_invalid(matrix, seen, x+r, y+c) and matrix[x+r][y+c] > matrix[x][y]:
                    seen.add((x+r, y+c))
                    stack.append((x+r, y+c, length+1))

        return curr_max_length

    def is_invalid(self, matrix, seen, row, col):
        if row < 0 or row >= len(matrix) or col < 0 or col >= len(matrix[0]) or (row, col) in seen:
            return True
        return False

s = Solution()
print(s.longestIncreasingPath([[1,2]]))

Algorithms/Leetcode/329 - Longest Increasing Path in a Matrix.py


import os
import time
from subprocess import Popen, PIPE

from common import *
from flocker import *
from parser import *

### Feed updater. ###

def fetch_file(url):
    '''
    Fetches a files, local or remote

    @param   url:str  The URL of the file
    @retrun  :str     The content of the file
    '''
    data = None
    if url.startswith('file://'):
        url = url[len('file://'):]
        if os.access(url, os.F_OK):
            with open(url, 'rb') as file:
                data = file.read().decode('utf-8', 'strict')
    else:
        data = Popen(['wget', url, '-O', '-'], stdout = PIPE).communicate()[0]
    return data

def update_feed(feed, if_group, now = None):
    '''
    Update a feed and its subfeeds

    @param  feed:dict<str, _|int|itr<↑>>  The feed
    @param  if_group:str?                 The name of the group the feed should belong to
                                          for it to be updated, `None` to update everything
    @param  now:tuple(int)?               The current time, intended for internal use
    '''
    # Get the current time.
    if now is None:
        now = time.gmtime()
        now = [now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec]

    # Update node.
    if 'inner' in feed:
        # This is a branch.

        # If the node is in the group, all its children are too.
        if feed['group'] == if_group:
            if_group = None

        # Update all children.
        for feed in feed['inner']:
            update_feed(feed, if_group, now)

    elif ((if_group is None) or (feed['group'] == if_group)) and ('url' in feed) and (feed['url'] is not None):
        # This is a leaf, it has an url, and is no the selected group.

        # Get the ID if the node.
        id = feed['id']

        # Get pathnames.
        metafile = '%s/%s' % (root, id)
        datafile = '%s/%s-content' % (root, id)

        # Acquire feed...
        with touch(metafile) as feed_flock:
            # ... and update.

            # Lock the feed file for writing.
            flock(feed_flock, True)

            # Load feed metadata.
            feed_info = None
            with open(metafile, 'rb') as file:
                feed_info = file.read().decode('utf-8', 'strict')
            feed_info = eval(feed_info) if len(feed_info) > 0 else {}

            # Default missing metadata.
            if 'have' not in feed_info:
                feed_info['have'] = set()
            if 'unread' not in feed_info:
                feed_info['unread'] = set()
            if 'url' not in feed_info:
                feed_info['url'] = feed['url']

            # Fetch metadata.
            have = feed_info['have']
            unread = feed_info['unread']
            url = feed_info['url']
            updated = True

            # Update content.
            try:
                # Fetch feed.
                feed_data = fetch_file(url)
                feed_data = [] if feed_data is None else parse_feed(feed_data)

                # Create backup, and parse content.
                bakdata = make_backup(datafile)
                content = [] if bakdata is None else eval(bakdata.decode('utf-8', 'strict'))

                # Find new articles.
                for channel in feed_data:
                    for item in channel['items']:
                        if 'guid' not in item:
                            # Default GUID to the link, if missing.
                            item['guid'] = item['link' if 'link' in item else 'title']
                        guid = item['guid']
                        if guid not in have:
                            # Article is new, remember that/it.
                            unread.add(guid)
                            have.add(guid)
                            content.append(item)
                            # Default publication time to retrieval, if missing.
                            if 'pubdate' not in item:
                                item['pubdate'] = now

                # Update content file.
                save_file(datafile, bakdata, lambda : repr(content).encode('utf-8'))
            except:
                updated = False

            # Update metadata.
            if updated:
                # Update metadata file.
                bakdata = make_backup(metafile)
                save_file(metafile, bakdata, lambda : repr(feed_info).encode('utf-8'));
                # Update new-articles counter.
                feed['new'] = len(unread)

            # Release lock over file, we are done here.
            unflock(feed_flock)


from typing import List, Optional
from pydantic import BaseModel

class PlatformBaseSchema(BaseModel):
    id: Optional[int]
    name: str
    platform_games: List[BaseModel] = list()

    class Config:
        orm_mode = True
        allow_population_by_field_name = True
        arbitrary_types_allowed = True

    def __eq__(self, other):
        if isinstance(self, type(other)):
            return self.name == other.name
        return False

class ListPlatformBaseResponse(BaseModel):
    status: str
    results: int
    platforms = List[PlatformBaseSchema]

    class Config:
        orm_mode = True
        allow_population_by_field_name = True
        arbitrary_types_allowed = True


from __future__ import print_function

# from multiprocessing import Process
# import os, sys

# def f(name):
#     print('{0:<15}'.format('hello '+name), 'module name:', '{0:<15}'.format(__name__), 'parent process:', '{0:<15}'.format(os.getppid()), 'process id:', '{0:<15}'.format(os.getpid()))

# if __name__ == '__main__':
#     for num in range(6):
#         p = Process(target=f, args=('bob '+str(num),))
#         p.start()
#         p.join()
# sys.exit()

import time
from multiprocessing import Process, Lock
def print_with_lock(loc, i):
    loc.acquire()
    try:
        print('hello world ', i)
        time.sleep(1)
    finally:
        loc.release()
if __name__ == '__main__':
    lock = Lock()
    for num in range(6):
        p = Process(target=print_with_lock, args=(lock, num))
        p.start()
        p.join()

# from StringIO import StringIO
# from multiprocessing import Process, Queue
# import sys, time
# # Proxy classes that both write to and capture strings
# # sent to stdout or stderr, respectively.
# class TeeOut(StringIO):
#     def write(self, s):
#         StringIO.write(self, s)
#         sys.__stdout__.write(s)
# class TeeErr(StringIO):
#     def write(self, s):
#         StringIO.write(self, s)
#         sys.__stderr__.write(s)
# class Parent(object):
#     def run(self):
#         # Start child process.
#         queue = Queue()

#         for i in range(3):
#             child = Process(target=self.spawn, args=(queue,))
#             child.start()
#             child.join()
#         # Do someting with out and err...
#         out = queue.get()
#         err = queue.get()
#     def spawn(self, queue):
#         # Save everything that would otherwise go to stdout.
#         out = sys.stdout = TeeOut()
#         err = sys.stderr = TeeErr()

#         try:
#             # Do something...
#             sys.stdout.write('out 1\n')
#             sys.stderr.write('error 1\n')
#         finally:
#             # Restore stdout, stderr and send their contents.
#             sys.stdout = sys.__stdout__
#             sys.stderr = sys.__stderr__
#             queue.put(out.getvalue())
#             queue.put(err.getvalue())
# if __name__ == '__main__':
#     Parent().run()


"""Misspellings.

---
layout:     post
source:     The Wall Street Journal
source_url: http://on.wsj.com/1rksm8k
title:      misspellings
date:       2014-06-10 12:31:19
categories: writing
---

Points out misspellings.

"""
from proselint.tools import memoize, preferred_forms_check

@memoize
def check(text):
    """Suggest the preferred forms."""
    err = "spelling.athletes"
    msg = "Misspelling of athlete's name. '{}' is the preferred form."

    misspellings = [
        ["Dwyane Wade",         ["Dwayne Wade"]],
        ["Miikka Kiprusoff",    ["Mikka Kiprusoff"]],
        ["Mark Buehrle",        ["Mark Buerhle"]],
        ["Skylar Diggins",      ["Skyler Diggins"]],
        ["Agnieszka Radwanska", ["Agnieska Radwanska"]],
        ["J.J. Redick",         ["J.J. Reddick"]],
        ["Manny Pacquiao",      ["Manny Packquaio"]],
        ["Antawn Jamison",      ["Antwan Jamison"]],
        ["Cal Ripken",          ["Cal Ripkin"]],
        ["Jhonny Peralta",      ["Johnny Peralta"]],
        ["Monta Ellis",         ["Monte Ellis"]],
        ["Alex Rodriguez",      ["Alex Rodriquez"]],
        ["Mark Teixeira",       ["Mark Texeira"]],
        ["Brett Favre",         ["Brett Farve"]],
        ["Torii Hunter",        ["Tori Hunter"]],
        ["Stephen Curry",       ["Stephon Curry"]],
        ["Mike Krzyzewski",     ["Mike Kryzewski"]],
    ]

    return preferred_forms_check(text, misspellings, err, msg)


def heapify(arr,l,ind):
    parent = ind
    left = 2*ind + 1
    right = 2*ind + 2

    if left < l and arr[left] < arr[parent]:
        parent = left

    if right < l and arr[right] < arr[parent]:
        parent = right

    if ind != parent:
        arr[parent], arr[ind] = arr[ind], arr[parent]
        heapify(arr,l,parent)

def heapsort(arr):

    for i in range(len(arr)-1,-1,-1):
        heapify(arr,len(arr),i)

    return arr

#user-code
arr = [12, 11, 13, 5, 6, 7]
print(heapsort(arr))


def launchapp(self, cmd, args=[], delay=0, env=1, lang="C"):
        """
        Launch application.

        @param cmd: Command line string to execute.
        @type cmd: string
        @param args: Arguments to the application
        @type args: list
        @param delay: Delay after the application is launched
        @type delay: int
        @param env: GNOME accessibility environment to be set or not
        @type env: int
        @param lang: Application language to be used
        @type lang: string

        @return: 1 on success
        @rtype: integer

        @raise LdtpServerException: When command fails
        """
        try:
            atomac.NativeUIElement.launchAppByBundleId(cmd)
            return 1
        except RuntimeError:
            if atomac.NativeUIElement.launchAppByBundlePath(cmd, args):
                # Let us wait so that the application launches
                try:
                    time.sleep(int(delay))
                except ValueError:
                    time.sleep(5)
                return 1
            else:
                raise LdtpServerException(u"Unable to find app '%s'" % cmd)


def solution(arr, queries):
    answer = []
    for s,e,k in queries:
        temp = []
        for i in range(s,e+1):
            if arr[i] > k:
                temp.append(arr[i])
        if temp:
            answer.append(min(temp))
        else:
            answer.append(-1)

    return answer

프로그래머스/unrated/181923. 수열과 구간 쿼리 2/수열과 구간 쿼리 2.py

수열과 구간 쿼리 2.py


#
# @lc app=leetcode id=23 lang=python
#
# [23] Merge k Sorted Lists
#

# @lc code=start
# Definition for singly-linked list.
from heapq import *
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution(object):
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        pq = []
        head = ListNode()
        tail = head
        for list in lists:
            if list:
                heappush(pq, (list.val, list))

        while pq:
            curr = heappop(pq)[1]
            tail.next = curr
            tail = tail.next
            if curr.next:
                heappush(pq, (curr.next.val, curr.next))
        return head.next
# @lc code=end


def _validate_clientsecrets(clientsecrets_dict):
    """Validate parsed client secrets from a file.

    Args:
        clientsecrets_dict: dict, a dictionary holding the client secrets.

    Returns:
        tuple, a string of the client type and the information parsed
        from the file.
    """
    _INVALID_FILE_FORMAT_MSG = (
        'Invalid file format. See '
        'https://developers.google.com/api-client-library/'
        'python/guide/aaa_client_secrets')

    if clientsecrets_dict is None:
        raise InvalidClientSecretsError(_INVALID_FILE_FORMAT_MSG)
    try:
        (client_type, client_info), = clientsecrets_dict.items()
    except (ValueError, AttributeError):
        raise InvalidClientSecretsError(
            _INVALID_FILE_FORMAT_MSG + ' '
            'Expected a JSON object with a single property for a "web" or '
            '"installed" application')

    if client_type not in VALID_CLIENT:
        raise InvalidClientSecretsError(
            'Unknown client type: {0}.'.format(client_type))

    for prop_name in VALID_CLIENT[client_type]['required']:
        if prop_name not in client_info:
            raise InvalidClientSecretsError(
                'Missing property "{0}" in a client type of "{1}".'.format(
                    prop_name, client_type))
    for prop_name in VALID_CLIENT[client_type]['string']:
        if client_info[prop_name].startswith('[['):
            raise InvalidClientSecretsError(
                'Property "{0}" is not configured.'.format(prop_name))
    return client_type, client_info


def callback(self, pk=None, host_config_key='', extra_vars=None):
        """Contact Tower and request a configuration update using this job template.

        =====API DOCS=====
        Contact Tower and request a provisioning callback using this job template.

        :param pk: Primary key of the job template to run provisioning callback against.
        :type pk: int
        :param host_config_key: Key string used to authenticate the callback host.
        :type host_config_key: str
        :param extra_vars: Extra variables that are passed to provisioning callback.
        :type extra_vars: array of str
        :returns: A dictionary of a single key "changed", which indicates whether the provisioning callback
                  is successful.
        :rtype: dict

        =====API DOCS=====
        """
        url = self.endpoint + '%s/callback/' % pk
        if not host_config_key:
            host_config_key = client.get(url).json()['host_config_key']
        post_data = {'host_config_key': host_config_key}
        if extra_vars:
            post_data['extra_vars'] = parser.process_extra_vars(list(extra_vars), force_json=True)
        r = client.post(url, data=post_data, auth=None)
        if r.status_code == 201:
            return {'changed': True}


# 用户输入目标文件
old_name = input("请输入要备份的文件名")
print(old_name)
index = old_name.rfind(".")
if index > 0:
    postfix = old_name[index:]
new_name = old_name[:index] + "(备份)" + postfix
print("new_name=%s" % (new_name))
old_f = open(old_name, "rb")
new_f = open(new_name, "wb")
while True:
    con = old_f.read(1024)
    new_f.write(con)
    if len(con) == 0:
        break
old_f.close()
new_f.close()


# -*- coding: utf-8 -*-

# At first, it was using facade pattern.
# Source: https://github.com/faif/python-patterns/blob/master/patterns/structural/facade.py
# but it lead to major code duplication, hence refactoring SpriteTracker to single class.

import math

import arcade

from . import globals
from .being import Player, Enemy
from .components.position import Position
from .components.sprite import Sprite
from .map_object import MapObject
from .pathfinding import Pathfinder
from .states import State
from .tile import Tile

class SpriteTracker:
    """
    SpriteTracker tracks all sprites that should be highlighted: active player, path from player to cursor,
    targets, etc. Before every use of SpriteTracker, 'player' and 'mouse_position' should be updated.
    sprite_active is currently selected player, sprite_selected (should be renamed maybe?) are sprites that player
    can click on (like when choosing a tile to attack from 4 available), sprite_targeted are sprites that will be
    affected by attack (shown only when cursor is over sprite_selected).

    Attributes:
    -----------
    _beings: Beings
        Takes instance of Beings; used to highlight active player, and players and enemies in the area of attack.
    _grid: Grid
        Takes instance of Grid; used to highlight targeted MapObject and Tile instances.
    _pathfinder: Pathfinder
        New instance of monostate-like Pathfinder class; used to hightlight path from active player to cursor.
    _tiles_sprites_in_range: arcade.SpriteList()
    _tiles_sprites_selected: arcade.SpriteList()
    _map_objects_sprites_selected: arcade.SpriteList()
    _beings_sprites_selected: arcade.SpriteList()
    _tiles_sprites_overlayed: arcade.SpriteList()
        Every _..._sprites_selected are SpriteLists that aggregate highlighted sprites: sprite_in_range, sprite_active,
        sprite_selected, sprite_targeted.
    mouse_position: Position
        Current Position of mouse, used to determine if cursor is over one of the currently shown sprite_selected, ergo
        to determine if sprite_targeted should be visible. mouse_position should be updated before every SpriteTracker
        usage.
    player: Being
        Currently active player; this value is used to determine a wide range of behaviours, from showing path to
        showing sprite_targeted.

    Methods:
    --------
    _add_to_sprite_list (Tile | MapObject | Player | Enemy, bool=False)
        Takes entity as first argument and bool for a second argument. Based on the entity type, the game adds sprites
        to the correct arcarde.SpriteList. Boolean value is used to decide if sprite_selected (default)
        or sprite_targeted should be used.
    _find (string)
        Generic-as-possible method to find all sprite_selected and sprite_targeted to show. String passed as argument
        is used to determine what list (from: tiles, map_objects, beings) will be iterated over to find the sprites.
        Uses string as argument because passing lists as arguments in Python results in unpredictable behaviour.
    _find_player_beings
    _find_enemy_beings
    _find_map_objects
    _find_tiles
        All these methods at the beginning handle additional cases that depend on the entity type, then call _find.
    _reset_sprite_lists
        Clears _tiles_sprites_selected, _map_objects_sprites_selected, and _beings_sprites_selected.
    track
        One of two public methods of SpriteTracker. 'track' clears all SpriteLists then fills them again by calling
        _find_... methods.
    draw
        Draws every Sprites that should be highlighted.
    """

    def __init__(self, beings, grid):
        self._beings = beings
        self._grid = grid
        self._pathfinder = Pathfinder(grid)
        self._tiles_sprites_in_range = arcade.SpriteList()
        self._tiles_sprites_selected = arcade.SpriteList()
        self._tiles_sprites_overlayed = arcade.SpriteList()
        self._map_objects_sprites_selected = arcade.SpriteList()
        self._beings_sprites_selected = arcade.SpriteList()
        self._attack_overlay = arcade.SpriteList()
        self.mouse_position = None
        self.player = None

    def _add_to_sprite_list(self, entity, targeted=False, overlay=False):
        """
        Adds entity sprite to corresponding sprite_list.
        Defaults to sprite_selected, but may add sprite_targeted too.
        """
        sprite = entity.sprite_selected.arcade_sprite
        if targeted:
            sprite = entity.sprite_targeted.arcade_sprite
        if isinstance(entity, Player) or isinstance(entity, Enemy):
            self._beings_sprites_selected.append(sprite)
        elif isinstance(entity, MapObject):
            self._map_objects_sprites_selected.append(sprite)
        elif isinstance(entity, Tile):
            self._tiles_sprites_selected.append(sprite)
        if overlay:
            sprite = Sprite(
                "target.png",
                entity.px_position,
                0.125,
            )
            self._attack_overlay.append(sprite)

    def _find(self, what):
        """
        Generic method used to find and add Sprites to _..._sprites_selected. Finds both "yellow" Sprites
        (sprite_selected, so sprites that are highlighted when player enters targeting mode) and "red" Sprites
        (sprite_targeted, when player hovers mouse over sprite_selected).
        Assigns lists using string passed as argument, because directly passing lists as argument may result in
        unpredicatble behaviour.
        """
        l = None
        if what == "player_beings":
            l = self._beings.player_beings
        elif what == "enemy_beings":
            l = self._beings.enemy_beings
        elif what == "map_objects":
            l = self._grid.map_objects.objects
        elif what == "tiles":
            l = self._grid.tiles
        for effect in self.player.attack.effects:
            for coords in effect.target_positions:
                try:
                    pos = Position(
                        self.player.cell_position.x + coords[0],
                        self.player.cell_position.y + coords[1],
                    )
                    for entity in l:
                        # Find "yellow" entity - ie tile that player can click on.
                        if (
                            entity.cell_position.x == pos.x
                            and entity.cell_position.y == pos.y
                        ):
                            self._add_to_sprite_list(entity)
                        # Find "red" entity - ie tile that will be attacked when player clicks on yellow tile.
                        if (
                            pos.x == self.mouse_position.x
                            and pos.y == self.mouse_position.y
                        ):
                            for coords2 in effect.attack_pattern:
                                pos2 = Position(
                                    pos.x + coords2[0],
                                    pos.y + coords2[1],
                                )
                                if (
                                    entity.cell_position.x == pos2.x
                                    and entity.cell_position.y == pos2.y
                                ):
                                    self._add_to_sprite_list(entity, True)
                except AttributeError:
                    pass  # No valid player_being found.
                except ValueError as e:
                    print(f"{e} in sprite_tracker._find({what})")

    def _find_player_beings(self):
        for player in self._beings.player_beings:
            if player.active:
                self._beings_sprites_selected.append(player.sprite_active.arcade_sprite)
        if globals.state == State.TARGET and not self.player.attacked:
            self._find("player_beings")

    def _find_enemy_beings(self):
        if globals.state == State.TARGET and not self.player.attacked:
            self._find("enemy_beings")

    def _find_map_objects(self):
        if globals.state == State.TARGET and not self.player.attacked:
            self._find("map_objects")

    def _find_tiles(self):
        # Find all tiles that are overlayed.
        for tile in self._grid.tiles:
            if tile.overlayed_by:
                try:
                    self._tiles_sprites_overlayed.append(
                        tile.sprite_overlayed.arcade_sprite
                    )
                except ValueError as e:  # Sprite already in SpriteList. Ignore.
                    pass
        if globals.state == State.MOVE and not self.player.moved:
            # Draw all tiles that are in player range.
            for tile in self._grid.tiles:
                self._pathfinder.set_up_path_grid(self._beings)
                self._pathfinder.find_path(
                    self.player.cell_position,
                    tile.cell_position,
                )
                for i, coords in enumerate(self._pathfinder.last_path):
                    tile = self._grid.find_tile_by_position(
                        Position(coords[0], coords[1])
                    )
                    try:
                        if i <= self.player.range:
                            self._tiles_sprites_in_range.append(
                                tile.sprite_in_range.arcade_sprite
                            )
                    except ValueError as e:  # Sprite already in SpriteList. .clear() not called?
                        pass
            try:
                # Then show path from player to cursor.
                self._pathfinder.set_up_path_grid(self._beings)
                self._pathfinder.find_path(
                    self.player.cell_position,
                    self.mouse_position,
                )
                for i, coords in enumerate(self._pathfinder.last_path):
                    tile = self._grid.find_tile_by_position(
                        Position(coords[0], coords[1])
                    )
                    # Use green "sprite_path" if tile is in range of active player.
                    spr = tile.sprite_path
                    if i > self.player.range:
                        # Otherwise, use red "sprite_targeted".
                        spr = tile.sprite_targeted
                    self._tiles_sprites_selected.append(spr.arcade_sprite)
            except TypeError:  # Empty last_path
                pass
        elif globals.state == State.TARGET and not self.player.attacked:
            self._find("tiles")

    def _reset_sprite_lists(self):
        self._beings_sprites_selected.clear()
        self._map_objects_sprites_selected.clear()
        self._tiles_sprites_in_range.clear()
        self._tiles_sprites_selected.clear()
        self._tiles_sprites_overlayed.clear()

    def track(self):
        """Clears all arcade.SpriteList instances in SpriteTracker and fills them again with updated data."""
        self._reset_sprite_lists()
        self._find_tiles()
        self._find_map_objects()
        self._find_enemy_beings()
        self._find_player_beings()

    def draw(self):
        self._tiles_sprites_in_range.draw()
        self._tiles_sprites_selected.draw()
        self._map_objects_sprites_selected.draw()
        self._beings_sprites_selected.draw()
        self._tiles_sprites_overlayed.draw()


from math import log2, sqrt

import tensorflow as tf
from tensorflow.keras import Model, Sequential
from tensorflow.keras.layers import Layer, Conv2D, ZeroPadding2D, Embedding, Conv2DTranspose, LayerNormalization
from tensorflow.keras.utils import normalize

from einops import rearrange
from dalle_tensorflow.utils import exists, is_empty, masked_mean, top_k, gumbel_softmax
from dalle_tensorflow.layers import Linear
from dalle_tensorflow.transformer import Transformer
from dalle_tensorflow.axial_positional_embedding import AxialPositionalEmbedding

class ResBlock(Layer):
    def __init__(self, filters):
        super(ResBlock, self).__init__()
        self.net = Sequential([
            ZeroPadding2D(padding=(1, 1)),
            Conv2D(filters, 3, padding='valid', activation='relu'),
            ZeroPadding2D(padding=(1, 1)),
            Conv2D(filters, 3, padding='valid', activation='relu'),
            Conv2D(filters, 1)
        ])

    def call(self, x):
        return self.net(x) + x

class DiscreteVAE(Model):
    def __init__(self, image_size=256, num_tokens=512, codebook_dim=512, num_layers=3, num_resblocks=0, hidden_dim=64,
                 channels=3, temperature=0.9, straight_through=False):
        super(DiscreteVAE, self).__init__()
        assert log2(image_size).is_integer(), 'Image size must be a power of 2.'
        assert num_layers >= 1, 'Number of layers must at least 1.'

        self.image_size = image_size
        self.num_tokens = num_tokens
        self.num_layers = num_layers
        self.temperature = temperature
        self.straight_through = straight_through
        self.codebook = Embedding(num_tokens, codebook_dim)

        self.codebook.build([None, self.num_tokens])

        encode_channels = [hidden_dim] * num_layers
        decode_channels = list(reversed(encode_channels))

        encode_channels = [channels, *encode_channels]

        decode_initial_channels = codebook_dim if not num_resblocks > 0 else decode_channels[0]
        decode_channels = [decode_initial_channels, *decode_channels]

        encode_channels_io, decode_channels_io = map(
            lambda tensor: list(zip(tensor[:-1], tensor[1:])), (encode_channels, decode_channels))

        encode_layers = []
        decode_layers = []

        for (encode_in, encode_out), (decode_in, decode_out) in zip(encode_channels_io, decode_channels_io):
            encode_layers.append(Sequential([
                ZeroPadding2D(padding=(1, 1)),
                Conv2D(encode_out, 4, strides=(2, 2), padding='valid', activation='relu')
            ]))
            decode_layers.append(Sequential([
                Conv2DTranspose(decode_out, 4, strides=(2, 2), padding='same', activation='relu'),
            ]))

        for _ in range(num_resblocks):
            decode_layers.insert(0, ResBlock(decode_channels[1]))
            encode_layers.append(ResBlock(encode_channels[-1]))

        if num_resblocks:
            decode_layers.insert(0, Conv2D(decode_channels[1], 1))

        encode_layers.append(Conv2D(num_tokens, 1))
        decode_layers.append(Conv2D(channels, 1))

        self.encoder = Sequential(encode_layers)
        self.decoder = Sequential(decode_layers)

    def get_codebook_indices(self, images):
        logits = tf.stop_gradient(self.call(images, return_logits=True))
        logits = tf.math.argmax(logits, axis=-1)
        codebook_indices = tf.reshape(logits, [-1, tf.reduce_prod(logits.shape[1:])])
        return codebook_indices

    def decode(self, image_sequence):
        image_embeddings = self.codebook(image_sequence)
        b, n, d = image_embeddings.shape
        h = w = int(sqrt(n))

        image_embeddings = rearrange(image_embeddings, 'b (h w) d -> b h w d', h=h, w=w)
        images = self.decoder(image_embeddings)
        return images

    def call(self, image, return_recon_loss=False, return_logits=False):
        logits = self.encoder(image)

        if return_logits:
            # Return logits for getting hard image indices for DALL-E training
            return logits

        soft_one_hot = gumbel_softmax(logits=logits, temperature=self.temperature, axis=-1, hard=self.straight_through)

        codebook_weights = tf.convert_to_tensor(self.codebook.get_weights())
        codebook_weights = tf.squeeze(input=codebook_weights, axis=0)
        sampled = tf.einsum('b h w n, n d -> b h w d', soft_one_hot, codebook_weights)
        out = self.decoder(sampled)

        if not return_recon_loss:
            return out
        loss = tf.compat.v1.losses.mean_squared_error(image, out)
        return loss

class CLIP(Layer):
    def __init__(self, *, text_dim=512, image_dim=512, latent_dim=512, num_text_tokens=10000, text_encode_depth=6,
                 text_sequence_len=256, text_heads=8, num_visual_tokens=512, visual_encode_depth=6, visual_heads=8,
                 visual_image_size=256, visual_patch_size=32, channels=3):
        super(CLIP, self).__init__()
        self.text_embedding = Embedding(num_text_tokens, text_dim)
        self.text_pos_embedding = Embedding(text_sequence_len, text_dim)
        self.text_transformer = Transformer(input_dim=text_dim, depth=text_encode_depth, sequence_len=text_sequence_len,
                                            causal=False, heads=text_heads)
        self.to_text_latent = Linear(units=latent_dim, input_dim=text_dim, bias=False)

        assert visual_image_size % visual_patch_size == 0, 'Image dimensions must be divisible by the patch size.'
        num_patches = (visual_image_size // visual_patch_size) ** 2
        patch_dim = channels * visual_patch_size ** 2

        self.visual_patch_size = visual_patch_size
        self.to_visual_embedding = Linear(units=image_dim, input_dim=patch_dim)
        self.visual_pos_embedding = Embedding(num_patches, image_dim)
        self.visual_transformer = Transformer(input_dim=image_dim, depth=visual_encode_depth, sequence_len=num_patches,
                                              causal=False, heads=visual_heads)
        self.to_visual_latent = Linear(latent_dim, image_dim, bias=False)

        self.temperature = tf.Variable(1.)

    def call(self, text, image, text_mask=None, return_loss=False):
        b, p = text.shape[0], self.visual_patch_size

        text_embeddings = self.text_embedding(text)
        text_embeddings += self.text_pos_embedding(tf.range(text.shape[1]))

        image_patches = rearrange(image, 'b c (h p1) (w p2) -> b (h w) (p1 p2 c)', p1=p, p2=p)
        image_embeddings = self.to_visual_embedding(image_patches)
        image_embeddings += self.visual_pos_embedding(tf.range(image_embeddings.shape[1]))

        encode_text = self.text_transformer(text_embeddings, mask=text_mask)
        encode_image = self.visual_transformer(image_embeddings)

        if exists(text_mask):
            text_latents = masked_mean(input_tensor=encode_text, mask=text_mask, axis=1)
        else:
            text_latents = tf.math.reduce_mean(input_tensor=encode_text, axis=1)

        image_latents = tf.math.reduce_mean(input_tensor=encode_image, axis=1)

        text_latents = self.to_text_latent(text_latents)
        image_latents = self.to_visual_latent(image_latents)

        text_latents, image_latents = map(lambda tensor: normalize(tensor, axis=-1, order=2), (text_latents, image_latents))
        temp = tf.math.exp(self.temperature)

        if not return_loss:
            sim = tf.einsum('n d, n d -> n', text_latents, image_latents) * temp
            return sim

        sim = tf.einsum('i d, j d -> i j', text_latents, image_latents) * temp
        labels = tf.range(b)
        loss = (tf.nn.softmax_cross_entropy_with_logits(labels, sim) +
                tf.nn.softmax_cross_entropy_with_logits(tf.transpose(labels), sim)) / 2
        return loss

class DALLE(Model):
    def __init__(self, *, dim, vae, num_text_tokens=10000, text_sequence_len=256, depth, heads=8, dim_head=64,
                 reversible=False, attn_dropout=0.0, ff_dropout=0.0, noncausal_attn_len=0, ignore_index=-100):
        super(DALLE, self).__init__()
        assert isinstance(vae, DiscreteVAE), 'VAE must be an instance of DiscreteVAE.'
        vae.trainable = False   # `Weights for model %s have not yet been created.` would be raised otherwise
        image_size = vae.image_size
        num_image_tokens = vae.num_tokens
        image_sequence_len = (vae.image_size // (2 ** vae.num_layers)) ** 2

        self.text_embedding = Embedding(num_text_tokens, dim)
        self.image_embedding = Embedding(num_image_tokens, dim)

        self.text_pos_embedding = Embedding(text_sequence_len + 1, dim)     # +1 for <BOS>
        self.image_pos_embedding = AxialPositionalEmbedding(dim=dim, axial_shape=(image_size, image_size))

        self.num_text_tokens = num_text_tokens  # For offsetting logits index and calculating cross entropy loss
        self.num_image_tokens = num_image_tokens
        total_tokens = num_text_tokens + num_image_tokens
        self.total_tokens = total_tokens

        self.text_sequence_len = text_sequence_len
        self.image_sequence_len = image_sequence_len
        total_sequence_len = text_sequence_len + image_sequence_len
        self.total_sequence_len = total_sequence_len

        self.noncausal_attn_len = noncausal_attn_len

        self.vae = vae
        if exists(self.vae):
            self.vae = vae
            self.image_embedding = vae.codebook

        self.transformer = Transformer(input_dim=dim, depth=depth, sequence_len=total_sequence_len,
                                       reversible=reversible, causal=True, heads=heads, dim_head=dim_head,
                                       attn_dropout=attn_dropout, ff_dropout=ff_dropout,
                                       noncausal_attn_len=noncausal_attn_len + 1)

        self.to_logits = Sequential([
            LayerNormalization(),
            Linear(units=self.total_tokens, input_dim=dim)
        ])

        sequence_range = tf.range(total_sequence_len)
        logits_range = tf.range(total_tokens)

        sequence_range = rearrange(sequence_range, 'n -> () n ()')
        logits_range = rearrange(logits_range, 'd -> () () d')

        self.logits_mask = tf.Variable(initial_value=(
                ((sequence_range >= text_sequence_len) & (logits_range < num_text_tokens)) |
                ((sequence_range < text_sequence_len) & (logits_range >= num_text_tokens))
        ), trainable=False, name='logits_mask')

        self.ignore_index = ignore_index

    def generate_images(self, text, *, clip=None, mask=None, filter_threshold=0.8, temperature=1):
        vae, text_sequence_len, image_sequence_len, num_text_tokens = \
            self.vae, self.text_sequence_len, self.image_sequence_len, self.num_text_tokens
        total_sequence_len = text_sequence_len + image_sequence_len

        if tf.rank(text) < 2:
            text = tf.expand_dims(input=text, axis=0)
        if exists(mask) and tf.rank(mask) < 2:
            mask = tf.expand_dims(input=mask, axis=0)

        out = text

        for cur_len in range(text.shape[1], total_sequence_len):
            is_image = cur_len >= text_sequence_len

            text, image = out[:, :text_sequence_len], out[:, text_sequence_len:]
            logits = tf.stop_gradient(self(text, image, mask=mask)[:, -1, :])
            filtered_logits = top_k(logits, threshold=filter_threshold)
            probs = tf.nn.softmax(logits=filtered_logits / temperature, axis=-1)
            sample = tf.random.categorical(logits=tf.math.log(probs), num_samples=1)
            # Offset sampled token if it is an image token, since logit space is composed of text and then image tokens
            sample -= num_text_tokens if is_image else 0
            out = tf.concat(values=[out, sample], axis=-1)

            if out.shape[1] <= text_sequence_len:
                mask = tf.pad(tensor=mask, paddings=[[0, 0], [0, 1]], mode='CONSTANT', constant_values=True)

        text_sequence = out[:, :text_sequence_len]

        image_sequence = out[:, -image_sequence_len:]
        images = vae.decode(image_sequence)

        if exists(clip):
            scores = clip(text=text_sequence, image=images, return_loss=False)
            return images, scores

        return images

    def call(self, text, image=None, mask=None, return_loss=False):
        ignore_index, total_sequence_len = self.ignore_index, self.total_sequence_len

        # Use padding 1 as <BOS>
        text = tf.pad(tensor=text, paddings=[[0, 0], [1, 0]], mode="CONSTANT", constant_values=1)

        if exists(mask):
            mask = tf.pad(tensor=mask, paddings=[[0, 0], [1, 0]], mode="CONSTANT", constant_values=True)

        tokens = self.text_embedding(text)
        tokens += self.text_pos_embedding(tf.range(text.shape[1]))

        sequence_len = tokens.shape[1]

        if exists(image) and not is_empty(image):
            is_raw_image = len(image.shape) == 4
            if is_raw_image:
                image = self.vae.get_codebook_indices(image)

            image_len = image.shape[1]
            image_embeddings = self.image_embedding(image)
            image_embeddings += self.image_pos_embedding(image_embeddings)
            tokens = tf.concat([tokens, image_embeddings], axis=1)
            sequence_len += image_len
            if exists(mask):
                mask = tf.pad(tensor=mask, paddings=[[0, 0], [0, image_embeddings.shape[1]]], mode='CONSTANT',
                              constant_values=True)

        # When training, if the length exceeds the total text + image length
        # Remove the last token, since it needs not to be trained
        if tokens.shape[1] > total_sequence_len:
            sequence_len -= 1
            tokens = tokens[:, :-1]

            if exists(mask):
                mask = mask[:, :-1]
        out = self.transformer(tokens, mask=mask)
        logits = self.to_logits(out)

        # Mask logits to make sure text predicts text (except last token), and image predicts image
        logits_mask = self.logits_mask[:, :sequence_len]
        max_negative_value = -tf.experimental.numpy.finfo(logits.dtype).max
        logits = tf.where(logits_mask, max_negative_value, logits)

        if not return_loss:
            return logits

        assert exists(image), 'When training, image must be supplied.'
        noncausal_attn_len = self.noncausal_attn_len
        offsetted_image = image + self.num_text_tokens
        offsetted_image = tf.cast(offsetted_image, dtype=text.dtype)
        labels = tf.concat(values=[text[:, 1:], offsetted_image], axis=1)

        if noncausal_attn_len:
            sequence_range = tf.range(sequence_len)
            noncausal_attn_mask = sequence_range < noncausal_attn_len
            noncausal_attn_mask = rearrange(noncausal_attn_mask, 'n -> () n')
            labels = tf.where(noncausal_attn_mask, ignore_index, labels)

        loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels, logits)
        return loss


import Util as utl
import os
import sys
sys.path.insert(0, "../data_preprocessing")
import shuffle_list as shuffle

def isValidFramesStack(clipPath, StartingFrame, NumStackingFrames, step=1):
    last_frame = StartingFrame + (NumStackingFrames-1)*step
    return os.path.exists(os.path.join(clipPath, "{:04}.jpg".format(last_frame)))

def stack_clips(ds_dir, dataset_txtfile, dataset_txtfile_output_path,
                NumStackingFrames = 16, overlapFrames=0, step=1):
    """
    Saves a zero based file with all the starts of each stack
    """
    if not os.path.isdir(utl.get_full_path_for_dir_containing_file(dataset_txtfile_output_path)):
        os.makedirs(utl.get_full_path_for_dir_containing_file(dataset_txtfile_output_path))

    with open(dataset_txtfile) as f:
        lines = f.readlines()

    NewGT = []

    for line in lines:
        l = line.split(',')[0].replace(".mp4", "")
        label = line.split(',')[1]

        # startingFrame = 1

        for startingFrame in range(1, step + 1):
            while True:
                if not isValidFramesStack(os.path.join(ds_dir, l), startingFrame, NumStackingFrames, step):
                    if os.path.exists(os.path.join(ds_dir, l, "{:04}.jpg".format(startingFrame)))\
                            and isValidFramesStack(os.path.join(ds_dir, l), startingFrame, NumStackingFrames/2, step):
                        NewGT += [l + " " + str(startingFrame) + " " + str(step) + " " + str(int(label)) + "\n"]
                    break
                NewGT += [l + " " + str(startingFrame) + " " + str(step) + " " + str(int(label)) + "\n"]
                startingFrame += NumStackingFrames*step

    with open(dataset_txtfile_output_path, 'w') as file_writer:
        file_writer.writelines(NewGT)

if __name__ == '__main__':
    for step in [1, 2, 3, 4, 6, 12]:
        # stack_clips(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
        stack_clips("/home/bassel/data/office-actions/office_actions_19/short_clips/resized_frms_224",
                    "/home/bassel/data/office-actions/office_actions_19/short_clips/labels/trainlist.txt",
                    "/home/bassel/data/office-actions/office_actions_19/short_clips/labels/multi_steps/step_"+str(step)+"/trainlist_stacks.txt",
                    NumStackingFrames=10, step=step)

        shuffle.shuffle_list("/home/bassel/data/office-actions/office_actions_19/short_clips/labels/multi_steps/step_"+str(step)+"/trainlist_stacks.txt")

        stack_clips("/home/bassel/data/office-actions/office_actions_19/short_clips/resized_frms_224",
                    "/home/bassel/data/office-actions/office_actions_19/short_clips/labels/testlist.txt",
                    "/home/bassel/data/office-actions/office_actions_19/short_clips/labels/multi_steps/step_"+str(step)+"/testlist_stacks.txt",
                    NumStackingFrames=10, step=step)


# -*- coding: utf-8 -*-
"""
Created on Sun Mar 31 13:19:30 2019

@author: Shalin
"""

#from __future__ import print_function
import collections
import os
import tensorflow as tf
import keras.backend as K
from keras.models import Sequential, load_model
from keras.layers import Dense, Activation, Embedding, Dropout, TimeDistributed
from keras.layers import LSTM
#from keras.optimizers import Adam
from keras.utils import to_categorical
#from keras.callbacks import ModelCheckpoint
import numpy as np
import argparse
import matplotlib.pyplot as plt
import pandas as pd

"""To run this code, you'll need to first download and extract the text dataset
    from here: http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz. Change the
    data_path variable below to your local exraction path"""

DATA_PATH = "..\simple-examples\data"

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--run_opt', type=str, default='train',choices=['train','test'], help='An integer: 1 to train, 2 to test')
    parser.add_argument('--data_path', type=str, default=DATA_PATH, help='The full path of the training data')
    parser.add_argument('--char_word', type=str, default='', help='Train/Test character or word')
    return parser.parse_args()

def read_words(filename):
    with tf.gfile.GFile(filename, "r") as f:
        return f.read().replace("\n", "<eos>").split()

def build_vocab(filename):
    data = read_words(filename)

    counter = collections.Counter(data)
    count_pairs = sorted(counter.items(), key=lambda x: (-x[1], x[0]))

    words, _ = list(zip(*count_pairs))
    word_to_id = dict(zip(words, range(len(words))))
    return word_to_id
#    return dict(zip(counter.keys(),range(len(counter))))

def file_to_word_ids(filename, word_to_id):
    data = read_words(filename)
    return [word_to_id[word] for word in data if word in word_to_id]

def load_data(char_word=''):
    # get the data paths
    if char_word:
        char_word += '.'
    train_path = os.path.join(DATA_PATH, "ptb."+char_word+"train.txt")
    valid_path = os.path.join(DATA_PATH, "ptb."+char_word+"valid.txt")
    test_path = os.path.join(DATA_PATH, "ptb."+char_word+"test.txt")

    # build the complete vocabulary, then convert text data to list of integers
    word_to_id = build_vocab(train_path)
    train_data = file_to_word_ids(train_path, word_to_id)
    valid_data = file_to_word_ids(valid_path, word_to_id)
    test_data = file_to_word_ids(test_path, word_to_id)
    vocabulary = len(word_to_id)
    reversed_dictionary = dict(zip(word_to_id.values(), word_to_id.keys()))

#    print(train_data[:5])
#    print(word_to_id)
#    print(vocabulary)
#    print(" ".join([reversed_dictionary[x] for x in train_data[:10]]))
    return train_data, valid_data, test_data, vocabulary, reversed_dictionary

def perplexity(y_true, y_pred):
    cross_entropy = K.categorical_crossentropy(y_true, y_pred)
    return K.pow(2.0, cross_entropy)

class KerasBatchGenerator(object):

    def __init__(self, data, num_steps, batch_size, vocabulary, skip_step=5):
        self.data = data
        self.num_steps = num_steps
        self.batch_size = batch_size
        self.vocabulary = vocabulary
        # this will track the progress of the batches sequentially through the
        # data set - once the data reaches the end of the data set it will reset
        # back to zero
        self.current_idx = 0
        # skip_step is the number of words which will be skipped before the next
        # batch is skimmed from the data set
        self.skip_step = skip_step

    def generate(self):
        x = np.zeros((self.batch_size, self.num_steps))
        y = np.zeros((self.batch_size, self.num_steps, self.vocabulary))
        while True:
            for i in range(self.batch_size):
                if self.current_idx + self.num_steps >= len(self.data):
                    # reset the index back to the start of the data set
                    self.current_idx = 0
                x[i, :] = self.data[self.current_idx:self.current_idx + self.num_steps]
                temp_y = self.data[self.current_idx + 1:self.current_idx + self.num_steps + 1]
                # convert all of temp_y into a one hot representation
                y[i, :, :] = to_categorical(temp_y, num_classes=self.vocabulary)
                self.current_idx += self.skip_step
            yield x, y

def create_model():
#    use_dropout=True
    model = Sequential()
    model.add(Embedding(vocabulary, hidden_size, input_length=num_steps))
    model.add(LSTM(hidden_size, return_sequences=True))
    #model.add(LSTM(hidden_size, return_sequences=True))
#    if use_dropout:
    model.add(Dropout(0.5))
    model.add(TimeDistributed(Dense(vocabulary)))
    model.add(Activation('softmax'))

#    optimizer = Adam()
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy',perplexity])

    print(model.summary())
    return model
#checkpointer = ModelCheckpoint(filepath=data_path + '/model-{epoch:02d}.hdf5', verbose=1)

if __name__ == '__main__':

    num_steps = 30
    batch_size = 100
    hidden_size = 500
    num_epochs = 20
    args = get_args()
#    if args.char_word=='char':
#        train_data, valid_data, test_data, vocabulary, reversed_dictionary = load_data('char')
#    else:
#        train_data, valid_data, test_data, vocabulary, reversed_dictionary = load_data()
    train_data, valid_data, test_data, vocabulary, reversed_dictionary = load_data(args.char_word)
    if args.data_path:
        DATA_PATH = args.data_path

    if args.run_opt == 'train':
        train_data_generator = KerasBatchGenerator(train_data, num_steps, batch_size, vocabulary,
                                                   skip_step=num_steps)
        valid_data_generator = KerasBatchGenerator(valid_data, num_steps, batch_size, vocabulary,
                                                   skip_step=num_steps)
        model = create_model()
        history = model.fit_generator(train_data_generator.generate(), len(train_data)//(batch_size*num_steps), num_epochs,
                            validation_data=valid_data_generator.generate(),
                            validation_steps=len(valid_data)//(batch_size*num_steps),verbose=2)#, callbacks=[checkpointer])
# =============================================================================
#         for key,values in iter(history.history):
#             history.history
#         loss_history = history.history['val_loss']#, 'val_categorical_accuracy', 'val_perplexity', 'loss', 'categorical_accuracy', 'perplexity']
#         numpy_loss_history = np.array(loss_history)
#         np.savetxt("loss_history.txt", numpy_loss_history, delimiter=",")
# =============================================================================
        df = pd.DataFrame.from_dict(history.history)
        df.to_csv('word.csv')
        if args.char_word=='':
            word ='word'
        else:
            word = 'char'
        model.save(DATA_PATH +'\\' + word +"_model.hdf5")

        fig, ax = plt.subplots()
        ax.plot(range(num_epochs), history.history['perplexity'], '-b', label='training perplexity')
        ax.plot(range(num_epochs), history.history['val_perplexity'], '-r', label='validation perplexity')
        ax.set_xlabel('Epoch')
        ax.set_ylabel('Perplexity')
        ax.legend()
        if args.char_word:
            ax.set_title('Character-level')
        else:
            ax.set_title('Word-level')
        plt.savefig('word-perplex.png')
        fig, ax = plt.subplots()
        ax.plot(range(num_epochs), history.history['categorical_accuracy'], '-b', label='training accuracy')
        ax.plot(range(num_epochs), history.history['val_categorical_accuracy'], '-r', label='validation accuracy')
        ax.set_xlabel('Epoch')
        ax.set_ylabel('Accuracy')
        ax.legend()
        if args.char_word:
            ax.set_title('Character-level')
        else:
            ax.set_title('Word-level')
        plt.savefig('word-acc.png')

#    elif args.run_opt == 'test':
    else:
        if args.char_word=='':
            word ='word'
        else:
            word = 'char'
        model = load_model(DATA_PATH + "\\"+word+"_model.hdf5", custom_objects={'perplexity': perplexity})
        dummy_iters = 40
        example_training_generator = KerasBatchGenerator(train_data, num_steps, 1, vocabulary,
                                                         skip_step=1)
        print("\nTraining data:")
        for i in range(dummy_iters):
            dummy = next(example_training_generator.generate())
        num_predict = 100
        true_print_out = "Actual words: \n"
        pred_print_out = "Predicted words: \n"
        for i in range(num_predict):
            data = next(example_training_generator.generate())
            prediction = model.predict(data[0])
            predict_word = np.argmax(prediction[:, num_steps-1, :])
            true_print_out += reversed_dictionary[train_data[num_steps + dummy_iters + i]] + " "
            pred_print_out += reversed_dictionary[predict_word] + " "
        print(true_print_out,'\n')
        print(pred_print_out)
        # test data set
        dummy_iters = 40
        example_test_generator = KerasBatchGenerator(test_data, num_steps, 1, vocabulary,
                                                         skip_step=1)
        print("\nTest data:")
        for i in range(dummy_iters):
            dummy = next(example_test_generator.generate())
        num_predict = 200
        true_print_out = "Actual words: \n"
        pred_print_out = "Predicted words: \n"
        for i in range(num_predict):
            data = next(example_test_generator.generate())
            prediction = model.predict(data[0])
            predict_word = np.argmax(prediction[:, num_steps - 1, :])
            true_print_out += reversed_dictionary[test_data[num_steps + dummy_iters + i]] + " "
            pred_print_out += reversed_dictionary[predict_word] + " "
        print(true_print_out,'\n')
        print(pred_print_out)


import socket
import threading
from tcplink import tcplink

## 服务端 ##

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定监听的地址和端口
# （服务器同时响应多个客户端请求，需要绑定一个端口并监听连接）

# 服务器可能有多块网卡，可以绑定到某一块网卡的ip上
# 0.0.0.0绑定到所有的网络地址
# 127.0.0.1绑定到本机地址，客户端必须在本机运行才能连接
# 端口号需预先指定。小于1024的端口号必须要管理员权限才能绑定

# 绑定端口
s.bind(('127.0.0.1', 9999))

# 监听
s.listen(5)     #指定等待连接的最大数量
print('Waiting for connection...')

while True:
    # 接受一个新连接
    sock, addr = s.accept()
    # 创建新线程来处理TCP连接。传入函数、参数
    t = threading.Thread(target = tcplink, args = (sock, addr))
    t.start()

# 每个连接都必须创建新线程（或进程）来处理