from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import time
import unittest

url = "C:\\Users\\glenn.d.badong\\Documents\\WORK\\Clockwork\\ArcadisGen_Automation\\chromedriver.exe"
global driver
global wait
driver = webdriver.Chrome(executable_path = url)
wait = WebDriverWait(driver, 10)
# driver = webdriver.Firefox(executable_path='C:\\Users\\glenn.d.badong\\Documents\\WORK\\Clockwork\\ArcadisGen_Automation\\webdrivers\\geckodriver.exe')

class CommonSteps(unittest.TestCase):
	def __init__(self):
		pass

	"""this will swith to main / default iframe"""
	def frame_switch(self):
		# driver.switch_to.frame(driver.find_element_by_xpath("//*[@id='header']/div/div/div[1]/div/div/div/div[1]/div/a/img"))
		driver.switch_to.default_content()

	"""this will wait until xpath element is visible"""
	#### wait_until_xpath_exists(<xpath of element>)
	def wait_until_xpath_exists(self, expat):
		wait.until(ec.presence_of_element_located((By.XPATH, expat)))

	"""this will close browser"""
	def test_close_browser(self):
		driver.close()
		self.assertTrue(True)

	"""this will open browser and open arcadisgen website"""
	# this is predefined url but in the future we can update this to cater firefox and other urls
	def test_open_url(self):
		driver.maximize_window()

		#navigate to the URL
		driver.get("https://www.arcadisgen.com/")

		#click I accept button
		self.wait_until_xpath_exists("//*[@id='cookiebot-js']/div/div[2]/div[3]/a[1]")
		try:
		    driver.find_element_by_xpath("//*[@id='cookiebot-js']/div/div[2]/div[3]/a[1]").click()
		except NoSuchElementException:
		    print("cookiebot not found")

		self.assertTrue(True)
		# print("login - DONE")

	"""this will validate all elements under products dropdown"""
	def test_validate_products_elements(self):
		#click Products dropdown
		self.frame_switch()
		# driver.find_element_by_xpath("//*[@id='header']/div/div/div[1]/div/div/div/div[1]/div/a/img")
		driver.find_element_by_xpath("//*[contains(text(), 'Products')]").click()
		try:
			driver.find_element_by_xpath("//*[contains(text(), 'Enterprise Decision Analytics')]")
		except NoSuchElementException:
			print("'Enterprise Decision Analytics' - not found")

		try:
			driver.find_element_by_xpath("//*[contains(text(), 'Enterprise Asset Management')]")
		except NoSuchElementException:
			print("'Enterprise Asset Management' - not found")

		try:
			driver.find_element_by_xpath("//*[contains(text(), 'InvestSmart')]")
		except NoSuchElementException:
			print("'InvestSmart' - not found")

		try:
			driver.find_element_by_xpath("//*[contains(text(), 'Universal Visual Optimizer')]")
		except NoSuchElementException:
			print("'Universal Visual Optimizer' - not found")

		try:
			driver.find_element_by_xpath("//*[contains(text(), 'Water Above Ground Optimizer')]")
		except NoSuchElementException:
			print("'Water Above Ground Optimizer' - not found")

		try:
			driver.find_element_by_xpath("//*[contains(text(), 'Water AI Pipe Predictor')]")
		except NoSuchElementException:
			print("'Water AI Pipe Predictor' - not found")

		try:
			driver.find_element_by_xpath("//*[contains(text(), 'Omnia')]")
		except NoSuchElementException:
			print("'Omnia' - not found")

		try:
			driver.find_element_by_xpath("//*[contains(text(), 'VIEW ALL PRODUCTS')]")
		except NoSuchElementException:
			print("'VIEW ALL PRODUCTS' - not found")

		self.assertTrue(True)
		# print("checking_elements_inside_product_dropdown - DONE")

	def test_validateHeaderButtons(self):
		self.frame_switch()
		try:
			driver.find_element_by_xpath("//*[contains(text(), 'Products')]")
		except NoSuchElementException:
			print("'Products' - not found")

		try:
			driver.find_element_by_xpath("//*[contains(text(), 'PROJECTS')]")
		except NoSuchElementException:
			print("'PROJECTS' - not found")

		try:
			driver.find_element_by_xpath("//*[contains(text(), 'INSIGHTS')]")
		except NoSuchElementException:
			print("'INSIGHTS' - not found")

		try:
			driver.find_element_by_xpath("//*[contains(text(), 'What We Do')]")
		except NoSuchElementException:
			print("'What We Do' - not found")

		try:
			driver.find_element_by_xpath("//*[contains(text(), 'Who We Are')]")
		except NoSuchElementException:
			print("'Who We Are' - not found")

		try:
			driver.find_element_by_xpath("//*[contains(text(), 'Join Us')]")
		except NoSuchElementException:
			print("'Join Us' - not found")

		self.assertTrue(True)


import tensorflow as tf
import matplotlib.pyplot as plt
class GradientDescentController:
    def __init__(self):
        pass

    def service_model(self):
        X = [1., 2., 3.]
        Y = [1., 2., 3.]
        m = n_samples = len(X)
        W = tf.placeholder(tf.float32)
        hypothesis = tf.multiply(X, W)
        cost = tf.reduce_mean(tf.pow(hypothesis - Y, 2)) / m
        W_val = []
        cost_val = []
        with  tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            for i in range(-30, 50):
                W_val.append(i * 0.1)
                cost_val.append(sess.run(cost, {W: i * 0.1}))
        plt.plot(W_val, cost_val, 'ro')
        plt.ylabel('cost')
        plt.xlabel('W')
        plt.savefig('static/img/gradient_descent.svg')
        return "경사하강법(gradient descent)"


import itertools
from math import gcd
from functools import reduce
from collections import defaultdict

class Moon:
    def __init__(self, position):
        self.position = defaultdict(int)
        self.position = {pos.split("=")[0]:int(pos.split("=")[1]) for pos in position}
        self.velocity = defaultdict(int)
        self.velocity = {pos:0 for pos in ["x", "y", "z"]}
        self.initial_position = defaultdict(int)
        self.initial_position = {pos.split("=")[0]:int(pos.split("=")[1]) for pos in position}
        self.initial_velocity = defaultdict(int)
        self.initial_velocity = {pos:0 for pos in ["x", "y", "z"]}

def lcm(denominators):
    return reduce(lambda a,b: a*b // gcd(a,b), denominators)

def universe():
    step = 0
    rotations = {}
    energy = 0

    while True:
        step += 1
        #apply gravity
        for pair in moon_pairs:
            for pos in ["x", "y", "z"]:
                if pair[0].position[pos] > pair[1].position[pos]:
                    pair[0].velocity[pos] += -1
                    pair[1].velocity[pos] += 1
                elif pair[0].position[pos] < pair[1].position[pos]:
                    pair[0].velocity[pos] += 1
                    pair[1].velocity[pos] += -1

        #apply velocity and calculate energy
        pot = 0
        kin = 0
        total = 0
        for moon in moon_list:
            for pos in ["x", "y", "z"]:
                moon.position[pos] += moon.velocity[pos]
            pot = (abs(moon.position["x"]) + abs(moon.position["y"]) + abs(moon.position["z"]))
            kin = (abs(moon.velocity["x"]) + abs(moon.velocity["y"]) + abs(moon.velocity["z"]))
            total += pot * kin

        for pos in ["x", "y", "z"]:
            if all(map(lambda moon: moon.position[pos] == moon.initial_position[pos] and moon.velocity[pos] == moon.initial_velocity[pos], moon_list)):
                if pos not in rotations.keys():
                    rotations[pos] = step

        if step == 1000:
            energy = total

        if len(rotations.keys()) == 3 and energy != 0:
            return energy, rotations

with open("input.txt") as f:
    positions = []
    for line in f:
        positions.append(line.strip()[1:-1].split(', '))

io = Moon(positions[0])
europa = Moon(positions[1])
ganymede = Moon(positions[2])
callisto = Moon(positions[3])

moon_list = [io, europa, ganymede, callisto]
moon_pairs = list(itertools.combinations(moon_list, 2))

part_1, part_2 = universe()
print(f"Total energy after 1000 steps: {part_1}")

steps = [step for name, step in part_2.items()]
print(lcm(steps))


import signalProcessor
import dspSignal
#import kivyPlot as plot
#import glfwplot as plot
import glutplot as plot
import numpy as np
import time
class FftPlot(signalProcessor.SignalProcessor):
    def __init__(self, title = "Plot", nmax=None):
        signalProcessor.SignalProcessor.__init__(self, "fftplot")
        self.fig = plot.Figure(title)
        self.nmax=nmax

    def stop(self):
        self.fig.close()

    def process(self, signalIn):
        signalOut = signalIn

        if not self.fig.isReady():
            return signalOut

        if self.nmax is None:
            nsamp = len(signalIn.samples)
        else:
            nsamp = min(len(signalIn.samples), self.nmax)

        signalOut = dspSignal.Signal(baseSig = signalIn)
        ft = np.fft.fftshift(np.fft.fft(signalIn.samples[0:nsamp]))
        db = np.clip(20*np.log10(np.abs(ft)), -100, None)
        fax = np.linspace(-signalIn.rate, signalIn.rate, nsamp)-signalIn.shift
        self.fig.plot(fax, db)
        return signalOut


# coding: utf-8

from __future__ import unicode_literals

import json
import logging
import sys

from flask import Flask, request

from seabattle import dialog_manager as dm
from seabattle import session

app = Flask(__name__)

handler = logging.StreamHandler(stream=sys.stderr)
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger = logging.getLogger('seabattle')
logger.addHandler(handler)
logger.setLevel(logging.INFO)

@app.route('/', methods=['POST'])
def main():
    logger.error('Request: %r', request.json)

    response = {
        'version': request.json['version'],
        'session': request.json['session'],
    }
    json_body = request.json

    user_id = json_body['session']['user_id']
    session_obj = session.get(user_id)
    dm_obj = dm.DialogManager(session_obj)

    message = json_body['request']['command'].strip()
    if not message:
        message = json_body['request']['original_utterance']

    dmresponse = dm_obj.handle_message(message)
    response['response'] = {
        'text': dmresponse.text,
        'end_session': dmresponse.end_session,
    }
    if dmresponse.tts is not None:
        response['response']['tts'] = dmresponse.tts

    logger.error('Response: %r', response)
    return json.dumps(response)


from rtamt.operation.abstract_operation import AbstractOperation
import rtamt.operation.stl.dense_time.online.intersection as intersect

class XorOperation(AbstractOperation):
    def __init__(self):
        self.left = []
        self.right = []
        self.last = []

    def update(self, *args, **kargs):
        left_list = args[0]
        right_list = args[1]
        self.left = []
        self.right = []
        self.last = []

        self.left = self.left + left_list
        self.right = self.right + right_list

        out, last, left, right = intersect.intersection(self.left, self.right, intersect.xor)

        if last:
            out.append(last)

        return out


from ayxproperty import AyxProperty
from tool import Tool
from field import Field
from xml.dom import minidom
import xml.etree.ElementTree as ET
from typing import Dict, List
from dataclasses import dataclass

@dataclass
class AutofieldField:
    """
    Contains information for a field that may or may not be selected in an Autofield tool.
    """
    field: str = ''
    selected: bool = True

class AutofieldTool(Tool):
    """
    Represents an Autofield tool in an Alteryx workflow.
    """
    def __init__(self, tool_id: str, autofield_fields: List[AutofieldField]):
        super().__init__(tool_id)
        self.plugin = 'AlteryxBasePluginsGui.AutoField.AutoField'
        self.engine_dll = 'AlteryxBasePluginsEngine.dll'
        self.engine_dll_entry_point = 'AlteryxAutoField'
        self.inputs.append('Input')
        self.outputs.append('Output')

        self.autofield_fields = autofield_fields

    def __set_configuration__(self) -> None:
        self.guisettings: Dict[str, AyxProperty] = dict({
            'Position': AyxProperty('Position').set_attribute('x', str(self.position[0])).set_attribute('y', str(self.position[1]))
        })

        fields: List[AyxProperty] = list()
        for field in self.autofield_fields:
            fields.append(
                AyxProperty('Field')
                .set_attribute('field', field.field)
                .set_attribute('selected', str(field.selected))
            )

        self.properties: Dict[str, AyxProperty] = dict({
            'Configuration': AyxProperty('Configuration').add_child(
                AyxProperty('Fields').add_children(fields)
            ),
            'Annotation': AyxProperty('Annotation')
                .set_attribute('DisplayMode', '0')
                .add_child(AyxProperty('Name'))
                .add_child(AyxProperty('DefaultAnnotationText'))
                .add_child(AyxProperty('Left').set_attribute('value', 'False'))
        })

        self.engine_settings = AyxProperty('EngineSettings').set_attribute('EngineDll', self.engine_dll).set_attribute('EngineDllEntryPoint', self.engine_dll_entry_point)

    def toxml(self) -> ET.Element:
        """
        Returns an XML representation of the tool.
        """
        self.__set_configuration__()

        # Dummy element that ElementTree extend will strip
        root: ET.Element = ET.Element('root')

        node: ET.SubElement = ET.SubElement(root, 'Node')
        node.set('ToolID', self.tool_id)

        guisettings: ET.SubElement = ET.SubElement(node, 'GuiSettings')
        guisettings.set('Plugin', self.plugin)
        for _, guisettings_val in self.guisettings.items():
            xml: ET.Element = guisettings_val.toxml()
            guisettings.extend(xml)

        properties: ET.SubElement = ET.SubElement(node, 'Properties')
        for _, prop_val in self.properties.items():
            xml: ET.Element = prop_val.toxml()
            properties.extend(xml)

        node.extend(self.engine_settings.toxml())

        return root


import re, sys
from utils import *
from handler import *
from rules import *

class Parser(object):
    def __init__(self, handler):
        self.rules = []
        self.filters = []
        self.handler = handler

    def addRule(self, rule):
        self.rules.append(rule)

    def addFilter(self, pattern, name):
        def filter(block, handler):
            return re.sub(pattern, self.handler.sub(name), block)

        self.filters.append(filter)

    def parse(self, file):
        self.handler.start('document')
        for block in blocks(file):
            for filter in self.filters:
                block = filter(block, self.handler)
                # print(block)
            for rule in self.rules:
                if rule.condition(block):
                    if rule.action(block, self.handler):
                        break
        self.handler.end('document')

class BasicTextParser(Parser):
    def __init__(self, handler):
        super(BasicTextParser, self).__init__(handler)
        self.addRule(ListRule())
        self.addRule(ListItemRule())
        self.addRule(TitleRule())
        self.addRule(HeadingRule())
        self.addRule(ParagraphRule())

        self.addFilter(r'\*(.+?)\*', 'emphasis')
        self.addFilter(r'(http://[\.a-z0-9A-Z/]+)', 'url')
        self.addFilter(r'([\.a-zA-Z]+@[\.a-zA-Z]+[a-zA-Z]+)', 'mail')

if __name__ == '__main__':
    handler = HTMLRenderer()
    parser = BasicTextParser(handler)
    parser.parse(sys.stdin)


#!/usr/bin/python3
# coding=UTF-8

#================================================================
#   Copyright (C) 2018 HALO Ltd. All rights reserved.
#
#   文件名称：thread_queue.py
#   创 建 者：Zhangdunfeng
#   创建日期：2018-11-23 10:49:11
#   描    述：多线程使用优先级队列
#
#================================================================

import queue
import threading
import time

exitFlag=0

class myThread(threading.Thread):
    def __init__(self, threadID, name, q):
        threading.Thread.__init__(self)
        self.threadID=threadID
        self.name=name
        self.q=q

    def run(self):
        print("开启线程：" + self.name)
        process_data(self.name, self.q)
        print("退出线程：" + self.name)

def process_data(threadName, q):
    while not exitFlag:
        queueLock.acquire()
        if not workQueue.empty():
            data=q.get()
            print("%s processing %s" % (threadName, data))
        queueLock.release()
        time.sleep(1)

threadList=["Thread-1", "Thread-2", "Thread-3"]
nameList=["One", "Two", "Three", "Four", "Five"]
queueLock=threading.Lock()
workQueue=queue.Queue(10)
threads=[]
threadID=1

for tName in threadList:
    thread=myThread(threadID, tName, workQueue)
    thread.start()
    threads.append(thread)
    threadID+=1

queueLock.acquire()
for word in nameList:
    workQueue.put(word)
queueLock.release()

while not workQueue.empty():
    pass

#通知线程退出
exitFlag=1

for t in threads:
    t.join()

print("退出主线程")


from django.urls import path
from . import views

urlpatterns = [
     path('', views.blog, name='blog'),
     path('add/', views.new_blog, name='new_blog'),
     path('edit/<int:blog_id>/', views.edit_blog,
          name='edit_blog'),
     path('delete/<int:blog_id>/', views.delete_blog,
          name='delete_blog'),
]


"""Input/output operations for Sentera imagery."""

import logging
import os
import re
import shutil
from glob import glob

import numpy as np
import pandas as pd
import tifffile as tf

from imgcorrect import detect_panel
from imgcorrect.sensor_defs import sensor_defs

logger = logging.getLogger(__name__)

def apply_sensor_settings(image_df):
    """Rebuild image dataframe with settings based on sensor model."""
    rows = []

    for index, row in image_df.iterrows():
        for s in sensor_defs:
            # verify image metadata matches that of a supported sensor
            meets_criteria = True
            for key, val in s["criteria"].items():
                if key not in row["EXIF"] or val not in str(row["EXIF"][key]):
                    meets_criteria = False
            if meets_criteria:
                # ignore images that meet ignore_criteria
                if "ignore_criteria" in s:
                    ignore = False
                    for key, val in s["ignore_criteria"].items():
                        if key in row["EXIF"] and val in str(row["EXIF"][key]):
                            logger.info("Ignoring %s", row["image_path"])
                            ignore = True
                    if ignore:
                        break

                # apply settings for that sensor
                for key, val in s["settings"].items():
                    row[key] = val

                # if each image contains data for multiple bands, configure accordingly
                if "bands" in s:
                    for band in s["bands"]:
                        band_row = row.copy()
                        band_row["band"] = band[0]
                        band_row["band_math"] = band[1]
                        band_row["XMP_index"] = band[2]
                        band_row["reduce_xmp"] = True
                        band_row["output_path"] = add_band_to_path(
                            row.output_path, band[0]
                        ).replace(".jpg", ".tif")
                        rows.append(band_row)
                # otherwise, assume band is indicated in root folder name
                else:
                    row["band"] = re.search(
                        r"[A-Za-z]+", os.path.basename(row.image_root)
                    ).group(0)
                    row["XMP_index"] = 0
                    row["reduce_xmp"] = False
                    rows.append(row)

                break
        else:
            logger.error("Sensor not supported")
            raise Exception("Sensor not supported")

    return pd.DataFrame(rows)

def create_image_df(input_path, output_path):
    """Build image dataframe."""
    if not output_path:
        output_path = input_path

    image_df = pd.DataFrame()

    image_df["image_path"] = glob(input_path + "/**/*.tif", recursive=True) + glob(
        input_path + "/**/*.jpg", recursive=True
    )
    image_df["image_root"] = image_df.image_path.apply(os.path.dirname)
    image_df["output_path"] = image_df.image_path.str.replace(
        input_path, output_path, regex=False
    )

    return image_df

def reflectance_if_panel(row):
    """If reflectance panel images are not identifiable by filename, try computing panel reflectance for all images."""
    if not row["cal_in_path"]:
        row["mean_reflectance"], row["aruco_id"] = detect_panel.get_reflectance(row)
    return row

def detect_cal(row, calibration_id):
    """If reflectance panel images are identifiable by filename, identify them. Otherwise, refer to results of reflectance_if_panel()."""
    if row["cal_in_path"]:
        return calibration_id in row["image_path"]
    return not np.isnan(row["mean_reflectance"])

def create_cal_df(image_df, calibration_id):
    """Build calibration image dataframe."""
    image_df = image_df.apply(reflectance_if_panel, axis=1)
    is_cal_image = image_df.apply(lambda row: detect_cal(row, calibration_id), axis=1)
    return image_df.loc[is_cal_image], image_df.loc[~is_cal_image]

def delete_all_originals(image_df):
    """Delete all input images."""
    image_df.image_path.apply(os.remove)

def add_band_to_path(path, band):
    """Add band directory to path just before filename."""
    dirname, base = os.path.split(path)
    return os.path.join(dirname, band, base)

def move_images(image_df_row):
    """Move corrected image to final destination."""
    shutil.move(image_df_row.temp_path, image_df_row.output_path)

def move_corrected_images(image_df):
    """Create output directories if necessary, then move corrected images to final destination."""
    for folder in image_df.output_path.apply(os.path.dirname).unique():
        os.makedirs(folder, exist_ok=True)
    image_df.apply(lambda row: move_images(row), axis=1)

def write_image(image_arr_corrected, image_df_row, temp_dir):
    """Write corrected image to temporary location and record maximum value in case normalization is required."""
    path_list = os.path.normpath(image_df_row.image_path).split(os.path.sep)
    path_list[0] = temp_dir
    temp_path = os.path.join(*path_list)
    if "band_math" in image_df_row.index:
        temp_path = add_band_to_path(temp_path, image_df_row.band).replace(
            ".jpg", ".tif"
        )
    os.makedirs(os.path.dirname(temp_path), exist_ok=True)
    # noinspection PyTypeChecker
    tf.imwrite(temp_path, image_arr_corrected)

    image_df_row["max_val"] = np.max(image_arr_corrected)
    image_df_row["temp_path"] = temp_path
    return image_df_row

def write_corrections_csv(image_df, file):
    """Write vital correction data from the dataframe to the given csv file."""
    columns = [
        "image_path",
        "independent_ils",
        "band",
        "autoexposure",
        "ILS_ratio",
        "slope_coefficient",
        "correction_coefficient",
    ]
    csv_df = image_df[columns].copy()
    base_dir = os.path.dirname(file)

    # Get the path relative to the output folder
    csv_df["image_path"] = csv_df["image_path"].apply(
        (lambda x: os.path.relpath(x, base_dir))
    )
    csv_df.to_csv(file, index=False)


import os

from MainWindow import MainWindow
from NewProjectWizard import NewProjectWizard
from Windows import StarterWindow

from PyQt5.QtCore import Qt, QDateTime, QStringListModel, QDate
from PyQt5.QtWidgets import qApp, QMessageBox, QFileDialog
from PyQt5 import QtGui
import pandas as pd
import json

DIR_NAME = os.path.abspath(os.path.dirname(__file__))
SETTINGS_DIR = os.path.join(DIR_NAME, 'Settings')
SETTINGS_PATH = os.path.join(SETTINGS_DIR, 'settings.json')
PROJECTS_DIR = os.path.join(DIR_NAME, 'Projects')
RESOURCES_DIR = os.path.join(DIR_NAME, 'Resources')

class ProjectController():
    def __init__(self):
        icon_path = os.path.join(RESOURCES_DIR, 'icons', 'app_icon.png')
        qApp.setWindowIcon(QtGui.QIcon(icon_path))

        if not os.path.exists(PROJECTS_DIR):
            os.mkdir(PROJECTS_DIR)
        if not os.path.exists(SETTINGS_DIR):
            self.createSettingsFile()

        self.loadSettings()
        self.sw = StarterWindow(self)
        self.mw = None

    def loadSettings(self):
        '''
        Load default user settings
        '''
        with open(SETTINGS_PATH, 'rt') as f:
            self.settings = json.loads(f.read())

        self.loadTheme(self.settings.get('Theme'))

    def saveSettings(self):
        '''
        Save updated settings data
        '''
        try:
            with open(SETTINGS_PATH, 'w+') as f:
                f.write(json.dumps(self.settings))
        except:
            return False
        else:
            return True

    def loadTheme(self, theme=None):
        '''
        Load given theme and save to settings file
        '''
        if theme:
            theme_path = os.path.join(RESOURCES_DIR, f'stylesheet_{theme}.css')
            with open(theme_path, 'rt') as f:
                qApp.setStyleSheet(f.read())
        else:
            qApp.setStyleSheet(None)

        self.settings['Theme'] = theme
        self.saveSettings()

    def setAPI(self, api_key):
        '''
        Save api key to settings file
        '''
        self.settings['API'] = api_key
        return self.saveSettings()

    def createSettingsFile(self):
        '''
        Create settings directory and settings.json file if it doesn't exist
        this function should only be called on first launch of application
        '''
        default_data = {
            'Theme': None,
            'API': None
        }
        os.mkdir(SETTINGS_DIR)
        with open(SETTINGS_PATH, 'w+') as f:
            f.write(json.dumps(default_data))

    def newProject(self, window_ref):
        '''
        window_ref: (QDialog) reference to the window that function is called from
        '''
        self.npw = NewProjectWizard()

        if self.npw.exec_():
            project_name = self.npw.dataPage.getProjectName()
            ref = self.npw.dataPage.getReferencePoint()
            if project_name and ref:
                self.createProject(window_ref, project_name, ref)

    def createProject(self, window_ref, project_name, ref):
        '''
        window_ref: (QDialog) reference to the window that function is called from
        project_name: (str) name of new project
        ref: (tuple) reference point
        '''
        project_file = os.path.join(PROJECTS_DIR, project_name, 'project_data.json')
        created_date = QDateTime().currentDateTime().toString('MM-dd-yyyy hh:mm:ss ap')
        default_data = {
                'ProjectName': project_name,
                'Created': created_date,
                'LastAccessed': created_date,
                'Reference': ref,
                'Scale': 0,
                'Units': '',
                'Points': []
        }
        try:
            os.makedirs(os.path.join(PROJECTS_DIR, project_name, 'Reports'))
            with open(project_file, 'w+') as f:
                f.write(json.dumps(default_data))

        except FileExistsError:
            QMessageBox.critical(
                window_ref,
                'Project Creation Error',
                f'Invalid project name: {project_name}'
            )
        else:
            window_ref.close()
            self.mw = MainWindow(project_name, self, openExisting=True, api=self.settings.get('API'))

    def closeProject(self):
        '''
        Close the currently open projects and redisplay starter window
        '''
        self.mw.close()
        self.sw = StarterWindow(self)

    def browseProjectsDir(self, window_ref):
        fileDialog = QFileDialog(window_ref, 'Projects', PROJECTS_DIR)
        fileDialog.setFileMode(QFileDialog.DirectoryOnly)
        fileDialog.setAttribute(Qt.WA_QuitOnClose, False)

        #If a valid path is returned from file dialog screen
        if fileDialog.exec_():
            selected_path = fileDialog.selectedFiles()[0]
            #Check if json data file is in selected folder
            projectName = selected_path.split('/')[-1]
            self.openProject(projectName, window_ref)

    def openProject(self, project_name, window_ref):
        '''
        window_ref: (QDialog) a reference to the window that called this function
        '''
        path = os.path.join(PROJECTS_DIR, project_name, 'project_data.json')

        if os.path.exists(path):
            window_ref.close()
            self.mw = MainWindow(project_name, self, openExisting=True, api=self.settings.get('API'))
            #alert for invalid project and return to main window or starter screen
        else:
            QMessageBox.critical(
                window_ref,
                'Export File',
                f'{project_name} file failed to be created'
            )

    def saveProject(self, project_name, project_data):
        '''
        Saves the project data in json format and writes to a file
        '''
        project_path = os.path.join(PROJECTS_DIR, project_name, 'project_data.json')

        try:
            with open(project_path, 'w+') as f:
                f.write(json.dumps(project_data, indent=2))
        except:
            return False
        else:
            return True

    def setProjectName(self, old_name, new_name):
        '''
        Rename project
        '''
        old_path = os.path.join(PROJECTS_DIR, old_name)
        new_path = os.path.join(PROJECTS_DIR, new_name)

        try:
            os.rename(old_path, new_path)
        except:
            return False
        else:
            return True

    def exportProjectData(self, project_name, data, file_type):
        '''
        '''
        df = pd.DataFrame(data)
        path = os.path.join(PROJECTS_DIR, project_name, 'Reports', QDate.currentDate().toString("MM-dd-yy") + f'_Report.{file_type}')

        try:
            if file_type == 'csv':
                df.to_csv(path, index=False)
            elif file_type == 'json':
                df.to_json(path)
            elif file_type == 'xlsx':
                df.to_excel(path, index=False)
            elif file_type == 'html':
                df.to_html(path, index=False)
            else:
                raise ValueError
        except:
            return False
        else:
            return True

    def getProjectData(self, project_name):

        path = os.path.join(PROJECTS_DIR, project_name, 'project_data.json')

        try:
            with open(path, 'r') as f:
                data = json.loads(f.read())
        except:
            return False
        else:
            return data

    def getAllProjectData(self):

        project_names = self.getProjects()
        data = []

        for name in project_names:
            data.append(self.getProjectData(name))

        return data

    def getProjects(self):
        '''
        Return list of project names within Projects directory
        '''
        return [f.name for f in os.scandir(PROJECTS_DIR) if f.is_dir()]


import matplotlib.pyplot as plt
import numpy as np

v1 = np.linspace(0, 10, 100)
# print(v1)
f1 = 1/10 + v1*0
# print(f1)

v2 = np.linspace(3, 10, 70)
f2 = (1/10)*(v2/np.sqrt(v2**2+3**2))

v3 = np.linspace(6, 10, 40)
f3 = (1/10)*(v3/np.sqrt(v3**2+6**2))

v4 = np.linspace(1, 10, 90)
f4 = (1/10)*(v4/np.sqrt(v4**2+1**2))

plt.scatter(v1, f1, s=6)
plt.scatter(v2, f2, s=6)
plt.scatter(v3, f3, s=6)
plt.scatter(v4, f4, s=6)

plt.show()


import unittest
import logging
import time, datetime
import thespian.test.helpers
from thespian.actors import *
from thespian.test import TestSystem

class EchoActor(Actor):
    def receiveMessage(self, msg, sender):
        logging.info('EchoActor got %s (%s) from %s', msg, type(msg), sender)
        self.send(sender, msg)

class Kill_The_Messenger(Actor):
    def receiveMessage(self, message, sender):
        self.send(sender, ActorExitRequest())

class FakeSystemMessage(ActorSystemMessage):
    pass

smallwait = datetime.timedelta(milliseconds=50)

class TestASimpleSystem(unittest.TestCase):
    testbase='Simple'
    scope='func'

    sysbase = 'simpleSystemBase'
    portbase = 17100

    def testCreateActorSystem(self):
        with TestSystem(self.sysbase, {'Admin Port': self.portbase+1}) as asys:
            pass

    def testSimpleActor(self):
        with TestSystem(self.sysbase, {'Admin Port': self.portbase+2}) as asys:
            echo = asys.createActor(EchoActor)

    def testSimpleMessageTell(self):
        with TestSystem(self.sysbase, {'Admin Port': self.portbase+3}) as asys:
            echo = asys.createActor(EchoActor)
            asys.tell(echo, 'hello')
            time.sleep(0.02)  # allow tell to work before ActorSystem shutdown

    def testSystemMessageTell(self):
        with TestSystem(self.sysbase, {'Admin Port': self.portbase+4}) as asys:
            echo = asys.createActor(EchoActor)
            asys.tell(echo, FakeSystemMessage())
            time.sleep(0.02)  # allow tell to work before ActorSystem shutdown

    def testKillMessageTell(self):
        with TestSystem(self.sysbase, {'Admin Port': self.portbase+5}) as asys:
            echo = asys.createActor(EchoActor)
            asys.tell(echo, ActorExitRequest())
            time.sleep(0.02)  # allow tell to work before ActorSystem shutdown

    def testKillMessageTellKiller(self):
        with TestSystem(self.sysbase, {'Admin Port': self.portbase+6}) as asys:
            ktm = asys.createActor(Kill_The_Messenger)
            asys.tell(ktm, 'hello')
            asys.tell(ktm, ActorExitRequest())
            time.sleep(0.02)  # allow tell to work before ActorSystem shutdown

    def testSimpleMessageAsk(self):
        with TestSystem(self.sysbase, {'Admin Port': self.portbase+7}) as asys:
            echo = asys.createActor(EchoActor)
            self.assertEqual(asys.ask(echo, 'hello', smallwait), 'hello')

    def testSystemMessageAsk(self):
        with TestSystem(self.sysbase, {'Admin Port': self.portbase+8}) as asys:
            echo = asys.createActor(EchoActor)
            # SystemMessages are explicitly filtered from being returned
            # via Ask() or Tell(), with the exception of PoisonMessage.
            self.assertIsNone(asys.ask(echo, FakeSystemMessage(), smallwait))

    def testKillMessageAsk(self):
        with TestSystem(self.sysbase, {'Admin Port': self.portbase+9}) as asys:
            echo = asys.createActor(EchoActor)
            # SystemMessages are explicitly filtered from being returned
            # via Ask() or Tell(), with the exception of PoisonMessage.
            self.assertIsNone(asys.ask(echo, ActorExitRequest(), smallwait))

    def testKillMessageAskKiller(self):
        with TestSystem(self.sysbase, {'Admin Port': self.portbase+10}) as asys:
            ktm = asys.createActor(Kill_The_Messenger)
            self.assertIsNone(asys.ask(ktm, 'hello', smallwait))
            self.assertIsNone(asys.ask(ktm, ActorExitRequest(), smallwait))

class TestMultiprocUDPSystem(TestASimpleSystem):
    testbase='MultiprocUDP'
    sysbase = 'multiprocUDPBase'
    portbase = 17120

class TestMultiprocTCPSystem(TestASimpleSystem):
    testbase='MultiprocTCP'
    sysbase = 'multiprocTCPBase'
    portbase = 17140

class TestMultiprocQueueSystem(TestASimpleSystem):
    testbase='MultiprocQueue'
    sysbase = 'multiprocQueueBase'
    portbase = 17160


import sys
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [
    Extension("nerven.epoc._parse", ["nerven/epoc/_parse.pyx"])
]

setup(name='nerven',
      version='0.1',
      author='Sharif Olorin',
      author_email='sio@tesser.org',
      requires=[
          'wxmpl',
          'numpy',
          ],
      cmdclass={'build_ext' : build_ext},
      ext_modules=ext_modules,
      package_dir={'' : ''},
      packages=['nerven', 'nerven.epoc', 'nerven.writer'],
      package_data={'nerven' : ['img/*.png']},
      scripts=['nerven_gui'],
      data_files=[('bin', ['nerven_gui'])],
      )


# -*- coding: utf8 -*-fr

"""
ItopapiIncident is an abstraction of a Incident representation on iTop
"""

from itopapi.model.prototype import ItopapiPrototype
from itopapi.model.features.hasOrganization import HasOrganization
from itopapi.model.features.hasTeam import HasTeam
from itopapi.model.features.hasAgent import HasAgent

__version__ = '1.0'
__authors__ = ['Julien Nauroy <julien.nauroy@u-psud.fr>']

class ItopapiIncident(ItopapiPrototype, HasOrganization, HasTeam, HasAgent):

    # Configuration specific to itop
    itop = {
        # Name of the class in Itop
        'name': 'Incident',
        # Define which fields to save when creating or updating from the python API
        'save': ['title', 'description', 'ref', 'start_date', 'end_date', 'close_date', 'last_update'],
        'foreign_keys': [
            HasOrganization.foreign_key,
            HasAgent.foreign_key,
            HasTeam.foreign_key,
        ],
        'list_types': {
            'functionalcis_list': 'functionalci_id_finalclass_recall',
            'contacts_list': 'contact_id_finalclass_recall'
        },
    }

    @staticmethod
    def find(key):
        """ Retrieve one or more instance of PhysicalInterface with the given key or criteria """
        return ItopapiPrototype.find(ItopapiIncident, key)

    @staticmethod
    def find_by_name(name):
        return ItopapiPrototype.find_by_name(ItopapiIncident, name)

    @staticmethod
    def find_all():
        """ Retrieve all instance of Person """
        return ItopapiPrototype.find_all(ItopapiIncident)

    """
    ItopapiPerson is an object that represents a Person from iTop
    """
    def __init__(self, data=None):
        super(ItopapiIncident, self).__init__(data)
        self.title = None
        self.friendlyname = None
        self.description = None
        self.ref = None
        self.start_date = None
        self.end_date = None
        self.close_date = None
        self.last_update = None

        ##################################
        #             Lists              #
        ##################################
        self.functionalcis_list = None
        self.contacts_list = None
        self.workers_list = None
        self.private_log = None


import random
from datetime import datetime, timedelta

def all_even_numbers():
    for i in range(0,100,2):
        yield i

def random_increasing_number(start_from = 0):
    next_num = random.randint(start_from, start_from + 100)
    while True:
        next_num = random.randint(next_num, next_num+100)
        yield next_num

def next_day():
    day = datetime.today().date()

    while True:
        yield day
        day += timedelta(days=1)


"""
CDICorporation spider created on the top of ATSSpider

scrapy crawl cdicorporation -a mining_job_id=9999 -a iteration=1 -a extract=1 -a url="https://cdicorporation.bullhorncloud.com/portal/jobBoardPublic/jobListPublic"

Sample URL:
    https://cdicorporation.bullhorncloud.com/portal/jobBoardPublic/jobListPublic
"""

from json import loads
from math import ceil
from scrapy.http import FormRequest, Request
from scrapy.selector import Selector
from urlparse import urljoin

from brightcorp.base.atsspiders import ATSSpider
from brightcorp.items import BrightcorpItemLoader
from brightcorp.processors import HtmlFormatter, NormalizedJoin, Prefix, Replace

class CDICorporation(ATSSpider):

    name = 'cdicorporation'
    page = 1
    total_pages = 0
    form_data = {
        'bRegex': 'false',
        'bRegex_0': 'false',
        'bRegex_1': 'false',
        'bRegex_2': 'false',
        'bRegex_3': 'false',
        'bRegex_4': 'false',
        'bRegex_5': 'false',
        'bSearchable_0': 'true',
        'bSearchable_1': 'true',
        'bSearchable_2': 'true',
        'bSearchable_3': 'true',
        'bSearchable_4': 'true',
        'bSearchable_5': 'true',
        'bSortable_0': 'true',
        'bSortable_1': 'true',
        'bSortable_2': 'true',
        'bSortable_3': 'true',
        'bSortable_4': 'true',
        'bSortable_5': 'false',
        'iColumns': '6',
        'iDisplayLength': '10',
        'iDisplayStart': '0',
        'iSortCol_0': '0',
        'iSortingCols': '1',
        'mDataProp_0': '0',
        'mDataProp_1': '1',
        'mDataProp_2': '2',
        'mDataProp_3': '3',
        'mDataProp_4': '4',
        'mDataProp_5': '5',
        'sColumns': '',
        'sEcho': '1',
        'sSearch': '',
        'sSearch_0': '',
        'sSearch_1': '',
        'sSearch_2': '',
        'sSearch_3': '',
        'sSearch_4': '',
        'sSearch_5': '',
        'sSortDir_0': 'asc',
    }

    def parse(self, response):
        yield FormRequest(
            callback=self.parse_jobs_list,
            formdata=self.form_data,
            url=urljoin(
                response.url,
                '/portal/jobBoardPublic/data?queryEntity='
            )
        )

    def parse_jobs_list(self, response):
        jsonResponse = loads(response.body.decode('unicode_escape'))
        if jsonResponse:
            # set expected job count
            if not self.expected_job_count_set:
                total_records = jsonResponse.get('iTotalRecords')
                self.total_pages = int(ceil(int(total_records) / 10.0))
                self.expected_job_count = total_records

            results = jsonResponse.get('aaData')
            if results:
                for item in results:
                    job_id = item[0]
                    if job_id:
                        href = '/portal/jobBoardPublic/jobDetailsPublic?jobOrderID=%s' % job_id
                        yield Request(
                            callback=self.parse_job_callback(),
                            meta={
                                'jobid': job_id,
                                'title': item[1],
                                'city': item[2],
                                'state': item[3],
                                'country': item[4],
                            },
                            url=urljoin(response.url, href)
                        )
            if self.page <= self.total_pages:
                self.page += 1
                count = (self.page - 1) * 10
                self.form_data.update({
                    'sEcho': str(self.page),
                    'iDisplayStart': str(count),
                })
                yield FormRequest(
                    callback=self.parse_jobs_list,
                    formdata=self.form_data,
                    url=urljoin(
                        response.url,
                        '/portal/jobBoardPublic/data?queryEntity='
                    )
                )

    def parse_job(self, response):
        """
        Extract all required information.
        """
        sel = Selector(response)

        loader = BrightcorpItemLoader(selector=sel)
        loader.add_value(
            'title', response.meta.get('title')
        )
        loader.add_value(
            'location',
            [
                response.meta.get('city'),
                response.meta.get('state'),
                response.meta.get('country'),
            ],
            NormalizedJoin(', '),
            Replace('Not Listed')
        )
        loader.add_value(
            'referencenumber',
            response.meta.get('jobid'),
            Prefix('%s-' % self.name)
        )
        loader.add_value('url', response.url)
        loader.add_xpath(
            'description',
            '//tr[@id="publicDescription"]',
            HtmlFormatter()
        )
        loader.add_value('apply_url', response.url)

        yield loader.load_item()


# Orca
#
# Copyright 2005-2009 Sun Microsystems Inc.
# Copyright 2010-2011 Orca Team
# Copyright 2011-2015 Igalia, S.L.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
# Boston MA  02110-1301 USA.

__id__        = "$Id$"
__version__   = "$Revision$"
__date__      = "$Date$"
__copyright__ = "Copyright (c) 2005-2009 Sun Microsystems Inc." \
                "Copyright (c) 2010-2011 Orca Team" \
                "Copyright (c) 2011-2015 Igalia, S.L."
__license__   = "LGPL"

import pyatspi
import urllib

from orca import debug
from orca import messages
from orca import object_properties
from orca import orca_state
from orca import settings_manager
from orca import speech_generator

_settingsManager = settings_manager.getManager()

class SpeechGenerator(speech_generator.SpeechGenerator):

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

    def _generateAncestors(self, obj, **args):
        if not self._script.utilities.inDocumentContent(obj):
            return super()._generateAncestors(obj, **args)

        role = args.get('role', obj.getRole())
        if role == pyatspi.ROLE_LINK:
            return []

        if self._script.utilities.isLandmark(obj):
            return []

        if self._script.utilities.isMath(obj):
            return []

        args['stopAtRoles'] = [pyatspi.ROLE_DOCUMENT_FRAME,
                               pyatspi.ROLE_DOCUMENT_WEB,
                               pyatspi.ROLE_EMBEDDED,
                               pyatspi.ROLE_INTERNAL_FRAME,
                               pyatspi.ROLE_FORM,
                               pyatspi.ROLE_MATH,
                               pyatspi.ROLE_MENU_BAR,
                               pyatspi.ROLE_TOOL_BAR]
        args['skipRoles'] = [pyatspi.ROLE_PARAGRAPH,
                             pyatspi.ROLE_LABEL,
                             pyatspi.ROLE_LIST_ITEM,
                             pyatspi.ROLE_TEXT]

        return super()._generateAncestors(obj, **args)

    def _generateAllTextSelection(self, obj, **args):
        if self._script.utilities.isZombie(obj) \
           or obj != orca_state.locusOfFocus:
            return []

        # TODO - JD: These (and the default script's) need to
        # call utility methods rather than generate it.
        return super()._generateAllTextSelection(obj, **args)

    def _generateAnyTextSelection(self, obj, **args):
        if self._script.utilities.isZombie(obj) \
           or obj != orca_state.locusOfFocus:
            return []

        # TODO - JD: These (and the default script's) need to
        # call utility methods rather than generate it.
        return super()._generateAnyTextSelection(obj, **args)

    def _generateClickable(self, obj, **args):
        if not self._script.utilities.inDocumentContent(obj):
            return []

        if not args.get('mode', None):
            args['mode'] = self._mode

        args['stringType'] = 'clickable'
        if self._script.utilities.isClickableElement(obj):
            result = [self._script.formatting.getString(**args)]
            result.extend(self.voice(speech_generator.SYSTEM))
            return result

        return []

    def _generateDescription(self, obj, **args):
        if self._script.utilities.isZombie(obj) \
           or obj != orca_state.locusOfFocus:
            return []

        formatType = args.get('formatType')
        if formatType == 'basicWhereAmI' and self._script.utilities.isLiveRegion(obj):
            return self._script.liveRegionManager.generateLiveRegionDescription(obj, **args)

        role = args.get('role', obj.getRole())
        if role == pyatspi.ROLE_TEXT and formatType != 'basicWhereAmI':
            return []

        # TODO - JD: This is private.
        if role == pyatspi.ROLE_LINK and self._script._lastCommandWasCaretNav:
            return []

        return super()._generateDescription(obj, **args)

    def _generateHasLongDesc(self, obj, **args):
        if not self._script.utilities.inDocumentContent(obj):
            return []

        if not args.get('mode', None):
            args['mode'] = self._mode

        args['stringType'] = 'haslongdesc'
        if self._script.utilities.hasLongDesc(obj):
            result = [self._script.formatting.getString(**args)]
            result.extend(self.voice(speech_generator.SYSTEM))
            return result

        return []

    def _generateLabelOrName(self, obj, **args):
        if self._script.utilities.isTextBlockElement(obj):
            return []

        if self._script.utilities.inDocumentContent(obj) and obj.name:
            result = [obj.name]
            result.extend(self.voice(speech_generator.DEFAULT))
            return result

        return super()._generateLabelOrName(obj, **args)

    def _generateLabel(self, obj, **args):
        if self._script.utilities.isTextBlockElement(obj):
            return []

        label, objects = self._script.utilities.inferLabelFor(obj)
        if label:
            result = [label]
            result.extend(self.voice(speech_generator.DEFAULT))
            return result

        return super()._generateLabel(obj, **args)

    def _generateNewNodeLevel(self, obj, **args):
        if self._script.utilities.isTextBlockElement(obj) \
           or self._script.utilities.isLink(obj):
            return []

        return super()._generateNewNodeLevel(obj, **args)

    def _generateNewRadioButtonGroup(self, obj, **args):
        # TODO - JD: Looking at the default speech generator's method, this
        # is all kinds of broken. Until that can be sorted out, try to filter
        # out some of the noise....
        return []

    def _generateNumberOfChildren(self, obj, **args):
        if _settingsManager.getSetting('onlySpeakDisplayedText'):
            return []

        # We handle things even for non-document content due to issues in
        # other toolkits (e.g. exposing list items to us that are not
        # exposed to sighted users)
        role = args.get('role', obj.getRole())
        if role not in [pyatspi.ROLE_LIST, pyatspi.ROLE_LIST_BOX]:
            return super()._generateNumberOfChildren(obj, **args)

        children = [x for x in obj if x.getRole() == pyatspi.ROLE_LIST_ITEM]
        if not children:
            return []

        result = [messages.listItemCount(len(children))]
        result.extend(self.voice(speech_generator.SYSTEM))
        return result

    # TODO - JD: Yet another dumb generator method we should kill.
    def _generateTextRole(self, obj, **args):
        return self._generateRoleName(obj, **args)

    def getLocalizedRoleName(self, obj, role=None):
        if not self._script.utilities.inDocumentContent(obj):
            return super().getLocalizedRoleName(obj, role)

        roledescription = self._script.utilities.getRoleDescription(obj)
        if roledescription:
            return roledescription

        return super().getLocalizedRoleName(obj, role)

    def _generateRoleName(self, obj, **args):
        if not self._script.utilities.inDocumentContent(obj):
            return super()._generateRoleName(obj, **args)

        result = []
        acss = self.voice(speech_generator.SYSTEM)

        roledescription = self._script.utilities.getRoleDescription(obj)
        if roledescription:
            result = [roledescription]
            result.extend(acss)
            return result

        role = args.get('role', obj.getRole())
        force = args.get('force', False)

        start = args.get('startOffset')
        end = args.get('endOffset')

        if not force:
            doNotSpeak = [pyatspi.ROLE_FOOTER,
                          pyatspi.ROLE_FORM,
                          pyatspi.ROLE_LABEL,
                          pyatspi.ROLE_MENU_ITEM,
                          pyatspi.ROLE_PARAGRAPH,
                          pyatspi.ROLE_SECTION,
                          pyatspi.ROLE_UNKNOWN]
        else:
            doNotSpeak = [pyatspi.ROLE_UNKNOWN]

        if not force:
            doNotSpeak.append(pyatspi.ROLE_TABLE_CELL)
            doNotSpeak.append(pyatspi.ROLE_TEXT)
            doNotSpeak.append('ROLE_STATIC')
            if args.get('formatType', 'unfocused') != 'basicWhereAmI':
                doNotSpeak.append(pyatspi.ROLE_LIST_ITEM)
                doNotSpeak.append(pyatspi.ROLE_LIST)
            if (start or end):
                doNotSpeak.append(pyatspi.ROLE_DOCUMENT_FRAME)
                doNotSpeak.append(pyatspi.ROLE_ALERT)

        if obj.getState().contains(pyatspi.STATE_EDITABLE):
            lastKey, mods = self._script.utilities.lastKeyAndModifiers()
            if ((lastKey in ["Down", "Right"] and not mods) or self._script.inSayAll()) and start:
                return []
            if lastKey in ["Up", "Left"] and not mods:
                text = self._script.utilities.queryNonEmptyText(obj)
                if text and end not in [None, text.characterCount]:
                    return []
            if role not in doNotSpeak:
                result.append(self.getLocalizedRoleName(obj, role))
                result.extend(acss)

        elif role == pyatspi.ROLE_HEADING:
            level = self._script.utilities.headingLevel(obj)
            if level:
                result.append(object_properties.ROLE_HEADING_LEVEL_SPEECH % {
                    'role': self.getLocalizedRoleName(obj, role),
                    'level': level})
                result.extend(acss)
            else:
                result.append(self.getLocalizedRoleName(obj, role))
                result.extend(acss)

        elif role == pyatspi.ROLE_LINK:
            if obj.parent.getRole() == pyatspi.ROLE_IMAGE:
                result.append(messages.IMAGE_MAP_LINK)
                result.extend(acss)
            else:
                if self._script.utilities.hasUselessCanvasDescendant(obj):
                    result.append(self.getLocalizedRoleName(obj, pyatspi.ROLE_IMAGE))
                    result.extend(acss)
                result.append(self.getLocalizedRoleName(obj, role))
                result.extend(acss)

        elif role not in doNotSpeak:
            result.append(self.getLocalizedRoleName(obj, role))
            result.extend(acss)

        index = args.get('index', 0)
        total = args.get('total', 1)
        ancestorRoles = [pyatspi.ROLE_HEADING, pyatspi.ROLE_LINK]
        if index == total - 1 \
           and (role == pyatspi.ROLE_IMAGE or self._script.utilities.queryNonEmptyText(obj)):
            speakRoles = lambda x: x and x.getRole() in ancestorRoles
            ancestor = pyatspi.findAncestor(obj, speakRoles)
            if ancestor and ancestor.getRole() != role:
                result.extend(self._generateRoleName(ancestor))

        return result

    def _generatePageSummary(self, obj, **args):
        if not self._script.utilities.inDocumentContent(obj):
            return []

        result = []
        acss = self.voice(speech_generator.DEFAULT)
        headings, forms, tables, vlinks, uvlinks, percent = \
            self._script.utilities.getPageSummary(obj)
        if headings:
            result.append(messages.headingCount(headings))
        if forms:
            result.append(messages.formCount(forms))
        if tables:
            result.append(messages.tableCount(tables))
        if vlinks:
            result.append(messages.visitedLinkCount(vlinks))
        if uvlinks:
            result.append(messages.unvisitedLinkCount(uvlinks))
        if percent is not None:
            result.append(messages.percentRead(percent))

        if result:
            result.extend(acss)
        return result

    def _generateSiteDescription(self, obj, **args):
        if not self._script.utilities.inDocumentContent(obj):
            return []

        link_uri = self._script.utilities.uri(obj)
        if not link_uri:
            return []

        link_uri_info = urllib.parse.urlparse(link_uri)
        doc_uri = self._script.utilities.documentFrameURI()
        if not doc_uri:
            return []

        result = []
        doc_uri_info = urllib.parse.urlparse(doc_uri)
        if link_uri_info[1] == doc_uri_info[1]:
            if link_uri_info[2] == doc_uri_info[2]:
                result.append(messages.LINK_SAME_PAGE)
            else:
                result.append(messages.LINK_SAME_SITE)
        else:
            linkdomain = link_uri_info[1].split('.')
            docdomain = doc_uri_info[1].split('.')
            if len(linkdomain) > 1 and len(docdomain) > 1  \
               and linkdomain[-1] == docdomain[-1]  \
               and linkdomain[-2] == docdomain[-2]:
                result.append(messages.LINK_SAME_SITE)
            else:
                result.append(messages.LINK_DIFFERENT_SITE)

        if result:
            result.extend(self.voice(speech_generator.HYPERLINK))

        return result

    def _generateExpandedEOCs(self, obj, **args):
        if not self._script.utilities.inDocumentContent(obj):
            return super()._generateExpandedEOCs(obj, **args)

        result = []
        startOffset = args.get('startOffset', 0)
        endOffset = args.get('endOffset', -1)
        text = self._script.utilities.expandEOCs(obj, startOffset, endOffset)
        if text:
            result.append(text)
        return result

    def _generatePositionInList(self, obj, **args):
        if _settingsManager.getSetting('onlySpeakDisplayedText'):
            return []

        if not args.get('forceList', False) \
           and not _settingsManager.getSetting('enablePositionSpeaking'):
            return []

        # TODO - JD: We cannot do this for XUL (or whatever Firefox is
        # using in its non-webcontent dialogs)
        #if not self._script.utilities.inDocumentContent(obj):
        #    return super()._generatePositionInList(obj, **args)
        menuRoles = [pyatspi.ROLE_MENU_ITEM,
                     pyatspi.ROLE_TEAROFF_MENU_ITEM,
                     pyatspi.ROLE_CHECK_MENU_ITEM,
                     pyatspi.ROLE_RADIO_MENU_ITEM,
                     pyatspi.ROLE_MENU]
        if obj.getRole() in menuRoles:
            return super()._generatePositionInList(obj, **args)

        if self._script.utilities.isTextBlockElement(obj):
            return []

        try:
            attrs = dict([attr.split(':', 1) for attr in obj.getAttributes()])
        except:
            attrs = {}

        position = attrs.get("posinset")
        total = attrs.get("setsize")
        if position is None or total is None:
            return super()._generatePositionInList(obj, **args)

        position = int(position)
        total = int(total)
        if position < 0 or total < 0:
            return []

        result = []
        result.append(self._script.formatting.getString(
            mode='speech',
            stringType='groupindex') \
            % {"index" : position,
               "total" : total})
        result.extend(self.voice(speech_generator.SYSTEM))
        return result

    def generateSpeech(self, obj, **args):
        if not self._script.utilities.inDocumentContent(obj):
            msg = "\nINFO: %s is not in document content. Calling default speech generator." % obj
            debug.println(debug.LEVEL_INFO, msg)
            return super().generateSpeech(obj, **args)

        msg = "\nINFO: Generating speech for document object %s" % obj
        debug.println(debug.LEVEL_INFO, msg)

        result = []
        if args.get('formatType') == 'detailedWhereAmI':
            oldRole = self._overrideRole('default', args)
        elif self._script.utilities.isLink(obj):
            oldRole = self._overrideRole(pyatspi.ROLE_LINK, args)
        elif self._script.utilities.treatAsDiv(obj):
            oldRole = self._overrideRole(pyatspi.ROLE_SECTION, args)
        else:
            oldRole = self._overrideRole(self._getAlternativeRole(obj, **args), args)

        result.extend(super().generateSpeech(obj, **args))
        result = list(filter(lambda x: x, result))
        self._restoreRole(oldRole, args)

        msg = "\nINFO: Speech generation for document object %s complete:\n%s\n" % (obj, result)
        debug.println(debug.LEVEL_INFO, msg)
        return result

    def generateContents(self, contents, **args):
        if not len(contents):
            return []

        result = []
        contents = self._script.utilities.filterContentsForPresentation(contents, False)
        msg = "INFO: Generating speech contents (length: %i)" % len(contents)
        debug.println(debug.LEVEL_INFO, msg)
        for i, content in enumerate(contents):
            obj, start, end, string = content
            msg = "ITEM %i: %s, start: %i, end: %i, string: '%s'" \
                  % (i, obj, start, end, string)
            debug.println(debug.LEVEL_INFO, msg)
            utterance = self.generateSpeech(
                obj, startOffset=start, endOffset=end, string=string,
                index=i, total=len(contents), **args)
            if utterance and utterance[0]:
                result.append(utterance)

        if not result:
            if self._script.inSayAll():
                string = ""
            else:
                string = messages.BLANK
            result = [string, self.voice(speech_generator.DEFAULT)]

        return result


# Note: These views are public with the exception of the response_view
# which has "secure" parts to it in the template.

import json
from datetime import datetime, timedelta

from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render

from elasticutils.contrib.django import F, es_required_or_50x
from funfactory.urlresolvers import reverse
from mobility.decorators import mobile_template
from tower import ugettext as _

from fjord.analytics.tools import (
    JSONDatetimeEncoder,
    generate_query_parsed,
    counts_to_options,
    zero_fill)
from fjord.base.helpers import locale_name
from fjord.base.util import (
    check_new_user,
    smart_int,
    smart_date,
    Atom1FeedWithRelatedLinks)
from fjord.feedback.models import Response, ResponseMappingType

@check_new_user
@mobile_template('analytics/{mobile/}response.html')
def response_view(request, responseid, template):
    response = get_object_or_404(Response, id=responseid)

    # We don't pass the response directly to the template and instead
    # do some data tweaks here to make it more palatable for viewing.
    return render(request, template, {
        'response': response,
    })

def generate_json_feed(request, search):
    """Generates JSON feed for first 100 results"""
    search_query = request.GET.get('q', None)
    responses = search.values_dict()[:100]
    json_data = {
        'total': len(responses),
        'results': list(responses),
        'query': search_query
    }
    return HttpResponse(
        json.dumps(json_data, cls=JSONDatetimeEncoder),
        mimetype='application/json')

def generate_atom_feed(request, search):
    """Generates ATOM feed for first 100 results"""
    search_query = request.GET.get('q', None)

    if search_query:
        title = _(u'Firefox Input: {query}').format(query=search_query)
    else:
        title = _(u'Firefox Input')

    # Build the non-atom dashboard url and maintain all the
    # querystring stuff we have
    dashboard_url = request.build_absolute_uri()
    dashboard_url = dashboard_url.replace('format=atom', '')
    dashboard_url = dashboard_url.replace('&&', '&')
    if dashboard_url.endswith(('?', '&')):
        dashboard_url = dashboard_url[:-1]

    feed = Atom1FeedWithRelatedLinks(
        title=title,
        link=dashboard_url,
        description=_('Search Results From Firefox Input'),
        author_name=_('Firefox Input'),
    )
    for response in search[:100]:
        categories = {
            'sentiment': _('Happy') if response.happy else _('Sad'),
            'platform': response.platform,
            'locale': response.locale
        }
        categories = (':'.join(item) for item in categories.items())

        link_url = reverse('response_view', args=(response.id,))
        link_url = request.build_absolute_uri(link_url)

        feed.add_item(
            title=_('Response id: {id}').format(id=response.id),
            description=response.description,
            link=link_url,
            pubdate=response.created,
            categories=categories,
            link_related=response.url_domain,
        )
    return HttpResponse(
        feed.writeString('utf-8'), mimetype='application/atom+xml')

def generate_dashboard_url(request, output_format='atom',
                                viewname='dashboard'):
    """For a given request, generates the dashboard url for the given format"""
    qd = request.GET.copy()

    # Remove anything from the querystring that isn't good for a feed:
    # page, start_date, end_date, etc.
    for mem in qd.keys():
        if mem not in ('happy', 'locale', 'platform', 'product',
                       'version', 'q'):
            del qd[mem]

    qd['format'] = output_format

    return reverse(viewname) + '?' + qd.urlencode()

@check_new_user
@es_required_or_50x(error_template='analytics/es_down.html')
def dashboard(request):
    template = 'analytics/dashboard.html'

    output_format = request.GET.get('format', None)
    page = smart_int(request.GET.get('page', 1), 1)

    # Note: If we add additional querystring fields, we need to add
    # them to generate_dashboard_url.
    search_happy = request.GET.get('happy', None)
    search_platform = request.GET.get('platform', None)
    search_locale = request.GET.get('locale', None)
    search_product = request.GET.get('product', None)
    search_version = request.GET.get('version', None)
    search_query = request.GET.get('q', None)
    search_date_start = smart_date(
        request.GET.get('date_start', None), fallback=None)
    search_date_end = smart_date(
        request.GET.get('date_end', None), fallback=None)
    search_bigram = request.GET.get('bigram', None)
    selected = request.GET.get('selected', None)

    filter_data = []
    current_search = {'page': page}

    search = ResponseMappingType.search()
    f = F()
    # If search happy is '0' or '1', set it to False or True, respectively.
    search_happy = {'0': False, '1': True}.get(search_happy, None)
    if search_happy in [False, True]:
        f &= F(happy=search_happy)
        current_search['happy'] = int(search_happy)

    def unknown_to_empty(text):
        """Convert "Unknown" to "" to support old links"""
        return u'' if text.lower() == u'unknown' else text

    if search_platform is not None:
        f &= F(platform=unknown_to_empty(search_platform))
        current_search['platform'] = search_platform
    if search_locale is not None:
        f &= F(locale=unknown_to_empty(search_locale))
        current_search['locale'] = search_locale
    if search_product is not None:
        f &= F(product=unknown_to_empty(search_product))
        current_search['product'] = search_product

        if search_version is not None:
            # Note: We only filter on version if we're filtering on
            # product.
            f &= F(version=unknown_to_empty(search_version))
            current_search['version'] = search_version

    if search_date_start is None and search_date_end is None:
        selected = '7d'

    if search_date_end is None:
        search_date_end = datetime.now()
    if search_date_start is None:
        search_date_start = search_date_end - timedelta(days=7)

    current_search['date_end'] = search_date_end.strftime('%Y-%m-%d')
    # Add one day, so that the search range includes the entire day.
    end = search_date_end + timedelta(days=1)
    # Note 'less than', not 'less than or equal', because of the added
    # day above.
    f &= F(created__lt=end)

    current_search['date_start'] = search_date_start.strftime('%Y-%m-%d')
    f &= F(created__gte=search_date_start)

    if search_query:
        current_search['q'] = search_query
        es_query = generate_query_parsed('description', search_query)
        search = search.query_raw(es_query)

    if search_bigram is not None:
        f &= F(description_bigrams=search_bigram)
        filter_data.append({
            'display': _('Bigram'),
            'name': 'bigram',
            'options': [{
                'count': 'all',
                'name': search_bigram,
                'display': search_bigram,
                'value': search_bigram,
                'checked': True
            }]
        })

    search = search.filter(f).order_by('-created')

    # If the user asked for a feed, give him/her a feed!
    if output_format == 'atom':
        return generate_atom_feed(request, search)

    elif output_format == 'json':
        return generate_json_feed(request, search)

    # Search results and pagination
    if page < 1:
        page = 1
    page_count = 20
    start = page_count * (page - 1)
    end = start + page_count

    search_count = search.count()
    opinion_page = search[start:end]

    # Navigation facet data
    facets = search.facet(
        'happy', 'platform', 'locale', 'product', 'version',
        filtered=bool(search._process_filters(f.filters)))

    # This loop does two things. First it maps 'T' -> True and 'F' ->
    # False.  This is probably something EU should be doing for
    # us. Second, it restructures the data into a more convenient
    # form.
    counts = {
        'happy': {},
        'platform': {},
        'locale': {},
        'product': {},
        'version': {}
    }
    for param, terms in facets.facet_counts().items():
        for term in terms:
            name = term['term']
            if name == 'T':
                name = True
            elif name == 'F':
                name = False

            counts[param][name] = term['count']

    def empty_to_unknown(text):
        return _('Unknown') if text == u'' else text

    filter_data.extend([
        counts_to_options(
            counts['happy'].items(),
            name='happy',
            display=_('Sentiment'),
            display_map={True: _('Happy'), False: _('Sad')},
            value_map={True: 1, False: 0},
            checked=search_happy),
        counts_to_options(
            counts['product'].items(),
            name='product',
            display=_('Product'),
            display_map=empty_to_unknown,
            checked=search_product)
    ])
    # Only show the version if we're showing a specific
    # product.
    if search_product:
        filter_data.append(
            counts_to_options(
                counts['version'].items(),
                name='version',
                display=_('Version'),
                display_map=empty_to_unknown,
                checked=search_version)
        )

    filter_data.extend(
        [
            counts_to_options(
                counts['platform'].items(),
                name='platform',
                display=_('Platform'),
                display_map=empty_to_unknown,
                checked=search_platform),
            counts_to_options(
                counts['locale'].items(),
                name='locale',
                display=_('Locale'),
                checked=search_locale,
                display_map=locale_name),
        ]
    )

    # Histogram data
    happy_data = []
    sad_data = []

    happy_f = f & F(happy=True)
    sad_f = f & F(happy=False)
    histograms = search.facet_raw(
        happy={
            'date_histogram': {'interval': 'day', 'field': 'created'},
            'facet_filter': search._process_filters(happy_f.filters)
        },
        sad={
            'date_histogram': {'interval': 'day', 'field': 'created'},
            'facet_filter': search._process_filters(sad_f.filters)
        },
    ).facet_counts()

    # p['time'] is number of milliseconds since the epoch. Which is
    # convenient, because that is what the front end wants.
    happy_data = dict((p['time'], p['count']) for p in histograms['happy'])
    sad_data = dict((p['time'], p['count']) for p in histograms['sad'])

    zero_fill(search_date_start, search_date_end, [happy_data, sad_data])
    histogram = [
        {'label': _('Happy'), 'name': 'happy',
         'data': sorted(happy_data.items())},
        {'label': _('Sad'), 'name': 'sad',
         'data': sorted(sad_data.items())},
    ]

    return render(request, template, {
        'opinions': opinion_page,
        'opinion_count': search_count,
        'filter_data': filter_data,
        'histogram': histogram,
        'page': page,
        'prev_page': page - 1 if start > 0 else None,
        'next_page': page + 1 if end < search_count else None,
        'current_search': current_search,
        'selected': selected,
        'atom_url': generate_dashboard_url(request),
    })


"""
https://leetcode.com/problems/sum-of-square-numbers
"""

class Solution(object):
    def judgeSquareSum(self, c):
        a = 0
        while a * a <= c:
            b = (c - (a * a)) ** 0.5
            if b == int(b):
                return True
            a += 1
        return False

for i in range(30):
    ans = Solution().judgeSquareSum(i)
    print('{}: {}'.format(i, ans))

ans = Solution().judgeSquareSum(1000)
print(ans)

# for i in range(30):
#     print('{}: {}'.format(i, int(i ** 0.5) ** 2))


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Investigation',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, verbose_name='ID', serialize=False)),
            ],
        ),
        migrations.CreateModel(
            name='Score',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, verbose_name='ID', serialize=False)),
                ('investigation', models.ForeignKey(default=None, to='scales.Investigation')),
            ],
        ),
    ]


# ref: http://air4thai.pcd.go.th/webV2/
aqi_tbl = [
    [0, 'great', 'blue'],
    [26, 'good', 'green'],
    [51, 'moderate', 'yellow'],
    [101, 'unhealthy', 'orange'],
    [201, 'very unhealthy', 'red']
]

grade_tbl = [
    [0, 'F'],
    [50, 'D'],
    [70, 'C'],
    [80, 'B'],
    [90, 'A']
]

def vlookup(v, tbl, col):
    # x = filter(lambda n: v >= n[0], tbl)
    # # print(list(x))
    # print(max(x)[col])
    # return 0
    # r = max(filter(lambda n: v >= n[0], tbl))
    # return r[col]
    return max(filter(lambda n: v >= n[0], tbl))[col]

if __name__ == "__main__":
    aqi=20
    print(vlookup(aqi, aqi_tbl, 2))
    # score = 81
    # print(vlookup(score, grade_tbl, 1))


class Solution:
    def findRelativeRanks(self, nums):
        """
        :type nums: List[int]
        :rtype: List[str]
        """
        length = len(nums)

        for i in range(length):
            nums[i] = [nums[i],i]
        listnums = sorted(nums,key = lambda x:x[0],reverse = True)

        for i in range(length):
            if i >= 3:
                listnums[i][0] = str(i + 1)
            elif i == 0:
                listnums[i][0] = "Gold Medal"
            elif i == 1:
                listnums[i][0] = "Silver Medal"
            elif i == 2:
                listnums[i][0] = "Bronze Medal"

        liststring = sorted(listnums,key = lambda x:x[1])
        result = []

        for i in range(length):
            result.append(liststring[i][0])

        return result


"""
Build SSW scripts from Jinja 2 templates
"""
import os
import datetime
import subprocess
import tempfile

from jinja2 import (Environment as Env,
                    FileSystemLoader,
                    PackageLoader)
from scipy.io import readsav

from .read_config import defaults
from .util import SSWIDLError

class Environment(object):
    """
    Environment for running SSW and IDL scripts

    Parameters
    ----------
    ssw_packages : list, optional
        List of SSW packages to load, e.g. 'sdo/aia', 'chianti'
    ssw_paths : list, optional
        List of SSW paths to pass to `ssw_path`
    extra_paths : list, optional
        Additional paths to add to the IDL namespace
    ssw_home : str, optional
        Root of SSW tree
    idl_home : str, optional
        Path to IDL executable
    """

    def __init__(self, ssw_packages=None, ssw_paths=None, extra_paths=None,
                 ssw_home=None, idl_home=None,):
        self.ssw_packages = ssw_packages if ssw_packages is not None else []
        self.ssw_paths = ssw_paths if ssw_paths is not None else []
        self.extra_paths = extra_paths if extra_paths is not None else []
        self.env = Env(loader=PackageLoader('hissw', 'templates'))
        self._setup_home(ssw_home, idl_home,)

    def _setup_home(self, ssw_home, idl_home,):
        """
        Setup SSW and IDL home locations
        """
        self.ssw_home = defaults['ssw_home'] if ssw_home is None else ssw_home
        if self.ssw_home is None:
            raise ValueError('''ssw_home must be set at instantiation or in the hisswrc file.''')
        self.idl_home = defaults['idl_home'] if idl_home is None else idl_home
        if self.idl_home is None:
            raise ValueError('''idl_home must be set at instantiation or in the hisswrc file.''')

    def custom_script(self, script, args):
        """
        Generate custom IDL scripts from templates
        """
        if os.path.isfile(script):
            env = Env(loader=FileSystemLoader(os.path.dirname(script)))
            idl_script = env.get_template(os.path.basename(script)).render(**args)
        else:
            env = Env()
            idl_script = env.from_string(script).render(**args)

        return idl_script

    def procedure_script(self, script, save_vars, save_filename):
        """
        Render inner procedure file
        """
        if save_vars is None:
            save_vars = []
        params = {'script': script, 'save_vars': save_vars, 'save_filename': save_filename}
        return self.env.get_template('procedure.pro').render(**params)

    def command_script(self, procedure_filename):
        """
        Generate parent IDL script
        """
        params = {'ssw_paths': self.ssw_paths,
                  'extra_paths': self.extra_paths,
                  'procedure_filename': procedure_filename}
        return self.env.get_template('parent.pro').render(**params)

    def shell_script(self, command_filename):
        """
        Generate shell script for starting up SSWIDL
        """
        params = {'ssw_home': self.ssw_home,
                  'ssw_packages': self.ssw_packages,
                  'idl_home': self.idl_home,
                  'command_filename': command_filename}
        return self.env.get_template('startup.sh').render(**params)

    def run(self, script, args=None, save_vars=None, verbose=True):
        """
        Set up the SSWIDL environment and run the supplied scripts.

        Parameters
        ----------
        script : str
            Literal script or path to script file
        args : dict, optional
            Input arguments to script
        save_vars : list, optional
            Variables to save and return from the IDL namespace
        verbose : bool, optional
        """
        args = {} if args is None else args
        with tempfile.TemporaryDirectory() as tmpdir:
            # Get filenames
            fn_template = os.path.join(
                tmpdir, '{name}_'+datetime.datetime.now().strftime('%Y%m%d-%H%M%S')+'.{ext}')
            save_filename = fn_template.format(name='idl_vars', ext='sav')
            procedure_filename = fn_template.format(name='idl_procedure', ext='pro')
            command_filename = fn_template.format(name='idl_script', ext='pro')
            shell_filename = fn_template.format(name='ssw_shell', ext='sh')
            # Render and save scripts
            idl_script = self.custom_script(script, args)
            with open(procedure_filename, 'w') as f:
                f.write(self.procedure_script(idl_script, save_vars, save_filename))
            with open(command_filename, 'w') as f:
                f.write(self.command_script(procedure_filename))
            with open(shell_filename, 'w') as f:
                f.write(self.shell_script(command_filename,))
            # Execute
            subprocess.call(['chmod', 'u+x', shell_filename])
            cmd_output = subprocess.run([shell_filename], shell=True, stderr=subprocess.PIPE,
                                        stdout=subprocess.PIPE)
            self._check_for_errors(cmd_output, verbose)
            results = readsav(save_filename)

        return results

    def _check_for_errors(self, output, verbose):
        """
        Check IDL output to try and decide if an error has occurred
        """
        stdout = output.stdout.decode('utf-8')
        stderr = output.stderr.decode('utf-8')
        # NOTE: For some reason, not only errors are output to stderr so we
        # have to check it for certain keywords to see if an error occurred
        if 'execution halted' in stderr.lower():
            raise SSWIDLError(stderr)
        if verbose:
            print(f'{stderr}\n{stdout}')


from src import modules as m
import numpy as np
from src import ofdm
from scipy.linalg import dft
from simulations.common import graph
from simulations.common import settings
import matplotlib.pyplot as plt

graph.init_graph()
dirname = "../results/ofdm/test"
settings.init_output(dirname)

params = {
    "block": 1000,
    "subcarrier": 10,
    "CP": 5,
    "chanel_len": 2,
    "SNR_MIN": 0,
    "SNR_MAX": 25,
    "SNR_NUM": 6,
    "SNR_AVERAGE": 100,
    "equalizer": "ZF",
}

settings.dump_params(params, dirname)

ofdm_zero = np.hstack((np.zeros((params["subcarrier"], params["CP"])), np.eye(params["subcarrier"])))

F = dft(params['subcarrier'], "sqrtn")
FH = F.conj().T

snrs_db = m.snr_db(params['SNR_MIN'], params['SNR_MAX'], params['SNR_NUM'])
sigmas = m.sigmas(snrs_db)
errors = np.zeros((params['SNR_NUM'], params['SNR_AVERAGE']))
q_errors = np.zeros((params['SNR_NUM'], params['SNR_AVERAGE']))

for trials_index in range(params['SNR_AVERAGE']):
    h_si = m.channel(1, params['chanel_len'])
    H = ofdm.toeplitz_channel(h_si.T, params['chanel_len'], params['subcarrier'], params['CP'])
    z_H = np.hstack((np.zeros((H.shape[0], params["subcarrier"] + params["CP"] - 3)), H))
    Hc = ofdm.circulant_channel(h_si.T, params['chanel_len'], params['subcarrier'])

    D = F @ Hc @ FH
    D_1 = np.linalg.inv(D)

    for sigma_index, sigma in enumerate(sigmas):
        d = np.random.choice([0, 1], (params['subcarrier'] * 2 * params['block'], 1))
        s_n = m.modulate_qpsk(d)
        s = s_n.reshape(params['subcarrier'], params['block'])
        x = np.matmul(FH, s)
        x_cp = ofdm.add_cp(x, params['CP'])

        x_receive = x_cp
        if params["chanel_len"] > 1:
            x_receive = np.zeros((params['chanel_len'] - 1 + x_cp.shape[0], x_cp.shape[1]), dtype=complex)
            x_receive[:(params["chanel_len"] - 1), 1:] = x_cp[-(params["chanel_len"] - 1):, :-1]
            x_receive[(params["chanel_len"] - 1):, :] = x_cp

        noise = m.awgn((params['subcarrier'] + params['CP'], params['block']), sigma)
        r = np.matmul(H, x_receive) + noise

        r_s = r.flatten()

        y_p = r_s.reshape((params['subcarrier'] + params['CP'], params['block']))
        y_remove_cp = np.matmul(ofdm_zero, y_p)
        y = np.matmul(F, y_remove_cp)

        s_hat = np.matmul(D_1, y)
        s_n_hat = s_hat.reshape(params['subcarrier'] * params['block'])
        d_hat = m.demodulate_qpsk(s_n_hat).reshape((params['subcarrier'] * 2 * params['block'], 1))
        error = np.sum(d != d_hat)

        errors[sigma_index][trials_index] = error

        ### QPSK
        # q_r = (h_si * s_n) + m.awgn((params['subcarrier'] * params['block'], 1), sigma)
        # q_r = q_r * h_si.conj() / (np.abs(h_si) ** 2)
        # q_d_hat = m.demodulate_qpsk(q_r.squeeze()).reshape((params['subcarrier'] * 2 * params['block'], 1))
        # q_error = np.sum(d != q_d_hat)
        #
        # q_errors[sigma_index][trials_index] = q_error

ber_fig, ber_ax = graph.new_snr_ber_canvas(params['SNR_MIN'], params['SNR_MAX'], -4, 0)
n_sum = params['subcarrier'] * 2 * params['block'] * params['SNR_AVERAGE']
errors_sum = np.sum(errors, axis=1)
bers = errors_sum / n_sum
ber_ax.plot(snrs_db, bers, color="k", marker='o', linestyle='--', label="OFDM(QPSK)")

errors_sum = np.sum(q_errors, axis=1)
bers = errors_sum / n_sum
ber_ax.plot(snrs_db, bers, color="b", marker='x', linestyle=':', label="QPSK")
ber_ax.legend()

plt.tight_layout()
# plt.savefig(dirname + '/SNR_BER.pdf', bbox_inches='tight')
plt.show()


from bs4 import BeautifulSoup
import requests
import shutil

urls = []
query = 'puppies'

class data_miner:

    def __init__(self, base_url):
        self.base_url = base_url

    def get_links(self, query):
        html=self.base_url
        payload={'q': query}
        r=requests.get(html, params=payload)
        soup=BeautifulSoup(r.content, "html5lib")
        for image in soup.find_all('img'):
            urls.append(image.attrs['src'])

    def download_files(self, query):
        number = 1
        for item in urls:
            with open('static/happy-images/' + query + str(number) + '.jpg', 'wb') as out_file:
                req = requests.get(item, stream=True)
                shutil.copyfileobj(req.raw, out_file)
                number += 1

x = data_miner('http://www.google.com/images')
x.get_links(query)
x.download_files(query)


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

from libmozdata.bugzilla import Bugzilla

from bugbot import utils
from bugbot.bzcleaner import BzCleaner
from bugbot.severity import Severity

class SeverityHigherDuplicates(BzCleaner):
    def __init__(self):
        super().__init__()
        self.extra_ni = None

    def description(self):
        return "Bugs with duplicates that have higher severity"

    def handle_bug(self, bug, data):
        bugid = str(bug["id"])
        data[bugid] = {
            "severity": Severity(bug["severity"]),
            "duplicates": bug["duplicates"],
        }

        return bug

    def get_bugs(self, date="today", bug_ids=[], chunk_size=None):
        bugs = super().get_bugs(date, bug_ids, chunk_size)

        dup_bug_ids = {bug_id for bug in bugs.values() for bug_id in bug["duplicates"]}
        dup_bugs = {}

        Bugzilla(
            dup_bug_ids,
            include_fields=["id", "severity", "cf_last_resolved"],
            bughandler=self._handle_dup_bug,
            bugdata=dup_bugs,
        ).wait()

        # Keep only bugs that have duplicates with higher severity than them
        for bug_id, bug in list(bugs.items()):
            higher_severity_dups = [
                dup_bugs[dup_id]
                for dup_id in bug["duplicates"]
                if dup_id in dup_bugs and dup_bugs[dup_id]["severity"] > bug["severity"]
            ]

            if higher_severity_dups:
                bug["duplicates"] = higher_severity_dups
            else:
                del bugs[bug_id]

        # Exclude duplicate bugs that were linked before downgrading the severity
        Bugzilla(
            bugs.keys(),
            include_fields=["id", "triage_owner", "assigned_to", "history"],
            bughandler=self._handle_final_bug_filtering,
            bugdata=bugs,
        ).wait()

        self.extra_ni = bugs

        return bugs

    def _handle_dup_bug(self, bug, data):
        if bug["severity"] not in Severity.ACCEPTED_VALUES:
            return

        bug["severity"] = Severity(bug["severity"])
        data[bug["id"]] = bug

    def _get_last_severity_downgrade_time(self, bug):
        for entry in reversed(bug["history"]):
            for change in entry["changes"]:
                if (
                    change["field_name"] == "severity"
                    and change["added"] in Severity.SEVERITY_LEVELS
                    and change["removed"] in Severity.SEVERITY_LEVELS
                    and Severity(change["added"]) < Severity(change["removed"])
                ):
                    return entry["when"]
        return None

    def _handle_final_bug_filtering(self, bug, data):
        bugid = str(bug["id"])
        duplicates = data[bugid]["duplicates"]
        downgrade_time = self._get_last_severity_downgrade_time(bug)
        if downgrade_time:
            duplicates = [
                dup_bug
                for dup_bug in duplicates
                # cf_last_resolved reflects when it was linked as a duplicate
                if dup_bug["cf_last_resolved"] > downgrade_time
            ]

        if duplicates and self.add_auto_ni(bugid, utils.get_mail_to_ni(bug)):
            data[bugid]["duplicates"] = duplicates
            data[bugid]["suggested_severity"] = max(
                dup_bug["severity"] for dup_bug in duplicates
            )
        else:
            del data[bugid]

    def get_extra_for_needinfo_template(self):
        return self.extra_ni

    def columns(self):
        return ["id", "summary", "severity", "suggested_severity"]

    def get_bz_params(self, date):
        fields = ["severity", "duplicates"]

        params = {
            "include_fields": fields,
            "resolution": "---",
            "f1": "bug_severity",
            "o1": "anyexact",
            "v1": "S2,S3,S4,S5",
            "f2": "bug_type",
            "o2": "equals",
            "v2": "defect",
            "f3": "duplicates",
            "o3": "isnotempty",
            "n4": 1,
            "f4": "longdesc",
            "o4": "casesubstring",
            "v4": "could you consider increasing the severity of this bug to",
        }

        return params

if __name__ == "__main__":
    SeverityHigherDuplicates().run()


#!/usr/bin/env python

from os import path

from setuptools import find_packages
from distutils.core import setup

here = path.abspath(path.dirname(__file__))

with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

requires = [
    'gspread'
]

classifiers = [
    'Development Status :: 4 - Beta',
    'License :: OSI Approved :: MIT License',
    'Operating System :: OS Independent',
    'Intended Audience :: Developers',
    'Intended Audience :: Science/Research',
    'Programming Language :: Python',
    'Programming Language :: Python :: Implementation :: PyPy',
]

setup(
    name='labgsheet',
    version='0.1.1',
    packages=find_packages(exclude=['contrib', 'docs', 'tests']),
    license='MIT',
    description='A python library to note ml experiments on google sheet',
    long_description=long_description,
    long_description_content_type='text/markdown',
    url='https://github.com/shotarok/labgsheet',
    author='Shotaro Kohama',
    author_email='khmshtr28@gmail.com',
    install_requires=requires,
    classifiers=classifiers
)


from django.db import models
from django.contrib.auth import get_user_model

MEMBERSHIP_PLANS = (
        ('Free', 'Free'),
        ('Basic Plan', 'Basic Plan'),
        ('Standard Plan', 'Standard Plan'),
        ('Plus Plan', 'Plus Plan'),
    )

User = get_user_model()

# Create your models here.
class Account(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    membership = models.CharField(max_length=100, choices=MEMBERSHIP_PLANS, default='Free')
    payments_method = models.CharField(max_length=100, null=False, blank=False)
    number_transactions = models.IntegerField(null=False, blank=False, default=0)


import datetime

from django.contrib.auth.decorators import login_required
from django.http import HttpRequest
from django.shortcuts import render, redirect
from rest_framework.decorators import api_view
from rest_framework.response import Response

from lms_app.lms_dto.EnrollmentDto import *
from lms_app.models import Enrollment
from lms_app.serializers import Enrollments
from lms_app.service_controllers import service_controller

@login_required(login_url='login')
def initiate_enrollment(request, course_id):
    if request.user.has_perm('lms_app.add_enrollment'):
        l_as_list = []
        for g in request.user.groups.all():
            l_as_list.append(g.name)

        username = request.user.username

        context = {
            'username': username,
            'l_as_list': l_as_list,
        }
        enroll2 = __initiate_enrollment_method(request, course_id, context)
        if enroll2 == 1:
            return redirect('student_details')
        else:
            return render(request, 'enrollment/error_message.html', context)
    else:
        context={
            'message': 'You are not authorised'
        }
        return render(request, 'error_message.html', context)

@login_required(login_url='login')
def list_enrollments(request, course_id):
    if request.user.has_perm('lms_app.view_enrollment'):
        l_as_list = []
        for g in request.user.groups.all():
            l_as_list.append(g.name)

        username = request.user.username
        enrollments = service_controller.enrollment_management_service().list_student_for_enrollment(course_id)
        course = service_controller.course_management_service().details(course_id)
        context = {
            'enrollments': enrollments,
            'username': username,
            'l_as_list': l_as_list,
            'course': course,
        }
        return render(request, 'enrollment/list_enrollment.html', context)
    else:
        context={
            'message': 'You are not authorised'
        }
        return render(request, 'error_message.html', context)

@login_required(login_url='login')
def list_enrollment_for_student(request):
    if request.user.has_perm('lms_app.view_enrollment'):
        l_as_list = []
        for g in request.user.groups.all():
            l_as_list.append(g.name)
        username = request.user.username
        user_id = request.user.id
        student = service_controller.student_management_service().details(user_id)
        student_id = student.id

        enrollments = service_controller.enrollment_management_service().list_enrollment_for_student(student_id)
        enrollment_len = len(enrollments)
        context = {
            'username': username,
            'l_as_list': l_as_list,
            'enrollments': enrollments,
            'enrollment_len': enrollment_len,
            'student_id': student_id,
            'presently': 'Courses',

        }
        return render(request, 'student/student_courses.html', context)
    else:
        context={
            'message': 'You are not authorised'
        }
        return render(request, 'error_message.html', context)

@login_required(login_url='login')
def cancel_enrollment(request, enrollment_id):
    if request.user.has_perm('lms_app.delete_enrollment'):
        try:
            service_controller.enrollment_management_service().delete(enrollment_id)
            return redirect('student_details')
        except Enrollment.DoesNotExist as e:
            print('You are not enrolled for this course!')
            raise e
    else:
        context={
            'message': 'You are not authorised'
        }
        return render(request, 'error_message.html', context)

# APIs
@api_view(["GET"])
def enrollments_for_a_course(request, course_id):
    if request.method == "GET":
        enrollments = service_controller.enrollment_management_service().list_student_for_enrollment(course_id)
        serializer = Enrollments(enrollments, many=True)
        json_data = serializer.data
        return Response(json_data)

@api_view(["GET"])
def total_enrollments(request):
    if request.method == "GET":
        user_id = request.user.id
        student = service_controller.student_management_service().details(user_id)
        student_id = student.id
        enrollments = service_controller.enrollment_management_service().list_enrollment_for_student(student_id)
        serializer = Enrollments(enrollments, many=True)
        json_data = serializer.data
        return Response(json_data)

@api_view(["GET"])
def total_student_under_tutor(request):
    if request.method == 'GET':
        user_id = request.user.id
        tutor = service_controller.tutor_management_service().details(user_id=user_id)
        tutor_id = tutor.id
        enrollments = service_controller.enrollment_management_service().list()
        serializer = Enrollments(enrollments, many=True)
        json_data = serializer.data
        return Response(json_data)

def __set_enrollment_attribute_request(request: HttpRequest):
    initiate_enrollment_dto = InitiateEnrollmentDto()
    user_id = request.user.id
    student = service_controller.student_management_service().details(user_id)
    student_id = student.id
    initiate_enrollment_dto.student_id = student_id
    initiate_enrollment_dto.date_enrolled = datetime.date.today()
    return initiate_enrollment_dto

def __initiate_enrollment_method(request, course_id, context):
    try:
        enrollment = __set_enrollment_attribute_request(request)
        enrollment.course_id = course_id
        student_id = enrollment.student_id
        if Enrollment.objects.filter(course_id=course_id, student_id=student_id).exists():
            context['saved'] = 'failed'
            context['message'] = 'You are already enrolled for the choosen course. Please select another course!'
            return 0
        else:
            service_controller.enrollment_management_service().register(enrollment)
            context['saved'] = 'success'
            return 1

    except Exception as e:
        print('This enrollment could not be completed')
        raise e