import csv
from csis_sql_orm import Session, CSISCurrent
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from config.config import Configs

class CSISStatus(Configs):
    def __init__(self):
        # Pull in config class variables
        super().__init__()
        # Initialize DB/ORM session for use in class
        self.session = Session()
        # Pull status from db, if exists.
        try:
            self.db_status = self.session.query(CSISCurrent).filter_by(id=1).one()
        except NoResultFound:
            self.db_status = None
        except MultipleResultsFound:
            print('multiple results found')
            try:
                self.session.query(CSISCurrent).delete()
                self.session.commit()
            except:
                self.session.rollback()

        self.status_list = self.status_from_file()

    @property
    def db_status(self):
        return self.__db_status

    @db_status.setter
    def db_status(self, db_status):
        """
        Sets db_status to what is in the file if the database, and adds the file data to
        the database if None.
        :param db_status: namedtuple: status data set.
        :return: No return
        """
        if db_status is None:
            # self.db_status = CSISCurrent(self.status_from_file())
            # self.session.add(self.db_status)
            status_dict = self.status_from_file()
            sql_labels = {
                'batch_id': status_dict["batch_id"], 'total': status_dict["Total"],
                'passed': status_dict["Pass"], 'failed': status_dict["Fail"],
                'failed_od': status_dict["Fail OD"],
                'backwards': status_dict["Backwards"], 'n_a': status_dict["N/A"],
                'lost_homing': status_dict[" Lost to Homing"],
                'batch_homes': status_dict["Batch Homes"],
            }
            self.__db_status = CSISCurrent(id=1, **sql_labels)
            self.session.add(self.__db_status)
            self.session.commit()
        else:
            self.__db_status = db_status

    @property
    def status_list(self):
        return self.__status_list

    @status_list.setter
    def status_list(self, status_list):
        if status_list is None:
            self.__status_list = self.status_from_file()
        else:
            self.__status_list = status_list

    def status_from_file(self):
        """
        Pull status data from the file, and return a dict whose key is the data title,
        and whose value is converted to int, or float, or string in that order, based
        on convertability
        :return: status list
        """
        data_path = self.data_path + '/Status.txt'
        with open(data_path, 'r') as file_data:
            reader = csv.reader(file_data, delimiter=':')
            status_dict = {}
            status_dict['batch_id'] = next(reader)[1][1:]
            for data in reader:
                try:
                    status_dict[data[0]] = int(data[1])
                except ValueError:
                    try:
                        status_dict[data[0]] = float(data[1])
                    except ValueError:
                        status_dict[data[0]] = data[1]
            return status_dict

    def compare_status_w_file(self):
        file = self.status_from_file()
        if file == self.status_list:
            return True
        else:
            self.status_list = file
            return False

    def update_db_status(self):
        self.db_status.batch_id = self.status_list['batch_id']
        self.db_status.total = self.status_list['Total']
        self.db_status.passed = self.status_list['Pass']
        self.db_status.failed = self.status_list['Fail']
        self.db_status.failed_od = self.status_list['Fail OD']
        self.db_status.backwards = self.status_list['Backwards']
        self.db_status.n_a = self.status_list['N/A']
        self.db_status.lost_homing = self.status_list[' Lost to Homing']
        self.db_status.batch_homes = self.status_list['Batch Homes']
        self.session.add(self.db_status)
        self.session.commit()

    def reset_session(self):
        self.session.close()
        self.session = Session()

if __name__ == '__main__':
    CSISStatus().update_db_status()


import json

from django.core.paginator import Paginator
from django.db import transaction
from django.shortcuts import render, HttpResponse

from django.views.decorators.csrf import csrf_exempt
from index.models import *

def get_all(request):
    rows = request.GET.get('rows', 2)
    page = request.GET.get('page', 1)
    st_list = list(Rotation.objects.all().order_by('id'))
    paginator = Paginator(st_list, int(rows))
    try:
        rows = list(paginator.page(page).object_list)
    except Exception as tips:
        print(tips)
        rows = list(paginator.page(int(page) - 1).object_list)
        page = int(page) - 1

    page_data = {
        'page': page,
        'total': paginator.num_pages,
        'records': paginator.count,
        'rows': rows
    }

    def mydefault(u):
        if isinstance(u, Rotation):
            return {
                'id': u.id,
                'desc': u.title,
                'date': u.publish_time.strftime("%Y-%m-%d %H:%M:%S"),
                'status': u.status,
                'img_url': str(u.img_url),
            }

    data = json.dumps(page_data, default=mydefault)
    return HttpResponse(data)

@csrf_exempt
def edit(request):
    oper = request.POST.get('oper')
    desc = request.POST.get('desc')
    id = request.POST.get('id')
    status = request.POST.get('status')
    if oper == 'edit':
        with transaction.atomic():
            car = Rotation.objects.get(id=id)
            car.status = True if status == '1' else False
            car.title = desc
            car.save()
    elif oper == 'del':
        with transaction.atomic():
            Rotation.objects.get(id=id).delete()
    return HttpResponse()

@csrf_exempt
def add(request):
    title = request.POST.get('title')
    status = True if request.POST.get('status') == '1' else False
    pic = request.FILES.get('pic')
    Rotation.objects.create(title=title, status=status, img_url=pic)
    return HttpResponse()


from common.model.base_enum import BaseEnum
from common.util.enum_utils import ChoiceEnum

class PersonTypeEnum(BaseEnum):
    system_admin = 1
    hamyar = 2
    madadkar = 3
    madadju = 4

class AccountStatusEnum(ChoiceEnum):
    active = 1
    deactive = 2

class AdminRateEnum(ChoiceEnum):
    low = 1
    medium = 2
    high = 3

class SupportStatusEnum(ChoiceEnum):
    none = 1
    ok = 2

class StudyingStatusEnum(ChoiceEnum):
    low = 1
    medium = 2
    high = 3

class HousingStatusEnum(ChoiceEnum):
    low = 1
    medium = 2
    high = 3

class HamyarPeriodEnum(ChoiceEnum):
    month = 2
    day = 1
    year = 3

class GenderEnum(ChoiceEnum):
    male = 1
    female = 2


while True:
    print ("Enter '0' for exit")
    ch = input("Enter the charcter to check:\n")
    if ch == '0':
        break
    else:
        if ((ch >= 'a' and ch <= 'z' )or (ch >= 'A' and ch <= 'Z')):
            print (ch,"is a alphabet")
        else:
            print (ch,"is not alphabet")


from app import db, helper
from .models.customer import Customer
from .models.video import Video
from flask import request, Blueprint, make_response, jsonify, Response
from sqlalchemy import desc, asc
from datetime import date
import os
import requests
import json

#WAVE 1 CRUD /CUSTOMERS

customer_bp = Blueprint("customers", __name__, url_prefix="/customers")

# GET all customers
@customer_bp.route("", methods=["GET"], strict_slashes=False)
def get_customers():

    customers = Customer.query.all()

    customer_response =[]
    for customer in customers:
        customer_response.append(customer.details_of_customer_response())
    return jsonify(customer_response), 200

#GET customers with specific ID
@customer_bp.route("/<customer_id>", methods=["GET"], strict_slashes=False)
def get_specific_customers(customer_id):

    if not helper.is_int(customer_id):
        return {
            "message": "id must be an integer",
            "success": False
        },400

    customer =  Customer.query.get(customer_id)

    if customer == None:
        return Response ("" , status=404)

    if customer:
        return make_response(customer.details_of_customer_response(), 200)

#POST /customers details
@customer_bp.route("", methods=["POST"], strict_slashes=False)
def add_customers():

    request_body = request.get_json()

    if ("name" not in request_body or
        "postal_code" not in request_body or
        "phone" not in request_body):

        return jsonify(details="Invalid data"),400

    new_customer = Customer(name=request_body["name"],
                            postal_code=request_body["postal_code"],
                            phone=request_body["phone"])

    db.session.add(new_customer)
    db.session.commit()

    return jsonify(id=new_customer.customer_id) ,201

#PUT update a customer detail
@customer_bp.route("<customer_id>", methods=["PUT"], strict_slashes=False)
def update_customer(customer_id):

    customer = Customer.query.get(customer_id)

    if customer == None or not customer:
        return Response("", 404)

    form_data = request.get_json()

    if (form_data != None and "name" in form_data.keys() and "postal_code" in form_data and "phone" in form_data):

        customer.name = form_data["name"]
        customer.postal_code = form_data["postal_code"]
        customer.phone = form_data["phone"]

        db.session.commit()

        return customer.details_of_customer_response(), 200

    return jsonify(""), 400

#DELETE a customer
@customer_bp.route("/<customer_id>", methods=["DELETE"], strict_slashes=False)
def delete_customer(customer_id):

    customer = Customer.query.get(customer_id)

    if customer == None:
        return Response("", status=404)

    if customer:
        db.session.delete(customer)
        db.session.commit()

        return jsonify(id=int(customer_id)), 200

#WAVE 2 - GET /customers/<id>/rentals
@customer_bp.route("/<customer_id>/rentals", methods=["GET"], strict_slashes=False)
def get_videos_checked_out(customer_id):
    if not helper.is_int(customer_id):
        return {
            "message": "id must be an integer",
            "success": False
        },400

    customer = Customer.query.get(customer_id)

    if not customer or customer == None:
        return jsonify(""), 404

    if customer:
        rental_list = []
        for rental in customer.rentals:
            video = Video.query.get(rental.video_id)
            if not video or video == None:
                return jsonify(details = ""), 404
            else:
                rental_list.append({"release_date" : video.release_date,
                "title" : video.title,
                "due_date" : rental.due_date
                })

        return jsonify(rental_list)

#OPTIONAL ENHANCEMENTs
#GET /customers/<id>/history


import threading as th
import time
import keyboard

import asyncio
import datetime
import random
import websockets

import cbpro
import sys

cache = []
class MyWebsocketClient(cbpro.WebsocketClient):
    def on_open(self):
        self.url = "wss://ws-feed-public.sandbox.pro.coinbase.com"
        self.products = ["BTC-USD"]
        self.message_count = 0
        self.channels = ["ticker"]
        print("Lets count the messages!")

    def on_message(self, msg):
        self.message_count += 1
        if 'price' in msg and 'type' in msg:
            print ("Message type:", msg["type"],
                "\t@ {:.3f}".format(float(msg["price"])))
        cache.append(msg)
        if len(cache) > 100:
            cache.pop(-1)
            print("removing one... %s" % len(cache))

    def on_close(self):
        print("-- Goodbye! --")

wsClient = MyWebsocketClient()
wsClient.start()

loop = asyncio.get_event_loop()

keep_going = True
def key_capture_thread():
    global keep_going
    a = keyboard.read_key()
    if a== "esc":
        keep_going = False

async def do_stuff():
    th.Thread(target=key_capture_thread, args=(), name='key_capture_thread', daemon=True).start()
    while keep_going:
        if len(cache) > 0:
            msg = cache.pop(0)
            await websocket.send(msg)
        else:
            await asyncio.sleep(random.random() * 3)
    print ("End")
    wsClient.close()
    loop.close()
    sys.exit()

start_server = websockets.serve(do_stuff, "127.0.0.1", 5678)

loop.run_until_complete(start_server)
# loop.run_forever()


from PIL import Image
from PIL import ImageOps
import numpy as np
from scipy import ndimage

class PicEditor:
    #returns all pictures that might contain a digit -> ALL images in the Image...
    def getAll(self, original):
        img = original.copy() # we don't change the original, please.
        #Let's darken it again, but make the "background" white
        px = img.load()
        for i in range(img.size[0]):
            for j in range(img.size[1]):
                if (px[i,j]) < 225:
                    px[i,j] = int(px[i,j] - 0.5*px[i,j])
                else:
                    px[i,j] = 255
        img = self.removeWhites(img)

        splitted, right, left = self.split(img)
        if not splitted:
            img = self.removeWhites(img)
            img = self.resize(img)
            img = self.recenter(img)
            #Now we can recontrast it  (for real)
            img = self.recolor(img)
            img.show()
            #But we have to do the other things again.
            img = self.removeWhites(img) #Height-diff, again (maybe)
            img = self.resize(img)
            img = self.recenter(img)
            return (False, [img]) #Let's pretend there is a need for a list - Otherwise we have to recode other things...

        img = left
        img = self.resize(img)
        recentered = self.recenter(img)
        images = [recentered]

        while splitted:
            splitted, right, left = self.split(right)

            if splitted:
                img = left
                img = self.removeWhites(img)
                img = self.resize(img)
                img = self.recenter(img)
                #Now we can recontrast it  (for real)
                img = self.recolor(left)
                #But we have to do the other things again.
                img = self.removeWhites(img) #Height-diff, again (maybe)
                img = self.resize(img)
                img = self.recenter(img)
                images.append(img)
            else:
                #nothing was splitted, so it's the last one.
                right = self.removeWhites(right)
                right = self.resize(right)
                right = self.recenter(right)
                images.append(right)
                return (True, images)

    #checks if there could be a digit (from left to right.)
    def split(self, img):
        img = self.removeWhites(img)
        px = np.array(img)
        found = False
        for x in range(len(px[0])):
            if (min(px[:,x]) == 255):
                found = True
                px2 = np.delete(px,[x for x in range(0, x)],1)
                px = np.delete(px,[x for x in range(x-1, len(px[0])-1)],1)
                break
        if (found):
            img = Image.fromarray(px)
            next = Image.fromarray(px2)
            return (True, next, img)
        else:
            return (False, img, None)

    def recolor(self, img):
        """
        px = img.load()
        for i in range(img.size[0]):
            for j in range(img.size[1]):
                if (px[i,j]) < 225:
                    px[i,j] = int(px[i,j] - 0.5*px[i,j])
                else:
                    px[i,j] = 255
        """
        ImageOps.autocontrast(img)
        return img

    def removeWhites(self, img):
        px = np.array(img)
        #left:
        while min(px[:,0]) == 255:
            px = np.delete(px,0,1)
        #right:
        while min(px[:,-1]) == 255:
            px = np.delete(px,-1,1)
        #top:
        while min(px[0]) == 255:
            px = px[1:]
        #bottom:
        while min(px[-1]) == 255:
            px = px[:-1]
        img = Image.fromarray(px)
        return img

    def resize(self, img):
        width, height = img.size
        #Exactly the same sizes? - NICE!
        if (width == height):
            img = img.resize((20,20), resample=Image.BICUBIC)
        elif (width < height):
            ratio = width / height
            newWidth = round(20*ratio)
            if (newWidth == 0): #Could happen. Somehow...
                newWidth = 1
            img = img.resize((newWidth,20), resample=Image.BICUBIC)
        else:
            ratio = height / width
            newHeight = round(20*ratio)
            if newHeight == 0:
                newHeight = 1
            img = img.resize((20, newHeight), resample=Image.BICUBIC)
        return img

    def recenter(self, img):
        px = img.load()
        newImg = Image.new("L", (28,28), "white") #We will put "our" picture into this bad boy (later)
        pixels = newImg.load()
        cy, cx = ndimage.measurements.center_of_mass(np.array(img))

        cy,cx = round(cy), round(cx)

        for i in range(img.size[0]):
            for j in range(img.size[1]):
                if (i+14-cx >= 0) and (j+14-cy >= 0) and (i+14-cx < newImg.size[0]) and (j+14-cy < newImg.size[1]):
                    pixels[i+14-cx,j+14-cy] = px[i,j]
        img = newImg
        return img


"""TO BE EDITED.

TO BE EDITED.

Available defs:
- f: TO BE EDITED.
"""
import matplotlib as mpl
mpl.use('TkAgg')

from pymc3 import *
import numpy as np
import matplotlib.pyplot as plt

size = 200
true_intercept = 1
true_slope = 2

x = np.linspace(0, 1, size)
# y = a + b*x
true_regression_line = true_intercept + true_slope * x
# add noise
y = true_regression_line + np.random.normal(scale=.5, size=size)

data = dict(x=x, y=y)

fig = plt.figure(figsize=(7, 7))
ax = fig.add_subplot(111, xlabel='x', ylabel='y', title='Generated data and underlying model')
ax.plot(x, y, 'x', label='sampled data')
ax.plot(x, true_regression_line, label='true regression line', lw=2.)
plt.legend(loc=0)

with Model() as model: # model specifications in PyMC3 are wrapped in a with-statement
    # Define priors
    sigma = HalfCauchy('sigma', beta=10, testval=1.)
    intercept = Normal('Intercept', 0, sd=20)
    x_coeff = Normal('x', 0, sd=20)

    # Define likelihood
    likelihood = Normal('y', mu=intercept + x_coeff * x,
                        sd=sigma, observed=y)

    # Inference!
    start = find_MAP() # Find starting value by optimization
    step = NUTS(scaling=start) # Instantiate MCMC sampling algorithm
    trace = sample(2000, step, start=start, progressbar=True) # draw 2000 posterior samples using NUTS sampling

plt.figure(figsize=(7, 7))
traceplot(trace[100:])
plt.tight_layout()

plt.figure(figsize=(7, 7))
plt.plot(x, y, 'x', label='data')
glm.plot_posterior_predictive(trace, samples=100,
                              label='posterior predictive regression lines')
plt.plot(x, true_regression_line, label='true regression line', lw=3., c='y')

plt.title('Posterior predictive regression lines')
plt.legend(loc=0)
plt.xlabel('x')
plt.ylabel('y')

def main():
    print("Started!")

    # Load data

    # Create new features

    # Write out data
    print("Finished!")

if __name__ == '__main__':
    main()


#!/usr/bin/env python3

import argparse
import numpy as np
import os
import pickle
import sys
from tqdm import tqdm

def transform_batch_to_single(dir_layers, layer_no, split_str=None):
    """
    Load layer files and transforms them to single
    pickle
    """
    dir_layers = dir_layers+'/layer_{layer_no}'.format(layer_no=layer_no)

    pickle_files = os.listdir(dir_layers)

    if split_str is None:
        split_str = 'batch_'.format(layer_no=layer_no)

    files = [(x, int(x.split(split_str)[1].split('.pickle')[0])) for x in pickle_files]

    sorted_files = [x[0] for x in sorted(files, key=lambda x: x[1])]
    print(len(files), len(sorted_files))
    final_array = []

    for b, batch_name in enumerate(tqdm(sorted_files)):
        batch_file = os.path.join(dir_layers, batch_name)
        with open(batch_file, 'rb') as f:
            pickle_data = pickle.load(f)
            if b is 0:
                final_array = pickle_data
            final_array = np.vstack((final_array,pickle_data))

    with open('{dir_layers}/all.pickle'.format(dir_layers=dir_layers), 'wb') as ph:
        pickle.dump(final_array, ph)
    return

if __name__ == '__main__':
    parser = argparse.ArgumentParser('Transform Pickles into Single Batch')
    g1 = parser.add_argument_group('computation parameters')
    g1.add_argument('--layer_no', type=int, default=0)
    g1.add_argument('--dir', default='pickle_data')

    args = parser.parse_args()

    transform_batch_to_single(args.dir, args.layer_no)
    print("Batch Transform Complete Layer - {layer}".format(layer=args.layer_no))


# -*- coding: utf-8 -*-
"""
Created on Sat Feb 15 09:08:56 2020

@author: Horace Yang
"""

import pandas as pd
import matplotlib.pyplot as plt
from PyQt5 import QtWidgets, uic
from numpy import corrcoef
import os
import sys

# In[1]
fdir = 'dataset/'
fname = 'exp1.xlsx'

sfile = pd.ExcelFile(fdir+fname)
sfile.sheet_names
df = sfile.parse(sfile.sheet_names[2])  # sheet name
# plt.plot(df.iloc[:,0])
# plt.show()f = sfile.parse('第1顆2500_40_51')  # sheet name

# In[2]
# https://pythonspot.com/pyqt5-matplotlib/
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib
matplotlib.use("Qt5Agg")

# find  matplotlib default font
# https://medium.com/marketingdatascience/解決python-3-matplotlib與seaborn視覺化套件中文顯示問題-f7b3773a889b
from matplotlib.font_manager import FontProperties

class PlotCanvas(FigureCanvas):
    def __init__(self, parent=None, width=10.5, height=8.3, dpi=100):
        #第一步：建立一個建立Figure
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        #第二步：在父類中啟用Figure視窗
        super(PlotCanvas,self).__init__(self.fig)
        #第三步：建立一個子圖，用於繪製圖形用，111表示子圖編號，如matlab的subplot(1,1,1)
        self.axes1 = self.fig.add_subplot(211)
        self.axes2 = self.fig.add_subplot(212)

        self.applyfont = FontProperties(fname=r'C:\Anaconda3\Lib\site-packages\matplotlib\mpl-data\fonts\ttf\微软正黑体.ttf')

        FigureCanvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def plot(self, data1, title1, data2, title2):
        self.axes1.cla()  #  which clears data and axes
        self.axes1.plot(data1, 'b-')
        self.axes1.set_title(title1, fontproperties=self.applyfont)

        self.axes2.cla()  #  which clears data and axes
        self.axes2.plot(data2, 'b-')
        self.axes2.set_title(title2, fontproperties=self.applyfont)

        self.draw()

# In[3]
path = os.getcwd()
guiFile = 'ExploreData(v1).ui'
MainWindow, QtBaseClass = uic.loadUiType(guiFile)

class MainUi(QtWidgets.QMainWindow, MainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        QtWidgets.QMainWindow.__init__(self)  # initial mainform
        MainWindow.__init__(self)
        self.setupUi(self)
        self.options = QtWidgets.QFileDialog.Options()
        self.options |= QtWidgets.QFileDialog.DontUseNativeDialog

        self.btnOverview.clicked.connect(self.overview)   # connect button
        self.btnNextSheet1.clicked.connect(self.nextSheet1)   # connecbutton
        self.btnPrevSheet1.clicked.connect(self.prevSheet1)   # connect button
        self.btnNextSheet2.clicked.connect(self.nextSheet2)   # connecbutton
        self.btnPrevSheet2.clicked.connect(self.prevSheet2)   # connect button
        self.btnNextCol1.clicked.connect(self.nextCol1)   # connect button
        self.btnPrevCol1.clicked.connect(self.prevCol1)   # connect button
        self.btnNextCol2.clicked.connect(self.nextCol2)   # connect button
        self.btnPrevCol2.clicked.connect(self.prevCol2)   # connect button

        self.plotchart = PlotCanvas(self,)
        graphicscene = QtWidgets.QGraphicsScene()  #建立一個場景
        graphicscene.addWidget(self.plotchart)  #將圖形元素新增到場景中
        self.graphicsView.setScene(graphicscene)  #將建立新增到圖形檢視顯示視窗
        self.graphicsView.show()

    def overview(self): # when press button
        fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*)", options=self.options)
        if fileName:
            self.edSourceName.setText(fileName)
            self.sfile = pd.ExcelFile(fileName)
            self.sheetPt1=0  # default sheet1 no.
            self.sheetPt2=0  # default sheet2 no.
            self.colPt1=0    # default column1 no.
            self.colPt2=0    # default column2 no.
            self.df1 = pd.DataFrame()  # new dataframe1
            self.df2 = pd.DataFrame()  # new dataframe2
        self.RefreshForm()

    def nextSheet1(self):
        if self.sheetPt1 < len(self.sfile.sheet_names):
            self.sheetPt1 = self.sheetPt1 + 1
        self.RefreshForm()

    def prevSheet1(self):
        if self.sheetPt1 > 0:
            self.sheetPt1 = self.sheetPt1 - 1
        self.RefreshForm()

    def nextSheet2(self):
        if self.sheetPt2 < len(self.sfile.sheet_names):
            self.sheetPt2 = self.sheetPt2 + 1
        self.RefreshForm()

    def prevSheet2(self):
        if self.sheetPt2 > 0:
            self.sheetPt2 = self.sheetPt2 - 1
        self.RefreshForm()

    def RefreshForm(self):
        sn1 = self.sfile.sheet_names[self.sheetPt1]
        self.edSheetName1.setText(sn1)
        self.df1 = self.sfile.parse(sn1)  # sheet name
        sample1 = self.df1.iloc[1:,self.colPt1]
        title1 = sn1 + ': ' + str(self.df1.columns[self.colPt1])
        self.edFieldName1.setText(str(self.colPt1)+': '+str(self.df1.columns[self.colPt1]))
        self.edFieldData1.setText(sample1.to_string())

        sn2 = self.sfile.sheet_names[self.sheetPt2]
        self.edSheetName2.setText(sn2)
        self.df2 = self.sfile.parse(sn2)  # sheet name
        sample2 = self.df2.iloc[1:,self.colPt2]
        title2 = sn2 + ': ' + str(self.df2.columns[self.colPt2])
        self.edFieldName2.setText(str(self.colPt2)+': '+str(self.df2.columns[self.colPt2]))
        self.edFieldData2.setText(sample2.to_string())

        minlen = min(len(sample1),len(sample2))
        self.edCorr.setText(str(corrcoef(sample1[:minlen-1], sample2[:minlen-1])[1,0])[:5])
        self.plotchart.plot(sample1, title1, sample2, title2)

    def nextCol1(self):
        if self.colPt1 < self.df1.shape[1]:
            self.colPt1 = self.colPt1 + 1
        self.RefreshForm()

    def prevCol1(self):
        if self.colPt1 > 0:
            self.colPt1 = self.colPt1 - 1
        self.RefreshForm()

    def nextCol2(self):
        if self.colPt2 < self.df2.shape[1]:
            self.colPt2 = self.colPt2 + 1
        self.RefreshForm()

    def prevCol2(self):
        if self.colPt2 > 0:
            self.colPt2 = self.colPt2 - 1
        self.RefreshForm()

# In[]
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    Form = MainUi()

    Form.show()
    sys.exit(app.exec_())


#import sys
cse=int(input("Enter the no of students placed in CSE:"))
ece=int(input("Enter the no of students placed in ECE:"))
mech=int(input("Enter the no of students placed in MECH:"))
if cse==ece and ece==mech:
    print("None of the departments got highest placement")
elif cse<0 or ece<0 or mech<0:
    print("Invalid Input")

else:

    print("Highest Placement")
    m=max(cse,ece,mech)
    if m==cse:
        print("CSE")
    if m==ece:
        print("ECE")
    if m==mech:
        print("MECH")


# Imports

import pandas as pd
import numpy as np

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import ENGLISH_STOP_WORDS

import ast

from collections import Counter

# Spacy
import spacy
from spacy.attrs import LOWER, POS, ENT_TYPE, IS_ALPHA
from spacy.tokens import Doc

import en_core_web_sm

# Define cvlass
class MCW():

    def __init__(self, output_stem = '', text_col = 'contents', output_number = 100, max_word_phrases = 3,
                 additional_stop_words = [],
                 pos_dict = {'nouns' : 'NOUN','adjectives' : 'ADJ','verbs' : 'VERB',
                               'adverbs' : 'ADV'}):

        # Set attributes
        self.output_stem = output_stem
        self.text_col = text_col
        self.output_number = output_number
        self.max_word_phrases = max_word_phrases
        self.additional_stop_words = additional_stop_words
        self.pos_dict = pos_dict
        self.nlp = en_core_web_sm.load()

    def token_pos_grabber(self, doc, part_of_speech):

        indexes = []

        for index,token in enumerate(doc):
            if token.pos_ == part_of_speech:
                indexes.append(index)

        doc2 = Doc(doc.vocab, words=[t.text for i, t in enumerate(doc) if i in indexes])

        doc3 = ' '.join(token.string for token in doc2)

        return doc3

    def fit_transform(self, df, nlp_transform = True):

        # Now get dataframe
        self.df = df

        #check
        if nlp_transform == True:

            # Now tokenize
            self.df['tokenized'] = self.df[self.text_col].map(lambda x: self.nlp(x))

            # Now loop through and populate the df with new columns
            for key in self.pos_dict:

                # Add column
                self.df[key] = self.df.tokenized.map(lambda x: self.token_pos_grabber(x, self.pos_dict[key]))

    # Functions to get word combinations
    def most_common_words(self, text_column, word_range):

        # stop words
        add_stop_words = ENGLISH_STOP_WORDS.union(self.additional_stop_words)

        # Fill with balnk spaces
        self.df[text_column] = self.df[text_column].fillna('')

        X = self.df[text_column].values.astype(str)

        vect = CountVectorizer(stop_words=add_stop_words, ngram_range= (word_range,word_range))
        X = vect.fit_transform(X)

        word_counts = list(zip(vect.get_feature_names(),np.asarray(X.sum(axis=0)).ravel()))

        columns = ['word', 'count']
        word_counts = pd.DataFrame(word_counts, columns=columns)

        word_counts = word_counts.sort_values(by='count', ascending=False)

        return word_counts

    def phrase_counter(self, col, word_range):

        '''
        This method takes in the column to parse and the word range.

        You can also set the additional stop words'''

        # Change keyword argument
        stop_status = ENGLISH_STOP_WORDS.union(self.additional_stop_words)

        # Initiate the vectoriser

        # Use kwargs min_word_range, max_word_range and reference stop words
        vect = CountVectorizer(ngram_range=(word_range, word_range), stop_words=stop_status)

        # Fill with balnk spaces
        self.df[col] = self.df[col].fillna('')

        # Add in space before join
        self.df[col] = self.df[col] + ' '

        # Just into one big string
        summaries = ''.join(self.df[col])
        ngram_summaries = vect.build_analyzer()(summaries)

        # Now generate output - use output number here
        count_list = Counter(ngram_summaries).most_common(self.output_number)

        wordcount_dict = {'word' : [],
                         'count' : []}

        # Loop through the count_list
        for element in count_list:

            # Append the phrases and counts to dict
            wordcount_dict['word'].append(element[0])
            wordcount_dict['count'].append(element[1])

        # Output a dataframe
        count_df = pd.DataFrame(wordcount_dict)

        # Just so look pretty
        count_df = count_df[['word', 'count']]

        return count_df

    # Now a method to actually make the word counts
    def make_word_counts(self):

        '''
        This method will actually build the word counts for each column and word count
        '''

        self.output_dict = {}

        # Make columns to parse and check they are there
        self.cols_to_parse = [self.text_col] + [c for c in self.df.columns if c in list(self.pos_dict.keys())]

        # loop through
        for col in self.cols_to_parse:

            # make a new level
            self.output_dict[col] = {}

            # Now get the various word counts
            for i in range(1, self.max_word_phrases + 1):

                # check output number
                if self.output_number == None:

                    # Populate dictionary
                    self.output_dict[col][i] = self.most_common_words(col, i)

                else:

                    # Populate dictionary
                    self.output_dict[col][i] = self.phrase_counter(col, i)

    def show_word_count_table(self, col, count_number):
        '''
        Displays the word count table for each word type and count combnination'''

        return self.output_dict[col][count_number]

    def update_stop_words(self, extra_stop_words, method = 'append'):

        # Condition
        if method == 'append':

            self.additional_stop_words = self.additional_stop_words + extra_stop_words

        else:

            self.additional_stop_words = extra_stop_words

    def export_to_excel(self, filename):

        '''
        This method will output to excel in standard format.

        It will output each word type to a separate sheet'''

        writer = pd.ExcelWriter(self.output_stem + filename)

        for key in self.output_dict.keys():

            # Now next level
            for word_count in self.output_dict[key].keys():

                # Save to a sheet
                self.output_dict[key][word_count].to_excel(writer, key, index=False, startcol = word_count * 3)

        writer.save()


import os
import socket
import http.client

def connect_http(address):
    client = http.client.HTTPConnection(address)
    client.request('GET', '/')

    raw_reply = client.getresponse().read()

    print(raw_reply)
    with open('neki_fajl.html', 'w') as file:
        file.write(raw_reply.decode('utf-8'))

def kek(address):
    sock = socket.socket()
    sock.connect((address, 80))
    request = "GET / \r\n\r\n"
    sock.sendall(request.encode('ascii'))
    raw_reply = b''

    while True:
        more = sock.recv(4096)
        if not more:
            break
        raw_reply += more

    print(raw_reply.decode('utf-8'))

if __name__ == "__main__":
    kek('192.168.1.1')
    # connect_http('192.168.1.1')


### TODO ###
# Normalize mouse vector so it uses the whole sketch
# Correctly write motor vector angle (° and fix where the front is)
# Visualise the ritation arc in the middle of the chassis

from math import sqrt

w, h, rotation = 100, 100, 0

def setup():
    # Default settings
    size(800, 600)
    ellipseMode(CENTER)
    textAlign(CENTER, CENTER)

def draw():
    global w,h,rotation

    background(255)

    # Scale and translate the coordinate system so it's like regular functions
    # Great resource: https://processing.org/tutorials/drawing/
    scale(1, -1)
    translate(width/2, -height/2)

    # The position of the mouse in the translated coordinate system
    mouse = (mouseX - width / 2, -mouseY + height / 2)

    # If the mouse is pressed, adjust w and h
    if mousePressed and mouseButton == LEFT:
        w = abs(mouse[0]) * 2
        h = abs(mouse[1]) * 2

        # Set size limit for the chassis
        if w < 100:
            w = 100
        if h < 100:
            h = 100

    # Normalize the mouse vector
    normalizedMouse = (float(mouse[0]) / (width/2), float(mouse[1]) / (height/2))

    # The vectors of the motors
    vectors = swerve_drive(normalizedMouse[0], normalizedMouse[1], rotation, w, h)

    draw_chassis(w, h) # Draw the chassis of the robot
    draw_motor_vectors(vectors, w, h) # Draw the vectors of the motor
    draw_vector(0, 0, normalizedMouse) # Draw the normalized vector from [0, 0] to mouse
    draw_motors(vectors, w, h) # Draw the motors

def draw_motors(vectors, w, h):
    """Draws each of the motors."""
    draw_motor(w / 2, h / 2, vectors[0])
    draw_motor(-w / 2, h / 2, vectors[1])
    draw_motor(-w / 2, -h / 2, vectors[2])
    draw_motor(w / 2, -h / 2, vectors[3])

def draw_motor_vectors(vectors, w, h):
    """Draws each of the motor vectors."""
    draw_vector(w / 2, h / 2, vectors[0])
    draw_vector(-w / 2, h / 2, vectors[1])
    draw_vector(-w / 2, -h / 2, vectors[2])
    draw_vector(w / 2, -h / 2, vectors[3])

def draw_vector(x1, y1, vector, vector_scalar = 50, draw_distance = True):
    """Draws a vector of certain size at specified coordinates."""
    # Coordinates of the end of the vector
    x2 = x1 + vector[0] * vector_scalar
    y2 = y1 + vector[1] * vector_scalar

    # Draw the arrow line
    strokeWeight(2.5)
    line(x1, y1, x2, y2)

    # Draw the triangle at the end of the arrow
    pushMatrix();
    translate(x2, y2);
    rotate(atan2(vector[1], vector[0]));
    triangle(0, 0, -8, 4, -8, -4);
    popMatrix();

    # Also (by default) draws the distance and the angle
    fill(0)
    textAlign(LEFT, CENTER)
    drawText('{0:.2f}'.format(sqrt(vector[0]**2 + vector[1]**2)), x2 + 10, y2 + 8)
    drawText('{0:.2f}'.format(degrees(atan2(vector[1], vector[0])))+" deg", x2 + 10, y2 - 8)

def draw_motor(x1, y1, vector):
    """Draws a motor at specified coordinates."""
    pushMatrix()
    translate(x1, y1)
    noFill()
    strokeWeight(2.5)
    rotate(atan2(vector[1], vector[0]))
    rectMode(CENTER)
    rect(0, 0, 40, 20)
    popMatrix()

def draw_chassis(w, h):
    """Draw the chasis of the robot."""
    rectMode(CENTER)

    noFill()
    stroke(0)
    strokeWeight(2.5)
    rect(0, 0, w, h) # The actual chassis

    strokeWeight(1)
    line(-w/2, -h/2, w/2, h/2) # A diagonal
    line(-w/2, 0, w/2, 0) # A horizontal
    line(0, -h/2, 0, h/2) # A vertical

    textAlign(CENTER, CENTER)
    drawText("Width: "+str(w), 0, h / 2 + 15)
    drawText("Height: "+str(h), w / 2 + 15, 0, PI/2)

def swerve_drive(x, y, rotate, w, h):
    # Normalize the w/h ratio
    chasis_diagonal = sqrt(w * w + h * h)
    h /= chasis_diagonal
    w /= chasis_diagonal

    # Variables used in the calculation
    var = [x - rotate * h, x + rotate * h, y - rotate * w, y + rotate * w]

    # Speeds of the motors (top right, top left, bottom left, bottom right)
    motor_speeds = [
        sqrt(var[1] * var[1] + var[2] * var[2]),
        sqrt(var[1] * var[1] + var[3] * var[3]),
        sqrt(var[0] * var[0] + var[3] * var[3]),
        sqrt(var[0] * var[0] + var[2] * var[2])
    ]

    # Normalize the speeds of the motors
    max_motor_speed = max(motor_speeds)
    if (max_motor_speed > 1):
        for i in range(len(motor_speeds)):
            var[i] /= max_motor_speed

    # Return the x and y components of the vectors of the motors
    return [
        (var[1], var[2]),
        (var[1], var[3]),
        (var[0], var[3]),
        (var[0], var[2])
    ]

def mouseWheel(event):
    """Adjusts the rotation according to the mouse wheel."""
    global rotation
    # Upwards spin increases the angle, downwards spin decreases it
    if event.getCount() == 1:
        rotation += PI / 45
    else:
        rotation -= PI / 45

    # Limits the rotation from -PI to PI
    if rotation > PI:
        rotation = PI
    elif rotation < -PI:
        rotation = -PI

def drawText(string, x, y, rotateBy=0):
    """Draw inverted text (normal for us, since we changed the coordinate system)."""
    pushMatrix()
    translate(x, y)
    scale(1, -1)
    rotate(rotateBy)
    text(string, 0, 0)
    popMatrix()


import json

__author__ = 'Gerryflap'
import socket
try:
    import _thread
except Exception:
    import thread as _thread
import time

def connect(ip, port):
    mySocket = socket.create_connection((ip, port))
    return Connection(mySocket)

class Connection(object):
    def __init__(self, sock):
        self.sock = sock
        self.ports = {}
        self.port = 0
        _thread.start_new_thread(self.listen, tuple())

    def addConnection(self):
        lastPort = self.port
        print("Sending a port request over socket %s"%(self.sock))
        self.sock.send(bytes(json.dumps({"type": "REQUEST"}), 'UTF-8'))
        print("Sent a port request over socket %s"%(self.sock))
        while self.port == lastPort:
            time.sleep(0.01)
        newPortConnection = PortConnection(self, self.port)
        self.ports[str(self.port)] = newPortConnection
        return (newPortConnection, self.port)

    def listen(self):
        print("Started listening")
        while True:
            recieved = self.sock.recv(4096)
            if(len(recieved) != 0):
                try:
                    recieved = json.loads(recieved.decode("utf-8"))
                    if "type" in recieved:
                        self.handleProtocol(recieved)
                except ValueError:
                    pass

    def handleProtocol(self, data):
        type = data["type"]

        if type == "DATA":
            if data["port"] in self.ports:
                self.ports[data["port"]].onRecv(data)
        elif type == "REQUEST_OK":
            self.port = data["port"]
            print("New port opened on %s"%self.port)

    def closeConnection(self, port):
        del self.ports[port]

    def send(self, data):
        self.sock.send(data)

class PortConnection(object):
    def __init__(self, connection, port):
        self.connection = connection
        self.port = port
        self.listener = 0

    def onRecv(self, data):
        if self.listener != 0:
            try:
                try:
                    self.listener.onRecv(data["data"])
                except ValueError as e:
                    print(e)
            except AttributeError as e:
                print("Error: Please add a onRecv(self, data) method to your object!")
        else:
            print("Recieved %s, but no listener has been added"%data)

    def send(self, port, data):
        self.connection.send(bytes(json.dumps({"type": "DATA", "port": port, "data": data}), 'UTF-8'))

    def setListener(self, object):
        print("Added listener: %s"%object)
        self.listener = object


#-*-coding:utf-8-*-
from enum import Enum
LOGIN = 1000

OPEN_SIMULATOR = 1001
CLOSE_SIMULATOR = 1003
UPLOAD_LOG = 1005
REPORT_RES_RATE = 1007
MYSQL_HOST = "192.168.8.203"
MYSQL_DBNAME = ""
class ClientType(Enum):
    controler = 0
    performer = 1

# 协议说明
# 通知控制客户端打开压测工具
# {"command_id":1001,"room_list":["107894","107916"], "username_prefix":"jaxa", "start_index":0,"end_index":60,"userpwd":"000000" }
# 返回打开登录结果
{"comamnd_id":1002,"result":0}

# 通知控制客户端关闭压测工具
# {"command_id":1003}

# 通知控制客户端上传日志
{"command_id":1005}
# 控制客户端上传日志回复
{"comamnd_id":1006,"result":0}

# 上报执行机资源占用
{"command_id":1007,"cpu":75,"mem":60,"band":10}


import sys
import itertools

def primes_sieve2(limit=2**10):
    a = [True] * limit
    a[0] = a[1] = False

    for (i, isprime) in enumerate(a):
        if isprime:
            yield i
            for n in range(i*i, limit, i):
                a[n] = False

prime_tuple = tuple(primes_sieve2(limit=2**16))

def binary_generator(N):
    for middle in itertools.product('01', repeat=N-2):
        yield '1' + ''.join(middle) + '1'

def find_min_divisor(number):
    for prime in prime_tuple:
        if number != prime and number % prime == 0:
            return prime

def nonprime_generator(N):
    for binary in binary_generator(N):
        not_prime = True
        divisors = []
        for base in range(2, 11):
            number = int(binary, base=base)
            divisor = find_min_divisor(number)
            if divisor is None:
                not_prime = False
                continue
            divisors.append(divisor)

        if not_prime:
            yield (binary, divisors)

def solve(T, N, J):
    nonprimes = nonprime_generator(N)
    print('Case #{}:'.format(T))
    for i in range(J):
        nonprime, divisors = next(nonprimes)
        print('{} {}'.format(nonprime, ' '.join(map(str, divisors))))

def main():
    n_cases = int(sys.stdin.readline().strip())
    for case in range(n_cases):
        N, J = map(int, sys.stdin.readline().strip().split(' '))
        solve(case + 1, N, J)

if __name__ == '__main__':
    main()


# 给你一个包含 n 个整数的数组 nums，判断 nums 中是否存在三个元素 a，b，c ，使得 a + b + c = 0 ？请你找出所有和为 0 且不重
# 复的三元组。
#
#  注意：答案中不可以包含重复的三元组。
#
#
#
#  示例 1：
#
#
# 输入：nums = [-1,0,1,2,-1,-4]
# 输出：[[-1,-1,2],[-1,0,1]]
#
#
#  示例 2：
#
#
# 输入：nums = []
# 输出：[]
#
#
#  示例 3：
#
#
# 输入：nums = [0]
# 输出：[]
#
#
#
#
#  提示：
#
#
#  0 <= nums.length <= 3000
#  -105 <= nums[i] <= 105
#
#  Related Topics 数组 双指针
#  👍 2897 👎 0

# leetcode submit region begin(Prohibit modification and deletion)
class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        n = len(nums)
        res = []
        # order array
        nums.sort()
        # outer loop
        for k in range(n - 2):
            if nums[k] > 0:  # nums[k]>0, 意味没有解
                break
            # 如果k和k-1的元素相等，继续移动k
            if k > 0 and nums[k] == nums[k - 1]:
                k += 1
                continue
            i, j = k + 1, n - 1
            # inner loop
            while i < j:
                s = nums[k] + nums[i] + nums[j]
                if s < 0:  # i太小，移动i
                    i += 1
                    while i < j and nums[i] == nums[i - 1]:
                        i += 1
                elif s > 0:  # j太大，移动j
                    j -= 1
                    while i < j and nums[j] == nums[j + 1]:
                        j -= 1
                else:
                    res.append([nums[k], nums[i], nums[j]])
                    i += 1
                    j -= 1
                    while i < j and nums[i] == nums[i - 1]:
                        i += 1
                    while i < j and nums[j] == nums[j + 1]:
                        j -= 1

        return res

# leetcode submit region end(Prohibit modification and deletion)


tn = int(input())
for i in range(tn):
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    visit = [1] + [0] * sum(A)
    stack = []
    q = 0
    for ii, val in enumerate(A):
        qq = []
        for iii in range(q + 1):
            if visit[iii] == 1 and visit[iii + val] == 0:
                qq.append(iii + val)
                if q < iii + val:
                    q = iii + val
        for i3 in qq:
            visit[i3] = 1

    print('#{} {}'.format(i+1, visit.count(1)))

lecture/algorithm/problem/3752. 가능한 시험 점수.py


import requests
from bs4 import BeautifulSoup
import re
import random,time
from compare_url import compare_url

def get_content(url):
    headers = {
        "referer": url,
        "User-Agent": "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36"}
    try_number = 0
    while try_number < 10:
        try:
            content = requests.get(url, headers=headers, timeout=120)
            content.encoding = "utf-8"
            # print(content.text)
            time.sleep(random.randint(0, 5))
            return content.text
        except requests.exceptions.RequestException:
            try_number += 1
            content = []
            print("time out")
    if content == []:
        content = requests.get('https://www.google.com/')

    return content.text

def get_page(url):
    content = get_content(url)
    #print(content)
    soup = BeautifulSoup(content, 'html.parser')
    each_paper_url = soup.findAll("a", {'class': "visitable"})
    pattern = r'\d+'
    Vol_Iss = re.findall(pattern, str(each_paper_url[0].get('href')))
    k = ("Vol. "+str(Vol_Iss[2])+", Iss. "+str(Vol_Iss[3]))
    url = ("https://www.onlinelibrary.wiley.com" + str(each_paper_url[0].get('href')))
    #print(k)
    #print(url)
    return str(k), str(url)

def get_latest_issue_inf(url):
    content = get_content(url)
    soup = BeautifulSoup(content, 'html.parser')
    each_paper_url = soup.findAll("a", {'class': "issue-item__title visitable"})
    paper_url_list = []
    for paper in each_paper_url:
        paper_url = paper.get('href')
        paper_url_list.append("https://www.onlinelibrary.wiley.com" + str(paper_url))
    return paper_url_list

def get_abstract(url):
    content = get_content(url)
    soup = BeautifulSoup(content, 'html.parser')
    if (len(soup.findAll("meta", {'name': "citation_title"}))) > 0:
        paper_title = soup.findAll("meta", {'name': "citation_title"})[0].get('content')
    else:
        paper_title = []
    each_paper_abstract = soup.findAll("section", {'class': "article-section article-section__abstract"})
    pattern1 = r'.*<p>(.*)\n.*'
    pattern2 = r'<.*?>'
    paper_abstract = re.sub(pattern2, "", str(re.findall(pattern1, str(each_paper_abstract))), count=0).replace("['", "").replace("']", "")
    return paper_title, paper_abstract

def spider_Wiley_Small(ISS):
    url = ("https://onlinelibrary.wiley.com/loi/16136829")
    [latest_issue, latest_issue_url] = get_page(url)
    print(latest_issue_url)
    print(latest_issue)
    if latest_issue == ISS:
        url_list = []
        url_list.append(str(latest_issue_url))
        print(url_list)
    else:
        url_list = []
        url_list.append(str(latest_issue_url))
        pattern1 = r'\d+'
        iss = re.findall(pattern1, str(ISS))
        url_list.append("https://www.onlinelibrary.wiley.com/toc/16136829/" + str(2004 + int(iss[0])) + "/" + str(int(iss[0])) + "/" + str(int(iss[1])))
        print(url_list)
    paper_url_list = []
    for url in url_list:
        paper_url = get_latest_issue_inf(url)
        for paper_url_1 in paper_url:
            paper_url_list.append(paper_url_1)
    print(len(paper_url_list))
    paper_url_list = compare_url(paper_url_list, "data\\Wiley_Small.csv", "data\\Temp\\Wiley_Small.csv")
    print(len(paper_url_list))
    if len(paper_url_list) > 0:
        paper_title_list = ["a" for _x in range(len(paper_url_list))]
        paper_abstract_list = ["a" for _x in range(len(paper_url_list))]
        for i in range(0, len(paper_url_list)):
            print(paper_url_list[i])
            [paper_title_list[i], paper_abstract_list[i]] = get_abstract(paper_url_list[i])
            # print(paper_abstract_list[i])
        return paper_title_list, paper_url_list, paper_abstract_list, latest_issue
    else:
        paper_title_list = []
        paper_url_list = []
        paper_abstract_list = []
        return paper_title_list, paper_url_list, paper_abstract_list, latest_issue

#ISS = 'Vol. 16, Iss. 43'
#[paper_title_list, paper_url_list, paper_abstract_list, latest_issue] = spider_Wiley_Small(ISS)
#print(paper_title_list)
#print(paper_url_list)
#print(paper_abstract_list)


from __future__ import absolute_import
from __future__ import with_statement

from kombu.serialization import unregister, SerializerNotInstalled

from .. import current_app
from ..exceptions import ImproperlyConfigured

from .serialization import register_auth

def _disable_insecure_serializers(whitelist=[]):
    for name in set("pickle", "json", "yaml", "msgpack") - set(whitelist):
        try:
            unregister(name)
        except SerializerNotInstalled:
            pass

def setup_security(allowed_serializers=[]):
    """setup secure serialization"""
    conf = current_app.conf
    if conf.CELERY_TASK_SERIALIZER != "auth":
        return

    try:
        from OpenSSL import crypto  # noqa
    except ImportError:
        raise ImproperlyConfigured(
            "You need to install the pyOpenSSL library to use "
            "the auth serializer.")

    key = conf.CELERY_SECURITY_KEY
    cert = conf.CELERY_SECURITY_CERTIFICATE
    store = conf.CELERY_SECURITY_CERT_STORE

    if key is None or cert is None or store is None:
        raise ImproperlyConfigured(
            "CELERY_SECURITY_KEY, CELERY_SECURITY_CERTIFICATE and "
            "CELERY_SECURITY_CERT_STORE options are required "
            "settings when using the auth serializer")

    with open(key) as kf:
        with open(cert) as cf:
            register_auth(kf.read(), cf.read(), store)
    _disable_insecure_serializers(allowed_serializers)


#!/usr/bin/python3
import matplotlib.pyplot as plt
import numpy as np
import sys

if (len(sys.argv) != 2):
    sys.exit("Expect 1 argument: Usage udc_process.py <log file>")

x, y1, y2, y3, y4, y5 = np.genfromtxt(sys.argv[1], delimiter=',', unpack=True)
#plt.plot(x,y1, label='Recall 1 in 10')

#approach1 - just plot one at a time
approach1 = False

if approach1:
    plt.xlabel('Number of Epochs')
    plt.ylabel('Recall@1')
    plt.title('Recall@1')
    plt.grid()
    plt.legend()
    plt.show()
else:
    fig, ax = plt.subplots()
    plt.grid()
    plt.xlabel('Number of Epochs')
    plt.ylabel('Recall Value')

    #Limit Range of x axis values
    ax.set_xlim(right=100000)

    #Plot the recall values for @1, @2, @5 and loss with epochs.
    ax.plot(x, y1, 'k--', color='green', label='Recall@1')
    ax.plot(x, y2, 'k:', color='blue', label='Recall@2')
    ax.plot(x, y3, 'k', color='orange', label='Recall@5')
    ax.plot(x, y5*0.25, 'k', color='black' , label='Loss')

    legend = ax.legend(loc='lower right', shadow=True, fontsize='large')

    # Put a nicer background color on the legend.
    #legend.get_frame().set_facecolor('C0')
    plt.show()


from app import app
import pandas as pd
from flask import request,jsonify
import numpy as np
import csv
import math

allreturns = pd.read_csv(f"../database/industry_info/all_stocks_returns.csv", sep=',')

def spearman_corr(first, second):
    return first.corr(second, method='spearman')

@app.route('/get-diversify-recommendations', methods=['POST'])
def get_diversify_recommendations():
    req_data = request.get_json()
    portfolio = req_data['portfolio']

    dates = allreturns['date']
    df = allreturns[portfolio]

    output = {}
    avg = df.mean(axis=1)
    for ticker in allreturns.columns:
        if('return' in ticker):
            output[ticker.replace('_return', '')] = spearman_corr(avg, allreturns[ticker])
    sortedOutput = sorted(output.items(), key=lambda x: x[1])
    result = sortedOutput[:10]
    return jsonify({'recommendations': result})

SixthSense - Finding Alpha/server/routes/get_diversify_recommendations.py


#RemoteError (Susan Lin & Kenny Li)
#SoftDev1 pd8
#K17 Average
#2018-10-05

import sqlite3   #enable control of an sqlite database
import csv       #facilitates CSV I/O

DB_FILE="discobandit.db"

db = sqlite3.connect(DB_FILE) #open if file exists, otherwise create
c = db.cursor()               #facilitate db ops

#======================== ROUND 1 =========================

with open("data/peeps.csv") as csvfile:
    reader = csv.DictReader(csvfile)
    c.execute("CREATE TABLE IF NOT EXISTS peeps(name TEXT, age INTEGER, id INTEGER)")
    for row in reader:
        c.execute("INSERT INTO peeps VALUES(" + "'" + row["name"] + "'"  + "," + row["age"] + "," + row["id"] + ")")

#======================== ROUND 2 =========================

with open("data/courses.csv") as csvfile:
    reader = csv.DictReader(csvfile)
    c.execute("CREATE TABLE IF NOT EXISTS courses(code TEXT, mark INTEGER, id INTEGER)")
    for row in reader:
        c.execute("INSERT INTO courses VALUES(" + "'" + row["code"] + "'"  + "," + row["mark"] + "," + row["id"] + ")")

#==========================================================

dictData = {}
#fills dictionary with name as key and value as a list of [id, grade]
def fillDict():
    data = c.execute("SELECT name, peeps.id, mark FROM peeps, courses WHERE peeps.id = courses.id")
    for row in data:
        #if name not in dictionary creates a key value pair
        if row[0] not in dictData:
            listIdGrade = [row[1], row[2]]
            dictData[row[0]] = listIdGrade
        #adds grade onto corresponding student
        else:
            dictData[row[0]][1] += row[2]
    computeAverage()

#computes average of student's grade
def computeAverage():
    for key in dictData:
        counter = 0.0
        data = c.execute("SELECT name, mark FROM peeps, courses WHERE peeps.id = courses.id")
        for row in data:
            if key == row[0]:
                counter += 1
        dictData[key][1] = dictData[key][1] / counter

#displays name, id and average from dictionary
def displayDict():
    retStr = ""
    for key in dictData:
        retStr += "Name:" + key + ", Id:" + str(dictData[key][0]) + ", Avg:" + str(dictData[key][1]) + "\n"
    return retStr

#fills peeps_avg with values from dictionary
def fillDatabase():
    c.execute("CREATE TABLE IF NOT EXISTS {0}({1}, {2}, {3})".format("peeps_avg", "name", "id", "avg"))
    for key in dictData:
        c.execute("INSERT INTO peeps_avg VALUES(?,?,?)", (key, dictData[key][0], dictData[key][1]))

#adds courses based on parameters given
def addCourse(code, mark, id):
    c.execute("INSERT INTO courses VALUES(?,?,?)", (str(code), int(mark), int(id)))

fillDict()
print (displayDict())
fillDatabase()
addCourse("biology", 100, 1)

db.commit() #save changes
db.close()  #close database


import tensorflow as tf
import numpy as np
import utils
import pandas as pd
import time
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score

PATH = 'dense_1024'
HIGHT_WIGHT = 105

class SiameseNetwork:

    def __init__(self, parameters):
        self.model = self.create_network((HIGHT_WIGHT, HIGHT_WIGHT, 1), parameters['optimizer'])

    def create_network(self, input_shape, optimizer):
        conv_net = tf.keras.Sequential()

        conv_net.add(tf.keras.layers.Conv2D(
            input_shape=input_shape,
            filters=64,
            kernel_size=[10, 10],
            activation='relu', bias_initializer=tf.keras.initializers.RandomNormal(mean=0.5, stddev=0.01),
            kernel_initializer=tf.keras.initializers.RandomNormal(stddev=0.01)))

        conv_net.add(tf.keras.layers.MaxPooling2D (pool_size=[2, 2], strides=2))

        conv_net.add(tf.keras.layers.Conv2D(
            filters=128,
            kernel_size=[7, 7],
            activation='relu',
            kernel_initializer=tf.keras.initializers.RandomNormal (stddev=0.01),
            bias_initializer=tf.keras.initializers.RandomNormal (mean=0.5, stddev=0.01)))

        conv_net.add(tf.keras.layers.MaxPooling2D(pool_size=[2, 2], strides=2))

        conv_net.add(tf.keras.layers.Conv2D(
            filters=128,
            kernel_size=[4, 4],
            activation='relu', bias_initializer=tf.keras.initializers.RandomNormal (mean=0.5, stddev=0.01),
            kernel_initializer=tf.keras.initializers.RandomNormal(stddev=0.01)))

        conv_net.add(tf.keras.layers.MaxPooling2D (pool_size=[2, 2], strides=2))

        conv_net.add (tf.keras.layers.Conv2D(
            filters=256,
            kernel_size=[4, 4],
            activation='relu', bias_initializer=tf.keras.initializers.RandomNormal(mean=0.5, stddev=0.01),
            kernel_initializer=tf.keras.initializers.RandomNormal(stddev=0.01)))

        conv_net.add(tf.keras.layers.Flatten())
        conv_net.add(tf.keras.layers.Dense(1024, activation='sigmoid',
                                           kernel_initializer=tf.keras.initializers.RandomNormal(stddev=0.2)))

        input1 = tf.keras.Input(input_shape)
        input2 = tf.keras.Input(input_shape)
        output1 = conv_net(input1)
        output2 = conv_net(input2)
        distance = tf.keras.layers.Lambda(
            lambda tensors: tf.keras.backend.abs(tensors[0] - tensors[1]))([output1, output2])
        pred = tf.keras.layers.Dense(units=1, activation=tf.keras.activations.sigmoid,
                                     kernel_initializer=tf.keras.initializers.RandomNormal(stddev=0.2))(distance)

        model = tf.keras.Model(inputs=[input1, input2], outputs=pred)

        model.compile(loss='binary_crossentropy', metrics=['acc'], optimizer=optimizer)
        return model

    def fit_model(self, X, y, ephocs, batch_size):
        """
        fit the model on training set
        :param X: training set
        :param y: labels
        :param ephocs: number of maximal ephocs
        :param batch_size: number of examples in each iteration
        :return: the history of training and validation loss in each iteration
        """

        checkpointer = tf.keras.callbacks.ModelCheckpoint(
            filepath="models\\{}_{}_{}_{}.h5".format(PATH, optimizer, batch, lr),
            save_best_only=True)
        #
        # reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.2,
        #                                                  patience=3, min_lr=0.00001)

        x_train, x_val, y_train, y_val = train_test_split(list(range(len(y))), y, stratify=y, test_size=0.1)
        X_train = list(np.array(X)[:, x_train])
        X_val = list(np.array(X)[:, x_val])

        early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)

        tb = tf.keras.callbacks.TensorBoard(histogram_freq=1,
                                            write_graph=True)

        history = self.model.fit(X_train, y_train,
                                 validation_data=(X_val, y_val), verbose=2, epochs=ephocs,
                                 batch_size=batch_size,  callbacks=[early_stop,reduce_lr, checkpointer, tb]) # reduce_lr
        return history

    def evaluate_model(self, x_test, y_test):
        return self.model.evaluate (x_test, y_test, verbose=2)

    def predict(self, x):
        return self.model.predict(x, verbose=2)

# batch_sizes = [64, 128, 256]
# learning_rate = [0.001, 0.01, 0.1]
# optimizers = ['adagrad', 'SGD', 'adam']

batch_sizes = [64]
learning_rate = [0.01]
optimizers = ['adagrad']
image_dict = utils.load_data('lfwa', HIGHT_WIGHT)
output_list = []
x_train, x_test, y_train, y_test = utils.train_test_create('train.csv', 'test.csv', image_dict, HIGHT_WIGHT)

opt = 0
ephocs = 200
for optimizer in optimizers:
    for lr in learning_rate:
        for batch in batch_sizes:
            if optimizer is 'SGD':
                opt = tf.keras.optimizers.SGD(lr=lr, momentum=0.5)
            if optimizer is 'adam':
                opt = tf.keras.optimizers.Adam(lr=lr)
            if optimizer is 'adagrad':
                opt = tf.keras.optimizers.Adagrad (lr=lr)
            net = SiameseNetwork({'optimizer': opt})
            net.model.summary()

            start = time.time()
            history = net.fit_model(
                x_train, y_train, ephocs, batch)
            pd.DataFrame.from_dict(history.history).to_csv("{}\\{}_{}_{}.csv".format(PATH, optimizer, batch, lr))
            end_time = time.time() - start

            test_loss, test_acc = net.evaluate_model(x_test, y_test)
            test_pred = net.predict(x_test)
            auc_score = roc_auc_score(y_test, test_pred)
            output_list.append({'batch size': batch, 'learning_rate': lr,
                                 'optimizer': optimizer,
                                 'ephocs': ephocs,
                                 'test accuracy': test_acc, 'test auc': auc_score,
                                 'test loss': test_loss, 'learning_time': end_time})

            df_out = pd.DataFrame (output_list)
            df_out.to_csv('{}\\test_results-test.csv'.format(PATH))


import numpy as np
import cv2
import mahotas

class Preprocessor:
    def __init__(self, image_path):
        # 读取图像并截取
        self.image = cv2.imdecode(np.fromfile(image_path, dtype=np.uint8), -1)

    def find_roi(self):
        # 转灰度图
        if len(self.image.shape)>2:
            gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
        else:
            gray = self.image

        # 去噪
        blurred = cv2.GaussianBlur(gray, (7, 7), 0)
        blurred = cv2.bilateralFilter(blurred, 17, 16, 32)
        # cv2.imshow('Blurred', blurred)

        # 二值化
        T = mahotas.thresholding.otsu(blurred)
        # print("Otsu’s threshold: {}".format(T))
        thresh = blurred.copy()
        thresh[thresh > T] = 255
        thresh[thresh < 255] = 0
        cv2.imshow('Thresh', thresh)

        # 边缘检测
        edged = cv2.Canny(thresh, 30, 150)
        (_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

        # 轮廓去噪，去除噪声轮廓
        '''有bug，如果寻找所有轮廓，则contour不连续，外接框无法框定最大范围
        如果只保留最大轮廓，contour又不完整，外接框只框定部分范围'''
        contour = []
        for each in cnts:
            # 去除长度小于10的轮廓，看做噪声
            if each.shape[0] > 10:
                contour.append(each)

        # 显示轮廓
        chara = self.image.copy()
        cv2.drawContours(chara, contour, -1, (0, 255, 0), 2)
        cv2.imshow("Contour", chara)
        # cv2.waitKey(0)

        # 轮廓的外接矩形框
        (x, y, w, h) = cv2.boundingRect(np.array(contour))

        # 显示原图中的字符区域
        ROI = thresh[y:y + h, x:x + w]
        cv2.imshow("ROI", ROI)
        cv2.waitKey(0)

        return ROI


import time
from appium import webdriver

class DouYinApp():
    def __init__(self):
        self.params = {}
        self.params['deviceName'] = '127.0.0.1:62001 device'
        self.params['platformVersion'] = '5.1.1'
        self.params['automationName'] = 'Appium'
        self.params['platformName'] = 'Android'
        self.params['autoAcceptAlerts'] = 'true'
        self.params['noReset'] = 'true'
        self.params['appPackage'] = 'com.ss.android.ugc.aweme'
        self.params['appActivity'] = 'com.ss.android.ugc.aweme.live.LivePlayActivity'
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_capabilities=self.params)

    def swipDown(self, n):
        L = self.driver.get_window_size()
        x = L['width'] * 0.5
        y1 = L['height'] * 0.8
        y2 = L['height'] * 0.3
        num = 0
        # while num < n:
        while True:
            self.driver.swipe(x, y1, x, y2, 6000)
            # num += 1

    def swipLeft(self):
        L = self.driver.get_window_size()
        x1 = L['width'] * 0.8
        x2 = L['width'] * 0.3
        y1 = L['height'] * 0.5

        self.driver.swipe(x1, y1, x2, y1, 500)

    def login(self):
        self.driver.find_element_by_xpath('//android.widget.TextView[@text="我"]').click()
        time.sleep(0.5)
        self.driver.find_element_by_xpath('//android.widget.TextView[@text="密码登录"]').click()
        time.sleep(0.5)
        self.driver.find_element_by_xpath('//android.widget.EditText[@text="请输入手机号"]').send_keys('18595794414')
        time.sleep(0.5)
        self.driver.find_element_by_id('com.ss.android.ugc.aweme:id/dfk').send_keys('525748037')
        time.sleep(0.5)
        self.driver.find_element_by_xpath('//android.widget.CheckBox').click()
        time.sleep(0.5)
        self.driver.find_element_by_xpath('//android.widget.TextView[@text="登录"]').click()
        time.sleep(1)
        self.driver.find_element_by_id('com.ss.android.ugc.aweme:id/ep8').click()
        print('登录成功')

    def guanggao(self):
        try:
            guanggao = self.driver.find_element_by_id('com.ss.android.ugc.aweme:id/eb')
        except:
            guanggao = ''
        return guanggao

    def stream(self):
        self.driver.find_element_by_id('com.ss.android.ugc.aweme:id/cnt').click()

    def more(self):
        self.driver.find_element_by_xpath('//android.widget.TextView[@text="更多直播"]').click()

    def run(self, n):
        try:
            guanggao = self.guanggao()
            if guanggao != '':
                guanggao.click()
            print('--------跳过广告--------')
            time.sleep(1)

            # print('开始登录')
            # self.login()
            # time.sleep(5)

            self.stream()
            print('----------直播---------')
            time.sleep(3)

            self.swipLeft()
            print('----------更多---------')
            time.sleep(2)

            print('--------开始爬取-----------')
            self.swipDown(n)

        except BaseException as e:
            print(e)

if __name__ == '__main__':
    douyin = DouYinApp()
    douyin.run(100)


import db_connection
import configparser

import build_query
from setup import super_logger

config = configparser.ConfigParser()
config.read('config.ini')

credentials = '/Volumes/Data/sling-stream-recover-eb60c169ef8a.json'
projectid = 'sling-stream-recover'

reportingdate = config.get('reporting_date', 'reportingdate')

# Schema
bq_project = config.get('bigquery', 'project_id')

bqschema = config['schema']['bqschema']
redshiftbq = config['schema']['redshiftschema']

# Dany delete
credentials = '/Volumes/Data/sling-stream-recover-eb60c169ef8a.json'
projectid = 'sling-stream-recover'

bqconn = db_connection.big_query(credentials, projectid)

def delete_data(uuid):
    lines = []
    queries = []
    with open("bqtables.txt") as file_in:

        for line in file_in:
            lines.append(line.strip())

    with open('lisquery.txt') as file:
        for quer in file:
            queries.append(quer.strip())

        for i in lines:
            super_logger.info(f'*****************{i}**********************')
            print('*****************{i}**********************')

            for j in queries:
                selectstatement = ' '.join(j.split()[1:2])
                query = str(j).format(bqschema.format(str(i)),
                                      "'" + uuid + "'") + ';'

                super_logger.info(f'BigQuery:{query}:')
                print('BigQuery:{query}:')

                # Get the result from Bq
                BQ_query = build_query.selectquery_bq(query, bqconn)
                result_BQ = BQ_query
                super_logger.info(f'BigQuery:{selectstatement}:{result_BQ}')
                print('BigQuery:{selectstatement}:{result_BQ}')

if __name__ == '__main__':
    delete_data("uuid")


#######################################################################
# Module: SQLiteDataAccessLayer
# Purpose: This class has all the data access code specific to SQLite
#######################################################################

from core.expense_category import ExpenseCategory
from core.store import Store
from core.expense import Expense
from core.person import Person
from core.expense_category import ExpenseCategory
from core.payment_type import PaymentType
from core.da.dal import DataAccessLayer
from core.da.sqlitedriver import SQLiteDriver
from core.constants import _DATE_STR_DISPLAY_FORMAT_
from core.constants import _DATE_STR_STORAGE_FORMAT_

class SQLiteDataAccessLayer( DataAccessLayer ):

    def __init__( self ) :
        # TODO: The following line should not be hard coded and should come from param
        self.data_file = './db/expenses.db'

    # All expense methods
    def addExpense( self, expense: Expense ) -> None:
        print( 'sqlite_dat: add Expense' )
        expense_detail = expense.getExpenseDetail()
        expense_date = expense.getExpenseDate().strftime( _DATE_STR_STORAGE_FORMAT_ )
        expense_amount = expense.getExpenseAmount()
        person_id = expense.getPerson().getId()
        store_id = expense.getStore().getId()
        expense_category_id = expense.getExpenseCategory().getId()
        payment_type_id = expense.getPaymentType().getId()

        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """INSERT INTO EXPENSE( EXPENSE_DETAIL, EXPENSE_DATE, EXPENSE_AMOUNT, EXPENSE_CATEGORY_ID, PAYMENT_TYPE_ID, PERSON_ID, STORE_ID ) VALUES( ?, ?, ?, ?, ?, ?, ? )"""
            cursor.execute(_SQL, (expense_detail, expense_date, expense_amount, expense_category_id, payment_type_id, person_id, store_id ))

    def deleteExpense( self, id : str ) -> None :
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """DELETE FROM EXPENSE WHERE ID = ? """
            cursor.execute(_SQL, (id, ))

    def listExpenses( self ) -> list:
        contents = []
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """ SELECT ID, EXPENSE_DETAIL, EXPENSE_DATE, EXPENSE_AMOUNT,
                        EXPENSE_CATEGORY_ID, PAYMENT_TYPE_ID, PERSON_ID, STORE_ID FROM EXPENSE """
            _ORDERBY = " ORDER BY EXPENSE.EXPENSE_DATE DESC "
            cursor.execute(_SQL + _ORDERBY)
            contents = cursor.fetchall()
        print( contents )
        return contents

    # Expense category methods
    def addExpenseCategory( self, expense_category : ExpenseCategory ) -> None :
        expense_type : str = expense_category.getExpenseType()
        expense_type_detail : str = expense_category.getExpenseDetail()

        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """INSERT INTO EXPENSE_CATEGORY(expense_type, expense_type_detail ) VALUES( ?, ? )"""
            cursor.execute(_SQL, (expense_type, expense_type_detail))

    def deleteExpenseCategory( self, id : str ) -> None :
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """DELETE FROM EXPENSE_CATEGORY WHERE ID = ? """
            cursor.execute(_SQL, (id, ))

    def listExpenseCategories( self ) -> list :
        contents = []
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """ SELECT ID, expense_type, expense_type_detail FROM expense_category  """
            _ORDERBY = """ ORDER BY expense_type """
            cursor.execute(_SQL + _ORDERBY)
            contents = cursor.fetchall()
        return contents

    # Store(s) methods
    def addStore( self, store : Store ) -> None :
        store_name = store.getStoreName()
        store_detail = store.getStoreDetail()
        home_delivery = store.getHomeDeliveryString()

        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """INSERT INTO STORE(STORE_NAME, STORE_DETAIL, HOME_DELIVERY ) VALUES( ?, ?, ? )"""
            cursor.execute(_SQL, (store_name, store_detail, home_delivery))

    def deleteStore( self, id : str ) -> None :
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """DELETE FROM STORE WHERE ID = ? """
            cursor.execute(_SQL, (id, ))

    def listStores( self ) -> list :
        contents = []
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """ SELECT ID, STORE_NAME, STORE_DETAIL, HOME_DELIVERY FROM STORE  """
            _ORDERBY = """ ORDER BY STORE_NAME """
            cursor.execute(_SQL + _ORDERBY)
            contents = cursor.fetchall()
        return contents

    # Payment Type methods
    def addPaymentType( self, payment_type : PaymentType ) -> None:
        payment_mode = payment_type.getPaymentMode()
        payment_mode_detail = payment_type.getPaymentModeDetail()

        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """ INSERT INTO PAYMENT_TYPE(PAYMENT_MODE, PAYMENT_MODE_DETAIL ) VALUES( ?, ? ) """
            cursor.execute(_SQL, (payment_mode, payment_mode_detail))

    def deletePaymentType( self, id : str ) -> None:
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """DELETE FROM PAYMENT_TYPE WHERE ID = ? """
            cursor.execute(_SQL, (id, ))

    def listPaymentTypes( self ) -> list:
        contents = []
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """SELECT ID, PAYMENT_MODE, PAYMENT_MODE_DETAIL from PAYMENT_TYPE  ORDER BY PAYMENT_MODE  """
            cursor.execute( _SQL )
            contents = cursor.fetchall()
        return contents

    # Person methods
    def addPerson( self, person : Person ) -> None:
        person_first_name = person.getFirstName()
        person_last_name = person.getLastName()
        person_short_name = person.getShortName()

        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """INSERT INTO PERSON(PERSON_FIRST_NAME, PERSON_LAST_NAME, PERSON_SHORT_NAME ) VALUES( ?, ?, ? )"""
            cursor.execute(_SQL, (person_first_name, person_last_name, person_short_name))

    def deletePerson( self, id : str ) -> None :
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """DELETE FROM PERSON WHERE ID = ? """
            cursor.execute(_SQL, (id, ))

    def listPeople( self ) -> list:
        contents = []
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """SELECT ID, PERSON_FIRST_NAME, PERSON_LAST_NAME, PERSON_SHORT_NAME  from PERSON  """
            cursor.execute( _SQL )
            contents = cursor.fetchall()
        return contents

    # Other utility methods
    def getPersonByShortName( self, short_name : str  ) -> Person:
        contents = []
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """SELECT ID, PERSON_FIRST_NAME, PERSON_LAST_NAME, PERSON_SHORT_NAME  from PERSON  """
            _WHERE = "WHERE PERSON_SHORT_NAME = ? "
            cursor.execute( _SQL + _WHERE, (short_name,) )
            contents = cursor.fetchall()
        print( contents )
        if contents:
            content = contents[ 0 ]
            return Person( content[ 0 ], content[ 1 ], content[ 2 ], content[ 3 ])
        else:
            return None

    def getStoreByStoreName( self, store_name : str ) -> Store:
        contents = []
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """ SELECT ID, STORE_NAME, STORE_DETAIL, HOME_DELIVERY FROM STORE  """
            _WHERE = """ WHERE STORE_NAME = ? """
            cursor.execute(_SQL + _WHERE, (store_name, ) )
            contents = cursor.fetchall()
        if contents:
            content = contents[ 0 ]
            return Store( content[ 0 ], content[ 1 ], content[ 2 ], content[ 3 ])
        else:
            return None

    def getPaymentTypeByMode( self, payment_mode : str ) -> PaymentType:
        contents = []
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """SELECT ID, PAYMENT_MODE, PAYMENT_MODE_DETAIL from PAYMENT_TYPE  """
            _WHERE = """ WHERE PAYMENT_MODE = ?  """
            cursor.execute( _SQL + _WHERE, (payment_mode, ) )
            contents = cursor.fetchall()
        if contents:
            content = contents[ 0 ]
            return PaymentType( content[ 0 ], content[ 1 ], content[ 2 ])
        else:
            return None

    def getExpenseCategoryByExpenseType( self, expense_type : str ) -> ExpenseCategory:
        contents = []
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """ SELECT ID, EXPENSE_TYPE, EXPENSE_TYPE_DETAIL FROM expense_category  """
            _WHERE = """ WHERE EXPENSE_TYPE = ? """
            cursor.execute(_SQL + _WHERE, ( expense_type, ))
            contents = cursor.fetchall()

        print( contents )
        if contents:
            content = contents[ 0 ]
            return ExpenseCategory( content[ 0 ], content[ 1 ], content[ 2 ]  )
        else:
            return None

    # All reporting functions
    def getExpenseSummaryForMonth( self, search_string : str ) -> tuple:
        contents = []
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """ SELECT SUBSTR( EXPENSE_DATE, 1, 7 ), COUNT( ID ), SUM(EXPENSE_AMOUNT) FROM EXPENSE GROUP BY SUBSTR( EXPENSE_DATE, 1, 7 ) """
            _WHERE = """ HAVING SUBSTR( EXPENSE_DATE, 1, 7) = ?  """
            cursor.execute( _SQL + _WHERE, ( search_string, ) )
            contents = cursor.fetchall()
        if contents:
            content = contents[ 0 ]
            return ( content[ 0 ], content[ 1 ], content[ 2 ] )
        else:
            return None

    def getMonthwiseExpenseSummary( self ) -> list:
        contents = []
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """ SELECT SUBSTR( EXPENSE_DATE, 1, 7 ), COUNT( ID ), SUM(EXPENSE_AMOUNT) FROM EXPENSE GROUP BY SUBSTR( EXPENSE_DATE, 1, 7 )  ORDER BY EXPENSE_DATE DESC """
            cursor.execute( _SQL )
            contents = cursor.fetchall()
        if contents:
            return contents
        else:
            return None

    def getMonthwiseCategoryExpense( self ) -> list:
        contents = []
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """ SELECT SUBSTR( EXPENSE_DATE, 1, 7),  EXPENSE_CATEGORY.EXPENSE_TYPE,  COUNT( EXPENSE.ID), SUM( EXPENSE_AMOUNT ) from EXPENSE, EXPENSE_CATEGORY WHERE EXPENSE.EXPENSE_CATEGORY_ID = EXPENSE_CATEGORY.ID GROUP BY SUBSTR( EXPENSE_DATE, 1, 7), EXPENSE_CATEGORY.EXPENSE_TYPE ORDER BY EXPENSE_DATE DESC,  EXPENSE_CATEGORY.EXPENSE_TYPE  """
            cursor.execute( _SQL  )
            contents = cursor.fetchall()
        if contents:
            return contents
        else:
            return None

    def getMonthwisePaymentTypeSummary( self ) -> list:
        contents = []
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """ SELECT SUBSTR( EXPENSE_DATE, 1, 7),  PAYMENT_TYPE.PAYMENT_MODE,  COUNT( EXPENSE.ID), SUM( EXPENSE_AMOUNT ) from EXPENSE, PAYMENT_TYPE WHERE EXPENSE.PAYMENT_TYPE_ID = PAYMENT_TYPE.ID GROUP BY SUBSTR( EXPENSE_DATE, 1, 7), PAYMENT_TYPE.PAYMENT_MODE ORDER BY EXPENSE_DATE DESC,  PAYMENT_TYPE.PAYMENT_MODE  """
            cursor.execute( _SQL )
            contents = cursor.fetchall()
        if contents:
            return contents
        else:
            return None

    def getMonthwisePersonSummary( self ) -> list:
        contents = []
        with SQLiteDriver( self.data_file ) as cursor:
            _SQL = """ SELECT SUBSTR( EXPENSE_DATE, 1, 7),  PERSON.PERSON_SHORT_NAME,  COUNT( EXPENSE.ID), SUM( EXPENSE_AMOUNT ) from EXPENSE, PERSON WHERE EXPENSE.PERSON_ID = PERSON.ID GROUP BY SUBSTR( EXPENSE_DATE, 1, 7), PERSON.PERSON_SHORT_NAME ORDER BY EXPENSE_DATE DESC,  PERSON.PERSON_SHORT_NAME  """
            cursor.execute( _SQL )
            contents = cursor.fetchall()
        if contents:
            return contents
        else:
            return None


# Teste seu código aos poucos.
# Não teste tudo no final, pois fica mais difícil de identificar erros.
# Use as mensagens de erro para corrigir seu código.
psd = float(input(" digite psd: "))
d = 5/100

if (psd>=200):
	pcd = psd -(psd * d)
	print(round(pcd,2))
else:
	print(round(psd,2))

5 - Notebooks e Data/1 - Análises numéricas/Arquivos David/Atualizados/logDicas-master/data/2019-1/225/users/3994/codes/1636_869.py


import re

import pytest
from django.urls import include, path

from drf_spectacular.generators import SchemaGenerator
from tests import assert_schema

transforms = [
    # User model first_name differences
    lambda x: re.sub(r'(first_name:\n *type: string\n *maxLength:) 150', r'\g<1> 30', x, re.M),
    # User model username validator tail
    lambda x: x.replace(r'+$', r'+\Z'),
]

@pytest.mark.contrib('dj_rest_auth', 'allauth')
def test_rest_auth(no_warnings):
    urlpatterns = [
        path('rest-auth/', include('dj_rest_auth.urls')),
        path('rest-auth/registration/', include('dj_rest_auth.registration.urls')),
    ]

    generator = SchemaGenerator(patterns=urlpatterns)
    schema = generator.get_schema(request=None, public=True)

    assert_schema(
        schema, 'tests/contrib/test_rest_auth.yml', transforms=transforms
    )


#-----------------------------------------------------------------------------------
#This code was made in python v3.5+ =)

#-----------------------------------------------------------------------------------

monthDict = {'January': 31, 'February1': 29,'February2': 28, 'Mars': 31,
'April': 30, 'May': 31, 'June':30, 'July':31, 'August':31,
'September':30, 'Oktober':31, 'November':30, 'December':31 }

monthinput = input('Which month?(lowercase letters please!) ')

def getAnswer(userInput):
	if (userInput == 'january'):
		print ("You wrote January! And the amount of days are: %s" % monthDict.get('January'))
	elif (userInput == 'february'):
		isit = input('Oh You wrote february! Is it a leap year? ')
		if (isit == "yes"):
			print ("The amount of days are: %s" % monthDict.get('February1'))
		else:
			print ("The amount of days are: %s" % monthDict.get('February2'))
	elif (userInput == 'mars'):
		print ("You wrote Mars! And the amount of days are: %s" % monthDict.get('Mars'))
	elif (userInput == 'april'):
		print ("You wrote April! And the amount of days are: %s" % monthDict.get('April'))
	elif (userInput == 'may'):
		print ("You wrote May! And the amount of days are: %s" % monthDict.get('May'))
	elif (userInput == 'june'):
		print ("You wrote June! And the amount of days are: %s" % monthDict.get('June'))
	elif (userInput == 'july'):
		print ("You wrote July! And the amount of days are: %s" % monthDict.get('July'))
	elif (userInput == 'august'):
		print ("You wrote August! And the amount of days are: %s" % monthDict.get('August'))
	elif (userInput == 'september'):
		print ("You wrote September! And the amount of days are: %s" % monthDict.get('September'))
	elif (userInput == 'oktober'):
		print ("You wrote Oktober! And the amount of days are: %s" % monthDict.get('Oktober'))
	elif (userInput == 'november'):
		print ("You wrote November! And the amount of days are: %s" % monthDict.get('November'))
	elif (userInput == 'december'):
		print ("You wrote December! And the amount of days are: %s" % monthDict.get('December'))
	else:
		print ("You misspelt it or that's not a month =)")

getAnswer(monthinput)


from __future__ import print_function
import functools
import tensorflow as tf
import numpy as np
import math

from model import lazy_property, numLayers
from cells import EGRUCell, EGRUCell_V1, EGRUCell_V2

class MRnnPredictor:

    def __init__(self, data, target, seqlen, num_class, training, dropout, num_hidden=200, num_layers=1, learning_rate=1e-4):
        self.data = data
        self.target = target
        self.seqlen = seqlen
        self.training = training
        self.dropout = dropout
        self._num_hidden = num_hidden
        self._num_layers = num_layers
        self._num_class = num_class
        self._learning_rate = learning_rate
        self.prediction
        self.accuracy
        self.optimize
        self.cost

    @lazy_property
    def prediction(self):
        rnn = self.rnn(self, self.data)
        # ln = tf.contrib.layers.layer_norm(inputs=rnn)
        # dnn = self.dnn(self, rnn)
        output = tf.compat.v1.layers.dense(
            inputs=rnn,
            units=self._num_class,
            kernel_initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.01),
            bias_initializer=tf.compat.v1.constant_initializer(0.1),
            activation=tf.nn.elu
        )
        return output

    @staticmethod
    def dnn(self, input):
        with tf.compat.v1.variable_scope("dnn"):
            dense = tf.compat.v1.layers.dense(
                inputs=input,
                units=self._num_hidden*2,
                kernel_initializer=tf.compat.v1.truncated_normal_initializer(
                    stddev=0.01),
                bias_initializer=tf.compat.v1.constant_initializer(0.1)
                # activation=tf.nn.elu
            )
            dense = tf.compat.v1.layers.dense(
                inputs=dense,
                units=self._num_hidden,
                kernel_initializer=tf.compat.v1.truncated_normal_initializer(
                    stddev=0.01),
                bias_initializer=tf.compat.v1.constant_initializer(0.1),
                # activation=tf.nn.elu
            )
            dense = tf.compat.v1.layers.dense(
                inputs=dense,
                units=self._num_hidden//2,
                kernel_initializer=tf.compat.v1.truncated_normal_initializer(
                    stddev=0.01),
                bias_initializer=tf.compat.v1.constant_initializer(0.1),
                activation=tf.nn.relu6
            )
            dropout = tf.compat.v1.layers.dropout(
                inputs=dense, rate=0.8, training=self.training)
            return dropout

    @staticmethod
    def rnn(self, input):
        # Recurrent network.
        gru1 = tf.compat.v1.nn.rnn_cell.GRUCell(
            num_units=self._num_hidden,
            kernel_initializer=tf.compat.v1.truncated_normal_initializer(
                stddev=0.01),
            bias_initializer=tf.compat.v1.constant_initializer(0.1)
        )
        gru2 = tf.compat.v1.nn.rnn_cell.GRUCell(
            num_units=self._num_hidden*2,
            kernel_initializer=tf.compat.v1.truncated_normal_initializer(
                stddev=0.01),
            bias_initializer=tf.compat.v1.constant_initializer(0.1)
        )
        gru2 = tf.compat.v1.nn.rnn_cell.DropoutWrapper(
            cell=gru2,
            output_keep_prob=(1.0-self.dropout)
        )
        cell = tf.compat.v1.nn.rnn_cell.MultiRNNCell([gru1, gru2] * self._num_layers)
        output, _ = tf.compat.v1.nn.dynamic_rnn(
            cell,
            input,
            dtype=tf.float32,
            sequence_length=self.seqlen
        )

        return self.last_relevant(output, self.seqlen)

    @staticmethod
    def last_relevant(output, length):
        batch_size = tf.shape(input=output)[0]
        relevant = tf.gather_nd(output, tf.stack(
            [tf.range(batch_size), length-1], axis=1))
        return relevant

    @lazy_property
    def cost(self):
        cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
            labels=self.target, logits=self.prediction, name="xentropy")
        loss = tf.reduce_mean(input_tensor=cross_entropy)
        return loss

    @lazy_property
    def optimize(self):
        update_ops = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.UPDATE_OPS)
        optimizer = None
        with tf.control_dependencies(update_ops):
            optimizer = tf.compat.v1.train.AdamOptimizer(self._learning_rate).minimize(
                self.cost, global_step=tf.compat.v1.train.get_global_step())
        return optimizer

    @lazy_property
    def accuracy(self):
        accuracy = tf.equal(
            tf.argmax(input=self.target, axis=1), tf.argmax(input=self.prediction, axis=1))
        return tf.reduce_mean(input_tensor=tf.cast(accuracy, tf.float32), name="accuracy")