""""
10、10分
问答题
一个数如果恰好等于它的因子之和，这个数就称为“完数”。
例如6=1＋2＋3.
编程找出1000以内的所有完数。

理解：不包含本身（1，2，3），因数包含本身（1，2，3，6）
"""""

# dict_test = {}
# dict_test.update(i=sum_divisor)
# dict_test.update({i: sum_divisor})
# dict_test[i] = divisors

result = []

for i in range(1000):
    # empty is False, not empty is True
    divisors = []
    for j in range(1, i):
        if i % j == 0:
            divisors.append(str(j))

    if divisors and eval("+".join(divisors)) == i:
        result.append("{}={}".format(i, "+".join(divisors)))

print("结果如下：")
for element in result:
    print(element)


from requests import get
from functools import reduce
from data.text_utils import remove_unwanted, remove_special_characters

def get_subreddit_top_posts(subreddit):
    """
    inputs:
        subreddit - a subreddit name as a string
    returns:
        post_urls - a list of the urls of top posts in subreddit
    """
    base_url = "https://www.reddit.com/r/"
    top = "/top.json"
    user_agent = {'User-agent': 'degenbot 1.0'}
    resp = get(base_url + subreddit + top, headers = user_agent)
    if resp.status_code != 200:
        print("ERROR: Could not complete Reddit request. ERROR CODE: " + str(resp.status_code))
        return
    posts = resp.json().get('data',{}).get('children')
    if not posts:
        print("ERROR: No valid posts returned from Reddit request.")
        return
    # Map data to text of post
    post_urls = list(map(lambda post: post.get('data',{}).get('url'), posts))
    # Remove posts with no text
    post_urls = list(filter(lambda url: url, post_urls))
    return post_urls

def get_comment_text(comment):
    """
    inputs:
        comment - a dict that expects comment content
                  as a string in comment['data']['body']
    returns:
        valid_comment_text - the comment contend without undesired characters
                             as a list of words
    """
    if not comment:
        return []
    comment_text = comment.get("data",{}).get("body")
    if not comment_text:
        return []
    comment_text = comment_text.lower()
    comment_text = remove_unwanted(comment_text)
    comment_text_list = comment_text.split(".")
    comment_text_list = list(map(lambda text: text.split(" "), comment_text_list))
    valid_comment_text = list(filter(lambda comment: len(comment)>5, comment_text_list))
    return valid_comment_text

def get_post_and_comment_text(post_url):
    user_agent = {'User-agent': 'degenbot 1.0'}
    resp = get(post_url + "comments.json", headers = user_agent)
    if resp.status_code != 200:
        print("ERROR: Could not find comments for post "+ post_url +". ERROR CODE: " + str(resp.status_code))
        return
    try:
        post = resp.json()[0]
        comments =  resp.json()[1]
    except:
        print("ERROR: Invalid response type for "+ post_url +".")
        return
    comments = comments.get("data",{}).get("children")
    comment_texts = list(map(get_comment_text,comments))
    post_text = post.get("data",{}).get("selftext")
    if post_text:
        post_text = post_text.lower()
        post_text = remove_unwanted(post_text)
        post_text = post_text.split(".")
        post_text = list(map(lambda text: text.split(" "), post_text))
    else:
        post_text = []
    content_text = post_text + comment_texts
    valid_content_text = list(filter(lambda text: text, content_text))
    if not valid_content_text:
        return
    valid_content_text = list(reduce(lambda s1, s2: s1+s2 , valid_content_text))
    valid_content_text = list(map(remove_special_characters, valid_content_text))
    return valid_content_text

def get_subreddit_content(subreddit):
    post_urls = get_subreddit_top_posts(subreddit)
    content_texts = list(map(get_post_and_comment_text, post_urls))
    content_texts = list(filter(lambda text: text, content_texts))
    if not content_texts:
        print("ERROR: No usable content from r/" + subreddit)
        return []
    content = list(reduce(lambda s1, s2: s1+s2, content_texts))
    return content

if __name__ == "__main__":
    content = get_subreddit_content("wallstreetbets")
    print(content[0])


# This script will calculate the average, standard deviation, correlation fuction and statistical inefficiency of a property A with respect to time.

import numpy as np
import matplotlib.pylab as plt

# input file
filename = 'v.data'
data = np.loadtxt(filename);
# Declare variables
nbr_of_k = 100
M = 400
begin_calc = 400
totCorr = []

# Correlation function
for k in range(1,nbr_of_k):
    tmp = 0
    for i in range(1,len(data[0,:])):
        tmp = tmp+(np.corrcoef(data[begin_calc:begin_calc+M-k,i],data[begin_calc+k:begin_calc+M,i]))[0,1]

    totCorr.extend([tmp/len(data[1,:])])

# initial size of plot window
plt.rcParams['figure.figsize'] = 20, 12

# plot
plt.subplot(1,2,1)
plt.subplots_adjust(wspace=0.3)
plt.plot(range(1,nbr_of_k),totCorr/totCorr[0] ,'-')

# labels
plt.xlabel('k', fontsize=20)
plt.ylabel('totCorr', fontsize=20)

# tick fontsize
plt.xticks(fontsize=10)
plt.yticks(fontsize=10)

# display the plot
plt.show()


# -*- coding: utf-8 -*-
"""
Adam Ano - 101110983
Prendi Bobo - 101187580
Stefan Maric - 101208175
"""

class Customer:

    def __init__(self,customerId,fName,lName,ph):
        self.customerId=customerId
        self.fName=fName
        self.lName=lName
        self.ph=ph

    def customerId(self):
        return self.customerId

    def fName(self):
        return self.fName

    def lName(self):
        return self.lName

    def ph(self):
        return self.ph

class Flight():

    def __init__(self,flightNo,orig,dest,nSeats=50):#nSeat default to 50 if they havent entered a number
        self.flightNo=flightNo
        self.orig=orig
        self.dest=dest
        self.nSeats=nSeats
        self.numPass=0

    def getFlightNo(self):
        return self.flightNo

    def getOrig(self):
        return self.orig

    def getDest(self):
        return self.dest

    def getNSeats(self):
        return self.nSeats

    def getNumPass(self):
        return self.numPass

    def __str__(self):
        return "Flight Number: {}\nOrigin: {}\nDestination: {}\nNumber of Seats: {}\nNumber of Passengers: {}".format(self.flightNo,self.orig,self.dest,self.nSeats,self.numPass)

def menu():

    print("  Welcome to GBC Airlines")
    print("  1. Add Customer ")
    print("  2. Add Flight ")
    print("  3. Add Booking ")
    print("  4. View Flights ")
    print("  5. View Customer ")
    print("  6. View Flight ")
    print("  7. Exit ")

def main():
    menu()
    customerList=[]
    flightList=[]
    loop=True
    while loop:

        user=int(input("Please enter the following options: "))

        if user==1:
            print("Adding customer...")
            customerId=int(input("Please enter your customer Id: "))
            fName=input("Please enter your first name: ")
            lName=input("Please enter your last name: ")
            ph=input("Please enter your phone number: ")

            customerList.append(Customer(customerId,fName,lName,ph))

            print("You have been added...\n")

        elif user==2:
            print("Adding Flight...")
            flightNo=int(input("Please enter the flight number: "))
            orig=input("Please enter the origin: ")
            dest=input("Pleas enter the destination: ")
            nSeat=int(input("Please enter the number of seats: "))

            flightList.append(Flight(flightNo,orig,dest,nSeat))

            print("Flight added...\n")

        elif user==3:
            print("Add Booking...\n")
            print("List of Flights")
            for obj in flightList:
                print("\nFlight Number: {}\nOrigin: {}\nDestination: {}\nNumber of Seats: {}\n".format(obj.flightNo,obj.orig,obj.dest,obj.nSeats))
                print("----")

            print("List of Customers")
            for obj in customerList:
                print("\nCustomer ID:{}\nFirst Name: {}\nLast Name: {}\nPhone Number: {}\n".format(obj.customerId,obj.fName,obj.lName,obj.ph))
                print("----")

            userId=int(input("Please enter your customer ID: "))
            flightId=int(input("Please enter the flight ID: "))
            for obj in customerList:

                if userId==obj.customerId:
                     print("\nCustomer Id Found\n")

                else:
                    print("\nCustomer Id not found\n")

            for obj in flightList:
                if flightId==obj.flightNo:
                     print("Flight Id found\n")

                else:
                    print("Flight Id not found\n")

            if obj.nSeats>0:
                print("\nCustomer has been Added\n")
                obj.nSeats+=1
            else:
                print("All flight seats are taken\n")

        elif user==4:
            if len(flightList) > 0:
                print("View flights...")
                for obj in flightList:
                    print("\nFlight Number: {}\nOrigin: {}\nDestination: {}\nNumber of Seats: {}\n".format(obj.flightNo,obj.orig,obj.dest,obj.nSeats))
                    print("----")
            else:
                print("No flights found...")
        elif user==5:
            if len(customerList) > 0:
                print("View customers...")
                for obj in customerList:
                    print("\nCustomer ID:{}\nFirst Name: {}\nLast Name: {}\nPhone Number: {}\n".format(obj.customerId,obj.fName,obj.lName,obj.ph))
                    print("----")
            else:
                print("No customers found...")
        elif user==6:
            if len(flightList) > 0:

                flightId=int(input("Please enter flight Id: "))

                for obj in flightList:
                    #print("Flight Number: {}\n".format(obj.flightNo))

                    if flightId==obj.flightNo:
                        print("\nFlight Number: {}\nOrigin: {}\nDestination: {}\nNumber of Seats: {}\n".format(obj.flightNo,obj.orig,obj.dest,obj.nSeats))
                    else:
                        print("Flight Number not found\n")
            else:
                print("No flights found... Please book one")

        elif user==7:
            print("Now exiting...")
            loop=False
            break

        else:
            print("Please enter a valid option")
            break

        menu()

    """
    TESTING OBJECT!!!

    f1=Flight(1234,"Point A","Point b")
    print (f1)
    """
if __name__=='__main__':
    main()


from django.urls import path
from . import views

urlpatterns = [
    path('', views.Home.as_view(), name='home'),
    path('about/', views.about, name='about'),
    path('tasks/', views.tasks_index, name='tasks_index'),
    path('tasks/<int:task_id>/', views.tasks_detail,
    name='tasks_detail'),
    path('tasks/create/', views.TaskCreate.as_view(),
    name='tasks_create'),
    path('tasks/<int:pk>/update/', views.TaskUpdate.as_view(),
    name='tasks_update'),
    path('tasks_<int:pk>/delete/', views.TaskDelete.as_view(),
    name='tasks_delete'),
    path('accounts/signup', views.signup, name='signup'),
]


def str_match(string, pattern):
    table = [[False]*(len(string)+1) for item in range(len(pattern)+1)]
    table[0][0] = True
    for i in range(2, len(pattern) + 1):
        table[i][0] = table[i - 2][0] and pattern[i - 1] == '*'
    for i in range(1, len(pattern)+1):
        for j in range(1, len(string)+1):
            if pattern[i - 1] != "*":
                table[i][j] = table[i-1][j-1] and\
                              (string[j-1] == pattern[i-1] or pattern[i-1] == ".")
            else:
                table[i][j] = table[i-2][j] or table[i-1][j]
                if pattern[i-2] == string[j-1] or pattern[i-2] == ".":
                    table[i][j] |= table[i][j-1]
    return table[-1][-1]

if __name__ == "__main__":
    p = "ab*ac*a"
    s = "aaa"
    print(str_match(s, p))


from os.path import isdir, join
from os import listdir

def walksimply(dir_):
    for e in sorted(listdir(dir_)):
        j = join(dir_, e)
        if isdir(j):
            for y in walksimply(j):
                yield y
        else:
            yield e, j


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import argparse
from .core import core
from .include import Include
from .exceptions import IncludeError
# TODO: Improved error handling.

def _input_file_argument(parser):
    # Input file command line argument.
    input_file_kwargs = {
        "type": str,
        "default": core.default_input_file,
        "metavar": "FILE",
        "help": "python input file",
    }
    parser.add_argument("-f", "--file", **input_file_kwargs)

def _parse_primary_arguments(argv):
    # Parse primary options from command line arguments.
    # Create argument parser and add primary arguments.
    parser = argparse.ArgumentParser(prog=core.name, add_help=False)
    version_argument(parser)
    _input_file_argument(parser)
    # Add target arguments to prevent catching target options.
    target_arguments(parser)

    # Parse known arguments, unhandled arguments passed to secondary parser.
    args, discard = parser.parse_known_args(argv)

    # Returns the input file.
    return args.file

def _parse_secondary_arguments(argv, path):
    # Parse secondary options from command line arguments.
    # Get description from module docstring or fallback on core.
    desc = Include.get_description(path)
    desc = core.description if desc is None else desc

    # Create argument parser, add primary and secondary arguments.
    parser = argparse.ArgumentParser(prog=core.name, description=desc)
    version_argument(parser)
    _input_file_argument(parser)
    logging_arguments(parser)
    # Use included rules in target arguments.
    target_arguments(parser, [key for key in Include.rules()])

    # Parse all arguments, throw exception on unknown.
    args = parser.parse_args(argv)

    # Internal core configuration.
    core.configure(args)

    # Returns argument parser, target name and remainder arguments.
    return (parser, args.target, args.argv)

def _parse_target_arguments(target, rule, argv):
    # Parse target command line arguments using options.
    # Construct program name using target.
    prog = "{} {}".format(core.name, target)
    desc = rule._rule.description

    # Create argument parser for options with target rule description.
    parser = argparse.ArgumentParser(prog=prog, description=desc)

    # Add rule options to parser.
    rule._rule.add_options(parser)

    # Parser remainder argumments using constructed parser.
    args = parser.parse_args(argv)

    # Call rule options with parsed arguments.
    rule._rule.call_options(args)

def version_argument(parser, core=core):
    """Version information command line option, allows override of the core
    class instance where required.

    `parser`
        Instance of ArgumentParser.
    `core`
        Package core instance.
    """
    version_kwargs = {
        "action": "version",
        "version": str("%(prog)s (" + core.version + ")"),
    }
    parser.add_argument("-v", "--version", **version_kwargs)

def logging_arguments(parser, core=core):
    """Logging command line arguments, allows override of the core class
    instance where required.

    `parser`
        Instance of ArgumentParser.
    `core`
        Package core instance.
    """
    # Logging level option.
    log_level_kwargs = {
        "type": str,
        "default": core.DEFAULT_LOG_LEVEL,
        "metavar": "LEVEL",
        "help": "logging level",
    }
    parser.add_argument("-l", **log_level_kwargs)

    # Logging file option.
    log_file_kwargs = {
        "type": str,
        "default": core.DEFAULT_LOG_FILE,
        "metavar": "FILE",
        "help": "logging file",
    }
    parser.add_argument("--log-file", **log_file_kwargs)

def target_arguments(parser, targets=None):
    """Target command line arguments.

    `parser`
        Instance of ArgumentParser.
    """
    # Target name and remainder arguments.
    target_kwargs = {
        "nargs": "?",
        "type": str,
        "help": "target name"
    }
    # Add targets as choices if available.
    if targets is not None:
        target_kwargs["choices"] = targets
    parser.add_argument("target", **target_kwargs)

    # Target remainder arguments.
    argv_kwargs = {
        "nargs": argparse.REMAINDER,
        "help": "target arguments"
    }
    parser.add_argument("argv", **argv_kwargs)

def main(argv=sys.argv[1:], **kwargs):
    """Main command line interface.

    `argv`
        Command line arguments list excluding script name.
    `**kwargs`
        Keyword arguments to internal package core configuration.
    """
    # Default standard output/error streams.
    kwargs.setdefault("stdout", sys.stdout)
    kwargs.setdefault("stderr", sys.stderr)

    # Internal configuration.
    core(**kwargs)

    # Parse primary command line arguments, this supports the `-f/--file`
    # option to load a specified python input file. If not specified the
    # default input file is imported: `$PWD/faffin.py`.
    input_file = _parse_primary_arguments(list(argv))

    # Load available rules from input file using `Include` class.
    try:
        Include(input_file)
    # Catch exceptions for user readability.
    except IncludeError as err:
        # Continue execution to handle help flag.
        core.stderr(err.message)

    # Parse secondary command line arguments, these may depend on rules which
    # have been imported from the input file. Returns the constructed argument
    # parser, target name and remainder arguments.
    parser, target, argv = _parse_secondary_arguments(argv, input_file)

    # If target is not specified, attempt to default to `default`.
    if (target is None) and (Include.get_rule("default") is not None):
        target = "default"

    # If no target, print usage.
    if target is None:
        parser.print_usage()
        return 0

    # Get rule from `Include` class.
    rule = Include.get_rule(target)
    # Write stderr and return if not found.
    if rule is None:
        core.stderr("unknown rule `{}`".format(target))
        return 1

    # Target exists, parse target arguments.
    _parse_target_arguments(target, rule, argv)

    # Call target rule object.
    # TODO: Catch FaffError exceptions.
    success, results = rule()

    # Report information to stdout or stderr based on success.
    info = "`{}` updated ({}/{} {:.3f}s)"
    stream = core.stdout if success else core.stderr
    stream(info, target, results["updated"], results["total"], results["time"])

    # Return code based on rule success.
    return int(success is False)


import sys
from sys import argv
import math

# Count the overall unique words
global Vocabulary, dic_vocabulary
Vocabulary = 0
dic_vocabulary = {}

# Count the number and possibility of lib or con documents
global num_lib_doc, num_con_doc, p_lib_doc, p_con_doc
num_lib_doc = 0
num_con_doc = 0
p_con_doc = 0.0
p_lib_doc = 0.0

# Check whether a word appears and the value is the number of occurance
global dic_lib_train, dic_con_train
dic_lib_train = {}
dic_con_train = {}

# Count the overall amout of words in con and lib
global N_con_words, N_lib_words
N_lib_words = 0
N_con_words = 0

def main():

	# Declare global variables
	global Vocabulary, num_lib_doc, num_con_doc, dic_lib_train, dic_con_train
	global p_lib_doc, p_con_doc, N_con_words, N_lib_words

	# 1st argv is this python filename, 2nd argv is train, 3rd argv is test
	# Might misssing 3rd argv

	# Get file names
	train_file_name = sys.argv[1]

	# Read training data
	read_train_data(train_file_name)

	if (num_lib_doc + num_con_doc) == 0:
		print("Number of both lib and con is 0!")
		exit(-1)

	p_lib_doc = math.log(num_lib_doc * 1.0 / (num_lib_doc + num_con_doc))
	p_con_doc = math.log(num_con_doc * 1.0 / (num_lib_doc + num_con_doc))

	# Get the overall number of dics
	for key in dic_con_train:
		N_con_words += dic_con_train[key]
	for key in dic_lib_train:
		N_lib_words += dic_lib_train[key]

	# print(N_con_words)
	# print(N_lib_words)
	# print(dic_lib_train)
	# print(dic_con_train)
	# print(len(dic_lib_train.keys()))
	# print(Vocabulary)

	# Change the value of dics from number to possibilities
	# To make it easier to compute, just save the log value
	non_con = math.log(1.0) - math.log(N_con_words + Vocabulary)
	for key in dic_vocabulary:
		if dic_con_train.has_key(key):
			dic_con_train[key] = math.log(dic_con_train[key] + 1.0) - math.log(N_con_words + Vocabulary)
		else:
			dic_con_train[key] = non_con
		# print(key + ": " + str(dic_new_con[key]))
	non_lib = math.log(1.0) - math.log(N_lib_words + Vocabulary)
	for key in dic_vocabulary:
		if dic_lib_train.has_key(key):
			dic_lib_train[key] = math.log(dic_lib_train[key] + 1.0) - math.log(N_lib_words + Vocabulary)
		else:
			dic_lib_train[key] = non_lib

	# Compute the new value
	dic_lib_new = {}
	dic_con_new = {}
	for key in dic_con_train:
		dic_con_new[key] = dic_con_train[key] - dic_lib_train[key]
		dic_lib_new[key] = dic_lib_train[key] - dic_con_train[key]

	# Get the top 20 words and print
	i = 0
	for key in sorted(dic_lib_new, key = dic_lib_new.get, reverse = True):
		if i < 20:
			temp = dic_lib_new[key]
			temp = round(temp, 4)
			print(key + " " + str(temp))
			i += 1
		else:
			break

	print("")

	i = 0
	for key in sorted(dic_con_new, key = dic_con_new.get, reverse = True):
		if i < 20:
			temp = dic_con_new[key]
			temp = round(temp, 4)
			print(key + " " + str(temp))
			i += 1
		else:
			break

# read drom training data
def read_train_data(filename):

	# Declare global variables
	global num_con_doc, num_lib_doc

	f = open(filename, 'r')

	while 1:
		line = str(f.readline()).strip()
		if not line:
			break

		# If the filename contains lib
		if line.find("lib") != -1:
			num_lib_doc += 1
			read_train_file(line, 0)

		# If the filename contains con
		elif line.find("con") != -1:
			num_con_doc += 1
			read_train_file(line, 1)

		# Illegal filename, continue
		else:
			continue

# Read files, if option == 0, then means it is reading lib training data
# 			  if option == 1, then means it is reading con training data
def read_train_file(filename, option):

	# Declare global variables
	global dic_lib_train, dic_con_train, Vocabulary, dic_vocabulary

	f = open(filename, 'r')

	while 1:
		line = str(f.readline()).strip()
		if not line:
			break

		line = line.lower()

		# Update vocabulary and its dictionary
		if not dic_vocabulary.has_key(line):
			dic_vocabulary[line] = 1
			Vocabulary += 1

		# Reading training lib data now
		if option == 0:
			if not dic_lib_train.has_key(line):
				dic_lib_train[line] = 1
			else:
				dic_lib_train[line] = dic_lib_train[line] + 1

		# Reading training con data now
		elif option == 1:
			if not dic_con_train.has_key(line):
				dic_con_train[line] = 1
			else:
				dic_con_train[line] = dic_con_train[line] + 1

		# Illegal option
		else:
			print("ERROR: Illegal option in read_lib_file")
			exit(-1)

if __name__ == "__main__":
	main()


import os
import numpy as np

import chainer
from chainer import iterators
from chainer import optimizers
from chainer import training
from chainer.datasets import TransformDataset
from chainer.datasets import cifar
import chainer.links as L
from chainer.training import extensions
from chainercv import transforms
import cv2 as cv
from skimage import transform as skimage_transform

def get_image_filepath_list(dir):
  file_path_list = []
  #クラスの抽出
  classes = sorted(os.listdir(dir))
  label_dic = {name:label for label, name in enumerate(classes)}

  for clas in classes:
      files = os.listdir(dir+clas)
      for file in files:
          file_path_list.append((dir+clas+"/"+file, label_dic[clas]))

  return file_path_list

USE_OPENCV = True

def cv_rotate(img, angle):
    if USE_OPENCV:
        img = img.transpose(1, 2, 0) / 255.
        center = (img.shape[0] // 2, img.shape[1] // 2)
        r = cv.getRotationMatrix2D(center, angle, 1.0)
        img = cv.warpAffine(img, r, img.shape[:2])
        img = img.transpose(2, 0, 1) * 255.
        img = img.astype(np.float32)
    else:
        # scikit-image's rotate function is almost 7x slower than OpenCV
        img = img.transpose(1, 2, 0) / 255.
        img = skimage_transform.rotate(img, angle, mode='edge')
        img = img.transpose(2, 0, 1) * 255.
        img = img.astype(np.float32)
    return img

def transform(inputs, random_angle=15., pca_sigma=255., train=True):
    img, label = inputs
    img = img.copy()

    #Random rotate
    # if train and random_angle != 0:
    #     angle = np.random.uniform(-random_angle, random_angle)
    #     img = cv_rotate(img, angle)

    if train:
        # random sized crop
        img = transforms.random_sized_crop(img)
        # Color augmentation
        img = transforms.pca_lighting(img, pca_sigma)
        # Random flip
        img = transforms.random_flip(img, x_random=True)

    # resize image
    img = transforms.resize(img, (128,128))
    # Standardization
    img /= 255.

    return img, label

if __name__ == "__main__":
    a = get_image_filepath_list("cats_and_dogs_small/train/")
    print(a)


"""
Makes and saves plots for the SH WFS
"""

import plotly
from plotly.graph_objs import Scatter, Layout
import os

from .. import wfs

SOAPYTEST_DIR = os.path.join(
        os.path.dirname(os.path.realpath(__file__)), "../../")

def plotSHPixelScale():
    print("\nPLOT SH WFS PIXELSCALE\n***")
    ps, mps = wfs.shpixelscale.testPixelScale()

    filename = os.path.join(SOAPYTEST_DIR, 'plots/shpixelscale.html')
    plotly.offline.plot(
            {   "data":[
                    Scatter(x=ps, y=mps, name="Soapy Pixel Scale"),
                    Scatter(x=ps, y=ps, name='Theoretical', line={'dash':'dash', 'color':'black'})],
                "layout":Layout(
                        title='Pixel Scale',
                        legend={'x':0})

            },
            auto_open=False,
            filename=filename)

if __name__ == '__main__':
    plotSHPixelScale()


import pygame
import numpy as np

class eater():
    def __init__(self,game, parent):
        self.game=game
        self.parent=parent
        self.tick=0
        self.delay=60
        self.eat_radius=30
        self.eat_radius_sq=self.eat_radius*self.eat_radius

    def eat(self, target):
        if 'edible' in target.components:
            target.components['edible'].on_eat(self.parent)

    def find_entity_to_eat(self):
        dmin=999999
        edible_ent=None
        for ent in self.game.entities:
            if hasattr(ent,'components') and 'edible' in ent.components:
                if ent==self:
                    continue
                dx=self.parent.x-ent.x
                dy=self.parent.y-ent.y
                dr=dx*dx+dy*dy
                if dr < dmin and dr < self.eat_radius_sq:
                    dmin=dr
                    edible_ent=ent
        return edible_ent

    def eat_nearest_entity(self):
        if self.tick > 0:
            return
        edible_entity = self.find_entity_to_eat()
        if edible_entity:
            self.tick=self.delay
            self.eat(edible_entity)

    def update(self):
        if self.tick > 0:
            self.tick-=1


# -*- python -*-
Import('env')

env.header('tablestore/core', [env.Glob('*.hpp'), env.Glob('*.ipp')])

public_objs = env.Object(env.Glob('*.cpp'))
http_objs = env.subDir('http')
impl_objs = env.subDir('impl')
proto_objs = env.subDir('protocol')
plainbuffer_objs = env.subDir('plainbuffer')

env.Install('$LIB_DIR', [
    env.StaticLibrary('tablestore_core_static',
                      [public_objs, http_objs, impl_objs, proto_objs, plainbuffer_objs]),
    env.SharedLibrary('tablestore_core',
                      [public_objs, http_objs, impl_objs, proto_objs, plainbuffer_objs])])
env.libDeps('tablestore_core_static',
            ['tablestore_util_static', 'protobuf', 'crypto', 'ssl', 'boost_system'])


"""
Various I/O helper functions
"""
import os
import datetime

def read_line_from_file(filename):
    val = ''
    try:
        infile = open(filename)
        line = infile.readline()
        infile.close()
        val = line.rstrip()
    except:
        raise

    return val

def append_results_log_to_csv(test_log, summary_filename, print_measurements_only=True):
    print_headers_first = False
    if not os.path.exists(summary_filename):
        print_headers_first = True

    summary_file = open(summary_filename, 'a')

    if print_headers_first:
        line = test_log.sprint_csv_summary_line(print_headers_only=True,
                                                print_measurements_only=print_measurements_only)
        summary_file.write(line)

    line = test_log.sprint_csv_summary_line(print_headers_only=False, print_measurements_only=print_measurements_only)
    summary_file.write(line)
    summary_file.close()

# actually datetime, but whatever...
def timestamp(datetime_object=None):
    """Helper function to standardize timestamp formats used in this module."""

    if datetime_object is None:
        datetime_object = datetime.datetime.now()
    return datetime_object.strftime("%Y%m%d-%H%M%S")

def datestamp(datetime_object=None):
    if datetime_object is None:
        datetime_object = datetime.datetime.now()
    return datetime_object.strftime("%Y%m%d")


from os import path

from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core import signing
from django.core.exceptions import PermissionDenied
from django.shortcuts import get_object_or_404
from sendfile import sendfile

from general_model import get_ext
from general_view import get_grouptype
from projects.models import ProjectAttachment, ProjectImage, Project
from students.models import StudentFile
from studyguide.models import CapacityGroupImage, MasterProgramImage
from support.models import Promotion

"""
Uploads go to /media/ directory
Download links point to /download
Downloads can either be by file path (old way, as if we're using a public file storage pool). Used by editfile widget
or as a primary key of the object the file belongs to (new way). Used in all templates.
"""

# @login_required
# def PublicFiles(request, fileid):
#     """
#     Public files, uploaded by type3, viewable on index.html, model in support-app
#     Cannot be viewed via edit-files, because access via URL is not supported. Only access via file id.
#     :param request:
#     :param fileid: The ID of the public file to download.
#     :return: file download
#     """
#     #first try PK, then filename
#     #try:
#     #    obj = PublicFile.objects.get(id=fileid)
#     #except:
#     #    obj = get_object_or_404(PublicFile, File='public_files/'+fileid)
#
#     obj = get_object_or_404(PublicFile, id=fileid)
#     return sendfile(request, obj.File.path, attachment=True, attachment_filename=obj.OriginalName)

def project_files(request, fileid, project_id=None, ty=None):
    """
    project files, attachments or images, viewable on project details, model in projects-app
    Because projects images and attachments are separate models, but are stored in the same folder, the ID is not
    unique, therefore only the UUID is possible to get the file.
    Mode 1: (old) projectid gives the folder to search in, fileid is the filename (an UUID)
    Mode 2: (new) ty is either "a" or "i" to refer to image or attachment and fileid is the ID (pk) of-
     the corresponding model
    When the file is an image, it is send inline, otherwise it is send as attachment.

    :param request:
    :param fileid: id of the project file.
    :param project_id: id of the project, corresponds to directory name appendix.
    :param ty: type, image or attachment
    :return: file download
    """
    if not request.user.is_authenticated:
        # allow anonymous users when viewing from a sharelink
        # make sure referrerpolicy is set on links, otherwise HTTP_REFERER might not be available.
        # https: // bugs.chromium.org / p / chromium / issues / detail?id = 455987
        if "HTTP_REFERER" in request.META:
            ref = request.META["HTTP_REFERER"].split('/')
            if 'share' in ref:  # url  /api/share/<sharetoken>/
                # check if sharetoken is valid. Same as in api.views
                try:
                    pk = signing.loads(ref[-2], max_age=settings.MAXAGESHARELINK)
                except signing.SignatureExpired:
                    raise PermissionDenied('Not allowed!')
                except signing.BadSignature:
                    raise PermissionDenied('Not allowed!')
                if not Project.objects.filter(pk=pk).exists():
                    # a check whether this image/attachment belongs to this project would be better
                    # but is more difficult.
                    raise PermissionDenied('Not allowed!')
                pass  # anonymous user viewing a valid share link
            else:
                raise PermissionDenied('Not allowed!')  # random referred
        else:  # direct access to file
            raise PermissionDenied('Not allowed!')

    # first try filename as image, then as attachment
    if project_id:  # via a project id and a filename (UUID) as fileid, the old way
        ext = get_ext(fileid)
        if ext in settings.ALLOWED_PROJECT_IMAGES:
            obj = get_object_or_404(ProjectImage, File='project_{}/{}'.format(project_id, fileid))
            return sendfile(request, obj.File.path, attachment=False)  # serve inline
        elif ext in settings.ALLOWED_PROJECT_ATTACHMENTS:
            obj = get_object_or_404(ProjectAttachment, File='project_{}/{}'.format(project_id, fileid))
            return sendfile(request, obj.File.path, attachment=True, attachment_filename=obj.OriginalName)
        else:
            raise PermissionDenied("File extension not allowed.")
    elif ty:  # by specifying image or attachement and a ID (pk) as fileid, the new way
        if ty == "a":  # attachment, like pdf
            obj = get_object_or_404(ProjectAttachment, id=fileid)
            return sendfile(request, obj.File.path, attachment=True, attachment_filename=obj.OriginalName)
        elif ty == "i":  # image
            obj = get_object_or_404(ProjectImage, id=fileid)
            return sendfile(request, obj.File.path)
        else:
            raise PermissionDenied("Invalid request.")
    else:
        raise PermissionDenied("Invalid request.")

@login_required
def promotion_files(request, fileid):
    """

    :param request:
    :param fileid: object pk or file uuid
    :return:
    """
    try:
        obj = Promotion.objects.get(id=fileid)
    except:
        obj = get_object_or_404(Promotion, File='promotion/{}'.format(fileid))
    return sendfile(request, obj.File.path)

@login_required
def student_files(request, fileid, distid=''):
    """
    Student file, uploaded by student as
    Responsible and assistant of student can view files.
    Student itself can view its own files

    :param request:
    :param fileid: id of the student file.
    :param distid: id of the distribution of the student.
    :return:
    """
    # first try PK, then filename
    try:
        obj = StudentFile.objects.get(id=fileid)
    except (StudentFile.DoesNotExist, ValueError):
        # accessed by distribution, then by filename
        obj = get_object_or_404(StudentFile, File='dist_{}/{}'.format(distid, fileid))

    if get_grouptype("studyadvisors") in request.user.groups.all() \
            or obj.Distribution.Project.ResponsibleStaff == request.user \
            or request.user in obj.Distribution.Project.Assistants.all() \
            or obj.Distribution.Student == request.user:
        # Allowed to view this file
        return sendfile(request, obj.File.path, attachment=True, attachment_filename=obj.OriginalName)
    else:
        # not allowed
        raise PermissionDenied("You are not allowed to view this file.")

@login_required
def capacity_group_images(request, fileid, capid=''):
    """

    :param request:
    :param fileid: id of the image.
    :param capid: id of the capacitygroup.
    :return:
    """
    # first try PK, then filename
    try:
        obj = CapacityGroupImage.objects.get(id=fileid)
    except (CapacityGroupImage.DoesNotExist, ValueError):
        # accessed by distribution, then by filename
        obj = get_object_or_404(CapacityGroupImage, File='capacitygroup_{}/{}'.format(capid, fileid))
    return sendfile(request, obj.File.path, attachment=True, attachment_filename=obj.OriginalName)

@login_required
def master_program_images(request, fileid, capid=''):
    """

    :param request:
    :param fileid: id of the image.
    :param capid: id of the master program.
    :return:
    """
    # first try PK, then filename
    try:
        obj = MasterProgramImage.objects.get(id=fileid)
    except (MasterProgramImage.DoesNotExist, ValueError):
        # accessed by distribution, then by filename
        obj = get_object_or_404(MasterProgramImage, File='masterprogram_{}/{}'.format(capid, fileid))
    return sendfile(request, obj.File.path, attachment=True, attachment_filename=obj.OriginalName)

def markdown_file_download(request, file_name):
    """
    Function to return images uploaded by markdown EasyMDE. Not stored in object/database, only file is saved.

    :param request:
    :param file_name: The name of the file
    :return: file inline
    """
    file_path = path.join(settings.MEDIA_ROOT, settings.MARKDOWN_IMAGE_UPLOAD_FOLDER, file_name)
    return sendfile(request, file_path, attachment=False)


import nltk
import random                #To randomize the dataset

from nltk.corpus import movie_reviews

"""
documents = []

for category in movie_reviews.categories():
    for fileid in movie_reviews.fileids(category):
        documents.append(list(movie_reviews.words(fileid)),category)
"""

# The above can be done in a one-liner

documents = [(list(movie_reviews.words(fileid)),category)
                    for category in movie_reviews.categories()
                    for fileid in movie_reviews.fileids(category)]

random.shuffle(documents)
print(documents[1])

all_words = []
for w in movie_reviews.words():
    all_words.append(w.lower())

all_words = nltk.FreqDist(all_words)
print(all_words.most_common(15))        # 15 Most common words

print(all_words["stupid"])      # Find number of times the word "stupid" has appeared :


#!/usr/bin/env python
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# pylint: disable=protected-access

import itertools
import threading
import unittest

import six

from devil import devil_env
from devil.android import logcat_monitor
from devil.android.sdk import adb_wrapper

with devil_env.SysPath(devil_env.PYMOCK_PATH):
  import mock  # pylint: disable=import-error

def _CreateTestLog(raw_logcat=None):
  test_adb = adb_wrapper.AdbWrapper('0123456789abcdef')
  test_adb.Logcat = mock.Mock(return_value=(l for l in raw_logcat))
  test_log = logcat_monitor.LogcatMonitor(test_adb, clear=False)
  return test_log

def zip_longest(expected, actual):
  # pylint: disable=no-member
  if six.PY2:
    return itertools.izip_longest(expected, actual)
  else:
    return itertools.zip_longest(expected, actual)

class LogcatMonitorTest(unittest.TestCase):

  _TEST_THREADTIME_LOGCAT_DATA = [
      '01-01 01:02:03.456  7890  0987 V LogcatMonitorTest: '
      'verbose logcat monitor test message 1',
      '01-01 01:02:03.457  8901  1098 D LogcatMonitorTest: '
      'debug logcat monitor test message 2',
      '01-01 01:02:03.458  9012  2109 I LogcatMonitorTest: '
      'info logcat monitor test message 3',
      '01-01 01:02:03.459  0123  3210 W LogcatMonitorTest: '
      'warning logcat monitor test message 4',
      '01-01 01:02:03.460  1234  4321 E LogcatMonitorTest: '
      'error logcat monitor test message 5',
      '01-01 01:02:03.461  2345  5432 F LogcatMonitorTest: '
      'fatal logcat monitor test message 6',
      '01-01 01:02:03.462  3456  6543 D LogcatMonitorTest: '
      'last line'
  ]

  def assertIterEqual(self, expected_iter, actual_iter):
    for expected, actual in zip_longest(expected_iter, actual_iter):
      self.assertIsNotNone(
          expected,
          msg='actual has unexpected elements starting with %s' % str(actual))
      self.assertIsNotNone(
          actual,
          msg='actual is missing elements starting with %s' % str(expected))
      self.assertEqual(actual.group('proc_id'), expected[0])
      self.assertEqual(actual.group('thread_id'), expected[1])
      self.assertEqual(actual.group('log_level'), expected[2])
      self.assertEqual(actual.group('component'), expected[3])
      self.assertEqual(actual.group('message'), expected[4])

    with self.assertRaises(StopIteration):
      next(actual_iter)
    with self.assertRaises(StopIteration):
      next(expected_iter)

  @mock.patch('time.sleep', mock.Mock())
  def testWaitFor_success(self):
    test_log = _CreateTestLog(
        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)
    test_log.Start()
    actual_match = test_log.WaitFor(r'.*(fatal|error) logcat monitor.*', None)
    self.assertTrue(actual_match)
    self.assertEqual(
        '01-01 01:02:03.460  1234  4321 E LogcatMonitorTest: '
        'error logcat monitor test message 5', actual_match.group(0))
    self.assertEqual('error', actual_match.group(1))
    test_log.Stop()
    test_log.Close()

  @mock.patch('time.sleep', mock.Mock())
  def testWaitFor_failure(self):
    test_log = _CreateTestLog(
        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)
    test_log.Start()
    actual_match = test_log.WaitFor(r'.*My Success Regex.*',
                                    r'.*(fatal|error) logcat monitor.*')
    self.assertIsNone(actual_match)
    test_log.Stop()
    test_log.Close()

  @mock.patch('time.sleep', mock.Mock())
  def testWaitFor_buffering(self):
    # Simulate an adb log stream which does not complete until the test tells it
    # to. This checks that the log matcher can receive individual lines from the
    # log reader thread even if adb is not producing enough output to fill an
    # entire file io buffer.
    finished_lock = threading.Lock()
    finished_lock.acquire()

    def LogGenerator():
      for line in type(self)._TEST_THREADTIME_LOGCAT_DATA:
        yield line
      finished_lock.acquire()

    test_adb = adb_wrapper.AdbWrapper('0123456789abcdef')
    test_adb.Logcat = mock.Mock(return_value=LogGenerator())
    test_log = logcat_monitor.LogcatMonitor(test_adb, clear=False)
    test_log.Start()

    actual_match = test_log.WaitFor(r'.*last line.*', None)
    finished_lock.release()
    self.assertTrue(actual_match)
    test_log.Stop()
    test_log.Close()

  @mock.patch('time.sleep', mock.Mock())
  def testFindAll_defaults(self):
    test_log = _CreateTestLog(
        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)
    test_log.Start()
    test_log.WaitFor(r'.*last line.*', None)
    test_log.Stop()
    expected_results = [('7890', '0987', 'V', 'LogcatMonitorTest',
                         'verbose logcat monitor test message 1'),
                        ('8901', '1098', 'D', 'LogcatMonitorTest',
                         'debug logcat monitor test message 2'),
                        ('9012', '2109', 'I', 'LogcatMonitorTest',
                         'info logcat monitor test message 3'),
                        ('0123', '3210', 'W', 'LogcatMonitorTest',
                         'warning logcat monitor test message 4'),
                        ('1234', '4321', 'E', 'LogcatMonitorTest',
                         'error logcat monitor test message 5'),
                        ('2345', '5432', 'F', 'LogcatMonitorTest',
                         'fatal logcat monitor test message 6')]
    actual_results = test_log.FindAll(r'\S* logcat monitor test message \d')
    self.assertIterEqual(iter(expected_results), actual_results)
    test_log.Close()

  @mock.patch('time.sleep', mock.Mock())
  def testFindAll_defaults_miss(self):
    test_log = _CreateTestLog(
        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)
    test_log.Start()
    test_log.WaitFor(r'.*last line.*', None)
    test_log.Stop()
    expected_results = []
    actual_results = test_log.FindAll(r'\S* nothing should match this \d')
    self.assertIterEqual(iter(expected_results), actual_results)
    test_log.Close()

  @mock.patch('time.sleep', mock.Mock())
  def testFindAll_filterProcId(self):
    test_log = _CreateTestLog(
        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)
    test_log.Start()
    test_log.WaitFor(r'.*last line.*', None)
    test_log.Stop()
    actual_results = test_log.FindAll(
        r'\S* logcat monitor test message \d', proc_id=1234)
    expected_results = [('1234', '4321', 'E', 'LogcatMonitorTest',
                         'error logcat monitor test message 5')]
    self.assertIterEqual(iter(expected_results), actual_results)
    test_log.Close()

  @mock.patch('time.sleep', mock.Mock())
  def testFindAll_filterThreadId(self):
    test_log = _CreateTestLog(
        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)
    test_log.Start()
    test_log.WaitFor(r'.*last line.*', None)
    test_log.Stop()
    actual_results = test_log.FindAll(
        r'\S* logcat monitor test message \d', thread_id=2109)
    expected_results = [('9012', '2109', 'I', 'LogcatMonitorTest',
                         'info logcat monitor test message 3')]
    self.assertIterEqual(iter(expected_results), actual_results)
    test_log.Close()

  @mock.patch('time.sleep', mock.Mock())
  def testFindAll_filterLogLevel(self):
    test_log = _CreateTestLog(
        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)
    test_log.Start()
    test_log.WaitFor(r'.*last line.*', None)
    test_log.Stop()
    actual_results = test_log.FindAll(
        r'\S* logcat monitor test message \d', log_level=r'[DW]')
    expected_results = [('8901', '1098', 'D', 'LogcatMonitorTest',
                         'debug logcat monitor test message 2'),
                        ('0123', '3210', 'W', 'LogcatMonitorTest',
                         'warning logcat monitor test message 4')]
    self.assertIterEqual(iter(expected_results), actual_results)
    test_log.Close()

  @mock.patch('time.sleep', mock.Mock())
  def testFindAll_filterComponent(self):
    test_log = _CreateTestLog(
        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)
    test_log.Start()
    test_log.WaitFor(r'.*last line.*', None)
    test_log.Stop()
    actual_results = test_log.FindAll(r'.*', component='LogcatMonitorTest')
    expected_results = [('7890', '0987', 'V', 'LogcatMonitorTest',
                         'verbose logcat monitor test message 1'),
                        ('8901', '1098', 'D', 'LogcatMonitorTest',
                         'debug logcat monitor test message 2'),
                        ('9012', '2109', 'I', 'LogcatMonitorTest',
                         'info logcat monitor test message 3'),
                        ('0123', '3210', 'W', 'LogcatMonitorTest',
                         'warning logcat monitor test message 4'),
                        ('1234', '4321', 'E', 'LogcatMonitorTest',
                         'error logcat monitor test message 5'),
                        ('2345', '5432', 'F', 'LogcatMonitorTest',
                         'fatal logcat monitor test message 6'),
                        ('3456', '6543', 'D', 'LogcatMonitorTest', 'last line')]
    self.assertIterEqual(iter(expected_results), actual_results)
    test_log.Close()

if __name__ == '__main__':
  unittest.main(verbosity=2)


from deeplearning.scientific_articles import Article
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from keras.layers import Dense, Dropout, Activation, Flatten, Input, Embedding, LSTM
from keras.layers import Convolution2D, MaxPooling2D, GlobalMaxPooling2D
import os
import keras
from keras.models import Model
from keras.utils import np_utils
from keras.models import load_model
from keras.layers.noise import GaussianNoise, GaussianDropout
from keras.regularizers import l2
from keras.layers.normalization import BatchNormalization

def get_reshaped_embed_data(embed_data):
    """
    Reshape the data for deeplearning model input (CNN)

    Parameters
    ----------
    embed_data: the embedding data

    Return
    ------
    reshaped_embed_data: shaped as [num_sections, num_data(max_section_lengths_of_words, 1, embed_size)]
    """

    print('\nReshaping Data...\n')
    title_data = list()
    abstract_data = list()
    keywords_data = list()
    for i in range(embed_data.shape[0]):
        for j in range(embed_data.shape[1]):
            if i == 0:
                print(embed_data[i][j].shape)
            reshaped_data = embed_data[i][j].reshape(embed_data[i][j].shape[0], 1, embed_data[i][j].shape[1])
            if j == 0:
                title_data.append(reshaped_data)
            elif j == 1:
                abstract_data.append(reshaped_data)
            elif j == 2:
                keywords_data.append(reshaped_data)
        if (i + 1) % 1000 == 0:
            print('Reshaped %d Data!' % (i + 1))
    title_data = np.asarray([title_data[i] for i in range(len(title_data))])
    abstract_data = np.asarray([abstract_data[i] for i in range(len(abstract_data))])
    keywords_data = np.asarray([keywords_data[i] for i in range(len(keywords_data))])
    print('Finished Reshaping Data!')
    reshaped_embed_data = [title_data, abstract_data, keywords_data]
    return reshaped_embed_data

def get_deeplearning_model(reshaped_embed_data, num_classes):
    """
    Get the deep-learning model for scientific articles

    Parameters
    ----------
    reshaped_embed_data: the reshaped embedding data of raw texts (train or text)

    num_classes: the number of classes

    """
    print('\nBuilding Deeplearning Model...\n')

    # Load Shape & Input
    title_shape = reshaped_embed_data[0][0].shape
    abstract_shape = reshaped_embed_data[1][0].shape
    keywords_shape = reshaped_embed_data[2][0].shape

    # title_input = reshaped_embed_data[0]
    # abstract_input = reshaped_embed_data[1]
    # keywords_input = reshaped_embed_data[2]

    title_input = Input(title_shape, dtype='float32', name='title_input')
    abstract_input = Input(abstract_shape, dtype='float32', name='abstract_input')
    keywords_input = Input(keywords_shape, dtype='float32', name='keywords_input')

    # Title Part
    title_input_bn = BatchNormalization()(title_input)
    title_conv_1_1 = Convolution2D(input_shape=title_shape,
                                   filters=10,
                                   kernel_size=(2, 1),
                                   strides=(1, 1),
                                   activation='relu',
                                   data_format='channels_last')(title_input_bn)
    title_pool_1_1 = GlobalMaxPooling2D()(title_conv_1_1)
    title_conv_1_2 = Convolution2D(input_shape=title_shape,
                                   filters=10,
                                   kernel_size=(3, 1),
                                   strides=(1, 1),
                                   activation='relu',
                                   data_format='channels_last')(title_input)
    title_pool_1_2 = GlobalMaxPooling2D()(title_conv_1_2)
    title_merge = keras.layers.concatenate([title_pool_1_1, title_pool_1_2])
    title_merge_bn = BatchNormalization()(title_merge)
    title_output = Dense(64, activation='sigmoid', kernel_regularizer=l2(0.01))(title_merge_bn)

    # Abstract Part
    abstract_input_bn = BatchNormalization()(abstract_input)
    abstract_conv_1_1 = Convolution2D(input_shape=abstract_shape,
                                      filters=20,
                                      kernel_size=(20, 1),
                                      strides=(1, 1),
                                      activation='relu',
                                      data_format='channels_last')(abstract_input_bn)
    abstract_pool_1_1 = MaxPooling2D(pool_size=(2, 1))(abstract_conv_1_1)
    abstract_conv_2_1 = Convolution2D(filters=10,
                                      kernel_size=(10, 1),
                                      strides=(1, 1),
                                      activation='relu',
                                      data_format='channels_last')(abstract_pool_1_1)
    abstract_pool_2_1 = MaxPooling2D(pool_size=(3, 1))(abstract_conv_2_1)
    abstract_conv_3_1 = Convolution2D(filters=10,
                                      kernel_size=(3, 1),
                                      strides=(2, 1),
                                      activation='relu',
                                      data_format='channels_last')(abstract_pool_2_1)
    abstract_pool_3_1 = GlobalMaxPooling2D()(abstract_conv_3_1)
    abstract_conv_3_2 = Convolution2D(filters=10,
                                      kernel_size=(4, 1),
                                      strides=(2, 1),
                                      activation='relu',
                                      data_format='channels_last')(abstract_pool_2_1)
    abstract_pool_3_2 = GlobalMaxPooling2D()(abstract_conv_3_2)
    abstract_conv_3_3 = Convolution2D(filters=10,
                                      kernel_size=(5, 1),
                                      strides=(2, 1),
                                      activation='relu',
                                      data_format='channels_last')(abstract_pool_2_1)
    abstract_pool_3_3 = GlobalMaxPooling2D()(abstract_conv_3_3)
    abstract_conv_3_4 = Convolution2D(filters=10,
                                      kernel_size=(6, 1),
                                      strides=(2, 1),
                                      activation='relu',
                                      data_format='channels_last')(abstract_pool_2_1)
    abstract_pool_3_4 = GlobalMaxPooling2D()(abstract_conv_3_4)
    abstract_merge = keras.layers.concatenate([abstract_pool_3_1, abstract_pool_3_2,
                                               abstract_pool_3_3, abstract_pool_3_4])
    abstract_merge_bn = BatchNormalization()(abstract_merge)
    abstract_output = Dense(512, activation='sigmoid', kernel_regularizer=l2(0.01))(abstract_merge_bn)

    # Keywords Part
    keywords_input_bn = BatchNormalization()(keywords_input)
    keywords_flatten = Flatten()(keywords_input_bn)
    keywords_output = Dense(64, activation='sigmoid', kernel_regularizer=l2(0.01))(keywords_flatten)

    # Merge and Output
    merge_layer = keras.layers.concatenate([title_output, abstract_output, keywords_output])
    merge_layer_bn = BatchNormalization()(merge_layer)
    dense_1 = Dense(256, activation='sigmoid', kernel_regularizer=l2(0.01))(merge_layer_bn)
    dropout_1 = GaussianDropout(0.25)(dense_1)
    dense_2 = Dense(32, activation='sigmoid', kernel_regularizer=l2(0.01))(dropout_1)
    dropout_2 = GaussianDropout(0.25)(dense_2)
    total_output = Dense(num_classes, activation='softmax', kernel_regularizer=l2(0.01), name='total_output')(dropout_2)

    # Print Model Summary
    dl_model = Model(inputs=[title_input, abstract_input, keywords_input], outputs=[total_output])
    print(dl_model.summary())
    print('\nDeeplearning Model Building Completed...\n')
    return dl_model

if __name__ == '__main__':
    os.chdir('E:/HDQ/Projects/Graduation')

    _article_data_path = './output/texts_without_main_text'
    _id_label_path = './output/doc_topic-15_1124.txt'
    scientific_articles = Article(article_data_path=_article_data_path,
                                  id_label_path=_id_label_path)
    _word2vec_path = './output/paper_without_main_text_400_word2vec'
    scientific_articles_embed_data = scientific_articles.get_embed_data(400, word2vec_path=_word2vec_path)
    X_train, X_test, y_train, y_test = train_test_split(scientific_articles_embed_data,
                                                        scientific_articles.data_collection['labels'],
                                                        test_size=0.4,
                                                        random_state=1)
    X_Reshaped = get_reshaped_embed_data(scientific_articles_embed_data)
    X_train_Reshaped = get_reshaped_embed_data(X_train)
    X_test_Reshaped = get_reshaped_embed_data(X_test)
    y = scientific_articles.data_collection['labels']
    for i in range(len(y)):
        y[i] -= 1
    for i in range(len(y_train)):
        y_train[i] -= 1
    for i in range(len(y_test)):
        y_test[i] -= 1
    Y = np_utils.to_categorical(y, 15)
    Y_train = np_utils.to_categorical(y_train, 15)
    Y_test = np_utils.to_categorical(y_test, 15)

    model = get_deeplearning_model(X_Reshaped, 15)

    model.compile(loss='categorical_crossentropy',
                  optimizer='sgd',
                  metrics=['accuracy'])
    model.fit({'title_input': X_train_Reshaped[0],
               'abstract_input': X_train_Reshaped[1],
               'keywords_input': X_train_Reshaped[2]},
              {'total_output': Y_train},
              batch_size=32,
              nb_epoch=30,
              validation_data=({'title_input': X_test_Reshaped[0],
                                'abstract_input': X_test_Reshaped[1],
                                'keywords_input': X_test_Reshaped[2]},
                               {'total_output': Y_test},))

    model.save('./output/nn_model/dl_model_171215.h5')
    score = model.evaluate({'title_input': X_test_Reshaped[0],
                            'abstract_input': X_test_Reshaped[1],
                            'keywords_input': X_test_Reshaped[2]},
                           {'total_output': Y_test},)
    print('Test score:', score[0])
    print('Test accuracy:', score[1])


# Author: Shadowys 18/6/2014
# Description:
# Logic for game version checking

from csv import reader
from download import parsepagedl
from checksum import checksum, getallfiles

def gethash():
    """ Returns the checksum hash of the current folder.
        needed files names are hardcoded.
        might need to add dlls too
    """
    needed=["level","asset"]
    sumset=checksum(getallfiles(needed))
    return sumset

def parsecsv(filename, self):
    """ Parse the version hash csv file to look for version
        This is also hardcoded to prevent easy hacks
        Change to proper get/post request when
        ftp server is available.
    """
    #self.inc() is a function that acts as a counter for the thread client
    base="https://github.com"
    url="/Shadowys/Zoids-MMORPG/tree/master/Hash"
    key="blob"
    switch="raw"
    self.inc(3)
    print("downloading")
    filename=parsepagedl(base,url,key,switch,filename, False)
    self.inc()
    with open((r"%s"%(filename)), 'r') as f:
        self.inc()
        data=reader(f)
        self.inc()
        #The csv is {version}:{checksum}
        #Inverting it makes it easier to lookup later
        data={line[1]:line[0] for line in list(reader(f))[1:]}
        self.inc()
        return data

def confirmversion(checksum, data):
    """ Confirm if the version now is latest version.
        0 if yes
        version number if older version
        -1 if version number not found, meaning something got hacked.
    """
    val=-1
    if checksum in data.keys():
        val= 0 if data[checksum]==max(data.values()) else data[checksum]
    return val

# Simple test for dev when creating new hash
'''
val=gethash()
print(val)
#'''


from itertools import combinations
from random import shuffle
from random import choice
import yaml
import datetime

class Game:
    def __init__(self, t1, t2):
        if t1.p1 in t2 or t1.p2 in t2:
            raise ValueError("can't have a game with same person on both teams")
        else:
            self.t1 = t1
            self.t2 = t2
            self.__i = 0

    # item is a team
    def __contains__(self, item):
        return self.t1 == item or self.t2 == item

    def __str__(self):
        return "{} vs {}".format(str(self.t1), str(self.t2))

    def __eq__(self, other):
        return self.t1 in other and self.t2 in other

    def __iter__(self):
        return self

    def __next__(self):
        if self.__i == 0:
            self.__i = 1
            return self.t1
        elif self.__i == 1:
            self.__i = 2
            return self.t2
        else:
            self.__i = 0
            raise StopIteration

class Team:
    # no need for duplicate player error checking. combinations() won't give duplicate player team. ex P01 P01
    def __init__(self, p1, p2):
        self.p1 = p1
        self.p2 = p2
        self.__i = 0

    # overrides the "x in y" phrase! :)
    # check player in team. "item" is a player (int)
    def __contains__(self, item):
        return self.p1 == item or self.p2 == item

    def __str__(self):
        return "(P{:02}, P{:02})".format(self.p1, self.p2)

    def __eq__(self, other):
        """
        :param other: a Team object to compare against
        :return: True if players are identical
        """
        return self.p1 in other and self.p2 in other

    def __iter__(self):
        return self

    def __next__(self):
        if self.__i == 0:
            self.__i = 1
            return self.p1
        elif self.__i == 1:
            self.__i = 2
            return self.p2
        else:
            self.__i = 0
            raise StopIteration

def team_players_duplicate_in_round(team1, round):
    val = False
    for game in round:
        for tm in game:
            if team1.p1 in tm or team1.p2 in tm:
                val = True
    return val

def team_duplicate_in_tourney(team, tourney):
    val = False
    for round in tourney:
        for game in round:
            if team in game:
                val = True
    return val

def create_tourny():
    """
        read in the yaml, output tournament brackets
    """
    # TODO: make it so the same person can't be left out of multiple games before everyone else has been left out
    # TODO: add capability to demand certain games before generation starts by placing info in yaml

    with open("kqoc.yaml", 'r') as yml:
        cfg = yaml.load(yml, Loader=yaml.FullLoader)

    # list of ints ex: [1, 2, 3, 4]
    players = [x for x in range(1, cfg['num_players'] + 1)]
    print("Number of players: {}".format(cfg['num_players']))

    # list of team objects
    all_possible_teams = [Team(x, y) for x, y in combinations(players, 2)]
    print("Number of possible teams: " + str(len(all_possible_teams)))

    tourney_rounds = []
    while cfg['num_rounds'] > 0:
        # create one round and append to tourney
        single_round_games = []
        for x in range(cfg['num_players'] // 4):
            # runs for each game for a round. num of games per round = numplayers // 4

            # choose team1.
            # team1 cannot duplicate players within this round
            # team1 cannot duplicate teams within this tourney
            possible_team1 = []
            team1 = None
            for team in all_possible_teams:
                if not team_players_duplicate_in_round(team, single_round_games) and not team_duplicate_in_tourney(team,
                                                                                tourney_rounds):
                    possible_team1.append(team)
            try:
                team1 = choice(possible_team1)
            except:
                pass

            # choose team2
            # team2 cannot duplicate players within this round (don't forget team1!)
            # team2 cannot duplicate teams within this tourney
            possible_team2 = []
            team2 = None
            for team in all_possible_teams:
                if not team_players_duplicate_in_round(team, single_round_games) and not team_duplicate_in_tourney(team,
                                                                                tourney_rounds) and team.p1 not in team1 and team.p2 not in team1:
                    possible_team2.append(team)
            try:
                team2 = choice(possible_team2)
            except:
                pass

            # create game, add to round
            if team1 is not None and team2 is not None:
                single_round_games.append(Game(team1, team2))

        if len(single_round_games) == (cfg['num_players'] // 4):
            tourney_rounds.append(single_round_games)
        cfg['num_rounds'] -= 1
    return tourney_rounds

if __name__ == "__main__":
    tourney = create_tourny()

    with open('tourny_{}.txt'.format(datetime.datetime.now().strftime('%Y%m%d')), 'w') as file:
        file.write('KQOC Tournament Generator\n\n')
        i = 1
        for vb_round in tourney:
            file.write("round {}:\n".format(i))
            for game in vb_round:
                file.write(str(game) + ': \n')
            i += 1
            file.write('\n')


from ANET import *
from HEX import *
from MCTS import *
from TOURNAMENT import *
import random
import os

class HexNN:

    def __init__(self, mcts, save_int=10, buffer=list(), buffer_int=20, preload=False):
        self.mcts = mcts
        self.save_int = save_int
        self.buffer = buffer
        self.p1_wins = 0
        self.p2_wins = 0
        self.buffer_clear = buffer_int
        self.preload = preload

    def run(self, mcts_sim, games):
        for i in range(games):
            print("Game number", i+1)
            best_path = list()
            mcts_current = self.mcts
            state = mcts_current.root_node.state
            game_sim = mcts_sim
            while not state.check_finished():  # Game has no winner
                next_node = mcts_current.run(game_sim)
                best_path.append(next_node)
                mcts_current = MCTS(next_node.state, anet=self.mcts.anet)
                state = next_node.state
                game_sim += 400
            winner = state.player % 2 + 1
            if winner == 1:
                self.p1_wins += 1
            else:
                self.p2_wins += 1
            print("Player", winner, "won!!")
            for node in best_path:
                label = create_distribution(node.parent)
                if label.count(0) != node.state.dimension**2:
                    board = node.parent.state.Hex_to_list()
                    board.append(node.parent.state.player)
                    net_board = node.state.list_to_net(board)
                    self.add_data(net_board, label)
            x_train, y_train = self.random_minibatch()
            self.mcts.anet.train(x_train, y_train)
            if i % self.save_int == 0 and i != 0:
                if self.preload:
                    for case in self.buffer:
                        self.add_data_to_file("RBUF.txt", case[0], case[1])
                self.mcts.anet.save_model(str(i))
            if i % self.buffer_clear == 0 and i != 0:
                if len(self.buffer) > 500:
                    self.buffer = self.buffer[500:]
            self.mcts.root_node.state.player = mcts_current.root_node.state.change_player()

    def add_data(self, x, label):
        self.buffer.append([x, label])

    def random_minibatch(self):
        x_train = []
        y_train = []
        if self.mcts.anet:
            for i in range(self.mcts.anet.batch_size):
                case = random.choice(self.buffer)
                x_train.append(case[0])
                y_train.append(case[1])
        else:
            for i in range(64):
                case = random.choice(self.buffer)
                x_train.append(case[0])
                y_train.append(case[1])
        return np.array(x_train), np.array(y_train)

    def add_data_to_file(self, filename, x, label):
        if not os.stat(filename).st_size == 0:
            string = "\n"
        else:
            string = ""
        for i in range(len(x) - 1):
            string += (str(x[i]) + ",")
        string += str(x[-1])
        string += ";"
        for i in range(len(label) - 1):
            string += (str(label[i]) + ",")
        string += str(label[-1])
        file_obj = open(filename, 'a')
        file_obj.write(string)

def create_distribution(parent):
    p_board = parent.state.Hex_to_list()
    distribution = [0 for i in range(len(p_board))]
    if parent.children:
        for i in range(len(p_board)):
            if p_board[i] == 0:
                for child in parent.children:
                    if child.state.Hex_to_list()[i] != 0 and parent.wins != 0:
                        distribution[i] = child.wins / parent.wins
    return distribution

def print_cases_to_file(cases):
    file = open("cases.txt", 'w')
    for case in cases:
        file.write(''.join(str(c)+":" for c in case[0]) + "----" + ''.join(str(c)+":" for c in case[1]) + "\n")

def preload_data(filename):
    # The data uses ROW vectors for a data point, that's what Keras assumes.
    file_obj = open(filename, 'r')
    data = list()
    for line in file_obj.readlines():
        line_vec = line.split(';')
        input_vec = line_vec[0].split(',')
        label = line_vec[1].split(',')
        data.append([list(map(float, input_vec)), list(map(float, label))])
    return data

if __name__ == '__main__':
    anet = Anet([52, 128, 25], batch_size=64)
    anet.create_anet()
    # buffer = preload_data("RBUF.txt")
    root_board = create_root_board(5)
    hex_state = Hex(root_board, dimension=5, player=1)
    anet2 = ANET.load_model("5_25", [52, 128, 25])
    anet1 = ANET.load_model("30_25", [52, 128, 25])
    # anet1 = ANET.load_model("bad10", [52, 128, 25])
    # bad = ANET.load_model("bad")
    tournament = Tournament(hex_state, games=1000, anet2=anet2)
    tournament.play_tournament()
    mcts = MCTS(hex_state, anet=anet)
    # anet1 = ANET.load_model("70_25", batch_size=32)
    mcts = MCTS(hex_state, anet=anet)
    hex_nn = HexNN(mcts)
    hex_nn.run(10000, 201)


#!/Users/carolinagiovagnoli/Documents/GitHub/Andada-Pro/build-env/bin/python3.9
#
# Copyright 2017 Google Inc. All Rights Reserved.
#
# 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.
#
"""Tool to print subsets supported by a given font file.

"""
from __future__ import print_function
import os

from absl import flags
from gftools.util import google_fonts as fonts
from absl import app

FLAGS = flags.FLAGS

flags.DEFINE_integer('min_pct', 0,
                     'What percentage of subset codepoints have to be supported'
                     ' for a non-ext subset.')
flags.DEFINE_integer('min_pct_ext', 0,
                     'What percentage of subset codepoints have to be supported'
                     ' for a -ext subset.')

def main(argv):
  for arg in argv[1:]:
    subsets = fonts.SubsetsInFont(arg, FLAGS.min_pct, FLAGS.min_pct_ext)
    for (subset, available, total) in subsets:
      print('%s %s %d/%d' % (os.path.basename(arg), subset, available, total))

if __name__ == '__main__':
  app.run(main)


#-------------------------------------------------------------------------------
# Name:        testlobsterdirectory.py
# Purpose:     This module contains the tester functions for CCVarDirectory class.
#
# Author:      Kruti
#
# Created:     04-02-2014
# Copyright:   (c) Account Prism Pvt. Ltd.
# Licence:     all_rights_reserved
#-------------------------------------------------------------------------------

#   imports
import unittest
import lobsterdirectory

#   class-name :- TestCCVarDirectory
#   class-description :- This class tests all the methods of CCVarDirectory class
class TestCCVarDirectory(unittest.TestCase):

    def setUp(self):

        """

        Purpose :- performs all the initialization operations prior to each test-case.

        """
        #   creating object of CCvarDirectory, by passing all the required arguments
        self.m_objCCVarDirectory = lobsterdirectory.CCVarDirectory(
                                                                    strNodeName= "Current Node", lsStrChildDirectories = ["Child1", "Child2", "Child3"],
                                                                    strParentDirectoryName = "Parent.New Directory",
                                                                    lsStrSiblingDirectories = ["SiblingDir1", "SiblingDir2"],
                                                                    lsStrSiblingCVars = ["SiblingCVar1", "SiblingCVar2"],
                                                                    lsStrChildCVars = ["ChildCVar1", "ChildCvar2"]
                                                                )

        #   creating object of CCvarDirectory, by passing only nodename and list of child directories
        self.m_objCCVarDirectory2 = lobsterdirectory.CCVarDirectory(strNodeName = "New Node", lsStrChildDirectories = ["Child"])

        return

    """----------------------------------------------------------------------"""

    def testinit(self):

        """
        Tester Function :-

        Purpose :- Tests constructor of CCVarDirectory class

        """

        objCCVarDirectory = lobsterdirectory.CCVarDirectory(
                        "Current Node", ["Child1", "Child2", "Child3"],
                        "Parent.New Directory", ["SiblingDir1", "SiblingDir2"],
                        ["SiblingCVar1", "SiblingCVar2"],["ChildCVar1", "ChildCvar2"]
                        )

        self.assertEqual(objCCVarDirectory.MGetAbsolutePath(), "Parent.New Directory.Current Node")

        self.assertEqual(objCCVarDirectory.MGetChildCVars(), ['ChildCVar1', 'ChildCvar2'])

        self.assertEqual(objCCVarDirectory.MGetChildNodes(),["Child1", "Child2", "Child3"])

        self.assertEqual(objCCVarDirectory.MGetNodeName(), "Current Node")

        self.assertEqual(objCCVarDirectory.MGetSiblingCVars(),["SiblingCVar1", "SiblingCVar2"])

        self.assertEqual(objCCVarDirectory.MGetSiblingDirectories(), ["SiblingDir1", "SiblingDir2"])

        return

    """----------------------------------------------------------------------"""

    def tearDown(self):

        """
        Deallocates the memory of the member variables

        """

        self.m_objCCVarDirectory = None
        self.m_objCCVarDirectory2 = None

        return

    """----------------------------------------------------------------------"""

    def testMHasChildCVars(self):

        """
        Tester Function :-

        Purpose : - Tests MHasChildCVars() function of CVar class.

        """
        #   asserting that MHasChildCVars() return True for m_objCCVarDirectory
        self.assertEqual(self.m_objCCVarDirectory.MHasChildCVars(), True)
        #   asserting that MHasChildCVars() return False for m_objCCVarDirectory
        self.assertEqual(self.m_objCCVarDirectory2.MHasChildCVars(), False)

        return

    """----------------------------------------------------------------------"""

    def testMAppendChildCVar(self):

        """
        Tester Function :-

        Purpose : - Tests MAppendChildCVar() function of CVar class.

        """
        self.m_objCCVarDirectory.MAppendChildCVar("NewChildCVar")
        #   ValueError Exception will be raised, if MAppendChildCVar() has not
        #   completed successfully(i.e, NewChildCVar is not in the list of childcvars)
        self.m_objCCVarDirectory.MGetChildCVars().index("NewChildCVar")
        #   expecting Exception to be raised, because MAppendChildCVar() is invoked
        #   with integer value as argument.
        self.assertRaises(Exception, self.m_objCCVarDirectory.MAppendChildCVar, (1, ))
        #   expecting Exception to be raised, because MAppendChildCVar() is invoked
        #   with None as argument
        self.assertRaises(Exception, self.m_objCCVarDirectory.MAppendChildCVar, (None, ))
        #   expecting Exception to be raised, because MAppendChildCVar() is invoked
        #   with empty string as argument
        self.assertRaises(Exception, self.m_objCCVarDirectory.MAppendChildCVar, (None, ))

        return

    """----------------------------------------------------------------------"""

    def testMGetChildCVars(self):

        """
        Tester Function :-

        Purpose : - Tests MGetChildCVars() function of CVar class.

        """
        self.assertEqual(self.m_objCCVarDirectory.MGetChildCVars(), ['ChildCVar1', 'ChildCvar2'])
        #   asserting that MGetChildCVars() for m_objCCvarDirectory2 returns empty list
        self.assertEqual(self.m_objCCVarDirectory2.MGetChildCVars(), [])

        return

    """----------------------------------------------------------------------"""

    def testMGetSiblingDirectories(self):

        """
        Tester Function :-

        Purpose : - Tests MGetSiblingDirectories() function of CVar class.

        """
        self.assertEqual(self.m_objCCVarDirectory.MGetSiblingDirectories(), ['SiblingDir1', 'SiblingDir2'])
        #   asserting that MGetSiblingDirectories() for m_objCCvarDirectory2 returns empty list
        self.assertEqual(self.m_objCCVarDirectory2.MGetSiblingDirectories(), [])

        return

    """----------------------------------------------------------------------"""

    def testMGetSiblingCVars(self):

        """
        Tester Function :-

        Purpose : - Tests Constructor MGetSiblingCVars() of CVar class.

        """
        self.assertEqual(self.m_objCCVarDirectory.MGetSiblingCVars(), ['SiblingCVar1', 'SiblingCVar2'])
        #   asserting that MGetSiblingCVars() for m_objCCvarDirectory2 returns empty list
        self.assertEqual(self.m_objCCVarDirectory2.MGetSiblingCVars(), [])

        return

    """----------------------------------------------------------------------"""

    def testMGetAbsolutePath(self):

        """
        Tester Function :-

        Purpose : - Tests MGetAbsolutePath() function of CVar class.

        """
        #   asserting that MGetAbsolutePath() for m_objCCvarDirectory returns "Parent.New Directory.Current Node"
        self.assertEqual(self.m_objCCVarDirectory.MGetAbsolutePath(), "Parent.New Directory.Current Node")
        #   asserting that MGetAbsolutePath() for m_objCCvarDirectory1 returns "New Node"
        self.assertEqual(self.m_objCCVarDirectory2.MGetAbsolutePath(), "New Node")

        return

    """----------------------------------------------------------------------"""

if __name__ == '__main__':
    unittest.main()