import pytest
import System
import Student

# IMPORTANT - The code only uses assignments from 'comp_sci'
#                         but each course has only "assignment1" and "assignment2" by default
#                         so it will always pass pytest unless you add and check an alternate\
#                         will add a section to create the assignment here
def test_view_assignments(grading_system):

    #added to ensure there is the appropriate assignment tested for
    grading_system.login('goggins', 'augurrox')
    grading_system.usr.create_assignment('assignment5', '03/10/20', 'databases')

    # actual test
    grading_system.login('akend3', '123454321')
    grading_system.usr.view_assignments('databases')
    assert 'assignment5' in grading_system.usr.users['akend3']['courses']['databases']

@pytest.fixture
def grading_system():
    gradingSystem = System.System()
    gradingSystem.load_data()
    return gradingSystem

# fails pytest

CS_4320 Software Development 1/assignments/assignment-five/05-testing/test_view_assignments.py


class ExamException(Exception):
    pass

class CSVTimeSeriesFile():
    #Istanzio il nome del file in cui sono presenti i dati
    def __init__(self,name):
        self.name = name
    #Classe get_data che ritornerà una lista di liste contenenti i dati delle misurazioni
    def get_data(self):
        #Apro il file "data.csv"i
        try:
            my_file = open(self.name, 'r')
            my_file.readline()
        except Exception as e:
            raise ExamException('Errore in apertura del file: "{}"'.format(e))

        #Lista di appoggio per manipolare i dati
        elements = []
        #Lista che verrà usata per ritornare i dati
        list = []
        #Ciclo for che assegna a line ogni volta un elemento diverso di my_file
        prev_element = None

        for line in my_file:
                #Divido line in 2 parti sulla virgola per dividere  i giorni dalle temperature
            elements = line.split(',')

            #Escludo la prima colonna in cui è presente l'intestazione
            if elements[0] != 'epoch':
                    #Elimino il carattere \n dal secondo elemento di "elements"

                try:
                    elements[1] = elements[1].strip('\n')
                    elements[0] = float(elements[0])
                    if isinstance(elements[0], float):
                        elements[0] = int(elements[0])

                    elements[1] = float(elements[1])
                except:
                    continue
                if prev_element is not None and elements[0] < prev_element:
                    raise ExamException("Errore! I dati non sono in ordine crescente")
                prev_element = elements[0]
                    #Assegno a list i dati una volta formattati
                list.append(elements)
        for i in range(len(list)):
            for j in range(len(list)):
                    if list[i][0] == list[j][0] and i != j:
                        raise ExamException("Errore! Sono presenti time stamps duplicati")
        #print(list)
        my_file.close()
        return list

time_series_file = CSVTimeSeriesFile(name='data.csv')
time_series = time_series_file.get_data()
#print(time_series)

#Funzione che calcolerà la differenza massima di temperatura di un giorno
def compute_daily_max_difference(time_series):
    temp_difference = []
    one_day_data = []

    for i in range(len(time_series) - 1):
        current_day = time_series[i][0] - (time_series[i][0] % 86400)
        next_day = time_series[i+1][0] - (time_series[i+1][0] % 86400)
        if  current_day == next_day:
            one_day_data.append(time_series[i][1])
        else:
            one_day_data.append(time_series[i][1])
            max_temp = max(one_day_data)
            min_temp = min(one_day_data)
            if len(one_day_data) == 1:
                temp_difference.append(None)
                one_day_data.clear()
            else:
                temp_difference.append(round(max_temp-min_temp, 2))
                if i != len(time_series) - 2:
                    one_day_data.clear()

    if len(one_day_data) == 1 and not (i == len(time_series ) - 1):
        temp_difference.append(None)
    elif i == len(time_series ) - 1:
        one_day_data.append(time_series[i][1])
        one_day_data.append(time_series[i+1][1])
        current_day = time_series[i][0] - (time_series[i][0] % 86400)
        next_day = time_series[i+1][0] - (time_series[i+1][0] % 86400)

        if current_day != next_day:
            temp_difference.append(None)
        else:
            max_temp = max(one_day_data)
            min_temp = min(one_day_data)
            temp_difference.append(round(max_temp-min_temp, 2))
    return temp_difference

max_temp = compute_daily_max_difference(time_series)
#print(max_temp)


from django.shortcuts import render
from django.http import JsonResponse
from tethys_sdk.gizmos import SelectInput, RangeSlider
from django.contrib.auth.decorators import login_required

from .options import *
from .utilities import *
from .app import Gfs as App
from .utilities import new_id

def home(request):
    """
    Controller for the app home page.
    """

    gfsdate = currentgfs()

    variables = SelectInput(
        display_text='Select GFS Variable',
        name='variables',
        multiple=False,
        original=True,
        options=gfs_variables(),
    )

    levels = SelectInput(
        display_text='Available Forecast Levels',
        name='levels',
        multiple=False,
        original=True,
        options=variable_levels()['al'],
    )

    regions = SelectInput(
        display_text='Pick A World Region (ESRI Living Atlas)',
        name='regions',
        multiple=False,
        original=True,
        options=list(worldregions())
    )

    colorscheme = SelectInput(
        display_text='GFS Color Scheme',
        name='colorscheme',
        multiple=False,
        original=True,
        options=wms_colors(),
        initial='rainbow'
    )

    opacity = RangeSlider(
        display_text='GFS Layer Opacity',
        name='opacity',
        min=.5,
        max=1,
        step=.05,
        initial=1,
    )

    gj_color = SelectInput(
        display_text='Boundary Border Colors',
        name='gjClr',
        multiple=False,
        original=True,
        options=geojson_colors(),
        initial='#ffffff'
    )

    gj_opacity = RangeSlider(
        display_text='Boundary Border Opacity',
        name='gjOp',
        min=0,
        max=1,
        step=.1,
        initial=1,
    )

    gj_weight = RangeSlider(
        display_text='Boundary Border Thickness',
        name='gjWt',
        min=1,
        max=5,
        step=1,
        initial=2,
    )

    gj_fillcolor = SelectInput(
        display_text='Boundary Fill Color',
        name='gjFlClr',
        multiple=False,
        original=True,
        options=geojson_colors(),
        initial='rgb(0,0,0,0)'
    )

    gj_fillopacity = RangeSlider(
        display_text='Boundary Fill Opacity',
        name='gjFlOp',
        min=0,
        max=1,
        step=.1,
        initial=.5,
    )

    context = {
        # data options
        'variables': variables,
        'levels': levels,
        'gfsdate': gfsdate,
        'regions': regions,

        # display options
        'colorscheme': colorscheme,
        'opacity': opacity,
        'gjClr': gj_color,
        'gjOp': gj_opacity,
        'gjWt': gj_weight,
        'gjFlClr': gj_fillcolor,
        'gjFlOp': gj_fillopacity,

        # metadata
        'app': App.package,
        'githublink': App.githublink,
        'datawebsite': App.datawebsite,
        'version': App.version,
        'thredds_url': App.get_custom_setting('thredds_url'),
        'instance_id': new_id(),
    }

    return render(request, 'gfs/home.html', context)

@login_required()
def checkworkflowstatus(request):
    threddspath = os.path.join(App.get_custom_setting('thredds_path'))
    fail = os.path.join(threddspath, 'last_run_failed.txt')
    run = os.path.join(threddspath, 'running.txt')
    last = os.path.join(threddspath, 'last_run.txt')
    if os.path.isfile(last):
        with open(last, 'r') as f:
            return JsonResponse({'succeeded': f.readlines()})
    elif os.path.isfile(run):
        return JsonResponse({'running': 'running'})
    elif os.path.isfile(fail):
        with open(fail, 'r') as f:
            return JsonResponse({'failed': f.readlines()})
    else:
        return JsonResponse({'unknown': 'no indicator files were found'})


from herdingspikes import spikeclass, QualityMeasures
import numpy as np
import numpy.testing as npt
import unittest

class TestHS(unittest.TestCase):
    n1 = 10000
    data1 = np.random.normal(size=(2, n1))
    data2 = np.random.normal(3, 1, size=(2, n1))
    data = np.hstack([data1, data2])
    n = 2*n1
    princcomp = np.random.random((2, n))

    def test_ms_simple(self):
        # the simplest import
        O = spikeclass(self.data)
        self.assertEqual(O.NData(), self.n)
        # combined mean shift, expected result 2 clusters
        O.CombinedMeanShift(2., 0.001, PrincComp=self.princcomp, njobs=1)
        self.assertEqual(O.NClusters(), 2)
        # check ClusterSizes and the cluster sizes
        npt.assert_array_almost_equal(O.ClusterSizes()/self.n,
                                      [0.5, 0.5], decimal=3)

    def test_gaussian_overlap(self):
        O = spikeclass(self.data)
        O.CombinedMeanShift(2., 0.001, PrincComp=self.princcomp, njobs=1)
        fakePCA = np.empty_like(self.data)
        QM = QualityMeasures(O, scorePCA=fakePCA)
        data = []
        for ind in [0, 1]:
            data.append(O.Locations()[:, O.ClusterID() == ind].T)
        estCent = np.array([np.mean(d, axis=0) for d in data])
        g1 = QM._fit_gaussian_mixture(data)
        npt.assert_array_almost_equal(g1.means_, estCent, decimal=1)
        g2 = QM._fit_gaussian_individuals(data)
        npt.assert_array_almost_equal(g2.means_, estCent, decimal=1)
        npt.assert_array_almost_equal(g1.covars_, g2.covars_, decimal=1)
        npt.assert_array_almost_equal(g1.weights_, g2.weights_, decimal=2)

        m1 = QM._data_gaussian_overlap(data, fit_mode="mixture")
        m2 = QM._data_gaussian_overlap(data, fit_mode="individuals")
        npt.assert_array_almost_equal(m1, m2, decimal=2)

if __name__ == '__main__':
    unittest.main()


import tweepy as tw
import datetime, random, smtplib
from datetime import timedelta

auth = tw.OAuthHandler('consumer_key','consumer_secret') #insert consumer_key and consumer_secret
auth.set_access_token('access_token','access_token_secret') #insert access_token and access_token_secret
api = tw.API(auth, wait_on_rate_limit=True)

user = 'naval' #insert the username for whose random favorite tweet you'd like to get over email
all_fav_tweets = []
old_date = datetime.datetime(2019,1,1,0,0,0)
tweets=api.favorites(user,tweet_mode='extended',include_entities=True)

sender_email = "a@gmail.com"  # Enter your address
receiver_email = "d@gmail.com"  # Enter receiver address

for t in tweets:
    all_fav_tweets.append(t._json['id'])
print(f"Fetched {len(all_fav_tweets)} tweets, fetching more")

while (tweets[-1].created_at>old_date and len(all_fav_tweets)<=1000):
    tweets=api.favorites(user,max_id=tweets[-1].id,tweet_mode='extended',include_entities=True)
    for t in tweets:

        all_fav_tweets.append(t._json['id'])
    print(f"Fetched {len(all_fav_tweets)} tweets, fetching more")
random_tweet_id=all_fav_tweets[random.randint(0,len(all_fav_tweets))]
random_tweet=api.get_status(random_tweet_id,tweet_mode='extended',include_entities=True)
random_tweet_text=random_tweet._json['full_text']
random_tweet_user=random_tweet._json['user']['screen_name']
random_tweet_url = "https://twitter.com/"+str(random_tweet_user)+"/status/"+str(random_tweet_id)

SUBJECT = "Random Liked Tweet For The Day"
TEXT = "\nTweet By "
TEXT = TEXT + str(random_tweet_user) + ": " +str(random_tweet_text) + "\n\n" + str(random_tweet_url)
message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)
print(message)
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login("email id", "password") #email id and password

message= message.encode('ascii', 'ignore').decode('ascii')
server.sendmail(sender_email,receiver_email,message)
print("Email Sent")
server.quit()


"""
Created on 15:06, Apr. 15th, 2021
Author: fassial
Filename: Logger.py
Description:
    Read & Write record. The code was modified from https://github.com/dmlc/tensorboard
"""
import re
import struct

from .crc32c import crc32c

__all__ = [
    "Logger",
]

## def Logger class
class Logger(object):
    # init macro
    _struct_len = {
        "I": 4,
        "Q": 8,
    }

    def __init__(self, fname):
        # init params
        self.fname = fname

        # init vars
        self._writer = open(self.fname, "wb")
        self._reader = open(self.fname, "rb")

    def __del__(self):
        # close writer
        if self._writer is not None:
            self._writer.close()
        # close reader
        if self._reader is not None:
            self._reader.close()

    def write(self, log):
        # write len-crc(len)-log-crc(log)
        header = struct.pack("Q", len(log))
        self._writer.write(header)
        self._writer.write(struct.pack("I", _masked_crc32c(header)))
        self._writer.write(log)
        self._writer.write(struct.pack("I", _masked_crc32c(log)))
        # flush writer
        self._writer.flush()

    def read(self):
        # init logs
        logs = []
        # get header
        header = self._reader.read(Logger._struct_len["Q"])
        while header:
            # unpack header
            log_len = struct.unpack("Q", header)[0]
            # get header_crc & check crc
            header_crc = struct.unpack("I", self._reader.read(Logger._struct_len["I"]))[0]
            if header_crc != _masked_crc32c(header):
                raise ValueError("Failed at checking crc of header in utils.Logger.")
            # get log
            log = self._reader.read(log_len)
            # get log_crc & check crc
            log_crc = struct.unpack("I", self._reader.read(Logger._struct_len["I"]))[0]
            if log_crc != _masked_crc32c(log):
                raise ValueError("Failed at checking crc of log in utils.Logger.")

            # add log to logs
            logs.append(log)

            # update header
            header = self._reader.read(Logger._struct_len["Q"])

        # reset frp
        self._reader.seek(0, 0)

        return logs

## def helper func
# def _u32 func
def _u32(x):
    return x & 0xffffffff

# def _masked_crc32c func
def _masked_crc32c(data):
    x = _u32(crc32c(data))
    return _u32(((x >> 15) | _u32(x << 17)) + 0xa282ead8)


import argparse
import os

def parse_ko_metadata(metadata_f):
    """ Parses the ko metadata file and returns a list of KOs """

    kos = []
    with open(metadata_f, 'r') as IN:
        for line in IN:
            # skip header
            if line.startswith("Trait\t"):
                continue

            kos.append(line.split("\t", 1)[0])

    return sorted(kos)

def count_KOs(ko_table_f, ko_list):
    """ Parses a JGI KO table and counts KOs on the list """
    ko_counts = {k: 0 for k in ko_list}
    ko_not_in_list = 0
    with open(ko_table_f, 'r') as IN:
        for line in IN:
            # skip header
            if line.startswith("KO ID"):
                continue

            elems = line.rstrip().split("\t")

            ko = elems[0]
            try:
                count = elems[-1]
            except IndexError:
                print((line,))
                sys.exit()

            # strip the "KO:" off the ko name
            ko = ko[3:]

            try:
                ko_counts[ko] = float(count)
            except KeyError:
                ko_not_in_list += 1

    print("{} KOs not in the ko_list.".format(ko_not_in_list))
    return ko_counts

def main(args):
    ko_list = parse_ko_metadata(args.ko_metadata)

    with open(args.out, 'w') as OUT:
        OUT.write("\t".join(["OTU_IDs"] + ko_list) + "\n")

        for ko_table in args.ko:
            name = os.path.splitext(os.path.basename(ko_table))[0]

            ko_counts = count_KOs(ko_table, ko_list)

            # count the number of
            ko_not_counted = list(ko_counts.values()).count(0)

            print("{} KOs not found in genome.".format(ko_not_counted))
            print("{} KOs found in the genome.".format(len(list(ko_counts.values())) - ko_not_counted))
            print("")

            to_write = [name]
            [to_write.append(str(ko_counts[ko])) for ko in ko_list]
            OUT.write("\t".join(to_write) + "\n")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Converts JGI's KO table into a PICRUSt trait table")
    parser.add_argument("-ko", help="one or more JGI KO tables", nargs="+")
    parser.add_argument("-ko_metadata", help="the KO metadata table from the PICRUSt deconstructed files", required=True)
    parser.add_argument("-out", help="output path for the trait table", default="trait_table.tab")

    args = parser.parse_args()

    main(args)


from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.forms import Textarea

from .models import CustomUser

class CustomUserCreationForm(UserCreationForm):

    class Meta:
        model = CustomUser
        fields = ('email',)
        widgets = {
            'email' : Textarea(attrs={'cols' : 80, 'rows': 20})
        }

class CustomUserChangeForm(UserChangeForm):

    class Meta:
        model = CustomUser
        fields = ('email',)
        widgets = {
            'email' : Textarea(attrs={'cols' : 80, 'rows': 20})
        }


from django.db import models
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from institutions.models import InstitutionVerification
from submissions.models import *
from users.models import NewUser
from .pusher import pusher_client

class Notification(models.Model):
    class UnreadNofif(models.Manager):
        def get_queryset(self):
            return super().get_queryset().filter(isRead=False)

    options = (("verification", "verification"), ("submission", "submission"), ("recommendation", "recommendation"))
    type = models.CharField(choices=options, max_length=20)
    receiver = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
    )
    sender = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="+", blank=True, null=True
    )
    message = models.TextField(null=True, blank=True)
    isRead = models.BooleanField(default=False)
    dateCreated = models.DateTimeField(auto_now_add=True)
    dateModified = models.DateTimeField(auto_now=True)
    redirectID = models.IntegerField(null=True, blank=True)

    objects = models.Manager()
    unread = UnreadNofif()

    class Meta:
        ordering = ["-dateModified"]

    def __str__(self) -> str:
        return self.message

@receiver(post_save, sender=InstitutionVerification)
def apply_verification_handler(created, instance, *args, **kwargs):
    # if created:  # if a verification is created
    #     notif = Notification.objects.create(sender=instance.institution.owner, type="verification")

    #     notif.receiver.add(NewUser.objects.get(pk=1).id)
    #     notif.save()
    print(instance.status)
    if instance.status == "approved":

        pusher_client.trigger(
            "notification",
            instance.institution.creator.username,
            {
                "type": "verification",
                "message": "%s verification application is disapproved!" % instance.institution.name,
                "redirectID": instance.institution.id,
                # "room": self.request.data["room"],
            },
        )
        notif = Notification.objects.create(
            sender=NewUser.objects.get(pk=1),
            type="verification",
            message="%s verification application is disapproved!" % instance.institution.name,
            redirectID=instance.institution.id,
        )

        notif.receiver.add(instance.institution.creator)
        notif.save()
    if instance.status == "disapproved":

        pusher_client.trigger(
            "notification",
            instance.institution.creator.username,
            {
                "type": "verification",
                "message": "%s verification application is disapproved!" % instance.institution.name,
                "redirectID": instance.institution.id,
                # "room": self.request.data["room"],
            },
        )
        notif = Notification.objects.create(
            sender=NewUser.objects.get(pk=1),
            type="verification",
            message="%s verification application is disapproved!" % instance.institution.name,
            redirectID=instance.institution.id,
        )

        notif.receiver.add(instance.institution.creator)
        notif.save()

@receiver(post_save, sender=Submission)
def submission_created_handler(created, instance, *args, **kwargs):
    if created:
        # breakpoint()
        pusher_client.trigger(
            "notification",
            instance.file.folder.workspace.classroom.adviserInstance.username,
            {
                "type": "submission",
                "message": "A submission is created at %s" % instance.file.folder.workspace.classroom.name,
                "redirectID": instance.file.folder.workspace.classroom.id,
                # "room": self.request.data["room"],
            },
        )
        notif = Notification.objects.create(
            # sender=NewUser.objects.get(pk=1),
            type="submission",
            message="A submission is created at %s" % instance.file.folder.workspace.classroom.name,
            redirectID=instance.file.folder.workspace.classroom.id,
        )
        # adviserInstance
        notif.receiver.add(instance.file.folder.workspace.classroom.adviserInstance)
        notif.save()
        pass

@receiver(post_save, sender=Recommendation)
def recommendation_created_handler(created, instance, *args, **kwargs):
    if created:

        pusher_client.trigger(
            "notification",
            instance.department.institution.creator.username,
            {
                "type": "recommendation",
                "message": "A recommendation is created at %s" % instance.department.name,
                "redirectID": instance.department.id,
                # "room": self.request.data["room"],
            },
        )
        notif = Notification.objects.create(
            # sender=NewUser.objects.get(pk=1),
            type="recommendation",
            message="A recommendation is created at %s" % instance.department.name,
            redirectID=instance.department.id,
        )
        notif.receiver.add(instance.department.institution.creator)
        notif.save()
        pass


from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtCore import QThread
from GUI import Ui_MainWindow
import sys
import speech_recognition as sr

class TreadVoice(QThread):

    def __init__(self):
        QThread.__init__(self)

    def __del__(self):
        self.wait()

    def run(self):
        pass

class mainwindow(QtWidgets.QMainWindow):

    def __init__(self):
        super(mainwindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.pushButton_listen.clicked.connect(self.voiceListen)

    def voiceListen(self):
        print('Произошёл запуск')
        r = sr.Recognizer()

        with sr.Microphone() as source:
            # self.ui.label.setText("Говорите")
            r.pause_threshold = 0.5
            r.adjust_for_ambient_noise(source, duration=1)
            audio = r.listen(source)

        try:
            task = r.recognize_google(audio, language="ru-RU").lower()
            if "закрыть" in task:
                sys.exit(0)
            else:
                self.ui.listWidget.addItem("Вы сказали " + task)
                # self.ui.label.setText("Вы сказали " + task)
                # self.ui.label.adjustSize()
        except (sr.UnknownValueError, sr.RequestError):
            self.ui.listWidget.addItem('Не понятно, повторите')
            # self.ui.label.setText('Не понятно, повторите')
            task = self.voiceListen()
        return task

    # def sayText(task):
    #     if "стоп" in task:
    #         sys.exit(0)
    #     else:
    #         self.ui.label.setText("Вы сказали " + task)
    #         self.ui.label.adjustSize()

    def main(self):
        self.voiceListen()

app = QtWidgets.QApplication([])
application = mainwindow()
application.show()

sys.exit(app.exec())


import sys
import json
import schedule
import time
import threading

reload(sys)
sys.setdefaultencoding('UTF8')

config_file = "load.conf"

instance_required_keys = ["instance_name", "schedule_pattern", "delay", "hive_table"]
config_required_keys = ["schedule_patterns", "instances_config"]
pattern_required_keys = ["mask"]

def write_config(config):
    with open(config_file, "w") as f:
        json.dump(config, f, indent=5)
        print("finished dump dic to file")

def load_config():
    with open(config_file, "r") as f:
        config = json.load(f)
        print("finished load config from json")
        return config

def validate_config(config_map, required_fields):
    for config_key in required_fields:
        if not config_map.has_key(config_key):
            print("Config key %s is required" % str(config_key))
            return False
    return True

def pattern_match(config_pattern, mask):
    return int("0b" + config_pattern, 2) & int("0b" + mask, 2) > 0

def schedule_job(instance_config, job_type):
    instance_name = instance_config.get(instance_required_keys[0])
    print("Active job type %s for instance %s" % (job_type, instance_name))
    if job_type == "secondly":
        schedule.every().second.do(job, instance_config, job_type)
    elif job_type == "minutely":
        schedule.every().minute.do(job, instance_config, job_type)
    elif job_type == "hourly":
        schedule.every().hour.do(job, instance_config, job_type)
    elif job_type == "daily":
        schedule.every().day.do(job, instance_config, job_type)
    elif job_type == "weekly":
        schedule.every(7).days.do(job, instance_config, job_type)
    else:
        print("Currently do not support schedule job type: %s" % job_type)

def start_schedule_job(instance_config, schedule_pattern):
    if not validate_config(instance_config, instance_required_keys):
        print("Instance config not passed validate: %s" % str(instance_config))
        return
    instance_pattern_config = instance_config.get(instance_required_keys[1])
    for pattern_type, pattern in schedule_pattern.items():
        if not validate_config(pattern, pattern_required_keys):
            print("Pattern config not passed validate: %s" % str(pattern))
        if pattern_match(instance_pattern_config, pattern.get(pattern_required_keys[0])):
            schedule_job(instance_config, pattern_type)

def job(instance_config, job_type):
    threading.Thread(target=job_task, args=(instance_config, job_type)).start()

def job_task(instance_config, job_type):
    instance_name = instance_config.get(instance_required_keys[0])
    print("Running job type %s on config %s" % (job_type, instance_name))

def run_main():
    config = load_config()
    if not validate_config(config, config_required_keys):
        print("Config not passed validate, please check config: %s" % str(config))
        return

    for instance_config in config.get(config_required_keys[1]):
        start_schedule_job(instance_config, config.get(config_required_keys[0]))

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

if __name__ == '__main__':
    run_main()


# -*- coding: utf-8 -*-
# @Time    : 2020/6/7 9:29
# @Author  : Equator
import matplotlib.pyplot as plt
import code.preprocessing.data_scan as data_scan
from pandas.plotting import scatter_matrix
import numpy as np

# 单一特征图表
def hist(data):
    data.hist(sharex=False, sharey=False, xlabelsize=1, ylabelsize=1)
    plt.show()

# 密度图
def density(data):
    data.plot(kind='density', subplots=True, layout=(4, 4), sharex=False, fontsize=1)
    plt.show()

# 箱线图
def box(data):
    data.plot(kind='box', subplots=True, layout=(4, 4), sharex=False, fontsize=1)
    plt.show()

# 散点图矩阵
def scatter(data):
    scatter_matrix(data, figsize=(48, 48))
    plt.show()

def figure(data):
    fig = plt.figure(figsize=(13, 13))
    ax = fig.add_subplot(111)
    cax = ax.matshow(data.corr(), vmin=-1, vmax=1, interpolation='none')
    fig.colorbar(cax)
    # 刻度
    ticks = np.arange(0, 14, 1)
    ax.set_xticks(ticks)
    ax.set_yticks(ticks)
    names = list(data.columns)
    ax.set_xticklabels(names)
    ax.set_yticklabels(names)
    plt.show()

if __name__ == '__main__':
    data = data_scan.get_data()
    # hist(data)
    # density(data)
    # box(data)
    # scatter(data)
    figure(data)


# 실습 과제: IMDB 영화 정보 크로링 통해 csv로 저장하기

import requests
from bs4 import BeautifulSoup

f = open('test.csv','w',encoding='utf-8')
f.write('title,rate \n')
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'}

url ='https://www.imdb.com/chart/boxoffice/?ref_=nv_ch_cht'
raw = requests.get(url,headers=headers)

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

# 중간 print로 확인해보는 작업 얘기해주기
# print(soup)
container = soup.select('tbody > tr')
for info in container:
    title = info.select_one('td.titleColumn > a').text.strip()
    rate = info.select_one('td.ratingColumn').text.strip()
    f.write(title + ',' + rate + '\n')

f.close()


from helpers import randfloat
from time import sleep
import keyboard as kb
import sys

# fn=input("File name: ")
fn=sys.argv[1]
waits = bool(int(sys.argv[2]))
with open(fn,"r") as file:
    print("reading "+fn)
    lines=file.readlines()
    print("start in 5 seconds!")
    sleep(5)
    for line in lines:
        if waits and .2>=randfloat(0.0,1.0):
            sleep(randfloat(0.25,.9))
        text=line[:line.index("\n")]
        kb.press_and_release("command+shift+left")#reset indents
        kb.write(text)
        print(text)
        kb.press_and_release("command+shift+down")#delete stray ) or ] autogenerated by IDE
        kb.press_and_release("enter")


import networkx as nx
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE

def visualize_graph(G, color):
    plt.figure(figsize=(7,7))
    plt.xticks([])
    plt.yticks([])
    nx.draw_networkx(G, pos=nx.spring_layout(G, seed=42), with_labels=False,
                     node_color=color, cmap="Set2")
    plt.show()

def visualize_embedding(h, color, epoch=None, loss=None):
    plt.figure(figsize=(7,7))
    plt.xticks([])
    plt.yticks([])
    h = h.detach().cpu().numpy()
    plt.scatter(h[:, 0], h[:, 1], s=140, c=color, cmap="Set2")
    if epoch is not None and loss is not None:
        plt.xlabel(f'Epoch: {epoch}, Loss: {loss.item():.4f}', fontsize=16)
    plt.show()

def visualize_tsne(h, color):
    z = TSNE(n_components=2).fit_transform(h.detach().cpu().numpy())

    plt.figure(figsize=(10,10))
    plt.xticks([])
    plt.yticks([])

    plt.scatter(z[:, 0], z[:, 1], s=70, c=color, cmap="Set2")
    plt.show()


from binance.client import Client
import variables as var
from datetime import datetime

dollar = 1000
trading = 1000
position = 0

##Setup the client with your APIs stored in the variables.py folder
client = Client(var.API,var.APIKey)

klines = client.get_historical_klines(var.Pair, Client.KLINE_INTERVAL_1DAY, var.StartDate)

print("You are trading the",var.BigEMA,"vs",var.SmallEMA,"exponential moving average on"
      , var.Pair, " since ",var.StartDate, " and ", dollar , " dollars.")

old_direction = 'Null'

returns = []

for i in range(var.BigEMA,len(klines)):
    price_now = float(klines[i][4])
    if old_direction == 'Null':

        ##Calculate SMA for shorter time period.
        smallMA = 0
        for each in klines[i - var.SmallEMA:i]:
            smallMA += float(each[4])
        smallMA = smallMA / var.SmallEMA

        ##Calculate SMA for longer time period.
        bigMA = 0
        for each in klines[i - var.BigEMA:i]:
            bigMA += float(each[4])
        bigMA = bigMA / var.BigEMA

        SmallEMA = smallMA
        smallWeight = var.Smoothing / (var.SmallEMA + 1)
        BigEMA = bigMA
        bigWeight = var.Smoothing / (var.BigEMA + 1)
    elif old_direction != 'Null':
        ##Calculate EMA for shorter time period.
        SmallEMA = (price_now * smallWeight) + SmallEMA *(1-smallWeight)
        ##Calculate EMA for shorter time period.
        BigEMA = (price_now * bigWeight) + BigEMA *(1-bigWeight)

    if (BigEMA < SmallEMA) and (price_now > SmallEMA):
        direction = 'Positive'
    elif BigEMA < SmallEMA and old_direction == 'Null':
        direction = 'Positive'
    if (BigEMA > SmallEMA) and (price_now < BigEMA):
        direction = 'Negative'
    elif BigEMA > SmallEMA and old_direction == 'Null':
        direction = 'Negative'
    if old_direction != direction:
        if old_direction == 'Null':
            old_direction = direction
        elif old_direction != direction and i > 3*var.BigEMA:
            # Determine the date and time of the trade
            date = datetime.utcfromtimestamp(float(klines[i][6])/1000).strftime('%Y-%m-%d %H:%M:%S')
            if old_direction == 'Positive' and position != 0:
                print("Sell at " + str(klines[i][4]))
                dollar += position * price_now * (1-var.TakerFee)
                position = 0
                if price_now > buy:
                    gain = round((10000*(price_now-buy)/price_now))
                    print("Profit = " + str(price_now - buy) + " Or " + str(gain/100) + "% at " + date)
                    returns += [gain/100]
                else:
                    loss = round((10000*(price_now-buy)/price_now))
                    print("Loss = " + str(price_now - buy) + " Or " + str(loss/100) + "% at " + date)
                    returns += [loss/100]
                buy = 0
            elif old_direction != 'Positive':
                print("Buy at " + str(klines[i][4]))
                position += (trading/price_now) * (1-var.MakerFee)
                dollar -= trading
                buy = price_now
            old_direction = direction

if position != 0:
    print("Final sell at " + str(float(klines[len(klines)-1][4])))
    if float(klines[len(klines)-1][4]) > buy:
        print("Profit = " + str(float(klines[len(klines)-1][4]) - buy) + " Or " + str(
            (round((10000 * (float(klines[len(klines)-1][4]) - buy) / float(klines[len(klines)-1][4])))) / 100) + "%.")
    else:
        print("Loss = " + str(float(klines[len(klines)-1][4]) - buy) + "Or " + str(
            (round((10000 * (float(klines[len(klines)-1][4]) - buy) / float(klines[len(klines)-1][4])))) / 100) + "%.")

    dollar += position * float(klines[len(klines)-1][4])
    position = 0
print("Dollar is ",str(dollar))
print(returns)

ReturnsTest = var.Returns(returns)

print("Average is ",str(ReturnsTest.average()))
print("Variance is ",str(ReturnsTest.variance()))
print("Standard Deviation is ",str(ReturnsTest.standard_dev()))


import setuptools
import warnings

DISTNAME            = "scipydirect"
DESCRIPTION         = "Python wrapper to the DIRECT algorithm"
LONG_DESCRIPTION    ="""
DIRECT is a method to solve global bound constraint optimization problems and
was originally developed by D. R. Jones, C. D. Perttunen and B. E. Stuckmann.

The scipydirect is a fork of pydirect providing a scipy.optimize compatible
syntax. It uses the Fortran implementation of DIRECT written by
Joerg M Gablonsky, DIRECT Version 2.0.4.

For more info see the `documentation <http://scipydirect.readthedocs.io/en/latest/>`_ or the `source code <http://github.com/andim/scipydirect>`_.
"""
MAINTAINER          = "Andreas Mayer"
MAINTAINER_EMAIL    = "andimscience@gmail.com"
URL                 = "http://github.com/andim/scipydirect"
LICENSE             = "MIT"
VERSION             = "1.4"

classifiers =  ['Development Status :: 5 - Production/Stable',
                'Programming Language :: Python',
                "Programming Language :: Python :: 2",
                "Programming Language :: Python :: 2.7",
                "Programming Language :: Python :: 3",
                "Programming Language :: Python :: 3.8",
                'License :: OSI Approved :: MIT License',
                'Intended Audience :: Science/Research',
                'Topic :: Scientific/Engineering',
                'Topic :: Scientific/Engineering :: Mathematics',
                'Operating System :: OS Independent']

def configuration(parent_package='', top_path=None):
    from numpy.distutils.misc_util import Configuration
    config = Configuration(DISTNAME, parent_package, top_path,
                           version=VERSION,
                           maintainer=MAINTAINER,
                           maintainer_email=MAINTAINER_EMAIL,
                           description=DESCRIPTION,
                           license=LICENSE,
                           url=URL,
                           long_description=LONG_DESCRIPTION)

    return config

if __name__ == "__main__":
    from numpy.distutils.core import setup
    kwargs = dict(
            packages=setuptools.find_packages(),
            include_package_data=True,
            platforms=["any"],
            requires=["numpy"],
            install_requires=["numpy"],
            tests_require=['nose',],
            test_suite='nose.collector',
            zip_safe=True,
            classifiers=classifiers,
            )
    thiskwargs = kwargs.copy()
    config = configuration()
    config.add_extension('direct', sources=['src/direct.pyf', 'src/DIRect.f', 'src/DIRserial.f', 'src/DIRsubrout.f'])
    thiskwargs.update(config.todict())
    setup(**thiskwargs)


# NOTE:
# 2023 08 03, Stefano Rapisarda:
# Defining a class for a pseudo code seems a but an overshooting, but
# there may be some extra functionality to be added in the future
# related to that, so... that's it

import re

class PseudoCode(str):
    def __new__(cls,code=''):
        instance = super().__new__(cls, code)
        if instance:
            assert len(instance) == 6, 'Pseudo code must contain exactly 6 characters'
            assert instance[0] in ['A','B'], 'First character must be either A or B'
            assert instance[1:].isnumeric(), 'All but first character must be numbers'
        return instance


import os
from pathlib import Path
from typing import List

import cv2
import numpy as np

from variables import (
    IS_DOCKER,
    MOUNT_IMAGE_DEST,
    MOUNT_IMAGE_SRC,
    VIDEO_FILE_EXTENSIONS,
)
from utils.logger import logger

class IOVideoManager(object):
    """IOVideoManager class to work with IO"""

    def __init__(self, src_folder: Path, dest_folder: Path, silent=False) -> None:
        self._src_folder = src_folder
        self._dest_folder = dest_folder
        # The '_silent' variable is currently not used but it could be useful for logging
        # purposes
        self._silent = silent

    def _log_found_files(self, file_names: List[str]) -> None:
        """Helper function to log all the valid files found in the src folder"""

        logger.info(f"Found {len(file_names)} valid file(s).")
        for f in file_names:
            logger.info(f"   - {f}")

    def get_video_paths(self) -> List[dict]:
        """Gets the paths of the videos in the src folder"""

        path_objs = []
        abs_src_folder = self._src_folder.absolute()
        for p in os.listdir(abs_src_folder.as_posix()):
            if p.lower().endswith(tuple(VIDEO_FILE_EXTENSIONS)):
                full_path = ""
                if IS_DOCKER:
                    full_path = Path(os.path.join(MOUNT_IMAGE_SRC, p))
                else:
                    full_path = Path(os.path.join(abs_src_folder.as_posix(), p))

                tmp = {"name": p, "path": full_path}
                path_objs.append(tmp)

        self._log_found_files([p_obj["name"] for p_obj in path_objs])
        return path_objs

    def save_img(self, dest_path: Path, image: np.array) -> None:
        """Saves a np.array with cv2 to dest folder"""
        full_save_path = ""
        if IS_DOCKER:
            full_save_path = os.path.join(MOUNT_IMAGE_DEST, dest_path.as_posix())
        else:
            full_save_path = os.path.join(
                self._dest_folder.as_posix(), dest_path.as_posix()
            )

        # Create dirs if they do not exist already
        os.makedirs(os.path.dirname(full_save_path), exist_ok=True)
        cv2.imwrite(full_save_path, image)


from django import forms
from django.forms.widgets import NumberInput

class CartAddProductForm(forms.Form):
    quantity = forms.IntegerField(min_value=1, initial=1, widget=NumberInput(
        attrs={
            "style": "width:43px; text-align:center; margin-top:15px; border: 1px solid; margin-bottom: 25px;"
            # "class": "form-control"

        }
    ))
    update = forms.BooleanField(required=False, initial=False, widget=forms.HiddenInput)


# -*- coding: utf-8 -*-
import re
import json
from time import sleep
from scrapy import Spider
from selenium import webdriver
from scrapy.selector import Selector
from scrapy.shell import inspect_response
from scrapy.http import Request, FormRequest
from scrapy.utils.response import open_in_browser

class OuSpider(Spider):
    name = 'ou'
    # start_urls = ['https://webapps.ou.edu/common/services/searchServices.cfc?callback=jQuery112408144499212139631_1531864722002&method=person&primaryid=476413']
    # start_urls = ['https://webapps.ou.edu/common/services/searchServices.cfc?callback=jQuery112408144499212139631_1531864722002&method=person&primaryid=488385']
    start_urls = []
    for number in range(488385, 490000):
        start_urls.append('https://webapps.ou.edu/common/services/searchServices.cfc?callback=jQuery112408144499212139631_1531864722002&method=person&primaryid=' + str(number))

    def parse(self, response):
        # inspect_response(response, self)
        # open_in_browser(response)

        response_text = str(response.body)
        response_text = response_text.strip()
        response_text = response_text.replace('jQuery112408144499212139631_1531864722002 (', '')
        response_text = response_text.replace('\r', '')
        response_text = response_text.replace('\n', '')
        response_text = response_text.replace('\t', '')
        response_text = response_text.replace('\\r', '')
        response_text = response_text.replace('\\n', '')
        response_text = response_text.replace('\\t', '')
        response_text = response_text.replace(';', '')
        response_text = response_text.replace("'b\'", '')
        response_text = response_text.replace("\'", '')
        response_text = response_text.strip()
        response_text = response_text[1:-1]

        jsonresponse = json.loads(response_text)

        if jsonresponse['person'][0]['alias']:
            email = jsonresponse['person'][0]['alias'] + '@ou.edu'

        stuclass = jsonresponse['person'][0]['stuclass']
        if stuclass:
            if 'undergraduate' in stuclass.lower() or 'freshman' in stuclass.lower() or 'sophomore' in stuclass.lower() or 'junior' in stuclass.lower() or 'senior' in stuclass.lower():
                undergraduate = True
            else:
                undergraduate = False
        else:
            undergraduate = False

        if not undergraduate:
            yield {'alias': jsonresponse['person'][0]['alias'],
                   'caddr': jsonresponse['person'][0]['caddr'],
                   'col': jsonresponse['person'][0]['col'],
                   'cphone': jsonresponse['person'][0]['cphone'],
                   'dept': jsonresponse['person'][0]['dept'],
                   'deptDept': jsonresponse['person'][0]['deptDept'],
                   'deptTitle': jsonresponse['person'][0]['deptTitle'],
                   'fname': jsonresponse['person'][0]['fname'],
                   'gen': jsonresponse['person'][0]['gen'],
                   'haddr': jsonresponse['person'][0]['haddr'],
                   'hphone': jsonresponse['person'][0]['hphone'],
                   'lname': jsonresponse['person'][0]['lname'],
                   'loc': jsonresponse['person'][0]['loc'],
                   'mname': jsonresponse['person'][0]['mname'],
                   'ophone': jsonresponse['person'][0]['ophone'],
                   'stuclass': stuclass,
                   'title': jsonresponse['person'][0]['title'],
                   'url1': jsonresponse['person'][0]['url1'],
                   'email': email}


import math
from .type import TypeImplementation, registerType
from . import primitive

def paramCurve(params):
    def fn(x):
        lp = len(params) - 1
        a = math.floor(x * lp)
        period = 1/lp
        if a >= lp:
            return params[-1]
        if a < 0:
            return params[0]
        b = a + 1
        c = (x - a*period) / period
        return params[a] * (1 - c) + params[b] * c
    return fn

class CurveType(TypeImplementation):
    signature = b'curv'

    def read(self):
        self.file.read(4)
        self.entries = primitive.readU32(self.file)
        if self.entries == 0:
            self.fn = lambda x: x
        elif self.entries == 1:
            self.value = primitive.readU8Fixed8Number(self.file)
            self.fn = lambda x: math.pow(x, self.value)
        else:
            self.values = []
            for n in range(self.entries):
                self.values.append(primitive.readU16(self.file) / 65535.0)
            self.fn = paramCurve(self.values)

    def get(self, x):
        return self.fn(x)

    def getDefault(self):
        return self.fn

registerType(CurveType)


from logzero import logger
import torch
from src.genai.model.Generators.gen import Generator
from src.genai.model.discriminators.disc import Discriminator
from src.genai.dataoperations.dataoperations import DataLoadingOperations
import torch.optim as optim
import torch.nn as nn
from tqdm import tqdm
import wandb
import torchvision
import os

class TrainConfig:
    def __init__(self) -> None:
        pass

class GanTraining:
    def __init__(self, config) -> None:
        self.config = config

    def optimizer(self, params, model):
        if params["name"] == "adam":
            return optim.Adam(model.parameters(), lr=params["lr"])

    def criterion(self, params):
        if params["name"] == "bceloss":
            return nn.BCELoss()

    def train(self):
        model_info = self.config["gan"]

        if model_info["name"] == "simple_gan":
            trainconfig = self.config_simple_gan()
            self.train_simple_gan(trainconfig)

    def config_simple_gan(self):
        self.trainconfig = TrainConfig()
        self.trainconfig.data = 0
        self.trainconfig.device = self.config["operation"]["device"]
        self.trainconfig.disc = Discriminator(self.config["gan"]["image_dim"])
        self.trainconfig.gen = Generator(self.config["gan"]["z_dim"], self.config["gan"]["image_dim"])

        # print(self.trainconfig.gen)
        # print(self.trainconfig.disc)

        logger.debug("The Generator and Discriminator are built.")

        tfms = DataLoadingOperations.get_tfs()
        data = DataLoadingOperations.get_mnist(tfms)
        self.trainconfig.dataloader = DataLoadingOperations.dataloading(data)

        logger.debug("The dataloader is loaded.")

        gan_params = self.config["gan"]["gen"]["opt"]
        self.trainconfig.gen_opt = self.optimizer(gan_params, self.trainconfig.gen)

        disc_params = self.config["gan"]["disc"]["opt"]
        self.trainconfig.disc_opt = self.optimizer(disc_params, self.trainconfig.disc)

        logger.debug("The optimizers are init.")

        criterion_params = self.config["gan"]["criterion"]
        self.trainconfig.criterion = self.criterion(criterion_params)

        logger.debug("The criterion is set.")

        self.trainconfig.device = self.config["operation"]["device"]
        self.trainconfig.z_dim = self.config["gan"]["z_dim"]
        self.trainconfig.batch_size = self.config["gan"]["batch_size"]
        self.trainconfig.num_epochs = self.config["gan"]["num_epochs"]

        self.trainconfig.wandb_status = self.config["operation"]["wandb_flag"]
        freq = self.config["operation"]["wandb_model_freq"]
        if self.trainconfig.wandb_status:
            wandb.init(project="gan_training", config=self.config)
            wandb.watch(self.trainconfig.disc, log_freq=freq, log="all")
            wandb.watch(self.trainconfig.gen, log_freq=freq, log="all")

        return self.trainconfig

    def train_simple_gan(self, config):
        logger.debug("training a simple gan")

        # make sure the below are there configured in the config dic
        # before training.
        device = self.trainconfig.device
        z_dim = self.trainconfig.z_dim
        batch_size = self.trainconfig.batch_size
        num_epochs = self.trainconfig.num_epochs

        disc = config.disc
        gen = config.gen

        loader = config.dataloader
        opt_disc = config.disc_opt
        opt_gen = config.gen_opt

        criterion = config.criterion
        wandb_status = config.wandb_status

        # convert models to device
        disc = disc.to(device)
        gen = gen.to(device)

        fixed_noise = torch.randn((batch_size, z_dim)).to(device)
        step = 0

        for epoch in range(num_epochs):
            for batch_idx, (real, _) in enumerate(loader):
                real = real.view(-1, 784).to(device)
                batch_size = real.shape[0]

                ### Train Discriminator: max log(D(x)) + log(1 - D(G(z)))
                noise = torch.randn(batch_size, z_dim).to(device)
                fake = gen(noise)
                disc_real = disc(real).view(-1)
                lossD_real = criterion(disc_real, torch.ones_like(disc_real))
                disc_fake = disc(fake).view(-1)
                lossD_fake = criterion(disc_fake, torch.zeros_like(disc_fake))
                lossD = (lossD_real + lossD_fake) / 2
                disc.zero_grad()
                lossD.backward(retain_graph=True)
                opt_disc.step()

                ### Train Generator: min log(1 - D(G(z))) <-> max log(D(G(z))
                # where the second option of maximizing doesn't suffer from
                # saturating gradients
                output = disc(fake).view(-1)
                lossG = criterion(output, torch.ones_like(output))
                gen.zero_grad()
                lossG.backward()
                opt_gen.step()

                if wandb_status:
                    wandb_logs = {"lossD": lossD, "lossG": lossG, "epoch": epoch}
                    wandb.log(wandb_logs)

                if batch_idx == 0:
                    print("Epoch :", epoch)
                    print("Disc Loss :", lossD)
                    print("Gen Loss :", lossG)

                    with torch.no_grad():
                        fake = gen(fixed_noise).reshape(-1, 1, 28, 28)
                        data = real.reshape(-1, 1, 28, 28)
                        img_grid_fake = torchvision.utils.make_grid(fake, normalize=True)
                        img_grid_real = torchvision.utils.make_grid(data, normalize=True)
                        img_grid_fake = wandb.Image(
                            img_grid_fake,
                        )

                        wandb.log({"img_grid_fake": img_grid_fake})

                        img_grid_real = wandb.Image(
                            img_grid_real,
                        )

                        wandb.log({"img_grid_real": img_grid_real})

                    step += 1

        # save the model
        os.makedirs("./output", exist_ok=True)
        wandb.unwatch(gen)
        wandb.unwatch(disc)
        op = {
            "model_disc": disc.state_dict(),
            "model_gen": gen.state_dict(),
            "opt_disc": opt_disc.state_dict(),
            "opt_gen": opt_gen.state_dict(),
        }
        torch.save(op, "output/op.pt")
        logger.info("Saved the models of gen, disc.")


import os

# Inputs
desktop_directory = "/Users/peterjmyers/Desktop"
do_not_clean_files_and_folders = ['To-do', 'Janitor', 'Code', 'Other', '.DS_Store']

def create_janitor_directory_if_not_exists():
    full_directory_to_janitor = "{}/Janitor".format(desktop_directory)
    if not os.path.exists(full_directory_to_janitor):
        os.makedirs(full_directory_to_janitor)

def move_files_to_janitor():
    documents = os.listdir(desktop_directory)
    for doc in documents:
        if doc not in do_not_clean_files_and_folders:
            os.rename("{}/{}".format(desktop_directory, doc), "{}/Janitor/{}".format(desktop_directory, doc))

if __name__ == "__main__":
    create_janitor_directory_if_not_exists()
    move_files_to_janitor()
    print("Success")


import random as rnd
def play_guess_the_number():
  #Set minimum value
  print("Please enter the minimum value: ")
  minimum = int(input())

  #Set maximum value
  print("Please enter the maximum value: ")
  maximum = int(input())

  random_number = rnd.randrange(minimum,maximum)

  print(f"I am thinking of a number between {minimum} and {maximum}. Can you guess what it is?")

  guess = int(input())
  while True:
    if guess < random_number:
      print("Your guess is too low.")
      print("Try again")
      guess = int(input())
    elif guess > random_number:
      print("Your guess is too high.")
      print("Try again")
      guess = int(input())
    elif guess == random_number:
      print( "Congratulations! You guessed my number!")
      return guess

play_guess_the_number()


def canPartition(nums):
    """
    :type nums: List[int]
    :rtype: bool
    """
    # The idea is to find the sum of the array. If it is odd, then we cannot divide the array into two subsets with equal sum, so we return False. If it is even, then we sort the array in descending order and check if we can find a subset with sum = sum/2
    nums.sort(reverse=True)
    total = sum(nums)
    if total % 2 != 0:
        return False
    else:
        target = total / 2
        return findSubset(nums, target, 0, 0)

if __name__ == '__main__':
    canPartition()


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

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt
from skimage.io import imread
from skimage.transform import resize
import numpy as np
import skimage.transform as tf
from PIL import Image
from io import BytesIO
from images.utils import figure_to_PIL

def transform(corners):
    """
    This function is based in the jupyter notebook with the same name.
    Look at it for documentation.
    """
    image_path = corners["image"]
    image = imread(image_path, as_gray=True)
    image_shape = tuple(corners["image_shape"])
    image_resize = resize(image, image_shape, mode="symmetric", preserve_range=True)

    # Given that the new shape will be 300 by 300 this is the given points
    src = np.array([[0, 0], [0, 300], [300, 300], [300, 0]])

    corners["up_left"] = [float(x) for x in corners["up_left"]]
    corners["down_left"] = [float(x) for x in corners["down_left"]]
    corners["up_right"] = [float(x) for x in corners["up_right"]]
    corners["down_right"] = [float(x) for x in corners["down_right"]]

    dst = np.array(
        [
            corners["up_left"],
            corners["down_left"],
            corners["down_right"],
            corners["up_right"],
        ]
    )

    tform = tf.ProjectiveTransform()
    tform.estimate(src, dst)
    warped = tf.warp(image_resize, tform, output_shape=image_shape)

    new_image = warped[0:300, 0:300]

    fig, ax = plt.subplots()

    ax.imshow(new_image, cmap=plt.cm.gray)

    divisor = 299
    color = "#FF0000"

    for i in range(9):
        x = (divisor / 9) * (i + 1)
        y1 = 0
        y2 = 299
        linewidth = int((i + 1) % 3 == 0) * 3 or 1
        plt.plot([x, x], [y1, y2], "-", linewidth=linewidth, color=color)
        plt.plot([y1, y2], [x, x], "-", linewidth=linewidth, color=color)
    plt.plot([0, 0], [0, divisor], "-", linewidth=3, color=color)
    plt.plot([0, divisor], [0, 0], "-", linewidth=3, color=color)

    pil_image = figure_to_PIL(fig, ax, plt)

    response = {
        "image": pil_image,
        "transform_image": new_image
        }

    return response


import collections
from random import randint,choice

def adj(G,u):
    a=[]
    for v in G[u]:
        if G[u][v]!=0:
            a.append(v)
    return a

def edges(G):
    e={}
    for u in G:
        for v in adj(G,u):
            if not (v,u) in e.keys():
                e[(u,v)]=G[u][v]
    return e

def NewAdjMatrix(G,copy=False):
    am={}
    for u in G:
        am[u]={}
        for v in G[u]:
            if copy:
                am[u][v]=G[u][v]
            else:
                am[u][v]=0
    return am

def CopyAdjMatrix(G):
    return NewAdjMatrix(G,True)

def RevPath(D,d):
    P=[d]
    while D[d]['p']!=None:
        d=D[d]['p']
        P.append(d)
    return P

def RevWFI(P,s,d):
    PR=[d]
    while P[s][d]!=0:
        d=P[s][d]
        PR.append(d)
    return PR

def FindCyclesMatrix(G):
    visited=[]
    cycs=[]
    S=collections.deque()
    for u0 in G:
        if u0 not in visited:
            P=NewAdjMatrix(G)
            FindCyclesMatrix_r(G,P,u0,visited,S,cycs)
    return cycs

def FindCyclesMatrix_r(G,P,u,visited,S,cycs):
    visited.append(u)
    S.append(u)
    n=[]
    for v in G[u]:
        if G[u][v]!=0: n.append(v)
    for v in n:
        if v not in S:
            P[u][v]=P[v][u]=1
            FindCyclesMatrix_r(G,P,v,visited,S,cycs)
        else:
            if P[u][v]!=1: # prevents turning back to the previous vertex in undirected graphs
                c = []
                for vx in reversed(S):
                    c.append(vx)
                    if vx is v:
                        break
                if c not in cycs:
                    cycs.append(c)
    S.pop()

def FindCycleWithWeightsMatrix(G):
    c=FindCyclesMatrix(G)
    E2={}
    E=edges(G)
    if len(c)>0:
        c=c[0]
        cp=[(c[-1],c[0])]
        for i in range(len(c)-1):
            cp.append((c[i],c[i+1]))
        for cpi in cp:
            E2=E2|dict(filter(lambda x:x[0][0] in cpi and x[0][1] in cpi,E.items()))
    return E2

def FindCycles(G):
    visited=[]
    cycs=[]
    S=collections.deque()
    for u0 in G:
        if u0 not in visited:
            FindCycles_r(G,u0,visited,S,cycs)
    return cycs

def FindCycles_r(G,u,visited,S,cycs):
    visited.append(u)
    S.append(u)
    for v in G[u]['adj']:
        if v not in S:
            G[v]['p']=u
            FindCycles_r(G,v,visited,S,cycs)
        else:
            if v is not G[u]['p']: # prevents turning back to the previous vertex in undirected graphs
                c = []
                for vx in reversed(S):
                    c.append(vx)
                    if vx is v:
                        break
                if c not in cycs:
                    cycs.append(c)
    S.pop()

def DetectCycle(G):
    visited=[]
    S=collections.deque()
    for u0 in G:
        if u0 not in visited:
            r=DetectCycle_r(G,u0,visited,S)
            if r: return True
    return False

def DetectCycle_r(G,u,visited,S):
    visited.append(u)
    S.append(u)
    for v in G[u]['adj']:
        if v not in S:
            G[v]['p']=u
            r=DetectCycle_r(G,v,visited,S)
            if r: return True
        else:
            if v is not G[u]['p']: # prevents turning back to the previous vertex in undirected graphs
                return True
    S.pop()
    return False

def MakeSet(F,x):
    if x not in F:
        F[x]={'p':x}
    return F

def Find(F,x):
    while F[x]['p']!=x:
        F[x]['p']=F[F[x]['p']]['p']
        x=F[x]['p']
    return x

def Union(F,x,y):
    x=Find(F,x)
    y=Find(F,y)
    F[x]['p']=y

def MatrixToTable(G):
    GR={}
    for v in G:
        GR[v]={'adj':adj(G,v)}
    return GR

def GenerateRandomMatrix(nodes:list, start:chr) -> dict:
    G={n1:{n2:0 for n2 in nodes} for n1 in nodes}
    svl=nodes.index(start)
    w1=[svl]
    while True:
        clds=randint(1,2)
        while clds+len(w1)>len(nodes): clds=randint(1,2)
        vls=[]
        stop=False
        while not stop:
            for c in range(clds):
                x=svl
                while x==svl or x in vls: x=randint(0,len(nodes)-1)
                vls.append(x)
            checks=0
            for tvl in vls:
                if tvl not in w1: checks=checks+1
            stop=checks==clds
            for tvl in vls:
                if G[nodes[svl]][nodes[tvl]]!=0 or G[nodes[tvl]][nodes[svl]]!=0: stop=False
            if not stop: vls.clear()
        if len(w1)>1:
            stop=False
            while not stop:
                s1=svl
                while s1==svl:
                    s1,t1=choice(w1),choice(vls)
                if G[nodes[s1]][nodes[t1]]==0 and G[nodes[t1]][nodes[s1]]==0: stop=True
            w=randint(-10,20)
            if w==0: w=21
            G[nodes[s1]][nodes[t1]]=w
        for tvl in vls:
            w=randint(-10,20)
            if w==0: w=21
            G[nodes[svl]][nodes[tvl]]=w
            if tvl not in w1: w1.append(tvl)
        svl=vls[0]
        if len(w1)==len(nodes): return G

def GenerateRandomCompleteUndirectedGraph(nodes:list) -> dict:
    G={n1:{n2:0 for n2 in nodes} for n1 in nodes}
    for u in G:
        for v in G[u]:
            ui,vi=nodes.index(u),nodes.index(v)
            if vi>ui: G[u][v]=G[v][u]=randint(0,20)
    return G

class StringPL:
    def __init__(self, s: str, p: int=0, l: int=None):
        self.s, self.p = s, p
        if l is None: self.l = len(s)
        else: self.l = l
        if self.s is not None and self.p>len(self.s): raise 'cloning position greater than the length of the string'
        if self.s is not None and self.p+self.l>len(self.s): raise 'cloning position+length greater than the length of the string'
    def substring(self) -> (str):
        return self.s[self.p:self.p + self.l]
    def indexSubstring(self, tp:int, tl:int) -> (str):
        return self.s[tp:tp+tl]
    def clone(self, tp:int, tl:int) -> (object):
        if self.s is None: return StringPL(None, 0, 0)
        if tp>len(self.s): raise 'cloning position greater than the length of the string'
        if tp+tl>len(self.s): raise 'cloning position+length greater than the length of the string'
        return StringPL(self.s, tp, tl)
    def __getitem__(self, item):
        if self.s is None: return None
        if isinstance(item, slice):
            return self.s[item.start+self.p:item.stop+self.p]
        else:
            if item>len(self): raise 'item position greater than the length of the string'
            return self.substring()[item]
    def __len__(self):
        return self.l
    def __str__(self):
        return self.substring()
    def __lt__(self, other):
        return self.substring() < other.substring()
    def lcp(self, other):
        l = 0
        ss = self.substring()
        if isinstance(other, str): os = other
        else: os = other.substring()
        for i in range(0, min(len(ss), len(os))):
            if ss[i] == os[i]:
                l = l + 1
            else:
                break
        return l
    def append(self, c: chr):
        self.s = self.s + c
        self.l = self.l + 1


"""
Test the performance of the best genome produced by evolve.py.
"""

import os
import pickle
import sys

import gymnasium as gym
import neat

from evolve import eval_genomes

if sys.argv[1] == "best":
    print("Using best instead of winner.")
    with open("best", "rb") as f:
        winner = pickle.load(f)
else:
    # load the winner
    with open("winner", "rb") as f:
        winner = pickle.load(f)

print("Loaded genome:")
print(winner)

# Load the config file, which is assumed to live in
# the same directory as this script.
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, "config")
config = neat.Config(
    neat.DefaultGenome,
    neat.DefaultReproduction,
    neat.DefaultSpeciesSet,
    neat.DefaultStagnation,
    config_path,
)

net = neat.nn.FeedForwardNetwork.create(winner, config)
env = gym.make("BipedalWalker-v3", render_mode="human")
observation, info = env.reset()

done = False
while not done:
    action = net.activate(observation)
    observation, reward, terminated, truncated, info = env.step(action)
    done = terminated or truncated


import os, sys
import config, db_manage
from main import app

#check db exists else create
if not os.path.isfile(config.db_path):
    db_manage.db_create() #creates db if not exist
    db_manage.db_migrate() #creates first version migration - for rollbacks

#check db up to date else migrate
result = db_manage.compare_db_model()
if result and result != "No schema diffs":
    db_manage.db_migrate()

#run app
app.run(debug = True)


#!/bin/python

'''
permutation test (one sided p-value)
following the description of: Ensemble-based Prediction of RNA Secondary Structures (Nima Aghaeepour and Holger H. Hoos)

Created on Dez 1, 2011

@author: Marius Schneider

Input: csv file
'''

import sys
import os
import math
import csv
import argparse
import random
import functools

def readIn(file,c1,c2,deli):
    Matrix = csv.reader(open(file, 'r'), delimiter=deli)
    col1 = []
    col2 = []
    for row in Matrix:
        try:
            col1.append(float(row[c1-1]))
            col2.append(float(row[c2-1]))
        except:
            pass
    if(len(col1) != len(col2)):
        print("WARNING: data have not the same length!")
    if(len(col1) == 0):
        print("WARNING: no data found!")
    return [col1,col2]

def randomSwap(col1,col2):
    n = len(col1)
    sc1 = []
    sc2 = []
    for i in range(0,n):
        if (random.random() > 0.5):
            sc1.append(col2[i])
            sc2.append(col1[i])
        else:
            sc1.append(col1[i])
            sc2.append(col2[i])
    avg1 = avg(sc1)
    avg2 = avg(sc2)
    return avg1-avg2    # betrag?

def getPValue(col1,col2,p,avgS):
    avgs = [avgS]
    for i in range(0,p-1):
        avgs.append(randomSwap(col1,col2))
    print("AVG Start : " +str(avgS))
    print("min AVG : "+str(min(avgs)))
    print("max AVG : "+str(max(avgs)))
    v = percentile(avgs,avgS)
    print("Percentile : "+str(v))
    pValue = v / 100
    return pValue

def percentile(N, value):
    ''' returns percentile of value on list N '''
    N.sort()
    #print(N)
    first = N.index(value)
    count = N.count(value)
    #return float(first+count-1)*100/len(N)
    return float(first)*100/len(N)

def avg(N):
    return (float(sum(N)) / len(N))

def filter(col1,col2,noise):
    for i in range(0,len(col1)):
        delta = math.sqrt(noise/2) * math.sqrt((col1[i]+col2[i])/2)
        avgI = (col1[i]+col2[i])/2
        #print("Delta : "+str(delta))
        #print("AVG : "+str(avgI))
        #print("cols :"+str(col1[i]) + " : "+str(col2[i]))
        if ((col1[i] < col2[i] and col1[i] >= (avgI-delta)) or (col2[i] <= col1[i] and col2[i] >= (avgI-delta))):
            col1[i] = col2[i]
            #print("Set equal")
    return [col1,col2]

def doTest(col1,col2,alpha,permutations,name1,name2,noise):
    if(noise != None):
        [col1,col2] = filter(col1,col2,float(noise))
    oriAVG = avg(col1) - avg(col2)
    switched = 1    # 1: not switched; -1: switched
    if (oriAVG > 0):
        print("Alternative Hypothesis: Coloumn "+str(name2) + " is better than Coloumn "+str(name1))
        tmp = col1
        col1 = col2
        col2 = tmp
        oriAVG *= -1
        switched = -1
    else:
        print("Alternative Hypothesis: Coloumn "+str(name1) + " is better than Coloumn "+str(name2))
    random.seed(1234)
    pValue = getPValue(col1,col2,permutations,oriAVG)
    if (pValue < alpha):
        print("Reject Null Hypothesis (p-value: "+str(pValue)+")")
        return [1,switched,pValue]
    else:
        print("Don't reject Null Hypothesis (p-value: "+str(pValue)+")")
        return [0,switched,pValue]

if __name__ == '__main__':

    description  = "usage: python -O permutationtest.py --csv=[file]  [options]"
    parser = argparse.ArgumentParser(prog="master.py",usage=description)
    req_group = parser.add_argument_group("Required Options")
    req_group.add_argument('--csv', dest='csv', action='store', default="", required=True,  help='csv file with instance in rows and configurations as columns')
    req_group.add_argument('--alpha', dest='alpha', action='store', default=0.05, required=True, type=float,  help='confidence level')

    opt_group = parser.add_argument_group("Misc Options")
    opt_group.add_argument('--coloumn1', dest='c1', action='store', default=1, type=int, help='coloumn 1')
    opt_group.add_argument('--coloumn2', dest='c2', action='store', default=2, type=int, help='coloumn 2')
    opt_group.add_argument('--delimeter', dest='deli', action='store', default=",", help='delimeter')
    opt_group.add_argument('--perm', dest='p', action='store', default=10000, type=int, help='number of permutations')
    opt_group.add_argument('--noise', dest='noise', action='store', default=None, help='noise to filter')

    args = parser.parse_args()

    if (os.path.isfile(args.csv) == False):
        sys.stderr.write("ERROR: CSV File was not found!\n")
        sys.exit()

    [col1, col2] = readIn(args.csv,args.c1,args.c2,args.deli)
    doTest(col1,col2,args.alpha,args.p,args.c1,args.c2,args.noise)


import iograft
import iobasictypes

class WriteFileLines(iograft.Node):
    """
    Given a list of strings, write each string to the given file as
    individual lines.

    The 'write_mode' input allows setting the mode of the file write. This
    can be either 'w', 'wb', 'a', or 'ab'.
    """
    filename = iograft.InputDefinition("filename", iobasictypes.Path())
    lines = iograft.InputDefinition("lines", iobasictypes.StringList())
    write_mode = iograft.InputDefinition("mode", iobasictypes.String(),
                                         default_value="w")

    out_filename = iograft.OutputDefinition("filename", iobasictypes.Path())

    @classmethod
    def GetDefinition(cls):
        node = iograft.NodeDefinition("write_file_lines")
        node.SetMenuPath("General/File IO")
        node.AddInput(cls.filename)
        node.AddInput(cls.lines)
        node.AddInput(cls.write_mode)
        node.AddOutput(cls.out_filename)
        return node

    @staticmethod
    def Create():
        return WriteFileLines()

    def Process(self, data):
        filename = iograft.GetInput(self.filename, data)
        lines = iograft.GetInput(self.lines, data)
        write_mode = iograft.GetInput(self.write_mode, data)

        # Ensure that the read mode is value.
        if write_mode not in ["w", "wb", "a", "ab"]:
            raise ValueError("The 'write_mode' must be either 'w', 'wb',"
                             " 'a', or 'ab'")

        # Write the contents to file.
        with open(filename, write_mode) as f:
            f.writelines(lines)

        # Set the output value.
        iograft.SetOutput(self.out_filename, data, filename)

def LoadPlugin(plugin):
    node = WriteFileLines.GetDefinition()
    plugin.RegisterNode(node, WriteFileLines.Create)


import csv
import sys
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

TEST_SIZE = 0.4

def main():
    # Check command-line arguments
    if len(sys.argv) != 2:
        sys.exit("Usage: python shopping.py data")

    # Load data from spreadsheet and split into train and test sets
    evidence, labels = load_data(sys.argv[1])
    X_train, X_test, y_train, y_test = train_test_split(
        evidence, labels, test_size=TEST_SIZE
    )

    # Train model and make predictions
    model = train_model(X_train, y_train)
    predictions = model.predict(X_test)
    sensitivity, specificity = evaluate(y_test, predictions)

    # Print results
    print(f"Correct: {(y_test == predictions).sum()}")
    print(f"Incorrect: {(y_test != predictions).sum()}")
    print(f"True Positive Rate: {100 * sensitivity:.2f}%")
    print(f"True Negative Rate: {100 * specificity:.2f}%")

def load_data(filename):
    """
    Load shopping data from a CSV file `filename` and convert into a list of
    evidence lists and a list of labels. Return a tuple (evidence, labels).

    evidence should be a list of lists, where each list contains the
    following values, in order:
        - Administrative, an integer
        - Administrative_Duration, a floating point number
        - Informational, an integer
        - Informational_Duration, a floating point number
        - ProductRelated, an integer
        - ProductRelated_Duration, a floating point number
        - BounceRates, a floating point number
        - ExitRates, a floating point number
        - PageValues, a floating point number
        - SpecialDay, a floating point number
        - Month, an index from 0 (January) to 11 (December)
        - OperatingSystems, an integer
        - Browser, an integer
        - Region, an integer
        - TrafficType, an integer
        - VisitorType, an integer 0 (not returning) or 1 (returning)
        - Weekend, an integer 0 (if false) or 1 (if true)

    labels should be the corresponding list of labels, where each label
    is 1 if Revenue is true, and 0 otherwise.
    """
    df = pd.read_csv('shopping.csv')
    df['Month'] = df['Month'].apply(switch_months).fillna(0).astype('int64')
    df['VisitorType'] = df['VisitorType'].apply(returning_visitor).astype('int64')
    df['Weekend'] = df['Weekend'].apply(weekend).astype('int64')
    df['Revenue'] = df['Revenue'].apply(revenue).astype('int64')
    return df.iloc[1:, :17], df.iloc[1:, 17]

def switch_months(argument):
    switcher = {
        "Jan": int(0),
        "Feb": int(1),
        "Mar": int(2),
        "Apr": int(3),
        "May": int(4),
        "Jun": int(5),
        "Jul": int(6),
        "Aug": int(7),
        "Sep": int(8),
        "Oct": int(9),
        "Nov": int(10),
        "Dec": int(11)
    }
    return switcher.get(argument)

def returning_visitor(argument):
    if argument == "Returning_Visitor":
        return 1
    else:
        return 0

def weekend(argument):
    if argument == True:
        return 1
    else:
        return 0

def revenue(argument):
    if argument == True:
        return 1
    else:
        return 0

def train_model(evidence, labels):
    """
    Given a list of evidence lists and a list of labels, return a
    fitted k-nearest neighbor model (k=1) trained on the data.
    """
    model = KNeighborsClassifier(n_neighbors=1)
    model.fit(evidence, labels)
    return model

def evaluate(labels, predictions):
    """
    Given a list of actual labels and a list of predicted labels,
    return a tuple (sensitivity, specificty).

    Assume each label is either a 1 (positive) or 0 (negative).

    `sensitivity` should be a floating-point value from 0 to 1
    representing the "true positive rate": the proportion of
    actual positive labels that were accurately identified.

    `specificity` should be a floating-point value from 0 to 1
    representing the "true negative rate": the proportion of
    actual negative labels that were accurately identified.
    """
    tp = 0
    fn = 0
    fp = 0
    tn = 0
    #sensitivity tp/(tp + fn)
    #specificity tn/(tn + fp)
    for actual, prediction in zip(labels, predictions):
        if actual == prediction:
            #true positive
            if actual == 1:
                tp += 1
            #true negative
            else:
                tn += 1
        else:
            if actual == 1:
                #false positives
                if prediction == 0:
                    fp += 1
            else:
                #false negatives
                fn += 1

    return (tp) / (tp + fn), (tn) / (tn + fp)

if __name__ == "__main__":
    main()


import torch
import matplotlib
import matplotlib.pyplot as plt
matplotlib.style.use('ggplot')

def save_model(epochs, model, optimizer, criterion):
    """
    Function to save the trained model to disk.
    """
    torch.save({
                'epoch': epochs,
                'model_state_dict': model.state_dict(),
                'optimizer_state_dict': optimizer.state_dict(),
                'loss': criterion,
                }, 'outputs/model.pth')

def save_plots(train_acc, valid_acc, train_loss, valid_loss):
    """
    Function to save the loss and accuracy plots to disk.
    """
    # accuracy plots
    plt.figure(figsize=(10, 7))
    plt.plot(
        train_acc, color='green', linestyle='-',
        label='train accuracy'
    )
    plt.plot(
        valid_acc, color='blue', linestyle='-',
        label='validataion accuracy'
    )
    plt.xlabel('Epochs')
    plt.ylabel('Accuracy')
    plt.legend()
    plt.savefig('outputs/accuracy.png')

    # loss plots
    plt.figure(figsize=(10, 7))
    plt.plot(
        train_loss, color='orange', linestyle='-',
        label='train loss'
    )
    plt.plot(
        valid_loss, color='red', linestyle='-',
        label='validataion loss'
    )
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.legend()
    plt.savefig('outputs/loss.png')


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

from django.db import models, migrations
import smart_selects.db_fields

class Migration(migrations.Migration):

    dependencies = [
        ('baseline', '0006_auto_20150928_1711'),
    ]

    operations = [
        migrations.AlterField(
            model_name='baselineterm',
            name='follow_up_poll',
            field=models.ForeignKey(related_name='+', verbose_name='Observation Poll', to='polls.Poll'),
        ),
        migrations.AlterField(
            model_name='baselineterm',
            name='follow_up_question',
            field=smart_selects.db_fields.ChainedForeignKey(chained_model_field=b'poll', related_name='+', chained_field=b'follow_up_poll', verbose_name='Observation Question', auto_choose=True, to='polls.Question', help_text='Follow up poll responses over time to compare to the baseline values.'),
        ),
    ]


from mal import config
from mal.search import _Search, _SearchResult

class MangaSearchResult(_SearchResult):
    def __init__(self, url, image_url, title, synopsis, media_type, volumes, score):
        super().__init__(url, image_url, title, synopsis, media_type, score)
        self.volumes = volumes

class MangaSearch(_Search):
    def __init__(self, query, timeout=config.TIMEOUT):
        super().__init__(query, "manga", timeout)

    def reload(self):
        self.__init__(self._query)

    @property
    def results(self):
        try:
            self._results
        except AttributeError:
            trs = self._inner_page.find_all("tr")
            results = []
            for tr in trs[1:]:
                tds = tr.find_all("td")
                results.append(MangaSearchResult(
                    url=tds[0].find("a")["href"],
                    image_url=tds[0].find("img")["data-src"],
                    title=tds[1].find("strong").text.strip(),
                    synopsis=self._remove_suffix(tds[1].find("div", {"class": "pt4"}).text.strip(), "read more."),
                    media_type=tds[2].text.strip(),
                    volumes=self._parse_eps_vols(tds[3].text),
                    score=self._parse_score(tds[4].text)
                ))
            self._results = results
        return self._results


import kerastuner as kt
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_datasets as tfds
import os
import IPython
from kerastuner import HyperParameters

from absl import flags
from absl import logging
from absl import app

FLAGS = flags.FLAGS
# flag name, default value, explanation/help.
tf.compat.v1.flags.DEFINE_string('model_dir', 'default_model_dir', 'Directory or bucket for storing checkpoint model.')
tf.compat.v1.flags.DEFINE_bool('fine_tuning_choice', False, 'Retrain base parameters')
tf.compat.v1.flags.DEFINE_integer('train_batch_size', 32, 'Number of samples in a training batch')
tf.compat.v1.flags.DEFINE_integer('validation_batch_size', 40, 'Number of samples in a validation batch')
tf.compat.v1.flags.DEFINE_string('distribution_strategy', 'tpu', 'Distribution strategy for training.')
tf.compat.v1.flags.DEFINE_integer('train_epochs', 3, 'Number of epochs for training')
tf.compat.v1.flags.DEFINE_string('data_dir', 'gs://image-flowers/flower_photos/train', 'Location of training data')
tf.compat.v1.flags.DEFINE_integer('num_gpus', 4, 'Numer of GPU per worker')
tf.compat.v1.flags.DEFINE_string('tuner_type', 'Hyperband', 'Type of tuner. Default is hyperband')

def setup_keras_tuner_config():
    if 'TF_CONFIG' in os.environ:
        try:
            tf_config = json.loads(os.environ['TF_CONFIG'])
            cluster = tf_config['cluster']
            task = tf_config['task']
            chief_addr = cluster['chief'][0].split(':')
            chief_ip = socket.gethostbyname(chief_addr[0])
            chief_port = chief_addr[1]
            os.environ['KERASTUNER_ORACLE_IP'] = chief_ip
            #os.environ['KERASTUNER_ORACLE_IP'] = '0.0.0.0'
            os.environ['KERASTUNER_ORACLE_PORT'] = chief_port
            if task['type'] == 'chief':
                os.environ['KERASTUNER_TUNER_ID'] = 'chief'
            else:
                os.environ['KERASTUNER_TUNER_ID'] = 'tuner{}'.format(task['index'])

            print('set following environment arguments:')
            print('KERASTUNER_ORACLE_IP: %s' % os.environ['KERASTUNER_ORACLE_IP'])
            print('KERASTUNER_ORACLE_PORT: %s' % os.environ['KERASTUNER_ORACLE_PORT'])
            print('KERASTUNER_TUNER_ID: %s' % os.environ['KERASTUNER_TUNER_ID'])
        except Exception as ex:
            print('Error setting up keras tuner config: %s' % str(ex))

def model_builder(hp):
    os.environ["TFHUB_CACHE_DIR"] = "gs://ai-tpu-experiment/model-cache-dir/imagenet_resnet_v2_50_feature_vector_4"

    hp_units = hp.Int('units', min_value = 64, max_value = 256, step = 64)
    hp_activation = hp.Choice('dense_activation',
        values=['relu', 'tanh', 'sigmoid'])

    IMAGE_SIZE = (224, 224)
    model = tf.keras.Sequential([
    tf.keras.layers.InputLayer(input_shape=IMAGE_SIZE + (3,)),
    hub.KerasLayer("https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/4", trainable=False),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(units = hp_units, activation = hp_activation, kernel_initializer='glorot_uniform'),
    tf.keras.layers.Dense(5, activation='softmax', name = 'custom_class')
    ])

    model.build([None, 224, 224, 3])

    #hp_learning_rate = hp.Choice('learning_rate', values = [1e-2, 1e-4])
    #hp_optimizer = hp.Choice('selected_optimizer', ['sgd', 'adam'])
    model.compile(
        optimizer=tf.keras.optimizers.SGD(lr=1e-2, momentum=0.5),
        #optimizer=hp_optimizer,
        loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True, label_smoothing=0.1),
        metrics=['accuracy'])

    return model

class ClearTrainingOutput(tf.keras.callbacks.Callback):
        def on_train_end(*args, **kwargs):
            IPython.display.clear_output(wait = True)

def decode_and_resize(serialized_example):
    # resized image should be [224, 224, 3] and normalized to value range [0, 255]
    # label is integer index of class.

    parsed_features = tf.io.parse_single_example(
    serialized_example,
    features = {
    'image/channels' :  tf.io.FixedLenFeature([], tf.int64),
    'image/class/label' :  tf.io.FixedLenFeature([], tf.int64),
    'image/class/text' : tf.io.FixedLenFeature([], tf.string),
    'image/colorspace' : tf.io.FixedLenFeature([], tf.string),
    'image/encoded' : tf.io.FixedLenFeature([], tf.string),
    'image/filename' : tf.io.FixedLenFeature([], tf.string),
    'image/format' : tf.io.FixedLenFeature([], tf.string),
    'image/height' : tf.io.FixedLenFeature([], tf.int64),
    'image/width' : tf.io.FixedLenFeature([], tf.int64)
    })
    image = tf.io.decode_jpeg(parsed_features['image/encoded'], channels=3)
    label = tf.cast(parsed_features['image/class/label'], tf.int32)
    label_txt = tf.cast(parsed_features['image/class/text'], tf.string)
    label_one_hot = tf.one_hot(label, depth = 5)
    resized_image = tf.image.resize(image, [224, 224], method='nearest')
    return resized_image, label_one_hot

def normalize(image, label):
    #Convert `image` from [0, 255] -> [0, 1.0] floats
    image = tf.cast(image, tf.float32) / 255. + 0.5
    return image, label

def prepare_for_training(ds, cache=True, shuffle_buffer_size=1000):
    # This is a small dataset, only load it once, and keep it in memory.
    # use `.cache(filename)` to cache preprocessing work for datasets that don't
    # fit in memory.
    flags_obj = flags.FLAGS
    if cache:
        if isinstance(cache, str):
            ds = ds.cache(cache)
        else:
            ds = ds.cache()

    ds = ds.shuffle(buffer_size=shuffle_buffer_size)
    # Repeat forever
    ds = ds.repeat()
    ds = ds.batch(flags_obj.train_batch_size)
    # `prefetch` lets the dataset fetch batches in the background while the model
    # is training.
    AUTOTUNE = tf.data.experimental.AUTOTUNE
    ds = ds.prefetch(buffer_size=AUTOTUNE)
    return ds

def main(_):
    flags_obj = flags.FLAGS

    setup_keras_tuner_config()

    if flags_obj.distribution_strategy == 'tpu':
        resolver = tf.distribute.cluster_resolver.TPUClusterResolver()
        tf.config.experimental_connect_to_cluster(resolver)
        tf.tpu.experimental.initialize_tpu_system(resolver)
        strategy = tf.distribute.experimental.TPUStrategy(resolver)
        strategy_scope = strategy.scope()
        print("All devices: ", tf.config.list_logical_devices('TPU'))
    elif flags_obj.distribution_strategy == 'gpu':
        strategy = tf.distribute.MirroredStrategy()
        strategy_scope = strategy.scope()
        devices = ["device:GPU:%d" % i for i in range(flags_obj.num_gpus)]

    print('NUMBER OF DEVICES: ', strategy.num_replicas_in_sync)

    ## identify data paths and sources
    root_dir = flags_obj.data_dir # this is gs://<bucket>/folder where tfrecord are stored
    file_pattern = "{}/image_classification_builder-train*.tfrecord*".format(root_dir)
    val_file_pattern = "{}/image_classification_builder-validation*.tfrecord*".format(root_dir)

    file_list = tf.io.gfile.glob(file_pattern)
    all_files = tf.data.Dataset.list_files( tf.io.gfile.glob(file_pattern))

    val_file_list = tf.io.gfile.glob(val_file_pattern)
    val_all_files = tf.data.Dataset.list_files( tf.io.gfile.glob(val_file_pattern))

    train_all_ds = tf.data.TFRecordDataset(all_files, num_parallel_reads=tf.data.experimental.AUTOTUNE)
    val_all_ds = tf.data.TFRecordDataset(val_all_files, num_parallel_reads=tf.data.experimental.AUTOTUNE)

    # perform data engineering
    dataset = train_all_ds.map(decode_and_resize)
    val_dataset = val_all_ds.map(decode_and_resize)

    #
    BATCH_SIZE = flags_obj.train_batch_size
    VALIDATION_BATCH_SIZE = flags_obj.validation_batch_size
    dataset = dataset.map(normalize, num_parallel_calls=tf.data.experimental.AUTOTUNE)
    val_dataset = val_dataset.map(normalize, num_parallel_calls=tf.data.experimental.AUTOTUNE)

    val_ds = val_dataset.batch(VALIDATION_BATCH_SIZE)

    AUTOTUNE = tf.data.experimental.AUTOTUNE
    train_ds = prepare_for_training(dataset)

    FINE_TUNING_CHOICE = True
    NUM_CLASSES = 5
    IMAGE_SIZE = (224, 224)

    train_sample_size=0
    for raw_record in train_all_ds:
        train_sample_size += 1
    print('TRAIN_SAMPLE_SIZE = ', train_sample_size)
    validation_sample_size=0
    for raw_record in val_all_ds:
        validation_sample_size += 1
    print('VALIDATION_SAMPLE_SIZE = ', validation_sample_size)

    STEPS_PER_EPOCHS = train_sample_size // BATCH_SIZE
    VALIDATION_STEPS = validation_sample_size // VALIDATION_BATCH_SIZE

    """Runs the hyperparameter search."""
    if(flags_obj.tuner_type.lower() == 'BayesianOptimization'.lower()):
        tuner = kt.BayesianOptimization(
            hypermodel = model_builder,
            objective ='val_accuracy',
            tune_new_entries = True,
            allow_new_entries = True,
            max_trials = 5,
            directory = flags_obj.model_dir,
            project_name = 'hp_tune_bo',
            overwrite = True
            )
    elif (flags_obj.tuner_type.lower() == 'RandomSearch'.lower()):
        tuner = kt.RandomSearch(
            hypermodel = model_builder,
            objective='val_accuracy',
            tune_new_entries = True,
            allow_new_entries = True,
            max_trials = 5,
            directory = flags_obj.model_dir,
            project_name = 'hp_tune_rs',
            overwrite = True)
    else:
        tuner = kt.Hyperband(
            hypermodel = model_builder,
            objective = 'val_accuracy',
            max_epochs = 3,
            factor = 2,
            distribution_strategy=strategy,
            directory = flags_obj.model_dir,
            project_name = 'hp_tune_hb',
            overwrite = True)

    tuner.search(train_ds,
        steps_per_epoch=STEPS_PER_EPOCHS,
        validation_data=val_ds,
        validation_steps=VALIDATION_STEPS,
        epochs=3,
        callbacks=[tf.keras.callbacks.EarlyStopping('val_accuracy'), ClearTrainingOutput()])

    # Get the optimal hyperparameters
    best_hps = tuner.get_best_hyperparameters(num_trials = 1)[0]

    print(f"""
        The hyperparameter search is done.
        The best number of nodes in the dense layer is {best_hps.get('units')}.
        The best activation function in mid dense layer is {best_hps.get('dense_activation')}.
        """)

    # Build the model with the optimal hyperparameters and train it on the data
    model = tuner.hypermodel.build(best_hps)
    checkpoint_prefix = os.path.join(flags_obj.model_dir, "best_hp_train_ckpt_{epoch}")
    callbacks = [
    tf.keras.callbacks.TensorBoard(log_dir=os.path.join(flags_obj.model_dir, 'tensorboard_logs')),
    tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_prefix,
                                       save_weights_only=True)]

    model.fit(
        train_ds,
        epochs=3, steps_per_epoch=STEPS_PER_EPOCHS,
        validation_data=val_ds,
        validation_steps=VALIDATION_STEPS,
        callbacks=callbacks)

    logging.info('INSIDE MAIN FUNCTION user input model_dir %s', flags_obj.model_dir)
    # Save model trained with chosen HP in user specified bucket location
    model_save_dir = os.path.join(flags_obj.model_dir, 'best_save_model')
    model.save(model_save_dir)

if __name__ == '__main__':
    app.run(main)


def sf(self, x, *args, **kwds):
    args, loc, scale = self._parse_args(*args, **kwds)
    _a, _b = self._get_support(*args)
    x, loc, scale = map(asarray, (x, loc, scale))
    args = tuple(map(asarray, args))
    dtyp = np.find_common_type([x.dtype, np.float64], [])
    x = np.asarray((x - loc)/scale, dtype=dtyp)
    cond0 = self._argcheck(*args) & (scale > 0)
    cond1 = self._open_support_mask(x, *args) & (scale > 0)
    cond2 = cond0 & (x <= _a)
    cond = cond0 & cond1
    output = zeros(shape(cond), dtyp)
    place(output, (1-cond0)+np.isnan(x), self.badvalue)
    place(output, cond2, 1.0)
    if np.any(cond):
        goodargs = argsreduce(cond, *((x,)+args))
        place(output, cond, self._sf(*goodargs))
    if output.ndim == 0:
        return output[()]
    return output

"""
        Survival function (1 - `cdf`) at x of the given RV.

        Parameters
        ----------
        x : array_like
            quantiles
        arg1, arg2, arg3,... : array_like
            The shape parameter(s) for the distribution (see docstring of the
            instance object for more information)
        loc : array_like, optional
            location parameter (default=0)
        scale : array_like, optional
            scale parameter (default=1)

        Returns
        -------
        sf : array_like
            Survival function evaluated at x

        """


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/11/9
# @Author  : RookieDay
# @Site    :
# @File    : callback_play
# @Github  : https://github.com/rookieday
# @Software: PyCharm Community Edition

import pyaudio
import wave
import os
import time

wav_file = os.path.join(os.path.dirname(__file__),'Alizee.wav')

wf = wave.open(wav_file,'rb')
p = pyaudio.PyAudio()

def callback(in_data, frame_count, time_info, status):
    data = wf.readframes(frame_count)
    # print(data)
    return (data,pyaudio.paContinue)

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True,
                stream_callback=callback)
stream.start_stream()

while stream.is_active():
    time.sleep(1)

stream.stop_stream()
stream.close()
wf.close()
p.terminate()


import sys

t = int(input())
num = [[] for _ in range(t)]

def check(array):
    array.sort()
    for i in range(0,len(array)-1):
        if array[i] == array[i+1][0:len(array[i])]:
            print('NO')
            return
    print('YES')

for i in range(t):
    n = int(input())
    for _ in range(n):
        num[i].append(sys.stdin.readline().strip())
    check(num[i])


## Crie uma coleção de dados(lista,tupla,dicionário) e trabalhe com a função enumerate
# Aplicando condicionais.

listaMista = [1,"Chicago", 30, "Charlotte", 80, "Toronto"]

for i, valor in enumerate(listaMista):
    if i > 2:
        break
    else:
        print(valor)


from flask import Flask, jsonify, request
from flask_cors import CORS
import utils

app = Flask(__name__)
CORS(app, supports_credentials=True)

@app.route("/get_total", methods=["GET"])
def get_total():
    return jsonify(count=utils.query_counts())

@app.route("/get_request/<int:id>", methods=["GET"])
def get_request_info(id):
    src_ip, dest_ip, datas = utils.query_request(id)
    return jsonify(src_ip=src_ip, dest_ip=dest_ip, datas=datas)

@app.route("/get_src_ip_rank", methods=["GET"])
def get_src_ip_rank():
    name, counts = utils.query_src_rank()
    return jsonify(name=name,counts=counts)

@app.route("/get_dest_ip_rank", methods=["GET"])
def get_dest_ip_rank():
    name, counts = utils.query_host_rank()
    return jsonify(name=name,counts=counts)

@app.route("/get_interests", methods=["GET"])
def get_interests():
    names, series = utils.analysis_interests()
    return jsonify(names=names,series=series)

@app.route("/get_gps_points", methods=["GET"])
def get_gps_points():
    points = utils.gen_gps_point()
    return jsonify(points=points)

if __name__ == '__main__':
    app.run('0.0.0.0')