import os,sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # to import shared utils
import torch
import torch.optim as optim
import torch.nn as nn
import numpy as np
import argparse
import time
import os
import shutil
import copy
from IPython import embed

from DataLoader import ClevrDataLoader
from model.net import XNMNet
from utils.misc import todevice
from validate import validate

import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)-8s %(message)s')
logFormatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
rootLogger = logging.getLogger()

def train(args):
    logging.info("Create train_loader and val_loader.........")
    train_loader_kwargs = {
        'question_pt': args.train_question_pt,
        'scene_pt': args.train_scene_pt,
        'vocab_json': args.vocab_json,
        'batch_size': args.batch_size,
        'ratio': args.ratio,
        'shuffle': True
    }
    val_loader_kwargs = {
        'question_pt': args.val_question_pt,
        'scene_pt': args.val_scene_pt,
        'vocab_json': args.vocab_json,
        'batch_size': args.batch_size,
        'shuffle': False
    }

    train_loader = ClevrDataLoader(**train_loader_kwargs)
    val_loader = ClevrDataLoader(**val_loader_kwargs)

    logging.info("Create model.........")
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    model_kwargs = { k:v for k,v in vars(args).items() if k in {
        'dim_v', 'dim_pre_v', 'num_edge_cat', 'num_class', 'num_attribute',
        } }
    model_kwargs_tosave = copy.deepcopy(model_kwargs)
    model_kwargs['vocab'] = train_loader.vocab
    model = XNMNet(**model_kwargs).to(device)
    logging.info(model)

    optimizer = optim.Adam(model.parameters(), args.lr, weight_decay=args.l2reg)
    scheduler = optim.lr_scheduler.MultiStepLR(optimizer=optimizer, milestones=[int(1/args.ratio)], gamma=0.1)
    criterion = nn.CrossEntropyLoss().to(device)
    logging.info("Start training........")
    tic = time.time()
    iter_count = 0
    for epoch in range(args.num_epoch):
        for i, batch in enumerate(train_loader.generator()):
            iter_count += 1
            progress = epoch+i/len(train_loader)
            answers, questions, *batch_input = \
                    [todevice(x, device) for x in batch]

            logits, others = model(*batch_input)
            loss = criterion(logits, answers)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            if (i+1) % (len(train_loader) // 10) == 0:
                logging.info("Progress %.3f  loss = %.3f" % (progress, loss.item()))
        scheduler.step()
        if (epoch+1) % 1 == 0:
            valid_acc = validate(model, val_loader, device)
            logging.info('\n ~~~~~~ Valid Accuracy: %.4f ~~~~~~~\n' % valid_acc)

            save_checkpoint(epoch, model, optimizer, model_kwargs_tosave, os.path.join(args.save_dir, 'model.pt'))
            logging.info(' >>>>>> save to %s <<<<<<' % (args.save_dir))

def save_checkpoint(epoch, model, optimizer, model_kwargs_tosave, filename):
    state = {
        'epoch': epoch,
        'state_dict': model.state_dict(),
        'optimizer': optimizer.state_dict(),
        'model_kwargs': model_kwargs_tosave,
        }
    torch.save(state, filename)

def main():
    parser = argparse.ArgumentParser()
    # input and output
    parser.add_argument('--save_dir', type=str, required=True, help='path to save checkpoints and logs')
    parser.add_argument('--input_dir', required=True)
    parser.add_argument('--train_question_pt', default='train_questions.pt')
    parser.add_argument('--train_scene_pt', default='train_scenes.pt')
    parser.add_argument('--val_question_pt', default='val_questions.pt')
    parser.add_argument('--val_scene_pt', default='val_scenes.pt')
    parser.add_argument('--vocab_json', default='vocab.json')
    # training parameters
    parser.add_argument('--lr', default=0.001, type=float)
    parser.add_argument('--l2reg', default=0, type=float)
    parser.add_argument('--num_epoch', default=5, type=int)
    parser.add_argument('--batch_size', default=256, type=int)
    parser.add_argument('--seed', type=int, default=666, help='random seed')
    parser.add_argument('--ratio', default=1, type=float, help='ratio of training examples')
    # model hyperparameters
    parser.add_argument('--dim_pre_v', default=15, type=int, help='dimension of one-hot node representation')
    parser.add_argument('--dim_v', default=128, type=int, help='hidden dimension')
    parser.add_argument('--num_class', default=28, type=int)
    parser.add_argument('--num_edge_cat', default=9, type=int, help='number of relationship categories')
    parser.add_argument('--num_attribute', default=15, type=int, help='number of attribute values')
    args = parser.parse_args()

    # make logging.info display into both shell and file
    os.mkdir(args.save_dir)
    fileHandler = logging.FileHandler(os.path.join(args.save_dir, 'stdout.log'))
    fileHandler.setFormatter(logFormatter)
    rootLogger.addHandler(fileHandler)
    # args display
    for k, v in vars(args).items():
        logging.info(k+':'+str(v))
    # concat obsolute path of input files
    args.train_question_pt = os.path.join(args.input_dir, args.train_question_pt)
    args.train_scene_pt = os.path.join(args.input_dir, args.train_scene_pt)
    args.vocab_json = os.path.join(args.input_dir, args.vocab_json)
    args.val_question_pt = os.path.join(args.input_dir, args.val_question_pt)
    args.val_scene_pt = os.path.join(args.input_dir, args.val_scene_pt)
    # set random seed
    torch.manual_seed(args.seed)
    np.random.seed(args.seed)

    train(args)

if __name__ == '__main__':
    main()


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import (QWidget, QSlider,
    QLabel, QApplication)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
import sys

class Ui_MainWindow(object): #creating the window
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(927, 506)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMinimumSize(QtCore.QSize(927, 506))
        MainWindow.setMaximumSize(QtCore.QSize(927, 506))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("../Downloads/fb-icon-9.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
        self.plainTextEdit.setGeometry(QtCore.QRect(210, 330, 511, 111))
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(420, 250, 93, 28))
        self.pushButton.clicked.connect(self.button_click)
        self.pushButton.setObjectName("pushButton")
        self.horizontalSlider_2 = QtWidgets.QSlider(self.centralwidget)
        self.horizontalSlider_2.setGeometry(QtCore.QRect(198, 140, 541, 31))
        self.horizontalSlider_2.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider_2.setFocusPolicy(Qt.NoFocus)
        self.horizontalSlider_2.setRange(1, 150)

        self.horizontalSlider_2.valueChanged.connect(self.valu2)

        self.horizontalSlider_2.setObjectName("horizontalSlider_2")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(760, 130, 101, 51))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.lineEdit_2.textChanged.connect(self.value4)
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(70, 130, 111, 51))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(760, 50, 101, 51))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.textChanged.connect(self.value3)

        self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
        self.horizontalSlider.setGeometry(QtCore.QRect(200, 50, 541, 31))
        self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider.setFocusPolicy(Qt.NoFocus)
        self.horizontalSlider.setRange(1, 200)

        self.horizontalSlider.valueChanged.connect(self.valu)
        self.horizontalSlider.setObjectName("horizontalSlider")

        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(70, 50, 91, 31))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label.setFont(font)
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def valu2(self):
        self.k = str(self.horizontalSlider_2.value())
        self.lineEdit_2.setText(self.k)

    def valu(self):
        self.a = str(self.horizontalSlider.value())
        self.lineEdit.setText(self.a)

    def value3(self):
        self.j=str(self.lineEdit.text())
        self.horizontalSlider.setSliderPosition(int(self.j))
        if int(self.j)==0:
            self.horizontalSlider.setSliderPosition(int(1))

    def value4(self):
        self.j=str(self.lineEdit_2.text())
        self.horizontalSlider_2.setSliderPosition(int(self.j))
        if int(self.j)==0:
            self.horizontalSlider.setSliderPosition(int(1))

    def button_click(self): #calculations
        self.j = int(self.lineEdit_2.text())
        self.k = int(self.lineEdit.text())
        self.bmi =self.j/((self.k/100)**2)
        self.string="your body mass index is "+str(self.bmi)
        print (self.string)
        self.plainTextEdit.setPlainText(self.string)

    def retranslateUi(self, MainWindow): #labelling
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "BMI"))
        self.pushButton.setText(_translate("MainWindow", "calculate"))
        self.label_2.setText(_translate("MainWindow", "weight"))
        self.label.setText(_translate("MainWindow", "height"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())


# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import torch.nn as nn
import torch
class Model(nn.Module):
    def __init__(self, encoder):
        super(Model, self).__init__()
        self.encoder = encoder

    def forward(self, code_inputs=None, nl_inputs=None, cls=False):
        if code_inputs is not None:
            outputs = self.encoder(code_inputs,attention_mask=code_inputs.ne(1))[0]
            outputs = (outputs * code_inputs.ne(1)[:,:,None]).sum(1)/code_inputs.ne(1).sum(1)[:,None]
            return torch.nn.functional.normalize(outputs, p=2, dim=1)
        else:
            outputs = self.encoder(nl_inputs,attention_mask=nl_inputs.ne(1))[0]
            outputs = (outputs * nl_inputs.ne(1)[:,:,None]).sum(1)/nl_inputs.ne(1).sum(1)[:,None]
            return torch.nn.functional.normalize(outputs, p=2, dim=1)


from __future__ import print_function
import libvirt
from xml.etree import ElementTree

class LibvirtConnection(object):
    """This class represents the connection to libvirt"""

    def __init__(self):
        self.conn = libvirt.open('qemu:///system')
        if self.conn is None:
            print('Failed to open connection to qemu:///system')
            exit(1)

    # def get_domain_ids(self):
    #     domain_ids = self.conn.listDomainsID()
    #     if domain_ids is None:
    #         print('Failed to get a list of domain IDs')
    #     return domain_ids

    # for VM infos
    def get_domain_info(self, domain_name):
        dom = LibvirtConnection.domain_lookup(self, domain_name)
        state, maxmem, mem, cpus, cput = dom.info()
        return state, maxmem, cpus

    def domain_lookup(self, domain_name):
        dom = self.conn.lookupByName(domain_name)
        if dom is None:
            print('Failed to find the domain ', domain_name)
            exit(1)
        return dom

    # for RUI
    def get_vcpu_stats(self, domain_name):
        dom = self.conn.lookupByName(domain_name)
        # print "CPU stat in nanoseconds: ", dom.getCPUStats(True)
        cpu_time_seconds = (dom.getCPUStats(True)[0]['cpu_time']) / 1000000000.0
        return cpu_time_seconds

    def get_memory_stats(self, domain_name):
        dom = self.conn.lookupByName(domain_name)
        # print("Memory stats in Byte: ", dom.memoryStats())
        memory_stats = dom.memoryStats()
        swap_in = None
        if 'swap_in' in memory_stats:
            swap_in = memory_stats['swap_in']
        rss = memory_stats['rss']
        if swap_in is not None:
            return swap_in + rss
        else:
            return rss

    def get_network_stats(self, domain_name):
        dom = self.conn.lookupByName(domain_name)
        tree = ElementTree.fromstring(dom.XMLDesc())
        iface = tree.find('devices/interface/target').get('dev')
        stats = dom.interfaceStats(iface)
        return stats[0], stats[4]

    def get_disk_stats(self, domain_name):
        dom = self.conn.lookupByName(domain_name)
        tree = ElementTree.fromstring(dom.XMLDesc())
        device = tree.find('devices/disk/target').get('dev')
        rd_req, rd_bytes, wr_req, wr_bytes, err = dom.blockStats(device)
        return rd_bytes, wr_bytes, rd_req, wr_req


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright (c) 2020 ASMlover. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list ofconditions and the following disclaimer.
#
#  * Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materialsprovided with the
#    distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import asyncio
import uvloop

asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

WAIT_MSG = 1
READ_MSG = 2

async def on_serve(reader, writer):
    writer.write("*".encode())
    await writer.drain()

    status = WAIT_MSG
    while True:
        data = await reader.read(1024)
        if not data:
            break
        rbuf = data.decode()

        wbuf = []
        for c in rbuf:
            if status == WAIT_MSG:
                if c == '^':
                    status = READ_MSG
            elif status == READ_MSG:
                if c == '$':
                    status = WAIT_MSG
                else:
                    wbuf.append(chr(ord(c) + 1))

        if wbuf:
            writer.write(''.join(wbuf).encode())
            await writer.drain()
    writer.close()

async def launch():
    server = await asyncio.start_server(on_serve, '0.0.0.0', 5555)
    loop, addr = asyncio.get_event_loop(), server.sockets[0].getsockname()
    print(f"Serving on {addr} with {loop} ...")

    await server.serve_forever()

if __name__ == '__main__':
    try:
        asyncio.run(launch())
    except KeyboardInterrupt:
        print("Serve finished")
    except Exception as e:
        print(e)


import time, os
from threading import Thread, current_thread
from multiprocessing import Process, current_process, Pool
import sys

COUNT = 200000000
SLEEP = 10

def io_bound(sec):

    pid = os.getpid()
    threadName = current_thread().name
    processName = current_process().name

    #print(f"{pid} * {processName} * {threadName} ---> Start sleeping...")
    print(pid," * ",processName," * ",threadName," ---> Start sleeping...")
    time.sleep(sec)
    print(pid," * ",processName," * ",threadName," ---> Finish sleeping...")
    #print(f"{pid} * {processName} * {threadName} ---> Finished sleeping...")

def cpu_bound(n):

    pid = os.getpid()
    threadName = current_thread().name
    processName = current_process().name

    print(f"{pid} * {processName} * {threadName} ---> Start counting...")

    while n>0:
        n -= 1

    print(f"{pid} * {processName} * {threadName} ---> Finished counting...")

if __name__=="__main__":
    start = time.time()

    if len(sys.argv)!=2:
        print("USO: python lab3.py DEBUG")
        sys.exit(1)

    DEBUG=int(sys.argv[1])

    if DEBUG==1:
        io_bound(SLEEP)
        io_bound(SLEEP)
    elif DEBUG==2:
        t1 = Thread(target = io_bound, args =(SLEEP, ))
        t2 = Thread(target = io_bound, args =(SLEEP, ))
        t1.start()
        t2.start()
        t1.join()
        t2.join()
    elif DEBUG==3:
        cpu_bound(COUNT)
        cpu_bound(COUNT)
    elif DEBUG==4:
        t1 = Thread(target = cpu_bound, args =(COUNT, ))
        t2 = Thread(target = cpu_bound, args =(COUNT, ))
        t1.start()
        t2.start()
        t1.join()
        t2.join()
    elif DEBUG==5:
        p1 = Process(target = cpu_bound, args =(COUNT, ))
        p2 = Process(target = cpu_bound, args =(COUNT, ))
        p1.start()
        p2.start()
        p1.join()
        p2.join()
    elif DEBUG==6:
        pool = Pool(processes=2)
        pool.apply_async(cpu_bound, [COUNT])
        pool.apply_async(cpu_bound, [COUNT])
        pool.close()
        pool.join()
    elif DEBUG==7:
        p1 = Process(target = io_bound, args =(SLEEP, ))
        p2 = Process(target = io_bound, args =(SLEEP, ))
        p1.start()
        p2.start()
        p1.join()
        p2.join()

    end = time.time()
    print('Time taken in seconds -', end - start)


__all__ = ('ROSConsole', )

import os
import re
import shlex
import subprocess
import sys
from functools import partial

#from designer.utils.utils import get_fs_encoding
from kivy.app import runTouchApp
from kivy.clock import Clock
from kivy.compat import PY2
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.logger import Logger
from kivy.properties import (
    BooleanProperty,
    DictProperty,
    ListProperty,
    NumericProperty,
    ObjectProperty,
)
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.utils import platform
from pygments.lexers.shell import BashSessionLexer
import paramiko
from ROSClientSSH import ROSClientSSH
from ROSClientSSH1 import ROSClientSSH1

try:
    import thread
except ImportError:
    import _thread as thread

Builder.load_string('''
<ROSConsole>:
    cols:1
    txtinput_history_box: history_box.__self__
    txtinput_command_line: command_line.__self__
    ScrollView:
        bar_width: 10
        scroll_type: ['bars', 'content']
        CodeInput:
            id: history_box
            size_hint: (1, None)
            height: 801
            font_size: root.font_size_history
            readonly: False
            foreground_color: root.foreground_color
            background_color: root.background_color
    TextInput:
        id: command_line
        multiline: False
        size_hint: (1, None)
        font_size: root.font_size
        readonly: root.readonly
        foreground_color: root.foreground_color
        background_color: root.background_color_input
        height: 36
        on_text_validate: root.on_enter(*args)
        on_touch_up:
            self.collide_point(*args[1].pos)\\
            and root._move_cursor_to_end(self)
''')

class ROSConsole(GridLayout):
    '''This is a Console widget used for debugging and running external
    commands
    '''

    readonly = BooleanProperty(False)
    foreground_color = ListProperty((1, 1, 1, 1))
    background_color = ListProperty((0, 0, 0, 1))
    background_color_input = ListProperty((0.1, .1, .1, 1))

    cached_history = NumericProperty(200)
    cached_commands = NumericProperty(90)
    environment = DictProperty(os.environ.copy())
    font_size = NumericProperty(14)
    font_size_history = NumericProperty(14)
    textcache = ListProperty(['', ])

    txtinput_command_line = ObjectProperty(None)

    def __init__(self, **kwargs):
        self.register_event_type('on_subprocess_done')
        self.register_event_type('on_command_list_done')
        super(ROSConsole, self).__init__(**kwargs)
        # initialisations
        self.txtinput_command_line_refocus = False
        self.txtinput_run_command_refocus = False
        self.win = None
        self.scheduled = False
        self.command_history = []
        self.command_history_pos = 0
        self.command_status = 'closed'
        if sys.version_info >= (3, 0):
            self.cur_dir = os.getcwd()
        else:
            self.cur_dir = os.getcwdu()
        self.command_list = []  # list of cmds to be executed
        #self.stdout = std_in_out(self, 'stdout')
        #self.stdin = std_in_out(self, 'stdin')
        self.popen_obj = None
        # self.stderror = stderror(self)
        # delayed initialisation
        Clock.schedule_once(self._initialize)
        _trig = Clock.create_trigger(self._change_txtcache)
        Clock.schedule_once(_trig)
        self.bind(textcache=_trig)

        self.rosSSH = ROSClientSSH1("berry")

        self._hostname = 'unknown'
        try:
            if hasattr(os, 'uname'):
                self._hostname = os.uname()[1]
            else:
                self._hostname = os.environ.get('COMPUTERNAME', 'unknown')
        except Exception:
            pass
        self._username = os.environ.get('USER', '')
        if not self._username:
            self._username = os.environ.get('USERNAME', 'unknown')

    def clear(self, *args):
        '''Clear the Kivy Console area
        '''
        self.txtinput_history_box.text = ''
        self.textcache = ['']

    def _initialize(self, dt):
        '''Set console default variable values
        '''

        self.txtinput_history_box.lexer = BashSessionLexer()
        self.txtinput_history_box.text = ''.join(self.textcache)

        self.rosSSH.open(self.add_to_cache)

        self.txtinput_command_line.text = self.rosSSH.prompt()
        self.txtinput_command_line.bind(focus=self.on_focus)
        self.txtinput_command_line.bind(selection_text=self.on_txtinput_selection)
        self._focus(self.txtinput_command_line)

    def on_txtinput_selection(self, *args):
        '''Callback to command input text selection.
        Cannot select the PS1 variable, so it'll handle to select only
        input text.
        '''
        ticl = self.txtinput_command_line
        col = len(self.rosSSH.prompt())
        ticl.select_text(col, len(ticl.text))

    def _move_cursor_to_end(self, instance):
        '''Moves the command input cursor to the end
        '''
        def mte(*l):
            instance.cursor = instance.get_cursor_from_index(len_prompt)
        len_prompt = len(self.rosSSH.prompt())
        if instance.cursor[0] < len_prompt:
            Clock.schedule_once(mte, -1)

    def _focus(self, widg, t_f=True):
        Clock.schedule_once(partial(self._deffered_focus, widg, t_f))

    def _deffered_focus(self, widg, t_f, dt):
        if widg.get_root_window():
            widg.focus = t_f

    def _change_txtcache(self, *args):
        '''Update the Kivy Console output area
        '''
        try:
            # if passing the text cache limit, omit it
            self.textcache = self.textcache[-self.cached_history:]
        except IndexError:
            pass
        for i in range(len(self.textcache)):
            pass
            #if PY2 and isinstance(self.textcache[i], unicode):
            #    self.textcache[i] = self.textcache[i].encode('utf-8')
        tihb = self.txtinput_history_box
        tihb.text = ''.join(self.textcache)
        if not self.get_root_window():
            return
        tihb.height = max(tihb.minimum_height, tihb.parent.height)
        tihb.parent.scroll_y = 0

    def on_key_down(self, *l):
        '''Handle the on_key_down from keyboard
        '''
        ticl = self.txtinput_command_line

        print (l)

        def move_cursor_to(col):
            '''Update the cursor position
            '''
            ticl.cursor =\
                col, ticl.cursor[1]

        def search_history(up_dn):
            if up_dn == 'up':
                plus_minus = -1
            else:
                plus_minus = 1
            l_curdir = len(self.rosSSH.prompt())
            col = ticl.cursor_col
            command = ticl.text[l_curdir: col]
            max_len = len(self.command_history) - 1
            chp = self.command_history_pos

            while max_len >= 0:
                if plus_minus == 1:
                    if self.command_history_pos > max_len - 1:
                        self.command_history_pos = max_len
                        return
                else:
                    if self.command_history_pos <= 0:
                        self.command_history_pos = max_len
                        return
                self.command_history_pos = self.command_history_pos\
                    + plus_minus
                cmd = self.command_history[self.command_history_pos]
                if cmd[:len(command)] == command:
                    ticl.text = ''.join((
                        self.rosSSH.prompt(), cmd))
                    move_cursor_to(col)
                    return
            self.command_history_pos = max_len + 1

        if ticl.focus:
            if len(l[4]) > 0 and l[4][0] == 'ctrl' and l[1] != 305:
                if (l[1]):
                    #ticl.text="^"+l[3]
                    self.rosSSH.sendSpecial(l[3])
                    #self.rosSSH.run(l[1], self.add_to_cache)

                return
            # if l[1] == 99 : #CTRL-C
            #     if len(l[4]) > 0 and l[4][0] == 'ctrl':
            #         print ("CC")
            #
            # if l[1] == 118:  # CTRL-V
            #     if len(l[4]) > 0 and l[4][0] == 'ctrl':
            #         print("CV")

            if l[1] == 273:
                # up arrow: display previous command
                if self.command_history_pos > 0:
                    self.command_history_pos -= 1
                    ticl.text = ''.join(
                        (self.rosSSH.prompt(),
                         self.command_history[self.command_history_pos]))
                return
            if l[1] == 274:
                # dn arrow: display next command
                if self.command_history_pos < len(self.command_history) - 1:
                    self.command_history_pos += 1
                    ticl.text = ''.join(
                        (self.rosSSH.prompt(),
                         self.command_history[self.command_history_pos]))
                else:
                    self.command_history_pos = len(self.command_history)
                    ticl.text = self.rosSSH.prompt()
                col = len(ticl.text)
                move_cursor_to(col)
                return
            if l[1] == 9:
                # tab: autocomplete
                def display_dir(cur_dir, starts_with=None):
                    # display contents of dir from cur_dir variable
                    try:
                        dir_list = os.listdir(cur_dir)
                    except OSError as err:
                        self.add_to_cache(''.join((err.strerror, '\n')))
                        return
                    if starts_with is not None:
                        len_starts_with = len(starts_with)
                    self.add_to_cache(
                            'contents of directory: ' + cur_dir + '\n')
                    txt = ''
                    no_of_matches = 0
                    for _file in dir_list:
                        if starts_with is not None:
                            if _file[:len_starts_with] == starts_with:
                                # if file matches starts with
                                txt = ''.join((txt, _file, '\t'))
                                no_of_matches += 1
                        else:
                            self.add_to_cache(''.join((_file, '\t')))
                    if no_of_matches == 1:
                        len_txt = len(txt) - 1
                        cmdl_text = ticl.text
                        len_cmdl = len(cmdl_text)
                        os_sep = os.sep \
                            if col == len_cmdl or (col < len_cmdl and
                                                   cmdl_text[col] !=
                                                   os.sep) else ''
                        ticl.text = ''.join(
                            (self.rosSSH.prompt(), text_before_cursor,
                             txt[len_starts_with:len_txt], os_sep,
                             cmdl_text[col:]))
                        move_cursor_to(col + (len_txt - len_starts_with) + 1)
                    elif no_of_matches > 1:
                        self.add_to_cache(txt)
                    self.add_to_cache('\n')

                # send back space to command line -remove the tab
                Clock.schedule_once(ticl.do_backspace, 0)
                l_curdir = len(self.rosSSH.prompt())
                move_cursor_to(l_curdir + len(ticl.text))
                ntext = os.path.expandvars(ticl.text)
                # store text before cursor for comparison
                col = ticl.cursor_col
                if ntext != ticl.text:
                    ticl.text = ntext
                    col = len(ntext)
                text_before_cursor = ticl.text[l_curdir: col]

                # if empty or space before: list cur dir
                if text_before_cursor == ''\
                   or ticl.text[col - 1] == ' ':
                    display_dir(self.cur_dir)
                # if in mid command:
                else:
                    # list commands in PATH starting with text before cursor
                    # split command into path till the seperator
                    cmd_start = text_before_cursor.rfind(' ')
                    cmd_start += 1
                    cur_dir = self.cur_dir\
                        if text_before_cursor[cmd_start] != os.sep\
                        else os.sep
                    os_sep = os.sep if cur_dir != os.sep else ''
                    cmd_end = text_before_cursor.rfind(os.sep)
                    len_txt_bef_cur = len(text_before_cursor) - 1
                    if cmd_end == len_txt_bef_cur:
                        # display files in path
                        if text_before_cursor[cmd_start] == os.sep:
                            cmd_start += 1
                        display_dir(''.join((cur_dir, os_sep,
                                    text_before_cursor[cmd_start:cmd_end])))
                    elif text_before_cursor[len_txt_bef_cur] == '.':
                        # if / already there return
                        if len(ticl.text) > col\
                           and ticl.text[col] == os.sep:
                            return
                        if text_before_cursor[len_txt_bef_cur - 1] == '.':
                            len_txt_bef_cur -= 1
                        if text_before_cursor[len_txt_bef_cur - 1]\
                           not in (' ', os.sep):
                            return
                        # insert at cursor os.sep: / or \
                        ticl.text = ''.join(
                                (self.rosSSH.prompt(),
                                 text_before_cursor, os_sep,
                                 ticl.text[col:]))
                    else:
                        if cmd_end < 0:
                            cmd_end = cmd_start
                        else:
                            cmd_end += 1
                        display_dir(''.join((
                                    cur_dir,
                                    os_sep,
                                    text_before_cursor[cmd_start:cmd_end])),
                                    text_before_cursor[cmd_end:])
                return
            if l[1] == 280:
                # pgup: search last command starting with...
                search_history('up')
                return
            if l[1] == 281:
                # pgdn: search next command starting with...
                search_history('dn')
                return
            if l[1] == 278:
                # Home: cursor should not go to the left of cur_dir
                col = len(self.rosSSH.prompt())
                move_cursor_to(col)
                if len(l[4]) > 0 and l[4][0] == 'shift':
                    ticl.selection_to = col
                return
            if l[1] == 276 or l[1] == 8:
                # left arrow/bkspc: cursor should not go left of cur_dir
                col = len(self.rosSSH.prompt())
                if ticl.cursor_col < col:
                    if l[1] == 8:
                        ticl.text = self.rosSSH.prompt()
                    move_cursor_to(col)
                return

    def on_focus(self, instance, value):
        '''Handle the focus on the command input
        '''
        if value:
            # focused
            if instance is self.txtinput_command_line:
                Window.unbind(on_key_down=self.on_key_down)
                Window.bind(on_key_down=self.on_key_down)
        else:
            # defocused
            Window.unbind(on_key_down=self.on_key_down)
            if self.txtinput_command_line_refocus:
                self.txtinput_command_line_refocus = False
                if self.txtinput_command_line.get_root_window():
                    self.txtinput_command_line.focus = True
                self.txtinput_command_line.scroll_x = 0
            if self.txtinput_run_command_refocus:
                self.txtinput_run_command_refocus = False
                instance.focus = True
                instance.scroll_x = 0
                instance.text = ''

    def add_to_cache(self, _string):
        if (_string!=None):
            self.textcache.append(_string)

    def kill_process(self, *args):
        '''Kill the current process
        '''
        if self.popen_obj:
            self.popen_obj.kill()

    def on_enter(self, *args):
        '''When the user press enter and wants to run a command
        '''
        self.unbind(on_subprocess_done=self.on_enter)
        # if there is a command running, it's necessary to stop it first
        if self.command_status == 'started':
            self.kill_process()
            # restart this call after killing the running process
            self.bind(on_subprocess_done=self.on_enter)
            return

        txtinput_command_line = self.txtinput_command_line
        add_to_cache = self.add_to_cache
        command_history = self.command_history

        def run_cmd(*args):
            '''Run the command
            '''
            if len(command) > 0:

                try:
                    # execute command
                    self.rosSSH.run(command)

                except (OSError, ValueError) as err:
                    add_to_cache(''.join(
                            (str(err),
                             ' < ', command, ' >\n')))
            self.command_status = 'closed'
            self.dispatch('on_subprocess_done')

            self.popen_obj = None
            txtinput_command_line.focus = True
            txtinput_command_line.text = self.rosSSH.prompt()
            #Clock.schedule_once(remove_command_interaction_widgets, 0)

        # append text to textcache
        #add_to_cache(self.txtinput_command_line.text + '\n')
        command = txtinput_command_line.text[len(self.rosSSH.prompt()):]

        if command == '':
            self.txtinput_command_line_refocus = True
            return

        # store command in command_history
        if self.command_history_pos > 0:
            self.command_history_pos = len(command_history)
            if command_history[self.command_history_pos - 1] != command:
                command_history.append(command)
        else:
            command_history.append(command)

        len_command_history = len(command_history)
        self.command_history_pos = len(command_history)

        # on reaching limit(cached_lines) pop first command
        if len_command_history >= self.cached_commands:
            self.command_history = command_history[1:]

        # replace $PATH with
        #command = os.path.expandvars(command)

        txtinput_command_line.text = self.rosSSH.prompt()

        # esecuzione in altro thread
        self.command_status = 'started'
        thread.start_new_thread(run_cmd, ())

    def on_subprocess_done(self, *args):
        '''Event handler for when a process was killed
        or just finished the execution.
        '''
        pass

    def on_command_list_done(self, *args):
        '''Event handler for when the whole command list was executed or killed
        '''
        pass

if __name__ == '__main__':
    runTouchApp(ROSConsole())


from flask import abort
from flask import render_template
from flask import request
from flask import url_for
from flask.ext.wtf import Form
from wtforms import validators
from wtforms import IntegerField, RadioField, SelectField, TextField

from logcas.bootstrap import *

# forms

class LogShowForm(Form):
    style = SelectField('Style', default=DEFAULT_STYLE, choices=STYLEMAP)

# controllers

@app.route('/logs/<ObjectId:log_id>')
def _log_show(log_id):
    forms = LogShowForm(request.args)
    if not forms.validate():
        abort(400)
    style = forms.style.data

    spec = {'_id': log_id}
    log = mongo.db.logs.find_one_or_404(spec)
    log.pop('_id')
    log_yaml = yaml.dump(log, width=200, default_flow_style=False)
    return render_template('log_show.html', **locals())

@app.route('/archived/logs/<ObjectId:log_id>')
def _archived_log_show(log_id):
    forms = LogShowForm(request.args)
    if not forms.validate():
        abort(400)
    style = forms.style.data

    spec = {'_id': log_id}
    log = mongo.db.archived_logs.find_one_or_404(spec)
    log.pop('_id')
    log_yaml = yaml.dump(log, width=200, default_flow_style=False)
    return render_template('archived_log_show.html', **locals())


from django.shortcuts import render

from .models import SearchQuery
from posts.models import Posts

def search_view(request):
    query = request.GET.get('q' , None)
    user = None
    if request.user.is_authenticated:
        user = request.user
    context = { 'query' : query }
    if query is not None:
        SearchQuery.objects.create(user=user , query = query)
        posts_list = Posts.objects.search(query)
        context ['posts_list'] = posts_list
    return render(request,'searches/search_view.html', context)


# -*- coding: utf-8 -*-
"""
Created on Thu Jun 11 16:50:48 2020

@author: pedro
"""

from Bio import AlignIO
from Bio import SeqIO
"""
with open("clustalw.aln", "r") as handle:
    for record in SeqIO.parse(handle, "clustal") :
        print(record)

filename = "out_filename.clustal_num"
format = "clustal"
alignment = AlignIO.read(filename, format)
# print("Alignment of length %i" % alignment.get_alignment_length())
print(dir(alignment))
print(alignment)

"""

from collections import Counter

def seqs_peptides_conserv(data, n_seq, window_size):

    AALetter = ["A", "R", "N", "D", "C", "E", "Q", "G", "H", "I", "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V", '-']

    protein_gaps = ''
    conservation_gaps = ''

    #alter this to function to every sequence of the alignment
    for i in range(len(data)-n_seq):
        if data[i+n_seq][0] not in AALetter:
            protein_gaps += data[i]
            conservation_gaps += data[i+n_seq]

    peptides = {}
    pos = 0
    pos_gap = []

    for i in range(len(protein_gaps)):
        if protein_gaps[i] == '-': pos_gap.append(i)

    protein = ''
    conservation = ''

    for i in range(len(protein_gaps)):
        if i not in pos_gap:
            protein += protein_gaps[i]
            conservation += conservation_gaps[i]

    max_conserv = 0
    for i in range(len(protein)-window_size):
        peptide_aux = protein[i:i+window_size]
        conser_aux = conservation[i:i+window_size]

        conver_value = 1
        for j in conser_aux:
            if j == '*': conver_value *= 1
            elif j == ':': conver_value *= 0.5
            elif j == '.': conver_value *= 0.25
            elif j == ' ': conver_value *= 0.05

        peptides[str(pos)+'-'+str(pos+window_size)] = [peptide_aux,conser_aux,conver_value]
        pos += 1

        if max_conserv<conver_value:
            max_conserv = conver_value

    for i in peptides.keys():
        peptides[i][2] = (peptides[i][2] / max_conserv) * window_size

    return peptides

def conservation_sequence(filename="out_filename.clustal_num", window_size = 15):
    """
    Parameters
    ----------
    filename : FILE, optional
        DESCRIPTION. The default is "out_filename.clustal_num".
    window_size : INTEGER, optional
        DESCRIPTION. The default is 15.

    Returns a dict containing all the peptides and their conservative legend.
    -------
    None.

    """

    f = open(filename, "r")

    filename = "out_filename.clustal_num"
    format = "clustal"
    alignment = AlignIO.read(filename, format)

    ids_seqs = []

    for i in alignment._records:
        ids_seqs.append(i.id)

    AALetter = ["A", "R", "N", "D", "C", "E", "Q", "G", "H", "I", "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V", '-']

    clustal_content = f.readlines()

    data_raw = []

    for i in clustal_content[1:len(clustal_content)]:# it removes the first line, that has Clustal version
        if sum(Counter(list(i)).values()) > 1:
            data_raw.append(i) #clears all the blank lines

    not_seq = True
    count = 0
    while not_seq: #finds the lenght of the string until the start of the sequence
        if data_raw[0][count] not in AALetter:
            count += 1
        else:
            not_seq = False

    data = []

    for i in data_raw: data.append(i[count:len(i)-1]) #only keeps the sequence / conservation legend

    legend = 0
    legend_found = False
    seqs = {}

    while not legend_found:
        if data[legend][0] not in AALetter:
            legend_found = True
        else: legend += 1

    for i in range(0, legend):
        seqs[ids_seqs[legend-i-1]] = seqs_peptides_conserv(data, i+1, window_size)

if __name__ == '__main__':
    conservation_sequence()