"""
Michael duPont - michael@mdupont.com
plate.py - Display ICAO METAR weather data with a Raspberry Pi and Adafruit LCD plate

Use plate keypad to select ICAO station/airport iden to display METAR data
  Left/Right - Choose position
  Up/Down    - Choose character A-9
  Select     - Confirm station iden
LCD panel now displays current METAR data (pulled from aviationweather.gov)
  Line1      - IDEN HHMMZ BA.RO   or   IDEN HHMMZ NOSIG
  Line2      - Rest of METAR report
LCD backlight indicates current Flight Rules
  Green      - VFR
  Blue       - MVFR
  Red        - IFR
  Violet     - LIFR
At the end of a line scroll:
  Holding select button displays iden selection screen
  Holding left and right buttons gives option to shutdown the Pi

Uses Adafruit RGB Negative 16x2 LCD - https://www.adafruit.com/product/1110
Software library for LCD plate - https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code
"""

# stdlib
import os
import sys
from copy import copy
from time import sleep
# library
import avwx
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
# module
import config as cfg
from common import logger, ident_to_station

numRows = 2
numCols = 16
lcd = Adafruit_CharLCDPlate()
lcdColors = [lcd.GREEN,lcd.BLUE,lcd.RED,lcd.VIOLET,lcd.ON]
replacements = [['00000KT','CALM'],['00000MPS','CALM'],['10SM','UNLM'],['9999','UNLM']]   #String replacement for Line2 (scrolling data)

#Program setup
#Returns None
def setup():
	lcd.begin(numCols, numRows)
	lcd.clear()
	return None

#Select METAR station
#Use LCD to update 'ident' values
#Returns None
def selectMETAR():
	cursorPos = 0
	selected = False

	lcd.clear()
	lcd.setCursor(0,0)
	lcd.message('4-Digit METAR')
	#Display default iden
	for row in range(4):
		lcd.setCursor(row,1)
		lcd.message(charList[ident[row]])
	lcd.setCursor(0,1)
	lcd.cursor()
	sleep(1)   #Allow finger to be lifted from select button
	#Selection loop
	while not selected:
		#Shutdown option
		if lcd.buttonPressed(lcd.LEFT) and lcd.buttonPressed(lcd.RIGHT):
			lcdShutdown()
			lcd.clear() #If no, reset screen
			lcd.setCursor(0,0)
			lcd.message('4-Digit METAR')
			for row in range(4):
				lcd.setCursor(row,1)
				lcd.message(charList[ident[row]])
			lcd.setCursor(0,1)
			lcd.cursor()
			sleep(1)
		#Previous char
		elif lcd.buttonPressed(lcd.UP):
			curNum = ident[cursorPos]
			if curNum == 0: curNum = len(charList)
			ident[cursorPos] = curNum-1
			lcd.message(charList[ident[cursorPos]])
		#Next char
		elif lcd.buttonPressed(lcd.DOWN):
			newNum = ident[cursorPos]+1
			if newNum == len(charList): newNum = 0
			ident[cursorPos] = newNum
			lcd.message(charList[ident[cursorPos]])
		#Move cursor right
		elif lcd.buttonPressed(lcd.RIGHT):
			if cursorPos < 3:
				cursorPos += 1
		#Move cursor left
		elif lcd.buttonPressed(lcd.LEFT):
			if cursorPos > 0:
				cursorPos -= 1
		#Confirm iden
		elif lcd.buttonPressed(lcd.SELECT):
			selected = True
		lcd.setCursor(cursorPos,1)
		sleep(buttonInterval)
	lcd.noCursor()
	return None

#Display METAR selection screen on LCD
#Returns None
def lcdSelect():
	lcd.backlight(lcdColors[4])
	selectMETAR()
	lcd.clear()
	lcd.message(ident_to_station(ident)+' selected\nFetching METAR')

#Display timeout message and sleep
#Returns None
def lcdTimeout():
	logger.warning('Connection Timeout')
	lcd.backlight(lcdColors[4])
	lcd.clear()
	lcd.setCursor(0,0)
	lcd.message('No connection\nCheck back soon')
	sleep(timeoutInterval)

#Display invalid station message and sleep
#Returns None
def lcdBadStation():
	lcd.clear()
	lcd.setCursor(0,0)
	lcd.message('No Weather Data\nFor '+ident_to_station(ident))
	sleep(3)
	lcdSelect()

#Display shutdown option
#Shuts down Pi or returns None
def lcdShutdown():
	selection = False
	selected = False
	lcd.backlight(lcdColors[4])
	lcd.clear()
	lcd.setCursor(0,0)
	if shutdownOnExit: lcd.message('Shutdown the Pi?\nY N')
	else: lcd.message('Quit the program?\nY N')
	lcd.setCursor(2,1)
	lcd.cursor()
	sleep(1)   #Allow finger to be lifted from LR buttons
	#Selection loop
	while not selected:
		#Move cursor right
		if lcd.buttonPressed(lcd.RIGHT) and selection:
			lcd.setCursor(2,1)
			selection = False
		#Move cursor left
		elif lcd.buttonPressed(lcd.LEFT) and not selection:
			lcd.setCursor(0,1)
			selection = True
		#Confirm selection
		elif lcd.buttonPressed(lcd.SELECT):
			selected = True
		sleep(buttonInterval)
	lcd.noCursor()
	if not selection: return None
	lcd.clear()
	lcd.backlight(lcd.OFF)
	if shutdownOnExit: os.system('shutdown -h now')
	sys.exit()

#Returns tuple of display data from METAR txt (Line1,Line2,BLInt)
#Line1: IDEN HHMMZ BB.bb
#Line2: Rest of METAR report
#BLInt: Flight rules int
def createDisplayData(txt):
	#Create dictionary of data
	parsedWX = parseMETAR(txt)
	#Create Lines
	if displayNOSIG:
		if parsedWX['Remarks'].find('NOSIG') != -1:
			line1 = parsedWX['Station']+' '+parsedWX['Time'][2:]+' NOSIG'
			txt = txt.replace(' NOSIG' , '')
		else: line1 = parsedWX['Station']+' '+parsedWX['Time'][2:]
	else:
		alt = parsedWX['Altimeter']
		if alt == '': alt = '----'
		line1 = parsedWX['Station']+' '+parsedWX['Time'][2:]+' '+alt[:2]+'.'+alt[2:]
		txt = txt.replace(' A'+alt , '') #Remove Alt
		txt = txt.replace(' Q'+alt , '') #Remove Alt
	line2 = txt.split(' ',2)[2] #Remove ID and Time
	if (not includeRemarks) and line2.find('RMK') != -1: line2 = line2[:line2.find('RMK')] #Opt remove Remarks
	for rep in replacements: line2 = string.replace(line2 , rep[0] , rep[1]) #Any other string replacements
	return line1 , line2.strip(' ') , getFlightRules(parsedWX['Visibility'],getCeiling(parsedWX['Cloud-List']))

#Display METAR data on LCD plate
#Returns approx time elapsed (float)
def displayMETAR(line1 , line2 , lcdLight):
	lcd.clear()
	#Set LCD color to match current flight rules
	lcd.backlight(lcdColors[lcdLight])
	#Write row 1
	lcd.setCursor(0,0)
	lcd.message(line1)
	#Scroll row 2
	timeElapsed = 0.0
	if line2 <= numCols:  #No need to scroll line
		lcd.setCursor(0,1)
		lcd.message(line2 , lcd.TRUNCATE)
	else:
		lcd.setCursor(0,1)
		lcd.message(line2[:numCols] , lcd.TRUNCATE)
		sleep(2) #Pause to read line start
		timeElapsed += 2
		for i in range(1 , len(line2)-(numCols-1)):
			lcd.setCursor(0,1)
			lcd.message(line2[i:i+numCols] , lcd.TRUNCATE)
			sleep(scrollInterval)
			timeElapsed += scrollInterval
	sleep(2) #Pause to read line / line end
	return timeElapsed + 2

#Program Main
#Returns 1 if error, else 0
def main():
	lastMETAR = ''
	userSelected = True
	setup()     #Initial Setup
	lcdSelect() #Show Ident Selection
	while True:
		METARtxt = getMETAR(ident_to_station(ident)) #Fetch current METAR
		while type(METARtxt) == int: #Fetch data until success
			if METARtxt == 0: lcdTimeout()       #Bad Connection
			elif METARtxt == 1:
				if userSelected: lcdBadStation() #Invalid Station
				else:
					logger.info('Ignoring non-user generated selection')
					METARtxt = copy(lastMETAR)   #Server data lookup error
					break
			else: return 1                       #Code error
			METARtxt = getMETAR(ident_to_station(ident))
		userSelected = False
		lastMETAR = copy(METARtxt)
		logger.info(METARtxt)
		L1,L2,FR = createDisplayData(METARtxt)            #Create display data
		logger.info('\t' + L1 + '\n\t' + L2) #Log METAR data
		totalTime = 0.0
		while totalTime < updateInterval:        #Loop until program fetches new data
			totalTime += displayMETAR(L1,L2,FR)  #Cycle display one loop. Add elapsed time to total time
			if lcd.buttonPressed(lcd.SELECT):    #If select button pressed at end of a cycle
				lcdSelect()  #Show Ident Selection
				userSelected = True
				break        #Break to fetch new METAR
			elif lcd.buttonPressed(lcd.LEFT) and lcd.buttonPressed(lcd.RIGHT):  #If right and left
				lcdShutdown() #Shutdown option

	return 0

if __name__ == '__main__': main()


## Converting PyTorch model to TorchScript model for C++ Inference
import sys
import os.path as osp
sys.path.append(osp.join('/'.join(osp.dirname(osp.abspath(__file__)).split('/')[:-1]), 'convnet'))

import argparse
import torch
import models

class Options:
    def __init__(self):
        p = osp.dirname(osp.abspath(__file__)).split('/')[:-1]
        self.proj_path = '/'.join(p)
        self.data_path = osp.join('/'.join(p[:-1]), 'dataset/')
        self.sample_path = osp.join(self.data_path, 'train-split.txt')
        self.img_height =  480
        self.img_width = 640
        self.n_class = 3
        self.output_scale = 8
        self.arch = ''

if __name__ == "__main__":
    model_choices = ['resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152',
        'rfnet50', 'rfnet101', 'rfnet152', 'pspnet50', 'pspnet101', 'pspnet18', 'pspnet34']

    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        '-a', '--arch', metavar='arch', default='', choices=model_choices,
        help='model architecture: ' + ' | '.join(model_choices) + ' (default: resnet18)'
    )
    parser.add_argument(
        '--checkpoint', required=True, help='model path',
    )
    parser.add_argument(
        '--output-path', default="", help='model path',
    )
    args = parser.parse_args()
    options = Options()

    rslt_path = osp.join(options.proj_path, "result") if args.output_path == "" else args.output_path

    print("loading model...")
    checkpoint = torch.load(args.checkpoint, map_location=torch.device('cpu'))
    options.arch = args.arch if args.arch != '' else checkpoint['arch']
    model = models.build_model(options.arch)
    model.load_state_dict(checkpoint['model_state_dict'])
    model.eval()

    # torch.save({
    #     'epoch': checkpoint['epoch'],
    #     'iteration': checkpoint['iteration'],
    #     'arch': args.arch,
    #     'optim_state_dict': checkpoint['optim_state_dict'],
    #     'model_state_dict': checkpoint['model_state_dict'],
    #     'best_mean_iu': checkpoint['best_mean_iu'],
    #     'best_prec': checkpoint['best_prec'],
    # }, args.checkpoint)

    # random input
    print("tracing model...")
    inp = torch.rand(1, 3, options.img_height, options.img_width)

    # generate a torch.jit.ScriptModule via tracing.
    traced_script_module = torch.jit.trace(model, (inp, inp))
    traced_script_module.save(osp.join(rslt_path, options.arch + "_converted.pt"))
    print("tracing done, model saved...")


from django import forms
from .models import Prescription, DoctorProfile
from patient.models import PatientProfile

class PrescriptionCreationForm(forms.ModelForm):
    class Meta:
        model = Prescription
        fields = ['symptoms', 'prescription', 'pat']
        def __init__(self, *args, **kwargs):
            model = Prescription
            user = kwargs.pop('user','')
            super(PrescriptionCreationForm, self).__init__(*args, **kwargs)
            self.fields['pat']=forms.ModelChoiceField(queryset=PatientProfile.objects.all())


import cv2

import tkinter as tk
from PIL import Image
from PIL import ImageTk
from tkinter import messagebox

class Screen():

	def __init__(self, master):
		self.master = master
		self.create_ui()

	def create_ui(self):
		frame1 = tk.Frame(self.master, borderwidth=2, relief='ridge')
		frame2 = tk.Frame(self.master, borderwidth=2, relief='ridge')
		frame3 = tk.Frame(self.master, borderwidth=2, relief='ridge')
		frame4 = tk.Frame(self.master, borderwidth=2, relief='ridge')
		frame5 = tk.Frame(self.master, borderwidth=2, relief='ridge')
		frame6 = tk.Frame(self.master, borderwidth=2, relief='ridge')
		frame7 = tk.Frame(self.master, borderwidth=2, relief='ridge')

		frame1.grid(column=0, row=0, sticky="nsew")
		frame2.grid(column=0, row=1, sticky="nsew")
		frame3.grid(column=1, row=0, sticky="nsew")
		frame4.grid(column=1, row=1, sticky="nsew")
		frame5.grid(column=2, row=0, sticky="nsew")
		frame6.grid(column=2, row=1, sticky="nsew")
		frame7.grid(column=3, row=0, sticky="nsew", rowspan=2)

		# Image
		self.image1 = self.create_image_label(frame1, 300, 300)
		self.image2 = self.create_image_label(frame2, 300, 300)
		self.image3 = self.create_image_label(frame3, 300, 300)
		self.image4 = self.create_image_label(frame4, 300, 300)
		self.image5 = self.create_image_label(frame5, 300, 300)
		self.image6 = self.create_image_label(frame6, 300, 300)

		self.image1.pack(fill=tk.BOTH, expand=tk.YES)
		self.image2.pack(fill=tk.BOTH, expand=tk.YES)
		self.image3.pack(fill=tk.BOTH, expand=tk.YES)
		self.image4.pack(fill=tk.BOTH, expand=tk.YES)
		self.image5.pack(fill=tk.BOTH, expand=tk.YES)
		self.image6.pack(fill=tk.BOTH, expand=tk.YES)

		# Information
		label_name = self.create_infor_label(frame7, "Họ tên:")
		label_position = self.create_infor_label(frame7, "Vị trí:")
		label_plate_number = self.create_infor_label(frame7, "Biển số xe:")
		label_timein = self.create_infor_label(frame7, "Thời gian vào:")
		label_timeout = self.create_infor_label(frame7, "Thời gian ra:")

		self.name = self.create_infor_label(frame7, "Nguyễn Văn Đức", font=("Helvetica", 16, 'bold'), width=20)
		self.position = self.create_infor_label(frame7, "DEV", font=("Helvetica", 16, 'bold'), width=20)
		self.plate_number = self.create_infor_label(frame7, "03829", font=("Helvetica", 16, 'bold'), width=20)
		self.timein = self.create_infor_label(frame7, "07:13:21", font=("Helvetica", 16, 'bold'), width=20)
		self.timeout = self.create_infor_label(frame7, "18:06:03", font=("Helvetica", 16, 'bold'), width=20)

		label_name.grid(row=0, column=0, pady=10, padx=20, sticky=tk.W)
		label_position.grid(row=1, column=0, pady=10, padx=20, sticky=tk.W)
		label_plate_number.grid(row=2, column=0, pady=10, padx=20, sticky=tk.W)
		label_timein.grid(row=3, column=0, pady=10, padx=20, sticky=tk.W)
		label_timeout.grid(row=4, column=0, pady=10, padx=20, sticky=tk.W)
		self.name.grid(row=0, column=1, sticky=tk.W)
		self.position.grid(row=1, column=1, sticky=tk.W)
		self.plate_number.grid(row=2, column=1, sticky=tk.W)
		self.timein.grid(row=3, column=1, sticky=tk.W)
		self.timeout.grid(row=4, column=1, sticky=tk.W)

		button = tk.Button(frame7, text="hello", width=20, command=self.show_error)
		button.grid(row=5, columnspan=2, pady=80)

	def create_image_label(self, container, width, height, background='#e8e8e8'):
		# default no_image path
		default_image_path = 'image/no_image.png'
		image = cv2.imread(default_image_path)
		image = cv2.resize(image, (width, height))
		image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
		image = Image.fromarray(image)
		image = ImageTk.PhotoImage(image)
		label = tk.Label(container, image=image, width=width, height=height, background=background)
		# keep reference for display image
		label.image = image
		return label

	def create_infor_label(self, container, text, font=("Helvetica", 16), fg="#282828", width=15):
		label = tk.Label(container, text=text, font=font, fg=fg, width=width, anchor='w')
		return label

	def set_image(self, location, image, width=300, height=300):
		# if image is None
		# set default image is no_image
		if len(image) == 0:
			default_image_path = 'image/no_image.jpg'
			image = cv2.imread(default_image_path)
		# resize and convert image to tkinter image type
		image = cv2.resize(image, (width, height))
		image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
		image = Image.fromarray(image)
		image = ImageTk.PhotoImage(image)
		# check localion image and put image to label
		if location == 1:
			self.image1.configure(image=image)
			self.image1.image = image
		elif location == 2:
			self.image2.configure(image=image)
			self.image2.image = image
		elif location == 3:
			self.image3.configure(image=image)
			self.image3.image = image
		elif location == 4:
			self.image4.configure(image=image)
			self.image4.image = image
		elif location == 5:
			self.image5.configure(image=image)
			self.image5.image = image
		else:
			self.image6.configure(image=image)
			self.image6.image = image

	def show_alert(self):
		messagebox.showwarning("Infor", "Could not open file")

	def show_error(self):
		top = tk.Toplevel()
		top.title('ALERT')
		tk.Message(top, text="ERROR", padx=20, pady=20).pack()
		top.after(2000, top.destroy)

	def show_success(self):
		top = tk.Toplevel()
		top.title('ALERT')
		tk.Message(top, text="SUCCESS", padx=20, pady=20).pack()
		top.after(2000, top.destroy)

def main():
	master = tk.Tk()
	screen = Screen(master)
	screen.show_success()
	master.mainloop()

if __name__ == '__main__':
	main()


#!/usr/bin/env python

# This script takes in a file consisting of 3 columns:
# ../data/annotations/germinalB/Bcells.GC.CB_450k_methylation.tsv.gz
#
# Chr   Pos     Beta
# chr1	15865	0.872831853992907
# chr1	18827	0.593831488421089
# chr1	29407	0.0518563054261236
# chr1	29425	0.119720029299259
# chr1	29435	0.0494004108738235

# The file above is originated in the script:
#
#

# From the above file it returns a new one:
# '../data/annotations/{}/{}_{}_450k_neighbor_naive.tsv'.format(bcell_type, bcell_type, bcell)
# which contains the methylation and distance to nearest CpG up- and down-stream
#
# Chr	Pos	dist_down	methyl_down	dist_up	methyl_up
# chr1	15865	2962	0.59	2962	0.59
# chr1	18827	10580	0.05	2962	0.87
# chr1	29407	18	    0.12	10580	0.59
# chr1	29425	10	    0.05	18	    0.05
# chr1	29435	39414	0.54	10	    0.12

# importing packages
import gzip
from sys import argv
from pathlib import Path
import subprocess
from zipfile import ZipFile

# Initializing variables
chr_start = int(argv[1])
chr_end = chr_start + 22

input_arr = Path(argv[2])
bcell = str(input_arr).split('/')[-1].split('_')[0]

bcell_type = argv[3]

chr_ls = []
arr_dct = {}
# Count that will keep track on how many lines there are in the input file
count_line = 0

for i in range(chr_start, chr_end):
    chr_ls.append('chr' + str(i))

# Save contents of 450k arr in a dictionary
with gzip.open(input_arr, "rt") as arr:
    next(arr) # skip de 1st line - header
    for line in arr:
        # print(line)
        line = line.rstrip()
        feat = line.split("\t")
        Chr = feat[0]
        Pos = int(float(feat[1]))
        methyl = round(float(feat[2]), 2)
        count_line += 1
        if Chr in chr_ls:
            if Chr not in arr_dct:
                arr_dct[Chr] = {}
            arr_dct[Chr][Pos] = methyl

# Check if the file belongs to a 450K or EPIC array
if count_line < 475000:
    meth_arr = '450k'
    arr_coord_path = Path('../data/annotations/coord_{}.tsv.gz'.format(meth_arr))
    # Chr	Pos
    # chr1	15865
    # chr1	18827
    # chr1	29407
    # chr1	29425
    # chr1	29435
elif count_line > 480000 and count_line < 875000:
    meth_arr = 'epic'
    # Define path of the array coordinate file
    arr_coord_path = Path('../data/annotations/coord_{}_mod.tsv.gz'.format(meth_arr))
    # Chr	Pos
    # chr1	15865
    # chr1	18827
    # chr1	29407
    # chr1	29425
    # chr1	29435

# Define name of the out file
file_out_arr_path = '../data/annotations/{}/{}_{}_{}_neighbor.tsv'.format(bcell_type, bcell_type, bcell, meth_arr)
file_out = open(Path(file_out_arr_path), "wt")
file_out.write("Chr\tPos\tdist_down\tmethyl_down\tdist_up\tmethyl_up\n")

# Write to file and each line is a position with the beta and distance of the nearest up and down-stream CpGs
with gzip.open(arr_coord_path, "rt") as arr:
    next(arr)
    for line in arr:
        line = line.rstrip()
        feat = line.split("\t")
        Chr = feat[0]
        Pos = int(float(feat[1]))
        if Chr in arr_dct:
            dif = [Pos - x for x in arr_dct[Chr].keys()]
            down_ls = [x for x in dif if x < 0]
            up_ls = [x for x in dif if x > 0]
            # Down stream
            if down_ls:
                down = max(down_ls)
            else:
                down = min(up_ls)
            dist_down = abs(down)
            pos_down = Pos - down
            methyl_down = arr_dct[Chr][pos_down]
            # methyl_down=0
            # Up stream
            if up_ls:
                up = min(up_ls)
            else:
                up = max(down_ls)
            dist_up = abs(up)
            pos_up = Pos - up
            methyl_up = arr_dct[Chr][pos_up]
            # methyl_up=0
            line_out = "{}\t{}\t{}\t{}\t{}\t{}\n".format(Chr, Pos, dist_down, methyl_down, dist_up, methyl_up)
            # print(line_out)
            file_out.write(line_out)

file_out.close()

file_out_arr_path_zip = "{}.gz".format(file_out_arr_path)
with ZipFile(file_out_arr_path_zip) as myzip:
    with myzip.open(file_out_arr_path) as myfile:
        print(myfile.read())


import json
import random
from collection import Collection
from standard import Standard
from pprint import pprint

collection = None

def handle_card(list, golden, message):
    if message == 'LEGENDARY':
        choice = collection.get_legendary_not_in_collection(list)
    else:
        choice = random.choice(list)
    choice['isGolden'] = True if golden else False
    return choice

def generate_card(standard):
    rand = random.uniform(0, 100)
    if rand < 0.10:
        return handle_card(standard.legendary, True, 'LEGENDARY')
    elif rand < 1.06:
        return handle_card(standard.legendary, False, 'LEGENDARY')
    elif rand < 1.36:
        return handle_card(standard.epic, True, 'EPIC')
    elif rand < 5.56:
        return handle_card(standard.epic, False, 'EPIC')
    elif rand < 7.04:
        return handle_card(standard.rare, True, 'RARE')
    elif rand < 28.71:
        return handle_card(standard.rare, False, 'RARE')
    elif rand < 30.33:
        return handle_card(standard.common, True, 'COMMON')
    else:
        return handle_card(standard.common, False, 'COMMON')

def get_non_common_card(standard):
    c = generate_card(standard)
    while c['rarity'] != 'COMMON':
        c = generate_card(standard)
    return c

def generate_pack(standard):
    pack = [generate_card(standard) for x in range(5)]
    commons = [c for c in pack if c['rarity'] == 'COMMON']
    if len(commons) == 5:
        pack.pop()
        pack.append(get_non_common_card(standard))
    for c in pack:
        collection.add_to_collection(c, c['isGolden'])

def main():
    global collection
    cards = json.loads(open('cards.collectible.json', encoding='utf8').read())
    standard_lst = [c for c in cards if (c['set'] in ['GILNEAS', 'LOOTAPALOOZA',
                                                    'EXPERT1', 'ICECROWN', 'UNGORO'])]

    standard = Standard(standard_lst)
    runs = 100
    total = 0
    for i in range(0, runs):
        collection = Collection(standard)
        counter = 0
        while collection.dust_remaining() > 0:
            generate_pack(standard)
            counter += 1

        total += counter
        print(f'{i}/{runs}\t\t', end='\r')

    print(f'Average: {total/runs}')
    print(f'Cost: ${((total/runs)/60)*88:.2f} CAD')

if __name__ == '__main__': main()


import subprocess as sp
import cgi
form=cgi.FieldStorage()
user=str(form.getvalue("User_Name"))
user=input("user")
storage=sp.getoutput("id {}".format(user))
lil=storage.split(" ")
if "no" in lil:
        print("user not exist")
else :
	print("wigfwief")


#!/usr/bin/env python
# encoding: utf-8

from django.conf import settings
from oneweekonething.utils import logger, cache
from .models import Category

def seo_processor(requests):
    key = 'seo_processor'
    value = cache.get(key)
    if value:
        logger.info('get processor cache.')
        return value
    else:
        logger.info('set processor cache.')
        value = {
            'SITE_NAME': settings.SITE_NAME,
            'nav_category_list': Category.objects.all(),
            'SITE_BASE_URL': requests.scheme + '://' + requests.get_host() + '/',

        }
        cache.set(key, value, 60 * 60 * 10)
        return value


from flask import render_template
from nyceats import app
from sqlalchemy import create_engine
from sqlalchemy_utils import database_exists, create_database
import pandas as pd
import psycopg2
from flask import request
import googlemaps
import json
from math import sin, cos, sqrt, atan2, radians

with open('nyceats/conf/gmaps_settings.json', 'r') as f:
    data = json.load(f)

gkey=data ['gmaps']['gkey']
gmaps = googlemaps.Client(key=gkey)

user = 'klapina'
host = 'localhost'
dbname = 'restr_db_all'
db = create_engine('postgres://%s%s/%s'%(user,host,dbname))
con = None
con = psycopg2.connect(database = dbname, user = user)

@app.route('/input')
@app.route('/')
@app.route('/index')
def input():
    return render_template("input.html" )

@app.route('/about')
def about():
    return render_template("about.html")

@app.route('/slides')
def slides():
    return render_template("slides.html")

@app.route('/output')
def nyc_output():
    R = 6373.0

    addr = request.args.get('addr')
    geocode_result = gmaps.geocode(address=addr, components={'locality': 'New York', 'country': 'US'})
    inlat = geocode_result[0]['geometry']['location']['lat']
    inlon = geocode_result[0]['geometry']['location']['lng']
    inlat = radians(inlat)
    inlon = radians(inlon)
    #just select restaurants from the  dtabase
    query = "SELECT name, address,zip,lat,lon,type FROM restr_table"
    query_results=pd.read_sql_query(query,con)

    dist=[]
    col=[]
    for index in range(0,query_results.shape[0]):
        lat1 = radians(query_results.iloc[index]['lat'])
        lon1 = radians(query_results.iloc[index]['lon'])
        dlon = lon1 - inlon
        dlat = lat1 - inlat
        a = sin(dlat / 2)**2 + cos(inlat) * cos(lat1) * sin(dlon / 2)**2
        c = 2 * atan2(sqrt(a), sqrt(1 - a))
        distance = 1000 *R * c
        dist.append(distance) # m
        mycolor="FFFF00"
        if query_results.iloc[index]['type']>1:
            mycolor="1D7BfD"
        if query_results.iloc[index]['type']==1:
            mycolor="FD1D7B"

        col.append(mycolor) # m

    query_results['dist']=dist
    query_results['color']=col

    all_names=query_results.sort_values(by='dist')

    return render_template("output.html",  all_names=all_names)

############################################################################


# uncompyle6 version 3.6.7
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: /Users/bogdankyryliuk/src/oss/incubator-superset/superset/migrations/versions/b4a38aa87893_deprecate_database_expression.py
# Compiled at: 2020-01-16 13:27:41
# Size of source mod 2**32: 1345 bytes
__doc__ = 'deprecate database expression\n\nRevision ID: b4a38aa87893\nRevises: ab8c66efdd01\nCreate Date: 2019-06-05 11:35:16.222519\n\n'
revision = 'b4a38aa87893'
down_revision = 'ab8c66efdd01'
import sqlalchemy as sa
from alembic import op

def upgrade():
    with op.batch_alter_table('table_columns') as (batch_op):
        batch_op.drop_column('database_expression')

def downgrade():
    with op.batch_alter_table('table_columns') as (batch_op):
        batch_op.add_column(sa.Column('database_expression', sa.String(255)))


"""
File name:  group_1_gdpr_project.py

Purpose of code:  CPCO will use information extracted from 5 data sources to create 5 dashboards, each with distinct information but that is all pertinent to data privacy regulations and the consequences of violating those regulations.  CPCO obtained its data by scraping JavaScript and html websites, and parsing a word file.  We also used an unusual source--Google Forms--to survey friends, family, and colleagues about their data privacy preferences.  We obtained 68 responses to 5 focused questions, each question offering the user the opportunity to select multiple answer choices or to provide additional comments.  We will use Python to delineate each of the unique answers that the users provided to each question and conduct calculations, including the percentage of users who selected each particular answer choice.

-  Dashboard A identifies specific US laws that meet the requirements of particular GDPR articles.

-  Dashboard B highlights European countries that have instituted privacy laws supplementing those mandated in the GDPR.  To view, please enter B into the input box
-  Dashboard C displays fines levied by the UK's Information Commissioner's Office (ICO) against British companies violating data privacy regulations, an example of one European country's domestic prosecutions citing GDPR.

-  Dashboard D demonstrates the most common methods hackers used in breaching data, the industries targeted most heavily, and the average number of records compromised per organization.

- Dashboard E provides a sample of customers' growing concerns about data privacy, based on a survey conducted by CP CO.

"""
#group_1_gdpr_project.py
# Carly Harris, Darshan Tina, Lokesh Nandiraju, Heather Manganello, Kopal Agrawal

#import statements
import textwrap
import pandas as pd
import csv
import datetime
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table as dt
from dash.dependencies import Input, Output
import matplotlib.pyplot as plt
import plotly.graph_objs as go
import numpy as np
from decimal import Decimal
import io
from docx import Document
import re

#dashboard styling
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

colors = {
    'background': '#D3E0F5'
}

# ************************** User Interface of Dash Board 1 ********************
#Read data from Word document into Dataframe
def read_docx(filename, tab_id=None, **kwargs):
    def read_docx_tab(tab, **kwargs):
        vf = io.StringIO()
        writer = csv.writer(vf)
        for row in tab.rows:
            writer.writerow(cell.text for cell in row.cells)
        vf.seek(0)
        return pd.read_csv(vf, **kwargs)

    doc = Document(filename)
    if tab_id is None:
        return [read_docx_tab(tab, **kwargs) for tab in doc.tables]
    else:
        try:
            return read_docx_tab(doc.tables[tab_id], **kwargs)
        except IndexError:
            print('Error: specified [tab_id]: {}  does not exist.'.format(tab_id))
            raise

#Parse raw data from .docx file
my_pan_file = read_docx('DS1_Uncleaned.docx')[0]

#Export read .docx content to a CSV file
my_pan_file.to_csv('DS_1_Cleaned.csv', sep=',')

####################################

#Read data from CSV file
my_panda = pd.read_csv('DS_1_Cleaned.csv')
del my_panda['Unnamed: 0']

### Take article number as input
print('\n\n##################################################################################################')
print('\nBeginning of execution on Data Source 1\n\n')

wanna_run_again=True
while wanna_run_again==True:
    art_num_good =False
    while art_num_good==False:
        try:
            art_no = int(input('\n\nPlease enter the article number: '))
            if 1<=art_no<=99:
                art_num_good=True
            else:
                print('Article number '+str(art_no)+' doesn\'t exist. Enter between 1 to 99 only')
        except:
            print('Bad format!. Please enter GDPR article number between 1 to 99 only')

    print('Article number '+str(art_no)+' is called \"'+str(my_panda.iloc[art_no,0])[3:]+'\"')

    ### Checking corresponding US LAW exists or not
    check_law_flag=False
    while check_law_flag==False:
        print()
        check_law=input('Do you want to check whether corresponding United States Law exists for the GDPR article number ' +str(art_no)+' ?\nEnter Y or N (Not case sensitive): ')
        if check_law=='Y' or check_law=='y':
            if str(my_panda.iloc[art_no,2])[0:3]=='N/A':
                print('\nNo corresponding U.S Law exists\nNON-COMPLIANT\n')
                check_law_flag=True
            else:
                #print Law exists and ask whether they want to see the text of that law
                print('\nRelevant U.S Law exists!!\nCOMPLIANT\n')
                check_display_law=False
                while check_display_law==False:
                    display_law=input('Do you want to view corresponding United States Law for GDPR article number ' +str(art_no)+' ?\nEnter Y or N (Not case sensitive): ')
                    if  display_law=='Y' or display_law=='y':
                        print('\nThe corresponding U.S Law for GDPR article number '+str(art_no)+' is: \n')
                        print('\"'+my_panda.iloc[art_no,2]+'\""')
                        check_law_flag=True
                        check_display_law=True
                    elif display_law=='N' or display_law=='n':
                        check_law_flag=True
                        check_display_law=True
                        print()
                    else:
                        print('Bad Input! Enter either Y or N: (Not case sensitive)')
        elif check_law=='N' or check_law=='n':
            print()
            check_law_flag=True
        else:
            print('Bad Input! Enter either Y or N (Not case sensitive): ')

    ### Ask user whether they want to see text for the GDPR article number they entered and display if they want to
    check_article_flag=False
    while check_article_flag==False:
        check_article=input('Do you want to view GDPR Regulation for article number '+str(art_no)+'?\n'+'Enter Y or N (Not case sensitive): ')
        if check_article=='Y' or check_article=='y':
            print()
            print('GDPR article number '+str(art_no)+' states that:')
            print()
            print('\"'+str(my_panda.iloc[art_no,1])+'\"')
            check_article_flag=True
            run_again_flag=False
            while run_again_flag==False:
                run_again=input('\nDo you want to run this again???\nEnter Y or N (Not case sensitive): \n')
                if run_again=='Y' or run_again=='y':
                    wanna_run_again=True
                    run_again_flag=True
                elif run_again=='N' or run_again=='n':
                    wanna_run_again=False
                    run_again_flag=True
                else:
                    run_again_flag=False
                    print('Bad input. Enter either Y or N (Not case sensitive): ')
        elif check_article=='N' or check_article=='n':
            check_article_flag=True
            run_again_flag=False
            while run_again_flag==False:
                run_again=input('\nDo you want to run this again???\nEnter Y or N (Not case sensitive): \n')
                if run_again=='Y' or run_again=='y':
                    wanna_run_again=True
                    run_again_flag=True
                elif run_again=='N' or run_again=='n':
                    wanna_run_again=False
                    run_again_flag=True
                else:
                    run_again_flag=False
                    print('Bad input. Enter either Y or N (Not case sensitive): ')

desc_lst=[]
for val in my_panda['GDPR & U.S Law Data.2']:
    if val[:3]=='N/A':
        desc_lst.append('Compliant')
    else:
        desc_lst.append('Not Compliant')
desc_pan=pd.Series(desc_lst)
print('\n\n\nCompliance Summary: \nThe number of GDPR articles for which compliant/corresponding U.S Laws exist are: '+str(desc_pan.describe()[3])+'.')
print('\nEnd of execution on Data Source 1')
print('##################################################################################################\n\n')

# ******************************* APPLICATION LAYOUT ********************************
# header layout
app.layout = html.Div(style={'backgroundColor': colors['background']}, children=[
    html.H1(children='CP CO',
            style={
                'textAlign': 'center'
                }),
    html.Div(children='GDPR and Information Security Consultancy',
             style={
                 'textAlign': 'center'
                 }),
    html.Div(children='Our mission: ',
             style={
                 'textAlign': 'center',
                 'font-weight': 'bold'
                 }),
    html.Div(children='To help organizations understand their vulnerabilities in data privacy protection and information security',
             style={
                 'textAlign': 'center'
                 }),
    html.Div(children='requirements for compliance with European regulations consequences for perceived violations, &',
             style={
                 'textAlign': 'center'
                 }),
    html.Div(children='avoid massive fines of up to 4% of revenue in order to effectively manage the use of European individuals personal information within their operations.',
             style={
                 'textAlign': 'center'
                 }),

    html.Div(children='Enter:'),
    html.Div(children='1-99 = Dashboard A: Correlation of US Laws to European Union\'s General Data Protection Regulation (GDPR) Articles '),
    html.Div(children='B = Dashboard B: Additional Data Privacy Requirements Per European Union Country'),
    html.Div(children='C = Dashboard C: Companies Fined by GDPR Law'),
    html.Div(children='D = Dashboard D: Statistics on Worldwide Data Breaches'),
    html.Div(children='E = Dashboard E: Surveys on Customers\' Preferences on Data Privacy'),
    html.Div(children=' '),
    html.Div(children='''
        Dashboard to Graph:
    '''),
    dcc.Input(id='input', value='', type='text'),
    html.Div(id='output-graph'),

])

#user input component
@app.callback(
    Output(component_id='output-graph', component_property='children'),
    [Input(component_id='input', component_property='value')]
)

#function to display graphs based on user input
def update_value(input_data):

    #-----------------------Dashboard A--------------------
    if(input_data.isdigit()):
        if(int(input_data) >= 0 and int(input_data) <= 99):

            # read cleaned csv data set
            df = pd.read_csv('DS_1_Cleaned.csv')
            df1 = pd.DataFrame(df)
            new = pd.DataFrame()
            new['Article'] = df1['GDPR & U.S Law Data']
            new['GDPR Law'] = df1['GDPR & U.S Law Data.1']
            new['US Law'] = df1['GDPR & U.S Law Data.2']

            temp = []
            vals = new.iloc[int(input_data)]
            t1 = pd.DataFrame(vals)
            for i in vals:
                temp.append(i)

            t1 = t1.transpose()
            # dash table of db1
            return dt.DataTable(
                    id='table',
                    columns=[{"name": i, "id": i} for i in new.columns],
                    data = t1.to_dict("rows"),
                    style_data={'whiteSpace': 'normal'},
                    css=[{
                        'selector': '.dash-cell div.dash-cell-value',
                        'rule': 'display: inline; white-space: inherit; overflow: inherit; text-overflow: inherit;'
                        }],
                    style_table={'overflowX': 'scroll'},

                    style_cell={
                        'textAlign': 'center',
                        'minWidth': '50px', 'maxWidth': '180px'
                        },
                    style_cell_conditional=[
                        {
                            'if': {'column_id': 'Region'},
                            'textAlign': 'center',
                            'if': {'row_index': 'odd'},
                            'backgroundColor': 'rgb(248, 248, 248)'
                            }
                        ],
                    style_header={
                        'backgroundColor': 'white',
                        'fontWeight':'bold'
                        }
                    )

    #-----------------------Dashboard B--------------------
    elif(input_data == 'B' or input_data == 'b'):

        # Web Scraping by implementing ParseHub API
        # http://www.alstongdprtracker.com/
        # Cleaning Raw Data Below
        """
        df = pd.read_csv('DataSet4_cleaned.csv')
        gdpr = pd.DataFrame()
        l1= []
        df = df.replace('Yes', 1)
        df = df.replace('No', 0)
        df = df.fillna(0)
        """
        # read csv DataSet and parse values
        df = pd.read_csv('DataSet4_cleaned.csv')
        gdpr = pd.DataFrame()
        df = df.replace('Yes', 1)
        df = df.replace('No', 0)
        df = df.fillna(0)
        gdpr = df.pivot(index='country', columns='article', values='deviation')
        #one_gdpr = np.arange(8)
        #one_gdpr = np.reshape(2,4)

        gdpr = gdpr.fillna(0)

        # get sum of supplemental laws for each country
        y_list = []
        for j in gdpr.values:
            sum = 0
            for i in j:
                sum += i
            y_list.append(sum)

        # get list of country whose article deviation value equals 1
        ls1 = []
        for i in gdpr.index:
            l1 = []
            for j in gdpr.columns:
                if(gdpr.loc[i][j] == 1.0):
                    l1.append(j)

            ls1.append(l1)

        # first trace value of bar graph
        trace1 = go.Bar(x=gdpr.index, y=y_list)

        # dictionary of gdpr article deviation per country
        valid = {}
        i=0
        for j in gdpr.index:
            valid[j] = ls1[i]
            i = i+1

        # fill empty values
        f1 = pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in valid.items() ]))

        # matplotlib
        '''obj = gdpr.index
        y_pos = np.arange(len(obj))
        plt.bar(y_pos, ls1)
        plt.xticks(y_pos, obj)'''

        #return graphs - bar chart and data table
        return (dcc.Graph(
            id='example_graph',
            figure={
                'data':[trace1],
                'layout':
                go.Layout(title='Number of Supplemental Privacy Laws Enacted by European Union Country',
                          yaxis={'title':'Supplemental Privacy Laws'},
                          xaxis={'title':'Country'},
                                 barmode='stack', hovermode='closest')
                }),
                html.H4(children='Supplemental Laws to GDPR Articles per Country'),
                dt.DataTable(
                    id='table',
                    columns=[{"name": i, "id": i} for i in f1.columns],
                    data=f1.to_dict("rows"),
                    style_table={'overflowX': 'scroll'},
                    style_cell={
                        'textAlign': 'center',
                        },
                    style_cell_conditional=[
                        {
                            'if': {'column_id': 'Region'},
                            'textAlign': 'center',
                            'if': {'row_index': 'odd'},
                            'backgroundColor': 'rgb(248, 248, 248)'
                            }
                        ],
                    style_header={
                        'backgroundColor': 'white',
                        'fontWeight':'bold'
                        }
                    )

                )

    #-----------------------Dashboard C--------------------
    elif(input_data == 'C' or input_data == 'c'):

        # Web Scraping by implementing ParseHub API
        # https://ico.org.uk/action-weve-taken/enforcement/
        # Cleaning Raw Data Below
        """
        pd = pd.read_csv('C:\\Users\Darshan\Desktop\Dataset5_raw - Copy.csv')
        l = []
        l1 = []
        l = pd['Company_Fines']
        l1 = pd['Company_Dates']

        a = []
        for i in l1:
            a.append(i.split(',')[0])

        pd['Refined_Dates'] = a

        def isNumber(inputstr):
            if inputstr.isdigit():
                return True
            else:
                return False
        b = []
        for i in l:
            i=i.replace(' ','')
            s = "";
            if('£' in i):
                s = s+'£'
                j = 1
                while((i[i.find('£')+j])==',' or isNumber(i[i.find('£')+j])) :
                    s = s+i[i.find('£')+j]
                    j = j+1
                b.append(s)
            else:
                b.append(' ')

        pd['Fine_In_Euros'] = b
        New = pd.copy(deep = True)
        New = New.drop('Company_Fines',1)
        New = New.drop('Company_Dates',1)
        New.to_csv('DataSet5_cleaned.csv')
        New.to_json('test.json')
        """
        df = pd.read_csv('DataSet5_cleaned.csv')
        df.columns = ['Sr.no','Company_Name','Refined_Dates','Fine_In_Euros']

        company_name_list = df['Company_Name']
        date_list = df['Refined_Dates']
        fine_list = df['Fine_In_Euros']

        fine_in_numbers = []
        ind = 0
        index = []
        # get list of fines
        for i in fine_list:
            if(i!=' '):
                t = i[1:]
                x = t.find(',')
                t = t[0:x] + t[x+1:]
                fine_in_numbers.append(float(t))
                index.append(ind)
            ind+=1

        company_fined = []

        # specific companies fined
        for i in index:
            company_fined.append(company_name_list[i])

        # dict of companies and amount fined
        d = dict(zip(company_fined,fine_in_numbers))
        company_fined = []
        for i in d.keys():
            company_fined.append(i)
        fine_in_numbers = []
        for i in d.values():
            fine_in_numbers.append(i)

        # line graph
        return dcc.Graph(id='example',
                         figure ={
                             'data': [
                                 {'x':company_fined, 'y':fine_in_numbers, 'type':'line', 'name':'Fines Levied','xlabel':'Companies'},
                                 ],
                             'layout':{
                                 'title':'Companies Fined by GDPR Law',
                                 'xaxis':{
                                     'title':'Company',
                                     'showgrid':False,
                                     'zeroline':False,
                                     'tickmode':'array',
                                     'tickvals':[]
                                     },
                                 'yaxis':{
                                     'title':'Fine Levied'
                                     }
                                 }
                             })

    #-----------------------Dashboard D--------------------

    elif(input_data == 'D' or input_data == 'd'):

        """from bs4 import BeautifulSoup
        import requests
        website_data = requests.get('https://en.wikipedia.org/wiki/List_of_data_breaches').text
        soup = BeautifulSoup(website_data,'html.parser')
        table = soup.find('table',{'class':'wikitable sortable'})

        headers = [th.text for th in table.select("tr th")]

        with open("out_cleaned.csv", "w") as f:
            wr = csv.writer(f)
            wr.writerow(headers[:-1])
            wr.writerows([[td.text for td in row.find_all("td")][:-1] for row in table.select("tr + tr")])
            """

        #reading a csv file using pandas
        data=pd.read_csv('datasource6_cleaned.csv',index_col=False)
        for x in range(0,len(data['Method'])):
            #removing redundant values and characters
            data['Method'][x]=str(data['Method'][x]).replace('unknown\n','unknown')
            data['Method'][x]=str(data['Method'][x]).replace('nan','unknown')
            data['Method'][x]=str(data['Method'][x]).replace('poor security\n','poor security')
            data['Method'][x]=str(data['Method'][x]).replace('hacked\n','hacked')
            data['Method'][x]=str(data['Method'][x]).replace('social engineering\n','social engineering')
            data['Method'][x]=str(data['Method'][x]).replace('lost / stolen media\n','lost / stolen media')
        #extracting distinct Method values
        setMethod=set(data['Method'])
        setMethod=list(setMethod)
        listZero=[0,0,0,0,0,0,0,0,0,0]
        dictMethod=dict(zip(setMethod,listZero))
        for x in range(0,len(data['Method'])):
            #calculating frequency of each method
            for y in setMethod:
                if data['Method'][x]==y:
                    dictMethod[y]+=1

        key_list = []
        for i in dictMethod.keys():
            key_list.append(i)
        val_list = []
        for j in dictMethod.values():
            val_list.append(j)

        setOT=set(data['Organization type'])

        for x in range(0,len(data['Organization type'])):
            #removing redundant characters and values
            data['Organization type'][x]=str(data['Organization type'][x]).replace('web, tech','tech, web')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('consulting, accounting\n','consulting, accounting')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('social network\n','social network')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('web\n','web')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('ticket distribution\n','ticket distribution')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('government, database\n','government, database')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('transport\n','transport')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('genealogy\n','genealogy')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('healthcare\n','healthcare')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('financial\n','financial')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('credit reporting\n','credit reporting')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('retail\n','retail')
        setOT=set(data['Organization type'])
        setOT=list(setOT)
        listZero=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
        dictOT=dict(zip(setOT,listZero))
        for x in range(0,len(data['Organization type'])):
            #calculating frequency of each Organiztion Type attacked
            for y in setOT:
                if data['Organization type'][x]==y:
                    dictOT[y]+=1

        k2 = []
        v2 = []
        for i in dictOT.keys():
            k2.append(i)
        for j in dictOT.values():
            v2.append(j)

        # data frame
        df = pd.read_csv('datasource6_cleaned.csv')
        n=[]
        j=0
        x = []
        #cleaning Records to only obtain integer values
        for i in df['Records']:
            j+=1
            if re.match('^[1-9][0-9]*',i):
                n.append(i)
                x.append(j)
        t=[]
        j=0
        for i in n:
            j+=1
            if re.search(r'^[^a-zA-Z]*$',i):
                t.append(i)
            else:
                x.remove(j)

        s = []
        for i in t:
            if '\n' in i:
                i = i[:i.find('\n')]
            s.append(i)
        q = []
        m=[]
        for i in s:
            q = i.split(',')
            string =""
            for i in q:
                string += i
            m.append(float(string))
        df.columns = ['Entity', 'Year', 'Records', 'Organization type', 'Method']
        year = []
        for i in df['Year']:
            if '\n' in i:
                i = i[:i.find('\n')]
            year.append(i)
        new_year =[]
        count =0
        for i in range(len(year)):
            if i not in x:
                year = year[:i]+year[i+1:]
        year = year[1:]
        d ={}

        for i in range(len(year)):
            if i not in d.keys():
                d[year[i]]=m[i]
            else:
                d[year[i]] += m[i]
        new_year = []
        new_values = []
        for i in d.keys():
            new_year.append(i)
        for i in d.values():
            new_values.append(i)

        a=np.array(df['Year'])
        b=np.array(df['Entity'])
        c=np.array([])
        for x in a:
            x.replace('\n','')

            c=np.append(c,[x])
        attack=pd.crosstab(b,c)
        attack[attack==1]='Compromised'
        e = []
        kopal = pd.DataFrame(attack)
        for i in kopal.index:
            e.append(i)
        kopal['Company_Names'] = e
        kopal = kopal.iloc[:,[16,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]]

        # bar graphs and data table
        return(html.Div([
    html.Div([
        html.Div([
            dcc.Graph(id='g1', figure={
                'data': [
                    {'x': key_list, 'y': val_list, 'type': 'bar'}],

                 'layout':
                 go.Layout(title='Number of Each Attack Vector Used',
                           yaxis={'title':'# of Attacks'},
                           barmode='stack', hovermode='closest')
                })
            ],
                 className="six columns"),

        html.Div([
            dcc.Graph(id='g2', figure={
                'data': [
                    {'x': k2, 'y': v2, 'type': 'bar'}],
                'layout':
                 go.Layout(title='Number of Attacks per Organization',
                           yaxis={'title':'# of Attacks'},
                           barmode='stack', hovermode='closest')
                })
        ], className="six columns"),
    ], className="row"),
    ]),
               dcc.Graph(
        id='example-graph',
        figure={
            'data': [
                {'x': new_year, 'y': new_values, 'type': 'bar'},
            ],
            'layout': {
                'title': 'Number of Records Compromised per Year',
                'xaxis':{
                    'title': 'Year'
                    },
                'yaxis':{
                    'title': '# of Records'
                    }
            }
        }
    ),
    html.H4(children='Years in which Companies Data were Compromised'),
                dt.DataTable(
                    id='table',
                    columns=[{"name": i, "id": i} for i in kopal.columns],
                    data=attack.to_dict("rows"),
                    style_table={'overflowX': 'scroll'},
                    style_cell={
                        'textAlign': 'center',
                        },
                    style_cell_conditional=[
                        {
                            'if': {'column_id': 'Region'},
                            'textAlign': 'center',
                            'if': {'row_index': 'odd'},
                            'backgroundColor': 'rgb(248, 248, 248)'
                            }
                        ],
                    style_header={
                        'backgroundColor': 'white',
                        'fontWeight':'bold'
                        }
                    )

)

    #-----------------------Dashboard E--------------------
    elif(input_data == 'E' or input_data == 'e'):

        pd1 = pd.read_csv('data_source3_raw_68.csv') #import raw data file, downloaded from Google Forms
        # Google Forms outoupts its information in a csv file, not requiring any scraping or parsing
        # This file contains responses from 68 individuals surveyed about their privacy preferences.
        # The form contained 5 questions, and respondents could select more than one answer to each.

        ShinyTbl = pd1.copy(deep=True)
        ShinyTbl = ShinyTbl.drop('Timestamp',1) #don't need the dates or times of the responses

        #ShinyTbl.describe()

        ShinyTbl.columns = ['Advertisements','Not_In_Favor','Feelings_PI','Companies_Know_You','GDPR_provisions'] #rename column headers

        # Note that respondents can select one or MORE of these options (so cells can contain several answers).
        # While most answers are distinct, searches for "no" responses would inaccurately also count "not sure" responses.
        # Therefore, need to adjust responses for the "Advertisements" and "Companies_Know_You" columns.

        for b in range(0,len(ShinyTbl['Advertisements'])):
            ShinyTbl['Advertisements'][b]=str(ShinyTbl['Advertisements'][b]).replace('Not sure','Unsure') #replace "not sure" with "unsure"

        for f in range(0,len(ShinyTbl['Companies_Know_You'])):
            ShinyTbl['Companies_Know_You'][f]=str(ShinyTbl['Companies_Know_You'][f]).replace('Not sure','Unsure')

        ShinyTbl.to_csv('DataSource_3_cleaned.csv') #outputing cleaned file

        # Counting Specific Responses to 1st question ("Advertisements")

        L1 = ShinyTbl['Advertisements']

        opt1_1 = 'protect'; opt2_1 = 'careful'; opt3_1 = 'No'; opt4_1 = 'Unsure'
        ctopt1_1 = 0; ctopt2_1 = 0; ctopt3_1 = 0; ctopt4_1 = 0

        for a in L1:
                a = (str)(a)
                if opt1_1 in a:
                    ctopt1_1 += 1
                if opt2_1 in a:
                    ctopt2_1 += 1
                if opt3_1 in a:
                    ctopt3_1 += 1
                if opt4_1 in a:
                    ctopt4_1 += 1
        totalAns1 = ctopt1_1 + ctopt2_1 + ctopt3_1 + ctopt4_1

        dict1 = {'Yes, even if they\'re not being careful with my data; I don\'t care': ctopt2_1,
                'Yes, as long as they protect and use my data responsibly' : ctopt1_1,
                'No' : ctopt3_1,
                'Not Sure' : ctopt4_1,
                'Total Responses' : totalAns1}

        # Counting Specific Responses to 2nd question ("Not In Favor")

        L2 = ShinyTbl['Not_In_Favor']

        opt1_2 = 'misusing'; opt2_2 = 'not protecting'; opt3_2 = 'watching'
        ctopt1_2 = 0; ctopt2_2 = 0; ctopt3_2 = 0

        for c in L2:
                c = (str)(c)
                if opt1_2 in c:
                    ctopt1_2 += 1
                if opt2_2 in c:
                    ctopt2_2 += 1
                if opt3_2 in c:
                    ctopt3_2 += 1

        totalAns2 = ctopt1_2 + ctopt2_2 + ctopt3_2

        dict2 = {'I am concerned about companies misusing my data' : ctopt1_2,
                'I am concerned about companies not protecting my data from hackers' : ctopt2_2,
                'I just don\'t like companies watching what I do online' : ctopt3_2,
                'Total Responses' : totalAns2}

        # Counting Specific Responses to 3rd question ("Feelings_PI")

        L3 = ShinyTbl['Feelings_PI']

        opt1_3 = 'mostly good'; opt2_3 = 'bad'; opt3_3 = 'care about'; opt4_3 = 'Not sure'
        ctopt1_3 = 0; ctopt2_3 = 0; ctopt3_3 = 0; ctopt4_3 = 0

        for d in L3:
                d = (str)(d)
                if opt1_3 in d:
                    ctopt1_3 += 1
                if opt2_3 in d:
                    ctopt2_3 += 1
                if opt3_3 in d:
                    ctopt3_3 += 1
                if opt4_3 in d:
                    ctopt4_3 += 1

        totalAns3 = ctopt1_3 + ctopt2_3 + ctopt3_3 + ctopt4_3

        dict3 = {'I think that they are mostly good at using our data in responsible and secure ways' : ctopt1_3,
                'I think that they mostly want to use our data in a responsible and secure way . . . but they are bad at it' : ctopt2_3,
                'I don\'t think that they care about using our data responsibly or securely at all' : ctopt3_3,
                'Not sure' : ctopt4_3,
                'Total Responses' : totalAns3}

        # Counting Specific Responses to 4th question ("Companies_Know_You")

        L4 = ShinyTbl['Companies_Know_You']

        opt1_4 = 'Yes'; opt2_4 = 'No'; opt3_4 = 'Unsure'
        ctopt1_4 = 0; ctopt2_4 = 0; ctopt3_4 = 0

        for e in L4:
                e = (str)(e)
                if opt1_4 in e:
                    ctopt1_4 += 1
                if opt2_4 in e:
                    ctopt2_4 += 1
                if opt3_4 in e:
                    ctopt3_4 += 1

        totalAns4 = ctopt1_4 + ctopt2_4 + ctopt3_4

        dict4 = {'Yes' : ctopt1_4,
                'No' : ctopt2_4,
                'Not sure' : ctopt3_4,
                'Total Responses' : totalAns4}

        # Counting Specific Responses to 5th question ("Feelings_PI")

        L5 = ShinyTbl['GDPR_provisions']

        opt1_5 = 'delete'; opt2_5 = 'being done'; opt3_5 = 'control'; opt4_5 = 'complain'
        ctopt1_5 = 0; ctopt2_5 = 0; ctopt3_5 = 0; ctopt4_5 = 0

        for g in L5:
                g = (str)(g)
                if opt1_5 in g:
                    ctopt1_5 += 1
                if opt2_5 in g:
                    ctopt2_5 += 1
                if opt3_5 in g:
                    ctopt3_5 += 1
                if opt4_5 in g:
                    ctopt4_5 += 1

        totalAns5 = ctopt1_5 + ctopt2_5 + ctopt3_5 + ctopt4_5

        dict5 = {'The right to ask companies to delete the data they have on me' : ctopt1_5,
                'The right to ask what is being done with my data' : ctopt2_5,
                'The ability to control how my data is being used' : ctopt3_5,
                'The ability to complain when my data is misused, which could result in steep fines for the offending company' : ctopt4_5,
                'Total Responses' : totalAns5}

        # question 1
        d1key = []
        d1Pct = []

        for i in dict1.keys():
            d1key.append(i)
        del d1key[-1]

        for i in dict1.values():
            d1Pct.append(i)

        total = d1Pct[-1]
        for i in range(4):
            d1Pct[i] = (d1Pct[i]/total) *100
            d1Pct[i] = round(d1Pct[i],2)

        del d1Pct[-1]

        #question 2
        d2key = []
        d2Pct = []
        for i in dict2.keys():
            d2key.append(i)
        del d2key[-1]

        for i in dict2.values():
            d2Pct.append(i)

        total = d2Pct[-1]

        for i in range(4):
            d2Pct[i] = (d2Pct[i]/total) *100
            d2Pct[i] = round(d2Pct[i],2)

        del d2Pct[-1]

        #question 3
        d3key = []
        d3Pct = []
        for i in dict3.keys():
            d3key.append(i)
        del d3key[-1]

        for i in dict3.values():
            d3Pct.append(i)

        total = d3Pct[-1]

        for i in range(4):
            d3Pct[i] = (d3Pct[i]/total) *100
            d3Pct[i] = round(d3Pct[i],2)

        del d3Pct[-1]

        #question 4
        d4key = []
        d4Pct = []

        for i in dict4.keys():
            d4key.append(i)
        del d4key[-1]

        for i in dict4.values():
            d4Pct.append(i)

        total = d4Pct[-1]
        for i in range(3):
            d4Pct[i] = (d4Pct[i]/total) *100
            d4Pct[i] = round(d4Pct[i],2)

        del d4Pct[-1]

        #question 5
        d5key = []
        d5Pct = []

        for i in dict5.keys():
            d5key.append(i)
        del d5key[-1]

        for i in dict5.values():
            d5Pct.append(i)

        total = d5Pct[-1]

        for i in range(4):
            d5Pct[i] = (d5Pct[i]/total) *100
            d5Pct[i] = round(d5Pct[i],2)

        del d5Pct[-1]

        q1 = 'Are you in favor of websites or apps using what they learn about you to show you advertisements that you might find interesting?'
        q1 = textwrap.wrap(q1)
        q2 = 'If you are not in favor of receiving targeted advertisements, why not?'
        q2 = textwrap.wrap(q2)
        q3 = 'In general, what are your feelings toward companies that host websites today and have access to our personal information?'
        q3 = textwrap.wrap(q3)
        q4 = 'Do you think companies hosting websites know too much about you?'
        q4 = textwrap.wrap(q4)
        q5 = 'Which of these types of privacy provisions--which already have been enacted in Europe--would you most like see applied in the US?'
        q5 = textwrap.wrap(q5)

        return (
            dcc.Graph(id='pi',
                         figure={
                             'data': [
                                 {
                                     'labels': d1key,
                                     'values': d1Pct,
                                     'type': 'pie',
                                     'title': '<br>'.join(q1),
                                     'marker': {'colors': ['rgb(114,119,121)',
                                                           'rgb(18, 36, 37)',
                                                           'rgb(34, 53, 101)',
                                                           'rgb(51,155,255)',
                                                           'rgb(6, 4, 4)']},
                                     'domain': {'x': [0, .48],
                                                'y': [0, .49]},
                                     'hoverinfo':'label+percent+name',
                                     'textinfo':'none'
                                     },
                                 {
                                     'labels': d2key,
                                     'values': d2Pct,
                                     'title': '<br>'.join(q2),
                                     'marker': {'colors': ['rgb(114,119,121)',
                                                           'rgb(18, 36, 37)',
                                                           'rgb(34, 53, 101)',
                                                           'rgb(51,155,255)',
                                                           'rgb(6, 4, 4)']},
                                     'type': 'pie',

                                     'domain': {'x': [.52, 1],
                                                'y': [0, .49]},
                                     'hoverinfo':'label+percent+name',
                                     'textinfo':'none'
                                     },
                                 {
                                     'labels': d3key,
                                     'values': d3Pct,
                                     'title':'<br>'.join(q3),
                                     'marker': {'colors': ['rgb(114,119,121)',
                                                           'rgb(18, 36, 37)',
                                                           'rgb(34, 53, 101)',
                                                           'rgb(51,155,255)',
                                                           'rgb(6, 4, 4)']},
                                     'type': 'pie',

                                     'domain': {'x': [0, .33],
                                                'y': [.5, 1]},
                                     'hoverinfo':'label+percent+name',
                                     'textinfo':'none'
                                     },
                                 {
                                     'labels': d4key,
                                     'values': d4Pct,
                                     'title': '<br>'.join(q4),
                                     'marker': {'colors': ['rgb(114,119,121)',
                                                           'rgb(18, 36, 37)',
                                                           'rgb(34, 53, 101)',
                                                           'rgb(51,155,255)',
                                                           'rgb(6, 4, 4)']},
                                     'type': 'pie',

                                     'domain': {'x': [.35, .67],
                                                'y': [.51, 1]},
                                     'hoverinfo':'label+percent+name',
                                     'textinfo':'none'
                                     },
                                 {
                                     'labels': d5key,
                                     'values': d5Pct,
                                     'title': '<br>'.join(q5),
                                     'marker': {'colors': ['rgb(114,119,121)',
                                                           'rgb(18, 36, 37)',
                                                           'rgb(34, 53, 101)',
                                                           'rgb(51,155,255)',
                                                           'rgb(6, 4, 4)']},
                                     'type': 'pie',

                                     'domain': {'x': [.70, 1],
                                                'y': [.51, 1]},
                                     'hoverinfo':'label+percent+name',
                                     'textinfo':'none'
                                     }
                                 ],
                             'layout': {'title': 'Customers\' Preferences on Data Privacy',
                                        'height':700,
                                        'showlegend': False}
                             }
                         )
            )

    #------------------Invalid User input------------------
    else:
        return html.Div('Please enter: 1-99 or B,C,D,E')

#run server on main module
if __name__ == '__main__':
    app.run_server(debug=False)


import asyncio
import aiohttp
import sortedcontainers
import logging
import dateutil.parser
import collections
from typing import Dict

from hailtop.utils import time_msecs, secret_alnum_string, periodically_call
from hailtop import aiotools, aiogoogle
from gear import Database

from .instance import Instance
from .zone_monitor import ZoneMonitor

log = logging.getLogger('inst_collection')

class InstanceCollection:
    def __init__(self, app, name, machine_name_prefix, is_pool):
        self.app = app
        self.db: Database = app['db']
        self.compute_client: aiogoogle.ComputeClient = self.app['compute_client']
        self.zone_monitor: ZoneMonitor = self.app['zone_monitor']

        self.name = name
        self.machine_name_prefix = f'{machine_name_prefix}{self.name}-'
        self.is_pool = is_pool

        self.name_instance: Dict[str, Instance] = {}
        self.live_free_cores_mcpu_by_zone: Dict[str, int] = collections.defaultdict(int)

        self.instances_by_last_updated = sortedcontainers.SortedSet(key=lambda instance: instance.last_updated)

        self.n_instances_by_state = {'pending': 0, 'active': 0, 'inactive': 0, 'deleted': 0}

        # pending and active
        self.live_free_cores_mcpu = 0
        self.live_total_cores_mcpu = 0

        self.boot_disk_size_gb = None
        self.max_instances = None
        self.max_live_instances = None

        self.task_manager = aiotools.BackgroundTaskManager()

    async def async_init(self):
        self.task_manager.ensure_future(self.monitor_instances_loop())

    def shutdown(self):
        self.task_manager.shutdown()

    @property
    def n_instances(self):
        return len(self.name_instance)

    def generate_machine_name(self):
        while True:
            # 36 ** 5 = ~60M
            suffix = secret_alnum_string(5, case='lower')
            machine_name = f'{self.machine_name_prefix}{suffix}'
            if machine_name not in self.name_instance:
                break
        return machine_name

    def adjust_for_remove_instance(self, instance):
        assert instance in self.instances_by_last_updated

        self.instances_by_last_updated.remove(instance)

        self.n_instances_by_state[instance.state] -= 1

        if instance.state in ('pending', 'active'):
            self.live_free_cores_mcpu -= max(0, instance.free_cores_mcpu)
            self.live_total_cores_mcpu -= instance.cores_mcpu
            self.live_free_cores_mcpu_by_zone[instance.zone] -= max(0, instance.free_cores_mcpu)

    async def remove_instance(self, instance, reason, timestamp=None):
        await instance.deactivate(reason, timestamp)

        await self.db.just_execute('UPDATE instances SET removed = 1 WHERE name = %s;', (instance.name,))

        self.adjust_for_remove_instance(instance)
        del self.name_instance[instance.name]

    def adjust_for_add_instance(self, instance):
        assert instance not in self.instances_by_last_updated

        self.n_instances_by_state[instance.state] += 1

        self.instances_by_last_updated.add(instance)
        if instance.state in ('pending', 'active'):
            self.live_free_cores_mcpu += max(0, instance.free_cores_mcpu)
            self.live_total_cores_mcpu += instance.cores_mcpu
            self.live_free_cores_mcpu_by_zone[instance.zone] += max(0, instance.free_cores_mcpu)

    def add_instance(self, instance):
        assert instance.name not in self.name_instance

        self.name_instance[instance.name] = instance
        self.adjust_for_add_instance(instance)

    async def call_delete_instance(self, instance, reason, timestamp=None, force=False):
        if instance.state == 'deleted' and not force:
            return
        if instance.state not in ('inactive', 'deleted'):
            await instance.deactivate(reason, timestamp)

        try:
            await self.compute_client.delete(f'/zones/{instance.zone}/instances/{instance.name}')
        except aiohttp.ClientResponseError as e:
            if e.status == 404:
                log.info(f'{instance} already delete done')
                await self.remove_instance(instance, reason, timestamp)
                return
            raise

    async def check_on_instance(self, instance):
        active_and_healthy = await instance.check_is_active_and_healthy()
        if active_and_healthy:
            return

        try:
            spec = await self.compute_client.get(f'/zones/{instance.zone}/instances/{instance.name}')
        except aiohttp.ClientResponseError as e:
            if e.status == 404:
                await self.remove_instance(instance, 'does_not_exist')
                return
            raise

        if (instance.state == 'active'
                and instance.failed_request_count > 5
                and time_msecs() - instance.last_updated > 5 * 60 * 1000):
            log.exception(f'deleting {instance} with {instance.failed_request_count} failed request counts after more than 5 minutes')
            await self.call_delete_instance(instance, 'not_responding')
            return

        # PROVISIONING, STAGING, RUNNING, STOPPING, TERMINATED
        gce_state = spec['status']

        log.info(f'{instance} gce_state {gce_state}')

        if (
            gce_state == 'PROVISIONING'
            and instance.state == 'pending'
            and time_msecs() - instance.time_created > 5 * 60 * 1000
        ):
            log.exception(f'{instance} did not provision within 5m after creation, deleting')
            await self.call_delete_instance(instance, 'activation_timeout')

        if gce_state in ('STOPPING', 'TERMINATED'):
            log.info(f'{instance} live but stopping or terminated, deactivating')
            await instance.deactivate('terminated')

        if gce_state in ('STAGING', 'RUNNING'):
            last_start_timestamp = spec.get('lastStartTimestamp')
            assert last_start_timestamp is not None, f'lastStartTimestamp does not exist {spec}'
            last_start_time_msecs = dateutil.parser.isoparse(last_start_timestamp).timestamp() * 1000

            if instance.state == 'pending' and time_msecs() - last_start_time_msecs > 5 * 60 * 1000:
                log.exception(f'{instance} did not activate within 5m after starting, deleting')
                await self.call_delete_instance(instance, 'activation_timeout')

        if instance.state == 'inactive':
            log.info(f'{instance} is inactive, deleting')
            await self.call_delete_instance(instance, 'inactive')

        await instance.update_timestamp()

    async def monitor_instances(self):
        if self.instances_by_last_updated:
            # [:50] are the fifty smallest (oldest)
            instances = self.instances_by_last_updated[:50]

            async def check(instance):
                since_last_updated = time_msecs() - instance.last_updated
                if since_last_updated > 60 * 1000:
                    log.info(f'checking on {instance}, last updated {since_last_updated / 1000}s ago')
                    await self.check_on_instance(instance)

            await asyncio.gather(*[check(instance) for instance in instances])

    async def monitor_instances_loop(self):
        await periodically_call(1, self.monitor_instances)


#“Given a set of n jobs with [start time, end time, cost] find a
#subset so that no 2 jobs overlap and the cost is maximum ?”

import itertools
job1 = (0,2,100)
job2 = (4,5,200)
job3 = (9,12,400)
job4 = (11,13,300)
job5 = (8,10,500)
listOfJobs = [job1, job2, job3, job4, job5]
bestList = []

def costMax(jobList):
    global bestList
    numJobs = len(jobList)
    maxCost = 0
    listOfJobs.sort()
    for i in range(1, numJobs+1):
        it = iter(itertools.combinations(listOfJobs, i))
        for element in it:
            #print(element)
            overlapFlag = 0
            for j in range(len(element)):
                if (j+1 < len(element)):
                    if (element[j][1] > element[j+1][0]):
                        #print('OVERLAP')
                        overlapFlag = 1
                        break
            if overlapFlag is 0:
                #print('NO OVERLAP')
                currentListCost = 0
                for k in range(len(element)):
                    currentListCost += element[k][2]
                if (currentListCost > maxCost):
                    maxCost = currentListCost
                    bestList = element
                    #print(bestList)
    return(maxCost)

finalAnswer = costMax(listOfJobs)
print('Highest Cost With No Overlap is: ', finalAnswer)
print('Best combination was: ', bestList)


import numpy, math
import scipy.fftpack as fftim
from PIL import Image
import cv2

# Opening the image and converting it to grayscale.
b = Image.open('../Figures/fft1.png').convert('L')
 # Performing FFT.
c = fftim.fft2(b)
# Shifting the Fourier frequency image.
d = fftim.fftshift(c)
# Intializing variables for convolution function.
M = d.shape[0]
N = d.shape[1]
# H is defined and
# values in H are initialized to 1.
H = numpy.ones((M,N))
center1 = M/2
center2 = N/2
d_0 = 30.0 # cut-off radius
t1 = 1 # the order of BLPF
t2 = 2*t1

# Defining the convolution function for BLPF.
for i in range(1,M):
    for j in range(1,N):
        r1 = (i-center1)**2+(j-center2)**2
        # Euclidean distance from
        # origin is computed.
        r = math.sqrt(r1)
        # Using cut-off radius to
        # eliminate high frequency.
        if r > d_0:
            H[i,j] = 1/(1 + (r/d_0)**t1)

# Converting H to an image
H = Image.fromarray(H)
# Performing the convolution.
con = d * H
# Computing the magnitude of the inverse FFT.
e = abs(fftim.ifft2(con))
# Saving e.
cv2.imwrite('../Figures/blowpass_output.png', e)


import sys
from bs4 import BeautifulSoup
import requests
import pprint
sys.path.append("../")
from summarize import getSummary

import datetime
def parseTime(time):
    return time
headers = {
    "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "User-Agent" : "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0",
}

def nytimes(url):
    page = requests.get(url, headers=headers)
    soup = BeautifulSoup(page.content, 'html.parser')
    try:
        text = soup.find_all("section")[2].text
    except:
        return{}
    try:
        image = soup.find(class_ = "css-79elbk").find("img")["src"]
    except:
        image = None
    headline = soup.find("h1").text
    time = None
    try:
        headline = soup.find("h1").text
    except:
        return{}
    category = "entertainment"
    content = text
    summary, keywords = getSummary(content)
    article = {
        "headline": headline,
        "time": time,
        "category": category,
        "url": url,
        "source": "Times Of India",
        "image_url": image,
        "body":summary,
        "keywords": keywords
    }

    return article

    # Object Model:
    # headline --
    # time(in datetime format) --
    # keywords(returned from summarizer.py)
    # category(from scraping e.g. finance, tech etc.) --
    # body(summary returned from summarizer)
    # url(url to the full article) --
    # source(name of media channel e.g times of india, indianExpress etc.) --
    # image_url(url for article image, if cannot find one then give some boilerplate image from net corresponding to category) --
    # randomly generated id for object(mongo creates one on its own, idk about firebase, please see that..)
    # any other field you can think of
    # 9 including id


# A test for likelihood ration methods to minimize the object E[r(X)]
# where r(x) =
# r.v. X has a distribution of the form exp(-(x-theta_0)^2/theta_1)

import numpy as np
import matplotlib.pyplot as plt
import copy

N = 1000

class GaussianPolicy:
    def __init__(self):
        self.theta = np.ones(2)

    def generator(self):
        return np.random.normal(self.theta[0], self.theta[1])

    def __call__(self, x):
        return (1 / np.sqrt(2 * np.pi * self.theta[1] ** 2)) * np.exp(
            -(x - self.theta[0]) ** 2 / (2 * self.theta[1] ** 2))

    def derivative(self, x):
        a = 1 / (self.theta[1] * np.sqrt(2 * np.pi))
        nabula_theta = np.zeros(2)
        gp_value = a * np.exp(-(x - self.theta[0]) ** 2 / (2 * self.theta[1] ** 2))
        nabula_theta[0] = (x - self.theta[0]) * gp_value / (self.theta[1] ** 2)
        nabula_theta[1] = gp_value * (self.theta[0] ** 2 - 2 *
                                      self.theta[0] * x - self.theta[1] ** 2 + x ** 2) / (self.theta[1] ** 3)
        return nabula_theta

    def update_theta(self, nabula_theta, step_size):
        self.theta += step_size * nabula_theta
        if self.theta[1] < 0:
            self.theta[1] = -self.theta[1]

def reward(x):
    # theta_0 = 2
    # theta_1 = 3
    return (6. / (3 * np.sqrt(2 * np.pi))) * np.exp(-(x - 2) ** 2 / 18)

def likelihood_ratio(repeat_time):
    log_theta = []
    log_reward = []
    gp = GaussianPolicy()
    learning_rate = 1
    learning_rate_decay = 0.95
    for i in range(1, repeat_time):
        nabula_q = np.zeros(2)
        reward_i = 0
        for _ in range(N):
            x = gp.generator()
            r = reward(x)
            reward_i += r
            nabula_q += r * gp.derivative(x) / gp(x)
        nabula_q /= N
        reward_i /= N
        gp.update_theta(nabula_q, learning_rate)
        learning_rate *= learning_rate_decay
        log_reward.append(reward_i)
        log_theta.append(copy.deepcopy(gp.theta))
    plt.figure(0)
    log_theta = np.array(log_theta)
    plt.plot(log_theta[:, 0], label='$\mu$')
    plt.plot(log_theta[:, 1], label='$\delta$')
    plt.legend()
    plt.figure(1)
    plt.plot(log_reward, label='reward')
    plt.legend()
    plt.show()

if __name__ == '__main__':
    likelihood_ratio(500)
    print('finish')


from django import template
from django.utils.safestring import mark_safe

register = template.Library()

@register.filter
def searchChild(comment):
    child_comment_list=comment.child_comment.filter(cmt_to_parentCmt=comment.cmt_id)
    if child_comment_list:
        html_str=["
"]
        for child_comment in child_comment_list:
            html_str.append('''

                        %s
                        %s
                        %s

            %s

            '''%(child_comment.cmt_user.um.user_pic_url,
                 child_comment.cmt_user.username,
                 child_comment.cmt_content,
                 child_comment.cmt_pub_date,
                 searchChild(child_comment)))

        html_str.append("
")
    else:
        html_str=[]

    return "".join(html_str)

from repository import models
class DiggRet(object):
    delta=None
    count=None
@register.filter
def getDiggCount(news_id,user):
    '''
    获取点赞数
    :param news_id:
    :return:
    '''
    ret_obj = DiggRet()
    if user:
        user_id = user["uid"]
        mod_ret = models.Digg_User_News.objects.filter(digg_user_fk_id=user_id, digg_news_fk_id=news_id)
        if mod_ret.count():
            ret_obj.delta = "-1"
        else:
            ret_obj.delta = "+1"
    else:
        ret_obj.delta = "+1"
    news_obj=models.News.objects.get(news_id=news_id)
    ret_obj.count=news_obj.nTod.all().count()
    return ret_obj

@register.filter
def getCommentCount(news_id):
    '''获取评论数'''

    news_obj = models.News.objects.get(news_id=news_id)
    count=news_obj.cmt_to_news.all().count()
    return count


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

import sys
import os
import visa
import time as tm
import numpy as np
import matplotlib.pyplot as plot

sys.path.append('..')

from modules.labdev import GenComm, ScopeComm
from modules.wafo import WaveForm as wf # prbs, sin, square

def get_samples(samples):
    # 16k, 32k, 52k
    if samples == '32k':
        return 2**15
    elif samples == '512k':
        return 2**19
    else:
        return 2**14 # 16k

if __name__ == '__main__':

    addr_s = "10.0.4.30"
    addr_g = "10.0.4.48"

    # physical konstant
    c = 3e8    #  [m/s]; light speed
    vf = 0.695 #  [-] velocity factor
    order = 11

    # generation parameters
    bitrate = ((2**order)-1)*8e3     # [bit/s]
    samples = get_samples('512k') # [Sa/s]
    amp = 0.5                      # [Volts]
    fsmp = 125e6                   # [Sa/s]

    # devices init values
    # bw = 'FULL' # on RTE 1024 maximum bandwidth 200 MHz
    bw = 'B20'
    imp = 50      # [ohm]
    trig = amp/4  # [V]
    name = 'stdr' # signal name

    # prbs signal generation
    prbs = wf.generate_prbs_sequence(1, order)
    # prbs = [1, -1, 1, -1, 1]

    freq = bitrate/(len(prbs))
    print('Frequency: ' + str(freq))
    if(freq >= 8e6):
        print("Calculated frequency is too high.")
        sys.exit()

    signal = wf.get_bit_signal(prbs, samples, bitrate)
    size = len(signal) # size is reduced number of samples

    # DEBUG
    # plot.plot(test_data)
    # plot.show()

    # connect to devices
    generator = GenComm(addr_g)
    oscilloscope = ScopeComm(addr_s)

    # TODO input your custom names of reference and reflected data file
    def save_data(data1, data2):
        save_path = "data"
        print("Do you want to save reference and reflected signal data?")
        if(input('') == 'y'):
            print('write name of the reference data file with: ')
            reference = input()
            reference =  os.path.join(save_path, reference + ".txt")
            print('write name of the reflected data file with: ')
            reflected = input()
            reflected =  os.path.join(save_path, reflected + ".txt")
            np.savetxt(reference, data1, '%f')
            np.savetxt(reflected, data2, '%f')

    def wait_for_key():
        key = input('')
        if (key == ''):
            pass
        if (key == 'q'):
            sys.exit()

    # connect to devices
    generator = GenComm(addr_g)
    oscilloscope = ScopeComm(addr_s)

    # translate data for generator
    data = generator.prepare_wave_data(signal)
    # set signal on generator
    generator.send_data_and_set('1', freq, str(amp), '0', '0', name, data)
    # enable external clock
    generator.enable_external_osc()
    # dds mode
    generator.set_dds('1')
    generator.enable_sync_signal('2', freq)
    # load custom signal with particular name
    generator.activate_custom_signal('1', name)
    # enable sync signal on second channel
    generator.enable_output('1', str(imp))

    # set osciloscop initial values
    oscilloscope.setup(fsmp, bw, freq, amp + 0.6, imp, trig)
    # check if everything setup
    oscilloscope.check_setup()
    # check if everything was setup right
    oscilloscope.get_error_list()

    print("Connect load (press Enter to continue).")
    wait_for_key()
    reference = oscilloscope.get_wave_data() # get reference signal
    # oscilloscope.change_h_scale(2/freq)

    while(1):
        print("Connect cable and press Enter to continue.")
        wait_for_key()
        reflected = oscilloscope.get_wave_data()

        size_adc = len(reference)
        # reflected = reflected[:size_adc]

        # correlation
        print("Correlation ...")
        c0 = np.correlate(reflected, reference, "full")
        bound = size_adc - 1

        # resol = 0.5*vf*c*period
        resol = float(oscilloscope.resol)*vf*c*0.5
        t = np.linspace(0, bound, size_adc)*resol

        # correlation time domain
        t1 = np.linspace(-bound, bound, 2*size_adc-1)*resol

        p0_value = c0.max()
        p0_index = np.argmax(c0)
        d0 = t1[p0_index]
        print('Peak0 value: ' + str(p0_value))
        print('Peak0 distance: ' + str(d0))

        # second peak
        tmp = reflected - reference
        c1 = np.correlate(tmp, reference, "full")
        plot.show()

        p1_value = c1.max()
        p1_index = np.argmax(c1)
        d1 = t1[p1_index]
        print('Peak1 value: ' + str(p1_value))
        print('Peak1 distance: ' + str(d1))

        dt = (d1)
        print("Length of cabel is: " + str(dt) + " [m]")

        plot.subplot(3, 1, 1)
        plot.plot(t, reference)
        plot.subplot(3, 1, 2)
        plot.plot(t, reflected)
        plot.subplot(3, 1, 3)
        plot.plot(t1, c1)
        plot.show(block=True)

        # want you to save data?
        save_data(reference, reflected)

        print("Press Enter to continue or q to quit.")
        wait_for_key()

    oscilloscope.close()
    generator.close()


#!/usr/bin/python
#
#  Delete the label image from an Aperio SVS file.
#
#  Original by CMU, modified Caleb Grenko
#  cagrenko@davidson.edu
#
#  (i/o,  porting to Python 3)
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as
#  published by the Free Software Foundation, version 2.1.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this program. If not, see
#  <http://www.gnu.org/licenses/>.
#

import string
import struct
import sys
import os
import argparse

ASCII = 2
SHORT = 3
LONG = 4
LONG8 = 16

IMAGE_DESCRIPTION = 270
STRIP_OFFSETS = 273
STRIP_BYTE_COUNTS = 279

class TiffFile:
    def __init__(self, path):
        self.file = open(path, 'r+b')

        # Check header, decide endianness
        endian = self.file.read(2)
        if endian == 'II':
            self._fmt_prefix = '<'
        elif endian == 'MM':
            self._fmt_prefix = '>'
        else:
            raise IOError('Not a TIFF file')

        # Check TIFF version
        self._bigtiff = False
        version = self.read_fmt('H')
        if version == 42:
            pass
        elif version == 43:
            self._bigtiff = True
            magic2, reserved = self.read_fmt('HH')
            if magic2 != 8 or reserved != 0:
                raise IOError('Bad BigTIFF header')
        else:
            raise IOError('Not a TIFF file')

        # Read directories
        self.directories = []
        while True:
            in_pointer_offset = self.file.tell()
            directory_offset = self.read_fmt('Z')
            if directory_offset == 0:
                break
            self.file.seek(directory_offset)
            self.directories.append(TiffDirectory(self, in_pointer_offset))

    def _convert_format(self, fmt):
        # Format strings can have special characters:
        # y: 16-bit   signed on little TIFF, 64-bit   signed on BigTIFF
        # Y: 16-bit unsigned on little TIFF, 64-bit unsigned on BigTIFF
        # z: 32-bit   signed on little TIFF, 64-bit   signed on BigTIFF
        # Z: 32-bit unsigned on little TIFF, 64-bit unsigned on BigTIFF
        if self._bigtiff:
            fmt = fmt.translate(str.maketrans('yYzZ', 'qQqQ'))
        else:
            fmt = fmt.translate(str.maketrans('yYzZ', 'hHiI'))
        return self._fmt_prefix + fmt

    def fmt_size(self, fmt):
        return struct.calcsize(self._convert_format(fmt))

    def read_fmt(self, fmt, force_list=False):
        fmt = self._convert_format(fmt)
        vals = struct.unpack(fmt, self.file.read(struct.calcsize(fmt)))
        if len(vals) == 1 and not force_list:
            return vals[0]
        else:
            return vals

    def write_fmt(self, fmt, *args):
        fmt = self._convert_format(fmt)
        self.file.write(struct.pack(fmt, *args))

class TiffDirectory(object):
    def __init__(self, fh, in_pointer_offset):
        self.in_pointer_offset = in_pointer_offset
        self.entries = {}
        count = fh.read_fmt('Y')
        for _ in range(count):
            entry = TiffEntry(fh)
            self.entries[entry.tag] = entry
        self.out_pointer_offset = fh.tell()

class TiffEntry(object):
    def __init__(self, fh):
        self.start = fh.tell()
        self.tag, self.type, self.count, self.value_offset = \
                fh.read_fmt('HHZZ')
        self._fh = fh

    def value(self):
        if self.type == ASCII:
            item_fmt = 'c'
        elif self.type == SHORT:
            item_fmt = 'H'
        elif self.type == LONG:
            item_fmt = 'I'
        elif self.type == LONG8:
            item_fmt = 'Q'
        else:
            raise ValueError('Unsupported type')

        fmt = '%d%s' % (self.count, item_fmt)
        len = self._fh.fmt_size(fmt)
        if len <= self._fh.fmt_size('Z'):
            # Inline value
            self._fh.seek(self.start + self._fh.fmt_size('HHZ'))
        else:
            # Out-of-line value
            self._fh.seek(self.value_offset)
        items = self._fh.read_fmt(fmt, force_list=True)
        if self.type == ASCII:
            if items[-1] != '\0':
                raise ValueError('String not null-terminated')
            return ''.join(items[:-1])
        else:
            return items

def delete_aperio_label(filename):
    with TiffFile(filename) as fh:
        print("get")
        for directory in fh.directories:
            print(directory)
            # Check ImageDescription
            try:
                desc = directory.entries[IMAGE_DESCRIPTION].value()
            except KeyError:
                continue
            if not desc.startswith('Aperio'):
                # Not an Aperio directory
                continue
            lines = desc.splitlines()
            if len(lines) < 2 or not lines[1].startswith('label '):
                # Not the label
                continue

            # Get strip offsets/lengths
            try:
                offsets = directory.entries[STRIP_OFFSETS].value()
                lengths = directory.entries[STRIP_BYTE_COUNTS].value()
            except KeyError:
                raise IOError('Label is not stripped')

            # Wipe strips
            for offset, length in zip(offsets, lengths):
                fh.seek(offset)
                fh.write('\0' * length)

            # Delete directory
            fh.seek(directory.out_pointer_offset)
            out_pointer = fh.read_fmt('Z')
            fh.seek(directory.in_pointer_offset)
            fh.write_fmt('Z', out_pointer)

            # Done
            break
        else:
            raise IOError("Couldn't find Aperio label directory")

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument(
        "-manifest",
        "-m",
        help="path to a .csv containing a single column of paths to .svs files",
        type=str,
        default=None
    )

    parser.add_argument(
        "-folder",
        "-f",
        help="File/folder containing .svs files",
        type=str,
        default=None
    )

    if len(sys.argv) <= 1:
        print("You need some arguments")
        print("Either enter a manifest of file paths (-m), or point to a folder (-f) containing all slides you want anonymized")
        print("Alternatively, list all .svs files sequentally")
        print("... I feel like this is too many options")
        exit(1)

    paths = []
    if str(sys.argv[1]) is not None and str(sys.argv[1]).endswith(".svs"):
        for arg in sys.argv[1:]:
            if str(arg.endswith(".svs")):
                paths.append(arg)

    args = parser.parse_args()
    if args.folder is not None and args.manifest is not None:
        print("Please enter only a manifest or a folder, not both")
        exit(1)

    if args.folder is not None:
        if not str(args.folder).endswith("/"):
            args.folder += "/"
        for path in os.listdir(args.folder):
            if path.endswith(".svs"):
                paths.append(args.folder + path)

    elif args.manifest is not None:
        with open(args.manifest, "r") as manifest:
            for line in manifest:
                if line.strip().endswith(".svs"):
                    paths.append(line.strip)

    else:
        print("You need some arguments")
        print("Either enter a manifest of file paths (-m), or point to a folder (-f) containing all slides you want anonymized")
        print("Alternatively, list all .svs files sequentally")
        print("... I feel like this is too many options")
        exit(1)

    exit_code = 0
    for filename in paths:
        print(filename)
        try:
            delete_aperio_label(filename)
        except Exception as e:
            # print('%s: %s' % (filename, str(e)), file=sys.stderr)
            exit_code = 1

        sys.exit(exit_code)


from urllib.request import urlopen
import re
from bs4 import BeautifulSoup

class WebsiteParser:

    def manage_websites(self, websites):
        websites_info = []
        for website in websites:
            single_website_info = self.get_html(website)
            websites_info.append(single_website_info)
        return websites_info

    def get_html(self, website):
        website_data = {}

        if website == 'localhost':
            website = 'localhost:8000/localhost_template.html'

        # checking the link structure
        m = re.match(r"http.:", website)
        if not m:
            website = "http://%s" % website
        # get html code of the website
        html = urlopen(website)

        tag_num = self.soup_read(html)
        website_data['address'] = website
        website_data['tag_num'] = tag_num
        return website_data

        # print(html.read().decode('utf-8'))

    def soup_read(self, html):
        soup = BeautifulSoup(html.read(), "html.parser")

        # tag list

        tag_list = soup.body.find_all('button')

        regex = re.compile('submit|reset|button')
        tag_list2 = soup.body.find_all('input', {'type': regex})

        regex2 = re.compile('(?i).*btn.*|(?i).*button.*')
        tag_list3 = soup.body.find_all('', {'class': regex2})

        # all tags to one list, make it set in order to eliminate duplicates
        all_tags = set(tag_list + tag_list2 + tag_list3)

        # print(soup.body.find_all('', {'class': regex2}))
        all_button_num = len(all_tags)
        return all_button_num