import torch.optim as optim
import torchvision.transforms
import torch
from ltr.dataset import Lasot, Got10k, TrackingNet, MSCOCOSeq
from ltr.data import sampler, LTRLoader, processing_fcot
from ltr.models.tracking import fcotnet
import ltr.models.loss as ltr_losses
from ltr.models.loss.target_regression import REGLoss
from ltr import actors
from ltr.trainers import LTRFcotTrainer
import ltr.data.transforms as dltransforms
from ltr import MultiGPU
import collections

# Address the issue: "RuntimeError: received 0 items of ancdata"
import resource
rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)
resource.setrlimit(resource.RLIMIT_NOFILE, (2048, rlimit[1]))
torch.multiprocessing.set_sharing_strategy('file_system')

def run(settings):
    settings.description = 'Default train settings for FCOT with ResNet50 as backbone.'
    settings.multi_gpu = True
    settings.print_interval = 1
    settings.normalize_mean = [0.485, 0.456, 0.406]
    settings.normalize_std = [0.229, 0.224, 0.225]
    settings.search_area_factor = 5.0
    settings.output_sigma_factor = 1/4
    settings.clf_target_filter_sz = 4
    settings.reg_target_filter_sz = 3
    settings.feature_sz = 18
    settings.output_sz = settings.feature_sz * 16
    settings.center_jitter_factor = {'train': 3, 'test': 4.5}
    settings.scale_jitter_factor = {'train': 0.25, 'test': 0.5}
    settings.hinge_threshold = 0.05
    settings.logging_file = 'fcot_log.txt'

    # Train datasets
    lasot_train = Lasot(settings.env.lasot_dir, split='train')
    got10k_train = Got10k(settings.env.got10k_dir, split='vottrain')
    trackingnet_train = TrackingNet(settings.env.trackingnet_dir, set_ids=list(range(4)))
    coco_train = MSCOCOSeq(settings.env.coco_dir)

    # Validation datasets
    got10k_val = Got10k(settings.env.got10k_dir, split='votval')

    # Data transform
    transform_joint = dltransforms.ToGrayscale(probability=0.05)

    transform_train = torchvision.transforms.Compose([dltransforms.ToTensorAndJitter(0.2),
                                                      torchvision.transforms.Normalize(mean=settings.normalize_mean, std=settings.normalize_std)])

    transform_val = torchvision.transforms.Compose([torchvision.transforms.ToTensor(),
                                                    torchvision.transforms.Normalize(mean=settings.normalize_mean, std=settings.normalize_std)])

    # The tracking pairs processing module
    output_sigma = settings.output_sigma_factor / settings.search_area_factor
    proposal_params = {'min_iou': 0.1, 'boxes_per_frame': 8, 'sigma_factor': [0.01, 0.05, 0.1, 0.2, 0.3]}
    label_params = {'feature_sz': settings.feature_sz, 'sigma_factor': output_sigma, 'kernel_sz': settings.clf_target_filter_sz}
    data_processing_train = processing_fcot.AnchorFreeProcessing(search_area_factor=settings.search_area_factor,
                                                                 output_sz=settings.output_sz,
                                                                 center_jitter_factor=settings.center_jitter_factor,
                                                                 scale_jitter_factor=settings.scale_jitter_factor,
                                                                 mode='sequence',
                                                                 output_spatial_scale=72 / 288.,
                                                                 proposal_params=proposal_params,
                                                                 label_function_params=label_params,
                                                                 transform=transform_train,
                                                                 joint_transform=transform_joint)

    data_processing_val = processing_fcot.AnchorFreeProcessing(search_area_factor=settings.search_area_factor,
                                                               output_sz=settings.output_sz,
                                                               center_jitter_factor=settings.center_jitter_factor,
                                                               scale_jitter_factor=settings.scale_jitter_factor,
                                                               mode='sequence',
                                                               output_spatial_scale=72 / 288.,
                                                               proposal_params=proposal_params,
                                                               label_function_params=label_params,
                                                               transform=transform_val,
                                                               joint_transform=transform_joint)

    # Train sampler and loader
    dataset_train = sampler.FCOTSampler([lasot_train, got10k_train, trackingnet_train, coco_train], [settings.lasot_rate,1,1,1],
                                        samples_per_epoch=settings.samples_per_epoch, max_gap=30, num_test_frames=3, num_train_frames=3,
                                        processing=data_processing_train)

    loader_train = LTRLoader('train', dataset_train, training=True, batch_size=settings.batch_size,
                             num_workers=settings.num_workers, shuffle=True, drop_last=True, stack_dim=1)

    # Validation samplers and loaders
    dataset_val = sampler.FCOTSampler([got10k_val], [1], samples_per_epoch=5000, max_gap=30, num_test_frames=3,
                                      num_train_frames=3, processing=data_processing_val)

    loader_val = LTRLoader('val', dataset_val, training=False, batch_size=settings.batch_size, epoch_interval=5,
                           num_workers=settings.num_workers, shuffle=False, drop_last=True, stack_dim=1)

    # Create network
    net = fcotnet.fcotnet(clf_filter_size=settings.clf_target_filter_sz, reg_filter_size=settings.reg_target_filter_sz,
                          backbone_pretrained=True, optim_iter=5, norm_scale_coef=settings.norm_scale_coef,
                          clf_feat_norm=True, clf_feat_blocks=0, final_conv=True, out_feature_dim=512,
                          optim_init_step=0.9, optim_init_reg=0.1, init_gauss_sigma=output_sigma * settings.feature_sz,
                          num_dist_bins=100, bin_displacement=0.1, mask_init_factor=3.0, target_mask_act='sigmoid',
                          score_act='relu', train_reg_optimizer=settings.train_reg_optimizer,
                          train_cls_72_and_reg_init=settings.train_cls_72_and_reg_init, train_cls_18=settings.train_cls_18)

    # Load dimp-model as initial weights
    device = torch.device('cuda:{}'.format(settings.devices_id[0]) if torch.cuda.is_available() else 'cpu')
    if settings.use_pretrained_dimp:
        assert settings.pretrained_dimp50 is not None
        dimp50 = torch.load(settings.pretrained_dimp50, map_location=device)
        state_dict = collections.OrderedDict()
        for key, v in dimp50['net'].items():
            if key.split('.')[0] == 'feature_extractor':
                state_dict['.'.join(key.split('.')[1:])] = v

        net.feature_extractor.load_state_dict(state_dict)

        state_dict = collections.OrderedDict()
        for key, v in dimp50['net'].items():
            if key.split('.')[0] == 'classifier':
                state_dict['.'.join(key.split('.')[1:])] = v
        net.classifier_18.load_state_dict(state_dict)
        print("loading backbone and Classifier modules from DiMP50 done.")

    # Load fcot-model trained in the previous stage
    if settings.load_model:
        assert settings.fcot_model is not None
        load_dict = torch.load(settings.fcot_model)
        fcot_dict = net.state_dict()
        load_fcotnet_dict = {k: v for k, v in load_dict['net'].items() if k in fcot_dict}
        fcot_dict.update(load_fcotnet_dict)
        net.load_state_dict(fcot_dict)
        print("loading FCOT model done.")

    # Wrap the network for multi GPU training
    if settings.multi_gpu:
        net = MultiGPU(net, device_ids=settings.devices_id, dim=1).to(device)

    # Loss for cls_72, cls_18 and regression
    objective = {'test_clf_72': ltr_losses.LBHinge(threshold=settings.hinge_threshold),
                 'test_clf_18': ltr_losses.LBHinge(threshold=settings.hinge_threshold),
                 'reg_72': REGLoss(dim=4)
                 }

    # Create actor and adam-optimizer
    if settings.train_cls_72_and_reg_init and settings.train_cls_18:
        ### train regression branch and clssification branches jointly, except for regression optimizer (TODO: fix)
        print("train cls_72, cls_18 and reg_init jointly...")
        loss_weight = {'test_clf_72': 100, 'test_init_clf_72': 100, 'test_iter_clf_72': 400,
                       'test_clf_18': 100, 'test_init_clf_18': 100, 'test_iter_clf_18': 400,
                       'reg_72': 1}
        actor = actors.FcotActor(net=net, objective=objective, loss_weight=loss_weight, device=device)
        optimizer = optim.Adam([{'params': actor.net.classifier_72.filter_initializer.parameters(), 'lr': 5e-5},
                                {'params': actor.net.classifier_72.filter_optimizer.parameters(), 'lr': 5e-4},
                                {'params': actor.net.classifier_72.feature_extractor.parameters(), 'lr': 5e-5},
                                {'params': actor.net.classifier_18.filter_initializer.parameters(), 'lr': 5e-5},
                                {'params': actor.net.classifier_18.filter_optimizer.parameters(), 'lr': 5e-4},
                                {'params': actor.net.classifier_18.feature_extractor.parameters(), 'lr': 5e-5},
                                {'params': actor.net.regressor_72.parameters()},
                                {'params': actor.net.pyramid_first_conv.parameters()},
                                {'params': actor.net.pyramid_36.parameters()},
                                {'params': actor.net.pyramid_72.parameters()},
                                {'params': actor.net.feature_extractor.parameters(), 'lr': 2e-5}],
                                lr=2e-4)
        lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[35, 46, 60], gamma=0.2)
    elif settings.train_cls_72_and_reg_init:
        # Setting of the first training stage: train backbone, cls_72 and regression (except for regression optimizer) branch.
        print("train cls_72 and reg_init...")
        loss_weight = {'test_clf_72': 100, 'test_init_clf_72': 10, 'test_iter_clf_72': 400,
                       'test_clf_18': 0, 'test_init_clf_18': 0, 'test_iter_clf_18': 0,
                       'reg_72': 0.3}
        actor = actors.FcotCls72AndRegInitActor(net=net, objective=objective, loss_weight=loss_weight, device=device)
        optimizer = optim.Adam([{'params': actor.net.classifier_72.filter_initializer.parameters(), 'lr': 5e-5},
                                {'params': actor.net.classifier_72.filter_optimizer.parameters(), 'lr': 5e-4},
                                {'params': actor.net.classifier_72.feature_extractor.parameters(), 'lr': 5e-5},
                                {'params': actor.net.regressor_72.parameters()},
                                {'params': actor.net.pyramid_first_conv.parameters()},
                                {'params': actor.net.pyramid_36.parameters()},
                                {'params': actor.net.pyramid_72.parameters()},
                                {'params': actor.net.feature_extractor.parameters(), 'lr': 2e-5}],
                                lr=2e-4)
        lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[35, 45, 69], gamma=0.2)
    elif settings.train_reg_optimizer:
        # Setting of the second training stage: train regression optimizer.
        print("train regression optimizer...")
        loss_weight = {'test_reg_72': 1, 'test_init_reg_72': 0, 'test_iter_reg_72': 1}
        actor = actors.FcotOnlineRegressionActor(net=net, objective=objective, loss_weight=loss_weight, device=device)
        optimizer = optim.Adam([{'params': actor.net.regressor_72.filter_optimizer.parameters()}],
                               lr=5e-4)
        lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[2], gamma=0.2)
    elif settings.train_cls_18:
        print("train cls_18...")
        # Setting of the third training stage: train cls_18 branch.
        loss_weight = {'test_clf_18': 100, 'test_init_clf_18': 100, 'test_iter_clf_18': 400}
        actor = actors.FcotCls18Actor(net=net, objective=objective, loss_weight=loss_weight, device=device)
        optimizer = optim.Adam([{'params': actor.net.classifier_18.filter_initializer.parameters(), 'lr': 5e-5},
                                {'params': actor.net.classifier_18.filter_optimizer.parameters(), 'lr': 5e-4},
                                {'params': actor.net.classifier_18.feature_extractor.parameters(), 'lr': 5e-5}],
                                lr=2e-4)
        lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[25], gamma=0.2)
    else:
        # TODO: train jointly
        raise Exception("Please run training in correct way.")

    trainer = LTRFcotTrainer(actor, [loader_train, loader_val], optimizer, settings, device, lr_scheduler,
                             logging_file=settings.logging_file)

    trainer.train(settings.total_epochs, load_latest=True, fail_safe=True)


#-------------------------------------------------------------------------------
# Name:        sumSqFirmsPropsOfTotalFirmMetricByMarketForHHI
#
# Purpose:     sum the square of firms' proportion of total market metric by
#              market to calculate markets' HHIs
#
# Author:      Matthew Sutton
#
# Created:     23/12/2018
#-------------------------------------------------------------------------------
import arcpy

#get and set squared firms' proportion of total firm metric table
sqFirmsPropOfTotalFirmMetricTable = arcpy.GetParameterAsText(0)

#get path from squared firms' proportion of total firm metric table and create
#HHI table path in its geodatabase
sqFirmsPropOfTotalFirmMetricTablePath = [sqFirmsPropOfTotalFirmMetricTable]
wantedPathList = sqFirmsPropOfTotalFirmMetricTablePath[0].split('\\')
wantedPathList.pop(-1)
sqFirmsPropOfTotalFirmMetricTablePath = wantedPathList[0]
index = 1
while index < len(wantedPathList):
    sqFirmsPropOfTotalFirmMetricTablePath = sqFirmsPropOfTotalFirmMetricTablePath + "\\" + wantedPathList[index]
    index = index + 1
HHITableOutput = str(sqFirmsPropOfTotalFirmMetricTablePath) + "\\HHITable"

#delete table if it already exists
if arcpy.Exists(HHITableOutput):
    arcpy.Delete_management(HHITableOutput)

#create HHITable from sqFirmsPropOfTotalFirmMetricTable
arcpy.TableToTable_conversion   (sqFirmsPropOfTotalFirmMetricTable,
                                sqFirmsPropOfTotalFirmMetricTablePath,
                                "HHITable")

#add empty HHI field
arcpy.AddField_management   (HHITableOutput,
                            "HHI",
                            "FLOAT",
                            5,
                            4)

#calculate sum of sqFirmPropTotalMetric by market to create HHI
arcpy.Statistics_analysis   (sqFirmsPropOfTotalFirmMetricTable,
                            HHITableOutput,
                            [["sqFirmPropTotalMetric", "SUM"]],
                            ["market"])

#rename summed sqFirmPropTotalMetric field to HHI
arcpy.AlterField_management (HHITableOutput,
                            "SUM_sqFirmPropTotalMetric",
                            "HHI",
                            "HHI")

#create output node for model builder
arcpy.SetParameterAsText(1, HHITableOutput)


import logging
import os

from avocado.core import exceptions
from avocado.utils import process

LOG = logging.getLogger('avocado.' + __name__)
VIRSH_ARGS = {'debug': True, 'ignore_status': False}

def pre_save_setup(vm):
    """
    Setup vm before save:
    Start ping process and get uptime since when on vm

    :param vm: vm instance
    :return: a tuple of pid of ping and uptime since when
    """
    session = vm.wait_for_login()
    upsince = session.cmd_output('uptime --since').strip()
    LOG.debug(f'VM has been up since {upsince}')
    ping_cmd = 'ping 127.0.0.1'
    # This session shouldn't be closed or it will kill ping
    session.sendline(ping_cmd + '&')
    check_session = vm.wait_for_login()
    pid_ping = check_session.cmd_output('pidof ping').strip().split()[-1]
    check_session.close()
    LOG.debug(f'Pid of ping: {pid_ping}')

    return pid_ping, upsince

def post_save_check(vm, pid_ping, upsince):
    """
    Check vm status after save-restore:
    Whether ping is still running, uptime since when is the same as before
    save-restore.

    :param vm: vm instance
    :param pid_ping: pid of ping
    :param upsince: uptime since when
    """
    session = vm.wait_for_login()
    upsince_restore = session.cmd_output('uptime --since').strip()
    LOG.debug(f'VM has been up (after restore) since {upsince_restore}')
    LOG.debug(session.cmd_output(f'pidof ping'))
    proc_info = session.cmd_output(f'ps -p {pid_ping} -o command').strip()
    LOG.debug(proc_info)
    LOG.debug(session.cmd_output(f'ps -ef|grep ping'))
    session.close()

    if upsince_restore != upsince:
        raise exceptions.TestWarn(f'Uptime since {upsince_restore} is '
                                  f'incorrect, should be {upsince}')
    ping_cmd = 'ping 127.0.0.1'
    if ping_cmd not in proc_info:
        raise exceptions.TestFail('Cannot find running ping command '
                                  'after save-restore.')

def check_ownership(path, uid, gid):
    """
    Check whether ownership of path meets expectation

    :param path: path to check
    :param uid: expected uid
    :param gid: expected gid
    :param test: test instance
    """
    process.run(f'ls -lZ {path}', shell=True)
    stat = os.stat(path)
    LOG.debug(f'Path stat info: {stat}')
    if (stat.st_uid, stat.st_gid) != (uid, gid):
        raise exceptions.TestFail(f'File ownership not correct, '
                                  f'should be {uid, gid}, '
                                  f'not {stat.st_uid, stat.st_gid}')


# -*- coding: utf-8 -*-

import os
from robot.libraries.BuiltIn import BuiltIn
from robot.libraries.BuiltIn import RobotNotRunningError
from robot.api import logger
from .keywordgroup import KeywordGroup

class _LoggingKeywords(KeywordGroup):
    LOG_LEVEL_DEBUG = ['DEBUG']
    LOG_LEVEL_INFO = ['DEBUG', 'INFO']
    LOG_LEVEL_WARN = ['DEBUG', 'INFO', 'WARN']

    @property
    def _log_level(self):
        try:
            level = BuiltIn().get_variable_value("${APPIUM_LOG_LEVEL}", default='DEBUG')
        except RobotNotRunningError:
            level = 'DEBUG'
        return level

    def _debug(self, message):
        if self._log_level in self.LOG_LEVEL_DEBUG:
            logger.debug(message)

    def _info(self, message):
        if self._log_level in self.LOG_LEVEL_INFO:
            logger.info(message)

    def _warn(self, message):
        if self._log_level in self.LOG_LEVEL_WARN:
            logger.warn(message)

    def _html(self, message):
        logger.info(message, True, False)

    def _get_log_dir(self):
        variables = BuiltIn().get_variables()
        logfile = variables['${LOG FILE}']
        if logfile != 'NONE':
            return os.path.dirname(logfile)
        return variables['${OUTPUTDIR}']

    def _log(self, message, level='INFO'):
        level = level.upper()
        if (level == 'INFO'):
            self._info(message)
        elif (level == 'DEBUG'):
            self._debug(message)
        elif (level == 'WARN'):
            self._warn(message)
        elif (level == 'HTML'):
            self._html(message)

    def _log_list(self, items, what='item'):
        msg = ['Altogether %d %s%s.' % (len(items), what, ['s', ''][len(items) == 1])]
        for index, item in enumerate(items):
            msg.append('%d: %s' % (index+1, item))
        self._info('\n'.join(msg))
        return items


'''
    排序算法
        冒泡
        重复走访需要排序的数据,依次对比每两个相邻的元素,如果次序错误则交换
        重复进行直到没有相邻数据需要交换为止,此时完成排序
        每次对比n-1-->n-2-->n-3-->n-4....
        总共对比(n-1)!次
'''
import time
import random
from day15.search import cal_time

def b_s(value):
    for i in range(len(value) - 1):
        flag = False
        for j in range(len(value) - 1 - i):
            if value[j] > value[j + 1]:
                # 数据交换的写法
                value[j], value[j + 1] = value[j + 1], value[j]
                # 若在一次遍历中,数据发生交换则为Ture
                flag = True
        # 当走访一边后发现都没有进行数据交换,则不在遍历数据进行走访交换
        if flag is False:
            break

if __name__ == '__main__':
    heigh = [random.randint(100, 155) for i in range(10)]
    print(heigh)
    b_s(heigh)
    print(heigh)


import argparse
import requests
import supply
import logging

shouldPostToLocalhost = True

logging.basicConfig(
    filename='crontab-GPIO.log', format='%(levelname)s:%(message)s',
    level=logging.INFO)

if not supply.isTest:
    import RPi.GPIO as GPIO
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    for name in supply.outPins:
        pin = supply.outPins[name]['pin']
        if pin is None:
            continue
        if not (2 <= pin <= 27):
            continue
        GPIO.setup(pin, GPIO.OUT)

def set_pin(name, state):
    if state == 'switch':
        supply.outPins[name]['state'] ^= 1
    else:
        supply.outPins[name]['state'] = 1 if state in ('on', 'yes', 1) else 0

    pin = supply.outPins[name]['pin']
    if pin is None or supply.isTest:
        return
    GPIO.output(pin, GPIO.LOW if supply.outPins[name]['state'] else GPIO.HIGH)
    logging.info("GPIO pin {0} set {1} by crontab".format(
        name, 'on' if state in ('on', 'yes', 1) else 'off'))
    dictToSend = {"pin": name, "state": 1 if state in ('on', 'yes', 1) else 0,
                  "how": "Crontab-switched", "skipset": 1}
    if shouldPostToLocalhost:
        try:
            requests.post('http://localhost:80', json=dictToSend)
        except:  # noqa
            pass

def get_pins():
    if supply.isTest:
        return
    for name in supply.outPins:
        pin = supply.outPins[name]['pin']
        if pin is None:
            continue
        state = int(GPIO.input(pin) == GPIO.HIGH)
        supply.outPins[name]['state'] = int(not state)
    for name in supply.inPinsFromRaspberry:
        pin = supply.inPinsFromRaspberry[name]['pin']
        state = int(GPIO.input(pin) == GPIO.HIGH)
        supply.outPins[name]['state'] = int(state)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description='Switches GPIO pins on and off.')

    parser.add_argument(
        '--name', action='append', nargs=2, metavar=('name', 'state'),
        help="a pair of pin name and state, e.g. "
        "'light on', 'light off' or 'light switch'")

    args = parser.parse_args()
    # args = parser.parse_args("--name light on --name stream off".split())
    # print(args.name)
    # >> [['light', 'on'], ['stream', 'off']]

    get_pins()

    for name, pinState in args.name:
        if pinState not in ['on', 'off', 'switch']:
            raise ValueError("unknown state")
        if name in supply.outPins:
            set_pin(name, pinState)
            pin = supply.outPins[name]['pin']
            print("set {0} (pin # {1}) {2}".format(name, pin, pinState))
        else:
            raise ValueError("unknown pin")


from django.conf import settings
from rest_framework import serializers

def get_author_data(instance):
    """Returns author's data for a given instance"""
    user = instance.author
    profile = user.profile
    author = {
        'id': user.pk,
        'nickname': profile.nickname,
        'name': profile.name,
        'surname': profile.surname,
    }

    return author

def get_avatar_data(instance):
    """Returns avatar url for a given instance, if there is no avatar, it will return the default avatar"""
    avatar_url = None
    if instance.profile.avatar and instance.profile.avatar.file:
        avatar_url = instance.profile.avatar.url

    return avatar_url or settings.USER_AVATAR_DEFAULT_URL

def get_liked_data(self, instance):
    """Returns True if authenticated user has liked the instance, else False"""
    if not self.context:
        raise serializers.ValidationError('Request context is missing')

    request = self.context.get('request')
    if request.user and request.user.is_authenticated:
        return instance.likes.filter(pk=request.user.id).exists()

    return False

def get_disliked_data(self, instance):
    """Returns True if authenticated user has disliked the instance, else False"""
    if not self.context:
        raise serializers.ValidationError('Request context is missing')

    request = self.context.get('request')
    if request.user and request.user.is_authenticated:
        return instance.dislikes.filter(pk=request.user.id).exists()

    return False


#Imports de Python
#Imports de Django
from django import template
from django.utils import timezone

#Iniciamos el registro de tags en el procesador de templates
register = template.Library()

#Definimos nuestros tags
@register.simple_tag
def ct_color_alerta(config, seguimiento):
    if seguimiento:
        horas = (timezone.now() - seguimiento.fecha).total_seconds() / 3600
        if horas > config.alerta_roja:
            return 'rojo'
        elif horas > config.alerta_amarilla:
            return 'amarillo'
        elif horas > config.alerta_verde:
            return 'verde'
    return 'rojo'


import clr

clr.AddReference('RhinoInside.Revit')
clr.AddReference('RevitAPI')

from RhinoInside.Revit import Revit
from Autodesk.Revit import DB

# access the active document object
doc = Revit.ActiveDBDocument

def show_warning(msg):
    ghenv.Component.AddRuntimeMessage(RML.Warning, msg)

def show_error(msg):
    ghenv.Component.AddRuntimeMessage(RML.Error, msg)

def show_remark(msg):
    ghenv.Component.AddRuntimeMessage(RML.Remark, msg)

Names, Numbers, WallFinishes, FloorFinishes, CeilingFinishes = [], [], [], [], []

Rooms = DB.FilteredElementCollector(doc).OfCategory(DB.BuiltInCategory.OST_Rooms).WhereElementIsNotElementType().ToElements()

for room in Rooms:
    room_name = room.LookupParameter("Name").AsValueString()
    room_number = room.LookupParameter("Number").AsValueString()
    room_wfinish = room.LookupParameter("Wall Finish").AsValueString()
    room_ffinish = room.LookupParameter("Floor Finish").AsValueString()
    room_cfinish = room.LookupParameter("Ceiling Finish").AsValueString()
    Names.append(room_name)
    Numbers.append(room_number)
    WallFinishes.append(room_wfinish)
    FloorFinishes.append(room_ffinish)
    CeilingFinishes.append(room_cfinish)


from flask import request, render_template, url_for
from flask_socketio import join_room, leave_room
from datetime import timedelta, datetime
from model.issuer import Issuer
from model.verifier import Verifier
from model.credential import Credential
from model.credential_request import Credential_Request
from model.prescription import Prescription
from model.order import Order
from model.registry import Registry
from model.blockchain_reader import BlockchainReader
import json
import uuid
import collections.abc

def init_app(app, red, socketio, couch) :
        # Setup SSI model
    if(app.config['DID_METHOD']=='did:ethr'):
        issuer = Issuer(
            'ethr',
            f'did:ethr:{app.config["PHARMACY_PUBLIC_KEY"]}',
            json.dumps(json.load(open("ethkey.pem", "r")))
        )
    elif((app.config['DID_METHOD']=='did:key')):
        issuer = Issuer(
        'key',
        app.config['DID_KEY'],
        json.dumps(json.load(open("key.pem", "r")))
    )
    else:
        raise ValueError('did:method is not supported')

    registry = Registry(
        app.config['RPC_URL'],
        app.config['REGISTRY_ADDRESS'],
        app.config['DOCTOR_IDENTITY'],
        app.config['PHARMACY_IDENTITY']
    )

    verifier = Verifier(registry)

    reader = BlockchainReader(app.config['RPC_URL'], app.config['VC4MED_ADDRESS'])

    db = couch['pharmacy']

    app.add_url_rule('/authorize',  view_func=authorize, methods = ['POST'], defaults={"red" : red, "socketio" : socketio})
    app.add_url_rule('/verify/<stream_id>/<number_of_prescriptions>',
                     view_func=verify,
                     methods = ['GET','POST'],
                     defaults={"red" : red, "socketio" : socketio, "verifier" : verifier}
    )
    app.add_url_rule('/callback/<stream_id>',  view_func=callback, methods = ['GET','POST'], defaults={"red" : red})
    app.add_url_rule('/success/<tx>',  view_func=success, methods = ['GET','POST'], defaults={"red" : red})
    app.add_url_rule('/order/<stream_id>',
                     view_func=create_order,
                     methods = ['POST'],
                     defaults={"red" : red, "socketio" : socketio, "pharmacy" : app.config['PHARMACY_PUBLIC_KEY']})
    app.add_url_rule('/order/sign/<order_id>',  view_func=receive_sign, methods = ['POST'], defaults={"red" : red})
    app.add_url_rule('/order/qr/<code>',  view_func=create_qr_order, methods = ['GET'])
    app.add_url_rule('/order/pay/<code>',
                     view_func=pay_order,
                     methods = ['GET','POST'],
                     defaults={"red" : red, "rpc_url" : app.config['RPC_URL']})
    app.add_url_rule('/order/wait/<code>',
                     view_func=wait_order,
                     methods = ['GET'],
                     defaults={"red" : red, "db":db, "issuer":issuer, "reader":reader}
    )
    app.add_url_rule('/api/credentials',
                     view_func=get_credentials,
                     methods = ['GET', 'POST'],
                     defaults={"red" : red, "db": db, "issuer" : issuer})
    app.add_url_rule('/api/receipts/<status>',
                     view_func=get_receipts,
                     methods = ['GET'],
                     defaults={"db": db})
    app.add_url_rule('/api/credentials/<status>',
                     view_func=update_credentials_status,
                     methods = ['POST'],
                     defaults={"db": db})
    app.add_url_rule('/insurance',  view_func=insurance, methods = ['GET'])

    return

def authorize(red, socketio):

    #Generate stream id
    stream_id = str(uuid.uuid4().hex)

    @socketio.on('subscribe')
    def subscribe(data):
        # Add client to private channel to get further updates
        join_room(data['socket_id'])
        # Save socket_id on server session to make /authorize able to link socket_id and stream_id
        socket_id = data['socket_id']
        print(f'socket_id : {socket_id}, stream_id : {stream_id}')
        red.set(stream_id, json.dumps({'stream_id' : stream_id, 'socket_id' : socket_id, 'verified' : False}))

    #Generate QR Code
    url = url_for('verify', stream_id=stream_id, number_of_prescriptions=request.form['pnumber'], _external = True)
    return render_template('qrcode.html', url=url)

async def verify(stream_id, number_of_prescriptions, red, socketio, verifier):

    if request.method == 'GET':
        request_schema = json.load(open('credentials/CredentialRequest.json', 'r'))
        request_credential = Credential_Request(request_schema, 'MedicalPrescriptionCredential', int(number_of_prescriptions))
        return request_credential.stringify()

    else: #POST
        form = request.form
        presentation = form.get('presentation')

        socket_id = json.loads(red.get(stream_id).decode())['socket_id']

        try:
            result = await verifier.verify_presentation(presentation, 'MedicalPrescriptionCredential')
            if(result == False):
                socketio.emit('error', {'error' : 'Presentation is not valid'}, to=socket_id)
                return 'Presentation is not valid', 500

        except Exception as e:
            print(e)
            socketio.emit('error', {'error' : 'Credential verification failed.'}, to=socket_id)
            return 'Credential verification failed.', 400

        if(verifier.are_credentials_unique(presentation) == False):
            socketio.emit('error', {'error' : 'There are duplicate credentials in the presentation'}, to=socket_id)
            return 'There are duplicate credentials in the presentation', 500

        # Redirect client via server push
        socketio.emit('stream_id', {'stream_id' : stream_id}, to=socket_id)

        # Save the tokens in the session
        try:
            red.set(stream_id, json.dumps({
                'stream_id' : stream_id,
                'socket_id' : socket_id,
                'vp' : json.loads(presentation)
            }))
        except Exception as e:
            print(e)
            # TODO : signal to socket error while writing to redis
            return 200

        # Send ok to wallet
        return 'Credential verified successfully!', 200

def callback(stream_id, red):
    json_prescriptions = []
    prescriptions = []
    data = json.loads(red.get(stream_id).decode())['vp']['verifiableCredential']
    # if we have multiple vcs, just save the array
    if isinstance(data, collections.abc.Sequence):
        json_prescriptions = data
    # if vc is single, create an array
    else:
        json_prescriptions.append(data)

    for p in json_prescriptions:
        drug = p['credentialSubject']['drug']
        max_quantity = int(p['credentialSubject']['quantity'])
        id = p['credentialSubject']['id']
        price = 1
        prescriptions.append( Prescription(id, max_quantity, price, drug).__dict__)

    red.set(f'pr:{stream_id}', json.dumps({'prescriptions' : prescriptions}))
    return render_template('prescription.html', prescriptions=prescriptions, stream_id=stream_id)

# Handle request for creating a new order from pharmacy
def create_order(stream_id, red, socketio, pharmacy):

    prescriptions = json.loads(red.get(f'pr:{stream_id}').decode())['prescriptions']
    for p in prescriptions:
            p['quantity'] = int(request.form.get(p['prId']))
    order = Order([], pharmacy)
    order.load_prescriptions_from_json(prescriptions)
    red.set(order.orderId, json.dumps({'order' : order.serialize(), 'stream_id' : stream_id, 'signed_order' : 'null'}))
    return order.serialize()

# Receive order signed by pharmacy, store it on Redis and redirect to QRCode to make user able to pay with SC
def receive_sign(order_id, red):
    code = str(uuid.uuid4())
    rs = json.loads(red.get(order_id).decode())
    stream_id = rs['stream_id']
    order = rs['order']
    signed_order = request.get_json()['signedOrder']

    red.set(code, json.dumps({'order' : order, 'stream_id' : stream_id, 'signed_order' : signed_order}))
    return code, 200

def create_qr_order(code):
    url = url_for('pay_order', code=code, _external = True)
    return render_template('qrcode-sc.html', url=url, code=code)

async def wait_order(code, red, db, issuer, reader):
    rs = json.loads(red.get(code).decode())
    order_id = rs['order']['orderId']
    stream_id = rs['stream_id']

    # run blockchain listener
    status, tx = reader.read(order_id, 50, 2)
    if not status:
        return f'Error: {tx}', 500
    print(tx)
    # Get data from Redis (prescription_id, quantity, socket_id)
    vp = json.loads(red.get(stream_id).decode())['vp']

    # Issue new credential (Prescription + Proof of payment)
    schema = json.load(open('credentials/Receipt.jsonld', 'r'))
    receipt = Credential(schema)
    receipt.set_value("invoice", {
            "description": vp,
            "confirmationNumber": tx
    })
    receipt.set_id(f'did:example:{order_id}')

    signed_receipt = await issuer.issue_credential(receipt)

    # Save credential on couchDB
    db.save({
        '_id' : order_id,
        'receipt' : json.loads(signed_receipt),
        'refunded' : False,
        'date': datetime.utcnow().replace(microsecond=0).isoformat() + "Z"
    })

    #Save on redis
    red.set(tx, json.dumps({'success' : True, 'order': order_id}))

    return tx

def pay_order(code, red, rpc_url):
    order = json.loads(red.get(code).decode())
    return render_template('pay.html',order=json.dumps(order), mumbai_url=rpc_url)

def success(tx, red):
    try:
        txh = json.loads(red.get(tx).decode())
    except AttributeError as e:
        return 'Not Found', 404

    if(txh['success'] == True):
        return render_template('success.html', tx=tx)
    else:
        return 'Not Found TX', 404

def get_receipts(db, status):

    if(status == 'false'):
        status = False
    elif(status == 'true'):
        status = True

    # Mango query
    query = {
        "selector": {
            "refunded": {"$eq": status}
        }
    }

    rs = []
    for doc in db.find(query):
        rs.append({
            '_id' : doc.get('_id'),
            'date' : doc.get('date'),
            'refunded' : doc.get('refunded')
        })
    return rs

async def get_credentials(red, db, issuer):

    # Mango query
    query = {
        "selector": {
            "refunded": {"$eq": False}
        }
    }

    # Run query
    result_set = []
    for doc in db.find(query):

        # get receipt
        vc = doc['receipt']

        #Generate VPs
        vp = {
                "@context": ["https://www.w3.org/2018/credentials/v1","https://www.w3.org/2018/credentials/examples/v1"],
                "type": ["VerifiablePresentation"],
                "verifiableCredential": [vc],
            }
        try:
            vp = await issuer.issue_presentation(vp)
        except Exception as e:
            print(e)
            return 'Internal server error', 500

        result_set.append(vp)

    return result_set

def update_credentials_status(db, status):
    try:
        order_ids = request.json['order_ids']
    except:
        return 'Bad Request', 400

    if(status=='true'):
        status=True

    for order_id in order_ids:
        doc = db.get(order_id)
        doc['refunded'] = status
        db.save(doc)

    return 'Success', 200

def insurance():
    return render_template('insurance.html')


# encoding: utf-8

from __future__ import absolute_import, unicode_literals

import pathvalidate
import typepy
from six.moves import zip

from ._csv import CsvTableWriter

class LtsvTableWriter(CsvTableWriter):
    """
    A table writer class for
    `Labeled Tab-separated Values (LTSV) <http://ltsv.org/>`__ format.

        :Example:
            :ref:`example-ltsv-table-writer`
    """

    FORMAT_NAME = "ltsv"

    @property
    def format_name(self):
        return self.FORMAT_NAME

    @property
    def support_split_write(self):
        return True

    def __init__(self):
        super(LtsvTableWriter, self).__init__()

        self.is_write_header = False

        self._is_require_header = True

    def write_table(self):
        """
        |write_table| with
        `Labeled Tab-separated Values (LTSV) <http://ltsv.org/>`__ format.
        Invalid characters in labels/data are removed.

        :raises pytablewriter.EmptyHeaderError: If the |headers| is empty.
        :Example:
            :ref:`example-ltsv-table-writer`
        """

        with self._logger:
            self._verify_property()
            self._preprocess()

            for values in self._table_value_matrix:
                ltsv_item_list = [
                    "{:s}:{}".format(pathvalidate.sanitize_ltsv_label(header_name), value)
                    for header_name, value in zip(self.headers, values)
                    if typepy.is_not_null_string(value)
                ]

                if typepy.is_empty_sequence(ltsv_item_list):
                    continue

                self._write_line("\t".join(ltsv_item_list))


from flask import Flask, Blueprint, render_template, jsonify, request

from www.model import all_ind_pow_data_show

new_old_pow = Blueprint('new_old_pow', __name__, url_prefix='/new_old_pow')

@new_old_pow.route('/index/')
def t3_index():
    return render_template('{}/show_data.html'.format('show'),
                           menu_status={'c_menu': 'demo_show', 'p_menu': 'reputation'},
                           current_industry='show')

@new_old_pow.route('/index1/')
def t3_index1():
    return render_template('{}/power_data_show.html'.format('show'),
                           menu_status={'c_menu': '14-18', 'p_menu': 'dataDig'},
                           current_industry='show')

# 新旧行业占比
@new_old_pow.route('/power_proportion/')
def power_proportion():
    year = request.args.get('year')

    data = all_ind_pow_data_show.get_all_pow_consum(year)
    # print(data)

    return jsonify({"msg": 200, "data": data})

# 新行业排名
@new_old_pow.route('/new_pow_top10/')
def new_ind_top10():
    year = request.args.get('year')
    tag = request.args.get('tag')
    data = all_ind_pow_data_show.get_new_ind_top10(year, tag)
    # print('*' * 100)
    # print(data)
    # print(tag)
    # print('*' * 100)
    return jsonify({'data': data, 'msg': 200})

# 新动能行业用电量指数
@new_old_pow.route('new_old_ind_index')
def new_ind_index():
    data = all_ind_pow_data_show.get_new_ind_index()
    return jsonify({'data': data, 'msg': 200})

# 旧动能行业用电量指数
@new_old_pow.route('old_ind_index')
def old_ind_index():
    pass

# 新旧行业增速
@new_old_pow.route('new_old_ind_speed_up')
def use_pow_speed_up():
    data = all_ind_pow_data_show.get_pow_speed()
    return jsonify({'msg': 200, 'data': data})


import youtube_dl
import subprocess
import os.path
ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'vorbis',
        'preferredquality': '192',
    }],
}

songs = [
    ('http://www.youtube.com/watch?v=0a6j3rv1PFk', '我會守在這裡'),
    ('https://www.youtube.com/watch?v=BiiwclhI5Y8', '平凡的一天')
]

for url, out_name in songs:
    ydl_opts['outtmpl'] = '{}/{}.%(ext)s'.format(out_name, out_name)
    if not os.path.isfile('{}/{}.ogg'.format(out_name,out_name)):
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            ydl.download([url])
    else:
        print("{} already exists, skipping".format(out_name))

subprocess.call(['ffmpeg',  '-ss', '22', '-i', '平凡的一天/平凡的一天.ogg', '-acodec', 'copy', '平凡的一天/平凡的一天_cut.ogg'])


from itertools import product
from collections import defaultdict

from util import read_input

test_input = """L.LL.LL.LL
LLLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLLL
L.LLLLLL.L
L.LLLLL.LL"""
"""variable for the test cases"""

true_input = read_input(11)

dirs = [(-1, -1), (0, -1), (+1, -1), (-1, 0), (+1, 0), (-1, +1), (0, +1), (+1, +1)]

def neighbors(r, c):
    """ Get the neighbors

    This used to be a list comprehension.
    Swapping to a generator expression made everything slower.
    But going to this list of tuple expression saves 0.5-1 second runtime!
    """
    return [
        (r - 1, c - 1),
        (r + 0, c - 1),
        (r + 1, c - 1),
        (r - 1, c + 0),
        (r + 1, c + 0),
        (r - 1, c + 1),
        (r + 0, c + 1),
        (r + 1, c + 1),
    ]

ZERO = 0
LOW = 1
HIGH = 2

def statusA(r, c, state_now):
    s = 0
    for t in neighbors(r, c):
        s += state_now[t] == "#"
        if s == 4:
            return HIGH
    if s == 0:
        return ZERO
    else:
        return LOW

def statusB(r, c, h, w, state_now):
    s1 = 0
    for dx, dy in dirs:
        x = r + dx
        y = c + dy
        while 0 <= x <= h and 0 <= y <= w:
            v = state_now[(x, y)]
            if v == "#":
                s1 += 1
                if s1 == 5:
                    return HIGH
                break
            elif v == "L":
                break
            x += dx
            y += dy
    if s1 == 0:
        return ZERO
    else:
        return LOW

def process(mode, input):

    data = {
        (r, c): char
        for r, line in enumerate(input.splitlines())
        for c, char in enumerate(line)
    }
    h, w = (a + 1 for a in max(data.keys()))

    did_update = True
    state_now = defaultdict(lambda: ".", data)
    state_next = defaultdict(lambda: ".")
    while did_update:
        did_update = False
        for r, c in product(range(h), range(w)):
            if mode == "A":
                status = statusA(r, c, state_now)
            elif mode == "B":
                status = statusB(r, c, h, w, state_now)
            else:
                raise ValueError
            if state_now[(r, c)] == "L" and status == ZERO:
                state_next[(r, c)] = "#"
                did_update = True
            elif state_now[(r, c)] == "#" and status == HIGH:
                state_next[(r, c)] = "L"
                did_update = True
            else:
                state_next[(r, c)] = state_now[(r, c)]
        state_now = state_next
        state_next = defaultdict(lambda: ".")
    return sum(1 for k, v in state_now.items() if v == "#")

assert process("A", test_input) == 37

ansA = process("A", true_input)
assert ansA > 2189
assert ansA == 2247

assert process("B", test_input) == 26
ansB = process("B", true_input)
assert ansB == 2011


import sys
import os

from rouge import Rouge

sys.path.append('..')

from predict import Predict
from utils import timer
import config
from dataset import SamplesDataset

class RougeEval():
    def __init__(self, path, vocab=None):
        self.predict = Predict()
        self.rouge = Rouge()
        self.testset = SamplesDataset(path, vocab=self.predict.vocab)
        print('test vocab: ', len(self.testset.vocab))
        self.refs = self.process()
        self.hypos = self.build_hypos()
        self.scores = self.get_scores()

    def process(self):
        tgts = self.testset['tgt']
        refs = []
        for tgt in tgts:
            ref = ' '.join(tgt).replace('。', '.')
            refs.append(ref)
        print(f'Test set contains {len(self.testset)} samples.')
        return refs

    @timer('building hypotheses')
    def build_hypos(self):
        """Generate hypos for the dataset.

        Args:
            predict (predict.Predict()): The predictor instance.
        """
        print('Building hypotheses.')
        count = 0
        exception_count = 0
        hypos = []
        for sample in self.testset:
#             print(' '.join(sample['source']))
            count += 1
            if count % 100 == 0:
                print(count)
#             try:
            hypo = self.predict.predict(sample['x'], sample['OOV'], sample['img_vec'])
            hypos.append(hypo)
#             except Exception as e:
#                 print(sample)
#                 print(e)
#                 exit(0)
#             try:
#                 self.hypos.append(predict.predict(sample['x'], sample['OOV'], sample['img_vec']))
#             except:
#                 exception_count += 1
#                 print('exception: ', exception_count)
#                 self.hypos.append('')
        return hypos

    def get_scores(self):
        assert len(self.hypos) > 0, 'Build hypotheses first!'
        print('Calculating average rouge scores.')
        for hypo, ref in zip(self.hypos, self.refs):
            print(hypo, ' ||| ', ref)
        return self.rouge.get_scores(self.hypos, self.refs, avg=True)

    def one_sample(self, hypo, ref):
        return self.rouge.get_scores(hypo, ref)[0]

    def get_metric(self, metric):
        return self.scores[metric]['p']

rouge_eval = RougeEval(config.test_data_path)

result = rouge_eval.scores
print('rouge1: ', result['rouge-1'])
print('rouge2: ', result['rouge-2'])
print('rougeL: ', result['rouge-l'])
with open(config.rouge_path, 'a') as file:
    file.write(config.model_name)
    for r, metrics in result.items():
        file.write(r+'\n')
        for metric, value in metrics.items():
            file.write(metric+': '+str(value*100))
            file.write('\n')


# print a string
print("Mary had a little lamb.")
# print a string and use .format to insert a string as a variable
print("It's fleece was white as {}.".format('snow'))
# print a string
print("And everywhere that Mary went.")
# print a string 10 times
print("." * 10) # what'd that do?

# define a bunch of variables the hardest way possible
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"

# watch that comma at the end. try removing it to see what happens
# print all these variables, connect them together, and end the string with a charector. in this case a space
# the comma indicates that the list of variables and/or the string is ended and a parameter for the print command is next
print(end1 + end2 + end3 + end4 + end5 + end6, end=' ')
# print a bunch of variables ans connect them together in the order they're listed
print(end7 + end8 + end9 + end10 + end11 + end12)


import search
import random
import re

#https://courses.cs.ttu.ee/pages/ITI0120lab5

def optSwap(route,i,k):
    assert i >= 0 and i < (len(route) - 1)
    assert k > i and k < len(route)
    new_route = route[0:i]
    new_route.extend(reversed(route[i:k + 1]))
    new_route.extend(route[k+1:])
    assert len(new_route) == len(route)
    return new_route

def readingFile(fileName):
    dist_matrix=[]
    with open(fileName, 'r') as filehandle:
        next(filehandle)
        for line in filehandle:
            liners=[]
            line = line.split()
            liners.extend(line)
            dist_matrix.append(liners)
    return dist_matrix

class TSP(search.Problem):
    def __init__(self, instance):
        value =int(re.search(r'\d+', instance).group())
        listt=[]
        for x in range(value):
            listt.append(x)
        random.shuffle(listt)
        self.instance=instance
        self.initial=listt
    def actions(self, state):
        optimizeCount=5000
        pairs = [random.sample(range(len(state)), 2) for x in range(optimizeCount)]
        return pairs
    def result(self, state, action):
        action.sort()
        i=action[0]
        j=action[1]
        newState=optSwap(state,i,j)
        return newState
    def cost(self, state):
        routeDistance=0
        stateLength=len(state)
        for x in range(stateLength):
            distance=dist_matrix[state[(1+x)%stateLength]][state[(x)%stateLength]]
            routeDistance+=int(distance)
        return routeDistance
    def value(self, state):
        return 1/(self.cost(state)+1)

dist_matrix=readingFile("gr48.txt")
p = search.InstrumentedProblem(TSP("gr48.txt")) #pead ülevalt ka vahetama

g = search.hill_climbing(p)
print(g) #here is the list in order
print(p.cost(g))

5 - Heurestic search - Travelling salesman problem/_mathias luik harj 5.py


from aiogram import types
from handler import constans
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton

start_kb = InlineKeyboardMarkup(resize_keyboard=True)
start_kb.add(InlineKeyboardButton('меню', callback_data='menu'),
             InlineKeyboardButton('адрес', callback_data='adress')
             )

async def start_command(message: types.Message):
    '''
    Функция для запуска бота и перехода к его командам
    '''
    await message.answer(text=constans.START_BOT,
                         reply_markup=start_kb
                         )


# Create a program similar to above that counts the number of words in the files as well.
# After printing out information about each file, this program should also print the total
# number of lines & words in all 3 files. You can use the string split function to split a line
# of input into a list of words by splitting the line on spaces.

file_name = "counts.txt"
writeFile = open(file_name, "w")

file_names = ["text1.txt", "text2.txt", "text3.txt"]
all_words = 0
all_lines = 0

for file in file_names:
    dataFile = open(file, "r")

    count_lines = 0
    total_words = 0

    line = dataFile.readline()

    while line != "":
        print(line)
        new_words = len(line.split(" "))
        print(new_words)
        total_words += new_words
        print(total_words)
        count_lines += 1
        print(count_lines)
        line = dataFile.readline()

    print(file, ":", count_lines, "lines", total_words, " words.", file=writeFile)
    all_lines += count_lines
    all_words += total_words
    dataFile.close()

print("TOTAL: ", all_lines, " lines, ", all_words, " words.", file=writeFile)

writeFile.close()


#!/usr/bin/env python
"""Create input files for libRadtran simulation to be used in BACARDI processing

This will write all input files for a clearsky broadband libRadtran simulations along the flight path to be compared to BACARDI measurements.

**Required User Input:**

* flights in a list (optional, if not manually specified all flights will be processed)
* time_step, in what interval should the simulations be done along the flight path?
* solar_flag, write file for simulation of solar or thermal infrared fluxes?
* use_dropsonde flag, only available for |haloac3|

**Output:**

* log file
* input files for libRadtran simulation along flight track

Run like this:

.. code-block:: shell

    python libradtran_write_input_file_bacardi.py

Behind some options you find the page number of the manual, where the option is explained in more detail.
Set options to "None" if you don't want to use them.
* Variables which start with "_" are for internal use.
For |haloac3| a fixed radiosonde location (Longyearbyen) is used.
Uncomment the find_closest_station line for CIRRUS-HL.

*author*: Johannes Röttenbacher
"""
if __name__ == "__main__":
    # %% module import
    import pylim.helpers as h
    from pylim import reader, solar_position
    import pylim.halo_ac3 as meta
    from pylim.libradtran import find_closest_radiosonde_station
    import os
    import datetime
    import numpy as np
    import pandas as pd
    from global_land_mask import globe
    import logging

    log = logging.getLogger("pylim")
    log.addHandler(logging.StreamHandler())
    log.setLevel(logging.DEBUG)

    # %% user input
    campaign = "halo-ac3"
    all_flights = [key for key in meta.transfer_calibs.keys()] if campaign == "cirrus-hl" else list(meta.flight_names.values())
    all_flights = all_flights[18:19]  # select single flight if needed
    time_step = pd.Timedelta(minutes=1)
    solar_flag = True  # True for solar wavelength range, False for terrestrial wavelength range
    use_dropsonde = True if campaign == "halo-ac3" else False

    # %% run for all flights
    for flight in all_flights:
        date = flight[9:17] if campaign == "halo-ac3" else flight[7:15]
        # set paths
        _base_dir = h.get_path("base", campaign=campaign)
        _libradtran_dir = h.get_path("libradtran", flight, campaign)
        _bahamas_dir = h.get_path("bahamas", flight, campaign)
        _bahamas_file = [f for f in os.listdir(_bahamas_dir) if f.endswith(".nc")][0]
        input_path = f"{_libradtran_dir}/wkdir/{'solar' if solar_flag else 'thermal'}"
        h.make_dir(input_path)  # create directory
        radiosonde_dir = f"{_base_dir}/../0{1 if campaign == 'halo-ac3' else 2}_soundings/RS_for_libradtran"
        # only needed for HALO-AC3
        dropsonde_path = f"{_base_dir}/../01_soundings/RS_for_libradtran/Dropsondes_HALO/Flight_{date}"
        solar_source_path = f"{_base_dir}/../00_tools/0{5 if campaign == 'cirrus-hl' else 8}_libradtran"

        bahamas_ds = reader.read_bahamas(f"{_bahamas_dir}/{_bahamas_file}")
        timestamp = bahamas_ds.time[0]
        while timestamp < bahamas_ds.time[-1]:
            bahamas_ds_sel = bahamas_ds.sel(time=timestamp)
            lat, lon, alt, pres, temp = bahamas_ds_sel.IRS_LAT.values, bahamas_ds_sel.IRS_LON.values, bahamas_ds_sel.IRS_ALT.values, bahamas_ds_sel.PS.values, bahamas_ds_sel.TS.values
            is_on_land = globe.is_land(lat, lon)  # check if location is over land
            zout = alt / 1000  # page 127; aircraft altitude in km
            # need to create a time zone aware datetime object to calculate the solar azimuth angle
            dt_timestamp = datetime.datetime.fromtimestamp(timestamp.values.astype('O') / 1e9, tz=datetime.timezone.utc)

            # define radiosonde station or dropsonde
            if use_dropsonde:
                dropsonde_files = [f for f in os.listdir(dropsonde_path)]
                dropsonde_files.sort()  # sort dropsonde files
                # read out timestamps from file
                dropsonde_times = [int(f[9:14].replace(" ", "")) for f in dropsonde_files]
                # convert to date time to match with BAHAMAS/INS timestamp
                dropsonde_times = pd.to_datetime(dropsonde_times, unit="s", origin=pd.to_datetime(date))
                # find index of the closest dropsonde to BAHAMAS/INS timestamp
                time_diffs = dropsonde_times - timestamp.values
                idx = np.asarray(np.nonzero(time_diffs == time_diffs.min()))[0][0]
                dropsonde_file = dropsonde_files[idx]
                radiosonde = f"{dropsonde_path}/{dropsonde_file} H2O RH"
            else:
                # radiosonde_station = find_closest_radiosonde_station(lat, lon)
                radiosonde_station = "Longyearbyen_01004"  # standard for HALO-(AC)3
                station_nr = radiosonde_station[-5:]
                if campaign == "halo-ac3":
                    radiosonde = f"{radiosonde_dir}/Radiosonde_for_libradtran_{station_nr}_{dt_timestamp:%Y%m%d}_12.dat H2O RH"
                else:
                    radiosonde = f"{radiosonde_dir}/{radiosonde_station}/{dt_timestamp:%m%d}_12.dat H2O RH"

            # get time in decimal hours
            decimal_hour = dt_timestamp.hour + dt_timestamp.minute / 60 + dt_timestamp.second / 60 / 60
            year, month, day = dt_timestamp.year, dt_timestamp.month, dt_timestamp.day
            if is_on_land:
                calc_albedo = 0.2  # set albedo to a fixed value (will be updated)
            else:
                # calculate cosine of solar zenith angle
                cos_sza = np.cos(np.deg2rad(
                    solar_position.get_sza(decimal_hour, lat, lon, year, month, day, pres, temp - 273.15)))
                # calculate albedo after Taylor et al 1996 for sea surface
                calc_albedo = 0.037 / (1.1 * cos_sza ** 1.4 + 0.15)

            # optionally provide libRadtran with sza (libRadtran calculates it itself as well)
            sza_libradtran = solar_position.get_sza(decimal_hour, lat, lon, year, month, day, pres, temp - 273.15)
            if sza_libradtran > 90:
                log.debug(f"Solar zenith angle for {dt_timestamp} is {sza_libradtran:.2f}!\n"
                          f"Skipping this timestamp and moving on to the next one.")
                # increase timestamp by time_step
                timestamp = timestamp + time_step
                continue

            # %% create input file
            _input_filename = f"{dt_timestamp:%Y%m%d_%H%M%S}_libRadtran.inp"
            _input_filepath = f"{input_path}/{_input_filename}"

            # %% set options for libRadtran run - atmospheric shell
            atmos_settings = dict(
                albedo=f"{calc_albedo:.4f}",
                altitude=0,  # page 80; ground height above sea level in km (0 for over ocean)
                # atmosphere_file="/opt/libradtran/2.0.4/share/libRadtran/data/atmmod/afglms.dat",  # page 81
                data_files_path="/opt/libradtran/2.0.4/share/libRadtran/data",  # location of internal libRadtran data
                latitude=f"N {lat:.6f}" if lat > 0 else f"S {-lat:.6f}",  # page 96
                longitude=f"E {lon:.6f}" if lon > 0 else f"W {-lon:.6f}",  # BAHAMAS: E = positive, W = negative
                mol_file=None,  # page 104
                mol_modify="O3 300 DU",  # page 105
                radiosonde=radiosonde,  # page 114
                time=f"{dt_timestamp:%Y %m %d %H %M %S}",  # page 123
                source=f"solar {solar_source_path}/NewGuey2003_BBR.dat" if solar_flag else "thermal",  # page 119
                sur_temperature="293.15",  # page 121; set to 20 degC for now
                # sza=f"{sza_libradtran:.4f}",  # page 122
                # verbose="",  # page 123
                # SMART wavelength range (179.5, 2225), BACARDI solar (290, 3600), BACARDI terrestrial (4000, 100000)
                wavelength="290 3600" if solar_flag else "4000 100000",
                zout=f"{zout:.3f}",  # page 127; altitude in km above surface altitude
            )

            # set options for libRadtran run - radiative transfer equation solver
            rte_settings = dict(
                rte_solver="fdisort2",
            )

            # set options for libRadtran run - post-processing
            postprocess_settings = dict(
                output_user="sza albedo zout edir edn eup",  # page 109
                output_process="integrate",  # page 108
            )

            # %% write input file
            with open(_input_filepath, "w") as ifile:
                ifile.write(f"# libRadtran input file generated with libradtran_write_input_file_bacardi.py "
                            f"({datetime.datetime.utcnow():%c UTC})\n")
                for settings, line in zip([atmos_settings, rte_settings, postprocess_settings],
                                          ["Atmospheric", "RTE", "Post Process"]):
                    ifile.write(f"\n# {line} Settings\n")
                    for key, value in settings.items():
                        if value is not None:
                            ifile.write(f"{key} {value}\n")

            # increase timestamp by time_step
            timestamp = timestamp + time_step
            # end of while


# longest common subsequence
'''
https://en.wikipedia.org/wiki/Longest_common_subsequence_problem

optimal structure: the problem can be broken down into smaller, simple "subproblems"
overlapping subproblems: the solution to high-level subproblems often reuse lower level subproblems
'''

'''
1st property: if two sequences both end in the same element, shorten each sequence by removing
the last element

2nd property: if two sequences do not end in the same symbol, look for
max{LCS(Xn,Ym-1), LCS(Xn-1,Ym)}
'''

def lcs(x,y): #
    # create a memo
    memo = [[0 for i in range(0,len(y)+1)] for j in range(0,len(x)+1)]

    for i in range(0, len(x)+1):
        for j in range(0, len(y)+1):
            if x[i] == y[j]:
                if i == 0 or j == 0:
                    memo[i][j] = 0

                else:
                    memo[i][j] = memo[i-1][j-1] + 1
            else:
                memo[i][j] = max(memo[i][j-1], memo[i-1][j])

    return memo[len(x),len(y)]

print(lcs("abcdaf", "acbcf"))


"""Tests for mir.frelia.enja module."""

import io

import pytest

import mir.frelia.enja as enja

def test_load():
    """Test parsing a simple enja document from a file."""
    file = io.StringIO('foo: bar\n---\n
Hello world!
')
    doc = enja.load(file)
    assert doc.header == {'foo': 'bar'}
    assert doc.body == '
Hello world!
'

def test_load_empty_file():
    """Test parsing an empty enja document from a file."""
    file = io.StringIO('')
    doc = enja.load(file)
    assert doc.header == {}
    assert doc.body == ''

def test_dump():
    """Test dumping an Enja document to a file."""
    document = enja.Document('girl meets girl')
    document.header['sophie'] = 'prachta'
    file = io.StringIO()
    enja.dump(document, file)
    assert file.getvalue() == 'sophie: prachta\n---\ngirl meets girl'

def test_document_equal():
    """Test document equality."""
    doc1 = enja.Document('girl meets girl')
    doc1.header['sophie'] = 'prachta'
    doc2 = enja.Document('girl meets girl')
    doc2.header['sophie'] = 'prachta'
    assert doc1 == doc2

def test_document_unequal_body():
    """Test document with unequal body."""
    doc1 = enja.Document('girl meets girl')
    doc1.header['sophie'] = 'prachta'
    doc2 = enja.Document('')
    doc2.header['sophie'] = 'prachta'
    assert doc1 != doc2

def test_document_unequal_header():
    """Test document with unequal header."""
    doc1 = enja.Document('girl meets girl')
    doc1.header['sophie'] = 'prachta'
    doc2 = enja.Document('girl meets girl')
    assert doc1 != doc2

def test_document_unequal_type():
    """Test document against unequal type."""
    doc = enja.Document('girl meets girl')
    doc.header['sophie'] = 'prachta'
    assert doc != 'girl meets girl'

def test_document_repr():
    """Test document repr()."""
    doc = enja.Document('girl meets girl')
    doc.header['sophie'] = 'prachta'
    assert repr(doc) == "<Document with header={'sophie': 'prachta'}, body='girl meets girl'>"


import xchat, re

__module_name__="antiflood"
__module_version__="1.0"
__module_description__="zline joining spambots!"
__module_author__="shibumi"

REGEX="^[A-Za-z]{2}$" #example regex
CHANNEL="#channel"    #example channel

class antiflood(object):
  def __init__(self):
    xchat.hook_print("Join", self.onJoin)

  def onJoin(self, word, word_eol, userdata):
    username = word[0]
    pattern = re.compile(REGEX)
    userhost = word[2]
    userhost = userhost.split("@")
    ident = userhost[0]
    chars = len(ident)
    if word[1] == CHANNEL:
      if pattern.match(ident) != None:
        xchat.command("ZLINE %s 10d mess with the best die like the rest" % username)

antiflood()

#set ts=2 sts=2 sw=2 et


from mpi4py import MPI

import simplejson as json

import numpy as np

from nifty import Field, FieldArray, RGSpace
from d2o import distributed_data_object

comm = MPI.COMM_WORLD
size = comm.size
rank = comm.rank

class MagneticField(Field):
    def __init__(self, parameters={}, domain=None, val=None, dtype=None,
                 distribution_strategy=None, copy=False, random_seed=None):

        super(MagneticField, self).__init__(
                                domain=domain,
                                val=val,
                                dtype=dtype,
                                distribution_strategy=distribution_strategy,
                                copy=copy)

        assert(len(self.domain) == 3)
        assert(isinstance(self.domain[0], FieldArray))
        assert(isinstance(self.domain[1], RGSpace))
        assert(isinstance(self.domain[2], FieldArray))

        self._parameters = {}
        for p in self.descriptor_lookup:
            self._parameters[p] = np.float(parameters[p])

        casted_random_seed = distributed_data_object(
                                global_shape=(self.shape[0],),
                                dtype=np.int,
                                distribution_strategy=distribution_strategy)

        if random_seed is None:
            random_seed = np.random.randint(np.uint32(-1)/3,
                                            size=self.shape[0])
            random_seed = comm.bcast(random_seed, root=0)

        casted_random_seed[:] = random_seed
        self.random_seed = casted_random_seed

    @property
    def descriptor_lookup(self):
        return {}

    @property
    def parameters(self):
        return self._parameters

    def set_val(self, new_val=None, copy=False):
        if new_val is not None:
            raise RuntimeError("Setting the field values explicitly is not "
                               "supported by MagneticField.")
        self._val = self._create_field()

    def _to_hdf5(self, hdf5_group):
        hdf5_group.attrs['_parameters'] = json.dumps(self._parameters)
        ret_dict = super(MagneticField, self)._to_hdf5(hdf5_group=hdf5_group)
        ret_dict['random_seed'] = self.random_seed
        return ret_dict

    @classmethod
    def _from_hdf5(cls, hdf5_group, repository):
        new_field = super(MagneticField, cls)._from_hdf5(hdf5_group=hdf5_group,
                                                         repository=repository)
        new_field._parameters = json.loads(hdf5_group.attrs['_parameters'])
        new_field.random_seed = repository.get('random_seed', hdf5_group)
        return new_field


import csv
##将pagerank数据写入csv文件保存
header = ['url', 'prpoint']
with open('processrespagerank.txt', 'r', encoding='utf-8') as f:
    data = f.readlines()
onionrankurl =[]
onionrankpoint=[]
for points in data:
    url = points.split(' ')[0]
    propoint =points.split(' ')[1].replace('\n','')
    onionrankurl.append(url)
    onionrankpoint.append(propoint)

for i in range(0,len(onionrankpoint)):
    info_dict = {}
    info_dict['url'] = onionrankurl[i]
    info_dict['prpoint'] = onionrankpoint[i]
    csvfile = open('pagerankpoint.csv', 'a+', encoding='utf-8-sig', newline='')
    writeobj = csv.DictWriter(csvfile, fieldnames=header)
    writeobj.writerow(info_dict)


from django.urls import path
from tool import views

urlpatterns = [
    path('upload_Tool', views.uploadTool),
    path('submit', views.saveTool),
    path('getdata', views.getData),
    path('deleteTool',views.deleteTool),
    path('updateTool',views.updateTool),
]


# Uses python3
import sys

def get_optimal_value(capacity, weights, values):
    price = []
    value = 0.
    for i in range(n):
        price.append([values[i], weights[i]])

    price.sort(key=lambda x: -1.0*(x[0]/x[1]))

    for k in range(n):
        if capacity == 0:
            return value

        else:
            a = min(price[k][1], capacity)

            value += a * (1.0 * price[k][0] / price[k][1])

            capacity -= a

            price[k][1] -= a

    return value

if __name__ == "__main__":
    data = list(map(int, sys.stdin.read().split()))
    n, capacity = data[0:2]
    values = data[2:(2 * n + 2):2]
    weights = data[3:(2 * n + 2):2]
    opt_value = get_optimal_value(capacity, weights, values)
    print("{:.3f}".format(opt_value))


'''
输入有数行。

第一行包含数个整数，有正有负，表示原先账本上的内容。

接下来数行各包含一个字母（指令的类型）以及一个数字，以空白为间隔。

若输入为 "a 4"，请在账目中加入4 这个数字。

若输入为 "r 204"，请从账目中删除第一次出现 204 这个数字，若账本中没有 204 这个数字，则略过这个指令。

若输入为 "p -1"，请从账目中删除 index 为 -1 的账目，p 指令后如果是负数，保证是 -1。

若输入为 "p 2" ，请从账目中删掉 index 为 2 的账目，若 index 2 不存在账本中，则略过这个指令。

若输入为 "q 4" ，请结束输入的部分，并印出 index 为 4 的内容，以及所有账目的平均数；若该 index不存在，请输出 Error ，并且不需要输出平均数。

##
1 2 3 4 5 6 7
a 8
r 2
p -1
q 4

6
4

##
10 9 8 7 6 5 4 3 2 1
q 10

Error
'''
memo = [int(item) for item in input().split()]
run = True
while run:
    inputAlp = input().split()
    if inputAlp[0] == "q":
        run = False
        indexCount = int(inputAlp[1])
        try:
            print(memo[indexCount])
            print(sum(memo)//len(memo))
        except IndexError:
            print("Error")
        finally:
            break
    if inputAlp[0] == "a":
        memo.append(int(inputAlp[1]))
    if inputAlp[0] == "r":
        if int(inputAlp[1]) in memo:
            memo.remove(int(inputAlp[1]))
    if inputAlp[0] == "p":
        if int(inputAlp[1]) == -1:
            memo.pop(-1)
            continue
        if len(memo) <=int(inputAlp[1]):
            continue
        memo.pop(int(inputAlp[1]))


import numpy as np
import time

def d3_1_numpy(data):
    data = data.split("\n")
    mat = np.zeros((2500, 2500))
    for row in data:
        claim_id, _, offset, dimensions = row.split(" ")
        (x_offset, y_offset) = [int(e) for e in offset.rstrip(":").split(",")]
        (width, height) = [int(e) for e in dimensions.split("x")]
        mat[y_offset:y_offset + height, x_offset:x_offset + width] += 1
    return len(mat[np.where(mat > 1)])

def d3_1_dict(data):
    # Not working, too high (Numpy version is correct)
    data = data.split("\n")
    # Dictionary does not require pre-allocating space, but requires explicit iterations over sub-grids
    grid = {}
    for row in data:
        claim_id, _, offset, dimensions = row.split(" ")
        (y_offset, x_offset) = [int(e) for e in offset.rstrip(":").split(",")]
        (width, height) = [int(e) for e in dimensions.split("x")]
        for x in range(x_offset, x_offset + width):
            for y in range(y_offset, y_offset + width):
                if (x, y) not in grid:
                    grid[x, y] = 1
                else:
                    grid[x, y] += 1
    return len([elem for elem in grid if grid[elem] > 1])

def d3_2(data):
    data = data.split("\n")
    mat = np.zeros((2500, 2500))
    intact_claim = "NO INTACT CLAIM"
    for row in data:
        claim_id, _, offset, dimensions = row.split(" ")
        (x_offset, y_offset) = [int(e) for e in offset.rstrip(":").split(",")]
        (width, height) = [int(e) for e in dimensions.split("x")]
        mat[y_offset:y_offset + height, x_offset:x_offset + width] += 1
    for row in data:
        claim_id, _, offset, dimensions = row.split(" ")
        (x_offset, y_offset) = [int(e) for e in offset.rstrip(":").split(",")]
        (width, height) = [int(e) for e in dimensions.split("x")]
        if np.all(mat[y_offset:y_offset + height, x_offset:x_offset + width] == 1):
            intact_claim = claim_id
    return intact_claim

# My input
with open("d3.txt", "r") as f:
    data = "".join(f.readlines())
    data = data.rstrip("\n")

# Part 1
# Test cases
test_input = "#1 @ 1,3: 4x4\n#2 @ 3,1: 4x4\n#3 @ 5,5: 2x2"
print(f"With dict: expected {4} and got {d3_1_dict(test_input)}")
print(f"With Numpy: expected {4} and got {d3_1_numpy(test_input)}")
test_input = "#1 @ 1,3: 4x4\n#2 @ 1,3: 4x4"
print(f"With dict: expected {16} and got {d3_1_dict(test_input)}")
print(f"With Numpy: expected {16} and got {d3_1_numpy(test_input)}")
test_input = "#1 @ 1,1: 2x2\n#2 @ 0,4: 4x4"
print(f"With dict: expected {0} and got {d3_1_dict(test_input)}")
print(f"With Numpy: expected {0} and got {d3_1_numpy(test_input)}")

start = time.time()
res = d3_1_dict(data)
print(f"Part 1 {res} with dict took {time.time() - start} seconds")
start = time.time()
res = d3_1_numpy(data)
print(f"Part 1 {res} with numpy took {time.time() - start} seconds")

# Part 2
# Test cases
test_input = "#1 @ 1,3: 4x4\n#2 @ 3,1: 4x4\n#3 @ 5,5: 2x2"
print(f"Expected {3} and got {d3_2(test_input)}")

print(f"Part 2 {d3_2(data)}")


#!/usr/bin/python3

import sys
import os
import pandas as pd

def convert(file):
    if os.path.exists(os.path.dirname(file.path) + '/gis/' +
                      file.name + ' ΓΙΑ gis.xlsx'):
        print("Already converted. Skipping...")
        return

    # Διάβασε το αρχείο μετατρέποντας τα πάντα σε str
    # και αγνοώντας τα κενά κελιά
    df = pd.read_excel(file.path, dtype=str, keep_default_na=False)
    for i in df.columns.tolist():
        df[i] = df[i].str.strip()
    df.rename(columns={'ΑΡΙΘΜΟΣ ΜΗΤΡΩΟΥ ΜΑΘΗΤΗ': 'Αρ. μητρώου',
                       'ΔΗΜΟΣ': 'Διεύθυνση, περιοχή',
                       'Τ.Κ.': 'Διεύθυνση, Τ.Κ.'},
              inplace=True)
    df['Επώνυμο μαθητή'] = ""
    df['Όνομα μαθητή'] = ""
    df['Όνομα πατέρα'] = ""
    df['Διεύθυνση, οδός - αριθμός'] = df['Δ/ΝΣΗ ΚΑΤΟΙΚΙΑΣ ΜΑΘΗΤΗ'].str.cat(
        df['ΑΡΙΘΜΟΣ'], sep=' ', na_rep='')
    result = map(lambda x: 'ΑΔΕΡΦΟΣ/Η ΣΤΟ ' + x if x != '' else '',
                 df['ΓΥΜΝΑΣΙΟ ΑΔΕΛΦΟΥ/ΗΣ'].tolist())
    df['ΓΥΜΝΑΣΙΟ ΑΔΕΛΦΟΥ/ΗΣ'] = pd.Series(list(result))
    result = map(lambda x: 'ΤΜΗΜΑ ΕΝΤΑΞΗΣ ' + x if x != '' else '',
                 df['ΦΟΙΤΗΣΗ ΣΕ ΤΜΗΜΑ ΕΝΤΑΞΗΣ'].tolist())
    df['ΦΟΙΤΗΣΗ ΣΕ ΤΜΗΜΑ ΕΝΤΑΞΗΣ'] = pd.Series(list(result))
    df['Πληροφορίες'] = pd.Series(['\n'.join(i)
                                  for i in zip(df['ΓΥΜΝΑΣΙΟ ΑΔΕΛΦΟΥ/ΗΣ'],
                                               df['ΦΟΙΤΗΣΗ ΣΕ ΤΜΗΜΑ ΕΝΤΑΞΗΣ'],
                                               df['ΠΑΡΑΤΗΡΗΣΕΙΣ'])])
    df['Πληροφορίες'] = df['Πληροφορίες'].str.strip()
    df['Σχολείο Τοποθέτησης'] = ""
    df['email Γονέα'] = ""
    df['Διεύθυνση για Google (προαιρετικό)'] = ""
    df['Συντεταγμένες Διεύθυνσης (προαιρετικό)'] = ""
    newdf = df[["Αρ. μητρώου", "Επώνυμο μαθητή", "Όνομα μαθητή",
                "Όνομα πατέρα", "Διεύθυνση, οδός - αριθμός", "Διεύθυνση, Τ.Κ.",
                "Διεύθυνση, περιοχή", "Πληροφορίες", "Σχολείο Τοποθέτησης",
                "email Γονέα", "Διεύθυνση για Google (προαιρετικό)",
                "Συντεταγμένες Διεύθυνσης (προαιρετικό)"]]
    try:
        os.mkdir(os.path.dirname(file.path)+"/gis")
    except FileExistsError:
        pass
    with pd.ExcelWriter(os.path.dirname(file.path) + '/gis/' +
                        file.name + ' ΓΙΑ gis.xlsx') as writer:
        newdf.to_excel(writer)
        writer.save()

def scandir(dirlist):
    while dirlist:
        with os.scandir(dirlist.pop()) as it:
            for entry in it:
                if entry.is_dir() and entry.name != 'gis':
                    print("Found dir", entry.name)
                    dirlist.append(entry.path)
                else:
                    filename = entry.name.lower()
                    if filename.endswith('.xls') or \
                       filename.endswith('.xlsx'):
                        print("Converting file", entry.name)
                        convert(entry)

workdir = sys.argv[1]
scandir([workdir])


from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardMarkup

from Stuff.ParseMode import *

class InlineButton:
    client: Client

    def __init__(self, text: str, inlineKeyboardMarkup: InlineKeyboardMarkup):
        self._text = text
        self._keyboardMarkup = inlineKeyboardMarkup

    async def SendKeyboardMarkup(self, message):
        return await InlineButton.client.send_message(message.chat.id, self._text,
                                                      reply_markup=self._keyboardMarkup,
                                                      parse_mode=ParseMode.Markdown)

    async def EditKeyboardMarkup(self, callbackQuery: filters.CallbackQuery):
        return await callbackQuery.edit_message_text(self._text,
                                                     reply_markup=self._keyboardMarkup,
                                                     parse_mode=ParseMode.Markdown)


import math as m
import numpy as np
import xarray as xr
from trollimage.xrimage import XRImage
from tqdm import tqdm
import random as r
import os
# import netcdf4
month = ['0'+str(i) for i in range(1,10)]
month.extend([str(i) for i in range(10,13)])
day_31 = ['0'+str(i) for i in range(1,10)]
day_31.extend([str(i) for i in range(10,32)])
day_30 = ['0'+str(i) for i in range(1,10)]
day_30.extend([str(i) for i in range(10,31)])
day_28 = ['0'+str(i) for i in range(1,10)]
day_28.extend([str(i) for i in range(10,29)])
similar_month = r.choice(month)
similar_day = r.choice(day_31)

def sample_new2(array):
    shape_sim = np.shape(array.data)
    x = r.randint(256, shape_sim[2]-256)
    y = r.randint(256, shape_sim[1]-256)
    sliced = array.data.isel(x=slice(x, x+128), y=slice(y, y+128)) # To get teh slice of the array
    nearest = sliced.isel(x=56, y=56) # To get the nearest pixel
    lat, long = nearest.coords['latitude'].data, nearest.coords['longitude'].data # To get the latitude pf nearest pixel
    return sliced.values, lat, long

def array(path):
    similar_month = r.choice(month)
    path = path + similar_month
    if int(similar_month) in [1, 3, 5, 7, 8, 10, 12]:
        similar_day = r.choice(day_31)
    elif int(similar_month) == 2:
        similar_day = r.choice(day_28)
    else:
        similar_day = r.choice(day_30)
    path = path + '/' + similar_day
    files = os.listdir(path)
    nc_path = r.choice(files)
    nc_path = path + '/' + nc_path
    ds = xr.open_dataset(nc_path, )
    img = XRImage(ds['day_microphysics'])
    img.crude_stretch(min_stretch=[2.044909, 1.795136, 236.558919], max_stretch=[78.810258, 22.026770, 300.755732])
    return img

def triplet(path):
    while True:
        try:
            similar = array(path)
        except:
            continue
        if np.shape(similar.data)[1] != 2030:
            continue
        a, lat, long = sample_new2(similar)
        if np.any(np.isnan(a)):
            continue
        else:
            break
    return a, lat, long

# orig_memmap = np.memmap('./storage/climate-memmap/orig_memmap.memmap', dtype='float64', mode='w+', shape=(1000000, 3, 3, 128, 128))
path = '/storage/my-sdsc-storage/nrp/protected/sio/MODIS_Aqua_microphysics_images/2011/'
# for j in tqdm(range(1000000)):
#     # mem_file = np.memmap('./storage/climate-memmap/orig_memmap.memmap',  dtype='float64', mode='r+', shape=(1000000, 3, 3, 128, 128))
#     orig_memmap[j] = triplet(path)

import multiprocessing
import numpy as np
from tqdm import tqdm
import time
import json
# Function to generate triplet data
def generate_triplet(j):
    path = '/storage/my-sdsc-storage/nrp/protected/sio/MODIS_Aqua_microphysics_images/2011/'
    mem_file = np.memmap(
            '/storage/climate-memmap/coordinates_data/data/data_coords_'+str(j)+'.memmap',
            dtype='float64',
            mode='w+',
            shape=(1000, 3, 128, 128))
    coordinates = []
    for i in range(1000):
        mem_file[i], lat, long = triplet(path)
        coordinates.append([float(lat), float(long)])
    # print(coordinates)
    with open('/storage/climate-memmap/coordinates_data/coords/coord_'+str(j)+'.json', 'w') as file:
        json.dump(coordinates, file)

#         pass
start = time.time()
if __name__ == "__main__":
    print(start)
    num_processes = 20  # Number of processes to use
    pool = multiprocessing.Pool(processes=num_processes)

    results = pool.map(generate_triplet, range(num_processes))
print(time.time()-start)


# 2019年8月27日10:49:53剑指offer 面试题63
a=list([9, 11, 5, 7, 16, 1, 4, 2 ])

Last_Min=[]
Last_Max=[]
Cur_Min=0
Cur_Max=[]

for i in range(1,len(a)):
    if(a[i]==a[Cur_Min]):
        continue
    elif(a[i]<a[Cur_Min]):
        if(Cur_Min!=[] and Cur_Max!=[]):
            Last_Min=Cur_Min
            Last_Max=Cur_Max
        Cur_Min=i
        Cur_Max = []
    elif(a[i]>a[Cur_Min]):
        if(Cur_Max==[] or a[i] >a[Cur_Max]):
            Cur_Max=i
        else:
            pass
if(Cur_Min!=[] and Cur_Max!=[]):
    te=a[Cur_Max]-a[Cur_Min]

if(Last_Min!=[] and Last_Max!=[]):
    te2=a[Last_Max]-a[Last_Min]
    te=max(te,te2)
    print(te)


import random
import math

default_max = 512

class Sym:
  def __init__(self):
    self.counts = {}
    self.mode = None
    self.most = 0
    self.n = 0
    self._ent = None

  def bulkAdd(self, values, func = lambda x: x):
    for x in values:
      self.symInc(func(x))

  def symInc(self, value):
    if value == '?':
      return
    self._ent = None
    self.n += 1
    self.counts[value] = self.counts.get(value, 0) + 1
    if self.counts[value] > self.most:
      self.mode, self.most = value, self.counts[value]

  def symDec(self, value):
    self._ent = None
    if self.n > 0:
      self.n -= 1
      self.counts[value] -= 1

  def symEnt(self):
    if not self._ent:
      self._ent = 0
      for _,n in self.counts.items():
        p = n / self.n
        self._ent -= p*math.log(p,2)
    return self._ent


import numpy as np
from utils import assert_right_type
from sklearn.linear_model import LinearRegression
from copy import deepcopy

class Markowitz:
    def __init__(self, data: np.ndarray, rf: np.ndarray = None, logreturns: bool = True, gamma: float = 3.):
        """
        Compute mean-variance optimization with sample covariance matrix for the case of N risky assets.
        The risk-free rate 'rf' is utilized just for the optimization of excess returns.

        :param data: T-by-N price matrix with assets on axis=1 and time series on axis=0.
        :param rf: Risk-free rate. If the purpose is to optimize excess returns, then provide rf.
        :param logreturns: Log-return if True, else simple return.
        """
        assert_right_type(data, np.ndarray)
        self.__data: np.ndarray = data  # (T, N)
        # log_prices: np.ndarray = np.log1p(data)  # (T, N)
        # self.__log_returns: np.ndarray = np.diff(log_prices, axis=0)  # (T, N-1)
        return_type: dict = {True: Markowitz._log_returns, False: Markowitz._simple_returns}

        assert_right_type(gamma, float)
        self.__gamma = gamma
        self.__t: int = data.shape[0]
        self.__n: int = data.shape[1]
        if isinstance(rf, np.ndarray):
            assert_right_type(rf, np.ndarray)
            self.__rf: np.ndarray = rf  # (T-1, 1)
        else:  # rf = None
            self.__rf: np.ndarray = np.zeros((self.__t - 1, 1))  # (T-1, 1)

        # Log or simple returns; in excess of the risk-free rate if passed as argument.
        self.__returns: np.ndarray = return_type[logreturns](self.__data, self.__rf)  # (T-1, N)

        # Expected Returns
        self.__mu: np.ndarray = np.apply_along_axis(func1d=np.mean, axis=0, arr=self.__returns)  # shape -> (N,)

        # Sample covariance matrix
        self._sigma = np.cov(self.__returns.T)  # shape -> (N, N)

    @staticmethod
    def _log_returns(prices: np.ndarray, rf: np.ndarray) -> np.ndarray:
        r = np.diff(np.log1p(prices), axis=0) - rf
        # Check if there is an 'inf' left in the matrix
        assert not np.isinf(r).any(), f"'inf' values left in returns."
        return r  # (T-1, N)

    @staticmethod
    def _simple_returns(prices: np.ndarray, rf: np.ndarray) -> np.ndarray:
        # np.nan prevent 'inf' values to happen most of the time, at the end we convert them to 0
        r = np.diff(prices, axis=0) / prices[:-1, :] - rf
        assert not np.isinf(r).any(), f"'inf' values left in returns."
        return r  # (T-1, N)

    def _sigma_inv(self) -> np.ndarray:
        return np.linalg.inv(self._sigma)  # shape -> (N, N)

    def compute(self) -> np.ndarray:
        w_star = (1 / self.__gamma) * np.matmul(self._sigma_inv(), self.__mu)
        return deepcopy(w_star)  # shape -> (N,)

    def data(self) -> np.ndarray:
        return deepcopy(self.__data)

    def returns(self):
        return deepcopy(self.__returns)

    # def log_returns(self) -> np.ndarray:
    #     return self.__log_returns

    def mu(self) -> np.ndarray:
        return deepcopy(self.__mu)

class MarkowitzShrinkedConstantCorrelation(Markowitz):  # Constant Correlation
    def __constant_correlation(self):
        cor_matrix: np.ndarray = np.corrcoef(self.returns().T)
        mean_cor: np.ndarray = np.mean(cor_matrix)
        rows, columns = cor_matrix.shape
        f: np.ndarray = np.zeros((rows, columns))
        # Far from being optimized
        for i in range(rows):
            for j in range(columns):
                f[i][j] = mean_cor * np.sqrt(cor_matrix[i][i] * cor_matrix[j][j])
        return f

    def _sigma_inv(self) -> np.ndarray:
        lambda_ = .2
        f = self.__constant_correlation()
        shrinked = f * lambda_ + (1 - lambda_) * self._sigma
        return np.linalg.pinv(shrinked)

class MarkowitzShrinkedIdentity(Markowitz):  # Identity Matrix
    def _sigma_inv(self) -> np.ndarray:
        lambda_ = .2
        f = np.identity(self._sigma.shape[0])
        shrinked = f * lambda_ + (1 - lambda_) * self._sigma
        return np.linalg.pinv(shrinked)

class MarkowitzShrinkedSI(Markowitz):  # Single Index
    def __market_portfolio(self):
        data = self.data()
        log = np.log1p(data)
        diff = np.diff(log, axis=0)
        market = np.apply_along_axis(np.sum, 1, diff)
        return market

    def __f(self):
        market = self.__market_portfolio()
        returns = self.returns()
        reg = LinearRegression().fit(returns, market)
        pred = reg.predict(returns)
        residuals = market - pred
        residuals_variance = np.fromiter(
            (np.cov(returns[:, i], residuals)[0, 1] for i in range(returns.shape[1])),
            dtype=float)
        D = np.identity(residuals_variance.shape[0]) * residuals_variance
        s_2 = market.var()
        beta = reg.coef_
        return s_2 * beta * beta.reshape((-1, 1)) + D

    def _sigma_inv(self) -> np.ndarray:
        lambda_ = .2
        shrinked = self.__f() * lambda_ + (1 - lambda_) * self._sigma
        return np.linalg.pinv(shrinked)


# -*- coding: utf-8 -*-
"""
Created on Mon Oct  7 14:47:42 2019

@author: ali hussain
"""

from sklearn.feature_extraction.text import TfidfVectorizer
corpus=['This is the first document.',
        'This document is the second document .',
        'And this is the third one .',
        'Is this the first document ?']
v=TfidfVectorizer()
X=v.fit_transform(corpus)

print(X)

print(X.shape)

v.get_feature_names()

X.toarray()


from itertools import repeat
from typing import Union

from discord.ext import commands

from discord_bot.config import BotConfig

def get_help(
    command: Union[commands.Command, commands.Group],
    subcommands=False,
    show_hidden=False,
):
    """
    A util function that walks through all commands recursivey and returns help str.
    """
    if not show_hidden and command.hidden:
        return ""

    prefix = BotConfig.prefix
    if not isinstance(prefix, str):
        prefix = prefix[0]

    help_text = f"{prefix}{command.qualified_name} - {command.help}"

    aliases = command.aliases
    if len(aliases) > 0:
        help_text += f"\nCommand aliases - {', '.join(aliases)}\n"

    if isinstance(command, commands.Group):
        if subcommands:
            command_list = [command, *command.walk_commands()]
            return "\n\n".join(map(get_help, command_list))
        subcommand_names = ", ".join([x.name for x in command.walk_commands()])
        help_text += f"Command Subcommands - {subcommand_names}"

    return help_text

def get_help_cog(cog: commands.Cog, subcommands=False, show_hidden=False):
    """A util function to get help text for a cog"""
    commands = cog.get_commands()
    return map(get_help, commands, repeat(subcommands), repeat(show_hidden))


import numpy as np
import matplotlib.pyplot as plt

def airfoil(c, t, a, b):
    steps = 100
    x = np.linspace(0, c, steps)
    T = t/0.2*(0.2969*np.sqrt(x)-0.1260*x-0.3516*x**2+0.2843*x**3-0.1015*x**4)
    Z = a*((b-1)*x**3-b*x**2+x)
    Z_upper = Z + 0.5*T
    Z_lower = Z - 0.5*T
    return Z_upper, Z_lower, x

c = 1
t = 0.12
a = 0.1
b = 0.1

Z_upper, Z_lower, x = airfoil(c, t, a, b)
plt.plot(x, Z_upper)
plt.plot(x, Z_lower)
plt.show()


import os
import subprocess
import sys

path = r'G:\My Drive\Ninjavan Project\Send_report_Bot_id_52717207'

list_run = []

all_files = os.listdir(path)
items_not_run = ['Reco.bat', 'start_everything.bat']
bat_files = [bat for bat in all_files if bat.endswith('.bat')]

for items in bat_files:
    if 'test' in items or 'once' in items or 'quick' in items:
        items_not_run.append(items)

# remove files(not run) from run_list
for items in items_not_run:
    bat_files.remove(items)

for n in bat_files:
    subprocess.Popen(f'{path}/{n}', creationflags=subprocess.CREATE_NEW_CONSOLE)  # , cwd=f'{path}/'


import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Output, Input

from homepage import get_header, get_navbar, get_emptyrow

app = dash.Dash(__name__)
app.config['suppress_callback_exceptions']=True

server = app.server

externalgraph_colstyling = {
    'border-radius' : '10px',
    'border-style' : 'solid',
    'border-width' : '1px',
    'border-color' : 'green',
    'background-color' : 'green',
    'box-shadow' : '2px 5px 5px 1px rgba(0, 100, 0, .5)',
    'padding-top' : '10px'
}

externalgraph_rowstyling = {
    'margin-left' : '15px',
    'margin-right' : '15px'
}

def plrev_App():
    return html.Div([
        ##############################
        #Row 1 : Header
        get_header(),

        #Row 2 : Nav bar
        get_navbar('pl_rev'),

        #####################
        #Row 3 : Map, Instructions
        get_emptyrow(),
        html.Div([
            html.Div([
            ],
                className = 'col-1', #Blank column
            ),
            html.Div([ #External 10-column
                html.H4(children='Revenue Per License by County',
                style={'color': 'white', 'text-align': 'center'}),
                html.Div([ #Internal Row
                    html.Div([
                        dcc.Graph('plrev-map')
                    ],
                        className='col-8'
                    ),
                    html.Div([
                        dcc.Markdown('''CLICK ON GREEN SHADED COUNTIES to show revenue, license count, revenue per license, county ranking, and revenue change from 2019 to 2020. Counties are shaded to indicate realative revenue per license, with darker shades representing higher values. 2019 is the most recent year for license information.''',
                        style={'color': 'white'})
                    ],
                        className='col-4'
                    ),
                ],
                    className='row'
                ),
                get_emptyrow(),
                html.Div([
                    html.Div([
                        html.Div(id='pl-info')
                    ],
                        className='col-7'
                    ),
                ],
                    className='row'
                ),
            ],
                className='col-10',
                style = externalgraph_colstyling, # External 10-column
            ),
            html.Div([
            ],
                className = 'col-1', #Blank column
            ),
        ],
        className='row',
        style=externalgraph_rowstyling, # External row
        ),
        get_emptyrow(),
        html.Div(id='pl-data', style={'display': 'none'}),
    ])

app.layout = plrev_App


import numpy as np
m=5
n=3
"""
Without the loss of generality  we can take m>=n. If m<n then supply Tranpose of input matrix.
as A=U.S.Trans(V)
A'=U'.S'.Trans(V)'=Trans(A)=V.Trans(S).Trans(U)
This program works for any mXn real matrix
"""
TOL=1e-7
#A=np.array([0,1,1,0,1,0,1,1,0,0,1,0,1,0,1]).reshape(m,n)
A=np.array([0,1,1,0,1,0,1,1,0,0,1,0,1,0,1]).reshape(m,n)
#A=U(mxm)S(mxn)V^T(nxn)
S=np.zeros([m,n])
print("Input matrix A is\n",A)
N=np.transpose(A)@A #nxn matrix
M=A@np.transpose(A)#mxm matrix

nevl,nevec = np.linalg.eigh(N)
mevl,mevec = np.linalg.eigh(M)
print(mevl)
#print(nevl)
mevl=np.flip(mevl)
nevl=np.flip(nevl)

for i in range(0,n):
	if(np.abs(nevl[i])>TOL):
		S[i][i]=np.sqrt(nevl[i])

print("The S matrix:\n",S)
V=np.zeros([n,n])
U=np.zeros([n,n])
for i in range(0,n):
 V[:,i]=nevec[:,n-i-1]

#U=A@V/np.sqrt(nevl)
U=A@V
for i in range(0,n):
	if(np.abs(nevl[i])>TOL):
		U[:,i]=U[:,i]/np.sqrt(nevl[i])
	else:
		U[:,i]=U[:,i]/np.sqrt(np.dot(U[:,i],U[:,i]))

vec=mevec[:,np.arange(0,m-n)]
U=np.column_stack((U,vec))

print("V transpose is\n",np.transpose(V))
print("U is \n",U)
print("*******For checking*******\nU.S.Trans(V)\n",U@S@np.transpose(V))
print("Input matrix is\n",A)
print(
"""
From inbuilt function in Numpy
"""
)

u, s, vh = np.linalg.svd(A, full_matrices=True)

smat = np.zeros((m, n))

smat[:n, :n] = np.diag(s)

#print(smat)
print("Trans(V) is \n",vh)
print("U is \n",u)
print("S is \n",smat)
print("U.S.Trans(V)\n",u@smat@vh)

"""
Comparing the results from two methods we see that S matrix is same, V matrix is Same and U is different. But combining U,S and V we obtain the correct input matrix A(within tolerence) from both the methods. Timing measurements are done in SVD_Time.py
"""


"""Defines the consumers that handle connections based on the request paths."""

from django.urls import re_path

from . import consumers

websockets_urlpatterns = [
    re_path(
        r"ws/chat/(?P<conversation_id>[a-zA-Z0-9_-]+)/$",
        consumers.ConversationConsumer.as_asgi(),
    ),
]


# -*- coding: utf-8 -*-
"""
Created on Fri Feb  7 21:05:12 2020

@author: shari
"""

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import  astropy.constants as c

data = pd.read_table("synthetic_trilegal.dat", sep="\s+")
df = pd.DataFrame(data)

B = df.iloc[:, 12]
V = df.iloc[:, 13]
L = df.iloc[:, 4]
mbol = df.iloc[:, 10]
distmod = df.iloc[:, 7]
T_eff = df.iloc[:, 5]

M_bol11 = -2.5*(L+np.log10(c.L_sun/c.L_bol0))
M_bol1 = -2.5*L
M_bol2 = mbol-distmod
M_bol_err = np.sqrt((M_bol1-M_bol2)**2)
Col = B-V

plt.figure()
plt.plot(Col, M_bol1, "r.")
#plt.plot(Col, M_bol11, "y.")
plt.gca().invert_yaxis()
plt.grid()
plt.title("Colour Magnitude Diagram")
plt.ylabel("Absolute Magnitude")
plt.xlabel("B-V Colour")

plt.figure()
plt.plot(Col, mbol, "r.")
plt.gca().invert_yaxis()
plt.grid()
plt.title("Colour Magnitude Diagram")
plt.ylabel("Apparent Bolometric Magnitude")
plt.xlabel("B-V Colour")

plt.figure()
axes = plt.gca()
plt.plot(T_eff, L, "r.")
plt.gca().invert_xaxis()
plt.grid()
axes.set_xlim([4.6,3.4])
plt.title("Hertzsprung Russell Diagram")
plt.ylabel("Luminosity $log(L/L_{\odot})$")
plt.xlabel("Effective Temperature $log(T_{eff})$")


# -*- coding: utf-8 -*-
# 15-15.py

class Student:

    def __init__(self, name, gender, height):
        self.__name = name
        self.__gender = gender
        self.__height = height

    # property 선언은 읽기 전용
    @property
    def name(self):
        return self.__name

    @property
    def gender(self):
        return self.__gender

    @property
    def height(self):
        return self.__height

    # property 에 setter 선언 까지 하면 읽기에 쓰기 전용
    @height.setter
    def height(self, height):
        self.__height = height

    # 대부분의 객체를 출력 할 때에 많이 사용하는 함수
    # 객체의 표현을 지원해 주는 '리프리젠테이션' __repr__ 사용
    def __repr__(self):
        return "{0}(name: {1}, gender: {2}, height: {3})"\
            .format(self.__class__.__name__, self.name, self.gender, self.height)

students = [
    Student("홍길동", "남", 176.5),
    Student("이순신", "남", 188.5),
    Student("유관순", "여", 158.4),
    Student("강감찬", "남", 182.2)
]

for student in students:
    print(student)

# sorted 함수는 반복가능한 객체 대상 사용자 정의
# 객체 사용 시 해당 리스트 객체에 있는 각 항목에서 키를 사용한 정보 전달
# sorted(반복 가능 객체, key=(기준이 되는 key 값), reverse=False(기본값))

print("name으로 오름차순 정렬 후 ===>")
for student in sorted(students, key=lambda x: x.name):
    print(student)

print("name으로 내림차순 정렬 후 ===>")
for student in sorted(students, key=lambda x: x.name, reverse=True):
    print(student)

print("height로 오름차순 정렬 후 ===>")
for student in sorted(students, key=lambda x: x.height):
    print(student)

print("height로 내림차순 정렬 후 ===>")
for student in sorted(students, key=lambda x: x.height, reverse=True):
    print(student)