'''Trains a simple deep NN on the MNIST dataset.
Gets to 98.40% test accuracy after 20 epochs
(there is *a lot* of margin for parameter tuning).
2 seconds per epoch on a K520 GPU.
'''

# from __future__ import print_function

from framework.metric_learning.basemetriclearning import BasemetricLearning
from framework.metric_learning.metriclearningterms import MetricLearningTerms

import copy
import numpy as np
import numpy.random as rng
import pandas as pd
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Activation, Add, Merge, Input, add, merge
from keras.optimizers import RMSprop, Adam
from keras.preprocessing import sequence
from keras import backend as K
from keras.layers.normalization import BatchNormalization
from keras.callbacks import ModelCheckpoint
from keras.regularizers import l2
from sklearn.preprocessing import StandardScaler
from sklearn.metrics.pairwise import pairwise_distances
import os
import copy

import copy
import numpy as np
import numpy.random as rng
import pandas as pd
import keras
from keras.layers import Input,merge
from keras.models import Sequential, Model
from sklearn.preprocessing import StandardScaler
from keras.layers.core import *
import pandas as pd
import pdb
import matplotlib.pyplot as plt
import multiprocessing as mp

# np.random.seed(0)

class NonlinearDeepMetric(BasemetricLearning):
    def __init__(self, **kwargs):
        super().__init__(MetricLearningTerms.NONLINEAR)
        self.kwargs = kwargs
        self.batch_size = 10
        self.epochs = 4

    def train(self, data_pairs, similarity_labels,**kwargs):
        self.scaler = StandardScaler()
        X1 = []
        X2 = []
        for x, y in data_pairs:
            X1.append(x)
            X2.append(y)
        # print(len(X1), len(X2), len(similarity_labels))
        number_classes = len(np.unique(similarity_labels))
        # convert class vectors to binary class matrices

        train_portion = 0.8
        input_shape = data_pairs[0][0].shape

        kernels = 100
        s_size = len(similarity_labels)
        x1_train = X1[:int(s_size * train_portion)]
        x2_train = X2[:int(s_size * train_portion)]
        x1_test = X1[int(s_size * train_portion):]
        x2_test = X2[int(s_size * train_portion):]
        y_train = similarity_labels[:int(s_size * train_portion)]
        y_test = similarity_labels[int(s_size * train_portion): ]

        labels = keras.utils.to_categorical(similarity_labels, number_classes)
        y_train = keras.utils.to_categorical(y_train, number_classes)
        y_test = keras.utils.to_categorical(y_test, number_classes)

        left_input = Input(input_shape)
        right_input = Input(input_shape)

        #build f(x) to use in each siamese 'leg'
        model = Sequential()
        # model.add(Dense(kernels, activation='relu', input_shape = input_shape, kernel_regularizer=l2(2e-4)))
        # model.add(Dense(kernels, activation='relu', kernel_regularizer=l2(2e-4)))
        # model.add(Dense(kernels, activation='sigmoid', kernel_regularizer=l2(1e-3)))

        model.add(Dense(kernels, activation='relu', input_shape = input_shape))
        model.add(Dropout(0.2))
        model.add(Dense(kernels, activation='relu'))
        model.add(Dropout(0.2))
        model.add(Dense(kernels, activation='sigmoid'))

        #encode each of the two inputs into a vector with the model
        encoded_l = model(left_input)
        encoded_r = model(right_input)

        #merge two encoded inputs with the l1 distance between them
        L1_distance = lambda x: K.abs(x[0]-x[1])

        both = merge([encoded_l, encoded_r], mode = L1_distance, output_shape=lambda x: x[0])
        prediction = Dense(number_classes,activation='sigmoid')(both)
        siamese_net = keras.models.Model(inputs=[left_input,right_input],outputs=prediction)

        optimizer = RMSprop()
        siamese_net.compile(loss=self.contrastive_loss, optimizer=optimizer)

        siamese_net.count_params()

        x1_train = np.array(x1_train)
        x2_train = np.array(x2_train)
        y_train = np.array(y_train)

        x1_test = np.array(x1_test)
        x2_test = np.array(x2_test)
        y_test = np.array(y_test)

        x1_train = np.array(X1)
        x2_train = np.array(X2)
        y_train = keras.utils.to_categorical(similarity_labels, number_classes)

        data = np.append(x1_train, x2_train, axis=0)

        self.scaler.fit(data)
        x1_train = self.scaler.transform(x1_train)
        x2_train = self.scaler.transform(x2_train)

        history = siamese_net.fit([x1_train, x2_train], y_train,
                            batch_size=self.batch_size,
                            epochs=self.epochs,
                            validation_data=([x1_train, x2_train], y_train), verbose=0)

        score = siamese_net.evaluate([x1_test, x2_test], y_test, verbose=0)
        print('Test loss:', score)
        print('Test accuracy:', score)

        inp1, inp2 = siamese_net.input

        func = siamese_net.layers[-2].input
        dist = siamese_net.layers[-2].output
        self.functor1 = K.function([inp1]+ [K.learning_phase()], [func[0]])
        self.functor2 = K.function([inp2]+ [K.learning_phase()], [func[1]])
        self.functor3 = K.function([*[inp1, inp2]]+ [K.learning_phase()], [dist])
        return score

    # def transform(self, data_pairs):
    #     x, y = data_pairs
    #     x = self.scaler.transform(np.array([x]))
    #     y = self.scaler.transform(np.array([y]))

    #     distance = self.functor3([*[x, y], 1.])
    #     return distance[0].mean()

    def contrastive_loss(self, y_true, y_pred):
        margin = 1
        return K.mean((1-y_true) * 0.5 * K.square(y_pred) + 0.5 * y_true * K.square(K.maximum(margin - y_pred, 0)))

    def penalized_loss(self, branch1, branch2):
        def loss(y_true, y_pred):
            return K.mean(K.square(y_pred - y_true) - K.square(y_true - y_pred), axis=-1)
        return loss

    def euclideanSqDistance(self, inputs):
        if (len(inputs) != 2):
            raise 'oops'
        output = K.mean(K.square(inputs[0] - inputs[1]), axis=-1)
        output = K.expand_dims(output, 1)
        return output

    def W_init(self, shape,name="weigt"):
        """Initialize weights as in paper"""
        values = rng.normal(loc=0,scale=1e-2,size=shape)
        return K.variable(values,name=name)
        #//TODO: figure out how to initialize layer biases in keras.

    def b_init(self, shape,name="bais"):
        """Initialize bias as in paper"""
        values = rng.normal(loc=0.5,scale=1e-2,size=shape)
        return K.variable(values,name=name)

# def euc_dist(x):
#     'Merge function: euclidean_distance(u,v)'
#     s = x[0] - x[1]
#     output = K.sum(K.square(s),axis=1,keepdims=True)
#     return output

# def euc_dist_shape(input_shape):
#     'Merge output shape'
#     shape = list(input_shape)
#     outshape = (shape[0][0],1)
#     return tuple(outshape)

# class Deep_Metric(BasemetricLearning):
#     def __init__(self, **kwargs):
#         super().__init__(MetricLearningTerms.NONLINEAR)
#         self.kwargs = kwargs
#         self.batch_size = 10
#         self.epochs = 4

#     # def train(self, x1_train,x2_train,y_train,x1_test,x2_test,y_test):
#     def train(self, data_pairs, similarity_labels):
#         import keras

#         self.scaler = StandardScaler()
#         self.batch_size = 32
#         self.epochs = 100

#         X1 = []
#         X2 = []
#         for x, y in data_pairs:
#             X1.append(x)
#             X2.append(y)

#         kernels = 20
#         s_size = len(similarity_labels)

#         train_portion = 0.8
#         input_shape = data_pairs[0][0].shape

#         x1_train = X1[:int(s_size * train_portion)]
#         x2_train = X2[:int(s_size * train_portion)]
#         x1_test = X1[int(s_size * train_portion):]
#         x2_test = X2[int(s_size * train_portion):]
#         y_train = similarity_labels[:int(s_size * train_portion)]
#         y_test = similarity_labels[int(s_size * train_portion): ]

#         # input_shape = x1_train[0].shape

#         x1_train = np.array(x1_train)
#         x2_train = np.array(x2_train)
#         y_train = np.array(y_train)

#         x1_test = np.array(x1_test)
#         x2_test = np.array(x2_test)
#         y_test = np.array(y_test)

#         # x1_train = np.array(X1)
#         # x2_train = np.array(X2)

#         # y_train = keras.utils.to_categorical(similarity_labels, number_classes)

#         print(input_shape)

#         # define networks
#         left_input = Input(input_shape)
#         right_input = Input(input_shape)
#         model = Sequential()
#         model.add(Dense(kernels, activation='relu', input_shape = input_shape))
#         model.add(Dropout(0.3))
#         model.add(Dense(int(kernels/2), activation='relu'))
#         model.add(Dropout(0.3))
#         model.add(Dense(kernels, activation='relu'))
#         model.add(Dropout(0.3))
#         encoded_l = model(left_input)
#         encoded_r = model(right_input)
#         both = merge([encoded_l,encoded_r],mode=euc_dist,output_shape=euc_dist_shape)
#         self.distance_model = Model(input=[left_input,right_input],outputs=both)
#         self.distance_model.compile(loss=self.contrastive_loss, optimizer='Adam')

#         print('===training===')
#         self.distance_model.fit([x1_train, x2_train],y_train,
#                             batch_size=self.batch_size,
#                             epochs=self.epochs,
#                             validation_data=([x1_test, x2_test], y_test), verbose=1)

#         self.d_test = self.distance_model.predict([x1_test,x2_test])
#         similar_ind = np.where(y_test == 1) [0]
#         dissimilar_ind = np.where(y_test == 0)[0]
#         print('similar distances %s'%np.mean(self.d_test[similar_ind]))
#         print('similar distances var %s' % np.std(self.d_test[similar_ind]))
#         print('dissimilar distances %s' % np.mean(self.d_test[dissimilar_ind]))
#         print('dissimilar distances var %s' % np.std(self.d_test[dissimilar_ind]))

#     def transform(self, data_pairs):
#         x, y = data_pairs
#         x = x.reshape((1,len(x)))
#         y = y.reshape((1,len(y)))
#         distance = self.distance_model.predict([x,y])
#         return distance
#         # processes = []
#         # for i in range(4):
#         #     p = mp.Process(target=self.transform_multi, args=(data_pairs,))
#         #     processes.append(p)
#         # distance =  [x.start() for x in processes]
#         # return distance

#     # def transform_multi(self, data_pairs):
#     #     x, y = data_pairs
#     #     x = x.reshape((1,len(x)))
#     #     y = y.reshape((1,len(y)))
#     #     distance = self.distance_model.predict([x,y], batch_size=2048)
#     #     return distance

#     def contrastive_loss(self, y_true, d):
#         # 1 means simlar, and 0 means dissimilar
#         margin = 1
#         return K.mean(y_true*0.5*d + (1-y_true)*0.5*K.maximum(margin-d,0))


from django.contrib.auth.decorators import login_required

from django.db.models import Sum
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect, get_object_or_404
from django.views.generic import CreateView, UpdateView, ListView, DetailView
from .models import Project, ProjectPicture, Category, ProjectReport, Comment, CommentReport, Denote

from django.http import JsonResponse
from django.shortcuts import render,redirect, get_object_or_404
from django.views.generic import CreateView, UpdateView, ListView, DetailView
from .models import Project, ProjectPicture, Category , Rate

from .forms import ProjectForm
from django.contrib import messages
from django.contrib.auth.models import User
from taggit.models import Tag
from django.template.defaultfilters import slugify
from django.conf import settings
from django.contrib.auth import login
from django.core.mail import send_mail

# Create your views here.

def index(request):
    return render(request, 'project/index.html', {})

@login_required
def create_project(request):
    form = ProjectForm()
    if request.method == "POST":
        form = ProjectForm(request.POST, request.FILES)
        if form.is_valid():
            project = form.save(commit=False)
            project.user = request.user
            # project.user = User.objects.first()
            project.slug = slugify(project.title)
            project.save()
            form.save_m2m()
            for image in request.FILES.getlist('images'):
                ProjectPicture.objects.create(project=project, image=image)
            messages.success(request, 'project has been created')
            return redirect('home')
        return render(request, 'project/createproject.html', {"form": form})
    return render(request, 'project/createproject.html', {"form": form})

def project_detail(request, project_id):
    project = get_object_or_404(Project, pk=project_id)
    context = {
        "project": project,
        "project_target_collected": Denote.objects.filter(project=project).aggregate(Sum('amount'))['amount__sum'] or 0.0
    }
    return render(request, 'project/project_detail.html', context)

@login_required
def project_report(request):
    rec = ProjectReport.objects.create(user=request.user, body=request.GET.get('body'), project_id=request.GET.get('project'))
    if rec:
        data = {'return': True}
    else:
        data = {'return': False}
    return JsonResponse(data)

@login_required
def post_comment(request, project_id):
    if request.method == "POST":
        if Project.objects.filter(pk=project_id).exists():
            comment = Comment.objects.create(body=request.POST.get('comment-body'), user=request.user, project_id=project_id)
            return redirect('project_detail', project_id)
    return redirect('project_detail', project_id)

def comment_report(request, comment_id):
    if Comment.objects.filter(pk=comment_id).exists():
        rec = CommentReport.objects.create(body=request.GET.get('body'), user=request.user, comment_id=comment_id)
    if rec:
        return JsonResponse({"return": True})
    return JsonResponse({"return": False})

@login_required
def fund_project(request, project_id):
    if request.method == 'POST':
        if Project.objects.filter(pk=project_id).exists():
            rec = Denote.objects.create(user=request.user, project_id=project_id, amount=request.POST.get('amount'))
            if rec:
                messages.success(request, 'Your fund has been submitted')
                return redirect('project_detail', project_id)
            else:
                messages.success(request, 'error submitting the fund')
                return redirect('project_detail', project_id)

@login_required
def rating_project(request, project_id, rating_val):
    # user = get_object_or_404(User, pk=request.user.id)
    # project = get_object_or_404(Project, pk=project_id)

    try:
        # if user update his rating
        rating = Rate.objects.get(user=request.user, project_id=project_id)
        rating.rating = rating_val
        rating.save()
        message = {
            'status': 'update rating',
            'number_of_rating': rating.project.number_of_rating()
            }
    except:
        # user make first rating on this project
        rating = Rate(user=request.user, project_id=project_id, rating=rating_val)
        rating.save()
        message = {
            'status': 'new rating',
            'number_of_rating': rating.project.number_of_rating(),
            }

    return JsonResponse(message)


class Solution(object):
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        low = 0
        high = len(nums)-1
        if low == high and nums[low] == target:
            return low
        pivot = self.get_pivot(nums,0, high)
        if pivot != -1:
            if target == nums[pivot]:
                return pivot
            elif target > nums[pivot]:
                if target == nums[0]:
                    return 0
                elif target < nums[0]:
                    return self.search_range(nums, pivot+1, high, target)
                else:
                    return self.search_range(nums, low,pivot -1, target)
            else:
                return -1
        else:
            return self.search_range(nums, low, high, target)

    def get_pivot(self, nums, low, high):

        if low > high or low == high or nums[low] < nums[high]:
            return -1

        mid = int((low + high)/2)

        if nums[mid] > nums[mid+1] and nums[mid-1] < nums[mid]:
            return (mid+1)
        elif nums[mid-1] > nums[mid] and nums[mid+1] > nums[mid]:
            return mid
        elif nums[mid] < nums[0]:
            return self.get_pivot(nums,low,mid-1)
        else:
            return self.get_pivot(nums,mid+1,high)

    def search_range(self, nums, low, high, target):
        mid = int((low + high)/2)
        if low > high:
            return -1

        if nums[mid] == target:
            return mid
        elif nums[mid] > target:
            return self.search_range(nums,low, mid-1, target)
        elif nums[mid] < target:
            return self.search_range(nums,mid + 1, high, target)
        else:
            return -1

    def search_first_last(self, nums, target):

        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        low = 0
        high = len(nums)-1

        start_pos = -1
        end_pos = -1

        if low == high:
            if nums[low] == target:
                start_pos = low
                end_pos = high

        while(low <= high):
            mid = int((low+high)/2)

            if start_pos != -1 and end_pos != -1:
                break

            #if mid <= 0 or mid+1 > len(nums)-1:
            #    break
            '''if low == 0 and low == high-1:
                if nums[low] == target:
                    start_pos = low
                if nums[high] == target:
                    end_pos = high
            elif low == high:
                if nums[low] == target :
                    end_pos = high'''

            if nums[mid] < target:
                low = mid + 1
            elif nums[mid] > target:
                high = mid - 1
            elif nums[mid] == target and  (mid == 0 or nums[mid-1] < target):
                start_pos = mid
                low = mid + 1
            elif nums[mid] == target and ((mid + 1)== len(nums) or nums[mid + 1] > target):
                end_pos = mid
                high = mid - 1

        return [start_pos, end_pos]

    def extreme_insertion_index(self, nums, target, left):
        lo = 0
        hi = len(nums)

        while lo < hi:
            mid = (lo + hi) // 2
            if nums[mid] > target or (left and target == nums[mid]):
                hi = mid
            else:
                lo = mid+1

        return lo

    def searchRange(self, nums, target):
        left_idx = self.extreme_insertion_index(nums, target, True)

        if left_idx == len(nums) or nums[left_idx] != target:
            return [-1, -1]

        return [left_idx, self.extreme_insertion_index(nums, target, False)-1]

    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        max_profit = 0
        min_price = prices[0]
        for index in range(1, len(prices)):
            max_profit = max(max_profit, (prices[index]-min_price))
            min_price = min(min_price, prices[index])
        return max_profit

solution = Solution()
#print(solution.search([3,1], 0))
#print(solution.search_first_last([1,3],1))
#print(solution.searchRange([1,3],1))
print(solution.maxProfit([7,1,5,3,6,4]))


import unittest2 as unittest
import lxml
from DateTime import DateTime

import zope.event
from Products.CMFCore.utils import getToolByName
from Products.PluggableAuthService.events import PropertiesUpdated

from plone.app.testing import TEST_USER_ID
from plone.app.testing import logout, login, setRoles

from emas.theme.tests.base import FUNCTIONAL_TESTING
from emas.theme.browser.tests.test_practice_service_messages_viewlet import \
    find_viewlet
from emas.theme.interfaces import IEmasThemeLayer
from emas.theme.eventhandlers import *

class TestEventhandlers(unittest.TestCase):
    """ Test the intelligent practice messages service viewlets  """

    layer = FUNCTIONAL_TESTING

    def setUp(self):
        super(TestEventhandlers, self).setUp()
        self.portal = self.layer['portal']
        setRoles(self.portal, TEST_USER_ID, ['Reader'])
        self.context = self.portal.maths
        self.request = self.portal.REQUEST
        self.manager_name = 'plone.belowcontenttitle'
        self.themelayer = IEmasThemeLayer
        self.viewlet_name = 'emas.practice_service_messages'

    def test_first_login_message(self):
        portal_state = self.portal.restrictedTraverse('@@plone_portal_state')
        member = portal_state.member()
        setRoles(self.portal, TEST_USER_ID, ['Reader'])
        default = DateTime('2000/01/01')
        member.setProperties(login_time=default,
                             last_login_time=default)

        # The call to loginUser is the one that fires the required event.
        # The testcase's login method simly creates a new security manager, it
        # does not lead to the IUserLoggedInEvent.
        pmt = getToolByName(self.portal, 'portal_membership')
        pmt.loginUser(self.portal.REQUEST)

        viewlet = find_viewlet(self.context,
                               self.request,
                               self.manager_name,
                               self.viewlet_name,
                               self.themelayer)

        result = viewlet.render()
        doc = lxml.html.fromstring(result)
        elements = doc.xpath('//a[contains(.,"@@practice")]')
        self.assertEqual(len(elements), 0,
                         'No link to @@practice found.')

    def test_onMemberPropsUpdates_subscribe(self):
        portal_state = self.portal.restrictedTraverse('@@plone_portal_state')
        member = portal_state.member()
        member.setProperties(subscribe_to_newsletter=True)
        event = PropertiesUpdated(member, 'subscribe_to_newsletter')
        update_newsletter_subscription(member, event)

    def test_onMemberPropsUpdates_unsubscribe(self):
        portal_state = self.portal.restrictedTraverse('@@plone_portal_state')
        member = portal_state.member()
        member.setProperties(subscribe_to_newsletter=False)
        event = PropertiesUpdated(member, 'subscribe_to_newsletter')
        update_newsletter_subscription(member, event)

    def test_update_newsletter_subscription(self):
        self.fail()


# -*- coding: utf-8 -*-
from openerp.tests import common
import logging
logger = logging.getLogger(__name__)

class TestResPartnerSecurityFields(common.TransactionCase):
    def setUp(self):
        super(TestResPartnerSecurityFields, self).setUp()

        # ATTENTION: It is necessary to clean the ir.model.data cache or fragments will remain between tests which
        #            would lead to wrong results for self.env.ref()
        @self.addCleanup
        def reset_model_data_cache():
            self.env.invalidate_all()
            self.env['ir.model.data'].clear_caches()
            self.env['res.groups'].clear_caches()

        # Create a partner with a phone number
        self.partner = self.env["res.partner"].create({
            "name": "Test User",
            "phone": " 0043 03172 12-34-56-7 ",
            "mobile": " 0043 (0) 660 12-34-56-7 ",
        })

    def test_01_find_phone_single_match(self):
        partner_obj = self.env['res.partner']
        found = partner_obj.search_phone_fuzzy('+43 3172 123 456 7')
        self.assertTrue(len(found) == 1)

    def test_02_find_mobile_single_match(self):
        partner_obj = self.env['res.partner']
        found = partner_obj.search_phone_fuzzy('+43 660_12__34-56--(7)')
        self.assertTrue(len(found) == 1)

    def test_03_find_phone_single_match_multiple_zeros(self):
        partner_obj = self.env['res.partner']
        found = partner_obj.search_phone_fuzzy('00043 (0) 3172 123 456 7')
        self.assertTrue(len(found) == 1)

    def test_04_find_phone_single_match_multiple_plus(self):
        partner_obj = self.env['res.partner']
        found = partner_obj.search_phone_fuzzy('++43 (0) 3172+123++456+7+')
        self.assertTrue(len(found) == 1)

    def test_05_find_phone_and_mobile_single_match(self):
        self.partner.phone = self.partner.mobile
        partner_obj = self.env['res.partner']
        found = partner_obj.search_phone_fuzzy('  0043 0 660 1234567  ')
        self.assertTrue(len(found) == 1)
        for record, values in found.iteritems():
            self.assertTrue(all(field in values.keys() for field in ['phone', 'mobile']))

    def test_06_find_phone_multi_match(self):
        self.partner2 = self.env["res.partner"].create({
            "name": "Test User 2",
            "mobile": "  ++43 (0) 660 1234 567 ",
        })
        partner_obj = self.env['res.partner']
        found = partner_obj.search_phone_fuzzy('  +43 660 1234567  ')
        self.assertTrue(len(found) == 2)

    def test_07_find_phone_no_country_no_match(self):
        partner_obj = self.env['res.partner']
        found = partner_obj.search_phone_fuzzy('03172 1234567')
        self.assertTrue(len(found) == 0)

    def test_08_find_phone_no_country_in_both_match(self):
        partner_obj = self.env['res.partner']
        self.partner.phone = '03172 1234567'
        found = partner_obj.search_phone_fuzzy('(0) 3172 123-456-7')
        self.assertTrue(len(found) == 1)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 11 15:55:57 2017

@author: owen
"""

# On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K moves.
# The rows and columns are 0 indexed, so the top-left square is (0, 0), and the bottom-right square is (N-1, N-1).

# A chess knight has 8 possible moves it can make, as illustrated below.
# Each move is two squares in a cardinal direction, then one square in an orthogonal direction.

# Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.

# The knight continues moving until it has made exactly K moves or has moved off the chessboard.
# Return the probability that the knight remains on the board after it has stopped moving.

class Solution:
    def knightProbability(self, N, K, r, c):
        """
        :type N: int
        :type K: int
        :type r: int
        :type c: int
        :rtype: float
        """
        # time O(8 * N * N * K), space O(N * N)
        if K == 0:
            return 1.0

        dp = [[1] * N for _ in range(N)] # dp[i][j]: total ways which starts at (i, j) and still remains on the board after k-th move
        dirs = [[-1,-2], [-2,-1], [-2,1], [-1,2], [2,-1], [1,-2], [1,2], [2,1]]
        for __ in range(K):
            nxt = [[0] * N for _ in range(N)]
            for i in range(N):
                for j in range(N):
                    for d in dirs:
                        x, y = i + d[0], j + d[1]
                        if x < 0 or x >= N or y < 0 or y >= N:
                            continue
                        nxt[i][j] += dp[x][y]
            dp = nxt

        return dp[r][c] / 8 ** K

# http://www.cnblogs.com/grandyang/p/7639153.html
#class Solution(object):
#    def knightProbability(self, N, K, r, c):
#        """
#        :type N: int
#        :type K: int
#        :type r: int
#        :type c: int
#        :rtype: float
#        """
#        dirs=[[-1,-2],[-2,-1],[-2,1],[-1,2],[2,-1],[1,-2],[1,2],[2,1]]
#
#        def helper(dp, N, k, i, j):
#            if k==0:
#                return 1
#
#            if dp[k][i][j]!=0:
#                return dp[k][i][j]
#
#            for d in dirs:
#                x,y=i+d[0], j+d[1]
#                if x<0 or x>=N or y<0 or y>=N:
#                    continue
#                dp[k][i][j]+=helper(dp,N,k-1,x,y)
#
#            return dp[k][i][j]
#
#
#        dp=[ [ [0.0]*N for _ in range(N)] for _ in range(K+1)] # dp[k][i][j]: total ways which still remains on the board after k move
#        return helper(dp,N,K,r,c)/(8**K)

# https://leetcode.com/problems/knight-probability-in-chessboard/solution/
class Solution:
    def knightProbability(self, N, K, r, c):
        """
        :type N: int
        :type K: int
        :type r: int
        :type c: int
        :rtype: float
        """
        # Matrix Exponentiation, Time O(n^6 log(k)), space O(n^4), code works for Py.2x, not Py.3x ...
        def canonical(x, y):
            if 2 * x > N:
                x = N - 1 - x
            if 2 * y > N:
                y = N - 1 - y
            if x > y:
                x, y = y, x
            return x*N + y

        def matrix_mult(A, B):
            ZB = zip(*B)
            return [[sum(a * b for a, b in zip(row, col))
                     for col in ZB] for row in A]

        def matrix_expo(A, K):
            if K == 0:
                return [[+(i==j) for j in range(len(A))]
                        for i in range(len(A))]
            if K == 1:
                return A
            elif K % 2:
                return matrix_mult(matrix_expo(A, K-1), A)
            B = matrix_expo(A, K//2)
            return matrix_mult(B, B)

        index = [0] * (N*N)
        t = 0
        for x in range(N):
            for y in range(N):
                if x*N + y == canonical(x, y):
                    index[x*N + y] = t
                    t += 1
                else:
                    index[x*N + y] = index[canonical(x, y)]

        T = []
        for x in range(N):
            for y in range(N):
                if x*N + y == canonical(x, y):
                    row = [0] * t
                    for dx, dy in ((2,1),(2,-1),(-2,1),(-2,-1),
                                    (1,2),(1,-2),(-1,2),(-1,-2)):
                        if 0 <= x+dx < N and 0 <= y+dy < N:
                            row[index[(x+dx)*N + y+dy]] += 0.125
                    T.append(row)

        Tk = matrix_expo(T, K)
        i = index[r * N + c]
        return sum(Tk[i])

if __name__=="__main__":
    print(Solution().knightProbability(3, 2, 0, 0))
    print(Solution().knightProbability(1, 1, 0, 0))
    print(Solution().knightProbability(3, 1, 1, 1))
    print(Solution().knightProbability(3, 2, 1, 2)) # wrong

688. Knight Probability in Chessboard.py


#!/usr/bin/env python
# Copyright (c) 2017 Edouard FISCHER
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#     The above copyright notice and this permission notice shall be included
#     in all copies or substantial portions of the Software.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#     OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
#     NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
#     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
#     OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
#     USE OR OTHER DEALINGS IN THE SOFTWARE.

from argparse import ArgumentParser
from functools import wraps
import itertools
import multiprocessing
import signal
import os, errno
import numpy as np

# remove informative logging from tensorflow
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import tensorflow as tf

import generate
import model

# decorator for multiprocessing generator function
def mpgen(f):
    def mainmp(q, args, kwargs):
        try:
            for item in f(*args, **kwargs):
                q.put(item)
        finally:
            q.close()
    @wraps(f)
    def wrapped(*args, **kwargs):
        q = multiprocessing.Queue(3)
        proc = multiprocessing.Process(target=mainmp, args=(q, args, kwargs))
        proc.start()
        try:
            while True:
                item = q.get()
                yield item
        finally:
            proc.terminate()
            proc.join()
    return wrapped

@mpgen
def read_batches(shape, options, batch_size):
    """
    Returns a generator of batches
    """
    # get an (infinite) image generator
    g = generate.generate_ims(shape, options.bg)
    while True:
        #yield a batch of images of size batch_size
        yield list(itertools.islice(g, batch_size))

# default arguments
SAVE = 'my_model'
# for command line interpreter
def build_parser():
    parser = ArgumentParser()
    parser.add_argument('--sparse', '-x', type=float,
            dest='sparse', help="sparsity penalty (default '%(default)s')",
            metavar='SPARSE', default=0.0)
    parser.add_argument('--weights', '-w',
            dest='weights', help="load pre-trained weights",
            metavar='WEIGHTS')
    parser.add_argument('--save', '-s',
            dest='save', help="weight backup path (default '%(default)s')",
            metavar='SAVE', default=SAVE)
    parser.add_argument('--images', '-i',
            dest='bg', help='train images folder',
            metavar='BG', required=True)
    parser.add_argument('--width', '-l', type=int,
            dest='width', help="width of train images",
            metavar='WIDTH', required=True)
    parser.add_argument('--height', '-u', type=int,
            dest='height', help="height of train images",
            metavar='HEIGHT', required=True)
    parser.add_argument('--version', '-V', action='version', version='%(prog)s 0.0')
    return parser

def train(learn_rate, batch_size, shape, options):
    """
    Runs a training loop, minimizing L2 norm of the reconstruction error plus
    a L1 penalty on coding layer for sparsity
    """
    # set Session
    config = tf.ConfigProto()
    config.allow_soft_placement = True
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    # get model
    if options.weights == None:
        # create model
        x, y, params_enc = model.get_encoder_layers()
        x_, params_dec = model.get_decoder_layers(y, params_enc)
        graph = tf.get_default_graph()
        adam = tf.train.AdamOptimizer(learn_rate,name="adam")
        y_ = tf.placeholder(tf.float32, [None, None, None], name='y_')
        loss = tf.reduce_mean(tf.square(x_[:,:,:,0] - y_), name="loss")
        cst = tf.constant(options.sparse, name="sparsity")
        spa = tf.reduce_mean(tf.abs(y), name = "sparse")
        train_step = adam.minimize(loss + cst * spa, name="train_step")
        init = tf.global_variables_initializer()
        sess.run(init)
    else:
        # restore model from files
        print('Restoring model graph ...', end='')
        saver = tf.train.import_meta_graph('./' + options.weights + '/' + options.weights + '.meta')
        print('Done.')
        print('Restoring values from checkpoint ...', end='')
        saver.restore(sess, tf.train.latest_checkpoint('./' + options.weights))
        print('Done.')
        graph = tf.get_default_graph()
        x = graph.get_tensor_by_name('Input:0')
        y = graph.get_tensor_by_name('Code:0')
        x_ = graph.get_tensor_by_name('reconstruct:0')
        y_ = graph.get_tensor_by_name('y_:0')
        loss = graph.get_tensor_by_name('loss:0')
        spa = graph.get_tensor_by_name('sparse:0')
        train_step = graph.get_operation_by_name('train_step')

    # save model before exit
    make_sigint_handler(sess, options.save)

    # iterate train steps on batches
    batch_iter = enumerate(read_batches(shape, options, batch_size))
    print('┌────────┬─────────┬─────────┐')
    print('│ #Batch │ Reconst.│ Sparsity│')
    print('├────────┼─────────┼─────────┤')
    for batch_idx, batch_xs in batch_iter:
        _, ls, sp = sess.run([train_step, loss, spa], feed_dict={x: batch_xs, y_: batch_xs})
        print("│{:8d}│{:9.5f}│{:9.5f}│".format(batch_idx, np.sqrt(ls), sp))

def make_sigint_handler(sess,filename):
    """
    Handle CTRL-C from the user input, and saves the state of the network
    """
    def signal_handler(sig, frame):
        print('\n\033[1m\033[34m===== train.py[pid:{}] interrupted =====\033[0m'.format(os.getpid()))
        try:
            # create directory if doesn't exist
            os.makedirs(filename)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise
        saver = tf.train.Saver()
        print('Saving weights on', saver.save(sess, './' + filename + '/' + filename), ': done.')
        exit(0)
    # connect handler callback to SIGINT signal
    signal.signal(signal.SIGINT, signal_handler)

def main():
    print('\033[1m\033[34mTensorflow version: {} compiler: {} git: {}\033[0m'.format(tf.__version__, tf.__compiler_version__, tf.__git_version__))
    parser = build_parser()
    options = parser.parse_args()

    if options.weights == None:
        print('Training will start from new weights.')

    shape = (options.width, options.height)
    train(learn_rate=0.002,
          batch_size=50,
          shape=shape, options=options)

if __name__ == '__main__':
    main()


from __future__ import unicode_literals

import re
from django.core import validators
from django.core.exceptions import ValidationError

from django.core.exceptions import ImproperlyConfigured
from django import forms
from django.forms.widgets import Input
from django.forms.extras import SelectDateWidget
from django.utils.translation import ugettext_lazy as _

from forms_builder.forms.settings import USE_HTML5, EXTRA_FIELDS, EXTRA_WIDGETS
from forms_builder.forms.utils import html5_field, import_attr

class UrlMediaFileInput(Input):
    input_type = 'file'

    def __init__(self, *args, **kwargs):
        super(UrlMediaFileInput, self).__init__(*args, **kwargs)
        self.needs_multipart_form = False

    def render(self, name, value, attrs=None):
        attrs = attrs or {}
        attrs['upload_media'] = ""
        return super(UrlMediaFileInput, self).render(name, None, attrs=attrs)

    def value_from_datadict(self, data, files, name):
        "Url Media widgets take link to data from POST, not FILES"
        url = data.get(name, None)
        uploaded_file = files.get(name, None)
        if url:
            url = str(url.strip()) if url else None
        elif uploaded_file:
            uploaded_file.name = re.sub('[\'\!\@\#\$\%\^\&\*\(\)]', "", uploaded_file.name, 0, 0)
            self.needs_multipart_form = True
        return url or uploaded_file

    def _has_changed(self, initial, data):
        if data is None:
            return False
        return True

class MediaFileField(forms.CharField):

    @property
    def is_store_file(self):
        return self.widget.needs_multipart_form

    def to_python(self, value):
        """Return unicode if widget needs_multipart_form is False,
        otherwise UploadedFile
        """
        if not self.is_store_file:
            return super(forms.CharField, self).to_python(value)
        else:
            return self.file_to_python(value)

    def file_to_python(self, data):
        """
        Treet data like it do FileField!
        """
        if data in validators.EMPTY_VALUES:
            return None

        # UploadedFile objects should have name and size attributes.
        try:
            file_name = data.name
            file_size = data.size
        except AttributeError:
            raise ValidationError(self.error_messages['invalid'])
        return data

    def clean(self, value):
        """Custom method!
        Will not run validators if widget needs_multipart_form is True.

        Motivation:
          It can store file or link to GCS object.
        """
        value = self.to_python(value)
        self.validate(value)
        if not self.is_store_file:
            self.run_validators(value)
        return value

# Constants for all available field types.
TEXT = 1
TEXTAREA = 2
EMAIL = 3
CHECKBOX = 4
CHECKBOX_MULTIPLE = 5
SELECT = 6
SELECT_MULTIPLE = 7
RADIO_MULTIPLE = 8
FILE = 9
DATE = 10
DATE_TIME = 11
HIDDEN = 12
NUMBER = 13
URL = 14
DOB = 15
MEDIA_FILE = 16

# Names for all available field types.
NAMES = (
    (TEXT, _("Single line text")),
    (TEXTAREA, _("Multi line text")),
    (EMAIL, _("Email")),
    (NUMBER, _("Number")),
    (URL, _("URL")),
    (CHECKBOX, _("Check box")),
    (CHECKBOX_MULTIPLE, _("Check boxes")),
    (SELECT, _("Drop down")),
    (SELECT_MULTIPLE, _("Multi select")),
    (RADIO_MULTIPLE, _("Radio buttons")),
    (FILE, _("File upload")),
    (DATE, _("Date")),
    (DATE_TIME, _("Date/time")),
    (DOB, _("Date of birth")),
    (MEDIA_FILE, _("Media file")),
    (HIDDEN, _("Hidden")),
)

# Field classes for all available field types.
CLASSES = {
    TEXT: forms.CharField,
    TEXTAREA: forms.CharField,
    EMAIL: forms.EmailField,
    CHECKBOX: forms.BooleanField,
    CHECKBOX_MULTIPLE: forms.MultipleChoiceField,
    SELECT: forms.ChoiceField,
    SELECT_MULTIPLE: forms.MultipleChoiceField,
    RADIO_MULTIPLE: forms.ChoiceField,
    FILE: forms.FileField,
    DATE: forms.DateField,
    DATE_TIME: forms.DateTimeField,
    DOB: forms.DateField,
    HIDDEN: forms.CharField,
    NUMBER: forms.FloatField,
    URL: forms.URLField,
    MEDIA_FILE: MediaFileField,
}

# Widgets for field types where a specialised widget is required.
WIDGETS = {
    TEXTAREA: forms.Textarea,
    CHECKBOX_MULTIPLE: forms.CheckboxSelectMultiple,
    RADIO_MULTIPLE: forms.RadioSelect,
    DATE: SelectDateWidget,
    DOB: SelectDateWidget,
    MEDIA_FILE: UrlMediaFileInput,
    HIDDEN: forms.HiddenInput,
}

# Some helper groupings of field types.
CHOICES = (CHECKBOX, SELECT, RADIO_MULTIPLE)
DATES = (DATE, DATE_TIME, DOB)
MULTIPLE = (CHECKBOX_MULTIPLE, SELECT_MULTIPLE)

# HTML5 Widgets
if USE_HTML5:
    WIDGETS.update({
        DATE: html5_field("date", forms.DateInput),
        DATE_TIME: html5_field("datetime", forms.DateTimeInput),
        DOB: html5_field("date", forms.DateInput),
        EMAIL: html5_field("email", forms.TextInput),
        NUMBER: html5_field("number", forms.TextInput),
        URL: html5_field("url", forms.TextInput),
    })

# Add any custom fields defined.
for field_id, field_path, field_name in EXTRA_FIELDS:
    if field_id in CLASSES:
        err = "ID %s for field %s in FORMS_EXTRA_FIELDS already exists"
        raise ImproperlyConfigured(err % (field_id, field_name))
    CLASSES[field_id] = import_attr(field_path)
    NAMES += ((field_id, _(field_name)),)

# Add/update custom widgets.
for field_id, widget_path in EXTRA_WIDGETS:
    if field_id not in CLASSES:
        err = "ID %s in FORMS_EXTRA_WIDGETS does not match a field"
        raise ImproperlyConfigured(err % field_id)
    WIDGETS[field_id] = import_attr(widget_path)


from base_sort import BaseSort

class MergeSort(BaseSort):

	@staticmethod
	def merge(arr, left, mid, right):
		new_arr = []

		# fill temporary left and right arrays
		left_arr = []
		for i in range(0, mid-left+1):
			left_arr.append(arr[left+i])

		right_arr = []
		for i in range(0, right-mid):
			right_arr.append(arr[mid+1+i])

		i = 0
		j = 0
		curr_pos = left
		while i<len(left_arr) and j<len(right_arr):
			# use the smallest element to fill the general array until left or right arrays have no elements remaining
			if left_arr[i]<=right_arr[j]:
				arr[curr_pos] = left_arr[i]
				i+=1
			else:
				arr[curr_pos] = right_arr[j]
				j+=1
			curr_pos+=1

		# insert remaining elements from left array
		while i<len(left_arr):
			arr[curr_pos] = left_arr[i]
			i+=1
			curr_pos+=1

		# insert remaining elements from right array
		while j<len(right_arr):
			arr[curr_pos] = right_arr[j]
			j+=1
			curr_pos+=1

	def sort(self, arr):
		MergeSort.sort_range(arr, 0, len(arr)-1)

	@staticmethod
	def sort_range(arr, start, end):
		# recursively sort until only one element is remaining
		if start<end:
			mid = (start+end)//2
			MergeSort.sort_range(arr, start, mid)
			MergeSort.sort_range(arr, mid+1, end)
			MergeSort.merge(arr, start, mid, end)
		return arr


import numpy as np

from sklearn.linear_model import LinearRegression

class BiasRegressor(object):

    def __init__(self, model=LinearRegression()):
        self.model = model
        self.m_vals = []
        self.k_vals = []
        self.ftr_means: list = []
        self.over_under_lst = []
        self.p = 0
        self.over_under = 0

    @classmethod
    def get_params(self):
        return self.model

    def fit(self, X: np.ndarray, y: np.ndarray, split_size: float = 0.5, verbose: bool = False):
        '''
        This function does an initial fit with calibration along the way.
        '''

        p = 0

        if X.shape[0] != y.shape[0]:
            raise Exception("X and y must have the same shape.")
        split_idx = int(round(float(len(X) * split_size)))
        ftr_lsts = [[] for i in range(X.shape[1])]
        for ftr in X:
            for idx, val in enumerate(ftr):
                ftr_lsts[idx].append(val)
        for lst in ftr_lsts:
            self.ftr_means = np.append(self.ftr_means, (sum(lst)/len(lst)))
        self.ftr_means = np.array(self.ftr_means).reshape(-1, len(X[0]))

        X_train = X[:split_idx]
        X_val = X[split_idx:]

        y_train = y[:split_idx]
        y_val = y[split_idx:]

        self.model.fit(X_train, y_train)
        try:
            self.k_vals = np.zeros(y.shape[1]).tolist()
        except IndexError:
            self.k_vals = np.zeros(1).tolist()

        self.m_vals = self.model.predict(self.ftr_means)
        self.m_vals = self.m_vals[0]

        for idx, ftr in enumerate(X_val):
            ftr = ftr.reshape(1, -1)
            y_preds = self.model.predict(ftr)
            if sum(self.over_under_lst) > 0:
                y_preds = np.array([pred + p for pred in y_preds])
            elif sum(self.over_under_lst) < 0:
                y_preds = np.array([pred - p for pred in y_preds])
        p = self._calibrate(y_preds[0], y_val[idx])
        return p, self.model, sum(self.over_under_lst)

    def _calibrate(self, y_pred, y_val):
        if type(y_val) != np.ndarray:
            y_val = np.array([y_val])
        for i in range(len(y_val)):
            error = float(y_val[i] - y_pred[i])
            if error > 0.05:
                self.over_under_lst.append(1)
            elif error < -0.05:
                self.over_under_lst.append(-1)
            else:
                self.over_under_lst.append(0)
            m_val = float(self.m_vals[i])
            k = np.abs(error) / (m_val + 1)
            self.k_vals.append(k)
        return float(sum(self.k_vals) / len(self.k_vals))

    def predict(self, X: np.ndarray):
        y_preds = np.array([self.model.predict(X)])
        if self.over_under > 0:
            y_preds = np.array([pred + self.p for pred in y_preds])
        elif self.over_under < 0:
            y_preds = np.array([pred - self.p for pred in y_preds])
        return y_preds


# coding:utf-8

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

if __name__ == "__main__":
    MMSIList = [413376760]
    data = pd.read_csv("/home/qiu/Documents/data/DP-DATA/中国沿海AIS/"
                       "201609_chinaCoast.csv")
    data = data.loc[:, ['unique_ID', 'acquisition_time', 'longitude', 'latitude']]
    data = data[data["unique_ID"].isin(MMSIList)]
    data = data[(data["latitude"] < 36.5 * 1000000.) & (data["acquisition_time"] <= 1472691018)]
    data = data.sort_values(by=["unique_ID", "acquisition_time"])
    # data.columns = ["unique_ID", "acquisition_time", "longitude", "latitude"]
    data["longitude"] = data["longitude"] / 1000000.0
    data["latitude"] = data["latitude"] / 1000000.0
    print(len(data))

    X1 = np.array(data["longitude"])
    Y1 = np.array(data["latitude"])
    Z1 = np.array(data["acquisition_time"])
    ###########################################################################
    dataCompressDF = pd.read_csv("/home/qiu/Documents/data/DP-DATA/QDPOPT/"
                                 "413376760_qdpopt_50m_area.csv")
    dataCompressDF = dataCompressDF.sort_values(by=["unique_ID", "acquisition_time"])
    X2 = np.array(dataCompressDF["longitude"])
    Y2 = np.array(dataCompressDF["latitude"])
    Z2 = np.array(dataCompressDF["acquisition_time"])
    print(len(dataCompressDF))

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot(X1, Y1, Z1, "b-o")
    ax.plot(X2, Y2, Z2, "r-x")
    ax.legend()
    plt.show()


import requests
request = requests.get( 'http://adventofcode.com/day/7/input',
                   cookies = {'session':'53616c7465645f5f1f67ff685d72b6b7cc9c32e892bf4f09b44422b5ed91dff733905aaf35fa7d6895554ad0f8bfd069'}
                   )
instr = request.content.decode()

wires = {}
connections = instr.split('\n')
for line in connections:
    try:
        operation, target = line.split(' -> ')
        wires[target.strip()] = operation.strip()
    except:
        pass

wiresSave = wires.copy()

def calculate( expr ):
    try:
        return int(expr)
    except:
        parts = expr.split(' ')
        if len(parts) == 1: #один ідентифікатор
            if type(wires[parts[0]]) == int:
                return wires[parts[0]]
            else:
                wires[parts[0]] = calculate(wires[parts[0]])
                return wires[parts[0]]
        elif len(parts) == 2: #унарний оператор один - NOT
            if parts[0] != 'NOT':
                raise 'ERROR!' + parts[0]
            return ~calculate(parts[1])
        else: #бінарний оператор
            op1 = calculate(parts[0])
            op2 = calculate(parts[2])
            if   parts[1] == 'AND':
                return op1 & op2
            elif parts[1] == 'OR':
                return op1 | op2
            elif parts[1] == 'LSHIFT':
                return op1 << op2
            elif parts[1] == 'RSHIFT':
                return op1 >> op2
            else:
                raise 'ERROR ' + parts[1]

newB = calculate('a')

wires = wiresSave.copy()
wires['b'] = newB
print(calculate('a'))


from sqlalchemy.orm import scoped_session
from app import create_app
import unittest
import mock
from background import calc_stats_async
from database import db, StatsTab
from tests.common_tests_utility import mocked_get_ok, user1_json

class TestStatsDB(unittest.TestCase):

    def test_initDB(self):
        _app = create_app(debug=True)
        with _app.app_context():
            db.drop_all()
            db.create_all()

            session: scoped_session = db.session

            stats_record1: StatsTab = StatsTab()

            stats_record1.user_id = 30
            stats_record1.email = 'email@email.com'
            stats_record1.firstname = 'Mario'
            stats_record1.lastname = 'Rossi'

            session.add(stats_record1)
            session.commit()

            stats_check: StatsTab = session.query(StatsTab).filter(StatsTab.user_id == 30).first()
            self.assertIsNotNone(stats_check)
            self.assertEqual(stats_record1.user_id, stats_check.user_id)
            self.assertEqual(stats_record1.email, stats_check.email)

            stats_check.email = 'modified.email@email.ru'
            session.commit()

            stats_check_mod: StatsTab = session.query(StatsTab).filter(StatsTab.user_id == 30).first()
            self.assertIsNotNone(stats_check_mod)
            self.assertNotEqual('email@email.com', stats_check_mod.email)

            stats = stats_check_mod.to_json()
            print(stats)

    @mock.patch('requests.get', side_effect=mocked_get_ok)
    def test_Update(self, pippo):
        _app = create_app(debug=True)
        with _app.app_context():
            db.drop_all()
            db.create_all()

            calc_stats_async(user1_json['id'])

            session = db.session

            stats_check: StatsTab = session.query(StatsTab).filter(StatsTab.user_id == user1_json['id']).first()
            self.assertIsNotNone(stats_check)
            self.assertEqual(user1_json['id'], stats_check.user_id)
            self.assertEqual(user1_json['email'], stats_check.email)

    # @mock.patch('requests.get', side_effect=mocked_get_ok)
    # def test_Update_Async(self, pippo):
    #     _app = create_app(debug=True)
    #     with _app.app_context():
    #         db.drop_all()
    #         db.create_all()
    #
    #         # task = calc_stats_async
    #         # delay: AsyncResult = task.delay(user_id=user1_json['id'])
    #         # get = delay.get(timeout=10)
    #
    #         calc_stats_async.delay(user_id=user1_json['id']).get(timeout=5)
    #
    #         # sleep(10.0)
    #
    #         session = db.session
    #         stats_check: StatsTab = session.query(StatsTab).filter(StatsTab.user_id == user1_json['id']).first()
    #         self.assertIsNotNone(stats_check)
    #         self.assertEqual(user1_json['id'], stats_check.user_id)
    #         self.assertEqual(user1_json['email'], stats_check.email)


import jellyfish

def readFile(file):
	with open(file, 'r') as f:
		file = f.readlines()
		f.close()
		return file

def normalization(listfile):
	for i in range(len(listfile)):
		listfile[i] = listfile[i].strip()
	return listfile

misspell = readFile('wiki_misspell.txt')
correct = readFile('wiki_correct.txt')
dictionary = readFile('dict.txt')

misspell = normalization(misspell)
correct = normalization(correct)
dictionary = normalization(dictionary)

f = open('result.txt', 'w')

correctnumber = 0
total = 0

i = 0
while(i < len(misspell)):
	mis = misspell[i]
	soundex_mis = jellyfish.soundex(mis)
	resultlist = []
	for word in dictionary:
		distance = jellyfish.levenshtein_distance(mis, word)
		resultlist.append(distance)

	minimum = min(resultlist)
	j = 0
	result = [mis]
	while(j < len(resultlist)):
		if resultlist[j] == minimum:
			soundex_result = jellyfish.soundex(dictionary[j])
			if soundex_mis == soundex_result:
				result.append(dictionary[j])
				total += 1

		j += 1
	if correct[i] in result:
		result.append(True)
		correctnumber += 1
	else:
		result.append(False)

	result.append(correct[i])
	print(result)
	f.write(str(result) + '\n')
	i += 1

f.write(str(correctnumber) + ' ')
f.write(str(total))
f.close()

研一下/COMP90049 Knowledge Technologies/Assignment 1/data/levenshtein_soundex.py


# emacs: -*- mode: python-mode; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil; coding: utf-8 -*-
# ex: set sts=4 ts=4 sw=4 noet:
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
#   See COPYING file distributed along with the datalad package for the
#   copyright and license terms.
#
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
"""Test all extractors at a basic level"""

from six import (
    text_type,
)
from pkg_resources import iter_entry_points
from datalad.api import (
    Dataset,
    meta_extract,
    save,
    install,
)
from datalad.support.gitrepo import GitRepo

from nose import SkipTest
from datalad.tests.utils import (
    assert_repo_status,
    assert_true,
    assert_not_in,
    assert_in,
    assert_raises,
    assert_result_count,
    eq_,
    known_failure,
    with_tree,
    with_tempfile,
)
from ...tests import (
    make_ds_hierarchy_with_metadata,
    _get_dsid_from_core_metadata,
    _get_dsmeta_from_core_metadata,
)

@with_tree(tree={'file.dat': ''})
def check_api(annex, path):
    ds = Dataset(path).create(force=True, annex=annex)
    ds.save()
    assert_repo_status(ds.path)

    processed_extractors, skipped_extractors = [], []
    for extractor_ep in iter_entry_points('datalad.metadata.extractors'):
        # we need to be able to query for metadata, even if there is none
        # from any extractor
        try:
            res = meta_extract(
                dataset=ds,
                sources=[extractor_ep.name],
            )
        except Exception as exc:
            exc_ = str(exc)
            skipped_extractors += [exc_]
            continue
        # metalad_core does provide some information about our precious file
        if extractor_ep.name == 'metalad_core':
            assert_result_count(
                res,
                1,
                path=ds.path,
                type='dataset',
                status='ok',
            )
            assert_true(
                all('metalad_core' in r.get('metadata', {}) for r in res))
            # every single report comes with an identifier
            assert_true(all(
                (_get_dsid_from_core_metadata(r['metadata']['metalad_core'])
                    if r.get('type', None) == 'dataset'
                    else r['metadata']['metalad_core'].get('@id', None)) \
                is not None
                for r in res))
        processed_extractors.append(extractor_ep.name)
    assert "metalad_core" in processed_extractors, \
        "Should have managed to find at least the core extractor extractor"
    if skipped_extractors:
        raise SkipTest(
            "Not fully tested/succeded since some extractors failed"
            " to load:\n%s" % ("\n".join(skipped_extractors)))

@known_failure
def test_api_git():
    # should tolerate both pure git and annex repos
    yield check_api, False

@known_failure
def test_api_annex():
    yield check_api, True

@known_failure
@with_tempfile(mkdir=True)
def test_plainest(path):
    # blow on nothing
    assert_raises(
        ValueError,
        meta_extract, dataset=path, sources=['metalad_core'])
    r = GitRepo(path, create=True)
    # proper error, no crash, when there is the thinnest of all traces
    # of a dataset: but nothing to describe
    assert_result_count(
        meta_extract(
            dataset=r.path,
            on_failure='ignore',
        ),
        1,
        status='error',
        # message contains exception
        type='dataset',
        path=r.path,
    )
    # not we add some dummy content that does not count as metadata-relevant
    # and we still fail
    (r.pathobj / '.datalad').mkdir()
    (r.pathobj / '.datalad' / 'dummy').write_text(text_type('stamp'))
    ds = Dataset(r.path)
    ds.save()
    assert_result_count(
        meta_extract(
            dataset=ds.path,
            sources=['metalad_core'],
            on_failure='ignore',
        ),
        1,
        status='error',
        # message contains exception
        type='dataset',
        path=ds.path,
    )

@known_failure
@with_tempfile
@with_tempfile
def test_report(path, orig):
    origds, subds = make_ds_hierarchy_with_metadata(orig)
    # now clone to a new place to ensure no content is present
    ds = install(source=origds.path, path=path)
    # only dataset-global metadata
    res = meta_extract(dataset=ds, process_type='dataset')
    assert_result_count(res, 1)
    core_dsmeta = _get_dsmeta_from_core_metadata(
        res[0]['metadata']['metalad_core']
    )
    assert_in(
            {'@type': 'Dataset',
            '@id': 'datalad:{}'.format(subds.repo.get_hexsha()),
            'identifier': 'datalad:{}'.format(subds.id), 'name': 'sub'},
        core_dsmeta['hasPart']
    )
    # has not seen the content
    assert_not_in(
        'contentbytesize',
        core_dsmeta
    )
    res = meta_extract(dataset=ds, process_type='content')
    assert(any(
        dict(tag=['one', 'two']) == r['metadata'].get('metalad_annex', None)
        for r in res
    ))
    # we have a report on file(s)
    assert(len(res) > 0)
    # but no subdataset reports
    assert_result_count(res, 0, type='dataset')
    content_size = sum(
        (_get_dsmeta_from_core_metadata(
            r['metadata']['metalad_core'])
         if r['type'] == 'dataset'
         else r['metadata']['metalad_core'])['contentbytesize'] for r in res)
    # and now all together
    res = meta_extract(dataset=ds, process_type='all')
    # got a content size report that sums up all individual sizes
    eq_(
        (_get_dsmeta_from_core_metadata(
            res[0]['metadata']['metalad_core'])
         if res[0]['type'] == 'dataset'
         else res[0]['metadata']['metalad_core'])['contentbytesize'],
        content_size
    )


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('blog', '0004_comment'),
    ]

    operations = [
        migrations.AlterField(
            model_name='comment',
            name='id',
            field=models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True),
        ),
        migrations.AlterField(
            model_name='note',
            name='id',
            field=models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True),
        ),
    ]


print(_doc_)

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import styles

from sklearn.datasets import fetch_olivetti_faces
from sklearn.utils.validation import check_random_state

from sklearn.ensemble import ExtraTreesRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import RidgeCV

#Load the faces dataset
data = fetch_olivetti_faces()
targets = data.target

data = data.images.reshape((len(data.images), -1))
train = data[targets < 30]
test = data[targets > 30] # Test on independent people

#Test on a subset of people
n_faces = 5
rng = check_random_state(4)
face_ids = rng.randint(test.shape[0], size=(nfaces, )) #?
test = test[faceids, :]

n_pixels = data.shape[1]
#Upper half of the faces
X_train = train[:, :np.ceil(0.5 * n_pixels)]
#Upper half of faces
y_train = train[:, np.floor(0.5 * n_pixels):]

#Testing sets for both labels and features
X_test = test[:, :np.ceil(0.5 * n_pixels)]
y_test = test[:, np.floor(0.5 * n_pixels):]

ESTIMATORS = {
	"Extra trees": ExtraTreesRegressor(n_estimators=10, max_features=32, random_state=0),
	"K-nn": KNeighborsRegressor(),
	"Linear regression": LinearRegression(),
	"Ridge": RidgeCV(),
}

y_test_predict = dict()
for name, estimator in ESTIMATORS.items():
	estimator.fit(X_train, y_train)
	y_test_predict[name] = estimator.predict(X_test)

#Plot the completed faces
image_shape = (64, 64)

n_cols = 1 + len(ESTIMATORS)
plt.figure(figsize=(2. * n_cols, 2.26 * n_faces))
plt.suptitle("Face completion with multi-output estimators", size=16)

for i in range(n_faces):
	true_face = np.haystack((X_test[i], y_test[i]))


#Numpy
import numpy as np

##Rank 1 array
arr1=[3,4,5,6,7]
print(type(arr1))

narr1=np.array(arr1)

# multidimensional array
arr2=[[1,2,3,4],[3,4,5,6]]
narr2=np.array(arr2)
print(narr2)

arr3=[[1,2,3,4],[3,4,5,6],[7,8,9,6]]
narr3=np.array(arr3)
print(narr3)

print(narr3.shape)

#2*2 array with 0s
zer=np.zeros((2,2))
print(zer)

#2*2 array with 5
full=np.full((2,2),5)
print(full)

ex3 = np.eye(4,4) #diagonal 1
print(ex3)

#concatenate two numpy arrays
x=np.array([2,6,8,4])
y=np.array([11,8,2])
z=np.concatenate([x,y])
print(z)

arr=[[1,2,3,4],[3,4,5,6],[7,8,9,6],[12,7,10,9],[2,11,8,10]]
narr=np.array(arr) #convert array into numpy array
print (narr)


import textdistance
import numpy as np

orig_order_ids = ['5269129170', '4269129163', '3269129163', '6269129163', '7269129163', '8269129155','12444456']

orig_shelves_list = [['R1', 'G1', 'C1', 'D1', 'D2'],
                ['A1', 'C3', 'B1'],
                ['R1', 'S1', 'BG1', 'FA1'],
                ['C3', 'R1', 'M1', 'M2'],
                ['P1', 'P3', 'P4', 'G6', 'G7'],
                ['P3', 'P4', 'G6', 'G7'],
                ['P1','P3''G7']]

aisles_unsorted =  ['RGCD', 'ACB', 'RSBA', 'CRM', 'PG', 'PG','PG']     #list will be generated by part1 code

print("******Original******")
print(aisles_unsorted)
print(orig_order_ids)
print(orig_shelves_list)

aisles = sorted(aisles_unsorted, key = aisles_unsorted.count,
                                reverse = True)
length = len(aisles)

indexes = []
indexes_done = []
for i in range(0,length):
	for j in range(0,length):
		if aisles[i] == aisles_unsorted[j] and j not in indexes_done:
			indexes.append(j)
			indexes_done.append(j)
print("******indexes****",indexes)
order_ids = []
shelves_list = []
leven_matrix = np.zeros((length,length),dtype=int)                            #initialise an empty matrix

for ind in indexes:
	order_ids.append(orig_order_ids[ind])
	shelves_list.append(orig_shelves_list[ind])
print("****sorted***")
print(aisles)
print(order_ids)
print(shelves_list)
def invalidate(start,id,col):               #once aisles of user are processed, mark them as 999 so that they won't be
    if col:                                 #considered for further batches
        for i in range(start,length):
            leven_matrix[id][i] = 999
        for j in range(0,length):
            leven_matrix[j][id] = 999
    else:                                   #we don't want to overwrite the current index column.
        for i in range(start,length):
            leven_matrix[id][i] = 999

for i in range(0,length):                   #mark the diagnoals 999
    for j in range(0,length):
        if i == j:
            leven_matrix[i][j] = 999

n = 3
done = []                                   #list of processed elements
batches = []                                #list of aisles in group of 3
order_batches = []                          #list of orders in group of 3
shelves_batches = []                        #list of shelves corresponding to 3 order_ids
for i in range(0,length):
    if i not in done and length-len(done)>=n:
        current = aisles[i]
        col = []
        top_n = []
        order_n = []
        shelves_n = []

        for j in range(0,length):
            if i == j:
                col.append(leven_matrix[i][j])
            else:
                if j not in done:
                    dst = textdistance.levenshtein(current,aisles[j])
                    leven_matrix[j][i] = dst
                    col.append(dst)
                else:
                    col.append(leven_matrix[j][i])      #999

        sorted_list = sorted(range(len(col)), key=lambda k: col[k])    #sort the column values and get indexes of sorted values.

        top_indexes = sorted_list[:n-1]
        top_n.append(current)
        order_n.append(order_ids[i])
        shelves_n.append(shelves_list[i])

        for id in top_indexes:
        	done.append(id)                    #mark it as "processed"
        	invalidate(i+1,id,True)
        	top_n.append(aisles[id])
        	order_n.append(order_ids[id])
        	shelves_n.append(shelves_list[id])

        if i not in done: done.append(i)
        invalidate(i+1,i,False)

        batches.append(top_n)
        order_batches.append(order_n)
        shelves_batches.append(shelves_n)

left_aisles = []
left_orders = []
left_shelves = []
print("\n Final matrix values: \n")

print(leven_matrix)            #print final matrix

for i in range(0,length):
    '''
    as batches of three, if the total number of orders received are not a multiple of three,
    orders will be left, add it to "left_aisles" and left_orders
    '''
    if i not in done:
        left_aisles.append(aisles[i])
        left_orders.append(order_ids[i])
        left_shelves.append(shelves_list[i])

if len(left_aisles) != 0:
    batches.append(left_aisles)
    order_batches.append(left_orders)
    shelves_batches.append(left_shelves)

print("\nAisles grouped together:\n",batches)                 #final batches
print("\nOrder ids grouped together:\n",order_batches)           #orders batches
print("\nShelves grouped together:\n",shelves_batches)         #shelves batches

'''
Output:

(base) F:\WalKet-Basket\Part2>python lavenstein.py

 Final matrix values:

[[999. 999. 999. 999. 999. 999.]
 [  3. 999. 999. 999. 999. 999.]
 [  3. 999. 999. 999. 999. 999.]
 [  4. 999. 999. 999. 999. 999.]
 [  3. 999. 999.   3. 999. 999.]
 [  3. 999. 999.   3. 999. 999.]]

Aisles grouped together:
 [['RGCD', 'ACB', 'RSBA'], ['CRM', 'PG', 'PG']]

Order ids grouped together:
 [['5269129170', '4269129163', '3269129163'], ['6269129163', '7269129163', '8269129155']]

Shelves grouped together:
 [[['R1', 'G1', 'C1', 'D1', 'D2'], ['A1', 'C3', 'B1'], ['R1', 'S1', 'BG1', 'FA1']], [['C3', 'R1', 'M1', 'M2'], ['P1', 'P3', 'P4', 'G6', 'G7'], ['P3', 'P4', 'G6', 'G7']]]
'''


import pytest
from selenium import webdriver

def pytest_addoption(parser):
    parser.addoption("--browser",action="store",default="chrome",help="Type in browser name eg.chrome OR firefox")

@pytest.fixture(scope="class")
def test_setup(request):
        browser=request.config.getoption("--browser")
        if browser == 'chrome':
            driver=webdriver.Chrome(executable_path="../drivers/chromedriver.exe")
        #driver=webdriver.Chrome(executable_path="../drivers/chromedriver.exe")
        elif browser == 'firefox':
            driver=webdriver.Firefox(executable_path="../drivers/geckodriver.exe")

        driver.maximize_window()
        request.cls.driver=driver
        driver.implicitly_wait(5)
        yield
        driver.close()
        driver.quit()


import pandas as pd
import random

aircode_1 = pd.read_csv('data/L_AIRPORT.csv_')
aircode_2 = pd.read_csv('data/L_AIRPORT_ID.csv_')

aircode_1 = aircode_1.reset_index()
aircode_2 = aircode_2.reset_index()
aircodes = pd.merge(aircode_1, aircode_2, on='Description')

# Create a dictionary for quick lookup of airport code equivalents
aircode_dict = dict(zip(aircodes['Code_y'].astype(str), aircodes['Code_x']))

# Create a dictionary of all 3 letter airports
airports_3letter = dict(zip(aircode_1['Code'], aircode_1['Description']))

# Load data
n = 5819078
skip = random.sample(range(1,n),n-2000000)
flights = pd.read_csv('data/flights.csv',
                      skiprows=skip,
                      usecols = ['MONTH','DAY','DAY_OF_WEEK','AIRLINE',
                                 'ORIGIN_AIRPORT','DESTINATION_AIRPORT',
                                 'SCHEDULED_DEPARTURE','DEPARTURE_DELAY',
                                 'ARRIVAL_DELAY','CANCELLED'],
                      low_memory=False)
flights['ORIGIN_AIRPORT'] = flights['ORIGIN_AIRPORT'].values.astype(str)
flights['DESTINATION_AIRPORT'] = flights['DESTINATION_AIRPORT'].values.astype(str)

N_flights = len(flights)
for i in range(N_flights):
    if i % 100000 == 0:
        print(i)
    if len(flights['ORIGIN_AIRPORT'][i]) != 3:
        to_replace = flights['ORIGIN_AIRPORT'][i]
        value = aircode_dict[flights['ORIGIN_AIRPORT'][i]]
        flights = flights.replace(to_replace, value)
        print('replaced', to_replace, 'with', value)
    elif len(flights['DESTINATION_AIRPORT'][i]) != 3:
        to_replace = flights['DESTINATION_AIRPORT'][i]
        value = aircode_dict[flights['DESTINATION_AIRPORT'][i]]
        flights = flights.replace(to_replace, value)
        print('replaced', to_replace, 'with', value)

# Take in user input for the airport of choice
while True:
    airport = input('Enter a 3-letter airport code: ')
    if len(airport) != 3:
        print('Sorry, your airport code was not 3 letters')
        continue
    elif not airport in airports_3letter:
        print('Sorry, your airport was not found')
        continue
    else:
        # Need to ensure that all airport code are in the same format
        # Initially October airports were using the 5-digit scheme and all other months
        # were using a 3-digit scheme so we need to correct October
        airport = airport.upper()
        flights = flights[flights['ORIGIN_AIRPORT'] == airport]

        # Filter dataset to just contain the flights starting at the point of
        # interest
        break

#def airport_insights(airport_data):

#        airport_insights(airport_specific_flights)


#!/usr/bin/env python

from MitPanda.Tools.MultiThreading import MacroRunner

if __name__ == "__main__":
  mr = MacroRunner('applyBDT.C',None,["libMitPhysicsUtils.so"])
  argList = [['/local/snarayan/monotop25ns_v2/'+x+'.root'] for x in
                ['Diboson',
                 'TTbar',
                 'QCD',
                 'WJets',
                 'ZtoNuNu',
                 'ZJets',
                 'Signal1p1',
                 'Signal2p1',
                 'SingleTop',
                ]
            ]
  mr.setArgList(argList)
  mr.run(5)


import sys
import xml.dom.minidom
import os
import random
import datetime

filter_categories = ['800-8400','800-8300','800-8200','800-8000','700-7851','700-7850','700-7600','700-7450','700-7300','700-7000','600-6950','600-6900','600-6800','600-6700','600-6600','600-6500','600-6400','600-6300','600-6200','600-6100','600-6000','550-5520','500-5100','500-5000','300-3200','300-3100','300-3000','200-2300','200-2200','200-2100','200-2000','100-1100','100-1000']

country_sample_numbers = [
    # EEU
    {'country':'TUR', 'counts': [13,13,33,33,33]},
    {'country':'POL', 'counts': [5,5,10,10,10]},
    {'country':'RUS', 'counts': [3,3,8,8,8]},
    {'country':'ROU', 'counts': [2,2,7,7,7]},
    {'country':'GRC', 'counts': [2,2,5,5,6]},
    {'country':'CZE', 'counts': [1,2,4,4,4]},
    {'country':'HUN', 'counts': [1,2,4,4,4]},
    {'country':'HRV', 'counts': [1,1,2,3,3]},
    {'country':'BGR', 'counts': [1,1,2,3,3]},
    {'country':'SVK', 'counts': [1,1,1,1,1]},
    {'country':'KAZ', 'counts': [1,1,1,1,1]},
    # WEU
    {'country':'NLD', 'counts': [2,2,7,7,7]},
    {'country':'SWE', 'counts': [2,2,7,7,7]},
    {'country':'BEL', 'counts': [1,1,4,4,5]},
    {'country':'PRT', 'counts': [1,1,4,4,5]},
    {'country':'AUT', 'counts': [1,1,2,3,3]},
    {'country':'DNK', 'counts': [1,1,2,3,3]},
    {'country':'NOR', 'counts': [1,1,2,3,3]},
    {'country':'FIN', 'counts': [1,1,2,3,3]},
    {'country':'CHE', 'counts': [1,1,2,3,3]},
    {'country':'IRL', 'counts': [1,1,1,1,1]},
    {'country':'LUX', 'counts': [1,1,1,1,1]},
    {'country':'MLT', 'counts': [1,1,1,1,1]},
    {'country':'ISL', 'counts': [1,1,1,1,1]},
    # APAC
    {'country':'THA', 'counts': [4,10,10,12,12]},
    {'country':'IDN', 'counts': [4,4,9,9,9]},
    {'country':'VNM', 'counts': [2,2,2,7,7]},
    {'country':'MYS', 'counts': [1,2,4,4,4]},
    {'country':'PHL', 'counts': [1,1,2,3,3]},
    {'country':'TWN', 'counts': [1,1,2,3,3]},
    {'country':'SGP', 'counts': [1,1,2,3,3]},
    {'country':'HKG', 'counts': [1,1,1,1,1]},
    {'country':'IND', 'counts': [15,15,35,35,35]},
    {'country':'BGD', 'counts': [1,1,1,1,1]},
    {'country':'NPL', 'counts': [1,1,1,1,1]},
    {'country':'LKA', 'counts': [1,1,1,1,1]},
    # NA
    {'country':'CAN', 'counts': [12,12,32,32,32]},
    {'country':'BHS', 'counts': [2,2,2,2,2]},
    {'country':'CYM', 'counts': [1,1,1,1,1]},
    {'country':'JAM', 'counts': [2,2,2,2,2]},
    {'country':'VGB', 'counts': [1,1,1,1,1]},
    # LAM
    {'country':'BRA', 'counts': [7,8,20,20,20]},

    {'country':'MEX', 'counts': [3,3,8,8,8]},
    {'country':'ARG', 'counts': [2,2,2,2,2]},
    {'country':'COL', 'counts': [1,1,1,1,1]},
    {'country':'CHL', 'counts': [1,1,1,1,1]},
    {'country':'PER', 'counts': [1,1,1,1,1]},
    {'country':'ECU', 'counts': [1,1,1,1,1]},
    {'country':'PRY', 'counts': [1,1,1,1,1]},
    {'country':'PAN', 'counts': [1,1,1,1,1]},
    {'country':'VEN', 'counts': [1,1,1,1,1]},
    # BIG 7
    {'country':'AUS', 'counts': [15,15,40,40,40]},
    {'country':'DEU', 'counts': [15,15,40,40,40]},
    {'country':'ESP', 'counts': [15,15,40,40,40]},
    {'country':'GBR', 'counts': [15,15,20,20,20]},
    {'country':'ITA', 'counts': [15,15,40,40,40]},
    {'country':'FRA', 'counts': [15,15,40,40,40]},
    {'country':'USA', 'counts': [15,15,40,40,40]},
]

def main(country, region, kick_off_date):
    randomed_list = read_samples(country)
    csv_list = create_csv_list(country, region, randomed_list)
    save_csv_list(csv_list, country, region, kick_off_date)

def read_samples(country):
    randomed_list = []
    dir_path = os.path.dirname(os.path.realpath(__file__))
    fw = open (os.path.join(dir_path, "xml", country+"_tmp.csv"), 'rb')
    for line in fw:
        randomed_list.append(line)
    fw.close()
    os.remove(os.path.join(dir_path, "xml", country+"_tmp.csv"))
    return randomed_list

def create_csv_list(country, region, randomed_list):
    print("sampling: "+country)
    csv_list = []
    for i in range(0,3):
        random.shuffle(randomed_list)
        csv_list = []
        givenCountry = [x for x in country_sample_numbers if x['country']==country][0]
        current_counts = {
            'r1': givenCountry['counts'][0],
            'r2': givenCountry['counts'][1],
            'r3': givenCountry['counts'][2],
            'r4': givenCountry['counts'][3],
            'r5': givenCountry['counts'][4]
        }
        if region == "EEU":
            csv_list.append(
                "place_id,name,phone,house_number,street_basename,street_type,city,state,country,postal,nt_cat_id,nt_cat_name,pd_cat_id,pd_cat_name,lat,lon,isplace,isopen,isname,isaddr,isphone,reality_score,full_house_number,district"
            )
        else:
            csv_list.append(
                "place_id,name,phone,house_number,street_basename,street_type,city,state,country,postal,nt_cat_id,nt_cat_name,pd_cat_id,pd_cat_name,lat,lon,isplace,isopen,isname,isaddr,isphone,reality_score"
            )
        for item in randomed_list:
            doc = xml.dom.minidom.parseString(item)
            placeid = ""
            name = ""
            phone = ""
            house_number = ""
            street_basename = ""
            street_type = ""
            city = ""
            state = ""
            postal = ""
            lcms_category_id = ""
            lcms_category_name = ""
            poi_category_id = ""
            poi_category_name = ""
            lat = ""
            lon = ""
            isplace = "-0.1"
            isopen = "-0.1"
            isname = "-0.1"
            isaddr = "-0.1"
            isphone = "-0.1"
            reality_score = ""
            full_house_number = "NULL"
            district = ""

            # category lcms ---------------------------------------------
            categories = doc.getElementsByTagName('Category')
            for category in categories:
                if category.hasAttribute('primaryFlag') and category.getAttribute('primaryFlag') == "true" and category.getAttribute('categorySystem') == "navteq-lcms":
                    categoryIdNode = category.getElementsByTagName('CategoryId')
                    if categoryIdNode:
                        lcms_category_id = categoryIdNode[0].firstChild.data
                    categoryNameNode = category.getElementsByTagName('Text')
                    if categoryNameNode:
                        lcms_category_name = categoryNameNode[0].firstChild.data
                    break

            # Qyality scoring ----------------------------------------
            attributes = doc.getElementsByTagName('Attribute')
            for attr in attributes:
                if attr.hasAttribute('key') and attr.getAttribute('key') == "isPlace":
                    isplace = attr.firstChild.data
                if attr.hasAttribute('key') and attr.getAttribute('key') == "isOpen":
                    isopen = attr.firstChild.data
                if attr.hasAttribute('key') and attr.getAttribute('key') == "isNameCorrect":
                    isname = attr.firstChild.data
                if attr.hasAttribute('key') and attr.getAttribute('key') == "isAddressCorrect":
                    isaddr = attr.firstChild.data
                if attr.hasAttribute('key') and attr.getAttribute('key') == "isPhoneCorrect":
                    isphone = attr.firstChild.data
                if attr.hasAttribute('key') and attr.getAttribute('key') == "reality_score":
                    reality_score = attr.firstChild.data

            if region != "LAM":
                if not lcms_category_id[:8] in filter_categories:
                    continue
            if check_counts(current_counts, float(reality_score)) == False:
                continue
            # place id --------------------------------------------------
            placeIdNode = doc.getElementsByTagName('PlaceId')
            if placeIdNode:
                placeid = placeIdNode[0].firstChild.data
            # name --------------------------------------------------------
            names = doc.getElementsByTagName('Name')
            for n in names:
                if n.hasAttribute('primaryFlag') and n.getAttribute('primaryFlag') == "true":
                    baseTextNodes = n.getElementsByTagName("BaseText")
                    for baseText in baseTextNodes:
                        name = baseTextNodes[0].firstChild.data
                        break
            # phone -------------------------------------------------------
            contacts = doc.getElementsByTagName("Contact")
            for contact in contacts:
                if contact.hasAttribute('type') and contact.getAttribute('type') == "PHONE":
                    phone = contact.getElementsByTagName('ContactString')[0].firstChild.data
                    break

            # house number ------------------------------------------------
            houseNumberNode = doc.getElementsByTagName('HouseNumber')
            if houseNumberNode:
                house_number = houseNumberNode[0].firstChild.data

            # street base name --------------------------------------------------
            streetNameNode = doc.getElementsByTagName('StreetName')
            if streetNameNode:
                street_basenameNode = streetNameNode[0].getElementsByTagName('BaseName')
                if street_basenameNode:
                    street_basename = street_basenameNode[0].firstChild.data
                street_typeNode = street_basenameNode[0].getElementsByTagName('StreetType')
                if street_typeNode:
                    street_type = street_typeNode[0].firstChild.data
            # city ----------------------------------------------------------------
            level4 = doc.getElementsByTagName('Level4')
            if level4:
                city = level4[0].firstChild.data
            # state ------------------------------------------------------------------
            level2 = doc.getElementsByTagName('Level2')
            if level2:
                state = level2[0].firstChild.data
            # postal -----------------------------------------------------------------
            postalNode = doc.getElementsByTagName('PostalCode')
            if postalNode:
                postal = postalNode[0].firstChild.data

            # category poi ---------------------------------------------
            categories = doc.getElementsByTagName('Category')
            for category in categories:
                if category.getAttribute('categorySystem') == "navteq-poi":
                    categoryIdNode = category.getElementsByTagName('CategoryId')
                    if categoryIdNode:
                        poi_category_id = categoryIdNode[0].firstChild.data
                    categoryNameNode = category.getElementsByTagName('Text')
                    if categoryNameNode:
                        poi_category_name = categoryNameNode[0].firstChild.data
                    break
            # coordinates ---------------------------------------------------
            geoPositoins = doc.getElementsByTagName('GeoPosition')
            for geoPosition in geoPositoins:
                if geoPosition.hasAttribute('type') and geoPosition.getAttribute('type') == "ROUTING":
                    latNode = geoPosition.getElementsByTagName('Latitude')
                    if latNode:
                        lat = latNode[0].firstChild.data
                    lonNode = geoPosition.getElementsByTagName('Longitude')
                    if lonNode:
                        lon = lonNode[0].firstChild.data
                    break
            # full house number ----------------------------------------------
            additionalDataNodes = doc.getElementsByTagName('AdditionalData')
            for additData in additionalDataNodes:
                if additData.hasAttribute('key') and additData.getAttribute('key') == "FullHouseNumber":
                    full_house_number = additData.firstChild.data
            if region == "EEU":
                csv_list.append(
                    placeid
                    +","+fix_quote(name)
                    +","+fix_quote(phone)
                    +","+fix_quote(house_number)
                    +","+fix_quote(street_basename)
                    +","+fix_quote(street_type)
                    +","+fix_quote(city)
                    +","+fix_quote(state)
                    +","+fix_quote(country)
                    +","+fix_quote(postal)
                    +","+fix_quote(country)
                    +","+fix_quote(poi_category_id)
                    +","+fix_quote(poi_category_name)
                    +","+fix_quote(lcms_category_id+"|"+lcms_category_name)
                    +","+lat
                    +","+lon
                    +","+isplace
                    +","+isopen
                    +","+isname
                    +","+isaddr
                    +","+isphone
                    +","+reality_score
                    +","+full_house_number
                    +","+district
                )
            else:
                csv_list.append(
                    placeid
                    +","+fix_quote(name)
                    +","+fix_quote(phone)
                    +","+fix_quote(house_number)
                    +","+fix_quote(street_basename)
                    +","+fix_quote(street_type)
                    +","+fix_quote(city)
                    +","+fix_quote(state)
                    +","+fix_quote(country)
                    +","+fix_quote(postal)
                    +","+fix_quote(country)
                    +","+fix_quote(poi_category_id)
                    +","+fix_quote(poi_category_name)
                    +","+fix_quote(lcms_category_id+"|"+lcms_category_name)
                    +","+lat
                    +","+lon
                    +","+isplace
                    +","+isopen
                    +","+isname
                    +","+isaddr
                    +","+isphone
                    +","+reality_score
                )

            if check_counts_finished(current_counts):
                break
        if check_counts_finished(current_counts):
            break

    return csv_list

def check_counts(current_counts, score):
    if score < 0.21:
        if current_counts['r1']>0:
            current_counts['r1'] = current_counts['r1'] - 1
            return True
        else:
            return False
    if score >= 0.21 and score < 0.28:
        if current_counts['r2']>0:
            current_counts['r2'] = current_counts['r2'] - 1
            return True
        else:
            return False
    if score >= 0.28 and score < 0.55:
        if current_counts['r3']>0:
            current_counts['r3'] = current_counts['r3'] - 1
            return True
        else:
            return False
    if score >= 0.55 and score < 0.8:
        if current_counts['r4']>0:
            current_counts['r4'] = current_counts['r4'] - 1
            return True
        else:
            return False
    if score >= 0.8:
        if current_counts['r5']>0:
            current_counts['r5'] = current_counts['r5'] - 1
            return True
        else:
            return False

def check_counts_finished(current_counts):
    c1 = current_counts['r1']
    c2 = current_counts['r2']
    c3 = current_counts['r3']
    c4 = current_counts['r4']
    c5 = current_counts['r5']
    if c1 == 0 and c2 == 0 and c3 == 0 and c4 == 0 and c5 == 0:
        return True
    return False

def save_csv_list(csv_list, country, region, kick_off_date):
    dir_path = os.path.dirname(os.path.realpath(__file__))
    file_name = os.path.join(dir_path, kick_off_date,region+"_"+country+"_"+kick_off_date+".csv")
    if region == "BIG7":
        file_name = os.path.join(dir_path, kick_off_date, country+"_"+kick_off_date+".csv")
    fo = open(file_name, 'wb')
    for line in csv_list:
        fo.write(line.encode('UTF-8')+"\r\n")
        # fo.write(line+"\t\n")

def fix_quote(input):
    if "," in input:
        return "\""+input+"\""
    return input

if __name__ == "__main__":
    main(sys.argv[1], sys.argv[2], sys.argv[3])


from bs4 import BeautifulSoup
from urllib.parse import quote
import urllib3
import pandas as pd

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

def twitter():
# trackanalytics 'Most Followed Twitter Profiles' url pagina
    url = 'https://www.trackalytics.com/the-most-followed-twitter-profiles/page/'

    user_list = []
    screen_name_list = []
    username_list = []
    # Itera por las paginas
    for num in range(1, 2):
        try:
            # Carga pagina
            query = url + str(num) + '/'
            response = urllib3.PoolManager().request('GET', query).data

            # Evita warnings de decoding
            FromRaw = lambda r: r if isinstance(r, str) else r.decode('utf-8', 'ignore')

            # Parsea html de la pag
            soup = BeautifulSoup(FromRaw(response), 'html.parser')

            # Extrae los elementos de la fila en la tabla de usuarios
            rows = soup.find_all('tr')[1:16]
            for row in rows:
                try:
                    # Parsea las filas para extraer el link con el usuario de twitter
                    new = BeautifulSoup(str(row), 'html.parser').find_all('a')[1]
                    new2 = BeautifulSoup(str(row), 'html.parser').find_all('td')[2]
                    screen_name = new.get('title')
                    username = "@" + new.get('href').split('/')[-2]  # coge usuario por link

                    followers = str(new2).split('\n')
                    followers = followers[1].strip()
                    followers = followers.split(' ')
                    followers = followers[0]

                    # Almacena usuarios en user_list
                    user_list.append({'Nombre': screen_name, 'Usuario': username, 'Seguidores': followers})

                except Exception as e:
                    print('link error:', num, e)
                    continue
        except Exception as e:
            print('page error:', num, e)
            continue

    df = pd.DataFrame(user_list)
    print(df)
    return df
#df.to_csv('MasSeguidosTwitter19.csv')


"""
This file contain the case 3
"""
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import lorenz

sigma = 10
beta = 8.0/3.0
rho = 28

parameters = sigma, rho, beta
initial = [1.0, -5.0, 13.0]
lorenz.run.run_lorenz(parameters, ini_state = initial, t_d = 0.001,
                      plot = True, save = True, fname = 'case_3',
                      directory = 'case 3')