"""Process_pathconn"""
"""01/02/2021"""
# Def process_pathconn(data_name,connectivity_ipsi_name,connectivity_contra_name,ROI_coor_name, Snca_expression):
# Scna optional, ROI_coor ==> Depends on the data from the lab
# Debuter par la localisation des différent fichiers ?//Condition initiale?
#######################
#######LOAD DATA#######
#######################

import pandas as pd
import pickle
from Pipeline import *

basedir = params[0]  # Extract the basedir from params
os.chdir(basedir)  # Sets the wd
savedir = os.path.join(basedir, opdir, "processed")
try:
    os.mkdir(savedir)
except WindowsError as error:
    print(error)  # Prevent the algorithm to stop if the folder is already created. For Mac users need to replace by OSError.

data = pd.read_csv("../Data83018/data.csv", header=0)  # DataFrame with header, alpha-syn quantified in each brain region
connectivity_ipsi = pd.read_csv("../Data83018/connectivity_ipsi.csv", index_col=0) # Connectivity table
connectivity_contra = pd.read_csv("../Data83018/connectivity_contra.csv", index_col=0)

########################
##PROCESS REGION NAMES##
########################

conn_names = [i.split(' (')[0] for i in connectivity_contra.columns] # Extract the name of the brain regions but not the subregions
path_names = data.columns[2::]  # Extraction of the path names, they start from the column 3.

c_path_names_contra = [path_names[i] for i in range(0, len(path_names)) if
                       path_names[i][0] == "c"]  # Extraction of the names of the regions starting by a "c"
path_names_contra = [i[1:] for i in c_path_names_contra]  # Same without the first letter (to fit with conn.names)

i_path_names_ipsi = [path_names[i] for i in range(0, len(path_names)) if path_names[i][0] == "i"]
path_names_ipsi = [i[1:] for i in i_path_names_ipsi]

# Order path and connectivity by the same name
path_data_ipsi = data.loc[:, i_path_names_ipsi]  # Creates a DataFrame with reorganized columns
path_data_contra = data.loc[:, c_path_names_contra]

ordered_matched_ipsi = []
for i in range(0, len(conn_names)): # len(conn_names) = 58
    for k in range(0, len(path_names_ipsi)): # len(path_names_ipsi) =
        if path_names_ipsi[k] == conn_names[i]:
            ordered_matched_ipsi.append(k)
            # Returns a list containing the ranks of path_names_ipsi that fits conn.names

ordered_matched_contra = []
for i in range(0, len(conn_names)):
    for k in range(0, len(path_names_contra)):
        if path_names_contra[k] == conn_names[i]:
            ordered_matched_contra.append(k)
            # Returns a list containing the ranks of path_names_contra that fits conn.names

# Creation of path_data
path_data = pd.concat([data.loc[:, data.columns[0:2]._index_data],
                       path_data_ipsi.loc[:, path_data_ipsi.columns._index_data[ordered_matched_ipsi]],
                       path_data_contra.loc[:, path_data_contra.columns._index_data[ordered_matched_contra]]], axis=1)
# path_data is a DataFrame that contains the different timepoints, the different conditions,
# the sorted region columns (from ipsi to contra) containing the data of the quantified alpha synuclein

path_data = path_data.rename(columns={"MBSC Region": "Conditions"})  # Renaming a column

# tile matrix such that sources are rows, columns are targets (Oh et al. 2014 Fig 4)
connectivity_ipsi.columns = conn_names  # Sets the names of the columns and the index to be the same using the list conn_names
connectivity_ipsi.index = conn_names    #

connectivity_contra.columns = conn_names
connectivity_contra.index = conn_names

# Creation of the adjacency matrix
W = pd.concat([pd.concat([connectivity_ipsi, connectivity_contra], axis=1),
               pd.concat([connectivity_contra, connectivity_ipsi], axis=1)], axis=0)

# Connectivity Matrix
n_regions = len(W) # 116

# Checking if the matrix was tiled properly
if (((W.iloc[0:57,0:57] != W.iloc[0:57,0:57]).sum()).sum()>0): # Summing over columns and then rows
    print("Matrix failed to concatenate") # If False the double sum equals 0

# retain indices to reorder like original data variable for plotting on mouse brains
ROInames = ["i" + i for i in conn_names] + ["c" + i for i in
                                            conn_names]  # List of ROI w/ first the contro regions
# and then the ipsi regions.
orig_order = []
for i in range(0, len(ROInames)): # Reordering according to ROInames
    for k in range(0, len(data.columns._index_data) - 2):
        if ROInames[k] == data.columns._index_data[2::][i]:
            orig_order.append(k)# List containing the index of the original data

# Save the adjacency matrix
os.chdir(savedir) # Saving in the folder "processed"
W.to_csv('W.csv')

#Saving path_data, conn_names, orig_order, n_regions in a file located in the folder processed
path_data_objects = [path_data, conn_names, orig_order, n_regions] #Will depend on whether we will import directly this whole code ==> Need to check
path_data_pickle_out = open("path_data_pickle_out","wb")
pickle.dump(path_data_objects, path_data_pickle_out)

###########################
##PROCESS ROI COORDINATES##
###########################
os.chdir(basedir)  # Sets the wd for loading the data
coor = pd.read_csv("../Data83018/ROIcoords.csv")
coor.rename(columns = {'Unnamed: 0':'ROI'}, inplace = True)
idx = []
for i in range(0, len(ROInames)): # Reordering according to ROInames
    for k in range(0, len(coor['ROI'])):
        if ROInames[i] == coor['ROI'][k]:
            idx.append(k)
coor = coor.loc[idx,:] # From the new index created we reorganize the table by index.

# Saving the reorganized coordinates
os.chdir(savedir) #Saving in the folder processed
coor.to_csv('coor.csv')

###############################
### Process Snca Expression ###
###############################
os.chdir(basedir)  # Sets the wd back
synuclein = pd.read_csv("../Data83018/SncaExpression.csv", index_col = 0, header= None)
synuclein_ordered = []
for i in range(0, len(ROInames)): # Reordering according to ROInames
    for k in range(0, len(synuclein.index)):
        if ROInames[i] == synuclein.index[k]:
            synuclein_ordered.append(synuclein.index[k])
synuclein = synuclein.loc[synuclein_ordered,:] # Reordered Snca expression

# Saving the reorganized Snca expression
os.chdir(savedir) #Saving in the folder processed
synuclein.to_csv('Snca.csv')


import numpy as np
import matplotlib
import matplotlib.pyplot as plt
xf = np.loadtxt('logistic_x.txt')
y = np.loadtxt('logistic_y.txt')
colors = ['red', 'blue']
plt.scatter(xf[:, 0], xf[:, 1], c=y,cmap=matplotlib.colors.ListedColormap(colors))
plt.xlabel('x1')
plt.ylabel('x2')
m=y.shape[0]
n=xf.shape[1]
theta=np.zeros((n+1,1))
print (theta)
x=np.insert(xf,0,1,axis=1)
print (x)
print (y)
def g(t):
    return 1/(1+np.exp(-t))
def h(a):
    return np.dot(a,theta)
def costj(theta):
    l=0
    for i in range(m):
        u = y[i]*h(x[i, :])
        l-=np.log(u)/m
    return l
def gradient(l):
    grad=0
    for i in range(m):
        grad -= (1-g(y[i]*h(x[i, :])))*y[i]*x[i, :]/m
        return grad
def hessian(l):
    hess=np.zeros((n+1,n+1))
    for i in range(m):
        xi=np.dot(x[i,:],np.transpose(x[i,:]))
        hess -= g(y[i]*h(x[i, :])) * (1-g(y[i]*h(x[i, :])))*xi/m
        return hess
maxiters=10
for k in range(maxiters):
    theta=theta-np.matmul(np.linalg.pinv(hessian(l=costj(theta))),gradient(l=costj(theta)))
    print (theta)
p=np.zeros(m,1)
plot_y=np.zeros(m,1)
for i in range(m):
    p[i]= g(y[i]*h(x[i, :]))
    if p>0.5:
        plot_y[i]=1
    else:
        plot_y[i]=-1

plt.plot(xf,plot_y)
plt.show()


import subprocess
import time
import logging
import logging.handlers
import threading
import re
import os

import redsocks_template

LOGGER = logging.getLogger('fqrouter.%s' % __name__)

ROOT_DIR = os.path.dirname(__file__)
LOG_DIR = '/data/data/fq.router'
REDSOCKS_LOG_FILE = os.path.join(LOG_DIR, 'redsocks.log')
REDSOCKS_LOGGER = logging.getLogger('redsocks')
handler = logging.handlers.RotatingFileHandler(
    REDSOCKS_LOG_FILE, maxBytes=1024 * 1024, backupCount=1)
handler.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
REDSOCKS_LOGGER.handlers = [handler]

REFRESH_INTERVAL = 60 * 30
UPDATE_INTERVAL = 60 * 5

RE_IP_PORT = r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d+)'
RE_REDSOCKS_CLIENT = re.compile(RE_IP_PORT + '->')

# call back from full_proxy_service
list_proxies = None
handle_proxy_error = None
update_proxy = None
refresh_proxies = None

# internal
redsocks_process = None
updated_at = None
refreshed_at = None

def start_redsocks(proxies):
    for i in range(3):
        try:
            start_redsocks_once(proxies)
            return True
        except:
            LOGGER.exception('failed to start redsocks, retry')
            kill_redsocks()
    LOGGER.error('retry starting redsocks too many times, give up')
    return False

def start_redsocks_once(proxies):
    global redsocks_process
    if is_redsocks_live():
        LOGGER.error('another redsocks instace is running')
        return
    cfg_path = '/data/data/fq.router/redsocks.conf'
    with open(cfg_path, 'w') as f:
        f.write(redsocks_template.render(proxies))
    redsocks_process = subprocess.Popen(
        ['/data/data/fq.router/proxy-tools/redsocks', '-c', cfg_path],
        stderr=subprocess.STDOUT, stdout=subprocess.PIPE, bufsize=1, close_fds=True)
    time.sleep(2)
    if redsocks_process.poll() is None:
        LOGGER.info('redsocks seems started: %s' % redsocks_process.pid)
        t = threading.Thread(target=monitor_redsocks)
        t.daemon = True
        t.start()
    else:
        LOGGER.error('redsocks output:')
        LOGGER.error(redsocks_process.stdout.read())
        raise Exception('failed to start redsocks')

def monitor_redsocks():
    the_process = redsocks_process
    try:
        while is_redsocks_live():
            for line in iter(the_process.stdout.readline, b''):
                REDSOCKS_LOGGER.info(line.strip())
                if 'HTTP/' in line or 'No route to host' in line:
                    match = RE_REDSOCKS_CLIENT.search(line)
                    if match:
                        ip = match.group(1)
                        port = int(match.group(2))
                        for local_port, proxy in list_proxies():
                            if (ip, port) in proxy['clients']:
                                LOGGER.error(line.strip())
                                handle_proxy_error(local_port, proxy)
                update_proxies_according_to_schedule()
                refresh_proxies_according_to_schedule()
            time.sleep(1)
        LOGGER.error('redsocks died, clear proxies: %s' % the_process.poll())
        the_process.communicate()
        refresh_proxies()
    except:
        LOGGER.exception('failed to poll redsocks output')
        refresh_proxies()

def update_proxies_according_to_schedule():
    global updated_at
    if updated_at is None:
        updated_at = time.time()
    elif (time.time() - updated_at) > UPDATE_INTERVAL:
        updated_at = time.time()
        LOGGER.info('update proxies every %s seconds' % UPDATE_INTERVAL)
        for local_port, proxy in list_proxies():
            rank = int(proxy['pre_rank'] / 2) # factor in the previous performance
            LOGGER.info('update proxy %s rank: %s %s' %
                        (local_port, rank, str(proxy['connection_info'])))
            update_proxy(local_port, rank=rank, pre_rank=rank, clients=set())

def refresh_proxies_according_to_schedule():
    global refreshed_at
    if refreshed_at is None:
        refreshed_at = time.time()
    elif (time.time() - refreshed_at) > REFRESH_INTERVAL:
        refreshed_at = time.time()
        LOGGER.info('refresh proxies every %s seconds' % REFRESH_INTERVAL)
        time.sleep(15) # wait for current page
        kill_redsocks()

def kill_redsocks():
    try:
        if redsocks_process:
            LOGGER.info('found existing redsocks')
            redsocks_process.terminate()
            redsocks_process.communicate()
        LOGGER.info('redsocks killed')
    except:
        LOGGER.exception('failed to kill redsocks')
        time.sleep(2)

def is_redsocks_live():
    try:
        if not redsocks_process:
            return False
        return redsocks_process.poll() is None
    except:
        LOGGER.exception('failed to tell if redsocks is live')
        return False


#!/usr/bin/env python

import argparse
import datetime
import os
import shutil
import cv2
from signal import signal, SIGINT

import imutils
from imutils.video import FPS

from config import WildlifeConfig
from motion import MotionDetection
from notifier import TelegramNotifier
from video_writer import AsyncVideoWriter

exit_by_handler = False

def signal_handler(signal_received, f):
    global exit_by_handler
    print('[INFO] SIGINT or CTRL-C detected. Exiting gracefully')
    exit_by_handler = True

def create_video_filename(start_time, path):
    return "{}/{}-wildlife.avi".format(path, start_time.strftime("%Y%m%d-%H%M%S"))

start_recording_threshold_t1 = None
start_recording_threshold_t2 = None

def start_recording_threshold(activity_count):
    global start_recording_threshold_t1
    global start_recording_threshold_t2
    if activity_count % 5 == 0:
        start_recording_threshold_t2 = start_recording_threshold_t1
        start_recording_threshold_t1 = datetime.datetime.now()
        if start_recording_threshold_t2 is not None and (
                start_recording_threshold_t1 - start_recording_threshold_t2).seconds < 1:
            return True
    return False

last_activity = None

recording_status = "OFF"
recording_color = (255, 255, 225)
recording_info = ""
recording_filename = ""
start_recording_time = None
stop_recording_time = None
video_out = None
activity_count_total = 0
activity_count_during_recording = 0
last_recording_snapshot = None

signal(SIGINT, signal_handler)

ap = argparse.ArgumentParser()
ap.add_argument("-c", "--config", required=True,
                help="path to the JSON configuration file")
args = vars(ap.parse_args())

config = WildlifeConfig(args["config"])

# prepare video storage folder
if config.clean_store_on_startup:
    shutil.rmtree(config.store_path)
os.mkdir(config.store_path)

if config.system == "raspberrypi":
    from capture_picamera import CapturePiCameraAsync as Capture
else:
    from capture_opencv import CaptureOpencv as Capture

notifier = None
if config.telegram_notification:
    notifier = TelegramNotifier(config)

def writer_finished(file_name):
    global notifier
    if notifier is not None and last_recording_snapshot is not None:
        snapshot_filename = file_name.rsplit('.', 1)[0] + '.jpg'
        cv2.imwrite(snapshot_filename, last_recording_snapshot)
        notifier.send_message("New Wildlife Video: {}".format(os.path.basename(file_name)), snapshot_filename)

capture = Capture(config)

writer = AsyncVideoWriter(config, writer_finished)

motion = MotionDetection(config)

motion_detected = False
motion_rectangles = [(0, 0, config.resolution[0], config.resolution[1])]

capture.start()

fps = FPS().start()

frame_count = 0
while True:
    frame, frame_timestamp = capture.read()

    if frame is None:
        continue

    frame_count += 1

    timestamp = datetime.datetime.now()

    if config.motion_detection and frame_count % 3 == 0:
        motion_detected, motion_rectangles = motion.detect_motion(frame)

    motion_status = "activity"
    motion_status_color = (255, 255, 255)
    if motion_detected:
        last_activity = datetime.datetime.now()
        activity_count_total += 1

        if recording_status == "OFF" and start_recording_threshold(activity_count_total):
            recording_status = "ON"
            recording_color = (0, 0, 255)
            start_recording_time = frame_timestamp
            recording_filename = create_video_filename(start_recording_time, config.store_path)
            if config.store_video:
                activity_count_during_recording = 0
                writer.start(recording_filename)

        activity_count_during_recording += 1
        if activity_count_during_recording == config.store_activity_count_threshold + 1:
            last_recording_snapshot = frame.copy()

        motion_status = "activity"
        motion_status_color = (0, 255, 0)
        if motion_rectangles is not None:
            for r in motion_rectangles:
                cv2.rectangle(frame, (r[0], r[1]), (r[0] + r[2], r[1] + r[3]), motion_status_color, 1)

    if recording_status == "ON" and last_activity < timestamp and \
            (timestamp - last_activity).seconds >= config.min_recording_time_seconds:
        if config.store_video:
            writer.stop(activity_count_during_recording)
        recording_status = "OFF"
        recording_info = ""
        stop_recording_time = timestamp
        recording_color = (255, 255, 255)

    if config.store_video and recording_status == "ON":
        recording_info = " | " + recording_filename + " " + str((frame_timestamp - start_recording_time).seconds) + \
                         " activity: " + str(activity_count_during_recording)

    timestamp_str = frame_timestamp.strftime("%A %d %B %Y %I:%M:%S%p")
    cv2.putText(frame, motion_status, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, motion_status_color, 2)
    cv2.putText(frame, recording_status, (frame.shape[1] - 50, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.35, recording_color, 2)
    cv2.putText(frame, timestamp_str + recording_info, (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35
                , (255, 255, 255), 1)

    # store frame
    if config.store_video:
        writer.write(frame)

    if config.show_video:
        cv2.imshow('captured frame', frame)
        if cv2.waitKey(1) == ord('q'):
            break

    if exit_by_handler:
        break

    fps.update()

# shutdown
fps.stop()
print("[INFO] elapsed time: {:.2f} s".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
writer.stop(0)
capture.stop()
if config.show_video:
    cv2.destroyAllWindows()


from flask import Flask, jsonify, render_template, request
from flask_sslify import SSLify
import json
import os

app = Flask(__name__)
sslify = SSLify(app)

VERSION = '1.1';
VERSION_KEY = 'version_key';

CURRENT_DIRECTORY = os.path.dirname(__file__)
PATH_JSON_COURSES = os.path.join(CURRENT_DIRECTORY, 'data', 'courses.json')

with open(PATH_JSON_COURSES, 'r') as f:
    course_dictionary = json.load(f)

@app.route('/course/<string:course_code>', methods=['GET'])
def get_task(course_code):
    if course_code in course_dictionary:
        return jsonify({
            'code': course_code,
            'name': course_dictionary[course_code]
        })
    else:
        return jsonify({
            'code':course_code,
            'name': ''
        })

@app.route('/courses', methods=['GET'])
def get_courses():
    all_args = request.args.to_dict()

    response_dict = {}

    if VERSION_KEY in all_args:
        response_dict[VERSION_KEY] = VERSION
        # Delete the query key so we don't iterate over it below
        del all_args[VERSION_KEY]

    for _, course_code in all_args.items():
        if course_code in course_dictionary:
            response_dict[course_code] = course_dictionary[course_code]

    return jsonify(response_dict)

@app.route('/')
def index():
    return render_template('index.html')

# CORS
@app.after_request
def apply_caching(response):
    response.headers['Access-Control-Allow-Origin'] = 'https://ssc.adm.ubc.ca'
    response.headers['Access-Control-Allow-Methods'] = 'GET'
    response.headers['Access-Control-Allow-Headers'] = 'Content-Type'
    return response

if __name__ == '__main__':
    app.run()


# -*- coding: utf-8 -*-
"""
Created on Tue May 18 16:50:38 2021

@author: tudor
"""

#Import needed packages
import torch
import torch.nn as nn
from torchvision.datasets import CIFAR10
from torchvision.transforms import transforms
from torch.utils.data import DataLoader
from torch.optim import Adam
from torch.autograd import Variable
import numpy as np
from MyDataset import *

class Unit(nn.Module):
    def __init__(self, in_channels, out_channels):
        super(Unit, self).__init__()

        self.conv = nn.Conv2d(in_channels=in_channels,kernel_size=3,out_channels=out_channels,stride=1,padding=1)
        self.bn = nn.BatchNorm2d(num_features=out_channels)
        self.relu = nn.ReLU()

    def forward(self, input):
        output = self.conv(input)
        output = self.bn(output)
        output = self.relu(output)

        return output

class SimpleNet(nn.Module):
    def __init__(self,num_classes=2):
        super(SimpleNet,self).__init__()

        #Create 14 layers of the unit with max pooling in between
        self.unit1 = Unit(in_channels=3,out_channels=32)
        self.unit2 = Unit(in_channels=32, out_channels=32)
        self.unit3 = Unit(in_channels=32, out_channels=32)

        self.pool1 = nn.MaxPool2d(kernel_size=2)

        self.unit4 = Unit(in_channels=32, out_channels=64)
        self.unit5 = Unit(in_channels=64, out_channels=64)
        self.unit6 = Unit(in_channels=64, out_channels=64)
        self.unit7 = Unit(in_channels=64, out_channels=64)

        self.pool2 = nn.MaxPool2d(kernel_size=2)

        self.unit8 = Unit(in_channels=64, out_channels=128)
        self.unit9 = Unit(in_channels=128, out_channels=128)
        self.unit10 = Unit(in_channels=128, out_channels=128)
        self.unit11 = Unit(in_channels=128, out_channels=128)

        self.pool3 = nn.MaxPool2d(kernel_size=2)

        self.unit12 = Unit(in_channels=128, out_channels=128)
        self.unit13 = Unit(in_channels=128, out_channels=128)
        self.unit14 = Unit(in_channels=128, out_channels=128)

        self.avgpool = nn.AvgPool2d(kernel_size=4)

        #Add all the units into the Sequential layer in exact order
        self.net = nn.Sequential(self.unit1, self.unit2, self.unit3, self.pool1, self.unit4, self.unit5, self.unit6
                                 ,self.unit7, self.pool2, self.unit8, self.unit9, self.unit10, self.unit11, self.pool3,
                                 self.unit12, self.unit13, self.unit14, self.avgpool)

        self.fc = nn.Linear(in_features=128*7*7,out_features=num_classes)

    def forward(self, input):
        output = self.net(input)
        output = output.view(-1, 128*7*7)
        output = self.fc(output)
        return output

class Train:
    def __init__(self):
        self.train_loader = self.TrainLoader()
        self.test_loader = self.TestLoader()
        self.model = SimpleNet(num_classes=2)

        self.cuda_avail = torch.cuda.is_available()

        if self.cuda_avail:
            self.model.cuda()

        self.optimizer = optimizer = Adam(self.model.parameters(), lr = 0.0001, weight_decay=0.0001)
        self.loss_fn = nn.CrossEntropyLoss()

    def adjust_learning_rate(self, epoch):
        lr = 0.001

        if epoch > 180:
            lr = lr / 1000000
        elif epoch > 150:
            lr = lr / 100000
        elif epoch > 120:
            lr = lr / 10000
        elif epoch > 90:
            lr = lr / 1000
        elif epoch > 60:
            lr = lr / 100
        elif epoch > 30:
            lr = lr / 10

        for param_group in self.optimizer.param_groups:
            param_group["lr"] = lr

    def save_models(self, epoch):
        torch.save(self.model.state_dict(), "Model_{}.model".format(epoch))
        print("Checkpoint saved")

    def test(self):
        self.model.eval()
        test_acc = 0.0
        for i, (images, labels) in enumerate(self.test_loader):

            if self.cuda_avail:
                images = Variable(images.cuda())
                labels = Variable(labels.cuda())

            # Predict classes using images from the test set
            outputs = self.model(images)
            _, prediction = torch.max(outputs.data, 1)
            # prediction = prediction.cpu().numpy()

            test_acc += torch.sum(torch.eq(prediction, labels.data))

        # Compute the average acc and loss over all 75 test images
        test_acc = test_acc / 75

        return test_acc

    def TestLoader(self):
        dataset = getTestDataset()

        test_loader = DataLoader(dataset, batch_size=5, shuffle=False, num_workers=4)

        return test_loader

    def TrainLoader(self):
        dataset = getDataset()

        train_loader = DataLoader(dataset, batch_size=5, shuffle=False, num_workers=4)

        return train_loader

    def train(self, num_epochs):
        best_acc = 0.0

        for epoch in range(num_epochs):
            self.model.train()
            train_acc = 0.0
            train_loss = 0.0
            for i, (images, labels) in enumerate(self.train_loader):
                # Move images and labels to gpu if available
                if self.cuda_avail:
                    images = Variable(images.cuda())
                    labels = Variable(labels.cuda())

                # Clear all accumulated gradients
                self.optimizer.zero_grad()
                # Predict classes using images from the test set
                outputs = self.model(images)
                # Compute the loss based on the predictions and actual labels
                loss = self.loss_fn(outputs, labels)
                # Backpropagate the loss
                loss.backward()

                # Adjust parameters according to the computed gradients
                self.optimizer.step()

                train_loss += loss.cpu().data.item() * images.size(0)
                _, prediction = torch.max(outputs.data, 1)

                train_acc += torch.sum(prediction == labels.data)

            # Call the learning rate adjustment function
            self.adjust_learning_rate(epoch)

            # Compute the average acc and loss over all 50000 training images
            train_acc = train_acc / 150
            train_loss = train_loss / 150

            # Evaluate on the test set
            test_acc = self.test()

            # Save the model if the test acc is greater than our current best
            if test_acc > best_acc:
                self.save_models(epoch)
                best_acc = test_acc

            # Print the metrics
            print(
                "Epoch {}, Train Accuracy: {} , TrainLoss: {} , Test Accuracy: {}".format(epoch+1, train_acc, train_loss,
                                                                                test_acc))

    def run(self):
        torch.cuda.empty_cache()
        self.train(20)

if __name__ == "__main__":
    train = Train()
    train.run()


# -*- coding: utf-8 -*-
"""
Helper file to get DarkChem latent space vectors used in paper

Note: DarkChem must already be downloaded.
For more info, see https://github.com/pnnl/darkchem

Once downloaded, set DARKCHEM_PATH variable

"""

## Imports ##
import pandas as pd
import os
import numpy as np

## Globals ##
# save path to trained protonated weights, relative to the path of the code
DARKCHEM_PATH = 'darkchem-weights/protonated'

## Functions ##

def get_latent_space_vectors(df, smiles_col_name='SMILES'):
    '''
    Adds latent space vectors to a dataframe of SMILES using DarkChem.

    Parameters
    ----------
    df : pandas dataframe
        Pandas dataframe.

    Returns
    -------
    final : pandas dataframe
        Dataframe with latent vectors.
    '''

    # save smiles to a file for darkchem use
    df[smiles_col_name].to_csv('temp_smiles.txt', index=False)

    # call darkchem from the command line
    input_ = ['darkchem predict latent temp_smiles.txt', DARKCHEM_PATH]
    input_ = ' '.join(input_)
    os.popen(input_).read()
    os.remove('temp_smiles.txt')

    # read in the file of latent space vectors
    latent_vec = np.load('temp_smiles_latent.npy')
    os.remove('temp_smiles_latent.npy')
    latent_df = pd.DataFrame(latent_vec)

    # create column names for vector dataframe
    col_names = []
    for i in range(128):
        col_names.append(('LS' + str(i + 1)))
    latent_df.columns = col_names

    # replace empty values with 'None'
    latent_df = latent_df.fillna('None')
    final = df.join(latent_df)

    # return dataframe of SMILES with latent
    # space vectors
    return final

## Main ##

if __name__ == '__main__':

    # Use library from paper as example
    fname = r'../data/SupportingData.xlsx'
    data = pd.read_excel(fname, sheet_name='SuspectLibrary',
                         usecols=['SMILES'])
    data = get_latent_space_vectors(data)


#
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# This file is licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# This file 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.
#

# snippet-sourcedescription:[cloudcamera.py demonstrates how to mock an AWS IoT Things Graph camera device in the cloud.]
# snippet-service:[iotthingsgraph]
# snippet-keyword:[Python]
# snippet-sourcesyntax:[python]
# snippet-sourcesyntax:[python]
# snippet-keyword:[AWS IoT Things Graph]
# snippet-keyword:[Code Sample]
# snippet-sourcetype:[full-example]
# snippet-sourcedate:[2019-07-25]
# snippet-sourceauthor:[AWS]
# snippet-start:[iotthingsgraph.python.cloudcamera.complete]

from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
import logging
import time
import argparse
import json

AllowedActions = ['both', 'publish', 'subscribe']

# Custom MQTT message callback
def customCallback(client, userdata, message):
    print("Received a new message: ")
    print(message.payload)
    print("from topic: ")
    print(message.topic)
    print("--------------\n\n")

# Read in command-line parameters
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--endpoint", action="store", required=True, dest="host", help="Your AWS IoT custom endpoint")
parser.add_argument("-r", "--rootCA", action="store", required=True, dest="rootCAPath", help="Root CA file path")
parser.add_argument("-c", "--cert", action="store", dest="certificatePath", help="Certificate file path")
parser.add_argument("-k", "--key", action="store", dest="privateKeyPath", help="Private key file path")
parser.add_argument("-p", "--port", action="store", dest="port", type=int, help="Port number override")
parser.add_argument("-w", "--websocket", action="store_true", dest="useWebsocket", default=False,
                    help="Use MQTT over WebSocket")
parser.add_argument("-id", "--clientId", action="store", dest="clientId", default="basicPubSub",
                    help="Targeted client id")
parser.add_argument("-t", "--topic", action="store", dest="topic", default="Camera1/capture/finished", help="Targeted topic")
parser.add_argument("-m", "--mode", action="store", dest="mode", default="both",
                    help="Operation modes: %s"%str(AllowedActions))
parser.add_argument("-M", "--message", action="store", dest="message", default=True,
                    help="Message to publish")
parser.add_argument("-n", "--thingName", action="store", dest="thingName", default="Bot", help="Targeted thing name")

args = parser.parse_args()
host = args.host
rootCAPath = args.rootCAPath
certificatePath = args.certificatePath
privateKeyPath = args.privateKeyPath
port = args.port
useWebsocket = args.useWebsocket
clientId = args.clientId
topic = args.topic
thingName = args.thingName

images = ["https://images-na.ssl-images-amazon.com/images/I/41iz5Tw82IL._AC_US218_.jpg",
"https://images-na.ssl-images-amazon.com/images/I/51rMLSWgwRL._AC_US218_.jpg",
"https://images-na.ssl-images-amazon.com/images/I/31s6UyPtjOL._AC_US218_.jpg",
"https://images-na.ssl-images-amazon.com/images/I/41+K4pC74XL._AC_US218_.jpg"]

if args.mode not in AllowedActions:
    parser.error("Unknown --mode option %s. Must be one of %s" % (args.mode, str(AllowedActions)))
    exit(2)

if args.useWebsocket and args.certificatePath and args.privateKeyPath:
    parser.error("X.509 cert authentication and WebSocket are mutual exclusive. Please pick one.")
    exit(2)

if not args.useWebsocket and (not args.certificatePath or not args.privateKeyPath):
    parser.error("Missing credentials for authentication.")
    exit(2)

# Port defaults
if args.useWebsocket and not args.port:  # When no port override for WebSocket, default to 443
    port = 443
if not args.useWebsocket and not args.port:  # When no port override for non-WebSocket, default to 8883
    port = 8883

# Configure logging
logger = logging.getLogger("AWSIoTPythonSDK.core")
logger.setLevel(logging.DEBUG)
streamHandler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)

# Init AWSIoTMQTTClient
myAWSIoTMQTTClient = None
if useWebsocket:
    myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId, useWebsocket=True)
    myAWSIoTMQTTClient.configureEndpoint(host, port)
    myAWSIoTMQTTClient.configureCredentials(rootCAPath)
else:
    myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId)
    myAWSIoTMQTTClient.configureEndpoint(host, port)
    myAWSIoTMQTTClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath)

# AWSIoTMQTTClient connection configuration
myAWSIoTMQTTClient.configureAutoReconnectBackoffTime(1, 32, 20)
myAWSIoTMQTTClient.configureOfflinePublishQueueing(-1)  # Infinite offline Publish queueing
myAWSIoTMQTTClient.configureDrainingFrequency(2)  # Draining: 2 Hz
myAWSIoTMQTTClient.configureConnectDisconnectTimeout(10)  # 10 sec
myAWSIoTMQTTClient.configureMQTTOperationTimeout(5)  # 5 sec

myCount = 0

# General message notification callback
def customOnMessage(message):
    print('Received message on topic %s: %s\n' % (message.topic, message.payload))
    message = {}
    global myCount
    myCount = myCount + 1
    message['lastClickedImage'] = images[myCount%4]
    messageJson = json.dumps(message)
    myAWSIoTMQTTClient.publish(thingName + "/capture/finished", messageJson, 0)
    print('Published topic %s: %s\n' % (thingName + "/capture/finished", messageJson))

myAWSIoTMQTTClient.onMessage = customOnMessage

# Connect and subscribe to AWS IoT
myAWSIoTMQTTClient.connect()
if args.mode == 'both' or args.mode == 'subscribe':
    myAWSIoTMQTTClient.subscribe(thingName + "/capture", 0, customCallback)
time.sleep(2)

# Publish to the same topic in a loop forever
loopCount = 0
while True:
    time.sleep(1)

# snippet-end:[iotthingsgraph.python.cloudcamera.complete]


#!/usr/bin/env python3
import random

start_string = "|START|"

def wordgenerator(stream):
    """Yields one word at a time from a stream. Strips newlines and the utf8 byte order mark"""
    yield start_string
    for line in stream.readlines():
        for word in line.split(" "):
            yield word.translate({ord("\n"): None, ord("\ufeff"): None})

def train(stream, word_hash, depth):
    """Creates a nested "word hash" of words which follow each other, and their count at leaf nodes."""
    words = []
    for word in wordgenerator(stream):
        words.append(word)
        if len(words) < depth:
            continue
        else:
            update_hash(word_hash, words)
            words.pop(0)

def update_hash(word_hash, words):
    """Takes a list of words and increments or creates their leaf node in a word hash"""
    sub_hash = word_hash
    for word in words[:-1]:
        if word not in sub_hash.keys():
            sub_hash[word] = dict()
        sub_hash = sub_hash[word]
    if words[-1] in sub_hash.keys():
        sub_hash[words[-1]] += 1
    else:
        sub_hash[words[-1]] = 1

def get_weight(item):
    """Given a node in a word hash, returns its weight (or the sum of weights of its children)"""
    while True:
        if isinstance(item, int):
            return item
        else:
            return sum([get_weight(val) for key, val in item.items()])

def get_weighted_words(word_hash, words):
    """Gets a weighted list of word choices directly under the given words in the hash"""
    sub_hash = word_hash
    for word in words:
        if word in sub_hash.keys():
            sub_hash = sub_hash[word]
        else:
            return False
    return {word: get_weight(val) for word, val in sub_hash.items()}

def choose_weighted(weighted_words):
    """Chooses an entry at random from a weighted list"""
    total = sum([weight for word, weight in weighted_words.items()])
    i = random.randint(0, total - 1)
    for word, weight in weighted_words.items():
        if i < weight:
            return word
        else:
            i -= weight

def generate_word(word_hash, words):
    """Generates a single word given the current list of words from a word hash"""
    while len(words) > 0:
        weighted_words = get_weighted_words(word_hash, words)
        if weighted_words:
            break
        else:
            words = words[:-1]
    return choose_weighted(weighted_words)

def generate(word_hash, depth, length):
    """Generates a number of words from a word hash using a given depth"""
    words = [start_string]
    for i in range(length):
        words.append(generate_word(word_hash, words[-depth:]))
    words.pop(0)
    return words


samples_raw = [
    {
        "words": ["Здравствуйте", "Kirill", "Bulgakov", "Мы", "обратили", "внимание", "что", "Вы", "еще", "@не",
                  "@активировали",
                  "Вашу", "Предоплаченную", "@Дебетовую", "@Карту", "@Payoneer", "@MasterCard®", "и", "хотели", "бы",
                  "убедиться", "в",
                  "том", "что", "Вы", "@получили", "@ее", "Если", "Вы", "еще", "@не", "@получили", "Вашу", "@карту",
                  "пожалуйста",
                  "@посетите", "@Центр", "@Поддержки", "для", "получения", "дополнительной", "информации", "Если", "Вы",
                  "уже",
                  "@получили", "Вашу", "@карту", "Вы", "@можете", "@войти", "@в", "Ваш", "@аккаунт", "@Payoneer",
                  "@активировать", "@ее", "@и",
                  "@начать", "@получать", "@выплаты", "Для", "получения", "дополнительной", "информации",
                  "относительно",
                  "доставки",
                  "карт", "пожалуйста", "@прочитайте", "@эту", "@статью", "@в", "@блоге", "@Payoneer", "Спасибо",
                  "Коллектив",
                  "Payoneer"]
    },
    {
        "words": ["@«М", "@Видео»", "@по", "@ошибке", "@показал", "@фото", "@и", "@характеристики", "@нового",
                  "@«Яндекс",
                  "@Телефона»",
                  "16", "29", "02", "Декабря", "2018", "Москва", "Интернет", "магазин",
                  "@«М", "@Видео»", "@по", "@ошибке", "@выложил", "в", "сеть", "@фото", "@и", "@характеристики",
                  "@«Яндекс",
                  "@Телефона»", "@релиз", "@которого", "@был", "@назначен", "@только",
                  "@на", "@5", "@декабря", "", "", "Сообщается", "", "что", "на", "сайте",
                  "интернет", "магазина", "была", "по", "ошибке", "@опубликована", "@информация", "@о", "@том",
                  "@как", "именно", "@будет", "@выглядеть", "@новинка", "", "причем", "ее", "официальная",
                  "презентация",
                  "должна", "состояться", "только", "декабря", "@Сведения", "спустя", "некоторое", "время", "@были",
                  "@в",
                  "@срочном", "@порядке", "@удалены", "однако", "некоторым", "специалистам", "удалось", "успеть",
                  "сохранить", "сведения", "и", "медиафайлы", "Согласно", "обнародованным", "данным", "@корпус",
                  "нового", "@смартфона", "@будет", "@изготовлен", "@из", "@пластика", "@и", "@металла", "@а",
                  "@«сердцем»", "«Яндекс", "Телефона»", "@станет", "@Snapdragon", "@630", "@Разрешение", "@основной",
                  "@камеры", "@составит", "@16", "@Мп", "@а", "@фронтальной", "@5", "@Мп", "@Гаджет", "@будет",
                  "@оснащен",
                  "@чипом", "@NFC", "@и", "@зарядом", "@батареи", "@на", "@3060", "@мАЧ", "Также", "стала", "известна",
                  "@стоимость", "@устройства", "которая", "@составит", "@17", "@990", "@рублей", "Отметим", "что",
                  "@ранее",
                  "@в", "@сети", "@уже", "@появлялись", "@характеристики", "«@Яндекс", "@Телефона»", "@которые",
                  "@полностью",
                  "@совпадают", "@с", "@указанными", "@на", "@сайте", "@«М", "@Видео»",
                  ]
    },
    {
        "words": ["Добрый", "день", "@Помогают", "@ли", "@вам", "@онлайн", "@курсы", "@от",
                  "@Яндекса", "@и", "@МФТИ", "@добиваться", "@своих", "@целей", "@Расскажите",
                  "@нам", "@об", "э@том", "@заполнив", "@опрос", "@по", "@ссылке", "@Ваш", "@ответ",
                  "@поможет", "@улучшить", "@качество", "@программ", "@и", "@материалов", "которые",
                  "мы", "создаем", "С", "уважением", "Команда", "создателей", "курса"]
    }
]


# coding: utf-8

"""
    Memsource REST API

    Welcome to Memsource's API documentation. To view our legacy APIs please [visit our documentation](https://wiki.memsource.com/wiki/Memsource_API) and for more information about our new APIs, [visit our blog](https://www.memsource.com/blog/2017/10/24/introducing-rest-apis-qa-with-the-memsource-api-team/).    If you have any questions, please contact [Memsource Support](<mailto:support@memsource.com>).  # noqa: E501

    OpenAPI spec version: Latest

    Generated by: https://github.com/swagger-api/swagger-codegen.git
"""

import pprint
import re  # noqa: F401

import six

from memsource_cli.models.counts_dto import CountsDto  # noqa: F401,E501

class MatchCountsDto(object):
    """NOTE: This class is auto generated by the swagger code generator program.

    Do not edit the class manually.
    """

    """
    Attributes:
      swagger_types (dict): The key is attribute name
                            and the value is attribute type.
      attribute_map (dict): The key is attribute name
                            and the value is json key in definition.
    """
    swagger_types = {
        'match100': 'CountsDto',
        'match95': 'CountsDto',
        'match85': 'CountsDto',
        'match75': 'CountsDto',
        'match50': 'CountsDto',
        'match0': 'CountsDto'
    }

    attribute_map = {
        'match100': 'match100',
        'match95': 'match95',
        'match85': 'match85',
        'match75': 'match75',
        'match50': 'match50',
        'match0': 'match0'
    }

    def __init__(self, match100=None, match95=None, match85=None, match75=None, match50=None, match0=None):  # noqa: E501
        """MatchCountsDto - a model defined in Swagger"""  # noqa: E501

        self._match100 = None
        self._match95 = None
        self._match85 = None
        self._match75 = None
        self._match50 = None
        self._match0 = None
        self.discriminator = None

        if match100 is not None:
            self.match100 = match100
        if match95 is not None:
            self.match95 = match95
        if match85 is not None:
            self.match85 = match85
        if match75 is not None:
            self.match75 = match75
        if match50 is not None:
            self.match50 = match50
        if match0 is not None:
            self.match0 = match0

    @property
    def match100(self):
        """Gets the match100 of this MatchCountsDto.  # noqa: E501

        :return: The match100 of this MatchCountsDto.  # noqa: E501
        :rtype: CountsDto
        """
        return self._match100

    @match100.setter
    def match100(self, match100):
        """Sets the match100 of this MatchCountsDto.

        :param match100: The match100 of this MatchCountsDto.  # noqa: E501
        :type: CountsDto
        """

        self._match100 = match100

    @property
    def match95(self):
        """Gets the match95 of this MatchCountsDto.  # noqa: E501

        :return: The match95 of this MatchCountsDto.  # noqa: E501
        :rtype: CountsDto
        """
        return self._match95

    @match95.setter
    def match95(self, match95):
        """Sets the match95 of this MatchCountsDto.

        :param match95: The match95 of this MatchCountsDto.  # noqa: E501
        :type: CountsDto
        """

        self._match95 = match95

    @property
    def match85(self):
        """Gets the match85 of this MatchCountsDto.  # noqa: E501

        :return: The match85 of this MatchCountsDto.  # noqa: E501
        :rtype: CountsDto
        """
        return self._match85

    @match85.setter
    def match85(self, match85):
        """Sets the match85 of this MatchCountsDto.

        :param match85: The match85 of this MatchCountsDto.  # noqa: E501
        :type: CountsDto
        """

        self._match85 = match85

    @property
    def match75(self):
        """Gets the match75 of this MatchCountsDto.  # noqa: E501

        :return: The match75 of this MatchCountsDto.  # noqa: E501
        :rtype: CountsDto
        """
        return self._match75

    @match75.setter
    def match75(self, match75):
        """Sets the match75 of this MatchCountsDto.

        :param match75: The match75 of this MatchCountsDto.  # noqa: E501
        :type: CountsDto
        """

        self._match75 = match75

    @property
    def match50(self):
        """Gets the match50 of this MatchCountsDto.  # noqa: E501

        :return: The match50 of this MatchCountsDto.  # noqa: E501
        :rtype: CountsDto
        """
        return self._match50

    @match50.setter
    def match50(self, match50):
        """Sets the match50 of this MatchCountsDto.

        :param match50: The match50 of this MatchCountsDto.  # noqa: E501
        :type: CountsDto
        """

        self._match50 = match50

    @property
    def match0(self):
        """Gets the match0 of this MatchCountsDto.  # noqa: E501

        :return: The match0 of this MatchCountsDto.  # noqa: E501
        :rtype: CountsDto
        """
        return self._match0

    @match0.setter
    def match0(self, match0):
        """Sets the match0 of this MatchCountsDto.

        :param match0: The match0 of this MatchCountsDto.  # noqa: E501
        :type: CountsDto
        """

        self._match0 = match0

    def to_dict(self):
        """Returns the model properties as a dict"""
        result = {}

        for attr, _ in six.iteritems(self.swagger_types):
            value = getattr(self, attr)
            if isinstance(value, list):
                result[attr] = list(map(
                    lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
                    value
                ))
            elif hasattr(value, "to_dict"):
                result[attr] = value.to_dict()
            elif isinstance(value, dict):
                result[attr] = dict(map(
                    lambda item: (item[0], item[1].to_dict())
                    if hasattr(item[1], "to_dict") else item,
                    value.items()
                ))
            else:
                result[attr] = value
        if issubclass(MatchCountsDto, dict):
            for key, value in self.items():
                result[key] = value

        return result

    def to_str(self):
        """Returns the string representation of the model"""
        return pprint.pformat(self.to_dict())

    def __repr__(self):
        """For `print` and `pprint`"""
        return self.to_str()

    def __eq__(self, other):
        """Returns true if both objects are equal"""
        if not isinstance(other, MatchCountsDto):
            return False

        return self.__dict__ == other.__dict__

    def __ne__(self, other):
        """Returns true if both objects are not equal"""
        return not self == other


import sys
import datetime
import time
import random
import threading
from queue import Queue

from lib.utils import Logger, Dict
from lib.utils.mysql import DataBase
from . import Parser, Searcher, Requestor

thread_lock = threading.Lock()

def lock(thread_func):
    def wrapper(obj, *args, **kwargs):
        thread_lock.acquire()
        try:
            return thread_func(obj, *args, **kwargs)
        except Exception as e:
            raise e
        finally:
            thread_lock.release()
    return wrapper

class BaseUrlFetcher(threading.Thread):
    def __init__(self, request: Requestor):
        threading.Thread.__init__(self, daemon=True)
        self.request = request

class BaseCrawler(threading.Thread):
    def __init__(self, request: Requestor, parser: Parser, url_fetcher: BaseUrlFetcher):
        threading.Thread.__init__(self, daemon=True)
        self.request = request
        self.parser = parser
        self.url_fetcher = url_fetcher

class TargetedCrawler(BaseCrawler):
    def __init__(self, request: Requestor, parser: Parser, url_fetcher: BaseUrlFetcher, database: DataBase, table: str, logger: Logger, target: dict, queue: Queue, current_info: Dict, proxies=None):
        super().__init__(request, parser, url_fetcher)

        self.database = database
        self.table = table
        self.logger = logger

        self.target = target    # {'alias': '', 'host': ''}
        self.queue = queue
        self.current_info = current_info

        self.proxies = proxies
        self.crawled_urls = set([r['url'] for r in self.database.select(self.table, 'url', site=self.target['alias'])])

    def run(self):
        while self.url_fetcher.is_alive() or self.queue.qsize():
            if not self.queue.empty():
                try:
                    self.current_info.query = self.queue.get()
                    query = self.current_info.query
                    topic = query.get('topic', '')
                    soup, url = self.request(query['url'], no_headers=True)
                    if url['url'] not in self.crawled_urls:
                        data = self.parser(soup, url['netloc'], url['tld'])
                        next_page_url = data.pop('next', None)
                        while next_page_url:
                            try:
                                _soup, _url = self.request(next_page_url)
                                _data = self.parser(_soup, _url['netloc'], _url['tld'])
                                data['text'] += _data['text']
                                next_page_url = _data.pop('next', None)
                            except:
                                break
                        # data = {'publish_time': datetime.datetime.now(), 'source': random_str(4), 'title': random_str(8), 'text': random_str(16)}
                        self.save(**data, url=url['url'], site=self.target['alias'], entry_time=datetime.datetime.now(), topic=topic)
                        self.logger.info(f'parse and save (url:{url["url"]}, topic:{topic}, title:{data["title"]}) successfully')
                        self.crawled_urls.add(url['url'])
                    else:
                        self.logger.info(f'{url["url"]} is in database already')
                    self.current_info.pop('query')
                except:
                    self.logger.error(exc_info=sys.exc_info(), message=f'parse and save (url:{url["url"]}, topic:{topic})')
            else:
                time.sleep(2)

    @lock
    def save(self, **data):
        topic = data.pop('topic')
        last_row_id = self.database.insert(self.table, **data)
        self.database.insert(f'{self.table}_topic', news_id=last_row_id, topic_name=topic)


#!/usr/bin/python
# -*- coding: UTF-8 -*-
import zlib
import urllib.request
import urllib
from urllib import request
import http.cookiejar

class cookie(request.BaseHandler):
    def http_request(self, req):
        simple_cookie = 'ASP.NET_SessionId=5up1kajxifag0j45nimvjvru'   #这里填自定义cookie值
        if not req.has_header('Cookie'):
            req.add_unredirected_header('Cookie', simple_cookie)  #如果header中cookie为空，则创建cookie对象并把cookie值放进去
        else:
            cookie = req.get_header('Cookie')
            req.add_unredirected_header('Cookie', simple_cookie + '; ' + cookie)  #如果header中已经存在cookie，则把自定义cookie插到已有cookie的末尾
        return req

header={
        'Accept': 'text/html,application/xhtml xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,en-US;q=0.8',
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
    }

postdata =urllib.parse.urlencode({
#"JSESSIONID":"81530CFA37A4AB882173D09544BBEE55",
#"emall-timecard":"{\"userId\":\"00000185\",\"userName\":\"申建利\",\"mobile\":\"18211166905\",\"email\":\"shenjl@i2finance.net\",\"roleId\":\"1\",\"imageUrl\":\"http://210.14.78.246:82/images/3/00000202/1483092273778.jpg\"}"
}).encode('utf-8')
#JSESSIONID=81530CFA37A4AB882173D09544BBEE55; Path=/i2work-timecard
#emall-timecard=}; Path=/

header = {
"Accept":"text/html,application/xhtml xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"zh-CN,en-US;q=0.8",
"Connection":"keep-alive",
"Host":"bird2.i2soft.net",
"X-Requested-Width":"com.i2worker",
"User-Agent":"Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1/qianxs",
"Cookie":"JSESSIONID=81530CFA37A4AB882173D09544BBEE55"
}

def getTask(url):

    req = urllib.request.Request(url, postdata, header)
    response = urllib.request.urlopen(req)
    if response.info().get('Content-Encoding', "") == 'gzip':  # e.g www.sina.com.cn
        decompressed_data = zlib.decompress(response.read(), 16 + zlib.MAX_WBITS)
        html = decompressed_data.decode('utf8').splitlines(True)[:10]
    else:
        html = response.read()

    # 自动记住cookie
    cj = http.cookiejar.CookieJar()
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
    r = opener.open(req)
    result = ""
    if r.info().get('Content-Encoding', "") == 'gzip':  # e.g www.sina.com.cn
        decompressed_data = zlib.decompress(r.read(), 16 + zlib.MAX_WBITS)
        result = decompressed_data.decode('utf8').splitlines(True)[:10]
    else:
        result = response.read()
    print(result)
    print("cookie:",cj)
    return html

def getCookieRequest(url):
    mcj = http.cookiejar.MozillaCookieJar()
    cookiehand = urllib.request.HTTPCookieProcessor(mcj)
    opener = urllib.request.build_opener(cookiehand, cookie())
    print(mcj)
    opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36')]
    u = opener.open(url)
    result = ""
    if u.info().get('Content-Encoding', "") == 'gzip':  # e.g www.sina.com.cn
        decompressed_data = zlib.decompress(u.read(), 16 + zlib.MAX_WBITS)
        result = decompressed_data.decode('utf8').splitlines(True)[:10]
    else:
        result = u.read()
    print(result)
    print(mcj)

if __name__ == '__main__':
    ss = "{\"userId\":\"00000185\",\"userName\":\"申建利\",\"mobile\":\"18211166905\",\"email\":\"shenjl@i2finance.net\",\"roleId\":\"1\",\"imageUrl\":\"http://210.14.78.246:82/images/3/00000202/1483092273778.jpg\"}"
    print(ss)
    taskUrl = "http://bird2.i2soft.net/i2work-timecard/router.do?method=tc.taskModule.getTaskList"
    #html = getTask(taskUrl)
    #print(html)
    getCookieRequest(taskUrl)


__author__ = 'stas.semeniuta'

def get_subset(filenames, subset_size, save=False):
    if subset_size > 0 & subset_size < len(filenames):
        indexes = []
        subset = []
        size = len(filenames)
        for i in range(0, subset_size):
            from random import randint
            while True:
                index = randint(0, size - 1)
                if not index in indexes:
                    subset.append(filenames[index])
                    indexes.append(index)
                    break
    if save:
        with open("mp3subset.txt", "w") as f:
            for filename in subset:
                f.write("%s\n" % subset)

    return subset


import matplotlib.pyplot as plt
import pandas as pd
import math
import numpy as np

from sklearn import linear_model
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error

dataset = pd.read_csv('data_all.csv')
# print(dataset)

def MultiPlot(dataOnX, XName):

	def plotGraph( dataOnY, dataOnX, YName, XName):
		plt.figure(1)
		plt.scatter(dataOnY, dataOnX, color='blue')
		plt.title('Mental health as a function of song tempo')
		plt.xlabel(YName)
		plt.ylabel(XName)
		plt.show()
	plotGraph(dataset.tempo, dataOnX, 'tempo', XName);
	plotGraph(dataset.liveness, dataOnX, 'liveness', XName);
	plotGraph(dataset.valence, dataOnX, 'valence', XName);
	plotGraph(dataset.energy, dataOnX, 'energy', XName);
	plotGraph(dataset.dance, dataOnX, 'dance', XName);
	plotGraph(dataset.acoustic, dataOnX, 'acoustic', XName);
	plotGraph(dataset.instrumental, dataOnX, 'instrumental', XName);

MultiPlot(dataset.enjoy_life, 'enjoy life');
MultiPlot(dataset.resilience, 'resilience');
MultiPlot(dataset.balanced, 'balanced');
MultiPlot(dataset.emotional_flexibility, 'emotional flexibility');
MultiPlot(dataset.self_actualization, 'self actualization');
MultiPlot(dataset.health, 'health');


"""
url = http://dgfc.dg.gov.cn/dgwebsite_v2/Vendition/ProjectInfo.aspx?new=1
city : 东莞
CO_INDEX : 9
author: 吕三利
小区数量 : 60    2018/2/24

"""
from backup.crawler_base import Crawler
from lxml import etree
from backup.comm_info import Building, House
import requests, re
from backup.tool import Tool

co_index = 9

class Dongwan(Crawler):
    def __init__(self):
        self.url = 'http://dgfc.dg.gov.cn/dgwebsite_v2/Vendition/ProjectInfo.aspx?new=1'
        self.link_url = 'http://dgfc.dg.gov.cn/dgwebsite_v2/Vendition/'
        self.co_index = 9
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36',
        }
        self.area_list = ['东莞市莞城区',
                          '东莞市东城区',
                          '东莞市万江区',
                          '东莞市南城区',
                          '东莞市石龙镇',
                          '东莞市虎门镇',
                          '东莞市中堂镇',
                          '东莞市望牛墩镇',
                          '东莞市麻涌镇',
                          '东莞市石碣镇',
                          '东莞市高埗镇',
                          '东莞市道滘镇',
                          '东莞市洪梅镇',
                          '东莞市长安镇',
                          '东莞市沙田镇',
                          '东莞市厚街镇',
                          '东莞市松山湖',
                          '东莞市寮步镇',
                          '东莞市大岭山镇',
                          '东莞市大朗镇',
                          '东莞市黄江镇',
                          '东莞市樟木头镇',
                          '东莞市凤岗镇',
                          '东莞市塘厦镇',
                          '东莞市谢岗镇',
                          '东莞市清溪镇',
                          '东莞市常平镇',
                          '东莞市桥头镇',
                          '东莞市横沥镇',
                          '东莞市东坑镇',
                          '东莞市企石镇',
                          '东莞市石排镇',
                          '东莞市茶山镇',
                          '东莞市虎门港',
                          '东莞市生态产业园',
                          ]

    def start_crawler(self):
        town_list = self.get_town_name()
        print(town_list)
        view_dict = Tool.get_view_state(self.url,
                                        view_state='//*[@id="__VIEWSTATE"]/@value',
                                        event_validation='//*[@id="__EVENTVALIDATION"]/@value')
        # print(view_dict)
        # print(town_list)
        all_building_url_list = self.get_all_first_page_url(town_list, view_dict)
        print(all_building_url_list)

        house_url_list = self.get_build_detail(all_building_url_list)

        self.get_house_detail(house_url_list)

    def get_house_detail(self, house_url_list):
        print(house_url_list)
        for i in house_url_list:
            try:
                response = requests.get(i, headers=self.headers)
                html = response.text
                house_html = re.search('id=.roomTable.*?id="remarkDiv"', html, re.S | re.M).group()
                house_info_list = re.findall('", i, re.S | re.M).group(1)
                    house.ho_name = re.search("(.*?)	", i, re.S | re.M).group(1)
                    if 'id' in house.ho_name:
                        house.ho_name = re.search('(.*?)', house.ho_name, re.S | re.M).group(1)
                    house.insert_db()

            except Exception as e:
                print('房号错误，co_index={},url={}'.format(co_index, i), e)
        print('房号放入完成')

    def get_build_detail(self, all_building_url_list):
        house_url_list = []
        for i in all_building_url_list:
            try:
                response = requests.get(i, headers=self.headers)
                html = response.text
                tree = etree.HTML(html)
                bo_develops = tree.xpath('//*[@id="content_1"]/div[3]/text()[2]')[0]  # 开发商
                bu_build_size = tree.xpath('//*[@id="houseTable_1"]/tr[2]/td[6]/a/text()')  # 销售面积
                if bu_build_size:
                    bu_build_size = bu_build_size[0]
                bu_pre_sale = tree.xpath('//*[@id="houseTable_1"]/tr[2]/td[1]/a/text()')  # 预售证书
                if bu_pre_sale:
                    bu_pre_sale = bu_pre_sale[0]
                bu_floor = tree.xpath('//*[@id="houseTable_1"]/tr[2]/td[3]/a/text()')[0]  # 总层数
                bu_all_house = tree.xpath('//*[@id="houseTable_1"]/tr[2]/td[4]/a/text()')[0]  # 总套数
                bu_type = tree.xpath('//*[@id="houseTable_1"]/tr[2]/td[5]/a/text()')[0]  # 房屋用途
                build_html = re.search('houseTable_1.*?当前共有', html, re.S | re.M).group()
                build_detail_html = re.findall('class.*?	.*?	.*?	', build_html, re.S | re.M)
                bu_num = re.findall('项目名称：(.*?)
', html, re.S | re.M)[0].strip()
                url_list = []
                for bu in build_detail_html:
                    try:
                        build = Building(co_index)
                        build.bu_id = re.search("href='roomTable.aspx\?id=(.*?)&", bu, re.S | re.M).group(1)
                        build.bu_address = re.search("_blank.*?_blank'>(.*?)	", bu, re.S | re.M).group(
                            1).strip()
                        build.bo_develops = bo_develops
                        build.bu_build_size = bu_build_size
                        build.bu_pre_sale = bu_pre_sale
                        build.bu_num = bu_num
                        build.bu_floor = bu_floor
                        build.bu_all_house = bu_all_house
                        build.bu_type = bu_type
                        for k in self.area_list:
                            if k in build.bu_address:
                                build.area = k
                                continue
                        build.insert_db()
                        house_url = re.search("(roomTable.aspx\?id=.*?&vc=.*?)'", bu, re.S | re.M).group(1)
                        url_list.append('http://dgfc.dg.gov.cn/dgwebsite_v2/Vendition/' + house_url)
                    except Exception as e:
                        print('楼栋错误，co_index={},url={}'.format(co_index, i), e)
                house_url_list = url_list + house_url_list
            except Exception as e:
                print('楼栋错误，co_index={},url={}'.format(co_index, i), e)
        return house_url_list

    @staticmethod
    def get_all_first_page_url(town_list, view_dict):
        all_building_url_list = []
        for i in town_list:
            try:
                data = {
                    'townName': i,
                    '__EVENTVALIDATION': view_dict['__EVENTVALIDATION'],
                    '__VIEWSTATE': view_dict['__VIEWSTATE'],
                }
                res = requests.post('http://dgfc.dg.gov.cn/dgwebsite_v2/Vendition/ProjectInfo.aspx?new=1', data=data)
                html = etree.HTML(res.content.decode())
                url_list = html.xpath('//*[@id="resultTable"]/tr/td[1]/a/@href')
                complete_url_list = []
                for k in url_list:
                    complete_url_list.append('http://dgfc.dg.gov.cn/dgwebsite_v2/Vendition/' + k)
                all_building_url_list = all_building_url_list + complete_url_list
            except Exception as e:
                print(e)
                continue
        return all_building_url_list

    def get_town_name(self):
        res = requests.get(self.url)
        html = etree.HTML(res.content)
        return html.xpath('//*[@id="townName"]/option/@value')


import requests
from bs4 import BeautifulSoup

request = requests.get("http://www.johnlewis.com/john-lewis-wade-office-chair-black/p447855")
content = request.content
soup = BeautifulSoup(content, "html.parser")
element = soup.find("span", {"itemprop": "price", "class": "now-price"})
stringPrice = element.text.strip()
priceWithoutSymbol = stringPrice[1:]
price = float(priceWithoutSymbol)
num = input("Whats your budget? ")
budget = float(num)
if budget > price:
    print("Buy it")
else:
    print("Dont buy")

#$19.77

#print(request.content)


"""Create a Dungeon & Dragon character with attributes, items in inventory"""

# Yongju Kwon
# A01059332
# 05 Feb 2019

import random

def roll_die(number_of_rolls: int, number_of_sides: int):
    """Return sum of numbers on the side of dice rolled 'number of rolls' number of times.

    PARAM: number_of_rolls, a positive integer
    PARAM: number_of_sides, a positive integer
    PRECONDITION: number_of_rolls, must be a positive integer
    PRECONDITION: number_of_sides, must be a positive integer
    POSTCONDITION: calculate the sum of number on the side of die each time
    """
    random_numbers = []

    for roll in range(0, number_of_rolls):
        random_num = random.randint(1, number_of_sides)
        random_numbers.append(random_num)
    return sum(random_numbers)

def choose_inventory(inventory: list, selection: int):
    """Return sorted list having values as many as selection.

    PARAM: inventory, a list
    PARAM: selection, a positive integer
    PRECONDITION: inventory must be a list has items
    PRECONDITION: selection must be a positive integer
    POSTCONDITION: Randomly select items from inventory and sort the item list
    """
    if len(inventory) == 0 and selection == 0:
        return []
    elif selection < 0:
        print("Selection must be positive integer!")
        return ''
    elif selection > 0 and selection > len(inventory):
        print("Selection must be less than the length of inventory!")
        return ''
    elif selection == len(inventory):
        return inventory[:]
    else:
        sorted_items = random.sample(inventory, selection)
        sorted_items.sort()
        return sorted_items

def create_name(length: int):
    """Return random string as longer as the length.

    PARAM: length, a positive integer
    PRECONDITION: length, must be a positive integer
    POSTCONDITION: Randomly select the alphabets and concatenate them
    """
    alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                 'u', 'v', 'w', 'x', 'y', 'z']
    my_string = ""

    if length > 0:
        for letter in range(0, length):
            my_string += random.choice(alphabets)
    elif length <= 0:
        return None
    return my_string.title()

def create_character(name_length: int):
    """Return a list has value of names and attributes.

    PARAM: name_length, a positive integer
    PRECONDITION: name_length, must be a positive integer
    POSTCONDITION: Append names and attributes into new list
    """
    if name_length > 0:
        new_character = {'Name:': create_name(name_length),
                         'Strength:': roll_die(3, 6),
                         'Dexterity:': roll_die(3, 6),
                         'Constitution:': roll_die(3, 6),
                         'Intelligence:': roll_die(3, 6),
                         'Wisdom:': roll_die(3, 6),
                         'Charisma:': roll_die(3, 6)}
        return new_character
    else:
        print("Name_length must be a positive integer!")
        return None

def print_character(character):
    """Print the information of character.

    PARAM: character, a list
    PRECONDITION: character, must be formatted by the create_character function
    """
    for key, value in character.items():
        print(key, value)

def main():
    print("roll_die:", roll_die(3, 6))
    print("\n")

    user_number = int(input("We have 10 items. Enter the number of items you want to get\n"))
    user_items = choose_inventory(['Amulet of Health', 'Palm Sign', 'Ankh of life', 'Helm of Halav',
                                   'The Ring of Dimensional Rifts', 'Umber Spear', 'The Oculus', 'Yin & Yang',
                                   'Paradox Watch', 'Holy Night'], user_number)
    print("Your inventory: \n",
          user_items)
    print("\n")

    length_of_name = int(input("Enter the number of letters of your character name\n"))
    user_character = create_character(length_of_name)
    print("\n")
    user_character['Inventory:'] = user_items
    print_character(user_character)

if __name__ == '__main__':
    main()


#encoding: utf-8
from django.db import models
from django.contrib.auth.models import User
from django.core.mail import send_mail, get_connection, EmailMessage

class Choices(models.Model):
    tamanho = (
        #Banco de Dados, Mostrar ao Usuário
        ('P', 'P'),
        ('M', 'M'),
        ('G', 'G'),
        ('GG', 'GG'),
        ('XG', 'XG'),
    )

    situacao = (
        ('Apresentou', 'Apresentou'),
        ('Nao Apresentou', 'Nao Apresentou'),
    )
    campus = (
        ('IFF','IFF - Centro'),
        ('Guarus','IFF - Guarus'),
        ('Macaé','IFF - Macaé'),
    )

    participacao = (
        ('Professor','Professor'),
        ('Orientador','Orientador'),
        ('Coordenador','Coordenador'),
    )
    dia = (
        (u'01', u'01'),
        (u'02', u'02'),
        (u'03', u'03'),
        (u'04', u'04'),
        (u'05', u'05'),
        (u'06', u'06'),
        (u'07', u'07'),
        (u'08', u'08'),
        (u'09', u'09'),
        (u'10', u'10'),
        (u'11', u'11'),
        (u'12', u'12'),
        (u'13', u'13'),
        (u'14', u'14'),
        (u'15', u'15'),
        (u'16', u'16'),
        (u'17', u'17'),
        (u'18', u'18'),
        (u'19', u'19'),
        (u'20', u'20'),
        (u'21', u'21'),
        (u'22', u'22'),
        (u'23', u'23'),
        (u'24', u'24'),
        (u'25', u'25'),
        (u'26', u'26'),
        (u'27', u'27'),
        (u'28', u'28'),
        (u'29', u'29'),
        (u'30', u'30'),
        (u'31', u'31'),
    )

    mes = (
        (u'Janeiro', u'Janeiro'),
        (u'Fevereiro', u'Fevereio'),
        (u'Março', u'Março'),
        (u'Abril', u'Abril'),
        (u'Maio', u'Maio'),
        (u'Junho', u'Junho'),
        (u'Julho', u'Julho'),
        (u'Agosto', u'Agosto'),
        (u'Setembro', u'Setembro'),
        (u'Outubro', u'Outubro'),
        (u'Novembro', u'Novembro'),
        (u'Dezembro', u'Dezembro'),
   )

    ano = (
        (u'2011', u'2011'),
        (u'2012', u'2012'),
        (u'2013', u'2013'),
    )

class Area(models.Model):
    class Meta:
        verbose_name = 'Área'
        verbose_name_plural = 'Áreas'

    codArea = models.AutoField(primary_key=True)
    descricao = models.CharField("Descrição",max_length=50)

    def __unicode__(self):
        return self.descricao

class Campus(models.Model):
    class Meta:
        verbose_name = 'Campus'
        verbose_name_plural = 'Campi'

    codCampus = models.AutoField(primary_key=True)
    descricao = models.CharField("Descrição",max_length=50)

    def __unicode__(self):
        return self.descricao

class Projeto(models.Model):
    class Meta:
        verbose_name = 'Projeto'
        verbose_name_plural = 'Projetos'

    codProj = models.AutoField(primary_key=True)
    nomeProj = models.CharField("Nome do Projeto",max_length=20, blank=True,help_text="Digite um Nome ao seu projeto",unique=True)
    resumoProj = models.TextField("Resumo",max_length=640, blank=True,help_text="Faça um breve resumo de até 640 caracteres do seu projeto")
    materialProj = models.TextField("Material",max_length=640, blank=True,help_text="Material a ser Adquirido para o Projeto")
    blocoProj = models.CharField("Bloco",max_length=20, blank=True,default = "A ser definido")
    salaProj = models.CharField("Sala",max_length=20, blank=True,default = "A ser definido")
    equipamentoProj = models.TextField("Equipamento",max_length=640, blank=True,help_text="Equipamento necessário para o Projeto")
    criador = models.CharField("Criador", max_length=50,blank=True)
    situacao = models.CharField("Situacao", max_length=50, blank=True,default="Nao Apresentou")
    enviar = models.CharField('Enviar?',max_length=10,blank=True,default="Pendente")
    campusProj = models.ForeignKey('Campus')
    area = models.ForeignKey('Area')

    def __unicode__(self):
        return self.nomeProj

    def save(self, *args, **kwargs):
        super(Projeto, self).save(*args, **kwargs)
    ## ENVIA 1 EMAIL SOMENTE SE FOI APROVADO O CADASTRO ##
        if self.situacao == 'Aprovado' and self.enviar == 'Enviar':
           enviar = User.objects.get(username__iexact=self.criador)
           subject = 'Projeto Feitec'
           message = 'O projeto %s foi será no \nBloco: %s \nSala: %s' %(self.nomeProj,self.blocoProj,self.salaProj)
           from_email = ''#email remetente
           connection = get_connection(username = '', password ='')#email e senha de conexão
           send_email = EmailMessage(subject, message , from_email, [enviar.email], connection = connection)
           send_email.content_subtype = "html"
           send_email.send()

        if (self.situacao == 'Aprovado' or self.situacao == 'Reprovado') and self.enviar == 'Pendente':
            enviar = User.objects.get(username__iexact=self.criador)
            subject = 'Projeto Feitec'
            message = 'O projeto %s foi %s!' %(self.nomeProj,self.situacao)
            from_email = ''#email remetente
            connection = get_connection(username = '', password ='')#email e senha de conexão
            send_email = EmailMessage(subject, message , from_email, [enviar.email], connection = connection)
            send_email.content_subtype = "html"
            send_email.send()

        if self.situacao == 'Enviado':
            enviar = User.objects.get(username__iexact=self.criador)
            subject = 'Projeto Feitec'
            message = 'O projeto %s foi enviado com sucesso.  Aguarde a avaliação do próprio' %(self.nomeProj)
            from_email = ''#email remetente
            connection = get_connection(username = '', password ='')#email e senha de conexão
            send_email = EmailMessage(subject, message , from_email, [enviar.email], connection = connection)
            send_email.content_subtype = "html"
            send_email.send()

class Professor(models.Model):
    class Meta:
        verbose_name = 'Professor'
        verbose_name_plural = 'Professores'

    codProfessor = models.AutoField(primary_key=True)
    nomeProfessor = models.CharField("Nome do Professor",max_length=50, blank=True)
    emailProfessor = models.EmailField("Email",max_length=50, blank=True)
    matriculaProfessor = models.CharField("Matrícula SIAPE",max_length=7, blank=True)
    tipoProfessor = models.CharField('Participação',max_length=50,blank=True,choices=Choices.participacao)
    projeto = models.ForeignKey('Projeto')

    def __unicode__(self):
        return self.nomeProfessor

class Integrante(models.Model):
    class Meta:
        verbose_name = 'Integrante'
        verbose_name_plural = 'Integrantes'

    codIntegrante = models.AutoField(primary_key=True)
    nomeIntegrante = models.CharField("Nome do Integrante",max_length=50, blank=True)
    cpfIntegrante = models.CharField("Cpf", max_length=11, blank=True)
    cursoIntegrante = models.CharField("Curso", max_length=50, blank=True)
    matriculaIntegrante = models.CharField("Matrícula", max_length=15, blank=True)
    campusIntegrante = models.ForeignKey('Campus')
    tamanhocamisaIntegrante = models.CharField("Tamanho da Camisa", max_length=2,blank=True, choices=Choices.tamanho)
    situacaoIntegrante = models.CharField("Situacao", max_length=20, blank=True, choices=Choices.situacao,default="Pendente")
    projeto = models.ForeignKey('Projeto')

    def __unicode__(self):
        return self.nomeIntegrante

class CertificadoIntegrante(models.Model):
    class Meta:
        verbose_name = 'Certificado do Integrante'
        verbose_name_plural = 'Certificados dos Integrantes'

    codCert = models.AutoField(primary_key=True)
    numeroCert= models.CharField("Numero",max_length=10,default = "00",blank=True)
    livroCert= models.CharField("Livro", max_length=10,default = "00",blank=True)
    dataCert= models.CharField("Data", max_length=10,default = "--/--/----",blank=True)
    data = models.CharField("Data", max_length=10, choices = Choices.dia, blank = True)
    mes = models.CharField("Mes", max_length=20, choices = Choices.mes, blank = True)
    ano = models.CharField("Ano", max_length=10, choices = Choices.ano, blank = True)
    integrante = models.ForeignKey('Integrante')

class CertificadoProfessor(models.Model):
    class Meta:
        verbose_name = 'Certificado do Professor'
        verbose_name_plural = 'Certificados dos Professores'

    codCert = models.AutoField(primary_key=True)
    numeroCert= models.CharField("Numero",max_length=10,default = "00",blank=True)
    livroCert= models.CharField("Livro", max_length=10,default = "00",blank=True)
    dataCert= models.CharField("Data", max_length=10,default = "--/--/----",blank=True)
    data = models.CharField("Data", max_length=10, choices = Choices.dia, blank = True)
    mes = models.CharField("Mes", max_length=20, choices = Choices.mes, blank = True)
    ano = models.CharField("Ano", max_length=10, choices = Choices.ano, blank = True)
    professor = models.ForeignKey('Professor')