"""
Infinite evaluation loop going through the checkpoints in the model directory
as they appear and evaluating them. Accuracy and average loss are printed and
added as tensorboard summaries.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from datetime import datetime
import json
import math
import os
import sys
import time

import tensorflow as tf

from pgd.fmnist.model import Model
from pgd.fmnist.pgd_attack import LinfPGDAttack
from torchvision import datasets
from torch.utils.data import DataLoader

from sargan_dep.sargan_models import SARGAN
from sargan_dep.sar_utilities import add_gaussian_noise
from scipy.ndimage.filters import gaussian_filter
import numpy as np
from torchvision.transforms import Compose, ToTensor,  Resize
from PIL import Image
data_root='sar_data/FMNIST'

def cycle(iterable):
    while True:
        for x in iterable:
            yield x
def get_data(train_batch_size):

    data_transform = Compose([ToTensor()])

    train_loader = DataLoader(datasets.FashionMNIST(root=data_root, train=True, transform=data_transform, target_transform=None, download=True),
                              batch_size=train_batch_size, shuffle=True)

    return train_loader
global_step = tf.contrib.framework
# Global constants
with open('pgd/fmnist/config_fmnist.json') as config_file:
  config = json.load(config_file)
num_eval_examples = 64
eval_batch_size = 64
eval_on_cpu = config['eval_on_cpu']

model_dir = config['model_dir-n']
# Set upd the data, hyperparameters, and the model

img_size = [28,28,1]
trained_model_path = 'trained_models/sargan_fmnist-x1-1'
trained_model_path2 = 'trained_models/sargan_fmnist-x2-1'
trained_model_path3 = 'trained_models/sargan_fmnist-x3-1'
trained_model_path4 = 'trained_models/sargan_fmnist-x4-1'
BATCH_SIZE = 64
NOISE_STD_RANGE = [0.0, 0.2]

if eval_on_cpu:
  with tf.device("/cpu:0"):
      model = Model()
      attack = LinfPGDAttack(model,
                           .08,#config['epsilon'],
                           config['k'],
                           config['a'],
                           config['random_start'],
                           config['loss_func'])
else:
    model = Model()
    attack = LinfPGDAttack(model,
                         config['epsilon'],
                         config['k'],
                         config['a'],
                         config['random_start'],
                         config['loss_func'])

global_step = tf.contrib.framework.get_or_create_global_step()
# Setting up the Tensorboard and checkpoint outputs
if not os.path.exists(model_dir):
  os.makedirs(model_dir)
eval_dir = os.path.join(model_dir, 'eval')
if not os.path.exists(eval_dir):
  os.makedirs(eval_dir)

last_checkpoint_filename = ''
already_seen_state = False
saver = tf.train.Saver()
#summary_writer = tf.summary.FileWriter(eval_dir)
# A function for evaluating a single checkpoint
# A function for evaluating a single checkpoint
def evaluate_checkpoint(filename):
    #sys.stdout = open(os.devnull, 'w')
    #Different graphs for all the models
    gx1 =tf.Graph()
    gx2 =tf.Graph()
    gx3 =tf.Graph()
    gx4 =tf.Graph()
    with tf.Session() as sess:
    # Restore the checkpoint
        saver.restore(sess, filename);

        # Iterate over the samples batch-by-batch
        #number of batches
        num_batches = int(math.ceil(num_eval_examples / eval_batch_size))

        x_blur_list4=[]
        x_adv_list4=[]
        #Storing y values

        train_loader= get_data(BATCH_SIZE)
        trainiter = iter(cycle(train_loader))
        for ibatch in range(num_batches):

            x_batch2, y_batch = next(trainiter)
            x_batch2 = np.array(x_batch2.data.numpy().transpose(0,2,3,1))
            x_batch=np.zeros([len(x_batch2),img_size[0]*img_size[1]])

            x_batch3=np.zeros([img_size[0],img_size[1]])
            x_batch3[:,:]=x_batch2[0,:,:,0]*255
            nextimage=Image.fromarray(x_batch3.astype(np.uint8))
            nextimage.save("FMNIST_NATURALX0.Jpg","JPEG")

            for i in range(len(x_batch2)):
                x_batch[i]=x_batch2[i].reshape([img_size[0]*img_size[1]])
            x_batch_adv = attack.perturb(x_batch, y_batch, sess)

            x_batch2=np.zeros([len(x_batch),img_size[0],img_size[1],img_size[2]])
            x_batch_adv2=np.zeros([len(x_batch),img_size[0],img_size[1],img_size[2]])
            for k in range(len(x_batch)):
                x_batch2[k]=add_gaussian_noise(x_batch[k].reshape([img_size[0],img_size[1],img_size[2]]), sd=np.random.uniform(NOISE_STD_RANGE[1], NOISE_STD_RANGE[1]))
                x_batch_adv2[k]=add_gaussian_noise(x_batch_adv[k].reshape([img_size[0],img_size[1],img_size[2]]), sd=np.random.uniform(NOISE_STD_RANGE[1], NOISE_STD_RANGE[1]))

            x_blur_list4.append(x_batch2)
            x_adv_list4.append(x_batch_adv2)

            x_batch3=np.zeros([img_size[0],img_size[1]])
            x_batch3[:,:]=x_blur_list4[0][0,:,:,0]*255
            nextimage=Image.fromarray(x_batch3.astype(np.uint8))
            nextimage.save("FMNIST_BLURX0.Jpg","JPEG")
            x_batch3=np.zeros([img_size[0],img_size[1]])
            x_batch3[:,:]=x_adv_list4[0][0,:,:,0]*255
            nextimage=Image.fromarray(x_batch3.astype(np.uint8))
            nextimage.save("FMNIST_ADVX0.Jpg","JPEG")

    #Running through first autoencoder
    with gx1.as_default():
        with tf.Session() as sess2:
            sargan_model=SARGAN(img_size, BATCH_SIZE, img_channel=1)
            sargan_saver= tf.train.Saver()
            sargan_saver = tf.train.import_meta_graph(trained_model_path+'/sargan_mnist.meta');
            sargan_saver.restore(sess2,tf.train.latest_checkpoint(trained_model_path));
            for ibatch in range(num_batches):
                processed_batch=sess2.run(sargan_model.gen_img,feed_dict={sargan_model.image: x_adv_list4[ibatch], sargan_model.cond: x_adv_list4[ibatch]})
                x_adv_list4[ibatch]=processed_batch

                blurred_batch=sess2.run(sargan_model.gen_img,feed_dict={sargan_model.image: x_blur_list4[ibatch], sargan_model.cond: x_blur_list4[ibatch]})
                x_blur_list4[ibatch]=blurred_batch

            x_batch3[:,:]=x_blur_list4[0][0,:,:,0]*255
            nextimage=Image.fromarray(x_batch3.astype(np.uint8))
            nextimage.save("FMNIST_BLURX1.Jpg","JPEG")
            x_batch3=np.zeros([img_size[0],img_size[1]])
            x_batch3[:,:]=x_adv_list4[0][0,:,:,0]*255
            nextimage=Image.fromarray(x_batch3.astype(np.uint8))
            nextimage.save("FMNIST_ADVX1.Jpg","JPEG")

    with gx2.as_default():
        with tf.Session() as sessx2:
            sargan_model2=SARGAN(img_size, BATCH_SIZE, img_channel=1)
            sargan_saver2= tf.train.Saver()
            sargan_saver2 = tf.train.import_meta_graph(trained_model_path2+'/sargan_mnist.meta');
            sargan_saver2.restore(sessx2,tf.train.latest_checkpoint(trained_model_path2));
            for ibatch in range(num_batches):
                processed_batch=sessx2.run(sargan_model2.gen_img,feed_dict={sargan_model2.image: x_adv_list4[ibatch], sargan_model2.cond: x_adv_list4[ibatch]})
                x_adv_list4[ibatch]=(processed_batch)

                blurred_batch=sessx2.run(sargan_model2.gen_img,feed_dict={sargan_model2.image: x_blur_list4[ibatch], sargan_model2.cond: x_blur_list4[ibatch]})
                x_blur_list4[ibatch]=(blurred_batch)
            x_batch3=np.zeros([img_size[0],img_size[1]])
            x_batch3[:,:]=x_blur_list4[0][0,:,:,0]*255
            nextimage=Image.fromarray(x_batch3.astype(np.uint8))
            nextimage.save("FMNIST_BLURX2.Jpg","JPEG")
            x_batch3=np.zeros([img_size[0],img_size[1]])
            x_batch3[:,:]=x_adv_list4[0][0,:,:,0]*255
            nextimage=Image.fromarray(x_batch3.astype(np.uint8))
            nextimage.save("FMNIST_ADVX2.Jpg","JPEG")

    with gx3.as_default():
        with tf.Session() as sessx3:
            sargan_model3=SARGAN(img_size, BATCH_SIZE, img_channel=1)
            sargan_saver3= tf.train.Saver()
            sargan_saver3 = tf.train.import_meta_graph(trained_model_path3+'/sargan_mnist.meta');
            sargan_saver3.restore(sessx3,tf.train.latest_checkpoint(trained_model_path3));
            for ibatch in range(num_batches):
                processed_batch=sessx3.run(sargan_model3.gen_img,feed_dict={sargan_model3.image: x_adv_list4[ibatch], sargan_model3.cond: x_adv_list4[ibatch]})
                x_adv_list4[ibatch]=processed_batch

                blurred_batch=sessx3.run(sargan_model3.gen_img,feed_dict={sargan_model3.image: x_blur_list4[ibatch], sargan_model3.cond: x_blur_list4[ibatch]})
                x_blur_list4[ibatch]=blurred_batch

            x_batch3=np.zeros([img_size[0],img_size[1]])
            x_batch3[:,:]=x_blur_list4[0][0,:,:,0]*255
            nextimage=Image.fromarray(x_batch3.astype(np.uint8))
            nextimage.save("FMNIST_BLURX3.Jpg","JPEG")
            x_batch3=np.zeros([img_size[0],img_size[1]])
            x_batch3[:,:]=x_adv_list4[0][0,:,:,0]*255
            nextimage=Image.fromarray(x_batch3.astype(np.uint8))
            nextimage.save("FMNIST_ADVX3.Jpg","JPEG")

    #Final autoencoder setup
    with gx4.as_default():
        with tf.Session() as sessx4:
            sargan_model4=SARGAN(img_size, BATCH_SIZE, img_channel=1)
            sargan_saver4= tf.train.Saver()
            sargan_saver4 = tf.train.import_meta_graph(trained_model_path4+'/sargan_mnist.meta');
            sargan_saver4.restore(sessx4,tf.train.latest_checkpoint(trained_model_path4));
            for ibatch in range(num_batches):
                processed_batch=sessx4.run(sargan_model4.gen_img,feed_dict={sargan_model4.image: x_adv_list4[ibatch], sargan_model4.cond: x_adv_list4[ibatch]})
                x_adv_list4[ibatch]=processed_batch.reshape([len(x_batch),img_size[0]*img_size[1]])

                x_batch3=np.zeros([img_size[0],img_size[1]])
                x_batch3[:,:]=processed_batch[0,:,:,0]*255
                nextimage=Image.fromarray(x_batch3.astype(np.uint8))
                nextimage.save("FMNIST_ADVX4.Jpg","JPEG")

                blurred_batch=sessx4.run(sargan_model4.gen_img,feed_dict={sargan_model4.image: x_blur_list4[ibatch], sargan_model4.cond: x_blur_list4[ibatch]})
                x_blur_list4[ibatch]=blurred_batch.reshape([len(x_batch),img_size[0]*img_size[1]])

                x_batch3=np.zeros([img_size[0],img_size[1]])
                x_batch3[:,:]=blurred_batch[0,:,:,0]*255
                nextimage=Image.fromarray(x_batch3.astype(np.uint8))
                nextimage.save("FMNIST_BLURX4.Jpg","JPEG")

# eval loop
cur_checkpoint = tf.train.latest_checkpoint(model_dir)
evaluate_checkpoint(cur_checkpoint)
time.sleep(10)


import math

def frog(line):
    length = len(line)
    if length > 2:
        min = math.floor(length / 2)
        B = line.count('B')
        if (B >= min) and (B <= (length-2)):
            return('Y')
        else:
            return('N')
    else:
        return('N')

with open('input.txt','r') as inf:
    T = int(inf.readline())

    for x in range(1,T+1):
        line = inf.readline().strip()
        result = frog(line)
        with open('output.txt', 'a') as outf:
            outf.write('Case #{}: {}\n'.format(x,result))

Facebook hacker Cup 2019/Leap Frog Chapter 1/Leap_Frog_Chapter_1.py


# import ray
# ray.init()
from tensorboardX import SummaryWriter
import os

filelist = [ f for f in os.listdir('./experiment/') ]
for f in filelist:
    os.remove(os.path.join('./experiment/', f))

writer = SummaryWriter('./experiment/')

def run_experiment(agents, feeds, exp, num_episodes, r, env_type):
    import time
    from environment import FeedUnit, Feed

    cur_time = float(time.time())
    n = len(feeds)
    num_positive = 0
    for feed in feeds:
        if feed.interest > 0:
            num_positive += 1

#     print('num units: {}, num negative: {}, randomize: {}, experiment: {}'.format(n, n - num_positive, r, exp))

    envs = [
        Feed(feeds, num_positive, env_type) for _ in range(len(agents))
    ]

    agents_episode_reward = {}
    for agent in agents:
        # agents_cumulative_reward[agent.agent_name] = []
        agents_episode_reward[agent.agent_name] = []

    for j in range(num_episodes):
        if j % 100 == 0:
            print('num units: {}, num negative: {}, randomize: {}, experiment: {}'.format(n, n - num_positive, r, exp))
            print('Experiment {} at episode {}, used time {}'.format(exp, j, time.time() - cur_time))
            cur_time = time.time()
            for i, agent in enumerate(agents):
                print(agent.agent_name, agent.cum_rewards)

        for i, agent in enumerate(agents):
            envs[i].reset()
            agents[i].reset()
        for i in range(len(agents)):
            scroll = True
            # count = 0
            while scroll:
                action = agents[i].choose_action()
                # print('current_feed_interest: {}'.format(feeds[count].interest))
                # count += 1
                scroll, reward = envs[i].step(action)
                # print(reward)
                agents[i].update_buffer(scroll, reward)

            agents[i].learn_from_buffer()

            agents_episode_reward[agents[i].agent_name].append(agents[i].cum_rewards)

            if writer:
                if agents[i].agent_name != 'Oracle':
                    writer.add_scalar('running_loss_' + agents[i].agent_name + '_' + str(exp), agents[i].running_loss, j)
                writer.add_scalar('cumulative_reward_interest_unknown' + agents[i].agent_name + '_' + str(exp), agents[i].cum_rewards, j)

    return agents_episode_reward

# @ray.remote
def experiment_wrapper(feed_units, i, num_episodes, randomize, env_type):
    from supervised_agent import SupervisedAgent
    from supervised_agent_one_step import SupervisedAgentOneStep
    from dqn_agent import DQNAgent
    from deep_exp_hyper_agent import DeepExpHyperAgent
    from deep_exp_agent import DeepExpAgent
    from deep_exp_ids_agent import DeepExpIDSAgent
    import numpy as np

    deep_exp_agents = []
    num_positive = 0
    for feed in feed_units:
        if feed.interest > 0:
            num_positive += 1
    for prior in range(0, 1):
        deep_exp_agents.append(
            DeepExpAgent(
                [k for k in range(len(feed_units))],
                'deep_exploration_{}_{}_{}'.format(num_positive, len(feed_units), prior),
                prior_variance=10**prior,
#                     bootstrap=False
            )
        )
    agents = ([
        # OracleAgent(feed_units, session_size),
        SupervisedAgent([k for k in range(len(feed_units))], 'supervised_{}_{}'.format(num_positive, len(feed_units))),
        SupervisedAgentOneStep([k for k in range(len(feed_units))], 'supervised_one_step_{}_{}'.format(num_positive, len(feed_units))),
        DQNAgent([k for k in range(len(feed_units))], 'dqn_{}_{}'.format(num_positive, len(feed_units))),
        # DeepExpHyperAgent([k for k in range(len(feed_units))], 'hyper_deep_exploration_{}_{}'.format(num_positive, len(feed_units))),
        # DeepExpIDSAgent([k for k in range(len(feed_units))], 'ids_{}_{}'.format(num_positive, len(feed_units))),
    ]
    + deep_exp_agents
    )

    cumulative_reward = run_experiment(agents, feed_units, i, num_episodes, randomize, env_type)

    np.save('ids_experiment_{}_{}_{}_{}_{}'.format(
        num_positive, len(feed_units), int(randomize), i, env_type), cumulative_reward)

def generate_feeds(n, k):
    from environment import FeedUnit, Feed

    feed_units = []
    for i in range(k):
        feed_units.append(FeedUnit(-1, i))
    for i in range(n):
        feed_units.append(FeedUnit(1, i))

    return feed_units

def caller(n, k, num_experiment, num_episodes, randomize=False, env_type='sparse_reward'):
    import random

    feed_units = generate_feeds(n, k)

    if randomize:
        random.shuffle(feed_units)
#     writer = SummaryWriter('./experiment/')

    agents_cumulative_reward = []
    agents = []

    futures = []
    for i in range(num_experiment):
        experiment_wrapper(feed_units, i, num_episodes, randomize, env_type)
    #     futures.append(experiment_wrapper.remote(feed_units, i, num_episodes, randomize))
    # ray.get(futures)

#     with Pool(num_experiment) as p:
#         print(p.map(experiment_wrapper, [(feed_units, i, num_episodes, randomize) for i in range(num_experiment)]))

if __name__ == '__main__':
    num_experiments = 5
    inputs = []

    for n in range(5, 6):
        for k in range(2, 3):
            # for r in range(0, 1):
            num_episodes = 2000
            caller(n, k, num_experiments, num_episodes, env_type='sparse_reward')

    writer.close()
#     import torch.multiprocessing as mp
#     processes = []

#     for n in range(7, 8):
#         for k in range(3, 5):
#             for r in range(2):
#                 num_episodes = 10000 * (k + 1)
#                 caller(n, k, num_experiments, num_episodes, bool(r))
#                 p = mp.Process(target=caller, args=(n, k, num_experiments, num_episodes, bool(r)))
#                 p.start()
#                 processes.append(p)

#     print(len(processes))
#     for p in processes:
#         p.join()

#     from multiprocessing import Pool
#     with Pool(30) as p:
#         p.starmap(caller, inputs)


import numpy as np

def uniq2orderipix(uniq):
    """
    convert a HEALPix pixel coded as a NUNIQ number
    to a (norder, ipix) tuple
    """
    order = (np.log2(uniq // np.uint8(4))) // np.uint8(2)
    order = order.astype(np.uint8)
    ipix = uniq - np.uint64(4) * (np.uint64(4) ** np.uint64(order))

    return order, ipix.astype(np.uint64)


import bs4
from bs4 import NavigableString

from selenium import webdriver

import sys
import time
import os
import traceback
import logging
import tempfile
import re

from selenium.common.exceptions import NoSuchElementException

import pr_settings
import gmail

logging.basicConfig(format='%(asctime)s %(levelname)s : %(message)s', level=logging.INFO)
slot_container_with_date_regex = re.compile('^slot-container-(\d{4}-\d{2}-\d{2})$')
not_available_text = "No doorstep delivery windows are available".lower()

class HeartBeat:
    def __init__(self, freq_in_secs):
        self.freq_in_sec = freq_in_secs
        self.starting = time.time()

    def check(self):
        elapsed = time.time() - self.starting
        logging.info('elapsed sec : ' + str(elapsed))
        if elapsed > self.freq_in_sec:
            os.system('say "Program still alive"')
            self.starting = time.time()

class Slot:
    def __init__(self, dateStr, slots_open):
        self.dateStr = dateStr
        self.slotsOpen = slots_open

    def __str__(self):
        return "Slot[" + self.dateStr + (" HAS" if self.slotsOpen else " has NO") \
               + " open slots]"

def find_slot_from_slot_container_base(soup):
    """
    :param bs: beautifulsoup object
    :return:
    """
    slot_container_root = soup.find(id="slot-container-root")
    slots = []
    for slot in slot_container_root.findAll('div', id=slot_container_with_date_regex):
        dateStr = slot_container_with_date_regex.match(slot['id']).group(1)
        unattended_container = slot.find('div', id="slot-container-UNATTENDED")
        text = unattended_container.findAll(end_node)[0].text
        logging.debug("Found text : " + text)
        slotsOpen = False
        if (text is not None and text.lower().find(not_available_text) >= 0):
            slotsOpen = False
        else:
            slotsOpen = True
        slots.append(Slot(dateStr, slotsOpen))
    return slots

def alert(open_slots):
    slots_str = "slots : " + str([str(s) for s in open_slots])
    logging.info('SLOTS OPEN!')
    os.system('say "Slots for delivery opened!"')
    send_gmail(slots_str)
    logging.info('email sent')
    time.sleep(1400)

def saveToFile(folder, text):
    f = open(folder + next(tempfile._get_candidate_names())+'.html', "a")
    f.write(text)
    f.close()

def send_gmail(body_text):
    service = gmail.build_service()
    # Call the Gmail API
    msg = gmail.create_message(pr_settings.gmail_from, pr_settings.gmail_to, pr_settings.gmail_subject,
                               pr_settings.gmail_body+" "+body_text)
    gmail.send_message(service, 'me', msg)

def check_slots_algo1(soup):
    """
    :param soup: beautiful soup object representing the whole html
    :return: True if has open slots. False otherwise.
    """
    no_open_slots = True
    try:
        slots = find_slot_from_slot_container_base(soup)
        logging.info("slots : " + str([str(s) for s in slots]))
        open_slots = [sl for sl in slots if sl.slotsOpen]
        if len(open_slots) == 0:
            return False
        else:
            saveToFile(pr_settings.html_file_temp_folder, str(soup))
            alert(open_slots)
            return True
    except NoSuchElementException as e:
        traceback.print_exc(file=sys.stdout)

def end_node(tag):
    """
    Args:
      tag: html element.

    Returns: True if tag is a span and contains text and no other tags inside.
    """
    if tag.name not in ["span"]:
        return False
    if isinstance(tag, NavigableString):  # if str return
        return False
    if not tag.text:  # if no text return false
        return False
    elif len(tag.find_all(text=False)) > 0:  # no other tags inside other than text
        return False
    return True  # if valid it reaches here

def getWFSlot(productUrl):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
    }

    driver = webdriver.Chrome(executable_path=pr_settings.chrome_webdriver_executable_path)
    driver.get(productUrl)
    html = driver.page_source
    soup = bs4.BeautifulSoup(html, features="html.parser")
    time.sleep(60)
    has_open_slots = False
    heart_beat = HeartBeat(600)
    while not has_open_slots:
        heart_beat.check()
        driver.refresh()
        logging.info("refreshed")
        html = driver.page_source
        # saveToFile(pr_settings.html_file_temp_folder,
        #            html)
        soup = bs4.BeautifulSoup(html, features="html.parser")
        has_open_slots = check_slots_algo1(soup)
        if has_open_slots:
            time.sleep(1400)
        else :
            time.sleep(3)
        #
        # try:
        #     open_slots = soup.find('div', class_='orderSlotExists');
        #     if open_slots is not None and open_slots.text() != "false":
        #         no_open_slots = False
        #         alert()
        #     else:
        #         logging.info('Ah! No open slots yet.')
        # except AttributeError as e:
        #     traceback.print_exc(file=sys.stdout)

def main():
    getWFSlot('https://www.amazon.com/gp/buy/shipoptionselect/handlers/display.html?hasWorkingJavascript=1')

if __name__ == '__main__':
    main()


#!/usr/bin/env python
from setuptools import setup
import os
import sys

if os.geteuid() == 0:
    data_files = [('/etc/bash_completion.d', ['pythonpy/pycompletion.sh']),]
else:
    if sys.argv[1] == 'install':
        print(
'''******************************************************************************
Looks like you didn't run this command using sudo.
Pythonpy needs root privileges to copy pycompletion.sh to /etc/bash_completion.d
1) If you are in a virtualenv, you can configure tab completion without root using:
    source /path/to/virtualenv/bash_completion.d/pycompletion.sh
2) If you aren't using virtualenv, remember that pip requires sudo by default
    on most systems. py is a simple python script that does not require any
    root access or special privileges. If you don't like using root,
    learn virtualenv and refer to 1).
Installation proceeding without root access...
******************************************************************************''')
    data_files = [('bash_completion.d', ['pythonpy/pycompletion.sh']),]

py_entry = 'py%s = pythonpy.__main__:main'
pycompleter_entry = 'pycompleter%s = pythonpy.pycompleter:main'
endings = ('', sys.version[:1], sys.version[:3])
entry_points_scripts = []
for e in endings:
    entry_points_scripts.append(py_entry % e)
    entry_points_scripts.append(pycompleter_entry % e)

setup(
    name='pythonpy',
    version='0.3.5',
    description='python -c, with tab completion and shorthand',
    data_files=data_files,
    license='MIT',
    url='https://github.com/Russell91/pythonpy',
    long_description='https://github.com/Russell91/pythonpy',
    packages = ['pythonpy'],
    entry_points = {
        'console_scripts': entry_points_scripts
    },
)


# Databricks notebook source
# MAGIC %md
# MAGIC COPYRIGHT:  Columbia Sportswear 2019

# MAGIC DESCRIPTION: This notebook loads data to the stage demandsnapshot table
# MAGIC
# MAGIC -----------------------------------------------------------------
# MAGIC ###### MODIFICATION LOG
# MAGIC | Programmmer          | Change Request  | Date       | Change Description                                                 |
# MAGIC |----------------------|-----------------|------------|--------------------------------------------------------------------|
# MAGIC | Raghu Gummadavelli   | CSC Initial Build | 01/08/2019 | Initial creation  |

# COMMAND ----------

dbutils.widgets.text("schmNm", "", "")
dbutils.widgets.text("tblNm", "", "")

dbutils.widgets.text("deltaTS", "", "")
dbutils.widgets.text("initFlg", "", "")

deltaTS = dbutils.widgets.get("deltaTS")
initFlg = dbutils.widgets.get("initFlg")
schmNm = dbutils.widgets.get("schmNm")
tblNm = dbutils.widgets.get("tblNm")

#Update the Timestamp to remove T
deltaTS = deltaTS.replace('T',' ')
dbutils.widgets.text("region", "")

# COMMAND ----------

# MAGIC %sql
# MAGIC CREATE DATABASE IF NOT EXISTS ENTPR_SALES_ORDER
# MAGIC location '/mnt/entadls/published/eim/managed/entpr_sales_order';
# MAGIC CREATE TABLE IF NOT EXISTS ENTPR_SALES_ORDER.sap_opn_dmd_snpst
# MAGIC (
# MAGIC  SLS_DOC_NBR string
# MAGIC ,SLS_DOC_ITEM_NBR string
# MAGIC ,SLS_DOC_SCHD_LIN string
# MAGIC ,RQRT_TYP string
# MAGIC ,PLNT_CD string
# MAGIC ,MTRL_NBR string
# MAGIC ,SIZE_DIM_CD string
# MAGIC ,ITEM_CATG_CD string
# MAGIC ,DOC_CATG_CD string
# MAGIC ,SOLD_TO_CUST_NBR string
# MAGIC ,RQRT_CATG_CD string
# MAGIC ,VALN_TYP_CD string
# MAGIC ,REQD_DLVY_DT string
# MAGIC ,REQD_DLYY_SEAS string
# MAGIC ,BASE_UOM_CD string
# MAGIC ,LCL_CURR_CD string
# MAGIC ,OPN_QTY double
# MAGIC ,OPN_COST_AMT double
# MAGIC ,SNPST_DT date
# MAGIC ,SNPST_DT_MTH_NBR smallint
# MAGIC ,SNPST_DT_YR_NBR smallint
# MAGIC ,MTRL_PLNT_STAT_CD string
# MAGIC ,OFRNG_MTHD_CD string
# MAGIC ,RGN_CD string
# MAGIC ,EDW_CRT_TS timestamp
# MAGIC ,EDW_UPDT_TS timestamp
# MAGIC ,EDW_HASH_CHK string
# MAGIC ,EDW_ACTV_FLG string
# MAGIC )
# MAGIC USING DELTA
# MAGIC -- PARTITIONED BY (DOC_YR_NBR)
# MAGIC LOCATION "/mnt/entadls/published/eim/managed/entpr_sales_order/sap_opn_dmd_snpst"

# COMMAND ----------

if tblNm == "sap_opn_dmd_snpst_na" :
  tblNm = 'sap_opn_dmd_snpst'
  region = 'USA'

  sqlQuery = """CREATE OR REPLACE TEMPORARY VIEW v_plant AS
  SELECT distinct location, region
  from csc.dimlocation
  where region in ('USA','CAN') and channel = 'Wholesale'"""
  spark.sql(sqlQuery)

  sqlQuery = """delete from entpr_sales_order.sap_opn_dmd_snpst
   where rgn_cd in ('USA','CAN') and SNPST_DT = current_date """
  spark.sql(sqlQuery)

# COMMAND ----------

if tblNm == "sap_opn_dmd_snpst_china" :
  tblNm = 'sap_opn_dmd_snpst'
  region = 'CHN'

  sqlQuery = """CREATE OR REPLACE TEMPORARY VIEW v_plant AS
  SELECT distinct location, region
  from csc.dimlocation
  where region = 'CHN' and channel = 'Wholesale'"""
  spark.sql(sqlQuery)

  sqlQuery = """delete from entpr_sales_order.sap_opn_dmd_snpst
   where rgn_cd = 'CHN' and SNPST_DT = current_date """
  spark.sql(sqlQuery)

# COMMAND ----------

if tblNm == "sap_opn_dmd_snpst_europe" :
  tblNm = 'sap_opn_dmd_snpst'
  region = 'EUR'

  sqlQuery = """CREATE OR REPLACE TEMPORARY VIEW v_plant AS
  SELECT distinct location, region
  from csc.dimlocation
  where region = 'EUR' and channel = 'Wholesale'"""
  spark.sql(sqlQuery)

  sqlQuery = """delete from entpr_sales_order.sap_opn_dmd_snpst
   where rgn_cd = 'EUR' and SNPST_DT = current_date """
  spark.sql(sqlQuery)

# COMMAND ----------

if tblNm == "sap_opn_dmd_snpst_japan" :
  tblNm = 'sap_opn_dmd_snpst'
  region = 'JPN'

  sqlQuery = """CREATE OR REPLACE TEMPORARY VIEW v_plant AS
  SELECT distinct location, region
  from csc.dimlocation
  where region = 'JPN' and channel = 'Wholesale'"""
  spark.sql(sqlQuery)

  sqlQuery = """delete from entpr_sales_order.sap_opn_dmd_snpst
   where rgn_cd = 'JPN' and SNPST_DT = current_date """
  spark.sql(sqlQuery)

# COMMAND ----------

#Truncate working from 4.8
if initFlg == "X" :
  truncateTable = "TRUNCATE TABLE {0}.{1}".format(schmNm, tblNm)
  spark.sql(truncateTable)

# COMMAND ----------

# MAGIC %sql
# MAGIC
# MAGIC create or replace temp view v_bdsi as
# MAGIC
# MAGIC select
# MAGIC AUFNR as SLS_DOC_NBR
# MAGIC ,POSNR as SLS_DOC_ITEM_NBR
# MAGIC ,ETENR as SLS_DOC_SCHD_LIN
# MAGIC ,BDART as RQRT_TYP
# MAGIC ,WERKS as PLNT_CD
# MAGIC ,MATNR as MTRL_NBR
# MAGIC ,J_3ASIZE as SIZE_DIM_CD
# MAGIC ,PSTYP as ITEM_CATG_CD
# MAGIC ,VBTYP as DOC_CATG_CD
# MAGIC ,KUNNR as SOLD_TO_CUST_NBR
# MAGIC ,J_4KRCAT as RQRT_CATG_CD
# MAGIC ,case when j_4krcat = 'V1R' then 'V1' else j_4krcat end as VALN_TYP_CD
# MAGIC ,VDATU as REQD_DLVY_DT
# MAGIC ,case when cast(substring(VDATU,5,2) as int) <= 6 then 'S' || substring(VDATU,1,2)
# MAGIC       else 'F' || substring(VDATU,1,2) end  as REQD_DLYY_SEAS
# MAGIC ,MEINS as BASE_UOM_CD
# MAGIC ,MENGE as OPN_QTY
# MAGIC ,current_date as SNPST_DT
# MAGIC ,cast(substring(current_date,6,2) as int) as SNPST_DT_MTH_NBR
# MAGIC ,cast(substring(current_date,1,4) as int) as SNPST_DT_YR_NBR
# MAGIC ,CURRENT_TIMESTAMP as EDW_CRT_TS
# MAGIC ,CURRENT_TIMESTAMP as EDW_UPDT_TS
# MAGIC ,'NO CHECKSUM' as EDW_HASH_CHK
# MAGIC ,'Y' as EDW_ACTV_FLG
# MAGIC from edw_lnd_sap_view.j_3abdsi
# MAGIC inner join v_plant
# MAGIC on
# MAGIC werks = location
# MAGIC where BDART = '01'

# COMMAND ----------

# MAGIC %sql
# MAGIC create or replace temp view v_mbew as
# MAGIC select
# MAGIC  MATERIAL
# MAGIC ,BWKEY
# MAGIC ,BWTAR
# MAGIC ,currency
# MAGIC ,STPRS
# MAGIC from EDW_LND_SAP_view.EMDATU01
# MAGIC inner join v_plant
# MAGIC on
# MAGIC BWKEY = location

# COMMAND ----------

# MAGIC %sql
# MAGIC
# MAGIC create or replace temp view v_sap_plnt as
# MAGIC
# MAGIC select
# MAGIC plnt_cd,
# MAGIC sls_org_cd
# MAGIC from entpr_product_view.sap_plnt

# COMMAND ----------

# MAGIC %sql
# MAGIC
# MAGIC create or replace temp view v_sap_plnt_mtrl as
# MAGIC
# MAGIC select
# MAGIC mtrl_nbr,
# MAGIC t1.plnt_cd,
# MAGIC MTRL_PLNT_STAT_CD,
# MAGIC case when (VLD_FRM_DT > current_date) then '02' else MTRL_PLNT_STAT_CD end as mtrl_plnt_stat_cd_calc,
# MAGIC sls_org_cd
# MAGIC from entpr_product_view.sap_mtrl_plnt as t1
# MAGIC inner join v_sap_plnt as t2
# MAGIC on
# MAGIC t1.plnt_cd = t2.plnt_cd

# COMMAND ----------

# MAGIC %sql
# MAGIC
# MAGIC create or replace temp view v_bdsi_mat_plant as
# MAGIC
# MAGIC select
# MAGIC  SLS_DOC_NBR
# MAGIC ,SLS_DOC_ITEM_NBR
# MAGIC ,SLS_DOC_SCHD_LIN
# MAGIC ,RQRT_TYP
# MAGIC ,t1.PLNT_CD
# MAGIC ,t1.MTRL_NBR
# MAGIC ,SIZE_DIM_CD
# MAGIC ,ITEM_CATG_CD
# MAGIC ,DOC_CATG_CD
# MAGIC ,SOLD_TO_CUST_NBR
# MAGIC ,RQRT_CATG_CD
# MAGIC ,VALN_TYP_CD
# MAGIC ,REQD_DLVY_DT
# MAGIC ,REQD_DLYY_SEAS
# MAGIC ,BASE_UOM_CD
# MAGIC ,currency as LCL_CURR_CD
# MAGIC ,OPN_QTY
# MAGIC ,OPN_QTY * stprs as OPN_COST_AMT
# MAGIC ,SNPST_DT
# MAGIC ,SNPST_DT_MTH_NBR
# MAGIC ,SNPST_DT_YR_NBR
# MAGIC ,MTRL_PLNT_STAT_CD
# MAGIC ,mtrl_plnt_stat_cd_calc
# MAGIC ,sls_org_cd
# MAGIC ,EDW_CRT_TS
# MAGIC ,EDW_UPDT_TS
# MAGIC ,EDW_HASH_CHK
# MAGIC ,EDW_ACTV_FLG
# MAGIC
# MAGIC from v_bdsi as t1
# MAGIC left outer join v_mbew as t2
# MAGIC on
# MAGIC t1.plnt_cd = bwkey and
# MAGIC t1.mtrl_nbr = material and
# MAGIC VALN_TYP_CD = bwtar
# MAGIC
# MAGIC left outer join v_sap_plnt_mtrl as t3
# MAGIC on
# MAGIC t1.plnt_cd = t3.plnt_cd and
# MAGIC t1.mtrl_nbr = t3.mtrl_nbr

# COMMAND ----------

# MAGIC %sql
# MAGIC
# MAGIC create or replace temp view v_final_bdsi as
# MAGIC select
# MAGIC  SLS_DOC_NBR
# MAGIC ,SLS_DOC_ITEM_NBR
# MAGIC ,SLS_DOC_SCHD_LIN
# MAGIC ,RQRT_TYP
# MAGIC ,t1.PLNT_CD
# MAGIC ,t1.MTRL_NBR
# MAGIC ,SIZE_DIM_CD
# MAGIC ,ITEM_CATG_CD
# MAGIC ,DOC_CATG_CD
# MAGIC ,SOLD_TO_CUST_NBR
# MAGIC ,RQRT_CATG_CD
# MAGIC ,VALN_TYP_CD
# MAGIC ,REQD_DLVY_DT
# MAGIC ,REQD_DLYY_SEAS
# MAGIC ,BASE_UOM_CD
# MAGIC ,LCL_CURR_CD
# MAGIC ,OPN_QTY
# MAGIC ,OPN_COST_AMT
# MAGIC ,SNPST_DT
# MAGIC ,SNPST_DT_MTH_NBR
# MAGIC ,SNPST_DT_YR_NBR
# MAGIC ,MTRL_PLNT_STAT_CD
# MAGIC ,mtrl_plnt_stat_cd_calc
# MAGIC ,t1.sls_org_cd
# MAGIC ,'' ofrng_mthd_cd
# MAGIC ,region
# MAGIC ,EDW_CRT_TS
# MAGIC ,EDW_UPDT_TS
# MAGIC ,EDW_HASH_CHK
# MAGIC ,EDW_ACTV_FLG
# MAGIC from v_bdsi_mat_plant as t1
# MAGIC left outer join v_plant
# MAGIC on
# MAGIC plnt_cd = location

# COMMAND ----------

# MAGIC %sql
# MAGIC --Merge upsert to Staging table. The condition is on all the key columns
# MAGIC MERGE INTO entpr_sales_order.sap_opn_dmd_snpst pr
# MAGIC USING v_final_bdsi pd
# MAGIC on
# MAGIC pr.SLS_DOC_NBR = pd.SLS_DOC_NBR AND
# MAGIC pr.SLS_DOC_ITEM_NBR = pd.SLS_DOC_ITEM_NBR and
# MAGIC pr.SLS_DOC_SCHD_LIN = pd.SLS_DOC_SCHD_LIN and
# MAGIC pr.RQRT_TYP = pd.RQRT_TYP
# MAGIC
# MAGIC WHEN MATCHED THEN
# MAGIC UPDATE SET
# MAGIC pr.SLS_DOC_NBR = pd.SLS_DOC_NBR,
# MAGIC pr.SLS_DOC_ITEM_NBR = pd.SLS_DOC_ITEM_NBR,
# MAGIC pr.SLS_DOC_SCHD_LIN = pd.SLS_DOC_SCHD_LIN,
# MAGIC pr.RQRT_TYP = pd.RQRT_TYP,
# MAGIC pr.PLNT_CD = pd.PLNT_CD,
# MAGIC pr.MTRL_NBR = pd.MTRL_NBR,
# MAGIC pr.SIZE_DIM_CD = pd.SIZE_DIM_CD,
# MAGIC pr.ITEM_CATG_CD = pd.ITEM_CATG_CD ,
# MAGIC pr.DOC_CATG_CD = pd.DOC_CATG_CD ,
# MAGIC pr.SOLD_TO_CUST_NBR = pd.SOLD_TO_CUST_NBR ,
# MAGIC pr.RQRT_CATG_CD = pd.RQRT_CATG_CD ,
# MAGIC pr.VALN_TYP_CD = pd.VALN_TYP_CD ,
# MAGIC pr.REQD_DLVY_DT = pd.REQD_DLVY_DT ,
# MAGIC pr.REQD_DLYY_SEAS = pd.REQD_DLYY_SEAS ,
# MAGIC pr.BASE_UOM_CD = pd.BASE_UOM_CD ,
# MAGIC pr.LCL_CURR_CD = pd.LCL_CURR_CD ,
# MAGIC pr.OPN_QTY = pd.OPN_QTY ,
# MAGIC pr.OPN_COST_AMT = pd.OPN_COST_AMT ,
# MAGIC pr.SNPST_DT = pd.SNPST_DT,
# MAGIC pr.SNPST_DT_MTH_NBR = pd.SNPST_DT_MTH_NBR,
# MAGIC pr.SNPST_DT_YR_NBR = pd.SNPST_DT_YR_NBR ,
# MAGIC pr.MTRL_PLNT_STAT_CD = pd.mtrl_plnt_stat_cd_calc ,
# MAGIC pr.ofrng_mthd_cd = pd.ofrng_mthd_cd ,
# MAGIC pr.rgn_cd = pd.region,
# MAGIC pr.EDW_CRT_TS = pd.EDW_CRT_TS ,
# MAGIC pr.EDW_UPDT_TS = pd.EDW_UPDT_TS ,
# MAGIC pr.EDW_HASH_CHK = pd.EDW_HASH_CHK ,
# MAGIC pr.EDW_ACTV_FLG = pd.EDW_ACTV_FLG
# MAGIC
# MAGIC WHEN NOT MATCHED
# MAGIC THEN INSERT
# MAGIC
# MAGIC (
# MAGIC SLS_DOC_NBR
# MAGIC ,SLS_DOC_ITEM_NBR
# MAGIC ,SLS_DOC_SCHD_LIN
# MAGIC ,RQRT_TYP
# MAGIC ,PLNT_CD
# MAGIC ,MTRL_NBR
# MAGIC ,SIZE_DIM_CD
# MAGIC ,ITEM_CATG_CD
# MAGIC ,DOC_CATG_CD
# MAGIC ,SOLD_TO_CUST_NBR
# MAGIC ,RQRT_CATG_CD
# MAGIC ,VALN_TYP_CD
# MAGIC ,REQD_DLVY_DT
# MAGIC ,REQD_DLYY_SEAS
# MAGIC ,BASE_UOM_CD
# MAGIC ,LCL_CURR_CD
# MAGIC ,OPN_QTY
# MAGIC ,OPN_COST_AMT
# MAGIC ,SNPST_DT
# MAGIC ,SNPST_DT_MTH_NBR
# MAGIC ,SNPST_DT_YR_NBR
# MAGIC ,mtrl_plnt_stat_cd
# MAGIC ,ofrng_mthd_cd
# MAGIC ,rgn_cd
# MAGIC ,EDW_CRT_TS
# MAGIC ,EDW_UPDT_TS
# MAGIC ,EDW_HASH_CHK
# MAGIC ,EDW_ACTV_FLG
# MAGIC )
# MAGIC
# MAGIC VALUES
# MAGIC (
# MAGIC  pd.SLS_DOC_NBR
# MAGIC ,pd.SLS_DOC_ITEM_NBR
# MAGIC ,pd.SLS_DOC_SCHD_LIN
# MAGIC ,pd.RQRT_TYP
# MAGIC ,pd.PLNT_CD
# MAGIC ,pd.MTRL_NBR
# MAGIC ,pd.SIZE_DIM_CD
# MAGIC ,pd.ITEM_CATG_CD
# MAGIC ,pd.DOC_CATG_CD
# MAGIC ,pd.SOLD_TO_CUST_NBR
# MAGIC ,pd.RQRT_CATG_CD
# MAGIC ,pd.VALN_TYP_CD
# MAGIC ,pd.REQD_DLVY_DT
# MAGIC ,pd.REQD_DLYY_SEAS
# MAGIC ,pd.BASE_UOM_CD
# MAGIC ,pd.LCL_CURR_CD
# MAGIC ,pd.OPN_QTY
# MAGIC ,pd.OPN_COST_AMT
# MAGIC ,pd.SNPST_DT
# MAGIC ,pd.SNPST_DT_MTH_NBR
# MAGIC ,pd.SNPST_DT_YR_NBR
# MAGIC ,pd.mtrl_plnt_stat_cd_calc
# MAGIC ,pd.ofrng_mthd_cd
# MAGIC ,pd.region
# MAGIC ,pd.EDW_CRT_TS
# MAGIC ,pd.EDW_UPDT_TS
# MAGIC ,pd.EDW_HASH_CHK
# MAGIC ,pd.EDW_ACTV_FLG
# MAGIC )


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

import os.path
import re
from tkinter import ttk
import traceback

import thonny
from thonny import memory, roughparse
from thonny.common import ToplevelCommand, parse_shell_command
from thonny.misc_utils import running_on_mac_os, shorten_repr
from thonny.ui_utils import EnhancedTextWithLogging
import tkinter as tk
from thonny.globals import get_workbench, get_runner
from thonny.codeview import EDIT_BACKGROUND, PythonText
from thonny.tktextext import index2line

class ShellView (ttk.Frame):
    def __init__(self, master, **kw):
        ttk.Frame.__init__(self, master, **kw)

        self.vert_scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL)
        self.vert_scrollbar.grid(row=0, column=2, sticky=tk.NSEW)
        self.text = ShellText(self,
                            font=get_workbench().get_font("EditorFont"),
                            #foreground="white",
                            #background="#666666",
                            highlightthickness=0,
                            #highlightcolor="LightBlue",
                            borderwidth=0,
                            yscrollcommand=self.vert_scrollbar.set,
                            padx=4,
                            insertwidth=2,
                            height=10,
                            undo=True)

        get_workbench().event_generate("ShellTextCreated", text_widget=self.text)
        get_workbench().add_command("clear_shell", "edit", "Clear shell",
                                    self.clear_shell,
                                    group=200)

        self.text.grid(row=0, column=1, sticky=tk.NSEW)
        self.vert_scrollbar['command'] = self.text.yview
        self.columnconfigure(1, weight=1)
        self.rowconfigure(0, weight=1)

    def focus_set(self):
        self.text.focus_set()

    def add_command(self, command, handler):
        self.text.add_command(command, handler)

    def submit_command(self, cmd_line):
        self.text.submit_command(cmd_line)

    def clear_shell(self):
        self.text._clear_shell()

    def report_exception(self, prelude=None, conclusion=None):
        if prelude is not None:
            self.text.direct_insert("end", prelude + "\n", ("stderr",))

        self.text.direct_insert("end", traceback.format_exc() + "\n", ("stderr",))

        if conclusion is not None:
            self.text.direct_insert("end", conclusion + "\n", ("stderr",))

class ShellText(EnhancedTextWithLogging, PythonText):

    def __init__(self, master, cnf={}, **kw):
        if not "background" in kw:
            kw["background"] = EDIT_BACKGROUND

        EnhancedTextWithLogging.__init__(self, master, cnf, **kw)
        self.bindtags(self.bindtags() + ('ShellText',))

        self._before_io = True
        self._command_history = [] # actually not really history, because each command occurs only once
        self._command_history_current_index = None

        """
        self.margin = tk.Text(self,
                width = 4,
                padx = 4,
                highlightthickness = 0,
                takefocus = 0,
                bd = 0,
                #font = self.font,
                cursor = "dotbox",
                background = '#e0e0e0',
                foreground = '#999999',
                #state='disabled'
                )
        self.margin.grid(row=0, column=0)
        """

        self.bind("<Up>", self._arrow_up, True)
        self.bind("<Down>", self._arrow_down, True)
        self.bind("<KeyPress>", self._text_key_press, True)
        self.bind("<KeyRelease>", self._text_key_release, True)

        prompt_font = get_workbench().get_font("BoldEditorFont")
        vert_spacing = 10
        io_indent = 16
        code_indent = prompt_font.measure(">>> ")

        self.tag_configure("toplevel", font=get_workbench().get_font("EditorFont"))
        self.tag_configure("prompt", foreground="purple", font=prompt_font)
        self.tag_configure("command", foreground="black",
                           lmargin1=code_indent, lmargin2=code_indent)
        self.tag_configure("welcome", foreground="DarkGray", font=get_workbench().get_font("EditorFont"))
        self.tag_configure("automagic", foreground="DarkGray", font=get_workbench().get_font("EditorFont"))
        self.tag_configure("value", foreground="DarkBlue")
        self.tag_configure("error", foreground="Red")

        self.tag_configure("io", lmargin1=io_indent, lmargin2=io_indent, rmargin=io_indent,
                                font=get_workbench().get_font("IOFont"))
        self.tag_configure("stdin", foreground="Blue")
        self.tag_configure("stdout", foreground="Black")
        self.tag_configure("stderr", foreground="Red")
        self.tag_configure("hyperlink", foreground="#3A66DD", underline=True)
        self.tag_bind("hyperlink", "<ButtonRelease-1>", self._handle_hyperlink)
        self.tag_bind("hyperlink", "<Enter>", self._hyperlink_enter)
        self.tag_bind("hyperlink", "<Leave>", self._hyperlink_leave)

        self.tag_configure("vertically_spaced", spacing1=vert_spacing)
        self.tag_configure("inactive", foreground="#aaaaaa")

        # create 3 marks: input_start shows the place where user entered but not-yet-submitted
        # input starts, output_end shows the end of last output,
        # output_insert shows where next incoming program output should be inserted
        self.mark_set("input_start", "end-1c")
        self.mark_gravity("input_start", tk.LEFT)

        self.mark_set("output_end", "end-1c")
        self.mark_gravity("output_end", tk.LEFT)

        self.mark_set("output_insert", "end-1c")
        self.mark_gravity("output_insert", tk.RIGHT)

        self.active_object_tags = set()

        self._command_handlers = {}

        self._last_configuration = None

        get_workbench().bind("InputRequest", self._handle_input_request, True)
        get_workbench().bind("ProgramOutput", self._handle_program_output, True)
        get_workbench().bind("ToplevelResult", self._handle_toplevel_result, True)

        self._init_menu()

    def _init_menu(self):
        self._menu = tk.Menu(self, tearoff=False)
        self._menu.add_command(label="Clear shell", command=self._clear_shell)

    def add_command(self, command, handler):
        self._command_handlers[command] = handler

    def submit_command(self, cmd_line):
        assert get_runner().get_state() == "waiting_toplevel_command"
        self.delete("input_start", "end")
        self.insert("input_start", cmd_line, ("automagic",))
        self.see("end")
        self.mark_set("insert", "end")
        self._try_submit_input()

    def _handle_input_request(self, msg):
        self["font"] = get_workbench().get_font("IOFont") # otherwise the cursor is of toplevel size
        self.focus_set()
        self.mark_set("insert", "end")
        self.tag_remove("sel", "1.0", tk.END)
        self._current_input_request = msg
        self._try_submit_input() # try to use leftovers from previous request
        self.see("end")

    def _handle_program_output(self, msg):
        self["font"] = get_workbench().get_font("IOFont")

        # mark first line of io
        if self._before_io:
            self._insert_text_directly(msg.data[0], ("io", msg.stream_name, "vertically_spaced"))
            self._before_io = False
            self._insert_text_directly(msg.data[1:], ("io", msg.stream_name))
        else:
            self._insert_text_directly(msg.data, ("io", msg.stream_name))

        self.mark_set("output_end", self.index("end-1c"))
        self.see("end")

    def _handle_toplevel_result(self, msg):
        self["font"] = get_workbench().get_font("EditorFont")
        self._before_io = True
        if hasattr(msg, "error"):
            self._insert_text_directly(msg.error + "\n", ("toplevel", "error"))

        if hasattr(msg, "welcome_text"):
            configuration = get_workbench().get_option("run.backend_configuration")
            welcome_text = msg.welcome_text
            if hasattr(msg, "executable") and msg.executable != thonny.running.get_private_venv_executable():
                welcome_text += " (" + msg.executable + ")"
            if (configuration != self._last_configuration
                and not (self._last_configuration is None and not configuration)):
                    self._insert_text_directly(welcome_text, ("welcome",))

            self._last_configuration = get_workbench().get_option("run.backend_configuration")

        if hasattr(msg, "value_info"):
            value_repr = shorten_repr(msg.value_info["repr"], 10000)
            if value_repr != "None":
                if get_workbench().in_heap_mode():
                    value_repr = memory.format_object_id(msg.value_info["id"])
                object_tag = "object_" + str(msg.value_info["id"])
                self._insert_text_directly(value_repr + "\n", ("toplevel",
                                                               "value",
                                                               object_tag))
                if running_on_mac_os():
                    sequence = "<Command-Button-1>"
                else:
                    sequence = "<Control-Button-1>"
                self.tag_bind(object_tag, sequence,
                                   lambda _: get_workbench().event_generate(
                                        "ObjectSelect", object_id=msg.value_info["id"]))

                self.active_object_tags.add(object_tag)

        self.mark_set("output_end", self.index("end-1c"))
        self._insert_prompt()
        self._try_submit_input() # Trying to submit leftover code (eg. second magic command)
        self.see("end")

    def _insert_prompt(self):
        # if previous output didn't put a newline, then do it now
        if not self.index("output_insert").endswith(".0"):
            self._insert_text_directly("\n", ("io",))

        prompt_tags = ("toplevel", "prompt")

        # if previous line has value or io then add little space
        prev_line = self.index("output_insert - 1 lines")
        prev_line_tags = self.tag_names(prev_line)
        if "io" in prev_line_tags or "value" in prev_line_tags:
            prompt_tags += ("vertically_spaced",)
            #self.tag_add("last_result_line", prev_line)

        self._insert_text_directly(">>> ", prompt_tags)
        self.edit_reset();

    def intercept_insert(self, index, txt, tags=()):
        if (self._editing_allowed()
            and self._in_current_input_range(index)):
            #self._print_marks("before insert")
            # I want all marks to stay in place
            self.mark_gravity("input_start", tk.LEFT)
            self.mark_gravity("output_insert", tk.LEFT)

            if get_runner().get_state() == "waiting_input":
                tags = tags + ("io", "stdin")
            else:
                tags = tags + ("toplevel", "command")

            EnhancedTextWithLogging.intercept_insert(self, index, txt, tags)

            if get_runner().get_state() == "waiting_input":
                if self._before_io:
                    # tag first char of io differently
                    self.tag_add("vertically_spaced", index)
                    self._before_io = False

                self._try_submit_input()

            self.see("insert")
        else:
            self.bell()

    def intercept_delete(self, index1, index2=None, **kw):
        if index1 == "sel.first" and index2 == "sel.last" and not self.has_selection():
            return

        if (self._editing_allowed()
            and self._in_current_input_range(index1)
            and (index2 is None or self._in_current_input_range(index2))):
            self.direct_delete(index1, index2, **kw)
        else:
            self.bell()

    def perform_return(self, event):
        if get_runner().get_state() == "waiting_input":
            # if we are fixing the middle of the input string and pressing ENTER
            # then we expect the whole line to be submitted not linebreak to be inserted
            # (at least that's how IDLE works)
            self.mark_set("insert", "end") # move cursor to the end

            # Do the return without auto indent
            EnhancedTextWithLogging.perform_return(self, event)

            self._try_submit_input()

        elif get_runner().get_state() == "waiting_toplevel_command":
            # Same with editin middle of command, but only if it's a single line command
            whole_input = self.get("input_start", "end-1c") # asking the whole input
            if ("\n" not in whole_input
                and self._code_is_ready_for_submission(whole_input)):
                self.mark_set("insert", "end") # move cursor to the end
                # Do the return without auto indent
                EnhancedTextWithLogging.perform_return(self, event)
            else:
                # Don't want auto indent when code is ready for submission
                source = self.get("input_start", "insert")
                tail = self.get("insert", "end")

                if self._code_is_ready_for_submission(source + "\n", tail):
                    # No auto-indent
                    EnhancedTextWithLogging.perform_return(self, event)
                else:
                    # Allow auto-indent
                    PythonText.perform_return(self, event)

            self._try_submit_input()

        return "break"

    def on_secondary_click(self, event):
        super().on_secondary_click(event)
        self._menu.tk_popup(event.x_root, event.y_root)

    def _in_current_input_range(self, index):
        try:
            return self.compare(index, ">=", "input_start")
        except:
            return False

    def _insert_text_directly(self, txt, tags=()):
        def _insert(txt, tags):
            if txt != "":
                self.direct_insert("output_insert", txt, tags)

        # I want the insertion to go before marks
        #self._print_marks("before output")
        self.mark_gravity("input_start", tk.RIGHT)
        self.mark_gravity("output_insert", tk.RIGHT)
        tags = tuple(tags)

        if "stderr" in tags or "error" in tags:
            # show lines pointing to source lines as hyperlinks
            for line in txt.splitlines(True):
                parts = re.split(r'(File .* line \d+.*)$', line, maxsplit=1)
                if len(parts) == 3 and "<pyshell" not in line:
                    _insert(parts[0], tags)
                    _insert(parts[1], tags + ("hyperlink",))
                    _insert(parts[2], tags)
                else:
                    _insert(line, tags)
        else:
            _insert(txt, tags)

        #self._print_marks("after output")
        # output_insert mark will move automatically because of its gravity

    def _try_submit_input(self):
        # see if there is already enough inputted text to submit
        input_text = self.get("input_start", "insert")
        tail = self.get("insert", "end")

        # user may have pasted more text than necessary for this request
        submittable_text = self._extract_submittable_input(input_text, tail)

        if submittable_text is not None:
            if get_runner().get_state() == "waiting_toplevel_command":
                # clean up the tail
                if len(tail) > 0:
                    assert tail.strip() == ""
                    self.delete("insert", "end-1c")

            # leftover text will be kept in widget, waiting for next request.
            start_index = self.index("input_start")
            end_index = self.index("input_start+{0}c".format(len(submittable_text)))

            # apply correct tags (if it's leftover then it doesn't have them yet)
            if get_runner().get_state() == "waiting_input":
                self.tag_add("io", start_index, end_index)
                self.tag_add("stdin", start_index, end_index)
            else:
                self.tag_add("toplevel", start_index, end_index)
                self.tag_add("command", start_index, end_index)

            # update start mark for next input range
            self.mark_set("input_start", end_index)

            # Move output_insert mark after the requested_text
            # Leftover input, if any, will stay after output_insert,
            # so that any output that will come in before
            # next input request will go before leftover text
            self.mark_set("output_insert", end_index)

            # remove tags from leftover text
            for tag in ("io", "stdin", "toplevel", "command"):
                # don't remove automagic, because otherwise I can't know it's auto
                self.tag_remove(tag, end_index, "end")

            self._submit_input(submittable_text)

    def _editing_allowed(self):
        return get_runner().get_state() in ('waiting_toplevel_command', 'waiting_input')

    def _extract_submittable_input(self, input_text, tail):

        if get_runner().get_state() == "waiting_toplevel_command":
            if input_text.endswith("\n"):
                if input_text.strip().startswith("%"):
                    # if several magic command are submitted, then take only first
                    return input_text[:input_text.index("\n")+1]
                elif self._code_is_ready_for_submission(input_text, tail):
                    return input_text
                else:
                    return None
            else:
                return None

        elif get_runner().get_state() == "waiting_input":
            input_request = self._current_input_request
            method = input_request.method
            limit = input_request.limit
            # TODO: what about EOF?
            if isinstance(limit, int) and limit < 0:
                limit = None

            if method == "readline":
                # TODO: is it correct semantics?
                i = 0
                if limit == 0:
                    return ""

                while True:
                    if i >= len(input_text):
                        return None
                    elif limit is not None and i+1 == limit:
                        return input_text[:i+1]
                    elif input_text[i] == "\n":
                        return input_text[:i+1]
                    else:
                        i += 1
            else:
                raise AssertionError("only readline is supported at the moment")

    def _code_is_ready_for_submission(self, source, tail=""):
        # Ready to submit if ends with empty line
        # or is complete single-line code

        if tail.strip() != "":
            return False

        # First check if it has unclosed parens, unclosed string or ending with : or \
        parser = roughparse.RoughParser(self.indentwidth, self.tabwidth)
        parser.set_str(source.rstrip() + "\n")
        if (parser.get_continuation_type() != roughparse.C_NONE
                or parser.is_block_opener()):
            return False

        # Multiline compound statements need to end with empty line to be considered
        # complete.
        lines = source.splitlines()
        # strip starting empty and comment lines
        while (len(lines) > 0
               and (lines[0].strip().startswith("#")
                    or lines[0].strip() == "")):
            lines.pop(0)

        compound_keywords = ["if", "while", "for", "with", "try", "def", "class", "async", "await"]
        if len(lines) > 0:
            first_word = lines[0].strip().split()[0]
            if (first_word in compound_keywords
                and not source.replace(" ", "").replace("\t", "").endswith("\n\n")):
                # last line is not empty
                return False

        return True

    def _submit_input(self, text_to_be_submitted):
        if get_runner().get_state() == "waiting_toplevel_command":
            # register in history and count
            if text_to_be_submitted in self._command_history:
                self._command_history.remove(text_to_be_submitted)
            self._command_history.append(text_to_be_submitted)
            self._command_history_current_index = None # meaning command selection is not in process

            try:
                if text_to_be_submitted.startswith("%"):
                    command, _ = parse_shell_command(text_to_be_submitted)
                    get_workbench().event_generate("MagicCommand", cmd_line=text_to_be_submitted)
                    if command in self._command_handlers:
                        self._command_handlers[command](text_to_be_submitted)
                        get_workbench().event_generate("AfterKnownMagicCommand", cmd_line=text_to_be_submitted)
                    else:
                        self._insert_text_directly("Unknown magic command: " + command)
                        self._insert_prompt()
                else:
                    get_runner().send_command(
                        ToplevelCommand(command="execute_source",
                                        source=text_to_be_submitted))

            except:
                get_workbench().report_exception()
                self._insert_prompt()

            get_workbench().event_generate("ShellCommand", command_text=text_to_be_submitted)
        else:
            assert get_runner().get_state() == "waiting_input"
            get_runner().send_program_input(text_to_be_submitted)
            get_workbench().event_generate("ShellInput", input_text=text_to_be_submitted)

    def _arrow_up(self, event):
        if not self._in_current_input_range("insert"):
            return

        insert_line = index2line(self.index("insert"))
        input_start_line = index2line(self.index("input_start"))
        if insert_line != input_start_line:
            # we're in the middle of a multiline command
            return

        if len(self._command_history) == 0 or self._command_history_current_index == 0:
            # can't take previous command
            return "break"

        if self._command_history_current_index is None:
            self._command_history_current_index = len(self._command_history)-1
        else:
            self._command_history_current_index -= 1

        cmd = self._command_history[self._command_history_current_index]
        if cmd[-1] == "\n":
            cmd = cmd[:-1] # remove the submission linebreak
        self._propose_command(cmd)
        return "break"

    def _arrow_down(self, event):
        if not self._in_current_input_range("insert"):
            return

        insert_line = index2line(self.index("insert"))
        last_line = index2line(self.index("end-1c"))
        if insert_line != last_line:
            # we're in the middle of a multiline command
            return

        if (len(self._command_history) == 0
            or self._command_history_current_index == len(self._command_history)-1):
            # can't take next command
            return "break"

        if self._command_history_current_index is None:
            self._command_history_current_index = len(self._command_history)-1
        else:
            self._command_history_current_index += 1

        self._propose_command(self._command_history[self._command_history_current_index].strip("\n"))
        return "break"

    def _propose_command(self, cmd_line):
        self.delete("input_start", "end")
        self.intercept_insert("input_start", cmd_line)
        self.see("insert")

    def _text_key_press(self, event):
        # TODO: this underline may confuse, when user is just copying on pasting
        # try to add this underline only when mouse is over the value
        """
        if event.keysym in ("Control_L", "Control_R", "Command"):  # TODO: check in Mac
            self.tag_configure("value", foreground="DarkBlue", underline=1)
        """

    def _text_key_release(self, event):
        if event.keysym in ("Control_L", "Control_R", "Command"):  # TODO: check in Mac
            self.tag_configure("value", foreground="DarkBlue", underline=0)

    def _clear_shell(self):
        end_index = self.index("output_end")
        self.direct_delete("1.0", end_index)

    def compute_smart_home_destination_index(self):
        """Is used by EnhancedText"""

        if self._in_current_input_range("insert"):
            # on input line, go to just after prompt
            return "input_start"
        else:
            return super().compute_smart_home_destination_index()

    def _hyperlink_enter(self, event):
        self.config(cursor="hand2")

    def _hyperlink_leave(self, event):
        self.config(cursor="")

    def _handle_hyperlink(self, event):
        try:
            line = self.get("insert linestart", "insert lineend")
            matches = re.findall(r'File "([^"]+)", line (\d+)', line)
            if len(matches) == 1 and len(matches[0]) == 2:
                filename, lineno = matches[0]
                lineno = int(lineno)
                if os.path.exists(filename) and os.path.isfile(filename):
                    # TODO: better use events instead direct referencing
                    get_workbench().get_editor_notebook().show_file(filename, lineno)
        except:
            traceback.print_exc()

    def _invalidate_current_data(self):
        """
        Grayes out input & output displayed so far
        """
        end_index = self.index("output_end")

        self.tag_add("inactive", "1.0", end_index)
        self.tag_remove("value", "1.0", end_index)

        while len(self.active_object_tags) > 0:
            self.tag_remove(self.active_object_tags.pop(), "1.0", "end")


from flask import Flask,render_template,request, redirect,url_for
from database import *
app = Flask(__name__)

@app.route('/add_event',methods=["POST","GET"])
def addevent():
	if request.method=="POST":
		nameOfEvent = request.form['nameOfEvent']
		date = request.form['date']
		discription=request.form['discription']
		email=request.form['email']
		nameOfCreater=request.form['nameOfCreater']
		requirements=request.form['requirements']
		phoneNumber=request.form['phoneNumber']
		numberOfVolunteers=request.form['numberOfVolunteers']
		pic=request.form['pic']
		add_event(nameOfEvent,date,email, discription, nameOfCreater, phoneNumber, numberOfVolunteers, requirements, pic)
		return render_template("addevent.html")

	else:
		return render_template("addevent.html")

@app.route('/')
def home():
    return redirect(url_for("events"))

@app.route('/about')
def about():
    return render_template("about.html")

@app.route('/events')
def events():
	events=get_all_events()
	return render_template("events.html",events=events)

@app.route('/viewevent/<int:event_id>')
def viewevent(event_id):
	event=get_all_events_by_id(event_id)
	return render_template("viewevent.html",event=event)

@app.route('/volunform/<int:event_id>', methods=["POST"])
def volunform(event_id):
	event = get_all_events_by_id(event_id)
	volunteer(event.nameOfEvent)
	return redirect(url_for("viewevent",event_id=event_id))
if __name__ == '__main__':
    app.run(debug=True)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License,
    or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, see <http://www.gnu.org/licenses/>.

    @author: jeix
"""

import re
from pycurl import HTTPHEADER
from module.plugins.internal.SimpleHoster import SimpleHoster, create_getInfo, timestamp
from module.common.json_layer import json_loads

class UploadingCom(SimpleHoster):
    __name__ = "UploadingCom"
    __type__ = "hoster"
    __pattern__ = r"http://(?:www\.)?uploading\.com/files/(?:get/)?(?P<ID>[\w\d]+)"
    __version__ = "0.32"
    __description__ = """Uploading.Com File Download Hoster"""
    __author_name__ = ("jeix", "mkaay", "zoidberg")
    __author_mail__ = ("jeix@hasnomail.de", "mkaay@mkaay.de", "zoidberg@mujmail.cz")

    FILE_NAME_PATTERN = r'<title>Download (?P<N>.*?) for free on uploading.com</title>'
    FILE_SIZE_PATTERN = r'File size: (?P<S>.*?)'
    FILE_OFFLINE_PATTERN = r'
The requested file is not found
'

    def process(self, pyfile):
        # set lang to english
        self.req.cj.setCookie("uploading.com", "lang", "1")
        self.req.cj.setCookie("uploading.com", "language", "1")
        self.req.cj.setCookie("uploading.com", "setlang", "en")
        self.req.cj.setCookie("uploading.com", "_lang", "en")

        if not "/get/" in self.pyfile.url:
            self.pyfile.url = self.pyfile.url.replace("/files", "/files/get")

        self.html = self.load(pyfile.url, decode = True)
        self.file_info = self.getFileInfo()

        if self.premium:
            self.handlePremium()
        else:
            self.handleFree()

    def handlePremium(self):
        postData = {'action': 'get_link',
                    'code': self.file_info['ID'],
                    'pass': 'undefined'}

        self.html = self.load('http://uploading.com/files/get/?JsHttpRequest=%d-xml' % timestamp(), post=postData)
        url = re.search(r'"link"\s*:\s*"(.*?)"', self.html)
        if url:
            url = url.group(1).replace("\\/", "/")
            self.download(url)

        raise Exception("Plugin defect.")

    def handleFree(self):
        found = re.search('
((Daily )?Download Limit)
', self.html)
        if found:
            self.pyfile.error = found.group(1)
            self.logWarning(self.pyfile.error)
            self.retry(max_tries=6, wait_time = 21600 if found.group(2) else 900, reason = self.pyfile.error)

        ajax_url = "http://uploading.com/files/get/?ajax"
        self.req.http.c.setopt(HTTPHEADER, ["X-Requested-With: XMLHttpRequest"])
        self.req.http.lastURL = self.pyfile.url

        response = json_loads(self.load(ajax_url, post = {'action': 'second_page', 'code': self.file_info['ID']}))
        if 'answer' in response and 'wait_time' in response['answer']:
            wait_time = int(response['answer']['wait_time'])
            self.log.info("%s: Waiting %d seconds." % (self.__name__, wait_time))
            self.setWait(wait_time)
            self.wait()
        else:
            self.pluginParseError("AJAX/WAIT")

        response = json_loads(self.load(ajax_url, post = {'action': 'get_link', 'code': self.file_info['ID'], 'pass': 'false'}))
        if 'answer' in response and 'link' in response['answer']:
            url = response['answer']['link']
        else:
            self.pluginParseError("AJAX/URL")

        self.html = self.load(url)
        found = re.search(r'<form id="file_form" action="(.*?)"', self.html)
        if found:
            url = found.group(1)
        else:
            self.pluginParseError("URL")

        self.download(url)

        check = self.checkDownload({"html" : re.compile("\A<!DOCTYPE html PUBLIC")})
        if check == "html":
            self.logWarning("Redirected to a HTML page, wait 10 minutes and retry")
            self.setWait(600, True)
            self.wait()

getInfo = create_getInfo(UploadingCom)


from sqlalchemy.engine import create_engine
from sqlalchemy.orm.scoping import scoped_session
from sqlalchemy.orm.session import sessionmaker

class SQLAlchemy:
    def __init__(self, app=None):
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        if not hasattr(app, 'extensions'):
            app.extensions = {}

        app.extensions['sqlalchemy'] = self
        pool_size = app.config.get('SQLALCHEMY_POOL_SIZE', 5)
        max_overflow = app.config.get('SQLALCHEMY_MAX_OVERFLOW', 15)

        self.engine = create_engine(app.config['SQLALCHEMY_URL'], pool_size=pool_size, max_overflow=max_overflow)
        self.session = scoped_session(sessionmaker(self.engine))
        app.teardown_appcontext(self.close_session)

    def close_session(self, response_or_exc):
        try:
            if response_or_exc is None and self.session.is_active:
                self.session.commit()
        finally:
            self.session.remove()

        return response_or_exc


fname = 'phrase_YELP.emb'
fname2 = 'phrase_YELP.txt'

with open(fname) as f:
    lines = f.readlines()

output = open(fname2, 'w')

for l in lines:
    tokens = l.split()
    if len(tokens) <= 0 or tokens[0][0] != '_' or len(tokens[0])<=1:
        continue
    output.write(l)

output.close()


# -*- coding: UTF-8 -*-
################################################################################
#
# Copyright (c) 2021 Baidu.com, Inc. All Rights Reserved
#
################################################################################
"""配置读取封装模块"""
import configparser
import os
import logging

import read_urls

class Config(object):
    """配置类"""

    def __int__(self, config_path):
        self.config_path = config_path
        self.cfg = configparser.ConfigParser()

    # get_value获取全部的字符串，section区域名, option选项名
    def get_value(self, section, option):
        self.cfg.read(self.config_path)
        return self.cfg.get(section, option)

    # get_int获取整型，section区域名, option选项名
    def get_int(self, section, option):
        """
        get_int获取整型
        :param section: 区域名
        :param option: 选项名
        :return:
        """
        self.cfg.read(self.config_path)
        return self.cfg.getint(section, option)

    # get_float获取浮点数类型，section区域名, option选项名
    def get_float(self, section, option):
        self.cfg.read(self.config_path)
        return self.cfg.getfloat(section, option)

    # get_boolean获取布尔类型，section区域名, option选项名
    def get_boolean(self, section, option):
        self.cfg.read(self.config_path)
        return self.cfg.getboolean(section, option)

    # get_eval_data获取列表，section区域名, option选项名
    def get_eval_data(self, section, option):
        self.cfg.read(self.config_path)
        return eval(self.cfg.get(section, option))

    @staticmethod
    def write_config(datas, filename):
        """
        写入配置操做
        :param datas: 须要传入写入的数据
        :param filename: 指定文件名
        :return:
        """
        # 作校验，为嵌套字典的字典才能够（意思.隐私.谈.ce)
        if isinstance(datas, dict):  # 在外层判断是否为字典
            # 再来判断内层的 values 是否为字典
            for value in datas.values():  # 先取出value
                if not isinstance(value, dict):  # 在判断
                    return "数据不合法, 应为嵌套字典的字典"

            config = configparser.ConfigParser()  # 1.建立配置解析器---与写入配置操做一致
            for key in datas:  # 写入操做
                config[key] = datas[key]
            with open(filename, "w") as file:  # 保存到哪一个文件filename=须要指定文件名
                config.write(file)
            # return "写入成功"

    @staticmethod
    def conf_load(filepath):
        log = logging.getLogger('Spider.crawler')
        if not os.path.exists(filepath):
            log.error('config file %s not exist' % filepath)
            return None
        conf = {}
        try:
            conf.urls = Config.get_value('spider', 'feed_file')
            conf.output_dir = Config.get_value('spider', 'result')
            conf.mex_depth = Config.get_int('spider', 'max_depth')
            conf.crawl_interval = Config.get_float('spider', 'crawl_interval')
            conf.time_out = Config.get_float('spider', 'crawl_timeout')
            conf.thread_count = Config.get_int('spider', 'thread_count')
            conf.target_url = read_urls.read_file('../urls')
        except IOError as e:
            log.error("failed to open config file: %s, err: %s" % (filepath, e))
            return None
        except configparser.NoSectionError as e:
            log.error("config parser: no section error: %s" % e)
            return None
        except configparser.NoOptionError as e:
            log.error("config parser: no option error: %s" % e)
            return None
        except Exception as e:
            log.error("unknown error: %s" % e)
            return None
        return conf


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

from bs4 import BeautifulSoup
import boto3

# Get the service resource.
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('book')

response = table.get_item(
    Key={
        'id_title': '1'
    }
)
item = response['Item']
print(item)


"""
Vistas relacionadas con el modelo User (Usuarios)

Vistas
------
- List
- Read
- Create
- Update
- Delete
- ResetPassword
"""
from django.contrib.auth.models import User as main_model
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Q
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.views import View

from .user_forms import frmUser as base_form
from .user_forms import frmUserBottom
from .user_forms import frmUserLeft
from .user_forms import frmUserResetPassword
from .user_forms import frmUserRight
from .user_forms import frmUserTop
from .user_forms import frmUserTopMe
from .user_forms import frmUserTopReadUpdate
from .user_forms import frmUserUpdate
from .user_models import UserProfile
from .views import GenericCreate
from .views import GenericDelete
from .views import GenericList
from .views import GenericRead
from .views import GenericUpdate
from zend_django.templatetags.op_helpers import action_label
from zend_django.templatetags.op_helpers import crud_icon
from zend_django.templatetags.op_helpers import crud_label
from zend_django.templatetags.op_helpers import crud_smart_button
from zend_django.templatetags.utils import GenerateReadCRUDToolbar

def template_base_path(file):
    return 'zend_django/user/' + file + ".html"

class List(GenericList):
    html_template = template_base_path("list")
    titulo = "Usuarios"
    titulo_descripcion = ""
    main_data_model = main_model
    model_name = "user"
    tereapp = 'administrar'

    def get_data(self, search_value=''):
        if '' == search_value:
            return list(
                self.main_data_model.objects.all().order_by('username'))
        else:
            return list(self.main_data_model.objects.filter(
                Q(first_name__icontains=search_value) |
                Q(last_name__icontains=search_value) |
                Q(profile__apellido_materno__icontains=search_value) |
                Q(email__icontains=search_value) |
                Q(username__icontains=search_value)).order_by('username'))

class Read(GenericRead):
    titulo_descripcion = "Usuario"
    model_name = "user"
    base_data_form = base_form
    main_data_model = main_model
    tereapp = 'administrar'

    def get(self, request, pk):
        if not self.main_data_model.objects.filter(pk=pk).exists():
            return HttpResponseRedirect(reverse('item_no_encontrado'))
        obj = self.main_data_model.objects.get(pk=pk)
        try:
            obj.profile
        except ObjectDoesNotExist:
            UserProfile.objects.create(user=obj)
        form = {
            'top': frmUserTopReadUpdate(instance=obj),
            'bottom': frmUserBottom(instance=obj),
            'left': frmUserLeft(instance=obj, initial={
                'apellido_materno': obj.profile.apellido_materno,
                'telefono': obj.profile.telefono,
                'celular': obj.profile.celular,
                'whatsapp': obj.profile.whatsapp,
            }),
            'right': frmUserRight(instance=obj),
        }
        toolbar = GenerateReadCRUDToolbar(
            request, self.model_name, obj, self.main_data_model)
        return render(request, self.html_template, {
            'titulo': obj,
            'titulo_descripcion': self.titulo_descripcion,
            'toolbar': toolbar,
            'footer': False,
            'read_only': True,
            'alertas': [],
            'req_chart': False,
            'search_value': '',
            'forms': {
                'top': [{'form': form['top']}],
                'bottom': [{'form': form['bottom']}],
                'left': [{'form': form['left']}],
                'right': [{'form': form['right']}],
            },
            'tereapp': self.tereapp,
        })

class Create(GenericCreate):
    titulo = "Usuario"
    model_name = "user"
    base_data_form = base_form
    tereapp = 'administrar'

    def get(self, request):
        return self.base_render(request, {
                'top': [{'form': frmUserTop()}],
                'bottom': [{'form': frmUserBottom()}],
                'left': [{'form': frmUserLeft()}],
                'right': [{'form': frmUserRight()}],
            })

    def post(self, request):
        form = self.base_data_form(request.POST)
        form_aux = {
                'top': [{'form': frmUserTop(request.POST)}],
                'bottom': [{'form': frmUserBottom(request.POST)}],
                'left': [{'form': frmUserLeft(request.POST)}],
                'right': [{'form': frmUserRight(request.POST)}],
            }
        if form.is_valid():
            obj = form.save()
            obj.set_password(form.cleaned_data['password'])
            obj.save()
            UserProfile.objects.create(
                apellido_materno=form.cleaned_data['apellido_materno'],
                telefono=form.cleaned_data['telefono'],
                celular=form.cleaned_data['celular'],
                whatsapp=form.cleaned_data['whatsapp'],
                user=obj
            )
            return HttpResponseRedirect(reverse(
                f'{self.model_name}_read',
                kwargs={'pk': obj.pk}))
        return self.base_render(request, form_aux)

class Update(GenericUpdate):
    titulo = "Usuario"
    model_name = "user"
    main_data_model = main_model
    tereapp = 'administrar'

    def base_render(self, request, form):
        return render(request, self.html_template, {
            'titulo': self.titulo,
            'titulo_descripcion': crud_label('update'),
            'toolbar': None,
            'footer': False,
            'read_only': False,
            'alertas': [],
            'req_chart': False,
            'search_value': '',
            'forms': {
                'bottom': [{'form': form['bottom']}],
                'left': [{'form': form['left']}],
                'right': [{'form': form['right']}],
            },
            'tereapp': self.tereapp
        })

    def get(self, request, pk):
        if not self.main_data_model.objects.filter(pk=pk).exists():
            return HttpResponseRedirect(reverse('item_no_encontrado'))
        obj = self.main_data_model.objects.get(pk=pk)
        form = {
            'bottom': frmUserBottom(instance=obj),
            'left': frmUserLeft(instance=obj, initial={
                'apellido_materno': obj.profile.apellido_materno,
                'telefono': obj.profile.telefono,
                'celular': obj.profile.celular,
                'whatsapp': obj.profile.whatsapp,
            }),
            'right': frmUserRight(instance=obj),
        }
        return self.base_render(request, form)

    def post(self, request, pk):
        if not self.main_data_model.objects.filter(pk=pk).exists():
            return HttpResponseRedirect(reverse('item_no_encontrado'))
        obj = self.main_data_model.objects.get(pk=pk)
        form = frmUserUpdate(instance=obj, data=request.POST)
        form_aux = {
            'bottom': frmUserBottom(request.POST),
            'left': frmUserLeft(request.POST),
            'right': frmUserRight(request.POST)
        }
        if form.is_valid():
            obj = form.save()
            obj.profile.apellido_materno = form.cleaned_data[
                'apellido_materno']
            obj.profile.telefono = form.cleaned_data['telefono']
            obj.profile.celular = form.cleaned_data['celular']
            obj.profile.whatsapp = form.cleaned_data['whatsapp']
            obj.profile.save()
            return HttpResponseRedirect(reverse(
                f'{self.model_name}_read',
                kwargs={'pk': obj.pk}))
        return self.base_render(request, form_aux)

class Delete(GenericDelete):
    model_name = "user"
    main_data_model = main_model

class ResetPassword(View):
    """
    Reseteo de contraseñas

    Miembros
    --------
    - main_data_model = User

    Métodos
    -------
    - base_render(request, form)
    - get(request, username='')
    - post(request, username='')
    """
    main_data_model = main_model
    tereapp = 'configuracion'

    def base_render(self, request, form):
        return render(request, "zend_django/html/form.html", {
            'titulo': "Usuario",
            'titulo_descripcion': action_label('reset_password'),
            'toolbar': None,
            'footer': False,
            'read_only': False,
            'alertas': [],
            'req_chart': False,
            'search_value': '',
            'forms': {'top': [{'form': form}]},
            'tereapp': self.tereapp,
        })

    def get(self, request, username=''):
        return self.base_render(request, frmUserResetPassword(
            initial={'username': username}))

    def post(self, request, username=''):
        form = frmUserResetPassword(request.POST)
        if form.is_valid():
            obj = self.main_data_model.objects.filter(
                username=form.cleaned_data['username'])
            if obj.exists():
                obj = obj[0]
                obj.set_password(form.cleaned_data['password'])
                obj.save()
                return render(
                    request,
                    template_base_path('reset_password_ok'), {
                        'titulo': "Usuario",
                        'titulo_descripcion': action_label('reset_password'),
                        'toolbar': None,
                        'footer': False,
                        'read_only': False,
                        'alertas': [],
                        'req_chart': False,
                        'tereapp': self.tereapp,
                    })
            else:
                form.add_error('username', "No existe el usuario")
        return self.base_render(request, form)

class Me(GenericUpdate):
    titulo = "Mi Perfil"
    model_name = "user"
    main_data_model = main_model

    def base_render(self, request, form):
        return render(request, self.html_template, {
            'titulo': self.titulo,
            'titulo_descripcion': None,
            'toolbar': None,
            'footer': False,
            'read_only': False,
            'alertas': [],
            'req_chart': False,
            'search_value': '',
            'forms': {
                'top': [{'title': "Mi acceso", 'form': form['top']}],
                'bottom': [{'title': "Mis datos", 'form': form['bottom']}],
            },
            'tereapp': self.tereapp
        })

    def get(self, request):
        obj = request.user
        form = {
            'bottom': frmUserLeft(instance=obj, initial={
                'apellido_materno': obj.profile.apellido_materno,
                'telefono': obj.profile.telefono,
                'celular': obj.profile.celular,
                'whatsapp': obj.profile.whatsapp,
            }),
            'top': frmUserTopMe(initial={
                'username': obj.username,
            }),
        }
        return self.base_render(request, form)

    def post(self, request):
        obj = request.user
        form = {
            'bottom': frmUserLeft(instance=obj, data=request.POST),
            'top': frmUserTopMe(data=request.POST),
        }
        if form['bottom'].is_valid() and form['top'].is_valid():
            obj = form['bottom'].save()
            obj.username = form['top'].cleaned_data['username']
            if form['top'].cleaned_data['password']:
                obj.set_password(form['top'].cleaned_data['password'])
            obj.save()
            obj.profile.apellido_materno = form['bottom'].cleaned_data[
                'apellido_materno']
            obj.profile.telefono = form['bottom'].cleaned_data['telefono']
            obj.profile.celular = form['bottom'].cleaned_data['celular']
            obj.profile.whatsapp = form['bottom'].cleaned_data['whatsapp']
            obj.profile.save()
        return self.base_render(request, form)


from django.urls import path

from kahawa import views

app_name = 'kahawa'

urlpatterns = [
    path('', views.index, name='index'),
    path('check-credentials',
         views.check_credentials,
         name='check-credentials'),
    path('signup', views.signup, name='signup'),
    path('verify', views.verify, name='verify'),
    path('success', views.success, name='success'),
    path('terms', views.terms, name='terms'),
]


class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        n1 = len(nums1)
        n2 = len(nums2)
        if n1 >= n2:
            main = nums1
            sub = nums2
        else:
            main = nums2
            sub = nums1
        place = 0

        while len(sub) != 0:
            popcorn = sub.pop(0)
            while place < len(main):
                if main[place] >= popcorn:
                    main = main[:place] + [popcorn] + main[place:]
                    place += 1
                    break
                place += 1
                if place == len(main):
                    main = main + [popcorn]

        if (n1+n2)%2 == 1:
            median = main[(n1+n2)//2]
        else:
            median = (main[(n1+n2)//2-1] + main[(n1+n2)//2])/2
        return median


from django.views.generic import View

from modules.response import Response

from .user_model import UserModel

import sys
sys.path.append("../../modules")
sys.path.append("../models")

class User(View, Response):

    def __init__(self):
        super().__init__()
        User.model = UserModel()

    def post(self, request):

        uuid = request.POST.get("uuid")
        data = self.model.get_user(uuid)

        if not data:
            content = {}
            return self.response(content)

        lab_uuid = data["lab_uuid"]
        data["studentId"] = data["student_id"]

        lab_info = self.model.get_lab_info(lab_uuid)
        print(data)
        content = {
            "user": data,
            "lab_info": lab_info
        }

        return self.response(content)


import pandas as pd
import numpy as np
from sklearn.manifold import TSNE as TSNE
import matplotlib.pyplot as plt
import json
import plotly
import plotly.graph_objs as go

def makeComparisonGraph(af,q4):
    merged = pd.merge(q4, af, on="listener_id")
    merged = merged.dropna()

    af_numeric = merged.copy()
    del af_numeric['listener_id']
    del af_numeric['username']

    af_numeric_embedded = TSNE(n_components=2).fit_transform(af_numeric)
    tsne_x = np.zeros(len(af_numeric.index))
    tsne_y = np.zeros(len(af_numeric.index))

    for i in range(len(af_numeric_embedded)):
        tsne_x[i] = af_numeric_embedded[i][0]
        tsne_y[i] = af_numeric_embedded[i][1]

    names = merged['username']

    data3 = [go.Scatter(
        x=tsne_x,
        y=tsne_y,
        mode="markers",
        hovertext=names
    )]

    #fig.update_layout(title_text="Spotify Share Music Taste Universe")
    #fig.update_xaxes(showticklabels=False)
    #fig.update_yaxes(showticklabels=False)

    # data3 = [fig]
    graphJSON3 = json.dumps(data3, cls=plotly.utils.PlotlyJSONEncoder)

    return graphJSON3


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('homepage', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='ratings',
            name='feedbackid',
            field=models.ForeignKey(db_column='feedbackid', default='lololol', to='homepage.Feedbacks'),
        ),
    ]


import atexit
from Adafruit_MotorHAT import Adafruit_MotorHAT

class Motor(object):
    """Used to update speed of the Jetbot motors.

    Args:
        driver: An `Adafruit_MotorHAT` instance used to control the motor.
        channel: Motor channel. Left is channel 1 and right is channel 2.
        alpha: Motor configuration parameter.
        beta: Motor configuration parameter.
    """
    def __init__(self, driver, channel, alpha=1.0, beta=0.0, *args, **kwargs):
        self._driver = driver
        self._motor = self._driver.getMotor(channel)
        self.alpha = alpha
        self.beta = beta
        self.value = 0
        atexit.register(self._release)  # Release at exit

    def update_value(self, value):
        """Sets motor value between [-1, 1]."""
        self.value = value
        mapped_value = int(255.0 * (self.alpha * value + self.beta))
        speed = min(max(abs(mapped_value), 0), 255)
        self._motor.setSpeed(speed)
        if mapped_value < 0:
            self._motor.run(Adafruit_MotorHAT.FORWARD)
        else:
            self._motor.run(Adafruit_MotorHAT.BACKWARD)

    def _release(self):
        """Stops motor by releasing control."""
        self._motor.run(Adafruit_MotorHAT.RELEASE)


#!/usr/bin/env python3

import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", category=DeprecationWarning)

import os
import random
import argparse
import shutil

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--src', help='Path of source directory', type=str)
    parser.add_argument('--ext', help='Extension of files to move. Default = .png', type=str)
    parser.add_argument('--target', help='Path of target directory', type=str)
    parser.add_argument('--count', help='Number of items to be moved. Default = 1', type=str)
    parser.add_argument('--all', help='Moves all files, overrides random',default=False,action='store_true')
    args = parser.parse_args()

    if args.src is None:
        print("Please specify the source directory")
        exit(1)

    if args.target is None:
        print("Please specify the target directory")
        exit(1)

    if args.ext is None:
        args.ext='.png'

    if args.count is None:
        args.count=1

    print("Moving.. ")

    if args.all:
        print('In args all')
        for file in os.listdir(args.src):
            print(file)
            if file.endswith(args.ext):
                shutil.move(args.src+"\\"+file,args.target+"\\"+file)
    else:
        i=1
        while i <= int(args.count):
            file_name=random.choice(os.listdir(args.src))
            shutil.move(args.src+"\\"+file_name,args.target+"\\"+file_name)
            i+=1

if __name__ == '__main__':
    main()


from django.shortcuts import render_to_response, render
from web.models import Items, Apartments, Interests, Notifications
from django.http import HttpResponse
from forms import ItemForm, NotificationForm
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.contrib.auth.decorators import login_required
from django.db.models import Q

#main view: displays the items available
@login_required
def main(request):
    args = {}
    args.update(csrf(request))
    args['items'] = Items.objects.filter(status="Not Sold")
    args['notifications'] = Notifications.objects.filter(requestedTo=request.user, state="Pending")
    args['notifications_count'] = args['notifications'].count()
    args['transactions'] = Notifications.objects.filter(requestedFrom=request.user).order_by('item', '-bid')
    args['transactions_count'] = args['transactions'].count()
    args['online_user'] = request.user

    return render_to_response('main.html', args)

#showItem view: displays the item that was clicked on
@login_required
def showItem(request, item_id=1):
    if request.POST:
        form = NotificationForm(request.POST)
        if form.is_valid():
            #do not save yet, and add in default owners_id
            obj = form.save(commit=False)
            #add in the creator's id
            obj.requestedFrom = request.user
            obj.requestedTo = Items.objects.get(id=item_id).owners
            obj.item = Items.objects.get(id=item_id)
            obj.state = "Pending"
            #save data
            obj.save()

            return HttpResponseRedirect('/web/main/')
    else:
        form = NotificationForm()

    args = {}
    args.update(csrf(request))

    args['form'] = form
    args['item'] = Items.objects.get(id=item_id)
    args['notifications'] = Notifications.objects.filter(requestedTo=request.user, state="Pending")
    args['notifications_count'] = args['notifications'].count()
    args['transactions'] = Notifications.objects.filter(requestedFrom=request.user).order_by('item', '-bid')
    args['transactions_count'] = args['transactions'].count()
    args['online_user'] = request.user

    return render_to_response('showItem.html', args)

#createItem view: displays a form to add an item
@login_required
def createItem(request):
    if request.POST:
        form = ItemForm(request.POST, request.FILES)
        if form.is_valid():
            #do not save yet, and add in default owners_id
            obj = form.save(commit=False)
            #add in the creator's id
            obj.owners = request.user
            #add item status
            obj.status = "Not Sold"
            #save data
            obj.save()

            return HttpResponseRedirect('/web/main/')
    else:
        form = ItemForm()

    args = {}
    args.update(csrf(request))

    args['form'] = form
    args['notifications'] = Notifications.objects.filter(requestedTo=request.user, state="Pending")
    args['notifications_count'] = args['notifications'].count()
    args['transactions'] = Notifications.objects.filter(requestedFrom=request.user).order_by('item', '-bid')
    args['transactions_count'] = args['transactions'].count()
    args['online_user'] = request.user

    return render_to_response('createItem.html', args)

@login_required
def search_items(request):
    if request.method == 'POST':
        search_text = request.POST['search_text']
    else:
        search_text = ''

    #filter search by ignoring case and finding substring
    items = Items.objects.filter(name__icontains=search_text)

    return render_to_response('ajax_search.html',
                              { 'items': items })

#requests view: displays the users items
@login_required
def requests(request):
    if request.POST:
        notification_id = request.POST["notification_id"]
        item_id = request.POST["item_id"]

        notification = Notifications.objects.get(pk=notification_id)
        notification.state = "Accepted"
        notification.save()
        Notifications.objects.filter(item=notification.item,requestedTo=notification.requestedTo).exclude(pk=notification.id).update(state='Denied')

        Items.objects.filter(pk=item_id).update(status="Sold")

        return HttpResponseRedirect('/web/requests/')

    args = {}
    args.update(csrf(request))
    args['notifications'] = Notifications.objects.filter(requestedTo=request.user, state="Pending").order_by('item', '-bid')
    args['notifications_count'] = args['notifications'].count()
    args['transactions'] = Notifications.objects.filter(requestedFrom=request.user).order_by('item', '-bid')
    args['transactions_count'] = args['transactions'].count()
    args['online_user'] = request.user

    return render_to_response('requests.html', args)

#transactions view: displays the users items
@login_required
def transactions(request):
    args = {}
    args['notifications'] = Notifications.objects.filter(requestedTo=request.user, state="Pending").order_by('item', '-bid')
    args['notifications_count'] = args['notifications'].count()
    args['transactions'] = Notifications.objects.filter(requestedFrom=request.user).order_by('item', '-bid')
    args['transactions_count'] = args['transactions'].count()
    args['online_user'] = request.user

    return render_to_response('transactions.html', args)