import os
import argparse
from pathlib import Path
from tqdm import tqdm

def get_all_files_in_folder_rec(path: str, file_ext: str):
    files = list(Path(path).rglob(f"*{file_ext}"))
    return files

def decrypt_all_in_foler(args):
    files = get_all_files_in_folder_rec(args.folder, args.ext)

    for f in tqdm(files):
        decrypt_file(f, delete_after=args.delete)

def decrypt_file(path_obj, delete_after: bool = False):
    new_filepath = os.path.join(path_obj.parent, path_obj.stem)
    os.system(f"gpg -o {new_filepath} -d {path_obj}")
    if delete_after:
        os.remove(path_obj)

def encrypt_all_in_folder(args):
    files = get_all_files_in_folder_rec(args.folder, args.ext)

    for f in tqdm(files):
        encrypt_file(f, recipient=args.email, ext=args.ext, delete_after=args.delete)

def encrypt_file(path_obj, recipient: str, ext: str, delete_after: bool = False):
    new_filepath = str(path_obj) + ext
    os.system(f"gpg -o {new_filepath} -a -r {recipient} -e {path_obj}")
    if delete_after:
        os.remove(path_obj)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--email", help="Email of recipient", type=str)
    parser.add_argument("-e", action="store_true", help="Encrypt operations", required=False)
    parser.add_argument("-d", action="store_true", help="Decrypt operations. Note: on using encryption and decryption at the same time, first - decryption.", required=False)
    parser.add_argument("--folder", help="Repository path.", type=str)
    parser.add_argument("--ext", help="GPG file extention. Default = .gpg", required=False, default=".gpg")
    parser.add_argument("--delete", action="store_true", help="Old files will be deleted")
    args = parser.parse_args()

    if (args.d):
        decrypt_all_in_foler(args)
    if (args.e):
        encrypt_all_in_folder(args)

if __name__ == "__main__":
    main()


class Solution(object):
    def maxSubArray(self, nums):
        n = len(nums)
        dp = [0 for i in range(n)]
        dp[0] = nums[0]
        res = dp[0]
        for i in range(1,n):
          if dp[i-1] > 0:
            dp[i] = nums[i] + dp[i-1]
          else:
            dp[i] = nums[i]
          res = max(res,dp[i])
        return res

    def maxSubArray2(self,nums):
      max_sum = nums[0]
      temp_sum = nums[0]
      for index in range(1,len(nums)):
        temp_sum += nums[index]
        if nums[index]> temp_sum:
          temp_sum = nums[index]
        if temp_sum > max_sum:
          max_sum = temp_sum
      return max_sum

app = Solution()
lst = [-2,1,-3,4,-1,2,1,-5,4]
print(app.maxSubArray(lst))


import torch
from torch import nn
import torchvision.models as models
import torch.nn.functional as F
import torchvision.transforms as transforms

class PerceptualLossVgg(nn.Module):
  def __init__(self,
              device=torch.device('cpu'),
              layer=[34, 17]) -> None:
    super().__init__()
    self.vgg = models.vgg19(pretrained=True).features.to(device).eval()
    self.layer_out = {}
    self.fhooks = []
    self.layer = layer
    self.normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])

    for i, l in enumerate(self.vgg):
      if i in self.layer:
        self.fhooks.append(self.vgg[i].register_forward_hook(self._extract_feature_forward_hook(i)))

    # truncate model to the used layer
    self.vgg = self.vgg[0:max(self.layer)+1]

  def forward(self, input, target):
    input, target = self.normalize(input), self.normalize(target)
    self.vgg(input)
    input_feat_map = torch.cat(tuple(self.layer_out.values()))
    self.vgg(target)
    target_feat_map = torch.cat(tuple(self.layer_out.values())).detach()

    return F.l1_loss(input_feat_map, target_feat_map)

  def _extract_feature_forward_hook(self, layer_name):
    def hook(module, input, output):
      self.layer_out[layer_name] = output.view(-1)

    return hook

# Earth mover loss for WGAN type of training
class EMLoss(nn.Module):
  def __init__(self) -> None:
    super().__init__()

  def compute_discriminator_loss(self, real_scores, generated_scores):
    return real_scores.mean() - generated_scores.mean()

  def compute_generator_loss(self, generated_scores):
    return -1.0 * generated_scores.mean()

class TextureConsistencyLoss(nn.Module):
  def __init__(self,
      device,
      texture_layer,
      style_encoder,
      style_bottleneck) -> None:
    super().__init__()
    self.device = device
    self.texture_layer = texture_layer
    self.style_encoder = style_encoder
    self.style_bottleneck = style_bottleneck
    self.loss = nn.MSELoss()

  def forward(self, texture, sketch, generated_images):
    style = generated_images
    for _, layer in enumerate(self.style_encoder):
      _, style = layer(style)

    for _, layer in enumerate(self.style_bottleneck):
      style = layer(style)

    _, style, attn_weights = self.texture_layer(sketch, style)

    return self.loss(texture, style)


### TIPOS DE ESTRUCTURAS DE DATOS DEFAULT EN PYTHON

# Lists, tuples, dictionaries, sets, ranges

# list []

colores:list = ['red', 'brown', 'blue', 'purple']

# colores[0] = 'Red'
# print(colores)

# colores[1] = ['brown1', 'brown2']
# print(colores)

# print(colores)
# print(type(colores))
# print(colores[1])
# print(type(colores[1]))
# print(colores[0][2])
# print(colores[0].upper())

# Métodos para nuestras listas

# print(colores)
# colores.append('black')
# print(colores)
# colores.pop()
# colores.pop()
# print(colores)

# objetos = ['Cano', 21, 'Calistenia']
# print(objetos)
# print(type(objetos[0]))
# print(type(objetos[1]))
# print(type(objetos[2]))

gustos = [
    ['Cano', 21, 'Calistenia'],
    ['Dani', 21, 'Futbol']
]
# gustos.append(['Cano', 21, 'Calistenia'])
# gustos.append(['Dani', 21, 'Futbol'])
gustos.append(['Vicepresi', 19, 'Programar'])
gustos.append(['Angy', 21, 'Bailar'])

gustos[0][2] = 'Calistenia x2'

# print(gustos)
# print(gustos[0], '\n')
# print(gustos[0][2])

# tuple ()
# Las tuplas también son indexadas
# Las tuplas no son modificables

comida:tuple = ('crangreburger', 'ensalada', 'pechuga de pollo')

# print(comida)
# print(type(comida))

# comida[0] = 'Burger' # No se puede realizar, manda error

# dict {:}

cumple = {
    'Cano': 2000,
    'Karen': 2001,
    'Fer': 2002
}

#print(cumple['Kare']) # Esto genera error de tipo de llave

# print(cumple.get('Fernanda'))
# Cuando no escribimos bien
# la llave, nos regresa un dato tipo None

# Agregar elementos al diccionario

cumple.update({
    'Armando': 2003,
    'Kevin': 2000
})

cumple.update({
    2000: ['Cano', 'Kevin']
})

# print(cumple.get(2000))
# print(cumple.get('Kevin'))

# set conjunto {1, 2, 3, 4, 1}
# No tiene indice
# No permite datos repetidos
# No tiene orden

equipos = {'Paris', 'Manches', 'Barca', 'Madrid', 'Barca'}

equipos.add('Bayern')
equipos.add('Paris')
# print(equipos)

# print('green' in colores)
# print('green' not in colores)

# print('Paris' in equipos)

e_eliminados = {'Paris'}

# e_restantes = equipos - e_eliminados

# print('Madrid' not in (equipos - e_eliminados))


# 读取指定的excel文件,将物料属性以list形式存储，并写入json文件。
# 增加读取已有文件功能，编码存在则不写入
#
# -*-coding=utf-8-*-

import openpyxl as xl
import json
from datetime import datetime

# 根据excel 行读取到列表中
def add_item(n):
    code = std(n[1].value)
    draw = std(n[4].value)
    name = std(n[2].value)
    data = std(n[5].value)

    if code and code != '-':
        if code in all_code:
            all_code_rpt[code] = [all_code[code], [code, draw, name, data]]
        else:
            all_code[code]=[code, draw, name, data]

def std(s):
    if isinstance(s, datetime):
        return s.strftime('%Y-%m-%d')
    elif s:
        return str(s).upper().strip()
    else:
        return '-'

# 查找旧编码,并在编码前加标记 *
def old_item_check():
    # 取出字典中所有的key,组成list，然后排序进行比较
    keys=list(all_code.keys())
    keys.sort()
    # 物料按编码排序,对任一编码和下一个编码进行比对,如果除了最后一位相同,且比较小,则认为是旧编码
    for x,a in enumerate(keys):
        if a[2] == 'R':
            if a[-1] == 'P':
                a = a.replace('P', '')
            b = keys[x + 1]
            if b[-1] == 'P':
                all_code[a][0] += ' old'
            elif a[:-1] == b[:-1] and a[-1] <= b[-1]:
                all_code[a][0] +=' old'
        if x == len(keys) - 2:
            break

all_code = {}       # 所有编码的库文件，读取完毕会写入到文件中
all_code_rpt = {}    # 所以编码的字典文件，便于查找重复项

filename = ['折叠机加工件新编码.xlsx', '各种采购件新编码.xlsx', '滚筒烫平机加工件新编码.xlsx','OEM&集成系统&能效系统加工件新编码.xlsx']
patch = 'D:\\work\\python\\excel处理\\excel\\'
file = [patch + x for x in filename]

# 用于读取已有json文件
try:
    with open('all_code.json', 'r', encoding='utf-8') as fr:
        all_code = json.load(fr)
except:
    print('现有文件不存在')

for wbook in file:
    wb = xl.load_workbook(wbook, read_only=True)
    for sname in wb.sheetnames:
        ws = wb[sname]
        for m in ws.iter_rows(min_row=2, max_col=6):
            add_item(m)

old_item_check()

print(len(all_code))

# indent 有缩进，后一个是不用ascii编码，就可以在文件中显示成中文
# 把读取的物料表写入json文件,方便快速读取
with open('all_code.json', 'w', encoding='utf-8') as fw:
    json.dump(all_code, fw, indent=4, ensure_ascii=False)

物料库-读取excel保存字典.py


import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from pandas_datareader import data as pdr
from keras.models import load_model
from sklearn.preprocessing import MinMaxScaler
import yfinance as yfin
import io
import base64
from django.http import JsonResponse
from rest_framework.decorators import api_view

yfin.pdr_override()

matplotlib.use('agg')  # Use the 'agg' backend

# Load the pre-trained Keras model
model = load_model('/Users/ryan/Desktop/LSTM-Stock-Trading-App/keras_model.h5')

@api_view(['POST'])
def generate_prediction_graph(request):
    if request.method == 'POST':
        # stock_ticker = request.POST.get('stock_ticker', 'AAPL')
        # print(request.data['stock_ticker'])
        stock_ticker = request.data['stock_ticker']
        # print(stock_ticker)
        start = '2010-01-01'
        end = '2019-12-31'
        y_symbols = [stock_ticker]
        df = pdr.get_data_yahoo(y_symbols, start, end)

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

        # Generate the three graphs
        plt.figure(figsize=(12, 18))

        # Graph 1: Closing Price vs. Time Chart
        plt.subplot(3, 1, 1)
        plt.plot(df.index, df['Close'], label='Closing Price', color='blue')
        plt.title('Closing Price vs. Time Chart')
        plt.xlabel('Time')
        plt.ylabel('Price ($)')
        plt.legend()
        plt.grid(True)

        # Graph 2: Closing Price vs. Time Chart with Moving Averages
        ma100 = df['Close'].rolling(100).mean()
        ma200 = df['Close'].rolling(200).mean()
        plt.subplot(3, 1, 2)
        plt.plot(df.index, df['Close'], label='Closing Price', color='blue')
        plt.plot(df.index, ma100, label='100-day Moving Average', color='orange')
        plt.plot(df.index, ma200, label='200-day Moving Average', color='red')
        plt.title('Closing Price vs. Time Chart with Moving Averages')
        plt.xlabel('Time')
        plt.ylabel('Price ($)')
        plt.legend()
        plt.grid(True)

        data_training = pd.DataFrame(df['Close'][0:int(len(df) * 0.7)])
        data_testing = pd.DataFrame(df['Close'][int(len(df) * 0.7) : int(len(df))])

        scaler = MinMaxScaler(feature_range=(0,1))
        data_training_array = scaler.fit_transform(data_training)

        past_100_days = data_training.tail(100)
        final_df = pd.concat([past_100_days, data_testing], ignore_index=True)
        input_data = scaler.fit_transform(final_df)

        x_test = []
        y_test = []

        for i in range(100, input_data.shape[0]):
            x_test.append(input_data[i-100: i])
            y_test.append(input_data[i, 0])

        x_test, y_test = np.array(x_test), np.array(y_test)

        y_predicted = model.predict(x_test)

        scaler = scaler.scale_

        scale_factor = 1/scaler[0]
        y_predicted = y_predicted * scale_factor
        y_test = y_test * scale_factor

        # Graph 3: Predictions vs. Original
        plt.subplot(3, 1, 3)
        plt.plot(y_test, label='Original Price', color='blue')
        plt.plot(y_predicted, label='Predicted Price', color='red')
        plt.title('Closing Price vs. Predicted Price')
        plt.xlabel('Time (Days)')
        plt.ylabel('Price ($)')
        plt.legend()
        plt.grid(True)

        plt.tight_layout()

        # Save the graphs as images
        buffer = io.BytesIO()
        plt.savefig(buffer, format='png')
        plt.close()
        buffer.seek(0)

        # Convert the images to base64 to send to the frontend
        graph_base64 = base64.b64encode(buffer.getvalue()).decode()

        # Convert the DataFrame to a JSON object for the table
        # data_table = df.to_dict(orient='list')

        # Get the summary statistics using df.describe()
        data_summary = df.describe()

        # Convert the summary statistics to a dictionary for JSON response
        data_summary_dict = data_summary.to_dict()

        # print(graph_base64)

        return JsonResponse({
            'graph': graph_base64,
            'summary': data_summary_dict,
        })

    return JsonResponse({'error': 'Invalid request method.'}, status=405)


import torch
from torch import nn
from transformers import AutoModel, AutoTokenizer, BertModel

class BERTClassification(nn.Module):
    def __init__(self, num_class):
        super().__init__()
        self.bert: BertModel = AutoModel.from_pretrained("bert-base-uncased")
        for param in self.bert.parameters():
            param.requires_grad = False
        self.classifier = nn.Sequential(
            nn.Linear(768, 768),
            nn.ReLU(),
            nn.Dropout(),
            nn.Linear(768, num_class),
        )

    def forward(self, input_ids, attention_mask, token_type_ids):
        output = self.bert(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)
        output = self.classifier(output.pooler_output)
        return output

def test():
    from torchinfo import summary

    device = "cuda" if torch.cuda.is_available() else "cpu"
    net = BERTClassification(6).to(device)

    sentences = [
        "This is the first sentences",
        "I've been meaning to write for ages and finally today",
    ]
    tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
    inputs = tokenizer(sentences, return_tensors="pt", padding=True).to(device)
    summary(net, **inputs)

    out = net(**inputs)
    print(out.shape)

    print(net)
    # network_state = net.state_dict()
    # print("PyTorch model's state_dict:")
    # for layer, tensor in network_state.items():
    #     print(f"{layer:<45}: {tensor.size()}")

if __name__ == "__main__":
    test()


import math
from randompointgenerator import LatLongList

class vertex:
    def __init__(self, xValIn, yValIn):
        self.xVal = xValIn
        self.yVal = yValIn
        self.checked = False

def distance(first, second, vert):
    xVal = float(vert[first].xVal - vert[second].xVal)
    yVal = float(vert[first].yVal - vert[second].yVal)

    return math.sqrt(math.pow(xVal, 2) + math.pow(yVal, 2))

class theTSP:

    theFinal = []
    theWeight = 0

    def __init__(self, numVec):
        theWorld = []
        masterList = LatLongList(numVec)

        i = 0
        while i < numVec:
            theVert = vertex(masterList.l[i][0], masterList.l[i][1])
            theWorld.append(theVert)
            i += 1

        parents = []
        smallest = float("inf")
        index = 0

        i = 2
        while i < numVec:
            dist = distance(i, 0, theWorld)
            if smallest > dist:
                smallest = dist
                index = i
            i += 1

        parents.append(0)
        parents.append(index)

        theWorld[0].checked = True
        theWorld[index].checked = True

        smallest = float("inf")

        i = 1
        while i < numVec:
            if not theWorld[i].checked:
                j = 0
                while j < len(parents) - 1:
                    if smallest >= (distance(parents[j], i, theWorld) + distance(parents[j + 1], i, theWorld) - distance(parents[j], parents[j + 1], theWorld)):
                        smallest = (distance(parents[j], i, theWorld) + distance(parents[j+1], i, theWorld) - distance(parents[j], parents[j+1], theWorld))
                        index = j + 1
                    j += 1
                if smallest < (distance(0, i, theWorld) + distance(parents[len(parents) - 1], i, theWorld) - distance(0, parents[len(parents) - 1], theWorld)):
                    parents.insert(index, i)
                else:
                    parents.append(i)
                theWorld[i].checked = True

            smallest = float("inf")
            i += 1

        weight = float(distance(parents[0], parents[numVec - 1], theWorld))

        heaviestDist = weight
        heaviestNum = numVec - 1

        i = 0
        while i < numVec - 1:
            if distance(parents[i], parents[i + 1], theWorld) > heaviestDist:
                heaviestDist = distance(parents[i], parents[i + 1], theWorld)
                heaviestNum = i
            weight += distance(parents[i], parents[i + 1], theWorld)
            i += 1

        # print(repr(round(weight * 69, 6)) + ' miles')
        weight -= heaviestDist
        self.theWeight = weight * 69

        '''
        i = 0
        while i < numVec:
            toBePrinted = repr(round(theWorld[parents[i]].xVal, 6)) + ' ' + repr(round(theWorld[parents[i]].yVal, 6))
            print(toBePrinted)
            i += 1
        '''

        i = heaviestNum + 1
        while i < numVec:
            self.theFinal.append((theWorld[parents[i]].xVal, theWorld[parents[i]].yVal))
            i += 1

        i = 0
        while i < heaviestNum + 1:
            self.theFinal.append((theWorld[parents[i]].xVal, theWorld[parents[i]].yVal))
            i += 1

    def getList(self):
        return self.theFinal

    def getWeight(self):
        return self.theWeight

# For printing/testing purposes
#
# driver = theTSP(10)
#
# driver.run()
# print(driver.getWeight())
# i = 0
# while i < len(driver.getList()):
#         toBePrinted = repr(round(driver.getList()[i][0], 6)) + ' ' + repr(round(driver.getList()[i][1], 6))
#         print(toBePrinted)
#         i += 1


# -*- coding: utf-8 -*-
import math
n = int(input())
result={}
for i in range(n):
    name,x,y=input().split(' ')
    x=abs(int(x))
    y=abs(int(y))
    result[name]=math.sqrt(x*x+y*y)

loser=list(result.keys())[list(result.values()).index(max(result.values()))]
winner=list(result.keys())[list(result.values()).index(min(result.values()))]
print(' '.join([winner,loser]))


#!/usr/bin/python3.6
# coding: utf-8
'''
calibrate MISR dataset with ATM measurement data and returns the training dataset

input: dir path where is home to masked_toa_refl & ATM data
output: single datasets.csv for each ATM.csv; and then will merge all output single datasets together in the end and will return a final merged dataset for filtering

to-do:
- do not check empty lists
- check all code again
'''
'''
notes:
	-999.0 represents no MISR toa file
	-998 is the first line
'''

import MisrToolkit as mtk
import pandas as pd
import numpy as np
import sys, glob, os, csv
from platform import python_version
import datetime as dt

# !pip uninstall jedi --yes

# check python version be 3.6 cuz MTK works with 3.6

# py_version = sys.version
# py_ver = py_version[0:3]
# if (py_ver != str(3.6)):

# another way to get verion. note: this f() returns py version as str dtype, we change it to float
py_ver = float(python_version()[0:3])
print(py_ver)
if (py_ver != 3.6):
	print('python version is NOT 3.6- MTK does not work')
	raise SystemExit()
else:
	print('python version: %s' %py_ver)

##- set up paths
#################### input dir should include 9 dir
masked_toa_home = "/media/ehsan/Gdrive_18TB/all_masked_toa_refl_2010_2014_2019"
# masked_toa_home = "/media/ehsan/6T_part1/2016/april_2016/14528_apr2016/project_april_2016_9cam3bands/masked_toa_refl_april_2016_9cam4bands_day1_30_p1_233_b1_46"

atm_dir = "/home/ehsan/misr_lab/ATM_2010_2014_2019"
# atm_dir = "/media/ehsan/6T_part1/2016/april_2016/14528_apr2016/project_april_2016_3cam/ATM_apr2016_5days"

#################### output
trainingDS_dir = "/media/ehsan/Gdrive_18TB/training_DS_2010_2014_2019"
# trainingDS_dir = "/media/ehsan/6T_part1/2016/april_2016/14528_apr2016/project_april_2016_3cam/training_data/april_2016"

#################### setup pattern- do not change
atm_file_pattern = 'ILATM2*'+'.csv'
single_csv_ds_label = "9cam3bands"

# atm_file_sample = 'ILATM2_20160420_175457_smooth_nadir3seg_50pt.csv'
# atm_file_fp = os.path.join(atm_dir, atm_file_sample)
# check ATM file exists
# print(os.path.isfile(atm_file_fp))
# print(atm_file_fp)

# check runtime
t1 = dt.datetime.now()

#### create datadet table
# cam_labels = ['Da_r','Ca_r','Ba_r','Aa_r','An_r','An_g','An_b','An_nir','Af_r','Bf_r','Cf_r','Df_r','mean_ATM_roughness']

# add all found fields to pandas.DataFrame and make a dataset with column labels and filter for similar pixels
# column_names = ['path','orbit','block','line','sample','lat','lon', 'points_in_pixel', cam_labels[0],cam_labels[1],cam_labels[2],cam_labels[3],cam_labels[4],cam_labels[5],cam_labels[6],cam_labels[7],cam_labels[8],cam_labels[9],cam_labels[10],cam_labels[11],'mean_ATM_roughness']

ds_row_index = 0

column_names = ['path','orbit','block','line','sample','lat','lon',\
				'Da_r','Ca_r','Ba_r','Aa_r','An_r','An_g','An_b','An_nir','Af_r','Bf_r','Cf_r','Df_r',\
				'mean_ATM_roughness']#,'ATM_start_time','ATM_end_time']

final_ds_list = []

## get a list of ATM csv files
atm_list = glob.glob(os.path.join(atm_dir, atm_file_pattern))
total_atm_files_found = len(atm_list)
print('total ATM.csv files: %d' %total_atm_files_found)

## create pandas DF and set index to DF based on pre-estimate: 100000 per each ATM file
# final_df = pd.DataFrame(columns=column_names) #, index=range(total_atm_files_found*1000))  # if nut set the index number correctly, returns segmentation fault- Q- how create a DF with variable size?

## this does not work
# final_ds.set_index(total_atm_files_found*10000)

# open each ATM file and read each row and will eventually write out a train dataset for each ATM file
for atm_cntr, ATMfile in enumerate(atm_list):

	print('\n********** processing ATM file: (%d/%d)' %(atm_cntr+1, len(atm_list)))
	print("try to use ATM time and locations to find MISR pixel...\n")
	print(ATMfile)

	atm_filelabel = ATMfile.split('/')[-1]
	atm_filelabel_date_list = atm_filelabel.split('_')[0:3]
	atm_filelabel_date = atm_filelabel_date_list[0]+"_"+atm_filelabel_date_list[1]+"_"+atm_filelabel_date_list[2]
	output_ds_label = single_csv_ds_label+'_'+atm_filelabel_date+'.csv'
	print("output csv: %s" %output_ds_label)

	output_file_fp = os.path.join(trainingDS_dir, output_ds_label)
	if (os.path.isfile(output_file_fp)):
		print("CSV file exists on disk, continue to next ATM file")
		continue

	# get date-time from ATM file label
	atm_label = ATMfile.split('/')[-1]
	yrmonday = atm_label.split('_')[1]
	atm_yr = yrmonday[0:4]
	atm_mon = yrmonday[4:6]
	atm_day = yrmonday[6:8]
	# print(atm_yr)
	# print(atm_mon)
	# print(atm_day)

	hrminsec = atm_label.split('_')[2]
	atm_hr = hrminsec[0:2]
	atm_min = hrminsec[2:4]
	atm_sec = hrminsec[4:6]

	ATM_start_time = atm_yr+'-'+atm_mon+'-'+atm_day+"T00:00:00Z" # YYYY-MM-DDThh:mm:ssZ
	ATM_end_time = atm_yr+'-'+atm_mon+'-'+atm_day+"T23:59:59Z" # YYYY-MM-DDThh:mm:ssZ

	ATM_start_time_hrminsec = atm_yr+'-'+atm_mon+'-'+atm_day+"T"+atm_hr+":"+atm_min+":"+atm_sec+"Z" # YYYY-MM-DDThh:mm:ssZ
	ATM_end_time_hrminsec = atm_yr+'-'+atm_mon+'-'+atm_day+"T"+atm_hr+":"+atm_min+":"+atm_sec+"Z" # YYYY-MM-DDThh:mm:ssZ

	print("ATM start: %s" %ATM_start_time)
	print("ATM end:   %s" %ATM_end_time)

	# get a list of orbits for date-time
	orbit_list_today = mtk.time_range_to_orbit_list(ATM_start_time, ATM_end_time)
	# print('orbits today: %s' %len(orbit_list_today))
	# print(orbit_list_today)

	# for each orbit we find path number and use that to find MISR pixel
	for misr_orbit_today in orbit_list_today:
		# print('\nprocessing orbit: %d' %misr_orbit_today)

		# orbit to path
		misr_pathNumber_today = mtk.orbit_to_path(misr_orbit_today)
		# print('path: %s' %misr_pathNumber_today)

		# open and read ATM file
		# atm_file_columnt = []

		input_atm_file = pd.read_csv(ATMfile, comment='#', sep=',') #skiprows=15, header=0)

		# input_atm_file.head(5)

		# find total atm_rows to make DF with Nan
		csv_total_rows = input_atm_file.shape[0]
		# print(csv_total_rows)

		# read each row in ATM-file and extract latLong
		for atm_row_num in range(csv_total_rows):
			# print(type(atm_row_num))
			atm_lat = input_atm_file.iloc[atm_row_num, 1]
			atm_lon = input_atm_file.iloc[atm_row_num, 2]
			atm_roughness = input_atm_file.iloc[atm_row_num, 6]

			# print('atm lat %s' %atm_lat)
			# print('atm lon %s' %atm_lon)
			# print('roughness %s' %atm_roughness)

			# print('finidng list of paths for ATM location...')
			try:  # to ignore Exception for some path numbers
				paths_cover_ATM_location = mtk.latlon_to_path_list(atm_lat, atm_lon)
				# print('MISR paths cover ATM latlon:')
				# print(paths_cover_ATM_location)
			except Exception:
				print('Exception was raised- continue to next ATM row')
				continue

			if misr_pathNumber_today not in paths_cover_ATM_location:
				# print('MISR path %d in day %s not match with path that ATM lat-lon falls in, continue to next ATM latLon row!' %(misr_pathNumber_today, atm_day))
				continue    # to next location == latLon

			else:
				# print('MISR path %d covers ATM location- we will find Block-Line-Sample!' %misr_pathNumber_today)

				# find MISR pixel from that
				resolution_meters = 275

				misr_block, misr_pixel_x, misr_pixel_y = mtk.latlon_to_bls(misr_pathNumber_today, resolution_meters, atm_lat, atm_lon) # q- why crash? these paths cover that that ATM location

				# print('block: %s' %misr_block)
				# print('pixel x: %s' %misr_pixel_x)
				# print('pixel y: %s' %misr_pixel_y)

				path_num_str = str(misr_pathNumber_today).zfill(3)
				misr_orbit_str = str(misr_orbit_today).zfill(6)
				misr_block_str = str(misr_block).zfill(3)

				# print('path-str: %s' %path_num_str)
				# print('orbit-str: %s' %misr_orbit_str)
				# print('block-str: %s' %misr_block_str)

				#### create fulll path to 9 cameras and 4 spectral bands

				# find MISR files associated with P-O-B: masked_toa_P_O_B
				# aft/back cameras
				aa_red_fp = os.path.join(masked_toa_home,'Aa', ('masked_toa_refl_P'+path_num_str+'_'+'O'+misr_orbit_str+'_'+'B'+misr_block_str+'_aa_red.dat'))
				ba_red_fp = os.path.join(masked_toa_home,'Ba', ('masked_toa_refl_P'+path_num_str+'_'+'O'+misr_orbit_str+'_'+'B'+misr_block_str+'_ba_red.dat'))
				ca_red_fp = os.path.join(masked_toa_home,'Ca', ('masked_toa_refl_P'+path_num_str+'_'+'O'+misr_orbit_str+'_'+'B'+misr_block_str+'_ca_red.dat'))
				da_red_fp = os.path.join(masked_toa_home,'Da', ('masked_toa_refl_P'+path_num_str+'_'+'O'+misr_orbit_str+'_'+'B'+misr_block_str+'_da_red.dat'))

				# front cameras
				af_red_fp = os.path.join(masked_toa_home,'Af', ('masked_toa_refl_P'+path_num_str+'_'+'O'+misr_orbit_str+'_'+'B'+misr_block_str+'_af_red.dat'))
				bf_red_fp = os.path.join(masked_toa_home,'Bf', ('masked_toa_refl_P'+path_num_str+'_'+'O'+misr_orbit_str+'_'+'B'+misr_block_str+'_bf_red.dat'))
				cf_red_fp = os.path.join(masked_toa_home,'Cf', ('masked_toa_refl_P'+path_num_str+'_'+'O'+misr_orbit_str+'_'+'B'+misr_block_str+'_cf_red.dat'))
				df_red_fp = os.path.join(masked_toa_home,'Df', ('masked_toa_refl_P'+path_num_str+'_'+'O'+misr_orbit_str+'_'+'B'+misr_block_str+'_df_red.dat'))

				# nadir
				an_red_fp = os.path.join(masked_toa_home,'An', ('masked_toa_refl_P'+path_num_str+'_'+'O'+misr_orbit_str+'_'+'B'+misr_block_str+'_an_red.dat'))
				an_green_fp = os.path.join(masked_toa_home,'An', ('masked_toa_refl_P'+path_num_str+'_'+'O'+misr_orbit_str+'_'+'B'+misr_block_str+'_an_green.dat'))
				an_blue_fp = os.path.join(masked_toa_home,'An', ('masked_toa_refl_P'+path_num_str+'_'+'O'+misr_orbit_str+'_'+'B'+misr_block_str+'_an_blue.dat'))
				an_nir_fp = os.path.join(masked_toa_home,'An', ('masked_toa_refl_P'+path_num_str+'_'+'O'+misr_orbit_str+'_'+'B'+misr_block_str+'_an_NIR.dat')) # note here: should match the file names on drive ("an_NIR" is ok, not "an_nir")

				#### check if camera files availabel

				# create a list of camreas based on order from Da->Df- order if important
				masked_toaFile_orderedList = [da_red_fp, ca_red_fp, ba_red_fp, aa_red_fp,
											  an_red_fp, an_green_fp, an_blue_fp, an_nir_fp,
											  af_red_fp, bf_red_fp, cf_red_fp, df_red_fp]

				available_file_status = []
				# check file exists
				for masked_toa_file in masked_toaFile_orderedList:
					# print(masked_toa_file)
					available_status = os.path.isfile(masked_toa_file)
					# print(available_status)
					available_file_status.append(available_status)

				##-- check if all 12 files/cameras are available

				# if (all(available_file_status)==False): # check if any element is false==file not found
				#     # print('***some masked-files missing- continue*** \n')
				#     continue # to the next ATM row

				if (not any(available_file_status)):
					# print('did not find even a single camera value for ATM location/row- continue')
					continue

				# print("num. of masked toa files found:")
				# print(available_file_status)

				# open masked-toa-refl and extract pixel value
				pixel_values = []

				for maskedTOA_rawfile in masked_toaFile_orderedList:
					# print(maskedTOA_rawfile)
					is_file = os.path.isfile(maskedTOA_rawfile)
					# print(is_file)

					#-- if one camera is missing, assign fill value for that camera that was not found
					if (is_file==False):
						pixel_values.append(-999.0)
					else:
						rough_2d_arr = np.fromfile(maskedTOA_rawfile, dtype=np.double)[0:1048576].reshape((512,2048))   # 'double==float64'     # is this roughness in cm?
						# print(rough_2d_arr.shape)
						# print(type(rough_2d_arr))
						##- maybe make a image from it?
						pixel_value = rough_2d_arr[int(misr_pixel_x), int(misr_pixel_y)]
						pixel_values.append(round(pixel_value,5))

				# print('path-str: %s' %path_num_str)
				# print('orbit-str: %s' %misr_orbit_str)
				# print('block-str: %s' %misr_block_str)

				# print("found these pixel values for an ATM location:")
				# print(pixel_values)

				########################################################
				# new way of making a list and then open it in dataframe

				final_ds_values = [misr_pathNumber_today, misr_orbit_today, misr_block,\
				 int(misr_pixel_x), int(misr_pixel_y), round(atm_lat,7), round(atm_lon,7), \
				 pixel_values[0],pixel_values[1],pixel_values[2],pixel_values[3],pixel_values[4],\
				 pixel_values[5],pixel_values[6],pixel_values[7],pixel_values[8],pixel_values[9],\
				 pixel_values[10],pixel_values[11],atm_roughness]#,\
				 # ATM_start_time_hrminsec,ATM_end_time_hrminsec]

				zipped = zip(column_names, final_ds_values)
				a_dictionary = dict(zipped)
				# print(a_dictionary)
				final_ds_list.append(a_dictionary)

				ds_row_index +=1

	##################################################################
	# write out the train dataset for each ATM file

	if (len(final_ds_list) == 0):
		print(final_ds_list)
		print("ATM list is empty, we write -998 flag & continue to next ATM file")
		print("note: -998 means we did not find any MISR pixel that covers ATM locations in that file.")

		final_ds_list = [-998,-998,-998,-998,-998,-998,-998,-998,-998,-998,-998,-998,-998,-998,-998,-998,-998,-998,-998,-998]#,-998,-998]

		with open(output_file_fp, 'w', encoding='UTF8', newline='') as fileObj:
			writer = csv.writer(fileObj)  # create a new instance of the DictWriter class by passing the file object
			writer.writerow(column_names)
			writer.writerow(final_ds_list)

		final_ds_list.clear()
		# continue
		print(output_file_fp)

	else:
		print('writing out single CSV file at a time...')

		with open(output_file_fp, 'w', encoding='UTF8', newline='') as fileObj:
			writer = csv.DictWriter(fileObj, fieldnames=column_names)  # create a new instance of the DictWriter class by passing the file object
			writer.writeheader()
			writer.writerows(final_ds_list)

		print(output_file_fp)
		print('successfully finished writing a single CSV file!')

		## empty the list
		print("before empty the list...")
		print(len(final_ds_list))
		final_ds_list.clear()
		print("after empty the list...")
		print(len(final_ds_list))

# check runtime
t2 = dt.datetime.now()
runtime = t2-t1
print("\nruntime: %s" %runtime)
print("\nFINISHED SUCCESSFULLY: we processed %d csv files. In next step we should use another code to merge all single csv files..." %atm_cntr)

#########################################################################################################################################################
# ## now merge all csv files into a single dataframe and write the output
# print("\n")
# print("now merging all CSV files...")
# csv_file_pattern = "april_2016_9cam_4bands_ILATM2*"+".csv"
# csv_files_list = glob.glob(os.path.join(trainingDS_dir, csv_file_pattern))
# print(len(csv_files_list))
# ## join files
# final_df = pd.concat(map(pd.read_csv, csv_files_list), ignore_index=False, axis=0)  # true: assigns a continuous index numbers for the merged DF; axis=0 along the rows
# out_DS  = os.path.join(trainingDS_dir, output_final_ds_label)
# final_df.to_csv(out_DS, index=False) # do not include index column in output csv
# print(out_DS)
# print('successfully finished writing final DataFrame!')

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

######## old code
		## dataframe method
		# final_df = pd.DataFrame(columns=column_names)
		# final_df = final_df.append(final_ds_list, ignore_index=True) # add list to existing dataframe; True: preserve the DataFrame indices;

		# out_DS  = os.path.join(trainingDS_dir, output_ds_label)
		# final_df.to_csv(out_DS)
		# print(out_DS)
		# final_df=None
		# final_df.drop(columns=column_names, axis=1, inplace=True)

		## csv writer method
		# ## if each row is a list
		# with open (output_file_fp, "w") as fileObj:
		# 	writer = csv.writer(fileObj)
		# 	writer.writerow(column_names)
		# 	writer.writerows(final_ds_list)

		## if each row is a dictionary


from _decimal import Decimal

from rest_framework import serializers

from src.courses.repository.course_repository import CourseRepository
from src.grades.models import Grade
from src.users.repository.professor_repository import ProfessorRepository
from src.users.repository.student_repository import StudentRepository

class GradeSerializer(serializers.ModelSerializer):
    id = serializers.UUIDField(read_only=True)
    course = serializers.PrimaryKeyRelatedField(
        queryset=CourseRepository().search_all_objects(),
        required=False,
        write_only=True,
    )
    professor = serializers.PrimaryKeyRelatedField(
        queryset=ProfessorRepository().search_all_objects(),
        required=False,
        write_only=True,
    )
    student = serializers.PrimaryKeyRelatedField(
        queryset=StudentRepository().search_all_objects(),
        required=False,
        write_only=True,
    )
    course_name = serializers.SerializerMethodField()
    professor_name = serializers.SerializerMethodField()
    student_name = serializers.SerializerMethodField()
    value = serializers.DecimalField(max_digits=5, decimal_places=2)

    class Meta:
        model = Grade
        fields = [
            "id",
            "course",
            "professor",
            "student",
            "course_name",
            "professor_name",
            "student_name",
            "value",
        ]

    def get_course_name(self, obj):
        if obj.course:
            return obj.course.name
        return None

    def get_professor_name(self, obj):
        if obj.professor:
            return obj.professor.name
        return None

    def get_student_name(self, obj):
        if obj.student:
            return obj.student.name
        return None

    def validate_grade_value(self, value):
        if not value or (not isinstance(value, Decimal) or value < 0):
            raise serializers.ValidationError(
                {"value": "A Nota deve ser maior ou igual a 0 e decimal"}
            )
        return value

    def validate(self, data):
        if self.instance:
            return data

        self.validate_grade_value(data.get("value"))

        model_fields = self.Meta.fields.copy()
        model_fields.remove("id")
        model_fields.remove("course_name")
        model_fields.remove("professor_name")
        model_fields.remove("student_name")
        for field in model_fields:
            if field not in data:
                raise serializers.ValidationError({field: "Este campo é obrigatório."})

        return data


import os

from flask import Flask, request, render_template, Response

from helpers import sentiment_score

app = Flask(__name__)

@app.route('/')
def hello():
    return render_template('video.html');

@app.route('/sentiment/<sentence>')
def sentiment(sentence):
    score = sentiment_score(sentence)
    return Response(str(score))

if __name__ == '__main__':
    #setup dev server
    app.debug = True
    app.run(host='localhost', port=8080)


'''
Author: Ri Hong
Date AC'd: Feb 2, 2020
Problem: https://dmoj.ca/problem/ccc04s1
'''

#Explanation
'''
For each testcase, we are given three words and we want to check if each word is a prefix or suffix of another. So we have to compare
word 1 and word 2, word 2 and word 3 and word 1 and word 3.

Let us now compare two words, let's say word 1 and word 2. Note that we can only check if word 2 contains word 1 as a prefix/suffix if
word 2's length 1 greater than or equal to word 1's length. The same applies when we check if word 1 contains word 2 as a prefix/suffix.

To check if word 1 is a prefix of word 2, let us assign the length of word 1 to a variable a.
We want to see if the first n characters of word 2 forms the same string as word 1.
To check if word 1 is a suffix of word 2, we have to check if the last n characters of word 2 forms the same string as word 1. (this can
be done using negative indexing)
'''

#Code
#It is recommended that you read the main code before the function

#This function compares two words and returns true of both are fix-free of each other
def fixFree(word1, word2):
  if len(word2) >= len(word1): #only search for fixes within word 2 if word 2's length is greater than or equal to word 1's length
    if word2[:len(word1)] == word1: #if word 1 is a prefix of word 2
      return False
    if word2[-len(word1):] == word1: #if word 1 is a suffix of word 2
      return False

  if len(word1) >= len(word2): #only search for fixes within word 1 if word 1's length is greater than or equal to word 2's length
    if word1[:len(word2)] == word2: #if word 2 is a prefix of word 1
      return False
    if word1[-len(word2):] == word2: #if word 12 is a suffix of word 1
      return False

  return True #return true because if the function makes it to this point, it means that there are no fixes in either word

#--MAIN---
words = [] #stores teh three words we need to process for each testcase
numTestcases = int(input()) #get the number of testcases (also known as N in the problem statement) as an integer
for i in range(numTestcases): #go through each testcase
  #add the three words into the words list
  for j in range(3):
    words.append(input())

  #check if all combinations of words are fix free from each other and print the appropriate message
  if fixFree(words[0], words[1]) and fixFree(words[1], words[2]) and fixFree(words[0], words[2]):
    print("Yes")
  else:
    print("No")
  words.clear() #clear the list after each testcase


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 14 17:19:01 2021

@author: silicon
"""

import rdkit
from rdkit import Chem
from rdkit.Chem import AllChem, Draw
from rdkit import DataStructs
from collections import Counter
from rdkit.Chem.Draw import IPythonConsole, MolsToGridImage
from fragment_cut import cut_fragment as cf
import numpy as np
from sklearn.cluster import KMeans
import csv

path = "kmeans_10_fragments.log"
kmeans_library_1 = {i: [] for i in range(2)}  # num cluster
kmeans_library_2 = {i: [] for i in range(2)}
with open (path,"r") as f:
    lines = f.readlines()
for line in lines:
    if len(line.split())>0:
        num,smile = int(line.split()[0]), line.split()[1]
    kmeans_library_1[num].append(smile)
#print(kmeans_library_1)
for key, value in kmeans_library_1.items():
   # print(key,value)
    value_tem = [value[0]]
    #print(value_tem)

    for va in value[1:]:
        flag = True
        mol_va = Chem.MolFromSmiles(va)
        for va_t in value_tem:
            mol_va_t = Chem.MolFromSmiles(va_t)
           # print(key,value_tem)
            fp0 = AllChem.GetMorganFingerprintAsBitVect(mol_va,2)
            fp1 = AllChem.GetMorganFingerprintAsBitVect(mol_va_t,2)
            sm12 =DataStructs.FingerprintSimilarity(fp0,fp1,metric=DataStructs.DiceSimilarity)
            #print(key,sm12)
            if sm12 > 0.99:
                flag = False
                break
        if flag:
             value_tem.append(va)
               # print(value_tem)
               # exit
    kmeans_library_2[key] = value_tem

print(kmeans_library_2)
with open("non_repeat_cluster_smiles.log","w")as f:
     for key,value in kmeans_library_2.items():
         ss = str(key)
         for va in value:
             ss += ",  " +va
         f.write(ss)
         f.write('\n')

def plt_moles(smiles,plt_name):
    frag_moles = []
    for sm in smiles:
        mol = Chem.MolFromSmiles(sm)
        frag_moles.append(mol)

    img = Draw.MolsToGridImage(frag_moles, molsPerRow=5,subImgSize=(300, 300))
    img.save("_new_labels_"+str(plt_name)+".png")

for key, value in kmeans_library_2.items():
    plt_moles(value,key)


user = "gizynski.dominik@gmail.com"
lpmn="any2txt|wcrft2|wsd"
nlprestURL = "http://ws.clarin-pl.eu/nlprest2/base/process/"

import urllib2
# from dataEventsWro import toDate
import sys
import numpy as np
import json
import xml.etree.ElementTree as ET

words = []

def lpmnProcess(text):
    array = lpmnProcessSingle(text)
    for word in array:
        if word not in words:
            words.append(word)
    return array
    # data = {'lpmn':lpmn,'user':user,'text':text}
    # doc = json.dumps(data)
    # resp = urllib2.urlopen(urllib2.Request(nlprestURL,doc,{'Content-Type': 'application/xml'})).read()
    # # print(resp)
    # result = ET.fromstring(resp)
    # for sentence in result.iter('sentence'):
    #     for tok in sentence.findall('tok'):
    #         lex = tok.find('lex')
    #         base = lex.find('base').text
    #         ctag = lex.find('ctag').text
    #         if ctag == 'interp' or ctag == 'conj' or 'prep:' in ctag:
    #             continue
    #         if base not in words:
    #             words.append(base)
    # return words

def lpmnProcessSingle(text):
    array = []
    data = {'lpmn':lpmn,'user':user,'text':text}
    doc = json.dumps(data)
    resp = urllib2.urlopen(urllib2.Request(nlprestURL,doc,{'Content-Type': 'application/xml'})).read()
    # print(resp)
    result = ET.fromstring(resp)
    for sentence in result.iter('sentence'):
        for tok in sentence.findall('tok'):
            lex = tok.find('lex')
            base = lex.find('base').text
            ctag = lex.find('ctag').text
            if ctag == 'interp' or ctag == 'conj' or 'prep:' in ctag or 'adj:' in ctag:
                continue
            if base not in array:
                array.append(base)
    return array

# print(lpmnProcess("Ala ma kota kotu"))

def saveNewInputsClarin():
    dtn = np.string_
    names = np.loadtxt("eventsWroFiltered.csv", delimiter=",", dtype=dtn, usecols=[2])
    print(names.shape)
    print(names[0])
    # print(lpmnProcess(names[9]))
    # sys.exit()
    k = 1
    for i in names:
        lpmnProcess(i)
        k+=1
        # if k == 40:
        #     break
        print(k, i)
    print(words.__len__())

    np.savetxt("newInputs.csv", np.asarray(words), fmt='%s', delimiter=",")

def expandEventsFilteredWithClarin():
    dtn = np.string_
    csv = np.loadtxt("eventsWroFiltered.csv", delimiter=",", dtype=dtn, usecols=[0,1,2])
    print(csv.shape)
    print(csv[0])
    samples = []
    sample = []
    k = 1
    for i in csv:
        firstDay = i[0]
        lastDay = i[1]
        event = i[2]
        wordsArray = lpmnProcess(event)
        wordsString = ".".join(wordsArray)
        sample.append(firstDay)
        sample.append(lastDay)
        sample.append(event.decode('utf-8'))
        sample.append(wordsString)
        if wordsString != "":
            samples.append(sample)
        sample = []

        print(k, event)
        k+=1
        # if k == 401:
        #     break

    print(len(samples))
    np.savetxt("eventsWroFilteredClarin.csv", np.asarray(samples), fmt='%s', delimiter=",")

    np.savetxt("newInputs0.csv", np.asarray(words), fmt='%s', delimiter=",")

def main():
    expandEventsFilteredWithClarin()
    # print(lpmnProcess("Ala posiada przystojnego kota w kotu Estudiantes Twin Peaks ENEMEF Borussia"))

if __name__ == '__main__':
    main()

# <?xml version="1.0" encoding="UTF-8"?>
# <!DOCTYPE chunkList SYSTEM "ccl.dtd">
# <chunkList>
#  <chunk id="ch1" type="p">
#   <sentence id="s1">
#    <tok>
#     <orth>Ala</orth>
#     <lex disamb="1"><base>Al</base><ctag>subst:sg:gen:m1</ctag></lex>
#     <lex disamb="1"><base>Alo</base><ctag>subst:sg:gen:m1</ctag></lex>
#     <prop key="sense:ukb:syns_id">44973</prop>
#     <prop key="sense:ukb:syns_rank">44973/3861.3948432140 19218/2808.2367828538</prop>
#     <prop key="sense:ukb:unitsstr">obliczeniowa_sztuczna_inteligencja.1(6:umy) AL.1(6:umy) obliczenia_inteligentne.1(6:umy)</prop>
#    </tok>
#    <tok>
#     <orth>ma</orth>
#     <lex disamb="1"><base>moj</base><ctag>adj:sg:nom:f:pos</ctag></lex>
#     <prop key="sense:ukb:syns_id">9724</prop>
#     <prop key="sense:ukb:syns_rank">9724/6836.2122703515</prop>
#     <prop key="sense:ukb:unitsstr">moj.1(42:jak)</prop>
#    </tok>
#    <tok>
#     <orth>kota</orth>
#     <lex disamb="1"><base>kot</base><ctag>subst:sg:gen:m1</ctag></lex>
#     <prop key="sense:ukb:syns_id">5168</prop>
#     <prop key="sense:ukb:syns_rank">5168/1153.7902152892 34154/1131.5803080437 406482/814.2826927054 405249/797.3057302360 227074/791.1602261788 227075/780.0571662630 75697/763.4500264867</prop>
#     <prop key="sense:ukb:unitsstr">kot.1(21:zw)</prop>
#    </tok>
#   </sentence>
#  </chunk>
# </chunkList>


# =============================================================================
# Nodegraph-pyqt
#
# Everyone is permitted to copy and distribute verbatim copies of this
# document, but changing it is not allowed without permissions.
#
# For any questions, please contact: dsideb@gmail.com
#
# GNU LESSER GENERAL PUBLIC LICENSE (Version 3, 29 June 2007)
# =============================================================================

"""
Constants used in Node graph

"""

SCENE_WIDTH = 8000000
SCENE_HEIGHT = 4000000

DEBUG = False

NODES_COLOR = {
    "read": {"base_color": [100, 200, 100]},
    "camera": {"base_color": [100, 100, 200]},
    "merge": {"base_color": [200, 100, 100]}
}


# Prime permutations

from sympy import isprime

def solve():
    for i1 in [x for x in range(1000, 3340) if x != 1487]:
        i2, i3 = i1 + 3330, i1 + 2 * 3330
        if any(isprime(x) is False for x in (i1, i2, i3)) or any(
                sorted(x) != sorted(str(i1)) for x in (map(str, [i2, i3]))):
            continue
        return "".join(map(str, [i1, i2, i3]))

if __name__ == "__main__":
    print(solve())


from demo_package.two_d import area, perimeter
from demo_package.three_d import volume, surface_area

l=5
b=4
ar= area.calculate_area(l,b)
per= perimeter.caluculate_aparameter(l,b)
print("Area is :",ar)
print("Perimeter is :",per)

l=5
b=4
h=2
vol= volume.calculate_volume(l,b,h)
sur= surface_area.calculate_surface_area(l,b,h)
print("Volume is :",vol)
print("Surface Area is :",sur)


from appium import webdriver
import pytest

class Driver():

    @pytest.fixture(scope="function")
    def driver(self, request):
        desired_caps = {
            "automationName": "Appium",
            # "automationName": "uiautomator2",
            "appActivity": "com.leialoft.filebrowser.FileBrowserActivity",
            "platformName": "Android",
            "platformVersion": "8.1.0",
            "deviceName": "3a90b04d",
            "appPackage": "com.leialoft.redmediaplayer",
            "noReset": "true",
            # "autoGrantPermissions": "true"
        }
        driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

        def fin():
            driver.quit()

        request.addfinalizer(fin)
        return driver


from collections import defaultdict as dd
import csv
import datetime
import os
from pathlib import Path, PosixPath

from sklearn import metrics
from sklearn.decomposition import TruncatedSVD
from sklearn.feature_extraction.text import TfidfVectorizer, HashingVectorizer

from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
from sklearn.svm import LinearSVC

from common.clients.google_storage_client import (
    get_storage_client,
)
from style.config import style_app_settings
from style.constants import FILE_PATH_BOOK_DS, MODEL_EXPORT_PATH
from style.dataset.reader import (
    Dataset,
    DatasetReader,
)
from style.predict.servable.base import SklearnBasedClassifierServable

storage_client = get_storage_client(style_app_settings.MISC_BUCKET)

class TextNormalizer:
    pass

def split_dataset(
    dataset: Dataset,
    test_percentage: float = 0.2,
    random_state: int = 42,
):
    docs_train, docs_test, y_train, y_test = train_test_split(
        dataset.data,
        dataset.target,
        test_size=test_percentage,
        random_state=random_state,
    )
    return docs_train, docs_test, y_train, y_test, dataset.target

def train_sklearn_classification_model(
    docs_train,
    docs_test,
    y_train,
    y_test,
    pipeline: Pipeline,
    grid_search_params: dict,
    n_jobs: int,
    cv: int = 5,
):
    """This method trains multiple models via GridSearchCV and it creates a servable
    from the best model.

    See https://scikit-learn.org/stable/tutorial/statistical_inference
    /putting_together.html for more details.
    """

    grid_search = GridSearchCV(
        pipeline, grid_search_params, n_jobs=n_jobs, cv=cv
    )
    grid_search.fit(docs_train, y_train)
    y_predicted = grid_search.predict(docs_test)

    report_ = metrics.classification_report(
        y_test, y_predicted, output_dict=False
    )

    report_dict = metrics.classification_report(
        y_test, y_predicted, output_dict=True
    )

    confusion_matrix = metrics.confusion_matrix(y_test, y_predicted)

    return grid_search.best_estimator_, report_, report_dict, confusion_matrix

def report(report_, name, confusion_matrix, export_path: PosixPath):
    with open(export_path / f"{name}-report.txt", "w") as f:
        f.write(report_)
        print(f"The report is saved successfully, under {export_path}")
    with open(export_path / f"{name}-cm.txt", "w") as f:
        f.write(f"{str(confusion_matrix)}\n")
        print(
            f"The confusion matrix is saved successfully, under {export_path}"
        )

def model_comparison_report(export_path, params, results):
    # What will it print?
    """
    Model names and their metric pairs (F1 score by weighted average)
    document length
    cv
    test_percentage

    normalization
    reduction

    Returns:

    """
    # prefix = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M")
    with open(export_path / "model_comparison_report.tsv", "w") as f:
        tsv_writer = csv.writer(f, delimiter="\t")
        tsv_writer.writerow(["model_name", "f1-score", "ngram"])
        for model_name, values in results.items():
            tsv_writer.writerow(
                [model_name, values["f1-weighted-avg"], values["best-ngram"]]
            )
        tsv_writer.writerow("")
        params = sorted(params.items())
        for k, v in params:
            tsv_writer.writerow([k, v])

    print(
        f"The model comparison report is saved successfully, under {export_path}"
    )

def export(model, export_path):
    """
        # When the model is ready, initiate it and return at the end.
    # from style.predict.servable.base import SklearnBasedClassifierServable
    # servable = SklearnBasedClassifierServable(model=model).export(export_path)
    Returns:

    """
    SklearnBasedClassifierServable(model=model).export(export_path)

def get_vectorizer(vectorizer: str, min_df, n_features):
    """

    Args:
        min_df: This parameter specifies tf-idf min_df paramater. It only will use when tf-idf vectorizer is selected.
        n_features:

    Returns:

    """
    if vectorizer == "tf-idf":

        return TfidfVectorizer(
            min_df=min_df,
            max_features=None,
            strip_accents="unicode",
            analyzer="word",
            token_pattern=r"\w{1,}",
            ngram_range=(1, 1),
            use_idf=1,
            smooth_idf=1,
            sublinear_tf=1,
            stop_words="english",
        )

    elif vectorizer == "hashing-vectorizer":

        return HashingVectorizer(
            n_features=n_features,
            analyzer="word",
            stop_words="english",
            token_pattern=r"\w{1,}",
            ngram_range=(1, 1),
            alternate_sign=False,
        )

def create_pipeline(
    clf_name,
    estimator,
    vectorizer,
    normalize=False,
    reduction=False,
    min_df=3,
    n_features=2**20,
):
    steps = [
        (
            "vectorize",
            get_vectorizer(vectorizer, min_df, n_features),
        )
    ]

    if normalize:
        steps.insert(0, ("normalize", TextNormalizer()))

    if reduction:
        steps.append(("reduction", TruncatedSVD(n_components=10000)))

    # Add the estimator
    steps.append((clf_name, estimator))
    return Pipeline(steps)

def run():
    import argparse

    parser = argparse.ArgumentParser(
        description="Parameterize data preparation process models."
    )
    # Parameters related with data preparation
    parser.add_argument(
        "--num_books", type=int, help="Number of books for subsetting data"
    )
    parser.add_argument(
        "--document_length",
        type=int,
        default=style_app_settings.DOCUMENT_LENGTH,
        help="Number of words that a document contains",
    )
    parser.add_argument(
        "--test_percentage",
        type=float,
        default=style_app_settings.TEST_PERCENTAGE,
    )

    parser.add_argument(
        "--cross_validation",
        type=int,
        default=style_app_settings.CROSS_VALIDATION,
        help="argument for grid search",
    )
    parser.add_argument(
        "--normalize", action="store_true", help="Normalize text"
    )
    parser.add_argument(
        "--reduction", action="store_true", help="Truncated SVD"
    )

    # Paramaters related with vectorization (tf-idf)
    parser.add_argument(
        "--min_df",
        type=int,
        default=style_app_settings.MIN_DF,
        help="means ignore terms that appear in less than 5 documents or ignore terms "
        "that appear in less than 1% of the documents",
    )

    parser.add_argument(
        "--ngram_min", default=style_app_settings.NGRAM_MIN, type=int
    )

    parser.add_argument(
        "--ngram_max", default=style_app_settings.NGRAM_MAX, type=int
    )

    parser.add_argument(
        "--num_doc",
        default=style_app_settings.NUM_DOC,
        type=int,
        help="It is a Resample_balanced method parameter. It specifies the number of "
        "documents for every author should have.",
    )
    parser.add_argument(
        "--resampling_percentage",
        default=1.0,
        type=float,
        help="Size of resampling",
    )
    parser.add_argument(
        "--n_jobs",
        default=style_app_settings.N_JOBS,
        type=int,
        help="It is an integer, specifying the maximum number of concurrently running workers.",
    )

    parser.add_argument(
        "--vectorizer",
        default=style_app_settings.VECTORIZER,
    )

    parser.add_argument("--n_features", default=style_app_settings.N_FEATURES)
    args = parser.parse_args()
    print(args)

    # argparse
    # dataset
    num_books = args.num_books
    document_length = args.document_length

    # train model
    cross_validation = args.cross_validation
    num_job = args.n_jobs
    # split dataset
    test_percentage = args.test_percentage
    # under pipeline
    min_df = args.min_df
    normalize = args.normalize
    reduction = args.reduction
    num_doc = args.num_doc

    ngram_min = args.ngram_min
    ngram_max = args.ngram_max

    vectorizer = args.vectorizer
    n_features = args.n_features

    classifiers = [
        ("clf_svc", LinearSVC),
        ("clf_nb", MultinomialNB),
        ("clf_sgd", SGDClassifier),
    ]

    ngram_range = (ngram_min, ngram_max)

    parameters = [
        {
            "vectorize__ngram_range": [ngram_range],
            "clf_svc__C": [0.01, 0.1, 1, 10],
        },
        {
            "vectorize__ngram_range": [ngram_range],
            "clf_nb__alpha": [0.1, 0.3, 0.5, 0.8, 1],
        },
        {
            "vectorize__ngram_range": [ngram_range],
            "clf_sgd__loss": [
                "hinge",
                "squared_hinge",
            ],
            "clf_sgd__penalty": ["l2", "l1"],
        },
    ]

    dataset = DatasetReader.load_files(
        FILE_PATH_BOOK_DS, n=document_length, num_of_books=num_books
    )

    resampled_dataset = dataset.resample_balanced(num_doc=num_doc)

    exp_name = (
        f"experiment-date-{datetime.datetime.now().strftime('%Y-%m-%d-%H-%M')}"
    )
    experiment_dir_name = Path(exp_name)

    print(
        f"The total data size: {len(resampled_dataset)}. Experiment name: {exp_name}"
    )

    docs_train, docs_test, y_train, y_test, dataset_target = split_dataset(
        resampled_dataset, test_percentage=test_percentage
    )

    print(
        f"Size of the training data: {docs_train.shape} and size of the test "
        f"data {docs_test.shape}"
    )

    model_comparison_results = dd(dict)

    for (name, clf), params in list(zip(classifiers, parameters)):
        print(f"Parameter Search started for {name} with {params}")
        model_export_path = MODEL_EXPORT_PATH / (name + ".model")

        pipeline = create_pipeline(
            name,
            clf(),
            vectorizer=vectorizer,
            min_df=min_df,
            normalize=normalize,
            reduction=reduction,
            n_features=n_features,
        )
        model, report_, report_dict, cm = train_sklearn_classification_model(
            docs_train,
            docs_test,
            y_train,
            y_test,
            pipeline,
            params,
            n_jobs=num_job,
            cv=cross_validation,
        )

        print(f"Parameter Search finished for {name} with {params}")
        export(model, model_export_path)

        storage_client.upload(
            model_export_path,
            "style-resources" / experiment_dir_name / model_export_path.name,
        )

        model_comparison_results[name]["f1-weighted-avg"] = report_dict[
            "weighted avg"
        ]["f1-score"]
        model_comparison_results[name]["best-ngram"] = model.get_params()[
            "vectorize__ngram_range"
        ]
        print(f"The best model is written for {name} {model_export_path}")

        report(report_, name, cm, MODEL_EXPORT_PATH)

        report_output_path = (
            "style-resources" / experiment_dir_name / f"{name}-report.txt"
        )
        storage_client.upload(
            MODEL_EXPORT_PATH / f"{name}-report.txt", report_output_path
        )
        print(f"Report is written to {report_output_path}")

        confusion_matrix_output_path = (
            "style-resources" / experiment_dir_name / f"{name}-cm.txt"
        )
        storage_client.upload(
            MODEL_EXPORT_PATH / f"{name}-cm.txt", confusion_matrix_output_path
        )
        print(f"Confusion matrix is written to {confusion_matrix_output_path}")

    model_comparison_report(
        MODEL_EXPORT_PATH, args.__dict__, model_comparison_results
    )

    model_comparison_report_path = (
        "style-resources" / experiment_dir_name / "model_comparison_report.tsv"
    )

    storage_client.upload(
        MODEL_EXPORT_PATH / "model_comparison_report.tsv",
        model_comparison_report_path,
    )

    print(f"Model Comparison is written to {model_comparison_report_path}")
    print("Training & grid search & finding the best model is done.")

if __name__ == "__main__":
    run()


from mapFile import dave
import random

graph = []
for x in dave.adjacencies:
    graph.append(x)
target = random.choice(graph)
nodeCount = len(dave.adjacencies)
print("Start:",graph[0])
print("Target:",target)
print("Node Count:",nodeCount)

def findPath(graph,start,target): #returns a path to the end node from the start node
    q = [[start]]
    visited = []
    if start == target:
        return "You are on your target"
    while len(q) > 0:
        path = q.pop(0)
        node = path[-1]
        if node not in visited and node != "":
            adjacent = graph[node].split(";")
            for x in adjacent:
                tempPath = list(path)
                tempPath.append(x)
                q.append(tempPath)
                if x == target:
                    return tempPath

            visited.append(node)

print(findPath(dave.adjacencies,graph[0],target))


class Solution:
    def numRescueBoats(self, people: List[int], limit: int) -> int:
        people.sort()
        heaviest = len(people)-1
        lightest = 0
        boats = 0

        while lightest <= heaviest:
            if(people[lightest] > limit):
                break
            elif(lightest == heaviest and people[lightest]<=limit):
                boats+=1
                heaviest-=1
            elif(people[lightest]+people[heaviest]<= limit):
                boats+=1
                lightest+=1
                heaviest-=1
            elif(people[heaviest] <= limit):
                boats+=1
                heaviest-=1
            print(heaviest,lightest,boats)

        return boats


class Solution:
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
        if not intervals:
            return []
        if len(intervals) == 1:
            return intervals
        result = []
        for i in sorted(intervals, key=lambda x:x[0]):
            if result and i[0] <= result[-1][1]:
                if i[1] >= result[-1][1]:
                    result[-1][1] = i[1]
            else:
                result.append(i)
        return result


from datetime import datetime
import time
import os
import re

jsonCols = ("\"cols\":[" +
            "{\"type\":\"datetime\",\"id\":\"Time\",\"label\":\"Time\"}," +
            "{\"type\":\"number\",\"id\":\"BeerTemp\",\"label\":\"Beer temperature\"}," +
            "{\"type\":\"number\",\"id\":\"BeerSet\",\"label\":\"Beer setting\"}," +
            "{\"type\":\"string\",\"id\":\"BeerAnn\",\"label\":\"Beer Annotate\"}," +
            "{\"type\":\"number\",\"id\":\"FridgeTemp\",\"label\":\"Fridge temperature\"}," +
            "{\"type\":\"number\",\"id\":\"FridgeSet\",\"label\":\"Fridge setting\"}," +
            "{\"type\":\"string\",\"id\":\"FridgeAnn\",\"label\":\"Fridge Annotate\"}," +
            "{\"type\":\"number\",\"id\":\"RoomTemp\",\"label\":\"Room temp.\"}," +
            "{\"type\":\"number\",\"id\":\"State\",\"label\":\"State\"}" +
            "]")

def fixJson(j):
	j = re.sub(r"'{\s*?(|\w)", r'{"\1', j)
	j = re.sub(r"',\s*?(|\w)", r',"\1', j)
	j = re.sub(r"'(|\w)?\s*:", r'\1":', j)
	j = re.sub(r"':\s*(|\w*)\s*(|[,}])", r':"\1"\2', j)
	return j

def addRow(jsonFileName, row):
	jsonFile = open(jsonFileName, "r+")
	jsonFile.seek(-3, 2)  # Go insert point to add the last row
	ch = jsonFile.read(1)
	jsonFile.seek(0, os.SEEK_CUR)
	# when alternating between reads and writes, the file contents should be flushed, see
	# http://bugs.python.org/issue3207. This prevents IOError, Errno 0
	if ch != '[':
		# not the first item
		jsonFile.write(',')
	newRow = {}
	newRow['Time'] = datetime.today()

	# insert something like this into the file:
	# {"c":[{"v":"Date(2012,8,26,0,1,0)"},{"v":18.96},{"v":19.0},null,{"v":19.94},{"v":19.6},null]},
	jsonFile.write(os.linesep)
	jsonFile.write("{\"c\":[")
	now = datetime.now()
	jsonFile.write("{{\"v\":\"Date({y},{M},{d},{h},{m},{s})\"}},".format(
		y=now.year, M=(now.month - 1), d=now.day, h=now.hour, m=now.minute, s=now.second))
	if row['BeerTemp'] is None:
		jsonFile.write("null,")
	else:
		jsonFile.write("{\"v\":" + str(row['BeerTemp']) + "},")

	if row['BeerSet'] is None:
		jsonFile.write("null,")
	else:
		jsonFile.write("{\"v\":" + str(row['BeerSet']) + "},")

	if row['BeerAnn'] is None:
		jsonFile.write("null,")
	else:
		jsonFile.write("{\"v\":\"" + str(row['BeerAnn']) + "\"},")

	if row['FridgeTemp'] is None:
		jsonFile.write("null,")
	else:
		jsonFile.write("{\"v\":" + str(row['FridgeTemp']) + "},")

	if row['FridgeSet'] is None:
		jsonFile.write("null,")
	else:
		jsonFile.write("{\"v\":" + str(row['FridgeSet']) + "},")

	if row['FridgeAnn'] is None:
		jsonFile.write("null,")
	else:
		jsonFile.write("{\"v\":\"" + str(row['FridgeAnn']) + "\"},")

	if row['RoomTemp'] is None:
		jsonFile.write("null,")
	else:
		jsonFile.write("{\"v\":" + str(row['RoomTemp']) + "},")

	if row['State'] is None:
		jsonFile.write("null")
	else:
		jsonFile.write("{\"v\":" + str(row['State']) + "}")

	# rewrite end of json file
	jsonFile.write("]}]}")
	jsonFile.close()

def newEmptyFile(jsonFileName):
	jsonFile = open(jsonFileName, "w")
	jsonFile.write("{" + jsonCols + ",\"rows\":[]}")
	jsonFile.close()


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

from model.recall import *
from model.dataset.dataset import *

missoni = MyDataset(brand='missoni')

missoni.fit(metadata=['saleLine', 'macro'])

recall = Recall(dataset=missoni,
                n_factors=80,
                net_type='lightfm',
                use_metadata=True,
                use_cuda=False)

optimizer = torch.optim.Adam(recall.net.parameters(),
                             lr=3e-3)

recall.fit(optimizer=optimizer,
           batch_size=10_240,
           epochs=20,
           split_train_test=True,
           verbose=True)


import random
import string
import pytest

from selenium import webdriver as webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

from page_objects.home_page import HomePage

def pytest_addoption(parser):
    parser.addoption(
        "--browser",
        default="chrome",
        help="Options: firefox, chrome, opera. Default: chrome",
        choices=("chrome", "firefox", "opera")
    )
    parser.addoption(
        "--base_url",
        help="base_url"
    )

@pytest.fixture()
def base_url(request):
    return request.config.getoption("--base_url")

@pytest.fixture()
def driver(request):
    global browser
    browser_name = request.config.getoption("--browser")
    service = ChromeService()

    if browser_name == "chrome":
        browser = webdriver.Chrome(service=service)
    elif browser_name == "firefox":
        browser = webdriver.Firefox(service=service)
    elif browser_name == "opera":
        browser = webdriver.Chrome(service=service)

    yield browser

    browser.close()

@pytest.fixture()
def clear_cart(driver, base_url):
    home_page = HomePage(driver)
    home_page.open(base_url)

    home_page.click_cart()
    product_number = home_page.number_of_products_in_cart()
    if product_number > 0:
        for product in range(product_number):
            driver.find_element(By.CSS_SELECTOR, '[title="Remove"]')

    yield

    home_page.click_cart()
    product_number = home_page.number_of_products_in_cart()
    if product_number > 0:
        for product in range(product_number):
            driver.find_element(By.CSS_SELECTOR, '[title="Remove"]')

@pytest.fixture()
def random_email():
    random_email = ''
    for x in range(10):
        random_email += ''.join(random.choice(string.ascii_lowercase))
    yield random_email + "@gmail.com"


from django.urls import path
from . import api
from rest_framework_swagger.views import get_swagger_view

schema_view = get_swagger_view(title='Protein API')

urlpatterns = [
    path('', schema_view),
    path('api/protein/', api.ProteinCreate.as_view(), name='protein-create'),
    path('api/protein/<protein_id>', api.ProteinDetail.as_view(), name='protein-detail'),
    path('api/proteins/<taxonomy>', api.ProteinListByOrganism.as_view(), name='protein-list-by-organism'),
    path('api/pfam/<domain_id>', api.PfamDetail.as_view(), name='domain-pfam-detail'),
    path('api/pfams/<taxonomy>', api.PfamListByOrganism.as_view(), name='pfam-list-by-organism'),
    path('api/coverage/<protein_id>', api.ProteinDomainCoverage.as_view(), name='protein-domain-coverage'),
]


class myHashTable:
	KEY_NOT_FOUNDED = -999
	def __init__( self, size ):
		self._size = size
		self._elems = [ None ] * size
		self._linearHashDelta = [ x for x in range( 1, size ) ]

	'''
	hash函数，求key在数组中的索引
	'''
	def _hashFunc( self, key ):
		asciiSum = 0
		for c in key:
			asciiSum += ord( c )
		print( 'key:%s, asciiSum:%d, hashIndex:%d' % ( key, asciiSum, asciiSum % self._size ) )
		return asciiSum % self._size

	'''
	开放定址法（线性探测再散列）处理冲突
	@param collisionHashIndex 冲突时的hash地址
	@param delta 标识第几次处理冲突
	'''
	def _collisionFunc( self, collisionHashIndex, delta ):
		if delta >= self._size - 1:
			print( 'hashTable overflow' )
			return myHashTable.KEY_NOT_FOUNDED
		return ( collisionHashIndex + self._linearHashDelta[ delta ] ) % self._size

	'''
	寻找hashKey对应的hash地址
	'''
	def _search( self, key ):
		tmpHashIndex = self._hashFunc( key )
		hashIndex = tmpHashIndex
		hashData = self._elems[ tmpHashIndex ]
		delta = 0
		while hashData and hashData['key'] != key:
			hashIndex = self._collisionFunc( tmpHashIndex, delta )
			if hashIndex == myHashTable.KEY_NOT_FOUNDED: break
			hashData = self._elems[ hashIndex ]
			delta += 1
		return hashIndex

	def get( self, key ):
		hashIndex = self._search( key )
		if hashIndex == myHashTable.KEY_NOT_FOUNDED:
			return None
		hashData = self._elems[ hashIndex ]
		if not hashData:
			return None
		return hashData['value']

	def put( self, key, value ):
		hashIndex = self._search( key )
		self._elems[ hashIndex ] = { 'key': key, 'value': value }

hashTab = myHashTable( 6 )

hashTab.put( 'aj', 'value1' )
print( hashTab.get( 'aj' ) )

hashTab.put( 'bi', 'value2' )
print( hashTab.get( 'bi' ) )

hashTab.put( 'ch', 'value3' )
print( hashTab.get( 'ch' ) )

hashTab.put( 'dg', 'value4' )
print( hashTab.get( 'dg' ) )

hashTab.put( 'ef', 'value5' )
print( hashTab.get( 'ef' ) )

hashTab.put( 'hahaha44444444444444444444', 'value5' )
print( hashTab.get( 'hahaha' ) )

print( hashTab._elems )

#a b c d e f g h i j


import math

import torch
import torch.nn as nn
import torch.nn.functional as F

import config as cfg
from utils.helpers import truncated_normal_

class LSTMGenerator(nn.Module):

    def __init__(self, embedding_dim, hidden_dim, vocab_size, max_seq_len, padding_idx, gpu=False):
        super(LSTMGenerator, self).__init__()
        self.name = 'vanilla'

        self.hidden_dim = hidden_dim
        self.embedding_dim = embedding_dim
        self.max_seq_len = max_seq_len
        self.vocab_size = vocab_size
        self.padding_idx = padding_idx
        self.gpu = gpu

        self.temperature = 1.0

        self.embeddings = nn.Embedding(vocab_size, embedding_dim, padding_idx=padding_idx)
        self.lstm = nn.LSTM(embedding_dim, hidden_dim, batch_first=True)
        self.lstm2out = nn.Linear(hidden_dim, vocab_size)
        self.softmax = nn.LogSoftmax(dim=-1)

        self.init_params()

    def forward(self, inp, hidden, need_hidden=False):
        """
        Embeds input and applies LSTM
        :param inp: batch_size * seq_len
        :param hidden: (h, c)
        :param need_hidden: if return hidden, use for sampling
        """
        emb = self.embeddings(inp)  # batch_size * len * embedding_dim
        if len(inp.size()) == 1:
            emb = emb.unsqueeze(1)  # batch_size * 1 * embedding_dim

        out, hidden = self.lstm(emb, hidden)  # out: batch_size * seq_len * hidden_dim
        out = out.contiguous().view(-1, self.hidden_dim)  # out: (batch_size * len) * hidden_dim
        out = self.lstm2out(out)  # batch_size * seq_len * vocab_size
        out = self.temperature * out  # temperature
        pred = self.softmax(out)

        if need_hidden:
            return pred, hidden
        else:
            return pred

    def sample(self, num_samples, batch_size, start_letter=cfg.start_letter):
        """
        Samples the network and returns num_samples samples of length max_seq_len.
        :return samples: num_samples * max_seq_length (a sampled sequence in each row)
        """
        num_batch = num_samples // batch_size + 1 if num_samples != batch_size else 1
        samples = torch.zeros(num_batch * batch_size, self.max_seq_len).long()

        # 用LSTM随机生成batch_size个句子，生成下一个词的时候是按多项式分布来选择的，而不是概率最大那个
        for b in range(num_batch):
            hidden = self.init_hidden(batch_size)
            inp = torch.LongTensor([start_letter] * batch_size)
            if self.gpu:
                inp = inp.cuda()

            for i in range(self.max_seq_len):
                out, hidden = self.forward(inp, hidden, need_hidden=True)  # out: num_samples * vocab_size
                next_token = torch.multinomial(torch.exp(out), 1)  # num_samples * 1 (sampling from each row)
                samples[b * batch_size:(b + 1) * batch_size, i] = next_token.view(-1)
                inp = next_token.view(-1)
        samples = samples[:num_samples]

        return samples

    def init_params(self):
        for param in self.parameters():
            if param.requires_grad and len(param.shape) > 0:
                stddev = 1 / math.sqrt(param.shape[0])
                if cfg.gen_init == 'uniform':
                    torch.nn.init.uniform_(param, a=-0.05, b=0.05)
                elif cfg.gen_init == 'normal':
                    torch.nn.init.normal_(param, std=stddev)
                elif cfg.gen_init == 'truncated_normal':
                    truncated_normal_(param, std=stddev)

    def init_oracle(self):
        for param in self.parameters():
            if param.requires_grad:
                torch.nn.init.normal_(param, mean=0, std=1)

    def init_hidden(self, batch_size=cfg.batch_size):
        h = torch.zeros(1, batch_size, self.hidden_dim)
        c = torch.zeros(1, batch_size, self.hidden_dim)

        if self.gpu:
            return h.cuda(), c.cuda()
        else:
            return h, c

    def sample_vanilla_theta(self, batch_size, start_letter=cfg.start_letter):
        """
        Samples the network based on argmax theta (no Gumbel involved).
        :param start_letter: index of start_token
        :return θ: batch_size * max_seq_length * vocab_size
        """
        vanilla_theta = torch.zeros(batch_size, self.max_seq_len, self.vocab_size, dtype=torch.float)

        hidden = self.init_hidden(batch_size)
        inp = torch.LongTensor([start_letter] * batch_size)
        if self.gpu:
            inp = inp.cuda()

        for i in range(self.max_seq_len):
            emb = self.embeddings(inp).unsqueeze(1)  # batch_size * 1 * embedding_dim
            out, hidden = self.lstm(emb, hidden)
            out = self.lstm2out(out.squeeze(1))  # batch_size * vocab_size
            out = F.softmax(out, dim=-1)  # batch_size * vocab_size
            next_token = torch.argmax(out, dim=1).detach()  # batch_size * vocab_size

            vanilla_theta[:, i, :] = out
            inp = next_token.view(-1)

        if self.gpu:
            vanilla_theta = vanilla_theta.cuda()
        return vanilla_theta


import threading

class ReverseStrThread(threading.Thread):

    sentence = ""

    def __init__(self, word):
        threading.Thread.__init__(self)
        if not isinstance(word, str):
            raise Exception("word is not a string")
        else:

            self.__word = word

    def run(self):
        ReverseStrThread.sentence += " "+self.__word[::-1]


import numpy as np
import random
import torch
import torch.nn as nn
import torch.nn.functional as F
from copy import deepcopy
from collections import deque
from Agents.Utilities.LinearTransformations import transform_interval

class DDPG:
    def __init__(self, action_min, action_max, q_model, pi_model, noise,
                 q_model_lr=1e-3, pi_model_lr=1e-4, gamma=0.99, batch_size=64, tau=1e-3,
                 memory_len=100000):

        self.action_min = action_min
        self.action_max = action_max
        self.q_model = q_model
        self.pi_model = pi_model
        self.noise = noise

        self.q_model_lr = q_model_lr
        self.pi_model_lr = pi_model_lr
        self.gamma = gamma
        self.batch_size = batch_size
        self.tau = tau

        self.memory = deque(maxlen=memory_len)
        self.q_optimizer = torch.optim.Adam(self.q_model.parameters(), lr=q_model_lr)
        self.pi_optimizer = torch.optim.Adam(self.pi_model.parameters(), lr=pi_model_lr)
        self.q_target_model = deepcopy(self.q_model)
        self.pi_target_model = deepcopy(self.pi_model)
        return None

    def get_action(self, state):
        action = self.pi_model(state).detach().numpy() + self.noise.get()
        action = self.transform_interval(action)
        return np.clip(action, self.action_min, self.action_max)

    def transform_interval(self, action):
        return transform_interval(action, self.action_min, self.action_max)

    def fit(self, state, action, reward, done, next_state):
        #add to memory
        self.memory.append([state, action, reward, done, next_state])

        if len(self.memory) >= self.batch_size:
            #get batch
            batch = random.sample(self.memory, self.batch_size)
            states, actions, rewards, dones, next_states = map(torch.FloatTensor, zip(*batch))
            rewards = rewards.reshape(self.batch_size, 1)
            dones = dones.reshape(self.batch_size, 1)

            #get targets
            pred_next_actions = self.transform_interval(self.pi_target_model(next_states))
            next_q_values = self.get_q_values(self.q_target_model, next_states, pred_next_actions)
            targets = rewards + (1 - dones) * self.gamma * next_q_values

            #train q_model
            q_values = self.get_q_values(self.q_model, states, actions)
            q_loss = torch.mean((q_values - targets.detach()) ** 2)
            self.update_target_model(self.q_target_model, self.q_model, self.q_optimizer, q_loss)

            #train u_pi_model
            pred_actions = self.transform_interval(self.pi_model(states))
            q_values = self.get_q_values(self.q_model, states, pred_actions)
            pi_loss = - torch.mean(q_values)
            self.update_target_model(self.pi_target_model, self.pi_model, self.pi_optimizer, pi_loss)

        return None

    def get_q_values(self, q_model, states, actions):
        states_and_actions = torch.cat((states, actions), dim=1)
        return q_model(states_and_actions)

    def update_target_model(self, target_model, model, optimizer, loss):
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        for target_param, param in zip(target_model.parameters(), model.parameters()):
            target_param.data.copy_((1 - self.tau) * target_param.data + self.tau * param.data)
        return None

    def reset(self):
        self.noise.reset()
        self.noise.reduce()
        return None


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

from wdom.tag import Tag, H1, Button
from wdom.document import get_document

def sample_page() -> Tag:
    doc = get_document()
    text = H1(parent=doc.body)
    text.textContent = 'Title!'
    btn = Button('click!', parent=doc.body)

    def get_rect(data):
        print('send query')

        def show_rect(fut):
            print('DONE')
            print(fut.result())
        fut = text.getBoundingClientRect()
        fut.add_done_callback(show_rect)
        print(fut)

    btn.addEventListener('click', get_rect)

    return doc


# The idea is that you split the list into two lists: low and high. The separating
# point is TARGET / 2. This reduces the number of comparisons as two numbers in the
# high list will always be > TARGET, so they never need to be compared. The price of
# comparison is much more expensive that the price of sorting the list.

import sys

TARGET = 2020

if __name__ == "__main__":
    # Read in file and clean data
    f = open("puzzle_input.txt", "r")
    data = f.readlines()
    data = [int(x) for x in data]

    # Sort the list
    data.sort()

    # Split into low and high
    low = []
    high = []
    for d in data:
        if d < TARGET / 2:
            low.append(d)
        else:
            high.append(d)

    # Compare the low list against the high list
    for l in low:
        for h in high:
            my_sum = l + h

            if my_sum > TARGET:
                # Summing more values in the high list are guaranteed to be > TARGET
                break
            elif my_sum == TARGET:
                print(f"Two values are {l} * {h} = {l * h}")
                sys.exit(0)


# Import libraries
import sys
import csv

# Define the main function

def main():

    # Check if the user input the correct name of arguments
    if len(sys.argv) != 3:
        sys.exit("Usage: python dna.py data.csv sequence.txt")

    # Load CSV file containing names and DNA sequences
    # Load the file into a variable
    database_path = sys.argv[1]
    with open(database_path) as dna_database:
        # Create a reader
        reader = csv.reader(dna_database)
        # Create a variable who store all the STR in the database header
        STR_references = next(reader)[1:]

        # Load txt file, containing a DNA sequence
        sequence_file = sys.argv[2]
        with open(sequence_file) as dna_sequence:
            sequence = dna_sequence.read()
            # Counts the longest run consecutives repeats for each DNA sequences
            STR_counts = [max_consecutive_str(sequence, STR) for STR in STR_references]

        # Compare the sequences repeats number to the database and find if there's a mach
        match = check_match(reader, STR_counts)

        # Print the name of the match or the "No match" message
        if match == "nobody":
            print("No match")
        else:
            print(match)

# Define a function that calulcate the max number of time a STR is repetead into a string

def max_consecutive_str(string, STR):
    # Prepare and inizialize a counts variable to use as a reference with the input string
    counts = [0] * len(string)
    # Run a loop that look for every chunk of DNA in the input with a size of the STR you're searching
    for i in range(len(string) - len(STR), -1, -1):
        # Check if this single chunk is equal to the STR of reference
        if string[i: i + len(STR)] == STR:
            # Set the counter for that place of the string as 1 if it's the last chunk of DNA
            if i + len(STR) > len(string) - 1:
                counts[i] = 1
            # Set the counter for that place of the string as +1 of the previous chunk counter
            else:
                counts[i] = 1 + counts[i + len(STR)]
    # Return the maximum number of consecutive STR found in the input txt
    return max(counts)

# Define a function that compare a database with a list of STR counters to check if there's a match and return the name of it

def check_match(database, STR_counts):
    for line in database:
        person = line[0]
        c = [int(value) for value in line[1:]]
        if c == STR_counts:
            return(person)
    return("nobody")

# Call the main function

if __name__ == "__main__":
    main()


#!/usr/bin/env python3

"""Parse and save tables in a web page into tab-separated-values (tsv) files.

Takes input from stdin.

Writes files in the current directory based on "id" attribute of each table.

NOTE: to fit into tsv, cell values \t \n \r are made into spaces instead.
To exactly preserve values, we'll have to use csv.

Based on https://srome.github.io/Parsing-HTML-Tables-in-Python-with-BeautifulSoup-and-pandas/

Requires beautifulsoup4==4.9.1.
"""

import argparse
import logging
import sys
from bs4 import BeautifulSoup

def parse_text(txt, leave_html):
    """Parse all tables in the given HTML text"""
    soup = BeautifulSoup(txt, 'html.parser')
    idx = 1
    for table in soup.find_all('table'):
        _parse_html_table(table, f'table{idx:03d}', leave_html)
        idx += 1

def _clean_text(txt):
    """Make \t, \n, \r into spaces"""
    return txt.replace("\n", " ").replace("\t", " ").replace("\r", " ")

def _get_text(elem, leave_html):
    """Return elem.get_text() if leave_html is False, else str(elem)

    elem should be a td or th element.
    """
    assert elem.name in (u'td', u'th')
    val = None
    if leave_html:
        # Take everything, but not including the outer td or th tag
        val = ''.join([str(elem1) for elem1 in elem.contents])
    else:
        # only the text
        val = elem.get_text()
    return val

def _parse_html_table(table, backup_id, leave_html):
    """Parse tables out of the beautifulsoup table and write them into files."""
    n_columns = 0
    column_names = []

    table_id = table.get('id', backup_id)
    # logging.info(f"*** table {table_id}")
    # Find number of rows and columns
    # we also find the column titles if we can
    for row in table.find_all('tr'):
        # Determine the number of rows in the table
        td_tags = row.find_all('td')
        if len(td_tags) > 0:
            if n_columns == 0:
                # Set the number of columns for our table
                # TODO(dan): Handle colspan > 1
                n_columns = len(td_tags)

        # Handle column names if we find them
        th_tags = row.find_all('th')
        if len(th_tags) > 0 and len(column_names) == 0:
            assert len(column_names) == 0, "found more column names"
            for th in th_tags:
                column_names.append(_get_text(th, leave_html))

    # TODO(dan): Handle colspan > 1
    # Safeguard on Column Titles
    # if len(column_names) > 0 and len(column_names) != n_columns:
    #    raise Exception("Column titles do not match the number of columns")

    # TODO(dan): Handle table without id attribute
    filename = f"{table_id}.tsv"
    logging.info(f"Write {filename}..")
    with open(filename, 'w') as the_tsv:
        if not column_names:
            # there were no column names, so print a fake header col1, col2, ..
            print('\t'.join([f"col{col}" for col in range(n_columns)]),
                  file=the_tsv)
        for row in table.find_all('tr'):
            # always look for td or th because sometimes people use either in
            # a row, possibly for formatting reasons
            # this has the side effect of printing any existing table headers
            columns = row.find_all(['td', 'th'])
            col_texts = [_get_text(col, leave_html) for col in columns]
            if col_texts:
                print('\t'.join([_clean_text(col) for col in col_texts]),
                      file=the_tsv)

def main():
    parser = argparse.ArgumentParser(
        description='Convert HTML tables to tab-separated values files')
    parser.add_argument('--leave-html',
                        action='store_true',
                        default=False,
                        help='Leave the HTML in each cell')
    args = parser.parse_args()

    logging.basicConfig(format='%(name)s: %(message)s', level=logging.INFO)
    parse_text(sys.stdin.read(), args.leave_html)

main()


from bs4 import BeautifulSoup
import urllib.request as req
import os

tool_list = ['tool_a', 'tool_ka', 'tool_sa', 'tool_ta', 'tool_na',
            'tool_ha', 'tool_ma', 'tool_ya', 'tool_ra', 'tool_wa']

# 区切りの文字を指定
sep_str = '\t'

# ひみつ道具の一覧を取得
write_str = ""
for tool in tool_list:
    url = 'https://www.tv-asahi.co.jp/apps/page/back_number.php?P_ID=1575&C_DIR=' + \
          tool + '&TYPE=pc&CNT=400&ITEM=ttl'

    res = req.urlopen(url)
    html = res.read().decode('shift-jis')

    # 取得元のhtmlの構造にエラーあり。
    html = html.replace('</font>', '')

    soup = BeautifulSoup(html, 'html.parser')

    item_list = soup.select('dl > li > dt')
    rubi_list = soup.select('dl > li > dt > span')
    desc_list = soup.select('dl > li > dd')

    for i in range(len(item_list)):
        write_str += item_list[i].text.split('\n')[1].strip() + sep_str + \
        rubi_list[i].text + sep_str + desc_list[i].text + "\n"

    res.close()

# ファイル書き込み
path_w = os.getcwd() + "/data/item_list.tsv"

with open(path_w, mode='w') as f:
    f.write(write_str)


import os
import HighFidelityTrajectory
import Universe
import sys

if len(sys.argv) < 4:
        raise Exception("Unknown number of command line options!\n\
                         Syntax: python HighFidelityDriver.py source_emtgopt source_emtg outputFilePath\n")

originalOptionsFile = sys.argv[1]
originalMissionFile = sys.argv[2]
outputFilePath = sys.argv[3]

myHighFidelityTrajectory = HighFidelityTrajectory.HighFidelityTrajectory(originalMissionPath=originalMissionFile,
                                                                         originalOptionsPath=originalOptionsFile,
                                                                         useSundman=False)

myHighFidelityTrajectory.CreateMission()
myHighFidelityTrajectory.CreateInitialGuess()

newOptions = myHighFidelityTrajectory.getMissionOptions()
newOptions.short_output_file_names = 1
newOptions.mission_type = 9 #variable phase type
newOptions.MBH_time_hop_probability = 0.0
newOptions.mission_name = originalMissionFile.replace('.emtg', '') + '_HighFidelity'
newOptions.DisassembleMasterConstraintVectors()
newOptions.DisassembleMasterDecisionVector()

# create a universe object for each central body in the new options
central_body_names = []
universe_list = []
for JourneyIndex in range(0, len(newOptions.Journeys)):
    central_body_name = newOptions.Journeys[JourneyIndex].journey_central_body
    if central_body_name not in central_body_names:
        central_body_names.append(central_body_name)
        universe_file = central_body_name + ".emtg_universe"
        universe = Universe.Universe(os.path.join(newOptions.universe_folder, universe_file))
        universe_list.append(universe)

# add all of the bodies in each Journey's central body universe file as perturbers
for JourneyIndex in range(0, len(newOptions.Journeys)):
    central_body_name = newOptions.Journeys[JourneyIndex].journey_central_body
    for universe in universe_list:
        if universe.central_body_name == central_body_name:
            perturbation_body_IDs = [index + 1 for index in universe.perturbation_indices]
            newOptions.Journeys[JourneyIndex].perturbation_bodies = perturbation_body_IDs

newOptions.write_options_file(outputFilePath + '/' + newOptions.mission_name + '.emtgopt')


'''
Homework 9 Exercise 2
Riley Fitzgibbons
4/30/19
Create tcp client to communicate with server

Base code from: https://gist.github.com/Integralist/3f004c3594bbf8431c15ed6db15809ae
'''

import socket

hostname, sld, tld, port = 'www', 'integralist', 'co.uk', 80
target = '%s.%s.%s', (hostname, sld, tld)

# create an ipv4 (AF_INET) socket object using the tcp protocol (SOCK_STREAM)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# connect the client
# client.connect((target, port))
client.connect(('0.0.0.0', 9999))

# Connect and commiunicate to get menu
client.send('hello')

# receive the response data (4096 is recommended buffer size)
response = client.recv(4096)
print(response)

# Check on serv_str
client.send('1')
response = client.recv(4096)
print(response)

# Tell server to change serv_str
client.send('2 new server string')
response = client.recv(4096)
print(response)

# Exit sever
client.send('2 new server string')


#!python3
# -*- coding: utf-8 -*-

import re
'''Capturing one line of a document and converting it to an object.'''

class Line:
    '''One Line object is constructed from one markdown source line.'''

    # The following regular expressions are used for recognition
    # of the used markdown-syntax lines.

    # Title like ### Title ###.
    rexTitle = re.compile(r'^(?P<level>#+)\s+(?P<title>.+?)\s+\1\s*$')

    # Unnumbered list item.
    rexBullet = re.compile(r'^\*\s+(?P<uli>.+?)\s*$')

    # Image insertion (includes the filename).
    rexInsImg = re.compile(r'^Insert\s+(?P<img>\d+fig\d+\.png)\s*$')

    # Image caption.
    rexImgCaption = re.compile(r'^(Fig(ure)?|Obrázek)\.\s+(?P<num>\d+.+\d+).?\s+(?P<text>.+?)\s*$')

    # One code-snippet line.
    rexCode = re.compile(r'^( {4}|\t)(?P<code>.+?)\s*$')

    # Numbered list item.
    rexLi = re.compile(r'^(?P<num>\d+\.)\s+(?P<text>.+?)\s*$')

    def __init__(self, fname, lineno, line):
        self.fname = fname      # the source file name
        self.lineno = lineno    # line number in the source file
        self.line = line        # the line from the source file

        self.type = None        # line type
        self.attrib = None      # init -- line attributes (the type dependent)

        # The line that contains only whitespaces is considered empty (separator).
        if self.line.isspace():
            self.type = 'empty'
            self.attrib = ''   # represented as empty string
            return

        # Line with the title.
        m = self.rexTitle.match(line)
        if m:
            self.type = 'title'
            self.attrib = (len(m.group('level')), m.group('title'))
            return

        # Unnumbered list item.
        m = self.rexBullet.match(line)
        if m:
            self.type = 'uli'
            self.attrib = m.group('uli')
            return

        # Numbered list item.
        m = self.rexLi.match(line)
        if m:
            self.type = 'li'
            self.attrib = (m.group('num'), m.group('text'))
            return

        # Image insertion.
        m = self.rexInsImg.match(line)
        if m:
            self.type = 'img'
            self.attrib = m.group('img')
            return

        # Image caption.
        m = self.rexImgCaption.match(line)
        if m:
            self.type = 'imgcaption'
            self.attrib = (m.group('num'), m.group('text'))
            return

        # Code-snippet line.
        m = self.rexCode.match(line)
        if m:
            self.type = 'code'
            self.attrib = m.group('code')
            return

        # The empty line should not happen, but it means that
        # there the content in the file was exhausted.
        # From the solved-problem point of view it is not a separator.
        if self.line == '':
            self.type = 'EOF'
            self.attrib = None
            return

        # The other cases are considered text lines.
        self.type = 'text'
        self.attrib = line.rstrip()

    def __repr__(self):
        return repr((self.fname, self.lineno, self.type, self.attrib))

    def __str__(self):
        return self.line

class Element:
    '''One Element object is constructed from one document lines.

    Implementation note: The earlier Element implementation was earlier
    what the Lines class is now. The Element was abstracted further to
    make the content less formatting-dependent. The main reason was that
    the translated paragraphs were often split to more physical lines
    than in the original. (The original typically contains paragraphs
    as one very long line.'''

    def __init__(self, docline):
        self.fname = docline.fname  # the source file name
        self.doclines = [docline]   # list of lines object
        self.type = docline.type    # element type
        self.attrib = docline.attrib # init -- element attributes (the type dependent)

        # Initial implementation just wraps the doc.Line objects
        # as one-line lists. Then it corrects the 'text' type to 'para'.
        if self.type == 'text':
            self.type = 'para'

    def append(self, docline):
        '''Appends the docline to the list.'''
        self.doclines.append(docline)

    def extend_lines_from(self, element):
        '''Extends the doclines list by items from the element.'''
        self.doclines.extend(element.doclines)

    def _line(self):
        '''Probably legacy -- to be replaced by something better.'''
        return self.doclines[0].line

    def value(self, rstrip_lines=True):
        '''Returns the lines of the element as one string.'''
        if rstrip_lines:
            return ' '.join(dl.line.rstrip() for dl in self.doclines)
        else:
            return ''.join(dl.line for dl in self.doclines)

    def lineno(self):
        '''Returns the source line numbers as a readable string range with dash.'''
        no1 = self.doclines[0].lineno
        no2 = self.doclines[-1].lineno
        if no1 == no2:
            return str(no1)
        else:
            return '{}-{}'.format(no1, no2)

    def __repr__(self):
        return repr((self.fname, self.no, self.type,
                     self.attrib, self.doclines))

    def __str__(self):
        return ''.join.self.lines