import os
import hickle
import numpy as np
import cv2

target_size = (192, 224)

parent = '/export/public/ukita/prednet/kitti_data/'

filenames = ['X_train', 'X_val', 'X_test', 'sources_train', 'sources_val', 'sources_test']

for filename in filenames:
    tmp = hickle.load(parent + filename + '.hkl')

    if 'sources' in filename:
        tmp_converted = tmp
    else:
        tmp_converted = np.zeros((len(tmp), ) + target_size + (3, ), dtype=tmp.dtype)
        for i in range(len(tmp)):
            tmp_converted[i] = cv2.resize(tmp[i], (target_size[1], target_size[0]))

    hickle.dump(tmp_converted, parent + filename + str(target_size[0]) + 'x' +
                               str(target_size[1]) + '.hkl', mode='w')


from tqdm import tqdm
import sys
from pathlib import Path
import matplotlib.pyplot as plt
from vlib.image import draw_box_plt
from utils.train_utils import *

def test(cfgs):
    dcfg, mcfg, cfg = cfgs
    n_coop = cfg.TEST['n_coop'] if 'n_coop' in list(cfg.TEST.keys()) else 0
    com_range = cfg.TEST['com_range'] if 'com_range' in list(cfg.TEST.keys()) else 0

    test_dataloader = build_dataloader(dcfg, cfg, train=False)
    print(len(test_dataloader))
    # train on the GPU or on the CPU, if a GPU is not available
    device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
    # device = torch.device('cpu')

    model = build_model(mcfg, cfg, dcfg).to(device)

    log_path = Path(cfg.PATHS['run'])
    test_out_path = log_path / 'test_result_{:2d}m'.format(com_range)
    test_out_path.mkdir(exist_ok=True)
    # get metric
    from utils.eval import MetricAP
    metric = MetricAP(cfg.TEST, test_out_path, device='cuda', bev=cfg.TEST['bev'])
    thrs = cfg.TEST['ap_ious']

    if metric.has_test_detections:
        aps = [metric.cal_ap_all_point(IoU_thr=thr)[0] for thr in thrs]
        with open(test_out_path / 'thr{}_ncoop{}.txt'.format(cfg.TEST['score_threshold'], n_coop), 'w') as fh:
            for thr,  ap in zip(thrs, aps):
                fh.writelines('mAP@{}: {:.2f}\n'.format(thr, ap * 100))

    # load checkpoint
    model.load_state_dict(torch.load(str(log_path / 'epoch{:03}.pth'
                                         .format(cfg.TRAIN['total_epochs']))))
    # dir for save test images
    images_path = (test_out_path / 'images_{}_{}'.format(cfg.TEST['score_threshold'], n_coop))
    images_path.mkdir(exist_ok=True)
    load_data_to_device = load_data_to_gpu if device.type == 'cuda' else load_data_as_tensor
    model.eval()
    direcs = []
    i = 0
    with torch.no_grad():
        for batch_data in tqdm(test_dataloader):
            i += 1
            points = batch_data['points']
            points = points[points[:, 0] == 0, 1:3]
            boxes = batch_data['gt_boxes'][0]
            load_data_to_device(batch_data)

            # Forward pass
            batch_data = model(batch_data)
            predictions_dicts = model.post_processing(batch_data, cfg.TEST)
            pred_boxes = [pred_dict['box_lidar'] for pred_dict in predictions_dicts]
            scores = [pred_dict['scores'] for pred_dict in predictions_dicts]
            direcs.extend([boxes[:, -1] for boxes in pred_boxes])
            if i % 1 == 0:
                ax = plt.figure(figsize=(8, 8)).add_subplot(1, 1, 1)
                ax.set_aspect('equal', 'box')
                ax.set(xlim=(dcfg.pc_range[0], dcfg.pc_range[3]),
                       ylim=(dcfg.pc_range[1], dcfg.pc_range[4]))
                ax.plot(points[:, 0], points[:, 1], 'y.', markersize=0.3)
                ax = draw_box_plt(boxes, ax, color='green', linewidth_scale=2)
                ax = draw_box_plt(pred_boxes[0], ax, color='red')
                plt.xlabel('x')
                plt.ylabel('y')
                plt.savefig(str(images_path / '{}.png'.format(batch_data['frame'][0])))
                plt.close()

            metric.add_samples(batch_data['frame'], pred_boxes, batch_data['gt_boxes'], scores)

    metric.save_detections()
    aps = [metric.cal_ap_all_point(IoU_thr=thr)[0] for thr in thrs]
    with open(test_out_path / 'thr{}_ncoop{}.txt'.format(cfg.TEST['score_threshold'], n_coop), 'w') as fh:
        for thr, ap in zip(thrs, aps):
            fh.writelines('mAP@{}: {:.2f}\n'.format(thr, ap * 100))


from flask import Flask, render_template, request, jsonify, make_response
import csv
import json
import sqlite3
import data.statements as sql_statement
import data.students as students_dictionary

app = Flask(__name__)

def create_table():
    connection = sqlite3.connect('training.db')
    mycursor = connection.cursor()
    mycursor.execute(sql_statement.sqlite_create_student_table)
    connection.commit()
    connection.close()

def reset():
    try:
        connection = sqlite3.connect('training.db')
        mycursor = connection.cursor()
        mycursor.execute(sql_statement.sqlite_drop_student_table)
        connection.commit()
        connection.close()
    except Exception as e:
        print("Table does not exist: ", e)

def insert_students():
    try:
        list_of_students_for_sql_insert = []
        for student in students_dictionary.list_students:
            """
            As we have a NOT NULL Constraint on given_name and family_name
            we need to compensate for this in our application logic since
            the student data has missing values,
            or in some way not adhering to our database design
            """
            if student["family_name"] is None:
                student["family_name"] = "Unknown"
            tuple_object = (student["given_name"], student["family_name"])
            list_of_students_for_sql_insert.append(tuple_object)
        print(list_of_students_for_sql_insert)
        connection = sqlite3.connect('training.db')
        mycursor = connection.cursor()
        mycursor.executemany('INSERT INTO python_students (given_name,family_name) VALUES (?,?)', list_of_students_for_sql_insert)
        connection.commit()
        connection.close()
        return {
            "ok": True,
            "message": "Students successfully inserted into database"
        }
    except Exception as e:
        text = "Something went wrong: " + str(e)
        print(text)
        return {
            "ok": False,
            "message": text
        }

def get_all_students():
    result = []
    try:
        connection = sqlite3.connect('training.db')
        mycursor = connection.cursor()
        query_result = mycursor.execute('SELECT given_name, family_name FROM python_students ORDER BY given_name')
        for row in query_result.fetchall():
            dictionary_object = {
                "given_name": row[0],
                "family_name": row[1],
            }
            result.append(dictionary_object)
        connection.close()
    except Exception as e:
        print("Student table does not exist: ", e)
    return result

""" ROUTES """

@app.route('/')
def index():
    # TODO: Create table
    return render_template('base.html', student_list=get_all_students())

@app.route('/hello')
def hello_world():
    return 'Hello, World!'

@app.route('/create/stuff', methods=["POST"])
def create_stuff():
    request_data = request.get_json()
    action = request_data['action']
    result = {
        "ok": False,
        "action": action,
    }
    if action == "make a table for our students":
        create_table()
        result["ok"] = True
    elif action == "insert students":
        insert_response = insert_students()
        result["ok"] = insert_response["ok"]
        if not insert_response["ok"]:
            result["ok"] = False
        result["message"] = insert_response["message"]
    elif action == "delete everything":
        reset()
        result["ok"] = True
        result["message"] = "Ready to start over"
    return jsonify(result)

@app.route('/export/<export_type>', methods=["GET"])
def export(export_type):
    if not export_type:
        return "Try json or csv"
    result = get_all_students()
    if export_type == "json":
        with open('students.json', "w") as file:
            dump = json.dumps(result, indent=4)
            file.write(dump)
        return jsonify(result)
    if export_type == "csv":
        with open('students.csv', 'w', newline='') as csvfile:
            spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
            spamwriter.writerow(["First name", "Last name"])
            for student in result:
                spamwriter.writerow([student["given_name"], student["family_name"]])
        file = open('students.csv', 'r', newline='')
        return "<pre>" + file.read() + "</pre>"


import pygame
import time
import random

pygame.init()

crash_sound = pygame.mixer.Sound("crash.wav")

display_height=600
display_width=800
crash_count=0

game_display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('race3')

black = ( 0,0,0)
white = (255,255,255)

red = (255,0,0)
green = (0,200,0)

bright_red = (255,0,0)
bright_green = (0,255,0)

block_color = (53,115,255)

carwidth=389

clock = pygame.time.Clock()
crashed = False

roadimg= pygame.image.load('road1.png')
carimg= pygame.image.load('bhai1.png')
manimg= pygame.image.load('dead1.png')

pause = False

def car(x,y):
    game_display.blit(carimg, (x,y))
def text_objects(text,font):
    textsurface = font.render(text, True, red)
    return textsurface, textsurface.get_rect()
def message_display(text):
    largetext = pygame.font.Font('freesansbold.ttf',70)
    TextSurf, TextRect = text_objects(text, largetext)
    TextRect.center = ((display_width/2),(display_height/2))
    game_display.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

def crash():

    pygame.mixer.Sound.play(crash_sound)
    pygame.mixer.music.stop()

    largeText = pygame.font.SysFont("comicsansms",115)
    TextSurf, TextRect = text_objects("arey bhai bhai bhai", largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    game_display.blit(TextSurf, TextRect)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        button("Play Again",150,450,100,50,green,bright_green,game_loop)
        button("Quit",550,450,100,50,red,bright_red,quitgame)

        pygame.display.update()
        clock.tick(15)
def game_loop():
    x =(display_width * .45)
    y =(display_height * .3)
    y_change = 0
    x_change = 0
    game_exit = False

    while not game_exit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_exit = True

    ##        print(event)
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    y_change = -5
                elif event.key == pygame.K_DOWN:
                    y_change = 5
            if event.type ==pygame.KEYUP:
                if event.key == pygame.K_UP :
                    y_change=0
                if event.key == pygame.K_DOWN:
                    y_change=0
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5
            if event.type ==pygame.KEYUP:
                if event.key == pygame.K_LEFT :
                    x_change=0
                if event.key == pygame.K_RIGHT:
                    x_change=0

        x += x_change
        y += y_change
        game_display.blit(roadimg, (00,00))
        game_display.blit(manimg, (0,-40))
        car(x,y)
        if x<-30 or x >  .45*display_width+20 :
            crash()
        if y<-300 or y> .3*display_height+30:
            crash()

        pygame.display.update()
        clock.tick(60)

print('man image=',manimg.get_rect().size)
print('car image=',carimg.get_rect().size)
game_loop()

pygame.quit()
quit()


#
# Test function control submodel
#
import pybamm
import tests
import unittest

def external_circuit_function(variables):
    I = variables["Current [A]"]
    V = variables["Terminal voltage [V]"]
    liion_param = pybamm.LithiumIonParameters()

    return (
        V
        + I
        - pybamm.FunctionParameter(
            "Current plus voltage function",
            {"Time [s]": pybamm.t * liion_param.timescale},
        )
    )

class TestFunctionControl(unittest.TestCase):
    def test_public_functions(self):
        param = pybamm.LithiumIonParameters()
        submodel = pybamm.external_circuit.FunctionControl(
            param, external_circuit_function
        )
        variables = {"Terminal voltage [V]": pybamm.Scalar(0)}
        std_tests = tests.StandardSubModelTests(submodel, variables)
        std_tests.test_all()

if __name__ == "__main__":
    print("Add -v for more debug output")
    import sys

    if "-v" in sys.argv:
        debug = True
    pybamm.settings.debug_mode = True
    unittest.main()


import unittest
import pytest
import os
import pandas as pd
import math

from datetime import datetime, timedelta
from entity.constants import *
from controller.data.data import GQData

class TestData(unittest.TestCase):
    @pytest.mark.skipif(os.environ[ENV_TEST_LEVEL]
                        == TEST_LEVEL_UNIT, reason="unit test")
    def test_get_data_alpaca(self):
        self._test_datasource(GQData(), ["SPY", "UBER"], DATASOURCE_ALPACA)

    @pytest.mark.skipif(os.environ[ENV_TEST_LEVEL]
                        == TEST_LEVEL_UNIT, reason="unit test")
    def test_get_data_binance(self):
        self._test_datasource(
            GQData(), [
                "ETHBTC", "BTCUSDT"], DATASOURCE_BINANCE)

    def test_fill_nan(self):
        df1 = pd.DataFrame.from_dict({
            "date": [datetime(2019, 1, 1), datetime(2019, 1, 2), datetime(2019, 1, 3)],
            "value": [1, 2, 3],
        }).set_index("date")
        df2 = pd.DataFrame.from_dict({
            "date": [datetime(2019, 1, 1), datetime(2019, 1, 3)],
            "value": [1, 3],
        }).set_index("date")
        df1.index = pd.to_datetime(df1.index, unit='ms')
        df2.index = pd.to_datetime(df2.index, unit='ms')
        data_dict = {
            "s1": df1,
            "s2": df2,
        }
        data_dict_out = GQData._fill_nan(data_dict, fill_nan_method=None, remove_nan_rows=False)
        self.assertEqual(data_dict_out["s1"].shape, (3, 1))
        self.assertEqual(data_dict_out["s2"].shape, (3, 1))
        self.assertTrue(math.isnan(data_dict_out["s2"]["value"][1]))

        data_dict_out = GQData._fill_nan(data_dict, fill_nan_method=None, remove_nan_rows=True)
        self.assertEqual(data_dict_out["s1"].shape, (2, 1))
        self.assertEqual(data_dict_out["s2"].shape, (2, 1))

        data_dict_out = GQData._fill_nan(data_dict, fill_nan_method="ffill", remove_nan_rows=False)
        self.assertEqual(data_dict_out["s1"].shape, (3, 1))
        self.assertEqual(data_dict_out["s2"].shape, (3, 1))
        self.assertEqual(data_dict_out["s2"]["value"][1], 1)

    def _test_datasource(self, data, symbols, datasource,
                         start_datetime=None, end_datetime=None):
        if start_datetime is None or end_datetime is None:
            end_datetime = datetime.today()
            start_datetime = end_datetime - timedelta(weeks=1)
        # test load data remote
        data.clean_cache()
        df_dict = data.get_data(symbols=symbols,
                                freq=FREQ_DAY,
                                start_date=start_datetime,
                                end_date=end_datetime,
                                datasource=datasource,
                                use_cache=True,
                                dict_output=True)

        self._check_data(symbols, df_dict)

        # test load from cache
        df_dict = data.get_data(symbols=symbols,
                                freq=FREQ_DAY,
                                start_date=start_datetime,
                                end_date=end_datetime,
                                datasource=datasource,
                                use_cache=True,
                                dict_output=True)
        self._check_data(symbols, df_dict)

        for symbol in symbols:
            data_key = data.get_data_key(
                symbol, FREQ_DAY, start_datetime, end_datetime)
            self.assertTrue(data.check_data_key(data_key))
        data.clean_cache()

    def _check_data(self, symbols, df_dict):
        assert len(df_dict) == len(symbols)
        for symbol in symbols:
            assert symbol in df_dict
            assert df_dict[symbol].shape[0] >= 4
            assert df_dict[symbol].shape[0] <= 8
            assert df_dict[symbol].shape[1] == 7
            assert df_dict[symbol].shape[1] == 7
            self.assertListEqual(
                list(df_dict[symbol].columns),
                DATA_HISTORICAL_COLS)


import kivy

import os

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.config import Config
from kivy.uix.listview import ListItemButton, ListView
from kivy.adapters.listadapter import ListAdapter
import psutil

from kivy.uix.button import Button

class Folder(GridLayout):
    elements = 0
    folder = "c:\\"

    def __init__(self, **kwargs):
        kwargs['cols'] = 2
        super(Folder, self).__init__(**kwargs)

        self.show_list()

    def callback(self, obj):
        if obj.selection[0].text == '[..]':
            folder = ''

            i = 0
            for element in self.elements[0:-1]:
                if(i==0):
                    folder = folder + element
                else:
                    folder = folder + '/' + element
                i = i+1

            self.folder = folder

        else:
            self.folder = self.folder + '/' + obj.selection[0].text

        self.show_list()
    def changeDisk(self,obj):
        self.folder = obj.text
        self.show_list()

    def show_list(self):
        self.ids.folder_list.clear_widgets()
        self.ids.disk.clear_widgets()

        self.elements = self.folder.split("/")

        if len(self.elements)<=1:
            list = []
        else:
            list = ['[..]']

        list = list + os.listdir(self.folder)

        listA = ListAdapter(
            data=list,
            selection_mode='single',
            allow_empty_selection=False,
            cls=ListItemButton)

        lista = ListView(adapter=listA, size_hint=(.2, 1.0))

        listA.bind(on_selection_change=self.callback)

        dps = psutil.disk_partitions()
        for dp in dps:
            button = Button(text = dp.device)
            button.bind(on_press=self.changeDisk)

            self.ids.disk.add_widget(button)

        self.ids.folder_list.add_widget(lista)
class Program(App):
    title = 'Folder list'
    icon = 'icon.png'

    def build(self):
        return Folder()

Config.set('graphics', 'fullscreen', '0')

Program().run()


#from appium import webdriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
#from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.ui import WebDriverWait
import os
from time import sleep
from selenium.webdriver.chrome.options import Options

#chrome driver
driverLocation = ("C:\chrome\chromedriver.exe")
os.environ["webdriver.chrome.driver"] = driverLocation
opt = Options()
opt.add_argument("--disable-infobars")
opt.add_argument("start-maximized")
opt.add_argument("--disable-extensions")
# Pass the argument 1 to allow and 2 to block
opt.add_experimental_option("prefs", {
    "profile.default_content_setting_values.media_stream_mic": 1,
    "profile.default_content_setting_values.media_stream_camera": 1,
    "profile.default_content_setting_values.geolocation": 1,
    "profile.default_content_setting_values.notifications": 1
  })
driver = webdriver.Chrome(chrome_options=opt, executable_path=r'C:\chrome\chromedriver.exe')
#driver = webdriver.Chrome(driverLocation)
baseURL = "https://console.plivo.com/"
driver.maximize_window()
driver.implicitly_wait(3)

driver.get(baseURL)

# Andriod capabilities
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '8.0.0'
desired_caps['deviceName'] = 'SM-G885F'
desired_caps['fullReset'] = 'false'
desired_caps['noReset'] = 'true'
desired_caps["appPackage"] = "com.zoiper.android.zoiperbeta.app"


from flask import url_for

def test_alert(client):
    data = {
        "service_name": "chaos",
        "type": "inter",
        "message": "测试",
        "host": "10.0.0.1",
        "timestamp": 1537252076,
        "level": "error",
        "stack_info": "Error",
        "extra": {},
    }
    result = client.post(url_for("alertapi"), json=data).json
    assert result["code"] == 0


"""
    This spider is a OntarioJobs spider created on top of the ATSSpider
    scrapy crawl ontariojobs -a mining_job_id=9999 -a iteration=1 -a extract=1 -a url="http://www.ontariojobs.com/gosearch.cfm?language=English"

    sample job url:
        http://www.ontariojobs.com/gosearch.cfm?task=viewjob&postid=452155&language=English

    NOTE(ted) - we will probably not want to run this spider since the job quality is really low.
        1. Many job listings are to recruiting companies so the company is not accurate (e.g. regisandsmith)
        2. Some jobs do not have an apply url (e.g. Great Clips)
"""

from datetime import datetime
from re import compile

from scrapy.http import Request
from scrapy.selector import Selector
from urlparse import urlparse

from brightcorp.base.atsspiders import ATSSpider
from brightcorp.items import BrightcorpItemLoader

pattern = {
    'Ref_Number' : compile(r'postid=(\d+)'),
}

class OntarioJobs(ATSSpider):

    name = "ontariojobs"

    def parse(self, response):
        '''
            Iterating over the list of jobs available on a single page,
            All the nodes display the same pattern tags, So taking the in-depth node and
            moving on to parent node, then capturing the data.
        '''
        selector = Selector(response)
        domain_url = urlparse(response.url)

        # Set expected_job_count
        if not self.expected_job_count_set:
            expected_job_count = selector.xpath("//font[@face='geneva,arial']/p/b/text()").extract()
            if expected_job_count:
                self.expected_job_count = expected_job_count

        jobs = selector.xpath('//tr/td//b[contains(text(),"Title")]/../../../following-sibling::tr')
        for job in jobs:
            posting_date = job.xpath('./td[3]/font/text()').extract()[0]
            yield Request(
                callback=self.parse_job,
                meta = {
                    'posting_date': posting_date
                },
                url='%(scheme)s://%(domain)s/%(job_url)s' % {
                    'scheme': domain_url.scheme,
                    'domain': domain_url.netloc,
                    'job_url': job.xpath('./td[@valign="top"]//a/@href').extract()[0],
                },
            )

        # Using the next node and requesting for the next page and parsing the data.
        next_page_url = selector.xpath('//b/a[contains(text(), "next records")]/@href')
        if next_page_url:
            yield Request(
                callback=self.parse,
                url='%(scheme)s://%(domain)s/%(next_page_url)s' % {
                    'scheme': domain_url.scheme,
                    'domain': domain_url.netloc,
                    'next_page_url': next_page_url.extract()[0],
                },
            )

    def parse_job(self, response):
        '''
            Collecting all the information available for a single job.
        '''
        selector = Selector(response)
        loader = BrightcorpItemLoader(selector=selector)
        if 'posting_date' in response.meta:
                post_date = datetime.strptime(response.meta['posting_date'], '%m/%d/%y').strftime('%Y/%m/%d')
                loader.add_value('date', post_date)
        referencenumber = 'ontariojobs-%s' % pattern['Ref_Number'].search(response.url).group(1)

        jobcategory = selector.xpath('//tr/td[@valign="top"]//p/b[contains(text(), "Job Category")]/following-sibling::text()').extract()
        jobcategory = ', '.join(jobcategory)

        loader.add_xpath(
            'company',
            '//tr/td[@valign="top"]//p/b[contains(text(), "Job Posted by")]/following-sibling::text()'
        )
        loader.add_xpath(
            'description',
            '//tr/td[@valign="top"]//p'
        )
        loader.add_xpath(
            'jobtype',
            '//tr/td[@valign="top"]//p/b[contains(text(), "Job Type")]/following-sibling::text()'
        )
        loader.add_xpath(
            'location',
            '//tr/td[@valign="top"]//b[contains(text(),"Location")]/following-sibling::text()'
        )
        loader.add_xpath(
            'title',
            '//tr/td[@valign="top"]//b[contains(text(),"Position")]/following-sibling::text()'
        )
        loader.add_xpath('baseSalary',
                         '//p[contains(text(), "Salary:")]/text()',
                         lambda a: [x.replace('Salary:', '') for x in a]
        )

        loader.add_value('apply_url', response.url.replace('&language=English', ''))
        loader.add_value('url', response.url)
        loader.add_value('jobcategory', jobcategory)
        loader.add_value('referencenumber', referencenumber)

        yield loader.load_item()


import json
import pytest

from unittest import mock
from dcicutils import ecr_utils as ecr_utils_module
from dcicutils.ecr_utils import ECRUtils, ECRTagWatcher
from dcicutils.docker_utils import DockerUtils
from dcicutils.misc_utils import ignored, ignorable, filtered_warnings
from dcicutils.qa_utils import MockBoto3, MockBotoECR
from .helpers import using_fresh_cgap_state_for_testing

REPO_URL = '123456789.dkr.ecr.us-east-2.amazonaws.com/cgap-mastertest'  # dummy URL

def mocked_describe_respositories():
    """ Mocks the important info from the describe_repositories API call. """
    return {'repositories': [{
        'repositoryUri': REPO_URL,
       }]}

def mocked_ecr_login(*, username, password, registry):
    ignored(username, password, registry)
    return

@using_fresh_cgap_state_for_testing()
def test_ecr_utils_basic():
    """ Tests something simple for now, more tests to be added later. """
    # init args no longer default. -kmp 14-Jul-2022
    cli = ECRUtils(env_name='cgap-mastertest', local_repository='cgap-wsgi')
    with mock.patch.object(cli.ecr_client, 'describe_repositories', mocked_describe_respositories):
        url = cli.resolve_repository_uri()
        assert url == REPO_URL

@pytest.mark.skipif(not DockerUtils.docker_is_running(), reason="Docker is not running.")
@using_fresh_cgap_state_for_testing()
def test_ecr_utils_workflow():
    """ Tests URL + Login via Docker_cli"""
    # init args no longer default. -kmp 14-Jul-2022
    ecr_cli = ECRUtils(env_name='cgap-mastertest', local_repository='cgap-wsgi')
    docker_cli = DockerUtils()
    with mock.patch.object(ecr_cli.ecr_client, 'describe_repositories', mocked_describe_respositories):
        ecr_cli.resolve_repository_uri()
        ecr_user, ecr_pass = 'dummy', 'dummy'  # XXX: integrated test for this mechanism
        with mock.patch.object(docker_cli.client, 'login', mocked_ecr_login):
            ignorable(filtered_warnings)
            with filtered_warnings("ignore", category=DeprecationWarning):
                # e.g., as of docker 4.4.4, we see in site-packages/docker/utils/utils.py, lines 59-60:
                #  DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
                #    s1 = StrictVersion(v1)
                #  DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
                #    s2 = StrictVersion(v2)
                docker_cli.login(ecr_repo_uri=ecr_cli.url,
                                 ecr_user=ecr_user, ecr_pass=ecr_pass)
            # XXX: integrated test the remaining?

def test_ecr_utils_get_uri():
    print()  # start output on fresh line
    mock_boto3 = MockBoto3()
    with mock.patch.object(ecr_utils_module, "boto3", mock_boto3):
        ecr_client: MockBotoECR = mock_boto3.client('ecr')
        for repo in ['main', 'decoy-1', 'fourfront-foo', 'decoy-2']:
            print(f"Adding image repository"
                  f" {json.dumps(ecr_client.add_image_repository_for_testing(repo), indent=2, default=str)}")
        print("---- Setup complete. Beginning test. ----")
        ecr_utils = ECRUtils(env_name='fourfront-foo')
        assert ecr_utils.url is None
        with pytest.raises(Exception):
            ecr_utils.get_uri()  # This intentionally raises an error if there is no URL
        ecr_utils.resolve_repository_uri()
        print(f"ecr_utils.url = {ecr_utils.url}")
        assert ecr_utils.url is not None
        uri = ecr_utils.get_uri()
        print(f"ecr_utils.get_uri() = {uri}")
        assert isinstance(uri, str)
        assert uri.endswith('/fourfront-foo')

def test_ecr_utils_resolve_repository_uri():
    print()  # start output on fresh line
    mock_boto3 = MockBoto3()
    with mock.patch.object(ecr_utils_module, "boto3", mock_boto3):
        ecr_client: MockBotoECR = mock_boto3.client('ecr')
        for repo in ['main', 'decoy-1', 'fourfront-foo', 'decoy-2']:
            print(f"Adding image repository"
                  f" {json.dumps(ecr_client.add_image_repository_for_testing(repo), indent=2, default=str)}")
        print("---- Setup complete. Beginning test. ----")
        ecr_utils = ECRUtils(env_name='fourfront-foo')
        print(f"ecr_utils.url = {ecr_utils.url}")
        assert ecr_utils.url is None
        print("resolving repository URI")
        ecr_utils.resolve_repository_uri()
        print(f"ecr_utils.url = {ecr_utils.url}")
        assert ecr_utils.url is not None

def test_ecr_tag_watcher():
    print()  # start output on fresh line
    mock_boto3 = MockBoto3()
    with mock.patch.object(ecr_utils_module, "boto3", mock_boto3):
        ecr_client: MockBotoECR = mock_boto3.client('ecr')
        ecr_client.add_image_repository_for_testing('main')
        for i in range(5):
            last_from_loop = ecr_client.add_image_metadata_for_testing('main', tags=['latest'])
        watcher = ECRTagWatcher()
        first_found = watcher.get_current_image_digest()
        print(f"first_found = {first_found}")
        assert first_found == last_from_loop['imageDigest']
        to_deploy = watcher.check_for_new_image_to_deploy()
        assert not to_deploy
        print(f"to_deploy =   {to_deploy}")
        assert first_found == watcher.get_current_image_digest()
        print(f"Simulating deploy.")
        even_newer = ecr_client.add_image_metadata_for_testing('main', tags=['latest'])
        next_found = watcher.get_current_image_digest()
        print(f"next_found =  {next_found}")
        assert next_found != first_found
        assert next_found == even_newer['imageDigest']
        assert next_found == watcher.get_current_image_digest()  # It stays this until next deploy
        to_deploy = watcher.check_for_new_image_to_deploy()
        print(f"to_deploy =   {to_deploy}")
        assert to_deploy
        assert to_deploy == next_found
        to_deploy = watcher.check_for_new_image_to_deploy()
        print(f"to_deploy =   {to_deploy}")
        assert not to_deploy


from itertools import product
from random import shuffle
from types import MappingProxyType
from typing import Callable, Iterable, NoReturn, Tuple, Type
from uuid import uuid4, UUID

random_bytes: Callable[[int], bytes]
try:
    from secrets import token_bytes as random_bytes
except ImportError:
    from os import urandom as random_bytes

__all__ = ['BaseCharsEncodeError', 'BaseCharsDecodeError', 'BaseCharsProduct']

class BaseCharsEncodeError(ValueError):
    pass

class BaseCharsDecodeError(ValueError):
    pass

class BaseCharsProduct:

    __slots__: Tuple[str, ...] = ('chars', 'repeat', 'ivmap', 'vimap')

    _expected_chars_count: Tuple[int, ...] = (1 << 1, 1 << 2, 1 << 4, 1 << 8)
    _expected_chars_repeat: Tuple[int, ...] = (8, 4, 2, 1)

    def __init__(self, chars: str, do_shuffle: bool = False):
        cls: Type[BaseCharsProduct] = type(self)
        chars_count: int = len(chars)
        if not chars_count == len(set(chars)):
            raise ValueError(
                'Expected a string with different characters')
        repeat: int
        try:
            repeat = self._expected_chars_repeat[
                cls._expected_chars_count.index(chars_count)]
        except ValueError as e:
            raise ValueError(
                'Expected a string within %r different characters, get %d'
                % (cls._expected_chars_count, chars_count)) from e
        self.chars: str
        super().__setattr__('chars', chars)
        self.repeat: int
        super().__setattr__('repeat', repeat)
        prod: Iterable = product(chars, repeat=repeat)
        if do_shuffle:
            prod = list(prod)
            shuffle(prod)
        self.ivmap: MappingProxyType
        super().__setattr__('ivmap', MappingProxyType(
            {i: ''.join(l) for i, l in enumerate(prod)}))
        self.vimap: MappingProxyType
        super().__setattr__('vimap', MappingProxyType(
            {v: k for k, v in self.ivmap.items()}))

    def __setattr__(self, attr, val) -> NoReturn:
        raise TypeError('setting properties is not allowed')

    def encode(self, b: bytes) -> str:
        try:
            return ''.join(self.ivmap[i] for i in b)
        except Exception as e:
            raise BaseCharsEncodeError from e

    def decode(self, s: str) -> bytes:
        repeat: int = self.repeat
        try:
            return bytes(self.vimap[s[i:i+repeat]]
                         for i in range(0, len(s), repeat))
        except Exception as e:
            raise BaseCharsDecodeError from e

    def random(self, bytes_len: int = 1) -> Tuple[bytes, str]:
        rb: bytes = random_bytes(bytes_len)
        return rb, self.encode(rb)

    def random_uuid(self) -> Tuple[UUID, str]:
        uuid: UUID = uuid4()
        return uuid, self.encode(uuid.bytes)


import os
import subprocess
import xml.etree.ElementTree as ET

class XMLEditor:
    xml_template = "../../templates/resource_schema.xml"
    languages = ['', '.en']

    @staticmethod
    def create_resources(project_file, additional_path, entity_name, properties):
        print('Creating resource files for entity {0}'.format(entity_name))
        if len(properties) == 0:
            print('There is no properties to add into resources')
            return

        project_folder = os.path.dirname(project_file)
        target_folder = os.path.join(project_folder, 'Resources', additional_path, entity_name)
        target_file = os.path.join(target_folder, 'Resource.resx')
        if os.path.exists(target_file):
            print('Resource files for entity {0} already exist'.format(entity_name))
            return

        namespace = additional_path.replace('\\', '.')
        XMLEditor.create_resource_file(properties, target_folder)
        # XMLEditor.generate_strong_type_resource_classes(target_folder, namespace)
        XMLEditor.add_resources_to_project(project_file, additional_path, namespace, entity_name)
        print('Creating resource files was successfully')

    @staticmethod
    def create_resource_file(properties, target_folder):
        try:
            print('  Creating directory {0}...'.format(target_folder))
            os.makedirs(target_folder)
        except FileExistsError:
            print('    Directory {0} already exists'.format(target_folder))

        os.chdir(os.path.dirname(os.path.abspath(__file__)))
        tree = ET.ElementTree(file=XMLEditor.xml_template)
        root = tree.getroot()

        for prop in properties:
            data_item = ET.SubElement(root, "data", {
                "name": prop[0],
                "xml:space": "preserve"
            })
            value_item = ET.SubElement(data_item, "value")
            value_item.text = prop[1]
        XMLEditor._indent(root)

        tree = ET.ElementTree(root)
        resource1 = target_folder + "\\Resource.resx"
        with open(resource1, "wb") as f:
            print('  Creating file {0}...'.format(resource1))
            tree.write(f, encoding='utf-8')
        print('    File was created successfully')
        resource2 = target_folder + "\\Resource.en.resx"
        with open(resource2, "wb") as f:
            print('  Creating file {0}...'.format(resource2))
            tree.write(f, encoding='utf-8')
        print('    File was created successfully')

    @staticmethod
    def generate_strong_type_resource_classes(target_folder, namespace):
        input_file = os.path.join(target_folder, 'Resource.resx')
        for lang in XMLEditor.languages:
            output_file = os.path.join(target_folder, 'Resource{0}.Designer.cs'.format(lang))
            print('  Generating strong typed resource file {0}...'.format(output_file))
            if lang == '':
                subprocess.check_output([
                    'resgen',
                    input_file,
                    '/str:C#,{0},Resource,{1}'.format(namespace, output_file),
                    '/publicClass'],
                    shell=True
                )
            else:
                open(output_file, 'w').close()
        print('  Remove extra file {0}\\Resource.resources...'.format(target_folder))
        os.remove(os.path.join(target_folder, 'Resource.resources'))
        print('    File was removed successfully')
        pass

    @staticmethod
    def add_resources_to_project(project_file, additional_path, resource_namespace, entity_name):
        print('  Start to add resources to project file {0}'.format(project_file))
        print('    Reading file {0}...'.format(project_file))
        tree = ET.ElementTree(file=project_file)
        root = tree.getroot()
        ET.register_namespace('', "http://schemas.microsoft.com/developer/msbuild/2003")

        namespace, tag_name = XMLEditor._tag_uri_and_name(root)
        iter_collection = root.iter("{0}{1}".format('{' + namespace + '}', 'ItemGroup'))

        # find Compile tags
        print('    Adding Compile attributes')
        compile_element_group = None
        stop_search = False
        for item_group in iter_collection:
            for compile_item in item_group:
                if compile_item.tag == "{0}{1}".format('{' + namespace + '}', 'Compile'):
                    compile_element_group = item_group
                    stop_search = True
                    break
            if stop_search:
                break

        # add new Compile tags
        for lang in XMLEditor.languages:
            embedded_res = ET.SubElement(compile_element_group, 'Compile', {
                "Include": "Resources\{0}\{1}\Resource{2}.Designer.cs"
                                 .format(additional_path, entity_name, lang)
            })
            depend_upon = ET.SubElement(embedded_res, 'DependentUpon')
            depend_upon.text = 'Resource{0}.resx'.format(lang)
            auto_gen = ET.SubElement(embedded_res, 'AutoGen')
            auto_gen.text = 'True'
            design_time = ET.SubElement(embedded_res, 'DesignTime')
            design_time.text = 'True'
        XMLEditor._indent(compile_element_group)  # pretty print

        # find EmbeddedResource tags
        print('    Adding EmbeddedResource attributes')
        embedded_resources_group = None
        stop_search = False
        for item_group in iter_collection:
            for embedded_resource in item_group:
                if embedded_resource.tag == "{0}{1}".format('{' + namespace + '}', 'EmbeddedResource'):
                    embedded_resources_group = item_group
                    stop_search = True
                    break
            if stop_search:
                break

        # add new EmbeddedResource tags
        for lang in XMLEditor.languages:
            embedded_res = ET.SubElement(embedded_resources_group, 'EmbeddedResource', {
                "Include": "Resources\{0}\{1}\Resource{2}.resx"
                                 .format(additional_path, entity_name, lang)
            })
            generator = ET.SubElement(embedded_res, 'Generator')
            generator.text = 'PublicResXFileCodeGenerator'
            las_gen_output = ET.SubElement(embedded_res, 'LastGenOutput')
            las_gen_output.text = 'Resource{0}.Designer.cs'.format(lang)
            custom_namespace = ET.SubElement(embedded_res, 'CustomToolNamespace')
            custom_namespace.text = resource_namespace
        XMLEditor._indent(embedded_resources_group)  # pretty print

        tree = ET.ElementTree(root)
        with open(project_file, "wb") as f:
            print('    Saving changes...')
            tree.write(f, xml_declaration=True, encoding='utf-8')
        print('      Successfully saved')

    @staticmethod
    def _tag_uri_and_name(elem):
        if elem.tag[0] == "{":
            uri, ignore, tag = elem.tag[1:].partition("}")
        else:
            uri = None
            tag = elem.tag
        return uri, tag

    '''
    copy and paste from http://effbot.org/zone/element-lib.htm#prettyprint
    it basically walks your tree and adds spaces and newlines so the tree is
    printed in a nice way
    '''
    @staticmethod
    def _indent(elem, level=0):
        i = "\n" + level * "  "
        if len(elem):
            if not elem.text or not elem.text.strip():
                elem.text = i + "  "
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
            for elem in elem:
                XMLEditor._indent(elem, level + 1)
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
        else:
            if level and (not elem.tail or not elem.tail.strip()):
                elem.tail = i


from django import template
from django.template.defaultfilters import stringfilter
import json

#custom filters
#https://docs.djangoproject.com/en/3.1/howto/custom-template-tags/#template-filters-that-expect-strings

register = template.Library()

@register.filter(name="disp")
@stringfilter
def getCategoryDisplayNames(value):

  DISPLAY_NAMES = {
    'MT2020M': "Master's Male SAC",
    'MT2020F': "Master's Female SAC",
    'MT2020M1': "Master's Male SAC 1",
    'MT2020M2': "Master's Male SAC 2",
    'MT2020F1': "Master's Female SAC 1",
    'MT2020F2': "Master's Female SAC 2",
    'IMT2019M1': "iMtech 2019 Male SAC",
    'IMT2019F1': "iMtech 2019 Female SAC",
    'IMT2018M1': "iMtech 2018 Male SAC",
    'IMT2018F1': "iMtech 2018 Female SAC",
    'IMT2019M': "iMtech 2019 Male SAC",
    'IMT2019F': "iMtech 2019 Female SAC",
    'IMT2018M': "iMtech 2018 Male SAC",
    'IMT2018F': "iMtech 2018 Female SAC",
    'imtech': "iMtech",
    'mtech': "Master's"
  }

  try:
    name = DISPLAY_NAMES[value]
  except:
    name = ''

  return name

@register.filter(name="disp_1")
@stringfilter
def getCategoryDisplayNames(value):

  DISPLAY_NAMES = {
    'MT2020M1': "Master's SAC Nominee",
    'MT2020M2': "Master's SAC Nominee",
    'MT2020F1': "Master's SAC Nominee",
    'MT2020F2': "Master's SAC Nominee",
    'IMT2019M1': "iMtech 2019 SAC Nominee",
    'IMT2019F1': "iMtech 2019 SAC Nominee",
    'IMT2018M1': "iMtech 2018 SAC Nominee",
    'IMT2018F1': "iMtech 2018 SAC Nominee",
    'F': 'Female',
    'M': 'Male'
  }

  try:
    name = DISPLAY_NAMES[value]
  except:
    name = ''

  return name

@register.filter(name="getslug")
@stringfilter
def getSlug(value):
  SLUG_VALUES = {
    'IMT': 'imtech',
    'MT': 'mtech',
    'DT': 'mtech',
    'MS': 'mtech',
    'PH': 'mtech',
  }

  try:
    slug = SLUG_VALUES[value]
  except:
    slug = ''

  return slug

  return SLUG_VALUES

@register.filter(name="gethash")
@stringfilter
def getCategoryDisplayNames(value):
  value = value
  batch, hashed = value.split("@#@")
  hashed = hashed.replace("\'","\"")
  hashed = json.loads(hashed)
  return hashed[batch]


import utils.image2coord as i2c

class DepthTool():
    def readCord(self, file):
        self.cord = [[]]
        f = open(file, "r")

        id = 0 #index of current row

        for line in f:
            self.cord.append([])
            depthData = line.split(' ')
            for data in depthData:
                if data != "\n":
                    try:
                        convertedData = float(data)
                    except:
                        print((data))
                        pass
                    self.cord[id].append(convertedData)
            id += 1
        #remove last empty row
        self.cord.remove([])

        # height and width of image
        self.height = len(self.cord)
        self.width = len(self.cord[0])
        # print(self.height)
        # print(self.width)

    def getDepthBoundingBox(self, boxPos, gridSize, bias):
        # position of point on grid
        self.gridX = []
        self.gridY = []
        # pos[] = arr[i]
        #get bounding box position
        xLT = int(boxPos[0])# x left top
        yLT = int(boxPos[1]) # y left top
        xRD = int(boxPos[2]) # x right down
        yRD = int(boxPos[3]) # y right down
        # for i in range(yLT, yRD):
        #     print(cord[i][xLT:xRD])
        # devide box by grid size
        dX = xRD - xLT
        dY = yRD - yLT
        stepX = int(dX / gridSize)
        stepY = int(dY / gridSize)

        #save position of each point on grid
        for y in range(yLT, yRD, stepY):
            self.gridY.append(y)
        for x in range(xLT, xRD, stepX):
            self.gridX.append(x)

        self.dimY = len(self.gridY)
        self.dimX = len(self.gridX)
        depth = self.getDepthUsingVoteMap(bias)
        return depth

    def getAverageDepthAroundChosenPoint(self, x, y, bias):
        y = self.gridY[y]
        x = self.gridX[x]
        sum = 0.0
        count = 0
        for thisY in range(self.dimY):
            for thisX in range(self.dimX):
                yy = self.gridY[thisY]
                xx = self.gridX[thisX]
                if abs(self.cord[y][x] - self.cord[yy][xx]) < bias and self.cord[yy][xx] > 0:
                    sum += self.cord[yy][xx]
                    count += 1
        return sum / count

    def getDepthUsingVoteMap(self, bias):
        #vote map
        # print(posX)
        # print(posY)
        voteMap = [[0 for i in range(self.dimX)] for i in range(self.dimY)]
        for thisY in range(self.dimY):
            for thisX in range(self.dimX):
                for otherY in range(self.dimY):
                    for otherX in range(self.dimX):
                        y = min(max(self.gridY[thisY], 0), 719)
                        x = min(max(self.gridX[thisX], 0), 1279)
                        y2 = min(max(self.gridY[otherY], 0), 719)
                        x2 = min(max(self.gridX[otherX], 0), 1279)
                        # print ("({0}, {1}) ({2}, {3})".format(x, y, x2, y2))
                        if abs(self.cord[y][x] - self.cord[y2][x2]) < bias:
                            voteMap[thisY][thisX] += 1

        # print(voteMap)
        FposX = 0
        FposY = 0
        cur = 0
        for thisY in range(self.dimY):
            for thisX in range(self.dimX):
                # print(cord[posY[y]][posX[x]])
                y = self.gridY[thisY]
                x = self.gridX[thisX]
                if voteMap[thisY][thisX] > cur and self.cord[y][x] > 0.0:
                    cur = voteMap[thisY][thisX]
                    FposX = thisX
                    FposY = thisY
        # print(self.gridX[FposX], self.gridY[FposY])
        # return self.cord[self.gridY[FposY]][self.gridX[FposX]]
        #depth = self.getAverageDepthAroundChosenPoint(FposY, FposX, bias)
        depth = self.cord[self.gridY[FposY]][self.gridX[FposX]]
        # print(self.cord[self.gridY[FposY]][self.gridX[FposX]])
        return depth

if __name__ == '__main__':
    DepthTool = DepthTool()
    boxes = [[383,121, 430, 156], [853, 149, 894, 187]]
    f = "1574444192325.txt"
    bias = 0.1
    gridSize = 10
    DepthTool.readCord(f)
    for box in boxes:
        y = box[3]
        x = (box[0] + box[2]) / 2
        depth = DepthTool.getDepthBoundingBox(box, gridSize, bias) * 100
        print(x, y, depth)
        i2c.to_coord(x, y)
        i2c.to_coord_from_depth(x, y, depth)


import re
from emot import emo_unicode
#import emo_unicode

'''emot library to detect emoji and emoticons.

    >>> import emot
    >>> text = "I love python 👨 :-)"
    >>> emot.emoji(text)
    >>> {'value': ['👨'], 'mean': [':man:'], 'location': [[14, 14]], 'flag': True}
    >>> emot.emoticons(text)
    >>> {'value': [':-)'], 'location': [[16, 19]], 'mean': ['Happy face smiley'], 'flag': True}
'''

__all__ = ['emoji','emoticons']

def emoji(string):
    '''emot.emoji is use to detect emoji from text

        >>> text = "I love python 👨 :-)"
        >>> emot.emoji(text)
        >>> {'value': ['👨'], 'mean': [':man:'], 'location': [[14, 14]], 'flag': True}
    '''
    __entities = {}
    __value = []
    __mean = []
    __location = []
    flag = True
    try:
        pro_string = str(string)
        for pos,ej in enumerate(pro_string):
            if ej in emo_unicode.UNICODE_EMO:
                try:
                    __value.append(ej)
                    __mean.append(emo_unicode.UNICODE_EMO[ej])
                    __location.append([pos,pos])
                except Exception as e:
                    flag = False
                    __entities.append({"flag": False})
                    return __entities

    except Exception as e:
        flag = False
        __entities.append({"flag": False})
        return __entities
    if len(__value) < 1:
        flag = False
    __entities = {
        'value' : __value,
        'mean' : __mean,
        'location' : __location,
        'flag' : flag
    }

    return __entities

def emoticons(string):
    '''emot.emoticons is use to detect emoticons from text

        >>> text = "I love python 👨 :-)"
        >>> emot.emoticons(text)
        >>> {'value': [':-)'], 'location': [[16, 19]], 'mean': ['Happy face smiley'], 'flag': True}
    '''
    __entities = []
    flag = True
    try:
        pattern = u'(' + u'|'.join(k for k in emo_unicode.EMOTICONS) + u')'
        __entities = []
        __value = []
        __location = []
        matches = re.finditer(r"%s"%pattern,str(string),re.IGNORECASE)
        for et in matches:
            __value.append(et.group().strip())
            __location.append([et.start(),et.end()])

        __mean = []
        for each in __value:
            __mean.append(emo_unicode.EMOTICONS_EMO[each])

        if len(__value) < 1:
            flag = False
        __entities = {
        'value' : __value,
        'location' : __location,
        'mean' : __mean,
        'flag' : flag
        }
    except Exception as e:
        __entities = [{'flag' : False}]
        #print("No emoiticons found")
        return __entities

    return __entities

def test_emo():
    test = "I love python 👨 :-)"
    print(emoji(test))
    print(emoticons(test))
    return None

def about():
    text = "emot library: emoji and emoticons library for python. It return emoji or emoticons from string with location of it. \nAuthors: \n Neel Shah: neelknightme@gmail.com or https://github.com/NeelShah18 \n Subham Rohilla: kaka.shubham@gmail.com or https://github.com/kakashubham"
    print(text)
    return None

if __name__ == '__main__':
    test_emo()


import cv2
import numpy as np
import SimpleITK as sitk
from myshow import myshow, myshow3d

def getPos(img):
    def draw_circle(event, x, y, flags, param):
        global mouseX, mouseY
        if event == cv2.EVENT_LBUTTONDBLCLK:
            cv2.circle(img, (x, y), 5, (255, 255, 255), 1)
            mouseX, mouseY = x, y

    cv2.namedWindow('image')
    cv2.setMouseCallback('image', draw_circle)

    while 1:
        cv2.imshow('image', img)
        k = cv2.waitKey(20) & 0xFF
        if k == 27:  # click on escape to escape, genius
            break
        elif k == ord('a'):  # click on 'a' to get value
            try:
                print(mouseX, mouseY)
            except NameError:
                print('click first dumbass')

    cv2.destroyAllWindows()
    return mouseX, mouseY, img[mouseX, mouseY]

def getQ(imageFile, r = 2, multi = 7.5):
    ##############################################################################
    # Load Images
    # -----------
    # r = 2 # resampling factor
    resSlope = 1 #from dcm file
    reIntercep = 0 #from dcm file
    # u = slope * signal + intercept
    img_T1 = sitk.ReadImage(imageFile)
    img_T1 = sitk.Expand(img_T1, [r,r,1] * 3, sitk.sitkLinear)
    imArray = sitk.GetArrayFromImage(img_T1)
    imArray = imArray[0, :, :]
    # To visualize the labels image in RGB needs a image with 0-255 range
    img_T1_255 = sitk.Cast(sitk.RescaleIntensity(img_T1), sitk.sitkUInt8)

    ny, nx = img_T1_255.GetSize()[0], img_T1_255.GetSize()[1]
    imArray255 = sitk.GetArrayFromImage(img_T1_255)
    imArray255 = imArray255[0, :, :]
    print(imArray255.shape)
    imArray255 = imArray255.reshape((nx, ny))
    size = img_T1_255.GetSize()
    print('size = ', size)
    # myshow(img_T1_255, title='T1')  # , zslices=range(50, size[2] - 50, 20), title='T1')

    ##############################################################################
    # Seed selection
    # --------------
    print('getpos po ok')

    x, y, val = getPos(imArray255)
    print('getpos ok')
    seed = (x, y, 0)
    print('seed: ', seed)
    seg = sitk.Image(img_T1.GetSize(), sitk.sitkUInt8)
    seg.CopyInformation(img_T1)
    seg[seed] = 1

    seg = sitk.BinaryDilate(seg, 3)

    # myshow3d(sitk.LabelOverlay(img_T1_255, seg),
    #          xslices=range(img_T1.GetSize()[0], img_T1.GetSize()[1]), title="Initial Seed")

    ##############################################################################
    # ``ConfidenceConnected``
    # ^^^^^^^^^^^^^^^^^^^^^^^
    #
    #
    # Unlike in ``ConnectedThreshold``, you need not select the bounds in
    # ``ConfidenceConnected`` filter. Bounds are implicitly specified as
    # :math:`\mu\pm c\sigma`, where :math:`\mu` is the mean intensity of the seed
    # points, :math:`\sigma` their staimArray255rd deviation and :math:`c` a user specified
    # constant.
    #
    # This algorithm has some flexibility which you should familiarize yourself with:
    #
    # * The ``multiplier`` parameter is the constant :math:`c` from the formula above.
    # * You can specify a region around each seed point ``initialNeighborhoodRadius``
    #   from which the statistics are estimated, see what happens when you set it to zero.
    # * The ``numberOfIterations`` allows you to rerun the algorithm. In the first
    #   run the bounds are defined by the seed voxels you specified, in the
    #   following iterations :math:`\mu` and :math:`\sigma` are estimated from
    #   the segmented points and the region growing is updated accordingly.

    seg_conf = sitk.ConfidenceConnected(img_T1, seedList=[seed],
                                        numberOfIterations=1,
                                        multiplier=multi,
                                        initialNeighborhoodRadius=1,
                                        replaceValue=1)
    #
    # myshow3d(sitk.LabelOverlay(img_T1_255, seg_conf),
    #          xslices=range(img_T1.GetSize()[0], img_T1.GetSize()[1]), title="ConfidenceConnected")

    # Clean up, clean up
    # ------------------
    #
    # Use of low level segmentation algorithms such as region growing is often followed by a clean up step. In this step we fill holes and remove small connected components. Both of these operations are achieved by using binary morphological operations, opening (``BinaryMorphologicalOpening``) to remove small connected components and closing (``BinaryMorphologicalClosing``) to fill holes.
    #
    # SimpleITK supports several shapes for the structuring elements (kernels) including:
    #
    # - sitkAnnulus
    # - sitkBall
    # - sitkBox
    # - sitkCross
    #
    # The size of the kernel can be specified as a scalar (same for all dimensions) or as a vector of values, size per dimension.
    #
    # The following code illustrates the results of such a clean up, using
    # closing to remove holes in the original segmentation.

    vectorRadius = (2, 2, 1)
    kernel = sitk.sitkBall
    seg_clean = sitk.BinaryMorphologicalClosing(seg_conf,
                                                vectorRadius,
                                                kernel)

    myshow3d(sitk.LabelOverlay(img_T1_255, seg_clean), title="Cleaned up segmentation")

    # print seg_clean[234, 256,1]
    segArray = sitk.GetArrayFromImage(seg_clean)
    # print(segArray.shape)
    segArray = segArray[0,:,:]
    segImArray = imArray[segArray>0]
    # print(segImArray.shape, len(segImArray))
    # u = slope * signal + intercept
    sumPix = np.sum(segImArray)
    vag = np.mean(segImArray)
    # print('Q =', sumPix, 'Vavg = ', vag)
    # segImArrayPhysical = segImArray * resSlope + reIntercep #in physical units
    # sumPix = np.sum(segImArrayPhysical)
    # vag = np.mean(segImArrayPhysical)
    # print('Q =', sumPix, 'Vavg = ', vag)

    return  segArray

if __name__ == "__main__":
    img = np.zeros((512, 512, 3), np.uint8)
    getPos(img)


from time import time
import sys

import numpy as np
import numba
import matplotlib.pyplot as plt
from scipy.sparse import csr_matrix, dok_matrix

from lda import LDA
from utils import load_20newsgroups
from toy_dataset import generate_documents
from read_nips import read_nips

def draw_matrix(path, matrix, transpose=False):
    fig, ax = plt.subplots(figsize=(80, 10))
    if transpose:
        matrix = matrix.T
    plt.imshow(1-matrix, cmap='Greys')
    plt.axis('off')
    plt.savefig(path)

def save_matrix(path, matrix):
    np.save(path, matrix)

def clock_time(s):
    h, s = divmod(s, 3600)
    m, s = divmod(s, 60)
    return int(h), int(m), int(s)

def save_likelihood(likelihoods):
    with open('toy-dataset/likelihoods.txt', 'w') as f:
        for log_pred, log_joint in likelihoods:
            print(log_pred, log_joint, file=f)

ndocuments = 2000
nwords = 1000
ntopics = 20
niters = 1500

save_path = 'nips/'
save_every  = 100

# tf = load_20newsgroups(nwords, ndocuments)
vocab, tf = read_nips()
ndocuments, nwords = tf.shape

lda = LDA(ntopics, alpha=0.1)

gibbs_iterator = lda.gibbs_sampler(tf, niters)

likelihoods = []
try:
    total_time = 0
    for i in range(niters):
        t0 = time()
        # One full Gibbs iteration
        next(gibbs_iterator)

        # Timing
        elapsed = time() - t0
        total_time += elapsed
        avg_time = total_time / (i+1)
        h, m, s = clock_time(niters * avg_time - total_time)

        # Likelihood
        log_pred = lda.log_predictive()
        log_joint = lda.log_joint()
        likelihoods.append((log_pred, log_joint))

        if i % save_every == 0:
            phi = lda.phi()
            theta = lda.theta()
            save_matrix(save_path + 'phi.npy', phi)
            save_matrix(save_path + 'theta.npy', theta)

        print("| Iteration {} | Likelihood {:.2f} | {:.2f} sec/iter | eta {}h{}m{}s".format(i, log_pred, avg_time, h, m, s))

    draw_matrix(save_path + 'phi.pdf', phi)
    draw_matrix(save_path + 'theta.pdf', theta, transpose=True)
    save_likelihood(likelihoods)

except KeyboardInterrupt:
    save_matrix(save_path + 'phi.npy', phi)
    save_matrix(save_path + 'theta.npy', theta)

    draw_matrix(save_path + 'phi.pdf', phi)
    draw_matrix(save_path + 'theta.pdf', theta, transpose=True)
    save_likelihood(likelihoods)


# -*- coding: utf-8 -*-
import json
import re
import scrapy
from locations.items import GeojsonPointItem
from locations.hours import OpeningHours

class ProvidenceHealthServicesSpider(scrapy.Spider):
    name = "providence_health_services"
    item_attributes = { 'brand': "Providence Health Services" }
    allowed_domains = ["providence.org"]

    def start_requests(self):

        ll_urls = ['https://california.providence.org/locations/list-view/',
                   'https://alaska.providence.org/locations/list-view/',
                   'https://oregon.providence.org/location-directory/list-view/'
                   ]

        for url in ll_urls:
            yield scrapy.Request(url, callback=self.parse_ll)

        mp_urls = ['https://montana.providence.org/locations-directory/list-view/',
                   'https://washington.providence.org/locations-directory/search-results/'
                   ]

        for url in mp_urls:
            yield scrapy.Request(url, callback=self.mp_page)

    def ll_page(self, response):
        pages = response.xpath(
            '//*[@id="psjh_body_1_psjh_twocol_celltwo_0_locationsListing_locationsRepeater_headerPagingContainer" ]/a/@href').extract()
        update = pages[-1]
        update = update[:-1] + "1"
        pages.pop()
        pages.append(update)
        for page in pages:
            yield scrapy.Request(response.urljoin(page), callback=self.parse_mp)

    def parse_ll(self, response):
        script = " ".join(response.xpath('//script/text()').extract())
        data = re.search(r'locationsList\s=\s\'(.*?)\'', script).groups()[0]
        data = json.loads(data)

        for place in data:
            for x in place["Locations"]:
                try:
                    street = re.search(r'\s*(.*)(?=<)', x["Address"]).groups()[0]
                    properties = {
                        'name': x["Name"],
                        'ref': x["Name"] + x["Maps"],
                        'addr_full': street.replace("<br/>", ", "),
                        'city': re.search(r'(?<=>)\s*([\sA-Za-z]*)(?=,\s*[A-Z]{2}\s*[0-9]{5})', x["Address"]).groups()[
                            0],
                        'state': re.search(r'>\D*,\s*(.[A-Z]{2})\s*[0-9]{5}', x["Address"]).groups()[0],
                        'postcode': re.search(r'>\D*,.[A-Z]{2} \s*(\d{5})', x["Address"])[1],
                        'country': "US",
                        'phone': x["PhoneNumber"],
                        'website': x["Maps"],
                        'lat': place["Latitude"],
                        'lon': place["Longitude"],
                    }

                    yield GeojsonPointItem(**properties)
                ## There is one record in Alaska that is missing ["Address"]
                except:
                    pass

    def mp_page(self, response):
        pages = response.xpath(
            '//*[@id="psjh_body_1_psjh_twocol_celltwo_0_locationsListing_locationsRepeater_headerPagingContainer" ]/a/@href').extract()
        update = pages[-1]
        update = update[:-1] + "1"
        pages.pop()
        pages.append(update)
        for page in pages:
            yield scrapy.Request(response.urljoin(page), callback=self.parse_mp)

    def parse_mp(self, response):
        script = response.xpath('//*[@id="mapPoints"]/@value').extract()
        data = json.loads(script[0])

        for place in data:
            coords = place["mapPointLocation"].split(",")

            properties = {
                'name': place["name"],
                'ref': place["name"] + place["moreInfoUrl"],
                'addr_full': place["addressLineOne"],
                'city': place["city"],
                'state': place["state"],
                'postcode': place["postalCode"],
                'country': "US",
                'website': place["moreInfoUrl"],
                'lat': coords[0],
                'lon': coords[1],
            }

            yield GeojsonPointItem(**properties)


from dataclasses import asdict
from datetime import datetime
from unittest.mock import call, sentinel

import factory
import pytest
from freezegun import freeze_time
from h_matchers import Any

from lms.services.digest import (
    DigestContext,
    DigestService,
    UnifiedCourse,
    UnifiedUser,
    service_factory,
)
from lms.services.mailchimp import EmailRecipient, EmailSender
from tests import factories

class TestDigestService:
    @freeze_time("2023-04-30")
    def test_send_instructor_email_digests(
        self,
        svc,
        h_api,
        context,
        DigestContext,
        db_session,
        send_template,
        sender,
        email_unsubscribe_service,
    ):
        context.unified_users = UnifiedUserFactory.create_batch(2)
        digests = context.instructor_digest.side_effect = [
            {"total_annotations": 1},
            {"total_annotations": 2},
        ]

        svc.send_instructor_email_digests(
            sentinel.audience, sentinel.updated_after, sentinel.updated_before
        )

        h_api.get_annotations.assert_called_once_with(
            sentinel.audience, sentinel.updated_after, sentinel.updated_before
        )
        DigestContext.assert_called_once_with(
            db_session, sentinel.audience, tuple(h_api.get_annotations.return_value)
        )
        assert context.instructor_digest.call_args_list == [
            call(user.h_userid) for user in context.unified_users
        ]
        assert send_template.delay.call_args_list == [
            call(
                task_done_key=f"instructor_email_digest::{unified_user.h_userid}::2023-04-30",
                template_name="instructor-email-digest",
                sender=asdict(sender),
                recipient=asdict(
                    EmailRecipient(unified_user.email, unified_user.display_name)
                ),
                template_vars=digest,
                unsubscribe_url=email_unsubscribe_service.unsubscribe_url.return_value,
            )
            for unified_user, digest in zip(context.unified_users, digests)
        ]

    def test_send_instructor_email_digests_without_deduplication(
        self, svc, context, send_template
    ):
        context.unified_users = [UnifiedUserFactory()]
        context.instructor_digest.side_effect = [{"total_annotations": 1}]

        svc.send_instructor_email_digests(
            sentinel.audience,
            sentinel.updated_after,
            sentinel.updated_before,
            deduplicate=False,
        )

        # If deduplicate=True is passed to DigestService then it passes
        # task_done_key=None to send_template() which disables deduplication of
        # sent emails. This is used by admin pages that actually want to allow
        # you to send duplicate emails if requested.
        assert not send_template.delay.call_args.kwargs["task_done_key"]

    def test_send_instructor_email_digests_doesnt_send_empty_digests(
        self, svc, context, send_template
    ):
        context.unified_users = UnifiedUserFactory.create_batch(2)
        context.instructor_digest.return_value = {"total_annotations": 0}

        svc.send_instructor_email_digests(
            [sentinel.h_userid], sentinel.updated_after, sentinel.updated_before
        )

        send_template.delay.assert_not_called()

    def test_send_instructor_email_digests_ignores_instructors_with_no_email_address(
        self, svc, context, send_template
    ):
        context.unified_users = [UnifiedUserFactory(email=None)]
        context.instructor_digest.return_value = {"total_annotations": 1}

        svc.send_instructor_email_digests(
            sentinel.audience, sentinel.updated_after, sentinel.updated_before
        )

        send_template.delay.assert_not_called()

    @pytest.mark.usefixtures("email_unsubscribe_service")
    def test_send_instructor_email_digests_uses_override_to_email(
        self, svc, context, send_template
    ):
        context.unified_users = [UnifiedUserFactory()]
        context.instructor_digest.return_value = {"total_annotations": 1}

        svc.send_instructor_email_digests(
            sentinel.audience,
            sentinel.updated_after,
            sentinel.updated_before,
            override_to_email=sentinel.override_to_email,
        )

        assert (
            send_template.delay.call_args[1]["recipient"]["email"]
            == sentinel.override_to_email
        )

    @pytest.fixture(autouse=True)
    def DigestContext(self, patch):
        return patch("lms.services.digest.DigestContext")

    @pytest.fixture
    def context(self, DigestContext):
        return DigestContext.return_value

    @pytest.fixture
    def sender(self):
        return EmailSender(sentinel.subaccount, sentinel.from_email, sentinel.from_name)

    @pytest.fixture
    def h_api(self, h_api):
        h_api.get_annotations.return_value = [
            sentinel.annotation1,
            sentinel.annotation2,
        ]
        return h_api

    @pytest.fixture
    def svc(self, db_session, h_api, sender, email_unsubscribe_service):
        return DigestService(
            db=db_session,
            h_api=h_api,
            sender=sender,
            email_unsubscribe_service=email_unsubscribe_service,
        )

class TestDigestContext:
    def test_instructor_digest(self, db_session, make_instructor):
        courses = factories.Course.create_batch(2)
        instructor, learner = factories.User.create_batch(2)
        for course in courses:
            make_instructor(instructor, course)
        annotations = [
            *Annotation.create_batch(
                2,
                authority_provided_id=courses[0].authority_provided_id,
                userid=learner.h_userid,
            ),
            Annotation(
                authority_provided_id=courses[1].authority_provided_id,
                userid=learner.h_userid,
            ),
        ]
        context = DigestContext(db_session, [instructor.h_userid], annotations)

        digest = context.instructor_digest(instructor.h_userid)

        assert digest == {
            "total_annotations": 3,
            "courses": Any.list.containing(
                [
                    {"title": courses[0].lms_name, "num_annotations": 2},
                    {"title": courses[1].lms_name, "num_annotations": 1},
                ]
            ).only(),
        }

    def test_instructor_digest_removes_courses_with_no_learner_annotations(
        self, db_session, make_instructor
    ):
        course = factories.Course()
        instructor = factories.User()
        make_instructor(instructor, course)
        annotations = [
            Annotation(
                authority_provided_id=course.authority_provided_id,
                userid=instructor.h_userid,
            )
        ]
        context = DigestContext(db_session, [instructor.h_userid], annotations)

        digest = context.instructor_digest(instructor.h_userid)

        assert digest == {"total_annotations": 0, "courses": []}

    def test_instructor_digest_omits_courses_where_the_user_isnt_an_instructor(
        self, db_session, make_instructor
    ):
        course, other_course = factories.Course.create_batch(2)
        instructor, learner = factories.User.create_batch(2)
        make_instructor(instructor, other_course)
        annotations = [
            Annotation(
                authority_provided_id=course.authority_provided_id,
                userid=learner.h_userid,
            )
        ]
        context = DigestContext(db_session, [instructor.h_userid], annotations)

        digest = context.instructor_digest(instructor.h_userid)

        assert digest == {"total_annotations": 0, "courses": []}

    def test_unified_users(self, db_session):
        audience = factories.User.create_batch(2)
        context = DigestContext(db_session, [user.h_userid for user in audience], [])

        unified_users = context.unified_users

        assert unified_users == [
            UnifiedUser(h_userid=user.h_userid, email=Any(), display_name=Any())
            for user in audience
        ]
        assert context.unified_users is unified_users

    def test_unified_users_with_no_audience_or_annotations(self, db_session):
        context = DigestContext(db_session, [], [])

        assert context.unified_users == []

    def test_unified_users_ignores_duplicate_userids(self, db_session):
        user = factories.User()
        context = DigestContext(
            db_session,
            [user.h_userid, user.h_userid],
            Annotation.create_batch(2, userid=user.h_userid),
        )

        unified_users = context.unified_users

        assert unified_users == [
            UnifiedUser(h_userid=user.h_userid, email=Any(), display_name=Any())
        ]

    @pytest.mark.parametrize(
        "users,expected_email",
        [
            # It uses the most recent email.
            (
                [
                    factories.User.build(
                        h_userid="id",
                        email="email@example.com",
                        updated=datetime(year=2024, month=1, day=2),
                    ),
                    factories.User.build(
                        h_userid="id",
                        email="older@example.com",
                        updated=datetime(year=2024, month=1, day=1),
                    ),
                ],
                "email@example.com",
            ),
            # It uses None if we have no email for the user.
            (
                [factories.User.build(h_userid="id", email=None)],
                None,
            ),
            # If the most recent user has no email it falls back to an older one.
            (
                [
                    factories.User.build(
                        h_userid="id",
                        email=None,
                        updated=datetime(year=2024, month=1, day=2),
                    ),
                    factories.User.build(
                        h_userid="id",
                        email="email@example.com",
                        updated=datetime(year=2024, month=1, day=1),
                    ),
                ],
                "email@example.com",
            ),
        ],
    )
    def test_unified_users_email(self, db_session, users, expected_email):
        db_session.add_all(users)

        context = DigestContext(db_session, ["id"], [])

        assert context.unified_users == Any.list.containing(
            [
                Any.instance_of(UnifiedUser).with_attrs(
                    {"h_userid": "id", "email": expected_email}
                )
            ]
        )

    @pytest.mark.parametrize(
        "users,expected_display_name",
        [
            # It uses the most recent display name.
            (
                [
                    factories.User.build(
                        h_userid="id",
                        display_name="most_recent",
                        updated=datetime(year=2024, month=1, day=2),
                    ),
                    factories.User.build(
                        h_userid="id",
                        display_name="older",
                        updated=datetime(year=2024, month=1, day=1),
                    ),
                ],
                "most_recent",
            ),
            # It uses None if we have no display name for the user.
            (
                [factories.User.build(h_userid="id", display_name=None)],
                None,
            ),
            # If the most recent user has no display name it falls back to an older one.
            (
                [
                    factories.User.build(
                        h_userid="id",
                        display_name=None,
                        updated=datetime(year=2024, month=1, day=2),
                    ),
                    factories.User.build(
                        h_userid="id",
                        display_name="display_name",
                        updated=datetime(year=2024, month=1, day=1),
                    ),
                ],
                "display_name",
            ),
        ],
    )
    def test_unified_users_display_name(self, db_session, users, expected_display_name):
        db_session.add_all(users)

        context = DigestContext(db_session, ["id"], [])

        assert context.unified_users == Any.list.containing(
            [
                Any.instance_of(UnifiedUser).with_attrs(
                    {"h_userid": "id", "display_name": expected_display_name}
                )
            ]
        )

    def test_unified_courses(self, db_session, make_instructor, make_learner):
        course = factories.Course()
        instructors = factories.User.create_batch(2)
        for instructor in instructors:
            make_instructor(instructor, course)
        learner = factories.User()
        make_learner(learner, course)
        section = factories.CanvasSection(parent=course)
        annotations = [
            Annotation(
                authority_provided_id=grouping.authority_provided_id,
                userid=learner.h_userid,
            )
            for grouping in (course, course, section, section)
        ]
        context = DigestContext(
            db_session, [instructor.h_userid for instructor in instructors], annotations
        )

        unified_courses = context.unified_courses

        assert unified_courses == [
            Any.instance_of(UnifiedCourse).with_attrs(
                {
                    "authority_provided_id": course.authority_provided_id,
                    "instructor_h_userids": Any.tuple.containing(
                        [instructor.h_userid for instructor in instructors]
                    ).only(),
                    "learner_annotations": Any.tuple.containing(annotations).only(),
                }
            )
        ]
        assert context.unified_courses is unified_courses

    def test_unified_courses_with_multiple_groupings(self, db_session):
        instances = factories.ApplicationInstance.create_batch(2)
        # Two courses with the same authority_provided_id but different instances.
        courses = [
            factories.Course.create(
                authority_provided_id="course_id", application_instance=instance
            )
            for instance in instances
        ]
        sub_groupings = [
            # Two sub-groups with different authority_provided_id's belonging
            # to the same instance and course. (This happens for example when a
            # Canvas course has two sections.)
            *factories.CanvasSection.create_batch(
                2,
                application_instance=courses[0].application_instance,
                parent=courses[0],
            ),
            # Two sub-groups with the same authority_provided_id but different
            # instances, and belonging to courses that have the same course
            # authority_provided_id but different instances. (This happens for
            # example when a Canvas instance has two application instances both
            # used in groups assignments in the same course.)
            *[
                factories.CanvasGroup(
                    authority_provided_id="subgroup_id",
                    application_instance=course.application_instance,
                    parent=course,
                )
                for course in courses
            ],
        ]
        annotations = [
            Annotation(authority_provided_id=grouping.authority_provided_id)
            for grouping in courses + sub_groupings
        ]
        context = DigestContext(db_session, [], annotations)

        assert context.unified_courses == [
            Any.instance_of(UnifiedCourse).with_attrs(
                {
                    "authority_provided_id": "course_id",
                    "learner_annotations": Any.tuple.of_size(6),
                }
            )
        ]

    def test_unified_courses_with_no_annotations(self, db_session):
        context = DigestContext(db_session, [], [])

        assert context.unified_courses == []

    def test_unified_courses_title(self, db_session):
        courses = [
            factories.Course.build(
                authority_provided_id="id",
                lms_name="most_recent",
                updated=datetime(year=2024, month=1, day=2),
            ),
            factories.Course.build(
                authority_provided_id="id",
                lms_name="older",
                updated=datetime(year=2024, month=1, day=1),
            ),
        ]
        db_session.add_all(courses)
        context = DigestContext(
            db_session,
            [],
            [
                Annotation(authority_provided_id=course.authority_provided_id)
                for course in courses
            ],
        )

        assert context.unified_courses == Any.list.containing(
            [
                Any.instance_of(UnifiedCourse).with_attrs(
                    {
                        "authority_provided_id": "id",
                        "title": "most_recent",
                    }
                )
            ]
        )

    def test_unified_courses_doesnt_count_learners(self, db_session, make_learner):
        course = factories.Course()
        learner = factories.User()
        make_learner(learner, course)
        annotation = Annotation(
            authority_provided_id=course.authority_provided_id, userid=learner.h_userid
        )
        context = DigestContext(db_session, [], [annotation])

        assert context.unified_courses == [
            Any.instance_of(UnifiedCourse).with_attrs({"instructor_h_userids": ()})
        ]

    def test_unified_courses_doesnt_count_instructor_annotations(
        self, db_session, make_instructor
    ):
        course = factories.Course()
        instructor = factories.User()
        make_instructor(instructor, course)
        annotation = Annotation(
            authority_provided_id=course.authority_provided_id,
            userid=instructor.h_userid,
        )
        context = DigestContext(db_session, [], [annotation])

        assert context.unified_courses == [
            Any.instance_of(UnifiedCourse).with_attrs({"learner_annotations": ()})
        ]

    def test_unified_courses_doesnt_count_instructors_from_other_courses(
        self, db_session, make_instructor
    ):
        course, other_course = factories.Course.create_batch(2)
        user = factories.User()
        # `user` is an instructor in `other_course`.
        make_instructor(user, other_course)
        # `user` is a learner in `course` and has created an annotation.
        annotation = Annotation(
            authority_provided_id=course.authority_provided_id,
            userid=user.h_userid,
        )
        context = DigestContext(db_session, [], [annotation])

        assert context.unified_courses == [
            Any.instance_of(UnifiedCourse).with_attrs(
                {"instructor_h_userids": (), "learner_annotations": (annotation,)}
            )
        ]

    def test_unified_courses_doesnt_count_annotations_from_other_courses(
        self, db_session
    ):
        course, other_course = factories.Course.create_batch(2)
        course_annotation = Annotation(
            authority_provided_id=course.authority_provided_id
        )
        other_course_annotation = Annotation(
            authority_provided_id=other_course.authority_provided_id
        )
        context = DigestContext(
            db_session, [], [course_annotation, other_course_annotation]
        )

        assert context.unified_courses == Any.list.containing(
            [
                Any.instance_of(UnifiedCourse).with_attrs(
                    {"learner_annotations": (course_annotation,)}
                )
            ]
        )

    def test_unified_courses_ignores_unknown_authority_provided_ids(self, db_session):
        context = DigestContext(
            db_session, [], [Annotation(authority_provided_id="unknown")]
        )

        assert context.unified_courses == []

class TestServiceFactory:
    def test_it(self, pyramid_request, h_api, DigestService, email_unsubscribe_service):
        settings = pyramid_request.registry.settings
        settings["mailchimp_digests_subaccount"] = sentinel.digests_subaccount
        settings["mailchimp_digests_email"] = sentinel.digests_from_email
        settings["mailchimp_digests_name"] = sentinel.digests_from_name

        service = service_factory(sentinel.context, pyramid_request)

        DigestService.assert_called_once_with(
            db=pyramid_request.db,
            h_api=h_api,
            sender=EmailSender(
                sentinel.digests_subaccount,
                sentinel.digests_from_email,
                sentinel.digests_from_name,
            ),
            email_unsubscribe_service=email_unsubscribe_service,
        )
        assert service == DigestService.return_value

    @pytest.fixture
    def DigestService(self, patch):
        return patch("lms.services.digest.DigestService")

class Annotation(factory.Factory):
    """
    A factory for annotation dicts.

    >>> Annotation()
    {'author': {'userid': 'acct:user_1@lms.hypothes.is'}, 'group': {'authority_provided_id': 'group_1'}}
    >>> Annotation(userid='acct:custom_username@lms.hypothes.is')
    {'author': {'userid': 'acct:custom_username@lms.hypothes.is'}, 'group': {'authority_provided_id': 'group_2'}}
    >>> Annotation(authority_provided_id='custom_id')
    {'author': {'userid': 'acct:user_2@lms.hypothes.is'}, 'group': {'authority_provided_id': 'custom_id'}}
    """

    class Meta:
        model = dict

    userid = factory.Sequence(lambda n: f"acct:user_{n}@lms.hypothes.is")
    authority_provided_id = factory.Sequence(lambda n: f"group_{n}")

    @factory.post_generation
    def post(obj, *_args, **_kwargs):  # pylint:disable=no-self-argument
        # pylint:disable=unsupported-assignment-operation,no-member
        obj["author"] = {"userid": obj.pop("userid")}
        obj["group"] = {"authority_provided_id": obj.pop("authority_provided_id")}
        return obj

class UnifiedUserFactory(factory.Factory):
    class Meta:
        model = UnifiedUser

    h_userid = factory.Sequence(lambda n: f"acct:user_{n}@lms.hypothes.is")
    email = factory.Sequence(lambda n: f"user_{n}@example.com")
    display_name = factory.Sequence(lambda n: f"User {n}")

@pytest.fixture
def instructor_role():
    return factories.LTIRole(value="Instructor")

@pytest.fixture
def learner_role():
    return factories.LTIRole(value="Learner")

@pytest.fixture
def make_instructor(db_session, instructor_role):
    def make_instructor(user, course):
        """Make each user in `users` an instructor in `course`."""
        assignment = factories.Assignment()
        factories.AssignmentGrouping(assignment=assignment, grouping=course)
        factories.AssignmentMembership(
            assignment=assignment, user=user, lti_role=instructor_role
        )
        db_session.flush()

    return make_instructor

@pytest.fixture
def make_learner(db_session, learner_role):
    def make_learner(user, course):
        """Make `user` a learner in `course`."""
        assignment = factories.Assignment()
        factories.AssignmentGrouping(assignment=assignment, grouping=course)
        factories.AssignmentMembership(
            assignment=assignment, user=user, lti_role=learner_role
        )
        db_session.flush()

    return make_learner

@pytest.fixture(autouse=True)
def send_template(patch):
    return patch("lms.services.digest.send_template")


# https://github.com/twilio/twilio-python
# http://quest.twilio.com/
# (510) 788-2674
# https://twilio-python.readthedocs.org/en/latest/usage/twiml.html
# https://twilio-python.readthedocs.org/en/latest/api/twiml.html
# https://www.twilio.com/docs/quickstart/python/twiml/record-caller-leave-message
# http://flask.pocoo.org/docs/quickstart/
# https://github.com/twilio

import os

from flask import Flask
from flask import Response
from flask import request
from flask import render_template
from twilio import twiml
from twilio.rest import TwilioRestClient

# Pull in configuration from system environment variables
TWILIO_ACCOUNT_SID = os.environ.get('TWILIO_ACCOUNT_SID')
TWILIO_AUTH_TOKEN = os.environ.get('TWILIO_AUTH_TOKEN')
TWILIO_NUMBER = os.environ.get('TWILIO_NUMBER')

# create an authenticated client that can make requests to Twilio for your
# account.
client = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)

# Create a Flask web app
app = Flask(__name__)

# Render the home page
@app.route('/')
def index():
    return render_template('index.html')

# Handle a POST request to send a text message. This is called via ajax
# on our web page
@app.route('/message', methods=['POST'])
def message():
    # Send a text message to the number provided
    message = client.sms.messages.create(to=request.form['to'],
                                         from_=TWILIO_NUMBER,
                                         body='Good luck on your Twilio quest!')

    # Return a message indicating the text message is enroute
    return 'Message on the way!'

# Handle a POST request to make an outbound call. This is called via ajax
# on our web page
@app.route('/call', methods=['POST'])
def call():
    # Make an outbound call to the provided number from your Twilio number
    call = client.calls.create(to=request.form['to'], from_=TWILIO_NUMBER,
                               url='http://twimlets.com/message?Message%5B0%5D=http://demo.kevinwhinnery.com/audio/zelda.mp3')

    # Return a message indicating the call is coming
    return 'Call inbound!'

# Generate TwiML instructions for an outbound call
@app.route('/hello')
def hello():
    response = twiml.Response()
    response.say('Hello there! You have successfully configured a web hook.')
    response.say('Good luck on your Twilio quest!', voice='woman')
    return Response(str(response), mimetype='text/xml')

# Chapter 1
# Generate TwiML instructions for an outbound call
@app.route('/incoming/sms', methods = ["POST"])
def text():
    response = twiml.Response()
    response.message('I just responded to a text message. Huzzah!.')
    return Response(str(response), mimetype='text/xml')

# Chapter 1
# @app.route('/incoming/call', methods = ["POST"])
# def calls():
#     response = twiml.Response()
#     response.say('I just responded to a phone call.', voice='woman')
#     return Response(str(response), mimetype='text/xml')

# Chapter 2
# @app.route('/incoming/call', methods = ["POST"])
# def ivr():
#     twiml = '<?xml version="1.0" encoding="UTF-8"?> \
# <Response> \
#     <Gather timeout="10" finishOnKey="*" action="/incoming/gather" method="POST"> \
#         <Say>Please enter one for help and two for nothing followed by star.</Say> \
#     </Gather> \
# </Response>'
#     return Response(twiml, mimetype="text/xml")

# @app.route('/incoming/gather', methods = ['POST'])
# def gather():
#     digits = request.form['Digits']

#     if (digits == '1'):
#         response = twiml.Response()
#         response.say('Congrats this is 1 for help option.', voice='woman')
#         return Response(str(response), mimetype='text/xml')
#     elif (digits == '2'):
#         response = twiml.Response()
#         response.say('Congrats this is 2 for Nothing option.', voice='woman')
#         return Response(str(response), mimetype='text/xml')
#     else:
#         response = twiml.Response()
#         response.say('Please select 1 OR 2', voice='woman')
#         response.redirect(url='/incoming/call')
#         return Response(str(response), mimetype='text/xml')

# Chapter 3
@app.route('/incoming/call', methods = ["POST"])
def ivr():
    twiml = '<?xml version="1.0" encoding="UTF-8"?> \
<Response> \
    <Gather timeout="10" finishOnKey="*" action="/incoming/gather" method="POST"> \
        <Say>Please enter one to listen to a message and two to record followed by star.</Say> \
    </Gather> \
</Response>'
    return Response(twiml, mimetype="text/xml")

@app.route('/incoming/gather', methods = ['POST'])
def gather():
    digits = request.form['Digits']

    if (digits == '1'):
# Play
        recording_url = request.values.get("RecordingUrl", None)
        resp = twiml.Response()
        resp.say("Thanks for howling... take a listen to what you howled.")
        resp.play(recording_url)
        resp.say("Goodbye.")
        return str(resp)

# Record
    elif digits == "2":
        resp = twiml.Response()
        resp.say("Record your monkey howl after the tone.")
        resp.record(maxLength="30", action="/handle-recording")
        return str(resp)
    else:
        response = twiml.Response()
        response.say('Please select 1 OR 2', voice='woman')
        response.redirect(url='/incoming/call')
        return Response(str(response), mimetype='text/xml')

if __name__ == '__main__':
    # Note that in production, you would want to disable debugging
    app.run(debug=True)


import os
import shutil

import zarr
import numpy as np

from .. import ZarrIndexer
from jina.executors.indexers import BaseIndexer
from jina.executors.indexers.vector import NumpyIndexer

np.random.seed(500)
retr_idx = None
num_data = 10000
num_dim = 64
num_query = 100
vec_idx = np.arange(num_data)
np.random.shuffle(vec_idx)
vec = np.random.random([num_data, num_dim])
query = np.array(np.random.random([num_query, num_dim]), dtype=np.float32)

def rm_files(file_paths):
    for file_path in file_paths:
        if os.path.exists(file_path):
            if os.path.isfile(file_path):
                os.remove(file_path)
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path, ignore_errors=False, onerror=None)

def test_zarr_indexer():
    with ZarrIndexer(index_filename='test.zarr') as indexer:
        indexer.add(vec_idx, vec)
        indexer.save()
        assert os.path.exists(indexer.index_abspath)
        assert indexer.raw_ndarray.shape == vec.shape
        assert 'default' in indexer.write_handler.array_keys()
        index_abspath = indexer.index_abspath
        save_abspath = indexer.save_abspath

    with ZarrIndexer.load(save_abspath) as indexer:
        assert isinstance(indexer, NumpyIndexer)
        idx, dist = indexer.query(query, top_k=4)
        global retr_idx
        if retr_idx is None:
            retr_idx = idx
        else:
            np.testing.assert_almost_equal(retr_idx, idx)
        assert idx.shape == dist.shape
        assert idx.shape == (num_query, 4)
    rm_files([index_abspath, save_abspath])

def test_zarr_indexer_known():
    vectors = np.array([[1, 1, 1],
                        [10, 10, 10],
                        [100, 100, 100],
                        [1000, 1000, 1000]])
    keys = np.array([4, 5, 6, 7]).reshape(-1, 1)
    with ZarrIndexer(index_filename='test.zarr') as indexer:
        indexer.add(keys, vectors)
        indexer.save()
        assert os.path.exists(indexer.index_abspath)
        index_abspath = indexer.index_abspath
        save_abspath = indexer.save_abspath

    queries = np.array([[1, 1, 1],
                        [10, 10, 10],
                        [100, 100, 100],
                        [1000, 1000, 1000]])
    with ZarrIndexer.load(save_abspath) as indexer:
        assert isinstance(indexer, NumpyIndexer)
        idx, dist = indexer.query(queries, top_k=2)
        np.testing.assert_equal(idx, np.array([[4, 5], [5, 4], [6, 5], [7, 6]]))
        assert idx.shape == dist.shape
        assert idx.shape == (4, 2)
        np.testing.assert_equal(indexer.query_by_id([7, 4]), vectors[[3, 0]])

    rm_files([index_abspath, save_abspath])


import tornado.web
from concurrent.futures import ThreadPoolExecutor
from ..util.login import checktoken
from ..config.secure import *

class CustomHandler(tornado.web.RequestHandler):
    executor = ThreadPoolExecutor(2)

    def write_error(self, status_code, **kwargs):
        self.write(str(status_code) + '  error!!!')

    def writeResult(self, retcode=0, info={}):
        self.set_header("Access-Control-Allow-Origin", allowOriginStr)
        self.set_header("Access-Control-Allow-Credentials", allowCredentialsStr)
        info['ret'] = retcode
        self.write(str(info))

    def checkAuthorize(self):
        token = self.get_secure_cookie('token')
        if token:
            return checktoken(token)
        return 0, 0


#write a program that copies contents of file:
f=open('File.txt','w')
f.writelines("Give me the power,\nI will give you freedom. \nWhen ever you treat me bad,\nThe reaction of mine is also bad.")
f.close()

#write a program that produces error on name length less than 3:
name=input("enter the Name:")
if name.isalpha() and len(name)<3:
    raise NameError("Name is not valid")
else:
    print("accepted")

#write a program that produces 3 diffrent error and write handler for that error:

try:
    age = int(input("Enter your age: "))
    print ("You must be {0} years old.".format(age))
except ValueError:
    print( "Your age must be numeric.")

try:
    x=input("enter the integer:")
    add=x+t
except NameError:
    print("t is not declared")
except:
    print("t is declared")

try:
    a=input("enter the value")
    if a<3:
        print("enter the value again")
    else:
        print("value",a)

except SyntaxError:
    print("SyntexError in line 34 & line 36")
except:
    print("the value of a is defined")

#wap to correct the text:
str="Hello-this-is-my-file"
print(str.replace('-',' '))

#extra questions
from textblob import TextBlob
text=TextBlob('he is gret persan')
print(text.correct())


from setuptools import setup, find_packages

with open("README.md", "r") as fh:
    long_description = fh.read()

setup(
    name="chemreader",
    version="0.1.1",
    author="Yang Liu",
    author_email="thomasliuy@gmail.com",
    description="Read data from typical chemical file formats",
    long_description=long_description,
    long_description_content_type="text/markdown",
    packages=find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    install_requires=[],
    python_requires='>=3.6',
)


import pygal
from die import Die

MAX_R = 100000
SNUM = 8

# Create two D8 dices
die1 = Die(SNUM)
die2 = Die(SNUM)

# Make some rolls, and store results in a list.
# list comprehension added
results = [die1.roll() + die2.roll() for r in range(MAX_R)]

# Analyze the results.
max_result = die1.num_sides + die2.num_sides
# list comprehension added
frequencies = [results.count(value) for value in range(2, max_result+1)]

# Visualize the results.
hist = pygal.Bar()

hist.title = "Results of rolling two D%s dice %s times." % (SNUM, MAX_R)
# list comprehension added
hist.x_labels = [str(n) for n in range(2, max_result+1)]
hist.x_title = "Result"
hist.y_title = "Frequency of Result"

hist.add(('D%s + D%s' % (SNUM, SNUM)), frequencies)
hist.render_to_file('./dice_visual11.svg')