# ----------------------------------------------------------------------------
# Copyright (c) 2015--, micronota development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
# ----------------------------------------------------------------------------

from os.path import join, dirname, exists
from os import remove
from tempfile import mkdtemp
from unittest import main
from shutil import rmtree

from micronota.util import _DBTest, _get_named_data_path
from micronota.db._uniref import create_metadata, sort_uniref
from micronota.db.uniref100 import prepare_db

class UnirefTests(_DBTest):
    def setUp(self):
        self.tmp_dir = mkdtemp()

        self.db_fp = 'uniprotkb.db'
        self.obs_db_fp = join(self.tmp_dir, self.db_fp)
        self.exp_db_fp = _get_named_data_path(self.db_fp)
        self.uniprotkb = [_get_named_data_path('uniprot_sprot.xml.gz'),
                          _get_named_data_path('uniprot_trembl.xml.gz'),
                          12]
        self.d = dirname(self.exp_db_fp)

        self.uniref_fp = _get_named_data_path('uniref100.fasta.gz')
        self.uniref_res = [
            'Swiss-Prot_Archaea',
            'Swiss-Prot_Bacteria',
            'Swiss-Prot_Viruses',
            'Swiss-Prot_Eukaryota',
            'Swiss-Prot_other',
            'TrEMBL_Archaea',
            'TrEMBL_Bacteria',
            'TrEMBL_Viruses',
            'TrEMBL_Eukaryota',
            'TrEMBL_other',
            '_other']

    def test_prepare_metadata(self):
        n = create_metadata(self.uniprotkb[:2], self.obs_db_fp)
        self.assertEqual(n, self.uniprotkb[2])
        self._test_eq_db(self.obs_db_fp, self.exp_db_fp)

    def _test_eq(self):
        for fp in self.uniref_res:
            for suffix in ['fasta', 'dmnd']:
                fp = '.'.join([fp, suffix])
                obs = join(self.tmp_dir, fp)
                exp = _get_named_data_path(fp)
                if exists(exp):
                    with open(obs) as o, open(exp) as e:
                        self.assertEqual(o.read(), e.read())
                    remove(obs)

    def test_sort_uniref(self):
        sort_uniref(self.exp_db_fp, self.uniref_fp,
                    join(self.tmp_dir, 'uniref100'), 100)
        self._test_eq()

    def test_prepare_db(self):
        prepare_db(self.d, self.tmp_dir)
        self._test_eq()
        self._test_eq_db(self.obs_db_fp, self.exp_db_fp)

    def test_prepare_db_not_overwrite(self):
        with self.assertRaisesRegex(
                FileExistsError, r'The file .* exists.'):
            prepare_db(self.d, self.d)

    def tearDown(self):
        rmtree(self.tmp_dir)

if __name__ == '__main__':
    main()


from PySide2.QtWidgets import *
from gui.loading import Loading
from gui.home import Home
from gui.tools import Screen
from gui.vision import Vision
from gui.controls import Controls

class Manager(QWidget):

    def __init__(self):
        super(Manager, self).__init__()

        # Initialise the base layout
        self._base = QHBoxLayout()

        # Initialise the list of screens
        self._screens = QStackedWidget()

        # Add the screens
        self._screens.addWidget(Loading())
        self._screens.addWidget(Home())
        self._screens.addWidget(Controls())
        self._screens.addWidget(Vision())

        # Populate the base layout with the screens widget
        self._base.addWidget(self._screens)

        # Set the base layout as the manager's layout
        self.setLayout(self._base)

        # Display the manager
        self.show()

    def switch(self, screen: Screen):
        """
        Function used to switch in between screens.

        :param screen: Screen to switch to
        """

        # Switch the screen
        self._screens.setCurrentIndex(screen.value)

        # Set the window title
        self.setWindowTitle(screen.name)

    def get_current_screen(self):
        """
        Function used to get a :class:`Screen` from the manager.

        :param screen: Enum of the screen to be retrieved
        :return: Retrieved screen
        """

        return self._screens.currentWidget()

    def register_streams(self, streams: list):

        # Remember the reference to the streams
        self.streams = streams


# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from gaebusiness.business import CommandExecutionException
from gaecookie.decorator import no_csrf
from tekton.gae.middleware.json_middleware import JsonResponse
from category_app import category_facade
from gaepermission.decorator import login_required
from category_app.category_model import Category

@login_required
@no_csrf
def index():
    cmd = category_facade.list_categorys_cmd()
    category_list = cmd()
    category_form = category_facade.category_form()
    category_dcts = [category_form.fill_with_model(m) for m in category_list]
    return JsonResponse(category_dcts, secure_prefix="")

@login_required
@no_csrf
def new(_resp, **category_properties):
    cmd = category_facade.save_category_cmd(**category_properties)
    return _save_or_update_json_response(cmd, _resp)

@login_required
@no_csrf
def edit(_resp, id, **category_properties):
    print(category_properties)
    category = Category.get_by_id(int(id))
    category.name = category_properties.get('name')
    category.slug = category_properties.get('slug')
    try:
        category.put()
    except:
        return JsonResponse({"errors": "Erro ocorrido"}, secure_prefix="")
    form = category_facade.category_form()
    _resp.status_code = 200
    return JsonResponse(form.fill_with_model(category), secure_prefix="")
    # cmd = category_facade.update_category_cmd(id, **category_properties)
    # return _save_or_update_json_response(cmd, _resp)

@login_required
@no_csrf
def delete(_resp, id):
    cmd = category_facade.delete_category_cmd(id)
    try:
        cmd()
    except CommandExecutionException:
        _resp.status_code = 500
        return JsonResponse(cmd.errors, secure_prefix="")
    return JsonResponse({"status": 1}, secure_prefix="")

def _save_or_update_json_response(cmd, _resp):
    try:
        category = cmd()
    except CommandExecutionException:
        _resp.status_code = 500
        return JsonResponse(cmd.errors)
    category_form = category_facade.category_form()
    return JsonResponse(category_form.fill_with_model(category), secure_prefix="")


import sys

sys.path.append('..')
from tqdm import tqdm
import os
import argparse

import glob
import wave
import contextlib
from process_data import pitch_process
import time
__author__ = 'YosiShrem'

VOICE_STARTS_FILENAME = "voice_starts.txt"
PRAAT_PATH = '/home/yosi/custom_commands/praat'

"""
for every wav dir, generate voice_starts.txt file which includes the offset for each file.
the offset is when the speach detector activates.
"""

def get_wav_duration(wav_path):
    with contextlib.closing(wave.open(wav_path, 'r')) as f:
        frames = f.getnframes()
        rate = f.getframerate()
        duration = frames / float(rate)
    return round(duration, 4) - 0.0001  # make sure to 'floor' last digit

def main(args):
    global VOICE_STARTS_FILENAME, PRAAT_PATH

    parser = argparse.ArgumentParser(description='Get timing form TextGrid')
    parser.add_argument('input_dir', type=str, help='Path to TextGrid dir')
    parser.add_argument('--output_dir', type=str, help='Path to output dir')
    parser.add_argument('--out_filename', default=VOICE_STARTS_FILENAME, type=str,
                        help='requested output path(default is VOICE_STARTS_FILENAME')
    parser.add_argument('--praat_path', default=PRAAT_PATH, type=str, help='path to praat executable')
    parser.add_argument('--skip_pi', action='store_true', help='skip pi extraction')

    args = parser.parse_args(args)
    args.input_dir = os.path.abspath(args.input_dir)
    args.output_dir = os.path.abspath(args.output_dir) if args.output_dir else args.input_dir

    PRAAT_PATH = args.praat_path
    # mis_aligned = [line.strip() for line in open(os.path.join(args.input_dir,'misaligned_files.txt'),'r').readlines()]
    #####check for wrong inputs#####
    assert os.path.isdir(args.input_dir), "Couldn't find input dir [{}]".format(args.input_dir)
    assert os.path.isdir(args.output_dir), "Couldn't find output dir [{}]".format(args.output_dir)
    assert os.path.exists(PRAAT_PATH), "Couldn't find praat path : {}".format(args.praat_path)
    ####################

    wav_files = [glob.glob(os.path.join(args.input_dir, '*{}'.format(ext))) for ext in ['.WAV', '.wav']]
    wav_files = [item for sublist in wav_files for item in sublist]  # flatten list

    offsets_list = []
    is_skip_PI = args.skip_pi
    if not is_skip_PI:
        pitch_process.run_PI_for_dir(args.input_dir, PRAAT_PATH)
        time.sleep(1)
        is_skip_PI=True
    for wav_file in tqdm(wav_files):
        # if not wav_file.__contains__("VI007-3_43.wav"):
        #     continue
        # if not os.path.basename(wav_file) in mis_aligned:
        #     continue
        # if len(offsets_list ) > 20:
        #     break
        try:
            wav_path = os.path.join(args.input_dir, wav_file)
            out_path = os.path.join(args.output_dir, wav_file)
            voice_start = pitch_process.get_voice_start(wav_path, out_path, PRAAT_PATH, skip_extract_PI=is_skip_PI)
            wav_duration = get_wav_duration(wav_path)
            offsets_list += [(wav_path, voice_start, wav_duration)]
        except Exception as e:
            print ("Failed to process file [{}]\n error:{}".format(wav_file,e))

    if args.out_filename == VOICE_STARTS_FILENAME:  # if default value, write file in input dir
        VOICE_STARTS_FILENAME = [os.path.join(args.input_dir, VOICE_STARTS_FILENAME)]
                                 # os.path.join(args.output_dir, VOICE_STARTS_FILENAME)]
    # else:
    #     VOICE_STARTS_FILENAME = [VOICE_STARTS_FILENAME]

    for voice_start_file in VOICE_STARTS_FILENAME:
        with open(voice_start_file, 'w') as f:
            for (file, voice_start, duration) in offsets_list:
                f.write("{} : {}  :{}\n".format(file, voice_start, duration))

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


t=eval(input())
for i in range(0,t):
    temp=input().split()
    for j in range(0,len(temp)):
        temp[j]=int(temp[j])
    if temp[1]<10:
        result=(temp[0]-1)*(10-temp[1])
        print(result)
    else:
        print(10*(temp[0]-1))


import json
from django.urls import reverse
from django.test import TestCase, override_settings
from django.utils.crypto import get_random_string
from zentral.contrib.inventory.models import EnrollmentSecret, MachineSnapshot, MetaBusinessUnit
from zentral.contrib.osquery.conf import (INVENTORY_QUERY_NAME,
                                          INVENTORY_DISTRIBUTED_QUERY_PREFIX)
from zentral.contrib.osquery.models import Configuration, Enrollment
from zentral.core.probes.conf import all_probes
from zentral.core.probes.models import ProbeSource
from zentral.utils.api_views import make_secret

INVENTORY_QUERY_SNAPSHOT = [
    {'build': '15D21',
     'major': '10',
     'minor': '11',
     'name': 'Mac OS X',
     'patch': '3',
     'table_name': 'os_version'},
    {'computer_name': 'godzilla',
     'cpu_brand': 'Intel(R) Core(TM)2 Duo CPU T9600 @2.80GHz',
     'cpu_logical_cores': '2',
     'cpu_physical_cores': '2',
     'cpu_subtype': 'Intel 80486',
     'cpu_type': 'i486',
     'hardware_model': 'MacBookPro5,1 ',  # extra space must be removed by osquery module
     'hardware_serial': '0123456789',
     'hostname': 'godzilla.box',
     'physical_memory': '8589934592',
     'table_name': 'system_info'},
    {'address': '192.168.1.123',
     'broadcast': '192.168.1.255',
     'interface': 'en1',
     'mac': '00:23:ac:a8:49:a9',
     'mask': '255.255.255.0',
     'table_name': 'network_interface'}
]

OSX_APP_INSTANCE = {
    "bundle_id": "com.agilebits.onepassword4-updater",
    "bundle_name": "1Password Updater",
    "bundle_path": "/Applications/1Password 6.app/Contents/Helpers/1Password Updater.app",
    "bundle_version": "652003",
    "bundle_version_str": "6.5.2",
    "table_name": "apps"
}

AZURE_AD_INFO_TUPLES = [
    {"common_name": "d14a06da-2547-4c80-9c5a-4851d1e4c7b2",
     "not_valid_before": "1556232938",
     "table_name": "azure_ad_certificate"},
    {"username": "jean",
     "key": "aadUniqueId",
     "value": "fc0e524e-9b87-4f63-a318-02727dc7983e",
     "table_name": "azure_ad_user_info"},
    {"username": "jean",
     "key": "aadUserId",
     "value": "jean@example.com",
     "table_name": "azure_ad_user_info"},
    {"username": "jean",
     "key": "version",
     "value": "1.1",
     "table_name": "azure_ad_user_info"},
    {"username": "jean",
     "key": "aadAuthorityUrl",
     "value": "https://login.microsoftonline.com/common",
     "table_name": "azure_ad_user_info"},
]

@override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage')
class OsqueryAPIViewsTestCase(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.configuration = Configuration.objects.create(name=get_random_string(256))
        cls.meta_business_unit = MetaBusinessUnit.objects.create(name=get_random_string(64))
        cls.enrollment_secret = EnrollmentSecret.objects.create(meta_business_unit=cls.meta_business_unit)
        cls.enrollment = Enrollment.objects.create(configuration=cls.configuration,
                                                   secret=cls.enrollment_secret)

    def post_as_json(self, url_name, data):
        return self.client.post(reverse("osquery:{}".format(url_name)),
                                json.dumps(data),
                                content_type="application/json")

    def test_enroll_405(self):
        response = self.client.get(reverse("osquery:enroll"))
        self.assertEqual(response.status_code, 405)
        self.assertCountEqual(["POST", "OPTIONS"], (m.strip() for m in response["Allow"].split(",")))

    def test_enroll_bad_json(self):
        response = self.client.post(reverse("osquery:enroll"))
        self.assertEqual(response.status_code, 400)
        response = self.client.post(reverse("osquery:enroll"),
                                    data="lkjadslkjdsalkdjas",
                                    content_type="application/json")
        self.assertEqual(response.status_code, 400)

    def test_enroll_missing_json_keys(self):
        response = self.post_as_json("enroll", {"no_enroll_secret_key": True})
        self.assertEqual(response.status_code, 400)

    def test_enroll_bad_secret(self):
        response = self.post_as_json(
            "enroll",
            {"enroll_secret": "INVALID ENROLL SECRET",
             "host_details": {"system_info": {"hardware_serial": get_random_string(32)}}}
        )
        self.assertContains(response, "Unknown enrolled machine", status_code=403)

    def test_enroll_enroll_secret_bad_module_old_way(self):
        # TODO: deprecate and remove
        secret = "{}$SERIAL${}".format(make_secret("zentral.inexisting.module"), get_random_string(32))
        response = self.post_as_json("enroll", {"enroll_secret": secret})
        self.assertContains(response, "Invalid module", status_code=403)

    def test_enroll_not_machine_serial_number(self):
        response = self.post_as_json("enroll", {"enroll_secret": self.enrollment.secret.secret})
        self.assertContains(response, "No serial number", status_code=403)

    def test_enroll_ok_old_way(self):
        # TODO: deprecate and remove
        machine_serial_number = get_random_string(32)
        machine_test_qs = MachineSnapshot.objects.filter(source__module="zentral.contrib.osquery",
                                                         serial_number=machine_serial_number)
        # no machine
        self.assertEqual(machine_test_qs.count(), 0)
        # enroll machine
        secret = "{}$SERIAL${}".format(make_secret("zentral.contrib.osquery"),
                                       machine_serial_number)
        response = self.post_as_json("enroll", {"enroll_secret": secret})
        json_response = response.json()
        self.assertCountEqual(["node_key"], json_response.keys())
        self.assertEqual(machine_test_qs.count(), 1)
        machine = machine_test_qs.all()[0]
        self.assertEqual(machine.reference, json_response["node_key"])

    def test_enroll_with_host_identifier_ok(self):
        machine_serial_number = get_random_string(32)
        machine_test_qs = MachineSnapshot.objects.filter(source__module="zentral.contrib.osquery",
                                                         serial_number=machine_serial_number)
        # no machine
        self.assertEqual(machine_test_qs.count(), 0)
        # enroll machine
        secret = "{}$SERIAL${}".format(make_secret("zentral.contrib.osquery"),
                                       machine_serial_number)
        response = self.post_as_json("enroll", {"enroll_secret": secret,
                                                "host_identifier": "godzilla"})
        json_response = response.json()
        self.assertCountEqual(["node_key"], json_response.keys())
        self.assertEqual(machine_test_qs.count(), 1)
        machine = machine_test_qs.all()[0]
        self.assertEqual(machine.reference, json_response["node_key"])
        self.assertEqual(machine.system_info.computer_name, "godzilla")

    def test_re_enroll(self):
        machine_serial_number = get_random_string(32)
        # enroll machine
        secret = "{}$SERIAL${}".format(make_secret("zentral.contrib.osquery"),
                                       machine_serial_number)
        response = self.post_as_json("enroll", {"enroll_secret": secret,
                                                "host_identifier": "godzilla"})
        json_response = response.json()
        node_key = json_response["node_key"]
        # re-enroll machine
        response = self.post_as_json("enroll", {"enroll_secret": secret,
                                                "host_identifier": "godzilla"})
        json_response = response.json()
        self.assertEqual(json_response["node_key"], node_key)

    def test_config_405(self):
        response = self.client.get(reverse("osquery:enroll"))
        self.assertEqual(response.status_code, 405)
        self.assertCountEqual(["POST", "OPTIONS"], (m.strip() for m in response["Allow"].split(",")))

    def test_config_missing_node_key(self):
        response = self.post_as_json("config", {"godzilla": "ffm"})
        self.assertContains(response, "Missing node_key", status_code=403)

    def test_config_wrong_node_key(self):
        response = self.post_as_json("config", {"node_key": "godzilla"})
        self.assertContains(response, "Wrong node_key", status_code=403)

    def enroll_machine(self):
        machine_serial_number = get_random_string(64)
        response = self.post_as_json(
            "enroll",
            {"enroll_secret": self.enrollment.secret.secret,
             "host_details": {"system_info": {"hardware_serial": machine_serial_number}}}
        )
        self.assertEqual(response.status_code, 200)
        return machine_serial_number, response.json()["node_key"]

    def test_config_ok(self):
        _, node_key = self.enroll_machine()
        response = self.post_as_json("config", {"node_key": node_key})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response["Content-Type"], "application/json")
        json_response = response.json()
        self.assertIn("schedule", json_response)
        schedule = json_response["schedule"]
        self.assertIn(INVENTORY_QUERY_NAME, schedule)

    def test_osx_app_instance_schedule(self):
        _, node_key = self.enroll_machine()
        self.post_default_inventory_query_snapshot(node_key)
        # machine platform = MACOS now
        response = self.post_as_json("config", {"node_key": node_key})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response["Content-Type"], "application/json")
        json_response = response.json()
        self.assertIn("schedule", json_response)
        schedule = json_response["schedule"]
        self.assertIn(INVENTORY_QUERY_NAME, schedule)
        self.assertIn(" 'apps' ", schedule[INVENTORY_QUERY_NAME]["query"])

    def test_distributed_read_405(self):
        response = self.client.get(reverse("osquery:distributed_read"))
        self.assertEqual(response.status_code, 405)
        self.assertCountEqual(["POST", "OPTIONS"], (m.strip() for m in response["Allow"].split(",")))

    def test_distributed_read_default_inventory_query(self):
        _, node_key = self.enroll_machine()
        response = self.post_as_json("distributed_read", {"node_key": node_key})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response["Content-Type"], "application/json")
        json_response = response.json()
        query_names = ["{}{}".format(INVENTORY_DISTRIBUTED_QUERY_PREFIX, t)
                       for t in ("os_version", "system_info", "uptime", "network_interface")]
        self.assertCountEqual(json_response["queries"], query_names)

    def post_default_inventory_query_snapshot(self, node_key, with_app=False, with_azure_ad=False):
        snapshot = list(INVENTORY_QUERY_SNAPSHOT)
        if with_app:
            snapshot.append(OSX_APP_INSTANCE)
        if with_azure_ad:
            snapshot.extend(AZURE_AD_INFO_TUPLES)
        self.post_as_json("distributed_write",
                          {"node_key": node_key,
                           "queries": {"{}{}".format(INVENTORY_DISTRIBUTED_QUERY_PREFIX, i["table_name"]): [i]
                                       for i in snapshot}
                           })

    def test_default_inventory_query_snapshot(self):
        machine_serial_number, node_key = self.enroll_machine()
        self.post_default_inventory_query_snapshot(node_key)
        ms = MachineSnapshot.objects.current().get(serial_number=machine_serial_number)
        self.assertEqual(ms.os_version.build, INVENTORY_QUERY_SNAPSHOT[0]["build"])
        self.assertEqual(ms.system_info.hardware_model, INVENTORY_QUERY_SNAPSHOT[1]["hardware_model"].strip())

    def test_distributed_read_one_query_plus_default_inventory_query(self):
        _, node_key = self.enroll_machine()
        # one distributed query probe
        dq = "select * from users;"
        probe_source = ProbeSource.objects.create(
            name="Shellac",
            status=ProbeSource.ACTIVE,
            model="OsqueryDistributedQueryProbe",
            body={"distributed_query": dq}
        )
        dq_name = "dq_{}".format(probe_source.pk)
        # simulate an all_probes sync
        all_probes.clear()
        # distributed read
        response = self.post_as_json("distributed_read", {"node_key": node_key})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response["Content-Type"], "application/json")
        json_response = response.json()
        query_names = ["{}{}".format(INVENTORY_DISTRIBUTED_QUERY_PREFIX, t)
                       for t in ("os_version", "system_info", "uptime", "network_interface")]
        query_names.append(dq_name)
        self.assertCountEqual(json_response["queries"], query_names)
        self.assertEqual(json_response["queries"][dq_name], dq)
        # post default inventory snapshot.
        self.post_default_inventory_query_snapshot(node_key)
        # 2nd distributed read still has the inventory query
        # but with the apps and azure ad info queries, now that we know
        # what kind of machine it is
        response = self.post_as_json("distributed_read", {"node_key": node_key})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response["Content-Type"], "application/json")
        json_response = response.json()
        query_names = ["{}{}".format(INVENTORY_DISTRIBUTED_QUERY_PREFIX, t)
                       for t in ("os_version", "system_info", "uptime", "network_interface",
                                 "apps", "company_portal", "certificates")]
        self.assertCountEqual(json_response["queries"], query_names)
        # post default inventory snapshot with one app, and the azure ad info
        self.post_default_inventory_query_snapshot(node_key, with_app=True, with_azure_ad=True)
        # 3rd distributed read empty (2 snapshots done and no other distributed queries available)
        response = self.post_as_json("distributed_read", {"node_key": node_key})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response["Content-Type"], "application/json")
        json_response = response.json()
        self.assertEqual(json_response, {"queries": {}})

    def test_distributed_write_405(self):
        response = self.client.get(reverse("osquery:distributed_write"))
        self.assertEqual(response.status_code, 405)
        self.assertCountEqual(["POST", "OPTIONS"], (m.strip() for m in response["Allow"].split(",")))

    def test_distributed_write(self):
        _, node_key = self.enroll_machine()
        # query
        probe_source = ProbeSource.objects.create(
            name="Shellac",
            status=ProbeSource.ACTIVE,
            model="OsqueryDistributedQueryProbe",
            body={"distributed_query": "select username from users;"}
        )
        response = self.post_as_json("distributed_write",
                                     {"node_key": node_key,
                                      "queries": {"dq_{}".format(probe_source.pk): [{"username": "godzilla"}]}})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), {})

    def test_log_405(self):
        response = self.client.get(reverse("osquery:log"))
        self.assertEqual(response.status_code, 405)
        self.assertCountEqual(["POST", "OPTIONS"], (m.strip() for m in response["Allow"].split(",")))

    def test_log_default_inventory_query(self):
        machine_serial_number, node_key = self.enroll_machine()
        snapshot = [
            {
                "build": "15G1108",
                "major": "10",
                "minor": "11",
                "name": "Mac OS X",
                "patch": "6",
                "table_name": "os_version"
            },
            {
                "computer_name": "godzilla",
                "cpu_brand": "Intel(R) Core(TM) i7-4578U CPU @ 3.00GHz",
                "cpu_logical_cores": "4",
                "cpu_physical_cores": "2",
                "cpu_subtype": "Intel x86-64h Haswell",
                "cpu_type": "x86_64h",
                "hardware_model": "MacBookPro11,1",
                "hardware_serial": machine_serial_number,
                "hostname": "godzilla",
                "physical_memory": "17179869184",
                "table_name": "system_info"
            },
            {
                "address": "192.168.1.17",
                "broadcast": "192.168.1.255",
                "interface": "en3",
                "mac": "38:c9:87:21:b1:32",
                "mask": "255.255.255.0",
                "table_name": "network_interface"
            },
            OSX_APP_INSTANCE,
        ]
        post_data = {
            "node_key": node_key,
            "log_type": "result",
            "data": [
                {"name": INVENTORY_QUERY_NAME,
                 "unixTime": '1480605737',
                 "snapshot": snapshot}
            ]
        }
        # no machine named godzilla
        self.assertEqual(MachineSnapshot.objects.filter(reference=node_key,
                                                        system_info__computer_name="godzilla").count(), 0)
        # post new snapshot
        response = self.post_as_json("log", post_data)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response["Content-Type"], "application/json")
        json_response = response.json()
        self.assertEqual(json_response, {})
        # new machine snapshot, one of them is godzilla
        self.assertEqual(MachineSnapshot.objects.filter(reference=node_key).count(), 2)
        self.assertEqual(MachineSnapshot.objects.filter(reference=node_key,
                                                        system_info__computer_name="godzilla").count(), 1)
        self.assertEqual(MachineSnapshot.objects.filter(
            reference=node_key,
            osx_app_instances__app__bundle_name="1Password Updater").count(), 1)

    def test_log_status(self):
        _, node_key = self.enroll_machine()
        post_data = {
            "node_key": node_key,
            "log_type": "status",
            "data": [
                {'filename': 'scheduler.cpp',
                 'line': '63',
                 'message': 'Executing scheduled query: macos-attacks-query-pack_604dc4d3: '
                            "select * from startup_items where path like '%iWorkServices%';",
                 'severity': '0',
                 'version': '2.1.2'}
            ]
        }
        response = self.post_as_json("log", post_data)
        self.assertEqual(response["Content-Type"], "application/json")
        json_response = response.json()
        self.assertEqual(json_response, {})

    def test_log_event_format_result(self):
        _, node_key = self.enroll_machine()
        post_data = {
            "node_key": node_key,
            "log_type": "result",
            "data": [
                {'name': 'godzilla_kommt-343hdwkl',
                 'action': 'added',
                 'hostIdentifier': 'godzilla.local',
                 'columns': {'name': 'Dropbox', 'pid': '1234', 'port': '17500'},
                 'unixTime': '1480605737'}
            ]
        }
        response = self.post_as_json("log", post_data)
        self.assertEqual(response["Content-Type"], "application/json")
        json_response = response.json()
        self.assertEqual(json_response, {})

    def test_log_snapshot_format_result(self):
        _, node_key = self.enroll_machine()
        post_data = {
            "node_key": node_key,
            "log_type": "result",
            "data": [
                {'name': 'godzilla_kommt-343hdwkl',
                 'action': 'snapshot',
                 'hostIdentifier': 'godzilla.local',
                 "snapshot": [
                     {
                         "parent": "0",
                         "path": "/sbin/launchd",
                         "pid": "1"
                     },
                     {
                         "parent": "1",
                         "path": "/usr/sbin/syslogd",
                         "pid": "51"
                     }
                 ],
                 'unixTime': '1480605737'}
            ]
        }
        response = self.post_as_json("log", post_data)
        self.assertEqual(response["Content-Type"], "application/json")
        json_response = response.json()
        self.assertEqual(json_response, {})


import sys
from gsh.plugin import BaseExecutionHook

def update_host_handler(hostname, stream, line, prepend_host=False):
        if stream not in ("stdout", "stderr"):
            return
        writer = getattr(sys, stream)
        line = line.decode('utf-8')
        if prepend_host:
            line = "%s: %s" % (hostname, line)
        try:
            writer.write(line)
            writer.flush()
        except IOError as err:
            if writer is not sys.stderr:
                output = "Failed to write to stream %s: (%s, %s)\n" % (stream, err.errno, err.strerror)
                if prepend_host:
                    output = "%s: %s" % (hostname, output)
                sys.stderr.write(output)

class PrinterHook(BaseExecutionHook):
    def update_host(self, hostname, stream, line):
        return update_host_handler(hostname, stream, line)

class MachinePrinterHook(BaseExecutionHook):
    def update_host(self, hostname, stream, line):
        return update_host_handler(hostname, stream, line, prepend_host=True)


class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        l = len(nums)
        answer = []
        if l < 1:
            return answer

        def x(pre, left):
            if len(left) == 1:
                answer.append(pre + left)
                return
            ns = len(left)
            for i in range(ns):
                x(pre + [left[i]], left[:i] + left[i + 1:])

        x([], nums)
        return answer


#!/usr/bin/python3

"""The basic idea is this: the bugs grow for a certain period of time, dt. After this time, if their optical density,
OD, (as read by a photodetector) is above a threshhold, OD_thr, and they have grown since the last time point, drug is
administered through a pump, P_drug. If OD is less than OD_thr, then nutrient solution is added through another pump,
P_nut.

This system will be controlled by a Raspberry Pi, using the SPI and GPIO ports. To activate the pumps, GPIO ports are
set to 1/GPIO.HIGH/True for a certain period of time, t_pump. Optical density data is read via an analogue to digital
converter attached to one of the SPI ports on the RPi.

Data will be saved on the RPi and stored in the cloud. Using the Slack API, we will be able to query the RPi to find
out how the experiment is progressing."""

# Needed for Running the Pumps
import time
from datetime import datetime
import csv
import threading
import os
from subprocess import call
from subprocess import Popen, PIPE
import RPi.GPIO as GPIO
import numpy as np
import configparser

import board
import busio
import adafruit_ads1x15.ads1015 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
import adafruit_mcp230xx
import digitalio

# Needed for Slack Integration
import re
import json
# import psutil
from slackclient import SlackClient

# Needed for Screenshots
#import gtk.gdk
from subprocess import call

#Graphs
import pandas as pd
import matplotlib.pyplot as plt

#Logging
import logging

class Morbidostat:
    def __init__(self, sysnum):
        self.sysnum = sysnum
        self.sysstr = 'EVE' + str(self.sysnum)

        self.config = configparser.ConfigParser()
        self.config.read('eve-conf.ini')

        # Define Experiment Variables
        self.time_between_pumps = self.config[self.sysstr].getfloat('time_between_pumps')
        self.OD_thr = self.config[self.sysstr].getfloat('OD_thr')
        self.OD_min = self.config[self.sysstr].getfloat('OD_min')
        self.time_between_ODs = self.config[self.sysstr].getfloat('time_between_ODs') # how often to gather OD data, in seconds
        self.time_between_graphs = self.config[self.sysstr].getfloat('time_between_graphs') # how often to graph, in minutes
        # OD_thr is the threshold above which to activate drug pump  [vish bench tests: empty: 3.5V, Clear Vial: 0.265V, Very Cloudy Vial: 2.15V]

        #time_between_writes = 1  # how often to write out OD data, in minutes
        #loops_between_writes = (time_between_writes*60)/time_between_ODs # time bewteen writes in loops
        self.time_between_saves = self.config[self.sysstr].getfloat('time_between_saves')

        # Set Up I2C to Read OD Data
        # Create the I2C bus

        self.P_drug_times = self.config[self.sysstr].getfloat('P_drug_times')
        self.P_nut_times = self.config[self.sysstr].getfloat('P_nut_times')
        self.P_waste_times = self.config[self.sysstr].getfloat('P_waste_times')

        self.running_data = []  # the list which will hold our 2-tuples of time and OD
        self.pump_data = []
        self.OD_tmplist = []
        self.pump_tmplist = []
        # self.currOD = np.zeros(num_cham)
        self.currOD = 0
        # averaged OD value
        # self.avOD = np.zeros(num_cham)
        self.avOD = 0
        self.OD_av_length = self.config[self.sysstr].getint('OD_av_length')
        # OD averaging buffer
        self.avOD_buffer = np.zeros(self.OD_av_length)#need to change for multiplexing

        self.elapsed_loop_time = 0
        self.loops = 0
        self.last_dilutionOD = 0
        self.nut = 0
        self.drug = 1
        self.waste = 2

        self.drug_mass = 0

        self.total_time = self.config[self.sysstr].getfloat('Exp_time_hours')*3600 #in seconds
        self.loops_between_ODs = 1
        self.loops_between_pumps = (self.time_between_pumps*60)/self.time_between_ODs # time between pumps in loops

        # num_cham = 1 # number of morbidostat vials being used

        self.i2c = busio.I2C(board.SCL, board.SDA)
        # # Create the ADC object using the I2C bus
        self.ads = ADS.ADS1015(self.i2c)
        # # Create single-ended input on channel 0
        # # photoreceptor_channel = 0
        self.photod = AnalogIn(self.ads, getattr(ADS,'P'+ str(self.config[self.sysstr].getint('Analogin'))))

        # Setup the GPIO Pins to Control the Pumps
        self.pipins = self.config[self.sysstr].getboolean('Pi_pins')
        self.P_drug_pins = self.config[self.sysstr].getint('P_drug_pins')
        self.P_nut_pins = self.config[self.sysstr].getint('P_nut_pins')
        self.P_waste_pins = self.config[self.sysstr].getint('P_waste_pins')
        self.P_LED_pins = self.config[self.sysstr].getint('P_LED_pins')
	# P_fan_pins = self.config[self.sysstr].getint('P_fan_pins')
        self.pin_list = [self.P_drug_pins, self.P_nut_pins, self.P_waste_pins, self.P_LED_pins]

        if self.pipins:
            GPIO.setmode(GPIO.BCM)
            for pin in self.pin_list:
                GPIO.setup(pin, GPIO.OUT)
        else:
            self.pins = [None]*(max(self.pin_list)+1)
            self.mcp = adafruit_mcp230xx.MCP23017(self.i2c, address=self.config[self.sysstr].getint('m_address'))

            for pin in self.pin_list:
                self.pins[pin] = self.mcp.get_pin(pin)
                self.pins[pin].direction = digitalio.Direction.OUTPUT
                self.pins[pin].value = False

        self.writer = 0
        self.init_time = datetime.now()

        self.slack_client = SlackClient(self.config['MAIN']['slack_key'])
        # self.chanid = self.config['MAIN']['slack_chanid']
        self.slack_usericon = self.config[self.sysstr]['slack_icon']
        self.chan = self.config['MAIN']['slack_channel']
        self.slack_client.api_call(
            "chat.postMessage",
            channel = self.chan,
            username=self.sysstr,
            icon_url = self.slack_usericon,
            text = self.init_time.strftime('Initialized at %H:%M:%S')
            )

    def start(self):
        self.start_time = datetime.now()
        os.makedirs("/mnt/morbidodata/" + self.sysstr + "/" + str(self.start_time))

        self.elogr = logging.getLogger('self.elogr')
        self.elogr.setLevel(logging.DEBUG)
        self.elogrfh = logging.FileHandler('/mnt/morbidodata/%s/%s/exceptions.txt' % (self.sysstr, self.start_time))
        self.elogrfh.setFormatter("%(asctime)s — %(name)s — %(levelname)s — %(message)s")
        self.elogr.addHandler(self.elogrfh)

        self.ilogr = logging.getLogger('self.ilogr')
        self.ilogr.setLevel(logging.INFO)
        self.ilogrfh = logging.FileHandler('/mnt/morbidodata/%s/%s/info.txt' % (self.sysstr, self.start_time))
        self.ilogrfh.setFormatter("%(asctime)s — %(name)s — %(levelname)s — %(message)s")
        self.ilogr.addHandler(self.ilogrfh)

        # self.outfile_OD = "/mnt/morbidodata/%s/%s/ODdata_%s.csv" % (self.start_time, self.sysstr, self.start_time)
        self.outfile_OD = "/mnt/morbidodata/%s/%s/ODdata_%s.csv" % (self.sysstr, self.start_time, self.start_time)
        file = open(self.outfile_OD, 'a')
        wr = csv.writer(file)
        # wr.writerow(['Current OD', 'Average OD','OD Timing'])
        wr.writerow(['current', 'average','time','hour'])
        file.close()

        # self.outfile_pump = "/mnt/morbidodata/%s/%s/pump_%s.csv" % (self.start_time, self.sysstr, self.start_time)
        self.outfile_pump = "/mnt/morbidodata/%s/%s/pump_%s.csv" % (self.sysstr, self.start_time, self.start_time)
        file = open(self.outfile_pump, 'a')
        wr = csv.writer(file)
        # wr.writerow(['Nutrient Pump', 'Drug Pump','Waste Pump','Pump Timing', 'Drug Mass'])
        wr.writerow(['media', 'drug','waste','pump_time','hour','drug_mass'])
        file.close()

        #TURN ON THE FAN HERE

        # print('Experiment begun at %02s:%02s:%02s' % (self.start_time.hour, self.start_time.minute, self.start_time.second))
        print(self.start_time.strftime(self.sysstr + ' started at %H:%M:%S on %a - %b %d, %Y'))
        self.ilogr.info(self.start_time.strftime(self.sysstr + ' started at %H:%M:%S on %a - %b %d, %Y'))
        threading.Thread(self.on_timer()).start()

        self.initalmessage = self.slack_client.api_call(
            "chat.postMessage",
            channel = self.chan,
            username=self.sysstr,
            icon_url = self.slack_usericon,
            text = self.start_time.strftime('Experiment started at %H:%M:%S on %a - %b %d, %Y')
            )

        self.recgra = self.slack_client.api_call(
            "chat.postMessage",
            channel = self.chan,
            username=self.sysstr,
            icon_url = self.slack_usericon,
            text = self.start_time.strftime('Most recent graphs')
            )

        # self.history = self.slack_client.api_call("channels.history", channel=self.chanid, count = 1)
        # self.threadts = self.history['messages'][0]['ts']
        self.chanid = self.initalmessage['channel']
        self.threadts = self.initalmessage['ts']
        self.recgrats = self.recgra['ts']
        self.firstrec = True

        if self.pipins:
            GPIO.output(self.P_LED_pins,1)
        else:
            self.pins[self.P_LED_pins].value = True

    def get_OD(self):
        # self.currOD = self.photod.voltage #np.asarray(self.value)#[0]
        if self.pipins:
            GPIO.output(self.P_LED_pins,1)
            time.sleep(0.1)
            self.currOD = self.photod.voltage #np.asarray(self.value)#[0]
            time.sleep(0.1)
            GPIO.output(self.P_LED_pins,0)
        else:
            self.pins[self.P_LED_pins].value = True
            time.sleep(0.1)
            self.currOD = self.photod.voltage #np.asarray(self.value)#[0]
            time.sleep(0.1)
            self.pins[self.P_LED_pins].value = False

        self.avOD_buffer = np.append(self.avOD_buffer, self.currOD)  #might need to transpose if more than one pd (for multiplexing)

        # then remove the first item in the array, i.e. the oldest
        self.avOD_buffer = np.delete(self.avOD_buffer, 0)
        # calculate average for each flask
        self.avOD = np.mean(self.avOD_buffer)

    def pump_on(self,pump):
        if self.pipins:
            GPIO.output(pump, 1)
        else:
            self.pins[pump].value = True
        print('Turning on pump',pump)

    def pump_off(self,pump):
        if self.pipins:
            GPIO.output(pump, 0)
        else:
            self.pins[pump].value = False
        print('Turning off pump',pump)

    def all_pump_off(self):
        if self.pipins:
            for i in pin_list:
                GPIO.output(i, 0)
        else:
            for i in pin_list:
                self.pins[i].value = False
        print('Turning off all pumps')

    def bufferdata(self):
        self.OD_tmplist.append([self.currOD, self.avOD, self.nows, (self.elapsed_time.seconds)/3600])
        self.pump_tmplist.append([self.nut,self.drug,self.waste,self.nows,(self.elapsed_time.seconds)/3600,self.drug_mass])
        self.nut = 0
        self.drug = 1
        self.waste = 2

    def savefunc(self):
        self.writer = 1
        self.bufferdata()

        with open(self.outfile_OD, 'a') as file:
            # OD_tmplist.append(self.now)
            wr = csv.writer(file)
            wr.writerows(self.OD_tmplist)
            file.close()

        with open(self.outfile_pump, 'a') as file:
            wr = csv.writer(file)
            wr.writerows(self.pump_tmplist)
            file.close()

        self.OD_tmplist = []
        self.pump_tmplist = []
        self.writer = 0

    def graphOD(self):
        print('[%s] Generating graph' % self.sysstr)

        self.slack_client.api_call(
            "chat.postMessage",
            channel = self.chan,
            username=self.sysstr,
            icon_url = self.slack_usericon,
            text = ('Elapsed Time: %s ; OD = %.3f' % (self.secondsToText(self.elapsed_time.seconds),self.currOD)),
            thread_ts = self.threadts
            )

        allODs = pd.read_csv(self.outfile_OD, index_col='hour')
        # allODs['hour'] = allODs['time'] - allODs['time'].iloc[0]
        # allODs['hour'] = allODs['hour'].divide(3600)
        # allODs.set_index('hour')
        # print(allODs)
        #fig = plt.figure(dpi=1000)
        plt.rcParams["figure.dpi"] = 200
        ODplt = (allODs[['average']]).plot()  #figsize=(10,10) in the plot
        # ODplt = (allODs[['current']]).plot()  #figsize=(10,10) in the plot
        ODfig = ODplt.get_figure()
        self.outfile_OD = "/mnt/morbidodata/%s/%s/ODdata_%s.csv" % (self.sysstr, self.start_time, self.start_time)
        ODfig.savefig("/mnt/morbidodata/%s/%s/ODplot_%s.png" % (self.sysstr, self.start_time, self.start_time))
        ODplt = None; ODfig = None; fig = None
        with open("/mnt/morbidodata/%s/%s/ODplot_%s.png" % (self.sysstr, self.start_time, self.start_time), "rb") as file_content:
            self.slack_client.api_call(
                "files.upload",
                channels = self.chan,
                thread_ts = self.threadts,
                title = "ODPlot",
                file = file_content
            )

        allpumps = pd.read_csv(self.outfile_pump, index_col='hour')   # cols: 'media', 'drug','waste','pump_time','hour','drug_mass'

        allconcs = allpumps[['drug_mass']]/12
        allconcs.rename(columns={'drug_mass':'drug_conc'}, inplace=True)
        # allODs['hour'] = allODs['time'] - allODs['time'].iloc[0]
        # allODs['hour'] = allODs['hour'].divide(3600)
        # allODs.set_index('hour')
        # print(allODs)
        #fig = plt.figure(dpi=1000)
        colors = getattr(getattr(pd.plotting, '_style'), '_get_standard_colors')(num_colors=2)
        plt.rcParams["figure.dpi"] = 200
        ODplt = (allODs[['average']]).plot(label='average', color=colors[0])  #figsize=(10,10) in the plot
        ODplt.set_ylabel(ylabel='Average OD')
        lines, labels = ODplt.get_legend_handles_labels()

        DM = ODplt.twinx()
        DM.spines['right'].set_position(('axes', 1.0))
        allconcs.plot(ax = DM, label='drug_mass',color=colors[1],legend=False)
        DM.set_ylabel(ylabel='Drug Concentration (ug/mL)')
        line, label = DM.get_legend_handles_labels()
        lines += line
        labels += label
        ODplt.legend(lines, labels, loc=2)
        # ODplt = (allODs[['current']]).plot()  #figsize=(10,10) in the plot
        ODfig = ODplt.get_figure()
        ODfig.savefig("/mnt/morbidodata/%s/%s/ODconc_%s.png" % (self.sysstr, self.start_time, self.start_time),bbox_inches='tight')
        ODplt.figure = None; ODplt = None; ODfig = None; fig = None; allconcs= None; colors = None; DM = None
        with open("/mnt/morbidodata/%s/%s/ODconc_%s.png" % (self.sysstr, self.start_time, self.start_time), "rb") as file_content:
            self.slack_client.api_call(
                "files.upload",
                channels = self.chan,
                thread_ts = self.threadts,
                title = "ODConc",
                file = file_content
            )

        pumpa = allpumps[['media','drug','waste']]
        colors = getattr(getattr(pd.plotting, '_style'), '_get_standard_colors')(num_colors=4)
        PUplt = (allODs[['average']]).plot(label='average', color=colors[0])
        PUplt.set_ylabel(ylabel='Average OD')
        lines, labels = PUplt.get_legend_handles_labels()

        DM = PUplt.twinx()
        DM.spines['right'].set_position(('axes', 1.0))
        pumpa.plot(ax = DM,color=colors[1:4],legend=False)
        DM.set_yticklabels([])

        line, label = DM.get_legend_handles_labels()
        lines += line
        labels += label
        PUplt.legend(lines, labels, loc=2)

        PUfig = PUplt.get_figure()
        PUfig.savefig("/mnt/morbidodata/%s/%s/PUplot_%s.png" % (self.sysstr, self.start_time, self.start_time))
        allODs = None; allpumps = None; PUplt.figure = None; PUplt = None; PUfig = None; fig = None; allconcs= None; colors = None; DM = None; pumpa = None
        with open("/mnt/morbidodata/%s/%s/PUplot_%s.png" % (self.sysstr, self.start_time, self.start_time), "rb") as file_content:
            self.slack_client.api_call(
                "files.upload",
                channels = self.chan,
                thread_ts = self.threadts,
                title = "PUPlot",
                file = file_content
            )

        if self.firstrec:
            self.recmes = self.slack_client.api_call(
                "chat.postMessage",
                channel = self.chan,
                username=self.sysstr,
                icon_url = self.slack_usericon,
                text = ('Elapsed Time: %s ; OD = %.3f' % (self.secondsToText(self.elapsed_time.seconds),self.currOD)),
                thread_ts = self.recgrats
                )
            with open("/mnt/morbidodata/%s/%s/ODplot_%s.png" % (self.sysstr, self.start_time, self.start_time), "rb") as file_content:
                self.recod = self.slack_client.api_call(
                    "files.upload",
                    channels = self.chan,
                    thread_ts = self.recgrats,
                    title = "ODPlot",
                    file = file_content
                )
            with open("/mnt/morbidodata/%s/%s/ODconc_%s.png" % (self.sysstr, self.start_time, self.start_time), "rb") as file_content:
                self.recodc = self.slack_client.api_call(
                    "files.upload",
                    channels = self.chan,
                    thread_ts = self.recgrats,
                    title = "ODConc",
                    file = file_content
                )
            with open("/mnt/morbidodata/%s/%s/PUplot_%s.png" % (self.sysstr, self.start_time, self.start_time), "rb") as file_content:
                self.recpu = self.slack_client.api_call(
                    "files.upload",
                    channels = self.chan,
                    thread_ts = self.recgrats,
                    title = "PUPlot",
                    file = file_content
                )
            # print(self.recod['file']['shares']['public'][self.chanid][0]['ts'])
            self.firstrec = False
        else:
            self.slack_client.api_call(
                "chat.delete",
                channel = self.chanid,
                ts = self.recmes['ts']
                )
            self.slack_client.api_call(
                "chat.delete",
                channel = self.chanid,
                ts = self.recod['file']['shares']['public'][self.chanid][0]['ts']
                )
            self.slack_client.api_call(
                "chat.delete",
                channel = self.chanid,
                ts = self.recodc['file']['shares']['public'][self.chanid][0]['ts']
                )
            self.slack_client.api_call(
                "chat.delete",
                channel = self.chanid,
                ts = self.recpu['file']['shares']['public'][self.chanid][0]['ts']
                )
            self.recmes = self.slack_client.api_call(
                "chat.postMessage",
                channel = self.chan,
                username=self.sysstr,
                icon_url = self.slack_usericon,
                text = ('Elapsed Time: %s ; OD = %.3f' % (self.secondsToText(self.elapsed_time.seconds),self.currOD)),
                thread_ts = self.recgrats
                )
            with open("/mnt/morbidodata/%s/%s/ODplot_%s.png" % (self.sysstr, self.start_time, self.start_time), "rb") as file_content:
                self.recod = self.slack_client.api_call(
                    "files.upload",
                    channels = self.chan,
                    thread_ts = self.recgrats,
                    title = "ODPlot",
                    file = file_content
                )
            with open("/mnt/morbidodata/%s/%s/ODconc_%s.png" % (self.sysstr, self.start_time, self.start_time), "rb") as file_content:
                self.recodc = self.slack_client.api_call(
                    "files.upload",
                    channels = self.chan,
                    thread_ts = self.recgrats,
                    title = "ODConc",
                    file = file_content
                )
            with open("/mnt/morbidodata/%s/%s/PUplot_%s.png" % (self.sysstr, self.start_time, self.start_time), "rb") as file_content:
                self.recpu = self.slack_client.api_call(
                    "files.upload",
                    channels = self.chan,
                    thread_ts = self.recgrats,
                    title = "PUPlot",
                    file = file_content
                )

    def control_alg(self):
        # for i in range(num_cham):

        if self.avOD > self.OD_min:
            threading.Thread(target=self.pump_on, args=(self.P_waste_pins,)).start()
            threading.Timer(self.P_waste_times,self.pump_off, args=(self.P_waste_pins,)).start()

            self.waste = 3

            self.drug_mass = self.drug_mass - (self.drug_mass/12)

            if self.avOD > self.OD_thr and self.avOD > self.last_dilutionOD:
                print('[%s] OD Threshold exceeded, pumping cefepime' % self.sysstr)

                threading.Thread(target=self.pump_on, args=(self.P_drug_pins,)).start()
                threading.Timer(self.P_drug_times,self.pump_off, args=(self.P_drug_pins,)).start()
                self.drug = 2

                self.drug_mass = self.drug_mass + 2.5

                self.slack_client.api_call(
                    "chat.postMessage",
                    channel = self.chan,
                    username=self.sysstr,
                    icon_url = self.slack_usericon,
                    thread_ts = self.threadts,
                    text = "OD = %0.3f, pumping cefepime. Cefepime concentration: %f ug/mL" % (self.avOD, (self.drug_mass)/12)
                    )

            else:
                print('[%s] OD below threshold, pumping nutrient' % self.sysstr)

                threading.Thread(target=self.pump_on, args=(self.P_nut_pins,)).start()
                threading.Timer(self.P_nut_times,self.pump_off, args=(self.P_nut_pins,)).start()
                self.nut = 1

                self.slack_client.api_call(
                    "chat.postMessage",
                    channel = self.chan,
                    username=self.sysstr,
                    icon_url = self.slack_usericon,
                    thread_ts = self.threadts,
                    text = "OD = %0.3f, pumping nutrient. Cefepime concentration: %f ug/mL" % (self.avOD, (self.drug_mass)/12)
                    )

        else: #report even when pumps aren't activated yet

            self.drug_mass = 0

            self.slack_client.api_call(
                    "chat.postMessage",
                    channel = self.chan,
                    username=self.sysstr,
                    icon_url = self.slack_usericon,
                    thread_ts = self.threadts,
                    text = "OD = %0.3f, OD below nutrient pump threshold." % (self.avOD)
                    )

        self.last_dilutionOD = self.avOD

    def secondsToText(self,secs):
        if secs:
            days = secs//86400
            hours = (secs - days*86400)//3600
            minutes = (secs - days*86400 - hours*3600)//60
            seconds = secs - days*86400 - hours*3600 - minutes*60
            result = ("{0} day{1}, ".format(days, "s" if days!=1 else "") if days else "") + \
            ("{0} hour{1}, ".format(hours, "s" if hours!=1 else "") if hours else "") + \
            ("{0} minute{1}, ".format(minutes, "s" if minutes!=1 else "") if minutes else "") + \
            ("{0} second{1}, ".format(seconds, "s" if seconds!=1 else "") if seconds else "")
            return result[:-2]
        else:
            return "0 seconds"

    def on_timer(self):
        # note the time the loop starts
        self.now = datetime.now()
        self.nows = time.time()
        self.beginning = time.time()

        if self.loops < self.total_time/self.time_between_ODs:
            threading.Timer(self.time_between_ODs,self.on_timer).start()
        else:
            self.now = datetime.now()
            self.nows = time.time()
            print('[%s] Experiment Complete at %02s:%02s:%02s ' % (self.sysstr, self.now.hour, self.now.minute, self.now.second))
            # GPIO.output(P_fan_pins,0)

            self.slack_client.api_call(
                "chat.postMessage",
                channel = self.chan,
                username=self.sysstr,
                icon_url = self.slack_usericon,
                thread_ts = self.threadts,
                text = "Experiment Complete at %02s:%02s:%02s " % (self.now.hour, self.now.minute, self.now.second)
                )

        self.loops += 1
        #print(self.loops)

        # read OD data to be used for both controlling and saving during this loop
        threading.Thread(target=self.get_OD()).start()

        self.elapsed_time = self.now - self.start_time

        #self.elapsed_time_h = datetime(1,1,1) + self.elapsed_time
        print ('[%s] Elapsed Time: %s ; OD = %.3f' % (self.sysstr, self.secondsToText(self.elapsed_time.seconds),self.currOD))

        # activate pumps if needed and it's time (threaded to preserve time b/w ODss if this takes > time_between_OD
        if self.loops % (self.loops_between_pumps ) == 0:
            threading.Thread(self.control_alg()).start()

        # Graph if it is time per the global var
        if (self.loops % int(self.time_between_graphs*60/self.time_between_ODs)) == 0:
            threading.Thread(self.graphOD()).start()

        # save the data to disk if it's time (threaded to preserve time b/w ODs if this takes > time_between_ODs)
        if (self.loops % int(self.time_between_saves*60/self.time_between_ODs)) == 0:
            print('[%s] Saving to disk' % self.sysstr)
            threading.Thread(self.savefunc()).start()
        else:
            print('[%s] Buffering Data' % self.sysstr)
            threading.Thread(self.bufferdata()).start()

        # note the time the functions end
        self.end = time.time()
        self.interval = self.beginning - self.end

        # wait some period of time so that the total is time_between_ODs
        if self.interval > self.time_between_ODs:
            print('[%s] warning: loop took longer than requested OD interval' % self.sysstr)
        time.sleep(self.time_between_ODs - self.interval)
        #self.elapsed_loop_time += time_between_ODs

# Morbidostat()


#!/usr/bin/env python

import ROOT as r
from array import array

useCompHits = 1 # 0 means use recHits

folder = ""
coverage = 0.99 #look for bounds that give us 99% of the all entries in the histogram
if(useCompHits):
    folder = "compHits"
else:
    folder= "recHits"

threeWideColor = 9
legacyColor = 46
ccColor = 1

chambers = []

legacyPos = [array('d'), array('d')]
threeWidePos = [array('d'), array('d')]
ccPos = [array('d'), array('d')]

legacySlope = [array('d'), array('d')]
threeWideSlope = [array('d'), array('d')]
ccSlope = [array('d'), array('d')]

with open("intervals_%s.txt"%folder) as f:
    for l,line in enumerate(f):
        if l == 0: continue
        data  = line.strip("\n").split()
        chambers.append(data[0])
        legacyPos[0].append(abs(float(data[1])))
        legacyPos[1].append(float(data[2]))
        threeWidePos[0].append(abs(float(data[3])))
        threeWidePos[1].append(float(data[4]))
        ccPos[0].append(abs(float(data[5])))
        ccPos[1].append(float(data[6]))

        legacySlope[0].append(abs(float(data[7])))
        legacySlope[1].append(float(data[8]))
        threeWideSlope[0].append(abs(float(data[9])))
        threeWideSlope[1].append(float(data[10]))
        ccSlope[0].append(abs(float(data[11])))
        ccSlope[1].append(float(data[12]))

nchambers = len(chambers)

x = array('d')
y = array('d')
ex = array('d')

for i in range(nchambers):
    x.append(i)
    y.append(0)
    ex.append(0.5)

legacy = [legacyPos, legacySlope]
threeWide = [threeWidePos, threeWideSlope]
cc = [ccPos, ccSlope]

for i in range(2):
    c = r.TCanvas()

    legGraph = r.TGraphAsymmErrors(nchambers,x,y,ex,ex,legacy[i][0], legacy[i][1])
    threeGraph = r.TGraphAsymmErrors(nchambers,x,y,ex,ex,threeWide[i][0], threeWide[i][1])
    ccGraph = r.TGraphAsymmErrors(nchambers,x,y,ex,ex,cc[i][0], cc[i][1])

    for j in range(0,nchambers):
        threeGraph.GetXaxis().SetBinLabel(threeGraph.GetXaxis().FindBin(x[j]),chambers[j])

    threeGraph.GetXaxis().LabelsOption("u")

    if i == 0:
        threeGraph.GetYaxis().SetTitle("90% Coverage Segment - Pattern Position [strips]")
    else:
        threeGraph.GetYaxis().SetTitle("90% Coverage Segment - Pattern Slope [strips/layer]")
    threeGraph.GetYaxis().CenterTitle()
    threeGraph.SetTitle("")

    threeGraph.SetFillColor(threeWideColor)
    threeGraph.Draw("a2")

    legGraph.SetFillColor(legacyColor)
    legGraph.Draw("2")

    ccGraph.SetFillColor(ccColor)
    ccGraph.Draw("2")

    leg = r.TLegend(0.75,0.77, 0.97, 0.95)
    leg.AddEntry(legGraph,"Legacy")
    leg.AddEntry(threeGraph,"Three-Wide")
    leg.AddEntry(ccGraph,"Comparator Code")
    leg.Draw()

    if i == 0:
        c.SaveAs("../img/%s/coverageIntervalPosition.pdf"%(folder))
    else:
        c.SaveAs("../img/%s/coverageIntervalSlope.pdf"%(folder))


from tqdm import tqdm
import glob
import jpegio as jpio
from PIL import Image
import numpy as np
from scipy.fftpack import dct
from numpy import r_

## Write dictionary
img_path = '/export/data/mdorkenw/data/alaska2/'
folders = ['Cover', 'JUNIWARD', 'JMiPOD', 'UERD']
save_path = img_path + 'DCT/'

def dct2(a):
    return dct(dct(a, axis=0, norm='ortho'), axis=1, norm='ortho')

# for tech in folders:
for folder in folders:
    images = glob.glob(img_path + folder + "/*.jpg")
    print(images)
    exit()

    for idx, img in tqdm(enumerate(images)):
        # jpegStruct = jpio.read(img)
        im = np.array(Image.open(img))
        # dct_coeff = np.stack(jpegStruct.coef_arrays, axis=2)
        # breakpoint()

        imsize = im.shape
        dct_out = np.zeros(imsize)

        # Do 8x8 DCT on image (in-place)
        for i in r_[:imsize[0]:8]:
            for j in r_[:imsize[1]:8]:
                dct_out[i:(i + 8), j:(j + 8)] = dct2(im[i:(i + 8), j:(j + 8)])

        np.save(save_path + folder + '/' + str(idx).zfill(4) + '_block.npy', dct_out)


def factorial(n):
	result = 1
	for i in range(1, n+1):
		result *= i
	return result

def nthPermutation(symbolSet, n):
	symbolSet = list(map(str, symbolSet))
	if len(symbolSet) == 0:
		return ""
	result = ""
	sectorSize = factorial(len(symbolSet)-1)
	sector = int(n/sectorSize)
	remainder = n % sectorSize
	result += str(symbolSet[sector])
	del symbolSet[sector]
	result += nthPermutation(symbolSet, remainder)
	return result

def isPrime(n):
	for i in range(2, int(n**0.5+1)):
		if n % i == 0:
			return False
	if n == 1:
		return False
	return True

highest = 0

print(isPrime(2143))

for i in range(1, 10):
	if i*(i+1)/2 % 3 != 0:
		symbolSet = list(map(str, range(1, i+1)))
		for j in range(factorial(i)):
			n = nthPermutation(symbolSet, j)
			if isPrime(int(n)):
				print(n)
				highest = n

print(highest)


# 枚举法
def compression(matrix, up, down, col):
    sum = 0
    for i in range(up, down + 1):
        sum += matrix[i][col]
    return sum

def maxSubmatrix1(matrix):
    if len(matrix) == 0 or len(matrix[0]) == 0 or matrix is None:
        return 0
    m, n = len(matrx), len(matrix[0])
    maxSum = 0
    for up in range(m):
        for down in range(up, m):
            curSum = 0
            # 连续子数组最大和
            for col in range(n):
                if curSum > 0:
                    curSum += compression(matrix, up, down, col)
                else:
                    curSum = compression(matrix, up, down, col)
                maxSum = max(maxSum, cuSum)
    return maxSum

# 预先存储1
class Solution1:
    def maxSubmatrix2(self, matrix):
        # write your code here
        if matrix is None or len(matrix) == 0 or len(matrix[0]) == 0:
            return 0
        m, n = len(matrx), len(matrix[0])
        maxSum = 0
        for up in range(m):
            sum = [0 for m in range(n)]
            for down in range(up, m):
                for col in range(n):
                    sum[col] += matrix[down][col]
                temp = self.maxSubarray(sum)
                maxSum = max(maxSum, temp)
        return maxSum

    def maxSubarray(self, array):
        res = 0
        sum = 0
        for i in range(0, len(array)):
            sum += array[i]
            res = max(res, sum)
            sum = max(sum, 0)
        return res

# 预先存储2
class Solution2:
    def maxSubmatrix(self, matrix):
        if matrix is None or len(matrix) == 0 or len(matrix[0]) == 0:
            return 0
        # 定义空间 注意第一行之前再定义一个全0行
        m, n = len(matrix), len(matrix[0])
        total = [[0] * n for _ in range(m+1)]
        maxSum = 0
        # 计算前缀和 特殊处理第一行
        for i in range(m+1):
            for j in range(n):
                if i == 1:
                    total[i][j] = matrix[i-1][j]
                if i >= 2:
                    total[i][j] = total[i - 1][j] + matrix[i-1][j]
        # 计算范围和
        for up in range(1, m+1):
            for down in range(up, m+1):
                curSum = 0
                for col in range(n):
                    if curSum >= 0:
                        curSum += total[down][col] - total[up-1][col]
                    else:
                        curSum =  total[down][col] - total[up-1][col]
                    maxSum = max(curSum, maxSum)
        return maxSum

Leetcode/算法思想/动态规划/子序列/最大子矩阵和.py


import asyncio

import asynctest
import pytest

import aiozk.connection

@pytest.fixture
def connection(event_loop):
    connection = aiozk.connection.Connection(
        host='zookeeper.test',
        port=2181,
        watch_handler=asynctest.MagicMock(),
        read_timeout=30,
        loop=asynctest.MagicMock(wraps=event_loop))

    connection.writer = asynctest.MagicMock()
    return connection

@pytest.mark.asyncio
async def test_close_connection_in_state_closing_do_not_performs_abort(connection):
    connection.abort = asynctest.CoroutineMock()
    connection.closing = True

    await connection.close(asynctest.ANY)

    connection.abort.assert_not_awaited()

@pytest.mark.asyncio
async def test_close_cancels_read_loop_task(connection):
    connection.start_read_loop()
    connection.read_response = asynctest.CoroutineMock(return_value=(0, asynctest.ANY, asynctest.ANY))

    task_cancelled_future = connection.loop.create_future()

    def set_result(task):
        task_cancelled_future.set_result(task.cancelled())

    connection.read_loop_task.add_done_callback(set_result)

    await connection.close(asynctest.ANY)
    assert await task_cancelled_future


# TODO: FNF error not appending

# -*- coding: utf-8 -*-
__author__ = "Yechan Hong"
__maintainer__ = "Yechan Hong"
__email__ = "ychuh@pharmcadd.com"
__status__ = "Dev"

import cb_pdb_map_parser_ALL as cbpdb
from os import path
import math
import numpy as np
from PIL import Image
import ytil
from test import ffindex, distM
from collections import defaultdict
from cifparser import parse

from Bio.PDB.PDBParser import PDBParser
parser = PDBParser(QUIET=True)

#from Bio.PDB.MMCIFParser import MMCIFParser as MMCIFParser
from Bio.PDB.MMCIFParser import FastMMCIFParser as MMCIFParser
parser = MMCIFParser(QUIET=True)

def biopython_get_chains(filepath):
    structure = parser.get_structure("", filepath)
    x = {}
    for chain in structure[0].get_chains():
        x[chain.id] = extract_data(chain)
    return x

DICT_R2RES = {
    'ALA':'A', 'ARG':'R', 'ASN':'N', 'ASP':'D','CYS':'C', 'GLN':'Q', 'GLU':'E', 'GLY':'G',
    'HIS':'H', 'ILE':'I', 'LEU':'L', 'LYS':'K','MET':'M', 'PHE':'F', 'PRO':'P', 'SER':'S',
    'THR':'T', 'TRP':'W', 'TYR':'Y', 'VAL':'V','ASX':'N', 'GLX':'Q', 'UNK':'G', 'HSD':'H',
    }

# GOAL:
#
#
# Save coordinate info, save CA,N,C,(CB) info
#   See if CA,N,C
#   CB = -0.58273431 * a + 0.56802827 * b - 0.54067466 * c + CA
#
# Save sequence info
#   residue.get_resname()    # returns the residue name, e.g. "ASN"

def extract_data(chain):
    # https://biopython.readthedocs.io/en/latest/chapter_pdb.html
    # A residue id is a tuple with three elements (hetero-field, sequence identifier, insertion code)
    # hetero-field is blank for standard amino and nucleic acids.
    isamino = lambda r: r.get_id()[1] == ' '
    # The above method seems to crash when a amino residue has no CA?
    # As an alternative we use this but I am not sure if it is as good
    isamino = lambda r: ('CA' in r) and ('C' in r) and ('N' in r)
    x = []
    seq = ''
    for residue in chain:
        RES = residue.get_resname()
        if isamino(residue) and RES in DICT_R2RES:
            c = residue['CA'].get_coord()
            r = DICT_R2RES[RES]
            seq += r
            x.append(c)

    x = np.array(x)
    return {'CA':x, 'SEQ':seq}

# -------------------------------------------------------------------------------------------
# MAIN --------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------

if __name__ == '__main__':

    save_dir = '/raid0/total_pdb/ca_meta'
    FFINDEX = ffindex('pdb70_a3m.ffindex')
    pC = list( FFINDEX.items() )
    sidx = 0
    #sidx = 24768
    #sidx = 59234 # 6kzb issue where additional ? at end of exp. Resolved when using positive only indicies

    problems = []
    for i,(p,C) in enumerate(pC[sidx:]):

        try:
            print(i+sidx,p,C)
            #chains = biopython_get_chains('PDB/'+p+'.pdb')
            #chains = biopython_get_chains('/raid0/20210112_CIF/'+p.lower()+'.cif')
            chains = parse('/raid0/20210112_CIF/'+p.lower()+'.cif')
            for c in C:
                CA = chains[c]['CA']
                SEQ = chains[c]['SEQ']
                #print( len(SEQ) )
                #print(SEQ)
                M = distM(CA)

                name = p+'_'+c
                np.save( path.join(save_dir,name), {'CA':CA, 'SEQ':SEQ})
                im = Image.fromarray( M.astype(np.int8) )
                im.save( path.join(save_dir, name+'.png') )

        except FileNotFoundError as fnf_error:
            problems.append( str(i+sidx)+' '+str(p)+ ' '+str(fnf_error) )
            ytil.file__list(problems, 'FNF_ERRORS_CIF')
            continue


import csv
import cv2
import numpy as np

import sklearn
from sklearn.model_selection import train_test_split

from keras.models import Sequential
from keras.layers import Flatten, Dense, Lambda, Dropout, Activation
from keras.layers import Conv2D, BatchNormalization, LeakyReLU

samples = np.empty([0])

# Read samples

class Sample():
    def __init__(self, image_path, angle, flip):
        self.image_path = image_path
        self.angle = angle
        self.flip = flip

def extractFileName(path):
    path = path.replace('\\', '/')
    return path.split('/')[-1]

def importCsv(path, negativeOnly=None, positiveOnly=None, curvesOnly=None):
    result = []
    lines = []
    with open('./data/' + path + '/driving_log.csv') as csvfile:
        reader = csv.reader(csvfile)
        for line in reader:
            if (len(line) > 1):
                lines.append(line)

    print(path, len(lines), 'records')

    for line in lines:
        centerImage = './data/' + path + '/IMG/' + extractFileName(line[0])
        leftImage = './data/' + path + '/IMG/' + extractFileName(line[1])
        rightImage = './data/' + path + '/IMG/' + extractFileName(line[2])
        measurement = float(line[3])

        if abs(measurement) > 0.9:
            continue

        if negativeOnly is not None and measurement >= 0:
            continue

        if positiveOnly is not None and measurement <= 0:
            continue

        if positiveOnly is not None and abs(measurement) < 0.05:
            continue

        camera_correction = 0.25

        result.append(Sample(centerImage, measurement, False))
        result.append(Sample(leftImage, measurement + camera_correction, False))
        result.append(Sample(rightImage, measurement - camera_correction, False))

        result.append(Sample(centerImage, measurement, True))
        result.append(Sample(leftImage, measurement + camera_correction, True))
        result.append(Sample(rightImage, measurement - camera_correction, True))

    return result

# Drive in the middle of the road
samples = np.append(samples, importCsv('ccw'))
samples = np.append(samples, importCsv('cw'))

# Curves
samples = np.append(samples, importCsv('curves-ccw', curvesOnly = True ))
samples = np.append(samples, importCsv('curves-2', curvesOnly = True))
samples = np.append(samples, importCsv('curves-3', curvesOnly = True))
samples = np.append(samples, importCsv('curves-4', curvesOnly = True))
samples = np.append(samples, importCsv('curves-5', curvesOnly = True))
samples = np.append(samples, importCsv('curves-6', curvesOnly = True))

# Recovery
samples = np.append(samples, importCsv('recovery-minus', negativeOnly = True))
samples = np.append(samples, importCsv('recovery-plus', positiveOnly = True))
samples = np.append(samples, importCsv('recovery-minus-2', negativeOnly = True))
samples = np.append(samples, importCsv('recovery-plus-2', positiveOnly = True))
samples = np.append(samples, importCsv('bridge-2-minus', negativeOnly = True))
samples = np.append(samples, importCsv('bridge-3-plus', positiveOnly = True))

print('Total samples: ', len(samples))

# Preprocess images

def preprocess(sample):
    image = cv2.imread(sample.image_path)
    angle = sample.angle

    if (sample.flip):
        image = cv2.flip(image, 1)
        angle = -angle

    # Crop
    image = image[60:140]
    return image, angle

def preprocess_color(image):
    hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)
    gray = image[:, :, 1]   # Take G channel
    return np.dstack((gray, hls[:, :, 2]))

train_samples, valid_samples = train_test_split(samples, test_size=0.2)

ch, row, col = 2, 80, 320  # Trimmed image format

print('Train samples: ', len(train_samples))
print('Validation samples: ', len(valid_samples))

# Create generators

def generator(samples, batch_size=32):
    num_samples = len(samples)
    while 1: # Loop forever so the generator never terminates
        np.random.shuffle(samples)
        for offset in range(0, num_samples, batch_size):
            batch_samples = samples[offset:offset+batch_size]
            images = []
            angles = []
            for sample in batch_samples:
                image, angle = preprocess(sample)
                image = preprocess_color(image)
                images.append(image)
                angles.append(angle)

            X_train = np.array(images)
            y_train = np.array(angles)
            yield sklearn.utils.shuffle(X_train, y_train)

# compile and train the model using the generator function
train_generator = generator(train_samples, batch_size=32)
validation_generator = generator(valid_samples, batch_size=32)

# Create model

alpha = 0.05

model = Sequential()

model.add(Lambda(lambda x: x/127.5 - 1.0,
        input_shape=(row, col, ch),
        output_shape=(row, col, ch), name='input'))

model.add(Conv2D(24, 5, 5, name='conv_1_5x5', subsample=(2,3)))
model.add(BatchNormalization(name='norm_1'))
model.add(LeakyReLU(alpha=alpha, name='act_1'))

model.add(Conv2D(36, 5, 5, name='conv_2_5x5', subsample=(2,2)))
model.add(BatchNormalization(name='norm_2'))
model.add(LeakyReLU(alpha=alpha, name='act_2'))

model.add(Conv2D(48, 5, 5, border_mode='valid', name='conv3_5x5', subsample=(2,2)))
model.add(BatchNormalization(name='norm_3'))
model.add(LeakyReLU(alpha=alpha, name='act_3'))

model.add(Conv2D(64, 3, 3, border_mode='valid', name='conv_4_3x3', subsample=(2,1)))
model.add(BatchNormalization(name='norm_4'))
model.add(LeakyReLU(alpha=alpha, name='act_4'))

model.add(Conv2D(64, 3, 3, border_mode='valid', name='conv_5_3x3'))
model.add(BatchNormalization(name='norm_5'))
model.add(LeakyReLU(alpha=alpha, name='act_5'))

model.add(Flatten(name='flat'))
model.add(Dropout(0.5, name='drop_1'))
model.add(Dense(128, activation='elu', name='fc_1'))
model.add(Dropout(0.5, name='drop_2'))
model.add(Dense(64, activation='elu', name='fc_2'))
model.add(Dense(16, activation='elu', name='fc_3'))
model.add(Dense(1, name='regression'))
model.add(Activation(activation='tanh', name='result'))

print(model.summary())

# Train and save the model

from keras import optimizers

optimizer = optimizers.Adam(lr=0.0005)
model.compile(loss='mse', optimizer=optimizer)

history_object = model.fit_generator(
    train_generator,
    samples_per_epoch = len(train_samples),
    validation_data = validation_generator,
    nb_val_samples = len(valid_samples),
    nb_epoch=10,
    verbose = 1
)

model.save('model.h5')
print('model saved')


# coding=utf-8

import tensorflow as tf

# 这里解释了TensorFlow懒运行思想

x = 2
y = 3

op1 = tf.add(x, y)

op2 = tf.multiply(x, y)

# 此节点在TensorFlow运行时候不会执行
useless = tf.multiply(x, op1)

op3 = tf.pow(op2, op1)

with tf.Session() as sess:
    op3 = sess.run(op3)
    print(op3)


#!/usr/bin/env python3
# encoding=utf8

# these are the common/shared tools to process text, especially normalizing chinese text
# many of those are regular expression based parsing
import re

# this will return True if 80% of the character are english
def mostlyEnglish (text):
	match = re.findall('[a-zA-Z]+',text)
	enuLen= len(''.join(match))
	return True if enuLen > len(text)*0.8 else False

# this will take in limited characters set from incoming text string
def extract_chinese_text (text):
    # rmove all puntuations
    text=remove_punctuations(text)
    # find all chinese characters and return as list
    # it will return str on processed text
    chn_chars = re.findall('[0-9a-zA-Z]*[\u4E00-\u9FCC]+[0-9a-zA-Z]*',text)
    return ' '.join(chn_chars)

# this will match the emoticon [嘻嘻哈哈]　 or full-width ［　and ］ and hashtag #小美
# routine return a tuplet for tags-list, text-with-remove-tags
def remove_hashtags (text):
    expression='[@#](\w+)'
    tags=re.findall(expression,text,re.UNICODE)
    return tags, re.sub(expression,'',text)

def remove_emoticons (text):
    expression='\[(\w+)\]'
    emoticons=re.findall(expression,text,re.UNICODE)
    return emoticons, re.sub(expression,'',text)

# this will match the http://...
def remove_httpaddress (text):
    expression='(HTTP|FTP|HTTPS|http|ftp|https)://[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?'
    return re.sub(expression,'',text)

# these are puntuation that occurs on the training text...
import zhon.hanzi,string
regex_punc=re.compile('[%s]' % re.escape(string.punctuation))
def remove_punctuations (text):
    # remove all enu punctutations
    text=regex_punc.sub('',text)
    # remove all Mandarin puntuations
    return re.sub('[%s]' % zhon.hanzi.punctuation, '', text)

# mapping from full char to half-char
import unicodedata
def normalize_fullwidth(text):
    return  unicodedata.normalize('NFKC',text)

# mapping from 0, 1, ..., 9 to chinese character
digits={'0':'零', '1':'一', '2':'二', '3':'三', '4':'四', '5':'五', '6':'六', '7':'七', '8':'八', '9':'九',
        '０':'零', '１':'一', '２':'二', '３':'三', '４':'四', '５':'五', '６':'六', '７':'七', '８':'八', '９':'九'
        }
def spell_out(matchobj):
    # encounter some strange characters that do not corresponding to number
    return ''.join((digits[d]) for d in matchobj.group(0))

# this will process all number string (spell out over-all)
def process_numberstring (text):
    expression = '[0-9]+'
    return re.sub(expression, spell_out ,text)

# this implement a kindof list to graph data structure
""" A Python Class
A simple Python graph class, demonstrating the essential
facts and functionalities of graphs.
"""

class Graph(object):

    def __init__(self, graph_dict=None):
        """ initializes a graph object
            If no dictionary or None is given,
            an empty dictionary will be used
        """
        if graph_dict == None:
            graph_dict = {}
        self.__graph_dict = graph_dict

    def vertices(self):
        """ returns the vertices of a graph """
        return list(self.__graph_dict.keys())

    def edges(self):
        """ returns the edges of a graph """
        return self.__generate_edges()

    def add_vertex(self, vertex):
        """ If the vertex "vertex" is not in
            self.__graph_dict, a key "vertex" with an empty
            list as a value is added to the dictionary.
            Otherwise nothing has to be done.
        """
        if vertex not in self.__graph_dict:
            self.__graph_dict[vertex] = []

    def add_edge(self, edge):
        """ assumes that edge is of type set, tuple or list;
            between two vertices can be multiple edges!
        """
        edge = set(edge)
        (vertex1, vertex2) = tuple(edge)
        if vertex1 in self.__graph_dict:
            self.__graph_dict[vertex1].append(vertex2)
        else:
            self.__graph_dict[vertex1] = [vertex2]

    def __generate_edges(self):
        """ A static method generating the edges of the
            graph "graph". Edges are represented as sets
            with one (a loop back to the vertex) or two
            vertices
        """
        edges = []
        for vertex in self.__graph_dict:
            for neighbour in self.__graph_dict[vertex]:
                if {neighbour, vertex} not in edges:
                    edges.append({vertex, neighbour})
        return edges

    def __str__(self):
        res = "vertices: "
        for k in self.__graph_dict:
            res += str(k) + " "
        res += "\nedges: "
        for edge in self.__generate_edges():
            res += str(edge) + " "
        return res

    def find_path(self, start_vertex, end_vertex, path=None):
        """ find a path from start_vertex to end_vertex
            in graph """
        if path == None:
            path = []
        graph = self.__graph_dict
        path = path + [start_vertex]
        if start_vertex == end_vertex:
            return path
        if start_vertex not in graph:
            return None
        for vertex in graph[start_vertex]:
            if vertex not in path:
                extended_path = self.find_path(vertex,
                                               end_vertex,
                                               path)
                if extended_path:
                    return extended_path
        return None

    def find_all_paths(self, start_vertex, end_vertex, path=[]):
            """ find all paths from start_vertex to
                end_vertex in graph """
            graph = self.__graph_dict
            path = path + [start_vertex]
            if start_vertex == end_vertex:
                return [path]
            if start_vertex not in graph:
                return []
            paths = []
            for vertex in graph[start_vertex]:
                if vertex not in path:
                    extended_paths = self.find_all_paths(vertex,
                                                         end_vertex,
                                                         path)
                    for p in extended_paths:
                        paths.append(p)
            return paths

#str1 ='༢༠༡༣ལོའི་བསང་ཆུ་རྫོང་བོད་རིགས་སློབ་འབྲིང་གི་མཐོ་གསུམ་མཐར་ཕྱིན་གྱི་སློབ་མ་ཡོངས།མཐོ་གསུམ་སློབ་རིམ་གྱི་མཐར་ཕྱིན་མཉམ་པར།2013年夏a河县藏族中学的，高三毕业生的合影。'
#str2 ='年夏河县藏族中学的，高三毕业生的合影'
#str3 ='༢༠༡༣ལོའི་བསང་ཆུ་རྫོང་བ'

#print (extract_chinese_text(str3))


#!/usr/bin/python
import yaml, os, sys, ipaddress

MIN_NUMBER_HOSTS = 10

if len(sys.argv) != 2:
    print("Syntax: %s path/stack_user.yaml" % sys.argv[0])

content = open(sys.argv[1], "r")
template = yaml.load(content, Loader=yaml.FullLoader)

subnet_ips = {}

def find_subnet(network):
    for element, data in template["resources"].items():
        if data["type"] == "OS::Neutron::Subnet":
            if data["properties"]["network_id"]["get_resource"] == network:
                return element

for element, data in template["resources"].items():
    if data["type"] == "OS::Neutron::Port":
        if "fixed_ips" in data["properties"]:
           for fixed_ip in data["properties"]["fixed_ips"]:
                if "subnet" in fixed_ip:
                    subnet = fixed_ip["subnet"]["get_resource"]

                else:
                    subnet = find_subnet(data["properties"]["network"]["get_resource"])
                if subnet not in subnet_ips:
                    subnet_ips[subnet] = []
                subnet_ips[subnet].append(fixed_ip["ip_address"])

print("resources:")

for subnet,ips in subnet_ips.items():
    start = None
    end = 254
    print("  %s: " % (subnet))
    print("    properties:")
    print("      allocation_pools: [", end=" ")
    for ip in sorted(ips, key=lambda item: ipaddress.ip_address(item)):
        prefix = ".".join(ip.split(".")[0:3])
        if not start:
            start = int(ip.split(".")[3])+1
        else:
            end = int(ip.split(".")[3])-1
            if start < end and end-start > MIN_NUMBER_HOSTS:
                print("{\"start\": %s.%d , \"end\": %s.%d}," % (prefix, start, prefix, end), end=" ")
            start = int(ip.split(".")[3])+1
    if 254-end > MIN_NUMBER_HOSTS:
        print("{\"start\": %s.%d , \"end\": %s.%d}" % (prefix, end+2, prefix, 254), end=" ")
    print("]")


import subprocess as subp
import time
import sys

from pmod import ioparse as iop
from pmod import cmdline as cl
from pmod import strlist as strl
from pmod import vmed as vf

def exit_func():
    print("[vmed_con_eos_scripter] Exit: Error detected, see previous msg for details...exiting script")
    sys.exit()

tstart = time.time()

cml = cl.path_parse('Linux')

if('vincp' not in cml.var_path_contain):
    cml.cmd('mkdir vincp')
    exist = False
else:
    cml.cmd('rmdir vincp')
    cml.cmd('mkdir vincp')
    exist = True

### Group V contribs
vlist = vf.v_lines()
if(vlist == False or vlist == []):
    if(vlist == []):
        print("[vmed_con_eos_scripter] Error: no contributions could be parsed from 'contribs.txt'")
    exit_func()

# Go back to working directory and grab input file data
success, in_path_list = cml.cmd("dir sample_input.txt")
if(success == False):
    print("[vmed_con_eos_scripter] Error: 'sample_input.txt' could not be accessed")
    exit_func()

in_path = in_path_list[0]

lines_list = iop.flat_file_grab(in_path, [])
demark = lines_list[-1]

if(strl.str_space_clean(demark.strip('\n').strip('\r')) == ''):
    j = 0
    while(strl.str_space_clean(lines_list[-1+j].strip('\n').strip('\r')) == ''):
        j+=-1
    demark = lines_list[-1+j]
    lines_list = lines_list[:j]

if(demark != "end param."):
    print("[vmed_con_eos_scripter] Warning: the end line in 'sample_input.txt' is not 'end param.'")

sum_failure = False

lines_list = lines_list[:-1]

### Cycle through running each new contrib and storing the result

n = len(vlist)

output_lines = []

output_lines.append("\n")
output_lines.append("         Basic   Part    Tot      Final e   vspread  f    V     t    n\n")
output_lines.append("\n")

for i in range(n):

    # Generate new input
    new_lines = list(lines_list)
    for j in vlist[i]:
        val = j+'\n'
        new_lines.append(val)
    new_lines.append(demark)

    force = vf.ventry("force",vlist[i])
    if(len(force) == 1):
        force=force+' '
    vnum  = vf.ventry("v",vlist[i])
    if(len(vnum)==2):
        vnum = vnum+' '
    tensor= vf.ventry("tensor",vlist[i])

    # Write the new output and run the program
    iop.flat_file_write(in_path,new_lines)
    subp.call('./xtest',shell=True)

    #
    iop.flat_file_grab('test1.txt', scrub=True)
    values = vf.partial_eos(file_name = 'test1.txt')
    for j in range(len(values)):
        values[j] = strl.str_to_list(values[j], filtre = True)

    try:

        v1 = values[-1][-1]
        v2 = values[-2][-1]
        v3 = values[-3][-1]
        v5 = values[-5][-1]
        v8 = values[-8][-1]

        v12 = abs(float(v1)-float(v2))
        v13 = abs(float(v2)-float(v3))
        v15 = abs(float(v3)-float(v5))
        v18 = abs(float(v5)-float(v8))

        vspread = round((v12+v13+v15)/3.0,4)

        if(v18 >= v15 and v15 >= v13 and v13 >= v12):
            totconv = 'True '
        else:
            totconv = 'False'

        if(v18 >= v15 and v15 >= v12):
            partconv = 'True '
        else:
            partconv = 'False'

        if(v18 >= v12):
            basconv = 'True '
        else:
            basconv = 'False'

        test_line_seq = ["   ",
                         str(basconv),
                         str(partconv),
                         str(totconv),
                         str(v1),
                         str(vspread),
                         force,
                         vnum,
                         tensor,
                         str(i)+'\n']

    except:
        sum_failure = True
        print("[vmed_con_eos_scripter] Error: error occured when attempting to perform convergence tests")
        test_line_seq = ["   ","Failed","Failed","Failed","Failed","Failed",str(i)+"\n"]

    output_lines.append(strl.array_to_str(test_line_seq, spc = '   '))

    # Consolidating Files
    cml.cmd('mv test.txt;test1.txt vincp')
    cml.cmd('cd vincp')
    new_fold_name = 'run_'+str(i)
    cml.cmd('mkdir '+new_fold_name)
    cml.cmd('mv test.txt;test1.txt '+new_fold_name)
    cml.cmd('cd ..')

lines_list.append(demark)
iop.flat_file_write(in_path,lines_list)

try:
    iop.flat_file_write('sum_test.txt', output_lines)
except:
    sum_failure = True
    line1 = "[vmed_con_eos_scripter] Error: an error occured when attempting "
    line2 = "to write convergence tests results to 'sum_test.txt'"
    print(line1+line2)

tend = time.time()

print('')
spc = '    '

if(sum_failure):
    print("Finished, Error(s) reported (see above for error msg(s)), see below for run details:\n")
else:
    print("Finished! No Errors reported, see below for run details:\n")
print(spc+"The script took approx. "+str(round(tend-tstart,1))+" secs to run")
print(spc+"The script found "+str(n)+" contributions")
if(exist):
    print(spc+"The script found an existing 'vincp' directory and overwrote it\n")
else:
    print(spc+"The script did not find an existing 'vincp' directory; one was created\n")


# Create your views here.
from config.settings import HOST_DIR, FILE_ENCODING, IP_DIR, \
    PROXY_DNS_CMD_SERVERS
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
import os
import re
import socket

name_pattern = re.compile('^[0-9a-z-]{1,50}$')

CMD_IP = 'IP load %s'
CMD_HOST = 'HOSTS load %s'

def host_list(req):
    hosts_list = [f for f in os.listdir(HOST_DIR) if not f.startswith(".")]
    hosts_list.sort(key=lambda f: os.path.getmtime(HOST_DIR + f), reverse=True)
    return render_to_response('host/host/list.html', {'list':hosts_list})

def host_add(req):
    if req.method == 'GET':
        return render_to_response('host/host/add.html', {})
    else:
        name = req.POST.get('name', '').strip()
        content = req.POST.get('content', '')
        error = None
        if not name_pattern.match(name):
            error = 'Name Field must be a-z,0-9,and - !'
            return render_to_response('host/host/add.html', {'name':name, 'content':content, 'error':error})
        if os.path.exists(HOST_DIR + name):
            error = 'Hosts named %s is already exists!' % name
            return render_to_response('host/host/add.html', {'name':name, 'content':content, 'error':error})
        open(HOST_DIR + name, 'w').write(content.encode(FILE_ENCODING))
        cmd(CMD_HOST % name)
        return HttpResponseRedirect('/host/list/')

def host_update(req, name):
    if req.method == 'GET':
        content = open(HOST_DIR + name).read() if os.path.exists(HOST_DIR + name) else ''
        return render_to_response('host/host/update.html', {'name':name, 'content':content})
    else:
        content = req.POST.get('content')
        open(HOST_DIR + name, 'w').write(content.encode(FILE_ENCODING))
        cmd(CMD_HOST % name)
        return HttpResponseRedirect('/host/list/')

def host_delete(req, name):
    if os.path.exists(HOST_DIR + name):
        os.remove(HOST_DIR + name)
        cmd(CMD_HOST % name)
    return HttpResponseRedirect('/host/list/')

def ip_list(req):
    ip_list = [f for f in os.listdir(IP_DIR) if not f.startswith(".")]
    ip_list.sort(key=lambda f: os.path.getmtime(IP_DIR + f), reverse=True)
    return render_to_response('host/ip/list.html', {'list':ip_list})

def ip_info(req, ip):
    content = open(IP_DIR + ip).read() if os.path.exists(IP_DIR + ip) else ''
    return render_to_response('host/ip/info.html', {'ip':ip, 'content':content})

def ip_delete(req, ip):
    if os.path.exists(IP_DIR + ip):
        os.remove(IP_DIR + ip)
    cmd(CMD_IP % ip)
    return HttpResponseRedirect('/ip/list/')

def ip_update(req, name):
    ip = req.META.get('REMOTE_ADDR')
    open(IP_DIR + ip, 'w').write(name)
    cmd(CMD_IP % ip)
    return HttpResponseRedirect('/ip/list/')

def cmd(data):
    for cmd in PROXY_DNS_CMD_SERVERS:
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.connect(cmd)
        sock.settimeout(5)
        sock.sendall(data)
        sock.close()


from functools import lru_cache

###############################################################################
# Napisite funkcijo [najdaljse_narascajoce_podazporedje], ki sprejme seznam in
# poisce najdaljse (ne strogo) narascajoce podzaporedje stevil v seznamu.
#
# Na primer: V seznamu [2, 3, 6, 8, 4, 4, 6, 7, 12, 8, 9] je najdaljse naj vrne
# rezultat [2, 3, 4, 4, 6, 7, 8, 9].
###############################################################################

s = [2, 3, 6, 8, 4, 4, 6, 7, 12, 8, 9]

def najdaljse_narascajoce_podzaporedje(sez):

    @lru_cache(maxsize=None)
    def najdaljse(spodnja_meja, i):
        if i >= len(sez):
            return []
        elif sez[i] < spodnja_meja:
            return najdaljse(spodnja_meja, i+1)
        else:
            vzamemo = [sez[i]] + najdaljse(sez[i], i+1)
            ne_vzamemo = najdaljse(spodnja_meja, i+1)
            if len(vzamemo) > len(ne_vzamemo):
                return vzamemo
            else:
                return ne_vzamemo
    if len(sez) == 0:
        return []
    else:
        return najdaljse(sez[0], 0)

###############################################################################
# Nepreviden študent je pustil robotka z umetno inteligenco nenadzorovanega.
# Robotek želi pobegniti iz laboratorija, ki ga ima v pomnilniku
# predstavljenega kot matriko števil:
#   - ničla predstavlja prosto pot
#   - enica predstavlja izhod iz laboratorija
#   - katerikoli drugi znak označuje oviro, na katero robotek ne more zaplejati

# Robotek se lahko premika le gor, dol, levo in desno, ter ima omejeno količino
# goriva. Napišite funkcijo [pobeg], ki sprejme matriko, ki predstavlja sobo,
# začetno pozicijo in pa število korakov, ki jih robotek lahko naredi z
# gorivom, in izračuna ali lahko robotek pobegne. Soba ima vedno vsaj eno
# polje.
#
# Na primer za laboratorij:
# [[0, 1, 0, 0, 2],
#  [0, 2, 2, 0, 0],
#  [0, 0, 2, 2, 0],
#  [2, 0, 0, 2, 0],
#  [0, 2, 2, 0, 0],
#  [0, 0, 0, 2, 2]]
#
# robotek iz pozicije (3, 1) pobegne čim ima vsaj 5 korakov, iz pozicije (5, 0)
# pa v nobenem primeru ne more, saj je zagrajen.
###############################################################################

soba = [[0, 1, 0, 0, 2],
        [0, 2, 2, 0, 0],
        [0, 0, 2, 2, 0],
        [2, 0, 0, 2, 0],
        [0, 2, 2, 0, 0],
        [0, 0, 0, 2, 2]]

def pobeg(soba, pozicija, koraki):
    max_vrsta = len(soba)
    max_stolpec = len(soba[0])

    @lru_cache(maxsize=None)
    def pobegni(i, j, koraki):
        # Robotek pade iz sobe:
        if not (0 <= i < max_vrsta) or not (0 <= j < max_stolpec):
            return False
        # Robotek pobegne:
        elif soba[i][j] == 1:
            return True
        # Robotek nadaljuje s pobegom:
        elif soba[i][j] == 0 and koraki > 0:
            return any([pobegni(i+1, j, koraki-1),
                        pobegni(i-1, j, koraki-1),
                        pobegni(i, j+1, koraki-1),
                        pobegni(i, j-1, koraki-1)])
        # Robotku zmanjka korakov ali pa zadene oviro:
        else:
            return False
    return pobegni(pozicija[0], pozicija[1], koraki)