import mock
import os
import shutil
import tempfile
import unittest

from ngi_pipeline.engines.sarek.database import CharonConnector
from ngi_pipeline.engines.sarek.exceptions import BestPracticeAnalysisNotRecognized, SampleNotValidForAnalysisError
from ngi_pipeline.engines.sarek.models.sample import SarekAnalysisSample
from ngi_pipeline.engines.sarek.models.sarek import SarekAnalysis, SarekGermlineAnalysis
from ngi_pipeline.engines.sarek.parsers import ParserIntegrator
from ngi_pipeline.log.loggers import minimal_logger
from ngi_pipeline.tests.engines.sarek.test_launchers import TestLaunchers

@mock.patch("ngi_pipeline.engines.sarek.models.sarek.ReferenceGenome", autospec=True)
@mock.patch("ngi_pipeline.engines.sarek.database.CharonConnector", autospec=True)
class TestSarekAnalysis(unittest.TestCase):

    CONFIG = {}

    def setUp(self):
        self.log = minimal_logger(__name__, to_file=False, debug=True)
        self.config = TestSarekAnalysis.CONFIG
        self.analysis_obj = TestLaunchers.get_NGIAnalysis(log=self.log)

    def test_get_analysis_instance_for_project_germline(self, charon_connector_mock, reference_genome_mock):
        reference_genome_mock.get_instance.return_value = "this-is-a-reference-genome"
        charon_connector = charon_connector_mock.return_value
        charon_connector.best_practice_analysis.return_value = "something_germline_something"
        expected_analysis_class = SarekGermlineAnalysis
        observed_analysis_instance = SarekAnalysis.get_analysis_instance_for_project(
            "this-is-a-project-id",
            self.config,
            self.log,
            charon_connector=charon_connector)
        self.assertIsInstance(observed_analysis_instance, expected_analysis_class)

    def test_get_analysis_instance_for_project_somatic(self, charon_connector_mock, reference_genome_mock):
        reference_genome_mock.get_instance.return_value = "this-is-a-reference-genome"
        charon_connector = charon_connector_mock.return_value
        charon_connector.best_practice_analysis.return_value = "something_somatic_something"
        with self.assertRaises(NotImplementedError):
            SarekAnalysis.get_analysis_instance_for_project(
                "this-is-a-project-id",
                self.config,
                self.log,
                charon_connector=charon_connector)

    def test_get_analysis_instance_for_project_unknown(self, charon_connector_mock, reference_genome_mock):
        reference_genome_mock.get_instance.return_value = "this-is-a-reference-genome"
        charon_connector = charon_connector_mock.return_value
        charon_connector.best_practice_analysis.return_value = "something_unknown_something"
        with self.assertRaises(BestPracticeAnalysisNotRecognized):
            SarekAnalysis.get_analysis_instance_for_project(
                "this-is-a-project-id",
                self.config,
                self.log,
                charon_connector=charon_connector)

    def test_status_should_be_started(self, charon_connector_mock, reference_genome_mock):

        def _analysis_status_wrapper(status, *args):
            return CharonConnector(
                self.config, self.log, charon_session="charon-session").analysis_status_from_process_status(status)

        def _alignment_status_wrapper(status, *args):
            return CharonConnector(
                self.config, self.log, charon_session="charon-session").alignment_status_from_analysis_status(status)

        charon_connector = charon_connector_mock.return_value
        charon_connector.analysis_status_from_process_status.side_effect = _analysis_status_wrapper
        charon_connector.alignment_status_from_analysis_status.side_effect = _alignment_status_wrapper
        sarek_analysis = SarekAnalysis(
            reference_genome_mock.return_value, self.config, self.log, charon_connector=charon_connector)

        for restart_mode in True, False:
            self.assertEqual(
                restart_mode,
                sarek_analysis.status_should_be_started(
                    "FAILED",
                    restart_failed_jobs=restart_mode))

        for restart_mode in True, False:
            self.assertEqual(
                restart_mode,
                sarek_analysis.status_should_be_started(
                    "ANALYZED",
                    restart_finished_jobs=restart_mode))

        for restart_mode in True, False:
            self.assertEqual(
                restart_mode,
                sarek_analysis.status_should_be_started(
                    "UNDER_ANALYSIS",
                    restart_running_jobs=restart_mode))

        self.assertTrue(
            sarek_analysis.status_should_be_started("TO_BE_ANALYZED"))

    def test_configure_analysis(self, charon_connector_mock, reference_genome_mock):
        config = {
            "profile": "this-is-a-profile",
            "tools": ["tool-A", "tool-B"]}

        sarek_analysis = SarekAnalysis(
            reference_genome_mock.return_value,
            {"sarek": config},
            self.log,
            charon_connector=charon_connector_mock.return_value)

        for key in config.keys():
            self.assertEqual(config[key], sarek_analysis.sarek_config[key])
        self.assertEqual(SarekAnalysis.DEFAULT_CONFIG["nf_path"], sarek_analysis.sarek_config["nf_path"])
        self.assertEqual(SarekAnalysis.DEFAULT_CONFIG["sarek_path"], sarek_analysis.sarek_config["sarek_path"])

        config = {
            "config": "this-is-a-site-specific-config-file",
            "profile": "this-is-another-profile",
            "nf_path": "this-is-the-path-to-nextflow",
            "sarek_path": "this-is-the-path-to-sarek"}
        sarek_config = sarek_analysis.configure_analysis(config={"sarek": config})
        for key in config.keys():
            self.assertEqual(config[key], sarek_config[key])
        self.assertEqual(SarekAnalysis.DEFAULT_CONFIG["tools"], sarek_config["tools"])

    def test_command_line(self, charon_connector_mock, reference_genome_mock):

        sarek_analysis = SarekAnalysis(
            reference_genome_mock.return_value,
            self.config,
            self.log,
            charon_connector=charon_connector_mock.return_value)

        with self.assertRaises(NotImplementedError):
            sarek_analysis.command_line(None)

    def test_generate_tsv_file_contents(self, charon_connector_mock, reference_genome_mock):

        sarek_analysis = SarekAnalysis(
            reference_genome_mock.return_value,
            self.config,
            self.log,
            charon_connector=charon_connector_mock.return_value)

        with self.assertRaises(NotImplementedError):
            sarek_analysis.generate_tsv_file_contents(None)

    def test_create_tsv_file(self, charon_connector_mock, reference_genome_mock):
        sarek_analysis = SarekAnalysis(
            reference_genome_mock.return_value,
            self.config,
            self.log,
            charon_connector=charon_connector_mock.return_value)
        sample_obj = self.analysis_obj.project.samples.values()[0]
        analysis_sample = SarekAnalysisSample(self.analysis_obj.project, sample_obj, sarek_analysis)
        with mock.patch.object(sarek_analysis, "generate_tsv_file_contents") as tsv_mock, \
                mock.patch.object(analysis_sample, "sample_analysis_tsv_file") as tsv_path_mock:
            tsv_mock.return_value = []
            with self.assertRaises(SampleNotValidForAnalysisError) as e:
                sarek_analysis.create_tsv_file(analysis_sample)
            tsv_mock.return_value = [["this", "is"], ["some", "content"]]
            tempdir = tempfile.mkdtemp(prefix="test_create_tsv_file_")
            tsv_path_mock.return_value = os.path.join(tempdir, "tsv_parent_folder", "tsv_file.tsv")
            sarek_analysis.create_tsv_file(analysis_sample)
            self.assertTrue(os.path.exists(tsv_path_mock.return_value))
            shutil.rmtree(tempdir)

@mock.patch("ngi_pipeline.engines.sarek.models.resources.ReferenceGenome", autospec=True)
@mock.patch("ngi_pipeline.engines.sarek.database.CharonConnector", autospec=True)
@mock.patch("ngi_pipeline.engines.sarek.database.TrackingConnector", autospec=True)
@mock.patch("ngi_pipeline.engines.sarek.process.ProcessConnector", autospec=True)
class TestSarekGermlineAnalysis(unittest.TestCase):

    CONFIG = {
        "profile": "this-is-a-profile",
        "tools": ["haplotypecaller", "manta"],
        "nf_path": os.path.join("/this", "is", "the", "nextflow", "path"),
        "sarek_path": os.path.join("/this", "is", "the", "path", "to", "sarek"),
        "genome": "this-is-the-genome"}

    def setUp(self):
        self.log = minimal_logger(__name__, to_file=False, debug=True)
        self.config = TestSarekGermlineAnalysis.CONFIG
        self.analysis_obj = TestLaunchers.get_NGIAnalysis(log=self.log)

    def get_instance(
            self, process_connector_mock, tracking_connector_mock, charon_connector_mock, reference_genome_mock):
        reference_genome = reference_genome_mock.return_value
        reference_genome.__str__.return_value = self.config["genome"]
        return SarekGermlineAnalysis(
            reference_genome,
            {"sarek": self.config},
            self.log,
            charon_connector=charon_connector_mock,
            tracking_connector=tracking_connector_mock,
            process_connector=process_connector_mock)

    def test_cleanup(self, *mocks):
        sarek_analysis = self.get_instance(*mocks)
        with mock.patch("ngi_pipeline.engines.sarek.models.sample.SarekAnalysisSample", autospec=True) as sample_mock:
            expected_work_dir = "path/to/work/dir"
            sample_mock.sample_analysis_work_dir.return_value = expected_work_dir
            sarek_analysis.cleanup(sample_mock)
            sarek_analysis.process_connector.cleanup.assert_called_once_with(expected_work_dir)

    def test_command_line(
            self, process_connector_mock, tracking_connector_mock, charon_connector_mock, reference_genome_mock):
        sarek_analysis = self.get_instance(
            process_connector_mock, tracking_connector_mock, charon_connector_mock, reference_genome_mock)
        sample_obj = self.analysis_obj.project.samples.values()[0]
        analysis_sample = SarekAnalysisSample(self.analysis_obj.project, sample_obj, sarek_analysis)
        self.config["outDir"] = analysis_sample.sample_analysis_path()
        self.config["sample"] = analysis_sample.sample_analysis_tsv_file()
        observed_cmd = sarek_analysis.command_line(analysis_sample)

        self.assertIn("-profile {}".format(self.config["profile"]), observed_cmd)
        self.assertIn("--tools {}".format(",".join(self.config["tools"])), observed_cmd)
        self.assertTrue(observed_cmd.startswith("{} run {}".format(self.config["nf_path"], self.config["sarek_path"])))
        for key in filter(lambda k: k not in ["profile", "nf_path", "sarek_path", "tools"], self.config.keys()):
            self.assertIn("--{} {}".format(key, self.config[key]), observed_cmd)

    def test_generate_tsv_file_contents(
            self, process_connector_mock, tracking_connector_mock, charon_connector_mock, reference_genome_mock):
        sarek_analysis = self.get_instance(
            process_connector_mock, tracking_connector_mock, charon_connector_mock, reference_genome_mock)
        sample_obj = self.analysis_obj.project.samples.values()[0]
        analysis_sample = SarekAnalysisSample(self.analysis_obj.project, sample_obj, sarek_analysis)
        sample_data_path = analysis_sample.sample_data_path()
        with mock.patch.object(
                sarek_analysis, "libprep_should_be_started") as libprep_mock, \
                mock.patch.object(
                    sarek_analysis, "seqrun_should_be_started") as seqrun_mock:
            for start_mode in True, False:
                libprep_mock.return_value = start_mode
                seqrun_mock.return_value = not start_mode
                self.assertListEqual(
                    [],
                    sarek_analysis.generate_tsv_file_contents(analysis_sample))
            libprep_mock.return_value = True
            seqrun_mock.return_value = True
            expected_tsv = []
            for libprepid in map(lambda lp: '{}-{}'.format(sample_obj.name, lp), ['libprep1', 'libprep2']):
                for sample_index in ['1', '2']:
                    tsv_row = [sample_obj.name, 'ZZ', 0, sample_obj.name, 'ABC00{0}CXY.1.S{0}'.format(sample_index)]
                    tsv_row.extend([
                        os.path.join(
                            sample_data_path,
                            libprepid,
                            '180411_ST-0123_001{0}_AABC00{0}CXY'.format(sample_index),
                            '{}_S{}_L001_R{}_001.fastq.gz'.format(
                                libprepid, sample_index, read_num)) for read_num in ['1', '2']])
                    expected_tsv.append(tsv_row)
            observed_tsv = sarek_analysis.generate_tsv_file_contents(analysis_sample)
            self.assertListEqual(sorted(expected_tsv), sorted(observed_tsv))

    def test_analyze_sample(
            self, process_connector_mock, tracking_connector_mock, charon_connector_mock, reference_genome_mock):
        sarek_analysis = self.get_instance(
            process_connector_mock, tracking_connector_mock, charon_connector_mock, reference_genome_mock)
        with mock.patch.object(
                sarek_analysis, "create_tsv_file", return_value=os.path.join("/path", "to", "tsv", "file")) as tsv_mock:
            for sample_obj in self.analysis_obj.project:
                sarek_analysis.analyze_sample(sample_obj, self.analysis_obj)

    def test_runid_and_fastq_files_from_tsv_file(self, *args):
        fh, fake_tsv_file = tempfile.mkstemp(prefix="test_fastq_files_from_tsv_")
        with mock.patch("ngi_pipeline.engines.sarek.models.sarek.csv.reader", autospec=True) as csv_mock:
            expected_fastq_files = []
            for row in range(3):
                expected_fastq_files.append(
                    ["id.for.row.{}".format(row)] + [
                        os.path.join("/path", "to", "fastq", "file_{}-R{}.fastq.gz".format(row, i)) for i in [1, 2]])
            for row in range(2):
                expected_fastq_files.append(
                    ["id.for.row.{}".format(row+3)] +
                    [os.path.join("/path", "to", "fastq", "file_{}-R1.fastq.gz".format(row))])
            mocked_tsv_data = (
                ["col1", "col2", "col3", "col4"] +
                runid_and_fastq_files for runid_and_fastq_files in expected_fastq_files)
            csv_mock.return_value = mocked_tsv_data
            observed_fastq_files = list(SarekGermlineAnalysis.runid_and_fastq_files_from_tsv_file(fake_tsv_file))
            self.assertListEqual(sorted(expected_fastq_files), sorted(observed_fastq_files))
        os.unlink(fake_tsv_file)

    def test_collect_analysis_metrics(
            self, process_connector_mock, tracking_connector_mock, charon_connector_mock, reference_genome_mock):
        sarek_analysis = self.get_instance(
            process_connector_mock, tracking_connector_mock, charon_connector_mock, reference_genome_mock)
        expected_metrics = {
            "percent_duplication": [25.3],
            "autosomal_coverage": [35.8],
            "total_reads": [123456789]
        }

        def _serve_metric(metric):
            return expected_metrics[metric[4:]]

        with mock.patch.object(
                sarek_analysis, "processing_steps") as processing_steps, \
                mock.patch(
                    "ngi_pipeline.engines.sarek.models.sarek.ParserIntegrator", autospec=ParserIntegrator) as \
                        parser_mock:
            processing_steps.return_value = []
            parser_instance = parser_mock.return_value
            query_mock = parser_instance.query_parsers
            query_mock.side_effect = _serve_metric
            observed_metrics = sarek_analysis.collect_analysis_metrics("this-is-a-sample-analysis")
            self.assertDictEqual({metric: value[0] for metric, value in expected_metrics.items()}, observed_metrics)


from collections import Counter

class InvalidScoresheetException(Exception):
    pass

class Scorer:
    EXPECTED_TOKEN_COUNTS = {
        'A': 4,
        'B': 4,
        'C': 1,
    }

    def __init__(self, teams_data, arena_data):
        self._teams_data = teams_data
        self._arena_data = arena_data

    def calculate_scores(self):
        scores = {}
        for tla, info in self._teams_data.items():
            zone = info['zone']
            tokens = self._arena_data[zone]['tokens']

            # 1 point per token
            points = len(tokens)

            counts = Counter(tokens)

            if 'A' in counts and 'B' in counts:
                # 1 further point per B token if there's an A token
                points += counts['B']

                # 2 further points per C token if there are both A and B tokens
                points += 2 * counts.get('C', 0)

            scores[tla] = points

        return scores

    def validate(self, extra):
        all_tokens = ''.join(
            d['tokens']
            for d in self._arena_data.values()
        ).replace(' ', '')

        counts = Counter(all_tokens)

        if counts != self.EXPECTED_TOKEN_COUNTS:
            msg = "Found invalid token counts {0!r} (expecting: {1!r})".format(
                dict(counts),
                self.EXPECTED_TOKEN_COUNTS,
            )
            raise InvalidScoresheetException(msg)

if __name__ == '__main__':
    import libproton
    libproton.main(Scorer)


import numpy as np

def train(inputs, weights, targets, rate, bias, theta):
    hArray = np.dot(inputs, weights)
    activations = np.where(hArray > 0, 1, 0)
    weights -= rate * np.dot(np.transpose(inputs), activations-targets)

def test(inputs, weights, targets, bias, theta):
    hArray = np.dot(inputs, weights)
    print(np.where(hArray > 0, 1, 0))

def main():
    # learn logical gates
    m = 2
    n = 1
    N = 4
    # inputs is a N x m array, where N = # input vectors, m = # input nodes
    inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
    # add bias (-1) to inputs, so it's N x m+1
    inputs = np.concatenate((inputs, -np.ones((N, 1))), axis=1)
    print("Shape of inputs: ", np.shape(inputs))

    # weights m+1 x n array, m = # input nodes, n = # neurons
    # radom initial weights in range -0.05 to 0.05
    weights = np.random.rand(m+1, n)*0.1-0.05
    print("Shape of weights: ", np.shape(weights))

    # targets is a N x n array, where N = # input vectors, n = # neurons
    targetsOR = np.array([[0], [1], [1], [1]])
    targetsAND = np.array([[0], [0], [0], [1]])
    targetsXOR = np.array([[0], [1], [1], [0]])
    targetsNOR = np.array([[1], [0], [0], [0]])
    targetsNAND = np.array([[1], [1], [1], [0]])
    targets = [targetsOR, targetsAND, targetsXOR, targetsNOR, targetsNAND]

    # Choose T, learning rate
    T = 15
    rate = 0.25
    bias = -1
    theta = 0

    # Train perceptron function
    for target in targets:
        print("Training: ")
        print(target)
        for i in range(T):
            train(inputs, weights, target, rate, bias, theta)

        # Recall
        test(inputs, weights, target, bias, theta)
        print()

main()


# Tic Tac Toe

import random
import time

def drawBoard(board):
    # This function prints out the board that it was passed.

    # "board" is a list of 10 strings representing the board (ignore index 0)
    print('   |   |')
    print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
    print('   |   |')
    print('-----------')
    print('   |   |')
    print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
    print('   |   |')
    print('-----------')
    print('   |   |')
    print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
    print('   |   |')

print('Welcome to Tic Tac Toe!')

while True:
    # Reset and create the board, an array of characters to represent each position
    # For simplicity we're going to ignore index 0
    theBoard = [' '] * 10
    # Set the game state to playing.
    gameIsPlaying = True
    # Draw the empty game board
    drawBoard(theBoard)
    #Player marker O or X
    playerMarker = ' '

    print('YOUR_NAME - Tic-Tac-Toe Project.')
    print('---------------------------------------------')

    playerMarker = input("Choose your marker X/O")
    if playerMarker != 'X' or 'x' or 'o' or 'O' :
        print("Invalid marker; X or O ")
        playerMarker = input("Choose your marker X/O")

    #Cast out inputs to the correct variable type remember!
    move = int(input('Choose a position (1 - 9)'))

    def CheckMove(pos):
        if theBoard[pos] != ' ':
            print("Can't go here!")
            return False
        else:
            return True

    def CheckWin(GameBoard):
        if GameBoard[7] == 'X' and GameBoard[8] == 'X' and GameBoard[9] == 'X' :#Check top row
            print("YOU WIN!")

        if GameBoard[4] == 'X' and GameBoard[5] == 'X' and GameBoard[6] == 'X' :#Check middle row
            print("YOU WIN!")

        if GameBoard[1] == 'X' and GameBoard[2] == 'X' and GameBoard[3] == 'X' :#Check bottom row
            print("YOU WIN!")

    while gameIsPlaying:
        # Game Logic here, this is our game loop.
        # Check to make sure the player inputs a valid number between 1- 9

        CheckWin(theBoard)

        if move < 1 or move > 9 :
            print('Value not allowed')
            drawBoard(theBoard)
            move = int(input('Choose a position (1-9)'))

        if move == 1 :
                if CheckMove(1) != True:
                    move = int(input('Choose a position (1-9)'))
                    drawBoard(theBoard)
                    CheckWin(theBoard)
                else:
                    theBoard[1] = playerMarker
                    drawBoard(theBoard)
                    move = int(input('Choose a position (1 - 9)'))
        if move == 2:
            theBoard[2] = playerMarker
            drawBoard(theBoard)
            CheckWin(theBoard)
            move = int(input('Choose a position (1-9)'))
        if move == 3:
            theBoard[3] = playerMarker
            drawBoard(theBoard)
            CheckWin(theBoard)
            move = int(input('Choose a position (1-9)'))
        if move == 4:
            theBoard[4] = playerMarker
            drawBoard(theBoard)
            CheckWin(theBoard)
            move = int(input('Choose a position (1-9)'))
        if move == 5:
            theBoard[5] = playerMarker
            drawBoard(theBoard)
            CheckWin(theBoard)
            move = int(input('Choose a position (1-9)'))
        if move == 6:
            theBoard[6] = playerMarker
            drawBoard(theBoard)
            CheckWin(theBoard)
            move = int(input('Choose a position (1-9)'))
        if move == 7:
            theBoard[7] = playerMarker
            drawBoard(theBoard)
            CheckWin(theBoard)
            move = int(input('Choose a position (1-9)'))
        if move == 8:
            theBoard[8] = playerMarker
            drawBoard(theBoard)
            CheckWin(theBoard)
            move = int(input('Choose a position (1-9)'))
        if move == 9:
            theBoard[9] = playerMarker
            drawBoard(theBoard)
            CheckWin(theBoard)
            move = int(input('Choose a position (1-9)'))


def is_prime(n):
	"""check if integer n is a prime"""
	# make sure n is a positive integer

	n = abs(int(n))  # 0 and 1 are not primes
	if n < 2:
		return False

	# 2 is the only even prime number
	if n == 2:
		return True

		# all other even numbers are not primes
	if not n & 1:
		return False

	# range starts with 3 and only needs to go up
	# the square root of n for all odd numbers
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False

	return True

def to_binary(n):
	s = ""
	while n > 0:
		if n % 2:
			s = "1" + s
		else:
			s = "0" + s
		n //= 2
	return s

def to_base(val, base):
	output = 0
	for c in val:
		output *= base
		if c == "1":
			output += 1
	return output

def get_divisor(val):
	for i in range(2, val - 1):
		if val % i == 0:
			return i

def test(val):
	if val[-1] == "0":
		return False
	is_ok = True
	for base in range(2, 11):
		val_in_base = to_base(val, base)
		# print(val, base, val_in_base)
		if is_prime(val_in_base):
			is_ok = False
	if is_ok:
		print(val, end="")
		for base in range(2, 11):
			print(" " + str(get_divisor(to_base(val, base))), end="")
		print()
		return True
	return False

def solve(n, j):
	val = 2**(n-1)
	found = 0
	while found < j:
		# print("test=" + to_binary(val))
		if test(to_binary(val)):
			found += 1
		val += 1

def main():
	num_of_test = int(input())

	for test_id in range(1, num_of_test + 1):
		n, j = (int(i) for i in input().split())
		print("Case #" + str(test_id) + ":")
		solve(n, j)

if __name__ == "__main__":
	main()


import os

from rostran.core.format import FileFormat
from rostran.core.settings import ROOT
from rostran.providers.cloudformation.template import CloudFormationTemplate

tpl = {
    "ROSTemplateFormatVersion": "2015-09-01",
    "Description": "This template create vpc security group.",
    "Metadata": {
        "ALIYUN::ROS::Interface": {
            "ParameterGroups": {
                "Parameters": ["CidrBlock"],
                "Label": {"default": "VPC"},
            },
            "ParameterLabels": {"CidrBlock": {"default": "Vpc Cidr Block"}},
        }
    },
    "Parameters": {
        "CidrBlock": {
            "Type": "String",
            "Default": "10.0.0.0/16",
            "Label": "Vpc Cidr Block",
        }
    },
    "Resources": {
        "MyVpc": {
            "Type": "ALIYUN::ECS::VPC",
            "Properties": {
                "CidrBlock": {"Ref": "CidrBlock"},
                "Tags": [{"Key": "foo", "Value": "bar"}],
            },
            "DeletionPolicy": "Retain",
        },
        "MySg": {
            "Type": "ALIYUN::ECS::SecurityGroup",
            "Properties": {
                "Description": "Create vpc security group",
                "VpcId": {"Ref": "MyVpc"},
            },
            "DependsOn": "MyVpc",
        },
    },
    "Outputs": {
        "VpcId": {"Value": {"Ref": "MyVpc"}},
        "SgId": {"Value": {"Fn::GetAtt": ["MySg", "SecurityGroupId"]}},
    },
}

def test_template():
    aws_template = os.path.join(ROOT, "templates", "cloudformation", "vpc_sg.json")

    template = CloudFormationTemplate.initialize(aws_template, FileFormat.Json)
    assert tpl == template.transform().as_dict()


#!/usr/bin/env python2
# screamtitles.py
# ---------------

# for @inky re: https://twitter.com/inky/status/475023831770595328

from __future__ import division

import sys
import codecs
import re
import random
import math

# how many subtitles should be replaced with...whatever
percentage = 0.1

# other non-screaming sound effects!
screams = ('[screaming]',)

class Subtitle:
    def __init__(self, raw_text):
        raw_lines = list(map(lambda x: x.strip(), raw_text.split('\n')))
        self.s_id = raw_lines[0]
        self.time = raw_lines[1]
        self.text = raw_lines[2:]

    def __str__(self):
        return '\n'.join((self.s_id, self.time, '\n'.join(self.text)))

    def __repr__(self):
        return 'Subtitle(' + self.__str__() + ')'

def parse_subtitle_list(raw_text):
    split_text = re.split('\r?\n\r?\n', raw_text)
    return list(map(lambda x: Subtitle(x), split_text))

if __name__ == '__main__':
    input_subs = sys.argv[1]

    with codecs.open(input_subs, encoding='utf-8') as f:
        raw_text = f.read().strip().replace('\ufeff', '')

    split_text = re.split('\r?\n\r?\n', raw_text)
    subs = [Subtitle(x) for x in split_text]

    replace_sub_ids = random.sample(range(len(subs)), int(math.floor(percentage * len(subs))))

    for s_id in replace_sub_ids:
        subs[s_id].text = [random.choice(screams)]

    with codecs.open(input_subs + '.bak', 'w', encoding='utf-8') as f:
        f.write(raw_text)

    with codecs.open(input_subs, 'w', encoding='utf-8') as f:
        for sub in subs:
            f.write(str(sub) + '\n\n')

    print('screamysubs in {0}, original subs saved to {0}.bak'.format(input_subs))


from copy import copy

from mio import runtime
from mio.utils import method
from mio.object import Object
from mio.errors import StopIteration

class Generator(Object):

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

        self.context = None
        self.message = None

        self.create_methods()
        self.parent = runtime.find("Object")

    @method("init")
    def init(self, receiver, context, m):
        receiver.context = copy(context["call"]["sender"])
        receiver.message = context["call"]["sender"]["call"]["message"]
        return receiver

    @method()
    def setNextMessage(self, receiver, context, m, message):
        receiver.message = message.eval(context).next
        return receiver

    @method("__next__")
    def getNext(self, receiver, context, m):
        if receiver.message is None:
            raise StopIteration()
        return receiver.message.eval(receiver.context)


import tensorflow as tf
from tensorflow.keras.preprocessing.sequence import pad_sequences
from gluonnlp.data import PadSequence

class Corpus():
    def __init__(self, vocab, tokenizer):
        self._vocab = vocab
        self._tokenizer = tokenizer

    def token2idx(self, item):
        label_dict = {"neutral": 0, "entailment": 1, "contradiction": 2, "-": 3}
        sen1, sen2, label = tf.io.decode_csv(item, record_defaults=[[""],[""],[""]], field_delim='\t')

        sen1 = [self._tokenizer.morphs(sen.numpy().decode('utf-8')) for sen in sen1]
        sen1 = [[self._vocab.token_to_idx[token] for token in sen] for sen in sen1]
        sen1 = pad_sequences(sen1, maxlen=70, value=0,
                             padding='post', truncating='post')

        sen1 = tf.convert_to_tensor(sen1, dtype=tf.int32)

        sen2 = [self._tokenizer.morphs(sen.numpy().decode('utf-8')) for sen in sen2]
        sen2 = [[self._vocab.token_to_idx[token] for token in sen] for sen in sen2]
        sen2 = pad_sequences(sen2, maxlen=70, value=0,
                             padding='post', truncating='post')
        sen2 = tf.convert_to_tensor(sen2, dtype=tf.int32)

        label = tf.convert_to_tensor([label_dict[l.numpy().decode('utf-8')] for l in label], dtype=tf.int32)
        label = tf.reshape(label, [1024,1])
        return sen1, sen2, label


import logging
import random
import numpy as np
import torch

def set_logger():
    logging.basicConfig(
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        level=logging.INFO
    )

def set_seed(seed):
    """for reproducibility

    :param seed:
    :return:
    """
    np.random.seed(seed)
    random.seed(seed)

    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.enabled = False
    torch.backends.cudnn.benchmark = False
    torch.backends.cudnn.deterministic = True

def get_device(no_cuda=False, gpus='0'):
    return torch.device("cuda:" + gpus if torch.cuda.is_available() and not no_cuda else "cpu")

def detach_to_numpy(tensor):
    return tensor.detach().cpu().numpy()


# Lint as: python3
#
# Copyright 2020 The XLS Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for xls.dslx.interpreter.parse_and_interpret."""

import io

import unittest.mock as mock
from pyfakefs import fake_filesystem_unittest as ffu

from xls.dslx.interpreter import parse_and_interpret
from absl.testing import absltest

class ParseAndInterpretTest(absltest.TestCase):

  def test_assertion_failure_prints_positionally(self):
    program = """
    #![test]
    fn foo_test() {
      assert_eq(false, true)
    }
    """
    mock_stderr = io.StringIO()
    filename = 'test_filename.x'
    with ffu.Patcher() as patcher:
      patcher.fs.CreateFile(filename, contents=program)
      with mock.patch('sys.stderr', mock_stderr):
        parse_and_interpret.parse_and_test(
            program, 'test_program', filename=filename, raise_on_error=False)
    self.assertIn('* 0004:       assert_eq(false, true)',
                  mock_stderr.getvalue())
    self.assertIn(
        '        ~~~~~~~~~~~~~~~^-----------^ The program being interpreted failed!',
        mock_stderr.getvalue())

if __name__ == '__main__':
  absltest.main()


id_list = ["muzi", "frodo", "apeach", "neo"]
report = ["muzi frodo",
          "apeach frodo",
          "frodo neo",
          "muzi neo",
          "apeach muzi"]
k = 2
result = [2,1,1,0]

id_list = ["con", "ryan"]
report = ["ryan con", "ryan con", "ryan con", "ryan con"]
k = 3
result = [0,0]

def make_report_cnt_dict(report_parsing: list) -> dict:
    report_cnt_dict = {i[1]: 0 for i in report_parsing}
    for i in report_parsing:
        report_cnt_dict[i[1]] += 1

    return report_cnt_dict

def solution(id_list, report, k):
    # 신고리스트 파싱
    report_parsing = [i.split(" ")for i in [i for i in list(set(report))]]
    # 신고 결과 회신 메일 수 dict
    mail_cnt = {i:0 for i in id_list}

    # 신고당한 아이디별 신고 개수
    report_cnt = make_report_cnt_dict(report_parsing)

    # 신고 결과 회신 메일 수 입력
    for i in report_cnt.keys():
        if report_cnt[i] >= k:
            for j in report_parsing:
                if i == j[1]:
                    mail_cnt[j[0]]+=1

    answer = [mail_cnt[i] for i in mail_cnt.keys()]

    return answer

result= solution(id_list, report, k)


import unittest
from unittest.mock import Mock

from pyats.topology import Device
from pyats.topology import loader
from genie.metaparser.util.exceptions import SchemaEmptyParserError
from genie.libs.parser.junos.show_lacp import ShowLacpInterfacesInterface

""" TestCase for:
        * show lacp interfaces {interface}
"""
class TestShowLacpInterfacesInterface(unittest.TestCase):

    device = Device(name="aDevice")

    maxDiff = None

    empty_output = {"execute.return_value": ""}

    golden_output = {
        "execute.return_value": """
        show lacp interfaces ae4
        Aggregated interface: ae4
            LACP state:       Role   Exp   Def  Dist  Col  Syn  Aggr  Timeout  Activity
            xe-3/0/1       Actor    No    No   Yes  Yes  Yes   Yes     Fast    Active
            xe-3/0/1     Partner    No    No   Yes  Yes  Yes   Yes     Fast    Active
            LACP protocol:        Receive State  Transmit State          Mux State
            xe-3/0/1                  Current   Fast periodic Collecting distributing
    """
    }

    golden_parsed_output = {
        "lacp-interface-information-list": {
            "lacp-interface-information": {
                "lag-lacp-header": {"aggregate-name": "ae4"},
                "lag-lacp-protocol": [
                    {
                        "lacp-mux-state": "Collecting distributing",
                        "lacp-receive-state": "Current",
                        "lacp-transmit-state": "Fast periodic",
                        "name": "xe-3/0/1",
                    }
                ],
                "lag-lacp-state": [
                    {
                        "lacp-activity": "Active",
                        "lacp-aggregation": "Yes",
                        "lacp-collecting": "Yes",
                        "lacp-defaulted": "No",
                        "lacp-distributing": "Yes",
                        "lacp-expired": "No",
                        "lacp-role": "Actor",
                        "lacp-synchronization": "Yes",
                        "lacp-timeout": "Fast",
                        "name": "xe-3/0/1",
                    },
                    {
                        "lacp-activity": "Active",
                        "lacp-aggregation": "Yes",
                        "lacp-collecting": "Yes",
                        "lacp-defaulted": "No",
                        "lacp-distributing": "Yes",
                        "lacp-expired": "No",
                        "lacp-role": "Partner",
                        "lacp-synchronization": "Yes",
                        "lacp-timeout": "Fast",
                        "name": "xe-3/0/1",
                    },
                ],
            }
        }
    }

    def test_empty(self):
        self.device = Mock(**self.empty_output)
        obj = ShowLacpInterfacesInterface(device=self.device)
        with self.assertRaises(SchemaEmptyParserError):
            obj.parse()

    def test_golden_instance(self):
        self.device = Mock(**self.golden_output)
        obj = ShowLacpInterfacesInterface(device=self.device)
        parsed_output = obj.parse(interface="ae4")
        self.assertEqual(parsed_output, self.golden_parsed_output)

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


from django import forms
from .models import Poll , Choice
from django.forms import formset_factory

class PollCreateForm(forms.ModelForm):

	class Meta:

		model = Poll
		fields = ['name' , 'question' , 'image' , 'vote_limit']

		labels = {

			'name': "Name",
			'question': "Enter your poll's main question",
			'image': "you can upload an image for your poll" ,
			'vote_limit': "How many votes the the voters can take place in your poll?"
		}

	def __init__(self , *args , **kwargs):

		super(PollCreateForm,self).__init__(*args , **kwargs)

		name = self.fields['name']
		question = self.fields['question']
		image = self.fields['image']
		vote_limit = self.fields['vote_limit']

		name.widget.attrs.update({'class':'name-field'})
		question.widget.attrs.update({'class':'question-field'})
		image.widget.attrs.update({'class':'image-field'})
		vote_limit.widget.attrs.update({'class':'vote-field'})

class ChoiceForm(forms.ModelForm):

	class Meta:

		model = Choice
		fields = ['field']

		labels = {

			'field':'Enter your choice'
		}

	def __init__(self , *args , **kwargs):

		super(ChoiceForm,self).__init__(*args , **kwargs)

		field = self.fields['field']

		field.widget.attrs.update({'class' : 'field'})

ChoiceFormset = formset_factory(ChoiceForm , extra=5)


# -*- coding: utf-8 -*-
import os, sys

from fake_useragent import UserAgent
from requests_html import HTMLSession
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import re

cred = credentials.Certificate('/./root/Classmate/ServiceAccount.json')
firebase_admin.initialize_app(cred)
db = firestore.client()

ua = UserAgent()
uarandom = {'User-Agent': ua.random}
schulhauslinks = ('https://display.edubs.ch/bfsa', 'https://display.edubs.ch/bfsb', 'https://display.edubs.ch/bfsc',
                  'https://display.edubs.ch/bfsd', 'https://display.edubs.ch/dr1', 'https://display.edubs.ch/fzg1',
                  'https://display.edubs.ch/fms1', 'https://display.edubs.ch/gm1', 'https://display.edubs.ch/gb1',
                  'https://display.edubs.ch/gkg1', 'https://display.edubs.ch/gl1', 'https://display.edubs.ch/ii1',
                  'https://display.edubs.ch/kh1', 'https://display.edubs.ch/bibliothek',
                  'https://display.edubs.ch/medialab1', 'https://display.edubs.ch/sb1', 'https://display.edubs.ch/dw1',
                  'https://display.edubs.ch/dli1', 'https://display.edubs.ch/hb1', 'https://display.edubs.ch/sleo',
                  'https://display.edubs.ch/sand1', 'https://display.edubs.ch/alb1', 'https://display.edubs.ch/tb',
                  'https://display.edubs.ch/vog1', 'https://display.edubs.ch/wr1', 'https://display.edubs.ch/joh1',
                  'https://display.edubs.ch/dli2', 'https://display.edubs.ch/ts1', 'https://display.edubs.ch/wg1',
                  'https://display.edubs.ch/zbag1', 'https://display.edubs.ch/zbal1')
schulhauslinksTest = (
    'https://display.edubs.ch/gm1',
)

for haupturl in schulhauslinks:  # TODO: Test entfernen

    a = "1"
    b = "2"
# ======

    # Hier wird die Schule ausgelesen.
    session = HTMLSession()
    r = session.get(haupturl, headers=uarandom)
    schulehtml = r.html.find('body > div > h1', first=True)
    schule = schulehtml.text

    # Fromatiere schule fuer Target
    schuleTarget2 = re.sub('r"|\s|"', '', schule)
    schuleTarget = re.sub('[äöü]', '', schuleTarget2)

 # ======
    anzahlausfaelle1 = r.html.text
    anzahlausfaelle1split = anzahlausfaelle1.split('\n')
    del (anzahlausfaelle1split)[0:4]

    try:
        ersterausfalltaghtml = r.html.find('body > div > h3', first=True)
        ersterausfalltag = ersterausfalltaghtml.text
        ersterausfalltag1 = a + ersterausfalltag
        keinausfalltag = False

    except:  # keine Stellvertretungen
        keinausfalltag = True
        ersterausfalltaghtml = r.html.find(
            'body > div > div > div > strong')[0]
        ersterausfalltag = ersterausfalltaghtml.text
        ersterausfalltag1 = a + ersterausfalltag

    try:
        zweiteausfalltaghtml = r.html.find('body > div > h3')[1]
        zweiteausfalltag = zweiteausfalltaghtml.text
        zweiteausfalltag1 = b + zweiteausfalltag
        zweiterausfall = True

    except:  # kein zweiter Tag
        zweiterausfall = False
        zweiteausfalltaghtml = r.html.find('body > div > h2')[1]
        zweiteausfalltag = zweiteausfalltaghtml.text
        zweiteausfalltag1 = b + zweiteausfalltag

 # ======
    # Gruppiert alle informationen in 3ergruppen = 1 ausfall ->
    # wenn zweiter ausfall true = zwei listen(zb. montag und dienstag) mit den jeweiligen ausfaellen
    alleAusfaelleUnsortiert = anzahlausfaelle1split
    if zweiterausfall:
        # splittet alle ausfaelle in 2 listen bei zweitem tag
        splitIndex = alleAusfaelleUnsortiert.index(zweiteausfalltag)
        ersterTagListe, zweiterTagListeDirty = alleAusfaelleUnsortiert[
            :splitIndex], alleAusfaelleUnsortiert[splitIndex+1:]

        # splittet zweiten tag bei anlaesse
        anlaesseSplit = zweiterTagListeDirty.index('Anlässe')
        zweiterTagListe = zweiterTagListeDirty[:anlaesseSplit]

        # in 3er gruppen sortieren
        N = 3
        alleAusfaelleListe1Tag = [ersterTagListe[n:n+N]
                                 for n in range(0, len(ersterTagListe), N)]

        alleAusfaelleListe2Tag = [zweiterTagListe[n:n+N]
                                 for n in range(0, len(zweiterTagListe), N)]
    else:
        N = 3
        alleAusfaelleListe1Tag = [alleAusfaelleUnsortiert[n:n+N]
                                 for n in range(0, len(alleAusfaelleUnsortiert), N)]
# ======
    # Zaehlt ausfaelle des 1. Tags
    anzahlerstertag = 0
    for position in anzahlausfaelle1split:
        if position == zweiteausfalltag:
            break
        else:
            anzahlerstertag = anzahlerstertag + 1

    anzahlerstertagganz = anzahlerstertag + 5

    anzahlx = anzahlerstertag / 3

    if anzahlx < 1:
        anzahl1 = 0
    else:
        anzahl1 = anzahlx
# ======
    # Zaehlt ausfaelle des 2. Tags
    if zweiterausfall:
        anzahlzweitertag = 0
        anzahlT2 = len(zweiterTagListe) / 3
    else:
        anzahlT2 = 0
# ======
    # =========================================
    # ERSTER TAG
    # =========================================
    x = 0

    # Liest klasse aus HTML
    while x < anzahl1:
        try:
            # ----- INFOS -----# (benoetigt wird: Ausfall(alle Infos), Betroffene klassen, Tag des Ausfalls)

            # SCHULHAUS das bearbeitet wird
            # print(schule)

            # AUSFALL der bearbeitet wird (alle infos)

            auktellerAufall = alleAusfaelleListe1Tag[x]

            # TAG des Ausfalls
            # print(ersterausfalltag1)
            # print(zweiteausfalltag1)

            # klasse in html suchen
            klasse = r.html.find('body > div > div > div > strong')[x]
            klassetext = klasse.text

            # splittet den klassentext in einen array ( splittet bei komma und lehrzeichen )
            klassetextsplit = re.split(',| ', klassetext)

            klassentextlen = len(klassetextsplit)

            anz = 0  # setzt while loop unten zurueck
 # ======    # Liest verschindene Klassen aus und unterteilt sie in eine Liste
            while anz < klassentextlen:
                try:
                    klass = klassetextsplit[anz]
                    klasseSauber = klass.replace(':', '')

                    klasslen = len(klasseSauber)
                    klassenstufe = klass[0]

                    if klassenstufe.isdigit():

                        zeichen = 1  # setzt while loop unten zurueck
# ======                 # Liest zusammengesetzte Klassen aus. (Bsp. 1ac, 2BD, 3abc)
                        while zeichen <= klasslen:

                            try:
                                # TODO: check if 2. oder 3. stelle ein Buchstabe und nicht eine Zahl ist
                                # 2. oder 3. stelle nicht weiter!s
                                if klasseSauber[zeichen].isalpha():
                                    klassExtracted = klassenstufe + \
                                        klasseSauber[zeichen]
                                    # TODO: final goal is here bring here the ausfaelle!

                                    zeichen = zeichen + 1
                                    klassFinal = klassExtracted.upper()

                                    if klassetext == "Keine Anlässe":
                                        break
                                    if klassetext == "Keine Stellvertretungen":
                                        break
                                    else:
                                        if klassetext == "Keine Neuigkeiten":
                                            break
                                        else:
                                            num1 = x - 1 + zeichen
                                            num = str(num1)
                                            individualDoc = auktellerAufall[0] + \
                                                ' - '+num

                                            # fuegt 'am' hinzu bei allen wochentagen
                                            if ersterausfalltag == 'Heute:':
                                                tag1 = 'Ausfall '+ersterausfalltag
                                            else:
                                                tag1 = 'Ausfall am '+ersterausfalltag
                                            try:
                                                db.collection(u'Nachrichten').document(u'Schulen').collection(
                                                    schuleTarget2).document(individualDoc).set({

                                                        u'tag': tag1,
                                                        u'target': schuleTarget+'-'+klassFinal,
                                                        u'klasse': auktellerAufall[0],
                                                        u'grund': auktellerAufall[1],
                                                        u'raum': auktellerAufall[2],

                                                    })
                                            except:
                                                e = sys.exc_info()[0]

                                else:
                                    break

                            except:
                                break
                        anz = anz + 1
                    else:

                        anz = anz + 1

                except:

                    break
            x = x + 1
        except:
            break
# ======
    # =========================================
    # ZWEITER TAG
    # =========================================
    y = 0

    # Liest klasse aus HTML

    while y < anzahlT2:
        try:
            # ----- INFOS -----# (benoetigt wird: Ausfall(alle Infos), Betroffene klassen, Tag des Ausfalls)

            # SCHULHAUS das bearbeitet wird
            # print(schule)

            # AUSFALL der bearbeitet wird (alle infos)

            auktellerAufall = alleAusfaelleListe2Tag[y]

            # TAG des Ausfalls
            # print(ersterausfalltag1)
            # print(zweiteausfalltag1)

            # klasse in html suchen
            klasse = r.html.find('body > div > div > div > strong')[x+y]
            klassetext = klasse.text

            # splittet den klassentext in einen array ( splittet bei komma und lehrzeichen )
            klassetextsplit = re.split(',| ', klassetext)

            klassentextlen = len(klassetextsplit)

            anz = 0  # setzt while loop unten zurueck
# ======     # Liest verschindene Klassen aus und unterteilt sie in eine Liste
            while anz < klassentextlen:
                try:
                    klass = klassetextsplit[anz]
                    klasseSauber = klass.replace(':', '')

                    klasslen = len(klasseSauber)
                    klassenstufe = klass[0]

                    if klassenstufe.isdigit():

                        zeichen = 1  # setzt while loop unten zurueck
# ======                 # Liest zusammengesetzte Klassen aus. (Bsp. 1ac, 2BD, 3abc)
                        while zeichen <= klasslen:

                            try:

                                if klasseSauber[zeichen].isalpha():
                                    klassExtracted = klassenstufe + \
                                        klasseSauber[zeichen]

                                    zeichen = zeichen + 1
                                    klassFinal = klassExtracted.upper()

                                    if klassetext == "Keine Anlässe":
                                        break
                                    if klassetext == "Keine Stellvertretungen":
                                        break
                                    else:
                                        if klassetext == "Keine Neuigkeiten":
                                            break
                                        else:
                                            num1 = x + y - 1 + zeichen
                                            num = str(num1)
                                            individualDoc = auktellerAufall[0] + \
                                                ' - '+num
                                            # TODO:

                                            # fuegt 'am' hinzu bei allen wochentagen
                                            if zweiteausfalltag == 'Morgen:':
                                                tag2 = 'Ausfall '+zweiteausfalltag
                                            else:
                                                tag2 = 'Ausfall am '+zweiteausfalltag
                                            try:
                                                db.collection(u'Nachrichten').document(u'Schulen').collection(
                                                    schuleTarget2).document(individualDoc).set({
                                                        u'tag': tag2,
                                                        u'target': schuleTarget+'-'+klassFinal,
                                                        u'klasse': auktellerAufall[0],
                                                        u'grund': auktellerAufall[1],
                                                        u'raum': auktellerAufall[2],

                                                    })

                                            except:
                                                e = sys.exc_info()[0]

                                else:
                                    break
                            except:
                                break
                        anz = anz + 1
                    else:

                        anz = anz + 1

                except:

                    break
            y = y + 1
        except:
            break


# Copyright (C) 2012 Hiroki Horiuchi <x19290@gmail.com>
#
# [GNU all-permissive license]
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.
# This file is offered as-is, without any warranty.

from pbe1 import common_tex
from com.appspot.x19290.toytex import parse_tex, _Command, _Group, _Surface
from re import compile as regex
from sys import argv

def main(args=()):
    macro_file = args[0] if args else common_tex
    for command, nargs, definition in inddefs(macro_file):
        nargs = '[%d]' % nargs if nargs else ''
        yield command + nargs + definition

def inddefs(macro_file=common_tex):
    ind_re = regex(r'^\\[A-Za-z]*ind(?:ex)?(?:main)?$')
    tex = file(macro_file).read()
    g = parse_tex(tex)
    for y in g:
        newcommand = get_newcommand(y, g, tex)
        if not newcommand:
            continue
        command, nargs, definition = newcommand
        command = tex[command.begin:command.end]
        if not ind_re.search(command):
            continue
        if count_params(definition, tex) != nargs:
            count_params(definition)
            raise Exception(definition)
        yield command, nargs, tex[definition.begin:definition.end]

def get_newcommand(y, g, tex):
    if not isinstance(y, _Command):
        return None
    if tex[y.begin:y.end] != r'\newcommand':
        return None
    command, signature_or_definition = g.next(), g.next()
    if isinstance(signature_or_definition, _Surface):
        sign = tex[signature_or_definition.begin:signature_or_definition.end]
        nargs = get_nargs(sign)
        definition = g.next()
    else:
        nargs = 0
        definition = signature_or_definition
    command = check_command_and_definition(command, definition)
    return command, nargs, definition

def check_command_and_definition(command, definition):
    command.must_be(_Group)
    definition.must_be(_Group)
    command = command.parsed
    if len(command) != 1:
        command.error()
    command = command[0]
    command.must_be(_Command)
    return command

_signature = regex(r'^\[(\d+)\]$')
def get_nargs(signature):
    m = _signature.match(signature)
    if not m:
        raise Exception(signature)
    return int(m.group(1))

_param = regex(r'#(\d+)')
def count_params(definition, tex):
    max = 0
    for found in _param.findall(tex[definition.begin:definition.end]):
        found = int(found)
        if max < found:
            max = found
    return max

if __name__ == '__main__':
    for y in main(argv[1:]):
        print(y)


import time
import math
import os
from go_utils import GOUtils
from qusim.queue_simulator import QueueSimulator

class CheckRemoteQueue:

  def __init__(self, gu, resources={}, master='anl_laptop',
      master_dir='/executables/daemon_master', worker_dir='/executables/daemon_worker',
      cmd_input_dir='/cmds/input', cmd_output_dir='/cmds/output',cmd_file='trace.cmd',
      output_file='queue.result', worker_cmd_check_cycle_time=10):
    if master!='anl_laptop':
      raise ValueError('Only anl_laptop is known for command repository for now')
    if len(resources)<1 or master=='':
      raise ValueError('Number of resources for transfer class cannot be less than 2!')
    self.gu = gu
    self.resources = resources
    self.master = resources[master]
    self.cmd_file = cmd_file
    self.cmd_input_dir = cmd_input_dir
    self.cmd_output_dir = cmd_output_dir
    self.output_file = output_file
    self.worker_dir = worker_dir
    self.master_dir = master_dir
    self.worker_cmd_check_cycle_time = worker_cmd_check_cycle_time

  def __check_queue(
      self, n0,
      check_file_trial=120, check_file_delay=5):
    master_full_cmds_input_dir=(self.master['tw_dir_path']+
        self.master_dir+self.cmd_input_dir)
    master_full_cmds_output_dir=(self.master['tw_dir_path']+
        self.master_dir+self.cmd_output_dir)
    worker_full_cmds_input_dir=(n0['tw_dir_path']+
        self.worker_dir+self.cmd_input_dir)
    worker_full_cmds_output_dir=(n0['tw_dir_path']+
        self.worker_dir+self.cmd_output_dir)

    # Make sure target master and worker cmds/(input|output) folders don't contain
    # trace.cmd and rtt.result files, respectively
    #print("Cleaning up master and worker directories")
    if self.gu.go_check_file(data_endpoint_to=n0['end_point'],
        data_endpoint_to_dir=worker_full_cmds_output_dir,
        data_endpoint_to_filename=self.output_file, repeat=1)==0:
      self.gu.go_rm_files(data_endpoint_to=n0['end_point'],
            data_endpoint_to_dir=worker_full_cmds_output_dir,
            data_endpoint_to_filenames=[self.output_file])
    if self.gu.go_check_file(data_endpoint_to=n0['end_point'],
        data_endpoint_to_dir=worker_full_cmds_input_dir,
        data_endpoint_to_filename=self.cmd_file, repeat=1)==0:
      self.gu.go_rm_files(data_endpoint_to=n0['end_point'],
            data_endpoint_to_dir=worker_full_cmds_input_dir,
            data_endpoint_to_filenames=[self.cmd_file])
    if self.gu.go_check_file(data_endpoint_to=self.master['end_point'],
        data_endpoint_to_dir=master_full_cmds_input_dir,
        data_endpoint_to_filename=self.output_file, repeat=1)==0:
      self.gu.go_rm_files(data_endpoint_to=self.master['end_point'],
            data_endpoint_to_dir=master_full_cmds_input_dir,
            data_endpoint_to_filenames=[self.output_file])
    if self.gu.go_check_file(data_endpoint_to=self.master['end_point'],
        data_endpoint_to_dir=master_full_cmds_output_dir,
        data_endpoint_to_filename=self.cmd_file, repeat=1)==0:
      self.gu.go_rm_files(data_endpoint_to=self.master['end_point'],
            data_endpoint_to_dir=master_full_cmds_output_dir,
            data_endpoint_to_filenames=[self.cmd_file])

    # Create new cmd file
    f = open((master_full_cmds_output_dir+'/'+self.cmd_file), 'w')
    f.write(('output_path:'+ worker_full_cmds_output_dir +
      ';output_file:' + self.output_file + '\n'))
    f.write((n0['queue_info_cmd'] + '\n'))
    f.close()
    # Send trace.cmd file for execution
    #print("Transfering cmd file")
    ret=self.gu.go_transfer_file(
        data_endpoint_from=self.master['end_point'],
        data_endpoint_from_dir=master_full_cmds_output_dir,
        data_endpoint_from_filename=self.cmd_file,
        data_endpoint_to=n0['end_point'],
        data_endpoint_to_dir=worker_full_cmds_input_dir,
        data_endpoint_to_filename=self.cmd_file)
    # Check if execution is finalized
    #print("Checking if processed")
    ret=self.gu.go_check_file(
        data_endpoint_to=n0['end_point'],
        data_endpoint_to_dir=worker_full_cmds_output_dir,
        data_endpoint_to_filename=self.output_file,
        repeat=check_file_trial,
        repetition_time=check_file_delay)
    if ret!=0: raise RuntimeError('File check error:' + str(ret))
    #print("Transfering results back to master")
    # Receive resulting output file back to master
    ret=self.gu.go_transfer_file(
        data_endpoint_from=n0['end_point'],
        data_endpoint_from_dir=worker_full_cmds_output_dir,
        data_endpoint_from_filename=self.output_file,
        data_endpoint_to=self.master['end_point'],
        data_endpoint_to_dir=master_full_cmds_input_dir,
        data_endpoint_to_filename=self.output_file)
    # Delete remote output files
    #print("Cleaning up remote files")
    self.gu.go_rm_files(
          data_endpoint_to=n0['end_point'],
          data_endpoint_to_dir=worker_full_cmds_output_dir,
          data_endpoint_to_filenames=[self.output_file])
    job_file_path=(master_full_cmds_input_dir+'/'+self.output_file+'.'+n0['resource'])
    os.rename(
        (master_full_cmds_input_dir+'/'+self.output_file),
        job_file_path)
    return job_file_path

  def estimate(self, targets, tw_job):
    for k,v in tw_job.iteritems():
      if v=='' or v==None: raise ValueError('Job values cannot be empty!')
    job_key_list = ['estimated_time', 'required_num_processors', 'submission_time',
        'backlog_days', 'user_id']
    if len(set(job_key_list).intersection(set(tw_job.keys())))!=len(job_key_list):
      raise ValueError("Job key values do not match:{} vs {}".format(
        tw_job.keys(), job_key_list))

    target_dict = {}
    for target in targets:
      target_dict[target]=self.resources[target]
    return self.__estimate_helper(target_dict, tw_job)

  def __estimate_helper(self, target_dict, tw_job):
    for target,properties in target_dict.iteritems():
      if properties['hostname']=='':
        raise ValueError('Target hostname (' + target + ') is empty!')
      if properties['type']!='compute':
        raise ValueError('Target type is not compute!')
    job_file_paths={}
    #job_file_paths['gordon']='/Users/bicer/Projects/TraceWorkflow/executables/daemon_master/cmds/input/queue.result.gordon'
    for target,properties in target_dict.iteritems():
      #print("Retriving job queue:{}".format(target))
      properties['resource'] = target
      job_file_paths[target] = self.__check_queue(properties)
    job_queue_sim_results={}
    for cluster,job_queue_file_path in job_file_paths.iteritems():
      #print("Starting simulation: {}".format(cluster))
      job_queue_sim_results[cluster] = self.__simulate_job(
          job_queue_file_path,
          cluster,
          target_dict[cluster]['scheduler'],
          int(target_dict[cluster]['num_processors']),
          tw_job)
    return job_queue_sim_results

  def __simulate_job(self, job_file_path, resource, scheduler, num_processors, tw_job):
    simulator = QueueSimulator(
        num_processors=num_processors,
        resource=resource,
        scheduler_type=scheduler)
    return simulator.simulate(
        input_file_path=job_file_path,
        job_estimated_time=tw_job['estimated_time'],
        job_required_num_processors=tw_job['required_num_processors'],
        job_submission_time=tw_job['submission_time'],
        job_backlog_days=tw_job['backlog_days'],
        job_user_id=tw_job['user_id'])


# Image smoothing operations

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('logo.png')

# 5x5 averaging filter F (times 1/25)
'''
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)

plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()
#'''

#img = cv2.imread('logo_noise.png')

# 4x4 averaging filter
#blur = cv2.blur(img,(5,5))

# Gaussian filter
#blur = cv2.GaussianBlur(img,(5,5),0)

# Median filter
#median = cv2.medianBlur(img, 5)

# Bilateral filtering (preserves edges)
blur = cv2.bilateralFilter(img,9,75,75)

plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blur),plt.title('Blurred')
#plt.subplot(122),plt.imshow(median),plt.title('Median')
plt.xticks([]), plt.yticks([])
plt.show()
#'''


from .base import FunctionalTest

from day.helpers import timestamp

from unittest import skip

class ItemValidationTests(FunctionalTest):

	def test_cannot_add_empty_event(self):
		# a rider goes to the home page and tried to submit an empty
		# event
		self.create_test_user_and_login()
		self.browser.get(self.server_url)
		self.get_event_inputbox().send_keys("\n")

		# the home page refreshes and displays and error message saying
		# that events can't be blank
		error_text = "You can't have an empty event"
		error = self.browser.find_element_by_css_selector('.has-error')
		self.assertEqual(error.text, error_text)

		# the rider tries agian with some text and it works
		event_text = "and event to write about"
		self.get_event_inputbox().send_keys(event_text + "\n")
		time_enter_pressed = timestamp()
		self.check_for_row_in_day_table(time_enter_pressed, event_text)

		# the rider again tries to submit a blank event
		self.get_event_inputbox().send_keys("\n")

		# the rider recieves a warning on the day page
		self.check_for_row_in_day_table(time_enter_pressed, event_text)
		error = self.browser.find_element_by_css_selector('.has-error')
		self.assertEqual(error.text, error_text)

		# the rider submits some text and it works
		event_2_text = "a second event to write about"
		self.get_event_inputbox().send_keys(event_2_text + "\n")
		time_enter_pressed_2 = timestamp()
		self.check_for_row_in_day_table(time_enter_pressed, event_text)
		self.check_for_row_in_day_table(time_enter_pressed_2, event_2_text)


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

from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from skopt import BayesSearchCV
from keras.models import Sequential,Model,load_model
from keras.layers import Dense,Activation,Input,Dropout,Concatenate
from keras.layers.normalization import BatchNormalization
from keras.layers.core import Lambda
from keras.wrappers.scikit_learn import KerasRegressor
import random
import keras.backend as K
import keras.optimizers
import tensorflow as tf
import numpy as np
import pandas as pd
import sqlite3, pickle
import dataset2, util, evaluate, feature

USE_CACHE = True
LOOSE_VALUE = -100
DONT_BUY_VALUE = -20.0
REWARD_THREHOLD = -10
EVALUATE_INTERVAL = 10
MAX_ITERATION = 50000
BATCH_SIZE = 36
REFRESH_INTERVAL = 50
PREDICT_TYPE = "win_payoff"
#PREDICT_TYPE = "place_payoff"
MODEL_PATH = "./models/dqn_model2.h5"
PREDICT_MODEL_PATH = "./models/dqn_model2.h5"
MEAN_PATH = "./models/dqn_mean.pickle"
STD_PATH = "./models/dqn_std.pickle"
CACHE_PATH = "./cache/dueling_dqn"

def main():
    #predict_type = "place_payoff"
    predict_type = PREDICT_TYPE
    config = util.get_config("config/config.json")
    db_path = "db/output_v7.db"

    db_con = sqlite3.connect(db_path)
    if USE_CACHE:
        print("[*] load dataset from cache")
        datasets = dataset2.load_cache(CACHE_PATH)
    else:
        datasets = generate_dataset(predict_type,db_con,config)
    dnn(config.features_light,datasets)

def predict(db_con,config):
    add_col = ["info_horse_name"]
    features = config.features_light+add_col
    raw_x = dataset2.load_x(db_con,features)
    col_dic = dataset2.nominal_columns(db_con)
    nom_col = dataset2.dummy_column(raw_x,col_dic)
    raw_x = dataset2.get_dummies(raw_x,col_dic)

    target_columns = []
    remove = ["info_race_id","info_horse_name","info_race_name"]
    for col in raw_x.columns:
        if col not in remove:
            target_columns.append(col)
    target_columns = sorted(target_columns)

    x = dataset2.fillna_mean(raw_x,"horse")
    mean = load_value(MEAN_PATH)
    std = load_value(STD_PATH)
    x = dataset2.normalize(x,mean = mean,std = std,remove = nom_col+add_col)
    x = dataset2.pad_race_x(x)
    x = dataset2.to_race_panel(x)[0]

    inputs = x.loc[:,:,target_columns]
    model = load_model(PREDICT_MODEL_PATH)
    actions = []
    for i in range(len(inputs)):
        rx = x.iloc[i,:,:]
        ri = inputs.iloc[i,:,:]

        a = get_action(model,ri.as_matrix(),is_predict = True)
        a = pd.DataFrame(a,columns = ["dont_buy","buy"])
        rx = pd.concat([rx,a],axis = 1)
        print(rx.loc[:,["info_horse_name","buy"]])
        print("")
        if i > 12:
            break
    actions = pd.Panel(actions)

    print(actions)

def generate_dataset(predict_type,db_con,config):
    print("[*] preprocessing step")
    print(">> loading dataset")
    features = config.features_light
    x,y = dataset2.load_dataset(db_con,features,["is_win","win_payoff","is_place","place_payoff"])
    col_dic = dataset2.nominal_columns(db_con)
    nom_col = dataset2.dummy_column(x,col_dic)
    x = dataset2.get_dummies(x,col_dic)
    features = sorted(x.columns.drop("info_race_id").values.tolist())

    print(">> separating dataset")
    train_x,test_x,train_y,test_y = dataset2.split_with_race(x,y,test_nums = 1000)
    del x
    del y

    print(">> filling none value of train dataset")
    train_x = dataset2.fillna_mean(train_x,"horse")
    mean = train_x.mean(numeric_only = True)
    std = train_x.std(numeric_only = True).clip(lower = 1e-4)
    save_value(mean,MEAN_PATH)
    save_value(std,STD_PATH)
    train_x = dataset2.normalize(train_x,mean = mean,std = std,remove = nom_col)

    print(">> converting train dataset to race panel")
    train_x = dataset2.downcast(train_x)
    train_y = dataset2.downcast(train_y)
    train_x,train_y = dataset2.pad_race(train_x,train_y)
    train_y["win_payoff"] = train_y["win_payoff"] + LOOSE_VALUE
    train_y["place_payoff"] = train_y["place_payoff"] + LOOSE_VALUE
    train_y["dont_buy"] = pd.DataFrame(np.zeros(len(train_y.index))+DONT_BUY_VALUE,dtype = np.float32)
    train_y["is_win"] = train_y["win_payoff"].clip(lower = 0,upper = 1)
    train_y["is_place"] = train_y["place_payoff"].clip(lower = 0,upper = 1)

    train_x,train_y = dataset2.to_race_panel(train_x,train_y)
    train_x = train_x.loc[:,:,features]
    train_y = train_y.loc[:,:,["dont_buy","place_payoff","win_payoff","is_win","is_place"]]

    print(">> filling none value of test dataset")
    test_x = dataset2.fillna_mean(test_x,"horse")
    test_x = dataset2.normalize(test_x,mean = mean,std = std,remove = nom_col)

    print(">> converting test dataset to race panel")
    test_x,test_y = dataset2.pad_race(test_x,test_y)
    test_y["win_payoff"] = test_y["win_payoff"] + LOOSE_VALUE
    test_y["place_payoff"] = test_y["place_payoff"] + LOOSE_VALUE
    test_y["dont_buy"] = np.zeros(len(test_y.index),dtype = np.float32)+DONT_BUY_VALUE
    test_y["is_win"] = test_y["win_payoff"].clip(lower = 0,upper = 1)
    test_y["is_place"] = test_y["place_payoff"].clip(lower = 0,upper = 1)

    test_x,test_y = dataset2.to_race_panel(test_x,test_y)
    test_x = test_x.loc[:,:,features]
    test_y = test_y.loc[:,:,["dont_buy","place_payoff","win_payoff","is_win","is_place"]]

    datasets = {
        "train_x" : train_x,
        "train_y" : train_y,
        "test_x"  : test_x,
        "test_y"  : test_y,
    }
    dataset2.save_cache(datasets,CACHE_PATH)
    return datasets

def dnn(features,datasets):
    print("[*] training step")
    target_y = "is_win"
    train_x = datasets["train_x"]
    train_y = datasets["train_y"].loc[:,:,[target_y]]
    train_action = datasets["train_y"].loc[:,:,["dont_buy",PREDICT_TYPE]]
    #train_action.loc[:,:,"dont_buy"] = -10

    test_x = datasets["test_x"]
    test_y = datasets["test_y"].loc[:,:,target_y]
    test_action  = datasets["test_y"].loc[:,:,["dont_buy",PREDICT_TYPE]]

    model =  create_model()
    old_model = keras.models.clone_model(model)

    #main_loop
    batch_size = BATCH_SIZE
    gene = dataset_generator(batch_size,train_x,train_y,train_action)
    max_iteration = MAX_ITERATION

    for count in range(max_iteration):
        raw_x,raw_y,raw_reward = next(gene)
        x_ls = []
        y_ls = []
        a_ls = []
        prob_threhold = max(float(100 - count),10.0)/1000
        rob_threhold = 0.0
        for i in range(len(raw_x)):
            rx = raw_x.ix[i]
            ry = raw_y.ix[i]
            ra = raw_reward.ix[i]

            idx = np.random.randint(18)
            new_x = rx.ix[idx]
            new_y = ry.ix[idx]
            new_a = ra.ix[idx]
            #reward = get_q_value(old_model,others(rx,idx),others(ra,idx),action_threhold = prob_threhold)
            reward = get_reward(model,others(rx,idx),others(ra,idx),action_threhold = prob_threhold)
            new_a += reward
            new_a = clip(new_a)
            x_ls.append(new_x)
            y_ls.append(new_y)
            a_ls.append(new_a)
        x = np.array(x_ls)
        y = np.array(y_ls)
        a = np.array(a_ls)
        hist = model.fit(x,[a,y],verbose = 0,epochs = 1)
        if count % EVALUATE_INTERVAL == 0:
            evaluate(count,model,test_x,test_action)
            print(hist.history["loss"][0])
            print("")
            save_model(model,MODEL_PATH)
        if count % REFRESH_INTERVAL == 0:
            old_model = keras.models.clone_model(model)

def create_model(activation = "relu",dropout = 0.6,hidden_1 = 120,hidden_2 = 120,hidden_3 = 120):
    inputs_size = 134
    actions_size = 2

    inputs = Input(shape = (inputs_size,))
    x = inputs

    x = Dense(units = hidden_1)(x)
    x = Activation(activation)(x)
    x = BatchNormalization()(x)
    x = Dropout(dropout)(x)

    x = Dense(units = hidden_2)(x)
    x = Activation(activation)(x)
    x = BatchNormalization()(x)
    x = Dropout(dropout)(x)

    x = Dense(units = hidden_3)(x)
    x = Activation(activation)(x)
    x = BatchNormalization()(x)
    x = Dropout(dropout)(x)

    is_win = Dense(units = 1)(x)
    is_win = Activation("sigmoid")(is_win)

    pi = Dense(units = 8)(x)
    pi = Activation(activation)(pi)
    pi = BatchNormalization()(pi)
    pi = Concatenate()([pi,is_win])
    pi = Dense(units = actions_size)(pi)
    pi = Activation("softmax")(pi)

    # For Dueling
    #x = Dense(units = actions_size + 1)(x)
    #x = Lambda(lambda a:K.expand_dims(a[:,0],axis=-1) + a[:,1:],output_shape = (actions_size,))(x)

    model = Model(inputs = inputs,outputs = [pi,is_win])
    opt = keras.optimizers.Adam(lr=0.001)
    #model.compile(loss = [huber_loss,"binary_crossentropy"],optimizer=opt,metrics=["accuracy"])
    model.compile(loss = ["categorical_crossentropy","binary_crossentropy"],optimizer=opt,metrics=["accuracy"])
    return model

def evaluate(step,model,x,y):
    total_reward = 0
    total_buy = 0
    total_hit = 0
    for i in range(len(x)):
        rx = x.iloc[i]
        ry = y.iloc[i]
        is_win = ry.iloc[:,1].clip(lower = 0.0,upper = 1.0)
        buy = get_action(model,rx.as_matrix(),is_predict = True)[:,1]
        buy_num = buy.sum()
        is_hit = 1 if np.dot(is_win,buy) > 0 else 0
        reward = get_reward(model,rx,ry,is_predict = True)
        total_reward += reward
        total_buy += buy_num
        total_hit += is_hit
    avg_reward = total_reward/float(len(x))
    avg_buy = total_buy/float(len(x))
    avg_hit = total_hit/float(len(x))
    print("Step: {0}".format(step))
    print("Profit: {0} yen/race".format(avg_reward))
    print("Hit: {0} tickets/race".format(avg_hit))
    print("Buy : {0} tickets/race".format(avg_buy))

def dataset_generator(batch_size,*datasets):
    columns = []
    for d in datasets:
        columns.append(d.axes[2].tolist())
    con = pd.concat(datasets,axis = 2)
    _,i = np.unique(con.axes[2],return_index = True)
    con = con.iloc[:,:,i]

    while True:
        sample = con.sample(n = batch_size,axis = 0)
        ret = []
        for i,_ in enumerate(datasets):
            ret.append(sample.loc[:,:,columns[i]])
        yield ret

def get_q_value(model,x,y,is_predict = False,action_threhold = 0.01):
    x = x.as_matrix()
    y = y.as_matrix()

    pred,_ = model.predict(x)
    idx = pred.argmax(1)
    if not is_predict:
        for i in range(len(idx)):
            prob = np.random.rand()
            if prob < action_threhold:
                idx[i] = np.random.randint(2)
    pred = pred[idx]
    return np.sum(pred)

def get_reward(model,x,y,is_predict = False,action_threhold = 0.01):
    x = x.as_matrix()
    y = y.as_matrix()
    action = get_action(model,x,is_predict = is_predict,threhold = action_threhold)
    rewards = (y*action).sum()
    return rewards

def get_action(model,x,is_predict = False,threhold = 0.01):
    pred,_ = model.predict(x)
    action = pred.argmax(1)
    if not is_predict:
        for i in range(len(action)):
            prob = np.random.rand()
            if prob < threhold:
                action[i] = np.random.randint(2)
    action = np.eye(2)[action]
    return action

def clip(y):
    y = y/100.0
    #y = y/100.0
    y[y<=REWARD_THREHOLD] = 0
    y[y>REWARD_THREHOLD] = 1
    #y = y.clip(lower = -5.0,upper = 5.0)
    #y = y.clip(upper = 5.0)
    return y

def others(df,idx):
    df = df[~df.index.isin([idx])]
    return df

def save_model(model,path):
    print("Save model")
    model.save(path)

def save_value(v,path):
    with open(path,"wb") as fp:
        pickle.dump(v,fp)

def load_value(path):
    with open(path,"rb") as fp:
        v = pickle.load(fp)
        return v
    raise Exception ("File does not exist")

def huber_loss(y_true,y_pred):
    clip_value = 1.0
    x = y_true - y_pred
    condition = K.abs(x) < clip_value
    squared_loss = K.square(x)
    linear_loss = clip_value * (K.abs(x) -0.5*clip_value)
    return tf.where(condition,squared_loss,linear_loss)

if __name__=="__main__":
    main()


# інтераткивний підруОснови Python. Контрольні завдання 4

def double_tuple(value):
    '''takes a tuple of any numbers or strings and modifies it into a tuple of tuples of two elements (in pairs)'''
    return tuple([value[i:i + 2] for i, item in enumerate(value) if i % 2 == 0])

def trimmed_text(text, limit):
    '''the function truncates the text, the words do not break off in the middle (exception is the first word), and at the end adds "..."'''
    ellipsis = "..."
    len_text = len(text)
    if limit < len_text:
        index = text.rfind(' ', 0, (limit - 2))
        if index == -1:
            index = limit - len(ellipsis)
        return text[:index].rstrip(',.:;!?') + ellipsis
    return text

print(double_tuple.__doc__)
print(double_tuple((1, 4, 8, 6, 3, 7, 1)))

print(trimmed_text.__doc__)
print(trimmed_text( "Proin eget tortor risus", 24))  # "Proin eget tortor risus"
print(trimmed_text("Proin eget tortor risus.", 23))  # "Proin eget tortor..."
print(trimmed_text("Proin eget tortor risus.", 6))  # "Pro..."

ll = [1,[2,3],4]


import os
import sys

## Initialize Variables
filepath = raw_input()
infile = open(filepath, 'r')
outfile = open(filepath.replace(".","_energy."),'w')
data_list = []
data_flag = False
lines = infile.readlines()

## Read in offset, slope, and quadratic values
offset_list = lines[7].split()
offset = float(offset_list[1])
slope_list = lines[8].split()
slope = float(slope_list[1])
quad_list = lines[9].split()
quad = float(quad_list[1])

## Build a list of data points after "DATA:"
## and convert to energy
for line in lines:
    line = line.strip()
    if data_flag == True:
        if quad == 0:
            energy = float(line) * slope + offset
        else:
            energy = slope * float(line) ** quad + offset
        string = []
        string = line,'\t',str(energy),'\n'
        out = ''.join(string)
        outfile.write(out)

    ## Write header info to new file
    else:
        outfile.write(line)
        outfile.write('\n')

    if line == "DATA:":
        data_flag = True
        outfile.write("Counts\tEnergy\n")

infile.close()
outfile.close()


# -*- coding: utf-8 -*-
"""
Created on Sun Aug 13 10:48:37 2017
2017 - 2017

@author: Guojian Wang
"""

import numpy as np
import os

def mkdir(path):
    """Make a directory in a particular location if it is not exists, otherwise, do nothing.

    Usage : mkdir('/home/jian/test'), mkdir('test/one') or mkdir('../test/one')
    """
    #remove the blank space in the before and after strings
    #path.strip() is used to remove the characters in the beginning and the end of the character string
#    path = path.strip()
    #remove all blank space in the strings, there is no need to use path.strip() when using this command
    path = path.replace(' ', '')
    #path.rstrip() is used to remove the characters in the right of the characters strings

    if path=='':
        raise ValueError('The path cannot be an empty string')
    path = path.rstrip("/")
    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path)
        print('The directory "%s" is successfully created !'%path)
        return True
    else:
#        print('The directory "%s" is already exists!'%path)
#        return False
        pass

def savetxt(path, FileName, File):
    """Save the .txt files using np.savetxt() funtion

    path : the path of the file to be saved
    FileName : the name of the file to be saved
    File : the file to be saved
    """
    mkdir(path)
    np.savetxt(path + '/' + FileName + '.txt', File)

def savedat(path, FileName, File):
    """Save the .dat files using np.savetxt() funtion

    path : the path of the file to be saved
    FileName : the name of the file to be saved
    File : the file to be saved
    """
    mkdir(path)
    np.savetxt(path + '/' + FileName + '.dat', File)

def savenpy(path, FileName, File, dtype=np.float32):
    """Save an array to a binary file in NumPy .npy format using np.save() function

    path : the path of the file to be saved
    FileName : the name of the file to be saved
    File : the file to be saved
    """
    mkdir(path)
    File = File.astype(dtype)
    np.save(path + '/' + FileName + '.npy', File)


d = {"a":1,"b":2,"c":3} # 一个字典
a = iter(d) # 转换成迭代器,相当于多了一个__next__方法,其它不变.
# 如果少了上面那一句,则无法

while True:
    try:
        print(a.__next__())
    except StopIteration:
        break
# 这个时候并不会继续迭代,为啥呢?
    # 可能是现在的__next__方法已经指向异常了,因此就不能再迭代了
    # 因此如果需要多此使用的话,需要先将原来的迭代器删除,然后再创建一个
del a
a = iter(d)
for key in a:
    print(key)

01.可迭代对象的概念.py


#!/usr/bin/env python
import os
import string
import random
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import glob

import numpy as np

def iterate_lines_over_file(filename):
    with open(filename) as file:
        print(filename)
        for line in file:
            line = line.strip()
            if len(line) < 2:
                continue
            yield line

def iterate_lines(root):
    filenames = [root]
    if os.path.isdir(root):
        filenames = glob.glob(os.path.join(root, '**/*'), recursive=True)

    for filename in filenames:
        for val in iterate_lines_over_file(filename):
            yield val

if __name__ == '__main__':
    parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument('dev_dir')
    parser.add_argument('out_dir')
    parser.add_argument('--num', default=-1, type=int)

    args = parser.parse_args()
    random.seed(0)

    count = sum(1 for _ in iterate_lines(args.dev_dir))
    allowed = None
    if args.num > 0 and args.num < count:
        allowed = set(np.random.choice(count, args.num, replace=False))

    with open(os.path.join(args.out_dir, 'input.txt'), 'w+') as input_file, \
            open(os.path.join(args.out_dir, 'answer.txt'), 'w+') as ans_file:
        for i, line in enumerate(iterate_lines(args.dev_dir)):
            if allowed is not None and i not in allowed:
                continue
            keep = random.randint(2, len(line))
            line = line[:keep]
            input_file.write(line[:-1] + '\n')
            ans_file.write(line[-1] + '\n')


from ...Types.PrimitiveTypes.Node import Node
import logging
import xxhash

class Operation(Node):
    allowed_parent_label = ['Expression', 'Instruction']
    logger = logging.getLogger(__name__)

    def __init__(self, bn_object, self_uuid, parent_uuid, parent_node_label, parent_node_hash):
        self.bn_operation_object = bn_object

        if parent_node_label in self.allowed_parent_label:
            super().__init__(self.get_hash(), self_uuid, parent_uuid, parent_node_label, 'Operation',
                             parent_node_hash, {})
        else:
            self.logger.error(f'parent_node_label not allowed: {parent_node_label}')
        self.set_node_attributes()

    def get_hash(self):
        hash_digest = xxhash.xxh64()
        hash_digest.update(self.bn_operation_object.operation.name)
        return hash_digest.hexdigest()

    def set_node_attributes(self):
        node_attributes_dict = dict()
        node_attributes_dict.update({'Operation': self.bn_operation_object.operation})

        self.add_node_attributes(node_attributes_dict)

    def __str__(self):
        return super().__str__()


import random

picks = int(input("How many quick picks? "))
MIN = 1
MAX = 45

tgt = []
for x in range(0, picks, 1):
    tgt = []
    for num in range(0, 6, 1):
        value = random.randint(MIN, MAX)
        if value not in tgt:
            tgt.append(value)
    tgt.sort()
    print(" ".join(map(str, tgt)))


from nose.tools import assert_true
import os
import mock
import jsonbender
from validate.miflowcyt_validate import FlowRepoClient

map_file = os.path.join(os.path.dirname(__file__),
                        "../tests/data/MiFlowCyt/experiment_mapping.json")
base_schema = "experiment_schema.json"
range_limit = 1

class TestFlowRepoClient(object):

    @classmethod
    def setup_class(cls):
        cls.client = FlowRepoClient(map_file, base_schema, "this is a fake ID")
        cls.mock_request_patcher = mock.patch('validate.miflowcyt_validate.requests.request')
        cls.mock_request = cls.mock_request_patcher.start()

        cls.mock_xmljson_patcher = mock.patch('validate.miflowcyt_validate.xmljson.parker.data')
        cls.mock_xmljson = cls.mock_xmljson_patcher.start()

        cls.mock_etree_patcher = mock.patch('validate.miflowcyt_validate.elemTree.fromstring')
        cls.mock_etree = cls.mock_etree_patcher.start()

    @classmethod
    def teardown_class(cls):
        cls.mock_request_patcher.stop()
        cls.mock_xmljson_patcher.stop()
        cls.mock_etree_patcher.stop()

    def test_get_mapping(self):
        mapping = self.client.get_mapping(map_file)
        assert_true(isinstance(mapping['date'], jsonbender.selectors.OptionalS))
        assert_true(isinstance(mapping['keywords'], jsonbender.selectors.OptionalS))
        assert_true(isinstance(mapping['other'], jsonbender.selectors.OptionalS))
        assert_true(isinstance(mapping['primaryContact'], jsonbender.selectors.S))
        assert_true(isinstance(mapping['organization'], jsonbender.selectors.F))
        assert_true(isinstance(mapping['purpose'], jsonbender.selectors.OptionalS))
        assert_true(isinstance(mapping['qualityControlMeasures'], jsonbender.selectors.OptionalS))
        assert_true(isinstance(mapping['conclusions'], jsonbender.selectors.K))
        assert_true(isinstance(mapping['experimentVariables'], jsonbender.selectors.OptionalS))

        map_file_error = os.path.join(os.path.dirname(__file__),
                                      "../tests/data/MiFlowCyt/_mapping.json")
        error = self.client.get_mapping(map_file_error)
        assert_true(isinstance(error, Exception))

    def test_grab_user_content(self):
        self.mock_request.return_value.status_code = 200
        response = self.client.grab_user_content(self.client.clientID)
        assert_true(response.status_code == 200)

    def test_get_user_content_id(self):
        self.mock_request.return_value.status_code = 200
        self.mock_xmljson.return_value = {
            "public-experiments": {
                "experiment": [
                    {"id": "123"},
                    {"id": "456"}
                ]
            }
        }
        ids = self.client.get_user_content_id(self.client.clientID)
        assert_true('123' in ids)
        assert_true('456' in ids)

    def test_grab_experiment_from_api(self):
        self.mock_request.return_value.status_code = 200
        item_metadata = self.client.grab_experiment_from_api(self.client.clientID, "123")
        assert_true(item_metadata.status_code == 200)

        self.mock_request.return_value.status_code = 404
        item_metadata = self.client.grab_experiment_from_api(self.client.clientID, "123")
        assert_true(item_metadata.status_code == 404)

    def test_validate_instance_from_file(self):
        validation = self.client.validate_instance_from_file({"test": "test"}, "test",
                                                             "test.test")
        assert_true(isinstance(validation, Exception))

    def test_make_validation(self):
        self.mock_request.return_value.status_code = 401
        validation = self.client.make_validation(1)
        assert_true(isinstance(validation, Exception))


# cmt
from cmt.rig.meshretarget import *
import cmt.shortcuts as shortcuts

# maya
import pymel.core as pm
import maya.api.OpenMaya as OpenMaya
import maya.cmds as cmds

# numpy
import numpy as np

# scipy
from scipy.spatial.distance import cdist

#
import math
import time

guides = ("body_C0_root",
"shoulder_L0_root",
"shoulder_R0_root",
"shoulder_L0_tip",
"shoulder_R0_tip",
"arm_L0_elbow",
"arm_R0_elbow",
"arm_L0_wrist",
"arm_R0_wrist",
"arm_L0_eff",
"arm_R0_eff",
"meta_L0_root",
"meta_R0_root",
"meta_L0_0_loc",
"meta_R0_0_loc",
"meta_L0_1_loc",
"meta_R0_1_loc",
"meta_L0_2_loc",
"meta_R0_2_loc",
"thumbRoll_L0_root",
"thumbRoll_R0_root",
"thumb_L0_0_loc",
"thumb_R0_0_loc",
"thumb_L0_1_loc",
"thumb_R0_1_loc",
"thumb_L0_2_loc",
"thumb_R0_2_loc",
"finger_L0_root",
"finger_R0_root",
"finger_L0_0_loc",
"finger_R0_0_loc",
"finger_L0_1_loc",
"finger_R0_1_loc",
"finger_L0_2_loc",
"finger_R0_2_loc",
"finger_L1_root",
"finger_R1_root",
"finger_L1_0_loc",
"finger_R1_0_loc",
"finger_L1_1_loc",
"finger_R1_1_loc",
"finger_L1_2_loc",
"finger_R1_2_loc",
"finger_L2_root",
"finger_R2_root",
"finger_L2_0_loc",
"finger_L2_0_loc",
"finger_L2_1_loc",
"finger_L2_1_loc",
"finger_L2_2_loc",
"finger_L2_2_loc",
"finger_L3_root",
"finger_L3_0_loc",
"finger_L3_0_loc",
"finger_L3_1_loc",
"finger_L3_1_loc",
"finger_L3_2_loc",
"finger_L3_2_loc",
"leg_L0_root",
"leg_R0_root",
"leg_L0_knee",
"leg_R0_knee",
"leg_L0_ankle",
"leg_R0_ankle",
"foot_L0_heel",
"foot_R0_heel",
"foot_L0_inpivot",
"foot_R0_inpivot",
"foot_L0_outpivot",
"foot_R0_outpivot",
"foot_L0_0_loc",
"foot_R0_0_loc",
"leg_L0_eff",
"leg_R0_eff",
"foot_L0_1_loc",
"foot_R0_1_loc",)

def retarget(source, target, rbf=None, radius=0.5, stride=1):
    """Run the mesh retarget.
    :param source: Source mesh
    :param target: Modified source mesh
    :param shapes: List of meshes to retarget
    :param rbf: One of the RBF functions. See class RBF
    :param radius: Smoothing parameter for the rbf
    :param stride: Vertex stride to sample on the source mesh.  Increase to speed up
    the calculation but less accurate.
    """
    start_time = time.time()
    source_points = points_to_np_array(source, stride)
    target_points = points_to_np_array(target, stride)

    if rbf is None:
        rbf = RBF.linear
    weights = get_weight_matrix(source_points, target_points, rbf, radius)

    points = get_guide_points()
    n_points = points.shape[0]
    dist = get_distance_matrix(points, source_points, rbf, radius)
    identity = np.ones((n_points, 1))
    h = np.bmat([[dist, identity, points]])
    deformed = np.asarray(np.dot(h, weights))
    points = [OpenMaya.MPoint(*p) for p in deformed]
    set_guide_points(points)

    end_time = time.time()
    print("Transferred in {} seconds".format(end_time - start_time))

def get_guide_points(stride=1):
    points = [ pm.xform(guide, query=True, translation=True, worldSpace=True) for guide in guides]
    sparse_points = [OpenMaya.MPoint(p) for p in points][::stride]
    np_points = np.array([[p.x, p.y, p.z] for p in sparse_points])
    return np_points

def set_guide_points(points):
    for index, point in enumerate(points):
        pm.xform(guides[index], translation=point, worldSpace=True)

retarget(pm.ls(sl=1)[0], pm.ls(sl=1)[1])


from ClaseVehiculo import Vehiculo
import datetime

class Usado(Vehiculo):
    __marca=""
    __patente=""
    __año=""
    __kilometraje=0

    def __init__(self, modelo, puertas, color, preciobase,marca,patente,año,kilometraje):
        super().__init__(modelo, puertas, color, preciobase)
        self.__marca=marca
        self.__patente=patente
        self.__año=año
        self.__kilometraje=kilometraje
    def getPatente(self):
        return self.__patente

    def getPuertas(self):
        return super().getPuertas()
    def getPrecioBase(self):
        return super().getPrecioBase()
    def getModelo(self):
        return super().getModelo()
    def getColor(self):
        return super().getColor()

    def toJson(self):
        d=dict(
            __class__=self.__class__.__name__,
            __atributos__=dict(
                modelo=self.getModelo(),
                puertas=self.getPuertas(),
                color=self.getColor(),
                preciobase=self.getPrecioBase(),
                marca=self.__marca,
                patente=self.__patente,
                año=self.__año,
                kilometraje=self.__kilometraje
            )
        )
        return d

    def CalcPrecioVenta(self):
        km=0
        antig=int(datetime.date.today().year) - int(self.__año)
        porc=float((super().getPrecioBase() * 1)/100)
        if(int(self.__kilometraje)>100000):
            km=float((super().getPrecioBase() * 2)/100)
        total=float(super().getPrecioBase())-porc-antig-km
        return total
    def __str__(self):
        super().__str__()
        print('Marca: {}'.format(self.__marca))
        print('Patente: {}'.format(self.__patente))
        print('Año: {}'.format(self.__año))
        print('Kilometros: {}'.format(self.__kilometraje))
        return ""


from enum import Enum
from typing import List

class PyWiFiConstant(Enum):
    IFACE_DISCONNECTED = 0
    IFACE_SCANNING = 1
    IFACE_INACTIVE = 2
    IFACE_CONNECTING = 3
    IFACE_CONNECTED = 4

    # Define auth algorithms.
    AUTH_ALG_OPEN = 0
    AUTH_ALG_SHARED = 1

    # Define auth key mgmt types.
    AKM_TYPE_NONE = 0
    AKM_TYPE_WPA = 1
    AKM_TYPE_WPAPSK = 2
    AKM_TYPE_WPA2 = 3
    AKM_TYPE_WPA2PSK = 4
    AKM_TYPE_UNKNOWN = 5

    # Define ciphers.
    CIPHER_TYPE_NONE = 0
    CIPHER_TYPE_WEP = 1
    CIPHER_TYPE_TKIP = 2
    CIPHER_TYPE_CCMP = 3
    CIPHER_TYPE_UNKNOWN = 4

    KEY_TYPE_NETWORKKEY = 0
    KEY_TYPE_PASSPHRASE = 1

class PyWiFiUtil:
    def _send_cmd_to_wpas(self, iface, cmd, get_reply=False):
        pass

class PyWiFiProfile:
    def __init__(self, profile):
        self.id = profile["id"]
        self.auth = profile["auth"]
        self.akm = profile["akm"]
        self.cipher = profile["cipher"]
        self.ssid = profile["ssid"]
        self.bssid = profile["bssid"]
        self.key = profile["key"]
        self.freq = profile["freq"]
        self.signal = profile["signal"]

class PyWiFiInterfaceMock:
    state = PyWiFiConstant.IFACE_INACTIVE
    results: List[PyWiFiProfile] = []
    profiles: List[PyWiFiProfile] = []

    def __init__(self):
        self._wifi_ctrl = PyWiFiUtil()

    def name(self):
        return "wlan0"

    def disconnect(self):
        self.state = PyWiFiConstant.IFACE_DISCONNECTED

    def status(self):
        return self.state.value

    def connect(self, profile):
        self.state = PyWiFiConstant.IFACE_CONNECTED

    def scan(self):
        self.state = PyWiFiConstant.IFACE_SCANNING

        self.profiles = [
            {
                "id": 0,
                "auth": 0,
                "akm": [2, 4],
                "cipher": 0,
                "ssid": "Depto 606-5G",
                "bssid": "e0:cc:7a:fd:84:50",
                "key": None,
                "freq": 5220,
                "signal": -70,
            },
            {
                "id": 0,
                "auth": 0,
                "akm": [2, 4],
                "cipher": 0,
                "ssid": "Depto 606",
                "bssid": "e0:cc:7a:fd:84:4c",
                "key": None,
                "freq": 2447,
                "signal": -52,
            },
            {
                "id": 0,
                "auth": 0,
                "akm": [2, 4],
                "cipher": 0,
                "ssid": "VTR-5737196",
                "bssid": "c0:05:c2:68:46:69",
                "key": None,
                "freq": 2437,
                "signal": -57,
            },
            {
                "id": 0,
                "auth": 0,
                "akm": [2, 4],
                "cipher": 0,
                "ssid": "VTR-5737196",
                "bssid": "c0:05:c2:68:46:6f",
                "key": None,
                "freq": 5805,
                "signal": -81,
            },
            {
                "id": 0,
                "auth": 0,
                "akm": [2, 4],
                "cipher": 0,
                "ssid": "VTR-2049450",
                "bssid": "e4:57:40:99:ee:15",
                "key": None,
                "freq": 5765,
                "signal": -83,
            },
            {
                "id": 0,
                "auth": 0,
                "akm": [2, 4],
                "cipher": 0,
                "ssid": "VTR-7138797 - 5G",
                "bssid": "e4:57:40:35:da:0b",
                "key": None,
                "freq": 5745,
                "signal": -86,
            },
            {
                "id": 0,
                "auth": 0,
                "akm": [2, 4],
                "cipher": 0,
                "ssid": "VTR-3847319",
                "bssid": "40:0d:10:eb:e4:77",
                "key": None,
                "freq": 5180,
                "signal": -90,
            },
            {
                "id": 0,
                "auth": 0,
                "akm": [2, 4],
                "cipher": 0,
                "ssid": "Paraya_5G",
                "bssid": "e4:57:40:92:22:15",
                "key": None,
                "freq": 5240,
                "signal": -90,
            },
            {
                "id": 0,
                "auth": 0,
                "akm": [2, 4],
                "cipher": 0,
                "ssid": "Martina -1-5G",
                "bssid": "18:35:d1:20:98:5f",
                "key": None,
                "freq": 5200,
                "signal": -92,
            },
            {
                "id": 0,
                "auth": 0,
                "akm": [],
                "cipher": 0,
                "ssid": "Free internet!",
                "bssid": "58:55:31:10:48:3a",
                "key": None,
                "freq": 5200,
                "signal": -92,
            },
        ]

        self.results = [PyWiFiProfile(profile) for profile in self.profiles]

        self.state = PyWiFiConstant.IFACE_INACTIVE

    def scan_results(self):
        return self.results

    def remove_all_network_profiles(self):
        self.state = PyWiFiConstant.IFACE_INACTIVE
        self.profiles = []

    def bssid_connected(self):
        return ""

    def add_network_profile(self, profile):
        self.profiles.append(profile)

    def network_profiles(self):
        return self.profiles

class PyWiFiInstanceMock:
    def interfaces(self):
        return [PyWiFiInterfaceMock()]

class PyWiFiMock:
    const = PyWiFiConstant

    def PyWiFi(self):
        return PyWiFiInstanceMock()


from arraystack import ArrayStack
import copy

def queue_to_stack(queue):
    stack = ArrayStack()
    new_queue = copy.deepcopy(queue)
    while not new_queue.isEmpty():
        stack.add(new_queue.pop())
    return stack


import numpy as np

def MetEuler(x0, y0, x2):

 del2 = .001
 x = np.arange(x0, x2, del2)
 y = [y0]
 npun = len(x)

 y1 = y0
 for i in range(npun-1):
   y2 = y1 + del2 * 3 * y1
   y.append(y2)
   y1 = y2

 return {'x': x, 'y':y}