from math import nan, isnan
from numpy import vstack, hstack
from numpy import argmax
from numpy import asarray
from scipy.stats import norm
from warnings import catch_warnings
from warnings import simplefilter

desired_value = 1

class BayesOptimizer:
    def __init__(
        self,
        op,
        model,
        max_iterations=1000,
        epsilon=0.01,
    ):
        self.op = op
        self.model = model

        self.epsilon = epsilon

        self.max_iterations = max_iterations

        self.__best_found = nan
        self.__i = 0

    def optimize(self, debug=False, *args, **kwargs):
        """Method for opitmizing the objective function"""
        # sample the search space
        X = self.op.search_space.sample_multiple(kwargs["start_sample_size"])
        # print(X)
        y = asarray([self.op.function(x) for x in X])
        # print(y)
        self.__i = 0
        while self.__i < self.max_iterations:
            # select next point to sample
            x = self.__opt_acquisition(X, kwargs['sample_size'], y)
            # evaluate the point with the fuction
            actual = self.op.function(x)
            if isnan(self.__best_found) or self.__best_found < actual:
                self.__best_found = actual
            # print("next sample is with cost", x, actual)
            # summarize the finding
            est, _ = self.__surrogate([x])
            if debug:
                print(x + [actual])
                #print("Surrogate estimate", est)
                #print("Actual value ", actual)

            if (not isnan(self.op.optimal_value)) and abs(
                actual - self.op.optimal_value
            ) < self.epsilon:
                #p rint("Converged at ", self.__i, " iteration")
                return self.__i
            # add the data to the dataset
            X = vstack((X, [x]))
            y = hstack((y, actual))
            # update the model
            self.model.fit(X, y)
            self.__i += 1

        print("Finished at ", self.__i, " iterations")
        print("Best result is ", self.__best_found)
        print("Global optima is ", self.op.optimal_value)
        return self.__i

    def __acquisition(self, X, Xsamples):
        # calculate the best surrogate score found so far
        yhat, _ = self.__surrogate(X)
        best = max(yhat)
        # calculate mean and stdev via surrogate function
        mu, std = self.__surrogate(Xsamples)
        # mu = mu[:, 0]
        # calculate the probability of improvement
        probs = norm.cdf((mu - best) / (std + 1e-9))
        return probs

    def __opt_acquisition(self, X, num_samples, y):
        Xsamples = self.op.search_space.sample_multiple(num_samples)
        # calculate the acquisition function for each sample
        scores = self.__acquisition(X, Xsamples)
        # locate the index of the largest scores
        ix = argmax(scores)
        return Xsamples[ix]

    def __surrogate(self, X):
        # catch any warning generated when making a prediction
        with catch_warnings():
            simplefilter("ignore")
            return self.model.predict(X, return_std=True)

    @staticmethod
    def test_hyperparameters(op_problem, max_iterations, hp_array, avg_of=30):
        start_smp_sz = hp_array[0]
        smp_sz = hp_array[1]

        target = BayesOptimizer(
            op=op_problem,
            max_iterations=max_iterations,
            starting_sample_size=start_smp_sz,
            sample_size=smp_sz,
        )

        return target.optimize()


class Solution:
    def maxSatisfaction(self, satisfaction: List[int]) -> int:
        temp= 0
        res= 0
        satisfaction.sort()
        print(satisfaction)
        for i in range(len(satisfaction)):
            k= 1
            temp= 0
            for j in range(i, len(satisfaction)):
                temp += (satisfaction[j] * k)
                k+=1
            res= max(temp, res)
        return res


#!/usr/bin/env python

"""
    billy_pooling.py

    Compute bilinear features on the fly, w/ a 1x1 conv. layer to reduce dimensionality
    and avoid the quadratic explosion
"""

import bcolz
import pandas as pd

import keras
from keras.layers import *
from keras.models import Sequential, Model

from keras import backend as K
if K._BACKEND == 'tensorflow':
    def limit_mem():
        cfg = K.tf.ConfigProto()
        cfg.gpu_options.allow_growth = True
        cfg.gpu_options.visible_device_list="0"
        K.set_session(K.tf.Session(config=cfg))

    limit_mem()

def bili_pooling(x):
    shp = K.shape(x)
    bsz, w, h, c = shp[0], shp[1], shp[2], shp[3]

    # Bilinear pooling
    b1 = K.reshape(x, (bsz * w * h, c, 1))
    b2 = K.reshape(x, (bsz * w * h, 1, c))
    d  = K.reshape(K.batch_dot(b1, b2), (bsz, w * h, c * c))
    d = K.sum(d, 1)

    # Normalize
    d = K.sqrt(K.relu(d))
    d = K.l2_normalize(d, -1)
    return d

def bili_pooling_asym(data):
    x, y = data
    shp_x = K.shape(x)
    bsz, w, h, c_x = shp_x[0], shp_x[1], shp_x[2], shp_x[3]

    shp_y = K.shape(y)
    c_y = shp_y[3]

    # Bilinear pooling
    bx = K.reshape(x, (bsz * w * h, c_x, 1))
    by = K.reshape(y, (bsz * w * h, 1, c_y))

    # Normalize
    d = K.reshape(K.batch_dot(bx, by), (bsz, w * h, c_x * c_y))
    d = K.sum(d, 1)
    d = K.sqrt(K.relu(d))
    d = K.l2_normalize(d, axis=-1)
    return d

# --
# Prep

train_test_split = pd.read_csv('./data/cub/train_test_split.txt', sep=' ', header=None)
train_sel = np.array(train_test_split[1].astype('bool'))

image_ids = pd.read_csv('./data/cub/images.txt', sep=' ', header=None).set_index(1) - 1

# --
# Metadata IO

df = pd.read_csv('./data/convpaths', header=None)
df[0] = df[0].apply(lambda x: '/'.join(x.split('/')[-2:]))
lookup = pd.DataFrame(np.arange(df.shape[0]), index=df[0])
ord_ = np.array(lookup.loc[image_ids.index]).squeeze()

labs = np.array(df[0].apply(lambda x: x.split('/')[0]))[ord_]
n_classes = np.unique(labs).shape[0]
train_labs, test_labs = labs[train_sel], labs[~train_sel]

y_train = np.array(pd.get_dummies(train_labs)).argmax(1)
y_test = np.array(pd.get_dummies(test_labs)).argmax(1)

# Data IO
conv = bcolz.open('./conv.bc')[:]
train_conv = conv[ord_[train_sel]]
test_conv = conv[ord_[~train_sel]]

train_conv = train_conv.reshape((train_conv.shape[0], 28, 28, 512))
test_conv = test_conv.reshape((test_conv.shape[0], 28, 28, 512))

N = 16
M = 16

# Scaled down branch
inp_x = Input(shape=train_conv.shape[1:])
conv_x = Conv2D(N, (1, 1), data_format="channels_last")(inp_x)

# Identity branch
inp_y = Input(shape=train_conv.shape[1:])
conv_y = Conv2D(M, (1, 1), data_format="channels_last")(inp_y)

bili = Lambda(bili_pooling_asym, output_shape=(N * M,))([conv_x, conv_y])

x = Dense(512)(bili)
x = Dropout(0.5)(x)
x = Dense(n_classes, activation='softmax')(x)

model = Model(inputs=[inp_x, inp_y], outputs=x)
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['acc'])

conv_fitist = model.fit(
    [train_conv] * 2, y_train,
    verbose=True,
    batch_size=32,
    validation_data=([test_conv] * 2, y_test),
    epochs=50,
    callbacks=[
        keras.callbacks.EarlyStopping(patience=10),
        keras.callbacks.ReduceLROnPlateau(patience=5)
    ]
)


from dataclasses import dataclass

from common import load_lines

@dataclass
class Range:
    start: int
    end: int

    def fully_contains(self, other: "Range") -> bool:
        """"Checks if the 'other' Range is fully included within this Range."""
        return self.start <= other.start and self.end >= other.end

    def partially_overlaps(self, other: "Range") -> bool:
        """Checks if the 'other' Range is partially included within this Range. Caution: Not both ways!"""
        return self.start <= other.start <= self.end or self.start <= other.end <= self.end

@dataclass
class RangePair:
    first: Range
    second: Range

def to_range_pairs(lines: list[str]) -> list[RangePair]:
    range_pairs = []
    for line in lines:
        first, second = line.split(',')
        first_start, first_end = first.split('-')
        second_start, second_end = second.split('-')
        range_pairs.append(RangePair(Range(int(first_start), int(first_end)),
                                     Range(int(second_start), int(second_end))))
    return range_pairs

def find_number_of_fully_contained_pairs(range_pairs: list[RangePair]):
    """Task 1."""
    counter = 0
    for range_pair in range_pairs:
        if range_pair.first.fully_contains(range_pair.second) or range_pair.second.fully_contains(range_pair.first):
            counter += 1
    return counter

def find_number_of_partially_overlapping_pairs(range_pairs: list[RangePair]):
    """Task 2."""
    counter = 0
    for range_pair in range_pairs:
        if range_pair.first.partially_overlaps(range_pair.second) or range_pair.second.partially_overlaps(range_pair.first):
            counter += 1
    return counter

if __name__ == '__main__':
    input_lines = load_lines(day=4)

    # Task 1
    n_fully_contained_ranges = find_number_of_fully_contained_pairs(to_range_pairs(input_lines))
    print(f'Task 1: Found {n_fully_contained_ranges} fully contained ranges.')

    # Task 2
    n_partially_overlapping_ranges = find_number_of_partially_overlapping_pairs(to_range_pairs(input_lines))
    print(f'Task 1: Found {n_partially_overlapping_ranges} partially overlapping ranges.')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 31 21:57:53 2018

Tweet Publishing System for Sputnik v2 2.0

@author: pratheesh
"""

import tweepy
from twitterConf import api_key, api_secret_key, access_token, access_token_secret

def twitterPublish(message):
    try:
        auth = tweepy.OAuthHandler(api_key, api_secret_key) #Create OAuth handler instance
        auth.set_access_token(access_token, access_token_secret) #Comment if using 0Auth dance
        api = tweepy.API(auth) #Authenticate with the API
        response = api.update_status(message) #Will update the Twitter status
        return response._json
    except:
        print ('Error')


from collections import deque

def solution(k, dungeons):
    dp = deque()
    n = 0
    dp.append([k, []]) # 현재 남은 체력이 k고, 현재 [] 방문함

    while dp:
        p, v = dp.popleft() # 피로도, 던전
        for i in range(len(dungeons)):
            [a, b] = dungeons[i]

            if i not in v and p >= a and p - b >= 1:
                dp.append([p - b, v + [i]])
            else:
                n = max(n, len(v))
    return n


import numpy as np
from adbkit import ADBTools
from time import sleep
import random

def get_uiautomator_frame(screenshotfolder="c:\\ttscreenshots"):
    adb.aa_update_screenshot()
    return adb.aa_get_all_displayed_items_from_uiautomator(
        screenshotfolder=screenshotfolder,  # screenshots will be saved here
        max_variation_percent_x=10,
        # used for one of the click functions, to not click exactly in the center - more information below
        max_variation_percent_y=10,  # used for one of the click functions, to not click exactly in the center
        loung_touch_delay=(
            1000,
            1500,
        ),  # with this settings longtouch will take somewhere between 1 and 1,5 seconds
        swipe_variation_startx=10,  # swipe coordinate variations in percent
        swipe_variation_endx=10,
        swipe_variation_starty=10,
        swipe_variation_endy=10,
        sdcard="/storage/emulated/0/",
        # sdcard will be used if you use the sendevent methods, don't pass a symlink - more information below
        tmp_folder_on_sd_card="AUTOMAT",  # this folder will be created in the sdcard folder for using sendevent actions
        bluestacks_divider=32767,
        # coordinates must be recalculated for BlueStacks https://stackoverflow.com/a/73733261/15096247 when using sendevent
    )

def downswipe():
    adb.aa_swipe(random.randint(400, 600),
                 random.randint(1000, 1800),
                 random.randint(400, 600),
                 random.randint(400, 800),
                 random.uniform(0.4, 1.5))

def upswipe():
    adb.aa_swipe(random.randint(400, 600),
                 random.randint(400, 800),

                 random.randint(400, 600),
                 random.randint(1000, 1800),

                 random.uniform(0.4, 1.5))

ADBTools.aa_kill_all_running_adb_instances()
adb_path = r"C:\ProgramData\chocolatey\bin\adb.exe"
deviceserial = 'localhost:5555'
adb = ADBTools(adb_path=adb_path,
               deviceserial=deviceserial)

adb.aa_start_server()
sleep(3)
adb.aa_connect_to_device()
swipe = ([True] * 9) + [False]
like = ([False] * 3) + [True]
while True:
    downswipe() if random.choice(swipe) else upswipe()
    sleep(random.uniform(0.5, 4))
    if random.choice(like):
        while True:
            df = get_uiautomator_frame()
            if df.empty:
                continue
            dfm = df.loc[(df.bb_area > 1630) & (df.bb_area < 1690)]
            print(dfm)
            if not dfm.empty:
                dfm = dfm.drop_duplicates(subset='bb_bounds')
                dfm = dfm.loc[~dfm.bb_screenshot.apply(
                    lambda x: np.any(np.where((x[..., 2] == 255)
                                              & (x[..., 1] == 48) & (x[..., 0] == 64))))]
                if not dfm.empty:
                    dfm.ff_bb_tap_exact_center.iloc[0]()

            break


import os
import xml.etree.ElementTree as ET

def save_xml_text(file_name, user_text):
    # user inputs a string
    fileDir = os.path.dirname(os.path.realpath('__file__'))

    # os.path.join is used so that paths work in every operating system
    save_user_path = os.path.join(fileDir, '..', 'data', 'enwiki_txt')
    with open(os.path.join(save_user_path, str(file_name) + '.txt'), 'w+', encoding='utf-8') as f:
       f.write(user_text)

def etree_reader(path):
    """
    :param path: the path into amalgum dataset
    :param file: the file name in the folder: amalgum_genre_docxxx
    :return: an element tree object
    """
    xml_files = os.listdir(path)
    file_n = len(xml_files)
    print('{} files being processed'.format(file_n))
    count = 0
    for file in xml_files:
        if file.endswith(".xml"):
            sample_list = []
            tree = ET.parse(os.path.join(path, file))
            root = tree.getroot()
            for child in root:
                for child_a in child:
                    for i in child_a.itertext():
                        sample_list.append(i)
            count += 1

            new_list = ' '.join(sample_list)
            new_string = "".join(str(x) for x in new_list)
            print(new_string)
            save_xml_text(file_name=count, user_text=new_string)

path_parent = os.path.dirname(os.getcwd())
test_path = os.path.join(path_parent, 'data', 'enwiki_xml')

etree_reader(path=test_path)


DICTIONARY = {
	"言己": "記",
	"カ\"" : "ガ",
	"宇)" : "字)",
	"宇）": "字)",
	"幼安": "幼女",
	"培場": "劇場",
	"剖場": "劇場",
	"-" : "ー"
}

#指定した文字列を辞書に登録してあるパターンに補正する
def revision_from_dictionary(txt):
	result = txt.replace(" ", "")
	for i in DICTIONARY:
		result = result.replace(i, DICTIONARY[i])

	return result


g,s = map(int,input().split())
w = input()
S = input()

w_lst = [0] * 58
s_lst = [0] * 58

for char in w:
    w_lst[ord(char)- 65 ] += 1

st = 0
length = 0
cnt = 0
for i in range(s):
    s_lst[ord(S[i]) - 65] += 1
    length += 1

    if length == g:
        if s_lst == w_lst:
            cnt += 1

        length -= 1
        s_lst[ord(S[st]) - 65] -= 1
        st += 1

print(cnt)


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

from datetime import datetime

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors import LinkExtractor

from estate.items import EstateItem

class WangyiXmSpider(CrawlSpider):
    name = "163_xm"

    location = "xm"

    allowed_domains = [
        "fj.house.163.com",
    ]

    start_urls = (
        'http://fj.house.163.com/news/',
    )

    rules = (
        Rule(LinkExtractor(allow=('http://fj.house.163.com/special/027304TQ/[0-9a-zA-Z]+\.html',)), follow=True),
        Rule(LinkExtractor(allow=('http://fj.house.163.com/special/027304TQ/[0-9a-zA-Z]+_\d{2}\.html',)), follow=True),

        Rule(LinkExtractor(allow=('/15/(04|05|06)\d{2}/\d{2}/\w+\.html')), callback='parse_item'),
    )

    def parse_item(self, response):

        # from scrapy.shell import inspect_response
        # inspect_response(response)

        item_url = response.url

        if item_url.startswith('http://fj.house.163.com'):

            date_info = item_url.split('/')
            date_str = '20'+date_info[-4]+date_info[-3]

            item = EstateItem()

            item['url'] = item_url
            item['website'] = self.name
            item['location'] = self.location
            item['published_at'] = datetime.strptime(date_str, '%Y%m%d')
            item['html'] = response.body_as_unicode()

            yield item


# -*- coding: utf-8 -*-
"""
Created on Fri Jul  5 14:57:42 2019

@author: Lily\he\asamiko
"""
import pandas as pd
from sklearn import preprocessing
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

path = r'D:\TUT\Medical\biophysics\experiment'
#importing the dataset
dataset = pd.read_csv(f'{path}/Data_Cortex_Nuclear.csv')
# peeking at the dataset
dataset.head().T
#Descriptive stats of the variables in data
dataset.describe()

#analysis
col = range(1,78)
dataset1 = pd.read_csv(f'{path}/Data_Cortex_Nuclear.csv', usecols = col, header =1, skiprows = 1)

#standardize the data to normal distribution
dataset1_std = preprocessing.scale(dataset1)
dt1_df = pd.DataFrame(dataset1_std.astype(np.float16))
#replace blank cells with mean
dt1_df.fillna(np.mean(np.mean(dt1_df)), inplace=True)
dt1_df.dropna(how = 'all')
# find the appropriate cluster number
plt.figure()
wcss = []
for i in range(1, 31):
    kmeans = KMeans(n_clusters = i, init = 'k-means++', random_state = 42)
    kmeans.fit(dt1_df)
    wcss.append(kmeans.inertia_)
plt.plot(range(1, 31), wcss)
plt.title('The Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()
plt.savefig('best_cluster_number.pdf', dpi = 200)

# Fitting K-Means to the dataset
kmeans = KMeans(n_clusters = 4, init = 'k-means++', random_state = 42)
y_kmeans = kmeans.fit_predict(dt1_df)
#beginning of  the cluster numbering with 1 instead of 0
y_kmeans1=y_kmeans+1
# New Dataframe called cluster
cluster = pd.DataFrame(y_kmeans1)
# Adding cluster to the Dataset1
dt1_df['cluster_KNN'] = cluster

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x,y = np.meshgrid(dt1_df.iloc[:,0].astype('float16'), np.linspace(min(dt1_df.iloc[0,:].astype('float16')),max(dt1_df.iloc[0,:].astype('float16')),1078))
#Mean of clusters
#kmeans_mean_cluster = pd.DataFrame(round(dt1_df.groupby('cluster_KNN').mean(),1))
#kmeans_mean_cluster
z = pd.DataFrame(round(dt1_df.groupby(['cluster_KNN']).mean()))
for cluster, zlow, zhigh in [(1, min(z.iloc[0,:]),max(z.iloc[0,:])),(2, min(z.iloc[1,:]),max(z.iloc[1,:])),(3, min(z.iloc[2,:]),max(z.iloc[2,:])),(4, min(dt1_df.iloc[3,:]),max(dt1_df.iloc[3,:]))]:
    ax.scatter(x[0,:].astype('float16'), y[:,0],  cluster, cmap='prism')  # plot points with cluster dependent colors

#plt.title('Protein Data - KNN Clustering')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
plt.savefig('KNN Clustering Scatter_3D.pdf', dpi = 200)

# Hierarchical clustering for the same dataset
# creating a dataset for hierarchical clustering
#dt2_standardized = dataset1_standardized

from scipy.cluster.hierarchy import dendrogram, linkage
np.set_printoptions(precision=5, suppress=True)  # suppress scientific float notation
#creating the linkage matrix
H_cluster = linkage(dt1_df.iloc[:,0:77],'ward')
plt.title('Hierarchical Clustering Dendrogram (truncated)')
plt.xlabel('sample index or (cluster size)')
plt.ylabel('distance')
dendrogram(
    H_cluster,
    truncate_mode='lastp',  # show only the last p merged clusters
    p=4,  # show only the last p merged clusters
    leaf_rotation=90.,
    leaf_font_size=12.,
    show_contracted=True,  # to get a distribution impression in truncated branches
)

plt.show()
plt.savefig('Hierarchical Clustering.pdf', dpi = 200)
# Assigning the clusters and plotting the observations as per hierarchical clustering
from scipy.cluster.hierarchy import fcluster
k=4
cluster_2 = fcluster(H_cluster, k, criterion='maxclust')
cluster_2[0:70:,]
plt.figure()
plt.scatter(dt1_df.iloc[:,0], dt1_df.iloc[:,1],c=cluster_2, cmap='prism')  # plot points with cluster dependent colors
plt.title('Protein Data - Hierarchical Clutering')
plt.show()
plt.savefig('Hierarchical Clustering Scatter_1_2.pdf', dpi = 200)
# New Dataframe called cluster
cluster_Hierarchical = pd.DataFrame(cluster_2)
# Adding the hierarchical clustering to dataset
dt1_df['cluster_HC'] = cluster_Hierarchical

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x,y = np.meshgrid(dt1_df.iloc[:,0].astype('float16'), np.linspace(min(dt1_df.iloc[0,:].astype('float16')),max(dt1_df.iloc[0,:].astype('float16')),1078))
#Mean of clusters
#kmeans_mean_cluster = pd.DataFrame(round(dt1_df.groupby('cluster_KNN').mean(),1))
#kmeans_mean_cluster
z = pd.DataFrame(round(dt1_df.groupby(['cluster_HC']).mean()))
for cluster_2, zlow, zhigh in [(1, min(z.iloc[0,:]),max(z.iloc[0,:])),(2, min(z.iloc[1,:]),max(z.iloc[1,:])),(3, min(z.iloc[2,:]),max(z.iloc[2,:])),(4, min(dt1_df.iloc[3,:]),max(dt1_df.iloc[3,:]))]:
    ax.scatter(x[0,:].astype('float16'), y[:,0],  cluster_2, cmap='prism')  # plot points with cluster dependent colors

#plt.title('Protein Data - KNN Clustering')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
plt.savefig('Hierarchical Clustering Scatter_3D.pdf', dpi = 200)


from tkinter import *
def calculate():
   value_label['text'] =  str(round(float(input_entry.get()) * 1.609,2))

#Create elements
window = Tk()
window.title("Mile to Km Converter")
window.minsize()
window.config(padx=20, pady=20)

miles_label = Label(text="Miles")
input_entry = Entry(width=10)
equals_label = Label(text="is equal to")
value_label = Label(text="0")
km_label = Label(text="Km")
button = Button(text="Calculate", command=calculate)

#positions
input_entry.grid(column=2, row=0)
miles_label.grid(column=3, row=0)

equals_label.grid(column=1, row=1)
value_label.grid(column=2, row=1)
km_label.grid(column=3, row=1)

button.grid(column=2, row=2)

window.mainloop()


import argparse
import pathlib
from typing import Type

from envs.monkey_zoo.blackbox.config_templates.config_template import ConfigTemplate
from envs.monkey_zoo.blackbox.config_templates.grouped.depth_1_a import Depth1A
from envs.monkey_zoo.blackbox.config_templates.grouped.depth_2_a import Depth2A
from envs.monkey_zoo.blackbox.config_templates.grouped.depth_3_a import Depth3A
from envs.monkey_zoo.blackbox.config_templates.single_tests.powershell_credentials_reuse import (
    PowerShellCredentialsReuse,
)
from envs.monkey_zoo.blackbox.config_templates.single_tests.smb_pth import SmbPth
from envs.monkey_zoo.blackbox.config_templates.single_tests.wmi_mimikatz import WmiMimikatz
from envs.monkey_zoo.blackbox.config_templates.single_tests.zerologon import Zerologon
from envs.monkey_zoo.blackbox.island_client.island_config_parser import IslandConfigParser
from envs.monkey_zoo.blackbox.island_client.monkey_island_client import MonkeyIslandClient

DST_DIR_NAME = "generated_configs"
DST_DIR_PATH = pathlib.Path(pathlib.Path(__file__).parent.absolute(), DST_DIR_NAME)

parser = argparse.ArgumentParser(description="Generate config files.")
parser.add_argument(
    "island_ip",
    metavar="IP:PORT",
    help="Island endpoint. Example: 123.123.123.123:5000",
)

args = parser.parse_args()
island_client = MonkeyIslandClient(args.island_ip)

CONFIG_TEMPLATES = [
    Depth1A,
    Depth2A,
    Depth3A,
    Zerologon,
    SmbPth,
    WmiMimikatz,
    PowerShellCredentialsReuse,
]

def generate_templates():
    for template in CONFIG_TEMPLATES:
        save_template_as_config(template)

def save_template_as_config(template: Type[ConfigTemplate]):
    file_path = pathlib.Path(DST_DIR_PATH, f"{template.__name__}.conf")
    file_contents = IslandConfigParser.get_raw_config(template, island_client)
    save_to_file(file_path, file_contents)

def save_to_file(file_path, contents):
    with open(file_path, "w") as file:
        file.write(contents)

if __name__ == "__main__":
    generate_templates()


from websites.factories import WebsiteContentFactory, WebsiteFactory
from websites.management.commands.markdown_cleaning.cleaner import (
    WebsiteContentMarkdownCleaner,
)
from websites.management.commands.markdown_cleaning.rules.metadata_relative_urls import (
    MetadataRelativeUrlsRule,
)
from websites.management.commands.markdown_cleaning.testing_utils import (
    patch_website_contents_all,
)

def get_markdown_cleaner(website_contents):
    """Convenience to get rule-specific cleaner"""  # noqa: D401
    with patch_website_contents_all(website_contents):
        rule = MetadataRelativeUrlsRule()
        return WebsiteContentMarkdownCleaner(rule)

def test_updates_multiple_metadata_fields():
    """
    Check that a single call to update_website_content modifies multiple fields
    for rules that have multiple fields associated.
    """
    assert len(MetadataRelativeUrlsRule.fields) > 1

    website = WebsiteFactory.build(name="site-1")
    wc1 = WebsiteContentFactory.build(
        filename="thing1",
        dirpath="content/resources",
        website=website,
    )
    wc2 = WebsiteContentFactory.build(
        filename="thing2", dirpath="content/pages/two", website=website
    )

    content_to_clean = WebsiteContentFactory.build(
        metadata={
            "related_resources_text": """Hello
                Change this: [to thing1](resources/thing1#fragment "And a title!") cool'

                Leave this alone: [wiki](https://wikipedia.org) same

                And this [course link](/courses/8-02/pages/jigawatts)
            """,
            "image_metadata": {"caption": "And now [thing2](pages/two/thing2)"},
        },
        website=website,
    )

    cleaner = get_markdown_cleaner([wc1, wc2])
    cleaner.update_website_content(content_to_clean)

    expected_related_resources = """Hello
                Change this: [to thing1](/courses/site-1/resources/thing1#fragment) cool'

                Leave this alone: [wiki](https://wikipedia.org) same

                And this [course link](/courses/8-02/pages/jigawatts)
            """
    expected_caption = "And now [thing2](/courses/site-1/pages/two/thing2)"
    assert (
        content_to_clean.metadata["related_resources_text"]
        == expected_related_resources
    )
    assert content_to_clean.metadata["image_metadata"]["caption"] == expected_caption


import folium
from folium import plugins

graph_data = open('androidbaru.csv', 'r').readlines()
#graph_data = graph_data.replace('"', '')
lines = graph_data[1:]  # Skip header line
lines = [elem.replace('"', '') for elem in lines]

Long = []
Lat = []
Alt = []

for line in lines:
    # print(graph_data)
    if len(line) > 1:
        # _, _, _, _, _, _, _, _, _, _, long, lat, alt, _, _ = line.split(',')
        _, _, _, _, _, _, _, _, _, lat, long, alt, = line.split(',')
        #  ID.append(float(id))
        Long.append(float(long))
        Lat.append(float(lat))
        Alt.append(float(alt))

# Create a base map centered at a specific location
map_center = [-6.89729,107.58]
mymap = folium.Map(location=map_center, zoom_start=17)

# Sample GPS data
gps_data = []
for i in range(len(Lat)):
    print(Lat[i])
    print(Long[i])
    gps_point = {
        'latitude': Lat[i],
        'longitude': Long[i],
        'altitude': Alt[i]
        # 'altitude': 680
    }
    gps_data.append(gps_point)

# Add markers for each GPS data point
# for data_point in gps_data:
#     folium.Marker(
#         location=[data_point['latitude'], data_point['longitude']],
#         popup=f"Altitude: {data_point['altitude']}",
#         icon=folium.Icon(color='blue')
#     ).add_to(mymap)

# Add lines connecting consecutive points
for i in range(len(gps_data) - 1):
    line_points = [(gps_data[i]['latitude'], gps_data[i]['longitude']),
                   (gps_data[i+1]['latitude'], gps_data[i+1]['longitude'])]
    folium.PolyLine(locations=line_points, color='red').add_to(mymap)

# Display the map
mymap.save("gpstrackers.html")


import argparse
import csv
import time
import os
import numpy as np
import pandas as pd
import xgboost as xgb
import joblib as jl
import sklearn.metrics as sklm
from support.regressor_support import read_csv_dataset

def prepare_kwargs_for_regressor(args):
    if args.xgb_params != None:
        xgb_kwargs = eval('{' + args.xgb_params + '}')
    else:
        xgb_kwargs = {}
    return xgb_kwargs

def save_dumps(results):
    if args.dumpout_path is not None:
        if not os.path.exists(args.dumpout_path):
            os.makedirs(args.dumpout_path)
        for metric in args.val_metrics:
            dump_fn = os.path.join(args.dumpout_path, 'metric_' + metric + '.csv')
            np.savetxt(dump_fn, results['validation_0'][metric], delimiter=',')
            print("Generated dump file '%s'" % dump_fn)
            if (len(results.keys()) > 1):
                dump_fn = os.path.join(args.dumpout_path, 'val_' + metric + '.csv')
                np.savetxt(dump_fn, results['validation_1'][metric], delimiter=',')
                print("Generated dump file '%s'" % dump_fn)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='%(prog)s fits a multiple-input single-output (scalar) function dataset using a configurable XGBoost')

    parser.add_argument('--version', action='version', version='%(prog)s 1.0.0')

    parser.add_argument('--trainds',
                        type=str,
                        dest='train_dataset_filename',
                        required=True,
                        help='Train dataset file (csv format)')

    parser.add_argument('--modelout',
                        type=str,
                        dest='model_file',
                        required=True,
                        help='Output model file')

    parser.add_argument('--valds',
                        type=str,
                        dest='val_dataset_filename',
                        required=False,
                        help='Validation dataset file (csv format)')

    parser.add_argument('--metrics',
                        type=str,
                        dest='val_metrics',
                        required=False,
                        nargs = '+',
                        default = [],
                        help='List of built-in evaluation metrics to apply to validation dataset')

    parser.add_argument('--dumpout',
                        type=str,
                        dest='dumpout_path',
                        required=False,
                        help='Dump directory (directory to store metric values)')

    parser.add_argument('--earlystop',
                        type=int,
                        dest='early_stopping_rounds',
                        required=False,
                        help='Number of round for early stopping')

    parser.add_argument('--xgbparams',
                        type=str,
                        dest='xgb_params',
                        required=False,
                        help='Parameters of XGBoost constructor')

    args = parser.parse_args()

    print("#### Started %s ####" % os.path.basename(__file__));

    head_train, df_independent_train, df_dependent_train = read_csv_dataset(args.train_dataset_filename, 1)
    eval_set = [(df_independent_train, df_dependent_train)]

    if args.val_dataset_filename is not None:
        head_val, df_independent_val, df_dependent_val = read_csv_dataset(args.val_dataset_filename, 1)
        eval_set.append((df_independent_val, df_dependent_val))

    xgb_kwargs = prepare_kwargs_for_regressor(args)
    model = xgb.XGBRegressor(**xgb_kwargs)

    start_time = time.time()
    model.fit(
        df_independent_train,
        df_dependent_train,
        eval_set=eval_set,
        eval_metric=args.val_metrics,
        early_stopping_rounds=args.early_stopping_rounds)
    elapsed_time = time.time() - start_time
    print ("Training time:", time.strftime("%H:%M:%S", time.gmtime(elapsed_time)))

    results = model.evals_result()

    jl.dump(model, args.model_file)
    print("Generated one-variable function xgboost model '%s'" % args.model_file)

    save_dumps(results)

    print("#### Terminated %s ####" % os.path.basename(__file__));


import sys

sys.stdin = open("./input/3.txt" , 'rt')
a=list(range(21))

for _ in range(10):
    s, e = map(int , input().split())
    print(s , e)
    for i in range((e-s+1) // 2):
        a[s+i], a[e-i]=a[e-i] , a[s+i]

a.pop(0)
print(a)


import os

from Source.Application.Core.RNNNameClassifier import RNNNameClassifier
from Source.Application.Enumerator.StepTypes import StepTypes
from Source.Application.Service.DataTransformService import DataTransformService
from Source.Application.Service.FileTraitService import FileTraitService

class RNNTrainingService():

    def __init__(self):
        self.file_trait = FileTraitService()
        self.data_transform = DataTransformService()
        self.network = RNNNameClassifier(input_size=len(self.data_transform.dictionary),
                                         hidden_size=256,
                                         output_size=18)

    def execute(self):
        loss_epochs = list()
        accuracies = list()
        directory_path = os.path.join(os.getcwd(), "..\\..\\..\\Assets\\Data\\Names")

        names, categories = self.file_trait.read_directory(directory_path)

        features = self.data_transform.names_to_tensor(names)
        labels = self.data_transform.categories_to_tensor(categories)

        loss_epochs, accuracies = self.network.do_training(features=features,
                                                           labels=labels,
                                                           step_type=StepTypes.TRAINING,
                                                           epochs=100)

service = RNNTrainingService()
""
print(service.execute())


#!/usr/bin/python3
import grpc
import os
import sys
import time
asic = os.environ.get('ASIC', 'capri')
paths = [
    '/nic/build/x86_64/iris/' + asic + '/gen/proto/',
    '/nic/cli/', # To get tenjin_wrapper.
    '/mbt/'
]

if 'WS_TOP' in os.environ:
    ws_top = os.environ['WS_TOP']
else:
    ws_top = os.path.dirname(sys.argv[0]) + '/..'
    ws_top = os.path.abspath(ws_top)
    os.environ['WS_TOP'] = ws_top

for path in paths:
    fullpath = ws_top + path
    print("Adding Path: %s" % fullpath)
    sys.path.insert(0, fullpath)

from infra.common.logging import logger
from tenjin import *
from tenjin_wrapper import *

def genProxyServerMethods(proxy_handler, template, out_file, ws_dir=None):
    tenjin_prefix = "//::"

    dic = {
            "proxy_handler" : proxy_handler,
            "ws_top" : ws_dir
          }
    with open(out_file, "w") as of:
        render_template(of, template, dic, './', prefix=tenjin_prefix)
        of.close()

def initClient():
    HalChannel = grpc.insecure_channel('localhost:%s'%(port))
    grpc.channel_ready_future(HalChannel).result()
    print( "Connected to HAL" )

def serve(server):
    if 'MBT_GRPC_PORT' in os.environ:
        port = os.environ['MBT_GRPC_PORT']
    else:
        port = '50051'
    logger.info("Starting Model Based Tester GRPC Server on port %s" %(port))
    server.add_insecure_port('[::]:%s' %(port))
    server.start()
    try:
        while True:
            time.sleep(1000000)
    except KeyboardInterrupt:
        server.stop(0)

if __name__ == '__main__':
    serve()


#!/usr/bin/env python

import os, sys
import time
import numpy as np  # "pip install numpy" installs numpy
import random
import math
import csv
from numpy.linalg import inv

from envs.ea import de_R1, de_R2, de_R3, de_validate

import gym
from gym import spaces
from gym.utils import seeding

from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
from keras.optimizers import Adam

from callbacks import Callback
from rl.agents.dqn import DQNAgent

from rl.memory import SequentialMemory
from rl.util import *

import argparse
from inspect import currentframe, getframeinfo
from pathlib import Path

class ModelCheckpoint(Callback):
    def __init__(self, filepath, interval, verbose=1):
        super(ModelCheckpoint, self).__init__()
        self.filepath = filepath
        self.interval = interval
        self.verbose = verbose
        self.total_episodes = 0
        self.best_reward = -np.inf
        self.total_reward = 0

    def on_episode_end(self, episode, logs):
        """ Save weights at interval steps during training if
        reward improves """
        self.total_episodes += 1
        self.total_reward += logs['episode_reward']
        if self.total_episodes % self.interval != 0:
            # Nothing to do.
            return

        if self.total_reward > self.best_reward:
            if self.verbose > 0:
                print('Episode {}: Reward improved '
                        'from {} to {}'.format(self.total_episodes,
                        self.best_reward, self.total_reward))
            self.model.save_weights(self.filepath, overwrite=True)
            self.best_reward = self.total_reward
            self.total_reward = 0

class PolicyDebug(object):
    """Abstract base class for all implemented policies.

        Each policy helps with selection of action to take on an environment.

        Do not use this abstract base class directly but instead use one of the concrete policies implemented.
        To implement your own policy, you have to implement the following methods:

        - `select_action`

        # Arguments
        agent (rl.core.Agent): Agent used
    """
    def _set_agent(self, agent):
        self.agent = agent

    @property
    def metrics_names(self):
        return []

    @property
    def metrics(self):
        return []

    def select_action(self, **kwargs):
        raise NotImplementedError()

    def get_config(self):
        """Return configuration of the policy

            # Returns
            Configuration as dict
        """
        return {}

class BoltzmannQPolicy(PolicyDebug):
    """Implement the Boltzmann Q Policy

        Boltzmann Q Policy builds a probability law on q values and returns
        an action selected randomly according to this law.
    """
    def __init__(self, tau=1., clip=(-500., 500.)):
        super(BoltzmannQPolicy, self).__init__()
        self.tau = tau
        self.clip = clip

    def select_action(self, q_values):
        """Return the selected action

            # Arguments
            q_values (np.ndarray): List of the estimations of Q for each action

            # Returns
            Selection action
        """
        assert q_values.ndim == 1
        q_values = q_values.astype('float64')
        nb_actions = q_values.shape[0]

        exp_values = np.exp(np.clip(q_values / self.tau, self.clip[0], self.clip[1]))
        probs = exp_values / np.sum(exp_values)
        action = np.random.choice(range(nb_actions), p=probs)
        return action

    def get_config(self):
        """Return configurations of BoltzmannQPolicy

            # Returns
            Dict of config
        """
        config = super(BoltzmannQPolicy, self).get_config()
        config['tau'] = self.tau
        config['clip'] = self.clip
        return config

class EpsGreedyQPolicy(PolicyDebug):
    """Implement the epsilon greedy policy

        Eps Greedy policy either:

        - takes a random action with probability epsilon
        - takes current best action with prob (1 - epsilon)
        """
    def __init__(self, eps=.1):
        super(EpsGreedyQPolicy, self).__init__()
        self.eps = eps

    def select_action(self, q_values):
        """Return the selected action

            # Arguments
            q_values (np.ndarray): List of the estimations of Q for each action

            # Returns
            Selection action
            """
        assert q_values.ndim == 1
        nb_actions = q_values.shape[0]

        if np.random.uniform() < self.eps:
            action = np.random.random_integers(0, nb_actions-1)
        else:
            action = np.argmax(q_values)
        return action

    def get_config(self):
        """Return configurations of EpsGreedyQPolicy

            # Returns
            Dict of config
            """
        config = super(EpsGreedyQPolicy, self).get_config()
        config['eps'] = self.eps
        return config

                    ############################################### Parameters ##############################################################

parser = argparse.ArgumentParser(description = "de and ddqn parameters")

parser.add_argument('--trainingInstance_file', help='File with training instances')
parser.add_argument('--weight_file', help='File with NN weights')
parser.add_argument('--instance', type=int, help='Validation instance selected')
parser.add_argument('--FF', type=float, default=0.5,  help='scaling factor for DE')
parser.add_argument('--NP', type=int, default=100,  help='population size for DE')
parser.add_argument('--CR', type=float, default=0.5,  help='crossover rate for DE')
parser.add_argument('--FE', type=int, default=100,  help='function evaluations')
parser.add_argument('--max_gen', type=int, default=10,  help='maximum nuber of genertion')
parser.add_argument('--W', type=int, default=50,  help='window size')
parser.add_argument('--unit', type=int, default=100,  help='number of nodes in each hidden layer')
parser.add_argument('--batchsize', type=int, default=4,  help='batch size of neural network')
parser.add_argument('--gamma', type=float, default=0.99,  help='discount factor')
parser.add_argument('--C', type=int, default=1000,  help='target network synchronised')
parser.add_argument('--limit', type=int, default=100000,  help='memory size')
parser.add_argument('--warmup', type=int, default=10000,  help='number of evaluations used for warm up')
parser.add_argument('--LR', type=float, default=0.0001,  help='Adam learning rate')
parser.add_argument('--training_steps', type=int, default=100000000,  help='Steps required for each training')

args = parser.parse_args()

training_set = args.trainingInstance_file
weight_file = args.weight_file
instance = args.instance
FF = args.FF
NP = args.NP
CR = args.CR
FE = args.FE
max_gen = args.max_gen
W = args.W
unit = args.unit
batchsize = args.batchsize
gamma = args.gamma
C = args.C
limit = args.limit
warmup = args.warmup
LR = args.LR
training_steps = args.training_steps

FE = 1e4
#training_steps = 6048e5
training_steps = 32e6

ENV_NAME = 'ea'

def get_path_to_script():
    # MANUEL: This should probably go into a function get_path_to_script()
    filename = getframeinfo(currentframe()).filename
    parent = Path(filename).resolve().parent
    return parent

                            ################################################# Training phase ##############################################################

parent = get_path_to_script()
# MANUEL: This should be a parameter --training-set, not hard-coded here.
training_set_path = os.path.join(parent, training_set)

func_choice = []
with open(training_set_path, 'r') as f:
    for item in f:
        func_choice.append(float(item.rstrip()))
env_train = de_R2.DEEnv(func_choice, FF, NP, CR, FE, max_gen, W) # Can be changed to create an object of de-R1 or de-R3 for reward defintions R1 and R3 resp.

nb_actions = env_train.action_space.n

# Build a sequential model.
model = Sequential()
model.add(Flatten(input_shape=(1,) + env_train.observation_space.shape))
model.add(Dense(unit, activation = 'relu'))
model.add(Dense(unit, activation = 'relu'))
model.add(Dense(unit, activation = 'relu'))
model.add(Dense(unit, activation = 'relu'))
model.add(Dense(nb_actions, activation = 'linear'))
print("Model Summary: ",model.summary())

memory = SequentialMemory(limit=limit, window_length=1)

# Epsilon-Greedy Policy
policy = EpsGreedyQPolicy()

# DQN Agent: Finally, we configure and compile our agent. You can use every built-in Keras optimizer and even the metrics!
dqn = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, nb_steps_warmup=warmup, target_model_update=C, policy=policy, enable_double_dqn=True, batch_size=batchsize, gamma=gamma) # nb_steps_warmup >= nb_steps 2000
# DQN stores the experience in the memory buffer for the first nb_steps_warmup. This is done to get the required size of batch during experience replay.
# When number of steps exceeds nb_steps_warmup then the neural network would learn and update the weight.

# Neural Compilation
dqn.compile(Adam(lr=LR), metrics=['mae'])

callbacks = [ModelCheckpoint(weight_file, 32)]

# Fit the model: training for nb_steps = number of generations
dqn.fit(env_train, callbacks = callbacks, nb_steps=training_steps, visualize=False, verbose=0, nb_max_episode_steps = None)

                            ##################################################### Validation phase ##############################################################

env_validate = de_validate.DEEnv(instance, FF, NP, CR, FE, max_gen, W) # Can be changed to create an object of de-R1 or de-R3 for reward defintions R1 and R3 resp.

nb_actions = env_validate.action_space.n

# Build a sequential model.
model = Sequential()
model.add(Flatten(input_shape=(1,) + env_validate.observation_space.shape))
model.add(Dense(unit, activation = 'relu'))
model.add(Dense(unit, activation = 'relu'))
model.add(Dense(unit, activation = 'relu'))
model.add(Dense(unit, activation = 'relu'))
model.add(Dense(nb_actions, activation = 'linear'))

dqn.load_weights(weight_file)

dqn.test(env_validate, nb_episodes=1, visualize = False)
print(env_validate.best_so_far)


import re

# Task 1
def pattern_match(text):
    match = re.fullmatch(r'\w*', text)
    if match:
        print('Text matches the pattern!')
    else:
        print("Text contains smth except a-zA-Z0-9 and underscore. Please try again")
    return match

input_text = input("Введіть текст для перевірки патерну a-zA-Z0-9_: ")
print(pattern_match(input_text))

# Task 2
def remove_brackets(text):
    res = re.sub(r'\([^()]*\)', '', text)
    return res

# Task 3
def whitespace_insert(text):
    res = re.sub(r'([A-Z])', r' \1', text)
    return res

text_1 = 'GoodMorningAlex'
print(whitespace_insert(text_1))


def clearcloud(self,Dataset0,Dataset1):
    '''
    Clear the clouds
    :param Dataset0 : The image that want to clear cloud.
    :param Dataset1 : The better image that used to maske the first image better.
    :return         : better cloud image
    '''
    self.Dataset0 = Dataset0
    self.Dataset1 = Dataset1
    pixel0 = self.Dataset0.pixel_qa
    mask1 = xarray.where(pixel0==352,1,0)
    mask2 = xarray.where(pixel0==480,1,0)
    mask3 = xarray.where(pixel0==944,1,0)
    sum = mask1+mask2+mask3
    mask0 = xarray.where(sum.data>0,1,0)
    blue        = xarray.where(mask0,self.Dataset1.blue,self.Dataset0.blue)
    green       = xarray.where(mask0,self.Dataset1.green,self.Dataset0.green)
    red         = xarray.where(mask0,self.Dataset1.red,self.Dataset0.red)
    nir         = xarray.where(mask0,self.Dataset1.nir,self.Dataset0.nir)
    pixel_qa    = xarray.where(mask0,self.Dataset1.pixel_qa,self.Dataset0.pixel_qa)
    # Create DataArray
    return xarray.merge([blue,green,red,nir,pixel_qa])

def percentcloud(self,Dataset):
    '''
    :param Dataset : Xarray dataset pixel_qa bands
    :return        : percent of the cloud in the image
    '''
    self.Dataset = Dataset
    FashCloud = [352,480,944]
    dstest    = self.Dataset.pixel_qa
    dsnew     = xarray.where(dstest == FashCloud[0],numpy.nan,dstest)
    dsnew     = xarray.where(dsnew  == FashCloud[1],numpy.nan,dsnew)
    dsnew     = xarray.where(dsnew  == FashCloud[2],numpy.nan,dsnew)
    Cpixel    = (numpy.isnan(dsnew.to_numpy())).sum()
    Allpixel  = int(self.Dataset.pixel_qa.count())
    Cloudpercent = (Cpixel/Allpixel)*100
    print("Percent Cloud : %.4f"%Cloudpercent,"%")


#!/usr/bin/env python3
# coding : utf-8
import requests
from bs4 import BeautifulSoup as bs
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
import csv
import os
import json

__doc__ = '''
Extraction des propositions issues du site https://le-vrai-debat.fr/
* 9 thèmes
La première extraction consiste à lister toutes les urls des propositions par page
La deuxième consiste à extraire le texte de chacune des propositions
'''

# Extraction des données issues du vrai débat 9 thèmes
ROOT_URL = "https://le-vrai-debat.fr"

URLS = [
    "https://le-vrai-debat.fr/project/democratie-institutions-referendum-dinitiative-citoyenne/consultation/consultation/types/democratie-institutions-referendum-dinitiative-citoyenne",
    "https://le-vrai-debat.fr/project/transition-ecologique-solidaire-agriculture-alimentation/consultation/consultation-2/types/transition-ecologique-et-solidaire-agriculture-alimentation-transport",
    "https://le-vrai-debat.fr/project/justice-police-armee/consultation/consultation-3/types/justice-police-armee",
    "https://le-vrai-debat.fr/project/europe-affaires-etrangeres-outre-mer/consultation/consultation-4/types/europe-affaires-etrangeres-outre-mer",
    "https://le-vrai-debat.fr/project/sante-solidarite-handicap/consultation/consultation-5/types/sante-solidarite-handicap",
    "https://le-vrai-debat.fr/project/economie-finances-travail-compte-public/consultation/consultation-6/types/economie-finances-travail-compte-public",
    "https://le-vrai-debat.fr/project/education-jeunesse-enseignement-superieur-recherche-et-innovation/consultation/consultation-7/types/education-jeunesse-enseignement-superieur-recherche-et-innovation",
    "https://le-vrai-debat.fr/project/sport-culture/consultation/consultation-8/types/sport-culture",
    "https://le-vrai-debat.fr/project/expression-libre/consultation/consultation-9/types/expression-libre"
]

def get_theme_info(url):
    '''A partir d'une url de consultation récupérer les informations statistiques du thème correspondant'''
    headers = {
        "Host":"le-vrai-debat.fr",
        "Referer": "https://le-vrai-debat.fr/",
        "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linu…) Gecko/20100101 Firefox/65.0"}
    r = requests.get(url, headers)
    print(r)
    slug = url.split(
        "/consultation/")[0].replace("https://le-vrai-debat.fr/project/", "")
    soup = bs(r.text, "lxml")
    name = soup.h1.text.strip()
    info = soup.find("span", {"class":"excerpt"})
    author = {"name": info.a.text, "url":info.a.get("href")}
    contributions = int(soup.find(
        "li", id="contributions-counter-pill").find("span", {"class": "value"}).text)
    votes = soup.find(
        "li", id="votes-counter-pill").find("span", {"class": "value"}).text
    contributors = soup.find(
        "li", id="contributors-counter-pill").find("span", {"class": "value"}).text
    page_offset = contributions//10
    if contributions % 10 != 0:
        page_offset += 1

    return {
        slug : {
        "name": name,
        "slug": slug,
        "url": url,
        "author": author,
        "date": info.text.split(",")[1].strip(),
        "nb_contributions": int(contributions),
        "nb_votes": int(votes),
        "nb_participants": int(contributors),
        "page_nb": page_offset
    }}

def build_parameters():
    data = {}
    for url in URLS:
        data.update(get_theme_info(url))
    with open("parameters.json", "w") as f:
        json.dump(data, f)

def load_parameters():
    with open("parameters.json", "r") as f:
        data = json.load(f)

    return data

def get_proposal_url(soup):
    '''Recupérer seulement l'url de la proposition'''
    return [ROOT_URL+soup.h3.a.get("href")]

def get_proposal_ref(soup):
    '''Etant donné un élément html d'une liste extraire les références de la proposition'''
    author = {"name": soup.p.a.text, "url": soup.p.a.get("href")}
    created_date = soup.p.text.split(" • ")[1].strip()
    stats = dict(zip(["votes", "amendements", "arguments", "sources"], [
                 n.strip for n in soup.find("p", {"class": "opinion__votes"}).text.split(" • ")]))
    proposal = {"author": author, "url": soup.h3.a.get(
        "href"), "title": soup.h3.a.text.strip(), "date": created_date, "stats": stats}
    return proposal

def extract_by_page(url, page_nb):
    # try:
    options = webdriver.FirefoxOptions()
    options.binary_location = '/usr/bin/firefox'
    options.add_argument('-headless')
    options.add_argument('window-size=1920x1080')
    driver = webdriver.Firefox(options=options)
    page_url = url+"/page/{}".format(page_nb)
    driver.get(page_url)
    source = driver.page_source
    soup = bs(source, "lxml")
    driver.close()
    data = []
    for i in soup.findAll("div", {"class": "opinion__body"}):
        data.append(get_proposal_url(i))
    return map(get_proposal_url, soup.findAll("div", {"class": "opinion__body"}))
    # return data

def extract_content_proposal(soup):
    author_block = soup.find("div", {"class": "opinion__user"})
    title = soup.find("h3").text
    stats_block = [n.text.strip() for n in soup.find("ul").findAll("span")]
    opinion_description = "\n---\n".join([n.text.strip()
                                          for n in soup.findAll("div", {"class": 'opinion__description'})])
    # opinion_block = soup.find("div", {"class":"opinion__appendix__content"}).text.strip()
    votes = [n.text for n in soup.find("table").findAll("td")]
    actions_block = soup.find("ul", {"role": "tablist"})
    arguments_pour_block = soup.find("div", id="arguments-col--FOR")
    arguments_contre_block = soup.find("div", id="arguments-col--AGAINST")
    amendements_block = soup.find("div", id="versions-list")
    sources_block = soup.find("div", id="sources-list")

def collect_proposal(parameters, theme_nb = None):
    # options = webdriver.ChromeOptions()
    # options.binary_location = '/snap/bin/chromium/'
    #
    #  driver = webdriver.Chrome(options=options)
    if theme_nb is None:
        for slug, theme in parameters.items():
            with open(os.path.join("proposal_urls", slug+".csv"), "w") as f:
                w = csv.writer(f)
                for page_nb in range(1, theme["page_nb"]+1):
                    w.writerows(extract_by_page(theme["url"], page_nb))
    else:
        count = 0
        for slug, theme in parameters.items():
            count = count+1
            if count == theme_nb:
                with open(os.path.join("proposal_urls", slug+".csv"), "w") as f:
                    w = csv.writer(f)
                    for page_nb in range(1, theme["page_nb"]+1):
                        print(extract_by_page(theme["url"], page_nb))
                        w.writerows(extract_by_page(theme["url"], page_nb))
    # driver.close()
    return

def verify_proposal_count(parameters):
    with open("propasals-scrapping-stats.csv", "w") as f:
        header = ["THEME", "CONTRIBUTIONS_NB", "RESULTS_NB", "MISSING", "PAGE_NB_TO_START"]
        f.write("\t".join(header)+'\n')
        for theme_name, theme in parameters.items():
            try:
                with open(os.path.join("proposal_urls", theme_name+".csv"), "r") as fi:
                    result_nb = len(fi.readlines())
                row = [theme_name, str(theme["nb_contributions"]), str(result_nb), str(theme["nb_contributions"]-result_nb), str((result_nb/10)+1)]
                f.write("\t".join(row)+'\n')
            except FileNotFoundError:
                row = [theme_name, str(theme["nb_contributions"]), "0", str(theme["nb_contributions"]), "1"]
                f.write("\t".join(row)+'\n')

if __name__ == "__main__":
    build_parameters()
    parameters = load_parameters()
    # verify_proposal_count(parameters)
    collect_proposal(parameters, 2)


try:
    print("Resource open")
    a = int(input("Enter a number to divide:"))
    b = int(input("Enter 2nd number:"))
    c = a/b
    print(c)

except (ZeroDivisionError, ValueError):
    print("Invalid Input, can't divide")

else:
    print("Resource closed")


"""
This program implements a program that handles
the general case of drawing olygons of decreasing
sides,recursively.

The picture can be best seen at sides 6 and 7.
while entering input of 8 as side, the output will take
a bit of more time to generate.
"""

import turtle as t
import time
import math
import sys

def init():
    """
    Initialize for drawing.  (-1500, -1500) is in the lower left and
    (1500, 1500) is in the upper right.
    :pre: pos (0,0), heading (east), up
    :post: pos (0,0), heading (east), up
    :return: None
    """
    t.setworldcoordinates(-1500, -1500, 1500, 1500)
    t.speed(0)
    t.tracer(0, 0)
    t.title('Polygons')
    t.bgcolor("yellow")

def DrawPolygon(sides, length):
    """
     Draws polygon based on the sides and lenth.
     :pre: (relative) pos (0,0), heading (east), up
     :post: (relative) pos (0,0), heading (east), up
     :return: None

     It is an helper function to draw a single polygon
     based on no of sides passed.
     """

    for i in range(sides):
        if ((sides - 1) % 2 == 0):
            t.down()
            t.forward(length)
            t.left(360 / sides)
        else:
            t.forward(length)
            t.right(360 / sides)
    t.up()

def DrawPolygon_rec2(sides,states, length):

    """
     Draws polygon recursively based on the sides and lenth.
     :pre: (relative) pos (0,0), heading (east), up
     :post: (relative) pos (0,0), heading (east), up
     :return: sum - length of all sides

     if sides is less than 0, it returns 0 or else proceed
     with drawing of polygon and returning sum.
     """
    sum = 0
    if(sides<=2):
        return 0
    else:
        """
        Checks if state entered is fill,
        if true it will fill the drawn polygon
        else only polygon will be drawn without
        any filling

        it calls helper function DrawPolygon
        """
        if(states=='fill'):
            t.begin_fill()
        DrawPolygon(sides, length)
        t.end_fill()

        """
        for no of sides entered, each time,
        the function will perform a set of statements
        and will call itself recursively with
        sides redcued by 1 and length decreased by
        half of its value
        """

        for i in range (sides):
            t.down()

            """
            if sides entered is an even no, then it will draw
            the polygon above the respective x - axis that also means
            while calling it recursively it will draw the polygon
            insided the previous polygon
            """

            if((sides-1)%2==0):
                t.color("blue","red")
                if (states == 'fill'):
                    t.begin_fill()
                    t.end_fill()
                t.forward(length)
                t.left(360/sides)
                sum=sum+length

            else:

                """
                if the sides is an odd number, it will
                be drawn below the respective x-axis that is also
                outside of the previously drawn polygon.
                """

                t.color("blue","blue")
                if (states == 'fill'):
                    t.begin_fill()
                t.forward(length)
                t.right(360 / (sides))

                """
                Each time a polygon is drawn its length is added in sum
                each time a side of the same polygon is drawn
                hence in total it stores the combined length
                obtained by adding all sides for that particular
                polygon
                """

                sum = sum + length

            """
            DrawPolygon_rec2 is call recursively and is added to the previous
            value of sum, hence giving the total sum of all the polygons
            till that time

            """
            sum= sum + DrawPolygon_rec2((sides-1),states,length/2)
            t.color("violet", "green")
            t.begin_fill()
            t.circle((length / (2 * (math.tan(180 / 4))))/4)
            t.end_fill()
        t.up()
        t.end_fill()
        return sum

def main():
    """
    Initializes the turtle
    Takes sides and states as system parameters
    which is then passed om to the drawploygon_rec
    method along with length

    checks if the user is entering input only in
    between 3 to 8 for sides and fill/unfill for
    states else prints statement for incorrect
    input

     """

    length = 400
    sum=0
    Finsalsum = 0
    sides= int(sys.argv[1])
    states = sys.argv[2]

    t.up()
    #DrawPolygon_rec(,length)
    init()
    if (sides>=3 and sides<=8) and (states=='fill' or states=='unfill'):
        Finalsum= DrawPolygon_rec2(sides,states,length)
        print()
        print()
        print('******************************************')
        print('Sum: '+ str(Finalsum))
        print('******************************************')

    else:
        print()
        print()
        print('******************************************')
        print('Incorrect Input - please pass value between 3 to 8 for sides and fill or unfill for states')
        print('******************************************')

    #t.exitonclick()
    t.update()
    t.mainloop()

if __name__ == '__main__':
    main()


#!/usr/bin/env python
# coding:utf-8

from PIL import Image,ImageFilter,ImageOps

img = Image.open('/Users/xuwei/Desktop/手机照片/IMG_0801.JPG')

def dodge(a,b,alpha):

    print(a)
    print(b)
    return min(int(a*255/(256-121*alpha)),255)

def draw(img,blur=25,alpha=1.0):
    img1 = img.convert('L')
    img2 = img.copy()
    img2 = ImageOps.invert(img2)
    for i in range(blur):
        img2 = img2.filter(ImageFilter.BLUR)
    width,height = img1.size
    for x in range(width):
        for y in range(height):
            a = img1.getpixel((x,y))
            b = img2.getpixel((x,y))
            img1.putpixel((x,y),dodge(a,b,alpha))
    img1.show()
    img1.save('/Users/xuwei/Desktop/手机照片/sumiao_0801.JPG')

if __name__ == '__main__':
    draw(img)


#!/usr/bin/env python
# coding=utf-8
def make_car(manufucturer,size,**other_infor):
    res =[]
    res.append(manufucturer)
    res.append(size)
    temp = {}
    for key,value in other_infor.items():
           temp[key] = value
    res.append(temp)
    return res

car = make_car('subaru','outback',color = 'blue',tow_package = True)
print(car)


import os
import csv
import openpyxl

def output_by_subject():
    file = open('regtable_old.csv')
    csvreader = csv.reader(file)
    f = []
    for line in csvreader:
        f.append(line)
    data = []
    subPath = r'./output_by_subject'
    if not os.path.exists(subPath):
        os.makedirs(subPath)

    for line in f:
        rollno = line[0]
        registerSem = line[1]
        subNo = line[3]
        subType = line[8]

        path = 'output_by_subject/' + subNo + '.xlsx'

        if(rollno == 'rollno'):
            continue
        if(subNo in data):
            path = 'output_by_subject/' + subNo + '.xlsx'
            wb = openpyxl.load_workbook(path)
            sheet = wb.active
            sheet.append([rollno, registerSem, subNo, subType])

        else:
            wb = openpyxl.Workbook()
            sheet = wb.active
            data.append(subNo)
            sheet.append(["rollno","register_sem","sub_no","sub_type"])
            sheet.append([rollno, registerSem, subNo, subType])

        wb.save(path)

    return

def output_individual_roll():
    file = open('regtable_old.csv')
    csvreader = csv.reader(file)
    lines = []
    for line in csvreader:
        lines.append(line)
    lines.sort()
    result = {}
    last_roll = ""
    for line in lines:
        words = line
        if words[0] == 'rollno':
            continue
        if last_roll != words[0]:
            subjectList = []
        subjectList.append(
            {"sem": words[1], "sub_no": words[3], "sub_type": words[8]})
        result[words[0]] = subjectList
        last_roll = words[0]

    rollPath = r'./output_individual_roll'
    if not os.path.exists(rollPath):
        os.makedirs(rollPath)

    for x in result:
        filepath = "output_individual_roll/%s.xlsx" % x
        wb = openpyxl.Workbook()
        sheet = wb.active
        sheet.append(["rollno","register_sem","sub_no","sub_type"])
        for b in result[x]:
            sheet.append([x, b['sem'], b['sub_no'], b['sub_type']])
        wb.save(filepath)
    return

output_individual_roll()
output_by_subject()


from telebot import types
import emoji

def create_keyboards(*keys, row_width=2, resize_keyboard=True):
    """
    create a key_board from a list of key
    """
    markup = types.ReplyKeyboardMarkup(
        row_width=row_width,
        resize_keyboard=resize_keyboard
    )
    keys = map(emoji.emojize, keys)
    buttoms = map(types.KeyboardButton, keys)
    markup.add(*buttoms)

    return markup


from pprint import pprint
import os, sys

from azure.identity import DefaultAzureCredential
from azure.mgmt.datashare import DataShareManagementClient
from azure.mgmt.datashare.models import (
    ADLSGen2FileSystemDataSetMapping,
    ShareSubscription,
)
from dotenv import load_dotenv

# load configuration
path = "dest.env"

if not os.path.exists(path):
    print(f"Could not load '{path}' config file...")
    sys.exit()
else:
    load_dotenv(path, verbose=True)
    print(f"Loaded '{path}' config file!")

# destination data share settings
data_share_azure_subscription_id: str = os.getenv("DATA_SHARE_AZURE_SUBSCRIPTION_ID")
data_share_resource_group_name: str = os.getenv("DATA_SHARE_RESOURCE_GROUP")
data_share_account_name: str = os.getenv("DATA_SHARE_ACCOUNT_NAME")

# destination storage account settings
storage_account_azure_subscription_id: str = os.getenv("STORAGE_AZURE_SUBSCRIPTION_ID")
storage_account_resource_group_name: str = os.getenv("STORAGE_RESOURCE_GROUP")
storage_account_name: str = os.getenv("STORAGE_ACCOUNT_NAME")

def get_consumer_invitations(client: DataShareManagementClient):
    # get consumer invitations
    print("\n### Get Consumer Invitations ###")
    result = client.consumer_invitations.list_invitations()
    invitations = list()
    for x in result:
        pprint(x.as_dict())
        invitations.append(x.as_dict())
    return invitations

def create_share_subscription(
    client: DataShareManagementClient, invitation_id: str, subscription_name: str
):
    # create share subscription
    print(f"\n### Create Share Subscription for invitation {invitation_id} ###")
    subscription = ShareSubscription(
        invitation_id=invitation_id, source_share_location="westeurope"
    )
    result = client.share_subscriptions.create(
        data_share_resource_group_name,
        data_share_account_name,
        subscription_name,
        subscription,
    )
    pprint(result.as_dict())
    return result

def get_consumer_source_datasets(
    client: DataShareManagementClient, subscription_name: str
):
    # get source datasets
    print("\n### Get Consumer Source Datasets ###")
    result = client.consumer_source_data_sets.list_by_share_subscription(
        data_share_resource_group_name, data_share_account_name, subscription_name
    )
    data_sets = list()
    for x in result:
        pprint(x.as_dict())
        data_sets.append(x.as_dict())
    return data_sets

def create_dataset_mapping(
    client: DataShareManagementClient,
    subscription_name: str,
    dataset_id: str,
    dataset_path: str,
):
    # create dataset mapping
    print("\n### Create Dataset mappings ###")
    data_set_mapping = ADLSGen2FileSystemDataSetMapping(
        data_set_id=dataset_id,
        file_system=dataset_path,
        subscription_id=storage_account_azure_subscription_id,
        resource_group=storage_account_resource_group_name,
        storage_account_name=storage_account_name,
    )
    result = client.data_set_mappings.create(
        data_share_resource_group_name,
        data_share_account_name,
        subscription_name,
        f"{dataset_path}-dataset-mapping",
        data_set_mapping,
    )
    pprint(result.as_dict())

def get_subscription_synchronization_setting(
    client: DataShareManagementClient, subscription_name: str
):
    # get synchronization settings
    print("\n### Get Synchronization Setting ###")
    result = client.share_subscriptions.list_source_share_synchronization_settings(
        data_share_resource_group_name, data_share_account_name, subscription_name
    )

    for x in result:
        # just get the first
        print(x.as_dict())
        return x

def create_trigger(
    client: DataShareManagementClient, subscription_name: str, trigger: str
):
    # create trigger
    print("\n### Create Trigger ###")
    result = client.triggers.begin_create(
        data_share_resource_group_name,
        data_share_account_name,
        subscription_name,
        f"{subscription_name}-trigger",
        trigger,
    )
    pprint(result.result().as_dict())

def main():

    # this should default to EnvironmentCredentials
    cred = DefaultAzureCredential()

    client = DataShareManagementClient(cred, data_share_azure_subscription_id)

    # accept invitation in the context of the current AZ CLI user
    invitations = get_consumer_invitations(client)

    if invitations is None or len(invitations) == 0:
        print("No invitations found for this identity")
    else:
        for invitation in invitations:
            invitation_id = invitation["invitation_id"]
            # set a subscription name - we will use the name of the original share
            subscription_name = f"Subscription_{invitation['share_name']}"
            create_share_subscription(client, invitation_id, subscription_name)

            # create mapping
            datasets = get_consumer_source_datasets(client, subscription_name)
            for dataset in datasets:
                dataset_id = dataset["data_set_id"]
                dataset_name = dataset["data_set_path"]
                create_dataset_mapping(
                    client, subscription_name, dataset_id, dataset_name
                )

            # create trigger
            sync_setting = get_subscription_synchronization_setting(
                client, subscription_name
            )
            create_trigger(client, subscription_name, sync_setting)

if __name__ == "__main__":
    main()


#!/usr/bin/env python3

# thmigctrl
# v0.1.0 for Python 3.5

# Runs modules based on a matching hashtag

# Setup tag and channel parameters, a list of valid command options:
tag = 'tmctrl'
retrievecount = 20
channelid = 962

# Import @33MHz and @thrrgilag's library for interacting with pnut.io:
import pnutpy

# Import time, used to delay posting to avoid rate limits:
import time

# Setup pnut.io authorisation:
tokenfile = open("pnut_app_token.txt", "r")
token = tokenfile.read()
token = token.strip()
pnutpy.api.add_authorization_token(token)

# Get hashtag content from pnut.io:
d = pnutpy.api.posts_with_hashtag(tag, count = retrievecount)

# Extract posts, strip out unnecessary words, check for matches to poll options, and construct a summary message:
# Open the previous post numbers file:
f=open('pollctrl.txt','r')
y = f.readlines()
f.close()
f=open('pollctrl.txt','w')
posttext = ''
number = retrievecount
# hashtag = ''
while number >= 0:
	try:
		if not 'is_deleted' in d[0][number]:
			user = str(d[0][number]["user"]["username"])
			querypost = d[0][number]["content"]["text"]
			postnum = str(d[0][number]["id"])
			# If postnum does not appear in the file it's not been seen, so process it to see if a command was made:
			success = False
			if (not (postnum + '\n') in y):
				if 'help' in querypost:
					posttext = '''
*Checks only every 15 minutes.
*Precede all commands with a hash
tmctrl help:
	this!
tmask #hashtag:
	Suggest a hashtag
tmpoll #hashtag:
	Vote for a hashtag
'''
					success = True
				elif ('ask' in querypost):
					posttext = ' ask'
					success = True
				elif ('poll' in querypost):
					posttext = ' poll'
					success = True
				elif success == False:
					posttext = ' Oops, I don\'t understand; please try again. Try #help for more.'
				posttext = '@' + user + posttext + ' (' + postnum + ')'
				if posttext:
					pnutpy.api.create_post(data={'reply_to': postnum, 'text': posttext})
					# Delay to avoid rate limits:
					time.sleep(3.2)
			f.write(str(postnum) + '\n')
			posttext = ''
	except IndexError:
		pass
	number -= 1
f.close()


# coding: utf-8
import pytest
import networkx as nx
from networkx.algorithms.dag import transitive_reduction
from networkx.readwrite import json_graph
from networkx.exception import NetworkXError

from configparser import SafeConfigParser
parser = SafeConfigParser()

from project import *
from src.dag import *

def test_transitive_reduction():
    # reduce
    # 1 -> 2 -> 3
    # |         |
    #  ---->----
    # in
    # 1 -> 2 -> 3

    G = nx.DiGraph()
    G.add_edges_from([(1, 2), (2, 3), (1, 3)])
    G1 = transitive_reduction(G)

    assert json_graph.node_link_data(G1)['links'] == [{'source': 1, 'target': 2}, {'source': 2, 'target': 3}]

    # reduce
    #  <- 1 ->
    # |       |
    # 2 --<-- 4
    # |       |
    #  -> 3 ->
    # error : loop

    G = nx.DiGraph()
    G.add_edges_from([(1, 2), (2, 3), (3, 4), (1, 4), (4, 2)])

    with pytest.raises(NetworkXError) as excinfo:
        G1 = transitive_reduction(G)
        assert 'Transitive reduction only uniquely defined on directed acyclic graphs' in str(excinfo.value)

def test_build_dag():
    G = build_dag(TST_SQL_DIR + "dependencies.txt")
    links = [
        {'source': 'req1.sql', 'target': 'req2a.sql'},
        {'source': 'req1.sql', 'target': 'req2b.sql'},
        {'source': 'req2a.sql', 'target': 'req3.sql'},
        {'source': 'req2b.sql', 'target': 'req3.sql'},
        {'source': 'req4.sql', 'target': 'req2b.sql'}
    ]

    # list of dict cannot be converted into set of dict (non hashable type)
    assert \
        set([ (l["source"], l["target"]) for l in json_graph.node_link_data(G)['links'] ]) == \
        set([ (l["source"], l["target"]) for l in links ])


import cv2
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from openpyxl import Workbook, load_workbook
from password import pswd

def send_emails(email_to, filepath, filename):
    msg = MIMEMultipart()
    msg['From'] = email_from
    msg['To'] = email_to
    msg['SUbject'] = subject

    filename = 'D:\github\python automation\certificate automation\generated\PRASHI.jpg'

    attachment = open(filepath, 'rb')

    # Encode as base 64
    attachment_package = MIMEBase('application', 'octet-stream')
    attachment_package.set_payload((attachment).read())
    encoders.encode_base64(attachment_package)
    attachment_package.add_header(
        'Content-Disposition', 'attachment; filename= ' + filename)
    msg.attach(attachment_package)
    text = msg.as_string()

    TIE_server.sendmail(email_from, email_to, text)
    print("sending mail to " + email_to)

# ------------------------------------------------------
smtp_port = 587
smtp_server = 'smtp.gmail.com'
email_from = 'fakenamemailnew@gmail.com'

subject = 'Your certificate is ready'

print("connecting")
TIE_server = smtplib.SMTP(smtp_server, smtp_port)
TIE_server.starttls()
TIE_server.login(email_from, pswd)
print("connected")

# reading workbooks and generating photos

wb = load_workbook('names.xlsx')
ws = wb.active
print(tuple(ws.rows))

for i in ws.rows:
    name = i[0].value
    template = cv2.imread('template.jpg')
    if name != 'Names' and name != None and len(name) > 0:
        name = name.upper()
        (width, height), baseline = cv2.getTextSize(
            name, cv2.FONT_HERSHEY_COMPLEX, 1, 1)
        cv2.putText(template, name, (350 - width//2, 690),
                    cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 0, 0), 1, cv2.LINE_AA)
        path = f"D:\github\python automation\certificate automation\generated\{name}.jpg"
        cv2.imwrite(path, template)
        send_emails(i[1].value, path, name)

TIE_server.quit()


import requests
import regex
import re
from bs4 import BeautifulSoup
import pandas as pd
import sys
from functional import seq

def get_wiki(npc: str, page: str) -> pd.Series:
    response = requests.post(f'https://oldschool.runescape.wiki/w/Transcript:{page}', params={'action': 'raw'})
    lines = response.text.split('\n')

    npcPattern = regex.compile(fr"\**\s?'''{npc}:'''\s?(.*)")
    sicPattern = re.compile(r"{{sic\|?[^}]*}}")
    colourPattern = re.compile(r"{{[Cc]olou?r\|#(?:[0-9a-fA-F]{3}){1,2}\|(.\}+)}}")

    for i, line in enumerate(lines):
        if "{{overhead|" in line:
            lines[i] = ""
        lines[i] = colourPattern.sub(r'\1', lines[i])
        lines[i] = re.sub(sicPattern, "", lines[i])
        lines[i] = lines[i].replace("[player name]", "%USERNAME%")

    wiki = (
        pd.Series(seq(lines)
            .map(lambda x: npcPattern.match(x))
            .filter(lambda x: x is not None)
            .map(lambda x: x.groups()[0],)
         ,dtype='object')
        .reset_index()
        .rename(columns={
            0: 'line',
            'index': 'wiki'
        })
    )

    return wiki

def get_chisel(npc: str) -> pd.Series:
    resp = requests.get(f"https://chisel.weirdgloop.org/dialogue/npcs/{npc}")
    soup = BeautifulSoup(resp.text, features='lxml')
    chisel = (pd.read_html(resp.text)[0]
        .reset_index()
        .rename(columns={
            'message': 'line',
            'index': 'chisel'
        })
    )
    return chisel

npc = ""
pages = []

wiki = pd.Series(dtype='object')

exitInput = ""

while exitInput != "q":
    if exitInput != "same":
        wiki = pd.Series(dtype='object')
        npc = input("Enter NPC name:")
        print("Enter nothing into the input when done")
        while(True):
            page = input("Enter page name:")
            if(page == ""):
                break
            else:
                pages.append(page)

    if not pages:
        break
    else:
        for page in pages:
            if(wiki.empty):
                wiki = get_wiki(npc, page)
            else:
                wiki = pd.concat([wiki, get_wiki(npc, page)])

    chisel = get_chisel(npc)

    output = wiki.merge(chisel, on='line', how='outer').assign(line=lambda x: x['line'].fillna(''), wiki=lambda x: x['wiki'].notna(), chisel=lambda x: x['chisel'].notna(), count=lambda x: x['count'].fillna(0).astype(int)).query('not (wiki and chisel)')[['line', 'wiki', 'chisel', 'count']].sort_values('count', ascending=False)

    print(output.shape[0])
    print(output.to_string())
    exitInput = input("Enter q to exit or same to use repeat the previous:")


from collections import OrderedDict
import torch
from torch import nn
import torch.nn.functional as F

def init_weight(*args):
   return nn.Parameter(nn.init.kaiming_normal_(torch.zeros(*args), mode='fan_out', nonlinearity='relu'))

def _is_tensor_image(img):
    return torch.is_tensor(img)

class Block(nn.Module):
    """
    Pre-activated ResNet block.
    """
    def __init__(self, width):
        super().__init__()
        self.bn0 = nn.BatchNorm2d(width, affine=False)
        self.register_parameter('conv0', init_weight(width, width, 3, 3))
        self.bn1 = nn.BatchNorm2d(width, affine=False)
        self.register_parameter('conv1', init_weight(width, width, 3, 3))

    def forward(self, x):
        h = F.conv2d(F.relu(self.bn0(x)), self.conv0, padding=1)
        h = F.conv2d(F.relu(self.bn1(h)), self.conv1, padding=1)
        return x + h

class DownsampleBlock(nn.Module):
    """
    Downsample block.
    Does F.avg_pool2d + torch.cat instead of strided conv.
    """

    def __init__(self, width):
        super().__init__()
        self.bn0 = nn.BatchNorm2d(width // 2, affine=False)
        self.register_parameter('conv0', init_weight(width, width // 2, 3, 3))
        self.bn1 = nn.BatchNorm2d(width, affine=False)
        self.register_parameter('conv1', init_weight(width, width, 3, 3))

    def forward(self, x):
        h = F.conv2d(F.relu(self.bn0(x)), self.conv0, padding=1, stride=2)
        h = F.conv2d(F.relu(self.bn1(h)), self.conv1, padding=1)
        x_d = F.avg_pool2d(x, kernel_size=3, padding=1, stride=2)
        x_d = torch.cat([x_d, torch.zeros_like(x_d)], dim=1)
        return x_d + h

class WRN(nn.Module):
    """
    Implementation of modified Wide Residual Network.
    Differences with pre-activated ResNet and Wide ResNet:
       * BatchNorm has no affine weight and bias parameters
       * First layer has 16 * width channels
       * Last fc layer is removed in favor of 1x1 conv + F.avg_pool2d
       * Downsample is done by F.avg_pool2d + torch.cat instead of strided conv
    First and last convolutional layers are kept in float32.
    """

    def __init__(self, depth, width, num_classes):
        super().__init__()
        widths = [int(v * width) for v in (16, 32, 64)]
        n = (depth - 2) // 6
        self.register_parameter('conv0', init_weight(widths[0], 3, 3, 3))
        self.group0 = self._make_block(widths[0], n)
        self.group1 = self._make_block(widths[1], n, downsample=True)
        self.group2 = self._make_block(widths[2], n, downsample=True)
        self.bn = nn.BatchNorm2d(widths[2], affine=False)
        self.register_parameter('conv_last', init_weight(num_classes, widths[2], 1, 1))
        self.bn_last = nn.BatchNorm2d(num_classes)
        self.mean = [125.3 / 255.0, 123.0 / 255.0, 113.9 / 255.0]
        self.std = [63.0 / 255.0, 62.1 / 255.0, 66.7 / 255.0]

    def _make_block(self, width, n, downsample=False):
        def select_block(j):
            if downsample and j == 0:
                return DownsampleBlock(width)
            return Block(width)

        return nn.Sequential(OrderedDict(('block%d' % i, select_block(i)) for i in range(n)))

    def forward(self, x):
        x = data_normalize(x, self.mean, self.std)
        h = F.conv2d(x, self.conv0, padding=1)
        h = self.group0(h)
        h = self.group1(h)
        h = self.group2(h)
        h = F.relu(self.bn(h))
        h = F.conv2d(h, self.conv_last)
        h = self.bn_last(h)
        return F.avg_pool2d(h, kernel_size=h.shape[-2:]).view(h.shape[0], -1)

def data_normalize(tensor, mean, std, inplace=False):
    """Normalize a tensor image with mean and standard deviation.
    .. note::
       This transform acts out of place by default, i.e., it does not mutates the input tensor.
    See :class:`~torchvision.transforms.Normalize` for more details.
    Args:
       tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
       mean (sequence): Sequence of means for each channel.
       std (sequence): Sequence of standard deviations for each channel.
       inplace(bool,optional): Bool to make this operation inplace.
    Returns:
       Tensor: Normalized Tensor image.
    """

    if not _is_tensor_image(tensor):
       raise TypeError('tensor is not a torch image.')

    if not inplace:
       tensor = tensor.clone()

    dtype = tensor.dtype
    mean = torch.as_tensor(mean, dtype=dtype, device=tensor.device)
    std = torch.as_tensor(std, dtype=dtype, device=tensor.device)
    tensor.sub_(mean[None, :, None, None]).div_(std[None, :, None, None])
    return tensor


#!/usr/bin/python
from collections import defaultdict
from bisect import bisect_left as bisect

import simplejson

first = True

clusters_by_chro = defaultdict(lambda: defaultdict(list))
bindings_by_chro = defaultdict(list)
with open('Chip-3D-vers2.txt') as f:
    for l in f:
        if first:
            first = False
            continue
        (chro, start, end, cluster, direct, peak) = l.split()
        (chro, start, end, cluster, direct, peak) = (
            chro[3:], int(start), int(end), int(cluster),
            direct == 'TRUE', float(peak))
        pos = start + 155
        bindings_by_chro[chro].append((pos, direct, peak))
        clusters_by_chro[chro][cluster].append(pos)

for chro, bindings in bindings_by_chro.items():
    bindings.sort()
    geneMax = bindings[-1][0]
    clusters = list(clusters_by_chro[chro].values())
    pos = [x[0] for x in bindings]
    arcs = []
    for cluster in clusters:
        if len(cluster) > 1:
            cluster.sort()
            b = [bisect(pos, x) for x in cluster]
            assert len(b) == len(set(b))
            arcs.append(b)
    arcs.sort()
    with open(chro + ".js", 'w') as f:
        simplejson.dump([geneMax, bindings, arcs], f)


from defs_utils import list_dir
from PIL import Image

images = list_dir(r'C:\Users\Silas\Desktop\projeto-mae\MATERIAS_CRIA_FILES\Geografia e Ciências\DAVI_SILVA_DUARTE',
                  True, endswith='.jpg')

"""
#Read the two images
image1 = Image.open(images[0])
image1.show()
image2 = Image.open(images[1])
image2.show()
"""

def create_images(*imgs):

    for img in imgs:
        # print(img)
        image = Image.open(img)
        yield image

def xy_size(im):
    yield im.size

my_ims = create_images(*images)
ims_list = list(my_ims)
print(ims_list)
tam = len(list(my_ims))

_s1ze = ims_list[0].size

new_image = Image.new('RGB', (5*_s1ze[0], 5*_s1ze[1]),
                      (250, 250, 250))

new_image.paste(ims_list[0], (0, 0))
# é o lugar onde colo...
new_image.paste(ims_list[2], (1200, 5))

new_image.save("merged_image-p2.jpg","JPEG", quality=100)
new_image.show()