import json

import torch
from sklearn.metrics import precision_recall_fscore_support
from torch.utils.data import Dataset, DataLoader
import os
import logging
from collections import Counter
import numpy as np
import subprocess
import random
import argparse
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import auc

from myutils import read_file, write_file

UNK='UNK'

class PaddedTensorDataset(Dataset):
    """Dataset wrapping data, target and length tensors.
    Each sample will be retrieved by indexing both tensors along the first
    dimension.
    Arguments:
        data_tensor (Tensor): contains sample data.
        target_tensor (Tensor): contains sample targets (labels).
        length (Tensor): contains sample lengths.
        raw_data (Any): The data that has been transformed into tensor, useful for debugging
    """

    def __init__(self, data_tensor, target_tensor, length_tensor, raw_data, tids):
        assert data_tensor.size(0) == target_tensor.size(0) == length_tensor.size(0)
        self.data_tensor = data_tensor
        self.target_tensor = target_tensor
        self.length_tensor = length_tensor
        self.raw_data = raw_data
        self.tids = tids

    def __getitem__(self, index):
        return self.data_tensor[index], self.target_tensor[index], self.length_tensor[index], self.raw_data[index], self.tids[index]

    def __len__(self):
        return self.data_tensor.size(0)

def setup_logging(exp_path='.', logfile='log.txt'):
    # create a logger and set parameters
    logfile = os.path.join(exp_path, logfile)
    logFormatter = logging.Formatter("%(asctime)s [%(levelname)-5.5s]  %(message)s")
    rootLogger = logging.getLogger()
    rootLogger.setLevel(logging.DEBUG)
    fileHandler = logging.FileHandler(logfile)
    fileHandler.setFormatter(logFormatter)
    rootLogger.addHandler(fileHandler)
    consoleHandler = logging.StreamHandler()
    consoleHandler.setFormatter(logFormatter)
    rootLogger.addHandler(consoleHandler)

def print_class_distributions(labels):
    c = Counter(labels)
    s = ''
    for key, val in c.most_common():
        s+= '#seqs with label {}: {} ({:.2f}%)\n'.format(key, val, float(val)/len(labels)*100)
    return s

def upsample(seqs, labels):

    # upsample the data, such that the number of instances for each label is appr. the same
    c = Counter(labels)

    target_num = c.most_common(1)[0][1]
    labelset = c.keys()
    upsampled_idxs = []
    for target_label in labelset:
        print(target_num)
        print(c[target_label])
        idxs = [i for i in range(len(seqs)) if labels[i] == target_label]
        upsampled_idxs += list(np.random.choice(idxs, target_num-c[target_label]))

    seqs += [seqs[i] for i in upsampled_idxs]
    labels += [labels[i] for i in upsampled_idxs]
    return seqs, labels

def save_json(fname, data):
    with open(fname, 'w', encoding='utf-8') as f:
        json.dump(data, f)

def load_json(fname):
    with open(fname, 'r', encoding='utf-8') as f:
        j = json.load(f)
    return j

def prepare_labels(labels, labelset, binary=False):

    # if binary is True, make a binary labelset with {'1', '2'} --> '1' and '3' --> '0'
    if binary is True:
        labels = ['0' if label == '3' else '1' for label in labels]
    if labelset is None:
        labelset = list(set(labels))
    labelset = sorted(labelset)
    prepared_labels = []
    for label in labels:
        prepared_labels.append(labelset.index(label))
    return torch.LongTensor(prepared_labels), labelset

def sort_batch(batch, targets, lengths):
    seq_lengths, perm_idx = lengths.sort(0, descending=True)
    seq_tensor = batch[perm_idx]
    target_tensor = targets[perm_idx]
    return seq_tensor, target_tensor, seq_lengths

def get_tokens(line, lower):
    if lower:
        return [elm.strip().lower() for elm in line.strip().split()]
    else:
        return [elm.strip() for elm in line.strip().split()]

def get_chars(line, lower):
    if lower:
        line = line.lower()
    return [c for c in line]

def compute_dim_feature_map(len_in, kernel_size, stride, padding, dilation):
    """
    computes the kernel size for the pytorch pooling layer, such that the output has dimensionality out_dim
    :return:
    """
    out = ((len_in + 2*padding - dilation*(kernel_size - 1) -1)/float(stride)) + 1
    return np.ceil(out)

def compute_kernel_size(len_in, len_out, stride, padding, dilation):
    out = (len_in + 2*padding - 1 -(len_out-1)*stride + dilation)/dilation
    return np.ceil(out)

def pad_sequences(vectorized_seqs, seq_lengths):
    # create a zero matrix
    seq_tensor = torch.zeros((len(vectorized_seqs), seq_lengths.max())).long()
    for idx, (seq, seqlen) in enumerate(zip(vectorized_seqs, seq_lengths)):
        seq_tensor[idx, :seqlen] = torch.LongTensor(seq)
    return seq_tensor

def seqs2minibatches(seqs, golds, lengths, raw_sents, batch_size, tids=[]):
    padded_seqs = pad_sequences(seqs, lengths)
    if len(tids) == 0:
        tids = [0 for elm in raw_sents]
    return DataLoader(PaddedTensorDataset(padded_seqs, golds, lengths, raw_sents, tids), batch_size=batch_size)

def sents2seqs(sents, feat2idx, lower=True):
    """
        map sequences of words to sequences of indexes, pad the sequences and sort the minibatches according to lentgh
        :param sents:
        :param feat2idx:
        :param lower:
        :return:
        """
    if feat2idx is None:
        vocab = Counter()
        logging.info('Building vocabulary')
        for sent in sents:
            vocab.update(Counter(get_tokens(sent, lower=lower)))
        if '' in vocab.keys():
            del vocab['']
        feat2idx = dict()
        words = sorted(list(vocab.keys()))
        for word in words:
            feat2idx[word] = len(feat2idx)
    # add the unk token
    if UNK not in feat2idx:
        feat2idx[UNK] = len(feat2idx)
    logging.info('Mapping sentences to sequences')
    seqs = []
    lengths = []
    for sent in sents:
        if len(get_tokens(sent, lower=lower)) == 0:
            seq = [feat2idx[UNK]]
        else:
            seq = [feat2idx[tok] if tok in feat2idx else feat2idx[UNK] for tok in get_tokens(sent, lower=lower)]
        seqs.append(seq)
        lengths.append(len(seq))
    lengths = torch.LongTensor(lengths)
    return seqs, lengths, feat2idx

def sents2charseqs(sents, feat2idx, lower=True):
    """
    map sequences of chracters to sequences of character indexes, pad the sequences and sort the minibatches according to lentgh
    :param sents:
    :param word2idx:
    :param lower:
    :return:
    """
    if feat2idx is None:
        vocab = Counter()
        logging.info('Building char vocabulary')
        for sent in sents:
            vocab.update(Counter(get_chars(sent, lower=lower)))
        if '' in vocab.keys():
            del vocab['']
        feat2idx = dict()
        chars = sorted(list(vocab.keys()))
        for char in chars:
            feat2idx[char] = len(feat2idx)
    if not UNK in feat2idx:
        feat2idx[UNK] = len(feat2idx)
    logging.info('Mapping sentences to char sequences')
    seqs = []
    lengths = []
    for sent in sents:
        if len(get_chars(sent, lower=lower)) == 0:
            seq = [feat2idx[UNK]]
        else:
            seq = [feat2idx[tok] if tok in feat2idx else feat2idx[UNK] for tok in get_chars(sent, lower=lower)]
        seqs.append(seq)
        lengths.append(len(seq))
    lengths = torch.LongTensor(lengths)
    return seqs, lengths, feat2idx

def per_class_p_r(cm):
    for i in range(cm.shape[0]):
        p = float(cm[i,i])/(sum(cm[:,i]))
        r = float(cm[i, i]) / (sum(cm[i, :]))
        print('%d Prec: %.2f Rec:%.2f'%(i+1, p*100, r*100))

def load_embeddings_from_file(fname, max_vocab=-1):
    """
    Reload pretrained embeddings from a text file.
    """
    word2id = {}
    vectors = []

    # load pretrained embeddings
    with open(fname, 'r', encoding='utf-8') as f:
        for i, line in enumerate(f):
            if i == 0 and len(line.split()) == 2:
                continue
            else:
                word, vect = line.rstrip().split(' ', 1)

                vect = np.fromstring(vect, sep=' ')
                if np.linalg.norm(vect) == 0:  # avoid to have null embeddings
                    vect[0] = 0.01
                assert word not in word2id
                word2id[word] = len(word2id)
                vectors.append(vect[None])
            if max_vocab > 0 and i >= max_vocab:
                break
    # add a zero vector for the UNK token
    dim = vectors[-1].shape[1]
    if not UNK in word2id:
        word2id[UNK] = len(word2id)
        vectors.append(np.array([0 for i in range(dim)])[None])
    # compute new vocabulary / embeddings
    id2word = {v: k for k, v in word2id.items()}
    embeddings = np.concatenate(vectors, 0)

    return embeddings, word2id, id2word

def prefix_sequence(seq, prefix, strip_hs):
    if strip_hs is True:
        return ' '.join(['{}:{}'.format(prefix, elm.strip('#')) for elm in seq.split()])
    else:
        return ' '.join(['{}:{}'.format(prefix, elm) for elm in seq.split()])

def deprefix_sequence(seq):
    return ' '.join([elm.split(':')[-1] for elm in seq.split()])

def bool_flag(s):
    """
    Parse boolean arguments from the command line.
    """
    if s.lower() in ['off', 'false', '0']:
        return False
    if s.lower() in ['on', 'true', '1']:
        return True
    raise argparse.ArgumentTypeError("invalid value for a boolean flag (0 or 1)")

def get_exp_path(exp_path, exp_name, exp_id):
    """
    Create a directory to store the experiment.
    """
    # create the main dump path if it does not exist
    exp_folder = exp_path
    if not os.path.exists(exp_folder):
        subprocess.Popen("mkdir {}".format(exp_folder), shell=True).wait()
    assert exp_name != ''
    exp_folder = os.path.join(exp_folder, exp_name)
    if not os.path.exists(exp_folder):
        subprocess.Popen("mkdir {}".format(exp_folder), shell=True).wait()
    if exp_id == '':
        chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
        while True:
            exp_id = ''.join(random.choice(chars) for _ in range(10))
            exp_path = os.path.join(exp_folder, exp_id)
            if not os.path.isdir(exp_path):
                break
    else:
        exp_path = os.path.join(exp_folder, exp_id)
        assert not os.path.isdir(exp_path), exp_path
    # create the dump folder
    if not os.path.isdir(exp_path):
        subprocess.Popen("mkdir {}".format(exp_path), shell=True).wait()
    return exp_path

def p_r_f(gold, preds, labelset):
    results = {}
    results['macro'] = precision_recall_fscore_support(gold, preds, average='macro')
    results['micro'] = precision_recall_fscore_support(gold, preds, average='micro')
    results['per_class'] = {}
    labels = list(set([int(pred) for pred in preds]))

    # it's a tuple with precision, recall, f-score elements
    per_class_results = precision_recall_fscore_support(gold, preds, average=None, labels=labels)

    for i, label in enumerate(labels):
        results['per_class'][labelset[label]] = [elm[i] for elm in per_class_results]
    for label in labelset:
        if label not in results['per_class']:
            results['per_class'][label] = [0 for elm in results['macro']]
    return results

def get_auc(gold, probs, labelset):
    # iterate through classes and simulate 1 vs all binary prediction
    gold_binary = []
    probs_binary = []
    aucs = {}
    precs = {}
    recs = {}
    thr = {}
    for i, label in enumerate(labelset):
        for g, p, in zip(gold, probs):
            if g==i:
                gold_binary.append(1)
            else: gold_binary.append(0)
            probs_binary.append(p[i])
        precision, recall, thresholds = precision_recall_curve(gold_binary, probs_binary)

        auc_score = auc(recall, precision)
        thr[label] = thresholds
        aucs[label] = auc_score
        precs[label] = precision
        recs[label] = recall
    return aucs, precs, recs, thr

def print_result_summary(results):
    s =  '\nLabel\tP\tR\tF\t\nMacro\t{:.4f}\t{:.4f}\t{:.4f}\nMicro\t{:.4f}\t{:.4f}\t{:.4f}\n'.format(results['macro'][0],results['macro'][1],results['macro'][2],
                                                                        results['micro'][0], results['micro'][1], results['micro'][2])
    labels = sorted(results['per_class'].keys())
    for label in labels:
        s += '{}\t{:.4f}\t{:.4f}\t{:.4f}\n'.format(label, results['per_class'][label][0],results['per_class'][label][1],results['per_class'][label][2])
    if 'cm' in results:
        s += '{}'.format(results['cm'])
    return s

def print_auc_summary(aucs, labelset):
    s = 'AUC (precision-recall)\n'
    for label in labelset:
        s += '{}\t{:.4f}\n'.format(label, aucs[label])
    avg = np.mean(list(aucs.values()))
    avg12 = np.mean([aucs['1'], aucs['2']])
    s += 'Macro AUC: {:.4f}\n'.format(avg)
    s += '12 AUC: {:.4f}'.format(avg12)
    return s

def log_params(args):
    for key in sorted(args.keys()):
        logging.info('{}: {}'.format(key, args[key]))

if __name__=="__main__":
    fname = '/home/mareike/PycharmProjects/catPics/data/twitter/mh17/experiments/resources/mh17_60_20_20_vocab_extended_embs.txt'
    prefix = 'en'

    lines = read_file(fname)
    outlines = ['{}:{}'.format(prefix, line) for line in lines]
    write_file(fname + 'prefixed', outlines)


#!/bin/python

import math
import os
import random
import re
import sys

# Complete the repeatedString function below.
def repeatedString(s, n):
    s_len = len(s)
    mult = n / s_len
    addn = n % s_len
    count = 0
    for char in s[:n]:
        if 'a' in char:
            count += 1
    count *= mult
    for char in s[:addn]:
        if 'a' in char:
            count += 1
    return count

if __name__ == '__main__':
    s = "aba"

    n = 9

    result = repeatedString(s, n)

    print(str(result) + '\n')


# -*- coding: utf-8 -*-
import scrapy
import logging
import re
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors.lxmlhtml import LxmlLinkExtractor
from ..items import ProductItem

logger = logging.getLogger(__name__)

class HnammobileSpider(CrawlSpider):
    name = 'hnammobile'
    allowed_domains = ['www.hnammobile.com']
    start_urls = ['https://www.hnammobile.com/dien-thoai/']
    rules = (
        Rule(LxmlLinkExtractor(
            allow=('/dien-thoai/'),
            deny=(
                '/dien-thoai/#',
                '/tin-tuc/',
                '/may-tinh-bang/',
                '/phu-kien/',
                '/dong-ho-thong-minh/',
                '/kho-sim',
                '/event/',
                '/loai-dien-thoai/',
                '/mua-tra-gop',
                'https://www.hnammobile.com/dien-thoai/tel:19002012',
                'https://www.hnammobile.com/dien-thoai/tel:01234303000'
            )
        ), callback='parse_hnammobile'),
    )
    #handle_httpstatus_list = [404, 504]

    def parse_hnammobile(self, response):
        logger.info('Scrape url: %s' % response.url)
        for product_link in response.css('div.image>a::attr(href)'):
            yield response.follow(product_link, callback=self.parse_product_detail)

        # following pagination next page to scrape
        # links = response.xpath(
        #     '//li[contains(@class,"pagination-item") and (not(contains(@class,"active")))]/a/@href').getall()
        # if len(links) > 0:
        #     for next_page in links:
        #         yield response.follow(next_page, callback=self.parse_hnammobile)
        next_page = response.xpath(
            '//li[contains(@class,"pagination-item") and (not(contains(@class,"active")))]/a/@href').get()
        if next_page is not None:
            yield response.follow(next_page, callback=self.parse_hnammobile)
        pass

    def parse_product_detail(self, response):

        def extract_with_xpath(query):
            return response.xpath(query).get(default='').strip()

        def extract_price(query):
            price = response.xpath(query).get(default='').strip()
            return price

        def extract_xpath_all(query):
            gallery = response.xpath(query).getall()
            return gallery

        # Validate price with pattern
        price_pattern = re.compile("([0-9](\\w+ ?)*\\W+)")
        product_price = extract_price(
            '//h3[contains(@class,"price")]/font/font[@class="numberprice"]/text()')
        if re.match(price_pattern, product_price) is None:
            return

        product_title = extract_with_xpath('//h2[@class="title"]/text()')
        product_desc = extract_with_xpath(
            '//meta[@name="description"]/@content')
        product_swatchcolors = extract_xpath_all(
            '//div[@class="picker-color row"]/ul/li/div//text()')
        product_images = extract_xpath_all(
            '//div[@class="gallery"]/div[contains(@class,"item")]/@data-src')

        #product_specifications = response.xpath('//div[@class="content-body"]/div[@class="row size-screen"]//text()').getall()
        product_specifications = []
        for spec_row in response.xpath('//div[@class="content-body"]/div'):
            if spec_row is not None:
                try:
                    spec_key = spec_row.xpath('.//label/text()').get().strip()
                    spec_value = spec_row.xpath('.//p/text()').get().strip()
                    product_specifications.append({spec_key, spec_value})
                except:
                    pass

        product_link = response.url

        products = ProductItem()
        products['cid'] = 1  # 1: Smartphone
        products['title'] = product_title
        products['description'] = product_desc
        products['price'] = product_price
        products['swatchcolors'] = product_swatchcolors
        products['specifications'] = product_specifications
        products['link'] = product_link
        products['images'] = product_images
        products["shop"] = 'hnammobile'
        products["domain"] = 'hnammobile.com'
        products['body'] = response.text

        yield products


import sys
import re
import subprocess
import os
import numpy as np
import statistics as stats
import sys
import re
import subprocess
import os
import os.path

max_ligne = 2000
min_ligne = 440
max_tick = 3800

def lecture(file):
        global nbLignesSup3500,nbLignesInf3500,nbLignesSupTICK,nbLignesInfTICK
        print("Lecture du fichier: ", file)
        fichier = open(file, "r")
        nbLignes=0
        for line in fichier :
             s = re.findall(r"[-+]?\d*\.\d+|\d+", line)
             tick.append(float(s[0]))
             data1.append(float(s[1]))
             data2.append(float(s[2]))
             nbLignes+=1
        liste.append(nbLignes)
        if nbLignes>=max_ligne:
            nbLignesSup3500+=1
        if nbLignes<max_ligne and nbLignes>=min_ligne:
            nbLignesInf3500+=1
        if nbLignes>=max_ligne and float(s[0]) <=max_tick:
            nbLignesSupTICK+=1
        if nbLignes<max_ligne and float(s[0]) <= max_tick:
            nbLignesInfTICK+=1

nbLignesSup3500=0
nbLignesInf3500=0
nbLignesSupTICK=0
nbLignesInfTICK=0
nbTickSup=0
nbTickInf=0
event = []
tick = []
data1 = []
data2 = []
os.chdir('Apprentissage')
liste=[]
for root, dirs, files in os.walk(os.getcwd()):
    for file in files:
        if file.endswith('.txt'):
            lecture(file)

for element in tick:
    if element > max_tick:
        nbTickSup+=1
    if element <=max_tick:
        nbTickInf+=1

print('\n')
print("Maximum lignes = ",max(liste))
print("Minimim lignes = ",min(liste))
print("Moyenne lignes = ",int(np.mean(liste)))
print("Mediane lignes = ",int(np.median(liste)))
print("Mediane groupe lignes = ",stats.median_grouped(liste))
print("Nombre de lignes > 1500 = ",nbLignesSup3500)
print("Nombre de lignes < 1500 = ",nbLignesInf3500)
print("Nombre de lignes  et tick > = ",nbLignesSupTICK)
print("Nombre de lignes  et tick < = ",nbLignesInfTICK)
#print( list(sorted(set(liste))))

print('\n')
print("Maximum tick = ",max(tick))
print("Minimim tick = ",min(tick))
print("Moyenne tick = ",np.mean(tick))
print("Mediane tick = ",np.median(tick))
print("Ecart type tick = ",np.std(tick))
print("Nb Sup tick = ",nbTickSup)
print("Nb Inf tick = ",nbTickInf)

print('\n')
print("Maximum data1 = ",max(data1))
print("Minimim data1 = ",min(data1))
print("Moyenne data1 = ",np.mean(data1))
print("Mediane data1 = ",np.median(data1))

print('\n')
print("Maximum data2 = ",max(data2))
print("Minimim data2 = ",min(data2))
print("Moyenne data2 = ",np.mean(data2))
print("Mediane data2 = ",np.median(data2))

#from collections import Counter
#print(Counter(liste))


# -*- coding: utf-8 -*-

veclist = objvertices("ico.obj", 0)

def task(vec, axis):
	def shot_task(binder = [vec], rot = Matrix3.RotationAxis(vec ^ (vec ^ axis), RAD * 16)):
		shot = EntityShot(WORLD, "RICE_M", 0x0000A0)
		shot.Velocity = binder[0] * 6
		shot.LifeSpan = 100
		shot.SetMotionInterpolationCurve(Vector2(0.3, 0.9), Vector2(0.5, 0.95), shot.LifeSpan)

		def replace(org = shot):
			shot = EntityShotStraight(WORLD, "RICE_M", 0xA00000)
			shot.Pos = org.Pos
			shot.Velocity = normalize(org.Velocity) * Matrix3.RotationAxis(cross(randomvec(), org.Velocity), math.pi * gauss(0, 0.5)) * -2
			shot.LifeSpan = 300
			shot.Spawn()
		shot.AddTask(replace, 0, 1, shot.LifeSpan)
		shot.Spawn()

		binder[0] *= rot
	WORLD.AddTask(shot_task, 4, 100, 1)

for vec in veclist:
	for axis in Vector3.Units:
		task(vec, axis)


import copy
import datetime

import matplotlib.pyplot as plt

from chatylka_data.chatylka_constants import *

class FieldDynamics:
    def __init__(self, times, data_tr, data_mer, data_ann,
                 compr_num_float, compr_num, max_compressors_number_at_work, exceed_flags, i_mer_wells):

        self.time = times[0]
        self.dates = times[1]
        self.mer_dates = times[2]
        self.mer_time = times[3]
        self.year_dates = times[4]

        self.q_tr = data_tr[0]
        self.i_tr = data_tr[1]
        self.p_tr = data_tr[2]
        self.e_tr = data_tr[3]

        self.q_mer = data_mer[0]
        self.i_mer = data_mer[1]
        self.p_mer = data_mer[2]
        self.e_mer = data_mer[3]

        self.q_ann = data_ann[0]
        self.i_ann = data_ann[1]
        self.p_ann = data_ann[2]
        self.e_ann = data_ann[3]

        # calculate cumulative volumes
        self.Q_total = sum(self.q_tr)
        self.I_total = sum(self.i_tr)
        self.P_total = sum(self.p_tr)
        self.E_total = sum(self.e_tr)

        self.N_compr = compr_num
        self.N_compr_float = compr_num_float

        self.max_compressors_at_work_total = max_compressors_number_at_work[0]
        self.max_compressors_at_work_uninterruptedly = max_compressors_number_at_work[1]

        self.exceed_1 = exceed_flags[0]
        self.exceed_2 = exceed_flags[1]

        self.i_mer_wells = i_mer_wells

        self.FCF = None
        self.DCF = None
        self.NPV = None
        self.DCP = None

    def CalculateEconomics(self, netback, royalty, income_tax, c_oil, c_gas, r, first_year_econ):

        economics_shift = self.year_dates[0] - first_year_econ

        I = [self.i_ann[i] * volume_unit * Bg for i in range(len(self.year_dates))]
        Q = [self.q_ann[i] * volume_unit * rho / Bo for i in range(len(self.year_dates))]

        self.Revenue = [Q[i] * (netback - royalty - c_oil) for i in range(len(self.year_dates))]
        self.OPEX = [I[i] * c_gas for i in range(len(self.year_dates))]

        self.FCF = [(1.0 - income_tax) * (self.Revenue[i] - self.OPEX[i]) for i in range(len(self.year_dates))]
        self.DCF = [self.FCF[i] / ((1.0 + r) ** (0.5 + i + economics_shift)) for i in range(len(self.year_dates))]
        self.NPV = sum(self.DCF)

        # discounted cumulative production (just for interest)
        self.DCP = sum([self.q_ann[i] / ((1 + r) ** (0.5 + i)) for i in range(len(self.year_dates))])

def CalculateProfiles2(wells_all, sequence, E_distribution_all, e_annual, first_year, print_log,
                       show_graphs, max_compr_injection_annual, max_compressors_number):
    bug_mode = False

    print('Calculating dynamics...')
    # now we consider only sequential modes
    # sequence = [None, None, 0, 1, None, None, 2, None] - it means that only 3 wells are involved and numbers mean the sequence

    if print_log:
        print('sequence = ', sequence)

    # resulted profiles
    q_field = []
    i_field = []
    p_field = []
    e_field = []

    if len(wells_all) != len(sequence):
        raise ValueError('Error in lengths in CalculateProfiles')

    # daily injection limit
    max_i_daily = max_compr_injection_annual * max_compressors_number / volume_unit / 365.

    # how many wells we should inject
    wells_num = 0
    for i in range(len(wells_all)):
        if sequence[i] is not None:
            wells_num += 1
    # local variables determination
    wells = [0. for i in range(wells_num)] # wells for injection
    E_distribution = [0. for i in range(wells_num)] # final volumes of external supplies to each well
    wells_injected_external_volume = [0. for i in range(wells_num)] # current volumes of external supplies to each well
    mers_for_gdm = [[] for i in range(wells_num)]
    for i, w in enumerate(wells_all):
        if type(sequence[i]) == int:
            wells[sequence[i]] = copy.copy(w)
            E_distribution[sequence[i]] = copy.copy(E_distribution_all[i])
            wells_injected_external_volume[sequence[i]] = 0.
            # calculate current derivatives
            (wells[sequence[i]]).CalculateDynamicDerivatives(0.)

    # current well for injection
    order = 0

    # flag means that injection has exceeded the limit for 1,2,...N-1 wells
    exceed_1 = False

    # flag means that injection has exceeded the limit for N-th well
    exceed_2 = False

    # year-loop
    for t in range(len(e_annual)):

        # define the current year
        year = first_year + t
        days_in_current_year = (datetime.date(year + 1, 1, 1) - datetime.date(year, 1, 1)).days
        if print_log:
            print('Year ', year, ' days ', days_in_current_year)

        # external supply  rate during the year
        e_daily = e_annual[t] / days_in_current_year

        # day-loop during the year
        for day in range(1, days_in_current_year + 1):

            if bug_mode:
                print(day, ' main well ', (wells[order]).name)

            # We know the current E for the well - wells_injected_external_volume[well_index].
            # Let's find the corresponding I, Q and P values and then find i, q and p
            i_daily = (wells[order]).j * e_daily

            # current injection i_daily can be more than central facility constraint
            if i_daily < max_i_daily:

                if bug_mode:
                    print('no assistant')

                # it's ok: calculate current p and q
                p_daily = (wells[order]).dg_dI * i_daily
                q_daily = (wells[order]).dH_dE * e_daily

                # take into account the injected supplies
                wells_injected_external_volume[order] += e_daily * 1.0

                # recalculate the dynamics properties of the well
                (wells[order]).CalculateDynamicDerivatives(wells_injected_external_volume[order])

                for w in range(wells_num):
                    if w == order:
                        mers_for_gdm[w].append(i_daily)
                    else:
                        mers_for_gdm[w].append(0.)

            else:
                # we should inject some volume to the next well
                if order < len(wells) - 1:

                    # flag: have we found another well for injection?
                    assistant_well = False

                    # find next well with j < j of the current well
                    for ss in range(1, len(wells) - order):

                        if bug_mode:
                            print('Try ', (wells[order + ss]).name, ' with derivative = ', (wells[order + ss]).j, ' Current derivative = ', (wells[order]).j)

                        # check that portions of external supplies will > 0 and < e_daily
                        e_1 = (max_i_daily - (wells[order + ss]).j * e_daily) / (
                        (wells[order]).j - (wells[order + ss]).j)

                        if ((wells[order + ss]).j < (wells[order]).j) and (e_1 > 0.) and (e_1 < e_daily):

                            if bug_mode:
                                print('assistant is ', wells[order + ss].name)

                            assistant_well = True

                            # portion of external supplies to the next well
                            e_2 = e_daily - e_1

                            if bug_mode:
                                print('e_daily ', e_daily, ' = ', e_2, ' + ', e_1)

                            # calculate rates
                            i_daily = (wells[order]).j * e_1 + (wells[order + ss]).j * e_2
                            p_daily = (wells[order]).dg_dI * (wells[order]).j * e_1 + (wells[order + ss]).dg_dI * (
                                wells[order + ss]).j * e_2
                            q_daily = (wells[order]).dH_dE * e_1 + (wells[order + ss]).dH_dE * e_2

                            # take into account the injected supplies
                            wells_injected_external_volume[order] += e_1 * 1.0
                            wells_injected_external_volume[order + ss] += e_2 * 1.0

                            # recalculate the dynamics properties of the well
                            (wells[order]).CalculateDynamicDerivatives(wells_injected_external_volume[order])
                            (wells[order + ss]).CalculateDynamicDerivatives(wells_injected_external_volume[order + ss])

                            for w in range(wells_num):
                                if w == order:
                                    mers_for_gdm[w].append((wells[order]).j * e_1)
                                elif w == order + ss:
                                    mers_for_gdm[w].append((wells[order + ss]).j * e_2)
                                else:
                                    mers_for_gdm[w].append(0.)

                            break

                    if not assistant_well: # there were no additional well for injection

                        exceed_1 = True

                        if print_log:
                            print(
                                "WARNING: there were no additional well for injection; it exceeds the limit; all is injected to the current well")
                            if bug_mode:
                                input()

                        # it's NOT ok, but we can't do anything: calculate current p and q
                        p_daily = (wells[order]).dg_dI * i_daily
                        q_daily = (wells[order]).dH_dE * e_daily

                        # take into account the injected supplies
                        wells_injected_external_volume[order] += e_daily * 1.0

                        # recalculate the dynamics properties of the well
                        (wells[order]).CalculateDynamicDerivatives(wells_injected_external_volume[order])

                        for w in range(wells_num):
                            if w == order:
                                mers_for_gdm[w].append(i_daily)
                            else:
                                mers_for_gdm[w].append(0.)

                else:

                    exceed_2 = True

                    if print_log:
                        print("WARNING: injection value to the last well is exceeds the limit! But we can't do anything")
                        if bug_mode:
                            input()

                    # it's NOT ok, but we can't do anything: calculate current p and q
                    p_daily = (wells[order]).dg_dI * i_daily
                    q_daily = (wells[order]).dH_dE * e_daily

                    # take into account the injected supplies
                    wells_injected_external_volume[order] += e_daily * 1.0

                    # recalculate the dynamics properties of the well
                    (wells[order]).CalculateDynamicDerivatives(wells_injected_external_volume[order])

                    for w in range(wells_num):
                        if w == order:
                            mers_for_gdm[w].append(i_daily)
                        else:
                            mers_for_gdm[w].append(0.)

            # if injected volume is bigger than E_distribution -> inject to the next well
            if wells_injected_external_volume[order] >= E_distribution[order]:# or abs(E_distribution[order] - wells_injected_external_volume[order]) < volume_error:

                # go to the next well
                order += 1

                if bug_mode:
                    print(wells_injected_external_volume, E_distribution)
                    print('GO TO NEXT WELL\n')
                    input()

                # if all wells were involved, but there are some days in the current year
                if order == len(wells) and day != days_in_current_year:

                    if print_log:
                        print("Warning: there are ", (days_in_current_year - day), " days rest, but all wells were injected! Make all rates = 0.")
                    # exit(1)

                    order -= 1 # now order value doesn't matter, but order must be in list
                    i_daily = 0.
                    p_daily = 0.
                    q_daily = 0.
                    e_daily = 0. # it's important!

            # finally, fill the daily profiles
            q_field.append(q_daily * 1.0)
            p_field.append(p_daily * 1.0)
            i_field.append(i_daily * 1.0)
            e_field.append(e_daily * 1.0)

        if bug_mode:
            input()

    # make monthly reports and annual volumes
    print('Creating profiles...')

    time = [] # 1,2,3,...7305
    dates = [] # string type: '01.01.2021', '02.01.2021', ...
    mer_dates = [] # string type: '01.2021', '02.2021', ...
    mer_time = [] # 1,2,... 240
    year_dates = [] # 2021, 2022, ...

    # FIELD
    q_mer = []
    q_ann = [0.]
    i_mer = []
    i_ann = [0.]
    p_mer = []
    p_ann = [0.]
    e_mer = []
    e_ann = [0.]

    # WELLS
    i_mer_wells = [[] for i in range(wells_num)]

    # local summators
    month_duration = 0.
    sum_q = 0.
    sum_i = 0.
    sum_p = 0.
    sum_e = 0.
    sum_i_wells = [0. for i in range(wells_num)]

    # cut MER and annual profiles from cumulative profiles
    current_date = datetime.date(first_year, 1, 1)  # datetime.timedelta(days=1)

    year_dates.append(current_date.year)

    for t in range(len(e_field)):

        time.append(t + 1)
        dates.append(str(current_date.day) + '.' + str(current_date.month) + '.' + str(current_date.year))

        sum_q += q_field[t]
        sum_i += i_field[t]
        sum_p += p_field[t]
        sum_e += e_field[t]

        for w in range(wells_num):
            sum_i_wells[w] += mers_for_gdm[w][t]

        month_duration += 1

        # if this day is a final day of the month
        if (current_date + datetime.timedelta(days=1)).month != current_date.month:

            # fix current month date
            mer_dates.append(str(current_date.month) + '.' + str(current_date.year))
            mer_time.append(len(mer_time) + 1)

            # fix average rates
            q_mer.append(sum_q / month_duration)
            i_mer.append(sum_i / month_duration)
            p_mer.append(sum_p / month_duration)
            e_mer.append(sum_e / month_duration)

            for w in range(wells_num):
                i_mer_wells[w].append(sum_i_wells[w] / month_duration)

            # add cumulative volumes to the current annual production and injection
            q_ann[-1] += sum_q
            i_ann[-1] += sum_i
            p_ann[-1] += sum_p
            e_ann[-1] += sum_e

            # cut the annual cumulative volumes
            if (current_date + datetime.timedelta(days=1)).year != current_date.year and t != (len(e_field) - 1):
                q_ann.append(0.)
                i_ann.append(0.)
                p_ann.append(0.)
                e_ann.append(0.)
                year_dates.append(current_date.year + 1)

            # reset summators for the next month
            sum_q = 0.
            sum_i = 0.
            sum_p = 0.
            sum_e = 0.
            sum_i_wells = [0. for i in range(wells_num)]
            month_duration = 0.

        # next step
        current_date += datetime.timedelta(days=1)

    # check FIELD and WELLS rates
    if print_log:
        print('Compare field rates and sum of field rates...')
    max_delta = 0.0
    for month in range(len(i_mer)):
        local = sum([ i_mer_wells[w][month] for w in range(wells_num) ])
        if ( abs(local - i_mer[month]) / i_mer[month] * 100. ) > max_delta:
            max_delta = abs(local - i_mer[month]) / i_mer[month] * 100.
            if max_delta > 5.0:
                print('Error: the sum of well rates is not equal to field rate with precision 5%! Exit...')
                exit(1)
    if print_log:
        print('Max internal delta for rates in % = ', max_delta)

    # convert to abs. units: SM3/day
    for w in range(len(i_mer_wells)):
        for t in range(len(i_mer_wells[w])):
            i_mer_wells[w][t] *= volume_unit * Bg

    # add well names
    i_mer_wells.insert(0, [])
    # correct names according to mask
    for w in wells:
        for real_name, mask_name in names_mask.items():
            if mask_name == w.name:
                i_mer_wells[0].append(real_name)

    # calculate how many compressors do we need using different stepsizes: day, month and year
    one_compr_max = [max_compr_injection_annual / volume_unit / 365., max_compr_injection_annual / volume_unit / 365., max_compr_injection_annual / volume_unit ] # in relative units
    compr_num_float = [max(i_field) / one_compr_max[0], max(i_mer) / one_compr_max[1], max(i_ann) / one_compr_max[2]]
    compr_num = [int(compr_num_float[0]), int(compr_num_float[1]), int(compr_num_float[2])]
    for cn in range(len(compr_num)):
        if compr_num_float[cn] != compr_num[cn]:
            compr_num[cn] += 1

    print('Compressors number = ', compr_num_float, ' ', compr_num)

    # calculate how many days TOTALLY and UNINTERRUPTLY do we need the maximum number of working compressors
    if max_compressors_number == 1:
        max_compressors_number_at_work = [len(i_field) / 365., len(i_field) / 365.]
    else:
        # the power of (N-1) compressors
        pre_limit = one_compr_max[0] * (max_compressors_number - 1)
        # periods of injection when we needed the max number of compressors
        periods = [0]
        for k, inj in enumerate(i_field):
            if inj > pre_limit:
                periods[-1] += 1
                # stop the current period if next Inj rate is less than the limit; so we start the next period
                if k != (len(i_field) - 1) and i_field[k + 1] <= pre_limit:
                    periods.append(0)
        max_compressors_number_at_work = [sum(periods) / 365., max(periods) / 365.]
    print('Max compressors at work (in years) = ', max_compressors_number_at_work[0], ' total and', max_compressors_number_at_work[1], ' max constant period')

    if show_graphs:

        plt.plot(time, q_field, color='r')
        plt.plot(time, i_field, color='b')
        plt.plot(time, p_field, color='cyan')
        plt.plot(time, e_field, color='g')
        plt.plot([time[0], time[-1]], [0, 0], color='k', linestyle='--')
        for k in range(1, compr_num[0] + 1):
            plt.plot([time[0], time[-1]], [k * one_compr_max[0], k * one_compr_max[0]], color='maroon', linestyle='-')
        plt.show()

        plt.plot(mer_time, q_mer, color='r', marker='.')
        plt.plot(mer_time, i_mer, color='b', marker='.')
        plt.plot(mer_time, p_mer, color='cyan', marker='.')
        plt.plot(mer_time, e_mer, color='g', marker='.')
        plt.plot([mer_time[0], mer_time[-1]], [0, 0], color='k', linestyle='--')
        for k in range(1, compr_num[1] + 1):
            plt.plot([mer_time[0], mer_time[-1]], [k * one_compr_max[1], k * one_compr_max[1]], color='maroon', linestyle='-')
        plt.show()

        plt.plot(year_dates, q_ann, color='r', marker='.')
        plt.plot(year_dates, i_ann, color='b', marker='.')
        plt.plot(year_dates, p_ann, color='cyan', marker='.')
        plt.plot(year_dates, e_ann, color='g', marker='.')
        plt.plot([year_dates[0], year_dates[-1]], [0, 0], color='k', linestyle='--')
        for k in range(1, compr_num[2] + 1):
            plt.plot([year_dates[0], year_dates[-1]], [k * one_compr_max[2], k * one_compr_max[2]], color='maroon', linestyle='-')
        plt.show()

    # construct the solution
    result = FieldDynamics([time, dates, mer_dates, mer_time, year_dates], [q_field, i_field, p_field, e_field],
                           [q_mer, i_mer, p_mer, e_mer], [q_ann, i_ann, p_ann, e_ann], compr_num_float, compr_num, max_compressors_number_at_work, [exceed_1, exceed_2], i_mer_wells)

    if print_log:
        print(len(i_field))

    return result


#from __future__ import absolute_import
#from __future__ import division
#from __future__ import print_function

import numpy as np
import matplotlib.pyplot as plt
from random import randrange
# custom modules
from utils     import Options, rgb2gray
from simulator import Simulator

from transitionTable import TransitionTable
from data_utils import create_datasets

import tensorflow as tf

# 0. initialization
opt = Options()
sim = Simulator(opt.map_ind, opt.cub_siz, opt.pob_siz, opt.act_num)
trans = TransitionTable(opt.state_siz, opt.act_num, opt.hist_len,
                            opt.minibatch_size, opt.valid_size,
                            opt.states_fil, opt.labels_fil)
#datasets = create_datasets([trans.get_train(), trans.get_valid()], multi_dims=True)

# TODO: load your agent

sess = tf.Session()

saver = tf.train.import_meta_graph('./logs/model.ckpt-4999.meta')
saver.restore(sess, './logs/model.ckpt-4999')

images_placeholder = tf.get_collection('images_placeholder')[0]
labels_placeholder = tf.get_collection('labels_placeholder')[0]
dropout_placeholder = tf.get_collection('dropout_placeholder')[0]
logits = tf.get_collection('logits')[0]
eval_correct = tf.get_collection('eval_correct')[0]

feed_dict = {dropout_placeholder: 1.0}

agent = tf.reduce_max(tf.argmax(logits, axis=1))

# On/off visualizatoin
opt.disp_on = True

# 1. control loop
if opt.disp_on:
    win_all = None
    win_pob = None
epi_step = 0    # #steps in current episode
nepisodes = 0   # total #episodes executed
nepisodes_solved = 0
action = 0     # action to take given by the network

# start a new game
state = sim.newGame(opt.tgt_y, opt.tgt_x)
old_hist_state = None

for step in range(opt.eval_steps):

    # The accuracy on validation set works fine ! 61/64
    #image = datasets.validation.images[:64]
    #feed_dict[images_placeholder] = image
    #feed_dict[labels_placeholder] = datasets.validation.labels[:64]
    #print('Eval_correct:', sess.run(eval_correct, feed_dict=feed_dict))

    # check if episode ended
    if state.terminal or epi_step >= opt.early_stop:
        epi_step = 0
        nepisodes += 1
        if state.terminal:
            nepisodes_solved += 1
        # start a new game
        state = sim.newGame(opt.tgt_y, opt.tgt_x)
    else:
        #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        # TODO: here you would let your agent take its action
        #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        # this just gets a random action

        current_state = rgb2gray(state.pob).reshape([25, 25]).transpose([1, 0])

        if epi_step == 0:
            #old_hist_state = np.zeros([25, 25, 4])
            #old_hist_state[:, :, -1] = current_state
            old_hist_state = np.repeat(current_state, 4).reshape(current_state.shape + (4,))

            assert np.equal(current_state, old_hist_state[:, :, 0]).min()
        else:
            current_state = current_state.reshape([25, 25, 1])

            old_hist_state = np.append(old_hist_state, current_state, axis=-1)
            old_hist_state = np.delete(old_hist_state, 0, axis=-1)

            #old_hist_state = np.delete(old_hist_state, 0, axis=-1)
            #old_hist_state = np.insert(old_hist_state, -1, current_state, axis=-1)

        input_network = np.repeat(old_hist_state, 32).reshape(old_hist_state.shape + (32,)).transpose([3, 0, 1, 2])

        feed_dict[images_placeholder] = input_network

        action = sess.run(agent, feed_dict=feed_dict)

        print('####Step: {} #### Action: {}'.format(epi_step, action))

        #action = randrange(opt.act_num)
        if epi_step < 0:
            state = sim.step() # A* at first
        else:
            state = sim.step(action)
        #state = sim.step() # Perform A*, works fine !

        epi_step += 1

    if state.terminal or epi_step >= opt.early_stop:
        epi_step = 0
        nepisodes += 1
        if state.terminal:
            nepisodes_solved += 1
        # start a new game
        state = sim.newGame(opt.tgt_y, opt.tgt_x)

    if step % opt.prog_freq == 0:
        print(step)

    if opt.disp_on:
        if win_all is None:
            plt.subplot(121)
            win_all = plt.imshow(state.screen)
            plt.subplot(122)
            win_pob = plt.imshow(state.pob)
        else:
            win_all.set_data(state.screen)
            win_pob.set_data(state.pob)
        plt.pause(opt.disp_interval)
        plt.draw()

sess.close()
# 2. calculate statistics
print(float(nepisodes_solved) / float(nepisodes))
# 3. TODO perhaps  do some additional analysis


'''
BeagleBone library :: clsBB_commport.py - Serial port class for BeagleBone

Python 3

v0.01 - early stages of development. Likely to change frequently
and not always for the better

by Speculatrix :: http://speculatrix.tumblr.com/

Open source, GPL blah blah - use at your own risk. A credit is nice if you think
it works.

TO DO:
	* Lots more error checking, exceptions etc
	* Methods to change baud rate & other settings
'''

from serial import *
import BB
from clsBB_pin import GPIO_pin
from BBlib_common import *
import time

class CommPort:
	'''
	commPort class

	USAGE: Needs to be instantiated with the following params:
		portNum     0, 1, 2, 4 or 5		      	required
		baud        <baud rate>		optional	defaults to 19200
		sendInit	boolean			optional
	'''
	def __init__(self, portNum, baud=19200, sendInit=False):
		if 0 <= portNum <=5 and portNum != 3:
			# Uart - serial ports - 0,1,2,4 and 5
			# Port 3 is not available via headers
			# Port 0 refers to the USB. This doesn't need pin, mode & port
			# settings but I've put those in as zero values (which can be tested for)
			# just for completeness
			self.commPortInfo = [
				{'name': 'usb', 'dev': 'ttyACM0', 'txPin': 0, 'rxPin': 0, 'mode': 0, 'port': 0},
				{'name': 'uart1', 'dev': 'ttyO1', 'txPin': 24, 'rxPin': 26, 'mode': 0, 'port': 9},
				{'name': 'uart2', 'dev': 'ttyO2', 'txPin': 21, 'rxPin': 22, 'mode': 1, 'port': 9},
				0,
				{'name': 'uart4', 'dev': 'ttyO4', 'txPin': 13, 'rxPin': 11, 'mode': 6, 'port': 9},
				{'name': 'uart5', 'dev': 'ttyO5', 'txPin': 37, 'rxPin': 38, 'mode': 4, 'port': 8}
				]
			self.uart = portNum
			self.dev = '/dev/' + self.commPortInfo[self.uart]['dev']
			self.name = self.commPortInfo[self.uart]['name']
			self.baudRate = baud			# default
			self.byteSize = EIGHTBITS
			self.parity	= PARITY_NONE		# this is pySerial default anyway
			self.stopBits = STOPBITS_ONE	# also pySerial default
			self.timeOut = 3
			self.message = ''
			self.port = 0
			self.txPin = 0
			self.rxPin = 0
			self.mode = 0
			if self.uart > 0:
				# this is one of the uart ports rather than usb
				self.port = self.commPortInfo[self.uart]['port']
				self.txPin = self.commPortInfo[self.uart]['txPin']
				self.rxPin = self.commPortInfo[self.uart]['rxPin']
				self.mode = self.commPortInfo[self.uart]['mode']
				self.tx = GPIO_pin(self.port, self.txPin, BB.OUTPUT)
				self.tx.setMode(self.mode)
				self.rx = GPIO_pin(self.port, self.rxPin, BB.INPUT)
				self.rx.setMode(self.mode)
			try:
				self.serPort = Serial(self.dev,
					baudrate = self.baudRate,
					#parity = self.parity,
					#bytesize = self.byteSize,
					#stopbits = self.stopBits,
					timeout=0.1,
					xonxoff=0,
					rtscts=0,
					interCharTimeout=None)
				self.serPort.flushOutput()
				self.serPort.flushInput()
			except serial.serialutil.SerialException as e:
				print(e)
				exit()

			if sendInit:
				# Opening a serial connection to an Arduino can cause it to
				# reset, so let's send it an init message then give it a moment
				# to get its act together
				self.send('++INIT++')
				time.sleep(2)
			self.serPort.flushOutput()
			self.serPort.flushInput()

		else:
			# throw an exception one day, but for time being print a message
			print('*** ERROR: ' + str(portNum) + 'is not a valid uart number')
			exit()

	def send(self, sendStr, encoding='utf-8', addNewline=True, cleanString=True):
		'''
		Send a message. A newline is appended, so don't need to include
		that in the string.
		'''
		if cleanString:
			sendStr = sendStr.strip()		# clean it up first
		if addNewline:
			sendStr = sendStr + '\n'
		self.serPort.write(sendStr.encode(encoding, 'replace'))
		#self.serPort.flush()
		return

	def newMsgs(self, encoding='utf-8'):
		'''
		Obtain list of incoming messages. Assumes each message is terminated by newline.
		Dodgy bytes are treated using the .replace() method which does
		standard replacing according to codec. This avoids the program falling
		over should a bad byte appear that the utf-8 codec can't handle. But
		might be better to do some error handling here - maybe some basic
		flow control to request resending of message
		'''
		msgList = []
		while self.serPort.inWaiting() > 0:
			# using readline could create a problem where data has been sent
			# but isn't terminated by a newline. In this case, the while above
			# would cause this program to wait forever. For time being that's tolerable
			# because we'll be strict about always using a newline
			msgList.append(self.serPort.readline().decode(encoding, 'replace').strip())
		return msgList

	def newMsg(self):
		'''
		DEPRECATED: Reads one newline-terminated message from buffer. This might cause
		incoming messages to stack up, so using newMsgs() method above is
		preferable and this method might be removed.
		'''
		if self.serPort.inWaiting() > 0:
			self.message = self.serPort.readline().decode('utf-8', 'replace').strip()
			result = True
		else:
			result = False
		return result

	def __str__(self):
		'''
		Print information about this port
		'''
		print('COMM PORT', self.uart, '-', self.name,'on', self.dev,'-',
			str(self.baudRate) + ' baud', str(self.byteSize) + '-' + self.parity + '-' + str(self.stopBits))
		if self.uart > 0:
			print('port:', self.port, '| tx pin:', self.txPin, '| rx pin:', self.rxPin,
			  '| mode:', self.mode)
			print('--')
			print('TX:', self.tx)
			print('RX:', self.rx)
		return ' '


#!/usr/bin/python
# MIT License
#
# Copyright (c) 2016 Marc-Andre Chenier
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import Adafruit_GPIO.FT232H as FT232H
import Adafruit_GPIO.GPIO as GPIO
import time
import datetime

# FT232H module pins
PIN_SENSE_IN=0
PIN_SENSOR_TOP_DRIVE_OUT=1
PIN_SENSOR_BOTTOM_DRIVE_OUT=2
PIN_MUX_A_OUT=3
PIN_MUX_B_OUT=4
PIN_MUX_C_OUT=5
PIN_MUX_ENABLE0_OUT=6
PIN_MUX_ENABLE1_OUT=7
PIN_LED_GREEN_OUT=8
PIN_LED_RED_OUT=9
PIN_VALVE_OPTO_OUT=10
PIN_BUTTON_POS0_IN=11
PIN_BUTTON_POS1_IN=12
PIN_ALARM_RELAY_OUT=13

GPIO_DIR_DICT={ PIN_SENSE_IN: GPIO.IN,
                PIN_SENSOR_TOP_DRIVE_OUT: GPIO.OUT,
                PIN_SENSOR_BOTTOM_DRIVE_OUT: GPIO.OUT,
                PIN_MUX_A_OUT: GPIO.OUT,
                PIN_MUX_B_OUT: GPIO.OUT,
                PIN_MUX_C_OUT: GPIO.OUT,
                PIN_MUX_ENABLE0_OUT: GPIO.OUT,
                PIN_MUX_ENABLE1_OUT: GPIO.OUT,
                PIN_LED_GREEN_OUT: GPIO.OUT,
                PIN_LED_RED_OUT: GPIO.OUT,
                PIN_VALVE_OPTO_OUT: GPIO.OUT,
                PIN_BUTTON_POS0_IN: GPIO.IN,
                PIN_BUTTON_POS1_IN: GPIO.IN,
                PIN_ALARM_RELAY_OUT: GPIO.OUT
                }

GPIO_PIN_DICT={ "sense": PIN_SENSE_IN,
                "sensor_drive": { "bottom": PIN_SENSOR_BOTTOM_DRIVE_OUT, "top": PIN_SENSOR_TOP_DRIVE_OUT},
                "mux_enable": [PIN_MUX_ENABLE0_OUT, PIN_MUX_ENABLE1_OUT],
                "mux": [PIN_MUX_A_OUT, PIN_MUX_B_OUT, PIN_MUX_C_OUT],
                "led": { "green": PIN_LED_GREEN_OUT, "red": PIN_LED_RED_OUT },
                "valve_opto": PIN_VALVE_OPTO_OUT,
                "button": [PIN_BUTTON_POS0_IN, PIN_BUTTON_POS1_IN],
                "alarm_relay": PIN_ALARM_RELAY_OUT,
                }

###############################################################################
def log(msg):
    t = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
    print(t + " - " + msg)

###############################################################################
class WaterSensor:
    STATE_NOT_SENSED=0
    STATE_DISCONNECTED=1
    STATE_OK=2
    STATE_TROUBLE=3
    STATE_WATER_DETECTED=4 # Or short

    STATE_STR={STATE_DISCONNECTED:"disconnected",
               STATE_OK:"ok",
               STATE_TROUBLE:"trouble",
               STATE_WATER_DETECTED:"water detected"}

    def __init__(self, ftdi, addr, name = "", location = "", gpio_pin_dict = GPIO_PIN_DICT):
        # Addr = 5 bits
        # [0] = Mux A
        # [1] = Mux B
        # [2] = Mux C
        # [3] = Mux Enable0
        # [4] = Mux Enable1
        self.ftdi = ftdi
        self.addr = addr
        self.name = name
        self.location = location
        self.gpio_pin_dict = gpio_pin_dict

        self.state_stable = self.STATE_NOT_SENSED
        self.state_stable_time = 0
        self.state_stable_count = 0
        self.state_last_sense = self.STATE_NOT_SENSED
        self.state_last_sense_time = 0
        self.state_last_sense_count = 0
        self.histerysis_sec = 0
        self.histerysis_sample_count = 0
        self.stable = False
        self.event = False
        self.initialized = False

    def update(self):
        t = time.time()

        if (not self.initialized):
            self.state_last_sense = self.getSenseState()
            self.state_last_sense_time = t
            self.state_last_sense_count = 0
            self.initialized = True
            return

        state = self.getSenseState()
        if (state == self.state_last_sense):
            self.state_last_sense_count += 1

            if (((t - self.state_last_sense_time) >= self.histerysis_sec) and
                (self.state_last_sense_count >= self.histerysis_sample_count)):
                if (self.state_stable != state):
                    self.event = True
                    self.state_stable = state
                    self.state_stable_time = t
                    self.stable = True
                else:
                    self.state_stable_count += 1

        else:
            self.stable = False
            self.state_last_sense = state
            self.state_last_sense_time = t
            self.state_last_sense_count = 0

    def isEvent(self):
        return self.event

    def resetEvent(self):
        self.event = False

    def isStable(self):
        return self.stable

    def getStableState(self):
        return self.state_stable

    def getSenseState(self):
        try:
            # Set the addr of the sensor
            # Disable the enable pins first
            self.ftdi.output(self.gpio_pin_dict["mux_enable"][0],1)  # Mux enable is inversed
            self.ftdi.output(self.gpio_pin_dict["mux_enable"][1],1)  # Mux enable is inversed

            self.ftdi.output(self.gpio_pin_dict["mux"][0],1 if self.addr & 0x1 else 0)
            self.ftdi.output(self.gpio_pin_dict["mux"][1],1 if self.addr & 0x2 else 0)
            self.ftdi.output(self.gpio_pin_dict["mux"][2],1 if self.addr & 0x4 else 0)
            self.ftdi.output(self.gpio_pin_dict["mux_enable"][0],0 if self.addr & 0x8 else 1)
            self.ftdi.output(self.gpio_pin_dict["mux_enable"][1],0 if self.addr & 0x10 else 1)

            # Validate initial state of the sense pin (open collector)
            self.ftdi.output(self.gpio_pin_dict["sensor_drive"]["top"], 0)
            self.ftdi.output(self.gpio_pin_dict["sensor_drive"]["bottom"], 0)

            time.sleep(0.010)
            value = self.ftdi.input(self.gpio_pin_dict["sense"])
            if (not value):
                return self.STATE_TROUBLE

            # Validate connection with the diode
            self.ftdi.output(self.gpio_pin_dict["sensor_drive"]["top"], 0)
            self.ftdi.output(self.gpio_pin_dict["sensor_drive"]["bottom"], 1)

            time.sleep(0.010)
            value = self.ftdi.input(self.gpio_pin_dict["sense"])
            if (not value):
                return self.STATE_DISCONNECTED

            # Validate if there is water detected
            self.ftdi.output(self.gpio_pin_dict["sensor_drive"]["top"], 1)
            self.ftdi.output(self.gpio_pin_dict["sensor_drive"]["bottom"], 0)

            time.sleep(0.1)
            value = self.ftdi.input(self.gpio_pin_dict["sense"])
            if (value):
                return self.STATE_OK
            else:
                return self.STATE_WATER_DETECTED

        finally:
            # Disable muxes
            self.ftdi.output(self.gpio_pin_dict["mux_enable"][0],1)  # Mux enable is inversed
            self.ftdi.output(self.gpio_pin_dict["mux_enable"][1],1)  # Mux enable is inversed

###############################################################################
ft232h = FT232H.FT232H()

# Mux addressing format
# Addr = 5 bits
# [0] = Mux A
# [1] = Mux B
# [2] = Mux C
# [3] = Mux Enable0
# [4] = Mux Enable1

MUX_ENABLE_0=0x8
MUX_ENABLE_1=0x10
sensor_list = [ WaterSensor(ft232h, MUX_ENABLE_0 + 0, "sous-sol_0", "Drain en dessous de l'escalier."),
                WaterSensor(ft232h, MUX_ENABLE_0 + 1, "sous-sol_1", "Coin maison avant gauche."),
                WaterSensor(ft232h, MUX_ENABLE_0 + 2, "sous-sol_2", "Arriere maison au centre entre les caloriferes."),
                WaterSensor(ft232h, MUX_ENABLE_0 + 3, "sous-sol_3", "Tank a eau chaude."),
                WaterSensor(ft232h, MUX_ENABLE_0 + 4, "salle_eau_rdc_laveuse", "En arriere de la laveuse a linge."),
                WaterSensor(ft232h, MUX_ENABLE_0 + 5, "salle_eau_rdc_laveuse_mur", "En arriere de la laveuse a linge, dans le mur en dessous du retour d'eau."),
                WaterSensor(ft232h, MUX_ENABLE_0 + 6, "salle_eau_rdc_toilette", "En arriere de la toilette."),
                WaterSensor(ft232h, MUX_ENABLE_0 + 7, "salle_eau_rdc_vanite", "Dans la vanite, en dessous de la tuyeauterie."),
                WaterSensor(ft232h, MUX_ENABLE_1 + 0, "cuisine_evier", "En dessous de la tuyeauterie de l'evier."),
                WaterSensor(ft232h, MUX_ENABLE_1 + 1, "cuisine_lave-vaisselle", "A gauche, en avant du lave vaisselle."),
                WaterSensor(ft232h, MUX_ENABLE_1 + 2, "cuisine_frigidaire", "A gauche du frigidaire, venant de l'armoire."),
                WaterSensor(ft232h, MUX_ENABLE_1 + 3, "salle_bain_etage_toilette", "En arriere de la toilette."),
                WaterSensor(ft232h, MUX_ENABLE_1 + 4, "salle_bain_etage_lavabos", "En dessous de la tuyeauterie des lavabos.") ]

log("Setup FTDI pins ...")
ft232h.setup_pins(GPIO_DIR_DICT)

log("Begin water sensor monitoring")
led_green=1
led_red=0
valve_opto=0
alarm_relay=0

fault=False
button_pos="left"
button_pos_last="undefined"

operation_mode="off"
operation_mode_last="undefined"

try:
    while True:
        # Get the position of the slidding button
        # [ 0, 0 ] == impossible
        # [ 1, 0 ] == Left
        # [ 0, 1 ] == Right
        # [ 1, 1 ] == Center
        button_lut = { (0,0): "impossible",
                       (1,0): "left",
                       (0,1): "right",
                       (1,1): "center" }
        button_test_0 = ft232h.input(GPIO_PIN_DICT["button"][0])
        button_test_1 = ft232h.input(GPIO_PIN_DICT["button"][1])
        button_pos = button_lut[(button_test_0, button_test_1)]

        if (button_pos != button_pos_last):
            log("Slide button position changed to: " + button_pos)
            button_pos_last = button_pos
            if (button_pos == "left"):
                operation_mode = "off"
            elif (button_pos == "center"):
                operation_mode = "silent"
            elif (button_pos == "right"):
                operation_mode = "normal"
            else:
                log("There is trouble with the slidding button.  Stay in the same mode")

        # Define operation mode action
        if (operation_mode != operation_mode_last):
            log("Operation mode changed to: " + operation_mode)
            operation_mode_last = operation_mode

        if (operation_mode == "off"):
            valve_opto= 0
            alarm_relay=0
            fault = 0
            led_green = 1
            led_red = 0
        else:
            valve_opto= 0
            alarm_relay=0

            if (fault):
                led_red = 1
                if (operation_mode != "silent"):
                    valve_opto=1
                    alarm_relay=1
            else:
                led_red = 0

        # Set the valve and alarm pins
        ft232h.output(GPIO_PIN_DICT["valve_opto"],valve_opto)
        ft232h.output(GPIO_PIN_DICT["alarm_relay"],alarm_relay)

        # Set the led pins
        ft232h.output(GPIO_PIN_DICT["led"]["green"],led_green)
        ft232h.output(GPIO_PIN_DICT["led"]["red"],led_red)

        time.sleep(0.1)
        fault=False
        if (operation_mode in ["normal", "silent"]):
            led_green=led_green ^ 0x1

            for sensor in sensor_list:
                sensor.update()
                state = sensor.getStableState()
                if (sensor.isEvent()):
                    log("Sensor event: " + sensor.STATE_STR[state] + ", name: " + sensor.name)
                    sensor.resetEvent()

                if (state != sensor.STATE_OK):
                    fault = True

except KeyboardInterrupt:
    log("Ctrl-C detected")

valve_opto=0
alarm_relay=0
led_green=0
led_red=0

log("Shutdown opto, relay and leds")

# Set the opto and relay pins
ft232h.output(GPIO_PIN_DICT["valve_opto"],valve_opto)
ft232h.output(GPIO_PIN_DICT["alarm_relay"],alarm_relay)

# Set the led pins
ft232h.output(GPIO_PIN_DICT["led"]["green"],led_green)
ft232h.output(GPIO_PIN_DICT["led"]["red"],led_red)


from config.wsgi import *
from core.security.models import *
from django.contrib.auth.models import Permission
from core.user.models import User

dashboard = Dashboard()
dashboard.name = 'DSHMGER WEB V4.0'
dashboard.icon = 'fas fa-dice-d6'
dashboard.layout = 1
dashboard.card = ' '
dashboard.navbar = 'navbar-dark navbar-primary'
dashboard.brand_logo = ''
dashboard.sidebar = 'sidebar-dark-primary'
dashboard.save()

type = ModuleType()
type.name = 'Seguridad'
type.icon = 'fas fa-lock'
type.save()
print('insertado {}'.format(type.name))

module = Module()
module.moduletype_id = 1
module.name = 'Tipos de Módulos'
module.url = '/security/module/type/'
module.is_active = True
module.is_vertical = True
module.is_visible = True
module.icon = 'fas fa-door-open'
module.description = 'Permite administrar los tipos de módulos del sistema'
module.save()
for p in Permission.objects.filter(content_type__model=ModuleType._meta.label.split('.')[1].lower()):
    module.permits.add(p)
print('insertado {}'.format(module.name))

module = Module()
module.moduletype_id = 1
module.name = 'Módulos'
module.url = '/security/module/'
module.is_active = True
module.is_vertical = True
module.is_visible = True
module.icon = 'fas fa-th-large'
module.description = 'Permite administrar los módulos del sistema'
module.save()
for p in Permission.objects.filter(content_type__model=Module._meta.label.split('.')[1].lower()):
    module.permits.add(p)
print('insertado {}'.format(module.name))

module = Module()
module.moduletype_id = 1
module.name = 'Grupos'
module.url = '/security/group/'
module.is_active = True
module.is_vertical = True
module.is_visible = True
module.icon = 'fas fa-users'
module.description = 'Permite administrar los grupos de usuarios del sistema'
module.save()
for p in Permission.objects.filter(content_type__model=Group._meta.label.split('.')[1].lower()):
    module.permits.add(p)
print('insertado {}'.format(module.name))

module = Module()
module.moduletype_id = 1
module.name = 'Respaldos'
module.url = '/security/database/backups/'
module.is_active = True
module.is_vertical = True
module.is_visible = True
module.icon = 'fas fa-database'
module.description = 'Permite administrar los respaldos de base de datos'
module.save()
for p in Permission.objects.filter(content_type__model=DatabaseBackups._meta.label.split('.')[1].lower()):
    module.permits.add(p)
print('insertado {}'.format(module.name))

module = Module()
module.moduletype_id = 1
module.name = 'Conf. Dashboard'
module.url = '/security/dashboard/update/'
module.is_active = True
module.is_vertical = True
module.is_visible = True
module.icon = 'fas fa-tools'
module.description = 'Permite configurar los datos de la plantilla'
module.save()
print('insertado {}'.format(module.name))

module = Module()
module.moduletype_id = 1
module.name = 'Accesos'
module.url = '/security/access/users/'
module.is_active = True
module.is_vertical = True
module.is_visible = True
module.icon = 'fas fa-user-secret'
module.description = 'Permite administrar los accesos de los usuarios'
module.save()
for p in Permission.objects.filter(content_type__model=AccessUsers._meta.label.split('.')[1].lower()):
    module.permits.add(p)
print('insertado {}'.format(module.name))

module = Module()
module.moduletype_id = 1
module.name = 'Usuarios'
module.url = '/user/'
module.is_active = True
module.is_vertical = True
module.is_visible = True
module.icon = 'fas fa-user'
module.description = 'Permite administrar a los usuarios del sistema'
module.save()
for p in Permission.objects.filter(content_type__model=User._meta.label.split('.')[1].lower()):
    module.permits.add(p)
print('insertado {}'.format(module.name))

module = Module()
module.name = 'Cambiar password'
module.url = '/user/update/password/'
module.is_active = True
module.is_vertical = False
module.is_visible = True
module.icon = 'fas fa-key'
module.description = 'Permite cambiar tu password de tu cuenta'
module.save()
print('insertado {}'.format(module.name))

module = Module()
module.name = 'Editar perfil'
module.url = '/user/update/profile/'
module.is_active = True
module.is_vertical = False
module.is_visible = True
module.icon = 'fas fa-user'
module.description = 'Permite cambiar la información de tu cuenta'
module.save()
print('insertado {}'.format(module.name))

group = Group()
group.name = 'Administrador'
group.save()
print('insertado {}'.format(group.name))

for m in Module.objects.filter():
    gm = GroupModule()
    gm.module = m
    gm.group = group
    gm.save()
    for perm in m.permits.all():
        group.permissions.add(perm)
        grouppermission = GroupPermission()
        grouppermission.module_id = m.id
        grouppermission.group_id = group.id
        grouppermission.permission_id = perm.id
        grouppermission.save()

# user
u = User()
u.first_name = 'Juan Carlos'
u.last_name = 'Castañeda'
u.username = 'admin'
u.dni = '0928363993'
u.email = 'correo@gmail.com'
u.is_active = True
u.is_superuser = True
u.is_staff = True
u.set_password('Mabjuan_007@')
u.save()

u.groups.add(group)


#
# Escucha en tiempo real y almacena los tweets en un fichero de texto
#
import tweepy
import codecs
from secret import *

api = init_twitter('sysmanapy') #### PASO 1 (Pasamos por parámetro el usuario del cual cogerá las credenciales en el fichero secret)

class MyStreamListener(tweepy.StreamListener): #### PASO 2

	#cada vez que se publica un estado
	def on_status(self, status):	#### PASO 6	Cada vez que se reciba un tweet con el filtro indicado, extraemos sus datos
		autor = status.user.screen_name
		print('Autor: '+autor)
		print('Idioma: '+status.lang)
		print('Estado: \n'+status.text)

		api.create_favorite(status.id); # Damos like y retweet
		api.update_status("Genial! soy el script de @nievesborrero y @PabloLeonPsi, encantado "+ autor , in_reply_to_status_id=status.id);

		print("-"*10)

		# Almacenamos en un documento
		with codecs.open("streamSearch.txt", "a", "utf-8") as myfile:
			myfile.write('Autor: '+status.user.screen_name+'\n')
			myfile.write('Estado: \n'+status.text+'\n')
			myfile.write('\n-----\n')

if __name__ == '__main__':

	myStreamListener = MyStreamListener() #### PASO 3
	myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener) #### PASO 4
	myStream.filter(track=['sysmana2018']) #### PASO 5


# -*- coding: utf-8 -*-
import numpy as np

def show_path(start,end,path):
    cur = start
    order = []
    while(cur!=end):
        order.append(cur)
        cur = path[cur][end]
    order.append(cur)
    print(order)
    return

e, v = map(int, input().split())
#graph = [[0 for i in range(v)] for j in range(v)]
graph = np.zeros((v,v)).astype('uint16')
#path = [[0 for i in range(v)] for j in range(v)]
path = np.zeros((v,v)).astype('uint16')

for i in range(v):
    for j in range(v):
            graph[i][j] = 0 if (i==j) else 255
            path[i][j] = j

print(graph)
for i in range(e):
    src, dst, cost = map(int, input().split())
    graph[src][dst] = cost
    graph[dst][src] = cost

print(graph)
print(path)
for k in range(v):
    for i in range(v):
        for j in range(v):
            #print(' cost of '+str(i)+' to '+str(j)+'is '+str(graph[i][j])+' '+str(graph[i][k]+graph[k][j])+' via '+str(k))

            #graph[i][j] = min(graph[i][j],graph[i][k]+graph[k][j])
            if(graph[i][k]+graph[k][j]<graph[i][j]):
                graph[i][j] = graph[i][k]+graph[k][j]
                path[i][j] = path[i][k]

print(graph)
print(path)
for i in range(9):
    show_path(9,i,path)


"""updating DATABASE_URI

Revision ID: a08e52b94bd2
Revises:
Create Date: 2018-10-30 14:36:17.474564

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'a08e52b94bd2'
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('categories',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=60), nullable=False),
    sa.Column('description', sa.Text(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_categories_name'), 'categories', ['name'], unique=True)
    op.create_table('users',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('username', sa.String(length=36), nullable=False),
    sa.Column('email', sa.String(length=72), nullable=True),
    sa.Column('psw_hash', sa.String(length=128), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
    op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True)
    op.create_table('beings',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=60), nullable=False),
    sa.Column('meaning', sa.Text(), nullable=False),
    sa.Column('text', sa.Text(), nullable=True),
    sa.Column('source', sa.String(length=100), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('category_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['category_id'], ['categories.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_beings_name'), 'beings', ['name'], unique=True)
    op.create_table('stories',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('title', sa.String(length=60), nullable=False),
    sa.Column('meaning', sa.Text(), nullable=False),
    sa.Column('text', sa.Text(), nullable=True),
    sa.Column('source', sa.String(length=100), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('category_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['category_id'], ['categories.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_stories_title'), 'stories', ['title'], unique=True)
    op.create_table('comments',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('subject', sa.String(length=60), nullable=False),
    sa.Column('content', sa.Text(), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('story_id', sa.Integer(), nullable=True),
    sa.Column('being_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['being_id'], ['beings.id'], ),
    sa.ForeignKeyConstraint(['story_id'], ['stories.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('comments')
    op.drop_index(op.f('ix_stories_title'), table_name='stories')
    op.drop_table('stories')
    op.drop_index(op.f('ix_beings_name'), table_name='beings')
    op.drop_table('beings')
    op.drop_index(op.f('ix_users_username'), table_name='users')
    op.drop_index(op.f('ix_users_email'), table_name='users')
    op.drop_table('users')
    op.drop_index(op.f('ix_categories_name'), table_name='categories')
    op.drop_table('categories')
    # ### end Alembic commands ###


# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.linux-x86_64/egg/watchme/client/protect.py
# Compiled at: 2020-04-10 14:08:49
# Size of source mod 2**32: 693 bytes
"""

Copyright (C) 2019-2020 Vanessa Sochat.

This Source Code Form is subject to the terms of the
Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

"""
from watchme import get_watcher

def main(args, extra):
    """protect or freeze a watcher, or disable.
    """
    name = args.watcher[0]
    watcher = get_watcher(name, base=(args.base), create=False)
    if args.action in ('on', 'off'):
        watcher.protect(args.action)
    else:
        if args.action == 'freeze':
            watcher.freeze()
        else:
            if args.action == 'unfreeze':
                watcher.unfreeze()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# cython:profile=True
"""
Discrete Pulse Transform (Michiel, Februay 2018)
Python Code is a copypasta from (Dirk Laurie, July 2009)
@author: dekockmi
"""
from math import sqrt  # Sqrt function
from collections import deque  # Fast container

"""We are importing deque so use it for queue"""

class Queue(deque):
    get = deque.popleft
    put = deque.append

"""Define a Node using set class"""

class Node(set):
    def __init__(self, tag, value, size=1):
        super().__init__()
        self.parent = self
        self.tag = tag
        self.value = value
        self.size = size

    def active(self, size):
        return self.parent is self and self.size == size

    def __repr__(self):
        return "%s_%r(%r)=>%s;%s" % (self.tag, self.size, self.value, self.parent.tag, list(self))

# """Define arc function: connects node a and b"""
# def arc(a,b): a.add(b.tag); b.add(a.tag)
"""Define the Scheduler that returns feautures of the data"""

class Schedule:
    def __init__(self, cutoff=100):
        self.cutoff = cutoff
        self.boxes = []
        for k in range(cutoff):
            self.boxes.append(deque())
        # self.boxes = [deque() for k in range(cutoff)]
        self.start = self.stop = 0
        # start is the number of the box from which the next item will be popped.
        # stop-1 is the number of the last box that has actually been used.
        self.tiebreak = True
        self.store = {}

    def __getitem__(self, i):
        # Return the deque that holds items of width i+1.
        if i < self.cutoff:
            return self.boxes[i]
        if i not in self.store:
            self.store[i] = deque()
        return self.store[i]

    def current(self):
        # Find the deque off which the next item will be popped.
        box = None
        while self.start < self.stop:
            if self.start < self.cutoff:
                box = self.boxes[self.start]
            elif self.start in self.store:
                box = self.store[self.start]
            if box:
                return box
            self.start += 1
            self.tiebreak = policy
        return None

    def pop(self):
        while True:
            pulse = None
            box = self.current()
            if not box:
                break
            if self.tiebreak:
                pulse = box.pop()
            else:
                pulse = box.popleft()
            if pulse is None:
                break
            if pulse.active(self.start + 1):
                break
        return pulse

    def push(self, node, size, sign):
        self.stop = max(self.stop, size)
        box = self[size - 1]
        if sign > 0:
            box.append(node)
        else:
            box.appendleft(node)

    def debug(self):
        print('-------- start = %i ---- stop = %i --------') % (self.start, self.stop)
        for k in range(self.cutoff):
            if self.boxes[k]:
                print('size %i' % (k + 1), self.boxes[k])
        if self.store:
            print('store', self.store)

class GraphFunction:
    """ The graph is initialised by the list of nodes and the arcs that connect them
        Also initialise a Scheduler for the graph"""

    def __init__(self, values, arcs):
        n = len(values)
        self.schedule = Schedule(cutoff=int(sqrt(n)))
        self.nodes = tuple([Node(a, values[a]) for a in range(n)])
        self.root = None
        for (a, b) in arcs:
            # arc(graph.nodes[a], graph.nodes[b])
            self.nodes[a].add(self.nodes[b].tag)
            self.nodes[b].add(self.nodes[a].tag)

    def shrink(self, node):
        # read the paper
        agenda = Queue([node])
        sign = [0, 0]
        while agenda:
            item = agenda.get()
            for c in list(item):
                # graph.count += 1
                child = self.nodes[c]
                child.remove(item.tag)
                if child.parent is not node:
                    if node.value == child.value:
                        agenda.put(child)
                        child.parent = node
                        child.value = 0
                        node.discard(child.tag)
                        node.size += child.size
                    else:
                        node.add(child.tag)
                        child.add(node.tag)
                        # arc(node,child)
                        sign[node.value > child.value] = 1
        feature = sign[1] - sign[0]
        # graph.border += len(node)
        if not node:
            self.root = node
        elif feature:
            self.schedule.push(node, node.size, feature)

    def nearest(self, node):
        neg = -float("inf")
        pos = float("inf")
        nearest = None
        for i in node:
            diff = int(node.value) - int(self.nodes[i].value)
            if 0 < diff < pos:
                pos = diff
                nearest = i
            elif 0 >= diff > neg:
                neg = diff
                nearest = i
        return self.nodes[nearest]

    def dpt(self, thispolicy=True):
        # Transforms graph into a DPT
        global policy
        policy = thispolicy
        schedule = self.schedule

        for node in self.nodes:
            if node.parent is node:
                self.shrink(node)
        while True:
            node = schedule.pop()
            if node is None:
                break
            survivor = self.nearest(node)
            height = int(node.value) - int(survivor.value)
            node.value = survivor.value
            self.shrink(survivor)
            node.value = height

        #for node in self.nodes:
        #    node.clear()
        #for child in self.nodes:
        #    if child is not self.root:
        #        child.parent.add(child.tag)

    def revalue(self):
        for child in self.traverse():
            child.value += child.parent.value

    def traverse(self, root=None, include_root=False):
        """tree.traverse(node) returns a breadth-first iterator for the subtree
        starting at but not including node.  Default: node is tree.root.
        tree.traverse(node,include_root=True) does include node.  """
        if root is None:
            root = self.root
        if include_root:
            yield root
        agenda = Queue([root])
        while agenda:
            node = agenda.get()
            for c in node:
                child = self.nodes[c]
                yield child
                agenda.put(child)

    def __repr__(self):
        return str(self.nodes)

if __name__ == "__main__":
    import numpy as np
    from PIL import Image
    im = Image.open('Dna origami tile0013.tif')
    imarray = np.array(im)
    # im.show()
    # Create a Graph from a 2D Image
    # [xlen, ylen] = imarray.shape
    [xlen, ylen] = [2 << 8, 2 << 8]
    edges2D = [(j * ylen + k, j * ylen + k + 1) for j in range(xlen) for k in range(ylen - 1)] + [
        (j * ylen + k, (j + 1) * ylen + k) for j in range(xlen - 1) for k in range(ylen)]
    data = GraphFunction(imarray[:xlen, :ylen].flatten(), edges2D)
    data.dpt()
    data.revalue()
    output = np.asarray([a.value for a in data.nodes], dtype=np.uint8).reshape(xlen, ylen)
    out_image = Image.fromarray(output)
    out_image.show()


s = input()
listOfnums = list(map(int,s[s.index("[")+1:s.index("]")].split(",")))
k = int(s[s.find("k")+4])
t = int(s[s.find("t")+4])
result = False
for i in range(0,len(listOfnums)-k):

    for j in range(i+1,min(len(listOfnums),i+k+1)):

        if abs(listOfnums[i]-listOfnums[j])<=t:
            result = True
            break
if result:print("true")
else :print("false")


'''
/home/jmh563/compiled_python3/bin/python3
'''
import argparse

header = '''#!/bin/bash
#SBATCH -J {NAME}                         # Job name
#SBATCH -o {NAME}.out                     # Name of stdout output log file (%j expands to jobID)
#SBATCH -e {NAME}.err                     # Name of stderr output log file (%j expands to jobID)
#SBATCH -N 1                             # Total number of nodes requested
#SBATCH -n 1                             # Total number of cores requested
#SBATCH --mem=64000                      # Total amount of (real) memory requested (per node)
#SBATCH -t 168:00:00                     # Time limit (hh:mm:ss)
#SBATCH --partition=default_gpu          # Request partition for resource allocation
#SBATCH --gres=gpu:1                     # Specify a list of generic consumable resources (per node)
'''

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--python_executable',
        default='/home/jmh563/compiled_python3/bin/python3')
    parser.add_argument(
        '--commands_to_run',
        default='test_time_commands_to_run.txt')
    return parser.parse_args()

def main():
    args = parse_args()
    with open(args.commands_to_run) as f:
        all_commands = [x.strip() for x in f.readlines()]

    for idx in range(len(all_commands)):
        fname = 'test_{}.sub'.format(idx)
        cur_header = header.format(NAME='test_{}'.format(idx))

        with open(fname, 'w') as f:
            f.write(cur_header + '\n\n')
            f.write('cd ..; ')
            f.write(all_commands[idx] + ';\n')

if __name__ == '__main__':
    main()


from unittest import TestCase

import os
import shutil

from nimrod.tests.utils import get_config
from nimrod.tests.utils import calculator_project_dir
from nimrod.tests.utils import calculator_clean_project
from nimrod.tests.utils import calculator_target_dir
from nimrod.tests.utils import calculator_src_aor_1

from nimrod.utils import get_java_files, get_class_files
from nimrod.tools.java import Java
from nimrod.tools.maven import Maven
from nimrod.tools.randoop import Randoop
from nimrod.tools.safira import Safira

class TestRandoop(TestCase):

    def setUp(self):
        self.java_home = get_config()['java_home']
        self.maven_home = get_config()['maven_home']

        self.java = Java(self.java_home)
        self.maven = Maven(self.java, self.maven_home)

        self.maven.compile(calculator_project_dir(), 10)

    def test_generate(self):

        tests_src = os.path.join(calculator_project_dir(), 'randoop')

        randoop = Randoop(
            java=self.java,
            classpath=os.path.join(calculator_target_dir(), 'classes'),
            tests_src=tests_src,
            sut_class='br.ufal.ic.easy.operations.Sum'
        )
        randoop.parameters = ['--time-limit=1']

        (suite_name, suite_dir, suite_classes_dir,
         suite_classes) = randoop.generate()

        self.assertTrue(suite_name.startswith('randoop'))
        self.assertTrue(os.path.exists(suite_dir))

        self.assertTrue(len(get_java_files(suite_dir)) > 1)
        self.assertTrue(len(get_class_files(suite_classes_dir)) > 1)
        self.assertEquals(1, len(suite_classes))

        # shutil.rmtree(tests_src)

    def test_generate_with_impact_analysis(self):
        tests_src = os.path.join(calculator_project_dir(), 'randoop')
        classes_dir = os.path.join(calculator_target_dir(), 'classes')

        randoop = Randoop(
            java=self.java,
            classpath=classes_dir,
            tests_src=tests_src,
            sut_class='br.ufal.ic.easy.operations.Sum'
        )
        randoop.parameters = ['--time-limit=1']

        safira = Safira(self.java, classes_dir,
                        calculator_src_aor_1())

        (suite_name, suite_dir, suite_classes_dir,
         suite_classes) = randoop.generate_with_impact_analysis(safira)

        self.assertTrue(suite_name.startswith('randoop'))
        self.assertTrue(os.path.exists(suite_dir))

        self.assertTrue(len(get_java_files(suite_dir)) > 1)
        self.assertTrue(len(get_class_files(suite_classes_dir)) > 1)
        self.assertEquals(1, len(suite_classes))

        shutil.rmtree(tests_src)

    def tearDown(self):
        calculator_clean_project()


__author__ = 'fshaw'

import lxml.etree as etree
import pdb

class EnaStudyForm:
    # simple class to return parsed data
    def __init__(self, n=None, tn=None, t=None, values=[]):
        self.name = n
        self.tidy_name = tn
        self.type = t
        self.values = []

def get_study_form_controls(path):
    fileHandle = open(path, 'r')
    root = etree.parse(fileHandle)

    namespaces = {'xs': 'http://www.w3.org/2001/XMLSchema'}

    descriptor = root.findall(".//xs:element[@name='DESCRIPTOR']//xs:complexType//xs:all/xs:element", namespaces)

    str = ''

    # out is an array of EnaStudyForm objects
    out = []

    for el in iter(descriptor):
        el_name = el.get('name')
        el_name_tidy = el.get('name').replace('_', ' ').title()
        el_type = el.get('type')
        optional = int(el.get('minOccurs')) == 0
        ena = EnaStudyForm()
        ena.name = el_name
        ena.tidy_name = el_name_tidy

        if (el_type == 'xs:string'):

            if (el.get('name') == 'STUDY_ABSTRACT'):
                ena.type = 'textarea'
                out.append(ena)
            else:
                ena.type = 'input'
                out.append(ena)

        #make dropdown for study type
        if el.get('name') == 'STUDY_TYPE':
            enum = el.findall(".//xs:enumeration", namespaces)
            ena.type = 'select'
            for e in iter(enum):
                ena.values.append(e.get('value'))
                #str += "<option value='" + e.get('value') + "'>" + e.get('value') + "</option>"
            out.append(ena)
            #str += "</select>"
            #str += "
"
    return out

def get_sample_form_controls(path):
    fileHandle = open(path, 'r')
    root = etree.parse(fileHandle)
    namespaces = {'xs': 'http://www.w3.org/2001/XMLSchema'}

    sample_name = root.findall("./xs:complexType//xs:element", namespaces)

    str = ''

    for el in iter(sample_name):
        el_name = el.get('name')
        el_name_tidy = el.get('name').replace('_', ' ').title()
        el_type = el.get('type')
        el_required = False
        # pdb.set_trace()
        if (el.get('minOccurs') != None):
            el_required = (int(el.get('minOccurs')) > 0)

        if (el_type == 'xs:string' or el_type == 'xs:int'):
            str += "
"
            str += "" + el_name_tidy + ""
            #pdb.set_trace()
            if el_required:
                str += "<input type='text' class='form-control' id='" + el_name + "' name='" + el_name + "' xml_type='" + el_type + "' required/>"
            else:
                str += "<input type='text' class='form-control' id='" + el_name + "' name='" + el_name + "' xml_type='" + el_type + "'/>"
            str += "
"

    return str

def get_library_dropdown(xml_file, lib_part):
    # method to get html for library strategy dropdown
    fileHandle = open(xml_file, 'r')
    root = etree.parse(fileHandle)
    namespaces = {'xs': 'http://www.w3.org/2001/XMLSchema'}
    search_str = ".//xs:element[@name=\"" + lib_part + "\"]//xs:enumeration"
    options = root.findall(search_str, namespaces)
    out = ''
    for o in options:
        out += '<option>' + o.get('value') + '</option>'
    return out


'''
Created on Dec 20, 2012

@author: slarinier
'''
from pymongo.connection import Connection
import pymongo
import tldextract
class DNSTree(object):
    '''
    classdocs
    '''

    def __init__(self,db_value):
        '''
        Constructor
        '''
        connection=Connection('localhost',27017)
        self.db=connection[db_value]
    def process(self):
        list_domains=self.db['new_domaines'].distinct('domaine')
        for domain in list_domains:
            tldex=tldextract.extract(domain,False)
            tld=tldex.tld
            subdomains=tldex.subdomain
            domain_value=tldex.domain
            print (tld+','+domain_value+','+','.join(subdomains[::-1]).replace('www','')).replace(',,',',')


import torch
import pickle
from fastai.vision import *

import warnings
warnings.filterwarnings("ignore", category=torch.serialization.SourceChangeWarning)

def predict(filename):
    torch.load(open('data/export.pkl', 'rb'), map_location='cpu')
    learn = load_learner('./data')
    img = open_image(filename)
    preds = learn.predict(img)
    return preds

if __name__ == '__main__':
    testfilename = 'data/test/test.jpeg'
    preds = predict(testfilename)
    print(preds)


#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Andre Anjos <andre.dos.anjos@gmail.com>
# Sun 28 Apr 18:01:50 2013

"""Commands for inspecting the available packages for installation
"""

import re
from .. import repo
from .. import config

def action(args):
  """Lists installable packages

  Keyword arguments:

  args
    Arguments parsed by the ``argparse`` module.
  """

  logger = config.prepare_logger('winix', args.verbose)

  from ..version import __version__
  logger.info('Winix Package Installation Tool v%s' % __version__)

  packages = repo.load_packages(args.repo)
  regexp = re.compile(args.package, re.I)
  to_list = [k for k in packages if regexp.search(k.name)]

  logger.info('[ %d packages available at %s - listing %d ]' % (len(packages), args.repo, len(to_list)))

  if args.detail:

    first = True
    for p in to_list:
      if not first: print(79*'-')
      else: first = False
      print("Name: %s (%s.%s)" % (p.name, p.__class__.__module__,
        p.__class__.__name__))
      print("Version: %s" % p.version)
      print("Package: version %s, by %s <%s>" % (p.package_version,
          p.package_author_name, p.package_author_email))
      deps = ', '.join(p.dependencies) if p.dependencies else '(none)'
      print("Dependencies: %s" % deps)
      print("Website: %s" % p.website)
      print("Description: %s" % p.oneline_description)

  else:

    widths = dict(
        name=25,
        version=12,
        desc=36,
        )

    string = '%%-%(name)ds | %%%(version)ds | %%-%(desc)ds' % widths

    for p in to_list:
      print(string % (
        p.name[:widths['name']],
        (p.version + '-' + p.package_version)[:widths['version']],
        p.oneline_description[:widths['desc']],
        ))

  return 0

def setup(subparsers):
  """Sets up the parser to perform listing actions"""

  parser = subparsers.add_parser('list', help='lists installable packages')

  parser.add_argument('-v', '--verbose', action='count', default=0,
      help="Increase the verbosity level on execution")

  parser.add_argument('-r', '--repo', '--repository', metavar='REPO',
      dest='repo', default='winix.repo.default', help='The python package that will be used as repository (defaults to %(default)s)')

  parser.add_argument('-d', '--detail', default=False, action='store_true',
      help='If set, give out details of selected packages instead of a one-line summary (the default)')

  parser.add_argument('package', nargs='?', metavar='PACKAGE', default='.*',
      help="Regular expression (python-style) of names to list (defaults to '%(default)s')")

  parser.set_defaults(func=action)


from django.shortcuts import render
from django.views import View
from .models import Person
from apps.ranking.models import RMR
import json
# Create your views here.

class PersonView(View):

    def get(self, request):
        person_id = request.GET.get("id", None)
        person = Person.objects.filter(id=person_id)[0]
        image = json.loads(person.image)
        return render(request, "person.html", {
            "actor": person,
            "image": image[:5],
            "image_count": len(image)
        })

class PhotoView(View):

    def get(self, request):
        person_id = request.GET.get('id', None)
        person = Person.objects.filter(id=person_id)[0]
        image = json.loads(person.image)
        return render(request, "person_photo.html", {
            "person": person,
            "image": image,
            "image_count": len(image)
        })


'''Utility functions and classes for handling image datasets.'''

import os.path as osp
import numpy as np
import tensorflow as tf

def read_image(path, to_bgr=True):
    '''Returns the image at the given path as a tensor.'''
    # Read the file
    file_data = tf.read_file(path)
    # Figure out the image format from the extension
    ext = osp.splitext(path)[-1].lower()
    if ext == '.png':
        decoder = tf.image.decode_png
    elif ext in ('.jpg', '.jpeg'):
        decoder = tf.image.decode_jpeg
    else:
        raise ValueError('Unsupported image extension: {}'.format(ext))
    img = decoder(file_data, channels=3)
    if to_bgr:
        # Convert from RGB channel ordering to BGR
        # This matches, for instance, how OpenCV orders the channels.
        img = tf.reverse(img, [False, False, True])
    return img

def _load_image(path, scale, isotropic, crop, mean):
    '''Loads and pre-processes the image at the given path.
    scale : The image wil be first scaled to this size.
            If isotropic is true, the smaller side is rescaled to this,
            preserving the aspect ratio.
    crop  : After scaling, a central crop of this size is taken.
    mean  : Subtracted from the image
    '''
    # Read in the image
    img = read_image(path)
    # Rescale
    if isotropic:
        img_shape = tf.to_float(tf.shape(img)[:2])
        min_length = tf.minimum(img_shape[0], img_shape[1])
        new_shape = tf.to_int32((scale / min_length) * img_shape)
    else:
        new_shape = tf.pack([scale, scale])
    img = tf.image.resize_images(img, new_shape[0], new_shape[1])
    # Center crop
    # Use the slice workaround until crop_to_bounding_box supports deferred tensor shapes
    # See: https://github.com/tensorflow/tensorflow/issues/521
    offset = (new_shape - crop) / 2
    img = tf.slice(img, begin=tf.pack([offset[0], offset[1], 0]), size=tf.pack([crop, crop, -1]))
    # Mean subtraction
    return tf.to_float(img) - mean

def load_image(path, spec):
    '''Load a single image, processed based on the given spec.'''
    return _load_image(path=path,
                       scale=spec.scale_size,
                       isotropic=spec.isotropic,
                       crop=spec.crop_size,
                       mean=spec.mean)

def load_images(paths, spec):
    '''Load multiple images, processed based on the given spec.'''
    return tf.pack([load_image(path, spec) for path in paths])

class ImageNet(object):
    '''Iterates over the ImageNet validation set.'''

    def __init__(self, val_path, data_path, data_spec):
        # Read in the ground truth labels for the validation set
        # The get_ilsvrc_aux.sh in Caffe's data/ilsvrc12 folder can fetch a copy of val.txt
        gt_lines = open(val_path).readlines()
        gt_pairs = [line.split() for line in gt_lines]
        # Get the full image paths
        # You will need a copy of the ImageNet validation set for this.
        self.image_paths = [osp.join(data_path, p[0]) for p in gt_pairs]
        # The corresponding ground truth labels
        self.labels = np.array([int(p[1]) for p in gt_pairs])
        # The data specifications for the model being validated (for preprocessing)
        self.data_spec = data_spec

    def batches(self, n):
        '''Yields a batch of up to n preprocessed image tensors and their ground truth labels.'''
        for i in xrange(0, len(self.image_paths), n):
            images = load_images(self.image_paths[i:i + n], self.data_spec)
            labels = self.labels[i:i + n]
            yield (images, labels)

    def __len__(self):
        '''Returns the number of instances in the validation set.'''
        return len(self.labels)