import pymysql
from connect import connection
from datetime import datetime , timedelta
import re
import pandas as pd
import time

#transform list of id to str O
def listIdToStr(id_list):
    if id_list == None or len(id_list) == 0:
        return ","
    participant = str(id_list[0])
    for i in range(1 , len(id_list)):
        participant = participant + "," + str(id_list[i])
    return participant

#insert record O
def insertRecord(title = "testing record", roomname  = "TR-306", startDate = "2020-12-30", startSection  = "5", \
    endDate = "2020-12-30", endSection  = "8", participant = ['jerry','alien','wacky'], bookName = "jerry",type = "0"):

    #get booker userid
    sql = "SELECT  `userID` FROM `users` WHERE `userName`= %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,bookName)
        connection.commit()
        B_ID = cursor.fetchone()["userID"]

    #get classroom CR_ID
    sql = "SELECT  `CR_ID` FROM `classroom` WHERE `roomname`= %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,roomname)
        connection.commit()
        CR_ID = cursor.fetchone()["CR_ID"]
    print("CR_ID = "  + str(CR_ID))
    #process participant
    p_id = []
    sql = "SELECT  `userID` FROM `users` WHERE `userName`= %s"
    for ppl in participant:
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql,ppl)
            connection.commit()
            result = cursor.fetchone()
            if result != None:
                p_id.append(result["userID"])
    p_id_str = listIdToStr(p_id)
    print("p_id_str = " + str(p_id_str))
    #insert record into database
    sql = "INSERT INTO `record` (`title`, `CR_ID`,`startDate` , `startSection` , `endDate` , `endSection` , `participant` ,  `B_ID` , `type`) \
    VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,(title,CR_ID,startDate , startSection , endDate, endSection , p_id_str , B_ID,type))
        connection.commit()

#XX not for front-end , return dict of records(one section one record) retrun none if date not match
def processRecord(record,date):
    if str(record["startDate"]) != date:
        return None
    if record["startDate"] < record["endDate"]:
        endSection = 14
    else :
        endSection = record["endSection"]
    recordDict = {}
    for i in range(record["startSection"] , endSection + 1):
        #type , title , bookerName
        sql = "SELECT  `userName` FROM `users` WHERE `userID`= %s"
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql,record["B_ID"])
            connection.commit()
            name = cursor.fetchone()["userName"]
        recordDict[i] = (record["type"],record["title"] , name)
    return recordDict

#give the condition to search classroom
#parameter (building ,capacity ,roomname ,date)
def searchClassroom(building, capacity = -1 , roomname = "" , date = "2020-01-01"):
    if roomname != None and roomname != "":
        if  building != "":
            if roomname[0:2] != building:
                return None
        if  capacity != None and capacity != "" and int(capacity) > 0:
            sql = "SELECT * FROM classroom WHERE `roomname` = %s AND `capacity` >= %s"
            condition = (roomname,capacity)
        else:
            sql = "SELECT * FROM classroom WHERE `roomname` = %s"
            condition = roomname
    else:
        if capacity != None and capacity != "" and int(capacity) > 0:
            if building != "":
                sql = "SELECT * FROM classroom WHERE `building` = %s AND `capacity` >= %s"
                condition = (building,capacity)
            else:
                sql = "SELECT * FROM classroom WHERE `capacity` >= %s"
                condition = capacity
        elif building != "":
            sql = "SELECT * FROM classroom WHERE `building` = %s"
            condition = building
        else:
            print("no condition")
    print(condition)
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,condition)
        connection.commit()
        result = cursor.fetchall()
    output = []
    sql = "SELECT * FROM record WHERE `CR_ID` = %s AND `startDate` = %s"
    print(result)
    for i in result:
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql,(i["CR_ID"],date))
            connection.commit()
            tmp = cursor.fetchall()
            if tmp != ():
                for j in tmp:
                    item = processRecord(j , date)
                    #building , capacity , roomname , status
                    item = {"CR_ID" : i["CR_ID"] , "building" : i["building"] , "capacity" : i["capacity"] , "roomName" : i["roomname"] , "status" : item}
                    output.append(item)
            else:
                item = {"CR_ID" : i["CR_ID"] , "building" : i["building"] , "capacity" : i["capacity"] , "roomName" : i["roomname"] , "status" : {}}
                output.append(item)
    return output

#search for one classroom and return records of a week
# return format:  dict{ 'CR_ID' : CR_ID ,'building' : building , 'capacity' : capacity , 'roomName' : roomname , 'status' : dict{id : tuple()}(7days) }
#parameter (classroom name , date of the week)
def searchOneClassroom(CR_ID = "" , date = "2020-12-29") :
    sql = "SELECT * FROM classroom WHERE `CR_ID` = %s"
    with connection.cursor() as cursor:
        cursor.execute(sql,CR_ID)
        connection.commit()
        result = cursor.fetchone()
    building = result["building"]
    capacity = result["capacity"]
    roomname = result["roomname"]
    today = datetime.fromisoformat(date)
    n = today.weekday()
    delta = timedelta(days = n)
    startDay = str((today - delta).date())
    delta = timedelta(days = 6 - n)
    endDay = str((today + delta).date())
    print(startDay + " ~ " + endDay)
    sql = "SELECT * FROM record WHERE `CR_ID` = %s AND `startDate` >= %s AND `endDate` <= %s"
    output = []
    with connection.cursor() as cursor:
        cursor.execute(sql,(CR_ID,startDay,endDay))
        connection.commit()
        tmp = cursor.fetchall()
        if tmp != ():
            for d in range(0,7):
                delta = timedelta(days = n - d)
                dailyItem = {}
                for j in tmp:
                    item = processRecord(j,str((today - delta).date()))
                    if item != None:
                        dailyItem.update(item)
                output.append(dailyItem)
            return {'CR_ID' : CR_ID , 'building' : building , 'capacity' : capacity , 'roomName' :roomname , 'status' : output}
        else:
            for d in range(0,7):
                output.append({})
            return {'CR_ID' : CR_ID , 'building' : building , 'capacity' : capacity , 'roomName' :roomname , 'status' : output}

#search the records the user book
def getRecordByBooker(userName):
    sql = "SELECT  `userID` FROM `users` WHERE `userName`= %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,userName)
        connection.commit()
        B_ID = cursor.fetchone()["userID"]

    sql = "SELECT  * FROM `record` WHERE `B_ID`= %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,B_ID)
        connection.commit()
        results = cursor.fetchall()
    #sql1 = "SELECT `userName`  FROM `users` WHERE `userID`= %s"
    sql2 = "SELECT `roomname`  FROM `classroom` WHERE `CR_ID`= %s"
    for result in results:
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql2,result['CR_ID'])
            connection.commit()
            tmp = cursor.fetchone()
        result['roomName'] = tmp
        p_name = []
        participants = result['participant']
        participants = participants.split(',')
        if len(participants) == 0:
            continue
        sql1 = "SELECT `userName` FROM `users` WHERE `userID` IN ({seq})".format(seq=','.join(['%s']*len(participants)))
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql,participants)
            connection.commit()
            tmp = cursor.fetchall()
        for i in tmp:
            if i != None:
                p_name.append(i['userName'])
        result['participant'] = p_name
    return results

#search the records the user's email book
def getRecordByBookerEmail(email):
    sql = "SELECT  `userID` FROM `users` WHERE `email`= %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,email)
        connection.commit()
        B_ID = cursor.fetchone()["userID"]

    sql = "SELECT  * FROM `record` WHERE `B_ID`= %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,B_ID)
        connection.commit()
        results = cursor.fetchall()
    #sql1 = "SELECT `userName`  FROM `users` WHERE `userID`= %s"
    sql2 = "SELECT `roomname`  FROM `classroom` WHERE `CR_ID`= %s"

    for result in results:
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql2,result['CR_ID'])
            connection.commit()
            tmp = cursor.fetchone()
        result['roomName'] = tmp['roomname']
        p_name = []
        participants = result['participant']
        participants = participants.split(',')
        if len(participants) == 0:
            continue
        sql1 = "SELECT `userName` FROM `users` WHERE `userID` IN ({seq})".format(seq=','.join(['%s']*len(participants)))
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql1,participants)
            connection.commit()
            tmp = cursor.fetchall()
        for i in tmp:
            if i != None:
                p_name.append(i['userName'])
        result['participant'] = p_name
    return results

#get the record by id
def getRecordById(id):
    sql = "SELECT * FROM `record` WHERE `recordID` = %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,id)
        connection.commit()
        result = cursor.fetchone()
    if result == None:
        print("no id of this result")
        return None
    sql1 = "SELECT `userName`  FROM `users` WHERE `userID`= %s"
    sql2 = "SELECT `roomname`  FROM `classroom` WHERE `CR_ID`= %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql2,result['CR_ID'])
        connection.commit()
        tmp = cursor.fetchone()
    result['roomName'] = tmp['roomname']
    p_name = []
    participants = result['participant']
    participants = participants.split(',')
    for i in participants:
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql1,i)
            connection.commit()
            tmp = cursor.fetchone()
            if tmp != None:
                p_name.append(tmp['userName'])
            else :
                print(i)
    result['participant'] = p_name
    return result

#update record by record id
def updateRecord(recordID , title ,participants):
    if title != None and title != "":
        sql = "UPDATE `record` SET `title` = %s WHERE `recordID` = %s"
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql,(title,recordID))
            connection.commit()
    if participants != None and len(participants) != 0:
        p_id = []
        """
        sql = "SELECT  `userID` FROM `users` WHERE `userName`= %s FOR UPDATE"
        for ppl in participants:
            connection.ping(reconnect = True)
            with connection.cursor() as cursor:
                cursor.execute(sql,ppl)
                result = cursor.fetchone()
                if result != None:
                    p_id.append(result["userID"])
        """
        sql = "SELECT `userID` FROM `users` WHERE `userName` IN ({seq})".format(seq=','.join(['%s']*len(participants)))
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql,participants)
            connection.commit()
            result = cursor.fetchall()
        for i in result:
            if i != None:
                p_id.append(i["userID"])
        p_id_str = listIdToStr(p_id)
        sql = "UPDATE `record` SET `participant` = %s WHERE `recordID` = %s"
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql,(p_id_str,recordID))
            connection.commit()
    return True

def deleteRecord(recordID):
    sql = "DELETE FROM `record` WHERE `recordID` = %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,recordID)
        connection.commit()
    return True
#display all record
def showRecord():
    sql = "SELECT * FROM record"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql)
        connection.commit()
        result = cursor.fetchall()
    print(result)
    return result

def modify_record(data):
    recordId = data['recordID']
    participants = []
    for i in range(int(data['counter'])):
        p = data.get('participant' + str(i))
        if  p != None and p != '':
            participants.append(data['participant' + str(i)])
    print(participants)
    result = updateRecord(recordId,data['title'],participants)
    return result

def borrow(data, borrow_type , booker):
    """
    borrow a classroom
    process data and call insertRecord
    """
    participants = []
    have = 0
    if borrow_type == 'ban':
        borrow_type = 0
    else:
        borrow_type = 1
    sql = "SELECT `CR_ID` FROM `classroom` WHERE `roomname` = %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,data['roomName'])
        connection.commit()
        CR_ID = cursor.fetchone()['CR_ID']

    sql = "SELECT * FROM `record` WHERE `CR_ID` = %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,CR_ID)
        connection.commit()
        record_total = cursor.fetchall()
    d_startTime = datetime.fromisoformat(str(data["startDate"]))
    d_endTime = datetime.fromisoformat(str(data["endDate"]))
    record_delete = []
    for r in record_total:
        startTime = datetime.fromisoformat(str(r["startDate"]))
        endTime = datetime.fromisoformat(str(r["endDate"]))
        after_record = (endTime < d_startTime or (endTime == d_startTime and int(r["endSection"]) < int(data["startSection"])))
        before_record = (startTime > d_endTime or (startTime == d_endTime and int(r["startSection"]) > int(data["endSection"])))
        if not (after_record or before_record):
            record_delete.append(r)
            have = 1
    #already have record
    if borrow_type == 1 and have == 1:
        return False
    if borrow_type == 0:
        for r in record_delete:
            deleteRecord(r['recordID'])
    for i in range(int(data['counter'])):
        p = data.get('participant' + str(i))
        if  p != None and p != '':
            participants.append(data['participant' + str(i)])
    insertRecord(title = data['title'], roomname  = data['roomName'], \
    startDate = data['startDate'], startSection  = data['startSection'], \
    endDate = data['endDate'], endSection  = data['endSection'], participant = participants, \
    bookName = booker,type = borrow_type)

    return True

def filter_classroom(data): #開始日期 startDate,開始節數 startSection,結束日期endDate ,結束節數endSection,大樓(building),可容納人數(capacity)
    """
    get the record that between the time period
    """
    sql = "SELECT * FROM Classroom"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql)
        connection.commit()
        classroom_total = cursor.fetchall()
    classroom_total = pd.DataFrame(classroom_total)
    if data['building'] != None:
        building = re.findall("[A-Z0-9]+",data['building'])
        if(len(building) > 0):
            building = building[0]
        else:
            building = ""
        classroom_total = classroom_total.loc[classroom_total["building"] ==  building ]
    if data['capacity'] != None:
        classroom_total = classroom_total.loc[classroom_total["capacity"] > int(data["capacity"]) ]
    print(classroom_total)
    sql = "SELECT * FROM Record"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql)
        connection.commit()
        record_total = cursor.fetchall()
    record_CR_ID = []
    for r in record_total:
        if r["CR_ID"] in classroom_total["CR_ID"]:
            record_CR_ID.append(r)
    d_startTime = datetime.fromisoformat(str(data["startDate"]))
    d_endTime = datetime.fromisoformat(str(data["endDate"]))
    for r in record_CR_ID:
        startTime = datetime.fromisoformat(str(r["startDate"]))
        endTime = datetime.fromisoformat(str(r["endDate"]))
        after_record = (endTime < d_startTime or (endTime == d_startTime and int(r["endSection"]) < int(data["startSection"])))
        before_record = (startTime > d_endTime or (startTime == d_endTime and int(r["startSection"]) > int(data["endSection"])))
        if not (after_record or before_record):
            classroom_total = classroom_total.drop(classroom_total.loc[classroom_total["CR_ID"] == r["CR_ID"]].index)
    classroom_total = classroom_total.drop("CR_ID",axis = 1)
    classroom_total = classroom_total.T.to_dict().values()
    return classroom_total


# O(n^3)
# https://www.jiuzhang.com/problem/4sum/
class Solution:
    """
    @param numbers: Give an array
    @param target: An integer
    @return: Find all unique quadruplets in the array which gives the sum of zero
    """

    def fourSum(self, numbers, target):
        if not numbers:
            return []

        nums = sorted(numbers)
        results = []

        for i in range(len(nums)):
            if i > 0 and nums[i] == nums[i - 1]:
                continue
            for j in range(i + 1, len(nums)):
                if j > i + 1 and nums[j] == nums[j - 1]:
                    continue

                pairs = self.find_two_sum_pairs(
                    nums,
                    j + 1,
                    len(nums) - 1,
                    target - nums[i] - nums[j],
                )

                for (c, d) in pairs:
                    results.append([nums[i], nums[j], c, d])

        return results

    def find_two_sum_pairs(self, nums, left, right, target):
        pairs = []
        while left < right:
            if nums[left] + nums[right] < target:
                left += 1
            elif nums[left] + nums[right] > target:
                right -= 1
            else:
                if not pairs or (nums[left], nums[right]) != pairs[-1]:
                    pairs.append((nums[left], nums[right]))
                left += 1
                right -= 1
        return pairs

jiuzhang/Two pointer/Two pointer move face to face/TwoSum/4sum.py


# Copyright (C) 2009, Thomas Leonard

import trust_db
from xml.sax.saxutils import XMLGenerator

import webapp2

def load(db):
	return set(x.strip() for x in file(db))

debian = load('debian.db')
debian_maint = load('debian-maintainers.db')

class KeyLookup(webapp2.RequestHandler):
	def get(self, keyID):
		self.response.headers['Content-Type'] = 'application/xml'
		gen = XMLGenerator(self.response.out, 'utf-8')
		gen.startDocument()
		gen.startElement('key-lookup', {})

		def report(vote, text):
			gen.startElement('item', {'vote': vote})
			gen.characters(text)
			gen.endElement('item')

		hint = trust_db.hints.get(keyID, None)
		if hint:
			report('good', hint)

		if keyID in debian:
			report('good', 'This key belongs to a Debian Developer.')

		if keyID in debian_maint:
			report('good', 'This key belongs to a Debian Maintainer.')

		gen.endElement('key-lookup')
		gen.endDocument()

app = webapp2.WSGIApplication([
	('/key/([0-9A-F]+)', KeyLookup),
	],
	debug=True)


#!/root/miniconda3/bin/python
import pickle
import re
fun_dict = {}
fun = open("fun.txt","r")
for i in fun:
        if i.startswith(" "):
                i = i.strip().split('] ')
                key = str(i[0][1])
                value = i[1]
                fun_dict[key] = value
#print(fun_dict)
fun.close()
pickle_file =open("fun.dict","wb")
pickle.dump(fun_dict,pickle_file)
pickle_file.close()


from datetime import date
today = date.today()
from CRABClient.UserUtilities import config
config = config()

config.General.requestName = 'Chic_%s_2018A'%today
config.General.transferOutputs = True
config.General.transferLogs = True

config.JobType.pluginName = 'Analysis'
config.JobType.psetName = 'run-chic-miniaod_2018ABC.py'
config.JobType.outputFiles = ['rootuple.root']
config.JobType.sendExternalFolder = True
config.JobType.allowUndistributedCMSSW = True

config.Data.inputDataset = '/Charmonium/Run2018A-17Sep2018-v1/MINIAOD'
config.Data.inputDBS = 'global'
config.Data.splitting = 'FileBased'
config.Data.unitsPerJob = 5
config.Data.lumiMask = 'Cert_314472-325175_13TeV_Legacy2018_Collisions18_JSON_MuonPhys.txt'
config.Data.outLFNDirBase = '/store/user/xuyan/'
config.Data.publication = False
config.Data.outputDatasetTag  = 'Chic_%s_2018A'%today
config.Site.storageSite = 'T3_US_FNALLPC'


#! /usr/bin/env python3

# pylint: disable = no-self-use, unused-argument

import unittest
import unittest.mock

from tests import util

import yakserver.interface
import yakserver.usbdevice

class TestUSBInterface(util.TestCase):
    def test_initialize_connects_to_usb_device(self):
        mock_usbdevice = unittest.mock.Mock()
        interface = yakserver.interface.USBInterface(mock_usbdevice,
                                                     translator=None)

        interface.initialize()

        mock_usbdevice.connect.assert_called_once()

    def test_receives_event(self):
        stub_usbdevice = unittest.mock.Mock()
        stub_usbdevice.read.side_effect = lambda n, **kwargs: b'abc'[:n]
        stub_translator = unittest.mock.Mock()
        stub_translator.raw_data_to_event.side_effect = lambda x: x + b'_event'
        stub_translator.maximum_data_length.return_value = 1
        interface = yakserver.interface.USBInterface(stub_usbdevice,
                                                     stub_translator)
        interface.translator = stub_translator

        event = interface.get_event()

        self.assertEqual(event, b'a_event')

    def test_send_command(self):
        mock_usbdevice = unittest.mock.Mock()
        stub_translator = unittest.mock.Mock()
        stub_translator.event_to_raw_data.side_effect = lambda x: x[:1]
        interface = yakserver.interface.USBInterface(mock_usbdevice,
                                                     stub_translator)
        interface.translator = stub_translator

        interface.send_command(b'a_event')

        mock_usbdevice.write.assert_called_once_with(b'a')

class TestInterfaceManager(util.TestCase):
    DEFAULT_DEVICE_CLASS_ID = yakserver.usbdevice.DeviceClassID(
        vendor_id=0x04d8,
        product_id=0x5900,
        release_number=0x0000)

    class StubRawDevice:
        def __init__(self, device_class_id=yakserver.usbdevice.DeviceClassID(
                vendor_id=0x04d8,
                product_id=0x5900,
                release_number=0x0000)):
            self._device_class_id = device_class_id

        @property
        def class_identifier(self):
            return self._device_class_id

    def test_find_input_interfaces(self):
        # pylint: disable = protected-access
        interface_manager = yakserver.interface.InterfaceManager()
        connected_devices = [self.StubRawDevice(), self.StubRawDevice()]

        usb_find_patch = unittest.mock.patch('yakserver.usbdevice.find',
                                             return_value=connected_devices)
        with usb_find_patch as mock_usb_find:
            interfaces = interface_manager.input_interfaces()

            mock_usb_find.assert_called_once_with(vendor_id=0x04d8,
                                                  product_id=0x5900)

        devices = [interface._usb_device for interface in interfaces]
        self.assertCountEqual(devices, connected_devices)

    def test_find_output_interfaces(self):
        # pylint: disable = protected-access
        interface_manager = yakserver.interface.InterfaceManager()
        connected_devices = [self.StubRawDevice(), self.StubRawDevice()]

        usb_find_patch = unittest.mock.patch('yakserver.usbdevice.find',
                                             return_value=connected_devices)
        with usb_find_patch as mock_usb_find:
            interfaces = interface_manager.output_interfaces()

            mock_usb_find.assert_called_once_with(vendor_id=0x04d8,
                                                  product_id=0x5901)

        devices = [interface._usb_device for interface in interfaces]
        self.assertCountEqual(devices, connected_devices)


import numpy as np
from keras.callbacks import Callback

from libs.utils.utils import decode_batch, accuracies

class VizCallback(Callback):
    """
    The Custom Callback created for printing the Accuracy and Letter Accuracy Metrics at the End of Each Epoch
    """

    def __init__(self, test_func, text_img_gen, is_train, acc_compute_batches):
        self.test_func = test_func
        self.text_img_gen = text_img_gen
        self.is_train = is_train                # Used to indicate whether the callback is called to for Train or Validation Data
        self.acc_batches = acc_compute_batches  # Number of Batches for which the metrics are computed typically equal to steps/epoch

    def show_accuracy_metrics(self, num_batches):
        """
        Calculates the accuracy and letter accuracy for each batch of inputs,
        and prints the avarage accuracy and letter accuracy across all the batches
        """
        accuracy = 0
        letter_accuracy = 0
        batches_cnt = num_batches
        while batches_cnt > 0:
            word_batch = next(self.text_img_gen)[0]   # Gets the next batch from the Data generator
            decoded_res = decode_batch(self.test_func, word_batch['img_input'])
            actual_res = word_batch['source_str']
            acc, let_acc = accuracies(actual_res, decoded_res)
            accuracy += acc
            letter_accuracy += let_acc
            batches_cnt -= 1
        accuracy = accuracy / num_batches
        letter_accuracy = letter_accuracy / num_batches
        if self.is_train:
            print("\nTrain Average Accuracy: \t", np.round(accuracy, 2), " %")
            print("Train Average Letter Accuracy: \t", np.round(letter_accuracy, 2), " %")
        else:
            print("Validation Average Accuracy: \t\t", np.round(accuracy, 2), " %")
            print("Validation Average Letter Accuracy: \t", np.round(letter_accuracy, 2), " %")

    def on_epoch_end(self, epoch, logs=None):
        self.show_accuracy_metrics(self.acc_batches)


import sys
sys.stdin = open("4789.txt")

for t in range(int(input())):
    P = list(map(int, input()))
    length = len(P)
    ans = 0
    clap = 0
    for i in range(length):
        if i - 1 >= 0 and P[i] and P[i - 1] == 0:
            if clap < i:
                ans += i - clap
                clap += i - clap
        if P[i]:
            clap += P[i]
        i += 1

    print(f"#{t + 1} {ans}")


# coding: utf-8

# # Take a look at channel 1
#
# * [Modis channel listing](https://modis.gsfc.nasa.gov/about/specifications.php)
#
# * Band 1 centered at 6.45 microns (red)
#
# * data acquired at 250 m resolution, resampled to 1 km for the MYD021KM file

# In[1]:

from a301utils.a301_readfile import download
import numpy as np
import h5py
import sys
import a301lib
from a301lib.geolocate import fast_hist, fast_avg, fast_count,make_plot
from a301lib.radiation import planckInvert
from matplotlib import pyplot as plt

filename = 'MYD021KM.A2016136.2015.006.2016138123353.h5'
download(filename)
h5_file=h5py.File(filename)

# In[2]:

filename='MYD03.A2016136.2015.006.2016138121537.h5'
download(filename)
geo_file = h5py.File(filename)
lon_data=geo_file['MODIS_Swath_Type_GEO']['Geolocation Fields']['Longitude'][...]
lat_data=geo_file['MODIS_Swath_Type_GEO']['Geolocation Fields']['Latitude'][...]
from a301lib.geolocate import find_corners
corners=find_corners(lat_data,lon_data)

# Here is the corresponding red,green,blue color composite for the granule.

# In[3]:

from IPython.display import Image
Image(url='http://clouds.eos.ubc.ca/~phil/courses/atsc301/downloads/aqua_136_2015.jpg',width=600)

# ### **Read the radiance data from MODIS_SWATH_Type_L1B/Data Fields/EV_1KM_RefSB**
#
# #### Both radiance and reflectance calibratiions are included

# In[4]:

get_ipython().magic('matplotlib inline')
index=0
my_name = 'EV_1KM_RefSB'
chan1=h5_file['MODIS_SWATH_Type_L1B']['Data Fields'][my_name][index,:,:]
scale=h5_file['MODIS_SWATH_Type_L1B']['Data Fields'][my_name].attrs['reflectance_scales'][...]
offset=h5_file['MODIS_SWATH_Type_L1B']['Data Fields'][my_name].attrs['reflectance_offsets'][...]
chan1_calibrated =(chan1 - offset[index])*scale[index]
plt.hist(chan1_calibrated.ravel())
ax=plt.gca()  #get current axis
_=ax.set(title='channel 1 reflectance for Ft. McMurray')

# Now call the planckInvert function imported at the top of the notebook to convert radiance to brightness temperature

# In[5]:

fig,ax = plt.subplots(1,1,figsize = (10,14))
CS=ax.imshow(chan1_calibrated)
cax=fig.colorbar(CS)
ax.set_title('Channel 1 reflectance, Ft. McMurray')
out=cax.ax.set_ylabel('Chan 1 reflectance$)')
out.set_rotation(270)
out.set_verticalalignment('bottom')

# In[6]:

lon_min= -144
lon_max = -92

lat_min = 47
lat_max = 70
binsize = 0.1

lon_hist = fast_hist(lon_data.ravel(),lon_min,lon_max,binsize=binsize)
lat_hist =  fast_hist(lat_data.ravel(),lat_min,lat_max,binsize=binsize)
gridded_image = fast_avg(lat_hist,lon_hist,chan1_calibrated.ravel())

# In[7]:

lat_centers=(lat_hist['edges_vec'][1:] + lat_hist['edges_vec'][:-1])/2.
lon_centers=(lon_hist['edges_vec'][1:] + lon_hist['edges_vec'][:-1])/2.
lon_array,lat_array=np.meshgrid(lon_centers,lat_centers)
print(lon_array.shape)
masked_reflects = np.ma.masked_invalid(gridded_image)

# In[8]:

from matplotlib import cm
from matplotlib.colors import Normalize

cmap=cm.YlGnBu_r  #see http://wiki.scipy.org/Cookbook/Matplotlib/Show_colormaps
cmap.set_over('w')
cmap.set_under('b',alpha=0.1)
cmap.set_bad('0.75') #75% grey
#
# set the range over which the pallette extends so I use
# use all my colors on data 10 and 40 degrees centigrade
#
vmin= 0.0
vmax= 0.4
the_norm=Normalize(vmin=vmin,vmax=vmax,clip=False)
fig,ax = plt.subplots(1,1,figsize=(14,18))
corners['ax'] = ax
corners['resolution']='l'
corners['projection']='lcc'
corners['urcrnrlon'] = -70.
corners['urcrnrlat'] = 65.
corners['llcrnrlat'] = 46.
corners['llcrnrlon'] = -140.
proj = make_plot(corners)
lat_centers=(lat_hist['edges_vec'][1:] + lat_hist['edges_vec'][:-1])/2.
lon_centers=(lon_hist['edges_vec'][1:] + lon_hist['edges_vec'][:-1])/2.
lon_array,lat_array=np.meshgrid(lon_centers,lat_centers)
#
# translate every lat,lon pair in the scene to x,y plotting coordinates
# for th Lambert projection
#
x,y=proj(lon_array,lat_array)
CS=proj.pcolormesh(x, y,masked_reflects, cmap=cmap, norm=the_norm)
CBar=proj.colorbar(CS, 'right', size='5%', pad='5%',extend='both')
CBar.set_label('Channel 1 reflectance',
               rotation=270,verticalalignment='bottom',size=18)
_=ax.set_title('Modis Channel 1, May 15, 2016 -- Fort McMurray',size=22)

# ### back to channel 31

# In[9]:

index31=10

my_name = 'EV_1KM_Emissive'
chan31=h5_file['MODIS_SWATH_Type_L1B']['Data Fields'][my_name][index31,:,:]
scale=h5_file['MODIS_SWATH_Type_L1B']['Data Fields']['EV_1KM_Emissive'].attrs['radiance_scales'][...]
offset=h5_file['MODIS_SWATH_Type_L1B']['Data Fields']['EV_1KM_Emissive'].attrs['radiance_offsets'][...]
chan31_calibrated =(chan31 - offset[index31])*scale[index31]

# In[10]:

wavel=11.e-6  #chan 31 central wavelength, meters
chan31_mks = chan31_calibrated*1.e6  #W/m^2/m/sr
Tbright = planckInvert(wavel,chan31_mks)
Tbright = Tbright - 273.15 #convert to Centigrade

# In[11]:

plt.hist(Tbright.ravel())
ax=plt.gca()  #get current axis
_=ax.set(title='channel 31 brightness temperatures for Ft. McMurray')

# ### now do a heatmap by binning temperatures and reflectivities in a 2D histogram
#
# #### put reflectivities on the rows and temperatures on the columns so hist is 30 x 40

# In[12]:

temp_min= -30
temp_max = 40
num_temp_bins=40

refl_min = 0
refl_max = 0.7
num_refl_bins=30

temp_hist = fast_hist(Tbright.ravel(),temp_min,temp_max,numbins=num_temp_bins)
refl_hist =  fast_hist(chan1_calibrated.ravel(),refl_min,refl_max,numbins=num_refl_bins)
heatmap = fast_count(refl_hist,temp_hist)

# In[13]:

_=plt.imshow(heatmap)

# In[14]:

plt.pcolormesh(heatmap)
_=plt.colorbar()

# In[15]:

with np.errstate(divide='ignore'): #use a context manager (with)
    log_counts=np.log10(heatmap)  #turn off divide by zero warning
masked_counts = np.ma.masked_invalid(log_counts)
plt.pcolormesh(masked_counts)
_=plt.colorbar()

# In[ ]:


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 25 16:32:58 2018

@author: lagerwer
"""

import numpy as np
import AFFDK as aff
# %% Set variables

# Set path to you dataset
path = '/export/scratch2/lagerwer/data/pomegranate1_02MAR/processed_data/'
dataset = {'g': path + 'g_good_ODL.npy',
           'ground_truth' : path + 'ground_truth.npy',
           'mask': path + 'mask.npy'}
#dataset = {'g' : 'real_data_example.npy'}

# The amount of projection angles in the measurements
angles = 2000
ang_freq = 4 # parameter to reduce amount of angles for this dataset
# Set source and detector to center of rotation radius and detector size
# values are from the data_prepare.py file
src_rad = 59
det_rad = 10.9
pix_size = 0.00748
#pix_size = 0.0449 # sc 6
# Variables above are expressed in cm

# Create a data object
data_obj = aff.real_data(dataset, pix_size, src_rad, det_rad, angles, ang_freq)

# Expansion operator and binnin parameter
expansion_op = 'linear'
bin_param = 2

# %% Create the circular cone beam CT class
case = aff.CCB_CT(data_obj, angles=None, src_rad=None, det_rad=None, noise=None)
# Angles, source and detector radius are already passed through the object
# No options for additional noise

# Initialize the algorithms (FDK, SIRT, AFFDK)
case.init_algo()
# Init AFFDK
case.init_AFFDK(bin_param, expansion_op)

# %% Do some reconstructions
load_path = '/export/scratch2/lagerwer/1/ang_freq4_'
case.AtA = np.load(load_path + 'AtA.npy')
case.Atg = np.load(load_path + 'Atg.npy')
case.CtC = np.load(load_path + 'CtC.npy')
case.DDC_norm = np.load(load_path + 'DDC_norm.npy')
case.gradFDK_norm = np.load(load_path + 'gradFDK_norm.npy')

# %%
case.LSFDK.do()
case.TFDK.do(6e-7)
case.SFDK.do(2e-6)

# %%
case.table()
latex_table = open(load_path + '_latex_table.txt', 'w')
latex_table.write(case.table_latex)
latex_table.close()


# Copyright 2019 Canonical Ltd.
#
# 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.

import ast
from setuptools import setup, find_packages

def _read_me() -> str:
    with open("README.md", "rt", encoding="utf8") as fh:
        readme = fh.read()
    return readme

def _get_version() -> str:
    # ops.__init__ needs to pull in ops.charm to work around a circular
    # import so we can't import it here as that pulls in yaml which isn't
    # necessarily there yet.
    version = 'unknown'
    with open("ops/__init__.py", "rt", encoding="utf8") as fh:
        source = fh.read()
    code = ast.parse(source)
    for node in code.body:
        if isinstance(node, ast.Assign):
            targets = [i.id for i in node.targets]
            if '__version__' in targets:
                if isinstance(node.value, ast.Str):
                    # Python < 3.8
                    version = node.value.s
                else:
                    version = node.value.value
                break
    return version

setup(
    name="ops",
    version=_get_version(),
    description="The Python library behind great charms",
    long_description=_read_me(),
    long_description_content_type="text/markdown",
    license="Apache-2.0",
    url="https://github.com/canonical/operator",
    author="The Charmcraft team at Canonical Ltd.",
    author_email="charmcraft@lists.launchpad.net",
    packages=find_packages(include=('ops', 'ops.*')),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: Apache Software License",
        "Development Status :: 4 - Beta",

        "Intended Audience :: Developers",
        "Intended Audience :: System Administrators",
        "Operating System :: MacOS :: MacOS X",
        "Operating System :: POSIX :: Linux",
        # include Windows once we're running tests there also
        # "Operating System :: Microsoft :: Windows",
    ],
    python_requires='>=3.5',
    install_requires=["PyYAML"],
)


from selenium import webdriver
from selenium.webdriver.common.by import By
import time

#Driver Intialization
driver=webdriver.Chrome(executable_path="D:\SeleniumPython\drivers\chromedriver.exe")
driver.maximize_window()

driver.get("http://demo.automationtesting.in/Frames.html") # Launch Browser

#Single Iframe
driver.switch_to.frame("singleframe")  #Switch to frame using id or name
driver.find_element(By.XPATH,"//div[@class='col-xs-6 col-xs-offset-5']//input").send_keys("Siva")

#Switch focus to browser
driver.switch_to.default_content()
time.sleep(3)
driver.find_element(By.LINK_TEXT,"Iframe with in an Iframe").click()

time.sleep(3)
frames=driver.find_elements_by_tag_name("iframe")
print(len(frames))
counter=0
for f in frames:
    print(f.get_attribute("src"))
    print(counter)
    counter=counter+1

# #Iframe with in an frame
driver.switch_to.frame(1)  #Switch to frame using index
driver.switch_to.frame(0)
driver.find_element(By.XPATH,"//div[@class='col-xs-6 col-xs-offset-5']//input").send_keys("Kumar")


import asyncio
from .taskplanner import TaskPlanner
from os.path import join, isfile, isabs, dirname, abspath
from .common import fix_buffering, get_full_url
import argparse
import pickle
from sys import stderr

__all__ = ['run_scrape', 'request', 'challenge', 'go_parallel', 'get_loop']

parser = argparse.ArgumentParser()
parser.add_argument('--pipe', required=False, help='For inner usage by monitor process')
parser.add_argument('--noresume', action='store_true', help='Don\'t auto resume on start')
parser.add_argument('--verbose', action='store_true', help='Dump all messages intended for monitor')

fix_buffering()

task_planner = None

def get_loop():
    return task_planner.loop

async def go_parallel(corolist):
    if isinstance(corolist, dict):
        tmap, tasks = {}, []
        for k, coro in corolist.items():
            t = asyncio.ensure_future(coro, loop=task_planner.loop)
            tmap[id(t)] = k
            tasks.append(t)
    else:
        tasks = [asyncio.ensure_future(coro, loop=task_planner.loop) for coro in corolist]
    for t in tasks:
        t._log_destroy_pending = False
    done, _ = await asyncio.wait(tasks, loop=task_planner.loop)
    result = {} if isinstance(corolist, dict) else []
    for task in done:
        e = task.exception()
        if e is not None:
            print('go_parallel:', e, type(e), file=stderr)
        else:
            if isinstance(corolist, dict):
                result[tmap[id(task)]] = task.result()
            else:
                result.append(task.result())
    return result

async def _parallel_worker(coroiter, output):
    while True:
        try:
            try:
                key, coro = next(coroiter)
            except StopIteration:
                return
            output[key] = await coro
        except Exception as e:
            print('go_parallel:', e, type(e), file=stderr)

async def go_parallel_limited(corolist, workers=50):
    'Replacement for go_parallel with controllable memory consumption'

    dictmode = True
    if isinstance(corolist, dict):
        keycoroiter = iter(corolist.items())
    else:
        keycoroiter = enumerate(corolist)
        dictmode = False
    output = {}
    tasks = [
        asyncio.ensure_future(_parallel_worker(keycoroiter, output), loop=task_planner.loop)
        for i in range(min(workers, len(corolist)))
    ]
    for t in tasks:
        t._log_destroy_pending = False
    await asyncio.wait(tasks, loop=task_planner.loop)
    if dictmode:
        return output
    else:
        result = []
        for i in range(len(corolist)):
            if i not in output:
                continue
            result.append(output[i])
        return result

async def request(
    *,
    method='get', url, params=None, data=None, headers=None, auth=None, cookies=None,
    key=None, parser=None, parserkw=None,
    force_request_queue=None
):
    if params is not None:
        url = get_full_url(url, params)
    req = {
        'method': method,
        'url': url,
    }
    for k, v in (
        ('data', data),
        ('headers', headers),
        ('auth', auth),
        ('cookies', cookies),
        ('parserkw', parserkw),
        ('key', key),
        ('parser', parser),
    ):
        if v is not None:
            req[k] = v
    return (await task_planner.work_over_full_line(req, force_request_queue=force_request_queue))

async def challenge(html):
    return (await task_planner.challenge(html))

# TODO: prepare not a session, but domain
# i.e. we need authorization only for particular domain

async def control_reader_worker(reader, queue):
    # TODO: duplicate
    while True:
        msglen = await reader.readexactly(4)
        msglen = int.from_bytes(msglen, byteorder='big', signed=False)
        msg = await reader.readexactly(msglen)
        msg = pickle.loads(msg)
        await queue.put(msg)

async def control_writer_worker(writer, queue):
    # TODO: duplicate
    while True:
        msg = await queue.get()
        msg = pickle.dumps(msg)
        msglen = len(msg).to_bytes(length=4, byteorder='big')
        writer.write(msglen)
        writer.write(msg)
        await writer.drain()

async def null_queue_getter(queue, dump):
    while True:
        msg = await queue.get()
        if dump:
            print(msg)
        elif msg.get('type') == 'parserStdout':
            print(msg.get('line', '').rstrip('\n'))
        elif msg.get('type') == 'parserStderr':
            print(msg.get('line', '').rstrip('\n'), file=stderr)

async def scrape_main(main, prepare_session, parser_filename, config, *, proxy_list=None, sockfile='scutigera', loop):
    global task_planner

    args = parser.parse_args()

    rq = asyncio.Queue(loop=loop)
    wq = asyncio.Queue(loop=loop)

    if args.pipe is not None:
        reader, writer = await asyncio.open_unix_connection(args.pipe, loop=loop)
        t = asyncio.ensure_future(control_reader_worker(reader, rq), loop=loop)
        t._log_destroy_pending = False
        t = asyncio.ensure_future(control_writer_worker(writer, wq), loop=loop)
        t._log_destroy_pending = False
    else:
        t = asyncio.ensure_future(null_queue_getter(wq, args.verbose), loop=loop)
        t._log_destroy_pending = False
    del t

    task_planner = TaskPlanner(
        config, parser_filename,
        prepare_session=prepare_session, sockfile=sockfile,
        r_control=rq, w_control=wq, proxy_list=proxy_list,
        loop=loop
    )
    if args.pipe is None and not args.noresume:
        # if not connected to master server go on its own
        asyncio.ensure_future(task_planner.resume(), loop=loop)
    try:
        await main()
    finally:
        await asyncio.sleep(1)  # prevent finishing too fast, e.g. if main is a plain func
    await task_planner.finalize()

def load_proxylist(fname):
    proxy_list = []
    if not isfile(fname):
        return proxy_list
    with open(fname) as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            v = line.split(' ')
            addr = v[0]
            auth = (v[1], v[2]) if len(v) >= 3 else None
            proxy_list.append((addr, auth))
    return proxy_list

def run_scrape(main, prepare_session, parser_modulename, config):
    loop = asyncio.get_event_loop()

    proxy_list = []
    if config.get('AllowProxy') is True:
        proxfname = config.get('ProxyListFile')
        if not isabs(proxfname):
            proxfname = join(dirname(dirname(abspath(__file__))), proxfname)
        print(proxfname)
        proxy_list = load_proxylist(proxfname)
        print(len(proxy_list), 'proxies')

    loop.run_until_complete(scrape_main(
        main, prepare_session, parser_modulename, config, proxy_list=proxy_list, loop=loop
    ))
    loop.close()


import torch.nn as nn
import torch
import torch.nn.functional as F
from parameters import *

class ModelFeat(nn.Module):
    def __init__(self):
        super().__init__()
        # self.encoder = nn.LSTM(input_size = FEATURE_DIM, hidden_size = HIDDEN_SIZE, num_layers = NUM_LAYERS, dropout = LSTM_DROPOUT, batch_first = True)

        self.F = nn.Sequential(
              nn.Linear(FEATURE_DIM, D1_DIM),
              nn.ReLU(),
              nn.Dropout(),
              nn.Linear(D1_DIM, D2_DIM),
              nn.ReLU(),
        )

        self.S = nn.Sequential()

        self.classifier = nn.Sequential(
              nn.Linear(D2_DIM * 2, D2_DIM // 4),
              nn.ReLU(),
              nn.Linear(D2_DIM // 4, 1),
              nn.Sigmoid()
       )

    def forward(self, features, caption):
        f_output = self.F(features)
        s_output = self.S(caption)
        #print(f_output.shape,s_output.shape)
        c_input = torch.cat((f_output, s_output), dim = 1)
        c_output = self.classifier(c_input)
        return c_output


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 16 22:10:39 2020

@author: tomasg
"""

import numpy as np
from matplotlib import pyplot as plt
import pandas as pd
import pickle
from scipy.ndimage import gaussian_filter as gauss_fil
from os import listdir
from skimage import measure
from scipy.ndimage import rotate

def Mover_Imagen(image,x,y):
    Diaba=image
    if (x>=0):
        Da=np.zeros([35,35+x])
        Diaba=np.append(Diaba, Da,axis=1)
        Diaba=np.append(Da[:,:35-x], Diaba,axis=1)
    elif (x<0):
        Da=np.zeros([35,35-x])
        Diaba=np.append(Da, Diaba,axis=1)
        Diaba=np.append(Diaba,Da[:,:35+x] ,axis=1)
    if (y>=0):
        Da=np.zeros([35+y,105])
        Diaba=np.append(Diaba, Da,axis=0)
        Diaba=np.append(Da[:35-y,:], Diaba,axis=0)
    elif (y<0):
        Da=np.zeros([35-y,105])
        Diaba=np.append(Da, Diaba,axis=0)
        Diaba=np.append(Diaba, Da[:35+y,:],axis=0)
    #plt.imshow(Diaba,cmap="jet")

    return Diaba

def Mapa_Corr(Image_org, min_spikes = 20):

    Mapa_c=np.zeros([70,70])

    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_org,i,j)[35:70,35:70]
            Original=np.copy(Image_org)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>min_spikes:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35+i][35+j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35+i][35+j]=np.nan
    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_org,-i,-j)[35:70,35:70]
            Original=np.copy(Image_org)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>min_spikes:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35-i][35-j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35-i][35-j]=np.nan
    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_org,-i,j)[35:70,35:70]
            Original=np.copy(Image_org)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>min_spikes:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35-i][35+j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35-i][35+j]=np.nan
    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_org,i,-j)[35:70,35:70]
            Original=np.copy(Image_org)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>min_spikes:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35+i][35-j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35+i][35-j]=np.nan
    return Mapa_c

def Mapa_Cross_Corr(Image_Movil, Image_Fija):

    Mapa_c=np.zeros([70,70])

    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_Movil,i,j)[35:70,35:70]
            Original=np.copy(Image_Fija)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>20:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35+i][35+j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35+i][35+j]=np.nan
    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_Movil,-i,-j)[35:70,35:70]
            Original=np.copy(Image_Fija)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>20:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35-i][35-j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35-i][35-j]=np.nan
    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_Movil,-i,j)[35:70,35:70]
            Original=np.copy(Image_Fija)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>20:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35-i][35+j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35-i][35+j]=np.nan
    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_Movil,i,-j)[35:70,35:70]
            Original=np.copy(Image_Fija)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>20:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35+i][35-j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35+i][35-j]=np.nan
    return Mapa_c

def Cargar(name):
    """Cargar archivos"""
    with open (name, "rb") as hand:
        dic=pickle.load(hand)
    return dic

def Propiedades_mapa_return_thres(map_correlacion):
    """Funcion para determinar las propiedas de las regiones del mapa de grilla. Retorna las
    propiedades del mapa y el mapa umbrelizado"""
    map_correlacion[map_correlacion>0.2]=1
    map_correlacion[map_correlacion<=0.2]=0
    label=measure.label(map_correlacion)
    propiedades=measure.regionprops(label)
    return propiedades,map_correlacion

def Radio_inter_externo_distancia_segundo(propiedades,Corre):
    """Calculo de radio interno, externo y la distancia de los centros.
    radio interno: pixel más cercano al centro de la imagen (considerando los 6 cluster más cercanos)
    radio externo: centro más alejado del centro de la imagen (considerando los 6 cluster más cercanos)
    """
    center_x=Corre.shape[0]/2
    center_y=Corre.shape[1]/2
    distancia_x=[reg.centroid[0] for reg in propiedades]
    distancia_y=[reg.centroid[1] for reg in propiedades]
    distancias=Calculo_distancias(distancia_x, distancia_y,center_x,center_y)
    radio_interno=Calculo_radio_interno(propiedades,distancias, center_x, center_y)
    distancias_copy=distancias.copy()
    distancias_copy.sort()
    distancias_copy=distancias_copy[1:7]
    radio_externo=Selecion_distancia_maxima(distancias_copy)
    return distancias,radio_interno,radio_externo

def Recort_inferior(corr, radio_interno, center_x, center_y):
    """Elimina todos los valores que se encuentran dentro de una circunferencia
    delimitada por el radio interno (centrada en la imagen)"""
    matriz_verificacion=np.zeros_like(corr)
    matriz_verificacion[matriz_verificacion==0]=False
    for i in range(matriz_verificacion.shape[0]):
        for j in range(matriz_verificacion.shape[1]):
            if np.sqrt(np.square(i-center_x) + np.square(j-center_y) )<=radio_interno:
                matriz_verificacion[i][j]=True
    co=np.copy(corr)
    co[matriz_verificacion==1]=np.nan
    return co

def Recort_superior(corr, radio_externo, center_x, center_y):
    """Elimina todos los valores que se encuentran dentro de una circunferencia
    delimitada por el radio interno (centrada en la imagen)"""
    matriz_verificacion=np.zeros_like(corr)
    matriz_verificacion[matriz_verificacion==0]=False
    for i in range(matriz_verificacion.shape[0]):
        for j in range(matriz_verificacion.shape[1]):
            if np.sqrt(np.square(i-center_x) + np.square(j-center_y) )>=radio_externo:
                matriz_verificacion[i][j]=True
    co=np.copy(corr)
    co[matriz_verificacion==1]=np.nan
    return co

def Recortar_mapa(Corr, radio_interno, radio_externo, center_x, center_y):
    """Funcion que recorto la imagen dejando los valores entre el radio interno
    y el externo"""
    corr_to_recort= np.copy(Corr)
    corr_to_recort=Recort_inferior(corr_to_recort,radio_interno, center_x, center_y)
    corr_to_recort=Recort_superior(corr_to_recort,radio_externo, center_x, center_y)
    return corr_to_recort

def Eliminar_nan(mapa1, mapa2):
    """Elimina ordenadamete los nan de ambos mapas"""
    vect1=mapa1.flatten()
    vect2=mapa2.flatten()
    vect1[np.isnan(vect1)]=900
    vect2[np.isnan(vect2)]=900
    auxiliar_1=np.take(vect1,np.where(vect2!=900))
    auxiliar_2=np.take(vect2,np.where(vect1!=900))
    vect1=np.take(auxiliar_1,np.where(auxiliar_1[0]!=900))
    vect2=np.take(auxiliar_2,np.where(auxiliar_2[0]!=900))
    return vect1,vect2

def Eliminar_zeros(mapa1, mapa2):
    """Elimina ordenadamete los 0 de ambos mapas"""
    vect1=mapa1.flatten()
    vect2=mapa2.flatten()
    vect1[vect1==0]=900
    vect2[vect2==0]=900
    auxiliar_1=np.take(vect1,np.where(vect2!=900))
    auxiliar_2=np.take(vect2,np.where(vect1!=900))
    vect1=np.take(auxiliar_1,np.where(auxiliar_1[0]!=900))
    vect2=np.take(auxiliar_2,np.where(auxiliar_2[0]!=900))
    return vect1,vect2

def Propiedades_mapa_return_thres(map_correlacion):
    """Funcion para determinar las propiedas de las regiones del mapa de grilla. Retorna las
    propiedades del mapa y el mapa umbrelizado"""
    map_correlacion[map_correlacion>0.2]=1
    map_correlacion[map_correlacion<=0.2]=0
    label=measure.label(map_correlacion)
    propiedades=measure.regionprops(label)
    return propiedades,map_correlacion

def Eliminar_zeros(array1, array2):
    array2 = np.nan_to_num(array2).flatten()
    array1 = np.nan_to_num(array1).flatten()
    array2_tmp = np.take(array2, np.where(array1 != 0))
    array1_tmp = np.take(array1, np.where(array2 != 0))
    array2 = np.take(array2_tmp, np.where(array2_tmp[0] != 0))
    array1 = np.take(array1_tmp, np.where(array1_tmp[0] != 0))
    return array1.T, array2.T

def Calculo_dist_radial(lista, center_x, center_y):
    """Devuelve una lista con las distancias de cada pixel al centro"""
    radio_interno=lista.coords-(center_x, center_y)
    radio_interno=np.square(radio_interno)
    radio_interno=np.sum(radio_interno,axis=1)
    radio_interno=np.sqrt(radio_interno)
    return radio_interno

def Calculo_radio_interno(propiedades, distancias, center_x, center_y):
    """Devuelve el radio interno (para eliminar el centroide del centro)"""

    distancias_copy=distancias.copy()
    distancias_copy.sort()
    distancias_copy=distancias_copy[1:7]
    dist_regions=[]
    for i in distancias_copy:
        region=distancias.index(i)
        dist_regions.append(min(Calculo_dist_radial(propiedades[region], center_x, center_y) ) )
    radio_externo=min(dist_regions)
    return radio_externo

def Selecion_distancia_maxima(distancia):
    """Selecciona el radio externo de tal forma de asegurarse
    de que el radio seleccionado no sea mayor a 30"""
    if int(distancia[len(distancia)-1])<30:
        return distancia[len(distancia)-1]
    else:
        return Selecion_distancia_maxima(distancia[:len(distancia)-1])

def Calculo_radio_externo(propiedades, distancias, center_x, center_y):
    """Devuelve el radio externo (para segmentar unicamente los 6 patrones
    centrale"""
    distancias_copy=distancias.copy()
    distancias_copy.sort()
    distancias_copy=distancias_copy[1:7]
    dist_regions=[]
    for i in distancias_copy:
        region=distancias.index(i)
        dist_regions.append(max(Calculo_dist_radial(propiedades[region], center_x,
                                                    center_y) ) )
    radio_externo=max(dist_regions)
    return radio_externo

def Calculo_distancias(distancia_x, distancia_y,center_x,center_y):
    """Determino la distancia de los distintos centroides al centro del
    mapa de correlacion"""
    fun_distancia=lambda x,y: np.sqrt(np.square(x-center_x)+np.square(y-center_y))
    distancias=list(map(fun_distancia,distancia_x,distancia_y))
    return distancias

def Calculo_grid_not_origin_extern_radios_variables(corre, date="none", clu1="none", clu2="none"):
    """ prueba calculando la corre con la imagen thresholdiada y variando el radio externo
    para determinar el maximo grid score"""
    center_x = corre.shape[0]/2
    center_y = corre.shape[1]/2
    mapa_corre = np.copy(corre)
    propiedades, mapa_corre=Propiedades_mapa_return_thres(mapa_corre)
    dist,radio_interno,radio_externo=Radio_inter_externo_distancia_segundo(propiedades,corre)
    score=[]
    if len(propiedades)>7:
        for radio_externo in range(int(radio_externo),int(center_y),1):
            coef=[]
            for angle in range(30,180,30):
                # rotado2=Rotar(corre,angle,center_x,center_y)
                mapa_corre1=np.nan_to_num(mapa_corre.copy())
                mapa_rotado1=rotate(mapa_corre1,angle,reshape=False)
                mapa_rotado1=np.round(mapa_rotado1)
                mapa_rotado1=Recortar_mapa(mapa_rotado1, radio_interno, radio_externo, center_x, center_y)
                mapa_corre1=Recortar_mapa(mapa_corre1, radio_interno, radio_externo, center_x, center_y)
                mapa_corre1, mapa_rotado1=Eliminar_nan(mapa_corre1, mapa_rotado1)
                # mapa_corre1, mapa_rotado1=Eliminar_zeros(mapa_corre1,mapa_rotado1)
                coef.append(np.corrcoef( mapa_corre1,mapa_rotado1)[0][1])
            grid_score=min([ coef[i] for i in [1, 3] ] )-max([coef[i] for i in [0, 2, 4] ] )
            score.append(grid_score)
        try:
            return max(score)
        except:
            print("No se puedo ejecutar la siguiente combinacion date={}_clu1={}_clu2={}.".format(date, clu1, clu2))
    else:
        return np.nan

if __name__ == "__main__":
    print("Se ejecuta modo manual")
    a=np.random.rand(35,35)
    plt.imshow(Mapa_Corr(a))
    plt.show()


import pandas as pd
import pymysql
import requests as rq
import datetime

db=pymysql.connect("localhost","ssafy","ssafy","forchannel",charset="utf8")
cursor = db.cursor()
CSV_PATH = 'C:/Users/multicampus/Desktop/crawling/jobkorea.csv'
csv_test = pd.read_csv(CSV_PATH)
sub=pd.DataFrame(csv_test, columns=['title','contents','sdate','edate'])
# sub=csv_test.data[['전시회명','전시장소','전시시작기간','전시끝기간','전시설명']]
data = [tuple(list(x)+[29,datetime.datetime.now(),datetime.datetime.now()]) for x in sub.values]
print(data)
query="""insert into schedules(title,contents,sdate,edate,ch_no,created_date,modified_date) values (%s,%s,%s,%s,%s,%s,%s)"""
cursor.executemany(query, data)
db.commit()


from copy import deepcopy
import pandas as pd
import numpy as np

# Problem 1
########## DO NOT modify ###########
class Node:
    def __init__(self, data):
        self._data = data
        self._next = None

    def get_data(self):
        return self._data

    def set_data(self, new_data):
        self._data = new_data

    def get_next(self):
        return self._next

    def set_next(self, new_next):
        self._next = new_next
#####################################

class LinkedList:
    def __init__(self):
        # DO NOT modify __init__() class; No additional class variables are allowed
        self._head = None

    def __str__(self):
        cur = self._head
        str = []
        while cur != None:
            str.append(cur.get_data())
            cur = cur.get_next()
        return '{}'.format(str)

    def add(self, item):
        new_node = Node(data=item)
        new_node.set_next(self._head)
        self._head = new_node

    def erase(self, func):
        cur = self._head
        before = None
        while cur != None:
            if func(cur):
                if cur == self._head:
                    self._head = cur.get_next()
                    del cur
                    cur = self._head
                else:
                    before.set_next(cur.get_next())
                    del cur
                    cur = before.get_next()
            else:
                before = cur
                cur = cur.get_next()

    def search(self, func):
        cur = self._head
        index = 0
        li = []
        while cur != None:
            if func(cur):
                li.append(index)
            cur = cur.get_next()
            index += 1
        return li

    def sort(self, func):
        cur = self._head
        count = 0
        while cur != None:
            count += 1
            cur = cur.get_next()

        if count <= 1:
            return

        for i in range(count-1):  # if count == 7, (6) 0...5
            cur = self._head
            before = None
            for _ in range(count-i-1):  # 6 5 4 3 2 1
                if func(cur) > func(cur.get_next()):  # sorting
                    if cur == self._head:
                        self._head = cur.get_next()
                        cur.set_next(self._head.get_next())
                        self._head.set_next(cur)
                        before = self._head
                    else:
                        before.set_next(cur.get_next())
                        cur.set_next(cur.get_next().get_next())
                        before = before.get_next()
                        before.set_next(cur)
                else:
                    before = cur
                    cur = cur.get_next()

#test data
l = LinkedList()

l.add(9)
l.add(3)
l.add(8)
l.add(6)
print(l)

l.erase(lambda x: x.get_data() > 6)
print(l)

l.add(7)
l.add(3)
l.add(9)
l.add(4)
l.add(2)
print(l.search(lambda x: x.get_data() < 4))

def sorting_criterion(x):
    return x.get_data() % 5

l.sort(sorting_criterion)
print(l)

# Problem 2

def sequential_search(search_list, num):
    ret = []
    for i, item in enumerate(search_list):
        if item == num:
            ret.append(i)
    return ret

def binary_search(search_list, num, start=0, end=-1):
    if end == -1:
        end = len(search_list)-1
    if start > end:
        return []
    ret = []
    mid = (start + end) // 2
    if num == search_list[mid]:
        ret.append(mid)
        for i in range(mid-1, -1, -1):
            if search_list[i] == num:
                ret.append(i)
                continue
            else:
                break
        for i in range(mid+1, end+1):
            if search_list[i] == num:
                ret.append(i)
                continue
            else:
                break
    elif num > search_list[mid]:
        ret = binary_search(search_list, num, start=mid+1, end=end)
    else:
        ret = binary_search(search_list, num, start=start, end=mid-1)
    return ret

# test data
search_list = [1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6]

print(sequential_search(search_list, 5))
print(binary_search(search_list, 3))

def insertion_sort(sort_list, func, sfunc=None, start=0, gap=1):
    for i in range(start+gap, len(sort_list), gap):
        key = sort_list[i]
        j = i
        while j > 0:
            if func(sort_list[j-1]) > func(key):
                sort_list[j] = sort_list[j-1]
                j -= 1
            elif func(sort_list[j-1]) == func(key):
                if sfunc == None:
                    break
                elif sfunc(sort_list[j-1]) < sfunc(key):
                    sort_list[j] = sort_list[j-1]
                    j -= 1
                else:
                    break
            else:
                break
        sort_list[j] = key

def shell_sort(sort_list, func1, func2):
    gap = len(sort_list) // 2
    while gap > 0:
        for start in range(gap):
            insertion_sort(sort_list, func1, func2, start, gap)
        gap = gap // 2

sort_list = [1, 2, 3, 4, 5, 6]
sort_list2 = [1, 2, 3, 4, 5, 6]

insertion_sort(sort_list, lambda x: x ** 2 - 10 * x)
print(sort_list)
shell_sort(sort_list2, lambda x: x % 2, lambda x: x % 3)
print(sort_list2)

# Problem 3
########## DO NOT modify ###########
class Sorting:
    def __init__(self):
        pass

    def random_list(self, length):
        import random
        arr = []
        for _ in range(length):
            arr.append(random.randint(1, 1000))
        return arr

    def sorting_func(self, x):
        return eval('+'.join(str(x)))
#####################################
    def bunch(self, sort_list, func):  # 같은 item을 nest 시켜주는 함수
        bunch = [sort_list[0]]  # 같은 nest에 첫 번째 값을 넣음.
        new = []  # bunch를 묶어서 새롭게 return 될 list
        j = 0  # bunch가 초기화 될 때 len=0 이 되기 때문에 sort_list의 index와 비교하기 위한 gap 상수
        for i in range(len(sort_list)-1):  # sort_list 끝까지 탐색하는데, i+1까지 가야하므로 길이-1 만큼 loop
            # bunch의 item과 sort_list item의 func output 값이 같으면
            if func(bunch[i-j]) == func(sort_list[i+1]):
                bunch.append(sort_list[i+1])  # 같은 nest로 묶는다
            else:
                new.append(bunch)  # 아니면 현재까지의 bunch를 new list에 추가한다.
                bunch = [sort_list[i+1]]  # 그 다음 값으로 bunch를 초기화한다.
                j = i + 1  # 현재 인덱스를 j에 기억한다.
        new.append(bunch)  # loop가 끝나고 난 뒤 마지막 bunch를 new에 추가한다.
        sort_list = new  # sort_list를 nested된 list인 new로 갱신한다.
        return sort_list  # nested list return

    def bubble_sort(self, sort_list, func):
        if len(sort_list) <= 1:  # list의 길이가 1 미만이면 정렬 안함.
            return sort_list
        for size in reversed(range(len(sort_list))): # 버블정렬은 뒤에서부터 완료된다. 마지막 index가 size이다.
            for i in range(size): # size 만큼 loop를 돌면서 값을 비교한다.
                if func(sort_list[i]) < func(sort_list[i+1]): # func output 비교해서 큰 값을 앞으로
                    # swap
                    sort_list[i], sort_list[i + 1] = sort_list[i+1], sort_list[i]
        sort_list = self.bunch(sort_list, func) # nested list로 만들어준다.
        return sort_list

    def selection_sort(self, sort_list, func):
        if len(sort_list) <= 1: # list의 길이가 1 미만이면 정렬 안함.
            return sort_list
        for size in reversed(range(len(sort_list))): # 한 칸씩 (뒤에서부터 최솟값 찾아서 채움) 정렬이 완성되므로 size를 점점 줄여나간다.
            min_i = 0 # 최솟값 인덱스
            for i in range(1, 1+size): # loop
                if func(sort_list[i]) < func(sort_list[min_i]): # min보다 작은 값이 발견되면
                    min_i = i # min을 최솟값으로 갱신
                # swap
                sort_list[min_i], sort_list[size] = sort_list[size], sort_list[min_i]
        sort_list = self.bunch(sort_list, func) # nested list로 만들어준다.
        return sort_list

    def merge_sort(self, sort_list, func1, func2=None, depth=-1):
        if len(sort_list) <= 1: # list의 길이가 1 미만이면 정렬 안함.
            return sort_list
        depth += 1 # 현재 재귀 깊이
        if len(sort_list) > 1:
            mid = len(sort_list) // 2 # 반으로 분할하기 위한 index
            ll, rr = sort_list[:mid], sort_list[mid:] # list를 mid를 중심으로 ll(왼쪽), rr(오른쪽)로 분할한다.
            self.merge_sort(ll, func1, func2) # ll에 대해 merge sort
            self.merge_sort(rr, func1, func2) # rr에 대해 merge sort

            li, ri, i = 0, 0, 0 # ll index, rr index, sort_list index
            while li < len(ll) and ri < len(rr): # ll과 rr이 각각 마지막 index에 도달하기까지 loop
                if func1(ll[li]) > func1(rr[ri]): # ll list item이 rr list item보다 func output이 크면
                    sort_list[i] = ll[li] # sort_list에 ll의 item을 넣는다.
                    li += 1 # ll index 증가
                elif func1(ll[li]) == func1(rr[ri]):
                    if func2 != None:
                        if func2(ll[li]) > func2(rr[ri]):
                            sort_list[i] = ll[li]
                            li += 1
                        else:
                            sort_list[i] = rr[ri]
                            ri += 1
                else: # 반대면
                    sort_list[i] = rr[ri]  # sort_list에 rr의 item을 넣는다.
                    ri += 1 # rr index 증가
                i += 1 # sort_list index 증가
            sort_list[i:] = ll[li:] if li != len(ll) else rr[ri:] # ll과 rr 중 하나가 마지막 idnex에 도달했으면 그렇지 않은 list로 전부 sort_list를 채움

        # 재귀(정렬)이 끝난 root node에서 마지막 return 전에 호출
        if depth == 0:
            sort_list = self.bunch(sort_list, func1) # nested list로 만들어준다.
        return sort_list

    def quick_sort(self, sort_list, func1, func2=None, depth=-1):
        if len(sort_list) <= 1: # list의 길이가 1 미만이면 정렬 안함.
            return sort_list
        depth += 1 # 현재 재귀 깊이
        pivot = sort_list[len(sort_list) // 2] # 중앙 index를 pivot으로 잡는다.
        less_li, equal_li, great_li = [], [], [] # pivot기준으로 작은, 같은, 큰 list
        for item in sort_list: # sort_list 순회
            if func1(item) < func1(pivot): # item이 pivot보다 작으면 less_li에 append
                less_li.append(item)
            elif func1(item) > func1(pivot): # item이 pivot보다 크면 great_li에 append
                great_li.append(item)
            else:
                if func2 != None:
                    if func2(item) < func1(pivot):
                        less_li.append(item)
                    elif func2(item) > func2(pivot):
                        great_li.append(item)
                equal_li.append(item) # item이 pivot과 같으면 equal_li에 append

        # great + equal + less (내림차순)으로 quick sort 재귀 호출
        sort_list = self.quick_sort(
            great_li, func1, func2, depth) + equal_li + self.quick_sort(less_li, func1, func2, depth)
        # 재귀 함수(하위 노드)에서 return 받은 정렬된 sort_list를 현재 노드의 sort_list에 저장한다.

        # 재귀(정렬)이 끝난 root node에서 마지막 return 전에 호출
        if depth == 0:
            sort_list = self.bunch(sort_list, func1)
        return sort_list

    def compute_sorting_time(self):
        pass

    def sort_medals(self):
        data = pd.read_csv('./medals.csv')

        li = data.values.tolist()

        self.selection_sort(li, lambda x: x[1]) # Gold
        self.merge_sort(li, lambda x: x[1], lambda x: x[2]) #Sliver
        self.quick_sort(li, lambda x: x[1], lambda x: x[3]) #Bronze
        #print(li) # Gold 1, Sliver 2, Bronze 3

        dataframe = pd.DataFrame(li)
        dataframe.to_csv('sorted.csv')
        print(dataframe)

s = Sorting()
'''
l1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
l2, l3, l4 = deepcopy(l1), deepcopy(l1), deepcopy(l1)

print(s.bubble_sort(l1, lambda x: x % 2))

print(s.selection_sort(l2, lambda x: x % 3))

print(s.merge_sort(l3, lambda x: x % 4))

print(s.quick_sort(l4, lambda x: x % 5))
'''
s.sort_medals()


from __future__ import unicode_literals
from django.shortcuts import render, render_to_response, redirect
from django.http import HttpResponse
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.core.paginator import EmptyPage
from django.core.paginator import PageNotAnInteger
from django.db.models import Q
import json, re, datetime, time
import monitorDataShow.config as config

# Create your views here.

def paginatorProcess(info, page):
    paginator = Paginator(info, 100)
    try:
        business_page = paginator.page(page)
    except PageNotAnInteger:
        business_page = paginator.page(1)
    except EmptyPage:
        business_page = paginator.page(paginator.num_pages)

    return business_page

class TimeFormat(object):
    def __init__(self):
        self.timeStr = datetime.datetime.now()

    def lastDay(self, days):
        return datetime.datetime.strftime(self.timeStr + datetime.timedelta(days=-days), '%Y-%m-%d')

    def ProcessFortTime(self, res):
        curr_month = datetime.datetime.strftime(self.timeStr, '%Y%m')
        if not res['start_time'] and not res['stop_time']:
            start_time = datetime.datetime.strftime(self.timeStr - datetime.timedelta(hours=3), '%Y-%m-%d %H:%M:00')
            stop_time = datetime.datetime.strftime(self.timeStr, '%Y-%m-%d %H:%M:00')
        else:
            start_time = datetime.datetime.strftime(datetime.datetime.strptime(res['start_time'], '%Y-%m-%dT%H:%M'),
                                                    '%Y-%m-%d %H:%M:00')
            stop_time = datetime.datetime.strftime(datetime.datetime.strptime(res['stop_time'], '%Y-%m-%dT%H:%M'),
                                                   '%Y-%m-%d %H:%M:00')
        return start_time, stop_time, curr_month

@login_required
def NginxAccessIP(request):
    page = request.GET.get('page')
    timeStr = TimeFormat().lastDay(1)
    selectSql = "select create_time, ip, count, region, city " \
                "from django_ipcount " \
                "where " \
                    "create_time=\'%s 00:00:00\' " \
                    "and ip!=\'200\' " \
                "ORDER BY -count;" % timeStr
    res = config.MysqlOp(project=config.nginxDBConfig).select(selectSql)
    businesses = paginatorProcess(res, page)
    return render(request, 'nginx_access_ip.html', {'business_info': businesses})

@login_required
def NginxAccessAPI(request):
    page = request.GET.get('page')
    timeStr = TimeFormat().lastDay(1)
    selectSql = "select create_time, dn, api, count " \
                "from django_apicount " \
                "where " \
                    "create_time=\'%s 00:00:00\' " \
                    "and api not IN ('/', '/admin/')" \
                "ORDER BY -count;" % timeStr
    res = config.MysqlOp(project=config.nginxDBConfig).select(selectSql)
    businesses = paginatorProcess(res, page)
    return render(request, 'nginx_access_api.html', {'business_info': businesses})

def getDataFromMysql(dn, res):
    start_time, stop_time, curr_month = TimeFormat().ProcessFortTime(res)
    selectSQL = "SELECT re.create_time, re.api, re.avg " \
                "FROM requesttime_%s as re INNER JOIN " \
                "(SELECT api, max(avg) as avg " \
                "FROM requesttime_%s " \
                "WHERE domain_name = \'%s\' " \
                "AND create_time >= \'%s\' " \
                "AND create_time <= \'%s\' " \
                "GROUP BY api " \
                "order by max(avg) desc limit 10) as a  on a.api = re.api " \
                "where domain_name = \'%s\' " \
                "AND create_time >= \'%s\' " \
                "AND create_time <= \'%s\' " \
                "ORDER BY re.create_time;" % (curr_month, curr_month, dn, start_time, stop_time, dn, start_time, stop_time)
    return config.MysqlOp(project=config.nginxDBConfig).select(selectSQL)

def processData(dataRes):
    SourceDataTimeList = list()
    SourceDataApiList = list()
    ResTimeList = list()
    ResApiList = list()
    ResRequestTimeList = list()

    for SourceTimeStr, SourceApiStr, _ in dataRes:
        if SourceTimeStr not in SourceDataTimeList:
            SourceDataTimeList.append(SourceTimeStr)
        if SourceApiStr not in SourceDataApiList:
            SourceDataApiList.append(SourceApiStr)

    DefaultList = [[x, y, 0] for x in SourceDataTimeList for y in SourceDataApiList]

    for data in dataRes:
        for result in DefaultList:
            index = DefaultList.index(result)
            if list(data[0:2]) == result[0:2]:
                DefaultList[index][-1] = data[-1]
                break

    for ResTimeStr, ResApiStr, ResrequestStr in DefaultList:
        if ResTimeStr.strftime('%Y-%m-%d %H:%M') not in ResTimeList:
            ResTimeList.append(ResTimeStr.strftime('%Y-%m-%d %H:%M'))
        if ResApiStr not in ResApiList:
            ResApiList.append(ResApiStr)
            ResRequestTimeList.append([ResrequestStr])
        else:
            ResRequestTimeList[ResApiList.index(ResApiStr)].append(ResrequestStr)

    return ResTimeList, ResApiList, ResRequestTimeList

@login_required
def NginxAccessRequestTime(request):
    if request.method == 'POST':
        res = json.loads(request.body)
        if res['name'] == 'dinghuoapi':
            dataRes = getDataFromMysql('dinghuo.api.huimin100.cn', res)
            dateList, apiList, requestList = processData(dataRes)
            data = json.dumps({"data": [dateList, apiList, requestList]})
            return HttpResponse(data, content_type='application/json')
        elif res['name'] == 'loginapi':
            dataRes = getDataFromMysql('login.api.huimin100.cn', res)
            dateList, apiList, requestList = processData(dataRes)
            data = json.dumps({"data": [dateList, apiList, requestList]})
            return HttpResponse(data, content_type='application/json')
        elif res['name'] == 'productapi':
            dataRes = getDataFromMysql('product.api.huimin100.cn', res)
            dateList, apiList, requestList = processData(dataRes)
            data = json.dumps({"data": [dateList, apiList, requestList]})
            return HttpResponse(data, content_type='application/json')
        elif res['name'] == 'pcshop':
            dataRes = getDataFromMysql('pcshop.huimin100.cn', res)
            dateList, apiList, requestList = processData(dataRes)
            data = json.dumps({"data": [dateList, apiList, requestList]})
            return HttpResponse(data, content_type='application/json')
        elif res['name'] == 'client':
            dataRes = getDataFromMysql('client.huimin100.cn', res)
            dateList, apiList, requestList = processData(dataRes)
            data = json.dumps({"data": [dateList, apiList, requestList]})
            return HttpResponse(data, content_type='application/json')
    return render(request, 'nginx_access_request.html')


#calss header
class _ARABLE():
	def __init__(self,):
		self.name = "ARABLE"
		self.definitions = [u'Arable farming land is used for, or is suitable for, growing crops: ']

		self.parents = []
		self.childen = []
		self.properties = []
		self.jsondata = {}

		self.specie = 'adjectives'

	def run(self, obj1, obj2):
		self.jsondata[obj2] = {}
		self.jsondata[obj2]['properties'] = self.name.lower()
		return self.jsondata


import configparser
import logging
import os

logging.basicConfig(level=logging.DEBUG)

class Config:
    def __init__(self):
        self.config = configparser.ConfigParser()
        ini_path = os.path.dirname(os.path.realpath(__file__))
        self.config.read(os.path.join(ini_path, "default.ini"))

    # simple pattern from https://wiki.python.org/moin/ConfigParserExamples
    def map(self, section):
        dict1 = {}
        options = self.config.options(section)
        for option in options:
            try:
                dict1[option] = self.config.get(section, option)
                if dict1[option] == -1:
                    logging.debug("skip: %s" % option)
            except:
                print("exception on %s!" % option)
                dict1[option] = None
        return dict1


""" Testing reading DICOM files

"""

import numpy as np

from .. import dicomreaders as didr

from .test_dicomwrappers import (EXPECTED_AFFINE,
                                 EXPECTED_PARAMS,
                                 DATA)

from nose.tools import assert_true, assert_false, \
     assert_equal, assert_raises

from numpy.testing import assert_array_equal, assert_array_almost_equal

from dipy.testing import parametric, IO_DATA_PATH

@parametric
def test_read_dwi():
    img = didr.mosaic_to_nii(DATA)
    arr = img.get_data()
    yield assert_equal(arr.shape, (128,128,48))
    yield assert_array_almost_equal(img.get_affine(), EXPECTED_AFFINE)

@parametric
def test_read_dwis():
    data, aff, bs, gs = didr.read_mosaic_dwi_dir(IO_DATA_PATH, '*.dcm.gz')
    yield assert_equal(data.ndim, 4)
    yield assert_array_almost_equal(aff, EXPECTED_AFFINE)
    yield assert_array_almost_equal(bs, (0, EXPECTED_PARAMS[0]))
    yield assert_array_almost_equal(gs,
                                    (np.zeros((3,)) + np.nan,
                                     EXPECTED_PARAMS[1]))
    yield assert_raises(IOError, didr.read_mosaic_dwi_dir, 'improbable')


#生成游戏背景窗体
from tkinter import *
import time
import random
#生成小球类
class Ball:
    def __init__(self,canvas,color):
        self.canvas=canvas
        ad = [(30,30,55,55),(50,50,100,100),(45,45,75,75)]
        random.shuffle(ad)
        self.id=canvas.create_oval(ad[0],fill=color)#椭圆型球,数字控制形状（10，10是位置25，25控制球大小
        sitea = [245,100,300,252]
        random.shuffle(sitea)
        self.canvas.move(self.id,sitea[0],sitea[1])#小球移动一个点
        #让小球随机运动
        starts=[-3,-2,-1,1,2,3]#让小球从中随机挑选一个速度
        random.shuffle(starts)#shuffle： 打乱顺序功能
        self.x = starts[0]
        self.y = starts[1]
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()
    def draw(self):
        self.canvas.move(self.id,self.x,self.y)
        pos = self.canvas.coords(self.id)#获取位置
        if pos[1]<=0:self.y = 1
        if pos[3]>=self.canvas_height:self.y = -1#如果小球到达最顶端或者最下端往回走
        if pos[0]<=0:self.x = 1
        if pos[2]>=self.canvas_width:self.x = -1
tk = Tk()
tk.title("BallGame")
tk.resizable(1,1)#是否可以随意改变窗体大小 0，0时候不可以
tk.wm_attributes("-topmost",1)
canvas = Canvas(tk,width = 800,height = 600,bd = 0, highlightthickness = 0)
canvas.pack()
tk.update()
#小球实例化
ball1 = Ball(canvas,'red')
ball2 = Ball(canvas,'yellow')
ball3 = Ball(canvas,'blue')
while True:
    ball1.draw()
    ball2.draw()
    ball3.draw()
    tk.update_idletasks()#刷新
    tk.update()
    time.sleep(0.01)


"""
1.数据本地化
将数据以文件的形式，存储到本地磁盘中。
（程序中变量保存的数据都是存到内存中的，当程序运行结束，内存中的数据全部销毁）

常见的数据本地化方式：二进制文件（包含音频、视频、压缩包等），文本文件，json和xml文件，数据库文件。

2.文件操作（读和写）
文件操作的固定步骤:打开文件（新建文件） - 文件操作（读和写） - 关闭文件

3.打开文件
open(file,mode = "r",.....,encoding = None) - 返回的是被打开的文件对象（文件句柄）
说明：
file - 字符串：需要打开的文件的路径（可以是绝对路径，也可以是相对路径）
            绝对路径：文件的全路径（一般不使用绝对路径）
            相对路径：（相对当前的py文件对应的目录）xxx.txt或者./aaa.txt

mode - 打开方式；打开文件后不同操作，对应的打开方式不一样
        ”r“ - 默认值 ，以读的方式打开文件，读出来的是字符串
        “w” - 以写的方式打开文件，写的是写字符串
        “rb/br” - 以读的方式打开，读出来的是二进制
        “wb/bw” - 以写的方式打开文件，写的是需要些二进制进去
        “a” - 以写的方式打开，追加
        “+” - 以读写的方式打开
encoding - 文本文件的编码方式 ， 一般赋值为"UTF-8"
                               utf - 8 -支持中文编码
                               gbk - 不支持中文编码
"""
#以读的形式打开一个文本文件，保存到变量f中，对f进行操作，就是对被打开的文件操作
f =open("./files/蓝莲花.txt","r",encoding="utf-8")

"""
4.文件的读操作
文件对象.read() - 从文件读写位置开始读到文件结尾(默认就是获取文件中所有内容)
"""
# content= f.read() #读所有内容
# print(content)

# content =f.readline() #读一行内容
# print(content)

#练习：将文件中的内容读完，要求一行一行的读。
while True:
    content = f.readline()  # 读一行内容
    print(content)
    if content=="":
        break

"""
5.文件的写操作
文件对象.write（字符串） - 将字符串中的内容写入到文件中（会完全覆盖源文件中的内容）
"""
f =open("./files/蓝莲花.txt","w",encoding="utf-8")
f.write("你好吗？")
f =open("./files/蓝莲花.txt","a",encoding="utf-8")
f.write("aaaaaa")
f.write("\neeee")

"""
6.关闭文件
文件对象.close() - 关闭指定的文件
"""


import sublime
import sublime_plugin
import os
import sys
import tempfile
import shutil

# add PIL module
if sublime.platform() == "osx":
	package_file = os.path.normpath(os.path.abspath(__file__))
	package_path = os.path.dirname(package_file)
	lib_path =  os.path.join(package_path, "lib")
	if lib_path not in sys.path:
		sys.path.append(lib_path)
	from PIL import ImageGrab
	from PIL import ImageFile
	from PIL import Image

try:
	from MarkdownEditing.mdeutils import *
except ImportError:
	from mdeutils import *

class InsertImageFromClipboardCommand(MDETextCommand):
	"""docstring for InsertImagerFromClipboardCommand"""
	def load_settings(self):
		settings = sublime.load_settings('Markdown.sublime-settings')
		self.SAVE_METHOD = settings.get("mde.image.save.method")
		print(self.SAVE_METHOD)
		self.SAVE_BASEDIR = settings.get("mde.image.save.basedir")
		print(self.SAVE_BASEDIR)
		# current file dirname
		self.curdir = os.path.dirname(self.view.file_name())

	def run(self, edit):
		print("开始插入图片")
		self.load_settings()

		filepath = InsertImageFromClipboardCommand.save_image()
		if filepath:
			basename,url = self.getLocalUrl(filepath)
			self.view.run_command("insert_snippet", {"contents": "!["+basename+"](" + url + ")"})
		else:
			sublime.status_message("insert_snippet is error")

	@staticmethod
	def save_image():
		# 保存剪切板上的图片内容到临时文件
		m = ImageGrab.grabclipboard()
		if m :
			fd, tempFileName = tempfile.mkstemp(".png")
			try:
				m.save(tempFileName)
			except IOError as e:
				sublime.status_message("save image from clipboard is error")
				raise e
			return tempFileName

	def getLocalUrl(self,filePath):
		# 移动文件路径到制定目录，并且返回插入链接名和url
		if not filePath or not os.path.exists(filePath):
			return
		# filePath, ext
		dirname,ext=os.path.splitext(filePath)
		# 文件名，不含后缀
		basename=os.path.basename(dirname)

		basedir = os.path.expanduser(self.SAVE_BASEDIR) if self.SAVE_METHOD == "absoulte" else os.path.join(self.curdir,self.SAVE_BASEDIR)

		basedir = os.path.abspath(basedir)
		# 目标文件夹
		if not os.path.exists(basedir):
			os.makedirs(basedir)
		shutil.move(filePath,basedir)
		# 绝对路径运行
		realpath = basedir if self.SAVE_METHOD=="absoulte" else os.path.relpath(basedir,self.curdir)
		url = os.path.join(os.path.normpath(realpath),basename+""+ext)
		return basename,url


# program to take a long integer N from the user and print all the twin prime numbers between one and the given number
# twin primes are those pairs of prime numbers whose difference is 2

import math

def isPrime(num):
    i = 2
    while i >= 2 and i <= int(math.sqrt(num)):
        if num%i == 0:
            return False
        i += 1
    return True

if __name__ == "__main__":
    print("to find all twin prime number between one and a given upper bound number")
    upper_bound = int(input("enter the upperbound: "))
    print("the twin primes in the given range are:")
    for i in range(2,upper_bound+1):
        if isPrime(i) and isPrime(i+2):
            print('(%d,%d)'%(i,i+2))


from .instruccionAbstracta import InstruccionAbstracta
from Errores import errorReportar
from tabla_Simbolos import tablaSimbolos,simboloBaseDatos
import jsonMode

class createDatabase(InstruccionAbstracta):
    '''
        Esta clase representa la instrucción crear database.
        La instrucción crear database tiene un ID y una posible lista de owner y mode
    '''
    def __init__(self,reemplazar,si_no_existe,identificador, opciones=[]):
        self.reemplazar = reemplazar
        self.si_no_existe = si_no_existe
        self.identificador = identificador
        self.opciones = opciones

    def ejecutar(self,tabalSimbolos,listaErrores):

        bandera = tabalSimbolos.comprobarNombreBaseDatos(self.identificador)

        if bandera == 1 and self.reemplazar == None:
            if self.si_no_existe == None:
                errorEnviar = errorReportar.ErrorReportar(self.fila,self.columna,"Ejecucion","Error 42P04")
                listaErrores.append(errorEnviar)
                return
            else:
                #si es un id repetido pero dice crear si no existe
                return
        elif bandera == 1 and self.reemplazar != None:
            #si es un id repetido pero hay que eliminarlo y reemplazarlo
            #eliminarlo
            respuesta = jsonMode.dropDatabase(self.identificador)
            if respuesta == 0:
                tabalSimbolos.eliminarBaseDatos(self.identificador)
            elif respuesta == 1:
                errorEnviar = errorReportar.ErrorReportar(self.fila,self.columna,"Ejecucion","Error en la operacion")
                listaErrores.append(errorEnviar)
                return
            elif respuesta == 2:
                errorEnviar = errorReportar.ErrorReportar(self.fila,self.columna,"Ejecucion","Error 3D000, no existe la base de datos")
                listaErrores.append(errorEnviar)
                return
            print("si es un id repetido pero hay que eliminarlo y reemplazarlo " + str(respuesta))
            #reemplazarlo
            respuesta2 = jsonMode.createDatabase(self.identificador)
            if respuesta2 == 0:
                dbGuardar = simboloBaseDatos.SimboloBaseDatos(self.identificador)
                for i in range(len(self.opciones)):
                    try:
                        if self.opciones[i].nombreNodo == "OWNER":
                            print(self.opciones[i+2].valor)
                            dbGuardar.setearPropietario(self.opciones[i+2].valor)
                        elif self.opciones[i].nombreNodo == "modo":
                            print(self.opciones[i].hijos[3])
                            dbGuardar.setearModo(self.opciones[i].hijos[3])
                    except:
                        pass
                tabalSimbolos.guardarBaseDatos(dbGuardar)
            else:
                errorEnviar = errorReportar.ErrorReportar(self.fila,self.columna,"Ejecucion","Error 42P04")
                listaErrores.append(errorEnviar)
                return
            print("reemplazo "+ str(respuesta))
        else:
            #Llamar funcion de ingeniero
            respuesta = jsonMode.createDatabase(self.identificador)
            if respuesta == 0:
                dbGuardar = simboloBaseDatos.SimboloBaseDatos(self.identificador)
                for i in range(len(self.opciones)):
                    try:
                        if self.opciones[i].nombreNodo == "OWNER":
                            print(self.opciones[i+2].valor)
                            dbGuardar.setearPropietario(self.opciones[i+2].valor)
                        elif self.opciones[i].nombreNodo == "modo":
                            print(self.opciones[i].hijos[3])
                            dbGuardar.setearModo(self.opciones[i].hijos[3])
                    except:
                        pass
                tabalSimbolos.guardarBaseDatos(dbGuardar)
            else:
                errorEnviar = errorReportar.ErrorReportar(self.fila,self.columna,"Ejecucion","Error 42P04")
                listaErrores.append(errorEnviar)
                return
        pass


from imgurpython import ImgurClient
from imgurpython.helpers import GalleryAlbum
import os
import urllib.parse

os.environ['IMGUR_CLIENT_ID'] = '2fe13e5fb7324d1'
os.environ['IMGUR_CLIENT_SECRET'] = '5497654450389d556ef0de11c4f1b1a022ca11c3'

class ImgurWrapper():

    imgurObject = ImgurClient(
        client_id=os.environ['IMGUR_CLIENT_ID'], client_secret=os.environ['IMGUR_CLIENT_SECRET'])

    albumId = "aceituna"

    def getImage(self, imageId):
        return self.imgurObject.get_image(image_id=imageId)

    def search(self, query):
        response = self.imgurObject.gallery_search(
            q=query,
            sort="time",
        )

        linksList = []

        for item in response:
            if isinstance(item, GalleryAlbum):
                for item2 in item.images:
                    linksList.append(item2["link"])
            else:
                linksList.append(item.link)

        return linksList


from django.conf import settings # import the settings file
def context(request):
    # return the value you want as a dictionnary. you may add multiple values in there.
    from .signals import render_started
    from django.apps import apps
    asenzor=apps.get_app_config("asenzor")
    render_started.send(sender=request)
    """
    BASE_URL=settings.BASE_URL
    BASE_URL_CLEAN=settings.BASE_URL_CLEAN

    if request.META['REMOTE_ADDR']!="127.0.0.1" and settings.DEBUG:
    	BASE_URL=settings.PROTOCOL+settings.PRIVATE_HOST#request.META['REMOTE_ADDR']+"/"
    if request.META['REMOTE_ADDR']!="127.0.0.1" and settings.DEBUG:
    	BASE_URL_CLEAN=settings.PRIVATE_HOST
    """
    warnings=""
    for elem in asenzor.warnings:
        if request.user.has_perm(elem[0]):
            warnings+=elem[1]
    print("iiiiiiiiiii")
    print(warnings)
    data={'BASE_URL': request.scheme+"://"+request.get_host()+settings.BASE_URL,
    'ASENZOR_URL': request.scheme+"://"+request.get_host()+settings.ASENZOR_URL,
    'MEDIA_URL': request.scheme+"://"+request.get_host()+settings.MEDIA_URL,
    "WARNINGS":warnings,
    }

    data.update(settings.MENUS)
    return data