from django.shortcuts import render, HttpResponse, redirect
from main.models import *
from django.contrib import messages
from django.http import JsonResponse
from django.db.models import Q
import bcrypt
from main.decorators import loginauth

# Create your views here.

def friends(request):
    context= {
        'usuario': User.objects.get(id=request.session['user']),
        'usuarios': User.objects.all().exclude(id=request.session['user']),
        'amigos': User.objects.get(id=request.session['user']).friends.all()
    }
    return render(request, 'friends.html',context)

def profile(request, id):
    context= {
        'usuario': User.objects.get(id=request.session['user']),
        'profile': User.objects.get(id=id)
    }
    return render(request, 'profile.html', context)

def newfriend(request):
    userA= User.objects.get(id=request.session['user'])
    userB= User.objects.get(id=request.POST['id'])
    userA.friends.add(userB)
    userB.friends.add(userA)
    return redirect('/friends')

def delete(request):
    userA= User.objects.get(id=request.session['user'])
    userB= User.objects.get(id=request.POST['id'])
    userA.friends.remove(userB)
    return redirect('/friends')


from os import system, name
from time import sleep
import sys
import os.path
import json
import random
import requests

def clear():
    if name == 'nt':
        _ = system('cls')
    else:
        _ = system('clear')

def loadDict(dictFile):
    if not os.path.exists(dictFile):
        file = open(dictFile,"w")
        json.dump({},file)
        file.close()
    file = open(dictFile,"r")
    dictionary = json.load(file)
    file.close()
    return dictionary

def learn(dict,inp):
    tokens = inp.split(" ")
    for i in range(0,len(tokens)-1):
        currWord = tokens[i]
        nextWord = tokens[i+1]
        if currWord not in dict:
            dict[currWord] = {nextWord:1}
        else:
            # exist in dict
            allNextWords = dict[currWord]

            if nextWord not in allNextWords:
                #add new state
                dict[currWord][nextWord] = 1
            else:
                dict[currWord][nextWord] = dict[currWord][nextWord]+1
    return dict

def updateFile(fileName, dictionary):
    file = open(fileName,"w")
    json.dump(dictionary,file)
    file.close()
    print("--- OK - ZAPISANO ZAWARTOŚĆ SŁOWNIKA: "+fileName+" ---")

def getNextWord(lastWord,dictionary):
    if lastWord not in dictionary:
        #wez nowe
        newWord = randomWord(dictionary)
        return newWord
    else:
        candidates = dictionary[lastWord]
        candidatesNormalized = []

        for word in candidates:
            freq = candidates[word]
            for i in range(0,freq):
                candidatesNormalized.append(word)
        rnd = random.randint(0,len(candidatesNormalized)-1)
        return candidatesNormalized[rnd]

def randomWord(dict):
    randNum = random.randint(0,len(dict)-1)
    newWord = list(dict.keys())[randNum]
    return newWord


lines = open('input.txt').read().splitlines()

def rotate(direction, vector):
    """Takes a direction N or L and rotates a vector accordingly around origo"""
    # Swap and Negate
    # Draw the point (2, 1) on a coodrinate axis and rotate 4 times.
    # Note how x,y is always swaped. And depending on if you rotate left or right you negate one of the numbers.
    if direction == 'L':
        vector[0], vector[1] = vector[1], -vector[0]
    else:
        vector[0], vector[1] = -vector[1], vector[0]
    return vector

directions = {
    'N': (1, 0),
    'E': (0, 1),
    'S': (-1, 0),
    'W': (0, -1),
}

dirs = "NESW"
dir = 'E'
pos = [0, 0]
forward_vector = [1, 10]

for line in lines:
    c = line[0]
    num = int(line[1:])

    if c in 'NESW':
        vector = directions[c]
        forward_vector[0] += vector[0] * num
        forward_vector[1] += vector[1] * num
        # print(f"new forward_vector {forward_vector}")

    if c in 'LR':
        assert num in [0, 90, 180, 270]
        num = [0, 90, 180, 270].index(num)

        for i in range(num):
            rotate(c, forward_vector)
        # print(f"new forward_vector {forward_vector} rotated {c} {num} times")

    if c == 'F':
        # print(f"forward_vector: {forward_vector} {num} times")
        pos[0] += forward_vector[0] * num
        pos[1] += forward_vector[1] * num
    # print(pos)

print(f'final pos {pos}')
print(sum(abs(p) for p in pos))


from utils.normalize import denormalize
import torch
import librosa
from args import stft_kwargs, mel_kwargs

def hum_to_guitar(h_s, model, db_mm,stft_kwargs, mel_kwargs, return_mel=False):
    model.eval()
    with torch.no_grad():
        h_ = model(h_s).detach().cpu().squeeze().numpy()
    h_ = denormalize(h_, db_mm)
    h_ = librosa.db_to_power(h_)
    sig_h = librosa.feature.inverse.mel_to_audio(h_,
                                                 center=True,
                                                 n_fft=stft_kwargs['n_fft'],hop_length=stft_kwargs['hop_length'], win_length=stft_kwargs['win_length'],
                                                 fmax=mel_kwargs['fmax'], fmin=mel_kwargs['fmin']
                                                 )
    if return_mel:
        return sig_h, h_
    else:
        return sig_h


# coding=utf-8
"""Further reading form."""

from django import forms
from django.utils.translation import ugettext_lazy as _

from crispy_forms.helper import FormHelper
from crispy_forms.layout import (
    Layout,
    Fieldset,
    Field,
    Submit,
)
from tinymce.widgets import TinyMCE

from modeltranslation.forms import TranslationModelForm
from lesson.models.further_reading import FurtherReading

class FurtherReadingForm(TranslationModelForm):
    """Form for creating further reading item."""

    text = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))

    class Meta:
        model = FurtherReading
        fields = (
            'text',
        )

    def __init__(self, *args, **kwargs):
        self.worksheet = kwargs.pop('worksheet')
        self.helper = FormHelper()
        layout = Layout(
            Fieldset(
                _('Worksheet {}: further more item details').format(
                    self.worksheet),
                Field('text', css_class='form_control'),
                css_id='project-form'
            )
        )

        self.helper.layout = layout
        self.helper.html5_required = False

        super(FurtherReadingForm, self).__init__(*args, **kwargs)

        self.helper.add_input(Submit('submit', 'Submit'))

    def save(self, commit=True):
        instance = super(FurtherReadingForm, self).save(commit=False)
        instance.worksheet = self.worksheet
        instance.save()
        return instance


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 10 20:56:17 2018

This problem defines the longest Collatz-Chain

@author: sam
"""

n = 13

while n >= 0:
    if n%2 == 0:
        new = n/2
    else:
        new = 3*n - 1

print(n)


import cv2
import pyzed.camera as zcam
import pyzed.types as tp
import pyzed.core as core
import pyzed.defines as sl
import math
import numpy as np
import sys
import imutils

def main():
    i = 0
    image = core.PyMat()
    depth = core.PyMat()
    point_cloud = core.PyMat()

    print("Running...")
    init_params = zcam.PyInitParameters()
    init_params.depth_mode = sl.PyDEPTH_MODE.PyDEPTH_MODE_PERFORMANCE  # Use PERFORMANCE depth mode
    init_params.coordinate_units = sl.PyUNIT.PyUNIT_MILLIMETER  # Use milliliter units (for depth measurements)

    cam = zcam.PyZEDCamera()
    if not cam.is_opened():
        print("Opening ZED Camera...")
    status = cam.open(init_params)

    if status != tp.PyERROR_CODE.PySUCCESS:
        print(repr(status))
        exit()

    runtime = zcam.PyRuntimeParameters()
    runtime.sensing_mode = sl.PySENSING_MODE.PySENSING_MODE_STANDARD  # Use STANDARD sensing mode

    mat = core.PyMat()

    key = ''
    while key != 113:  # for 'q' key
        err = cam.grab(runtime)
        if err == tp.PyERROR_CODE.PySUCCESS:
            #cam.retrieve_image(mat, sl.PyVIEW.PyVIEW_LEFT)
           # cam.retrieve_image(mat, sl.PyVIEW.PyVIEW_DEPTH)
            #cam.retrieve_image(mat, sl.PyVIEW.PyVIEW_LEFT)

            cam.retrieve_image(mat, sl.PyVIEW.PyVIEW_NORMALS)
            cam.retrieve_measure(depth, sl.PyMEASURE.PyMEASURE_DEPTH)
            cam.retrieve_measure(point_cloud, sl.PyMEASURE.PyMEASURE_XYZRGBA)

            #euklinda distanca za mirit udaljenost objekta

            x = round(mat.get_width() / 2)
            y = round(mat.get_height() / 2)

            err, point_cloud_value = point_cloud.get_value(x, y)
            distance = math.sqrt(point_cloud_value[0] * point_cloud_value[0] +
                                 point_cloud_value[1] * point_cloud_value[1] +
                                 point_cloud_value[2] * point_cloud_value[2])

            if not np.isnan(distance) and not np.isinf(distance):
                distance = round(distance)
                print("Distance to Camera at ({0}, {1}): {2} mm\n".format(x, y, distance))
                # Increment the loop
                i = i + 1
            else:
                print("Can't estimate distance at this position, move the camera\n")
            sys.stdout.flush()
            #cam.retrieve_image(mat, sl.PyVIEW.PyVIEW_LEFT_UNRECTIFIED)

            cv2.imshow("ZED", mat.get_data())
            key = cv2.waitKey(5)
        else:
            key = cv2.waitKey(5)
    cv2.destroyAllWindows()

    cam.close()

main()


import smtplib
import os

def send_email(draws, info_dict):
    """sends email to everyone with their draw by getting their
    email from email_dict"""
    # creates SMTP session
    s = smtplib.SMTP('smtp.gmail.com', 587)

    # start TLS for security
    s.starttls()
    sender = "findmysecretsanta@gmail.com"

    # Authentication
    s.login(sender, os.environ['PASSWORD'])

    for sender in draws.keys():

        # message to be sent
        message = f"""\
Subject: Your secret santa draw.

Hi {sender},
You are {draws[sender]}'s secret santa. {draws[sender]}'s email is {info_dict[draws[sender]][0]}.
{draws[sender]}'s address is: {info_dict[draws[sender]][1]}
"""

        # sending the mail
        s.sendmail(sender, info_dict[sender][0], message)

    # terminating the session
    s.quit()

if __name__ == "__main__":
    draws = {
        'shaash': 'shashwat',
    }

    info_dict = {
        'shaash': ["findmysecretsanta@gmail.com", "851 california street, San francisco, 94108"],
        'shashwat': ["meshashwat007@gmail.com", "851 california street, San francisco, 94108"]
    }

    send_email(draws, info_dict)


from django.http import HttpResponse

from django.template import loader
from django.template.loader import get_template
# def index(request):
#     latest_question_list = ['a','b','c']
#     template = loader.get_template('product/index.html')
#     context = {
#         'latest_question_list': 'string',
#     }
#     return HttpResponse(template.render(context, request))

from my_backend.load_search import find_file
from django.shortcuts import render
from django.http import HttpResponseRedirect

from product.forms import NameForm
from product.forms import SelectForm
import os,sys
import config
# from tests.unit_tests import run_all_tests
INDEX = '/product/templates/product/index.html'
def name(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        form = NameForm(request.POST)
        result = str(form["your_product"].value())
        q_param = form["your_product"].value()
        resultt , search_r = find_file(q_param)
        # check whether it's valid:
        if form.is_valid():
            # print(form["your_product"].value())
            return render(request, "selected.html" , {'resultt':resultt,'search_results' : search_r})
    else:
        form = NameForm()

    return render(request, 'selected.html', {'form': form})

def results(request):
    return render(request, 'results.html')

def index(request):
    # TEMPLATE_PATH =os.path.join(os.getcwd(), INDEX)
    print("os.getcwd... ",os.getcwd())
    TEMPLATE_PATH = os.getcwd() + INDEX
    template = get_template(TEMPLATE_PATH)
    context = {
        'key': 'value',
    }
    return HttpResponse(template.render(context, request))

def selected(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        form = SelectForm(request.POST)
        resultt = (form["selected_choice"].value())
        print(form["selected_choice"].value())
        # check whether it's valid:
        if form.is_valid():
            print("debug_inside")
        else:
            print("debug !form is_valid")

        return render(request, "selected.html" , {'form' : form,'resultt':resultt})
# def test(request):
#     if request.method == 'GET':
#         unit_tests.run_all_tests()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2019/2/22 10:01
# @User    : zhunishengrikuaile
# @File    : Numbers.py
# @Email   : binary@shujian.org
# @MyBlog  : WWW.SHUJIAN.ORG
# @NetName : 書劍
# @Software: 百度识图Api封装
# Numbers
# 数字识别

import os
import base64
import requests
from bin.AccessToken.AccessToken import AccessToken
from config.config import LOCALHOST_PATH, URL_LIST_URL

ACCESS_TOKEN = AccessToken().getToken()['access_token']

NUMBERS_URL = URL_LIST_URL['NUMBERS'] + '?access_token={}'.format(ACCESS_TOKEN)

class NumbersSuper(object):
    pass

class Numbers(NumbersSuper):

    def __init__(self, image=None, recognize_granularity='small', detect_direction=True):
        self.HEADER = {
            'Content-Type': 'application/x-www-form-urlencoded',
        }
        self.IMAGE_CONFIG = {
            'recognize_granularity': recognize_granularity,
            'detect_direction': detect_direction
        }

        if image is not None:
            imagepath = os.path.exists(LOCALHOST_PATH['PATH'] + image)
            if imagepath == True:
                images = LOCALHOST_PATH['PATH'] + image
                with open(images, 'rb') as images:
                    self.IMAGE_CONFIG['image'] = base64.b64encode(images.read())

    def postNumbers(self):
        if self.IMAGE_CONFIG.get('image', None) == None:
            return 'image参数不能为空!'
        numbers = requests.post(url=NUMBERS_URL, headers=self.HEADER,
                                data=self.IMAGE_CONFIG)
        return numbers.json()


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

Problem: Sort an array of integers in ascending order.
Definition: Given an array of integers, sort the array
in ascending order using a divide and conquer approach.
The array is split in left and right recursively and
merged into a final ouput array by comparing the elements
in the left and right array and appending them to the output
based on increasing value.
Complexity: O(n log(n))
Usage:
    $ python3 -m doctest merge_sort.py
"""

def merge(L, R):
    result = []
    i , j = 0 , 0
    while i < len (L) and j < len (R):
        if L[i] <= R[j]:
            result.append(L[i])
            i+=1
        else:
            result.append(R[j])
            j+=1

    result += L[i:]
    result += R[j:]
    return result

def mergesort(A):
    if len(A) <= 1:
        return A

    middle = len(A) // 2
    L   = mergesort(A[:middle])
    R   = mergesort(A[middle:])

    return merge(L, R)

print(mergesort([100,2,33,5,3,14,3,33,2,4,7,8,9]))


total_bill = float(input("How much was your bill?\n> "))
service = input(("How was your service? (Good, Fair, Bad)\n> ".lower()))
people = int(input("How many people are splitting the bill?\n> "))

if service == "good":
    tip = total_bill * 0.20
    total = total_bill * 1.20
    split = total / people
elif service == "fair":
    tip = total_bill * 0.15
    total = total_bill * 1.15
    split = total / people
elif service == "bad":
    tip = total_bill * 0.10
    total = total_bill * 1.10
    split = total / people

print(f"Your tip amount is: {tip}.")
print(f"Your total bill is {total}.")
print(f"The amount per person is: {split}.")


import numpy as np

import gym
from gym import spaces

class NasEnvEmb(gym.Env):

    def __init__(self, space, evaluator, structure):

        self.space = space
        self.structure = structure
        self.evaluator = evaluator

        self.dim_ob_sp = len(structure.get_hash(0, 0))
        self.observation_space = spaces.Box(low=0, high=1,
            shape=(self.dim_ob_sp,), dtype=np.float32)
        self.action_space = spaces.Discrete(self.structure.max_num_ops)

        self._state = np.array([0 for i in range(self.dim_ob_sp)])
        self.action_buffer = []
        self.num_timesteps = self.structure.num_nodes

    def step(self, action, index, rank=None):

        self.action_buffer.append(action)

        if len(self.action_buffer) < self.num_timesteps:
            terminal = False
            reward = 0
            action_hash = self.structure.get_hash(len(self.action_buffer)-1, action)
            self._state = np.array([float(e) for e in action_hash])
            return self._state, reward, terminal, {}

        conv_action = np.array(self.action_buffer) / self.structure.max_num_ops

        terminal = True
        self.action_buffer = []
        self._state = np.array([0 for i in range(self.dim_ob_sp)])

        cfg = self.space.copy()
        cfg['arch_seq'] = list(conv_action)
        cfg['w'] = index
        if rank != None:
            cfg['rank'] = rank

        self.evaluator.add_eval(cfg)

        # ob, reward, terminal
        return self._state, None, terminal, {}

    def get_rewards_ready(self):
        return self.evaluator.get_finished_evals()

    def reset(self):
        self.__init__(self.space, self.evaluator, self.structure)
        return self._state


import json
from bvss import Vuln

tests = {'web': 'tests/web.json',
         'smart conract': 'tests/smart_contract.json',
         'blockchain': 'tests/blockchain.json',
         'hardware': 'tests/hardware.json'}
for key in tests:
    print('------------------------{}------------------------'.format(key))
    with open(tests[key]) as f:
        vulns = json.load(f)

    for vuln in vulns:
        item = Vuln(vuln)
        print("Desc: {}\nScore: {}\nBVSS: {}\n".format(item.desc, item.calc_score(), item.calc_bvss()))


import pandas as pd
from tensorflow import keras
from tensorflow.keras import utils
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import InputLayer, Conv2D, Flatten, Dense

def ecg_predict(x_test,y_test, smodel):
    score = smodel.evaluate(x_test,y_test,verbose=2)
    return score[0][1]*100

def useECG():

    df_train = pd.read_csv("data/mitbih_train.csv", header=None)
    df_test = pd.read_csv("data/mitbih_test.csv", header=None)
    df_test_x = pd.read_csv("data/test_data.csv")

    X_train = df_train.values[:, :-1]
    y_train = df_train.values[:, -1].astype(int)

    X_test  = df_test.values[:, :-1]
    y_test  = df_test.values[:, -1].astype(int)

    # testx,testy = df_test.values[:,-1]
    testx,testy = df_test_x.values[:,:-1],df_test_x.values[:,-1].astype(int)

    x_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1, 1)
    x_test = X_test.reshape(X_test.shape[0], X_test.shape[1],1, 1)
    testx = testx.reshape(testx.shape[0],testx.shape[1],1,1)

    y_train = utils.to_categorical(y_train)
    y_test  = utils.to_categorical(y_test)
    testy = utils.to_categorical(testy,num_classes=5)

    input_shape = x_train.shape[1:]
    num_classes= 5
    smodel = Sequential()
    smodel.add(InputLayer(input_shape=input_shape))
    smodel.add(Conv2D(16, kernel_size=5, activation='relu', padding='same'))
    smodel.add(Flatten())
    smodel.add(Dense(16))
    smodel.add(Dense(num_classes,activation='softmax'))
    smodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    batch_size = 100
    num_epochs = 10
    shistory = smodel.fit(x_train, y_train, epochs=num_epochs, batch_size=batch_size, validation_data=(testx,testy))
    ecg_predict(x_test,y_test, smodel)

# print(shistory.history["acc"])
# score = smodel.evaluate(x_test,y_test,verbose=2)
# print("Testing")
# smodel.save_model(smodel, 'ecg_cnn.hdf5')


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Torrez, Milton N.

import json
import requests
from bs4 import BeautifulSoup
import datetime
import time
import ast
from random import randint
from time import sleep

base_url = "https://www.ambito.com/contenidos/dolar.html"

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
}

def get_current_month_and_year():
    """Returns the current month and year in the format year-month."""
    now = datetime.datetime.now()
    month = now.month
    year = now.year
    return f"{year}-{month:02d}"

def get_current_date():
    """Returns the current date in the format dd/mm/yy."""
    today = datetime.date.today()
    return f"{today.day}/{today.month}/{today.year % 100}"

def get_current_time():
    """Returns the current time in the format HH:MM:SS."""
    now = datetime.datetime.now()
    return f"{now.hour:02d}:{now.minute:02d}:{now.second:02d}"

def pprint_data(d):
    """Prints nicely a dict."""
    data = json.dumps(d, indent=4, ensure_ascii=False)
    print(data)

def get_coti_data(d):
    c = {}
    c["name"] = d
    api_base_url = f"https://mercados.ambito.com//dolar/{d}/variacion"
    response = requests.get(api_base_url, headers=headers)
    c["cotization"] = response.json()
    return c

def process_data(d):
    data = {}
    #  DEBUG INFO
    data["debug_info"] = {}
    data["debug_info"]["running_date"] = get_current_date()
    data["debug_info"]["running_time"] = get_current_time()
    data["data"] = {}
    data["data"]["coti"] = []

    data_boxes = d.findAll("div", {"class": "indicador"})
    ref_names = []

    for i in data_boxes:
        for j in i["data-indice"].split("/"):
            if j != "dolar" and j != "":
                ref_names.append(j)

    for i in ref_names:
        data["data"]["coti"].append(get_coti_data(i))
        sleep(randint(1, 10))

    #  Append data to file.
    append_data_to_json_file(data, get_current_month_and_year())

def get_data():
    """Gets page content."""
    response = requests.get(base_url, headers=headers)
    return response.content

def append_data_to_json_file(new_data, filename):
    """Appends data to a JSON file, creating the file if it does not exist.

    Args:
    new_data: The data to append to the file.
    filename: The path to the JSON file.
    """

    try:
        # Open the file in reading and writing mode.
        with open(f"data/ambito/{filename}.json", "r+", encoding="utf-8") as f:
            # Load the existing data from the file.
            existing_data = json.load(f)

            # Append the new data to the existing data.
            existing_data.append(new_data)

            # Seek to the beginning of the file.
            f.seek(0)

            # Write the updated data to the file.
            json.dump(existing_data, f)
    except FileNotFoundError:
        # Create the file if it does not exist.
        with open(f"data/ambito/{filename}.json", "w") as f:
            json.dump([new_data], f)

def ambito_main():
    soup = BeautifulSoup(get_data(), "html.parser")
    process_data(soup)

if __name__ == "__main__":
    ambito_main()


'''
    app.py
    Zack Dong, Ali Ramazani, Muno Siyakurima

    A tiny Flask application that provides a website with an accompanying
    API (which is also tiny) to support that website.

'''
import sys
import flask
import api

########### Initializing Flask ###########
# Note that this stuff has to be up here at the top, because otherwise
# the @app.route lines would raise a "name not defined" exception.
app = flask.Flask(__name__, static_folder='static', template_folder='templates')
app.register_blueprint(api.api, url_prefix='/api')

########### The website routes ###########
@app.route('/')
def get_main_page():
    ''' This is the only route intended for human users '''
    return flask.render_template('index.html')

@app.route('/mockup1.html')
def get_mockup1():
    ''' This is the only route intended for human users '''
    return flask.render_template('mockups/mockup1.html')

@app.route('/mockup2.html')
def get_mockup2():
    ''' This is the only route intended for human users '''
    return flask.render_template('mockups/mockup2.html')

@app.route('/mockup3.html')
def get_mockup3():
    ''' This is the only route intended for human users '''
    return flask.render_template('mockups/mockup3.html')

@app.route('/mockup4.html')
def get_team_page():
    ''' This is the only route intended for human users '''
    return flask.render_template('mockups/mockup4.html')

########### Running the website server ###########
if __name__ == '__main__':
    if len(sys.argv) != 3:
        print('Usage: {0} host port'.format(sys.argv[0]), file=sys.stderr)
        exit()

    host = sys.argv[1]
    port = int(sys.argv[2])
    app.run(host=host, port=port, debug=True)


import json
try:
    from json import JSONDecodeError
except ImportError:
    JSONDecodeError = ValueError

from kubernetes.client.models import V1Container

from scan.fetchers.cli.cli_fetcher import CliFetcher
from scan.fetchers.kube.kube_access import KubeAccess
from base.utils.exceptions import CredentialsError, HostAddressError, SshError

class KubeFetchContainers(KubeAccess, CliFetcher):

    PROXY_ATTR = 'kube-proxy'
    SANDBOX_ID_ATTR = 'io.kubernetes.sandbox.id'
    ATTRIBUTES_TO_IGNORE = ['resources', 'liveness_probe', 'readiness_probe', 'security_context']

    def get(self, parent_id) -> list:
        pod_id = parent_id.replace('-containers', '')
        pod_obj = self.inv.get_by_id(self.get_env(), pod_id)
        if not pod_obj:
            self.log.error('inventory has no pod with uid={}'.format(pod_id))
            return []

        # TODO: temporary
        labels = pod_obj.get('labels', {})
        if labels and labels.get('calipso-rancher-pod-for-kube-proxy'):
            return [self.get_rancher_proxy_container(pod_obj)]

        host = pod_obj['host']
        pod_filter = 'spec.nodeName={}'.format(host)
        pods = self.api.list_pod_for_all_namespaces(field_selector=pod_filter)
        ret = []
        if not pods or len(pods.items) == 0:
            self.log.error('failed to find pod with nodeName={}'.format(host))
            return []

        pod = next((pod for pod in pods.items if pod.metadata.uid == pod_id),
                   None)
        if not pod:
            self.log.error('failed to find pod with uid={}'.format(pod_id))
            return []

        for container in pod.spec.containers:
            ret.append(self.get_container(container, pod, pod_obj))
        return ret

    def get_rancher_proxy_container(self, pod_obj):
        doc = {
            'type': 'container',
            'namespace': pod_obj['namespace'],
            'host': pod_obj['host'],
            'pod': {'id': pod_obj['id'], 'name': pod_obj['object_name']},
            'id': '{}-kube-proxy'.format(pod_obj['id']),
            'name': 'kube-proxy',
            'container_id': 'kube-proxy'
        }
        self.update_container_config(doc=doc, pod_obj=pod_obj)
        self.update_interface_link(doc=doc, pod_obj=pod_obj)
        self.update_proxy_container_info(container=doc, pod_obj=pod_obj)
        return doc

    def get_container(self, container, pod, pod_obj):
        doc = {
            'type': 'container',
            'environment': self.get_env(),
            'namespace': pod.metadata.namespace,
            **self.get_container_data(container)
        }
        self.update_container_status_data(doc, pod_obj)
        doc.update({
            'host': pod_obj['host'],
            'pod': {
                'id': pod_obj['id'],
                'name': pod_obj['object_name']
            },
            'ip_address': pod_obj.get('pod_status', {}).get('pod_ip', ''),
            'id': '{}-{}'.format(pod_obj['id'], doc['name'])
        })
        if doc.get('image'):
            doc['image'] = {"name": doc['image']}
        self.update_container_config(doc=doc, pod_obj=pod_obj)
        self.update_interface_link(doc=doc, pod_obj=pod_obj)
        self.update_proxy_container_info(container=doc, pod_obj=pod_obj)
        return doc

    @staticmethod
    def _get_state_from_container_status(container_status):
        for status_key in ('waiting', 'running', 'terminated'):
            if container_status.get('state', {}).get(status_key):
                return status_key

    def update_container_status_data(self, doc: dict, pod_obj: dict):
        container_statuses = pod_obj['pod_status']['container_statuses']
        container_status = next(s for s in container_statuses
                                if s['name'] == doc['name'])
        if not container_status:
            self.log.error('failed to find container_status record '
                           'for container {} in pod {}'
                           .format(doc['name'], pod_obj['name']))
            return
        container_id = container_status['container_id']
        if container_id is None:
            doc['container_type'] = container_status['name']
            doc['container_id'] = container_status['image']
        else:
            id_parts = container_id.split('://')
            doc['container_type'] = id_parts[0]
            doc['container_id'] = id_parts[1]
        doc['container_status'] = container_status
        doc['state'] = self._get_state_from_container_status(container_status)

    @classmethod
    def get_container_data(cls, container: V1Container):
        return cls.class_to_dict(data_object=container, exclude=cls.ATTRIBUTES_TO_IGNORE)

    def update_container_config(self, doc, pod_obj):
        if doc.get('state') == 'waiting':
            return

        cmd = 'docker inspect {}'.format(doc['container_id'])
        try:
            output = self.run(cmd, ssh_to_host=pod_obj['host'])
            data = json.loads(output)
        except SshError as e:
            self.log.warning('"docker inspect" cmd failed for container {}. '
                             'Error: {}'.format(doc['container_id'], e))
            return
        except JSONDecodeError as e:
            self.log.error('error reading output of cmd: {}, {}'
                           .format(cmd, str(e)))
            return
        if not data:
            return

        data = data[0]
        if 'State' in data:
            doc['container_state'] = data['State']
            doc['state'] = data['State']['Status']  # Prefer actual state to the one fetched from container_status
        if 'Config' in data:
            doc['config'] = data['Config']
            self.update_container_sandbox(doc=doc, pod_obj=pod_obj)

    def update_container_sandbox(self, doc: dict, pod_obj: dict) -> None:
        sandbox_id = doc['config'].get('Labels').get(self.SANDBOX_ID_ATTR)
        if not sandbox_id:
            return

        cmd = 'docker inspect {}'.format(sandbox_id)
        output = self.run(cmd, ssh_to_host=pod_obj['host'])
        try:
            data = json.loads(output)
        except JSONDecodeError as e:
            self.log.error('error reading output of cmd: {}, {}'
                           .format(cmd, str(e)))
            return
        if not data:
            return

        doc['sandbox'] = data[0]
        self.find_network(doc)

    def update_interface_link(self, doc: dict, pod_obj: dict):
        if doc.get('state') != 'running' or doc['namespace'] == 'cattle-system' or doc['name'] == 'kubernetes-dashboard':
            doc['vnic_index'] = ''
            return

        interface_name = 'vpp1' if 'VPP' in self.configuration.environment['mechanism_drivers'] else 'eth0'
        file_name = "/sys/class/net/{}/iflink".format(interface_name)
        cmd = 'docker exec {0} sh -c "test -f {1} && cat {1} || exit 0"'.format(doc['container_id'], file_name)
        try:
            output = self.run(cmd, ssh_to_host=pod_obj['host'])
            doc['vnic_index'] = output.strip()
            self.add_container_to_vnic(container=doc, pod_obj=pod_obj)
        except (SshError, CredentialsError, HostAddressError):
            doc['vnic_index'] = ''

    # find network matching the one sandbox, and keep its name
    def find_network(self, doc):
        networks = doc['sandbox']['NetworkSettings']['Networks']
        if not networks:
            return
        if isinstance(networks, dict):
            network_names = list(networks.keys())
            network = network_names[0]
            network = networks[network]
        else:
            network = networks[0]
        network_id = network['NetworkID']
        network_obj = self.inv.get_by_id(self.get_env(), network_id)
        if not network_obj:
            return
        doc['network'] = network_id

    def add_container_to_vnic(self, container: dict, pod_obj: dict) -> None:
        condition = {
            'environment': self.get_env(),
            'type': 'vnic',
            'host': pod_obj['host'],
        }
        if 'VPP' in self.configuration.environment['mechanism_drivers']:
            condition['ip_address'] = container['ip_address']
        else:
            condition['index'] = container['vnic_index']
        vnic = self.inv.find_one(condition)
        if not vnic:
            return

        # TODO: figure out this logic
        # re-calc new ID and name path for vNIC and vNICs folder
        # self.set_folder_parent(vnic, object_type='vnic',
        #                        master_parent_id=container['id'],
        #                        master_parent_type='container')
        vnic_containers = vnic.get('containers', [])
        vnic_container = {'id': container['container_id'], 'name': container['name']}
        vnic['containers'] = vnic_containers.append(vnic_container) if vnic_containers else [vnic_container]
        self.inv.set(vnic)

        # self.inv.save_inventory_object(vnic,
        #                                parent=container,
        #                                environment=self.get_env())

    def update_proxy_container_info(self, container, pod_obj):
        if container['name'] != self.PROXY_ATTR:
            return
        container['container_app'] = self.PROXY_ATTR
        self.update_proxy_container_config(container)
        self.update_proxy_nat_tables(container)
        container['vservices'] = self.get_proxy_container_vservices(pod_obj)
        self.add_proxy_container_to_vservices(container)

    def update_proxy_container_config(self, container: dict) -> None:
        command = container.get('command')
        if not command or not isinstance(command, list) or len(command) < 2:
            self.log.error('unable to find {} command file '
                           'for container {}'
                           .format(self.PROXY_ATTR, container['id']))
            return
        conf_line = command[1]
        if not isinstance(conf_line, str) \
                or not conf_line.startswith('--config='):
            self.log.error('unable to find {} command config file '
                           'for container {}'
                           .format(self.PROXY_ATTR, container['id']))
            return
        conf_file = conf_line[len('--config='):]
        cmd = 'docker exec {} cat  {}'.format(container['container_id'],
                                              conf_file)
        conf_file_contents = self.run(cmd=cmd, ssh_to_host=container['host'])
        container['kube_proxy_config'] = conf_file_contents

    def update_proxy_nat_tables(self, container: dict) -> None:
        cmd = 'docker exec {} iptables -t nat -n -L' \
            .format(container['container_id'])
        nat_tables = self.run(cmd=cmd, ssh_to_host=container['host'])
        container['nat_tables'] = nat_tables

    def get_proxy_container_vservices(self, pod_obj: dict) -> list:
        pods = self.inv.find_items({
            'environment': self.get_env(),
            'type': 'pod',
            'host': pod_obj['host'],
            'vservices': {'$exists': 1}
        })
        vservices = []
        for pod in pods:
            vservices.extend(pod['vservices'])
        return vservices

    def add_proxy_container_to_vservices(self, container: dict) -> None:
        for vservice in list(container.get('vservices', [])):
            self.add_proxy_container_to_vservice(container=container, vservice=vservice)

    def add_proxy_container_to_vservice(self, container: dict, vservice: dict) -> None:
        vservice_obj = self.inv.get_by_id(self.get_env(), vservice['id'])
        if not vservice_obj:
            self.log.error('failed to find vservice object with id {} '
                           'in container {} (id: {})'
                           .format(vservice['id'],
                                   container['object_name'],
                                   container['id']))
        if self.PROXY_ATTR not in vservice_obj:
            vservice_obj[self.PROXY_ATTR] = []
        matches = [p for p in vservice_obj[self.PROXY_ATTR]
                   if p['id'] == container['id']]
        if not matches:
            proxy_data = dict(id=container['id'], name=container['name'],
                              host=container['host'])
            vservice_obj[self.PROXY_ATTR].append(proxy_data)
            self.inv.set(vservice_obj)


class CommandCenter:
    def __init__(self, numbers):
        self.numbers = numbers

    def process(self):
        command_data = input().split()
        command_count = 0
        while True:
            if command_data[0] == "end":
                break

            if command_data[0] == "swap":
                index_1 = int(command_data[1])
                index_2 = int(command_data[2])
                if 0 <= index_1 <= len(self.numbers) - 1 and 0 <= index_2 <= len(self.numbers) - 1:
                    self.numbers[index_1], self.numbers[index_2] = self.numbers[index_2], self.numbers[index_1]
                    print(self.numbers)
                    command_count += 1
                else:
                    print(self.numbers)
                    command_count += 1
            elif command_data[0] == "enumerate_list":
                enumerate_list = []
                for i in range(len(self.numbers)):
                    tuple_result = (i, self.numbers[i])
                    enumerate_list.append(tuple_result)
                print(enumerate_list)
                command_count += 1
            elif command_data[0] == "max":
                print(max(self.numbers))
                command_count += 1
            elif command_data[0] == "min":
                print(min(self.numbers))
                command_count += 1
            elif command_data[0] == "get_divisible":
                divider = int(command_data[2])
                result_list = []
                for i in self.numbers:
                    if i % divider == 0:
                        result_list.append(i)
                print(result_list)
                command_count += 1
            command_data = input().split()

        print(command_count)

obj = CommandCenter(numbers=list(map(int, input().split())))
obj.process()


from scipy.spatial import distance

def getRelationDistance(entityData, x=650, y=740):
    location = entityData.info.location
    loc = location.split()
    x1 = float(loc[0])
    y1 = float(loc[1])
    p = (x1, y1)
    xFixed = getDistanceDictionaryX(p[0], x, y)
    yFixed = getDistanceDictionaryY(p[1], x, y)
    print(xFixed)
    return 0

def getDistanceDictionaryX(coord, x, y):
    point1 = (x, y)
    xFixed = {point1: []}
    for i in range(1079):
        point2 = (coord, i)
        d = distance.euclidean(point1, point2)
        xFixed[point1].append([point2, d])
    return xFixed

def getDistanceDictionaryY(coord, x, y):
    point1 = (x, y)
    yFixed = {point1: []}
    for i in range(1079):
        point2 = (i, coord)
        d = distance.euclidean(point1, point2)
        yFixed[point1].append([point2, d])
    return yFixed


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul  1 15:11:31 2019

@author: quanghuy
"""
import numpy as np
import imageio
import matplotlib.pyplot as plt
np.random.seed(1)

# filename structure
path = '/Users/quanghuy/Documents/Massp/Class/20190701/YALE/centered/' # path to the database
ids = range(1, 16) # 15 persons
states = ['centerlight', 'glasses', 'happy', 'leftlight',
          'noglasses', 'normal', 'rightlight','sad',
          'sleepy', 'surprised', 'wink' ]
prefix = 'subject'
surfix = '.pgm'

# data dimension
h = 231 # hight
w = 195 # width
D = h * w
N = len(states)*15
K = 100

# collect all data
X = np.zeros((D, N))
cnt = 0
for person_id in range(1, 15):
    for state in states:
        fn = path + prefix + str(person_id).zfill(2) + '.' + state + surfix
        X[:, cnt] = imageio.imread(fn).reshape(D)
        cnt += 1

# Doing PCA, note that each row is a datapoint
from sklearn.decomposition import PCA
pca = PCA(n_components=K) # K = 100
pca.fit(X.T) #training data

# projection matrix
U = pca.components_.T

for i in range(U.shape[1]): #show each of Xi
    plt.axis('off')
    f1 = plt.imshow(U[:, i].reshape(h,w), interpolation='nearest')
    # interpolation == 'nearest' means pixel color = nearest coordinates
    f1.axes.get_xaxis().set_visible(False)
    f1.axes.get_yaxis().set_visible(False)
#     f2 = plt.imshow(, interpolation='nearest' )
    plt.gray()
    fn = path + 'store/eigenface' + str(i).zfill(2) + '.png'
    plt.savefig(fn, bbox_inches='tight', pad_inches=0)
#    plt.show()

# See reconstruction of first 6 persons

for person_id in [2]:
    for state in ['centerlight']:
        fn = path + prefix + str(person_id).zfill(2) + '.' + state + surfix
        im = imageio.imread(fn)
        plt.axis('off')
#       plt.imshow(im, interpolation='nearest' )
        f1 = plt.imshow(im, interpolation='nearest')
        f1.axes.get_xaxis().set_visible(False)
        f1.axes.get_yaxis().set_visible(False)
        plt.gray()
        fn = path + 'store/ori' + str(person_id).zfill(2) + '.png'
        # show the the original image
        plt.savefig(fn, bbox_inches='tight', pad_inches=0)
        plt.show()
        # reshape and subtract mean, don't forget
        x = im.reshape(D, 1) - pca.mean_.reshape(D, 1)
        # x is the X-Xo = X1*z1+X2*z2...Xk*zk
        # encode
        z = U.T.dot(x)
        # z is the coordinates matrix
        #decode
        error = []
        for index in [1,10,20,50,100]:
            x_tilde = U[:,:index].dot(z[:index,:]) + pca.mean_.reshape(D, 1)
            # x_tilde = Xo + X1*z1 + ... X_index*z_index
    # reshape to orginal dim
            im_tilde = x_tilde.reshape(h,w)
            plt.axis('off')
    #       plt.imshow(im_tilde, interpolation='nearest' )
            f1 = plt.imshow(im_tilde, interpolation='nearest')
            f1.axes.get_xaxis().set_visible(False)
            f1.axes.get_yaxis().set_visible(False)
            plt.gray()
            fn = path + 'store/res' + str(person_id).zfill(2) + '.png'
            plt.savefig(fn, bbox_inches='tight', pad_inches=0)
            # show the im_tilde
            plt.show()
            loss = 0.0
            for i in range(D):
                loss = loss + float((x_tilde[i]-x[i]))**2
            print(index)
            print(loss**0.5)
            error.append(loss**0.5)
        print(error)
        plt.plot([1,10,20,50,100],error)


#
# @lc app=leetcode id=119 lang=python3
#
# [119] Pascal's Triangle II
#
# https://leetcode.com/problems/pascals-triangle-ii/description/
#
# algorithms
# Easy (51.66%)
# Likes:    1180
# Dislikes: 215
# Total Accepted:    345.1K
# Total Submissions: 667.2K
# Testcase Example:  '3'
#
# Given an integer rowIndex, return the rowIndex^th row of the Pascal's
# triangle.
#
# Notice that the row index starts from 0.
#
#
# In Pascal's triangle, each number is the sum of the two numbers directly
# above it.
#
# Follow up:
#
# Could you optimize your algorithm to use only O(k) extra space?
#
#
# Example 1:
# Input: rowIndex = 3
# Output: [1,3,3,1]
# Example 2:
# Input: rowIndex = 0
# Output: [1]
# Example 3:
# Input: rowIndex = 1
# Output: [1,1]
#
#
# Constraints:
#
#
# 0 <= rowIndex <= 33
#
#
#

# @lc code=start
class Solution:
    def getRow(self, rowIndex: int) -> List[int]:

        if rowIndex == 0:
            return [1]

        res = self.getRow(rowIndex - 1)
        for idx in range(len(res)-1, 0, -1):
            res[idx] = res[idx] + res[idx-1]
        res.append(1)
        return res

# @lc code=end


import os
from flask import Flask, render_template, request, jsonify

from modules import auth, compute, db_manager

app = Flask(__name__)

app.secret_key = os.environ['SESSION_SECRET_KEY']
auth.google_signin_key = os.environ['GOOGLE_SIGNIN_KEY']

@app.route('/')
def root():
    return render_template('index.html', gsignin_key=auth.google_signin_key)

@app.route('/get-user-data', methods=['GET'])
def get_user_data():
    user_info = auth.authenticate(request)
    if user_info:
        return render_template('partials/user_info.html', user_info=user_info)
    else:
        return jsonify({'message': 'error'}), 401

@app.route('/sign-out', methods=['GET'])
def sign_out():
    sign_out_request = auth.sing_out()

    if sign_out_request:
        return jsonify({'message': 'success'})
    else:
        return jsonify({'message': 'error'}), 400

@app.route('/zones', methods=['GET'])
def get_zones():
    user_info = auth.authenticate(request)
    if user_info:
        # zones = compute.list_zones()
        # Uncomment in case I want to check for all the zones
        zones = db_manager.get_available_zones()
        zone_count = len(zones)
        return jsonify({'zones': zones, 'zone_count': zone_count})
    else:
        return jsonify({'message': 'error'}), 401

@app.route('/instances/<zone>/<id>', methods=['GET'])
def get_instance(zone, id):
    user_info = auth.authenticate(request)
    if user_info:
        instance = compute.get_instance(zone, id)

        if instance:
            return render_template(
                'partials/instance.html', instance=instance, zone=zone)
        else:
            return jsonify({'message': 'empty zone'})
    else:
        return jsonify({'message': 'error'}), 401

@app.route('/instances/<zone>', methods=['GET'])
def get_instances(zone):
    user_info = auth.authenticate(request)
    if user_info:
        instances = compute.list_instances(zone)

        if instances:
            return render_template(
                'partials/instances.html', instances=instances, zone=zone)
        else:
            return jsonify({'message': 'empty zone'})
    else:
        return jsonify({'message': 'error'}), 401

@app.route('/instances/<action>', methods=['POST'])
def update_instance(action):
    user_info = auth.authenticate(request)
    if user_info:
        vm = request.form['instance']
        zone = request.form['zone']

        if action == 'start':
            return jsonify(compute.start_vm(zone, vm))
        else:
            return jsonify(compute.stop_vm(zone, vm))

    else:
        return jsonify({'message': 'error'}), 401

@app.route('/operations/<zone>/<operation>', methods=['GET'])
def get_operation(zone, operation):
    user_info = auth.authenticate(request)
    if user_info:
        return jsonify(compute.get_op(zone, operation))
    else:
        return jsonify({'message': 'error'}), 401

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


# Valid Perfect Square
"""
Given a positive integer num, return true if num is a perfect square or false otherwise.

A perfect square is an integer that is the square of an integer. In other words, it is the product of some integer with itself.

You must not use any built-in library function, such as sqrt.
"""

# solution:

class Solution:
    def isPerfectSquare(self, num: int) -> bool:

# solution 1:
#          x,square=1,0

#         while(square<num):
#             square=x**2
#             if square == num:
#                 return True
#             x+=1
#         return False

# solution 2:

        l,r= 0,num

        while(l<=r):
            mid=(l+r)//2
            squared=mid**2
            if squared ==num:
                return True
            elif squared<num:
                l =mid+1
            else:
                r=mid-1

        return False


raw_data = []

with open("traffic.txt") as txt:
	for line in txt.readlines():
		# This splits the string line on any spaces it finds and turns it into a list which is then appened to the end of raw_data
		raw_data.append( line[:-1].split(" ") )

room_numbers = []

#Get all of the unique room numbers from raw_data
for item in raw_data:
	#If room number is not in room_numbers append it to the the list as in integer to sort
	if int(item[1]) not in room_numbers:
		room_numbers.append( int(item[1]) )

#Sort the list
room_numbers.sort()

#COnvert integers back into strings
for index in range(len(room_numbers)):
	room_numbers[index] = str( room_numbers[index] )

#Key will be the room number and value will be a list of lists and each list will hold an entery or exit from a room
room = {}

#For each room find all items in raw data and add them to room with a key of the room number
for number in room_numbers:
	#Temp list will hold all items of a particular room
	#At the start of the for loop the list will be reset to an empty list
	temp_list = []
	#Check each item in raw data to see if it matches the current room
	for item in raw_data:
		if item[1] == number:
			#If it does match add it to the list
			temp_list.append(item)
	#Once all items have been checked add the temp list to the dictionary with the key of the room numbers
	room[number] = temp_list

room_times = {}

#For each room get the total time each person spent in that room and add them together
for specific_room in room_numbers:
	#Time_in_room tracks the total time everyone spent in that room
	time_in_room = 0
	#For each person in that room ff this entry is an In to the room find its pair.
	for item in room[specific_room]:
		if item[2] =="I":
			#Search for the pair
			for pair in room[specific_room]:
				#If it is a pair find the different in the enter and exit time for thetime spent in the room
				if item[0] == pair[0] and pair[2] == "O":
					enter_time = int(item[3])
					exit_time = int(pair[3])
					time_in_room += exit_time-enter_time
					break
	#Insert time into dictionary with the key of the current room number
	room_times[specific_room] = time_in_room

room_people = {}

#This for loop will get all of the unique people in a room
for specific_room in room_numbers:
	#People_in_room will store everyone in a specific room
	people_in_room = []
	#If a person is not in people_in_room we will add them to people_in_room
	for item in room[specific_room]:
		if item[0] not in people_in_room:
			people_in_room.append(item[0])
	#Add people_in_room to room_people iwht the key of the current room
	room_people[specific_room] = people_in_room

#OUTPUT

for specific_room in room_numbers:
	total_people = len(room_people[specific_room])
	time_in_room = room_times[specific_room]
	average_time = time_in_room // total_people
	print("Room "+specific_room+", "+ str(average_time)+
		" minute average visit, "+ str(total_people)+" visitor(s) total")

# Program is desined by Matt


import random
from SkillService import SkillService
from Model import Skill
import simplejson as json

def getImages(event, context):
    skillService = SkillService()
    try:
        primaryCategory = json.loads(event.get("body")).get("primary_category")
        secondary_category = json.loads(event.get("body")).get("secondary_category")
        tertiary_category = json.loads(event.get("body")).get("tertiary_category")

        # images = None
        Skill.validateImageTags(primaryCategory, secondary_category, skillService.getImageTags())

        if primaryCategory and secondary_category and tertiary_category:
            # primaryCategory, secondary_category, tertiary_category
            images = skillService.getMatchingImages(primaryCategory, secondary_category, tertiary_category)
            random.shuffle(images)

        elif primaryCategory and secondary_category:
            # primaryCategory, secondary_category
            images = skillService.getMatchingImages(primaryCategory, secondary_category, None)
            random.shuffle(images)

        else:
            # mystery (nothing else)
            tags = skillService.getImageTags()
            random.shuffle(tags)
            images = skillService.getMatchingImages(tags[0], tags[1], None)
            random.shuffle(images)

        response = {
            'statusCode': 200,
            'body': json.dumps(images, use_decimal=True)
        }
        return response

    except NameError as tagNotFoundException:
        response = {
            "errorType": "Not Found Error",
            "errorMessage": str(tagNotFoundException)
        }
        statusCode = 400
        return {
            'statusCode': statusCode,
            'body': json.dumps(response)
        }

def getMysteryImage(events, context):
    skillService = SkillService()
    tags = skillService.getImageTags()

    random.shuffle(tags.get('primary_categories'))
    random.shuffle(tags.get('secondary_categories'))
    images = skillService.getMatchingImages(tags.get('primary_categories')[0], tags.get('secondary_categories')[0], None)
    random.shuffle(images)

    response = {
        'statusCode': 200,
        'body': json.dumps(images, use_decimal=True)
    }
    return response


#!/usr/bin/env python2
#
# download_aemo.py: download AEMO dispatch, price and demand data.
#
# Copyright (c) 2014 Cameron Patrick <cameron@largestprime.net>
#
# This file is part of AusEnergyViz. AusEnergyViz is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, see <http://www.gnu.org/licenses/>.

import urllib3
import re
import os
import sys
import datetime
import argparse
import socket

# base URLs for AEMO/NEM data
URL_DISPATCH_CURRENT = 'http://www.nemweb.com.au/REPORTS/CURRENT/Dispatch_SCADA/'
URL_DISPATCH_ARCHIVE = 'http://www.nemweb.com.au/REPORTS/ARCHIVE/Dispatch_SCADA/'
URL_DISPATCH_SWIS = 'http://data.imowa.com.au/datafiles/facility-scada/'
URL_PRICEDEMAND = 'http://www.nemweb.com.au/mms.GRAPHS/data/'
RETRY_LIMIT = 5

# List of regions (states) for price/demand data. Each state has a start date
# which is the first month for which data is available, and an optional finish
# date which is the last month for which data is available.
STATES_PRICEDEMAND = [
        ('NSW1', (1998, 12), None),
        ('QLD1', (1998, 12), None),
        ('SA1', (1998, 12), None),
        ('SNOWY1', (1998, 12), (2008, 6)),
        ('VIC1', (1998, 12), None),
        ('TAS1', (2005, 5), None)
    ]

def fetch_url(url):
    """Returns the contents of a URL as a string."""
    retry = 0
    while True:
        try:
            http = urllib3.PoolManager()
            response = http.urlopen('GET', url)
            content = response.data
            break
        except (urllib3.exceptions, socket.error) as e:
            retry = retry + 1
            if retry > RETRY_LIMIT:
                sys.stderr.write("***FAILED*** reason: %s\n" % str(e.reason))
                sys.exit(1)
    return content

def extract_regexp_set(page, regexp):
    """Returns an alphabetically sorted list of matches to a regular expression.
    Useful for finding file names in a blob of HTML, for example.
    """
    return sorted(set(re.findall(regexp, page)))

def archived_zip_exists(z, archive_list):
    """Given a 'long' dispatch zip name (for a 5 minute data point), see
    if there is a matching daily archive zip present in the given set.
    """
    date_match = re.search(r'(?i)_([0-9_]+).zip', z)
    if date_match is None:
        return False
    date_str = date_match.group(1)
    archive_file = 'PUBLIC_DISPATCHSCADA_%s.ZIP' % date_str[0:8]
    return archive_file in archive_list

def fetch_aemo_zips(index_url, dir, dir_archive=None):
    """Downloads AEMO dispatch zip files from the AEMO web site. Zip files which
    have already been downloaded are skipped. Zip files containing 5min data
    where we already have the daily archive file are not downloaded, and any
    existing ones are deleted.

    Args:
        index_url: the URL of the base directory on the AEMO web site to
            download zip files from
        dir: the path to save dispatch data to
        dir_archive: if present, the path to archived dispatch data so we know
            what 5min data can be skipped or deleted
    """
    sys.stderr.write("%s: " % index_url)
    index_html = fetch_url(index_url)

    zip_list = extract_regexp_set(index_html, r'(?i)PUBLIC_DISPATCHSCADA_[0-9_]+.zip')
    sys.stderr.write("found %d zips.\n" % len(zip_list))

    if not os.path.isdir(dir):
        os.makedirs(dir)

    # make a list of archived/aggregated zips so we can skip duplicates
    if dir_archive is not None:
        archive_list = set(s.upper() for s in os.listdir(dir_archive))
    else:
        archive_list = set()

    existing_list = set(z.upper() for z in os.listdir(dir))

    skipped = 0
    for z in zip_list:
        path = os.path.join(dir, z)

        # skip non-aggregate files if we have the aggregated version
        if archived_zip_exists(z, archive_list):
            skipped += 1
            continue

        # skip files that already exist (case insensitive)
        if z.upper() in existing_list:
            skipped += 1
            continue

        # download from web site
        sys.stderr.write("Downloading: %s " % z)
        zip_content = fetch_url(index_url + z)

        # save to file
        f = file(path, 'wb')
        f.write(zip_content)
        f.close()

        sys.stderr.write("[%d bytes]\n" % len(zip_content))
    sys.stderr.write("Skipped %d zips.\n" % skipped)

    # clean old zip files
    for filename in os.listdir(dir):
        if archived_zip_exists(filename, archive_list):
            sys.stderr.write("Deleting:    %s\n" % filename)
            os.unlink(os.path.join(dir, filename))

def fetch_aemo_pricedemand(url, path, state, first, last=None):
    """Download price and demand data from the AEMO web site.

    Args:
        url: the base URL of the price/demand CSVs to download
        state: string containing the NEM state or region to download
        first: tuple (year,month) of the first month to download
        last: tuple (year,month) of the last month to download, or None to
            use the current date.
    """
    if last is None:
        today = datetime.date.today()
        last = (today.year, today.month)

    if not os.path.isdir(path):
        os.makedirs(path)

    cur = None
    while True:
        # advance to next month
        if cur is None:
            cur = first
        else:
            # increment month
            cur = (cur[0], cur[1] + 1)
            # if month passes 12, increment year
            if cur[1] > 12:
                cur = (cur[0] + 1, 1)
        # have we reached the current month yet?
        if cur >= last:
            break

        # determine name of file to download, skip if it exists
        filename = 'DATA%.4d%.2d_%s.csv' % (cur[0], cur[1], state)
        outfile = os.path.join(path, filename)
        if os.path.exists(outfile):
            continue

        # download from web site
        sys.stderr.write("Downloading: %s ..." % filename)
        csv_content = fetch_url(url + filename)

        # save to file
        f = file(outfile, 'wb')
        f.write(csv_content)
        f.close()
        sys.stderr.write(" done\n")

def fetch_swis_dispatch(url, path):
    """Download dispatch data for Western Australia (SWIS)."""

    if not os.path.isdir(path):
        os.makedirs(path)
    existing_list = set(s.lower() for s in os.listdir(path))

    # the most recent CSV file we have might have changed since last download.
    # if any newer CSV files are present, we assume that older ones are up-to-date.
    last_existing = None
    if len(existing_list) > 0:
        last_existing = sorted(existing_list, reverse=True)[0]

    sys.stderr.write("%s: " % url)
    index_html = fetch_url(url)
    csv_list = extract_regexp_set(index_html, r'(?i)facility-scada-[0-9]+-[0-9]+.csv')
    sys.stderr.write("found %d CSVs.\n" % len(csv_list))

    skipped = 0
    for z in csv_list:
        outfile = os.path.join(path, z)

        # skip files that already exist (case insensitive)
        if z.lower() in existing_list and z.lower() != last_existing:
            skipped += 1
            continue

        # download from web site
        sys.stderr.write("Downloading: %s " % z)
        csv_content = fetch_url(url + z)

        # save to file
        f = file(outfile, 'wb')
        f.write(csv_content)
        f.close()

        sys.stderr.write("[%d bytes]\n" % len(csv_content))
    sys.stderr.write("Skipped %d CSVs.\n" % skipped)

if __name__ == '__main__':
    # parse arguments
    parser = argparse.ArgumentParser(description='Download AEMO dispatch and demand data.')
    parser.add_argument('path_base', metavar='PATH',
            help='base directory to store downloaded data in')
    args = parser.parse_args()

    # determine output directories based on our base path
    path_archive = os.path.join(args.path_base, 'dispatch_daily')
    path_current = os.path.join(args.path_base, 'dispatch_5min')
    path_pricedemand = os.path.join(args.path_base, 'pricedemand')
    path_swis_dispatch = os.path.join(args.path_base, 'dispatch_swis')

    # download dispatch data - first daily archives, then 5 minute current data
    fetch_aemo_zips(URL_DISPATCH_ARCHIVE, path_archive)
    fetch_aemo_zips(URL_DISPATCH_CURRENT, path_current, path_archive)

    # download price and demand data
    for state, first, last in STATES_PRICEDEMAND:
        fetch_aemo_pricedemand(URL_PRICEDEMAND, path_pricedemand, state, first, last)

    # download SWIS (IMOWA) dispatch data
    fetch_swis_dispatch(URL_DISPATCH_SWIS, path_swis_dispatch)


"""
The script evaluate the overlap of a knowledge base mapped to ConceptNet with ConceptNet, and the precision@k based on ConceptNet.
It takes at least two arguments:
    * The knowledge base you want to evaluate. The format is the following: a subject, predicate, object triple separated by a comma, then a tab, then the frequency/score for this triple. The file must be sorted by score/frequency.
    * ConceptNet, formatted with a subject, predicate, object, score separated by a tab.
    * A file of files used for training (they must contain the alignement, with the [SEP] token.
"""

import os
from sys import argv

KB = argv[1]
CONCEPTNET_PATH = argv[2]
if len(argv) > 3:
    TRAINING_FILES = argv[3:]
else:
    TRAINING_FILES = []

cn = set()

with open(CONCEPTNET_PATH) as f:
    for line in f:
        s,p,o,_ = line.strip().split("\t")
        cn.add((s + "," + p + "," + o).lower().replace("_", ""))

cn2 = set()
for filename in TRAINING_FILES:
    with open(filename) as f:
        for line in f:
            line = line.replace("<|endoftext|>", "")
            if "[SEP]" in line:
                spo = line.strip().split("[SEP]")[1].lower().replace("\t", ",").replace(" ", "")
            else:
                line = line.strip().lower().replace(" ", "").split("\t")
                spo = line[0].split(",")
                spo[1] = line[1]
                spo = ",".join(spo)
            cn2.add(spo)

def compute_overlap(filename):
    preds = set()
    last_index = -1
    last_cn_triple = ""
    rrs = []
    with open(filename) as f:
        for i, line in enumerate(f):
            split_temp = line.lower().split("\t")
            if len(split_temp) != 2:
                continue
            spo, _ = split_temp
            spo = spo.replace(" ", "")
            preds.add(spo)
            if spo in cn:
                last_index = len(preds)
                rrs.append(1.0 / last_index)
                last_cn_triple = spo
    preds_in_cn = cn.intersection(preds)
    print("Total number of predictions:", len(preds))
    print("Position last prediction in ConceptNet:", last_index, "(", last_index / len(preds) * 100 ,")")
    print(last_cn_triple)
    print("MRR:", sum(rrs) / len(rrs))
    print("Preds in ConceptNet:", len(preds_in_cn), ",", len(preds_in_cn) / len(cn) * 100, "%")
    not_in_overlap = preds_in_cn.difference(cn2)
    preds_in_train = preds_in_cn.intersection(cn2)
    print("Preds in ConceptNet and not in overlap:", len(not_in_overlap), len(not_in_overlap) / (len(cn) - len(preds_in_train)) * 100, "%")
    return preds_in_cn, not_in_overlap

def p_at_k(filename, at=-1):
    res = []
    with open(filename) as f:
        for i, line in enumerate(f):
            spo, _ = line.lower().split("\t")
            spo = spo.replace(" ", "")
            if i == at:
                break
            if spo in cn:
                res.append(1)
            else:
                res.append(0)
    return sum(res) / len(res) * 100

preds_in_cn, not_in_overlap = compute_overlap(KB)

print("Precision: ", p_at_k(KB))
for i, at in enumerate([1, 5, 10, 20, 50, 100, 200, 300, 1000, 5000, 10000]):
    temp = p_at_k(KB, at)
    print("Precision at:", at, ":", temp)
    recall = temp * at / len(cn)
    print("Recall at:", at, ":", recall)


# -*- coding: utf-8 -*-
########################################################################################################
# AI人工智障写作 - https://github.com/BlinkDL/AI-Writer
########################################################################################################

import json

import numpy as np
import paddle

import paddle_src.utils
from paddle_src.model import GPT, GPTConfig

# paddle_src.utils.set_seed(42) # 是否固定随机数（固定后每次运行的生成结果都一样）

print("\nPaddle2.x版本AI人工智障写作 https://github.com/BlinkDL/AI-Writer")
print("注：这是使用paddle框架改写后的AI人工智障写作！！！！！！！！！！！！！")
print("请关注原作者的知乎 https://zhuanlan.zhihu.com/p/394766831")
print("\n声明：模型的训练数据全部来自网文，缺乏生活常识。生成的文字仅供娱乐。请遵守法律法规。")

# 需 paddle 2.2.x 及以上版本

MODEL_NAME = "model/wangwen-2022-02-15"  # 模型名
WORD_NAME = "model/wangwen-2022-02-15"  # 这个也修改

NUM_OF_RUNS = 999  # 写多少遍
LENGTH_OF_EACH = 512  # 每次写多少字

top_p = 0.75  # 这个的范围是 0 到 1。越大，变化越多。越小，生成效果越规矩。自己试试 0 和 0.5 和 1.0 的效果就知道了
top_p_newline = 0.9

# 开头非常重要。开头需创造剧情点。开头文笔越好，续写就越好。开头乱写，续写也乱写。
# 开头这样输入：
# context = "魔法"
# context = "“区区"
# context = "三体舰队"
context = "这是一颗"
# context = "众人一惊，没想到这林黛玉的剑法竟如此精妙，只见在那剑影下，剑尖朝着伏地魔的脖子探去，眼见避无可避，伏地魔情急，大喊"

# 多行的开头这样输入：
# context = """
# 这几天心里颇不宁静。今晚在院子里坐着乘凉，忽然想起日日走过的荷塘，在这满月的光里，总该另有一番样子吧。月亮渐渐地升高了，墙外马路上孩子们的欢笑，已经听不见了；妻在屋里拍着闰儿，迷迷糊糊地哼着眠歌。我悄悄地披了大衫，带上门出去。
# 沿着荷塘，是一条曲折的小煤屑路。这是一条幽僻的路；白天也少人走，夜晚更加寂寞。荷塘四面，长着许多树，蓊蓊郁郁的。路的一旁，是些杨柳，和一些不知道名字的树。没有月光的晚上，这路上阴森森的，有些怕人。今晚却很好，虽然月光也还是淡淡的。
# 路上只我一个人，背着手踱着。这一
# """

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

ctx_len = 512
n_layer = 12
n_head = 12
n_embd = n_head * 64
n_attn = n_embd
n_ffn = n_embd

context = context.strip().split("\n")
for c in range(len(context)):
    context[c] = context[c].strip().strip("\u3000")
context = "\n" + ("\n".join(context)).strip()
print("您输入的开头有 " + str(len(context)) + " 个字。注意，模型只会看最后 " + str(ctx_len) + " 个字。")

with open(WORD_NAME + ".json", "r", encoding="utf-16") as result_file:
    word_table = json.load(result_file)

vocab_size = len(word_table)

train_dataset = lambda: None
train_dataset.stoi = {v: int(k) for k, v in word_table.items()}
train_dataset.itos = {int(k): v for k, v in word_table.items()}
UNKNOWN_CHAR = train_dataset.stoi["\ue083"]

print("\nLoading model...")
model = GPT(
    GPTConfig(
        vocab_size,
        ctx_len,
        n_layer=n_layer,
        n_head=n_head,
        n_embd=n_embd,
        n_attn=n_attn,
        n_ffn=n_ffn,
    )
)
model.set_state_dict(paddle.load(MODEL_NAME + ".pdparams"))

model.eval()

print("done:", MODEL_NAME, "&", WORD_NAME)

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

for run in range(NUM_OF_RUNS):

    x = np.array(
        [train_dataset.stoi.get(s, UNKNOWN_CHAR) for s in context], dtype=np.int64
    )

    real_len = len(x)
    print_begin = 0

    for i in range(LENGTH_OF_EACH):

        if i == 0:

            print(("-" * 60) + "\n" + context.replace("\n", "\n  ").strip("\n"), end="")
            print_begin = real_len

        with paddle.no_grad():
            xxx = paddle.to_tensor(x[-ctx_len:], dtype="int64").unsqueeze(0)
            out = model(xxx)[0]
            out[:, :, UNKNOWN_CHAR] = -10000.0
        pos = -1 if real_len >= ctx_len else real_len - 1

        if train_dataset.itos[int(x[real_len - 1])] == "\n":
            char = paddle_src.utils.sample_logits(
                out, pos, temperature=1.0, top_p=top_p_newline
            )
        else:
            char = paddle_src.utils.sample_logits(
                out, pos, temperature=1.0, top_p=top_p
            )

        x = np.append(x, char)
        real_len += 1

        if i % 2 == 1 or i == LENGTH_OF_EACH - 1 or i < 10:
            completion = "".join(
                [train_dataset.itos[int(i)] for i in x[print_begin:real_len]]
            )
            print(completion.replace("\n", "\n  "), end="", flush=True)
            print_begin = real_len
    print()


#!/usr/bin/env python
# coding=utf-8
"""
Author: Deean
Date: 2022-02-04 23:06:10
LastEditTime: 2022-02-04 23:14:18
Description:
FilePath: 100162.字符串轮转.py
"""
#
# @lc app=leetcode.cn id=100162 lang=python3
#
# [面试题 01.09] 字符串轮转
#

# @lc code=start

class Solution:
    def isFlipedString(self, s1: str, s2: str) -> bool:
        if len(s1) != len(s2):
            return False
        elif len(s1) == len(s2) == 0:
            return True
        else:
            for i in range(0, len(s1)):
                if s1[i:] + s1[:i] == s2:
                    return True
            return False
# @lc code=end

if __name__ == "__main__":
    s = Solution()
    print(s.isFlipedString("aa", "aba"))
    print(s.isFlipedString(s1="waterbottle", s2="erbottlewat"))

面试题/面试题 01.09. 字符串轮转.py


#!/usr/bin/env python

from I3Tray import *
import sys, os, glob

#
## load everything

'''
def try_icecube_import(name):
    full_name = 'icecube.' + name
    try:
        __import__(full_name)
        globals()[name] = sys.modules[full_name]
    except ImportError:
        pass
lib_dir = os.path.join(os.getenv('I3_BUILD'),'lib','icecube')
libs = map(os.path.basename,glob.glob(lib_dir + os.path.sep + '*.so'))
for lib in libs:
    try_icecube_import(os.path.splitext(lib)[0])
'''

from icecube.level3_filter_cascade.CascadeL3TraySegment import CascadeL3

def make_parser():
    """Make the argument parser"""
    from optparse import OptionParser
    parser = OptionParser()

    parser.add_option("-i", "--input", action="store",
        type="string", default="", dest="infile",
        help="Input i3 file(s)  (use comma separated list for multiple files)")

    parser.add_option("-g", "--gcd", action="store",
        type="string", default="", dest="gcdfile",
        help="GCD file for input i3 file")

    parser.add_option("-o", "--output", action="store",
        type="string", default="", dest="outfile",
        help="Main i3 output file")

    parser.add_option ("-m", "--MC", default=False, action="store_true",
                       dest="isMC",
                       help="The data is MC (different behaivior for TWR)")
    parser.add_option ("-t", "--MT", default="corsika",
                       dest="MCType",
                       help="MCType (to book correctly the MCTruth)")

    parser.add_option("--amplitudetablepath", action="store",
        type="string", default="/cvmfs/icecube.opensciencegrid.org/data/photon-tables/splines/ems_mie_z20_a10.abs.fits",
        dest="AmplitudeTable", help="Path to AmplitudeTable")

    parser.add_option("--timingtablepath", action="store",
        type="string", default="/cvmfs/icecube.opensciencegrid.org/data/photon-tables/splines/ems_mie_z20_a10.prob.fits",
        dest="TimingTable", help="Path to TimingTable")

    parser.add_option("-n", "--num", action="store",
        type="int", default=-1, dest="num",
        help="Number of frames to process")

    parser.add_option("-y", "--year", action="store",
        type="string", default="2015", dest="year",
        help="The year of the data/mc being processed")

    return parser

def main(options, stats={}):
    icetray.logging.set_level("WARN")

    tray = I3Tray()

    tray.AddSegment(CascadeL3,
                    gcdfile=options['gcdfile'],
                    infiles=options['infile'],
                    output_i3=options['outfile'],
                    AmplitudeTable=options['AmplitudeTable'],
                    TimingTable=options['TimingTable'],
                    MCbool=options['isMC'],
                    MCtype=options['MCType'],
		    Year=options['year'])

    # make it go
    if options['num'] >= 0:
        tray.Execute(options['num'])
    else:
        tray.Execute()

    # print timers
    usagemap = tray.Usage()

    for mod in usagemap:
        print (mod)

### iceprod stuff ###
try:
    from iceprod.modules import ipmodule
except ImportError as e:
    print ('Module iceprod.modules not found. Will not define IceProd Class')
else:
    Level2Filter = ipmodule.FromOptionParser(make_parser(),main)
### end iceprod stuff ###

# the business!
if (__name__=="__main__"):

    from optparse import OptionParser

    parser = make_parser()

    (options,args) = parser.parse_args()
    opts = {}
    # convert to dictionary
    for name in parser.defaults:
        value = getattr(options,name)
        if name == 'infile' and ',' in value:
            value = value.split(',') # split into multiple inputs
        opts[name] = value

    main(opts)


import os

from HdRezkaApi import *
import requests

def download_video(directory, file_name, link):
    r = requests.get(link, stream=True)

    directory_exists = os.path.exists(directory)

    if not directory_exists:
        os.makedirs(directory)

    with open("{}/{}.mp4".format(directory, file_name), 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024 * 1024):
            if chunk:
                f.write(chunk)

url = input('Enter url: ')

rezka = HdRezkaApi(url)

seasons = rezka.getSeasons()
translations = list(rezka.getTranslations().keys())

qualities = list(rezka.getStream(1, 1).videos.keys())

for i in range(0, len(qualities)):
    print("{}. {}".format(i + 1, qualities[i]))

user_choice = input("\nSelect quality: ")
quality = qualities[int(user_choice) - 1]

print("\n\n")

for i in range(0, len(translations)):
    print("{}. {}".format(i + 1, translations[i]))

user_choice = input("\nSelect translation: ")
translation = translations[int(user_choice) - 1]

seasons_count = len(seasons[translation]['seasons'])

film_name = url.split('/')[-1].split('.')[0]

for i in range(1, seasons_count + 1):
    print("Downloading season {}/{}".format(i, seasons_count))

    episode_count = 0

    while True:
        try:
            episode_count += 1
            print("Downloading episode {}".format(episode_count))

            name = "{}-{}".format(i, episode_count)

            download_video(film_name, name, rezka.getStream(i, episode_count, translation)(quality))
        except:
            break


import numpy as np, pylab as py
from xrotorWT import *

airspeeds = np.array([7.,10.,15.,20.,25])
thrust    = np.zeros(airspeeds.shape)
torque    = np.zeros(airspeeds.shape)

for j,airspeed in enumerate(airspeeds):
  rotor = Rotor('nrelPhaseVI_v%3.2f'%airspeed)
  rotor.airspeed = airspeed

  rotor.run_rotor(72.)
  thrust[j] = rotor.performance['thrust']
  torque[j] = rotor.performance['torque']

thrust_exp = [1188.416016,1805.560303,2754.355713,3531.849365,4424.200195]
torque_exp = [822.479187,1431.653931,1622.347534,1219.413940,1497.689331]

py.figure()
py.subplot(2,1,1)
py.plot(airspeeds,thrust,'b.-')
py.plot(airspeeds,thrust_exp,'ks')
py.subplot(2,1,2)
py.plot(airspeeds,torque,'b.-')
py.plot(airspeeds,torque_exp,'ks')
py.show()


def getWordScore(word, n):
    """
    Returns the score for a word. Assumes the word is a valid word.
    The score for a word is the sum of the points for letters in the
    word, multiplied by the length of the word, PLUS 50 points if all n
    letters are used on the first turn.
    Letters are scored as in Scrabble; A is worth 1, B is worth 3, C is
    worth 3, D is worth 2, E is worth 1, and so on (see SCRABBLE_LETTER_VALUES)
    word: string (lowercase letters)
    n: integer (HAND_SIZE; i.e., hand size required for additional points)
    returns: int >= 0
    """

    score = 0

    if len(word) == 0:
        return 0

    else:
        for char in word:
            score += SCRABBLE_LETTER_VALUES[char]

        score *= len(word)

        if len(word) == n:
            score += 50

        return score

Introduction to Computer Science and Programming Using Python/Problem Set 4/PS4 Word Scores.py


import datetime as dt

class Employee:

    raise_amountGlobal = 15.003 # that is a global variable
    gmail = '@gmail.com'
    num_of_Employee = 0

    def __init__(self,firstName,lastName,salary,gender=None): # None helps to declare a optional parameter, not mandatory
        self.first = firstName  # self represents this class, the instance of this class
        self.last = lastName
        self.pay = salary
        self.email = firstName + '.' + lastName + self.gmail # always use self to access the instances of that file
        self.gen = gender
        Employee.num_of_Employee += 1 # this will help increment the number of employees.

    def apply_raiseLocal(self):
        raise_amountLocal = 10.002
        new_pay = int(self.pay + raise_amountLocal) # raise amount is a local variable. It does not work with self,. raise amount will only work for this function
        return new_pay

    def apply_raiseGlobal(self): # the apply function is a regular method
        new_pay = int(self.pay + self.raise_amountGlobal)
        return new_pay

    @staticmethod
    def is_workDay(day):
        if day.weekday == 5 or day.weekday == 6:
            return False
        else:
            return True

    @classmethod # that is a class method, its different from the other methods
    def set_raiseAmount(cls,amount): # a regular we need an object of that function to call it but a class function we can call it directly
        cls.raise_amountGlobal = amount

# print(f'Number of employees is: {Employee.num_of_Employee}')
emp_1 = Employee('Ishe','Murwira',20000)                  # we created an object for the class and call it an instant of the class. The object we created emp1 is still called the instant of the class
emp_2 = Employee('John','Jamerson',25000,'Male')
emp_3 = Employee ('Susan','Rusike',15000,'Female')
# print(f'Number of employees is: {Employee.num_of_Employee}')

# print(emp_1.apply_raiseGlobal())
# print(emp_1.apply_raiseLocal())

# print(Employee.raise_amountGlobal)
# Employee.set_raiseAmount(50)
# print(Employee.raise_amountGlobal)

myDate = dt.date(2021,6,24)
workDay = Employee.is_workDay(myDate)

print(workDay)


import streamlit as st
from models.lineal_model import LinealModel
from models.polynomial_regression import PolynomialRegression
from models.read_csv_model import ReadCsvModel

import pandas as pd

class SimpleRegressionPage:
    def __init__(self):
        model = st.selectbox("Simple Models", ["Linear Regression", "Polynomial Regression"])
        dataset = st.selectbox("DataSet", ["Salary Vs Experience", "Position Salaries", "Polynomial Example",  "Otro Conjunto de Datos"])

        real_name_dataset = {
            "Salary Vs Experience" : "Salary_Data",
            "Position Salaries"    : "Position_Salaries",
            "Polynomial Example"   : "polynomial-regression",
        }

        if dataset == "Otro Conjunto de Datos":
            dataset = ReadCsvModel( st.file_uploader("Otro Documento"), None)
        dataset = ReadCsvModel(real_name_dataset[dataset], "simple_regression")

        if dataset is not None:
            cols = dataset.columns.tolist()

            col1, col2 = st.columns(2)
            x_col = col1.selectbox('Columnas independientes (X)', cols)
            y_col = col2.selectbox('Columna dependiente (y)', cols)

        if model == "Polynomial Regression":
            grado = st.slider("Grado Polinomico", 0, 10, 5, 1)

        # Models
        if model == "Linear Regression":
            LinealModel(dataset, x_col, y_col)
        elif model == "Polynomial Regression":
            PolynomialRegression(dataset, x_col, y_col, grado)


from argparse import ArgumentParser

def get_divisors(n):
    divisors = []
    for i in range(1, int(n/2) + 1):
        if n % i == 0:
            divisors.append(i)
    return divisors

def are_amicable(n, m):
    n_divisors = get_divisors(n)
    if sum(n_divisors) == m:
        m_divisors = get_divisors(m)
        if sum(m_divisors) == n:
            return True
    return False

def main():
    parser = ArgumentParser()
    parser.add_argument('n', help="First number")
    parser.add_argument('m', help="Second number")
    args = parser.parse_args()
    n = int(args.n)
    m = int(args.m)

    if are_amicable(n, m):
        print(f"{n} and {m} are so-called amicable numbers - "
              f"which means that the sum of the proper divisors of one number is the other number, and vice versa.")
    else:
        print(f"Sorry, {n} and {m} are not amicable numbers :(")

if __name__ == '__main__':
    main()


import sqlite3

MIN = 1
MAX = 5
CREATE = 1
READ = 2
UPDATE = 3
DELETE = 4
QUIT = 5

def main():
    choice = 0
    while choice != QUIT:
        display_menu()
        choice = get_menu_choice()
        if choice == CREATE:
            create()
        elif choice == READ:
            read()
        elif choice == UPDATE:
            update()
        elif choice == DELETE:
            delete()

def display_menu():
    print('--------Menu---------')
    print('1. Create.')
    print('2. Read.')
    print('3. Update.')
    print('4. Delete.')
    print('5. Quit.')

def get_menu_choice():
    choice = int(input('Choose your option: '))

    while choice < MIN or choice > MAX:
        print(f'Please choose option between {MIN} and {MAX}.')
        choice = int(input('Choose your option: '))

    return choice

def create():
    print('Create a new position.')
    name = input('The name of position: ')
    price = input('Price: ')
    insert_row(name, price)

def read():
    name = input('Search for position: ')
    num_found = display_item(name)
    print(f'{num_found} row(s) found.')

def update():
    read()
    selected_id = int(input('Choose position to update: '))
    name = input('Enter new name: ')
    price = input('Enter new price: ')
    num_updated = update_row(selected_id, name, price)
    print(f'{num_updated} row(s) updated.')

def delete():
    read()
    selected_id = int(input('Choose position to delete: '))
    sure = input('Are you sure you want to delete this position? y/n\n')
    if sure.lower() == 'y':
        num_deleted = delete_row(selected_id)
        print(f'{num_deleted} row(s) deleted.')

def insert_row(name, price):
    conn = None
    try:
        conn = sqlite3.connect('Data/inventory.db')
        cur = conn.cursor()
        cur.execute('''INSERT INTO Inventory (ItemName, Price)
                        VALUES (?,?)''',
                    (name, price))
        conn.commit()
    except sqlite3.Error as err:
        print('Database error', err)
    finally:
        if conn != None:
            conn.close()

def display_item(name):
    conn = None
    result = []
    try:
        conn = sqlite3.connect('Data/inventory.db')
        cur = conn.cursor()
        cur.execute('''SELECT * FROM Inventory
                    WHERE ItemName == ?''',
                    (name, ))
        result = cur.fetchall()
        for row in result:
            print(f'ID: {row[0]:<3} Name: {row[1]:<15}'
                  f'Price:  {row[2]:<6}')
    except sqlite3.Error as err:
        print('Database error', err)
    finally:
        if conn != None:
            conn.close()
        return len(result)

def update_row(id, name, price):
    conn = None
    try:
        conn = sqlite3.connect('Data/inventory.db')
        cur = conn.cursor()
        cur.execute('''UPDATE Inventory
                        SET ItemName == ?, Price ==?
                        WHERE ItemID == ?''',
                    (name, price, id))
        conn.commit()
        num_updated = cur.rowcount
    except sqlite3.Error as err:
        print('Database error', err)
    finally:
        if conn != None:
            conn.close()
        return num_updated

def delete_row(id):
    conn = None
    try:
        conn = sqlite3.connect('Data/inventory.db')
        cur = conn.cursor()
        cur.execute('''DELETE FROM Inventory
                        WHERE ItemID == ?''',
                    (id,))
        conn.commit()
        num_deleted = cur.rowcount
    except sqlite3.Error as err:
        print('Database error', err)
    finally:
        if conn != None:
            conn.close()
        return num_deleted

if __name__ == '__main__':
    main()


from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask import Blueprint
from flask_restplus import Api
import os

db = SQLAlchemy()

def create_app():
    project_root = os.path.abspath(os.path.dirname(__file__))
    app = Flask("Spending-tracker")
    app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{os.path.join(project_root, 'spending_tracker.db')}"
    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
    db.init_app(app)
    return app

blueprint = Blueprint('api', __name__)
api = Api(
    blueprint,
    title='Spending Tracker',
    version='0.1',
    description='Track your spending with this app',
    doc='/api/doc',
    default_mediatype='json'
)


def solution(A):
    front = []
    back = []

    front.append(0)
    for i in range(1, len(A)):
        if front[i - 1] + A[i] < 0:
            front.append(0)
        else:
            front.append(front[i - 1] + A[i])

    back.append(0)
    for i in range(1, len(A)):
        if back[i - 1] + A[len(A) - 1 - i] < 0:
            back.append(0)
        else:
            back.append(back[i - 1] + A[len(A) - 1 - i])

    result = 0
    for i in range(1, len(A) - 1):
        result = max(result, front[i - 1] + back[len(A) - 2 - i])

    return result

if __name__ == "__main__":
    A = [3, 2, 6, -1, 4, 5, -1, 2]
    print(solution(A))


'''
Implementation taken from https://arxiv.org/abs/1903.09650
which follows derivation given in https://people.maths.ox.ac.uk/gilesm/files/NA-08-01.pdf
'''
import torch

def safe_inverse(x, epsilon=1E-12):
    return x/(x**2 + epsilon)

class SVDGESDD(torch.autograd.Function):
    @staticmethod
    def forward(self, A):
        U, S, V = torch.svd(A)
        self.save_for_backward(U, S, V)
        return U, S, V

    @staticmethod
    def backward(self, dU, dS, dV):
        U, S, V = self.saved_tensors
        Vt = V.t()
        Ut = U.t()
        M = U.size(0)
        N = V.size(0)
        NS = len(S)

        F = (S - S[:, None])
        F = safe_inverse(F)
        F.diagonal().fill_(0)

        G = (S + S[:, None])
        G.diagonal().fill_(float('inf'))
        G = 1/G

        UdU = Ut @ dU
        VdV = Vt @ dV

        Su = (F+G)*(UdU-UdU.t())/2
        Sv = (F-G)*(VdV-VdV.t())/2

        dA = U @ (Su + Sv + torch.diag(dS)) @ Vt
        if (M>NS):
            dA = dA + (torch.eye(M, dtype=dU.dtype, device=dU.device) - U@Ut) @ (dU/S) @ Vt
        if (N>NS):
            dA = dA + (U/S) @ dV.t() @ (torch.eye(N, dtype=dU.dtype, device=dU.device) - V@Vt)
        return dA

def test_SVDSYMEIG_random():
    M, N = 50, 40
    input = torch.rand(M, N, dtype=torch.float64, requires_grad=True)
    assert(torch.autograd.gradcheck(SVDGESDD.apply, input, eps=1e-6, atol=1e-4))

if __name__=='__main__':
    import os
    import sys
    sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

    test_SVDSYMEIG_random()


from airflow import DAG
from airflow.utils.dates import days_ago
from airflow.models import Variable
from airflow.operators.python_operator import PythonOperator

from datetime import datetime, timedelta
import pyodbc
import pandas as pd
from sqlalchemy import create_engine
import urllib
import requests
import time

        ##########################################################################
        # ------------------------- ДОП. ПАРАМЕТРЫ  ---------------------------- #
        ##########################################################################

## Лимит создания лидов в одном цикле
leadlimit = 9999 ##Пока без ограничений

## URL для Post запроса генерации лида в CRM
url_post = 'https://corp.synergy.ru/api/api_d_crm/crm_create_lead'

## Токен авторизации для CRM
token = Variable.get("CRM_API_Token")

## Формируем header для post запроса
header = {'App-Token': f'{token}'}

## Запрос для отбора локальной очереди на генерацию лидов в рамках одного цикла.
## Далее отработанные данные отметим в NM_api_minion с меткой IS_SENT = 1
query_lead_list = f'''
select top({leadlimit})
dd.*
from dbo.[NM_api_minion] dd
left join dbo.NM_api_minion_hst hst with(nolock) on hst.UNTI_ID = dd.UNTI_ID
where 1=1
and dd.IS_SENT = 0    --Статус "Не отправлен"
and hst.UNTI_ID is null --Проверяем что лид с таким UNTI не отправляли
'''

        ##########################################################################
        # --------------------------- Подключения ------------------------------ #
        ##########################################################################

## Подключение к BIDB01
def db_conn():
    print('db_conn starting')
    # Драйвер для подключения к БД
    driver = 'Driver={ODBC Driver 18 for SQL Server}'+ \
            ';TrustServerCertificate=Yes'+ \
            ';Server=' + 'MSK1-BIDB01.synergy.local' + \
            ';Database=' + 'Analytic' + \
            f';UID={Variable.get("tuz_login")}' +  \
            f';PWD={Variable.get("tuz_password")}'

    params = urllib.parse.quote_plus(driver)
    engine = create_engine(f'mssql+pyodbc:///?odbc_connect={params}')
    return engine

## Получить текущую очередь для генерации
def get_query():
    print('get_query starting')
    ## Выгружаем локальную очередь
    df_lead_list = pd.read_sql(query_lead_list, con=db_conn())
    print('get_query df_lead_list success')
    return df_lead_list

## Получить количество строк локальной очереди
def count_query():
    ## Считаем локальную очередь
    print('count_query start')
    count_lead_list = get_query().loc[get_query()['IS_SENT'] == 0].shape[0]
    print('count_query count_lead_list success')

    return count_lead_list

## Функция для обновления поля IS_SENT в очереди
def update_results(result, unti, lead, contact):
    print(f'update_results start -- result:{result} unti:{unti} lead:{lead} contact:{contact}')
    ## Если получили ответ "Успешно" записываем 1 в таблицу очереди
    if result == 1:
        print('result = 1 start update')
        update_query = f"""
        update	dbo.[NM_api_minion]
        set		IS_SENT = 1,
                CONTACT_ID = {contact},
                LEAD_ID = {lead}
        from	dbo.[NM_api_minion]
        where	UNTI_ID = {unti};
        """

    ## Если получили ответ "Ошибки" записываем 2 в таблицу очереди
    else:
        print('result = else start update')
        update_query = f"""
        update	dbo.[NM_api_minion]
        set		IS_SENT = 2
        from	dbo.[NM_api_minion]
        where	UNTI_ID = {unti};
        """
    ## Создаем подключение для обновления
    print('Connecting to BIDB01')
    conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server}'+ \
            ';TrustServerCertificate=Yes'+ \
            ';Server=' + 'MSK1-BIDB01.synergy.local' + \
            ';Database=' + 'Analytic' + \
            f';UID={Variable.get("tuz_login")}' +  \
            f';PWD={Variable.get("tuz_password")}')

    # Открываем подключение
    cursor = conn.cursor()
    print('Connection success')

    # Выполняем обновление
    print('start cursor execute update_query')
    cursor.execute(update_query)

    # Коммитим транзакцию
    conn.commit()

    # Закрываем курсор и подключение
    cursor.close()
    conn.close()

def insert_results(unti, json_value):
    ## Запрос для инсерта в таблицу истории обновления
    print('start insert_query')
    insert_query = f'''
    select
    d.UNTI_ID
    ,'{json_value}' api
    ,d.IS_SENT
    ,d.[VERSION] [CP_TYPE]
    ,d.CONTACT_ID
    ,d.LEAD_ID
    ,GETDATE() SYSMOMENT
    from dbo.NM_api_minion d with(nolock)
    left join NM_api_minion_hst dh with(nolock) on d.UNTI_ID = dh.UNTI_ID
    where
    d.IS_SENT <> 0
    and dh.UNTI_ID is null
    and d.UNTI_ID = {unti}
    '''
    print('try inserting')
    try:
        print('get insert df')
        df_insert = pd.read_sql(insert_query,con=db_conn())
        print('start inserting')
        df_insert.to_sql('NM_api_minion_hst',con=db_conn(),if_exists='append',index=False)
        print('inserting success')
    except:
        print('value already exists in hst table')

                                ###############################################################
                                ###################     ГЕНЕРАТОР ЛИДОВ     ###################
                                ###############################################################

def leadgenerator(leadlimit, url_post, header, query_lead_list):

    print('формируем json df_json_fields')
    ## Оставляем только нужные поля для отправки d JSON
    df_json_fields = get_query()[['LAND', 'VERSION', 'CAMPAIGN' ,'QUOTE_ID'
                ,'UNTI_ID' ,'LEAD_NAME' ,'PHONE' ,'EMAIL' ,'CITY' ,'REGION' ,'COMMENT','GENDER_STUD' ,'BIRTHDATE_STUD'
                ,'DOC_TYPE_STUD' ,'CITIZENSHIP_STUD','SNILS_STUD' ,'PASS_SERIES_STUD','PASS_NUMBER_STUD','PASS_AUTHORITY_STUD','PASS_DATE_STUD','PASS_CODE_STUD'
                ,'LAST_NAME_PARENT','NAME_PARENT','SECOND_NAME_PARENT','BIRTHDATE_PARENT','PHONE_PARENT','EMAIL_PARENT'
                ,'PASS_SERIES_PARENT','PASS_NUMBER_PARENT','PASS_AUTHORITY_PARENT','PASS_DATE_PARENT' ,'PASS_CODE_PARENT' ,'GENDER_PARENT', 'UF_CRM_1456812806']]

    count = count_query()

    print(f'''
        *********************
            count query: {count}
        *********************
        ''')

    while count > 0:
        try:
            for index, row in df_json_fields.iterrows():
                print('*' * 50)
                ## Формируем json файл для каждой строки
                json_value = row.to_json()

                ## Записываем unti_id в качестве переменной
                unti_id = row['UNTI_ID']

                ## Отправляем post запрос на генерацию лидов
                print('try Post request')
                try:
                    res = requests.post(url=url_post, headers=header, data=json_value)
                    print('Post request success')
                except:
                    print('Post request failed')

                ## Записываем результаты в переменные для дальнейшего обновления
                json_result = res.json().get('result')
                json_id_contact = res.json().get('id_contact')
                json_id_lead = res.json().get('id_lead')

                ## Делаем обновления боевой таблицы очереди
                print('start update_results')
                update_results(json_result, unti_id, json_id_lead, json_id_contact)

                ## Записываем данные в таблицу истории
                print('start insert_results')
                insert_results(unti_id, json_value)

                # Таймаут между циклами 1 сек
                print('start timeout')
                time.sleep(5)

                count = count_query()
                print(f'''
                *********************
                    count query: {count}
                *********************
                ''')

        except:
            print('FAIL LOOP')
            break

    print('finish')

##########################################################################
# --------------------------- Настройка DAG ------------------------------ #
##########################################################################

default_args = {'owner': 'Nesh_Markoski',
                'depends_on_past': False,
                'retries': 0,
                'retry_delay': timedelta(seconds=120)
                }

with DAG(dag_id="University_2035_LeadGenerator",
        tags = ['CF/CP'],
        description = "Генерация лидов в CRM системе" ,
        schedule_interval='45 6 * * *',
        start_date=days_ago(1),
        catchup=False,
        default_args=default_args) as dag:

    leadgenerator = PythonOperator(
                    task_id='leadgenerator',
                    python_callable=leadgenerator,
                    op_kwargs={'leadlimit': leadlimit,
                              'url_post':url_post,
                              'header':header,
                              'query_lead_list':query_lead_list})

    leadgenerator


import re,os
from tqdm import tqdm
from pypinyin.contrib.tone_convert import to_tone

path=os.path.abspath('create-database\cedict_1_0_ts_utf-8_mdbg.txt')
opath=os.path.abspath('create-database\cedict_1_0_ts_utf-8_mdbg.csv')

with open(path, 'r',encoding='utf-8-sig') as file:
    with open(opath, 'w',encoding='utf-8-sig') as ofile:
        lines = file.readlines()
        for line in tqdm(lines):
            line=line.lower()
            a=[i+1 for i,x in enumerate(line) if x=='[']
            b=[i for i,x in enumerate(line) if x==']']
            c=[line[a[i]:b[i]] for i in range(len(a))]

            for i in range(len(c)):
                b=list(c[i].split(" "))
                e= ''.join(to_tone(i)+' ' if index != (len(b)-1) else to_tone(i) for index,i in enumerate(b)  )
                e=f"[{e}]"
                # print("e",e)
                f= ''.join((i)+' ' if index != (len(b)-1) else i for index,i in enumerate(b)  )
                f=f"\[{f}\]"
                # print("f",f)
                line=re.sub(f,e,line)
                # print("line A",line)

            line = re.sub(",",";",line)
            line = re.sub("\s",",",line,2)
            line = re.sub(",,",",",line)
            line = re.sub("] ","],",line,1)

            ofile.write(line)


#!/usr/bin/python3
import boto3
import sys
from creds import Creds

creds = Creds("credentials.csv")
#print("Beware You are doing this activity using this account :" + " " + creds.username)

REGION = "ap-south-1"
ACCESS_KEY = creds.access_key_id
SECRET_KEY = creds.secret_key
ec2 = boto3.resource('ec2', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY, region_name=REGION)

instance_ids = ec2.instances.all()
for id in instance_ids:
 print(id.terminate())