from sklearn.multiclass import OneVsRestClassifier
from sklearn import linear_model
import numpy as np
from sklearn import metrics
import random
from sklearn import datasets

def get_groups(args):
    """
    获得处理好的顶点类别分组
    :param groups_path:
    :return:
    """
    groups = {}
    labels = set()
    with open(args.groups) as f:
        lines = f.readlines()
        for line in lines:
            words = line.split(",")
            if words[0] in groups:
                t = groups[words[0]]
                del groups[words[0]]
                t.append(words[1][:-1])
                groups[words[0]] = t
            else:
                groups[words[0]] = [words[1][:-1]]
            labels.add(words[1][:-1])

    return groups, labels

def get_data(num, embeddings, groups, class_num):
    """
    为了保证均匀性，对于每个1/10小组中的数据，我们选择
    :param label: 当前使用的类别label
    :param num: 分成num份
    :param model: word2vec获得的所有顶点的k维特征表示
    :return: 按照类别label划分成num份的list
    """
    X = []
    Y = []

    for key in embeddings:
        # print(key)
        y = [0] * class_num
        for label in groups[key]:    # 各个类别
            y[int(label)-1]=1
        Y.append(y)
        X.append(embeddings[key])

    return np.array(X),np.array(Y)

def get_split(k, num, X, Y):
    """

    :param i: 正反类别各取(i+1)/10
    :param num: 一共分成num份
    :param X: 全部集合X
    :param Y: 全部集合Y
    :return:
    """

    batch = int(1.0 * len(X) / num)
    train = random.sample(range(0, len(X)), k*batch)
    train_X = X[train]
    train_Y = Y[train]

    tot = [i for i in range(len(X))]
    test = list(set(tot) - set(train))
    test_X = X[test]
    test_Y = Y[test]

    return train_X,train_Y,test_X,test_Y

def read_embedding(args):
    """
    从node2vec中生成的文件中读取embedding
    :param args:
    :return:
    """
    with open(args.output) as f :
        lines = f.readlines()
        num = int(lines[0].split(" ")[0])
        dimension = int(lines[0].split(" ")[1])
        lines = lines[1:]
        embeddings = {}
        for line in lines:
            words = line.split(" ")
            embedding = []
            for j in range(1, len(words)):
                embedding.append(float(words[j]))
            embeddings[words[0]] = embedding
    return embeddings

def classification(args):
    """
    利用输入参数中顶点参数：
    :param groups_path: groups_path 顶点类别文件, model, 生成的每个单词的词向量
    :param model: 每个顶点的维度向量，由word2vec得来
    :return: None
    """

    embeddings = read_embedding(args)
    groups,groups_labels = get_groups(args)
    # print(groups_labels)

    num = 10
    ans = []
    X, Y = get_data(10, embeddings, groups, len(groups_labels))
    for i in range(1, num):
        # 不同比例的训练数据进行测试
        cnt = 0.0
        tot = 0.0
        #for label in groups_labels:
        # 把样本按照类别label分成10分

        train_X,train_Y,test_X,test_Y = get_split(i,10,X,Y)
        print(train_X.shape, train_Y.shape, test_X.shape, test_Y.shape)
        # print(X[0:1])
        # print(Y[0:1])
        """
        if(len(set(train_Y))<=1):
            print("train set length <= 1.")
            continue
        if(len(set(test_Y))<=1):
            print("test set length <= 1.")
            continue
        """

        clf = OneVsRestClassifier(linear_model.LogisticRegression(penalty='l2').fit(train_X, train_Y))
        # Z = clf.predict(np.c_[test_X.ravel(), test_Y.ravel()])
        y_pred = clf.predict(test_X)

        # print("  pre=" + str(np.mean(y_pred == test_Y)) + "  f1-score" + metrics.classification_report(test_Y, y_pred)[145:149])
        print(metrics.classification_report(test_Y, y_pred))
        # cnt += float(metrics.classification_report(test_Y, y_pred)[145:149])
        #tot += 1.0
        #print("i:" + str(i) + "  avg f1-score: " + str(1.0*cnt/tot) + "\n")
        #ans.append(1.0*cnt/tot)
    #print(ans)

import numpy
from sklearn.multiclass import OneVsRestClassifier
from sklearn.linear_model import LogisticRegression
# from itertools import izip
from sklearn.metrics import f1_score
from scipy.io import loadmat
from sklearn.utils import shuffle as skshuffle
import gensim
from collections import defaultdict
from scipy.sparse import lil_matrix
from gensim.models import Word2Vec

class TopKRanker(OneVsRestClassifier):
    def predict(self, X, top_k_list):
        assert X.shape[0] == len(top_k_list)
        probs = numpy.asarray(super(TopKRanker, self).predict_proba(X))
        all_labels = []
        for i, k in enumerate(top_k_list):
            probs_ = probs[i, :]
            labels = self.classes_[probs_.argsort()[-k:]].tolist()
            all_labels.append(labels)
        return all_labels

def sparse2graph(x):
    G = defaultdict(lambda: set())
    cx = x.tocoo()
    for i,j,v in zip(cx.row, cx.col, cx.data):
        G[i].add(j)
    return {str(k): [str(x) for x in v] for k,v in G.items()}

def scoring(args):

    # 0. Files
    embeddings_file = '../../data/BlogCatalog-dataset/data/blog_'+str(0.25)+'_'+str(0.25)+'.emb'
    matfile = "blogcatalog.mat"

    # 1. Load Embeddings
    #model = Word2Vec.load_word2vec_format(embeddings_file)
    model = gensim.models.KeyedVectors.load_word2vec_format(embeddings_file)

    # 2. Load labels
    mat = loadmat(matfile)
    A = mat['network']
    graph = sparse2graph(A)
    labels_matrix = mat['group']
    print(labels_matrix)
    print("type(labels):", type(labels_matrix))             #type(labels): <class 'scipy.sparse.csc.csc_matrix'>
    print(labels_matrix.shape)
    # Map nodes to their features (note:  assumes nodes are labeled as integers 1:N)
    features_matrix = numpy.asarray([model[str(node)] for node in range(1,len(graph)+1)])

    # 2. Shuffle, to create train/test groups
    shuffles = []
    number_shuffles = 3
    for x in range(number_shuffles):
        shuffles.append(skshuffle(features_matrix, labels_matrix))

    # 3. to score each train/test group
    all_results = defaultdict(list)

    # training_percents = [0.1, 0.5, 0.9]
    # uncomment for all training percents
    training_percents = numpy.asarray(range(1,10))*.1
    for train_percent in training_percents:
        for shuf in shuffles:

            X, y = shuf

            training_size = int(train_percent * X.shape[0])

            X_train = X[:training_size, :]
            y_train_ = y[:training_size]

            y_train = [[] for x in range(y_train_.shape[0])]

            cy = y_train_.tocoo()
            for i, j in zip(cy.row, cy.col):
                y_train[i].append(j)

            assert sum(len(l) for l in y_train) == y_train_.nnz

            X_test = X[training_size:, :]
            y_test_ = y[training_size:]

            y_test = [[] for x in range(y_test_.shape[0])]

            cy = y_test_.tocoo()
            for i, j in zip(cy.row, cy.col):
                y_test[i].append(j)

            # clf = TopKRanker(LogisticRegression())
            clf = TopKRanker(LogisticRegression(penalty='l2'))
            clf.fit(X_train, y_train)

            # find out how many labels should be predicted
            top_k_list = [len(l) for l in y_test]
            preds = clf.predict(X_test, top_k_list)

            results = {}
            #averages = ["samples", "micro", "macro", "weighted"]
            averages = ["micro", "macro"]
            for average in averages:
                results[average] = f1_score(y_test, preds, average=average)
                print(results[average])
            all_results[train_percent].append(results)

    print('Results, using embeddings of dimensionality', X.shape[1])
    print('-------------------')
    for train_percent in sorted(all_results.keys()):
        print('Train percent:', train_percent)
        for x in all_results[train_percent]:
            print(x)
        print('-------------------')

if __name__ == "__main__":
    print("hello, there is classify.")


import argparse
import os
import sys
import csv
import json
import datetime
import dateutil.parser as date_parser
import codecs

# Parse Arguements
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file', help='CSV input file', required=True)
args = parser.parse_args()
file=args.file

# Preflight Checks
if not os.path.exists(file):
    sys.exit("Cannot find "+file)

# Helper functions
def check_in_list(item, list):
    if item in list:
        list.remove(item)
        return True
    else:
        print("Error, not in list")
        return False

def choose_csv_column(hacker_tracker_field, csv_columns_left):
    output = ""
    while output == "":
        print("\nColumns left: "+str(csv_columns_left))
        user_input = str(input("Which column is "+hacker_tracker_field+"? ") or "")
        if check_in_list(user_input, csv_columns_left):
            output = user_input
            return (output, csv_columns_left)

def convert_to_isotime(text):
    date = date_parser.parse(text)
    return date.isoformat()

def set_endtime(iso_start_time, duration):
    print(duration)
    date_time_start = datetime.datetime.strptime(iso_start_time, '%Y-%m-%dT%H:%M:%S')
    end_time = date_time_start + datetime.timedelta(minutes=int(duration))
    return end_time.isoformat()

def get_speaker_id(speakers_list, speaker_handle, speaker_id, conference_code):
    # return speaker id if we already have it
    for speaker in speakers_list:
        if speaker['name'] == speaker_handle:
            return speakers_list, speaker['id']

    # Else just make a new one
    # Most of this is blank. Because Skytalks
    speaker_id += 1
    new_speaker_json = {}
    new_speaker_json.update({"name": speaker_handle})
    new_speaker_json.update({ "updated_at": datetime.datetime.now().isoformat()})
    new_speaker_json.update({"description": ""})
    new_speaker_json.update({"title": ""})
    new_speaker_json.update({"id": speaker_id})
    new_speaker_json.update({"twitter": ""})
    new_speaker_json.update({"conference": conference_code})
    new_speaker_json.update({"link": ""})
    speakers_list.append(new_speaker_json)
    return speakers_list, speaker_id

# Lets chew in the CSV
csv_columns=[]
list_of_events=[]
with open(file) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            for item in row:
                csv_columns.append(item)
            print(f'Column names are {", ".join(csv_columns)}')
            line_count += 1
        else:
            event_json={}
            for i in range(0,len(csv_columns)):
                event_json.update({csv_columns[i]: row[i]})
            list_of_events.append(event_json)
            line_count += 1

# Alrighty then. Now to line up with the inputs hacker tracker needs
csv_columns_left = csv_columns
print("Set global values")
conference_code = str(input("Conference Code [DC27]: ") or "DC27")
event_type = int(input("Event Type [9]: ") or "9")
#Uncomment if it's just one location
#location = str(input("Location [Bally’s Las Vegas - Jubilee Tower 2nd floor]: ") or "Bally’s Las Vegas - Jubilee Tower 2nd floor")

print('\n\n')
print("Choose which CSV column headers to set to which Hacker Tracker Option")
start_date, csv_columns_left = choose_csv_column("start_date", csv_columns_left)
duration, csv_columns_left = choose_csv_column("duration (in minutes)", csv_columns_left)
title, csv_columns_left = choose_csv_column("title", csv_columns_left)
speakers, csv_columns_left = choose_csv_column("speakers", csv_columns_left)
description, csv_columns_left = choose_csv_column("description", csv_columns_left)
location, csv_columns_left = choose_csv_column("location", csv_columns_left)
'''
# Hardcoded Skytalks Values
conference_code = "DC27"
event_type = "9"
location = "Bally’s Las Vegas - Jubilee Tower 2nd floor"
start_date = "Day-Time"
duration = "Duration"
title = "Talk Title"
speakers = "Speaker(s)"
description = "Talk Description"
'''
# And finally, let's make some JSON
schedule = []
speakers_list = []
id = 1337
current_speaker_id = 7331
for event in list_of_events:
    # Check for empty keys caused by newlines
    print(event)
    if event[title] == "":
        pass
    else:
        hacker_tracker_event_json = {}
        hacker_tracker_event_json.update({"start_date": str(convert_to_isotime(event[start_date]))})
        hacker_tracker_event_json.update({"id": id})
        hacker_tracker_event_json.update({"description": event[description]})
        hacker_tracker_event_json.update({"location": event[location]})
        # Uncomment if just one location
        #hacker_tracker_event_json.update({"location": location})
        hacker_tracker_event_json.update({"link": ""})
        # hacker_tracker_event_json.update({"speakers": event[speakers].splitlines()})
        this_event_speakers=[]
        for speaker in event[speakers].split(", "):
            speakers_list, speaker_to_append = get_speaker_id(speakers_list, speaker, current_speaker_id, conference_code)
            this_event_speakers.append(speaker_to_append)
            if current_speaker_id < speaker_to_append:
                current_speaker_id = speaker_to_append
        hacker_tracker_event_json.update({"speakers": this_event_speakers})
        hacker_tracker_event_json.update({"end_date": str(set_endtime(convert_to_isotime(event[start_date]), event[duration]))})
        hacker_tracker_event_json.update({"conference": conference_code})
        hacker_tracker_event_json.update({"event_type": int(event_type)})
        hacker_tracker_event_json.update({"includes": ""})
        hacker_tracker_event_json.update({"title": event[title]})
        hacker_tracker_event_json.update({"updated_at": datetime.datetime.now().isoformat()})

        schedule.append(hacker_tracker_event_json)
        id += 1

hacker_tracker_schedule_json = {}
hacker_tracker_schedule_json.update({"Schedule": schedule})
print(json.dumps(hacker_tracker_schedule_json, indent=2, ensure_ascii=False))

hacker_tracker_speakers_json = {}
hacker_tracker_speakers_json.update({"speakers": speakers_list})
print(json.dumps(hacker_tracker_speakers_json, indent=2, ensure_ascii=False))

# Let's write this sucker to a file
with codecs.open('events.json', 'w', 'utf-8') as events_file:
    events_file.write(json.dumps(hacker_tracker_schedule_json, indent=2, ensure_ascii=False))
with codecs.open('speakers.json', 'w', 'utf-8') as speakers_file:
    speakers_file.write(json.dumps(hacker_tracker_speakers_json, indent=2, ensure_ascii=False))


import os
import cv2
import numpy as np
import requests
import pyfakewebcam
from signal import signal, SIGINT
from sys import exit

# setup access to the *real* webcam
cap = cv2.VideoCapture('/dev/video0')
height, width = 720, 1280
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FPS, 30)

# In case the real webcam does not support the requested mode.
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))

# The scale factor for image sent to bodypix
sf = 0.5

# setup the fake camera
fake = pyfakewebcam.FakeWebcam('/dev/video2', width, height)

# declare global variables
background = None
foreground = None
f_mask = None
inv_f_mask = None

def load_images():
    global background
    global foreground
    global f_mask
    global inv_f_mask

    # load the virtual background
    background = cv2.imread("background.jpg")
    background = cv2.resize(background, (width, height))

    foreground = cv2.imread("foreground.jpg")
    foreground = cv2.resize(foreground, (width, height))

    f_mask = cv2.imread("foreground-mask.png")
    f_mask = cv2.normalize(f_mask, None, alpha=0, beta=1,
                        norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
    f_mask = cv2.resize(f_mask, (width, height))
    f_mask = cv2.cvtColor(f_mask, cv2.COLOR_BGR2GRAY)
    inv_f_mask = 1 - f_mask

def handler(signal_received, frame):
    load_images()
    print('Reloaded the background and foreground images')

def get_mask(frame, bodypix_url='http://127.0.0.1:9000'):
    frame = cv2.resize(frame, (0, 0), fx=sf, fy=sf)
    _, data = cv2.imencode(".png", frame)
    r = requests.post(
        url=bodypix_url,
        data=data.tobytes(),
        headers={'Content-Type': 'application/octet-stream'})
    mask = np.frombuffer(r.content, dtype=np.uint8)
    mask = mask.reshape((frame.shape[0], frame.shape[1]))
    mask = cv2.resize(mask, (0, 0), fx=1/sf, fy=1/sf,
                      interpolation=cv2.INTER_NEAREST)
    mask = cv2.dilate(mask, np.ones((20,20), np.uint8) , iterations=1)
    mask = cv2.blur(mask.astype(float), (30,30))
    return mask

def shift_image(img, dx, dy):
    img = np.roll(img, dy, axis=0)
    img = np.roll(img, dx, axis=1)
    if dy>0:
        img[:dy, :] = 0
    elif dy<0:
        img[dy:, :] = 0
    if dx>0:
        img[:, :dx] = 0
    elif dx<0:
        img[:, dx:] = 0
    return img

def get_frame(cap, background):
    _, frame = cap.read()
    # fetch the mask with retries (the app needs to warmup and we're lazy)
    # e v e n t u a l l y c o n s i s t e n t
    mask = None
    while mask is None:
        try:
            mask = get_mask(frame)
        except:
            print("mask request failed, retrying")

    # composite the foreground and background
    for c in range(frame.shape[2]):
        frame[:,:,c] = frame[:,:,c] * mask + background[:,:,c] * (1 - mask)

    for c in range(frame.shape[2]):
        frame[:,:,c] = frame[:,:,c] * inv_f_mask + foreground[:,:,c] * f_mask

    return frame

if __name__ == '__main__':
    load_images()
    signal(SIGINT, handler)
    print('Running...')
    print('Please press CTRL-\ to exit.')
    print('Please CTRL-C to reload the background and foreground images')
    # frames forever
    while True:
        frame = get_frame(cap, background)
        # fake webcam expects RGB
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        fake.schedule_frame(frame)


from re import L
import requests
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from lxml import etree
import os
from time import sleep
import xlrd
import win32api
import win32con
import xlsxwriter as xw
from openpyxl import load_workbook
# #实现无可视化界面
# from selenium.webdriver.chrome.options import Options
# #实现规避检测
# from selenium.webdriver import ChromeOptions
# #实现无可视化界面的操作
# chrome_options = Options()
# chrome_options.add_argument('--headless')
# chrome_options.add_argument('--disable-gpu')
# #实现规避检测
# option = ChromeOptions()
# option.add_experimental_option('excludeSwitches', ['enable-automation'])
# #如何实现让selenium规避被检测到的风险
# brs = webdriver.Chrome(executable_path='./selenium/chromedriver',chrome_options=chrome_options,options=option)
def Load_ExcelDone(list,n=0):
    data=xlrd.open_workbook('./paper/paper.xlsx')
    table=data.sheets()[0]
    nrows=table.nrows
    for i in range(nrows):
        try:list.append(int(float(table.row_values(i)[n])))
        except:
            continue
    print(list)

def add_information_excel(filename,paper,list_done):
    workbook=load_workbook(filename+'.xlsx')
    wb=workbook.active
    for p in paper:
        column_n='A'+str(p.n)
        column_name='B'+str(p.n)
        column_wos='C'+str(p.n)
        column_url='D'+str(p.n)
        wb[column_n]=p.n
        wb[column_name]=p.name
        wb[column_wos]=p.wos
        wb[column_url]=p.url
    workbook.save(filename+'.xlsx')

def savepage_pywin32():
    win32api.keybd_event(17, 0, 0, 0)           # 按下ctrl
    win32api.keybd_event(83, 0, 0, 0)           # 按下s
    win32api.keybd_event(83, 0, win32con.KEYEVENTF_KEYUP, 0)    # 释放s
    win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)    # 释放ctrl
    sleep(1)
    win32api.keybd_event(13, 0, 0, 0)           # 按下enter
    win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)    # 释放enter

def search(kw):
    #seach in sci
    search_input=brs.find_element_by_xpath('//input[@data-ta="search-criteria-input"]')
    try:
        wind=brs.find_element_by_id('pendo-close-guide-8fdced48')
        wind.click()                     #find serch box
        search_input.click()
        search_input.clear()
        search_input.send_keys(kw)
        search_input.send_keys(Keys.ENTER)                                          #input keywords
        butn=brs.find_element_by_xpath('//span[@class="mat-button-wrapper"]')   #find search button
        butn.click()
    except:
        search_input.click()
        search_input.clear()
        search_input.send_keys(kw)
        search_input.send_keys(Keys.ENTER)                                          #input keywords
        butn=brs.find_element_by_xpath('//span[@class="mat-button-wrapper"]')   #find search button
        butn.click()                                                            #click serch button and serch

def getsource(url,broser):
    broser.get(url)
    page_text=broser.page_source
    print('page_souce load successful')
    return page_text

def SaveHtml(HTML,Filename):
    if not os.path.exists('./paper/HTML'):
        os.makedirs('./paper/HTML')
    Filename=Filename+'.html'
    with open('./paper/HTML/'+Filename,'wb',encoding='utf-8') as fp:
        fp.write(HTML)
    return 'save successful'

def closewind(s):
    brs.find_element_by_id(s).click

def getpaper_wos_url(source):
    tree=etree.HTML(source)
    download=tree.xpath('//app-records-list//a[@data-ta="summary-record-title-link"]/@href')[0]
    Download='https://www.webofscience.com'+download
    return Download

def load_BasicExcel(lists,number,n=7):
    data=xlrd.open_workbook('./paper/gao.xlsx')
    table=data.sheets()[0]
    nrows=table.nrows
    if nrows>number:
        nrows=number
    for i in range(nrows):
        if i<=1:
            continue
        elif table.row_values(i)[n]==table.row_values(i-1)[n]:
            continue
        else :
            s=paper(i,table.row_values(i)[n],0,0)
            lists.append(s)

def getHTML(url,headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'}):
    c=requests.get(url=url,headers=headers).content
    return c

def getfulltext_url(page):
    tree=etree.HTML(page)
    c=tree.xpath('//app-full-record-links//a/@href')[0]
    return c

class paper:
    def __init__(self,n,name,wos,url):
        self.n=n
        self.name=name
        self.wos=wos
        self.url=url

brs = webdriver.Chrome(executable_path='./selenium/chromedriver')
url='https://www.webofscience.com/wos/woscc/basic-search'
#create list
Paper_list=[]           #save paper (url,name,i,)
Paper_list_result=[]
# url_fulltest_list=[]    #save url
Paper_list_failed=[]    #save paper_url_failed
Paper_list_success_n=[]
Load_ExcelDone(Paper_list_success_n)
Paper_list_done=Paper_list_success_n
load_BasicExcel(Paper_list,50) # load data

#Seach and find paper_url
for kw in Paper_list:
    if kw.n not in Paper_list_success_n:
        try:
            brs.get(url)
            sleep(6)
            search(kw.name)
            sleep(2)
            wos=getpaper_wos_url(brs.page_source)
            sleep(3)
            brs.get(wos)
            sleep(2)
            url_fulltest=getfulltext_url(brs.page_source)
            # #save page by ctrl+s - enter
            # brs.get(url_fulltest)
            # sleep(6)
            # savepage_pywin32()
            # sleep(20)
            #save in list
            kw.wos=wos
            kw.url=url_fulltest
            Paper_list_result.append(kw)
            # url_fulltest_list.append(kw.url)

            #save name of success
            # Paper_list_success.append(kw.name)
            Paper_list_success_n.append(kw.n)
            print(kw.n,'load susccessful')
        except:
            #save name of failed
            print(kw.n,'load failed')
            Paper_list_failed.append(kw)

brs.quit()

add_information_excel('./paper/paper',Paper_list_result,Paper_list_done)
add_information_excel('./paper/paper_failed',Paper_list_failed,[])


#
#  This monkey-patches M2Crypto's RSA and DSA support to allow us
#  to create keys directly from a given set of parameters.
#  It's based on the following patch from the M2Crypt bugtracker:
#
#      https://bugzilla.osafoundation.org/show_bug.cgi?id=12981
#
#  We use ctypes to avoid recompiling the M2Crypto binaries.

import ctypes
from M2Crypto import RSA, DSA, m2, __m2crypto

_m2lib = ctypes.CDLL(__m2crypto.__file__)

_m2lib.BN_free.argtypes = (ctypes.c_void_p,)
_m2lib.BN_mpi2bn.argtypes = (ctypes.c_char_p, ctypes.c_int, ctypes.c_void_p,)
_m2lib.BN_mpi2bn.restype = ctypes.c_void_p

class _RSA(ctypes.Structure):
    """OpenSSL struct representing a RSA key (struct rsa_st)."""
    _fields_ = [("pad", ctypes.c_int),
                ("version", ctypes.c_long),
                ("RSA_METHOD", ctypes.c_void_p),
                ("ENGINE", ctypes.c_void_p),
                ("n", ctypes.c_void_p),
                ("e", ctypes.c_void_p),
                ("d", ctypes.c_void_p)]
                # There are many more fields, but we don't need them.

class _DSA(ctypes.Structure):
    """OpenSSL struct representing a DSA key (struct dsa_st)."""
    _fields_ = [("pad", ctypes.c_int),
                ("version", ctypes.c_long),
                ("write_params", ctypes.c_int),
                ("p", ctypes.c_void_p),
                ("q", ctypes.c_void_p),
                ("g", ctypes.c_void_p),
                ("pub_key", ctypes.c_void_p),
                ("priv_key", ctypes.c_void_p)]
                # There are many more fields, but we don't need them.

def maybe_provide(obj):
    """Decorator to provide default implemenation of a function."""
    def decorator(func):
        if not hasattr(obj, func.__name__):
            setattr(obj, func.__name__, func)
        return func
    return decorator

@maybe_provide(m2)
def rsa_set_d(rsa, value):
    """Set the private-key component "d" of a RSA object."""
    bn = _m2lib.BN_mpi2bn(value, len(value), None)
    if not bn:
        raise RSA.RSAError("invalid private key data")
    rsa_p = ctypes.cast(ctypes.c_void_p(int(rsa)), ctypes.POINTER(_RSA))
    if rsa_p.contents.d:
        _m2lib.BN_free(rsa_p.contents.d)
    rsa_p.contents.d = bn

@maybe_provide(m2)
def dsa_set_pub(dsa, value):
    """Set the public-key component of a DSA object."""
    bn = _m2lib.BN_mpi2bn(value, len(value), None)
    if not bn:
        raise DSA.DSAError("invalid public key data")
    dsa_p = ctypes.cast(ctypes.c_void_p(int(dsa)), ctypes.POINTER(_DSA))
    if dsa_p.contents.pub_key:
        _m2lib.BN_free(dsa_p.contents.pub_key)
    dsa_p.contents.pub_key = bn

@maybe_provide(m2)
def dsa_set_priv(dsa, value):
    """Set the private-key component of a DSA object."""
    bn = _m2lib.BN_mpi2bn(value, len(value), None)
    if not bn:
        raise DSA.DSAError("invalid private key data")
    dsa_p = ctypes.cast(ctypes.c_void_p(int(dsa)), ctypes.POINTER(_DSA))
    if dsa_p.contents.priv_key:
        _m2lib.BN_free(dsa_p.contents.priv_key)
    dsa_p.contents.priv_key = bn

@maybe_provide(RSA)
def new_key(parameters):
    """Create a RSA object from the given parameters."""
    e, n, d = parameters
    rsa = m2.rsa_new()
    m2.rsa_set_e(rsa, e)
    m2.rsa_set_n(rsa, n)
    m2.rsa_set_d(rsa, d)
    return RSA.RSA(rsa, 1)

# Calling sign() on a public key will segfault M2Crypto.
# Stub it out so that it raises an error instead.
if "sign" not in RSA.RSA_pub.__dict__:
    RSA.RSA_pub.sign = RSA.RSA_pub.private_encrypt

@maybe_provide(DSA)
def load_pub_key_params(p, q, g, pub):
    """Create a DSA_pub object from parameters and key."""
    dsa = m2.dsa_new()
    m2.dsa_set_p(dsa, p)
    m2.dsa_set_q(dsa, q)
    m2.dsa_set_g(dsa, g)
    m2.dsa_set_pub(dsa, pub)
    return DSA.DSA_pub(dsa, 1)

@maybe_provide(DSA)
def load_key_params(p, q, g, pub, priv):
    """Create a DSA object from parameters and key."""
    dsa = m2.dsa_new()
    m2.dsa_set_p(dsa, p)
    m2.dsa_set_q(dsa, q)
    m2.dsa_set_g(dsa, g)
    m2.dsa_set_pub(dsa, pub)
    m2.dsa_set_priv(dsa, priv)
    return DSA.DSA(dsa, 1)


import cv2
import numpy as np

def morph_close(img, num_iter=1):
    strel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))

    for _ in range(num_iter):
        img = cv2.dilate(img, strel, iterations=1)
        img = cv2.erode(img, strel, iterations=1)

    return img

def morph_open(img, num_iter=1):
    strel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))

    for _ in range(num_iter):
        img = cv2.erode(img, strel, iterations=1)
        img = cv2.dilate(img, strel, iterations=1)

    return img

def get_guidance_points_and_weights(w, h, num, bottom_to_top=False):
    m = w // 2
    p = 0.35
    spacing = (0.9 - p) * h / (num - 1)

    # top -> bottom
    # TODO: may use f(x) = a*exp(-(x-b)^2 / (2c^2)) or a 3rd / 4th order polynomial
    weights = np.array([0.2, 0.25, 0.3, 0.4, 0.3, 0.25, 0.2, 0.2, 0.1, 0.1])
    assert num == len(weights)

    points = np.concatenate(
        (
            np.array([[m, int(p * h + i * spacing)] for i in range(num - 1)]),
            [[m, int(0.9 * h)]],
        )
    ).astype(np.int32)

    if bottom_to_top:
        weights = weights[::-1]
        points = points[::-1]

    return spacing, points, weights

def get_left_right_markers(row):
    left = (row == 255).argmax()
    right = len(row) - (row[::-1] == 255).argmax()

    return left, right

def get_point_on_lane(row, point):
    px, py = point
    left, right = row[:px], row[px:]

    ll, lr = get_left_right_markers(left)
    if ll < lr:
        return int(0.5 * (ll + lr))

    rl, rr = get_left_right_markers(right)
    if rl < rr:
        return int(0.5 * (rl + rr))

    return 0, 0

def compute_speed_delta(diffs, weights, div_factor=8, last_diffs=None):
    x = np.sum(diffs * weights)

    s = np.round(x / div_factor)
    # s = 1.6 * x / (1 + abs(x))

    return s

def get_region_of_interest(img, sx=0.23, sy=0.15, delta=200, return_vertices=False):
    """
    :param img: image to extract ROI from
    :param sx: X-axis factor for ROI bottom base
    :param sy: Y-axis factor for ROI top base
    :param delta: ROI top base length
    :param return_vertices: whether to return the ROI vertices
    :return: ROI (optional: vertices)
    """
    assert len(img.shape) == 2

    h, w = img.shape

    mask = np.zeros(img.shape)
    fill_color = 255

    vertices = np.array(
        [
            [0.5 * (w - delta), sy * h],
            [0.5 * (w + delta), sy * h],
            [(1 - sx) * w, h - 1],
            [sx * w, h - 1],
        ]
    )

    cv2.fillPoly(mask, np.array([vertices], dtype=np.int32), fill_color)

    roi = mask.astype(np.uint8) & img.astype(np.uint8)

    if return_vertices:
        return roi, vertices
    else:
        return roi

def draw_roi(img, roi_vertices) -> None:
    n = len(roi_vertices)

    for i in range(n):
        p1 = tuple(roi_vertices[i % n])
        p2 = tuple(roi_vertices[(i + 1) % n])
        cv2.line(img, p1, p2, color=(0, 255, 0), thickness=1)

def softmax(x):
    return np.exp(x) / np.sum(np.exp(x))


from flask_restful import Resource, reqparse
from db import query, connectToHost
import base64
import pymysql
from flask_jwt_extended import jwt_required

def convertToBlob(value):
    return base64.b64decode(value.encode('utf-8'))

"""
Using the resource in this module, admin can insert an image with select_status = 1.
The admin has to send just the request_no and the image.
All the other papers corresponding to the request_no get deleted
whether they have a select_status of 1 or not.
The entries in the requests table are deleted for a request_no, admin uploaded image is insered into requests.
Then active_exams and submissions table entries are deleted which have the request_no sent.
"""

class AdminQpRequest(Resource):

    @jwt_required
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('r_id', type=int, help="r_id cannot be left blank!")
        data = parser.parse_args()
        #create query string
        qstr = f""" SELECT * FROM requests where r_id = { data['r_id'] };"""
        try:
            return query(qstr)
        except:
            return {
                "message" : "There was an error connecting to the requests table while retrieving."
            }, 500

    @jwt_required
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('b_id', type=int, required=True, help="b_id cannot be left blank!")
        parser.add_argument('sem_no', type=int, required=True, help="sem_no cannot be left blank!")
        parser.add_argument('exam_type', type=str, required=True, help="exam_type cannot be left blank!")
        parser.add_argument('subtype', type=str, required=True, help="request_no cannot be left blank!")
        parser.add_argument('s_code', type=str, required=True, help="s_code cannot be left blank!")
        parser.add_argument('year', type=int, required=True, help="year cannot be left blank!")
        parser.add_argument('image', type=str, required=True, help="image cannot be left blank!")

        #parser.add_argument('select_status', type=int, required=False, default = 0)

        data = parser.parse_args()

        # a transaction is made, so not using query function from db module
        # we use connectToHost function from db module and commit explicitly
        # the query function from db module commits for each query which is not desirable in
        # a transaction sequence as follows.
        # here we execute several queries then commit.
        try:
            connection = connectToHost()

            #start connection, create cursor and execute query from cursor
            connection.begin()
            cursor = connection.cursor()

            #obtain request_no from the details provided, store in req_no

            qstr = f"""
            select DISTINCT request_no
            from timetable t
            inner join details d on (t.d_id = d.d_id)
            WHERE b_id = '{data['b_id']}' AND
            sem_no = '{data['sem_no']}' AND
            exam_type = '{data['exam_type']}' AND
            subtype = '{data['subtype']}' AND
            year = '{data['year']}' AND
            s_code = '{data['s_code']}'
            LIMIT 1;
            """

            cursor.execute(qstr)
            cursor.execute(qstr)
            result = cursor.fetchall()
            req_no = list(result[0].values())[0]

            # delete all the other entries in requests table
            # with the same request_no, whether selected or not
            qstr = f"""
            delete from requests
            where request_no = {req_no};
            """

            cursor.execute(qstr)

            #creating a tuple of values to be inserted because a formatted string is used
            #here its useful to avoid SQL syntax errors while inserting BLOB value into table
            vals_tuple = (req_no, convertToBlob(data['image']), 1 ) #set select status to 1
            #convertToBlob is used to convert base64 string to BLOB data

            qstr = f""" INSERT INTO requests (request_no, image, select_status)
                        values (%s, %s, %s); """

            cursor.execute(qstr, vals_tuple)

            # delete the corresponding entry from active_exams if paper is uploaded.
            qstr = f""" DELETE FROM active_exams
            WHERE request_no = { req_no }; """
            cursor.execute(qstr)

            # for deleting user info from submissions table
            qstr = f""" DELETE FROM User.submissions
            WHERE request_no = { req_no };
            """
            cursor.execute(qstr)

            connection.commit() #commit the changes made

            #close the cursor and connection
            cursor.close()
            connection.close()
        except IndexError:
            """
            this is to handle tuple index error
            which is raised if no data could be retrieved and stored
            where data is retrieved in this way
            result = cursor.fetchall()
            req_no = list(result[0].values())[0]
            """
            return {
                "message" : "Required data not present."
            }, 400
        except (pymysql.err.InternalError, pymysql.err.ProgrammingError, pymysql.err.IntegrityError) as e:
            return {
                "message" : "MySQL error: " + str(e)
            }, 500
        except Exception as e:
            return {
                "message" : "There was an error connecting to the requests table while inserting." + str(e)
            }, 500

        return {
            "message" : "Succesfully inserted"
        }, 200


from __future__ import absolute_import
from __future__ import unicode_literals
import csv

from datetime import datetime

import argparse
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand, CommandError

from auditcare.utils.export import write_log_events, get_users_to_export
from corehq.apps.domain.models import Domain
from corehq.apps.users.models import WebUser

def valid_date(s):
    try:
        return datetime.strptime(s, "%Y-%m-%d")
    except ValueError:
        msg = "Not a valid date: '{0}'.".format(s)
        raise argparse.ArgumentTypeError(msg)

class Command(BaseCommand):
    help = """Generate request report"""

    def add_arguments(self, parser):
        parser.add_argument('filename', help="Output file path")
        parser.add_argument(
            '-d'
            '--domain',
            dest='domain',
            help="Limit logs to only this domain"
        )
        parser.add_argument(
            '-u',
            '--user',
            dest='user',
            help="Limit logs to only this user"
        )
        parser.add_argument(
            '-s',
            '--startdate',
            dest='start',
            type=valid_date,
            help="The start date - format YYYY-MM-DD",
        )
        parser.add_argument(
            '-e',
            '--enddate',
            dest='end',
            type=valid_date,
            help="The end date - format YYYY-MM-DD",
        )
        parser.add_argument(
            '--display-superuser',
            action='store_true',
            dest='display_superuser',
            default=False,
            help="Include superusers in report, otherwise 'Dimagi User'",
        )

    def handle(self, filename, **options):
        domain = options["domain"]
        user = options["user"]
        display_superuser = options["display_superuser"]

        dimagi_username = ""
        if not display_superuser:
            dimagi_username = "Dimagi Support"

        if not domain and not user:
            raise CommandError("Please provide one of 'domain' or 'user'")

        if domain:
            domain_object = Domain.get_by_name(domain)
            if not domain_object:
                raise CommandError("Domain not found")

        users, super_users = get_users_to_export(user, domain)

        with open(filename, 'wb') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(['Date', 'User', 'Domain', 'IP Address', 'Request Path'])
            for user in users:
                write_log_events(
                    writer, user, domain,
                    start_date=options['start'], end_date=options['end']
                )

            for user in super_users:
                write_log_events(
                    writer, user, domain,
                    override_user=dimagi_username,
                    start_date=options['start'], end_date=options['end']
                )


#!/bin/python/

''' feature_join.py

    Process the

'''

import sys
import numpy as np
import sklearn as sk
from random import shuffle
from gensim import corpora, models, matutils, utils
from gensim.models import Doc2Vec
from gensim.models import Word2Vec
from gensim.models.doc2vec import LabeledSentence

class LabeledLineSentence(object):
    def __init__(self, doc_list, labels_list):
       self.labels_list = labels_list
       self.doc_list = doc_list
    def __iter__(self):
        for idx, doc in enumerate(self.doc_list):
            yield LabeledSentence(doc,['PostID_%s' % (self.labels_list[idx])])

def LSI_transform( feature_list, num_topics ):

    for (corp, vocab, name) in feature_list:
        tf_idf = models.TfidfModel( corp )
        corpus_tfIdf = tf_idf[corp]

        LSI = models.LsiModel( corpus_tfIdf, id2word=vocab, num_topics=num_topics )
        # apply the LDA transformation
        # convert from sparse to dense
        # save the matrix to disk
        np.save( name+'/'+name+'_LSI.npy',
                 np.transpose( matutils.corpus2dense(LSI[corpus_tfIdf], num_topics))
               )

    #using this tutorial: https://medium.com/@klintcho/doc2vec-tutorial-using-gensim-ab3ac03d3a1#.ymtcbtlk2
    #and this ref: https://linanqiu.github.io/2015/10/07/word2vec-sentiment/
def doc2vec(title_size, body_size, tag_size):
    postids = np.load("fixed_width/postids.npy")

    titles = np.load("title/title.npy")
    title_it = LabeledLineSentence(titles, postids)

    bodies = np.load("body/bodies.npy")
    body_it = LabeledLineSentence(bodies, postids)

    #tags = np.load("tags/tags.npy")
    #tags_it = LabeledLineSentence(tags, postids)

    #see param docs: http://radimrehurek.com/gensim/models/doc2vec.html
    #need to fine-tune on a larger sub-sample
    title_model = Doc2Vec(size=title_size, window=10, min_count=5, alpha=.025, min_alpha=.025)
    title_model.build_vocab(title_it)

    body_model = Doc2Vec(size=body_size, window=10, min_count=5, alpha=.025, min_alpha=.025)
    body_model.build_vocab(body_it)

    #tags_model = Doc2Vec(size=tag_size, window=10, min_count=166, alpha=.025, min_alpha=.025)
    #tags_model.build_vocab(tags_it)

    for epoch in range(10):
        title_model.train(title_it)
        title_model.alpha -= .002
        title_model.min_alpha = title_model.alpha

        body_model.train(body_it)
        body_model.alpha -= .002
        body_model.min_alpha = body_model.alpha

        #tags_model.train(tags_it)
        #tags_model.alpha -= .002
        #tags_model.min_alpha = tags_model.alpha

    title_vecs = np.zeros((len(titles), title_size))
    body_vecs = np.zeros((len(bodies), body_size))
    #tag_vecs = np.zeros((len(tags), tag_size))

    for idx,postid in enumerate(postids):
    	title_vecs[idx] = title_model.docvecs['PostID_%s' % postid]
    	body_vecs[idx] = body_model.docvecs['PostID_%s' % postid]
    	#tag_vecs[idx] = tags_model.docvecs['PostID_%s' % postid]

    title_model.save("title/doc2vec.title_model")
    np.save("title/title_vecs.npy", title_vecs)
    body_model.save("body/doc2vec.body_model")
    np.save("body/body_vecs.npy", body_vecs)
    #np.save("tags/tag_vecs.npy", tag_vecs)
    return(title_model, body_model)

def main():
    titleVocab = corpora.Dictionary.load("title/title_vocab.dict")
    titleCorpus = corpora.MmCorpus("title/title_word_corpus.mm")
    bodyVocab = corpora.Dictionary.load("body/body_vocab.dict")
    bodyCorpus = corpora.MmCorpus("body/body_word_corpus.mm")
    tagsVocab = corpora.Dictionary.load("tags/tags_vocab.dict")
    tagsCorpus = corpora.MmCorpus("tags/tags_word_corpus.mm")

    ''' hacky way to iterate over the corpora '''
    feature_list = [ ( titleCorpus, titleVocab, "title" ),
                     ( bodyCorpus, bodyVocab, "body" ),
                     ( tagsCorpus, tagsVocab, "tags" ) ]

    LSI_transform( feature_list, num_topics=50 )

if __name__ == '__main__':
    title_model, body_model = doc2vec(200, 200, 50)

    #inspect model performance
    '''
    print(body_model.doesnt_match("trouble issue problem python".split()))
    print(body_model.doesnt_match("html css compiler".split()))
    print(body_model.most_similar("javascript"))
    print(body_model.infer_vector("html test code problem".split()))
    print(body_model.similarity('html','css'))
    print(body_model.similarity('java','java'))
    print(body_model.similarity('compiler','python'))
    print(body_model.docvecs[0])
    print(body_model.docvecs['PostID_4'])
    print(body_model.docvecs.most_similar('PostID_4'))
    print(body_model.docvecs.most_similar('PostID_16114'))
  	'''


"""
Definition of views.
"""

from django.shortcuts import render,get_object_or_404
from django.http import HttpRequest
from django.template import RequestContext
from datetime import datetime
from django.http.response import HttpResponse, Http404
from django.http import HttpResponseRedirect, HttpResponse
from .models import Question,Choice,User, QuestionQuiz, Answers
from django.template import loader
from django.core.urlresolvers import reverse
from app.forms import QuestionForm, ChoiceForm,UserForm, QuestionFormQuiz, AnswerForm
from django.shortcuts import redirect
import json
from django.contrib import messages
from django.db.models import Count

def home(request):
    """Renders the home page."""
    assert isinstance(request, HttpRequest)
    return render(
        request,
        'app/index.html',
        {
            'title':'Home Page',
            'year':datetime.now().year,
        }
    )

def contact(request):
    """Renders the contact page."""
    assert isinstance(request, HttpRequest)
    return render(
        request,
        'app/contact.html',
        {
            'title':'Autor de la web',
            'message':'Datos de contacto',
            'year':datetime.now().year,
        }
    )

def about(request):
    """Renders the about page."""
    assert isinstance(request, HttpRequest)
    return render(
        request,
        'app/about.html',
        {
            'title':'About',
            'message':'Your application description page.',
            'year':datetime.now().year,
        }
    )
def index(request):
     if request.method == "POST":
         selected_theme = request.POST['mythemes']
         if selected_theme == "":
             latest_question_list = QuestionQuiz.objects.order_by('-pub_date')
         else:
            latest_question_list = QuestionQuiz.objects.order_by('-pub_date').filter(question_theme=selected_theme)
         template = loader.get_template('polls/index.html')
         themes = QuestionQuiz.objects.values('question_theme').distinct()
         context = {
                'title':'Lista de preguntas de la encuesta',
                'latest_question_list': latest_question_list,
                'themes' : themes,
                'mytheme' : selected_theme,
                }
     else:
         latest_question_list = QuestionQuiz.objects.order_by('-pub_date')
         template = loader.get_template('polls/index.html')
         themes = QuestionQuiz.objects.values('question_theme').distinct()
    #distinct = QuestionQuiz.objects.values('question_theme').annotate(theme_count=Count('question_theme')).filter(theme_count=1)
    #themes = QuestionQuiz.objects.filter(question_theme__in=[item['question_theme'] for item in distinct])
    #themes = [obj.question_theme for obj in latest_question_list]
    #themes = QuestionQuiz.objects.order_by('-pub_date').values('question_theme').distinct('question_theme')
         context = {
                'title':'Lista de preguntas de la encuesta',
                'latest_question_list': latest_question_list,
                'themes' : themes,
                }

     return render(request, 'polls/index.html', context)

def detail(request, question_id):
     question = get_object_or_404(QuestionQuiz, pk=question_id)
     return render(request, 'polls/detail.html', {'title':'Respuestas asociadas a la pregunta:','question': question})

def results(request, question_id, answer):
    question = get_object_or_404(QuestionQuiz, pk=question_id)
    a = get_object_or_404(Answers, pk=answer)
    if a.correct == True :
        resultado = 'Has contestado ' + a.answer +' y has acertado!'
    else:
        resultado = 'Has contestado ' + a.answer +' y has fallado.'
    return render(request, 'polls/results.html', {'title':'Resultados de la pregunta:','question': question, 'resultado': resultado, 'bool': a.correct,})

def vote(request, question_id):
    p = get_object_or_404(QuestionQuiz, pk=question_id)
    try:
        selected_choice = p.answers_set.get(pk=request.POST['choice'])
    except (KeyError, Answers.DoesNotExist):
        # Vuelve a mostrar el form.
        return render(request, 'polls/detail.html', {
            'question': p,
            'error_message': "ERROR: No se ha seleccionado una opcion",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Siempre devolver un HttpResponseRedirect despues de procesar
        # exitosamente el POST de un form. Esto evita que los datos se
        # puedan postear dos veces si el usuario vuelve atras en su browser.
        return HttpResponseRedirect(reverse('results', args=(p.id, selected_choice.id,)))

def question_new(request):
        if request.method == "POST":
            form = QuestionFormQuiz(request.POST)
            if form.is_valid():
                question = form.save(commit=False)
                question.pub_date=datetime.now()
                question.save()
                #return redirect('detail', pk=question_id)
                #return render(request, 'polls/index.html', {'title':'Respuestas posibles','question': question})
        else:
            form = QuestionFormQuiz()
        return render(request, 'polls/question_new.html', {'form': form})

def choice_add(request, question_id):
        question = QuestionQuiz.objects.get(id = question_id)
        if request.method =='POST':
            form = AnswerForm(request.POST)
            if form.is_valid():
                if question.answers_set.count() < 4:
                    choice = form.save(commit = False)
                    choice.question = question
                    choice.vote = 0
                    choice.save()
                    #form.save()
                else:
                   #messages.info(request, 'Esta pregunta ya tiene 4 respuestas.')
                   return render(request, 'polls/detail.html', {'title':'Respuestas asociadas a la pregunta:','question': question, 'max_answ':'Esta pregunta ya tiene 4 respuestas.',})
        else:
            form = AnswerForm()
        #return render_to_response ('choice_new.html', {'form': form, 'poll_id': poll_id,}, context_instance = RequestContext(request),)
        return render(request, 'polls/choice_new.html', {'title':'Pregunta:'+ question.question_text,'form': form, 'ans_count': question.answers_set.count(),})

def chart(request, question_id):
    q=QuestionQuiz.objects.get(id = question_id)
    qs = Answers.objects.filter(question=q)
    dates = [obj.answer for obj in qs]
    counts = [obj.votes for obj in qs]
    context = {
        'dates': json.dumps(dates),
        'counts': json.dumps(counts),
    }

    return render(request, 'polls/grafico.html', context)

def user_new(request):
        if request.method == "POST":
            form = UserForm(request.POST)
            if form.is_valid():
                user = form.save(commit=False)
                user.save()
                #return redirect('detail', pk=question_id)
                #return render(request, 'polls/index.html', {'title':'Respuestas posibles','question': question})
        else:
            form = UserForm()
        return render(request, 'polls/user_new.html', {'form': form})

def users_detail(request):
    latest_user_list = User.objects.order_by('email')
    template = loader.get_template('polls/users.html')
    context = {
                'title':'Lista de usuarios',
                'latest_user_list': latest_user_list,
              }
    return render(request, 'polls/users.html', context)


import os
import os.path
import shutil
import logging
import yaml
from piecrust import (
        RESOURCES_DIR, THEME_DIR, THEME_CONFIG_PATH, THEME_INFO_PATH)
from piecrust.commands.base import ChefCommand

logger = logging.getLogger(__name__)

class ThemesCommand(ChefCommand):
    def __init__(self):
        super(ThemesCommand, self).__init__()
        self.name = 'themes'
        self.description = "Manage the themes for the current website."

    def setupParser(self, parser, app):
        if app.root_dir is None:
            return

        subparsers = parser.add_subparsers()
        p = subparsers.add_parser(
                'create',
                help="Create a new theme for the current website.")
        p.add_argument(
                '--from-default',
                action='store_true',
                help=("Create a new theme by copying the default PieCrust "
                      "theme into the theme directory"))
        p.add_argument(
                'theme_name',
                help=("The name of the theme"))
        p.set_defaults(sub_func=self._createTheme)

        p = subparsers.add_parser(
                'override',
                help="Copies a theme to the website for customization.")
        p.set_defaults(sub_func=self._overrideTheme)

    def checkedRun(self, ctx):
        ctx.args.sub_func(ctx)

    def _createTheme(self, ctx):
        theme_dir = os.path.join(ctx.app.root_dir, THEME_DIR)
        if os.path.exists(theme_dir):
            logger.warning("A theme already exists, and will be overwritten. "
                           "Are you sure? [Y/n]")
            ans = input()
            if len(ans) > 0 and ans.lower() not in ['y', 'yes']:
                return 1

            shutil.rmtree(theme_dir)

        try:
            if ctx.args.from_default:
                def reporting_copy2(src, dst):
                    rel_dst = os.path.relpath(dst, ctx.app.root_dir)
                    logger.info(rel_dst)
                    shutil.copy2(src, dst)

                default_theme_dir = os.path.join(RESOURCES_DIR, 'theme')
                shutil.copytree(default_theme_dir, theme_dir,
                                copy_function=reporting_copy2)
                return 0

            logger.info("Creating theme directory.")
            os.makedirs(theme_dir)

            logger.info("Creating theme_config.yml")
            config_path = os.path.join(theme_dir, THEME_CONFIG_PATH)
            with open(config_path, 'w', encoding='utf8') as fp:
                fp.write('')

            logger.info("Creating theme_info.yml")
            info_path = os.path.join(theme_dir, THEME_INFO_PATH)
            with open(info_path, 'w', encoding='utf8') as fp:
                yaml.dump(
                        {
                            'name': ctx.args.theme_name or 'My New Theme',
                            'description': "A new PieCrust theme.",
                            'authors': ['Your Name Here <email or twitter>'],
                            'url': 'http://www.example.org'},
                        fp,
                        default_flow_style=False)
            return 0
        except:
            logger.error("Error occured, deleting theme directory.")
            shutil.rmtree(theme_dir)
            raise

    def _overrideTheme(self, ctx):
        app_dir = ctx.app.root_dir
        theme_dir = ctx.app.theme_dir
        if not theme_dir:
            logger.error("There is not theme currently applied to override.")
            return 1

        copies = []
        for dirpath, dirnames, filenames in os.walk(theme_dir):
            rel_dirpath = os.path.relpath(dirpath, theme_dir)
            for name in filenames:
                if (dirpath == theme_dir and
                        name in [THEME_CONFIG_PATH, THEME_INFO_PATH]):
                    continue
                src_path = os.path.join(dirpath, name)
                dst_path = os.path.join(app_dir, rel_dirpath, name)
                copies.append((src_path, dst_path))

        conflicts = []
        for c in copies:
            if os.path.exists(c[1]):
                conflicts.append(c[1])
        if conflicts:
            logger.warning("Some website files will be overwritten:")
            for c in conflicts:
                logger.warning(os.path.relpath(c, app_dir))
            logger.warning("Are you sure? [Y/n]")
            ans = input()
            if len(ans) > 0 and ans.lower() not in ['y', 'yes']:
                return 1

        for c in copies:
            logger.info(os.path.relpath(c[1], app_dir))
            if not os.path.exists(os.path.dirname(c[1])):
                os.makedirs(os.path.dirname(c[1]))
            shutil.copy2(c[0], c[1])


import logging as logg
from sys import stdout

DG_loger = logg.Logger('Dg_loger')
DG_debug_hendler = logg.FileHandler('DG_debug_log.txt', 'w')
DG_info_hendler = logg.StreamHandler(stdout)

DG_debug_hendler.setLevel(logg.DEBUG)
DG_info_hendler.setLevel(logg.INFO)

DG_debug_formater = logg.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
Dg_info_formater = logg.Formatter('%(message)s')

DG_debug_hendler.setFormatter(DG_debug_formater)
DG_info_hendler.setFormatter(Dg_info_formater)

DG_loger.addHandler(DG_debug_hendler)
DG_loger.addHandler(DG_info_hendler)


# 使用Scharr变换找到边缘幅度。相比其他方法有更好的旋转不变性

from skimage.filters import scharr, scharr_h, scharr_v
from skimage import io, img_as_float
from skimage.morphology import disk
from skimage.color import rgb2gray

image = io.imread('/home/qiao/PythonProjects/Scikit-image_On_CT/Test_Img/4.jpg')
# image = rgb2gray(image)
io.imshow(image)
io.show()

md = scharr(image)
io.imshow(md)
io.show()

md = scharr_h(image)
io.imshow(md)
io.show()

md = scharr_v(image)
io.imshow(md)
io.show()


import cv2
import os
import numpy as np

matching_results = {}
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for file in files:
	if file.endswith(".ppm"):
		match = False
		image1 = cv2.imread(file)
		img1_gray = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)

		height, width = img1_gray.shape

		for file1 in files:
			if file1.endswith(".ppm") and file != file1:
				image2 = cv2.imread(file1)
				img2_gray = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
				# image_2 = image_resize(img2_gray, height = 800)

				height1, width1 = img2_gray.shape
				if height == height1 and width == width1:
					difference = img1_gray - img2_gray
					result = not np.any(difference)
					if result == True:
						matching_results[str(file1)] = True
					else:
						if str(file1) in matching_results:
							if matching_results[str(file1)] != True:
								matching_results[str(file1)] = False

for key, value in matching_results.items():
	if value == False:
		print('No match found for the image : ',key)
	else:
		print('Match found for the file : ', key)


import pandas as pd
from bs4 import BeautifulSoup
import requests
from datetime import date

urlfinancials = 'https://www.marketwatch.com/investing/stock/msft/financials'
urlbalancesheet = 'https://www.marketwatch.com/investing/stock/msft/financials/balance-sheet'

text_soup_financials = BeautifulSoup(
    requests.get(urlfinancials).text, "lxml")  # read in
text_soup_balancesheet = BeautifulSoup(
    requests.get(urlbalancesheet).text, "lxml")  # read in

# build lists for Income statement
titlesfinancials = text_soup_financials.findAll(
    'td', attrs={'class': 'overflow__cell'})
# print(titlesfinancials)
epslist = []
netincomelist = []
longtermdebtlist = []
interestexpenselist = []
ebitdalist = []

for title in titlesfinancials:
    if 'EPS (Basic)' in title.text:
        for td in title.findNextSiblings(attrs={'class': 'overflow__cell'}):
            epslist.append(td.text)
    if 'Net Income' in title.text:
        for td in title.findNextSiblings(attrs={'class': 'overflow__cell'}):
            netincomelist.append(td.text)
    if 'Interest Expense' in title.text:
        for td in title.findNextSiblings(attrs={'class': 'overflow__cell'}):
            interestexpenselist.append(td.text)
    if 'EBITDA' in title.text:
        for td in title.findNextSiblings(attrs={'class': 'overflow__cell'}):
            ebitdalist.append(td.text)

# # find the table headers for the Balance sheet
titlesbalancesheet = text_soup_balancesheet.findAll(
    'td', {'class': 'overflow__cell'})
equitylist = []
for title in titlesbalancesheet:
    if 'Total Shareholders\' Equity' in title.text:
        for td in title.findNextSiblings(attrs={'class': 'overflow__cell'}):
            equitylist.append(td.text)
    if 'Long-Term Debt' in title.text:
        for td in title.findNextSiblings(attrs={'class': 'overflow__cell'}):
            longtermdebtlist.append(td.text)

def get_element(list, element):
    try:
        return list[element]
    except:
        return '-'

# # get the data from the income statement lists
# # use helper function get_element
# eps = get_element(epslist, 4)
# epsGrowth = get_element(epslist, 4)
# netIncome = get_element(netincomelist, 4)
# shareholderEquity = get_element(equitylist, 4)
# roa = get_element(equitylist, 4)

# longtermDebt = get_element(longtermdebtlist, 4)
# interestExpense = get_element(interestexpenselist, 4)
# ebitda = get_element(ebitdalist, 4)

# load all the data into dataframe
fin_df = pd.DataFrame({'eps': epslist[:5], 'net Income': netincomelist[:5], 'shareholder Equity': equitylist[:5],
                       'longterm Debt': longtermdebtlist[:5], 'interest Expense': interestexpenselist[:5], 'ebitda': ebitdalist[:5]},
                      index=range(date.today().year-5, date.today().year))

fin_df.reset_index(inplace=True)
print(fin_df)
# return fin_df

# def get_element(list, element):
#     try:
#         return list[element]
#     except:
#         return '-'

# content = driver.page_source
# soup = BeautifulSoup(content)
# for a in soup.findAll('a',href=True, attrs={'class':'_31qSD5'}):
#     name=a.find('div', attrs={'class':'_3wU53n'})
#     price=a.find('div', attrs={'class':'_1vC4OE _2rQ-NK'})
#     rating=a.find('div', attrs={'class':'hGSR34 _2beYZw'})
#     products.append(name.text)
#     prices.append(price.text)
#     ratings.append(rating.text)
# df = pd.DataFrame({'Product Name':products,'Price':prices,'Rating':ratings})
# df.to_csv('products.csv', index=False, encoding='utf-8')


#!/usr/bin/env python

import rospy
from nav_msgs.msg import Odometry
import tf
import numpy as np
import matplotlib.pyplot as plt
import time

start_time = 0.0
t_ = 0.0
roll_ = 0.0
pitch_ = 0.0

def callback(msg):
    # print('callback')

    global start_time
    global t_
    global roll_
    global pitch_

    e = tf.transformations.euler_from_quaternion((msg.pose.pose.orientation.x, msg.pose.pose.orientation.y, msg.pose.pose.orientation.z, msg.pose.pose.orientation.w))

    if start_time>0:
        t_ = time.time() - start_time
        roll_ = e[0]
        pitch_ = e[1]

def graph():
    print('graph_rpy')
    rospy.init_node('graph_rpy', anonymous=True)
    rospy.Subscriber("/imu_odometry", Odometry, callback)

    t = [0 for i in range(50)]
    roll = [0 for j in range(50)]
    pitch = [0 for k in range(50)]

    plt.ion()
    plt.figure()

    ### roll ###
    plt.subplot(2, 1, 1)
    # plt.title("roll")
    plt.xlabel("time[s]")
    plt.ylabel("roll[deg]")
    plt.ylim(-5, 5)
    plt.grid(True)
    li_r, = plt.plot(t, roll)

    ### pitch ###
    plt.subplot(2, 1, 2)
    # plt.title("pitch")
    plt.xlabel("time[s]")
    plt.ylabel("pitch[deg]")
    plt.ylim(-5, 5)
    plt.grid(True)
    li_p, = plt.plot(t, pitch)

    global start_time
    global t_
    global roll_
    global pitch_

    start_time = time.time()

    while not rospy.is_shutdown():
        # print('loop')

        t.append(t_)
        t.pop(0)
        roll.append(roll_/np.pi*180.0)
        roll.pop(0)
        pitch.append(pitch_/np.pi*180.0)
        pitch.pop(0)

        ### roll ###
        plt.subplot(2,1,1)
        li_r.set_xdata(t)
        li_r.set_ydata(roll)
        plt.xlim(min(t), max(t))
        # plt.draw()

        ### roll ###
        plt.subplot(2,1,2)
        li_p.set_xdata(t)
        li_p.set_ydata(pitch)
        plt.xlim(min(t), max(t))

        plt.draw()
        plt.pause(0.1)
    rospy.spin()

if __name__ == '__main__':
    graph()


#!/usr/bin/python
import os
from datetime import datetime
import mimetypes

# virtenv = os.environ['OPENSHIFT_PYTHON_DIR'] + '/virtenv/'
# virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
# try:
#     execfile(virtualenv, dict(__file__=virtualenv))
# except IOError:
#     pass

#
# IMPORTANT: Put any additional includes below this line.  If placed above this
# line, it's possible required libraries won't be in your searchable path
#

def show_404_app(environ, start_response):
    ctype = 'text/html'
    response_headers = [('Content-Type', ctype)]
    start_response('404 Not Found', response_headers)
    response_body = '<title>Not found</title>
Page not found in our servers. Visit the main page.
'
    return response_body

def content_type(path):
    """Return a guess at the mime type for this path
    based on the file extension"""
    mime_type, discard = mimetypes.guess_type(path)
    if mime_type != None:
        return mime_type
    return "application/octet-stream"

def last_modified(path):
    return datetime.utcfromtimestamp(os.path.getmtime(path)).strftime('%a, %d %b %Y %H:%M:%S GMT')

def static_app(environ, start_response):
    """Serve static files"""
    mypath = os.path.dirname(os.path.realpath(__file__))

    path = environ['PATH_INFO']

    for char in ('..', '*', '!', '`', '$', '<', '>'):
        path = path.replace(char, '')

    path = mypath + '/static' + path

    if path.endswith(('py', 'pyc', 'sh')):
        return show_404_app(environ, start_response)
    elif path.endswith('/'):
        path += 'index.html'
    if os.path.exists(path):
        with open(path, 'rb') as f:
            content = f.read()
        ctype = content_type(path)
        headers = [('Content-Type', ctype), \
                   ('Last-Modified', last_modified(path)), \
                   ('Content-Length', str(len(content)))]
        start_response('200 OK', headers)
        # if ctype == 'application/pdf' and os.path.exists(mypath + '/../logs'):
        return [content]
    else:
        return show_404_app(environ, start_response)

# the main WSGI application
def application(environ, start_response):
    pi = environ['PATH_INFO']
    qs = environ['QUERY_STRING']
    return static_app(environ, start_response)

#
# Below for testing only
#
if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    httpd = make_server('localhost', 8051, application)
    # Wait for a single request, serve it and quit.
    # httpd.handle_request()

    # Serve until process is killed
    httpd.serve_forever()


import unittest

import lore.transformers
import numpy
import pandas
import datetime

class TestAreaCode(unittest.TestCase):
    def setUp(self):
        self.transformer = lore.transformers.AreaCode('phone')

    def test_phone_formats(self):
        values = pandas.DataFrame({
            'phone': [
                '12345678901',
                '+12345678901',
                '1(234)567-8901',
                '1 (234) 567-8901',
                '1.234.567.8901',
                '1-234-567-8901',
                '2345678901',
                '234.567.8901',
                '(234)5678901',
                '(234) 567-8901',
            ]
        })
        result = self.transformer.transform(values)
        self.assertEqual(result.tolist(), numpy.repeat('234', len(values)).tolist())

    def test_bad_data(self):
        values = pandas.DataFrame({
            'phone': [
                '1234567',
                '(123)4567',
                '',
                None,
                12345678901,
            ]
        })
        result = self.transformer.transform(values)
        self.assertEqual(result.tolist(), ['', '', '', None, ''])

class TestEmailDomain(unittest.TestCase):
    def setUp(self):
        self.transformer = lore.transformers.EmailDomain('email')

    def test_transform(self):
        values = pandas.DataFrame({
            'email': [
                'montana@instacart.com',
                'sue-bob+anne@instacart.com'
            ]
        })
        result = self.transformer.transform(values)
        self.assertEqual(result.tolist(), numpy.repeat('instacart.com', len(values)).tolist())

class TestNameFamilial(unittest.TestCase):
    def setUp(self):
        self.transformer = lore.transformers.NameFamilial('name')

    def test_transform(self):
        values = pandas.DataFrame({
            'name': [
                'mom',
                'Dad',
                'sue bob'
            ]
        })
        result = self.transformer.transform(values)
        self.assertEqual(result.tolist(), [True, True, False])

class TestDateTime(unittest.TestCase):
    def test_transform_day_of_week(self):
        transformer = lore.transformers.DateTime('test', 'dayofweek')
        data = pandas.DataFrame({'test': [datetime.datetime(2016, 12, 31), datetime.date(2017, 1, 1)]})
        transformed = transformer.transform(data)
        self.assertEqual(transformed.iloc[0] + 1, transformed.iloc[1])

class TestAge(unittest.TestCase):
    def test_transform_age(self):
        transformer = lore.transformers.Age('test', 'days')
        yesterday = datetime.datetime.now() - datetime.timedelta(days=1)

        data = pandas.DataFrame({'test': [datetime.datetime.now(), yesterday]})
        transformed = transformer.transform(data)
        self.assertEqual(transformed.astype(int).tolist(), [0, 1])

class TestNameAge(unittest.TestCase):
    def test_transform_name(self):
        transformer = lore.transformers.NameAge('test')

        data = pandas.DataFrame({'test': ['bob', 'Bob']})
        transformed = transformer.transform(data)
        self.assertTrue(transformed.iloc[0] > 0)
        self.assertEqual(transformed.iloc[0], transformed.iloc[1])

class TestNameSex(unittest.TestCase):
    def test_transform_name(self):
        transformer = lore.transformers.NameSex('test')

        data = pandas.DataFrame({'test': ['bob', 'Bob']})
        transformed = transformer.transform(data)
        self.assertTrue(transformed.iloc[0] > 0)
        self.assertEqual(transformed.iloc[0], transformed.iloc[1])

class TestNamePopulation(unittest.TestCase):
    def test_transform_name(self):
        transformer = lore.transformers.NamePopulation('test')

        data = pandas.DataFrame({'test': ['bob', 'Bob']})
        transformed = transformer.transform(data)
        self.assertTrue(transformed.iloc[0] > 0)
        self.assertEqual(transformed.iloc[0], transformed.iloc[1])

class TestStringLower(unittest.TestCase):
    def test_transform_name(self):
        transformer = lore.transformers.String('test', 'lower')

        data = pandas.DataFrame({'test': ['bob', 'Bob']})
        transformed = transformer.transform(data)
        self.assertEqual(transformed.iloc[0], 'bob')
        self.assertEqual(transformed.iloc[1], 'bob')


#!/usr/bin/env python
#encoding:utf-8
import numpy as np
import pandas as pd
import matplotlib as mpl
mpl.use('Agg')
from matplotlib.pyplot import *

import sys

#mpl.rcParams['font.family'] = ['sans-serif']
#mpl.rcParams['font.sans-serif'] = ['SimHei']
myfont = matplotlib.font_manager.FontProperties(fname='/usr/share/fonts/truetype/simhei.ttf')
begin_date = sys.argv[1]
end_date = sys.argv[2]
#begin_date = '2015-06-01'
#end_date = '2015-08-07'
SCALE = 20

figure(figsize=(20,12), dpi=200)
title(begin_date + u' ~ ' + end_date + u'  沪市每日融券余额及增减图', fontproperties=myfont, fontsize = 28)
#df = pd.read_csv('sh_rzrq_result.txt', index_col=0, parse_dates=True)
df = pd.read_csv('../../data/sh_rzrq_result_all.txt', index_col=0)
df = df.sort_index().loc[begin_date:end_date,'rqzj']

df1 = df.copy()
df1[df1.values < 0] = 0
df1[df1.values > SCALE] = SCALE
df1.plot(kind='bar', color='red', label=u'较前一日0-%s亿的增加量' %SCALE)

df2 = df.copy()
df2[df2.values > 0] = 0
df2[df2.values < -SCALE] = -SCALE
df2.plot(kind='bar', color='green', label=u'较前一日0-%s亿的减少量' %SCALE)

legend(loc='best')
ylim(-SCALE * 1.05, SCALE * 1.05)
#yticks([-SCALE, -SCALE*3/4, -SCALE/2, -SCALE/4, 0, SCALE/4, SCALE/2, SCALE*3/4, SCALE],
#       [r'$-20$', r'$-15$', r'$-10$', r'$-5$', r'$0$', r'$5$', r'$10$', r'$15$', r'$20$'], fontsize=18)
#ylabel(u'每日融券增减 (亿)', fontproperties=myfont, fontsize=18)
yticks([-SCALE, -SCALE/2, 0, SCALE/2, SCALE],
       [r'$0$', r'$25$', r'$50$', r'$75$', r'$100$'], fontsize=24)
ylabel(u'每日融券余额 (亿)', fontproperties=myfont, fontsize=24)
axhline(0,linestyle='-',linewidth=1, color='black')
grid(True)
ax1 = gca()
leg = ax1.get_legend()
ltext  = leg.get_texts()
setp(ltext, fontproperties=myfont, fontsize=18)
ax2 = ax1.twinx()

df7 = pd.read_csv('../../data/sh_rzrq_result_all.txt', index_col=0)
df7 = df7.sort_index().loc[begin_date:end_date,'rqye']
df7 = (df7 - 50) / 2.5
df7.plot(color="#EE00EE",linewidth=2.5,linestyle="-")

#xticks([])
x_min, x_max = ax2.get_xlim()
xlim(x_min - 1, x_max + 1)
ylim(-SCALE * 1.05, SCALE * 1.05)
#yticks([-SCALE, -SCALE*3/4, -SCALE/2, -SCALE/4, 0, SCALE/4, SCALE/2, SCALE*3/4, SCALE],
#       [r'$-20$', r'$-15$', r'$-10$', r'$-5$', r'$0$', r'$5$', r'$10$', r'$15$', r'$20$'], fontsize=18)
#yticks([-SCALE, -SCALE/2, 0, SCALE/2, SCALE],
#       [r'$0$', r'$25$', r'$50$', r'$75$', r'$100$'], fontsize=18)
#ylabel(u'每日融券余额 (亿)', fontproperties=myfont, fontsize=18)
yticks([-SCALE, -SCALE*3/4, -SCALE/2, -SCALE/4, 0, SCALE/4, SCALE/2, SCALE*3/4, SCALE],
       [r'$-20$', r'$-15$', r'$-10$', r'$-5$', r'$0$', r'$5$', r'$10$', r'$15$', r'$20$'], fontsize=24)
ylabel(u'每日融券增减 (亿)', fontproperties=myfont, fontsize=24)

savefig(u'../../pic/sh_rq_%s_Fig.jpg' %end_date, dpi=200)