# Write a program that asks for a number
# It would ask this many times to enter an integer
# If all the integers are entered, it should print the sum and average of these
# integers like:
# Sum: 22, Average: 4.4
num = int(input("Enter a number: "))
i = 0
total_sum = 0
while i < num:
    temp = int(input(f"Enter {i} Integer: "))
    total_sum += temp
    i += 1

print(f"Sum: {total_sum} , Average: {total_sum / num}")


import sys
from PyQt5.QtWidgets import *
from Views.editFileWindow import Ui_editFileWindow
from Model.model import *

class EditFileWindow(QDialog):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.ui = Ui_editFileWindow()
        self.ui.setupUi(self)

        self.data = None

        # Connect addFileButton to add_file()
        self.ui.saveFileButton.clicked.connect(lambda: self.save_file())

        # Connect cancelButton to close function
        self.ui.cancelButton.clicked.connect(lambda: self.close())

        # Connect deleteButton to delete_file()
        self.ui.deleteButton.clicked.connect(lambda: self.delete_file())

    # Save a file
    def save_file(self):
        """
        Save changes made to the current files's data.
        """
        # Check if any fields are empty
        if self.ui.boxNumberInput.text() == "NULL":
            msg = "Must assign a box number to this file"
            self.show_popup("Error", msg)
            return
        elif self.ui.boxNumberInput.text() == "":
            msg = "Box Number can't be empty"
            self.show_popup("Error", msg)
            return
        elif self.ui.firstNameInput.text() == "":
            msg = "First Name can't be empty"
            self.show_popup("Error", msg)
            return
        elif self.ui.lastNameInput.text() == "":
            msg = "Last Name can't be empty"
            self.show_popup("Error", msg)
            return
        elif self.ui.yearInput.text() == "":
            msg = "Year can't be empty"
            self.show_popup("Error", msg)
            return

        # Check if the box number in the input field exists
        try:
            exists = box_number_exists(self.ui.boxNumberInput.text())
        except:
            self.show_popup("Error", "Box Number must be a whole number")
            return

        # If box doesn't exist then warn the user
        if not exists:
            msg = f"Box {self.ui.boxNumberInput.text()} does not exist. Either enter a different box number or create a new box."
            self.show_popup("Error", msg)
            return

        # Try to save the data
        try:
            form_data = (self.ui.boxNumberInput.text(),
                self.ui.typeComboBox.currentText(),
                self.ui.lastNameInput.text(),
                self.ui.firstNameInput.text(),
                self.ui.officeComboBox.currentText(),
                self.ui.yearInput.text(), self.data[0])
            update_file(form_data)
        except ValueError:
            self.show_popup("Error", "Box Number must be a whole number")
        else:
            self.close()
            self.show_popup("Success", f"You have saved changes to {form_data[3]} {form_data[2]}")

        self.parent().load_data()

    # Delete a file
    def delete_file(self):
        """
        Delete a file from the database.
        """
        # Confirm user wants to delete file
        delete = self.delete_file_confirmation()

        # If user confirms they want to delete, then try to do it
        if delete:
            try:
                delete_file(self.data[0])
            except:
                self.show_popup("Error", "Problem deleting file.")
            else:
                self.show_popup("Success", f"You have deleted {self.data[4]} {self.data[3]}'s file.")

        self.close()
        self.parent().load_data()

    # Confirm you want to delete the box
    def delete_file_confirmation(self):
        """
        Prompts the user to confirm if they want to delete the file from the database.
        """
        msg = f"Are you sure you want to delete {self.data[4]} {self.data[3]}'s file? This action cannot be undone."

        reply = QMessageBox.question(self, "Delete Box",
                                     msg, QMessageBox.Yes |
                                     QMessageBox.No, QMessageBox.No)

        if reply == QMessageBox.Yes:
            return True
        else:
            return False

    # Error/Info Popups
    def show_popup(self, popup_type, popup_msg):
        """
        Display a popup with either an error or success message.

        This function takes a popup_type which is either Error or Succes, and a popup_msg to be displayed.
        """
        # Setup the MessageBox
        msg = QMessageBox()

        # Title the window
        msg.setWindowTitle(f"{popup_type}")

        # Set text inside the window
        if popup_type == "Error":
            msg.setText(f"Error: {popup_msg}")
        elif popup_type == "Success":
            msg.setText(f"Success: {popup_msg}")

        # Set the icon
        if popup_type == "Error":
            msg.setIcon(QMessageBox.Warning)
        elif popup_type == "Success":
            msg.setIcon(QMessageBox.Information)

        # Add buttons to the bottom
        msg.setStandardButtons(QMessageBox.Cancel)

        x = msg.exec_()

    def pass_data(self, data):
        self.data = data
        self.load_input_fields(data)

    def load_input_fields(self, data):
        self.ui.boxNumberInput.setText(str(self.data[1]))
        self.ui.typeComboBox.setCurrentText(self.data[2])
        self.ui.firstNameInput.setText(self.data[4])
        self.ui.lastNameInput.setText(self.data[3])
        self.ui.officeComboBox.setCurrentText(self.data[5])
        self.ui.yearInput.setText(self.data[6])


import storage
import combat
import skill_check

def scene_1():
    storage.clear()
    choice = None
    while choice is None:
        user_input = input("""
        You have entered the Castle Dungeons..
        It is dark, however thankfully your torch is lit and
        you can see up to 20 feet away from you.
        The stone walls are damp, the smell of rats and orcs is strong.
        You walk down a narrow corridor, until you reach a thick stone wall.

        The corridor continues on the left, and on the right.

        What do you do? Enter below the number.

        1 - Turn left
        2 - Turn right
        > """)

        if user_input == "1":
            choice = "1"
            scene_2()
        elif user_input == "2":
            choice = "2"
            scene_3()
        else:
            print("""
            Not a valid choice, type a number """)

def scene_2():
    storage.clear()
    choice = None
    while choice is None:
        user_input = input("""
        From the darkness behind you.. you hear a strange noise.

        What do you do? Enter below the number.

        1 - Continue walking
        2 - Stop to listen
        > """)
        if user_input == "1":
            choice = "1"
            combat.combat()
        elif user_input == "2":
            choice = "2"
            skill_check.skill_check()
        else:
            print("  Not a valid choice, type a number")

def scene_3():
    storage.clear()
    choice = None
    while choice is None:
        user_input = input("""
    From the darkness ahead of you.. you hear a strange noise.

    What do you do?

    1 - Continue walking
    2 - Stop to listen

    > """)
        if user_input == "1" or user_input == "continue":
            choice = "1"
            combat.combat()
        elif user_input == "2" or user_input == "stop":
             choice = "2"
             skill_check.skill_check()
        else:
            print("  Not a valid choice, type a number")


from collections import defaultdict
import random, math
import numpy as np
import pickle
from functools import partial
class QLearningAgent:
    def __init__(self, alpha, epsilon, discount, get_legal_actions):

        self.get_legal_actions = get_legal_actions
        #self._qvalues = defaultdict(lambda: defaultdict(lambda: 0))
        self._qvalues = defaultdict(partial(defaultdict,int))
        self.alpha = alpha
        self.epsilon = epsilon
        self.discount = discount

    def get_qvalue(self, state, action):
        return self._qvalues[state][action]

    def set_qvalue(self,state,action,value):
        self._qvalues[state][action] = value

    def get_str_qvalue(self,state):
        return str(self._qvalues[state])

    def get_value(self, state):

        possible_actions = self.get_legal_actions(state)

        #If there are no legal actions, return 0.0
        if len(possible_actions) == 0:
            return 0.0

        value=None
        for action in possible_actions:
            val=self.get_qvalue(state,action)
            if value is None:
                value=val
            if value<val:
                value=val

        return value

    def update(self, state, action, reward, next_state):

        #agent parameters
        gamma = self.discount
        learning_rate = self.alpha

        Q= (1-learning_rate) * self.get_qvalue(state,action) + learning_rate * (reward+gamma*self.get_value(next_state))

        self.set_qvalue(state, action, Q)

    def get_best_action(self, state):

        possible_actions = self.get_legal_actions(state)

        #If there are no legal actions, return None
        if len(possible_actions) == 0:
            return None

        value=None
        best_action=None
#         print(possible_actions)
        for action in possible_actions:
            val=self.get_qvalue(state,action)
            if value is None:
                value=val
                best_action=action
            if value<val:
                value=val
                best_action=action

        return best_action

    def get_action(self, state):

        # Pick Action
        possible_actions = self.get_legal_actions(state)
        action = None

        if len(possible_actions) == 0:
            return None

        epsilon = self.epsilon

        if random.random()<epsilon:
            chosen_action=random.choice(possible_actions)
        else:
            chosen_action=self.get_best_action(state)

        return chosen_action

    def save_param(self, path):
        with open(path, "wb") as p:
            pickle.dump(self._qvalues, p, protocol=pickle.HIGHEST_PROTOCOL)

    def load_param(self, path):
        try:
            with open(path, 'rb') as p:
                self._qvalues = pickle.load(p)
                # print(self._qvalues)
        except:
            print("load param failure! let's start from a empty q table!")
            #self._qvalues = defaultdict(lambda: defaultdict(lambda: 0))
            self._qvalues = defaultdict(partial(defaultdict,int))


from flask import Blueprint, abort, render_template, request, flash, redirect, url_for
from bson.objectid import ObjectId
from flask_login import current_user, login_required
from __init__ import mongo
from routes.image import uploadImage

bakery = Blueprint('bakery', __name__, template_folder="templates")

def role_required(role):
    # role required decorator
    # storing this here for now because it will only be used
    # for bakery routes and shop items
    """Checks if the current user has the required role"""
    def wrapper(function):

        def inner(*args, **kwargs):

            if current_user.is_anonymous:
                abort(401)
            else:
                if not current_user.role == role:
                    flash("Your account does not have permission to view this page.")
                    abort(401)

            return function()
        inner.__name__ = function.__name__
        return inner
    return wrapper

############################################################
# BAKERY ROUTES
############################################################

@bakery.route('/register-bakery', methods=["GET", "POST"])
@role_required("user")
@login_required
def register_bakery():
    if request.method == 'POST':

        ownerId = current_user.get_id()
        name = request.form.get('name')
        email = request.form.get('email')
        phonenumber = request.form.get('phonenumber')
        address = request.form.get('address')

        newBakery = {
            "ownerId": ownerId,
            "name": name,
            "email": email,
            "phonenumber": phonenumber,
            "address": address,
            "image": "default.jpg",
            "description": ""
        }

        isUniqueEmail = mongo.db.bakeries.find({"email": email}).count() == 0
        isUniquePhone = mongo.db.bakeries.find(
            {"phonenumber": phonenumber}).count() == 0
        isUniqueOwner = mongo.db.bakeries.find(
            {"ownerId": ownerId}).count() == 0

        if(isUniqueEmail and isUniquePhone and isUniqueOwner):
            mongo.db.bakeries.insert_one(newBakery)
            flash("Bakery Regsistration Application Sent Successfully.")
        else:
            if not isUniqueOwner:
                flash("You have already submitted this form.")
            elif not isUniqueEmail:
                flash("Your bakery email has already been registered.")
            elif not isUniquePhone:
                flash("Your bakery phone number has already been registered")

        return render_template("register-bakery.html")

    else:
        return render_template("register-bakery.html")

@bakery.route('/bakery/<bakeryId>')
def view_bakery(bakeryId):
    bakery = mongo.db.bakeries.find_one_or_404({"_id": ObjectId(bakeryId)})

    bakeryItems = mongo.db.shop_items.find({"bakeryId": bakery["_id"]})

    context = {
        "name": bakery["name"],
        "address": bakery["address"],
        "image": bakery["image"],
        "description": bakery["description"],
        "is_owner": current_user.get_id() == bakery["ownerId"],
        "shop_items": bakeryItems
    }

    return render_template("bakery-store.html", **context)

@bakery.route('/mybakery')
@role_required("bakery")
@login_required
def my_bakery():
    bakery = mongo.db.bakeries.find_one_or_404(
        {"ownerId": current_user.get_id()})
    bakery_id = bakery["_id"]
    return redirect(url_for("bakery.view_bakery", bakeryId=bakery_id))

@bakery.route('/edit-bakery-profile', methods=["GET", "POST"])
@role_required("bakery")
@login_required
def edit_bakery():
    """Allow the user to edit their bakery's store page"""

    bakery = mongo.db.bakeries.find_one_or_404(
        {"ownerId": current_user.get_id()})

    # if the user does not own this bakery then redirect user to login.
    if not bakery["ownerId"] == current_user.get_id():
        abort(401)

    if request.method == 'POST':

        name = request.form.get("name")
        email = request.form.get("email")
        phonenumber = request.form.get("phonenumber")
        address = request.form.get("address")
        description = request.form.get("desc")

        # upload the image and save the path if successful
        image = uploadImage(request, bakery["image"])

        mongo.db.bakeries.update_one({
            "_id": bakery["_id"]
        },
            {
            '$set': {
                'name': name,
                'email': email,
                'phonenumber': phonenumber,
                'address': address,
                'image': image,
                'description': description
            }
        })

        return redirect(url_for("bakery.my_bakery"))
    else:

        context = {
            "name": bakery["name"],
            "email": bakery["email"],
            "phonenumber": bakery["phonenumber"],
            "address": bakery["address"],
            "image": bakery["image"],
            "description": bakery["description"]
        }

        return render_template("edit-bakery-store.html", **context)


import sys

def readdata(engfile,frenchfile,dutchfile,germanfile,swedishfile,traindata):
	with open(engfile,'r') as eng, open(frenchfile, 'r') as fr, open(dutchfile, 'r') as du, open(germanfile, 'r') as ge, open(swedishfile, 'r') as sw, open(traindata,'a') as train:
		i=0
		for line in eng.readlines():
			if(i<2500):
				msg='1 '+str(line)
				train.write(msg)
				i+=1
		i=0
		for line in fr.readlines():
			if(i<2500):
				msg='2 '+str(line)
				train.write(msg)
				i+=1
		i=0
		for line in du.readlines():
			if(i<2500):
				msg='3 '+str(line)
				train.write(msg)
				i+=1
		i=0
		for line in ge.readlines():
			if(i<2500):
				msg='4 '+str(line)
				train.write(msg)
				i+=1
		i=0
		for line in sw.readlines():
			if(i<2500):
				msg='5 '+str(line)
				train.write(msg)
				i+=1

if __name__=='__main__':
	engfile=sys.argv[1]
	frenchfile=sys.argv[2]
	dutchfile=sys.argv[3]
	germanfile=sys.argv[4]
	swedishfile=sys.argv[5]
	traindata=sys.argv[6]
	readdata(engfile,frenchfile,dutchfile,germanfile,swedishfile,traindata)


import sqlite3
import ftplib
#connect to our local database
#based off of http://kaikaichen.com/?p=59
conn = sqlite3.connect('edgar_idx.db')
cur = conn.cursor()

ftp = ftplib.FTP('ftp.sec.gov')
ftp.login()

def query(name=None, startDate='1993-01-01', endDate='2017-01-01', type='8-K'):
    print(name, startDate, endDate, type)
    cur.execute("SELECT * FROM idx WHERE date>? and conm LIKE ? and type=?", (startDate, '%'+name+'%', type))
    paths = cur.fetchall()
    for path in paths:
        print(path[1] + ' ' + path[2] + ' ' + path[3])
        saveName='-'.join([path[1], path[2], path[3]])
        saveNameNoSlashes = ''
        for char in saveName:
                if char != '/':
                    saveNameNoSlashes += char
        location = path[4].strip()
        with open(saveNameNoSlashes, 'wb') as f:
            ftp.retrbinary('RETR %s' %location, f.write)
#query to
query('apple', '1993-01-01', '1995-01-01', '10-K')
query('Microsoft', '1993-01-01', '1995-01-01', '10-K')
query('apple', '2012-01-01')
query('apple', '1993-01-01', '1995-01-01', '10-K')
query('tesla', '2012-01-05')


import os
import re
import shlex
import sys
from setuptools import setup

exec(open('./_version.py').read())

if sys.argv[-1] == "publish":
    os.system(shlex.quote("cleanpy ."))
    os.system(shlex.quote("python setup.py sdist"))
    os.system(shlex.quote(f"twine upload dist/HSPsquared-{__version__}.tar.gz"))
    sys.exit()

README = open("./README.md").read()

pypi_map = {"jupyter-lsp-python": "python-lsp-server[all]"}

def process_env_yaml(fname, dev=False):
    yaml_lines = []
    with open("environment.yml") as fp:
        collect = False
        for line in fp.readlines():
            # Handle comments
            line = line.split("#")[0].rstrip()

            # Handle blank lines
            if not line.strip():
                continue

            line = line.strip(" -").replace(" ", "")

            if collect is True:
                words = re.split("=|<|>", line)
                # Shouldn't have interactivity, development tools, conda, pip,
                # or python as a dependency.
                if dev is True:
                    if words[0] in ["conda", "conda-build", "hdf5", "nb_conda" "pip", "python", "pip:"]:
                        continue
                else:
                    if words[0] in [
                        "jupyterlab",
                        "ipywidgets",
                        "matplotlib",
                        "conda",
                        "conda-build",
                        "pip",
                        "python",
                        "lckr-jupyterlab-variableinspector",
                        "jupyter-lsp-python",
                        "jupyterlab-lsp",
                        "pip:",
                        "hdf5",
                        "nb_conda",
                    ]:
                        continue

                # On PyPI pytables is tables.
                if words[0] == "pytables":
                    line = line.replace("pytables", "tables")

                yaml_lines.append(line)

            if line.rstrip() == "dependencies:":
                collect = True
    yaml_lines = [pypi_map.get(i, i) for i in yaml_lines]
    return yaml_lines

install_requires = process_env_yaml("environment.yml")
print(install_requires)
extras_require = {
    "dev": process_env_yaml("environment_dev.yml", dev=True) + ["cleanpy", "twine"]
}

setup(
    name="HSPsquared",
    version=__version__,
    description="Hydrological Simulation Program - Python",
    long_description=README,
    classifiers=[
        # Get strings from
        # http://pypi.python.org/pypi?%3Aaction=list_classifiers
        "Development Status :: 5 - Production/Stable",
        "Intended Audience :: Science/Research",
        "Intended Audience :: End Users/Desktop",
        "Intended Audience :: Developers",
        "Environment :: Console",
        "License :: OSI Approved :: GNU Affero General Public License v3",
        "Natural Language :: English",
        "Operating System :: OS Independent",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Topic :: Scientific/Engineering",
        "Topic :: Software Development :: Libraries :: Python Modules",
    ],
    keywords="hydrology hydrological hydraulic hspf",
    author="RESPEC, Inc",
    author_email="",
    url="http://www.respec.com/product/hydrologic-simulation-program-python-hsp%C2%B2/",
    packages=["HSP2", "HSP2tools", "HSP2IO"],
    py_modules=['_version'],
    include_package_data=True,
    package_data={"HSP2tools": ["data/*"]},
    zip_safe=False,
    install_requires=install_requires,
    extras_require=extras_require,
    entry_points={"console_scripts": ["hsp2=HSP2tools.HSP2_CLI:main"]},
    test_suite="tests",
    python_requires=">3.6",
)


import enum
import typing

class ChannelMode(enum.IntEnum):
    """Channel mode."""

    Servo = 0
    """The channel is a servo."""
    ServoMultiplied = 1  # unused, as far as I can tell
    """This value is defined within the Maestro Control Centre, but unused.

    It is not recommended to use this channel mode, as its purpose is unclear."""
    Output = 2
    """The channel is an output.

    Output channels are controlled with a value in the range [0, 1023], which maps onto
    the range [0, Vcc] volts."""
    Input = 3
    """The channel is an input.

    The value is in the range [0, 1), corresponding to the input voltage range [0, Vcc]
    volts."""

class USCParameter(enum.IntEnum):
    """Constants for getting and setting Maestro parameters."""

    Initialized = 0
    ServosAvailable = 1
    ServoPeriod = 2
    SerialMode = 3
    SerialTimeout = 6
    ChannelModes0To3 = 12
    ChannelModes4To7 = 13
    ChannelModes8To11 = 14
    ChannelModes12To15 = 15
    ChannelModes16To19 = 16
    ChannelModes20To23 = 17

    ServoMultiplier = 26

    # Servo 0 is at base, servo 1 is 9 up, servo 2 is 18 up, and so on.
    ServoHomeBase = 30
    ServoMinBase = 32
    ServoMaxBase = 33
    ServoNeutralBase = 34
    ServoRangeBase = 36
    ServoSpeedBase = 37
    ServoAccelerationBase = 38

_parameter_sizes: typing.Dict[USCParameter, int] = {
    USCParameter.Initialized: 8,
    USCParameter.ServosAvailable: 8,
    USCParameter.ServoPeriod: 8,
    USCParameter.SerialMode: 2,
    USCParameter.SerialTimeout: 16,
    USCParameter.ChannelModes0To3: 8,
    USCParameter.ChannelModes4To7: 8,
    USCParameter.ChannelModes8To11: 8,
    USCParameter.ChannelModes12To15: 8,
    USCParameter.ChannelModes16To19: 8,
    USCParameter.ChannelModes20To23: 8,
    USCParameter.ServoMultiplier: 8,
    USCParameter.ServoHomeBase: 15,
    USCParameter.ServoMinBase: 8,
    USCParameter.ServoMaxBase: 8,
    USCParameter.ServoNeutralBase: 15,
    USCParameter.ServoRangeBase: 6,
    USCParameter.ServoSpeedBase: 8,
    USCParameter.ServoAccelerationBase: 8,
}
"""Parameter sizes in bits

To find the number of bytes for the response, take math.ceil(size / 8.0).
"""

def get_parameter_size(parameter: USCParameter) -> int:
    if parameter >= USCParameter.ServoHomeBase:
        parameter = (
            (parameter - USCParameter.ServoHomeBase) % 9
        ) + USCParameter.ServoHomeBase

    if parameter in _parameter_sizes:
        return _parameter_sizes[parameter]
    else:
        raise KeyError(parameter)

class Request(enum.IntEnum):
    """Constants for Maestro USB control requests.

    See :doc:`protocol` for more details about control requests."""

    GetRawParameter = 129  # REQUEST_GET_PARAMETER
    SetRawParameter = 130  # REQUEST_SET_PARAMETER
    GetVariablesMicroMaestro = 131  # REQUEST_GET_VARIABLES
    SetServoVariable = 132  # REQUEST_SET_SERVO_VARIABLE
    SetTarget = 133  # REQUEST_SET_TARGET
    ClearErrors = 134  # REQUEST_CLEAR_ERRORS
    GetVariablesMiniMaestro = 135  # REQUEST_GET_SERVO_SETTINGS
    GetStack = 136  # REQUEST_GET_STACK
    GetCallStack = 137  # REQUEST_GET_CALL_STACK
    SetPWM = 138  # REQUEST_SET_PWM
    Reinitialize = 144  # REQUEST_REINITIALIZE
    EraseScript = 160  # REQUEST_ERASE_SCRIPT
    WriteScript = 161  # REQUEST_WRITE_SCRIPT
    SetScriptDone = 162  # REQUEST_SET_SCRIPT_DONE
    RestartScriptAtSubroutine = 163  # REQUEST_RESTART_SCRIPT_AT_SUBROUTINE
    RestartScriptAtSubroutineWithParameter = (
        164  # REQUEST_RESTART_SCRIPT_AT_SUBROUTINE_WITH_PARAMETER
    )
    RestartScript = 165  # REQUEST_RESTART_SCRIPT
    StartBootloader = 255  # REQUEST_START_BOOTLOADER

class ServoVariableMask(enum.IntEnum):
    Speed = 0
    Acceleration = 128


# coding=utf-8
import cv2
import numpy
import matplotlib.pyplot as plot

cap = cv2.VideoCapture(0)
while 1:
    # get a frame
    ret, frame = cap.read()
    # show a frame
    cv2.imshow("capture", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        cv2.imwrite("now.jpg", frame)
        break
cap.release()
cv2.destroyAllWindows()


# Author:  DINDIN Meryll
# Date:    15/03/2019
# Project: graphs

try: from graphs.imports import *
except: from imports import *

# Object to run the optimized Dijkstra algorithm

class ShortestPath:

    def __init__(self, graph):

        self.G = graph

    def dijkstra(self, origin, goal):

        inf = float('inf')
        D = {origin: 0}
        Q = PQDict(D)
        P = {}
        U = set(self.G.keys())

        while U:

            (v, d) = Q.popitem()
            D[v] = d
            U.remove(v)
            if v == goal: break

            for w in self.G[v]:
                if w in U:
                    d = D[v] + self.G[v][w]
                    if d < Q.get(w, np.inf):
                        Q[w] = d
                        P[w] = v

        v = goal
        path = [v]

        while v != origin:
            v = P[v]
            path.append(v)

        path.reverse()

        return path

# Transform a Shapefile into a Dict Graph

class CityGraph:

    def __init__(self, shapefile):

        self.rds = geopandas.read_file(shapefile)

        self.dtf = []

        print('> Nodes extraction:')
        for line in tqdm.tqdm(self.rds.geometry.values):
            for idx, component in enumerate(list(line.coords)):
                x_1, y_1 = component
                try:
                    x_2, y_2 = list(line.coords)[idx+1]
                    distance = np.sqrt(np.square(x_2 - x_1) + np.square(y_2 - y_1))
                    self.dtf.append([x_1, y_1, x_2, y_2, distance])
                except: pass
                try:
                    x_2, y_2 = list(line.coords)[idx-1]
                    distance = np.sqrt(np.square(x_2 - x_1) + np.square(y_2 - y_1))
                    self.dtf.append([x_1, y_1, x_2, y_2, distance])
                except: pass

        self.dtf = pd.DataFrame(self.dtf, columns=['from_x', 'from_y', 'to_x', 'to_y', 'distance'])
        self.dtf = self.dtf.drop_duplicates()
        self.dtf['point_id'] = self.dtf.index

    def unify_points(self):

        print('> Nodes unification:')
        for row in tqdm.tqdm(self.dtf.values):
            tmp = self.dtf[(self.dtf.from_x == row[0]) & (self.dtf.from_y == row[1])]
            self.dtf.loc[tmp.index, 'point_id'] = min(tmp.point_id)

    def build_graph(self, dump=None):

        self.unify_points()
        graph = dict()

        print('> Build edges:')
        for point_id in tqdm.tqdm(self.dtf.point_id.unique()):
            point = self.dtf[self.dtf.point_id == point_id].values[0]
            edges = dict()
            for row in self.dtf[self.dtf.point_id == point_id].values:
                edges['{}:{}'.format(row[2], row[3])] = row[4]
            graph['{}:{}'.format(point[0], point[1])] = edges

        if not (dump is None): joblib.dump(graph, dump)
        else: return graph

# Automated way of linking two points on a map

class Trajectory:

    def __init__(self, graphfile):

        self.G = joblib.load(graphfile)
        self.P = ShortestPath(self.G)

    def update_graph(self, key, weight):

        old = self.G[key]
        for key in old.keys(): old[key] = weight
        self.G[key] = old

    def closest_key(self, longitude, latitude):

        keys = [(float(e.split(':')[0]), float(e.split(':')[1])) for e in self.G.keys()]
        dist = np.sum(np.abs(np.asarray(keys) - np.asarray([longitude, latitude])), axis=1)
        clos = keys[dist.argmin()]

        return '{}:{}'.format(clos[0], clos[1])

    def shortest_path(self, origin, goal):

        return self.P.dijkstra(origin, goal)

if __name__ == '__main__':

    graph = {'a': {'b': 1},
             'b': {'c': 2, 'b': 5},
             'c': {'d': 1},
             'd': {}}

    print(ShortestPath(graph).dijkstra('a', 'c'))


import os
import random
import pandas as pd
from sklearn.naive_bayes import MultinomialNB as MB
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import HashingVectorizer
from sklearn.metrics import confusion_matrix
from sklearn import metrics
import tarfile
import urllib.request
from abc  import abstractmethod
from os.path import exists

##################################################################################

                    ## Multinomial Naive Bayes classification ##

##################################################################################

def parse_20_news(directory):
        print("Parsing 20_news...")
        paths = []
        names = []

        for filename in os.scandir(directory):
            if filename.is_dir:
                paths.append(filename.path)
        for path in paths:
            name = path.split('.')[-1]
            if name in names:
                names.append(name+"_"+path.split('.')[-2])
                continue
            names.append(name)
        DATA = {name:[] for name in names}
        for i in range(len(paths)):
            texts = b''
            for filename in os.scandir(paths[i]):
                if filename.is_file:
                    with open(filename.path, "rb" ) as file:
                            DATA[names[i]].append(file.read())
        #print("Done")
        return DATA

class Naive_Bayes:
    def __init__(self,vectorizer_type = "Count",test_train_ratio=.2,Data = None):
        self.vectorizer = None
        self.test_train_ratio = test_train_ratio
        self.classifier = MB()
        if vectorizer_type =="Count": self.vectorizer = CountVectorizer(decode_error='ignore')
        #elif vectorizer_type == "Hash": self.vectorizer = HashingVectorizer(decode_error='ignore')
        elif vectorizer_type == "Tfidf": self.vectorizer = TfidfVectorizer(decode_error='ignore')
        else: raise(ValueError("Invalid vectorizer type"))

        if not Data:
            self.init_20_news_data()
            self.Data = parse_20_news('20news-18828')
        else:
            self.Data = Data
        self.Train_X,self.Train_Y,self.Test_X,self.Test_Y = self.split_data()

    def split_data(self):
        Train = []
        Test = []
        try:
            for key in self.Data:
                random.shuffle(self.Data[key])
                test = self.Data[key][0:int(len(self.Data[key])*self.test_train_ratio)]
                train = self.Data[key][int(len(self.Data[key])*self.test_train_ratio): len(self.Data[key])]
                Train+=([[doc,key] for doc in test])
                Test+=([[doc,key] for doc in train])
        except:
            raise(ValueError("Error in split_data()"))
           # print("issues")
        random.shuffle(Train)
        random.shuffle(Test)
        Train_X = [pair[0] for pair in Train]
        Test_X = [pair[0] for pair in Test]

        Train_Y = [pair[1] for pair in Train]
        Test_Y = [pair[1] for pair in Test]

        return Train_X,Train_Y,Test_X,Test_Y

    def train(self):
        print("Training...")
        try:
            transformed = self.vectorizer.fit_transform(self.Train_X)
            self.classifier.fit(transformed,self.Train_Y)
        except:
            raise(ValueError("Error in train()"))
        #print("Done")
    def test(self):
        print("Testing...")
        try:
            transformed = self.vectorizer.transform(self.Test_X)
            predict = self.classifier.predict(transformed)
        except:
            raise(ValueError("Error in test()"))
        #print("Done")
        return predict
    def confusion(self,prediction):
        return confusion_matrix(self.Test_Y,prediction,labels = list(set(self.Test_Y)))

    def init_20_news_data(self):
        if not exists("20news-18828"):
            print("Downloading 20 news data...")
            URL = "https://figshare.com/ndownloader/files/13356110"
            urllib.request.urlretrieve(URL, "temp_file")
            file = tarfile.open("temp_file")
            file.extractall()
            file.close()
            os.remove("temp_file")
            return
        print("20 news data exists in local directory")
        #print("Done")

##########################################################################

                             ## MAIN ##

###########################################################################

if __name__ == "__main__":
    Model_test = Naive_Bayes()
    Model_test.train()
    prediction =Model_test.test()
    print("Naive Bayes accuracy: ",metrics.accuracy_score(Model_test.Test_Y,prediction))


import argparse
import json
import os
import time
import torch

#=====START: ADDED FOR DISTRIBUTED======
from distributed import init_distributed, apply_gradient_allreduce, reduce_tensor
from torch.utils.data.distributed import DistributedSampler
#=====END:   ADDED FOR DISTRIBUTED======

from torch.utils.data import DataLoader
from wavenet import WaveNet
from mel2samp_onehot import Mel2SampOnehot
from utils import to_gpu

class CrossEntropyLoss(torch.nn.Module):
    def __init__(self):
        super(CrossEntropyLoss, self).__init__()
        self.num_classes = wavenet_config["n_out_channels"]

    def forward(self, inputs, targets):
        """
        inputs are batch by num_classes by sample
        targets are batch by sample
        torch CrossEntropyLoss needs
            input = batch * samples by num_classes
            targets = batch * samples
        """
        targets = targets.view(-1)
        inputs = inputs.transpose(1, 2)
        inputs = inputs.contiguous()
        inputs = inputs.view(-1, self.num_classes)
        return torch.nn.CrossEntropyLoss()(inputs, targets)

def load_checkpoint(checkpoint_path, model, optimizer):
    assert os.path.isfile(checkpoint_path)
    checkpoint_dict = torch.load(checkpoint_path, map_location='cpu')
    iteration = checkpoint_dict['iteration']
    optimizer.load_state_dict(checkpoint_dict['optimizer'])
    model_for_loading = checkpoint_dict['model']
    model.load_state_dict(model_for_loading.state_dict())
    print("Loaded checkpoint '{}' (iteration {})" .format(
          checkpoint_path, iteration))
    return model, optimizer, iteration

def save_checkpoint(model, optimizer, learning_rate, iteration, filepath):
    print("Saving model and optimizer state at iteration {} to {}".format(
          iteration, filepath))
    model_for_saving = WaveNet(**wavenet_config).cuda()
    model_for_saving.load_state_dict(model.state_dict())
    torch.save({'model': model_for_saving,
                'iteration': iteration,
                'optimizer': optimizer.state_dict(),
                'learning_rate': learning_rate}, filepath)

def train(num_gpus, rank, group_name, output_directory, epochs, learning_rate,
          iters_per_checkpoint, batch_size, seed, checkpoint_path):
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    #=====START: ADDED FOR DISTRIBUTED======
    if num_gpus > 1:
        init_distributed(rank, num_gpus, group_name, **dist_config)
    #=====END:   ADDED FOR DISTRIBUTED======

    criterion = CrossEntropyLoss()
    model = WaveNet(**wavenet_config).cuda()

    #=====START: ADDED FOR DISTRIBUTED======
    if num_gpus > 1:
        model = apply_gradient_allreduce(model)
    #=====END:   ADDED FOR DISTRIBUTED======

    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

    # Load checkpoint if one exists
    iteration = 0
    if checkpoint_path != "":
        model, optimizer, iteration = load_checkpoint(checkpoint_path, model,
                                                      optimizer)
        iteration += 1  # next iteration is iteration + 1

    trainset = Mel2SampOnehot(**data_config)
    # =====START: ADDED FOR DISTRIBUTED======
    train_sampler = DistributedSampler(trainset) if num_gpus > 1 else None
    # =====END:   ADDED FOR DISTRIBUTED======
    train_loader = DataLoader(trainset, num_workers=1, shuffle=False,
                              sampler=train_sampler,
                              batch_size=batch_size,
                              pin_memory=False,
                              drop_last=True)

    # Get shared output_directory ready
    if rank == 0:
        if not os.path.isdir(output_directory):
            os.makedirs(output_directory)
            os.chmod(output_directory, 0o775)
        print("output directory", output_directory)

    model.train()
    epoch_offset = max(0, int(iteration / len(train_loader)))
    # ================ MAIN TRAINNIG LOOP! ===================
    for epoch in range(epoch_offset, epochs):
        print("Epoch: {}".format(epoch))
        for i, batch in enumerate(train_loader):
            model.zero_grad()

            x, y = batch
            x = to_gpu(x).float()
            y = to_gpu(y)
            x = (x, y)  # auto-regressive takes outputs as inputs
            y_pred = model(x)
            loss = criterion(y_pred, y)
            if num_gpus > 1:
                reduced_loss = reduce_tensor(loss.data, num_gpus)[0]
            else:
                reduced_loss = loss.data[0]
            loss.backward()
            optimizer.step()

            print("{}:\t{:.9f}".format(iteration, reduced_loss))

            if (iteration % iters_per_checkpoint == 0):
                if rank == 0:
                    checkpoint_path = "{}/wavenet_{}".format(
                        output_directory, iteration)
                    save_checkpoint(model, optimizer, learning_rate, iteration,
                                    checkpoint_path)

            iteration += 1

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', '--config', type=str,
                        help='JSON file for configuration')
    parser.add_argument('-r', '--rank', type=int, default=0,
                        help='rank of process for distributed')
    parser.add_argument('-g', '--group_name', type=str, default='',
                        help='name of group for distributed')
    args = parser.parse_args()

    # Parse configs.  Globals nicer in this case
    with open(args.config) as f:
        data = f.read()
    config = json.loads(data)
    train_config = config["train_config"]
    global data_config
    data_config = config["data_config"]
    global dist_config
    dist_config = config["dist_config"]
    global wavenet_config
    wavenet_config = config["wavenet_config"]

    num_gpus = torch.cuda.device_count()
    if num_gpus > 1:
        if args.group_name == '':
            print("WARNING: Multiple GPUs detected but no distributed group set")
            print("Only running 1 GPU.  Use distributed.py for multiple GPUs")
            num_gpus = 1

    if num_gpus == 1 and args.rank != 0:
        raise Exception("Doing single GPU training on rank > 0")

    torch.backends.cudnn.enabled = True
    torch.backends.cudnn.benchmark = False
    train(num_gpus, args.rank, args.group_name, **train_config)


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

import os
import mne

dir_root='/Users/nm.costa/Desktop/'
filename='1_pycorder_1min'
filepath=os.path.join(dir_root, 'e1_debug_experiment_2/raw_pycorder_data', filename+'.vhdr')
raw = mne.io.read_raw_brainvision(filepath, preload=True)
out_filepath=os.path.join(dir_root, 'e1_debug_experiment_2/raw_pycorder_data', filename+'.fif')
raw.save(out_filepath)


from itertools import tee,zip_longest
from textblob import TextBlob
import nltk
import re
import math

def pairwise(iterable):
    """
    Adjacent pairs with overlap
    >>> list(pairwise(range(5)))
    [(0, 1), (1, 2), (2, 3), (3, 4), (4, None)]
    """
    a, b = tee(iterable) # tee returns n independent iterable objects
    next(b)
    # return an iterator that aggregate elements from each of the iterators
    # zip_longest((a,b,c),(1,2)) => ((a,1),(b,2),(c,_))
    return zip_longest(a, b)

def info_entropy(data,new_words): # new_word 是一个set
    word_prefix = {}
    word_suffix ={}
    word_pre_suf_entropy = {}
    for token in new_words:
        word_prefix[token] = {}
        word_suffix[token] = {}
    for line in data:
        line = ["start"] + line + ["end"]
        for i,token in enumerate(line[1:len(line)-1]):
            if token in new_words:
                if line[i] in word_prefix[token]: # 统计前缀
                    word_prefix[token][line[i]] +=1
                else:
                    word_prefix[token][line[i]] == 1
                if line[i+2] in word_suffix[token]: # 统计后缀
                    word_suffix[token][line[i+2]] +=1
                else:
                    word_suffix[token][line[i+2]] == 1
    for token in new_words:
        # calculate pre entropy
        total =0
        for k,v in word_prefix[token]:
            total += v
        pre_entropy = 0
        for k,v in word_prefix[token]:
            pre_entropy += -(v/total)*math.log(v/total)
        # suf entropy
        total = 0
        for k, v in word_suffix[token]:
            total += v
        suf_entropy = 0
        for k, v in word_suffix[token]:
            suf_entropy += -(v / total) * math.log(v / total)
        word_pre_suf_entropy[token] = [pre_entropy,suf_entropy]
    return word_pre_suf_entropy

line = [0]+[1,2,3,4]+[5]
print(line)


'''

7.15(금)
체육복

-번호는 체격순
-바로 앞이나 뒤에게만 빌려줄 수 있다.
-최대한  많은 학생
'''

from collections import deque
n = 5
lost = [2, 4]
reserve = [1, 3, 5]

def solution(n, lost, reserve):
    #여벌 체육복을 가져온 학생이 체육복을 도난당했을 수 있는 경우
    lost_s = set(lost)-set(reserve)
    reserve_s = set(reserve) - set(lost)

    for i in reserve_s:
        f = i-1
        b = i+1
        if f in lost_s:
            lost_s.remove(f)
        elif b in lost_s:
            lost_s.remove(b)
    return n - len(lost_s)


from platypus import NSGAII, Problem, Real
from Library.DataClass import *
from Library.RiskCalc import *

def calc_total_value(universe: Universe, weights: [float]):
    total = 0
    for i in range(universe.count):
        total += universe.universe_set[i].last_price * weights[i]

    return total

class OptPortfolio(Problem):

    def __init__(self, universe: Universe, buying_power: float):
        super(OptPortfolio, self).__init__(universe.count,1,1)
        self.universe = universe
        self.universe_historical_data = gen_universe_hist_data(universe.universe_set, "Adj. Close")
        self.buying_power = buying_power
        self.types[:] = Real(0,1)
        self.constraints[:] = "<=0"
        self.directions[:] = Problem.MAXIMIZE

    def evaluate(self, solution):
        solution.objectives[:] = gen_fitness_value(solution.variables, self.universe_historical_data)
        solution.constraints[0] = sum(solution.variables) - 1
        #solution.constraints[1] = calc_total_value(self.universe, solution.variables) - self.buying_power

def generate_portfolio(universe: Universe, buying_power: float, user_id: int):
    algorithm = NSGAII(OptPortfolio(universe,buying_power))
    algorithm.run(1000)

    feasible_solutions = [s for s in algorithm.result if s.feasible]

    sol = feasible_solutions[0]

    alloc = {}
    for i in range(universe.count):
        alloc[universe.universe_set[i].ticker] = sol.variables[i]

    print(alloc)

    portf = Portfolio(user_id=user_id,buying_power=buying_power*(1-sum(sol.variables)),assets=universe.universe_set,asset_alloc=alloc)

    return portf


#cadena de caracteres preformateado(respeta las lineas)
#codigo repetido, (Don't repeat yourself) condigo sin funciones
menu= """
Bienvenido al conversor de moneda ❤

1- pesos colombianos
2- pesos argentinos
3- pesos mexicanos

elige una opcion
 """
 # una vez que haya codigo en los bloques se puede quitar el pass
opcion =int(input(menu))
if opcion == 1:
    #bloque de codigo 1
    colombian_pesos = input("¿Cuantos pesos colombianos tienes?")
    colombian_pesos =  float(colombian_pesos)
    valor_dolar=3875
    dolares = colombian_pesos/valor_dolar
    dolares = round(dolares, 2)#funcion para redondear los decimales (el numero flotanto, cuantos decimales tener)
    dolares = str(dolares)
    print("Tienes $"+ dolares +" dolares")

elif opcion ==2:
    #bloque de codigo 1
    pesos_argentinos = input("¿Cuantos pesos argentinos tienes?")
    pesos_argentinos =  float(pesos_argentinos)
    valor_dolar=65
    dolares = pesos_argentinos/valor_dolar
    dolares = round(dolares, 2)#funcion para redondear los decimales (el numero flotanto, cuantos decimales tener)
    dolares = str(dolares)
    print("Tienes $"+ dolares +" dolares")

elif opcion==3:
    #bloque de codigo 1
    pesos_mexicanos = input("¿Cuantos pesos mexicanos tienes?")
    pesos_mexicanos =  float(pesos_mexicanos)
    valor_dolar=65
    dolares = pesos_mexicanos/valor_dolar
    dolares = round(dolares, 2)#funcion para redondear los decimales (el numero flotanto, cuantos decimales tener)
    dolares = str(dolares)
    print("Tienes $"+ dolares +" dolares")

else:
    print("digite una opcion valida")

dolares_2 = input("digite cuantos dolares tiene: ")
cop = str(round(float(dolares_2) * 3875, 2) )
print("usted tiene $"+cop +" pesos colombianos")


def find_substrings(string, substring):
    res = []
    counter = 0
    while counter < len(string):
        counter = string.find(substring, counter)
        if counter == -1:
            return res
        else:
            res.append(counter+1)
            counter += 1
    return res

numOfRows = int(input())
for _ in range(numOfRows):
    row = input().split()
    array = find_substrings(row[0], row[1])

    if(len(array) <= 0):
        print("Not Found")
    else:
        print(len(array))
        print(*array)


from operator import *
from functools import reduce
def do(i):
    w={}
    o={'NOT':invert,'AND':and_,'OR':or_,'LSHIFT':lshift,'RSHIFT':rshift}
    f=sorted(i.split('\n')[:-1])[::-1]
    g=lambda v:w[v] if v.isalpha() else int(v)
    while f:
        l=f.pop()
        a,b=l.split(' -> ')
        p,c=reduce(lambda x,y:x[y not in o].append(y) or x,a.split(),([],[]))
        try:w[b]=o[p[0]](*map(g,c)) if len(p) else g(c[0])
        except KeyError:f.insert(0,l)
    return w
inpa=open('input/7.txt').read()
parta=do(inpa)
inpb=inpa.replace('14146 -> b', str(parta['a']) + ' -> b')
print('parta', parta['a'])
print('partb', do(inpb)['a'])


import arcpy
import arcgis
import datetime
import os
import numpy

arcpy.env.overwriteOutput = True
def main():
    #TableImport()
    #caseBaseTable()
    violationsBaseTable()
    #actionsBaseTable()
    #Action()
    #Inspection()
    #Contact()
    Duration()
    #Compliance()
    #stDev()
    #Employee()
    #AddressField()
    #Geocode()
    #Update()

def TableImport():
    ceTables = {"CASE_MAIN" : ["STARTED", "1500"], "CASE_INSPECTIONS": ["COMPLETED_DATE", "1500"], "Case_Actions": ["COMPLETED_DATE", "1500"], "Case_Violations2": ["DATE_OBSERVED", "365"], "LICENSE2_Main": ["ISSUED", "1500"], "LICENSE2_Inspections": ["COMPLETED_DATE", "1500"], "LICENSE2_Actions": ["COMPLETED_DATE", "365"], "Permit_Reviews": ["DATE_SENT", "365"], "LICENSE2_REVIEWS": ["DATE_SENT", "365"], "Inspections": ["COMPLETED_DATE", "365"], "LICENSE2_Inspections": ["COMPLETED_DATE", "365"]}
    originalPath = "\\\\GISAPP\\Workspace\\sdeFiles\\LogosLive.sde"
    newPath = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb"
    for key, value in ceTables.items():
        originalFile = originalPath + "\\CRW_PROD.dbo." + key
        sql = value[0] + ">= CURRENT_DATE - " + value[1]
        in_mem_table = arcpy.management.CopyRows(originalFile, r"in_memory\tbl")
        arcpy.conversion.TableToTable(in_mem_table, newPath, key, sql)
        del in_mem_table

def caseBaseTable():
    #Creates Base Table to be consumed by Dashboard
    caseMain = ["CASE_NO", "STARTED", "CLOSED", "CaseSubType", "STATUS", "SITE_ADDR"]
    caseTable = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\CASE_MAIN"
    newTable = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb"
    #Creating Field mapping
    fieldmappings = arcpy.FieldMappings()
    fieldmappings.addTable(caseTable)
    #Removing all Fields that aren't needed in the end product
    for f in fieldmappings.fields:
            if f.name not in caseMain:
                fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex(f.name))
    arcpy.conversion.TableToTable(caseTable, newTable, "Dashboard_Case", "", fieldmappings)

def violationsBaseTable():
    #Creates Base Table to be consumed by Dashboard
    violationsMain = ["CASE_NO", "DATE_OBSERVED", "DATE_CORRECTED", "Violation_Type", "USERID", "STATUS"]
    violationsTable = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Case_Violations2"
    newTable = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb"
    #Creating Field mapping
    fieldmappings = arcpy.FieldMappings()
    fieldmappings.addTable(violationsTable)
    #Removing all Fields that aren't needed in the end product
    for f in fieldmappings.fields:
            if f.name not in violationsMain:
                fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex(f.name))
    arcpy.conversion.TableToTable(violationsTable, newTable, "Dashboard_Violations", "", fieldmappings)
    print("done")
def actionsBaseTable():
    #Creates Base Table to be consumed by Dashboard
    violationsMain = ["CASE_NO", "ACTION_BY", "ACTION_DATE", "COMPLETED_DATE", "ACTION_TYPE"]
    violationsTable = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Case_Actions"
    newTable = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb"
    #Creating Field mapping
    fieldmappings = arcpy.FieldMappings()
    fieldmappings.addTable(violationsTable)
    #Removing all Fields that aren't needed in the end product
    for f in fieldmappings.fields:
            if f.name not in violationsMain:
                fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex(f.name))
    arcpy.conversion.TableToTable(violationsTable, newTable, "Dashboard_Actions", "", fieldmappings)

def Action():
    firstAction = {}
    recentAction = {}
    #Identify First Action(Chronology or Inspection) taken on a case for use in the Dashboard
    #Search through Actions to find first response
    actionsLayer = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Case_Actions"
    actionsFields = ["CASE_NO", "ACTION_BY", "ACTION_TYPE", "COMPLETED_DATE"]
    #Create Working Dictionary to determine which Action is first before adding to the First Action Dictionary
    workingDict = {}
    with arcpy.da.SearchCursor(actionsLayer, actionsFields) as Scur:
        for row in Scur:
            if row[0] in workingDict.keys():
                workingDict[row[0]].append(row[3])
            else:
                workingDict[row[0]] = [row[3]]

    dashLayer = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Dashboard_Case"
    arcpy.management.AddField(dashLayer, "firstActionDate", "DATE")
    arcpy.management.AddField(dashLayer, "recentActionDate", "DATE")

    fieldsFA = ["CASE_NO", "firstActionDate", "recentActionDate"]
    with arcpy.da.UpdateCursor(dashLayer, fieldsFA) as Ucur:
        for row in Ucur:
            for key, value in workingDict.items():
                if key == row[0]:
                    fA = min(value)
                    rA = max(value)
                    row[1] = fA
                    row[2] = rA
                    Ucur.updateRow(row)

def Inspection():
    inspection = {}
    actionsLayer = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Inspections"
    actionsFields = ["ActivityID", "COMPLETED_DATE"]

    workingDict = {}
    with arcpy.da.SearchCursor(actionsLayer, actionsFields) as Scur:
        for row in Scur:
            if row[0] in workingDict.keys():
                workingDict[row[0]].append(row[1])
            else:
                workingDict[row[0]] = [row[1]]

    dashLayer = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Dashboard_Case"
    arcpy.management.AddField(dashLayer, "firstInspectionDate", "DATE")
    arcpy.management.AddField(dashLayer, "recentInspectionDate", "DATE")
    fields = ["CASE_NO", "firstInspectionDate", "recentInspectionDate"]
    with arcpy.da.UpdateCursor(dashLayer, fields) as Ucur:
        for row in Ucur:
            for key, value in workingDict.items():
                if key == row[0]:
                    ins = min(value)
                    newins = max(value)
                    row[1] = ins
                    row[2] = newins
                    Ucur.updateRow(row)

def Contact():
    dashLayer = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Dashboard_Case"
    arcpy.management.AddField(dashLayer, "firstContactDate", "DATE")
    fields = ["firstActionDate", "firstInspectionDate", "firstContactDate"]
    with arcpy.da.UpdateCursor(dashLayer, fields) as Ucur:
        for row in Ucur:
            if row[0] is not None and row[1] is not None:
                datelst = []
                datelst.append(row[0])
                datelst.append(row[1])
                newDate = min(datelst)
                row[2] = newDate
                Ucur.updateRow(row)
            if row[0] is not None and row[1] is None:
                row[2] = row[0]
                Ucur.updateRow(row)
            if row[0] is None and row[1] is not None:
                row[2] = row[1]
                Ucur.updateRow(row)

    dashLayer = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Dashboard_Case"
    arcpy.management.AddField(dashLayer, "recentContactDate", "DATE")
    fields = ["recentActionDate", "recentInspectionDate", "recentContactDate"]
    with arcpy.da.UpdateCursor(dashLayer, fields) as Ucur:
        for row in Ucur:
            if row[0] is not None and row[1] is not None:
                datelst = []
                datelst.append(row[0])
                datelst.append(row[1])
                newDate = min(datelst)
                row[2] = newDate
                Ucur.updateRow(row)
            if row[0] is not None and row[1] is None:
                row[2] = row[0]
                Ucur.updateRow(row)
            if row[0] is None and row[1] is not None:
                row[2] = row[1]
                Ucur.updateRow(row)

def Duration():
    dashLayer = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Dashboard_Case"
    arcpy.management.AddField(dashLayer, "DurationContact", "DOUBLE")
    fields = ["STARTED", "firstContactDate", "DurationContact"]
    with arcpy.da.UpdateCursor (dashLayer, fields) as Ucur:
        for row in Ucur:
            if row[1] is None and row[0] is not None:
                duration = datetime.datetime.today() - row[0]
                row[2] = duration.days
                Ucur.updateRow(row)
            if row[1] is not None and row[0] is not None:
                duration = row[1] - row[0]
                row[2] = duration.days
                Ucur.updateRow(row)
            else:
                pass

    dashLayer = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Dashboard_Violations"
    arcpy.management.AddField(dashLayer, "DurationComply", "DOUBLE")
    fields = ["DATE_OBSERVED", "DATE_CORRECTED", "DurationComply"]
    with arcpy.da.UpdateCursor (dashLayer, fields) as Ucur:
        for row in Ucur:
            if row[1] is None and row[0] is not None:
                duration = datetime.datetime.today() - row[0]
                row[2] = duration.days
                Ucur.updateRow(row)
            if row[1] is not None and row[0] is not None:
                duration = row[1] - row[0]
                row[2] = duration.days
                Ucur.updateRow(row)
            else:
                pass

    dictFields = ["CASE_NO", "DurationComply"]
    testDict = {}
    with arcpy.da.SearchCursor(dashLayer, dictFields) as Scur:
        for row in Scur:
            if row[0] in testDict.keys():
                testDict[row[0]].append(row[1])
            else:
                testDict[row[0]] = [row[1]]
    testlst = []
    perclst = []
    for key, value in testDict.items():
        newset = set(value)
        for x in newset:
            testlst.append(x)

    percent = numpy.percentile(testlst, 90)
    print(percent)
    print(testlst)

def Compliance():
    #Calculate compliance of Violation 10 days ultimate goal/ 30 day fallback goal
    violationPath = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Dashboard_Violations"
    arcpy.management.AddField(violationPath, "Compliance10", "TEXT")
    fields = ["DATE_OBSERVED", "DATE_CORRECTED", "Compliance10"]
    with arcpy.da.UpdateCursor(violationPath, fields) as Ucur:
        for row in Ucur:
            if row[0] is not None and row[1] is not None:
                duration = row[1] - row[0]
                comply = duration.days
                if comply <= 10:
                    row[2] = "Full Compliance"
                    Ucur.updateRow(row)
                else:
                    row[2] = "Not within Compliance"
                    Ucur.updateRow(row)
            if row[0] is not None and row[1] is None:
                duration = datetime.datetime.today() - row[0]
                comply = duration.days
                if comply <= 10:
                    row[2] = "Compliance to be Determined"
                    Ucur.updateRow(row)
                if comply > 10:
                    row[2] = "Not within Compliance"
                    Ucur.updateRow(row)
        del Ucur

    arcpy.management.AddField(violationPath, "Compliance30", "TEXT")
    fields = ["DATE_OBSERVED", "DATE_CORRECTED", "Compliance30"]
    with arcpy.da.UpdateCursor(violationPath, fields) as Ucur:
        for row in Ucur:
            if row[0] is not None and row[1] is not None:
                duration = row[1] - row[0]
                comply = duration.days
                if comply <= 30:
                    row[2] = "Full Compliance"
                    Ucur.updateRow(row)
                else:
                    row[2] = "Not within Compliance"
                    Ucur.updateRow(row)
            if row[0] is not None and row[1] is None:
                duration = datetime.datetime.today() - row[0]
                comply = duration.days
                if comply <= 30:
                    row[2] = "Compliance to be Determined"
                    Ucur.updateRow(row)
                if comply > 30:
                    row[2] = "Not within Compliance"
                    Ucur.updateRow(row)

def stDev():
    mainTable = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Dashboard_Violations"
    fields = ["DurationComply"]
    outlst =[]
    percentlst = []
    with arcpy.da.SearchCursor(mainTable, fields) as Scur:
        for row in Scur:
            if row[0] != None:
                outlst.append(row[0])
            else:
                pass

    arcpy.management.AddField(mainTable, "num_status", "TEXT")
    stdv = numpy.std(outlst)
    avgOutlier= sum(outlst) / len(outlst)
    sdtvOutlier = avgOutlier + stdv * 3
    fields = ["DurationComply", "num_status"]
    with arcpy.da.UpdateCursor(mainTable, fields) as Scur:
        for row in Scur:
            if row[0] == None:
                pass
            elif row[0] >= sdtvOutlier:
                row[1] = "Outlier"
                Scur.updateRow(row)
            else:
                percentlst.append(row[0])

    avg = sum(percentlst) / len(percentlst)
    numPercentile = numpy.percentile(percentlst, 90)
    print(avg, numPercentile)
    statusField = ["num_status", "DurationComply"]
    with arcpy.da.UpdateCursor(mainTable, statusField) as Ucur:
        for row in Ucur:
            if row[1] == None:
                continue
            if row[1] >= sdtvOutlier or row[1] == None:
                pass
            if row[1] > numPercentile and row[1] < sdtvOutlier:
                row[0] = "Not within 90th Percentile"
                Ucur.updateRow(row)
            if row[1] <= numPercentile:
                row[0] = "Within 90th Percentile"
                Ucur.updateRow(row)

def Employee():
    employeeDict = {}
    dashLayer = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Dashboard_Violations"
    arcpy.management.AddField(dashLayer, "EmployeeName", "TEXT")
    fields = ["UserID", "EmployeeName"]
    with arcpy.da.UpdateCursor (dashLayer, fields) as Ucur:
        for row in Ucur:
            for key, value in employeeDict.items():
                if row[0] == key:
                    row[1] = value
                    Ucur.updateRow(row)

def AddressField():
    caseMain = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Dashboard_Case"
    caseMainFields = ["CASE_NO", "SITE_ADDR"]
    addrDict = {}
    with arcpy.da.SearchCursor(caseMain, caseMainFields) as Scur:
        for row in Scur:
            addrDict[row[0]] = row[1]
    print("a")

    caseViolations = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Dashboard_Violations"
    arcpy.management.AddField(caseViolations, "SITE_ADDR", "TEXT")
    caseViolationsFields =  ["CASE_NO", "SITE_ADDR"]
    with arcpy.da.UpdateCursor(caseViolations, caseViolationsFields) as Ucur:
        for row in Ucur:
            for key,value in addrDict.items():
                if key == row[0]:
                    row[1] = value
                    Ucur.updateRow(row)
    print("b")

    caseActions = "\\\\GISAPP\\Workspace\\GIS Staff Workspace\\cschultz\\CodeEnforcement.gdb\\Dashboard_Actions"
    arcpy.management.AddField(caseActions, "SITE_ADDR", "TEXT")
    caseActionsFields =  ["CASE_NO", "SITE_ADDR"]
    with arcpy.da.UpdateCursor(caseActions, caseActionsFields) as Ucur:
        for row in Ucur:
            for key,value in addrDict.items():
                if key == row[0]:
                    row[1] = value
                    Ucur.updateRow(row)

def Geocode():
    layerName = ["Case"]
    for i in layerName:

        if arcpy.Exists("\\\\gisapp\\workspace\\gis staff workspace\\cschultz\\CodeEnforcement.gdb\\Analytics_{}".format(i)) == True:
            arcpy.management.Delete("\\\\gisapp\\workspace\\gis staff workspace\\cschultz\\CodeEnforcement.gdb\\Analytics_{}".format(i))
        arcpy.geocoding.GeocodeAddresses("\\\\gisapp\\workspace\\gis staff workspace\\cschultz\\CodeEnforcement.gdb\\Dashboard_{}".format(i), "\\\\gisapp\\Workspace\\MyArcGIS\\Geocoders\\AM_Composite.loc", "'Single Line Input' SITE_ADDR VISIBLE NONE", "\\\\gisapp\\workspace\\gis staff workspace\\cschultz\\CodeEnforcement.gdb\\Analytics_{}".format(i), "STATIC", None, '', None, "LOCATION_ONLY")
        print(i)

def Update():

    prjPath = "\\\\GISAPP\\Workspace\\Horizon\\ArcGISPro_Projects\\FeatureServices\\CodeEnforcement\\CodeEnforcement.aprx"

    dict = {
            "License_Main": ["License_Main","City Boundaries, City Limits, ETJ, General, Administrative Boundaries, Polygon", "This layer contains the City Limits and Extra Territorial Jurisdiction (ETJ) for the City of Pearland.",0],
            "Permit_Reviews": ["Permit_Reviews","City Boundaries, City Limits, ETJ, General, Administrative Boundaries, Polygon", "This layer contains the City Limits and Extra Territorial Jurisdiction (ETJ) for the City of Pearland.",1],
            "License_Reviews": ["License_Reviews","City Boundaries, City Limits, ETJ, General, Administrative Boundaries, Polygon", "This layer contains the City Limits and Extra Territorial Jurisdiction (ETJ) for the City of Pearland.",2],
            "Inspections": ["Inspections","City Boundaries, City Limits, ETJ, General, Administrative Boundaries, Polygon", "This layer contains the City Limits and Extra Territorial Jurisdiction (ETJ) for the City of Pearland.",3],
            "Dashboard_Case": ["Dashboard_Case","City Boundaries, City Limits, ETJ, General, Administrative Boundaries, Polygon", "This layer contains the City Limits and Extra Territorial Jurisdiction (ETJ) for the City of Pearland.",4],
            "Dashboard_Actions": ["Dashboard_Actions","City Boundaries, City Limits, ETJ, General, Administrative Boundaries, Polygon", "This layer contains the City Limits and Extra Territorial Jurisdiction (ETJ) for the City of Pearland.",5],
            "Dashboard_Violations": ["Dashboard_Violations","City Boundaries, City Limits, ETJ, General, Administrative Boundaries, Polygon", "This layer contains the City Limits and Extra Territorial Jurisdiction (ETJ) for the City of Pearland.",6]
            }
    n=0

    for key, value in dict.items():
        sd_fs_name = "{}".format(key)
        portal = "https://pearland.maps.arcgis.com"
        user = ""
        password = ""

        shrOrg = True
        shrEveryone = True

        relPath = "\\\\GISAPP\\Workspace\\Horizon\\Scripts"
        sddraft = os.path.join(relPath, "WebUpdate.sddraft")
        sd = os.path.join(relPath, "WebUpdate.sd")

        arcpy.env.overwriteOutput = True
        prj = arcpy.mp.ArcGISProject(prjPath)
        m = prj.listMaps('Map')[0]
        mp = m.listTables()[value[3]]
        arcpy.mp.CreateWebLayerSDDraft(mp, sddraft, sd_fs_name, 'MY_HOSTED_SERVICES', 'FEATURE_ACCESS','CodeEnforcement', True, True, False, True)
        arcpy.StageService_server(sddraft, sd)

        gis = arcgis.GIS(portal, user, password)
        searchItem = gis.content.search(query="title:"+ value[0] + " AND owner: " + user, item_type="Service Definition", sort_field='title', sort_order='asc')
        print(searchItem)
        for item in searchItem:
            print(item["title"])
            if item["title"] == key:
                sdItem = item
        sdItem.update(data=sd)
        sdItem.update(item_properties={'tags': '{}'.format(value[1]), 'snippet': "This Table is updated automatically through a Python Script.",
                                        'description': '{}'.format(value[2])})

        fs = sdItem.publish(overwrite=True)
        fs.share(org=shrOrg, everyone=shrEveryone)
        print(sd_fs_name, "done")

main()
print(".")


import random
import discord

from utils import http
from discord.ext import commands

class Animals (commands.Cog):

    def __init__ (self, bot):

        self.bot            = bot
        self.unsplash_token = self.bot.unsplash_token

    @commands.command(name = 'cat', aliases = ['meow', 'randomcat'])
    async def get_random_cat (self, ctx):

        """Returns a random cat image."""

        async with ctx.channel.typing():

            try:
                response = await http.get('http://aws.random.cat/meow', res_method = 'json')

                e = discord.Embed()
                e.set_image(url = response['file'])
                e.add_field(name = 'Link', value = response['file'])

                msg = await ctx.send(embed = e)
                await msg.add_reaction('🐾')

            except:
                await ctx.send("There are no cats around right now. 🐾")

    @commands.command(name = 'dog', aliases = ['woof', 'randomdog'])
    async def get_random_dog (self, ctx):

        """Returns a random dog image."""

        async with ctx.channel.typing():

            try:
                response = await http.get('http://random.dog/woof', res_method = 'text')

                e = discord.Embed()
                e.set_image(url = f'https://random.dog/{response}')
                e.add_field(name = 'Link', value = f'https://random.dog/{response}')

                msg = await ctx.send(embed = e)
                await msg.add_reaction('🐶')

            except:
                await ctx.send("There are no dogs around right now. 🐶")

    @commands.command(name = 'doge')
    async def get_random_doge (self, ctx):

        """Returns a random doge image."""

        async with ctx.channel.typing():

            try:
                response = await http.get('https://shibe.online/api/shibes?count=1&urls=true&httpsUrls=true', res_method = 'json')

                e = discord.Embed()
                e.set_image(url = response[0])
                e.add_field(name = 'Link', value = response[0])

                msg = await ctx.send(embed = e)
                await msg.add_reaction('🐶')

            except:
                await ctx.send("There are no dogs around right now. 🐶")

    @commands.command(name = 'fox', aliases = ['fox-noises', 'randomfox'])
    async def get_random_fox (self, ctx):

        """Returns a random fox image."""

        async with ctx.channel.typing():

            try:
                response = await http.get('http://randomfox.ca/floof', res_method = 'json')

                e = discord.Embed()
                e.set_image(url = response['image'])
                e.add_field(name = 'Link', value = response['image'])

                msg = await ctx.send(embed = e)
                await msg.add_reaction('🦊')

            except:
                await ctx.send("There are no foxes around right now. 🦊")

    @commands.command(name = 'bunny', aliases = ['rabbit', 'chungus'])
    async def get_random_bunny (self, ctx):

        """Returns a random bunny image."""

        async with ctx.channel.typing():

            try:
                r = random.randint(0,10)
                t = self.unsplash_token
                url = f'https://api.unsplash.com/search/photos?query=bunny&page={r}&per_page=30&client_id={t}'

                response = await http.get(url, res_method = 'json')
                entry_number = random.randint(0,29)

                e = discord.Embed()
                e.set_image(url = response['results'][entry_number]['urls']['regular'])
                e.set_footer(text = 'Photographer: ' + response['results'][entry_number]['user']['name'])
                e.add_field(name = 'Link', value = response['results'][entry_number]['user']['links']['html'] + '?utm_source=unseen;ninja_bot&utm_medium=referral')

                msg = await ctx.send(embed = e)
                await msg.add_reaction('🐰')

            except:
                await ctx.send("There are no bunnies around right now. 🐰")

    @commands.command(name = 'pig', aliases = ['waddles'])
    async def get_random_pig (self, ctx):

        """Returns a random pig image."""

        async with ctx.channel.typing():

            try:
                r = random.randint(0,10)
                t = self.unsplash_token
                url = f'https://api.unsplash.com/search/photos?query=pig&page={r}&per_page=30&client_id={t}'

                response = await http.get(url, res_method = 'json')
                entry_number = random.randint(0,29)

                e = discord.Embed()
                e.set_image(url = response['results'][entry_number]['urls']['regular'])
                e.set_footer(text = 'Photographer: ' + response['results'][entry_number]['user']['name'])
                e.add_field(name = 'Link', value = response['results'][entry_number]['user']['links']['html'] + '?utm_source=unseen;ninja_bot&utm_medium=referral')

                msg = await ctx.send(embed = e)
                await msg.add_reaction('🐷')

            except:
                await ctx.send("There are no pigs around right now. 🐷")

    @commands.command(name = 'duck', aliases = ['quack'])
    async def get_random_duck (self, ctx):

        """Returns a random duck image."""

        async with ctx.channel.typing():

            try:
                response = await http.get('https://random-d.uk/api/quack', res_method = 'json')

                e = discord.Embed()
                e.set_image(url = response['url'])
                e.add_field(name = 'Link', value = response['url'])
                e.set_footer(text = response['message'])

                msg = await ctx.send(embed = e)
                await msg.add_reaction('🦆')

            except:
                await ctx.send("There are no ducks around right now. 🦆")

    @commands.command(name = 'lizard')
    async def get_random_lizard (self, ctx):

        """Returns a random lizard image."""

        async with ctx.channel.typing():

            try:
                response = await http.get('https://nekos.life/api/v2/img/lizard', res_method = 'json')

                e = discord.Embed()
                e.set_image(url = response['url'])
                e.add_field(name = 'Link', value = response['url'])

                msg = await ctx.send(embed = e)
                await msg.add_reaction('🦎')

            except:
                await ctx.send("There are no lizards around right now. 🦎")

    @commands.command(name = 'owl', aliases = ['who'])
    async def get_random_owl (self, ctx):

        """Returns a random owl image."""

        async with ctx.channel.typing():

            try:
                response = await http.get('https://pics.floofybot.moe/owl', res_method = 'json')

                e = discord.Embed()
                e.set_image(url = response['image'])
                e.add_field(name = 'Link', value = response['image'])

                msg = await ctx.send(embed = e)
                await msg.add_reaction('🦉')

            except:
                await ctx.send("There are no owls around right now. 🦉")

    @commands.command(name = 'panda')
    async def get_random_panda (self, ctx):

        """Returns a random panda image."""

        async with ctx.channel.typing():

            try:
                response = await http.get('https://some-random-api.ml/img/panda', res_method = 'json')

                e = discord.Embed()
                e.set_image(url = response['link'])
                e.add_field(name = 'Link', value = response['link'])

                msg = await ctx.send(embed = e)
                await msg.add_reaction('🐼')

            except:
                await ctx.send("There are no pandas around right now. 🐼")

    @commands.command(name = 'redpanda')
    async def get_random_redpanda (self, ctx):

        """Returns a random red panda image."""

        async with ctx.channel.typing():

            try:
                response = await http.get('https://some-random-api.ml/img/red_panda', res_method = 'json')

                e = discord.Embed()
                e.set_image(url = response['link'])
                e.add_field(name = 'Link', value = response['link'])

                await ctx.send(embed = e)

            except:
                await ctx.send("There are no red pandas around right now.")

    @commands.command(name = 'koala')
    async def get_random_koala (self, ctx):

        """Returns a random koala image."""

        async with ctx.channel.typing():

            try:
                response = await http.get('https://some-random-api.ml/img/koala', res_method = 'json')

                e = discord.Embed()
                e.set_image(url = response['link'])
                e.add_field(name = 'Link', value = response['link'])

                msg = await ctx.send(embed = e)
                await msg.add_reaction('🐨')

            except:
                await ctx.send("There are no koalas around right now. 🐨")

    @commands.command(name = 'birb', aliases = ['bird'])
    async def get_random_birb (self, ctx):

        """Returns a random birb image."""

        async with ctx.channel.typing():

            try:
                response = await http.get('https://some-random-api.ml/img/birb', res_method = 'json')

                e = discord.Embed()
                e.set_image(url = response['link'])
                e.add_field(name = 'Link', value = response['link'])

                msg = await ctx.send(embed = e)
                await msg.add_reaction('🐦')

            except:
                await ctx.send("There are no birbs around right now. 🐦")

def setup (bot):
    bot.add_cog(Animals(bot))


from modules.tweet_scraper import *

def remove_non_ascii(text):
    return ''.join(i for i in text if ord(i)<128)

def get_tweet_csv_data_into_text_dict(start_date, end_date, search_term):
    # Creating dict to insert tweet data to
    tweets_dict = {}
    months = calculate_number_of_months(start_date, end_date, '%m-%d-%Y')
    final_date = end_date
    for month_count in range(0, months + 1):
        if not months == month_count:
            end_date = calculate_end_date(start_date, '%m-%d-%Y')
        else:
            end_date = final_date
        file_name = os.path.join(tweet_path, str(get_file_name_string(search_term, end_date)))
        df = pd.read_csv(file_name, usecols=['Text'])
        df['Text'] = df['Text'].apply(remove_non_ascii)
        tweets_dict[subtract_date_by_days(end_date, 1, '%m-%d-%Y')] = df['Text'].tolist()
        start_date = add_date_by_days(end_date, 1, '%m-%d-%Y')
    return tweets_dict


#!/usr/bin/python
# encoding: utf8
import os
import sys
import urllib2
import socket
import time
import datetime as dt
import xml.etree.ElementTree as Et
import multiprocessing as mp
import idna
import urlparse
import logging
from argparse import ArgumentParser

cliparse = ArgumentParser()
cliparse.add_argument('-l', '--log', default='/var/log/zapret_checker.log', help='Path and name of file log. '
                                                                                'Default: /var/log/zapret_checker.log')
cliparse.add_argument('-d', '--debug', default=0, type=int, help='Level of debug. Low 0 High 4. Default 0 is off')
cliparse.add_argument('-f', '--file', default='/noc/zapret/dump.xml', help='XML dump file of RKN.')
cliparse.add_argument('-r', '--result', default='/var/log/zapret_result.log', help='File with validation result.')
cliparse.add_argument('-p', '--proc', default=100, type=int, help='Count of processes per core. Default 100.')
cliargs = cliparse.parse_args()

LOG_LEVEL = {0: 'NOTSET',
             1: 'INFO',
             2: 'WARNING',
             3: 'ERROR',
             4: 'DEBUG'}

def run_once(file_of_pid):
    """Guardian to prevent the launch of multiple copies.
     Exits if one copy is already running.
    :param file_of_pid: Path for tmp file of pid
    """
    if os.path.exists(file_of_pid):
        fpid = open(file_of_pid)
        # Checking the existence of pid
        if os.path.exists('/proc/{}/status'.format(fpid.read())):
            sys.exit(0)
        fpid.close()

    # Creating file with pid
    f = open(file_of_pid, 'w')
    f.write('{}'.format(os.getpid()))
    f.close()

def getter(domain):
    """Getting status code for domain or URL
    :param domain: domain name or URL as str
    :return: returned True if status code equals 200 OK
    """
    if not domain.startswith('http://') and not domain.startswith('https://'):
        domain = 'http://' + domain
    normal_code = [403, 404, 451]
    # Errors that we expect
    error_list = [socket.EAI_NONAME, socket.errno.ECONNRESET, socket.errno.ECONNREFUSED,
                  socket.errno.EHOSTUNREACH, socket.errno.ENETUNREACH, socket.EAI_AGAIN,
                  socket.EAI_NODATA, socket.SSL_ERROR_SSL, socket.errno.EADDRNOTAVAIL,
                  socket.error.errno]
    # Make a request and get the response code
    try:
        # Making a query
        request = urllib2.Request(domain)
        request.add_header('User-agent', 'Chrome/65.0.3325.181 (Linux RKN)')

        response = urllib2.urlopen(request, timeout=2)
        if response.code == 200:
            response.close()
            log.warning('URL avail: {}'.format(domain))
            return True

    except urllib2.HTTPError as e:
        if e.code not in normal_code:
            log.debug('urllib2.HTTPError: {}'.format(e))

    except urllib2.URLError as e:
        if e.reason[0] == 'timed out':
            # This exception can be if you exceed conntrack
            # Remember this, and if you need to change the conntrack restriction
            pass
        elif e.reason[0] not in error_list:
            log.debug('urllib2.URLError: {}'.format(e))

    except socket.error as e:
        if e != 'timed out':
            log.debug('SocketErr: {}'.format(e))

    except IOError as e:
        log.debug('IOerror: {}'.format(e))

    except Exception as e:
        log.debug('Unknown exception: {}'.format(e))

def _queue_manager(input_queue, output_queue):
    """Receives the URL from the queue, starts the check, and places the result in the output queue
    :param input_queue: Queue with URLs
    :param output_queue: Queue for write passes
    """
    try:
        while True:
            domain = input_queue.get()
            if domain:
                output_queue.put(getter(domain))
    except KeyboardInterrupt:
        SystemExit(1)

def _counter(output_queue, avail):
    """Pass counter.
    :param output_queue:
    :param avail:
    """
    try:
        while True:
            if output_queue.get():
                avail.value += 1
    except KeyboardInterrupt:
        SystemExit(1)

def starter(domain_list):
    """Create queue subprocesses and add URLs to the queue
    :param domain_list: prepared list of URLs
    :return: dictionary where:
     'len' length of domain_list
     'avail' count of available URLs
     'percent' count of available URLs in percentage
    """
    # Shared variable for counting passes
    avail = mp.Value('i', 0)
    # Set the number of processes multiplied by the number of cores
    number_of_processes = mp.cpu_count()*cliargs.proc
    log.debug('{} subprocesses will be created'.format(number_of_processes))
    # Create queues
    input_queue = mp.Queue()
    output_queue = mp.Queue()
    # Process for the pass count
    mp.Process(target=_counter, args=(output_queue, avail)).start()
    try:
        # Run worker processes
        for i in range(number_of_processes):
            mp.Process(target=_queue_manager, args=(input_queue, output_queue)).start()
        log.debug('Subprocesses created')
        for dome in domain_list:
            input_queue.put(dome)
        # Wait until all queues are empty
        while not input_queue.empty():
            time.sleep(0.01)
    except KeyboardInterrupt:
        log.debug('User interrupted processes')
        print('\n\n\033[93mPlease wait. Work is terminating.\033[0m\n\n')

        output_queue.close()
        output_queue.join_thread()
        input_queue.close()
        input_queue.join_thread()

        print('\033[92mThe queue is cleared.\033[0m')

        for child in mp.active_children():
            child.terminate()

        print('\033[92mWork completed.\033[0m')
        sys.exit(0)

    log.debug('Clearing queues')
    output_queue.close()
    output_queue.join_thread()
    input_queue.close()
    input_queue.join_thread()

    # Terminating all children
    log.debug('The child processes are terminated')
    for child in mp.active_children():
        child.terminate()
    # We are waiting for all the processes to finish work.
    for child in mp.active_children():
        child.join()
    log.debug('All children stopped work')

    return dict(len=len(domain_list), avail=avail.value,
                percent=float(avail.value) / float(len(domain_list)) * 100 if avail.value > 0 else 0)

def parse_dump(dump=''):
    """Parsing the XML file and getting URLs and DOMAINs
    :param dump: https://vigruzki.rkn.gov.ru/docs/description_for_operators_actual.pdf
    :return: tuple of prepared URLs and DOMAINs
    """
    dmp_xml = Et.parse(dump).getroot()
    urls = []
    for cont in dmp_xml.findall('content'):
        blocktype = cont.get('blockType')

        if blocktype == 'url' or not blocktype:
            url = cont.find('url')

            if hasattr(url, 'text'):
                parsed_url = urlparse.urlparse(url.text)
                orig_scheme = parsed_url.scheme
                orig_domain = parsed_url.netloc

                try:
                    domain = idna.encode(orig_domain)
                except idna.core.IDNAError:
                    domain = orig_domain.encode('idna')

                path = url.text.split(u'{}://{}'.format(orig_scheme, orig_domain))[1].encode('utf-8')
                urls.append('{}://{}{}'.format(orig_scheme, domain, path))
        elif blocktype == 'domain':
            bt_domain = cont.find('domain')

            if hasattr(bt_domain, 'text'):
                try:
                    bt_domain = idna.encode(bt_domain.text)
                except idna.core.IDNAError:
                    bt_domain = bt_domain.text.encode('idna')

                urls.append(bt_domain)
    log.debug('Number of records to check: {}'.format(len(urls)))
    return tuple(urls)

if __name__ == '__main__':
    # Finish work if a copy of the script is started
    run_once('/tmp/zapret-checker.pid')

    log = logging.getLogger('zapret-checker')
    # File for logging
    hdlr = logging.FileHandler(cliargs.log)
    # Log format
    formatter = logging.Formatter('%(asctime)s %(process)d/%(threadName)s %(levelname)s : %(message)s')
    hdlr.setFormatter(formatter)
    log.addHandler(hdlr)
    # Set the Logging Level
    if cliargs.debug not in range(5):
        log.setLevel(logging.DEBUG)
    else:
        log.setLevel(LOG_LEVEL.get(cliargs.debug))

    log.debug('Work is started.')
    start = time.time()

    if os.path.exists(cliargs.file):
        domains = parse_dump(cliargs.file)
        if len(domains) == 0:
            log.error('The domain list is empty. Work is stopped.')
            sys.exit(1)
    else:
        log.error('Dump file does not exist')
        sys.exit(1)

    result = starter(domains)

    try:
        with open(cliargs.result, 'w') as res:
            res.write('size={}\navail_count={}\navail_percent={}\n'.format(
                result.get('len'), result.get('avail'), result.get('percent')))
    except IOError:
        log.error('Can not write to {}'.format(cliargs.result))

    log.debug('Work time: {}'.format(dt.timedelta(seconds=(time.time() - start))))


# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import sys
import os

def postprocess(src_path,save_path):
    in_files = os.listdir(src_path)
    for idx, file in enumerate(in_files):
      idx = idx + 1
      print(file, "===", idx)
      data = np.fromfile(src_path+'/'+file,np.float32).reshape(3,880,880)
      data=np.transpose(data, (1, 2, 0))
      fig=plt.gcf()
      plt.figure("visualization")
      plt.imshow(data)
      plt.gca().xaxis.set_major_locator(plt.NullLocator())
      plt.gca().yaxis.set_major_locator(plt.NullLocator())
      img_path=save_path+'/'+file.split('.')[0] + ".png"
      plt.savefig(img_path, bbox_inches='tight',dpi=300,pad_inches=0.0)
      plt.show()

if __name__ == '__main__':
    src_path = sys.argv[1]
    save_path = sys.argv[2]
    if not os.path.isdir(src_path):
        os.makedirs(os.path.realpath(src_path))
    if not os.path.isdir(save_path):
        os.makedirs(os.path.realpath(save_path))
    postprocess(src_path,save_path)


"""
Newton-Raphson for linear system of equations.

Taylor series approximation to n = 1

f(x)  = \sum_{n=0}^1 f^{n}(a)/n! * (x - a)^n
      = f(a) + f'(a)*(x-a)
let a = x_i
let y = f(x) //goal solution

y = f(x_i) + f'(x_i)*(x - x_i)

x = (y - f(x_i)) *(f'(x_i))^{-1} + x_i

x = x_i + (y - f(x_i)) *(f'(x_i))^{-1}
"""
f = input('enter f(x): ')
fp = input("enter f'(x): ")
func = lambda x: eval(f)
fprime = lambda x: eval(fp)

x_i = float(input('enter initial guess for x: '))
y = float(input('enter goal solution: '))
lim = float(input('enter accuracy metric to terminate function: '))

x = 0

while True:
    x = x_i + (y - func(x_i))*1/(fprime(x_i))

    if abs(x_i - x) <= lim:
        break
    #update x_i
    x_i = x
    print('x: ',x)


"""
Created by Jan Schiffeler on 03.12.20
jan.schiffeler[at]gmail.com

Changed by

Selection of functions for lookback options.
Also utility functions

Python 3.7
Library version:
numpy 1.19.4
matplotlib 3.3.3

"""

import numpy as np

def generate_s(N: int, S_0: float, e_u: float, e_d: float) -> np.ndarray:
    """
    Generate a matrix with all up and down paths

    :param N: time steps
    :param S_0: start value
    :param e_u: exp of up
    :param e_d: exp of down
    :return: matrix containing the paths
    """
    N += 1
    S_layer = np.zeros([N, N])
    S_layer[0, 0] = S_0
    for i in range(1, N):
        S_layer[:, i] = np.roll(S_layer[:, i - 1], 1) * e_d
        S_layer[0, i] = S_layer[0, i - 1] * e_u

    return S_layer

def generate_european_put_price(params: np.ndarray, N: int, K: float) -> np.ndarray:
    """
    Calculate the inital values for a European put for all sets in the parameter set

    :param params:  containing a set of different parameter values ->
               |    p
               |    alpha
               V    sigma
                    r
                    T
                    S_0
    :param N: number of time steps
    :param K: strike price
    :return: initial values for this param set
    """
    pi = np.zeros(params.shape[1])
    for i in range(params.shape[1]):
        # retrieve parameters from matrix
        p, alpha, sigma, r, T, S_0 = params[:, i]
        e_d, e_u, h, q_d, q_u = unpack_parameters(N, T, alpha, p, r, sigma)

        assert q_u > 0 and q_d > 0, f"Market not arbitrage free! {q_u}; {q_d}"

        # get the payoff
        stock_price = generate_s(N=N, S_0=S_0, e_u=e_u, e_d=e_d)
        option_price = np.zeros_like(stock_price)
        option_price[:, -1] = np.maximum(K - stock_price[:, -1], 0)

        q_u = (np.exp(r * h) - e_d) / (e_u - e_d)
        q_d = (e_u - np.exp(r * h)) / (e_u - e_d)
        e_r = np.exp(r)

        for j in range(N - 1, -1, -1):
            option_price[:, j] = 1 / e_r * (q_u * option_price[:, j + 1] +
                                            q_d * np.pad(option_price[:, j + 1], (0, 1), 'constant')[1:])
        option_price = np.triu(option_price)

        # get the price at t=0 for each M samples
        pi[i] = option_price[0, 0]

    return pi

def generate_paths(M: int, N: int, S_0: float, e_u: float, e_d: float) -> [np.ndarray]:
    """
    Generate a sample of M possible paths

    :param M: number of samples
    :param N: number of time steps
    :param S_0: start value
    :param e_u: exp of up
    :param e_d: exp of down
    :return: 1. matrix containing the possible paths of the stock price
             2. matrix containing the ups (1) and downs (0) [this is used to count N_u and N_d]
    """
    # generate a matrix of 1 and 0, where 1 = up and 0 = down of the stockprice
    instructions_pre = np.random.randint(0, 2, (M, N))
    # create a matrix with e_u and e_d entries based on the previous
    instructions = instructions_pre * e_u + (1 - instructions_pre) * e_d

    # create a matrix containing the stock price process for all M paths
    stock_price = np.zeros((M, N))
    stock_price[:, 0] = S_0
    for i in range(1, N):
        column = instructions[:, i]
        stock_price[:, i] = stock_price[:, i-1] * column

    return stock_price, instructions_pre

def calculate_payoff(stock: np.ndarray) -> np.ndarray:
    """
    Calculate the payoff of a set of paths

    :param stock: stock path matrix
    :return: return price vector
    """
    maximum = np.max(stock, axis=1)
    strike_price = stock[:, -1]
    return maximum - strike_price

def calculate_initial_price(params: np.ndarray, M: int, n: int, N: int=100) -> np.ndarray:
    """
    Calculate the initial for a given parameter set and for n repetitions

    :param params:  containing a set of different parameter values ->
                   |    p
                   |    alpha
                   V    sigma
                        r
                        T
                        S_0
    :param M: number of samples
    :param N: number of time steps
    :param n: number of repetitions of each experiment [test of parameter]
    :return: averaged initial price vector
    """
    pi_0 = np.zeros(params.shape[1])
    for j in range(n):
        pi_0 += get_initial_price(params=params, M=M, n=n, N=N, j=j)

    return pi_0/n

def get_initial_price(params: np.ndarray, M: int, n: int, N: int, j: int) -> np.ndarray:
    """
    Calculate the initial for a given parameter set.

    :param params:  containing a set of different parameter values ->
                   |    p
                   |    alpha
                   V    sigma
                        r
                        T
                        S_0
    :param M: number of samples
    :param N: number of time steps
    :param n: number of repetitions of each experiment [test of parameter]
    :param j: call number [only for printout]
    :return: initial price vector
    """
    print(f"Sampling Repetition {j + 1}/{n}")
    pi = np.zeros(params.shape[1])
    for i in range(params.shape[1]):
        # retrieve parameters from matrix
        p, alpha, sigma, r, T, S_0 = params[:, i]
        e_d, e_u, h, q_d, q_u = unpack_parameters(N=N, T=T, alpha=alpha, p=p, r=r, sigma=sigma)

        assert q_u > 0 and q_d > 0, f"Market not arbitrage free! {q_u}; {q_d}"

        # get the payoff for M samples of paths
        stock_paths, instructions = generate_paths(M=M, S_0=S_0, N=N, e_u=e_u, e_d=e_d)
        payoff_paths = calculate_payoff(stock=stock_paths)

        # get the price at t=0 for each M samples
        N_u = np.sum(instructions, axis=1)
        N_d = N - N_u
        pi[i] = (2 ** N) / M * np.exp(-r * h * N) * np.sum(q_u ** N_u * q_d ** N_d * payoff_paths)

    return pi

def unpack_parameters(N: int, T: float, alpha: float, p: float, r: float, sigma: float) -> [float]:
    """
    Calculate all needed parameters based on the given ones.

    :param N: number of time steps
    :param T: maturity time in years
    :param alpha: mean of log return
    :param p: probabilty to increase stock price
    :param r: risk free rate
    :param sigma: volatility
    :return: exp of up step, exp of down step,
             length time step, risk neutral probability up, risk neutral probability down
    """
    h = T / N
    e_u = np.exp(alpha * h + sigma * np.sqrt(h) * np.sqrt((1 - p) / p))
    e_d = np.exp(alpha * h - sigma * np.sqrt(h) * np.sqrt(p / (1 - p)))
    q_u = (np.exp(r * h) - e_d) / (e_u - e_d)
    q_d = (e_u - np.exp(r * h)) / (e_u - e_d)
    return e_d, e_u, h, q_d, q_u

def generate_text(axis, message: str):
    """
    Make everything in the subplot invisible and print text

    :param axis: subplot handel
    :param message: text to print out
    :return: subplot handel
    """
    axis.text(0, 0.5, message)
    axis.axes.xaxis.set_visible(False)
    axis.axes.yaxis.set_visible(False)
    axis.spines["top"].set_visible(False)
    axis.spines["right"].set_visible(False)
    axis.spines["bottom"].set_visible(False)
    axis.spines["left"].set_visible(False)
    return axis


from flask import Flask, request
from utils import get_requirements, get_users, get_astronauts, get_mean

app = Flask(__name__)

@app.route('/requirements/')
def requirements():
    """
    Task #1
    open requirements.txt file and return its contents
    """
    result = get_requirements()
    return result

@app.route('/generate-users/')
def generate_users():
    """
    Task #2
    return 100 randomly generated users (mail + name) 'Dmytro aasdasda@mail.com'
    + parameter that controls the number of users
    """

    count = request.args.get('count') or '100'

    if count.isdigit():
        count = int(count)
        max_count = 200

        if count > max_count:
            return f'Max count should be less that {max_count}.'
    else:
        return f'Invalid parameter count {count}. Integer is expected.'

    result = get_users(count)
    return result

@app.route('/space/')
def space():
    """
    Task #3
    return the number of astronauts at the moment (http://api.open-notify.org/astros.json)
    """

    result = get_astronauts()
    return result

@app.route('/mean/')
def mean():
    """
    Task #4
    Read hw.csv file and calculate average height, average weight in cm and kg respectively
    """
    result = get_mean()
    return result

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000, debug=True)