import logging
import requests
import os
import zipfile
import datetime

import requests_unixsocket

from node_mgmt import EnvScanner

logger = logging.getLogger(__name__)

def send_log(node):
    """ Upload system logs to S3 """

    # Package logs in zipped archive
    zipped_logs = __create_log_archive(node)
    if not zipped_logs:
        logger.warn('No log archive available. Exiting log upload.')
        return

    # Obtain S3 location for file upload
    upload_url = node.api.get_upload_url()
    if not upload_url:
        logger.warn('No upload URL available. Exiting log upload.')
        return

    # Send logs
    try:
        fh = open(zipped_logs, 'rb')
    except Exception:
        logger.exception('Cannot open log archive. Exiting log upload.')
        return

    try:
        r = requests.put(upload_url, data=fh.read(), headers={'Content-Disposition': os.path.basename(zipped_logs)})

        if r.status_code == 200:
            logger.info('Upload successful')
            logger.debug(r.text)
        else:
            logger.warn('Upload not successful')
            logger.info(r.text)
    except Exception:
        logger.exception('Exception while uploading log archive')
    finally:
        fh.close()

    # Delete temporary file
    try:
        os.remove(zipped_logs)
    except Exception:
        logger.warn('Cannot delete local log archive', exc_info=True)

def __create_log_archive(node):
    """ Find the systemd logs and create a zipped archive of them """

    # List of directories where to look for logs.
    # The function will stop and try to get logs from the first one that exists
    LOG_DIRS_TO_CHECK = ['/run/log/journal/']
    output_path = None

    for log_dir in LOG_DIRS_TO_CHECK:
        if os.path.isdir(log_dir):
            filename = f"logs_{node.node_id}_{datetime.datetime.utcnow().strftime('%Y%m%dT%H%M%SZ')}.zip"
            output_path = os.path.join(os.environ['SNAP_DATA'], filename)

            logger.info('Zipping logs in %s into %s' % (log_dir, output_path))
            __zip_directory(log_dir, output_path)

            break

    return output_path

def __zip_directory(dir_path, output_path):
    """Zip the contents of an entire folder (with that folder included
    in the archive). Empty subfolders will be included in the archive
    as well.
    """
    parent_folder = os.path.dirname(dir_path)
    # Retrieve the paths of the folder contents.
    contents = os.walk(dir_path)
    try:
        zip_file = zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED)
        for root, folders, files in contents:
            # Include all subfolders, including empty ones.
            for folder_name in folders:
                absolute_path = os.path.join(root, folder_name)
                relative_path = absolute_path.replace(parent_folder + '\\', '')
                logger.debug('Adding %s to archive.' % absolute_path)
                zip_file.write(absolute_path, relative_path)
            for file_name in files:
                absolute_path = os.path.join(root, file_name)
                relative_path = absolute_path.replace(parent_folder + '\\', '')
                logger.debug('Adding %s to archive.' % absolute_path)
                zip_file.write(absolute_path, relative_path)
        logger.debug('%s created successfully.' % output_path)
    except IOError:
        logger.exception('I/O Error')
        return None
    except OSError:
        logger.exception('OS Error')
        return None
    except zipfile.BadZipfile:
        logger.exception('"Bad ZIP file" error')
        return None
    finally:
        zip_file.close()
        return True

def snap_refresh(node):
    __snapd_socket_post({'action': 'refresh'})

def snap_switch_stable(node):
    __snapd_socket_post({'action': 'refresh', 'channel': 'stable'})

def snap_switch_candidate(node):
    __snapd_socket_post({'action': 'refresh', 'channel': 'candidate'})

def snap_switch_beta(node):
    __snapd_socket_post({'action': 'refresh', 'channel': 'beta'})

def snap_switch_edge(node):
    __snapd_socket_post({'action': 'refresh', 'channel': 'edge'})

def __snapd_socket_post(payload):
    try:
        with requests_unixsocket.Session() as s:
            res = s.post('http+unix://%2Frun%2Fsnapd.socket/v2/snaps/ammp-edge', json=payload)
        logger.info(f"Response from snapd API: Status {res.status_code} / {res.text}")
    except Exception:
        logger.exception('Exception while doing snapd API socket request')

def env_scan(node):
    logger.info('Starting environment scan')
    scanner = EnvScanner()
    scan_result = scanner.do_scan()
    logger.info('Completed environment scan. Submitting results to API.')
    node.api.post_env_scan(scan_result)

def imt_sensor_address(node):
    # Note: unlike the other commands here, this one is normally triggered from the Web UI
    # Ideally this will be placed elsewhere in future, within a more systematic
    # action/command framework
    import serial
    from reader.modbusrtu_reader import Reader

    def readall(s):
        resp = b''
        while True:
            r = s.read()
            if len(r) > 0:
                resp += r
            else:
                break
        return resp

    ser = serial.Serial('/dev/ttyAMA0', baudrate=9600, timeout=5)
    result = {}

    logger.info("Changing address of IMT sensor from 1 to 2 (command 0x01460402630c")
    req = bytes.fromhex('01460402630c')
    ser.write(req)
    result['Address change response (expect 01460402630c'] = readall(ser).hex()

    logger.info("Restarting IMT sensor communications (command 0x010800010000b1cb")
    req = bytes.fromhex('010800010000b1cb')
    ser.write(req)
    result['Comms restart response (expect 010800010000b1cb'] = readall(ser).hex()

    logger.info("Testing sensor reading")
    try:
        with Reader('/dev/ttyAMA0', 2, baudrate=9600, debug=True) as r:
            result['Data read test from address 2'] = r.read(0, 1, 4)
    except Exception as e:
        result['Error'] = f"Exception: {e}"

    logger.info(f"Result: {result}")

    return result

def sys_reboot(node):
    os.system(
        'busctl call org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager Reboot b "false"')

def sys_start_snapd(node):
    os.system(
        'busctl call org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager StartUnit ss "snapd.service" "replace"'
    )

def sys_stop_snapd(node):
    os.system(
        'busctl call org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager StopUnit ss "snapd.service" "replace"'
    )

def sys_remount_rw(node):
    os.system(
        'busctl call org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager StartUnit ss "remount-rw.service" "replace"'
    )

def sys_remount_ro(node):
    os.system(
        'busctl call org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager StopUnit ss "remount-rw.service" "replace"'
    )


# -*- encoding: utf-8 -*-

import odoo
import odoo.modules.registry
import ast

from odoo import http, _
from odoo.http import request
from odoo.addons.web.controllers.main import Home

import datetime
import json
import pytz
import os
from odoo.addons.student_portal.controllers.main import StudentPortal
import logging

_logger = logging.getLogger(__name__)

#----------------------------------------------------------
# OpenERP Web web Controllers
#----------------------------------------------------------

class StudentPortal(StudentPortal):

    # Main student Portal Profile
    @http.route(['/student/portal/profile'], type='http', auth="user", website=True)
    def student_portal_profile(self, **kw):
        partner = request.env.user.partner_id
        student_id = request.env['student.student'].sudo().search([('user_id','=',request.env.user.id)], limit=1)
        country_ids = request.env['res.country'].sudo().search([])
        state_ids = request.env['res.country.state'].sudo().search([])
        mother_tongue_ids = request.env['mother.toungue'].sudo().search([])
        general_survey_ids = request.env['general.survey'].sudo().search([])
        highest_qualification_ids = request.env['highest.qualification'].sudo().search([])
        values = {'student_profile': student_id,
                'gender':student_id.gender,
                'school_ids':student_id.sudo().school_id,
                'standard_ids': student_id.sudo().standard_id,
                'year_ids': student_id.sudo().year,
                'country_id': student_id.sudo().country_id,
                'country_ids': country_ids,
                'state_id': student_id.sudo().state_id,
                'state_ids': state_ids,
                'marital_status': student_id.maritual_status,
                'mother_tongue_ids': mother_tongue_ids,
                'mother_tongue_id': student_id.sudo().mother_tongue,
                'income':student_id.sudo().income,
                'general_survey_ids': general_survey_ids,
                'general_survey_id': student_id.sudo().general_survey_id,
                'highest_qualification_ids': highest_qualification_ids,
                'highest_qualification_id': student_id.sudo().highest_qualification_id,
                'references_ids': student_id.sudo().reference_ids,
                'parent_ids': student_id.sudo().parent_id,
                'education_background_ids': student_id.sudo().education_background_id,
                'family_ids': student_id.sudo().family_con_ids,
        }
        return request.render("student_portal.student_portal_profile_template", values)

    # Student Profile Update
    @http.route(['/student/profile/update'], type='http', auth="public", website=True, csrf=False)
    def student_profile_update(self, **post):
        partner = request.env.user.partner_id
        student_id = request.env['student.student'].sudo().search([('user_id','=',request.env.user.id)], limit=1)
        vals = {
                'name': post.get('first_name',False) or '',
                'gender': post.get('gender',False),
                'street':post.get('address1',False),
                'street2':post.get('address2',False),
                'state_id':post.get('state_ids',False) and int(post.get('state_ids',False)),
                'zip':post.get('zip',False),
                'country_id':post.get('country_ids',False) and int(post.get('country_ids',False)),
                'phone':post.get('phone',False),
                'mobile':post.get('mobile',False),
                'email':post.get('email',False),
                'contact_phone1':post.get('phone_no',False),
                'contact_mobile1':post.get('mobile_no',False),
                'nric':post.get('nric',False),
                'maritual_status': post.get('marital_status',False),
                'hp_no':post.get('hp_no',False),
                'occupation': post.get('occupation',False),
                'income': '' if post.get('income',False) == 'select_income' else post.get('income',False) ,
                'citizenship': post.get('citizenship',False),
                'remark': post.get('qualification_remark',False),
                'mother_tongue': post.get('mother_tongue_ids',False) and int(post.get('mother_tongue_ids',False)),
                }
        if student_id:
            student_id.sudo().write(vals)
        return request.redirect("/student/portal/profile")


import numpy as np
import math

class Env1(object):
    def __init__(self):
        # 帧结构
        self.frame_slot = 0.005          # 帧时隙时间长度
        self.beam_slot = 100       # 波束选择时隙数
        self.right = 4                 # 正确传输最低的SNR
        # self.frame_period = self.beam_slot * self.frame_slot  # 帧周期时间长度

        # 车辆和道路
        self.road_length = 200          # 道路长度
        self.straight = 100             # 基站和道路的直线距离

        self.v_min = 3                  # 车辆的最小速度
        self.v_max = 5                 # 车辆的最大速度
        self.accelerate = 8             # 车辆的加速度

        self.road_section = 1          # 每几米划分成一个路段
        self.beam_section = 0.5          # 每几米划分成一个路段

        self.road_range = 10                               # 动作可以选择的范围

        # 天线
        self.ann_num = 96                                  # 天线数目

        # 存储单元
        self.cars_posit = 0            # 车辆的位置（连续）
        self.cars_speed = 0            # 车辆的速度（连续

    # 由道路上的所有车辆得到所有车辆的路段
    def get_section(self, pos):
        section = math.ceil(pos / self.road_section)
        return section

    def get_reward(self, act, reward):
        # 直角边
        a = abs(self.road_length / 2 - self.cars_posit)
        # 斜边
        b = np.sqrt(np.square(a) + np.square(self.straight))
        if self.cars_posit > self.road_length / 2:
            th1 = math.pi - math.acos(a / b)
        else:
            th1 = math.acos(a / b)

        channel = []
        for t in range(self.ann_num):
            m = complex(math.cos(math.pi * t * math.cos(th1)), -math.sin(math.pi * t * math.cos(th1)))
            channel.append(m.conjugate())

        # 直角边
        c = abs(self.road_length / 2 - act)
        # 斜边
        d = np.sqrt(np.square(c) + np.square(self.straight))
        if act > self.road_length / 2:
            th2 = math.pi - math.acos(c / d)
        else:
            th2 = math.acos(c / d)

        signal = []
        for t in range(self.ann_num):
            n = complex(math.cos(math.pi * t * math.cos(th2)), -math.sin(math.pi * t * math.cos(th2)))
            signal.append(n)

        SNR = np.square(np.linalg.norm(np.dot(channel, signal)))

        if SNR >= self.right:
            reward += 1
        return reward

    def reset(self):
        # 道路环境初始化
        self.cars_speed = np.random.uniform(self.v_min, self.v_max)
        self.cars_posit = 0
        # 形成状态
        state = [0,0]
        return state

    def step(self, action, state):

        # 道路的（位置更新）
        reward = 0
        for i in range(self.beam_slot):  # 标记当前车辆是否之前被操作过，保证一个时隙车只跑一个时隙的量
            self.cars_posit += self.cars_speed * self.frame_slot
            reward = self.get_reward(action,reward)

        state_ =[self.get_section(self.cars_posit), state[0]]

        if self.cars_posit > self.road_length:
            done = 1
        else:
            done = 0

        return state_, reward, done


#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Challenge Description:

Print out the grade school multiplication table upto 12*12.
'''

col = 12
row = 12
row0 = range(1,col+1)
for l in range(1,row+1):
    line = [l*x for x in row0]
    new = []
    for j in line:
        new.append(('%4d' % j))
    print (''.join(new)).strip()


class Solution(object):
    def findRadius(self, houses, heaters):
        """
        :type houses: List[int]
        :type heaters: List[int]
        :rtype: int
        """
        ans=-1
        heaters.sort()
        for h in houses:
            two_heaters = self.binary_search(h, heaters)
            temp = min(abs(h-two_heaters[0]), abs(two_heaters[1]-h))
            if temp>ans:
                ans=temp
        return ans

    def binary_search(self, val, li):
        begin = 0
        end = len(li) - 1
        p = (begin + end) / 2
        while end - begin > 1:
            if li[p] > val:
                end = p
                p = (begin + end) / 2
            elif li[p] < val:
                begin = p
                p = (begin + end) / 2
            else:
                return [val, val]
        return [li[begin], li[end]]


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

from random import randint
import math

def solve(n, j):
    jamcoins = []
    while len(jamcoins) < j:
        # random jamcoin
        jc = "1"+"".join([str(randint(0,1)) for _ in range(n-2)])+"1"
        divisors = []
        for b in range(2,11):
            jc_nb = int(jc,b)
            div_found = False
            for d in range(3,int(math.sqrt(jc_nb))+1,2):
                if jc_nb % d == 0:
                    divisors.append(d)
                    div_found = True
                    break
            if not div_found:
                break
        if len(divisors) == 9: # 2 to 10
            jamcoins.append(jc + " " + " ".join([str(d) for d in divisors]))
    return "\n".join(jamcoins)

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


"""
Als gemeente wil ik dat de aanvraag tbh een straatoptreden als zaak wordt
gecreëerd zodat mijn dossiervorming op orde is en de voortgang transparant is.

Ref: https://github.com/VNG-Realisatie/gemma-zaken/issues/163

Zie ook: test_userstory_39.py, test_userstory_169.py
"""
import datetime

from django.test import override_settings

from rest_framework import status
from rest_framework.test import APITestCase
from zds_schema.constants import RolOmschrijving, RolTypes
from zds_schema.tests import get_operation_url

# aanvraag aangemaakt in extern systeem, leeft buiten ZRC
from zrc.datamodel.constants import ZaakobjectTypes
from zrc.datamodel.models import Zaak
from zrc.datamodel.tests.factories import ZaakFactory
from zrc.tests.utils import parse_isodatetime

CATALOGUS = 'https://example.com/ztc/api/v1/catalogus/878a3318-5950-4642-8715-189745f91b04'
ZAAKTYPE = f'{CATALOGUS}/zaaktypen/283ffaf5-8470-457b-8064-90e5728f413f'
STATUS_TYPE = f'{ZAAKTYPE}/statustypen/1'

VERANTWOORDELIJKE_ORGANISATIE = 'https://www.example.com/orc/api/v1/rsgb/nietnatuurlijkepersonen/1234'
AVG_INZAGE_VERZOEK = 'https://www.example.com/orc/api/v1/avg/inzageverzoeken/1234'
BEHANDELAAR = 'https://www.example.com/orc/api/v1/brp/natuurlijkepersonen/1234'

@override_settings(LINK_FETCHER='zds_schema.mocks.link_fetcher_200')
class US153TestCase(APITestCase):

    def test_create_zaak_with_kenmerken(self):
        zaak_create_url = get_operation_url('zaak_create')
        data = {
            'zaaktype': ZAAKTYPE,
            'bronorganisatie': '517439943',
            'verantwoordelijkeOrganisatie': VERANTWOORDELIJKE_ORGANISATIE,
            'identificatie': 'AVG-inzageverzoek-1',
            'omschrijving': 'Dagontheffing - Station Haarlem',
            'toelichting': 'Het betreft een clown met grote trom, mondharmonica en cymbalen.',
            'startdatum': '2018-08-15',
            'kenmerken': [{
                'kenmerk': 'kenmerk 1',
                'bron': 'bron 1',
            }, {
                'kenmerk': 'kenmerk 2',
                'bron': 'bron 2',
            }]
        }

        response = self.client.post(zaak_create_url, data, HTTP_ACCEPT_CRS='EPSG:4326')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)

        zaak = Zaak.objects.get(identificatie=data['identificatie'])
        self.assertEqual(zaak.zaakkenmerk_set.count(), 2)

    def test_read_zaak_with_kenmerken(self):
        zaak = ZaakFactory.create()
        zaak.zaakkenmerk_set.create(kenmerk='kenmerk 1', bron='bron 1')
        self.assertEqual(zaak.zaakkenmerk_set.count(), 1)

        zaak_read_url = get_operation_url('zaak_read', uuid=zaak.uuid)

        response = self.client.get(zaak_read_url, HTTP_ACCEPT_CRS='EPSG:4326')
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)

        data = response.json()

        self.assertTrue('kenmerken' in data)
        self.assertEqual(len(data['kenmerken']), 1)
        self.assertDictEqual(
            data['kenmerken'][0],
            {
                'kenmerk': 'kenmerk 1',
                'bron': 'bron 1',
            }
        )

    def test_update_zaak_with_kenmerken(self):
        zaak = ZaakFactory.create()
        kenmerk_1 = zaak.zaakkenmerk_set.create(kenmerk='kenmerk 1', bron='bron 1')
        self.assertEqual(zaak.zaakkenmerk_set.count(), 1)

        zaak_read_url = get_operation_url('zaak_read', uuid=zaak.uuid)
        response = self.client.get(zaak_read_url, HTTP_ACCEPT_CRS='EPSG:4326')

        data = response.json()

        zaak_update_url = get_operation_url('zaak_update', uuid=zaak.uuid)
        data['kenmerken'].append({
            'kenmerk': 'kenmerk 2',
            'bron': 'bron 2',
        })

        response = self.client.put(zaak_update_url, data, HTTP_ACCEPT_CRS='EPSG:4326')
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)

        zaak = Zaak.objects.get(identificatie=zaak.identificatie)
        self.assertEqual(zaak.zaakkenmerk_set.count(), 2)

        # All objects are deleted, and (re)created.
        self.assertFalse(kenmerk_1.pk in zaak.zaakkenmerk_set.values_list('pk', flat=True))

    def test_full_flow(self):
        zaak_create_url = get_operation_url('zaak_create')
        zaakobject_create_url = get_operation_url('zaakobject_create')
        status_create_url = get_operation_url('status_create')
        rol_create_url = get_operation_url('rol_create')

        # Creeer InzageVerzoek
        # self.client.post(...)

        # Creeer Zaak
        data = {
            'zaaktype': ZAAKTYPE,
            'bronorganisatie': '517439943',
            'verantwoordelijkeOrganisatie': VERANTWOORDELIJKE_ORGANISATIE,
            'identificatie': 'AVG-inzageverzoek-1',
            'omschrijving': 'Melding binnengekomen via website.',
            'toelichting': 'Vanuit melding: Beste,\n\nGraag zou ik ...',
            'startdatum': '2018-08-22',
            'kenmerken': [{
                'kenmerk': 'kenmerk 1',
                'bron': 'bron 1',
            }, {
                'kenmerk': 'kenmerk 2',
                'bron': 'bron 2',
            }]
        }
        response = self.client.post(zaak_create_url, data, HTTP_ACCEPT_CRS='EPSG:4326')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)

        zaak = response.json()

        # Koppel Zaak aan InzageVerzoek
        data = {
            'zaak': zaak['url'],
            'object': AVG_INZAGE_VERZOEK,
            'relatieomschrijving': 'Inzage verzoek horend bij deze zaak.',
            'type': ZaakobjectTypes.avg_inzage_verzoek
        }
        response = self.client.post(zaakobject_create_url, data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)

        # Geef de Zaak een initiele Status
        data = {
            'zaak': zaak['url'],
            'statusType': STATUS_TYPE,
            'datumStatusGezet': datetime.datetime.now().isoformat(),
        }
        response = self.client.post(status_create_url, data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)

        # Haal mogelijke rollen op uit ZTC...
        # self.client.get(...)

        # Voeg een behandelaar toe.
        data = {
            'zaak': zaak['url'],
            'betrokkene': BEHANDELAAR,
            'betrokkeneType': RolTypes.natuurlijk_persoon,
            'rolomschrijving': RolOmschrijving.behandelaar,
            'roltoelichting': 'Initiele behandelaar die meerdere (deel)behandelaren kan aanwijzen.'
        }
        response = self.client.post(rol_create_url, data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)

        # Status wijzigingen...

        # Update Zaak met nieuwe behandeltermijn, uitstel van 2 weken.
        zaak_update_url = get_operation_url('zaak_update', uuid=zaak['url'].rsplit('/', 1)[1])

        if zaak['einddatumGepland']:
            end_date_planned = parse_isodatetime(zaak['einddatumGepland'])
        else:
            end_date_planned = datetime.datetime.now()

        data = zaak.copy()
        data['einddatumGepland'] = (end_date_planned + datetime.timedelta(days=14)).strftime('%Y-%m-%d')

        response = self.client.put(zaak_update_url, data, HTTP_ACCEPT_CRS='EPSG:4326')
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)

        # Voeg documenten toe...
        # self.client.post(...)
        # Koppel documenten aan Zaak
        # self.client.post(...)


import numpy as np
from PIL import Image
import torch
import torchvision.transforms as T
from random import random
from krazy_gridworld import Color_Index

class Goal(object):
    red_goal = 0
    blue_goal = 1
    green_goal = 2
    red_lava = 3
    blue_lava = 4
    green_lava = 5
    avoid_lava = 6
    avoid_goal = 7

desired_advices = {
    "Reach red goal",
    "Reach blue goal",
    "Reach green goal"
}

advices = {
    "Reach red goal",
    "Reach blue goal",
    "Reach green goal",
    "Reach red lava",
    "Reach blue lava",
    "Reach green lava"
}

class OptimisticTeacher(object):
    def __init__(self):
        super(OptimisticTeacher, self).__init__()

    def get_advice(self, color, at_goal, is_lava):
        advice_list = []

        for advice in desired_advices:
            if advice_satisfied(advice, color, at_goal, is_lava):
                advice_list.append(advice)

        if len(advice_list) == 0:
            return None
        return advice_list[int(random() * len(advice_list))]

class KnowledgableTeacher(object):
    def __init__(self):
        super(KnowledgableTeacher, self).__init__()

    def get_advice(self, color, at_goal, is_lava):
        advice_list = []

        for advice in advices:
            if advice_satisfied(advice, color, at_goal, is_lava):
                advice_list.append(advice)

        if len(advice_list) == 0:
            return None
        return advice_list[int(random() * len(advice_list))]

class DiscouragingTeacher(object):
    def __init__(self):
        super(DiscouragingTeacher, self).__init__()

    def get_advice(self, color, at_goal, is_lava):
        advice_list = []

        for advice in desired_advices:
            if not advice_satisfied(advice, color, at_goal, is_lava):
                advice_list.append(advice)

        if len(advice_list) == 0:
            return None
        return advice_list[int(random() * len(advice_list))]

class ReplayBuffer:
    def __init__(self, optismic_teacher_instead=False, max_size=5000):
        if optismic_teacher_instead:
            self.teacher1 = OptimisticTeacher()
        else:
            self.teacher1 = KnowledgableTeacher()
        self.teacher2 = DiscouragingTeacher()
        self.max_size = max_size
        self.reset()

    def reset(self):
        self.all_states = []
        self.all_advices = []
        self.all_actions = []
        self.all_rewards = []
        self.all_next_states = []
        self.all_dones = []

    def new_episode(self):
        self.cur_states = []
        self.cur_actions = []
        self.cur_next_states = []
        self.cur_dones = []
        self.cur_colors = []
        self.cur_at_goals = []
        self.cur_is_lavas = []

    def add(self, state, action, next_state, done, color, at_goal, is_lava):
        self.cur_states.append(state)
        self.cur_actions.append(action)
        self.cur_next_states.append(next_state)
        self.cur_dones.append(done)
        self.cur_colors.append(color)
        self.cur_at_goals.append(at_goal)
        self.cur_is_lavas.append(is_lava)

    def compute_reward(self, color, at_goal, is_lava, gamma=0.99):
        advice1 = self.teacher1.get_advice(color, at_goal, is_lava)
        advice2 = self.teacher2.get_advice(color, at_goal, is_lava)

        if advice1 is not None:
            for i in range(len(self.cur_states)):
                self.all_states.append(self.cur_states[i])
                self.all_advices.append(advice1.split(' '))
                self.all_actions.append(self.cur_actions[i])
                if advice_satisfied(advice1, self.cur_colors[i], self.cur_at_goals[i], self.cur_is_lavas[i]):
                    self.all_rewards.append(1)
                else:
                    self.all_rewards.append(0)
                self.all_next_states.append(self.cur_next_states[i])
                self.all_dones.append(self.cur_dones[i])

        if advice2 is not None:
            for i in range(len(self.cur_states)):
                self.all_states.append(self.cur_states[i])
                self.all_advices.append(advice2.split(' '))
                self.all_actions.append(self.cur_actions[i])
                if advice_satisfied(advice2, self.cur_colors[i], self.cur_at_goals[i], self.cur_is_lavas[i]):
                    self.all_rewards.append(1)
                else:
                    self.all_rewards.append(0)
                self.all_next_states.append(self.cur_next_states[i])
                self.all_dones.append(self.cur_dones[i])

        if (len(self.all_states) > self.max_size):
            self.all_states = self.all_states[-self.max_size:]
            self.all_advices = self.all_advices[-self.max_size:]
            self.all_actions = self.all_actions[-self.max_size:]
            self.all_rewards = self.all_rewards[-self.max_size:]
            self.all_next_states = self.all_next_states[-self.max_size:]
            self.all_dones = self.all_dones[-self.max_size:]

    def __len__(self):
        return len(self.all_states)

    def sample(self, bs=64):

        if len(self.all_states) < bs:
            return None

        indexes = np.arange(len(self.all_states))
        np.random.shuffle(indexes)
        indexes = indexes[:bs]
        states, advices, actions, rewards, next_states, dones = [], [], [], [], [], []

        for i in indexes:
            state, advice, action, reward, next_state, done = self.all_states[i], self.all_advices[
                i], self.all_actions[i], self.all_rewards[i], self.all_next_states[i], self.all_dones[i]
            states.append(np.array(state, copy=False))
            advices.append(np.array(advice, copy=False))
            actions.append(np.array(action, copy=False))
            rewards.append(np.array(reward, copy=False))
            next_states.append(np.array(next_state, copy=False))
            dones.append(np.array(done, copy=False))

        return (np.array(states), np.asarray(advices)), np.array(actions), np.array(rewards), \
            np.array(next_states), np.array(dones)

def sample_advice():
    advice_list = list(desired_advices)
    advice = advice_list[int(random() * len(advice_list))]
    return advice

def get_state(env):
    return env.get_combined_obs().transpose((2, 0, 1))

def advice_satisfied(initial_advice, color, at_goal, is_lava):
    if at_goal and color == Color_Index.red and initial_advice == "Reach red goal":
        return True
    if at_goal and color == Color_Index.blue and initial_advice == "Reach blue goal":
        return True
    if at_goal and color == Color_Index.green and initial_advice == "Reach green goal":
        return True
    if is_lava and color == Color_Index.red and initial_advice == "Reach red lava":
        return True
    if is_lava and color == Color_Index.blue and initial_advice == "Reach blue lava":
        return True
    if is_lava and color == Color_Index.green and initial_advice == "Reach green lava":
        return True
    if at_goal and initial_advice == "Avoid any goal":
        return True
    if is_lava and initial_advice == "Avoid any lava":
        return True
    return False


import pip
import pkg_resources
# *** Add required packages to this list ***
packages = ['pyyaml', 'numpy', 'matplotlib']
# *** Add required packages to this list ***

def main(packages, upgrade = False):
    # Install gslab_tools if it's not installed yet or it has the wrong version
    pip.main(['install', '--upgrade', 'git+http://git@github.com/gslab-econ/gslab_python.git@v4.1.0'])

    # Install required packages using pip
    installed_packages = [pkg.key for pkg in pip.get_installed_distributions()]
    for pkg in packages:
        if upgrade:
            pip.main(['install', '--upgrade', pkg])
        elif pkg not in installed_packages:
            pip.main(['install', pkg])

# upgrade = TRUE will update all packages to the most current version
# upgrade = FALSE will skip packages that are already installed
main(packages, upgrade = False)


#!/usr/bin/env python3
import socket
import logging
import sys
logging.basicConfig(format='%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)

HOST = "www.google.com"
PORT = 80
BUFFER_SIZE = 1024

# payload = """GET / HTTP/1.0
# Host: {HOST}
# """.format(HOST=HOST)
payload = "GET / HTTP/1.0\r\n\r\n"

def conn_socket(addr_tup):
    (family, socktype, proto, canonname, sockaddr) = addr_tup
    print(proto)
    try:
        s = socket.socket(family, socktype, proto)
        s.connect(sockaddr)
        s.sendall(payload.encode())
        #s.sendall(payload)

        full_data = b""
        while True:
            data = s.recv(BUFFER_SIZE)
            #print(data)
            if not data:
                break
            full_data += data

        #print(full_data)
    except Exception as e:
        print(e)
        pass
    finally:
        s.close()

def main():
    addr_info = socket.getaddrinfo(HOST, PORT, proto=socket.SOL_TCP)
    addr_tup = addr_info[0]
    logging.debug('addr_tup = {}'.format(addr_tup))

    conn_socket(addr_tup)

if __name__ == "__main__":
    main()


#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
import logging
import sys
# import copy
import traceback
import subprocess
import os
from time import sleep

def send_message(message: str, chat: str, max_tryes: int, logger, token: str, additional_log_info: str = None) -> int:
    """
    Функция отправляющая текстовое сообщение
    :param str message: текст отправляемого сообщения
    :param str chat: ChatID  куда отправлять
    :param int max_send_tryes: максимальное количество попыток на отправку
    :param logging.handlers.RotatingFileHandler logger: логгер куда писать о событиях в процессе,  рекомендуемый тип: logging.handlers.RotatingFileHandler
    :param str token: токен бота с  помощью которого будут отправляться сообщение
    :param str additional_log_info: дополнительная информация котрорая будет записана в лог перед выполнением функции
    :return int: результат выполнения. 0 - в случае успеха, 1 - неудача
    """
    # fixme Прикрутить условие - использовать тор прокси или нет
    result = 255
    msg_send_state = 2
    msg_send_tryes = 0
    # max_tryes = copy(max_send_tryes)
    while (msg_send_state != 0):
        try:
            c = 'curl --socks5 127.0.0.1:9050 --max-time 9000 --connect-timeout 900 -d chat_id=' + chat + ' -d text="' + message + '"  https://api.telegram.org/bot' + token + '/sendMessage'
            logger.debug('Собираюсь отправить информационное текстовое сообщение')
            if additional_log_info is not None:
                if type(additional_log_info) == str:
                    logger.debug(additional_log_info)
                else:
                    logger.debug('Дополнительная информация для логгера не является строкой и записана не будет')
            logger.debug('Команда которая будет выполнена: ' + str(c) + '\n')
            proc = subprocess.Popen(c, shell=True, stdout=subprocess.PIPE)
            proc.wait()
            out = proc.communicate()
            logger.debug('Подробности отправки информационного текстового сообщения: ')
            logger.debug('Код отправки: ' + str(proc.returncode))
            logger.debug('stdout отправки: ' + str(out[0].decode('utf-8')))
            if out[1] is not None:
                logger.debug('stderr отправки: ' + str(out[1].decode('utf-8')))
            else:
                logger.debug('stderr был пуст')
            if proc.returncode == 0:
                msg_send_state = 0
                result = 0
                if msg_send_tryes == 0:
                    logger.info('информационное текстовое сообщение было успешно отправлено с первой попытки')
                else:
                    logger.info(
                        'информационное текстовое сообщение было отправлено с ' + str(msg_send_tryes + 1) + ' попытки')
                break
            else:
                msg_send_state = 1
            if msg_send_state == 1:
                msg_send_tryes = msg_send_tryes + 1
                logger.info('Попытки отправить информационное текстовое сообщение: ' + str(msg_send_tryes))
                max_tryes = max_tryes - 1
                if max_tryes == 0:
                    result = 1
                    logger.critical('Превышено максимальное количество попыток отправки, заданное '
                                    'в параметрах: ' + str(msg_send_tryes))
                    sys.exit('Превышено максимальное количество попыток отправки, заданное '
                             'в параметрах')
                sleep(10)
                continue
        except Exception as e:
            logger.error('Произошла ошибка отправки сообщения')
            logger.debug('Exception при отправке текстового сообщения, подробности ниже')
            logger.debug('Тип Exception: ' + str(type(e)))
            logger.debug('Аргументы исключения:' + str(e.args))
            # logger.debug('Стэк вызовов: \n' + str(e.with_traceback()))
            logger.debug('Команда использованная для отправки: ' + c)
            logger.debug('Стэк вызовов: ' + str(traceback.format_exc()))
            logger.debug('Код возврата: ' + str(proc.returncode))
            logger.debug('stdout: ' + str(proc.stdout.read().decode('utf-8')))
            if proc.stderr is not None:
                logger.debug('stderr: ' + str(proc.stderr.read().decode('utf-8')))
            sys.exit('Произошла ошибка при отправке сообщения')
        finally:
            proc.kill()
            return result

def send_files(files: tuple, logger, max_tryes: int, chat: str, token: str) -> int:
    """
    Функция отправляющая указанные файлы в чат
    :param tuple files: массив файлов, тип каждого элемента str
    :param logging.handlers.RotatingFileHandler logger: логгер куда писать о событиях в процессе,  рекомендуемый тип: logging.handlers.RotatingFileHandler
    :param int max_tryes: максимальное количество попыток на отправку
    :param str chat: ID чата куда отправлять
    :param str token: token бота с помощью которого отправлять
    :return int: результат выполнения. В случае успеха 0, в случае неудачи  1
    """
    result = 255
    not_uploaded = []
    logger.info('Всего файлов к отправке: ' + str(len(files)))
    logger.info('Начинаю отправку файлов....')
    # fixme Добавить уведомление в случае облома
    total = len(files)
    sent = 0
    for file in files:
        # На случай если понадобится менять identity у  tor
        # with Controller.from_port(port=9050) as controller:
        #     controller.signal(Signal.NEWNYM)

        file_send_state = 2
        file_send_tryes = 0
        while (file_send_state != 0):
            if not os.path.isfile(file):
                logger.critical('Файл ' + file + ' не найден, но числится  в массиве')
                logger.debug('Поиск производился: ' + str(os.getcwd()) + '\n')
            logger.debug('\n\n\nСобираюсь отправить файл ' + file)
            cmd = 'curl --socks5 127.0.0.1:9050 --max-time 9000 --connect-timeout 900 ' \
                  '-F document=@"' + file + '" https://api.telegram.org/bot' + token + '/sendDocument?chat_id=' + chat
            # #cmd = 'curl --socks5 127.0.0.1:9050 --max-time 9000 --connect-timeout 900 ' \
            #       '-F document=@"' + file + '" https://api.telegram.org/bot888746107:AAGNg4KGik97AkmHnbDQeGUrlG57Qjb7B1c/' \
            #                                 'sendDocument?chat_id=-341753010'
            logger.debug('Команда на выполнение для отправки файлов: ' + cmd + '\n')
            logger.debug('Значение в file: ' + file + '\n')
            if os.path.exists(file):
                logger.debug('Файл существует ' + file + '\n')
            try:
                proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                proc.wait()
                # logger.info('Собираюсь отправить файл: ' + file + '\n')
                # logger.debug('С помощью команды: \n' + cmd + '\n')
                out = proc.communicate()
                if out[0] is not None:
                    logger.debug('stdout для отправки: ' + str(out[0].decode('utf-8')))
                if out[1] is not None:
                    logger.debug('stderr для отправки: ' + str(out[1].decode('utf-8')))
                if proc.returncode == 0:
                    file_send_state = 0
                    sent = sent + 1
                    logger.info('Файлов успешно отправлено на текущий момент: ' + str(sent))
                    logger.info('Осталось отправить файлов: ' + str(total - sent))
                    if file_send_tryes > 0:
                        logger.info(
                            'Файл ' + file + 'был  успешно отправлен  с ' + str(file_send_tryes + 1) + ' попытки')
                    else:
                        logger.info(' Файл ' + file + ' был успешно отправлен с первой попытки')
                        proc.kill()
                    break
                else:
                    not_uploaded.append(file)
                    logger.critical('Файл ' + file + ' не удалось отправить, curl вернул ' + str(proc.returncode))
                    # logger.critical('Файлов неудачно отправлено на текущий момент: ' + str(len(not_uploaded)))
                    file_send_state = 1
                    file_send_tryes = file_send_tryes + 1
                    max_tryes = max_tryes - 1
                    if max_tryes == 0:
                        logger.critical(
                            'Превышено максимальное количество попыток отправки, указанное в конфигурации:' +
                            str(max_tryes))
                        result = 1
                        sys.exit(1)
                    logger.info('Попыток отправки для этого файла: ' + str(file_send_tryes))
                    logger.debug('Этот текст читаем ?')
                    logger.debug('Код отправки файла: ' + str(proc.returncode))
                    logger.debug('stdout отправки файла: ' + file + '\n' + str(out[0].decode('utf-8')))
                    if out[1] is not None:
                        logger.debug('stderr отправки файла: ' + file + '\n' + str(out[1].decode('utf-8')))
                    sleep(10)
                    proc.kill()
                    continue
            except Exception as e:
                logger.debug('Случился Exception, ниже инфа о нем:' + '\n')
                logger.debug('тип случившегося исключения: ' + str(type(e)) + '\n')
                logger.debug('Аргументы исключения: ' + str(e.args) + '\n')
                logger.critical('Произошла неизведанная хуйня(' + str(type(e)) + '), чтобы узнать подробности нужно '
                                                                                'включить уровень DEBUG для логгирования \n')
                pass
    return result

def clear_folder(dir_name: str, logger, force=False) -> int:
    """ функция удаляющая созданный образ ova из указанной папки и архивы из нее же
    :param str dir_name: Директория откуда нужно удалить файлы
    :param logging.handlers.RotatingFileHandler logger: logger куда записывать о результатах
    :param bool force: если укзать True будет использован параметр -f  для rm, по умолчанию False
    :return: В случае успеха - 0,  в случае если не удалось удалить образ ova, но удалось удалить архивы - 1, в
    случае если не удалось удалить ничего - 2
    """
    result = 255
    if os.path.isdir(dir_name):
        logger.info('Директория для удаления файлов обнаружена, перехожу в директорию для зачистки')
        os.chdir(dir_name)
    else:
        logger.error('Не удалось удалить файлы, так как была указана некорректная директоррия')
        sys.exit('Указана некорректная директория для удаления файлов')
    if not force:
        cmd = 'rm -r  ./*.ova'
    else:
        cmd = 'rm -rf ./*.ova'
    logger.info('Приступаю к удалению файла образа в директории: ' + str (os.getcwd()))
    logger.debug('C помощью команды: ' + cmd)
    proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    proc.wait()
    out = proc.communicate()
    if proc.returncode == 0:
        logger.info('Файл образа был успешно удален')
        result = 254
    logger.debug('Код возврата для операции удаления образа: ' + str(proc.returncode) + '\n')
    logger.debug('stdout операции удаления образа: ' + str(out[0].decode('utf-8')))
    if out[1] is not None:
        logger.debug('stderr операции удаления образа' + str(out[1].decode('utf-8')))
    else:
        logger.debug('stderr операции удаления образа был пустым')
    proc.kill()
    logger.debug('Собираюсь удалить файлы ./*.tar.bz* в директории ' + dir_name + '\n')
    logger.info('Проверяю наличие директории')
    if os.path.isdir(dir_name):
        logger.info('Директория обнаружена')
        os.chdir(dir_name)

    if not force:
        cmd = 'rm -r  ./*.tar.bz*'
    else:
        cmd = 'rm -rf ./*.tar.bz*'
    logger.debug('Команда которая будет использована для удаления: ' + cmd)
    logger.info('Приступаю к удалению...')
    proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    proc.wait()
    out = proc.communicate()
    if proc.returncode == 0:
        logger.info('Удаление архивов прошло успешно')
        if result == 254:
            result = 0
        if result == 255:
            result = 1
    else:
        result = 2
    logger.debug('Код возврата для операции удаления: ' + str(proc.returncode) + '\n')
    logger.debug('stdout операции удаления архивов' + str(out[0].decode('utf-8')))
    if out[1] is not None:
        logger.debug('stderr операции удаления архивов; \n' + str(out[1].decode('utf-8')))
    else:
        logger.debug('stderr операции удаления архивов был пустым')
    proc.kill()
    return result

def main(*args, **kwargs):
    print('Этот файл не предназначен для самостоятельного запуска')
    print('Используйте import telegramAPI...')

if __name__ == '__main__':
    main()
    sys.exit('Неправильное использование модуля')


def ivtk_scene(actors):
	from tvtk.tools import ivtk
	#创建一个带Crust (Python Shell)的窗口
	win = ivtk.IVTKWithCrustAndBrowser()
	win.open()
	win.scene.add_actor(actors)

	#修正错误
	dialog = win.control.centralWidget().widget(0).widget(0)
	from pyface.qt import QtCore
	dialog.setWindowFlags(QtCore.Qt.WindowFlags(0x00000000))
	dialog.show()
	return win

def event_loop():
	#开始界面消息循环
	from pyface.api import GUI
	gui = GUI()
	gui.start_event_loop()


import os
from PIL import Image

SQUARE_FIT_SIZE = 300
LOGO_FILENAME = 'catlogo.png'

logoIm = Image.open(LOGO_FILENAME)
logoWidth, logoHeight = logoIm.size

for filename in os.listdir('.'):
    if not (filename.lower().endswith('.png') or filename.lower().endswith('.jpg') or filename.lower().endswith('.gif') or filename.lower().endswith('bmp')) or filename == LOGO_FILENAME:
        continue

    im = Image.open(filename)
    width, height = im.size

    if width > SQUARE_FIT_SIZE and height > SQUARE_FIT_SIZE:
        if width > height:
            height = int((SQUARE_FIT_SIZE / width) * height)
            width = SQUARE_FIT_SIZE
        else:
            width = int((SQUARE_FIT_SIZE / height) * width)
            height = SQUARE_FIT_SIZE

    print('Resizing %s...' % (filename))
    im = im.resize((width, height))

    if width < logoWidth*2 or height < logoHeight*2:
        print("The logo will not fit %s file"%filename)
    else:
        print('Adding logo to %s...' % (filename))
        im.paste(logoIm, (width-logoWidth, height-logoHeight), logoIm)

    im.save(os.path.join('withLogo', filename))

Manipulating Images/Extending and Fixing the Chapter Project Programs.py


# -*- coding: utf-8 -*-
import data_setup

import pandas as pd
import numpy as np

from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.ensemble import RandomForestRegressor

def get_score_from_cross_validation(model, input_train, target, all_columns_except_target, valid_split_size=5):
    train = input_train.copy(deep=True)
    items_in_valid_set = len(train) // valid_split_size
    accum_score_from_validation = 0

    for i in range(valid_split_size):
        valid_set = train[i*items_in_valid_set:(i+1)*items_in_valid_set]
        train_set = train[:i*items_in_valid_set].append(train[(i+1)*items_in_valid_set:])
        model.fit(train_set[all_columns_except_target],train_set[target])
        y_pred = model.predict(valid_set[all_columns_except_target])
        score_on_train = mean_squared_error(valid_set[target], y_pred)
        accum_score_from_validation += score_on_train

    score_from_validation = accum_score_from_validation/valid_split_size
    return score_from_validation

def score_model(model, train, target, all_columns_except_target, model_name, model_setup="default"):
    print("Model: {}, {}".format(model_name, model_setup))
    print("Score on train data using cross-validation: {}".format(get_score_from_cross_validation(model, train, target, all_columns_except_target, valid_split_size=5)))
    print("---------------------------------")

def score_model_on_test_data(model, train_data, test_data, target, all_columns_except_target, model_name, model_setup="default"):
    model.fit(train_data[all_columns_except_target], train_data[target])
    y_pred_test = model.predict(test_data[all_columns_except_target])
    score_on_test = mean_squared_error(test_data[target], y_pred_test)
    print("Model: {}, {}".format(model_name, model_setup))
    print("Score on test data: {}".format(score_on_test))
    print("---------------------------------")

def remove_items_from_list(main_list, items_to_be_removed_list):
    for item_to_remove in items_to_be_removed_list:
        if item_to_remove in main_list:
            main_list.remove(item_to_remove)
    return main_list

def from_2_lists_get_1_with_common_items(list1, list2):
    common_list = list1[:]
    for item in list1:
        if item not in list2:
            common_list.remove(item)
    return common_list

def main():
    # 2 input parameters for the model
    start_date_is_used = True #change to False if start date (Ienākšanas datums) should not be used
    case_end_year_in_test_data_is_only_2018 = True #set to True if in test set all cases end in 2018, otherwise set to False

    #-----------------------------------------------------------------------------------------------------
    #data cleaning and features engineering section for train data
    print("Reading input files")
    raw_data = pd.read_csv('train_input.csv')
    public_data = pd.read_csv('public_data.csv')

    print("===========================================================")
    print("================  Train data setup  =======================")
    print("===========================================================")

    transformed_data = data_setup.setup(raw_data, public_data,
            start_date_is_used = start_date_is_used,
            train_data_setup = True,
            case_end_year_in_test_data_is_only_2018 = case_end_year_in_test_data_is_only_2018)

    data = transformed_data.copy(deep=True)

    print("================  Train data setup done  ==================")
    print("===========================================================")
    print()

    #Splitting data on train and test parts. Also saving the full training set for the final model
    train, test = train_test_split(data, test_size=0.2, random_state=1)
    full_train_set = data.copy(deep=True)

    #store the name of the variable we want to predict. Separately store the names of other variables we want to exclude
    target = 'duration_m'

    columns_to_exclude = [
         'start_date', 'end_date', 'court_name', 'case_id', 'court_id', 'date_of_birth',
         'duration_m', 'region',
        # 'court_aizkraukles_rajona_tiesa', # 'court_alūksnes_rajona_tiesa', # 'court_bauskas_rajona_tiesa', # 'court_cēsu_rajona_tiesa',
        # 'court_daugavpils_tiesa', # 'court_dobeles_rajona_tiesa', # 'court_gulbenes_rajona_tiesa', # 'court_jelgavas_tiesa',
        # 'court_jēkabpils_rajona_tiesa', # 'court_kuldīgas_rajona_tiesa', # 'court_kurzemes_rajona_tiesa', # 'court_liepājas_tiesa',
        # 'court_limbažu_rajona_tiesa', # 'court_madonas_rajona_tiesa', # 'court_ogres_rajona_tiesa', # 'court_rēzeknes_tiesa',
        # 'court_rīgas_pilsētas_kurzemes_rajona_tiesa', # 'court_rīgas_pilsētas_latgales_priekšpilsētas_tiesa', # 'court_rīgas_pilsētas_pārdaugavas_tiesa',
        # 'court_rīgas_pilsētas_vidzemes_priekšpilsētas_tiesa', # 'court_rīgas_pilsētas_zemgales_priekšpilsētas_tiesa', # 'court_rīgas_pilsētas_ziemeļu_rajona_tiesa',
        # 'court_rīgas_rajona_tiesa', # 'court_rīgas_rajona_tiesa_jūrmalas_tiesu_nams', # 'court_saldus_rajona_tiesa', # 'court_talsu_rajona_tiesa',
        # 'court_tukuma_rajona_tiesa', # 'court_valkas_rajona_tiesa', # 'court_valmieras_rajona_tiesa', # 'court_ventspils_tiesa',
        # 'court_meetings_set', # 'court_meetings_happened',
         'admin_penalty', 'admin_penalty_not_zero', 'money_amount_from_2_to_15', 'money_amount_from_15_to_50', 'money_amount_from_50_to_500',
         'money_amount_from_500_to_1000', 'money_amount_from_1000_to_2000', 'money_amount_from_2000_to_5000', 'money_amount_from_5000_to_50_000',
         'money_amount_from_50_000_to_1000_000', 'money_amount_over_1000_000', 'court_meetings_set_is_zero', 'court_meetings_happened_is_zero',
     ]

    all_columns_except_target = list(train) #list with all columns in train data
    all_columns_except_target = remove_items_from_list(all_columns_except_target, columns_to_exclude) #excluding some variables
    all_columns_except_target_train = all_columns_except_target[:] #save a copy

    #-----------------------------------------------------------------------------------------------------
    #testing the model on the training set
    #default settings vs manually calibrated settings - RandomForestRegressor
    print("===========================================================")
    print("================  Testing the model  ======================")
    print("===========================================================")

    model = RandomForestRegressor(n_estimators = 60, min_samples_leaf=2, random_state=1, n_jobs=-1)
    score_model(model, train, target, all_columns_except_target, 'RandomForestRegressor', "calibrated")
    score_model_on_test_data(model, train, test, target, all_columns_except_target, 'RandomForestRegressor' ,'calibrated')

    #-----------------------------------------------------------------------------------------------------
    # prepoaring unseen/test data on which predictions will be made
    print()
    print("Reading input file for prediction and the public data file")
    raw_data_to_predict = pd.read_csv('input_data_to_predict.csv')
    public_data = pd.read_csv('public_data.csv')

    print("===========================================================")
    print("================  Preparing unseen/test data  =============")
    print("===========================================================")

    transformed_data_to_predict = data_setup.setup(raw_data_to_predict, public_data,
            start_date_is_used = start_date_is_used,
            train_data_setup = False,
            case_end_year_in_test_data_is_only_2018 = case_end_year_in_test_data_is_only_2018)

    unseen_data = transformed_data_to_predict.copy(deep=True)

    all_columns_except_target = list(unseen_data)
    all_columns_except_target = remove_items_from_list(all_columns_except_target, columns_to_exclude)
    all_columns_except_target = from_2_lists_get_1_with_common_items(all_columns_except_target, all_columns_except_target_train)

    print("================  Data setup done  ========================")
    print("===========================================================")
    print()

    #training the model on the full training set and then making the prediction on the unseen data
    print("===========================================================")
    print("======  Training the model and making predictions =========")
    print("===========================================================")

    final_model = RandomForestRegressor(n_estimators = 60, min_samples_leaf=2, random_state=1, n_jobs=-1)
    final_model.fit(full_train_set[all_columns_except_target],full_train_set[target])
    unseen_data['predictions'] = final_model.predict(unseen_data[all_columns_except_target])
    unseen_data.to_csv('predictions.csv')

    print("======  Predictions are done and stored in a CSV file =====")
    print("===========================================================")

if __name__ == "__main__":
    main()


name = 'GIDEON'
name_variations = ['GIDEON', 'Gideen', 'Gidein', 'Gydeen', 'Gydein', 'Gydeon', 'Gideonn']
search_engines = ['google', 'wikipedia']
search_terms = ['who', 'what', 'where', 'when', 'why', 'google', 'show me', 'search']

def set_engine_properties(user):
    if not user:
        return [
            "Ready for voice commands.",
            "Can you tell me your ",
            "Sorry I couldn't understand. Can we try again? I thought you said '",
            "Okay, I'll get the information from Wikipedia.",
            "Okay, I'll search Google for ",
            "Okay, I'll bring up Google Maps for ",
            "I've updated my name to ",
            "Alright. Stopping now."

        ]
    else:
        return {
            "hello": "%s, I'm ready for voice commands." % user,
            "utility": "%s can you tell me your " % user,
            "unable to process": "Sorry %s I couldn't understand. Can we try again? I thought you said '" % user,
            "wikipedia search": "Okay %s, I'll get the information from Wikipedia." % user,
            "google search": "Okay %s, I'll search Google for " % user,
            "google maps location request": "Okay %s, I'll bring up Google Maps for " % user,
            "update name": "%s, I've updated my name to " % user,
            "end program": "Alright %s." % user,
            "get system time": "Okay %s, I'll get you the time." % user

        }

def update_name(user_request):
    global name
    name = user_request


#!/usr/bin/env python3

from tkinter import *
from hangman import hangMan
from tkinter import font as font

class hangManGUI(Frame):
    def __init__(self):
        self.root = Tk()

        Frame.__init__(self, self.root)

        self.pack()

        self.__widgets__(self.__makeGuess__)
        self.__play__()

        self.mainloop()

    def __makeGuess__(self, key):
        if key == "new game":
            self.__play__()

        self.__game.guessLetter(key)
        self.__showDisplay()

    def __showDisplay(self):
        self.__guessCount.config(text=str("Guesses:" + str(self.__game.guesses)))

        self.__displayed.configure(state='normal')

        self.__displayed.delete('1.0', END)

        self.__displayed.insert(END, '\n')

        for l in self.__game.displayed:
            self.__displayed.insert(END, str(l))

        self.__guessedLetters.configure(state='normal')

        self.__guessedLetters.delete('1.0', END)

        for l in self.__game.guessed:
            self.__guessedLetters.insert(END, str(l))

        self.__guessedLetters.configure(state='disabled')

    def __widgets__(self, callback):
        #initialize the buttons
        j = 0
        i = 0
        block = 10
        mRow = 0

        letters = [['q','w','e','r','t','y','u','i','o','p'],
                   ['a','s','d','f','g','h','j','k','l'],
                   [' ','z','x','c','v','b','n','m','new game']]

        self.__buttons = Frame(self)
        self.__buttons.grid(row=1, column=0)

        for column in letters:
            for l in column:
                if l.isalpha():
                    b = HMButton(self.__buttons, callback, text=str(l))
                    b.config(width=block)
                    b.grid(row=i, column=j, columnspan=1, sticky=N + E + W + S)

                if len(l) > 1:
                    b = HMButton(self.__buttons, callback, text=str(l))
                    b.config(width=block)
                    b.grid(row=i, column=j, columnspan= mRow - j + 1, sticky=N + E + W + S)

                if mRow < j:
                    mRow = j

                j+=1

            j=0
            i+=1

        #set the upper portion of the GUI
        self.__upper = Frame(self, width = 10, height = 10)
        self.__upper.grid(row=0, column=0)
        self.__font = font.Font(family='Helvetica', size=30, weight='bold')

        #set the text
        self.__displayed = Text(self.__upper, font = self.__font, height=5, width=20)
        self.__displayed.config(state=DISABLED)
        self.__displayed.grid(row = 2, column = 1)

        #set the guess portion
        self.__guessDisplay = Frame(self.__upper)
        self.__guessDisplay.grid(row=0, column=0, sticky=N+W)

        self.__guessCount = Label(self.__guessDisplay)
        self.__guessCount.grid(row=0, column=0, sticky=N+W)

        self.__guessedLetters = Text(self.__guessDisplay, font=self.__font, height = 1, width=20)
        self.__guessedLetters.config(state=DISABLED)
        self.__guessedLetters.grid(row=1, column=0, stick=N+W)

    def __play__(self):
        self.__game = hangMan()
        self.__showDisplay()

class HMButton(Button):
    def __init__(self, master, command, text):
        Button.__init__(self, master, command = lambda : command(text), text=text)

h = hangManGUI()


#! python3
# -*- coding: utf-8 -*-

import random
import docx
from . import docxprint
def word_shuffle(some_nouns):
	Aufgabe = {
		"Kopfzeile": "Name: 				Klasse: 				Datum:  \n ",
		"Titel": "",
		"1. Aufgabe": "Finde die Wörter aus den Lücken!\n",
		"Hinweise": "Hier sind die Wörter aus den Lücken: \n",
		"Rätselwörter": "Hier ein paar Rätselwörter aus dem Text: \n",
	}
	doc = docx.Document()# initializing python-docx
	save_path = docxprint.docx_print(Doc= doc, save= 'wordshuffle')
	some_nouns = list(set(some_nouns))
	#list(dict.fromkeys(some_nouns)) #erase duplicates
	random.shuffle(some_nouns) #shuffle für Rätselwörter
	docxprint.docx_print(printText=Aufgabe["Rätselwörter"], Bold=True, Doc=doc)
	print(some_nouns)
	for word in some_nouns:
		if word == some_nouns[-1]:
			break
		word = str(word)
		shuffled = list(word)
		random.shuffle(shuffled)
		shuffled = ''.join(shuffled)
		docxprint.docx_print(printText=shuffled + ' ______________________________', Doc=doc)

	doc.save(save_path)


'''
There is an array of n integers. There are also 2 disjoint sets, A and B, each containing m integers.
You like all the integers in set A and dislike all the integers in set B. Your initial happiness is 0.
For each i integer in the array, if i in A, you add 1 to your happiness. If i in B, you add -1 to your happiness.
Otherwise, your happiness does not change. Output your final happiness at the end.

Note: Since A and B are sets, they have no repeated elements. However, the array might contain duplicate elements.

Constraints
1 <= n <= 10 ^ 5
1 <= m <= 10 ^ 5
1 <= Any integer in the input <= 10 ^ 9

Input Format

The first line contains integers n and m separated by a space.
The second line contains n integers, the elements of the array.
The third and fourth lines contain m integers, A and B, respectively.

Output Format

Output a single integer, your total happiness.
'''

_ = input()
array = input().split()
like = set(input().split())
dislike = set(input().split())
like1 = 0
dislike1 = 0
for elem in array:
    if elem in like:
        like1 += 1
for elem2 in array:
    if elem2 in dislike:
        dislike1 += 1

res = like1 - dislike1
print(res)


import subprocess as sp
import os
import sys
import signal
import env_configs as cfg
import time
import requests

class Worker:
  def __init__(self, envs=None):
    if envs is None:
      self.envs = cfg.Enviornment().envs
    else:
      self.envs = envs

    assert 'GSKY_GRPC_POOL_SIZE' in self.envs
    self.is_init = False
    self.envs['GSKY_GRPC_POOL_SIZE'] = '32' # AVS

  def start_async(self):
    if not self.is_init:
      if 'TMPDIR' in self.envs:
        if not os.path.exists(self.envs['TMPDIR']):
          os.makedirs(self.envs['TMPDIR'])
      self.proc = sp.Popen('''(set -xeu
        cd "$GSKY_SHARE_DIR" && ./grpc_server -p $GSKY_GRPC_PORT -n $GSKY_GRPC_POOL_SIZE
        )''', env=self.envs, shell=True, stdout=sys.stdout, stderr=sys.stderr, preexec_fn=os.setpgrp)

      self.is_init = True

  def start(self):
    self.start_async()

    pool_size = int(self.envs['GSKY_GRPC_POOL_SIZE'])

    worker_timeout = 30
    for i in range(worker_timeout):
      try:
        n_procs_str = sp.check_output('ps s|grep gdal-process|wc -l', shell=True, stderr=sys.stderr, universal_newlines=True).strip()
        n_procs = int(n_procs_str)
        # pool_size + 1 takes 'grep gdal-process' into account
        if n_procs >= pool_size + 1:
          return
      except:
        pass

      ret = self.proc.poll()
      if ret is not None:
        self.is_init = False
        raise Exception('worker abnormally exited')

      time.sleep(1)

    raise Exception('worker startup timed out')

  def stop(self):
    if self.is_init:
      os.killpg(os.getpgid(self.proc.pid), signal.SIGTERM)


from collections import namedtuple
import subprocess

#given a diff output (list of lines), return the changed files and changed lines
def get_diff_detail(diff):
    result = namedtuple('result','oldlines newlines')
    filelines = {}
    curfile = ''
    oldline = []
    newline = []
    flag = False
    for line in diff:
        if line.startswith('--- a/'):
            #store the previous file & reset the curfile and curline
            if len(curfile) > 0:
                filelines[curfile] = result(oldline,newline)
            curfile = line[6:]
            oldline = []
            newline = []
        if line.startswith('--- /dev/null'):
            flag = True
        if flag and line.startswith('+++ b/'):
            if len(curfile) > 0:
                filelines[curfile] = result(oldline,newline)
            curfile = line[6:]
            oldline = []
            newline = []
            flag = False
        if line.startswith('@@ '):
            lines = get_line_number(line)
            #oldline/newline might be empty (), but we don't check this to ensure they are in pairs
            oldline.append(lines.oldline)
            newline.append(lines.newline)
    #the last file
    filelines[curfile] = result(oldline,newline)
    return filelines

#given a line starts with @@, get its diff line numbers
def get_line_number(line):
        rawline = ''
        if line.find(' @@ ') > -1:
            rawline = line[3:line.find(' @@ ')]
        else:
            rawline = line[3:line.find(' @@')]
        half1 = rawline[1:rawline.find(' +')]
        half2 = rawline[rawline.find(' +')+2:]

        #tuple (start-line, end-line)
        oldline = ()
        if ',' in half1:
            count = int(half1[half1.find(',')+1:])
            if not count == 0:
                startline = int(half1[:half1.find(',')])
                oldline = (startline, startline + count -1)
        else:
            oldline = (int(half1), int(half1))

        newline = ()
        if ',' in half2:
            count = int(half2[half2.find(',')+1:])
            if not count == 0:
                startline = int(half2[:half2.find(',')])
                newline = (startline,startline + count -1)
        else:
            newline = (int(half2), int(half2))

        result = namedtuple("result", "oldline newline")
        return result(oldline, newline)

#get all the changed lines in the given file in the given commit
def get_file_change_lines(commit_hash, file, context):
    #list of range tuple (startline, endline)
    old_line_range = []
    new_line_range = []
    #TODO not sure we should switch branch here (maybe not, cuz now it's in the right branch). Also this call should belong to crawler instead of util
    cmd = 'git show -w --unified=' + str(context) + ' --pretty=format:b% ' + commit_hash + ' -- ' + file
    output = subprocess.check_output(cmd, shell=True)
    diff = []
    try:
        diff = output.decode().split('\n')
    except:
        pass
    for line in diff:
        if line.startswith('@@ '):
            lines = get_line_number(line)
            old_line_range.append(lines.oldline)
            new_line_range.append(lines.newline)

    result = namedtuple('result', 'oldlines newlines')
    return result(old_line_range,new_line_range)

#if the target lines are changed in the given commit/diff
def is_overlap(target_line, diff):
    # the target lines are the same as the oldlines in text diff
    for l in diff.oldlines:
        if len(l) == 0:
            continue
        for lc in target_line:
            if l[0] <= lc <= l[1]:
                return True
    return False

#check if target lines are changed in the given commit; also, return its corresponding lines after this commit
def get_lines_after_commit(target_lines, diff):
    hunkcount = len(diff.oldlines)
    for i in range(hunkcount):
        oldline = diff.oldlines[i]
        newline = diff.newlines[i]
        # only new lines are added
        if len(oldline) == 0:
            target_lines = refresh_target_lines(newline[0], newline[1]-newline[0]+1, target_lines, 'case1')
        # only lines are deleted
        elif len(newline) == 0:
            target_lines = refresh_target_lines(oldline[0], oldline[1]-oldline[0]+1, target_lines, 'case2')
        else:
            # update the subsequent target lines (if oldrange = newrange, then no impact)
            oldrange = oldline[1] - oldline[0] + 1
            newrange = newline[1] - newline[0] + 1
            if newrange > oldrange:
                # equal to from old endline+1 add the differnce of the range
                target_lines = refresh_target_lines(newline[0] + oldrange, newrange - oldrange, target_lines, 'case1')
            elif oldrange > newrange:
                # equal to from new endline+1 delete the difference of the range
                target_lines = refresh_target_lines(newline[1]+1, oldrange - newrange, target_lines, 'case2')

    return target_lines

def refresh_target_lines(start, range, target_lines, case):
    new_target_lines = []
    if case == "case1":
        for l in target_lines:
            # all lines after increase by range
            if l >= start:
                new_target_lines.append(l+range)
            else:
                new_target_lines.append(l)
    if case == "case2":
        for l in target_lines:
            if l > start + range - 1:
                new_target_lines.append(l-range)
            elif l < start:
                new_target_lines.append(l)
            # if l inbetween, then it's simply deleted

    return new_target_lines

def flatten(lines):
    flat_lines = []
    for l in lines:
        if len(l) == 0:
            continue
        flat_lines = flat_lines + [i for i in range(l[0],l[1]+1)]
    return flat_lines

def write_to_file(file, content):
    f = open(file, 'w')
    f.write(content)
    f.close()


'''
Created on 2018/12/24

@author: shunsuke
'''
import unittest
from tests import utility

class Sample(object):

    def __init__(self, element1='abc', element2='123'):
        self.element1 = element1
        self.element2 = element2

class Test(unittest.TestCase):

    def setUp(self):
        print('setUp start.')
        utility.copy_test_dir_before_test()
        print('setUp finished.')

    def tearDown(self):
        print('tearDown start.')
        utility.remove_test_dir_after_test()
        print('tearDown finished.')

    def testName(self):
        print('Test Start.')
        sample1 = Sample()
        print(sample1.__dict__)
        sample2 = Sample('qaz', 'wsx')
        print(sample2.__dict__)
        print('Test Finished.')

if __name__ == "__main__":
    import sys;sys.argv = ['', 'Test.testName']
    unittest.main()


import os
import jax
import jax.numpy as jnp
import flax

# We use HuggingFace's tokenizer and input pipeline.
import transformers
import datasets
import numpy as np

# We use the Flax model.
import data
import modeling as flax_models
import training

os.environ['TOKENIZERS_PARALLELISM'] = 'true'

import ml_collections

import transformers

import import_weights
from flax import nn
from flax import optim

# from tensorflow.io import gfile

from transformers import BertTokenizerFast

import time

import functools

def get_config(init_checkpoint, dataset):
  hf_config = transformers.AutoConfig.from_pretrained(init_checkpoint)
  model_config = ml_collections.ConfigDict({
      'vocab_size': hf_config.vocab_size,
      'hidden_size': hf_config.hidden_size,
      'num_hidden_layers': hf_config.num_hidden_layers,
      'num_attention_heads': hf_config.num_attention_heads,
      'hidden_act':  hf_config.hidden_act,
      'intermediate_size': hf_config.intermediate_size,
      'hidden_dropout_prob': hf_config.hidden_dropout_prob,
      'attention_probs_dropout_prob': hf_config.attention_probs_dropout_prob,
      'max_position_embeddings': hf_config.max_position_embeddings,
      'type_vocab_size': hf_config.type_vocab_size,
    })
  is_regression_task = (
    dataset['train'].features['label'].dtype == 'float32')
  if is_regression_task:
    num_classes = 1
  else:
    num_classes = dataset['train'].features['label'].num_classes
  config = ml_collections.ConfigDict({
      'init_checkpoint': init_checkpoint,
      'tokenizer': init_checkpoint,
      'model': model_config,
      'warmup_proportion': 0.1,
      'max_seq_length': 128,
      'num_classes': num_classes
  })
  return config

def import_pretrained_params(config):
  return import_weights.load_params(
      init_checkpoint=config.init_checkpoint,
      hidden_size=config.model.hidden_size,
      num_attention_heads=config.model.num_attention_heads,
      num_classes=config.num_classes)

def create_model(config, initial_params):
  model_kwargs = dict(
      config=config.model,
      n_classes=config.num_classes,
  )
  model_def = flax_models.BertForSequenceClassification.partial(**model_kwargs)
  model = nn.Model(model_def, initial_params)
  return model

def create_optimizer(config, model, initial_params):
  """Create a model, starting with a pre-trained checkpoint."""
  common_kwargs = dict(
    learning_rate=config.learning_rate,
    beta1=0.9,
    beta2=0.999,
    eps=1e-6,
  )
  optimizer_decay_def = optim.Adam(
    weight_decay=0.01, **common_kwargs)
  optimizer_no_decay_def = optim.Adam(
    weight_decay=0.0, **common_kwargs)
  decay = optim.ModelParamTraversal(lambda path, _: 'bias' not in path)
  no_decay = optim.ModelParamTraversal(lambda path, _: 'bias' in path)
  optimizer_def = optim.MultiOptimizer(
    (decay, optimizer_decay_def), (no_decay, optimizer_no_decay_def))
  # TODO(marcvanzee): MultiOptimizer triggers double XLA compilation on TPU so
  # we use Adam here, but we should investigate why this happens.
  optimizer_def = optim.Adam(learning_rate=config.learning_rate)
  optimizer = optimizer_def.create(model)
  optimizer = optimizer.replicate()
  del model  # don't keep a copy of the initial model
  return optimizer

def get_num_train_steps(config, data_pipeline):
  num_train_examples = len(data_pipeline.dataset['train'])
  print(f'Num train examples: {num_train_examples}')
  num_train_steps = int(
      num_train_examples * config.num_train_epochs // config.train_batch_size)
  return num_train_steps

def get_learning_rate_fn(config, num_train_steps):
  warmup_steps = int(config.warmup_proportion * num_train_steps)
  cooldown_steps = num_train_steps - warmup_steps
  learning_rate_fn = training.create_learning_rate_scheduler(
      factors='constant * linear_warmup * linear_decay',
      base_learning_rate=config.learning_rate,
      warmup_steps=warmup_steps,
      steps_per_cycle=cooldown_steps,
  )
  return learning_rate_fn

def compute_loss_and_metrics(model, batch, rng):
  """Compute cross-entropy loss for classification tasks."""
  with nn.stochastic(rng):
    metrics = model(
        batch['input_ids'],
        (batch['input_ids'] > 0).astype(np.int32),
        batch['token_type_ids'],
        batch['label'])
  return metrics['loss'], metrics

def compute_classification_stats(model, batch):
  with nn.stochastic(jax.random.PRNGKey(0)):
    y = model(
        batch['input_ids'],
        (batch['input_ids'] > 0).astype(np.int32),
        batch['token_type_ids'],
        deterministic=True)
  return {
      'idx': batch['idx'],
      'label': batch['label'],
      'prediction': y.argmax(-1)
  }

def get_validation_splits(dataset_name):
  if dataset_name == 'mnli':
    return ['validation_matched', 'validation_mismatched']
  else:
    return ['validation']

def get_prefix(split):
  return 'eval_mismatched' if split == 'validation_mismatched' else 'eval'


from django.urls import reverse

from django_webtest import WebTest

from ..models import HuntingOfSnarkGame
from .factories import HuntingOfSnarkCriteriaFactory
from .factories import HuntingOfSnarkGameFactory

class HuntingOfSnarkGameTest(WebTest):

    def setUp(self):
        HuntingOfSnarkCriteriaFactory.create_batch(50)
        self.game = HuntingOfSnarkGameFactory()

    def test_show_main_page_and_create_new_game(self):
        self.assertEqual(HuntingOfSnarkGame.objects.count(), 1)
        response = self.app.get(reverse('games:hunting_of_snark_create'), status=200)

        player_name = 'Pepito'
        total_points = 10

        form = response.forms['game_form']
        form['player_name'] = player_name
        form['total_points'] = total_points
        response = form.submit().follow()

        self.assertEqual(response.status_code, 200)
        self.assertEqual(HuntingOfSnarkGame.objects.count(), 2)

        game = HuntingOfSnarkGame.objects.filter(player_name=player_name).first()
        self.assertEqual(game.total_points, total_points)
        self.assertEqual(game.total_points, total_points)
        self.assertEqual(game.criteria.count(), total_points)

        self.assertEqual(response.context['game'], game)
        self.assertContains(response, player_name)

    def test_show_criteria_list(self):
        self.app.get(
            reverse('games:hunting_of_snark_criteria_list'),
            status=200
        )

    def test_show_games_list(self):
        self.app.get(
            reverse('games:hunting_of_snark_games_list'),
            status=200
        )

    def test_show_bbc_top_100(self):
        self.app.get(
            reverse('games:hunting_of_snark_bbc_top_100'),
            status=200
        )

    def test_show_bbc_top_100(self):
        self.app.get(
            reverse('games:hunting_of_snark_detail', args=[self.game.token]),
            status=200
        )


from pytz import all_timezones

TIMEZONE_CHOICES = [(tz, tz) for tz in all_timezones]
PUBLIC_DOMAIN_GROUP = "public"

# anonymous system user
PONTIFEX_SYSTEM_USER = "system"

# support email
SUPPORT_EMAIL = "team@pontifexproject.com"

USER_BLACKLIST = ["system"]


# -*- coding: utf-8 -*-
from nose import tools as nt

from tests.base import AdminTestCase
from admin.rdm_announcement.forms import PreviewForm
from random import Random

data = dict(
    title='test title',
    body='test body',
    announcement_type='Email',
)

class TestPreviewForm(AdminTestCase):

    def test_clean_from_email_okay(self):
        mod_data = dict(data)
        email_body = self.random_body(10000)
        mod_data.update({'body': email_body, })
        form = PreviewForm(data=mod_data)
        self.assertTrue(form.is_valid())

    def test_clean_from_twitter_okay(self):
        mod_data = dict(data)
        twitter_body = self.random_body(140)
        mod_data.update({'body': twitter_body, 'announcement_type': 'SNS (Twitter)'})
        form = PreviewForm(data=mod_data)
        self.assertTrue(form.is_valid())

    def test_clean_from_twitter_raise(self):
        mod_data = dict(data)
        twitter_body = self.random_body(141)
        mod_data.update({'body': twitter_body, 'announcement_type': 'SNS (Twitter)'})
        form = PreviewForm(data=mod_data)
        nt.assert_false(form.is_valid())
        nt.assert_in('Body should be at most 140 characters', form.errors['__all__'])

    '''
    def test_clean_from_push_okay(self):
        mod_data = dict(data)
        twitter_body = self.random_body(2000)
        mod_data.update({'body': twitter_body, 'announcement_type': 'Push notification'})
        form = PreviewForm(data=mod_data)
        self.assertTrue(form.is_valid())

    def test_clean_from_push_raise(self):
        mod_data = dict(data)
        push_body = self.random_body(2001)
        mod_data.update({'body': push_body, 'announcement_type': 'Push notification'})
        form = PreviewForm(data=mod_data)
        nt.assert_false(form.is_valid())
        nt.assert_in('Body should be at most 2000 characters', form.errors['__all__'])
    '''

    ''' disable #GRDM-6902
    def test_clean_from_facebook_okay(self):
        mod_data = dict(data)
        facebook_body = self.random_body(10000)
        mod_data.update({'body': facebook_body, 'announcement_type': 'SNS (Facebook)'})
        form = PreviewForm(data=mod_data)
        self.assertTrue(form.is_valid())
    '''
    def random_body(self, count):
        body = ''
        chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789-_'
        length = len(chars) - 1
        random = Random()
        for i in range(0, count):
            body += chars[random.randint(0, length)]
        return body


import tensorflow as tf
from tensorflow import constant as const
from colored import stylize, fg
from math import sqrt
import numpy as np
import capslayer as cl
lookup = tf.nn.embedding_lookup

def lstm(inputs, sequence_length, hidden_size, scope):
    cell_fw = tf.nn.rnn_cell.LSTMCell(hidden_size // 2, initializer=tf.contrib.layers.xavier_initializer())
    cell_bw = tf.nn.rnn_cell.LSTMCell(hidden_size // 2, initializer=tf.contrib.layers.xavier_initializer())
    outputs, state = tf.nn.bidirectional_dynamic_rnn(
        cell_fw=cell_fw, cell_bw=cell_bw, inputs=inputs,
        sequence_length=sequence_length, dtype=tf.float32, scope=scope)
    outputs = tf.concat(outputs, axis=2)
    return outputs, state

def var(name, shape, initializer):
    return tf.get_variable(name, shape=shape, initializer=initializer)

class LSTM(object):

    def __init__(self, args):
        self.max_doc_len = args['max_doc_len']
        self.max_sen_len = args['max_sen_len']
        self.cls_cnt = args['cls_cnt']
        self.embedding = args['embedding']
        self.emb_dim = args['emb_dim']
        self.hidden_size = args['hidden_size']
        self.B = args['hidden_size']
        self.C = args['hidden_size']
        self.D = args['hidden_size']
        self.E = args['cls_cnt']
        self.usr_cnt = args['usr_cnt']
        self.prd_cnt = args['prd_cnt']
        self.sen_hop_cnt = args['sen_hop_cnt']
        self.doc_hop_cnt = args['doc_hop_cnt']
        self.l2_rate = args['l2_rate']
        self.aspect_cnt = 5
        # self.convert_flag = args['convert_flag']
        # self.debug = args['debug']
        # self.lambda1 = args['lambda1']
        # self.lambda2 = args['lambda2']
        # self.lambda3 = args['lambda3']

        self.best_dev_acc = .0
        self.best_test_acc = .0
        self.best_test_rmse = .0

        # initializers for parameters
        self.weights_initializer = tf.contrib.layers.xavier_initializer()
        self.biases_initializer = tf.initializers.zeros()
        self.emb_initializer = tf.contrib.layers.xavier_initializer()
        # self.weights_initializer = tf.initializers.random_uniform(-.01, .01)
        # self.biases_initializer = tf.initializers.random_uniform(-.01, .01)
        # self.emb_initializer = tf.initializers.random_uniform(-.01, .01)

        hsize = self.hidden_size

        # embeddings in the model
        with tf.variable_scope('emb'):
            self.embeddings = {
                'wrd_emb': const(self.embedding, name='wrd_emb', dtype=tf.float32),
                # 'wrd_emb': tf.Variable(embedding, name='wrd_emb', dtype=tf.float32),
                'usr_emb': var('usr_emb', [self.usr_cnt, hsize], self.emb_initializer),
                'prd_emb': var('prd_emb', [self.prd_cnt, hsize], self.emb_initializer)
            }

        # for tensorboard
        # if self.debug:
        #     tf.summary.histogram('usr_emb', self.embeddings['usr_emb'])
        #     tf.summary.histogram('prd_emb', self.embeddings['prd_emb'])

    def get_weight(self, name, shape):
        return var(name, shape, self.weights_initializer)

    def get_bias(self, name, shape):
        return var(name, shape, self.biases_initializer)

    def lstm(self, x, max_sen_len, max_doc_len, sen_len, doc_len, usr, prd):
        # spilt each batch into max_doc_len ones
        sen_x = tf.reshape(x, [-1, max_sen_len, self.emb_dim], name='sentence_x')
        sen_len = tf.reshape(sen_len, [-1], name='sen_len')

        d = self.hidden_size
        # lstm_output should be tensor with shape
        # [batch_size*max_doc_len, max_sen_len, hidden_size]
        # here a real batch is considered as max_doc_len batches
        lstm_output, _state = lstm(sen_x, sen_len, self.hidden_size, 'embedding_to_lstm')
        lstm_output = tf.reshape(lstm_output, [-1, max_sen_len, self.hidden_size, 1],
                                 name='lstm_output')

        # caps_sen should be tensor with shape
        # [batch_size*max_doc_len, aspect_cnt, d, 1]
        # here a real batch is considered as max_doc_len batches
        activation = tf.zeros([1, max_sen_len])
        caps_sen, _ = cl.layers.dense(inputs=lstm_output, activation=activation,
                                      num_outputs=self.aspect_cnt, out_caps_dims=[d, 1],
                                      sequence_length=sen_len, max_length=max_sen_len,
                                      share=True,
                                      routing_method='DynamicRouting', name='lstm_to_caps')
        caps_sen = tf.reshape(caps_sen, [-1, max_doc_len * self.aspect_cnt, d], name='caps_sen')

        # lstm_output should be tensor with shape
        # [batch_size, max_doc_len, d]
        # here a real batch is considered as max_doc_len batches
        caps_sen = tf.reshape(caps_sen, [-1, max_doc_len, self.aspect_cnt * d], name='caps_sen_for_lstm')
        lstm_output, _ = lstm(caps_sen, doc_len, d, 'lstm2')
        # [batch_size, max_doc_len, d, 1]
        lstm_output = tf.reshape(lstm_output, [-1, max_doc_len, d, 1])

        # caps_doc should be tensor with shape
        # [batch_size, aspect_cnt, d, 1]
        # here a real batch is considered as max_doc_len batches
        activation = tf.zeros([1, max_doc_len * self.aspect_cnt])
        caps_doc, _ = cl.layers.dense(inputs=lstm_output, activation=activation,
                                      num_outputs=self.aspect_cnt, out_caps_dims=[d, 1],
                                      sequence_length=doc_len,
                                      share=True,
                                      max_length=max_doc_len,
                                      routing_method='DynamicRouting', name='sen_to_doc')

        caps_doc = tf.layers.flatten(caps_doc)
        outputs = tf.layers.dense(caps_doc, self.cls_cnt)

        return outputs

    def build(self, data_iter):
        # get the inputs
        with tf.variable_scope('inputs'):
            input_map = data_iter.get_next()
            usrid, prdid, input_x, input_y, sen_len, doc_len = \
                (input_map['usr'], input_map['prd'],
                 input_map['content'], input_map['rating'],
                 input_map['sen_len'], input_map['doc_len'])

            usr = lookup(self.embeddings['usr_emb'], usrid, name='cur_usr_embedding')
            prd = lookup(self.embeddings['prd_emb'], prdid, name='cur_prd_embedding')
            input_x = lookup(self.embeddings['wrd_emb'], input_x, name='cur_wrd_embedding')

        # build the process of model
        d_hat = self.lstm(input_x, self.max_sen_len, self.max_doc_len,
                          sen_len, doc_len, usr, prd)
        prediction = tf.argmax(d_hat, 1, name='prediction')
        # predictionu = tf.argmax(d_hatu, 1, name='predictionu')
        # predictionp = tf.argmax(d_hatp, 1, name='predictionp')

        with tf.variable_scope("loss"):
            sce = tf.nn.softmax_cross_entropy_with_logits_v2
            self.loss = sce(logits=d_hat, labels=tf.one_hot(input_y, self.cls_cnt))
            # y = tf.one_hot(input_y, self.cls_cnt)
            # sign = 2 * y - 1
            # M = .8 * y + .1
            # # lam = .5 + .5 * y
            # lam = 1
            # loss = sign * (M - d_hat)
            # self.loss = lam * (tf.cast(loss > 0, tf.float32) * loss) ** 2

            regularizer = tf.zeros(1)
            params = tf.trainable_variables()
            for param in params:
                if param not in self.embeddings.values():
                    regularizer += tf.nn.l2_loss(param)
            self.loss = tf.reduce_mean(self.loss) + self.l2_rate * regularizer

        with tf.variable_scope("metrics"):
            correct_prediction = tf.equal(prediction, input_y)
            mse = tf.reduce_sum(tf.square(prediction - input_y), name="mse")
            correct_num = tf.reduce_sum(tf.cast(correct_prediction, dtype=tf.int32), name="correct_num")
            accuracy = tf.reduce_sum(tf.cast(correct_prediction, "float"), name="accuracy")

        return self.loss, mse, correct_num, accuracy

    def output_metrics(self, metrics, data_length):
        loss, mse, correct_num, accuracy = metrics
        info = 'Loss = %.3f, RMSE = %.3f, Acc = %.3f' % \
            (loss / data_length, sqrt(float(mse) / data_length), float(correct_num) / data_length)
        return info

    def record_metrics(self, dev_metrics, test_metrics, devlen, testlen):
        _dev_loss, dev_mse, dev_correct_num, dev_accuracy = dev_metrics
        _test_loss, test_mse, test_correct_num, test_accuracy = test_metrics
        dev_accuracy = float(dev_correct_num) / devlen
        test_accuracy = float(test_correct_num) / testlen
        test_rmse = sqrt(float(test_mse) / testlen)
        if dev_accuracy > self.best_dev_acc:
            self.best_dev_acc = dev_accuracy
            self.best_test_acc = test_accuracy
            self.best_test_rmse = test_rmse
            info = 'NEW best dev acc: %.3f, NEW best test acc: %.3f, NEW best test RMSE: %.3f' % \
                (self.best_dev_acc, self.best_test_acc, self.best_test_rmse)
        else:
            info = 'best dev acc: %.3f, best test acc: %.3f, best test RMSE: %.3f' % \
                (self.best_dev_acc, self.best_test_acc, self.best_test_rmse)
        return info

    def train(self, optimizer, global_step):
        grads_and_vars = optimizer.compute_gradients(self.loss)

        train_op = optimizer.apply_gradients(grads_and_vars, global_step=global_step)
        return train_op


import cv2
import numpy as np
import os
import glob

import pandas as pd
import random
import matplotlib.pyplot as plt

'''
Code picked up from the
http://www.pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/
'''

def bb_intersection_over_union(boxA, boxB):
    # determine the (x, y)-coordinates of the intersection rectangle
    xA = max(boxA[0], boxB[0])
    yA = max(boxA[1], boxB[1])
    xB = min(boxA[2], boxB[2])
    yB = min(boxA[3], boxB[3])

    # compute the area of intersection rectangle
    interArea = (xB - xA + 1) * (yB - yA + 1)

    # compute the area of both the prediction and ground-truth
    # rectangles
    boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
    boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)

    # compute the intersection over union by taking the intersection
    # area and dividing it by the sum of prediction + ground-truth
    # areas - the interesection area
    iou = interArea / float(boxAArea + boxBArea - interArea)

    # return the intersection over union value
    return iou

'''
Calculate the Tau for a given video and ten values
bins = 20
bin size = 1/20

'''
def calculate_t(truth, prediction):
    iou_list = []
    for original,predicted in zip(truth,prediction):
        iou = bb_intersection_over_union(original,predicted)
        iou_list.append(iou)
    bins = np.linspace(0,1,20)
    freq, bins = np.histogram(iou_list,bins)
    print(freq)

feature_params = dict( maxCorners = 100,
                       qualityLevel = 0.3,
                       minDistance = 7,
                       blockSize = 7 )

# Parameters for lucas kanade optical flow
lk_params = dict( winSize  = (15,15),
                  maxLevel = 2,
                  criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

"""
Read the ground truth.

"""

file = open('3/groundtruth_rect.txt', 'r')
# print(file.readline())
labels = file.readlines()
label = []
for l in labels:
    l = l.split(",")
    x = int(l[0])
    y = int(l[1])
    width = int(l[2])
    height = int(l[3])
    label.append([x,y,x+width,y+height])

"""
Read the images
"""

images = []
for filename in glob.iglob('3/**/*.jpg' , recursive = True):
    img = cv2.imread(filename)
    images.append(img)

"""
Take the images in sequence

"""

old_frame = images[0]
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
# p0 = cv2.goodFeaturesToTrack(old_gray, mask = None, **feature_params)
p0 = cv2.cornerHarris(old_gray,blockSize = 1, ksize = 3, k = 0.02)

mask = np.zeros_like(old_frame)
X1 = label[0][0]
Y1 = label[0][1]
X2 = label[0][2]
Y2 = label[0][3]
pt1 = (X1,Y1)
pt2 = (X2,Y2)

(a,b) = np.where(p0)

p0 = np.stack((a, b), axis=-1)
print(type(p0))

p0 = p0.reshape(-1,1,2)
p0 =  np.array(p0)
p0 = p0.astype(np.float32)
prediction = []

for img in images:
    frame_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)

    # Compute the previous and new points
    good_new = p1[st==1]
    good_old = p0[st==1]

    # Application of affine transformation
    gn = np.float32(good_new)[0:3]
    go = np.float32(good_old)[0:3]

    M = cv2.getAffineTransform(go,gn)

    pt1 = np.concatenate((pt1,[1]))
    pt2 = np.concatenate((pt2,[1]))
    new_pt1 = M.dot(pt1)
    new_pt2 = M.dot(pt2)

    img = cv2.rectangle(img,(np.int32(new_pt1[0]),np.int32(new_pt1[1])),(np.int32(new_pt2[0]),np.int32(new_pt2[1])),(0,255,0),50)
    # cv2.imshow('frame',img)
    # cv2.waitKey(0)
    k=0
    if k == 27:
        break
    old_gray = frame_gray.copy()
    p0 = good_new.reshape(-1,1,2)
    pt1 = new_pt1
    pt2 = new_pt2
    prediction.append(np.concatenate((new_pt1,new_pt2)))

cv2.destroyAllWindows()

calculate_t(label,prediction)


from django.urls import path
from rest_framework import routers

from .views import ManuscriptViewSet, ImagesViewSet, ImageInfoView, img_rotate

urlpatterns = [
    path('img-info/<int:pk>/', ImageInfoView.as_view(), name='manuscript-img-details'),
    path('img_rotate/<int:pk>/', img_rotate, name='img_rotate'),

]

# генерация url и их регистрация
router = routers.DefaultRouter()
# правый слэш на конце не ставится!
router.register('img', ImagesViewSet, basename='manuscript-img')
router.register('', ManuscriptViewSet, basename='manuscript-api')
urlpatterns += router.urls


import torch
import torch.nn

def variance(data,dim):
    centered = data - data.mean(dim).expand_as(data)
    return centered.pow(2).mean(dim)

class KL_sphere_loss(torch.nn.Module):
    def __init__(self,minimize,debug=0):
        super(KL_sphere_loss, self).__init__()
        self.minimize = minimize
        self.debug = debug

    def forward(self, samples):
        samples = samples.view(samples.size(0), -1)

        var = variance(samples,0)
        #var = samples.var(0)
        var += 1e-6
        mean = samples.mean(0)

        t1 = var + mean.pow(2)
        t2 = -var.log()
        KL = (t1+t2-1).mean()/2
        if not self.minimize:
            KL *= -1
        if self.debug:
            print("mean mean: {:.3f}, mean var: {:.3f}, mean log var: {:.3f}".format(
              mean.mean().item(), var.mean().item(), var.log().mean().item()))

        return KL