# -*- coding: utf-8 -*-
"""
Created on Tue Jul 11 20:03:13 2017

@author: cai

finetuning 网络， 使用自己的数据集，并调用alexnet网络结构， 使用tf处理数据，ubuntu下使用，TF 0.8版本。
"""

import os
import numpy as np
import tensorflow as tf
from datetime import datetime
from alexnet import AlexNet
from tensorflow_datagenerator import ImageDataGenerator
import parameters as pm

# start time
start_time = datetime.now()

# Path to the textfiles for the trainings and validation set
rootPath = 'D:/dataset/food-101'
train_file = rootPath + '/data/train_101.txt'
val_file = rootPath + '/data/val_101.txt'

num_epochs = 100
train_batch_size = 128
test_batch_size = 50
test_iter = 1000
# set save model iteration intervals
saveModel_iter = 10000

# Network params
dropout_rate = 0.5
num_classes = 101
# no need to finetuning layers
train_layers = ['fc8']

# How often we want to write the tf.summary data to disk
display_step = 20

# Path for tf.summary.FileWriter and to store model checkpoints
filewriter_path = rootPath + "/logs/%depoch_%dbatch"%(num_epochs,train_batch_size)
checkpoint_path = rootPath + "/models/%depoch_%dbatch"%(num_epochs,train_batch_size)

weights_path = rootPath + '/tensorflow/bvlc_alexnet.npy'
test_log_file = rootPath + '/logs/tensorflow_finetuning_%depoch_%dbatch_tensorflowDatagenerator.log'%(num_epochs,train_batch_size)

# print the shape of every weight and bias in all layer
#==============================================================================
# net_data = np.load(weights_path).item()
# print type(net_data)
# for name, val in net_data.iteritems():
#     print 'layer:',name
#     if len(val)==2:
#         print 'weight shape:',val[0].shape
#         print 'bias shape:', val[1].shape
#==============================================================================

# Create parent path if it doesn't exist
if not os.path.isdir(checkpoint_path):
    os.makedirs(checkpoint_path)
if not os.path.isdir(filewriter_path):
    os.makedirs(filewriter_path)

# 设置占位符
x = tf.placeholder(tf.float32, [None, 227, 227, 3])
y = tf.placeholder(tf.float32, [None, num_classes])
keep_prob = tf.placeholder(tf.float32)

# Initialize model
print('{} Initialize model'.format(datetime.now()))
model = AlexNet(x, keep_prob, num_classes, train_layers,weights_path=weights_path)

#link variable to model output
score = model.fc8

# Initalize the data generator seperately for the training and validation set
# Place data loading and preprocessing on the cpu
print('{} prepare data'.format(datetime.now()))
with tf.device('/cpu:0'):
    tr_data_generator = ImageDataGenerator(train_file,
                                 mode=pm.TRAINING_MODE,
                                 batch_size=train_batch_size,
                                 num_classes=pm.num_classes,
                                 shuffle=True,
                                 is_flip=True)
    val_data_generator = ImageDataGenerator(val_file,
                                  mode=pm.VAL_MODE,
                                  batch_size=test_batch_size,
                                  num_classes=pm.num_classes,
                                  shuffle=False,
                                  is_flip=False)

# Get the number of training/validation steps per epoch
train_batches_per_epoch = np.floor(tr_data_generator.data_size / train_batch_size).astype(np.int16)
val_batches_per_epoch = np.floor(val_data_generator.data_size / test_batch_size).astype(np.int16)

print('{} train_batches_per_epoch: {}'.format(datetime.now(), train_batches_per_epoch))
print('{} val_batches_per_epoch: {}'.format(datetime.now(), val_batches_per_epoch))

# Learning params
start_learning_rate = 0.001
global_step = tf.Variable(0, trainable=False)
num_epochs_per_decay = 10
# set a decrease learning rate with training iterations increasing
learningRate_decayIters = train_batches_per_epoch * num_epochs_per_decay
# decayed_learning_rate = learning_rate * decay_rate ^ (global_step / decay_steps)
learning_rate = tf.train.exponential_decay(start_learning_rate, global_step,
                                           learningRate_decayIters,0.96,staircase=True)

# List of trainable variables of the layers we want to train
var_list = [v for v in tf.trainable_variables() if v.name.split('/')[0] in train_layers]
#==============================================================================
# tmp_sess = tf.Session()
# for var in var_list:
#     print var.name
#==============================================================================

# Op for calculating the loss
with tf.name_scope("cross_ent"):
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = score, labels = y))

# Train op
with tf.name_scope("train"):
    # Get gradients of all trainable variables
    gradients = tf.gradients(loss, var_list)
    gradients = list(zip(gradients, var_list))

    # Create optimizer and apply gradient descent to the trainable variables
    optimizer = tf.train.GradientDescentOptimizer(learning_rate)
    train_op = optimizer.apply_gradients(grads_and_vars=gradients,global_step=global_step)

# Add gradients to summary
for gradient, var in gradients:
    # 1.2.0
    tf.summary.histogram(var.name + '/gradient', gradient)
    # old version
    #tf.histogram_summary(var.name + '/gradient', gradient)

# Add the variables we train to the summary
for var in var_list:
    tf.summary.histogram(var.name, var)
    #tf.histogram_summary(var.name, var)

# Add the loss to summary
tf.summary.scalar('cross_entropy', loss)
#tf.scalar_summary('cross_entropy', loss)

# Evaluation op: Accuracy of the model
with tf.name_scope("accuracy"):
    correct_pred = tf.equal(tf.argmax(score, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# Add the accuracy to the summary
tf.summary.scalar('accuracy', accuracy)
#tf.scalar_summary('accuracy', accuracy)

# Merge all summaries together
merged_summary = tf.summary.merge_all()
#merged_summary = tf.merge_all_summaries()

# Initialize the FileWriter
writer = tf.summary.FileWriter(filewriter_path)
#writer = tf.train.SummaryWriter(filewriter_path)

# Initialize an saver for store model checkpoints
saver = tf.train.Saver()

# train batch input
#==============================================================================
# image_batch_out, label_batch_out, filename_batch = readData.batch_input(is_train=True, is_random=False, batch_size=train_batch_size,
#                                                                         is_flip=True)
# image_batch = tf.reshape(image_batch_out, (train_batch_size, 227, 227, 3))
# label_batch_one_hot = tf.one_hot(label_batch_out, depth=num_classes, on_value=1.0, off_value=0.0)
#
# # val batch input
# val_image_batch_out, val_label_batch_out, val_filename_batch = readData.batch_input(is_train=False, is_random=False, batch_size=test_batch_size,
#                                                                         is_flip=False)
# val_image_batch = tf.reshape(val_image_batch_out, (test_batch_size, 227, 227, 3))
#
# val_label_batch_one_hot = tf.one_hot(val_label_batch_out, depth=num_classes, on_value=1.0, off_value=0.0)
#==============================================================================

flog = open(test_log_file,'w')

flog.writelines('finetuning alexnet, weights model path:{}\n'.format(weights_path)+\
                'learning_rate = {}, learningRate_decayIters={}\ntrain_batch_size={},test_batch_size={}\n'.format(start_learning_rate,learningRate_decayIters, train_batch_size,test_batch_size)+\
                'train_batches_per_epoch={0},val_batches_per_epoch={1}\n'.format(train_batches_per_epoch,val_batches_per_epoch)+\
                'num_epochs={0}\ntest_iter={1}\n'.format(num_epochs,test_iter)+\
                'num_classes={0}\ntrain_layers={1}\n'.format(num_classes,train_layers))

maxIters = num_epochs * train_batches_per_epoch
print('{} maxIters= {}'.format(datetime.now(), maxIters))
flog.writelines('maxIters={}\n'.format(maxIters))
# record train iters
trainIters = 0

# Start Tensorflow session
with tf.Session() as sess:

    # Initialize all variables
    sess.run(tf.global_variables_initializer())
    #sess.run(tf.initialize_all_variables())

    # Add the model graph to TensorBoard
    writer.add_graph(sess.graph)

    # Load the pretrained weights into the non-trainable layer
    model.load_initial_weights(sess,isTrainable=True)
    # To continue training from one of checkpoints
    #saver.restore(sess, "path/to/checkpoint/model.ckpt")
    #coord = tf.train.Coordinator()
    #threads = tf.train.start_queue_runners(coord=coord, sess = sess)

    print("{} Start training...".format(datetime.now()))
    print("{} Open Tensorboard at --logdir {}".format(datetime.now(),
                                                    filewriter_path))
    flog.writelines("{} Start training...\n".format(datetime.now()))
    flog.writelines("{} Open Tensorboard at --logdir {}\n".format(datetime.now(),
                                                    filewriter_path))
    # Loop over number of iterations
    while trainIters < maxIters:

        # Get a batch of images and labels
        #batch_xs, batch_ys = train_generator.next_batch(train_batch_size,rootPath)
        #image_out, label_out, label_batch_one_hot_out, filename_out = sess.run([image_batch, label_batch_out,
        #                                                                        label_batch_one_hot, filename_batch])

        # get next batch of data
        img_batch, label_batch = tr_data_generator.next_batch()
        img_batch, label_batch = sess.run([img_batch, label_batch])

        # And run the training op
        sess.run(train_op, feed_dict={x: img_batch,
                                      y: label_batch,
                                      keep_prob: dropout_rate})
        trainIters += 1
        # Generate summary with the current batch of data and write to file
        if trainIters % display_step == 0:
            s = sess.run(merged_summary, feed_dict={x: img_batch,
                                                    y: label_batch,
                                                    keep_prob: 1.})
            writer.add_summary(s, trainIters)
            # 计算精度
            #acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})
            # 计算损失值
            acc, loss_value = sess.run([accuracy, loss], feed_dict={x: img_batch,
                                                                    y: label_batch,
                                                                    keep_prob: 1.})
            # 获取当前学习率
            lr = sess.run(learning_rate)
            print("{} Iter {}, lr = {},  Loss={}, Training Accuracy= {}".format(
                    datetime.now(), trainIters,lr,loss_value, acc))

            flog.writelines("{} Iter {}, lr = {}, Loss={}, Training Accuracy= {}\n".format(
                    datetime.now(), trainIters,lr,loss_value, acc))

        # Validate the model on the entire validation set
        if trainIters % test_iter == 0 or trainIters == maxIters:
            print("{} Iter {}, Start validation".format(datetime.now(), trainIters))
            flog.writelines("{} Iter {}, Start validation\n".format(datetime.now(), trainIters))
            #val_image_out, val_label_out, val_label_batch_one_hot_out, val_filename_out = sess.run([val_image_batch, val_label_batch_out,
             #                                                                   val_label_batch_one_hot, val_filename_batch])

            test_acc = 0.
            test_count = 0
            test_loss = 0.
            for _ in range(val_batches_per_epoch):
                img_batch, label_batch = val_data_generator.next_batch()
                img_batch, label_batch = sess.run([img_batch, label_batch])
                #batch_tx, batch_ty = val_generator.next_batch(test_batch_size,rootPath)
                acc_tess, loss_test = sess.run([accuracy, loss], feed_dict={x: img_batch,
                                                    y: label_batch,
                                                    keep_prob: 1.})
#==============================================================================
#                 loss_test = sess.run(loss, feed_dict={x: batch_tx,
#                                                     y: batch_ty,
#                                                     keep_prob: 1.})
#==============================================================================
                test_acc += acc_tess
                test_loss += loss_test
                test_count += 1
            test_acc /= test_count
            test_loss /= test_count
            print("{} Iter {}, Validation Loss= {}".format(datetime.now(),trainIters, test_loss) + \
                  ", Validation Accuracy = {}".format(test_acc))
            flog.writelines("{} Iter {}, Validation Loss= {}".format(datetime.now(), trainIters, test_loss) + \
                  ", Validation Accuracy = {}\n".format(test_acc))
            #val_generator.reset_pointer()

        # Reset the file pointer of the image data generator
        #if trainIters % train_batches_per_epoch==0:
           # train_generator.reset_pointer()
        if trainIters % saveModel_iter == 0:
            print("{} Saving checkpoint of model...".format(datetime.now()))
            flog.writelines("{} Saving checkpoint of model...\n".format(datetime.now()))

            #save checkpoint of the model
            checkpoint_name = os.path.join(checkpoint_path, 'model_epoch_'+'_iter_'+str(trainIters)+'.ckpt')
            save_path = saver.save(sess, checkpoint_name)

            print("{} Model checkpoint saved at {}".format(datetime.now(), checkpoint_name))
            flog.writelines("{} Model checkpoint saved at {}\n".format(datetime.now(), checkpoint_name))

    #coord.request_stop()
    #coord.join(threads)
    sess.close()

print('finish {} epoch, {} iterations training'.format(num_epochs, maxIters))
end_time = datetime.now()
print('start time:{}'.format(start_time))
print('end time:{}'.format(end_time))

flog.writelines('finish {} epoch training\n'.format(num_epochs))
flog.writelines('start time:{}'.format(start_time) + ', end time:{}\n'.format(end_time))
flog.close()


###############################################################################
# Kolton Benoit
# Script to resize images
###############################################################################
import os, sys, argparse
from PIL import Image

#######################################
# Argument Parsing
#######################################
parser = argparse.ArgumentParser(description="Resize Image")
parser.add_argument("-f", required=True, help="The path to the image to be resized")
parser.add_argument("-r", default=0.5, dest="r",
                    help="The ratio from 0.0 to 1.0 to size the image to. I.E., " +
					"0.75 = 75%. Defaults to 0.5")
parser.add_argument("-w", default=0, des="w",
					help="The width to scale the image to. Will scale height by " +
					"the same ratio if it isn't provided. This will override -r")
parser.add_argument("-h", default=0, des="h",
					help="The height to scale the image to. Will scale width by " +
					"the same ratio if it isn't provided. This will override -r")
args = parser.parse_args()

#######################################
# Image scaling
#######################################
try:
	#Open the image
	im = Image.open(args.f)

	#Calculate the size based on the ratio.
	size = (int(im.width * args.r), int (im.height * args.r))

	#If the width or height argument isn't 0
	if args.w != 0 or args.h != 0:
		#If width is 0, calculate it based on the height's ratio
		if args.w == 0:
			size = (int((args.h / im.height) * im.width), int(args.h))
		#Else if height is 0, calculate it based on the width's ratio
			size = (int(args.w), int((args.w / im.width) * im.height))
		#Else set size to the given width and height
			size = (int(args.w), int(args.h))

	#Resize the image to the calculated size
	out = im.resize(size)
	#Save the resized image
	outfile = args.f + "_s.jpg"
	out.save(outfile, "JPEG")
except IOError:
	print("Can't resize ", args.f)


fp = open('requete_UTF8.txt')
result = {}
for line in fp.readlines():
    table = line.split(' ')
    for word in table:
        word = word.strip("\n").lower()
        result.setdefault(word, 0)
        result[word] += 1
fp.close()

fp = open('request', 'w')
for word, nb in result.items():
    fp.write(word + "\t" + str(nb) + "\n")
fp.close()


import cv2

cap0 = cv2.VideoCapture('/dev/video0')
cap1 = cv2.VideoCapture('/dev/video4')
cap2 = cv2.VideoCapture('/dev/video8')

cap0.set(3, 160)
cap0.set(4, 120)

cap1.set(cv2.CAP_PROP_FRAME_WIDTH, 240)
cap1.set(cv2.CAP_PROP_FRAME_HEIGHT, 320)

cap2.set(cv2.CAP_PROP_FRAME_WIDTH, 240)
cap2.set(cv2.CAP_PROP_FRAME_HEIGHT, 320)

while True:
    ret0, frame0 = cap0.read()
    ret1, frame1 = cap1.read()
    ret2, frame2 = cap2.read()

    cv2.imshow('frame0', frame0)
    cv2.imshow('frame1', frame1)
    cv2.imshow('frame2', frame2)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap0.release()
cap1.release()
cap2.release()
cv2.destroyAllWindows()


#!/usr/bin/env python2
# -*- encoding UTF-8 -*-
"""
Module which defines graph search algorithms.
"""

import Queue as queue
# import unittest
import graph
import csv
import pprint

# class NodeInformation(object):
#     """
#     A thin wrapper to hold information about a node used in the assignment.
#     """

def manhatan_distance(n1, n2):
    x1, y1 = n1.properties["loc"]
    x2, y2 = n2.properties["loc"]
    return abs(x2 - x1) + abs(y2 - y1)

class AStarSearch(object):
    """
    An A* search object.
    """

    def __init__(self, network, start, goal, heuristic):  # -> None
        """
        Build an AStar search.

        @param network {graph.Graph} The graph to search

        @param start {graph.Node} The node from which to start the search

        @param goal {graph.Node} The node which we wish to find a path to

        @param heuristic {(n1: Node, n2: Node) -> int} a function which guides
        the search.
        """
        self.graph = network
        self.start = start
        self.goal = goal
        self.heuristic = heuristic
        self._white = queue.PriorityQueue()
        self._white.put((0, start))
        self._from = {start: None}
        self._consideration = []
        self._cost = {start: 0}
        self._locations_searched = 0

    def run(self):  # -> None

        self._consideration.append((0, self.start))
        while not self._white.empty():
            node = self._white.get()[1]
            self._locations_searched += 1

            if node is self.goal:
                print("found goal")
                break

            for edge in self.graph.incident_edges(node):
                current_cost = self._cost[node] + edge.properties["weight"]
                neighbor = edge.low if edge.low is not node else edge.high
                if neighbor not in self._cost or current_cost < self._cost[neighbor]:
                    self._cost[neighbor] = current_cost
                    estimate = current_cost + self.heuristic(self.goal, neighbor)
                    self._white.put((estimate, neighbor))
                    self._consideration.append((estimate, neighbor))
                    self._from[neighbor] = node

    @property
    def cost(self):
        return self._cost

    def path(self):
        last = self._from.get(self.goal, None)
        yield last
        while last is not None:
            yield last
            last = self._from.get(last, None)

    def travel(self):
        return self._from

def build_graph_from_file(file_name):  # -> graph.Graph
    network = graph.Graph()
    pp = pprint.PrettyPrinter()
    terain_matrix = []
    with open(file_name, 'rb') as csvfile:
        reader = csv.reader(csvfile, delimiter=' ')
        for row in reader:
            terain_matrix.append([])
            for terain_code in row:
                terain_matrix[-1].append(int(terain_code))

    del terain_matrix[-1]

    pp.pprint(terain_matrix)
    N = len(terain_matrix)
    M = len(terain_matrix[0])

    node_matrix = [[None for i in range(M)] for j in range(N)]

    for i in range(N):
        for j in range(M):
            node_matrix[i][j] = network.add_vertex(graph.Node(key=hash((i, j))))
            node_matrix[i][j].properties["loc"] = (i, j)

    for i, row in zip(range(N), terain_matrix):
        for j, code in zip(range(M), row):
            if code == 2:
                continue

            node = node_matrix[i][j]

            if j != M - 1:
                down = node_matrix[i][j + 1]
                e = build_edge(terain_matrix, node, down)
                if e is not None:
                    network.add_edge(e)

            if i != N - 1:
                right = node_matrix[i + 1][j]
                e = build_edge(terain_matrix, node, right)
                if e is not None:
                    network.add_edge(e)

            if i != N - 1 and j != M - 1:
                down_right = node_matrix[i + 1][j + 1]
                e = build_edge(terain_matrix, node, down_right)
                if e is not None:
                    network.add_edge(e)

    start = node_matrix[-1][0]
    goal = node_matrix[0][-1]
    return (network, start, goal)

def build_edge(terain_matrix, source, target):
    si, sj = source.properties["loc"]
    i, j = target.properties["loc"]
    code = terain_matrix[i][j]
    if code == 2:
        return None
    if si != i and sj != j:
        d = "d"
    elif si != i:
        d = "h"
    elif sj != j:
        d = "v"
    else:
        raise ValueError("bad edge")
    c = cost(d, code)
    if c is not None:
        e = graph.Edge(source, target)
        e.properties["weight"] = c
        return e
    else:
        return None

_direction_cost = {"h": 10, "v": 10, "d": 14}
_terain_cost = {1: 10, 0: 0}

def cost(direction, terain):
    if terain == 2:
        return None
    else:
        return _direction_cost[direction] + _terain_cost[terain]

def main():
    network, start, goal = build_graph_from_file("World1.txt")
    print("start = ", start.properties["loc"])
    print("goal = ", goal.properties["loc"])
    heuristic = manhatan_distance
    search = AStarSearch(network, goal, start, heuristic)
    search.run()
    for node in search.path():
        print(node.properties["loc"])

if __name__ == "__main__":
    main()


from UI.render import Window
from UI.render import Screen, Sprite
from UI.getchar import Getchar
import time

class Village:

    active_door = Sprite('graphic/village/door_active.txt')
    door = Sprite('graphic/village/door.txt')

    direct_dict = {
        'wetlands': [
            Screen(4, 13, 10, 3),
            Sprite('graphic/village/wetlands.txt'),
            Sprite('graphic/village/wetlands_active.txt'),
        ],
        'new_forrest': [
            Screen(4, 28, 10, 3),
            Sprite('graphic/village/new_forrest.txt'),
            Sprite('graphic/village/new_forrest_active.txt'),
        ],
        'tavern': [
            Screen(55, 23, 7, 5),
            door,
            active_door,
        ],
        'armory': [
            Screen(91, 23, 7, 5),
            door,
            active_door,
        ],
        'magic': [
            Screen(116, 23,  7, 5),
            door,
            active_door,
        ],
        'old_forrest': [
            Screen(118, 39,  11, 3),
            Sprite('graphic/village/old_forrest.txt'),
            Sprite('graphic/village/old_forrest_active.txt'),
        ],
    }

    @staticmethod
    def draw_template():
        Window.draw_template('graphic/village/template.txt')

    @staticmethod
    def select():
        getch = Getchar()
        Village.draw_template()

        focus_list = [
            'wetlands', 'new_forrest', 'tavern',
            'armory', 'magic', 'old_forrest'
        ]

        focus = 2
        Village.direct_dict[focus_list[focus]][0].draw_sprite(
            Village.direct_dict[focus_list[focus]][2]
        )

        while True:
            action = getch().lower()

            if action == 'a':
                Village.direct_dict[focus_list[focus]][0].draw_sprite(
                    Village.direct_dict[focus_list[focus]][1]
                )
                focus -= 1
                if focus < 0:
                    focus = len(focus_list) - 1
                elif focus >= len(focus_list):
                    focus = 0
                Village.direct_dict[focus_list[focus]][0].draw_sprite(
                    Village.direct_dict[focus_list[focus]][2]
                )
            elif action == 'd':
                Village.direct_dict[focus_list[focus]][0].draw_sprite(
                    Village.direct_dict[focus_list[focus]][1]
                )
                focus += 1
                if focus < 0:
                    focus = len(focus_list) - 1
                elif focus >= len(focus_list):
                    focus = 0
                Village.direct_dict[focus_list[focus]][0].draw_sprite(
                    Village.direct_dict[focus_list[focus]][2]
                )
            elif action == ' ':
                return focus_list[focus]

    @staticmethod
    def tavern():
        pass

    @staticmethod
    def armory():
        pass

    @staticmethod
    def magic():
        pass


#!/usr/bin/env python3
"""
Author : jasongiles
Date   : 2019-04-15
Purpose: Rock the Casbah
"""

import argparse
import sys
import os
import logging
from itertools import zip_longest

# --------------------------------------------------
def get_args():
    """get command-line arguments"""
    parser = argparse.ArgumentParser(
        description="Hamming Distance",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )

    parser.add_argument("FILE", metavar="FILE", help="File inputs", nargs=2)

    parser.add_argument("-d", "--debug", help="logging file", action="store_true")

    return parser.parse_args()

# --------------------------------------------------
def warn(msg):
    """Print a message to STDERR"""
    print(msg, file=sys.stderr)

# --------------------------------------------------
def test_dist():
    """dist ok"""
    tests = [
        ("foo", "boo", 1),
        ("foo", "faa", 2),
        ("foo", "foobar", 3),
        ("TAGGGCAATCATCCGAG", "ACCGTCAGTAATGCTAC", 9),
        ("TAGGGCAATCATCCGG", "ACCGTCAGTAATGCTAC", 10),
    ]

    for s1, s2, n in tests:
        d = dist(s1, s2)
        print(s1)
        print(s2)
        assert d == n

def die(msg="Something bad happened"):
    """warn() and exit with error"""
    warn(msg)
    sys.exit(1)

def dist(a, b):
    if type(a) is str:
        a = a.split()
    if type(b) is str:
        b = b.split()
    zipped = zip(a, b)
    # print(list((zipped)))
    i = 0
    for zip1, zip2 in zipped:
        if len(zip1) > len(zip2):
            longest = zip1
        else:
            longest = zip2
        j = 0
        for letter in range(len(longest)):
            # print(zip1[letter:letter+1],'ballz')
            if zip1[letter: letter + 1] != zip2[letter: letter + 1]:
                i += 1
                j += 1
                # print(zip1[letter:letter+1],'hello')
        logging.debug("s1 = {}, s2 = {}, d = {}".format(zip1, zip2, j))
        # print('s1 = {}, s2 = {}, d = {}'.format(zip1,zip2,j))
    # print(i)
    return i

# --------------------------------------------------
def main():
    """Make a jazz noise here"""
    args = get_args()
    giles = args.FILE
    debug = args.debug

    logging.basicConfig(
        filename=".log",
        filemode="w",
        level=logging.DEBUG if args.debug else logging.CRITICAL, )

    for gile in giles:
        if not os.path.isfile(gile):
            die('"{}" is not a file'.format(gile))

    text1 = open(giles[0])
    text2 = open(giles[1])

    text1_list = []
    text2_list = []

    for line in text1:
        text1_list = text1_list + line.split()
    # print(text1_list)
    for line in text2:
        text2_list = text2_list + line.split()
    # print(text2_list)

    # print(sum(map(dist,text1_list,text2_list)),'checker1')
    logging.debug("file1 = {}, file2 = {}".format(giles[0], giles[1]))
    d = dist(text1_list, text2_list)
    print(d)

# --------------------------------------------------
if __name__ == "__main__":
    main()


class BuildingElement:
    def __init__(self, **kwargs):
        self._A = kwargs['A'] if 'A' in kwargs.keys() else 1.0           # area [m^2]
        self._t = kwargs['t'] if 't' in kwargs.keys() else 0.0           # thickness [m]
        self._k = kwargs['k'] if 'k' in kwargs.keys() else 0.0           # conduction coefficient [W/(m.K)]
        self._rho = kwargs['rho'] if 'rho' in kwargs.keys() else 0.0     # mass density [kg/m^3]
        self._cm = kwargs['cm'] if 'cm' in kwargs.keys() else 0.0        # specific thermal capacity [J/(kg.K)]
        self._cf_r = kwargs['cf_r'] if 'cf_r' in kwargs.keys() else 0.0  # correction factor for resistance

        self._r = kwargs['r'] if 'r' in kwargs.keys() else 0.0           # thermal resistance of 1 m^2 [(m^2.K)/W]
        self._ca = kwargs['ca'] if 'ca' in kwargs.keys() else 0.0        # thermal capacity per m^2 [J/(m^2.K)]

    @property
    def r(self):
        if not self._r:
            self._r = self._t / self._k + self._cf_r
        return self._r

    @property
    def u(self):
        return 1.0 / self.r

    @property
    def U(self):
        return self.u * self._A

    @property
    def ca(self):
        if not self._ca:
            self._ca = self._rho * self._cm * self._t
        return self._ca

    @property
    def C(self):
        return self.ca * self._A

    @property
    def A(self):
        return self._A

    @property
    def t(self):
        return self._t

class BuildingCompositeElement:
    def __init__(self, *args):
        self._r = 0.0
        self._ca = 0.0
        self.building_elements = []
        for elem in args:
            self.building_elements.append(elem)

    @property
    def r(self):
        A = 0.0
        U = 0.0
        for elem in self.building_elements:
            A += elem.A
            U += elem.U
        self._r = A / U
        return self._r

    @property
    def ca(self):
        A = 0.0
        C = 0.0
        for elem in self.building_elements:
            A += elem.A
            C += elem.C
        self._ca = C / A
        return self._ca

    @property
    def t(self):
        for elem in self.building_elements:
            if elem.t: return elem.t
        return 0.0

class BuildingPart:
    def __init__(self, *elements, cf_u=0.0):
        self.building_elements = elements
        self._r = 0.0               # specific thermal resistance of building part [(m^2.K)/W]
        self._ca = 0.0              # specific thermal capacity per unit area of building part [J/(m^2.K)]
        self._t = 0.0               # total thickness of building part [m]
        self._cf_u = cf_u           # eventual correction factor for u-value

        self.T_inside = 0.0         # temperature at the inside of the building part [°C]
        self.T_outside = 0.0        # temperature at the outside of the building part [°C]
        self.A_inside = 0.0         # inside surface of building part [m^2]
        self.A_outside = 0.0        # outside surface of building part [m^2]

    @property
    def r(self):
        self._r = 0.0
        for elem in self.building_elements:
            self._r += elem.r
        self._r = 1.0 / (1.0 / self._r + self._cf_u)
        return self._r

    @property
    def u(self):
        return 1.0 / self.r

    @property
    def ca(self):
        self._ca = 0.0
        for elem in self.building_elements:
            self._ca += elem.ca
        return self._ca

    @property
    def t(self):
        self._t = 0.0
        for elem in self.building_elements:
            self._t += elem.t
        return self._t

    def __call__(self, T_inside=0.0, T_outside=0.0, A_inside=0.0, A_outside=0.0):
        self.T_inside = T_inside
        self.T_outside = T_outside
        self.A_inside = A_inside
        self.A_outside = A_outside

class Space:
    def __init__(self, *building_parts):
        self.building_parts = building_parts
        self.T_inside = 22.0        # space air temperature [°C]
        self.T_outside = -10.0      # outside air temperature [°C]
        self.r_conv_wi = 0.13       # thermal convection resistance on inside of wall [(m^2.K)/W]
        self.r_conv_ci = 0.11       # thermal convection resistance on inside of ceiling [(m^2.K)/W]
        self.r_conv_o = 0.04        # thermal convection resistance on the outside [(m^2.K)/W]
        self.floor_area = 0.0       # floor area [m^2]
        self.space_height = 0.0     # height of space [m]
        self.Qtr = 0.0              # transmission heat loss [W]

    def __call__(self, T_inside=22.0, T_outside=-10.0, r_conv_wi=0.13, r_conv_ci=0.11, r_conv_o=0.04, floor_area=0.0,
                 space_height=0.0):
        self.T_inside = T_inside
        self.T_outside = T_outside
        self.r_conv_wi = r_conv_wi
        self.r_conv_ci = r_conv_ci
        self.r_conv_o = r_conv_o
        self.floor_area = floor_area
        self.space_height = space_height

    def _Rtr(self):
        # global thermal transmission resistance [K/W]
        self.Qtr = 0.0  # transmission heat loss of space [W]
        for building_part in self.building_parts:
            self.Qtr += building_part.u * (building_part.A_inside + building_part.A_outside) / 2.0 \
                   * (building_part.T_inside - building_part.T_outside)
        return (self.T_inside - self.T_outside) / self.Qtr

    @property
    def R_rm1(self):
        # global thermal convection resistance between room air and building mass [K/W]
        A_inside = 0.0
        for building_part in self.building_parts:
            A_inside += building_part.A_inside
        return self.r_conv_wi / A_inside

    @property
    def R_rm2(self):
        # global thermal conduction resistance between room air and building mass [K/W]
        return 0.5 * self._Rtr()

    @property
    def R_rm(self):
        # global thermal resistance (convection + conduction) between room air and building mass [K/W]
        return self.R_rm1 + self.R_rm2

    @property
    def R_mo1(self):
        # global thermal conduction resistance between building mass and outside environment [K/W]
        return 0.5 * self._Rtr()

    @property
    def R_mo2(self):
        # global thermal convection resistance between building mass and outside environment [K/W]
        A_outside = 0.0
        for building_part in self.building_parts:
            A_outside += building_part.A_outside
        return self.r_conv_o / A_outside

    @property
    def R_mo(self):
        # global thermal resistance (convection + conduction) between building mass and outside air [K/W]
        return self.R_mo1 + self.R_mo2

    @property
    def C_r(self):
        # thermal capacity of room air [J/K]
        V_inside = self.floor_area * self.space_height
        return 1.205 * 1005.0 * V_inside

    @property
    def C_m(self):
        # thermal capacity of building mass [J/K]
        C_m = 0.0
        for building_part in self.building_parts:
            C_m += building_part.ca * (building_part.A_inside + building_part.A_outside) / 2.0
        return C_m

if __name__ == '__main__':
    import copy

    facing_brick = BuildingElement(A=1.22e-2, t=0.088, k=0.91, rho=1500.0, cm=840.0)
    facing_mortar = BuildingElement(A=3.18e-3, t=0.088, k=1.5, rho=1800.0, cm=840.0)
    facade = BuildingCompositeElement(facing_brick, facing_mortar)

    cavity = BuildingElement(t=0.02, r=0.085, rho=1.205, cm=1005.0)

    insulation = BuildingElement(t=0.06, k=0.041, rho=25.0, cm=840.0, cf_r=-0.1)

    big_brick = BuildingElement(A=3.97e-2, t=0.138, k=0.32, rho=1100.0, cm=840.0)
    inner_mortar = BuildingElement(A=3.46e-3, t=0.138, k=1.0, rho=1900.0, cm=840.0)
    inner_wall = BuildingCompositeElement(big_brick, inner_mortar)

    plaster = BuildingElement(t=0.01, k=0.57, rho=1300.0, cm=840.0)

    wall = BuildingPart(facade, cavity, insulation, inner_wall, plaster, cf_u=0.018)

    concrete_slab = BuildingElement(t=0.15, k=1.9, rho=2500.0, cm=840.0)

    ceiling = BuildingPart(concrete_slab, plaster)

    floor_surface = BuildingElement(t=0.015, k=3.5, rho=3000.0, cm=840.0)

    floor = BuildingPart(concrete_slab, floor_surface)

    print(f'wall: r = {wall.r} (m^2.K)/W, ca = {wall.ca} J/(m^2.K)')
    print(f'ceiling: r = {ceiling.r} (m^2.K)/W, ca = {ceiling.ca} J/(m^2.K)')
    print(f'floor: r = {floor.r} (m^2.K)/W, ca = {floor.ca} J/(m^2.K)')

    wall_1 = copy.deepcopy(wall)
    wall_2 = copy.deepcopy(wall)
    wall_3 = copy.deepcopy(wall)
    wall_4 = copy.deepcopy(wall)

    wall_1(
        T_inside=22.0,
        T_outside=-10.0,
        A_inside=9.0,
        A_outside=9.0 + wall_2.t + wall_4.t
    )
    wall_2(
        T_inside=22.0,
        T_outside=-10.0,
        A_inside=9.0,
        A_outside=9.0 + wall_1.t + wall_4.t
    )
    wall_3(
        T_inside=22.0,
        T_outside=-10.0,
        A_inside=12.0,
        A_outside=12.0 + wall_2.t + wall_4.t
    )
    wall_4(
        T_inside=22.0,
        T_outside=-10.0,
        A_inside=12.0,
        A_outside=12.0 + wall_1.t + wall_3.t
    )
    ceiling(
        T_inside=22.0,
        T_outside=18.0,
        A_inside=12.0,
        A_outside=(4.0 + wall_1.t + wall_3.t) * (3.0 + wall_2.t + wall_4.t)
    )
    floor(
        T_inside=22.0,
        T_outside=10.0,
        A_inside=12.0,
        A_outside=ceiling.A_outside
    )

    room = Space(wall_1, wall_2, wall_3, wall_4, floor, ceiling)
    room(
        T_inside=22.0,
        T_outside=-10.0,
        floor_area=floor.A_inside,
        space_height=3.0
    )

    print(f"thermal resistance between 'room air' and 'building mass' = {room.R_rm:.2e} K/W")
    print(f"thermal resistance between 'building mass' and 'outside' = {room.R_mo:.2e} K/W")
    print(f"thermal capacity of 'room air' = {room.C_r:.2e} J/K")
    print(f"thermal capacity of 'building mass' = {room.C_m:.2e} J/K")


class Solution:
    def letterCasePermutation(self, S):
        """
        :type S: str
        :rtype: List[str]
        """
        ans = [[]]
        for c in S:
            n = len(ans)
            if c.isalpha():
                for i in range(n):
                    ans.append(ans[i][:])
                    ans[i].append(c.lower())
                    ans[n+i].append(c.upper())
            else:
                for i in range(n):
                    ans[i].append(c)
        for i in range(len(ans)):
            ans[i] = ''.join(ans[i])
        return ans


# Solution was taken from HackerRank on youtube, because my solution was a lot more complicated and failed a couple of test cases.
# Did not get the logic right

#!/bin/python3

import math
import os
import random
import re
import sys

# sum of any 2 sides has to be greater than the 3rd side
def maximumPerimeterTriangle(sticks):
    sticks.sort()
    i = len(sticks) - 3

    while i >= 0 and sticks[i] + sticks[i+1] <= sticks[i+2]:
        i -= 1

    if i >= 0:
        return [sticks[i], sticks[i+1], sticks[i+2]]
    else:
        return [-1]

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input().strip())

    sticks = list(map(int, input().rstrip().split()))

    result = maximumPerimeterTriangle(sticks)

    fptr.write(' '.join(map(str, result)))
    fptr.write('\n')

    fptr.close()


import logging
import sys

# To solve ModuleNotFoundError in anki/__init__.py
sys.path.append('./external/anki')
from external.anki.anki import Collection as aopen

class CardManager(object):
    def __init__(self, coll_file, deck_name):
        self.coll_file = coll_file
        self.deck_name = deck_name

    def __enter__(self):
        self.deck = aopen(self.coll_file)
        self.deckID = self.deck.decks.id(self.deck_name)
        self.deck.decks.select(self.deckID)
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type is None:
            self.deck.save()
            self.deck.close()
        else:
            self.deck.close()

    def has_model(self, model_name):
        models = self.deck.models.allNames()
        return model_name in models

    def list_models(self):
        print("The list of the model (card type):")
        for item in self.deck.models.allNames():
            print("Name of model: {}".format(item))

    def create_model(self, model_name):
        new_model = self.deck.models.new(model_name)
        # ====== copy the color blue ================
        # Change flds, tmples, css, req to other type
        basic_model = self.deck.models.byName('ColorBlue')
        equallist = ['flds', 'tmpls', 'css', 'req']
        for item in equallist:
            new_model[item] = basic_model[item]
        # ============================================
        self.deck.models.add(new_model)
        self.deck.save()

    def create_note(self, model_name, content_dic, tags=None):
        select_model = self.deck.models.byName(model_name)
        self.deck.models.setCurrent(select_model)
        select_model['did'] = self.deckID
        note = self.deck.newNote(forDeck=False)
        if tags:
            for t in tags:
                note.addTag(t)
        for key, value in content_dic.items():
            # print("The content of {} is {}".format(key, content_dic[key]))
            note[key] = content_dic[key]
        self.deck.addNote(note)

def test():
    import libs.config_loader
    args = libs.config_loader.load_config()
    coll_file = args.collection
    deck_name = args.deck
    print(coll_file, deck_name)

    model_name = 'KindleJP'
    content_dict = {'word': '行く', 'stem': 'いく', 'context': 'どこに行きますか。', 'explanation': 'BlahBlah'}

    with CardManager(coll_file, deck_name) as cm:
        cm.create_note(model_name, content_dict)

if __name__ == '__main__':
    logging.getLogger().setLevel(logging.DEBUG)
    test()


import tensorflow as tf
import numpy as np
import os

#-----------------------------------------------------------------------------------------

def create_dir(dir_name):
    dir = os.path.join(os.getcwd(), dir_name)
    if not os.path.isdir(dir):
        os.makedirs(dir)
   # print("{} directory was created successfully.".format(dir_name))
    return

# ---------------------------------------------------------------------------------------------------

def save_4d_array(array, file_name):
    # file.write("array shape = " + str(array.shape))
    # file.write("\n")
    # file.write("[")
    file = open(file_name, 'a')
    for d1 in array:
        #   file.write("[")
        for d2 in d1:
            #      file.write("[")
            for d3 in d2:
                #         file.write("[")
                np.savetxt(file, d3, fmt='%s')
                #        file.write("]")
                #   file.write("]")
                # file.write("]")
    # file.write("]")
    file.close()
    return

# -----------------------------------------------------------------------------------------------------

def save_1d_array(array, file_name):
    # file.write("array shape = " + str(array.shape))
    # file.write("\n")
    # file.write("[")
    file = open(file_name, "a")
    np.savetxt(file, array, fmt='%s')
    # file.write("]")
    # file.write("\n")
    file.close()
    return

#---------------------------------------------------------------------------------------

def save_2d_array(array , file_name):
    file = open(file_name , "a")
    for d1 in array:
        np.savetxt(file , d1 , fmt='%s')
    file.close()
    return

#-------------------------------------------------------------------------------------------

def saveLog(loss , acc , iteration , dir , learning_rate , file_name):
    file = open(dir + file_name , "a")
    record = str(loss) + " " + str(acc) + " " + str(iteration) + " " + str(learning_rate) +  "\n"
    file.write(record)
    file.close()
    return

# -----------------------------------------------------------------------------------------------------

def save_networkInfo(model , n_epoch , batch_size , optimizer , dataset , testing_acc):
    file_name = "NetworkInfo.txt"
    file = open(file_name , "w")
    record = "model : " + str(model) + "\n" \
             "n_epoch : " + str(n_epoch) + "\n" \
             "batch_size : " + str(batch_size) + "\n" \
             "optimizer : " + str(optimizer) + "\n" \
             "dataset : " + str(dataset) + "\n" \
             "testing accuracy : " + str(testing_acc) + "\n"
    file.write(record)
    file.close()
    return

#---------------------------------------------------------------------------------------------

def save_weights_to_file(session , weights , weights_dir , dir):
        working_dir = weights_dir + dir
        create_dir(working_dir)
        for name in weights.keys():
            file_name = working_dir + "/" + str(name) + ".txt"
            weight = session.run(weights[name])
            if ("convolution" in name):
                save_4d_array(weight , file_name)

            elif ("fc" in name):
                    save_2d_array(weight , file_name)

            else:
                    continue

            #print("array with shape : " + str(weight.shape) + " " + "was saved successfully")

        #print("saving operation : done :)")

# -----------------------------------------------------------------------------------------------------


number = int ( input("Enter number:"))
num = number
rev = 0

def getReverse():
    global num, rev
    while num > 0:
        rev = rev + (num % 10)
        rev = rev * 10
        num = int(num/10)
    rev = int(rev/10)

getReverse()
print(number,"reversed is", rev)


# ############################################################
# Author: Manfred Lee
# Produce prediction based on extreme gradient boosting
# Last updated: July 2017
# ############################################################

def predict(RACE_DATE):
    # ---------------------------------------------------------------------------------------------------------
    # a) Import libraries
    # ---------------------------------------------------------------------------------------------------------
    from sklearn import preprocessing
    import xgboost as xgb
    import numpy as np
    import pandas as pd
    import math
    import random
    import string

    # ---------------------------------------------------------------------------------------------------------
    # b) Declaration of functions for splitting race
    # ---------------------------------------------------------------------------------------------------------
    def race_split(df, stratum_name, ratio):
        num_rand = math.floor(ratio * len(set(df[stratum_name])))
        random.seed(1)
        rand_list = random.sample(list(set(df[stratum_name])), num_rand)
        train = df[df[stratum_name].isin(rand_list)].copy()
        train = train.reset_index()
        train = train.drop('index', axis=1)
        test = df[-df[stratum_name].isin(rand_list)].copy()
        test = test.reset_index()
        test = test.drop('index', axis=1)
        return train, test

    # ---------------------------------------------------------------------------------------------------------
    # c) Initialization
    # ---------------------------------------------------------------------------------------------------------
    print('Initializing...')
    TRAIN_FILE = 'inputs/train.csv'  # Base training dataset
    ADDITIONAL_FILE = 'inputs/additional.csv'  # Another dataset containing additional features
    ADDITIONAL_FILE_2 = 'inputs/additional_2.csv'  # Another dataset containing additional features
    PEDIGREE_FILE = 'inputs/PEDIGREE.csv'  # Information about breeding history
    PRED_FILE = 'inputs/XGBoost_' + RACE_DATE + '.csv'  # Dataset of current race for producing predictions
    PRED_FILE_ADDITIONAL = 'inputs/XGBoost_' + RACE_DATE + '_2.csv'  # Additional features for producing predictions
    PRED_FILE_ADDITIONAL_2 = 'inputs/XGBoost_' + RACE_DATE + '_3.csv'  # Additional features for producing predictions
    TEST_1617_IND = True  # If True, the testing results for races in 2016-17 race season will be produced
    PRED_IND = True  # If True, prediction will be made for current race

    # ---------------------------------------------------------------------------------------------------------
    # d) Read file and clean the data
    # ---------------------------------------------------------------------------------------------------------
    # Read the relevant files
    print('Loading Data...')
    train = pd.read_csv(TRAIN_FILE, encoding='iso-8859-1')
    additional = pd.read_csv(ADDITIONAL_FILE, encoding='iso-8859-1')
    additional_col = list(additional)[6:]
    additional = additional[additional_col]
    additional_2 = pd.read_csv(ADDITIONAL_FILE_2, encoding='iso-8859-1')
    additional_2_col = list(additional_2)[6:]
    additional_2 = additional_2[additional_2_col]
    train = pd.concat([train, additional, additional_2], axis=1)
    col_name_list = list(train)
    pedigree_map = pd.read_csv(PEDIGREE_FILE)

    if PRED_IND == True:
        current = pd.read_csv(PRED_FILE, encoding='iso-8859-1')
        current_len = current.shape[0]
        current_add = pd.read_csv(PRED_FILE_ADDITIONAL, encoding='iso-8859-1')
        current_add_2 = pd.read_csv(PRED_FILE_ADDITIONAL_2, encoding='iso-8859-1')
        current_additional_col = list(current_add)[6:]
        current_additional_2_col = list(current_add_2)[6:]
        current_add = current_add[current_additional_col]
        current_add_2 = current_add_2[current_additional_2_col]
        current = pd.concat([current, current_add, current_add_2], axis=1)
        train = pd.concat([train, current], axis=0)
        train.reset_index(inplace=True)
        train.drop(['index'], axis=1, inplace=True)
        train = train[col_name_list]

    # The following section is used for cleaning the data
    print('Cleaning Data...')
    # Clean FP
    train = train[train['FP'].notnull()]
    train = train[train['FP'] > 0]
    train['FP'] = train['FP'].astype(int)

    # Clear Final Odds
    train = train[train['Final Odds'] > 0]

    # Clean LastFP
    train.set_value(train['Last_FP_2'] == 0, 'Last_FP_2', np.nan)
    train.set_value(train['Last_FP_3'] == 0, 'Last_FP_3', np.nan)
    train.set_value(train['Last_FP_4'] == 0, 'Last_FP_4', np.nan)
    train.set_value(train['Last_FP_5'] == 0, 'Last_FP_5', np.nan)
    train.set_value(train['Last_FP_6'] == 0, 'Last_FP_6', np.nan)

    # Clean MarginLen
    train.set_value((train['MarginLen'].isnull()) & (train['FP'].notnull()) & (train['FP'] > 0), 'MarginLen', 99)

    # Clean Speed
    for index, row in train[train['Speed'].isnull()].iterrows():
        train.set_value(index, 'Speed', np.min(train.loc[train['Season-No'] == row['Season-No'], 'Speed']))

    # Clean Jockey and Stable
    jockey_list = ['J Moreira', 'Z Purton', 'N Callan', 'S Clipperton', 'D Whyte', 'N Rawiller', 'K Teetan',
                   'B Prebble', 'C Schofield', 'K C Leung', 'K K Chiong', 'O Doleuze', 'C Y Ho', 'H T Mo',
                   'H N Wong', 'M L Yeung', 'M F Poon', 'M Chadwick', 'H W Lai', 'U Rispoli', 'T H So',
                   'C Williams', 'K C Ng', 'C Murray', 'W M Lai']
    stable_list = ['J Size', 'J Moore', 'C Fownes', 'W Y So', 'A S Cruz', 'C S Shum', 'P F Yiu', 'Y S Tsui',
                   'A T Millard', 'D E Ferraris', 'C H Yip', 'D J Hall', 'L Ho', 'T P Yung', 'P O\'Sullivan',
                   'K W Lui', 'A Lee', 'R Gibson', 'K L Man', 'C W Chang', 'T K Ng', 'D Cruz']
    train.set_value(~train['Jockey'].isin(jockey_list), 'Jockey', np.nan)
    train.set_value(~train['Stable'].isin(stable_list), 'Stable', np.nan)

    # Clean Dw
    train['Dw'] = train['Dw'].astype(object)

    # Clean Rtg_Diff
    train.set_value(train['Horse_Class'] == 'New', 'Rtg_Diff', 0)
    train.set_value(abs(train['Rtg_Diff']) == train['Rtg'], 'Rtg_Diff', 0)
    train.set_value(train['Rtg'] == 0, 'Rtg_Diff', 0)
    train.set_value(train['Rtg_Diff'].isnull(), 'Rtg_Diff', 0)
    train['Rtg_Diff'] = train['Rtg_Diff'].astype(int)

    # Clean Age_Months
    train.set_value(train['Age_Months'].isnull(), 'Age_Months', train['Age'] * 12)
    train['Age_Months'] = train['Age_Months'].astype(int)

    # Clean Entry_weight
    train.set_value(train['Entry_weight'].isnull(), 'Entry_weight', train['Net_Loading'])
    train['Entry_weight'] = train['Entry_weight'].astype(int)

    # Clean Body Wt
    train.set_value(train['Body Wt'] == 0, 'Body Wt', np.nan)

    # Clean Body Wt Chg
    train.set_value(train['Body Wt Chg'].isnull(), 'Body Wt Chg', 0)
    train['Body Wt Chg'] = train['Body Wt Chg'].astype(int)

    # Clean Bleed
    train.set_value(train['Bleeding'].notnull(), 'Bleeding', 1)
    train.set_value(train['Bleeding'].isnull(), 'Bleeding', 0)
    train['Bleeding'] = train['Bleeding'].astype(int)

    # Clean AvgSpeed (Use LastSpeed to impute for ***)
    train.set_value(train['AvgSpeed'] == '**************', 'AvgSpeed', train['LastSpeed'])
    train['AvgSpeed'] = train['AvgSpeed'].astype(float)

    # Clean Arrival_Year
    year_int = train['Date'].apply(lambda x: int(x[-4:]))
    train.set_value(train['Arrival_Year'] == 0, 'Arrival_Year', np.nan)
    train.set_value(train['Arrival_Year'].notnull(), 'Arrival_Year', year_int - train['Arrival_Year'] + 1)

    # Clean Horse_Class
    for index, row in train.iterrows():
        if type(row['Horse_Class']) != str:
            train.set_value(index, 'Horse_Class', str(row['Horse_Class']))

    # Clean Sick History
    train.set_value(train['Sick History'].notnull(), 'Sick History', 1)
    train.set_value(train['Sick History'].isnull(), 'Sick History', 0)
    train['Sick History'] = train['Sick History'].astype(int)

    # Clean BestGallop (map 0 to nan)
    train.set_value(train['BestGallop_1200'] == 0, 'BestGallop_1200', np.nan)
    train.set_value(train['BestGallop_800'] == 0, 'BestGallop_800', np.nan)
    train.set_value(train['BestGallop_400'] == 0, 'BestGallop_400', np.nan)

    # Clean Activity
    act_split = train['Activity'].apply(lambda x: x.split('/'))
    act_split_1 = [x[0].split(',') for x in act_split]
    for x in act_split_1:
        for i in range(0, 3):
            x[i] = x[i].strip()
            if x[i] == '':
                x[i] = 0
            else:
                x[i] = int(x[i])
    act_split_2 = [x[1].split(',') for x in act_split]
    for x in act_split_2:
        for i in range(0, 3):
            x[i] = x[i].strip()
            if x[i] == '':
                x[i] = 0
            else:
                x[i] = int(x[i])
    train['Gp'] = [x[0] for x in act_split_1]
    train['Jg'] = [x[1] for x in act_split_1]
    train['Sw'] = [x[2] for x in act_split_1]
    train['Ib'] = [x[0] for x in act_split_2]
    train['Ab'] = [x[1] for x in act_split_2]
    train['Gb'] = [x[2] for x in act_split_2]

    # Clean AveRating
    ave_rating = train.groupby(by=['Season-No'])['Rtg'].mean()
    ave_rating = train['Season-No'].map(ave_rating)
    train['AveRating'] = ave_rating

    # Clean Distance
    train['Distance'] = train['Distance'].astype(object)

    # Clean Race_Prize
    train.set_value(train['Race_Prize'] == 0, 'Race_Prize', np.nan)

    # Clean Power_Figure
    train.set_value(train['Power_Figure'] == 0, 'Power_Figure', np.nan)

    # Clean Yeung index
    train.set_value(train['Yeung index'] == '*************', 'Yeung index', np.nan)
    train['Yeung index'] = train['Yeung index'].astype(float)

    # Clean JC Past Incidents
    train.set_value(train['JC Past Incident Extracts'].notnull(), 'JC Past Incident Extracts', 1)
    train.set_value(train['JC Past Incident Extracts'].isnull(), 'JC Past Incident Extracts', 0)
    train['JC Past Incident Extracts'] = train['JC Past Incident Extracts'].astype(int)

    # Clean JC Running Comments
    # Get unique wording
    train.set_value(train['JC Running Comment'].isnull(), 'JC Running Comment', 'None')
    semantics = []
    negative = ['slow', 'never', 'late', 'weakened', 'lost', 'faded', 'unable', 'failed', 'niggled', 'harmed',
                'worse', 'outpaced', 'awkwardly', 'lacked', 'couldnt', 'shaken', 'restrained', 'slowly', 'tired',
                'hampered', 'badly', 'awkward', 'rearward', 'limited', 'difficult', 'trailed', 'tiring', 'disputed',
                'hardridden', 'losing']
    positive = ['eased', 'strongly', 'good', 'winner', 'improved', 'nicely', 'strong', 'muster', 'benefited',
                'quicken', 'overhauled', 'leading', 'enjoyed', 'competitive', 'improve', 'quickening', 'smart',
                'snagged']
    for index, row in train.iterrows():
        sem_ind = 0
        temp_word_list = row['JC Running Comment'].lower()
        temp_word_list = ''.join(c for c in temp_word_list if c not in string.punctuation)
        temp_word_list = temp_word_list.split(" ")
        for word in temp_word_list:
            if word in negative:
                sem_ind = sem_ind - 1
            elif word in positive:
                sem_ind = sem_ind + 1
        semantics.append(sem_ind)
    train['JC Running Comment'] = semantics

    # Create variables for DI and CD
    train['Sire DI'] = np.nan
    train['Dam DI'] = np.nan
    train['Sire CD'] = np.nan
    train['Dam CD'] = np.nan
    sire_available = set(train['Sire']) & set(pedigree_map['Pedigree'])
    dam_available = set(train['Dam']) & set(pedigree_map['Pedigree'])
    for p in list(sire_available):
        train.set_value(train['Sire'] == p, 'Sire DI', np.sum(pedigree_map['DI'][pedigree_map['Pedigree'] == p]))
        train.set_value(train['Sire'] == p, 'Sire CD', np.sum(pedigree_map['CD'][pedigree_map['Pedigree'] == p]))
    for p in list(dam_available):
        train.set_value(train['Dam'] == p, 'Dam DI', np.sum(pedigree_map['DI'][pedigree_map['Pedigree'] == p]))
        train.set_value(train['Dam'] == p, 'Dam CD', np.sum(pedigree_map['CD'][pedigree_map['Pedigree'] == p]))

    # ---------------------------------------------------------------------------------------------------------
    # e) Split train and test data
    # ---------------------------------------------------------------------------------------------------------
    print('Creating training and testing data...')

    train = train.drop(['Season-Race,HorseNo', 'Date', 'No.', 'Horse', 'ABTCount', 'GBTCount', 'Races', 'Activity',
                        'Power_Figure', 'Sire', 'Dam', 'Horse_Owner', 'CountryID', 'Blinker', 'Blinker_New',
                        'SpecialBit',
                        'Horse_Place_SameGoing', 'Horse_Win_SameGoing', 'Horse_Origin'], axis=1)
    train.reset_index(inplace=True)
    train.drop(['index'], axis=1, inplace=True)

    # Transform the season-no index to integer first
    row_index = train[train['Season-No'] == '2016-001'].index[0]  # Get the row index before encoding
    lbl = preprocessing.LabelEncoder()
    lbl.fit(list(train['Season-No'].values))
    train['Season-No'] = lbl.transform(list(train['Season-No'].values))
    train = pd.get_dummies(train)
    if PRED_IND == True:
        pred_df = train[-current_len:]
        train = train[0:(train.shape[0] - current_len)]
        train.reset_index(inplace=True)
        train.drop(['index'], axis=1, inplace=True)
        pred_df.reset_index(inplace=True)
        pred_df.drop(['index'], axis=1, inplace=True)
    if TEST_1617_IND == True:
        test = train[row_index:]
        train = train[0:row_index]
        train.reset_index(inplace=True)
        train.drop(['index'], axis=1, inplace=True)
        test.reset_index(inplace=True)
        test.drop(['index'], axis=1, inplace=True)
    else:
        train, test = race_split(train, 'Season-No', 0.9)

    # Group variables for training extreme gradient boosting
    group_train = train.groupby(by='Season-No').count()['FP']
    group_test = test.groupby(by='Season-No').count()['FP']
    if PRED_IND == True:
        group_pred = pred_df.groupby(by='Season-No').count()['FP']

    # Storing odds, finishing positions and race strata for later uses
    test_win_odds = test['Final Odds']
    test_q_odds = test['Q_Odds']
    test_FP = test['FP']
    test_stratum = test['Season-No']

    # Create training (train_x, train_y), validating (test_x, test_y) and predicting data (pred_x)
    train_x = train.drop(["Season-No", "FP", "Final Odds", "Q_Odds", "Speed", "MarginLen"], axis=1)
    train_y = -(train['FP'] * (train['FP'] <= 4) + 99 * (train['FP'] > 4))  # FP as target
    test_x = test.drop(["Season-No", "FP", "Final Odds", "Q_Odds", "Speed", "MarginLen"], axis=1)
    test_y = -(test['FP'] * (test['FP'] <= 4) + 99 * (test['FP'] > 4))  # FP as target
    if PRED_IND == True:
        pred_x = pred_df.drop(["Season-No", "FP", "Final Odds", "Q_Odds", "Speed", "MarginLen"], axis=1)

    # ---------------------------------------------------------------------------------------------------------
    # f) Fit model
    # ---------------------------------------------------------------------------------------------------------
    print('Fit Model')

    params = {}
    params['eta'] = 0.1
    params['max_depth'] = 6
    params['subsample'] = 0.6
    params['colsample_bytree'] = 0.6
    params['seed'] = 2034
    params['objective'] = 'rank:pairwise'
    params['eval_metric'] = 'ndcg@6'
    d_train = xgb.DMatrix(train_x, label=train_y, missing=np.nan)
    d_train.set_group(group_train)
    d_test = xgb.DMatrix(test_x, label=test_y, missing=np.nan)
    d_test.set_group(group_test)
    watchlist = [(d_train, 'train'), (d_test, 'test')]
    bst = xgb.train(params, d_train, 1000, watchlist, early_stopping_rounds=50, verbose_eval=True)

    # ---------------------------------------------------------------------------------------------------------
    # g) Analysis of test results
    # ---------------------------------------------------------------------------------------------------------
    # 0: Pairwise ranking - FP as target, 99: Odds only
    print('Generating test results...')
    d_test = xgb.DMatrix(test_x, label=test_y, missing=np.nan)
    d_test.set_group(group_test)

    TEST_MODEL = [0]  # If more than 1 model are trained, can specify which model to backtest here
    TEST_DISTANCE = []  # Specify which race distances to test
    TEST_CLASS = []  # Specifiy which race classes to test
    TEST_LOOP = len(TEST_DISTANCE) + len(TEST_CLASS)  # The total number of test is the sum of the 2 above

    if TEST_LOOP == 0:
        TEST_IND = False
        TEST_LOOP += 1
    else:
        TEST_IND = True

    # The following constructs the validation results assuming 2 naive strategies (Please see my blog for details)
    pred_win = list()
    pred_place = list()
    pred_win_odd = list()
    pred_q_win = list()
    pred_q_win_odd = list()
    for j in range(0, TEST_LOOP):
        for i in TEST_MODEL:
            if i == 0:
                pred = pd.DataFrame({'Score': bst.predict(d_test)})
            elif i == 99:
                pred = pd.DataFrame({'Score': 1 / test_win_odds})
            pred['Stratum'] = test_stratum
            pred['Final Odds'] = test_win_odds
            pred['Q_odd'] = test_q_odds
            pred['FP'] = test_FP
            pred['Rank'] = pred.groupby('Stratum')['Score'].rank(ascending=False)
            pred['Exp Score'] = np.exp(pred['Score'])
            if TEST_IND == True and j <= len(TEST_DISTANCE) - 1:
                distance_name = 'Distance_' + str(TEST_DISTANCE[j])
                pred = pred[test[distance_name] == 1]
                pred.reset_index(inplace=True)
                pred.drop(['index'], axis=1, inplace=True)
            elif TEST_IND == True and j > len(TEST_DISTANCE) - 1:
                class_name = 'Horse_Class_' + str(TEST_CLASS[j - len(TEST_DISTANCE)])
                pred = pred[test[class_name] == 1]
                pred.reset_index(inplace=True)
                pred.drop(['index'], axis=1, inplace=True)
            exp_score_by_group = pred.groupby(by='Stratum').sum()['Exp Score']
            pred['Prob'] = 0.0
            for index, row in pred.iterrows():
                pred.set_value(index, 'Prob', row['Exp Score'] / exp_score_by_group[row['Stratum']])
            pred_place.append(sum((pred['Rank'] == 1) & (pred['FP'] <= 3)) / len(set(pred['Stratum'])))
            pred_win.append(sum((pred['Rank'] == 1) & (pred['FP'] <= 1)) / len(set(pred['Stratum'])))
            pred_win_odd.append(sum(pred.loc[(pred['Rank'] == 1) & (pred['FP'] <= 1), 'Final Odds']) /
                                sum((pred['Rank'] == 1) & (pred['FP'] <= 1)))
            # Quinella
            first = pred[(pred['Rank'] == 1) & (pred['FP'] <= 2)]
            second = pred[(pred['Rank'] > 1) & (pred['Rank'] <= 3) & (pred['FP'] <= 2)]
            q_set = set(first['Stratum']) & set(second['Stratum'])
            pred_q_win.append(len(q_set) / len(set(pred['Stratum'])))
            q_ind = [x in list(q_set) for x in pred['Stratum']]
            race_ind = (pred['Rank'] == 1)
            q_ind = [x & y for x, y in zip(q_ind, race_ind)]
            pred_q_win_odd.append(sum(pred.loc[q_ind, 'Q_odd']) / len(q_set))
            pred_results = pd.DataFrame({'Win': pred_win, 'Place': pred_place, 'Win odd': pred_win_odd, 'Q': pred_q_win,
                                         'Q odd': pred_q_win_odd})
            pred_results['Win return'] = [100 * (x * y - 1) / 1 for x, y in
                                          zip(pred_results['Win'], pred_results['Win odd'])]
            pred_results['Q return'] = [100 * (x * y - 20) / 20 for x, y in
                                        zip(pred_results['Q'], pred_results['Q odd'])]

            # Plot Graph
            win_set = list(set(pred.loc[(pred['Rank'] == 1) & (pred['FP'] <= 1), 'Stratum']))
            proj_graph = pd.DataFrame({'Stratum': sorted(list(set(pred['Stratum'])))})
            proj_graph['Final Odds'] = 0.0
            proj_graph['Q_odd'] = 0.0
            for index, row in proj_graph.iterrows():
                win_dvd = pred.loc[(pred['Stratum'] == row['Stratum']) & (pred['FP'] == 1), 'Final Odds'].reset_index()
                win_dvd = win_dvd['Final Odds'][0]
                q_dvd = pred.loc[(pred['Stratum'] == row['Stratum']) & (pred['FP'] == 1), 'Q_odd'].reset_index()
                q_dvd = q_dvd['Q_odd'][0]
                proj_graph.set_value(index, 'Final Odds', win_dvd)
                proj_graph.set_value(index, 'Q_odd', q_dvd)
            proj_graph['Win_Ind'] = 1 * (proj_graph['Stratum'].isin(win_set))
            proj_graph['Win_Gain'] = proj_graph['Win_Ind'] * proj_graph['Final Odds'] * 10 - 10
            proj_graph['Q_Ind'] = 1 * (proj_graph['Stratum'].isin(q_set))
            proj_graph['Q_Gain'] = proj_graph['Q_Ind'] * proj_graph['Q_odd'] - 20
            counter = 0
            for index, row in proj_graph.iterrows():
                if counter == 0:
                    counter += 1
                    temp_win = row['Win_Gain'].copy()
                    temp_q = row['Q_Gain'].copy()
                else:
                    cur_win = row['Win_Gain'].copy()
                    cur_q = row['Q_Gain'].copy()
                    proj_graph.set_value(index, 'Win_Gain', cur_win + temp_win)
                    proj_graph.set_value(index, 'Q_Gain', cur_q + temp_q)
                    temp_win += cur_win
                    temp_q += cur_q
        print(pred_results)

    # ---------------------------------------------------------------------------------------------------------
    # i) Make predictions
    # ---------------------------------------------------------------------------------------------------------
    # Output the predictions to local
    if PRED_IND == True:
        d_pred = xgb.DMatrix(pred_x)
        d_pred.set_group(group_pred)
        p_test = bst.predict(d_pred)
        prediction = pd.DataFrame()
        prediction['Results'] = np.exp(p_test)
        prediction.to_csv('output/Prediction_' + RACE_DATE + '.csv')

    return prediction

if __name__ == '__main__':
    predict()


class YahooDBAdapter:
    def __init__(self,dbConnection):
        self.dbConnection = dbConnection

    def selectStockCodeList(self):
        stockCodeList = []

        # open a cursor
        cursor = self.dbConnection.createCursor()

        # run the query
        query = "SELECT stockCode FROM YahooStockList"
        cursor.execute(query)
        # fetch the results
        for row in cursor:
            stockCode = str(row[0])
            stockCodeList.append(stockCode)

        # close a cursor
        cursor.close()

        return stockCodeList

    def updateQuotation(self,dailyQuote):
        # open a cursor
        cursor = self.dbConnection.createCursor()

        dailyQuoteQuery = '''
            INSERT INTO
            DailyQuotes (code, scraper, isGain, amount, currency, variation, variationPercentage, updateTime)
            VALUES ('%(code)s', '%(scraper)s', '%(isGain)s', '%(amount)s', '%(currency)s', '%(variation)s', '%(variationPercentage)s', '%(updateTime)s')
            ON DUPLICATE KEY UPDATE code='%(code)s', scraper='%(scraper)s', isGain='%(isGain)s', amount='%(amount)s', currency='%(currency)s', variation='%(variation)s', variationPercentage='%(variationPercentage)s', updateTime='%(updateTime)s'
            '''

        dailyQuoteDictionary = {
              'code': dailyQuote.code,
              'scraper': dailyQuote.scraper,
              'isGain': dailyQuote.isGain,
              'amount': dailyQuote.amount,
              'currency': dailyQuote.currency,
              'variation': dailyQuote.variation,
              'variationPercentage': dailyQuote.variationPercentage,
              'updateTime': dailyQuote.updateTime,
            }

        # insert new data
        cursor.execute(dailyQuoteQuery % dailyQuoteDictionary)

        # make sure data is committed to the database
        self.dbConnection.commitTransaction()

        # close a cursor
        cursor.close()


import json
import requests

r = requests.get("http://nymag.com/_components/nymag-latest-feed/instances/index@published")
formattedlist = r.json()
xyz = formattedlist["manualArticleUrls"]

with open('datas.txt', 'a') as outfile:
    for element in xyz:
        outfile.write(element + "\n" )
outfile.close()


import math

class Q(object):
    def __init__(self,a,b=1):       #初期値を書くと省略可能
        #gcd = math.gcd(a,b)
        self.a=a
        self.b=b

    def __repr__(self):
        if self.b==1:
            return str(self.a)
        else:
            return f'{self.a}/{self.b}'

    def __add__(self,q):                #分数の足し算　　受け取るのはq,返すのはQ
        a=self.a                    #自分自身がself
        b=self.b
        c=q.a                       #引数で受け取ったほうがq
        d=q.b
        #print(a,b,c,d)
        a=a*d + b*c
        b=b*d
        return Q(a,b)

    def __sub__(self,q):
        a=self.a
        b=self.b
        c=q.a
        d=q.b
        a=a*d-b*c
        b=b*d
        return Q(a,b)

    def __mul__(self,q):
        a=self.a
        b=self.b
        c=q.a
        d=q.b
        a=a*c
        b=b*d
        return Q(a,b)

    def __truediv__(self,q):
        a=self.a
        b=self.b
        c=q.a
        d=q.b
        a=a*d
        b=b*c
        return Q(a,b)

q1=Q(1,2)
q2=Q(1,3)
q3=Q(4,6)
print(q1 + q2)
print(q1 - q2)
print(q1 * q2)
print(q1 / q2)


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

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
from os import mkdir, path
# from pathlib import Path

# const
faviconFile = 'favicon.png'
mainWindowTitle = 'Тест 4 КДСО (1.1)'
# MainWindow dimensions
mainWindowWidth = 700
mainWindowHeight = 670
# MainWindow position
mainWindowXPos = 100
mainWindowYPos = 200
# init data directory and name file
initFilePath = '.\\'
initFileName = 'cards.txt'
# user work directory and extension file
# if path.isdir('userData'):
#     pass
# else:
#     mkdir('userData')
userFilePath = '.\\'
# userFilePath = '.\\userData\\'
userFileExtension = '.txt'
# fonts for labels for .setFont()
# fontMain = QFont('Arial', 16)

# text for labels
textHintHead = 'Инструкция'

textMainPreTest1 = str('' +
                '
Прочитайте инструкцию ниже
' +
                '
Затем нажмите [Блок #1]
' +
                '')
textHintPreTest1 = str('' +
                '
Вверху окна вам будут показываться ' +
                'слова-прилагательные
' +
                '')
textHintTest1 = str('' +
                '
- прочитайте слово
' +
                '
- нажмите кнопку [Знакомое], если вы знаете/слышали ' +
                'это слово
' +
                '
- нажмите кнопку [Незнакомое], если это слово вам ' +
                'незнакомо/непонятно
' +
                '')
textMainPreTest2 = str('' +
                '
Блок #1 пройден
' +
                '
Теперь прочитайте инструкцию ниже
' +
                '
Затем нажмите [Блок #2]
' +
                '')
textHintPreTest2 = str('' +
                '
Вверху окна вам будут показываться ' +
                'слова-прилагательные, которые вы выбрали как знакомые
' +
                '')
textHintTest2 = str('' +
                '
- прочитайте слово. ' +
                'Представьте, к какой части тела оно подойдет или с какой ' +
                'частью тела оно ассоциируется
' +
                '- нажмите кнопку [Новая часть тела] и напишите туда часть ' +
                'тела, с которой соотносится это слово. ' +
                'Например: ноги, голова, руки, живот, всё тело, ' +
                'зубы, душа и т.д. Придумайте своё
' +
                '- нажмите [Ок]
' +
                '- каждое следующее новое слово относите к какой-то части ' +
                'тела, при этом вы можете выбирать созданные ранее группы и ' +
                'создавать новые, в том числе группу "никуда"
' +
                '')
textMainPreTest3 = str('' +
                '
Блок #2 пройден
' +
                '
Теперь прочитайте инструкцию ниже
' +
                '
Затем нажмите [Блок #3]
' +
                '')
textHintPreTest3 = str('' +
                '
Вверху окна вам будут показываться ' +
                'слова-прилагательные, которые вы выбрали как знакомые. ' +
                'Их нужно разделить на 2 группы по любому признаку, ' +
                'какой вы придумаете сами. Например: "сложное" и ' +
                '"простое", "движение" и "покой", "боль" и "радость", ' +
                '"могу управлять" и "не могу управлять" ' +
                'и т.д. Подумайте о том, как бы вы могли разделить все эти ' +
                'слова на 2 группы
' +
                '')
textHintTest3 = str('' +
                '
- прочитайте слово. Мысленно отнесите это слово к одной из ' +
                '2 придуманных групп
' +
                '- нажмите кнопку [Новая группа] и дайте ей название
' +
                '- каждое следующее слово относите к заданной 1 группе ' +
                'или ко 2 группе
' +
                '- вторую группу вы можете создать нажатием на кнопку ' +
                '[Новая группа]
' +
                '
! Помните, что создать можно будет только 2 группы
' +
                '')
textMainPreTest4 = str('' +
                '
Блок #3 пройден
' +
                '
Теперь прочитайте инструкцию ниже
' +
                '
Затем нажмите [Блок #4]
' +
                '')
textHintTest4 = str('' +
                '
- прочитайте слово
' +
                '- нажмите кнопку [Голод], если это слово напоминает вам ' +
                'состояние, когда вы были очень голодны
' +
                '- нажмите кнопку [Холод], если это слово напоминает вам ' +
                'состояние, когда вы замерзли
' +
                '- нажмите кнопку [Боль], если это слово напоминает вам ' +
                'состояние, когда у вас что-то болит или получена травма
' +
                '- нажмите кнопку [Никуда], если не можете никуда отнести это ' +
                'слово
' +
                '')
textMainEndTest4 = str('' +
                '
Блок #4 пройден
' +
                '
Теперь прочитайте инструкцию ниже, как отправить мне ' +
                'результат
' +
                '')

# UI for Main window
class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        # work variables
        self.currentTest = 0
        self.userName = ''

        self.initDataList = []  # init data
        self.workKeysList = []  # data keys from user file
        self.endKeyPos = 0      # end position member in workKeysList
        self.currentKeyPos = 0  # current position member in workKeysList
        self.resultDataList =[]

        self.listCBTest2 = [] # список категорий из теста №2
        self.listCBTest3 = [] # список категорий из теста №2

        self.initUI()
        self.show() # drawing MainWindow

# windows UI
    def initUI(self): # creation Gui depending on the flags
        self.createMainWindow()
        # buttons
        self.btnStart = QPushButton('', self)
        self.btnOn(self.btnStart, 'Войти')
        self.setBtn(self.btnStart, 170, 355, 'Arial', 10)
        self.btnStart.clicked.connect(self.startTests)

        self.btnStartTest1 = QPushButton('', self) #Блок #1
        self.setBtn(self.btnStartTest1, 170, 155, 'Arial', 10)
        self.btnStartTest1.clicked.connect(self.test1)
        self.btnOff(self.btnStartTest1)

        self.btnTest1Known = QPushButton('', self)
        self.setBtn(self.btnTest1Known, 350, 155, 'Arial', 10)
        self.btnTest1Known.clicked.connect(self.test1Known)
        self.btnOff(self.btnTest1Known)

        self.btnTest1Unknown = QPushButton('', self)
        self.setBtn(self.btnTest1Unknown, 530, 155, 'Arial', 10)
        self.btnTest1Unknown.clicked.connect(self.test1Unknown)
        self.btnOff(self.btnTest1Unknown)

        self.btnStartTest2 = QPushButton('', self)
        self.setBtn(self.btnStartTest2, 170, 205, 'Arial', 10)
        self.btnStartTest2.clicked.connect(self.test2)
        self.btnOff(self.btnStartTest2)

        self.btnTest2New = QPushButton('', self)
        self.setBtn(self.btnTest2New, 350, 205, 'Arial', 10)
        self.btnTest2New.clicked.connect(self.test2New)
        self.btnOff(self.btnTest2New)

        self.btnStartTest3 = QPushButton('', self)
        self.setBtn(self.btnStartTest3, 170, 255, 'Arial', 10)
        self.btnStartTest3.clicked.connect(self.test3)
        self.btnOff(self.btnStartTest3)

        self.btnTest3New = QPushButton('', self)
        self.setBtn(self.btnTest3New, 350, 255, 'Arial', 10)
        self.btnTest3New.clicked.connect(self.test3New)
        self.btnOff(self.btnTest3New)

        self.btnStartTest4 = QPushButton('', self)
        self.setBtn(self.btnStartTest4, 170, 305, 'Arial', 10)
        self.btnStartTest4.clicked.connect(self.test4)
        self.btnOff(self.btnStartTest4)

        self.btnTest4Cold = QPushButton('', self)
        self.setBtn(self.btnTest4Cold, 350, 305, 'Arial', 10)
        self.btnTest4Cold.clicked.connect(self.test4Cold)
        self.btnOff(self.btnTest4Cold)

        self.btnTest4Hunger = QPushButton('', self)
        self.setBtn(self.btnTest4Hunger, 530, 305, 'Arial', 10)
        self.btnTest4Hunger.clicked.connect(self.test4Hunger)
        self.btnOff(self.btnTest4Hunger)

        self.btnTest4Pain = QPushButton('', self)
        self.setBtn(self.btnTest4Pain, 350, 355, 'Arial', 10)
        self.btnTest4Pain.clicked.connect(self.test4Pain)
        self.btnOff(self.btnTest4Pain)

        self.btnTest4Nowhere = QPushButton('', self)
        self.setBtn(self.btnTest4Nowhere, 530, 355, 'Arial', 10)
        self.btnTest4Nowhere.clicked.connect(self.test4Nowhere)
        self.btnOff(self.btnTest4Nowhere)

        # labels
        self.mainLabel = QLabel('Нажмите кнопку [Войти]', self)
        self.setLabel(self.mainLabel, 540, 100, 80, 20,
                            'Arial', 16, 'wrap', 'center')

        self.hintLabel = QLabel('', self)
        self.setLabel(self.hintLabel, 640, 255, 30, 395,
                            'Arial', 12, 'wrap')

        # combo boxes
        self.comboBoxTest2 = QComboBox(self)
        self.setComboBox(self.comboBoxTest2,
                        self.listCBTest2, 530, 205)
        self.comboBoxTest2.addItems(self.listCBTest2)
        self.comboBoxTest2.activated[str].connect(self.onActivatedCBTest2)
        self.comboBoxOff(self.comboBoxTest2)

        self.comboBoxTest3 = QComboBox(self)
        self.setComboBox(self.comboBoxTest3,
                        self.listCBTest3, 530, 255)
        self.comboBoxTest3.activated[str].connect(self.onActivatedCBTest3)
        self.comboBoxOff(self.comboBoxTest3)

    def createMainWindow(self):
        self.setFixedSize(mainWindowWidth, mainWindowHeight)
        # self.move(mainWindowXPos, mainWindowYPos)
        self.center()
        self.setWindowTitle(mainWindowTitle)
        self.setWindowIcon(QIcon(faviconFile))

# modify properties of elements (buttons, labels, combo boxrs, etc.)
    def center(self):
        fg = self.frameGeometry()
        c = QDesktopWidget().availableGeometry().center()
        fg.moveCenter(c)
        self.move(fg.topLeft())

    def setBtn(self, btn, posX, posY,
                fontName, fontSize): # set button in window
        font = QFont(fontName, fontSize)
        btn.setFont(font)
        btn.resize(125, 25) # btn.sizeHint()
        btnWidth = btn.width()
        btnHeight = btn.height()
        btn.move(posX - btnWidth / 2, posY - btnHeight / 2)

    def setLabel(self, label, width, height,
                posX, posY, fontName, fontSize,
                flag1=None, flag2=None): # set label in window
        label.resize(width, height)
        label.move(posX, posY)
        font = QFont(fontName, fontSize)
        label.setFont(font)
        if flag1 == 'wrap':
            label.setWordWrap(True)
        if flag2 == 'center':
            label.setAlignment(Qt.AlignCenter)

    def setComboBox(self, combo, list, posX, posY):
        combo.addItems(list)
        combo.resize(125, 25)
        comboWidth = combo.width()
        comboHeight = combo.height()
        combo.move(posX - comboWidth / 2, posY - comboHeight / 2)

    def btnOn(self, btn, name):
        btn.setText(name)
        btn.setDisabled(False)

    def btnOff(self, btn):
        btn.clearFocus()
        btn.setText('')
        btn.setDisabled(True)

    def comboBoxOn(self, comboBox):
        comboBox.setDisabled(False)

    def comboBoxOff(self, comboBox):
        comboBox.clearFocus()
        comboBox.setDisabled(True)

    def setMainLabelText(self, label, text):
        label.setText(text)

# main programm logig
    def startTests(self):
        # check user (authorization), switch state
        oldUser = self.userExist()
        if oldUser == 'cancel':
            pass
        elif oldUser == True:
            self.initState('userExist')
        elif oldUser == False:
            self.initState('startTest1')

# work with state
    def initState(self, flag): # set current state
        self.btnOff(self.btnStart)
        if flag == 'userExist':
            finished = self.checkTestFinished()
            if finished == 'test4':
                self.mainLabel.setText(textMainEndTest4)
                self.hintLabel.setText(textHintHead +
                        str('' +
                        '
Ваши результаты сохранены в файле, который ' +
                        'называется ' + self.userName + '.txt
' +
                        '
Этот файл находится в той же папке, где и ' +
                        'эта программа
' +
                        '
Отправьте его мне на e-mail:  ' +
                        'zorina-margarita@mail.ru  ' +
                        'вместе с другими результатами тестов 1, 2, 3
' +
                        '
Теперь закройте это окно
' +
                        ''))
            elif finished == 'test3':
                self.mainLabel.setText('Вы закончили тест 3')
                self.preTest4()
            elif finished == 'test2':
                self.mainLabel.setText('Вы закончили тест 2')
                self.preTest3()
            elif finished == 'test1':
                self.preTest2()
        elif flag == 'startTest1':
            self.preTest1()

# user authentication
    def userExist(self):
        # get user name
        text, ok = QInputDialog.getText(self, 'Авторизация',
            'Введите ваш номер:')
        if ok:
            if text == '':
                return 'cancel' # if pressed <Cancel>
            else:
                self.userName = 'Тест4_' + text
                userFile = userFilePath + self.userName + userFileExtension
                if path.exists(userFile):
                    # return flag new user True/False
                    return True
                else:
                    return False
        else:
            return 'cancel' # if pressed <Cancel>

# tests logic
    def preTest1(self):
        self.btnStartTest1.setText('Блок #1')
        self.btnOn(self.btnStartTest1, 'Блок #1')
        self.btnTest1Known.setText('Знакомое')
        self.btnTest1Unknown.setText('Незнакомое')
        self.mainLabel.setText(textMainPreTest1)
        self.hintLabel.setText(textHintHead + textHintPreTest1 + textHintTest1)

    def test1(self):
        self.btnOff(self.btnStartTest1)
        self.btnOn(self.btnTest1Known, 'Знакомое')
        self.btnOn(self.btnTest1Unknown, 'Незнакомое')
        self.readInitData()
        for i in self.initDataList:
            self.workKeysList.append(int(i[0]))
        currentKeyPos = 0
        workWord = self.extractWordByKey()
        self.mainLabel.setText(workWord)
        self.hintLabel.setText(textHintHead + textHintTest1)

    def test1Known(self):
        self.resultDataList.append(self.workKeysList[self.currentKeyPos])
        self.currentKeyPos += 1
        if self.currentKeyPos < len(self.workKeysList):
            workWord = self.extractWordByKey()
            self.mainLabel.setText(workWord)
        else:
            self.endTest1()

    def test1Unknown(self):
        self.currentKeyPos += 1
        if self.currentKeyPos < len(self.workKeysList):
            workWord = self.extractWordByKey()
            self.mainLabel.setText(workWord)
        else:
            self.endTest1()

    def endTest1(self):
        if self.resultDataList == []:
            self.crashTest1()
        else:
            self.mainLabel.setText('Test 1 finished')
            data = self.parseData(self.resultDataList, 'saveTest1')
            self.saveFile(userFilePath, self.userName + userFileExtension,
                        data, 'test1')
            self.btnOff(self.btnTest1Known)
            self.btnOff(self.btnTest1Unknown)
            self.preTest2()

    def crashTest1(self):
        self.btnOff(self.btnTest1Known)
        self.btnOff(self.btnTest1Unknown)
        self.mainLabel.setText('Вы не указали ни одного знакомого слова,\n' +
                                'тест завершен, спасибо!')
        self.hintLabel.setText('')
        # add save in file?

    def preTest2(self):
        self.btnOn(self.btnStartTest2, 'Блок #2')
        self.btnTest2New.setText('Новая часть тела')
        self.mainLabel.setText(textMainPreTest2)
        self.hintLabel.setText(textHintHead + textHintPreTest2 + textHintTest2)

    def test2(self):
        self.btnOff(self.btnStartTest2)
        self.btnOn(self.btnTest2New, 'Новая часть тела')
        self.prepareforTests2_4()
        self.hintLabel.setText(textHintHead + textHintTest2)

    def test2New(self):
        text, ok = QInputDialog.getText(self, 'Новая категория',
                            'Введите новую категорию:')
        if ok:
            self.addItemComboBox(text, 'test2')
            tempSubList = [self.workKeysList[self.currentKeyPos], text]
            self.resultDataList.append(tempSubList)
            self.currentKeyPos += 1
            if self.currentKeyPos < len(self.workKeysList):
                workWord = self.extractWordByKey()
                self.mainLabel.setText(workWord)
            else:
                self.endTest2()

    def onActivatedCBTest2(self, text):
        self.addItemComboBox(text, 'test2')
        tempSubList = [self.workKeysList[self.currentKeyPos], text]
        self.resultDataList.append(tempSubList)
        self.currentKeyPos += 1
        if self.currentKeyPos < len(self.workKeysList):
            workWord = self.extractWordByKey()
            self.mainLabel.setText(workWord)
        else:
            self.endTest2()

    def endTest2(self):
        self.mainLabel.setText('Finished test 2')
        self.btnOff(self.btnTest2New)
        self.comboBoxOff(self.comboBoxTest2)
        self.comboBoxTest2.clear()
        data = self.parseData(self.resultDataList, 'saveTest2')
        self.saveFile(userFilePath, self.userName + userFileExtension,
                    data, 'test2')
        self.preTest3()

    def preTest3(self):
        self.btnOn(self.btnStartTest3, 'Блок #3')
        self.mainLabel.setText(textMainPreTest3)
        self.hintLabel.setText(textHintHead + textHintPreTest3 + textHintTest3)

    def test3(self):
        self.btnOff(self.btnStartTest3)
        self.btnOn(self.btnTest3New, 'Новая группа')
        self.prepareforTests2_4()
        self.hintLabel.setText(textHintHead + textHintTest3)

    def test3New(self):
        text, ok = QInputDialog.getText(self, 'Новая категория',
                            'Введите новую категорию:')
        if ok:
            self.addItemComboBox(text, 'test3')
            tempSubList = [self.workKeysList[self.currentKeyPos], text]
            self.resultDataList.append(tempSubList)
            self.currentKeyPos += 1
            if self.currentKeyPos < len(self.workKeysList):
                workWord = self.extractWordByKey()
                self.mainLabel.setText(workWord)
            else:
                self.endTest3()

    def onActivatedCBTest3(self, text):
        self.addItemComboBox(text, 'test3')
        tempSubList = [self.workKeysList[self.currentKeyPos], text]
        self.resultDataList.append(tempSubList)
        self.currentKeyPos += 1
        if self.currentKeyPos < len(self.workKeysList):
            workWord = self.extractWordByKey()
            self.mainLabel.setText(workWord)
        else:
            self.endTest3()

    def endTest3(self):
        self.mainLabel.setText('Finished test 3')
        self.btnOff(self.btnTest3New)
        self.comboBoxOff(self.comboBoxTest3)
        self.comboBoxTest3.clear()
        data = self.parseData(self.resultDataList, 'saveTest3')
        self.saveFile(userFilePath, self.userName + userFileExtension,
                    data, 'test3')
        self.preTest4()

    def preTest4(self):
        self.btnOn(self.btnStartTest4, 'Блок #4')
        self.btnTest4Cold.setText('Холод')
        self.btnTest4Hunger.setText('Голод')
        self.btnTest4Pain.setText('Боль')
        self.btnTest4Nowhere.setText('Никуда')
        self.mainLabel.setText(textMainPreTest4)
        self.hintLabel.setText(textHintHead + textHintPreTest2 + textHintTest4)

    def test4(self):
        self.btnOff(self.btnStartTest4)
        self.btnOn(self.btnTest4Cold, 'Холод')
        self.btnOn(self.btnTest4Hunger, 'Голод')
        self.btnOn(self.btnTest4Pain, 'Боль')
        self.btnOn(self.btnTest4Nowhere, 'Никуда')
        self.prepareforTests2_4()
        self.hintLabel.setText(textHintHead + textHintTest4)

    def test4Cold(self):
        self.operationTest4('Холод')

    def test4Hunger(self):
        self.operationTest4('Голод')

    def test4Pain(self):
        self.operationTest4('Боль')

    def test4Nowhere(self):
        self.operationTest4('Никуда')

    def operationTest4(self, flag):
        tempSubList = [self.workKeysList[self.currentKeyPos], flag]
        self.resultDataList.append(tempSubList)
        self.currentKeyPos += 1
        if self.currentKeyPos < len(self.workKeysList):
            workWord = self.extractWordByKey()
            self.mainLabel.setText(workWord)
        else:
            self.endTest4()

    def endTest4(self):
        self.mainLabel.setText('Вы закончили все тесты')
        self.btnOff(self.btnTest4Cold)
        self.btnOff(self.btnTest4Hunger)
        self.btnOff(self.btnTest4Pain)
        self.btnOff(self.btnTest4Nowhere)
        data = self.parseData(self.resultDataList, 'saveTest4')
        self.saveFile(userFilePath, self.userName + userFileExtension,
                    data, 'test4')
        self.mainLabel.setText(textMainEndTest4)
        self.hintLabel.setText(textHintHead +
                        str('' +
                        '
Ваши результаты сохранены в файле, который ' +
                        'называется ' + self.userName + '.txt
' +
                        '
Этот файл находится в той же папке, где и ' +
                        'эта программа
' +
                        '
Отправьте его мне на e-mail:  ' +
                        'zorina-margarita@mail.ru  ' +
                        'вместе с другими результатами тестов 1, 2, 3
' +
                        '
Теперь закройте это окно
' +
                        ''))

    def prepareforTests2_4(self):
        self.currentKeyPos = 0
        self.workKeysList = []
        self.resultDataList = []
        self.readInitData()
        self.readTest1Result()
        workWord = self.extractWordByKey()
        self.mainLabel.setText(workWord)

    def existInList(self, list, text):
        for i in list:
            if i == text:
                return True
                break
        return False

    def addItemComboBox(self, text, flag):
        if flag == 'test2':
            if not self.existInList(self.listCBTest2, text):
                self.listCBTest2.append(text)
                self.updateComboBox('test2')
        elif flag == 'test3':
            if not self.existInList(self.listCBTest3, text):
                self.listCBTest3.append(text)
                self.updateComboBox('test3')

    def updateComboBox(self, flag):
        if flag == 'test2':
            self.comboBoxTest2.clear()
            self.comboBoxTest2.addItems(self.listCBTest2)
            if not self.comboBoxTest2.isEnabled():
                self.comboBoxOn(self.comboBoxTest2)
        elif flag == 'test3':
            self.comboBoxTest3.clear()
            self.comboBoxTest3.addItems(self.listCBTest3)
            if not self.comboBoxTest3.isEnabled():
                self.comboBoxOn(self.comboBoxTest3)
            if len(self.listCBTest3) == 2:
                self.btnOff(self.btnTest3New)

# work with file
    def openFile(self, path, fileName, flag='r'):
        # open file
        file = open(path + fileName, flag, encoding='utf-8')
        # read data
        data = file.read()
        # close file
        file.close()
        # return raw data
        return data

    def saveFile(self, path, fileName, data, flag):
        file = open(path + fileName, 'a', encoding='utf-8')
        if flag == 'test1':
            file.write(data + '\ntest1')
        elif flag == 'test2':
            file.write('\n' + data + '\ntest2')
        elif flag == 'test3':
            file.write('\n' + data + '\ntest3')
        elif flag == 'test4':
            file.write('\n' + data + '\ntest4')
        file.close()

    def parseData(self, data, flag): # parsing
        # parsing by flag
        if flag == 'progress':
            pass
        # return work variables
        elif flag == 'init':
            # print(data)
            result = []
            tempList = (data.split('\n'))
            for i in tempList:
                result.append(i.split('. '))
            self.endKeyPos = len(result) - 1
        elif flag == 'test1Result':
            result = []
            strList = data.split(',')
            for i in strList:
                result.append(int(i))
            self.endKeyPos = len(result) - 1
        elif flag == 'saveTest1':
            result = ''
            for i in data[:-1]:
                result += str(i) + ','
            result += str(data[-1])
        elif flag == 'saveTest2':
            result = ''
            for i in data[:-1]:
                result += str(i[0]) + ',' + i[1] + ';'
            result += str(data[-1][0]) + ',' + data[-1][1]
        elif flag == 'saveTest3':
            result = ''
            for i in data[:-1]:
                result += str(i[0]) + ',' + i[1] + ';'
            result += str(data[-1][0]) + ',' + data[-1][1]
        elif flag == 'saveTest4':
            result = ''
            for i in data[:-1]:
                result += str(i[0]) + ',' + i[1] + ';'
            result += str(data[-1][0]) + ',' + data[-1][1]

        return result

    def readInitData(self):
        file = self.openFile(initFilePath, initFileName)
        self.initDataList = self.parseData(file, 'init')

    def readTest1Result(self):
        file = open(userFilePath + self.userName + userFileExtension, 'r',
                    encoding='utf-8')
        for i, line in enumerate(file):
            if i == 0:
                self.workKeysList = self.parseData(line[0:-1], 'test1Result')
            else:
                break
        file.close()

    def checkTestFinished(self):
        file = open(userFilePath + self.userName + userFileExtension, 'r',
                    encoding='utf-8')
        lastTest = file.readlines()[-1]
        file.close()
        return lastTest

    def extractWordByKey(self):
        tempKey = self.workKeysList[self.currentKeyPos]
        workWord = self.initDataList[tempKey - 1][1]
        return workWord

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    sys.exit(app.exec_())


from tkinter import *

txtWin = Tk()
scrollbar = Scrollbar(txtWin)
scrollbar.pack(side = RIGHT, fill = Y)

area = Text(txtWin, yscrollcommand = scrollbar.set, background = 'black', foreground = 'green', font = ('None, 11'), insertbackground = 'yellow', insertwidth = 5, selectbackground = 'red' )
area.pack(expand=True, fill='both')

scrollbar.config(command = area.yview)
txtWin.mainloop()