from urllib.request import urlopen
import json
json_usrl = 'https://raw.githubusercontent.com/muxuezi/btc/master/btc_close_2017.json'
response = urlopen(json_usrl)
req = response.read()
with open('btc_colose_2017_urllib.json', 'wb') as f:
    f.write(req)
file_urllib = json.loads(req)

import requests

json_url = 'https://raw.githubusercontent.com/muxuezi/btc/master/btc_close_2017.json'
req = requests.get(json_url)
with open('btc_close_2017_request.json', 'w') as f:
    f.write(req.text)
file_requests = req.json()
print(file_urllib == file_requests)


from django import forms
from shipping_label.models import Shipping_data
from order_status.models import OrderStatus

# forms.ModelForm
class UserShippingForm(forms.ModelForm):
    first_name = forms.CharField(
        widget=forms.TextInput(
            attrs={"placeholder": "Enter your name", "id": "frist_name",
                   "class": "inputs"}),
        max_length=30, help_text="Enter your first name")

    last_name = forms.CharField(
        widget=forms.TextInput(
            attrs={"placeholder": "Enter your last name", "id": "lastname",
                   "class": "inputs"}),
        max_length=30, help_text="Enter your last name")

    email = forms.EmailField(widget=forms.TextInput(
        attrs={"type": "email", "placeholder": "Enter your email"},
    ), max_length=40, help_text="Enter a valid email")

    phone_number = forms.CharField(widget=forms.TextInput(
        attrs={"placeholder": "Enter your phone number"}
    ), max_length=10, help_text="Enter a valid phone number")

    address = forms.CharField(widget=forms.TextInput(
        attrs={"placeholder": "Enter your mailing address"}
    ), max_length=100, help_text="Enter your shipping address")

    class Meta:
        model = Shipping_data
        fields = [
            "first_name",
            "last_name",
            "email",
            "phone_number",
            "address"
        ]

class Shipping_Status(forms.ModelForm):
    shipping_status = (
        ("in_wharehouse", "In Wharehouse"),
        ("in_shipping_facility", "In Shipping Facility"),
        ("out_for_delivery", "Out For Delivery"),
        ("attempted_delivery", "Delivery Attempted"),
        ("delived", "Delivered"),
    )

    order_status = forms.ChoiceField(required=True, choices=shipping_status,
                                     widget=forms.Select(attrs={"id": "order_status"}))

    order_number = forms.CharField(required=False, widget=forms.TextInput(attrs={"id": "order_number", "hidden": True}))

    item = forms.CharField(
        widget=forms.TextInput(
            attrs={"placeholder": "Enter the item's title", "id": "item"}),
        max_length=30, help_text="Enter your last name")

    price = forms.DecimalField(required=True, max_value=None, min_value=1, max_digits=None, decimal_places=2)

    bought_by = forms.CharField(required=False, widget=forms.NumberInput(attrs={"hidden": True}))

    class Meta:
        model = OrderStatus
        fields = [
            "order_number",
            "order_status",
            "item",
            "price",
            "bought_by",
        ]


#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2019-03-25 11:01:52
# Project: firsttest

from pyspider.libs.base_handler import *

class Handler(BaseHandler):
    crawl_config = {

    }

    def __init__(self):
        self.base_url = 'https://blog.csdn.net/u011331731/article/list/'
        self.page_num = 1
        self.total_num = 6

    @every(minutes=24 * 60)
    def on_start(self):
         while self.page_num <= self.total_num:
            url = self.base_url + str(self.page_num)
            self.crawl(url, callback=self.index_page)
            self.page_num += 1

    @config(age=10 * 24 * 60 * 60)
    def index_page(self, response):
        for each in response.doc('a[href^="http"]').items():
            if each.attr.href.find('details')<=0:
                continue
            self.crawl(each.attr.href, callback=self.detail_page)

    @config(priority=2)
    def detail_page(self, response):
        return {
            "url": response.url,
            "title": response.doc('title').text(),
        }


# -*- coding: utf-8 -*-
"""
Created on Fri Jan 22 13:02:39 2021

@author: bcyk5
"""

import sys

import pickle as pckl
import pandas as pd
import os
from scraper_api import ScraperAPIClient
#from bs4 import BeautifulSoup
from datetime import datetime
import time

def saveOutFile2(outLoc,var2Save,fileName):
    ###pickles the outfile and saves it to a dir
    import pickle
    import os

    #wrkDir = os.getcwd()
    #'relative/path/to/file/you/want'
    #os.chdir('')

    fileName = fileName + '.pckl'
    path = outLoc + '/' + fileName
    with open(path, 'wb') as f:  # Python 3: open(..., 'wb')
        pickle.dump(var2Save, f)
    f.close()

    #os.chdir(wrkDir)

def loadPickled2(outName,path,fileName):
    #import pickle
    fileName = path + fileName + '.pckl'
    with open(fileName,'rb') as f:
        outName = pckl.load(f)

    return outName

def importInput(fileName):

    df = pd.read_csv(fileName,low_memory=False,dtype={'ids': object})
    df['ids'] = df['ids'].astype(str)
    try:
        df = df.drop(labels = ['Unnamed: 0'],axis = 1,inplace = False)
    except:
        pass

    df = df.astype(str)

    return df

def cleanTitle(title):

    title = title.replace('\n','')
    title = title.replace('  ',' ')

    return title

def makeScrapInput2(dictIn,prevDone,num): ##### appending to df is too slow.
### rewriting so all operations are with dictionaries

    wrkDir = os.getcwd()
    #os.chdir(prevDone)
    directory = wrkDir + '/' + prevDone + '/'
    outDic = {}
    returnDict = {}
    outList = []
    for filename in os.listdir(directory):
        if filename.endswith(".pckl"):
            outName = filename.replace(".pckl","")

            outList.append(loadPickled2(outName,directory,outName))

    #merging dict
    #https://stackoverflow.com/questions/38987/how-do-i-merge-two-dictionaries-in-a-single-expression-in-python-taking-union-o
    #https://www.tutorialspoint.com/python3/dictionary_update.htm

    for dic in outList:
        outDic.update(dic)

    #### loop through the df, if that row isn't allready in the dic
    #https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.append.html
    # column_names = ["ids", "jref", "title"]
    # dfOut = pd.DataFrame(columns = column_names)
    count = 0

    for i,key in enumerate(dictIn):
        if count > num:   #### only loads num amount of data points for scrapping
            break

        if dictIn[key]['ids'] not in outDic:
            returnDict[count] = dictIn[key]
            count= count+1

    # for i, row in df.iterrows():
    #     if count>num:
    #         break

    #     if row['ids'] not in outDic:
    #         dfOut = dfOut.append(row)
    #         count = count +1

    return returnDict

    # look in the folder of stuff thats already been done
    # import the pickled dicts (data thats already scrapped needs to be saved as dict)
    # cat dicts into one
    #loop through df, if id not in dict, add to new df or lists
    # when hit limit, stop

    #return df to be used as input

def getCitedBy(textIn):
    #need to grab the first occurance of cited by
    ind = textIn.index('Cited by')
    ind2 = ind + len('Cited by')

    ind3 = textIn.index('',ind,-1)

    return textIn[ind:ind3]

    #return None

def checkTitle2(textIn):
    ## in the "oldRes"  Calculation of prompt diphoton production cross sections at Fermilab Tevatron and CERN LHC energies
    #title comes a bit after data-clk-atid

    #can use this to grab a small substring, then search for partial matches
    C1 = textIn.index('Cited by ')

    Tguess = textIn.rfind('data-clk-atid',0,C1)

    tempStr = textIn[Tguess:C1]

    indTag = tempStr.index('')

    return tempStr[0:indTag]

def checkTitle3(textIn):
    ###V3 also returns some extra text which may or may not be useful later.
    ## in the "oldRes"  Calculation of prompt diphoton production cross sections at Fermilab Tevatron and CERN LHC energies
    #title comes a bit after data-clk-atid

    #can use this to grab a small substring, then search for partial matches
    C1 = textIn.index('Cited by ')

    Tguess = textIn.rfind('data-clk-atid',0,C1)

    tempStr = textIn[Tguess:C1]

    indTag = tempStr.index('')

    return tempStr[0:indTag], tempStr

def makeUrl(strIn):
    ## just replace all the spaces with "+" and return the string
    #url template =https://scholar.google.com/scholar?q=Astrophys.J.679:1272-1287,2008&hl=en&as_sdt=0,48

    tempStr =  strIn.replace(' ','+')

    return 'https://scholar.google.com/scholar?q=' + tempStr + '&hl=en&as_sdt=0,48'

def popList(listIn,val):
    for i in range(len(listIn)):
        listIn[i] = val

    return listIn

def mainLoop(dfIn,saveDir,client,chckPntNum):

    def setupSave(saveDir,outDict):
        now = datetime.now()
        dt = now.strftime("%d/%m/%Y %H:%M:%S")
        dt = dt.replace('/','-')
        dt = dt.replace(':','_')
        saveOutFile2(saveDir,outDict,dt)

    outDict = {}
    sleep = False
    calls = 0
    for i, row in dfIn.iterrows():
        print(row['title'])
        if i%chckPntNum== 0:
            setupSave(saveDir,outDict) ### saving every 1000 times in case something crashes
            outDict = {}

        tempList = [[],[],[],[],[]]
        if sleep == True:
            time.sleep(0.3)
        try:

            url = makeUrl(row['jref'])

            result = client.get(url = url)
            calls = calls +1
            resStr = str(result)

            if resStr == '<Response [200]>':
                tempList[0] = row['title']
                tempList[1] = row['jref']
                tempList[2] = getCitedBy(result.text)
                #### modifying so that the extra text is also saved in the dict
                titleStr,extraTxt = checkTitle3(result.text)
                tempList[3] = titleStr
                tempList[4] = extraTxt
                #tempList[3] = checkTitle2(result.text)
                ###do the normal stuff
                #tempList = some functions
                outDict[row['ids']] = tempList
                pass

            elif resStr == '<Response [403]>':
                print('out of API requests!!!!!!!!!!!')
                break
            elif resStr == '<Response [429]>':
                print('requesting too fast')
                sleep = True

            else:
                tempList = popList(tempList,resStr)
                outDict[row['id']] = tempList

        except:

            print('exception')
            error = str(("Unexpected error: " +  str(sys.exc_info()[0])))
            print(error)

            tempList = popList(tempList,error)
            outDict[row['ids']] = tempList

    setupSave(saveDir,outDict)
    print('num API calls is ' + str(calls))
    return outDict,calls

#testing Phys.Lett.B653:434-438,2007
#url template =https://scholar.google.com/scholar?q=Astrophys.J.679:1272-1287,2008&hl=en&as_sdt=0,48

def findStrDF(df,col,strIn):
    return None

def findStrList(listIn,strIn):
    outList = []

    for thing in listIn:
        if strIn in thing:
            outList.append(thing)

    return outList

def dateHist(jrefs,dates):
    # outList = []
    # for i in range(len(dates)):
    #     outList.append([])

    outDict = {}
    for i,item in enumerate(dates):
        outDict[item] = 0

    for thing in jrefs:
        for t in dates:
            if str(t) in thing:
                outDict[t] = outDict[t] +1
                break
    return outDict

def testErrorCode(url):

    client = ScraperAPIClient('XXXXXXXXXXXXXXXXXXXXXX')
    return client.get(url = url)

def writeBasicTxt(strIn,fileName):
    fileName = fileName + ".txt"
    strIn = str(strIn)

    f = open(fileName, "a")
    f.write(strIn)
    f.close()

if __name__ == "__main__":
    saveDir = 'prevDone'
    client = ScraperAPIClient('XXXXXXXXXXXXXXXXXXXXXXXXX')
    ####
    num = 10**6
    chckPntNum = 2000
    for file in os.listdir(os.getcwd()):
        if file.endswith('.csv') and 'dfSplitInput' in file:
            fileName = file

    df = importInput(fileName)
    df = df.astype(str)
    dictIn = df.to_dict('index')

    returnedDict = makeScrapInput2(dictIn,saveDir,num)
    dfIn = pd.DataFrame(returnedDict).transpose()
    del df
    start_time = time.time()

    #call setup funcs
    #make saveDir if not there

    outDict,calls = mainLoop(dfIn,saveDir,client,chckPntNum)

    exeTime = (time.time() - start_time)

    print('exe time was ' + str(exeTime))

    now = datetime.now()
    dt = now.strftime("%d/%m/%Y %H:%M:%S")
    dt = dt.replace('/','-')
    dt = dt.replace(':','_')

    fileName = 'runTime ' + dt

    writeBasicTxt(exeTime,fileName)

##################### functions written for ad-hoc checking of results and looking for errors
def adHocFindErr(dic):
    outList = []
    for key in dic:
        if "Unexpected error: <class 'ValueError'>" in dic[key][0]:
            outList.append(key)

    return outList

def findErIds(listIn,df):
    listOut = []
    for i, row in df.iterrows():
        for ID in listIn:
            if ID == row['ids']:
                listOut.append([row['ids'],row['jref'],row['title']])

    return listOut
############## end functions written for ad-hoc checking.........


import math
import copy
import numpy as np
from random import randrange
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

class Node:
    def __init__(self, probs, state, probs_mask, reward, terminal, parent = None):
        self.state = state
        self.P = probs * probs_mask
        probs_sum = self.P.sum().item()
        length = len(self.P)

        if probs_sum == 0.0:
            mask_sum = probs_mask.sum().item()
            if mask_sum == 0.0:
                self.P = torch.ones(length, dtype = torch.float32)
            else:
                self.P = probs_mask * (1.0 / mask_sum)
        else:
            self.P = self.P / probs_sum

        self.N = torch.zeros(length, dtype = torch.int16)
        self.Q = torch.zeros(length, dtype = torch.float32)
        self.R = reward
        self.T = terminal

        self.children = {}
        self.parent = parent
class MCTS:
    def __init__(self, root, env, c1, c2, gamma):
        self.root = root
        self.env = env

        self.c1 = c1
        self.c2 = c2
        self.gamma = gamma

    def selection(self):
        node = self.root
        actions = []

        while True:
            if node.T == True:
                return node, -1, actions

            sum = node.N.sum().item()
            if sum > 0:
                c = self.c1 + math.log((sum + self.c2 + 1.0) / self.c2)
                u = node.Q + node.P * c * (math.sqrt(sum) / (1.0 + node.N))

                u_max = -10000
                a = 0
                for i in range(len(u)):
                    if node.P[i] > 0.0:
                        if u[i] > u_max:
                            a = i
                            u_max = u[i]
            else:
                a = torch.argmax(node.P).item()
            actions.append(a)

            if a in node.children:
                node = node.children[a]
            else:
                return node, a, actions
    def expansion(self, node, action, probs, state, probs_mask, reward, terminal):
        new_node = Node(probs, state, probs_mask, reward, terminal, node)
        node.children[action] = new_node
        return new_node
    def backup(self, node, value, actions):
        v = node.R + self.gamma * value * (1 - int(node.T))
        actions.reverse()

        for a in actions:
            node = node.parent
            v = node.R - self.gamma * v
            node.Q[a] = (node.N[a] * node.Q[a] + v) / (node.N[a] + 1)
            node.N[a] += 1
class Game:
    def __init__(self, states, values, policy):
        self.states = states
        self.target_values = values
        self.target_policy = policy
class ReplayBuffer:
    def __init__(self, max_size):
        self.max_size = max_size
        self.buffer = []

    def store(self, game):
        if len(self.buffer) > self.max_size:
            self.buffer.pop(0)
        self.buffer.append(game)

    def sample(self, batch_size):
        length = len(self.buffer)
        game_indexes = np.random.choice(length, batch_size)

        states, target_values, target_policy = [], [], []
        for index in game_indexes:
            game = self.buffer[index]
            action_index = randrange(len(game.states))
            states.append(game.states[action_index])
            target_values.append(game.target_values[action_index])
            target_policy.append(game.target_policy[action_index])

        return torch.stack(states), torch.stack(target_values), torch.stack(target_policy)

class AZAgent:
    def __init__(self, env, model, replay_buffer, actions, simulation_count = 800, gamma = 0.997, training_steps = 5000, c1 = 1.25, c2 = 19652.0, temperature_update_steps = 100000,
                 dirichlet_alpha = 0.1, exploration_fraction = 0.25, weight_decay = 0.0001, lr = 2e-2, arena_count = 25, self_play_steps = 100, batch_size = 512, name = 'az',
                 run_iteration = 1000, steps = 0):
        self.env = env
        self.model = model
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        print('device: ', self.device)
        self.model.to(self.device)
        self.weight_decay = weight_decay
        self.optimizer = optim.Adam(self.model.parameters(), lr=lr, weight_decay = weight_decay)

        self.replay_buffer = replay_buffer
        self.actions = actions

        self.dirichlet_alpha = dirichlet_alpha
        self.exploration_fraction = exploration_fraction

        self.simulation_count = simulation_count
        self.arena_count = arena_count
        self.self_play_steps = self_play_steps
        self.training_steps = training_steps
        self.steps = steps
        self.batch_size = batch_size
        self.run_iteration = run_iteration + 1

        self.name = name

        self.c1 = c1
        self.c2 = c2
        self.gamma = gamma
        self.lr = lr

        self.T = 1
        self.temperature_update_steps = temperature_update_steps
        self.update_parameters()

    def update_parameters(self):
        if self.steps == 3 * self.temperature_update_steps:
            self.T = 0.25
            self.lr = 1e-5
            self.optimizer = optim.Adam(self.model.parameters(), lr=self.lr, weight_decay = self.weight_decay)
        elif self.steps == 2 * self.temperature_update_steps:
            self.T = 0.5
            self.lr = 1e-4
            self.optimizer = optim.Adam(self.model.parameters(), lr=self.lr, weight_decay = self.weight_decay)
        elif self.steps == self.temperature_update_steps:
            self.lr = 1e-3
            self.optimizer = optim.Adam(self.model.parameters(), lr=self.lr, weight_decay = self.weight_decay)

    def run_mcts(self, state, moves, env, model, training = False):
        logit, value = model.forward(state.unsqueeze(0).to(self.device).float())

        probs = F.softmax(logit.cpu(), dim=-1)
        if training == True:
            noise = torch.tensor(np.random.gamma(self.dirichlet_alpha, 1, self.actions))
            probs = probs[0] * (1 - self.exploration_fraction) + noise * self.exploration_fraction
        else:
            probs = probs[0]

        root = Node(probs, state.cpu(), moves, 0, False)
        mcts = MCTS(root, env, self.c1, self.c2, self.gamma)

        for simulation in range(self.simulation_count):
            node, a, actions = mcts.selection()
            if a > -1:
                new_state, reward, terminal, new_moves = env.step(a, node.state)
                if terminal:
                    probs, value = torch.tensor(self.actions).float(), 0
                else:
                    logit, value = model.forward(new_state.unsqueeze(0).to(self.device).float())
                    probs = F.softmax(logit.cpu(), dim=-1)[0]
                    value = value.cpu().item()

                new_node = mcts.expansion(node, a, probs, new_state, new_moves, reward, terminal)
                mcts.backup(new_node, value, actions)
            else:
                mcts.backup(node, 0, actions)

        print('Q', mcts.root.Q)
        print('p', mcts.root.P)
        return mcts.root.N

    def play(self, model1, model2):
        terminal = False
        state, moves = self.env.start()
        player = 1

        states, rewards, target_policy = [], [], []

        logs = ''
        while not terminal:
            with torch.no_grad():
                if player == 1:
                    visit_childs = self.run_mcts(state, moves, self.env, model1)
                else:
                    visit_childs = self.run_mcts(state, moves, self.env, model2)

            temperature_coef = 1.0/self.T
            visit_childs = visit_childs.float()**temperature_coef
            sum = visit_childs.sum().item()
            probs = visit_childs / sum
            action = torch.argmax(probs).item()

            logs += str(action) + ';' + str(probs) + '\n'

            state, reward, terminal, moves = self.env.step(action, state)
            player = - player

            states.append(state.float())
            rewards.append(reward)
            target_policy.append(probs)

        length = len(rewards)
        target_values = torch.zeros(length)

        value = 0
        for i in reversed(range(length)):
            value = rewards[i] - self.gamma * value
            target_values[i] = value

        self.replay_buffer.store(Game(states, target_values, target_policy))

        return reward, logs

    def self_play(self):
        terminal = False
        states, rewards, target_policy = [], [], []
        state, moves = self.env.start()
        while not terminal:
            with torch.no_grad():
                visit_childs = self.run_mcts(state, moves, self.env, self.model, True)
            temperature_coef = 1.0/self.T
            visit_childs = visit_childs.float()**temperature_coef
            sum = visit_childs.sum().item()
            probs = visit_childs / sum
            action = probs.multinomial(num_samples=1).detach().item()

            new_state, reward, terminal, moves = self.env.step(action, state)

            states.append(state.float())
            rewards.append(reward)
            target_policy.append(probs)
            state = new_state

        length = len(rewards)
        target_values = torch.zeros(length)

        value = 0
        for i in reversed(range(length)):
            value = rewards[i] - self.gamma * value
            target_values[i] = value

        self.replay_buffer.store(Game(states, target_values, target_policy))
        return rewards[length-1]

    def learn(self, batch_size):
        states, target_values, target_policy = self.replay_buffer.sample(batch_size)

        logits, values = self.model.forward(states.to(self.device))
        log_probs = F.log_softmax(logits.cpu(), dim=-1)[0]
        values = values.cpu()[0]

        loss_value = (values - target_values)**2
        loss_policy = - (target_policy * log_probs)
        loss = loss_value.mean() + loss_policy.mean()

        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()

    def arena(self, model1, model2, iteration):
        wins1, wins2, draft = 0, 0, 0
        text = ''
        min_iteration = max(0, iteration - 10)
        game = 1

        for i in range(min_iteration, iteration):
            print(game)
            model2.load_state_dict(torch.load('models/' + self.name + str(i) + '.pt'))

            text += 'arena game ' + str(game) + '\n'
            result, logs = self.play(model1, model2)
            text += logs

            if result == 1:
                wins1 += 1
            elif result == 0:
                draft += 1
            else:
                wins2 += 1

            game += 1
            text += 'arena game ' + str(game) + '\n'
            result, logs = self.play(model2, model1)
            text += logs

            if result == 1:
                wins2 += 1
            elif result == 0:
                draft += 1
            else:
                wins1 += 1

            game += 1

        print('win 1: ', wins1, 'win 2:', wins2, 'draft: ', draft)
        return wins1 / float(wins1 + wins2 + 1e-10), text

    def run(self):
        for iteration in range(1, self.run_iteration):
            print('iteration: ', iteration)

            for i in range(self.self_play_steps):
                if i % 5 == 0:
                    print('self play ', i)
                self.self_play()

            torch.save(self.model.state_dict(), 'models/' + self.name + str(iteration - 1) + '.pt')

            for i in range(self.training_steps):
                if i % 250 == 0:
                    print('training ', i)
                self.learn(self.batch_size)

            self.steps += self.training_steps
            self.update_parameters()

            model2 = copy.deepcopy(self.model)
            model2.to(self.device)

            print('arena')
            ratio, text = self.arena(self.model, model2, iteration)
            print('arena ratio: ', ratio)

            f = open('logs/az/arena' + str(iteration) + '.txt', "w")
            f.write(text)
            f.close()

            del model2

    def run2(self):
        terminal = False
        rewards, target_policy = [], []
        state, moves = self.env.start2()
        for y in range(6):
            line = ' '
            for x in range(6):
                if state[0, y, x] == 1:
                    line += ' X '
                elif state[1, y, x] == 1:
                    line += ' O '
                else:
                    line += ' _ '
            print(line)

        print('m', moves.reshape(6, 6))
        while not terminal:
            with torch.no_grad():
                visit_childs = self.run_mcts(state, moves, self.env, self.model, False)

            temperature_coef = 1.0/self.T
            print('visit', visit_childs)
            visit_childs = visit_childs.float()**temperature_coef
            sum = visit_childs.sum().item()
            probs = visit_childs / sum
            print('visits probs', probs)
            action = probs.multinomial(num_samples=1).detach().item()

            new_state, reward, terminal, moves = self.env.step(action, state)

            rewards.append(reward)
            state = new_state

            for y in range(6):
                line = ' '
                for x in range(6):
                    if state[0, y, x] == 1:
                        line += ' X '
                    elif state[1, y, x] == 1:
                        line += ' O '
                    else:
                        line += ' _ '
                print(line)

            return -1

        length = len(rewards)
        target_values = torch.zeros(length)

        value = 0
        for i in reversed(range(length)):
            value = rewards[i] - self.gamma * value
            target_values[i] = value

        return rewards[length-1]


# https://leetcode.com/problems/reordered-power-of-2/

def compare_str(str_1, str_2):
    char_count = {}
    for c in str_1:
        if c not in char_count:
            char_count[c] = 1
        else:
            char_count[c] += 1

    for c in str_2:
        if c not in char_count:
            return False
        else:
            char_count[c] -= 1

    for c in char_count:
        if char_count[c] != 0:
            return False

    return True

class Solution(object):
    def reorderedPowerOf2(self, N):
        """
        :type N: int
        :rtype: bool
        """
        N = str(N)
        n = len(N)
        two_power = set()
        i = 0
        while True:
            temp = 2 ** i
            if temp < 10 ** (n - 1):
                i += 1
                continue
            if temp > 10 ** n:
                break
            two_power.add(str(temp))
            i += 1

        # print('two power set = {}'.format(two_power))

        for num in two_power:
            if compare_str(N, num):
                return True

        return False

if __name__ == '__main__':
    s = Solution()

    test = [0,1, 2, 3, 46, 23, 32, 64, 281, 280, 1024, 2401, 65536, 55663]
    for t in test:
        print('{}  ->  {}'.format(t, s.reorderedPowerOf2(t)))

    print(compare_str('324', '234'))


from utils import enc, dec
from biliob import BiliobSpider
from time import sleep
from datetime import datetime
from datetime import timedelta
url = 'https://space.bilibili.com/ajax/member/getSubmitVideos?mid={}&pagesize=10&page=1&order=pubdate'
url = 'https://api.bilibili.com/x/space/arc/search?mid={}&ps=100&tid=0&pn=1&keyword=&order=pubdate'

class AddPublicVideoSpider(BiliobSpider):
  def __init__(self):
    super().__init__(name='追加作者最新上传的视频')

  def gen_url(self):
    while True:
      try:
        cursor = self.db.author.find({}, {'mid': 1}).batch_size(20)
        try:
          for each_author in cursor:
            yield url.format(each_author['mid'])
        except Exception as e:
          self.logger.exception(e)
          sleep(1)
      except Exception as e:
        self.logger.exception(e)
        sleep(1)

  def parse(self, res):
    try:
      j = res.json()
      result = []
      if 'data' not in j or 'list' not in j['data'] or 'vlist' not in j['data']['list']:
        return None
      bvid = None
      aid = None
      for each_video in j['data']['list']['vlist']:
        if 'bvid' in each_video:
          bvid = each_video['bvid']
        if 'aid' in each_video:
          aid = each_video['aid']
        result.append([bvid, aid])
      return result
    except Exception as e:
      self.logger.exception(e)

  def update_video_interval(self, interval: int, aid, bvid):
    now = datetime.utcnow() + timedelta(hours=8)
    if aid == None:
      aid = enc(bvid)
    if bvid == None:
      bvid = dec(aid)
    return {
        'next': now,
        'interval': interval,
        'aid': aid,
        'bvid': bvid
    }

  def save(self, items):
    for [bvid, aid] in items:
      bvid = bvid.lstrip('BV')
      interval_data = self.db.video_interval.find_one(
          {'bvid': bvid, 'aid': aid})
      if interval_data == None:
        self.db.video_interval.update_one(
            {'bvid': bvid, 'aid': aid}, {'$set': self.update_video_interval(3600 * 12, aid, bvid), '$setOnInsert': {'date': datetime.utcnow() + timedelta(hours=8)}}, upsert=True)

    return items

s = AddPublicVideoSpider()

if __name__ == "__main__":

  s.run()


from django.urls import path, include
from .views import StadiumsViewSet, StadiumSeatsView

from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'', StadiumsViewSet, basename='seats')

router_seats = DefaultRouter()
router_seats.register(r'', StadiumSeatsView, basename='seats')

urlpatterns = [

    # STADIUMS
    path('', include(router.urls)),
    path('seats/<int:stadium>/', include(router_seats.urls)),
    # path('stadium/matches/<int:pk>', StadiumDeleteView.as_view()),

]


#!/usr/bin/env python
from string import Template

class Templator:
	templateDict = None
	addsubandor = """@SP
A=M-1
D=M
A=A-1
D=M%sD
M=D
@SP
M=M-1
0
0
0
0
0
"""
	pushconst = """@$var
D=A
@SP
A=M
M=D
@SP
M=M+1
0
0
0
0
0
"""
	ltgteq = """@SP
A=M-1
D=M
A=A-1
D=M-D
@13
M=-1
@$nlabel
D; %s
@13
M=0
($nlabel)
@13
D=M
@SP
A=M-1
A=A-1
M=D
@SP
M=M-1
0
0
0
0
0
"""
	negnot = """@SP
A=M-1
M=%sM
0
0
0
0
0
"""
	pushvar = """@%s
D=M
@$var
A=D+A
D=M
@SP
A=M
M=D
@SP
M=M+1
0
0
0
0
0
"""
	pushstat = """@$file.$var
D=M
@SP
A=M
M=D
@SP
M=M+1
"""
	pushtemp = """@5
D=A
@$var
A=D+A
D=M
@SP
A=M
M=D
@SP
M=M+1
"""
	popvar = """@SP
AM=M-1
D=M
@13
M=D
@%s
D=M
@$var
D=D+A
@14
M=D
@13
D=M
@14
A=M
M=D
0
0
0
0
0
"""
	popstat = """@SP
AM=M-1
D=M
@$file.$var
M=D
"""
	poptemp = """@SP
AM=M-1
D=M
@13
M=D
@5
D=A
@$var
D=D+A
@14
M=D
@13
D=M
@14
A=M
M=D
"""
	pop = """@SP
M=M-1
0
0
0
0
0
"""
	popptr = """@SP
AM=M-1
D=M
@$var
M=D
0
0
0
0
0
"""
	pushptr = """@$var
D=M
@SP
A=M
M=D
@SP
M=M+1
0
0
0
0
0
"""
	label = "($var)\n"
	ifgoto = """@SP
AM=M-1
D=M
@$var
D; JNE
0
0
0
0
0
"""
	goto = "@$var\n0; JMP\n"
	functionDef = """($var)
$initLcls
0
0
0
0
0
"""
	returnCode = """@SP
A=M-1
D=M
@13
M=D	// 13 holds the return VALUE
0 // set SP to lcl-1->that
0
@LCL
D=M-1
@SP
M=D
0 // restore that
@SP
AM=M-1	// decrement the stack pointer: SP->THIS
A=A+1	// A = SP (pre-decrement) -> THAT
D=M
@THAT
M=D
0 // restore this
0
@SP
AM=M-1	// decerement SP : SP->ARG
A=A+1	// A = SP -> THIS
D=M
@THIS
M=D
0 // save arg in R14, restore arg
0
@ARG
D=M
@14
M=D	// 14 now holds arg
@SP
AM=M-1	// decrement SP
A=A+1
D=M
@ARG
M=D
0 // restore lcl
0
@SP
AM=M-1
A=A+1
D=M
@LCL
M=D
0 // save return address in 15
0
@SP
A=M
D=M
@15
M=D
0 // restore SP
0
@14
D=M
@SP
M=D
0 // save return VALUE onto the stack
0
@13
D=M
@SP
AM=M+1
A=A-1
M=D
0 // jump to return ADDRESS
0
@15
A=M
0; JMP
"""
	callCode = """@ARG
D=M
@13
M=D // save current ARG in 13
@SP
D=M
@$var
D=D-A
@ARG
M=D // ARG = current SP - nargs
@13
D=M
@SP
A=M+1
A=A+1
M=D
0	// SP will now be pointing to the return address location
@$retAddr
D=A
@SP
AM=M+1
A=A-1
M=D
@LCL // save LCL
D=M
@SP
A=M
M=D	// put LCL on the stack
@SP // Set LCL to the beginning of the locals
D=M+1
D=D+1
D=D+1
D=D+1
@LCL
M=D
@SP
M=M+1
M=M+1
@THIS // save THIS
D=M
@SP
A=M
M=D	// put THIS on the stack
@SP
M=M+1
@THAT // save THAT
D=M
@SP
A=M
M=D	// put THAT on the stack
@SP
M=M+1
@$jmpAddr
0; JMP
($retAddr)
0
0
0
0
0
"""

	def __init__(self):
		if (not self.templateDict):
			self.templateDict = {
				'add'			: Template(self.addsubandor % '+'),
				'sub' 			: Template(self.addsubandor % '-'),
				'lt'			: Template(self.ltgteq % 'JLT'),
				'gt'			: Template(self.ltgteq % 'JGT'),
				'eq'			: Template(self.ltgteq % 'JEQ'),
				'neg'			: Template(self.negnot % '-'),
				'not'			: Template(self.negnot % '!'),
				'or'			: Template(self.addsubandor % '|'),
				'and'			: Template(self.addsubandor % '&'),
				'pop'			: Template(self.pop),
				'pushconstant'	: Template(self.pushconst),
				'pushargument'	: Template(self.pushvar % "ARG"),
				'pushlocal'		: Template(self.pushvar % "LCL"),
				'pushstatic'	: Template(self.pushstat),
				'pushthat'		: Template(self.pushvar % "THAT"),
				'pushthis'		: Template(self.pushvar % "THIS"),
				'pushargument'	: Template(self.pushvar % "ARG"),
				'pushtemp'		: Template(self.pushtemp),
				'popargument'	: Template(self.popvar % "ARG"),
				'poplocal'		: Template(self.popvar % "LCL"),
				'popstatic'		: Template(self.popstat),
				'popthat'		: Template(self.popvar % "THAT"),
				'popthis'		: Template(self.popvar % "THIS"),
				'popargument'	: Template(self.popvar % "ARG"),
				'poptemp'		: Template(self.poptemp),
				'poppointer'	: Template(self.popptr),
				'pushpointer'	: Template(self.pushptr),
				'label'			: Template(self.label),
				'if-goto'		: Template(self.ifgoto),
				'goto'			: Template(self.goto),
				'function'		: Template(self.functionDef),
				'return'		: Template(self.returnCode),
				'call'			: Template(self.callCode)
			}
	def lookup(self, key):
		return self.templateDict[key]


from pycassa.types import Column

__all__ = ['ColumnFamilyMap']

def create_instance(cls, **kwargs):
    instance = cls()
    instance.__dict__.update(kwargs)
    return instance

def combine_columns(column_dict, columns):
    combined_columns = {}
    for column, type in column_dict.iteritems():
        combined_columns[column] = type.default
    for column, value in columns.iteritems():
        combined_columns[column] = column_dict[column].unpack(value)
    return combined_columns

class ColumnFamilyMap(object):
    def __init__(self, cls, column_family, columns=None):
        """
        Construct a ObjectFamily

        Parameters
        ----------
        cls      : class
            Instances of cls are generated on get*() requests
        column_family: ColumnFamily
            The ColumnFamily to tie with cls
        """
        self.cls = cls
        self.column_family = column_family

        self.columns = {}
        for name, column in self.cls.__dict__.iteritems():
            if not isinstance(column, Column):
                continue

            self.columns[name] = column

    def get(self, key, *args, **kwargs):
        """
        Fetch a key from a Cassandra server

        Parameters
        ----------
        key : str
            The key to fetch
        columns : [str]
            Limit the columns fetched to the specified list
        column_start = str
            Only fetch when a column is >= column_start
        column_finish = str
            Only fetch when a column is <= column_finish
        column_reversed = bool
            Fetch the columns in reverse order. Currently this does nothing
            because columns are converted into a dict.
        column_count = int
            Limit the number of columns fetched per key
        include_timestamp = bool
            If true, return a (value, timestamp) tuple for each column

        Returns
        -------
        Class instance
        """
        if 'columns' not in kwargs:
            kwargs['columns'] = self.columns.keys()
        columns = self.column_family.get(key, *args, **kwargs)
        columns = combine_columns(self.columns, columns)
        return create_instance(self.cls, key=key, **columns)

    def multiget(self, *args, **kwargs):
        """
        Fetch multiple key from a Cassandra server

        Parameters
        ----------
        keys : [str]
            A list of keys to fetch
        columns : [str]
            Limit the columns fetched to the specified list
        column_start = str
            Only fetch when a column is >= column_start
        column_finish = str
            Only fetch when a column is <= column_finish
        column_reversed = bool
            Fetch the columns in reverse order. Currently this does nothing
            because columns are converted into a dict.
        column_count = int
            Limit the number of columns fetched per key
        include_timestamp = bool
            If true, return a (value, timestamp) tuple for each column

        Returns
        -------
        {'key': Class instance}
        """
        if 'columns' not in kwargs:
            kwargs['columns'] = self.columns.keys()
        kcmap = self.column_family.multiget(*args, **kwargs)
        ret = {}
        for key, columns in kcmap.iteritems():
            columns = combine_columns(self.columns, columns)
            ret[key] = create_instance(self.cls, key=key, **columns)
        return ret

    def get_count(self, *args, **kwargs):
        """
        Count the number of columns for a key

        Parameters
        ----------
        key : str
            The key with which to count columns

        Returns
        -------
        int Count of columns
        """
        return self.column_family.get_count(*args, **kwargs)

    def get_range(self, *args, **kwargs):
        """
        Get an iterator over keys in a specified range

        Parameters
        ----------
        start : str
            Start from this key (inclusive)
        finish : str
            End at this key (inclusive)
        columns : [str]
            Limit the columns fetched to the specified list
        column_start = str
            Only fetch when a column is >= column_start
        column_finish = str
            Only fetch when a column is <= column_finish
        column_reversed = bool
            Fetch the columns in reverse order. Currently this does nothing
            because columns are converted into a dict.
        column_count = int
            Limit the number of columns fetched per key
        row_count = int
            Limit the number of rows fetched
        include_timestamp = bool
            If true, return a (value, timestamp) tuple for each column

        Returns
        -------
        iterator over Class instance
        """
        if 'columns' not in kwargs:
            kwargs['columns'] = self.columns.keys()
        for key, columns in self.column_family.get_range(*args, **kwargs):
            columns = combine_columns(self.columns, columns)
            yield create_instance(self.cls, key=key, **columns)

    def insert(self, instance, columns=None):
        """
        Insert or update columns for a key

        Parameters
        ----------
        instance : Class instance
            The key to insert or update the columns at
        columns : ['column']
            Limit the columns inserted to this list

        Returns
        -------
        int timestamp
        """
        insert_dict = {}
        if columns is None:
            columns = self.columns.keys()

        for column in columns:
            insert_dict[column] = self.columns[column].pack(instance.__dict__[column])

        return self.column_family.insert(instance.key, insert_dict)

    def remove(self, instance, column=None):
        """
        Remove this instance

        Parameters
        ----------
        instance : Class instance
            Remove the instance where the key is instance.key
        column : str
            If set, remove only this column

        Returns
        -------
        int timestamp
        """
        # Hmm, should we only remove the columns specified on construction?
        # It's slower, so we'll leave it out.
        return self.column_family.remove(instance.key, column)


import json

from charmhelpers.core import hookenv

from charms.reactive import hook
from charms.reactive import scopes
from charms.reactive import RelationBase

class TlsRequires(RelationBase):
    '''The class that requires a TLS relationship to another unit.'''
    # Use the gloabal scope for requires relation.
    scope = scopes.GLOBAL

    @hook('{requires:tls-certificates}-relation-joined')
    def joined(self):
        '''When joining with a TLS provider request a certificate..'''
        # Get the conversation scoped to the unit.
        conversation = self.conversation()
        conversation.set_state('{relation_name}.available')

    @hook('{requires:tls-certificates}-relation-changed')
    def changed(self):
        '''Only the leader should change the state to sign the request. '''
        # Get the global scoped conversation.
        conversation = self.conversation()
        # When the conversation has a CA set notify that the ca is available.
        if conversation.get_remote('ca'):
            conversation.set_state('{relation_name}.ca.available')
        # When the client.cert has a value notify that the client is available.
        if conversation.get_remote('client.cert'):
            conversation.set_state('{relation_name}.client.cert.available')
        # Get the name of the unit this code is running on.
        name = hookenv.local_unit().replace('/', '_')
        # Prefix the key with the name so each unit is notified cert available.
        if conversation.get_remote('{0}.server.cert'.format(name)):
            conversation.set_state('{relation_name}.server.cert.available')

    @hook('{provides:tls-certificates}-relation-{broken,departed}')
    def broken_or_departed(self):
        '''Remove the states that were set.'''
        conversation = self.conversation()
        conversation.remove_state('{relation_name}.available')

    def get_ca(self):
        '''Return the certificate authority from the relation object.'''
        # Get the global scoped conversation.
        conversation = self.conversation()
        # Find the certificate authority by key, and return the value.
        return conversation.get_remote('ca')

    def get_client_cert(self):
        '''Return the client certificate and key from the relation object.'''
        conversation = self.conversation()
        client_cert = conversation.get_remote('client.cert')
        client_key = conversation.get_remote('client.key')
        return client_cert, client_key

    def get_server_cert(self):
        '''Return the server certificate and key from the relation objects.'''
        conversation = self.conversation()
        # Get the name of the unit this code is running on.
        name = hookenv.local_unit().replace('/', '_')
        # Prefix the keys with name so each unit can get unique certs and keys.
        server_cert = conversation.get_remote('{0}.server.cert'.format(name))
        server_key = conversation.get_remote('{0}.server.key'.format(name))
        return server_cert, server_key

    def request_server_cert(self, cn, sans, cert_name):
        '''Set the CN, list of sans, and certifiicate name on the relation to
        request a server certificate.'''
        conversation = self.conversation()
        # A server certificate requires a CN, sans, and a certificate name.
        conversation.set_remote('common_name', cn)
        conversation.set_remote('sans', json.dumps(sans))
        conversation.set_remote('certificate_name', cert_name)


__license__ = """
Copyright 2012 DISQUS

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from unittest import TestCase

from samsa.config import ConsumerConfig

class Configured(object):

    def __init__(self, **kwargs):
        self.config = ConsumerConfig.build(kwargs)

class TestConfig(TestCase):

    def test_default(self):
        obj = Configured()

        self.assertEquals(obj.config['socket_buffersize'], ConsumerConfig.socket_buffersize)

    def test_override(self):

        obj = Configured(autocommit_enable=False)
        self.assertFalse(obj.config['autocommit_enable'])

    def test_raises(self):

        self.assertRaises(AttributeError, Configured, foo='bar')


import sys, pickle

def pBan(data):
	for line in data:
		for char, count in line:
			sys.stdout.write(char * count)
		sys.stdout.write('\n')

def pLoad(name):
	f = open(name)
	out = pickle.loads(f.read())
	f.close()
	return out

if __name__ == '__main__':
	pBan(pLoad(sys.argv[1]))


# coding: utf-8

import numpy as np
import statsmodels.api as sm
from scipy import stats
import pandas as pd
from statsmodels.graphics.api import qqplot

df = data
df.index = df['Start date']

dfOneSlice = df.loc[(df['Start Station'] == 'Ohio Dr & West Basin Dr SW / MLK & FDR Memorials') | (df['Start Station'] == 'US Dept of State / Virginia Ave & 21st St NW') |(df['Start Station'] == '21st St & Constitution Ave NW') |(df['Start Station'] == 'Lincoln Memorial') ]
dfOneSlice.head()

start_date = datetime.datetime(2012,1,1,0,0,0,0); date=start_date; t = datetime.timedelta(minutes = 15)
end_date = datetime.datetime(2012,1,8,0,0,0,0);
dfOneSlice_week1 = dfOneSlice.loc[(dfOneSlice['Start date'] >= start_date) & (dfOneSlice['End date'] < end_date)]
rideCounts = []
rideTimes = []
date = start_date;

t= datetime.timedelta(minutes = 15)
while date != end_date:
    date = date + t;
    rideTimes.append(start_date)
    rideCounts.append(len(dfOneSlice_week1.loc[(dfOneSlice_week1['Start date'] >=start_date)& (dfOneSlice_week1['Start date'] < date)].index))
    start_date = date;

series = pd.DataFrame({'times': rideTimes, 'count': rideCounts})
series.index = series['times']

series = series.drop('times',1)

arma_mod = sm.tsa.ARMA(series, (20,0))
arma_res = arma_mod.fit()

prediction = arma_res.predict('2012-01-01 00:00:00', '2012-01-08 00:00:00')
fig, ax = plt.subplots(figsize=(12, 8))
ax = series.ix['2012-01-01 00:00:00':].plot(ax=ax)
fig = arma_res.plot_predict('2012-01-01 00:00:00', '2012-01-08 00:00:00', ax=ax, plot_insample=True)


import sys

from pyspark import SparkContext, SparkConf
from pyspark.sql import SparkSession, SQLContext
from pca.pca_model_utils_demo import get_predictors

def main(site, sector, threshold_factor, s3_output_path):
    conf = SparkConf().setAppName("PCA predictions for demo cells")
    sc = SparkContext.getOrCreate(conf)

    spark = SparkSession(sc)
    sqlContext = SQLContext(sc)

    # Cell counters data source
    sectorDf = sqlContext.read.parquet("s3://lte-data-ingestion/parquet/cell_counters_v3/site="
                                       + site + "/sector=" + sector)

    sectorDf.createOrReplaceTempView(sector + "_counters")
    allSectorCountersDf = spark.sql("SELECT * FROM " + sector + "_counters")

    # Get incident dates for site
    incidentsDf = sqlContext.read.csv(
        "s3://aws-athena-query-results-293603929259-us-west-2/all_incidents/2018/06/16/all_incidents.csv",
        header=True)

    incidentsDf.createOrReplaceTempView("all_incidents")

    # The naming of select cols is prefixed with 'i' to avoid ambiguity for future ops.
    siteIncidentsSql = get_incidents_sql().format(site, sector)

    allSiteIncidentsDf = spark.sql(siteIncidentsSql)

    # Join incidents with site counters
    cond = [allSectorCountersDf.austs == allSiteIncidentsDf.iAusts]

    # Map side join
    allSectorCountersDf = allSectorCountersDf.join(allSiteIncidentsDf.hint("broadcast"), cond, 'left_outer')

    # Here comes the pandas
    predictorsDf = get_predictors(sector, allSectorCountersDf.toPandas(), threshold_factor)

    if len(predictorsDf) > 0:
        spark.createDataFrame(predictorsDf).repartition(1).write.csv(
            's3n://' + s3_output_path + ' /site=' + site + '/sector=' + sector, header=True)
    sc.stop()

def get_incidents_sql():
    return """
        select  ausTimestamp AS iAusts,
                regexp_extract(moidleaf, 'EUtranCellFDD=(.+)$', 1) AS iSector,
            CASE
                WHEN locked_incident = '1' THEN 'locked'
                WHEN downtime_incident='1' THEN 'downtime'
                WHEN rrc_emergency_succ_incident='1' THEN 'rrc_emergency_succ'
                WHEN (rrc_succ_rate_incident='1' AND rrc_fail_volume_incident='1') THEN 'rrc_drop'
                WHEN (erab_drop_by_enb_and_mme_incident='1' AND erab_drop_fail_volume_incident='1') THEN 'erab_drop'
                WHEN (s1_estab_succ_incident='1' AND s1_estab_fail_volume_incident='1') THEN 's1_drope'
                WHEN (erab_init_estab_succ_incident='1' AND erab_init_estab_fail_volume_incident='1') THEN 'erab_init_drop'
                WHEN (volte_quality_ok_incident='1' AND volte_fail_volume_incident='1') THEN 'volte_quality_drop'
            END AS incident
        FROM all_incidents
        WHERE siteCode = '{0}'
            AND moidleaf = 'EUtranCellFDD={1}'
            AND (locked_incident = '1'
            OR downtime_incident='1'
            OR rrc_emergency_succ_incident='1'
            OR (rrc_succ_rate_incident='1' AND rrc_fail_volume_incident='1')
            OR (erab_drop_by_enb_and_mme_incident='1' AND erab_drop_fail_volume_incident='1')
            OR (s1_estab_succ_incident='1' AND s1_estab_fail_volume_incident='1')
            OR (erab_init_estab_succ_incident='1' AND erab_init_estab_fail_volume_incident='1')
            OR (volte_quality_ok_incident='1' AND volte_fail_volume_incident='1'))
        """

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print("Usage: pca_model  <site> <sector> <threshold-factor> <s3_output_path>", file=sys.stderr)
        exit(-1)

    main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])


# Importing the libraries
import numpy as np # for numerical, stats, matrix operation
import matplotlib.pyplot as plt # for plotting
import pandas as pd # for data manipulation

def df_info(df): # function to report some general info of a dataframe
	print("Printing general info:\n")
	print(df.columns)
#	print(df.dtypes)
	print(df.shape)
	print("Null check:\n")
	nullcheck = pd.isnull(df).sum()
	print(nullcheck[nullcheck>0])

# Importing the dataset
#col_names = ["ID","Diagnosis","radius","texture","perimeter","area","smoothness","compactness","concavity","cancave points","symmetry","fractal"]
df = pd.read_csv(r'diabetes.csv')

df_info(df)

#df['Glucose'][df['Glucose'] == 0] = np.nan
#df['Glucose'] = df['Glucose'].apply(lambda x: np.nan if x == 0.0) # tag
#df = df.dropna(axis = 0) # dropping the rows

# applying a lambda function onto a pandas Series

#def test(x): # x will just be one element in the Series
#    return 1

#df['Glucose'] = df['Glucose'].apply(lambda x:1)
#df.iloc[0,:] = df.iloc[0,:].apply(lambda x:1)

# Now, I want to apply a function to a dataframe

def subbymean(col_in): # function takes in a whole column
    col = col_in.copy()
    col_nonz = col[col != 0]
    zero_c = col[col == 0].count()
    real_mean = col_nonz.mean()
    real_std = col_nonz.std()
    random_list = np.random.uniform(real_mean - real_std, real_mean + real_std, size=zero_c)
    col[col == 0] = random_list
#    col[col == 0] = real_mean
    return col

def test(row):
    return row

cols_to_substitute = ['Glucose','BloodPressure', 'SkinThickness', 'Insulin', 'BMI']

df.loc[:,cols_to_substitute] = df.loc[:,cols_to_substitute].apply(subbymean, axis=0) # apply this function to a column

X = df.loc[:,['Glucose', 'BMI', 'DiabetesPedigreeFunction', 'Age']].values # Features
#X = df.iloc[:,:8].values # Features
y = df.iloc[:,8].values # outcome that we want to predict

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
# 25% will be test set, 75% will be training set

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

from sklearn.svm import SVC # SVC support vector classifier

clf = SVC(kernel = 'rbf',random_state = 0).fit(X_train, y_train) # hyperparameters to be tuned
y_pred = clf.predict(X_test)

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

#print(np.mean((y_test == clf.predict(X_test))))
#print(np.mean((y_train == clf.predict(X_train))))

y_pred_fake = np.zeros(192)

from sklearn.model_selection import cross_val_score # this is for cross-validation
scores = cross_val_score(clf, X_train, y_train, cv=10) # 10 partitions
print(scores.mean(),scores.std())

print("fake model accuracy: {}".format((y_test == y_pred_fake).mean()))

from sklearn.metrics import roc_auc_score, accuracy_score
auc_score = roc_auc_score(y_test, y_pred, average='macro')
auc_score_fake = roc_auc_score(y_test, y_pred_fake, average='macro')
print("real model AUC: {}".format(auc_score))
print("fake model AUC: {}".format(auc_score_fake))

from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier

knn = KNeighborsClassifier(n_neighbors = 5)
nb = GaussianNB()
svc = SVC()
lr = LogisticRegression()
DT = DecisionTreeClassifier()
RF = RandomForestClassifier()

from sklearn.model_selection import GridSearchCV
'''
# GS for DT
param_grid = {'criterion': ['gini', 'entropy'],
              'splitter': ['best', 'random'],
              'max_depth': [2,3,4,5,6,7,8],
              'min_samples_split': [2,3,4,5,6,7,8],
              'min_samples_leaf': [1,2,3,4,5,6]}
grid_search = GridSearchCV(estimator = DT,
                           param_grid = param_grid,
                           scoring = 'accuracy',
                           cv = 10,
                           n_jobs = -1)
grid_search = grid_search.fit(X_train, y_train)
best_parameters = grid_search.best_params_
print(best_parameters)
DT = DecisionTreeClassifier(**best_parameters)
'''

'''
# GS for RF
param_grid = {'max_depth': [2,3,4,5,6,7,8],
              'min_samples_split': [2,3,4,5,6,7,8],
              'min_samples_leaf': [1,2,3,4,5,6]}
grid_search = GridSearchCV(estimator = RF,
                           param_grid = param_grid,
                           scoring = 'accuracy',
                           cv = 10,
                           n_jobs = -1)
grid_search = grid_search.fit(X_train, y_train)
best_parameters = grid_search.best_params_
print(best_parameters)
RF = DecisionTreeClassifier(**best_parameters)
'''

'''
# Note: here is how you perform the gsCV, SVC
parameters = [{'C': [0.1, 1, 10, 100, 1000], 'kernel': ['linear']},
              {'C': [0.1, 1, 10, 100, 1000], 'kernel': ['rbf'], 'gamma': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]}]
grid_search = GridSearchCV(estimator = svc,
                           param_grid = parameters,
                           scoring = 'accuracy',
                           cv = 10,
                           n_jobs = -1)
grid_search = grid_search.fit(X_train, y_train)
best_parameters = grid_search.best_params_
svc = SVC(**best_parameters)
'''

models = {"knn":knn, "nb":nb, "svc":svc, "LR": lr, "DT": DT, "RF": RF}

for name, model in models.items():
    print("\n"+name+":\n")
    model.fit(X_train,y_train) # loads the data into the model and fit it
    y_pred = model.predict(X_test)
    print("Accuracy score: {}".format(accuracy_score(y_test, y_pred)))
    print("AUC score: {}".format(roc_auc_score(y_test, y_pred, average='macro')))


"""dentymz URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from api import views
from chat import views as chat_view
from app import views as app_views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/auth/login/', views.login),
    path('api/collect-schools/', views.collect_schools),
    path('api/collect-year-groups/', views.collect_year_groups),
    path('api/account/sign-up/', views.sign_up),
    path('api/account/complete-sign-up/', views.complete_sign_up),
    path('api/account/upload-photo/', views.upload_profile_photo),
    path('api/account/change-password/', views.change_password),
    path('api/post/load/', views.load_posts, name='load-posts'),
    path('api/post/text/', views.post_text),
    path('api/post/photo/', views.post_photo),
    path('api/post/load-likes/', views.load_likes),
    path('api/post/load-comments/', views.load_comments),
    path('api/post/like/', views.like_post, name='like-post'),
    path('api/post/comment/', views.comment_on_post),
    path('api/post/delete/', views.delete_post),
    path('api/post/report/', views.report_post),
    path('api/comment/edit/', views.edit_comment),
    path('api/comment/delete/', views.delete_comment),
    path('api/school/follow/', views.follow_school),
    path('api/school/get/', views.get_school),
    path('api/school/collect-students/', views.collect_school_students),
    path('api/school/report-missing/', views.report_missing_school),
    path('api/user/load-data/', views.load_user_data),
    path('api/user/set-status/', views.set_status),
    path('api/user/set-online-status/', views.set_online_status),
    path('api/user/update-fcm-token/', views.update_user_fcm_token),
    path('api/user/update-app-version/', views.update_user_app_version),
    path('api/user/collect-following/', views.collect_following),
    path('chat/', chat_view.index),
    path('chat/room/', chat_view.room, name='room'),
    path('api/chat/load/', views.load_chats),
    path('api/chat/load-messages/', views.load_messages),
    path('api/chat/send-message/', views.send_message),
    path('api/chat/request/', views.chat_request),
    path('api/chat/load-chat-requests/', views.load_chat_requests),
    path('api/chat/load-blocked-users/', views.load_blocked_users),
    path('api/support/report-problem/', views.report_problem),
    path('api/contact/send-feedback/', views.send_feedback),
    path('', app_views.index, name='index'),
    path('login/', app_views.LoginView.as_view(), name="login"),
    path('signup/', app_views.RegisterView.as_view(), name="signup"),
    path('complete-signup/', app_views.CompleteRegistrationView.as_view(), name="complete-signup"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


import socket
import random

# IP = '192.168.1.95'
# PORT = 5005
#
# sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #socket.SOCK_DGRAM means UDP packet
#
# message = "Hello, World"
#
# bytes = random._urandom(1024)
#
# sock.sendto(bytes,('192.168.1.95',5001))

sock= socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

address=('192.168.1.115', 5006)

sock.bind(address)

data,addr=sock.recvfrom(1024)
print(data[4])
print(addr)


import pymongo
from ShoppingWebsiteCrawlwer.settings import MONGODB_SERVER
import datetime

import datetime
import time
date=datetime.date.today()
date_stamp=time.mktime(date.timetuple())-24*60*60
thirty_ago_stamp=date_stamp-30*24*60*60
thirty_ago_date=str(datetime.date.fromtimestamp(thirty_ago_stamp))
client = pymongo.MongoClient(MONGODB_SERVER)

for name in ['jd', 'sn']:
    db = client[name + 'Good']
    recomm_ist=[]
    if db["30_days"]:
        db.drop_collection("30_days")
    for c_name in db.list_collection_names():
        collect = db[c_name]
        items=collect.find()
        for item in items:
            try:
                current_price=float(item["date_price"][str(date)][0])+float(item["date_price"][str(date)][1])
                thirty_ago=float(item["date_price"][thirty_ago_date][0])+float(item["date_price"][str(date)][1])
            except Exception:
                print(item["_id"])
                continue
            if current_price>=thirty_ago:
                continue
            recomm_ist.append({"id":item["_id"],"rank":(thirty_ago-current_price)/thirty_ago,"collect":collect.name})
    recomm_ist.sort(key=lambda x:x["rank"])
    for item in recomm_ist[:50]:
        _id=item["id"]
        collect=db[item["collect"]]
        detail_item=collect.find_one({"_id":_id})
        detail_item["rank"]=item["rank"]
        detail_item.pop("_id")
        recomm_db=db["30_days"]
        recomm_db.insert_one(detail_item)


import numpy as np
import matplotlib.pyplot as plt

def training_testing():
    #
    #  Reading the data from the experiment and FEM
    #

    sim_data = 'interpolated_simulation_data.txt'
    exp_data = 'noise_data.txt'

    exp_stress = np.loadtxt(exp_data,usecols=(0))
    exp_strain = np.loadtxt(exp_data,usecols=(1))
    sim_stress = np.loadtxt(sim_data,usecols=(0))
    sim_strain = np.loadtxt(sim_data,usecols=(1))

    #
    # taking out the equal number of data in experiment and simulation
    #
    exp_stress = exp_stress[:len(sim_stress)]
    exp_strain = exp_strain[:len(sim_strain)]

    exp_stress_strain = np.array([exp_stress,exp_strain]).T

    shuffle_stress_strain = np.array([exp_stress,exp_strain]).T
    #
    #  shuffling the data set
    #
    np.random.shuffle(shuffle_stress_strain)

    #
    #  seperating the data set of training and testing
    #

    training = shuffle_stress_strain[:int(len(sim_stress)*0.6)]
    testing = shuffle_stress_strain[int(len(sim_stress)*0.6):]

    #
    #  Plotting the simulation curve fits with training and testing data
    #

    fig,ax = plt.subplots(2,constrained_layout=True)
    ax[0].plot(sim_strain,sim_stress,color='blue',label='Predicted data')
    ax[0].scatter(training[:,1],training[:,0],color='red',marker='.',label='Training data')
    ax[1].plot(sim_strain,sim_stress,color='blue',label='Predicted data')
    ax[1].scatter(testing[:,1],testing[:,0],color='red',marker='.',label='Testing data')
    ax[0].legend()
    ax[1].legend()
    ax[0].grid()
    ax[1].grid()
    ax[0].set_title('Training ')
    ax[0].set_xlabel('Strain')
    ax[0].set_ylabel('Stress')
    ax[1].set_title('Testing')
    ax[1].set_xlabel('Strain')
    ax[1].set_ylabel('Stress')
    fig.savefig('Training_Testing')
    plt.close()
    return None


def House(l):
  b = len(l)
  if (b == 2):
    return max(l)
  if (b == 1):
    return l[0]
  if (b == 3):
    return max(l[1],l[0]+House(l[2:]))
  return max(l[1],House(l[3:]),l[0]+House(l[2:]))

n = int(input("Enter the number of houses in a line : "))
l = []
for i in range (0,n):
  a = int(input("Enter the value in the house number"+str(i+1)+":"))
  l.append(a)
print ("The maximum stolen value from the Houses is",House(l))


#! /usr/bin/env python
from __future__ import print_function, division
try:
    range = xrange
except NameError:
    pass

# import collections
# import functools
import itertools as it
# import numpy as np # See http://www.numpy.org
# import sympy as sp # See http://sympy.org/en/index.html
# import gmpy2 # See https://code.google.com/p/gmpy/
# import networkx as nx # See http://networkx.github.io/

import os
import sys
# MY MODULES - available at https://github.com/lackofcheese/CodeJamLib/
sys.path.append(os.path.join(
    os.environ['GOOGLE_DRIVE'], 'Coding', 'GCJ', 'CodeJamLib'))
import codejam_io

def toks_line(f_in, fun=int):
    return [fun(k) for k in f_in.readline().split()]

def process_first(f_in):
    num_cases = int(f_in.readline())
    other_data = None
    return num_cases, other_data

def process_case(f_in, f_out, case_no, other_data=None):
    N, R, P, S = toks_line(f_in)
    ans = solve(N, R, P, S)
    if ans is None:
        ans = "IMPOSSIBLE"
    print("Case #{}: {}".format(case_no, ans), file=f_out)

def solve(N, R, P, S):
    lineups = []
    players = "R" * R + "P" * P + "S" * S
    for perm in it.permutations(players):
        lineup = ''.join(perm)
        for i in range(N):
            lineup = round(lineup)
            if lineup is None:
                break
        if lineup is not None:
            lineups.append(''.join(perm))
    if not lineups:
        return None
    lineups.sort()
    return lineups[0]

def round(lineup):
    new_lineup = ""
    for i in range(len(lineup)//2):
        winner = fight(lineup[i*2], lineup[i*2+1])
        if winner is None:
            return None
        new_lineup += winner
    return new_lineup

WINNERS = dict([
        (frozenset(['R', 'P']), 'P'),
        (frozenset(['R', 'S']), 'R'),
        (frozenset(['S', 'P']), 'S')
        ])

def fight(a, b):
    if a == b:
        return None
    else:
        return WINNERS[frozenset([a, b])]

if __name__ == '__main__':
    codejam_io.process_input(process_case, process_first, __file__)


#!/usr/bin/env python
# Luiz Sales
# luiz@lsales.biz

# Habilitar o item  baseado na chave que ele recebe

from zabbix_api import *
import sys

HOST = sys.argv[1]
CHAVE = sys.argv[2]

zapi = ZabbixAPI(server="")
zapi.login("", "" )

host_get = zapi.host.get({ "output": "hostid", "filter": { "name" : HOST } })
for x in host_get:
	hId = x[u'hostid']
	item_get = zapi.item.get({ "output": "extend", "hostids" : hId, "search" : { "key_" : CHAVE } })
	for w in item_get:
		iId = w[u'itemid']
		item_update = zapi.item.update({ "itemid" : iId, "status": 0 })


import file_writer as fw
import flux_simulator as fs
import quantifiers as qs
import os.path
import parameters
import piquant_options as po

RUN_SCRIPT = "run_quantification.sh"

TRANSCRIPT_COUNTS_SCRIPT = "count_transcripts_for_genes.py"
UNIQUE_SEQUENCE_SCRIPT = "calculate_unique_transcript_sequence.py"
ASSEMBLE_DATA_SCRIPT = "assemble_quantification_data.py"
ANALYSE_DATA_SCRIPT = "analyse_quantification_run.py"

RUN_PREQUANTIFICATION_VARIABLE = "RUN_PREQUANTIFICATION"
QUANTIFY_TRANSCRIPTS_VARIABLE = "QUANTIFY_TRANSCRIPTS"
ANALYSE_RESULTS_VARIABLE = "ANALYSE_RESULTS"

TPMS_FILE = "tpms.csv"
TRANSCRIPT_COUNTS_FILE = "transcript_counts.csv"
UNIQUE_SEQUENCE_FILE = "unique_sequence.csv"

def _get_script_path(script_name):
    return os.path.join(
        os.path.abspath(os.path.dirname(__file__)), script_name)

def _get_transcript_counts_file(quantifier_dir):
    return os.path.join(quantifier_dir, TRANSCRIPT_COUNTS_FILE)

def _get_unique_sequence_file(quantifier_dir):
    return os.path.join(quantifier_dir, UNIQUE_SEQUENCE_FILE)

def _add_run_prequantification(
        writer, quant_method, quant_params,
        quantifier_dir, transcript_gtf_file):

    with writer.if_block("-n \"$RUN_PREQUANTIFICATION\""):
        # Perform preparatory tasks required by a particular quantification
        # method prior to calculating abundances; for example, this might
        # include mapping reads to the genome with TopHat
        quant_method.write_preparatory_commands(writer, quant_params)
        with writer.section():
            _add_calculate_transcripts_per_gene(
                writer, quantifier_dir, transcript_gtf_file)
        with writer.section():
            _add_calculate_unique_sequence_length(
                writer, quantifier_dir, transcript_gtf_file)

def _add_quantify_transcripts(writer, quant_method, quant_params, cleanup):
    # Use the specified quantification method to calculate per-transcript TPMs
    with writer.if_block("-n \"$QUANTIFY_TRANSCRIPTS\""):
        with writer.section():
            writer.add_comment(
                "Use {method} to calculate per-transcript TPMs.".format(
                    method=quant_method))
            quant_method.write_quantification_commands(writer, quant_params)

        if cleanup:
            writer.add_comment(
                "Remove files not necessary for analysis of quantification.")
            quant_method.write_post_quantification_cleanup(writer)

def _add_calculate_transcripts_per_gene(
        writer, quantifier_dir, transcript_gtf_file):

    # Calculate the number of transcripts per gene and write to a file
    writer.add_comment("Calculate the number of transcripts per gene.")

    counts_file = _get_transcript_counts_file(quantifier_dir)
    with writer.if_block("! -f " + counts_file):
        writer.add_line("{command} {transcript_gtf} > {counts_file}".format(
            command=_get_script_path(TRANSCRIPT_COUNTS_SCRIPT),
            transcript_gtf=transcript_gtf_file,
            counts_file=counts_file))

def _add_calculate_unique_sequence_length(
        writer, quantifier_dir, transcript_gtf_file):

    # Calculate the length of unique sequence per transcript and write to a
    # file.
    writer.add_comment(
        "Calculate the length of unique sequence per transcript.")

    unique_seq_file = _get_unique_sequence_file(quantifier_dir)
    with writer.if_block("! -f " + unique_seq_file):
        writer.add_line(
            "{command} {transcript_gtf} > {unique_seq_file}".format(
                command=_get_script_path(UNIQUE_SEQUENCE_SCRIPT),
                transcript_gtf=transcript_gtf_file,
                unique_seq_file=unique_seq_file))

def _add_assemble_quantification_data(
        writer, quantifier_dir, fs_pro_file, quant_method):

    # Now assemble data required for analysis of quantification performance
    # into one file
    writer.add_comment(
        "Assemble data required for analysis of quantification performance " +
        "into one file")

    writer.add_line(
        ("{command} --method={method} --out={out_file} {fs_pro_file} " +
         "{counts_file} {unique_seq_file}").format(
            command=_get_script_path(ASSEMBLE_DATA_SCRIPT),
            method=quant_method,
            out_file=TPMS_FILE,
            fs_pro_file=fs_pro_file,
            counts_file=_get_transcript_counts_file(quantifier_dir),
            unique_seq_file=_get_unique_sequence_file(quantifier_dir)))

def _add_analyse_quantification_results(
        writer, run_dir, piquant_options, **params):

    # Finally perform analysis on the calculated TPMs
    writer.add_comment("Perform analysis on calculated TPMs.")

    options_dict = {p.name: p.option_name for
                    p in parameters.get_run_parameters()}

    params_spec = ""
    for param_name, param_val in params.items():
        params_spec += "{name}={val} ".format(
            name=options_dict[param_name],
            val=str(param_val))

    writer.add_line(
        ("{command} --plot-format={format} " +
         "--grouped-threshold={gp_threshold} {params_spec} " +
         "{tpms_file} {output_basename}").format(
            command=_get_script_path(ANALYSE_DATA_SCRIPT),
            format=piquant_options[po.PLOT_FORMAT],
            gp_threshold=piquant_options[po.GROUPED_THRESHOLD],
            params_spec=params_spec,
            tpms_file=TPMS_FILE,
            output_basename=os.path.basename(run_dir)))

def _add_process_command_line_options(writer):
    # Process command line options - these allow us to subsequently re-run just
    # part of the analysis
    writer.add_comment("Process command line options.")

    with writer.section():
        writer.set_variable(RUN_PREQUANTIFICATION_VARIABLE, "")
        writer.set_variable(QUANTIFY_TRANSCRIPTS_VARIABLE, "")
        writer.set_variable(ANALYSE_RESULTS_VARIABLE, "")

    with writer.while_block("getopts \":pqa\" opt"):
        with writer.case_block("$opt"):
            with writer.case_option_block("p"):
                writer.set_variable(RUN_PREQUANTIFICATION_VARIABLE, 1)
            with writer.case_option_block("q"):
                writer.set_variable(QUANTIFY_TRANSCRIPTS_VARIABLE, 1)
            with writer.case_option_block("a"):
                writer.set_variable(ANALYSE_RESULTS_VARIABLE, 1)
            with writer.case_option_block("\?"):
                writer.add_line("echo \"Invalid option: -$OPTARG\" >&2")

def _add_analyse_results(
        writer, reads_dir, run_dir, quantifier_dir, piquant_options,
        quant_method, read_length, read_depth, paired_end, errors, bias):

    fs_pro_file = os.path.join(reads_dir, fs.EXPRESSION_PROFILE_FILE)

    with writer.if_block("-n \"$ANALYSE_RESULTS\""):
        with writer.section():
            _add_assemble_quantification_data(
                writer, quantifier_dir, fs_pro_file, quant_method)
        _add_analyse_quantification_results(
            writer, run_dir, piquant_options,
            quant_method=quant_method,
            read_length=read_length, read_depth=read_depth,
            paired_end=paired_end, errors=errors, bias=bias)

def _get_quant_params(reads_dir, quantifier_dir, transcript_gtf,
                      genome_fasta, paired_end, errors):

    quant_params = {
        qs.TRANSCRIPT_GTF_FILE: transcript_gtf,
        qs.GENOME_FASTA_DIR: genome_fasta,
        qs.QUANTIFIER_DIRECTORY: quantifier_dir,
        qs.FASTQ_READS: errors
    }

    if paired_end:
        quant_params[qs.LEFT_SIMULATED_READS] = \
            os.path.join(reads_dir,
                         fs.get_reads_file(errors, paired_end=fs.LEFT_READS))
        quant_params[qs.RIGHT_SIMULATED_READS] = \
            os.path.join(reads_dir,
                         fs.get_reads_file(errors, paired_end=fs.RIGHT_READS))
    else:
        quant_params[qs.SIMULATED_READS] = \
            os.path.join(reads_dir, fs.get_reads_file(errors))

    return quant_params

def write_run_quantification_script(
        reads_dir, run_dir, piquant_options,
        quant_method=None, read_length=50, read_depth=10,
        paired_end=False, errors=False, bias=False,
        transcript_gtf=None, genome_fasta=None):

    os.mkdir(run_dir)

    with fw.writing_to_file(
            fw.BashScriptWriter, run_dir, RUN_SCRIPT) as writer:
        with writer.section():
            _add_process_command_line_options(writer)

        quantifier_dir = os.path.join(
            piquant_options[po.OUTPUT_DIRECTORY], "quantifier_scratch")

        quant_params = _get_quant_params(
            reads_dir, quantifier_dir, transcript_gtf,
            genome_fasta, paired_end, errors)

        with writer.section():
            _add_run_prequantification(
                writer, quant_method, quant_params,
                quantifier_dir, transcript_gtf)

        with writer.section():
            cleanup = not piquant_options[po.NO_CLEANUP]
            _add_quantify_transcripts(
                writer, quant_method, quant_params, cleanup)

        _add_analyse_results(
            writer, reads_dir, run_dir, quantifier_dir, piquant_options,
            quant_method, read_length, read_depth, paired_end, errors, bias)


# Sergio Alvarez RA115735
import math

# Ajusta o valor em graus, caso seja maior que 360
def adjustValue(deg):
    if (deg >= 360):
        deg -= math.floor(deg/360)*360
    return deg

def calcSin(rad):
    resp = 0
    # 5 = número de termos da serie
    for i in range(5):
        exp = (i*2)+1
        resp += (float((-1)**i * rad**exp/math.factorial(exp)))
    return resp

def calcCos(rad):
    resp = 0
    # 5 = número de termos da serie
    for i in range(5):
        exp = (i*2)
        resp += (float((-1)**i * rad**exp/math.factorial(exp)))
    return resp

def select(rad):
    sin, cos = 0, 0
    # rad <= 45 graus
    if (rad <= math.pi/4):
        sin = calcSin(rad)
        cos = calcCos(rad)
    # rad > 45 graus e rad <= 90 graus
    elif (rad > math.pi/4 and rad <= math.pi/2):
        sin = calcCos(math.pi/2 - rad)
        cos = calcSin(math.pi/2 - rad)
    # rad > 90 graus e rad <= 135 graus
    elif (rad > math.pi/2 and rad <= 3*math.pi/4):
        sin = calcCos(rad - math.pi/2)
        cos = -1 * calcSin(rad - math.pi/2)
    # rad > 135 graus e rad <= 180 graus
    elif (rad > 3*math.pi/4 and rad <= math.pi):
        sin = calcSin(math.pi - rad)
        cos = -1 * calcCos(math.pi - rad)
    # rad > 180 graus and rad <= 225 graus
    elif (rad > math.pi and rad <= 5*math.pi/4):
        sin = -1 * calcSin(rad - math.pi)
        cos = -1 * calcCos(rad - math.pi)
    # rad > 225 graus and rad <= 270 graus
    elif (rad > 5*math.pi/4 and rad <= 3*math.pi/2):
        sin = -1 * calcCos(3*math.pi/2 - rad)
        cos = -1 * calcSin(3*math.pi/2 - rad)
    # rad > 270 graus and rad <= 315 graus
    elif (rad >  3*math.pi/2 and rad <= 7*math.pi/4):
        sin = -1 * calcCos(rad - 3*math.pi/2)
        cos = -1 * calcSin(rad - 3*math.pi/2)
    # rad > 315 graus and rad <= 360 graus
    elif (rad > 7*math.pi/4 and rad <= 2*math.pi):
        sin = -1 * calcSin(2*math.pi - rad)
        cos = -1 * calcCos(2*math.pi - rad)
    return sin, cos

# joga fora os numeros após a 10 casa decimal
def truncar(num):
    numString = str(num)
    numString = numString.split(".")
    numStringAux = numString[1][:10]
    return float(numString[0]+"."+numStringAux)

def main ():
   # deg = input("Insira um valor em graus : ")
    for i in range (0,361,5):
        deg = adjustValue(float(i))
        rad = float(deg) * (math.pi / 180)
        sin, cos = select(rad)
        sin, cos = truncar(sin), truncar(cos)
        sin_2 = math.sin(rad)
        print("Graus", i, " Sen Calculado: ", sin, " Sen Real: ", sin_2)

if __name__ == '__main__':
   main()


from selenium import webdriver
import time
import os

driver = webdriver.Chrome()
file_path = 'file:///'+os.path.abspath("C:/课件/我的课件/测试/selenium2/locateElement/selenium2html/upload.html")
driver.get(file_path)
time.sleep(3)
driver.find_element_by_name("file").send_keys("C:/课件\\课间歌单\\歌单.txt")

time.sleep(3)
driver.quit()


# Sample code for exporting a group's indicator data and burst data from SmartDiagnostics.

from urllib import request
from urllib.request import HTTPError
from datetime import datetime
from pathlib import Path
import json
import time
import os
import argparse

# Initiates a burst data group export.
#  - groupId: The group to include in the export.
#  - startMillis: Indicates the start date, represented as milliseconds from the unix epoch,
#                 within which export data will be included.
#  - endMillis: Indicates the end date, represented as milliseconds from the unix epoch,
#                 within which export data will be included.
#  - apiKey: The api key for the account that contains the data to be exported.
#  - localExportFolderPath: A path on the local file system where exported data will be placed
#                 upon export completion.
def export_group_burst_data(groupId, startMillis, endMillis, apiKey, localExportFolderPath):
    print(f'Exporting burst data for group {groupId} between "{get_filename_friendly_date(startMillis)}" UTC and "{get_filename_friendly_date(endMillis)}" UTC')

    requestData = {
        "StartTime": startMillis,
        "EndTime": endMillis,
        # Number of chronological burst samples, starting from earliest date, to include in the export
        # within the specified time range. If set to 0, all samples in the time range are included.
        "SampleSize": 0,
        # Set to True to include the node id and the sensor type in the first two columns, respectively,
        # of the exported data set files.
        "EmbedMetadata": False
    }

    # Send the request to the web API.
    httpResponse = request.urlopen(request.Request(
        f'https://sd.kcftech.com/public/exports/group/{groupId}/burstData?apiKey={apiKey}',
        # Convert the requestData dictionary to a JSON string that is represented as an array of bytes
        # that is suitable for sending in this http request.
        data = json.dumps(requestData).encode('utf-8'),
        headers = { 'content-type': 'application/json', 'Accept': 'application/json' }
    ))
    # Read the export id from the http response.
    exportId = httpResponse.read().decode('UTF-8').strip('"')
    exportStatusResult = wait_for_export_completion(apiKey, exportId)
    # Build the full path to where the file should be output locally. The filename will include
    # the time range of the export request.
    exportFilePath = os.path.join(localExportFolderPath, f'{get_filename_friendly_date(startMillis)}--{get_filename_friendly_date(endMillis)}_burst.zip')
    handle_export_result(exportStatusResult, exportFilePath)

# Initiates an indicator group data export.
#  - groupId: The group to include in the export.
#  - startMillis: Indicates the start date, represented as milliseconds from the unix epoch,
#                 within which export data will be included.
#  - endMillis: Indicates the end date, represented as milliseconds from the unix epoch,
#                 within which export data will be included.
#  - apiKey: The api key for the account that contains the data to be exported.
#  - localExportFolderPath: A path on the local file system where exported data will be placed
#           upon export completion.
def export_group_indicator_data(groupId, startMillis, endMillis, apiKey, localExportFolderPath):
    print(f'Exporting indicator data for group {groupId} between "{get_filename_friendly_date(startMillis)}" UTC and "{get_filename_friendly_date(endMillis)}" UTC')

    requestData = {
        "StartTime": startMillis,
        "EndTime": endMillis,
        # Set to True to include the parent group hierarchy ids and names in the first two columns
        # of the exported data set files. Default value is False.
        "EmbedMetadata": False,
        # Specific indicator types that should be either included or excluded from the exported data
        # set files. If not specified, all indicator types are included. Valid types are:
        #
        #   "Voltage","DamageAccumulationAccel","Temperature","VibrationOverallCrestFactorAccel",
        #   "VibrationOverallPeakAccel","VibrationOverallRmsAccel","VibrationBandMax","VibrationOverall",
        #   "VibrationOverallPeak","VibrationOverallRms","RunningSpeed","SignalStrength","Pressure",
        #   "Flow","Humidity","Power","DifferentialPressure","GeneralizedAtoD","VibrationBandRms",
        #   "VibrationOverallSkewness","VibrationOverallKurtosis","VibrationOverallCrestFactor",
        #   "DamageAccumulation","VibrationBandRmsAccel","VibrationBandMaxAccel",
        #   "VibrationOverallSkewnessAccel", "VibrationOverallKurtosisAccel","VibrationOverallAccel",
        #   "GeneralTimeSeries","Math","OnStatistics", "OffStatistics","AlarmStatistics",
        #   "WarningStatistics","PositivePeakPressure","NegativePeakPressure", "RmsPressure",
        #   "BandPressure","OnPercentStatistics","OffPercentStatistics","AlarmPercentStatistics",
        #   "WarningPercentStatistics","DamageAccumulationPressure","OilHumidity","OilTemperature",
        #   "DamageAccumulationAccelRaw","MultiSensorDifferentialPressure","Group"
        "FilteredIndicatorTypes": [],
        # If True, the indicator types specified in the FilteredIndicatorTypes property will be excluded
        # from the exported data. If False (default), the types will be included in the exported data.
        "FilterExclude": False
    }

    # Send the request to the web API.
    httpResponse = request.urlopen(request.Request(
        f'https://sd.kcftech.com/public/exports/group/{groupId}/indicatorData?apiKey={apiKey}',
        # Convert the requestData dictionary to a JSON string that is represented as an array of bytes
        # that is suitable for sending in this http request.
        data = json.dumps(requestData).encode('utf-8'),
        headers = { 'content-type': 'application/json', 'Accept': 'application/json' }
    ))
    # Read the export id from the http response.
    exportId = httpResponse.read().decode('UTF-8').strip('"')
    exportStatusResult = wait_for_export_completion(apiKey, exportId)
    # Build the full path to where the file should be output locally. The filename will include
    # the time range of the export request.
    exportFilePath = os.path.join(localExportFolderPath, f'{get_filename_friendly_date(startMillis)}--{get_filename_friendly_date(endMillis)}_indicator.zip')
    handle_export_result(exportStatusResult, exportFilePath)

# Polls the export status API until the export has completed. Once the export has completed,
# this method returns an object describing whether the final state of the export process. Refer
# to get_export_status_result for the definition of this object.
def wait_for_export_completion(apiKey, exportId):
    exportCompleted = False
    while not exportCompleted:
        exportStatusResult = get_export_status_result(exportId, apiKey)
        exportCompleted = exportStatusResult['exportCompleted']
        if not exportCompleted:
            print('Export still in progress...')
            # Sleep for a few seconds before trying to check the status from the server again.
            time.sleep(4)
        else:
            return exportStatusResult

# Returns an object that describes the current status of a given export.
# This object structure is:
#
#  {
#    "downloadUrl": "String",
#    "exportCompleted": Boolean,
#    "reportsProgress": Boolean,
#    "progress": Integer,
#    "error": "String"
#  }
def get_export_status_result(exportId, apiKey):
    # Send the status request to the web API.
    httpResponse = request.urlopen(request.Request(
        f'https://sd.kcftech.com/public/exports/{exportId}/status?apiKey={apiKey}',
        headers = { 'content-type': 'application/json', 'Accept': 'application/json' }
    ))
    # Read the export status JSON string from the http response.
    stringResult = httpResponse.read().decode('UTF-8')
    # Convert and return the string result as an object.
    return json.loads(stringResult)

# Downloads the exported data to the specified localExportFilePath (if the export was successful).
def handle_export_result(exportStatusResult, localExportFilePath):
    if 'downloadUrl' in exportStatusResult:
        # Download the export file to the local exportFilePath.
        request.urlretrieve(exportStatusResult['downloadUrl'], localExportFilePath)
        print(f'** Export file downloaded to {localExportFilePath} **')
    else:
        exportErrorText = exportStatusResult["error"]
        if exportErrorText == 'No data to export.':
            print(f'** The export completed with an empty data set. No file was downloaded. **')
        else:
            print(f'** Something went wrong with the export: "{exportStatusResult["error"]}" **')

def winapi_path(dos_path, encoding = None):
    path = os.path.abspath(dos_path)
    if path.startswith("\\\\"):
        path = "\\\\?\\UNC\\" + path[2:]
    else:
        path = "\\\\?\\" + path
    return path

def datetime_to_millis(dateTime):
    return dateTime.timestamp() * 1000

def datetime_string_to_millis(dateTimeString):
    dateTime = datetime.strptime(dateTimeString, '%Y-%m-%d %H:%M:%S')
    return datetime_to_millis(dateTime)

def millis_to_datetime(millis):
    return datetime.fromtimestamp(millis / 1000)

# Convert the input millis to a format that is allowed in a filename (something like '2019-07-20_06-12-42')
def get_filename_friendly_date(millis):
    dt = millis_to_datetime(millis)
    return dt.strftime('%Y-%m-%d %H-%M-%S')

ap = argparse.ArgumentParser()
ap.add_argument('-a', '--apikey', required = True,
   help = 'Your account API Key.')
ap.add_argument('-s', '--start', required = True,
   help = 'The start date/time of the export range in the format "YYYY-MM-DD HH:mm:ss" (e.g. "2018-09-25 00:00:00").')
ap.add_argument('-e', '--end', required = False,
   help = 'The end date/time of the export range in the format "YYYY-MM-DD HH:mm:ss" (e.g. "2018-09-25 00:00:00"). Defaults to the current date/time if omitted.')
ap.add_argument('-i', '--indicator', required = False, action='store_true',
   help = 'Specify this flag to include indicator data in the export.')
ap.add_argument('-b', '--burst', required = False, action='store_true',
   help = 'Specify this flag to include burst data in the export.')
ap.add_argument('-g', '--groupId', required = True,
   help = 'The Group ID containing the data to be exported.')
args = vars(ap.parse_args())
if not args['burst'] and not args['indicator']:
    ap.error('You must specify at least one type of export (i.e. burst or indicator).')

apiKey = args['apikey']
startMillis = datetime_string_to_millis(args['start'])
endMillis = datetime_to_millis(datetime.utcnow())
includeIndicator = args['indicator']
includeBurst = args['burst']
groupId = args['groupId']

if args['end'] is not None:
    endMillis = datetime_string_to_millis(args['end'])

# Defaults to outputting the exported files to the current path where this script is run.
localDataExportFolderPath = '.'

if includeIndicator:
    try:
       export_group_indicator_data(groupId, startMillis, endMillis, apiKey, localDataExportFolderPath)
    except HTTPError as err:
       if err.code == 401:
           print(f'** The specified API key is invalid. **')
       elif err.code == 404:
           print(f'** No group burst data exists for the specified time frame. **')
       else:
           raise
if includeBurst:
    try:
       export_group_burst_data(groupId, startMillis, endMillis, apiKey, localDataExportFolderPath)
    except HTTPError as err:
       if err.code == 401:
           print(f'** The specified API key is invalid. **')
       elif err.code == 404:
           print(f'** No group burst data exists for the specified time frame. **')
       else:
           raise


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

from django.db import models, migrations
import datetime

class Migration(migrations.Migration):

    dependencies = [
        ('app', '0004_auto_20160208_1901'),
    ]

    operations = [
        migrations.AddField(
            model_name='device',
            name='password',
            field=models.CharField(default=1, max_length=128),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='device',
            name='last_queried',
            field=models.DateTimeField(default=datetime.datetime(2016, 2, 8, 19, 8, 45, 500399)),
            preserve_default=True,
        ),
    ]


import pandas as pd
from scipy.interpolate import interp1d, interp2d
from scipy import stats
import numpy as np
from astropy.io import fits
from sys import exit

import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,
                               AutoMinorLocator)
import matplotlib.gridspec as gridspec

from glob import glob
import os

from astropy.visualization import (PercentileInterval, LinearStretch,
                                    ImageNormalize, ZScaleInterval)
from astropy import wcs
from astropy import units as u
from astropy.modeling.models import Ellipse2D
from astropy.coordinates import Angle

import WIFISSpectrum as WS

bluelow =  [4827.875, 4946.500, 5142.625]
bluehigh = [4847.875, 4977.750, 5161.375]
linelow =  [4847.875, 4977.750, 5160.125]
linehigh = [4876.625, 5054.000, 5192.625]
redlow =   [4876.625, 5054.000, 5191.375]
redhigh =  [4891.625, 5065.250, 5206.375]

c = 299792.458

def ellipse_region_sauron(cube, center, a, ecc, theta, annulus = False):
    ''' Function that returns a mask that defines the elements that lie
        within an ellipsoidal region. The ellipse can also be annular.

        Inputs:
            Cube:    The data array
            center:  Tuple of the central coordinates (x_0, y_0) (spaxels)
            a:       The semi-major axis length (spaxels).
            ecc:     The eccentricity of the ellipse (0<ecc<1)
            theta:   The rotation angle of the ellipse from vertical (degrees)
                     rotating clockwise.

        Optional Inputs:
            annulus: False if just simple ellipse, otherwise is the INNER
                     annular radius (spaxels)

    '''

    # Define angle, eccentricity term, and semi-minor axis
    an = Angle(theta, 'deg')
    e = np.sqrt(1 - (ecc**2))
    b = a * e

    # Create outer ellipse
    ell_region = Ellipse2D(amplitude=10, x_0 = center[0], y_0 = center[1],\
                a=a, b=b, theta=an.radian)
    x,y = np.mgrid[0:cube.shape[0], 0:cube.shape[1]]
    ell_good = ell_region(x,y)

    if annulus:
        # Define inner ellipse parameters and then create inner mask
        a2 = annulus
        b2 = a2 * e
        print(a2, b2)
        ell_region_inner = Ellipse2D(amplitude=10, x_0 = center[0],\
                y_0 = center[1], a=a2, b=b2, theta=an.radian)

        # Set region of outer mask and within inner ellipse to zero, leaving
        # an annular elliptical region
        ell_inner_good = ell_region_inner(x,y)
        ell_good[ell_inner_good > 0] = 0

    #fig, ax = mpl.subplots(figsize = (12,7))
    #mpl.imshow(np.nansum(cube, axis = 0), origin = 'lower', interpolation = None)
    #mpl.imshow(ell_good, alpha=0.2, origin = 'lower', interpolation = None)
    #mpl.show()

    # Return the mask
    return np.array(ell_good, dtype = bool)

def prepare_sauron(fl, ellipse, write=False, plot=False):

    ff = fits.open(fl)
    data = ff[0].data
    head = ff[0].header
    image = ff[1].data
    table = ff[2].data
    wl = np.arange(data.shape[1])*head['CDELT1'] + head['CRVAL1']

    x = table['A']
    y = table['D']
    SNflat = table['SN']

    xx, yy = np.meshgrid(np.sort(list(set(x))), np.sort(list(set(y))))
    zz = np.zeros(xx.shape)
    ss = np.zeros((xx.shape[0],xx.shape[1],data.shape[1]))
    SN = np.zeros(xx.shape) + 0.0001
    im = np.mean(data, axis = 1)
    #print(xx[0,:].shape, yy[:,0].shape,zz.shape,xx.shape, yy.shape,\
        #len(np.sort(list(set(x)))), len(np.sort(list(set(y)))))
    for i, val in enumerate(zip(x,y)):
        xi = np.where(xx[0,:] == val[0])
        yi = np.where(yy[:,0] == val[1])
        try:
            zz[yi,xi] = im[i]
            SN[yi,xi] = SNflat[i]
            ss[yi,xi,:] = data[i,:]
        except:
            print(xi,yi,i)

    SNspec = np.zeros(ss.shape)
    for i in range(ss.shape[-1]):
        SNspec[:,:,i] = ss[:,:,i] / SN * np.sqrt(ss.shape[-1])

    #ellipse = (cx[0][0],cx[1][0],-28.,5,0.759,False)
    cx = np.where(np.logical_and(xx == 0, yy == 0))
    whgood = ellipse_region_sauron(zz, (cx[0][0], cx[1][0]),\
                    ellipse[3], ellipse[4], ellipse[2], \
                    annulus = ellipse[5])
    whgoodflat = whgood.flatten()

    ssnew = np.moveaxis(ss, 2, 0)
    ssflat = ssnew.reshape(ssnew.shape[0], -1)
    specslice = ssflat[:,whgoodflat]

    specmeanorig = np.nanmean(specslice, axis = 1)
    goodi = np.ones(specslice.shape[1], dtype=bool)
    for i in range(specslice.shape[1]):
        if np.median(specslice[:,i]) < 0.05:
            goodi[i] = False
    specslice = specslice[:,goodi]

    specmean = np.nanmean(specslice, axis = 1)
    specsum = np.nansum(specslice, axis = 1)

    SNnew = np.moveaxis(SNspec, 2, 0)
    SNflat = SNnew.reshape(SNnew.shape[0], -1)
    noiseslice = SNflat[:,whgoodflat]
    noiseslice = noiseslice[:,goodi]
    noisemean = np.sqrt(np.sum(noiseslice**2.0, axis = 1)) / specslice.shape[1]
    noisesum = np.sqrt(np.sum(noiseslice**2.0, axis = 1))

    if plot:
        gg = np.zeros(zz.shape)
        gg[whgood] = 1.0
        fig, ax = plt.subplots(figsize=(8,8))
        #ax.scatter(x,y,c = np.mean(data, axis = 1), s=50)
        ax.imshow(zz, origin='lower', extent = (np.min(x)-0.4,np.max(x)+0.4,\
            np.min(y)-0.4, np.max(y)+0.4), interpolation=None)

        if ellipse[5]:
            e= np.sqrt(1 - (ellipse[4]**2.0))
            an = Angle(ellipse[2],'deg')
            b = ellipse[3]*e
            el = patches.Ellipse((0,0), 2*ellipse[3],2*b, angle=-an.degree+90,
                        linewidth=2, facecolor='none', edgecolor='m')
            ax.add_patch(el)

            b = ellipse[5]*e
            el2 = patches.Ellipse((0,0), 2*ellipse[5],2*b, angle=-an.degree+90,
                        linewidth=2, facecolor='none', edgecolor='r')
            ax.add_patch(el2)

        else:
            e= np.sqrt(1 - (ellipse[4]**2.0))
            an = Angle(ellipse[2],'deg')
            b = ellipse[3]*e
            el = patches.Ellipse((0,0), 2*ellipse[3],2*b, angle=-an.degree+90,
                        linewidth=2, facecolor='none', edgecolor='r')
            ax.add_patch(el)

        plt.show()

        fig, ax = plt.subplots(figsize=(10,6))

        ax.plot(wl, specsum, label='CorrMean')
        #ax.plot(wl, specmeanorig, label='Mean')
        ax.fill_between(wl, specsum - noisesum, y2 = specsum + noisesum,\
                 alpha = 0.3, color='#333333')
        ax.legend()
        plt.show()

        fig, ax = plt.subplots(figsize=(14,10))

        for i in range(specslice.shape[1]):
            ax.plot(wl, specslice[:,i]/specslice[20,i])
        plt.show()

    if write:
        print("Writing extracted spectrum....")
        hdu = fits.PrimaryHDU(specsum)
        hdu2 = fits.ImageHDU(wl, name = 'WL')
        hdu3 = fits.ImageHDU(noisesum, name = 'ERR')
        hdul = fits.HDUList([hdu,hdu2,hdu3])

        hdul.writeto(write, overwrite=True)
        print("Wrote to: ", write)

    return wl, specsum, noisesum


def quicksort(lst, lst2):
    acc = 0
    while True:
        lst += lst2
        lst2 = []
        for x in range(len(lst) - 2, -1, -1):
            pivot = lst[-1]
            acc += 1
            if acc >= len(lst):
                return lst
            if lst[x] >= pivot:
                temp = lst[x]
                lst.remove(lst[x])
                lst2.append(temp)
                acc = 0

if __name__ == '__main__':
    lst2 = []
    lst= [4, 7, 2, 9, 1]
    ans = quicksort(lst, lst2)
    print(ans)