#!/usr/bin/env python
# Program: DNS Domain Expiration Checker
# Author: Matty < matty at prefetch dot net >
# Current Version: 9.1
# Date: 01-27-2020
# License:
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.

import sys
import time
import argparse
import smtplib
import dateutil.parser
import subprocess
from datetime import datetime
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

EXPIRE_STRINGS = [ "Registry Expiry Date:",
                   "Expiration:",
                   "Domain Expiration Date",
                   "Registrar Registration Expiration Date:",
                   "expire:",
                   "expires:",
                   "Expiry date"
                 ]

REGISTRAR_STRINGS = [
                      "Registrar:"
                    ]

DEBUG = 0

def debug(string_to_print):
    """
       Helper function to assist with printing debug messages.
    """
    if DEBUG:
        print(string_to_print)

def print_heading():
    """
       Print a formatted heading when called interactively
    """
    print("%-25s  %-20s  %-30s  %-4s" % ("Domain Name", "Registrar",
          "Expiration Date", "Days Left"))

def print_domain(domain, registrar, expiration_date, days_remaining):
    """
       Pretty print the domain information on stdout
    """
    print("%-25s  %-20s  %-30s  %-d" % (domain, registrar,
          expiration_date, days_remaining))

def make_whois_query(domain):
    """
       Execute whois and parse the data to extract specific data
    """
    debug("Sending a WHOIS query for the domain %s" % domain)
    try:
        p = subprocess.Popen(['whois', domain],
                             stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    except Exception as e:
        print("Unable to Popen() the whois binary. Exception %s" % e)
        sys.exit(1)

    try:
        whois_data = p.communicate()[0]
    except Exception as e:
        print("Unable to read from the Popen pipe. Exception %s" % e)
        sys.exit(1)

    # TODO: Work around whois issue #55 which returns a non-zero
    # exit code for valid domains.
    # if p.returncode != 0:
    #    print("The WHOIS utility exit()'ed with a non-zero return code")
    #    sys.exit(1)

    return(parse_whois_data(whois_data))

def parse_whois_data(whois_data):
    """
       Grab the registrar and expiration date from the WHOIS data
    """
    debug("Parsing the whois data blob %s" % whois_data)
    expiration_date = "00/00/00 00:00:00"
    registrar = "Unknown"

    for line in whois_data.splitlines():
        if any(expire_string in line for expire_string in EXPIRE_STRINGS):
            expiration_date = dateutil.parser.parse(line.partition(": ")[2], ignoretz=True)

        if any(registrar_string in line for registrar_string in
               REGISTRAR_STRINGS):
            registrar = line.split("Registrar:")[1].strip()

    return expiration_date, registrar

def calculate_expiration_days(expire_days, expiration_date):
    """
       Check to see when a domain will expire
    """
    debug("Expiration date %s Time now %s" % (expiration_date, datetime.now()))

    try:
        domain_expire = expiration_date - datetime.now()
    except:
        print("Unable to calculate the expiration days")
        sys.exit(1)

    if domain_expire.days < expire_days:
        return domain_expire.days
    else:
        return 0

def check_expired(expiration_days, days_remaining):
    """
       Check to see if a domain has passed the expiration
       day threshold. If so send out notifications
    """
    if int(days_remaining) < int(expiration_days):
        return days_remaining
    else:
        return 0

def domain_expire_notify(domain, config_options, days):
    """
       Functions to call when a domain is about to expire. Adding support
       for Nagios, SNMP, etc. can be done by defining a new function and
       calling it here.
    """
    debug("Triggering notifications for the DNS domain %s" % domain)

    # Send outbound e-mail if a rcpt is passed in
    if config_options["email"]:
        send_expire_email(domain, days, config_options)

def send_expire_email(domain, days, config_options):
    """
       Generate an e-mail to let someone know a domain is about to expire
    """
    debug("Generating an e-mail to %s for domain %s" %
         (config_options["smtpto"], domain))
    msg = MIMEMultipart()
    msg['From'] = config_options["smtpfrom"]
    msg['To'] = config_options["smtpto"]
    msg['Subject'] = "The DNS Domain %s is set to expire in %d days" % (domain, days)

    body = "Time to renew %s" % domain
    msg.attach(MIMEText(body, 'plain'))

    smtp_connection = smtplib.SMTP(config_options["smtpserver"],config_options["smtpport"])
    message = msg.as_string()
    smtp_connection.sendmail(config_options["smtpfrom"], config_options["smtpto"], message)
    smtp_connection.quit()

def processcli():
    """
        parses the CLI arguments and returns a domain or
        a file with a list of domains.
    """
    parser = argparse.ArgumentParser(description='DNS Statistics Processor')

    parser.add_argument('--domainfile', help="Path to file with list of domains and expiration intervals.")
    parser.add_argument('--domainname', help="Domain to check expiration on.")
    parser.add_argument('--email', action="store_true", help="Enable debugging output.")
    parser.add_argument('--interactive',action="store_true", help="Enable debugging output.")
    parser.add_argument('--expiredays', default=10000, type=int, help="Expiration threshold to check against.")
    parser.add_argument('--sleeptime', default=60, type=int, help="Time to sleep between whois queries.")
    parser.add_argument('--smtpserver', default="localhost", help="SMTP server to use.")
    parser.add_argument('--smtpport', default=25, help="SMTP port to connect to.")
    parser.add_argument('--smtpto', default="root", help="SMTP To: address.")
    parser.add_argument('--smtpfrom', default="root", help="SMTP From: address.")

    # Return a dict() with all of the arguments passed in
    return(vars(parser.parse_args()))

def main():
    """
        Main loop
    """
    days_remaining = 0
    conf_options = processcli()

    if conf_options["interactive"]:
        print_heading()

    if conf_options["domainfile"]:
        with open(conf_options["domainfile"], "r") as domains_to_process:
            for line in domains_to_process:
                try:
                     domainname, expiration_days = line.split()
                except Exception as e:
                    print("Unable to parse configuration file. Problem line \"%s\"" % line.strip())
                    sys.exit(1)

                expiration_date, registrar = make_whois_query(domainname)
                days_remaining = calculate_expiration_days(expiration_days, expiration_date)

                if check_expired(expiration_days, days_remaining):
                    domain_expire_notify(domainname, conf_options, days_remaining)

                if conf_options["interactive"]:
                    print_domain(domainname, registrar, expiration_date, days_remaining)

                # Need to wait between queries to avoid triggering DOS measures like so:
                # Your IP has been restricted due to excessive access, please wait a bit
                time.sleep(conf_options["sleeptime"])

    elif conf_options["domainname"]:
        expiration_date, registrar = make_whois_query(conf_options["domainname"])
        days_remaining = calculate_expiration_days(conf_options["expiredays"], expiration_date)

        if check_expired(conf_options["expiredays"], days_remaining):
            domain_expire_notify(conf_options["domainname"], conf_options, days_remaining)

        if conf_options["interactive"]:
            print_domain(conf_options["domainname"], registrar, expiration_date, days_remaining)

        # Need to wait between queries to avoid triggering DOS measures like so:
        # Your IP has been restricted due to excessive access, please wait a bit
        time.sleep(conf_options["sleeptime"])

if __name__ == "__main__":
    main()


#!/bin/python3

import math
import os
import random
import re
import sys
import pandas as pd
import numpy as np

pd.set_option('display.max_rows', 10)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

#
# Complete the 'case1' function below.
#
# The function accepts STRING_ARRAY fp_data as parameter.
#

def case1(financial_data):
    print(financial_data.iloc[:5,])
    print(financial_data.iloc[-5:,])
    print(financial_data.describe())
    # Print First 5 rows of MSFT
    # Print Last 5 rows of MSFT
    # Print Describe MSFT

def case2(financial_data):
    re = financial_data.resample('M').mean()
    print(re.iloc[:5,])
    #Resample to monthly data
    #Display the first 5 rows

def case3(financial_data):
    daily_close = pd.DataFrame()
    daily_close['Adj Close'] = (financial_data['Adj Close'].shift(-1)- financial_data['Adj Close'])/financial_data['Adj Close']
    daily_close = daily_close[:-1]
    daily_close.index=financial_data.index[1:]
    print(daily_close)
    # Create a variable daily_close and copy Adj Close from financial_data
    # Print daily returns

def case4(financial_data):
    daily_close = pd.DataFrame()
    daily_close['Adj Close'] = (financial_data['Adj Close'].shift(-1)- financial_data['Adj Close'])/financial_data['Adj Close']
    daily_close = daily_close[:-1]
    daily_close.index=financial_data.index[1:]
    daily_close['Adj Close'][0] += 1
    for i in range(1,len(daily_close['Adj Close'])):
        daily_close['Adj Close'][i] = daily_close['Adj Close'][i-1] * (daily_close['Adj Close'][i]+1)
    print(daily_close)
    # Calculate the cumulative daily returns
    # Print it

def case5(financial_data):
    daily_close = pd.DataFrame()
    daily_close['Adj Close'] = (financial_data['Adj Close'].shift(-1)- financial_data['Adj Close'])/financial_data['Adj Close']
    daily_close = daily_close[:-1]
    daily_close.index=financial_data.index[1:]
    daily_close['Adj Close'][0] += 1
    for i in range(1,len(daily_close['Adj Close'])):
        daily_close['Adj Close'][i] = daily_close['Adj Close'][i-1] * (daily_close['Adj Close'][i]+1)
    re = daily_close.resample('M').mean()
    print(re)
    # Resample the cumulative daily return to cumulative monthly return

def case6(financial_data):
    am_20 = pd.DataFrame()
    am_20['Adj Close'] = financial_data['Adj Close'].rolling(window = 20).mean()
    print(am_20)

    # Isolate the adjusted closing prices and store it in a variable
    # Calculate the moving average for a window of 20
    # Display the last 20 moving average number

def case7(financial_data):
    am_100 = pd.DataFrame()
    am_100['Adj Close'] = financial_data['Adj Close'].pct_change().rolling(window = 100).std()*10
    print(am_100)
    # Calculate the volatility for a period of 100 don't forget to multiply by square root
    # don't forget that you need to use pct_change

def case8(financial_data):

    # Initialize the short rolling window (window=50)
    short_window = 50
    # Initialize the long rolling window (window=100)
    long_window = 100

    # You will create a signals dataframe
    # using the index of financial_data
    signals = pd.DataFrame(index = financial_data.index)

    # You will assign 0 to the column signal of the dataframe signals
    signals['signal'] = 0
    # Create short simple moving average over the short window
    signals['short_mavg'] = financial_data['Close'].rolling(window = short_window,min_periods = 1,center = False).mean()
    # Create long simple moving average over the long window
    signals['long_mavg'] = financial_data['Close'].rolling(window = long_window,min_periods = 1,center = False).mean()
    # You will not populate the value 1 when the small window moving average
    # is higher than the long window moving average else 0
    signals['signal']= np.where(signals['short_mavg']>signals['long_mavg'],1.0,0.0)

    # Generate trading orders by inserting in a new column orders
    # 1 if it is a buy order -1 if it is a sell order
    # you should just use the diff command on the column signal
    signals['orders'] = signals['signal'].diff()
    # Print the dataframe signals
    print(signals)

def case9(financial_data):

    # You will need to use the dataframe signals
    signals = pd.DataFrame(index = financial_data.index)
    signals['signal'] = 0
    signals['short_mavg'] = financial_data['Close'].rolling(window = 50,min_periods = 1,center = False).mean()
    signals['long_mavg'] = financial_data['Close'].rolling(window = 100,min_periods = 1,center = False).mean()
    signals['signal']= np.where(signals['short_mavg']>signals['long_mavg'],1.0,0.0)
    signals['orders'] = signals['signal'].diff()

    # You are going to set your initial amount of money you want
    # to invest --- here it is 10,000
    initial = float(10000.0)

    # You are going to create a new dataframe positions
    # Remember the index is still the same as signals
    positions = pd.DataFrame(index = signals.index).fillna(0.0)

    # You are going to buy 10 shares of MSFT when signal is 1
    # You are going to sell 10 shares of MSFT when signal is -1
    # You will assign these values to the column MSFT of the
    # dataframe positions
    positions['MSFT'] = 10 * signals['signal']
    # You are now going to calculate the notional (quantity x price)
    # for your portfolio. You will multiply Adj Close from
    # the dataframe containing prices and the positions (10 shares)
    # You will store it into the variable portfolio
    portfolio = pd.DataFrame(index = positions.index)
    portfolio['MSFT'] = positions.multiply(financial_data['Adj Close'],axis = 0)

    # Add `holdings` to portfolio
    portfolio['holdings'] = (positions.multiply(financial_data['Adj Close'],axis = 0)).sum(axis = 1)

    # You will store positions.diff into pos_diff
    pos_diff = positions.diff()
    # You will now add a column cash in your dataframe portfolio
    # which will calculate the amount of cash you have
    # initial_capital - (the notional you use for your different buy/sell)
    portfolio['cash'] = initial - (pos_diff.multiply(financial_data['Adj Close'],axis = 0).sum(axis = 1).cumsum())
    portfolio['total'] = portfolio['cash']+portfolio['holdings']
    portfolio['returns'] = portfolio['total'].pct_change()

    # You will now add a column total to your portfolio calculating the part of holding
    # and the part of cash

    # Add `returns` to portfolio

    # Print the first lines of `portfolio`
    print(portfolio)

if __name__ == '__main__':
    case_number=input().strip()
    df = pd.read_csv(sys.stdin, header=0, index_col='Date', parse_dates=True)
    globals()['case'+case_number](df)


# coding=utf-8
import unittest
from time import sleep

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

import sys
print (sys.path)

class MyTestCase(unittest.TestCase):

    def setUp(self):

        self.driver = webdriver.Chrome()

    def test_something(self):
        driver = self.driver
        driver.get("https://us.ceair.com/zh/flight-result.html")
        self.assertIn(u"中国东方航空", driver.title, "中国东方航空 is not in title")
        Departure=driver.find_element_by_xpath("//*[@id='statusform']")
        Departure.click()
        Departure.send_keys(u"上海")
        list=driver.find_elements_by_xpath("//*[@class='autocomplete-list-box']/div[@class='search-menu-container']/li/div/div")
        sleep(2)
        Destination=driver.find_element_by_xpath("//*[@id='findRouteMain']/div[2]/div[1]/form/div[3]/div/input")
        Destination.click()
        Destination.send_keys(Keys.DOWN)
        Destination.send_keys(Keys.DOWN)
        sleep(2)
        submit=driver.find_element_by_xpath("//*[@id='findRouteMain']/div[2]/div[2]/button")
        submit.click()
        sleep(5)

    def tearDown(self):
        self.driver.close()

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


import subprocess
import os
# import time

print("\nPython simulation\n")

# blond_dir = '/afs/cern.ch/work/k/kiliakis/git/cpu-gpu-bench/'
blond_dir = '/home/kiliakis/git/cpu-gpu-bench/'

exe_dir = blond_dir + 'benchmarks/fft/'
# exe_list = ['matrixconvolve-cuda.py']
exe_list = ['matrixconvolve-cuda.py']
# outfiles = blond_dir + 'results/fftconvolve/matrixconvolve-bench-v1/'
outfiles = blond_dir + 'results/fftconvolve/matrixconvolve-breakdown-p100/'

if not os.path.exists(outfiles):
    os.makedirs(outfiles)

n_particles_list = ['100000', '200000', '500000',
                    '1000000', '2000000', '5000000']
# n_particles_list = ['5000000']
n_turns_list = ['100']
repeats = 1
os.chdir(exe_dir)

total_sims = len(n_particles_list) * len(exe_list) * len(n_turns_list) * repeats

current_sim = 0
for exe in exe_list:
    for n_turns in n_turns_list:
        for n_particles in n_particles_list:
            name = 'n_p-' + n_particles + '-'
            # if not os.path.exists(outfiles + exe + '/'):
            # os.makedirs(outfiles + exe + '/')
            # res = open(outfiles + name+'.res', 'w')
            stdout = open(outfiles + name+'.txt', 'w')
            for i in range(0, repeats):
                print(n_particles, i)
                # start = time.time()
                subprocess.call(['python', exe, n_turns, n_particles],
                                stdout=stdout,
                                stderr=stdout,
                                env=os.environ.copy()
                                )
                # end = time.time()
                current_sim += 1
                # res.write(str(end-start)+'\n')
                print("%lf %% is completed" %
                      (100.0 * current_sim / total_sims))


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

   File Name：     hasGroupsSizeX
   Author :       jing
   Date：          2020/3/27

   https://leetcode-cn.com/problems/x-of-a-kind-in-a-deck-of-cards/

   卡牌分组
"""

class Solution:
    def hasGroupsSizeX(self, deck) -> bool:
        if deck is None or len(deck) == 0:
            return False
        import collections
        len_deck = len(deck)
        count = collections.Counter(deck)
        for X in range(2, len_deck+1):
            if len_deck % X == 0:
                if all(v % X == 0 for v in count.values()):
                    return True
        return False


from django.contrib.auth import get_user_model
from django_countries.serializers import CountryFieldMixin
from rest_framework import serializers

from core.serializers.image_fallback_mixin import ImageFallbackMixin

User = get_user_model()

class AnonymousUserSerializer(
    CountryFieldMixin, ImageFallbackMixin, serializers.ModelSerializer
):
    id = serializers.ReadOnlyField()

    class Meta:
        model = User
        fields = (
            "id",
            "first_name",
            "last_name",
            "description",
            "city",
            "image",
            "company_name",
        )
        image_fallback_fields = [
            ("image", "image_url"),
            ("id_photo", "id_photo_url"),
            ("business_license", "business_license_url"),
        ]


# 2019.1.16
# encoder take only x as input

import matplotlib
import matplotlib.pyplot as plt
import rdkit
from rdkit import Chem
from rdkit.Chem import Draw
import h5py
import pdb
import numpy as np
import argparse
import random
import sys, os
import torch
from torch.autograd import Variable
from torch.nn.parameter import Parameter
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.optim.lr_scheduler import ReduceLROnPlateau
from tqdm import tqdm

sys.path.append('%s/util/' % os.path.dirname(os.path.realpath('__file__')))
from cmd_args import cmd_args
import cfg_parser as parser

# parameters to be specified
#cmd_args.save_dir = './vanilla_supervised_vae'
#cmd_args.training_data_dir = '/work/molecule_generation/sdvae/mol_vae_clean/zinc_data_experiments/data/data_278'

cmd_args.output_dim = 1
cmd_args.batch_size = 300
cmd_args.num_epochs= 500
cmd_args.output_dim = 1
cmd_args.kl_coeff = 1
seed = 19260817

class Prepare_data():
    def __init__(self, cmd_args):
        self.data_dir = cmd_args.training_data_dir + '/QM9_clean_smi_train.h5'
        self.label_dir = cmd_args.training_data_dir + '/QM9_normalized_train_y.npy'

    def load_data(self):
        h5f = h5py.File(self.data_dir, 'r')
        all_true_binary = h5f['x_train'][:]
        all_rule_masks = h5f['masks_train'][:]
        h5f.close()
        train_label = np.load(self.label_dir)
        num_val = min([10000, int(all_true_binary.shape[0] * 0.1)])
        return all_true_binary[num_val:], all_rule_masks[num_val:], all_true_binary[0:num_val], all_rule_masks[0:num_val] , train_label[num_val:,2], train_label[0:num_val,2]

## define the model
sys.path.append('%s/util/' % os.path.dirname(os.path.realpath('__file__')))
from mol_util import rule_ranges, terminal_idxes, DECISION_DIM
from train_util import PerpCalculator
from train_util import raw_logit_to_smile_labels, run_job

class MolVAE(nn.Module):
    def __init__(self):
        super(MolVAE, self).__init__()
        self.latent_dim = cmd_args.latent_dim
        self.encoder = CNNEncoder(max_len=cmd_args.max_decode_steps, latent_dim=cmd_args.latent_dim)
        self.state_decoder = StateDecoder(max_len=cmd_args.max_decode_steps, latent_dim=cmd_args.latent_dim)
        self.perp_calc = PerpCalculator()

    def reparameterize(self, mu, logvar):
        if self.training:
            eps = mu.data.new(mu.size()).normal_(0, cmd_args.eps_std)
            if cmd_args.mode == 'gpu':
                eps = eps.cuda()
            eps = Variable(eps)

            return mu + eps * torch.exp(logvar * 0.5)
        else:
            return mu

    def forward(self, x_inputs, y_inputs, true_binary, rule_masks, t_y):
        z_mean, z_log_var = self.encoder(x_inputs)
        z = self.reparameterize(z_mean, z_log_var)
        raw_logits = self.state_decoder(z, y_inputs, n_step = None)
        perplexity = self.perp_calc(true_binary, rule_masks, raw_logits)
        kl_loss = -0.5 * torch.sum(1 + z_log_var - z_mean ** 2 - torch.exp(z_log_var), -1)

        return perplexity, cmd_args.kl_coeff * torch.mean(kl_loss)

from pytorch_initializer import weights_init
#q(z|x)
class CNNEncoder(nn.Module):
    def __init__(self, max_len, latent_dim):
        super(CNNEncoder, self).__init__()
        self.latent_dim = latent_dim
        self.max_len = max_len

        self.conv1 = nn.Conv1d(DECISION_DIM, 9, 9)
        self.conv2 = nn.Conv1d(9, 9, 9)
        self.conv3 = nn.Conv1d(9, 10, 11)

        self.last_conv_size = max_len - 9 + 1 - 9 + 1 - 11 + 1
        self.w1 = nn.Linear(self.last_conv_size * 10, 435)
        self.mean_w = nn.Linear(435, latent_dim)
        self.log_var_w = nn.Linear(435, latent_dim)
        weights_init(self)

    def forward(self, x_cpu):
        if cmd_args.mode == 'cpu':
            batch_input = Variable(torch.from_numpy(x_cpu))
        else:
            batch_input = Variable(torch.from_numpy(x_cpu).cuda())

        h1 = self.conv1(batch_input)
        h1 = F.relu(h1)
        h2 = self.conv2(h1)
        h2 = F.relu(h2)
        h3 = self.conv3(h2)
        h3 = F.relu(h3)

        # h3 = torch.transpose(h3, 1, 2).contiguous()
        flatten = h3.view(x_cpu.shape[0], -1)
        h = self.w1(flatten)
        h = F.relu(h)

        z_mean = self.mean_w(h)
        z_log_var = self.log_var_w(h)

        return (z_mean, z_log_var)

#decoder
class StateDecoder(nn.Module):
    def __init__(self, max_len, latent_dim):
        super(StateDecoder, self).__init__()
        self.latent_dim = latent_dim + cmd_args.output_dim
        self.max_len = max_len

        self.z_to_latent = nn.Linear(self.latent_dim, self.latent_dim)
        if cmd_args.rnn_type == 'gru':
            self.gru = nn.GRU(self.latent_dim, 501, cmd_args.output_dim)
        elif cmd_args.rnn_type == 'sru':
            self.gru = SRU(self.latent_dim, 501, cmd_args.output_dim)
        else:
            raise NotImplementedError

        self.decoded_logits = nn.Linear(501, DECISION_DIM)
        weights_init(self)

    def forward(self, z, y, n_step):

        if n_step is None:
            n_step = self.max_len

        if cmd_args.mode == 'cpu':
            y = torch.tensor(y).float()
        else:
            y = torch.tensor(y).cuda().float()

        assert len(z.size()) == 2 # assert the input is a matrix
        h = self.z_to_latent(torch.cat((z, y.view(y.shape[0],1)), 1))
        h = F.relu(h)
        rep_h = h.expand(n_step, z.size()[0], z.size()[1] + cmd_args.output_dim) # repeat along time steps
        out, _ = self.gru(rep_h) # run multi-layer gru
        logits = self.decoded_logits(out)
        return logits

# train vae model for one epoch
sys.path.append('%s/util/' % os.path.dirname(os.path.realpath('__file__')))
from pytorch_initializer import weights_init
from train_util import get_batch_input_vae
#q(z|x,y)
def epoch_train(phase, epoch, ae, sample_idxes, data_binary, data_masks, data_property, cmd_args, optimizer_vae=None):
    total_loss = []
    pbar = tqdm(range(0, (len(sample_idxes) + (cmd_args.batch_size - 1) * (optimizer_vae is None)) // cmd_args.batch_size), unit='batch')

    if phase == 'train' and optimizer_vae is not None:
        ae.train()
    else:
        ae.eval()

    n_samples = 0
    for pos in pbar:
        selected_idx = sample_idxes[pos * cmd_args.batch_size : (pos + 1) * cmd_args.batch_size]
        x_inputs, y_inputs,v_tb, v_ms, t_y = get_batch_input_vae(selected_idx, data_binary, data_masks, data_property)  # no grad for evaluate mode.
        loss_list = ae.forward(x_inputs, y_inputs,v_tb,v_ms, t_y)
        loss_vae = loss_list[0] + loss_list[1]

        perp = loss_list[0].data.cpu().numpy()[0] # reconstruction loss
        kl = loss_list[1].data.cpu().numpy()

        minibatch_vae_loss = loss_vae.data.cpu().numpy()
        pbar.set_description('At epoch: %d  %s vae loss: %0.5f perp: %0.5f kl: %0.5f' % (epoch, phase, minibatch_vae_loss, perp, kl))

        if optimizer_vae is not None:
            assert len(selected_idx) == cmd_args.batch_size
            optimizer_vae.zero_grad()
            loss_vae.backward(retain_graph=True)
            optimizer_vae.step()

        total_loss.append(np.array([minibatch_vae_loss, perp, kl]) * len(selected_idx))

        n_samples += len(selected_idx)

    if optimizer_vae is None:
        assert n_samples == len(sample_idxes)

    total_loss = np.array(total_loss)

    avg_loss = np.sum(total_loss, 0) / n_samples
    return ae, avg_loss

def main():
	random.seed(seed)
	np.random.seed(seed)
	torch.manual_seed(seed)
	torch.cuda.manual_seed_all(seed)

	if not os.path.exists(cmd_args.save_dir):
	    os.makedirs(cmd_args.save_dir)

	cmd_args.saved_model = cmd_args.save_dir + '/epoch-best.model'

	sys.path.append('%s/util/' % os.path.dirname(os.path.realpath('__file__')))
	getting_data = Prepare_data(cmd_args)
	train_binary_x, train_masks, valid_binary_x, valid_masks, train_y, valid_y = getting_data.load_data()
	print('num_train: %d\tnum_valid: %d' % (train_y.shape[0], valid_binary_x.shape[0]))

	ae = MolVAE()

	if cmd_args.mode == 'gpu':
	    ae = ae.cuda()

	kl = []
	prep = []

	assert cmd_args.encoder_type == 'cnn'
	optimizer_vae = optim.Adam(ae.parameters(), lr=cmd_args.learning_rate)
	lr_scheduler = ReduceLROnPlateau(optimizer_vae, 'min', factor=0.5, patience=5, verbose=True, min_lr=0.001)

	sample_idxes = list(range(train_binary_x.shape[0]))
	best_valid_loss = None

	for epoch in range(cmd_args.num_epochs):
	    random.shuffle(sample_idxes)

	    ## update the vae

	    ae, vae_loss = epoch_train('train',epoch, ae, sample_idxes, train_binary_x, train_masks, train_y,cmd_args, optimizer_vae)
	    print('>>>>average \033[92mtraining\033[0m of epoch %d: loss %.5f perp %.5f kl %.5f' % (epoch, vae_loss[0], vae_loss[1], vae_loss[2]))
	    kl.append(vae_loss[2])
	    prep. append(vae_loss[1])

	    if epoch % 1 == 0:
	        _, valid_loss = epoch_train('valid', epoch,  ae, list(range(valid_binary_x.shape[0])), valid_binary_x, valid_masks, valid_y,cmd_args)
	        print('>>>>average \033[93mvalid\033[0m of epoch %d: loss %.5f perp %.5f kl %.5f' % (epoch, valid_loss[0], valid_loss[1], valid_loss[2]))
	        valid_loss = valid_loss[0]
	        lr_scheduler.step(valid_loss)
	        torch.save(ae.state_dict(), cmd_args.save_dir + '/epoch-%d.model' % epoch)

	        if best_valid_loss is None or valid_loss < best_valid_loss:
	            best_valid_loss = valid_loss
	            print('saving to best model since this is the best valid loss so far.----')
	            torch.save(ae.state_dict(), cmd_args.save_dir + '/epoch-best.model')

	np.save('./kl.npy', kl)
	np.save('./prep.npy', prep)

if __name__ == '__main__':
   main()


from datetime import datetime, time
from dateutil.relativedelta import relativedelta
from pytz import timezone, utc

from odoo import api, fields, models, _
from odoo.exceptions import ValidationError

class ResourceCalendarLeaves(models.Model):
    _name = "resource.calendar.leaves"
    _description = "Resource Time Off Detail"
    _order = "date_from"

    def default_get(self, fields_list):
        res = super().default_get(fields_list)
        if 'date_from' in fields_list and 'date_to' in fields_list and not res.get('date_from') and not res.get('date_to'):
            # Then we give the current day and we search the begin and end hours for this day in resource.calendar of the current company
            today = fields.Datetime.now()
            user_tz = timezone(self.env.user.tz or self._context.get('tz') or self.company_id.resource_calendar_id.tz or 'UTC')
            date_from = user_tz.localize(datetime.combine(today, time.min))
            date_to = user_tz.localize(datetime.combine(today, time.max))
            intervals = self.env.company.resource_calendar_id._work_intervals_batch(date_from.replace(tzinfo=utc), date_to.replace(tzinfo=utc))[False]
            if intervals:  # Then we stop and return the dates given in parameter
                list_intervals = [(start, stop) for start, stop, records in intervals]  # Convert intervals in interval list
                date_from = list_intervals[0][0]  # We take the first date in the interval list
                date_to = list_intervals[-1][1]  # We take the last date in the interval list
            res.update(
                date_from=date_from.astimezone(utc).replace(tzinfo=None),
                date_to=date_to.astimezone(utc).replace(tzinfo=None)
            )
        return res

    name = fields.Char('Reason')
    company_id = fields.Many2one(
        'res.company', string="Company", readonly=True, store=True,
        default=lambda self: self.env.company, compute='_compute_company_id')
    calendar_id = fields.Many2one('resource.calendar', 'Working Hours', domain="[('company_id', 'in', [company_id, False])]", check_company=True, index=True)
    date_from = fields.Datetime('Start Date', required=True)
    date_to = fields.Datetime('End Date', compute="_compute_date_to", readonly=False, required=True, store=True)
    resource_id = fields.Many2one(
        "resource.resource", 'Resource', index=True,
        help="If empty, this is a generic time off for the company. If a resource is set, the time off is only for this resource")
    time_type = fields.Selection([('leave', 'Time Off'), ('other', 'Other')], default='leave',
                                 help="Whether this should be computed as a time off or as work time (eg: formation)")

    @api.depends('calendar_id')
    def _compute_company_id(self):
        for leave in self:
            leave.company_id = leave.calendar_id.company_id or self.env.company

    @api.depends('date_from')
    def _compute_date_to(self):
        user_tz = timezone(self.env.user.tz or self._context.get('tz') or self.company_id.resource_calendar_id.tz or 'UTC')
        for leave in self:
            date_to_tz = user_tz.localize(leave.date_from) + relativedelta(hour=23, minute=59, second=59)
            leave.date_to = date_to_tz.astimezone(utc).replace(tzinfo=None)

    @api.constrains('date_from', 'date_to')
    def check_dates(self):
        if self.filtered(lambda leave: leave.date_from > leave.date_to):
            raise ValidationError(_('The start date of the time off must be earlier than the end date.'))

    @api.onchange('resource_id')
    def onchange_resource(self):
        if self.resource_id:
            self.calendar_id = self.resource_id.calendar_id

    def _copy_leave_vals(self):
        self.ensure_one()
        return {
            'name': self.name,
            'date_from': self.date_from,
            'date_to': self.date_to,
            'time_type': self.time_type,
        }


import random

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        myDict = {}

        # Create a map that maps number to its position (ID)
        for i in range(len(nums)):
            if not nums[i] in myDict:
                myDict[nums[i]] = []
            myDict[nums[i]].append(i)

        # Now search for a pair that adds up to target
        for i in range(len(nums)):
            n = nums[i]
            m = target - n
            if m in myDict:
                for q in myDict[m]:
                    if q != i:
                        return [i, q]

        return [-1, -1]

if __name__ == "__main__":
    sol = Solution()

    #nums = random.sample(range(20), 20)
    #target = 20

    #print (nums)
    print (sol.twoSum([3, 2, 4], 6))


#uwu?
import mesa
import random
import numpy as np
import math

def get_mission(pos, boxes):
    dist = 100
    assigned_box = None
    for box in boxes:
        manhattan_dist = abs(pos[0] - box.pos[0]) + abs(pos[1] - box.pos[1])
        if dist > manhattan_dist:
            dist = manhattan_dist
            assigned_box = box
    boxes.remove(assigned_box)
    return assigned_box

def get_distance(p, q):
    """ Returns euclidean distance from A to B"""
    return math.sqrt((q[1] - p[1])**2 + (q[0] - p[0])**2)

class Almacen (mesa.Model):
    def __init__(self, width, height, num_boxes) -> None:

        # Model Variables Shared Between Agents
        self.shared_map = [[[0] * height] * width]
        self.boxes = []
        self.dropoff_pos = (0,0)
        self.end_pos = (width-1, height-1)
        self.ordered_boxes = []
        self.num_boxes = num_boxes
        self.robot_moves = 0
        self.step_num = 0

        # Creating Grid
        self.grid = mesa.space.MultiGrid(width, height, False)

        # Creating Scheduler
        self.schedule = mesa.time.BaseScheduler(self)

        # Inserting Agents
        for i in range(num_boxes):
            box = Box(f"box_{i}", self)
            pos = (random.randint(0, width-1), random.randint(0, width-1))
            while (len(self.grid.get_cell_list_contents(pos)) == 1 or pos[0] == 0 or pos[1] == 0):
                pos = (random.randint(0, width-1), random.randint(0, width-1))
            self.grid.place_agent(box, pos)
            self.boxes.append(box)

        for i in range(5):
            robot = Robot(f"robot_{i}", self)
            pos = (random.randint(0, width-1), random.randint(0, width-1))
            while (len(self.grid.get_cell_list_contents(pos)) == 1):
                pos = (random.randint(0, width-1), random.randint(0, width-1))
            self.grid.place_agent(robot, pos)
            self.schedule.add(robot)

        self.data = mesa.DataCollector(
            {
                "Steps": Almacen.steps,
                "RobotMoves": Almacen.robot_moves,
            }
        )

    def step(self):
        if (len(self.ordered_boxes) < self.num_boxes):
            self.schedule.step()
            self.step_num += 1
        else:
            print("Steps", self.step_num)
            print("Robot moves", self.robot_moves)
        self.data.collect(self)

    @staticmethod
    def steps(model):
        return model.step_num

    @staticmethod
    def robot_moves(model):
        return model.robot_moves

class Box (mesa.Agent):
    def __init__(self, unique_id, model):
        super().__init__(unique_id, model)
        self.can_be_grabbed = True

class Robot (mesa.Agent):
    def __init__(self, unique_id, model):
        super().__init__(unique_id, model)
        self.box = None
        self.assigned = None

    def get_route(self):
        if (isinstance(self.box, Box)):
            pass
        else:
            pass

    def grab(self, agent):
        agent.can_be_grabbed = False
        self.box = agent
        self.assigned = None

    def drop(self):
        self.model.ordered_boxes.append(self.box)
        self.model.grid.move_agent(self.box, self.model.dropoff_pos)
        if (len(self.model.grid.get_cell_list_contents(self.model.dropoff_pos)) == 5):
            self.model.dropoff_pos = (self.model.dropoff_pos[0], self.model.dropoff_pos[1] + 1)
        self.box = None

    def step(self):
        objective = self.pos

        if (self.assigned == None and self.box == None):
            if (len(self.model.boxes) == 0):
                # Move towards end position
                objective = self.model.end_pos
                self.dir = False
            else:
                # Get a box assignation and moves towards it
                self.assigned = get_mission(self.pos, self.model.boxes)
                objective = self.assigned.pos
                if (abs(self.pos[0] - objective[0]) + abs(self.pos[1] - objective[1]) == 1):
                    box = self.model.grid.get_cell_list_contents(objective)
                    self.grab(box[0])
                    return
        else:
            if (self.box == None):
                # Move towards assigned box
                objective = self.assigned.pos
                if (abs(self.pos[0] - objective[0]) + abs(self.pos[1] - objective[1]) == 1):
                    box = self.model.grid.get_cell_list_contents(objective)
                    self.grab(box[0])
                    return
            else:
                # Move towards dropoff location
                objective = self.model.dropoff_pos
                if (abs(self.pos[0] - objective[0]) + abs(self.pos[1] - objective[1]) == 1):
                    self.drop()
                    return

        possible_steps = self.model.grid.get_neighborhood(
            self.pos, moore=False, include_center=False
        )

        depurated_steps = []

        for steps in possible_steps:
            searching = self.model.grid.get_cell_list_contents([steps])
            cannot_use_step = False

            if len(searching) > 0:
                print(f":::--Agent found in {steps}")
                for agent in searching:
                    if len(self.model.ordered_boxes) != self.model.num_boxes:
                        if isinstance(agent, Robot) or isinstance(agent, Box):
                            cannot_use_step = True
                        else:
                            print("Can use this step!")
                            break
                            # cannot move and exit
            if cannot_use_step:
                continue
            else:
                depurated_steps.append(steps)

        if (len(depurated_steps) == 0):
            self.model.robot_moves -= 1
            return

        min = 2000
        best = []

        for opts in depurated_steps:
            x_i, y_i = opts
            x_j, y_j = objective

            new_point = [x_i, y_i]
            objective = [x_j, y_j]

            aux = get_distance(new_point, objective)

            if (aux < min):
                best = new_point
                min = aux

        print(f"The best possible distance is {min}")
        self.model.grid.move_agent(self, tuple(e for e in best))

        if (objective != self.model.end_pos):
            self.model.robot_moves += 1

        try:
            self.model.grid.move_agent(self.box, tuple(e for e in best))
        except:
            pass


"""
   Step 1 in the analysis: get drought events, and save the indices to *.npy

   Input files: soil moisture anomalies, and land masking files over western and eastern Mediterranean.
   Requires a pre-process soil moisture data, interpolated at 70cm, anomalies calculated, and separated by west and east Mediterranean.

"""

import numpy as np
from netCDF4 import Dataset

from drought_estimate import drought_calculation, drought_sorting

maindir = './'
savedir = './'

"""
   Input information
"""

model='GISS-E2-R'
region='wmedi'
mean_type='ymean'  ## read smean or ymean
period = '0850-1949'
varname='mrlsl'

"""
   Reading soil moisture and land mask for west and east regions.
"""

#==== Read the soil file

# West
reg_all_west = 'west.'+mean_type+'.anomaly.'+model+'.SOILIQ.'+period+'.nc'

print(reg_all_west)

reg_all_east = 'east.'+mean_type+'.anomaly.'+model+'.SOILIQ.'+period+'.nc'
print(reg_all_east)

var_tmp_west = Dataset(maindir + reg_all_west,'r')
series_all_west = var_tmp_west.variables[varname][:]
series_all_west = np.squeeze(series_all_west)

reg_mean_west = 'regmean.' + reg_all     # this is the regional averaged time series
var_tmp2_west = Dataset(maindir + reg_mean_west, 'r')
series_reg_west = var_tmp2_west.variables[varname][:]
series_reg_west = np.squeeze(series_reg_west)

# East
var_tmp_east = Dataset(maindir + reg_all_east,'r')
series_all_east = var_tmp_east.variables[varname][:]
series_all_east = np.squeeze(series_all_east)

reg_mean_east = 'regmean.' + reg_all     # this is the regional averaged time series
var_tmp2_east = Dataset(maindir + reg_mean_east, 'r')
series_reg_east = var_tmp2_east.variables[varname][:]
series_reg_east = np.squeeze(series_reg_east)

#==== Reading the land mask file

landfile_west = maindir + model + 'west.landfile.nc'
var_land_west = Dataset(landfile_west,'r')
landmass_west = var_land_west.variables['land'][:]
landmass_west = np.squeeze(landmass_west)

landfile_east = maindir + model + 'east.landfile.nc'
var_land_east = Dataset(landfile_east, 'r')
landmass_east = var_land_east.variables['land'][:]
landmass_east = np.squeeze(landmass_east)

#==== Drought estimation

Drought_west = drought_calculation(series_reg_west, series_all_west, landmass_west, reg_per=60.)
Drought_east = drought_calculation(series_reg_east, series_all_east, landmass_east, reg_per=60.)

sorted_indices = drought_sorting(Drought_east[0], Drought_east[-2], Drought_west[0], Drought_west[-2])

emedi_drought = sorted_indices[0]
wmedi_drought = sorted_indices[1]

np.save(savedir+ model +'_east_ymean_0850-1849_sorted_index_lm.npy',emedi_drought)

np.save(savedir+ model +'_west_ymean_0850-1849_sorted_index_lm.npy',wmedi_drought)


import time
import requests
import json
import xml.etree.ElementTree as ET
from collections import OrderedDict

'''
Original Code
https://github.com/ssut/py-hanspell
'''

def _remove_tags(text):
    text = u'<content>{}</content>'.format(text).replace('
','')

    result = ''.join(ET.fromstring(text).itertext())

    return result

def spell_check(text):
    payload = {
            '_callback': 'window.__jindo2_callback._spellingCheck_0',
            'q': text
        }

    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
        'referer': 'https://search.naver.com/',
    }

    base_url = 'https://m.search.naver.com/p/csearch/ocontent/spellchecker.nhn'

    _agent = requests.Session()
    start_time = time.time()
    r = _agent.get(base_url, params=payload, headers=headers)
    passed_time = time.time() - start_time

    r = r.text[42:-2]

    data = json.loads(r)
    html = data['message']['result']['html']
    result = {
        'result': True,
        'original': text,
        'checked': _remove_tags(html),
        'errors': data['message']['result']['errata_count'],
        'time': passed_time,
        'words': OrderedDict(),
    }

    html = html.replace('', '<green>') \
                .replace('', '<red>') \
                .replace('', '<purple>') \
                .replace('', '<blue>') \
                .replace('', '<end>')
    items = html.split(' ')
    words = []
    tmp = ''
    for word in items:
        if tmp == '' and word[:1] == '<':
            pos = word.find('>') + 1
            tmp = word[:pos]
        elif tmp != '':
            word = u'{}{}'.format(tmp, word)

        if word[-5:] == '<end>':
            word = word.replace('<end>', '')
            tmp = ''

        words.append(word)

    result = []
    for word in words:
        if word[:5] == '<red>':
            word = word.replace('<red>', '')
        elif word[:7] == '<green>':
            word = word.replace('<green>', '')
        elif word[:8] == '<purple>':
            word = word.replace('<purple>', '')
        elif word[:6] == '<blue>':
            word = word.replace('<blue>', '')
        result.append(word)

    return ' '.join(result)


import tensorflow
import cv2
import pandas
import numpy as np
import os
import tqdm
from collections import Counter

img_path = './data/image_data/'
model_path = './models/new_image_model.h5'
CATEGORIES = ["non-violence", "blood", "bomb", "explosion", "fighting", "gunshoting", "hitting", "knife"]
values_dict = [3, 5, 3, 3, 3, 4, 2, 2]

img_list = []
IMG_SIZE = 100

def image():
    img_dir = os.listdir(img_path)
    for img in img_dir:

        try:

            img_array = cv2.imread(os.path.join(img_path, img), cv2.IMREAD_GRAYSCALE)

            new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
            new_array = new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)

            img_list.append(new_array)
        except Exception as e:
            pass

    prediction_list = []
    cleaned_list = []
    vals = 0

    model = tensorflow.keras.models.load_model(model_path)

    for data in img_list:

        prediction = model.predict(data)
        # print(prediction)
        pred = np.argmax(prediction, axis=1)
        classes = pred[0]
        prediction_list.append(classes)
    print(prediction_list)
    counter = Counter(prediction_list)
    new_cls = sorted(counter.items())

    for i in range(0, (len(new_cls))):
        val = new_cls[i][1]
        cleaned_list.append(val)

    nonv_score = cleaned_list[0] * values_dict[0]

    for j in range(1, len(cleaned_list)):
        val = cleaned_list[j] * values_dict[j]
        vals = vals + val

    full_score = nonv_score + vals
    full_length = len(prediction_list)
    #print(full_score)

    violent_score = ((full_score - nonv_score) / full_score) * 100

    #print("Violence precentage is: {} %".format("%.2f" % violent_score))
    rounded_final_score = round(violent_score)
    val_frames = (full_length - cleaned_list[0])

    # val_frames = rounded_final_score * 1.5
    # return None
    return violent_score, val_frames, full_length
    # print("Violence precentage is: {} %".format("%.2f" % final_score))


import re

def read_input():
    pts = []
    with open('input', 'r') as f:
        for line in f:
            m = re.match(r'Step (\w) must be finished before step (\w) can begin\.', line)
            pts.append((m.group(1), m.group(2)))
    return pts

class Task:
    def __init__(self, preconditions, name):
        self.preconditions = preconditions
        self.name = name
        self.required_time = (ord(name) - ord('A') + 1) + 60
        self.finished = False

    def can_be_started(self, done):
        for p in self.preconditions:
            if p not in done:
                return False
        return True

    def __hash__(self):
        return ord(self.name)

def make_tasks(order):
    names = set(x[0] for x in order) | set(x[1] for x in order)
    todo = {n: Task([], n) for n in names}
    for t in order:
        todo[t[1]].preconditions.append(t[0])
    return todo

def get_runnable_tasks(tasks, todo, done):
    return list(sorted([t for t in todo if (not tasks[t].finished) and tasks[t].can_be_started(done)]))

def run(tasks, workers):
    todo = set([t.name for t in tasks.values()])
    done = set()
    in_progress = set()
    spare_workers = workers
    runnable = get_runnable_tasks(tasks, todo, done)
    time = 0
    while len(todo) + len(in_progress) > 0:
        while spare_workers > 0 and len(runnable) > 0:
            new_task = runnable.pop(0)
            in_progress.add(new_task)
            todo.remove(new_task)
            spare_workers -= 1
        still_in_progress = set()
        for task_name in in_progress:
            task = tasks[task_name]
            task.required_time -= 1
            if task.required_time == 0:
                task.finished = True
                done.add(task_name)
                spare_workers += 1
            else:
                still_in_progress.add(task_name)
        in_progress = still_in_progress
        runnable = get_runnable_tasks(tasks, todo, done)
        time += 1
    return time

raw_order = read_input()
tasks = make_tasks(raw_order)
result = run(tasks, 5)
print(result)


import torch
from torch import nn
from torch.nn import functional as F
from .fusion_utils import GanGenerator, GanDiscriminator

class AutoFusion(nn.Module):
    """docstring for AutoFusion"""
    def __init__(self, config, input_features):
        super(AutoFusion, self).__init__()
        self.config = config
        self.input_features = input_features

        self.fuse_in = nn.Sequential(
            nn.Linear(input_features, input_features//2),
            nn.Tanh(),
            nn.Linear(input_features//2, config['latent_dim']),
            nn.ReLU()
            )
        self.fuse_out = nn.Sequential(
            nn.Linear(config['latent_dim'], input_features//2),
            nn.ReLU(),
            nn.Linear(input_features//2, input_features)
            )
        self.criterion = nn.MSELoss()

    def forward(self, z):
        compressed_z = self.fuse_in(z)
        loss = self.criterion(self.fuse_out(compressed_z), z)
        output = {
            'z': compressed_z,
            'loss': loss
        }
        return output

class GanFusionSingle(nn.Module):
    """docstring for GanFusionSingle: fusion module for one target modality"""
    def __init__(self,
                 config,
                 generator,
                 discriminator,
                 input_features):
        """
        target_latent: target modality's latent code
        compl_latent: complementary modalities' latent codes (concatenated)
        """
        super(GanFusionSingle, self).__init__()
        self.config = config
        self.auto_fuse = AutoFusion(config,
                                    input_features)
        self.generator = generator
        self.discriminator = discriminator
        self.gen_metrics = None
        self.disc_metrics = None

    def forward(self, z_dict, mode, stage=None):
        # refer to the paper for conventions
        if mode != 'train':
            stage = 'gen'

        if stage == 'disc_real':
            # pass compl_latent through AutoFusion Module
            autofusion_output_dict = self.auto_fuse(self.config,
                                                    z_dict['compl'])
            z_tr = autofusion_output_dict['z']
            auto_fusion_loss = autofusion_output_dict['loss']
            labels = torch.ones_like(z_tr)
            output = self.discriminator(z_tr, labels)
            # add autofusion's loss as well
            output['loss'] += auto_fusion_loss
            output['z_g'] = self.generator(z_dict['target'])['z_g']
            self.disc_metrics = {
                'dral': auto_fusion_loss,
                'drl': output['loss'],
                'dracc': output['accuracy']
            }
        elif stage == 'disc_fake':
            # get the generated latent z_g
            z_g = self.generator(z_dict['target'])['z_g']
            labels = torch.zeros_like(z_g)
            output = self.discriminator(z_g, labels)
            output['z_g'] = z_g
            self.disc_metrics = {
                'dfl': output['loss'],
                'dfacc': output['accuracy']
            }
        elif stage == 'gen':
            output = self.generator(z_dict['target'], self.discriminator)
            self.gen_metrics = {
                'gl': ouptut['loss'],
                'gacc': output['accuracy']
            }
        else:
            raise ValueError(f'invalid stage: {stage}')
        return output

class GanFusion(nn.Module):
    """docstring for GanFusion: contains GAN-Fusion module for all the
    modalities"""
    def __init__(self, config):
        super(GanFusion, self).__init__()
        self.text_gan = GanFusionSingle(config,
                                        GanGenerator(config),
                                        GanDiscriminator(config['latent_dim'],
                                                        config['hidden_dim']))
        self.speech_gan = GanFusionSingle(config,
                                          GanGenerator(config),
                                          GanDiscriminator(config['latent_dim'],
                                                        config['hidden_dim']))
        self.video_gan = GanFusionSingle(config,
                                         GanGenerator(config),
                                         GanDiscriminator(config['latent_dim'],
                                                        config['hidden_dim']))

        self.ff_input_features = config['latent_dim']*3
        self.feed_forward = nn.Sequential(
            nn.Linear(self.ff_input_features, self.ff_input_features//2),
            nn.Tanh(),
            nn.Linear(self.ff_input_features//2, config['latent_dim']),
            nn.ReLU()
            )

    def z_fuse(self, fusion_dict):
        z_fuse_t = fusion_dict['text_dict']['z_g']
        z_fuse_s = fusion_dict['speech_dict']['z_g']
        z_fuse_v = fusion_dict['video_dict']['z_g']
        return self.feed_forward(torch.cat((z_fuse_t,
                                            z_fuse_s,
                                            z_fuse_v), dim=-1))

    def get_loss(self, fusion_dict):
        return fusion_dict['text_dict']['loss'] + \
               fusion_dict['speech_dict']['loss'] + \
               fusion_dict['video_dict']['loss']

    def forward(self, latent_dict, mode, stage=None):
        z_t = latent_dict['z_t']
        z_s = latent_dict['z_s']
        z_v = latent_dict['z_v']

        fusion_dict = {
            'text_dict': self.text_gan({'target': z_t, 'compl': torch.cat((z_s, z_v), dim=-1)}, mode, stage),
            'speech_dict': self.speech_gan({'target': z_s, 'compl': torch.cat((z_t, z_v), dim=-1)}, mode, stage),
            'video_dict': self.video_gan({'target': z_v, 'compl': torch.cat((z_t, z_s), dim=-1)}, mode, stage)
        }

        return {
            'z': self.z_fuse(fusion_dict),
            'loss': self.get_loss(fusion_dict)
        }


import uuid
from datetime import timedelta
from functools import wraps
from typing import Callable, Any, Optional

from flask import request
from flask_login import current_user
from limits.storage import RedisStorage
from werkzeug import exceptions

lock_redis: Optional[RedisStorage] = None

def set_redis_concurrent_lock(redis: RedisStorage):
    global lock_redis
    lock_redis = redis

class _InnerLock:
    def __init__(
        self,
        lock_suffix: Optional[str] = None,
        max_wait_secs: int = 5,
        only_when: Optional[Callable[..., bool]] = None,
    ):
        self.lock_suffix = lock_suffix
        self.max_wait_secs = max_wait_secs
        self.only_when = only_when

    def acquire_lock(self, lock_name: str, lock_value: str):
        if not lock_redis.storage.set(
            lock_name, lock_value, ex=timedelta(seconds=self.max_wait_secs), nx=True
        ):
            raise exceptions.TooManyRequests()

    def release_lock(self, lock_name: str, lock_value: str):
        current_lock_value = lock_redis.storage.get(lock_name)
        if current_lock_value == lock_value.encode("utf-8"):
            lock_redis.storage.delete(lock_name)

    def __call__(self, f: Callable[..., Any]):
        if self.lock_suffix is None:
            lock_suffix = f.__name__
        else:
            lock_suffix = self.lock_suffix

        @wraps(f)
        def decorated(*args, **kwargs):
            if self.only_when and not self.only_when():
                return f(*args, **kwargs)
            if not lock_redis:
                return f(*args, **kwargs)

            lock_value = str(uuid.uuid4())[:10]
            if "id" in dir(current_user):
                lock_name = f"cl:{current_user.id}:{lock_suffix}"
            else:
                lock_name = f"cl:{request.remote_addr}:{lock_suffix}"
            self.acquire_lock(lock_name, lock_value)
            try:
                return f(*args, **kwargs)
            finally:
                self.release_lock(lock_name, lock_value)

        return decorated

def lock(
    name: Optional[str] = None,
    max_wait_secs: int = 5,
    only_when: Optional[Callable[..., bool]] = None,
):
    return _InnerLock(name, max_wait_secs, only_when)


from config import DEFILLAMA_CACHE_OUTPUT, DEFILLAMA_MISSING_OUTPUT
from datetime import datetime, timezone
import csv
import os
import requests
import sys

PRICE_CACHE = []
MISSING_CACHE = []
HEADERS = ["date", "chain", "symbol", "token_id", "price"]

def get_timestamp_from_date(date_str, date_format="%Y-%m-%d %H:%M:%S"):
    """Convert a date string (it assumes UTC) to a timestamp."""
    dt = datetime.strptime(date_str, date_format)
    # Make the datetime object timezone-aware, assuming it's in UTC
    dt = dt.replace(tzinfo=timezone.utc)
    timestamp = int(dt.timestamp())
    return timestamp

def get_date_from_timestamp(timestamp, date_format="%Y-%m-%d %H:%M:%S"):
    """Convert a timestamp to a UTC date string."""
    dt = datetime.fromtimestamp(timestamp, tz=timezone.utc)
    date_str = dt.strftime(date_format)
    return date_str

def check_cache(date, chain, symbol, token_id):
    """Check the cache for a price."""
    for price in PRICE_CACHE:
        if price[0] == date and \
            price[1] == chain and \
            price[3] == token_id:
            return price[4]
    return None

def is_known_missing(date, chain, symbol, token_id) -> bool:
    """Check the missing cache for a price."""
    for price in MISSING_CACHE:
        if price[0] == date and \
            price[1] == chain and \
            price[3] == token_id:
            return True
    return False

def clean_cache(file_path):
    with open(file_path, 'r') as f:
        reader = csv.reader(f)
        data = list(reader)

    # If the CSV has headers, separate them from the data
    headers = data[0]
    data = data[1:]

    # De-duplicate by converting to a set of tuples and then back to a list of lists
    data = [list(item) for item in set(tuple(row) for row in data)]

    # Sort by date, chain, symbol, and token_id
    data.sort(key=lambda x: (x[0], x[1], x[2], x[3]))

    # Write the sorted list back to the CSV file
    with open(file_path, 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(headers)  # Write the headers back
        writer.writerows(data)

def save_cache(date, chain, symbol, token_id, price):
    """Save a price to the cache."""
    PRICE_CACHE.append([
        date,
        chain,
        symbol,
        token_id,
        price
    ])
    with open(DEFILLAMA_CACHE_OUTPUT, "a", newline="") as f:
        writer = csv.writer(f)
        writer.writerow(PRICE_CACHE[-1])

def save_missing(date, chain, symbol, token_id):
    """Save a missing price to the cache."""
    MISSING_CACHE.append([
        date,
        chain,
        symbol,
        token_id,
        ''
    ])
    with open(DEFILLAMA_MISSING_OUTPUT, "a", newline="") as f:
        writer = csv.writer(f)
        writer.writerow([date, chain, symbol, token_id, ''])

def get_price(date, chain, symbol, token_id):
    """
    Get historical prices.

    Chain can be:
    - ethereum
    - avax
    - fantom
    """
    price = check_cache(date, chain, symbol, token_id)
    if price is None and \
        not is_known_missing(date, chain, symbol, token_id):

        price = _get_price(date, chain, symbol, token_id)
        if price is not None:
            save_cache(date, chain, symbol, token_id, price)
        else:
            save_missing(date, chain, symbol, token_id)

    return price

def _get_price(date, chain, symbol, token_id):

    timestamp = get_timestamp_from_date(date)
    url = f"https://coins.llama.fi/prices/historical/{timestamp}/{chain}:{token_id}"
    print(f"GET {url} ({symbol})", end=" => ")
    response = requests.get(url)
    if response.status_code != 200:
        print(f"ERROR: {response.status_code} for {date} {chain} {symbol} {token_id}")
        print(response.text)
        return None
    if "coins" in response.json():
        coins = response.json()["coins"]
        for k,v in coins.items():
            print(f"{response.status_code} / Price: {v['price']}")
            return v["price"]
    print(f"{response.status_code} / No price found")
    return None

if os.path.exists(DEFILLAMA_CACHE_OUTPUT):
    clean_cache(DEFILLAMA_CACHE_OUTPUT)
    with open(DEFILLAMA_CACHE_OUTPUT, "r") as f:
        next(f)
        reader = csv.reader(f)
        for row in reader:
            PRICE_CACHE.append(row)
else:
    with open(DEFILLAMA_CACHE_OUTPUT, "w") as f:
        writer = csv.writer(f)
        writer.writerow(HEADERS)

if os.path.exists(DEFILLAMA_MISSING_OUTPUT):
    clean_cache(DEFILLAMA_MISSING_OUTPUT)
    with open(DEFILLAMA_MISSING_OUTPUT, "r") as f:
        next(f)
        reader = csv.reader(f)
        for row in reader:
            MISSING_CACHE.append(row)
else:
    with open(DEFILLAMA_MISSING_OUTPUT, "w") as f:
        writer = csv.writer(f)
        writer.writerow(HEADERS)


# -*- coding: utf-8 -*-
"""
Created on Fri Mar 31 10:43:22 2017

@author: mghosh
"""

import pandas as pd
import numpy as np

df=pd.read_table(filepath_or_buffer ="//10.8.8.51/lv0/Move to Box/Mithun/projects/9.BR_US_DS_Project/1.data/BR_disc_sens_geo_data.txt",delimiter="|",header=None,names=['customer_key','percent_disc_last_12_mth','br_net_sales_amt_12_mth','flag_employee','addr_zip_code','resp_disc_percent','browse_hit_ind_tot','purchased','br_online_sales12_mth','br_offline_sales_12mth'] ,dtype={'addr_zip_code':'str'})

df["br_retail_percent"]= df["br_offline_sales_12mth"] / df["br_net_sales_amt_12_mth"]

df["br_online_percent"] =  df["br_online_sales12_mth"] / df["br_net_sales_amt_12_mth"]

df=df.replace([np.inf, -np.inf], np.nan)
df["percent_disc_last_12_mth"]=df["percent_disc_last_12_mth"].replace([np.inf, -np.inf], np.nan)
df["percent_disc_last_12_mth"]=df["percent_disc_last_12_mth"].replace(np.nan,0)
#df=df.replace([np.inf, -np.inf], np.nan).dropna( how="all")

df["br_gross_sales_amt"] =  df["br_net_sales_amt_12_mth"] /  df["percent_disc_last_12_mth"].apply(lambda x : 1 - pd.to_numeric(x,errors="ignore"))

pd.Series((1-int(pd.to_numeric(val,errors="ignore"))) for val in df["percent_disc_last_12_mth"])

df["addr_zip_code"].astype("category")

#zip_wise_data = pd.pivot_table(data=df,values=["br_online_sales12_mth","br_offline_sales_12mth","br_net_sales_amt_12_mth","br_gross_sales_amt"],index=["addr_zip_code"] ,aggfunc=np.sum,fill_value =0)

#zip_wise_data = df.groupby(["addr_zip_code"]).sum()

addr_zip_code_column=df["addr_zip_code"].astype("category")

zip_wise_data2 = df.groupby([str("addr_zip_code")],as_index=False)

zip_wise_data3=zip_wise_data2["br_online_sales12_mth","br_offline_sales_12mth","br_net_sales_amt_12_mth"].sum()

BR US DS Model mghosh/3.Documents/Geo Map Plotting.py


#Write a program that gives simple math quizzes. The program should display two random
#numbers that are to be added, such as:
#   6
#  +11
#The program should allow the student to enter the answer.If the answer is correct, a message
#of congratulations should be displayed. If the answer is incorrect, a messge showing the correct
#answer should be displayed.

import random

def random_number():
    random_number1=random.randint(1,250)
    random_number2=random.randint(1,250)
    return random_number1,random_number2

def Question():
    num1,num2=random_number()
    sum_by_user=int(input("What is the sum of "+str(num1)+" and "+str(num2)+" ? Ans: "))
    return sum_by_user==num1+num2
def main():
    if Question()==True:
        print("Congratulations!!!\n\tYour answer is correct! Keep going!")
    else:
        print("Sorry,your answer is wrong!")

main()

Starting Out with Python By Tony Gaddis/5.11.Math Quiz.py


class TemplateBase:
    """
    A class that only accepts kwargs if attributes are pre-defined.
    Automatically binds given kwargs to self
    Usage:
        class NewTemplate(TemplateBase):
            ... define acceptable fields here
    """
    def __init__(self, **kwargs):
        for field_name, field_value in kwargs.items():
            if hasattr(self, field_name):
                _nested_template = getattr(self, field_name)
                if _nested_template and self.__class__.__bases__[0] in _nested_template.__bases__:
                    setattr(self, field_name, _nested_template(**field_value))
                else:
                    setattr(self, field_name, field_value)
            else:
                raise ValueError(
                    f"{self.__class__.__name__} got an "
                    f"unexpected keyword argument: {field_name}"
                )

class ExampleTemplateBase(TemplateBase):
    """
    Only used as an example for nested template definitions
    """
    test_field = None

class FileTemplate(TemplateBase):
    # file_source = None
    file_repository = None
    file_description = None
    file_path = None
    file_dir_path = None
    file_path_length = None
    content = None
    number = None
    metadata = type('FileMetaData', (TemplateBase,), {  # Nested example 1
        "file_name": None,
        "file_size": None,
        "file_source": None,
        "last_access_time": None,
        "create_time": None
    })
    example_field = ExampleTemplateBase  # Nested example 2


'''
2520 is the smallest number that can be divided
by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible
by all of the numbers from 1 to 20?
'''
# coding: utf-8
import numpy as np

TEST_ARRAY = np.arange(1, 10)
TEST_SOLUTION = 2520
VERIFY_ARRAY = np.arange(1, 20)

# print('Test: 2*3*2*5*7*2*3 = {sol}'.format(sol=2*3*2*5*7*2*3))
# print('Solution: 2*3*2*5*7*2*3*11*13*2*17*19 = {sol}'.format(sol=2*3*2*5*7*2*3*11*13*2*17*19))

def smallest_multiple(array):
    sm = 1
    for number in array:
        for test in array:              #  <----------+
            if number % array != 0:     #             |
                sm *= number            #             |
                break                   # breaks this |
            else:
                sm *= (number / test)
    return sm

if __name__ == '__main__':
    # print(smallest_multiple(TEST_ARRAY))
    if smallest_multiple(TEST_ARRAY) == TEST_SOLUTION:
        print('Calculating ...')
        print('Solution: {sol}'.format(sol=smallest_multiple(VERIFY_ARRAY)))
    else:
        print('Program faulty.')


import sys
import os
from pathlib import Path

sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir,'bots'))

MAPS_DIR = Path(os.path.join(os.path.dirname(__file__), "game/maps"))

UP = "up"
DOWN = "down"
LEFT = "left"
RIGHT = "right"
TAKE = "take"
PASS = "pass"

LEVEL = "level"
PORTAL = "portal"
KEY = "key"
DOOR = "door"
PLAYER = "player"
GOLD = "gold"
WALL = "wall"
EMPTY = "empty"

BOT_TILE = 2128

CHUNK = 1024


#!/usr/bin/env python
# coding: utf-8
# jason
# ## 解析式

# 作用：解析式和生成器都是python特有的两个特性，他们能够帮助优化程序，大大减少代码的数量.

# ### 1.生成一个列表，列表0~9，对每一个元素自增1后求平方返回新的列表

# In[31]:

# 一般的写法

lst1 = list(range(10))
lst2 = []

for i in range(10):
    lst1[i] += 1
    square_num = lst1[i]**2
    lst2.append(square_num)
print(lst2)
print(lst1)

# In[32]:

# 根据规律简化代码

lst1 = list(range(10))
lst2 = []

for i in lst1:
    lst2.append((i+1)**2)

print(lst2)

# In[33]:

# 解析式写法
lst1 =list(range(10))
lst2 = [(i+1)**2 for i in lst1]

print(lst1)
print(lst2)

#  **注：能用列表解析式就用列表解释式，这种写法更加python**

# **注：[ 返回值 for 元素 in 可迭代对象 [if条件] ]**
#

# ### 2.获取10 以内的偶数，比较执行效率

# In[34]:

#  普通写法
even = []
for x in range(10):
    if x % 2 == 0:
        even.append(x)

print(even)

# 列表解析式写法
even = []
even = [x for x in range(10) if x % 2 == 0]
print(even)   # print 没有返回值

even = []
even = [print(x) for x in range(10) if x % 2 == 0] # print((x) 是没有返回值的
print(even)   # print 没有返回值

# ### 3. 获取20以内的偶数，并且打印3的倍数. 获取20以内的偶数，并且打印不是3的倍数.

# In[35]:

even = [i for i in range(20) if i % 2 == 0 or i % 3==0]
print(even)

even = [i for i in range(20) if i % 2 == 0 or  not i % 3==0]
print(even)

# ### 4.获取20以内能被2整除也能被3整除的数字

#
# 注：语法2
# [ expr for item in iterable if cond1 if cond2 ]
# 等价于
# ret = []
# for item in iterable:
#       if cond1:
#             if cond2:
#                   ret.append(expr)

# In[36]:

# 一下两种表达方式表达的逻辑是一致的，and等于两个if
even = [i for i in range(20) if i % 2 == 0 and  i %3 == 0]
print(even)

even = [i for i in range(20) if i % 2 == 0 if i %3 == 0]
print(even)

#  [expr for i in iterable1 for j in iterable2]
#  等价于
#  ret = []
#  for i in iterable1:
#     for j in iterable2:
#         ret.append(expr)

# In[37]:

# tuple 嵌套在列表中
[(x,y) for x in 'abcde' for y in range(3)]
# 字典嵌套在列表中
[{x,y} for x in 'abcde' for y in range(3)]
# 列表嵌套在列表中
[[x,y] for x in 'abcde' for y in range(3)]

# In[38]:

# 以下三种方法表达的意义结果一致
# 每个 for 都做好 if 判断
[(i,j) for i in range(7) if i>4 for j in range(20,25) if j >23]
# 先写for，之后再做判断
[(i,j) for i in range(7) for j in range(20,25) if i>4 if j > 23]
# 先写for，之后再判断，if cond1 and cond2   等同于 if cond1 if cond2
[(i,j) for i in range(7) for j in range(20,25) if i>4 and j>23]

# ### 5.返回1-10平方的列表

# In[39]:

[i**2 for i in range(10)]

# ### 6.有一个列表lst=[1,4,9,16,2,5,10,15],生成一个新列表，要求新列表元素是lst相邻2项的和

# In[40]:

lst = [1,4,9,16,2,5,10,15]
lst_length = len(lst)
[lst[i]+lst[i+1] for i in range(lst_length-1)]

# ### 7.打印九九乘法表

# In[41]:

["{}*{}={}".format(i,j,i*j) for i in range(1,10) for j in range(1,10) if i<=j]

# In[3]:

# 'String1'.join('String2'),九九乘法表的横向构造
# 列表套列表的方式实现
print('\n'.join([' '.join(['%s*%s=%-3s' %(x,y,y*x) for x in range(1,y+1)]) for y in range(1,10)]))

# In[1]:

# ()'\n' if i==j else ' '),三目运算符的写法
[print('{}*{}={:<3}{}'.format(j,i,j*i,'\n' if i==j else ' '),end="") for i in range(1,10) for j in range(1,10)]

# ### 8.生成如下格式的ID号，“0001.abadicddws”

# In[18]:

import random

#代码拆解
bytes(range(97,123)).decode() # 获取 26个英文字母
[random.choice(bytes(range(97,123)).decode())] # 在26个英文字母中随机获取一个字符
# 在26个英文字母中随机获取一个字符 ，取10次，但是获取后的格式不是需要的 ['b', 't', 'q', 'z', 'i', 'd', 'o', 'k', 'o', 'g']
[random.choice(bytes(range(97,123)).decode()) for _ in range(10)]
# 用 join() 函数格式化字符串
(''.join([random.choice(bytes(range(97,123)).decode()) for _ in range(10)]))
# 这种写法只是关注次数，但是每一次的值并不关心
# for _ in range(10)

# {:04} 四位有效数字，不够用0填充,系统默认就是右对齐
['{:04}.{}'.format(n,''.join([random.choice(bytes(range(97,123)).decode()) for _ in range(10)])) for n in range(1,101)]

# In[24]:

import random
# 生成随机数random函数有很多的方法
chr(random.randint(97,122))

['{:04}.{}'.format(i,"".join([chr(random.randint(97,122)) for _ in range(10)]))for i in range(1,101)]


import numpy as np
import itertools
import grid2op
import pandas as pd
from grid2op.dtypes import dt_int, dt_float, dt_bool
from grid2op.Exceptions import AmbiguousAction, Grid2OpException
from grid2op.Space import SerializableSpace
from grid2op.Action.BaseAction import BaseAction

LineNum = 20
GenNum = 6
LoadNum = 11
SubNum = 14
ElementNum = 57

n_features = 3

env = grid2op.make("C:/Users/IndigoSix/data_grid2op/l2rpn_case14_sandbox")
subNum = 14
# CumEleNum = np.zeros((1,subNum+1))
# CumEleNum = CumEleNum[0]
CumEleNum = [0]
EleNum = env.action_space.sub_info
for sub_id, num_el in enumerate(env.action_space.sub_info):
    #CumEleNum[sub_id+1] = int(sum(EleNum[:sub_id+1]))
    CumEleNum.append(int(sum(EleNum[:sub_id+1])))
#CumEleNum = list(CumEleNum)
print(len(CumEleNum))

#进行各类设备编号向图数据中索引编号的转换
Line_or_to_Graph = np.full(shape=LineNum, fill_value=0, dtype=dt_int)
for i in range(LineNum):
    subID = env.action_space.line_or_to_subid[i]
    subInternalID = env.action_space.line_or_to_sub_pos[i]
    Line_or_to_Graph[i] = CumEleNum[subID] + subInternalID

Line_ex_to_Graph = np.full(shape=LineNum, fill_value=0, dtype=dt_int)
for i in range(LineNum):
    subID = env.action_space.line_ex_to_subid[i]
    subInternalID = env.action_space.line_ex_to_sub_pos[i]
    Line_ex_to_Graph[i] = CumEleNum[subID] + subInternalID

Gen_to_Graph = np.full(shape=GenNum, fill_value=0, dtype=dt_int)
for i in range(GenNum):
    subID = env.action_space.gen_to_subid[i]
    subInternalID = env.action_space.gen_to_sub_pos[i]
    Gen_to_Graph[i] = CumEleNum[subID] + subInternalID

Load_to_Graph = np.full(shape=LoadNum, fill_value=0, dtype=dt_int)
for i in range(LoadNum):
    subID = env.action_space.load_to_subid[i]
    subInternalID = env.action_space.load_to_sub_pos[i]
    Load_to_Graph[i] = CumEleNum[subID] + subInternalID

print(Line_or_to_Graph)
print(Line_ex_to_Graph)
print(Gen_to_Graph)
print(Load_to_Graph)
np.save("Line_or_to_Graph",Line_or_to_Graph)
np.save("Line_ex_to_Graph",Line_ex_to_Graph)
np.save("Gen_to_Graph",Gen_to_Graph)
np.save("Load_to_Graph",Load_to_Graph)

# GraphX = np.zeros((ElementNum,n_features))#初始化节点特征矩阵
# GraphX[Line_or_to_Graph, 0] = obs.p_or
# GraphX[Line_or_to_Graph, 1] = obs.q_or
# GraphX[Line_or_to_Graph, 2] = obs.v_or
# GraphX[Line_ex_to_Graph, 0] = obs.p_ex
# GraphX[Line_ex_to_Graph, 1] = obs.q_ex
# GraphX[Line_ex_to_Graph, 2] = obs.v_ex
# GraphX[Gen_to_Graph, 0] = obs.prod_p
# GraphX[Gen_to_Graph, 1] = obs.prod_q
# GraphX[Gen_to_Graph, 2] = obs.prod_v
# GraphX[Load_to_Graph, 0] = obs.load_p
# GraphX[Load_to_Graph, 1] = obs.load_q
# GraphX[Load_to_Graph, 2] = obs.load_v
# print(GraphX)


import re

# import sys

def main():
    print(convert(input("Hours: ")))

def review_format(s):
    pattern = r"((1[0-2]|0?[1-9])(:[0-5][0-9])|(1[0-2]|0?[1-9])) (?:AM|PM) to ((1[0-2]|0?[1-9])(:[0-5][0-9])|(1[0-2]|0?[1-9])) (?:AM|PM)"
    result = re.search(pattern, s)

    if result is None:
        raise ValueError("Invalid format")

def convert(s):
    format = review_format(s)

    pattern = r"((1[0-2]|0?[1-9])(:[0-5][0-9])? (?:AM|PM))"
    matches = re.findall(pattern, s)

    hours = [match[0] for match in matches]
    print(hours)
    if ":" in hours[0]:
        start_hour, start_minutes = hours[0].split(":")
        start_hour = convert_hour(start_hour, start_minutes[-2:])
        start_minutes = start_minutes.replace(" AM", "").replace(" PM", "")
    else:
        start_hour = hours[0]
        start_hour = convert_hour(
            start_hour.replace(" AM", "").replace(" PM", ""), start_hour[-2:]
        )
        start_minutes = "00"

    if ":" in hours[1]:
        end_hour, end_minutes = hours[1].split(":")
        end_hour = convert_hour(end_hour, end_minutes[-2:])
        end_minutes = end_minutes.replace(" PM", "").replace(" AM", "")
    else:
        end_hour = hours[1]
        end_hour = convert_hour(
            end_hour.replace(" AM", "").replace(" PM", ""), end_hour[-2:]
        )
        end_minutes = "00"

    result = f"{start_hour}:{start_minutes} to {end_hour}:{end_minutes}"
    # print(result)
    return result

def convert_hour(hour, meridiem):
    # print(hour, meridiem)
    if meridiem == "AM" and hour == "12":
        result = 0
    elif meridiem == "AM" and hour != "12":
        result = int(hour)
    elif meridiem == "PM" and hour != "12":
        result = int(hour) + 12
    else:
        raise ValueError("Invalid start hour")

    # print(result)
    return "{:02d}".format(result)

if __name__ == "__main__":
    main()


import pandas as pd
from unittest import TestCase
from src.analysis.bow import BoW
from src.analysis.clustering.pca import PCA

class TestPCA(TestCase):

  def setUp(self):
    df = pd.read_csv('test/data/tweets.csv')
    bow = BoW(
      data=df['text'],
      language='arabic',
      ngram=(1, 1),
      vocabulary=None,
      no_features=None)

    self.vectors, _ = bow.tf()

  def test_cluster_should_reduce_the_dimensions_of_word_vector(self):
    pca = PCA(bow=self.vectors, no_components=1)

    clusters = pca.cluster()

    self.assertEqual(len(clusters), 5751)
    self.assertEqual(len(clusters[0]), 1)

    pca = PCA(bow=self.vectors, no_components=2)

    clusters = pca.cluster()

    self.assertEqual(len(clusters), 5751)
    self.assertEqual(len(clusters[0]), 2)

  def test_explained_variance_ratio_should_return_the_variance_ratio(self):
    pca = PCA(bow=self.vectors, no_components=5)
    pca.cluster()

    vectors = pca.explained_variance_ratio()

    self.assertEqual(len(vectors), 5)


import os
import sys
import logging
import smtplib
import arcpy
import csv

# Enable data to be overwritten
arcpy.env.overwriteOutput = True

# Set global variables
enableLogging = "false" # Use logger.info("Example..."), logger.warning("Example..."), logger.error("Example...")
logFile = "" # os.path.join(os.path.dirname(__file__), "Example.log")
sendErrorEmail = "false"
emailTo = ""
emailUser = ""
emailPassword = ""
emailSubject = ""
emailMessage = ""
output = None

# Start of main function
def mainFunction(mapInfoFolder,geodatabase,notIncludeConfigFile,renameConfigFile): # Get parameters from ArcGIS Desktop tool by seperating by comma e.g. (var1 is 1st parameter,var2 is 2nd parameter,var3 is 3rd parameter)
    try:
        # --------------------------------------- Start of code --------------------------------------- #

        root_len = len(os.path.abspath(str(mapInfoFolder)))
        # For each of the directories in the folder
        for root, dirs, files in os.walk(str(mapInfoFolder)):
          archive_root = os.path.abspath(root)[root_len:]
          # For each file
          for f in files:
            # Get the file path
            fullpath = os.path.join(root, f)
            fileName = os.path.join(archive_root, f)
            # If it is a tab file
            if fullpath.endswith(".TAB"):
                arcpy.AddMessage("MapInfo TAB file found - " + fullpath + "...")
                arcpy.AddMessage("Importing dataset...")

                noGeometryTest = fullpath + "\\NoGeometry"
                runImport = True
                # Check geometry is invalid
                if arcpy.Exists(noGeometryTest):
                    arcpy.AddWarning("Geometry is invalid...")
                    runImport = False
                # If brackets in file name
                if "(" in fileName:
                    arcpy.AddWarning("Filename is invalid because of \"(\"...")
                    runImport = False
                # If brackets in file name
                if ")" in fileName:
                    arcpy.AddWarning("Filename is invalid because of \")\"...")
                    runImport = False

                # If can continue with import
                if (runImport == True):
                    # Import MapInfo TAB file
                    arcpy.QuickImport_interop("MITAB,\"" + fullpath + "\",\"RUNTIME_MACROS,\"\"FME_TABLE_PASSWORD,,_MITAB_FULL_ENHANCED_GEOMETRY,YES,ENCODING,,EXPOSE_ATTRS_GROUP,,MITAB_EXPOSE_FORMAT_ATTRS,,USE_SEARCH_ENVELOPE,NO,SEARCH_ENVELOPE_MINX,0,SEARCH_ENVELOPE_MINY,0,SEARCH_ENVELOPE_MAXX,0,SEARCH_ENVELOPE_MAXY,0,CLIP_TO_ENVELOPE,NO,_MERGE_SCHEMAS,YES\"\",META_MACROS,\"\"SourceFME_TABLE_PASSWORD,,Source_MITAB_FULL_ENHANCED_GEOMETRY,YES,SourceENCODING,,SourceEXPOSE_ATTRS_GROUP,,SourceMITAB_EXPOSE_FORMAT_ATTRS,,SourceUSE_SEARCH_ENVELOPE,NO,SourceSEARCH_ENVELOPE_MINX,0,SourceSEARCH_ENVELOPE_MINY,0,SourceSEARCH_ENVELOPE_MAXX,0,SourceSEARCH_ENVELOPE_MAXY,0,SourceCLIP_TO_ENVELOPE,NO\"\",METAFILE,MITAB,COORDSYS,,IDLIST,,__FME_DATASET_IS_SOURCE__,true\"", os.path.join(arcpy.env.scratchFolder, "Data.gdb"))

                    arcpy.env.workspace = os.path.join(arcpy.env.scratchFolder, "Data.gdb")
                    featureClassList = arcpy.ListFeatureClasses()
                    # If data imported
                    if (featureClassList):
                        # Loop through the list of feature classes
                        count = 0
                        for featureClass in featureClassList:
                            desc = arcpy.Describe(featureClass)
                            datasetRemoved = False

                            # Get a count for the dataset
                            rowCount = arcpy.GetCount_management(featureClass)
                            if (rowCount == 0) and (datasetRemoved == False):
                                arcpy.AddWarning("Not importing dataset with no records - " + desc.name + "...")
                                arcpy.Delete_management(featureClass, "FeatureClass")
                                datasetRemoved = True

                            # Don't include datasets with "text" in name
                            if ("text" in str(desc.name)) and (datasetRemoved == False):
                                arcpy.AddWarning("Not importing text dataset - " + desc.name + "...")
                                arcpy.Delete_management(featureClass, "FeatureClass")
                                datasetRemoved = True

                            # Remove polygon from feature class name
                            if (("_polygon" in str(desc.name)) and (datasetRemoved == False)):
                                datasetName = desc.catalogPath
                                newDatasetName = datasetName.replace("_polygon", "");

                                renameDataset = True
                                # If already been renamed
                                if arcpy.Exists(newDatasetName):
                                    renameDataset = False

                                if (renameDataset == True):
                                    # Rename feature class
                                    arcpy.AddMessage("Removing polygon from feature class name...")
                                    arcpy.Rename_management(datasetName, newDatasetName, "FeatureClass")

                            # Remove point from feature class name
                            if (("_point" in str(desc.name)) and (datasetRemoved == False)):
                                datasetName = desc.catalogPath
                                newDatasetName = datasetName.replace("_point", "");

                                renameDataset = True
                                # If already been renamed
                                if arcpy.Exists(newDatasetName):
                                    renameDataset = False

                                if (renameDataset == True):
                                    # Rename feature class
                                    arcpy.AddMessage("Removing point from feature class name...")
                                    arcpy.Rename_management(datasetName, newDatasetName, "FeatureClass")

                            # Remove line from feature class name
                            if (("_line" in str(desc.name)) and (datasetRemoved == False)):
                                datasetName = desc.catalogPath
                                newDatasetName = datasetName.replace("_line", "");

                                renameDataset = True
                                # If already been renamed
                                if arcpy.Exists(newDatasetName):
                                    renameDataset = False

                                if (renameDataset == True):
                                    # Rename feature class
                                    arcpy.AddMessage("Removing line from feature class name...")
                                    arcpy.Rename_management(datasetName, newDatasetName, "FeatureClass")

                            count = count + 1

                        # Loop through the list of feature classes
                        featureClassList = arcpy.ListFeatureClasses()
                        for featureClass in featureClassList:
                            copyDataset = True
                            desc = arcpy.Describe(featureClass)

                            # If configuration provided
                            if (notIncludeConfigFile):
                                # Set CSV delimiter
                                csvDelimiter = ","

                                # Look through configuration file to see if any datasets in there to not include
                                # Open the CSV file
                                with open(notIncludeConfigFile, 'rb') as csvFile:
                                    # Read the CSV file
                                    rows = csv.reader(csvFile, delimiter=csvDelimiter)

                                    # For each row in the CSV
                                    count = 0
                                    for row in rows:
                                        # Ignore the first line containing headers
                                        if (count > 0):
                                            # Get the name of the dataset to not include
                                            datasetNotInclude = row[0]

                                            # If the current feature class is in the list
                                            if ((desc.name).lower() == datasetNotInclude.lower()):
                                                arcpy.AddWarning("Not including dataset - " + desc.name + "...")
                                                copyDataset = False
                                        count = count + 1

                                # Look through configuration file to see if any datasets in there to rename
                                # Open the CSV file
                                with open(renameConfigFile, 'rb') as csvFile:
                                    # Read the CSV file
                                    rows = csv.reader(csvFile, delimiter=csvDelimiter)

                                    # For each row in the CSV
                                    count = 0
                                    for row in rows:
                                        # Ignore the first line containing headers
                                        if (count > 0):
                                            # Get the name of the dataset to rename
                                            datasetRename = row[0]
                                            # Name to change dataset to
                                            datasetRenameTo = row[1]

                                            # If the current feature class is in the list
                                            if ((desc.name).lower() == datasetRename.lower()):
                                                datasetName = desc.catalogPath
                                                newDatasetName = datasetName.replace(datasetRename, datasetRenameTo);
                                                featureClass = newDatasetName
                                                arcpy.AddWarning("Renaming " + desc.name + " to " + datasetRenameTo + "...")
                                                arcpy.Rename_management(datasetName, newDatasetName, "FeatureClass")
                                                desc = arcpy.Describe(featureClass)
                                        count = count + 1

                            # If can continue with copy
                            if (copyDataset == True):
                                # Copy in the dataset to the geodatabase
                                arcpy.CopyFeatures_management(featureClass, os.path.join(geodatabase, desc.name))
                    else:
                       arcpy.AddWarning("No datasets to import...")

                    # Delete database
                    arcpy.Delete_management(arcpy.env.workspace, "Workspace")

        # --------------------------------------- End of code --------------------------------------- #

        # If called from gp tool return the arcpy parameter
        if __name__ == '__main__':
            # Return the output if there is any
            if output:
                arcpy.SetParameterAsText(1, output)
        # Otherwise return the result
        else:
            # Return the output if there is any
            if output:
                return output
        # Logging
        if (enableLogging == "true"):
            # Log end of process
            logger.info("Process ended.")
            # Remove file handler and close log file
            logging.FileHandler.close(logMessage)
            logger.removeHandler(logMessage)
        pass
    # If arcpy error
    except arcpy.ExecuteError:
        # Build and show the error message
        errorMessage = arcpy.GetMessages(2)
        arcpy.AddError(errorMessage)
        # Logging
        if (enableLogging == "true"):
            # Log error
            logger.error(errorMessage)
            # Log end of process
            logger.info("Process ended.")
            # Remove file handler and close log file
            logging.FileHandler.close(logMessage)
            logger.removeHandler(logMessage)
        if (sendErrorEmail == "true"):
            # Send email
            sendEmail(errorMessage)
    # If python error
    except Exception as e:
        errorMessage = ""
        # Build and show the error message
        for i in range(len(e.args)):
            if (i == 0):
                errorMessage = unicode(e.args[i]).encode('utf-8')
            else:
                errorMessage = errorMessage + " " + unicode(e.args[i]).encode('utf-8')
        arcpy.AddError(errorMessage)
        # Logging
        if (enableLogging == "true"):
            # Log error
            logger.error(errorMessage)
            # Log end of process
            logger.info("Process ended.")
            # Remove file handler and close log file
            logging.FileHandler.close(logMessage)
            logger.removeHandler(logMessage)
        if (sendErrorEmail == "true"):
            # Send email
            sendEmail(errorMessage)
# End of main function

# Start of set logging function
def setLogging(logFile):
    # Create a logger
    logger = logging.getLogger(os.path.basename(__file__))
    logger.setLevel(logging.DEBUG)
    # Setup log message handler
    logMessage = logging.FileHandler(logFile)
    # Setup the log formatting
    logFormat = logging.Formatter("%(asctime)s: %(levelname)s - %(message)s", "%d/%m/%Y - %H:%M:%S")
    # Add formatter to log message handler
    logMessage.setFormatter(logFormat)
    # Add log message handler to logger
    logger.addHandler(logMessage)

    return logger, logMessage
# End of set logging function

# Start of send email function
def sendEmail(message):
    # Send an email
    arcpy.AddMessage("Sending email...")
    # Server and port information
    smtpServer = smtplib.SMTP("smtp.gmail.com",587)
    smtpServer.ehlo()
    smtpServer.starttls()
    smtpServer.ehlo
    # Login with sender email address and password
    smtpServer.login(emailUser, emailPassword)
    # Email content
    header = 'To:' + emailTo + '\n' + 'From: ' + emailUser + '\n' + 'Subject:' + emailSubject + '\n'
    body = header + '\n' + emailMessage + '\n' + '\n' + message
    # Send the email and close the connection
    smtpServer.sendmail(emailUser, emailTo, body)
# End of send email function

# This test allows the script to be used from the operating
# system command prompt (stand-alone), in a Python IDE,
# as a geoprocessing script tool, or as a module imported in
# another script
if __name__ == '__main__':
    # Arguments are optional - If running from ArcGIS Desktop tool, parameters will be loaded into *argv
    argv = tuple(arcpy.GetParameterAsText(i)
        for i in range(arcpy.GetArgumentCount()))
    # Logging
    if (enableLogging == "true"):
        # Setup logging
        logger, logMessage = setLogging(logFile)
        # Log start of process
        logger.info("Process started.")
    mainFunction(*argv)


from collections import namedtuple

import torch.optim as optim
from tqdm import tqdm

from Loss_Functions import compute_v_loss, ppo_policy_loss
from Neural_Network import Actor, Critic
from Returns_Advantages_Function import compute_return_advantage
from Trajectory_Collector import trajectory_collector

Rewards = namedtuple('Reward', ['reward'])

def ppo_clip(device, env, num_episodes, epsilon, alpha_policy, alpha_value, num_epochs):
    input_size = 1
    output_size = env.action_space.n

    frames = 0

    ppo_policy_network = Actor(input_size, output_size).to(device)
    ppo_value_network = Critic(input_size).to(device)

    optimizer_policy = optim.Adam(ppo_policy_network.parameters(), lr=alpha_policy)
    optimizer_value = optim.Adam(ppo_value_network.parameters(), lr=alpha_value)

    reward_tracker = []

    for episode in tqdm(range(num_episodes)):

        states, actions, rewards, old_action_probs, frame_plus = trajectory_collector(env, output_size,
                                                                                ppo_policy_network, device)
        frames += frame_plus
        returns, advantages = compute_return_advantage(device, rewards, states, ppo_value_network)

        # Update the value network
        optimizer_value.zero_grad()
        for _ in range(num_epochs):
            loss = compute_v_loss(device, states, returns, ppo_value_network)
            loss.backward()

        optimizer_value.step()

        # Update the policy network
        optimizer_policy.zero_grad()

        for _ in range(num_epochs):
            policy_loss = ppo_policy_loss(states, actions, returns, ppo_policy_network, ppo_value_network, epsilon,
                                          old_action_probs, device)
            policy_loss.backward()

        optimizer_policy.step()
        reward_tracker.append(Rewards(reward=sum(rewards)))
        print(f'\n'
              f'{"~" * 40}\n'
              f'Episode: {episode + 1}\n'
              f'Reward: {sum(rewards)}\n'
              f'Frames: {frames}\n'
              f'{"~" * 40}\n')

    return ppo_policy_network, ppo_value_network, reward_tracker


import os
import json
import requests

YKB_OAUTH2_ENDPOINT = 'https://api.yapikredi.com.tr/auth/oauth/v2/token'
YKB_FUND_ENDPOINT = 'https://api.yapikredi.com.tr/api/calculation/v1/fundInformation'
SLACK_WEBHOOK = os.getenv('SLACK_WEBHOOK')

YKB_API_AUTH = {
    'client_id': os.getenv('YKB_CLIENT_ID'),
    'client_secret': os.getenv('YKB_CLIENT_SECRET'),
    'grant_type': 'client_credentials',
    'scope': 'oob'
}

def get_access_token():
    r = requests.post(YKB_OAUTH2_ENDPOINT, data=YKB_API_AUTH)
    if r.status_code != requests.codes.ok:
        r.raise_for_status()
    return r.json()

def get_funds():
    token = get_access_token()
    headers = {'Authorization': f'{token["token_type"]} {token["access_token"]}'}
    r = requests.get(YKB_FUND_ENDPOINT, headers=headers)
    if r.status_code != requests.codes.ok:
        r.raise_for_status()
    return r.json()['response']['return']['fundItemDtoList']

def get_yfay_price():
    funds = get_funds()
    for fund in funds:
        if fund['fundcode'] == 'YFAY1':
            return fund['fundprice']

def post_to_slack(price):
    r = requests.post(SLACK_WEBHOOK, json={'text': f'Bugün itibariyle YFAY1 {price} TL'})
    if r.status_code != requests.codes.ok:
        r.raise_for_status()
    return

if __name__ == '__main__':
    price = get_yfay_price()
    post_to_slack(price)
    exit()


#------------------------------------------------------------
#Project3: Resume parser and downloader (regex file handling)
#-------------------------------------------------------------
import re

out = open("Draft_Resume.txt","r")
print("Resume details: ")
line = out.readlines()
line = str(line)

#print(line)
print("")
print("")

name = re.findall(r"[A-Za-z]*",line)
#print("Name: ",name)

emails = re.findall(r"[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+", line)
print("Email:",emails)

phone = re.findall(r'[\+\(]?[1-9][0-9 .\-\(\)]{8,}[0-9]', line)
print("Moble :",phone)

urls = re.findall('https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', line)
count = len(urls)
for i in range(count):
 print(urls[i])

out.close()


# Change calculator that displays lowest possible amount of banknotes
class Purchase():
    def __init__(self, cost, spending):
        self.cost = cost
        self.spending = spending

    def Change(self):
        change = self.spending - self.cost
        print('\nTotal Amount Purchased: US$', "%.2f" % self.cost)
        print('Amount Received: US$', "%.2f" % self.spending)
        print('Change: US$', "%.2f" % change, end='\n')
        return change

    def Banknotes(self, cash):
        import math
        BanknotesValue = (100.00, 50.00, 20.00, 10.00, 5.00, 2.00, 1.00, 0.50, 0.25, 0.10, 0.05,
            0.01)
        # Converts change to paper or coin money as long as it's larger than the last array value
        while cash >= BanknotesValue[-1]:
            for counter in range(len(BanknotesValue)):
                cash = round(cash, 2)  # Prevents bugs with small values
                if BanknotesValue[counter] <= cash:
                    BanknotesAmount = math.floor(cash * 1/BanknotesValue[counter])
                    cash -= (BanknotesValue[counter] * BanknotesAmount)
                    print(BanknotesAmount, 'x', end=' ')
                    # Checks if it's printed money or not
                    if BanknotesValue[counter] > 0.50:
                        print(f"Paper{'s'[:BanknotesAmount^1]}: US$",
                            "%.2f" % BanknotesValue[counter], end='\n\n')

                    else:
                        print(f"Coin{'s'[:BanknotesAmount^1]}: US$",
                            "%.2f" % BanknotesValue[counter], end='\n\n')

def PurchasePrompt():
    while True:
        try:
            price = float(input('Insert product price: US$ '))
            payment = float(input('Insert amount paid by client: US$ '))

        except ValueError:
            print('ERROR: PLEASE TYPE A NUMERIC VALUE!\n')
            continue

        else:
            if payment < 0.01 or price < 0.01:
                print('ERROR: IMPOSSIBLE PRICE OR PAYMENT VALUE!')
                continue

            elif payment < price:
                print('ERROR: PAYMENT IS NOT ENOUGH TO COMPLETE TRANSACTION!')
                continue

        calc = Purchase(price, payment)
        result = calc.Change()
        calc.Banknotes(result)
        break

PurchasePrompt()

while True:
    choice = input('\nContinue? 1 - Yes/2 - No\n')
    if (choice == '1'):
        PurchasePrompt()
    elif (choice == '2'):
        break