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

"""
Written by Lucas Sinclair and Paul Rougieux

JRC biomass Project.
Unit D1 Bioeconomy.
"""

# Built-in modules #

# Third party modules #

# First party modules #
import autopaths
from autopaths            import Path
from autopaths.auto_paths import AutoPaths
from autopaths.tmp_path   import new_temp_dir
from plumbing.cache       import property_cached
from tqdm import tqdm

# Internal modules #
from cbmcfs3_runner.reports.scenario import ScenarioReport
from cbmcfs3_runner.pump.dataframes import concat_as_df

###############################################################################
class Scenario(object):
    """
    This object represents a modification of the input data for the purpose.
    A scenario can be harvest and economic scenario.
    Actual scenarios should inherit from this class.
    """

    all_paths = """
    /logs_summary.md
    """

    def __iter__(self): return iter(self.runners.values())
    def __len__(self):  return len(self.runners.values())

    def __getitem__(self, key):
        """Return a runner based on a country code."""
        return self.runners[key]

    def __init__(self, continent):
        # Save parent #
        self.continent = continent
        # This scenario dir #
        self.base_dir = Path(self.scenarios_dir + self.short_name + '/')
        # Automatically access paths based on a string of many subpaths #
        self.paths = AutoPaths(self.base_dir, self.all_paths)

    def __repr__(self):
        return '%s object with %i runners' % (self.__class__, len(self))

    def __call__(self, verbose=False):
        for code, steps in tqdm(self.runners.items()):
            for runner in steps:
                runner(interrupt_on_error=False, verbose=verbose)
        self.compile_log_tails()

    @property
    def runners(self):
        msg = "You should inherit from this class and implement this property."
        raise NotImplementedError(msg)

    @property
    def scenarios_dir(self):
        """Shortcut to the scenarios directory."""
        return self.continent.scenarios_dir

    @property_cached
    def report(self):
        return ScenarioReport(self)

    def compile_log_tails(self, step=-1):
        summary = self.paths.summary
        summary.open(mode='w')
        summary.handle.write("# Summary of all log file tails\n\n")
        summary.handle.writelines(r[step].tail for r in self.runners.values() if r[step])
        summary.close()

    # ------------------------------ Others ----------------------------------#
    def make_csv_zip(self, csv_name, dest_dir):
        """
        Will make a zip file will the specified CSV file from every country
        together and place it in the given destination directory.
        For instance you can do:

        >>> f = scenario.make_csv_zip('ipcc_pools', '~/exports/for_sarah/')
        >>> print(f)
        """
        # Files to put in the zip #
        files = {iso: rl[-1].post_processor.csv_maker.paths(csv_name)
                 for iso, rl in self.runners.items()}
        # Actual name of CSV file #
        csv_full_name = next(iter(files.items()))[1].name
        # Destination directory #
        dest_dir = Path(dest_dir)
        # If it's not a directory #
        assert isinstance(dest_dir, autopaths.dir_path.DirectoryPath)
        # Destination zip file #
        dest_zip = dest_dir + csv_full_name + '.zip'
        # Temporary directory #
        tmp_dir = new_temp_dir()
        zip_dir = tmp_dir + csv_full_name + '/'
        zip_dir.create()
        # Copy #
        for iso, f in files.items():
            try:
                f.copy(zip_dir + iso + '.csv')
            except Exception as e:
                print("no data in ", iso)
                print('Error loading data: '+ str(e))
        # Compress #
        zip_dir.zip_to(dest_zip)
        # Remove #
        tmp_dir.remove()
        # Return #
        return dest_zip

    def concat_as_df(self, *args, **kwargs):
        """A data frame with many countries together, crucial for analysis"""
        return concat_as_df(self, *args, **kwargs)


import os
import codecs

try:
    from setuptools import (setup, find_packages)
except ImportError:
    from distutils.core import (setup, find_packages)

VERSION = (0, 2, 0)
__version__ = '.'.join(map(str, VERSION[:3])) + "".join(VERSION[3:])

__package_name__ = 'pelican-readtime'
__description__ = 'Plugin for Pelican that computes average read time.'

__contact_names__ = 'David Jenkins, Deepak Bhalla, Jonathan Dektiar'
__contact_emails__ = 'djenkinsdev@gmail.com, contact@deepakrb.com, contact@jonathandekhtiar.eu'

__homepage__ = 'https://github.com/JenkinsDev/pelican-readtime'
__repository_url__ = 'https://github.com/JenkinsDev/pelican-readtime'
__download_url__ = 'https://github.com/JenkinsDev/pelican-readtime'

__docformat__ = 'markdown'
__license__ = 'MIT'
__keywords__ = 'pelican blogging blog static webdevelopment plugin pelican-plugin readtime python python3 python2'

here = os.path.abspath(os.path.dirname(__file__))
if os.path.exists('README.rst'):
    # codec is used for consistent encoding
    long_description = codecs.open(
        os.path.join(here, 'README.rst'), 'r', 'utf-8').read()
else:
    long_description = 'See ' + __homepage__

setup(
    name=__package_name__,
    version=__version__,
    description=__description__,
    long_description=long_description,
    url=__repository_url__,
    download_url=__download_url__,
    license='MIT',
    author=__contact_names__,
    author_email=__contact_emails__,
    maintainer=__contact_names__,
    maintainer_email=__contact_emails__,
    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Natural Language :: English',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Topic :: Software Development :: Libraries :: Python Modules'
    ],
    keywords=__keywords__,
    packages=[''],
    install_requires=['pelican>=3.6'],
    zip_safe=True,
    include_package_data=True
)


from elasticsearch import Elasticsearch
from services.caption_processor import split_captions

es = Elasticsearch()

def index_captions(captions, video_id):
    for ctime, ctext in split_captions(captions):
        doc = {
            'time': ctime,
            'text': ctext,
            'video': video_id
        }
        es.index(index="simple-captions", doc_type='caption', body=doc)

def index_caption_pause_splitted(captions, video_id, index_name = "pause-splitted-captions"):
    for ctime, ctext in captions:
        doc = {
            'time': ctime,
            'text': ctext,
            'video': video_id,
        }
        es.index(index=index_name, doc_type='caption', body=doc)


# import the packages
import matplotlib.pyplot as plt
import networkx as nx

# Define the data structures of vertices and edges
vertices = range(1, 10)
edges = [(7, 2), (2, 3), (7, 4), (4, 5), (7, 3), (7, 5),
         (1, 6), (1, 7), (2, 8), (2, 9)]
# Let's first instantiate the graph
G = nx.Graph()
# let's draw the graph
G.add_nodes_from(vertices)
G.add_edges_from(edges)
pos = nx.spring_layout(G)

#  Let's define the NF nodes
nx.draw_networkx_nodes(G, pos,
                       nodelist=[1, 4, 3, 8, 9],
                       label=True,
                       node_color='g',
                       node_size=1300)
# let's create the nodes that are known to be involved in fraud
nx.draw_networkx_nodes(G, pos,
                       nodelist=[2, 5, 6, 7],
                       label=True,
                       node_color='r',
                       node_size=1300)
# Let's create labels for the nodes
nx.draw_networkx_edges(G, pos, edges, width=3, alpha=0.5, edge_color='b')
# Creating labels names
labels = {1: r'1 NF', 2: r'2 F', 3: r'3 NF', 4: r'4 NF', 5: r'5 F', 6: r'6 F', 7: r'7F', 8: r'8 NF', 9: r'9 NF'}
nx.draw_networkx_labels(G, pos, labels={n: lab for n, lab in labels.items() if n in pos})
plt.show()
nx.draw_networkx_labels(G, pos)


from django.http import HttpResponse
from django.shortcuts import render
from .models import *

def home_view(request):
    names = ['Jitendra', 'Rimaljit', 'Mohit', 'Deepak']
    address = ['Chandigarh', 'Ludhiana', 'Ludhiana', 'Ludhian']
    info = zip(names, address)
    data = {
        'info': info

    }

    return render(request, 'home.html', context=data)

def about_view(request):
    qs = AdmissionDetails.objects.all()
    data = {'queryset': qs}

    return render(request, 'about.html', context=data)

def login_view(request):
    data = {}
    if (request.method=='POST'):

        ad_no = request.POST.get('adno_text')
        name = request.POST.get('name_text')
        section = request.POST.get('class_text')

        # orm code to store value in database
        # admission_details = AdmissionDetails()
        # admission_details.application_no = ad_no
        # admission_details.name = name
        # admission_details.section = section
        # admission_details.save()

        # OR

        # admission_details = AdmissionDetails(
        #         application_no = ad_no,
        #         name = name,
        #         section = section
        #     )

        # admission_details.save()

        # OR

        AdmissionDetails.objects.create(
                application_no = ad_no,
                name = name,
                section = section
            )

        #********************
        data['result'] = "your record has been saved"

    return render(request, 'login.html', context=data)


from ..utils import Object

class GetLanguagePackStrings(Object):
    """
    Returns strings from a language pack in the current localization target by their keys. Can be called before authorization

    Attributes:
        ID (:obj:`str`): ``GetLanguagePackStrings``

    Args:
        language_pack_id (:obj:`str`):
            Language pack identifier of the strings to be returned
        keys (List of :obj:`str`):
            Language pack keys of the strings to be returned; leave empty to request all available strings

    Returns:
        LanguagePackStrings

    Raises:
        :class:`telegram.Error`
    """
    ID = "getLanguagePackStrings"

    def __init__(self, language_pack_id, keys, extra=None, **kwargs):
        self.extra = extra
        self.language_pack_id = language_pack_id  # str
        self.keys = keys  # list of str

    @staticmethod
    def read(q: dict, *args) -> "GetLanguagePackStrings":
        language_pack_id = q.get('language_pack_id')
        keys = q.get('keys')
        return GetLanguagePackStrings(language_pack_id, keys)


"""Tools for looking for instances of godaddy cookie stuffing in individual
and collections of BroRecords."""

import re
from .affiliate import AffiliateHistory, FULL_DOMAIN

class ClickCashAffiliateHistory(AffiliateHistory):

    @classmethod
    def checkout_urls(cls):
        """Returns a list of strings, each of which, if found in a url
        on the current marketer, would count as a checkout attempt.  So,
        for example, returning "add-to-cart" would cause a request to
        "example.org/shopping/add-to-cart/item" to count as a checkout
        attempt.

        Return:
            A tuple or list of zero or more strings
        """
        return (

        )

    @classmethod
    def referrer_tag(cls, record):
        return "wid" if "wid=" in record.uri else "WID"

    @classmethod
    def cookie_set_pattern(cls):
        try:
            return cls._cookie_set_pattern
        except AttributeError:
            cls._cookie_set_pattern = re.compile(r'(?:&|\?|^|;)wid=', re.I)
            return cls._cookie_set_pattern

    @classmethod
    def domains(cls):
        return [
            ("www.model-perfect.com", FULL_DOMAIN),
            ("www.ifriends.net", FULL_DOMAIN),
            ("www.incrediblecams.com", FULL_DOMAIN),
            ("www.sizzlingcams.com", FULL_DOMAIN),
            ("www.webcamdating.us", FULL_DOMAIN),
            ("www.babefever.net", FULL_DOMAIN),
        ]

    @classmethod
    def name(cls):
        return "ClickCash Affiliate"


# Lab 24 - Rain Data

import datetime
import matplotlib.pyplot as plt

def open_file():
  """opens the file and returns its contents as a list separated by each row"""
  with open('lab24_ankeny_rain.txt', 'r') as data:
    return data.read().split("\n")

def get_dates(data):
  """accepts data and returns a list of just the dates"""
  dates = []
  for row in data:
    row_as_list = row.split(" ")
    for value in row_as_list:
      if len(value) == 11:
        dates.append(value)
  return dates

def get_daily_totals(data):
  """accepts data and returns a list of the first integer located in each row, i.e. the daily total for each date"""
  daily_totals = []
  for row in data:
    row_as_list = row.split(" ")
    for value in row_as_list:
      if value == "-":
        value = 0
      try:
        num = int(value)
        daily_totals.append(num)
        break
      except:
        pass
  return daily_totals

def get_merged_data(dates, daily_totals):
  """accepts two lists and zips together to return as a list of tuples"""
  merged_data = list(zip(dates, daily_totals))
  return merged_data

def calculate_mean(daily_totals):
  """calculates the mean of the data by dividing the total sum by the length of the list"""
  sum_total = sum(daily_totals)
  return sum_total / len(daily_totals)

def calculate_variance(mean, daily_totals):
  """calculates the variance by accepting the mean; then, for each number, subtracts the mean and squares the result; finally, returns the average of those squared differences"""
  sq_diffs = [((num - mean) ** 2) for num in daily_totals]
  sum_sq_diff = sum(sq_diffs)
  return sum_sq_diff / len(sq_diffs)

def get_max_rain_date(merged_data):
  """iterates through the tuples of merged rain data and returns the date with the highest rain total"""
  rain_totals = [date[1] for date in merged_data]
  max_rain_total = max(rain_totals)
  max_rain_date = [date[0] for date in merged_data if date[1] == max_rain_total]
  return max_rain_date[0]

def print_results(mean, variance, max_rain_date):
  """accepts results and prints each back to the user"""
  print(f"\nThe mean of the data is {mean}.")
  print(f"The variance of the data is {variance}.")
  print(f"The date which had the most rain was {max_rain_date}.\n")

def plot_results(dates, daily_totals):
  """creates an x, y graph with the dates along the x-axis and the daily totals along the y-axis"""
  plt.plot(dates, daily_totals)
  plt.ylabel("Daily Totals")
  plt.xlabel("Dates")
  plt.show()

def main():
  data = open_file()
  dates = get_dates(data)
  daily_totals = get_daily_totals(data)
  merged_data = get_merged_data(dates, daily_totals)
  mean = calculate_mean(daily_totals)
  variance = calculate_variance(mean, daily_totals)
  max_rain_date = get_max_rain_date(merged_data)
  print_results(mean, variance, max_rain_date)
  plot_results(dates, daily_totals)

main()


from deep_introspection import network, synthesis, utils
import numpy as np
import matplotlib.pyplot as plt

def test_synthesise_loss_is_low():
    net = network.CaffeNet('deep_introspection/test/VGG.prototxt', 'deep_introspection/test/VGG_ILSVRC_16_layers.caffemodel')
    img, offset, resFac, newSize = utils.imgPreprocess(img_path='deep_introspection/test/cat.jpg')

    net.set_new_size(newSize)
    print(np.argmax(np.mean(net.predict(img), axis=0)))

    layer = net.get_layer_names()[-1]
    print("Testing layer " + layer)

    target_rep = net.get_activations(layer)

    solution, loss = synthesis.synthesise(net, target_rep,layer)
    net.set_new_size(solution.shape[:2])
    net.predict(solution)

    assert(solution.shape == (224,224,3))
    assert(synthesis.loss(net.get_activations(layer), target_rep) < 0.1)

def test_boundary():

    net = network.CaffeNet('deep_introspection/test/VGG.prototxt', 'deep_introspection/test/VGG_ILSVRC_16_layers.caffemodel')
    img, offset, resFac, newSize = utils.imgPreprocess(img_path='deep_introspection/test/cat.jpg')

    net.set_new_size(newSize)

    layer = net.get_layer_names()[-2]
    solution, loss = synthesis.synthesise_boundary(net, img, layer,  185, 120, 140, 95)

    assert(solution.shape == (224,224,3))

def test_boundary_synthesis():

    net = network.CaffeNet('deep_introspection/test/VGG.prototxt', 'deep_introspection/test/VGG_ILSVRC_16_layers.caffemodel')

    target = np.zeros(1000)
    target[285] = 0.5
    target[7] = 0.5
    solution, loss = synthesis.synthesise(net, target, 'prob')

    assert(solution.shape == (224,224,3))


from asyncore import read

#JMP:xkalou03

__author__ = 'xkalou03'

import sys
import argparse
import re

import reader
import table

def checkParameters():

    parser = argparse.ArgumentParser(description = 'Projekt do IPP.', add_help = False)

    parser.add_argument('--help', action = "count", default = 0, help = 'Prints help')
    parser.add_argument('--input=', action = "store", default = [], dest = "input", nargs = 1, help = 'Input file')
    parser.add_argument('--output=', action = "store", default = [], dest = "output", nargs = 1, help = 'Output file')
    parser.add_argument('--cmd=', action = "store", default = "", nargs = '+', dest = "text", help = 'Input text')
    parser.add_argument('-r', action = "store_true", dest = "redef", default = False, help = 'Redefination macros')

    try:
        args = parser.parse_args()
    except:
        print("Parameters Error", file = sys.stderr)
        exit(1)

    if(args.help == 1):

        if len(sys.argv) == 2:
            print(parser.print_help())
            exit(0)
        else:
            print("Zadany help + jine parametry", file = sys.stderr)
            exit(1)

    return args

def readInput(input, reader, table, params):

    stringIn = ""
    rest = ""
    outputString = ""

    count = 0

    c = reader.getc()

    while c:

        if c == '@':
            x = macro(reader, table, params, c)

            if x in {'@', '{', '}', '$'}:
                outputString += x

        elif c in {'$', '}'}:
            exit(55)

        elif c == '{':

            block = reader.readBlock(False)
            if block != None:
                outputString += block

            else:

                exit(55)

        else:
            outputString += c

        c = reader.getc()

    return outputString

def macro(reader, table, params, x):

    macroString = ""
    reg = '^[a-zA-Z_][0-9a-zA-Z_]*$'
    i = 0

    c = reader.getc()

    if c in {'@', '{', '}', '$'} and x == '@':  # kontrola, zda nejde o escape sekvenci
        return c

    while c:

        i += 1
        if re.match(reg, macroString + c):

            macroString += c
            c = reader.getc()

            if not c:

                find = table.readMacro(macroString)

                if find:
                    argumentsMacro(reader, find, c, table, params)
                    c = '%'
                    break

                else:

                    print("Toto makro neexistuje", file = sys.stderr)
                    exit(56)

            continue

        else:
            # mam precten nazev - po kouknuti do tabulky budu koukat dale
            # print("Budu koukat do tabulky", macroString)

            find = table.readMacro(macroString)

            if find:
                argumentsMacro(reader, find, c, table, params)
                c = '%'
                break

            else:
                if i == 1:
                    exit(55)

                print("Toto makro neexistuje", file = sys.stderr)
                exit(56)

        macroString += c
        c = reader.getc()

    return c

def argumentsMacro(reader, find, x, table, params):

    name = ""
    temp = False

    if params.redef:
        temp = True
    c = x   # pismeno po nazvu makra

    if find.name == 'def' or find.name == '__def__':

        table.insertMacro(reader, x, temp)

    elif find.name == 'undef' or find.name == '__undef__':

        table.deleteMacro(reader, x)

    elif find.name == 'set' or find.name == '__set__':

        table.setMacro(reader, x)

    else:

        stringExpand = table.expandMacro(reader, x, find.name)
        reader.attachString(stringExpand)

    return

def main():

    params = checkParameters()

    if params.input:                           # Otevreni vstupniho souboru
        try:
            inputFile = open(params.input[0], 'r')

        except IOError:
            print("Vstupni soubor nejde otevrit", file = sys.stderr)
            exit(2)
    else:
        inputFile = sys.stdin

    if params.output:                          # Otevreni vystupniho souboru
        try:
            outputFile = open(params.output[0], 'w')

        except IOError:
            print("Soubor nejde otevrit", file = sys.stderr)
            exit(2)
    else:
        outputFile = sys.stdout

    r = reader.Reader(inputFile)    # vytvoreni readeru
    if params.text:
        r.attachString(str(params.text[0]))

    macroTable = table.Table()      # vytvoreni tabulky maker
    stringInput = readInput(inputFile, r, macroTable, params)   # spusteni cteni vstupu

    if params.output:
        print(stringInput, file = outputFile, end="")  # vytisknuti vystupu
    else:
        print(stringInput, file = sys.stdout)

if __name__ == "__main__":
   main()


# coding: utf-8
# device: Mi8SE
# screen: 2244x1080
# adb support
# open USB debugging and allow simulating input via USB debugging
import os
import time

fir_in_button_x, fir_in_button_y = 1485, 870  # first button, click to start
check_in_button_x, check_in_button_y = 1400, 750   # dialog：英雄不足三个是否继续
time_waiting = 13   # loading time (second)
skip_x, skip_y = 2060, 65      # skip useless story
direc_x1, direc_y1, direc_x2, direc_y2 = 900, 364, 995, 485  # two mark points, calculate the direction
direc_tan = (direc_y2 - direc_y1) / (direc_x2 - direc_x1)
magic_num1 = 35      # magic number to fix the direction so that you can walking straight
move_x, move_y = 450, 850    # the center of the left joystick
time_move1 = 32000     # time from the very begining to the boss, fixing according to your own moving speed (millisecond)
ult_ability_x, ult_ability_y = 1860, 620   # the position of the ultimate ability joystick(就是大招，反正之前打dota大招叫ultimate ability)
sec_ability_x, sec_ability_y = 1650, 750   # second ability
A_x, A_y = 1850, 900      # basic attack
again_x, again_y = 1800, 980    # button(再次挑战)

while(True):
	os.system('adb shell input swipe {} {} {} {} {}'.format(fir_in_button_x, fir_in_button_y, fir_in_button_x, fir_in_button_y, 10)) # start the game (see: 1.png)
	os.system('adb shell input swipe {} {} {} {} {}'.format(check_in_button_x, check_in_button_y, check_in_button_x, check_in_button_y, 10))  # check dialog (see: 2.png)
	time.sleep(time_waiting)  # wait for loading
	os.system('adb shell input swipe {} {} {} {} {}'.format(skip_x, skip_y, skip_x, skip_y, 10))     # skip story
	print('move')
	os.system('adb shell input swipe {} {} {} {} {}'.format(move_x, move_y, move_x-50, move_y-50*direc_tan-magic_num1, time_move1)) # move to the boss
	print('attack')
	os.system('adb shell input swipe {} {} {} {} {}'.format(ult_ability_x, ult_ability_y, ult_ability_x, ult_ability_y, 3)) # ultimate ability
	os.system('adb shell input swipe {} {} {} {} {}'.format(sec_ability_x, sec_ability_y, sec_ability_x, sec_ability_y, 3)) # second ability
	print('A')
	for i in range(40):
		os.system('adb shell input swipe {} {} {} {} {}'.format(A_x, A_y, A_x, A_y, 1))   # basic attack and skip following story
	print('done')
	os.system('adb shell input swipe {} {} {} {} {}'.format(again_x, again_y, again_x, again_y, 10))  # play again?
	time.sleep(3)


#Avoid Duplicates

print("Enter a list of words; press the enter button twice to finish>>")

words = []#List

while True:#Collects words from the user.
    word = input()

    if word not in words:#If the word entered doesn't already exist in the list, append it to the list.
       words.append(word)

    if word == "":
        break

words.remove(words[-1])#Removes the blank space from the list.

print(words)


a=""

# infix to postfix conversion
def infix_to_postfix(infix):
    prec = {}
    prec["*"] = 3
    prec["/"] = 3
    prec["+"] = 2
    prec["-"] = 2
    prec["("] = 1
    op_stack = []
    postfix_list = []
    token_list = infix.split()
    for token in token_list:
        if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
            postfix_list.append(token)
        elif token == '(':
            op_stack.append(token)
        elif token == ')':
            top_token = op_stack.pop()
            while top_token != '(':
                postfix_list.append(top_token)
                top_token = op_stack.pop()
        else:
            while (len(op_stack) > 0) and (prec[op_stack[-1]] >= prec[token]):
                  postfix_list.append(op_stack.pop())
            op_stack.append(token)
    while len(op_stack) > 0:
        postfix_list.append(op_stack.pop())
    return " ".join(postfix_list)

a=infix_to_postfix("(-b+((b*b)-4*a*c)^(0.5)/(2*a))")
print(a)


'''
 This library is compouse of the units that we are going to adopt for the different magnitudes
'''

# Magnitudes

r = 10**10  #cm (radius)
P = 10**15  #din cm−2 (pressure)
T = 10**7   #K (temperatura)
M = 10**33  #g (mass)
L = 10**33  #erg s−1 (luminosity)
rho = 1     #g cm−3 (density)
epsilon = 1 #erg g−1 s−1 (energy rate production)
kappa = 1   #cm2 g−1 (opacity)

# Parameters

X = 0.80      # Hydrogen fraction
Y = 0.16      # Helium fraction
Z = 1 - X - Y # Heavy elements fraction

mu = 1 / (2*X + 0.75*Y + 0.5*Z) # mu - reduce mass

MTot = 5                        # Total radius
RTot_initial = 12               # Initial Radius
LTot_initial = 40               # Initial Luminosity
layers = 100                    # Number of layers in the star


"""
Python data structures and algo practice
"""

class Node:
    """
	Basic node object type

	left_node ==> memory pointer for binary tree example
	right_node ==> memory pointer for binary tree example
	next_node ==> memory pointer for queue / stack examples

	data ==> simple value (int)
	"""

    def __init__(self, data: int =None, next_node= None):
        self.data = data  # is data is passed asign it
        self.left_node = None
        self.right_node = None
        self.next_node = next_node

##########################################
# example of a stack implementation using a stack class

'''
class EmptyStackException(Exception):
    pass

class Stack(object):
    def __init__(self):
        self.head = None

    def push(self, value: int) -> None:
        self.head = Node(value, self.head)

    def pop(self) -> int:
        if self.head is None:
            raise EmptyStackException("Pop from empty stack.")

        value = self.head.value
        self.head = self.head.next

        return value

    def peek(self) -> int:
        if self.head is None:
            raise EmptyStackException("Peek from empty stack.")

        return self.head.value

    def is_empty(self) -> bool:
        return self.head is None
'''

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

"""
Queue

FIFO Linear data structure with flexible sizing
"""

# asign H/T to null objects to make logic easier
head_node = None # remove nodes here
tail_node = None # add nodes here

def queue_is_empty():
    if head_node == None:
        return True
    else:
        return False

def queue_peek():
    if head_node == None:
        return "error msg: queue is empty"
    else:
        return head_node.data

def queue_add(data):
    global tail_node, head_node  # pull global vars into scope to update values

    new_node = Node(data)

    if (tail_node != None):
        tail_node.next_node = new_node  # point current tail's next at new_node

    tail_node = new_node  # set tail to point at new_node

    if (head_node == None):
        head_node = new_node

def queue_remove():
    global head_node, tail_node  # pull global vars into scope to update values

    data = head_node.data
    head_node = head_node.next_node
    if (head_node == None):
        tail_node = None

    return data

"""
Stack

FIFO data structure with flexible sizing
"""

top_node = None  # global var for stack

def stack_is_empty():
    return (top_node == None)

def stack_peek():
    if top_node == None:
        return "error msg: stack is empty"
    else:
        return top_node.data

def stack_push(data):
    global top_node # grab global top_node to update
    new_node = Node(data, top_node) # make new node with nex_node to top_node
    top_node = new_node

def stack_pop():
    global top_node # get global top node to update

    data = top_node.data # top_node data to return
    top_node = top_node.next_node # remove top node from stack
    return data

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

def queue_run():
    test_node = Node(24)
    print("Test node is a ", test_node.__class__)
    print("\nLet's use these nodes to make a queue...\n")

    print("Is queue empty? ", queue_is_empty())
    print(queue_peek())

    print("\nOk. Now lets add a few nodes... 2, 34, 53, 6")
    queue_add(2)
    queue_add(34)
    queue_add(53)
    queue_add(6)
    print("Now if we run remove on our queue we get ==> " +
          str(queue_remove()))
    print("\nThen running it again we get...")
    print("==> " + str(queue_remove()))
    print("==> " + str(queue_remove()))
    print("==> " + str(queue_remove()))
    print("\nNow is the queue empty? ", queue_is_empty())

def stack_run():
    print("\n\n Let's make a stack! \n")
    print("Lets peek our stack to make sure its empty... \n", stack_peek())
    print("\nNow let's push a few nodes onto the stack... 32, 80, 443, 8080")

    stack_push(32)
    stack_push(80)
    stack_push(443)
    stack_push(8080)

    print("\nNow when we run is_empty() it returns...\n==> ", stack_is_empty())
    print("\nNow lets pop the nodes off the stack...")
    print(stack_pop())
    print(stack_pop())
    print(stack_pop())
    print(stack_pop())
    print("\nNow when we check if the stack is empty it returns...\n", stack_is_empty())

def binary_search_tree_run():
    """
	Binary search tree

	create tree using ordered insert and lookup to get log N memory/time space

	value on left child is less than parent and right child is more
	binary search trees must be balanced to avoid losing log n time

	"""

    # no need for Tree object as the Tree itself is a concept; its made of connected nodes
    # nodes are the object; connections are self contained

    def binary_insert(root, node):
        if root is None:
            root = node
        else:
            if root.data > node.data:
                if root.l_child is None:
                    root.l_child = node
                else:
                    binary_insert(root.l_child, node)
            else:
                if root.r_child is None:
                    root.r_child = node
                else:
                    binary_insert(root.r_child, node)

    def in_order_print(root):
        if not root:
            return
        in_order_print(root.l_child)
        print(root.data)
        in_order_print(root.r_child)

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

def main():
    queue_run()
    stack_run()
    binary_search_tree_run()

if (__name__ == "__main__"):
    main()


# -*- coding: utf-8 -*-
"""
Created on Sat May  8 12:16:46 2021

@author: tamon
"""

import csv
import numpy as np
from scipy.interpolate import griddata
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

radius = []
angles = []
points = []
result = []

with open('angrad.csv', newline='') as csvfile:
    readfile = csv.reader(csvfile, quotechar='|')
    for row in readfile:
        radius.append(row[12])
        angles.append(row[13])
        result.append(row[20])

radius.pop(0)
angles.pop(0)
result.pop(0)
radius = [int(i) for i in radius]
angles = [int(i) for i in angles]
for i in range(len(radius)):
    points.append([angles[i], radius[i]])
result = [np.float64(i) for i in result]

xgrid, ygrid = np.mgrid[10:90:1000j, 30:240:1000j]
grid = griddata(points, result, (xgrid, ygrid), method='cubic')

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(angles, radius, 'k.', ms=1)
sp = ax.imshow(grid.T, cmap='jet', extent=(10, 90, 30, 240), origin='lower')
ax.set_aspect(80/(210))
ax.set_xlabel('Angle [deg]')
ax.set_ylabel('Radius [mm]')
clb = fig.colorbar(sp)
clb.set_label('Equivelant Maximum Stress [Pa]')
fig.savefig('angrad.pdf', format='pdf', bbox_inches='tight')
plt.show()

angslice1 = []
angslice2 = []
angslice3 = []
angs = np.linspace(10, 90, 9)
j = 1
for j in range(9):
    angslice1.append(result[8*j + 0])
    angslice2.append(result[8*j + 1])
    angslice3.append(result[8*j + 2])

xnew = np.linspace(10, 90, 200)
f1 = interp1d(angs, angslice1, kind='cubic')
f2 = interp1d(angs, angslice2, kind='cubic')
f3 = interp1d(angs, angslice3, kind='cubic')
plt.plot(xnew, f1(xnew), 'r', label='Radius=30 [mm]')
plt.plot(xnew, f2(xnew), 'b', label='Radius=60 [mm]')
plt.plot(xnew, f3(xnew), 'g', label='Radius=90 [mm]')
plt.grid('major')
plt.legend(loc='lower right')
plt.xlabel('Angle [deg]')
plt.ylabel('Equivelant Maximum Stress [Pa]')
plt.savefig('angslice.pdf', format='pdf', bbox_inches='tight')

# angslice1 = []
# angslice2 = []
# angslice3 = []
# angs = np.linspace(10, 90, 9)
# j = 1
# for j in range(9):
#     angslice1.append(result[8*j + 0])
#     angslice2.append(result[8*j + 1])
#     angslice3.append(result[8*j + 2])

# xnew = np.linspace(10, 90, 200)
# f1 = interp1d(angs, angslice1, kind='cubic')
# f2 = interp1d(angs, angslice2, kind='cubic')
# f3 = interp1d(angs, angslice3, kind='cubic')
# plt.plot(xnew, np.gradient(f1(xnew)), 'r', label='Radius=30 [mm]')
# plt.plot(xnew, np.gradient(f2(xnew)), 'b', label='Radius=60 [mm]')
# plt.plot(xnew, np.gradient(f3(xnew)), 'g', label='Radius=90 [mm]')
# plt.grid('major')
# plt.legend(loc='lower right')
# plt.xlabel('Angle [deg]')
# plt.ylabel('Equivelant Maximum Stress [Pa]')
# plt.savefig('angslice.pdf', format='pdf', bbox_inches='tight')

radslice1 = result[:8]
radslice2 = result[8:16]
radslice3 = result[16:24]
radslice4 = result[24:32]
radslice5 = result[32:40]
radslice6 = result[40:48]
radslice7 = result[48:56]
rads = np.linspace(30, 240, 8)

xnew = np.linspace(30, 240, 200)
f1 = interp1d(rads, radslice1, kind='cubic')
f2 = interp1d(rads, radslice2, kind='cubic')
f3 = interp1d(rads, radslice3, kind='cubic')
f4 = interp1d(rads, radslice4, kind='cubic')
f5 = interp1d(rads, radslice5, kind='cubic')
f6 = interp1d(rads, radslice6, kind='cubic')
f7 = interp1d(rads, radslice7, kind='cubic')

fig2 = plt.figure()
ax2 = plt.subplot(111)
ax2.plot(xnew, f1(xnew), 'r', label='Radius=10 [mm]')
ax2.plot(xnew, f2(xnew), 'b', label='Radius=20 [mm]')
ax2.plot(xnew, f3(xnew), 'g', label='Radius=30 [mm]')
ax2.plot(xnew, f4(xnew), 'y', label='Radius=40 [mm]')
ax2.plot(xnew, f5(xnew), 'orange', label='Radius=50 [mm]')
ax2.plot(xnew, f6(xnew), 'cyan', label='Radius=60 [mm]')
ax2.plot(xnew, f7(xnew), 'purple', label='Radius=70 [mm]')
ax2.grid('major')
chartBox = ax2.get_position()
ax2.set_position([chartBox.x0, chartBox.y0, chartBox.width*0.6, chartBox.height])
ax2.legend(loc='upper center', bbox_to_anchor=(1.4, 0.8), shadow=True, ncol=1)
ax2.set_xlabel('Radius [mm]')
ax2.set_ylabel('Equivelant Maximum Stress [Pa]')
fig2.savefig('radslice.pdf', format='pdf', bbox_inches='tight')


import os
from setuptools import setup

def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
    name='snmpdesk',
    version='0.0.91',
    description='Scripts for easy get snmp data',
    author='Svintsov Dmitry',
    author_email='spam@19216801.ru',
    url='http://github.com/uralbash/snmpdesk/',
    keywords = "snmp",
    install_requires=['pysnmp'],
    license='GPL',
    packages=['snmpdesk'],
    long_description=read('README'),
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Environment :: Console',
        'Intended Audience :: System Administrators',
        'License :: OSI Approved :: GNU General Public License (GPL)',
        'Natural Language :: English',
        'Natural Language :: Russian',
        'Operating System :: Unix',
        'Programming Language :: Python',
        'Topic :: System :: Networking :: Monitoring',
        ],
)


board=["-","-","-","-","-","-","-","-","-"]
current_player='X'
game_is_on=True
winner=None
print('WELCOME')
print("Let's  play Tic Tac Toe")
print('Plz give the reqiured detail')
player1=input('Player 1 name:')
player2=input('Player 2 name:')
current_name=player1
print(player1 + ' has been assigned with X')
print('AND')
print(player2 + ' has been assigned with 0')
b=True
def display_board():
    print(board[0] + '|'+ board[1]+'|'+ board[2])
    print(board[3] + '|'+ board[4]+'|'+ board[5])
    print(board[6] + '|'+ board[7]+'|'+ board[8])
def position_input(player,name):
    print(name+"'s turn:")
    position=input('Plz Choose a Position from 1-9:')
    valid = True
    while valid:
        while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
             position = input("Choose a position from 1-9: ")
        position = int(position) - 1
        if board[position] == "-":
            valid = False
        else:
            print("You can't go there. Go again.")
    board[position] = player
    display_board()
def give_turn():
    global current_player
    global current_name
    if current_player=='X':
        current_player='0'
    elif current_player=='0':
        current_player='X'
    if current_name == player1:
        current_name=player2
    elif current_name == player2:
        current_name=player1

def check_if_game_over():
    check_win()
    check_if_tie()

def check_win():
    global winner
    row_done=check_row()
    vertical_done=check_vertical()
    diagonal_done=check_diagonal()
    if row_done :
        winner=row_done
    elif vertical_done :
        winner=vertical_done
    elif diagonal_done :
        winner=diagonal_done
    else:
        winner=None
def check_row():
    global game_is_on
    row_1 = board[0] == board[1] == board[2] != "-"
    row_2 = board[3] == board[4] == board[5] != "-"
    row_3 = board[6] == board[7] == board[8] != "-"
    if row_1 or row_2 or row_3:
        game_is_on = False
    if row_1:
        return board[0]
    elif row_2:
        return board[3]
    elif row_3:
        return board[6]

    else:
        return None

def check_vertical():
    global game_is_on
    column_1 = board[0] == board[3] == board[6] != "-"
    column_2 = board[1] == board[4] == board[7] != "-"
    column_3 = board[2] == board[5] == board[8] != "-"
    if column_1 or column_2 or column_3:
        game_is_on = False
    if column_1:
        return board[0]
    elif column_2:
        return board[1]
    elif column_3:
        return board[2]
    else:
        return None
def check_diagonal():
     global game_is_on
     diagonal_1 = board[0] == board[4] == board[8] != "-"
     diagonal_2 = board[2] == board[4] == board[6] != "-"
     if diagonal_1 or diagonal_2:
         game_is_on=False
     if diagonal_1:
         return board[0]
     elif diagonal_2:
         return board[2]
     else:
         return None

def check_if_tie():
    global game_is_on
    if "-" not in board:
        game_is_on = False
        return True
    else:
        return False
def play():
    global b
    global current_player
    #global winner
    print('Are You Ready')
    print("Let's start the game")
    display_board()
    while game_is_on:
        position_input(current_player,current_name)
        check_if_game_over()
        give_turn()
    print('\n')
    if winner=='X' :
        print('CONGRATULATIONS!')
        print(player1 +' won \n' )
        print(player2 +' Better luck next time.')
    elif winner=='0' :
        print('CONGRATULATIONS!')
        print(player2 +' won \n')
        print(player1 +' Better luck next time.')
    elif winner==None:
        print("It's a tie")
#start thw game
play()


#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import (unicode_literals, division, absolute_import,
                        print_function)

__license__   = 'GPL v3'
__copyright__ = '2016, Daisuke Cato <daisuke.cato@gmail.com>'
__docformat__ = 'restructuredtext en'

try:
    from PyQt5 import QtWidgets as QtGui
    from PyQt5.Qt import QWidget, QGridLayout, QLabel, QLineEdit, QPushButton
except ImportError as e:
    from PyQt4 import QtGui
    from PyQt4.Qt import QWidget, QGridLayout, QLabel, QLineEdit, QPushButton
from calibre.utils.config import JSONConfig

from calibre_plugins.extract_isbn.common_utils import KeyValueComboBox, KeyboardConfigDialog

STORE_NAME = 'Options'
KEY_PATH_BROWSER = 'pathBrowser'

DEFAULT_STORE_VALUES = {
    KEY_PATH_BROWSER: '',
}

# This is where all preferences for this plugin will be stored
plugin_prefs = JSONConfig('plugins/Bibi Calibre')

# Set defaults
plugin_prefs.defaults[STORE_NAME] = DEFAULT_STORE_VALUES

class ConfigWidget(QWidget):

    def __init__(self, plugin_action):
        QWidget.__init__(self)
        self.plugin_action = plugin_action
        layout = QGridLayout(self)
        self.setLayout(layout)

        c = plugin_prefs[STORE_NAME]

        layout.addWidget(QLabel('Browser command:', self), 0, 0)
        path_browser = c.get(KEY_PATH_BROWSER, DEFAULT_STORE_VALUES[KEY_PATH_BROWSER])
        self.path_browser_ledit = QLineEdit(path_browser, self)
        layout.addWidget(self.path_browser_ledit, 1, 0)

    def save_settings(self):
        new_prefs = {}
        new_prefs[KEY_PATH_BROWSER] = unicode(self.path_browser_ledit.text())

        plugin_prefs[STORE_NAME] = new_prefs


import functools

from importlib import import_module

from pyws.public import InvalidPath

def route(path):
    def wrapper(func):
        @functools.wraps(func)
        def _wrapper(*args, **kwargs):
            return func(*args, **kwargs)

        _wrapper.__route__ = path
        return _wrapper

    return wrapper

class Route:
    routes = {}

    @classmethod
    def add_routes(cls, module_name):
        point = module_name.rfind('.')
        if point == (-1):
            mod = import_module(module_name)
        else:
            mod = getattr(import_module(module_name[:point]), module_name[point + 1:])
        for attr in dir(mod):
            if attr.startswith('_'):
                continue
            func = getattr(mod, attr)
            path = getattr(func, '__route__', None)
            if path and callable(func):
                cls.routes.setdefault(path, func)

    @classmethod
    def get(cls, path):
        func = cls.routes.get(path)
        if not func:
            raise InvalidPath
        return func


import random
import os
import argparse
from typing import DefaultDict

# from paper "Learning Unknown from Correlations:
# Graph Neural Network for Inter-novel-protein Interaction Prediction"
standard_acids = [
        ('A', 1), ('C', 6), ('D', 5), ('E', 7), ('F', 2),
        ('G', 1), ('H', 4), ('I', 2), ('K', 5), ('L', 2),
        ('M', 3), ('N', 4), ('P', 2), ('Q', 7), ('R', 4),
        ('S', 3), ('T', 3), ('V', 1), ('W', 4), ('Y', 3)]

class PPI:
    def __init__(self, fst_pro, sec_pro, label=1):
        self.fst_pro = fst_pro
        self.sec_pro = sec_pro
        self.label = label
        assert label == 1 or label == 0

    def __eq__(self, __o: object) -> bool:
        if (self.fst_pro == __o.fst_pro and self.sec_pro == __o.sec_pro) or \
            (self.fst_pro == __o.sec_pro and self.sec_pro == __o.fst_pro):
            return True
        else:
            return False

    def __str__(self) -> str:
        return f"{self.fst_pro}\t{self.sec_pro}\t{str(self.label)}\n"

def handle(pairfp, fastafp):
    with open(pairfp, "r") as f:
        ppi_lines = f.readlines()
    with open(fastafp, "r") as f:
        seq_lines = f.readlines()

    # 存储正样本邻居，tsv文件中负样本总在最后
    pneighbors = DefaultDict(set)

    ppis = []
    proteins = set()
    for ppiline in ppi_lines:
        fpro, spro, label = ppiline.split()
        proteins.update((fpro, spro))
        label = int(float(label))
        if label == 0:
            if spro in pneighbors[fpro] or fpro in pneighbors[spro]:
                # 标签冲突，作为正样本
                continue
        elif label == 1:
            pneighbors[fpro].add(spro)
            pneighbors[spro].add(fpro)
        else:
            raise ValueError
        ppis.append(PPI(fpro, spro, label))

    acid_seqs = {}
    for idx in range(0, len(seq_lines), 2):
        key = seq_lines[idx].strip()[1:].strip()
        value = seq_lines[idx+1].strip()
        if key in proteins:
            acid_seqs[key] = value
    assert len(acid_seqs) == len(proteins)
    return ppis, acid_seqs

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--pair_dir", type=str, default="./data/dscript/data/pairs")
    parser.add_argument("--seq_dir", type=str, default="./data/dscript/data/seqs")
    parser.add_argument('--processed_dir', type=str, default="./data/dscript/processed")

    # 过滤样本的参数
    parser.add_argument('--max_length', type=int, default=800)
    parser.add_argument('--min_length', type=int, default=50)
    args = parser.parse_args()

    if not os.path.exists(args.processed_dir):
        os.mkdir(args.processed_dir)
        os.mkdir(os.path.join(args.processed_dir, "pairs"))
        os.mkdir(os.path.join(args.processed_dir, "seqs"))

    pair_fns = os.listdir(args.pair_dir)
    for pairfn in pair_fns:
        organism = pairfn.split("_")[0].strip()
        pairfp = os.path.join(args.pair_dir, pairfn)
        fastafp = os.path.join(args.seq_dir, organism + ".fasta")
        ppis, acid_seqs = handle(pairfp, fastafp)

        # 被丢弃的蛋白质
        dropout_proteins = {"Too short": [], "Too long": [],} # "With non-standard acids": []}
        # 蛋白质长度
        protein_lengths = []
        for pro, acid_seq in acid_seqs.items():
            # 筛选蛋白质
            qualified = False
            if len(acid_seq) < args.min_length:
                dropout_proteins['Too short'].append(pro)
            elif len(acid_seq) > args.max_length:
                dropout_proteins['Too long'].append(pro)
            # elif len(set(list(acid_seq)) - set([acid[0] for acid in standard_acids])) > 0:
            #     dropout_proteins['With non-standard acids'].append(pro)
            else:
                qualified = True
            if qualified:
                protein_lengths.append((pro, len(acid_seq)))

        # 输出蛋白质的数量等信息
        if sum([len(value) for value in dropout_proteins.values()]) > 0:
            print(f"============{pairfn.split('.')[0]} Dataset Filter============")
            print(f"Total {len(acid_seqs)} proteins.")
            print(f"\tFilter {len(dropout_proteins['Too short'])} because they are too short.")
            print(f"\tFilter {len(dropout_proteins['Too long'])} because they are too long.")
            # print(f"\tFilter {len(dropout_proteins['With non-standard acids'])} because they have non-standard acids.")
            # 集合过滤掉的蛋白质
            dropout_proteins = set(dropout_proteins['Too long'] + dropout_proteins['Too short'])
            # + dropout_proteins['With non-standard acids'])

            # 删除不合格的蛋白质和ppi
            ppis = [ppi for ppi in ppis if ppi.fst_pro not in dropout_proteins and ppi.sec_pro not in dropout_proteins]
            acid_seqs = {key: value for key, value in acid_seqs.items() if key not in dropout_proteins}

        with open(os.path.join(args.processed_dir, "pairs", pairfn), "w") as f:
            f.writelines([str(ppi) for ppi in ppis])
        with open(os.path.join(args.processed_dir, "seqs", pairfn.split(".")[0] + ".fasta"), "w") as f:
            f.writelines([f"{pro}\t{sequence}\n" for pro, sequence in acid_seqs.items()])

        # print statistics
        print(f"============{pairfn.split('.')[0]} Dataset Statistics============")
        print(f'Total {len(ppis)} positive samples:')
        print(f'\t Positive: {len([ppi for ppi in ppis if ppi.label == 1])}')
        print(f'\t Negative: {len([ppi for ppi in ppis if ppi.label == 0])}')

        print(f"Total {len(acid_seqs)}  Proteins:" )
        print(f"\tMax length of protein: {max([pro[1] for pro in protein_lengths])}")
        print(f"\tMin length of protein: {min([pro[1] for pro in protein_lengths])}")
        print(f"\tAvg length of protein: {round(sum([pro[1] for pro in protein_lengths])/len(protein_lengths), 3)}")


#!/usr/bin/python

import json
import re
import os
import sys

# Get data.json from Twitter for
# Followers: https://oauth-playground.glitch.me/?id=usersIdFollowers&params=%28%27user.fields%21%27description%27%29_
# Followings: https://oauth-playground.glitch.me/?id=usersIdFollowing&params=%28%27user.fields%21%27description%27%29_

with open(os.path.join(sys.path[0], "data.json"), "r") as fileData:
    jsonData = json.load(fileData)

    mastodonUrlRegex = re.compile(r'@\w*@\w*\.\w*')
    webUrlRegex = re.compile(r'http(s?)://.*/@\w*')

    for follower in jsonData['data']:
        name = follower['name']
        username = follower['username']
        description = follower['description']

        match1 = mastodonUrlRegex.search(description)
        if match1:
            print("%s (@%s) - %s" % (name, username, match1.group()))

        match2 = webUrlRegex.search(description)
        if match2:
            print("%s (@%s) - %s" % (name, username, match2.group()))


__revision__ = "src/engine/SCons/Tool/JavaCommon.py bee7caf9defd6e108fc2998a2520ddb36a967691 2019-12-17 02:07:09 bdeegan"

import os
import os.path
import re
import glob

java_parsing = 1

default_java_version = '1.4'

# a switch for which jdk versions to use the Scope state for smarter
# anonymous inner class parsing.
scopeStateVersions = ('1.8')

# Glob patterns for use in finding where the JDK is.
# These are pairs, *dir_glob used in the general case,
# *version_dir_glob if matching only a specific version.
# For now only used for Windows.
java_win32_dir_glob = 'C:/Program Files*/Java/jdk*/bin'
# On windows, since Java 9, there is a dash between 'jdk' and the version
# string that wasn't there before. this glob should catch either way.
java_win32_version_dir_glob = 'C:/Program Files*/Java/jdk*%s*/bin'

# Glob patterns for use in finding where the JDK headers are.
# These are pairs, *dir_glob used in the general case,
# *version_dir_glob if matching only a specific version.
java_macos_include_dir_glob = '/System/Library/Frameworks/JavaVM.framework/Headers/'
java_macos_version_include_dir_glob = '/System/Library/Frameworks/JavaVM.framework/Versions/%s*/Headers/'

java_linux_include_dirs_glob = [
    '/usr/lib/jvm/default-java/include',
    '/usr/lib/jvm/java-*/include'
]
# Need to match path like below (from Centos 7)
# /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/include/
java_linux_version_include_dirs_glob = [
    '/usr/lib/jvm/java-*-sun-%s*/include',
    '/usr/lib/jvm/java-%s*-openjdk*/include',
    '/usr/java/jdk%s*/include'
]

if java_parsing:
    # Parse Java files for class names.
    #
    # This is a really cool parser from Charles Crain
    # that finds appropriate class names in Java source.

    # A regular expression that will find, in a java file:
    #     newlines;
    #     double-backslashes;
    #     a single-line comment "//";
    #     single or double quotes preceeded by a backslash;
    #     single quotes, double quotes, open or close braces, semi-colons,
    #         periods, open or close parentheses;
    #     floating-point numbers;
    #     any alphanumeric token (keyword, class name, specifier);
    #     any alphanumeric token surrounded by angle brackets (generics);
    #     the multi-line comment begin and end tokens /* and */;
    #     array declarations "[]".
    _reToken = re.compile(r'(\n|\\\\|//|\\[\'"]|[\'"\{\}\;\.\(\)]|' +
                          r'\d*\.\d*|[A-Za-z_][\w\$\.]*|<[A-Za-z_]\w+>|' +
                          r'/\*|\*/|\[\])')

    class OuterState(object):
        """The initial state for parsing a Java file for classes,
        interfaces, and anonymous inner classes."""

        def __init__(self, version=default_java_version):

            if version not in ('1.1', '1.2', '1.3', '1.4', '1.5', '1.6', '1.7',
                               '1.8', '5', '6', '9.0', '10.0', '11.0', '12.0'):
                msg = "Java version %s not supported" % version
                raise NotImplementedError(msg)

            self.version = version
            self.listClasses = []
            self.listOutputs = []
            self.stackBrackets = []
            self.brackets = 0
            self.nextAnon = 1
            self.localClasses = []
            self.stackAnonClassBrackets = []
            self.anonStacksStack = [[0]]
            self.package = None

        def trace(self):
            pass

        def __getClassState(self):
            try:
                return self.classState
            except AttributeError:
                ret = ClassState(self)
                self.classState = ret
                return ret

        def __getPackageState(self):
            try:
                return self.packageState
            except AttributeError:
                ret = PackageState(self)
                self.packageState = ret
                return ret

        def __getAnonClassState(self):
            try:
                return self.anonState
            except AttributeError:
                self.outer_state = self
                ret = SkipState(1, AnonClassState(self))
                self.anonState = ret
                return ret

        def __getSkipState(self):
            try:
                return self.skipState
            except AttributeError:
                ret = SkipState(1, self)
                self.skipState = ret
                return ret

        def _getAnonStack(self):
            return self.anonStacksStack[-1]

        def openBracket(self):
            self.brackets = self.brackets + 1

        def closeBracket(self):
            self.brackets = self.brackets - 1
            if len(self.stackBrackets) and \
                    self.brackets == self.stackBrackets[-1]:
                self.listOutputs.append('$'.join(self.listClasses))
                self.localClasses.pop()
                self.listClasses.pop()
                self.anonStacksStack.pop()
                self.stackBrackets.pop()
            if len(self.stackAnonClassBrackets) and \
                    self.brackets == self.stackAnonClassBrackets[-1] and \
                    self.version not in scopeStateVersions:
                self._getAnonStack().pop()
                self.stackAnonClassBrackets.pop()

        def parseToken(self, token):
            if token[:2] == '//':
                return IgnoreState('\n', self)
            elif token == '/*':
                return IgnoreState('*/', self)
            elif token == '{':
                self.openBracket()
            elif token == '}':
                self.closeBracket()
            elif token in ['"', "'"]:
                return IgnoreState(token, self)
            elif token == "new":
                # anonymous inner class
                if len(self.listClasses) > 0:
                    return self.__getAnonClassState()
                return self.__getSkipState()  # Skip the class name
            elif token in ['class', 'interface', 'enum']:
                if len(self.listClasses) == 0:
                    self.nextAnon = 1
                self.stackBrackets.append(self.brackets)
                return self.__getClassState()
            elif token == 'package':
                return self.__getPackageState()
            elif token == '.':
                # Skip the attribute, it might be named "class", in which
                # case we don't want to treat the following token as
                # an inner class name...
                return self.__getSkipState()
            return self

        def addAnonClass(self):
            """Add an anonymous inner class"""
            if self.version in ('1.1', '1.2', '1.3', '1.4'):
                clazz = self.listClasses[0]
                self.listOutputs.append('%s$%d' % (clazz, self.nextAnon))
            elif self.version in ('1.5', '1.6', '1.7', '1.8', '5', '6', '9.0', '10.0', '11.0', '12.0'):
                self.stackAnonClassBrackets.append(self.brackets)
                className = []
                className.extend(self.listClasses)
                self._getAnonStack()[-1] = self._getAnonStack()[-1] + 1
                for anon in self._getAnonStack():
                    className.append(str(anon))
                self.listOutputs.append('$'.join(className))

            self.nextAnon = self.nextAnon + 1
            self._getAnonStack().append(0)

        def setPackage(self, package):
            self.package = package

    class ScopeState(object):
        """
        A state that parses code within a scope normally,
        within the confines of a scope.
        """

        def __init__(self, old_state):
            self.outer_state = old_state.outer_state
            self.old_state = old_state
            self.brackets = 0

        def __getClassState(self):
            try:
                return self.classState
            except AttributeError:
                ret = ClassState(self)
                self.classState = ret
                return ret

        def __getAnonClassState(self):
            try:
                return self.anonState
            except AttributeError:
                ret = SkipState(1, AnonClassState(self))
                self.anonState = ret
                return ret

        def __getSkipState(self):
            try:
                return self.skipState
            except AttributeError:
                ret = SkipState(1, self)
                self.skipState = ret
                return ret

        def openBracket(self):
            self.brackets = self.brackets + 1

        def closeBracket(self):
            self.brackets = self.brackets - 1

        def parseToken(self, token):
            # if self.brackets == 0:
            #     return self.old_state.parseToken(token)
            if token[:2] == '//':
                return IgnoreState('\n', self)
            elif token == '/*':
                return IgnoreState('*/', self)
            elif token == '{':
                self.openBracket()
            elif token == '}':
                self.closeBracket()
                if self.brackets == 0:
                    self.outer_state._getAnonStack().pop()
                    return self.old_state
            elif token in ['"', "'"]:
                return IgnoreState(token, self)
            elif token == "new":
                # anonymous inner class
                return self.__getAnonClassState()
            elif token == '.':
                # Skip the attribute, it might be named "class", in which
                # case we don't want to treat the following token as
                # an inner class name...
                return self.__getSkipState()
            return self

    class AnonClassState(object):
        """A state that looks for anonymous inner classes."""

        def __init__(self, old_state):
            # outer_state is always an instance of OuterState
            self.outer_state = old_state.outer_state
            self.old_state = old_state
            self.brace_level = 0

        def parseToken(self, token):
            # This is an anonymous class if and only if the next
            # non-whitespace token is a bracket. Everything between
            # braces should be parsed as normal java code.
            if token[:2] == '//':
                return IgnoreState('\n', self)
            elif token == '/*':
                return IgnoreState('*/', self)
            elif token == '\n':
                return self
            elif token[0] == '<' and token[-1] == '>':
                return self
            elif token == '(':
                self.brace_level = self.brace_level + 1
                return self
            if self.brace_level > 0:
                if token == 'new':
                    # look further for anonymous inner class
                    return SkipState(1, AnonClassState(self))
                elif token in ['"', "'"]:
                    return IgnoreState(token, self)
                elif token == ')':
                    self.brace_level = self.brace_level - 1
                return self
            if token == '{':
                self.outer_state.addAnonClass()
                if self.outer_state.version in scopeStateVersions:
                    return ScopeState(old_state=self.old_state).parseToken(token)
            return self.old_state.parseToken(token)

    class SkipState(object):
        """A state that will skip a specified number of tokens before
        reverting to the previous state."""

        def __init__(self, tokens_to_skip, old_state):
            self.tokens_to_skip = tokens_to_skip
            self.old_state = old_state

        def parseToken(self, token):
            self.tokens_to_skip = self.tokens_to_skip - 1
            if self.tokens_to_skip < 1:
                return self.old_state
            return self

    class ClassState(object):
        """A state we go into when we hit a class or interface keyword."""

        def __init__(self, outer_state):
            # outer_state is always an instance of OuterState
            self.outer_state = outer_state

        def parseToken(self, token):
            # the next non-whitespace token should be the name of the class
            if token == '\n':
                return self
            # If that's an inner class which is declared in a method, it
            # requires an index prepended to the class-name, e.g.
            # 'Foo$1Inner'
            # https://github.com/SCons/scons/issues/2087
            if self.outer_state.localClasses and \
                    self.outer_state.stackBrackets[-1] > \
                    self.outer_state.stackBrackets[-2] + 1:
                locals = self.outer_state.localClasses[-1]
                try:
                    idx = locals[token]
                    locals[token] = locals[token] + 1
                except KeyError:
                    locals[token] = 1
                token = str(locals[token]) + token
            self.outer_state.localClasses.append({})
            self.outer_state.listClasses.append(token)
            self.outer_state.anonStacksStack.append([0])
            return self.outer_state

    class IgnoreState(object):
        """A state that will ignore all tokens until it gets to a
        specified token."""

        def __init__(self, ignore_until, old_state):
            self.ignore_until = ignore_until
            self.old_state = old_state

        def parseToken(self, token):
            if self.ignore_until == token:
                return self.old_state
            return self

    class PackageState(object):
        """The state we enter when we encounter the package keyword.
        We assume the next token will be the package name."""

        def __init__(self, outer_state):
            # outer_state is always an instance of OuterState
            self.outer_state = outer_state

        def parseToken(self, token):
            self.outer_state.setPackage(token)
            return self.outer_state

    def parse_java_file(fn, version=default_java_version):
        with open(fn, 'r') as f:
            data = f.read()
        return parse_java(data, version)

    def parse_java(contents, version=default_java_version, trace=None):
        """Parse a .java file and return a double of package directory,
        plus a list of .class files that compiling that .java file will
        produce"""
        package = None
        initial = OuterState(version)
        currstate = initial
        for token in _reToken.findall(contents):
            # The regex produces a bunch of groups, but only one will
            # have anything in it.
            currstate = currstate.parseToken(token)
            if trace: trace(token, currstate)
        if initial.package:
            package = initial.package.replace('.', os.sep)
        return (package, initial.listOutputs)

else:
    # Don't actually parse Java files for class names.
    #
    # We might make this a configurable option in the future if
    # Java-file parsing takes too long (although it shouldn't relative
    # to how long the Java compiler itself seems to take...).

    def parse_java_file(fn):
        """ "Parse" a .java file.

        This actually just splits the file name, so the assumption here
        is that the file name matches the public class name, and that
        the path to the file is the same as the package name.
        """
        return os.path.split(fn)

def get_java_install_dirs(platform, version=None):
    """
    Find the java jdk installation directories.

    This list is intended to supply as "default paths" for use when looking
    up actual java binaries.

    :param platform: selector for search algorithm.
    :param version: If specified, only look for java sdk's of this version
    :return: list of default paths for java.
    """

    paths = []
    if platform == 'win32':
        if version:
            paths = glob.glob(java_win32_version_dir_glob % version)
        else:
            paths = glob.glob(java_win32_dir_glob)
    else:
        # other platforms, do nothing for now
        pass

    return sorted(paths)

def get_java_include_paths(env, javac, version):
    """
    Find java include paths for JNI building.

    :param env: construction environment, used to extract platform.
    :param javac: path to detected javac.
    :return: list of paths.
    """

    paths = []
    if not javac:
        # there are no paths if we've not detected javac.
        pass
    elif env['PLATFORM'] == 'win32':
        # on Windows, we have the right path to javac, so look locally
        javac_bin_dir = os.path.dirname(javac)
        java_inc_dir = os.path.normpath(os.path.join(javac_bin_dir, '..', 'include'))
        paths = [java_inc_dir, os.path.join(java_inc_dir, 'win32')]
    elif env['PLATFORM'] == 'darwin':
        if not version:
            paths = [java_macos_include_dir_glob]
        else:
            paths = sorted(glob.glob(java_macos_version_include_dir_glob % version))
    else:
        base_paths = []
        if not version:
            for p in java_linux_include_dirs_glob:
                base_paths.extend(glob.glob(p))
        else:
            for p in java_linux_version_include_dirs_glob:
                base_paths.extend(glob.glob(p % version))

        for p in base_paths:
            paths.extend([p, os.path.join(p, 'linux')])

    # print("PATHS:%s"%paths)
    return paths

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4:


import logging

import numpy as np
from sklearn.model_selection import train_test_split, StratifiedKFold

from utils import logging as lg
from heatmap_tutorial import utils as ht_utils

lg.set_logging()

def get_mnist(dataset, dir_path='./data/mnist'):

    if dataset == 'train':
        prefix = 'train'
    elif dataset == 'test':
        prefix = 't10k'
    else:
        raise ValueError('No dataset MNIST - %s' % dataset)

    logging.debug('Load %s : %s' % (dir_path, dataset))

    x_path = '%s/%s-images-idx3-ubyte' % (dir_path, prefix)
    y_path = '%s/%s-labels-idx1-ubyte' % (dir_path, prefix)

    with open(x_path) as xf:
        with open(y_path) as yf:
            x = 2.0*np.fromfile(xf, dtype='ubyte', count=-1)[16:].reshape((-1, 784)) / 255 - 1
            y = np.fromfile(yf, dtype='ubyte', count=-1)[8:]
            y = (y[:, np.newaxis] == np.arange(10)) * 1.0
    return x, y

def get_empty_data():
    return np.zeros((28, 28)) - 1

def fill_left_right_digit(x, y, seed=71):
    new_x = np.zeros((x.shape[0], 28, 28*3))

    new_x[:, :, 28:(28*2)] = x

    def plot_sample_propotions(indices, label):
        y_not_in_class_i = classes[indices]
        counts = dict()
        for jj in range(10):
            counts[jj] = np.sum(y_not_in_class_i == jj)

        logging.info('%s | sample propotions' % (label))
        logging.info(counts)

    np.random.seed(seed)
    classes = np.argmax(y, axis=1)

    # plot_sample_propotions(range(classes.shape[0]), 'total')

    for i in range(10):
        samples_in_class_i = np.squeeze(np.argwhere(classes == i))
        total = samples_in_class_i.shape[0]

        samples_not_in_class_i = np.squeeze(np.argwhere(classes != i))

        left_indices = np.random.choice(samples_not_in_class_i, total)
        # plot_sample_propotions(left_indices, 'left-%d' % i)

        right_indices = np.random.choice(samples_not_in_class_i, total)
        # plot_sample_propotions(right_indices, 'right-%d' % i)

        new_x[samples_in_class_i, :, :28] = x[left_indices, :, :]
        new_x[samples_in_class_i, :, -28:] = x[right_indices, :, :]

    return new_x, y

def expand_samples(x, y, n=7, seed=71):
    new_x = np.zeros((x.shape[0], x.shape[1], x.shape[2]*n))
    np.random.seed(seed)
    classes = np.argmax(y, axis=1)

    original_sample_idx = np.floor(n / 2).astype(int)

    new_x[:, :, x.shape[2]*original_sample_idx:x.shape[2]*(original_sample_idx+1)] = x

    for i in range(y.shape[1]):
        samples_in_class_i = np.squeeze(np.argwhere(classes == i))
        total = samples_in_class_i.shape[0]

        samples_not_in_class_i = np.squeeze(np.argwhere(classes != i))

        for j in range(n):
            if j == original_sample_idx:
                continue
            indices = np.random.choice(samples_not_in_class_i, total)
            new_x[samples_in_class_i, :, j*x.shape[2]:(j+1)*x.shape[2]] = x[indices, :, :]

    return new_x, y

def create_majority_data(x, y, seed=71):
    np.random.seed(seed)
    classes = np.argmax(y, axis=1)

    new_x = np.tile(x, (1, 3))

    digit_positions = np.zeros((new_x.shape[0], 3))

    for i in range(10):
        samples_in_class_i = np.squeeze(np.argwhere(classes == i))
        total = samples_in_class_i.shape[0]

        samples_not_in_class_i = np.squeeze(np.argwhere(classes != i))

        fake_digit_idx = np.random.choice(samples_not_in_class_i, total)
        same_class_digit_idx = np.random.choice(samples_in_class_i, total)

        for j, idx in zip(range(total), samples_in_class_i):
            dd = [x[idx, :, :], x[same_class_digit_idx[j], :, :], x[fake_digit_idx[j], :, :]]
            permuted_pos = np.random.permutation(range(3))

            digit_positions[idx] = permuted_pos
            dd_permuted = [dd[jj] for jj in permuted_pos]

            new_x[idx, :, :] = np.concatenate(dd_permuted, axis=1)

    return new_x, y, digit_positions <= 1

def create_middle_mark(no_x, no_digit=3):
    zeros = np.zeros((no_x, no_digit))
    zeros[:, int(np.floor(no_digit/2))] = 1
    return zeros

def build_cvdataset(data, k=10):

    xar = []
    yar = []
    mar = []

    total_data = 0
    for d in [data.train2d, data.test2d, data.val2d]:
        xar.append(d.x)
        yar.append(d.y)
        mar.append(d.correct_digit_mark)

        total_data += d.y.shape[0]

    datasets = []

    x = np.vstack(xar)
    logging.info('total x shape')
    logging.info(x.shape)

    y = np.vstack(yar)
    logging.info('total y shape')
    logging.info(y.shape)

    mark = np.vstack(mar)
    logging.info('total mark shape')
    logging.info(mark.shape)

    skf = StratifiedKFold(n_splits=k, random_state=71, shuffle=True)
    for train_indices, test_indices in skf.split(x, np.argmax(y, axis=1)):
        dtrain = DataSet(x=x[train_indices, ...], y=y[train_indices, ...], correct_digit_mark=mark[train_indices, ...])
        dtest = DataSet(x=x[test_indices, ...], y=y[test_indices, ...], correct_digit_mark=mark[test_indices, ...])
        datasets.append((dtrain, dtest, dtest))

    return datasets

class DatasetLoader():
    def __init__(self, data_dir):
        self.prepend_dir = lambda p: '%s/%s' % (data_dir, p)
        self.cache = dict()

    def load(self, dataset_name):

        if self.cache.get(dataset_name):
            return self.cache[dataset_name]

        if dataset_name == 'mnist':
            data = MNISTData(dir_path=self.prepend_dir('mnist'))
        elif dataset_name == 'fashion-mnist':
            data = FashionMNISTData(dir_path=self.prepend_dir('fashion-mnist'))
        elif dataset_name == 'ufi-cropped':
            data = UFICroppedData(dir_path=self.prepend_dir('ufi-cropped'))
        elif dataset_name == 'mnist-3-digits':
            data = MNIST3DigitsData(dir_path=self.prepend_dir('mnist'))
        elif dataset_name == 'mnist-3-digits-maj':
            data = MNIST3DigitsWithMajorityData(dir_path=self.prepend_dir('mnist'))
        elif dataset_name == 'fashion-mnist-3-items':
            data = FashionMNIST3ItemsData(dir_path=self.prepend_dir('fashion-mnist'))
        elif dataset_name == 'fashion-mnist-3-items-maj':
            data = FashionMNIST3DigitsWithMajorityData(dir_path=self.prepend_dir('fashion-mnist'))
        elif dataset_name == 'mnist-7-digits':
            data = MNISTMiddleSampleProblem(n=7, seed=5, dir_path=self.prepend_dir('mnist'))
        elif dataset_name == 'fashion-mnist-7-items':
            data = MNISTMiddleSampleProblem(n=7, seed=15, dir_path=self.prepend_dir('fashion-mnist'))
        else:
            raise SystemError('No dataset name `%s`' % dataset_name)

        self.cache[dataset_name] = data

        return self.cache[dataset_name]

class DataSet:
    def __init__(self, x, y, correct_digit_mark=None):
        self.x = x
        self.y = y
        self.correct_digit_mark = correct_digit_mark

    def get_batch(self, no_batch, seed=71):
        total = len(self.x)

        np.random.seed(seed)
        shuffled_indices = np.random.permutation(total)

        x = self.x[shuffled_indices, :, :]
        y = self.y[shuffled_indices, :]

        for ndx in range(0, total, no_batch):
            yield (x[ndx:min(ndx + no_batch, total)], y[ndx:min(ndx + no_batch, total)])

class MNISTData:
    def __init__(self, dir_path='./data/mnist'):

        self.dir_path = dir_path

        x_train, y_train = get_mnist('train', dir_path=dir_path)
        x_test, y_test = get_mnist('test', dir_path=dir_path)

        x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.2, random_state=71)

        self.no_classes = 10
        self.dims = (28, 28)

        self.train = DataSet(x_train, y_train)
        self.val = DataSet(x_val, y_val)
        self.test = DataSet(x_test, y_test)

        self.train2d = DataSet(x_train.reshape(-1, 28, 28), y_train)
        self.val2d = DataSet(x_val.reshape(-1, 28, 28), y_val)
        self.test2d = DataSet(x_test.reshape(-1, 28, 28), y_test)

        self.labels = {
            0: 'Digit 0',
            1: 'Digit 1',
            2: 'Digit 2',
            3: 'Digit 3',
            4: 'Digit 4',
            5: 'Digit 5',
            6: 'Digit 6',
            7: 'Digit 7',
            8: 'Digit 8',
            9: 'Digit 9'
        }

    def get_text_label(self, label_index):
        return 'Digit %d' % label_index

    def get_samples_for_vis(self, n=12):

        np.random.seed(1234)

        r = np.random.randint(0,  self.test2d.y.shape[0], n)

        return self.test2d.x[r, :, :], self.test2d.y[r]

class MNIST3DigitsData(MNISTData):
    def __init__(self, **kwargs):
        super(MNIST3DigitsData, self).__init__(**kwargs)

        self.dims = (28, 28*3)

        self.train2d = DataSet(*fill_left_right_digit(self.train2d.x, self.train2d.y, seed=0),
                               correct_digit_mark=create_middle_mark(self.train2d.x.shape[0]))

        self.val2d = DataSet(*fill_left_right_digit(self.val2d.x, self.val2d.y, seed=1),
                             correct_digit_mark=create_middle_mark(self.val2d.x.shape[0])
                             )

        self.test2d = DataSet(*fill_left_right_digit(self.test2d.x, self.test2d.y, seed=3),
                              correct_digit_mark=create_middle_mark(self.test2d.x.shape[0])
                              )

        self.train = self.train2d
        self.val = self.val2d
        self.test = self.test2d

class MNIST3DigitsWithMajorityData(MNISTData):
    def __init__(self, **kwargs):
        super(MNIST3DigitsWithMajorityData, self).__init__(**kwargs)

        self.dims = (28, 28*3)

        x, y, train2d_correct_digit_mark = create_majority_data(self.train2d.x, self.train2d.y, seed=0)
        self.train2d = DataSet(x, y, correct_digit_mark=train2d_correct_digit_mark)
        assert self.train2d.correct_digit_mark.shape[0] == self.train2d.y.shape[0]

        x, y, val2d_correct_digit_mark = create_majority_data(self.val2d.x, self.val2d.y, seed=1)
        self.val2d = DataSet(x, y, correct_digit_mark=val2d_correct_digit_mark)
        assert self.val2d.correct_digit_mark.shape[0] == self.val2d.y.shape[0]

        x, y, test2d_correct_digit_mark = create_majority_data(self.test2d.x, self.test2d.y, seed=3)
        self.test2d = DataSet(x, y, correct_digit_mark=test2d_correct_digit_mark)
        assert self.test2d.correct_digit_mark.shape[0] == self.test2d.y.shape[0]

        self.train = self.train2d
        self.val = self.val2d
        self.test = self.test2d

class MNISTMiddleSampleProblem(MNISTData):
    def __init__(self, n=7, seed=1, **kwargs):
        super(MNISTMiddleSampleProblem, self).__init__(**kwargs)
        self.dims = (28, 28*n)

        self.train2d = DataSet(*expand_samples(self.train2d.x, self.train2d.y, n, seed=seed))
        self.train2d_correct_digit_mark = create_middle_mark(self.train2d.x.shape[0], no_digit=n)

        self.val2d = DataSet(*expand_samples(self.val2d.x, self.val2d.y, n, seed=seed+1))
        self.val2d_correct_digit_mark = create_middle_mark(self.val2d.x.shape[0], no_digit=n)

        self.test2d = DataSet(*expand_samples(self.test2d.x, self.test2d.y, n, seed=seed+2))
        self.test2d_correct_digit_mark = create_middle_mark(self.test2d.x.shape[0], no_digit=n)

        self.train = self.train2d
        self.val = self.val2d
        self.test = self.test2d

        labels = {
            0: 'Digit 0',
            1: 'Digit 1',
            2: 'Digit 2',
            3: 'Digit 3',
            4: 'Digit 4',
            5: 'Digit 5',
            6: 'Digit 6',
            7: 'Digit 7',
            8: 'Digit 8',
            9: 'Digit 9'
        }

        if 'fashion' in kwargs['dir_path']:
            labels = {
                0: 'T-shirt/top',
                1: 'Trouser',
                2: 'Pullover',
                3: 'Dress',
                4: 'Coat',
                5: 'Sandal',
                6: 'Shirt',
                7: 'Sneaker',
                8: 'Bag',
                9: 'Ankle boot'
            }

        self.labels = labels

class FashionMNISTData:
    def __init__(self, dir_path='./data/fashion-mnist'):

        x_train, y_train = get_mnist('train', dir_path=dir_path)
        x_test, y_test = get_mnist('test', dir_path=dir_path)

        x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.2, random_state=71)

        self.dims = (28, 28)
        self.no_classes = 10

        self.train = DataSet(x_train, y_train)
        self.val = DataSet(x_val, y_val)
        self.test = DataSet(x_test, y_test)

        self.train2d = DataSet(x_train.reshape(-1, 28, 28), y_train)
        self.val2d = DataSet(x_val.reshape(-1, 28, 28), y_val)
        self.test2d = DataSet(x_test.reshape(-1, 28, 28), y_test)

        self.labels = {
            0: 'T-shirt/top',
            1: 'Trouser',
            2: 'Pullover',
            3: 'Dress',
            4: 'Coat',
            5: 'Sandal',
            6: 'Shirt',
            7: 'Sneaker',
            8: 'Bag',
            9: 'Ankle boot'
        }

    def get_samples_for_vis(self, n=12):

        indices = [588, 314, 47, 145, 258, 641, 561, 3410, 1094, 4059, 518, 9304][:n]

        return self.test2d.x[indices, :], self.test2d.y[indices]

    def get_text_label(self, label_index):
        return self.labels[label_index]

class FashionMNIST3ItemsData(FashionMNISTData):
    def __init__(self, **kwargs):
        super(FashionMNIST3ItemsData, self).__init__(**kwargs)

        self.dims = (28, 28*3)

        self.train2d = DataSet(*fill_left_right_digit(self.train2d.x, self.train2d.y, seed=20),
                               correct_digit_mark=create_middle_mark(self.train2d.x.shape[0]))

        self.val2d = DataSet(*fill_left_right_digit(self.val2d.x, self.val2d.y, seed=21),
                             correct_digit_mark=create_middle_mark(self.val2d.x.shape[0])
                             )

        self.test2d = DataSet(*fill_left_right_digit(self.test2d.x, self.test2d.y, seed=23),
                              correct_digit_mark=create_middle_mark(self.test2d.x.shape[0])
                              )

        self.train = self.train2d
        self.val = self.val2d
        self.test = self.test2d

class FashionMNIST3DigitsWithMajorityData(FashionMNISTData):
    def __init__(self, **kwargs):
        super(FashionMNIST3DigitsWithMajorityData, self).__init__(**kwargs)

        self.dims = (28, 28*3)

        x, y, train2d_correct_digit_mark = create_majority_data(self.train2d.x, self.train2d.y, seed=0)
        self.train2d = DataSet(x, y, correct_digit_mark=train2d_correct_digit_mark)
        assert self.train2d.correct_digit_mark.shape[0] == self.train2d.y.shape[0]

        x, y, val2d_correct_digit_mark = create_majority_data(self.val2d.x, self.val2d.y, seed=1)
        self.val2d = DataSet(x, y, correct_digit_mark=val2d_correct_digit_mark)
        assert self.val2d.correct_digit_mark.shape[0] == self.val2d.y.shape[0]

        x, y, test2d_correct_digit_mark = create_majority_data(self.test2d.x, self.test2d.y, seed=3)
        self.test2d = DataSet(x, y, correct_digit_mark=test2d_correct_digit_mark)
        assert self.test2d.correct_digit_mark.shape[0] == self.test2d.y.shape[0]

        self.train = self.train2d
        self.val = self.val2d
        self.test = self.test2d

class UFICroppedData:
    def __init__(self, dir_path='./data/ufi-cropped'):
        # subsampling_indices = list(np.arange(0, 128, 2))

        def avg_pooling(x):
            new_x = np.zeros((x.shape[0], 64, 64))

            for i in range(0, x.shape[1], 2):
                for j in range(0, x.shape[2], 2):
                    new_x[:, int(i/2), int(j/2)] = np.mean(x[:, i:(i+2), j:(j+2)].reshape(-1, 4), axis=1)

            return new_x

        def flip_data(x, y):
            total = x.shape[0]
            new_x = np.tile(x, (2, 1, 1))
            new_y = np.tile(y, (2, 1))

            new_x[total:, :, :] = x[:, :, ::-1]

            np.random.seed(0)

            shuffled_indices = np.arange(total*2)
            np.random.shuffle(shuffled_indices)

            return new_x[shuffled_indices, :, :], new_y[shuffled_indices, :]

        x_train = avg_pooling(np.load('%s/train-x.npy' % dir_path).reshape(-1, 128, 128))
        y_train = np.load('%s/train-y.npy' % dir_path)

        # print(x_train[0])
        # print(np.argmax(y_train[0]))

        x_train, y_train = flip_data(x_train, y_train)
        # print('Train data', x_train.shape)

        x_test = avg_pooling(np.load('%s/test-x.npy' % dir_path).reshape(-1, 128, 128))
        y_test = np.load('%s/test-y.npy' % dir_path)

        x_test, y_test = flip_data(x_test, y_test)
        # print('Test data', x_test.shape)

        self.dims = (64, 64)

        # This is a bad idea but we have limited amount of data
        x_val, y_val = x_test, y_test

        self.no_classes = y_test.shape[1]

        self.train = DataSet(x_train, y_train)
        self.val = DataSet(x_val, y_val)
        self.test = DataSet(x_test, y_test)

        self.train2d = DataSet(x_train, y_train)
        self.val2d = DataSet(x_val, y_val)
        self.test2d = DataSet(x_test, y_test)

    def get_samples_for_vis(self, n=12):

        print("WARNING! this is data sampled from training set not testing one")
        indices = [2785, 2973, 57, 906, 393, 3666, 3502, 1222, 731, 2659, 3400, 656]

        return self.train2d.x[indices, :], self.train2d.y[indices]

    def get_text_label(self, label_index):
        return 'Person %d' % label_index


# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import sys
import asyncio
import datetime
import time
import collections
import json

# function to execute aws cli 'iam' command
async def getAwsIamData(require_data:str) -> list:
    command = "aws iam "
    command += require_data
    process = await asyncio.create_subprocess_shell(
        command,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE)
    return await process.communicate()

# function to get user list
async def getAwsUserList() -> str:
    output, errs = await getAwsIamData("list-users")
    if errs:
        print("Error occurred to get user list")
        return "error"

    return output.decode()

# function to get access key list
async def getAwsAccessKeys(userName) -> str:
    output, errs = await getAwsIamData("list-access-keys --user-name " + userName)
    if errs:
        print("Error occurred to get access key list:", errs)
        return "error"

    return output.decode()

# get access key list per user and arranging them in the dictionary
async def arrangeUserAccessKeyData(user_map, userName:str, current_time:float, valid_time:int):
    # request access key list assigned to {userName}
    keys = json.loads(await getAwsAccessKeys(userName))

    # iterate access keys and check if it is valid.
    for key in keys["AccessKeyMetadata"]:
        if await checkIsValid(key["CreateDate"], current_time, valid_time):
            user_map[key["UserName"]]["UserName"] = key["UserName"]
            user_map[key["UserName"]]["AccessKeyId"] = key["AccessKeyId"]
            user_map[key["UserName"]]["CreateDate"] = key["CreateDate"]

# check if create_time + valid_time is older than current_time
async def checkIsValid(created_time:str, current_time:float, valid_time:int) -> bool:
    date = datetime.datetime.strptime(created_time, '%Y-%m-%dT%H:%M:%S+00:00')
    created_at = time.mktime(date.timetuple())

    # print("diff: ", (current_time - (created_at + valid_time)) // 3600)
    if created_at + valid_time < current_time: # user is not valid
        return True
    else: # user is still valid
        return False

async def detectInvalidUserkey(valid_time:int):

    users = json.loads(await getAwsUserList())
    if users == "error":
        print("Error occurred to get User data")
        return

    # dictionary to save user/key data
    user_data = collections.defaultdict(dict)
    # current time to be base
    current_time = time.time()
    valid_time *= 3600

    tasks = []
    # get access keys per user and arrange them in 'user_data' dictionary
    for user in users["Users"]:
        tasks.append(arrangeUserAccessKeyData(user_data, user["UserName"], current_time, valid_time))
    # running the tasks asynchronously
    await asyncio.gather(*tasks)

    # write result(user_data) on the file
    with open("invalid_access_keys.txt", "w") as fd:
        fd.write(" - Get Invalid User List - At: " + datetime.datetime.fromtimestamp(current_time).strftime("%m/%d/%Y, %H:%M:%S") + "\n\n")
        for key, val in user_data.items():
            fd.write(" " + "{\n")
            for nkey, nval in val.items():
                fd.write(" \t" + nkey + ": " + nval + "\n")
            fd.write(" }\n")

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    limit = int(sys.argv[1])
    if not limit:
        print("please put validation time to filter access keys.")
    else:
        asyncio.run(detectInvalidUserkey(limit))


import csv
import os
from flask import Blueprint, redirect, render_template, request
import sqlalchemy
from forms.addTeacherForm import AddTeacherForm
from models import GroupOfTeacher, Teacher, TeacherInGroup, db
from forms.editForm import EditTeacherForm

ROWS_PER_PAGE = 5

teachers_blueprint = Blueprint("teachers_blueprint", __name__)

@teachers_blueprint.route("/teachers", methods=["get", "post"])
def teachers():
    page = request.args.get('page', 1, type=int)

    teachers = Teacher.query.order_by(Teacher.last_name).paginate(page=page, per_page=ROWS_PER_PAGE)
    addTeacherFormData = AddTeacherForm()

    return render_template("teachers/teachers.html",
                           teachers=teachers,
                           form=addTeacherFormData)

add_teachers_blueprint = Blueprint("add_teachers_blueprint", __name__)

@add_teachers_blueprint.route("/teachers/add", methods=["get", "post"])
def add_teachers():
    addTeacherFormData = AddTeacherForm()
    # teachers = db.session.query(Teacher).all()
    if True:  # request.method == "POST":
        if addTeacherFormData.validate_on_submit():
            teacherData = Teacher()
            teacherData.first_name = addTeacherFormData.first_name.data
            teacherData.last_name = addTeacherFormData.last_name.data
            teacherData.birthdate = addTeacherFormData.birthdate.data
            # teacherData.teacher_in_group_Id = addTeacherFormData.teacher_in_group_Id.data

            db.session.add(teacherData)
            db.session.commit()

            return redirect("/teachers")

        else:
            return render_template("teachers/addTeacherForm.html",
                                   form=addTeacherFormData)

show_edit_teachers_blueprint = Blueprint(
    "show_edit_teachers_blueprint", __name__)

@show_edit_teachers_blueprint.route("/teachers/edit")
def show_edit_teachers():
    editTeacherFormData = EditTeacherForm()
    global current_data
    current_data = editTeacherFormData
    # itemId auslesen
    teacher_Id = request.args["teacher_Id"]
    # Item laden
    teacher_to_edit = db.session.query(Teacher).filter(
        Teacher.teacher_Id == teacher_Id).first()
    # Form befüllen
    editTeacherFormData.teacher_Id.data = teacher_Id
    editTeacherFormData.first_name.data = teacher_to_edit.first_name
    editTeacherFormData.last_name.data = teacher_to_edit.last_name
    editTeacherFormData.birthdate.data = teacher_to_edit.birthdate
    # editTeacherFormData.teacher_in_group_Id.data = teacher_to_edit.teacher_in_group_Id

    group_of_teachers = db.session.query(GroupOfTeacher).filter(
        sqlalchemy.and_(TeacherInGroup.group_of_teachers_Id == GroupOfTeacher.group_of_teachers_Id,
                        TeacherInGroup.teacher_Id == teacher_Id))

    return render_template("/teachers/editTeacherForm.html", form=editTeacherFormData,
                           group_of_teachers=group_of_teachers)

submit_edit_teachers_blueprint = Blueprint(
    "submit_edit_teachers_blueprint", __name__)

@submit_edit_teachers_blueprint.route("/teachers/edit", methods=["post"])
def submit_edit_teachers():
    editTeacherFormData = EditTeacherForm()
    create_report_file(editTeacherFormData)
    if editTeacherFormData.validate_on_submit():
        # daten aus Form auslesen
        teacher_Id = editTeacherFormData.teacher_Id.data
        teacher_to_edit = db.session.query(Teacher).filter(
            Teacher.teacher_Id == teacher_Id).first()
        # daten mit update in DB speichern
        teacher_to_edit.first_name = editTeacherFormData.first_name.data
        teacher_to_edit.last_name = editTeacherFormData.last_name.data
        teacher_to_edit.birthdate = editTeacherFormData.birthdate.data
        # teacher_to_edit.teacher_in_group_Id = editTeacherFormData.teacher_in_group_Id.data

        db.session.commit()

        return redirect("/teachers")

    else:
        raise("Fatal Error")

def create_report_file(teacher_data):
    header = ["Data", "Previous Data", "New Data"]
    teacher_id = ["Teacher ID", current_data.teacher_Id.data,
                  teacher_data.teacher_Id.data]
    first_name = ["First Name", current_data.first_name.data,
                  teacher_data.first_name.data]
    last_name = ["Last Name", current_data.last_name.data,
                 teacher_data.last_name.data]
    birthdate = ["Birthdate", current_data.birthdate.data,
                 teacher_data.birthdate.data]
    i = 0
    while os.path.exists("TeacherDataEdit%s.csv" % i):
        i += 1
    f = open(f"TeacherDataEdit{i}.csv", "w")

    writer = csv.writer(f)

    writer.writerow(header)
    writer.writerow(teacher_id)
    writer.writerow(first_name)
    writer.writerow(last_name)
    writer.writerow(birthdate)