# ------------------------------------------------------------------------------#
#                     Author     : Nicklas Sindlev Andersen                    #
# ------------------------------------------------------------------------------#
# ------------------------------------------------------------------------------#
#               Import packages from the python standard library               #
# ------------------------------------------------------------------------------#
import random
import uuid
from datetime import datetime

# ------------------------------------------------------------------------------#
#                      Import third-party libraries: Others                    #
# ------------------------------------------------------------------------------#
from infostop import utils

from .helpers.DataPoint import DataPoint
# ------------------------------------------------------------------------------#
#                          Import local libraries/code                         #
# ------------------------------------------------------------------------------#
from .helpers.DataPointStream import DataPointStream

# ------------------------------------------------------------------------------#
#                 Import third-party libraries: Django std. lib.               #
# ------------------------------------------------------------------------------#
#
# ------------------------------------------------------------------------------#
#                 Import third-party libraries: Django extensions              #
# ------------------------------------------------------------------------------#

def rand_24_bit():
    """Returns a random 24-bit integer."""
    return random.randrange(0, 16 ** 6)

def color_dec():
    """Alias of rand_24 bit()"""
    return rand_24_bit()

def color_hex(num=rand_24_bit()):
    """Returns a 24-bit int in hex."""
    return "%06x" % num

def color_rgb(num=rand_24_bit()):
    """Returns three 8-bit numbers, one for each channel in RGB."""
    hx = color_hex(num)
    barr = bytearray.fromhex(hx)
    return (barr[0], barr[1], barr[2])

def coords_to_geojson(data):
    # feature_array_one = []
    # for datapoint in data:
    #     feature = {
    #         "type": "Feature",
    #         "properties": {
    #             "color": "#33C9EB",
    #         },
    #         "geometry": {
    #             "type": "LineString",
    #             "coordinates": (datapoint["latitude"], datapoint["longitude"]),
    #         },
    #     }
    #     feature_array_one.extend([feature])
    # line_data = {
    #     "type":"FeatureCollection",
    #     "features": feature_array_one
    # }
    feature_array_two = []
    for datapoint in data:
        feature = {
            "type": "Feature",
            "properties": {"color": "#33C9EB",},
            "geometry": {
                "type": "Point",
                "coordinates": [datapoint["longitude"], datapoint["latitude"]],
            },
        }
        feature_array_two.append(feature)
    point_data = {"type": "FeatureCollection", "features": feature_array_two}
    return point_data

# def segments_to_geojson(data):
#     # feature_array_one = []
#     # for datapoint in data:
#     #     feature = {
#     #         "type": "Feature",
#     #         "properties": {
#     #             "color": "#33C9EB",
#     #         },
#     #         "geometry": {
#     #             "type": "LineString",
#     #             "coordinates": (datapoint["latitude"], datapoint["longitude"]),
#     #         },
#     #     }
#     #     feature_array_one.extend([feature])
#     # line_data = {
#     #     "type":"FeatureCollection",
#     #     "features": feature_array_one
#     # }
#     feature_array_two = []
#     for datapoint in data:
#         feature = {
#             "type": "Feature",
#             "properties": {
#                 "color": "#33C9EB",
#             },
#             "geometry": {
#                 "type": "Point",
#                 "coordinates": [datapoint["longitude"], datapoint["latitude"]],
#             },
#         }
#         feature_array_two.append(feature)
#     point_data = {
#         "type":"FeatureCollection",
#         "features": feature_array_two
#     }
#     return point_data

def parse_date(text):
    for fmt in ["%Y-%m-%dT%H:%M:%S.%f", "%Y-%m-%dT%H:%M:%S"]:
        try:
            return datetime.strptime(text, fmt)
        except ValueError:
            pass
    raise ValueError("No valid date format found")

# def handle_datapoints(points):
#     dps = DataPointStream(
#         min_time = 300,
#         min_distance = 250,
#     )
#     for point in points:
#         time = try_parsing_date(point["external_timestamp"])
#         dp = DataPoint(
#             latitude = point["latitude"],
#             longitude = point["longitude"],
#             time = time,
#         )
#         dps.update(dp)
#     feature_array_one = []
#     for segment in dps.segments:
#         v = []; c = color_dec(); x = color_hex(c)
#         for dp in segment:
#             v.append([dp.longitude, dp.latitude])
#         feature = {
#             "type": "Feature",
#             "properties": {
#                 "color": "#" + str(x), # "#33C9EB",
#             },
#             "geometry": {
#                 "type": "LineString",
#                 "coordinates": v,
#             },
#         }
#         feature_array_one.append(feature)
#     line_data = {
#         "type":"FeatureCollection",
#         "features": feature_array_one
#     }
#     return line_data

def handle_datapoints(points):
    dps = DataPointStream(min_time=600, min_distance=300,)
    new_points = []
    for i in range(1, len(points)):
        time = parse_date(points[i]["external_timestamp"])
        # dp0 = DataPoint(
        #     latitude = points[i - 1]["latitude"],
        #     longitude = points[i]["longitude"],
        #     time = time,
        # )
        # new_points.append(dp0)
        dp1 = DataPoint(
            latitude=points[i]["latitude"],
            longitude=points[i]["longitude"],
            time=time,
        )
        new_points.append(dp1)

        # dp2 = DataPoint(
        #     latitude = points[i]["latitude"],
        #     longitude = points[i - 1]["longitude"],
        #     time = time,
        # )
        # new_points.append(dp2)

    for point in new_points:
        dps.update(point)
        if len(dps.tracks) > 20:
            break

    # for point in points:
    # for point in new_points:
    #     time = parse_date(point["external_timestamp"])
    #     dp = DataPoint(
    #         latitude = point["latitude"],
    #         longitude = point["longitude"],
    #         time = time,
    #     )
    #     dps.update(dp)
    dps.retrieve_clusters()
    # ppoints = dps.model._stat_coords[dps.model._stat_labels >= 0]
    # llabels = dps.model._stat_labels[dps.model._stat_labels >= 0]
    # feature_array_one = []
    # feature_array_two = render_polygons(ppoints, llabels)
    # print("True: All segements: ", len(dps.segments))

    feature_array_one = []
    feature_array_two = []
    print("True: All segements: ", len(dps.tracks))

    for segment in dps.tracks:
        v = []
        c = color_dec()
        x = color_hex(c)
        lon_min = 0  # np.amin([dp.longitude for dp in segment])
        lat_min = 0  # np.amin([dp.latitude for dp in segment])
        lon_max = 0  # np.amax([dp.longitude for dp in segment])
        lat_max = 0  # np.amax([dp.latitude for dp in segment])
        decoded_geohash_lon_min = 0  # np.amin(np.array([dp.decoded_geohash_lon for dp in segment]).astype(np.float))
        decoded_geohash_lat_min = 0  # np.amin(np.array([dp.decoded_geohash_lat for dp in segment]).astype(np.float))
        dx_euclidean_min = 0  # np.amin([dp.dx_euclidean for dp in segment])
        dx_manhatten_min = 0  # np.amin([dp.dx_manhatten for dp in segment])
        dt_min = 0  # np.amin([dp.dt for dp in segment])
        acceleration_min = 0  # np.amin([dp.acceleration for dp in segment])
        velocity_min = 0  # np.amin([dp.velocity for dp in segment])
        compass_bearing_min = (
            0  #  np.amin([dp.compass_bearing for dp in segment])
        )
        # print("Segment length: ", len(segment))
        if len(segment.datapoints) > 0:
            segment.datapoints[0].get_unix_timestamp()
            datapoint_template_dict = {
                "meta": {"id": str(uuid.uuid4()),},
                "raw_data": {
                    "longitude": segment.datapoints[0].longitude,
                    "latitude": segment.datapoints[0].latitude,
                    # "time": str(segment.datapoints[0].time),
                    # "timestamp": segment.datapoints[0].get_unix_timestamp(),
                    "decoded_geohash_lon": 0,  # segment.datapoints[0].decoded_geohash_lon,
                    "decoded_geohash_lat": 0,  # segment.datapoints[0].decoded_geohash_lat,
                    "dx_euclidean": segment.datapoints[0].dx_euclidean,
                    "dx_manhatten": segment.datapoints[0].dx_manhatten,
                    "dt": segment.datapoints[0].dt,
                    "acceleration": segment.datapoints[0].acceleration,
                    "velocity": segment.datapoints[0].velocity,
                    "compass_bearing": segment.datapoints[0].compass_bearing,
                },
                "regularized_data": {
                    "longitude": segment.datapoints[0].longitude - lon_min,
                    "latitude": segment.datapoints[0].latitude - lat_min,
                    # "time": str(segment.datapoints[0].time),
                    # "timestamp": 0,
                    "decoded_geohash_lon": 0,  # float(segment.datapoints[0].decoded_geohash_lon) - decoded_geohash_lon_min,
                    "decoded_geohash_lat": 0,  # float(segment.datapoints[0].decoded_geohash_lat) - decoded_geohash_lat_min,
                    "dx_euclidean": segment.datapoints[0].dx_euclidean
                    - dx_euclidean_min,
                    "dx_manhatten": segment.datapoints[0].dx_manhatten
                    - dx_manhatten_min,
                    "dt": segment.datapoints[0].dt - dt_min,
                    "acceleration": segment.datapoints[0].acceleration
                    - acceleration_min,
                    "velocity": segment.datapoints[0].velocity - velocity_min,
                    "compass_bearing": segment.datapoints[0].compass_bearing
                    - compass_bearing_min,
                },
            }
            for key in datapoint_template_dict["raw_data"]:
                if key != "time":
                    datapoint_template_dict["raw_data"][key] = float(
                        datapoint_template_dict["raw_data"][key]
                    )
            for key in datapoint_template_dict["regularized_data"]:
                if key != "time":
                    datapoint_template_dict["regularized_data"][key] = float(
                        datapoint_template_dict["regularized_data"][key]
                    )
            v.append(datapoint_template_dict)
            for i in range(1, len(segment.datapoints)):
                datapoint_template_dict = {
                    "meta": {"id": str(uuid.uuid4()),},
                    "raw_data": {
                        "longitude": segment.datapoints[i].longitude,
                        "latitude": segment.datapoints[i].latitude,
                        # "time": str(segment.datapoints[i].time),
                        # "timestamp": segment.datapoints[i].get_unix_timestamp(),
                        "decoded_geohash_lon": 0,  # float(segment.datapoints[i].decoded_geohash_lon),
                        "decoded_geohash_lat": 0,  # float(segment.datapoints[i].decoded_geohash_lat),
                        "dx_euclidean": segment.datapoints[i].dx_euclidean,
                        "dx_manhatten": segment.datapoints[i].dx_manhatten,
                        "dt": segment.datapoints[i].dt,
                        "acceleration": segment.datapoints[i].acceleration,
                        "velocity": segment.datapoints[i].velocity,
                        "compass_bearing": segment.datapoints[
                            i
                        ].compass_bearing,
                    },
                    "regularized_data": {
                        "longitude": segment.datapoints[i].longitude - lon_min,
                        "latitude": segment.datapoints[i].latitude - lat_min,
                        # "time": str(segment.datapoints[i].time),
                        # "timestamp": segment.datapoints[i].get_unix_timestamp() - start_time,
                        "decoded_geohash_lon": 0,  # float(segment.datapoints[i].decoded_geohash_lon) - decoded_geohash_lon_min,
                        "decoded_geohash_lat": 0,  # float(segment.datapoints[i].decoded_geohash_lat) - decoded_geohash_lat_min,
                        "dx_euclidean": segment.datapoints[i].dx_euclidean
                        - dx_euclidean_min,
                        "dx_manhatten": segment.datapoints[i].dx_manhatten
                        - dx_manhatten_min,
                        "dt": segment.datapoints[i].dt - dt_min,
                        "acceleration": segment.datapoints[i].acceleration
                        - acceleration_min,
                        "velocity": segment.datapoints[i].velocity
                        - velocity_min,
                        "compass_bearing": segment.datapoints[i].compass_bearing
                        - compass_bearing_min,
                    },
                }
                v.append(datapoint_template_dict)
                for key in datapoint_template_dict["raw_data"]:
                    if key != "time":
                        datapoint_template_dict["raw_data"][key] = float(
                            datapoint_template_dict["raw_data"][key]
                        )
                for key in datapoint_template_dict["regularized_data"]:
                    if key != "time":
                        datapoint_template_dict["regularized_data"][
                            key
                        ] = float(
                            datapoint_template_dict["regularized_data"][key]
                        )
            segment_template_dict = {
                "meta": {
                    "color": "#" + str(x),
                    # "id": str(uuid.uuid4()),
                    "uuid": str(segment.uuid),
                    "min_lon": lon_min,
                    "max_lon": lon_max,
                    "min_lat": lat_min,
                    "max_lat": lat_max,
                },
                "segment": v,
            }
            feature_array_one.append(segment_template_dict)
        else:
            pass
    print("All segments: ", len(feature_array_one))
    return feature_array_one, feature_array_two

def render_polygons(points, labels, color="#FFFFFF", opacity=0.3):
    """Render convex hulls of points in each label-group.
    Parameters
    ----------
        color : str
            Color of stop location areas.
        opacity : float (in [0, 1])
            Opacity of stop location areas.
    """

    def _style_function(feature):
        return {
            "fillColor": feature["properties"]["color"],
            "color": feature["properties"]["color"],
            "weight": 1,
            "fillOpacity": opacity,
        }

    stop_hulls = []
    for stop_idx in set(labels) - {-1}:
        p = points[labels == stop_idx]
        stop_hulls.append(utils.convex_hull(p))

    features = {
        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "properties": {"color": color},
                "geometry": {"type": "Polygon", "coordinates": []},
            }
        ],
    }

    for hull in stop_hulls:
        features["features"][0]["geometry"]["coordinates"].append(
            hull[:, ::-1].tolist()
        )
    return features


"""
树:树是一种非线性的数据结构，具有很好的层次性
二叉树的存储方式分为顺序存储和链式存储，顺序存储采用列表方式，而链式存储采用链表方式
二叉树的链式存储中，树节点包含数据域、左子链域和右子链域
"""

#定义树节点类
class Node(object):
    def __init__(self,data = -1,lchild = None,rchild = None):
        self.data = data
        self.lchild = lchild
        self.rchild = rchild
#定义二叉树类,root是Node的对象，表示根节点
class BinaryTree(object):
    def __init__(self):
        self.root = Node()
    def add(self,data):
        node = Node(data)
        if self.isEmpty():
            self.root = node
        else:
            #以列表存储二叉树，每一次都是空的列表，从根节点开始加数据，这里保证加的原则是先左后右
            queue = []
            queue.append(self.root)
            while queue:
                #将列表第一个元素移除（作为根节点），检查左右子树是否有值
                tree_node = queue.pop(0)
                if tree_node.lchild is None:
                    tree_node.lchild = node
                    return
                elif tree_node.rchild is None:
                    tree_node.rchild = node
                    return
                else:
                    #左右子树都有值，则将值加入列表，那么下一个根节点就是原本根节点的左子树了
                    queue.append(tree_node.lchild)
                    queue.append(tree_node.rchild)
    #先序遍历
    def pre_order(self,start):
        #其基本思想：根节点-左子树-右子树
        node = start
        if node is None:
            return
        print(node.data,end='')
        if node.lchild is None and node.rchild is None:
            return
        self.pre_order(node.lchild)
        self.pre_order(node.rchild)
    #中序遍历
    def in_order(self,start):
        # 其基本思想：左子树-根节点-右子树
        node = start
        if node is None:
            return
        self.in_order(node.lchild)
        print(node.data,end='')
        self.in_order(node.rchild)
    #后序遍历
    def post_order(self,start):
        # 其基本思想：左子树-右子树-根节点
        node = start
        if node is None:
            return
        self.post_order(node.lchild)
        self.post_order(node.rchild)
        print(node.data,end='')
    def isEmpty(self):
        return True if self.root.data == -1 else False
    def length(self):
        return len(self.queue)

#创建实例
if __name__ == "__main__":
    tree = BinaryTree()
    for i in range(10):
        tree.add(i)

    print('pre order:')
    tree.pre_order(tree.root)
    print('\nin order:')
    tree.in_order(tree.root)
    print('\npost order:')
    tree.post_order(tree.root)


import colorama
from colorama import Fore, Back, Style
#http://blog.csdn.net/maoersong/article/details/32714781
class A:
    def fun0(self):
        print("call normal method")
    @staticmethod
    def fun1():
        print("call staticmethod")

    @classmethod
    def fun2(cls):
        print("call classmethod")
        print("cls.__name__ is:",cls.__name__)

def main():
    a  = A()
    a.fun0()
    a.fun1()
    a.fun2()
    A.fun2()
    A.fun1()

    print(Fore.GREEN + '-'*20 + Fore.RESET+Back.RED+'demo2'+Back.RESET)

if __name__ == '__main__':
    main()


import pygame
import sys
import os
from settings import *
from level import Level

class Game:
    def __init__(self) -> None:
        pygame.init()
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        pygame.display.set_caption('Sprout land')
        self.clock = pygame.time.Clock()
        self.level = Level()

    def run(self):
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

                # exit
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_F6:
                        pygame.quit()
                        sys.exit()

            delta = self.clock.tick() / 1000
            self.level.run(delta)
            pygame.display.update()

if __name__ == '__main__':
    os.chdir(os.path.dirname(os.path.realpath(__file__)))
    game = Game()
    game.run()


#!/usr/bin/env python

# Needed for antipackage with python 2
from __future__ import absolute_import

import re
import sys

def to_camel_case(snake_str):
    if snake_str.startswith('"') and snake_str.endswith('"'):
        return snake_str
    components = snake_str.split('_')
    # We capitalize the first letter of each component except the first one
    # with the 'title' method and join them together.
    return components[0] + ''.join((x[0].upper() + x[1:]) for x in components[1:])

def convert(fname):
    with open(fname, 'r+') as f:
        content = f.readlines()
        out = []
        for line in content:
            words = re.split(r'(\s+)', line)
            for idx, w in enumerate(words):
                words[idx] = to_camel_case(w)
            out.append(''.join(words))

        f.seek(0)
        f.writelines(out)
        f.truncate()

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


import pygame

from settings import *

""" Convert a spritesheet to a single frame image.
"""
def get_spritesheet_frame(sheet, frame_nr, width, height, scale):
    image = pygame.Surface((width, height), TRANSPARENT).convert_alpha()
    image.blit(sheet, (0,0), (frame_nr*width, 0, width, height))
    image = pygame.transform.scale(image, (width*scale, height*scale))
    return image

""" Create an animation which is a list of images.
"""
def create_animation(path, n_steps, width, height, scale):
    sheet = pygame.image.load(path).convert_alpha()

    # Convert sheet to animation.
    animation_list = []
    for frame_nr in range(n_steps):
        animation_list.append(get_spritesheet_frame(sheet, frame_nr, width, height, scale))

    return animation_list


import json

from firebase_admin.auth import AuthError

from dbcls.models import User
from .fixtures import client, users

class TestSignUp:
    def test_signup_ok(self, client, users, mocker):
        mocker.patch('firebase_admin.auth.get_user')
        custom_token_mock = mocker.Mock()
        custom_token_mock.decode.return_value = 'dummy_custom_token'
        mocker.patch('firebase_admin.auth.create_custom_token').return_value = custom_token_mock

        with client:
            before_count = User.query.count()
            data = json.dumps({
                'firebase_uid': 'aabbccdd',
                'display_name': 'タピ岡',
            })
            res = client.post('/api/v1/signup', data=data, content_type='application/json')
            assert res.status_code == 201
            assert res.get_json() == {'custom_token': custom_token_mock.decode()}
            assert User.query.count() == before_count + 1

    def test_signup_not_found_on_firebase(self, client, mocker):
        get_user_mock = mocker.patch('firebase_admin.auth.get_user')
        get_user_mock.side_effect = AuthError(111, 'user not found on firebase')

        with client:
            data = json.dumps({
                'firebase_uid': 'aabbccdd',
                'display_name': 'タピ岡',
            })
            res = client.post('/api/v1/signup', data=data, content_type='application/json')
            assert res.status_code == 400
            assert res.get_json() == {'message': 'user not found on firebase'}

    def test_signup_already_exist(self, client, users, mocker):
        mocker.patch('firebase_admin.auth.get_user')

        with client:
            user = User.query.first()
            data = json.dumps({
                'firebase_uid': user.firebase_uid,
                'display_name': 'タピ岡',
            })
            res = client.post('/api/v1/signup', data=data, content_type='application/json')
            assert res.status_code == 400
            assert res.get_json() == {'message': 'already exists'}


import findspark #must to work
findspark.init()
#import and create spark session
from pyspark.sql import SparkSession
from pyspark.sql.functions import col
from sparkmeasure import StageMetrics

spark = SparkSession.builder.master("spark://john-VirtualBox:7077").appName("Erotima_10").config("spark.jars", "spark-measure_2.12-0.17.jar").getOrCreate()

#import csv

movies = spark.read.csv("Baseis/movie.csv",inferSchema =True, header=True)
ratings = spark.read.csv("Baseis/rating.csv",inferSchema =True, header=True)
tags = spark.read.csv("Baseis/tag.csv",inferSchema =True, header=True)
genome_tags = spark.read.csv("Baseis/genome_tags.csv",inferSchema =True, header=True)

#Category filter

import re
import string
unique = []
tot_genres = movies.select('genres').collect()
tot_genres = [str(row.genres) for row in tot_genres]
x = []
for i in range(len(tot_genres)):
    x.append(re.sub("[" + string.punctuation[-3] + "]", ' ',tot_genres[i]).split())

for i in x:
    for j in i:
        if (j not in unique):unique.append(j)
unique.pop(-1)
unique.pop(-1)
unique[-1] = '(no genres listed)'

#create spark metrics object
stagemetrics = StageMetrics(spark)

#start measuring performance
stagemetrics.begin()

#Erotima 10

for i in unique:
    tag_movies = movies.join(tags, movies['movieId'] == tags['movieId'], 'outer') \
        .select(movies['movieId'],"genres","tag").filter(tags.tag == "funny") \
        #.show(truncate=False)

    avg_ratings = tag_movies.join(ratings, tag_movies['movieId'] == ratings['movieId'], 'outer') \
        .select(tag_movies['movieId'],"rating").groupBy("movieId").avg("rating") \
        #.show(truncate = False)

    results = tag_movies.join(avg_ratings, tag_movies['movieId'] == avg_ratings['movieId'], 'outer') \
    .select("tag").filter(col("avg(rating)") > 3.5) \
    .filter(col("genres").like("%{0}%".format(i))).groupBy("tag").count()

    results.select(col("count").alias("{0}".format(i))).show(truncate = False)

#stop measuring performance
stagemetrics.end()

#print performance metrics
stagemetrics.print_report()

spark.stop()


import os
import json
import pandas as pd

def data_preparation(args):
    dset_path   = os.path.join(args.processed_path, args.dataset)
    full_df     = pd.read_csv(os.path.join(dset_path, 'full.csv'))
    train_df    = full_df[full_df['filter'] == 'Train'].reset_index(drop=True)
    # test_df     = pd.read_csv(os.path.join(dset_path, 'test.csv'))
    test_df     = full_df[full_df['filter'] == 'Test'].reset_index(drop=True)

    word_dict_path  = os.path.join(dset_path, 'word_dict.json')
    word_dict       = json.load(open(word_dict_path, 'r'))

    return train_df, test_df, full_df, word_dict


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

import asyncio
from pathlib import Path
import sys
import unittest

from syncer import sync

from pyppeteer.errors import NetworkError, PageError

from .base import BaseTestCase

class TestNetworkEvent(BaseTestCase):
    @sync
    async def test_request(self):
        requests = []
        self.page.on('request', lambda req: requests.append(req))
        await self.page.goto(self.url + 'empty')
        self.assertEqual(len(requests), 1)
        req = requests[0]
        self.assertEqual(req.url, self.url + 'empty')
        self.assertEqual(req.resourceType, 'document')
        self.assertEqual(req.method, 'GET')
        self.assertTrue(req.response)
        self.assertEqual(req.frame, self.page.mainFrame)
        self.assertEqual(req.frame.url, self.url + 'empty')

    @sync
    async def test_request_post(self):
        await self.page.goto(self.url + 'empty')

        from tornado.web import RequestHandler

        class PostHandler(RequestHandler):
            def post(self):
                self.write('')

        self.app.add_handlers('localhost', [('/post', PostHandler)])
        requests = []
        self.page.on('request', lambda req: requests.append(req))
        await self.page.evaluate('fetch("/post", {method: "POST", body: JSON.stringify({foo: "bar"})})')  # noqa: E501
        self.assertEqual(len(requests), 1)
        req = requests[0]
        self.assertTrue(req)
        self.assertEqual(req.postData, '{"foo":"bar"}')

    @sync
    async def test_response(self):
        responses = []
        self.page.on('response', lambda res: responses.append(res))
        await self.page.goto(self.url + 'empty')
        self.assertEqual(len(responses), 1)
        response = responses[0]
        self.assertEqual(response.url, self.url + 'empty')
        self.assertEqual(response.status, 200)
        # self.assertTrue(response.ok)
        self.assertFalse(response.fromCache)
        self.assertFalse(response.fromServiceWorker)
        self.assertTrue(response.request)
        self.assertEqual(response.securityDetails, {})

    @sync
    async def test_response_https(self):
        responses = []
        self.page.on('response', lambda res: responses.append(res))
        await self.page.goto('https://example.com/')
        self.assertEqual(len(responses), 1)
        response = responses[0]
        self.assertEqual(response.url, 'https://example.com/')
        self.assertEqual(response.status, 200)
        self.assertTrue(response.ok)
        self.assertFalse(response.fromCache)
        self.assertFalse(response.fromServiceWorker)
        self.assertTrue(response.request)
        self.assertTrue(response.securityDetails)
        self.assertEqual(response.securityDetails.protocol, 'TLS 1.2')

    @sync
    async def test_from_cache(self):
        responses = {}

        def set_response(resp):
            basename = resp.url.split('/').pop()
            responses[basename] = resp

        self.page.on('response', set_response)

        await self.page.goto(self.url + 'static/cached/one-style.html')
        await self.page.reload()

        self.assertEqual(len(responses), 2)
        self.assertEqual(responses['one-style.html'].status, 304)
        self.assertFalse(responses['one-style.html'].fromCache)
        self.assertEqual(responses['one-style.css'].status, 200)
        self.assertTrue(responses['one-style.css'].fromCache)

    @sync
    async def test_response_from_service_worker(self):
        responses = {}

        def set_response(resp):
            basename = resp.url.split('/').pop()
            responses[basename] = resp

        self.page.on('response', set_response)

        await self.page.goto(
            self.url + 'static/serviceworkers/fetch/sw.html',
            waitUntil='networkidle2',
        )
        await self.page.evaluate('async() => await window.activationPromise')
        await self.page.reload()

        self.assertEqual(len(responses), 2)
        self.assertEqual(responses['sw.html'].status, 200)
        self.assertTrue(responses['sw.html'].fromServiceWorker)
        self.assertEqual(responses['style.css'].status, 200)
        self.assertTrue(responses['style.css'].fromServiceWorker)

    @unittest.skipIf(sys.platform.startswith('msys'), 'Fails on MSYS')
    @sync
    async def test_response_body(self):
        responses = []
        self.page.on('response', lambda res: responses.append(res))
        await self.page.goto(self.url + 'static/simple.json')
        self.assertEqual(len(responses), 1)
        res = responses[0]
        self.assertTrue(res)
        self.assertEqual(await res.text(), '{"foo": "bar"}\n')
        self.assertEqual(await res.json(), {'foo': 'bar'})

    @sync
    async def test_fail_get_redirected_body(self):
        response = await self.page.goto(self.url + 'redirect1')
        redirectChain = response.request.redirectChain
        self.assertEqual(len(redirectChain), 1)
        redirected = redirectChain[0].response
        self.assertEqual(redirected.status, 302)
        with self.assertRaises(NetworkError) as cm:
            await redirected.text()
        self.assertIn(
            'Response body is unavailable for redirect response',
            cm.exception.args[0],
        )

    @unittest.skip('This test hangs')
    @sync
    async def test_not_report_body_unless_finished(self):
        await self.page.goto(self.url + 'empty')
        serverResponses = []

        from tornado.web import RequestHandler

        class GetHandler(RequestHandler):
            def get(self):
                serverResponses.append(self)
                self.write('hello ')

        self.app.add_handlers('localhost', [('/get', GetHandler)])
        pageResponse = asyncio.get_event_loop().create_future()
        finishedRequests = []
        self.page.on('response', lambda res: pageResponse.set_result(res))
        self.page.on('requestfinished', lambda: finishedRequests.append(True))

        asyncio.ensure_future(
            self.page.evaluate('fetch("./get", {method: "GET"})'))
        response = await pageResponse
        self.assertTrue(serverResponses)
        self.assertTrue(response)
        self.assertEqual(response.status, 200)
        self.assertFalse(finishedRequests)

        responseText = response.text()
        serverResponses[0].write('wor')
        serverResponses[0].finish('ld!')
        self.assertEqual(await responseText, 'hello world!')

    @sync
    async def test_request_failed(self):
        await self.page.setRequestInterception(True)

        async def interception(req):
            if req.url.endswith('css'):
                await req.abort()
            else:
                await req.continue_()

        self.page.on(
            'request', lambda req: asyncio.ensure_future(interception(req)))

        failedRequests = []
        self.page.on('requestfailed', lambda req: failedRequests.append(req))
        await self.page.goto(self.url + 'static/one-style.html')
        self.assertEqual(len(failedRequests), 1)
        self.assertIn('one-style.css', failedRequests[0].url)
        self.assertIsNone(failedRequests[0].response)
        self.assertEqual(failedRequests[0].resourceType, 'stylesheet')
        self.assertEqual(
            failedRequests[0].failure()['errorText'], 'net::ERR_FAILED')
        self.assertTrue(failedRequests[0].frame)

    @sync
    async def test_request_finished(self):
        requests = []
        self.page.on('requestfinished', lambda req: requests.append(req))
        await self.page.goto(self.url + 'empty')

        self.assertEqual(len(requests), 1)
        req = requests[0]
        self.assertEqual(req.url, self.url + 'empty')
        self.assertTrue(req.response)
        self.assertEqual(req.frame, self.page.mainFrame)
        self.assertEqual(req.frame.url, self.url + 'empty')

    @sync
    async def test_events_order(self):
        events = []
        self.page.on('request', lambda req: events.append('request'))
        self.page.on('response', lambda res: events.append('response'))
        self.page.on(
            'requestfinished', lambda req: events.append('requestfinished'))
        await self.page.goto(self.url + 'empty')
        self.assertEqual(events, ['request', 'response', 'requestfinished'])

    @sync
    async def test_redirects(self):
        events = []
        self.page.on('request', lambda req: events.append(
            '{} {}'.format(req.method, req.url)))
        self.page.on('response', lambda res: events.append(
            '{} {}'.format(res.status, res.url)))
        self.page.on('requestfinished', lambda req: events.append(
            'DONE {}'.format(req.url)))
        self.page.on('requestfailed', lambda req: events.append(
            'FAIL {}'.format(req.url)))
        response = await self.page.goto(self.url + 'redirect1')
        self.assertEqual(events, [
            'GET {}'.format(self.url + 'redirect1'),
            '302 {}'.format(self.url + 'redirect1'),
            'DONE {}'.format(self.url + 'redirect1'),
            'GET {}'.format(self.url + 'redirect2'),
            '200 {}'.format(self.url + 'redirect2'),
            'DONE {}'.format(self.url + 'redirect2'),
        ])

        # check redirect chain
        redirectChain = response.request.redirectChain
        self.assertEqual(len(redirectChain), 1)
        self.assertIn('redirect1', redirectChain[0].url)

class TestRequestInterception(BaseTestCase):
    @sync
    async def test_request_interception(self):
        await self.page.setRequestInterception(True)

        async def request_check(req):
            self.assertIn('empty', req.url)
            self.assertTrue(req.headers.get('user-agent'))
            self.assertEqual(req.method, 'GET')
            self.assertIsNone(req.postData)
            self.assertTrue(req.isNavigationRequest())
            self.assertEqual(req.resourceType, 'document')
            self.assertEqual(req.frame, self.page.mainFrame)
            self.assertEqual(req.frame.url, 'about:blank')
            await req.continue_()

        self.page.on('request',
                     lambda req: asyncio.ensure_future(request_check(req)))
        res = await self.page.goto(self.url + 'empty')
        self.assertEqual(res.status, 200)

    @sync
    async def test_referer_header(self):
        await self.page.setRequestInterception(True)
        requests = list()

        async def set_request(req):
            requests.append(req)
            await req.continue_()

        self.page.on('request',
                     lambda req: asyncio.ensure_future(set_request(req)))
        await self.page.goto(self.url + 'static/one-style.html')
        self.assertIn('/one-style.css', requests[1].url)
        self.assertIn('/one-style.html', requests[1].headers['referer'])

    @sync
    async def test_response_with_cookie(self):
        await self.page.goto(self.url + 'empty')
        await self.page.setCookie({'name': 'foo', 'value': 'bar'})

        await self.page.setRequestInterception(True)

        async def continue_(req):
            await req.continue_()

        self.page.on('request', lambda r: asyncio.ensure_future(continue_(r)))
        response = await self.page.reload()
        self.assertEqual(response.status, 200)

    @sync
    async def test_request_interception_stop(self):
        await self.page.setRequestInterception(True)
        self.page.once('request',
                       lambda req: asyncio.ensure_future(req.continue_()))
        await self.page.goto(self.url + 'empty')
        await self.page.setRequestInterception(False)
        await self.page.goto(self.url + 'empty')

    @sync
    async def test_request_interception_custom_header(self):
        await self.page.setExtraHTTPHeaders({'foo': 'bar'})
        await self.page.setRequestInterception(True)

        async def request_check(req):
            self.assertEqual(req.headers['foo'], 'bar')
            await req.continue_()

        self.page.on('request',
                     lambda req: asyncio.ensure_future(request_check(req)))
        res = await self.page.goto(self.url + 'empty')
        self.assertEqual(res.status, 200)

    @sync
    async def test_request_interception_custom_referer_header(self):
        await self.page.goto(self.url + 'empty')
        await self.page.setExtraHTTPHeaders({'referer': self.url + 'empty'})
        await self.page.setRequestInterception(True)

        async def request_check(req):
            self.assertEqual(req.headers['referer'], self.url + 'empty')
            await req.continue_()

        self.page.on('request',
                     lambda req: asyncio.ensure_future(request_check(req)))
        res = await self.page.goto(self.url + 'empty')
        self.assertEqual(res.status, 200)

    @sync
    async def test_request_interception_abort(self):
        await self.page.setRequestInterception(True)

        async def request_check(req):
            if req.url.endswith('.css'):
                await req.abort()
            else:
                await req.continue_()

        failedRequests = []
        self.page.on('request',
                     lambda req: asyncio.ensure_future(request_check(req)))
        self.page.on('requestfailed', lambda e: failedRequests.append(e))
        res = await self.page.goto(self.url + 'static/one-style.html')
        self.assertTrue(res.ok)
        self.assertIsNone(res.request.failure())
        self.assertEqual(len(failedRequests), 1)

    @sync
    async def test_request_interception_custom_error_code(self):
        await self.page.setRequestInterception(True)

        async def request_check(req):
            await req.abort('internetdisconnected')

        self.page.on('request',
                     lambda req: asyncio.ensure_future(request_check(req)))
        failedRequests = []
        self.page.on('requestfailed', lambda req: failedRequests.append(req))
        with self.assertRaises(PageError):
            await self.page.goto(self.url + 'empty')
        self.assertEqual(len(failedRequests), 1)
        failedRequest = failedRequests[0]
        self.assertEqual(
            failedRequest.failure()['errorText'],
            'net::ERR_INTERNET_DISCONNECTED',
        )

    @unittest.skip('Need server-side implementation')
    @sync
    async def test_request_interception_amend_http_header(self):
        pass

    @sync
    async def test_request_interception_abort_main(self):
        await self.page.setRequestInterception(True)

        async def request_check(req):
            await req.abort()

        self.page.on('request',
                     lambda req: asyncio.ensure_future(request_check(req)))
        with self.assertRaises(PageError) as cm:
            await self.page.goto(self.url + 'empty')
        self.assertIn('net::ERR_FAILED', cm.exception.args[0])

    @sync
    async def test_request_interception_redirects(self):
        await self.page.setRequestInterception(True)
        requests = []

        async def check(req):
            await req.continue_()
            requests.append(req)

        self.page.on('request', lambda req: asyncio.ensure_future(check(req)))
        response = await self.page.goto(self.url + 'redirect1')
        self.assertEqual(response.status, 200)

    @sync
    async def test_redirect_for_subresource(self):
        await self.page.setRequestInterception(True)
        requests = list()

        async def check(req):
            await req.continue_()
            requests.append(req)

        self.page.on('request', lambda req: asyncio.ensure_future(check(req)))
        response = await self.page.goto(self.url + 'one-style.html')
        self.assertEqual(response.status, 200)
        self.assertIn('one-style.html', response.url)
        self.assertEqual(len(requests), 5)
        self.assertEqual(requests[0].resourceType, 'document')
        self.assertEqual(requests[1].resourceType, 'stylesheet')

        # check redirect chain
        redirectChain = requests[1].redirectChain
        self.assertEqual(len(redirectChain), 3)
        self.assertIn('/one-style.css', redirectChain[0].url)
        self.assertIn('/three-style.css', redirectChain[2].url)

    @unittest.skip('This test is not implemented')
    @sync
    async def test_request_interception_abort_redirects(self):
        pass

    @unittest.skip('This test is not implemented')
    @sync
    async def test_request_interception_equal_requests(self):
        pass

    @sync
    async def test_request_interception_data_url(self):
        await self.page.setRequestInterception(True)
        requests = []

        async def check(req):
            requests.append(req)
            await req.continue_()

        self.page.on('request', lambda req: asyncio.ensure_future(check(req)))
        dataURL = 'data:text/html,
yo
'
        response = await self.page.goto(dataURL)
        self.assertEqual(response.status, 200)
        self.assertEqual(len(requests), 1)
        self.assertEqual(requests[0].url, dataURL)

    @sync
    async def test_request_interception_abort_data_url(self):
        await self.page.setRequestInterception(True)

        async def request_check(req):
            await req.abort()

        self.page.on('request',
                     lambda req: asyncio.ensure_future(request_check(req)))
        with self.assertRaises(PageError) as cm:
            await self.page.goto('data:text/html,No way!')
        self.assertIn('net::ERR_FAILED', cm.exception.args[0])

    @sync
    async def test_request_interception_with_hash(self):
        await self.page.setRequestInterception(True)
        requests = []

        async def check(req):
            requests.append(req)
            await req.continue_()

        self.page.on('request', lambda req: asyncio.ensure_future(check(req)))
        response = await self.page.goto(self.url + 'empty#hash')
        self.assertEqual(response.status, 200)
        self.assertEqual(response.url, self.url + 'empty')
        self.assertEqual(len(requests), 1)
        self.assertEqual(requests[0].url, self.url + 'empty')

    @sync
    async def test_request_interception_encoded_server(self):
        await self.page.setRequestInterception(True)

        async def check(req):
            await req.continue_()

        self.page.on('request', lambda req: asyncio.ensure_future(check(req)))
        response = await self.page.goto(self.url + 'non existing page')
        self.assertEqual(response.status, 404)

    @unittest.skip('Need server-side implementation')
    @sync
    async def test_request_interception_badly_encoded_server(self):
        pass

    @unittest.skip('Need server-side implementation')
    @sync
    async def test_request_interception_encoded_server_2(self):
        pass

    @unittest.skip('This test is not implemented')
    @sync
    async def test_request_interception_invalid_interception_id(self):
        pass

    @sync
    async def test_request_interception_disabled(self):
        error = None

        async def check(req):
            try:
                await req.continue_()
            except Exception as e:
                nonlocal error
                error = e

        self.page.on('request', lambda req: asyncio.ensure_future(check(req)))
        await self.page.goto(self.url + 'empty')
        self.assertIsNotNone(error)
        self.assertIn('Request interception is not enabled', error.args[0])

    @sync
    async def test_request_interception_with_file_url(self):
        await self.page.setRequestInterception(True)
        urls = []

        async def set_urls(req):
            urls.append(req.url.split('/').pop())
            await req.continue_()

        self.page.on(
            'request', lambda req: asyncio.ensure_future(set_urls(req)))

        def pathToFileURL(path: Path):
            pathName = str(path).replace('\\', '/')
            if not pathName.startswith('/'):
                pathName = '/{}'.format(pathName)
            return 'file://{}'.format(pathName)

        target = Path(__file__).parent / 'static' / 'one-style.html'
        await self.page.goto(pathToFileURL(target))
        self.assertEqual(len(urls), 2)
        self.assertIn('one-style.html', urls)
        self.assertIn('one-style.css', urls)

    @sync
    async def test_request_respond(self):
        await self.page.setRequestInterception(True)

        async def interception(req):
            await req.respond({
                'status': 201,
                'headers': {'foo': 'bar'},
                'body': 'intercepted',
            })

        self.page.on(
            'request', lambda req: asyncio.ensure_future(interception(req)))
        response = await self.page.goto(self.url + 'empty')
        self.assertEqual(response.status, 201)
        self.assertEqual(response.headers['foo'], 'bar')
        body = await self.page.evaluate('() => document.body.textContent')
        self.assertEqual(body, 'intercepted')

    @unittest.skip('Sending binary object is not implemented')
    @sync
    async def test_request_respond_bytes(self):
        pass

class TestNavigationRequest(BaseTestCase):
    @sync
    async def test_navigation_request(self):
        requests = dict()

        def set_request(req):
            requests[req.url.split('/').pop()] = req

        self.page.on('request', set_request)
        await self.page.goto(self.url + 'redirect3')
        self.assertTrue(requests['redirect3'].isNavigationRequest())
        self.assertTrue(requests['one-frame.html'].isNavigationRequest())
        self.assertTrue(requests['frame.html'].isNavigationRequest())
        self.assertFalse(requests['script.js'].isNavigationRequest())
        self.assertFalse(requests['style.css'].isNavigationRequest())

    @sync
    async def test_interception(self):
        requests = dict()

        async def on_request(req):
            requests[req.url.split('/').pop()] = req
            await req.continue_()

        self.page.on('request',
                     lambda req: asyncio.ensure_future(on_request(req)))

        await self.page.setRequestInterception(True)
        await self.page.goto(self.url + 'redirect3')
        self.assertTrue(requests['redirect3'].isNavigationRequest())
        self.assertTrue(requests['one-frame.html'].isNavigationRequest())
        self.assertTrue(requests['frame.html'].isNavigationRequest())
        self.assertFalse(requests['script.js'].isNavigationRequest())
        self.assertFalse(requests['style.css'].isNavigationRequest())

    @sync
    async def test_image(self):
        requests = []
        self.page.on('request', lambda req: requests.append(req))
        await self.page.goto(self.url + 'static/huge-image.png')
        self.assertEqual(len(requests), 1)
        self.assertTrue(requests[0].isNavigationRequest())


# based on
# https://github.com/znxlwm/pytorch-MNIST-CelebA-cGAN-cDCGAN/blob/master/pytorch_MNIST_cDCGAN.py

import torch
import torch.nn as nn
import torch.nn.functional as F

# G(z)
class Generator(nn.Module):
    # initializers
    def __init__(self, channels=1, conv_resolution=128, train_classes=True):
        super(Generator, self).__init__()
        if train_classes:
            self.deconv1 = nn.ConvTranspose2d(100, conv_resolution * 4, kernel_size=4, stride=1, padding=0)
            self.deconv1_bn = nn.BatchNorm2d(conv_resolution * 4)
            self.label_deconv = nn.ConvTranspose2d(10, conv_resolution * 4, kernel_size=4, stride=1, padding=0)
            self.label_deconv_bn = nn.BatchNorm2d(conv_resolution * 4)
        else:
            self.label_deconv = None
            self.deconv1 = nn.ConvTranspose2d(100, conv_resolution * 8, kernel_size=4, stride=1, padding=0)
            self.deconv1_bn = nn.BatchNorm2d(conv_resolution * 8)
        self.deconv2 = nn.ConvTranspose2d(conv_resolution * 8, conv_resolution * 4, kernel_size=4, stride=2, padding=1)
        self.deconv2_bn = nn.BatchNorm2d(conv_resolution * 4)
        self.deconv3 = nn.ConvTranspose2d(conv_resolution * 4, conv_resolution * 2, kernel_size=4, stride=2, padding=1)
        self.deconv3_bn = nn.BatchNorm2d(conv_resolution * 2)
        self.deconv4 = nn.ConvTranspose2d(conv_resolution * 2, conv_resolution, kernel_size=4, stride=2, padding=1)
        self.deconv4_bn = nn.BatchNorm2d(conv_resolution)
        self.deconv5 = nn.ConvTranspose2d(conv_resolution, channels, kernel_size=4, stride=2, padding=1)

    # weight_init
    def weight_init(self, mean, std):
        for m in self._modules:
            normal_init(self._modules[m], mean, std)

    # forward method
    def forward(self, input, label):
        # x = F.relu(self.deconv1(input))
        x = F.relu(self.deconv1_bn(self.deconv1(input)))
        if self.label_deconv is not None:
            y = F.relu(self.label_deconv_bn(self.label_deconv(label)))
            x = torch.cat([x, y], 1)
        x = F.relu(self.deconv2_bn(self.deconv2(x)))
        x = F.relu(self.deconv3_bn(self.deconv3(x)))
        x = F.relu(self.deconv4_bn(self.deconv4(x)))
        x = F.tanh(self.deconv5(x))

        return x

def normal_init(m, mean, std):
    if isinstance(m, nn.ConvTranspose2d) or isinstance(m, nn.Conv2d):
        m.weight.data.normal_(mean, std)
        m.bias.data.zero_()


from http import HTTPStatus
from typing import Optional

from starlette.datastructures import URL
from starlette.requests import Request
from starlette.responses import Response

from liminus.middlewares.mixins.session_mixin import Session, SessionHandlerMixin
from liminus.proxy_request import http_request
from liminus.redis_client import redis_client
from liminus.settings import logger
from liminus.utils import get_cache_hash_key, to_seconds

class JwtHandlerMixin(SessionHandlerMixin):
    AUTH_JWT_HEADER: str
    JWKS_URL: URL
    REFRESH_JWT_URL: URL
    REFRESH_JWT_IF_TTL_LESS_THAN_SECONDS: int = to_seconds(minutes=30)
    REFRESH_JWT_SEMAPHORE_TTL_SECONDS: int = 10

    async def _store_jwt_if_present(self, response: Response, session: Session) -> bool:
        # if the response has a member auth JWT, create a new session (and CSRF) and add this JWT to it
        if self.AUTH_JWT_HEADER not in response.headers:
            return False

        # store this in the session too
        session.session_data = session.session_data or {}
        session.session_data['jwt'] = response.headers[self.AUTH_JWT_HEADER]

        # we don't want this JWT to be in our response, so remove it
        # if we delete the header entirely then Tyk will keep the upstream value, so we just have to overwrite
        response.headers[self.AUTH_JWT_HEADER] = ''

        # when we change auth levels, we always create a new session id, keeping existing data
        # but destroy the current one first
        await self._store_session(session.session_id, None)
        return True

    async def _append_jwt_if_present(self, request: Request, session: Session) -> Optional[str]:
        if not session.session_data or 'jwt' not in session.session_data:
            # nothing to do
            return None

        valid_jwt = session.session_data['jwt']

        if valid_jwt:
            # append the JWT to the backing service request
            request.state.headers[self.AUTH_JWT_HEADER] = valid_jwt
            logger.debug(f'{request} is for authenticated user, adding {self.AUTH_JWT_HEADER} header')
            return valid_jwt

        return None

    async def _refresh_jwt(self, jwt):
        # set a semaphore in Redis, so we only have one concurrent refresh request at a time
        semaphore_key = get_cache_hash_key('jwt-refresh-', jwt)
        semaphore_created = await redis_client().setnx(semaphore_key, '1')
        if not semaphore_created:
            # the semaphore already existed, this request should not attempt to refresh the JWT
            return jwt

        # this semaphore expires after a few seconds
        # we don't need to explicitly delete it - let it provide poor-man's rate limiting
        await redis_client().expire(semaphore_key, self.REFRESH_JWT_SEMAPHORE_TTL_SECONDS)

        response = await http_request('POST', self.REFRESH_JWT_URL, json={'jwt': jwt})
        if response.status == HTTPStatus.OK:
            if self.AUTH_JWT_HEADER in response.headers:
                # we got a nice new token
                return response.headers.getone(self.AUTH_JWT_HEADER)

        # could not be refreshed
        return None


"""
Description: Train emotion classification model
"""

from keras.callbacks import CSVLogger, ModelCheckpoint, EarlyStopping
from keras.callbacks import ReduceLROnPlateau
from keras.preprocessing.image import ImageDataGenerator
from load_and_process import load_fer2013
from load_and_process import preprocess_input
from models.cnn import mini_XCEPTION
from sklearn.model_selection import train_test_split

# parameters
batch_size = 32
num_epochs = 10000
input_shape = (48, 48, 1)
validation_split = .2
verbose = 1
num_classes = 7
patience = 50
base_path = 'models/'

# data generator
data_generator = ImageDataGenerator(
                        featurewise_center=False,
                        featurewise_std_normalization=False,
                        rotation_range=10,
                        width_shift_range=0.1,
                        height_shift_range=0.1,
                        zoom_range=.1,
                        horizontal_flip=True)

# model parameters/compilation
model = mini_XCEPTION(input_shape, num_classes)
model.compile(optimizer='adam', loss='categorical_crossentropy',
              metrics=['accuracy'])
model.summary()

    # callbacks
log_file_path = base_path + '_emotion_training.log'
csv_logger = CSVLogger(log_file_path, append=False)
early_stop = EarlyStopping('val_loss', patience=patience)
reduce_lr = ReduceLROnPlateau('val_loss', factor=0.1,
                                  patience=int(patience/4), verbose=1)
trained_models_path = base_path + '_mini_XCEPTION'
model_names = trained_models_path + '.{epoch:02d}-{val_acc:.2f}.hdf5'
model_checkpoint = ModelCheckpoint(model_names, 'val_loss', verbose=1,
                                                    save_best_only=True)
callbacks = [model_checkpoint, csv_logger, early_stop, reduce_lr]

# loading dataset
faces, emotions = load_fer2013()
faces = preprocess_input(faces)
num_samples, num_classes = emotions.shape
xtrain, xtest,ytrain,ytest = train_test_split(faces, emotions,test_size=0.2,shuffle=True)
model.fit_generator(data_generator.flow(xtrain, ytrain,
                                            batch_size),
                        steps_per_epoch=len(xtrain) / batch_size,
                        epochs=num_epochs, verbose=1, callbacks=callbacks,
                        validation_data=(xtest,ytest))


import ast
def single(nums):
    res=0
    for n in nums:
        res= n^res
    return res

    # stack=[]
    # for i in nums:
    #     if i in stack:
    #         stack.remove(i)
    #     else:
    #         stack.append(i)
    # return stack[0]

print(single(ast.literal_eval(input())))


from transformers import (
    M2M100ForConditionalGeneration,
    M2M100Tokenizer,
    PreTrainedTokenizerBase,
    DataCollatorForSeq2Seq,
)
from tqdm import tqdm
import argparse
import torch
from torch.utils.data import DataLoader
from dataset import DatasetReader, count_lines
import os
from accelerate import Accelerator, DistributedType
from accelerate.memory_utils import find_executable_batch_size

def get_dataloader(
    accelerator: Accelerator,
    filename: str,
    tokenizer: PreTrainedTokenizerBase,
    batch_size: int,
    max_length: int,
) -> DataLoader:

    dataset = DatasetReader(filename, tokenizer, max_length)
    if accelerator.distributed_type == DistributedType.TPU:
        data_collator = DataCollatorForSeq2Seq(
            tokenizer,
            padding="max_length",
            max_length=max_length,
            label_pad_token_id=tokenizer.pad_token_id,
            return_tensors="pt",
        )
    else:
        data_collator = DataCollatorForSeq2Seq(
            tokenizer,
            padding=True,
            label_pad_token_id=tokenizer.pad_token_id,
            # max_length=max_length, No need to set max_length here, we already truncate in the preprocess function
            pad_to_multiple_of=8,
            return_tensors="pt",
        )

    return DataLoader(
        dataset,
        batch_size=batch_size,
        collate_fn=data_collator,
    )

def main(
    sentences_path: str,
    output_path: str,
    source_lang: str,
    target_lang: str,
    starting_batch_size: int,
    model_name: str = "facebook/m2m100_1.2B",
    cache_dir: str = None,
    precision: str = "32",
    max_length: int = 128,
    num_beams: int = 4,
):

    if not os.path.exists(os.path.dirname(output_path)):
        os.makedirs(os.path.dirname(output_path))

    accelerator = Accelerator(
        mixed_precision=precision if precision != "32" else "no", split_batches=True
    )

    print("Loading tokenizer...")
    tokenizer = M2M100Tokenizer.from_pretrained(
        pretrained_model_name_or_path=model_name, cache_dir=cache_dir
    )
    print("Loading model...")
    model = M2M100ForConditionalGeneration.from_pretrained(
        pretrained_model_name_or_path=model_name, cache_dir=cache_dir
    )

    model.eval()

    print(f"Preparing data...\n")

    if precision == "32":
        model = model.float()
    elif precision == "fp16":
        model = model.half()
    elif precision == "bf16":
        model = model.bfloat16()
    else:
        raise ValueError("Precision not supported. Supported values: 32, fp16, bf16")

    tokenizer.src_lang = source_lang
    lang_code_to_idx = tokenizer.lang_code_to_id[target_lang]

    gen_kwargs = {
        "max_length": max_length,
        "num_beams": num_beams,
        "num_return_sequences": 1,
    }

    total_lines: int = count_lines(sentences_path)
    print(
        f"We will translate {total_lines} lines. Initial batch size: {starting_batch_size}"
    )

    @find_executable_batch_size(starting_batch_size=starting_batch_size)
    def inference(batch_size):
        nonlocal model, tokenizer, sentences_path, max_length, output_path, lang_code_to_idx, gen_kwargs, total_lines, precision

        print(f"Translating with batch size {batch_size}")

        data_loader = get_dataloader(
            accelerator=accelerator,
            filename=sentences_path,
            tokenizer=tokenizer,
            batch_size=batch_size,
            max_length=max_length,
        )

        model, data_loader = accelerator.prepare(model, data_loader)

        with tqdm(
            total=total_lines, desc="Dataset translation", leave=True, ascii=True
        ) as pbar, open(output_path, "w", encoding="utf-8") as output_file:
            with torch.no_grad():
                first_batch = True
                for batch in data_loader:
                    batch["input_ids"] = batch["input_ids"]
                    batch["attention_mask"] = batch["attention_mask"]

                    generated_tokens = accelerator.unwrap_model(model).generate(
                        **batch, forced_bos_token_id=lang_code_to_idx, **gen_kwargs
                    )

                    generated_tokens = accelerator.pad_across_processes(
                        generated_tokens, dim=1, pad_index=tokenizer.pad_token_id
                    )

                    generated_tokens = (
                        accelerator.gather(generated_tokens).cpu().numpy()
                    )

                    tgt_text = tokenizer.batch_decode(
                        generated_tokens, skip_special_tokens=True
                    )
                    if not first_batch:
                        print(file=output_file)
                    else:
                        first_batch = False
                    print("\n".join(tgt_text), file=output_file, end="")

                    pbar.update(len(tgt_text))

    inference()
    print(f"Translation done.\n")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Run the translation experiments")
    parser.add_argument(
        "--sentences_path",
        type=str,
        required=True,
        help="Path to a txt file containing the sentences to translate. One sentence per line.",
    )

    parser.add_argument(
        "--output_path",
        type=str,
        required=True,
        help="Path to a txt file where the translated sentences will be written.",
    )

    parser.add_argument(
        "--source_lang",
        type=str,
        required=True,
        help="Source language id. See: supported_languages.md",
    )

    parser.add_argument(
        "--target_lang",
        type=str,
        required=True,
        help="Target language id. See: supported_languages.md",
    )

    parser.add_argument(
        "--starting_batch_size",
        type=int,
        default=128,
        help="Starting batch size, we will automatically reduce it if we find an OOM error.",
    )

    parser.add_argument(
        "--model_name",
        type=str,
        default="facebook/m2m100_1.2B",
        help="Path to the model to use. See: https://huggingface.co/models",
    )

    parser.add_argument(
        "--cache_dir",
        type=str,
        default=None,
        help="Cache directory from which to load the model, or None to not cache",
    )

    parser.add_argument(
        "--max_length",
        type=int,
        default=128,
        help="Maximum number of tokens in the source sentence and generated sentence. "
        "Increase this value to translate longer sentences, at the cost of increasing memory usage.",
    )

    parser.add_argument(
        "--num_beams",
        type=int,
        default=5,
        help="Number of beams for beam search, m2m10 author recommends 5, but it might use too much memory",
    )

    parser.add_argument(
        "--precision",
        type=str,
        default="32",
        choices=["bf16", "fp16", "32"],
        help="Precision of the model. bf16, fp16 or 32.",
    )

    args = parser.parse_args()

    main(
        sentences_path=args.sentences_path,
        output_path=args.output_path,
        source_lang=args.source_lang,
        target_lang=args.target_lang,
        starting_batch_size=args.starting_batch_size,
        model_name=args.model_name,
        cache_dir=args.cache_dir,
        num_beams=args.num_beams,
        precision=args.precision,
    )


list_1 = [['a','cow'], ['b','pig'], ['a','horse'], ['c','dog'], ['b','cat'], ['a','mouse'], ['a','cow']]
##dict_1 = {}
##for entry in list_1:
##     letter = entry[0]
##     animal = entry[1]
##     if letter in dict_1:
##          new_animal = animal
##          old_animal = dict_1[letter]
##          animals = old_animal + new_animal
##          dict_1[letter] = animals
##     else:
##          dict_1[letter] = animal
##print(dict_1)
###output: {'a': 'cowhorsemousecow', 'c': 'dog', 'b': 'pigcat'}

#try making a list within

dict_1 = {}
for entry in list_1:
     letter = entry[0]
     animal = entry[1]
     if letter in dict_1:
        new_animal = animal
        old_animal = dict_1[letter]
        animals_list = old_animal
        if new_animal in old_animal:
           dict_1[letter] = old_animal
        else:
            animals_list.append(new_animal)
            dict_1[letter] = animals_list
     else:
          dict_1[letter] = [animal]
print(dict_1)

#successful result! = {'b': ['pig', 'cat'], 'a': ['cow', 'horse', 'mouse'], 'c': ['dog']}


from framework.exceptions import HTTPError

from website.util.client import BaseClient

from addons.figshare import settings

class FigshareClient(BaseClient):

    def __init__(self, access_token):
        self.access_token = access_token

    @classmethod
    def from_account(cls, account):
        if account is None:
            return cls(None)
        else:
            return cls(account.oauth_key)

    @property
    def _default_headers(self):
        if self.access_token:
            return {'Authorization': 'token {}'.format(self.access_token)}
        return {}

    @property
    def _default_params(self):
        return {'page_size': 100}

    def userinfo(self):
        return self._make_request(
            'GET',
            self._build_url(settings.API_BASE_URL, 'account'),
            expects=(200, ),
            throws=HTTPError(403)
        ).json()

    # PROJECT LEVEL API
    def projects(self):
        return self._make_request(
            'GET',
            self._build_url(settings.API_BASE_URL, 'account', 'projects')
        ).json()

    def project(self, project_id):
        if not project_id:
            return
        project = self._make_request(
            'GET',
            self._build_url(settings.API_BASE_URL, 'account', 'projects', project_id),
            expects=(200,)
        ).json()
        if not project:
            return
        articles = self._make_request(
            'GET',
            self._build_url(settings.API_BASE_URL, 'account', 'projects', project_id, 'articles')
        ).json()
        project['articles'] = []
        if(articles):
            project['articles'] = []
            for article in articles:
                fetched = self.article(article['id'])
                if fetched:
                    project['articles'].append(fetched)
        return project

    # ARTICLE LEVEL API
    def articles(self, only_folders=False):
        article_list = self._make_request(
            'GET',
            self._build_url(settings.API_BASE_URL, 'account', 'articles')
        ).json()
        if only_folders:
            article_list = [x for x in article_list
                            if x['defined_type'] in settings.FIGSHARE_FOLDER_TYPES]
        return [self.article(article['id']) for article in article_list]

    def article_is_public(self, article_id):
        return self.article(article_id).get('is_public')

    def project_is_public(self, project_id):
        return bool(self.project(project_id).get('date_published'))

    def container_is_public(self, container_id, container_type):
        if container_type == 'project':
            return self.project_is_public(container_id)
        elif container_id in settings.FIGSHARE_FOLDER_TYPES:
            return self.article_is_public(container_id)

    def article(self, article_id):
        return self._make_request(
            'GET',
            self._build_url(settings.API_BASE_URL, 'account', 'articles', article_id),
            expects=(200, )
        ).json()

    # OTHER HELPERS
    def get_folders(self):
        """ Return a list containing both projects and folder-like articles. """

        projects = self.projects()
        project_list = [
            {
                'name': project['title'],
                'path': 'project',
                'id': str(project['id']),
                'kind': 'folder',
                'permissions': {'view': True},
                'addon': 'figshare',
                'hasChildren': False
            } for project in projects
        ]

        article_list = [
            {
                'name': (article['title'] or 'untitled article'),
                'path': settings.FIGSHARE_IDS_TO_TYPES[article['defined_type']],
                'id': str(article['id']),
                'kind': 'folder',
                'permissions': {'view': True},
                'addon': 'figshare',
                'hasChildren': False
            } for article in self.articles(only_folders=True)
        ]

        return project_list + article_list

    def get_linked_folder_info(self, _id):
        """ Returns info about a linkable object -- 'project', 'dataset', or 'fileset' """
        ret = {}
        try:
            folder = self._make_request(
                'GET',
                self._build_url(settings.API_BASE_URL, 'account', 'projects', _id),
                expects=(200, ),
                throws=HTTPError(404)
            ).json()
            ret['path'] = 'project'
        except HTTPError:
            folder = self.article(_id)
            if folder.get('defined_type') not in settings.FIGSHARE_FOLDER_TYPES:
                raise
            ret['path'] = settings.FIGSHARE_IDS_TO_TYPES[folder.get('defined_type')]
        ret['name'] = folder['title'] or 'untitled article'
        ret['id'] = str(_id)
        return ret


from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.utils.safestring import mark_safe
from django.views.generic import TemplateView

from parametros.models import Periodo
from costos.models import CostoParametro, ArchivosAdjuntosPeriodo
from registro.models import Certificacion, AjusteCombustible
from zweb_utils.views import LoginAndPermissionRequiredMixin
from .stats import get_utilizacion_equipo, get_cc_on_periodo, get_ventas_costos

from zweb_utils.excel import ExportPanelControl

class Index(LoginAndPermissionRequiredMixin, TemplateView):
    template_name = "frontend/panel_control.html"
    permission_required = 'costos.can_view_panel_control'
    permission_denied_message = "No posee los permisos suficientes para ingresar a esa sección"
    raise_exception = True

    def get_context_data(self, periodos, **kwargs):
        context = super(Index, self).get_context_data(**kwargs)
        context["periodos"] = periodos
        if 'periodo' in self.request.GET:
            periodo = Periodo.objects.get(pk=self.request.GET["periodo"])
        else:
            periodo = periodos[0] if periodos else None
        context["periodo"] = periodo
        try:
            context["equipos"], context["totales"] = get_utilizacion_equipo(periodo)
            context["resumen_costos"], context["total"], totales_costos = get_cc_on_periodo(periodo, context["totales"])
            context["cert_costos"], context["costos_ventas_total"] = get_ventas_costos(periodo, totales_costos)
            context["archivos"] = ArchivosAdjuntosPeriodo.objects.filter(periodo=periodo)
        except CostoParametro.DoesNotExist as e:
            messages.add_message(self.request, messages.WARNING,
                                 mark_safe("No están definidos los parámetros de costos para el "
                                           "periodo {}".format(periodo)))
        except Certificacion.DoesNotExist as e:
            messages.add_message(self.request, messages.WARNING,
                                 mark_safe("No hay certificaciones de obras para el "
                                           "periodo {}".format(reverse('admin:registro_certificacion_changelist'), periodo)))
        return context

    def get(self, request, *args, **kwargs):
        context = {}
        periodos = Periodo.objects.all().order_by('-fecha_inicio')
        if periodos:
            context = self.get_context_data(periodos)
        else:
            messages.add_message(request, messages.WARNING, "No hay periodos definidos en el sistema.")
        return self.render_to_response(context)

class ExportarPanel2Excel(Index):
    permission_required = ('costos.can_view_panel_control', 'costos.can_export_panel_control', )

    def get(self, request, *args, **kwargs):
        periodos = Periodo.objects.all().order_by('-fecha_inicio')
        if periodos:
            context = self.get_context_data(periodos)
            if 'cert_costos' in context:
                response = HttpResponse(content_type='application/vnd.ms-excel')
                response['Content-Disposition'] = 'attachment; filename=Report.xlsx'
                xlsx_data = ExportPanelControl().fill_export(context)
                response.write(xlsx_data)
                return response
        else:
            messages.add_message(request, messages.WARNING, "No hay periodos definidos en el sistema.")
        return HttpResponseRedirect(reverse("frontend:index"))

index = Index.as_view()
export_panel_control_excel = ExportarPanel2Excel.as_view()


class AudioFile:
    def __init__(self, filename):
        if not filename.endswith(self.ext): #uses self.ext without defining it
            raise Exception("Invalid file format")
        self.filename = filename

class MP3File(AudioFile):
    ext = "mp3" #here, in the child, ext is defined (self. exists only in functions)
    def play(self):
        print("playing {} as mp3".format(self.filename))

class WavFile(AudioFile):
    ext = "wav" #here, in the child, ext is defined
    def play(self):
        print("playing {} as wav".format(self.filename))

class OggFile(AudioFile):
    ext = "ogg" #here, in the child, ext is defined
    def play(self):
        print("playing {} as ogg".format(self.filename))

if __name__=='__main__':
    ogg = OggFile("myfile.ogg")
    ogg.play()

    mp3 = MP3File("myfile.mp3")
    mp3.play()

    not_an_mp3 = MP3File("myfile.ogg")


# Given two binary strings, return their sum (also a binary string).
#
# The input strings are both non-empty and contains only characters 1 or 0.
#
# Example 1:
#
# Input: a = "11", b = "1"
# Output: "100"
# Example 2:
#
# Input: a = "1010", b = "1011"
# Output: "10101"

a = [1,0,1,1]

def add_binary(n, m):
    max_len = max(len(n), len(m))
    n = n.zfill(max_len)
    m = m.zfill(max_len)
    result = ""
    carryover = 0
    for i in range(-1, -max_len-1, -1):
        s = carryover
        s += 1 if n[i] == '1' else 0
        s += 1 if m[i] == '1' else 0
        result = ('1' if s % 2 == 1 else '0') + result
        carryover = 0 if s < 2 else 1
        print(result, carryover, s)
    if carryover != 0:
        result = '1' + result
    return result

print(add_binary("11", "1"))


import fileinput

def read_floats(fin):
    line = next(fin)
    nums = line.split()
    return list(map(float, nums))

def problems(fin):
    T = int(next(fin))
    for _ in range(T):
        N = int(next(fin))
        w1 = read_floats(fin)
        w2 = read_floats(fin)
        assert len(w1) == N
        assert len(w2) == N
        yield w1, w2

def play0(w1, w2):
    lost = 0
    lo, hi = 0, len(w2) - 1
    for m1 in w1:
        if m1 > w2[hi]:
            break
        if m1 > w2[lo]:
            lo = lo + 1
            continue
        if m1 < w2[lo]:
            lost, hi = lost + 1, hi - 1
            continue
        assert False
    return len(w1) - lost

def play1(w1, w2):
    lost = 0
    i1, i2 = len(w1) - 1, len(w2) - 1
    while i1 >= 0 and i2 >= 0:
        m1, m2 = w1[i1], w2[i2]
        if m2 > m1:
            i1, i2, lost = i1 - 1, i2 - 1, lost + 1
            continue
        if m2 < m1:
            i1 = i1 - 1
            continue
        assert False

    return len(w1) - lost

def solve(problem):
    w1, w2 = problem
    w1, w2 = sorted(w1), sorted(w2)
    return "{} {}".format(play0(w1, w2), play1(w1, w2))

# def test(l):
#     import bisect
#     def war(w1, w2):
#         ans = 0
#         w2 = list(w2)
#         for m1 in w1:
#             i = bisect.bisect_left(w2, m1)
#             if i >= len(w2):
#                 ans += 1
#                 w2.pop(0)
#             else:
#                 w2.pop(i)
#         return ans

#     from itertools import permutations, combinations
#     s = set(range(2 * l))
#     for c in combinations(s, l):
#         w1 = sorted(c)
#         w2 = sorted(s - set(c))
#         R1 = max(war(w, w2) for w in permutations(w1))
#         R2 = play1(w1, w2)
#         print("{} vs {}: {} = {}".format(w1, w2, R1, R2))
#         assert R1 == R2

def main():
    for i, p in enumerate(problems(fileinput.input())):
        ans = solve(p)
        print("Case #{}: {}".format(i + 1, ans))

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
"""
Created on Tue May 03 11:05:31 2016

@author: Mike
"""

#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

addedContent = np.load('data/cleaned/timeline_analysis_added_content.npy')
consistency = np.load('data/cleaned/timeline_analysis_consistency.npy')
consistency = 1/consistency
consistency[6] = 0

nr_of_clusters = [2, 3, 4, 5, 6, 7, 8, 9]
X = np.column_stack((consistency, addedContent))
X = np.delete(X, 6, 0)

score = []
for nr in nr_of_clusters:
    km = KMeans(n_clusters=nr)
    km.fit(X)
    score.append(km.score(X))

plt.plot(nr_of_clusters, score)
plt.plot(4, score[2], 'r.', markersize=8.0)
plt.plot(3, score[1], 'g.', markersize=8.0)
title = plt.title('Finding k for K-Means clustering: Elbow method', y=1.08)
x_label = plt.xlabel('Number of clusters')
y_label = plt.ylabel('Sum of distances to cluster center (score)')

plt.grid()
plt.savefig('data/cleaned/summary_plots/interaction_cluster_elbow.png', dpi=300, bbox_extra_artists=(title, x_label, y_label, ), bbox_inches='tight')
plt.close()


from PIL import Image, ImageDraw

n = int(input())
p = int(input())
v = []
maxx = 0
maxy = 0
minx = n
miny = n

for i in range(p):
	x = int(input())
	if x > maxx:
		maxx = x
	if x < minx:
		minx = x
	y = int(input())
	if y > maxy:
		maxy = y
	if y < miny:
		miny = y
	v.append((x,y))

img = Image.new('RGB', (n, n), 'Beige')
dib = ImageDraw.Draw(img)

dib.polygon([(minx, miny), (maxx, miny), (maxx, maxy), (minx, maxy)], 'Red')

for (x,y) in v:
	dib.point((x,y), 'Black')

img.save('output.png')


# #!/usr/bin/env python
# import sys, re
import re
import pandas as pd
import os
import sys

# build valience lookup table
# valences_df = pd.read_csv('AFINN-en-165.txt', sep='\t', header=None, names = ["word", "valence"])
# valence_lookup_table = dict(zip(valences_df.word, valences_df.valence))

def main(argv):
    word_count = 0
    valence_aggregate = 0

    # input comes from STDIN (standard input)
    for line in sys.stdin:
        # remove leading and trailing whitespace
        line = line.strip()
        # keep alphabets
        pattern = re.compile("[a-zA-Z][a-zA-Z0-9]*")
        # split the line into words
        words = line.split()
        try:
            for word in pattern.findall(line):
                print ("LongValueSum:" + word.lower() + "\t" + "1")

        except EOFError as error:
            return None

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


#!/usr/bin/python3
"""Defines a class erectangle"""
BaseGeometry = __import__('7-base_geometry').BaseGeometry

class Rectangle(BaseGeometry):
    """BaseGeometry class from previous task."""

    def __init__(self, width, height):
        """It Intializes a new Rectangle.

        Args:
            width (int): The width of the new Rectangle.
            height (int): The height of the new Rectangle.
        """
        self.integer_validator("width", width)
        self.__width = width
        self.integer_validator("height", height)
        self.__height = height


class Node(object):
    """docstring for Node"""

    def __init__(self, val):
        self.val = val
        self.next = None

class MyLinkedList(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.head = Node(0)
        self.tail = None
        self.length = 0

    def get(self, index):
        """
        Get the value of the index-th node in the linked list. If the index is invalid, return -1.
        :type index: int
        :rtype: int
        """
        if index < 0 or index >= self.length:
            return -1
        node = self.head.next
        while index:
            node = node.next
            index -= 1
        return node.val

    def addAtHead(self, val):
        """
        Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
        :type val: int
        :rtype: void
        """
        new_node = Node(val)

        new_node.next = self.head.next
        self.head.next = new_node
        if self.length == 0:
            self.tail = new_node
        self.length += 1

    def addAtTail(self, val):
        """
        Append a node of value val to the last element of the linked list.
        :type val: int
        :rtype: void
        """
        new_node = Node(val)
        if self.length == 0:
            self.head.next = self.tail = new_node
        else:
            self.tail.next = new_node
            self.tail = new_node
        self.length += 1

    def addAtIndex(self, index, val):
        """
        Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
        :type index: int
        :type val: int
        :rtype: void
        """
        if index < 0 or index > self.length:
            return
        if index == 0:
            return self.addAtHead(val)
        if index == self.length:
            return self.addAtTail(val)
        node = self.head
        new_node = Node(val)
        while index:
            node = node.next
            index -= 1
        new_node.next = node.next
        node.next = new_node
        self.length += 1

    def deleteAtIndex(self, index):
        """
        Delete the index-th node in the linked list, if the index is valid.
        :type index: int
        :rtype: void
        """
        if index < 0 or index >= self.length:
            return
        node = self.head
        for i in range(index):
            node = node.next
        if index == self.length - 1:
            self.tail = node

        node.next = node.next.next
        self.length -= 1


from rest_framework import serializers
from .models import Standing

class StandingSerializer(serializers.ModelSerializer):

    class Meta:
        model = Standing
        fields = (
            'id', 'stage', 'sub_group', 'team', 'position',
            'played', 'won', 'drawn', 'lost', 'goal_difference', 'points', 'is_playing',
        )
        depth = 2


import copy

n = int(input())
k = int(input())
apples = [tuple(map(int, input().split())) for _ in range(k)]
l = int(input())

times = dict()
for _ in range(l):
    second, direction = input().split()
    times[int(second)] = direction

apple_board = [[0]*n for _ in range(n)]
for x, y in apples:
    apple_board[x-1][y-1] = 1

length = 1
snakes = [(0,0)]
snakes_dir = [0]
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
change = [(3, 2), (2, 3), (0, 1,), (1, 0)]

sec = 1
tail = (0, 0)
head = (0, 0)
flag = False
while True:
    # 전진
    for i in range(length):
        if i == length - 1:
            tail = snakes[i]
        a = snakes[i][0] + dirs[snakes_dir[i]][0]
        b = snakes[i][1] + dirs[snakes_dir[i]][1]
        snakes[i] = (a, b)

        # 보드 밖을 나가는 경우
        if snakes[i][0] < 0 or snakes[i][1] < 0 or snakes[i][0] > n-1 or snakes[i][1] > n-1:
            flag = True
            break
        # 자신의 몸과 닿는 경우
        if len(set(snakes)) < len(snakes):
            flag = True
            break

    if flag:
        break

    # 사과먹기
    x, y = snakes[0]
    if apple_board[x][y] == 1:
        length += 1
        snakes.append(tail)
        snakes_dir.append(snakes_dir[::-1])
        apple_board[x][y] = 0

    # 자신의 몸과 닿는 경우
    if len(set(snakes)) < len(snakes):
        break

    # 다음 방향 설정
    temp = copy.deepcopy(snakes_dir)
    for j in range(length-1):
        snakes_dir[j+1] = temp[j]

    # 방향 전환
    if times.get(sec) != None:
        if times.get(sec) == 'L':
            snakes_dir[0] = change[snakes_dir[0]][0]
        if times.get(sec) == 'D':
            snakes_dir[0] = change[snakes_dir[0]][1]
    sec += 1
print(sec)


#!/usr/bin/env python

from __future__ import print_function
import argparse
from collections import OrderedDict

VERSION = '0.1.0'

def supply_args():
    """
    Populate args.
    https://docs.python.org/2.7/library/argparse.html
    """
    parser = argparse.ArgumentParser(description='')
    parser.add_argument('in_vcf', help='Input VCF')
    parser.add_argument('probeqc', help='Input Probe QC')
    parser.add_argument('sample_id', help='Sample ID')
    parser.add_argument('outfile', help='Output VCF')
    parser.add_argument('outfile_table', help='Output Table')

    parser.add_argument('--version', action='version', version='%(prog)s ' + VERSION)
    args = parser.parse_args()
    return args

def find_sample_index(header, sample_id):
    """
    Get the index of the sample ID from the VCF column header.
    :return:
    """
    try:
        return header.index(sample_id)
    except IndexError:
        raise ValueError("Sample ID does not exist in input VCF.")

def split_line(line, idx):
    """
    Split apart the line based on index.
    :param line:
    :param idx:
    :return:
    """
    nline = line[:9]
    nline.append(line[idx])
    return nline

def trans_call(value):
    """
    Take the raw 1 or 2 value from the VCF and translate it to DEL or DUP.
    :return:
    """
    if value == '1':
        return 'DEL'
    elif value == '2':
        return 'DUP'
    elif value == '.':
        return 'NO CALL'
    else:
        raise Exception('Call is not del or dup, please investigate.')

def parse_probeqc(filename):
    """
    Parse the ProbeQC.
    1	1634904	1635073	NM_024011.2;NM_033529.2;XM_005244780.1;XM_005244781.1;XM_005244782.1;XM_005244783.1;
    XM_005244784.1;XM_005244785.1;XM_005244786.1;XM_005244787.1;XM_005244788.1;XM_005244789.1;XM_005244790.1
    CDK11A	30.0	86.7	0.0	0.0	0.0	80.0	100.0
    :return:
    """
    pqc = OrderedDict()
    with open(filename, 'r') as probeqc:
        for line in probeqc:
            line = line.rstrip('\n').split('\t')
            uniq_key = (line[0], line[1])
            hgnc = line[4]
            pqc[uniq_key] = hgnc
    return pqc

def find_genes(coord, numt, pqc):
    """
    Find all gene names that overlap the regions.
    :param coord:
    :param numt:
    :param pqc:
    :return:
    """
    chrom = coord.split(':')[0]
    start = coord.split(':')[1].split('-')[0]
    to_find = (chrom, start)
    genes = []

    idx = list(pqc.keys()).index(to_find)

    for i in range(idx, idx+numt):
        this_gene = list(pqc.values())[i]
        if this_gene not in genes:
            genes.append(this_gene)

    return genes

def manip_line(nline):
    """
    Take the information from a VCF line and adjust it to be more useful.
    1	1634904	1:1634904-1639033	<DIP>	<DEL>,<DUP>	.	.
    AC=7,3;AF=0.12,0.05;AN=59;END=1639033;IMPRECISE;SVLEN=4130;SVTYPE=CNV;TPOS=16
    34904;TEND=1639033;NUMT=8;GQT=17;PREVTARGSTART=1268875;PREVTARGEND=1269851;POSTTARGSTART=1639608;POSTTARGEND=1639694
    GT:NDQ:DQ:EQ:SQ:NQ:LQ:RQ:PL:RD:ORD:DS
    CVR	1:99:0:17,0:99,0:0,99:17,0:6,0:104,0,255:-3.38:30.08:Y
    :return:
    """
    region = nline[2]
    this_call = nline[9].split(':')[0]
    cnv_call = trans_call(this_call)
    pop_dels = nline[7].split(';')[0].split('=')[1].split(',')[0]
    pop_dups = nline[7].split(';')[0].split('=')[1].split(',')[1]
    pop_dels_af = nline[7].split(';')[1].split('=')[1].split(',')[0]
    pop_dups_af = nline[7].split(';')[1].split('=')[1].split(',')[1]
    svlen = nline[7].split(';')[5].split('=')[1]
    zscore = nline[9].split(':')[9]
    avg_depth = nline[9].split(':')[10]
    numt = nline[7].split(';')[9].split('=')[1]
    allele_count = nline[7].split(';')[2].split('=')[1]
    new_info = [region, cnv_call, svlen, zscore, avg_depth, numt, allele_count, pop_dels, pop_dels_af,
                pop_dups, pop_dups_af]
    return new_info

def tbl_header():
    """
    Provide the table header.
    :return:
    """
    header = ['REGION', 'DEL/DUP', 'CNV LENGTH', 'ZSCORE', 'MEAN DEPTH', 'NUMBER OF PROBES', 'TOTAL ALLELES',
              'POP DEL COUNT', 'POP DEL AF', 'POP DUP COUNT', 'POP DUP AF', 'GENES']
    return header

def main():

    args = supply_args()
    probeqc = parse_probeqc(args.probeqc)
    handle_out = open(args.outfile, 'w')
    handle_out_tbl = open(args.outfile_table, 'w')
    handle_out_tbl.write('\t'.join(tbl_header()))
    handle_out_tbl.write('\n')

    with open(args.in_vcf, 'r') as myvcf:
        for line in myvcf:
            if line.startswith('#CHROM'):
                line = line.rstrip('\n').split('\t')
                samp_idx = find_sample_index(line, args.sample_id)
                nline = split_line(line, samp_idx)
                handle_out.write('\t'.join(nline))
                handle_out.write('\n')
            elif line.startswith('#'):
                handle_out.write(line)
            else:
                line = line.rstrip('\n').split('\t')
                nline = split_line(line, samp_idx)
                if nline[9].split(':')[-1] == 'Y':
                    handle_out.write('\t'.join(nline))
                    handle_out.write('\n')
                    tbl_line = manip_line(nline)
                    tbl_line.append(','.join(find_genes(tbl_line[0], int(tbl_line[5]), probeqc)))
                    handle_out_tbl.write('\t'.join(tbl_line))
                    handle_out_tbl.write('\n')

    handle_out.close()
    handle_out_tbl.close()

if __name__ == "__main__":
    main()


from sympy import *
import sympy as sp
sp.init_printing(use_latex = "mathjax")
a, L, x , EI, psi, kAG, C1, C2, C3, C4, Mo, P = symbols("a L x EI psi kAG C_1 C_2 C_3 C_4 M P")
q = 0
#This seems like the output is incorrect???
EIpsi = integrate(q,x,x,x) + C1*x**2/2 + C2*x + C3
EIy = integrate(q,x,x,x,x) + C1*x**3/6 + C2*x**2/2 + C3*x + C4 - EI/kAG*(integrate(q,x,x) + C1*x)
M = EIpsi.diff(x)
V = EIpsi.diff(x,2)
M2 = M.subs(x,L) - Mo
y1 = (EIy/EI).subs(x,0)
psi1 = (EIpsi/EI).subs(x,0)
V2 = V.subs(x,L) - P
s = solve([psi1,y1,M2,V2], [C1, C2, C3, C4])
print(s)
EIpsi = EIpsi.subs({C1:s[C1], C2:s[C2], C3:s[C3], C4:s[C4]})
EIy = EIy.subs({C1:s[C1], C2:s[C2], C3:s[C3], C4:s[C4]})
print("y = ", simplify(EIy/EI))
print("psi = ", simplify(EIpsi/EI))


#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt
from scipy import random

#compute m draws from the dirichlet distribition with data given in vector a
def rdirich(m, a):
    th = np.zeros((m, len(a)+1))

    #generates m random gamma draws based on each of the data points
    for j in range(len(a)):
        th[:,j] = random.gamma(a[j], 1, m)
    th = th/np.sum(th, axis=1)[:,np.newaxis] #normalizes
    th[:,-1] = th[:,0] - th[:,1] #calculates gamma
    return th

if __name__ == '__main__':
    #define parameters
    p = 3
    m = int(1e6)

    #define prior and data, calculate posterior
    prior = np.ones(p) * .01
    data = np.asarray((727, 583, 137))
    post = rdirich(m, prior+data)

    #calculate confidence interval size and print with mean
    ci =  np.percentile(post[:,-1], (2.5, 97.5))
    print(np.average(post[:,-1]), ci, np.sum(post[:,-1]>0)/m)

    #plot posterior histograms
    plt.figure(figsize=(10,4))
    plt.subplot(121)
    plt.hist(post[:,0], label = r'$\theta_1$', bins = 50)
    plt.hist(post[:,1], label = r'$\theta_2$', bins = 50)
    plt.xlabel('Vote Fraction')
    plt.ylabel('Counts')
    plt.legend()
    plt.subplot(122)
    plt.hist(post[:,-1], label = r'$\gamma$', bins = 50, color = 'g')
    plt.axvline(x=ci[0], color = 'k', ls = '--')
    plt.axvline(x=ci[1], color = 'k', ls = '--')
    plt.xlabel('Vote Fraction')
    plt.ylabel('Counts')
    plt.legend()
    plt.tight_layout()
    plt.show()


import sys
sys.setrecursionlimit(10**6)

dr = [-1, 1, 0, 0]
dc = [0, 0, -1, 1]

def dfs(r, c):
    global size
    painting[r][c] = 2
    size += 1
    for d in range(4):
        nr = r + dr[d]
        nc = c + dc[d]
        if 0 <= nr < n and 0 <= nc < m and painting[nr][nc] == 1:
            dfs(nr, nc)

n, m = map(int, input().split())
painting = [list(map(int, input().split())) for _ in range(n)]

cnt = 0
size_lst = [0]

for i in range(n):
    for j in range(m):
        if painting[i][j] == 1:
            size = 0
            dfs(i, j)
            cnt += 1
            size_lst.append(size)

print(cnt)
print(max(size_lst))


from django.shortcuts import render,redirect
from .forms import spalfeedback

# Create your views here.
def feedback(request):
  if request.method == 'POST':
    fm = spalfeedback(request.POST,request.FILES)
    if fm.is_valid():
      fm.save()
      fm = spalfeedback()
      return redirect('index')
  else:
    fm = spalfeedback()
  return render(request,'Feedback/feedback.html',{'form':fm})


def isPalindrome(nb):
  return str(nb) == str(nb)[::-1]

def largestPalindrome(dimension):
  limit = int("9" * dimension) + 1
  numbers = [str(x) for x in range(limit)]
  digitNb = list(filter(lambda x: len(str(x)) == dimension, numbers))
  digitNb = [int(x) for x in digitNb]
  palindromes = [x * y for x in digitNb for y in digitNb if isPalindrome(x * y)]

  return max(palindromes)

print(largestPalindrome(3)) # 906609


import stdarray
import stdio

n = int(input("Enter size of matrix: "))

a = stdarray.create2D(n,n,1)

for i in range(1,n+1):
    for j in range(1,n+1):
        mn = min(i,j)
        for k in range(1,mn+1):
            if i%k == 0 and j%k == 0:
                hcf = k
            if hcf == 1:
                a[i-1][j-1] = True
            else:
                a[i-1][j-1] = False

for i in range(n+1):
    if i == 0:
        stdio.write("  ")
    else:
        stdio.write(str(i) + " ")
    for j in range(n):
        if i == 0:
            stdio.write(str(j+1) + " ")
        else:
            if a[i-1][j] == True:
                stdio.write("* ")
            else:
                stdio.write("  ")
    stdio.writeln()

Chapter 1 - Elements of Programming/1.4 Arrays/1-4-14.py


# -*- coding:utf-8 -*-
import time
from datetime import datetime

from log import logger

class Timer(object):

    def __init__(self, buy_time, sleep_interval=0.5):

        # '2018-09-28 22:45:50.000'
        self.buy_time = datetime.strptime(buy_time, "%Y-%m-%d %H:%M:%S.%f")
        self.sleep_interval = sleep_interval

    def start(self):
        logger.info('正在等待到达设定时间:%s' % self.buy_time)
        now_time = datetime.now
        while True:
            if now_time() >= self.buy_time:
                logger.info('时间到达，开始执行……')
                break
            else:
                time.sleep(self.sleep_interval)


# IMPORTS
from PrepSteps.ReSize import FindSize, SizeScaler
from PrepSteps.OneShotAugment import FindMax, AugmentData

def PrepareData(data_path, threshold, left_overs=True):
    """ Gives information about the dataset
        - Size of smallest image and largest
        - MEAN width and height
        - MEDIAN width and height
        -The most popular image sizes in our dataset

    This could help to choose the most appropriate final size of the images

    IMPORTANT :
        The directory should have folders and sub-folders !
        The images should be .PNG (Otherwise change to the appropriate extension)
    """

    FindSize(data_path)
    ####################################################################

    '''
    Checks the images based on the given thrushold (tuple)
    Resizes the images based on a final indicated size (tuple)    '''
    print("============================================================\n")
    SizeScaler(data_path)

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

    ''' Calculates the maximum possible size of the dataset after
    the 6 augmentations
    + removes the folders with smaller size than the indicated one

    * Creates a separate folder (set_aside) where the eliminated/smaller folders are located  '''
    print("============================================================\n")

    ### Create the path for the left over folders
    rem = len(data_path) - len(data_path.split('\\')[-1]) - 1
    new_path = data_path[:rem]

    if left_overs == True:
        FindMax(new_path, threshold)

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

    #### Augment the data to increase the size
    print("============================================================\n")
    AugmentData(new_path)