from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from user.models import MusicUser
from .models import Playlist, Song
from django.forms.models import model_to_dict
from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
from django.shortcuts import render
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt
from mutagen.mp3 import MP3
from django.db.utils import IntegrityError
import json
# Create your views here.

class ComplexEncoder(json.JSONEncoder):
    def default(self, obj):
        """
        自定义JSON编码器，增加对时间的编码方式
        """
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, obj)

@login_required
def mymusic(request, success=4):
    music_user = request.user
    collect = music_user.collect_user.all()
    build = music_user.build_user.all()
    collect_list = list(collect.values())
    build_list = list(build.values())

    # 前后端数据对接
    collect_result = []
    for each in collect_list:
        dict_collect = {}
        dict_collect.update({"id":each["id"]})
        dict_collect.update({"playList_name":each["playlistname"]})
        dict_collect.update({"playlist_createdTime":str(each["build_date"])})
        play = Playlist.objects.filter(id = each["id"])
        song_num = len(play[0].songs.all().values())
        dict_collect.update({"playlist_cnt":song_num})
        collect_result.append(dict_collect)

    build_result = []
    for each in build_list:
        dict_build = {}
        dict_build.update({"id":each["id"]})
        dict_build.update({"playList_name":each["playlistname"]})
        dict_build.update({"playlist_createdTime":str(each["build_date"])})
        play=Playlist.objects.filter (id = each["id"])
        song_num = len(play[0].songs.all().values())
        dict_build.update({"playlist_cnt":song_num})
        build_result.append(dict_build)

    return render(request, 'music/playlist.html', {
        'username': request.user.username,
        'build_result': build_result,
        'collect_result': collect_result,
        'style1': 'nav_hover',
        'success': success,
    })

@login_required
def upload(request):
    """
    上传音乐界面
    """
    error = ['','','','上传音乐文件','上传歌词文件','上传配图文件']
    flag = True
    if request.method == "POST":
        songname = ""
        singer = ""
        words = ""
        song_url = ""
        picture_url = ""
        album_name = ""
        song_time = 0
        songname = request.POST.get("songname",None)  #输入歌曲名
        singer = request.POST.get("singer",None) #输入歌手名
        album_name = request.POST.get("album_name",None) #输入专辑名
        #获取歌曲文件
        song_file = request.FILES.get("song_file",None)
        #获取歌词文件
        song_words_file = request.FILES.get("song_words_file",None)
        #获取图片文件
        picture_file = request.FILES.get("picture_file",None)
        #判断输入是否出错
        if album_name == None or album_name == "":
            error[0] = "专辑名不能为空"
            flag = False
        if songname == None or songname == "":
            error[1] = "歌名不能为空"
            flag = False
        if singer == None or singer == "":
            error[2] = "歌手名不能为空"
            flag = False
        if song_file == None or song_file.name[-4:]!=".mp3":
            error[3] = "歌曲文件未选择或格式不为MP3"
            flag = False
        if song_words_file == None or song_words_file.name[-4:]!=".txt":
            error[4] = "歌词txt文件未选择或格式不为txt"
            flag = False
        if picture_file == None:
            error[5] = "歌曲配图文件未选择"
            flag = False
        #按要求输入相关数据和文件后进行数据处理
        if flag:
            thesong = Song.objects.create(
                    songname = songname,
                    singer = singer,
                    album_name = album_name,
                    words = "words",
                    song_url = "song_url",
                    picture_url = "picture_url",
                    song_time = "song_time",
                    userid = request.user
                )
            id=thesong.id
            #歌曲文件及路径名
            file_name_song = "./static/song_file/" + str(id) + "_" + song_file.name
            #歌词文件及路径名
            file_name_song_words = "./static/song_words_file/" + str(id) + "_" + song_words_file.name
            #图片文件及路径名
            file_name_picture = "./static/picture_file/" + str(id) + "_" + picture_file.name
            picture_url = file_name_picture
            song_url = file_name_song
            #分块写入文件
            with open(file_name_song, mode='wb+') as f:
                for chunk in  song_file.chunks():
                    f.write(chunk)
            with open(file_name_song_words, mode='wb+') as f:
                for chunk in  song_words_file.chunks():
                    f.write(chunk)
            with open(file_name_picture, mode='wb+') as f:
                for chunk in  picture_file.chunks():
                    f.write(chunk)
            #读取歌词存为string
            for line in  open(file_name_song_words, encoding='utf-8'):
                line = line.strip('\n')
                words = words+line+"\n"
            #获取音乐时长
            audio = MP3(song_url)
            song_time = str(audio.info.length)
            #创建model
            thesong = Song.objects.filter(id=id).update(
                    words = words,
                    song_url = song_url[1:],
                    picture_url = picture_url[1:],
                    song_time = song_time
                )
            #插入到默认歌单：我发布的音乐
            thesong = Song.objects.get(id=id)
            playlist = Playlist.objects.get(playlistname=str(request.user.username)+"_发布的音乐")
            playlist.songs.add(thesong)
            return render(request, 'music/upload.html', {
                'alertBool': 1,
                'username': request.user.username,
                })
        else:
            return render(request, 'music/upload.html', {
                    'alertBool': 0,
                    'username': request.user.username,
                })
    else:
        return render(request, 'music/upload.html', {
                    'username': request.user.username,
                    'alertBool': 2,
                    "error": error,
                    "style2":"nav_hover"
                })

def search_playlist(user, value):
    """
    根据关键字搜索歌单
    """
    playlists = Playlist.objects.filter(playlistname__icontains=value)
    playlists_result = []
    tmep_playlists_result = list(playlists.all().values())
    for each_playlist in tmep_playlists_result:
        dict_playlist = {}
        dict_playlist.update({"playlist_id":each_playlist["id"]})
        dict_playlist.update({"songListT_name":each_playlist["playlistname"]})

        play = Playlist.objects.filter (id = each_playlist["id"])
        song_num = len(play[0].songs.all().values())

        dict_playlist.update({"songListT_num":song_num})
        dict_playlist.update({"songList_songauthor":play[0].build_user.username})
        if len(play[0].collectuser.all().filter(username=user.username)) > 0:
            # 已收藏
            dict_playlist.update({"playlist_flag":"1"})
        else:
            # 未收藏
            dict_playlist.update({"playlist_flag":"0"})
        playlists_result.append(dict_playlist)
    return playlists_result

def search_music(value):
    """
    根据关键字搜索歌曲
    """
    songs = Song.objects.filter (songname__icontains=value)
    temp_result = list(songs.values())

    songs_result=[]
    for each_song in temp_result:
        dict_song = {}
        dict_song.update({"song_id":each_song["id"]})
        dict_song.update({"songList_songname":each_song["songname"]})
        dict_song.update({"songList_songauthor":each_song["singer"]})
        dict_song.update({"songList_album":each_song["album_name"]})
        temp = each_song["song_time"]
        time = str(int(float(temp)/60)) + ':' + str(int(float(temp))%60)
        dict_song.update({"songList_songtime":time})
        songs_result.append(dict_song)
    return songs_result

@login_required
def search(request):
    """
    搜索歌曲或歌单
    """

    value = request.GET.get("value",None)
    songs_result = search_music(value)
    user = request.user
    playlists_result = search_playlist(user, value)
    return render(request, 'music/Search.html', {
        'value': value,
        'songs_result': songs_result,
        'playlists_result' : playlists_result,
        'username': user.username
    })

@login_required
def music_player_song(request):
    """
    音乐播放界面
    """
    value = request.GET.get("value")
    songid = request.GET.get("songid")
    songs_result=search_music(value)
    return render(request, 'music/music_player.html', {
        'songid' : songid,
        'songs_result': songs_result,
        'username': request.user.username
    })

@login_required
def music_player_playlist(request):
    """
    获取完整歌单播放
    """
    playlistid = request.GET.get("playlistid")
    songid = request.GET.get("songid")
    p = Playlist.objects.filter (id=playlistid)
    playlist = p.all().values()[0]
    songs = list(p[0].songs.all().values())
    songs_result=[]
    for each_song in songs:
        dict_song = {}
        dict_song.update({"song_id":each_song["id"]})
        dict_song.update({"songList_songname":each_song["songname"]})
        dict_song.update({"songList_songauthor":each_song["singer"]})
        dict_song.update({"songList_album":each_song["album_name"]})
        temp = each_song["song_time"]
        time = str(int(float(temp)/60)) + ':' + str(int(float(temp))%60)
        dict_song.update({"songList_songtime":time})
        songs_result.append(dict_song)

    return render(request, 'music/music_player.html', {
        'songid' : songid,
        'songs_result': songs_result,
        'username': request.user.username
    })

@login_required
def single_playlist_info(request):
    """
    歌单详情页面
    """
    playlistid = request.GET.get("id")
    p = Playlist.objects.filter (id=playlistid)
    playlist = p.all().values()[0]

    # 歌单相关字典
    playlists_result = []
    dict_playlist = {}
    dict_playlist.update({"playlist_id":playlist["id"]})
    dict_playlist.update({"playList_name":playlist["playlistname"]})
    dict_playlist.update({"playList_date":str(playlist["build_date"])})
    dict_playlist.update({"picture_url":playlist["picture_url"]})

    song_num = len(p[0].songs.all().values())
    dict_playlist.update({"playList_num":song_num})
    dict_playlist.update({"playList_build_user":p[0].build_user.username})
    playlists_result.append(dict_playlist)

    # 歌曲相关字典
    songs = list(p[0].songs.all().values())
    songs_result=[]
    for each_song in songs:
        dict_song = {}
        dict_song.update({"song_id":each_song["id"]})
        dict_song.update({"songList_songname":each_song["songname"]})
        dict_song.update({"songList_songauthor":each_song["singer"]})
        dict_song.update({"songList_album":each_song["album_name"]})
        temp = each_song["song_time"]
        time = str(int(float(temp)/60)) + ':' + str(int(float(temp))%60)
        dict_song.update({"songList_songtime":time})
        songs_result.append(dict_song)

    return render(request, 'music/single_playlist_info.html', {
        'playlists_result': playlists_result,
        'songs_result': songs_result,
        'username': request.user.username,
    })

def get_music_detail(request):
    """
    搜索功能：获取歌曲信息
    """
    songid = request.GET.get('id', None)
    result = list(Song.objects.filter(id=songid).values())

    songname = result[0]['songname']
    album_name = result[0]['album_name']
    song_url = result[0]['song_url']
    words = result[0]['words']
    picture_url = result[0]['picture_url']

    jsondata = {
            'songname': songname,
            'album_name': album_name,
            'song_url': song_url,
            'words':words,
            'picture_url': picture_url
            }
    return JsonResponse(jsondata, json_dumps_params={'ensure_ascii':False})

@login_required
def playlist(request):
    """
    搜索功能：获取歌单详情信息
    """
    playlistid = request.GET.get('playlistid', None)
    p = Playlist.objects.filter(id=playlistid)
    playlist = p.all().values()[0]
    songs = list(p[0].songs.all().values())
    playlist['build_user'] = p[0].build_user.username
    playlist['songs_counts'] = len(songs)
    playlist['songs'] = json.dumps(songs, cls=ComplexEncoder)
    playlist['attrs'] = Song.getattr()
    return render(request, 'music/playlist.html', {
        'username': request.user.username,
        'playlist': playlist,
    })

@login_required
def createlist(request):
    """
    创建歌单，前台利用ajax发送请求
    """
    if request.method == 'POST':
        playlistname = request.POST.get('playlistname', None)
        build_user = request.user
        picture_file = request.FILES.get("picture_file",None)
        pl = Playlist.objects.filter(playlistname=playlistname, build_user=build_user)
        if len(pl) != 0:
            message = '歌单名已存在，请重新输入！'
            return render(request, 'music/createlist.html', {
                'alertBool': 0,
                'username': request.user.username,
                'message': message,
            })
        #创建歌单
        thelist = Playlist.objects.create(
            playlistname = playlistname,
            picture_url = "",
            build_user = build_user,
        )
        id = thelist.id
        file_name_picture = "./static/playlist_picture_file/" + str(id) + "_" + picture_file.name
        picture_url = file_name_picture
        #写入文件
        with open(file_name_picture, mode='wb+') as f:
            for chunk in  picture_file.chunks():
                f.write(chunk)
        #更新歌单图片路径
        thelist = Playlist.objects.filter(id=id).update(
            #playlistname = playlistname,
            picture_url = picture_url[1:],
            #build_user = build_user,
        )
        return mymusic(request, 3)
    return render(request, 'music/createlist.html', {
        'alertBool': 2,
        'username': request.user.username,
    })

@login_required
def remove_playlist(request):
    """
    删除歌单
    """
    flag = 0
    listid = request.GET.get("id",None)
    playlist = Playlist.objects.get(id = listid)
    if playlist.build_user == request.user:  #删除用户自建的歌单
        # 检查是否为系统默认歌单，默认歌单无法删除
        if playlist.playlistname == str(request.user.username)+"_发布的音乐" or playlist.playlistname == str(request.user.username)+"_喜欢的音乐":
            return mymusic(request, 0)
        else:
            playlist.delete()
            return mymusic(request, 1)
    elif request.user in list(playlist.collectuser.all()): #删除用户收藏的歌单
        playlist.collectuser.remove(request.user)
        return mymusic(request, 2)

@login_required
def getsonglist(request):
    """
    获取当前用户所创建的歌单
    """
    theuser = request.user
    build = Playlist.objects.filter(build_user = theuser).exclude(playlistname = "%s_发布的音乐" % theuser.username)
    result = [{"play_listid":x.id,"list_img":x.picture_url,"list_num":len(list(x.songs.all())),"list_name":x.playlistname} for x in build]

    return JsonResponse( result,safe=False,json_dumps_params={'ensure_ascii':False})

@login_required
def remove_song(request):
    """
    删除歌曲
    """
    theuser = request.user
    songid =  request.GET.get('songid', None)
    songlistid =  request.GET.get('songlistid', None)
    if songid == None or  songlistid == None:
        return JsonResponse({"message":"参数错误","status":0} ,json_dumps_params={'ensure_ascii':False})
    song = Song.objects.get(id = songid ) #歌曲
    songlist = Playlist.objects.get(id = songlistid) #歌单
    # 检查是否问当前歌曲的上传者
    if theuser != songlist.build_user:
        return JsonResponse({"message":"这不是您创建的歌单，您没有权限!","status":0},json_dumps_params={'ensure_ascii':False})
    if song  in songlist.songs.all():
        songlist.songs.remove(song)
        return JsonResponse({"message":"您成功将歌曲在歌单 %s 内删除 !"%songlist.playlistname ,"status":1},json_dumps_params={'ensure_ascii':False})
    else:
        return JsonResponse({"message":"该歌曲不在歌单内 %s"%songlist.playlistname ,"status":1},json_dumps_params={'ensure_ascii':False})

@login_required
def add_song(request):
    theuser = request.user
    songid =  request.GET.get('songid', None)
    songlistid =  request.GET.get('songlistid', None)
    if songid == None or  songlistid == None:
        return JsonResponse({"message":"参数错误","status":0} ,json_dumps_params={'ensure_ascii':False})
    song = Song.objects.get(id = songid ) #歌曲
    songlist = Playlist.objects.get(id = songlistid) #歌单
    if theuser != songlist.build_user:
        return JsonResponse({"message":"这不是您创建的歌单，您没有权限!","status":0},json_dumps_params={'ensure_ascii':False})
    if song not in songlist.songs.all():
        songlist.songs.add(song)
        return JsonResponse({"message":"您成功将歌曲添加入歌单：%s !"%songlist.playlistname ,"status":1},json_dumps_params={'ensure_ascii':False})
    else:
        return JsonResponse({"message":"歌单： %s已存在该收藏歌曲"%songlist.playlistname ,"status":1},json_dumps_params={'ensure_ascii':False})

@login_required
def add_songlist(request):
    """
    添加歌曲，前台利用ajax技术
    """
    theuser = request.user
    songlistid = request.GET.get("songlistid",None)
    if songlistid == None:
        return JsonResponse({"message":"parameters errors"})
    songlist = Playlist.objects.get(id = songlistid)
    if theuser in songlist.collectuser.all():
        songlist.collectuser.remove(theuser)
        return JsonResponse({"message":"你已取消收藏该歌单"})
    else:
        songlist.collectuser.add(theuser)
        return JsonResponse({"message":"你成功收藏改该歌单"})

@login_required
def alterSong(request):
    """
    编辑歌曲信息
    """
    song_name = request.GET.get('song_name', None)
    list_name = request.GET.get('list_name', None)
    action = request.GET.get('action', None)

    song = Song.objects.get(songname=song_name)
    playlist = Playlist.objects.get(playlistname=list_name)

    if action == '1':
        # 添加联系
        if song not in playlist.songs.all():
            playlist.songs.add(song)
    else:
        # 删除联系
        if song in playlist.songs.all():
            playlist.songs.remove(song)

    return JsonResponse({'Action': ('Add' if action=='1' else 'Delete'), 'result': 'Success'})

@login_required
def alterPlayList(request):
    """
    收藏歌单
    """
    playlist = request.GET.get('playlist', None)
    action = request.GET.get('action', None)
    user = request.user
    playlist = models.Playlist.objects.get(playlistname=playlist)

    if action == '1':
        # 添加联系
        if user not in playlist.collectuser.all():
            playlist.collectuser.add(user)
    else:
        # 删除联系
        if user in playlist.collectuser.all():
            playlist.collectuser.remove(user)

    return JsonResponse({'Action': ('Add' if action=='1' else 'Delete'), 'result': 'Success'})


class Person:
  def speak(self):
    print('Just a Person')

class Student(Person):
  def speak(self):
    print('Just a Student')

student = Student()
student.__class__

student.speak
Student.speak

Person.speak
person = Person()
person.speak

id(person.speak)
id(Person.speak)

student.__class__ = Person
student.speak

id(student.speak)


# -*- coding: utf-8 -*-
from scapy.all import conf
import concurrent.futures

def bridge_from_eth0_to_eth1():
    eth0_socket = conf.L2socket(iface='eth0')
    eth1_socket = conf.L2socket(iface='eth1')

    while True:
        p = eth0_socket.recv()
        if p:
            eth1_socket.send(p.original)

def bridge_from_eth1_to_eth0():
    eth0_socket = conf.L2socket(iface='eth0')
    eth1_socket = conf.L2socket(iface='eth1')

    while True:
        p = eth1_socket.recv()
        if p:
            eth0_socket.send(p.original)

def bridge():
    try:
        # Python2のProcessPoolExecutorには問題があるので、クリティカルなところでは使えない
        # https://pypi.python.org/pypi/futures
        executor = concurrent.futures.ProcessPoolExecutor(max_workers=3)
        executor.submit(bridge_from_eth0_to_eth1)
        executor.submit(bridge_from_eth1_to_eth0)

    except KeyboardInterrupt:
        # threadingと異なり、特に何もしなくてもCtrl + C が可能
        pass

if __name__ == '__main__':
    bridge()


import json
from http import HTTPStatus

import requests

import secrets_app

def _get_country_page(content):
    return int(content['postback']['payload'].split('_')[-1])

def _write_message(payload, message):
    payload['message'] = message

def _get_timezone(countryCode):
    req_point = 'http://localhost:8000/api/region/global/timezone/'

    response = requests.get(req_point, params={'CountryCode': countryCode})

    return response

def _get_user_info(user_id):
    req_point = 'https://graph.facebook.com/v11.0/{user_id}'.format(user_id=user_id)

    response = requests.get(
        req_point,
        params={
            'fields': 'id,name',
            'access_token': secrets_app.FACEBOOK_CHAT_ACCESS_TOKEN
        }
    )

    return response

def _save_user_info(user_data):
    req_point = 'http://localhost:8000/api/facebook/users/info/'

    # user_data saved check
    response = requests.get(req_point, params=user_data)

    if response.status_code == HTTPStatus.BAD_REQUEST:  # should POST
        return requests.post(req_point, params=user_data)

    else:
        if json.loads(response.content) != user_data:  # should PUT
            return requests.put(req_point, params=user_data)

    return response

def _delete_user_info(user_data):
    req_point = 'http://localhost:8000/api/facebook/users/info/'

    # user_data saved check
    response = requests.get(req_point, params=user_data)

    if response.status_code == HTTPStatus.OK:
        return requests.delete(req_point, params=user_data)

    return response


#!/usr/bin/env python
"""An advanced Reducer, using Python iterators and generators."""

from itertools import groupby
from operator import itemgetter
import sys
import os
import random

counter = 1

def main():

    global counter
    for line in sys.stdin:
        x= line.split()
        if x[len(x)-3]:
            y = x[len(x)-3]
            z = y[:(len(x)-2)]
            point = ","
            if z.endswith(point):
                if int(z[:1]) == counter:
                    num = random.randint(1, 10000)
                    if num < 3125:
                        #print(z[:1], len(z[:1]))
                        print(line)
            else:
                if int(z) == counter:
                    num = random.randint(1, 10000)
                    if num < 3125:
                        #print(z, len(z))
                        print(line)

if __name__ == "__main__":
    main()


import os
import numpy as np
import paddle.fluid as fluid

from ..model import ModelBase
from . import resnet_video
from .nonlocal_utils import load_params_from_file

import logging
logger = logging.getLogger(__name__)

__all__ = ["NonLocal"]

# To add new models, import them, add them to this map and models/TARGETS

class NonLocal():
    def __init__(self, name, cfg, mode='train'):
        super(NonLocal, self).__init__(name, cfg, mode=mode)
        self.get_config()

    def get_config(self):
        # video_length
        self.video_length = self.get_config_from_sec(self.mode, 'video_length')
        # crop size
        self.crop_size = self.get_config_from_sec(self.mode, 'crop_size')

    def create_model_args(self):
        return None

    def build_model(self):
        pred, loss = resnet_video.create_model(
            data=self.feature_input[0],
            label=self.label_input,
            cfg=self.cfg,
            is_training=self.is_training,
            mode=self.mode)
        if loss is not None:
            loss = fluid.layers.mean(loss)
        self.network_outputs = [pred]
        self.loss_ = loss

    def optimizer(self):
        base_lr = self.get_config_from_sec('TRAIN', 'learning_rate')
        lr_decay = self.get_config_from_sec('TRAIN', 'learning_rate_decay')
        step_sizes = self.get_config_from_sec('TRAIN', 'step_sizes')
        lr_bounds, lr_values = get_learning_rate_decay_list(base_lr, lr_decay,
                                                            step_sizes)
        learning_rate = fluid.layers.piecewise_decay(
            boundaries=lr_bounds, values=lr_values)

        momentum = self.get_config_from_sec('TRAIN', 'momentum')
        use_nesterov = self.get_config_from_sec('TRAIN', 'nesterov')
        l2_weight_decay = self.get_config_from_sec('TRAIN', 'weight_decay')
        logger.info(
            'Build up optimizer, \ntype: {}, \nmomentum: {}, \nnesterov: {}, \
                                         \nregularization: L2 {}, \nlr_values: {}, lr_bounds: {}'
            .format('Momentum', momentum, use_nesterov, l2_weight_decay,
                    lr_values, lr_bounds))
        optimizer = fluid.optimizer.Momentum(
            learning_rate=learning_rate,
            momentum=momentum,
            use_nesterov=use_nesterov,
            regularization=fluid.regularizer.L2Decay(l2_weight_decay))
        return optimizer

    def loss(self):
        return self.loss_

    def outputs(self):
        return self.network_outputs

    def feeds(self):
        return self.feature_input if self.mode == 'infer' else \
                     self.feature_input + [self.label_input]

    def pretrain_info(self):
        return (
            'Nonlocal_ResNet50_pretrained',
            'https://paddlemodels.bj.bcebos.com/video_classification/Nonlocal_ResNet50_pretrained.tar.gz'
        )

    def weights_info(self):
        pass

    def load_pretrain_params(self, exe, pretrain, prog, place):
        load_params_from_file(exe, prog, pretrain, place)

def get_learning_rate_decay_list(base_learning_rate, lr_decay, step_lists):
    lr_bounds = []
    lr_values = [base_learning_rate * 1]
    cur_step = 0
    for i in range(len(step_lists)):
        cur_step += step_lists[i]
        lr_bounds.append(cur_step)
        decay_rate = lr_decay**(i + 1)
        lr_values.append(base_learning_rate * decay_rate)

    return lr_bounds, lr_values


from tensorflow.examples.tutorials.mnist import input_data
import model_helper
import tensorflow as tf
import vae_model
import numpy as np
import scipy
import random
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

def plot_images(outputs, xs, hparams, output_infer):
    for i in range(outputs.shape[0]):
        out_img = outputs[i, :]
        out_img = np.reshape(out_img, (hparams.img_width, hparams.img_height))
        scipy.misc.imsave(output_infer + '/gen_img_%d_z_%d.png' % (i,hparams.z_dim), out_img)
        orig_img = xs[i, :]
        orig_img = np.reshape(orig_img, (hparams.img_width, hparams.img_height))
        scipy.misc.imsave(output_infer + '/orig_img_%d_z_%d.png' % (i,hparams.z_dim), orig_img)

def plot_zs(data,labels, output_infer):
    plt.figure(figsize=(10, 8))
    plt.scatter(data[:, 0], data[:, 1], c=np.argmax(labels, 1))
    plt.colorbar()
    plt.grid()
    plt.savefig(output_infer+'/distr.png')

def inference(ckpt, inference_output_folder, hparams):
    output_infer = inference_output_folder

    # Read data
    mnist = input_data.read_data_sets("data/", one_hot=True)
    if hparams.infer_source == "test":
        infer_data = mnist.test.images
        infer_labels = mnist.test.labels
    elif hparams.infer_source == "validation":
        infer_data = mnist.validation.images
        infer_labels = mnist.validation.labels
    elif hparams.infer_source == "train":
        infer_data = mnist.train.images
        infer_labels = mnist.train.labels
    infer_sample_idxs = random.sample(range(infer_data.shape[0]), hparams.infer_sample)
    infer_sample = infer_data[infer_sample_idxs, :]
    infer_sample_labels = infer_labels[infer_sample_idxs, :]

    dataset, x_placeholder, batch_placeholder = model_helper.create_batched_dataset(hparams.x_dim)
    iterator = tf.contrib.data.Iterator.from_structure(mnist.train.images.dtype,
                                                       tf.TensorShape([None, hparams.x_dim]))
    init_op = iterator.make_initializer(dataset)
    next_x = iterator.get_next()
    dropout = tf.placeholder(tf.float32, name='dropout')
    model = vae_model.FullyConnectedVAE(hparams, next_x,dropout)

    with tf.Session() as session:
        model = model_helper.load_model(model, ckpt, session)
        session.run(init_op, feed_dict={x_placeholder: infer_sample,
                                        batch_placeholder: infer_sample.shape[0],
                                        dropout:0.0})
        # Decode
        outputs, zs, xs = model.infer(session)
        plot_images(outputs, xs, hparams, output_infer)
        if zs.shape[1] == 2:
            plot_zs(zs, infer_sample_labels, output_infer)


import numpy as np
import logging
from .UserTrip import UserTrip
from .Location import Location

class UserAutonomous:
    id_count = -1

    def __init__(
        self, env, graph, ui, config, results, origin, destination, departure_time, target_time,
    ):
        self.next_id()
        self.id = UserAutonomous.id_count

        self.env = env
        self.graph = graph
        self.ui = ui
        self.config = config
        self.results = results
        self.user_trip = UserTrip()

        self.origin = origin
        self.destination = destination
        self.departure_time = departure_time
        self.target_time = target_time

        self.location = None
        self.bike_id = None
        self.bike_location = Location()
        self.time_wait = None
        self.time_ride = None
        self.instant_bike = False

        self.WALKING_SPEED = config["WALKING_SPEED"] / 3.6  # m/s
        self.INSTANT_BETA = config["INSTANT_BETA"] # %

    @classmethod
    def reset(cls):
        UserAutonomous.id_count = -1

    def next_id(self):
        UserAutonomous.id_count += 1

    def walk_to(self, location):
        distance = self.dist(self.location, location)
        time = distance / self.WALKING_SPEED
        yield self.env.timeout(time)
        logging.info("[%.2f] User %d walked from [%.4f, %.4f] to location [%.4f, %.4f]" % (self.env.now, self.id, self.location.lon, self.location.lat, location.lon, location.lat,))
        self.location = location

    def ride_bike_to(self, location):
        logging.info(
            "[%.2f] User %d biking with bike %d from [%.4f, %.4f] to location [%.4f, %.4f]" % (self.env.now, self.id, self.bike_id, self.location.lon, self.location.lat, location.lon, location.lat,)
        )
        yield self.env.process(self.ui.bike_ride(self.bike_id, location))
        self.location = location

    def dist(self, a, b):
        return self.graph.get_shortest_path_length(a, b)

    def start(self):
        self.env.process(self.process())

    def init_user(self):
        # waits until its the hour to initialize user
        yield self.env.timeout(self.departure_time)
        self.location = self.origin
        logging.info("[%.2f] User %d initialized at location [%.4f, %.4f]" % (self.env.now, self.id, self.location.lon, self.location.lat))

    def process(self):
        # 0-Setup

        # 1-Init on origin
        yield self.env.process(self.init_user())

        # 2-Call autonomous bike
        self.bike_id, bike_location = self.call_autonomous_bike(self.location)

        if self.bike_id is None:
            rand_number = np.random.randint(100)
            if rand_number <= self.INSTANT_BETA:
                self.instant_bike = True
                self.bike_id, bike_location = self.call_autonomous_instant_bike(self.location)

        if self.bike_id is None:
            logging.info("[%.2f] User %d will not make the trip" % (self.env.now, self.id))
            self.save_user_trip()
            return

        logging.info("[%.2f] User %d was assigned the autonomous bike %d" % (self.env.now, self.id, self.bike_id))

        # 3-Wait for autonomous bike
        yield self.env.process(self.autonomous_drive(self.instant_bike))

        self.bike_location = bike_location
        self.time_wait = self.env.now - self.departure_time
        yield self.env.process(self.unlock_bike())

        # 4-Ride bike
        yield self.env.process(self.ride_bike_to(self.destination))

        # 5-Lock bike
        yield self.env.process(self.lock_bike())

        # 6-Finish
        # TODO: estimate travel from building to nearest node
        logging.info("[%.2f] User %d arrived to final destination" % (self.env.now, self.id))

        self.time_ride = self.env.now - self.time_wait - self.departure_time
        # 7-Charge bike if low battery
        yield self.env.process(self.charge_bike())

        # 8-Save state
        self.save_user_trip()

    def autonomous_drive(self, instant=False, rebalancing=False, liberate=False, charge=False):
        yield self.env.process(self.ui.autonomous_drive(self.bike_id, self.location, self.id, instant, rebalancing, liberate, charge))

    def call_autonomous_bike(self, location):
        return self.ui.call_autonomous_bike(location)

    def call_autonomous_instant_bike(self, location):
        return self.ui.call_autonomous_instant_bike(location)

    def charge_bike(self):
        yield self.env.process(self.ui.bike_charge(self.bike_id))

    def unlock_bike(self):
        self.ui.bike_unlock(self.bike_id, self.id)
        yield self.env.timeout(1)
        logging.info("[%.2f] User %d unlocked bike %d" % (self.env.now, self.id, self.bike_id))

    def lock_bike(self):
        yield self.env.timeout(1)
        self.ui.bike_lock(self.bike_id)
        logging.info("[%.2f] User %d locked bike %d" % (self.env.now, self.id, self.bike_id))

    def save_user_trip(self):
        self.user_trip.set("user_id", self.id)
        self.user_trip.set("bike_id", self.bike_id)
        self.user_trip.set("mode", 2)

        self.user_trip.set("time_departure", self.departure_time, 0)
        self.user_trip.set("time_target", self.target_time, 0)
        self.user_trip.set("time_ride", self.time_ride, 0)
        self.user_trip.set("time_wait", self.time_wait, 0)

        self.user_trip.set("origin_lon", self.origin.lon, 5)
        self.user_trip.set("origin_lat", self.origin.lat, 5)
        self.user_trip.set("destination_lon", self.destination.lon, 5)
        self.user_trip.set("destination_lat", self.destination.lat, 5)

        self.user_trip.set("bike_lon", self.bike_location.lon)
        self.user_trip.set("bike_lat", self.bike_location.lat)

        self.user_trip.set("instant_bike", self.instant_bike)

        self.results.add_user_trip(self.user_trip)


from Fruits import Fruit
import numpy as np

def ligne():

    Ananas = Fruit("ananas", 50, 0.2, "assets/ananas.png")
    Cerise = Fruit("cerise", 15, 0.25, "assets/cerise.png")
    Orange = Fruit("orange", 5, 0.4, "assets/orange.png")
    Pasteque = Fruit("pasteque", 150, 0.1, "assets/pasteque.png")
    Pomme_dore = Fruit("pomme_dore", 10000, 0.05, "assets/pomme_dore.png")

    liste_fruit = [Ananas, Cerise, Orange, Pasteque, Pomme_dore]
    liste_proba = [Ananas.proba, Cerise.proba, Orange.proba, Pasteque.proba, Pomme_dore.proba]

    tirage=np.random.choice(liste_fruit, 3, liste_proba)
    print(tirage)
    return tirage

def check_ligne(result):
    gain=0
    if result[0].nom == result[1].nom == result[2].nom:
        gain = result[0].gain
        print(f"gagne 3 {result[0].nom} vous avez obtenu  {result[0].gain} jetons ")
    else:
        print("perdu ")
    return gain


from yamlformat.validator import subfield_lib

SUBFIELD_FOLDER = subfield_lib.SubfieldFolder(folderpath='subfields')
SUBFIELD_UNIVERSE = subfield_lib.SubfieldUniverse(folders=[SUBFIELD_FOLDER])

SUBFIELD_FOLDER.AddFromConfig(
    config_filename='subfields/subfields.yaml',
    documents=[{
        'aggregation': {
            'average': 'foobar',
            'max': 'foobar',
            'min': 'foobar',
            'total': 'foobar',
        },
        'component': {
            'battery': 'foobar',
            'cable': 'foobar',
            'coil': 'foobar',
            'compressor': 'foobar',
            'condenser': 'foobar',
            'controller': 'foobar',
            'cooler': 'foobar',
            'damper': 'foobar',
            'dehumidifier': 'foobar',
            'device': 'foobar',
            'dial': 'foobar',
            'dimmer': 'foobar',
            'dishwasher': 'foobar',
            'dryer': 'foobar',
            'evaporator': 'foobar',
            'fan': 'foobar',
            'filter': 'foobar',
            'guidevane': 'foobar',
            'heater': 'foobar',
            'humidifier': 'foobar',
            'lamp': 'foobar',
            'pump': 'foobar',
            'shade': 'foobar',
            'tank': 'foobar',
            'valve': 'foobar',
            'vane': 'foobar',
            'wheel': 'foobar',
        },
        'descriptor': {
            'air': 'foobar',
            'aisle': 'foobar',
            'blowdown': 'foobar',
            'boost': 'foobar',
            'broken': 'foobar',
            'building': 'foobar',
            'bypass': 'foobar',
            'calendar': 'foobar',
            'call': 'foobar',
            'chilled': 'foobar',
            'circulation': 'foobar',
            'co': 'foobar',
            'co2': 'foobar',
            'cogeneration': 'foobar',
            'cold': 'foobar',
            'condensing': 'foobar',
            'conference': 'foobar',
            'control': 'foobar',
            'cooling': 'foobar',
            'dehumidification': 'foobar',
            'detection': 'foobar',
            'discharge': 'foobar',
            'east': 'foobar',
            'economizer': 'foobar',
            'electric': 'foobar',
            'email': 'foobar',
            'emergency': 'foobar',
            'entering': 'foobar',
            'evaporative': 'foobar',
            'event': 'foobar',
            'exchange': 'foobar',
            'exercise': 'foobar',
            'exhaust': 'foobar',
            'panel': 'foobar',
            'fabric': 'foobar',
            'fire': 'foobar',
            'flue': 'foobar',
            'gas': 'foobar',
            'glycol': 'foobar',
            'guide': 'foobar',
            'hash': 'foobar',
            'heating': 'foobar',
            'heat': 'foobar',
            'high': 'foobar',
            'hot': 'foobar',
            'humidification': 'foobar',
            'id': 'foobar',
            'inlet': 'foobar',
            'input': 'foobar',
            'inter': 'foobar',
            'isolation': 'foobar',
            'leak': 'foobar',
            'leaving': 'foobar',
            'limit': 'foobar',
            'line': 'foobar',
            'loop': 'foobar',
            'low': 'foobar',
            'makeup': 'foobar',
            'manufacturer': 'foobar',
            'matched': 'foobar',
            'master': 'foobar',
            'medium': 'foobar',
            'message': 'foobar',
            'mixed': 'foobar',
            'mixing': 'foobar',
            'model': 'foobar',
            'neutral': 'foobar',
            'next': 'foobar',
            'no2': 'foobar',
            'north': 'foobar',
            'occupancy': 'foobar',
            'occupied': 'foobar',
            'ongoing': 'foobar',
            'output': 'foobar',
            'particle': 'foobar',
            'outside': 'foobar',
            'phase1': 'foobar',
            'phase2': 'foobar',
            'phase3': 'foobar',
            'photocatalyst': 'foobar',
            'preheating': 'foobar',
            'pressurization': 'foobar',
            'primary': 'foobar',
            'process': 'foobar',
            'production': 'foobar',
            'program': 'foobar',
            'protection': 'foobar',
            'recovery': 'foobar',
            'release': 'foobar',
            'refrigerant': 'foobar',
            'request': 'foobar',
            'return': 'foobar',
            'reversing': 'foobar',
            'room': 'foobar',
            'scene': 'foobar',
            'schedule': 'foobar',
            'season': 'foobar',
            'secondary': 'foobar',
            'seismic': 'foobar',
            'side': 'foobar',
            'smoke': 'foobar',
            'source': 'foobar',
            'south': 'foobar',
            'southeast': 'foobar',
            'southwest': 'foobar',
            'spray': 'foobar',
            'stage': 'foobar',
            'steam': 'foobar',
            'suction': 'foobar',
            'summer': 'foobar',
            'supervisor': 'foobar',
            'supply': 'foobar',
            'suppression': 'foobar',
            'sweeper': 'foobar',
            'sync': 'foobar',
            'tertiary': 'foobar',
            'ultraviolet': 'foobar',
            'unoccupied': 'foobar',
            'use': 'foobar',
            'user': 'foobar',
            'ventilation': 'foobar',
            'voc': 'foobar',
            'water': 'foobar',
            'west': 'foobar',
            'wind': 'foobar',
            'winter': 'foobar',
            'zone': 'foobar',
        },
        'measurement': {
            'concentration': 'foobar',
            'current': 'foobar',
            'energy': 'foobar',
            'flowrate': 'foobar',
            'frequency': 'foobar',
            'humidity': 'foobar',
            'illuminance': 'foobar',
            'level': 'foobar',
            'linearvelocity': 'foobar',
            'percentage': 'foobar',
            'powerfactor': 'foobar',
            'power': 'foobar',
            'pressure': 'foobar',
            'resistance': 'foobar',
            'specificenthalpy': 'foobar',
            'temperature': 'foobar',
            'voltage': 'foobar',
            'volume': 'foobar',
        },
        'measurement_descriptor': {
            'absolute': 'foobar',
            'apparent': 'foobar',
            'brightness': 'foobar',
            'charge': 'foobar',
            'closed': 'foobar',
            'deadband': 'foobar',
            'dewpoint': 'foobar',
            'differential': 'foobar',
            'efficiency': 'foobar',
            'end': 'foobar',
            'extent': 'foobar',
            'failed': 'foobar',
            'lost': 'foobar',
            'match': 'foobar',
            'motion': 'foobar',
            'index': 'foobar',
            'offset': 'foobar',
            'open': 'foobar',
            'override': 'foobar',
            'position': 'foobar',
            'reactive': 'foobar',
            'relative': 'foobar',
            'run': 'foobar',
            'saturation': 'foobar',
            'speed': 'foobar',
            'start': 'foobar',
            'static': 'foobar',
            'thermal': 'foobar',
            'tilt': 'foobar',
            'wetbulb': 'foobar',
        },
        'point_type': {
            'accumulator': 'foobar',
            'alarm': 'foobar',
            'capacity': 'foobar',
            'counter': 'foobar',
            'command': 'foobar',
            'count': 'foobar',
            'label': 'foobar',
            'mode': 'foobar',
            'requirement': 'foobar',
            'sensor': 'foobar',
            'setpoint': 'foobar',
            'status': 'foobar',
            'specification': 'foobar',
            'timestamp': 'foobar',
        }
    }])


import scrapy
from scrapy.loader import ItemLoader
from itemloaders.processors import TakeFirst
from datetime import datetime
from banquethaler.items import Article

class BanquethalerSpider(scrapy.Spider):
    name = 'banquethaler'
    start_urls = ['http://www.banquethaler.ch/investment-letters']

    def parse(self, response):
        links = response.xpath('//a[@class="readmore"]/@href').getall()
        yield from response.follow_all(links, self.parse_article)

    def parse_article(self, response):
        item = ItemLoader(Article())
        item.default_output_processor = TakeFirst()

        title = "".join(response.xpath('//h1//text()').getall())
        if title:
            title = title.strip()

        date = response.xpath('//span[@class="datum"]/text()').get()
        if date:
            date = date.strip()
        else:
            return

        content = response.xpath('//article//text()').getall()
        content = [text for text in content if text.strip()]
        content = "\n".join(content).strip()

        item.add_value('title', title)
        item.add_value('date', date)
        item.add_value('link', response.url)
        item.add_value('content', content)

        return item.load_item()


import tkinter as tk
from tkinter import ttk

import serial
from serial.tools import list_ports

root = tk.Tk()
msg = tk.StringVar()

def greet():
    print(msg.get())

    serial.Serial()

def main():
    root.title("Autodrone Configurator")

    # frames
    mainframe = tk.Frame(root)
    mainframe.pack()

    tk.Entry(mainframe, textvariable=msg).grid(row=0, column=0)
    ttk.Button(mainframe, text="Click Me!", command=greet).grid(row=0, column=1)

    choicesvar = tk.StringVar(value=list_ports.comports())
    l = tk.Listbox(mainframe, listvariable=choicesvar)
    l.grid(row=1, column=0, columnspan=2)

    if(l.selection_includes(2)):
        print("Selected 2!")

    root.mainloop()

if __name__ == '__main__':
    main()


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

def solve(right, cur):
    right_len = len(right)
    # print(right)
    # print(cur)
    val = 0
    for i in range(right_len):
        if right[i] == cur[i]:
            val += 20
        else:
            if val < 10:
                continue
            else:
                val -= 10
    return val

if __name__ == '__main__':
    right = list(input())
    cur = list(input())
    ans = solve(right, cur)
    print(ans)


from __future__ import absolute_import

import logging

import globals.globals as g

from globals.six import text_type
import globals.constants as c
if c.PYQT5notPYQT4:
    from PyQt5.QtWidgets import QGraphicsView, QMenu
    from PyQt5 import QtCore
else:
    from PyQt4.QtGui import QGraphicsView, QMenu
    from PyQt4 import QtCore

logger = logging.getLogger("DxfImport.myCanvasClass")

"""
This Canvas function can be called as any class.
Since it will pretend to be, depending on the settings,
to be the canvas3d or canvas2d class
"""
def Canvas(parent=None):
    if g.config.mode3d:
        from gui.canvas3d import GLWidget
        return GLWidget(parent)
    else:
        from gui.canvas2d import MyGraphicsView
        return MyGraphicsView(parent)

def CanvasObject():
    if g.config.mode3d:
        QtVersion = QtCore.QT_VERSION_STR.split(".")
        if not (int(QtVersion[0]) >= 5 and int(QtVersion[1]) >= 4):
            raise Exception("For the 3d mode you need a PyQt version that includes a Qt version of at least 5.4.\n"
                            "Set mode3d to False in the config file, or update your PyQt version.\n"
                            "Current version found: PyQt%s (which includes Qt%s)"
                            % (QtCore.PYQT_VERSION_STR, QtCore.QT_VERSION_STR))
        from PyQt5.QtWidgets import QOpenGLWidget
        return QOpenGLWidget
    else:
        return QGraphicsView

class CanvasBase(CanvasObject()):
    def __init__(self, parent=None):
        super(CanvasBase, self).__init__(parent)

        self.isMultiSelect = False

class MyDropDownMenu(QMenu):
    def __init__(self, canvas_scene, position, clicked, offset=None):
        QMenu.__init__(self)

        self.clicked = clicked
        self.offset = offset

        self.canvas_scene = canvas_scene

        self.selectedItems = [shape for shape in canvas_scene.shapes if shape.isSelected()]

        if len(self.selectedItems) == 0:
            return

        invertAction = self.addAction(self.tr("Invert Selection"))
        disableAction = self.addAction(self.tr("Disable Selection"))
        enableAction = self.addAction(self.tr("Enable Selection"))

        self.addSeparator()

        swdirectionAction = self.addAction(self.tr("Switch Direction"))
        SetNxtStPAction = self.addAction(self.tr("Set Nearest StartPoint"))

        if g.config.machine_type == 'drag_knife':
            pass
        else:
            self.addSeparator()
            submenu1 = QMenu(self.tr('Cutter Compensation'), self)
            self.noCompAction = submenu1.addAction(self.tr("G40 No Compensation"))
            self.noCompAction.setCheckable(True)
            self.leCompAction = submenu1.addAction(self.tr("G41 Left Compensation"))
            self.leCompAction.setCheckable(True)
            self.reCompAction = submenu1.addAction(self.tr("G42 Right Compensation"))
            self.reCompAction.setCheckable(True)

            logger.debug(self.tr("The selected shapes have the following direction: %i") % (self.calcMenuDir()))
            self.checkMenuDir(self.calcMenuDir())

            self.addMenu(submenu1)

        invertAction.triggered.connect(self.invertSelection)
        disableAction.triggered.connect(self.disableSelection)
        enableAction.triggered.connect(self.enableSelection)

        swdirectionAction.triggered.connect(self.switchDirection)
        SetNxtStPAction.triggered.connect(self.setNearestStPoint)

        if g.config.machine_type == 'drag_knife':
            pass
        else:
            self.noCompAction.triggered.connect(self.setNoComp)
            self.leCompAction.triggered.connect(self.setLeftComp)
            self.reCompAction.triggered.connect(self.setRightComp)

        self.exec_(position)

    def tr(self, string_to_translate):
        """
        Translate a string using the QCoreApplication translation framework
        @param string_to_translate: a unicode string
        @return: the translated unicode string if it was possible to translate
        """
        return text_type(QtCore.QCoreApplication.translate('MyDropDownMenu',
                                                           string_to_translate))

    def calcMenuDir(self):
        """
        This method returns the direction of the selected items. If there are
        different cutter directions in the selection 0 is returned, else
        1 for Left and 2 for right.
        """
        dir = self.selectedItems[0].cut_cor
        for item in self.selectedItems:
            if not(dir == item.cut_cor):
                return -1

        return dir-40

    def checkMenuDir(self, dir):
        """
        This method checks the buttons in the Contextmenu for the direction of
        the selected items.
        @param dir: The direction of the items -1=different, 0=None, 1=left, 2=right
        """
        self.noCompAction.setChecked(False)
        self.leCompAction.setChecked(False)
        self.reCompAction.setChecked(False)

        if dir == 0:
            self.noCompAction.setChecked(True)
        elif dir == 1:
            self.leCompAction.setChecked(True)
        elif dir == 2:
            self.reCompAction.setChecked(True)

    def invertSelection(self):
        """
        This function is called by the Contextmenu of the Graphicsview.
        @purpose: Inverts the selection of all shapes.
        """
        for shape in self.canvas_scene.shapes:
            shape.setSelected(not shape.isSelected())
            g.window.TreeHandler.updateShapeSelection(shape, shape.isSelected())
        self.canvas_scene.update()

    def disableSelection(self):
        """
        Disable all shapes which are currently selected. Based on the view
        options they are not shown, or showed in a different color
        """
        for shape in self.selectedItems:
            if shape.allowedToChange:
                shape.setDisable(True)
                g.window.TreeHandler.updateShapeEnabling(shape, False)
        self.canvas_scene.update()

    def enableSelection(self):
        """
        Enable all shapes which are currently selected. Based on the view
        options they are not shown, or showed in a different color
        """
        for shape in self.selectedItems:
            if shape.allowedToChange:
                shape.setDisable(False)
                g.window.TreeHandler.updateShapeEnabling(shape, True)
        self.canvas_scene.update()

    def switchDirection(self):
        """
        Switch the Direction of all items. For example from CW direction to CCW
        """
        for shape in self.selectedItems:
            shape.reverse()
            logger.debug(self.tr('Switched Direction at Shape Nr: %i') % shape.nr)
            self.canvas_scene.repaint_shape(shape)
        self.canvas_scene.update()
        g.window.TreeHandler.prepareExportOrderUpdate()

    def setNearestStPoint(self):
        """
        Search the nearest StartPoint to the clicked position of all selected shapes.
        """
        xyForZ = {}
        for shape in self.selectedItems:
            clicked = self.clicked
            if self.offset is not None:
                z = shape.axis3_start_mill_depth
                if z not in xyForZ:
                    clicked = xyForZ[z] = self.canvas_scene.determineSelectedPosition(self.clicked, z, self.offset)
            shape.setNearestStPoint(clicked)
            self.canvas_scene.repaint_shape(shape)
        self.canvas_scene.update()
        g.window.TreeHandler.prepareExportOrderUpdate()

    def setNoComp(self):
        """
        Sets the compensation to 40, which is none, for the selected shapes.
        """
        for shape in self.selectedItems:
            shape.cut_cor = 40
            logger.debug(self.tr('Changed Cutter Correction to None for shape: %i') % shape.nr)
            self.canvas_scene.repaint_shape(shape)
        self.canvas_scene.update()

    def setLeftComp(self):
        """
        Sets the compensation to 41, which is Left, for the selected shapes.
        """
        for shape in self.selectedItems:
            shape.cut_cor = 41
            logger.debug(self.tr('Changed Cutter Correction to left for shape: %i') % shape.nr)
            self.canvas_scene.repaint_shape(shape)
        self.canvas_scene.update()

    def setRightComp(self):
        """
        Sets the compensation to 42, which is Right, for the selected shapes.
        """
        for shape in self.selectedItems:
            shape.cut_cor = 42
            logger.debug(self.tr('Changed Cutter Correction to right for shape: %i') % shape.nr)
            self.canvas_scene.repaint_shape(shape)
        self.canvas_scene.update()


import pandas as pd
import numpy as np
from sklearn import neural_network
from sklearn import preprocessing
df_ip=pd.read_csv('onset_ip.csv')
df_op=pd.read_csv('onset_op.csv')

print(df_ip.T.head())
print(df_op.head())
print(len(df_op))
print(len(df_ip))
print(len(df_ip.T))
ip_frame=[]

##### Step 1 : Data Preprocessing
print(len(df_ip.T[1:37]))
for i in range(0,43):
    temp=np.array(df_ip.T[(i)*12:i*12+36].values)
    temp=temp.ravel()
    ip_frame.append(temp)

print(len(ip_frame[1:30]))
print(len(ip_frame))
#print(ip_frame)
df_op_a=np.asarray(df_op)
#print(df_op_a)

df_op_ap=preprocessing.scale(df_op_a)
print(df_op_ap)
clf=neural_network.MLPRegressor(random_state=0,hidden_layer_sizes=500,activation='tanh',max_iter=500)
clf.fit(ip_frame[1:30],df_op_a[1:30])
print(clf.score(ip_frame[1:30],df_op_a[1:30]))

#### Step 2: implementing a predictor

from sklearn import ensemble

clf2=ensemble.AdaBoostRegressor(random_state=0)
clf2.fit(ip_frame[1:30],df_op_a[1:30])

print(clf2.score(ip_frame[1:30],df_op_a[1:30]))
pred_train=clf2.predict(ip_frame[1:30])
pred=clf2.predict(ip_frame[30:40])
print(pred)
print(df_op_a[30:-1])
#pred_train=clf2.predict(input.T[0:384])

#### Step 3:   Plotting the results
import matplotlib.pyplot as plt
plt.figure(1)
plt.plot(range(0,len(ip_frame[30:40])),pred,'red',range(0,len(ip_frame[30:40])),df_op_a[30:40],'blue')
plt.figure(2)
plt.plot(range(0,len(df_op_a[1:30])),pred_train,'red',range(0,len(df_op_a[1:30])),df_op_a[1:30],'blue')
plt.show()


import pickle
import pandas as pd
from bs4 import BeautifulSoup
import requests
import html5lib
import tkinter as tk

# Functions
def load_model():
    model_filename = 'commute_estimator_model.pkl'
    with open(model_filename, 'rb') as file:
        model = pickle.load(file)
    return model

def load_ct():
    ct_filename = 'commute_estimator_ct.pkl'
    with open(ct_filename, 'rb') as file:
        ct = pickle.load(file)
    return ct

def get_current_weather():
    URL = "https://www.timeanddate.com/weather/canada/calgary"
    r = requests.get(URL)

    soup = BeautifulSoup(r.content, 'html5lib')
    qlook = soup.find('div', attrs={'id': 'qlook'})
    text = qlook.find_all('p')[1].text
    weather = text.split()
    hi = weather[4]
    lo = weather[6]
    mean = (int(hi) + int(lo)) / 2
    wind_s = weather[8]

    default_values = {
        'Max Temp (°C)': hi,
        'Min Temp (°C)': lo,
        'Mean Temp (°C)': mean,
        'Wind Speed (km/h)': wind_s,
    }

    return default_values

def get_current_weather_and_set_defaults():
    default_values = get_current_weather()
    max_temp_entry.delete(0, tk.END)
    max_temp_entry.insert(0, default_values['Max Temp (°C)'])
    min_temp_entry.delete(0, tk.END)
    min_temp_entry.insert(0, default_values['Min Temp (°C)'])
    mean_temp_entry.delete(0, tk.END)
    mean_temp_entry.insert(0, default_values['Mean Temp (°C)'])
    wind_spd_entry.delete(0, tk.END)
    wind_spd_entry.insert(0, default_values['Wind Speed (km/h)'])

def run_model():
    model = load_model()
    ct = load_ct()

    wind_dir_dict = {'N':0, 'NE': 4.5, 'E':9, 'SE':14.5, 'S':18, 'SW':22.5, 'W':27, 'NW':31.5}
    commute_dir = 'northbound' if commute_dir_var.get() else 'southbound'
    max_temp = max_temp_entry.get()
    min_temp = min_temp_entry.get()
    mean_temp = mean_temp_entry.get()
    rain = rain_entry.get()
    snow = snow_entry.get()
    precip = precip_entry.get()
    snowg = snow_on_gnd_entry.get()
    wind_dir = selected_wind_direction.get()
    wind_spd = wind_spd_entry.get()

    new_data = pd.DataFrame({
        'direction': [commute_dir],
        'Max Temp (°C)': [float(max_temp)],
        'Min Temp (°C)': [float(min_temp)],
        'Mean Temp (°C)': [float(mean_temp)],
        'Total Rain (mm)': [float(rain)],
        'Total Snow (cm)': [float(snow)],
        'Total Precip (mm)': [float(precip)],
        'Snow on Grnd (cm)': [float(snowg)],
        'Dir of Max Gust (10s deg)': [wind_dir_dict[wind_dir]],
        'Spd of Max Gust (km/h)': [float(wind_spd)]
    })

    transformed_new_data = ct.transform(new_data)
    prediction = model.predict(transformed_new_data)

    if new_data['direction'].values[0] == 'northbound':
        text = f"It will take {int(prediction//60)}min, {int(prediction%60)}sec to get to school."
    else:
        text = f"It will take {int(prediction//60)}min, {int(prediction%60)}sec to get home."

    output = tk.Label(root, text=text)
    global grid_depth
    output.grid(row=16, column=0, columnspan=3, pady=10)

# Initializing window
root = tk.Tk()
root.title("Commute Predictor")

# Checkbox variables
commute_dir_var = tk.IntVar()
selected_wind_direction = tk.StringVar()

# Setting up grid in GUI
commute_dir_label = tk.Label(root, text='Commute Direction').grid(row=0, column=0, padx=5, pady=5)
northbound_checkbox = tk.Checkbutton(root, text='Northbound', variable=commute_dir_var, onvalue=1, offvalue=0)
southbound_checkbox = tk.Checkbutton(root, text='Southbound', variable=commute_dir_var, onvalue=0, offvalue=1)
northbound_checkbox.grid(row=0, column=1, padx=5, pady=5, sticky="w")
southbound_checkbox.grid(row=1, column=1, padx=5, pady=5, sticky="w")

get_weather_button = tk.Button(root, text="Get Current Weather", command=get_current_weather_and_set_defaults)
get_weather_button.grid(row=2, column=0, columnspan=3, pady=10)

max_temp_label = tk.Label(root, text='Max Temp (°C)').grid(row=3, column=0, padx=5, pady=5)
max_temp_entry = tk.Entry(root)
max_temp_entry.grid(row=3, column=1, columnspan=2, padx=5, pady=5, sticky="w")

min_temp_label = tk.Label(root, text='Min Temp (°C)').grid(row=4, column=0, padx=5, pady=5)
min_temp_entry = tk.Entry(root)
min_temp_entry.grid(row=4, column=1, columnspan=2, padx=5, pady=5, sticky="w")

mean_temp_label = tk.Label(root, text='Mean Temp (°C)').grid(row=5, column=0, padx=5, pady=5)
mean_temp_entry = tk.Entry(root)
mean_temp_entry.grid(row=5, column=1, columnspan=2, padx=5, pady=5, sticky="w")

rain_label = tk.Label(root, text='Total Rain (mm)').grid(row=6, column=0, padx=5, pady=5)
rain_entry = tk.Entry(root)
rain_entry.grid(row=6, column=1, columnspan=2, padx=5, pady=5, sticky="w")

snow_label = tk.Label(root, text='Total Snow (cm)').grid(row=7, column=0, padx=5, pady=5)
snow_entry = tk.Entry(root)
snow_entry.grid(row=7, column=1, columnspan=2, padx=5, pady=5, sticky="w")

precip_label = tk.Label(root, text='Total Precip (mm)').grid(row=8, column=0, padx=5, pady=5)
precip_entry = tk.Entry(root)
precip_entry.grid(row=8, column=1, columnspan=2, padx=5, pady=5, sticky="w")

snow_on_gnd_label = tk.Label(root, text='Snow on Ground (cm)').grid(row=9, column=0, padx=5, pady=5)
snow_on_gnd_entry = tk.Entry(root)
snow_on_gnd_entry.grid(row=9, column=1, columnspan=2, padx=5, pady=5, sticky="w")

wind_spd_label = tk.Label(root, text='Wind Speed (km/h)').grid(row=10, column=0, padx=5, pady=5)
wind_spd_entry = tk.Entry(root)
wind_spd_entry.grid(row=10, column=1, columnspan=2, padx=5, pady=5, sticky="w")

wind_dir_label = tk.Label(root, text='Wind Direction').grid(row=11, column=0, padx=5, pady=5)

N_button = tk.Radiobutton(root, text='N', variable=selected_wind_direction, value='N')
N_button.grid(row=11, column=1, padx=1, pady=5, sticky="w")
NE_button = tk.Radiobutton(root, text='NE', variable=selected_wind_direction, value='NE')
NE_button.grid(row=11, column=2, padx=1, pady=5, sticky="w")

E_button = tk.Radiobutton(root, text='E', variable=selected_wind_direction, value='E')
E_button.grid(row=12, column=1, padx=1, pady=5, sticky="w")
SE_button = tk.Radiobutton(root, text='SE', variable=selected_wind_direction, value='SE')
SE_button.grid(row=12, column=2, padx=1, pady=5, sticky="w")

S_button = tk.Radiobutton(root, text='S', variable=selected_wind_direction, value='S')
S_button.grid(row=13, column=1, padx=1, pady=5, sticky="w")
SW_button = tk.Radiobutton(root, text='SW', variable=selected_wind_direction, value='SW')
SW_button.grid(row=13, column=2, padx=1, pady=5, sticky="w")

W_button = tk.Radiobutton(root, text='W', variable=selected_wind_direction, value='W')
W_button.grid(row=14, column=1, padx=1, pady=5, sticky="w")
NW_button = tk.Radiobutton(root, text='NW', variable=selected_wind_direction, value='NW')
NW_button.grid(row=14, column=2, padx=1, pady=5, sticky="w")

# Button which calls run_model()
calc_button = tk.Button(root, text="Calculate Commute", command=lambda: run_model())
calc_button.grid(row=15, column=0, columnspan=3, pady=10)

# fit window to contents
root.update_idletasks()

root.mainloop()


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import sklearn.preprocessing
class FocalLoss(nn.Module):
    def __init__(self, gamma=0, alpha=None, size_average=True):
        super(FocalLoss, self).__init__()
        self.gamma = gamma
        self.alpha = alpha
        if isinstance(alpha,(float,int)): self.alpha = torch.Tensor([alpha,1-alpha])
        if isinstance(alpha,list): self.alpha = torch.Tensor(alpha)
        self.size_average = size_average

    def forward(self, input, target):
        if input.dim()>2:
            input = input.view(input.size(0),input.size(1),-1)  # N,C,H,W => N,C,H*W
            input = input.transpose(1,2)    # N,C,H*W => N,H*W,C
            input = input.contiguous().view(-1,input.size(2))   # N,H*W,C => N*H*W,C
        target = target.view(-1,1)

        logpt = F.log_softmax(input)
        logpt = logpt.gather(1,target)
        logpt = logpt.view(-1)
        pt = Variable(logpt.data.exp())

        if self.alpha is not None:
            if self.alpha.type()!=input.data.type():
                self.alpha = self.alpha.type_as(input.data)
            at = self.alpha.gather(0,target.data.view(-1))
            logpt = logpt * Variable(at)

        loss = -1 * (1-pt)**self.gamma * logpt
        if self.size_average: return loss.mean()
        else: return loss.sum()

class LabelSmoothing(nn.Module):
    """NLL loss with label smoothing.
    """
    def __init__(self, smoothing=0.0):
        """Constructor for the LabelSmoothing module.
        :param smoothing: label smoothing factor
        """
        super(LabelSmoothing, self).__init__()
        self.confidence = 1.0 - smoothing
        self.smoothing = smoothing

    def forward(self, x, target):
        logprobs = torch.nn.functional.log_softmax(x, dim=-1)
        nll_loss = -logprobs.gather(dim=-1, index=target.unsqueeze(1))
        nll_loss = nll_loss.squeeze(1)
        smooth_loss = -logprobs.mean(dim=-1)
        loss = self.confidence * nll_loss + self.smoothing * smooth_loss
        return loss.mean()

def binarize(T, nb_classes):
    T = T.cpu().numpy()

    T = sklearn.preprocessing.label_binarize(
        T, classes = range(0, nb_classes)
    )
    T = torch.FloatTensor(T).cuda()
    return T

def l2_norm(input):
    input_size = input.size()
    buffer = torch.pow(input, 2)
    normp = torch.sum(buffer, 1).add_(1e-12)
    norm = torch.sqrt(normp)
    _output = torch.div(input, norm.view(-1, 1).expand_as(input))
    output = _output.view(input_size)
    return output

class Adaptive_Proxy_Anchor(torch.nn.Module):
    def __init__(self, nb_classes, sz_embed, config,  mrg=0.1, alpha=32, scale_margin=1):
        torch.nn.Module.__init__(self)
        # Proxy Anchor Initialization
        self.proxies = torch.nn.Parameter(torch.randn(nb_classes, sz_embed).to(config.device))
        nn.init.kaiming_normal_(self.proxies, mode='fan_out')
        self.mrg = torch.nn.Parameter(torch.tensor([mrg], requires_grad=True, device=config.device,
                                                   dtype=torch.double))
        self.nb_classes = nb_classes
        self.sz_embed = sz_embed
        self.alpha = alpha
        self.scale_margin = scale_margin

    def forward(self, X, T):
        P = self.proxies

        cos = F.linear(l2_norm(X), l2_norm(P))  # Calcluate cosine similarity
        P_one_hot = binarize( T = T, nb_classes = self.nb_classes)
        N_one_hot = 1 - P_one_hot

        pos_exp = torch.exp(-self.alpha * (cos - self.mrg))
        neg_exp = torch.exp(self.alpha * (cos + self.mrg))

        with_pos_proxies = torch.nonzero(P_one_hot.sum(dim = 0) != 0).squeeze(dim = 1)   # The set of positive proxies of data in the batch
        num_valid_proxies = len(with_pos_proxies)   # The number of positive proxies

        P_sim_sum = torch.where(P_one_hot == 1, pos_exp, torch.zeros_like(pos_exp)).sum(dim=0)
        N_sim_sum = torch.where(N_one_hot == 1, neg_exp, torch.zeros_like(neg_exp)).sum(dim=0)

        pos_term = torch.log(1 + P_sim_sum).sum() / num_valid_proxies
        neg_term = torch.log(1 + N_sim_sum).sum() / self.nb_classes
        loss = pos_term + neg_term + self.scale_margin*1 / (torch.mean(self.mrg))
        return loss

class Proxy_NCA(torch.nn.Module):
    def __init__(self, nb_classes, sz_embed, scale=32):
        super(Proxy_NCA, self).__init__()
        self.nb_classes = nb_classes
        self.sz_embed = sz_embed
        self.scale = scale
        self.loss_func = losses.ProxyNCALoss(num_classes = self.nb_classes, embedding_size = self.sz_embed, softmax_scale = self.scale).to(config.device)

    def forward(self, embeddings, labels):
        loss = self.loss_func(embeddings, labels)
        return loss

class MultiSimilarityLoss(torch.nn.Module):
    def __init__(self ):
        super(MultiSimilarityLoss, self).__init__()
        self.thresh = 0.5
        self.epsilon = 0.1
        self.scale_pos = 2
        self.scale_neg = 50

        self.miner = miners.MultiSimilarityMiner(epsilon=self.epsilon)
        self.loss_func = losses.MultiSimilarityLoss(self.scale_pos, self.scale_neg, self.thresh)

    def forward(self, embeddings, labels):
        hard_pairs = self.miner(embeddings, labels)
        loss = self.loss_func(embeddings, labels, hard_pairs)
        return loss

class ContrastiveLoss(torch.nn.Module):
    def __init__(self, margin=0.5, **kwargs):
        super(ContrastiveLoss, self).__init__()
        self.margin = margin
        self.loss_func = losses.ContrastiveLoss(neg_margin=self.margin)

    def forward(self, embeddings, labels):
        loss = self.loss_func(embeddings, labels)
        return loss

class TripletLoss(torch.nn.Module):
    def __init__(self, margin=0.1, **kwargs):
        super(TripletLoss, self).__init__()
        self.margin = margin
        self.miner = miners.TripletMarginMiner(margin, type_of_triplets = 'semihard')
        self.loss_func = losses.TripletMarginLoss(margin = self.margin)

    def forward(self, embeddings, labels):
        semi_hard_pairs = self.miner(embeddings, labels)
        loss = self.loss_func(embeddings, labels, semi_hard_pairs)
        return loss

class NPairLoss(torch.nn.Module):
    def __init__(self, l2_reg=0):
        super(NPairLoss, self).__init__()
        self.l2_reg = l2_reg
        self.loss_func = losses.NPairLoss(l2_reg_weight=self.l2_reg, normalize_embeddings=False)

    def forward(self, embeddings, labels):
        loss = self.loss_func(embeddings, labels)
        return loss


import sys,os,pytest,filecmp,shutil,pytest
main_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(main_dir,'..'))
import model
import itertools
import numpy as np

def test_linearExpansionModel():
    """This function tests that linearExpansion connects is working properly."""
    #This portions test to make sure the code fails with n values that cause non-integer steps
    with pytest.raises(SystemExit) as output:
        model.PlumeModel.linearExpansionModel(14)
        assert output.type == SystemExit
        assert output.value.code == 42

    #Manually check that it produces correct adj matrix otherwise
    n=10
    connects = model.PlumeModel.linearExpansionModel(n).connects
    ones = [(0,1),(0,2),(1,3),(1,4),(1,5),(2,3),(2,4),(2,5)]
    ones += [(3,6),(3,7),(3,8),(3,9),(4,6),(4,7),(4,8),(4,9),(5,6),(5,7),(5,8),(5,9)]
    ones += [(n,0),(n,1),(n,2),(n,3),(n,5),(n,6),(n,9)]
    trange = range(connects.shape[0])
    for i in trange:
        for j in trange:
            if (i,j) in ones:
                assert connects[i,j] == 1
            else:
                assert connects[i,j] == 0

def test_gridModel():
    """Use this function to test grid model connects matrix."""
    #Manually check that it produces correct adj matrix otherwise
    n = m = 3
    connects = model.PlumeModel.gridModel(n=n,m=m).connects
    ones = np.zeros(connects.shape)
    diag=4
    ones[0,1:diag] = 1
    diagStart = diag
    for i in range(1,int(n*m-2)):
        ones[i,diagStart] = 1
        diagStart+=1
    for ext in [0,1,3,4,6,7,9]:
        ones[-1,ext] = 1
    trange = range(connects.shape[0])
    for i in trange:
        for j in trange:
            assert connects[i,j] == ones[i,j]

def test_simpleModel():
    """Use this function to test grid model connects matrix."""
    #Manually check that it produces correct adj matrix otherwise
    n = m = 3
    connects = model.PlumeModel.simpleModel().connects
    ones = np.zeros(connects.shape)
    for i,row in enumerate(connects):
        for j,val in enumerate(row):
            if i == 1 and j == 0:
                assert connects[i,j] == 1
            else:
                assert connects[i,j] == 0

def test_connectReactors():
    """Use this function to test the mass flow connections"""

def test_createReactors():
    """Use this function to test creation of reactors."""

def runTests():
    """Use this function to run pytests."""
    pytest.main([__file__,"--verbose"])


"""Prometheus middleware."""

import time
from typing import Tuple

from opentelemetry import trace
from prometheus_client import Counter, Gauge, Histogram
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import Match
from starlette.status import HTTP_500_INTERNAL_SERVER_ERROR
from starlette.types import ASGIApp

from app.internal.pkg.middlewares.handle_http_exceptions import (
    handle_api_exceptions,
    handle_internal_exception,
)
from app.pkg.models.base import BaseAPIException
from app.pkg.models.types.fastapi import FastAPITypes

__all__ = ["PrometheusMiddleware"]

class PrometheusMiddleware(BaseHTTPMiddleware):
    app: FastAPITypes.FastAPIInstance

    __FILTER_UNHANDLED_PATHS: bool
    __app_name: str

    __INFO = Gauge("fastapi_app_info", "FastAPI application information.", ["app_name"])
    __REQUESTS = Counter(
        "fastapi_requests_total",
        "Total count of requests by method and path.",
        ["method", "path", "app_name"],
    )
    __RESPONSES = Counter(
        "fastapi_responses_total",
        "Total count of responses by method, path and status codes.",
        ["method", "path", "status_code", "app_name"],
    )
    __REQUESTS_PROCESSING_TIME = Histogram(
        "fastapi_requests_duration_seconds",
        "Histogram of requests processing time by path (in seconds)",
        ["method", "path", "app_name"],
    )
    __EXCEPTIONS = Counter(
        "fastapi_exceptions_total",
        "Total count of exceptions raised by path and exception type",
        ["method", "path", "exception_type", "app_name"],
    )
    __REQUESTS_IN_PROGRESS = Gauge(
        "fastapi_requests_in_progress",
        "Gauge of requests by method and path currently being processed",
        ["method", "path", "app_name"],
    )

    def __init__(
        self,
        app: ASGIApp,
        app_name: str = "api",
        filter_unhandled_paths: bool = True,
    ) -> None:
        super().__init__(app)
        self.__app_name = app_name
        self.__INFO.labels(app_name=self.__app_name).inc()
        self.__FILTER_UNHANDLED_PATHS = filter_unhandled_paths

    async def dispatch(
        self,
        request: Request,
        call_next: RequestResponseEndpoint,
    ) -> Response:
        method = request.method
        path_template, is_handled_path = self.__get_path_template(request)

        if self.__is_path_filtered(is_handled_path):
            return await call_next(request)

        self.__REQUESTS_IN_PROGRESS.labels(
            method=method,
            path=path_template,
            app_name=self.__app_name,
        ).inc()
        self.__REQUESTS.labels(
            method=method,
            path=path_template,
            app_name=self.__app_name,
        ).inc()
        before_time = time.perf_counter()
        status_code = HTTP_500_INTERNAL_SERVER_ERROR
        try:
            response = await call_next(request)
        except (BaseAPIException, Exception) as e:
            self.__EXCEPTIONS.labels(
                method=method,
                path=path_template,
                exception_type=type(e).__name__,
                app_name=self.__app_name,
            ).inc()
            if isinstance(e, BaseAPIException):
                return handle_api_exceptions(request=request, exc=e)
            return handle_internal_exception(request=request, exc=e)
        else:
            status_code = response.status_code
            after_time = time.perf_counter()
            span = trace.get_current_span()
            trace_id = trace.format_trace_id(span.get_span_context().trace_id)

            self.__REQUESTS_PROCESSING_TIME.labels(
                method=method,
                path=path_template,
                app_name=self.__app_name,
            ).observe(after_time - before_time, exemplar={"TraceID": trace_id})
        finally:
            self.__RESPONSES.labels(
                method=method,
                path=path_template,
                status_code=status_code,
                app_name=self.__app_name,
            ).inc()
            self.__REQUESTS_IN_PROGRESS.labels(
                method=method,
                path=path_template,
                app_name=self.__app_name,
            ).dec()

        return response

    @staticmethod
    def __get_path_template(request: Request) -> Tuple[str, bool]:
        for route in request.app.routes:
            match, child_scope = route.matches(request.scope)
            if match == Match.FULL:
                return route.path, True

        return request.url.path, False

    def __is_path_filtered(self, is_handled_path: bool) -> bool:
        return self.__FILTER_UNHANDLED_PATHS and not is_handled_path


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

import os
import re
import sys
from setuptools import setup, find_packages

name = 'log'
package = 'log'
description = 'because you deserve better than log4j'
url = 'https://github.com/vforgione/log'
author = 'Vince Forgione'
author_email = 'vince.4gione@icloud.com'
license = 'MIT'

install_requires = [
    'PyYAML',
    'six'
]
if sys.version_info < (3, 4, 0):
    install_requires.append('enum34')

timezone_requires = install_requires + [
    'arrow',
]

dev_requires = timezone_requires + [
    'pytest',
    'pytest-cov',
    'capturer',
    'coveralls',
    'flake8',
    'sphinx',
    'sphinx-autobuild',
    'sphinx-rtd-theme',
]

classifiers = [
    'Development Status :: 5 - Production/Stable',
    'Environment :: Console',
    'Environment :: Web Environment',
    'Intended Audience :: Developers',
    'Intended Audience :: Information Technology',
    'Intended Audience :: System Administrators',
    'License :: OSI Approved :: MIT License',
    'Natural Language :: English',
    'Operating System :: MacOS',
    'Operating System :: POSIX',
    'Programming Language :: Python :: 3.4',
    'Programming Language :: Python :: 3.5',
]

def get_version(pkg):
    init_py = open(os.path.join(pkg, '__version__.py')).read()
    return re.search("^__version__ = ['\"]([^'\"]+)['\"]", init_py, re.MULTILINE).group(1)

setup(
    name=name,
    version=get_version(package),
    url=url,
    license=license,
    description=description,
    author=author,
    author_email=author_email,
    classifiers=classifiers,
    packages=find_packages(exclude=['*tests*']),
    install_requires=install_requires,
    extras_require={
        'timezone': timezone_requires,
        'dev': dev_requires,
    },
)


from pymongo import MongoClient
from time import strftime
import re
from django.http import JsonResponse
from pandas import Series

def main(mydb):
    '''
    获取各种类型下的邮件
    '''
    time_list = []
    myThemeRiver = mydb.theme_river
    myInnerCollection = mydb.inner_email
    myOuterCollection = mydb.outer_email
    # inner_reader = myInnerCollection.aggregate([{"$group":{"_id":"$Type","num":{"$sum":1},"list":{"$addToSet":"$Sent"}}}])
    inner_reader = myInnerCollection.find({})
    print("处理内部邮件类型中...")
    type_list = get_email(inner_reader)
    outer_reader = myOuterCollection.find({},no_cursor_timeout = True)
    print("处理外部邮件中...")
    for row in outer_reader:
        # time_list.append(strftime("%Y-%m-%d",tuple(row["Sent"])))
        time_list.append(strftime("%Y-%m",tuple(row["Sent"])))
    ds = Series(time_list)
    counts = ds.value_counts()
    for date,count in counts.iteritems():
        type_list.append([
            date,
            count,
            "外部邮件"
        ])
    print("正在存入数据库中...")
    for row in type_list:
        myThemeRiver.insert({
            "data":row
        })

def get_email(data_reader):
    '''
    处理邮件类型
    '''
    type_list = []
    email_list = []
    for row in data_reader:
        # for i in range(row["list"].__len__()):
        #     row["list"][i] = strftime("%Y-%m-%d",tuple(row["list"][i]))
        # ds = Series(row["list"])
        # counts = ds.value_counts()
        # for date,count in counts.iteritems():
        #     type_list.append([
        #         date,
        #         count,
        #         row["_id"]
        #     ])
        email_list.append(strftime("%Y-%m",tuple(row["Sent"])))
    ds = Series(email_list)
    counts = ds.value_counts()
    for date,count in counts.iteritems():
        type_list.append([
            date,
            count,
            "内部邮件"
        ])
    return type_list

if __name__ == "__main__":
    print("开始处理数据")
    conn = MongoClient("127.0.0.1",27017)
    mydb = conn.test
    main(mydb)


import gst_streamer as gs
import cv2
#import time

indices =[0,-1,-1]
#manager = gs.Manager(indexes)
#manager.start()

peg = gs.Streamer(indices[0])

while 1:
    peg.stream()
    frame = peg.frame
    cv2.imshow("peg", frame)
    cv2.waitKey(1)


#nums = int(input())
#data = list()
# for i in range(nums):
#    data.append(int(input()))

#nums = 8
#data = list([4, 3, 6, 8, 7, 5, 2, 1])

def Solution(data, nums, result):
    stack = list()
    current = 1
    input_idx = 0

    while True:
        if len(stack) == 0:
            stack.append(current)
            current += 1
            result.append('+')
        else:
            if data[input_idx] > stack[-1]:
                stack.append(current)
                current += 1
                result.append('+')

            elif data[input_idx] == stack[-1]:
                stack.pop()
                result.append('-')
                input_idx += 1
                if input_idx == nums:
                    return True
            elif data[input_idx] < stack[-1]:
                if stack[-1] != data[input_idx]:
                    return False

def Solution_fast(num):
    count = 1
    stack = list()
    result = list()
    for i in range(num):
        data = int(input())
        while count <= data:
            stack.append(count)
            count += 1
            result.append('+')
        if stack[-1] == data:
            stack.pop()
            result.append('-')
        else:
            print('NO')
            return
    print('\n'.join(result))
    return

#result = list()
# if Solution(data, nums, result):
#    for i in result:
#        print(i)
# else:
#    print('NO')

num = int(input())
Solution_fast(num)


from project3.eulerstep import eulerstep as estep
from project3.TRstep import TRstep
from mpl_toolkits import mplot3d
import numpy as np
import scipy.sparse as sp
import matplotlib.pyplot as plt

N = 30  # interior position points
dx = 1/(N+1)  # distance between grid points
M = 186  # time points  (186 to examine cfl)
tend = 0.1
dt = tend/M  # distance between time points
xx = np.linspace(0, 1, N+2)
tt = np.linspace(0, tend, M+1)  # VARFÖR inte +2 här?

T, X = np.meshgrid(tt, xx)

# CREATE TOEPLITZ MATRIX
k = np.array([np.ones(N - 1), -2 * np.ones(N), np.ones(N - 1)])
offset = [-1, 0, 1]
Tdx = 1/dx**2 * sp.diags(k, offset).toarray()

# SOLVE THE PDE
solmat = np.zeros((N, M+1))  # initiate matrix containing the solution of PDE
gx = 10*(0.5 - abs(xx[1:-1] - 0.5))  # u(0,x) = gx (initial value)
solmat[:, 0] = gx
uold = gx

for i in range(M):
    # unew = estep(Tdx, uold, dt)  # task 1.1
    unew = TRstep(Tdx, uold, dt)  # task 1.2  # STÄMMER DENNA PLOTTEN?
    # print(unew)
    # print(unew[0])
    # print(unew.shape)
    solmat[:, i+1] = unew
    uold = unew

solmat = np.insert(solmat, [0, len(solmat)], np.zeros(M+1), 0)  # insert boundary conditions
# print(solmat)

# PLOT
fig = plt.figure(1)
ax = plt.axes(projection='3d')
ax.plot_surface(X, T, solmat, cmap='jet')  # https://medium.com/@yzhong.cs/beyond-data-scientist-3d-plots-in-python-with-examples-2a8bd7aa654b
ax.set_title('task11')
plt.show()

cfl = dt/dx**2
print(cfl)  # 0.5166..


import glob
import os
import pandas

### Combine Shop A records

# List all files from Shop A
desiredFiles = os.path.join('./midterm/data/Shop A', '*.xlsx')
filePaths = glob.glob(desiredFiles)

# Read data from each file and combine
shopA = pandas.DataFrame()
for filePath in filePaths:
    sheet = pandas.read_excel(filePath)
    shopA = pandas.concat([shopA, sheet])

# Add shop name column
shopA['Shop Name'] = 'Shop A'

# Filter columns
shopA = shopA.iloc[:, [7, 3, 4, 6, 5]]

# Rename column
shopA = shopA.rename(columns={'Laptop Brand': 'Brand', 'Laptop Model': 'Model'})

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

### Combine Shop B records

# List all files from Shop B
desiredFiles = os.path.join('./midterm/data/Shop B', '*.xlsx')
filePaths = glob.glob(desiredFiles)

# Read data from each file and combine
shopB = pandas.DataFrame()
for filePath in filePaths:
    sheet = pandas.read_excel(filePath)
    shopB = pandas.concat([shopB, sheet])

# Add shop name and model column
shopB['Shop Name'] = 'Shop B'
shopB['Model'] = 'N/A'

# Filter columns
shopB = shopB.iloc[:, [6, 3, 7, 4, 5]]

# Rename column
shopB = shopB.rename(columns={'Laptop': 'Brand'})

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

### Combine Shop C records
# List all files from Shop C
desiredFiles = os.path.join('./midterm/data/Shop C', '*.*')
filePaths = glob.glob(desiredFiles)

# Read data from each file and combine
shopC = pandas.DataFrame()
for filePath in filePaths:
    if filePath.endswith('.xlsx'):
        sheet = pandas.read_excel(filePath)
        shopC = pandas.concat([shopC, sheet])
    elif filePath.endswith('.csv'):
        sheet = pandas.read_csv(filePath)
        shopC = pandas.concat([shopC, sheet])

# Add shop name and model column
shopC['Shop Name'] = 'Shop C'
shopC['Brand'] = ''
shopC['Model'] = ''

# Split Laptop column into Brand and Model
#shopC[['Laptop', 'Model']] = shopC.Laptop.str.split('-', expand = True)
rows = []
for index, row in shopC.iterrows():
    laptop = row['Laptop']
    brand, model = laptop.split('-')
    row['Brand'] = brand
    row['Model'] = model
    rows.append(row)

shopC = pandas.DataFrame(rows)

# Filter columns
shopC = shopC.iloc[:, [6, 7, 8, 5, 4]]

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

# Combine all shops together
allRecords = pandas.concat([shopA, shopB, shopC])

# Calculate average price
averagePrice = allRecords['Price'].mean()
print('The average latop price is:', averagePrice)

# Add average below the records
averageDataFrame = pandas.DataFrame({'Model': ['Average'], 'Price': [averagePrice]})
allRecords = pandas.concat([allRecords, averageDataFrame])

# Write data to CSV file
outputFilePath = './midterm/data/Output.csv'
allRecords.to_csv(outputFilePath)


# Imports the Google Cloud client library
from google.cloud import language_v1
import os
import pickle
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/Users/jennyromero/Desktop/code/STOCKS/moodFinder-2d03c34a57da.json"

# Instantiates a client
client = language_v1.LanguageServiceClient()

titles = []
tickers = []
time = []
upvotes = []
magnitude = []
sent = []

FullList = open("wallStreetBets_hot.txt","r")
entries = FullList.read().split("|")

remainder = 0
for i in range(len(entries)):
    if i%2 == remainder:
        TUN = entries[i].split("\t")
        if len(TUN) == 4:
            time.append(TUN[0])
            tickers.append(TUN[1])
            upvotes.append(int(TUN[2]))
            titles.append(TUN[3])
        else:
            if remainder == 0:
                remainder = 1
            else:
                remainder = 0

for i in range(len(titles)):
    # The text to analyze
    text = titles[i]
    up = upvotes[i]

    document = language_v1.Document(content=text, type_=language_v1.Document.Type.PLAIN_TEXT)

    # Detects the sentiment of the text
    sentiment = client.analyze_sentiment(request={'document': document}).document_sentiment
    sc = sentiment.score
    mag = sentiment.magnitude

    sent.append(sc)
    magnitude.append(mag)

    print("Text: {}".format(text))
    print("Sentiment: {}, {}".format(sc, mag))

# titles = []
# tickers = []
# time = []
# upvotes = []
# magnitude = []
# sent = []

pickle.dump( titles , open( "titles.p", "wb" ) )
pickle.dump( tickers , open( "tickers.p", "wb" ) )
pickle.dump( time, open( "time.p", "wb" ) )
pickle.dump( upvotes, open( "upvotes.p", "wb" ) )
pickle.dump( magnitude, open( "magnitude.p", "wb" ) )
pickle.dump( sent, open( "sent.p", "wb" ) )


import flwr as fl
from strategies.FedAvg import FedAvg
from strategies.FedOpt import FedOpt
from typing import Callable, Dict, List, Optional, Tuple
import uploads.model as ICU
import json
import torch
import os.path, requests
from app import contract, job_contract_address
import numpy as np
from collections import OrderedDict
from web3 import Web3
from abi import job_abi
import ipfshttpclient

DEFAULT_SERVER_ADDRESS = "[::]:8080"
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
DATA_ROOT = "uploads/testset.csv"

# Sign in to Ethereum Account
web3 = Web3(Web3.HTTPProvider('https://ropsten.infura.io/v3/ec89decf66584cd984e5f89b6467f34f'))
account = web3.eth.account.from_key('0x6b162e9dbfa762373e98b3944279f67b8fac61dc85f255da0108ebdc408af182')
web3.eth.default_account = account._address

job_abi = json.loads(job_abi)
job_contract_address = '0xD1a210292F6D37098114AFF851D747Ba6ccBAB9B'
contract = web3.eth.contract(address=job_contract_address, abi=job_abi)

def get_eval_fn(
    testset,
) -> Callable[[fl.common.Weights], Optional[Tuple[float, float]]]:
    """Return an evaluation function for centralized evaluation."""

    def evaluate(weights: fl.common.Weights) -> Optional[Tuple[float, float]]:
        model = ICU.Loader(DATA_ROOT).load_model()
        state_dict = OrderedDict(
            {k: torch.Tensor(v) for k, v in zip(model.state_dict().keys(), weights)}
        )
        model.load_state_dict(state_dict, strict=True)

        model.to(DEVICE)
        testloader = torch.utils.data.DataLoader(testset, batch_size=32, shuffle=False)

        return ICU.test(model, testloader, device=DEVICE)

    return evaluate
def get_on_fit_config_fn() -> Callable[[int], Dict[str, str]]:
    """Return a function which returns training configurations."""

    def fit_config(rnd: int) -> Dict[str, str]:
        """Return a configuration with static batch size and (local) epochs."""

        config = {
            "learning_rate": str(data['lr']),
            "batch_size": str(data["batch_size"]),
            "epochs": str(data["epoch"]),
        }
        return config

    return fit_config

def configure_flower_server():

    strategy_type = data["strategy"]
    if (strategy_type == 'datasize' or strategy_type == 'accuracy'):
        strategy = FedAvg(
            fraction_fit=float(data["fraction_fit"]),
            fraction_eval=float(data["fraction_eval"]),
            min_fit_clients=int(data["min_fit_clients"]),
            min_eval_clients=int(data["min_eval_clients"]),
            min_available_clients=int(data["min_clients"]),
            on_fit_config_fn=get_on_fit_config_fn(),
            eval_fn=None,
            on_evaluate_config_fn=None,
            accept_failures=bool(data["failure"]),
            mode=strategy_type,
            model=ICU
            # eval_fn=get_eval_fn(testloader)
            # Minimum number of clients that need to be connected to the server before a training round can start
        )
    else:
        model = ICU.Loader(DATA_ROOT).load_model()
        distribution = data["distr"]
        for name, param in model.named_parameters():
            if distribution == 'normal':
                torch.nn.init.normal_(param, mean=float(data["mean"]), std=float(data["std"]))
            elif distribution == 'uniform':
                torch.nn.init.uniform_(param, a=float(data["lb"]), b=float(data["ub"]))
            else:
                if 'bias' not in name:
                    if distribution == 'xuniform':
                        torch.nn.init.xavier_uniform_(param, gain=float(data["gain"]))
                    if distribution == 'xnormal':
                        torch.nn.init.xavier_normal_(param, gain=float(data["gain"]))
                    if distribution == 'kuniform':
                        torch.nn.init.kaiming_uniform_(param, a=float(0), mode=data["fan"], nonlinearity=data["linear"])
                    if distribution == 'knormal':
                        torch.nn.init.kaiming_normal_(param, a=float(0), mode=data["fan"], nonlinearity=data["linear"])
                else:
                    torch.nn.init.zeros_(param)
        strategy = FedOpt(
            fraction_fit=float(data["fraction_fit"]),
            fraction_eval=float(data["fraction_eval"]),
            min_fit_clients=int(data["min_fit_clients"]),
            min_eval_clients=int(data["min_eval_clients"]),
            min_available_clients=int(data["min_clients"]),
            on_fit_config_fn=get_on_fit_config_fn(),
            eval_fn=None,
            on_evaluate_config_fn=None,
            accept_failures=bool(data["failure"]),
            beta=data["beta"],
            initial_parameters=model.get_weights(),
            eta=data["slr"],
            eta_l=data["clr"],
            tau=data["da"],
            mode=strategy_type,
            model=ICU
        )

    return strategy

def launch_fl_server():
    strategy = configure_flower_server()
    fl.server.start_server("0.0.0.0:8080", config={"num_rounds": data["round"]}, strategy=strategy)

def contrib_cal(rnd):
    if os.path.exists('contrib.json'):
        f = open('contrib.json', )
        data = json.load(f)
        f.close()
        weights = []
        for i in range(rnd):
            weights.append(1/(i+1))
        m = 0
        for k, v in data.items():
            data[k]=np.average(v, weights=weights)
            m += data[k]
        for k, v in data.items():
            data[k] = v/m
        return data

    return 'No such file'

def calculate_compensations(job_id, compensation_weights):
    # Retrieve Job's bounty
    job = contract.functions.jobs(job_id).call()
    bounty = job[10]

    # Calculate compensations
    compensations = {address: int(weight*bounty) for address, weight in compensation_weights.items()}

    # Save compensations in the log ?

    return compensations

def save_weights_and_training_log(num_rounds):
    client = ipfshttpclient.connect('/dns/ipfs.infura.io/tcp/5001/https')

    model_weights_file_name = './round-'+str(num_rounds)+'-model.pt'
    model_weights_hash = client.add(model_weights_file_name)['Hash']

    log_hash = client.add('./contrib.json')['Hash']
    return model_weights_hash, log_hash

def send_compensations(job_id, compensations, model_weights_hash, log_hash):
    addresses = []
    compensation_values = []
    for address, value in compensations.items():
        addresses.append(str(address))
        compensation_values.append(int(value))

    # Create Transaction
    #  compensate(uint _id, uint [] memory _compensation, address payable [] memory _clients, string memory _resultsHash, string memory _weightsHash)
    transaction = contract.functions.compensate(int(job_id), compensation_values, addresses, log_hash, model_weights_hash) \
        .buildTransaction({'chainId': 3, 'gas': 7000000, 'nonce': web3.eth.getTransactionCount(account._address)})

    # Sign transaction with private key
    signed_txn = web3.eth.account.signTransaction(transaction, account._private_key)

    # Send transaction
    txn_hash = web3.eth.sendRawTransaction(signed_txn.rawTransaction)

    # Wait for transaction receipt
    receipt = web3.eth.wait_for_transaction_receipt(txn_hash)

    return receipt

if os.path.exists('uploads/strategy.json'):
    f = open('uploads/strategy.json', )
    data = json.load(f)
    f.close()
    if data['strategy'] != "":
        launch_fl_server()
        with open('uploads/job_id.txt', 'r') as f:
            job_id = int(f.readline())

        compensation_weights = contrib_cal(int(data["round"]))
        compensations = calculate_compensations(job_id, compensation_weights)
        model_weights_hash, log_hash = save_weights_and_training_log(int(data["round"]))
        #print(model_weights_hash)
        #print(log_hash)
        print("compensations")
        print(compensations)
        receipt = send_compensations(job_id, compensations, model_weights_hash, log_hash)
        if receipt['status'] == 0:
            print('transaction failed')
        else:
            print('transaction success')


from audioop import reverse
from platform import release
from turtle import title
from django.http import JsonResponse
from django.shortcuts import render, HttpResponse, redirect
from django.urls import reverse
from .models import Tvshow, Network
from datetime import datetime

def redirigir(request):
    return redirect('/shows')

def index(request):
    context = {
        'alltvshows': Tvshow.objects.all(),
        'allnetworks': Network.objects.all(),
    }
    return render(request, 'app/index.html', context)

def newtvshow(request):
    return render(request, 'app/formulario_new.html')

def createtvshow(request):
    if request.method == 'POST':
        if Network.objects.all().filter(name=request.POST['network']):
            print(Network.objects.all().filter(name=request.POST['network']))
            networks_query = Network.objects.all().filter(
                name=request.POST['network'])
            for query in networks_query:
                this_network = Network.objects.get(id=query.id)
                print(this_network.name)
                print(this_network.id)
                tv_show = Tvshow.objects.create(
                    title=request.POST['title'], network=this_network, releasedate=request.POST['releasedate'], description=request.POST['description'])
                print(tv_show.__dict__)
                showid = tv_show.id
            return redirect(f"/shows/{showid}")
        else:
            print(request.POST)
            create = Tvshow.objects.create(title=request.POST['title'], network=Network.objects.create(name=request.POST['network']),
                                           releasedate=request.POST['releasedate'], description=request.POST['description'])
            showid = create.id
            return redirect(f"/shows/{showid}")

def details(request, id):
    detalle = Tvshow.objects.get(id=id)
    context = {
        "detailstvshow": detalle,
    }
    return render(request, 'app/detalle.html', context)

def edit(request, id):
    if request.method == 'GET':
        editar = Tvshow.objects.get(id=id)
        network = editar.network.name
        release_date = editar.releasedate
        release_date = release_date.strftime('%Y-%m-%d')
        print(release_date)
        context = {
            "edit": editar,
            "release_date": release_date,
            "network": network
        }
    return render(request, 'app/formulario_edit.html', context)

def update(request, id):
    print(request.POST)
    update = Tvshow.objects.get(id=id)
    update_network = Network.objects.get(id=update.network.id)
    update.title = request.POST['title']
    update_network.name = request.POST['network']
    update.description = request.POST['description']
    update.releasedate = request.POST['releasedate']
    update_network.save()
    update.save()
    return redirect(f"/shows/{id}")

def destroy(request, id):
    print(request.POST)
    deleteshow = Tvshow.objects.get(id=id)
    deleteshow.delete()
    return redirect("/shows")