Item={'Sugar':[131,50],'Bread(sliced)':[311,200],'Bread(unsliced)':[229,150],'Egg':[545,50],'Three crown(tin)':[201,150],'Peak milk(tin)':[230,120],'Peak milk(sachet)':[791,50],'Bournvita(sachet)':[611,50],'Milo(tin)':[367,500],'Peak milk (Large Sachet)':[889,700],'Milo (Large Sachet)':[934,700],'Bournvita (Large Sachet)':[758,100],'Custard (small sachet)':[383,200],'Corn flakes (small sachet)':[647,150],'Golden morn (small sachet)':[121,100],'Detergent (small Wawu)':[198,120],'Detergent (small Aerial)':[354,115],'Detergent (Big Wawu)':[323,200],'Detergent (Big Aerial)':[222,250],'Corn flakes (big sachet)':[341,750],'Golden morn (Large sachet)':[458,650],'Sprite (small)':[134,80],'Pepsi (small)':[674,80],'Fanta (small)':[757,80],'Lacasera (small)':[127,80],'Sprite (big)':[956,150],'Pepsi (big)':[374,150],'Fanta (big)':[267,150],'Lacasera (big)':[786,150],'Coke (big)':[546,150]}

def PriceOfGoods():
    print ("{:^30}|{:^15}".format("Item","Price"))
    print ('{:^35}'.format("----------------------------------------"))

    for i in Item.keys():
        p=Item[i][1]
        print("{:^30}|{:^15}".format(i,p))

def PriceAndQuantity ():
    print ("{:^27}|{:^5}|{:^5}".format("Item","Price","Quantity"))
    print ('{:^35}'.format("----------------------------------------"))

    for i in Item.keys():
        p=Item[i][1]
        print("{:^27}|{:^5}|{:^5}".format(i,p,Item[i][0]))

def buy():
    c=input('what do you want to buy: ').split(',')
    d=input('Enter corresponding prices: '). split (',')
    item=dict(zip(c,d))
    print ('Are these what you want? ',item)
    T=0;
    for i in c:
        if i in Item:
            j=eval(item[i])*Item[i][1]
            T+=j
            if len(c)<5:
                Tp=(120*T/100)
            elif len(c)>10:
                Tp=(130*T/100)
            elif len(c)>10 and Item[i]>=100:
                Tp=T+800
            else:
                Tp=T

    print ('Items purchased are:',"\n")

    print("{:^20}|{:^10}|{:^5}".format('Item bought', 'Quantity','price'))
    print ('{:^35}'.format("----------------------------------------"))
    for i in item.keys():
        if i in Item:
            j=eval(item[i])*Item[i][1]
            Item[i][0]=Item[i][0]-int(item[i])
            print ("{:^20}|{:^10}|{:^5}".format(i,item[i],j))
        else:
            print (i," is not available")
            continue
    print ("\n")
    print (f"Total payment is {Tp} \n")

def check ():
    while True:
        d=input("Enter an Item to change its price: ")
        dp=input("Enter new price: ")

        if d=="done":
            print ("Price Updated")

            break
        elif d in Item:
            Item[d][1]=dp
            continue
        else:
             q=input ("quantity of new item:")

             Item[d]=[q,dp]
             continue
    PriceAndQuantity()

def user():
    while True:
        l=[]
        n=input ("Are you Admin or User? ")
        if n=="Admin":
            PriceAndQuantity()
            check()

        elif n=="User":
            s=input("Enter your name: \n ")
            print ("Welcome "+s)
            PriceOfGoods()
            buy()

            print ("Thank you",s)
            l.append(n)
        else:
            print ("You have to be an Admin or User \n",)
            print ('kindly check the price list of our available goods')
            PriceOfGoods()
            quit ()

user()


from torch.nn import functional as F
from torchvision.transforms import functional as Fv
from PIL import Image
import pandas as pd
import torchvision

from matplotlib import pyplot as plt
import time

from kymatio.torch import Scattering2D
device = "cuda:0"

t0 = time.time()

#img = Image.open('../trump.jpg')
img = Image.open('../chris.jpeg')
img_tensor = Fv.to_tensor(img)
x = img_tensor[None]
x = x.cuda()

B,C,W,H = x.shape
model = Scattering2D(J=2, shape=(W,H), L=8)
model.to(device)
model.to(device)
y = model(x)
y = y.view(y.size(0), -1, y.size(3), y.size(4))

for i in range(243):
    name = "out/order2/scatter/chris_scatt_{}.png".format(i)
    #torchvision.utils.save_image(y[0,i:i+1].detach().cpu(), name)
    plt.imshow(y[0,i].detach().cpu().numpy())
    plt.savefig(name)
    plt.show()

tFinnish = time.time() - t0
print("time: ", tFinnish)

# =============================================================================
# R, G, B = torch.chunk(y, 3, dim=1)
# for i in range(9):
#     name = "out/s_color_{}.png".format(i)
#
#     out = torch.stack([R[0,i], G[0,i], B[0,i]], dim=0)
#     torchvision.utils.save_image(out.detach().cpu(), name)
#
#     plt.imshow(Fv.to_pil_image(out.detach().cpu()))
#     plt.show()
# =============================================================================


# -*- coding: utf-8 -*-
"""
@author: Remy
"""

class Settings:

    K = 3 #Please insert your value of k here. The number of clusters
    EPSILON = 0.1 #Your value of epsilon that determines the convergence criterion
    MAX_ITER = 1000 #Value of maximum iteration to avoid infinite loop in case of non convergence
    DISTANCE_TYPE = "Euclidean" #Choose between "Euclidean" and "L1" or "Hamming" if binary data is used


import random

def make_ss(photos):
    slideshow = []
    current_photo = 0

    i = random.randint(0, len(photos))

    slideshow.append(photos[i])
    del photos[i]

    #pick next photo
    while len(photos) > 0:
        rand1 = random.randint(0, len(photos))
        rand2 = random.randint(0, len(photos))

        if comparePhotos(slideshow[current_photo], photos[rand1]) > comparePhotos(slideshow[current_photo], photos[rand2]):
            slideshow.append(photos[rand1])
            del photos[rand1]
            current_photo = rand1
        else:
            slideshow.append(photos[rand2])
            del photos[rand2]
            current_photo = rand2

def comparePhotos(t1,t2): #tags for p1 and p2
    common = 0
    t1NotInT2 = 0
    t2NotInT1 = 0
    for el1 in t1:
        tempResult = auxCompare(el1, t2)
        if tempResult == 1:
            common+=1
        else:
            t1NotInT2+=1

    for el2 in t2:
        tempResult = auxCompare(el2, t1)
        if tempResult == 1:
            common+=1
        else:
            t2NotInT1+=1

def auxCompare(el, t2):
    for el2 in t2:
        if(el == el2):
            return 1
    return 0

def ss_out(slideshow):
    out = ""
    out += len(slideshow)
    for i in slideshow:
        out.append(i)


import pandas as pd
import torch
import numpy as np
import os
from torch.utils.data import Dataset

_FILE_PATH = "data\\processed"
_FILE_NAME = "70man"
_DAY = 96

class ASPDataset(Dataset):
    """
    This is our custom dataset. In this project, it will load data to train from csv files.
    If databases which have all of informations for training exist, it will load data to train from the database

    """
    def __init__(self, file_path = _FILE_PATH, file_name = _FILE_NAME, mode = "train"):
        super().__init__()
        self.file_path = file_path
        self.file_name = file_name
        self.mode = mode
        self.dataframe = pd.read_csv(os.path.join(self.file_path, "{}_{}.csv".format(self.file_name, self.mode)), header = 0, names = ["Time", "Glucose"])
        self.predict_length = 3
        self.sequence_length =  3
        self.dataset_length = len(self.dataframe) - (self.predict_length*_DAY + self.sequence_length*_DAY) + 1

    def __len__(self):
        return self.dataset_length

    def __getitem__(self, idx):
        # Get data for training
        data = np.array(self.dataframe["Glucose"][idx : idx + (self.predict_length + self.sequence_length)*_DAY])

        # Split data to input and label
        input_data = torch.tensor(data[:-(self.predict_length*_DAY)], dtype = torch.long).clone()
        label = torch.tensor(data[-(self.predict_length*_DAY):], dtype = torch.float32).clone()
        return input_data, label


# -*- coding: utf-8 -*-
"""
Created on Sat Jun 27 12:14:44 2015

@author: gl391
"""
# %%
import comm.ScpiInstrumentWrapper as Wrap
import numpy as np
import time
from wanglib.wanglib import pylab_extensions
# import matplotlib

# %%
# Define variables: frequency list
f_upper = 100  # MHz
f_lower = 60  # MHz
n_steps = 400
f_list = np.linspace(f_lower, f_upper, num=n_steps, retstep=True)
# Define variables: FM
f_mod = 1E3  # Hz
f_range = 5E3  # Hz
amp_a = 0.5  # V
print("step width:  ", f_list[1])
# %%
# define instrument handles
sig = Wrap.ScpiInstrumentWrapper('PROLOGIX::COM3::GPIB::13')
amp = Wrap.ScpiInstrumentWrapper('PROLOGIX::COM3::GPIB::8')

# %%
# test/print identity
print(sig.query('*IDN?'))
print(amp.query('*IDN?'))

# %%
# reset signal generator and amplifier
sig.write('*RST')
time.sleep(0.1)
sig.write('*CLS')
time.sleep(0.1)
amp.write('*RST')
time.sleep(0.1)

# %%
# set lock in amp
amp.write('SLVL %f' % amp_a)
time.sleep(0.1)
amp.write('FREQ %f' % f_mod)
time.sleep(0.1)

# %%
# set signal generator
sig.write('FM1:STAT ON')
time.sleep(0.1)
sig.write('FM1:SOUR EXT1')
time.sleep(0.1)
sig.write('FM1:DEV %f HZ' % f_range)
time.sleep(0.1)
sig.write('POW:AMPL 10dBm')
time.sleep(0.1)
sig.write('FREQ %d MHz' % f_lower)
time.sleep(0.1)
sig.write('OUTP:STAT ON')
time.sleep(0.1)

# %%
def scan_fq(fqs):
    for fq in fqs:
        sig.write('FREQ %d MHz' % fq)
        time.sleep(0.3)
        val = amp.query('OUTP?1')
        yield fq, val

fqs = np.arange(f_lower, f_upper, f_list[1])
fig1 = pylab_extensions.plotgen(scan_fq(fqs))


import logging
import re

from bs4 import BeautifulSoup
from decimal import Decimal

from storescraper.categories import CPU_COOLER
from storescraper.product import Product
from storescraper.store import Store
from storescraper.utils import session_with_proxy, remove_words, \
    html_to_markdown

class Zegucom(Store):
    @classmethod
    def categories(cls):
        return [
            # 'ExternalStorageDrive',
            'StorageDrive',
            'SolidStateDrive',
            'Motherboard',
            'Processor',
            CPU_COOLER,
            'Ram',
            'VideoCard',
            'PowerSupply',
            'ComputerCase',
            # 'Ups',
            'Mouse',
            'Keyboard',
            'KeyboardMouseCombo',
            'Monitor',
            # 'Headphones',
            'Tablet',
            'Notebook',
            # 'StereoSystem',
            # 'OpticalDiskPlayer',
            # 'Printer',
            # 'MemoryCard',
            'Cell',
            # 'UsbFlashDrive',
            'Television',
            # 'Camera',
            # 'Projector',
            # 'AllInOne',
        ]

    @classmethod
    def discover_urls_for_category(cls, category, extra_args=None):
        url_extensions = [
            ['sub=SAT', 'StorageDrive'],
            ['sub=DNS', 'StorageDrive'],
            ['sub=SSD', 'SolidStateDrive'],
            ['fam=TM', 'Motherboard'],
            ['fam=PR', 'Processor'],
            ['sub=DIS', CPU_COOLER],
            ['sub=ENF', CPU_COOLER],
            ['sub=DR3', 'Ram'],
            ['sub=DR4', 'Ram'],
            ['sub=LD3', 'Ram'],
            ['sub=LD4', 'Ram'],
            # ['sub=PPC', 'VideoCard'],
            ['sub=PCI', 'VideoCard'],
            ['sub=FP', 'PowerSupply'],
            ['sub=GC', 'ComputerCase'],
            ['sub=OP', 'Mouse'],
            ['sub=TE', 'Keyboard'],
            ['sub=TI', 'Keyboard'],
            ['sub=TCO', 'KeyboardMouseCombo'],
            ['sub=TCI', 'KeyboardMouseCombo'],
            ['sub=LED', 'Monitor'],
            ['sub=TAB', 'Tablet'],
            ['sub=NT', 'Notebook'],
            ['sub=IY', 'Printer'],
            ['sub=LS', 'Printer'],
            ['sub=MFI', 'Printer'],
            ['sub=MFL', 'Printer'],
            ['sub=SMP', 'Cell'],
            ['sub=TLED', 'Television'],
            ['sub=AIO', 'AllInOne'],
        ]

        base_url = 'https://www.zegucom.com.mx/index.php?' \
                   'mod=search&{}&sp={}'

        product_urls = []
        session = session_with_proxy(extra_args)

        for url_extension, local_category in url_extensions:
            if local_category != category:
                continue

            page = 0

            while True:
                url = base_url.format(url_extension, page)
                print(url)

                if page >= 20:
                    raise Exception('Page overflow: ' + url)

                soup = BeautifulSoup(session.get(url).text, 'html.parser')
                product_container = soup.find('div', 'search-results')
                if not product_container or not \
                        product_container.findAll('div', 'result-description'):
                    if page == 0:
                        logging.warning('Empty category: ' + url_extension)
                    break
                products = product_container.findAll(
                    'div', 'result-description')

                for product in products:
                    product_urls.append('https://www.zegucom.com.mx{}'.format(
                        product.find('a')['href']))

                page += 1

        return product_urls

    @classmethod
    def products_for_url(cls, url, category=None, extra_args=None):
        print(url)
        session = session_with_proxy(extra_args)

        page_source = session.get(url).text
        soup = BeautifulSoup(page_source, 'html.parser')

        well = soup.find('div', 'well')
        if well and 'no disponible' in well.text:
            return []

        if not soup.find('div', 'item-description'):
            return []

        name = soup.find('div', 'item-description').text.strip()[:250]

        sku = None
        part_number = None
        stock = 0

        data_block = soup.findAll('div', 'item-tech-info')

        for data_row in data_block:
            data_row_contents = data_row.findAll('div', 'col-flex')
            for data_col in data_row_contents:
                data_pair = data_col.text.strip().split(':')
                if data_pair[0] == 'UPC':
                    sku = data_pair[1].strip()
                if data_pair[0] == 'Núm. de parte':
                    part_number = data_pair[1].strip()
                if data_pair[0] == 'Disponibilidad':
                    stock = int(data_pair[1].strip().split(' ')[0])

        price_tags = soup.findAll('span', 'price-text')
        prices = []

        for price_tag in price_tags:
            price_text = re.search(r'\$(\d*,?\d+\.?\d*)',
                                   price_tag.text).groups()[0]
            prices.append(Decimal(price_text.replace(',', '')))

        price = min(prices)

        if soup.find('img', 'larger2'):
            picture_urls = ['https://www.zegucom.com.mx/{}'.format(
                soup.find('img', 'larger2')['src'])]
        else:
            picture_urls = []

        description = html_to_markdown(
            str(soup.find('div', {'id': 'ficha'})))

        p = Product(
            name,
            cls.__name__,
            category,
            url,
            url,
            sku,
            stock,
            price,
            price,
            'MXN',
            sku=sku,
            picture_urls=picture_urls,
            description=description,
            part_number=part_number
        )

        return [p]


import os
import logging
from telegram import ReplyKeyboardMarkup, KeyboardButton, ReplyKeyboardRemove, \
                     InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler, \
                         CallbackQueryHandler
from services.service_data import store_bot_data, upload_image_to_dbx, get_telegram_upload_image_paths
from bots.bot_services import get_station_by_location
import bots.constants as const
# TODO delete before production!:

logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)

PHOTO, CHOICE, SELECT_STATION, LOCATION, SET_DATA, GET_DATA, DATALOC, IN_DEV = range(8)

def start(bot, update):

    bot.send_message(chat_id=update.effective_message.chat_id,
                     text=const.start_text,
                     reply_markup=InlineKeyboardMarkup(const.start_keyboard))
    return CHOICE

def start_button(bot, update):
    query = update.callback_query
    if query.data == 'send':
        return send_location(bot, update)
    elif query.data == 'get':
        return getdata(bot, update)

def getdata(bot, update):
    query = update.callback_query
    location_button = KeyboardButton('Sent location', request_location=True)
    no_matter = KeyboardButton('Doesn\'t matter')
    bot.send_message(chat_id=query.message.chat_id,
                     text='Please share your location to get data depending your place '
                          'or press "Skip" if location doesn\'t matter',
                     reply_markup=ReplyKeyboardMarkup([[location_button], [no_matter]],
                                                      one_time_keyboard=True, resize_keyboard=True)
                     )
    return GET_DATA

def setdata(bot, update, location, user_data):
    stations = get_station_by_location(lat=location['latitude'], lng=location['longitude'])
    if not stations:
        bot.send_message(text="There are no gas stations in your location, please try again.",
                         chat_id=update.message.chat_id)
        return send_location(bot, update)
    user_data['stations'] = stations
    buttons = [[InlineKeyboardButton(text=st["name"]+f'\n{st["adress"]}',
                                     callback_data=stations.index(st))] for st in stations]
    bot.send_message(text="Please choose fuel company from the list: ",
                     chat_id=update.message.chat_id,
                     reply_markup=InlineKeyboardMarkup(buttons))
    return SELECT_STATION

def select_station(bot, update, user_data):
    query = update.callback_query
    user_data['gas_st'] = user_data['stations'][int(query.data)]
    bot.send_message(text='You\'ve selected "{}".\n Please send us your photo:'
                     .format(user_data['stations'][int(query.data)]['name']),
                     chat_id=query.message.chat_id)
    return PHOTO

def send_location(bot, update):
    location_button = KeyboardButton('Send current location', request_location=True)
    bot.send_message(chat_id=update.effective_message.chat_id,
                     text='Please, share you location so we can find nearest gas stations.\n'
                          'Tap the button if you are near gas station now, or choose location manually',
                     reply_markup=ReplyKeyboardMarkup([[location_button]],
                                                      one_time_keyboard=True, resize_keyboard=True))
    return LOCATION

def got_location(bot, update, user_data):
    chat_id = update.message.chat_id
    bot.send_message(chat_id=chat_id, text="Thanks!", reply_markup=ReplyKeyboardRemove())
    loc = update.message.location
    return setdata(bot, update, loc, user_data)

def get_data_by_location(bot, update):

    bot.send_message(chat_id=update.message.chat_id, text="ok!",
                     reply_markup=ReplyKeyboardRemove())
    bot.send_message(chat_id=update.message.chat_id, text="Please choose:",
                     reply_markup=InlineKeyboardMarkup(const.data_by_loc_keyboard))
    return DATALOC

def dataloc(bot, update):
    bot.send_message(chat_id=update.effective_message.chat_id,
                     text="Select types of fuel:\n\nIN DEVELOPMENT",
                     reply_markup=ReplyKeyboardRemove())
    return start(bot, update)

def helpme(bot, update):
    update.message.reply_text("Still in development. /start")

def error(bot, update, err):
    logger.warning("Update {} caused error {}".format(update, err))

def cancel(bot, update):
    return ConversationHandler.END

def send_file_dbx(bot, update, user_data):
    # TODO move this to services, realize else
    if update.message.document:
        file_id = update.message.document.file_id
    elif update.message.photo:
        file_id = update.message.photo[-1].file_id
    else:
        file_id = None
        pass
    user_id = update.message.from_user.id
    station_name = user_data['gas_st']['name']
    adress = user_data['gas_st']['adress']
    lat, lng = user_data['gas_st']['lat'], user_data['gas_st']['lng']
    tg_down_path, dbx_path = get_telegram_upload_image_paths(file_id)

    dbx_link = upload_image_to_dbx(tg_down_path, dbx_path)
    bot.send_message(chat_id=update.message.chat_id, text="download success! "+dbx_path)
    response = store_bot_data(telegram_id=user_id, image_link=dbx_link, company_name=station_name,
                              address=adress, lat=lat, lng=lng)
    bot.send_message(chat_id=update.message.chat_id, text=response)
    """
    is_recognized, fuel_type, price = digit_to_price(dbx_path)
    if is_recognized:
        bot.send_message(chat_id=update.message.chat_id, text=f"Recognized!\n"
                         f"A{fuel_type}: {price}грн")
    else:
        bot.send_message(chat_id=update.message.chat_id, text="Failed to recognize")
    """
    return start(bot, update)

def main(poll=True):
    telegram_token = os.environ['TELEGRAM_TOKEN']
    updater = Updater(telegram_token)
    disp = updater.dispatcher
    disp.add_error_handler(error)

    conv_handler = ConversationHandler(
        entry_points=[CommandHandler('start', start), ],

        states={
            CHOICE: [CallbackQueryHandler(start_button)],
            LOCATION: [MessageHandler(Filters.location, got_location, pass_user_data=True)],
            GET_DATA: [MessageHandler(Filters.location, get_data_by_location), MessageHandler(Filters.text, dataloc)],
            DATALOC: [CallbackQueryHandler(dataloc)],
            SELECT_STATION: [CallbackQueryHandler(select_station, pass_user_data=True)],
            PHOTO: [(MessageHandler(Filters.document, send_file_dbx, pass_user_data=True)),
                    MessageHandler(Filters.photo, send_file_dbx, pass_user_data=True)]
        },

        fallbacks=[CommandHandler('cancel', cancel)]
    )
    disp.add_handler(conv_handler)
    disp.add_handler(CommandHandler("help", helpme))
    disp.add_handler(CommandHandler("start", start))
    disp.add_error_handler(error)

    if poll:
        updater.start_polling()
        updater.idle()
    else:
        updater.start_webhook(listen="0.0.0.0",
                              port=int(os.environ['PORT']),
                              url_path=telegram_token)
        updater.bot.setWebhook(f'{os.environ["URL_PATH"]}/{telegram_token}')
        updater.idle()

if __name__ == '__main__':
    main()


from lh3.api import *
from dashboard.utils.utils import (
    soft_anonimyzation,
    operatorview_helper,
    Chats,
    retrieve_transcript,
    search_chats,
)
from datetime import datetime, timedelta, timezone
from django.views.generic import TemplateView
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.http import Http404
from django.http import HttpResponse, HttpResponseNotFound
from pprint import pprint as print
from django.contrib import messages
import random

from datetime import datetime
from datetime import timezone
import json

from dateutil.parser import parse

from django.http import JsonResponse

from dashboard.utils.ask_schools import (
    find_school_by_operator_suffix,
    find_queues_from_a_school_name,
    find_school_by_queue_or_profile_name,
)

import warnings

warnings.filterwarnings("ignore")

"""
List of VIEW FUNCTIONS/CLASSES
SearchProfileResultsView
get_this_profile
get_transcript
get_chats_for_this_school_using_an_username
get_chats_for_this_school_using_this_queue_name
get_chats_from_this_queue_using_only_the_queue_name
get_chats_for_this_user
get_chats_for_this_queue
get_chats_from_yesterday
get_chats_from_yesterday_from_mentees
"""

def get_chats_for_this_school_using_an_username(request, *args, **kwargs):
    """[summary]

    Args:
        request ([type]): [description]

    Returns:
        [type]: [description]
    """
    client = Client()
    username = kwargs.get("username", None)
    school_name = find_school_by_operator_suffix(username).lower()
    queues_from_school = find_queues_from_a_school_name(school_name)
    print("username: {0}".format(username))
    print("school_name: {0}".format(school_name))
    query = {
        "query": {
            "queue": queues_from_school,
            "from": "2021-01-01",
            "to": "2021-12-31",
        },
        "sort": [{"started": "descending"}],
    }
    queue_chats = client.api().post("v4", "/chat/_search", json=query)
    chats = soft_anonimyzation(queue_chats)
    today = datetime.today()
    current_year = today.year
    total_chats = len(chats)

    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    # print(chats)
    chats = [Chats(chat) for chat in chats]
    if request.is_ajax():
        return JsonResponse(
            {
                "object_list": chats,
                "heatmap_chats": heatmap_chats,
                "username": school_name,
                "current_year": current_year,
                "total_chats": total_chats,
            },
            safe=False,
        )
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "heatmap_chats": heatmap_chats,
            "username": school_name,
            "current_year": current_year,
            "total_chats": total_chats,
        },
    )

def get_chats_for_this_school_using_this_queue_name(request, *args, **kwargs):
    """[summary]

    Args:
        request ([type]): [description]

    Returns:
        [type]: [description]
    """
    client = Client()
    queue_name = kwargs.get("queue_name", None)
    school_name = find_school_by_queue_or_profile_name(queue_name)
    queues_from_school = find_queues_from_a_school_name(school_name)

    print("queue_name: {0}".format(queue_name))
    print("school_name: {0}".format(school_name))
    query = {
        "query": {
            "queue": queues_from_school,
            "from": "2021-01-01",
            "to": "2021-12-31",
        },
        "sort": [{"started": "descending"}],
    }
    # breakpoint()
    queue_chats = client.api().post("v4", "/chat/_search", json=query)
    chats = soft_anonimyzation(queue_chats)
    today = datetime.today()
    current_year = today.year
    total_chats = len(chats)

    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    # print(chats)
    chats = [Chats(chat) for chat in chats]
    if request.is_ajax():
        return JsonResponse(
            {
                "object_list": chats,
                "heatmap_chats": heatmap_chats,
                "username": school_name,
                "current_year": current_year,
                "total_chats": total_chats,
            },
            safe=False,
        )
    return render(
        request,
        "results/chats_from_queues.html",
        {
            "object_list": chats,
            "heatmap_chats": heatmap_chats,
            "username": school_name,
            "current_year": current_year,
            "total_chats": total_chats,
        },
    )

def get_chats_from_this_queue_for_this_year_using_only_the_queue_name(
    request, *args, **kwargs
):
    client = Client()
    queue_name = kwargs.get("queue_name", None)
    today = datetime.today()

    print("queue_name: {0}".format(queue_name))
    query = {
        "query": {"queue": [queue_name], "from": str(today.year)+"-01-01", "to": str(today.year)+"-12-31"},
        "sort": [{"started": "descending"}],
    }
    queue_chats, content_range = search_chats(
            client, query, chat_range=(0, 500)
        )
    # breakpoint()
    chats = soft_anonimyzation(queue_chats)
    today = datetime.today()
    current_year = today.year
    total_chats = len(chats)

    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    # print(chats)
    chats = [Chats(chat) for chat in chats]
    if request.is_ajax():
        return JsonResponse(
            {
                "object_list": chats,
                "heatmap_chats": heatmap_chats,
                "username": queue_name,
                "get_chat_for_this_year": True,
                "current_year": current_year,
                "total_chats": total_chats,
            },
            safe=False,
        )
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "get_chat_for_this_year": True,
            "heatmap_chats": heatmap_chats,
            "username": queue_name,
            "current_year": current_year,
            "total_chats": total_chats,
        },
    )

def get_chats_for_this_user(request, username):
    """[summary]

    Args:
        request ([type]): [description]
        username ([type]): [description]
        information ([type], optional): [description]. Defaults to None.

    Returns:
        [type]: [description]
    """
    client = Client()
    today = datetime.today()
    query = {
        "query": {"operator": [username], "from": str(today.year)+"-01-01", "to": str(today.year)+"-12-31"},
        "sort": [{"started": "descending"}],
    }
    chats_from_users, content_range = search_chats(
            client, query, chat_range=(0, 500)
        )
    chats = soft_anonimyzation(chats_from_users)

    today = datetime.today()
    current_year = today.year
    total_chats = len(chats_from_users)

    assignments = operatorview_helper(username)
    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    if request.is_ajax():
        return JsonResponse(
            {
                "chats": chats,
                "assignments": assignments,
                "get_chat_for_this_year": True,
                "heatmap_chats": heatmap_chats,
                "username": username,
                "current_year": current_year,
                "total_chats": total_chats,
            },
            safe=False,
        )
    chats = [Chats(chat) for chat in chats]
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "assignments": assignments,
            "get_chat_for_this_year": True,
            "heatmap_chats": heatmap_chats,
            "username": username,
            "current_year": current_year,
            "total_chats": total_chats,
        },
    )

def get_chats_for_this_user_for_this_year(request, username, information=None):
    client = Client()
    today = datetime.today()
    query = {
        "query": {
            "operator": [username],
            "from": str(today.year) + "-01-01",
            "to": str(today.year) + "-12-31",
        },
        "sort": [{"started": "descending"}],
    }
    chats_from_users, content_range = search_chats(client, query, chat_range=(0, 500))
    chats = soft_anonimyzation(chats_from_users)

    today = datetime.today()
    current_year = today.year
    total_chats = len(chats_from_users)

    assignments = operatorview_helper(username)
    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    if request.is_ajax():
        return JsonResponse(
            {
                "chats": chats,
                "assignments": assignments,
                "heatmap_chats": heatmap_chats,
                "username": username,
                "current_year": current_year,
                "total_chats": total_chats,
            },
            safe=False,
        )
    chats = [Chats(chat) for chat in chats]
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "assignments": assignments,
            "heatmap_chats": heatmap_chats,
            "username": username,
            "current_year": current_year,
            "total_chats": total_chats,
        },
    )

def get_chats_for_this_queue(request, *args, **kwargs):
    client = Client()
    queue = kwargs.get("queue_name", None)
    query = {
        "query": {"queue": [queue], "from": "2021-01-01", "to": "2021-01-19"},
        "sort": [{"started": "descending"}],
    }
    queue_chats = client.api().post("v4", "/chat/_search", json=query)
    chats = soft_anonimyzation(queue_chats)
    return JsonResponse(chats, safe=False)

def get_chats_of_today(request, *args, **kwargs):
    client = Client()
    today = datetime.today()

    query = {
        "query": {"from": today.strftime("%Y-%m-%d")},
        "sort": [{"started": "ascending"}],
    }
    chats_from_users = client.api().post("v4", "/chat/_search", json=query)
    chats = soft_anonimyzation(chats_from_users)

    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    username = "Yesterday"
    # filter chats from yesteday only
    chats = [Chats(chat) for chat in chats]

    # return JsonResponse(chats, safe=False)
    if request.is_ajax():
        return JsonResponse(chats, safe=False)
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "heatmap_chats": heatmap_chats,
            "username": username,
            "current_year": "Yesterday",
            "total_chats": len(chats),
        },
    )

def get_chats_from_yesterday(request, *args, **kwargs):
    client = Client()
    today = datetime.today()
    yesterday = today - timedelta(days=1)

    query = {
        "query": {"from": yesterday.strftime("%Y-%m-%d")},
        "sort": [{"started": "ascending"}],
    }
    # chats_from_users = client.api().post('v4', '/chat/_search', json = query)
    chats_from_users, content_range = search_chats(client, query, chat_range=(0, 350))
    chats = soft_anonimyzation(chats_from_users)

    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    username = "Yesterday"
    # filter chats from yesteday only
    chats = [
        chat
        for chat in chats
        if yesterday.strftime("%Y-%m-%d")
        == parse(chat.get("started")).strftime("%Y-%m-%d")
    ]
    chats = [Chats(chat) for chat in chats]

    # return JsonResponse(chats, safe=False)
    if request.is_ajax():
        return JsonResponse(chats, safe=False)
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "heatmap_chats": heatmap_chats,
            "username": username,
            "current_year": "Yesterday",
            "total_chats": len(chats),
        },
    )

def get_chats_from_yesterday_from_mentees(request, *args, **kwargs):
    client = Client()
    today = datetime.today()
    yesterday = today - timedelta(days=1)

    query = {
        "query": {"from": yesterday.strftime("%Y-%m-%d")},
        "sort": [{"started": "descending"}],
    }
    # chats_from_users = client.api().post('v4', '/chat/_search', json = query)
    chats_from_users, content_range = search_chats(client, query, chat_range=(0, 100))
    chats = soft_anonimyzation(chats_from_users)
    chat_picked_up_by_mentees = list()
    for chat in chats:
        if chat.get("accepted"):
            if "_int" in chat.get("operator"):
                chat_picked_up_by_mentees.append(chat)
    chats = chat_picked_up_by_mentees

    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    username = "Mentees"
    # filter chats from yesteday only
    chats = [
        chat
        for chat in chats
        if yesterday.strftime("%Y-%m-%d")
        == parse(chat.get("started")).strftime("%Y-%m-%d")
    ]
    chats = [Chats(chat) for chat in chats]

    # return JsonResponse(chats, safe=False)
    if request.is_ajax():
        return JsonResponse(chats, safe=False)
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "heatmap_chats": heatmap_chats,
            "username": username,
            "current_year": "Yesterday",
            "total_chats": len(chats),
        },
    )

def get_chats_from_yesterday_sample_size(request, *args, **kwargs):
    client = Client()
    today = datetime.today()
    yesterday = today - timedelta(days=1)

    query = {
        "query": {"from": yesterday.strftime("%Y-%m-%d")},
        "sort": [{"started": "ascending"}],
    }
    chats_from_users, content_range = search_chats(client, query, chat_range=(0, 350))
    chats_from_users = random.sample(chats_from_users, int(50 * 0.20))
    chats = soft_anonimyzation(chats_from_users)

    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    username = "Yesterday"
    # filter chats from yesteday only
    chats = [
        chat
        for chat in chats
        if yesterday.strftime("%Y-%m-%d")
        == parse(chat.get("started")).strftime("%Y-%m-%d")
    ]
    chats = [Chats(chat) for chat in chats]

    # return JsonResponse(chats, safe=False)
    if request.is_ajax():
        return JsonResponse(chats, safe=False)
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "heatmap_chats": heatmap_chats,
            "username": username,
            "current_year": "Yesterday",
            "total_chats": len(chats),
        },
    )

import os
from tempfile import gettempdir
from pathlib import Path
import pandas as pd
import pathlib
import random
from django.http import FileResponse
from shutil import rmtree

def download_get_chat_for_date_range(request, *args, **kwargs):
    filename = kwargs.get("filename", "")
    tmp_folder_name = kwargs.get("tmp_folder_name", "")
    if filename:
        tmp = os.path.join(gettempdir(), '.{}'.format(hash(int(tmp_folder_name))))
        filepath = str(pathlib.PurePath(tmp, filename))
        return FileResponse(open(filepath, "rb"), as_attachment=True, filename=filename)
    else:
        raise Http404()

def get_chat_for_date_range(request, *args, **kwargs):
    start_date = request.GET.get("start_date", "")
    end_date = request.GET.get("end_date", "")
    if start_date and end_date:
        if start_date:
            start_date_with_time = parse(start_date)

        if end_date:
            end_date_with_time = parse(end_date)

        if end_date < start_date:
            messages.warning(
                request, "The End_date should be greater than the Start Date"
            )
            return render(
                request, "results/search_between_date.html", {"object_list": None}
            )
        client = Client()
        chats = client.chats()
        to_date = (
            str(end_date_with_time.year)
            + "-"
            + "{:02d}".format(end_date_with_time.month)
            + "-"
            + str(end_date_with_time.day)
        )
        all_chats = chats.list_day(
            year=start_date_with_time.year,
            month=start_date_with_time.month,
            day=start_date_with_time.day,
            to=to_date,
        )

        #Tmp save the chats list
        df = pd.DataFrame(all_chats)
        del df['tags']
        del df['referrer']
        del df['id']
        del df['profile']
        del df['desktracker_id']
        del df['reftracker_url']
        del df['ip']
        del df['reftracker_id']
        del df['desktracker_url']
        df['school_from_operator_username'] = df['operator'].apply(lambda x: find_school_by_operator_suffix(x))
        df['school_from_queue_name'] = df['queue'].apply(lambda x: find_school_by_queue_or_profile_name(x))
        df['guest'] = df['guest'].apply(lambda x: x.split('@')[0][0:8])

        today = datetime.today().strftime("%Y-%m-%d-%H:%M")

        tmp_folder_name ='4564565464321'
        rmtree(tmp_folder_name, ignore_errors=True)
        tmp = os.path.join(gettempdir(), '.{}'.format(hash(4564565464321)))
        try:
            os.makedirs(tmp)
        except:
            pass

        filename = "list_of_chats_from_date_range_results_" + str(random.randint(1,7000))+".xlsx"
        filepath = str(pathlib.PurePath(tmp, filename))

        writer = pd.ExcelWriter(filepath, engine="xlsxwriter")
        df.to_excel(writer, index=False)
        writer.save()

        # Continue

        chats = [Chats(chat) for chat in all_chats]
        selected_chats = list()
        for chat in chats:
            if (parse(chat.started) >= start_date_with_time):
                try:
                    if (parse(chat.ended) <= end_date_with_time):
                        selected_chats.append(chat)
                except:
                    selected_chats.append(chat)
        return render(
            request, "results/search_between_date.html", {
                            "object_list": selected_chats,
                            "filename":filename,
                            "tmp_folder_name":tmp_folder_name}
        )
    else:
        messages.warning(
                request, "There should be a valid Start_Date and End_date"
            )
    return render(request, "results/search_between_date.html", {"object_list": None})

def search_chats_within_2_hours(request, *args, **kwargs):
    client = Client()
    chat_id = int(kwargs.get("chat_id", None))
    chat = client.one("chats", chat_id).get()

    if chat:
        start_date = parse(chat.get("started"))
        chats = client.chats()
        chats = chats.list_day(start_date.year, start_date.month, start_date.day)

        chat_within_2_hours = list()
        for chat in chats:
            started = parse(chat.get("started"))
            # print("{0} > {1} < {2}".format(started-timedelta(minutes=60), start_date , started+timedelta(minutes=60)))
            if started - timedelta(60) > start_date < started + timedelta(60):
                chat_within_2_hours.append(chats)

        # print(chat_within_2_hours)
        chats = None
        if chat_within_2_hours:
            chats = soft_anonimyzation(chat_within_2_hours)

        return JsonResponse(chats, safe=False)
    return JsonResponse(None, safe=False)

@csrf_exempt
def search_chats_with_this_guestID(request, *args, **kwargs):
    guest_id = request.POST.get("guest_id", None)
    chats = None
    if guest_id:
        if "@" in guest_id:
            pass
        else:
            guest_id = guest_id + "*"
        query = {
            "query": {
                "guest": [guest_id],
            },
            "sort": [{"started": "descending"}],
        }
        client = Client()
        chats = client.api().post("v4", "/chat/_search", json=query)
        chats = soft_anonimyzation(chats)
        chats = [Chats(chat) for chat in chats]
        if request.is_ajax():
            return JsonResponse(
                {"object_list": chats, "guest_id": guest_id}
            )
        return render(
            request,
            "results/search_guest.html",
            {"object_list": chats, "guest_id": guest_id},
        )
    if request.is_ajax():
        return JsonResponse(
            {"object_list": None}
        )
    return render(request, "results/search_guest.html", {"object_list": None})

class SearchGuestResultsView(TemplateView):
    template_name = "results/search_guest.html"


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('product', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Business',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=50)),
            ],
        ),
        migrations.CreateModel(
            name='Link',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('url_link', models.CharField(max_length=250)),
                ('business', models.ForeignKey(to='business.Business')),
            ],
        ),
        migrations.CreateModel(
            name='Sucursal',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('lat', models.FloatField()),
                ('lon', models.FloatField()),
                ('business', models.ForeignKey(to='business.Business')),
                ('products', models.ManyToManyField(to='product.Product')),
            ],
        ),
    ]


from rest_framework.authentication import get_authorization_header
from rest_framework.views import APIView
from rest_framework.response import Response

from pitter.acc_actions.auth import TokenAuthentication
from pitter.models import Pitt, Follower
from pitter.models.user_model import User
from pitter.decorators import request_query_serializer

from api_client.validation_serializers.user_serializers import FindRequest

class FindUser(APIView):
    @classmethod
    @request_query_serializer(FindRequest)
    def get(cls, request) -> Response:
        """
        Finds user in the DB with the login from the query
        :param request:
        :return: Response dict with the user data
        """
        access = TokenAuthentication.get(request)
        auth_token = get_authorization_header(request).decode('utf-8')

        data = request.query_params
        login = data['login']
        feed_pitts = []
        follow_link = f'http://localhost:8000/follownode/?login={login}&token={auth_token}'
        unfollow_link = f'http://localhost:8000/follownode/?login={login}&unfollow=True&token={auth_token}'

        try:
            user = User.objects.get(login=login)
            follower = User.objects.get(email_address=access['email'])

        except User.DoesNotExist:
            return Response('User is not found.', status=200)
        except KeyError:
            return Response('Yiu are logged out.', status=200)

        for pitt in Pitt.objects.all():
            if pitt.user_id == user.id:
                pitt_info = (pitt.audio_decoded, pitt.created_at)
                feed_pitts.append(pitt_info)

        returned_data = dict(
            id=user.id,
            login=user.login,
            email=user.email_address,
            pitts=feed_pitts,
        )

        try:
            follower_exists = Follower.objects.get(user_id=user.id, follower_id=follower.id)
            if follower_exists:
                returned_data['unfollow_link'] = unfollow_link
            return Response(returned_data, status=200)

        except Follower.DoesNotExist:
            returned_data['follow_link'] = follow_link
            return Response(returned_data, status=200)


#!/usr/bin/env python3

# Copyright 2018 Daniel Oakey
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Based on java_launcher by Lawrence Kesteloot
# (see https://github.com/lkesteloot/java_launcher)
#
# Put this script in your PATH and put the following hashbang at the top
# of the Scala "script" you want to execute:
#
#   #!/usr/bin/env scala-launcher.py
#
# For more information see https://github.com/danieloakey/scala-launcher

import hashlib
import os
import subprocess
import sys

def sha256sum(filepath):
    BUFFER_SIZE = 65536
    sha256 = hashlib.sha256()
    with open(filepath, 'rb') as f:
        while True:
            data = f.read(BUFFER_SIZE)
            if not data:
                break
            sha256.update(data)

    return sha256.hexdigest()

def main(args):
    if len(args) < 2:
        print("scala-launcher: Missing script argument")
        return

    src_path = args[1]
    src_sum = sha256sum(src_path)
    src_filename = os.path.basename(src_path)
    class_name = src_filename.replace('.scala', '')
    cache_dir = os.path.join(os.path.expanduser("~"), '.scala-launcher-cache')
    class_dir = os.path.join(cache_dir, src_sum)
    processed_src = os.path.join(class_dir, src_filename)
    processed_class = os.path.join(class_dir, '{}.class'.format(class_name))

    os.makedirs(class_dir, exist_ok=True)

    # Compile if necessary
    if not os.path.exists(processed_class):
        # Copy file, removing lines starting with hash
        with open(src_path, 'r') as f_in:
            with open(processed_src, 'w') as f_out:
                for line in f_in.readlines():
                    if line[0] != '#':
                        f_out.write(line)

        command = 'scalac -d {0} {1}'.format(class_dir, processed_src)
        subprocess.call(command.split(' '))

    # Run
    classpath = class_dir
    command = 'scala -classpath {0} {1}'.format(classpath, class_name)
    subprocess.call(command.split(' '))

if __name__ == '__main__':
    main(sys.argv)


from affine import Affine
from atbash import Atbash
from keywords import Keyword

def run():
    """
    Command line interface for encrypting / decrypting messages.
    :return: None
    """
    # dictionary containing implemented ciphers
    cipher_dict = {'affine': Affine, 'atbash': Atbash, 'keyword': Keyword}
    while True:
        print('This is the Secret Messages project for the '
              'Treehouse Techdegree \n')
        print('The following ciphers are available:\n')
        print('1. Affine \n')
        print('2. Atbash \n')
        print('3. Keyword \n')
        print('Enter quit or q to end.')
        print()
        user_input = input("Which cipher would you like to use? ").lower()
        print()
        if user_input == 'q' or user_input == 'quit':
            print('Have a wonderful day!')
            break
        if user_input in cipher_dict:
            message = input("What's the message? ")
            method = input('Do you want to encrypt or decrypt? ')
            while True:
                if method == 'encrypt':
                    cipher = cipher_dict[user_input]()
                    print('Your message has been encrypted:\n')
                    print(cipher.encrypt(message))
                    break
                elif method == 'decrypt':
                    cipher = cipher_dict[user_input]()
                    print('Your message has been decrypted:\n')
                    print(f'*'*4, cipher.decrypt(message), '*'*4)
                    break
                else:
                    print('[!] - Incorrect entry you can only encrypt '
                          'or decrypt')
                    method = input('Please try again would you like to encrypt '
                                   'or decrypt')

            print()
            another = input('Would you like to Encrypt/Decrypt'
                            ' another Y/N: ').lower()
            if another == 'y' or another == 'yes':
                continue
            else:
                break
        else:
            print('[!] -  You can only select from the ciphers listed')

if __name__ == "__main__":
    run()


# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: D:\BuildAgent\work\test/iobjectspy/ml\vision\_inference.py
# Compiled at: 2019-12-31 04:09:00
# Size of source mod 2**32: 5871 bytes
import os, tempfile
from iobjectspy import Dataset, conversion
from iobjectspy._jsuperpy._utils import check_lic
from ._inference_collector import ObjectDetection, BinaryClassification, MultiClassification, SceneClassification
from toolkit._toolkit import _is_image_file, del_dir, _get_dataset_readonly

class Inference:

    def __init__(self, input_data, model_path, out_data, out_dataset_name):
        """
        图像数据模型推理功能入口

        :param input_data: 待推理的数据
        :type  input_data: str or Dataset
        :param model_path: 模型存储路径
        :type  model_path: str
        :param out_data: 输出文件(或数据源)路径
        :type  out_data: str or Datasource or DatasourceConnectionInfo
        :param out_dataset_name: 输出文件(或数据集)名称
        :type  out_dataset_name: str
        """
        if _is_image_file(input_data):
            self.is_del_tmp_file = False
        else:
            self.is_del_tmp_file = True
            input_data = _get_dataset_readonly(input_data)
            temp_tif_path = os.path.join(tempfile.mkdtemp(), 'temp') + '.tif'
            conversion.export_to_tif(input_data, temp_tif_path)
            input_data = temp_tif_path
        self.input_data = input_data
        self.model_path = model_path
        self.out_data = out_data
        self.out_dataset_name = out_dataset_name
        check_lic()

    def object_detect_infer(self, category_name, nms_thresh=0.3, score_thresh=0.5):
        """
        影像数据目标检测

        | 支持 tif、img(Erdas Image)等影像文件，以及 jpg、png等图像文件，检测结果为GeoJSON格式文件
        | 支持SuperMap SDX下的影像数据集，检测结果为矢量线数据集

        需要注意:
            - 当 input_data 为待检测文件时，out_data 为输出文件路径，out_dataset_name 为.json后缀的文件名
            - 当 input_data 为待检测数据集时，out_data 为输出数据源路径(或数据源对象)，out_dataset_name 为数据集名

        :param category_name: 目标检测类别，支持 'plane', 'ship', 'tennis-court', 'vehicle'
        :type  category_name: list[str] or str
        :param nms_thresh: nms的阈值
        :type  nms_thresh: float
        :param score_thresh: 类别分数的阈值
        :type  score_thresh: float
        :return: None
        """
        result = ObjectDetection(self.input_data, self.model_path, category_name, self.out_data, self.out_dataset_name, nms_thresh, score_thresh).infer()
        del_dir(os.path.abspath(os.path.join(self.input_data, os.path.pardir)), self.is_del_tmp_file)
        return result

    def binary_classify_infer(self, offset, result_type, **kwargs):
        """
        遥感影像数据二元分类
        支持 tif、img(Erdas Image)等影像文件，以及 jpg、png等图像文件，分类结果为二值栅格或矢量文件
        支持SuperMap SDX下的影像数据集，分类结果为矢量或栅格数据集

        可添加关键字参数：'dsm_dataset' 输入与影像相匹配的DSM数据，实现基于DOM和DSM提取建筑物面。
        其中影像和DSM可以使用SuperMap iDesktop 桌面基于倾斜摄影数据提取：
            打开三维场景，使用 三维分析->生成DOM 三维分析->生成DSM，分辨率建议选择0.1m

        :param offset: 图像分块偏移，大幅图像需分块预测，其值为分块间重叠部分大小，以提高图像块边缘预测结果
        :type offset: int
        :param result_type: 结果返回类型，支持矢量面和栅格: 'region' or 'grid'
        :type result_type: str
        :return: 数据集名字
        """
        result = BinaryClassification((self.input_data), (self.model_path), (self.out_data), (self.out_dataset_name), offset,
         result_type, **kwargs).infer()
        del_dir(os.path.abspath(os.path.join(self.input_data, os.path.pardir)), self.is_del_tmp_file)
        return result

    def scene_classify_infer(self, result_type, **kwargs):
        """
        遥感影像数据场景分类
        支持 tif、img(Erdas Image)等影像文件，以及 jpg、png等图像文件，分类结果为二值栅格或矢量文件
        支持SuperMap SDX下的影像数据集，分类结果为矢量或栅格数据集

        :param result_type: 结果返回类型，支持矢量面和栅格: 'region' or 'grid'
        :type result_type: str
        :return: 数据集名字
        """
        result = SceneClassification((self.input_data), (self.model_path), (self.out_data), (self.out_dataset_name),
         result_type, **kwargs).infer()
        del_dir(os.path.abspath(os.path.join(self.input_data, os.path.pardir)), self.is_del_tmp_file)
        return result

    def multi_classify_infer(self, offset, result_type, **kwargs):
        """
        遥感影像数据多分类，地物分类
        支持 tif、img(Erdas Image)等影像文件，以及 jpg、png等图像文件，分类结果为多值栅格或矢量文件
        支持SuperMap SDX下的影像数据集，分类结果为矢量或栅格数据集

        :param offset: 图像分块偏移，大幅图像需分块预测，其值为分块间重叠部分大小，以提高图像块边缘预测结果
        :type offset: int
        :param result_type: 结果返回类型，支持矢量面和栅格: 'region' or 'grid'
        :type result_type: str
        :return: 数据集名字
        """
        result = MultiClassification((self.input_data), (self.model_path), (self.out_data), (self.out_dataset_name), offset,
         result_type, **kwargs).infer()
        del_dir(os.path.abspath(os.path.join(self.input_data, os.path.pardir)), self.is_del_tmp_file)
        return result


#!/usr/bin/env python
""" """

# Standard library modules.
import unittest

# Third party modules.
from qtpy import QtCore, QtGui, QtWidgets

from qtpy.QtTest import QTest #@UnusedImport

# Local modules.
from pyhmsa_gui.util.settings import Settings

# Globals and constants variables.

class MockController(QtCore.QObject):

    def __init__(self):
        self._settings = Settings("HMSA", "testcase")

    @property
    def settings(self):
        return self._settings

_instance = None

class TestCaseQApp(unittest.TestCase):
    '''Helper class to provide QApplication instances'''

    qapplication = True

    def setUp(self):
        super().setUp()
        if isinstance(QtGui.qApp, type(None)):
            self.app = QtWidgets.QApplication([])
        else:
            self.app = QtGui.qApp

        self.controller = MockController()

    def tearDown(self):
        '''Deletes the reference owned by self'''
        del self.app
        super().tearDown()


import ROOT
import sys

def openRootFile(fName, option="read"):
  oFile = ROOT.TFile.Open(fName, option)
  if oFile == 0:
    if option == "read":
      print("ERROR: Cannot open file: %s"%fName)
    else:
      print("ERROR: Cannot create file: %s"%fName)
    sys.exit(0)
  return oFile

def copyHists(source, target, hList):
  for iH in hList:
    source.cd()
    tmpH = source.Get(iH)
    if tmpH == 0:
      continue
    #copy hist
    target.cd()
    tmpH.Write()

def main():

  iFileName = "/afs/cern.ch/user/y/yabulait/workarea/multibAnalysis/stats/testHist/MVA/13TeV_TwoLepton_2tag2pjet_150ptv_SR_mva.root"
  outFileName = "/afs/cern.ch/user/y/yabulait/workarea/multibAnalysis/stats/testHist/MVA/13TeV_TwoLepton_2tag2pjet_150ptv_SR_mvaTEST.root"

  iFile = openRootFile(iFileName, "read")
  outFile = openRootFile(outFileName, "recreate")

  #Copy nominal
  copyHists(iFile, outFile, ["Zbb"])

  #Copy Sys
  sysTDir = iFile.Get("Systematics")
  if sysTDir == 0:
    print("ERROR: can not get TDir: Systematics")
    iFile.Close()
    outFile.Close()
    sys.exit(0)

  sysSaveTDir = outFile.mkdir("Systematics")
  sysSaveTDir.cd()

  sysList = ["Zbb_SysJET_21NP_JET_EffectiveNP_2__1up", "Zbb_SysJET_JvtEfficiency__1up", "Zbb_SysJET_JvtEfficiency__1down"]
  copyHists(sysTDir, sysSaveTDir, sysList)

  iFile.cd()
  iFile.Close()
  outFile.cd()
  outFile.Close()

if __name__ == "__main__":
  main()


import torch
import numpy as np
import timeit
from random import sample
from sklearn.metrics import pairwise_distances

def spearman(a, b):

    order_penalty = (a - b) ** 2
    weight = a * b

    distance = 1e4 * np.sum(order_penalty * weight)
    return distance

def spearman_pairwise_distance(X, centers=None):

    if (centers is not None):
        return torch_spearman_pairwise_distance(X, centers)

    else:
        return torch_spearman_pairwise_distance(X, X)

def torch_spearman_pairwise_distance(X, centers):

    max_calculation_size = 250000000
    calculation_size = X.shape[0] * X.shape[1] * centers.shape[0]
    size_ratio = max_calculation_size / calculation_size

    if (size_ratio >= 1):
        return torch_spearman_pairwise_distance_core(X, centers)

    else:

        nb_center_per_step = int(size_ratio * len(centers))
        if (nb_center_per_step <= 0):
            nb_center_per_step = 1

        all_column_distance = []
        for i in range(0, len(centers), nb_center_per_step):
            all_column_distance += [torch_spearman_pairwise_distance_core(X, centers[i:i+nb_center_per_step])]

        return np.concatenate(all_column_distance, axis=1)

def torch_spearman_pairwise_distance_core(X, centers):

    nb_data = X.shape[0]
    nb_cluster = centers.shape[0]
    vectSize = centers.shape[1]

    torch_x = torch.from_numpy(X).cuda()
    torch_x = torch_x.unsqueeze(1)
    torch_x = torch_x.expand(nb_data, nb_cluster, vectSize)

    torch_centers = torch.from_numpy(centers).cuda()
    torch_centers = torch_centers.expand(nb_data, nb_cluster, vectSize)

    order_penalty = (torch_x - torch_centers) ** 2
    weight = torch_x * torch_centers

    distance = 1e4 * torch.sum(order_penalty * weight, dim=2)

    return distance.data.cpu().numpy()

if __name__ == '__main__':

    nb_row = 500
    nb_feature = 20
    nb_cluster = 7
    nb_run = 5

    a = np.random.uniform(0, 10, (nb_row,nb_feature))

    cluster_index = sample(list(range(nb_row)), nb_cluster)
    b = a[cluster_index, :]

    print("\n\nSK Dist")

    sk_dist = None
    all_time = []
    for i in range(nb_run):

        start = timeit.default_timer()

        sk_dist = pairwise_distances(a, b, metric=spearman, n_jobs=12)

        stop = timeit.default_timer()
        time_in_second = stop - start
        all_time += [time_in_second]

    print(sk_dist)
    print(sk_dist.shape)
    print(np.mean(all_time))
    print(np.sum(all_time))

    print("\nTorch Dist")

    torch_dist = None
    all_time = []
    for i in range(nb_run):
        start = timeit.default_timer()

        torch_dist = spearman_pairwise_distance(a, b)

        stop = timeit.default_timer()
        time_in_second = stop - start
        all_time += [time_in_second]

    print(torch_dist)
    print(torch_dist.shape)
    print(np.mean(all_time))
    print(np.sum(all_time))

    print(np.allclose(sk_dist, torch_dist, atol=0.1))

    print("\nTorch Dist 2")

    torch_dist = None
    all_time = []
    for i in range(nb_run):
        start = timeit.default_timer()

        torch_dist = spearman_pairwise_distance(a)

        stop = timeit.default_timer()
        time_in_second = stop - start
        all_time += [time_in_second]

    print(torch_dist)
    print(torch_dist.shape)
    print(np.mean(all_time))
    print(np.sum(all_time))

    print("\n\nSK Dist 2")

    sk_dist = None
    all_time = []
    for i in range(nb_run):
        start = timeit.default_timer()

        sk_dist = pairwise_distances(a, metric=spearman, n_jobs=12)

        stop = timeit.default_timer()
        time_in_second = stop - start
        all_time += [time_in_second]

    print(sk_dist)
    print(sk_dist.shape)
    print(np.mean(all_time))
    print(np.sum(all_time))

    print(np.allclose(sk_dist, torch_dist, atol=0.1))


from PIL import Image
im = Image.open("bnn8.PNG")
pix = im.load()
h,w= im.size

for i in range(h):
    for j in range(w):
        r,g,b,h= pix[i,j]
        #av=(r+g+b)/3
        pix[i,j]=(r,b,g,h)
im.save('nabaGray4.png')


#!/usr/bin/env python3
# implementation of a cubic hermite spline

# load modules
from math import pi
import matplotlib.pyplot as plt
from pylab import *

class cubic_hermite_spline():
	def __init__(self):
		pass

	def v2d_scalar_mul (self, v, f):
		return [v[0]*f, v[1]*f]

	def v2d_add (self, v1, v2):
		return [v1[0]+v2[0], v1[1]+v2[1]]

	def goto_wpt (self, p1, t1, p2, t2, steps):
		# http://cubic.org/docs/hermite.htm
		# http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Interpolation%20on%20a%20single%20interval
		p = []
		for t in range(steps):
			s = t/(steps * 1.0) # scale s to go from 0 to 1

			# # calculate basis function
			h1 = 2*s**3 - 3*s**2 + 1
			h2 = -2*s**3 + 3*s**2
			h3 = s**3 - 2*s**2 + s
			h4 = s**3 - s**2

			# multiply and sum functions together to build the interpolated point along the curve
			v1 = self.v2d_scalar_mul(p1,h1)
			v2 = self.v2d_scalar_mul(p2,h2)
			v3 = self.v2d_scalar_mul(t1,h3)
			v4 = self.v2d_scalar_mul(t2,h4)

			p.append(self.v2d_add(self.v2d_add(self.v2d_add(v1,v2),v3),v4))
		return p

chs = cubic_hermite_spline()

p1 = [0.0, 0.0]
t1 = [0.0, 5.0]
p2 = [2.0, 0.0]
t2 = [0.0, 5.0]
steps = 50

rte = chs.goto_wpt (p1,t1,p2,t2,steps)

print (rte)

# plot route
rteT = list(zip(*rte))
rte_plt = plot(rteT[0],rteT[1],'blue')

title ('Route')
axis('equal')
xlabel('Easting [m]')
ylabel('Northing [m]')
plt.savefig ('route_plan.png')
ion() # turn interaction mode on
show()
ioff()


'''
This set of tests performs tests on all parameterized endpoints generically, any
more specific tests should be broken out into their own files.
'''

import random
import pytest

from model_mommy import mommy
from rest_framework import status

from django.apps import apps

from talentmap_api.user_profile.tests.mommy_recipes import owned_saved_search
from talentmap_api.messaging.tests.mommy_recipes import owned_notification

parameterized_fields = "endpoint, model, recipe, retrievable"
parameterized_data = [
    # Permission Endpoints
    ('/api/v1/permission/group/', 'auth.Group', None, True),

    # Saved Searches
    ('/api/v1/searches/', 'user_profile.SavedSearch', owned_saved_search, True),

    # Messaging Endpoints
    ('/api/v1/notification/', 'messaging.Notification', owned_notification, True),

    # Glossary
    ('/api/v1/glossary/', 'glossary.GlossaryEntry', None, True),
]

@pytest.mark.django_db(transaction=True)
@pytest.mark.parametrize(parameterized_fields, parameterized_data)
def test_endpoints_list(authorized_client, authorized_user, endpoint, model, recipe, retrievable):
    number = random.randint(5, 10)
    # Create a random amount of objects from the recipe, if it is given
    if recipe:
        if callable(recipe):
            for i in range(0, number):
                recipe()
        else:
            mommy.make_recipe(recipe, _quantity=number)
    elif model:
        mommy.make(model, _quantity=number)

    response = authorized_client.get(endpoint)

    assert response.status_code == status.HTTP_200_OK
    assert len(response.data["results"]) == apps.get_model(model).objects.count()
    assert len(response.data["results"]) == number

@pytest.mark.django_db(transaction=True)
@pytest.mark.parametrize(parameterized_fields, parameterized_data)
def test_endpoints_retrieve(authorized_client, authorized_user, endpoint, model, recipe, retrievable):
    # Skip any endpoints that don't support "retrieve" actions
    if not retrievable:
        return

    number = random.randint(5, 10)
    # Create a random amount of objects from the recipe, if it is given
    if recipe:
        if callable(recipe):
            for i in range(0, number):
                recipe()
        else:
            mommy.make_recipe(recipe, _quantity=number)
    elif model:
        mommy.make(model, _quantity=number)

    # Check that each item is retrievable
    for id in apps.get_model(model).objects.values_list('id', flat=True):
        response = authorized_client.get(f"{endpoint}{id}/")
        assert response.status_code == status.HTTP_200_OK


#!/usr/bin/env python

import setuptools

_depends = '''
numpy
'''

setuptools.setup( \
  name='automatamm', \
  version='git', \
  description='automata-- wrapper for python', \
  author='Brandon Niemczyk', \
  author_email='brandon.niemczyk@gmail.com', \
  url='http://github.com/bniemczyk/automata--', \
  py_modules=['automatamm', 'smbuilder'], \
  test_suite='tests', \
  license='BSD', \
  install_requires=_depends, \
  classifiers = [ \
    'Development Status :: 3 - Alpha', \
    'Intended Audience :: Developers', \
    'Intended Audience :: Science/Research', \
    'License :: OSI Approved :: BSD License', \
    'Topic :: Scientific/Engineering :: Mathematics' \
    ]
  )


"""
Read gap data and output gams file
Date:2021-3-5
"""
class Read_GAP_data:
    def __init__(self):
        self.file="input.txt"
        self.g_number_of_agents=0
        self.g_number_of_jobs=0
    def read_data(self):
        with open(self.file,"r") as fl:
            lines = fl.readlines()
            #data-1:number of agents and jobs
            line_1=lines[0].strip().split()
            self.g_number_of_agents=int(line_1[0])
            self.g_number_of_jobs=int(line_1[1])
            #data-2:resouce
            # line_1 = lines[-1].strip().split()
            # self.g_agent_resouce_list=[0]*self.g_number_of_agents
            # for m in range(self.g_number_of_agents):
            #     self.g_agent_resouce_list[m]=int(line_1[m])
            #data-3:cost
            Flag=0
            self.cost_matrix=[]
            self.resouce_matrix=[]
            empty_list = []
            for line in lines[1:]:
                line_1 = line.strip().split()
                if len(empty_list)==self.g_number_of_jobs:
                    if Flag<self.g_number_of_agents:
                        self.cost_matrix.append(empty_list)
                        empty_list=[]
                        Flag+=1
                    else:
                        self.resouce_matrix.append(empty_list)
                        empty_list = []
                        Flag += 1
                for item in line_1:
                    empty_list.append(int(item))
                    # empty_list.append(int(item))
                if len(empty_list)==self.g_number_of_agents and line==lines[-1]:
                    self.g_agent_resouce_list=empty_list
        print("read data is finished!")
# put data in list
        self.agent_list=[]
        self.job_list=[]
        for m in range(self.g_number_of_agents):
            agent=Agent()
            agent.agent_id=m
            agent.resource=self.g_agent_resouce_list[m]
            agent.cost_list_each_job=self.cost_matrix[m]
            agent.resource_list_each_job=self.resouce_matrix[m]
            self.agent_list.append(agent)

        for j in range(self.g_number_of_jobs):
            #initialize the multipliers
            multiplier_for_j=10000
            for m in range(self.g_number_of_agents):
                if multiplier_for_j>self.cost_matrix[m][j]:
                    multiplier_for_j = self.cost_matrix[m][j]

            job=Job()
            job.job_id=j
            job.multiplier=multiplier_for_j*(-1)
            job.multiplier_ALR=multiplier_for_j*(-1)
            self.job_list.append(job)
        #output GAMS file
        self.output_GAMS_file()
        return self.g_number_of_agents,self.g_number_of_jobs,self.agent_list,self.job_list

    def output_GAMS_file(self):
        with open("GAMS_input_file.txt", "w") as fl:
            fl.write("set j job /0*{}/;\n".format(self.g_number_of_jobs - 1))
            fl.write("set i machine /0*{}/;\n".format(self.g_number_of_agents - 1))

            fl.write("parameter c(i,j)/\n")
            for i in range(self.g_number_of_agents):
                agent = self.agent_list[i]
                for j in range(self.g_number_of_jobs):
                    cost = agent.cost_list_each_job[j]
                    fl.write(str(i) + ". " + str(j) + " " + str(cost) + "\n")
            fl.write("/;\n")

            fl.write("parameter a(i,j)/\n")
            for i in range(self.g_number_of_agents):
                agent = self.agent_list[i]
                for j in range(self.g_number_of_jobs):
                    resource = agent.resource_list_each_job[j]
                    fl.write(str(i) + ". " + str(j) + " " + str(resource) + "\n")
            fl.write("/;\n")

            fl.write("parameter b(i)/\n")
            for i in range(self.g_number_of_agents):
                resource = self.agent_list[i].resource
                fl.write(str(i) + " " + str(resource) + "\n")
            fl.write("/;\n")

# a=Read_GAP_data()
# a.read_data()
class Agent:#machine
    def __init__(self):
        self.agent_id=0
        self.resource=0
        self.cost_list_each_job=None
        self.resource_list_each_job=None

class Job:
    def __init__(self):
        self.job_id=0
        self.multiplier=0
        self.multiplier_ALR=0 #三项


import pandas as pd
import numpy as np
#收集資料 https://data.humdata.org/dataset/novel-coronavirus-2019-ncov-cases

#讀取資料
data = pd.read_csv("time_series_covid19_confirmed_global.csv")

#觀察資料
# print("資料數量", data.shape) # 幾欄、幾列
# print("資料欄位", data.columns)
# print(data["Country/Region"][240])

condition = data["Country/Region"] == "Taiwan*" # 布林值
twindex = data[condition].index # Taiwan* 的索引值為 240
twdata = np.array(data.iloc[twindex])
twdata = twdata[0][4:] # 此為每日累積確診人數
# column=0~3 ：Province/State, Country/Region, Lat, Long

twdata2 = twdata.copy() # 另存一份，用以紀錄每日新增確診人數
print(twdata)

for i in range(1,len(twdata)):
    twdata2[i] = twdata[i] - twdata[i-1] # 紀錄每日新增確診人數
print(twdata2)
print(data.columns[4:])

import matplotlib.pyplot as plt

index = np.arange(len(twdata)) # 若直接印出日期 X 軸太多文字 #index = data.columns[4:]

# plt.plot(index, twdata) # 累積人數
# plt.xlabel("Date") # X 軸
# plt.ylabel("person") # Y 軸
# plt.title("COVID-19 accumulation confirmed of Taiwan") # 圖表名稱
# plt.show() # 印出圖表

# plt.plot(index, twdata2)
# plt.xlabel("Date")
# plt.ylabel("person")
# plt.title("COVID-19 confirmed of Taiwan")
# plt.show()

plt.plot(index, twdata)
plt.plot(index, twdata2*15) # 每日確診人數的數值放大 20 倍，僅為方便觀察
plt.xlabel("Date")
plt.ylabel("person")
plt.title("COVID-19 confirmed and accumulation confirmed of Taiwan")
plt.show()


import pyautogui
import cv2
from time import sleep
import keyboard
import numpy as np

def clippaperclip():
    sleep(5)
    pyautogui.screenshot('img/screenshot.png')
    img_rgb = cv2.imread('img/screenshot.png')
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    template = cv2.imread('img/paperclip.png', 0)
    res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
    threshold = 0.8
    loc = np.where(res >= threshold)

    spacepress = False
    for pt in zip(*loc[::-1]):
        count = 0
        print("Clicking...")
        if spacepress:
            quit()
        while True:  # making a loop
            try:  # used try so that if user pressed other than the given key error will not be shown
                if keyboard.is_pressed('q'):  # if key 'q' is pressed
                    spacepress = True
                    break  # finishing the loop
                else:
                    pyautogui.click(pt[0] + 20, pt[1] + 5)
            except:
                pass

if __name__ == '__main__':
    clippaperclip()


#!/usr/bin/env python3
from __future__ import print_function

import random
import string
import subprocess
from random import shuffle
from flask import Flask, render_template, request, jsonify, abort, session
from mongo_db import store_valid_in_mongo, replete_valid_db, store_anno_in_mongo, replete_anno_db
from nmtkeras.sample_ensemble import predict
from proces_and_convert_to_char import process, convert_char, restore
from byte_pair_loading import bpe, bpe_nl, graph
from nmtkeras.sample_ensemble import load_in

# from loading_models import *
# initialize the Flask application and the Keras model
app = Flask(__name__)

app.secret_key = ''.join(random.choice(string.printable) for _ in range(20))

char_args, char_params, char_models, char_dataset = load_in("char", "NL_GRO")
char_args_nl, char_params_nl, char_models_nl, char_dataset_nl = load_in("char", "GRO_NL")

bpe_args, bpe_params, bpe_models, bpe_dataset = load_in("BPE", "NL_GRO")
bpe_args_nl, bpe_params_nl, bpe_models_nl, bpe_dataset_nl = load_in("BPE", "GRO_NL")

# to use flask.session, a secret key must be passed to the app instance

def bpe_encode(input):
    return "".join(bpe.process_line(input))

def bpe_encode_nl(input):
    return "".join(bpe_nl.process_line(input))

def get_predictions(text, args, params, models, dataset):
    pred = predict(text, args, params, models, dataset)
    return pred

"""Flask env """

@app.route('/')
def home():
    return render_template('vertaal.html')

def update_anno(read_items=None):
    if read_items is None:
        read_items = []
    files = replete_anno_db(read_items)
    all = [(str(instance._id), instance.orginal_gronings) for instance in files if
           str(instance._id) not in read_items]
    return all

def update_valid(read_items=None):
    if read_items is None:
        read_items = []
    files = replete_valid_db(read_items)
    all = [(str(instance._id), instance.annotated_gronings, instance.orginal_gronings) for
           instance in files if
           str(instance._id) not in read_items]
    return all

@app.route('/help', methods=['GET'])
def display_sent():
    """function to return the HTML page to display the sentence"""
    session['count'] = 0

    if "read_items" in session:
        read_items = session.get('read_items', None)
        session['read_items'] = read_items
        all = update_anno(read_items)

    else:
        all = update_anno()
    shuffle(all)

    return render_template('help.html', all=all, count=session['count'])

@app.route('/get_anno', methods=['GET'])
def get_anno():
    _direction = request.args.get('direction')
    count = session.get('count', None)
    session['count'] = count
    session['count'] = session['count'] + (1 if _direction == 'f' else - 1)
    if "read_items" in session:
        read_items = session.get('read_items', None)
        session['read_items'] = read_items
        all = update_anno(read_items)

    else:
        all = update_anno()

    return jsonify(
        {'forward': str(session['count'] + 1 < len(all)),
         'back': str(bool(session['count'])), "count": session['count'], "all": all})

@app.route('/store_in_mongo', methods=['POST'])
def store_in_mongo():
    if request.method == 'POST':
        anno = request.form['annotation']
        original_id = request.form['original_id']
        store_anno_in_mongo(anno, original_id)
        if 'read_items' in session:
            read_items = session.get('read_items', None)
            read_items.append(str(original_id))
            session['read_items'] = read_items
        else:
            session['read_items'] = [str(original_id)]

        if "read_items" in session:
            read_items = session.get('read_items', None)
            session['read_items'] = read_items
            all = update_anno(read_items)

        else:
            all = update_anno()
        count = session.get('count', None)

        if int(count) != 0:
            session['count'] = count - 1

        return jsonify({"count": session['count'], "all": all})

@app.route('/validation', methods=['GET'])
def display_validation():
    """function to return the HTML page to display the sentence"""
    session['validation_count'] = 0

    if "read_validations" in session:
        read_items = session.get('read_validations', None)
        session['read_validations'] = read_items
        all = update_valid(read_items)

    else:
        all = update_valid()

    shuffle(all)

    return render_template('validation.html', all=all, count=session['validation_count'])

@app.route('/get_validations', methods=['GET'])
def get_validations():
    _direction = request.args.get('direction')
    val_count = session.get('validation_count', None)
    session['validation_count'] = val_count
    session['validation_count'] = session['validation_count'] + (1 if _direction == 'f' else - 1)

    if "read_validations" in session:
        read_items = session.get('read_validations', None)
        session['read_validations'] = read_items
        all = update_valid(read_items)

    else:
        all = update_valid()

    return jsonify(
        {'forward': str(session['validation_count'] + 1 < len(session['all_validations'])),
         'back': str(bool(session['validation_count'])), "count": session['validation_count'],
         'all': all})

@app.route('/store_validation_in_mongo', methods=['POST'])
def store_validation_in_mongo():
    if request.method == 'POST':
        original_id = request.form['original_id']
        best = request.form['best_pick']
        store_valid_in_mongo(best, original_id)
        if 'read_validations' in session:
            read_items = session.get('read_validations', None)
            read_items.append(str(original_id))
            session['read_validations'] = read_items
        else:
            session['read_validations'] = [str(original_id)]

        if "read_validations" in session:
            read_items = session.get('read_validations', None)
            session['read_validations'] = read_items
            all = update_valid(read_items)

        else:
            all = update_valid()

        count = session.get('validation_count', None)
        if int(count) != 0:
            session['validation_count'] = count - 1

        return jsonify({'count': session['validation_count'],
                        'all_validations': all,
                        'data': render_template('response.html', count=session['validation_count'],
                                                all_validations=all)})

"""Char NL"""

@app.route('/predict_CHAR_nl-gro', methods=['POST'])
def predict_predict_nl_gro():
    # Validation
    if request.method == 'POST':
        translation_query = request.form['translation']
        translation_query = translation_query.strip()

        if translation_query[-1] not in string.punctuation:
            translation_query = translation_query + "."
            punctuation_alert = True
        else:
            punctuation_alert = False
        # Preprocess
        processed = process(translation_query)
        # Tokenize to Char
        char_encoding = convert_char(processed)
        # Translate
        with graph.as_default():
            output = get_predictions(char_encoding, char_args, char_params, char_models, char_dataset)
        # Detokenize and restore
        output_sen = restore(output)
        if punctuation_alert:
            output_sen = output_sen[0:-1]
        return jsonify({'translation': output_sen})
    else:
        return abort(404)

"""Char gro nl"""

@app.route('/predict_CHAR_gro-nl', methods=['POST'])
def predict_gro_nl():
    # Validation
    if request.method == 'POST':
        translation_query = request.form['translation']
        translation_query = translation_query.strip()

        if translation_query[-1] not in string.punctuation:
            translation_query = translation_query + "."
            punctuation_alert = True
        else:
            punctuation_alert = False
        # Preprocess
        processed = process(translation_query)
        # Tokenize to Char
        char_encoding = convert_char(processed)
        # Translate
        with graph.as_default():
            output = get_predictions(char_encoding, char_args_nl, char_params_nl, char_models_nl, char_dataset_nl)
        # Detokenize and restore
        output_sen = restore(output)
        if punctuation_alert:
            output_sen = output_sen[0:-1]

        return jsonify({'translation': output_sen})

    else:
        return abort(404)

"""BPE NL GRO"""

@app.route('/predict_BPE_nl-gro', methods=['POST'])
def predict_nl_gro_bpe():
    # Validation
    if request.method == 'POST':
        translation_query = request.form['translation']
        translation_query = translation_query.strip()

        if translation_query[-1] not in string.punctuation:
            translation_query = translation_query + "."
            punctuation_alert = True
        else:
            punctuation_alert = False
        # Preprocess
        encoded_text = bpe_encode(translation_query)

        # Translate
        with graph.as_default():
            output = get_predictions(encoded_text, bpe_args, bpe_params, bpe_models, bpe_dataset)
        # Detokenize and restore
        output_string = "".join(output)

        decode_string = subprocess.run(['bash', 'restore.sh', output_string],
                                       stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False,
                                       check=True,
                                       text=True)
        output_sen = decode_string.stdout
        if punctuation_alert:
            output_sen = output_sen[0:-1]

        return jsonify({'translation': output_sen})

    else:
        return abort(404)

"""BPE GRO NL"""

@app.route('/predict_BPE_gro-nl', methods=['POST'])
def predict_gro_nl_bpe():
    # Validation
    if request.method == 'POST':
        translation_query = request.form['translation']
        translation_query = translation_query.strip()

        if translation_query[-1] not in string.punctuation:
            translation_query = translation_query + "."
            punctuation_alert = True
        else:
            punctuation_alert = False
        # Preprocess
        encoded_text = bpe_encode_nl(translation_query)

        with graph.as_default():

            output = get_predictions(encoded_text, bpe_args_nl, bpe_params_nl, bpe_models_nl, bpe_dataset_nl)
        # Detokenize and restore
        output_string = "".join(output)

        decode_string = subprocess.run(['bash', 'restore.sh', output_string],
                                       stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False,
                                       check=True,
                                       text=True)
        output_sen = decode_string.stdout
        if punctuation_alert:
            output_sen = output_sen[0:-1]

        return jsonify({'translation': output_sen})

    else:
        return abort(404)

if __name__ == '__main__':
    print(("* Loading Keras model and Flask starting server...""please wait until server has fully started"))

    app.run(debug=False, use_reloader=False)


#!/usr/bin/env python3

"""
Align sequences using a given substitution matrix

Usage:
    homework3 align <substitution_matrix> <sequence_pairs> [options]
    homework3 gaps
    homework3 thresholds
    homework3 compare [options]
    homework3 optimize <matrix_to_optimize>

Arguments:
    <substitution_matrix>
        Name of the substitution matrix to use for the alignments

    <sequence_pairs>
        Name of file containing space delimited pairs of sequences to align

    align
        Run alignment with specified substitution matrix and sequence pairs

    thresholds
        Run routine to determine the 0.7 score threshold for each gap opening/
        extension penalty combination

    gaps
        Run routine to determine optimal gap penalties for the BLOSUM50 matrix
        using the previously determined threshold scores

    compare
        Compare substitution matrices in terms of false positive rate. Also
        generate ROC curves

    optimize
        Run algorithm to optimize scoring matrix...

    <matrix_to_optimize>
        Name of the matrix to run optimization on

Options:
    -n --normalize
        Normalize the raw scores from the alignment by the length of the
        shorter of the two sequences

    -o --output <path>
        Save alignment output to a file named <path>

    -c --compare_optimized <matrix>
        Compare 1) default matrix, 2) optimized scoring matrix against default
        matrix alignments, and 3) optimized scoring matrix against optimized
        alignments

"""

if __name__ == '__main__':
    from .align import align
    from .util import determine_thresholds, determine_gap_penalties, run_alignments, compare_matrices, compare_optimized, matrix_optimization

    import docopt
    import re
    import collections
    import os
    import sys
    import numpy as np
    import pandas as pd
    from Bio import SeqIO
    import seaborn as sns
    import matplotlib.pyplot as plt

    args = docopt.docopt(__doc__)
    seq_align = align()

    # Set substitution matrix
    if args['align']:
        # Initialize variables and stuff

        substitution_matrices = {'BLOSUM50': 6,
                                 'BLOSUM62': 6,
                                 'BLOSUM62-Optimized': 0,
                                 'MATIO': 2,
                                 'MATIO-Optimized': 0,
                                 'PAM100': 9,
                                 'PAM100-Optimized': 0,
                                 'PAM250': 9
                                 }

        seq_align.substitution_matrix = pd.read_table(open('./{}'.format(args['<substitution_matrix>'])),
                                                      delim_whitespace=True,
                                                      header=substitution_matrices[args['<substitution_matrix>']]
                                                      )

        seq_align.substitution_matrix = seq_align.substitution_matrix.set_index(seq_align.substitution_matrix.columns.values)

        seq_align.substitution_matrix.to_csv('{}.csv'.format(args['<substitution_matrix>']))

        seq_align.working_pairs = open(args['<sequence_pairs>'])
        run_alignments(seq_align, args['--output'])

    if args['thresholds'] == True:
        determine_thresholds()

    if args['gaps'] == True:
        determine_gap_penalties()

    if args['compare'] == True:
        if args['--normalize']:
            normalize = True
        else:
            normalize = False

        if args['--compare_optimized']:
            compare_optimized(args['--compare_optimized'])
        else:
            compare_matrices(normalize)

    if args['optimize'] == True:
        matrix_optimization(args['<matrix_to_optimize>'])


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

from odoo import models, fields, api
from datetime import timedelta
import json

class DependingTasks(models.Model):
    _name = "project.depending.tasks"
    _description = "The many2many table that has extra info (relation_type)"

    task_id = fields.Many2one('project.task')
    project_id = fields.Many2one(related='task_id.project_id')
    depending_task_id = fields.Many2one('project.task')
    relation_type = fields.Selection([
        ("0", "Finish to Start"),
        ("1", "Start to Start"),
        ("2", "Finish to Finish"),
        ("3", "Start to Finish")
    ], default="0", required=True)
    state = fields.Selection([('draft', 'Draft'), ('confirm', 'Confirm'), ('done', 'Done')], default='draft')

class Task(models.Model):
    _inherit = "project.task"

    planned_duration = fields.Integer("Planned Duration (in Days)")
    date_start = fields.Datetime('Start Date')
    date_end = fields.Datetime('End Date')
    progress = fields.Float('Progress')
    is_open = fields.Boolean('Open')
    depending_task_ids = fields.One2many('project.depending.tasks', 'task_id')
    dependency_task_ids = fields.One2many('project.depending.tasks', 'depending_task_id')
    links_serialized_json = fields.Char('Serialized Links JSON', compute="compute_links_json")

    @api.multi
    def compute_links_json(self):
        for r in self:
            links = []
            r.links_serialized_json = '['
            for link in r.dependency_task_ids:
                json_obj = {
                    'id': link.id,
                    'source': link.task_id.id,
                    'target': link.depending_task_id.id,
                    'type': link.relation_type
                }
                links.append(json_obj)
            r.links_serialized_json = json.dumps(links)

    def duration_between_dates(self, date_from, date_to):
        return (date_to - date_from).days

    def add_days(self, target_date, days):
        return target_date + timedelta(days=days)

    @api.multi
    def compute_critical_path(self):
        # evidently the critical path is the longest path on the network graph
        project = self.project_id
        tasks = project.task_ids.sorted('date_start')
        critical_path = []
        # last_end_date = False
        current_task = tasks and tasks[0] or False
        while current_task:
            critical_path.append(current_task)
            print(current_task.depending_task_ids)
            depending_tasks = current_task.depending_task_ids.mapped('depending_task_id')
            sorted_by_duration = depending_tasks.sorted('planned_duration', True)
            current_task = sorted_by_duration and sorted_by_duration[0] or False
        print('critical_path')
        txt = ''
        for path in critical_path:
            txt += str(path.date_start) + ' >> '
        print(txt)


from time import sleep

from appium import webdriver

class TestWebViewXueQiu():
    def setup(self):
        desired_caps = {
            "platformName": "android",
            "platformVersion": "6.0",
            "deviceName": "127.0.0.1:7555",
            "appPackage": "com.xueqiu.android",
            "appActivity": "com.xueqiu.android.common.MainActivity",
            "noReset": True,  # 保留上次操作痕迹（比如登录状态）
            "dontStopAppOnReset": True,
            "unicodeKeyBoard": True,
            "resetKeyBoard": True,
            "chromedriverExecutable": "C:/Users/Administrator/Desktop/chromedriver52.exe"
        }
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        self.driver.implicitly_wait(5)

    def teardown(self):
        self.driver.quit()

    def test_xueqiuweb(self):
        self.driver.find_element_by_xpath(
            "//android.widget.TextView[@resource-id='com.xueqiu.android:id/tab_name' and @text='交易']").click()
        print(self.driver.contexts)

        self.driver.find_element_by_xpath("//android.view.View[@content-desc='A股开户']").click()
        sleep(3)
        print(self.driver.contexts)

        self.driver.switch_to.context(self.driver.contexts[-1])
        self.driver.find_element_by_id('phone-number').send_keys('15986612345')
        self.driver.find_element_by_id('code').send_keys('123456')
        self.driver.find_element_by_xpath('//*[@class="btn-submit"]').click()


""" The basic field attributes. """

from pymongo.dbref import DBRef

class EmptyRequiredField(Exception):
    """ Raised when a required field is not set on the model instance. """
    pass

class Field(object):
    """
    This class may eventually do type-checking, default values,
    etc. but for right now it's for subclassing and glorified
    documentation.
    """

    default = None
    value_type = None

    def __init__(self, value_type=None, **kwargs):
        self.value_type = value_type
        self.required = kwargs.get("required", False) is True
        self.default = kwargs.get("default", None)
        self._set_callback = kwargs.get("set_callback")
        self._get_callback = kwargs.get("get_callback")
        self.id = id(self)

    def __get__(self, instance, klass=None):
        if instance is None:
            # Classes see the descriptor itself
            return self
        value = self._get_value(instance)
        return value

    def _get_field_name(self, instance):
        """ Try to retrieve field name from instance """
        fields = getattr(instance, "_fields")
        return fields[self.id]

    def _get_value(self, instance):
        """ Retrieve the value from the instance """
        field_name = self._get_field_name(instance)
        if not instance.has_key(field_name):
            if self.required:
                raise EmptyRequiredField("'%s' is required but is empty."
                                         % field_name)
            else:
                instance[field_name] = self._get_default()
        value = instance[field_name]
        if self._get_callback:
            value = self._get_callback(value)
        return value

    def _get_default(self):
        """ Retrieve the default value and return it """
        if callable(self.default):
            return self.default()
        else:
            return self.default

    def _check_value_type(self, value):
        """ Verifies that a value is the proper type """
        if value is not None and self.value_type is not None:
            valid = isinstance(value, self.value_type)
            if not valid:
                return False
        return True

    def __set__(self, instance, value):
        value_type = type(value)
        if not self._check_value_type(value):
            try:
                value = self.value_type(value)
            except:
                raise TypeError("Invalid type %s instead of %s" %
                    (value_type, self.value_type)
                )
        if self._set_callback:
            value = self._set_callback(value)
        field_name = self._get_field_name(instance)
        instance[field_name] = value

class ReferenceField(Field):
    """ Simply holds information about the reference model. """

    def __init__(self, model, **kwargs):
        kwargs.setdefault("set_callback", self._set_callback)
        kwargs.setdefault("get_callback", self._get_callback)
        super(ReferenceField, self).__init__(model, **kwargs)
        self.model = model

    def _set_callback(self, value):
        """ Resolves a Model to a DBRef """
        if value:
            value = DBRef(self.model._get_name(), value.id)
        return value

    def _get_callback(self, value):
        """ Retrieves the id, then retrieves the model from the db """
        if value:
            # Should be a DBRef
            return self.model.find_one({"_id": value.id})
        return value


from miniFontTools.bezierTools import calcCubicParameters, solveQuadratic, splitCubicAtT

# Helper functions

def getExtremaForCubic(pt1, pt2, pt3, pt4, h=True, v=False):
    (ax, ay), (bx, by), c, d = calcCubicParameters(pt1, pt2, pt3, pt4)
    ax *= 3.0
    ay *= 3.0
    bx *= 2.0
    by *= 2.0
    h_roots = []
    v_roots = []
    if h:
        h_roots = [t for t in solveQuadratic(ay, by, c[1]) if 0 < t < 1]
    if v:
        v_roots  = [t for t in solveQuadratic(ax, bx, c[0]) if 0 < t < 1]
    roots = h_roots + v_roots
    return [p[3] for p in splitCubicAtT(pt1, pt2, pt3, pt4, *roots)[:-1]]

# Classes

class RedArrowError(object):
    def __init__(self, position, kind, badness=1):
        self.position = position
        self.kind = kind
        self.badness = badness

    def __repr__(self):
        return "%s at (%i, %i)" % (self.kind, self.position[0], self.position[1])