import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from sklearn.metrics import confusion_matrix
import time
from datetime import timedelta
import os

# Use PrettyTensor to simplify Neural Network construction.
import prettytensor as pt

from tutorials.hvassTutorials.cifar import cifar10
from tutorials.hvassTutorials.cifar import helperFunction

# Successfully downloaded cifar.gz 170498071 bytes.
# / 1024(转化为 K) / 1024(转化为M) = 162.599M
# cifar10.maybe_download_and_extract()

class_names = cifar10.load_class_names()
print(class_names)
# ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

images_train, cls_train, labels_train = cifar10.load_training_data()
images_test, cls_test, labels_test = cifar10.load_test_data()
print("Size of:")
print("- Training-set:\t\t{}".format(len(images_train)))
print("- Test-set:\t\t{}".format(len(images_test)))

from tutorials.hvassTutorials.cifar.cifar10 import img_size, num_channels, num_classes

img_size_cropped = 24

# Get the first images from the test-set.
images = images_test[0:9]

# Get the true classes for those images.
cls_true = cls_test[0:9]
print(labels_test[0:9])
# 上面的像素化图像是神经网络将作为输入。如果我们平滑像素smooth=True，图像可能对于人眼来说更容易识别。
helperFunction.plot_images(images=images, cls_true=cls_true, smooth=False)

x = tf.placeholder(tf.float32, shape=[None, img_size, img_size, num_channels], name='x')

y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true')

y_true_cls = tf.argmax(y_true, dimension=1)

distorted_images = helperFunction.pre_process(images=x, training=True)

def main_network(images, training):
    # Wrap the input images as a Pretty Tensor object.
    x_pretty = pt.wrap(images)

    # Pretty Tensor uses special numbers to distinguish between
    # the training and testing phases.
    if training:
        phase = pt.Phase.train
    else:
        phase = pt.Phase.infer

    # Create the convolutional neural network using Pretty Tensor.
    # It is very similar to the previous tutorials, except
    # the use of so-called batch-normalization in the first layer.
    with pt.defaults_scope(activation_fn=tf.nn.relu, phase=phase):
        y_pred, loss = x_pretty. \
            conv2d(kernel=5, depth=64, name='layer_conv1', batch_normalize=True). \
            max_pool(kernel=2, stride=2). \
            conv2d(kernel=5, depth=64, name='layer_conv2'). \
            max_pool(kernel=2, stride=2). \
            flatten(). \
            fully_connected(size=256, name='layer_fc1'). \
            fully_connected(size=128, name='layer_fc2'). \
            softmax_classifier(num_classes=num_classes, labels=y_true)

    return y_pred, loss

def create_network(training):
    # Wrap the neural network in the scope named 'network'.
    # Create new variables during training, and re-use during testing.
    with tf.variable_scope('network', reuse=not training):
        # Just rename the input placeholder variable for convenience.
        images = x

        # Create TensorFlow graph for pre-processing.
        images = helperFunction.pre_process(images=images, training=training)

        # Create TensorFlow graph for the main processing.
        y_pred, loss = main_network(images=images, training=training)

    return y_pred, loss

global_step = tf.Variable(initial_value=0,
                          name='global_step', trainable=False)

_, loss = create_network(training=True)

optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(loss, global_step=global_step)

y_pred, _ = create_network(training=False)

y_pred_cls = tf.argmax(y_pred, axis=1)

correct_prediction = tf.equal(y_pred_cls, y_true_cls)

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

saver = tf.train.Saver()

def get_weights_variable(layer_name):
    # Retrieve an existing variable named 'weights' in the scope
    # with the given layer_name.
    # This is awkward because the TensorFlow function was
    # really intended for another purpose.

    with tf.variable_scope("network/" + layer_name, reuse=True):
        variable = tf.get_variable('weights')

    return variable

weights_conv1 = get_weights_variable(layer_name='layer_conv1')
weights_conv2 = get_weights_variable(layer_name='layer_conv2')

def get_layer_output(layer_name):
    # The name of the last operation of the convolutional layer.
    # This assumes you are using Relu as the activation-function.
    tensor_name = "network/" + layer_name + "/Relu:0"

    # Get the tensor with this name.
    tensor = tf.get_default_graph().get_tensor_by_name(tensor_name)

    return tensor

output_conv1 = get_layer_output(layer_name='layer_conv1')
output_conv2 = get_layer_output(layer_name='layer_conv2')

session = tf.Session()

save_dir = 'checkpoints/'

if not os.path.exists(save_dir):
    os.makedirs(save_dir)

save_path = os.path.join(save_dir, 'cifar10_cnn')

try:
    print("Trying to restore last checkpoint ...")

    # Use TensorFlow to find the latest checkpoint - if any.
    last_chk_path = tf.train.latest_checkpoint(checkpoint_dir=save_dir)

    # Try and load the data in the checkpoint.
    saver.restore(session, save_path=last_chk_path)

    # If we get to this point, the checkpoint was successfully loaded.
    print("Restored checkpoint from:", last_chk_path)
except:
    # If the above failed for some reason, simply
    # initialize all the variables for the TensorFlow graph.
    print("Failed to restore checkpoint. Initializing variables instead.")
    session.run(tf.global_variables_initializer())

train_batch_size = 64

def random_batch():
    # Number of images in the training-set.
    num_images = len(images_train)

    # Create a random index.
    idx = np.random.choice(num_images,
                           size=train_batch_size,
                           replace=False)

    # Use the random index to select random images and labels.
    x_batch = images_train[idx, :, :, :]
    y_batch = labels_train[idx, :]

    return x_batch, y_batch

def optimize(num_iterations):
    # Start-time used for printing time-usage below.
    start_time = time.time()

    for i in range(num_iterations):
        # Get a batch of training examples.
        # x_batch now holds a batch of images and
        # y_true_batch are the true labels for those images.
        x_batch, y_true_batch = random_batch()

        # Put the batch into a dict with the proper names
        # for placeholder variables in the TensorFlow graph.
        feed_dict_train = {x: x_batch,
                           y_true: y_true_batch}

        # Run the optimizer using this batch of training data.
        # TensorFlow assigns the variables in feed_dict_train
        # to the placeholder variables and then runs the optimizer.
        # We also want to retrieve the global_step counter.
        i_global, _ = session.run([global_step, optimizer],
                                  feed_dict=feed_dict_train)

        # Print status to screen every 100 iterations (and last).
        if (i_global % 100 == 0) or (i == num_iterations - 1):
            # Calculate the accuracy on the training-batch.
            batch_acc = session.run(accuracy,
                                    feed_dict=feed_dict_train)

            # Print status.
            msg = "Global Step: {0:>6}, Training Batch Accuracy: {1:>6.1%}"
            print(msg.format(i_global, batch_acc))

        # Save a checkpoint to disk every 1000 iterations (and last).
        if (i_global % 1000 == 0) or (i == num_iterations - 1):
            # Save all variables of the TensorFlow graph to a
            # checkpoint. Append the global_step counter
            # to the filename so we save the last several checkpoints.
            saver.save(session,
                       save_path=save_path,
                       global_step=global_step)

            print("Saved checkpoint.")

    # Ending time.
    end_time = time.time()

    # Difference between start and end-times.
    time_dif = end_time - start_time

    # Print the time-usage.
    print("Time usage: " + str(timedelta(seconds=int(round(time_dif)))))

def plot_example_errors(cls_pred, correct):
    # This function is called from print_test_accuracy() below.

    # cls_pred is an array of the predicted class-number for
    # all images in the test-set.

    # correct is a boolean array whether the predicted class
    # is equal to the true class for each image in the test-set.

    # Negate the boolean array.
    incorrect = (correct == False)

    # Get the images from the test-set that have been
    # incorrectly classified.
    images = images_test[incorrect]

    # Get the predicted classes for those images.
    cls_pred = cls_pred[incorrect]

    # Get the true classes for those images.
    cls_true = cls_test[incorrect]

    # Plot the first 9 images.
    helperFunction.plot_images(images=images[0:9],
                cls_true=cls_true[0:9],
                cls_pred=cls_pred[0:9])

def plot_confusion_matrix(cls_pred):
    # This is called from print_test_accuracy() below.

    # cls_pred is an array of the predicted class-number for
    # all images in the test-set.

    # Get the confusion matrix using sklearn.
    cm = confusion_matrix(y_true=cls_test,  # True class for test-set.
                          y_pred=cls_pred)  # Predicted class.

    # Print the confusion matrix as text.
    for i in range(num_classes):
        # Append the class-name to each line.
        class_name = "({}) {}".format(i, class_names[i])
        print(cm[i, :], class_name)

    # Print the class-numbers for easy reference.
    class_numbers = [" ({0})".format(i) for i in range(num_classes)]
    print("".join(class_numbers))


from pymisc.decorators import logprint, memoized

@logprint
def test_logprint():

    @logprint
    def test(q):
        x = [a + 10 for a in q]
        print('123')
        print('321')
        return x

    r = test([1,23])
    print(r)

count = 0
def test_memoized():
    @memoized
    def fibonacci(x):
        global count
        count += 1
        return fibonacci(x - 1) + fibonacci(x - 2) if x > 2 else 1

    assert(fibonacci(10) == 55)
    assert(count == 10)

if __name__ == "__main__":
    test_logprint()
    test_memoized()


import requests
import bot
import pandas as pd
import time
from bs4 import BeautifulSoup
import re

def get_request(url):
	response=""
	try:
		response = requests.get("https://www.binance.com"+url).text
	except Exception as e:
		print('Error Details : {}'.format(e))
	return response

def get_soup(data):
	soup =""
	try:
		soup = BeautifulSoup(data,'html.parser')
	except Exception as e:
		print('Error details : {}'.format(e))
	return soup

def send_userMessage(coin,date,title):
	message = "Title : {} , Coin Pair : {} , Date : {}".format(title,coin,date)
	last_msg= bot.retrieve_LastMessage()
	Userid=last_msg['message']['from']['id']
	bot.bot_send_message(message, Userid)

def find_date_time(text,remove=None):
	if text is not None:
		strDate = re.search(r'(\d{4}(\/\d{2}){2}.+UTC\))', text)
		coin = re.findall (r'([A-Z]{2,6}\/[a-zA-Z]{2,6})', text)
	return strDate.group(),coin

while True:
	listingData = get_request("/en/support/announcement/c-48")
	soup = get_soup(listingData)
	allLists = soup.find_all('a',class_='css-1neg3js')
	link_df = pd.read_csv("HrefLinks.csv")
	if allLists is not None:
		for listdata in allLists:
			data=None
			title=listdata.text
			link= str(listdata['href'])
			if link not in list(link_df['Links']):
				add_data = pd.DataFrame({'Links' : [link]})
				add_data.to_csv('HrefLinks.csv' , mode='a',header=False ,index = False)
				listDetails = get_request(link)
				if "binance will list" in listdata.text.lower():
					soup = get_soup(listDetails)
					data = str(soup.find('article',class_='css-1ii68zy'))
				elif "binance lists" in listdata.text.lower():
					soup = get_soup(listDetails)
					data = soup.find_all("div",class_='css-3fpgoh')[1].text
				elif "Notice of Removal" in listdata.text:
					soup = get_soup(listDetails)
					data = str(soup.find('article',class_='css-1ii68zy'))
					removeBool = True
				else:
					pass
				if data is not None:
					date,coin = find_date_time(data)
					send_userMessage(" ".join(coin),date,title)

	time.sleep(30)


from .base import BaseActionTestCase

class GeneralActionWSGITester(BaseActionTestCase):
    """
    Tests the action WSGI application in general.
    """

    def test_request_wrong_method(self) -> None:
        response = self.client.get("/")
        self.assert_status_code(response, 405)

    def test_request_wrong_media_type(self) -> None:
        response = self.client.post("/")
        self.assert_status_code(response, 400)
        self.assertIn("Wrong media type.", response.json["message"])

    def test_request_missing_body(self) -> None:
        response = self.client.post("/", content_type="application/json")
        self.assert_status_code(response, 400)
        self.assertIn("Failed to decode JSON object", response.json["message"])

    def test_request_fuzzy_body(self) -> None:
        response = self.client.post(
            "/",
            json={"fuzzy_key_Eeng7pha3a": "fuzzy_value_eez3Ko6quu"},
        )
        self.assert_status_code(response, 400)
        self.assertIn("data must be array", response.json["message"])

    def test_request_fuzzy_body_2(self) -> None:
        response = self.client.post(
            "/",
            json=[{"fuzzy_key_Voh8in7aec": "fuzzy_value_phae3iew4W"}],
        )
        self.assert_status_code(response, 400)
        self.assertIn(
            "data[0] must contain ['action', 'data'] properties",
            response.json["message"],
        )

    def test_request_no_existing_action(self) -> None:
        response = self.request("fuzzy_action_hamzaeNg4a", {})
        self.assert_status_code(response, 400)
        self.assertIn(
            "Action fuzzy_action_hamzaeNg4a does not exist.",
            response.json["message"],
        )

    def test_health_route(self) -> None:
        response = self.client.get("/health")
        self.assert_status_code(response, 200)
        self.assertIn("healthinfo", response.json)
        actions = response.json["healthinfo"]["actions"]
        some_example_actions = (
            "topic.create",
            "motion.delete",
            "user.update_temporary",
        )
        for action in some_example_actions:
            self.assertIn(action, actions.keys())


# !pip install -q tensorflow-gpu==2.0.0
import tensorflow as tf
import numpy as np
import os
import time
import re

# 구글 코랩에서 실행할 경우 언코멘트 할 것
'''
from google.colab import drive
drive.mount('/content/drive')
text = open('/content/drive/My Drive/Colab Notebooks/text/genesis.txt', 'r').read()
'''

# 한글을 자모 단위로 쪼개기 or 붙이기
# https://rachelsdotcom.tistory.com/79
# -*- coding: utf-8 -*-
# 초성 리스트. 0 ~ 18
CHOSUNG_LIST = ['ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']

# 중성 리스트. 0 ~ 20
JUNGSUNG_LIST = ['ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ', 'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ',
                 'ㅣ']

# 종성 리스트. 0 ~ 27 + 1(1개 없음)
JONGSUNG_LIST = [' ', 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ',
                 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']

def seperator(string):
    # 유니코드 한글 시작 : 44032, 끝 : 55199
    BASE_CODE, CHOSUNG, JUNGSUNG = 44032, 588, 28

    sp_list = list(string)  # string make list
    result = []
    for keyword in sp_list:
        # 한글 여부 check 후 분리
        if (re.match('.*[ㄱ-ㅎㅏ-ㅣ가-힣]+.*', keyword) is not None):

            # 초성
            char_code = ord(keyword) - BASE_CODE
            char1 = int(char_code / CHOSUNG)
            result.append(CHOSUNG_LIST[char1])

            # 중성
            char2 = int((char_code - (CHOSUNG * char1)) / JUNGSUNG)
            result.append(JUNGSUNG_LIST[char2])

            # 종성
            char3 = int((char_code - (CHOSUNG * char1) - (JUNGSUNG * char2)))
            if char3 == 0:
                pass
            else:
                result.append(JONGSUNG_LIST[char3])

        elif (keyword == ' ') or (keyword == '\n') or (keyword == ".") or (keyword == ","):
            result.append(keyword)

        else:
            pass

    output = "".join(result)
    return output  # 자소 분리 결과 출력

# https://gigglehd.com/zbxe/14052329
def hangeulJoin(inputlist):
    result = ""
    cho, jung, jong = 0, 0, 0
    inputlist.insert(0, "")
    while len(inputlist) > 1:
        if inputlist[-1] in JONGSUNG_LIST:
            if inputlist[-2] in JUNGSUNG_LIST:
                jong = JONGSUNG_LIST.index(inputlist.pop())
            else:
                result += inputlist.pop()
        elif inputlist[-1] in JUNGSUNG_LIST:
            if inputlist[-2] in CHOSUNG_LIST:
                jung = JUNGSUNG_LIST.index(inputlist.pop())
                cho = CHOSUNG_LIST.index(inputlist.pop())
                result += chr(0xAC00 + ((cho * 21) + jung) * 28 + jong)
                cho, jung, jong = 0, 0, 0
            else:
                result += inputlist.pop()
        else:
            result += inputlist.pop()
    else:
        return result[::-1]

def aggregator(inputtext):
    """입력된 문장에 있는 모든 풀어쓰기된 한글을 모아쓰기로 바꾼다."""
    t1 = []
    for i in inputtext:
        t1.append(i)
    return hangeulJoin(t1)

string = seperator("안녕하세여!")
print(string)
print(aggregator(string))


"""Functions that alter the matplotlib rc dictionary on the fly."""
import contextlib
from numpy import isreal
import matplotlib as mpl
from seaborn import utils

def set(context="notebook", style="darkgrid", palette="deep", font="Arial",
        gridweight=None):
    """Set new RC params in one step."""
    set_axes_style(style, context, font=font, gridweight=gridweight)
    set_color_palette(palette)

def reset_defaults():
    """Restore all RC params to default settings."""
    mpl.rcParams.update(mpl.rcParamsDefault)

def reset_orig():
    """Restore all RC params to original settings (respects custom rc)."""
    mpl.rcParams.update(mpl.rcParamsOrig)

def set_axes_style(style, context, font="Arial", gridweight=None):
    """Set the axis style.

    Parameters
    ----------
    style : darkgrid | whitegrid | nogrid | ticks
        Style of axis background.
    context: notebook | talk | paper | poster
        Intended context for resulting figures.
    font : matplotlib font spec
        Font to use for text in the figures.
    gridweight : extra heavy | heavy | medium | light
        Width of the grid lines. None

    """

    # Validate the arguments
    if not {"darkgrid", "whitegrid", "nogrid", "ticks"} & {style}:
        raise ValueError("Style %s not recognized" % style)

    if not {"notebook", "talk", "paper", "poster"} & {context}:
        raise ValueError("Context %s is not recognized" % context)

    if not isreal(gridweight) and \
       (not {"None", "extra heavy", "heavy",
             "medium", "light"} & {gridweight}):
        raise ValueError("Gridweight %s is not recognized" % gridweight)

    # Determine the axis parameters
    # -----------------------------

    # Turn ticks off; they get turned back on in 'ticks' style
    mpl.rc("xtick.major", size=0)
    mpl.rc("ytick.major", size=0)
    mpl.rc("xtick.minor", size=0)
    mpl.rc("ytick.minor", size=0)

    # select grid line width:
    gridweights = {
        'extra heavy': 1.5,
        'heavy': 1.1,
        'medium': 0.8,
        'light': 0.5,
    }
    if gridweight is None:
        if context == "paper":
            glw = gridweights["medium"]
        else:
            glw = gridweights['extra heavy']
    elif isreal(gridweight):
        glw = gridweight
    else:
        glw = gridweights[gridweight]

    if style == "darkgrid":
        lw = .8 if context == "paper" else 1.5
        ax_params = {"axes.facecolor": "#EAEAF2",
                     "axes.edgecolor": "white",
                     "axes.linewidth": 0,
                     "axes.grid": True,
                     "axes.axisbelow": True,
                     "grid.color": "w",
                     "grid.linestyle": "-",
                     "grid.linewidth": glw}

    elif style == "whitegrid":
        lw = 1.0 if context == "paper" else 1.7
        ax_params = {"axes.facecolor": "white",
                     "axes.edgecolor": "#CCCCCC",
                     "axes.linewidth": lw,
                     "axes.grid": True,
                     "axes.axisbelow": True,
                     "grid.color": "#DDDDDD",
                     "grid.linestyle": "-",
                     "grid.linewidth": glw}

    elif style == "nogrid":
        ax_params = {"axes.grid": False,
                     "axes.facecolor": "white",
                     "axes.edgecolor": "black",
                     "axes.linewidth": 1}

    elif style == "ticks":
        ticksize = 3. if context == "paper" else 6.
        tickwidth = .5 if context == "paper" else 1
        ax_params = {"axes.grid": False,
                     "axes.facecolor": "white",
                     "axes.edgecolor": "black",
                     "axes.linewidth": 1,
                     "xtick.direction": "out",
                     "ytick.direction": "out",
                     "xtick.major.width": tickwidth,
                     "ytick.major.width": tickwidth,
                     "xtick.minor.width": tickwidth,
                     "xtick.minor.width": tickwidth,
                     "xtick.major.size": ticksize,
                     "xtick.minor.size": ticksize / 2,
                     "ytick.major.size": ticksize,
                     "ytick.minor.size": ticksize / 2}

    mpl.rcParams.update(ax_params)

    # Determine the font sizes
    if context == "talk":
        font_params = {"axes.labelsize": 16,
                       "axes.titlesize": 19,
                       "xtick.labelsize": 14,
                       "ytick.labelsize": 14,
                       "legend.fontsize": 13,
                       }

    elif context == "notebook":
        font_params = {"axes.labelsize": 11,
                       "axes.titlesize": 12,
                       "xtick.labelsize": 10,
                       "ytick.labelsize": 10,
                       "legend.fontsize": 10,
                       }

    elif context == "poster":
        font_params = {"axes.labelsize": 18,
                       "axes.titlesize": 22,
                       "xtick.labelsize": 16,
                       "ytick.labelsize": 16,
                       "legend.fontsize": 16,
                       }

    elif context == "paper":
        font_params = {"axes.labelsize": 8,
                       "axes.titlesize": 12,
                       "xtick.labelsize": 8,
                       "ytick.labelsize": 8,
                       "legend.fontsize": 8,
                       }

    mpl.rcParams.update(font_params)

    # Set other parameters
    mpl.rcParams.update({
        "lines.linewidth": 1.1 if context == "paper" else 1.4,
        "patch.linewidth": .1 if context == "paper" else .3,
        "xtick.major.pad": 3.5 if context == "paper" else 7,
        "ytick.major.pad": 3.5 if context == "paper" else 7,
        })

    # Set the constant defaults
    mpl.rc("font", family=font)
    mpl.rc("legend", frameon=False, numpoints=1)
    mpl.rc("lines", markeredgewidth=0, solid_capstyle="round")
    mpl.rc("figure", figsize=(8, 5.5))
    mpl.rc("image", cmap="cubehelix")

def set_color_palette(name, n_colors=6, desat=None):
    """Set the matplotlib color cycle in one of a variety of ways.

    Parameters
    ----------
    name : hls | husl | matplotlib colormap | seaborn color palette
        palette name
    n_colors : int
        only relevant for hls or matplotlib palettes
    desat : float
        desaturation factor for each color

    """
    colors = utils.color_palette(name, n_colors, desat)
    mpl.rcParams["axes.color_cycle"] = colors
    mpl.rcParams["patch.facecolor"] = colors[0]

@contextlib.contextmanager
def palette_context(palette, n_colors=6, desat=None):
    """Context manager for temporarily setting the color palette."""
    orig_palette = mpl.rcParams["axes.color_cycle"]
    set_color_palette(palette, n_colors, desat)
    yield
    set_color_palette(orig_palette, len(orig_palette))


import pygame
import random

class Square:

    def __init__(self, x, y, terr_type, walkable):
        self.x = x
        self.y = y
        self.terr_type = terr_type
        self.walkable = walkable
        self.px = 40  # same for mobs
        self.choose_image()
        self.img = pygame.transform.scale2x(self.img)
        self.mob = None

    def choose_image(self):
        images = {"grass": 3}
        if self.terr_type in images.keys():
            images_amount = images[self.terr_type]
            chosen_img_number = random.randint(1, images_amount)
            self.img = pygame.image.load("images/" + self.terr_type + str(chosen_img_number) + ".png")
        else:
            self.img = pygame.image.load("images/" + self.terr_type + ".png")

    def someone_stands(self, mob):
        self.walkable = False
        self.mob = mob

    def someone_leaves(self):
        self.walkable = True
        self.mob = None

    def display(self, window):
        window.blit(self.img, (self.x * self.px, self.y * self.px))

class Map:

    def __init__(self, width, height):
        self.map = []
        self.height = height
        self.width = width

    def add_square(self, square):
        self.map.append(square)

    def find_square(self, x, y):
        for square in self.map:
            if square.x == x and square.y == y:
                return square
        return Square(-1, -1, "grass", False)  # returns it, when it's a barrier square

    def load_from_file(self, name):
        temp_dict = {"G": "grass", "W": "water"}
        with open("maps/" + name + ".mmap", "r") as file:
            for line in file:
                line = line.replace("\n", "")
                words = line.split(" ")
                x = int(words[0])
                y = int(words[1])
                terr_type = temp_dict[words[2]]
                walkable = words[3] == "W"
                self.add_square(Square(x, y, terr_type, walkable))

    def display(self, window):
        for square in self.map:
            square.display(window)


####################################
#file_name: fresh_and_clean.py     #
#author: Riccardo La Grassa        #
#data created: 16/11/2016          #
#data last modified: 2/12/2017     #
#Python interpreter: 3.5.2         #
#mail: riccardo2468@gmail.com      #
####################################

import re

def load_stop_word(path):
    #grammar file. i will erase all the useless words
    grammarlist=[]
    try:
        with open(path, 'r') as f:
            grammarlist.append(f.read().splitlines())
        f.close()
        return grammarlist

    except IOError:
        print('stop word file not found!\nabort')
        exit()

def clean_text(original_list,grammarlist):
    list_splitted=[]
    list_clean=[]
    for i in original_list:
        if i:
            list_splitted.append((i.split()))
    clean_title=[]

    for i in range(0, len(list_splitted)):
        for j in range(0, len(list_splitted[i])):
            if not re.search('https?|RT|href=|class=|src=|align=|border=|height=|width=|alt=|<p>|reading|@', list_splitted[i][j]):
                list_splitted[i][j]=re.sub('[\U0001F300-\U0001F6FF]|•|‘|\'s|\'|"|”|!|’s|“|,|:|&|;|/|\+|\?|…|[.]+|-|–|—|→|\(|\)|#|', '', list_splitted[i][j]) #i clean the text from link replytweet and @tag
                if not (len(list_splitted[i][j]) < 3):
                    if not (any(list_splitted[i][j].lower() in s for s in grammarlist)):
                        clean_title.append(list_splitted[i][j].lower())
        list_clean.append([i for i in clean_title])
        clean_title.clear()
    return list_clean


import numpy as np
import sys
import emcee
import pickle as pickle
import time
from emcee.utils import MPIPool

from petitRADTRANS import Radtrans
from petitRADTRANS import nat_cst as nc
import master_retrieval_model as rm
import rebin_give_width as rgw

sys.stdout.flush()

################################################################################
################################################################################
### Hyperparameter setup, where to save things to / read things from
################################################################################
################################################################################

retrieval_name = 'JWST_transmission_petitRADTRANSpaper'
absolute_path = '' # end with forward slash!
observation_files = {}
observation_files['NIRISS SOSS'] = 'NIRISS_SOSS.dat'
observation_files['NIRSpec G395M'] = 'NIRSpec_G395M.dat'
observation_files['MIRI LRS'] = 'MIRI_LRS.dat'

# For testing purposes only
plotting = False
if plotting:
    import pylab as plt

# Retrieval hyperparameters
stepsize = 1.75
n_walkers = 240
n_iter = 4200

cluster = False       # Submit to cluster
n_threads = 1         # Use mutliprocessing (local = 1)
write_threshold = 200 # number of iterations after which diagnostics are updated

# Wavelength range of observations, fixed parameters that will not be retrieved
WLEN = [0.8, 14.0]
LOG_G =  2.58
R_pl =   1.84*nc.r_jup_mean
R_star = 1.81*nc.r_sun

################################################################################
################################################################################
### READ IN OBSERVATION
################################################################################
################################################################################

# Read in data, convert all to cgs!
data_wlen = {}
data_flux_lambda = {}
data_flux_lambda_error = {}
data_wlen_bins = {}

for name in observation_files.keys():

    dat_obs = np.genfromtxt(observation_files[name])
    data_wlen[name] = dat_obs[:,0]*1e-4
    data_flux_lambda[name] = dat_obs[:,1]
    data_flux_lambda_error[name] = dat_obs[:,2]

    data_wlen_bins[name] = np.zeros_like(data_wlen[name])
    data_wlen_bins[name][:-1] = np.diff(data_wlen[name])
    data_wlen_bins[name][-1] = data_wlen_bins[name][-2]

################################################################################
################################################################################
### MODEL SETUP
################################################################################
################################################################################

### Create and setup radiative transfer object
# Create random P-T profile to create RT arrays of the Radtrans object.
temp_params = {}
temp_params['log_delta'] = -6.
temp_params['log_gamma'] = np.log10(0.4)
temp_params['t_int'] = 750.
temp_params['t_equ'] = 0.
temp_params['log_p_trans'] = -3.
temp_params['alpha'] = 0.
p, t = nc.make_press_temp(temp_params)

# Create the Ratrans object here
rt_object = Radtrans(line_species=['H2', 'CO_all_iso', 'H2O', \
                                  'CH4', 'NH3', 'CO2', 'H2S', \
                                  'Na', 'K'], \
                    rayleigh_species=['H2','He'], \
                    continuum_opacities = ['H2-H2','H2-He'], \
                    mode='c-k', \
                    wlen_bords_micron = WLEN)

# Create the RT arrays of appropriate lengths
rt_object.setup_opa_structure(p)

################################################################################
################################################################################
###   PRIOR SETUP (For all priors, neglect the constant that would be added
###         because of the normalization of the normal distribution)
################################################################################
################################################################################

def b_range(x, b):
    if x > b:
        return -np.inf
    else:
        return 0.

def a_b_range(x, a, b):
    if x < a:
        return -np.inf
    elif x > b:
        return -np.inf
    else:
        return 0.

log_priors = {}
log_priors['log_delta']      = lambda x: -((x-(-5.5))/2.5)**2./2.
log_priors['log_gamma']      = lambda x: -((x-(-0.0))/2.)**2./2.
log_priors['t_int']          = lambda x: a_b_range(x, 0., 1500.)
log_priors['t_equ']          = lambda x: a_b_range(x, 0., 4000.)
log_priors['log_p_trans']    = lambda x: -((x-(-3))/3.)**2./2.
log_priors['alpha']          = lambda x: -((x-0.25)/0.4)**2./2.
log_priors['log_g']          = lambda x: a_b_range(x, 2.0, 3.7)
log_priors['log_P0']         = lambda x: a_b_range(x, -4, 2.)

# Priors for log mass fractions
log_priors['CO_all_iso']     = lambda x: a_b_range(x, -10., 0.)
log_priors['H2O']            = lambda x: a_b_range(x, -10., 0.)
log_priors['CH4']            = lambda x: a_b_range(x, -10., 0.)
log_priors['NH3']            = lambda x: a_b_range(x, -10., 0.)
log_priors['CO2']            = lambda x: a_b_range(x, -10., 0.)
log_priors['H2S']            = lambda x: a_b_range(x, -10., 0.)
log_priors['Na']             = lambda x: a_b_range(x, -10., 0.)
log_priors['K']              = lambda x: a_b_range(x, -10., 0.)

################################################################################
################################################################################
### DEFINE LOG PROBABILITY
################################################################################
################################################################################

# Declare diagnostics
function_calls = 0
computed_spectra = 0
NaN_spectra = 0
delta_wt = write_threshold

start_time = time.time()
file_object = open(absolute_path + 'diag_' + \
                       retrieval_name + '.dat', 'w').close()

def calc_log_prob(params):

    log_delta, log_gamma, t_int, t_equ, log_p_trans, alpha, \
      log_g, log_P0 = params[:-8]

    # Make dictionary for modified Guillot parameters
    temp_params = {}
    temp_params['log_delta'] = log_delta
    temp_params['log_gamma'] = log_gamma
    temp_params['t_int'] = t_int
    temp_params['t_equ'] = t_equ
    temp_params['log_p_trans'] = log_p_trans
    temp_params['alpha'] = alpha

    # Make dictionary for log 'metal' abundances
    ab_metals = {}
    ab_metals['CO_all_iso']     = params[-8:][0]
    ab_metals['H2O']            = params[-8:][1]
    ab_metals['CH4']            = params[-8:][2]
    ab_metals['NH3']            = params[-8:][3]
    ab_metals['CO2']            = params[-8:][4]
    ab_metals['H2S']            = params[-8:][5]
    ab_metals['Na']             = params[-8:][6]
    ab_metals['K']              = params[-8:][7]

    global function_calls
    global computed_spectra
    global NaN_spectra
    global write_threshold

    function_calls += 1

    # Prior calculation of all input parameters
    log_prior = 0.

    # Alpha should not be smaller than -1, this
    # would lead to negative temperatures!
    if alpha < -1:
        return -np.inf

    for key in temp_params.keys():
        log_prior += log_priors[key](temp_params[key])

    log_prior += log_priors['log_g'](log_g)
    log_prior += log_priors['log_P0'](log_P0)

    # Metal abundances: check that their
    # summed mass fraction is below 1.
    metal_sum = 0.
    for name in ab_metals.keys():
        log_prior += log_priors[name](ab_metals[name])
        metal_sum += 1e1**ab_metals[name]

    if metal_sum > 1.:
        log_prior += -np.inf

    # Return -inf if parameters fall outside prior distribution
    if (log_prior == -np.inf):
        return -np.inf

    # Calculate the log-likelihood
    log_likelihood = 0.

    # Calculate the forward model, this
    # returns the wavelengths in cm and the planet radius
    # in R_jup.
    wlen, flux_lambda = \
            rm.retrieval_model_plain(rt_object, temp_params, log_g, \
                                         log_P0, R_pl, ab_metals)

    # Just to make sure that a long chain does not die
    # Unexpectedly:
    # Return -inf if retrieval model returns NaN values
    if np.sum(np.isnan(flux_lambda)) > 0:
        print("NaN spectrum encountered")
        NaN_spectra += 1
        return -np.inf

    # Convert to observation for transmission case
    flux_sq = (flux_lambda*nc.r_jup_mean/R_star)**2

    # Calculate log-likelihood
    for instrument in data_wlen.keys():

        # Rebin model to observation
        flux_rebinned = rgw.rebin_give_width(wlen, flux_sq, \
                        data_wlen[instrument], data_wlen_bins[instrument])

        if plotting:
            plt.errorbar(data_wlen[instrument], \
                             data_flux_lambda[instrument], \
                             data_flux_lambda_error[instrument], \
                             fmt = 'o', \
                             zorder = -20, \
                             color = 'red')

            plt.plot(data_wlen[instrument], \
                             flux_rebinned, \
                             's', \
                             zorder = -20, \
                             color = 'blue')

        # Calculate log-likelihood
        log_likelihood += \
               -np.sum(((flux_rebinned - data_flux_lambda[instrument])/ \
                    data_flux_lambda_error[instrument])**2.)/2.

    if plotting:
        plt.plot(wlen, flux_sq, color = 'black')
        plt.xscale('log')
        plt.show()

    computed_spectra += 1

    # Write diagnostics file
    if (function_calls >= write_threshold):

        write_threshold += delta_wt
        hours = (time.time() - start_time)/3600.0
        info_list = [function_calls, computed_spectra, NaN_spectra, \
                 log_prior + log_likelihood, hours]

        file_object = open(absolute_path + 'diag_' + \
                               retrieval_name + '.dat', 'a')

        for i in np.arange(len(info_list)):
            if (i == len(info_list) - 1):
                file_object.write(str(info_list[i]).ljust(15) + "\n")
            else:
                file_object.write(str(info_list[i]).ljust(15) + " ")
        file_object.close()

    print(log_prior + log_likelihood)
    print("--> ", function_calls, " --> ", computed_spectra)
    return log_prior + log_likelihood

def lnprob(x):
    return calc_log_prob(x)

################################################################################
################################################################################
### MCMC
################################################################################
################################################################################

n_dim = len(log_priors)

# Set initial position vectors in parameter space
p0 = [np.array([np.random.normal(loc = -5.5, scale = 2.5, size=1)[0], \
                np.random.normal(loc = 0., scale = 2., size=1)[0], \
                0.+1500.*np.random.uniform(size=1)[0], \
                0.+4000.*np.random.uniform(size=1)[0], \
                np.random.normal(loc = -3., scale = 3., size=1)[0], \
                np.random.normal(loc = -0.25, scale = 0.4, size=1)[0], \
                LOG_G,
                -4.+6.*np.random.uniform(size=1)[0], \
                -10.+10.*np.random.uniform(size=1)[0], \
                -10.+10.*np.random.uniform(size=1)[0], \
                -10.+10.*np.random.uniform(size=1)[0], \
                -10.+10.*np.random.uniform(size=1)[0], \
                -10.+10.*np.random.uniform(size=1)[0], \
                -10.+10.*np.random.uniform(size=1)[0], \
                -10.+10.*np.random.uniform(size=1)[0], \
                -10.+10.*np.random.uniform(size=1)[0]] \
                ) for i in range(n_walkers)]

print('Run pre-burn-in!')
print()

# Initialize the MPI-based pool used for parallelization.
if cluster:
    pool = MPIPool()
    if not pool.is_master():
        pool.wait()
        sys.exit(0)
    sampler = emcee.EnsembleSampler(n_walkers, n_dim, lnprob, \
                                        a = stepsize, pool = pool)
else:
    if n_threads > 1:
        sampler = emcee.EnsembleSampler(n_walkers, n_dim, lnprob, \
                                            a = stepsize, threads = n_threads)
    else:
        sampler = emcee.EnsembleSampler(n_walkers, n_dim, lnprob, \
                                            a = stepsize)

# Run the actual MCMC
pre_burn_in_runs = int(np.min([399, n_iter/10])) + 3
pos, prob, state = sampler.run_mcmc(p0, pre_burn_in_runs)

# Get the best-fit position
highest_prob_index = np.unravel_index(sampler.lnprobability.argmax(), \
                                          sampler.lnprobability.shape)
best_position = sampler.chain[highest_prob_index]

f = open('best_position_pre_burn_in_' + retrieval_name + '.dat', 'w')
f.write(str(best_position))
f.close()

# Run actual chain
p0 = [np.array([best_position[0]+np.random.normal(size=1)[0]*0.8, \
                best_position[1]+np.random.normal(size=1)[0]*0.5, \
                best_position[2]+np.random.normal(size=1)[0]*70., \
                best_position[3]+np.random.normal(size=1)[0]*200., \
                best_position[4]+np.random.normal(size=1)[0]*0.5, \
                best_position[5]+np.random.normal(size=1)[0]*0.1, \
                LOG_G, \
                best_position[7]+np.random.normal(size=1)[0]*0.2, \
                best_position[8]+np.random.normal(size=1)[0]*0.3, \
                best_position[9]+np.random.normal(size=1)[0]*0.3, \
                best_position[10]+np.random.normal(size=1)[0]*0.3, \
                best_position[11]+np.random.normal(size=1)[0]*0.3, \
                best_position[12]+np.random.normal(size=1)[0]*0.3, \
                best_position[13]+np.random.normal(size=1)[0]*0.3, \
                best_position[14]+np.random.normal(size=1)[0]*0.3, \
                best_position[15]+np.random.normal(size=1)[0]*0.3] \
                   ) for i in range(n_walkers)]

print('Run chain!')
print()

if cluster:
    sampler = emcee.EnsembleSampler(n_walkers, n_dim, lnprob, \
                                        a = stepsize, pool = pool)
else:
    if n_threads > 1:
        sampler = emcee.EnsembleSampler(n_walkers, n_dim, lnprob, \
                                            a = stepsize, threads = n_threads)
    else:
        sampler = emcee.EnsembleSampler(n_walkers, n_dim, lnprob, \
                                            a = stepsize)

pos, prob, state = sampler.run_mcmc(p0, n_iter)

if cluster:
    pool.close()

################################################################################
################################################################################
### Save results
################################################################################
################################################################################

f = open('chain_pos_' + retrieval_name + '.pickle','wb')
pickle.dump(pos,f)
pickle.dump(prob,f)
pickle.dump(state,f)
samples = sampler.chain[:, :, :].reshape((-1, n_dim))
pickle.dump(samples,f)
f.close()

with open('chain_lnprob_' + retrieval_name + '.pickle', 'wb') as f:
    pickle.dump([sampler.lnprobability], \
                f, protocol=pickle.HIGHEST_PROTOCOL)

################################################################################
################################################################################
### Done.
################################################################################
################################################################################


N = int(input())
dir = {"E":"S","S":"E"}

for x in range(N):
    size = input()
    lydia = list(input())
    stack = []
    for item in lydia:
        stack.append(dir[item])
    print ("Case #" + str(x+1) + ": " + ''.join([str(x) for x in stack]))


#!/usr/bin/env python3

import sys
import argparse

from modules.checker import Checker
from modules.useful import termcolor, open_file
from modules.sqrt_structure import SqrtDecomposition

class FirstTask:

    def __init__(self):
        pass

    def start(self):
        args = self.init_parser()
        self.output_file = args.output
        self.input_file = args.input
        if args.auto:
            try:
                checker = Checker(args.count_test,
                                    args.size_array,
                                    args.output)
                checker.generate()
            except:
                print("{0}ERROR: Parameters for auto-checker "\
                                            "are wrong!{1}".format(
                                                            termcolor.FAIL,
                                                            termcolor.ENDC))
        else:
            self.sqrt_manage()

    def sqrt_manage(self):
        # input file if presented
        if self.input_file is not None:
            self.cin = open_file(self.input_file, "r",
                                            "Can't open input file!")
        # output file if presented
        if self.output_file is not None:
            self.cout = open_file(self.output_file, "w",
                                            "Can't create output file!")
        # enter array
        try:
            array = [ int(i) for i in self.input("Enter array: ") ]
        except:
            print(termcolor.FAIL
                + "ERROR: Check the entered array!"
                + termcolor.ENDC)
            exit()
        # create sqrt structure
        sqrt = SqrtDecomposition(array)
        query = self.input("[INFO] "\
            "{0}1 <= L <= R <= {2}{1}"\
            "\nQueries should match the following syntax : "\
            "\n\t{0}assign L R X{1}\tassign the value of X to elements [L, R]"\
            "\n\t{0}add L R X{1}   \tadd the value of X to elements [L, R]"\
            "\n\t{0}get L R{1}     \tget the sum of elements [L, R]"\
            "\n\t{0}exit{1}        \tstop the program\n\n".format(
                                                            termcolor.BOLD,
                                                            termcolor.ENDC,
                                                            len(array)))
        # processing queries
        count = 1
        while True:
            if query[0] == "exit":
                if self.output_file is not None:
                    self.cout.close()
                if self.input_file is not None:
                    self.cin.close()
                exit()
            if query[0] == "":
                query = self.input()
                continue
            # else let's find the instructions
            try:
                query[1:3] = [ int(i)-1 for i in query[1:3] ]
                query[3:] = [ int(i) for i in query[3:] ]
                if query[1] < 0 or query[2] >= len(array)\
                                or query[1] > query[2]:
                    raise Exception
                if query[0] == "assign":
                    query.pop(0)
                    sqrt.assign(*query)
                elif query[0] == "add":
                    query.pop(0)
                    sqrt.add(*query)
                elif query[0] == "get":
                    query.pop(0)
                    self.print(str(sqrt.get(*query)))
                else:
                    raise Exception
            except:
                print("{0}ERROR: Check your query (#{1}){2}\n".format(
                                                                termcolor.FAIL,
                                                                count,
                                                                termcolor.ENDC))
            query = self.input()
            count += 1

    def print(self, text):
        if self.output_file is None:
            print(text)
        else:
            self.cout.write(text + "\n")

    def input(self, text=""):
        if self.input_file is None:
            payload = str(input(text))
        else:
            payload = self.cin.readline()
        return payload.strip().split(" ")

    def init_parser(self):
        pr = argparse.ArgumentParser(
                            description="Use this module to interact with "\
                            "Sqrt Decomposition by "\
                            "[file | console | auto-checker]")
        pr.add_argument("-i", "--input",
                            help="Specify input file.")
        pr.add_argument("-a", "--auto",
                            help="Specify to run auto-checker.",
                            action="store_true")
        pr.add_argument("-s", "--size-array",
                            help="Auto-checker argument.",
                            default=10,
                            type=int)
        pr.add_argument("-c", "--count-test",
                            help="Auto-checker argument.",
                            default=1000,
                            type=int)
        pr.add_argument("-o", "--output",
                            help="Specify output file.")
        return pr.parse_args()

if __name__ == "__main__":
    task = FirstTask()
    task.start()


# -*- coding: utf-8 -*-
import contextlib
import inspect
import os
import socket

import mock
import pytest

from .socketsig_helper import socket_signature_validators

try:
    from cryptography import x509
    from OpenSSL.crypto import FILETYPE_PEM, load_certificate

    from urllib3.contrib.pyopenssl import (
        WrappedSocket,
        _dnsname_to_stdlib,
        get_subj_alt_name,
    )
except ImportError:
    pass

def setup_module():
    try:
        from urllib3.contrib.pyopenssl import inject_into_urllib3

        inject_into_urllib3()
    except ImportError as e:
        pytest.skip("Could not import PyOpenSSL: %r" % e)

def teardown_module():
    try:
        from urllib3.contrib.pyopenssl import extract_from_urllib3

        extract_from_urllib3()
    except ImportError:
        pass

from ..test_util import TestUtilSSL  # noqa: E402, F401
from ..with_dummyserver.test_https import (  # noqa: E402, F401
    TestHTTPS,
    TestHTTPS_IPSAN,
    TestHTTPS_IPv6Addr,
    TestHTTPS_IPV6SAN,
    TestHTTPS_NoSAN,
    TestHTTPS_TLSv1,
    TestHTTPS_TLSv1_1,
    TestHTTPS_TLSv1_2,
    TestHTTPS_TLSv1_3,
)
from ..with_dummyserver.test_socketlevel import (  # noqa: E402, F401
    TestClientCerts,
    TestSNI,
    TestSocketClosing,
    TestSSL,
)

class TestPyOpenSSLHelpers(object):
    """
    Tests for PyOpenSSL helper functions.
    """

    def test_dnsname_to_stdlib_simple(self):
        """
        We can convert a dnsname to a native string when the domain is simple.
        """
        name = u"उदाहरण.परीक"
        expected_result = "xn--p1b6ci4b4b3a.xn--11b5bs8d"

        assert _dnsname_to_stdlib(name) == expected_result

    def test_dnsname_to_stdlib_leading_period(self):
        """
        If there is a . in front of the domain name we correctly encode it.
        """
        name = u".उदाहरण.परीक"
        expected_result = ".xn--p1b6ci4b4b3a.xn--11b5bs8d"

        assert _dnsname_to_stdlib(name) == expected_result

    def test_dnsname_to_stdlib_leading_splat(self):
        """
        If there's a wildcard character in the front of the string we handle it
        appropriately.
        """
        name = u"*.उदाहरण.परीक"
        expected_result = "*.xn--p1b6ci4b4b3a.xn--11b5bs8d"

        assert _dnsname_to_stdlib(name) == expected_result

    @mock.patch("urllib3.contrib.pyopenssl.log.warning")
    def test_get_subj_alt_name(self, mock_warning):
        """
        If a certificate has two subject alternative names, cryptography raises
        an x509.DuplicateExtension exception.
        """
        path = os.path.join(os.path.dirname(__file__), "duplicate_san.pem")
        with open(path, "r") as fp:
            cert = load_certificate(FILETYPE_PEM, fp.read())

        assert get_subj_alt_name(cert) == []

        assert mock_warning.call_count == 1
        assert isinstance(mock_warning.call_args[0][1], x509.DuplicateExtension)

    def test_socket_signature(self):
        """
        Check WrappedSocket methods match corresponding socket.socket
        method signatures.
        """
        with contextlib.closing(socket.socket()) as sock:
            validators = socket_signature_validators()
            wrapper = WrappedSocket(None, sock)

            # iterate through all WrappedSocket methods and check
            # the ones that override socket.socket (heuristic: same
            # name but don't start with an underscore)
            for name, method in inspect.getmembers(wrapper, inspect.ismethod):
                if name.startswith("_"):
                    continue
                if not hasattr(sock, name):
                    continue

                # test fails here if WrappedSocket defines overrides
                # socket.socket but there is no corresponding validator
                # defined in sockesig_helper
                assert name in validators

                print("Check function signature: WrappedSocket.{}".format(name))

                # the trick here is to avoid calling WrappedSocket
                # method directly because we don't have a proper
                # fixture. Instead create a mock with the same signature
                # (autospec) and call the validator on it.
                setattr(wrapper, name, mock.create_autospec(getattr(wrapper, name)))
                for counter, m_call in enumerate(
                    [m for group in validators[name] for m in group]
                ):
                    print("    WrappedSocket.{} test call #{}".format(name, counter))

                    # test fails here if WrapperSocket method does
                    # not match socket.socket method signature
                    m_call(wrapper)


# Basic training configuration file
from pathlib import Path
from torchvision.transforms import RandomVerticalFlip, RandomHorizontalFlip
from torchvision.transforms import RandomResizedCrop
from torchvision.transforms import ToTensor, Normalize
from common.dataset import get_test_data_loader

SEED = 12345
DEBUG = True

OUTPUT_PATH = Path("output") / "val_probas"
dataset_path = Path("/home/fast_storage/imaterialist-challenge-furniture-2018/")

SAVE_PROBAS = True
# SAMPLE_SUBMISSION_PATH = dataset_path / "sample_submission_randomlabel.csv"

TEST_TRANSFORMS = [
    RandomResizedCrop(350, scale=(0.8, 1.0), interpolation=3),
    RandomVerticalFlip(p=0.5),
    RandomHorizontalFlip(p=0.5),
    ToTensor(),
    Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
]

N_CLASSES = 128
BATCH_SIZE = 24
NUM_WORKERS = 15

TEST_LOADER = get_test_data_loader(
    dataset_path=dataset_path / "validation",
    test_data_transform=TEST_TRANSFORMS,
    batch_size=BATCH_SIZE,
    num_workers=NUM_WORKERS,
    pin_memory=True)

MODEL = (Path("output") / "train" / "train_inceptionv4_350_fc_random_resized_crop" / "20180506_2103" /
         "model_FurnitureInceptionV4_350_FC_10_val_loss=0.5576787.pth").as_posix()

N_TTA = 12


import json

from cartoview.app_manager.models import App, AppInstance
from cartoview.app_manager.views import StandardAppViews
from django.shortcuts import HttpResponse

from . import APP_NAME
_js_permissions_mapping = {
    'whoCanView': 'view_resourcebase',
    'whoCanChangeMetadata': 'change_resourcebase_metadata',
    'whoCanDelete': 'delete_resourcebase',
    'whoCanChangeConfiguration': 'change_resourcebase'
}

def change_dict_None_to_list(access):
    for permission, users in list(access.items()):
        if not users:
            access[permission] = []

class FeatureList(StandardAppViews):
    def get_users_permissions(self, access, initial, owner):
        change_dict_None_to_list(access)
        users = []
        for permission_users in list(access.values()):
            if permission_users:
                users.extend(permission_users)
        users = set(users)
        for user in users:
            user_permissions = []
            for js_permission, gaurdian_permission in \
                    list(_js_permissions_mapping.items()):
                if user in access[js_permission]:
                    user_permissions.append(gaurdian_permission)
            if len(user_permissions) > 0 and user != owner:
                initial['users'].update({'{}'.format(user): user_permissions})
        if len(access["whoCanView"]) == 0:
            initial['users'].update({'AnonymousUser': [
                'view_resourcebase',
            ]})

    def save(self, request, instance_id=None):
        user = request.user
        res_json = dict(success=False)
        data = json.loads(request.body)
        config = data.get('config', None)
        map_id = data.get('map', None)
        title = data.get('title', "")
        access = data.get('access', None)
        keywords = data.get('keywords', [])
        config.update(access=access, keywords=keywords)
        config = json.dumps(data.get('config', None))
        abstract = data.get('abstract', "")

        if instance_id is None:
            instance_obj = AppInstance()
            instance_obj.app = App.objects.get(name=self.app_name)
            instance_obj.owner = user
        else:
            instance_obj = AppInstance.objects.get(pk=instance_id)
            user = instance_obj.owner

        instance_obj.title = title
        instance_obj.config = config
        instance_obj.abstract = abstract
        instance_obj.map_id = map_id
        instance_obj.save()
        owner_permissions = [
            'view_resourcebase',
            'download_resourcebase',
            'change_resourcebase_metadata',
            'change_resourcebase',
            'delete_resourcebase',
            'change_resourcebase_permissions',
            'publish_resourcebase',
        ]
        permessions = {
            'users': {
                '{}'.format(request.user.username): owner_permissions,
            }
        }
        self.get_users_permissions(access, permessions, user.username)
        instance_obj.set_permissions(permessions)
        if hasattr(instance_obj, 'keywords') and keywords:
            new_keywords = [
                k for k in keywords if k not in instance_obj.keyword_list()]
            instance_obj.keywords.add(*new_keywords)

        res_json.update(dict(success=True, id=instance_obj.id))
        return HttpResponse(json.dumps(res_json),
                            content_type="application/json")

    def edit(self, request, instance_id,
            template=None, context={}, * args, **kwargs):
        return super(FeatureList, self).edit(request, instance_id=instance_id, template=None, context={'APP_NAME': APP_NAME}, * args, **kwargs)

    def new(self, request, template=None, context={}, *args, **kwargs):
        return super(FeatureList, self).new(request, template=None, context={'APP_NAME': APP_NAME}, *args, **kwargs)

    def view_app(self, request, instance_id, template=None, context={}):
        return super(FeatureList, self).view_app(request, instance_id=instance_id, template=None, context={'APP_NAME': APP_NAME})

feature_list = FeatureList(APP_NAME)


#!/usr/bin/env python3

import json
import pathlib
import sys
import uuid

class Project(object):
    def __init__(self, path):
        self.path = path

    def __call__(self):
        self.set_images()
        self.set_textures()
        self.set_objects()
        self.set_materials()
        self.set_previz_data()
        self.set_previz_scene_data()

    def __enter__(self):
        with self.exported_scene_path.open() as fp:
            self.d = json.load(fp)
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        with self.assembly_scene_path.open('w') as fp:
            json.dump(self.d, fp, indent=2, sort_keys=True)
        del self.d

    def set_images(self):
        self.d['images'] = list(self.build_image(p) for p in self.image_paths)

    def build_image(self, path):
        return {
            'uuid': self.uuid(),
            'name': path.name,
            'url':str(path.relative_to(path.parent.parent))
        }

    def set_textures(self):
        self.d['textures'] = list(self.build_texture(i) for i in self.d['images'])

    def build_texture(self, image):
        return {
            'name': self.texture_path_to_name(pathlib.Path(image['name'])),
            'uuid': self.uuid(),
            'image': image['uuid'],
            'minFilter': 1003,
            'magFilter': 1003
        }

    def texture_path_to_name(self, path):
        stem = path.stem
        return ''.join(stem.split('_')[1:])

    @property
    def image_paths(self):
        return (x for x in self.scene_assets_paths if x.suffix != '.json')

    def set_materials(self):
        self.d['materials'] = []

        normal_mat = {
            'name': 'NormalMaterial',
            'type': 'MeshNormalMaterial',
            'uuid': self.uuid()
        }
        self.d['materials'].append(normal_mat)

        for mesh in self.meshes:
            mat = self.build_mapping_material(mesh['name'])
            self.d['materials'].append(mat)

            mesh_mat = normal_mat
            if mat.get('map') is not None:
                mesh_mat = mat
            mesh['material'] = mesh_mat['uuid']

    def set_previz_scene_data(self):
        self.scene['userData'] = {'previz': self.build_scene_data()}

    def set_previz_data(self):
        for mesh in self.meshes:
            mesh['userData'] = {'previz': self.build_previz_data(mesh)}

    def build_previz_data(self, mesh):
        ret = {}

        mesh_name = mesh['name']

        ret['defaultMaterialUuid'] = self.default_material['uuid']
        ret['mappingMaterialUuid'] = self.mapping_material(mesh_name)['uuid']
        ret['mapsUuids'] = [m['uuid'] for m in self.mesh_maps]
        ret['alphaMapsUuids'] = [m['uuid'] for m in self.mesh_alpha_maps]

        return ret

    def build_scene_data(self):
        imagesUuids = []
        videosUuids = []

        imageTexturesUuids = []
        videoTexturesUuids = []

        for media in self.d['images']:
            l = imagesUuids
            if self.is_video(media):
                l = videosUuids
            l.append(media['uuid'])

        for texture in self.d['textures']:
            l = imageTexturesUuids
            if self.is_source_video(texture):
                l = videoTexturesUuids
            l.append(texture['uuid'])

        return {'imagesUuids': imagesUuids,
                'videosUuids': videosUuids,
                'imageTexturesUuids': imageTexturesUuids,
                'videoTexturesUuids': videoTexturesUuids}

    @staticmethod
    def is_video(media):
        return media['url'].endswith('.mp4')

    def is_source_video(self, texture):
        media_uuid = texture['image']
        for media in self.d['images']:
            if media['uuid'] == media_uuid:
                return self.is_video(media)
        raise ValueError('Cannot find image {} for texture {}'.format(media_uuid, texture['uuid']))

    @property
    def default_material(self):
        for m in self.d['materials']:
            if m['type'] == 'MeshNormalMaterial':
                return m

    def mapping_material(self, mesh_name):
        for m in self.d['materials']:
            if m['name'] == self.mapping_material_name(mesh_name):
                return m

    def mapping_material_name(self, mesh_name):
        return 'MappingMaterial-{}'.format(mesh_name)

    def build_mapping_material(self, mesh_name):
        m =  {
            'name': self.mapping_material_name(mesh_name),
            'type': 'MeshBasicMaterial',
            'uuid': self.uuid()
        }

        maps = list(self.mesh_maps)
        if len(maps) > 0:
            m['map'] = maps[0]['uuid']

        alpha_maps = list(self.mesh_alpha_maps)
        if len(alpha_maps) > 0:
            m['alphaMap'] = alpha_maps[0]['uuid']

        return m

    def source_filename_startswith(self, texture, prefix):
        for image in self.d['images']:
            if image['uuid'] == texture['image']:
                return pathlib.Path(image['url']).stem.startswith(prefix)
        raise ValueError('Cannot find image {} for texture {}'.format(texture['image'], texture['uuid']))

    @property
    def mesh_maps(self):
        return (t for t in self.d['textures'] if self.source_filename_startswith(t, 'map_'))

    @property
    def mesh_alpha_maps(self):
        return (t for t in self.d['textures'] if self.source_filename_startswith(t, 'alphaMap_'))

    @property
    def meshes(self):
        return (o for o in self.scene['children'] if o['type'] == 'Mesh')

    @property
    def scene(self):
        return self.d['object']

    def set_objects(self):
        def is_valid_object(o):
            type = o['type']
            return type == 'Mesh' or type.endswith('Camera')

        self.d['object']['children'] = list(o for o in self.d['object']['children'] if is_valid_object(o))

    @property
    def exported_scene_path(self):
        for p in self.scene_assets_paths:
            if p.suffix == '.json':
                return p

    @property
    def assembly_scene_path(self):
        return self.path / 'scene.json'

    @property
    def scene_assets_paths(self):
        return (self.path / 'scene_assets').iterdir()

    @staticmethod
    def uuid():
        return str(uuid.uuid4()).upper()

if __name__ == '__main__':
    p = Project(pathlib.Path(sys.argv[1]))
    with p as p:
        p()


def validate_github_webhook(request, secret: str) -> bool:
    import hmac

    sig = request.headers.get('X-Hub-Signature')
    if not sig:
        return False

    scheme, sig = sig.split('=')
    if scheme != 'sha1':
        return False

    mac = hmac.new(secret.encode(), msg=request.data, digestmod='sha1')
    return hmac.compare_digest(str(mac.hexdigest()), str(sig))


"""
https://www.codewars.com/kata/next-bigger-number-with-the-same-digits/train/python
1. If number has only 1 digit return -1.
2. If number has digits in increasing order it has a bigger number, else: return -1.
3. Convert number to each digit list.
4. Split the list at digit (named "border") at which increase is observed (first occurrence from the right side).
5. Head is preserved intact.
6. Tail: find the next to border bigger number (named "substitute").
7. Exchange substitute and border in tail. Sort tail.
8. Return concatenated list converted to number.
"""

def next_bigger(n):
    arr = [x for x in str(n)]
    for i in range(len(arr) - 1, 0, -1):
        if arr[i] > arr[i - 1]:
            border, tail, head = arr[i - 1], arr[i:], arr[:i - 1]
            substitute = min([y for y in filter(lambda x: x > border, tail)])
            tail.remove(substitute)
            tail.append(border)
            tail.sort()
            return int(''.join(head+[substitute]+tail))
    return -1

print(next_bigger(80464931186))
print(next_bigger(1))


import matplotlib.pyplot as plt
import sys

infile = sys.argv[1]

eigvals = []

for lines in open(infile, "r"):
    col = lines.split()
    eigvals.append(float(col[0]))

plt.plot(eigvals)
plt.show()


def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print(f"You have {cheese_count} cheeses!")
    print(f"You have {boxes_of_crackers} boxes of crackers!")
    print("Man that's enough for a party!")
    print("Get a blanket.\n")

print("We can just give the function numbers directly:")
cheese_and_crackers(20, 30)

print("OR, we can use variables from our script:")
amount_of_cheese = 10
amount_of_crackers = 50

cheese_and_crackers(amount_of_cheese, amount_of_crackers)

print("We can even do math inside too:")
cheese_and_crackers(10 + 20, 5 + 6)

print("And we can combine the two, variables and math:")
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

def my_function(input1, input2):
    my_string = f"{input1} is a {input2}"
    print(my_string)
    return my_string

my_function("bob", "man")
my_function("mallard", "duck")
my_function("four", "form of nothingness")
my_function(my_function("one", "number"), "string, actually")
#... and so on

def a_function(arg1, arg2, arg3):
    print(arg1, arg2, arg3, sep = ", ")

var1 = 10
var2 = 20
a_function(var1, var2, var1 + var2)


class DistributeCandies(object):
    def distributeCandiesSolution1(self, candies):
        """
        :type candies: List[int]
        :rtype: int
        """
        MIN_VALUE = -100000
        MAX_VALUE = 100000
        existing_candies = [0] * (MAX_VALUE - MIN_VALUE + 1)
        res = 0
        for idx in range(len(candies)):
            if existing_candies[candies[idx] - MIN_VALUE] == 0:
                res += 1
                existing_candies[candies[idx] - MIN_VALUE] = 1
            if res == len(candies) / 2:
                return res
        return res

    def distributeCandiesSolution2(self, candies):
        """
        :type candies: List[int]
        :rtype: int
        """
        return min(len(candies)/2, len(set(candies)))


# University of Sheffield
# Project: Individual Project
# Author: Will Sanders
# Date: 22/10/19
# Version: 1.0
# Description: Script that takes a video and saves the individual frames as separate images.
#              Useful when making a data set of images for deep learning techniques.

import cv2
import argparse

# Argument parsing setup
# Initialises both the file to be split, and the name, and path of the
# images that will be saved
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", required=True, help="path to the video file")
ap.add_argument("-n", "--name", required=True, help="name to save images as")
args = vars(ap.parse_args())

# Grabs the specified video
video = cv2.VideoCapture(args["video"])

# Sets the image save location and file name
save_location = args["name"]

# Set a counter to 0 for the start of the video
counter = 3261

# Loops over all the frames in the video
while True:
    # Grabs the current frame
    (grabbed, frame) = video.read()

    # Breaks while loop if no new frame is grabbed
    if args.get("video") and not grabbed:
        break

    # Increase the counter by one to save the frame as a new image
    counter += 1

    # Saves the frame as a new image in the specified location
    cv2.imwrite("{}_{}.jpg".format(save_location, counter), frame)

# Displays to the screen the total number of frames saves
print("{} frames saves".format(counter))


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

# import Python modules
from os import *
from shutil import *

# import user modules
#from error import *
#from aips import *

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

def file_exists( file_path ):
  return path.isfile( file_path )

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

def directory_exists( dir_path ):
  return path.isdir( dir_path )

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

def remove_file( path ):
  status = True
  if file_exists( path ):
    try:
      remove( path )
    except:
      status = False
  return status

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

def move_file( src_path, dest_path, overwrite = True ):
  status = True
  if ( ( not file_exists( src_path ) ) or ( ( overwrite == False ) and ( file_exists( dest_path ) ) ) ):
    status = False
  else:
    if ( ( overwrite == True ) and ( file_exists( dest_path ) ) ):
      status = remove_file( dest_path )
  if ( status == True ):
    try:
      move( src_path, dest_path )
    except:
      status = False
  return status

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

def copy_file( src_path, dest_path, overwrite = True ):
  status = True
  if ( ( not file_exists( src_path ) ) or ( ( overwrite == False ) and ( file_exists( dest_path ) ) ) ):
    status = False
  else:
    if ( ( overwrite == True ) and ( file_exists( dest_path ) ) ):
      status = remove_file( dest_path )
  if ( status == True ):
    try:
      copy( src_path, dest_path )
    except:
      status = False
  return status

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

def make_directory( dir_path ):
  try:
    mkdir( dir_path )
  except OSError:
    raise error( 'creation of directory %s failed' % ( dir_path ) )
  return

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

def remove_directory( dir_path ):
  try:
    rmdir( dir_path )
  except OSError:
    raise error( 'removal of directory %s failed' % ( dir_path ) )
  return

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


# -*- encoding: utf-8 -*-
from django.forms import *
from django import forms
from .models import *

class Tarea_form(forms.ModelForm):
	class Meta:
		model = Tarea
		fields = '__all__'
		exclude = ('proyecto', 'user')

	def __init__(self, *args, **kwargs):
		super(Tarea_form, self).__init__(*args, **kwargs)
		for name, field in self.fields.iteritems():
			field.widget.attrs['class'] = 'form-control'
			field.widget.attrs['required'] = 'required'


from collections import namedtuple
from zipfile import ZipFile
import tempfile
import tarfile
import shutil
import uuid
import json
import csv
import os
import logging

from pbcommand.models.report import Report
from pbcommand.pb_io.report import load_report_from_json

import pbcoretools.chunking.gather as G

from base import get_temp_file, get_temp_dir

log = logging.getLogger(__name__)

class Record:
    def __init__(self, idx, alpha):
        self.idx = idx
        self.alpha = alpha

    def to_dict(self):
        return dict(id=self.idx, alpha=self.alpha)

def _to_n_records(nrecords):
    for i in range(nrecords):
        r = Record(i, 90)
        yield r

def _write_records_to_csv(records, output_csv):
    fields = records[0].to_dict().keys()
    with open(output_csv, 'w') as w:
        writer = csv.DictWriter(w, fieldnames=fields)
        writer.writeheader()
        writer.writerows([r.to_dict() for r in records])

class TestCsvGather:

    def test_smoke(self):
        t = get_temp_file(suffix="-records-1.csv")
        _write_records_to_csv(list(_to_n_records(100)), t)

        t2 = get_temp_file(suffix="-records-2.csv")
        _write_records_to_csv(list(_to_n_records(57)), t2)

        tg = get_temp_file(suffix="records-gather.csv")
        G.gather_csv([t, t2], tg)

        nrecords = 0
        with open(tg, 'r') as r:
            reader = csv.DictReader(r)
            log.debug(reader.fieldnames)
            for _ in reader:
                nrecords += 1

        assert nrecords == 157

def _write_stats_to_json(stats, output_json):
    with open(output_json, 'w') as w:
        w.write(Report.from_simple_dict(
            report_id="pbcommand_test",
            raw_d=stats,
            namespace="pb").to_json())

class TestJsonGather:

    def test_smoke(self):
        t = get_temp_file(suffix="-stats-1.json")
        _write_stats_to_json({'n_reads': 549, 'n_zmws': 100}, t)
        t2 = get_temp_file(suffix="-stats-2.json")
        _write_stats_to_json({'n_reads': 733, 'n_zmws': 100}, t2)

        tg = get_temp_file(suffix="stats-gather.json")
        G.gather_report([t, t2], tg)

        r = load_report_from_json(tg)
        stats = {a.id: a.value for a in r.attributes}
        assert stats['pb_n_reads'] == 549+733
        assert stats['pb_n_zmws'] == 200

def _mkjson():
    uuid_ = str(uuid.uuid4())
    json_file = "{u}.json".format(u=uuid_)
    with open(json_file, "w") as json_out:
        json_out.write('{"uuid":"%s"}' % uuid_)
    return json_file

def create_tarball(file_name, n=2):
    with tarfile.open(file_name, mode="w:gz") as tgz_out:
        for i in range(n):
            json_file = _mkjson()
            tgz_out.add(json_file)
            os.remove(json_file)
    return file_name

class TestTgzGather:

    def test_gather_tgz(self):
        tmp_dir = tempfile.mkdtemp()
        base_dir = os.getcwd()
        try:
            os.chdir(tmp_dir)
            tgz_files = [
                create_tarball(get_temp_file(suffix="-1.tgz")),
                create_tarball(get_temp_file(suffix="-2.tgz"))
            ]
            tgz_out = get_temp_file(suffix="-0.tgz")
            G.gather_tgz(tgz_files, tgz_out)
            uuids = set()
            with tarfile.open(tgz_out, mode="r:gz") as gathered:
                for member in gathered.getmembers():
                    d = json.loads(gathered.extractfile(member.name).read())
                    uuids.add(d["uuid"])
            assert len(uuids) == 4
        finally:
            os.chdir(base_dir)
            shutil.rmtree(tmp_dir)

def create_zip(file_name, n=2):
    with ZipFile(file_name, "w") as zip_out:
        for i in range(n):
            json_file = _mkjson()
            zip_out.write(json_file)
            os.remove(json_file)
    return file_name

class TestZipGather:

    def test_gather_zip(self):
        tmp_dir = tempfile.mkdtemp()
        base_dir = os.getcwd()
        try:
            os.chdir(tmp_dir)
            zip_files = [
                create_zip(get_temp_file(suffix="-1.zip")),
                create_zip(get_temp_file(suffix="-2.zip"))
            ]
            zip_out = get_temp_file(suffix="-0.zip")
            G.gather_zip(zip_files, zip_out)
            uuids = set()
            with ZipFile(zip_out, "r") as gathered:
                for member in gathered.namelist():
                    d = json.loads(gathered.open(member).read())
                    uuids.add(d["uuid"])
            assert len(uuids) == 4
        finally:
            os.chdir(base_dir)
            shutil.rmtree(tmp_dir)


def neighbour(list_a,a):
    list_b=[]
    for i in range(a):
        if i==0:
            list_b.append(list_a[i]+list_a[i+1])
        elif i==a-1:
            list_b.append(list_a[i]+list_a[i-1])
        else:
            list_b.append(list_a[i]+list_a[i-1]+list_a[i+1])
        print("added element",i,"which is",list_b[i])
    return list_b

num = input("How many numbers do you want to enter:")
num = int(num)
list_a=[]
print("Enter",num,"numbers")
for i in range(num):
    list_a.append(int(input()))
list_b=neighbour(list_a,num)

print("The neighbours of the numbers are:")
for i in list_b:
    print(i)


# -*- coding: utf-8 -*-
"""
Created on Mon May 23 01:51:06 2016

@author: Аманжол

Bayes_SIS
"""

import os
import csv
import math

manTrainingSet={}
womanTrainingSet={}
manTestSet={}

manTrainingSetCount=0
womanTrainingSetCount=0
manTrainingSetUnique=0
womanTrainingSetUnique=0
manTestSetUnique=0

file=""

for file in os.listdir('C:\\Users\\PC-2\\Desktop\\Documents\\KBTU\\2 semester\\Machine Learning\\Project\\men'):

	if file.endswith(".txt"):

		file="men/"+file
		with open(file, 'r') as csvfile:

			lines=csv.reader(csvfile, delimiter=' ')
			dataset=list(lines)
			dataset=dataset.pop()
			del dataset[-1]
			for i in range(len(dataset)):

				womanTrainingSetCount+=1
				if dataset[i] in womanTrainingSet:
					womanTrainingSet[dataset[i]]+=1
				else:
					womanTrainingSetUnique+=1
					womanTrainingSet[dataset[i]]=1

for file in os.listdir('C:\\Users\\PC-2\\Desktop\\Documents\\KBTU\\2 semester\\Machine Learning\\Project\\women'):

	if file.endswith(".txt"):

		file="women/"+file
		with open(file, 'r') as csvfile:

			lines=csv.reader(csvfile, delimiter=' ')
			dataset=list(lines)
			dataset=dataset.pop()
			del dataset[-1]
			for i in range(len(dataset)):

				manTrainingSetCount+=1
				if dataset[i] in manTrainingSet:
					manTrainingSet[dataset[i]]+=1
				else:
					manTrainingSetUnique+=1
					manTrainingSet[dataset[i]]=1

arr = []
ans = ["Perviy", "Vtoroy", "Tretiy", "Chetvertiy", "Pyatiy", "Shestoy", "Sedmoy", "Vosmoy", "Devyatiy", "Desyatiy"]

with open('questions.txt') as input_file:

    for line in input_file:

        print(line.strip('\r\n, *'))

        if(line.startswith("*")):

            str1 = input()
            print(" ")

            if(str1 == "1"):
                arr.append("Y ")
            elif(str1 == "2"):
                arr.append("N ")
            else:
                print("Error")
                break

stroka = ""
for i in range(10):

    stroka = stroka + ans[i] + arr[i]

file = open("test.txt", "w")
file.write(stroka)
file.close()

file="test.txt"
with open(file, 'r') as csvfile:

	lines=csv.reader(csvfile, delimiter=' ')
	dataset=list(lines)
	dataset=dataset.pop()
	del dataset[-1]

	for i in range(len(dataset)):

		if dataset[i] in manTestSet:
			manTestSet[dataset[i]]+=1
		else:
			manTestSetUnique+=1
			manTestSet[dataset[i]]=1

for key in womanTrainingSet.keys():
	womanTrainingSet[key]=(1+womanTrainingSet[key])/(womanTrainingSetCount+manTestSetUnique)

for key in manTrainingSet.keys():
    manTrainingSet[key]=(1+manTrainingSet[key])/(manTrainingSetCount+manTestSetUnique)

probabilityMan=math.log(0.5)
probabilityWoman=math.log(0.5)

for key in manTestSet.keys():

	if key in womanTrainingSet.keys():
		probabilityWoman+=math.log(math.pow(womanTrainingSet[key], manTestSet[key]))
	elif key not in womanTrainingSet.keys():
		probabilityWoman+=math.log(1/(womanTrainingSetCount+manTestSetUnique))

	if key in manTrainingSet.keys():
		probabilityMan+=math.log(math.pow(manTrainingSet[key], manTestSet[key]))
	elif key not in manTrainingSet.keys():
		probabilityMan+=math.log(1/(manTrainingSetCount+manTestSetUnique))

print(" ")

probMan = (probabilityMan/(probabilityWoman+probabilityMan))*100
probWoman = (probabilityWoman/(probabilityWoman+probabilityMan))*100

print("Man: ", probMan, "%")
print("Woman: ", probWoman, "%")

print(" ")

if(probMan>probWoman):

    print("I think you're a man")

else:

    print("I think you're a woman")


# If we list all the natural numbers below 10 that are multiples of 3 or 5,
# we get 3, 5, 6 and 9. The sum of these multiples is 23.
# Find the sum of all the multiples of 3 or 5 below 1000.

def problem_1(number):
    var_for_sum = 0
    for num in range(number):
        if num % 3 == 0:
            var_for_sum += num
        elif num % 5 == 0:
            var_for_sum += num
    return var_for_sum
print(problem_1(1000))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
sptensor.py contains the SP_TENSOR class which is the object representation
of the sparse tensor X in COO format.

References
========================================
[1] General software, latest release: Brett W. Bader, Tamara G. Kolda and others, Tensor Toolbox for MATLAB, Version 3.2.1, www.tensortoolbox.org, April 5, 2021.\n
[2] Dense tensors: B. W. Bader and T. G. Kolda, Algorithm 862: MATLAB Tensor Classes for Fast Algorithm Prototyping, ACM Trans. Mathematical Software, 32(4):635-653, 2006, http://dx.doi.org/10.1145/1186785.1186794.\n
[3] Sparse, Kruskal, and Tucker tensors: B. W. Bader and T. G. Kolda, Efficient MATLAB Computations with Sparse and Factored Tensors, SIAM J. Scientific Computing, 30(1):205-231, 2007, http://dx.doi.org/10.1137/060676489.\n
[4] Chi, E.C. and Kolda, T.G., 2012. On tensors, sparsity, and nonnegative factorizations. SIAM Journal on Matrix Analysis and Applications, 33(4), pp.1272-1299.

@author: Maksim Ekin Eren
"""
import sys
import numpy as np

class SP_TENSOR():

    def __init__(self, Coords, Values):
        """
        Initilize the SP_TENSOR class.\n
        Sorts the tensor entries.

        Parameters
        ----------
        Coords : Numpy array (i.e. array that is a list of list)
            Array of non-zero coordinates for sparse tensor X. COO format.\n
            Each entry in this array is a coordinate of a non-zero value in the tensor.\n
            Used when Type = 'sptensor' and tensor parameter is not passed.\n
            len(Coords) is number of total entiries in X, and len(coords[0]) should give the number of dimensions.
        Values : Numpy array (i.e. list of non-zero values corresponding to each list of non-zero coordinates)
            Array of non-zero tensor entries. COO format.\n
            Used when Type = 'sptensor' and tensor parameter is not passed.\n
            Length of values must match the length of coords.

        """

        self.Size = list()
        self.Dimensions = Coords.shape[1]
        self.Coords, self.Values = self.__sort_coords(Coords, Values)
        self.Type = 'sptensor'

        for d in range(self.Dimensions):
            self.Size.append(np.amax(self.Coords[:, d]) + 1)

    def ttv(self, vecs):
        """
        Tensor times vector for KRUSKAL tensor M.

        Parameters
        ----------
        vecs : array
            coluumn vector.

        Returns
        -------
        c : array
             product of KRUSKAL tensor X with a (column) vector vecs.

        """

        dims = np.arange(self.Dimensions)
        vidx = np.arange(self.Dimensions)

        remdims = np.setdiff1d(dims, vidx)

        for d in range(self.Dimensions):
            if vecs[str(vidx[d])].shape != self.Size[d]:
                sys.exit('Multiplicand is wrong size')

        newvals = self.Values
        subs = self.Coords

        if len(subs) == 0:
            newsubs = []

        for d in range(self.Dimensions):
            idx = self.Coords[:, d]
            w = vecs[str(vidx[d])]
            bigw = w[idx]
            newvals = np.multiply(newvals, bigw)

        newsubs = subs[:, remdims]

        if len(remdims) == 0:
            c = np.sum(newvals)
            return c

        print("TODO")
        sys.exit("Reached to a location that has not been imlemented yet.")
        return -1

    def __sort_coords(self, Coords, Values):
        """
        Helper function to sort the COO representation of the tensor.

        Parameters
        ----------
        Coords : array
            Coordinates of non-zero values.
        Values : array
            List of values for each coordinate.

        Returns
        -------
        Coords : array
            Sorted coordinates of non-zero values..
        Values : array
            Sorted list of values for each coordinate..

        """

        for d in range(self.Dimensions, 0, -1):
            if d == self.Dimensions:
                sort_indices = Coords[:, d - 1].argsort()

            else:
                sort_indices = Coords[:, d - 1].argsort(kind='mergesort')

            Coords = Coords[sort_indices]
            Values = Values[sort_indices]

        return Coords, Values


# -*- coding: utf-8 -*-
"""
Created on Thu Oct 12 15:34:50 2017

@author: zqx
"""
import jieba

with open('F:\\python2.7\data.txt','r') as f:
    for line in f:
        seg = jieba.cut(line.strip(), cut_all = False)
        s= ' '.join(seg)
        m=list(s)
        with open('data2result.txt','a+')as f:
            for word in m:
                f.write(word.encode('utf-8'))
                #print word


"""Plot analysis."""
from operator import xor

from resdk.resources.utils import (
    get_data_id, get_resolwe, get_samples, is_collection, is_data, is_relation,
)

__all__ = ('bamplot', )

def bamplot(resource, genome, input_gff=None, input_region=None, stretch_input=None, color=None,
            sense=None, extension=None, rpm=None, yscale=None, names=None, plot=None, title=None,
            scale=None, bed=None, multi_page=None):
    """Run ``bamplot`` on the resource.

    This method runs `bamplot`_ with bams, genome and gff or region
    specified in arguments.

    .. _bamplot:
        http://resolwe-bio.readthedocs.io/en/latest/catalog-definitions.html#process-bamplot

    :param list resource: resource from which bam objects will be get
    :param str genome: Genome used in the process (options are HG18,
        HG19, MM9 and MM10)
    :param input_gff: id of annotation file is given
    :type input_gff: int or `~resdk.resources.data.Data`
    :param str input_region: enter a genomic region
    :param int stretch_input: stretch the input regions to a minimum
        length
    :param str color: enter a colon separated list of colors
    :param str sense: map to forward, reverse or'both strand,
        default maps to ``both``
    :param int extension: extends reads by n bp, dfault value is 200bp
    :param bool rpm: normalizes density to reads per million (rpm),
        default is ``False``
    :param str yscale: choose either relative or uniform y axis scaling,
        default is ``relative scaling``
    :param str names: a comma separated list of names for your bams
    :param str plot: choose all lines on a single plot or multiple plots
    :param str title: title for the output plot(s), default will be the
        coordinate region
    :param str scale: a comma separated list of multiplicative scaling
        factors for your bams, default is ``None``
    :param list beds: subset of bed files to run process on, if empty
        processes for all bed files will be run
    :param bool multi_page: if flagged will create a new pdf for each
        region

    """
    input_objects = []

    if not input_gff and not input_region:
        raise KeyError('Please specify `input_gff` or `input_region.')
    if input_gff and input_region:
        raise KeyError('Please specify `input_gff` or `input_region.')

    valid_genomes = ['HG18', 'HG19', 'MM8', 'MM9', 'MM10', 'RN4', 'RN6']
    if genome not in valid_genomes:
        raise KeyError('Invalid `genome`, please use one of the following: '
                       '{}'. format(', '.join(valid_genomes)))

    bams = [sample.get_bam() for sample in get_samples(resource)]
    input_objects.extend(bams)
    bams = [get_data_id(bam) for bam in bams]

    inputs = {
        'genome': genome,
        'bam': bams,
    }

    if color is not None:
        inputs['color'] = color

    if sense is not None:
        inputs['scale'] = scale

    if extension is not None:
        inputs['extension'] = extension

    if rpm is not None:
        inputs['rpm'] = rpm

    if yscale is not None:
        inputs['yscale'] = yscale

    if names is not None:
        inputs['names'] = names

    if plot is not None:
        inputs['plot'] = plot

    if title is not None:
        inputs['title'] = title

    if scale is not None:
        inputs['scale'] = scale

    if multi_page is not None:
        inputs['multi_page'] = multi_page

    if input_gff is not None:
        input_objects.append(input_gff)
        inputs['input_gff'] = get_data_id(input_gff)

    if input_region is not None:
        inputs['input_region'] = input_region

    if bed is not None:
        if isinstance(bed, list):
            input_objects.extend(bed)
            inputs['bed'] = [get_data_id(bed_obj) for bed_obj in bed]
        else:
            input_objects.append(bed)
            inputs['bed'] = [get_data_id(bed)]

    resolwe = get_resolwe(*input_objects)

    bamplot_obj = resolwe.get_or_run(slug='bamplot', input=inputs)

    if is_collection(resource):
        resource.add_data(bamplot_obj)
    elif is_relation(resource):
        resource.collection.add_data(bamplot_obj)

    return bamplot_obj

def bamliquidator(resource, cell_type=None, bin_size=None, regions=None, extension=None,
                  sense=None, skip_plot=None, black_list=None, threads=None):
    """Run ``bamliquidator`` on the resource.

    This method runs `bamliquidator`_ with bams, where three different
    analysis type options are possible: Bin mode, Region mode and BED
    mode.

    .. _bamliquidator:
        http://resolwe-bio.readthedocs.io/en/latest/catalog-definitions.html#process-bamliquidator

    :param list resource: resource from which bam objects will be get
    :param str cell_type: the name of cell type will be given in counts
        tables
    :param int bin_size: number of base pairs in each bin. Default is
        100000.
    :param regions: gtf or bed annotation object used in region mode
    :type regions: `~resdk.resources.data.Data`
    :param int extension: Extends reads by number of bp. Default is 200.
    :param str sense: Mapping strand to gff file. Use '+' for forwaed,
        '-' for reverse and '.' for both. Defoult is both.
    :param bool skip_plot: True for skip plot.
    :param list str black_list: One or more chromosome patterns to skip
        during bin liquidation. Default is to skip any chromosomes that
        contain any of the following substrings `chrUn`, `_random`,
        `Zv9_` or `_hap`.
    :param int threads: Number of CPUs

    """
    if not xor(bin_size, regions):
        raise KeyError('Exactly one of `bin_size` and `regions` parameters must be given.')

    if regions and not is_data(regions):
        raise KeyError('`regions` parameter must be data object.')

    input_objects = []

    bams = [sample.get_bam() for sample in get_samples(resource)]
    input_objects.extend(bams)
    bams = [get_data_id(bam) for bam in bams]

    inputs = {
        'bam': bams,
    }

    if bin_size:
        inputs['analysis_type'] = 'bin'
        inputs['bin_size'] = bin_size
    else:  # regions
        if regions.process_type == 'data:annotation:gtf:':
            inputs['analysis_type'] = 'gtf'
        elif regions.process_type == 'data:bed:':
            inputs['analysis_type'] = 'bed'
        else:
            raise KeyError(
                '`regions` object must be of type `data:annotation:gtf:` or `data:bed:`'
            )

        input_objects.append(regions)
        inputs['regions_file_gtf'] = get_data_id(regions)

    if cell_type is not None:
        inputs['cell_type'] = cell_type

    if extension is not None:
        inputs['extension'] = extension

    if sense is not None:
        inputs['sense'] = sense

    if skip_plot is not None:
        inputs['skip_plot'] = skip_plot

    if black_list is not None:
        inputs['black_list'] = black_list

    if threads is not None:
        inputs['threads'] = threads

    resolwe = get_resolwe(*input_objects)

    bamliquidator_obj = resolwe.get_or_run(slug='bamliquidator', input=inputs)

    if is_collection(resource):
        resource.add_data(bamliquidator_obj)
    elif is_relation(resource):
        resource.collection.add_data(bamliquidator_obj)

    return bamliquidator_obj


# -*-coding: utf-8 -*-
# Python 3.6
# Author:Zhang Haitao
# Email:13163385579@163.com
# TIME:2018-11-23  16:02
# NAME:FT_hp-attribution.py
import os
import pandas as pd

directory=r'G:\FT_Users\HTZhang\FT\multiFactors_pit\backtest_mixed_signal'

# ret=pd.read_pickle(os.path.join(directory,'750_iw2_cw2_5_criteria3_150_3','ret.pkl'))
#
# for mode in range(3,7):
ret=pd.read_pickle(os.path.join(r'G:\FT_Users\HTZhang\FT\tmp\test','new','ret.pkl'))
factor=pd.read_csv(r'E:\FT_Users\HTZhang\tmp\factor_return_inc.csv',sep='\t')

factor=factor.set_index('trd_dt')
factor.index=pd.to_datetime(factor.index.astype(str))
factor['new_size']=factor['nl_size']+factor['size']
cols=['beta','growth','leverage','liquidity','momentum','profit','reversal','new_size','value']
factor=factor[cols]

# factor=factor.astype(float)

ret.name='ret'
import numpy as np

comb=pd.concat([ret,factor],axis=1,sort=True).dropna(axis=0)
comb=comb['2009':]
comb=comb.replace(r'\N',np.nan)

comb['const']=1

import statsmodels.api as sm
comb=comb.astype(float)

r=sm.OLS(comb['ret'],comb[['const']+cols]).fit()

print(r.tvalues['new_size'])
#
# import pickle
# result=pickle.load(open(os.path.join(r'G:\FT_Users\HTZhang\FT\tmp\test',str(mode),'result.pkl'),'rb'))
# print(mode,result['hedged_perf_yearly'].loc[['2017','2018'],'年度收益'].values)
#

    # print(mode,r.tvalues['size'],r.tvalues['nl_size'])

    # print(r.summary())


import sys
input = lambda : sys.stdin.readline().rstrip()
a, b = map(int, input().split())

arr = [0]
cnt = 1
for i in range(1, (b+1)// 2 + 1):
    for j in range(cnt):
        arr.append(cnt)
    cnt += 1

for i in range(1, b+1):
    arr[i] += arr[i-1]

print(arr[b]- arr[a-1])