import pandas as pd
from keras.utils import to_categorical
import numpy as np
import time
import gc

train = pd.read_csv('mnist.csv')
np.random.seed(2099)
index = np.random.permutation(train.shape[0])
train_index = index[0: 40000]
test_index = index[40000: 42000]
test = train.iloc[test_index].copy()
train = train.iloc[train_index]
train_label = train["label"].copy()
test_label = test["label"].copy()
del train["label"], train_index, test["label"], test_index, index
gc.collect()

train = np.array(train)
test = np.array(test)
train_label = np.array(train_label)
train_label = to_categorical(train_label, num_classes=10, dtype="int")
test_label = np.array(test_label)
test_label = to_categorical(test_label, num_classes=10, dtype="int")

def soft_max(z):
    """
    :param z: input, an p*n matrix
    :return: p*n matrix
    """
    e = np.exp(z)
    total = np.sum(e, axis=0, keepdims=True)
    weight = e / total
    return weight

def accuracy(y, y_hat):
    y = np.argmax(y, axis=0)
    y_hat = np.argmax(y_hat, axis=0)
    return sum(y == y_hat) / len(y)

def loss(y, y_hat):
    """
    :param y: the ture value p*n matrix
    :param y_hat: the predicted value
    :return: minimizing loss is the same as maximizing likelihood function, so we spare np.log
    """
    n = y.shape[1]
    tmp = y_hat**y
    tmp = -np.log(tmp.prod(axis=0)).sum()/n
    return tmp

def leaky_relu(x, k=0.3):
    return (x > 0) * x + k * (x < 0) * x

def d_leaky_relu(x, k=0.3):
    return (x > 0) + k * (x < 0)

def init_w(b, a):
    w = np.random.randn(a * b)
    w = np.reshape(w, [b, a])
    return w

def init_b(b):
    b = np.zeros([b, 1])
    return b

def forward(x, parameter, cache):
    """
    :param x:input data p*n matrix
    :param parameter: a dict storing parameters
    :param cache: a dict storing computation result of each layer
    :return: the predicted value
    """
    cache['C1'] = np.dot(parameter['W1'], x) + parameter['b1']
    cache['A1'] = leaky_relu(cache['C1'])
    cache['C2'] = np.dot(parameter['W2'], cache['A1']) + parameter['b2']
    cache['A2'] = leaky_relu(cache['C2'])
    cache['C3'] = np.dot(parameter['W3'], cache['A2']) + parameter['b3']
    cache['A3'] = soft_max(cache['C3'])
    return cache

def back_propagation(x, y, parameter, cache, step):
    """
    X 784*n / Y 10*n
    dW1 W1 800*784, db1 b1 800*1, A1 C1, 800*n
    dW2 W2 400*800, db2 b2 400*1, A2 C2, 400*n
    dW3 W3 10*400, db3 b3 10*1, A3 C3, 10*n
    :param y: true value
    :param parameter: dictionary storing all parameters
    :param cache: dictionary storing all the computation in process
    :param step: learning rate
    :return: updated parameters
    """
    number = y.shape[1]
    cache['dC3'] = cache['A3'] - y  # 10*n
    # cache['dC3'] = (cache['A3'] - y)*y  # 10*n
    cache['dW3'] = np.dot(cache['dC3'], cache['A2'].T) / number  # 10*400
    cache['db3'] = np.sum(cache['dC3'], axis=1, keepdims=True) / number  # 10*1
    parameter['W3'] = parameter['W3'] - step * cache['dW3']  # 10*400
    parameter['b3'] = parameter['b3'] - step * cache['db3']  # 10*1

    cache['dC2'] = np.dot(parameter['W3'].T, cache['dC3']) * d_leaky_relu(cache['C2'])  # 400*n
    cache['dW2'] = np.dot(cache['dC2'], cache['A1'].T) / number  # 400*800
    cache['db2'] = np.sum(cache['dC2'], axis=1, keepdims=True) / number  # 400*1
    parameter['W2'] = parameter['W2'] - step * cache['dW2']  # 400*800
    parameter['b2'] = parameter['b2'] - step * cache['db2']  # 400*1

    cache['dC1'] = np.dot(parameter['W2'].T, cache['dC2']) * d_leaky_relu(cache['C1'])  # 800*n
    cache['dW1'] = np.dot(cache['dC1'], x.T) / number  # 800*784
    cache['db1'] = np.sum(cache['dC1'], axis=1, keepdims=True)  # 800*1
    parameter['W1'] = parameter['W1'] - step * cache['dW1']  # 800*784
    parameter['b1'] = parameter['b1'] - step * cache['db1']  # 800*1
    return cache, parameter

def fit(x, y, learning_rate=0.001, batch_size=128, epoch=5):
    """
    :param x: training data
    :param y: training label
    :param learning_rate: the length of a step
    :param batch_size: numbers of samples we train in a round
    :param epoch: rounds we train through training data
    :return: a trained set of parameters
    """
    parameter = dict()
    nx = x.shape[1]
    parameter['W1'] = init_w(800, 784) / 100
    parameter['b1'] = init_b(800)
    parameter['W2'] = init_w(400, 800) / 100
    parameter['b2'] = init_b(400)
    parameter['W3'] = init_w(10, 400) / 100
    parameter['b3'] = init_b(10)

    index = np.array([], dtype='int')
    for i in range(0, nx, batch_size):
        index = np.append(index, i)
    index = np.append(index, nx)

    cache = dict()
    for i in range(0, epoch):
        shuffle = np.random.permutation(nx)
        for j in range(0, int(nx / batch_size) + 1):
            one_batch_x = x[:, shuffle[index[j]:index[j + 1]]]
            one_batch_y = y[:, shuffle[index[j]:index[j + 1]]]
            cache = forward(one_batch_x, parameter, cache)
            prob = loss(one_batch_y, cache['A3'])
            acc = accuracy(one_batch_y, cache['A3'])
            print(str(i) + '--' + str(j) + '--' + str(index[j + 1]))
            print('loss' + str(prob))
            print('accuracy' + str(acc))
            cache, parameter = back_propagation(one_batch_x, one_batch_y, parameter, cache, step=learning_rate)
    return cache, parameter

tic = time.clock()
cache, parameter = fit(x=train.T, y=train_label.T, epoch=5)
toc = time.clock()
print('time spent:' + str(toc - tic))

cache = forward(test.T, parameter, cache)
acc = accuracy(test_label.T, cache['A3'])
print(acc)


import csv
import time
import datetime

sentiment_total = 0
tweet_with_query_count = 0

# this part turns the csv file into a list of list of tweets
tweet_data = open('tweets (x=0) opening.csv')
spam_reader = csv.reader(tweet_data)
list_of_tweets = list(spam_reader)

date_list = ["12/8/16","12/12/16","12/13/16","12/14/16","12/15/16","12/19/16",
             "12/20/16","12/21/16","12/22/16","12/26/16","12/27/16","12/28/16",
             "12/29/16","1/3/17","1/4/17","1/5/17","1/9/17","1/10/17","1/11/17",
             "1/12/17","1/16/17","1/17/17","1/18/17","1/19/17","1/23/17",
             "1/24/17","1/25/17","1/26/17","1/30/17","1/31/17","2/1/17",
             "2/2/17","2/4/17","2/5/17","2/6/17","2/7/17","2/8/17","2/9/17",
             "2/13/17","2/15/17","2/16/17","2/20/17","2/21/17","2/22/17",
             "2/23/17","2/27/17","2/28/17","3/1/17","3/2/17","3/6/17","3/7/17",
             "3/8/17","3/9/17","3/13/17","3/14/17","3/15/17","3/16/17",
             "3/20/17","3/21/17","3/22/17","3/23/17","3/27/17","3/28/17",
             "3/29/17","3/30/17","4/3/17","4/4/17","4/5/17","4/6/17","4/10/17",
             "4/11/17","4/12/17","4/13/17","4/17/17","4/18/17","4/19/17",
             "4/20/17","4/24/17","4/25/17","4/26/17","4/27/17","5/1/17",
             "5/2/17","5/3/17","5/4/17","5/8/17","5/9/17","5/10/17","5/11/17",
             "5/15/17","5/16/17","5/17/17","5/18/17","5/22/17"]

# list of all the companies
companies = ['$MMM', '3M', '$AXP', 'American Express', '$AAPL', 'Apple', '$BA', 'Boeing',
            '$CAT', 'Caterpillar', '$CVX', 'Chevron', '$CSCO', 'Cisco', '$KO', 'Coca-Cola',
            '$DIS', 'Disney', '$DD', 'DuPont', '$XOM', 'Exxon Mobil', '$GE', 'General Electric',
            '$GS', 'Goldman Sachs', '$HD', 'Home Depot', '$IBM', 'IBM', '$INTC', 'Intel',
            '$JNJ', 'Johnson & Johnson', '$JPM', 'JPMorgan Chase', '$MCD', 'McDonald\'s',
            '$MRK', 'Merck', '$MSFT', 'Microsoft', '$NKE', 'Nike', '$PFE', 'Pfizer',
            '$PG', 'Procter & Gamble', '$TRV', 'Travelers Companies Inc', '$UTX',
            'United Technologies', '$UNH', 'UnitedHealth', '$VZ', 'Verizon', '$V', 'Visa',
            '$WMT', 'Wal-Mart']

cashtags = ['$MMM', '$AXP', '$AAPL', '$BA', '$CAT',  '$CVX', '$CSCO',
            '$KO', '$DIS', '$DD', '$XOM',  '$GE',  '$GS', '$HD', '$IBM', '$INTC',
            '$JNJ',  '$JPM', '$MCD',  '$MRK',  '$MSFT', '$NKE',  '$PFE', '$PG',
            '$TRV', '$UTX', '$UNH',  '$VZ', '$V',  '$WMT']

def company_in_cashtag(company):
    return company in cashtags

with open ('wekaized_tweets (x=0) opening weekend-fix.csv', 'ab') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=',',
                            quotechar='"', quoting=csv.QUOTE_MINIMAL)

    spamwriter.writerow(["date", "cashtag?", "average sentiment",
                         "price change (%)", "increase/decrease"])

    # Iterate over the list of companies
    for company in companies:

        # Iterate over the date covered
        for date in date_list:

            # Iterate over all the tweets in the csv file
            for tweet in list_of_tweets:
                if tweet[0] == date and tweet[1] == company:

                    cashtag_indicator = tweet[2]
                    ## if the cashtag indicator is 1 and the query (company) is cashtag
                    if tweet[4] != '':
                        if cashtag_indicator == 1 and company_in_cashtag(company):
                            sentiment_total += float(tweet[3])
                            tweet_with_query_count += 1
                            price_change = float(tweet[4])
                            cashtag_indicator = tweet[2]
                        ## if the cashtag indicator is 0 and the query (company) is not a cashtag
                        elif cashtag_indicator == 0 and (not company_in_cashtag(company)):
                            sentiment_total += float(tweet[3])
                            tweet_with_query_count += 1
                            price_change = float(tweet[4])
                            cashtag_indicator = tweet[2]
                        else:
                            sentiment_total += float(tweet[3])
                            tweet_with_query_count += 1
                            price_change = float(tweet[4])

                        # based on 0.04% avg daily increase
                        # first condition checks for on trend, but allows .02 uncertainty
                        if 'price_change' in locals():
                            if (price_change > 0.02 and price_change < 0.06):
                                direction = "on trend"
                            # next two check if the increase is small (<= 2.5x trend)
                            elif price_change >= 0.06:
                                direction = "GT trend (inc)"
                            elif price_change < 0.02:
                                direction = "LT trend (dec)"
                    else:
                        pass

            # Once we've iterated over all tweets w/ company x and date x
            # we will then move on to date x+1

            if tweet_with_query_count != 0: # and (not bot_tweet):
                avg_sentiment = sentiment_total / tweet_with_query_count
                spamwriter.writerow([date, cashtag_indicator, avg_sentiment, price_change, direction])
                sentiment_total = 0
                tweet_with_query_count = 0


# -*- coding: utf-8 -*-
from odoo import fields, models, api, _
from odoo.exceptions import ValidationError
import odoo.addons.decimal_precision as dp
import json
import uuid

class QualityPoint(models.Model):
    _inherit = "sa.quality.point"

    active = fields.Boolean(
        'Active', default=True,
        help="If the active field is set to False, it will allow you to hide Quality Check Point without removing it.")

    tolerance_min = fields.Float('Torque Min Tolerance', digits=dp.get_precision('Quality Tests'), default=0.0)
    tolerance_max = fields.Float('Torque Max Tolerance', digits=dp.get_precision('Quality Tests'), default=0.0)

    tolerance_min_degree = fields.Float('Degree Min Tolerance', digits=dp.get_precision('Quality Tests'), default=0.0)
    tolerance_max_degree = fields.Float('Degree Max Tolerance', digits=dp.get_precision('Quality Tests'), default=0.0)

    bom_line_id = fields.Many2one('mrp.bom.line', ondelete='cascade')

    # parent_id = fields.Many2one('sa.quality.point', ondelete='cascade')

    operation_id = fields.Many2one('mrp.routing.workcenter', index=True)

    max_redo_times = fields.Integer('Operation Max Redo Times', default=3)  # 此项重试业务逻辑在HMI中实现

    operation_point_ids = fields.One2many('operation.point', 'parent_qcp_id', string='Quality Points(Tightening Point)')

    operation_id_domain = fields.Char(
        compute="_compute_operation_id_domain",
        readonly=True,
        store=False,
    )

    _sql_constraints = [
        ('product_bom_line_id_uniq', 'unique(bom_line_id)', 'Only one quality point per product bom line is allowed')]

    @api.model
    def default_get(self, fields):
        res = super(QualityPoint, self).default_get(fields)

        operation_id = self.env.context.get('default_operation_id')
        if operation_id:
            operation = self.env['mrp.routing.workcenter'].sudo().browse(operation_id)
            if 'max_redo_times' in fields:
                res.update({'max_redo_times': operation.max_redo_times})
            if 'sequence' in fields and operation.operation_point_ids:
                res.update({'sequence': max(operation.operation_point_ids.mapped('sequence')) + 1})
        return res

    @api.multi
    def name_get(self):
        res = []
        for point in self:
            res.append((point.id, _('[%s] %s') % (point.operation_id.name, point.name)))
        return res

    @api.onchange('operation_id')
    def _onchange_opeartion_id(self):
        self.ensure_one()
        bom_line_ids = self.env['mrp.bom.line'].search(
            [('bom_id.product_id', '=', self.product_id.id), ('operation_id', '=', self.operation_id.id)])
        qtys = [bom_line_id.product_qty for bom_line_id in bom_line_ids]
        self.times = sum(qtys)

    @api.constrains('product_id', 'product_tmpl_id')
    def _product_tmpl_product_constraint(self):
        if self.product_id.product_tmpl_id.id != self.product_tmpl_id.id:
            raise ValidationError('The product template "%s" is invalid on product with name "%s"' % (
            self.product_tmpl_id.name, self.product_id.name))

    @api.multi
    @api.depends('operation_id', 'product_id', 'workcenter_id')
    def _compute_operation_id_domain(self):
        for rec in self:
            operation_ids = rec.product_id.bom_ids.mapped('routing_id.operation_ids').ids or []
            rec.operation_id_domain = json.dumps(
                [('workcenter_id', '=', rec.workcenter_id.id), ('id', 'in', operation_ids)])

    @api.model
    def create(self, vals):
        # tightening_point_type = self.env.ref('quality.test_type_tightening_point').id
        # if 'test_type_id' in vals and vals.get('test_type_id') == tightening_point_type:
        #     vals.update({
        #         'name': uuid.uuid4()
        #     })
        ret = super(QualityPoint, self).create(vals)
        return ret

class QualityCheck(models.Model):
    _inherit = "sa.quality.check"

    measure_degree = fields.Float('Measure Degree', default=0.0, digits=dp.get_precision('Quality Tests'),
                                  track_visibility='onchange')

    assembly_line_id = fields.Many2one('mrp.assemblyline', string='Assembly Line ID',
                                       related='production_id.assembly_line_id', readonly=True)

    @api.one
    @api.depends('measure', 'measure_degree')
    def _compute_measure_success(self):
        if self.point_id.test_type == 'passfail':
            self.measure_success = 'none'
        else:
            if self.measure < self.point_id.tolerance_min or self.measure > self.point_id.tolerance_max:
                self.measure_success = 'fail'
            elif self.measure_degree < self.point_id.tolerance_min_degree or self.measure_degree > self.point_id.tolerance_max_degree:
                self.measure_success = 'fail'
            else:
                self.measure_success = 'pass'


print('Кравченко О.О. ІС-93 Лабораторна рoбота №5 Варіант 13')

firstNum = int(input('Enter number p: ')) # p initialization
secondNum = int(input('Enter number q: ')) # q initialization
result = [] # List with all needed dividers

def dividers (number):  # Function for searching dividers
    divArray = []
    for divider in range(number + 1): # Loop for searching number's dividers
        if number % (divider + 1) == 0: # Condition for number's divider
            divArray.append(divider + 1)
    return divArray
    # return set(divArray) # When we use code with embedded function

firstNumDividers = dividers(firstNum)  # All p's dividers
firstNumDividers.remove(1)  # Because first divider is always"1"
secondNumDividers = dividers(secondNum)  # All q's dividers
secondNumDividers.remove(1)  # Because first divider is always "1"

"""
# Code with embedded function
for element in firstNumDividers:  # Loop for searching dividers of p's divider
    elementDividers = dividers(element)
    if not elementDividers & secondNumDividers:  # Finding common dividers
        result.append(str(element))  # Add divider into list of all dividers
"""

flag = True

for element in firstNumDividers:  # Loop for searching dividers of p's divider
    elementDividers = dividers(element)
    for item in elementDividers: # Compare dividers of p's divider with q's dividers
        for number in secondNumDividers:
            if item == number: # If they have common dividers then p's divider and q aren't coprime numbers
                flag = False
                break
    if flag:
        result.append(str(element))

print('All dividers for p and coprime numbers for q:', ', '.join(result))


# 1560. 圆形赛道上经过次数最多的扇区.py
from collections import defaultdict
from typing import List

class Solution:
    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:
        counter = defaultdict(int)
        start = rounds[0]
        for nxt in rounds[1:]:
            if nxt < start:
                for i in range(start, n + 1):
                    counter[i] += 1
                for i in range(1, nxt + 1):
                    counter[i] += 1
            else:
                for i in range(start, nxt + 1):
                    counter[i] += 1
            start = nxt + 1
        max_count = max(counter.values())
        res = []
        for c in counter:
            if counter[c] == max_count:
                res.append(c)
        res.sort()
        return res

def main():
    pass

if __name__ == '__main__':
    main()

Week11/周赛/1560. 圆形赛道上经过次数最多的扇区.py

1560. 圆形赛道上经过次数最多的扇区.py


import FWCore.ParameterSet.Config as cms

from DQM.SiPixelPhase1Common.HistogramManager_cfi import *

StandardSpecifications1D.append(
    Specification(PerLayer1D).groupBy("PXBarrel|PXForward/PXLayer|PXDisk/OnlineBlock") # per-layer with history for online
                             .groupBy("PXBarrel|PXForward/PXLayer|PXDisk", "EXTEND_Y")
                             .save()
                             .custom()
                             .save()
)
StandardSpecifications1D.append(
    Specification().groupBy("PXBarrel|PXForward/OnlineBlock") # per-layer with history for online
                   .groupBy("PXBarrel|PXForward", "EXTEND_Y")
                   .save()
                   .custom()
                   .save()
)

StandardSpecifications1D_Num.append(
    Specification(PerLayer1D).groupBy("PXBarrel|PXForward/PXLayer|PXDisk/OnlineBlock/DetId/Event") # per-layer with history for online
                             .reduce("COUNT")
                             .groupBy("PXBarrel|PXForward/PXLayer|PXDisk/OnlineBlock")
                             .groupBy("PXBarrel|PXForward/PXLayer|PXDisk", "EXTEND_Y")
                             .save()
                             .custom()
                             .save()
)
StandardSpecifications1D_Num.append(
    Specification().groupBy("PXBarrel|PXForward/OnlineBlock/DetId/Event") # per-layer with history for online
                   .reduce("COUNT")
                   .groupBy("PXBarrel|PXForward/OnlineBlock")
                   .groupBy("PXBarrel|PXForward", "EXTEND_Y")
                   .save()
                   .custom()
                   .save()
)

# Configure Phase1 DQM for Phase0 data
SiPixelPhase1Geometry.n_inner_ring_blades = 24 # no outer ring

# Turn on 'online' harvesting. This has to be set before other configs are
# loaded (due to how the DefaultHisto PSet is later cloned), therefore it is
# here and not in the harvestng config.
DefaultHisto.perLumiHarvesting = True

# Pixel Digi Monitoring
from DQM.SiPixelPhase1Digis.SiPixelPhase1Digis_cfi import *
SiPixelPhase1DigisAnalyzer.src = cms.InputTag("siPixelDigis") # adapt for real data

# Cluster (track-independent) monitoring
from DQM.SiPixelPhase1Clusters.SiPixelPhase1Clusters_cfi import *

# We could overwrite the Harvesters like this, and use the custom() steps to
# perform resetting of histograms.
#SiPixelPhase1ClustersHarvester = cms.EDAnalyzer("SiPixelPhase1OnlineHarvester",
#    histograms = SiPixelPhase1ClustersConf,
#    geometry = SiPixelPhase1Geometry
#)

# Raw data errors
from DQM.SiPixelPhase1RawData.SiPixelPhase1RawData_cfi import *

PerModule.enabled = True

siPixelPhase1OnlineDQM_source = cms.Sequence(SiPixelPhase1DigisAnalyzer
                                            + SiPixelPhase1ClustersAnalyzer
                                            + SiPixelPhase1RawDataAnalyzer
                                            )

siPixelPhase1OnlineDQM_harvesting = cms.Sequence(SiPixelPhase1DigisHarvester
                                                + SiPixelPhase1ClustersHarvester
                                                + SiPixelPhase1RawDataHarvester
                                                )


import requests
from Reposter.utils.configurer import config

# https://stackoverflow.com/a/28418469
# Make sure to ask for page access token and request publish_pages option when getting the short lived token

page = "STEPHANIE"

app_id = config.get_configuration("app_id", page)
app_secret = config.get_configuration("app_secret", page)
short_token = config.get_configuration("short_token", page)

url = "https://graph.facebook.com/v2.10/oauth/access_token"

params = {
    "grant_type": "fb_exchange_token",
    "client_id": app_id,
    "client_secret": app_secret,
    "fb_exchange_token": short_token
}

response = requests.get(url, params=params)

if not response.ok:
    print(response.text)
    exit("Something bad happened")

data = response.json()

long_access_token = data['access_token']
print(long_access_token)

config.write_configuration("long_token", long_access_token, page)

accound_id_url = "https://graph.facebook.com/v2.10/me"

response = requests.get(accound_id_url, params={"access_token": long_access_token})

if not response.ok:
    print(response.text)
    exit("Something bad happened")

data = response.json()

account_id = data['id']
print(account_id)

permanent_token_url = "https://graph.facebook.com/v2.10/%s/accounts" % account_id

requests.get(permanent_token_url, params={"access_token": long_access_token})

if not response.ok:
    print(response.text)
    exit("Something bad happened")


#+
# Copyright 2015 iXsystems, Inc.
# All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted providing that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#####################################################################

import os
import re
import errno
import ipaddress
import logging
import contextlib
from freenas.dispatcher.rpc import RpcException, description, accepts, returns, generator
from freenas.dispatcher.rpc import SchemaHelper as h
from task import Provider, Task, TaskException, query, TaskDescription
from lib.system import system, SubprocessException
from bsd import kld
from freenas.utils import query as q
from freenas.utils.password import unpassword

RE_ATTRS = re.compile(r'^(?P<key>^.+?)\s+?:\s+?(?P<val>.+?)\r?$', re.M)
IPMI_ATTR_MAP = {
    'IP Address Source': 'dhcp',
    'IP Address': 'address',
    'Subnet Mask': 'netmask',
    'Default Gateway IP': 'gateway',
    '802.1q VLAN ID': 'vlan_id'
}

logger = logging.getLogger('IPMIPlugin')
channels = []

@description('Provides information about IPMI')
class IPMIProvider(Provider):
    @accepts()
    @returns(bool)
    def is_ipmi_loaded(self):
        return os.path.exists('/dev/ipmi0')

    @accepts()
    @returns(h.array(int))
    def channels(self):
        return channels

    @query('Ipmi')
    @generator
    def query(self, filter=None, params=None):
        if not self.is_ipmi_loaded():
            raise RpcException(errno.ENXIO, 'The IPMI device could not be found')

        result = []
        for channel in self.channels():
            try:
                out, err = system('/usr/local/bin/ipmitool', 'lan', 'print', str(channel))
            except SubprocessException as e:
                raise RpcException(errno.EFAULT, 'Cannot receive IPMI configuration: {0}'.format(e.err.strip()))

            raw = {k.strip(): v.strip() for k, v in RE_ATTRS.findall(out)}
            ret = {IPMI_ATTR_MAP[k]: v for k, v in list(raw.items()) if k in IPMI_ATTR_MAP}
            ret['id'] = channel
            ret['vlan_id'] = None if ret.get('vlan_id') == 'Disabled' else ret.get('vlan_id')
            ret['dhcp'] = True if ret.get('dhcp') == 'DHCP Address' else False
            result.append(ret)

        return q.query(result, *(filter or []), stream=True, **(params or {}))

@accepts(int, h.ref('Ipmi'))
@description("Configures IPMI module")
class ConfigureIPMITask(Task):
    @classmethod
    def early_describe(cls):
        return 'Configuring IPMI module'

    def describe(self, id, updated_params):
        config = self.dispatcher.call_sync('ipmi.query', [('id', '=', id)], {'single': True})
        return TaskDescription('Configuring {name} IPMI module', name=config.get('address', '') if config else '')

    def verify(self, id, updated_params):
        return ['system']

    def run(self, id, updated_params):
        if not self.dispatcher.call_sync('ipmi.is_ipmi_loaded'):
            raise TaskException(errno.ENXIO, 'No IPMI module loaded')

        if id not in self.dispatcher.call_sync('ipmi.channels'):
            raise TaskException(errno.ENXIO, 'Invalid channel')

        config = self.dispatcher.call_sync('ipmi.query', [('id', '=', id)], {'single': True})
        config.update(updated_params)
        channel = str(id)

        if updated_params.get('gateway') is None:
            config['gateway'] = '0.0.0.0'

        try:
            if config['dhcp']:
                system('/usr/local/bin/ipmitool', 'lan', 'set', channel, 'ipsrc', 'dhcp')
            else:
                system('/usr/local/bin/ipmitool', 'lan', 'set', channel, 'ipsrc', 'static')
                system('/usr/local/bin/ipmitool', 'lan', 'set', channel, 'ipaddr', config['address'])
                system('/usr/local/bin/ipmitool', 'lan', 'set', channel, 'netmask', cidr_to_netmask(config['netmask']))
                system('/usr/local/bin/ipmitool', 'lan', 'set', channel, 'defgw', 'ipaddr', config['gateway'])

            vlan_id = config['vlan_id'] if config.get('vlan_id') else 'off'
            system('/usr/local/bin/ipmitool', 'lan', 'set', channel, 'vlan', 'id', str(vlan_id))
            system('/usr/local/bin/ipmitool', 'lan', 'set', channel, 'access', 'on')
            system('/usr/local/bin/ipmitool', 'lan', 'set', channel, 'auth', 'USER', 'MD2,MD5')
            system('/usr/local/bin/ipmitool', 'lan', 'set', channel, 'auth', 'OPERATOR', 'MD2,MD5')
            system('/usr/local/bin/ipmitool', 'lan', 'set', channel, 'auth', 'ADMIN', 'MD2,MD5')
            system('/usr/local/bin/ipmitool', 'lan', 'set', channel, 'auth', 'CALLBACK', 'MD2,MD5')

            with contextlib.suppress(SubprocessException):
                system('/usr/local/bin/ipmitool', 'lan', 'set', channel, 'arp', 'respond', 'on')

            with contextlib.suppress(SubprocessException):
                system('/usr/local/bin/ipmitool', 'lan', 'set', channel, 'arp', 'generate', 'on')

            if 'password' in updated_params:
                system('/usr/local/bin/ipmitool', 'user', 'set', 'password', '2', unpassword(updated_params['password']))
                system('/usr/local/bin/ipmitool', 'user', 'enable', '2')

        except SubprocessException as err:
            raise TaskException(errno.EFAULT, 'Cannot configure IPMI channel {0}: {1}'.format(channel, err.err))

def cidr_to_netmask(cidr):
    iface = ipaddress.ip_interface('0.0.0.0/{0}'.format(cidr))
    return str(iface.netmask)

def _init(dispatcher, plugin):
    plugin.register_schema_definition('Ipmi', {
        'type': 'object',
        'additionalProperties': False,
        'properties': {
            'id': {'type': 'integer'},
            'password': {'type': 'password'},
            'dhcp': {'type': 'boolean'},
            'address': {'$ref': 'Ipv4Address'},
            'netmask': {'type': 'integer'},
            'gateway': {
                'anyOf': [
                    {'$ref': 'Ipv4Address'},
                    {'type': 'null'}
                ]
            },
            'vlan_id': {'type': ['integer', 'null']}
        }
    })

    plugin.register_provider('ipmi', IPMIProvider)
    plugin.register_task_handler('ipmi.update', ConfigureIPMITask)

    # Load ipmi kernel module
    try:
        kld.kldload('/boot/kernel/ipmi.ko')
    except OSError as err:
        if err.errno != errno.EEXIST:
            logger.warning('Cannot load IPMI module: %s', str(err))
            logger.warning('IPMI unavailable')
            return

    # Scan available channels
    for i in range(1, 17):
        try:
            system('/usr/local/bin/ipmitool', 'lan', 'print', str(i))
        except SubprocessException:
            continue

        channels.append(i)


# This file simulates the randomly selection of the server.
# The one choice and two choices are both implemented
# in this file that runs Simulator1 and Simulator2.
from __future__ import division
import math
#import matplotlib.pyplot as plt
#import networkx as nx
from multiprocessing import Pool
import sys
import numpy as np
import scipy.io as sio
import time
#import simplejson as json
#import csv
#import string
#from BallsBins import *
#from BallsBins.Server import Server
from BallsBins.Simulator1 import Simulator1
from BallsBins.Simulator1 import Simulator1_lowmem
from BallsBins.Simulator2 import Simulator2
from BallsBins.Simulator2 import Simulator2_lowmem

#--------------------------------------------------------------------
#log = math.log
#sqrt = math.sqrt

#--------------------------------------------------------------------
# Simulation parameters

# Choose the simulator. It can be the following values:
# 'one choice'
# 'one choice, low mem'
# 'two choice'
# 'two choice, low mem'
#simulator = 'one choice'
#simulator = 'one choice, low mem'
#simulator = 'two choice'
simulator = 'two choice, low mem'

# Base part of the output file name
base_out_filename = 'CacheSzVar'
# Pool size for parallel processing
pool_size = 4
# Number of runs for computing average values. It is more eficcient that num_of_runs be a multiple of pool_size
num_of_runs = 4
# Number of servers
srv_num = 1000

# Cache size of each server (expressed in number of files)
#cache_sz = 2
# Cache increment step size
cache_step_sz = 20
# Total number of files in the system
file_num = 200
# The list of cache sizes that will be used in the simulation
cache_range = range(1,20) + range(20,file_num,cache_step_sz)

# The graph structure of the network
# It can be: 'RGG' for random geometric graph, and
# 'Lattice' for square lattice graph. For the lattice the graph size should be perfect square.
graph_type = 'Lattice'

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

if __name__ == '__main__':
    # Create a number of workers for parallel processing
    pool = Pool(processes=pool_size)

    t_start = time.time()

    rslt_maxload = np.zeros((len(cache_range),1+num_of_runs))
    rslt_avgcost = np.zeros((len(cache_range),1+num_of_runs))
    for i, cache_sz in enumerate(cache_range):
        params = [(srv_num, cache_sz, file_num, graph_type) for itr in range(num_of_runs)]
        print(params)
        if simulator == 'one choice':
            rslts = pool.map(Simulator1, params)
#            rslts = map(Simulator1, params)
        elif simulator == 'one choice, low mem':
            rslts = pool.map(Simulator1_lowmem, params)
        elif simulator == 'two choice':
            rslts = pool.map(Simulator2, params)
        elif simulator == 'two choice, low mem':
            rslts = pool.map(Simulator2_lowmem, params)
        else:
            print('Error: an invalid simulator!')
            sys.exit()

        for j,rslt in enumerate(rslts):
            rslt_maxload[i,j+1] = rslt['maxload']
            rslt_avgcost[i,j+1] = rslt['avgcost']
#                tmpmxld = tmpmxld + rslt['maxload']
#                tmpavgcst = tmpavgcst + rslt['avgcost']

        rslt_maxload[i,0] = cache_sz
        rslt_avgcost[i,0] = cache_sz
#            result[i,1] = tmpmxld/num_of_runs
#            result[i,2] = tmpavgcst/num_of_runs

#    elif simulator == 'two choice':
#        i = -1
#        rslt_maxload = np.zeros((len(cache_range),1+num_of_runs))
#        rslt_avgcost = np.zeros((len(cache_range),1+num_of_runs))
#        for cache_sz in cache_range:
#            i = i + 1
#            tmpmxld = 0
#            tmpavgcst = 0
#            params = [(srv_num, cache_sz, file_num, graph_type) for itr in range(num_of_runs)]
#            print(params)
#            rslts = pool.map(Simulator2, params)
#            rslts = map(Simulator2, params)
            #rslts = Simulator2((200,1,20))
#            for j,rslt in enumerate(rslts):
#                rslt_maxload[i,j+1] = rslt['maxload']
#                rslt_avgcost[i,j+1] = rslt['avgcost']
#                tmpmxld = tmpmxld + rslt['maxload']
#                tmpavgcst = tmpavgcst + rslt['avgcost']

#            rslt_maxload[i,0] = cache_sz
#            rslt_avgcost[i,0] = cache_sz
#            result[i,1] = tmpmxld/num_of_runs
#            result[i,2] = tmpavgcst/num_of_runs

    t_end = time.time()
    print("The runtime is {}".format(t_end-t_start))

    if (simulator == 'one choice') or (simulator == 'one choice, low mem'):
        sio.savemat(base_out_filename+'_one_choice_'+'sn={}_fn={}_itr={}.mat'.format(srv_num,file_num,num_of_runs), {'maxload':rslt_maxload,'avgcost':rslt_avgcost})
    elif (simulator == 'two choice') or (simulator == 'two choice, low mem'):
        sio.savemat(base_out_filename+'_two_choice_'+'sn={}_fn={}_itr={}.mat'.format(srv_num,file_num,num_of_runs), {'maxload':rslt_maxload,'avgcost':rslt_avgcost})

#    if simulator == 'one choice':
#        np.savetxt(base_out_filename+'_sim1_'+'sn={}_fn={}_itr={}.txt'.format(srv_num,file_num,num_of_runs), result, delimiter=',')
#    elif simulator == 'two choice':
#        np.savetxt(base_out_filename+'_sim2_'+'sn={}_fn={}_itr={}.txt'.format(srv_num,file_num,num_of_runs), result, delimiter=',')

#    plt.plot(result[:,0], result[:,1])
#    plt.plot(result[:,0], result[:,2])
#    plt.xlabel('Cache size in each server (# of files)')
#    plt.title('Random server selection methods. Number of servers = {}, number of files = {}'.format(srv_num,file_num))
#    plt.show()

    #print(result['loads'])
    #print("------")
    #print('The maximum load is {}'.format(result['maxload']))
    #print('The average request cost is {0:0}'.format(result['avgcost']))

    #fl_lst = [srvs[i].get_files_list() for i in range(n)]
    #print(fl_lst)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Date   : 2017/10/12 14:43
# Author : lixingyun

# 礼物不丢弃http://gate.huomaotv.com.cn:7172
# 普通达到速率后丢弃http://gate.huomaotv.com.cn:7173

# 全平台 /1/push/all
# 单房间 /1/push/room?rid=

import time
import requests
import json

url = 'http://gate.huomaotv.com.cn:7172/1/push/room?rid=2'
data = {
    'time': '1507793926',
    'platform': '1',
    'code': '100001',
    'speak':
        {
            'badge': [],
            'fans':
                {
                    'is_live': '1',
                    'avatar': 'http://img.new.huomaotv.com.cn/upload/web/images/headimgs/cfb3d65969e7768350ab33b8ad3d36ae/20170823171927312_normal.jpeg',
                    'gname': '娱乐星秀',
                    'cid': '11',
                    'name': '粉丝',
                    'level': '16',
                    'gcmd': 'ylxx',
                    'rname': 'WWWWWWWWWWWWWWWW',
                    'rid': '11',
                    'zb_name': 'testroom11nc'
                },
            'channel':
                {
                    'name': '哈哈哈11',
                    'screenType': '2',
                    'platType': '1',
                    'rid': '1',
                    'cid': '2'
                },
            'barrage':
                {
                    'type': '100',
                    'msg': '测试',
                    'color': '',
                    'num': '0'
                },
            'extend':
                {
                    'is_guard': '0',
                    'is_fg': '0',
                    'is_zb': '0'
                },
            'user':
                {
                    'name': '哈哈哈%',
                    'level': '3',
                    'avatar': 'http://img.new.huomaotv.com.cn/upload/web/images/headimgs/cfb3d65969e7768350ab33b8ad3d36ae/20170823172044543_normal.jpeg',
                    'uid': '5257'
                }
        }
}

def test():
    res = requests.post(url, json=data, timeout=1)
    print(res.json())

test()

# t1 = time.time()
# for i in range(1, 1000):
#     test()
# t2 = time.time()
# print(t2-t1)


#9/27/18 Question 4, no sources
terms = []
def prRed(skk): print("\033[91m {}\033[00m" .format(skk)) #defining some fancy fonts
def prCyan(skk): print("\033[96m {}\033[00m" .format(skk))
def fib(n):
	a,b = 1,1
	for i in range(n-1):
		a,b = b,a+b
	return a
i = 1
while len(terms) < 50:
	if (fib(i))%2 != 0:
		terms.append(fib(i))
	i+=1
for i in range(0, 50):
	if i%2 == 0:
		prRed(fib(i))
	else:
		prCyan(fib(i))


#!/usr/bin/env python

from main_controller.msg import *
import rospy
import smach
import smach_ros
import time

g_remote_output = None

class Sleep(smach.State):
    def __init__(self):
        smach.State.__init__(self, outcomes=['waking up', 'stay asleep'])

    def execute(self, userdata):
        time.sleep(3)
        if not g_remote_output is None:
            if g_remote_output.action == g_remote_output.WAKE:
                return 'waking up'

        return 'stay asleep'

class Awake(smach.State):
    def __init__(self):
        smach.State.__init__(self, outcomes=['go to sleep', 'stay awake', 'need to rest'])

    def execute(self, userdata):
        time.sleep(3)
        if not g_remote_output is None:
            if g_remote_output.action == g_remote_output.REST:
                return 'need to rest'

        return 'stay awake'

class Rest(smach.State):
    def __init__(self):
        smach.State.__init__(self, outcomes=['go to sleep', 'stop resting', 'need more rest'])

    def execute(self, userdata):
        time.sleep(3)
        return 'need more rest'

def callback(msg):
    global g_remote_output
    g_remote_output = msg
    rospy.loginfo('received action: [%d]' % g_remote_output.action)

def main():
    rospy.init_node('smach_example_state_machine')

    # Create a SMACH state machine
    sm = smach.StateMachine(outcomes=['waking up'])

    # Subscribers
    rospy.Subscriber('/remote', Remote, callback)

    # Open the container
    with sm:
        # Add states to the container
        smach.StateMachine.add('SLEEP', Sleep(),
                transitions={'waking up':'AWAKE', 'stay asleep':'SLEEP'})
        smach.StateMachine.add('AWAKE', Awake(),
                transitions={'go to sleep':'SLEEP', 'stay awake':'AWAKE', 'need to rest':'REST'})
        smach.StateMachine.add('REST', Rest(),
                transitions={'go to sleep':'SLEEP', 'stop resting':'AWAKE', 'need more rest':'REST'})

    # Execute SMACH plan
    outcome = sm.execute()
    rospy.spin()

if __name__ == '__main__':
    main()


from django.conf.urls import url
from app01 import views

# url(正則表達式, views視圖函數, 參數, 別名)
# 由上到下匹配
urlpatterns = [
    # -----無命名分組url配置(正則表達式(有分組), views視圖函數)-----
    url(r'no_name_grouping/(\d{4})/(\d{2})', views.no_name),
    # -----有命名分組url配置(正則表達式(有分組), views視圖函數)-----
    url(r'name_grouping/(?P<year>\d{4})/(?P<month>\d{2})', views.has_name),  # 函數參數需對應命名

    # -----基本url配置(正則表達式, views視圖函數)-----
    url(r'no_alias_register', views.no_alias_register),
    # -----有別名url配置(正則表達式, views視圖函數, 別名)-----
    url(r'alias_register', views.alias_register, name="reg"),  # 使用別名，減低維護複雜度
]


#Laura August 2019
#Python 2.7.15+ on Ubuntu 18.04
#Reads the Seeboat data from serial, writes it to csv, and plots it in real time

#Based on: https://makersportal.com/blog/2018/2/25/python-datalogger-reading-the-serial-output-from-arduino-to-analyze-data-using-pyserial
#possible useful resources
#added this via https://stackoverflow.com/questions/12822762/pylab-ion-in-python-2-matplotlib-1-1-1-and-updating-of-the-plot-while-the-pro

import serial
import time
import csv
import matplotlib
matplotlib.use("tkAgg")
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime

#take off the labels on the x axis
#do many plots

filename = "seeBoatData"
serial_port = '/dev/ttyACM1'
baud_rate = 9600
path = "%s_%s.csv" % (filename, str(datetime.now()))

ser = serial.Serial(serial_port, baud_rate)
ser.flushInput()

plot_window = 50 #This is the number of points that you're plotting at once
#There are four different sensors we're plotting data for
y_varC = np.array(np.zeros([plot_window])) #conductivity
y_varN = np.array(np.zeros([plot_window])) #turbitidy
y_varT = np.array(np.zeros([plot_window])) #temperature
y_varP = np.array(np.zeros([plot_window])) #pH

#Set up the plotting space
plt.ion()
fig, ax = plt.subplots(nrows=2, ncols=2) #this should be four subplots
#line, = ax.plot(y_var)

lineC, = ax[0,0].plot(y_varC)
lineT, = ax[0,1].plot(y_varT)
lineN, = ax[1,0].plot(y_varN)
lineP, = ax[1,1].plot(y_varP)

ax[0,0].set_title("Conductivity")
ax[0,1].set_title("Temperature")
ax[1,0].set_title("Turbidity")
ax[1,1].set_title("pH")

# Define a function to clean up the plot axes
def plotfix( plotname ):
   plotname.relim()
   plotname.axes.get_xaxis().set_visible(False) #get rid of x-axis labels
   plotname.autoscale_view()
   return;

#Put in the header for the csv
with open(path, 'a') as f:
    header = ["boatName,", "deviceID,", "GPSlat,", "GPSlong,", "GPShour,", "GPSmin,", "GPSsec,", "GPSms,", "tempC,", "conduS,", "pH,","turbNTU,","date,", "time\n"]
    f.writelines(header)

while True:
    try:
	#Save the data to a csv
        ser_bytes = ser.readline()
        try:
            #needs to be a string to write out okay, the seperate into parts by the commas
            decoded_bytes = str(ser_bytes[0:len(ser_bytes)-2].decode("utf-8"))
            decoded_bytesList= decoded_bytes.split(",")
            print(decoded_bytes) #if you want to see what you're getting
        except:
            print("decode fail")
            continue
        with open(path,"a") as f:
#            f.writelines([decoded_bytes]) #write this to the file
            f.writelines([decoded_bytes, ", %s , %s \n " % (datetime.now().date(), datetime.now().time())]) #write this to the file

        #Make a plot, including subplots for all sensors. Indices are how the come from arduino
        y_varC = np.append(y_varC,float(decoded_bytesList[9]))
        y_varN = np.append(y_varN,float(decoded_bytesList[11]))
        y_varT = np.append(y_varT,float(decoded_bytesList[8]))
        y_varP = np.append(y_varP,float(decoded_bytesList[10]))

        y_varC = y_varC[1:plot_window+1]
        y_varN = y_varN[1:plot_window+1]
        y_varT = y_varT[1:plot_window+1]
        y_varP = y_varP[1:plot_window+1]

        lineC.set_ydata(y_varC)
        lineN.set_ydata(y_varN)
        lineT.set_ydata(y_varT)
        lineP.set_ydata(y_varP)

        plotfix(ax[0,0])
        plotfix(ax[0,1])
        plotfix(ax[1,0])
        plotfix(ax[1,1])

        fig.canvas.draw()
        fig.canvas.flush_events()
#        plt.pause(1) #added this via website to make sure we see the plot
    except:
        print("Keyboard Interrupt")
        break


import unittest
from models.parts import Board, Piece

class TestBoard(unittest.TestCase):
    def test_setup(self):
        board = Board()
        self.assertEqual(
            len(board.positions),
            4
        )
        for column in board.positions:
            self.assertEqual(
                len(column),
                4
            )

    def test_setup_specify_size(self):
        board = Board(3)
        self.assertEqual(
            len(board.positions),
            3
        )
        for column in board.positions:
            self.assertEqual(
                len(column),
                3
            )

    def test_get_rows_bottom_left(self):
        board = Board()
        rows = board.get_rows(0, 0)
        self.assertEqual(
            rows,
            [
                [(0, 0), (1, 0), (2, 0), (3, 0)],
                [(0, 0), (0, 1), (0, 2), (0, 3)],
                [(0, 0), (1, 1), (2, 2), (3, 3)]
            ]
        )

    def test_get_rows_mid_right(self):
        board = Board()
        rows = board.get_rows(3, 2)
        self.assertEqual(
            rows,
            [
                [(0, 2), (1, 2), (2, 2), (3, 2)],
                [(3, 0), (3, 1), (3, 2), (3, 3)]
            ]
        )

    def test_get_rows_mid(self):
        board = Board(3)
        rows = board.get_rows(1, 1)
        self.assertEqual(
            rows,
            [
                [(0, 1), (1, 1), (2, 1)],
                [(1, 0), (1, 1), (1, 2)],
                [(0, 0), (1, 1), (2, 2)],
                [(0, 2), (1, 1), (2, 0)]
            ]
        )

    def test_get_simple(self):
        board = Board(2)
        for x in range(2):
            for y in range(2):
                self.assertEqual(
                    board.get(x, y),
                    None
                )

    def test_get_simple_bad(self):
        board = Board(2)
        for x, y in [
            (1, 5), (5, 1), (-1, 2), (3, -2)
        ]:
            with self.assertRaises(Exception):
                board.get(x, y)

    def test_put_simple(self):
        board = Board(2)
        piece = Piece.from_attributes(True, False)
        board.put(piece, 1, 1)
        with self.assertRaises(Exception):
            board.put(piece, 1, 1)

class TestPiece(unittest.TestCase):
    def test_creation(self):
        standard_piece = Piece.from_attributes()
        self.assertEqual(standard_piece.value, 0)

    def test_creation_with_positive_attributes(self):
        standard_piece = Piece.from_attributes(True)
        self.assertEqual(standard_piece.value, 1)

        standard_piece = Piece.from_attributes(True, True)
        self.assertEqual(standard_piece.value, 3)

        standard_piece = Piece.from_attributes(True, True, True, True)
        self.assertEqual(standard_piece.value, 15)

        standard_piece = Piece.from_attributes(True, False, True, True)
        self.assertEqual(standard_piece.value, 13)

        standard_piece = Piece.from_attributes(True, False, False, True)
        self.assertEqual(standard_piece.value, 9)

    def test_piece_overlap_opposite(self):
        standard_piece = Piece.from_attributes()
        opposite_piece = Piece.from_attributes(True, True, True, True)
        self.assertFalse(
            Piece.overlap(
                standard_piece,
                opposite_piece
            )
        )

    def test_piece_overlap(self):
        standard_piece = Piece.from_attributes()
        opposite_piece = Piece.from_attributes(True, True, False, True)
        self.assertTrue(
            Piece.overlap(
                standard_piece,
                opposite_piece
            )
        )

    def test_positive_overlap(self):
        a = Piece.from_attributes(True, True, False, True)
        b = Piece.from_attributes(True, False, False, True)
        c = Piece.from_attributes(True, True, False, False)
        d = Piece.from_attributes(True, False, True, False)
        self.assertTrue(
            Piece.overlap(
                a, b, c, d
            )
        )

    def test_negative_overlap(self):
        a = Piece.from_attributes(True, True, False, True)
        b = Piece.from_attributes(True, False, False, True)
        c = Piece.from_attributes(False, True, False, False)
        d = Piece.from_attributes(True, False, False, False)
        self.assertTrue(
            Piece.overlap(
                a, b, c, d
            )
        )

    def test_no_overlap(self):
        a = Piece.from_attributes(True, True, False, True)
        b = Piece.from_attributes(True, False, True, True)
        c = Piece.from_attributes(False, True, False, False)
        d = Piece.from_attributes(True, False, False, False)
        self.assertFalse(
            Piece.overlap(
                a, b, c, d
            )
        )

    def test_full_overlap(self):
        pieces = []
        for black in (True, False):
            for tall in (True, False):
                for hole in (True, False):
                    for square in (True, False):
                        pieces.append(
                            Piece.from_attributes(black, tall, hole, square)
                        )
        for piece in pieces:
            overlaps = 0
            for other_piece in pieces:
                if piece == other_piece:
                    continue
                if Piece.overlap(piece, other_piece):
                    overlaps += 1

            self.assertEqual(
                overlaps, 14
            )

    def test_to_dict(self):
        self.assertEqual(
            Piece(5).to_dict(),
            {'value': 5}
        )

    def test_from_dict(self):
        self.assertEqual(
            Piece.from_dict(
                {'value': 5}
            ),
            Piece(5)
        )

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


from history_analysis.history import compute_rebuilt_matrices
from calage_backtesting_parissportifs import simulate_bet_over
from collections import OrderedDict
from itertools import product

database = []

from_year = 2015
to_year = 2018

full_data = {}

data_from_year = 2011
data_to_year = 2018
dataset = product(range(2000, 2001,3), range(2015, 2018), range(2,5,1), range(1,2,10), range(1,2,10))

for data_from_year, data_to_year, n_cat, t1, t2 in dataset:
    print(data_from_year, data_to_year, n_cat, t1, t2)
    matrices = compute_rebuilt_matrices(data_from_year, data_to_year, 'data_built_m4.csv',
                             threshold_1=t1,
                             threshold_2=t2,
                             NCAT=n_cat,
                             printing=False)
    play_scores, bet_details, final_model = simulate_bet_over(data_from_year, data_to_year, from_year, to_year,
                                                              proba_table_file='', n_cat=n_cat, matrices=matrices,
                                                              printing=False)
    #print('Threshold 1,2 = {}, {}'.format(t1, t2))
    for season, r in play_scores.items():
        print("Season {:^5} Total {:^5.0f} {:^5.3f} Prono {:^5.0f} {:^5.3f} Exact {:^5.0f} {:^5.3f} Gain {:^5.0f} Stake:{:^5.0f} ROI:{:^5.0f}".format(
            season, r['total'][0], r['total'][1], r['prono'][0], r['prono'][1], r['exact'][0], r['exact'][1],
            r['paris'][0], r['paris'][1], r['paris'][2] * 100
        ))
    for season, results in play_scores.items():
        row = OrderedDict()
        row['NCAT'] = n_cat
        row['Data_from'] = data_from_year
        row['Data_to'] = data_to_year
        row['Season'] = season
        row['T1'] = str(t1)
        row['T2'] = str(t2)
        row['total'] = str(results['total'][0])
        row['prono'] = str(results['prono'][0])
        row['exact'] = str(results['exact'][0])
        row['paris_gain'] = str(int(results['paris'][0]))
        row['paris_stake'] = str(int(results['paris'][1]))
        row['paris_roi'] = "{:.2f}".format(100 * results['paris'][2])
        database.append(row)

first_row = False
for row in database:
    if not first_row:
        first_row = True
        print(",".join(row.keys()))
    print(','.join([str(r) for r in row.values()]))


from xai.brain.wordbase.nouns._prude import _PRUDE

#calss header
class _PRUDES(_PRUDE, ):
	def __init__(self,):
		_PRUDE.__init__(self)
		self.name = "PRUDES"
		self.specie = 'nouns'
		self.basic = "prude"
		self.jsondata = {}


from PyQt4.QtGui import *
from PyQt4.QtCore import *

class devir_hizi_hesaplama(QDialog):
    def __init__(self,ebeveyn=None):
        super(devir_hizi_hesaplama,self).__init__(ebeveyn)

        grid=QGridLayout()
        grid.addWidget(QLabel("Personel Devir Hızı Hesaplama"))

        grid.addWidget(QLabel("Aybaşı Çalışan Sayısı:"),1,0)

        self.aybasiCalisan=QLineEdit()
        grid.addWidget(self.aybasiCalisan,1,1)

        grid.addWidget(QLabel("Aysonu Çalışan Sayısı:"),2,0)

        self.aysonuCalisan=QLineEdit()
        grid.addWidget(self.aysonuCalisan,2,1)

        grid.addWidget(QLabel("İşten Çıkan Personel Sayısı:"),3,0)

        self.istenCikan=QLineEdit()
        grid.addWidget(self.istenCikan,3,1)

        grid.addWidget(QLabel("Personel Devir Hızınız(%):"),4,0)
        self.sonucOran=QLabel("Sonuç")
        grid.addWidget(self.sonucOran,4,1)

        hesaplaDugme=QPushButton("Hesapla")
        grid.addWidget(hesaplaDugme,5,1,1,2)
        self.connect(hesaplaDugme,SIGNAL('pressed()'),self.hesapla)

        self.setLayout(grid)
        self.setWindowTitle("Turnover")

    def hesapla(self):
        abCa=int(self.aybasiCalisan.text())
        asCa=int(self.aysonuCalisan.text())
        iC=int(self.istenCikan.text())
        sonuc=(iC/((abCa+asCa)/2))*100
        self.sonucOran.setText('%0.1f'%sonuc)

uygulama=QApplication([])
pencere=devir_hizi_hesaplama()
pencere.show()
uygulama.exec_


import urllib.request
class extraction(object):
    def extracting(self,text,data,final):
        start=text.find(data)
        point=0
        pointe=0
        price=['0','0','0']
        a=-1
        flag=0
        while a<2:
            flag=0
            while 1:
                point =point+1
                te=text[start+point]
                if te>='0' and te<='9':
                    a=a+1
                    if text[start+point-1]=='(':
                        price[a]='-'
                        price[a]=price[a]+te
                        break
                    price[a]=te
                    break
                if te=='-':
                    a=a+1
                    price[a]='0'
                    flag=1
                    break
            pointe=point
            while 1:
                if flag==1:
                    break
                if te=='"':
                    price[a]='0'
                    break
                pointe=pointe+1
                te=text[start+pointe]
                if te=='&' or te=='<':
                    break
                if te!=',' and te>='0' and te<='9':
                    price[a]=price[a]+te
            point=pointe
        print (data,'\t',price[0],'\t',price[1],'\t',price[2])
        final.append([(int(price[0])),(int(price[1])),(int(price[2]))])
    def connect(self,link):
        try:
            return urllib.request.urlopen(link)
        except:
            print ("connecting",link)
            return self.connect(link)
    def income(self,final,company):
        link='http://finance.yahoo.com/q/is?s='+company+'+Income+Statement&annual'
        page=self.connect(link)
        text=page.read().decode('utf8')
        data='Operating Income'
        self.extracting(text,data,final)
        data='Gross Profit'
        self.extracting(text,data,final)
        data='Total Revenue'
        self.extracting(text,data,final)
    def balance(self,final,company):
        link='http://finance.yahoo.com/q/bs?s='+company+'+Balance+Sheet&annual'
        page=self.connect(link)
        text=page.read().decode('utf8')
        data='Total Current Assets'
        self.extracting(text,data,final)
        data='Common Stock</td>'
        self.extracting(text,data,final)
        data='Total Liabilities'
        self.extracting(text,data,final)
        data='Total Current Liabilities'
        self.extracting(text,data,final)
        data='Total Assets'
        self.extracting(text,data,final)
    def cash(self,final,company):
        link='http://finance.yahoo.com/q/cf?s='+company+'+Cash+Flow&annual'
        page=self.connect(link)
        text=page.read().decode('utf8')
        data='Total Cash Flow From Operating Activities'
        self.extracting(text,data,final)
def getData(company):
    final=[]
    a=extraction()
    print (company,'\n')
    a.income(final,company)
    a.balance(final,company)
    a.cash(final,company)
    return final


import explore_enron_data as enron

data = enron.enron_data

total = 0
nan = 0

for key, value in data.iteritems() :
    total += 1
    if data[key]['total_payments'] == 'NaN':
        nan += 1

print('NaN total_payments: ' + str(nan))
perc = float(nan)/float(total)
perc *= 100
print('NaN% of total: ' + str(perc))


import tensorflow as tf
import numpy as np
import math
from tensorflow.contrib.layers.python.layers import layers
import tensorflow.contrib.seq2seq as tf_seq2seq
from tensorflow.python.layers import core as layers_core

class BasicModel_Train:
    """
        Without attention model!
    """

    def __init__(self, batch_size, n_feat, vocab_size, embedding_size, max_encoder_time, max_decoder_time,
                 hidden_units, max_gradient_norm=5, learning_rate=0.0001, dropout=0.2, use_scheduled_sampling=False,
                 decay_steps=5, decay_rate=0.5):
        # image feat matrix [batch, time_step, n_feat]
        # no need for embedding
        self.encoder_inputs = tf.placeholder(tf.float32, [batch_size, max_encoder_time, n_feat],
                                             name='encoder_inputs')

        self.decoder_lengths = tf.placeholder(tf.int32, [batch_size], name='max_decoder_time')

        self.decoder_inputs = tf.placeholder(tf.int32, [batch_size, max_decoder_time],
                                             name='decoder_inputs')

        self.decoder_outputs = tf.placeholder(tf.int32, [batch_size, max_decoder_time],
                                              name='decoder_outputs')

        self.decoder_mask = tf.placeholder(tf.float32, [batch_size, max_decoder_time],
                                           name='decoder_mask')

        self.global_step = tf.placeholder(tf.int32, shape=[], name='global_step')

        if use_scheduled_sampling:
            self.sampling_probability = tf.placeholder(tf.float32, [], name="sampling_probability")

        with tf.variable_scope("embedding"):
            self.embeddings = tf.Variable(tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0),
                                          dtype=tf.float32, name="embedding_table")

        with tf.variable_scope("encoder"):
            self.encoder_cell = tf.nn.rnn_cell.BasicLSTMCell(hidden_units)
            self.encoder_cell = tf.contrib.rnn.DropoutWrapper(
                cell=self.encoder_cell, input_keep_prob=(1.0 - dropout))

            self.encoder_inputs_sequence_length = tf.fill([batch_size], tf.shape(self.encoder_inputs)[1])
            self.encoder_outputs, self.encoder_final_state = tf.nn.dynamic_rnn(
                self.encoder_cell, inputs=self.encoder_inputs, dtype=tf.float32)

        with tf.variable_scope("decoder"):
            self.projection_layer = layers_core.Dense(vocab_size, use_bias=False, name="projection_layer")

            self.decoder_cell = tf.nn.rnn_cell.BasicLSTMCell(hidden_units)

            self.decoder_cell = tf.contrib.rnn.DropoutWrapper(
                cell=self.decoder_cell, input_keep_prob=(1.0 - dropout))

            self.decoder_inputs_embedded = tf.nn.embedding_lookup(self.embeddings, self.decoder_inputs)

            if use_scheduled_sampling:
                self.helper = tf_seq2seq.ScheduledEmbeddingTrainingHelper(self.decoder_inputs_embedded,
                                                                          self.decoder_lengths, self.embeddings,
                                                                          sampling_probability=self.sampling_probability,
                                                                          time_major=False)
            else:
                self.helper = tf_seq2seq.TrainingHelper(self.decoder_inputs_embedded, self.decoder_lengths,
                                                        time_major=False)
            # batch_size & max_decoder_time error
            self.decoder = tf_seq2seq.BasicDecoder(
                cell=self.decoder_cell, helper=self.helper, initial_state=self.encoder_final_state,
                output_layer=self.projection_layer)

            self.outputs, _, _ = tf_seq2seq.dynamic_decode(self.decoder, output_time_major=False)
            self.logits = self.outputs.rnn_output
            self.translations = self.outputs.sample_id

        with tf.variable_scope("loss"):
            self.crossent = tf.nn.sparse_softmax_cross_entropy_with_logits(
                labels=self.decoder_outputs, logits=self.logits)
            self.train_loss = (tf.reduce_sum(self.crossent * self.decoder_mask)) / batch_size
            tf.summary.histogram("cross_entropy", self.crossent)
            tf.summary.scalar("train_loss", self.train_loss)

        with tf.variable_scope("computation"):
            self.params = tf.trainable_variables()
            self.gradients = tf.gradients(self.train_loss, self.params)
            self.clipped_gradients, _ = tf.clip_by_global_norm(
                self.gradients, max_gradient_norm)

        with tf.variable_scope("optimization"):
            self.learning_rate = tf.train.exponential_decay(
                learning_rate=learning_rate,
                global_step=self.global_step,
                decay_steps=decay_steps,
                decay_rate=decay_rate)

            self.optimizer = tf.train.GradientDescentOptimizer(self.learning_rate)
            self.update_step = self.optimizer.apply_gradients(
                zip(self.clipped_gradients, self.params))

        self.merged_summary = tf.summary.merge_all()

class BasicModel_Infer:
    def __init__(self, batch_size, n_feat, vocab_size, embedding_size, max_encoder_time, max_decoder_time,
                 hidden_units, max_gradient_norm=100, learning_rate=0.0001, bos_id=0, eos_id=1,
                 maximum_iterations=100, use_beam_search=True, beam_width=10, **kwargs):
        # image feat matrix [batch, time_step, n_feat]
        # no need for embedding
        self.encoder_inputs = tf.placeholder(tf.float32, [batch_size, max_encoder_time, n_feat],
                                             name='encoder_inputs')

        with tf.variable_scope("embedding"):
            self.embeddings = tf.Variable(tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0),
                                          dtype=tf.float32, name="embedding_table")

        with tf.variable_scope("encoder"):
            self.encoder_cell = tf.nn.rnn_cell.BasicLSTMCell(hidden_units)

            self.encoder_outputs, self.encoder_final_state = tf.nn.dynamic_rnn(
                self.encoder_cell, inputs=self.encoder_inputs, dtype=tf.float32)

        with tf.variable_scope("decoder"):
            self.projection_layer = layers_core.Dense(vocab_size, use_bias=False, name="projection_layer")
            self.decoder_cell = tf.nn.rnn_cell.BasicLSTMCell(hidden_units)

            if use_beam_search:
                self.decoder_initial_state = tf.contrib.seq2seq.tile_batch(
                    self.encoder_final_state, multiplier=beam_width)

                self.start_tokens = tf.fill([batch_size], bos_id)
                self.end_token = eos_id
                self.decoder = tf.contrib.seq2seq.BeamSearchDecoder(
                    cell=self.decoder_cell,
                    embedding=self.embeddings,
                    start_tokens=self.start_tokens,
                    end_token=self.end_token,
                    initial_state=self.decoder_initial_state,
                    beam_width=beam_width,
                    output_layer=self.projection_layer,
                    length_penalty_weight=0.0)
                self.outputs, _, _ = tf_seq2seq.dynamic_decode(self.decoder, maximum_iterations=maximum_iterations)
                self.translations = self.outputs.predicted_ids[:, :, 0]

            else:
                # batch_size & max_decoder_time error
                self.helper = tf_seq2seq.GreedyEmbeddingHelper(self.embeddings,
                                                               tf.fill([batch_size], bos_id), eos_id)
                self.decoder = tf_seq2seq.BasicDecoder(
                    cell=self.decoder_cell, helper=self.helper, initial_state=self.encoder_final_state,
                    output_layer=self.projection_layer)

                self.outputs, _, _ = tf_seq2seq.dynamic_decode(self.decoder, maximum_iterations=maximum_iterations)

                self.translations = self.outputs.sample_id


import sys
sys.setrecursionlimit(1 << 20)
INF = float('inf')

def read_int_list():
    return list(map(int, input().split()))

def read_ints():
    return map(int, input().split())

def main():
    N = int(input())
    A = read_int_list()
    ans = 1
    cur = A[0]
    up = None
    for a in A[1:]:
        if cur == a:
            continue
        if a > cur:
            if up is not None and up is False:
                ans += 1
                up = None
            else:
                up = True
        else:
            if up is not None and up is True:
                ans += 1
                up = None
            else:
                up = False
        cur = a
    print(ans)

main()


import sys
from openpyxl.styles import PatternFill

Style_FillPu = PatternFill(start_color='ccd9ff', fill_type='solid')

def GetCellValue(outWorksheet,x,y):
    value = 0
    try:value = int(outWorksheet.cell(row=x,column=y).value)
    except:value = 0
    return value

def AddTotalHorizontal(worksheet, sourceCount):
    endCol = 17
    for row in range(3, 3+sourceCount):
        rowValue = 0
        for col in range(2, endCol):
            rowValue += GetCellValue(worksheet, row, col)
        #update value at last next cell
        worksheet.cell(row=row,column=endCol).value = rowValue
        worksheet.cell(row=row,column=endCol).fill = Style_FillPu
    worksheet['Q2'] = 'Total by Ward'
    return worksheet

def AddTotalVertical(worksheet, sourceCount):
    endCol = 17
    for col in range(2, endCol):
        monthValue = 0
        for row in range(3,2+sourceCount):
            monthValue += GetCellValue(worksheet, row, col)

        worksheet.cell(row=2+sourceCount,column=col).value = monthValue
        worksheet.cell(row=2+sourceCount,column=col).fill = Style_FillPu

    tar = 'A'+str(2+sourceCount)
    worksheet[tar] = 'Total by Sample'
    worksheet.row_dimensions[2+sourceCount].height = 20
    return worksheet


import random

import PIL
import torch
from torch.utils.tensorboard import SummaryWriter
import datetime
import pathlib
import os
import torchvision
from torch.utils.data import DataLoader
from transformers import DistilBertConfig, DistilBertForSequenceClassification, BertConfig, BertModel
import numpy as np
import models
import data_loading
import matplotlib.pyplot as plt
import training
import re
import logging

os.environ["TOKENIZERS_PARALLELISM"] = "false"
seq_size = 70
bs = 16
intermediate_size = 200
tabular_output_size = 200
image_output_size = 200
num_attention_heads = 10
img_size = 200
path = "/home/marcel/projetos/data/csgo_analyze/processed_test"
embeds_size = image_output_size + tabular_output_size
experiment_name="model_training"
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
seed=42
np.random.seed(seed)
torch.manual_seed(seed)
device = "cuda:0"
logging.basicConfig(filename='sketch.log', level=logging.INFO)

image_files = data_loading.get_files(path, extensions=['.jpg'])
tabular_files = data_loading.get_files(path, extensions=['.csv'])

randomlist = random.sample(range(len(tabular_files)), 500)
tabular_files = [tab_file for i,tab_file in enumerate(tabular_files) if i in randomlist]

# renamed_files = 0
# for path_image in image_files:
#     new_path = re.sub(r'output_map(\d).jpg', r'output_map0\1.jpg', str(path_image))
#     if new_path != str(path_image):
#         os.rename(path_image, new_path)
#         renamed_files += 1
#
# if renamed_files > 0:
#     image_files = data_loading.get_files(path, extensions=['.jpg'])
#     tabular_files = data_loading.get_files(path, extensions=['.csv'])
#     print(len(image_files))
#     print(len(tabular_files))

full_csv = data_loading.filterTabularData(tabular_files, data_loading.columns)
filtered_image_files = data_loading.filterImageData(full_csv)

splits = data_loading.folderSplitter(filtered_image_files)

assert filtered_image_files[55]==full_csv.iloc[55, -2]

# class groups:
# mainweapon, secweapon,flashbangs,hassmoke,hasmolotov,hashe,hashelmet,hasc4,hasdefusekit

train_tabular = full_csv.iloc[splits[0], :]
train_rounds, train_winners = data_loading.get_rounds_and_winners(train_tabular)
train_images = [image for i, image in enumerate(filtered_image_files) if image.rfind("train/")!=-1]

valid_tabular = full_csv.iloc[splits[1], :]
valid_rounds, valid_winners = data_loading.get_rounds_and_winners(valid_tabular)
valid_images = [image for i, image in enumerate(filtered_image_files) if image.rfind("val/")!=-1]

cat_transforms = data_loading.TransformPipeline([data_loading.Categorize(data_loading.x_category_map), data_loading.ToTensor])
cont_transforms = data_loading.TransformPipeline(
    [data_loading.Normalize(train_tabular.iloc[:, :-2], category_map=data_loading.x_category_map), data_loading.ToTensor])
label_transforms = data_loading.TransformPipeline(
    [data_loading.Categorize(data_loading.y_category_map, multicat=False), data_loading.ToTensor])
image_transforms = torchvision.transforms.Compose(
    # [torchvision.transforms.Resize(200), torchvision.transforms.ToTensor()])
    [data_loading.resize_lycon(img_size)])
batch_transform = data_loading.TransformPipeline([data_loading.ToTensor, data_loading.NormalizeImage((0.0019, 0.0040, 0.0061),
                                                                                (0.0043, 0.0084, 0.0124))])

train_x_tabular = train_tabular.iloc[:, :-2]
#
# dali_iterator = data_loading.ExternalInputIterator( train_images, train_rounds, train_x_tabular, train_winners,bs, cat_transforms,
#                  cont_transforms, label_transforms, data_loading.x_category_map, data_loading.y_category_map)
#
#
# pipe = data_loading.ExternalSourcePipeline(image_size = img_size,batch_size=bs, num_threads=2, device_id = 0,
#                               external_data = dali_iterator,seed=seed)
# pii = data_loading.PyTorchIterator(pipe, size=dali_iterator.size, last_batch_padded=True, fill_last_batch=False)

train_dataset = data_loading.CSGORoundsDataset(train_images, train_rounds, train_x_tabular, train_winners,
                                               image_transforms,
                                               cat_transforms, cont_transforms, label_transforms, data_loading.x_category_map,
                                               data_loading.y_category_map, batch_transform)

valid_y_dict = valid_tabular["winner"]
valid_x_tabular = valid_tabular.iloc[:, :-2]
valid_dataset = data_loading.CSGORoundsDataset(valid_images, valid_rounds, valid_x_tabular, valid_winners,
                                               image_transforms,
                                               cat_transforms, cont_transforms, label_transforms, data_loading.x_category_map,
                                               data_loading.y_category_map, batch_transform)

train_dl_mixed = torch.utils.data.DataLoader(train_dataset, batch_size=bs, shuffle=True, num_workers=4,
                                             collate_fn=data_loading.snapshot_sequence_batching(seq_size), drop_last=True)
valid_dl_mixed = torch.utils.data.DataLoader(valid_dataset, batch_size=bs, shuffle=True, num_workers=4,
                                             collate_fn=data_loading.snapshot_sequence_batching(seq_size), drop_last=True)

# tensor_to_image = torchvision.transforms.ToPILImage(mode="RGB")
# train_example_batch = batch_transform.decode(next(iter(train_dl_mixed)))
# mask_size=torch.sum((train_example_batch[3]==True),dim=1)
# key_image = train_example_batch[2][:, mask_size,:, :, :]

iter_train = iter(train_dl_mixed)
print(next(iter_train))

group_count = 0
category_groups_sizes = {}
for class_group, class_group_categories in data_loading.x_category_map.items():
    category_groups_sizes[class_group] = (group_count, len(class_group_categories))
    group_count += 1
category_list = data_loading.cat_names

lr = 5e-5
n_epochs = 5
now = datetime.datetime.now()
creation_time = now.strftime("%H:%M")
tensorboard_writer = SummaryWriter(os.path.expanduser('~/projetos/data/csgo_analyze/experiment/tensorboard/sequence_class/') +
                                   experiment_name+"/"+now.strftime("%Y-%m-%d")+"/"+creation_time +"/"+
                                   "seed-"+str(seed)+"-lr-"+str(lr))

tensorboard_class = training.TensorboardClass(tensorboard_writer)

tab_model = models.TabularModelCustom(category_list, category_groups_sizes, len(data_loading.cont_names), [200, 100], ps=[0.2, 0.2])

image_model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet34', pretrained=False, num_classes=image_output_size)
image_model.to(device)

configuration = BertConfig(hidden_size=image_output_size+tabular_output_size,
                           num_attention_heads=num_attention_heads,intermediate_size=intermediate_size,output_hidden_states=True)

seq_model = BertModel(configuration)

# encoder_layer = torch.nn.TransformerEncoderLayer(d_model=image_output_size + tabular_output_size, nhead=4)
# seq_model = torch.nn.TransformerEncoder(encoder_layer, num_layers=4)
# configuration = DistilBertConfig(dim=tabular_output_size+image_output_size,
#                            n_heads=num_attention_heads,hidden_dim=intermediate_size,dropout=0.2,sinusoidal_pos_embds=True,
#                                  attention_dropout=0.2)

# seq_model = DistilBertForSequenceClassification(configuration)

seq_model.to(device)

# image_model.load_state_dict(
#     torch.load(os.path.expanduser('~/projetos/data/csgo_analyze/processed_test/image_output-bceloss200-0.pt')))
# tab_model.load_state_dict(
#     torch.load(os.path.expanduser('~/projetos/data/csgo_analyze/processed_test/tab_output-bceloss200-0.pt')))

model = models.CustomMixedModel(image_model, tab_model, seq_model,image_output_size, embeds_size)
model = model.to(device)
model.load_state_dict(torch.load(os.path.expanduser('~/projetos/data/csgo_analyze/processed_test/full_model-unfrozen-seq_model0.pt')))
# model.load_state_dict(
#     torch.load(os.path.expanduser('~/projetos/data/csgo_analyze/processed_test/full_model-0.pt')))

loss_fn = torch.nn.BCEWithLogitsLoss().to(device=device)

#torch.set_num_threads(5)
optimizer = torch.optim.Adam(model.parameters(), lr=lr  # ,betas=(0.6,0.99),weight_decay=1e-4
                             )

# scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer, max_lr=lr, steps_per_epoch=len(train_dl_mixed), epochs=n_epochs)
# for epoch in range(n_epochs):
#     print("epoch:",epoch)
#     training.one_epoch(loss_fn, model, train_dl_mixed, valid_dl_mixed, optimizer, tensorboard_class,scheduler,train_embeds=False,train_seq_model=False)
#     #torch.save(model.seq_model.state_dict(), str(pathlib.Path(path) / "seq_model-")+str(epoch)+".pt")
#     torch.save(model.state_dict(), str(pathlib.Path(path) / "full_model-")+str(epoch)+".pt")
#
# lr=lr/10.0
# optimizer = optimizer_type(model.parameters(), lr=lr  # ,betas=(0.6,0.99),weight_decay=1e-4
#      )

scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer, max_lr=lr, steps_per_epoch=len(train_dl_mixed), epochs=n_epochs)


class Solution:
    def validMountainArray(self, arr: List[int]) -> bool:
        if len(arr) < 3 or arr[0] >= arr[1]:
            return False

        is_going_up = True

        for i in range(1, len(arr)-1):
            if is_going_up:
                if arr[i] < arr[i+1]:
                    continue
                elif arr[i] > arr[i+1]:
                    is_going_up = False
                else:
                    return False
            else:
                if arr[i] <= arr[i+1]:
                    return False

        return is_going_up == False


# Copyright 2014 Roberto Brian Sarrionandia
#
# 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.

from google.appengine.ext import ndb

class TUser(ndb.Model):
	"""Models a user account"""
	g_user = ndb.UserProperty()
	nickname = ndb.StringProperty()
	full_name = ndb.StringProperty()
	current_institution = ndb.StringProperty()
	public_profile = ndb.BooleanProperty()

class Attending(ndb.Model):
	"""Models a user attending a tournament
	The parent of the object will be the TUser attending """
	role = ndb.StringProperty()
	tournament=ndb.KeyProperty(kind='Tournament')

class Tournament(ndb.Model):
	"""Models an individual tournament"""
	name = ndb.StringProperty()
	owner = ndb.KeyProperty(kind='TUser', repeated=True)
	trackpin = ndb.StringProperty()
	start = ndb.DateProperty()
	end = ndb.DateProperty()
	customRoomStatus = ndb.StringProperty(repeated=True)

	#Return a list of rooms attached to the tournament
	def rooms(self):
		return Room.query(ancestor=self.key).order(Room.name)

	#Return the pre-registration record for the tournament
	def preRegRecord(self):
		return PreRegRecord.query(ancestor=self.key)

	#Delete the tournament and its attached rooms and registration record
	def destroy(self):
		for r in self.rooms():
			r.key.delete()

		preReg = self.preRegRecord().get()
		if preReg:
			preReg.destroy()

		#Get rid of all of the attendance records
		q = Attending.query()
		q.filter(Attending.tournament == self.key)
		keys = q.fetch(keys_only=True)
		ndb.delete_multi(keys)

		self.key.delete()

class Room(ndb.Model):
	"""Models a room in a tournament"""
	name = ndb.StringProperty()
	active = ndb.BooleanProperty()
	status = ndb.StringProperty()
	changed = ndb.TimeProperty()
	comment = ndb.StringProperty()

class PreRegRecord(ndb.Model):
	"""Models the pre-registration of a tournament"""
	open = ndb.BooleanProperty()
	teamCap = ndb.IntegerProperty()

	#Delete the object and its associated judges, teams, and institutions
	def destroy(self):
		for j in self.indyJudges():
			j.key.delete()
		for t in self.teams():
			t.key.delete()
		for i in self.institutions():
			i.destroy()
		self.key.delete()

	def isRegistered(self, tuser):
		if tuser:
			return self.isInstitution(tuser) or self.isJudge(tuser) or self.isOpenTeam(tuser)

	#Independent Judges registered
	def indyJudges(self):
		return RegisteredIndependentJudge.query(ancestor=self.key)

	#Check if a user is registered as a judge
	def isJudge(self, tuser):
		if tuser:
			q = RegisteredIndependentJudge.query(ancestor=self.key)
			q = q.filter(RegisteredIndependentJudge.user == tuser.key)

			return q.get()
		else:
			return None

	#Check if a user is registered a an Open Team
	def isOpenTeam(self, tuser):
		if tuser:
			q = RegisteredOpenTeam.query(ancestor=self.key)
			q = q.filter(RegisteredOpenTeam.user == tuser.key)

			return q.get()
		else:
			return None

	#Check if a user is registered as an institution
	def isInstitution(self, tuser):
		if tuser:
			q = RegisteredInstitution.query(ancestor=self.key)
			q = q.filter(RegisteredInstitution.user == tuser.key)

			return q.get()
		else:
			return None

	#Teams registered
	def teams(self):
		return RegisteredOpenTeam.query(ancestor=self.key)

	#Institutions registered
	def institutions(self):
		return RegisteredInstitution.query(ancestor=self.key)

	#Return the total number of teams, independent + institutional
	def totalTeamCount(self):
		nTeams = self.teams().count(limit=1000)
		for i in self.institutions():
			nTeams = nTeams + i.teams().count(limit=1000)
		return nTeams

	#Return the total number of judges, independent + institutional
	def totalJudgeCount(self):
		nJudges = self.indyJudges().count(limit=1000)
		for i in self.institutions():
			nJudges = nJudges + i.judges().count(limit=1000)
		return nJudges

class RegisteredIndependentJudge(ndb.Model):
	"""Models a participant in the tournament"""
	name = ndb.StringProperty()
	cv = ndb.TextProperty()
	email = ndb.StringProperty()
	phone = ndb.StringProperty()
	user = ndb.KeyProperty(kind='TUser')

	def prefs(self):
		return RegisteredPreferences.query(ancestor=self.key).get()

	#Check if the user is authorised to modify
	def authorised(self, tuser):
		#Check if they own the object
		if self.user == tuser.key:
			return True
		#Check if they own the tournament
		elif tuser.key in self.key.parent().parent().get().owner:
			return True
		else:
			return False

class RegisteredOpenTeam(ndb.Model):
	"""Models an open team in the tournament"""
	leadName = ndb.StringProperty()
	email = ndb.StringProperty()
	phone = ndb.StringProperty()
	user = ndb.KeyProperty(kind='TUser')
	teamName = ndb.StringProperty()
	sp1Name = ndb.StringProperty()
	sp2Name = ndb.StringProperty()
	sp1ESL = ndb.BooleanProperty()
	sp2ESL = ndb.BooleanProperty()
	sp1Novice = ndb.BooleanProperty()
	sp2Novice = ndb.BooleanProperty()

	def authorised(self, tuser):
		if self.user == tuser.key:
			return True
		elif tuser.key in self.key.parent().parent().get().owner:
			return True
		else:
			return False

	def institutionName(self):
		return "Composite"

class RegisteredInstitution(ndb.Model):
	"""Models an institution registered for the tournment"""
	name = ndb.StringProperty()
	leadName = ndb.StringProperty()
	phone = ndb.StringProperty()
	user = ndb.KeyProperty(kind='TUser')
	email = ndb.StringProperty()

	#Return a query of all of the teams attached to the institution
	def teams(self):
		return InstitutionTeam.query(ancestor=self.key)

	#Return a query of all of the judges attached to the institution
	def judges(self):
		return InstitutionJudge.query(ancestor=self.key)

	#Check whether they are authorised to edit
	def authorised(self, tuser):
		return self.user == tuser.key or tuser.key in self.key.parent().parent().get().owner
	#Delete the institution along with its teams and judges
	def destroy(self):
		for judge in self.judges():
			judge.key.delete()
		for team in self.teams():
			team.key.delete()
		self.key.delete()

class InstitutionTeam(ndb.Model):
	"""A team attached to an institution"""
	teamName = ndb.StringProperty()
	sp1Name = ndb.StringProperty()
	sp2Name = ndb.StringProperty()
	sp1ESL = ndb.BooleanProperty()
	sp2ESL = ndb.BooleanProperty()
	sp1Novice = ndb.BooleanProperty()
	sp2Novice = ndb.BooleanProperty()

	def authorised(self, tuser):
		return self.key.parent().get().user == tuser.key or  tuser.key in self.key.parent().parent().parent().get().owner

	def institutionName(self):
		return self.key.parent().get().name

class InstitutionJudge(ndb.Model):
	"""A judge attached to an institution"""
	name = ndb.StringProperty()
	cv = ndb.TextProperty()

	#Check if the user is authorised to modify
	def authorised(self, tuser):
		#Check if they own the object
		if self.key.parent().get().user == tuser.key:
			return True
		#Check if they are the tournament owner
		elif tuser.key in self.key.parent().parent().parent().get().owner:
			return True
		else:
			return False

class RegisteredPreferences(ndb.Model):
	"""The preferences of a registered participant"""
	vegetarian = ndb.BooleanProperty()
	glutenfree = ndb.BooleanProperty()
	vegan = ndb.BooleanProperty()
	halal = ndb.BooleanProperty()
	kosher = ndb.BooleanProperty()
	special = ndb.StringProperty()

class PerformanceTournamentSpeakerRecord(ndb.Model):
	"""The performance record of a speaker at a tournament"""
	tournament = ndb.StringProperty()
	startDate = ndb.DateProperty()
	teamName = ndb.StringProperty()

	teamRank = ndb.IntegerProperty()
	averageSpeaks = ndb.FloatProperty()
	speakerRank = ndb.IntegerProperty()

	champion = ndb.BooleanProperty()
	finalist = ndb.BooleanProperty()
	semifinalist = ndb.BooleanProperty()
	quarterfinalist = ndb.BooleanProperty()
	octofinalist = ndb.BooleanProperty()
	doubleoctofinalist = ndb.BooleanProperty()

	ESLBreak = ndb.BooleanProperty()
	ESLChampion = ndb.BooleanProperty()
	EFLBreak = ndb.BooleanProperty()
	EFLChampion = ndb.BooleanProperty()

	NoviceBreak = ndb.BooleanProperty()
	NoviceChampion = ndb.BooleanProperty()


class TreeNode:
     def __init__(self, x):
         self.val = x
         self.left = None
         self.right = None

from collections import defaultdict

class Solution:
    def widthOfBinaryTree(self, root: TreeNode):
        stack = [(0,0,root)]
        info = defaultdict(list)
        while stack:
            lev,pos,node = stack.pop()
            info[lev].append(pos)
            if node:
                if node.right:
                    stack.append((lev+1,pos*2,node.right))
                if node.left:
                    stack.append((lev+1,pos*2+1,node.left))
        best = 0
        for key in info:
            best = max(best,max(info[key])-min(info[key])+1)
        return best