from __future__ import unicode_literals, print_function
import random
from spacy.util import minibatch, compounding
import argparse
from spacy.tokens import Doc
from spacy.training import Example
from data_reader import ConllReader
from spacy.vocab import Vocab
from spacy.language import Language

nlp = Language(Vocab())

def get_sentences(file_path):
    sentences = []

    with open(file_path, encoding="utf-8") as f:
        lines = f.readlines()
        curr_sentence = []
        for line in lines:

            if line == "\n":
                sentences.append(curr_sentence)
                curr_sentence = []
                continue

            if line.startswith("#"):
                continue

            curr_sentence.append(line)

    return sentences

def evaluate_model(parsing_model):
    dev_data = ConllReader("dev").get_examples()
    unlabeled = 0
    all_words = 0
    labeled = 0

    for text, pos, heads, deps in dev_data:
        all_words += len(text)

        analyzed_sent = parsing_model(Doc(nlp.vocab, words=text, pos=pos))
        for i, word in enumerate(analyzed_sent):
            if word.head.i == heads[i]:
                unlabeled += 1
                if word.dep_ == deps[i]:
                    labeled += 1
    print(f"unlabeled: {round(unlabeled*100/all_words, 2)}%")
    print(f"labeled: {round(labeled*100/all_words, 2)}%")

def pipeline(model_path, n_epochs, batch_size):
    train_data = ConllReader("train").get_examples()
    parser = nlp.add_pipe("parser", first=True)
    # parser.initialize(lambda: [], nlp=nlp)

    # Add the tags. This needs to be done before you start training.
    for texts, pos, heads, deps in train_data:
        for dep in deps:
            parser.add_label(dep)

    other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'parser']
    with nlp.disable_pipes(*other_pipes):
        optimizer = nlp.initialize()
        # optimizer = nlp.begin_training()
        # optimizer = nlp.create_optimizer()

        print("Start training!")
        for i in range(n_epochs):
            random.shuffle(train_data)
            losses = {}
            # batch up the examples using spaCy's minibatch
            batches = minibatch(train_data, size=compounding(batch_size, 2, 1.001))
            for batch in batches:
                texts, pos, heads, deps = zip(*batch)
                examples = []
                try:
                    for j in range(len(texts)):
                        doc = Doc(nlp.vocab, words=texts[j], pos=pos[j])
                        example = Example.from_dict(doc, {"heads": heads[j], "deps": deps[j]})
                        examples.append(example)
                    losses = parser.update(examples, sgd=optimizer, losses=losses)
                except Exception as error:
                    print(error)
            print(f'Loss in epoch {i+1} is {losses["parser"]}')
            evaluate_model(parser)
        print("Training is done!")
        parser.to_disk(model_path)
        print(f"Parser saved to {model_path}")

def main(args):
    try:
        pipeline(args.model_path_to_save, args.epochs, args.batch_size)
        # parser = nlp.add_pipe("parser")
        # parser.from_disk("models/trained_parser")
        # evaluate_model(parser)
    except Exception as error:
        print(error)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Aligner model')

    parser.add_argument('model_path_to_save', help='path to save the trained model', type=str)
    parser.add_argument('-tp', '--train_path', help='path to the training data', type=str)
    parser.add_argument('-e', '--epochs', help='number of epochs', default=10, type=int)
    parser.add_argument('-b', '--batch_size', help='batch size', default=64, type=int)

    args = parser.parse_args()
    main(args)


'''
Completed script for NSO Training Day 2 Lab 4

Your goal in this lab is to learn how to iterate through the list objects
and manipulate large sets of NSO objects.

Step 1:
    Run the main function and see the outputted data.

Step 2:
    For any GigabitEthernet interface with a "2" digit in the interface ID
    and already has an assigned vlan, change the data vlan to 2.
    For example, Gi1/0/2, Gi1/0/20, Gi2/0/1 would all be in scope to be
    modified. If the vlan is None, ignore it.

Step 3:
    Complete the audit function so that it returns a list of interface IDs
    that belong to vlan 2.
'''
from __future__ import print_function
import ncs

def main(hostname, new_vlan=None):
    '''Connects to an SVL device and changes the vlan for many interfaces.

    Args:
        hostname (str): Name of the SVL switch that you are configuring.
        new_vlan (str): The new vlan that you want to change to.
    '''
    with ncs.maapi.single_write_trans('admin', 'python') as trans:
        root = ncs.maagic.get_root(trans)
        device = root.devices.device[hostname]

        for interface in device.config.ios__interface.GigabitEthernet:
            vlan_id = interface.switchport.access.vlan
            print("GigabitEthernet{0} is on vlan {1}"
                  .format(interface.name, vlan_id))
            if "2" in interface.name and new_vlan and vlan_id:
                print("Will modify interface.name")
                interface.switchport.access.vlan = new_vlan

        print("Applying transaction...")
        trans.apply()

def audit_vlan2(hostname):
    '''Return a list of all interfaces that are on vlan 2.

    Args:
        hostname (str): Name of the SVL switch that you are configuring.

    Returns:
        (list[str]): List of interface IDs that belong on vlan 2.
    '''
    with ncs.maapi.single_write_trans('admin', 'python') as trans:
        root = ncs.maagic.get_root(trans)
        device = root.devices.device[hostname]

        vlan_2_interfaces = []
        for interface in device.config.ios__interface.GigabitEthernet:
            vlan_id = interface.switchport.access.vlan
            if vlan_id == 2:
                vlan_2_interfaces.append(interface.name)
        return vlan_2_interfaces

if __name__ == "__main__":
    main("svl290-gg07-c3850-sw5-econ.cisco.com", "2")
    print(audit_vlan2("svl290-gg07-c3850-sw5-econ.cisco.com"))


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

from tensorflow.core.protobuf import config_pb2
from tensorflow.core.protobuf import rewriter_config_pb2
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import errors
from tensorflow.python.framework import kernels
from tensorflow.python.framework import ops
from tensorflow.python.framework import test_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import collective_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import variables
from tensorflow.python.platform import test

class CollectiveOpTest(test.TestCase):

  def _testCollectiveReduce(self, inputs, expected, set_graph_key,
                            communication_hint='auto'):
    group_key = 1
    group_size = len(inputs)
    instance_key = 1
    device_type = 'CPU'
    config = config_pb2.ConfigProto(device_count={device_type: group_size})
    devices = ['/{}:{}'.format(device_type, i) for i in range(group_size)]

    with self.session(config=config) as sess:
      colred = []
      for i in range(group_size):
        with ops.device(devices[i]):
          tensor = constant_op.constant(inputs[i])
          colred.append(collective_ops.all_reduce(
              tensor, group_size, group_key, instance_key, 'Add', 'Div',
              communication_hint=communication_hint))
      run_options = config_pb2.RunOptions()
      if set_graph_key:
        run_options.experimental.collective_graph_key = 1
      results = sess.run(colred, options=run_options)
    for i in range(group_size):
      self.assertAllClose(results[i], expected, rtol=1e-5, atol=1e-5)

  def _testMultipleConcurrentCollectiveReduce(self, t0, t1, expected):
    group_key = 1
    group_size = 2
    num_instances = 2
    all_reduces = []
    config = config_pb2.ConfigProto(device_count={'CPU': group_size})
    config.experimental.collective_deterministic_sequential_execution = True
    with self.session(config=config) as sess:
      for cpu in range(group_size):
        with ops.device('/CPU:%d' % cpu):
          in_tensor = constant_op.constant(t0 if cpu == 0 else t1)
          for instance in range(num_instances):
            all_reduces.append(collective_ops.all_reduce(
                in_tensor, group_size, group_key, instance, 'Add', 'Div'))
      results = sess.run(all_reduces)
    for i in range(group_size * num_instances):
      self.assertAllClose(results[i], expected, rtol=1e-5, atol=1e-5)

  @test_util.run_deprecated_v1
  def testCollectiveReduce(self):
    self._testCollectiveReduce(
        inputs=[[0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1],
                [0.3, 1.3, 2.3, 3.3, 4.3, 5.3, 6.3, 7.3]],
        expected=[0.2, 1.2, 2.2, 3.2, 4.2, 5.2, 6.2, 7.2],
        set_graph_key=True)

  @test_util.run_deprecated_v1
  def testCollectiveAutoGraphKey(self):
    self._testCollectiveReduce(
        inputs=[[0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1],
                [0.3, 1.3, 2.3, 3.3, 4.3, 5.3, 6.3, 7.3]],
        expected=[0.2, 1.2, 2.2, 3.2, 4.2, 5.2, 6.2, 7.2],
        set_graph_key=False)

  @test_util.run_deprecated_v1
  def testCollectiveMultipleConcurrentReduce(self):
    self._testMultipleConcurrentCollectiveReduce(
        [0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1],
        [0.3, 1.3, 2.3, 3.3, 4.3, 5.3, 6.3, 7.3],
        [0.2, 1.2, 2.2, 3.2, 4.2, 5.2, 6.2, 7.2])

  @test_util.run_deprecated_v1
  def testNcclHintFallbackToRingReduce(self):
    """Tests that setting `communication_hint=nccl` works on non-GPU builds."""
    if kernels.get_registered_kernels_for_op('NcclAllReduce'):
      self.skipTest('Run only on non-GPU environments')
    self._testCollectiveReduce(
        inputs=[[0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1],
                [0.3, 1.3, 2.3, 3.3, 4.3, 5.3, 6.3, 7.3]],
        expected=[0.2, 1.2, 2.2, 3.2, 4.2, 5.2, 6.2, 7.2],
        set_graph_key=False,
        communication_hint='nccl')

  def _testWhile(self, num_vars, num_iterations, key_base):
    group_size = 2
    group_key = 1
    instances = [(key_base + i) for i in range(num_vars)]
    devices = ['CPU:{}'.format(i) for i in range(group_size)]

    config = config_pb2.ConfigProto(device_count={'CPU': group_size})
    rewrite_options = config.graph_options.rewrite_options
    rewrite_options.scoped_allocator_optimization = (
        rewriter_config_pb2.RewriterConfig.ON)
    del rewrite_options.scoped_allocator_opts.enable_op[:]
    rewrite_options.scoped_allocator_opts.enable_op.append('CollectiveReduce')

    with self.session(config=config) as sess:
      loop_vars = []
      for device in devices:
        with ops.device(device):
          loop_vars.append(
              [variables.VariableV1((1 << i) * 1.) for i in range(num_vars)])
      # This variable controls number of iterations.
      loop_vars.append(variables.VariableV1(0.))
      def loop_body(dev0_tensors, dev1_tensors, loop_tensor):
        return_ops = []
        for i in range(len(devices)):
          device = devices[i]
          device_tensors = dev0_tensors if i == 0 else dev1_tensors
          with ops.device(device):
            device_collectives = []
            for j in range(num_vars):
              # NOTE(ayushd): we need the `identity` here to ensure that the
              # input to `all_reduce` has an explicit device string.
              input_tensor = array_ops.identity(device_tensors[j])
              collective_op = collective_ops.all_reduce(
                  input_tensor, group_size, group_key, instances[j],
                  'Add', 'Id')
              device_collectives.append(collective_op)
            return_ops.append(device_collectives)
        return_ops.append(math_ops.add(loop_tensor, 1.))
        return return_ops
      # Run until last variable exceeds number of iterations.
      loop_cond = lambda d0, d1, i: math_ops.less(i, num_iterations)
      sess.run(variables.global_variables_initializer())
      results = sess.run(control_flow_ops.while_loop(loop_cond, loop_body,
                                                     loop_vars))
      self.assertEqual(results[:-1], [
          [((1 << (num_iterations + v)) * 1.) for v in range(num_vars)]
          for _ in range(group_size)])

  @test_util.run_deprecated_v1
  def testSimpleWhile(self):
    self._testWhile(num_vars=1, num_iterations=4, key_base=20)

  @test_util.run_deprecated_v1
  def testWhileMultipleAllReduce(self):
    self.skipTest('Temporarily disabled')  # TODO(b/135686041): re-enable
    self._testWhile(num_vars=2, num_iterations=4, key_base=20)

  @test_util.run_deprecated_v1
  def testWhileWithScopedAllocator(self):
    group_size = 2
    group_key = 1
    instance_key0 = 1
    instance_key1 = 2

    config = config_pb2.ConfigProto(device_count={'CPU': group_size})
    rewrite_options = config.graph_options.rewrite_options
    rewrite_options.scoped_allocator_optimization = (
        rewriter_config_pb2.RewriterConfig.ON)
    del rewrite_options.scoped_allocator_opts.enable_op[:]
    rewrite_options.scoped_allocator_opts.enable_op.append('CollectiveReduce')

    with self.session(config=config) as sess:
      run_ops = []
      for i in range(group_size):
        with ops.device('CPU:%d' % i):
          constant = constant_op.constant(0.)
          cond = lambda i: math_ops.less(i, 10.)
          body = lambda i: math_ops.add(i, 1.)
          input0 = control_flow_ops.while_loop(cond, body, [constant])
          input1 = math_ops.add(constant, 5)
          colred0 = collective_ops.all_reduce(input0, group_size, group_key,
                                              instance_key0, 'Add', 'Id')
          colred1 = collective_ops.all_reduce(input1, group_size, group_key,
                                              instance_key1, 'Add', 'Id')
          run_ops.append(math_ops.add_n([colred0, colred1]))
      results = sess.run(run_ops)
      self.assertEqual(results, [30., 30.])

  @test_util.run_deprecated_v1
  def testCollectiveReduceScalar(self):
    self._testCollectiveReduce(inputs=[0.1, 0.3], expected=0.2,
                               set_graph_key=True)

  def _testCollectiveBroadcast(self, t0):
    group_key = 1
    instance_key = 1
    with self.session(
        config=config_pb2.ConfigProto(device_count={'CPU': 2})) as sess:
      with ops.device('/CPU:0'):
        in0 = constant_op.constant(t0)
        out0 = collective_ops.broadcast_send(in0, in0.shape, in0.dtype,
                                             2, group_key, instance_key)
      with ops.device('/CPU:1'):
        c1 = constant_op.constant(t0)
        out1 = collective_ops.broadcast_recv(c1.shape, c1.dtype,
                                             2, group_key, instance_key)
      run_options = config_pb2.RunOptions()
      run_options.experimental.collective_graph_key = 1
      results = sess.run([out0, out1], options=run_options)
    self.assertAllClose(results[0], t0, rtol=1e-5, atol=1e-5)
    self.assertAllClose(results[1], t0, rtol=1e-5, atol=1e-5)

  @test_util.run_deprecated_v1
  def testCollectiveBroadcast(self):
    self._testCollectiveBroadcast([0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1])

  def _testCollectiveGather(self, t0, t1, expected, set_graph_key):
    group_key = 1
    instance_key = 1
    with self.session(
        config=config_pb2.ConfigProto(device_count={'CPU': 2})) as sess:
      with ops.device('/CPU:0'):
        in0 = constant_op.constant(t0)
        c0 = collective_ops.all_gather(in0, 2, group_key, instance_key)
      with ops.device('/CPU:1'):
        in1 = constant_op.constant(t1)
        c1 = collective_ops.all_gather(in1, 2, group_key, instance_key)
      run_options = config_pb2.RunOptions()
      if set_graph_key:
        run_options.experimental.collective_graph_key = 1
      results = sess.run([c0, c1], options=run_options)
    self.assertAllClose(results[0], expected, rtol=1e-5, atol=1e-5)
    self.assertAllClose(results[1], expected, rtol=1e-5, atol=1e-5)

  @test_util.run_deprecated_v1
  def testCollectiveGather(self):
    self._testCollectiveGather([0, 1, 2, 3, 4, 5, 6, 7],
                               [10, 11, 12, 13, 14, 15, 16, 17],
                               [0, 1, 2, 3, 4, 5, 6, 7,
                                10, 11, 12, 13, 14, 15, 16, 17],
                               True)
    self._testCollectiveGather([[0, 1, 2, 3], [4, 5, 6, 7]],
                               [[10, 11, 12, 13], [14, 15, 16, 17]],
                               [[0, 1, 2, 3], [4, 5, 6, 7],
                                [10, 11, 12, 13], [14, 15, 16, 17]],
                               True)
    self._testCollectiveGather([[[0, 1], [2, 3]], [[4, 5], [6, 7]]],
                               [[[10, 11], [12, 13]], [[14, 15], [16, 17]]],
                               [[[0, 1], [2, 3]], [[4, 5], [6, 7]],
                                [[10, 11], [12, 13]], [[14, 15], [16, 17]]],
                               True)

  '''
  @test_util.run_deprecated_v1
  def testCollectiveGatherShapeMismatch(self):
    group_key = 1
    instance_key = 1
    t0 = [1, 2, 3, 4]
    t1 = [5, 6, 7, 8]
    t2 = [9, 10]
    with self.session(
        config=config_pb2.ConfigProto(device_count={'CPU': 2})) as sess:
      with ops.device('/CPU:0'):
        in0 = constant_op.constant(t0)
        c0 = collective_ops.all_gather(in0, 2, group_key, instance_key)
      with ops.device('/CPU:1'):
        in1 = constant_op.constant(t1)
        in2 = constant_op.constant(t2)
        c1 = collective_ops.all_gather(in1, 2, group_key, instance_key)
        c2 = collective_ops.all_gather(in2, 2, group_key, instance_key)
      run_options = config_pb2.RunOptions()
      run_options.experimental.collective_graph_key = 1
      sess.run([c0, c1], options=run_options)
      with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                   'Shape mismatch'):
        sess.run([c0, c2], options=run_options)

  @test_util.run_deprecated_v1
  def testCollectiveGatherShapeMismatchAcrossDevices(self):
    group_key = 1
    instance_key = 1
    t0 = [1, 2, 3, 4]
    t1 = [5, 6]
    with self.session(
        config=config_pb2.ConfigProto(device_count={'CPU': 2})) as sess:
      with ops.device('/CPU:0'):
        in0 = constant_op.constant(t0)
        c0 = collective_ops.all_gather(in0, 2, group_key, instance_key)
      with ops.device('/CPU:1'):
        in1 = constant_op.constant(t1)
        c1 = collective_ops.all_gather(in1, 2, group_key, instance_key)
      run_options = config_pb2.RunOptions()
      run_options.experimental.collective_graph_key = 1
      with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                   'Shape mismatch'):
        sess.run([c0, c1], options=run_options)
  '''

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


import os
os.environ['MUJOCO_GL'] = 'egl'
import warnings
warnings.filterwarnings('ignore')

import hydra
import imageio
import numpy as np
import torch
from termcolor import colored

from common.parser import parse_cfg
from common.seed import set_seed
from envs import make_env
from tdmpc2 import TDMPC2

torch.backends.cudnn.benchmark = True

@hydra.main(config_name='config', config_path='.')
def evaluate(cfg: dict):
	"""
	Script for evaluating a single-task / multi-task TD-MPC2 checkpoint.

	Most relevant args:
		`task`: task name (or mt30/mt80 for multi-task evaluation)
		`model_size`: model size, must be one of `[1, 5, 19, 48, 317]` (default: 5)
		`checkpoint`: path to model checkpoint to load
		`eval_episodes`: number of episodes to evaluate on per task (default: 10)
		`save_video`: whether to save a video of the evaluation (default: True)
		`seed`: random seed (default: 1)

	See config.yaml for a full list of args.

	Example usage:
	````
		$ python evaluate.py task=mt80 model_size=48 checkpoint=/path/to/mt80-48M.pt
		$ python evaluate.py task=mt30 model_size=317 checkpoint=/path/to/mt30-317M.pt
		$ python evaluate.py task=dog-run checkpoint=/path/to/dog-1.pt save_video=true
	```
	"""
	assert torch.cuda.is_available()
	assert cfg.eval_episodes > 0, 'Must evaluate at least 1 episode.'
	cfg = parse_cfg(cfg)
	set_seed(cfg.seed)
	print(colored(f'Task: {cfg.task}', 'blue', attrs=['bold']))
	print(colored(f'Model size: {cfg.model_size}', 'blue', attrs=['bold']))
	print(colored(f'Checkpoint: {cfg.checkpoint}', 'blue', attrs=['bold']))
	if not cfg.multitask and ('mt80' in cfg.checkpoint or 'mt30' in cfg.checkpoint):
		print(colored('Warning: single-task evaluation of multi-task models is not currently supported.', 'red', attrs=['bold']))
		print(colored('To evaluate a multi-task model, use task=mt80 or task=mt30.', 'red', attrs=['bold']))

	# Make environment
	env = make_env(cfg)

	# Load agent
	agent = TDMPC2(cfg)
	assert os.path.exists(cfg.checkpoint), f'Checkpoint {cfg.checkpoint} not found! Must be a valid filepath.'
	agent.load(cfg.checkpoint)

	# Evaluate
	if cfg.multitask:
		print(colored(f'Evaluating agent on {len(cfg.tasks)} tasks:', 'yellow', attrs=['bold']))
	else:
		print(colored(f'Evaluating agent on {cfg.task}:', 'yellow', attrs=['bold']))
	if cfg.save_video:
		video_dir = os.path.join(cfg.work_dir, 'videos')
		os.makedirs(video_dir, exist_ok=True)
	scores = []
	tasks = cfg.tasks if cfg.multitask else [cfg.task]
	for task_idx, task in enumerate(tasks):
		if not cfg.multitask:
			task_idx = None
		ep_rewards, ep_successes = [], []
		for i in range(cfg.eval_episodes):
			obs, done, ep_reward, t = env.reset(task_idx=task_idx), False, 0, 0
			if cfg.save_video:
				frames = [env.render()]
			while not done:
				action = agent.act(obs, t0=t==0, task=task_idx)
				obs, reward, done, info = env.step(action)
				ep_reward += reward
				t += 1
				if cfg.save_video:
					frames.append(env.render())
			ep_rewards.append(ep_reward)
			ep_successes.append(info['success'])
			if cfg.save_video:
				imageio.mimsave(
					os.path.join(video_dir, f'{task}-{i}.mp4'), frames, fps=15)
		ep_rewards = np.mean(ep_rewards)
		ep_successes = np.mean(ep_successes)
		if cfg.multitask:
			scores.append(ep_successes*100 if task.startswith('mw-') else ep_rewards/10)
		print(colored(f'  {task:<22}' \
			f'\tR: {ep_rewards:.01f}  ' \
			f'\tS: {ep_successes:.02f}', 'yellow'))
	if cfg.multitask:
		print(colored(f'Normalized score: {np.mean(scores):.02f}', 'yellow', attrs=['bold']))

if __name__ == '__main__':
	evaluate()


from PyQt5 import QtWidgets

class ImageWidget(QtWidgets.QLabel):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setScaledContents(True)

    # def hasHeightForWidth(self):
    #     return self.pixmap() is not None
    #
    # def heightForWidth(self, w):
    #     if self.pixmap():
    #         return int(w * (self.pixmap().height() / self.pixmap().width()))


import httpx
import json
import time
from dynaconf import settings, LazySettings
from prometheus_client import start_http_server
from prometheus_client.core import REGISTRY, GaugeMetricFamily
settings = LazySettings(ENVVAR_PREFIX_FOR_DYNACONF='MONITIS')

class Monitis():
    def __init__(self, apikey, secretkey):
        self.apikey = apikey
        self.secretkey = secretkey

    def token(self):
        params = {
            'action': 'authToken',
            'apikey': self.apikey,
            'secretkey': self.secretkey,
            'output': 'json'
        }
        r = httpx.get('https://api.monitis.com/api', params=params)
        output = json.loads(r.content)
        return output['authToken']

    def tests(self, test_type='https'):
        params = {
            'apikey': self.apikey,
            'output': 'json',
            'version': 3,
            'action': 'tests',
            'validation': 'token',
            'authToken': self.token()
        }
        list_output = httpx.get('https://api.monitis.com/api', params=params)
        output = json.loads(list_output.content)
        return [elem['name'] for elem in output['testList'] if elem['type'] == test_type]

    def last_test_result(self):
        params = {
            'apikey': self.apikey,
            'output': 'json',
            'version': 3,
            'action': 'testsLastValues',
            'validation': 'token',
            'authToken': self.token(),
        }
        last_result_output = httpx.get('https://api.monitis.com/api', params=params)
        output = json.loads(last_result_output.content)
        return output

class MonitisCollector():
    def __init__(self, monitis):
        self.monitis = monitis

    def collect(self):
        result = self.monitis.last_test_result()
        labels = ['name', 'location', 'testType', 'status']
        gauge = GaugeMetricFamily("monitis_https", "HTTP/S Response Time", value=None, labels=labels)
        for location in result:
            for site in location['data']:
                if site['testType'] == 'https':
                    labelvalues = [site['name'], location['name'], 'https', site['status']]
                    gauge.add_metric(labelvalues, site['perf'])
        yield gauge

        labels = ['name', 'location', 'status']
        gauge = GaugeMetricFamily("monitis_certificate", "Certificate Expiry time", value=None, labels=labels)
        for location in result:
            for site in location['data']:
                if site['testType'] == 'certificate':
                    labelvalues = [site['name'], location['name'], site['status']]
                    gauge.add_metric(labelvalues, site['perf'])
        yield gauge
def setup():
    m = Monitis(settings.APIKEY, settings.SECRETKEY)
    REGISTRY.register(MonitisCollector(m))
    start_http_server(8000)

if __name__ == '__main__':
    setup()
    while True:
        time.sleep(1)


# Undirected Graph Data Structures
# Vertices are IDs
# Edges are most important, hold weighted connections between IDs
from collections import deque

# Non-directional vertex, essentially just a string ID
class Vertex:
	def __init__(self, name: str):
		# Potential attributes: Rank, IO degree
		self.ID = name
	# Map ID str to object reference
	def __str__(self):
		return self.ID
	def __repr__(self):
		return self.ID

# Weighted edge shared between two vertices
# Not necessarily undirectional (we'll use this in a directed flow network)
# In UndiGraph, start and end distinction is arbitrary
# However, in a DiGraph, start/end can indicate direction of travel
class Edge:
	def __init__(self, start, end, weight=0):
		self.start = start
		self.end = end
		self.W = weight

# Undirected Graph Object using Adjacency Matrix
class UndiGraph:
	def __init__(self, vertex_list, edge_list=None):
		# key:		dict {"ID": Index}, map Vertex IDs to Matrix index
		# size:		number of vertices
		# Matrix:	Adjacency matrix, plot of edge weights
		self.key = {name.ID: num for num, name in enumerate(vertex_list)}
		self.size = len(vertex_list)
		self.Matrix = [[0 for col in range(self.size)]for row in range(self.size)]

		if edge_list:
			self.add_edges(edge_list)

	# Takes an Edge, adds to Matrix
	# Add s->e to Matrix[key[s]][key[e]]
	# Add e->s to Matrix[key[e]][key[s]]
	def add_edge(self, e):
		# s->e
		self.Matrix[self.key[e.start]][self.key[e.end]] = e.W
		# e->s
		self.Matrix[self.key[e.end]][self.key[e.start]] = e.W

	# Takes list of Edges, adds each to Matrix
	def add_edges(self, edges):
		for edge in edges:
			self.add_edge(edge)

	# Check if something is a vertex in self.key
	def is_v(self, s):
		return s in self.key.keys()

	# Takes a number and gives the ID with corresponding index
	def get_ID(self, s):
		for k, v in self.key.items():
			if v == s:
				return k
		print("Could not find index...")

	def show_matrix(self):
		print('\n')

		# Column names
		for t in self.key.keys():
			print(f'\t{t}', end='')
		# Row
		for k, v in self.key.items():
			# Column
			print(f'\n{k}', end='\t')
			for k2, v2 in self.key.items():
				print(self.Matrix[v][v2], end='\t')

		print('\n')

	# Return edge between (start,end) in Matrix
	# Accepts either string of int
	def edge_at(self, s, e):
		# If string IDs, convert and return
		if isinstance(s, str) and isinstance(e, str):
			return self.Matrix[self.key[s]][self.key[e]]
		# Otherwise, if num, simply return
		return self.Matrix[s][e]

	# Breadth-first search from given source (int or str)
	# Depth=0, 1, 2, 3, until no edges left, next vertex
	# Unlike traverse(), we will be checking every cell in every row
	# Completeness counts, we want to explore all vertices and edges, even dups
	def bfs(self, source):
		# If source is str ID, convert to int
		if isinstance(source, str):
			if not self.is_v(source):
			 	print("No corresponding start point...")
			 	return
			root = self.key[source]
		# Otherwise, use int(source) as root
		elif isinstance(source, int):
			root = source

		# Data structures
		# Visited list with len() of matrix, initialize all to False
		visited = [False]*self.size
		# Depth queue
		q = deque()
		q.appendleft(root)
		# Visit source
		visited[root] = True
		# BFS path expansion
		while q:
			current = q.popleft()
			print(f'Current Node: {self.get_ID(current)}')

			# For every non-visited adj vertex to current
			for i in range(self.size):
				if ((self.edge_at(current, i)) and (not visited[i])):
					q.appendleft(i)
					visited[i] = True

	# Perform BFS on all vertices
	def bfs_all(self):
		c = 0
		for i in range(self.size):
			print(f'Search #{i+1}')
			self.bfs(i)

	# Query the Matrix
	# Since edges are two-way, we only need to check ((size^2)/2) cells
	# Also, we do not need to check a vertex against itself (ex.A->A)
	def traverse(self):
		counter = 0
		# Row
		for i in range(self.size):
			# Col
			# Ignore vertices before and including this one
			for j in range(i+1, self.size):
				# Get current cell value (index = [i][j])
				cell = self.edge_at(i, j)
				counter+=1

				# Do something if there's an edge
				if cell:
					print(f'Edge between {self.get_ID(i)} and {self.get_ID(j)}: {cell}')
				else:
					print(f'No edge between {self.get_ID(i)} and {self.get_ID(j)}')

		print(f'Number of cells in Matrix:\t{self.size**2}')
		print(f'Number of cells searched:\t{counter}')


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def binaryTreePaths(self, root: TreeNode) -> List[str]:
        # recursive dfs
        if not root:
            return []

        results = []

        def dfs(node, sub_path):
            nonlocal results
            if not node.left and not node.right:
                results.append(sub_path)

            if node.left:
                dfs(node.left, sub_path + [node.left])
            if node.right:
                dfs(node.right, sub_path + [node.right])

        dfs(root, [root])

        outputs = []
        for result in results:
            my_str = "->".join(str(x.val) for x in result)
            outputs.append(my_str)

        return outputs

"""
Results:
Runtime: 36 ms, faster than 38.50% of Python3 online submissions for Binary Tree Paths.
Memory Usage: 13.6 MB, less than 95.56% of Python3 online submissions for Binary Tree Paths.

"""

Round_1/257. Binary Tree Paths/solution_3.py


import json
from pathlib import Path
import os
import time

class CommonUtils:
    def readfromjson( key,filename):
        try:
            filename = filename + ".json"
            path = Path(__file__).parent.parent
            path =os.path.join(path,"Resources\\JSON")
            os.chdir(path)
            with open(filename,'r+')as f:
                jso = json.load(f)
            return jso[key]
        except FileNotFoundError:
            print(filename + " Not found")
            return None

    def Click(Element):
        Element.click()

    def isDisplayed(Element):
        try:
            if Element != None:
                Flag = Element.is_displayed()
                if Flag == True:
                    return True
                else:
                    return False
            else:
                return False
        except Exception as e:
            print(e.message)

    def wait():
        time.sleep(1)


import os
from unittest import TestCase
from unittest.mock import Mock

from malicious_file_crawler.src.utils.read_config import ConfigReader

from gw_crawler.malicious_file_crawler.src.spiders.glasswall_crawler import GlasswallScraper

class TestGlasswallCrawler(TestCase):
    def setUp(self):
        site = 'corvus'
        config = ConfigReader(site.upper()).read_config()
        self.cfg = config
        self.scrapper = GlasswallScraper(self.cfg)

    def test_start_requests(self):
        self.url = os.environ['login_url']
        response = self.scrapper.start_requests()
        self.assertIsNotNone(response)

    def test_download_files(self):
        content = self.scrapper.download_files(os.getenv('login_url'))
        self.assertIsNotNone(content)

    def test_navigate_to(self):
        url = os.environ['file_page_url']
        response = self.scrapper.navigate_to(Mock())
        self.assertIsNotNone(response)

class TestDasMalwerkScraper:

    def test_start_requests(self):
        pass


class Tool(object):
    #使用赋值语句定义类属性
    count=0   #count 就是类Tool特有的属性，就是类属性
    def __init__(self,name):

        self.name=name
        #通过 类名.count 访问类属性，并进行操作
        Tool.count+=1

tool_1=Tool("工具1")
tool_2=Tool("工具2")
#输出工具对象的总数
print(Tool.count)


import discord
import asyncio
import wikipedia
import random

import Board

class Game():
	"""Implements the game nPeopleAreLying"""
	def __init__(self, channel, players):

		self.Channel = channel
		self.Players = players
		self.PlayerTurn = 1
		self.GameStage = 0
		self.Board = Board.Board("./4 Categories Board.json", len(players)-1)
		# Game Stages:
		# 0 - Roll dice
		# 1 - Choose Direction
		# 2 - Choose category
		# 3 - answer question

		self.Board.StartGraphics()
		print("Game Started")

	async def on_message(self, message):
		"""Will handle messages for the game nPeopleAreLying"""

		command = message.content.split(" ")

		if self.GameStage == 0:     # Stage 0 - Roll dice
			if command[0] == "-roll" and self.Players[self.PlayerTurn] == message.author:
				self.steps = random.randint(1, 6)

				await self.Channel.send(f"You rolled a {self.steps}!")

				await self.move(self.steps)

		elif self.GameStage == 1:   # Stage 1 - Choose Direction
			if command[0] == "-go" and self.Players[self.PlayerTurn] == message.author:
				await self.move(self.steps, command[1])

		elif self.GameStage == 3:   # Stage 3 - answer question
			if command[0] == "-accept" and self.Players[0] == message.author:
				if self.Board.GiveCheese(self.PlayerTurn):	# won cheese
					await self.Channel.send(f"Great! You won a cheese.")

				self.GameStage = 0
				mention = self.Players[self.PlayerTurn].mention
				await self.Channel.send(f"Great! {mention} you get to go again. \nRoll by typing ``-roll`` in chat.")

			elif command[0] == "-reject" and self.Players[0] == message.author:
				self.GameStage = 0
				self.PlayerTurn += 1
				if self.PlayerTurn >= len(self.Players):
					self.PlayerTurn = 1

				mention = self.Players[self.PlayerTurn].mention
				await self.Channel.send(f"Too bad! {mention} it's your turn. \nRoll by typing ``-roll`` in chat.")

	async def move(self, steps, direction = -1):
		""" Moves the player """

		try:
			self.Board.MovePlayer(self.PlayerTurn, steps, int(direction))
			await self.get_question()

		except Board.DecisionNeeded as decision:
			position = self.Board.players[self.PlayerTurn]["Position"]
			self.GameStage = 1
			self.steps = decision.steps
			await self.Channel.send(f"You are in {position} choose one of the following directions: {decision.options}. \nChoose by typing ``-go [option]`` in chat.")

		except ValueError:
			await self.Channel.send(f"{direction} is not a number. Try again.")

	async def get_question(self):
		""" Gets a question for the player """

		try:
			await self.Channel.send(f"Your question: {self.Board.GetQuestionForPlayer(self.PlayerTurn)}")
			self.GameStage = 3
		except Board.QuestionError as e:
			self.Board.RenewQuestions(e.category)
			await self.Channel.send(f"All questions from {e.category} were used, will now be repeated.")
			await self.get_question()


from __future__ import (division, unicode_literals)

import sys
import unittest
import warnings

import numpy as np
import witness as wit
import witness.logic
import witness.refine

assert sys.version_info >= (2, 7)

# TOFIX: restructure tests code in a unittest-friendly fashion
class test_refine(unittest.TestCase):

    # run tests
    def test_refine(self):

        # test exception
        try:
            raise wit.refine.NoDataRefinedError
            self.fail('Unable to raise exception ?')
        except wit.refine.NoDataRefinedError:
            pass

        # construct a data refining backend
        class unvalue:
            v = 0

        x = unvalue()

        def xinc(state, parent):
            x.v += state + len(parent.mdata['some']) / 100.

        rd = wit.refine.data('paul', xinc, xinc, some='thing')
        self.assertTrue(rd.name == 'paul')
        self.assertTrue(rd.mdata['some'] == 'thing')
        self.assertTrue(rd._transform == rd._inverse == xinc)

        # check calls
        self.assertTrue(x.v == 0.)
        rd.transform(1)
        self.assertTrue(x.v == 1.05)

        rd.mdata['some'] = 'not'
        rd.inverse(3)
        self.assertTrue(x.v == 4.08)

        # check warning & repr
        re = wit.refine.data('more', None, None)
        with warnings.catch_warnings(record=True) as w:
            m = ('No inverse function provided while refining ' +
                 'data with "more" backend ' +
                 '(id: more{}).'.format(object.__repr__(re)))

            try:
                re.inverse(3)
                self.fail('No NoDataRefinedError exception raised ?')
            except wit.refine.NoDataRefinedError:
                pass
            self.assertTrue(m in str(w[0].message).replace('\n', ' '))

        # test the textual labels backend
        lc = wit.backends.labels('again', 21)
        self.assertTrue('again' + object.__repr__(lc) == repr(lc))
        self.assertTrue(repr(lc.source) == 'label<again>()')
        self.assertTrue(lc.size == 21)

        # add various labels to the collection
        lc.add(label='false', value=False)
        lc.add(dict(label='true', value=True))
        lc.add([
            dict(label='always', value=None),
            dict(label='even', value=True),
            dict(label='ever', value=[True, False, False]),
            dict(label='still', value=np.bool_(False)),
            dict(label='there', value=[np.bool_(True),
                                       np.bool_(False)]),
            dict(label='more', value=wit.logic.tbsl.true(4)),
            dict(label='again', value=wit.logic.ebsl.uniform(7)),
            dict(label='exotic', where=slice(3, None, 3)),
        ])

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


from colorthief import ColorThief
import webcolors

color_thief = ColorThief('a.jpg')
# get the dominant color
dominant_color = color_thief.get_color(quality=1)
# build a color palette
palette = color_thief.get_palette(color_count=6)

print(webcolors.rgb_to_hex(dominant_color))
palette = [webcolors.rgb_to_hex(color) for color in palette]
print(palette)

import math
def distance(color1, color2):
    return math.sqrt(sum([(e1-e2)**2 for e1, e2 in zip(color1, color2)]))

def best_match(sample, colors):
    by_distance = sorted(colors, key=lambda c: distance(c, sample))
    return by_distance[0:5]

colors = [

(0, 0, 0),
(0, 0, 170),
(0, 170, 0),
(0, 170, 170),
(170, 0, 0),
(170, 0, 170),
(255, 170, 0),
(170, 170, 170),
(85, 85, 85),
(85, 85, 255),
(85, 255, 85),
(85, 255, 255),
(255, 85, 85),
(255, 85, 255),
(255, 255, 85),
(255, 255, 255)

]

result = best_match((60,43,44), colors)
palette = [webcolors.rgb_to_hex(color) for color in result]
print(palette)

from flask_sqlalchemy import SQLAlchemy


# !/usr/bin/python

#
# IMPORTS
#
import blivet  # For debug only
import logging  # For debug only
from partition_util import *
from snack import *
from partition import *
from partitioner import *
from blivet.devicefactory import SIZE_POLICY_AUTO
from blivet.devicefactory import SIZE_POLICY_MAX
from msg_box import *

#
# CONSTANTS
#
from viewer.__data__ import PART_TITLE_VG
from viewer.__data__ import PART_LABEL_VGDESC
from viewer.__data__ import PART_LABEL_POLICY
from viewer.__data__ import PART_LABEL_NEWVG
from viewer.__data__ import PART_LABEL_NAME
from viewer.__data__ import PART_LABEL_TOTAL
from viewer.__data__ import PART_LABEL_FREE
from viewer.__data__ import PART_BUTTON_DEL
from viewer.__data__ import PART_BUTTON_OK
from viewer.__data__ import PART_BUTTON_BACK
from viewer.__data__ import PART_FORM_RESET

from viewer.__data__ import PART_POLICY_AUTO
from viewer.__data__ import PART_POLICY_MAX
from viewer.__data__ import PART_POLICY_FIXED

from viewer.__data__ import PART_TITLE_PARM_ERR
from viewer.__data__ import PART_ERR_VG_EMPTY_NAME
from viewer.__data__ import PART_ERR_VG_EXIST
from viewer.__data__ import PART_ERR_VG_NO_DISK
from viewer.__data__ import PART_ERR_VG_NO_SIZE
# Dummy constant for consistent
SIZE_POLICY_FIXED = 1
#
# CODE
#

class SelectVg:
    """
    Represents the disk selelction screen
    """
    def __init__(self, screen, partitioner, disks, partition):
        # 1. Store parameters
        self._screen = screen
        self._partitioner = partitioner
        self._disks = disks
        self._partition = partition

        # 2. Build form components
        #   VG List
        self._vgInfo = TextboxReflowed(35, PART_LABEL_VGDESC.localize())
        self._vgList = Listbox(1, width=15)
        for vg in self._partitioner.vgs:    # vgs from property of blivet instance
            if self._partitioner.vgInDisks(vg, self._disks):
                self._vgList.append(vg.name, vg)
        self._vgList.append(PART_LABEL_NEWVG.localize(), None)
        if (self._partition is not None) and \
           (self._partition.device is not None) and \
           (type(self._partition.device) == LVMLogicalVolumeDevice):
            self._vgList.setCurrent(self._partition.device.vg)
        else:
            self._vgList.setCurrent(None)

        vg = self._vgList.current()
        self._diskTree = CheckboxTree(5, scroll=1, width=30)
        #   Name grid includes:
        self._nameGrid = Grid(2, 1)
        self._nameLabel = Label(PART_LABEL_NAME.localize())
        self._name = Entry(10, vg.name if vg is not None else "")
        self._nameGrid.setField(self._nameLabel, 0, 0, anchorLeft=1)
        self._nameGrid.setField(self._name, 1, 0, anchorLeft=1)

        #   Disk Tree
        self._diskTree = CheckboxTree(5, scroll=1, width=35)
        freeSpaces = self._partitioner.getFreeSpace(self._disks)
        for disk in self._disks:
            if vg is None:
                self._diskTree.addItem(
                    "%s    %-9s  %.2fMB" %
                    (disk.name, disk.size,
                     (float)(freeSpaces[disk.name][0].convertTo("m"))),
                    (snackArgs['append'], ),
                    disk, 0)
            else:
                if self._partitioner.vgInDisk(vg, disk):
                    self._diskTree.addItem(
                        "%s    %-9s  %.2fMB" %
                        (disk.name, disk.size,
                         (float)(freeSpaces[disk.name][0].convertTo("m"))),
                        (snackArgs['append'], ),
                        disk, 1)
                else:
                    self._diskTree.addItem(
                        "%s    %-9s  %.2fMB" %
                        (disk.name, disk.size,
                         (float)(freeSpaces[disk.name][0].convertTo("m"))),
                        (snackArgs['append'], ),
                        disk, 0)
        #   Info grid includes:
        self._infoGrid = Grid(4, 1)
        self._totalLabel = Label(PART_LABEL_TOTAL.localize())
        self._total = Textbox(10, 1,
                              "%dMB" % vg.size if vg is not None else "",
                              wrap=1)
        self._freeLabel = Label(PART_LABEL_FREE.localize())
        self._free = Textbox(10, 1,
                             "%dMB" % max(0, vg.freeSpace) if vg is not None else "",
                             wrap=1)
        self._infoGrid.setField(self._totalLabel, 0, 0)
        self._infoGrid.setField(self._total, 1, 0)
        self._infoGrid.setField(self._freeLabel, 2, 0)
        self._infoGrid.setField(self._free, 3, 0)

        #   Size grid includes:
        self._sizeGrid = Grid(3, 1)
        self._policyLabel = Label(PART_LABEL_POLICY.localize())
        self._policyList = Listbox(1, scroll=1, width=10)
        self._policyList.append(PART_POLICY_AUTO.localize(), SIZE_POLICY_AUTO)
        self._policyList.append(PART_POLICY_MAX.localize(), SIZE_POLICY_MAX)
        self._policyList.append(PART_POLICY_FIXED.localize(),
                                SIZE_POLICY_FIXED)

        self._size = Entry(10, "")

        if vg is None:
            self._policyList.setCurrent(SIZE_POLICY_AUTO)
        else:
            if vg in self._partitioner.newVgs.keys():
                if self._partitioner.newVgs[vg] > 0:
                    self._policyList.setCurrent(SIZE_POLICY_FIXED)
                    self._size.set(("%d " + SIZE_UNITS_IN_FACTORY) % self._partitioner.newVgs[vg])
                else:
                    self._policyList.setCurrent(self._partitioner.newVgs[vg])
            else:
                self._policyList.setCurrent(SIZE_POLICY_AUTO)
        # Set the states of the size entry
        if (self._policyList.current() == SIZE_POLICY_AUTO) or \
           (self._policyList.current() == SIZE_POLICY_MAX):
            self._size.setFlags(FLAG_DISABLED, FLAGS_SET)
        else:
            self._size.setFlags(FLAG_DISABLED, FLAGS_RESET)
        self._sizeGrid.setField(self._policyLabel, 0, 0)
        self._sizeGrid.setField(self._policyList, 1, 0)
        self._sizeGrid.setField(self._size, 2, 0)

        #   Buttons
        self._buttonBar = ButtonBar(self._screen,
                                    [(PART_BUTTON_DEL.localize(),
                                      PART_BUTTON_DEL.localize()),
                                     (PART_BUTTON_OK.localize(),
                                      PART_BUTTON_OK.localize()),
                                     (PART_BUTTON_BACK.localize(),
                                      PART_BUTTON_BACK.localize())])

        # 3. Build form
        self._form = GridForm(self._screen, PART_TITLE_VG.localize(), 1, 7)
        self._form.add(self._vgInfo, 0, 0, anchorLeft=1)
        self._form.add(self._vgList, 0, 1, anchorLeft=1)
        self._form.add(self._nameGrid, 0, 2, anchorLeft=1)
        self._form.add(self._diskTree, 0, 3, anchorLeft=1)
        self._form.add(self._infoGrid, 0, 4, anchorLeft=1)
        self._form.add(self._sizeGrid, 0, 5, anchorLeft=1,
                       padding=(0, 1, 0, 0))
        self._form.add(self._buttonBar, 0, 6, anchorLeft=1)

    def run(self):
        self._form.setCurrent(self._vgList)
        # Set callback function
        self._vgList.setCallback(self.on_vg_selected)
        self._policyList.setCallback(self.on_policy_selected)
        result = self._form.run()
        self._screen.popWindow()
        # Parse result
        rc = self._buttonBar.buttonPressed(result)
        if rc == PART_BUTTON_OK.localize():
            # Some parameters are not valid, re-enter VG select form
            if not self.validateParms():
                return PART_FORM_RESET.localize()
        return rc

    def on_vg_selected(self):
        vg = self._vgList.current()
        self._name.set("" if vg is None else vg.name)
        # Clear all selections
        for sel in self._diskTree.getSelection():
            self._diskTree.setEntryValue(sel, 0)
        for disk in self._disks:
            if vg is None:
                self._diskTree.setEntryValue(disk, 0)
            # Only the disks which vg belongs to will be marked selected
            elif self._partitioner.vgInDisk(vg, disk):
                self._diskTree.setEntryValue(disk, 1)
            else:
                self._diskTree.setEntryValue(disk, 0)
        # New VG
        if vg is None:
            self._policyList.setCurrent(SIZE_POLICY_AUTO)
            self._size.set("")
        elif vg in self._partitioner.newVgs.keys():
            if self._partitioner.newVgs[vg] > 0:
                self._policyList.setCurrent(SIZE_POLICY_FIXED)
                self._size.set(("%d " + SIZE_UNITS_IN_FACTORY) % self._partitioner.newVgs[vg])
            else:
                self._policyList.setCurrent(self._partitioner.newVgs[vg])
                self._size.set("")
        else:
            self._policyList.setCurrent(SIZE_POLICY_AUTO)
        self._total.setText("%dMB" % vg.size if vg is not None else "     ")
        self._free.setText("%dMB" % max(0, vg.freeSpace) if
                           vg is not None else "     ")

    def on_policy_selected(self):
        vg = self._vgList.current()
        '''
        if vg is not None:
            if vg in self._partitioner.newVgs.keys():
                oldPolicy = self._partitioner.newVgs[vg]
                if oldPolicy > 0:
                    self._policyList.setCurrent(SIZE_POLICY_FIXED)
                    self._size.set("%d"%oldPolicy)
                else:
                    self._policyList.setCurrent(oldPolicy)
                    '''
        policy = self._policyList.current()
        if (policy == SIZE_POLICY_AUTO) or (policy == SIZE_POLICY_MAX):
            self._size.set("")
            self._size.setFlags(FLAG_DISABLED, FLAGS_SET)
        else:
            self._size.setFlags(FLAG_DISABLED, FLAGS_RESET)
            if vg in self._partitioner.newVgs.keys():
                self._size.set(("%d " + SIZE_UNITS_IN_FACTORY) % self._partitioner.newVgs[vg])
            else:
                self._size.set("")

    def validateParms(self):
        name = self._name.value()
        vg = self._vgList.current()
        # For new VG,
        #   1. You must give a valid vg name.
        if vg is None:
            if len(name.strip()) == 0:
                MsgBox(self._screen, PART_TITLE_PARM_ERR.localize(),
                       PART_ERR_VG_EMPTY_NAME.localize())
                return False
        #   2. New vg is alreay exist.
            existVgsNames = []
            for vg in self._partitioner.vgs:
                if self._partitioner.vgInDisks(vg, self._disks):
                    existVgsNames.append(vg.name)
            if name in existVgsNames:
                MsgBox(self._screen, PART_TITLE_PARM_ERR.localize(),
                       PART_ERR_VG_EXIST.localize() % name)
                return False
        # For both new and exist VG,
        #   1. You must select at least one disk
        selDisks = self._diskTree.getSelection()
        if len(selDisks) == 0:
            MsgBox(self._screen, PART_TITLE_PARM_ERR.localize(),
                   PART_ERR_VG_NO_DISK.localize())
            return False
        #   2. You must give a valid size for policy "Fixed"
        policy = self._policyList.current()
        size = self._size.value().strip()
        if policy == SIZE_POLICY_FIXED and strToSize(size) is None:
            MsgBox(self._screen, PART_TITLE_PARM_ERR.localize(),
                   PART_ERR_VG_NO_SIZE.localize())
            return False
        return True

    @property
    def disks(self):
        return self._diskTree.getSelection()

    @property
    def vg(self):
        return self._vgList.current()

    @property
    def name(self):
        return self._name.value()

    @property
    def size(self):
        '''size is assigned to value of map partitioner.newVgs with number type

        '''
        if self._policyList.current() > 0:  # policy value of > 0 is a fixed size request
            return int(strToSize(self._size.value()).convertTo(spec=SIZE_UNITS_IN_FACTORY))
        else:
            return self._policyList.current()


import data
import T_list_astr

URL = "https://api.nasa.gov/neo/rest/v1/feed"

def run():
    request = data.requests.get(URL, data.params)
    count = request.headers['X-RateLimit-Remaining']
    data.count_req = count
    text = data.json.loads(request.text)
    list_astr = text['near_earth_objects']['2020-08-18']
    l_astr = ""
    for i in range(len(list_astr)):
        l = list_astr[i]
        l_astr+= str(l['name']) + "  "   + str(l['estimated_diameter']['kilometers']['estimated_diameter_max']) + "\n"
    t = T_list_astr.show_astr()
    t.show(l_astr)


from django.contrib import admin
from .models import IssueTracker, Status, Type, Project

class IssueAdmin(admin.ModelAdmin):
    filter_horizontal = ('type',)
    list_filter = ('status',)
    list_display = ('pk', 'summary',)
    list_display_links = ('pk', 'summary')
    search_fields = ('type',)

class ProjectAdmin(admin.ModelAdmin):
    list_display = ('pk', 'is_deleted', 'name',)
    list_display_links = ('pk', 'name')
    search_fields = ('name',)

admin.site.register(IssueTracker, IssueAdmin)
admin.site.register(Status)
admin.site.register(Type)
admin.site.register(Project, ProjectAdmin)


#=========================================================================================
# Licence, Reference and Credits
#=========================================================================================
__copyright__ = "Copyright (C) CCPN project (http://www.ccpn.ac.uk) 2014 - 2019"
__credits__ = ("Ed Brooksbank, Luca Mureddu, Timothy J Ragan & Geerten W Vuister")
__licence__ = ("CCPN licence. See http://www.ccpn.ac.uk/v3-software/downloads/license")
__reference__ = ("Skinner, S.P., Fogh, R.H., Boucher, W., Ragan, T.J., Mureddu, L.G., & Vuister, G.W.",
                 "CcpNmr AnalysisAssign: a flexible platform for integrated NMR analysis",
                 "J.Biomol.Nmr (2016), 66, 111-124, http://doi.org/10.1007/s10858-016-0060-y")
#=========================================================================================
# Last code modification
#=========================================================================================
__modifiedBy__ = "$modifiedBy: Luca Mureddu $"
__dateModified__ = "$dateModified: 2017-07-07 16:32:26 +0100 (Fri, July 07, 2017) $"
__version__ = "$Revision: 3.0.0 $"
#=========================================================================================
# Created
#=========================================================================================
__author__ = "$Author: Luca Mureddu $"
__date__ = "$Date: 2017-05-28 10:28:42 +0000 (Sun, May 28, 2017) $"
#=========================================================================================
# Start of code
#=========================================================================================

#### GUI IMPORTS
from ccpn.ui.gui.widgets.PipelineWidgets import GuiPipe, _getWidgetByAtt
from ccpn.ui.gui.widgets.PulldownList import PulldownList
from ccpn.ui.gui.widgets.Label import Label
from ccpn.AnalysisScreen.gui.widgets import HitFinderWidgets as hw

#### NON GUI IMPORTS
import numpy as np
from ccpn.framework.lib.Pipe import SpectraPipe
from ccpn.AnalysisScreen.lib.experimentAnalysis import WaterLogsy as wl
from ccpn.AnalysisScreen.lib.experimentAnalysis.NewHit import _addNewHit, _getReferencesFromSample
from ccpn.AnalysisScreen.lib.experimentAnalysis.MatchPositions import matchHitToReference

########################################################################################################################
###   Attributes:
###   Used in setting the dictionary keys on _kwargs either in GuiPipe and Pipe
########################################################################################################################

## Widget variables and/or _kwargs keys
ReferenceSpectrumGroup = 'Reference_SpectrumGroup'
SCasRef = 'Use_SampleComponents_as_References'
TargetSpectrumGroup = 'WL_Target_SpectrumGroup'
ControlSpectrumGroup = 'WL_Control_SpectrumGroup'
SGVarNames = [ControlSpectrumGroup, TargetSpectrumGroup, ReferenceSpectrumGroup]

MatchPeaksWithin = 'Match_Peaks_Within_(ppm)'
RefPLIndex = 'Reference_PeakList'
TargetPeakListIndex = 'Target_PeakList'
MinEfficiency = 'Minimal_Intensity_Change'
ModeHit = 'Finding_Mode'

## defaults
DefaultEfficiency = 10
DefaultPeakListIndex = -1
DefaultMinDist = 0.01

## PipeName
PipeName = 'WaterLogsy Hits'

########################################################################################################################
##########################################      ALGORITHM       ########################################################
########################################################################################################################

## See AnalysisScreen Lib

########################################################################################################################
##########################################     GUI PIPE    #############################################################
########################################################################################################################

class WaterLogsyHitFinderGuiPipe(GuiPipe):
    preferredPipe = False
    applicationSpecificPipe = True
    pipeName = PipeName

    def __init__(self, name=pipeName, parent=None, project=None, **kwds):
        super(WaterLogsyHitFinderGuiPipe, self)
        GuiPipe.__init__(self, parent=parent, name=name, project=project, **kwds)
        self._parent = parent

        row = 0
        self.modeLabel = Label(self.pipeFrame, ModeHit, grid=(row, 0))
        setattr(self, ModeHit, PulldownList(self.pipeFrame, texts=wl.MODES, callback=self._modeCallback, grid=(row, 1)))

        row += 1
        hw._addSGpulldowns(self, row, SGVarNames)

        row += len(SGVarNames)
        hw._addCommonHitFinderWidgets(self, row, ReferenceSpectrumGroup, SCasRef, MatchPeaksWithin,
                                      DefaultMinDist, MinEfficiency, DefaultEfficiency)

        self._updateWidgets()

    def _updateWidgets(self):
        self._setSpectrumGroupPullDowns(SGVarNames)

    def _modeCallback(self, selected):
        'manages the spectrumgroups pullDown'
        if selected == wl.PositiveOnly:
            _getWidgetByAtt(self, ControlSpectrumGroup).setEnabled(False)
            _getWidgetByAtt(self, ControlSpectrumGroup)._clear()
        elif selected == wl.IntensityChanged or selected == wl.SignChanged:
            _getWidgetByAtt(self, ControlSpectrumGroup).setEnabled(True)

########################################################################################################################
##########################################       PIPE      #############################################################
########################################################################################################################

class WaterLogsyHitFinderPipe(SpectraPipe):
    guiPipe = WaterLogsyHitFinderGuiPipe
    pipeName = PipeName

    _kwargs = {
        ReferenceSpectrumGroup: 'spectrumGroup.pid',
        TargetSpectrumGroup: 'spectrumGroup.pid',
        ControlSpectrumGroup: 'spectrumGroup.pid',
        SCasRef: False,
        MatchPeaksWithin: DefaultMinDist,
        MinEfficiency: MinEfficiency,
        }

    def runPipe(self, spectra):
        '''
        :param spectra: inputData
        :return: spectra. Add Hits to project
        '''
        referenceSpectrumGroup = self._getSpectrumGroup(self._kwargs[ReferenceSpectrumGroup])
        wLcontrolSpectrumGroup = self._getSpectrumGroup(self._kwargs[ControlSpectrumGroup])
        wLtargetSpectrumGroup = self._getSpectrumGroup(self._kwargs[TargetSpectrumGroup])
        minimumDistance = float(self._kwargs[MatchPeaksWithin])
        references = []
        if wLtargetSpectrumGroup is not None:
            targetSpectra = wLtargetSpectrumGroup.spectra
            if wLcontrolSpectrumGroup is None:  # if no control is given. Find hits just by positive peaks in the target spectrum
                controlSpectra = [None] * len(targetSpectra)
                mode = wl.PositiveOnly
            else:
                controlSpectra = wLcontrolSpectrumGroup.spectra
                mode = self._kwargs[ModeHit]

            if len(controlSpectra) == len(targetSpectra):
                for targetSpectrum, controlSpectrum in zip(targetSpectra, controlSpectra):
                    if targetSpectrum.experimentType is None:
                        targetSpectrum.experimentType = 'Water-LOGSY.H'
                    if self._kwargs[SCasRef]:  # sampleComponents as References
                        references = _getReferencesFromSample(targetSpectrum)
                    else:
                        if referenceSpectrumGroup is not None:
                            references = referenceSpectrumGroup.spectra
                    hits = wl.findWaterLogsyHits(wLTarget=targetSpectrum, wLControl=controlSpectrum,
                                                 mode=mode, limitRange=minimumDistance, limitIntensityChange=float(self._kwargs[MinEfficiency]))
                    filtered = self._filterNewHits(hits, targetSpectrum, references, minimumDistance)

                    # quick hack for demo on EM Symposium
                    if filtered:
                        if len(filtered) > 0:
                            from ccpn.AnalysisScreen.gui.modules.HitAnalyis import HitsAnalysis

                            hitModule = HitsAnalysis(self.mainWindow)
                            self.mainWindow.moduleArea.addModule(hitModule)

        SGSpectra = [sp for sg in self.spectrumGroups if sg is not None for sp in sg.spectra]
        return set(list(spectra) + SGSpectra)

    def _filterNewHits(self, hits, targetSpectrum, references, minimumDistance):
        if hits:
            if len(targetSpectrum.peakLists) > 0:
                matchedRef = matchHitToReference(targetSpectrum, references, limitRange=minimumDistance,
                                                 refPeakListIndex=DefaultPeakListIndex)
                matchedRef = [i for hit in matchedRef for i in hit]  # clean up the empty sublists
                matchedHit = []
                for i in matchedRef:
                    if len(i) == 3:
                        rp, tp, pos_i = i
                        for j in hits:
                            if len(j) == 3:
                                rp, tp, pos_j = j
                                if type(pos_i) is np.ndarray:
                                    pos_i = pos_i.ravel()
                                    if len(pos_i) > 0:
                                        pos_i = pos_i[-1]

                                if float(pos_j) == float(pos_i):
                                    matchedHit.append(i)
                if len(matchedHit) > 0:
                    _addNewHit(targetSpectrum, matchedHit)
                    return matchedHit

WaterLogsyHitFinderPipe.register()  # Registers the pipe in the pipeline


import copy

from qtpy.QtGui import QDoubleValidator
from qtpy.QtWidgets import QDialog, QDialogButtonBox, QGridLayout, QGroupBox, QLabel, QPushButton, QVBoxLayout

from .useful_widgets import LineEditExtended, set_tooltip

class DialogFindElements(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        self._dialog_data = {}

        self.setWindowTitle("Find Elements in Sample")

        # Check this flag after the dialog is exited with 'True' value
        #   If this flag is True, then run element search, if False,
        #   then simply save the changed parameter values.
        self.find_elements_requested = False

        self.validator = QDoubleValidator()

        self.le_e_calib_a0 = LineEditExtended()
        self.le_e_calib_a0.setValidator(self.validator)
        self.pb_e_calib_a0_default = QPushButton("Default")
        self.pb_e_calib_a0_default.setAutoDefault(False)

        self.le_e_calib_a1 = LineEditExtended()
        self.le_e_calib_a1.setValidator(self.validator)
        self.pb_e_calib_a1_default = QPushButton("Default")
        self.pb_e_calib_a1_default.setAutoDefault(False)

        self.le_e_calib_a2 = LineEditExtended()
        self.le_e_calib_a2.setValidator(self.validator)
        self.pb_e_calib_a2_default = QPushButton("Default")
        self.pb_e_calib_a2_default.setAutoDefault(False)

        self.group_energy_axis_calib = QGroupBox("Polynomial Approximation of Energy Axis")
        set_tooltip(
            self.group_energy_axis_calib,
            "Parameters of polynomial approximation of energy axis. "
            "The values of the bins for photon energies are approximated "
            "using 2nd degree polynomial E(n) = a0 + a1 * n + a2 * n^2, "
            "where n is bin number (typically in the range 0..4096).",
        )
        grid = QGridLayout()
        grid.addWidget(QLabel("Bias (a0):"), 0, 0)
        grid.addWidget(self.le_e_calib_a0, 0, 1)
        grid.addWidget(self.pb_e_calib_a0_default, 0, 2)
        grid.addWidget(QLabel("Linear (a1):"), 1, 0)
        grid.addWidget(self.le_e_calib_a1, 1, 1)
        grid.addWidget(self.pb_e_calib_a1_default, 1, 2)
        grid.addWidget(QLabel("Quadratic (a2):"), 2, 0)
        grid.addWidget(self.le_e_calib_a2, 2, 1)
        grid.addWidget(self.pb_e_calib_a2_default, 2, 2)
        self.group_energy_axis_calib.setLayout(grid)

        self.le_fwhm_b1 = LineEditExtended()
        self.le_fwhm_b1.setValidator(self.validator)
        self.pb_fwhm_b1_default = QPushButton("Default")
        self.pb_fwhm_b1_default.setAutoDefault(False)

        self.le_fwhm_b2 = LineEditExtended()
        self.le_fwhm_b2.setValidator(self.validator)
        self.pb_fwhm_b2_default = QPushButton("Default")
        self.pb_fwhm_b2_default.setAutoDefault(False)

        self.group_fwhm = QGroupBox("Peak FWHM Settings")
        set_tooltip(
            self.group_fwhm,
            "Parameters used to estimate FWHM of peaks based on energy: "
            "b1 - 'offset', b2 - 'fanoprime'",
        )
        grid = QGridLayout()
        grid.addWidget(QLabel("Coefficient b1:"), 0, 0)
        grid.addWidget(self.le_fwhm_b1, 0, 1)
        grid.addWidget(self.pb_fwhm_b1_default, 0, 2)
        grid.addWidget(QLabel("Coefficient b2:"), 1, 0)
        grid.addWidget(self.le_fwhm_b2, 1, 1)
        grid.addWidget(self.pb_fwhm_b2_default, 1, 2)
        self.group_fwhm.setLayout(grid)

        self.le_incident_energy = LineEditExtended()
        self.le_incident_energy.setValidator(self.validator)
        set_tooltip(self.le_incident_energy, "Incident energy in keV.")
        self.pb_incident_energy_default = QPushButton("Default")
        self.pb_incident_energy_default.setAutoDefault(False)
        self.le_range_low = LineEditExtended()
        self.le_range_low.setValidator(self.validator)
        set_tooltip(self.le_range_low, "Lower boundary of the selected range in keV.")
        self.pb_range_low_default = QPushButton("Default")
        self.pb_range_low_default.setAutoDefault(False)
        self.le_range_high = LineEditExtended()
        self.le_range_high.setValidator(self.validator)
        set_tooltip(self.le_range_high, "Upper boundary of the selected range in keV.")
        self.pb_range_high_default = QPushButton("Default")
        self.pb_range_high_default.setAutoDefault(False)
        self.group_energy_range = QGroupBox("Incident Energy and Selected Range")
        grid = QGridLayout()
        grid.addWidget(QLabel("Incident energy, keV"), 0, 0)
        grid.addWidget(self.le_incident_energy, 0, 1)
        grid.addWidget(self.pb_incident_energy_default, 0, 2)
        grid.addWidget(QLabel("Range (low), keV"), 1, 0)
        grid.addWidget(self.le_range_low, 1, 1)
        grid.addWidget(self.pb_range_low_default, 1, 2)
        grid.addWidget(QLabel("Range (high), keV"), 2, 0)
        grid.addWidget(self.le_range_high, 2, 1)
        grid.addWidget(self.pb_range_high_default, 2, 2)
        self.group_energy_range.setLayout(grid)

        self.pb_find_elements = QPushButton("Find &Elements")
        self.pb_find_elements.clicked.connect(self.pb_find_elements_clicked)
        self.pb_apply_settings = QPushButton("&Apply Settings")

        # 'Close' button box
        button_box = QDialogButtonBox(QDialogButtonBox.Cancel)
        button_box.addButton(self.pb_find_elements, QDialogButtonBox.YesRole)
        button_box.addButton(self.pb_apply_settings, QDialogButtonBox.AcceptRole)
        button_box.button(QDialogButtonBox.Cancel).setDefault(True)
        button_box.accepted.connect(self.accept)
        button_box.rejected.connect(self.reject)

        self.pb_cancel = button_box.button(QDialogButtonBox.Cancel)
        self.pb_cancel.setAutoDefault(False)
        self.pb_apply_settings.setAutoDefault(True)

        vbox = QVBoxLayout()
        vbox.addWidget(self.group_energy_axis_calib)
        vbox.addWidget(self.group_fwhm)
        vbox.addWidget(self.group_energy_range)
        vbox.addWidget(button_box)

        self.setLayout(vbox)

        self.le_e_calib_a0.editingFinished.connect(self.le_e_calib_a0_editing_finished)
        self.le_e_calib_a1.editingFinished.connect(self.le_e_calib_a1_editing_finished)
        self.le_e_calib_a2.editingFinished.connect(self.le_e_calib_a2_editing_finished)
        self.le_fwhm_b1.editingFinished.connect(self.le_fwhm_b1_editing_finished)
        self.le_fwhm_b2.editingFinished.connect(self.le_fwhm_b2_editing_finished)
        self.le_incident_energy.editingFinished.connect(self.le_incident_energy_editing_finished)
        self.le_range_low.editingFinished.connect(self.le_range_low_editing_finished)
        self.le_range_high.editingFinished.connect(self.le_range_high_editing_finished)

        self.le_e_calib_a0.focusOut.connect(self.le_e_calib_a0_focus_out)
        self.le_e_calib_a1.focusOut.connect(self.le_e_calib_a1_focus_out)
        self.le_e_calib_a2.focusOut.connect(self.le_e_calib_a2_focus_out)
        self.le_fwhm_b1.focusOut.connect(self.le_fwhm_b1_focus_out)
        self.le_fwhm_b2.focusOut.connect(self.le_fwhm_b2_focus_out)
        self.le_incident_energy.focusOut.connect(self.le_incident_energy_focus_out)
        self.le_range_low.focusOut.connect(self.le_range_low_focus_out)
        self.le_range_high.focusOut.connect(self.le_range_high_focus_out)

        self.le_e_calib_a0.textChanged.connect(self.le_e_calib_a0_text_changed)
        self.le_e_calib_a1.textChanged.connect(self.le_e_calib_a1_text_changed)
        self.le_e_calib_a2.textChanged.connect(self.le_e_calib_a2_text_changed)
        self.le_fwhm_b1.textChanged.connect(self.le_fwhm_b1_text_changed)
        self.le_fwhm_b2.textChanged.connect(self.le_fwhm_b2_text_changed)
        self.le_incident_energy.textChanged.connect(self.le_incident_energy_text_changed)
        self.le_range_low.textChanged.connect(self.le_range_low_text_changed)
        self.le_range_high.textChanged.connect(self.le_range_high_text_changed)

        self.pb_e_calib_a0_default.clicked.connect(self.pb_e_calib_a0_default_clicked)
        self.pb_e_calib_a1_default.clicked.connect(self.pb_e_calib_a1_default_clicked)
        self.pb_e_calib_a2_default.clicked.connect(self.pb_e_calib_a2_default_clicked)
        self.pb_fwhm_b1_default.clicked.connect(self.pb_fwhm_b1_default_clicked)
        self.pb_fwhm_b2_default.clicked.connect(self.pb_fwhm_b2_default_clicked)
        self.pb_incident_energy_default.clicked.connect(self.pb_incident_energy_default_clicked)
        self.pb_range_low_default.clicked.connect(self.pb_range_low_default_clicked)
        self.pb_range_high_default.clicked.connect(self.pb_range_high_default_clicked)

    def _format_float(self, value):
        return f"{value:.10g}"

    def set_dialog_data(self, dialog_data):
        self._dialog_data = copy.deepcopy(dialog_data)
        self.le_e_calib_a0.setText(self._format_float(self._dialog_data["e_offset"]["value"]))
        self.le_e_calib_a1.setText(self._format_float(self._dialog_data["e_linear"]["value"]))
        self.le_e_calib_a2.setText(self._format_float(self._dialog_data["e_quadratic"]["value"]))
        self.le_fwhm_b1.setText(self._format_float(self._dialog_data["fwhm_offset"]["value"]))
        self.le_fwhm_b2.setText(self._format_float(self._dialog_data["fwhm_fanoprime"]["value"]))
        self.le_incident_energy.setText(self._format_float(self._dialog_data["coherent_sct_energy"]["value"]))
        self.le_range_low.setText(self._format_float(self._dialog_data["energy_bound_low"]["value"]))
        self.le_range_high.setText(self._format_float(self._dialog_data["energy_bound_high"]["value"]))

    def get_dialog_data(self):
        return self._dialog_data

    def pb_find_elements_clicked(self):
        self.find_elements_requested = True

    def _read_le_value(self, line_edit, value_ref):
        value_ref["value"] = float(line_edit.text())

    def le_e_calib_a0_editing_finished(self):
        self._read_le_value(self.le_e_calib_a0, self._dialog_data["e_offset"])

    def le_e_calib_a1_editing_finished(self):
        self._read_le_value(self.le_e_calib_a1, self._dialog_data["e_linear"])

    def le_e_calib_a2_editing_finished(self):
        self._read_le_value(self.le_e_calib_a2, self._dialog_data["e_quadratic"])

    def le_fwhm_b1_editing_finished(self):
        self._read_le_value(self.le_fwhm_b1, self._dialog_data["fwhm_offset"])

    def le_fwhm_b2_editing_finished(self):
        self._read_le_value(self.le_fwhm_b2, self._dialog_data["fwhm_fanoprime"])

    def le_incident_energy_editing_finished(self):
        self._read_le_value(self.le_incident_energy, self._dialog_data["coherent_sct_energy"])
        self._read_le_value(self.le_range_high, self._dialog_data["energy_bound_high"])

    def le_range_low_editing_finished(self):
        self._read_le_value(self.le_range_low, self._dialog_data["energy_bound_low"])

    def le_range_high_editing_finished(self):
        self._read_le_value(self.le_range_high, self._dialog_data["energy_bound_high"])

    def _validate_as_float(self, text):
        return self.validator.validate(text, 0)[0] == QDoubleValidator.Acceptable

    def _validate_text(self, line_edit, text):
        line_edit.setValid(self._validate_as_float(text))
        self._update_exit_buttons_states()

    def _update_exit_buttons_states(self):
        if (
            self.le_e_calib_a0.isValid()
            and self.le_e_calib_a1.isValid()
            and self.le_e_calib_a2.isValid()
            and self.le_fwhm_b1.isValid()
            and self.le_fwhm_b2.isValid()
            and self.le_incident_energy.isValid()
            and self.le_range_low.isValid()
            and self.le_range_high.isValid()
        ):
            all_valid = True
        else:
            all_valid = False

        self.pb_find_elements.setEnabled(all_valid)
        self.pb_apply_settings.setEnabled(all_valid)

    def _range_high_update(self, text, margin=0.8):
        """Update the range 'high' limit based on incident energy"""
        v = float(text) + margin
        self.le_range_high.setText(self._format_float(v))

    def le_e_calib_a0_text_changed(self, text):
        self._validate_text(self.le_e_calib_a0, text)

    def le_e_calib_a1_text_changed(self, text):
        self._validate_text(self.le_e_calib_a1, text)

    def le_e_calib_a2_text_changed(self, text):
        self._validate_text(self.le_e_calib_a2, text)

    def le_fwhm_b1_text_changed(self, text):
        self._validate_text(self.le_fwhm_b1, text)

    def le_fwhm_b2_text_changed(self, text):
        self._validate_text(self.le_fwhm_b2, text)

    def le_incident_energy_text_changed(self, text):
        if self._validate_as_float(text) and float(text) > 0:
            self.le_incident_energy.setValid(True)
            self._range_high_update(text)
        else:
            self.le_incident_energy.setValid(False)
        self._update_exit_buttons_states()

    def le_range_low_text_changed(self, text):
        text_valid = False
        if self._validate_as_float(text):
            v = float(text)
            if 0 <= v < self._dialog_data["energy_bound_high"]["value"]:
                text_valid = True
        self.le_range_low.setValid(text_valid)
        self.le_range_high.setValid(text_valid)
        self._update_exit_buttons_states()

    def le_range_high_text_changed(self, text):
        text_valid = False
        if self._validate_as_float(text):
            v = float(text)
            if v > self._dialog_data["energy_bound_low"]["value"]:
                text_valid = True
        self.le_range_high.setValid(text_valid)
        self.le_range_low.setValid(text_valid)
        self._update_exit_buttons_states()

    def _recover_last_valid_le_text(self, line_edit, last_value):
        if not self._validate_as_float(line_edit.text()):
            line_edit.setText(self._format_float(last_value["value"]))
            return False
        else:
            return True

    def le_e_calib_a0_focus_out(self):
        self._recover_last_valid_le_text(self.le_e_calib_a0, self._dialog_data["e_offset"])

    def le_e_calib_a1_focus_out(self):
        self._recover_last_valid_le_text(self.le_e_calib_a1, self._dialog_data["e_linear"])

    def le_e_calib_a2_focus_out(self):
        self._recover_last_valid_le_text(self.le_e_calib_a2, self._dialog_data["e_quadratic"])

    def le_fwhm_b1_focus_out(self):
        self._recover_last_valid_le_text(self.le_fwhm_b1, self._dialog_data["fwhm_offset"])

    def le_fwhm_b2_focus_out(self):
        self._recover_last_valid_le_text(self.le_fwhm_b2, self._dialog_data["fwhm_fanoprime"])

    def le_incident_energy_focus_out(self):
        if not self._recover_last_valid_le_text(self.le_incident_energy, self._dialog_data["coherent_sct_energy"]):
            self.le_range_high.setText(self._format_float(self._dialog_data["energy_bound_high"]["value"]))

    def le_range_low_focus_out(self):
        self._recover_last_valid_le_text(self.le_range_low, self._dialog_data["energy_bound_low"])

    def le_range_high_focus_out(self):
        self._recover_last_valid_le_text(self.le_range_high, self._dialog_data["energy_bound_high"])

    def _reset_le_to_default(self, line_edit, data_ref):
        data_ref["value"] = data_ref["default"]
        line_edit.setText(self._format_float(data_ref["value"]))

    def pb_e_calib_a0_default_clicked(self):
        self._reset_le_to_default(self.le_e_calib_a0, self._dialog_data["e_offset"])

    def pb_e_calib_a1_default_clicked(self):
        self._reset_le_to_default(self.le_e_calib_a1, self._dialog_data["e_linear"])

    def pb_e_calib_a2_default_clicked(self):
        self._reset_le_to_default(self.le_e_calib_a2, self._dialog_data["e_quadratic"])

    def pb_fwhm_b1_default_clicked(self):
        self._reset_le_to_default(self.le_fwhm_b1, self._dialog_data["fwhm_offset"])

    def pb_fwhm_b2_default_clicked(self):
        self._reset_le_to_default(self.le_fwhm_b2, self._dialog_data["fwhm_fanoprime"])

    def pb_incident_energy_default_clicked(self):
        self._reset_le_to_default(self.le_incident_energy, self._dialog_data["coherent_sct_energy"])

    def pb_range_low_default_clicked(self):
        self._reset_le_to_default(self.le_range_low, self._dialog_data["energy_bound_low"])

    def pb_range_high_default_clicked(self):
        self._reset_le_to_default(self.le_range_high, self._dialog_data["energy_bound_high"])


import json
f = open('data.json')
data = json.load(f)
# print(data[0]["batters"]["batter"][2]["id"])
# print(data[0]["topping"][2]["type"])

def sample():
   # try :
        if id=="0001":
            if val :
                if int(val) in range(1001,1010):
                    s=int(val[-1])-1
                    print(data[0]["batters"]["batter"][int(s)]["type"])
                else:
                    if int(val) in range(5001,5010):
                        s=int(val[-1])-1
                        print(data[0]["topping"][int(s)]["type"])

        if id=="0002":
            if val :
                if int(val) in range(1001,1999):
                    s=int(val[-1])-1
                    print(data[1]["batters"]["batter"][int(s)]["type"])
                else:
                    if int(val) in range(5001,6000):
                        s=int(val[-1])-1
                        print(data[1]["topping"][int(s)]["type"])

        if id=="0003":
            if val:
                if int(val) in range(1001,1999):
                    s=int(val[-1])-1
                    print(data[2]["batters"]["batter"][int(s)]["type"])
                else:
                    if int(val) in range(5001,6000):
                        s=int(val[-1])-1
                        print(data[2]["topping"][int(s)]["type"])

   # except IndexError:
      #  print("not valid")

id=input("Enter the ID: ")
print(id,"Valid Data")

val=input("Enter the VALUE id: ")

sample()


# Uses python3
import random
from queue import Queue

def partition2(arr, left, right):
    # рандомизируем опорную точку
    rnd_idx = random.randint(left, right)
    arr[right], arr[rnd_idx] = arr[rnd_idx], arr[right]

    j = left - 1
    pivot = arr[right]

    for i in range(left, right):
        if arr[i] <= pivot:
            j += 1
            arr[j], arr[i] = arr[i], arr[j]
    arr[j + 1], arr[right] = arr[right], arr[j + 1]
    return j + 1

def quick(arr, verbose=False):
    buff = Queue()
    buff.put((0, len(arr) - 1))
    while not buff.empty():
        left, right = buff.get()  # block=False)
        if verbose:
            print("left=", left, "right=", right)
        if left < right:
            p = partition2(arr, left, right)
            if verbose:
                print("p=", p)
            buff.put((left, p - 1))  # ,block=False)
            buff.put((p + 1, right))  # ,block=False)
    return arr

def partition3(arr, left, right, verbose=False):
    # рандомизируем опорную точку
    rnd_idx = random.randint(left, right)
    pivot = arr[rnd_idx]
    # right bound of section with elements that less than pivot
    right_less = left - 1
    # left bound of section with elements that more than pivot
    left_more = right + 1
    if verbose:
        print("pivot=", pivot)
    i = left
    while i < left_more:
        if verbose:
            print("pre: arr[", i, "]=", arr[i], "(pivot=", pivot, "|", "arr2 =", arr, ")")
        if arr[i] < pivot:
            right_less += 1
            arr[i], arr[right_less] = arr[right_less], arr[i]
            i += 1
        elif arr[i] > pivot:
            left_more -= 1
            arr[i], arr[left_more] = arr[left_more], arr[i]
        else:
            i += 1
        if verbose:
            print("post: arr[", i, "]=", arr[i], "(pivot=", pivot, "|", "arr2 =", arr, ")")
    if verbose:
        print("arr2 =", arr)
    return right_less, left_more

def quick3(arr, verbose=False):
    buff = Queue()
    buff.put((0, len(arr) - 1))
    while not buff.empty():
        left, right = buff.get()  # block=False)
        if verbose:
            print("left=", left, "right=", right)
        if left < right:
            right_less, left_more = partition3(arr, left, right)
            if verbose:
                print("right_less=", right_less, "left_more=", left_more)
            if right_less >= 0:
                buff.put((left, right_less))  # ,block=False)
            if left_more < right:
                buff.put((left_more, right))  # ,block=False)
    return arr

if __name__ == '__main__':
    # print(quick3([random.randint(1, 9) for _ in range(20)]))
    n = int(input())
    arr = [int(x) for x in input().split()]
    result = quick3(arr)
    print(" ".join([str(x) for x in result]))


from django.conf import settings
import random
import pycountry
import numpy as np

from constant.models import YardBucket, FeetBucket
from core.models import PRACTICE_TYPES, DeltaShotReport, Practice
from profiles.models import ClubBag

def pick_random_distances(putting=False, practice=None, driver=None, clubs=None):
    # get existing dist_list
    # --- e.g: 75, 128, 103, 155, 189, 140, 170, 105, 200, 240, 85, 110, 185, 133, 160, 350, 70, 162, 120, 130
    if practice is None:
        existing_dist_list = []
    else:
        existing_dist_list = DeltaShotReport.objects.filter(practice=practice).values_list('distance', flat=True)
        if len(existing_dist_list) > 0:
            existing_dist_list = list(set(existing_dist_list))

    # pick_number e.g: [1,2,2,2,1,1,2]
    if putting:
        buckets = FeetBucket.objects.all()
    else:
        buckets = YardBucket.objects.all()

    # get new distance in every bucket
    new_dist = {}
    final_dist_list = []
    clubs_list = clubs.all()
    club_dist_list = []
    rest_dist_list = []

    if len(clubs.all()):
        for club in reversed(clubs_list):
            club_dist_list.append(club.avg_dist)
        for i in range(len(club_dist_list)-1):
            random_choose_dist = random.choice(list(range(club_dist_list[i], club_dist_list[i+1])))
            if random_choose_dist >= 75:
                rest_dist_list.append(random_choose_dist)

    for bucket in buckets:
        new_dist[bucket.pk] = bucket.pick_dists(existing_dist_list, driver)

    # randomize by bucket order
    bucket_order = list(buckets.values_list('pk', flat=True))
    random.shuffle(bucket_order)

    while True:
        old_count = len(final_dist_list)
        for bucket_pk in bucket_order:
            try:
                final_dist_list.append(new_dist[bucket_pk].pop())
            except IndexError:  # pop from empty list
                pass
        if len(final_dist_list) >= settings.PICK_COUNT:
            break
        if len(final_dist_list) == old_count:  # not be appended anymore
            break

    final_dist_list = final_dist_list + rest_dist_list
    random.shuffle(final_dist_list)

    # if length > PICK_COUNT, randomly pop
    _length = len(final_dist_list)
    if _length > settings.PICK_COUNT:
        poped_random_index_list = random.sample(range(_length), _length - settings.PICK_COUNT)
        poped_random_index_list.sort(reverse=True)
        for index in poped_random_index_list:
            final_dist_list.pop(index)

    return final_dist_list

def pick_standard_putts(practice=None):
    if practice is None:
        existing_dist_list = []
    else:
        existing_dist_list = practice.get_existing_dist_list()

    buckets = FeetBucket.objects.filter(max__lte=25)  # NOTE: hardcode 25
    # get new distance in every bucket
    new_dist = {}
    for bucket in buckets:
        new_dist[bucket.pk] = bucket.pick_standard_dists(existing_dist_list)

    # randomize by bucket order
    bucket_order = list(buckets.values_list('pk', flat=True))
    random.shuffle(bucket_order)

    final_dist_list = []
    for bucket_pk in bucket_order * 3:  # NOTE: hardcoded 3.
        try:
            final_dist_list.append(new_dist[bucket_pk].pop())

        except IndexError:  # pop from empty list
            pass

    # if length > PICK_COUNT, randomly pop
    _length = len(final_dist_list)
    if _length > settings.PICK_COUNT:
        poped_random_index_list = random.sample(range(_length), _length - settings.PICK_COUNT)
        for index in poped_random_index_list:
            final_dist_list.pop(index)

    return final_dist_list

def pick_chip_distances(practice=None):
    return random.sample(set(range(5, 25)), 10)

def pick_pitch_distances(practice=None):
    return random.sample(set(range(25, 75)), 10)

def get_blocked_bin(user=None):
    """
    :return: bin for block practice. NOTE: first bin (check again later.)
    """
    if user is None:
        return None

    bin_list = YardBucket.get_hist_bin()
    practice_list = Practice.objects.filter(
        user=user,
        practice_type__in=[PRACTICE_TYPES.random, PRACTICE_TYPES.block, PRACTICE_TYPES.serial, PRACTICE_TYPES.custom]
    )

    block_bin = None
    yards = YardBucket.objects.all()

    for yard in yards:
        all_shots1 = DeltaShotReport.objects.filter(
            practice__in=practice_list).filter(distance__in=range(yard.min, yard.max)).order_by("-reported_at")
        all_shots = all_shots1[0:10]
        all_dists = [shot.distance for shot in all_shots]
        hit_dists = [shot.distance for shot in all_shots if shot.hit == 0]

        total_hist, bins = np.histogram(all_dists, bins=bin_list)
        hit_hist, bins = np.histogram(hit_dists, bins=bin_list)

        for h, t, b in zip(hit_hist, total_hist, bin_list):
            if t != 0:
                if t < 10 or h / t < 0.3:
                    continue

                # if len(hit_dists):
                #     if sum(hit_dists) / len(hit_dists) > 224:
                #         block_bin = YardBucket.objects.get(min=196)
                #         break

                block_bin = YardBucket.objects.get(min=b)
                break

        if block_bin is not None:
            break

    return block_bin

def pick_custom_distances(min, max):
    if min == max:
        return [min for i in range(10)]
    try:
        dist_set = list(range(min, max+1))
        if len(dist_set) < 10:
            dist_set = (10//len(dist_set) + 1) * dist_set
        return random.sample(dist_set, 10)
    except ValueError as e:
        return []

def is_full_swing(p_type):
    """
    :param p_type: practice_type
    :return: True/False
    """
    if p_type in [PRACTICE_TYPES.random, PRACTICE_TYPES.warmup,
                  PRACTICE_TYPES.serial, PRACTICE_TYPES.block,
                  PRACTICE_TYPES.custom]:
        return True
    else:
        return False

def is_short_game(p_type):
    """
    :param p_type: practice_type
    :return: True/False
    """
    if p_type in [PRACTICE_TYPES.chip, PRACTICE_TYPES.pitch]:
        return True
    else:
        return False

def get_country_list():
    """Return a list of ISO 3166-1 Alpha 2 country codes."""

    countries0 = [(x.alpha_2, x.name)for x in pycountry.countries]
    countries_sorted = sorted(countries0, key=lambda x: x[1])
    country_list = [
        ("", "--- Select country ---"),
    ]
    country_list.extend(countries_sorted)
    return country_list

def groupby2dict(grouped_df):
    levels = len(grouped_df.index.levels)
    dicts = [{} for i in range(levels)]
    last_index = None

    for index, value in grouped_df.itertuples():

        if not last_index:
            last_index = index

        for (ii, (i, j)) in enumerate(zip(index, last_index)):
            if not i == j:
                ii = levels - ii - 1
                dicts[:ii] = [{} for _ in dicts[:ii]]
                break

        for i, key in enumerate(reversed(index)):
            dicts[i][key] = value
            value = dicts[i]

        last_index = index

    return dicts[-1]