import pygame
import sys
from pygame.locals import *
from snake.apple import Apple
from snake.snake import Snake
from snake.map import Map

class App:
    def __init__(self, map_file, collector, controller):
        self._running = True
        self._display_surf = None
        self.size = self.weight, self.height = 600, 640
        self._map_file = map_file
        self._collector = collector
        self._controller = controller

    def on_init(self):
        pygame.init()
        self._display_surf = pygame.display.set_mode(self.size)
        pygame.display.set_caption('Snake')
        self._running = True

        # Font
        self._font = pygame.font.SysFont('Arial', 20)

        # Clock
        self._clock = pygame.time.Clock()

        self._map = Map(self._map_file)
        self._map.on_init()

        self._snake = Snake(self.size, self._controller)
        self._snake.on_init()

        self._apple = Apple()
        self._apple.on_init()
        self._apple.generate_new_pos(self._map)

    def on_event(self):
        for event in pygame.event.get(pygame.QUIT):
            self._running = False
        self._snake.on_event()

        pygame.event.clear()

    def on_loop(self):
        self._snake.on_loop()

        if(self._snake.hit_himself() or self._snake.hit_wall(self._map)):
            self.on_die()
            self._running = False
            return

        snake_xs, snake_ys = self._snake.get_head_pos()
        if collide(snake_xs, self._apple.applepos[0], snake_ys, self._apple.applepos[1], 20, 20, 20, 20):
            self._apple.generate_new_pos(self._map)
            self._snake.on_collision()

        self._collector.collect(self._snake, self._apple, self._map)

    def on_render(self):
        self._display_surf.fill((255, 255, 255))

        self._map.on_render(self._display_surf)
        self._snake.on_render(self._display_surf)
        self._apple.on_render(self._display_surf)

        t = self._font.render(str(self._snake.get_score()), True, (0, 0, 0))
        self._display_surf.blit(t, (10, 10))
        pygame.display.update()

    def on_cleanup(self):
        self._collector.save()
        pygame.quit()

    def on_execute(self):
        if self.on_init() == False:
            self._running = False
        while(self._running):
            self._clock.tick(10)
            self.on_event()
            self.on_loop()
            self.on_render()
        self.on_cleanup()

    def on_die(self):
        f = pygame.font.SysFont('Arial', 30)
        t = f.render('Your score was: ' +
                     str(self._snake.get_score()), True, (0, 0, 0))
        self._display_surf.blit(t, (10, 270))
        pygame.display.update()
        pygame.time.wait(2000)

def collide(x1, x2, y1, y2, w1, w2, h1, h2):
    if x1+w1 > x2 and x1 < x2+w2 and y1+h1 > y2 and y1 < y2+h2:
        return True
    else:
        return False


from __future__ import division
from shutil import rmtree
import subprocess
import time
import string
import numpy as np
from sklearn.metrics import fbeta_score
from skimage.io import imread
from skimage import color
from scipy.misc import imresize
import os
import math
from keras import backend as K

from bisect import bisect
from random import random
from constants import LABELS, WEATHER_IDX, DATA_DIR, ORIGINAL_LABEL_FILE, CHANNELS, IMG_ROWS, IMG_COLS, IMG_SCALE, TRAIN_DATA_DIR

def get_uniq_name():
	"""
	A uniq name to diffenrenciate generated files
	"""
	gitHash = subprocess.check_output(["git", "describe", "--always"]).strip().decode("utf-8")
	gitMessage =  subprocess.check_output(["git", "log", "--format=%B", "-n", "1", "HEAD"]).strip().decode("utf-8")
	# remove bad filename characters: http://stackoverflow.com/a/295146/2054629
	valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
	gitMessage = "".join(c for c in gitMessage if c in valid_chars)

	t = time.strftime("%Y-%m-%d.%H:%M:%S")
	return "{t}-{h}-{m}".format(t=t, h=gitHash, m=gitMessage).lower().replace(" ", "_")

def get_predictions(y, labels, thresholds=None):
	"""
	return the label predictions for an input of shape (n, labelCount)
	"""
	if thresholds is None:
		thresholds = np.array([0.2]*len(labels))
	labels_idx = [i for i, l in enumerate(LABELS) if l in labels]
	weather_idx = [i for i in labels_idx if i in WEATHER_IDX]

	def row_pred(row):
		"""
		Make prediction on a row
		"""
		weather = [weather_idx[np.argmax(row[weather_idx])]] if len(weather_idx) > 1 else []
		tags = [i for i, v in enumerate(row) if i not in weather_idx and v > thresholds[i]]
		return [LABELS[k] for k in weather + tags]

	return (row_pred(row) for row in y)

# from https://www.kaggle.com/c/planet-understanding-the-amazon-from-space/discussion/32475
def optimise_f2_thresholds(y, p, verbose=True, resolution=100):
	"""
	find optimal thresholds to predict labels
	- Parameter y: a (n) array of 0 - 1 representing true values
	- Parameter p: a (n, n_labels) matrix of probabilies
	"""
	n_labels = p.shape[1]
	def mf(x):
		p2 = np.zeros_like(p)
		for i in range(n_labels):
			p2[:, i] = (p[:, i] > x[i]).astype(np.int)
		score = fbeta_score(y, p2, beta=2, average="samples")
		return score

	x = [0.2] * n_labels
	for i in range(n_labels):
		best_i2 = 0
		best_score = 0
		for i2 in range(resolution):
			i2 /= resolution
			x[i] = i2
			score = mf(x)
			if score > best_score:
				best_i2 = i2
				best_score = score
		x[i] = best_i2

	return x

def get_labels_dict():
	"""
	Return a dictionary of { image_name: [0-1 tag] }
	"""
	labels_dict = {}
	with open(ORIGINAL_LABEL_FILE) as f:
		f.readline()
		for l in f:
			filename, rawTags = l.strip().split(",")
			tags = rawTags.split(" ")
			bool_tags = np.array([1 if tag in tags else 0 for tag in LABELS])

			# some rows are missing a label tag...
			has_weather_tag = False
			for i in WEATHER_IDX:
				if bool_tags[i] == 1:
					has_weather_tag = True
					break
			if not has_weather_tag:
				bool_tags[LABELS.index("clear")] = 1

			file = filename.split("/")[-1].split(".")[0]
			labels_dict[file] = bool_tags
	return labels_dict

def get_inputs_shape():
	"""
	Return the image format, and the share images should have
	"""
	image_data_fmt = K.image_data_format()
	if image_data_fmt == "channels_first":
		input_shape = (CHANNELS, int(IMG_ROWS * IMG_SCALE), int(IMG_COLS * IMG_SCALE))
	else:
		input_shape = (int(IMG_ROWS * IMG_SCALE), int(IMG_COLS * IMG_SCALE), CHANNELS)
	img_size = (int(IMG_ROWS * IMG_SCALE), int(IMG_COLS * IMG_SCALE))
	return image_data_fmt, input_shape, img_size

def get_resized_image(f, data_dir, image_data_fmt, input_shape):
	"""
	Read the image from file and return it in the expected size
	"""
	img = imread(os.path.join(data_dir, "{}.jpg".format(f)))
	img = color.rgb2hsv(img)
	if image_data_fmt == "channels_first":
		img = img.reshape((CHANNELS, IMG_ROWS, IMG_COLS))
	return imresize(img, input_shape)

# deprecated
def get_generated_images(originalImageFileName, ext="jpg"):
	"""
	return an array of images generated from an original image.
	originalImageFileName should not contain extension nor directory path
	"""
	for i in range(8):
		newFileName = "{n}--{i}.{ext}".format(n=originalImageFileName, i=i, ext=ext)
		yield os.path.join(DATA_DIR, newFileName)

def files_proba(file_labels, labels):
	# file_labels is a dict of (img -> binary vector of labels)
	train_tags = file_labels

	count_labels = np.array([0] * len(labels))
	for img in train_tags:
		count_labels += train_tags[img]
	n_doc = len(train_tags)
	# idf is a vector representing the idf of each tag
	# We've modified this idf by removing the log here, which leads
	# to more balanced sampling
	idf = np.array([n_doc / (1 + count_labels[i]) for i, _ in enumerate(labels)])

	tf_idf = {}
	for img in train_tags:
		# We use train_tags[img] as a mask over the idf of each tag and sum
		tf_idf[img] = (np.array(train_tags[img]) * idf).sum()

	sum_tf = sum(tf_idf.values())
	proba = {img: tf_idf[img] / sum_tf for img in tf_idf}
	return proba

def files_and_cdf_from_proba(proba):
	files_probs = sorted(proba.items(), key=lambda i: i[1])
	return list(map(lambda i: i[0], files_probs)), np.cumsum(list(map(lambda i: i[1], files_probs)))

def pick(n, files, cdf):
	return [files[bisect(cdf, random())] for i in range(n)]

def remove(path):
	"""
	Remove a file or directory if it exists, else do nothing
	"""
	try:
		if os.path.isdir(path):
			rmtree(path)
		else:
			os.remove(path)
	except FileNotFoundError:
		pass

def F2Score(predicted, actual):
	# see https://www.kaggle.com/c/planet-understanding-the-amazon-from-space#evaluation
	predicted = set(predicted)
	actual = set(actual)
	tp = len(predicted & actual)
	tn = len(LABELS) - len(predicted | actual)
	fp = len(predicted) - tp
	fn = (len(LABELS) - len(predicted)) - tn
	p = tp / (tp + fp)
	r = tp / (tp + fn)
	if p == 0 or r == 0:
		return 0
	b = 2
	return (1 + b**2) * p * r / (b**2*p + r)


from common.listnode import ListNode

class Solution(object):
    def mergeTwoLists(self, list1, list2):
        """
        :type list1: ListNode
        :type list2: ListNode
        :rtype: ListNode
        """
        out = ListNode(0)
        current = out
        while list1 is not None or list2 is not None:
            if list1 is None or list2 is not None and list1.val > list2.val:
                current.next = ListNode(list2.val)
                list2 = list2.next
            else:
                current.next = ListNode(list1.val)
                list1 = list1.next
            current = current.next
        return out.next


from source.main.utils import stopwords as st

import re
import pickle
import warnings
import numpy as np
import pandas as pd
from glob import glob
from tika import parser
from collections import Counter
import matplotlib.pyplot as plt
import logging

from nltk.stem.snowball import SnowballStemmer
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import confusion_matrix, f1_score, accuracy_score

warnings.filterwarnings('ignore')

class Train :
    '''
    This class is used to train the resumes.
    '''
    def __init__(self,path, model_path) :

        self.path = path
        self.model_path = model_path
        self.count_vect = CountVectorizer()
        self.le = LabelEncoder()
        self.df = pd.DataFrame()
        self.scaler = StandardScaler()

    def preprocess_resume(self,path) :
        '''
        This method performs the pre-processing of the resumes,
        getting the text out of resume, getting the common words and its count,
        getting the stop word count.

        @param path :: string: Contains the path to resumes file.

        @return:
            txt :: string: Extracted text from resume.
            common_word :: string: List of common word present in text.
            common_count :: int: Count of common words
            stop_count :: int: Count of stopwords
        '''

        try:
            txt = parser.from_file(path)['content'].lower().strip()
            txt = re.sub(st.PREPROCESS_STRING, " ", txt)
            txt_list = re.sub(st.SPACE_STRING, " ", txt).split(' ')
            stop_count = len(set(txt_list) & st.STOP )
            txt_list = [ word for word in txt_list if word not in st.STOP ]
            common_words = ' '.join(list(zip(*Counter(txt_list).most_common(10)))[0])
            common_count = Counter(txt_list).most_common(1)[0][1]
            txt = ' '.join(txt_list)
        except:
            txt = ''
            common_words = ''
            common_count = 0
            stop_count = 0

        return txt, common_words, common_count, stop_count

    def extract_resume_content(self) :

        '''
        This method adds the following features:
            :path :: string: path to resumes - Path to resumes is passed by the user in config file.
            :resume_name :: string: File name
            :resume_type :: string: Domain

        '''

        self.df['path'] = glob(self.path + '*/**/*')
        self.df['resume_name'] = self.df['path'].astype(str).str.split('/').str[-1]
        self.df['resume_type'] = self.df['path'].astype(str).str.split('/').str[-2]
        self.df['resume'], self.df['common_words'], self.df['common_count'], self.df['stop_count'] = zip(*self.df['path'].apply(self.preprocess_resume))

    def add_features(self) :

        '''
        This method adds some more feature for model training:
            :word_count :: int: Count of each word
            :character_count :: int: Count of each character
            :avg_word_size :: int: Average word count
        '''

        self.df['word_count'] = self.df['resume'].astype(str).str.split().str.len()
        self.df['character_count'] = self.df['resume'].astype(str).str.len()
        self.df['avg_word_size'] = self.df['character_count']/self.df['word_count']

    def train_model(self) :
        '''
        This method will train the model using Logistic regression.

        '''

        self.df.drop_duplicates(subset='resume',inplace=True)
        self.add_features()
        self.save_data('resume_dataframe',self.df)
        self.df['resume_type'] = self.le.fit_transform(self.df['resume_type'])                  # data leakage.

        X = self.df.drop('resume_type', axis = 1)
        y = self.df['resume_type']

        self.save_encoder('classes')

        x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 123)

        self.x_test = x_test
        self.y_test = y_test

        x_train_count = self.count_vect.fit_transform(x_train['resume'], y = y_train)

        self.save_data('count_vect',self.count_vect)

        clf = LogisticRegression(random_state = 123, class_weight = 'balanced')

        clf.fit(x_train_count, y_train)

        self.clf = clf
        self.save_data('latest_model', clf)

    def test_model(self) :

        '''
        This method test the model accuracy on unseen test data and gives the following metrics
            :Accuracy
            :F1 Score
            :Confusion matix
        '''

        x_test_count = self.count_vect.transform(self.x_test['resume'])

        y_pred = self.clf.predict(x_test_count)

        print('the accuracy score of model is '+ str(round((accuracy_score(self.y_test, y_pred)) * 100, 2)))
        print('the f1 score of model is '+ str(round((f1_score(self.y_test, y_pred, average = 'macro')) * 100, 2)))
        print('Confusion matix '+ str(confusion_matrix(self.y_test, y_pred)))

    def save_data(self, filename, data) :

        '''
        This method is used to save the object passed using pickle.

        @param filename :: string: Name of object to be saved.
        @param data :: object: Object to be saved.
        '''

        filename = self.model_path + filename+'.pkl'
        pickle.dump(data, open(filename, 'wb'))

    def save_encoder(self, filename):

        '''
        This method saves the encoder using numpy.

        '''

        np.save(self.model_path + filename +'.npy', self.le.classes_)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from __future__ import unicode_literals, print_function, division

from yara import compile
import os

class YaraCheck(object):
    def __init__(self, rule_path):
        super(YaraCheck, self).__init__()
        self.Rules = self._setRules(rule_path)

    def _setRules(self, path_):
        yaraRule = compile(path_)
        return yaraRule

    def scan(self, file_info, is_path):
        if is_path:
            with open(file_info,"rb") as fin:
                bdata = fin.read()
            matches = self.Rules.match(data=bdata)
        else:
            matches = self.Rules.match(data=file_info)
        # for i in matches:
        #    print(i.rule, i.tags)
        return [i.rule for i in matches]

def check_crypto_by_static(yc, file_info, is_path=True):
    '''基于特征码的加密识别
    :param yc: YaraCheck类实例
    :param file_info: PE文件路径或PE文件数据
	:param is_path: bool，指明file_info是PE文件路径还是PE文件数据，默认True为文件路径
    :return: 成功True, list；失败False, string
    '''
    try:
        results = yc.scan(file_info, is_path)
        return True, results
    except Exception as e:
        return False, str(e)

if __name__ == '__main__':
    # 初始化YaraChek类，给出参数yara_rules地址
    yara_rules_path = "../rules/crypto_index.yar"
    yc = YaraCheck(rule_path=yara_rules_path)

    # 方法一：调用识别函数，用文件路径
    #check_suss, results = check_crypto_by_static(yc, "./reverse1_final.exe")

    # 方法二：调用识别函数，用文件数据
    for root, dirs, files in os.walk("../1_2000_black/"):
        # root 表示当前正在访问的文件夹路径
        # dirs 表示该文件夹下的子目录名list
        # files 表示该文件夹下的文件list

        # 遍历文件

        for file_name in files:
          file_path = os.path.join(root,file_name)
          with open(file_path, 'rb') as fin:
            bdata = fin.read()

          check_suss, results = check_crypto_by_static(yc, bdata, False)

          # 检查结果
          if check_suss:
             print(results)
          else:
             print(check_suss,results)


from tdigest import TDigest

def calculateQuantiles(col, min_tdigest_items):
    digest = TDigest()

    for i in range(min_tdigest_items):
     digest.update(col[i])

    quantiles = []
    for i in range(len(col)):
        val = col[i]
        c = digest.cdf(val)
        quantiles.append(c)
        if i >= min_tdigest_items:
            digest.update(val)

    return quantiles

def calculateQuantilesForDf(df, min_tdigest_items, cols_to_skip):
    cols = list(df.columns)
    cols_to_process = [x for x in cols if (x in cols_to_skip) == False]
    for x in cols_to_process:
        print("Processing " + x)
        df[x + "_q"] = calculateQuantiles(df[x], min_tdigest_items)


import sys
from subprocess import call
import argparse

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("-s", "--sketch", dest="sketch", required=True)
    parser.add_argument("-r", "--reads", dest="reads", required=True)

    return parser.parse_args()

class frec:
    def __init__(self):
        self.name = ""
        self.seq = ""
        self.qual = ""
        self.anno = ""

    def stringify(self):
        return "\n".join([self.name, self.seq, self.anno, self.seq])

if __name__ == "__main__":
    args = parse_args()

    sketch = args.sketch

    f_dict = {}
    rec = None
    with open(args.reads, "r") as ifi:
        for line in ifi:
            if line.startswith("@"):
                rec = frec()
                rec.name = line.strip()
            elif line.startswith("+"):
                rec.anno = line.strip()
            elif line.startswith("A") or line.startswith("G") or line.startswith("T") or line.startswith("C"):
                rec.seq = line.strip()
            else:
                rec.qual = line.strip()
                f_dict[rec.name] = rec

    count = 0

    with open("mashed.pred.txt", "w") as mfi:
        for rec in f_dict:
            with open(rec.strip("@") + ".tmp", "w") as ofi:
                ofi.write(f_dict[rec].stringify())
            run_str = "../bin/mash dist -i " + sketch + " " + rec.strip("@") + "| sort -n -k3 | head -n 1 >> mashed.pred.txt"
            call(run_str, shell=True)
            count += 1
            if count % 10 == 0:
                sys.stderr.write("processed %i reads" % count)


#This test only tries connecting to the twitter API
import twitter

key = open("config.cfg","r").read().strip('\n').split("    ")
api = twitter.Api(consumer_key=key[0],
                  consumer_secret=key[1],
                  access_token_key=key[2],
                  access_token_secret=key[3])

statuses = api.GetUser(screen_name = "callodehacha")
print(statuses.followers_count)


import cv2

video = cv2.VideoCapture(0) #bisa menggunakan video jadi 'video.mp4'
face = cv2.CascadeClassifier('ball2.xml')

while True:
    _, frame = video.read()

    edge = cv2.Canny(frame, 70, 70)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # cvt = convert
    muka = face.detectMultiScale(gray, 1.1, 1)

    for (x, y, w, h) in muka:  # sumbu x dan y, sdgkan w & h = lebar dan tinggi muka
        # membuat kotak pada wajah gambar img mulai dari kordinat (x, y) sampai kodrdinat (x+w, y+h) dengan warna hijau(0,255,0) tebal garis 4px
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

        # Region of Image (ROI) untuk mata
        roi_warna = frame[y:y + h, x:x + w]
        roi_gray = gray[y:y + h, x:x + w]   #matrix wajah

    cv2.imshow('Face & Eye Detection Live Video', frame)
    cv2.imshow('Edge', edge)

    exit = cv2.waitKey(1) & 0xff #waitKey(1) exit menggunakan perintah kwyboard
    if exit == 27: # esc
        break

cv2.destroyAllWindows()
video.release()


# -*- coding: utf-8 -*-
"""Triage - What is your company name? Page Object."""
import logging
import random
from urllib.parse import urljoin

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from settings import EXRED_UI_URL
from utils import assertion_msg, take_screenshot

NAME = "ExRed Triage - What is your company name"
URL = urljoin(EXRED_UI_URL, "triage")

QUESTION = "label[for=js-typeahead-company-name]"
COMPANY_NAME_INPUT = "js-typeahead-company-name"
SUGGESTIONS = "ul.SelectiveLookupDisplay"
FIRST_SUGGESTION = "ul.SelectiveLookupDisplay > li:nth-child(1)"
CONTINUE_BUTTON = ".exred-triage-form button.button"
PREVIOUS_STEP_BUTTON = ".exred-triage-form button.previous-step"
CONTINUE_WO_NAME_BUTTON = "div.exred-triage-form button[name=wizard_skip_step]"
BACK_TO_HOME_LINK = ".home-link a"
EXPECTED_ELEMENTS = {
    "question": "label[for=js-typeahead-company-name]",
    "continue button": CONTINUE_BUTTON,
    "previous step button": PREVIOUS_STEP_BUTTON,
    "continue without providing name": CONTINUE_WO_NAME_BUTTON,
    "back to home link": BACK_TO_HOME_LINK
}

def should_be_here(driver: webdriver):
    for element_name, element_selector in EXPECTED_ELEMENTS.items():
        element = driver.find_element_by_css_selector(element_selector)
        with assertion_msg(
                "It looks like '%s' element is not visible on %s",
                element_name, NAME):
            assert element.is_displayed()
    take_screenshot(driver, NAME)
    logging.debug("All expected elements are visible on '%s' page", NAME)

def hide_suggestions(driver: webdriver):
    suggestions = driver.find_element_by_css_selector(SUGGESTIONS)
    WebDriverWait(driver, 5).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, SUGGESTIONS)))
    if suggestions.is_displayed():
        question = driver.find_element_by_css_selector(QUESTION)
        question.click()

def click_on_first_suggestion(driver: webdriver):
    suggestions = driver.find_element_by_css_selector(SUGGESTIONS)
    WebDriverWait(driver, 5).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, SUGGESTIONS)))
    if suggestions.is_displayed():
        first_suggestion = driver.find_element_by_css_selector(FIRST_SUGGESTION)
        first_suggestion.click()

def enter_company_name(driver: webdriver, company_name: str = None):
    if not company_name:
        company_name = random.choice(["automated", "browser", "tests"])
    input_field = driver.find_element_by_id(COMPANY_NAME_INPUT)
    input_field.clear()
    input_field.send_keys(company_name)
    take_screenshot(driver, NAME + " after typing in company name")

def get_company_name(driver: webdriver) -> str:
    input_field = driver.find_element_by_id(COMPANY_NAME_INPUT)
    return input_field.get_attribute("value")

def submit(driver: webdriver):
    button = driver.find_element_by_css_selector(CONTINUE_BUTTON)
    button.click()
    take_screenshot(driver, NAME + " after submitting")

def is_company_name(driver: webdriver, company_name: str):
    given = get_company_name(driver)
    with assertion_msg(
            "Expected the company name input box to be prepopulated with: '%s'"
            " but got '%s' instead", company_name, given):
        assert given.lower() == company_name.lower()


#!/usr/bin/env python

import sys

if __name__ == "__main__":
    # Support globbing `$PREDUCE/reducers/*.py` to get a list of reducers.
    sys.exit(0)

import os
import subprocess
import tempfile

def num_lines_in_file(path):
    """Return the number of lines in the file at the given path."""
    num_lines = 0
    with open(path, "r") as f:
        for _ in f:
            num_lines += 1
    return num_lines

def get_executable(possibles):
    for p in possibles:
        if os.path.isfile(p) and os.access(p, os.X_OK):
            return p
    return None

def get_topformflat():
    """Try to find the `topformflat` program."""
    return get_executable([
        "/usr/local/libexec/topformflat",
        "/usr/libexec/topformflat",
        "/usr/lib/x86_64-linux-gnu/topformflat",
        "/usr/lib/creduce/topformflat",
        "/usr/local/Cellar/creduce/2.7.0/libexec/topformflat",
    ])

def get_clex():
    """Try to find the `clex` program."""
    return get_executable([
        "/usr/local/libexec/clex",
        "/usr/libexec/clex",
        "/usr/lib/x86_64-linux-gnu/clex",
        "/usr/lib/creduce/clex",
        "/usr/local/Cellar/creduce/2.7.0/libexec/clex",
    ])

def get_clang_delta():
    """Try to find the `clang_delta` program."""
    return get_executable([
        "/usr/local/libexec/clang_delta",
        "/usr/libexec/clang_delta",
        "/usr/lib/x86_64-linux-gnu/clang_delta",
        "/usr/lib/creduce/clang_delta",
        "/usr/local/Cellar/creduce/2.7.0/libexec/clang_delta",
    ])

def chunk_sizes(min_chunk_size, max_chunk_size):
    """Generate chunk sizes from min_chunk_size to max_chunk_size."""
    chunk_size = max_chunk_size
    while chunk_size >= min_chunk_size:
        yield chunk_size
        chunk_size = chunk_size // 2

def copy_without_lines(from_path, to_path, start_skip_line, num_skip_lines):
    """Copy the file at `from_path` to `to_path`, without the lines in the range
    [start_skip_line, start_skip_line + num_skip_lines)

    """
    end_skip_lines = start_skip_line + num_skip_lines
    with open(to_path, "w") as out_file:
        with open(from_path, "r") as in_file:
            for j, line in enumerate(in_file):
                if start_skip_line <= j < end_skip_lines:
                    continue
                else:
                    out_file.write(line)

def chunking_reducer(seed, min_chunk_size = 1, max_chunk_size = None):
    """Implements a reducer that removes chunks of lines from the given seed file.

    """
    num_lines = num_lines_in_file(seed)

    if max_chunk_size is None:
        max_chunk_size = num_lines

    for chunk_size in chunk_sizes(min_chunk_size, max_chunk_size):
        for i in range(0, num_lines - (chunk_size - 1)):
            # Read the file path from stdin.
            out_file_path = sys.stdin.readline().strip()
            if out_file_path == "":
                return

            # Copy the file without the current chunk.
            copy_without_lines(seed, out_file_path, i, chunk_size)

            # Tell `preduce` we generated the reduction.
            sys.stdout.write("\n")
            sys.stdout.flush()

def topformflat_reducer(seed, flatten):
    """Run `topformflat` on the seed file, and then create a chunking reducer from
    it.

    """
    topformflat = get_topformflat()
    if topformflat is None:
        return

    with tempfile.NamedTemporaryFile(mode="w+", delete=False) as tmp_file:
        with open(seed, "r") as in_file:
            subprocess.check_call([topformflat, str(flatten)], stdin=in_file, stdout=tmp_file)

    chunking_reducer(tmp_file.name)

def clex_reducer(seed, clex_command):
    clex = get_clex()
    if clex is None:
        return

    index = 0
    while True:
        # Read the file path from stdin.
        out_file_path = sys.stdin.readline().strip()
        if out_file_path == "":
            return

        retcode = 0
        with open(out_file_path, "w") as out_file:
            retcode = subprocess.call([clex, clex_command, str(index), seed],
                                      stdout=out_file)

        # I don't know why clex is written with these bizarre exit codes...
        if retcode != 51:
            return;

        index += 1

        # Tell `preduce` we generated the reduction.
        sys.stdout.write("\n")
        sys.stdout.flush()

def clang_delta_reducer(seed, transformation):
    clang_delta = get_clang_delta()
    if clang_delta is None:
        return

    index = 1
    while True:
        # Read the file path from stdin.
        out_file_path = sys.stdin.readline().strip()
        if out_file_path == "":
            return

        retcode = None
        with open(out_file_path, "w") as out_file:
            retcode = subprocess.call([clang_delta,
                                       "--transformation={}".format(transformation),
                                       "--counter={}".format(str(index)),
                                       seed],
                                      stdout=out_file)

        if retcode == 0:
            # Tell `preduce` we generated the reduction.
            sys.stdout.write("\n")
            sys.stdout.flush()

            index += 1
            continue
        elif retcode == 255 or retcode == 1:
            return
        else:
            # Ideally, we would do this:
            #
            #     raise Exception("Unknown return code from clang_delta: {}".format(str(retcode)))
            #
            # Except that `clang_delta` SIGSEGVs frequently enough that the
            # backtraces from raising this exception would completely drown out
            # any useful information we are otherwise logging.
            return


#!/usr/bin/python3

#DEPENDENCIES: blkid,smartctl(smartmontools)

import argparse
import re
import subprocess
import sys
import os

def script_action(script):
  """
  Run specified script.
  """
  subprocess.run(script.split(" "))

def print_output(smart_values,importance,script):
  """
  Prints output to stdout.

  Keyword arguments:
  smart_values -- smart detail info of problem device(s)
  importance -- given script importance
  script -- script to be executed when threshol is reached
  """
  ret_str = importance + "\n"
  ret_str = ret_str + smart_values
  if script is not None:
      script_action(script)
  return ret_str

def find_block_devs():
  """
  Returns block devices in machine
  """
  blkid = subprocess.run(["blkid"],stdout=subprocess.PIPE)
  blkid = blkid.stdout.decode("utf-8")
  uniq_devs = []
  for line in blkid.split('\n')[:-1]:
    try:
      dev = re.search("(.*)\d+:.*",line).group(1)  #filter only blockdevs
    except AttributeError:
      dev = re.search("(.*):.*",line).group(1)  #filter only blockdevs
    if dev not in uniq_devs:
      uniq_devs.append(dev)  #append dev only if it is not in list
  return uniq_devs

def check_values(block_devs):
  """
  Returns block device which is about to fail

  Keyword arguments:
  block_devs -- all block devices in machine
  """
  ret_str = ""
  for dev in block_devs:
    smart_out = subprocess.run(["smartctl","-a",dev],stdout=subprocess.PIPE)
    smart_out = smart_out.stdout.decode("utf-8")
    try:
      smart_values = re.search("(ID# ATTRIBUTE_NAME          FLAG     VALUE WORST THRESH TYPE      UPDATED  WHEN_FAILED RAW_VALUE\n)([\s\S]*?\n\n)",smart_out).group(2)  #filter smart values only
    except AttributeError:
      ret_str = ret_str + "S.M.A.R.T. VALUES UNAVAILABLE FOR " + str(dev) + "\n"
      ret_str = ret_str + smart_out + "\n"
      continue
    first_time = True  #flag, that error has occured for the first time on a device
    for line in smart_values.split('\n')[:-2]:
      value = re.search(" *\d+ +.*? +.*? +(.*?) +(.*?) +(.*?) +.*", line).group(1)
      threshold = re.search(" *\d+ +.*? +.*? +(.*?) +(.*?) +(.*?) +.*", line).group(3)
      if (int(value) <= int(threshold)) and first_time:
        ret_str = ret_str + "\n" + "Block device \"" + dev + "\" status: Error \n"
        ret_str = ret_str + re.search("=== START OF INFORMATION SECTION ===([\s\S]*?\n\n)",smart_out).group(1)
        ret_str = ret_str +  "Threshold has reached its value:\n" + line + "\n"
        first_time = False
      elif (int(value) <= int(threshold)):
        ret_str = ret_str + line + "\n"
  return ret_str

def uuid_to_block(uuid):
  """
  Return name of block device according to uuid

  Keyword arguments:
  uuid -- id of partition
  """
  blkid = subprocess.run(["blkid"],stdout=subprocess.PIPE)
  blkid = blkid.stdout.decode("utf-8")
  try:
    block_dev = re.search("(.*)\:.*UUID=\"" + re.escape(uuid) + "\".*",blkid).group(1)  #filter block dev name
  except AttributeError:
    block_dev = ""
  return block_dev

def main():
  version = 1.0
  parser = argparse.ArgumentParser(prog="disk_smart_mon.py", description="Monitoring SMART value on every disk",formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  parser.add_argument("-v","--version",action="version", version="%(prog)s"+" "+str(version))
  parser.add_argument("-i","--importance",help="defines priority tag in notification message",default="info",choices=["info","warning","error","critical"])
  parser.add_argument("-e","--execute",help="run specified script after threshold value or error found",action="store",type=str)
  specific_disk = parser.add_mutually_exclusive_group(required=True)
  specific_disk.add_argument("-b","--blockdev",help="disk partition identified as block device e.g. /dev/sda1",action="store",type=str)
  specific_disk.add_argument("-a","--alldevices",help="defines all block devices connected to machine",action="store_true")
  specific_disk.add_argument("-u","--uuid",help="disk partition identified with uuid",action="store",type=str)
  args = parser.parse_args()

  if args.alldevices:
    block_devs = find_block_devs()
  elif args.blockdev:
   block_devs = []
   block_devs.append(args.blockdev)
  elif args.uuid:
    block_devs = []
    block_devs.append(uuid_to_block(args.uuid))

  smart_values = check_values(block_devs)
  if smart_values == "":
    ret_str = ""
  else:
    ret_str = print_output(smart_values,args.importance,args.execute)
  print(ret_str)

if __name__ == '__main__':
  try:
    main()
  except Exception as e:
    print(e)


# Imports from 3rd party libraries
import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

# Imports from this application
from app import app, server
from pages import index, predictions, insights, process

navbar = dbc.NavbarSimple(
    brand='BTCUSD Predictor',
    brand_href='/',
    children=[
        dbc.NavItem(dcc.Link('About', href='/about', className='nav-link')),
    ],
    sticky='top',
    color="#082255",
    dark=True,
	fluid=True,
	className='h1',
)
"""

navbar = dbc.Navbar(
	dbc.Container(
		[
			html.A(
				dbc.Row(
					[
						dbc.Col(html.Img(src="../assets/Bitcoin_logo-700x155.png", height="30px")),
						dbc.Col(dbc.NavbarBrand("Dash", className="m1-2")),
					],
					#align="center",
					no_gutters=True,
				),
				href='/',
			),
			dbc.Nav(
				dbc.NavItem(dcc.Link('About', href='/about', className='nav-link')),
				navbar=True,
			),
		]
	),
	expand="lg",
	dark=True,
	sticky='top',
	color="#082255",
	className='h1',
)
"""

footer = dbc.Container(
    dbc.Row(
        dbc.Col(
            html.P(
                [
                    html.Span('Hanchung Lee      ', className='mr-2'),
                    html.A(html.I(className='fas fa-envelope-square mr-3'), href='mailto:lee.hanchung@gmail.com'),
                    html.A(html.I(className='fab fa-github-square mr-3'), href='https://github.com/leehanchung/btc_dash'),
                    html.A(html.I(className='fab fa-linkedin mr-3'), href='https://www.linkedin.com/in/hanchunglee/'),
                    html.A(html.I(className='fab fa-twitter-square mr-3'), href='https://twitter.com/hanchunglee'),
                ],
                className='h1'#'lead'
            )
        )
    ),
	fluid=True
)

# For more explanation, see:
# Plotly Dash User Guide, URL Routing and Multiple Apps
# https://dash.plot.ly/urls
app.layout = html.Div([
    dcc.Location(id='url', refresh=False),
    navbar,
    dbc.Container(id='page-content', className='mt-4', fluid=True),
    html.Hr(),
    footer
])

@app.callback(Output('page-content', 'children'),
              [Input('url', 'pathname')])
def display_page(pathname):
    if pathname == '/':
        return index.layout
    elif pathname == '/about':
        return process.layout
    else:
        return dcc.Markdown('## Page not found')

if __name__ == '__main__':
    app.run_server(debug=True)


from __future__ import print_function
import pytest

from landlab.layers import MaterialLayers, MaterialLayersMixIn
from landlab import RasterModelGrid

def test_MaterialLayersMixIn():
    mg = RasterModelGrid(4,4)
    ml = mg.material_layers
    assert hasattr(mg, 'material_layers') == True
    assert ml.number_of_layers == 0
    assert ml.number_of_stacks == 4

def test_adding_untracked_layer():
    layers = MaterialLayers(3)
    layers.add(1., type=3., size='sand')
    layers.add([0., 0., 1.], type=3., size='sand')
    with pytest.raises(ValueError):
        layers.add([1.], type=3., size='sand', spam='eggs')


#######################################################
## Script: kmeans learner
## Argument: 1. data   : Input training data file
##           2. output : Files towrite  prototype and SSE
## Output: Writes prototype/ cluster centre co-ordinates and SSE in tsv files
## Limitation: i)Input features must be numerical, no preprocessing of input feature
## owner: Arnab Das
## date: 05/01/2019
#######################################################

###Imports
import os
import csv
import sys
import math
import argparse

###Declaring perser for cmmand line arguments
prsr = argparse.ArgumentParser()
prsr.add_argument('--data', required=True)
prsr.add_argument('--output', required=True)

###Parsing argments
args = prsr.parse_args()

### Identifying data output derectory
directory = os.path.dirname(args.output)

### Identifying file name
dataFileName = os.path.basename(args.data)
baseName = dataFileName.split(".")[0]

### Creating output filename format
outputFileName_Proto=baseName+"-Proto"+".tsv"
outputFileName_Progr=baseName+"-Progr"+".tsv"

### Checking and removing existing file
if os.path.isfile(os.path.join(directory,outputFileName_Proto)):
	print("Removing existing Proto file")
	os.remove(os.path.join(directory,outputFileName_Proto))

if os.path.isfile(os.path.join(directory,outputFileName_Progr)):
	print("Removing existing Progr file")
	os.remove(os.path.join(directory,outputFileName_Progr))

###Initialising some variable
data_list = []
instance_list = []
prototype_List = []
centre_List = []
centre_List_copy = []

### Function to read data file from command line argument --data
def read_DataFile():
	with open(args.data) as data_file:
		data = csv.reader(data_file, delimiter='	')
		for row in data:
			if any(row):
				data_list.append(row)
	data_file.close()

### Function for data write for csv file and console
def	data_writer(targetList,prop,outputFile):
	with open(outputFile,'a+',newline=prop) as op:
		cursor = csv.writer(op,delimiter = "	")
		cursor.writerows([targetList])
	op.close()

# Class for each instance
class instance:
	attb1 = 0
	attb2 = 0
	def __init__(self,dataPoint):
		self.attb1 = float(dataPoint[1])
		self.attb2 = float(dataPoint[2])

class prototype:
	attb1 = 0
	attb2 = 0
	cluster_list = []

	def __init__(self,dataPoint):
		self.attb1 = dataPoint[0]
		self.attb2 = dataPoint[1]
		self.cluster_list = []

	def getDist(self, inst):
		return ((self.attb1 - inst.attb1)**2 + (self.attb2 - inst.attb2)**2)**0.5

	def getSqrErr(self):
		totErr = 0
		for points in self.cluster_list:
			totErr = totErr + self.getDist(points)**2
		return totErr

	def updateCluster(self,inst):
		self.cluster_list.append(inst)

	def updatePrototype(self):
		sumAttb1 = 0
		sumAttb2 = 0

		for points in self.cluster_list:
			sumAttb1 = sumAttb1 + points.attb1
			sumAttb2 = sumAttb2 + points.attb2

		self.attb1 = sumAttb1/len(self.cluster_list)
		self.attb2 = sumAttb2/len(self.cluster_list)
		self.cluster_list = []

	def getPrototype(self):
		return str(self.attb1) + "," + str(self.attb2)

#reading input data
print("Reading input data")
read_DataFile()

#converting data to instance list
print("Processing data")
for data in data_list:
	instance_list.append(instance(data))

#initialising prototypes
prototype_List.append(prototype([0,5]))
prototype_List.append(prototype([0,4]))
prototype_List.append(prototype([0,3]))

for prots in prototype_List:
	centre_List.append(prots.getPrototype())

#processing
print("Moving prototypes/cluster centres")
previous_SqrErr = 0
squareError = 0
while True :
	previous_SqrErr = squareError
	squareError = 0
	centre_List_copy = centre_List
	centre_List = []

	for inst in instance_list:
		dist_dict = {}

		for prots in prototype_List:
			dist_dict.update({prots.getDist(inst):prots})

		nearestProt = sorted(dist_dict.items(), key = lambda x: x[0])[0][1]
		nearestProt.updateCluster(inst)

	for prots in prototype_List:
		squareError = squareError + prots.getSqrErr()
		prots.updatePrototype()
		centre_List.append(prots.getPrototype())

	if abs(squareError == previous_SqrErr):
		break

	data_writer(centre_List_copy,"",os.path.join(directory,outputFileName_Proto))
	data_writer([squareError],"",os.path.join(directory,outputFileName_Progr))

print("Data Written to appropriate files")
print("Process finished")


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

# Copyright 2011-2015, Nigel Small
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from py2neo.core import Resource, Node, Rel, Rev, Relationship, Service, Path
from py2neo.error import BindError
from test.util import Py2neoTestCase

class BindUnbindTestCase(Py2neoTestCase):

    def test_can_create_bindable_with_initial_uri(self):
        uri = "http://localhost:7474/db/data/node/1"
        bindable = Service()
        bindable.bind(uri)
        assert bindable.bound
        assert bindable.uri == uri

    def test_can_create_bindable_with_initial_uri_and_metadata(self):
        uri = "http://localhost:7474/db/data/node/1"
        metadata = {"foo": "bar"}
        bindable = Service()
        bindable.bind(uri, metadata)
        assert bindable.bound
        assert bindable.uri == uri
        assert bindable.resource.metadata == metadata

    def test_can_create_bindable_with_initial_uri_template(self):
        uri = "http://localhost:7474/db/data/node/{node_id}"
        bindable = Service()
        bindable.bind(uri)
        assert bindable.bound
        assert bindable.uri == uri

    def test_cannot_create_bindable_with_initial_uri_template_and_metadata(self):
        uri = "http://localhost:7474/db/data/node/{node_id}"
        metadata = {"foo": "bar"}
        service = Service()
        try:
            service.bind(uri, metadata)
        except ValueError:
            assert True
        else:
            assert False

    def test_default_state_for_node_is_unbound(self):
        node = Node()
        assert not node.bound
        with self.assertRaises(BindError):
            _ = node.resource

    def test_bound_path_is_bound(self):
        alice = Node(name="Alice")
        bob = Node(name="Bob")
        carol = Node(name="Carol")
        dave = Node(name="Dave")
        path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
        self.graph.create(path)
        assert path.bound

    def test_unbound_path_is_not_bound(self):
        alice = Node(name="Alice")
        bob = Node(name="Bob")
        carol = Node(name="Carol")
        dave = Node(name="Dave")
        path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
        assert not path.bound

    def test_can_bind_node_to_resource(self):
        uri = "http://localhost:7474/db/data/node/1"
        node = Node()
        node.bind(uri)
        assert node.bound
        assert isinstance(node.resource, Resource)
        assert node.resource.uri == uri
        node.unbind()
        assert not node.bound
        with self.assertRaises(BindError):
            _ = node.resource

    def test_can_bind_rel_to_resource(self):
        uri = "http://localhost:7474/db/relationship/1"
        rel = Rel()
        rel.bind(uri)
        assert rel.bound
        assert isinstance(rel.resource, Resource)
        assert rel.resource.uri == uri
        rel.unbind()
        assert not rel.bound
        with self.assertRaises(BindError):
            _ = rel.resource

    def test_can_bind_rev_to_resource(self):
        uri = "http://localhost:7474/db/relationship/1"
        rel = Rev()
        rel.bind(uri)
        assert rel.bound
        assert isinstance(rel.resource, Resource)
        assert rel.resource.uri == uri
        rel.unbind()
        assert not rel.bound
        with self.assertRaises(BindError):
            _ = rel.resource

    def test_can_bind_relationship_to_resource(self):
        uri = "http://localhost:7474/db/relationship/1"
        metadata = {
            "start": "http://localhost:7474/db/node/1",
            "end": "http://localhost:7474/db/node/2",
        }
        relationship = Relationship({}, "", {})
        # Pass in metadata to avoid callback to server
        relationship.bind(uri, metadata=metadata)
        assert relationship.bound
        assert isinstance(relationship.resource, Resource)
        assert relationship.resource.uri == uri
        relationship.unbind()
        assert not relationship.bound
        with self.assertRaises(BindError):
            _ = relationship.resource

    def test_can_unbind_node_if_not_cached(self):
        node, = self.graph.create({})
        Node.cache.clear()
        node.unbind()
        assert not node.bound

    def test_can_unbind_rel_if_not_cached(self):
        a, b, ab = self.graph.create({}, {}, (0, "KNOWS", 1))
        Rel.cache.clear()
        ab.rel.unbind()
        assert not ab.bound

    def test_can_unbind_relationship_if_not_cached(self):
        a, b, ab = self.graph.create({}, {}, (0, "KNOWS", 1))
        Relationship.cache.clear()
        ab.unbind()
        assert not ab.bound

    def test_can_unbind_relationship_with_already_unbound_nodes(self):
        a, b, ab = self.graph.create({}, {}, (0, "KNOWS", 1))
        a.unbind()
        b.unbind()
        assert not a.bound
        assert not b.bound
        ab.unbind()
        assert not ab.bound

    def test_can_unbind_bound_path(self):
        alice = Node(name="Alice")
        bob = Node(name="Bob")
        carol = Node(name="Carol")
        dave = Node(name="Dave")
        path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
        self.graph.create(path)
        path.unbind()
        assert not path.bound

    def test_can_unbind_unbound_path_without_error(self):
        alice = Node(name="Alice")
        bob = Node(name="Bob")
        carol = Node(name="Carol")
        dave = Node(name="Dave")
        path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
        path.unbind()
        assert not path.bound

    def test_unbinding_rel_also_unbinds_rev(self):
        a, b, ab = self.graph.create({}, {}, (0, "KNOWS", 1))
        rel = ab.rel
        assert rel.pair is None
        rev = -rel
        assert rel.pair is rev
        assert rev.pair is rel
        assert rel.bound
        assert rev.bound
        assert rel.resource is rev.resource
        rel.unbind()
        assert not rel.bound
        assert not rev.bound

    def test_unbinding_rev_also_unbinds_rel(self):
        a, b, ab = self.graph.create({}, {}, (0, Rev("KNOWS"), 1))
        rev = ab.rel
        rel = -rev
        assert rev.pair is rel
        assert rel.pair is rev
        assert rev.bound
        assert rel.bound
        assert rev.resource is rel.resource
        rev.unbind()
        assert not rev.bound
        assert not rel.bound


import sys
import psutil
import time
import argparse

def main():

    parser = argparse.ArgumentParser(description='Record CPU and memory usage for a process')

    parser.add_argument('process_id', type=int, help='the process id')

    parser.add_argument('--log', type=str,
                        help='output the statistics to a file')

    parser.add_argument('--plot', type=str,
                        help='output the statistics to a plot')

    parser.add_argument('--duration', type=float,
                        help='how long to record for (in seconds). If not '
                             'specified, the recording is continuous until '
                             'the job exits.')

    parser.add_argument('--interval', type=int,
                        help='how long to wait between each sample (in '
                             'seconds). By default the process is sampled '
                             'as often as possible.')

    args = parser.parse_args()

    # Attach to process
    pr = psutil.Process(args.process_id)

    # Record start time
    start_time = time.time()

    if args.log:
        f = open(args.filename, 'w')

    log = {}
    log['times'] = []
    log['cpu'] = []
    log['mem_real'] = []
    log['mem_virtual'] = []

    # Start main event loop
    while True:

        # Find current time
        current_time = time.time()

        # Check if we have reached the maximum time
        if args.duration is not None and current_time - start_time > args.duration:
            break

        # Get current CPU and memory
        try:
            current_cpu = pr.get_cpu_percent()
            current_mem = pr.get_memory_info()
        except:
            break
        current_mem_real = current_mem.rss / 1024. ** 2
        current_mem_virtual = current_mem.vms / 1024. ** 2

        if args.log:
            f.write("{0:10.3f} {1:10.3f} {2:10.3f} {3:10.3f}\n".format(current_time, current_cpu, current_mem_real, current_mem_virtual))
            f.flush()

        if args.interval is not None:
            time.sleep(args.interval)

        # If plotting, record the values
        if args.plot:
            log['times'].append(current_time - start_time)
            log['cpu'].append(current_cpu)
            log['mem_real'].append(current_mem_real)
            log['mem_virtual'].append(current_mem_virtual)

    if args.log:
        f.close()

    if args.plot:

        import matplotlib.pyplot as plt

        fig = plt.figure()
        ax = fig.add_subplot(1,1,1)

        ax.plot(log['times'],log['cpu'], '-', lw=1, color='r')

        ax.set_ylabel('CPU (%)', color='r')
        ax.set_xlabel('time (s)')

        ax2 = ax.twinx()

        ax2.plot(log['times'],log['mem_real'], '-', lw=1, color='b')

        ax2.set_ylabel('Real Memory (MB)', color='b')

        ax.grid()

        fig.savefig(args.plot)


# -*- coding: utf-8 -*-
"""
Created on Tue Apr 14 14:30:14 2020

@author: ethan
"""

import numpy as np
import argparse
from copy import deepcopy
import torch
import gym

from normalized_env import NormalizedEnv
from evaluator import Evaluator, BeamSelectionEvaluator, DDPGAgentEval
from ddpg import DDPG
from multiwindow_DDPG import multiwindow_DDPG
from Autoencoder_DDPG import Autoencoder_DDPG
from util import *
from BeamManagementEnv import BeamManagementEnv
import matplotlib.pyplot as plt
import time
import seaborn as sns
from tqdm import tqdm

def iterative_selection(observation:np.ndarray, num_beams_per_UE, nb_actions):
    #observation is num_measurements x num_beams matrix, iteratively pick best beam
    # selected_beams = np.argsort(np.sum(observation,axis=0))[-num_beams_per_UE:]

    selected_beams = []
    pool = list(np.arange(nb_actions))
    while len(selected_beams) < num_beams_per_UE:
        temp_r = [observation[:,np.array(selected_beams+[i])].max(axis=1).sum() for i in pool]
        temp_b = pool[np.argmax(temp_r)]
        selected_beams.append(temp_b)
        pool.remove(temp_b)

    binary_beams = np.zeros(nb_actions)
    binary_beams[np.array(selected_beams)] = 1
    return binary_beams

def top_k_selection(observation:np.ndarray, num_beams_per_UE, nb_actions):
    #observation is num_measurements x num_beams matrix, iteratively pick best beam
    # selected_beams = np.argsort(np.sum(observation,axis=0))[-num_beams_per_UE:]

    selected_beams = np.argsort(np.sum(observation,axis=0))[-num_beams_per_UE:]
    binary_beams = np.zeros(nb_actions)
    binary_beams[np.array(selected_beams)] = 1
    return binary_beams

if __name__ == "__main__":

    env = BeamManagementEnv(num_antennas = 64,
                            oversampling_factor = 1,
                            num_beams_per_UE = 8,
                            ue_speed = 10,
                            enable_baseline = True,
                            enable_genie = True,
                            enable_exhaustive = True,
                            combine_state = False,
                            full_observation = True,
                            num_measurements = 8)

    nb_states = env.observation_space.shape[0]
    nb_actions = env.action_space.shape[0]

    num_epoch = int(5e2)
    baseline = []
    genie = []
    iterative = []
    random = []
    test = []
    top_k = []
    top_k_genie = []
    top_k_gap = []

    for epi in tqdm(range(num_epoch)):
        done = False
        baseline_epi = genie_epi = iterative_epi = test_epi = random_epi = top_k_epi = top_k_genie_epi = epi_step = 0
        # ob = env.reset()
        ob, info = env.reset()
        # prev_ob = ob
        while not done:
            print(env.ue_traveled_dist_next)
            action = iterative_selection(info['next_observation'], env.num_beams_per_UE, nb_actions)
            random_beams = np.random.choice(np.arange(env.codebook_size),env.num_beams_per_UE,replace=False)
            random_epi += env._calc_reward(random_beams)[0]
            top_k_genie_r = env._calc_reward(np.nonzero(top_k_selection(info['next_observation'],env.num_beams_per_UE,nb_actions))[0])[0]
            top_k_genie_epi += top_k_genie_r
            top_k_epi += env._calc_reward(np.nonzero(top_k_selection(ob,env.num_beams_per_UE,nb_actions))[0])[0]
            # action = np.zeros(nb_actions)
            # action[np.argsort(ob)[-8:]] = 1
            ob, r, done, info = env.step(action)
            baseline_epi += info['baseline_reward']
            genie_epi += info['genie_reward']
            iterative_epi += info['incremental_reward']
            top_k_gap.append(info['genie_reward']-top_k_genie_r)
            if info['genie_reward']-top_k_genie_r < 0:
                print('ahh')
            test_epi += r
            epi_step += 1
        baseline.append(baseline_epi/epi_step)
        genie.append(genie_epi/epi_step)
        iterative.append(iterative_epi/epi_step)
        random.append(random_epi/epi_step)
        test.append(test_epi/epi_step)
        top_k_genie.append(top_k_genie_epi/epi_step)
        top_k.append(top_k_epi/epi_step)

    plt.figure()
    sns.kdeplot(test,label='iterative selection with perfect predictor')
    sns.kdeplot(baseline,label='baseline')
    sns.kdeplot(genie,label='upperbound')
    sns.kdeplot(iterative,label='iterative selection with genie')
    sns.kdeplot(random,label='random selection')
    sns.kdeplot(top_k,label='top k selection')
    sns.kdeplot(top_k_genie,label='top k selection with genie')
    plt.legend();

    plt.figure()
    sns.kdeplot(top_k_gap,label='top_k_gap')


"""tools for ipython notebook"""

import matplotlib.pyplot as plt
from matplotlib.pyplot import cm
import itertools
import numpy as np
from sklearn.model_selection import cross_val_score

def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=cm.Blues,
                          figsize=(15,12)):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        #print("Normalized confusion matrix")
    else:
        pass
        #print('Confusion matrix, without normalization')

    #print(cm)
    plt.figure(figsize=figsize)
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title, fontsize=22)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.ylabel('True label', fontsize=22)
    plt.xlabel('Predicted label', fontsize=22)
    plt.tight_layout()

def plot_correlation_matrix(cm, attributes, title='Correlation Matrix', cmap='RdBu', figsize=(12,12),savename=None):
    fig=plt.figure(figsize=figsize)
    #plt.matshow(eucorr.as_matrix(), cmap=plt.cm.Blues)
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    plt.clim(-1,1)
    tick_marks = np.arange(len(attributes))
    plt.xticks(tick_marks, attributes, rotation=45)
    plt.yticks(tick_marks, attributes)
    fmt = 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], '.2f'),
                  horizontalalignment="center",
                  verticalalignment="center",
                  color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    if savename != None:
        fig.savefig(savename,bbox_inches='tight')

def gen_log_space(limit, n):
    """ generate integers in logaritmic space"""
    result = [1]
    if n>1:  # just a check to avoid ZeroDivisionError
        ratio = (float(limit)/result[-1]) ** (1.0/(n-len(result)))
    while len(result)<n:
        next_value = result[-1]*ratio
        if next_value - result[-1] >= 1:
            # safe zone. next_value will be a different integer
            result.append(next_value)
        else:
            # problem! same integer. we need to find next_value by artificially incrementing previous value
            result.append(result[-1]+1)
            # recalculate the ratio so that the remaining values will scale correctly
            ratio = (float(limit)/result[-1]) ** (1.0/(n-len(result)))
    # round, re-adjust to 0 indexing (i.e. minus 1) and return np.uint64 array
    return np.array(list(map(lambda x: round(x)-1, result)), dtype=int)

def cross_validate_model(model, x_train, y_train):
    m = cross_val_score(model, x_train, y_train)

    test_score, train_score = m[0], m[1]

    return (np.mean(test_score), np.std(test_score)),(np.mean(train_score), np.std(train_score))


from config import db
from pprint import pprint
import traceback

def get_boardid(conn, prefix="bbs"):
    """
    返回当前数据库中以prefix前缀开头的表名称list
    """
    cur = conn.cursor()
    cur.execute("show tables")
    sqlresult = cur.fetchall()
    return [i[0] for i in sqlresult if i[0].startswith(prefix)]

def alter_to_utf8mb4():
    conn = db()
    names = get_boardid(conn)+get_boardid(conn,prefix="big") #以bbs开头的表和以bigbbs开头的表都要处理
    for tablename in names:
        print(tablename)
        # 修改列的字符集、表的字符集
        sql = """
ALTER TABLE `{tablename}`
MODIFY COLUMN `user`  varchar(66) CHARACTER SET utf8mb4 NOT NULL AFTER `edittime`,
MODIFY COLUMN `content`  longtext CHARACTER SET utf8mb4 NOT NULL AFTER `user`,
DEFAULT CHARACTER SET=utf8mb4;""".format(tablename=tablename)
        cursor = conn.cursor()
        try:
            cursor.execute(sql)
            conn.commit()
        except Exception as e:
            traceback.print_exc() #发生了异常，就显示报错信息，停下来等待处理
            input("Wait...") # 如果不严重可以继续处理下一个表 按回车；终止整个程序 按Ctrl+C

if __name__ == "__main__":
    alter_to_utf8mb4()


""" Users model """

# Django
from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):

	email = models.EmailField(
		'email address',
		unique=True,
		error_messages={'unique': 'This email address is already in use'}
	)

	is_client = models.BooleanField(
		'client',
		default=False,
		help_text='client is the main type of users.'
	)

	is_admin = models.BooleanField(
		'admin',
		default=False,
		help_text='user with high privileges.'
	)

	catalogue = models.ForeignKey(
		'catalogues.ClientCatalogue',
		on_delete=models.SET_NULL,
		null=True,
		related_name='clients')

	USERNAME_FIELD = 'username'
	REQUIRED_FIELDS = ['email', 'first_name', 'last_name']

	def __str__(self):
		return self.username


import os

from kivy.app import App
from kivy.properties import StringProperty, Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen
from kivymd.theming import ThemableBehavior
from kivymd.uix.list import OneLineIconListItem

from logo import strings

class ContentNavigationDrawer(BoxLayout):
    pass

class NavigationItem(OneLineIconListItem):
    icon = StringProperty()
    goto = StringProperty()

class Main(ThemableBehavior, Screen):
    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)

    def on_pre_enter(self, *args):
        app = App.get_running_app()
        app.user_name = "[b]{e.given_name} {e.family_name}[/b]".format(
            e=app.ioc.facade.data.portfolio.entity)
        app.profile_picture = os.path.join(os.environ["LOGO_MESSENGER_ASSETS"], "images/mask.png")

        def goto(item):
            self.ids.scr_mngr.current = item.goto
            self.ids.nav_drawer.set_state("close")

        for item in (
                ("monitor-dashboard", "dashboard", strings.TEXT_SECTION_DASHBOARD),
                ("email-outline", "messages", strings.TEXT_SECTION_MESSAGES),
                ("contact-mail-outline", "contacts", strings.TEXT_SECTION_CONTACTS),
                ("folder-account-outline", "documents", strings.TEXT_SECTION_DOCUMENTS),
                ("web", "networks", strings.TEXT_SECTION_NETWORKS),
                ("face-profile", "profile", strings.TEXT_PROFILE),
                ("settings", "preferences", strings.TEXT_SECTION_PREFERENCES),
        ):
            self.ids.content_drawer.ids.box_item.add_widget(
                NavigationItem(
                    text=item[2],
                    icon=item[0],
                    goto=item[1],
                    on_release=goto
                )
            )


import sys
import os
import re
import keyboard
import time
from _thread import start_new_thread
from KeyCollector import KeyCollector
from colorama import init as colorama
colorama()

options_dir = "C:\\CMD\\editor\\"

class Color:
    black = "30"
    red = "31"
    green = "32"
    yellow = "33"
    blue = "34"
    purple = "35"
    cyan = "36"
    white = "37"

themes = {
    "dark_red": {
        "name": "Dark Red",
        "help_line": [Color.black, Color.black, 1],
        "help_text": [Color.black, Color.black, 1],
        "help_title": [Color.red, Color.black, 1],

        "esc": [Color.red, Color.black, 1],
        "toolbar_line": [Color.black, Color.black, 1],
        "toolbar_text": [Color.black, Color.black, 1],
        "toolbar_title": [Color.red, Color.black, 1],

        "editor_line_number": [Color.red, Color.black, 0],
        "editor_line_side": [Color.red, Color.black, 1],
        "editor_text": [Color.black, Color.black, 1],
        "editor_cursor": [Color.red, Color.black, 1],

        "editor_bottom": [Color.red, Color.black, 0],

        "credits": [Color.black, Color.black, 1],

        "debug": [Color.white, Color.black, 0]
    },
    "dark_cyan": {
        "name": "Dark Cyan",
        "help_line": [Color.black, Color.black, 1],
        "help_text": [Color.cyan, Color.black, 0],
        "help_title": [Color.cyan, Color.black, 1],

        "esc": [Color.cyan, Color.black, 1],
        "toolbar_line": [Color.black, Color.black, 1],
        "toolbar_text": [Color.black, Color.black, 1],
        "toolbar_title": [Color.cyan, Color.black, 1],

        "editor_line_number": [Color.cyan, Color.black, 1],
        "editor_line_side": [Color.blue, Color.black, 1],
        "editor_text": [Color.black, Color.black, 1],
        "editor_cursor": [Color.cyan, Color.black, 0],

        "editor_bottom": [Color.cyan, Color.black, 0],

        "credits": [Color.black, Color.black, 1],

        "debug": [Color.white, Color.black, 0]
    },
    "light_purple": {
        "name": "Light Purple",
        "help_line": [Color.white, Color.black, 1],
        "help_text": [Color.purple, Color.black, 1],
        "help_title": [Color.purple, Color.black, 1],

        "esc": [Color.purple, Color.black, 1],
        "toolbar_line": [Color.black, Color.black, 1],
        "toolbar_text": [Color.purple, Color.black, 0],
        "toolbar_title": [Color.purple, Color.black, 1],

        "editor_line_number": [Color.purple, Color.black, 0],
        "editor_line_side": [Color.black, Color.black, 1],
        "editor_text": [Color.black, Color.black, 1],
        "editor_cursor": [Color.purple, Color.black, 1],

        "editor_bottom": [Color.purple, Color.black, 0],

        "credits": [Color.black, Color.black, 1],

        "debug": [Color.white, Color.black, 0]
    }
}
themes_list = []
for name, x in themes.items():
    themes_list.append(name)
for x, name in enumerate(themes_list):
    if name == "dark_red":
        current_theme = themes[themes_list[x]]
        current_theme_id = x

cursor_color = "\033[{}m".format(";".join([
    str(current_theme["editor_cursor"][2]),
    current_theme["editor_cursor"][0],
    str(int(current_theme["editor_cursor"][1])+10)
]))
cursor_color_end = "\033[{}m".format(";".join([
    str(current_theme["editor_text"][2]),
    current_theme["editor_text"][0],
    str(int(current_theme["editor_text"][1])+10)
]))

# REWRITING PRINT()
fprint_string = ""

def fprint_dispatch():
    global fprint_string
    print(fprint_string)
    fprint_string = ""

def fprint(string="", end="\n"):
    global fprint_string
    fprint_string += string + end

def next_theme():
    global current_theme, current_theme_id, cursor_color, cursor_color_end
    if current_theme_id == len(themes_list)-1:
        current_theme_id = 0
        current_theme = themes[themes_list[0]]
    else:
        current_theme_id += 1
        current_theme = themes[themes_list[current_theme_id]]

    cursor_color = "\033[{}m".format(";".join([
        str(current_theme["editor_cursor"][2]),
        current_theme["editor_cursor"][0],
        str(int(current_theme["editor_cursor"][1]) + 10)
    ]))
    cursor_color_end = "\033[{}m".format(";".join([
        str(current_theme["editor_text"][2]),
        current_theme["editor_text"][0],
        str(int(current_theme["editor_text"][1]) + 10)
    ]))

def color_set(setting):
    if setting == "none":
        fprint("\033[0m", end="")
    else:
        settings = current_theme[setting]
        settings = str(settings[2]) + ";" + settings[0] + ";" + str(int(settings[1])+10)
        fprint("\033[{}m".format(settings), end='')

description = {
    "help": "\nUsage:\n\n> editor file_name [new/open] [file path]\n" + " " * 30 + " ^ W.I.P ^ "
}

try:
    if sys.argv[1] in ["-help", "-h"]:
        fprint(description["help"])
        exit()
except IndexError:
    fprint(description["help"])
    exit()

print("Please wait while we set things up for you!")
print("Loading...")

def file_exists(path, file_name):
    return os.path.exists(path + "\\" + file_name)

def get_file_name(file_name):
    return re.compile(r"([a-zA-Z0-9\. \(\)]*)\..*$").search(file_name).group(1)

def get_file_extension(file_name):
    return "." + re.compile(r"\.([a-zA-Z0-9]*)$").search(file_name).group(1)

def insert_to_filename(file_name, insert):
    return get_file_name(file_name) + insert + get_file_extension(file_name)

def get_free_file_number(file_name, path=os.getcwd()):
    if not file_exists(path, file_name):
        available_name = file_name
    else:
        available_name = None
    i = 1
    while available_name is None:
        test_name = insert_to_filename(file_name, " (" + str(i) +")")
        if not file_exists(path, test_name):
            available_name = test_name
        i += 1
    return available_name

def create_new_file_here(file_name):
    file_name = get_free_file_number(file_name)
    with open(file_name, "w") as f:
        f.write("")
    return file_name

def read_file_here(file_name):
    file_content = []
    x = 0
    with open(file_name, "r") as f:
        for line in f:
            x += 1
            file_content.append(line.rstrip())
    if x == 0:
        file_content.append("")
    return file_content

def write_file_here(filename, lines):
    with open(filename, "w") as f:
        f.close()
    with open(filename, "a") as f:
        for line in lines:
            f.write(line + "\n")

def read_option_file(filename):
    global is_toggle_help, fps
    try:
        with open(options_dir + filename) as f:
            for line in f:
                line = line.strip("\n")
                if line.startswith("is_toggle_help = "):
                    is_toggle_help = line.replace("is_toggle_help = ", "")
                    if is_toggle_help == "True":
                        is_toggle_help = True
                    else:
                        is_toggle_help = False
                elif line.startswith("debug.enabled = "):
                    if line.replace("debug.enabled = ", "") == "True":
                        debug.enable()
                    else:
                        debug.disable()
                elif line.startswith("fps = "):
                    fps = int(line.replace("fps = ", ""))
                elif line.startswith("current_theme = "):
                    selected_theme = line.replace("current_theme = ", "")
                    print(selected_theme)
                    x = 0
                    while selected_theme != themes_list[current_theme_id]:
                        next_theme()
    except FileNotFoundError:
        if is_toggle_help:
            is_toggle_help_string = "True"
        else:
            is_toggle_help_string = "False"
        write_file_here(options_dir + filename,
                        ["fps = " + str(fps),
                         "is_toggle_help = " + is_toggle_help_string,
                         "debug.enabled = " + debug.is_enabled_string(),
                         "current_theme = " + themes_list[current_theme_id]])

class SystemData:
    def __init__(self, argv):
        try:
            self.working_file = argv[1].lower()
        except IndexError:
            self.working_file = "New Text Document.txt"
        try:
            self.working_path = argv[3].lower()
        except IndexError:
            self.working_path = os.getcwd()
        try:
            if not argv[2].lower() in ["new", "open"]:
                print("You must mention what to do with the file\neditor {new/open}{file name} {file path}")
                exit()
            self.order = argv[2].lower()
        except IndexError:
            if file_exists(self.working_path, self.working_file):
                self.order = "open"
            else:
                self.order = "new"

    def get_currently_working_file(self):
        return self.working_file

    def change_currently_working_file(self, new_file):
        self.working_file = new_file

    def get_order(self):
        return self.order

    def get_currently_working_path(self):
        return self.working_path

class File:
    def __init__(self, file, path):
        self.file = file
        self.path = path
        self.lines = read_file_here(self.file)

    def get_line(self, line):
        return self.lines[line]

    def get_lines(self):
        return self.lines

    def new_line_after(self):
        global cursor_pos
        left = self.get_line(cursor_pos[1])[:cursor_pos[0]]
        right = self.get_line(cursor_pos[1])[cursor_pos[0]:]
        self.lines[cursor_pos[1]] = left
        self.lines.insert(cursor_pos[1] + 1, right)
        cursor_pos[1] += 1
        cursor_pos[0] = 0

    def add_character(self, character):
        global cursor_pos
        temp_line = list(self.get_line(cursor_pos[1]))

        temp_line.insert(cursor_pos[0], character)
        self.lines[cursor_pos[1]] = "".join(temp_line)
        cursor_pos[0] += 1

    def remove_line(self):
        global cursor_pos
        if cursor_pos[1] != 0:
            self.lines.pop(cursor_pos[1])
            cursor_pos[1] -= 1
        else:
            self.lines[0] = ""

    def remove_character(self):
        global cursor_pos
        if cursor_pos[0] != 0:
            temp_line = list(self.get_line(cursor_pos[1]))
            temp_line.pop(cursor_pos[0] - 1)
            self.lines[cursor_pos[1]] = "".join(temp_line)
            cursor_pos[0] -= 1
        elif cursor_pos[0] == 0 and cursor_pos[1] != 0:
            temp = self.get_line(cursor_pos[1])
            self.lines.pop(cursor_pos[1])
            cursor_pos[1] -= 1
            cursor_pos[0] = opened_file.line_length(cursor_pos[1])
            self.lines[cursor_pos[1]] += temp

    def remove_character_right(self):
        global cursor_pos
        if cursor_pos[1] == len(self.get_lines()) - 1 and cursor_pos[0] == len(self.get_line(cursor_pos[1])):
            # End of file, can't use DEL
            pass
        elif cursor_pos[0] == len(self.get_line(cursor_pos[1])):
            temp_line = list(self.get_line(cursor_pos[1]))
            temp_line.insert(cursor_pos[0], self.get_line(cursor_pos[1]+1))
            self.lines[cursor_pos[1]] = "".join(temp_line)
            self.lines.pop(cursor_pos[1]+1)
        else:
            temp_line = list(self.get_line(cursor_pos[1]))
            temp_line.pop(cursor_pos[0])
            self.lines[cursor_pos[1]] = "".join(temp_line)

    def line_length(self, line):
        return len(self.lines[line])

class Debug:
    def __init__(self):
        self.enabled = False
        self.feedback = []

    def is_enabled(self):
        return self.enabled

    def is_enabled_string(self):
        if self.enabled:
            return "True"
        else:
            return "False"

    def enable(self):
        self.enabled = True

    def disable(self):
        self.enabled = False

    def toggle(self):
        self.enabled = not self.enabled

    def display(self):
        color_set("debug")
        fprint("Debug mode enabled:")
        fprint("Fps: {}".format(fps))
        fprint("Cursor: ({}, {})".format(cursor_pos[0], cursor_pos[1]))
        fprint("Display: ({}, {})".format(display_pos[0], display_pos[1]))
        fprint("File name: {}".format(system_data.get_currently_working_file()))
        fprint("File path: {}".format(system_data.get_currently_working_path()))
        if writing:
            fprint("writing = True")
        else:
            fprint("writing = False")
        if options[0]:
            fprint("options = True")
        else:
            fprint("options = False")
        fprint("Theme: {} ({})".format(current_theme["name"], current_theme_id))
        fprint("\nFeed:")
        for feed in self.feedback:
            fprint(feed)

    def add_feedback(self, feed):
        self.feedback.append(feed)

def open_editor():
    global opened_file
    opened_file = File(system_data.get_currently_working_file(), system_data.get_currently_working_path())
    editor()

def cursor_move(direction):
    global cursor_pos
    if direction == "u" and cursor_pos[1] != 0:
        cursor_pos[1] -= 1
        if cursor_pos[0] >= opened_file.line_length(cursor_pos[1]):
            cursor_pos[0] = opened_file.line_length(cursor_pos[1])
    if direction == "d" and cursor_pos[1] != len(opened_file.get_lines()) - 1:
        cursor_pos[1] += 1
        if cursor_pos[0] >= opened_file.line_length(cursor_pos[1]):
            cursor_pos[0] = opened_file.line_length(cursor_pos[1])
    if direction == "l" and cursor_pos[0] != 0:
        cursor_pos[0] -= 1
    elif direction == "l" and cursor_pos[0] == 0 and cursor_pos[1] != 0:
        cursor_pos[1] -= 1
        cursor_pos[0] = opened_file.line_length(cursor_pos[1])
    if direction == "r" and cursor_pos[0] != opened_file.line_length(cursor_pos[1]):
        cursor_pos[0] += 1
    elif direction == "r" and cursor_pos[0] == opened_file.line_length(cursor_pos[1]) and cursor_pos[1] < len(opened_file.get_lines()) - 1:
        cursor_pos[1] += 1
        cursor_pos[0] = 0

def toggle_help():
    global is_toggle_help
    is_toggle_help = not is_toggle_help

def save_document():
    write_file_here(system_data.working_file, opened_file.get_lines())
    debug.add_feedback("File was save successful!")

def editor():
    global cursor_visible, opened_file, cursor_pos, is_toggle_help, writing, options
    global display_height, display_width, display_pos, fps
    fps = 10  # FPS
    is_toggle_help = False

    display_height = 49
    display_width = 190
    display_pos = [0, 0]

    time_stamps = {
        "print_time": 0,
    }
    key_collector = KeyCollector()
    start_new_thread(key_collector.start_recording, ())

    cursor_pos = [0, 0]
    cursor_visible = True
    writing = True
    options = [False, "0"]

    options_file = "options.txt"
    # Overwrite options
    read_option_file(options_file)

    while True:
        combo = key_collector.get_next_combo()
        if options[0]:
            if not isinstance(combo, list):
                if combo in list("1234567"):
                    options[1] = combo
            else:
                action = combo[0]
                if action == "ESC":
                    writing = True
                    options = [False, "0"]
                if action == "DOWN_ARROW" and int(options[1]) < 7:
                    options[1] = str(int(options[1]) + 1)
                if action == "UP_ARROW" and int(options[1]) > 1:
                    options[1] = str(int(options[1]) - 1)
                if action == "ENTER":
                    if options[1] == "1":
                        color_set("none")
                        if is_toggle_help:
                            is_toggle_help_string = "True"
                        else:
                            is_toggle_help_string = "False"
                        write_file_here(options_dir + options_file,
                                        ["fps = " + str(fps),
                                         "is_toggle_help = " + is_toggle_help_string,
                                         "debug.enabled = " + debug.is_enabled_string(),
                                         "current_theme = " + themes_list[current_theme_id]])
                        os.system("mode con: cols=81 lines=81")
                        exit()
                    if options[1] == "2":
                        save_document()
                    if options[1] == "3":
                        toggle_help()
                    if options[1] == "4":
                        debug.toggle()
                    if options[1] == "5":
                        if fps < 60:
                            fps += 1
                    if options[1] == "6":
                        if fps > 1:
                            fps -= 1
                    if options[1] == "7":
                        next_theme()

        elif writing:
            # If key stroke
            if not isinstance(combo, list):
                if combo != "":
                    opened_file.add_character(combo)
            # If action request
            else:
                action = combo[0]
                if action == "F1":
                    debug.toggle()
                if action == "F2":
                    next_theme()
                if action == "F3":
                    toggle_help()
                if action == "ENTER":
                    opened_file.new_line_after()

                # "ctrl+backspace" should do opened_file.remove_line() <-- Add it later
                if action == "BACKSPACE":
                    opened_file.remove_character()

                if action == "DELETE":
                    opened_file.remove_character_right()

                if action == "ESC":
                    writing = False
                    options = [True, 0]

                for arrow_key in ["UP", "DOWN", "LEFT", "RIGHT"]:
                    if action == "{}_ARROW".format(arrow_key):
                        cursor_move(arrow_key[:1].lower())

        if current_time() - time_stamps["print_time"] > round(1 / fps * 1000):
            time_stamps["print_time"] = current_time()
            display_tick()
            cursor_visible = not cursor_visible

def current_time():
    return int(round(time.time() * 1000))

def display_tick():
    global opened_file, is_toggle_help
    os.system("cls")
    if is_toggle_help:
        color_set("help_line")
        fprint("-"*96, end="")
        color_set("help_title")
        fprint("[ HELP ]", end="")
        color_set("help_line")
        fprint("-"*96,)

        help_pop_up = [
            "Welcome to my little CMD Editor. The original idea came to me on 10th of January",
            "I was able to make the application usable on 13th of January as writing, saving and editing was added.",
            "Later I added themes to add colors and here we are. This is what you are using now!",
            "I appreciate it if you are currently using the editor!"
        ]
        color_set("help_text")
        for line in help_pop_up:
            fprint("  " + line + " "*(76-len(line)) + " ")
        fprint()
    color_set("esc")
    fprint("[ESC]", end="")
    color_set("toolbar_line")
    fprint("="*90, end="")
    color_set("toolbar_title")
    fprint("[ Editor ]", end="")
    color_set("toolbar_line")
    fprint("="*95)
    top_menu = [
        "1) EXIT (no save)     4) Toggle Debug [F1]   7) Next Theme [F2]",
        "2) Save               5) Increase fps",
        "3) Toggle Help [F3]   6) Decrease fps"

    ]
    color_set("toolbar_text")
    if options[0]:
        fprint()
        selected_top_menu = []
        for line in top_menu:
            selected_top_menu.append(line.replace(str(options[1])+")", "->"))
        for line in selected_top_menu:
            fprint("  " + line + " "*(76-len(line)) + " ")
        fprint("_"*200)
    fprint()

    # Display adjustment
    # <- width check ->
    if display_pos[0] == 0 and cursor_pos[0] == 0:
        pass
    elif cursor_pos[0] >= display_pos[0] + display_width:
        display_pos[0] = cursor_pos[0] - display_width
    elif cursor_pos[0] <= display_pos[0]:
        display_pos[0] = cursor_pos[0]
    # /\ height check \/
    if display_pos[1] == 0 and cursor_pos[1] == 0:
        pass
    elif cursor_pos[1] >= display_pos[1] + display_height:
        display_pos[1] = cursor_pos[1] - display_height
    elif cursor_pos[1] <= display_pos[1]:
        display_pos[1] = cursor_pos[1]

    for i, line in enumerate(opened_file.get_lines()):
        if i < display_pos[1]:
            continue
        if i > display_pos[1] + display_height:
            continue
        line = line[display_pos[0]:]
        if display_width < len(line):
            line = line[:display_width]

        number_index_width = 5
        number_index = str(i) + " " * (number_index_width - len(str(i)))
        if cursor_pos[1] == i and cursor_visible:
            temp = list(line)
            if writing:
                while True:
                    try:
                        temp[cursor_pos[0]-display_pos[0]] = cursor_color + "<" + cursor_color_end
                        break
                    except IndexError:
                        temp.append(" ")
            color_set("editor_line_number")
            fprint(number_index, end="")
            color_set("editor_line_side")
            fprint("> ", end="")
            color_set("editor_text")
            fprint("".join(temp))
        else:
            color_set("editor_line_number")
            fprint(number_index, end="")
            color_set("editor_line_side")
            fprint("> ", end="")
            color_set("editor_text")
            fprint(line)
    fill_lines = (display_height-len(opened_file.get_lines()))
    if fill_lines > 0:
        fprint("\n" * fill_lines, end="")
    color_set("editor_bottom")
    fprint("\n" + "#"*200)

    bottom_credits = [
        "CMD text editor developed by Artur Wagner",
        "Jan 2019"
    ]
    color_set("credits")
    fprint()
    for line in bottom_credits:
        space_right = (201 - len(line))/2
        space_left = space_right - 1
        if isinstance(len(line)/2, float):
            fprint(round(space_left) * " " + line + " " * round(space_right))
        else:
            fprint(int(space_left) * " " + line + "" * int(space_right))
    color_set("debug")
    if debug.is_enabled():
        debug.display()
    color_set("none")
    fprint_dispatch()

def main():
    global system_data, debug
    os.system("mode con: cols=201 lines=81")
    system_data = SystemData(sys.argv)
    debug = Debug()
    order = system_data.get_order()
    if order == "new":
        system_data.change_currently_working_file(
            create_new_file_here(system_data.get_currently_working_file())
        )
    open_editor()

main()


from lib.ip2region.ip2Region import Ip2Region
import os

dbFile = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ip2region.db')

def ip2region(line):
    searcher = Ip2Region(dbFile)
    algorithm = 'memory'

    line = line.strip()

    if line == "":
        print("[Error]: Invalid ip address.")

    if line == "quit":
        print("[Info]: Thanks for your use, Bye.")

    if not searcher.isip(line):
        print("[Error]: Invalid ip address.")

    try:

        if algorithm == "binary":
            data = searcher.binarySearch(line)
        elif algorithm == "memory":
            data = searcher.memorySearch(line)
        else:
            data = searcher.btreeSearch(line)
        # print("%s|%s in %5f millseconds" % (data["city_id"], data["region"].decode('utf-8')))
    except Exception as e:
        print("[Error]: %s" % e)

    searcher.close()

    data_list = data['region'].decode('utf-8').split('|')

    country = data_list[0]

    if data_list[3] == '0' :
        region = data_list[2]
        city = None
        isp = None
    else:
        region = data_list[2]
        city = data_list[3]
        isp = data_list[4]

    data_dict = {
        'country':country,
        'region':region,
        'city':city,
        'isp':isp
    }

    return data_dict

if __name__ == '__main__':
    # print(ip2region('107.182.21.166'))
    # print(ip2region('39.106.4.32'))
    testip = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'iplist.txt')
    with open(testip) as f:
        for i in f:
            print(ip2region(i))


# This script invokes blender to import and save external model formats as
# .blend files to be processed further.
#
# Example usage for this importer:
#  blender --background --factory-startup --python $HOME/import_3ds.py -- \
#          --i="/tmp/hello.3ds" \
#          --o="/tmp/hello.blend" \
#
# See blender --help for details.

import bpy

# Imports a file using importer
def import_file(file_path):
    # Import the model
    bpy.ops.import_scene.autodesk_3ds(filepath = file_path)

# Clear existing objects.
def clear_scene():
    scene = bpy.context.scene
    scene.camera = None
    for obj in scene.objects:
        scene.objects.unlink(obj)

# Save current scene as .blend file
def save_file(save_path):
    # Check if output file exists already
    try:
        f = open(save_path, 'w')
        f.close()
        ok = True
    except:
        print("Cannot save to path %r" % save_path)

        import traceback
        traceback.print_exc()

    # Save .blend file
    if ok:
        bpy.ops.wm.save_as_mainfile(filepath=save_path)

def main():
    import sys       # to get command line args
    import argparse  # to parse options for us and print a nice help message

    # get the args passed to blender after "--", all of which are ignored by
    # blender so scripts may receive their own arguments
    argv = sys.argv

    if "--" not in argv:
        argv = []  # as if no args are passed
    else:
        argv = argv[argv.index("--") + 1:]  # get all args after "--"

    # When --help or no args are given, print this help
    usage_text = \
    "Run blender in background mode with this script:"
    "  blender --background --factory-startup --python " + __file__ + " -- [options]"

    parser = argparse.ArgumentParser(description=usage_text)

    # Possible types are: string, int, long, choice, float and complex.
    parser.add_argument("-i", "--input", dest="file_path", metavar='FILE',
            help="Import the specified file")
    parser.add_argument("-o", "--output", dest="save_path", metavar='FILE',
            help="Save the generated file to the specified path")

    args = parser.parse_args(argv)  # In this example we wont use the args

    if not argv:
        parser.print_help()
        return

    # Run the conversion
    clear_scene()
    import_file(args.file_path)
    save_file(args.save_path)

    print("batch job finished, exiting")

if __name__ == "__main__":
    main()


import argparse
import os,sys,inspect
import copy
from enum import Enum

from argg_hdl.argg_hdl_base import *
from argg_hdl.argg_hdl_v_symbol import *
from argg_hdl.argg_hdl_v_Package import *
from argg_hdl.argg_hdl_v_class import *

class counter_state(Enum):
    idle    = 0
    running = 1
    done    = 2

class time_span(v_class):
    def __init__(self, CounterLength):
        super().__init__("time_span"+ str(CounterLength))
        self.__v_classType__         = v_classType_t.Record_t
        self.min  = v_slv(CounterLength)
        self.max  = v_slv(CounterLength)

class counter(v_class):
    def __init__(self, CounterLength):
        super().__init__("counter_"+ str(CounterLength))
        self.__v_classType__         = v_classType_t.Slave_t
        AddDataType(  time_span(CounterLength)  )
        self.state = v_enum(counter_state)
        self.Count = v_slv(CounterLength)
        self.MaxCount = v_slv(CounterLength)

    def _onPull(self):
        if self.isRunning():
            self.Count <<  self.Count +1

        if self.isRunning() and self.Count >= self.MaxCount:
            self.state << counter_state.done
            self.Count << 0

    def StartCountTo(self, MaxCount= port_in(v_slv())):
        if self.isReady():
            self.state << counter_state.running
            self.MaxCount << MaxCount
            self.Count << 0

    def StartCountFromTo(self,MinCount= port_in(v_slv()), MaxCount= port_in(v_slv())):
        if self.isReady():
            self.state << counter_state.running
            self.MaxCount << MaxCount
            self.Count << MinCount

    def stopCounter(self):
        self.state << counter_state.done

    def isReady(self):
        return self.state == counter_state.idle

    def isRunning(self):
        return self.state == counter_state.running

    def isDone(self):
        return self.state == counter_state.done

    def reset(self):
        self.state << counter_state.idle
        self.Count << 0

    def InTimeWindowSLV(self,TimeMin=port_in(v_slv()),TimeMax=port_in(v_slv()), DataIn=port_in(v_slv())):

        DataOut=v_slv("DataIn'length")
        DataOut << 0
        if self.isRunning() and TimeMin <= self.Count and self.Count < TimeMax:
            DataOut << DataIn

        return DataOut

    def InTimeWindowSl(self,TimeMin=port_in(v_slv()),TimeMax=port_in(v_slv())):
        DataOut=v_sl()
        DataOut << 0
        if self.isRunning() and TimeMin <= self.Count and self.Count < TimeMax:
            DataOut << 1
        return DataOut

    def InTimeWindowSLV_r(self,TimeSpan=port_in(dataType()), DataIn=port_in(v_slv())):

        DataOut=v_slv("DataIn'length")
        DataOut << 0
        if self.isRunning() and TimeSpan.min <= self.Count and self.Count < TimeSpan.max:
            DataOut << DataIn

        return DataOut

    def InTimeWindowSl_r(self,TimeSpan=port_in(dataType())):
        DataOut=v_sl()
        DataOut << 0
        if self.isRunning() and TimeSpan.min <= self.Count and self.Count < TimeSpan.max:
            DataOut << 1
        return DataOut


#
# Copyright (c) 2016 NORDUnet A/S
# All rights reserved.
#
#   Redistribution and use in source and binary forms, with or
#   without modification, are permitted provided that the following
#   conditions are met:
#
#     1. Redistributions of source code must retain the above copyright
#        notice, this list of conditions and the following disclaimer.
#     2. Redistributions in binary form must reproduce the above
#        copyright notice, this list of conditions and the following
#        disclaimer in the documentation and/or other materials provided
#        with the distribution.
#     3. Neither the name of the NORDUnet nor the names of its
#        contributors may be used to endorse or promote products derived
#        from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

from flask import request, session, redirect, abort
from flask import current_app, Blueprint

from eduid_common.authn.utils import get_location
from eduid_common.authn.eduid_saml2 import get_authn_request, get_authn_response
from eduid_common.authn.eduid_saml2 import authenticate
from eduid_webapp.authn.acs_registry import get_action, schedule_action

import logging
logger = logging.getLogger(__name__)

authn_views = Blueprint('authn', __name__)

@authn_views.route('/login')
def login():
    """
    login view, redirects to SAML2 IdP
    """
    redirect_url = current_app.config.get('SAML2_LOGIN_REDIRECT_URL', '/')
    came_from = request.args.get('next', redirect_url)
    idp = session.get('idp', None)
    idp = request.args.get('idp', idp)
    authn_request = get_authn_request(current_app.config,
                                      session, came_from, idp)
    schedule_action('login-action')
    logger.debug('Redirecting the user to the IdP')
    return redirect(get_location(authn_request))

@authn_views.route('/saml2-acs', methods=['POST'])
def assertion_consumer_service():
    """
    Assertion consumer service, receives POSTs from SAML2 IdP's
    """
    if 'SAMLResponse' not in request.form:
        abort(400)
    xmlstr = request.form['SAMLResponse']
    session_info = get_authn_response(current_app.config, session, xmlstr)
    logger.debug('Trying to locate the user authenticated by the IdP')

    user = authenticate(current_app, session_info)
    if user is None:
        logger.error('Could not find the user identified by the IdP')
        raise HTTPUnauthorized("Access not authorized")

    action = get_action()
    return action(session_info, user)