from copy import deepcopy
import json
import re

from errors import Asg3Error
from helpers import tokenize

class DocData:
  """
    Represents doc data in a structured format.
      docs: list of tuples containing the data.
        tuple[0] is the doc string, tuple[1] indicates
        whether it is positive or non-positive.
        -1 -> unknown
        0 -> negative
        1 -> positive
      label_count: keeps a count of the number of docs by label
      name: given id for the object
      words: all unique words from data, mapped to list of
        indexes of docs that use that word
  """

  def __init__(self, name, priors):
    self.docs = []
    self.label_count = {}
    self.name = name
    self.priors = deepcopy(priors)
    self.vocab = set()
    self.words = {}

    # initialize vars
    for i in range(2):
      self.label_count[i] = 0

  def add_doc(self, doc, flag):
    """
      Adds doc and doc components to class data.
        -1 -> unknown
        0 -> negative
        1 -> positive
    """

    self.docs.append((doc, flag))

    # add the doc's words
    tokens = tokenize(doc)

    # update the vocab
    self.vocab.update(tokens)

    if flag != -1:
      self.label_count[flag] += len(tokens)

    # get the number of times that this word appears in the doc
    word_counts = {}
    for word in tokens:
      if word not in word_counts:
        word_counts[word] = 0
      word_counts[word] += 1

    # increment all of the word counts
    for word in tokens:
      if word not in self.words:
        self.words[word] = {}
      self.words[word][len(self.docs)-1] = word_counts[word]

    # for word in tokens:
    #   if word not in self.words:
    #     self.words[word] = set()
    #   # associate this word with the index of this doc
    #   self.words[word].add(len(self.docs)-1)

  def add_docs(self, docs, flag):
    """
      Add doc data to the object. Must specify the label to use for all included docs.
        -1 -> unknown
        0 -> negative
        1 -> positive
    """
    if flag not in [-1, 0, 1]:
      raise Asg3Error('flag')

    for doc in docs:
      self.add_doc(doc, flag)

  def load(self, doc_data_obj):
    """
      Loads a model from a saved one
    """
    obj = json.load(doc_data_obj)
    self.docs = obj['docs']
    self.label_count = obj['label_count']
    self.name = obj['name']
    self.priors = obj['priors']
    self.vocab = set(obj['vocab'])
    self.words = {w: set(self.words[w]) for w in obj['words']}

  def num_positive(self):
    return self.label_count[1]

  def num_positive_docs(self):
    n = 0
    for doc in self.docs:
      if doc[1] == 1:
        n += 1
    return n

  def num_negative(self):
    return self.label_count[0]

  def num_negative_docs(self):
    n = 0
    for doc in self.docs:
      if doc[1] == 0:
        n += 1
    return n

  def stringify(self):
    """
      Saves the model to a file for later loading
    """
    return json.dumps({
      'docs': self.docs,
      'label_counts': self.label_count,
      'name': self.name,
      'priors': self.priors,
      'vocab': list(self.vocab),
      'words': {w: list(self.words[w]) for w in self.words}
    })

  def vocab_size(self):
    return len(self.vocab)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import testsets
import evaluation
import twokenize
import collections
import os
# import sklearn.feature_extraction
# from nltk.classify.scikitlearn import SklearnClassifier
# import sklearn
# import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
import textPreprocessor02
import nltk
# from nltk.stem import *
from nltk.probability import FreqDist
# from nltk.corpus import sentiwordnet as swn

# from gensim.models import word2vec
import pickle
import word2vecReader
# from sklearn.naive_bayes import GaussianNB, MultinomialNB
# from sklearn.pipeline import Pipeline
# from sklearn import svm
# from sklearn.externals import joblib
# from sklearn.feature_extraction import DictVectorizer
from sklearn.linear_model import LogisticRegression
# from sklearn import preprocessing
# import pandas as pd
# from sklearn.preprocessing import StandardScaler
# from sklearn import tree
# from sklearn.ensemble import RandomForestClassifier
# from sklearn.neighbors import KNeighborsClassifier
# pickle.
# from nltk.corpus import wordnet
# from nltk.stem.wordnet import WordNetLemmatizer

# TODO: load training data
def read_training_data(training_data):
    id_gts = {}
    with open(training_data, 'r', encoding='utf-8') as f:
        for line in f:
            fields = line.split('\t')
            tweetid = fields[0]
            gt = fields[1]
            content = fields[2].strip()
            id_gts[tweetid] = gt, content
    return id_gts

# input here
def perprocessing(tdic):
    new_dic = {}
    for line in tdic:
        id = line
        gt = tdic[line][0]
        raw = ' '.join(twokenize.tokenizeRawTweetText(tdic[line][1]))
        text = twokenize.normalizeTextForTagger(raw)
        text_tk = twokenize.tokenize(text)
        telist = []
        for word in text_tk:
            word = word.lower()
            ps = nltk.stem.PorterStemmer()
            word = ps.stem(word)
            telist.append(word)
        newtext = ' '.join(telist)
        # print(newtext)
        newtext = textPreprocessor02.replaceall(newtext)
        new_dic[id] = gt, newtext
        # print(newtext)
    return new_dic

# print(new_dic)
def get_train_corpus(new_dic):

    traincorpus = []
    for line in new_dic:
        traincorpus.append(new_dic[line][1])
    # print(traincorpus)
    return traincorpus

def get_split_corpus(new_dic):
    split_traincorpus = []
    for line in new_dic:
        split_traincorpus.append(new_dic[line][1].split())
    return split_traincorpus

# TODO extract features

def get_vect():
    if os.path.isfile('X.pkl') and os.path.isfile('vector.pkl'):
        return
    else:
        vect = CountVectorizer(stop_words='english',ngram_range=(1,2), max_features=17000, lowercase=True)
        # vect = CountVectorizer(stop_words='english', min_df=  ,lowercase=True)
        # vectorizer = CountVectorizer(stop_words='english', ngram_range=(1, 2))
        X = vect.fit_transform(train_corpus)
        # print(type(X))
        # print(X)
        # print(vect)
        pickle.dump(X,open('X.pkl', 'wb'))
        pickle.dump(vect, open('vector.pkl', 'wb'))
    # return vect, X

def get_train_ngrams():
    get_vect()
    with open('X.pkl', 'rb') as f:
        X_train = pickle.load(f)
        # return f1
    # X = pickle.load(open('X.pkl','rb'))
        ans = np.array(X_train.todense())
        # pickle.dump(ans, open('F1.pkl', 'wb'))
        return ans
        # return np.array(X.todense())

def get_test_ngrams(corpus):
    # vect = get_vect()[0]
    get_vect()
    with open('vector.pkl', 'rb') as f:
        # f1 = pickle.load(f)
        # return f1
        vect = pickle.load(f)
        X = vect.transform(corpus)
        b = X.todense()
        return np.array(b)

def get_senti_score(split_corpus):
    def senti_file():
        senti_dic = collections.defaultdict(lambda: 0)
        with open('SentiWords_1.1.txt', 'r') as f:
            for line in f:
                fields = line.strip().split('\t')
                senti_dic[fields[0]] = float(fields[1])
            return senti_dic
    mydic = senti_file()
    all_socre = []
    for line in split_corpus:
        after_tag = nltk.pos_tag(line)
        score_feature = 0
        for word,tag in after_tag:
            if tag.startswith('N'):
                after = word+str('#n')
                score = mydic[after]
            elif tag.startswith('V'):
                after = word+str('#v')
                score = mydic[after]
            elif tag.startswith('J'):
                after = word + str('#a')
                score = mydic[after]
            elif tag.startswith('R'):
                after = word + str('#r')
                score = mydic[after]
            else:
                after = word + str('#n')
                score = mydic[after]
            score_feature += score
        all_socre.append([score_feature])
    ans = np.array(all_socre)
    return ans

def get_tfidf(corpus):
    get_vect()
    with open('vector.pkl', 'rb') as f:
        vect = pickle.load(f)
        tfidf = TfidfVectorizer(vocabulary=list(vect.vocabulary_.keys()), lowercase=True, stop_words='english')
        tfs = tfidf.fit_transform(corpus)
        tt = tfs.todense()
        return np.array(tt)

model_path = "word2vec_twitter_model.bin"
model = word2vecReader.Word2Vec.load_word2vec_format(model_path, binary=True)
def word_embedding(split_corpus, model=model, size=400):
    # using external twitter specific per-trained model
    ans = []
    for line in split_corpus:
        vec = np.zeros(size)
        count = 0.
        for word in line:
            try:
                vec += model[word]
                count += 1.
            except KeyError:
                continue
        if count != 0:
            vec /= count
        ans.append(vec.tolist())
    arr = np.array(ans)
    return arr

def senti_bi_lexicon(split_corpus):
    def inputfile(file):
        with open(file, 'r') as my_file:
            words = [every_line.rstrip() for every_line in my_file]
            return words

    def count_p_n(mylist):
        pos_num = 0
        neg_num = 0
        positive = inputfile('positive-words.txt')
        negative = inputfile('negative-words.txt')
        p_dic = FreqDist(positive)
        n_dic = FreqDist(negative)
        for word in mylist:
            pos_num += p_dic[word]
            neg_num += n_dic[word]
        return pos_num, neg_num

    P_N = []
    for line in split_corpus:
        p_num_all = n_num_all = 0
        p_n_num = count_p_n(line)
        p_num_all += p_n_num[0]
        n_num_all += p_n_num[1]
        P_N.append([p_num_all, n_num_all])
    # print('sent..')
    # print(len(P_N))
    return np.array(P_N)

def get_url(split_corpus):
    url = []
    for i in split_corpus:
        num = i.count('URLLINK')
        url.append([num])
    # print(url)
    # print(len(url))
    return np.array(url)

def get_mention(split_corpus):
    men = []
    for i in split_corpus:
        num = i.count('USERMENTION')
        men.append([num])
        # print(url)
        # print(len(url))
    return np.array(men)

def get_face(split_corpus):
    face = []
    for i in split_corpus:
        numi = i.count('HAPPYFACE')
        numj = i.count('SADFACE')
        face.append([numi, numj])
        # print(url)
        # print(len(url))
    return np.array(face)

def get_F2():
    if os.path.isfile('F2.pkl'):
        with open('F2.pkl', 'rb') as f:
            f2 = pickle.load(f)
            return f2
    else:
        ans = get_tfidf(train_corpus)
        pickle.dump(ans, open('F2.pkl', 'wb'))
        return ans
def get_F3():
    if os.path.isfile('F3.pkl'):
        with open('F3.pkl', 'rb') as f:
            f3 = pickle.load(f)
            return f3
    else:
        ans = senti_bi_lexicon(split_corpus)
        pickle.dump(ans, open('F3.pkl', 'wb'))
        return ans
def get_F4():
    if os.path.isfile('F4.pkl'):
        with open('F4.pkl', 'rb') as f:
            f4 = pickle.load(f)
            return f4
    else:
        ans = word_embedding(split_corpus)
        pickle.dump(ans, open('F4.pkl', 'wb'))
        return ans
def get_F5():
    if os.path.isfile('F5.pkl'):
        with open('F5.pkl', 'rb') as f:
            f = pickle.load(f)
            return f
    else:
        ans = get_url(split_corpus)
        pickle.dump(ans, open('F5.pkl', 'wb'))
        return ans
def get_F6():
    if os.path.isfile('F6.pkl'):
        with open('F6.pkl', 'rb') as f:
            f = pickle.load(f)
            return f
    else:
        ans = get_mention(split_corpus)
        pickle.dump(ans, open('F6.pkl', 'wb'))
        return ans
def get_F7():
    if os.path.isfile('F7.pkl'):
        with open('F7.pkl', 'rb') as f:
            f = pickle.load(f)
            return f
    else:
        ans = get_face(split_corpus)
        pickle.dump(ans, open('F7.pkl', 'wb'))
        return ans
def get_F8():
    if os.path.isfile('F8.pkl'):
        with open('F8.pkl', 'rb') as f:
            f = pickle.load(f)
            return f
    else:
        ans = get_senti_score(split_corpus)
        pickle.dump(ans, open('F8.pkl', 'wb'))
        return ans

traindic = read_training_data('twitter-training-data.txt')
newdic = perprocessing(traindic)
train_corpus = get_train_corpus(newdic)
split_corpus = get_split_corpus(newdic)
labels_to_array = {"positive": 0, "negative": 2, "neutral": 1}
labels = [labels_to_array[newdic[tweet][0]] for tweet in newdic]
Y = np.array(labels)

for classifier in ['Logistic Regression0','Logistic Regression1','Logistic Regression2']:

    if classifier == 'Logistic Regression0':
        print('Training ' + classifier)
        if os.path.isfile('LR0.pkl'):
            with open('LR0.pkl', 'rb') as f:
                model = pickle.load(f)
        else:
            # F1 = get_train_ngrams()
            F2 = get_tfidf(train_corpus)
            F3 = get_F3()
            # print(F3)
            # F4 = get_F4()
            X = np.concatenate((F2, F3), axis=1)
            # pickle.dump(X, open('X0.pkl', 'wb'))
            model = LogisticRegression(class_weight='balanced')
            model.fit(X, Y)
            pickle.dump(model, open('LR0.pkl', 'wb'))

    elif classifier == 'Logistic Regression1':
        print('Training ' + classifier)
        if os.path.isfile('LR1.pkl'):
            with open('LR1.pkl', 'rb') as f:
                model = pickle.load(f)
        else:
            F2 = get_tfidf(train_corpus)
            F3 = get_F3()
            F4 = get_F4()
            X = np.concatenate((F2, F3, F4), axis=1)
            model = LogisticRegression(class_weight='balanced')
            model.fit(X, Y)
            pickle.dump(model, open('LR1.pkl', 'wb'))

    elif classifier == 'Logistic Regression2':
        print('Training ' + classifier)
        if os.path.isfile('LR2.pkl'):
            with open('LR2.pkl', 'rb') as f:
                model = pickle.load(f)
        else:
            # F1 = get_train_ngrams()
            F2 = get_tfidf(train_corpus)
            F3 = get_F3()
            F4 = get_F4()
            F5 = get_F5()
            F7 = get_F7()
            F8 = get_F8()
            X = np.concatenate((F2, F3, F4, F5, F7, F8), axis=1)
            model = LogisticRegression(class_weight='balanced')
            model.fit(X, Y)
            pickle.dump(model, open('LR2.pkl', 'wb'))

    for testset in testsets.testsets:
        # TODO: classify tweets in test set
        test = read_training_data(testset)
        testdic = perprocessing(test)
        t_corpus = get_train_corpus(testdic)
        ts_corpus = get_split_corpus(testdic)
        if classifier == 'Logistic Regression0':
            # tF1 = get_test_ngrams(t_corpus)
            tF2 = get_tfidf(t_corpus)
            tF3 = senti_bi_lexicon(ts_corpus)
            # tF4 = word_embedding(ts_corpus)
            Xt = np.concatenate((tF2,tF3), axis=1)
        elif classifier == 'Logistic Regression1':
            tF2 = get_tfidf(t_corpus)
            tF3 = senti_bi_lexicon(ts_corpus)
            tF4 = word_embedding(ts_corpus)
            Xt = np.concatenate((tF2, tF3, tF4), axis=1)
        elif classifier == 'Logistic Regression2':
            # tF1 = get_test_ngrams(t_corpus)
            tF2 = get_tfidf(t_corpus)
            tF3 = senti_bi_lexicon(ts_corpus)
            tF4 = word_embedding(ts_corpus)
            tF5 = get_url(ts_corpus)
            tF7 = get_face(ts_corpus)
            tF8 = get_senti_score(ts_corpus)
            Xt = np.concatenate((tF2, tF3, tF4,tF5, tF7, tF8), axis=1)

        # prob = model.predict_proba(Xt)
        # print(prob)
        ans_num = model.predict(Xt)
        # print(ans_num)
        ans_num = []
        # for each in prob:
        #     pos = each[0]
        #     neu = each[1]+0.05
        #     neg = each[2]+0.05
        #     if pos > neu and pos > neg:
        #         ans = 0
        #     elif  neu > neg:
        #         ans = 1
        #     else:
        #         ans = 2
        #     ans_num.append(ans)

        array_to_labels = {0: "positive", 1: "neutral", 2: "negative"}
        labels = [array_to_labels[i] for i in ans_num]
        predictions = dict(zip(list(testdic.keys()), labels))
        evaluation.evaluate(predictions, testset, classifier)
        evaluation.confusion(predictions, testset, classifier)


from sklearn.metrics import f1_score, precision_recall_curve, precision_recall_fscore_support, auc
from matplotlib import pyplot as plt
import numpy as np

def no_sampling(x_train, x_test, y_train, y_test, classifier, standalone=False):

	clf = classifier
	clf.fit(x_train, y_train)

	y_pred = clf.predict(x_test)

	if standalone:
		# calculate scores
		lr_probs = classifier.predict_proba(x_test)
		lr_probs = lr_probs[:, 1]
		lr_precision, lr_recall, _ = precision_recall_curve(y_test, lr_probs)
		lr_f1, lr_auc = f1_score(y_test, y_pred, average='micro'), auc(lr_recall, lr_precision)
		print('No Sampling: f1 = %.2f%% auc = %.2f%%' % (lr_f1 * 100, lr_auc * 100))
		prec, rec, f1, _ = precision_recall_fscore_support(y_test, y_pred, average='micro')
		print('No Sampling: precision = %.2f%% recall = %.2f%%' % (prec * 100, rec * 100))
		no_skill = len(y_test[y_test == 0]) / len(y_test)

		# Precision - Recall Curve Plot
		plt.plot([0, 1], [no_skill, no_skill], linestyle='--', label='No Skill')
		plt.plot(lr_recall, lr_precision, marker='.', label='No Sampling')
		plt.xlabel('Recall')
		plt.ylabel('Precision')
		plt.legend()
		plt.title("Precision-Recall Curve")
		plt.show()

	return y_pred

def only_majority_class(x_train, x_test, y_train, y_test, classifier):
	'''
	classifier is dump variable, only for reasons of uniformity.
	Function classifies data into the majority class.
	'''

	y_pred = np.zeros(y_test.shape[0])

	# calculate scores
	prec, rec, f1, _ = precision_recall_fscore_support(y_test, y_pred, average='micro')
	print('Only majority: f1 = %.2f%%' % (f1 * 100))

if __name__ == '__main__':
	from data_preprocessing import read_preprocessed
	from pathlib import Path
	from sklearn.ensemble import AdaBoostClassifier

	clf = AdaBoostClassifier()

	datapath = Path('data')
	x_train, x_test, y_train, y_test = read_preprocessed(datapath)

	no_sampling(x_train, x_test, y_train, y_test, clf, standalone=True)


"""Required modules"""
import time
from datetime import datetime
import requests
from dateutil import relativedelta

class IotHandler:
    """Handler class for challange_slide_182"""

    url = "https://jsonmock.hackerrank.com/api"
    iot_devices_endpoint = "/iot_devices/search"

    def get_data(self):
        """Get all data from API and return in weird shape"""

        params = {
            "page": 1
        }
        response_body_all = []
        for _ in range(10):
            response = requests.get(f"{self.url}{self.iot_devices_endpoint}",
                                    params=params, timeout=10)
            response_body = response.json()
            response_body_all.append(response_body["data"])
            params["page"] += 1

        return response_body_all

    def parse_all_records(self):
        """Parse data and return as list of dictionaries"""

        final_table_records = []
        data = self.get_data()
        for i in data:
            temp_table = []
            temp_table.append(i)
            for j in temp_table[0]:
                final_table_records.append(j)
            temp_table = []
        return final_table_records

    def num_devices(self, status_query: str, threshold: int, date_str: str) -> int:
        """Return num of devices with given parameters"""

        counter = 0
        check_list = []
        timestamp_start = int(time.mktime(datetime.strptime(date_str, "%m-%Y").timetuple()))
        timestamp_end = int((datetime.fromtimestamp(timestamp_start) +
                            relativedelta.relativedelta(months=1)).timestamp())

        final_table = self.parse_all_records()
        for dictionary in final_table:
            if dictionary["status"] == status_query \
                    and dictionary["operatingParams"]["rootThreshold"] > threshold\
                    and timestamp_start * 1000 < dictionary["timestamp"] < timestamp_end * 1000:
                counter += 1
                check_list.append(dictionary)
        return counter


import os
import json
import boto3
import logging
import argparse
import hashlib
import botocore
import time

from pprint import pprint

#############################################################################
#  Utils
#############################################################################

def get_config(parser):
    # FIXEM replace with fire (?)
    subparsers = parser.add_subparsers()

    # Copy Dashboard Sub command
    ####################################
    parser_copy  = subparsers.add_parser('copy')

    parser_copy_common = argparse.ArgumentParser(add_help=False)
    parser_copy_common.add_argument("--src-profile", action="store", dest="myAWSSrcProfile", required=True,
                        help="Your AWS source user Profile ")
    parser_copy_common.add_argument("--dst-profile", action="store", dest="myAWSDstProfile", required=True,
                        help="Your AWS destination user Profile ")
    parser_copy_common.add_argument("--src-name", action="store", dest="mySrcItemName",default="",
                        help="Copy command [iteam:version] ")
    parser_copy_common.add_argument("--data-set-name", action="store", dest="myDstDataSetName",default="",
                        help="Destination data set name ")
    parser_copy_common.add_argument("--dst-name", action="store", dest="myDstItemName",default=None,
                            help="Destination name")
    parser_copy_common.add_argument("--dst-perm-name", action="store", dest="myDstGroupName",default="",
                            help="Comment for Template creation")
    parser_copy_common.add_argument("--comment", action="store", dest="myComment",default="",
                            help="Comment for Template creation")

    parser_copy_sub = parser_copy.add_subparsers()
    parser_copy_dashboard = parser_copy_sub.add_parser('dashboard', parents=[parser_copy_common])

    parser_copy_dashboard.add_argument("--src-version", action="store", dest="mySrcItemVer",default=None,
                                help="Version [item:version] ")
    parser_copy_dashboard.set_defaults(func=copyItem,itemType='dashboard')

    parser_copy_analysis = parser_copy_sub.add_parser('analysis', parents=[parser_copy_common])
    parser_copy_analysis.add_argument("--src-version", action="store", dest="mySrcItemVer",default=None,
                                help="Version [item:version] ")
    parser_copy_analysis.set_defaults(func=copyItem,itemType='analysis')

    # ingest params by

    # List
    ####################################
    parser_list = subparsers.add_parser('list')
    parser_list_sub = parser_list.add_subparsers()
    #common vars
    parser_list_common = argparse.ArgumentParser(add_help=False)
    parser_list_common.add_argument("--profile", action="store", dest="myAWSProfile", required=True,
                                    help="Your AWS  user Profile ")

    parser_list_dashboard = parser_list_sub.add_parser('dashboard', parents=[parser_list_common])
    parser_list_dashboard.set_defaults(func=listDashboards)

    parser_list_dataset = parser_list_sub.add_parser('dataset', parents=[parser_list_common])
    parser_list_dataset.set_defaults(func=listDataSets)

    parser_list_template = parser_list_sub.add_parser('template', parents=[parser_list_common])
    parser_list_template.set_defaults(func=listTemplates)

    parser_list_analysis = parser_list_sub.add_parser('analysis', parents=[parser_list_common])
    parser_list_analysis.set_defaults(func=listAnalysis)

    # Desc
    ####################################
    parser_desc = subparsers.add_parser('desc')
    parser_desc_sub = parser_desc.add_subparsers()

    parser_desc_common = argparse.ArgumentParser(add_help=False)
    parser_desc_common.add_argument("--profile", action="store", dest="myAWSProfile", required=True,
                            help="Your AWS  user Profile ")
    parser_desc_common.add_argument("-id", action="store", dest="myItemId",default="",
                            help="ID ")

    parser_desc_dashboard = parser_desc_sub.add_parser('dashboard', parents=[parser_desc_common])
    parser_desc_dashboard.set_defaults(func=descDashboard)

    parser_desc_dataset = parser_desc_sub.add_parser('dataset', parents=[parser_desc_common])
    parser_desc_dataset.set_defaults(func=descDataSet)

    parser_desc_template = parser_desc_sub.add_parser('template', parents=[parser_desc_common])
    parser_desc_template.set_defaults(func=descTemplate)

    parser_desc_analysis = parser_desc_sub.add_parser('analysis', parents=[parser_desc_common])
    parser_desc_analysis.set_defaults(func=descAnalysis)

    # Global params
    ####################################
    #FIXME move to parents
    parser.add_argument("-v", "--verbosity", action="store",   dest="verbosity", default="INFO",
                        help="Debug level [ERROR|INFO|DEBUG]")

    args = parser.parse_args()
    return args

def init_logger( name, level, trace=False):
    #FIXME create config object
    log_level={'DEBUG':logging.DEBUG, 'INFO':logging.INFO, 'ERROR':logging.ERROR }
    logger = logging.getLogger(name)
    logger.setLevel(log_level[level])
    streamHandler = logging.StreamHandler()
    formatter     = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    streamHandler.setFormatter(formatter)
    logger.addHandler(streamHandler)

    return logger

def checkConfig(config,parser):
        pass

def check_status(response):
    if response['Status'] != 200:
        logger.error(f'Error : {response}')
        exit(1)

#############################################################################
#  Internal fucntions
#############################################################################
def copyDataSets():
        data_set_list = quicksight.list_data_sets(AwsAccountId=config.mySourceAccount)

        for data_set in data_set_list:
            data_set_details = quicksight.describe_data_sets(AwsAccountId=config.mySourceAccount,
                                                                DataSetId=data_set['DataSetId'])
            del data_set_details['Arn']
            del data_set_details['CreatedTime']
            del data_set_details['LastUpdatedTime']
            del data_set_details['ConsumedSpiceCapacityInBytes']

def getDashboardId(quicksight,awsAccountId,dashboardName):
    data = quicksight.list_dashboards(AwsAccountId=awsAccountId)
    #print(dashboards)

    for item in data['DashboardSummaryList']:
        if item['Name'] == dashboardName:
            return item

    return None

def getAnalysisId(quicksight, awsAccountId, analysisName):
    data = quicksight.list_analyses(AwsAccountId=awsAccountId)

    for item in data['AnalysisSummaryList']:
        if item['Name'] == analysisName:
            return item

    return None

def getDashboardById(quicksight, awsAccountId , dashboardId,VersionNumber=None):
    dashboard = quicksight.describe_dashboard(AwsAccountId=awsAccountId,
                                              DashboardId=dashboardId,
                                              VersionNumber=VersionNumber)['Dashboard']
    logger.debug('Describe dashboard '+ str(dashboard))
    return dashboard

def getAnalysisById(quicksight, awsAccountId , analisysId ):
    analysis = quicksight.describe_analysis(AwsAccountId=awsAccountId,
                                              AnalysisId=analisysId)['Analysis']
    logger.debug('Describe dashboard '+ str(analysis))
    return analysis

#cyclete over the dataset
def setTemplate(quicksight,awsAccountId,itemName,entityArn,dataSetArn,VersionNumber='0'):

    template_name = itemName+'-'+ VersionNumber+'-template'
    template_id = hashlib.sha224(template_name.encode()).hexdigest()

    #FIXME switch to use ALIAS
    entry = None
    logger.info(f'Searching template {template_id}')
    try :
        entry = quicksight.describe_template( AwsAccountId=awsAccountId,TemplateId=template_id)
        logger.info(f'Found template:  {template_id} ')
        logger.debug(f'Found template:  {entry} ')
    except botocore.exceptions.ClientError as error:
         if error.response['Error']['Code'] != 'ResourceNotFoundException':
            logger.info(f'Template {template_id} Not Found ')

    template_dict = {   'AwsAccountId':awsAccountId,
                        'TemplateId': template_id,
                        'Name': itemName+'-template',
                        'SourceEntity':{
                            'SourceAnalysis': {
                                'Arn': entityArn,
                                #'Arn': dashboard['Version']['SourceEntityArn'],
                                'DataSetReferences': [
                                    {
                                        'DataSetPlaceholder': 'dataset_placeholder',
                                        'DataSetArn': dataSetArn
                                        #'DataSetArn': dashboard['Version']['DataSetArns'][0]
                                    }
                                 ]
                              },
                         },
                         'VersionDescription':VersionNumber
                    }

    logger.debug(f'Template {template_dict}')

    if entry is None:
        entry = quicksight.create_template(**template_dict)
        logger.info(f'Template created with id {template_id} ')
    else :
        entry = quicksight.update_template(**template_dict)
        logger.info(f'Template updated with id {template_id}')

    #FIXME Check the Status and wait until resource is created
    return entry

def setAnalysis(quicksight, analysisName, templateArn, datasetArn, awsAccount, dstGroupName, ItemVer, comment):
    item = None
    analysisId = hashlib.sha224(analysisName.encode()).hexdigest()

    try:
        item = getAnalysisId(quicksight, awsAccount, analysisName)
        logger.info(f'Found analysis  : {item}')
        logger.debug(f'Found alaysis object : {item}')
    except botocore.exceptions.ClientError as error:
         if error.response['Error']['Code'] != 'ResourceNotFoundException':
            logger.info(f'Dashboard {analysisName} not Found')

    entry =  {
                    "AwsAccountId": awsAccount ,
                    "AnalysisId": analysisId,
                    "Name": analysisName,
                    "SourceEntity": {
                        "SourceTemplate": {
                            "DataSetReferences": [
                                {
                                    "DataSetPlaceholder": "dataset_placeholder",
                                    "DataSetArn": datasetArn
                                    #"DataSetArn": dataset['Arn']
                                }
                            ],
                            "Arn": templateArn
                            #"Arn": template['Arn']
                        }
                    }
                }

    logger.debug(f'analysis entry : {entry} ')

    if item is None:
        logger.info(f'analysis created in target account : {analysisName} ')
        response = quicksight.create_analysis(**entry)
    else:
        logger.info(f'analysis updated in target account : {analysisName} ')
        response = quicksight.update_analysis(**entry)

    logger.debug(f'Analysis  Creation result : {entry}')

    #Check status
    while True:

        item = getAnalysisById(quicksight, awsAccount , analysisId )

        logger.info(f"Status : {item['Status']} ")
        logger.debug(f'Status : {item} ')
        if 'Errors' in item and len(item['Errors']) > 0:
            logger.error(item['Errors'])
            exit(1)

        if item['Status'] == 'CREATION_SUCCESSFUL' or item['Status'] == 'UPDATE_SUCCESSFUL':
            break
        time.sleep(10)

    #update permission
    perm = getPermAnalisysTemplate(dstGroupName, awsAccount)

    logger.debug(f'Set Permission : {perm}')
    logger.info(f'Analysis  permission grant to: {dstGroupName}')
    response = quicksight.update_analysis_permissions( AwsAccountId=awsAccount,
                                                        AnalysisId=analysisId,
                                                        GrantPermissions=perm)
    # CHECK Result
    check_status(response)

def setDashboard(quicksight, dashboardName, templateArn, datasetArn, awsAccount , dstGroupName, dashboardVer, comment):
    item = None
    dashboardId = hashlib.sha224(dashboardName.encode()).hexdigest()
    #dashboardDstId =  dashboard['DashboardId']+"-"+target

    try:
        item = getDashboardId(quicksight, awsAccount, dashboardName)
        logger.info(f'Found dashboard in target account : {dashboardName}')
        logger.debug(f'Found Dashboard object : {item}')
    except botocore.exceptions.ClientError as error:
         if error.response['Error']['Code'] != 'ResourceNotFoundException':
            logger.info(f'Dashboard {dashboardName} not Found')

    # ADD some specific permissions?
    entry =  {
                    "AwsAccountId":awsAccount ,
                    "DashboardId": dashboardId,
                    "Name": dashboardName,
                    "SourceEntity": {
                        "SourceTemplate": {
                            "DataSetReferences": [
                                {
                                    "DataSetPlaceholder": "dataset_placeholder",
                                    "DataSetArn": datasetArn
                                    #"DataSetArn": dataset['Arn']
                                }
                            ],
                            "Arn": templateArn
                            #"Arn": template['Arn']
                        }
                    },
                    "VersionDescription": comment,
                    "DashboardPublishOptions": {
                        "AdHocFilteringOption": {
                            "AvailabilityStatus": "ENABLED"
                        },
                        "ExportToCSVOption": {
                            "AvailabilityStatus": "ENABLED"
                        },
                        "SheetControlsOption": {
                            "VisibilityState": "EXPANDED"
                        }
                    }
                }

    logger.debug(f'Dasboard entry : {entry} ')

    if item is None:
        logger.info(f'Dasboard created in target account : {dashboardName} ')
        entry = quicksight.create_dashboard(**entry)
    else:
        logger.info(f'Dasboard updated in target account : {dashboardName} ')
        entry = quicksight.update_dashboard(**entry)
        logger.info(f'Dasboard updated in target account : {dashboardName} ')

    logger.debug(f'Dashboard Creation result : {entry}')

    #FIXME check the response
    dstVer = int(entry['VersionArn'].split('/')[-1])

    #Check status
    while True:
        item =  getDashboardById(quicksight, awsAccount , dashboardId, int(dashboardVer) )
        logger.info(f"Status : {item['Version']['Status']} ")
        logger.debug(f'Status : {item} ')
        if 'Errors' in item['Version'] and len(item['Version']['Errors']) > 0:
            logger.error(item['Version']['Errors'])
            exit(1)

        if item['Version']['Status'] == 'CREATION_SUCCESSFUL' or item['Version']['Status'] == 'UPDATE_SUCCESSFUL':
            break
        time.sleep(10)

    response = quicksight.update_dashboard_published_version(AwsAccountId=awsAccount ,
                                                         DashboardId=dashboardId,
                                                         VersionNumber=int(dashboardVer))
    check_status(response)
    logger.info(f'Dasboard published in target account : {dashboardName} with version {dashboardVer} ')

    #update permission
    perm = getPermTemplate(dstGroupName, awsAccount)

    logger.debug(f'Set Permission : {perm}')
    logger.info(f'Dashboard permission grant to: {dstGroupName}')
    response = quicksight.update_dashboard_permissions( AwsAccountId=awsAccount,
                                                        DashboardId=dashboardId,
                                                        GrantPermissions=perm)
    # CHECK Result
    check_status(response)

def getPermTemplate(dstGroupName, awsAccountId):
    grpPerm = f"user/default/PA_DEVELOPER/{dstGroupName}"
    #update permission
    perm =  [
                {
                    "Principal": f"arn:aws:quicksight:eu-west-1:{awsAccountId}:{grpPerm}",
                    "Actions": [
                        "quicksight:DescribeDashboard",
                        "quicksight:ListDashboardVersions",
                        "quicksight:UpdateDashboardPermissions",
                        "quicksight:QueryDashboard",
                        "quicksight:UpdateDashboard",
                        "quicksight:DeleteDashboard",
                        "quicksight:DescribeDashboardPermissions",
                        "quicksight:UpdateDashboardPublishedVersion"
                    ]
                },
                {
                    "Principal": f"arn:aws:quicksight:eu-west-1:{awsAccountId}:namespace/default",
                    "Actions": [
                        "quicksight:DescribeDashboard",
                        "quicksight:ListDashboardVersions",
                        "quicksight:QueryDashboard"
                    ]
                }
            ]
    return perm

def getPermAnalisysTemplate(dstGroupName, awsAccountId):
    grpPerm = f"user/default/PA_DEVELOPER/{dstGroupName}"
    #update permission
    perm =  [
                {
                    "Principal": f"arn:aws:quicksight:eu-west-1:{awsAccountId}:{grpPerm}",
                    "Actions": [
                        "quicksight:RestoreAnalysis",
                        "quicksight:UpdateAnalysisPermissions",
                        "quicksight:DeleteAnalysis",
                        "quicksight:QueryAnalysis",
                        "quicksight:DescribeAnalysisPermissions",
                        "quicksight:DescribeAnalysis",
                        "quicksight:UpdateAnalysis"
                    ]
                },
                {
                    "Principal": f"arn:aws:quicksight:eu-west-1:{awsAccountId}:namespace/default",
                    "Actions": [
                        "quicksight:DescribeAnalysis",
                        "quicksight:QueryAnalysis"
                    ]
                }
            ]
    return perm

def getDataSetId(quicksight, awsAccountId, myDstDataSetName):
    data_set = quicksight.list_data_sets(AwsAccountId=awsAccountId)

    for item in data_set['DataSetSummaries']:
        if item['Name'] == myDstDataSetName:
            logger.info(f"Find Data set in target account : {item['Arn']}")
            logger.debug(f'Find Data set in target account : {item}')
            return item

    return None

def setTemplatePerm(quicksight,awsSrcAccount,awsDstAccount,templateId):

    if awsSrcAccount==awsDstAccount:
        logger.debug(f'Set permission skipped, source {awsSrcAccount} and destination {awsDstAccount} account are the same ')
        return

    entry = {'AwsAccountId':awsSrcAccount,
             'TemplateId':templateId,
             'GrantPermissions':[
                {
                    'Principal': f'arn:aws:iam::{awsDstAccount}:root',
                    #'Principal': f'arn:aws:iam::099769128691:root',
                    'Actions': [
                        "quicksight:DescribeTemplate",
                        "quicksight:UpdateTemplatePermissions"
                    ]
                 }
               ]
              }

    response = quicksight.update_template_permissions(**entry)

def getAwsAccount(profile):
    return profile.split('_')[0]

def getQskSession(myAWSProfile):
    session = boto3.session.Session(profile_name=myAWSProfile)
    return session.client('quicksight')

def printTable(title,data,fields):
    #FIXME moveto pretty table
    print(f'{title}')
    print('-------------------------------------------------------------------')
    for item in data:
        row = []
        for field in fields:
            row.append(f'{item[field]}\t')
        print(''.join(row))

#############################################################################
#  Commands
#############################################################################

def copyItem(config):
    global logger, quicksight

    #Open Conenctions
    logger.info(f'** {config.itemType} creation started ')
    awsSrcAccount = getAwsAccount(config.myAWSSrcProfile)
    quicksight[awsSrcAccount] = getQskSession(config.myAWSSrcProfile)
    awsDstAccount = getAwsAccount(config.myAWSDstProfile)
    quicksight[awsDstAccount] = getQskSession(config.myAWSDstProfile)

    if config.itemType == 'dashboard' :
        #Get Dashboard ID
        item = getDashboardId(quicksight[awsSrcAccount], awsSrcAccount,
                                        config.mySrcItemName)
        logger.info(f"Dashboard src ID : {item['DashboardId']}")
        #Get Dashboard description
        item = getDashboardById(quicksight[awsSrcAccount], awsSrcAccount,
                                        item['DashboardId'], int(config.mySrcItemVer) )

        dataSetSrcArn = item['Version']['DataSetArns'][0]
        entityArn  = item['Version']['SourceEntityArn']
    elif config.itemType == 'analysis':
        #Get Dashboard ID
        item = getAnalysisId(quicksight[awsSrcAccount], awsSrcAccount, config.mySrcItemName)
        logger.debug(f"Analysis  : {item}")
        item = getAnalysisById(quicksight[awsSrcAccount], awsSrcAccount,
                                        item['AnalysisId'])
        logger.debug(f"Analysis  : {item}")
        logger.info(f"Analysis src ID : {item['AnalysisId']}")
        dataSetSrcArn = item['DataSetArns'][0]
        entityArn  = item['Arn']
        config.mySrcItemVer = "1"

    #Create / Update template
    template = setTemplate(quicksight[awsSrcAccount],awsSrcAccount,config.mySrcItemName,
                                entityArn, dataSetSrcArn, config.mySrcItemVer)

    #set permissions to tempalte
    setTemplatePerm(quicksight[awsSrcAccount],awsSrcAccount,awsDstAccount,
                                template['TemplateId'])

    #Fetch dataset ARN
    dataSetDst = getDataSetId(quicksight[awsDstAccount], awsDstAccount,
                                config.myDstDataSetName)
    if dataSetDst is None:
        raise Exception(f'Destination dataset not found: {config.myDstDataSetName}')
    logger.info(f"Destination DataSet id : {dataSetDst['DataSetId']}")

    #Create / Update Item

    if config.myDstItemName is None:
        config.myDstItemName = config.mySrcItemName

    if config.myComment is None:
        config.myComment = '-'

    if config.itemType == 'dashboard' :
        setDashboard(quicksight[awsDstAccount], config.myDstItemName, template['Arn'], dataSetDst['Arn'], awsDstAccount,
                        config.myDstGroupName,config.mySrcItemVer,
                        config.myComment)
    elif config.itemType == 'analysis':
        setAnalysis(quicksight[awsDstAccount], config.myDstItemName, template['Arn'], dataSetDst['Arn'], awsDstAccount,
                        config.myDstGroupName, config.mySrcItemVer,
                        config.myComment)

    #FIXME movethe permission out of the dashboard Creation
    #setDashboardPerm()

    logger.info(f'**{config.itemType} creation Finished')

#
# LIST Section
###############################################################################
#FIXME build a decorator
def listDashboards(config):
    awsAccount = getAwsAccount(config.myAWSProfile)
    quicksight = getQskSession(config.myAWSProfile)
    data = quicksight.list_dashboards(AwsAccountId=awsAccount)

    printTable(f'Dashboard List of the account {awsAccount}',
                                data['DashboardSummaryList'] ,
                                ["DashboardId","Name","PublishedVersionNumber"])

def listTemplates(config):
    awsAccount = getAwsAccount(config.myAWSProfile)
    quicksight = getQskSession(config.myAWSProfile)
    data = quicksight.list_templates(AwsAccountId=awsAccount)

    printTable(f'Templates List of the account {awsAccount}',
                                data['TemplateSummaryList'] ,
                                ["TemplateId","Name","LatestVersionNumber"])

def listDataSets(config):
    awsAccount = getAwsAccount(config.myAWSProfile)
    quicksight = getQskSession(config.myAWSProfile)
    data = quicksight.list_data_sets(AwsAccountId=awsAccount)

    printTable(f'DataSet List of the account {awsAccount}',
                                data['DataSetSummaries'] ,
                                ["DataSetId","Name"])

def listAnalysis(config):
    awsAccount = getAwsAccount(config.myAWSProfile)
    quicksight = getQskSession(config.myAWSProfile)
    data = quicksight.list_analyses(AwsAccountId=awsAccount)

    printTable(f'Templates List of the account {awsAccount}',
                                    data['AnalysisSummaryList'] ,
                                    ["AnalysisId","Name","Status"])

#
# LIST Section
###############################################################################
def descDashboard(config):
    awsAccount = getAwsAccount(config.myAWSProfile)
    quicksight = getQskSession(config.myAWSProfile)
    dashboard = quicksight.describe_dashboard(AwsAccountId=awsAccount,
                                                   DashboardId=config.myItemId)
                                                   #VersionNumber=int(config.myVersion))

    pprint(data)

def descDataSet(config):
    awsAccount = getAwsAccount(config.myAWSProfile)
    quicksight = getQskSession(config.myAWSProfile)
    dataset = quicksight.describe_data_set(AwsAccountId=awsAccount,
                                                   DataSetId=config.myItemId,
                                                   )
    pprint(dataset)

def descTemplate(config):
    awsAccount = getAwsAccount(config.myAWSProfile)
    quicksight = getQskSession(config.myAWSProfile)
    template = quicksight.describe_template(AwsAccountId=awsAccount ,
                                                   TemplateId=config.myItemId,
                                                   )
    pprint(template)

def descAnalysis(config):
    awsAccount = getAwsAccount(config.myAWSProfile)
    quicksight = getQskSession(config.myAWSProfile)
    template = quicksight.describe_analysis(AwsAccountId=awsAccount ,
                                                   AnalysisId=config.myItemId,
                                                   )
    pprint(template)

##############################################################################
# MAIN
##############################################################################
quicksight = dict()
config = None
parser = argparse.ArgumentParser()

config = get_config(parser)
    #checkConfig(config,parser)

logger = init_logger("QuickSight", config.verbosity)

#Execute command
config.func(config)
#main()


'''Given a string s of '(' , ')' and lowercase English characters.

Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string.

Formally, a parentheses string is valid if and only if:

It is the empty string, contains only lowercase characters, or
It can be written as AB (A concatenated with B), where A and B are valid strings, or
It can be written as (A), where A is a valid string.

Example 1:

Input: s = "lee(t(c)o)de)"
Output: "lee(t(c)o)de"
Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
Example 2:

Input: s = "a)b(c)d"
Output: "ab(c)d"'''

from collections import deque

class Solution:
    def minRemoveToMakeValid(self, s):
        length=len(s)

        stack1=[]
        num_left=0
        num_right=0
        for i in range(length):
            if s[i]!='(' and s[i]!=')':
                stack1.append(s[i])
            elif s[i]=='(':
                stack1.append(s[i])
                num_left+=1
            else:
                if num_left>num_right:
                    stack1.append(s[i])
                    num_right+=1
        stack3=[]
        deque1=deque()

        length2=len(stack1)
        num_left=0
        num_right=0
        for i in range(-1,-length2-1,-1):
            if deque1==deque() and stack1[i]=='(':
                pass
            elif deque1!=deque() and  stack1[i]=='(' and num_left<num_right:
                deque1.appendleft(stack1[i])
                stack3.append(stack1[i])
                num_left+=1
            elif stack1[i]==')':
                stack3.append(stack1[i])
                deque1.appendleft(stack1[i])
                num_right+=1
            elif stack1[i]!='(':
                stack3.append(stack1[i])
        return ''.join(stack3[::-1])

Minimum Remove to Make Valid Parentheses.py


import numpy as np
import matplotlib.pyplot as plt

def l_Ir(ll_r,ll_m):
    print("ll_r")
    print(ll_r.shape)
    print("ll_m")
    print(ll_m)
    #ll_r_T=np.transpose(ll_r,(1,0))
    t0=np.matmul(ll_r.T,ll_r)
    #print(t0[np.nonzero(t0)])
    t1=np.linalg.inv(np.matmul(ll_r.T,ll_r))
    print("t1")
    print(t1)
    t2=np.matmul(t1,ll_r.T)
    print("t2")
    print(t2)
    ll_Ir=np.matmul(t2,ll_m)
    print("answer!")
    print(ll_Ir)
    return ll_Ir

#Data file visibilities will be pulled from
datams='sgr_apr07_flagcor.ms'
ms.open(datams,nomodify=True)

#Collect data from ms
visdata = ms.getdata(['antenna1','antenna2','data','data_desc_id','axis_info'],ifraxis=True)
visdata['data'] = np.squeeze(visdata['data'])
ms.close()

print(len(visdata['axis_info']['time_axis']['MJDseconds']))
print(len(np.unique(visdata['axis_info']['time_axis']['MJDseconds'])))

ELO=np.unique(visdata['axis_info']['time_axis']['MJDseconds'])
print(ELO)

allants=np.concatenate((visdata['antenna1'],visdata['antenna2']))
antlist=np.unique(allants)
#print(antlist)

#Calculating number of antennas and baselines
nant=int(len(antlist))
nbl=int(nant*(nant-1)/2)

print(nant)
print(nbl)
#print(len(np.unique(visdata['antenna1'])))
#print(len(np.unique(allants)))

#Defining Jacobian and measured phase matrix
script_L=np.zeros((nbl,nant,len(ELO)),dtype=int)
r_size=script_L.shape
l_m=np.zeros((nbl,1,len(ELO)),dtype=float)
anttrack=np.full(shape=(nbl,2,len(ELO)),fill_value=-1,dtype=int)
l_r=np.zeros((nbl,1,len(ELO)),dtype=float)

'''
for ant1 in np.unique(visdata['antenna1']):
    for ant2 in np.unique(visdata['antenna2']):
        if ant1 < ant2:
            thisbase = (visdata['antenna1']==ant1) & (visdata['antenna2']==ant2)
            #print(len(visdata['data'][0][thisbase]))
'''

#Setting integer for cycling through baselines
nb=0
refant=4
tb=0
for time in ELO:
    thistime=(visdata['axis_info']['time_axis']['MJDseconds']==time)
    itime=np.where(ELO==time)[0]
    print(itime)
    nb=0
    for ant1 in np.unique(visdata['antenna1']):
        for ant2 in np.unique(visdata['antenna2']):
            if ant1 < ant2:
                thisbase = (visdata['antenna1']==ant1) & (visdata['antenna2']==ant2)
                iant1=np.where(antlist==ant1)[0]
                iant2=np.where(antlist==ant2)[0]
                #print(iant1)
                #print(iant2)
                if thisbase.sum()>0:
                    print("length!")
                    #print(visdata['data'][0][thisbase][0][thistime][0])
                    pt=visdata['data'][0][thisbase][0][thistime][0]
                    #print(pt)
                    amp=np.absolute(pt)
                    print(amp)
                    if amp<=0:
                        #print("ah!")
                        nb+=1
                        continue
                    else:
                        l_m[nb,0,itime]=np.log10(amp)
                        anttrack[nb,0,itime]=ant1
                        anttrack[nb,1,itime]=ant2

                        script_L[nb,iant1,itime]=1
                        script_L[nb,iant2,itime]=1

                        nb+=1
                    '''
                    if ant2==4: Theta_r[nb,ant1]=1
                    if ant2!=4:
                        Theta_r[nb,ant1]=1
                        Theta_r[nb,ant2]=-1
                    '''

                    '''
                    if ant1==refant: script_L[nb,iant2-1]=1
                    if ant2==refant: script_L[nb,iant1]=1
                    if ant1!=refant and ant1>refant:
                        script_L[nb,iant1-1,itime]=1
                        script_L[nb,iant2-1,itime]=1
                    if ant1!=refant and ant2<refant:
                        script_L[nb,iant1,itime]=1
                        script_L[nb,iant2,itime]=1
                    if (ant1!=refant and (ant2>refant and ant1<refant)):
                        script_L[nb,iant1,itime]=1
                        script_L[nb,iant2-1,itime]=1
                    '''

    script_L_chunk=script_L[:,:,tb]
    l_m_chunk=l_m[:,:,tb]
    l_r[:,:,itime]=l_Ir(ll_r=script_L_chunk,ll_m=l_m_chunk)
    print(str(itime))
    tb+=1

print("script_L \n"+str(script_L))
print("l_m \n"+str(l_m))

#print(script_L[:,:,0].shape)
#print(l_m[:,:,0].shape)

l_r=[l_Ir(ll_r=script_L[:,:,int(x)],ll_m=l_m[:,:,int(x)]) for x in np.where(ELO==x)]
#t1=np.linalg.inv(np.matmul(Theta_r.T,Theta_r))
#t2=np.matmul(t1,Theta_r.T)
#theta_Ir=np.matmul(t2,theta_m)

print("l_r \n"+str(l_r))

#Residuals
l_del=l_m-np.matmul(script_L,l_r)

print("l_del \n"+str(l_del))

l_Ir_res=l_Ir(ll_r=script_L,ll_m=l_del)

print("l_Ir w/ resids: \n"+str(l_Ir_res))

l_Ir_final=l_r+l_Ir_res

print("final amps (log form) \n"+str(l_Ir_final))

Ir_converted=np.vstack([10.0**x for x in l_Ir_final])

print("final amps (converted) \n"+str(Ir_converted))

bpts=range(nant-1)
#print(len(bpts))
#print([:,0].shape)

plt.scatter(bpts,Ir_converted[:,0])
plt.show()


import numpy as np
import torch
from torch.distributions import Categorical
import torch.multiprocessing as mp
from worker import worker

class ParallelEnv:
    def __init__(self, n_workers):
        self.n_workers = n_workers
        self.workers = []

        self.master_ends, worker_ends = zip(*[mp.Pipe() for _ in range(n_workers)])

        for worker_end in worker_ends:
            p = mp.Process(target=worker, args=(worker_end,))
            p.daemon = True
            p.start()
            self.workers.append(p)

    def reset(self, level):
        for master_end in self.master_ends:
            master_end.send(('reset', level))
        return np.stack([master_end.recv() for master_end in self.master_ends])

    def step(self, actions):
        for master_end, action in zip(self.master_ends, actions):
            master_end.send(('step', action))

        results = [master_end.recv() for master_end in self.master_ends]
        observations, rewards, dones, infos = zip(*results)

        return np.stack(observations), np.stack(rewards), np.stack(dones), infos

    def close(self):
        for master_end in self.master_ends:
            master_end.send(('close', None))
        for worker in self.workers:
            worker.join()

    def solve(self, level, model):
        observation = torch.tensor(self.reset(level))
        actions = torch.zeros((self.n_workers, 300), dtype=torch.uint8)

        for t in range(300):
            with torch.no_grad():
                pi, _ = model(observation)

                p = Categorical(pi)
                action = p.sample()
                actions[:, t] = action

            observation, reward, done, info = self.step(action.tolist())
            observation = torch.tensor(observation)
            dones = [env["finished"] for env in info]

            if any(dones) == True:
                index = [i for i,done in enumerate(dones) if done == True][0]

                return True, actions[index, 0:t+1]

        return False, actions[0]


import sys
import numpy as np
import joblib as jl
import pandas as pd
import matplotlib.pyplot as plt

# interpreter  python 3.7.2

# filename = 'model_NBC.sav'
# filename = '221105NBC.json'
filename = '221105NBC_2.pkl'
loaded_model = jl.load(filename)

# load models, test and return result
def pose_classification (angles) :
    angles_p = [angles] # input is 2d array!!
    # print(angles_p, type(angles_p))
    result = list(loaded_model.predict(angles_p))[0]
    return result

def main () :
    #get parameter
    ag = list(map(float, sys.argv[1:]))
    data = pose_classification(ag)
    # print("result : ", result, type(result))
    print(data)
    return data

if __name__ == "__main__" :
    main()


from collections import defaultdict
import time

import pytest

from mos_tests.functions import common
from mos_tests.functions import os_cli

@pytest.mark.check_env_('has_ironic_conductor')
@pytest.mark.need_devops
@pytest.mark.testrail_id('631921', params={'boot_instance_before': False})
@pytest.mark.testrail_id('631922', params={'boot_instance_before': True})
@pytest.mark.parametrize('boot_instance_before', [True, False],
                         ids=['boot_instance_before', 'boot_instance_after'])
def test_reboot_conductor(env, ironic, os_conn, ironic_nodes, make_image,
                          flavors, keypair, devops_env, boot_instance_before):
    """Check ironic state after restart conductor node

    Scenario:
        1. Boot Ironic instance (if `boot_instance_before`)
        2. Reboot Ironic conductor.
        3. Wait 5-10 minutes.
        4. Run network verification.
        5. Run OSTF including Ironic tests.
        6. Verify that CLI ironicclient can list nodes, ports, chassis, drivers
        7. Boot new Ironic instance (if not `boot_instance_before`).
    """
    flavor, ironic_node = zip(flavors, ironic_nodes)[0]
    image = make_image(node_driver=ironic_node.driver)
    if boot_instance_before:
        instance = ironic.boot_instance(image=image,
                                        flavor=flavor,
                                        keypair=keypair)

    conductor = env.get_nodes_by_role('ironic')[0]

    devops_node = devops_env.get_node_by_fuel_node(conductor)
    devops_node.reset()

    time.sleep(10)

    common.wait(conductor.is_ssh_avaliable,
                timeout_seconds=60 * 10,
                sleep_seconds=20,
                waiting_for='ironic conductor node to reboot')

    def is_ironic_available():
        try:
            ironic.client.driver.list()
            return True
        except Exception:
            return False

    common.wait(is_ironic_available,
                timeout_seconds=60 * 5,
                sleep_seconds=20,
                waiting_for='ironic conductor service to start')

    result = env.wait_network_verification()
    assert result.status == 'ready', 'Result data:\n{0}'.format(result.data)

    env.wait_for_ostf_pass(['sanity'], timeout_seconds=60 * 5)

    with env.get_nodes_by_role('controller')[0].ssh() as remote:
        ironic_cli = os_cli.Ironic(remote)
        for cmd in ['node-list', 'port-list', 'chassis-list', 'driver-list']:
            ironic_cli(cmd)

    if not boot_instance_before:
        instance = ironic.boot_instance(image=image,
                                        flavor=flavor,
                                        keypair=keypair)

    assert os_conn.nova.servers.get(instance.id).status == 'ACTIVE'

@pytest.mark.check_env_('has_2_or_more_ironic_conductors')
@pytest.mark.need_devops
@pytest.mark.testrail_id('638353')
def test_reboot_all_ironic_conductors(env, devops_env):
    """Check ironic state after restart all conductor nodes

    Scenario:
        1. Ensure ironic conductor service works correctly on each conductor
            node (e.g., ensure "ironic driver-list" returns correct list of
            drivers)
        2. Shutdown all ironic conductor nodes
        3. Turn on all ironic conductor nodes
        4. SSH to controller and ensure that "ironic driver-list" return same
            result as for step 1
    """
    controller = env.get_nodes_by_role('controller')[0]
    with controller.ssh() as remote:
        driver_hosts = os_cli.Ironic(remote).get_driver_hosts()

    conductors = env.get_nodes_by_role('ironic')
    conductor_hosts = set(node.data['fqdn'] for node in conductors)

    assert conductor_hosts == driver_hosts
    devops_nodes = [devops_env.get_node_by_fuel_node(x) for x in conductors]

    for node in devops_nodes:
        node.destroy()

    for node in devops_nodes:
        node.start()

    common.wait(lambda: all(x.is_ssh_avaliable() for x in conductors),
                timeout_seconds=10 * 60,
                waiting_for='conductor nodes to boot')

    with controller.ssh() as remote:
        ironic_cli = os_cli.Ironic(remote)
        common.wait(lambda: ironic_cli.get_driver_hosts() == conductor_hosts,
                    timeout_seconds=60 * 2,
                    waiting_for="ironic conductor to register their drivers")

@pytest.mark.check_env_('has_2_or_more_ironic_conductors')
@pytest.mark.need_devops
@pytest.mark.testrail_id('675246')
def test_kill_conductor_service(env, os_conn, ironic_nodes, make_image,
                                flavors, keypair, env_name, ironic):
    """Kill ironic-conductor service with one bare-metal node

    Scenario:
        1. Launch baremetal instance
        2. Kill Ironic-conductor service for conductor node that had booted
            instance
        3. Wait some time
        4. Baremetal node must be reassigned to another Ironic-conductor
        5. Run OSTF including Ironic tests.
        6. Check that Ironic instance still ACTIVE and operable

    Info: https://bugs.launchpad.net/mos/+bug/1557464
    """
    flavor, ironic_node = zip(flavors, ironic_nodes)[0]
    image = make_image(node_driver=ironic_node.driver)

    def find_conductor_node(ironic_node_uuid, conductors):
        cmd = 'ls /var/log/remote/ironic/{0}/'.format(ironic_node_uuid)
        for conductor in conductors:
            with conductor.ssh() as remote:
                result = remote.execute(cmd)
                if result.is_ok:
                    return conductor

    def find_takeover_node(ironic_node_uuid, conductors):
        grep = 'grep "taking over node {inst_uid}" {log}'.format(
            inst_uid=ironic_node_uuid,
            log='/var/log/ironic/ironic-conductor.log')
        for conductor in conductors:
            with conductor.ssh() as remote:
                result = remote.execute(grep)
                if result.is_ok:
                    return conductor

    instance = ironic.boot_instance(image=image,
                                    flavor=flavor,
                                    keypair=keypair)

    conductors = env.get_nodes_by_role('ironic')
    conductor = find_conductor_node(ironic_node.uuid, conductors)
    if conductor is None:
        raise Exception("Can't find conductor node booted istance")

    with conductor.ssh() as remote:
        remote.check_call('service ironic-conductor stop')

    conductors.remove(conductor)
    common.wait(
        lambda: (find_takeover_node(ironic_node.uuid, conductors)
                 not in (conductor, None)),
        timeout_seconds=10 * 60,
        waiting_for='node to migrate to another conductor',
        sleep_seconds=60)

    assert os_conn.nova.servers.get(instance.id).status == 'ACTIVE'

    with os_conn.ssh_to_instance(env,
                                 instance,
                                 vm_keypair=keypair,
                                 username='ubuntu') as remote:
        remote.check_call('uname')

@pytest.mark.testrail_id('1295473')
@pytest.mark.check_env_('has_ironic_conductor')
@pytest.mark.parametrize('ironic_nodes', [2], indirect=True)
@pytest.mark.need_devops
def test_restart_all_ironic_services(env, os_conn, ironic_nodes, ironic,
                                     make_image, flavors, keypair):
    """Test ironic after restarting all ironic services

    Scenario:
        1. Launch baremetal instance 'test1'
        2. Check that 'test1' instance ACTIVE and operable
        3. Stop all ironic-<name> services on each controllers and ironic
            nodes:
            service ironic-<name of service> restart
        4. Check that 'test1' instance are accesible with SSH
        5. Launch all ironic services
        6. Launch baremetal instance 'test2'
        7. Check that 'test1' instance ACTIVE and operable
        8. Delete created instances
    """
    image = make_image(node_driver=ironic_nodes[0].driver)

    instance1 = ironic.boot_instance(name='test1',
                                     image=image,
                                     flavor=flavors[0],
                                     keypair=keypair)

    nodes = (set(env.get_nodes_by_role('controller')) |
             set(env.get_nodes_by_role('ironic')))
    ironic_services_cmd = ("service --status-all 2>&1 | grep '+' | "
                           "grep ironic | awk '{ print $4 }'")

    stopped_services = defaultdict(set)
    for node in nodes:
        with node.ssh() as remote:
            output = remote.check_call(ironic_services_cmd).stdout_string
            for service in output.splitlines():
                remote.check_call('service {0} stop'.format(service))
                stopped_services[node].add(service)

    assert os_conn.is_server_ssh_ready(instance1)

    for node, services in stopped_services.items():
        with node.ssh() as remote:
            for service in services:
                remote.check_call('service {0} start'.format(service))

    instance2 = ironic.boot_instance(name='test2',
                                     image=image,
                                     flavor=flavors[1],
                                     keypair=keypair)

    instance1.delete()
    instance2.delete()

    os_conn.wait_servers_deleted([instance1, instance2])


from os import path
from codecs import open

from setuptools import setup, find_packages

HERE = path.abspath(path.dirname(__file__))
PROMOTER_VER = '1.0.1'

setup(
    name='django-promoter',

    version=PROMOTER_VER,

    description='Django commands used to promote or demote users',
    long_description=open('README.rst').read(),

    url='https://github.com/numberly/django-promoter',

    author='Theo Massard',
    author_email='theo.massard@1000mercis.com',

    license='MIT',

    classifiers=[
        'Development Status :: 5 - Production/Stable',

        'Environment :: Console',

        'Framework :: Django',

        'Intended Audience :: Developers',
        'Intended Audience :: System Administrators',

        'License :: OSI Approved :: MIT License',

        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.5',
    ],

    keywords='django auth admin cli python3.5',

    packages=find_packages(),
)


'''
Created on June 5, 2018

@author: perej1
'''
import numpy as np
import pandas as pd
import argparse as arg
import matplotlib.pyplot as plt

def pplot(n1, n2, s, f1, f2, r, g, d):
    '''
    function for plotting Hill estimator convergence in probability.
    n1 = smallest sample size
    n2 = largest sample size
    s = step
    f1 = file path for data
    f2 = file path and name for the plot
    r = number of estimates in one csv
    g = true value of the extreme value index
    d = distribution
    '''
    q_1 = np.array([])
    q_2 = np.array([])
    q_3 = np.array([])
    n = np.arange(n1, n2+1, s)
    val = g * np.ones(n.shape)

    for i in n:
        k = int(np.floor(np.sqrt(i)))
        data_i = pd.read_csv("%s/%s-%s-%s-%s.csv" % (f1, i, r, k, d))
        q_1 = np.append(q_1, data_i[d].quantile(0.25))
        q_2 = np.append(q_2, data_i[d].median())
        q_3 = np.append(q_3, data_i[d].quantile(0.75))

    fig, ax = plt.subplots()
    plt.rcParams['font.sans-serif'] = 'Times New Roman'
    plt.rcParams["font.family"] = 'Times New Roman'
    ax.set_xlim(left=0, right=20000)
    ax.plot(n, q_1, '-r', lw=1, label='1st quartile')
    ax.plot(n, q_2, '-k', lw=1, label='median')
    ax.plot(n, q_3, '-b', lw=1, label='3rd quartile')
    ax.plot(n, val, '--g', alpha=0.75, lw=1, label=r'$ \gamma $')
    ax.set_ylabel(r'$\hat{\gamma}$')
    ax.set_xlabel('n')
    ax.legend()
    ax.set_title('Hill estimates with samples from %s distribution' % d.capitalize())
    plt.savefig(f2, format='pdf', dpi=1000)

def main():

    parser = arg.ArgumentParser(
        description="Plots graphs about Hill estimator"
    )
    parser.add_argument("-n1", help="lower bound", type=int)
    parser.add_argument("-n2", help="upper bound", type=int)
    parser.add_argument("-s", help="step", type=int)
    parser.add_argument("-f1", help="data file path", type=str)
    parser.add_argument("-f2", help="fig file path", type=str)
    parser.add_argument("-r", help="sample size", type=int)
    parser.add_argument("-p", help="plot type", type=str, choices=("pplot"))
    parser.add_argument("-g", help="extreme value index", type=float)
    parser.add_argument("-d", help="distribution", type=str,
                        choices=("pareto", "cauchy")
                        )

    args = parser.parse_args()

    if args.p == "pplot":
        pplot(args.n1, args.n2, args.s,
              args.f1, args.f2, args.r, args.g, args.d)

if __name__ == "__main__":
    main()


from PIL import Image, ImageDraw
from math import sin, cos, pi
import sys

def __strip_non_directives(inp):
	return [x for x in inp if x in 'F+-']

def make_image(raw_data, iters, growth, size):
	scale = pow(growth, iters)
	data =__strip_non_directives(raw_data)
	direction = 0

	move_dist = scale*size
	#loc = size*scale, size*scale
	loc = 0,0

	output = Image.new('RGBA', (size, size), (255,255,255))
	draw = ImageDraw.Draw(output)

	for rule in data:

		if rule == 'F':
			tmp_loc = loc[0] + move_dist*cos(direction), \
					  loc[1] + move_dist*sin(direction)

			draw.line((loc, tmp_loc), fill = (128, 128, 128))
			loc = tmp_loc
		elif rule == '+':
			direction -= pi/2
		elif rule == '-':
			direction += pi/2
		else:
			sys.stdout.write('This should never happen.\n')

	del draw
	return output


#!/usr/bin/env python3

import socket
import argparse
import sys
import json
from threading import Thread
from hub.base.hipc import HIPCRequestSerializer
from hub.base import jsonrpc

sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect("/tmp/hub_sock")

def no_exit(self, status=0, message=None):
    if message:
        self._print_message(message, sys.stderr)

argparse.ArgumentParser.exit = no_exit

def sendcmd():
    while True:
        cmd = input(">>> ")
        if len(cmd.split()) == 0:
            continue
        if cmd.split()[0].strip() == "add_device":

            print("请输入设备的属性")
            vender = input("厂商：")
            uniqid = input("唯一ID：")
            hwversion = input("硬件版本：")
            swversion = input("软件版本：")
            dtype = input("类型：")
            operations = input("操作：")
            operations = operations.split()

            dev = {
                "vender": vender,
                "uniqid": uniqid,
                "hwversion": hwversion,
                "swversion": swversion,
                "type": dtype,
                "operations": operations
            }

            body = jsonrpc.RequestBuilder(method = "add_device", rpcid = 1, params = dev).build()

            ser = HIPCRequestSerializer("device")
            ser.set_body(body)
            sock.send(ser.get_binary())

        elif cmd.split()[0].strip() == "remove_device":

            parser = argparse.ArgumentParser()
            parser.add_argument("-d", dest="devid", type=int, required=True, help="the device id")
            args = None
            try:
                args = parser.parse_args(cmd.split()[1:])
            except Exception:
                del args
                continue

            ser = HIPCRequestSerializer("device")
            ser.set_body('{"jsonrpc": "2.0","method": "remove_device","params":{"id": ' + str(args.devid) + '},"id": 1}')
            sock.send(ser.get_binary())

            del args

        elif cmd.split()[0].strip() == "get_devices":

            ser = HIPCRequestSerializer("device")
            ser.set_body('{"jsonrpc": "2.0","method": "get_devices","id": 1}')
            sock.send(ser.get_binary())

        elif cmd.split()[0].strip() == "get_name":

            parser = argparse.ArgumentParser()
            parser.add_argument("-d", dest="devid", type=int, required=True, help="设备的id")
            args = None
            try:
                args = parser.parse_args(cmd.split()[1:])
            except Exception:
                del args
                continue

            ser = HIPCRequestSerializer("device/" + str(args.devid))
            ser.set_body('{"jsonrpc": "2.0","method": "get_name","params":{"id": ' + str(args.devid) + '},"id": 1}')
            sock.send(ser.get_binary())

            del args

        elif cmd.split()[0].strip() == "set_name":

            parser = argparse.ArgumentParser()
            parser.add_argument("-d", dest="devid", type=int, required=True, help="the device id")
            parser.add_argument("-n", dest="name", type=str, required=True, help="要设置的名称")
            args = None
            try:
                args = parser.parse_args(cmd.split()[1:])
            except Exception:
                del args
                continue
            ser = HIPCRequestSerializer("device/" + str(args.devid))
            ser.set_body('{"jsonrpc": "2.0","method": "set_name","params":{"name": "' + str(args.name) + '"},"id": 1}')
            sock.send(ser.get_binary())

            del args

        elif cmd.split()[0].strip() == "get_position":

            parser = argparse.ArgumentParser()
            parser.add_argument("-d", dest="devid", type=int, required=True, help="the device id")
            args = None
            try:
                args = parser.parse_args(cmd.split()[1:])
            except Exception:
                del args
                continue

            ser = HIPCRequestSerializer("device/" + str(args.devid))
            ser.set_body('{"jsonrpc": "2.0","method": "get_position","id": 1}')
            sock.send(ser.get_binary())

            del args

        elif cmd.split()[0].strip() == "set_position":

            parser = argparse.ArgumentParser()
            parser.add_argument("-d", dest="devid", type=int, required=True, help="the device id")
            parser.add_argument("-p", dest="position", type=str, required=True, help="要设置的位置")
            args = None
            try:
                args = parser.parse_args(cmd.split()[1:])
            except Exception:
                del args
                continue
            else:
                ser = HIPCRequestSerializer("device/" + str(args.devid))
                ser.set_body('{"jsonrpc": "2.0","method": "set_position","params":{"position": "' + str(args.position) + '"},"id": 1}')
                sock.send(ser.get_binary())
                del args

def display():
    ser = HIPCRequestSerializer("component")
    ser.set_body('{"jsonrpc": "2.0","method": "register_component","params":{"name": "zigbee link adapter", "type": "ZigbeeLinkAdapter"},"id": 1}')
    sock.send(ser.get_binary())
    while True:
        data = sock.recv(100)
        print(data)

dth = Thread(target = display)
dth.start()

sth = Thread(target = sendcmd)
sth.start()


from django.shortcuts import render
from django.http import HttpResponse
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from category.models import Category
from category.serializers import CategorySerializer

@api_view(['GET'])
def category_collection(request):
    if request.method == 'GET':
        categories = Category.objects.all()
        serializer = CategorySerializer(categories, many=True)
        return Response(serializer.data)

@api_view(['GET'])
def category_element(request, pk):
    try:
        category = Category.objects.get(pk=pk)
    except Category.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = CategorySerializer(Category)
        return Response(serializer.data)

@api_view(['POST'])
def category_create(request):
    if request.method == 'POST':

        data = {'name': request.DATA.get('name'),
        }

        serializer = CategorySerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


"""Support for collecting sensors from 4-noks devices."""

import async_timeout
import datetime
import logging
import time

from datetime import timedelta
from homeassistant.components.sensor import (SensorDeviceClass,
                                             SensorEntity,
                                             SensorStateClass)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (CONF_HOST, CONF_NAME)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import event
from homeassistant.helpers.update_coordinator import (
    CoordinatorEntity,
    DataUpdateCoordinator,
    UpdateFailed,
)

from .common import get_dat
from .const import (CONF_SERIAL, DEFAULT_BRAND, DOMAIN)

_LOGGER = logging.getLogger(__name__)

async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_entities):
    """Set up energy sensors."""

    async def async_update_data():
        """Fetch data from socket endpoint."""

        async with async_timeout.timeout(10):
            dat = await hass.async_add_executor_job(get_dat, config.data, False)
            if dat is None:
                _LOGGER.error("No data available")

        return dat

    coordinator = DataUpdateCoordinator(
        hass,
        _LOGGER,
        # Name of the data. For logging purposes.
        name="4-noks sensor",
        update_method=async_update_data,
        # Polling interval. Will only be polled if there are subscribers.
        update_interval=timedelta(seconds=60),
    )

    # Get a list of sensors and units of measurement
    dat = await hass.async_add_executor_job(get_dat, config.data, True)

    entities = []
    for name, um in dat.items():
        entities.append(FourNOKSSensor(coordinator, config, name, um))

    async_add_entities(entities)

    # Fetch initial data so we have data when entities subscribe
    await coordinator.async_refresh()

class FourNOKSSensor(CoordinatorEntity, SensorEntity):
    """Representation of a 4-noks sensor."""

    def __init__(self, coordinator, config, sensor_name, sensor_um):
        """Initialize the sensor."""
        super().__init__(coordinator)
        self._device_name = config.data[CONF_NAME]
        self._serial = config.data[CONF_SERIAL]

        self._basename = sensor_name
        self._fullname = self._device_name + "_" + self._basename
        self._name = self._device_name + "_" + self._basename.lower().replace(' ', '_')
        self._unique_id = self._name
        self._um = sensor_um
        if (self._um.lower() == "kw"):
            self._device_class = SensorDeviceClass.POWER
            self._state_class = SensorStateClass.MEASUREMENT
        elif (self._um.lower() == "kwh"):
            self._device_class = SensorDeviceClass.ENERGY
            self._state_class = SensorStateClass.TOTAL_INCREASING
        elif (self._um.lower() == "dd/mm/yyyy hh:mm:ss"):
            self._um = ""
            self._device_class = SensorDeviceClass.DATE
#            self._device_class = None
            self._state_class = None
        else:
            self._device_class = None
            self._state_class = None

    @property
    def name(self):
        """Return the name of the sensor."""
        return self._name

    @property
    def native_value(self):
        """Return the value of the sensor."""
        if self.coordinator.data is not None:
            return self.coordinator.data[self._basename]

    @property
    def native_unit_of_measurement(self):
        """Return the unit of measurement of the sensor, if any."""
        return self._um

    @property
    def available(self) -> bool:
        """Return True if entity is available."""
        if not self.coordinator.last_update_success:
            return False

        return True

    @property
    def unique_id(self):
        """Return a unique ID."""
        return self._unique_id

    @property
    def device_class(self):
        """Return the icon to use in the frontend."""
        return self._device_class

    @property
    def state_class(self):
        """Return state class."""
        return self._state_class

    @property
    def device_info(self):
        """Return device specific attributes."""
        return {
            "name": self._device_name,
            "identifiers": {(DOMAIN, self._serial)},
            "manufacturer": DEFAULT_BRAND,
            "model": DOMAIN,
        }


#!/usr/bin/python

import sys
import pymongo

### Create location data

def list_locations(mydb):

    for doc in mydb.find():
        print(doc) # iterate the cursor

###############################################################################
# main
###############################################################################

def main(args):

    connection = pymongo.MongoClient('ds121861.mlab.com', 21861)
    db = connection['python_twitter']

    db.authenticate('test', 'nnnn1858')

    dont_delete = []
    dont_delete.append('system.indexes')
    print(dont_delete)
    for col in db.collection_names():

        #print(col not in dont_delete)
        if (col not in dont_delete):
            print('deleting', col)
            db.drop_collection(col)

    connection.close()

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


from django.conf.urls import url

from . import views

urlpatterns = [
            url(r'^fort$', views.fort, name='fort'),
            url(r'^pokestop$', views.pokestop, name='pokestop'),
            url(r'^gym$', views.gym, name='gym'),
            url(r'^pokemon$', views.pokemon, name='pokemon'),
            ]


from flask import Blueprint, jsonify, request
from app.models import Category, db, Project, User

category_routes = Blueprint('categories', __name__)

@category_routes.route('/')
def categories():
    categories = Category.query.all()
    return {'categories': [category.to_dict() for category in categories]}

@category_routes.route('/<int:id>')
def get_category(id):
    categories = Category.query.filter(Project.category_id == id).all()
    categoryList = []
    for category in categories:
        categoryList.append(category.to_dict())
    return jsonify(categoryList)

@category_routes.route('/project/<int:id>')
def get_project_category(id):
    projects = Project.query.filter(Project.category_id == id).all()
    projectList = []
    for project in projects:
        projectList.append(project.to_dict())
    return jsonify(projectList)


from django.urls import include, path
from rest_framework.authtoken.views import obtain_auth_token
from rest_framework.routers import DefaultRouter

from .views import CommentViewSet, PostViewSet

v1_router = DefaultRouter()
v1_router.register(r'posts', PostViewSet, basename='Post')
v1_router.register(r'posts/(?P<id>\d+)/comments', CommentViewSet,
                   basename='Comment')

api_patterns = [
    path('v1/api-token-auth/', obtain_auth_token),
    path('v1/', include(v1_router.urls))
]

urlpatterns = [
    path('api/', include(api_patterns)),
]