import sys
import os
import heapq

from datetime import datetime

column_details={}
input_size=0
no_partitions=0
index_list=[]
skip_data={}
record_size=0

class MyObject(object):
    def __init__(self, val):
        #print(val)
        self.val = val
    def __lt__(self, other):
        if sort_order=='asc':
            return self.val[0]<other.val[0]
        elif sort_order=='desc':
            return self.val[0]>other.val[0]
        #return cmp(self.val, other.val)

def readmeta():
    global column_details
    global record_size

    f=open('metadata.txt','r')
    cont=f.read().strip()
    mylst=cont.split('\n')
    for item in mylst:
        col=item.split(',')
        column_details[col[0]]=int(col[1])
        record_size+=int(col[1])

    record_size+=2*(len(mylst)-1)
    f.close()
    #print(record_size)

def getinputsize():
    global input_size
    input_size=os.stat('input.txt').st_size

def getpartitions():
    global no_partitions
    no_partitions=int(input_size/mem_size)
    if(no_partitions==0):
        no_partitions=1

    if(no_partitions*record_size>mem_size):
        print("Memory limit exceeded")
        sys.exit()

    f=open(input_file,'rb')
    #read_bytes=0
    for i in range(1,no_partitions+1):
        f1=open('p'+str(i),'wb')

        cont=f.read(mem_size)

        f1.write(cont)
        f1.close()

    f.close()

def getindexlst():
    global index_list
    col_list=list(column_details.keys())

    for col in tosort_on:
        index_list.append(col_list.index(col))

def getskipdata():
    global skip_data
    for col in tosort_on:
            skip_data[col]=0
            for i in list(column_details.keys()):
                if i!=col:
                    skip_data[col]+=(column_details[i])+2
                else:
                    break

def sort_lists(file_lst,str_lst):
    zipped_pairs = zip(str_lst, file_lst)

    if sort_order=='asc':
        z = [x for _, x in sorted(zipped_pairs)]
    elif sort_order=='desc':
        z = [x for _, x in sorted(zipped_pairs,reverse=True)]

    return z

def sortpartitions():
    global skip_data

    getskipdata()

    print("##running Phase-1")
    print("Number of sub-files (splits): " + str(no_partitions)+'\n')
    #print(skip_data)

    for p in range(1,no_partitions+1):
        f=open('p'+str(p),'r')

        file_lst=f.readlines()
        temp_str=''

        f.close()
        #print(file_lst)
        #print("after")

        str_lst=[]
        for row in file_lst:
            temp_str=''
            for i in list(skip_data.keys()):
                read_till=skip_data[i]+column_details[i]
                temp_str+=row[skip_data[i]:read_till]
            str_lst.append(temp_str)

        print("sorting #"+str(p)+" sublist")
        file_lst=sort_lists(file_lst,str_lst)
        #print(file_lst)

        print("Writing to disk #"+str(p)+'\n')
        f=open('p'+str(p),'w')
        for row in file_lst:
            f.write(row)
        f.close()

def generateoutput():
    global skip_data
    hq=[]

    print("##running phase-2\n")
    fo=open(output_file,'w')

    part_content={}
    part_ptr={}
    for p in range(1,no_partitions+1):
        fi=open('p'+str(p),'r')
        part_ptr['p'+str(p)]=fi

        cont=fi.readline()
        temp_lst=[]

        temp_str=''
        for i in list(skip_data.keys()):
            read_till=skip_data[i]+column_details[i]
            temp_str+=cont[skip_data[i]:read_till]
        temp_lst.append(temp_str)
        temp_lst.append('p'+str(p))

        part_content['p'+str(p)]=cont
        heapq.heappush(hq, MyObject(temp_lst))

    print("Sorting...\n")
    print("Writing to disk\n")

    while len(hq)!=0:
        obj = heapq.heappop(hq)
        #print(type(obj.val))
        part=obj.val[1]
        cont=str(part_content[part])

        fo.write(cont)

        re_cont=part_ptr[part].readline()
        if(len(re_cont)==0):
            os.remove(part)
            part_ptr[part].close()
            continue

        temp_str=''
        temp_lst=[]
        for i in list(skip_data.keys()):
            read_till=skip_data[i]+column_details[i]
            temp_str+=re_cont[skip_data[i]:read_till]
        temp_lst.append(temp_str)
        temp_lst.append(part)

        part_content[part]=re_cont
        heapq.heappush(hq, MyObject(temp_lst))

    fo.close()
    print("###completed execution")

if len(sys.argv)<5:
    print("Invalid Parameters")
    sys.exit()

input_file=sys.argv[1]
output_file=sys.argv[2]
mem_size=int(sys.argv[3])*pow(10,6)
sort_order=sys.argv[4]
tosort_on=''

for i in range(5,len(sys.argv)):
    tosort_on+=sys.argv[i]+' '
tosort_on=tosort_on[:-1].split()

print("###start execution\n")
b_time=datetime.now()
print(b_time)
readmeta()
getinputsize()
getpartitions()
getindexlst()
sortpartitions()

generateoutput()
e_time=datetime.now()
print(e_time)
print("time taken: "+ str(e_time-b_time))

#print(input_size)
#print(tosort_on)
#print(index_list)
#print(no_partitions)
#print(column_details)


from entities.entities import Device, User
from helper.device_helper import DeviceHelper
from my_stats_messages.my_stats_messages import *
import logging

class UserHelper():
    def __init__(self):
        logging.info("constructor calling.")

    def link_device(self, user, device):
        user.devices.append(device)
        user.put()
        return self.get_user_by_fb_id(user.fb_id)

    def create_user(self, fb_id, name, email):
        users = self.get_user_by_fb_id(fb_id)
        if len(users) > 0:
            pass
        else:
            user = User(fb_id=fb_id, name=name, email=email)
            user.put()

        return self.get_user_by_fb_id(fb_id)

    def get_user_by_fb_id(self, fb_id):
        query = User.query(User.fb_id == fb_id)
        return query.fetch()

    def create_user_resp(self, fb_id):
        users = self.get_user_by_fb_id(fb_id)
        device_helper = DeviceHelper()
        if len(users) > 0:
            user = users[0]
            user_resp = UserMessage(fb_id=user.fb_id, name=user.name, email=user.email)
            if len(user.devices) > 0:
                for device in user.devices:
                    user_resp.devices.append(device_helper.create_device_resp(device))
        return user_resp

    def is_device_already_registered(self, device, device_list):
        is_registered = False
        logging.info("****Before loop*****")
        logging.info(device)
        logging.info(device.device_id)
        for d in device_list:
            logging.info(sorted(device.device_id.split()))
            logging.info(sorted(d.device_id.split()))

            if sorted(device.device_id.split()) == sorted(d.device_id.split()):
                logging.info("is_registered=True")
                is_registered = True
        return is_registered


"""
587 Erect the Fence

There are some trees, where each tree is represented by (x,y) coordinate in a two-dimensional garden.
Your job is to fence the entire garden using the minimum length of rope as it is expensive.
The garden is well fenced only if all the trees are enclosed. Your task is to help find the coordinates of trees which are exactly located on the fence perimeter.

Example 1:
Input: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
Output: [[1,1],[2,0],[4,2],[3,3],[2,4]]
Explanation:

Example 2:
Input: [[1,2],[2,2],[4,2]]
Output: [[1,2],[2,2],[4,2]]
Explanation:

Even you only have trees in a line, you need to use rope to enclose them.
Note:

All trees should be enclosed together. You cannot cut the rope to enclose trees that will separate them in more than one group.
All input integers will range from 0 to 100.
The garden has at least one tree.
All coordinates are distinct.
Input points have NO order. No order required for output.
"""

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return '['+str(self.x)+','+str(self.y)+']'

from functools import reduce
class Solution:
    # help from https://www.cnblogs.com/weedboy/p/6896478.html
    def outerTrees(self, points):
        """
        :type points: List[Point]
        :rtype: List[Point]
        """
        def sign(p, q, r):
            # < 0: r is on the right side of vector p->q (or external)
            # == 0: r is on the line of vector p->q
            # > 0: r is on the left side of vector p->q (or inner)
            return (p.x-r.x)*(q.y-r.y) - (p.y-r.y)*(q.x-r.x)

        def drive(hull, r): # reduce inner call: accum_value = func(accum_value, p)
            hull.append(r)
            while len(hull) > 2 and sign(*hull[-3:]) < 0:
                hull.pop(-2)

            return hull

        # main
        points.sort(key = lambda p: (p.x, p.y))
        # get lower board
        lower = reduce(drive, points, [])
        # get upper board
        upper = reduce(drive, points[::-1], [])

        return list(set(lower + upper))

nums = [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
points = []
for x, y in nums:
    points.append(Point(x, y))

print(Solution().outerTrees(points))


# pytesseract is for OCR from images
# Check PyPi Project - https://pypi.org/project/pytesseract/
import pytesseract

# OpenCV for pre-processing images before feeding to the tesseract OCR.
# Check PyPi Project - https://pypi.org/project/opencv-python/
import cv2

import numpy as np

from yolo_object import detect_license_plate

import numpy as np
from skimage import io
from skimage.transform import rotate
from skimage.color import rgb2gray
from deskew import determine_skew

# Including the path to the tesseract.exe for PyTesseract to work
pytesseract.pytesseract.tesseract_cmd = 'Tesseract-OCR/tesseract.exe'
# PyTesseract Config Options
# language = English
# psm or Page segmentation modes = 7 represents treat the image as a single line text
# oem or Engine Mode - Nueral Nets LSTM engine only
config = ('-l eng --oem 1 --psm 7')

def crop_image_only_outside(img,tol=0):
    # img is 2D image data
    # tol  is tolerance
    mask = img>tol
    m,n = img.shape
    mask0,mask1 = mask.any(0),mask.any(1)
    col_start,col_end = mask0.argmax(),n-mask0[::-1].argmax()
    row_start,row_end = mask1.argmax(),m-mask1[::-1].argmax()
    return img[row_start:row_end,col_start:col_end]

def secondCrop(img):
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret,thresh = cv2.threshold(gray,127,255,0)
    contours,_ = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    areas = [cv2.contourArea(c) for c in contours]
    if(len(areas)!=0):
        max_index = np.argmax(areas)
        cnt=contours[max_index]
        x,y,w,h = cv2.boundingRect(cnt)
        bounds = cv2.boundingRect(cnt)
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
        secondCrop = img[y:y+h,x:x+w]
    else:
        secondCrop = img
    return secondCrop

def read_text(img):
    # img = cv2.imread('images/img7.jpg')
    img = detect_license_plate(img)
    second_img = secondCrop(img)
    img = cv2.resize(second_img, None, fx=7, fy=7, interpolation = cv2.INTER_AREA)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(img,(5,5),0)
    r, threshold = cv2.threshold(blur, 150, 255, cv2.THRESH_BINARY)
    cv2.imwrite('test.png', threshold)
    image = io.imread('test.png')
    grayscale = rgb2gray(image)
    angle = determine_skew(grayscale)
    rotated = rotate(image, angle, resize=True) * 255
    text = pytesseract.image_to_string(rotated, config=config)
    print(text)
    return text
    # cv2.imshow("Image", rotated)
    # cv2.waitKey(0)
    # text = read_text(img)

if __name__ == '__main__':
    read_text()


#! /usr/bin/env python
# -*- coding: utf-8 -*-
import json
import time
import pymongo
from main.config import *

class GetSysData(object):

    def __init__(self, machine_id, monitor_item, timing, no=0):
        self.machine_id = machine_id
        self.monitor_item = monitor_item
        self.timing = timing
        self.no = no

    def get_data(self):
        client = pymongo.MongoClient(MONGO_URL,MONGO_PORT)
        db = client[MONGO_DB]
        collection = db[self.machine_id]
        now_time = int(time.time())
        find_time = now_time-self.timing
        cursor = collection.find({'timestamp': {'$gte': find_time}}, {self.monitor_item: 1, "timestamp": 1}).limit(self.no)
        return cursor


# Here import your model
from sklearn.svm import SVR

from sklearn.model_selection import validation_curve
from sklearn.utils import resample
from sklearn import preprocessing
import pandas as pd
import numpy as np
from helpers import root_mean_squared_log_error, rmsle_scorer

print("load")
dataset = pd.read_csv('data/train_merged.csv')

# Extract the objective values
y = dataset['trip_duration_in_minutes'].values
# Delete irrelevant columns in training set
del dataset['trip_duration'], dataset["id"], dataset["trip_duration_in_minutes"]
X = dataset.values

means = np.nanmean(X, axis=0)
nan_locations = np.where(np.isnan(X))
X[nan_locations] = np.take(means, nan_locations[1])

print("preprocess")
# Normalize X
X = preprocessing.scale(X)

X_train, y_train = resample(X, y, n_samples=5000)

print("fit")

regressor = SVR(verbose=10)

gamma_range =  np.logspace(-5, 2, 10)
train_scores, valid_scores = validation_curve(regressor, X_train, y_train, "gamma", gamma_range, n_jobs=-1, scoring=rmsle_scorer)
valid_scores = [np.mean(s) for s in valid_scores]

# Take the alpha giving the highest validation score, and test it on test set
best_gamma = gamma_range[np.nanargmax(valid_scores)]
print("best gamma:", best_gamma)
regressor.set_params(gamma=best_gamma)

X_train, y_train = resample(X, y, n_samples=20000)
regressor.fit(X_train, y_train)

print("test")
# Since we can't load the whole dataset, do batch testing
batch_size = 5000
X_test, y_test = resample(X, y, n_samples=100000)
y_pred = np.ndarray((0,))
for i in range(0, X_test.shape[0], batch_size):
    print(i)
    y_pred = np.hstack((y_pred, regressor.predict(X_test[i: i + batch_size])))
print("RMSLE =", root_mean_squared_log_error(y_test, y_pred))


import matplotlib.pyplot as plt
from model_config import vgg16_net, model, batch_size
from tensorflow.keras.optimizers import Adam
from model_compile import nb_train_samples, train_generator, val_generator, nb_validation_samples, test_generator

vgg16_net.trainable = True
trainable = False
for layer in vgg16_net.layers:
    if layer.name == 'block5_conv3':
        trainable = True
    layer.trainable = trainable

# model.summary()  # Comment if you don`t need the summary

if __name__ == "__main__":
    model.compile(loss='categorical_crossentropy',
                  optimizer=Adam(lr=1e-5),
                  metrics=['accuracy'])

    history = model.fit(
                train_generator,
                steps_per_epoch=nb_train_samples // batch_size,
                epochs=5,
                validation_data=val_generator,
                validation_steps=nb_validation_samples // batch_size)

    scores = model.evaluate(test_generator)
    print(f"Accuracy on test data: {(scores[1]*100):.2f}")
    model.save("tuned_model.keras")

    plt.plot(history.history['accuracy'])
    plt.plot(history.history['val_accuracy'])
    plt.title('Model accuracy')
    plt.ylabel('accuracy')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Val'], loc='upper left')
    plt.savefig('tuned_model_training.png')
    plt.show()


# Documentação:
# https://pythonhosted.org/PyPDF2/
# Mais exemplos de uso:
# http://www.blog.pythonlibrary.org/2018/06/07/an-intro-to-pypdf2/

# pip install pypdf2 # virtualenv
# pipenv install pypdf2 # pipenv
import PyPDF2
from pathlib import Path

CAMINHO_ROOT = Path(__file__).parent
CAMINHO_PDF = CAMINHO_ROOT / 'pdf'
CAMINHO_NOVO_PDF = CAMINHO_PDF / 'novo_arquivo.pdf'
CAMINHO_PDF_SEPARADO = CAMINHO_ROOT / 'pdf_separado'

# Junta os arquivos
novo_pdf = PyPDF2.PdfMerger()
for arquivo_pdf in CAMINHO_PDF.iterdir():
    arquivo = open(arquivo_pdf, 'rb')
    novo_pdf.append(arquivo)

with open(CAMINHO_NOVO_PDF, 'wb') as meu_novo_pdf:
    novo_pdf.write(meu_novo_pdf)

# # Separa o arquivo
# with open(CAMINHO_NOVO_PDF, 'rb') as arquivo_pdf:
#     leitor = PyPDF2.PdfReader(arquivo_pdf)
#     num_paginas = len(leitor.pages)

#     for num_pagina in range(num_paginas):
#         escritor = PyPDF2.PdfWriter()
#         pagina_atual = leitor.pages[num_pagina]
#         escritor.add_page(pagina_atual)

#         with open(CAMINHO_PDF_SEPARADO / f'{num_pagina}.pdf', 'wb') as novo_pdf:
#             escritor.write(novo_pdf)


from enum import Enum
from pathlib import Path
import struct

def u(f, format):
    bytes = f.read(struct.calcsize(format))
    return struct.unpack(format, bytes)

class ChunkType(Enum):
    VERTEX = 0
    EDGE = 1
    FACE_EDGE = 2
    PLANE = 3
    TEXINFO = 4
    LIGHTMAP = 5
    FACE = 6
    OBJECT = 12

    LIGHTMAP_DATA = 26
    NUM = 27

def load(f, dump_geometry_to=None, verbose=False):
    magic = f.read(8)
    assert magic == b"S3D\0\x33\x33\xF3\x40"

    unk_0 = u(f, "<ffffff")
    print("HEADER", unk_0)

    chunks = []
    for i in range(ChunkType.NUM.value):
        start, length = u(f, "<II")
        try:
            t = ChunkType(i)
            print(t, f"{start:06X}h {length:06X}h")

            if t == ChunkType.VERTEX: print(f"\t{length // 12} vertices")
            if t == ChunkType.EDGE: print(f"\t{length // 4} edges")
            if t == ChunkType.FACE_EDGE: print(f"\t{length // 4} face edge indices")
            if t == ChunkType.PLANE: print(f"\t{length // 20} planes")
            if t == ChunkType.TEXINFO: print(f"\t{length // 48} texinfo entries")
            if t == ChunkType.LIGHTMAP: print(f"\t{length // 24} lightmap entries")
        except ValueError:
            print(f"BLOCK_{i:02X}h {start:06X}h {length:06X}h")

        chunks.append((start, length))

    assert f.tell() == chunks[0][0]

    if dump_geometry_to is not None:
        f.seek(chunks[ChunkType.VERTEX.value][0])

        for i in range(chunks[ChunkType.VERTEX.value][1] // 12):
            vtx = u(f, "<fff")
            dump_geometry_to.write(f"v {vtx[0]} {vtx[1]} {vtx[2]}\n")

        f.seek(chunks[ChunkType.EDGE.value][0])

        linesegs = []
        for i in range(chunks[ChunkType.EDGE.value][1] // 4):
            a, b = u(f, "<HH")
            linesegs.append((a, b))

        f.seek(chunks[ChunkType.FACE_EDGE.value][0])

        while f.tell() < chunks[ChunkType.FACE_EDGE.value][0] + chunks[ChunkType.FACE_EDGE.value][1]:
            start = None
            indexes = []

            while True:
                idx, = u(f, "<i")

                a, b = linesegs[abs(idx)]

                if idx < 0:
                    a, b = b, a

                if start is None:
                    start = a
                else:
                    pass

                indexes.append(a)

                if b == start:
                    break

            verts = " ".join([str(1 + x) for x in indexes])
            dump_geometry_to.write(f"f {verts}\n")

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description="Parse SBP format")
    parser.add_argument("input", type=Path)
    args = parser.parse_args()

    with open(args.input, "rb") as f:
        # For now, dump scene geometry
        dump_path = Path(args.input.name).with_suffix(".obj")
        with open(dump_path, "wt") as fobj:
            load(f, dump_geometry_to=fobj, verbose=True)
        print("Written", dump_path)


"""Call summarization routines."""

import click

from kmtools.action import summarize
from kmtools.action.kagi import kagi_action
from kmtools.source import WebResource

@click.command(name="summarize")
@click.option(
    "-q", "--quiet", is_flag=True, default=False, help="Output just the summary"
)
@click.option("-k", "--kagi", is_flag=True, default=False, help="Get summary from Kagi")
@click.argument("url")
@click.pass_obj
def summarize_command(details, url=None, quiet=False, kagi=False) -> None:
    """Output a summarization of the specified URL

    :param details: Context object
    :param url: URL to summarize
    """
    if url:
        source = WebResource(url)
        derived_date, summarization = summarize.summarize(source)
        if kagi:
            summarization = kagi_action.retrieve_summary(url)
        if not quiet:
            click.echo(
                f"The webpage at {url} was published on {derived_date}. It can be summarized as follows\n"
            )
        click.echo(summarization)
    else:
        click.echo("No URL submitted.")


import numpy as np
from itertools import product
from math import trunc
print('\n--> Loading parameters...')

global par
par = {

    'save_dir'              : './savedir/',
    'save_fn'               : 'rate_dms_t02_a0999',
    'iters_per_output'      : 1,

    'batch_size'            : 256,
    'iterations'            : 100001,

    'learning_method'       : 'TA',     # Evo search = 'ES', genetic = 'GA', thermal = 'TA'
    'cell_type'             : 'rate',   # 'rate', 'adex'
    'use_stp'               : True,
    'use_adam'              : True,     # Only for 'ES'

    'temperature'           : 0.2,
    'temperature_decay'     : 0.999,

    'ES_learning_rate'      : 0.005,
    'ES_sigma'              : 0.02,

    'local_learning'        : False,
    'local_learning_vars'   : ['W_out', 'b_out'],
    'local_learning_rate'   : 0.005,

    'EI_prop'               : 0.8,
    'balance_EI'            : True,
    'exc_model'             : 'RS',
    'inh_model'             : 'cNA',
    'current_divider'       : 3e6,

    'use_latency'           : False,
    'latency_min'           : 5,
    'latency_max'           : 20,

    'freq_cost'             : 1e-4,
    'freq_target'           : 0.,

    'reciprocal_cost'       : 0.,
    'reciprocal_threshold'  : 3.,
    'reciprocal_max'        : 0.2,

    'use_weight_momentum'   : False,
    'momentum_scale'        : 1.,

    'adam_beta1'            : 0.9,
    'adam_beta2'            : 0.999,
    'adam_epsilon'          : 1e-8,

    'n_networks'            : 5001,
    'n_hidden'              : 100,
    'n_output'              : 3,

    'num_motion_tuned'      : 24,
    'num_fix_tuned'         : 0,
    'num_rule_tuned'        : 0,
    'num_receptive_fields'  : 1,
    'num_motion_dirs'       : 8,

    'input_gamma'           : 0.08,
    'rnn_gamma'             : 0.04,
    'output_gamma'          : 0.08,
    'noise_rnn_sd'          : 0.2,
    'noise_in_sd'           : 0.1,

    'dt'                    : 20,
    'membrane_constant'     : 100,
    'output_constant'       : 20,

    'tau_fast'              : 200,
    'tau_slow'              : 1500,

    'dead_time'             : 100,
    'fix_time'              : 200,
    'sample_time'           : 200,
    'delay_time'            : 300,
    'test_time'             : 200,
    'mask_time'             : 40,
    'fixation_on'           : False,

    'survival_rate'         : 0.10,
    'mutation_rate'         : 0.25,
    'mutation_strength'     : 2,
    'cross_rate'            : 0.25,

    'task'                  : 'dms',
    'kappa'                 : 2.0,
    'tuning_height'         : 4.0,
    'response_multiplier'   : 4.0,
    'num_rules'             : 1,

}

def update_parameters(updates):
    for k in updates.keys():
        print(k.ljust(24), ': {}'.format(updates[k]))
        par[k] = updates[k]
    update_dependencies()

def update_dependencies():

    par['n_networks'] += 1 if par['learning_method'] == 'ES' \
        and par['n_networks']%2 == 0 else 0

    par['use_adam'] = par['use_adam'] and par['learning_method'] == 'ES'

    par['trial_length'] = par['dead_time'] + par['fix_time'] + +par['sample_time'] + par['delay_time'] + par['test_time']
    par['num_time_steps'] = par['trial_length'] // par['dt']

    par['n_input'] = par['num_motion_tuned']*par['num_receptive_fields'] + par['num_fix_tuned'] + par['num_rule_tuned']
    par['n_EI'] = int(par['n_hidden']*par['EI_prop'])

    par['h_init_init']  = np.zeros([par['n_networks'],1,par['n_hidden']]).astype(np.int8)
    par['W_in_init']    = np.minimum(np.random.exponential(scale=par['rnn_exponential'], size=[par['n_networks'], par['n_hidden'], par['n_output']]).astype(np.int8), 127)

    par['W_out_init']   = np.minimum(np.random.exponential(scale=par['rnn_exponential'], size=[par['n_networks'], par['n_hidden'], par['n_output']]).astype(np.int8), 127)
    par['W_rnn_init']   = np.minimum(np.random.exponential(scale=par['rnn_exponential'], size=[par['n_networks'], par['n_hidden'], par['n_output']]).astype(np.int8), 127)
    # par['W_rnn_init']   = np.float16(np.random.gamma(shape=par['rnn_gamma'], scale=1., size=[par['n_networks'], par['n_hidden'], par['n_hidden']]))

    if par['balance_EI']:
        par['W_rnn_init'][:,par['n_EI']:,:par['n_EI']] = np.random.exponential(scale=2*par['rnn_exponential'], size=par['W_rnn_init'][:,par['n_EI']:,:par['n_EI']].shape).astype(np.int8)
        par['W_rnn_init'][:,:par['n_EI'],par['n_EI']:] = np.random.exponential(scale=2*par['rnn_exponential'], size=par['W_rnn_init'][:,par['n_EI']:,:par['n_EI']].shape).astype(np.int8)

    par['b_rnn_init']   = np.zeros([par['n_networks'], 1, par['n_hidden']]).astype(np.int8)
    par['b_out_init']   = np.zeros([par['n_networks'], 1, par['n_output']]).astype(np.int8)

    par['W_rnn_mask']   = 1 - np.eye(par['n_hidden'])[np.newaxis,:,:]
    par['W_rnn_init']  *= par['W_rnn_mask']

    par['EI_vector']    = np.ones(par['n_hidden']).astype(np.int8)
    par['EI_vector'][par['n_EI']:] *= -1
    par['EI_mask']      = np.diag(par['EI_vector'])[np.newaxis,:,:]

    par['y_init_shape'] = [par['num_time_steps'], par['n_networks'], par['batch_size'], par['n_output']]

    par['dt_sec']       = par['dt']/1000
    par['alpha_neuron'] = np.float32(par['dt']/par['membrane_constant'])
    par['beta_neuron']  = np.float32(par['dt']/par['output_constant'])
    par['noise_rnn']    = np.float32(np.sqrt(2*par['alpha_neuron'])*par['noise_rnn_sd'])
    par['noise_in']     = np.float32(np.sqrt(2/par['alpha_neuron'])*par['noise_rnn_sd'])

    par['num_survivors'] = int(par['n_networks'] * par['survival_rate']) if par['learning_method'] in ['GA', 'TA'] else 1

    ### Synaptic plasticity
    if par['use_stp']:
        par['alpha_stf']  = np.ones((1, 1, par['n_hidden']), dtype=np.float32)
        par['alpha_std']  = np.ones((1, 1, par['n_hidden']), dtype=np.float32)
        par['U']          = np.ones((1, 1, par['n_hidden']), dtype=np.float32)

        par['syn_x_init'] = np.zeros((1, 1, par['n_hidden']), dtype=np.float32)
        par['syn_u_init'] = np.zeros((1, 1, par['n_hidden']), dtype=np.float32)

        for i in range(0,par['n_hidden'],2):
            par['alpha_stf'][0,0,i] = par['dt']/par['tau_slow']
            par['alpha_std'][0,0,i] = par['dt']/par['tau_fast']
            par['U'][0,0,i] = 0.15
            par['syn_x_init'][0,0,i] = 1
            par['syn_u_init'][0,0,i] = par['U'][0,0,i+1]

            par['alpha_stf'][0,0,i+1] = par['dt']/par['tau_fast']
            par['alpha_std'][0,0,i+1] = par['dt']/par['tau_slow']
            par['U'][0,0,i+1] = 0.45
            par['syn_x_init'][0,0,i+1] = 1
            par['syn_u_init'][0,0,i+1] = par['U'][0,0,i+1]

        par['stp_mod'] = par['dt_sec'] if par['cell_type'] == 'rate' else 1.

    ### Adaptive-Expoential spiking
    if par['cell_type'] == 'adex':

        # Note that voltages are in units of mV and currents
        # are in units of mA.  When pulling from a table based in volts/amps,
        # multiply E, V_T, D, b, V_r, and Vth by 1000
        par['cNA'] = {
            'C'   : 59e-12,     'g'   : 2.9e-9,     'E'   : -62,
            'V_T' : -42,        'D'   : 3,          'a'   : 1.8e-9,
            'tau' : 16e-3,      'b'   : 61e-9,      'V_r' : -54,
            'Vth' : 20,         'dt'  : par['dt']/1000 }
        par['RS']  = {
            'C'   : 104e-12,    'g'   : 4.3e-9,     'E'   : -65,
            'V_T' : -52,        'D'   : 0.8,        'a'   : -0.8e-9,
            'tau' : 88e-3,      'b'   : 65e-9,      'V_r' : -53,
            'Vth' : 20,         'dt'  : par['dt']/1000 }

        par['adex'] = {}
        for (k0, v_exc), (k1, v_inh) in zip(par[par['exc_model']].items(), par[par['inh_model']].items()):
            assert(k0 == k1)
            par_matrix = np.ones([1,1,par['n_hidden']], dtype=np.float32)
            par_matrix[...,:int(par['n_hidden']*par['EI_prop'])] *= v_exc
            par_matrix[...,int(par['n_hidden']*par['EI_prop']):] *= v_inh
            par['adex'][k0] = par_matrix

        par['w_init'] = par['adex']['b']
        par['adex']['current_divider'] = par['current_divider']

    ### Latency-based weights
    if par['use_latency']:
        par['max_latency'] = par['latency_max']//par['dt']
        par['latency_matrix'] = np.random.uniform(par['latency_min']//par['dt'], par['latency_max']//par['dt'], \
            size=[par['n_hidden'], par['n_hidden']]).astype(np.int8)

        par['latency_mask'] = np.zeros([par['max_latency'], par['n_hidden'], par['n_hidden']]).astype(np.int8)
        for i, j in product(range(par['n_hidden']), range(par['n_hidden'])):
            par['latency_mask'][par['latency_matrix'][i,j],i,j] = 1.

        par['state_buffer_shape'] = [par['max_latency'], par['n_networks'], par['batch_size'], par['n_hidden']]

update_dependencies()
print('--> Parameters loaded.\n')


from utils.util import Util
from db.database import Database
from datetime import date
from models.cliente import Cliente
from models.produto import Produto

class Pedido():
    def __init__(self):
        self.id_pedido = 0
        self.cliente = None
        self.itens = []
        self.total = 0

    def __to_object(itens):

        lista_produto = []
        lista_pedido = []
        lista_pedido_item_somado = []
        soma_total_pedido = 0

        for index, elem in enumerate(itens):
            curr_item = itens[index]
            next_item = itens[(index + 1) % len(itens)]

            id_pedido_atual = curr_item[3]
            id_pedido_proximo = next_item[3]

            if id_pedido_atual == id_pedido_proximo:

                id_item = curr_item[5]
                resultado = any(
                    elem == id_item for elem in lista_pedido_item_somado)
                if resultado == False:
                    produto = Produto(
                        curr_item[7], curr_item[8], curr_item[9], curr_item[10])
                    lista_produto.append(produto)
                    lista_pedido_item_somado.append(id_item)
                    soma_total_pedido += float(curr_item[10])

                id_item = next_item[5]
                resultado = any(
                    elem == id_item for elem in lista_pedido_item_somado)
                if resultado == False:
                    produto = Produto(
                        next_item[7], next_item[8], next_item[9], next_item[10])
                    lista_produto.append(produto)
                    lista_pedido_item_somado.append(id_item)
                    soma_total_pedido += float(next_item[10])

            else:
                cliente = Cliente(curr_item[0], curr_item[1], curr_item[2])

                pedido = Pedido()
                pedido.id_pedido = curr_item[3]
                pedido.cliente = cliente
                pedido.itens = lista_produto

                pedido.total = soma_total_pedido
                lista_pedido.append(pedido)

                lista_produto = []
                lista_pedido_item_somado = []
                soma_total_pedido = 0

        return lista_pedido

    @ staticmethod
    def obtem_todos():

        try:
            query_sql = " select cli.id_cliente, "
            query_sql += " cli.nome, "
            query_sql += " cli.email, "
            query_sql += " ped.id_pedido, "
            query_sql += " ped.data_pedido, "
            query_sql += " pit.id_item, "
            query_sql += " pit.data_pedido_item, "
            query_sql += " pro.id_produto, "
            query_sql += " pro.nome_produto, "
            query_sql += " pro.descricao, "
            query_sql += " pro.preco "
            query_sql += " from clientes as cli "
            query_sql += " inner join pedidos as ped on (cli.id_cliente = ped.id_cliente) "
            query_sql += " inner join pedido_itens as pit on (ped.id_pedido = pit.id_pedido) "
            query_sql += " inner join produtos as pro on(pit.id_produto = pro.id_produto) "
            query_sql += " order by ped.data_pedido"

            resultado = Database.consultar(query_sql)
            return Pedido.__to_object(resultado)

        except:
            return []

    @ staticmethod
    def obtem_por_id_pedido(id_pedido):
        query_sql = " select cli.id_cliente, "
        query_sql += " cli.nome, "
        query_sql += " cli.email, "
        query_sql += " ped.id_pedido, "
        query_sql += " ped.data_pedido, "
        query_sql += " pit.id_item, "
        query_sql += " pit.data_pedido_item, "
        query_sql += " pro.id_produto, "
        query_sql += " pro.nome_produto, "
        query_sql += " pro.descricao, "
        query_sql += " pro.preco "
        query_sql += " from clientes as cli "
        query_sql += " inner join pedidos as ped on (cli.id_cliente = ped.id_cliente) "
        query_sql += " inner join pedido_itens as pit on (ped.id_pedido = pit.id_pedido) "
        query_sql += " inner join produtos as pro on(pit.id_produto = pro.id_produto) "
        query_sql += f" where ped.id_pedido = {id_pedido} "
        query_sql += " order by ped.data_pedido "
        resultado = Database.consultar(query_sql)
        return Pedido.__to_object(resultado)

    def salvar_arquivo(self):
        try:
            data_pedido = date.today()
            query_insert_pedido = f"insert into pedidos (id_cliente, data_pedido) values ({self.cliente.id_cliente}, {data_pedido})"
            resultado_id_pedido = Database.executar(query_insert_pedido)

            for item in self.itens:
                data_pedido_item = date.today()
                query_insert_itens_pedido = f"insert into pedido_itens(id_pedido, id_produto, data_pedido_item) values ({str(resultado_id_pedido)}, {str(item.id_produto)}, {data_pedido_item})"
                Database.executar(query_insert_itens_pedido)

        except Exception as e:
            print(f"{Util.FAIL}Erro: {e}{Util.ENDC}")
            return False
        finally:
            return True


from __future__ import division, print_function, absolute_import
import argparse

parser = argparse.ArgumentParser(description='Example with non-optional arguments')

parser.add_argument('method', action="store")
parser.add_argument('dataDir', action="store")
parser.add_argument('baseImage', action="store")
parser.add_argument('entryPoint', action="store")
args = parser.parse_args()
method = args.method
dataDir = args.dataDir
baseImage = args.baseImage
entryPoint = args.entryPoint
print (parser.parse_args())
import os
import sys
import tensorflow as tf
import numpy as np

def nkTrain():
    # Genrating random linear data
    # There will be 50 data points ranging from 0 to 50
    x = np.linspace(0, 50, 50)
    y = np.linspace(0, 50, 50)

    # Adding noise to the random linear data
    x += np.random.uniform(-4, 4, 50)
    y += np.random.uniform(-4, 4, 50)

    n = len(x) # Number of data points

    X = tf.placeholder("float")
    Y = tf.placeholder("float")
    W = tf.Variable(np.random.randn(), name = "W")
    b = tf.Variable(np.random.randn(), name = "b")
    learning_rate = 0.01
    training_epochs = 1000

    # Hypothesis
    y_pred = tf.add(tf.multiply(X, W), b)

    # Mean Squared Error Cost Function
    cost = tf.reduce_sum(tf.pow(y_pred-Y, 2)) / (2 * n)

    # Gradient Descent Optimizer
    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

    # Global Variables Initializer
    init = tf.global_variables_initializer()

    sess = tf.Session()
    sess.run(init)

    # Iterating through all the epochs
    for epoch in range(training_epochs):

        # Feeding each data point into the optimizer using Feed Dictionary
        for (_x, _y) in zip(x, y):
            sess.run(optimizer, feed_dict = {X : _x, Y : _y})

        # Displaying the result after every 50 epochs
        if (epoch + 1) % 50 == 0:
            # Calculating the cost a every epoch
            c = sess.run(cost, feed_dict = {X : x, Y : y})
            print("Epoch", (epoch + 1), ": cost =", c, "W =", sess.run(W), "b =", sess.run(b))

    # Storing necessary values to be used outside the Session
    training_cost = sess.run(cost, feed_dict ={X: x, Y: y})
    weight = sess.run(W)
    bias = sess.run(b)

    print('Weight: ', weight, 'Bias: ', bias)
import random, string
import os
import logging
import subprocess
import importlib
from kubeflow import fairing
from kubeflow.fairing import TrainJob
from kubeflow.fairing.backends import KubeflowAWSBackend

AWS_REGION = 'us-west-2'
FAIRING_BACKEND = 'KubeflowAWSBackend'
AWS_ACCOUNT_ID = fairing.cloud.aws.guess_account_id()
BASE_DOCKER_IMAGE = baseImage
DOCKER_REGISTRY = '{}.dkr.ecr.{}.amazonaws.com'.format(AWS_ACCOUNT_ID, AWS_REGION)
#S3_BUCKET = f'{HASH}-kubeflow-pipeline-data'
S3_BUCKET = 'pjz16s-eks-ml-data'
ENTRY_POINT = entryPoint

#NOTEBOOK_BASE_DIR = fairing.notebook.notebook_util.get_notebook_name()

DATASET = dataDir
REQUIREMENTS = 'requirements.txt'

if FAIRING_BACKEND == 'KubeflowAWSBackend':
    from kubeflow.fairing.builders.cluster.s3_context import S3ContextSource
    BuildContext = S3ContextSource(
        aws_account=AWS_ACCOUNT_ID, region=AWS_REGION,
        bucket_name=S3_BUCKET
    )

BackendClass = getattr(importlib.import_module('kubeflow.fairing.backends'), FAIRING_BACKEND)

print("About to train job setup...")

from kubeflow.fairing import TrainJob
#train_job = TrainJob(str(ENTRY_POINT), input_files=[DATASET,REQUIREMENTS],
#                     base_docker_image=BASE_DOCKER_IMAGE,
#                     docker_registry=DOCKER_REGISTRY,
#                     backend=BackendClass(build_context_source=BuildContext))

print('ENTRY POINT for building image is :'+ ENTRY_POINT)

if ENTRY_POINT == "nkTrain" :
    print('Now in FUNCTION  or CLASS entrypoint. Entry point is set as '+ENTRY_POINT)
    train_job = TrainJob(nkTrain, input_files=[DATASET,REQUIREMENTS],
                     base_docker_image=BASE_DOCKER_IMAGE,
                     docker_registry=DOCKER_REGISTRY,
                     backend=BackendClass(build_context_source=BuildContext))
else :
    print('Now in NON function or class entrypoint. Entry point is set as '+ENTRY_POINT)
    train_job = TrainJob(str(ENTRY_POINT), input_files=[DATASET,REQUIREMENTS],
                     base_docker_image=BASE_DOCKER_IMAGE,
                     docker_registry=DOCKER_REGISTRY,
                     backend=BackendClass(build_context_source=BuildContext))

print("about to submit job")

outJob = train_job.submit()

print ("Job to create image  is :  " + str(outJob))
print("Successfully created Docker Image in the configured registry "+ DOCKER_REGISTRY)


from __future__ import print_function
import httplib2
import os

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/sheets.googleapis.com-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Fantasy College Football'

def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'sheets.googleapis.com-python-quickstart.json')

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

def main():
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?'
                    'version=v4')
    service = discovery.build('sheets', 'v4', http=http,
                              discoveryServiceUrl=discoveryUrl)

    spreadsheetId = '1zSEYZLVuCdJilT0Oo_wy3QO7WlXGJoX7cMez64e19W8'
    #rangeName = 'Draft!A2:A8'
    rangeName = 'Team - John B!A3:E'
    result = service.spreadsheets().values().get(
        spreadsheetId=spreadsheetId, range=rangeName).execute()
    values = result.get('values', [])

    if not values:
        print('No data found.')
    else:
        print('Player, Position, Eligible?:')
        for row in values:
            # Print columns A and E, which correspond to indices 0 and 4.
            if len(row) >= 5:
                print('%s, %s, %s' % (row[1], row[0], row[4]))

    value_input_option = 'RAW'
    range_name = 'Draft!C1'
    values = [
        [
            # Cell values ...
            'test'
        ],
        # Additional rows ...
    ]
    body = {
      'values': values
    }
    result = service.spreadsheets().values().update(
        spreadsheetId=spreadsheetId, range=range_name,
        valueInputOption=value_input_option, body=body).execute()

if __name__ == '__main__':
    main()


import ast
import pdb
import pprint
import logging
logger = logging.getLogger(__name__)

SETUP_FUNCTION_NAMES = [
    'setup',
    # if setuptools.setup() is the function call. occurs in
    # https://github.com/jackmaney/python-stdlib-list
    'setuptools',
]

def parse(path_to_setuppy):
    """Helper function to gather information from setup.py

    Parameters
    ----------
    path_to_setuppy : list
        Path to setup.py file.
        e.g. '/home/edill/dev/conda/conda-skeletor/setup.py'

    Returns
    -------
    dict
        Dictionary keyed on the kwargs that get passed to the setup() function
        inside of setup.py
    """
    with open(path_to_setuppy, 'r') as f:
        code = f.read()
    # pdb.set_trace()
    tree = ast.parse(code)
    scraper = SetupScraper()
    logger.debug("\nScraping setup.py\n"
                   "-----------------")
    # try:
    scraper.visit(tree)
    # except IndexError as ie:
    #     pdb.set_trace()
    #     logger.debug('setup.py scaping failed -- %s' % ie)
    #     raise
    # info = {v[0]: v[1:] if len(v) == 2 else v[1]
    #         for v in scraper.setup_info}
    info = {}
    for line in scraper.setup_info:
        value = line[1:]
        if len(line) == 2:
            value = line[1]
        info[line[0]] = value

    # TODO correctly parse the entry_points argument
    # this will break as soon as the setup.py does not look like:
    # entry_points={
    #     'console_scripts': [
    #         'replay = replay.replay:main']},
    entry_points = info.get('entry_points')
    if entry_points:
        info['entry_points'] = {entry_points[0]: entry_points[1]}
    return info

class SetupScraper(ast.NodeVisitor):
    def __init__(self):
        self.in_setup = False
        self.setup_info = []

    def visit_keyword(self, node):
        if self.in_setup:
            logger.debug('Adding kwarg=%s to setup info' % node.arg)
            self.setup_info.append([node.arg])
        self.visit(node.value)

    def visit_Call(self, node):
        if self.in_setup:
            self.setup_info[-1].append(node)
        if ((isinstance(node.func, ast.Name) and node.func.id == 'setup') or
            (isinstance(node.func, ast.Attribute) and node.func.attr == 'setup')):
            logger.debug('entering setup function')
            self.in_setup = True
        logger.debug(pprint.pformat(vars(node)))
        self.generic_visit(node)
        if ((isinstance(node.func, ast.Name) and node.func.id == 'setup') or
            (isinstance(node.func, ast.Attribute) and node.func.attr == 'setup')):
            logger.debug('leaving setup function')
            self.in_setup = False

    def visit_Name(self, node):
        logger.debug('node info = %s' % vars(node))
        if node.id in SETUP_FUNCTION_NAMES:
            logger.debug('in setup function, skipping the name visitor for '
                         'node.id=%s' % node.id)
            return
        if self.in_setup:
            self.setup_info[-1].append(node.id)

    def visit_Str(self, node):
        if self.in_setup:
            self.setup_info[-1].append(node.s)

    def generic_visit(self, node):
        """Called if no explicit visitor function exists for a node.

        Overridden from the ast.NodeVisitor base class so that I can add some
        local state to keep track of whether or not my node visitor is inside
        a try/except block.  When a try block is encountered, the node is added
        to the `trys` instance attribute and then the try block is recursed in
        to.  Once the recursion has exited, the node is removed from the `trys`
        instance attribute
        """
        for field, value in ast.iter_fields(node):
            if isinstance(value, list):
                for item in value:
                    if isinstance(item, ast.AST):
                        self.visit(item)
            elif isinstance(value, ast.AST):
                self.visit(value)


from operator import truediv
from tkinter import FIRST
from selenium import webdriver

import time

class CookieClicker:

    def __init__(self):
       self.SITE_LINK = "https://orteil.dashnet.org/cookieclicker/"
       self.SITE_MAP = {
           "buttons": {
               "biscoito":{
                   "xpath": "/html/body/div/div[2]/div[15]/div[8]/button"
               },
               "upgrade":{
                   "xpath": "/html/body/div/div[2]/div[19]/div[3]/div[6]/div[$$number$$]"
               },
               "pt":{
                   "xpath": "/html/body/div[2]/div[2]/div[2]/div/div[11]"
               },
               "clique_em_go_it":{
                   "xpath": "/html/body/div[1]/div/a[1]"
               },
               "label_qtd_cookies":{
                   "xpath": "/html/body/div/div[2]/div[15]/div[4]"
               }
           }
       }

       self.driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
       self.driver.maximize_window()

    def abrir_site(self):
           time.sleep(2)
           self.driver.get(self.SITE_LINK)
           time.sleep(10)

    def clicar_em_pt(self):
           self.driver.find_element_by_xpath(self.SITE_MAP["buttons"]["pt"]["xpath"]).click()

    def clique_em_go_it(self):
           self.driver.find_element_by_xpath(self.SITE_MAP["buttons"]["clique_em_go_it"]["xpath"]).click()

    def clicar_no_cookei(self):
        self.driver.find_element_by_xpath(self.SITE_MAP["buttons"]["biscoito"]["xpath"]).click()

    def pega_melhor_upgrade(self):
            encontrei = False

            elemento_atual = 2

            while not encontrei:
                objeto = self.SITE_MAP["buttons"]["upgrade"]["xpath"].replace("$$number$$" , str(elemento_atual))
                classes_objeto = self.driver.find_element_by_xpath(objeto).get_attribute("class")

                if not "enabled" in classes_objeto:
                    encontrei: True
                    break
                else:
                    elemento_atual += 1

            return  elemento_atual - 1

    def comprar_upgrade(self):
           objeto = self.SITE_MAP["buttons"]["upgrade"]["xpath"].replace("$$number$$", str(self.pega_melhor_upgrade()))
           self.driver.find_element_by_xpath(objeto).click()

    def recuperarValorDeCookies(self):
            valor_do_campo_cookie = self.driver.find_element_by_xpath(self.SITE_MAP["buttons"]["label_qtd_cookies"]["xpath"]).get_attribute("innerHTML")
            return valor_do_campo_cookie

biscoito = CookieClicker()
biscoito.abrir_site()
biscoito.clicar_em_pt()
biscoito.clique_em_go_it()

i = 0
time.sleep(3)
while True:
    if i % 100 == 0 and i != 0:
        time.sleep(1)
        qtd_cookies = biscoito.recuperarValorDeCookies()
        print("o valor de I é ", i)
        print("quantidade de cookies quando cliquei é ", qtd_cookies)
        biscoito.comprar_upgrade()
        time.sleep(1)
    biscoito.clicar_no_cookei()
    i += 1


from flask import Flask, request
import time
import os
import requests
import base64
from PIL import Image
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

daprPort = os.getenv('DAPR_HTTP_PORT')
connection_string = os.getenv("AZURE_STORAGE_CONNECTION_STRING")
app = Flask(__name__)
@app.route("/processmessage", methods=['POST'])
def incoming():
    #print(request.data.decode('UTF-8'), flush=True)
    file = request.data.decode('UTF-8')

    thumbnail_file = resize(file)
    upload_file(thumbnail_file)
    return "Message processed"

def resize(file):

    local_path = "./images"

    blob_service_client = BlobServiceClient.from_connection_string(connection_string)

    print(f"\n Fetching image to resize from blob container images {file} ", flush=True)

    container_client = blob_service_client.get_container_client(container="images")
    download_file_path = os.path.join(local_path,file)

    with open(file=download_file_path, mode="wb") as download_file:
        download_file.write(container_client.download_blob(file).readall())

    #Upload the created file
    with Image.open(download_file_path) as image:
        image.load()

    print(image.size, flush=True)
    image.thumbnail((90,90))
    thumbnail_file = os.path.join(local_path,f"thunb-{file}")
    image.save(thumbnail_file)

    return thumbnail_file

def upload_file(file):
    # Upload the thumbnail file
    print(file, flush=True)

    ## read as binary
    with open(file=file, mode="rb") as filedata:
        binary_data = filedata.read()

    url = f'http://localhost:{daprPort}/v1.0/bindings/azureblob'
    # encode into base64 bytes
    base64_encoded_data = base64.b64encode(binary_data)
    # decode into base64 string
    base64_message = base64_encoded_data.decode('utf-8')
    uploadcontents = '{ "operation": "create", "data": "'+ base64_message+ '", "metadata": { "blobName": "'+ file+'" } }'
    requests.post(url, data = uploadcontents)
    print("uploaded file", flush=True)


#Esse script ultiliza a versão 3.6 do python e todas as bibliotecas em suas ultimas versões
import tkinter as tk
from tkinter import *
from tkinter import filedialog
from PIL import Image,ImageTk
import tabula
import PyPDF2
import pandas as pd
import openpyxl
from manipulacao_csv import manipulacao
from tkinter import messagebox

def selecionar_arquivo():
    arquivo_pdf = filedialog.askopenfilename(filetypes=[("Arquivos PDF","*.pdf")])
    tabela = tabula.read_pdf(arquivo_pdf)
    pdf = open(arquivo_pdf,"rb")
    conteudo_pdf = PyPDF2.PdfReader(pdf)
    n_pagina = len(conteudo_pdf.pages)

    if tabela: #essa parte do programa ta ok, façta adicionar para a função ler como texto
        listas_tabela= tabula.read_pdf(arquivo_pdf,pages="all",multiple_tables=True)
        for tabela in listas_tabela:
            tabela=tabela.drop("Unnamed: 0",axis=1)
            tabela=tabela.drop("Unnamed: 1",axis=1)
            tabela=tabela.drop("Unnamed: 2",axis=1)
            tabela=tabela.drop("Unnamed: 3",axis=1)
        tabela.to_excel("extractionPDF.xlsx")
        messagebox.showinfo('[AVISO]','Já terminei de extrair os dados e salvei o arquivo extractionPDF na pasta do programa.')

    elif n_pagina == 1:
        pageObj = conteudo_pdf.pages[0]
        pageObj.extract_text()
        texto_pagina=pageObj.extract_text()
        lista_trans=texto_pagina.split("\n")
        df=pd.DataFrame(zip(lista_trans))
        df.to_csv("extractionPDF.csv")
        manipulacao()

    elif n_pagina > 1:
        texto_pagina = []
        for i in range(n_pagina):
            pageObj = conteudo_pdf.pages[i]
            pageObj.extract_text()
            texto_pagina.append(pageObj.extract_text())
        result_3=[item.split("\n") for item in texto_pagina]
        result_3_flat=[item for l in result_3 for item in l]
        df=pd.DataFrame(zip(result_3_flat))
        df.to_csv("extractionPDF.csv")
        manipulacao()

vermelho_flamengo = "#bf2512"
janela_1 = tk.Tk()
janela_1.geometry("700x500")
janela_1.config(bg="black")
janela_1.iconphoto(False, PhotoImage(file="flamengo.png"))
janela_1.title("Extrator de pdf")
h1 = tk.Label (width=50,height=2,text="Olá Daniel Araya, selecione o pdf que deseja extrair os dados",font="arial 15 bold",fg=vermelho_flamengo, bg="black")
h1.grid(row=1,column=0)
h1.pack()
btn_selecionar_arquivo = tk.Button (text="Selecionar Arquivo pdf", bg=vermelho_flamengo, fg="black",font="arial",command=selecionar_arquivo)
btn_selecionar_arquivo.pack()
imagem_escudo = "escudo_flamengo.jpg"
image = Image.open(imagem_escudo) #abrindo a imagem
photo = ImageTk.PhotoImage(image)
escudo = tk.Label (janela_1,image=photo)
escudo.pack()
janela_1.resizable(width=False, height=False)
janela_1.mainloop()


# -*- coding: utf-8 -*-
"""
Created on Thu Aug 16 23:58:16 2018

@author: Elliott
"""
import numpy as np
import cv2 as cv
class Feature:
    def __init__(self,pic_info_path,pic_path,feature_path):
        self.pic_info_path =pic_info_path
        self.pic_path =pic_path
        self.feature_path=feature_path

    #每行 列白点数
    def Feature1Extraction1(self):
        data =np.loadtxt(self.pic_info_path,dtype =str,skiprows=1,delimiter='\t')
        index =data[:,0]
        datacategory =data[:,1]
        pic =data[:,2]
        length = pic.shape[0]
        filepath1 = self.feature_path+'feature1.txt'
        file =open(filepath1,'w+')
        for i in range(length):
            image =cv.imread(self.pic_path+pic[i],0)
            pic_length ,pic_width=image.shape  #92 47
            featurelist1=[]
            for lines in range(pic_length):
                count =0
                for columns in range(pic_width):
                    value  =image[lines,columns]
                    if (value==0):
                            count +=1
                file.writelines(str(count/pic_width))
                file.writelines('\t')
            for lines in range(pic_width):
                count = 0
                for columns in range(pic_width):
                    value =image[lines,columns]
                    if (value==0):
                            count +=1
                file.writelines(str(count/pic_width))
                file.writelines('\t')
            file.writelines('\n')
        file.close()

    #区域密度
    def Feature1Extraction2(self,kernel_size):
        data =np.loadtxt(self.pic_info_path,dtype=str,skiprows=1,delimiter='\t')
        index  =data[:,0]
        datacategory =data[:,1]
        pic =data[:,2]
        legth =pic.shape[0]
        #image test
        #filepath_test =self.feature_path+'featuretest.txt'
        #filetest =open(filepath_test,'w+')
        #image =cv.imread(self.pic_path +pic[0],0)
        #pic_length ,pic_width=image.shape  #92 47
        #for i in range(pic_length):
        #    for j in range(pic_width):
        #        filetest.writelines(str(image[i,j]))
        #        filetest.writelines('\t')
        #    filetest.writelines('\n')
        #filetest.close()

        filepath2 = self.feature_path+'feature2.txt'
        file =open(filepath2,'w+')
        for i in range(legth):
            image =cv.imread(self.pic_path+pic[i],0)
            pic_length ,pic_width=image.shape  #92 47
            kernel_line_size = int(pic_length/kernel_size)-1
            kernel_col_size = int(pic_width/kernel_size)-1
            for lines in range(kernel_line_size):
                for columns in range(kernel_col_size):
                    total =0
                    for parameter_line in range(kernel_size):
                        for parameter_column in range(kernel_size):
                            total += image[lines*kernel_size+parameter_line,columns*kernel_size+parameter_column]
                    file.writelines(str(total))
                    file.writelines('\t')
            file.writelines('\n')
        file.close()

    #图像区域取最大值代替原区域，模糊处理
    def Feature1Extraction3(self,kernel_size):
        data =np.loadtxt(self.pic_info_path,dtype=str,skiprows=1,delimiter='\t')
        index  =data[:,0]
        datacategory =data[:,1]
        pic =data[:,2]
        legth =pic.shape[0]

        filepath3 = self.feature_path+'feature3.txt'
        file =open(filepath3,'w+')
        for i in range(legth):
            image =cv.imread(self.pic_path+pic[i],0)
            pic_length ,pic_width=image.shape  #92 47
            kernel_line_size = int(pic_length/kernel_size)-1
            kernel_col_size = int(pic_width/kernel_size)-1
            for lines in range(kernel_line_size):
                for columns in range(kernel_col_size):
                    maxnum =0
                    for parameter_line in range(kernel_size):
                        for parameter_column in range(kernel_size):
                            if maxnum<image[lines*kernel_size+parameter_line,columns*kernel_size+parameter_column]:
                                maxnum =image[lines*kernel_size+parameter_line,columns*kernel_size+parameter_column]
                    file.writelines(str(maxnum))
                    file.writelines('\t')
            file.writelines('\n')
        file.close()

    def Feature1Extraction4(self):
        pass

    def Feature1Extraction5(self):
        pass

    def Feature1Extraction6(self):
        pass


import copy

import cv2
from matplotlib import pyplot as plt, font_manager, rcParams

N = 9

def correction(arr):
    for row in range(9):
        temp = []
        for col in range(9):
            if arr[row][col] not in temp:
                temp.append(arr[row][col])
            else:
                grid[row][col] = 0

    for col in range(9):
        temp = []
        for row in range(9):
            if arr[row][col] not in temp:
                temp.append(arr[row][col])
            else:
                grid[row][col] = 0

def printing(arr):
    for i in range(N):
        for j in range(N):
            print(arr[i][j], end=" ")
        print()

def isSafe(grid, row, col, num):
    for x in range(9):
        if grid[row][x] == num:
            return False

    for x in range(9):
        if grid[x][col] == num:
            return False

    startRow = row - row % 3
    startCol = col - col % 3

    for i in range(3):
        for j in range(3):
            if grid[i + startRow][j + startCol] == num:
                return False
    return True

def solveSuduko(grid, row, col):
    if row == N - 1 and col == N:
        return True

    if col == N:
        row += 1
        col = 0

    if grid[row][col] > 0:
        return solveSuduko(grid, row, col + 1)

    for num in range(1, N + 1, 1):
        if isSafe(grid, row, col, num):
            grid[row][col] = num

            if solveSuduko(grid, row, col + 1):
                return True
        grid[row][col] = 0
    return False

grid = []
with open("grid.txt", "r") as text_file:
    temp = []
    for i in range(0, 9):
        line = text_file.readline()
        for ele in line[:-1]:
            temp.append(int(ele))
        grid.append(temp)
        temp = []
_grid = copy.deepcopy(grid)
correction(grid)
#print(grid)
if (solveSuduko(grid, 0, 0)):
    printing(grid)
else:
    print("Reupload Image")

image_orig = cv2.imread('output_grid.PNG')
image = copy.copy(image_orig)
font = cv2.FONT_HERSHEY_SIMPLEX

color_green = (0, 107, 56) #006B38FF
color_black = (16,24,32) #101820FF
x = 60
y = 60
org = (60, 60)
fontScale = 1
thickness = 2

#print(_grid)

for i in range(0, 9):
    for j in range(0,9):
        org = (x, y)
        if _grid[j][i] > 0:
            image = cv2.putText(image, str(grid[j][i]), org, font, fontScale, color_black, thickness, cv2.LINE_AA)
        else:
            image = cv2.putText(image, str(grid[j][i]), org, font, fontScale, color_green, thickness, cv2.LINE_AA)
        y += 75
    x += 75
    y = 60

cv2.imwrite("output_sudoku" + ".jpg", image)

#plt.imshow(image)
#plt.show()


import numpy as np
import matplotlib.pyplot as plt

from activelabeling.auxillary import target_func
from activelabeling.kernel import Kernel

plt.rcParams['text.usetex'] = True
plt.rcParams["font.family"] = "serif"
plt.rcParams["font.serif"] = "Times"

np.random.seed(100)

def solution(x_train, kernel, gamma=.3, verbose=False):

    n_train = x_train.shape[0]
    kernel.set_support(x_train)
    K = kernel(x_train)
    alpha = np.zeros(n_train, float)

    if isinstance(gamma, float):
        gamma = np.full(n_train, gamma, float)

    if verbose:
        history = np.zeros((n_train, 2), float)

    for i in range(n_train):
        z = K[i] @ alpha
        epsilon = np.sign(z - target_func(x_train[i,0]))
        alpha[i] -= epsilon * gamma[i]
        if verbose:
            history[i, 0] = epsilon
            history[i, 1] = z

    return alpha, history

n_train = 30
x_train = np.random.rand(n_train)[:, np.newaxis]

sigma = .2
kernel = Kernel('gaussian', sigma=sigma)

x_test = np.linspace(0, 1, 50)
y_test = target_func(x_test)

alpha, history = solution(x_train, kernel, verbose=True)
k_test = kernel(x_test[:, np.newaxis]).T

fig, ax = plt.subplots(1,1, figsize=(2.5, 1.75))
ind = history[:, 0] == 1
a = ax.scatter(x_train[ind, 0], history[ind, 1], s=10)
ind = history[:, 0] == -1
b = ax.scatter(x_train[ind, 0], history[ind, 1], s=10)
c, = ax.plot(x_test, y_test, linestyle='--')
y_pred = k_test @ alpha
d, = ax.plot(x_test, y_pred, alpha=.7)
ax.legend([c,d], ['Signal', 'Reconstruction'], prop={"size": 8})
ax.set_ylim(-2.5, 2.5)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title(r'Active strategy', size=10)
ax.set_xlabel(r'Inputs',size=8)
ax.set_ylabel(r'Outputs', size=8)
fig.tight_layout()
fig.savefig('one_d_active.pdf')

epsilon_bis = np.zeros((n_train, 2), float)
epsilon_bis[:, 1] = np.random.randn(n_train)

alpha_bis = np.zeros(n_train, float)
gamma_0 = .7
gamma = np.full(n_train, gamma_0, float)
K = kernel(x_train)
for i in range(n_train):
    epsilon_bis[i, 0] = np.sign(epsilon_bis[i, 1] - target_func(x_train[i,0]))
    err = np.sign(epsilon_bis[i, 1] - K[i] @ alpha_bis)
    if err != epsilon_bis[i, 0]:
        alpha_bis[i] -= epsilon_bis[i, 0] * gamma[i]

fig, ax = plt.subplots(1,1, figsize=(2.5, 1.75))
ind = epsilon_bis[:, 0] == 1
a = ax.scatter(x_train[ind, 0], epsilon_bis[ind, 1], s=10)
ind = epsilon_bis[:, 0] == -1
b = ax.scatter(x_train[ind, 0], epsilon_bis[ind, 1], s=10)
c, = ax.plot(x_test, y_test, linestyle='--')
y_pred_bis = k_test @ alpha_bis
d, = ax.plot(x_test, y_pred_bis, alpha=.7)
ax.legend([a,b], ['Above', 'Below'], prop={"size": 8})
ax.set_ylim(-2.5, 2.5)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title('Passive strategy', size=10)
ax.set_xlabel(r'Inputs',size=8)
ax.set_ylabel(r'Outputs', size=8)
fig.tight_layout()
fig.savefig('one_d_passive.pdf')


import os
from pathlib import Path
from dotenv import dotenv_values

BASE_DIR = Path(__file__).resolve().parent.parent

ENV_PATH = BASE_DIR / ".env"
BOT_TOKEN = dotenv_values(ENV_PATH)['TOKEN_TG']
admins = [
    1260426275
]

ip = dotenv_values(ENV_PATH)["IP"]

aiogram_redis = {
    'host': '188.72.209.127',
}

redis = {
    'address': ('188.72.209.127', 6379),
    'encoding': 'utf8'
}


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 15 15:07:50 2018

@author: ajdavis
"""

import json
import urllib.request
import pandas as pd
import requests
from bs4 import BeautifulSoup
import re
import seaborn as sns
import statsmodels.api as sm
import matplotlib.pyplot as plt
from statsmodels.graphics.gofplots import ProbPlot
import patsy
from diagnostic_plots import diagnostic_plots
import numpy as np
import geocoder
import itertools
import seaborn as sns
from sklearn.linear_model import LinearRegression
from sklearn import metrics
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.cross_validation import train_test_split
from sklearn.cross_validation import KFold
from sklearn.cross_validation import cross_val_score
from sklearn.model_selection import learning_curve

import warnings
warnings.filterwarnings('ignore')

## Helper functions
def trans_price(row):
    if row['two_grams'] != 0:
        return row['two_grams']/2
    elif row['eighth'] !=0:
        return row['eighth']/3.5
    elif row['quarter'] !=0:
        return row['quarter']/7
    elif row['half_ounce'] !=0:
        return row['half_ounce']/14
    elif row['ounce'] !=0:
        return row['half_ounce']/28
    else:
        return row['gram']

def get_gcs(list_names):
    gcs = []
    for name in list_names:
        g = geocoder.google(name)
        gc = g.latlng
        gcs.append(gc)
        gcs = list(map(str, gcs))
        gcs = [gc.replace('[', '') for gc in gcs]
        gcs = [gc.replace(']', '') for gc in gcs]
        gcs = [gc.replace(', ', ',') for gc in gcs]
        gcs = [i for i in gcs if i != 'None']
    return gcs

def get_disp_names(gc):
    url = 'https://api-g.weedmaps.com/wm/v2/location?include%5B%5D=regions.listings&latlng={}'.format(gc)
    req = urllib.request.urlopen(url)
    data = json.loads(req.read().decode('utf-8'))
    lst = data['data']['regions']['dispensary']['listings']
    disp_df = pd.DataFrame()
    for d in lst:
        df = pd.DataFrame.from_dict(d)
        disp_df= disp_df.append(df)
    return list(disp_df['slug'].unique())

def menu_to_df(disp_names):
    url = 'https://api-g.weedmaps.com/wm/web/v1/listings/{}/menu?type=dispensary'.format(disp_names)
    req = urllib.request.urlopen(url)
    data = json.loads(req.read().decode('utf-8'))
    try:
        m_dict = { k:[d[k] for d in data['categories']] for k in data['categories'][0] }
    except:
        print("Error")
    weed_df = pd.DataFrame()
    try:
        for d in m_dict['items']:
            df = pd.DataFrame.from_dict(d)
            weed_df = weed_df.append(df)
            weed_df = weed_df[['prices', 'body', 'license_type', 'category_name']]
    except:
        print("Error")
    return weed_df

def munge_df(df):
    df[['THC','CBD']] = df['body'].str.split('THC', 1, expand = True)
    df['CBD'] = pd.to_numeric(df['CBD'].str.split('%').str[0].fillna(0), errors = 'coerce')
    df['THC'] = pd.to_numeric(df['THC'].str.split('%').str[0].fillna(0), errors = 'coerce')
    df['prices'] = df['prices'].astype(str)
    df["prices"] = df["prices"].apply(lambda x : dict(eval(x)) )
    tmp = df["prices"].apply(pd.Series )
    df = pd.concat([df, tmp], axis=1)
    df['price'] = df.apply(trans_price, axis=1)
    df = df[['price', 'THC', 'CBD', 'license_type', 'category_name']]
    df['ln_price'] = np.log(df['price'] + .000000001)
    df['sqrt_price'] = np.sqrt(df['price'])
    df = df[(df != 0).all(1)]
    df = df.dropna()
    df = pd.get_dummies(df)
    return df


import pandas as pd
from math import *
from random import randint
from itertools import permutations
from pandas import *
from time import *
"""
Module à import : math et random
Le programme prend trois fonctions, une fonction angramme, une fonction maths, une fonction exe
La fonction anagramme prend en argument un chaine de caractère, et trouve le mot correct en y incluant une base de donnée.
"""
def verification(c):
    c=c.lower()
    listing=pd.read_csv("Lexique383.csv")
    searching=listing[(listing["1_ortho"] == c)]
    if len(searching)>0:
        return True
    return False

def verification_txt(c):
    c=c.lower()
    fichier=open("pli07.txt","r")
    l=[]
    for i in fichier:
        i=i.replace('\n',"")
        i=i.lower()
        l.append(i)
    if c in l:
        return True
    return False

def search_word():
    listing=pd.read_csv("Lexique383.csv")
    index=len(listing["1_ortho"])
    word_rand=randint(0,index+1)
    word=listing["1_ortho"][word_rand]
    return word

def search_word_txt():
    fichier=open("pli07.txt","r")
    l=[]
    for i in fichier:
        i=i.replace('\n',"")
        i=i.lower()
        l.append(i)
    word_rand=randint(0,len(l)+1)
    word=l[word_rand]
    if len(word)>10:
        search_word_txt()
    return word

def math():
    #lvl=int(input("Niveau de difficulté des calcul entre 1 et 10"))
    a=randint(0,10)
    b=randint(0,10)
    return a,b

def arrangement(c):
    """Prend en argument une chaine de caractère et retourne le nombre d'arangement possible de ce mot"""
    factorielle = lambda n: n * factorielle(n-1) if n != 0 else 1
    return factorielle(len(c))

#print(arrangement("bonjour"))

def anagramme(c):
    debut=time()
    """Prend en argument une chaine de caractère et retourne le mot existant dans le bon ordre."""
    c=c.lower()
    print(f"Le nombre de combinaison possible du mot est : {arrangement(c)}")
    print(f"Le mot à {len(c)} lettres")
    a=[''.join(p) for p in permutations(c)]
    ana_rand=randint(1,len(a))
    #print(f"ana_rang : {ana_rand}")
    anag=a[ana_rand-1]

    if verification_txt(c)==False:
        print("Le mot est absent de la base de donnée!")
        return None
    print("Le mot est présent dans la base de donnée !")

    i=0
    mem=[] #Memoire des lettres trouvés
    while i!=len(c):
        x,y=math()
        res=int(input(f"Veuillez rentrer de resultat de {x}x{y} :"))
        if res==x*y:
            mem.append(anag[i])
            print(f"Lettre trouvé : {anag[i]}")
            i+=1
        else:
            print(f"Perdu, le résultat était {x*y}")
            return None
    print("Bravo voici les lettres trouvées :",end=" ")
    for i in range(len(anag)):
        print(anag[i],end="")
    print("")
    mot=input("Quel est le mot mystere ? : ")

    if mot==c:
        print("Vous avez gagné ! Félicitation !")
        return None
    print(f"Dommage vous avez perdu, le mot était {c}")
    fin=time()
    print(f"Essai effectué en {floor(fin-debut)} secondes")

anagramme(search_word_txt())


from typing import List
class MergeTwoSortedArray:
    def __init__(self,array1,array2) -> None:
        self.array1 = array1
        self.array2 = array2
        self.m=m
        self.n=n

    def merge(self):
        mergedArray = []
        if len(self.array1) == 0:
            return self.array2
        if len(self.array2) == 0:
            return self.array1
        i = 0
        j = 0
        while i < len(self.array1) and j< len(self.array2):
            if self.array1[i] <= self.array2[j]:
                mergedArray.append(self.array1[i])
                i+=1
            elif self.array2[j] <= self.array1[i]:
                mergedArray.append(self.array2[j])
                j+=1
        return mergedArray+self.array1[i:]+self.array2[j:]

def mergeinplace(nums1: List[int], m: int, nums2: List[int], n: int) -> None:
    """
    Do not return anything, modify nums1 in-place instead.
    """
        # Initialize nums1's index
    i = m - 1
    # Initialize nums2's index
    j = n - 1
    # Initialize a variable k to store the last index of the 1st array...
    k = m + n - 1
    while j >= 0:
        if i >= 0 and nums1[i] > nums2[j]:
            nums1[k] = nums1[i]
            k -= 1
            i -= 1
        else:
            nums1[k] = nums2[j]
            k -= 1
            j -= 1

if __name__=="__main__":
    array1 = [1,3,4,6,20]
    array2 = [2,3,4,5,6,9,11,76]
    mtsa = MergeTwoSortedArray(array1,array2)
    print(mtsa.merge())

    nums1 = [1,2,3,0,0,0]
    nums2 = [2,5,6]
    m = 3
    n = 3
    mergeinplace(nums1, m, nums2, n)
    nums1 = [1]
    m = 1
    nums2 = []
    n = 0
    mergeinplace(nums1, m, nums2, n)


# Django
from django.utils.html import format_html
from django.contrib import messages

# Models
from community.apps.posts.models.index import Post

# Bases
from community.bases.admin import Admin
from django.contrib import admin

@admin.register(Post)
class PostAdmin(Admin):
    list_display = ('thumbnail_media_tag', 'user', 'title', 'community',
                    'visit_count', 'comment_count', 'like_count',
                    'is_active', 'web_url')
    search_fields = ('user__username', 'title', 'community__title')
    list_filter = ('community',)

    fieldsets = (
        ("1. 정보", {"fields": ('community', 'board', 'title', 'content', 'web_url')}),
        ("2. 이미지", {"fields": ('thumbnail_media_tag', 'thumbnail_media_url')}),
        ("3. 유저", {"fields": ('user', 'profile')}),
        ("4. 통계", {"fields": ('point', 'like_count', 'dislike_count', 'comment_count', 'bookmark_count', 'visit_count',
                              'reported_count', 'share_count')}),
        ("5. 활성화 유무", {"fields": ('is_active',)}),
        ('6. 생성일', {'fields': ('created', 'achieved_20_points_at')}),
    )

    readonly_fields = ('achieved_20_points_at', 'thumbnail_media_tag', 'thumbnail_media_url',
                       'like_count', 'dislike_count', 'comment_count', 'bookmark_count', 'visit_count', 'reported_count',
                       'share_count', 'web_url')
    summernote_fields = ('content',)
    # inline_actions = ['create_post_like', 'create_post_dislike']

    def get_readonly_fields(self, request, obj=None):
        return self.readonly_fields

    def thumbnail_media_tag(self, obj):
        if obj.thumbnail_media_url:
            return format_html('<img src="{}" width="100px;"/>'.format(obj.thumbnail_media_url))

    thumbnail_media_tag.short_description = '썸네일'

    # 어드민 사이트에서 유저 필드 넣는 함수
    def save_model(self, request, obj, form, change):
        if not obj.user:
            obj.user = request.user
        super(PostAdmin, self).save_model(request, obj, form, change)

    # def create_post_like(self, request, obj, parent_obj=None):
    #     obj.create_post_like()
    #     messages.success(request, '포스트 좋아요가 생성됐습니다.')
    #
    # def create_post_dislike(self, request, obj, parent_obj=None):
    #     obj.create_post_dislike()
    #     messages.success(request, '포스트 싫어요가 생성됐습니다.')


import requests
import json
import streamlit as st

st.title(" ☕☕ List of coffees ☕☕ ")
type_of_coffee = st.selectbox("type of coffee?",["hot","iced"])
filtertitle = st.text_input("filter?")

if type_of_coffee:
    url = "https://api.sampleapis.com/coffee/" + type_of_coffee + "/?title_like=" + filtertitle
    st.text(f"Fetching: {url} ")
    response = requests.get(url)
    coffees =  json.loads(response.content)
    #st.table(coffees)
    for coffee in coffees:
        st.text(coffee["title"])
        st.image(coffee["image"])


from aiogram.types import ReplyKeyboardMarkup, KeyboardButton

maslahat_olish = ReplyKeyboardMarkup(keyboard=[
    [KeyboardButton(text="✍ Maslahat olish")],
    [KeyboardButton("🔙 Orqaga qaytish")]
], resize_keyboard=True)

phone = KeyboardButton(text="📞 Telefon raqam yuborish", request_contact=True)
back_button = KeyboardButton(text="🔙 Orqaga qaytish")
back_main_page = KeyboardButton("🏘 Bosh menyu")

advice_phone = ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
advice_phone.add(phone)
advice_phone.add(back_main_page)


from nose.plugins.attrib import attr
from w3af.plugins.tests.helper import PluginTest, PluginConfig

class TestFingerGoogle(PluginTest):

    base_url = 'http://www.w3af.org/'

    _run_configs = {
        'cfg': {
            'target': base_url,
            'plugins': {'infrastructure': (PluginConfig('finger_google'),)}
        }
    }

    @attr('ci_fails')
    def test_fuzzer_user(self):
        cfg = self._run_configs['cfg']
        self._scan(cfg['target'], cfg['plugins'])

        emails = self.kb.get('emails', 'emails')

        self.assertEqual(len(emails), 3, emails)

        users = [em['user'] for em in emails]
        self.assertNotIn('x3d', users)


import numpy as np
from delira.data_loading.dataset import AbstractDataset, DictDataset, \
    IterableDataset
from collections import Iterable, defaultdict

class DataLoader:
    """
    Basic Dataloader class, that returns data for a given set of indices and
    combines it as batches
    """

    def __init__(self, data):
        """
        Parameters
        ----------
        data : Any
            the data to use; Ideally this either is a dataset, an iterable or
            a dict, but in general, this must only be indexable, have a length
            and return a dict of arrays if indexed
        """
        self._process_id = None
        if isinstance(data, AbstractDataset):
            dataset = data

        else:
            # wrap it into dataset depending on datatype
            if isinstance(data, dict):
                dataset = DictDataset(data)
            elif isinstance(data, Iterable):
                dataset = IterableDataset(data)
            else:
                raise TypeError("Invalid dataset type: %s"
                                % type(data).__name__)

        self.dataset = dataset

    def __call__(self, indices):
        """
        Loads data for given indices and combines them to batches
        Parameters
        ----------
        indices : list
            a list of integers specifying the data indices
        Returns
        -------
        dict
            a dict of numpy arrays (specifying the batches)
        """

        # get data for all indices
        data = [self.dataset[idx] for idx in indices]

        data_dict = defaultdict(list)

        # concatenate dict entities by keys
        for _result_dict in data:
            for key, val in _result_dict.items():
                data_dict[key].append(val)

        # convert list to numpy arrays
        for key, val_list in data_dict.items():
            data_dict[key] = np.asarray(val_list)

        return data_dict

    @property
    def process_id(self):
        """
        A Property to access the process id
        Returns
        -------
        int
            the process id
        """
        if self._process_id is None:
            return 0
        return self._process_id

    @process_id.setter
    def process_id(self, new_id):
        """
        Setter for the :attr:`process_id`; Makes sure, that the process id is
        only set once
        Parameters
        ----------
        new_id : int
        Raises
        ------
        AttributeError
            if the process id has already been set once
        """
        if self._process_id is not None:
            raise AttributeError("Attribute 'process_id' can be set only once")

        self._process_id = new_id


import logging
import numpy as np
import os
import tensorflow as tf
from tensorflow.python.training.summary_io import SummaryWriterCache

class EnvRunner(object):
    def __init__(self, env=None,
                 test_env=None,
                 agent=None,
                 observation_adapter=None,
                 action_adapter=None,
                 epoch_n=None,
                 batch_n=None,
                 step_n=16,
                 test_after_epoch=True,
                 train=True,
                 restore=False,
                 logdir=None):
        self._env = env
        self._test_env = test_env
        self._agent = agent
        self._obs_adapter = observation_adapter
        self._act_adapter = action_adapter
        self._epoch_n = epoch_n
        self._batch_n = batch_n
        self._step_n = step_n
        self._test_after_epoch = test_after_epoch
        self._obs = None
        self._train = train
        self._summary_writer = None
        self._saver = tf.train.Saver()
        self._logdir = logdir
        if logdir:
            if not os.path.isdir(logdir):
                os.makedirs(logdir)
            self._summary_writer = SummaryWriterCache.get(logdir)
        if restore or not train:
            self._saver.restore(
                self._agent._sess, tf.train.latest_checkpoint(logdir))

    def run(self, *args, **kwargs):
        if self._train:
            self._obs = self._env.reset()
        for epoch in range(self._epoch_n):
            if self._train:
                for batch in range(self._batch_n):
                    logging.info("epoch:%d,batch:%d" % (epoch, batch))
                    self._batch()
                checkpoint = self._logdir + '/%d.ckpt' % epoch
                self._saver.save(self._agent._sess, checkpoint)
            if self._test_after_epoch:
                total_reward = self._test()
                logging.info("epoch:%d,reward:%d" % (epoch, total_reward))
                self._record(step=epoch, reward=total_reward)

    def _batch(self):
        # train batch
        ## todo refactor
        obs = []
        func_calls = []
        acts = None
        for _ in range(self._step_n):
            obs.append(self._obs)
            state = self._obs_adapter.transform(self._obs, state_only=True)
            funcs_or_acts = self._agent.step(state=state, obs=self._obs)
            if isinstance(funcs_or_acts[0], tuple):
                function_calls = funcs_or_acts
                func_calls.extend(function_calls)
            else:
                if not acts:
                    acts = [[] for _ in range(len(funcs_or_acts))]
                for c, e in zip(acts, funcs_or_acts):
                    c.append(e)
                function_calls = self._act_adapter.reverse(funcs_or_acts)
            self._obs = self._env.step([(f,) for f in function_calls])
        obs.append(self._obs)

        (states, next_states,
         rewards, dones, timestamps) = self._obs_adapter.transform(obs)

        if not acts:
            acts = self._act_adapter.transform(func_calls)
        else:
            acts = [np.concatenate(c, axis=0) for c in acts]
        batch = (states, acts,
                 next_states, rewards, dones, timestamps)
        summary, step = self._agent.update(*batch)
        self._record(summary=summary, step=step)

    def _test(self):
        obs = self._test_env.reset()
        total_reward = 0
        while not obs[0].last():
            state = self._obs_adapter.transform(obs, state_only=True)
            action = self._agent.step(state=state, evaluate=True)
            function_calls = self._act_adapter.reverse(action)
            obs = self._test_env.step([(f,) for f in function_calls])
            total_reward += obs[0].reward
        return total_reward

    def _record(self, step=None, summary=None, **kwargs):
        if not self._train or not self._summary_writer:
            return
        if summary:
            self._summary_writer.add_summary(summary, step)
        for tag, value in kwargs.items():
            summary = tf.Summary()
            summary.value.add(tag=tag, simple_value=np.mean(value))
            self._summary_writer.add_summary(summary, step)


import random
import time
import warnings
import sys
import argparse
import shutil

import torch
import torch.backends.cudnn as cudnn
from torch.optim import SGD
from torch.utils.data import DataLoader
import torchvision.transforms as T
import torch.nn.functional as F
import os

from common.modules.classifier import Classifier
import common.vision.datasets as datasets
import common.vision.models as models
from common.vision.transforms import ResizeImage
from common.utils.metric import accuracy
from common.utils.meter import AverageMeter, ProgressMeter
from common.utils.data import ForeverDataIterator
from common.utils.logger import CompleteLogger

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

def main(args: argparse.Namespace):
    logger = CompleteLogger(args.log, args.phase)
    print(args)

    if args.seed is not None:
        random.seed(args.seed)
        torch.manual_seed(args.seed)
        cudnn.deterministic = True
        warnings.warn('You have chosen to seed training. '
                      'This will turn on the CUDNN deterministic setting, '
                      'which can slow down your training considerably! '
                      'You may see unexpected behavior when restarting '
                      'from checkpoints.')

    cudnn.benchmark = True

    # Data loading code
    normalize = T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    train_transform = T.Compose([
        ResizeImage(256),
        T.RandomResizedCrop(224),
        T.RandomHorizontalFlip(),
        T.ToTensor(),
        normalize
    ])
    val_transform = T.Compose([
        ResizeImage(256),
        T.CenterCrop(224),
        T.ToTensor(),
        normalize
    ])

    dataset = datasets.__dict__[args.data]
    train_dataset = dataset(root=args.root, split='train', sample_rate=args.sample_rate,
                            download=True, transform=train_transform)
    train_loader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True,
                              num_workers=args.workers, drop_last=True)
    train_iter = ForeverDataIterator(train_loader)
    val_dataset = dataset(root=args.root, split='test', sample_rate=100, download=True, transform=val_transform)
    val_loader = DataLoader(val_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers)

    # create model
    print("=> using pre-trained model '{}'".format(args.arch))
    backbone = models.__dict__[args.arch](pretrained=True)
    if args.pretrained:
        if os.path.isfile(args.pretrained):
            print("=> loading checkpoint '{}'".format(args.pretrained))
            model_dir = args.pretrained
            checkpoint = torch.load(model_dir)
            model_state = checkpoint['state_dict_best'] if not model_dir.endswith('.pth.tar') else checkpoint['state_dict']
            print("=> loaded checkpoint '{}' "
                  .format(args.pretrained))
            for k in list(model_state.keys()):
                # retain only encoder_q up to before the embedding layer
                if k.startswith('module.encoder_q') and not k.startswith('module.encoder_q.fc'):
                    model_state[f"{k[len('module.encoder_q.'):]}"] = model_state[k]
                # delete renamed or unused k
                del model_state[k]
            weights = model_state
            print(list(backbone.state_dict().keys()))
            print(f"TOTAL: {len(list(backbone.state_dict().keys()))}\n======")
            weights = {k: weights[k] for k in weights if k in backbone.state_dict()}
            print(f"Pretrained weights found (TOTAL: {len(list(weights.keys()))}):\n{weights.keys()}\n")
            x = backbone.state_dict()
            x.update(weights)
            backbone.load_state_dict(x)
    num_classes = train_dataset.num_classes
    classifier = Classifier(backbone, num_classes).to(device)

    # define optimizer and lr scheduler
    optimizer = SGD(classifier.get_parameters(args.lr), lr=args.lr, momentum=args.momentum, weight_decay=args.wd, nesterov=True)
    lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, args.lr_decay_epochs, gamma=args.lr_gamma)

    # resume from the best checkpoint
    if args.phase == 'test':
        checkpoint = torch.load(logger.get_checkpoint_path('best'), map_location='cpu')
        classifier.load_state_dict(checkpoint)
        acc1 = validate(val_loader, classifier, args)
        print(acc1)
        return

    # start training
    best_acc1 = 0.0
    for epoch in range(args.epochs):
        print(lr_scheduler.get_lr())
        # train for one epoch
        train(train_iter, classifier, optimizer, epoch, args)
        lr_scheduler.step()
        # evaluate on validation set
        acc1 = validate(val_loader, classifier, args)

        # remember best acc@1 and save checkpoint
        torch.save(classifier.state_dict(), logger.get_checkpoint_path('latest'))
        if acc1 > best_acc1:
            shutil.copy(logger.get_checkpoint_path('latest'), logger.get_checkpoint_path('best'))
        best_acc1 = max(acc1, best_acc1)

    print("best_acc1 = {:3.1f}".format(best_acc1))
    logger.close()

def train(train_iter: ForeverDataIterator, model: Classifier, optimizer: SGD,
          epoch: int, args: argparse.Namespace):
    batch_time = AverageMeter('Time', ':4.2f')
    data_time = AverageMeter('Data', ':3.1f')
    losses = AverageMeter('Loss', ':3.2f')
    cls_accs = AverageMeter('Cls Acc', ':3.1f')

    progress = ProgressMeter(
        args.iters_per_epoch,
        [batch_time, data_time, losses, cls_accs],
        prefix="Epoch: [{}]".format(epoch))

    # switch to train mode
    model.train()

    end = time.time()
    for i in range(args.iters_per_epoch):
        x, labels = next(train_iter)

        x = x.to(device)
        label = labels.to(device)

        # measure data loading time
        data_time.update(time.time() - end)

        # compute output
        y, f = model(x)
        cls_loss = F.cross_entropy(y, label)
        loss = cls_loss

        # measure accuracy and record loss
        losses.update(loss.item(), x.size(0))
        cls_acc = accuracy(y, label)[0]
        cls_accs.update(cls_acc.item(), x.size(0))

        # compute gradient and do SGD step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        if i % args.print_freq == 0:
            progress.display(i)

def validate(val_loader: DataLoader, model: Classifier, args: argparse.Namespace) -> float:
    batch_time = AverageMeter('Time', ':6.3f')
    losses = AverageMeter('Loss', ':.4e')
    top1 = AverageMeter('Acc@1', ':6.2f')
    top5 = AverageMeter('Acc@5', ':6.2f')
    progress = ProgressMeter(
        len(val_loader),
        [batch_time, losses, top1, top5],
        prefix='Test: ')

    # switch to evaluate mode
    model.eval()

    with torch.no_grad():
        end = time.time()
        for i, (images, target) in enumerate(val_loader):
            images = images.to(device)
            target = target.to(device)

            # compute output
            output, _ = model(images)
            loss = F.cross_entropy(output, target)

            # measure accuracy and record loss
            acc1, acc5 = accuracy(output, target, topk=(1, 5))
            losses.update(loss.item(), images.size(0))
            top1.update(acc1.item(), images.size(0))
            top5.update(acc5.item(), images.size(0))

            # measure elapsed time
            batch_time.update(time.time() - end)
            end = time.time()

            if i % args.print_freq == 0:
                progress.display(i)

        print(' * Acc@1 {top1.avg:.3f} Acc@5 {top5.avg:.3f}'
              .format(top1=top1, top5=top5))

    return top1.avg

if __name__ == '__main__':
    architecture_names = sorted(
        name for name in models.__dict__
        if name.islower() and not name.startswith("__")
        and callable(models.__dict__[name])
    )
    dataset_names = sorted(
        name for name in datasets.__dict__
        if not name.startswith("__") and callable(datasets.__dict__[name])
    )
    parser = argparse.ArgumentParser(description='Baseline for Finetuning')
    # dataset parameters
    parser.add_argument('root', metavar='DIR',
                        help='root path of dataset')
    parser.add_argument('-d', '--data', metavar='DATA',
                        help='dataset: ' + ' | '.join(dataset_names))
    parser.add_argument('-sr', '--sample-rate', default=100, type=int,
                        metavar='N',
                        help='sample rate of training dataset (default: 100)')
    # model parameters
    parser.add_argument('-a', '--arch', metavar='ARCH', default='resnet50',
                        choices=architecture_names,
                        help='backbone architecture: ' +
                             ' | '.join(architecture_names) +
                             ' (default: resnet50)')
    parser.add_argument('--pretrained', default=None,
                        help="pretrained checkpoint of the backbone. "
                             "(default: None, use the ImageNet supervised pretrained backbone)")
    # training parameters
    parser.add_argument('-b', '--batch-size', default=48, type=int,
                        metavar='N',
                        help='mini-batch size (default: 48)')
    parser.add_argument('--lr', '--learning-rate', default=0.01, type=float,
                        metavar='LR', help='initial learning rate', dest='lr')
    parser.add_argument('--lr-gamma', default=0.1, type=float, help='parameter for lr scheduler')
    parser.add_argument('--lr-decay-epochs', type=int, default=(12, ), nargs='+', help='epochs to decay lr')
    parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
                        help='momentum')
    parser.add_argument('--wd', '--weight-decay', default=0.0005, type=float,
                        metavar='W', help='weight decay (default: 5e-4)')
    parser.add_argument('-j', '--workers', default=2, type=int, metavar='N',
                        help='number of data loading workers (default: 4)')
    parser.add_argument('--epochs', default=20, type=int, metavar='N',
                        help='number of total epochs to run')
    parser.add_argument('-i', '--iters-per-epoch', default=500, type=int,
                        help='Number of iterations per epoch')
    parser.add_argument('-p', '--print-freq', default=100, type=int,
                        metavar='N', help='print frequency (default: 100)')
    parser.add_argument('--seed', default=None, type=int,
                        help='seed for initializing training. ')
    parser.add_argument("--log", type=str, default='baseline',
                        help="Where to save logs, checkpoints and debugging images.")
    parser.add_argument("--phase", type=str, default='train', choices=['train', 'test'],
                        help="When phase is 'test', only test the model.")
    args = parser.parse_args()
    main(args)