import pickle

# variable settings for learning process
learning_rate = 1
discount_factor = 1

class Instruction:
    def __init__(self, thread_id, instruction_offset, accessed_offset, is_read, is_write):
        self.thread_id = thread_id
        self.instruction_offset = instruction_offset
        self.accessed_offset = accessed_offset
        self.is_read = is_read
        self.is_write = is_write

instructions = []
with open("profiler_rl.log", "r") as file:
    lines = file.readlines()
    for line in lines:
        temp1, temp2, temp3, temp4, temp5 = line.split()
        instruction = Instruction(temp2[:-1], temp1[:-1], temp3[:-1], temp4[:-1], temp5)
        instructions.append(instruction)

trace = []
with open("trace", "r") as file:
    lines = file.readlines()
    for line in lines:
        trace.append(int(line.rstrip()))

def calculate_reward(instruction_offset_1, instruction_offset_2):
    """
    docstring
    """

    instructions_length = len(instructions)
    for i in range(instructions_length):
        if int(instructions[i].instruction_offset) == instruction_offset_1:
            index_1 = i
        if int(instructions[i].instruction_offset) == instruction_offset_2:
            index_2 = i

    reward = 0

    if instructions[index_1].accessed_offset == instructions[index_2].accessed_offset:
        if instructions[index_1].thread_id != instructions[index_2].thread_id:
            if instructions[index_1].is_read == "1" and instructions[index_2].is_write == "1":
                reward = 1
            elif instructions[index_1].is_write == "1" and instructions[index_2].is_read == "1":
                reward = 1

    # print("Reward of ", instruction_offset_1, instruction_offset_2, reward)
    return reward

class State:
    def __init__(self, offset):
        self.offset = offset
        self.q_value = {}

states = []
with open("states_binary", "rb") as pickle_file:
    states = pickle.load(pickle_file)

def create_state(offset):
    new_state = State(offset)
    states.append(new_state)

def update_q_value(current, next):
    is_contained_current = 0
    is_contained_next = 0
    list_length = len(states)
    reward = calculate_reward(current, next)
    for i in range(list_length):
        if str(states[i].offset) == str(current):
            is_contained_current = 1
            index_current = i
        if str(states[i].offset) == str(next):
            is_contained_next = 1
            index_next = i

    if is_contained_current == 0 and is_contained_next == 0:
        create_state(current)
        states[-1].q_value[str(next)] = learning_rate * reward

    elif is_contained_current == 0 and is_contained_next == 1:
        create_state(current)
        states[-1].q_value[str(next)] = learning_rate * (reward + discount_factor * max(states[index_next].q_value.values()))

    elif is_contained_current == 1 and is_contained_next == 1:
        if str(next) in states[index_current].q_value:
            states[index_current].q_value[str(next)] = states[index_current].q_value[str(next)] + learning_rate * (reward + discount_factor * max(states[index_next].q_value.values()) - states[index_current].q_value[str(next)])
        else:
            states[index_current].q_value[str(next)] = learning_rate * (reward + discount_factor * max(states[index_next].q_value.values()))

trace_length = len(trace)
for i in range(trace_length-1):
    update_q_value(trace[i], trace[i+1])

for state in states:
    print(state.offset, state.q_value)

with open("states_binary", "wb") as pickle_file:
    pickle.dump(states, pickle_file)


from sklearn.preprocessing import StandardScaler
import pandas as pd
import numpy as np
import tensorflow as tf
import random as rnd

#Import Data Set
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
dataframe = pd.read_csv('prima-indians-diabetes.txt', names=names)
dataset = dataframe.as_matrix()

#Create Scaled Input Layer Data
x_data = dataset[:,0:7]
Y = dataset[:,8]
scalerS = StandardScaler().fit(x_data)
x_data = scalerS.transform(x_data)

#Define Model Variables
X = tf.Variable(x_data,tf.float64)
iterations = 1000
layers = {'0':63,'1':84,'output':2}
features = 7
alpha = .05

biases = {
      '0':tf.cast(tf.Variable(tf.ones([layers['0']])),tf.float64),
      '1':tf.cast(tf.Variable(tf.ones(layers['1'])),tf.float64),
      '2':tf.cast(tf.Variable(tf.ones([layers['output']])),tf.float64)
      }

weights = {
           '0':tf.cast(tf.Variable(tf.random_normal([features,layers['0']])),tf.float64),
           '1':tf.cast(tf.Variable(tf.random_normal([layers['0'],layers['1']])),tf.float64),
           '2':tf.cast(tf.Variable(tf.random_normal([layers['1'],layers['output']])),tf.float64),
           }

def feedforward(X):
    z1 = (tf.matmul(X,weights['0'])+biases['0'])
    z2 = (tf.matmul(z1,weights['1'])+biases['1'])
    z3 = (tf.matmul(z2,weights['2'])+biases['2'])
    #Final Hypothesis
    ht = z3
    return ht

def model(X,Y,layers,iterations,features,alpha):

    sess = tf.Session()
    ht = feedforward(X)

    y_matrix = np.zeros((768,2))

    for i,h in enumerate(Y):
        if Y[i]==1:
            y_matrix[i,1]=1
        else:
            y_matrix[i,0]=1

    Y = tf.constant(y_matrix,tf.float64)
    Y = tf.cast(Y,tf.float64)

    #Create Cost Function & Train
    #cross_entropy = tf.reduce_mean(-tf.reduce_sum(Y - tf.log(ht),reduction_indices=[1]))
    cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=Y,logits=ht))
    train_step = tf.train.GradientDescentOptimizer(alpha).minimize(cross_entropy)

    init = tf.global_variables_initializer()
    sess.run(init)

    for _ in range(iterations):
        sess.run(train_step)

    ans = []
    correct_prediction = tf.equal(tf.argmax(ht,1),tf.argmax(Y,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    ans = sess.run(correct_prediction)
    #Print Accuracy
    model_accuracy = sess.run(accuracy)

    return model_accuracy

def test_hyperparams(X,Y,iterations,features):
    best_config = {'accuracy':.001, 'config':{}}
    for i in range(100):
        l1n = rnd.randint(3, 100) #layer one nueron count
        l2n = rnd.randint(3, 100)   #layer two nueron count
        alpha = .05
        layers = {'0':l1n,'1':l2n, 'output':2}
        ans = (model(X,Y,layers,iterations,features,alpha))
        if ans > best_config['accuracy']:
            best_config['accuracy'] = ans
            best_config['config'] = layers
        print(best_config)
    return best_config

#final_config= print(test_hyperparams(X,Y,iterations,features))
#print('Best Configuration Identified:')
#print(final_config)

#print(model(X,Y,layers,iterations,features,alpha))
print(dataset[1,:])
print(x_data[1,:])

test_x = dataset[1,:]
print(test_x)
scalerT = StandardScaler().fit(test_x)
test_x = scalerT.transform(scalerT)
print(test_x)
test_x = tf.Variable(test_x,tf.float64)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run([test_x]))

#print(sess.run([X[1,:]]))
#print(X[1,:])

#dataset = np.concatenate((ans,dataset))
#print(ans)
#print(ans.size)
#print(dataset.shape)
#print(y_matrix.size)
#print(sess.run(ht[766]))
#print(sess.run(Y[766]))
#print(dataset[766,:])
#print(ans[766])

#Forward Prop
#Try building this as a function
#def forward_prop(b,w):
#    layer_count = len(weights.keys())
#    layers = {}
#    print(w)
#    for li in range(layer_count):
#        layers[li] = tf.matmul(w[i],w[i])
#
#        #print(li)
#        if li==0:
#            #print(0)
#            #print(w)
#            #layers[li] = (tf.matmul(X,w[i]),+b[i])
#        #else:
#            #layers[li] = (tf.matmul(layers[i],w[i]),+b[i]])
#            print(layers)
#
#
#        #layers = {}
#        #index,val
##    for i,v in enumerate(biases):
##        z = 0;
##        weight = w.values()[i]
##        print(weight)
##        if i == 1:
##            print(1)
##        layers['l'+str(i)] = v
#
#    #print(layers)
#    return 'done'


from __future__ import print_function

import argparse
import sys
from nlp.reviews.utils import *
from mlflow import mlflow

spark = get_spark()
logger = get_logger()

try:
    mlflow_tracking_uri = sys.argv[1]  # "databricks://adb-field-eng"
    data_file_location = sys.argv[2]  # "/databricks-datasets/amazon/data20K"
    scoring_data_file_location = sys.argv[3]  # "/databricks-datasets/amazon/test4K"
except:
    # parser.print_help()
    logger.warn("the following arguments are required: mlflow_tracking_uri, data_file_location, scoring_data_file_location")
    logger.warn("received no args at runtime, using defaults")
    logger.warn("\n")
    mlflow_tracking_uri = "databricks://adb-field-eng"
    data_file_location = "/databricks-datasets/amazon/data20K"
    scoring_data_file_location = "/databricks-datasets/amazon/test4K"

'''
- Tests connectivity to hosted mlflow server using a databricks cli profile configured on your local system
    - check_mlflow_connectivity("databricks://adb-field-eng")
- Checks if datasets used are available
    - check_datasets_availability("/databricks-datasets/amazon/data20K","/databricks-datasets/amazon/test4K")
- Checks if mlflow lib (PyPi) is installed on the cluster
    - check_if_libs_installed()
'''

def check_if_libs_installed():
    print("\n *********** checking if mlflow is installed ***************")
    if(len(mlflow.version.VERSION) > 0):
        print("\nMLflow Version installed on databricks cluster:", mlflow.version.VERSION)
    else:
        logger.error("mlflow lib not found on databricks cluster, please install mlflow lib")
        assert False

def check_datasets_availability(data_file_location,scoring_data_file_location):
    print("\n *********** checking if datasets exists ***************")
    raw_data = spark.read.parquet(data_file_location).select("rating","review")
    if(raw_data.count() > 0):
        print(data_file_location + " exists and contains " + str(raw_data.count()) + " records")
    else:
        logger.error("couldn't locate " + data_file_location )
        assert False

    scoring_data = spark.read.parquet(scoring_data_file_location).select("review")
    if (scoring_data.count() > 0):
        print(scoring_data_file_location + " exists and contains " + str(scoring_data.count()) + " records")
    else:
        logger.error("couldn't locate " + scoring_data_file_location)
        assert False

def check_mlflow_connectivity(mlflow_tracking_uri):
    print("\n *********** checking mlflow connectivity ***************")
    mlflow_tracking_uri = mlflow_tracking_uri
    mlflow.set_tracking_uri(mlflow_tracking_uri)
    client = mlflow.tracking.MlflowClient()
    print("There are in total " + str(len(client.list_experiments())) + " experiments on your mlflow tracking server")
    print("You are connected to databricks hosted tracking server using ******" + mlflow_tracking_uri)

# check_if_libs_installed()
# check_mlflow_connectivity("databricks://adb-field-eng")

def validate_env_setup(data_file_location, scoring_data_file_location,mlflow_tracking_uri):
    print("***************** running environment setup tests ********************")
    check_datasets_availability(data_file_location, scoring_data_file_location)
    check_if_libs_installed()
    check_mlflow_connectivity(mlflow_tracking_uri)

#validate setup

if __name__ == "__main__":
    validate_env_setup(data_file_location, scoring_data_file_location, mlflow_tracking_uri)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import codecs

from codecs_to_hex import to_hex

def main():
    text = b'abcdefghijklmnopqrstuvwxyz\n'
    repetitions = 50

    print("Text length :", len(text))
    print("Repetitions :", repetitions)
    print("Expected len:", len(text) * repetitions)

    encoder = codecs.getincrementalencoder("bz2")()
    encoded = []

    print("\nEncoding:", end=' ')
    last = repetitions - 1
    for i in range(repetitions):
        en_c = encoder.encode(text, final=(i == last))
        if en_c:
            print("\nEncoded: {} bytes".format(len(en_c)))
            encoded.append(en_c)
        else:
            sys.stdout.write('.')

    all_encoded = b''.join(encoded)
    print("\nTotal encoded length: {}\n".format(len(all_encoded)))

    decoder = codecs.getincrementaldecoder("bz2")()
    decoded = []

    print("Decoding:", end=' ')
    for i, b in enumerate(all_encoded):
        final = (i + 1) == len(text)
        c = decoder.decode(bytes([b]), final)
        if c:
            print("\nDecoded: {} characters".format(len(c)))
            print("Decoding:", end=' ')
            decoded.append(c)
        else:
            sys.stdout.write('.')

    restored = b''.join(decoded)
    print("\n\nTotal uncompressed length:", len(restored))

if __name__ == '__main__':
    main()


#!/usr/bin/env python

import sys
import time
import RPi.GPIO as gp

gp.setmode(gp.BOARD)

pin_list = [7,11,12,13,15,16]

def blink(pin):
    for x in range(1):
        gp.output(pin, gp.HIGH)
        time.sleep(0.25)
        gp.output(pin, gp.LOW)
        time.sleep(0.1)

if len(sys.argv) > 1:
    pin = int(sys.argv[1])
    gp.setup(pin, gp.OUT)
    for x in range(15):
        blink(pin)
        time.sleep(0.1)
else:
    for x in range(15):
        for p in pin_list:
            gp.setup(p, gp.OUT)
            blink(p)

gp.cleanup()


fh = open('words.txt')

word_dict = dict()
for line in fh :
    line = line.rstrip()
    words = line.split()
    for w in words :
        word_dict[w] = ''
    print(word_dict)

s = input('Enter a word: ')
if s in word_dict :
    print(s, 'is in the dictionary')
else:
    print(s, 'is not in the dictionary')


# You are climbing a stair case. It takes n steps to reach to the top.
# Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
#
# Examples:
# Input: 2
# Output: 2
# Explanation: There are two ways to climb to the top.
# 1. 1 step + 1 step
# 2. 2 steps
#
# Input: 3
# Output: 3
# Explanation: There are three ways to climb to the top.
# 1. 1 step + 1 step + 1 step
# 2. 1 step + 2 steps
# 3. 2 steps + 1 step

def climbStairs(n: int) -> int:
    if n ==1:
        return 1
    elif n==2:
        return 2
    result= climbStairs(n - 1) + climbStairs(n - 2)
    return result

print (climbStairs(5))


#Author : Chitrasen
#Date : 11/1/2013
# Basic benchmark code LogisticRegression with sampling agents

import pandas as pd
import re
from sklearn import cross_validation,metrics
from sklearn.feature_extraction.text import CountVectorizer
from sklearn import linear_model as lm
from sklearn.metrics import mean_squared_error
from sklearn.decomposition import TruncatedSVD
from sklearn.tree import DecisionTreeClassifier

#import PorterStemmer

def clean(s):
    s = s.lower()
    s = re.sub('n\'t',' not',s)
    s = re.sub('{link}',' ',s)
    s = re.sub('#\w+',' ',s)
    s = re.sub('@\w+',' ',s)
    s = re.sub('[\W]',' ',s)
    s = re.sub('\s+',' ',s)
    s = removeStopWords(s)
    return s

def removeStopWords(s):
    stopWordsStr = 'a about above after again against all am an and any are as at be because been before being below between both but by cannot could did do does doing down during each few for from further had has have having he he\'d he\'ll he\'s her here here\'s hers herself him himself his how how\'s i i\'d i\'ll i\'m i\'ve if in into is it it\'s its itself let\'s me more most my myself no nor not of off on once only or other ought our ours  ourselves out over own rt same she she\'d she\'ll she\'s should so some such than that that\'s the their theirs them themselves then there there\'s these they they\'d they\'ll they\'re they\'ve this those through to too under until up very was we we\'d we\'ll we\'re we\'ve were what what\'s when when\'s where where\'s which while who who\'s whom why why\'s with would you you\'d you\'ll you\'re you\'ve your yours yourself yourselves'
    stopWords = re.split('\s+',stopWordsStr)
    cleanwords = [w for w in re.split('\W+',s) if w not in stopWords]

    p = PorterStemmer()

    s = ''
    for word in cleanwords:
        s += " " +p.stem(word,0,len(word)-1)
    return s

def normalize_prob(a):
    row_sums = a.sum(axis =1)
    return a/row_sums[:,np.newaxis]

def isItRetweet(df):
    return [len(re.findall('^rt', tweet)) > 0 for tweet in df.tweet]

def hashTagsCount(df):
    return [len(re.findall('\#.',tweet)) for tweet in df.tweet]

def mentionCount(df):
    return [len(re.findall('\@.',tweet)) for tweet in df.tweet]

def numericContains(df):
    return [len(re.findall('[0-9]+[:.]*[0-9]*.',tweet)) for tweet in df.tweet]

def containsAMPM(df):
    return [len(re.findall('[0-9]+\s*am|pm',tweet)) for tweet in df.tweet]

def linksCount(df):
    return [len(re.findall('{link}',tweet)) for tweet in df.tweet]

def containsHashWeather(df):
    return [len(re.findall('\#weather',tweet)) for tweet in df.tweet]

def tweetLength(df):
    return [len(tweet) for tweet in df.tweet]

def getHighFeqWords(df,col,percent = 0.85, stop_words = 'english',max_features = 50):
    df1 = df['tweet'][df[col] > percent]
    countVect = CountVectorizer(strip_accents = 'ascii',analyzer='word',stop_words = stop_words,
                                max_features = max_features,binary = True)

    countVect.fit(df1.values)
    maxFeatures = countVect.fit(df['tweet'][df[col] > percent]).get_feature_names()
    print(maxFeatures)
    countVect = CountVectorizer(vocabulary=maxFeatures)
    return countVect.fit_transform(df.tweet).toarray()

def getFeatureVecture(df,function_list = None,col_category=None):

    returnVals = None

    if(function_list == None):
        function_chain = [isItRetweet,hashTagsCount,mentionCount,numericContains,containsAMPM,linksCount,
                      containsHashWeather,tweetLength]
        returnVals = np.hstack([np.matrix(func(df)).T for func in function_chain])

    return returnVals

def main():
    print('Reading datasets...')
    train = pd.read_csv('/home/trupti/data_local/kaggle/partly_sunny/train.csv',header = 0)
    test = pd.read_csv('/home/trupti/data_local/kaggle/partly_sunny/test.csv', header = 0)

    pred = pd.DataFrame(np.zeros((test.shape[0], train.shape[1]- test.shape[1])), columns = train.columns[4:])
    pred.index = test['id']

    tweets_train = train['tweet']
    tweets_test = test['tweet']
    #X_all = list(tweets_train)+list(tweets_test)

    X_all_a = list(tweets_train)+list(tweets_test)
    X_all = [clean(s) for s in X_all_a]

    tfidf = CountVectorizer(X_all,min_df=5,  max_features=None, strip_accents='unicode',
        analyzer='word',token_pattern=r'\w{1,}',ngram_range=(1,1),stop_words = 'english')

    lr = lm.LogisticRegression(penalty='l2', dual=True, tol=0.0001,
                           C=1, fit_intercept=True, intercept_scaling=1.0,
                           class_weight=None, random_state=None)

    clf = DecisionTreeClassifier()

    print('fitting TfIDF...')
    trainLen = tweets_train.shape[0]

    tfidf.fit(X_all)
    X_all = tfidf.transform(X_all)
    lsa = TruncatedSVD(n_components = 100)

    print('Fitting in LSA')
    X_all = lsa.fit_transform(X_all)

    X_train = X_all[:trainLen]
    X_test = X_all[trainLen:]

    X_train = np.hstack((X_train,getFeatureVecture(train)))

    # sampling from the data and fitting indivisual samples linear model and average the results

    final_cv_error = 0
    sampling_count = 10

    kf = cross_validation.KFold(X_train.shape[0],n_folds = 10)
    cols = {'s':['s1','s2','s3','s4','s5'],
        'w':['w1','w2','w3','w4'],
        'k':['k1','k2','k3','k4','k5','k6','k7','k8','k9','k10','k11','k12','k13','k14','k15']}

    col_keys = ['s','w','k']
    final_error = []
    j = 0

    for train_idx,cv_idx in kf:
        j +=1
        print('prcessing CV :',j)
        X_train_kf = X_train[train_idx,:]
        X_cv_kf = X_train[cv_idx,:]

        y_train_kf = train.ix[train_idx,4:]
        y_cv_kf = train.ix[cv_idx,4:]
        #creating a place holder of cv predictions
        pred_cv = pd.DataFrame(np.zeros(y_cv_kf.shape), columns = train.columns[4:])

        error_pred = 100

        for y_col in col_keys:
            #do this for 100s simulation

            print('    Normalizing prediction values')
            y_train_kf_norm = normalize_prob(y_train_kf[cols[y_col]].values)

            pred_sampling = np.zeros((len(cv_idx),sampling_count))

            #print 'sampling for ',y_col,sampling
            sim_data = [np.random.choice(np.arange(len(cols[y_col])),
                                     sampling_count,replace = True,
                                     p = y_train_kf_norm[i,:]) for i in np.arange(len(train_idx))]
            sim_data = np.array(sim_data)
            print('    sim_data shape', sim_data.shape)
            #print sim_data[:10]
            for sampling in range(sampling_count):
                #print 'sim_data', sim_data[sampling]
                clf.fit(X_train_kf,sim_data[:,sampling])
                pred_sampling[:,sampling] = clf.predict(X_cv_kf)
            pred_sampling = pd.DataFrame(pred_sampling)

            print('    processed all the samplings')

            col_index = 0
            for col in cols[y_col]:
                col_val = pred_sampling == col_index
                pred_cv[col] = np.array(col_val.sum(axis = 1), dtype = float)/sampling_count
                col_index +=1
        print('Score CV',j, sqrt(mean_squared_error(y_cv_kf,pred_cv)))

if __name__ == '__main__':
    main()


import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt

import skimage
from skimage import io, filters, feature, morphology, color
from skimage.morphology import disk

from PIL import Image
import warnings
import time
import os.path
import glob
import re

# find all the training images with a filename
# of which matches the pattern train*.jpg, where
# the '*' symbols could represent any character
im_list = glob.glob('../images/train*.jpg')

# sort the filenames which matched the specified path
# this is used for saving the center of ojbects.mat files
im_list.sort(key=lambda f: int(re.findall('([0-9]+)', os.path.basename(f))[0]))

# Read each image file and store them in a list
train_imgs = [io.imread(fpath) for fpath in im_list]

# use the skimage.color.rgb2gray() function to
# convert the multichannel color images to
# single channel grayscale image
train_grey = [color.rgb2gray(im) for im in train_imgs]

# imgray = train_grey[-1]

# Empty list for center coordinates
train_centers = []

# start a timer
start = time.time()

for imgray in train_grey:
    # smooth the image using a rank median filter
    denoised = filters.rank.median(imgray, disk(2))

    # detect contrast differences using Canny edge filtering
    edges = feature.canny(denoised, sigma=1)

    # binary-dilation algorithm to fill groups
    segmented = ndimage.binary_fill_holes(edges)

    label_objects, nb_labels = ndimage.label(segmented)

    # threshold size, then perform essentially an
    sizes = np.bincount(label_objects.ravel())
    mask_sizes = sizes > 40
    mask_sizes[0] = 0

    # logically AND operation with the surviving labels
    cleaned = mask_sizes[label_objects]
    labels, _ = ndimage.label(cleaned)
    labels.min(), labels.max()
    centers = ndimage.measurements.center_of_mass(denoised,labels=labels, index=np.arange(labels.min(),labels.max()))
    train_centers.append(centers)

end = time.time()

# imrgb = train_imgs[-1]
# imrgb[np.logical_not(cleaned),0] = 0
# imrgb[np.logical_not(cleaned),1] = 0
# imrgb[np.logical_not(cleaned),2] = 0
# io.imshow(imrgb)

print('Took {} seconds'.format(end -start))
(end-start)/len(centers)

for fpath, img, centers in zip(im_list, train_imgs, train_centers):

    # split the directory path from the file name
    dirname, fname = os.path.split(fpath)

    # split the file name from the file extention
    fname, _ = os.path.splitext(fname)

    # make a directiory to store the centers
    if not os.path.exists(dirname+'/centers'):
        os.mkdir(dirname+'/centers')
    else:
        print('Over-writing images in centers directory!')

    newf = '{}/centers/{}_centers.jpg'.format(dirname,fname)
    print('Saving "{}"'.format(newf))

    fig = plt.figure()
    plt.imshow(img)
    for c in centers:
        y,x = c
        plt.plot(x,y, 'r.', markersize=14)
    fig.savefig(newf)


# -*- coding:utf-8 -*-
# @Time     : 2019-09-19 10:16
# @Author   : Richardo Mu
# @FILE     : q_18.PY
# @Software : PyCharm
"""
实例018：复读机相加

题目 求s=a+aa+aaa+aaaa+aa…a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)，几个数相加由键盘控制
"""
N = input('please choose number u want')
n = int(input('please choose how many number u want to add'))
sum = 0
for i in range(1,n+1):
    sum += int(N*i)
print(sum)


# %%
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##########################################################################################
# import packages needed
import pandas as pd
import numpy as np
from numpy import mean, sqrt, std, where, diag,sum
import statsmodels.api as sm
import scipy.stats
import qfc_checked
#######################################################################################

# %%
############# get s^2,residual,and cov matrix for continuous phenotype #############
def get_linear_residual(pheno, cov, outtype):
    if len(cov) != 0:
        cov_x = sm.add_constant(cov)
    else:
        cov_x = np.ones(len(pheno))
    lm = sm.OLS(pheno,cov_x).fit()
    s2 = lm.mse_resid
    res = lm.resid
    return s2, res, cov_x

# %%
############# get cov matrix , mu, pi_1, res, res_out(resampling if sample size <2000) dichotomous phenotype #############
def get_logistic_residual(pheno, cov, n_resampling):
    if len(cov) != 0:
        cov_x = sm.add_constant(cov)
    else:
        cov_x = np.ones(len(pheno))
    ####from logistic regression#####
    lm = sm.Logit(pheno,cov_x).fit()
    mu = lm.predict()
    pi_1 = mu*(1-mu)
    res = pheno.values.flatten() - mu
    n1 = len(res)
    n_case = sum(pheno)
    res_out = 0
    ####resampling####
    if n_resampling > 0:
        res_out = np.zeros((n1,n_resampling))
        for i in range(n_resampling):
            res_out1 = np.random.binomial(1,mu,n1)
            res_out2 = np.random.binomial(1,mu,n1)
            id_case1 = where(res_out1==1)
            id_case2 = where(res_out2==1)
            id_c1 = set(id_case1[0]).intersection(id_case2[0]) # intersection
            id_c1 = np.array(list(id_c1))
            set1 = set(id_case1[0]).difference(set(id_case2[0]))
            set2 = set(id_case2[0]).difference(set(id_case1[0]))
            id_c2 = set1.union(set2)
            id_c2 = np.array(list(id_c2))
            if n_case[0] <= len(id_c1):
                id_case = np.random.choice(id_c1, size = n_case[0])
            elif (n_case[0] > len(id_c1)) and n_case[0] <= (len(id_c1) + len(id_c2)):
                id_c3 = np.random.choice(id_c2, size = n_case[0] - len(id_c1),replace = False)
                id_case = np.concatenate((id_c1,id_c3))
            else:
                id_case3 = set(id_c1).union(set(id_c2))
                id_c4 = set(range(n1)).difference(id_case3)
                n_needed = n_case[0] - len(np.array(list(id_case3)))
                id_c4 = np.array(list(id_c4))
                id_c5 = np.random.choice(id_c4, size = n_needed, p = mu[id_c4]/sum(mu[id_c4]))
                id_case = id_case3.union(set(id_c5))
                id_case = np.array(list(id_case))
            res_out[id_case, i] = 1
            res_out [:,i] = res_out [:,i] - mu
    return cov_x, mu, pi_1, res, res_out

# %%
############# get residual (resampling & without resampling) #############
def SKAT_Null_Model_MomentAdjust(pheno, cov, n_resampling):
    re1 = get_logistic_residual(pheno, cov, 0)
    re2 = get_logistic_residual(pheno, cov, n_resampling)
    return (re1, re2)

# %%
############# SKAT Null Model #############
def SKAT_Null_Model(pheno, cov, outtype):
    regdata = pd.concat([pheno, cov], axis=1)
    samplesize = len(regdata.dropna())
    n_resampling = 0
    if outtype == "D":
        if samplesize < 2000:
            n_resampling=10000
            re = SKAT_Null_Model_MomentAdjust(pheno, cov, n_resampling)
        else:
            re = get_logistic_residual(pheno, cov, 0)
    if outtype =="C":
        re = get_linear_residual(pheno, cov, outtype)
    return re, n_resampling

# %%
############# logistic model with adjustment (sample size < 2000) #############
def SKAT_With_NullModel_ADJ(re, weight, geno):
    obj_res = re[0]
    res = obj_res[3]
    res2 = re[1][4]
    X1 = obj_res[0]
    pi_1 = obj_res[2]
    mu = obj_res[1]
    res_moment = res2
    Z1 = weight * geno
    Q1 = (res).dot(Z1)
    Q = Q1.dot(Q1.T) / 2
    Z2 = (sqrt(pi_1) * Z1.T).T - ((sqrt(pi_1) * X1.T).T).dot(np.linalg.inv((X1.T).dot((pi_1 * X1.T).T))).values.dot((X1.T).dot((pi_1 * Z1.T).T).values)
    Q_Temp_res1 = (res_moment.T).dot(Z1)
    Q_sim = pow(Q_Temp_res1, 2).sum(axis = 1) / 2
    p_value = SKAT_PValue_Logistic_VarMatching(Q, Z2 /sqrt(2), mu, Q_sim)
    return p_value

# %%
############# Get_Lambda_U_From_Z #############
def Get_Lambda_U_From_Z(Z):
    out_svd = np.linalg.svd(Z)
    lambda_org = pow(out_svd[1],2)
    IDX = where(lambda_org > mean(lambda_org)/100000)
    lambda_org = pow(out_svd[1],2)
    Lambda = lambda_org[IDX]
    u = out_svd[0]
    U = u[:,IDX[0]]
    return Lambda, U

# %%
############# SKAT_Get_Cov_Param #############
def SKAT_Get_Cov_Param(Lambda,p_all,U):
    p_m = len(Lambda)
    m4 = p_all * (1 - p_all) * (3 * pow(p_all, 2) - 3 * p_all + 1) / pow(p_all * (1 - p_all), 2)
    zeta = np.zeros(len(Lambda))
    var_i = np.zeros(len(Lambda))
    varQ = 0
    for i in range(p_m):
        temp_M1 = pow(sum(pow(U[:,i], 2)), 2) - sum(pow(U[:,i], 4))
        zeta[i] = sum(m4 * pow(U[:,i], 4)) + 3 * temp_M1
        var_i[i] = zeta[i] - 1
    if p_m == 1:
        cov_mat = zeta * pow(Lambda, 2)
    elif p_m > 1:
        cov_mat = np.diag(zeta * pow(Lambda, 2))
        for i in range(p_m - 1):
            for j in range(i+1, p_m):
                cov_mat[i,j] = SKAT_Get_Var_Elements(m4, p_all, U[:,i], U[:,j])
                cov_mat[i,j] = cov_mat[i,j] * Lambda[i] * Lambda[j]

    cov_mat = cov_mat + cov_mat.T
    cov_mat2 = cov_mat
    cov_mat2[np.diag_indices_from(cov_mat2)] = np.diag(cov_mat)/2
    varQ = sum(cov_mat2) - pow(sum(Lambda), 2)
    muQ = sum(Lambda)
    Lambda_new = Lambda * sqrt(var_i) / sqrt(2)
    return zeta, var_i, varQ, muQ, Lambda_new, p_m

# %%
############# SKAT_GET_kurtosis #############
def SKAT_GET_kurtosis(x):
    if std(x) == 0:
        return -100
    m4 = mean(pow(x - mean(x), 4))
    kurt = m4 / pow(std(x), 4) - 3
    return kurt

# %%
############# SKAT_GET_skewness #############
def SKAT_GET_skewness(x):
    m3 = mean(pow(x - mean(x), 3))
    skew = m3 / pow(std(x), 3)
    return skew

# %%
############# SKAT_Get_DF_Sim #############
def SKAT_Get_DF_Sim(Q_sim):
    s2_sim = SKAT_GET_kurtosis(Q_sim)
    df_sim = 12 / s2_sim
    if s2_sim <= 0 :
        df_sim = 100000
    elif df_sim < 0.01 :
        s1_sim = SKAT_GET_skewness(Q_sim)
        df_sim = 8 / pow(s1_sim, 2)
    return df_sim

# %%
############# Get_Liu_Params_Mod #############
def Get_Liu_Params_Mod(c1):
    muQ = c1[0]
    sigmaQ = sqrt(2 * c1[1])
    s1 = c1[2] / pow(c1[1], 3/2)
    s2 = c1[3] / pow(c1[1], 2)
    if pow(s1, 2) > s2:
        a = 1 / (s1 - sqrt(pow(s1, 2) - s2))
        d = s1 *pow(a, 3) - pow(a, 2)
        l = pow(a, 2) - 2*d
    else:
        l = 1/s2
        a = sqrt(l)
        d = 0
    muX = l + d
    sigmaX = sqrt(2) * a
    return l, d, muQ, muX, sigmaQ, sigmaX

# %%
############# SKAT_Logistic_VarMatching_GetParam #############
def SKAT_Logistic_VarMatching_GetParam(Lambda, U, p_all, Q_sim):
    re = SKAT_Get_Cov_Param(Lambda, p_all, U)
    Lambda_new = re[4]
    muQ = re[3]
    varQ = re[2]
    df = SKAT_Get_DF_Sim(Q_sim)
    c1 = np.zeros(4)
    for i in range(4):
        c1[i] = sum(pow(Lambda, i + 1))
    para = Get_Liu_Params_Mod(c1)
    n_Lambda = len(Lambda_new)
    return muQ, varQ, df, Lambda_new, para, n_Lambda

# %%
############# SKAT_Logistic_VarMatching_GetParam1 #############
def SKAT_Logistic_VarMatching_GetParam1(Z, p_all, Q_sim):
    out_svd = Get_Lambda_U_From_Z(Z)
    Lambda = out_svd[0]
    U = out_svd[1]
    para = SKAT_Logistic_VarMatching_GetParam(Lambda, U, p_all, Q_sim)
    return para

# %%
############# SKAT_Get_Var_Elements #############
def SKAT_Get_Var_Elements(m4,p_all,u1,u2):
    temp1 = pow(u1, 2) * pow(u2, 2)
    a1 = sum(m4 * temp1)
    a2 = sum(pow(u1, 2))*sum(pow(u2, 2)) - sum(temp1)
    a3 = pow(sum(u1 * u2), 2) - sum(temp1)
    a3 = a3 * 2
    return (a1 + a2 + a3)

# %%
############# SKAT_PValue_Logistic_VarMatching (get p_value and p_value without adjustment) logistic #############
def SKAT_PValue_Logistic_VarMatching(Q, Z, p_all, Q_sim):
    para = SKAT_Logistic_VarMatching_GetParam1(Z, p_all, Q_sim)
    if para[1] == 0:
        p_value = np.ones(1, len(Q))
    param = para[4]
    Q_norm = (Q - para[0]) / sqrt(para[1])
    Q_norm1 = Q_norm * sqrt(2 * para[2]) + para[2]
    p_value =  scipy.stats.distributions.chi2.sf(x = Q_norm1, df = para[2])
    if len(param) > 1:
        if param[4] == 0:
            p_value_noadj = np.ones(1, len(Q))
        else:
            Q_norm = (Q - param[2]) / param[4]
            Q_norm1 = Q_norm * param[5] + param[3]
            p_value_noadj =  scipy.stats.distributions.chi2.sf(x = Q_norm1, df = param[0])
    return p_value, p_value_noadj

# %%
############# SKAT logistic Model(get Q,W) ################
def SKAT_Logistic_Model(geno, weights, re):
    X = re[0]
    mu = re[1]
    pi_1 = re[2]
    res = re[3]
    Z1 = weights * geno
    Q1 = (res.T).dot(Z1)
    Q = Q1.dot(Q1.T) / 2
    W = (Z1.T).dot((pi_1 * Z1.T).T.values) - (pi_1 * Z1.T).dot(X).dot(np.linalg.inv((X.T).dot((pi_1 * X.T).T.values))).dot((X.T).values.dot((pi_1 * Z1.T).T.values))
    return Q,W

# %%
############# SKAT Linear Model(get Q,W) ################
def SKAT_Linear_Model(geno, weights, re):
    s2 = re[0]
    res = re[1]
    X = re[2]
    Z1 = weights * geno
    Q1 = (res.T).dot(Z1)
    Q = Q1.dot(Q1.T) / s2 / 2
    W = (Z1.T).dot(Z1)  - ((Z1.T).dot(X)).dot(np.linalg.inv((X.T).dot(X))).dot(((X.T).dot(Z1)))
    return Q,W

# %%
############# Get_Lambda #############
def Get_Lambda(K):
    lambda1 = np.linalg.eigvalsh(K, UPLO = 'L')
    a = mean(lambda1[lambda1 >= 0] / 100000)
    lambda2 = lambda1[lambda1 > a]
    #len(lambda2)==0, stop?
    return lambda2

# %%
############# Get_Liu_Params_Mod_Lambda (function from Get_Liu_PVal_MOD_Lambda) #############
def Get_Liu_Params_Mod_Lambda(Lambda):
    c1 = np.zeros(shape = (4))
    for i in range (0,4):
        c1[i] = sum(pow(Lambda, i + 1))
    muQ = c1[0]
    sigmaQ = sqrt(2 * c1[1])
    s1 = c1[2] / (pow(c1[1], 3 / 2))
    s2 = c1[3] / (pow(c1[1], 2))
    if pow(s1, 2) > s2:
        a = 1 / (s1 - sqrt(pow(s1,2) - s2))
        d = s1 * pow(a, 3) - pow(a, 2)
        l = pow(a, 2) - 2 * d
    else:
        l = 1 / s2
        a = sqrt(l)
        d = 0
    muX = l + d
    sigmaX = sqrt(2) * a
    return l, d, muQ, muX, sigmaQ, sigmaX

# %%
############# Get_Liu_PVal_MOD_Lambda (get_pvalue) #############
def Get_Liu_PVal_MOD_Lambda(Q,Lambda):
    l, d, muQ, muX, sigmaQ, sigmaX = Get_Liu_Params_Mod_Lambda(Lambda)
    Q_Norm = (Q - muQ) / sigmaQ
    Q_Norm1 = Q_Norm * sigmaX + muX
    p_value = scipy.stats.distributions.chi2.sf(x = Q_Norm1, df = l)
    return p_value

# %%
############# SKAT_davies method to get p_value #############
def SKAT_davies(Q, Lambda):
    nc = np.zeros(len(Lambda))
    n = np.ones(len(Lambda))
    r = len(Lambda) # int r1
    sigma = 0 # double sigma
    lim = 10000 # int lim1
    acc = pow(10,-6) # double acc
    trace = np.zeros(7) # double trace
    ifault = 0 # int ifault
    res = 0 # double
    trace, res, ifault = qfc_checked.qfc(Lambda, nc, n, r, sigma, Q, lim, acc, trace, ifault, res)
    return trace, res, ifault

# %%
############# caculate p_value #############
def Get_PValue_Lambda(Lambda,Q):
    p_val_liu = Get_Liu_PVal_MOD_Lambda(Q, Lambda)
    trace, res, ifault = SKAT_davies(Q, Lambda)
    p_val = 1 - res
    if len(Lambda) == 1:
        p_val = p_val_liu
    if p_val > 1 or p_val <= 0:
        p_val = p_val_liu
    return p_val

# %%
############# combine to get p_value #############
def Get_pvalue(W, Q):
    K = W / 2
    Lambda = Get_Lambda(K)
    p_val = Get_PValue_Lambda(Lambda,Q)
    return p_val

# %%
#############  phenotype, cov, genotype with samples without missing  #############
def missingcheck(geno, pheno, cov):
    regdata = pd.concat([pheno, cov], axis=1)
    regdata["id"] = range(len(regdata))
    regdata_nona = regdata.dropna()
    if len(regdata) != len(regdata_nona):
        keep_col = regdata_nona["id"]
        geno_mat_nona = geno[keep_col,:]
        pheno_nona = regdata_nona[pheno.columns]
        cov_nona = regdata_nona[cov.columns]
        return geno_mat_nona, pheno_nona, cov_nona
    else:
        return geno, pheno, cov

# %%
############# SKAT method main function #############
def SKAT(geno, pheno, cov, weights, outtype):
    geno, pheno, cov = missingcheck(geno, pheno, cov)
    if outtype == "C":
        re, n_resampling = SKAT_Null_Model(pheno, cov, outtype)
        Q,W = SKAT_Linear_Model(geno, weights, re)
        p_val = Get_pvalue(W, Q)
    if outtype =="D":
        re, n_resampling = SKAT_Null_Model(pheno, cov, outtype)
        if n_resampling > 0:
            p_val = SKAT_With_NullModel_ADJ(re, weights, geno)
        else:
            Q,W = SKAT_Logistic_Model(geno, weights, re)
            p_val = Get_pvalue(W, Q)
    return p_val

# %%
############# SKAT method for summary statistics #############
def SKAT_summary(beta_var, beta_estimate, weight, sample_size, COV, D):
    weight_square = pow(weight,2)
    y_estimate = np.zeros(len(weight))
###estimate y'y
    for i in range(len(weight)):
        y_estimate[i] = (sample_size-1) *D[i]*beta_var[i]*(sample_size-1)+ D[i]*pow(beta_estimate[i],2)*(sample_size-1)
###get median as y'y and variance of y
    y_square_estimate =np.median(y_estimate)
    y_estimate_var =y_square_estimate/(sample_size-1)
###get W
    W = (1/y_estimate_var)*(sample_size-1)*diag(weight).dot(COV).dot(diag(weight))
###get G'y
    res_geno_temp = diag(D*sample_size).dot(beta_estimate)
    res_geno = res_geno_temp/y_estimate_var
    square_res_geno = pow(res_geno,2)
    Q = square_res_geno.dot(weight_square)
    p_val = Get_pvalue(2 * W, Q)
    return p_val

# %%
############# SKAT method for summary statistics #############
def SKAT_summary(beta_var, beta_estimate, weight, sample_size, COV, D):
    weight_square = pow(weight,2)
    y_estimate = np.zeros(len(weight))
###estimate y'y
    for i in range(len(weight)):
        y_estimate[i] = (sample_size-1) *D[i]*beta_var[i]*(sample_size-1)+ D[i]*pow(beta_estimate[i],2)*(sample_size-1)
###get median as y'y and variance of y
    y_square_estimate =np.median(y_estimate)
    y_estimate_var =y_square_estimate/(sample_size-1)
###get W
    W = (1/y_estimate_var)*(sample_size-1)*diag(weight).dot(COV).dot(diag(weight))
###get G'y
    res_geno_temp = diag(D*sample_size).dot(beta_estimate)
    res_geno = res_geno_temp/y_estimate_var
    square_res_geno = pow(res_geno,2)
    Q = square_res_geno.dot(weight_square)
    p_val = Get_pvalue(2 * W, Q)
    return p_val
# %%
############# SKAT method for summary statistics #############
def SKAT_summary_gety(beta_var,beta_estimate, sample_size, COV, D):
    y_estimate = np.zeros(len(beta_var))
###estimate y'y
    for i in range(len(beta_var)):
        y_estimate[i] = (sample_size-1) *D[i]*beta_var[i]*(sample_size-1)+ D[i]*pow(beta_estimate[i],2)*(sample_size-1)
###get median as y'y and variance of y
    y_square_estimate =np.median(y_estimate)
    return y_square_estimate

# %%
############# SKAT method for summary statistics #############
def SKAT_summary_withy(y_est,beta_estimate, weight, sample_size, COV, D):
    weight_square = pow(weight,2)
###get median as y'y and variance of y
    y_estimate_var =y_est/(sample_size-1)
###get W
    W = (1/y_estimate_var)*(sample_size-1)*diag(weight).dot(COV).dot(diag(weight))
###get G'y
    res_geno_temp = diag(D*sample_size).dot(beta_estimate)
    res_geno = res_geno_temp/y_estimate_var
    square_res_geno = pow(res_geno,2)
    Q = square_res_geno.dot(weight_square)
    p_val = Get_pvalue(2 * W, Q)
    return p_val


######Lab 15
######06.10.2020
######
class Node:
    def __init__(self, data, next = None):
        self.__data = data
        self.__next = next

    def get_data(self):
        return self.__data

    def get_next(self):
        return self.__next

    def set_data(self, new_data):
        self.__data = new_data

    def set_next(self, new_next):
        self.__next = new_next

    def __str__(self):
        return str(self.__data)

    ##Ex 2
    def add_after(self, value):
        new_node = Node(value)
        temp = self.__next
        self.__next = new_node
        new_node.set_next(temp)

     ##Ex 3
    def remove_after(self):
        temp = self.__next
        self.__next = temp.__next

    ##Ex 4
    def __contains__(self, value):
        temp = self.__next

        if self.__data == value:
            return True

        while temp != None:
            if temp.__data == value:
                return True
            temp = temp.__next

    ##Ex 5
    def get_sum(self):
        count = 0
        temp = self

        while temp != None:
            count += temp.__data
            temp = temp.__next

        return count

######Lab 15
##Ex 1
class LinkedList:
    def __init__(self):
        self.__head = Node(None)
        self.__count = 0
        self.__r_count = 0

    def add(self, value):
        new_node = Node(value)
        new_node.set_next(self.__head)
        self.__head = new_node
        self.__count += 1

    def size(self):
        return self.__count

    def get_head(self):
        return self.__head

##Ex 2
    def clear(self): ####Question
        self.__count = 0
        return self.__count

    def is_empty(self):
        if self.__count == 0:
            return True
        return False

    def __len__(self):
        return self.__count

##Ex 3
    def __str__(self):
        result = "["
        current = self.__head
        while current.get_next():
            result += str(current) + ", "
            current = current.get_next()

        result += "]"
        result = result[:-3] + result[-1]

        if self.__count == 0:
            return "[]"

        if self.__r_count != 0:
            number = result[1::3]
            number = number[::-1]
            result = result[0] + ", ".join(number) + result[-1]
            return result

        return result

##    def __str__(self):
##        node = []
##        current = self.__head
##        while current != None:
##            node.append(str(current.get_data()))
##            current = current.get_next()
##        return '[' + ', '.join(node) + ']'

    ##Ex 4
    def __contains__(self, search_value):
        current_node = self.__head

        if current_node.get_data() == search_value:
            return True
        else:
            current_node = current_node.get_next()

        while current_node != None:
            if current_node.get_data() == search_value:
                return True
            else:
                current_node = current_node.get_next()

        return False

    ##Ex 5
    def __getitem__(self, index):
        current = self.__head
        list_index = 0
        while current != None:
            if list_index == index:
                return current.get_data()
            list_index += 1
            current = current.get_next()

    ##Ex 6
    def add_all(self, a_list):
        index = 0
        while len(a_list) != index:
            new_node = Node(a_list[index])
            new_node.set_next(self.__head)
            self.__head = new_node
            self.__count += 1
            index += 1

    ##Ex 7
    def get_min_odd(self):
        current = self.__head
        count = 0
        current_best = 1
        while current.get_next() != None:
            data = current.get_data()

            if data % 2 != 0:
                if current_best >= data:
                    current_best = data
                count += 1

            current = current.get_next()

        if count == 0:
            return 999
        else:
            return current_best

    ##Ex 8
##    def reverse(self):
##        previous = None
##        current = self.__head
##        following_next = current.get_next()
##        while current != None:
##            current.get_next = previous
##            previous = current
##            current = following_next
##            if current.get_next != None:
##                following_next = following_next.get_next()
##
##        self.__head = previous

    def reverse(self):
        self.__r_count += 1
        return self.__r_count

values = LinkedList()
values.add(1)
values.add(2)
values.add(3)
print(values)
values.reverse()
print(values)

##Ex 9
class SquareNumber:
    def __init__(self, count=5):
        self.__count = count

    def __iter__(self):
        return SquareNumberIterator(self.__count)

class SquareNumberIterator:
    def __init__(self, count = 5):
        self.__count = count
        self.__current = 1

    def __current(self):
        return self.__current

    def __count(self):
        return self.__count

    def __next__(self):
        if self.__count < self.__current:
            raise StopIteration
        else:
            square = self.__current ** 2
            self.__current += 1
            return square

##Ex 10
class LinkedListIterator:
    def __init__(self, head):
        self.__head = head
        self.__current = self.__head

    def __next__(self):
        if self.__current == None:
            raise StopIteration
        else:
            data = self.__current.get_data()
            self.__current = self.__current.get_next()
            return data


import re

import time
from distutils.command import register

from django.contrib.auth.decorators import login_required

from django.shortcuts import render, redirect
from django.contrib import auth

from account.forms import SlidesForm
from account.models import Presentation, PresentationCopy
from google_api.drive import files_export_media
from project_teta import settings
import hashlib
from django.core.files import File
from django.template.defaulttags import register
import urllib
import json

@register.filter
def get_item0(dictionary, key):
    val = dictionary.get(key)
    if val:
        return int(val[0])
    return 0
@register.filter
def get_item1(dictionary, key):
    val = dictionary.get(key)
    if val:
        return int(val[1])
    return 0
#

# Create your views here.

@login_required
def account_info(request):
    presentations = Presentation.objects.filter(user=auth.get_user(request))
    return render(request, 'account.html', locals())

@login_required
def presentation_info(request, id=0):
    id = int(id)
    link = ""
    if id <= 0:
        return redirect('/account/presentations/')
    presentation = Presentation.objects.get(id=id)

    status = ""
    if presentation.is_active:
        status = "Close"
    else:
        status = "Open"

    if presentation is None:
        return redirect('/account/presentations/')

    if request.POST.get('status'):
        presentation.is_active = not presentation.is_active
        presentation.save()
        if presentation.is_active:
            status = "Close"
        else:
            status = "Open"
    link = ""

    if request.POST.get('add_link'):
        pcopy = PresentationCopy()
        pcopy.origin = presentation
        pcopy.name = request.POST['name']
        magic = pcopy.name + str(time.time())
        pcopy.hash = hashlib.md5(magic.encode()).hexdigest()
        pcopy.save()

    if request.POST.get('delete'):
        idc = request.POST['copy_id']
        obj = PresentationCopy.objects.get(id=idc)
        PresentationCopy.delete(obj)

    api_url = 'https://api-metrika.yandex.ru/stat/v1/data?'
    counter_id = "id=47725444"
    metrics = '&metrics=ym:pv:pageviews,ym:pv:users'
    dimensions = '&dimensions=ym:pv:URLPath'
    auth_token = '&oauth_token=AQAAAAAOGSvpAATQtPCjgBAfGU1wobj5hSPXApg'
    filter_url = '&filters=ym:pv:URLPath=~%27/show/' + str(presentation.id) + '/[0-9]%2B/*%27'
    request_url = api_url + counter_id + metrics + dimensions + filter_url + auth_token
    print(request_url)
    f = urllib.request.urlopen(request_url)
    ans = f.read()
    json_ans = json.loads(ans)
    data = json_ans['data']

    view_stat = {}

    for item in data:
        path = item['dimensions'][0]['name'].strip().split('/')
        view_stat[int(path[3])] = item['metrics']

    print(view_stat)

    copys = PresentationCopy.objects.filter(origin=presentation)
    return render(request, 'presentation.html', locals())

@login_required
def add_presentation(request):
    form = SlidesForm(request.GET)
    if request.POST:
        newslidesform = SlidesForm(request.POST, request.FILES)
        if newslidesform.is_valid():
            presentation = Presentation()
            presentation.name = newslidesform["name"].value()
            presentation.description = newslidesform["description"].value()
            presentation.url = newslidesform["url"].value()
            presentation.user = auth.get_user(request)
            file_url = presentation.url
            start = file_url.find("/d/") + 3
            end = file_url.find("/edit")
            presentation.presentationId = file_url[start:end]
            pdf_s = newslidesform['pdf'].value()
            f = None
            if pdf_s is None:
                fh = files_export_media(presentation.presentationId, 'application/pdf')
                f = open(settings.MEDIA_URL + "user_files/" + presentation.presentationId + ".pdf", 'wb')
                f.write(fh.getvalue())
                f.close()
                f = open(settings.MEDIA_URL + "user_files/" + presentation.presentationId + ".pdf", 'rb')
                pdf_s = File(f)

            # Assumes the default UTF-8
            hash = hashlib.md5(presentation.name.encode()).hexdigest()
            presentation.hash = hash
            presentation.pdf.save(presentation.presentationId + ".pdf", pdf_s)
            # Presentation.objects.create(name=presentation.name, description=presentation.description,
            #                             url=presentation.url,
            #                             presentationId=presentation.presentationId, pdf=pdf_s, user=presentation.user,
            #                             hash=hash)
            if f is not None:
                f.close()
            return redirect('/account/')
        else:
            form = SlidesForm(request.POST)

    return render(request, 'upload_presentation.html', locals())


from django.shortcuts import render, render_to_response
from django.views.decorators.csrf import csrf_exempt,csrf_protect,ensure_csrf_cookie
from .models import Domain
# Create your views here.

def logout(request):
    return render_to_response('logout.html')

@ensure_csrf_cookie
@csrf_exempt
def ansviews(request, template_name='mypage.html'):
    if request.method == "POST":  # checking POST request
        name = request.POST.get('name', '')  # get the input from front end text box
        favcolor = request.POST.get('favcolor', '')
        pet = request.POST.get('pet', '')
        print(name, favcolor, pet)
        Domain.objects.create(name=name, favcolor=favcolor, pet=pet)
        context = {
            'name': name,
            'favcolor': favcolor,
            'pet': pet
        }
        return render(request, template_name, context)
    else:
        print("GET Type")
        return render(request, template_name)


# -*- coding: utf-8 -*-
import os
from setuptools import setup, find_packages

here = os.path.abspath(os.path.dirname(__file__))

with open(os.path.join(here, 'README.rst')) as f:
    README = f.read()

setup(name='rawgithub',
      version='0.1',
      description='rawgithub',
      long_description=README,
      classifiers=[
          "Programming Language :: Python",
          "Framework :: Pylons",
          "Topic :: Internet :: WWW/HTTP",
          "Topic :: Internet :: WWW/HTTP :: WSGI :: Application"
      ],
      keywords="web services",
      author=u'Rémy HUBSCHER',
      author_email='hubscher.remy@gmail.com',
      url='https://github.com/diecutter/rawgithub',
      packages=find_packages(),
      include_package_data=True,
      zip_safe=False,
      install_requires=['cornice', 'waitress', 'requests'],
      entry_points={
          'paste.app_factory': [
              'main = rawgithub:main',
          ]
      },
      paster_plugins=['pyramid'])


import pynupack as nu # renamed the nupack directory from multistrand, slightly modified, used elsewhere
import random
import numpy as np
from Bio.Seq import Seq
import matplotlib.pyplot as plt
from Bio.SeqUtils import MeltingTemp as mt
# from Bio import AlignIO
# from Bio.Align import MultipleSeqAlignment
# from Bio import AlignIO
# from Bio import pairwise2
# from Bio.SeqRecord import SeqRecord
# from Bio.Align.Applications import MuscleCommandline
# from Bio.Alphabet import IUPAC
# from Bio.Align import AlignInfo
# from Bio.pairwise2 import format_alignment
# from Bio import SeqIO
# import Levenshtein

def get_base_sampling_key(pA = 0.3,pT = 0.3,pC = 0.2,pG = 0.2):
    base_probabilities = {"A":pA, "T":pT, "C":pC, "G":pG}
    pdistGTC =np.array([base_probabilities["G"],base_probabilities["T"],base_probabilities["C"]])/np.sum([base_probabilities["G"],base_probabilities["T"],base_probabilities["C"]])
    pdistATC =np.array([base_probabilities["A"],base_probabilities["T"],base_probabilities["C"]])/np.sum([base_probabilities["A"],base_probabilities["T"],base_probabilities["C"]])
    pdistAGC =np.array([base_probabilities["A"],base_probabilities["G"],base_probabilities["C"]])/np.sum([base_probabilities["A"],base_probabilities["G"],base_probabilities["C"]])
    pdistAGT =np.array([base_probabilities["A"],base_probabilities["G"],base_probabilities["T"]])/np.sum([base_probabilities["A"],base_probabilities["G"],base_probabilities["T"]])
    base_sampling_key = { "A":[["G","T","C"],pdistGTC],"G":[["A","T","C"],pdistATC],
                                "T":[["A","G","C"],pdistAGC],"C":[["A","G","T"],pdistAGT]}
    return base_sampling_key

def longest(s):
    maximum = count = 0
    current = ''
    for c in s:
        if c == current:
            count += 1
        else:
            count = 1
            current = c
        maximum = max(count,maximum)
    return maximum

def random_seq_of_length(length):
    seq = ''.join(random.choice("ATGC") for _ in range(length))
    return seq

def mutate(sequence, base_sampling_key):
    base = random.randrange(len(sequence))
    new_base = np.random.choice(base_sampling_key[sequence[base]][0],p=base_sampling_key[sequence[base]][1])
    new_sequence = sequence[0:base] + new_base + sequence[base+1:]
#     print(new_sequence)
#     print(sequence)
    return new_sequence

def get_most_represented_base_freq(sequence):
    length1 = int(len(sequence)/2)
    freqA1 = float(sequence[0:length1].count("A"))/length1
    freqG1 = float(sequence[0:length1].count("G"))/length1
    freqC1 = float(sequence[0:length1].count("C"))/length1
    freqT1 = float(sequence[0:length1].count("T"))/length1
    max_freq1 = np.max([freqA1, freqG1, freqC1, freqT1])

    freqA2 = float(sequence[length1:].count("A"))/length1
    freqG2 = float(sequence[length1:].count("G"))/length1
    freqC2 = float(sequence[length1:].count("C"))/length1
    freqT2 = float(sequence[length1:].count("T"))/length1
    max_freq2 = np.max([freqA2, freqG2, freqC2, freqT2])
    penalty = (1+(max_freq1-0.25))*1+(max_freq2-0.25)
    return penalty

# def tm():
# for seq in inp.values():
#     N = len(seq)
#     dA = seq.count("A")
#     dG = seq.count("G")
#     dC = seq.count("C")
#     dT = seq.count("T")
#     GC = float(dG+dC)/float(N) #in percent %
#     print(GC)
#     cation = sodium + 120*np.sqrt(magnesium - dNTPs)
#     owen_Tm = 87.16 + 0.345*GC + np.log(cation)*(20.17 - 0.066*GC) - .75*percentDMSO

#     print(owen_Tm)

def prime(sequence):
    complement = Seq(sequence).reverse_complement().__str__()
    return complement

def get_complements_list(domains):
    comp_list = []
    for dom in domains:
        comp_list += [Seq(dom).reverse_complement().__str__()]
    return comp_list

def get_interaction_score(domains, domains_c,T=25,magnesium=0, sodium=1.0):
    e_mat_domains = get_energy_matrix(domains, domains_c, T = T, magnesium = magnesium, sodium=sodium)
    target_duplex_energies = np.diag(e_mat_domains)
    cross_reactions = e_mat_domains-np.eye(len(domains))*target_duplex_energies
    problematic_row = np.argmax(np.sum(-1*cross_reactions,axis=0),axis=0)
    cross_reaction_score = np.sum(cross_reactions)
    target_reaction_score = np.sum(target_duplex_energies)
    master_score = target_reaction_score-cross_reaction_score
    return master_score,problematic_row

def get_quick_interaction_score(domains, domains_c,row,T=25,magnesium=0, sodium=1.0, tm_target = 60):
    no_domains = len(domains)
    no_domains_c = len(domains_c)
    e_vec_domains = np.zeros((no_domains))
    for i in range(0,no_domains):
        e_vec_domains[i] = get_duplex_energy([domains[row], domains_c[i]], T = T, magnesium = magnesium, sodium=sodium)
    target_score = e_vec_domains[row]
    cross_reactions = np.sum(np.delete(e_vec_domains, row))
    tm_wallace = mt.Tm_Wallace(domains[row])
    tm_penalty = 1+np.abs(tm_target-tm_wallace)
    quick_score = target_score-cross_reactions*longest(domains[row])*get_most_represented_base_freq(domains[row])*tm_penalty
    return quick_score

def get_energy_matrix(domains, domains_c,T=25,magnesium=0, sodium=1.0):
    no_domains = len(domains)
    no_domains_c = len(domains_c)
    e_mat_domains = np.zeros((no_domains,no_domains_c))
    for i in range(0,no_domains):
        for j in range(0, no_domains_c):
            e_mat_domains[i,j] = get_duplex_energy([domains[i], domains_c[j]], T = T, magnesium = magnesium, sodium=sodium)
    return e_mat_domains

def optimize_domains_quick(input_domains_dict,fixed_domains_dict, iterations, algorithm_reversion_probability = 0.1,plot_interval=20,file_prefix="", T = 25, magnesium = 0.0, sodium=1.0):
    base_sampling_key = get_base_sampling_key(pA = .25, pT = .25, pC =.25, pG = .25)
    input_domains = [seq for seq,temp in input_domains_dict.values()]
    fixed_domains = fixed_domains_dict.values()
    tms = [temp for seq,temp in input_domains_dict.values()]
    domains = input_domains+fixed_domains+get_complements_list(input_domains+fixed_domains)
    scores = np.zeros((np.round(iterations/plot_interval)))
    random_starting_domain =random.choice(range(0,len(input_domains)))
    for iteration in range(0,iterations):

        number_of_mutations = 1
        for mutation in range(0,number_of_mutations):
            domain_pick = (random_starting_domain + iteration)%len(input_domains) #problematic_row #random.choice(range(0,len(input_domains)))
            initial_score = get_quick_interaction_score(domains, get_complements_list(domains), row=domain_pick, T = T, magnesium = magnesium, sodium=sodium, tm_target = tms[domain_pick])
            proposed_new_domain = mutate(domains[domain_pick],base_sampling_key)
            if domain_pick == 0:
                proposed_new_input_domains = [proposed_new_domain]+domains[1:len(input_domains)]

            else:
                proposed_new_input_domains = domains[0:domain_pick]+[proposed_new_domain]+domains[domain_pick+1:len(input_domains)]
            proposed_new_domains = proposed_new_input_domains+fixed_domains+get_complements_list(proposed_new_input_domains+fixed_domains)
            proposed_score = get_quick_interaction_score(proposed_new_domains, get_complements_list(proposed_new_domains),row=domain_pick, T = T, magnesium = magnesium, sodium=sodium, tm_target = tms[domain_pick])
        if proposed_score < initial_score or random.random() < algorithm_reversion_probability:

            domains = [] + proposed_new_domains
        print(iteration, initial_score, proposed_score)
        if iteration%plot_interval == 0 or iteration == 0:
            print("new variable domains: ", domains[0:len(input_domains)])
            plot_energy_matrix(domains,name= file_prefix+"energy_matrix"+str(int(iteration/plot_interval))+".svg" )
#             print(iteration, scores)
#             scores[int(iteration/plot_interval)],problematic_row = get_interaction_score(domains, get_complements_list(domains), T = T, magnesium = magnesium, sodium=sodium)
    new_variable_domain_dict = dict(zip(input_domains_dict.keys(), zip(domains[0:len(input_domains)], tms)   ))
    return new_variable_domain_dict

def plot_energy_matrix(domains, name):
    no_domains = len(domains)
    no_domains_c = len(get_complements_list(domains))
    fig, ax1 = plt.subplots(1, 1,figsize=(20,20))
    ax1.set_yticks(np.arange(0,no_domains)+.5)

    ax1.imshow(get_energy_matrix(domains,get_complements_list(domains)),cmap="brg_r")
    ax1.set_yticklabels([x for x in domains],rotation=0)
    ax1.set_xticks(np.arange(0,no_domains)+.5)
    ax1.set_xticklabels([x for x in get_complements_list(domains)],rotation=90)
    ax1.set_ylim(len(domains),0)
    ax1.set_xlim(len(domains),0)
    mesh = ax1.pcolormesh(get_energy_matrix(domains,get_complements_list(domains)),cmap="brg_r")
    cbar = fig.colorbar(mesh)
    cbar.set_label('$\Delta$ G', rotation=270)
    plt.savefig(name)
    plt.close()

def get_duplex_energy(sequences, material = 'dna',ordering = None, dangles = 'some',
    T = 25,multi = False ,pseudo = False,sodium = 1.0,magnesium = 0.0 ,degenerate = False, full_output = False):
    #### compute annealed structures of hairpin strands ####
    args, cmd_input = \
    nu.setup_nupack_input(exec_name = 'mfe', sequences = sequences, ordering = ordering,
                       material = material, sodium = sodium, magnesium = magnesium,
                       dangles = dangles, T = T, multi = multi, pseudo = pseudo)
    output = nu.call_with_file(args, cmd_input, '.mfe')
    cleaned_output = list(enumerate(output))[0][1]
    try:
        duplex_energy = float(cleaned_output[14].replace("\n", ""))
        if full_output == True:
            print(duplex_energy, cleaned_output[15].replace("\n", ""), sequences)
    except:
        duplex_energy = 0
    return duplex_energy


from flask import Flask
from flask_ask import Ask, statement, question, session
import json
import requests
import bs4

app = Flask(__name__)
ask = Ask(app, "/techcrunch_reader")

def get_headlines():
    res = requests.get('https://techcrunch.com/')
    res.raise_for_status()
    techcrunch_site = bs4.BeautifulSoup(res.text)
    elements = techcrunch_site.select('.block-title')
    elements2 = techcrunch_site.select('.plain-title')

    headlines = ''
    for i in range (0, len(elements)):
        headlines += elements[i].getText()

    for i in range (0, len(elements2)):
        headlines += elements2[i].getText()

    return headlines

@app.route('/')
def homepage():
    homepage_message = "Welcome"
    return homepage_message

@ask.launch
def start_skill():
    welcome_message = "Hi, would you like to know the current Techcrunch headlines?"
    return question(welcome_message)

@ask.intent("YesIntent")
def share_headlines():
    techcrunch_headlines = get_headlines()
    techcrunch_headines_msg = "Great! The current news are " + techcrunch_headlines
    return statement(techcrunch_headines_msg)

@ask.intent("NoIntent")
def no_intent():
    closing_message = "Well, guess I will talk to you late"
    return statement(closing_message)

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


# Robert Tessnow-Ramirez
# October 17, 2016 ©
# Assignment 3
# The Pseudocode
#   Start the Program
# make a dictionary for nine Tampa Bay Rays (see below). You can make up the player names. These are old.
# Use the player names as keys and a list for each value. See page 374.
# Each value list should hold the position played by the player, the batting order, and current batting average.
# When the dictionary is complete, use a for loop to display the dictionary keys and values.
# Next, use loop(s) to print the "lineup" (the dictionary in batting order).
# Duplicate the sample output for full points.
# Pull Rivera and DeJesus. Substitute Wilson as catcher and Beckham as DH (see sample output).
# Reprint the new lineup.
# SAMPLE OUTPUT
# Rays starters
#
# DeJesus ['DH', 6, 299]
# Loney ['1B', 4, 222]
# Rivera ['C', 9, 194]
# Forsythe ['2B', 5, 304]
# Souza Jr ['RF', 2, 229]
# Longoria ['3B', 3, 282]
# Cabrera ['SS', 7, 214]
# Kiermaier ['CF', 1, 240]
# Guyer ['LF', 8, 274]
#
# Today's lineup
#
# Batting 1 : CF Kiermaier ,current avg: 240
# Batting 2 : RF Souza Jr ,current avg: 229
# Batting 3 : 3B Longoria ,current avg: 282
# Batting 4 : 1B Loney ,current avg: 222
# Batting 5 : 2B Forsythe ,current avg: 304
# Batting 6 : DH DeJesus ,current avg: 299
# Batting 7 : SS Cabrera ,current avg: 214
# Batting 8 : LF Guyer ,current avg: 274
# Batting 9 : C Rivera ,current avg: 194
#
# Lineup changed
#
# Batting 1 : CF Kiermaier ,current avg: 240
# Batting 2 : RF Souza Jr ,current avg: 229
# Batting 3 : 3B Longoria ,current avg: 282
# Batting 4 : 1B Loney ,current avg: 222
# Batting 5 : 2B Forsythe ,current avg: 304
# Batting 6 : DH Beckham ,current avg: 200
# Batting 7 : SS Cabrera ,current avg: 214
# Batting 8 : LF Guyer ,current avg: 274
# Batting 9 : C Wilson ,current avg: 152
#   End Program
import random
def main():  ### Define main function

    print('Rays starters\n')
    rays_players = {'Dejesus': ['DH', 6, 299],  ##create dictionary key and list values
                    'Loney': ['1B', 4, 222],
                    'Rivera': ['C', 9, 194],
                    'Forsythe': ['2B', 5, 304],
                    'Souza Jr': ['RF', 2, 229],
                    'Longoria': ['3B', 3, 282],
                    'Cabrera': ['SS', 7, 214],
                    'Kiermaier': ['CF', 1, 240],
                    'Guyer': ['LF', 8, 274]}
    for player_name, player_attribute in sorted(rays_players.items()):  ## display sorted key and value
        print(player_name, player_attribute)
    #################################################################################################
                                                                        #uses nested list
    print('\nToday\'s lineup\n')
    # sorted_list = [(key,value) for value,key in sorted([(value[1],key) for key,value in rays_players.items()])]
    # for key, value in sorted_list:
    #     print('Batting ', value,': ', rays_players[key][0], key,',current avg',rays_players[key][2])
    #################################################################################################
    # Here is a method that works...
    # start a for loop that will run from 1 to 9 inclusive
    # inside that loop, start another loop that uses the items method
    #  (like line 4 in the code atop page 379)
    # in that loop, test to see if the batting order matches the control variable of the outer loop
    # if it does, print
    ctrl_vari = 0
    for i in range(9):
        ctrl_vari += 1
        for key, value in rays_players.items():
            if rays_players[key][1] == ctrl_vari:
                print('Batting ', rays_players[key][1],': ', rays_players[key][0], key,',current avg',rays_players[key][2])
    #################################################################################################
    print()                                                             ## creates an empty line
    print('Lineup changed\n')
    substitute = {'Wilson': ['C', 9, 152], 'Beckham': ['DH', 6, 299]}   # create substitute dictionary
    del rays_players['Dejesus']                                         # remove element from rays_player dictionary
    del rays_players['Rivera']                                          # remove element from rays_player dictionary
    rays_players.update(substitute)                                     # update rays_player dictionary to include substitute dictionary
    #################################################################################################
    ctrl_vari = 0
    for i in range(9):
        ctrl_vari += 1
        for key, value in rays_players.items():
            if rays_players[key][1] == ctrl_vari:
                print('Batting ', rays_players[key][1],': ', rays_players[key][0], key,',current avg',rays_players[key][2])
    #################################################################################################

    # sorted_list = [(key,value) for value,key in sorted([(value[1],key) for key,value in rays_players.items()])]
    # for key, value in sorted_list:

    #     print('Batting ', value,': ', rays_players[key][0], key,',current avg',rays_players[key][2])
    #################################################################################################
main()  # call main function


import unittest

from egnyte import EgnyteTasks

class TestEgnyteTasks(unittest.TestCase):
    def setUp(self):
        self.tasks = EgnyteTasks()

    def test_difference_calculation(self):
        self.assertEqual(self.tasks._EgnyteTasks__difference(10, 6), 4,
                         'difference should be substraction'
        )
        self.assertEqual(self.tasks._EgnyteTasks__difference(6, 10), 4,
                         'difference should be absolute substraction'
        )
        self.assertEqual(self.tasks._EgnyteTasks__difference('10', '6'), 4,
                         'difference should work with strings'
        )
        self.assertEqual(self.tasks._EgnyteTasks__difference(1, 1), 0,
                         'difference should be 0 for equal arguments'
        )
        self.assertEqual(self.tasks._EgnyteTasks__difference(-3, -1), 2,
                         'difference should work with negative numbers'
        )
        self.assertEqual(self.tasks._EgnyteTasks__difference(-3, 1), 4,
                         'difference should work with various signs numbers'
        )

    def test_new_min(self):
        self.assertEqual(self.tasks._EgnyteTasks__new_min(None, 1), True,
                         'new_min should be True when old is None'
        )
        self.assertEqual(self.tasks._EgnyteTasks__new_min(6, 10), False,
                         'new_min should be False for larger new number'
        )
        self.assertEqual(self.tasks._EgnyteTasks__new_min(10, 6), True,
                         'new_min should be True for smaller new number'
        )

    def test_weather_matching(self):
        self.assertIsNone(self.tasks._EgnyteTasks__weather_match(''),
                         'weather_match should be None for empty string'
        )
        legend_line = ('  Dy MxT   MnT   AvT   HDDay  AvDP 1HrP TPcpn WxType '
                       + 'PDir AvSp Dir MxS SkyC MxR MnR AvSLP'
        )
        self.assertIsNone(self.tasks._EgnyteTasks__weather_match(legend_line),
                         'weather_match should be None for legend line'
        )
        valid_line = ('   1  88    59    74          53.8       0.00 F'
                      + '       280  9.6 270  17  1.6  93 23 1004.5'
        )
        self.assertIsNotNone(
            self.tasks._EgnyteTasks__weather_match(valid_line),
            'weather_match should not be None for valid line'
        )
        short_line = '   1  88    59    74 '
        self.assertIsNotNone(
            self.tasks._EgnyteTasks__weather_match(short_line),
            'weather_match should not be None for short line'
        )
        line_with_stars = '   1  88*   59*   74 '
        self.assertIsNotNone(
            self.tasks._EgnyteTasks__weather_match(line_with_stars),
            'weather_match should not be None for line with stars'
        )

    def test_team_matching(self):
        self.assertIsNone(self.tasks._EgnyteTasks__team_match(''),
                         'team_match should be None for empty string'
        )
        legend_line = ('       Team            P     W    L   D    '
                       +'F      A     Pts'
        )
        self.assertIsNone(self.tasks._EgnyteTasks__team_match(legend_line),
                         'team_match should be None for legend line'
        )
        valid_line = ('    1. Arsenal         38    26   9   3    '
                      +'79  -  36    87'
        )
        self.assertIsNotNone(
            self.tasks._EgnyteTasks__team_match(valid_line),
            'team_match should not be None for valid line'
        )
        short_line = '    1. Arsenal   79  -  36 '
        self.assertIsNotNone(
            self.tasks._EgnyteTasks__team_match(short_line),
            'team_match should not be None for short line'
        )

    def test_default_weather_file(self):
        self.assertEqual(self.tasks.smallest_temperature_spread(), 14,
                        'wrong day from default file'
        )

    def test_default_football_file(self):
        self.assertEqual(self.tasks.smallest_goals_difference(),
                         'Aston_Villa', 'wrong team from default file'
        )

if __name__ == "__main__":
    unittest.main()


import turtle,time,random,winsound
#set up the screen
win = turtle.Screen()
win.title("snake game")
win.bgcolor('#90ee90')
win.setup(width=630,height=630)
win.tracer(0)
#Snake Head
head = turtle.Turtle()
head.speed(1)
head.shape("square")
head.right(45)
head.shapesize(1.50,1.50 )
head.color("seagreen")
head.penup()
head.goto(0, 100)
head.direction = "stop"
def move():
    if head.direction == "up":
        y = head.ycor() #y coordinate of the turtle
        head.sety(y + 20)

    if head.direction == "down":
        y = head.ycor() #y coordinate of the turtle
        head.sety(y - 20)

    if head.direction == "right":
        x = head.xcor() #y coordinate of the turtle
        head.setx(x + 20)

    if head.direction == "left":
        x = head.xcor() #y coordinate of the turtle
        head.setx(x - 20)
def go_up():
    if head.direction != "down":
        head.direction = "up"

def go_down():
    if head.direction != "up":
        head.direction = "down"

def go_right():
    if head.direction != "left":
        head.direction = "right"

def go_left():
    if head.direction != "right":
        head.direction = "left"
win.listen()
win.onkey(go_up, "Up")
win.onkey(go_down, "Down")
win.onkey(go_right, "Right")
win.onkey(go_left, "Left")
delay=0.09
# Snake food
food = turtle.Turtle()
food.speed(1)
food.shape("circle")
food.color("#CD5C5C")
food.penup()
food.shapesize(1,1)
food.goto(0, 0)
score=0
# score
high_score = 0
pen = turtle.Turtle()
pen.speed(1)
pen.shape("square")
pen.color("#fafafa")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: {}".format(score), align="center", font=("Courier", 24, "bold"))

segments = []
line=turtle.Turtle()
line.penup()
line.goto(-300,-300)
line.pensize(3)
line.color('seagreen')
line.pendown()
line.goto(-300,300)
line.goto(300,300)
line.goto(300,-300)
line.goto(-300,-300)
line.ht()
# Main game loop
while True:
    win.update()
    move()
    time.sleep(delay)
    if head.distance(food) < 15:
        delay-=0.001
        x = random.randint(-280, 280)
        y = random.randint(-280, 280)
        food.goto(x, y)
        score = score+10
        if score > high_score:
            high_score = score
        pen.clear()
        pen.write("Score: {}".format(score,high_score), align="center", font=("Courier", 24, "bold"))
        new_segment = turtle.Turtle()
        new_segment.speed(1)
        new_segment.shape("square")
        new_segment.right(45)
        new_segment.shapesize(1.20,1.20)
        new_segment.color("seagreen")
        new_segment.penup()
        segments.append(new_segment)
        winsound.Beep(1200,50)
    # move the end segment in reverse order

    for index in range(len(segments)-1, 0, -1):
        x = segments[index-1].xcor()
        y = segments[index-1].ycor()
        segments[index].goto(x, y)

    if len(segments) > 0:
        x = head.xcor()
        y = head.ycor()
        segments[0].goto(x, y)
    # Check for head collision
    end=0
    segend=list(segments)
    if len(segments) > 0:
        del segend[0]

    for segment in segend:
        if segment.distance(head) < 10:
            end=1
            segment.color('red','red')
            win.update()
            break;
    if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
            win.update()
            end=1
    if end==1:
        winsound.Beep(2000,500)
        time.sleep(0.8)
        food.ht()
        win.update()
        break
for segn in range(len(segments)-1,-1,-1):
    win.update()
    segments[segn].ht()
    time.sleep(0.1)
head.ht()
win.update()
head.goto(0, 0)
pen.clear()
head.write(" GAME  OVER  ",align="center", font=("monospace", 50, "normal"))
head.goto(0, -50)
head.write("   SCORE: {}   ".format(score),align="center", font=("monospace", 40, "normal"))


from __future__ import print_function, unicode_literals
import json
from time import time
from textwrap import TextWrapper

from google.oauth2 import service_account
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build

from lumapps.utils import (
    DiscoveryCache,
    pop_matches,
    ApiCallError,
    GOOGLE_APIS,
    FILTERS,
)

class ApiClient(object):
    """
        Args:
            user (str): The user email.
            auth_info (dict): A service account key (json file).
            api_info (dict): A dict containing the description of your api. If
                no api_info is given this defaults to the lumsites api infos.
            credentials (dict): oauth2 credentials.
            token (str): A bearer token.
            token_getter (object): a token getter function
            prune (bool): Whether or not to use FILTERS to prune the LumApps
                API responses. Defaults to False.
            num_retries (int): Number of times that a request will be retried.
                Default to 1.

        Note:
            At least one type of authentication info is required (auth_info,
            credentials, token)
    """

    def __init__(
        self,
        auth_info=None,
        api_info=None,
        credentials=None,
        user=None,
        token=None,
        token_getter=None,
        prune=False,
        num_retries=1,
    ):
        self._get_token_user = None
        self._token_expiry = 0
        self.num_retries = num_retries
        self.prune = prune
        self._auth_info = auth_info
        self._user = {}
        self.email = ""
        self.last_cursor = None
        self.token_expiration = None

        # Api infos setup : construct the api url.
        if not api_info:
            api_info = {}
        self.api_info = api_info
        self._api_name = api_info.get("name", "lumsites")
        self._api_scopes = api_info.get(
            "scopes", ["https://www.googleapis.com/auth/userinfo.email"]
        )
        self._api_version = api_info.get("version", "v1")
        self.base_url = api_info.get("base_url", "https://lumsites.appspot.com").rstrip(
            "/"
        )
        if self._api_name in GOOGLE_APIS:
            url_path = "discovery/v1/apis"
        else:
            url_path = "_ah/api/discovery/v1/apis"
        self._url = "{}/{}/{}/{}/rest".format(
            self.base_url, url_path, self._api_name, self._api_version
        )
        self._methods = None
        self._service = None
        self.token_getter = token_getter
        if token_getter:
            self.creds = None
            self._check_access_token()
        elif credentials:
            self.creds = credentials
        elif auth_info and "refresh_token" in auth_info:
            self.creds = Credentials(None, **auth_info)
        elif auth_info and "bearer" in auth_info:
            self.creds = Credentials(auth_info["bearer"].replace("Bearer ", ""))
        elif token:
            self.creds = None
            self.token = token
        elif auth_info:  # service account
            self.creds = service_account.Credentials.from_service_account_info(
                auth_info
            )
            if self._api_scopes:
                self.creds = self.creds.with_scopes(self._api_scopes)
            if user:
                self.creds = self.creds.with_subject(user)
        else:
            raise Exception(
                "You must provide authentication infos (token_getter, "
                "auth_info, credentials or token)."
            )
        if user:
            self.email = user

    def _check_access_token(self):
        if not self.token_getter:
            return
        exp = self._token_expiry
        if exp and exp > time() + 120:
            return
        self.token, self._token_expiry = self.token_getter()

    def _prune(self, method_parts, content):
        """Prune the api response.
        """
        if not self.prune:
            return content
        for filter_method in FILTERS:
            filter_method_parts = filter_method.split("/")
            if len(method_parts) != len(filter_method_parts):
                continue
            for filter_part, part in zip(filter_method_parts, method_parts):
                if filter_part not in ("*", part):
                    break
            else:
                for pth in FILTERS[filter_method]:
                    if isinstance(content, list):
                        for o in content:
                            pop_matches(pth, o)
                    else:
                        pop_matches(pth, content)
        return content

    def get_new_client_as(self, user):
        return ApiClient(self._auth_info, self.api_info, user=user)

    @property
    def token(self):
        if not self.creds.token:
            self.service._http.request(self.base_url)
        return self.creds.token

    @token.setter
    def token(self, v):
        if self.creds and self.creds.token == v:
            return
        self._service = None
        self.creds = Credentials(v)

    @property
    def service(self):
        """Setup the service object.
        """
        self._check_access_token()
        if self._service is None:
            self._service = build(
                self._api_name,
                self._api_version,
                discoveryServiceUrl=self._url,
                credentials=self.creds,
                cache_discovery=True,
                cache=DiscoveryCache(),
            )
        return self._service

    @property
    def methods(self):
        if self._methods is None:
            self._methods = {
                n: m for n, m in self.walk_api_methods(self.service._resourceDesc)
            }
        return self._methods

    def get_help(self, method_parts, debug=False):
        help_lines = []

        def w(l):
            help_lines.append(l)

        wrapper = TextWrapper(initial_indent="\t", subsequent_indent="\t")
        method = self.methods[method_parts]
        w(method.get("httpMethod", "?") + " method: " + " ".join(method_parts) + "\n")
        if "description" in method:
            w(method["description"].strip() + "\n")
        if debug:
            w(json.dumps(method, indent=4, sort_keys=True))
        params = method.get("parameters", {})
        if method.get("httpMethod", "") == "POST":
            params.update(
                {"body": {"required": True, "type": "JSON"}, "fields": {"type": "JSON"}}
            )
        if not params:
            w("API method takes no parameters")
        else:
            w("Parameters (*required):")
            for param_name in sorted(params):
                param = params[param_name]
                descr = param.get("description")
                descr = "\n" + wrapper.fill(descr) if descr else ""
                w(
                    "  {}: {} {} {}".format(
                        param_name,
                        param["type"],
                        "*" if param.get("required") else "",
                        descr,
                    )
                )
        return "\n".join(help_lines)

    def get_method_descriptions(self, methods):
        lines = []
        for method_parts in methods:
            method = self.methods[method_parts]
            lines.append(
                (
                    " ".join(method_parts),
                    method.get("description", "").strip().split("\n")[0],
                )
            )
        longest_name = max(len(l[0]) for l in lines)
        fmt = "  {{: <{}}}  {{}}".format(longest_name)
        return "\n".join(fmt.format(*l) for l in lines)

    def walk_api_methods(self, resource, parents=()):
        for method_name, method in resource.get("methods", {}).items():
            yield tuple(parents + (method_name,)), method
        for rsc_name, rsc in resource.get("resources", {}).items():
            for method_name, method in self.walk_api_methods(
                rsc, tuple(parents + (rsc_name,))
            ):
                yield method_name, method

    def _get_api_call(self, method_parts, params):
        """Construct the method to call by using the service.
        """
        api_call = self.service
        for part in method_parts[:-1]:
            api_call = getattr(api_call, part)()
        try:
            return getattr(api_call, method_parts[-1])(**params)
        except TypeError as err:
            raise ApiCallError(err)

    def get_call(self, *method_parts, **params):
        """
        Args:
            *method_parts (str): API method.
            **params: Parameters.

        Returns:
            dict: An object, or list of objects returned by API method.

        Example:
            List feedtypes in LumApps:
            -> GET https://.../_ah/api/lumsites/v1/feedtype/list

            With this method:

                >>> feedtypes = get_call("feedtype", "list")
                >>> print(feedtypes)
        """
        if params is None:
            params = {}
        items = []
        cursor = None
        if "body" in params and isinstance(params["body"], str):
            params["body"] = json.loads(params["body"])
        while True:
            if cursor:
                if "body" in params:
                    params["body"]["cursor"] = cursor
                else:
                    params["cursor"] = cursor
            response = self._get_api_call(method_parts, params).execute(
                num_retries=self.num_retries
            )
            if "more" in response and "items" not in response:
                self.last_cursor = None
                return items  # empty list
            if "more" in response and "items" in response:
                items.extend(response["items"])
                if response.get("more", False):
                    self.last_cursor = cursor = response["cursor"]
                else:
                    return self._prune(method_parts, items)
            else:
                self.last_cursor = None
                return self._prune(method_parts, response)

    def iter_call(self, *method_parts, **params):
        """
        Args:
            *method_parts (str): API method.
            **params: Parameters.

        Yields:
            dict: Objects returned by API method.

        Example:
            List feedtypes in LumApps:
            -> GET https://.../_ah/api/lumsites/v1/feedtype/list

            With this method:

                >>> feedtypes = iter_call("feedtype", "list")
                >>> for feedtype in feedtypes: print(feedtype)
        """
        if params is None:
            params = {}
        cursor = None
        if "body" in params and isinstance(params["body"], str):
            params["body"] = json.loads(params["body"])
        while True:
            if cursor:
                if "body" in params:
                    params["body"]["cursor"] = cursor
                else:
                    params["cursor"] = cursor
            response = self._get_api_call(method_parts, params).execute(
                num_retries=self.num_retries
            )
            if "more" in response and "items" not in response:
                return  # empty list
            if "more" in response and "items" in response:
                for item in response["items"]:
                    yield self._prune(method_parts, item)
                if response.get("more", False):
                    cursor = response["cursor"]
                else:
                    return
            else:
                yield self._prune(method_parts, response)

    def get_matching_methods(self, method_parts):
        # find exact matches of all parts up to but excluding last
        matches = [n for n in self.methods if len(n) >= len(method_parts)]
        for i, part in enumerate(method_parts[:-1]):
            matches = [n for n in matches if len(n) >= i and n[i] == part]
        # find 'startswith' matches of the last part
        last = method_parts[-1]
        idx = len(method_parts) - 1
        matches = [m for m in matches if len(m) >= idx and m[idx].startswith(last)]
        if not matches:
            return "API method not found"
        return (
            "API method not found. Did you mean any of these?\n"
            + self.get_method_descriptions(sorted(matches))
        )


from flask import render_template
from flask_login import login_required
from app import app

from app.MSSql.SqlGet import SQLGet
from app.routes.Spravochniki.sql.get_commands import *

@app.route('/admin/spravochniki/torgovietochki/kateg_torg_tochek')
@login_required
def spravochniki_finance_kateg_torg_tochek():
        sqlcmd = """
               Select CategoryTTNo, CategoryTTName, Active from D_CategoryTT
            """

        context = {
            'title': 'Категории Торг Точек',
            'username': 'admin',
            'table_fields': ['Номер', 'Наименование категории ТТ', 'Активен'],
            'table_data': list(SQLGet(sqlcmd).get_sql_from_db()),
        }
        return render_template('admin/pages/table_view.html', context=context)

@app.route('/admin/spravochniki/torgovietochki/rajoni')
@login_required
def spravochniki_torgovietochki_rajoni():
    sqlcmd = """
        select RegionNo,RegionName, Visible
        from Regions

        order by RegionName
    """

    context = {
        'title': 'Районы',
        'username': 'admin',
        'table_fields': ['Номер', 'Наименование', 'Спрятать'],
        'table_data': list(SQLGet(sqlcmd).get_sql_from_db()),
    }
    return render_template('admin/pages/table_view.html', context=context)

@app.route('/admin/spravochniki/torgovietochki/territorii')
@login_required
def spravochniki_torgovietochki_territorii():
    sqlcmd = """
        select * from KOATUUOfPepsico
    """
    context = {
        'title': 'территории',
        'username': 'admin',
        'table_fields': ['KOATUU_ID_CODE', 'KOATUU_L1_NAME_RUS', 'KOATUU_L2_NAME_RUS'],
        'table_data': list(SQLGet(sqlcmd).get_sql_from_db()),
    }
    return render_template('admin/pages/table_view.html', context=context)

@app.route('/admin/spravochniki/torgovietochki/ABCDZ_klassificator')
@login_required
def spravochniki_torgovietochki_ABCDZ_klassificator():
    sqlcmd = """
                   Select * from ClassifierABCDZ
                """

    context = {
        'title': 'ABCDZ Классивикатор',
        'username': 'admin',
        'table_fields': ['Номер', 'Наименование', 'Активен'],
        'table_data': list(SQLGet(sqlcmd).get_sql_from_db()),
    }
    return render_template('admin/pages/table_view.html', context=context)

@app.route('/admin/spravochniki/torgovietochki/torgovie_seti')
@login_required
def spravochniki_torgovietochki_torgovie_seti():
    sqlcmd = """
        select *
        from OurCommercialNetwork ocn
          where (ocn.NameCommercialNetwork is not null or ocn.NameCommercialNetwork <> ' ')  and Visible = 1 and Trash = 0
        order by ocn.NameCommercialNetwork
    """
    context = {
        'title': 'Торговые сети',
        'username': 'admin',
        'table_fields': ['Код', 'Наименование сети', 'V', 'D'],
        'table_data': list(SQLGet(sqlcmd).get_sql_from_db()),
    }
    return render_template('admin/pages/table_view.html', context=context)

@app.route('/admin/spravochniki/torgovietochki/logicZones')
@login_required
def spravochniki_torgovietochki_logic_zones():
    sqlcmd = """
        select *
         from LogisticZone lz
          where (lz.NameLogisticZone is not null or lz.NameLogisticZone <> ' ')  and Visible = 1 and Trash = 0
        order by lz.NameLogisticZone
    """
    context = {
        'title': 'Лоические зоны',
        'username': 'admin',
        'table_fields': ['Код', 'Наименование зоны', 'V', 'D'],
        'table_data': list(SQLGet(sqlcmd).get_sql_from_db()),
    }
    return render_template('admin/pages/table_view.html', context=context)


import click

from .utils import do_my_super_path_stuff

@click.command()
@click.option('--graph-path', required=True, type=click.Path(dir_okay=False, file_okay=True), help='Path to networkx pickle file')
@click.option('--df-path', required=True, help='Path to TSV file')
@click.option('--output-path', required=True, help='Where to stick output TSV file')
@click.option('--dgxp-threshold', default=2.0, show_default=True, type=float, help='What makes a gene differentially expressed?')
def cli(graph_path: str, df_path: str, output_path: str, dgxp_threshold):
    print('Doing my super path stuff')
    print(graph_path)
    print(df_path)
    print(output_path)
    print(dgxp_threshold)
    do_my_super_path_stuff(graph_path, df_path, output_path)

if __name__ == '__main__':
    cli()