from github import Github
from themes.level_1.level1 import genHTMLLevel1
from themes.level_2.level2 import genHTMLLevel2
from functions.RepoToHTML import repotohtml
from functions.addSocialLinks import strtoLinksHTML
from distutils.util import strtobool
import sys
import os

git = Github(sys.argv[1])
theme_selected = sys.argv[2]
blogs = strtobool(sys.argv[3])
include_hackathon = strtobool(sys.argv[4])
stats_choice = sys.argv[5]
social_links = sys.argv[6]
resume_link = sys.argv[7]
allow_footer = bool(sys.argv[8])

start = git.rate_limiting[0]
print(f'Request left at start of the script: {start}')

user_object = git.get_user()
git_username = user_object.login
user_data = {'username': git_username,
             'git_photo_url': user_object.avatar_url,
             'git_bio': user_object.bio,
             'git_email': user_object.email,
             'git_followers': user_object.followers,
             'git_following': user_object.following,
             'name': user_object.name,
             'latest_updated': str(user_object.updated_at)}

repo_list = [repo.name for repo in git.get_user().get_repos()]
project_data = {}
hackathon_data = {}

if include_hackathon:
    for repo in repo_list:
        print(f'Repo being checked: {repo}')
        try:
            repo_object = git.get_repo(git_username + '/' + repo)
            repo_topics = repo_object.get_topics()
            if len(repo_topics) != 0:
                if 'project' in repo_topics:
                    project_data[f'{repo}'] = {'repo_topics': repo_topics,
                                               'repo_description': repo_object.description,
                                               'repo_stars': int(repo_object.stargazers_count),
                                               'repo_forks': int(repo_object.forks_count)}

                if 'hackathon' in repo_topics:
                    hackathon_data[f'{repo}'] = {'repo_topics': repo_topics,
                                                 'repo_description': repo_object.description,
                                                 'repo_stars': int(repo_object.stargazers_count),
                                                 'repo_forks': int(repo_object.forks_count)}

                else:
                    continue
            else:
                continue
        except:
            continue
else:
    for repo in repo_list:
        print(f'Repo being checked: {repo}')
        try:
            repo_object = git.get_repo(git_username + '/' + repo)
            repo_topics = repo_object.get_topics()
            if len(repo_topics) != 0:
                if 'project' in repo_topics:
                    project_data[f'{repo}'] = {'repo_topics': repo_topics,
                                               'repo_description': repo_object.description,
                                               'repo_stars': int(repo_object.stargazers_count),
                                               'repo_forks': int(repo_object.forks_count)}
                else:
                    continue
            else:
                continue
        except:
            continue

end = git.rate_limiting[0]
print(f'Request left at end of the script: {end}')
print(f'Requests Consumed in this process: {start - end}')

project_repos = repotohtml(project_data, git_username)
if include_hackathon:
    hackathon_repos = repotohtml(hackathon_data, git_username)
else:
    hackathon_repos = None

social_data = strtoLinksHTML(social_links, user_data['username'])

if theme_selected == '1':
    indexFile = genHTMLLevel1(
        user_data, project_repos, hackathon_repos, blogs, stats_choice, social_data, resume_link, allow_footer)

elif theme_selected == '2':
    indexFile = genHTMLLevel2(user_data, project_repos,
                              hackathon_repos, blogs, stats_choice, social_data, resume_link, allow_footer)

with open('index.html', 'w', encoding='utf-8') as f:
    f.write(indexFile)


from Sample_Tests.quoraAutomation.Page_Objects.BaseClass import BasePage
from appium.webdriver.common.mobileby import By
import time
class Homepage(BasePage):

    homeButton=(By.XPATH,'//android.widget.RelativeLayout//android.widget.ImageView[@content-desc="Me"]')
    moreOptions=(By.XPATH,"//android.widget.Button[@text='More options']")
    logout_Option=(By.XPATH,"//android.widget.TextView[@text='Logout']")
    confirm_Logout=(By.XPATH,"//android.widget.Button[@text='Logout']")

    def navigate_To_Logout(self, ):
        time.sleep(10)
        self.driver.find_element(*Homepage.homeButton).click()
        self.driver.find_element(*Homepage.moreOptions).click()
        self.driver.find_element(*Homepage.logout_Option).click()

        return self.driver.find_element(*Homepage.confirm_Logout)


from builtins import str
from builtins import object
import pytest
from chispa.dataframe_comparer import assert_df_equality
from pyspark.sql import types as sql_types
from pyspark.sql import functions as F
from pyspark.sql import Row
import datetime as dt

from spooq2.transformer import ThresholdCleaner

class TestBasicAttributes(object):
    """ Basic attributes of the transformer instance """

    def test_has_logger(self):
        assert hasattr(ThresholdCleaner(), "logger")

    def test_has_name(self):
        assert ThresholdCleaner().name == "ThresholdCleaner"

    def test_has_str_representation(self):
        assert str(ThresholdCleaner()) == "Transformer Object of Class ThresholdCleaner"

class TestCleaning(object):

    # fmt: off
    @pytest.fixture(scope="module")
    def input_df(self, spark_session):
        input_data = [
            #ids  #floats   #integers  #strings         #timestamps           #datetimes
            [0,       12.0,      12,      "12",  dt.datetime(1850,1,1, 12,0,0), dt.date(1850,1,1)],
            [1,       65.7,      65,      "65",  dt.datetime(2020,6,1, 12,0,0), dt.date(2020,6,1)],
            [2,      300.0,     300,     "300",  dt.datetime(2020,6,1, 15,0,0), dt.date(2020,6,15)],
            [4,     5000.0,    5000,    "5000",  dt.datetime(2020,6,1, 16,0,0), dt.date(2020,7,1)],
            [5,      -75.0,     -75,     "-75",  dt.datetime(9999,1,1, 12,0,0), dt.date(9999,1,1)],
        ]
        schema = sql_types.StructType(
            [
                sql_types.StructField("id",         sql_types.IntegerType(),   True),
                sql_types.StructField("floats",     sql_types.DoubleType(),    False),
                sql_types.StructField("integers",   sql_types.LongType(),      False),
                sql_types.StructField("strings",    sql_types.StringType(),    False),
                sql_types.StructField("timestamps", sql_types.TimestampType(), False),
                sql_types.StructField("datetimes",  sql_types.DateType(),      False),
            ]
        )
        return spark_session.createDataFrame(input_data, schema=schema)

    @pytest.fixture(scope="module")
    def thresholds(self):
        return {
            "integers":   {"min":  1,                           "max":  300},
            "floats":     {"min":  1.0,                         "max":  300.0},
            "strings":    {"min": "1",                          "max": "300"},
            "timestamps": {"min": dt.datetime(2020,6,1,12,0,0), "max": dt.datetime(2020,6,1,16,0,0)},
            "datetimes":  {"min": dt.date(2020,6,1),            "max": "2020-7-1"},
        }

    @pytest.fixture(scope="module")
    def expected_result(self):
        return {
            "integers":   [ 12,    65,    300,   None, None],
            "floats":     [ 12.0,  65.7,  300.0, None, None],
            "strings":    ["12",  "65",  "300",  None, None],
            "timestamps": [None, dt.datetime(2020,6,1, 12,0,0), dt.datetime(2020,6,1, 15,0,0),
                           dt.datetime(2020,6,1, 16,0,0), None],
            "datetimes":  [None, dt.date(2020,6,1), dt.date(2020,6,15), dt.date(2020,7,1), None],
        }
    # fmt: on

    @pytest.mark.parametrize("column_name", ["integers", "floats", "timestamps", "datetimes"])
    def test_clean_supported_format(self, column_name, input_df, thresholds, expected_result):
        thresholds_to_test = {column_name: thresholds[column_name]}
        transformer = ThresholdCleaner(thresholds=thresholds_to_test)
        df_cleaned = transformer.transform(input_df)
        result = [x[column_name] for x in df_cleaned.collect()]
        expected = expected_result[column_name]

        assert result == expected
        assert input_df.columns == df_cleaned.columns

    @pytest.mark.parametrize("column_name", ["strings"])
    def test_raise_exception_for_unsupported_format(self, column_name, input_df, thresholds):
        thresholds_to_test = dict([k_v1 for k_v1 in list(thresholds.items()) if k_v1[0] == column_name])
        transformer = ThresholdCleaner(thresholds_to_test)

        with pytest.raises(ValueError):
            transformer.transform(input_df).count()

    def test_dynamic_default_value(self, spark_session):
        input_df = spark_session.createDataFrame([
            Row(id=1, num=1),
            Row(id=2, num=2),
            Row(id=3, num=100),
            Row(id=4, num=4),
            Row(id=5, num=-1024),
        ])
        thresholds_to_test = dict(num=dict(min=0, max=99, default=F.col("id") * -1))
        output_df = ThresholdCleaner(thresholds_to_test).transform(input_df)
        expected_output_df = spark_session.createDataFrame([
            Row(id=1, num=1),
            Row(id=2, num=2),
            Row(id=3, num=-3),
            Row(id=4, num=4),
            Row(id=5, num=-5),
        ])
        assert_df_equality(expected_output_df, output_df)


from collections import defaultdict
from pathlib import Path

import numpy as np
import torch.utils.data as torch_data

from ..utils import common_utils
from .augmentor.data_augmentor import DataAugmentor
from .processor.data_processor import DataProcessor
from .processor.point_feature_encoder import PointFeatureEncoder

class DatasetTemplate(torch_data.Dataset):
    def __init__(self, dataset_cfg=None, class_names=None, training=True, root_path=None, logger=None):
        super().__init__()
        self.dataset_cfg = dataset_cfg
        self.training = training
        self.class_names = class_names
        self.logger = logger
        self.root_path = root_path if root_path is not None else Path(self.dataset_cfg.DATA_PATH)
        self.logger = logger
        if self.dataset_cfg is None or class_names is None:
            return

        self.point_cloud_range = np.array(self.dataset_cfg.POINT_CLOUD_RANGE, dtype=np.float32)
        self.point_feature_encoder = PointFeatureEncoder(
            self.dataset_cfg.POINT_FEATURE_ENCODING,
            point_cloud_range=self.point_cloud_range
        )
        self.data_augmentor = DataAugmentor(
            self.root_path, self.dataset_cfg.DATA_AUGMENTOR, self.class_names, logger=self.logger
        ) if self.training else None
        self.data_processor = DataProcessor(
            self.dataset_cfg.DATA_PROCESSOR, point_cloud_range=self.point_cloud_range,
            training=self.training, num_point_features=self.point_feature_encoder.num_point_features
        )

        self.grid_size = self.data_processor.grid_size
        self.voxel_size = self.data_processor.voxel_size
        self.total_epochs = 0
        self._merge_all_iters_to_one_epoch = False

        if hasattr(self.data_processor, "depth_downsample_factor"):
            self.depth_downsample_factor = self.data_processor.depth_downsample_factor
        else:
            self.depth_downsample_factor = None

    @property
    def mode(self):
        return 'train' if self.training else 'test'

    def __getstate__(self):
        d = dict(self.__dict__)
        del d['logger']
        return d

    def __setstate__(self, d):
        self.__dict__.update(d)

    @staticmethod
    def generate_prediction_dicts(batch_dict, pred_dicts, class_names, output_path=None):
        """
        To support a custom dataset, implement this function to receive the predicted results from the model, and then
        transform the unified normative coordinate to your required coordinate, and optionally save them to disk.

        Args:
            batch_dict: dict of original data from the dataloader
            pred_dicts: dict of predicted results from the model
                pred_boxes: (N, 7), Tensor
                pred_scores: (N), Tensor
                pred_labels: (N), Tensor
            class_names:
            output_path: if it is not None, save the results to this path
        Returns:

        """

    def merge_all_iters_to_one_epoch(self, merge=True, epochs=None):
        if merge:
            self._merge_all_iters_to_one_epoch = True
            self.total_epochs = epochs
        else:
            self._merge_all_iters_to_one_epoch = False

    def __len__(self):
        raise NotImplementedError

    def __getitem__(self, index):
        """
        To support a custom dataset, implement this function to load the raw data (and labels), then transform them to
        the unified normative coordinate and call the function self.prepare_data() to process the data and send them
        to the model.

        Args:
            index:

        Returns:

        """
        raise NotImplementedError

    def prepare_data(self, data_dict):
        """
        Args:
            data_dict:
                points: optional, (N, 3 + C_in)
                gt_boxes: optional, (N, 7 + C) [x, y, z, dx, dy, dz, heading, ...]
                gt_names: optional, (N), string
                ...

        Returns:
            data_dict:
                frame_id: string
                points: (N, 3 + C_in)
                gt_boxes: optional, (N, 7 + C) [x, y, z, dx, dy, dz, heading, ...]
                gt_names: optional, (N), string
                use_lead_xyz: bool
                voxels: optional (num_voxels, max_points_per_voxel, 3 + C)
                voxel_coords: optional (num_voxels, 3)
                voxel_num_points: optional (num_voxels)
                ...
        """
        if self.training:
            assert 'gt_boxes' in data_dict, 'gt_boxes should be provided for training'
            gt_boxes_mask = np.array([n in self.class_names for n in data_dict['gt_names']], dtype=np.bool_)

            data_dict = self.data_augmentor.forward(
                data_dict={
                    **data_dict,
                    'gt_boxes_mask': gt_boxes_mask
                }
            )

        if data_dict.get('gt_boxes', None) is not None:
            selected = common_utils.keep_arrays_by_name(data_dict['gt_names'], self.class_names)
            data_dict['gt_boxes'] = data_dict['gt_boxes'][selected]
            data_dict['gt_names'] = data_dict['gt_names'][selected]
            gt_classes = np.array([self.class_names.index(n) + 1 for n in data_dict['gt_names']], dtype=np.int32)
            gt_boxes = np.concatenate((data_dict['gt_boxes'], gt_classes.reshape(-1, 1).astype(np.float32)), axis=1)
            data_dict['gt_boxes'] = gt_boxes

            if data_dict.get('gt_boxes2d', None) is not None:
                data_dict['gt_boxes2d'] = data_dict['gt_boxes2d'][selected]

        if data_dict.get('points', None) is not None:
            data_dict = self.point_feature_encoder.forward(data_dict)

        data_dict = self.data_processor.forward(
            data_dict=data_dict
        )

        if self.training and len(data_dict['gt_boxes']) == 0:
            new_index = np.random.randint(self.__len__())
            return self.__getitem__(new_index)

        data_dict.pop('gt_names', None)

        return data_dict

    @staticmethod
    def collate_batch(batch_list, _unused=False,
                      accumulated_step=1):
        data_dict = defaultdict(list)
        for cur_sample in batch_list:
            for key, val in cur_sample.items():
                data_dict[key].append(val)
        batch_size = len(batch_list)

        ret_list = []
        assert batch_size % accumulated_step == 0, 'batch size must be divisible by accumulated_step'
        accumulated_interval = batch_size // accumulated_step
        for step in range(accumulated_step):
            ret = dict()
            start_idx = step * accumulated_interval
            end_idx = (step + 1) * accumulated_interval
            for key, val in data_dict.items():
                val = val[start_idx:end_idx]
                try:
                    if key in ['voxels', 'voxel_num_points']:
                        ret[key] = np.concatenate(val, axis=0)
                    elif key in ['points', 'voxel_coords']:
                        coors = []
                        for i, coor in enumerate(val):
                            coor_pad = np.pad(coor, ((0, 0), (1, 0)), mode='constant', constant_values=i)
                            coors.append(coor_pad)
                        ret[key] = np.concatenate(coors, axis=0)
                    elif key in ['gt_boxes']:
                        max_gt = max([len(x) for x in val])
                        batch_gt_boxes3d = np.zeros((accumulated_interval, max_gt, val[0].shape[-1]), dtype=np.float32)
                        for k in range(accumulated_interval):
                            batch_gt_boxes3d[k, :val[k].__len__(), :] = val[k]
                        ret[key] = batch_gt_boxes3d
                    elif key in ['gt_boxes2d']:
                        max_boxes = max([len(x) for x in val])
                        batch_boxes2d = np.zeros((accumulated_interval, max_boxes, val[0].shape[-1]), dtype=np.float32)
                        for k in range(accumulated_interval):
                            if val[k].size > 0:
                                batch_boxes2d[k, :val[k].__len__(), :] = val[k]
                        ret[key] = batch_boxes2d
                    elif key in ["images", "depth_maps"]:
                        # Get largest image size (H, W)
                        max_h = 0
                        max_w = 0
                        for image in val:
                            max_h = max(max_h, image.shape[0])
                            max_w = max(max_w, image.shape[1])

                        # Change size of images
                        images = []
                        for image in val:
                            pad_h = common_utils.get_pad_params(desired_size=max_h, cur_size=image.shape[0])
                            pad_w = common_utils.get_pad_params(desired_size=max_w, cur_size=image.shape[1])
                            pad_width = (pad_h, pad_w)
                            # Pad with nan, to be replaced later in the pipeline.
                            pad_value = np.nan

                            if key == "images":
                                pad_width = (pad_h, pad_w, (0, 0))
                            elif key == "depth_maps":
                                pad_width = (pad_h, pad_w)

                            image_pad = np.pad(image,
                                               pad_width=pad_width,
                                               mode='constant',
                                               constant_values=pad_value)

                            images.append(image_pad)
                        ret[key] = np.stack(images, axis=0)
                    else:
                        ret[key] = np.stack(val, axis=0)
                except:
                    print('Error in collate_batch: key=%s' % key)
                    raise TypeError

            ret['batch_size'] = accumulated_interval
            ret_list.append(ret)
        return ret_list


from Util import *
import copy

""" This class manages a shader and all of its inputs as it is applied to an object """
class LEShader:
    def __init__(self, asset, inputs={}):
        self.asset = asset
        self.inputs = inputs    #keys are names, values are shader input objects (see below)
        self.obj = None #the object that this shader affects

    def __copy__(self):
        return LEShader(self.asset, copy.deepcopy(self.inputs))

    #adds or replaces an input to this shader
    def setInput(self, input):
        if self.obj:
            self.obj.nodePath.setShaderInput(input.name, input.getValue())
        self.inputs[input.name] = input

    #renames an existing input to this shader
    def renameInput(self, oldName, newName):
        input = self.inputs[oldName]
        input.name = newName
        self.inputs[newName] = input
        del self.inputs[oldName]

    #removes an input from this shader
    def clearInput(self, name):
        del self.inputs[name]
        if self.obj:
            self.obj.nodePath.clearShaderInput(name)

    #actually applies this shader to an object
    def applyToObject(self, obj):
        shader = Shader.load(self.asset.getFullFilename())
        obj.nodePath.setShader(shader)
        for i in self.inputs.values():
            i.updateOnce = False
            obj.nodePath.setShaderInput(i.name, i.getValue())

        self.obj = obj

    #updates any shader inputs for this shader
    def update(self):
        for i in self.inputs.values():
            if i.updateMe:
                self.obj.nodePath.setShaderInput(i.name, i.getValue())
            elif i.updateOnce:
                i.updateOnce = False
                self.obj.nodePath.setShaderInput(i.name, i.getValue())

    def encode(self, doc):
        node = doc.createElement("Shader")
        asset = doc.createElement("Asset")
        node.appendChild(asset)
        asset.appendChild(doc.createTextNode(self.asset.name))
        for i in self.inputs.values():
            node.appendChild(i.encode(doc))

        active = doc.createElement("Active")
        active.appendChild(doc.createTextNode(str(int(self.obj!=None))))
        node.appendChild(active)

        return node

    def decode(node, lib=None):
        inputs = {}
        for n in node.childNodes:
            if n.localName == "Asset":
                assetName = n.childNodes[0].data.strip()
                assetName = n.childNodes[0].data.strip()
                asset = lib.shaders[assetName]
            elif n.localName == "Active":
                active = bool(int(n.childNodes[0].data.strip()))
            elif n.localName == "LEShaderInputFloat":
                i = LEShaderInputFloat.decode(n)
                inputs[i.name] = i
            elif n.localName == "LEShaderInputFloat3":
                i = LEShaderInputFloat3.decode(n)
                inputs[i.name] = i
            elif n.localName == "LEShaderInputFloat4":
                i = LEShaderInputFloat4.decode(n)
                inputs[i.name] = i
            elif n.localName == "LEShaderInputFloat4x4":
                i = LEShaderInputFloat4x4.decode(n)
                inputs[i.name] = i
            elif n.localName == "LEShaderInputTime":
                i = LEShaderInputTime.decode(n)
                inputs[i.name] = i
            elif n.localName == "LEShaderInputObj":
                i = LEShaderInputObj.decode(n)
                inputs[i.name] = i
            elif n.localName == "LEShaderInputCameraPos":
                i = LEShaderInputCameraPos.decode(n)
                inputs[i.name] = i
            elif n.localName == "LEShaderInputTexture":
                i = LEShaderInputTexture.decode(n, lib=lib)
                inputs[i.name] = i
            elif n.localName == "LEShaderInputCubemap":
                i = LEShaderInputCubemap.decode(n, lib=lib)
                inputs[i.name] = i

        shader = LEShader(asset, inputs)
        shader.active = active

        return shader

    decode = staticmethod(decode)

    def BVWEncode(self):
        lines = []

        lines.append("    shader = Shader.load('" + self.asset.filename.getFullpath() + "')\n")
        lines.append("    objects['" + self.obj.name + "'].setShader(shader)\n")

        for i in self.inputs.values():
            lines.extend(i.BVWEncode(self.obj.name))

        return lines

    def incRefCounts(self):
        self.asset.incSceneCount()
        for i in self.inputs.values():
            i.incRefCounts()

    def decRefCounts(self):
        self.asset.decSceneCount()
        for i in self.inputs.values():
            i.decRefCounts()

#base class for all shader inputs
class LEShaderInput:
    def __init__(self, name, updateMe=False):
        self.name = name
        self.updateMe = updateMe    #specifies whether this input needs to be updated every frame
        self.updateOnce = False     #change this when something has changed that requires a one-time update

    def encode(self, doc):
        node = doc.createElement(self.__class__.__name__)
        name = doc.createElement("Name")
        node.appendChild(name)
        name.appendChild(doc.createTextNode(self.name))

        return node

    def decode(node):
        for n in node.childNodes:
            if n.localName == "Name":
                name = n.childNodes[0].data.strip()

        return LEShaderInput(name)

    decode = staticmethod(decode)

    def incRefCounts(self):
        pass

    def decRefCounts(self):
        pass

#base class for all constant shader inputs
class LEShaderInputConst(LEShaderInput):
    def __init__(self, name, value=None):
        LEShaderInput.__init__(self, name, updateMe=False)
        self.value = value

    def __deepcopy__(self, memo):
        return self.__class__(copy.copy(self.name), copy.copy(self.value))

    def decode(node):
        i = LEShaderInput.decode(node)
        i.updateMe = False
        i.__class__ = LEShaderInputConst
        return i

    decode = staticmethod(decode)

    def BVWEncode(self, objName):
        lines = []

        lines.append("    objects['" + objName + "'].setShaderInput('" + self.name + "', " + str(self.getValue()) + ")\n")

        return lines

    def getValue(self):
        return self.value

    def setValue(self, newValue):
        self.value = newValue
        self.updateOnce = True

#constant shader input with a float value
class LEShaderInputFloat(LEShaderInputConst):
    def __init__(self, name, value=0.0):
        LEShaderInputConst.__init__(self, name, value=value)
    def encode(self, doc):
        node = LEShaderInputConst.encode(self, doc)
        value = doc.createElement("Value")
        node.appendChild(value)
        value.appendChild(doc.createTextNode(str(self.value)))

        return node

    def decode(node):
        input = LEShaderInputConst.decode(node)
        input.__class__ = LEShaderInputFloat
        for n in node.childNodes:
            if n.localName == "Value":
                input.value = float(n.childNodes[0].data.strip())

        return input

    decode = staticmethod(decode)

#constant shader input whose value is a vector of 3 floats
class LEShaderInputFloat3(LEShaderInputConst):
    def __init__(self, name, value=Vec3(0,0,0)):
        LEShaderInputConst.__init__(self, name, value=value)
    def encode(self, doc):
        node = LEShaderInputConst.encode(self, doc)
        node.appendChild(encodeVec3(self.value, "Value", doc))

        return node

    def decode(node):
        input = LEShaderInputConst.decode(node)
        input.__class__ = LEShaderInputFloat3
        for n in node.childNodes:
            if n.localName == "Value":
                input.value = decodeVec3(n)

        return input

    decode = staticmethod(decode)

#constant shader input whose value is a vector of 4 floats
class LEShaderInputFloat4(LEShaderInputConst):
    def __init__(self, name, value=VBase4(0,0,0,0)):
        LEShaderInputConst.__init__(self, name, value=value)
    def encode(self, doc):
        node = LEShaderInputConst.encode(self, doc)
        node.appendChild(encodeColor(self.value, "Value", doc))

        return node

    def decode(node):
        input = LEShaderInputConst.decode(node)
        input.__class__ = LEShaderInputFloat4
        for n in node.childNodes:
            if n.localName == "Value":
                input.value = decodeColor(n)

        return input

    decode = staticmethod(decode)

#constant shader input whose value is a 4x4 matrix of floats
class LEShaderInputFloat4x4(LEShaderInputConst):
    def __init__(self, name, value=Mat4.identMat()):
        LEShaderInputConst.__init__(self, name, value=value)
    def encode(self, doc):
        node = LEShaderInputConst.encode(self, doc)
        node.appendChild(encodeMat4(self.value, "Value", doc))

        return node

    def decode(node):
        input = LEShaderInputConst.decode(node)
        input.__class__ = LEShaderInputFloat4x4
        for n in node.childNodes:
            if n.localName == "Value":
                input.value = decodeMat4(n)

        return input

    decode = staticmethod(decode)

#shader input that is the current global clock time
class LEShaderInputTime(LEShaderInput):
    def __init__(self, name, offset=0):
        LEShaderInput.__init__(self, name, updateMe=True)
        self.offset = offset    #this offset will be added to the global clock's time

    def __deepcopy__(self, memo):
        return LEShaderInputTime(copy.copy(self.name), copy.copy(self.offset))

    def encode(self, doc):
        node = LEShaderInput.encode(self, doc)
        offset = doc.createElement("Offset")
        offset.appendChild(doc.createTextNode(str(self.offset)))

        return node

    def decode(node):
        i = LEShaderInput.decode(node)
        i.updateMe = True
        i.__class__ = LEShaderInputTime

        i.offset = 0
        for n in node.childNodes:
            if n.localName == "Offset":
                i.offset = float(n.childNodes[0].data.strip())

        return i

    decode = staticmethod(decode)

    def BVWEncode(self, objName):
        lines = []

        lines.append("    objects['" + objName + "'].setShaderInput('" + self.name + "', globalClock.getFrameTime() + " + str(self.offset) + ")\n")
        lines.append("    timeObjs.append( ('" + self.name + "', " + str(self.offset) + ", objects['" + objName + "']) )\n")

        return lines

    def setOffset(self, offset):
        self.offset = offset

    def getOffset(self):
        return self.offset

    def getValue(self):
        return globalClock.getFrameTime() + self.offset

#Shader input that references another nodepath
class LEShaderInputObj(LEShaderInput):
    def __init__(self, name, obj=None, updateMe=False):
        LEShaderInput.__init__(self, name, updateMe=updateMe)
        self.obj = obj

    def __deepcopy__(self, memo):
        return LEShaderInputObj(copy.copy(self.name), self.obj, copy.copy(self.updateMe))

    def encode(self, doc):
        node = LEShaderInput.encode(self, doc)
        obj = doc.createElement("Obj")
        obj.appendChild(doc.createTextNode(self.obj.name))
        node.appendChild(obj)

        return node

    def decode(node):
        i = LEShaderInput.decode(node)
        i.__class__ = LEShaderInputObj

        for n in node.childNodes:
            if n.localName == "Obj":
                i.obj = n.childNodes[0].data.strip()

        return i

    decode = staticmethod(decode)

    def BVWEncode(self, objName):
        lines = []

        lines.append("    objects['" + objName + "'].setShaderInput('" + self.name + "', objects['" + self.obj.name + "'])\n")

        return lines

    def getObj(self):
        return self.obj

    def setObj(self, newObj):
        self.obj = newObj
        self.updateOnce = True

    def getValue(self):
        return self.obj.nodePath

#For using the camera position as a shader input
class LEShaderInputCameraPos(LEShaderInput):
    def __init__(self, name):
        LEShaderInput.__init__(self, name, updateMe=True)

    def __deepcopy__(self, memo):
        return LEShaderInputCameraPos(copy.copy(self.name))

    def decode(node):
        i = LEShaderInput.decode(node)
        i.__class__ = LEShaderInputCameraPos
        i.updateMe = True

        return i

    decode = staticmethod(decode)

    def getValue(self):
        if base.cam:
            return base.cam.getPos(render)
        else:
            return base.le.ui.perspView.cam.getPos(render)

    def BVWEncode(self, objName):
        lines = []

        lines.append("    objects['" + objName + "'].setShaderInput('" + self.name + "', base.cam.getPos(render))\n")
        lines.append("    cameraObjs.append( ('" + self.name + "', objects['" + objName + "']) )\n")

        return lines

#Shader input whose value is a texture
class LEShaderInputTexture(LEShaderInput):
    def __init__(self, name, tex = None, updateMe = False):
        LEShaderInput.__init__(self, name, updateMe = updateMe)
        self.tex = tex

        if tex != None:
            self.texture = loader.loadTexture(tex.getFullFilename())

    def __deepcopy__(self, memo):
        return LEShaderInputTexture(copy.copy(self.name), self.tex, self.updateMe)

    def encode(self,doc):
        node = LEShaderInput.encode(self, doc)

        if self.tex:
            tex = doc.createElement("Tex")
            tex.appendChild(doc.createTextNode(self.tex.name))
            node.appendChild(tex)

        return node

    def decode(node, lib=None):
        if not lib:
            lib = base.le.lib

        i = LEShaderInput.decode(node)
        i.__class__ = LEShaderInputTexture
        i.tex = None
        for n in node.childNodes:
            if n.localName == "Tex":
                texName = n.childNodes[0].data.strip()
                i.tex = lib.textures[texName]
                i.texture = loader.loadTexture(i.tex.getFullFilename())
        return i

        pass

    decode = staticmethod(decode)

    def BVWEncode(self, objName):
        lines = []

        lines.append("    tex = loader.loadTexture('" + self.tex.filename.getFullpath() + "')\n")
        lines.append("    objects['" + objName + "'].setShaderInput('" + self.name + "', tex)\n")

        return lines

    def incRefCounts(self):
        if self.tex:
            self.tex.incSceneCount()

    def decRefCounts(self):
        if self.tex:
            self.tex.decSceneCount()

    def getTexture(self):
        return self.tex

    def setTexture(self, texture):
        if self.tex:
            self.tex.decSceneCount()
        self.tex = texture  #pointer to a texture asset
        self.texture = loader.loadTexture(self.tex.getFullFilename())   #an actual panda texture
        self.tex.incSceneCount()

    def getValue(self):
        return self.texture

#Cube map shader input
class LEShaderInputCubemap(LEShaderInput):
    def __init__(self, name, tex = None, updateMe = False):
        LEShaderInput.__init__(self, name, updateMe = updateMe)
        self.tex = tex
        if tex != None:
            self.texture = loader.loadCubeMap(tex.getFullFilename())

    def __deepcopy__(self, memo):
        return LEShaderInputCubemap(copy.copy(self.name), self.tex, self.updateMe)

    def encode(self,doc):
        node = LEShaderInput.encode(self, doc)
        if self.tex:
            tex = doc.createElement("Cubemap")
            tex.appendChild(doc.createTextNode(self.tex.name))
            node.appendChild(tex)

        return node

    def decode(node, lib=None):
        if not lib:
            lib = base.le.lib

        i = LEShaderInput.decode(node)
        i.__class__ = LEShaderInputCubemap
        i.tex = None

        for n in node.childNodes:
            if n.localName == "Cubemap":
                texName = n.childNodes[0].data.strip()
                i.tex = lib.textures[texName]
                i.texture = loader.loadCubeMap(i.tex.getFullFilename())
        return i

        pass

    decode = staticmethod(decode)

    def BVWEncode(self, objName):
        lines = []

        lines.append("    cubemap = loader.loadCubeMap('" + self.tex.filename.getFullpath() + "')\n")
        lines.append("    objects['" + objName + "'].setShaderInput('" + self.name + "', cubemap)\n")

        return lines

    def getTexture(self):
        return self.tex

    def setTexture(self, texture):
        if self.tex:
            self.tex.decSceneCount()
        self.tex = texture
        self.texture = loader.loadCubeMap(self.tex.getFullFilename())
        self.tex.incSceneCount()

    def getValue(self):
        return self.texture

    def incRefCounts(self):
        if self.tex:
            self.tex.incSceneCount()

    def decRefCounts(self):
        if self.tex:
            self.tex.decSceneCount()
class DuplicateInputError(Exception):
    def __init__(self, name):
        self.name = name


n = int(input())
arr = []
for _ in range(n):
    arr.append(int(input().split(" ")[0]))
counts = [0]*100
for i in arr:
    counts[i]+=1
prev_sum = 0
for i in range(1,100):
    counts[i] += counts[i-1]
print(" ".join(map(str, counts)))


class QryUnAuthPayrollCompanionKafala:
  def __init__(self,formObj,parentObj):
    self.form = formObj
    self.app = formObj.ClientApplication
    self.FormView = None

  def Show(self):
    self.ShowKUM3()
    self.FormContainer.Show()

  def FilterClick(self,sender):
    self.ShowKUM3()

  def ShowKUM3(self):
    query1 = self.query1
    uipFilter = self.uipFilter
    AddParam = ''
    scol = uipFilter.Incolumn or 0
    fsearch={0:'',1:'LTransaction.TransactionNo',2:'AccountNo',3:'Description', 4:'EkuivalenAmount',5:'LTransaction.Inputer'}
    keyword = uipFilter.keywords or ''
    if fsearch[scol] != '' :
      AddParam += "AND %s LIKE '%s'" % (fsearch[scol],keyword)
      if scol==4:
        AddParam= "AND %s=%s" % (fsearch[scol],keyword)
    query1.OQLText = "Select from ProductTransactionItem \
           [LTransaction.IsAuthorized = 'F' \
           AND PartitionCode = 'S' \
           AND IsInternalPaidTo = 'T' \
           AND LTransaction.TransactionNo LIKE 'Kafala' %s ] \
           (LTransaction.TransactionId, \
           LTransaction.TransactionDate, \
           LTransaction.TransactionNo, \
           AccountNo, \
           EkuivalenAmount, \
           LTransaction.Inputer, \
           Description, \
            self); " % (AddParam)
    query1.DisplayData()

  def Aktifkan(self,key):
    if (self.app.ConfirmDialog('Yakin Data ini diotorisasi ?')):
      params = self.app.CreateValues(['key',key])
      retval = self.form.CallServerMethod('Aktif',params)
      self.query1.DisplayData
      status = retval.FirstRecord
      if status.IsErr :
        pesan = status.ErrMessage
        self.app.ShowMessage(pesan)
        return 0
      else :
        self.app.ShowMessage('Transaksi Telah Berhasil Diotorisasi')

  def Hapus(self,key):
    if (self.app.ConfirmDialog('Yakin Transaksi ini akan dibatalkan ?')):
      params = self.app.CreateValues(['key',key])
      retval = self.form.CallServerMethod('Proses',params)

      status = retval.FirstRecord
      #raise '',status.Peringatan
      if status.Peringatan != '':
        self.app.ShowMessage(status.Peringatan)
        return 0
      else :
        self.app.ShowMessage('Data Transaksi Telah Dihapubatalakan')


import matplotlib.pyplot as plt
import numpy as np

def F(x):
    return x ** 3 - x ** 2 + 0.1

def Fp(x):
    return 3.0 * (x ** 2) - 2.0 * x

def Fa(x, x_0, y_0, m):
    return m * (x - x_0) + y_0

pause_time = 2

# Plot the function
xmin, xmax = -0.5, 1.5
x = np.linspace(xmin, xmax, 10001)
fig, ax = plt.subplots(figsize=(10, 6))
ax.axhline(0.0, color = "#888888")
ax.plot(x, F(x), color = "k", linestyle = "-")
ax.set(xlabel=r"$x$", ylabel=r"$F(x)$", xlim=(xmin, xmax), ylim=(-0.1, 0.2))
plt.ion()
plt.show()

# Initial guess
x_sa = 0.7

for k in range(10):
    lab_k = ax.text((xmax + xmin)/2, 0.18, f'Iteration k = {k}',
                    horizontalalignment='center')

    # Current guess
    marker_line = ax.axvline(x_sa, color = "r", linestyle = ":")
    current_guess = ax.plot([x_sa], [F(x_sa)],
                            marker = "x", markersize = 10,
                            color = "r", markeredgewidth = 2)
    plt.pause(pause_time)

    # New tangent
    x_0, y_0, m = x_sa, F(x_sa), Fp(x_sa)

    xa = np.array([xmin, xmax])
    tangent = ax.plot(xa, Fa(xa, x_0, y_0, m), color = "r", linestyle = "-")
    plt.pause(pause_time)

    # Next guess
    x_sa = (-y_0 / m) + x_0

    next_guess = ax.plot([x_sa], [0.0],
                         marker = "x", markersize = 10,
                         color = "r", markeredgewidth = 2)
    plt.pause(pause_time)

    tangent[0].remove()
    current_guess[0].remove()
    marker_line.remove()
    next_guess[0].remove()
    lab_k.remove()


#!/usr/bin/env python3

class Set:
    def __init__(self):
        self.items = []

    def has(self, item):
        return item in self.items

    def put(self, item):
        assert not (item in self.items)
        self.items.append(item)

    def size(self):
        return len(self.items)

    def take(self, item):

        index = 0
        while index < self.size():
            if self.items[index] == item:
                self.items.pop(index)
            else:
                index += 1

    def common(self, other):
        items = Set()

        for item in self.items:
            if other.has(item):
                items.put(item)

        return items

# TESTS
failed = 0
ran = 0

def test(name, actual, expected):
    global ran, failed

    if actual == expected:
        print(name, 'OK')
    else:
        print(name, 'FAILED: got', actual, 'instead of', expected)
        failed += 1
    ran += 1

even = Set()
even.put(0)
even.put(2)
even.put(4)
even.put(6)

squares = Set()
squares.put(0)
squares.put(1)
squares.put(4)
squares.put(9)

even_squares = even.common(squares)
test('even squares size', even_squares.size(), 2)
test('0 is an even square', even_squares.has(0), True)
test('4 is an even square', even_squares.has(4), True)
test('2 is even, not square', even_squares.has(2), False)

square_evens = squares.common(even)
test('same size', even_squares.size() == square_evens.size(), True)
for n in range(10):
    test('has ' + str(n), even_squares.has(n) == square_evens.has(n), True)

odd = Set()
odd.put(1)
odd.put(3)

test('no common numbers', odd.common(even).size(), 0)

print()
print('All tests run:', ran - failed, 'OK,', failed, 'FAILED')

if failed == 0:
    print('''All tests passed!''')


# Programmer: Christine Doan
# Assignment 4, Functions

# Part 1: Statistical Functions
# Write four functions for means

# import
import math

# variables

a = float(input("Insert the first number: "))
b = float(input("Insert the second number: "))
c = float(input("Insert the third number: "))

# functions

def mean (x, y, z):
    result = (x + y + z) / 3
    return result

def geometric_mean(x, y, z):
    gresult = ((a * b * c) **(1/3))
    return gresult

def harmonic_mean(x, y, z):
    hresult = (3 / ((1/x) + (1/y) + (1/z)))
    return hresult

def reciprocal (r):
    return 1.0 / r

# calculations
armean = mean(a, b, c)
gmean = geometric_mean(a, b, c)
hmean = harmonic_mean (a, b, c)

# outputs
print("Arithmetic mean is", (format(armean, '.2f')))
print("Geometric mean is", (format(gmean, '.2f')))
print("Harmonic mean is", (format(hmean, '.2f')))

print("Reciprocal for first number is", (reciprocal(a)))
print("Reciprocal for second number is", (reciprocal(b)))
print("Reciprocal for third number is", (reciprocal(c)))


#!/usr/bin/env python3
#
# Copyright 2021 Graviti. Licensed under MIT License.
#

"""Implementation of gas commit."""

from typing import Dict

import click

from .tbrn import TBRN, TBRNType
from .utility import edit_input, error, get_dataset_client, get_gas

_COMMIT_HINT = """{}
# Please enter the commit message for your changes.
# The Default commit message is set as the draft title.
# Lines starting with '#' will be ignored.
# And an empty commit message aborts the commit.
"""

def _implement_commit(obj: Dict[str, str], tbrn: str, message: str) -> None:
    gas = get_gas(**obj)
    info = TBRN(tbrn=tbrn)
    dataset_client = get_dataset_client(gas, info)

    if info.type != TBRNType.DATASET:
        error(f'To operate a commit, "{info}" must be a dataset')

    if not info.is_draft:
        error(f'To commit, "{info}" must be in draft status, like "{info}#1"')

    dataset_client.checkout(draft_number=info.draft_number)
    if not message:
        message, description = edit_input(_COMMIT_HINT.format(dataset_client.get_draft().title))

    if not message:
        error("Aborting commit due to empty commit message")

    if description:
        error('Commit with "description" is not supported yet')

    dataset_client.commit(message)
    commit_tbrn = TBRN(info.dataset_name, revision=dataset_client.status.commit_id).get_tbrn()
    click.echo(f"Committed successfully: {tbrn} -> {commit_tbrn}")


import sys
import os
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class RPathView(QCheckBox):
    def __init__(self,parent=None):
        super(RPathView,self).__init__(parent)
        self.setText('path view')

class RFileItem(QStandardItem):
    def __init__(self,url,parent=None):
        super(RFileItem,self).__init__(parent)
        self.oldpath = url.toLocalFile()
        self.oldfilename = os.path.basename(self.oldpath)
        self.newfilename = self.oldfilename
        self.setText(self.newfilename)

    def __eq__(self,other):
        return self.oldpath == other.oldpath

    def setFileName(self):
        newtext = self.text()
        if newtext == self.oldpath:
            pass
        elif newtext == self.oldfilename:
            self.newfilename = newtext
            self.setBackground(QBrush(QColor(255,255,255)))
        else:
            self.newfilename = newtext
            self.setBackground(QBrush(QColor(255,255,0)))

class RFileListModel(QStandardItemModel):
    sameItem = pyqtSignal(RFileItem)

    def __init__(self,parent=None):
        super(RFileListModel,self).__init__(parent)

        self.itemChanged.connect(self.backgroundCheck)

    def appendRow(self,item):
        if item in self.allRows():
            self.sameItem.emit(item)
        else:
            super(RFileListModel,self).appendRow(item)

    def allRows(self):
        items = []
        for index in range(self.rowCount()):
            items.append(self.item(index))
        return items

    def pathMode(self,mode):
        items = self.allRows()
        for item in items:
            if mode:
                item.setText(item.oldpath)
            else:
                item.setText(item.newfilename)

    def selectionMode(self,new,old):
        Nindexes = new.indexes()
        Oindexes = old.indexes()
        for index in Nindexes:
            item = self.itemFromIndex(index)
            item.setText(item.newfilename)
        for index in Oindexes:
            item = self.itemFromIndex(index)
            item.setText(item.oldpath)

    def backgroundCheck(self,item):
        item.setFileName()

    def mimeData(self,indexes):
        mime = QMimeData()
        mime.rInternalFlag = True
        mime.rmodel = self
        items = []
        for index in indexes:
            items.append(self.item(index.row()))

        mime.ritems = items
        return mime

class RFileListView(QListView):
    def __init__(self,parent=None):
        super(RFileListView,self).__init__(parent)
        self.model = RFileListModel()
        self.setModel(self.model)
        self.setAcceptDrops(True)
        self.setDragEnabled(True)
        self.setDragDropMode(QAbstractItemView.DragDrop)
        self.setSelectionMode(QAbstractItemView.ExtendedSelection)

    def dragEnterEvent(self,event):
        mime = event.mimeData()
        if mime.hasUrls():
            urls = mime.urls()
            if any(list(map(lambda x:os.path.isfile(x.toLocalFile()),urls))):
                event.accept()
            else:
                event.ignore()
        else:
            event.ignore()

    def dragMoveEvent(self,event):
        event.accept()

    def dropEvent(self,event):
        urls = event.mimeData().urls()
        for url in urls:
            self.model.appendRow(RFileItem(url))
        event.accept()

    def keyPressEvent(self,event):
        if event.text() == 'd':
            indexes = self.selectedIndexes()
            for index in indexes:
                self.model.takeRow(index.row())

class RFileListWidget(QListWidget):
    newsize = pyqtSignal(QSize)

    def __init__(self,parent=None):
        super(RFileListWidget,self).__init__(parent)
        self.view = RFileListView(self)

        self.newsize.connect(self.view.resize)

    def resizeEvent(self,event):
        self.newsize.emit(event.size())
        super(RFileListWidget,self).resizeEvent(event)

class RFileList(QWidget):
    def __init__(self,parent=None):
        super(RFileList,self).__init__(parent)
        layout = QVBoxLayout()
        self.pathview = RPathView()
        self.filelist = RFileListWidget()
        layout.addWidget(self.pathview)
        layout.addWidget(self.filelist)
        self.setLayout(layout)

        self.pathview.stateChanged.connect(self.filelist.view.model.pathMode)
        self.pathview.stateChanged.connect(self.pathMode)

    def pathMode(self,mode):
        if mode:
            self.filelist.view.selectionModel().selectionChanged.connect(self.filelist.view.model.selectionMode)
        else:
            self.filelist.view.selectionModel().selectionChanged.disconnect(self.filelist.view.model.selectionMode)

def main():
    app = QApplication(sys.argv)

    w = RFileList()
    w.show()

    app.exec_()

if __name__ == '__main__':
    main()


import sys, action, status, helpers
# new imports start here

# settings = helpers.get_settings()

try:
	action = str(sys.argv[1])
except:
	action = None

if action == 'status' or action == None:
	status.execute()

elif action == 'action':
	# You will want to change the name to something specific, when developing
	action.execute()
# new actions start here


import tensorflow as tf
import numpy as np
from tensorflow.lite.python.lite import Optimize

tf.compat.v1.disable_eager_execution() # 즉시 실행모드를 끈다

print(tf.executing_eagerly()) #False

print(tf.__version__)

# tf.set_random_seed(104)

# 1
from tensorflow.keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

from tensorflow.keras.utils import to_categorical
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

x_train = x_train.reshape(60000,28,28,1).astype('float32')/255
x_test = x_test.reshape(10000,28,28,1).astype('float32')/255

learning_rate = 0.0001
trianing_epochs = 15
batch_size = 100
total_batch = int(len(x_train)/batch_size)

x = tf.compat.v1.placeholder(dtype='float32', shape=[None,28,28,1])
y = tf.compat.v1.placeholder(dtype='float32', shape=[None,10])

# 2

# L1.
w1 =  tf.compat.v1.get_variable("w1",shape=[4,4,1,128])
L1 = tf.nn.conv2d(x,w1, strides=[1,1,1,1],padding='SAME')
print(L1)
# Conv2D(filter, kernel_size, input_shape)
# Conv2D(32, (3,3), input_shape=(28, 28, 1))
# strides=[1,2,2,1] : strides=2 일떄
# strides=[1,3,3,1] : strides=3 일떄 앞뒤에 1은 차원을 맞춰주는 용도
L1 = tf.nn.selu(L1)
L1 = tf.nn.max_pool(L1, ksize=[1,2,2,1],strides=[1,2,2,1], padding='SAME') # padding='SAME'은 줄일때 홀수이면 +1 VALID는 -1
print(L1)

# L2.
w2 =  tf.compat.v1.get_variable("w2",shape=[4,4,128,128])
L2 = tf.nn.conv2d(L1, w2, strides=[1,1,1,1],padding='SAME')
L2 = tf.nn.elu(L2)
print(L2)

# L3.
w3 =  tf.compat.v1.get_variable("w3",shape=[2,2,128,128])
L3 = tf.nn.conv2d(L2, w3, strides=[1,1,1,1],padding='SAME')
L3 = tf.nn.relu(L3)
L3 = tf.nn.max_pool(L3, ksize=[1,2,2,1],strides=[1,2,2,1], padding='SAME') # padding='SAME' :(?, 4, 4, 8) , padding='VALID' :(?, 3, 3, 8)
print(L3)

# L4.
w4 =  tf.compat.v1.get_variable("w4",shape=[2,2,128,128])
L4 = tf.nn.conv2d(L3, w4, strides=[1,1,1,1],padding='SAME')
L4 = tf.nn.relu(L4)
print(L4)

# Flatten
L_flat = tf.reshape(L4, [-1,L4.shape[1]*L4.shape[2]*L4.shape[3]])
print(L_flat)

# L5
w5 = tf.compat.v1.get_variable('w5', shape = [L4.shape[1]*L4.shape[2]*L4.shape[3], 32],initializer=tf.compat.v1.keras.initializers.VarianceScaling)
b5 = tf.compat.v1.Variable(tf.random.normal([32]), name='b5')
L5 = tf.nn.relu(tf.matmul(L_flat, w5) + b5)
L5 = tf.nn.dropout(L5, rate=0.2)
print(L5)

# L6
w6 = tf.compat.v1.get_variable('w6', shape = [32, 16],initializer=tf.compat.v1.keras.initializers.VarianceScaling)
b6 = tf.compat.v1.Variable(tf.random.normal([16]), name='b6')
L6 = tf.nn.relu(tf.matmul(L5, w6) + b6)
L6 = tf.nn.dropout(L6, rate=0.2)
print(L6)

# hypothesis
w7 = tf.compat.v1.get_variable('w7', shape = [16, 10],initializer=tf.compat.v1.keras.initializers.VarianceScaling)
b7 = tf.compat.v1.Variable(tf.random.normal([10]), name='b7')
hypothesis = tf.nn.softmax(tf.matmul(L6, w7) + b7)
print(hypothesis)

#3

loss =  tf.reduce_mean(-tf.reduce_sum(y*tf.math.log(hypothesis), axis=1)) #categorical_crossentropy
optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate).minimize(loss)

# 훈련
sess = tf.compat.v1.Session()
sess.run(tf.compat.v1.global_variables_initializer())

for epoch in range(trianing_epochs):
    avg_cost = 0

    for i in range(total_batch):     # 600번 돈다
        start = i * batch_size
        end = start + batch_size

        batch_x, batch_y = x_train[start:end], y_train[start:end]
        feed_dict = {x:batch_x, y:batch_y}
        c, _ = sess.run([loss, optimizer], feed_dict=feed_dict)
        avg_cost += c/total_batch
    print('epoch : ','%04d' %(epoch+1),'cost = {:.9f}'.format(avg_cost))

print(" 훈련 끝 ")

prediction = tf.equal(tf.argmax(hypothesis,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(prediction,tf.float32))
print('acc : ', sess.run(accuracy,feed_dict = {x:x_test,y:y_test}))


from typing import Optional, Tuple

import numpy as np
import sys

from .processing.processor import SpectrumIterator, process_mzml
from ms_process.processing.filters import ElectricNoiseFilter, ResamplerFilter, SGolayFilter, AsFloat32Filter

def process_file(in_filename: str,
                 out_filename: str,
                 threshold_multiplier: int,
                 resampler_step: float,
                 central_mz: float):
    sys.stderr.write("Processing data...\n")
    filters = [
        ElectricNoiseFilter(threshold_multiplier=threshold_multiplier),
        ResamplerFilter(sampling_rate=resampler_step, central_mz=central_mz),
        SGolayFilter(window_length=11, polyorder=4),
        AsFloat32Filter(),
    ]
    process_mzml(in_filename, out_filename, filters, last_ms1_specra_count=10)


import os
from contextlib import contextmanager

@contextmanager
def environ(**kwargs):
    orig = dict()
    todel = []
    try:
        for k, newval in kwargs.items():
            if k in os.environ:
                orig[k] = os.environ[k]
            else:
                todel.append(k)
            os.environ[k] = newval
        yield
    finally:
        for k, oldval in orig.items():
            os.environ[k] = oldval
        for k in todel:
            del os.environ[k]

def AnyType(cls):
    class AnyType(cls):
        def __eq__(self, other):
            return isinstance(other, cls)

    return AnyType()


""" From "COMPUTATIONAL PHYSICS" & "COMPUTER PROBLEMS in PHYSICS"
    by RH Landau, MJ Paez, and CC Bordeianu (deceased)
    Copyright R Landau, Oregon State Unv, MJ Paez, Univ Antioquia,
    C Bordeianu, Univ Bucharest, 2017.
    Please respect copyright & acknowledge our work."""

# HOnumericReflex.py: Quantum HO wave functions via ODE solver

import numpy as np
import matplotlib.pylab as plt
from rk4Algor import rk4Algor

n = 5   #  Quantum number n = npr + L + 1 = integer > 0
xx = np.zeros((1000),float)            # x values for plot
yy = np.zeros((1000),float)            # wave function values
fvector = [0]*(2)                      # force function f
y = [0]*(2)     # array for 2 values

def f(x,y):                          # Force function for HO
    fvector[0] = y[1]
    fvector[1] = -(2*n+1-x**2)*y[0]
    return fvector

if (n%2 == 0):   y[0] = 1.;   y[1] = 0.   # Even parity
else:            y[0] = 0;    y[1] = -1.   # Odd parity

xRight = 5
f(0,y)     # force function at starting value
dx = 0.01
i = 0
# Compute WF from 0 to xRight in steps of dr
for x in np.arange(0,xRight,dx):
    xx[i+500] = x
    y = rk4Algor(x, dr, 2, y, f)
    yy[i+500] = y[0]   #
    i = i+1      # Advance i as well as x
          # Now reflect
i = 0
for x in np.arange(-dx,-xRight,-dx):
    xx[499-i] =  -xx[i+499]
    if (n%2 == 0): yy[499-i] =  yy[i+499]    # Even parity
    else:          yy[499-i] = -yy[i+499]    # Ddd parity
    i = i + 1
plt.figure()
plt.plot(xx,yy)
plt.grid()
plt.title('Harmonic Oscillator Wave Function n = 5')
plt.xlabel('x')
plt.ylabel('Wave Function u(x)')
plt.show()


import requests
import os
import pandas as pd
import json
import smtplib
from pathlib import Path
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from datetime import datetime as dt

# sources
# https://stackoverflow.com/questions/3362600/how-to-send-email-attachments
# https://www.tutorialspoint.com/send-mail-with-attachment-from-your-gmail-account-using-python

# read in data
credentials = pd.read_csv('backend/email_credentials.csv').squeeze()
recipients = json.load(open('backend/mailing_list.json', 'r'))
auth = pd.read_csv('backend/auth.csv').squeeze()

def post_slack_message(text, blocks = None):
    return requests.post('https://slack.com/api/chat.postMessage',
    {
        'token': auth['slack_token'],
        'channel': auth['slack_channel'],
        'text': text,
        'blocks': json.dumps(blocks) if blocks else None
    }).json()

def build_slack_message(email_details):

    return '\n'.join(str(detail[0]) + ' received data from ' + str(detail[1]) +
           ' file(s) regarding ' + str(detail[2]) + ' in the following regions: ' + ', '.join(detail[3])
           for detail in email_details)

def build_file_details(receiver, parsed_files):
    body = []
    for file in parsed_files:
        text_part = ('
' + str(file['Name']) + '' +
                     '
Regional Level: ' + receiver['region_level'] +
                     '
Regions: ' +', '.join(receiver['regions']) +
                     '
Date: ' + str(file['Date']) +
                     '
Value (95% CI): ' + str(file['Value']) + ' (' +
                                              str(file['Low_CI']) + ', ' +
                                              str(file['Upper_CI']) + ')')
        body.append(text_part)

    return ''.join(body)

def send_email(credentials, receiver, file_paths, parsed_files):
    message = MIMEMultipart()
    message['From'] = credentials['sender']
    message['To'] = receiver['address']

    # TODO: add handling for multiple dates/files
    message['Subject'] = (receiver['nickname'] + ' ' + receiver['data_type'] +
                          ' [' + parsed_files[0]['Date'] + ']')

    message['Cc'] = ','.join(receiver['cc'])
    # The subject line
    #The body and the attachments for the mail
    file_details = build_file_details(receiver, parsed_files)
    message_out = ('Hello,
Here is your requested data:
' + file_details +
                   '
Notes: ' + receiver['notes'] +
                   '

This is a bot. Please contact ' + credentials['real_email'] + ' if you have any questions.')
    message.attach(MIMEText(message_out, 'html'))

    if receiver['send_files']:
        for path in file_paths :
            part = MIMEBase('application', 'octate-stream')

            with open(path, 'rb') as file:
                part.set_payload(file.read())
            encoders.encode_base64(part)
            part.add_header('Content-Disposition',
                            'attachment; filename="{}"'.format(Path(path).name))
            message.attach(part)

#Create SMTP session for sending the mail
    session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
    session.starttls() #enable security
    session.login(credentials['sender'], credentials['sender_pass']) #login with mail_id and password
    text = message.as_string()

    all_receivers = [receiver['address']] + receiver['cc']

    session.sendmail(credentials['sender'], all_receivers, text)
    session.quit()

# extract most recent values from files to be used in email body text
def parse_file(file):
    file_name = file[0]
    file_path = file[1]

    # assumes format Date, value low CI, upper CI
    if file_path.endswith('csv'):
        df = pd.read_csv(file_path)
        max_date = max(df['Date'])
        df_new = df[(df.Date == max_date)]

        value = round(df_new.iloc[0, 1], 3)
        low_CI = round(df_new.iloc[0, 2], 3)
        upper_CI = round(df_new.iloc[0, 3], 3)

        file_values = {'Name':file_name, 'Date':max_date, 'Value':value,
                       'Low_CI':low_CI, 'Upper_CI': upper_CI}

    return file_values

# send emails
email_details = []
for receiver in recipients:
    parsed_files = [parse_file(file) for file in receiver['files']]
    file_paths = [file[1] for file in receiver['files']]

    send_email(credentials, receiver, file_paths, parsed_files)
    email_details.append([receiver['address'], len(receiver['files']), receiver['data_type'], receiver['regions']])

# build & send slack message
text = build_slack_message(email_details)
post_slack_message(text)


#!/usr/bin/env python3

"""
This Python script is used to translate a specific word from it source language,
such as Spanish into American English using the MyMemory translation service.
"""
__author__ = 'John Bumgarner'
__date__ = 'September 24, 2021'
__status__ = 'Production'
__license__ = 'MIT'
__copyright__ = "Copyright (C) 2021 John Bumgarner"

##################################################################################
# “AS-IS” Clause
#
# Except as represented in this agreement, all work produced by Developer is
# provided “AS IS”. Other than as provided in this agreement, Developer makes no
# other warranties, express or implied, and hereby disclaims all implied warranties,
# including any warranty of merchantability and warranty of fitness for a particular
# purpose.
##################################################################################

##################################################################################
#
# Date Completed: September 24, 2021
# Author: John Bumgarner
#
# Date Last Revised:
# Revised by:
#
##################################################################################

##################################################################################
# Python imports required for basic operations
##################################################################################
import sys
import logging
import requests
import traceback
from string import punctuation
from backoff import on_exception, expo
from requests.adapters import HTTPAdapter
from ratelimit import limits, RateLimitException
from requests.packages.urllib3.util.retry import Retry
from wordhoard.utilities.exceptions import RequestError
from wordhoard.utilities.exceptions import NotValidLength
from wordhoard.utilities.exceptions import TooManyRequests
from wordhoard.utilities.user_agents import get_random_user_agent
from wordhoard.utilities.exceptions import LanguageNotSupportedException

logger = logging.getLogger(__name__)

rand_user_agent = get_random_user_agent()
http_headers = {'user-agent': rand_user_agent}

class Translator(object):

    def __init__(self,
                 source_language='',
                 str_to_translate='',
                 email_address=None
                 ):

        self._source_language = source_language
        self._str_to_translate = str_to_translate
        self._url_to_query = 'http://api.mymemory.translated.net/get'
        self._email_address = email_address
        self._headers = http_headers

        ratelimit_status = False
        self._rate_limit_status = ratelimit_status

        # Retries the requests after a certain time period has elapsed
        handler = on_exception(expo, RateLimitException, max_time=60, on_backoff=self._backoff_handler)
        # Establishes a rate limit for making requests to the MyMemory translation service
        limiter = limits(calls=30, period=60)
        self.translate_word = handler(limiter(self.translate_word))
        self.reverse_translate = handler(limiter(self.reverse_translate))

    def _colorized_text(self, r, g, b, text):
        return f"\033[38;2;{r};{g};{b}m{text} \033[38;2;255;255;255m"

    def _backoff_handler(self, details):
        if self._rate_limit_status is False:
            print(self._colorized_text(255, 0, 0,
                                       'The MyMemory translation service query rate Limit was reached. The querying '
                                       'process is entering a temporary hibernation mode.'))
            logger.info('The MyMemory translation service query rate limit was reached.')
            self._rate_limit_status = True

    def _mymemory_supported_languages(self):
        """
        This function determines if the requested source language is
        one supported languages for the MyMemory Translator.

        :return: language
        :rtype: string
        """
        # MyMemory Translator supported languages as of 09-03-2021
        supported_languages = {'af': 'afrikaans',
                               'sq': 'albanian',
                               'am': 'amharic',
                               'ar': 'arabic',
                               'hy': 'armenian',
                               'az': 'azerbaijani',
                               'eu': 'basque',
                               'be': 'belarusian',
                               'bn': 'bengali',
                               'bs': 'bosnian',
                               'bg': 'bulgarian',
                               'ca': 'catalan',
                               'ceb': 'cebuano',
                               'ny': 'chichewa',
                               'zh-CN': 'chinese (simplified)',
                               'zh-TW': 'chinese (traditional)',
                               'co': 'corsican',
                               'hr': 'croatian',
                               'cs': 'czech',
                               'da': 'danish',
                               'nl': 'dutch',
                               'en': 'english',
                               'eo': 'esperanto',
                               'et': 'estonian',
                               'tl': 'filipino',
                               'fi': 'finnish',
                               'fr': 'french',
                               'fy': 'frisian',
                               'gl': 'galician',
                               'ka': 'georgian',
                               'de': 'german',
                               'el': 'greek',
                               'gu': 'gujarati',
                               'ht': 'haitian creole',
                               'ha': 'hausa',
                               'haw': 'hawaiian',
                               'iw': 'hebrew',
                               'hi': 'hindi',
                               'hmn': 'hmong',
                               'hu': 'hungarian',
                               'is': 'icelandic',
                               'ig': 'igbo',
                               'id': 'indonesian',
                               'ga': 'irish',
                               'it': 'italian',
                               'ja': 'japanese',
                               'jw': 'javanese',
                               'kn': 'kannada',
                               'kk': 'kazakh',
                               'km': 'khmer',
                               'rw': 'kinyarwanda',
                               'ko': 'korean',
                               'ku': 'kurdish',
                               'ky': 'kyrgyz',
                               'lo': 'lao',
                               'la': 'latin',
                               'lv': 'latvian',
                               'lt': 'lithuanian',
                               'lb': 'luxembourgish',
                               'mk': 'macedonian',
                               'mg': 'malagasy',
                               'ms': 'malay',
                               'ml': 'malayalam',
                               'mt': 'maltese',
                               'mi': 'maori',
                               'mr': 'marathi',
                               'mn': 'mongolian',
                               'my': 'myanmar',
                               'ne': 'nepali',
                               'no': 'norwegian',
                               'or': 'odia',
                               'ps': 'pashto',
                               'fa': 'persian',
                               'pl': 'polish',
                               'pt': 'portuguese',
                               'pa': 'punjabi',
                               'ro': 'romanian',
                               'ru': 'russian',
                               'sm': 'samoan',
                               'gd': 'scots gaelic',
                               'sr': 'serbian',
                               'st': 'sesotho',
                               'sn': 'shona',
                               'sd': 'sindhi',
                               'si': 'sinhala',
                               'sk': 'slovak',
                               'sl': 'slovenian',
                               'so': 'somali',
                               'es': 'spanish',
                               'su': 'sundanese',
                               'sw': 'swahili',
                               'sv': 'swedish',
                               'tg': 'tajik',
                               'ta': 'tamil',
                               'tt': 'tatar',
                               'te': 'telugu',
                               'th': 'thai',
                               'tr': 'turkish',
                               'tk': 'turkmen',
                               'uk': 'ukrainian',
                               'ur': 'urdu',
                               'ug': 'uyghur',
                               'uz': 'uzbek',
                               'vi': 'vietnamese',
                               'cy': 'welsh',
                               'xh': 'xhosa',
                               'yi': 'yiddish',
                               'yo': 'yoruba',
                               'zu': 'zulu'}
        try:
            if self._source_language in supported_languages.keys():
                return self._source_language
            elif self._source_language in supported_languages.values():
                return self._source_language
            else:
                return None
        except LanguageNotSupportedException as error:
            """
            An exception is thrown if the user uses a language that is not supported by the translator
            """
            logger.info(f'The language provided is not one of the supported languages of the MyMemory '
                        f'translation service.')
            logger.info(f'Requested language: {self._source_language}')
            logger.error(''.join(traceback.format_tb(error.__traceback__)))

    # reference: https://urllib3.readthedocs.io/en/latest/reference/urllib3.util.html#module-urllib3.util.retry
    @staticmethod
    def _requests_retry_session(retries=5,
                                backoff_factor=0.5,
                                status_forcelist=(500, 502, 504),
                                session=None,
                                ):
        session = session or requests.Session()
        retry = Retry(
            total=retries,
            read=retries,
            connect=retries,
            backoff_factor=backoff_factor,
            status_forcelist=status_forcelist,
        )
        adapter = HTTPAdapter(max_retries=retry)
        session.mount('http://', adapter)
        session.mount('https://', adapter)
        return session

    def _mymemory_translate(self, original_language):
        """
        This function is used to translated a word from it source language, such as Spanish
        into American English.

        :param original_language: language to translated from
        :return: translated word
        :rtype: string
        """
        try:
            if self._email_address is None:
                print(self._colorized_text(255, 0, 0,
                                           'A valid email address is required to use the MyMemory translation '
                                           'service.'))
                sys.exit(1)
            else:
                response = self._requests_retry_session().get(self._url_to_query,
                                                              params={'langpair': f'{original_language}|en-us',
                                                                      'q': self._str_to_translate,
                                                                      'de': self._email_address},
                                                              headers=self._headers)

                if response.status_code == 429:
                    raise TooManyRequests()
                elif response.status_code != 200:
                    raise RequestError()
                else:
                    data = response.json()
                    if not data:
                        return f'MyMemory could not translate the word {self._str_to_translate}.'

                    translation = data.get('responseData').get('translatedText')
                    if translation:
                        return str(translation).lower().rstrip(punctuation)

        except NotValidLength as error:
            """
            The exception is thrown if the provided text exceed the length limit of the translator
            """
            logger.error(f'The text length for the word: {self._str_to_translate} exceed the length limit of '
                         f'the MyMemory translation service.')
            logger.error(''.join(traceback.format_tb(error.__traceback__)))
        except TooManyRequests as error:
            """
            The exception is thrown if an error occurred during the request call, e.g a connection problem.
            """
            logger.error('Server Error: There has been too many requests to the server.')
            logger.error(''.join(traceback.format_tb(error.__traceback__)))

    def _mymemory_translate_reverse(self, return_all=False):
        """
        This function is used to translated a word from it source language, such as Spanish
        into American English.

        :return: translated word
        :rtype: string
        """
        try:
            if self._email_address is None:
                print(self._colorized_text(255, 0, 0,
                                           'A valid email address is required to use the MyMemory translation '
                                           'service.'))
                sys.exit(1)
            else:
                response = self._requests_retry_session().get(self._url_to_query,
                                                              params={'langpair': f'en-us|{self._source_language}',
                                                                      'q': self._str_to_translate,
                                                                      'de': self._email_address},
                                                              headers=self._headers)

                if response.status_code == 429:
                    raise TooManyRequests()
                elif response.status_code != 200:
                    raise RequestError()
                else:
                    data = response.json()
                    if not data:
                        return f'MyMemory could not translate the word {self._str_to_translate}.'

                    translation = data.get('responseData').get('translatedText')

                    if translation:
                        return str(translation).lower().strip(punctuation)

        except NotValidLength as error:
            """
            The exception is thrown if the provided text exceed the length limit of the translator
            """
            logger.error(f'The text length for the word: {self._str_to_translate} exceed the length limit of '
                         f'the MyMemory translation service.')
            logger.error(''.join(traceback.format_tb(error.__traceback__)))
        except TooManyRequests as error:
            """
            The exception is thrown if an error occurred during the request call, e.g a connection problem.
            """
            logger.error('Server Error: There has been too many requests to the server.')
            logger.error(''.join(traceback.format_tb(error.__traceback__)))

    def translate_word(self):
        """
        This function is used to translated a word from it source language, such as Spanish
        into American English.

        :return: translated word
        :rtype: string
        """
        supported_language = self._mymemory_supported_languages()
        if supported_language:
            return self._mymemory_translate(supported_language)
        elif not supported_language:
            logger.info(f'The language provided is not one of the supported languages for the MyMemory '
                        f'translation service.')
            logger.info(f'Requested language: {self._source_language}')
            return None

    def reverse_translate(self):
        """
        This function is used to translated a word from American English into
        another language, such as Spanish.

        :return: translated word
        :rtype: string
        """
        return self._mymemory_translate_reverse()


#!/usr/bin/env python

from setuptools import find_packages, setup

tests_require = [
    'pytest>=5.0.0,<6.0.0',
]

extras_require = {
    "docs": [
        'bdc-readthedocs-theme @ git+git://github.com/brazil-data-cube/bdc-readthedocs-theme.git#egg=bdc-readthedocs-theme',
        'Sphinx>=2.1.2',
    ],
    "tests": tests_require
}

setup(
    name='bdc-core',
    version='0.0.4',
    description='Brazilian Data Cube Core Package',
    author='Admin',
    author_email='admin@admin.com',
    license="MIT",
    packages=find_packages(),
    url='https://github.com/brazil-data-cube/',
    install_requires=[
        'Flask>=1.0.3',
        'flask-restplus>=0.12.1',
        'Flask-JWT>=0.3.2',
        'jsonschema>=3.0.1',
        'secure-smtplib==0.1.1',
        'Mako==1.1.1'
    ],
    extras_require=extras_require,
    tests_require=tests_require
)


import torch
import torch.nn as nn

#MODEL HELPERS

def int_func(X,Y,func):
    if func == 'exp':
        #interaction map
        I = torch.exp(torch.mm(X,Y.t()))
        #inverse of sum of columns for I and transpose I
        x_norm = torch.pow(torch.sum(I, dim=1),-1)
        y_norm = torch.pow(torch.sum(I.t(), dim=1),-1)
        #interaction maps with normalised columns
        I_x = I * x_norm[:,None]
        I_y = I.t() * y_norm[:,None]
        #contexts
        X_context = torch.mm(I_x,Y)
        Y_context = torch.mm(I_y,X)
    if func == 'tanh':
        #interaction map
        I = torch.tanh(torch.mm(X,Y.t()))
        #contexts
        X_context = torch.mm(I,Y)
        Y_context = torch.mm(I.t(),X)
    #concatenate
    catX = torch.cat((X,X_context))
    catY = torch.cat((Y,Y_context))
    return [catX, catY]

def int_func_map(X,Y,func):
    if func == 'exp':
        #interaction map
        I = torch.exp(torch.mm(X,Y.t()))
        #inverse of sum of columns for I and transpose I
        x_norm = torch.pow(torch.sum(I, dim=1),-1)
        y_norm = torch.pow(torch.sum(I.t(), dim=1),-1)
        #interaction maps with normalised columns
        I_x = I * x_norm[:,None]
        I_y = I.t() * y_norm[:,None]
        return [I_x, I_y]
    if func == 'tanh':
        #interaction map
        I = torch.tanh(torch.mm(X,Y.t()))
        return I

def get_activation_function(activation: str) -> nn.Module:
    """
    Gets an activation function module given the name of the activation.

    Supports:

    * :code:`ReLU`
    * :code:`LeakyReLU`
    * :code:`PReLU`
    * :code:`tanh`
    * :code:`SELU`
    * :code:`ELU`

    :param activation: The name of the activation function.
    :return: The activation function module.
    """
    if activation == 'ReLU':
        return nn.ReLU()
    elif activation == 'LeakyReLU':
        return nn.LeakyReLU(0.1)
    elif activation == 'PReLU':
        return nn.PReLU()
    elif activation == 'tanh':
        return nn.Tanh()
    elif activation == 'SELU':
        return nn.SELU()
    elif activation == 'ELU':
        return nn.ELU()
    else:
        raise ValueError(f'Activation "{activation}" not supported.')


from flask import Flask, render_template, request
import os
import re
import random

from random import randint, choice
from nltk.util import ngrams
from functools import reduce
from flask import jsonify
import math

#Taken from su27's stackexchange post https://stackoverflow.com/questions/18967441/add-a-prefix-to-all-flask-routes/36033627#36033627
class PrefixMiddleware(object):

    def __init__(self, app, prefix=''):
        self.app = app
        self.prefix = prefix

    def __call__(self, environ, start_response):

        if environ['PATH_INFO'].startswith(self.prefix):
            environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):]
            environ['SCRIPT_NAME'] = self.prefix
            return self.app(environ, start_response)
        else:
            start_response('404', [('Content-Type', 'text/plain')])
            return ["This url does not belong to the app.".encode()]

app = Flask(__name__)
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/thefaraway')

MAX_N = 5
SEED_MAX_SIZE = 20
MUTATION_CHANCE = 0.05
ADD_CHANCE = 0.03
LOSE_CHANCE = 0.03
POP_SIZE = 50
HINGE_P = .2
JOIN_P = .08
MIN_LENGTH = 10
MAX_LENGTH = 20

def read_text(txt_path=os.path.join(os.getcwd(), "texts", "WhateverItIsWhereverYouAre.txt")):
    with open(txt_path, 'r') as f:
        out = f.read()
    return out

def get_ngrams(txt, n=MAX_N):
    words = re.split(r"[ \t\n\r\f\v\.\!\?\",]+", txt.lower())
    return list_ngrams(words, n)

def list_ngrams(lst, n=MAX_N):
    the_ngrams = dict()
    the_ngrams[1] = lst
    for i in range(2, n + 1):
        the_ngrams[i] = set(ngrams(lst, i))

    return the_ngrams

txt = read_text()
ngs = get_ngrams(txt)

def n_sim(txta, n=MAX_N):
    a_ngrams = list_ngrams(txta)

    score = 0
    for i in range(2, n + 1):
        if len(a_ngrams[i]) >= 1:
            score += math.log1p(i) * len(a_ngrams[i].intersection(ngs[i])) / float(len(a_ngrams[i]))

    return score / (n - 1)

def weights(pop):
    return list(map(n_sim, pop))

def display_pop(pop):
    return "<br>".join(map(lambda l: " ".join(l), pop))

def mutate(t, mut_p = MUTATION_CHANCE, add_p = ADD_CHANCE, lose_p = LOSE_CHANCE, min_l = MIN_LENGTH, max_l = MAX_LENGTH):
    new = []
    words = ngs[1]
    for i, w in enumerate(t):
        r = random.random()
        if r <= mut_p:
            new.append(choice(words))
        elif r <= mut_p + add_p:
            new.append(w)
            new.append(choice(words))
        elif r <= mut_p + add_p + lose_p:
            pass
        else:
            new.append(w)
    if len(new) < min_l:
        for i in range(min_l - len(new)):
            new.append(choice(words))
    elif len(new) > max_l:
        new = new[:max_l]
    return new

def crossover(l, r):
    hingel = randint(0, len(l))
    hinger = randint(0, len(r))
    return l[:hingel] + r[hinger:]

def mutate_with_weight(x, mutp=MUTATION_CHANCE, addp=ADD_CHANCE, losep=LOSE_CHANCE):
    y = mutate(x, mut_p=mutp, add_p=addp, lose_p=losep)
    return [y, n_sim(y)]

def update(weighted_pop, mutp=MUTATION_CHANCE, addp=ADD_CHANCE, losep=LOSE_CHANCE):
    pop = [wp[0] for wp in weighted_pop]
    pop_weights = weights(pop)
    sample_l = random.choices(pop, pop_weights, k=POP_SIZE)
    sample_r = random.choices(pop, pop_weights, k=POP_SIZE)
    new_pop = []
    for i in range(POP_SIZE):
        new_pop.append(crossover(sample_l[i], sample_r[i]))
    return list(map(lambda x: mutate_with_weight(x, mutp, addp, losep), new_pop))

@app.route('/start/')
def genetictext():
    def sim_fit(t):
        return n_sim(t, ngs)

    def text_seed(max_len=SEED_MAX_SIZE):
        n = randint(MIN_LENGTH, SEED_MAX_SIZE)
        s = reduce(lambda a, b: a + [choice(ngs[1])], range(n + 1), [])
        return [s, n_sim(s)]

    pop = list(map(lambda x: text_seed(), range(POP_SIZE)))

    return jsonify(pop)

@app.route('/update/', methods=['POST'])
def a_generation():
    dat = request.get_json()
    new_pop = update(dat['population'], mutp=float(dat["mutp"]), addp=float(dat["addp"]), losep=float(dat["losep"]))
    return jsonify(new_pop)

@app.route('/')
def start_here():
    return render_template('environ.html')

if __name__ == '__main__':
    app.run(host='0.0.0.0')


## writing text to file

text = "this will be written to the file"

saveFile = open("textfile.txt", "w")
saveFile.write(text)
saveFile.close()

##text to be added to the file
appendMe = "New info for file"

##appending more text to the file
appendFile = open("textfile.txt", "a")
appendFile.write('\n')
appendFile.write(appendMe)
appendFile.close()

##reading the file
readMe = open("textfile.txt", "r").readlines()
print(readMe)


from django.conf import settings
from django.conf.urls import url, include, patterns
from django.contrib import admin
from rest_framework.urlpatterns import format_suffix_patterns

from dashboard import api
from dashboard.views import download_database, home

urlpatterns = [
    url(r'^$', home),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^jet/', include('jet.urls', 'jet')),
    url(r'^api/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^api/changelog/$', api.ChangeLog.as_view()),
    url(r'^api/changelog/(?P<pk>[0-9]+0)/$', api.ChangeLogDetail.as_view()),
    url(r'^api/db_changelog/$', api.DataBaseChangeLog.as_view()),
    url(r'^api/db_changelog/(?P<pk>[0-9]+0)/$', api.DataBaseChangeLogDetail.as_view()),
    url(r'^api/bus_search/$', api.BusDepartures.as_view()),
    url(r'^api/canceled_events/$', api.CanceledEvents.as_view()),
    url(r'^download_database/(?P<filename>[^/]+)/$', download_database, name='db-download'),
]

test = format_suffix_patterns(urlpatterns)

urlpatterns += patterns('',
    (r'^assets/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}))


from easygraph.functions.path import *

__all__ = [
    'closeness_centrality',
]

def closeness_centrality(G, weight=None):
    '''Compute closeness centrality for nodes.

    .. math::

        C_{WF}(u) = \frac{n-1}{N-1} \frac{n - 1}{\sum_{v=1}^{n-1} d(v, u)},

    Notice that the closeness distance function computes the
    outcoming distance to `u` for directed graphs. To use
    incoming distance, act on `G.reverse()`.

    Parameters
    ----------
    G : graph
      A easygraph graph

    weight : None or string, optional (default=None)
      If None, all edge weights are considered equal.
      Otherwise holds the name of the edge attribute used as weight.

    Returns
    -------
    nodes : dictionary
      Dictionary of nodes with closeness centrality as the value.

    '''
    result_dict = dict()
    nodes = G.nodes
    length = len(nodes)
    import functools
    if weight is not None:
        path_length = functools.partial(single_source_dijkstra, weight=weight)
    else:
        path_length = functools.partial(single_source_bfs)
    for node in nodes:
        x = path_length(G, node)
        dist = sum(x.values())
        cnt = len(x)
        if dist  == 0:
            result_dict[node] = 0
        else:
            result_dict[node] = (cnt-1)*(cnt-1)/(dist*(length-1))
    return result_dict


# 9. 回文数
#
# 判断一个整数是否是回文数。回文数是指正序（从左向右）和倒序（从右向左）读都是一样的整数。
#
# 示例 1:
#
# 输入: 121
# 输出: true
#
# 示例 2:
#
# 输入: -121
# 输出: false
# 解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
#
# 示例 3:
#
# 输入: 10
# 输出: false
# 解释: 从右向左读, 为 01 。因此它不是一个回文数。
#
# 进阶:
#
# 你能不将整数转为字符串来解决这个问题吗？
#
# 来源：力扣（LeetCode）
# 链接：https://leetcode-cn.com/problems/palindrome-number
# 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x < 0:
            return False
        rev, n = 0, x
        while n > 0:
            rev, n = rev * 10 + n % 10, n // 10
        return rev == x

sol = Solution()
print(sol.isPalindrome(121))
print(sol.isPalindrome(-121))
print(sol.isPalindrome(10))


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

import sys
import stdio

n = int(sys.argv[1])

power = 1
i = 0

while i <= n:
    stdio.writeln(str(i) + ' ' + str(power))
    power *= 2
    i += 1


import json
import datetime as dt
from datetime import timedelta, timezone
from dateutil.relativedelta import relativedelta, FR

import numpy as np
import pandas as pd
from bokeh.plotting import figure
from bokeh.embed import components
from bokeh.models import AjaxDataSource, ColumnDataSource, HoverTool, CrosshairTool, formatters, CustomJS, DateRangeSlider, Plot,  Grid, LinearAxis, MultiLine
from bokeh.layouts import column

from market_dicts import title_dict, price_types

class BPBuilder():
	def __init__(self, tables, ohlc_tables, db, minutes=30):
		self.tables = tables
		self.minutes = minutes
		self.ohlc_tables = ohlc_tables
		self.db = db

		self.TOOLS = "pan,box_zoom,reset"

	def build_returns_components(self):
		# xs, ys, color, label = [], [], [], []
		colors = [
			'#2f7ed8', '#0d233a', '#8bbc21', '#910000', '#1aadce',\
			'#492970', '#f28f43', '#77a1e5', '#c42525', '#a6c96a'
		]

		plot = figure(
			plot_height=500,
			plot_width=1250,
			x_axis_type='datetime',
			toolbar_location="above",
			tools=self.TOOLS,
			title='Daily Returns Going Back 3 Years'
		)
		for i, table in enumerate(self.ohlc_tables):
			cp = table.__tablename__
			price_type = price_types[cp[:6]]
			'''
			direct markets:
				return = t1 quote / t0 quote

			indirect markets:
				return  = t0 quote / t1 quote

			to make this more accurate (account for spread)

			direct markets:
				return = t1 ask / t0 buy

			indirect markets:
				return = t0 buy / t1 ask
			'''
			if price_type == 'BID':
				period = -1
			elif price_type == 'ASK':
				period = 1
			cols = ['timestamp', 'close']
			df = pd.read_sql_table(cp, self.db.engine, columns=cols)\
				.set_index('timestamp', drop=True)
			three_years_ago = dt.datetime.now() - timedelta(days=365*3)
			df = df[df.index >= three_years_ago] # include range selector
			df.drop_duplicates(inplace=True)
			df = df.pct_change(period)
			df['close'] = df['close']*100
			df.dropna(inplace=True)
			x = (
				np.array(
					df.index.values.tolist()
				) / 1000000
			).astype(int).tolist()
			y = df.close.values.tolist()
			plot.line(
				x,
				y,
				line_color=colors[i],
				legend_label=cp[:6].upper()
			)
		plot.legend.location = "top_left"
		plot.legend.click_policy="hide"
		plot.title.text_color = "#f8f9fa"
		plot.background_fill_color = '#f8f9fa'
		plot.border_fill_color = "#343a40"
		plot.xaxis.axis_label_text_color = "#868e96"
		plot.yaxis.axis_label_text_color = "#868e96"
		plot.xaxis.major_label_text_color = "#868e96"
		plot.yaxis.major_label_text_color = "#868e96"
		plot.xgrid.visible=False
		return components(plot)

	def build_ohlc_components(self):
		plot_dict = {}
		for table in self.ohlc_tables:
			tname = table.__tablename__
			cp = tname[:6]

			df = pd.read_sql_table(
				tname,
				self.db.engine,
				columns=['timestamp','open', 'high','low','close']
			)
			df.drop_duplicates(inplace=True)
			r = pd.date_range(
				start=df.timestamp.min(), end=df.timestamp.max()
			)
			df = df.set_index('timestamp').reindex(r).fillna(np.NaN)
			df.index = df.index.rename('timestamp')

			df['open_inc'],df['high_inc'],df['low_inc'],df['close_inc']=\
			df['open'],    df['high'],    df['low'],    df['close']
			df['open_dec'],df['high_dec'],df['low_dec'],df['close_dec']=\
			df['open'],    df['high'],    df['low'],    df['close']

			inc = df.close > df.open
			dec = df.open > df.close

			df['open_dec'][inc]=None
			df['high_dec'][inc]=None
			df['low_dec'][inc]=None
			df['close_dec'][inc]=None

			df['open_inc'][dec]=None
			df['high_inc'][dec]=None
			df['low_inc'][dec]=None
			df['close_inc'][dec]=None

			source = ColumnDataSource(data=df)
			reference = ColumnDataSource(data=df)

			plot = figure(
				x_axis_type="datetime",
				plot_height=250,
				plot_width=1400,
				toolbar_location="above",
				tools=self.TOOLS,
				# these didn't do much
				# sizing_mode='stretch_width',
				# sizing_mode='stretch_height',
				# sizing_mode='stretch_both',
				# sizing_mode='scale_width',
				# sizing_mode='scale_height',
				# sizing_mode='scale_both',
				# sizing_mode='fixed',
				title=title_dict[cp]
			)
			seg = plot.segment(
				x0='timestamp',
				y0='high',
				x1='timestamp',
				y1='low',
				color='black',
				source=source
			)
			inc_bar = plot.vbar(
				x='timestamp',
				width=20*60*60*1000,
				top='close_inc',
				bottom='open_inc',
				fill_color="#D5E1DD",
				line_color="black",
				source=source
			)
			dec_bar = plot.vbar(
				x='timestamp',
				width=20*60*60*1000,
				top='open_dec',
				bottom='close_dec',
				fill_color="#F2583E",
				line_color="black",
				source=source
			)

			date_range_slider = DateRangeSlider(
				# width=400,
				align='center',
				margin=(25,50,5,50),
				# format="%x",
				format="%B %e, %Y",
				value=(
					df.index.min(),
					df.index.max()
				),
				start=df.index.min(),
				end=df.index.max(),
				background='#343a40',
				bar_color='#f8f9fa',
				step=86400000
			)

			args = dict(
				source=source,
				reference=reference
			)
			code =\
			"""
			const data = source.data;
			const data_ref = reference.data;

			const from_date = this.value[0];
			const to_date = this.value[1];

			var from_pos = data_ref['timestamp'].indexOf(to_date);
			var to_pos = data_ref['timestamp'].indexOf(from_date);

			data['timestamp'] = data_ref['timestamp'].slice(to_pos,from_pos);

			data['high'] = data_ref['high'].slice(to_pos,from_pos);
			data['low'] = data_ref['low'].slice(to_pos,from_pos);

			data['open_inc'] = data_ref['open_inc'].slice(to_pos,from_pos);
			data['high_inc'] = data_ref['high_inc'].slice(to_pos,from_pos);
			data['low_inc'] = data_ref['low_inc'].slice(to_pos,from_pos);
			data['close_inc'] = data_ref['close_inc'].slice(to_pos,from_pos);

			data['open_dec'] = data_ref['open_dec'].slice(to_pos,from_pos);
			data['high_dec'] = data_ref['high_dec'].slice(to_pos,from_pos);
			data['low_dec'] = data_ref['low_dec'].slice(to_pos,from_pos);
			data['close_dec'] = data_ref['close_dec'].slice(to_pos,from_pos);

			source.change.emit();
			"""
			callback = CustomJS(args=args, code=code)
			date_range_slider.js_on_change("value", callback)

			# NaN's sometimes show up in hover tool
			hover_tool = HoverTool(
				tooltips=[
				# https://docs.bokeh.org/en/latest/docs/reference/models/formatters.html#bokeh.models.formatters.DatetimeTickFormatter
					('timestamp', '@{timestamp}{%x}'),
					('open', '@{open}{%0.4f}'),
					('high', '@{high}{%0.4f}'),
					('low', '@{low}{%0.4f}'),
					('close', '@{close}{%0.4f}')
				],
				formatters={
					'@{timestamp}': 'datetime',
					'@{open}': 'printf',
					'@{high}': 'printf',
					'@{low}': 'printf',
					'@{close}': 'printf'

				},
				renderers=[inc_bar,dec_bar]
			)
			plot.add_tools(hover_tool)
			plot.background_fill_color = '#f8f9fa'
			plot.border_fill_color = "#343a40"
			plot.title.text_color = "#f8f9fa"
			plot.xaxis.axis_label_text_color = "#f8f9fa"
			plot.yaxis.axis_label_text_color = "#f8f9fa"
			plot.xaxis.major_label_text_color = "#f8f9fa"
			plot.yaxis.major_label_text_color = "#f8f9fa"
			plot.xgrid.visible = False
			plot.xaxis.formatter = formatters.DatetimeTickFormatter(
				days="%m/%d/%Y",
				months = "%m/%d/%Y"
			)
			# plot.background_fill_color = '#868e96'
			plot_dict[cp] = column(plot, date_range_slider)

		return components(plot_dict)

	def build_components(self, is_closed):
		plot_dict = {}
		for table in self.tables:
			cp = table.__tablename__
			url =f"http://localhost:5000/data/{cp}/{self.minutes}"
			if is_closed:
				# don't really need to use ajax
				source = AjaxDataSource(
					data_url=url,
					polling_interval=1000*60*60*48, # 2 days
					mode='replace'
				)
			else:
				source = AjaxDataSource(
					data_url=url,
					polling_interval=1000,
					mode='replace'
				)
			plot = figure(
				plot_height=150,
				plot_width=1200,
				x_axis_type='datetime',
				toolbar_location="above",
				tools=self.TOOLS,
			)

			plot.xaxis.formatter = formatters.DatetimeTickFormatter(
				days="%m/%d",
				hours = "%l:%M %P",
				hourmin = "%l:%M %P",
				minutes="%l:%M %P",
				minsec="%l:%M:%S %P",
				seconds="%l:%M:%S %P",
				microseconds="%l:%M:%S:%f %P",
				milliseconds="%l:%M:%S:%f %P"

			)
			# plot.xaxis.axis_label = "UTC"

			line = plot.line(
				'timestamp',
				'rate',
				source=source
			)
			plot.background_fill_color = '#f8f9fa'
			plot.border_fill_color = "#343a40"
			plot.xaxis.axis_label_text_color = "#868e96"
			plot.yaxis.axis_label_text_color = "#868e96"
			plot.xaxis.major_label_text_color = "#868e96"
			plot.yaxis.major_label_text_color = "#868e96"
			plot.xgrid.visible=False

			hover_tool = HoverTool(
				tooltips=[
				# https://docs.bokeh.org/en/latest/docs/reference/models/formatters.html#bokeh.models.formatters.DatetimeTickFormatter
					('timestamp', '@{timestamp}{%d %b %Y %l:%M:%S:%N %P}'),
					('rate', '@{rate}{%0.4f}'),
				],
				formatters={
					'@{timestamp}': 'datetime',
					'@{rate}': 'printf'
				}
			)
			plot.add_tools(hover_tool)
			callback = CustomJS(
				args={'line':line, 'source':source, 'cp':cp}, code="""
				var rates = source.data.rate;
				var first_val = rates[0];
				var last_val = rates[rates.length-1];
				var delta = Number.parseFloat(Math.abs(last_val-first_val)).toFixed(5);
				var increasing = first_val < last_val;
				if (increasing) {
					line.glyph.line_color = 'green';
				} else {
					line.glyph.line_color = 'red';
				}
						var card_class_dict = {
						true: {
						  "card_class":"card-text text-center font-weight-lighter text-success",
						  "new_color": "green",
						  "arrow": "▲"
						},
						false: {
						  "card_class":"card-text text-center font-weight-lighter text-danger",
						  "new_color": "red",
						  "arrow": "▼"
						  }
						}
						var formats = card_class_dict[increasing];

						$('#delta_'+cp)
						.removeClass()
						.addClass(
							  formats['card_class']
						)
						.html(
							formats["arrow"].concat(delta)
						);
						$('#current_'+cp).html(last_val);

				"""
			)
			source.js_on_change('change:data', callback)
			plot_dict[cp] = plot
		return components(plot_dict)