#! /usr/bin/env python
#
def ncoh_compute ( n ):

#*****************************************************************************80
#
## NCOH_COMPUTE computes a Newton-Cotes "open half" quadrature rule.
#
#  Discussion:
#
#    The input value N is used to define N equal subintervals of [-1,+1].
#    The I-th abscissa is the center of the I-th subinterval.
#
#    The integral:
#
#      Integral ( X_MIN <= X <= X_MAX ) F(X) dx
#
#    The quadrature rule:
#
#      Sum ( 1 <= I <= N ) W(I) * F ( X(I) ).
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    21 June 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the order.
#
#    Output, real X(N), the abscissas.
#
#    Output, real W(N), the weights.
#
  import numpy as np
  from nc_compute_weights import nc_compute_weights

  x = np.zeros ( n )

  x_min = -1.0
  x_max =  1.0

  for i in range ( 0, n ):
    x[i] = ( float ( 2 * n - 2 * i - 1 ) * x_min   \
           + float (         2 * i + 1 ) * x_max ) \
           / float ( 2 * n             )

  w = nc_compute_weights ( n, x_min, x_max, x )

  return x, w

def ncoh_compute_test ( ):

#*****************************************************************************80
#
## NCOH_COMPUTE_TEST tests NCOH_COMPUTE.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    21 June 2015
#
#  Author:
#
#    John Burkardt
#
  import platform

  print ( '' )
  print ( 'NCOH_COMPUTE_TEST' )
  print ( '  Python version: %s' % ( platform.python_version ( ) ) )
  print ( '  NCOH_COMPUTE computes a Newton-Cotes Open Half quadrature rule' )
  print ( '' )
  print ( '  Index       X             W' )

  for n in range ( 1, 11 ):

    x, w = ncoh_compute ( n )

    print ( '' )

    for i in range ( 0, n ):
      print ( '  %2d  %24.16g  %24.16g' % ( i, x[i], w[i] ) )
#
#  Terminate.
#
  print ( '' )
  print ( 'NCOH_COMPUTE_TEST' )
  print ( '  Normal end of execution.' )
  return

if ( __name__ == '__main__' ):
  from timestamp import timestamp
  timestamp ( )
  ncoh_compute_test ( )
  timestamp ( )


import numpy as np
import os
import random
import torch

def get_images(paths, labels, nb_samples=None, shuffle=True):
    """
    Takes a set of character folders and labels and returns paths to image files
    paired with labels.
    Args:
        paths: A list of character folders
        labels: List or numpy array of same length as paths
        nb_samples: Number of images to retrieve per character
    Returns:
        List of (label, image_path) tuples
    """
    if nb_samples is not None:
        sampler = lambda x: random.sample(x, nb_samples)
    else:
        sampler = lambda x: x
    images_labels = [(i, os.path.join(path, image))
                     for i, path in zip(labels, paths)
                     for image in sampler(os.listdir(path))]
    if shuffle:
        random.shuffle(images_labels)
    return images_labels

def image_file_to_array(filename, dim_input):
    """
    Takes an image path and returns numpy array
    Args:
        filename: Image filename
        dim_input: Flattened shape of image
    Returns:
        1 channel image
    """
    import imageio
    image = imageio.imread(filename)
    image = image.reshape([dim_input])
    image = image.astype(np.float32) / 255.0
    image = 1.0 - image
    return image

class DataGenerator(object):
    """
    Data Generator capable of generating batches of Omniglot data.
    A "class" is considered a class of omniglot digits.
    """

    def __init__(self, num_classes, num_samples_per_class, config={}, device = torch.device('cpu')):
        """
        Args:
            num_classes: int
                Number of classes for classification (N-way)

            num_samples_per_class: int
                Number of samples per class in the support set (K-shot).
                Will generate additional sample for the querry set.

            device: cuda.device:
                Device to allocate tensors to.
        """
        self.num_samples_per_class = num_samples_per_class
        self.num_classes = num_classes

        data_folder = config.get('data_folder', './omniglot_resized')
        self.img_size = config.get('img_size', (28, 28))

        self.dim_input = np.prod(self.img_size)
        self.dim_output = self.num_classes

        character_folders = [os.path.join(data_folder, family, character)
                             for family in os.listdir(data_folder)
                             if os.path.isdir(os.path.join(data_folder, family))
                             for character in os.listdir(os.path.join(data_folder, family))
                             if os.path.isdir(os.path.join(data_folder, family, character))]

        random.seed(1)
        random.shuffle(character_folders)
        num_val = 100
        num_train = 1100
        self.metatrain_character_folders = character_folders[: num_train]
        self.metaval_character_folders = character_folders[
            num_train:num_train + num_val]
        self.metatest_character_folders = character_folders[
            num_train + num_val:]
        self.device = device

    def sample_batch(self, batch_type, batch_size):
        """
        Samples a batch for training, validation, or testing
        Args:
            batch_type: str
                train/val/test set to sample from

            batch_size: int:
                Size of batch of tasks to sample

        Returns:
            images: tensor
                A tensor of images of size [B, K+1, N, 784]
                where B is batch size, K is number of samples per class,
                N is number of classes

            labels: tensor
                A tensor of images of size [B, K+1, N, N]
                where B is batch size, K is number of samples per class,
                N is number of classes
        """
        if batch_type == "train":
            folders = self.metatrain_character_folders
        elif batch_type == "val":
            folders = self.metaval_character_folders
        else:
            folders = self.metatest_character_folders

        #############################
        #### YOUR CODE GOES HERE ####
        #############################

        N=self.num_classes
        K=self.num_samples_per_class
        B=batch_size

        images=torch.zeros(B,N,K+1,784,dtype=torch.float32)
        labels=torch.zeros(B,N,K+1,N,dtype=torch.float32)

        # random.seed(5533)
        for b in range(B):
            task_folders=[folders[random.randint(0, len(folders)-1)] for i in range(N)]

            task_images=torch.zeros(N,K+1,784,dtype=torch.float32)
            task_labels=torch.zeros(N,K+1,N,dtype=torch.float32)
            for class_index,class_path in enumerate(task_folders):
                imgs_paths=os.listdir(class_path)
                # 从imgs_paths 选择K+1个路径 ，这里考虑不放回
                K_imgs_paths=[os.path.join(class_path, imgs_paths[r]) for r in random.sample(list(range(len(imgs_paths))),K+1)]
                #归一化处理？？
                task_images[class_index]=torch.stack([torch.tensor(image_file_to_array(p,28*28)) for p in K_imgs_paths])
                task_labels[class_index]= torch.nn.functional.one_hot(torch.tensor(class_index), num_classes=N)

            idx=list(range(N))
            sh_idx=idx[:]
            random.shuffle(sh_idx)

            task_images[idx,K]=task_images[sh_idx,K]
            task_labels[idx,K]=task_labels[sh_idx,K]
            images[b]=task_images
            labels[b]=task_labels
        images.transpose_(1, 2)
        labels.transpose_(1, 2)
        return images.to(self.device),labels.to(self.device)
        # SOLUTION:


import torch as th
import time
import termcolor
import collections

from refact_encoding import RefactEncoding, hlprint
import code_contrast.format_2022q3 as format_2022q3

from refact_scratchpads import ScratchpadBase

from typing import Dict, Optional, Any, List, Tuple

class ScratchpadDiff(ScratchpadBase):
    def __init__(
        self,
        enc: RefactEncoding,
        intent: str,
        cursor_file: str,
        cursor0: int,
        cursor1: int,
        function: str,
        max_edits: int,
        sources: Dict[str, str],
        poi: Optional[List[Dict[str, Any]]] = None,
        **kwargs
    ):
        super().__init__(enc, **kwargs)
        self.intent = intent
        self.cursor_file = cursor_file
        self.cursor0 = cursor0
        self.cursor1 = cursor1
        self.function = function
        self.max_edits = max_edits
        self.sources = sources
        self.file_poi = collections.defaultdict(set)
        if poi is not None:
            # "poi": [{'filename': 'attractgame_mainloop.py', 'cursor0': 0, 'cursor1': 971, 'priority': 0.944}]
            for rec in poi:
                fn = rec.get("filename", None)
                if not isinstance(fn, str):
                    continue
                if fn.endswith(cursor_file):  # temporary kludge, remove
                    continue
                if not isinstance(rec.get("cursor0", None), int):
                    continue
                if not isinstance(rec.get("cursor1", None), int):
                    continue
                self.file_poi[fn].add(int(rec["cursor0"]))
                self.file_poi[fn].add(int(rec["cursor1"]))
        self.state_before_first_tpos = True
        self.diff: format_2022q3.ContrastDiff = None
        self.diff_out: Optional[format_2022q3.ContrastDiff] = None
        self.diff_out_us: Optional[format_2022q3.UntokenizeState] = None
        self.highlight = []
        self.highlight16 = []
        self.t_cursor0 = -1
        self.t_cursor1 = -1
        self.tpos_cursor1 = -1
        self.edits_uploaded = 0
        self.prompt_edits = 0
        self.cursorfile_tokens1 = None
        self.cursorfile_tokens2 = None
        self.cursorfile_map1to2 = None
        self.cursorfile_map2to1 = None
        self.increase_logits = []
        self.no_stop_tokens_until = -1
        self.selected_newlines = -1
        self.selection_is_important = (self.function in ["diff-atcursor", "diff-selection"])
        self.backward_cache_snippet: str = ""
        self.backward_cache_tokens: List[int] = []
        self.backward_cache_cursor: int = 0
        self.ugly_hack_reattach_next_line: Optional[str] = None
        self.P1 = 0.35
        self.P2 = 0.20
        self.JP1 = 0.20

    def set_model_thresholds(self, P1, P2, JP1, **more):
        self.P1 = P1
        self.P2 = P2
        self.JP1 = JP1
        super().set_model_thresholds(**more)

    def before_token_selection(
            self,
            m: Any,
            b: int,
            logits: th.Tensor,
            heads: List[th.Tensor],
            **unused
    ) -> Dict[str, Any]:
        if self.state_before_first_tpos:
            if self.function == "highlight":
                self.highlight_method4(m, b, logits, heads)
            self.state_before_first_tpos = False
        prev_token = self.diff.r[-1]
        suggest_tokens = []
        logits_intrusion: Dict[int, float] = dict()
        if prev_token == self.enc.CHUNK:
            for tpos in self.increase_logits:
                logits_intrusion[tpos] = +4.5
        if (
                self.diff_out_us is not None and
                self.diff_out_us.state == format_2022q3.contrast.DEL and
                self.diff_out_us.brewing_edit.real_delstart != -1 and
                self.diff_out_us.brewing_edit.fn == self.cursor_file
        ):
            e = self.diff_out_us.brewing_edit
            scratch = self.diff_out_us.scratch[e.fn]
            if self.cursorfile_tokens2 is not None:
                tokens2 = self.cursorfile_tokens2
                assert all(tokens2[i] == scratch[i] for i in range(len(scratch)))
                # print("todel:", termcolor.colored(self.enc.decode(scratch[e.real_delstart:e.real_delends]), "yellow"))
                # good, _ = self._lookahead_ignoring_tpos(scratch, e.real_delstart, e.todel)
                # print("suggest: [%s]" % termcolor.colored(self.enc.decode(scratch[e.real_delends:e.real_delends + 8]).replace("\n", "\\n"), "blue"))
                suggest_tokens = scratch[e.real_delends:e.real_delends + 10]
                suggest_tokens = [t for t in suggest_tokens if not self.enc.is_tpos(t)][:8]
                if self.selection_is_important:
                    beyond_selection = self.diff_out_us.brewing_edit.real_delends - self.t_cursor1
                    if beyond_selection >= -1:
                        extra_newlines = len([t for t in scratch[self.t_cursor1:self.diff_out_us.brewing_edit.real_delends] if t == self.enc.LF])
                        if extra_newlines >= 0:
                            logits_intrusion[self.enc.ESCAPE] = 5.0 + 0.5 * extra_newlines
                # edit works like this: scratch[e.real_delstart:e.real_delends] = e.toins
        T = logits.shape[0]
        UPTO = 10
        if T > UPTO and self.backward_cache_cursor > UPTO+1:  # infill function only
            # self.backward_cache_cursor -- position of the infill token minus about three
            # self.backward_cache_snippet -- original file until cursor
            argmax = th.argmax(logits[self.backward_cache_cursor-UPTO-1:self.backward_cache_cursor], dim=1)
            self.backward_cache_tokens = []
            for minus in range(1, UPTO):
                t = self.diff.r[self.backward_cache_cursor - minus]
                if self.enc.is_tpos(t):
                    continue
                if t == argmax[-minus-1]:
                    self.backward_cache_tokens = [t] + self.backward_cache_tokens
                else:
                    self.debuglog("PROMPT TOKEN \"%s\" BUT ARGMAX FROM PREVIOUS \"%s\""  % (self.enc.decode([t]), self.enc.decode([argmax[-minus-1].item()])))
                    break
            self.debuglog("BACK CACHE \"%s\"" % (self.enc.decode(self.backward_cache_tokens).replace("\n", "\\n")))
        return dict(
            logits_intrusion=logits_intrusion,
            suggest_tokens=suggest_tokens,
        )

    def after_token_selection(
            self,
            m,
            chosen_token: th.Tensor,
            **unused
    ) -> Dict[str, Any]:
        t = chosen_token.item()
        self.diff.r.append(t)
        self.diff_out_catch_up()
        self.generated_tokens_n += 1
        if self.generated_tokens_n <= 3:
            self.backward_cache_tokens.append(t)
        return dict()

    def toplevel_fields(self):
        if self.function == "highlight":
            return {"highlight_tokens": self.highlight, "highlight_lines": self.highlight16}
        if self.function == "infill":
            cached_longer_txt = self.enc.decode(self.backward_cache_tokens)
            #self.debuglog("backward_cache \"%s\"" % (cached_longer_txt.replace("\n", "\\n")))
            #self.debuglog("backward_cache_snippet \"%s\"" % (self.backward_cache_snippet.replace("\n", "\\n")))
            for cut_extra in range(min(len(cached_longer_txt), 40)):
                cached = cached_longer_txt[:len(cached_longer_txt)-cut_extra]
                if self.backward_cache_snippet.endswith(cached):
                    # self.debuglog("backward_cache final \"%s\"" % (cached.replace("\n", "\\n")))
                    return {"backward_cache": cached}
            # self.debuglog("backward_cache Ъци")
            return {"backward_cache": ""}
        return {}

    def completion(self, final: bool):
        if final:
            if self.diff_out_us is not None:
                self.diff_out_catch_up()
                self.diff_out.untokenize_finish_state(self.diff_out_us, self.diff_out_cursor)
            else:
                self.debuglog("ScratchpadDiff: nothing useful available")
                return None
        else:
            self.diff_out_catch_up()
        dest_tokens = self.diff_out.apply_edits_return_dest(self.diff_out_us)
        result = {}
        for fn in dest_tokens:
            result[fn] = self.enc.decode(self.diff_out.dest_tokens[fn])
        self.debuglog("ScratchpadDiff: final=%i" % final, self.diff_out_us.stats, self.finish_reason)
        return result

    def diff_out_catch_up(self):
        if self.diff_out_us is None:
            return
        def finish(reason):
            self.finish_reason = reason
            self.diff_out.untokenize_finish_state(self.diff_out_us, self.diff_out_cursor)
            if reason in ["ins-stoptoken", "ins-stop-lf", "ins-stop-lflf"] and self.ugly_hack_reattach_next_line is not None:
                if len(self.diff_out.edits) == 1:
                    self.debuglog("REATTACH '%s'\n" % (self.ugly_hack_reattach_next_line.replace("\n", "\\n")))
                    self.diff_out.edits[0].toins.extend(self.enc.encode(self.ugly_hack_reattach_next_line) + [self.enc.LF])
            self.diff_out_us.state = format_2022q3.contrast.WAIT
        try:
            while self.diff_out_cursor < len(self.diff.r):
                c = self.diff_out_cursor
                t = self.diff.r[c]
                if t==self.enc.EOT:
                    finish("eot")
                    break
                self.diff_out.untokenize_new_token(self.diff_out_us, t, c)
                self.diff_out_cursor += 1
                if self.diff_out_us.state == format_2022q3.contrast.CHUNK and self.max_edits >= 0 and len(self.diff_out.edits) - self.prompt_edits >= self.max_edits:
                    finish("max-edits")
                    break
                if c >= self.no_stop_tokens_until and self.diff_out_us.state == format_2022q3.contrast.INS:
                    if t in self.stop_tokens:
                        finish("ins-stoptoken")
                        break
                    if self.stop_lf_lf and (self.diff.r[c - 1], t) == (self.enc.LF, self.enc.LF):
                        finish("ins-stop-lflf")
                        break
                    if self.stop_lf and t == self.enc.LF:
                        finish("ins-stop-lf")
                        break
                    if t == self.enc.LF:
                        if self.stream:
                            self.needs_upload = True
                if self.diff_out_us.state in [format_2022q3.contrast.DEL, format_2022q3.contrast.SHIFT]:
                    # print("TEST epos=%i in %s\n\n" % (self.diff_out_us.e_tpos, self.increase_logits))
                    if len(self.increase_logits) > 0 and (self.diff_out_us.brewing_edit.tpos not in self.increase_logits):
                        finish("out-of-selection")
                        break
        except format_2022q3.DecodeError as e:
            self.debuglog("Exception in diff_out.untokenize_new_token: %s" % e)
            self.finish_reason = "diff-application-error"

    def prompt_infill(self, T):
        for fn, text in self.sources.items():
            if self.cursor_file == fn:
                cut_slash_n = text[self.cursor0:]
                slash_n_idx = cut_slash_n.find("\n")
                if slash_n_idx >= 0:
                    cut_slash_n = cut_slash_n[slash_n_idx+1:]
                if 1:
                    # To fully remove this, we need to retrain the model, the q1 version should fix it
                    next_slash_n = cut_slash_n.find("\n")
                    if next_slash_n >= 0:
                        self.ugly_hack_reattach_next_line = cut_slash_n[:next_slash_n]
                        self.debuglog("self.ugly_hack_reattach_next_line \"%s\"" % self.ugly_hack_reattach_next_line.replace("\n", "\\n"))
                    else:
                        self.ugly_hack_reattach_next_line = None
                self.odm["orig"][fn] = text[:self.cursor0] + self.enc.decode([self.enc.INFILL]) + cut_slash_n
                self.odm["dest"][fn] = text[:self.cursor0] + self.enc.decode([self.enc.DUMMY]) + cut_slash_n
            elif fn in self.file_poi:
                self.odm["orig"][fn] = text
                self.odm["dest"][fn] = text
        self.orig_tokens = self.diff.from_odm_dict(
            self.odm,
            n_ctx=(T - self.max_tokens),
            tight_shrink=True,
            exact_cx_lines0=2,
            exact_cx_lines1=0,
            external_poi=self.file_poi,
            )
        self.backward_cache_cursor = self.diff.r.index(self.enc.INFILL)
        self.diff.write_edits()
        assert len(self.diff.edits) == 1
        while len(self.diff.r) > 0:
            t = self.diff.r.pop()
            if t == self.enc.DUMMY:
                break
        del3more = 3
        self.backward_cache_snippet = text[:self.cursor0]
        self.backward_cache_deltokens = 0
        while len(self.diff.r) > 0 and self.diff.r[-1] not in [self.enc.LF] and del3more > 0:
            self.diff.r.pop()
            self.backward_cache_cursor -= 1
            del3more -= 1

    def prompt_edit_chain(self, T):
        minrev = 10000
        for fn, text in self.sources.items():
            fn, revision = format_2022q3.contrast.parse_fn(fn)
            if revision is None:
                continue
            if self.function != "edit-chain":
                continue
            if self.cursor_file != fn:
                continue
            if revision < minrev:
                minrev = revision
            else:
                continue
            self.odm["orig"][fn] = text
            # self.debuglog("revision", revision)
            # self.debuglog("EDIT CHAIN BASE", text)
        for fn, text in self.sources.items():
            fn, suffix = format_2022q3.contrast.parse_fn(fn)
            if suffix is not None:
                continue
            self.odm["dest"][fn] = text
            # self.debuglog("EDIT CHAIN DEST", text)
        self.orig_tokens = self.diff.from_odm_dict(
            self.odm,
            n_ctx=(T - self.max_tokens),
            tight_shrink=True,
            exact_cx_lines0=2,
            exact_cx_lines1=0,
            )
        self.diff.write_edits()
        assert self.diff.r[-1] == self.enc.EOT
        self.diff.r = self.diff.r[:-1]
        self.prompt_edits = len(self.diff.edits)

    def prompt_normal_diff(self, T):
        # Highlight also goes here
        for fn, text in self.sources.items():
            self.odm["orig"][fn] = text
            if self.cursor_file == fn:
                # make sure cursor01 is visible
                self.odm["dest"][fn] = text[:self.cursor0] + self.enc.decode([self.enc.DUMMY]) + text[self.cursor1:]
            else:
                self.odm["dest"][fn] = text
        self.orig_tokens = self.diff.from_odm_dict(
            self.odm,
            n_ctx=(T - self.max_tokens),
            tight_shrink=True,
            exact_cx_lines0=2,
            exact_cx_lines1=0,
            )
        self.diff_out = format_2022q3.contrast.ContrastDiff(self.enc)
        self.diff_out_us = self.diff_out.untokenize_init(self.orig_tokens)
        self.diff_out_cursor = 0
        if self.cursor0 != -1:
            self._find_selection_in_tokens()
        if self.function == "highlight":
            self.diff.write_esc_chunk()
            return
        if self.selected_newlines in [0, 1] and self.selection_is_important:
            # selected single line or atcursor, write most of the chunk immediately
            self.max_edits = 1
            # tpos = self.cursorfile_tokens2[self.tpos_cursor0]
            # assert self.enc.is_tpos(tpos)
            # self.diff.r.append(tpos)
            self.diff.write_edits()
            assert len(self.diff.edits) == 1
            while len(self.diff.r) > 0:
                t = self.diff.r.pop()
                if t == self.enc.DUMMY:
                    break
            while len(self.diff.r) > 0 and self.diff.r[-1] not in [self.enc.LF]:
                self.diff.r.pop()
        elif self.cursorfile_tokens2 is not None and self.selection_is_important:
            # multi line selection, logits
            i = self.t_cursor0
            over = False
            while 1:
                t = self.cursorfile_tokens2[i]
                if self.enc.is_tpos(t):
                    self.debuglog("diff-selection increase logits", hlprint(self.enc, [t]))
                    self.increase_logits.append(t)
                    if over: break
                if i >= self.t_cursor1:
                    if len(self.increase_logits) > 0:
                        break
                    over = True
                if i >= len(self.cursorfile_tokens2):
                    break
                i += 1
            self.increase_logits.append(self.enc.EOT)
            self.diff.write_esc_chunk()
        else:
            self.diff.write_esc_chunk()

    def prompt(self, T):
        t0 = time.time()
        self.diff = format_2022q3.contrast.ContrastDiff(self.enc)
        self.odm = {
            "orig": dict(),
            "commitmsg": self.intent,
            "dest": dict(),
        }
        # "^(highlight|infill|diff-anywhere|diff-atcursor|diff-selection|edit-chain)$"
        if self.function == "infill":
            self.prompt_infill(T)
        elif self.function == "edit-chain":
            self.prompt_edit_chain(T)
        else:
            self.prompt_normal_diff(T)
        if len(self.diff.r) >= T:
            self.debuglog("PACKING FAILED %i TOKENS\n" % (len(self.diff.r)))
            return []
        self.no_stop_tokens_until = len(self.diff.r)
        if self.diff_out is None:
            self.diff_out = format_2022q3.contrast.ContrastDiff(self.enc)
            self.diff_out_us = self.diff_out.untokenize_init(self.orig_tokens)
            self.diff_out_cursor = 0
            self.diff_out_catch_up()
            if self.cursor0 != -1:
                self._find_selection_in_tokens()
        return self.diff.r

    def _find_selection_in_tokens(self):
        assert self.cursor0 > -1 and self.cursor1 > -1, "cursor not set cursor0=%i cursor1=%i" % (self.cursor0, self.cursor1)
        if self.cursorfile_tokens1 is None:
            self.diff_out_catch_up()
            # equals to self.diff_out
            self.cursorfile_tokens1 = \
                self.diff.orig_tokens[self.cursor_file]
            # all tokens including previously cut top/bottom, with postion tokens in the middle
            self.cursorfile_tokens2 = \
                self.diff_out.orig_withpos[self.cursor_file]
            # works fast ~1ms
            self.cursorfile_map1to2, self.cursorfile_map2to1 = \
                self._fn_create_map1to2(self.cursorfile_tokens1, self.cursorfile_tokens2)

        # potentially slow if we get non-unicode encoded string
        self.t_cursor0, self.t_cursor1 = self._find_cursor_in_tokens(
            self.cursor0, self.cursor1, self.cursorfile_tokens1, self.cursorfile_map1to2)

        # self.debuglog(
        #     termcolor.colored(self.enc.decode(self.cursorfile_tokens2[:self.t_cursor0]), "yellow") +
        #     termcolor.colored("|", "green") +
        #     termcolor.colored(self.enc.decode(self.cursorfile_tokens2[self.t_cursor0:self.t_cursor1]), "red") +
        #     termcolor.colored("|", "green") +
        #     termcolor.colored(self.enc.decode(self.cursorfile_tokens2[self.t_cursor1:]), "yellow")
        #     )
        self.selected_newlines = len([t for t in self.cursorfile_tokens2[self.t_cursor0:self.t_cursor1] if t == self.enc.LF])

    def _fn_create_map1to2(self, tokens1: List[int], tokens2: List[int]) -> Tuple[List[int], List[int]]:
        i1 = 0
        map1to2 = []
        map2to1 = []
        # At the end after escape, only diamonds and the last tpos are allowed:
        seen_escape = False
        for i2, t in enumerate(tokens2):
            map2to1.append(i1)
            if self.enc.is_tpos(t):
                continue
            if i1 == len(map1to2) and i1 < len(tokens1):
                map1to2.append(i2)
            if t == self.enc.ESCAPE:
                seen_escape = True
                i1 += 1
            elif t == self.enc.DIAMOND:
                i1 += 1
            elif not seen_escape:
                assert t == tokens1[i1]
                i1 += 1
            else:
                assert 0
        assert len(tokens1) == len(map1to2)
        assert len(tokens2) == len(map2to1)
        return map1to2, map2to1

    def _find_cursor_in_tokens(self, cursor0: int, cursor1: int, tokens: List[int], map1to2: List[int]):
        assert cursor0 <= cursor1
        t_cursor0 = None
        t_cursor1 = None
        token_idx = 0
        text_idx = 0

        def decodable_text_steps(token_idx: int) -> Tuple[int, int]:
            token_jdx = token_idx + 1
            while token_jdx <= len(tokens):
                try:
                    text = self.enc.decode_utf8(tokens[token_idx:token_jdx])
                    return token_jdx - token_idx, len(text)
                except UnicodeDecodeError:
                    token_jdx += 1
            else:
                self.debuglog("invalid tokens: cannot decode utf8 sequence")
                return len(tokens) - token_idx, len(self.enc.decode(tokens[token_idx:]))

        while token_idx < len(tokens):
            token_step, text_step = decodable_text_steps(token_idx)
            if t_cursor0 is None and text_idx + text_step > cursor0:
                t_cursor0 = token_idx
            if t_cursor1 is None and text_idx >= cursor1:
                t_cursor1 = token_idx
            token_idx += token_step
            text_idx += text_step

        t_cursor0 = len(tokens) - 1 if t_cursor0 is None else t_cursor0
        t_cursor1 = t_cursor1 if cursor0 < cursor1 else t_cursor0  # special case
        t_cursor1 = len(tokens) - 1 if t_cursor1 is None else t_cursor1
        assert t_cursor0 <= t_cursor1
        return map1to2[t_cursor0], map1to2[t_cursor1]

    def highlight_method4(self, m: Any, b, logit, heads):
        t0 = time.time()
        x_bte = heads["x_bte"][b:b+1]
        first_bt = th.zeros_like(x_bte[:, :, 0])
        first_bt[:, 0] = 1
        diffhlpoint_bt = th.zeros_like(x_bte[:, :, 0])
        diffhlpoint_bt[:, -1] = 1
        # ed_joint = m.highlight_forward(x_bte, first_bt, diffhlpoint_bt)
        # e2_logits = m.bidir_2logits(ed_joint)
        inside = m.highlight_forward(x_bte, first_bt, diffhlpoint_bt)
        ed_joint = x_bte + inside
        e2_logits = m.bidir_2logits(m.bidir_2logits_ln(ed_joint))

        pd_hl = th.distributions.categorical.Categorical(logits=e2_logits[0] / 1.0)
        probs = pd_hl.probs
        t1 = time.time()
        tokens1, tokens2, map2to1 = self.cursorfile_tokens1, self.cursorfile_tokens2, self.cursorfile_map2to1
        # tokens1 without position tokens
        # tokens2 with position tokens
        # both tokens1 and tokens2 are full, not cut at top/bottom
        start = self.diff.fn2tstart[self.cursor_file]   # index in r
        end = start + len(self.diff.fn2tind[self.cursor_file])
        cut0 = self.diff_out.fn2cut0[self.cursor_file]
        self.highlight = []
        self.highlight16 = []
        inside_yellow = False
        inside_purple = False
        starts16 = -1
        ends16 = -1
        def no_longer_16():
            nonlocal starts16, ends16
            if starts16 == -1:
                return
            tmp1 = self.enc.decode(tokens1[:starts16])
            tmp2 = self.enc.decode(tokens1[:ends16])
            self.highlight16.append((len(tmp1), len(tmp2), 0.15))
            starts16 = -1
            ends16 = -1
        for ti in range(start, end):
            if self.diff.r[ti] == self.enc.ESCAPE:
                break
            assert tokens2[ti - start + cut0] == self.diff.r[ti]
            if self.diff.r[ti] != self.enc.LF:
                continue
            prev_lf = ti - 1
            while prev_lf >= start and self.diff.r[prev_lf] != self.enc.LF:
                prev_lf -= 1
            p0 = float(probs[ti][0].item())
            p1 = float(probs[ti][1].item())
            p2 = float(probs[ti][2].item())
            tokens0pos = map2to1[prev_lf + 1 - start + cut0]
            tokens1pos = map2to1[ti - start + cut0]
            want16 = 0
            if p1 > self.P1:
                want16 = 0.3
                inside_yellow = True
            elif p2 > self.P2 and inside_yellow:
                want16 = 0.15
                inside_purple = True
            else:
                inside_yellow = False
                inside_purple = False
                no_longer_16()
            if want16 > 0:
                if starts16 == -1:
                    starts16 = tokens0pos
                ends16 = tokens1pos
            self.debuglog(
                "%-60s" % (self.enc.decode(self.diff.r[prev_lf+1:ti+1]).replace("\n", "\\n")),
                termcolor.colored(
                    " %0.1f%% %0.1f%% %0.1f%%" % (100*p0, 100*p1, 100*p2),
                    ("magenta" if inside_purple else "red") if inside_yellow else None,
                ),
            )
            if inside_yellow:
                for tj in range(prev_lf+1, ti+1):
                    jp0 = float(probs[tj][0].item())
                    jp1 = float(probs[tj][1].item())
                    jp2 = float(probs[tj][2].item())
                    self.debuglog(
                        termcolor.colored(
                            "  %-20s" % (self.enc.decode(self.diff.r[tj:tj+1]).replace("\n", "\\n")),
                            "blue"),
                        termcolor.colored(
                            " %0.1f%%" % (100*jp1,),
                            "yellow" if (jp1 > self.JP1) else None,
                        )
                    )
                    if self.enc.is_tpos(self.diff.r[tj]):
                        continue
                    if jp1 > self.JP1:
                        tokens1pos = map2to1[tj - start + cut0]
                        tokens2pos = map2to1[tj + 1 - start + cut0]
                        jtmp1 = self.enc.decode(tokens1[:tokens1pos])
                        jtmp2 = self.enc.decode(tokens1[:tokens2pos])
                        self.highlight.append((len(jtmp1), len(jtmp2), 0.95))
                        # self.highlight.extend(self.enc.decode(tokens1[:tokens1pos]))
        no_longer_16()
        t2 = time.time()
        self.debuglog("highlight_method4 calc %0.2fs tokens %0.2fs" % (t1-t0, t2-t1))

    def dump(self) -> bytes:
        import pickle
        enc = self.enc
        self.enc = None
        if self.diff is not None:
            self.diff.enc = None
        if self.diff_out is not None:
            self.diff_out.enc = None
        d = pickle.dumps(self)
        self.enc = enc
        if self.diff is not None:
            self.diff.enc = enc
        if self.diff_out is not None:
            self.diff_out.enc = enc
        return d

    def set_enc(self, enc: RefactEncoding):
        self.enc = enc
        if self.diff is not None:
            self.diff.enc = enc
        if self.diff_out is not None:
            self.diff_out.enc = enc


# from itertools import product

# n,m = map(int, input().split())
# nums = input().split()
# nums.sort(key=lambda x:int(x))
# ans = [0] * m
# visited = set()

# for p in product(nums, repeat=m):
#     s = ' '.join(p)
#     if s not in visited:
#         visited.add(s)
#         print(s)

def permutation(cnt):
    if cnt == m:
        str_ans = ''.join(map(str,ans))
        if str_ans not in dic:
            dic[str_ans] = True
            print(*ans)
        return

    for i in range(n):
        ans[cnt] = nums[i]
        permutation(cnt+1)

n,m = map(int, input().split())
nums = sorted(list(map(int, input().split())))
ans = [0] * m
visited = [False] * n
dic = {}

permutation(0)


import pytest
from huma_utils import chain_utils

from huma_signals import exceptions
from huma_signals.adapters.ethereum_wallet import adapter as ethereum_wallet_adapter
from huma_signals.adapters.polygon_wallet import adapter as polygon_wallet_adapter
from huma_signals.adapters.request_network import request_invoice_adapter
from huma_signals.clients.request_client import request_client, request_types
from tests.fixtures.adapters import (
    fake_ethereum_wallet_adapter,
    fake_polygon_wallet_adapter,
)
from tests.fixtures.clients.request import fake_request_client, request_type_factories
from tests.helpers import address_helpers

def describe_RequestInvoiceAdapter() -> None:
    @pytest.fixture
    def payer_wallet_address() -> str:
        return address_helpers.fake_hex_address()

    @pytest.fixture
    def payee_wallet_address() -> str:
        return address_helpers.fake_hex_address()

    @pytest.fixture
    def payee_wallet_tenure() -> int:
        return 42

    @pytest.fixture
    def payer_wallet_tenure() -> int:
        return 24

    @pytest.fixture
    def request_id() -> str:
        return address_helpers.fake_hex_address()

    @pytest.fixture
    def invoice(
        payer_wallet_address: str, payee_wallet_address: str
    ) -> request_types.Invoice:
        return request_type_factories.InvoiceFactory.create(
            payer=payer_wallet_address,
            payee=payee_wallet_address,
            token_owner=payee_wallet_address,
        )

    @pytest.fixture
    def request_client_(
        invoice: request_types.Invoice,
    ) -> fake_request_client.FakeRequestClient:
        return fake_request_client.FakeRequestClient(invoice=invoice)

    @pytest.fixture
    def adapter(
        request_client_: request_client.BaseRequestClient,
        wallet_adapter: ethereum_wallet_adapter.BaseEthereumWalletAdapter
        | polygon_wallet_adapter.BasePolygonWalletAdapter,
        chain: chain_utils.Chain,
    ) -> request_invoice_adapter.RequestInvoiceAdapter:
        return request_invoice_adapter.RequestInvoiceAdapter(
            request_client_=request_client_,
            wallet_adapter=wallet_adapter,
            chain=chain,
        )

    def describe_fetch() -> None:
        def with_eth_chain() -> None:
            @pytest.fixture
            def chain() -> chain_utils.Chain:
                return chain_utils.Chain.ETHEREUM

            @pytest.fixture
            def signals(
                payee_wallet_address: str,
                payer_wallet_address: str,
                payee_wallet_tenure: int,
                payer_wallet_tenure: int,
            ) -> dict[str, ethereum_wallet_adapter.EthereumWalletSignals]:
                return {
                    payee_wallet_address: fake_ethereum_wallet_adapter.EthereumWalletSignalsFactory.create(
                        wallet_tenure_in_days=payee_wallet_tenure
                    ),
                    payer_wallet_address: fake_ethereum_wallet_adapter.EthereumWalletSignalsFactory.create(
                        wallet_tenure_in_days=payer_wallet_tenure
                    ),
                }

            @pytest.fixture
            def wallet_adapter(
                signals: dict[str, ethereum_wallet_adapter.EthereumWalletSignals]
            ) -> ethereum_wallet_adapter.BaseEthereumWalletAdapter:
                return fake_ethereum_wallet_adapter.FakeEthereumWalletAdapter(
                    output_signals=signals
                )

            async def it_fetches_the_signals(
                adapter: request_invoice_adapter.RequestInvoiceAdapter,
                payee_wallet_address: str,
                payer_wallet_tenure: int,
                payee_wallet_tenure: int,
                request_id: str,
                invoice: request_types.Invoice,
            ) -> None:
                signals = await adapter.fetch(
                    borrower_wallet_address=payee_wallet_address,
                    receivable_param=request_id,
                )
                assert signals.payer_count == 10
                assert signals.payee_count == 10
                assert signals.mutual_count == 0
                assert signals.payer_tenure == payer_wallet_tenure
                assert signals.payee_tenure == payee_wallet_tenure
                assert signals.payee_match_borrower
                assert signals.borrower_own_invoice
                assert signals.payer_match_payee is False
                assert signals.invoice_amount == invoice.amount
                assert signals.token_id == invoice.token_id

            def when_payee_is_not_the_borrower() -> None:
                async def it_returns_false_for_the_signal_field(
                    adapter: request_invoice_adapter.RequestInvoiceAdapter,
                    request_id: str,
                ) -> None:
                    signals = await adapter.fetch(
                        borrower_wallet_address=address_helpers.fake_hex_address(),
                        receivable_param=request_id,
                    )
                    assert signals.payee_match_borrower is False

            def when_the_borrower_does_not_own_the_invoice() -> None:
                @pytest.fixture
                def invoice() -> request_types.Invoice:
                    return request_type_factories.InvoiceFactory.create()

                async def it_returns_false_for_the_signal_field(
                    adapter: request_invoice_adapter.RequestInvoiceAdapter,
                    payee_wallet_address: str,
                    request_id: str,
                ) -> None:
                    signals = await adapter.fetch(
                        borrower_wallet_address=payee_wallet_address,
                        receivable_param=request_id,
                    )
                    assert signals.borrower_own_invoice is False

            def when_payer_and_payee_have_the_same_address() -> None:
                @pytest.fixture
                def payee_wallet_address(payer_wallet_address: str) -> str:
                    return payer_wallet_address

                async def it_returns_true_for_the_signal_field(
                    adapter: request_invoice_adapter.RequestInvoiceAdapter,
                    payee_wallet_address: str,
                    request_id: str,
                ) -> None:
                    signals = await adapter.fetch(
                        borrower_wallet_address=payee_wallet_address,
                        receivable_param=request_id,
                    )
                    assert signals.payer_match_payee

            def with_invalid_borrower_address() -> None:
                @pytest.fixture
                def payee_wallet_address() -> str:
                    return "0xabc"

                async def it_throws_error(
                    adapter: request_invoice_adapter.RequestInvoiceAdapter,
                    payee_wallet_address: str,
                    request_id: str,
                ) -> None:
                    with pytest.raises(exceptions.InvalidAddressException):
                        await adapter.fetch(
                            borrower_wallet_address=payee_wallet_address,
                            receivable_param=request_id,
                        )

        def with_polygon_chain() -> None:
            @pytest.fixture
            def chain() -> chain_utils.Chain:
                return chain_utils.Chain.POLYGON

            @pytest.fixture
            def signals(
                payee_wallet_address: str,
                payer_wallet_address: str,
                payee_wallet_tenure: int,
                payer_wallet_tenure: int,
            ) -> dict[str, polygon_wallet_adapter.PolygonWalletSignals]:
                return {
                    payee_wallet_address: fake_polygon_wallet_adapter.PolygonWalletSignalsFactory.create(
                        wallet_tenure_in_days=payee_wallet_tenure
                    ),
                    payer_wallet_address: fake_polygon_wallet_adapter.PolygonWalletSignalsFactory.create(
                        wallet_tenure_in_days=payer_wallet_tenure
                    ),
                }

            @pytest.fixture
            def wallet_adapter(
                signals: dict[str, polygon_wallet_adapter.PolygonWalletSignals]
            ) -> polygon_wallet_adapter.BasePolygonWalletAdapter:
                return fake_polygon_wallet_adapter.FakePolygonWalletAdapter(
                    output_signals=signals
                )

            async def it_fetches_the_signals(
                adapter: request_invoice_adapter.RequestInvoiceAdapter,
                payee_wallet_address: str,
                payer_wallet_tenure: int,
                payee_wallet_tenure: int,
                request_id: str,
            ) -> None:
                signals = await adapter.fetch(
                    borrower_wallet_address=payee_wallet_address,
                    receivable_param=request_id,
                )
                assert signals.payer_count == 10
                assert signals.payee_count == 10
                assert signals.mutual_count == 0
                assert signals.payer_tenure == payer_wallet_tenure
                assert signals.payee_tenure == payee_wallet_tenure

            def with_invalid_borrower_address() -> None:
                @pytest.fixture
                def payee_wallet_address() -> str:
                    return "0xabc"

                async def it_throws_error(
                    adapter: request_invoice_adapter.RequestInvoiceAdapter,
                    payee_wallet_address: str,
                    request_id: str,
                ) -> None:
                    with pytest.raises(exceptions.InvalidAddressException):
                        await adapter.fetch(
                            borrower_wallet_address=payee_wallet_address,
                            receivable_param=request_id,
                        )


#!/usr/bin/env python3
l = dict([i.strip().split(')')[::-1] for i in open('input6.txt').readlines()])
dist = {}
def getDist(o):
    if o == 'COM':
        return 0
    if o in dist:
        return dist[o]
    else:
        dist[o] = getDist(l[o]) + 1
        return dist[o]

print(sum([getDist(x) for x in l.keys()]))


import logging
import os
import shutil

def copytree(src, dst, symlinks=False, ignore=None):
    for item in os.listdir(src):
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if os.path.isdir(s):
            shutil.copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)

def edit_metadata(file_name, translate_dict):
    try:
        if not file_name.startswith('.'):
            with open(file_name, "r") as f:
                content = f.read()
            for key, value in translate_dict.items():
                content = content.replace(key, value)

            with open(file_name, "w") as f:
                f.write(content)
    except:
        logging.warning(f"Could not edit {file_name}")

def default_input(prompt, default):
    res = input(prompt)
    if res == "":
        return default
    return res

def non_empty_check(text):
    if text == "":
        raise ValueError("This value cannot be empty")
    return text

def init_project():
    destination = os.getcwd()  # exec place
    data_path = os.path.join(os.path.dirname(__file__), "template")

    name = non_empty_check(input("Project name:"))
    description = input("Project decription:")
    version = default_input("Version [default = 0.0.1]:", "0.0.1")
    author = non_empty_check(input("Author:"))
    email = input("Email:")
    destination = destination + "/" + name
    os.makedirs(destination)
    copytree(data_path, destination)

    translate_dict = {
        "$PROJECT_NAME$": name,
        "$PROJECT_VERSION$": version,
        "$PROJECT_AUTHOR$": author,
        "$PROJECT_EMAIL$": email,
        "$PROJECT_DESCRIPTION$": description,
    }
    os.rename(f"{destination}/src/src_folder", f"{destination}/src/{name}")
    for root, _, files in os.walk(destination):
        for file in files:
            edit_metadata(os.path.join(root, file), translate_dict)

    print("Project installed successfully!")
    print("To install the project, run:")
    print(f"    cd {name}")
    print(f"    pip install -e .")
    print("Fill the README.md, good coding!")


import json
from flask import Flask
from flask import render_template

from flask import Flask, request,redirect,url_for,jsonify

app= Flask(__name__)

@app.route('/')
def index():
    users=['rose','jasmine','lily']
    return render_template('index.html',title='welcome',usernames=users)

@app.route('/hello')
def hello_world():
    return jsonify({'name':'rose','email':'rose@rose.com'})

@app.route('/login',methods =['POST','GET'])
def login():
    if request.method=='POST':
        user= request.form['name']
        return redirect(url_for('dashboard',name=user))
    else:
        user1=request.args.get('name')
        return render_template('login.html')

@app.route('/dashboard/<name>')
def dashboard(name):
    return "Welcome" + name

@app.route('/sale/<transaction_id>')
def displaytid(transaction_id=0):
    return "The Transaction id is :" +str(transaction_id)

@app.route('/json')
def readjsonvariable():
    person='{"name":"mudassar","languages":["english","hindi"]}'
    person_dict=json.loads(person)
    if "english" in person_dict['languages']:
        return person_dict['name'] + " knows english"
    else :
        return person_dict['name'] + " does not know english"

@app.route('/jsonfile',methods=['GET'])
def readfromjsonfile():
    with open('data/person.json') as f:
        persondict=json.load(f)
        if "english" in persondict['languages']:
            response=json.dumps(persondict)
            return response
        else:
            return "does not have english"

@app.route('/testjsonfile')
def testjsonfile():
    with open('data/person.json') as f:
        datadict=json.load(f)
        return json.dumps(datadict['languages'])

app.run(host='0.0.0.0', port=3000)


import numpy as np
import random

class qmem(object):
    def __init__(self, maxsize=10000, margin=100, alpha=1):
        self.s = None
        self.a = None
        self.new_s = None
        self.r = None
        self.done = None
        self.v = None

        self.maxsize = maxsize
        self.margin = margin
        self.alpha= alpha #for importance sampling
        self.size = 0

    def add_instance(self, state, action, reward, new_state, done):
        if self.size == 0:
            self.s = state[np.newaxis, :]
            self.a = action
            self.r = reward
            self.new_s = new_state[np.newaxis, :]
            self.done = done
            self.v = 0
        else:
            self.s = np.r_[self.s, state[np.newaxis, :]]
            self.a = np.r_[self.a, action]
            self.r = np.r_[self.r, reward]
            self.new_s = np.r_[self.new_s, new_state[np.newaxis, :]]
            self.done = np.r_[self.done, done]
            self.v = np.r_[self.v, 0]

        self.size +=1

        # print self.s
        # print self.a
        # raw_input("Countinue...")

        if self.size>self.maxsize:
            self.del_instances(range(self.margin))

    def del_instances(self, idx):
        # idx is a nump vec of index
        self.s = np.delete(self.s, idx, axis=0)
        self.new_s = np.delete(self.new_s, idx, axis=0)
        self.a = np.delete(self.a, idx)
        self.r = np.delete(self.r, idx)
        self.done = np.delete(self.done, idx)
        self.v = np.delete(self.v, idx)
        self.size -= len(idx)

    def sampling(self, n, importance=False):
        idx = None
        if importance:
            samp_p = self._calculate_importance()
            idx = np.random.choice(self.size, size=n, p=samp_p)
        else:
            idx = np.random.randint(self.size, size=n)
        s = self.s[idx, :]
        new_s = self.new_s[idx, :]
        a = self.a[idx]
        r = self.r[idx]
        done = self.done[idx]
        self.v[idx] += 1

        return (s, a, r, new_s, done)

    def _calculate_importance(self):
        p = (1.0/ (self.v + 1.0))**self.alpha
        return p/np.sum(p)


import tempfile
from fastapi import UploadFile
from pathlib import Path
import shutil
from typing import Callable
from typing import Tuple
from PIL import Image as imageMain
from PIL.Image import Image
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
import math
from PIL import ImageFont, ImageDraw

# scikit-image
from skimage.exposure import is_low_contrast
# brisque score
import imquality.brisque as brisque

import uuid
from matplotlib import pyplot as plt
import sys

class OptionWatermark:
    def __init__(self, text: str):
        self.text = text

class ImageHandling:
    tempfile.tempdir = "/tmp"

    #constructor
    def __init__(self):
        pass

    def getImageFromTmp():
        pass

    def saveImageToTmp(upload_file: UploadFile) -> Path:
        try:
            suffix = Path(upload_file.filename).suffix
            with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
                shutil.copyfileobj(upload_file.file, tmp)
                tmp_path = Path(tmp.name)
        finally:
            upload_file.file.close()
        return tmp_path

    # clean up 1 image
    def cleanSingleImage(self, filePathWithName: str):
        try:
            isDeleted = False
            if os.path.exists(filePathWithName):
                os.remove(filePathWithName)
                isDeleted = True
        except os.error as e:
            print("Cannot delete certain image file with error: {}".format(e))
        return isDeleted

    # clean up 1 directory
    def cleanSingleDirectory(self, directoryPath: str):
        try:
            isDeleted = False
            if os.path.exists(directoryPath):
                shutil.rmtree(directoryPath)
                isDeleted = True
        except os.error as e:
            print("Cannot delete directory with error: {}".format(e))
        return isDeleted

    # create 1 directory
    def createSingleDirectory(self, directoryPath: str):
        try:
            isCreated = False
            if os.path.exists(directoryPath) is False:
                os.mkdir(directoryPath)
                isCreated = True
        except os.error as e:
            print("Cannot Create directory with error: {}".format(e))
        return isCreated

    def cleanTmpFolder():
        folder = '/tmp'
        for filename in os.listdir(folder):
            file_path = os.path.join(folder, filename)
            try:
                if os.path.isfile(file_path) or os.path.islink(file_path):
                    os.unlink(file_path)
                elif os.path.isdir(file_path):
                    shutil.rmtree(file_path)
                print("Clean tmp/ folder completed!")
            except Exception as e:
                print('Failed to delete %s. Reason: %s' % (file_path, e))
        pass

    def handleUploadFile(
        self,
        upload_file: UploadFile, handler: Callable[[Path], None]
    ) -> None:
        tmp_path = self.saveImageToTmp(upload_file)
        try:
            handler(tmp_path)  # Do something with the saved temp file
        finally:
            tmp_path.unlink()  # Delete the temp file

    def printTmpDir():
        return tempfile.gettempdir()

    # TODO: Refactor for images handling, image rotation, paint over border, extract contours
    def openImagePil(imagePath: str) -> Image:
        return imageMain.open(imagePath)

    def convertPilImageToCvImage(pilImage: Image):
        return cv2.cvtColor(np.array(pilImage), cv2.COLOR_RGB2BGR)

    def convertCvImagetToPilImage(cvImage) -> Image:
        return imageMain.fromarray(cv2.cvtColor(cvImage, cv2.COLOR_BGR2RGB))

    def openImageCv(imagePath: str):
        return ImageHandling.convertPilImageToCvImage(ImageHandling.openImagePil(imagePath))

    def cvToGrayScale(cvImage):
        return cv2.cvtColor(cvImage, cv2.COLOR_BGR2GRAY)

    def cvApplyGaussianBlur(cvImage, size: int):
        return cv2.GaussianBlur(cvImage, (size, size), 0)

    # Extracts all contours from the image, and resorts them by area (from largest to smallest)
    def cvExtractContours(cvImage):
        contours, hierarchy = cv2.findContours(cvImage, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
        contours = sorted(contours, key = cv2.contourArea, reverse = True)
        return contours

    # Apply new color to the outer border of the image
    def paintOverBorder(cvImage, borderX: int, borderY: int, color: Tuple[int, int, int]):
        newImage = cvImage.copy()
        height, width, channels = newImage.shape
        for y in range(0, height):
            for x in range(0, width):
                if (y <= borderY) or (height - borderY <= y):
                    newImage[y, x] = color
                if (x <= borderX) or (width - borderX <= x):
                    newImage[y, x] = color
        return newImage

    # Rotate the image around its center
    def rotateImage(cvImage, angle: float):
        newImage = cvImage.copy()
        (h, w) = newImage.shape[:2]
        center = (w // 2, h // 2)
        M = cv2.getRotationMatrix2D(center, angle, 1.0)
        newImage = cv2.warpAffine(newImage, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
        cv2.imwrite("../../test-data/hoho.png", newImage)
        return newImage

    def removeBackground(imagePath) :
        image = cv2.imread(str(imagePath))
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        blur = cv2.GaussianBlur(gray, (3,3), 0)
        thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
        result = cv2.bitwise_and(image, image, mask=thresh)
        result[thresh==0] = [255,255,255]
        strUUID = uuid.uuid4().hex
        bg = '%s/%s.png' % (tempfile.gettempdir(), strUUID)
        cv2.imwrite(bg, result)
        return bg

    def isBlur(self, filePath, size=60, thresh=10, vis=False):
        image = cv2.imread(str(filePath))
        # is blur
    	# grab the dimensions of the image and use the dimensions to
        # derive the center (x, y)-coordinates
        (h, w, _) = image.shape
        (cX, cY) = (int(w / 2.0), int(h / 2.0))
        # compute the FFT to find the frequency transform, then shift
        # the zero frequency component (i.e., DC component located at
        # the top-left corner) to the center where it will be more
        # easy to analyze
        fft = np.fft.fft2(image)
        fftShift = np.fft.fftshift(fft)
        # check to see if we are visualizing our output
        if vis:
            # compute the magnitude spectrum of the transform
            magnitude = 20 * np.log(np.abs(fftShift))
            # display the original input image

            (fig, ax) = plt.subplots(1, 2, )
            ax[0].imshow(image, cmap="gray")
            ax[0].set_title("Input")
            ax[0].set_xticks([])
            ax[0].set_yticks([])

            # display the magnitude image
            ax[1].imshow(magnitude, cmap="gray")
            ax[1].set_title("Magnitude Spectrum")
            ax[1].set_xticks([])
            ax[1].set_yticks([])

        # zero-out the center of the FFT shift (i.e., remove low
        # frequencies), apply the inverse shift such that the DC
        # component once again becomes the top-left, and then apply
        # the inverse FFT
        fftShift[cY - size:cY + size, cX - size:cX + size] = 0
        fftShift = np.fft.ifftshift(fftShift)
        recon = np.fft.ifft2(fftShift)
        # compute the magnitude spectrum of the reconstructed image,
        # then compute the mean of the magnitude values
        magnitude = 20 * np.log(np.abs(recon))
        mean = np.mean(magnitude)
        # the image will be considered "blurry" if the mean value of the
        # magnitudes is less than the threshold value
        return (mean <= thresh)

    def isNoise(self, filePath):
        image = cv2.imread(str(filePath))
        # Convert image to HSV color space
        rgb2bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        bgr2hsv = cv2.cvtColor(rgb2bgr, cv2.COLOR_BGR2HSV)

        # Calculate histogram of saturation channel
        s = cv2.calcHist([bgr2hsv], [1], None, [256], [0, 256])

        # Calculate percentage of pixels with saturation >= p
        p = 0.05
        s_perc = np.sum(s[int(p * 255):-1]) / np.prod(bgr2hsv.shape[0:2])

        # Percentage threshold; above: valid image, below: noise
        s_thr = 0.5
        return s_perc > s_thr

    def isLowContrast(self, filePath):
        img = cv2.imread(str(filePath))
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        return is_low_contrast(gray, fraction_threshold=0.3)

    def badImageFilter():
        # nudity images
        pass

    def isInWrongSkew():
        pass

    def brisqueScore(self, filePath):
        img = cv2.imread(str(filePath))
        brisqueScore: float = brisque.score(img)
        return brisqueScore

    # Adaptive histogram equalization
    def AdaptiveHistogramEqualization(self, img):
        clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
        cl1 = clahe.apply(img)
        return cl1

    def addWatermark(imgPath: str, op: OptionWatermark):
        try:
            path = os.path.dirname(__file__)
            # --- original image ---
            originalImage = imageMain.open(imgPath).convert("RGBA")
            width, height = originalImage.size

            # --- text image ---
            filePath = os.path.join(path, '../../fonts', 'impact.ttf')
            font = ImageFont.truetype(filePath, 40)

            # calculate text size in pixels (width, height)
            textSize = font.getsize(op.text)

            # create image for text
            textImage = imageMain.new('RGBA', size=textSize, color=(255,255,255,0))
            textDraw = ImageDraw.Draw(textImage)

            # draw text and color on images
            textDraw.text((0, 0), text=op.text, fill=(255, 255, 255, 129), font=font) # fill="#F5F5F5"

            # rotate text image and fill with transparent color
            rotatedTextImage = textImage.rotate(45, expand=True, fillcolor=(0,0,0,0))

            rotatedTextImageWith, rotatedTextImageHeight = rotatedTextImage.size

            # calculate top/left corner for centered text
            parts = 6
            offsetX = width//parts
            offsetY = height//parts

            startX = width//parts - rotatedTextImageWith//2
            startY = height//parts - rotatedTextImageHeight//2

            for a in range(0, parts, 2):
                for b in range(0, parts, 2):
                    x = startX + a*offsetX
                    y = startY + b*offsetY
                    # image with the same size and transparent color (..., ..., ..., 0)
                    watermarksImage = imageMain.new('RGBA', size=(width, height), color=(255,255,255,0))
                    # put text in expected place on watermarks image
                    watermarksImage.paste(rotatedTextImage, (x, y))
                    # put watermarks image on original image
                    originalImage = imageMain.alpha_composite(originalImage, watermarksImage)

            originalImage = originalImage.convert('RGB')
            originalImage.save(imgPath)

            return imgPath
        except:
            print("Errors addWatermark: {}".format(sys.exc_info()))


from pathlib import Path
import logging
import os

BOT_TOKEN = os.environ["TELEGRAM_BOT_TOKEN"]  # change here!
USE_TOR_SOCKS = True  # change here
FILES_FOLDER = Path('.') / 'files'
if not FILES_FOLDER.exists():
    FILES_FOLDER.mkdir()

logging.basicConfig(
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        level=logging.INFO,
)


import pygame
import random
from Pawns import *

# Player Constructor Class
class Player():
    def __init__(self, name, color, pawns):
        self.name = name
        self.color = color
        # not yet implemented kills attribute
        #self.kills = 0
        # the list of pawns that belong to this player
        self.pawnlist = pawns
        # for tracking how many pawns are currently in play
        self.pawns = 0
        # self.active represents whether the player is on the board or not
        self.active = False
        # self.turn represents whether it is the player's turn
        self.turn = False
        #set player objects Boolean variables
        self.Player1 = False
        self.Player2 = False
        self.Player3 = False
        self.Player4 = False
        #set player number based on name upon object initialization
        self.set_player_number()

    #this may be more useful later for naming and referencing purposes
    def set_player_number(self):
         if self.name == 'Player1':
             # if we set an attribute named after the player number, and set it to true, we can simply write if self.Player1 to check if an object on a list in a for loop is this specific player
             # this is also useful to determine if this instance of a player object is the one we intend to work with
            self.Player1 = True
         elif self.name == 'Player2':
            self.Player2 = True
         elif self.name == 'Player3':
            self.Player3 = True
         elif self.name == 'Player4':
            self.Player4 = True

    # this method is called before the mainloop is initialized
    def set_statekeeper(self, statekeeper):
        #set statekeeper object as an attribute
        self.Statekpr = statekeeper
        #set the list of players as an attribute
        self.players = self.Statekpr.players
        # set self.player attribute to refer to the specific object it is, this can be very useful, all you have to do is write self.Player to refer to this specific instance of player object.
        if self.Player1:
             self.Player = self.Statekpr.playerRed
        elif self.Player2:
             self.Player = self.Statekpr.playerBlue
        elif self.Player3:
             self.Player = self.Statekpr.playerYellow
        elif self.Player4:
             self.Player = self.Statekpr.playerGreen

    #ensure player attributes are up to date and represent the current status
    def update_self(self):
        if self.Player1:
            # self.active is true if this player is active
            self.active = self.Statekpr.redActive
            # self.turn is true if it is currently this player's turn
            self.turn = self.Statekpr.redTurn
        elif self.Player2:
            self.active = self.Statekpr.blueActive
            self.turn = self.Statekpr.blueTurn
        elif self.Player3:
            self.active = self.Statekpr.yellowActive
            self.turn = self.Statekpr.yellowTurn
        elif self.Player4:
            self.active = self.Statekpr.greenActive
            self.turn = self.Statekpr.greenTurn

    # ensure the statekeeper is updated with the latest attribute statuses
    def update_statekeeper(self):
        if self.Player1:
            self.Statekpr.redActive = self.active
            self.Statekpr.redTurn = self.turn
        elif self.Player2:
            self.Statekpr.blueActive = self.active
            self.Statekpr.blueTurn = self.turn
        elif self.Player3:
            self.Statekpr.yellowActive = self.active
            self.Statekpr.yellowTurn = self.turn
        elif self.Player4:
            self.Statekpr.greenActive = self.active
            self.Statekpr.greenTurn = self.turn
        # self.activeplayer refers to the currently active player who's turn it is
        self.Statekpr.activeplayer = self.activeplayer
        # self.nextplayer refers to the player who's turn is next
        self.Statekpr.nextplayer = self.nextplayer

        # update the activeplayer attribute to the current active player
        # update the nextplayer attribute to player who will be active next
        # this method is called in the player Turn Method
    def update_active_and_next(self):
        self.activeplayer = self.Statekpr.activeplayer
        self.nextplayer = self.set_next_player()

    def set_next_player(self):
        # for each of the 4 players,
        for i in range(0,4):
            # if the active player is indexed in the players list at the current value of i
            if self.activeplayer == self.players[i]:
                # take the current index of the players list that equals the activeplayer and add 1 to the value
                nextnumber = i + 1
                # check if adding 1 exceeds the range of the index
                if nextnumber < 4:
                    # if it does not exceed, then the next player is the located at the next number of the index
                    nextplayer = self.players[nextnumber]
                else:
                    #if it does exceed the range of the list, then the next player is at index 0.
                    nextplayer = self.players[0]
        # pass the next player object from this method to a variable
        return nextplayer

    def dice_roll(self):
        # randomly determine a value from 1 through and including 6
        rand = random.randint(1, 6)
        print(rand)
        #pass the value from this method to a variable
        return rand

    #attempt to move out of the starting location by rolling dice
    def move_out_onto_the_board(self):
        # "roll the dice" and assign it to the roll variable
        roll = self.dice_roll()
        # if a 6 is rolled, then move out onto the board
        if roll == 6:
            #increment the value of active pawns belonging to the player
            self.pawns += 1
            #player is now considered active
            self.active = True
            #ensure the statekeeper is current
            self.update_statekeeper()
            #run the move active player pawn method
            self.move()

    #The method that runs when it is a player's turn
    def Turn(self):
        #ensure the active and next player attributes reflect the current status
        self.update_active_and_next()
        # if the player has no active pawns then attempt to move one from the start area
        if self.pawns < 1:
            self.move_out_onto_the_board()
        else:
            # else just move the active player pawn
            self.move()

    #the method for moving the active players active pawn
    def move(self):
        #roll dice and assign the movement distance to a variable
        distance = self.dice_roll()
        #loop through pawns in the players group to check for active pawn
        for pawn in self.pawnlist:
             #check which pawn is active
            if pawn.activepawn:
                #move active pawn according to dice roll and pass distance integer variable and the statekeeper object
                pawn.move(distance, self.Statekpr)
                #update pawn states
                pawn.update_pawn_state(self.activeplayer, self.nextplayer)
                #active pawn found, moved and updated so loop can stop
                break
            #keep looping until active pawn found
            else:
                continue
        #ensure the statekeeper is kept current
        self.update_statekeeper()


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import open3d as o3d
import os, sys, glob
import laspy as lp
from dbfread import DBF
from scipy.stats import chisquare
from time import process_time
from scipy import interpolate
import pyrr
from numpy import genfromtxt
from mpl_toolkits.mplot3d import Axes3D
from plyfile import PlyData, PlyElement

__author__ = 'Omar A. Ruiz Macias'
__copyright__ = 'Copyright 2021, PLANTTECH'
__version__ = '0.1.0'
__maintainer__ = 'Omar A. Ruiz Macias'
__email__ = 'omar.ruiz.macias@gmail.com'
__status__ = 'Dev'

# Global
basedir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
_py = os.path.join(basedir, 'py')
_data = os.path.join(basedir, 'data')
_images = os.path.join(basedir, 'images')

def numpy2npy(mockname, downsample=None):
    '''
    Convert Blensor output txt files that have fake numpy
    extension to real npy.
    '''

    datapath = os.path.join(_data, mockname)

    # read the numpy files
    for file in glob.glob(os.path.join(datapath, 's*.numpy')):
        df = np.loadtxt(file)
        filename = file.split('/')[-1].split('.')[0]
        print('%s done --> Number of beams: %i' %(file.split('/')[-1], len(df)))
        outfile = os.path.join(_data, mockname, filename)

        if downsample is not None:
            keep = np.random.randint(0, len(df), int(len(df) * downsample))
            # print(filename, len(df), len(df[keep]))
            np.save(outfile, df[keep])
        else:
            np.save(outfile, df)

def csv2npy(mockname, downsample=None):
    '''
    Convert GroIMP point cloud output files into real npy
    '''
    datapath = os.path.join(_data, mockname)
    # read the numpy files
    for file in glob.glob(os.path.join(datapath, '*.csv')):
        df = np.loadtxt(file, delimiter=',',
                 dtype={'names':('x', 'y', 'z'),
                 'formats':('f4', 'f4', 'f4')})
        #df = np.genfromtxt(file, delimiter=',', encoding=None, names=False,
                        #    dtype= float)
        #df = np.loadtxt(file)
       # print(df)

        filename = file.split('/')[-1].split('.')[0]

        print('%s done --> Number of beams: %i' %(file.split('/')[-1], len(df)))
        outfile = os.path.join(_data, mockname, filename)

        if downsample is not None:
            keep = np.random.randint(0, len(df), int(len(df) * downsample))
            # print(filename, len(df), len(df[keep]))
            np.save(outfile, df[keep])
        else:
            np.save(outfile, df)

def load_scan_pos(filename):
    '''
    Read file with sensor positions.
    '''

    scan = np.loadtxt(filename, delimiter=',', dtype={'names':('scan', 'x', 'y', 'z'),
                'formats':('S4', 'f4', 'f4', 'f4')})

    return scan

def npy2pandas(mockname, downsample=None):
    '''
    Read the npy files and merge in a pandas dataframe.
    This function also adds the information of the sensor
    coordinates called the bia (beam inclination angle) positions.
    '''

    datapath = os.path.join(_data, mockname)

    # read the numpy files
    files = {}
    bia_pos = {}
    N = len(glob.glob(os.path.join(datapath, 's*.npy')))
    print('Number of files: %i' %(N))
    for file in glob.glob(os.path.join(datapath, 's*.npy')):
        df = np.load(file)
        filename = file.split('/')[-1]

        if downsample is not None:
            keep = np.random.randint(0, len(df), int(len(df) * downsample))
            df = df[keep]

        # sensor coordinates
        spos = os.path.join(datapath, 'scanner_pos.txt')
        scan = load_scan_pos(spos)
        id = [i.decode("utf-8") for i in scan['scan']]
        keep = np.array(id) == filename[:3]
        _, sx, sy, sz = scan[keep][0]

        if df.shape[0] > 0:
            files[filename] = df
            bia = np.full((len(df),3), np.array([sx, sy, sz]))
            bia_pos[filename] = bia
            # print(filename, df.shape, bia.shape)
        else:
            print('file %s empty' %(filename))

    # concatenate all data
    # if len(list(files.keys())) < 2:
        # print(list(files.values()))
    #     df = list(files.values())[0]
    #     bias = list(bia_pos.values())[0]
    # else:
    df = np.concatenate(list(files.values()))
    bias = np.concatenate(list(bia_pos.values()))

    # pass this to a pandas data frame for simplicity
    scan = pd.DataFrame(df, columns=['timestamp', 'yaw', 'pitch', 'distance','distance_noise',
                                    'x','y','z',
                                    'x_noise','y_noise','z_noise',
                                    'object_id', 'color0', 'color1','color2', 'idx'])

    bia = pd.DataFrame(bias, columns=['sx', 'sy', 'sz'])

    # concat
    scan = pd.concat((scan, bia), axis=1)

    return scan

def plynpy2pandas(mockname, downsample=None):
    '''
    Read the npy files and merge in a pandas dataframe.
    This function also adds the information of the sensor
    coordinates called the bia (beam inclination angle) positions.
    '''

    datapath = os.path.join(_data, mockname)

    # read the numpy files
    files = {}
    bia_pos = {}
    N = len(glob.glob(os.path.join(datapath, '*.npy')))
    print('Number of files: %i' %(N))
    for file in glob.glob(os.path.join(datapath, '*.npy')):
        df = np.load(file, allow_pickle=True)
        filename = file.split('/')[-1]

        if downsample is not None:
            keep = np.random.randint(0, len(df), int(len(df) * downsample))
            df = df[keep]

        # sensor coordinates
        spos = os.path.join(datapath, 'scanner_pos.txt')
        scan = load_scan_pos(spos)
        #id = [i.decode("utf-8") for i in scan['scan']]

        _, sx, sy, sz = scan[0]

        if df.shape[0] > 0:
            files[filename] = df
            bia = np.full((len(df),3), np.array([sx, sy, sz]))
            bia_pos[filename] = bia
            # print(filename, df.shape, bia.shape)
        else:
            print('file %s empty' %(filename))

    # concatenate all data
    # if len(list(files.keys())) < 2:
        # print(list(files.values()))
    #     df = list(files.values())[0]
    #     bias = list(bia_pos.values())[0]
    # else:
    df = np.concatenate(list(files.values()))
    bias = np.concatenate(list(bia_pos.values()))

    df2 = pd.DataFrame(df, columns=[ 'x','y','z'])
    new_index = ['timestamp',
    'yaw', 'pitch', 'distance','distance_noise',
    'x','y','z','x_noise','y_noise','z_noise',
    'object_id', 'color0', 'color1','color2', 'idx']
    scan = df2.reindex(columns= new_index, fill_value=0)

    # pass this to a pandas data frame for simplicity
   # scan = pd.DataFrame(df, columns=['timestamp', 'yaw', 'pitch', 'distance','distance_noise',
   #                                 'x','y','z',
   #                                 'x_noise','y_noise','z_noise',
   #                                 'object_id', 'color0', 'color1','color2', 'idx'])

    bia = pd.DataFrame(bias, columns=['sx', 'sy', 'sz'])

    # concat
    scan = pd.concat((scan, bia), axis=1)

    return scan

def points2pcd(points, colors=None):
    '''
    Numpy 3D-array to open3D PCD.
    '''

    pcd = o3d.geometry.PointCloud()
    pcd.points = o3d.utility.Vector3dVector(points)
    if colors is not None:

        if isinstance(colors, list):
            color = np.full_like(points, colors)
            pcd.colors = o3d.utility.Vector3dVector(color)
        else:
            pcd.colors = o3d.utility.Vector3dVector(colors)
    else:
        pcd.colors = o3d.utility.Vector3dVector(np.random.uniform(0, 1, size=np.asarray(pcd.points).shape))

    return pcd

def DF2array(df):

    # points = np.vstack((np.array(df['x']), np.array(df['y']), np.array(df['z']))).transpose()
    points = df.to_numpy()

    return points

def showPCfromDF(df):

    points = np.vstack((np.array(df['x']), np.array(df['y']), np.array(df['z']))).transpose()
    pcd = points2pcd(points)
    o3d.visualization.draw_geometries([pcd])

def extract_leaves(df, show=False):
    '''
    Extract leaves from dataframe.
    '''

    leaves = df['object_id'] == 1986094444.0
    if show:
        showPCfromDF(df[leaves])

    return leaves

def extract_leaves_2(df, show=False):
    '''
    Extract leaves from dataframe.
    '''

    leaves = np.ones(len(df['x']), dtype=bool)
    if show:
        showPCfromDF(df[leaves])

    return leaves

def loadlaz(name):

    filepath = os.path.join(_data, name, 'lidar.laz')
    las = lp.read(filepath)

    return las

def loaddbf(name):

    filepath = os.path.join(_data, name, 'trajectory.dbf')
    table = DBF(filepath, load=True)

    return table

def showPCDS(pointslist, colours):

    pcds = []

    for num, points in enumerate(pointslist):

        pcd = points2pcd(points)

        if isinstance(colours[num], list):
            color = np.full_like(points, colours[num])
            pcd.colors = o3d.utility.Vector3dVector(color)
        else:
            pcd.colors = o3d.utility.Vector3dVector(colours[num])

        pcds.append(pcd)

    o3d.visualization.draw_geometries(pcds)

def interp_traj(traj, gpstime):

    tck, u = interpolate.splprep([np.array(traj.x), np.array(traj.y), np.array(traj.z)], s=2)
    limits = traj['gpstime'].min(), traj['gpstime'].max()
    gpstimenorm = normgpstime(gpstime, limits)
    xs, ys, zs = interpolate.splev(gpstimenorm, tck)

    return xs, ys, zs

def normgpstime(gpstime, limits):
    '''
    Normalize the gpstime to run from zero to one.
    '''

    dd = limits[1] - limits[0]
    gpstimenorm = (gpstime - limits[0]) / dd

    return gpstimenorm

def coordsDF(las, traj):

    xs, ys, zs = interp_traj(traj, las.gps_time)
    df = np.vstack((las.x, las.y, las.z, xs, ys, zs)).transpose()
    df = pd.DataFrame(df, columns=['x', 'y', 'z', 'xs', 'ys', 'zs'])

    return df

def showbeams(df):

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    points = DF2array(df[['x', 'y', 'z']])
    sensors = DF2array(df[['xs', 'ys', 'zs']])

    for p1, p2 in zip(points, sensors):

        line = pyrr.line.create_from_points(p1, p2, dtype=None)
        ax.plot(*line.T.tolist(), lw=0.1)
        ax.scatter3D(*p1, c='g', s=2)
        ax.scatter3D(*p2, c='r', s=5)

def remove_outliers(a, b, nbins=100, bounds=(1, 99)):

    bins = np.linspace(a.min(), a.max(), nbins)
    keep = np.zeros(len(a), dtype=bool)
    res = []

    for i in range(len(bins)-1):

        mask = (a > bins[i]) & (a < bins[i+1])
        median = np.median(b[mask])
        low, upp = np.percentile(b[mask], bounds)
        mask &= (b > low) & (b < upp)
        res.append([(bins[i+1] + bins[i])/2, median, low, upp])

        plt.scatter(a[mask], b[mask], s=0.01)

        keep |= mask

    return res, keep

def load_lias_ws(mockname, treename):

    # Check if angles and weights are available
    outdir_angs = os.path.join(_data, mockname, 'lia', 'angles_%s.npy' %(treename))
    outdir_ws = os.path.join(_data, mockname, 'lia', 'weights_%s.npy' %(treename))

    lia = np.load(outdir_angs)
    ws = np.load(outdir_ws)

    return lia, ws


import os

from fuel_agent.utils import utils

# Please take a look at the linux kernel documentation
# https://github.com/torvalds/linux/blob/master/Documentation/devices.txt.
# KVM virtio volumes have major number 252 in CentOS, but 253 in Ubuntu.
VALID_MAJORS = (3, 8, 65, 66, 67, 68, 69, 70, 71, 104, 105, 106, 107, 108, 109,
                110, 111, 202, 252, 253)

# We are only interested in getting these
# properties from udevadm report
# MAJOR major device number
# MINOR minor device number
# DEVNAME e.g. /dev/sda
# DEVTYPE e.g. disk or partition for block devices
# DEVPATH path to a device directory relative to /sys
# ID_BUS e.g. ata, scsi
# ID_MODEL e.g. MATSHITADVD-RAM_UJ890
# ID_SERIAL_SHORT e.g. UH00_296679
# ID_WWN e.g. 0x50000392e9804d4b (optional)
# ID_CDROM e.g. 1 for cdrom device (optional)
UDEV_PROPERTIES = set(['MAJOR', 'MINOR', 'DEVNAME', 'DEVTYPE', 'DEVPATH',
                       'ID_BUS', 'ID_MODEL', 'ID_SERIAL_SHORT', 'ID_WWN',
                       'ID_CDROM'])

# more details about types you can find in dmidecode's manual
SMBIOS_TYPES = {'bios': '0',
                'base_board': '2',
                'processor': '4',
                'memory_array': '16',
                'memory_device': '17'}

def parse_dmidecode(type):
    """Parses `dmidecode` output.

    :param type: A string with type of entity to display.

    :returns: A list with dictionaries of entities for specified type.
    """
    output = utils.execute('dmidecode', '-q', '--type', type)
    lines = output[0].split('\n')
    info = []
    multiline_values = None
    section = 0

    for line in lines:
        if len(line) != 0 and len(line.strip()) == len(line):
            info.append({})
            section = len(info) - 1
        try:
            k, v = (l.strip() for l in line.split(':', 1))
        except ValueError:
            k = line.strip()
            if not k:
                multiline_values = None
            if multiline_values:
                info[section][multiline_values].append(k)
        else:
            if not v:
                multiline_values = k.lower()
                info[section][multiline_values] = []
            else:
                info[section][k.lower()] = v

    return info

def parse_lspci():
    """Parses `lspci` output.

    :returns: A list of dicts containing PCI devices information
    """
    output = utils.execute('lspci', '-vmm', '-D')
    lines = output[0].split('\n')
    info = [{}]
    section = 0

    for line in lines[:-2]:
        try:
            k, v = (l.strip() for l in line.split(':', 1))
        except ValueError:
            info.append({})
            section += 1
        else:
            info[section][k.lower()] = v

    return info

def parse_simple_kv(*command):
    """Parses simple key:value output from specified command.

    :param command: A command to execute

    :returns: A dict of parsed key-value data
    """
    output = utils.execute(*command)
    lines = output[0].split('\n')
    info = {}

    for line in lines[:-1]:
        try:
            k, v = (l.strip() for l in line.split(':', 1))
        except ValueError:
            break
        else:
            info[k.lower()] = v

    return info

def is_disk(dev, bspec=None, uspec=None):
    """Checks if given device is a disk.

    :param dev: A device file, e.g. /dev/sda.
    :param bspec: A dict of properties which we get from blockdev.
    :param uspec: A dict of properties which we get from udevadm.

    :returns: True if device is disk else False.
    """

    # Filtering by udevspec
    if uspec is None:
        uspec = udevreport(dev)
    if uspec.get('ID_CDROM') == '1':
        return False
    if uspec.get('DEVTYPE') == 'partition':
        return False
    if 'MAJOR' in uspec and int(uspec['MAJOR']) not in VALID_MAJORS:
        return False

    # Filtering by blockdev spec
    if bspec is None:
        bspec = blockdevreport(dev)
    if bspec.get('ro') == '1':
        return False

    return True

def udevreport(dev):
    """Builds device udevadm report.

    :param dev: A device file, e.g. /dev/sda.

    :returns: A dict of udev device properties.
    """
    report = utils.execute('udevadm',
                           'info',
                           '--query=property',
                           '--export',
                           '--name={0}'.format(dev),
                           check_exit_code=[0])[0]

    spec = {}
    for line in [l for l in report.splitlines() if l]:
        key, value = line.split('=', 1)
        value = value.strip('\'')

        # This is a list of symbolic links which were created for this
        # block device (e.g. /dev/disk/by-id/foobar)
        if key == 'DEVLINKS':
            spec['DEVLINKS'] = value.split()

        if key in UDEV_PROPERTIES:
            spec[key] = value
    return spec

def blockdevreport(blockdev):
    """Builds device blockdev report.

    :param blockdev: A block device file, e.g. /dev/sda.

    :returns: A dict of blockdev properties.
    """
    cmd = [
        'blockdev',
        '--getsz',        # get size in 512-byte sectors
        '--getro',        # get read-only
        '--getss',        # get logical block (sector) size
        '--getpbsz',      # get physical block (sector) size
        '--getsize64',    # get size in bytes
        '--getiomin',     # get minimum I/O size
        '--getioopt',     # get optimal I/O size
        '--getra',        # get readahead
        '--getalignoff',  # get alignment offset in bytes
        '--getmaxsect',   # get max sectors per request
        blockdev
    ]
    opts = [o[5:] for o in cmd if o.startswith('--get')]
    report = utils.execute(*cmd, check_exit_code=[0])[0]
    return dict(zip(opts, report.splitlines()))

def extrareport(dev):
    """Builds device report using some additional sources.

    :param dev: A device file, e.g. /dev/sda.

    :returns: A dict of properties.
    """
    spec = {}
    name = os.path.basename(dev)

    # Finding out if block device is removable or not
    # actually, some disks are marked as removable
    # while they are actually not e.g. Adaptec RAID volumes
    try:
        with open('/sys/block/{0}/removable'.format(name)) as file:
            spec['removable'] = file.read().strip()
    except Exception:
        pass

    for key in ('state', 'timeout'):
        try:
            with open('/sys/block/{0}/device/{1}'.format(name, key)) as file:
                spec[key] = file.read().strip()
        except Exception:
            pass

    return spec

def list_block_devices(disks=True):
    """Gets list of block devices, tries to guess which of them are disks
    and returns list of dicts representing those disks.

    :returns: A list of dict representing disks available on a node.
    """
    bdevs = []

    report = utils.execute('blockdev', '--report', check_exit_code=[0])[0]
    lines = [line.split() for line in report.splitlines() if line]

    startsec_idx = lines[0].index('StartSec')
    device_idx = lines[0].index('Device')
    size_idx = lines[0].index('Size')

    for line in lines[1:]:
        device = line[device_idx]
        uspec = udevreport(device)
        bspec = blockdevreport(device)
        espec = extrareport(device)

        # if device is not disk,skip it
        if disks and not is_disk(device, bspec=bspec, uspec=uspec):
            continue

        bdev = {
            'device': device,
            'startsec': line[startsec_idx],
            'size': int(line[size_idx]),
            'uspec': uspec,
            'bspec': bspec,
            'espec': espec
        }
        bdevs.append(bdev)
    return bdevs

def match_device(uspec1, uspec2):
    """Tries to find out if uspec1 and uspec2 are uspecs from the same device.
    It compares only some fields in uspecs (not all of them) which, we believe,
    is enough to say exactly whether uspecs belong to the same device or not.

    :param uspec1: A dict of properties which we get from udevadm.
    :param uspec1: A dict of properties which we get from udevadm.

    :returns: True if uspecs match each other else False.
    """

    # False if ID_WWN is given and does not match each other
    if ('ID_WWN' in uspec1 and 'ID_WWN' in uspec2
            and uspec1['ID_WWN'] != uspec2['ID_WWN']):
        return False

    # False if ID_SERIAL_SHORT is given and does not match each other
    if ('ID_SERIAL_SHORT' in uspec1 and 'ID_SERIAL_SHORT' in uspec2
            and uspec1['ID_SERIAL_SHORT'] != uspec2['ID_SERIAL_SHORT']):
        return False

    # True if at least one by-id link is the same for both uspecs
    if ('DEVLINKS' in uspec1 and 'DEVLINKS' in uspec2
            and any(x.startswith('/dev/disk/by-id') for x in
                    set(uspec1['DEVLINKS']) & set(uspec2['DEVLINKS']))):
        return True

    # True if ID_WWN is given and matches each other
    # and DEVTYPE is given and is 'disk'
    if (uspec1.get('ID_WWN') == uspec2.get('ID_WWN') is not None
            and uspec1.get('DEVTYPE') == uspec2.get('DEVTYPE') == 'disk'):
        return True

    # True if ID_WWN is given and matches each other
    # and DEVTYPE is given and is 'partition'
    # and MINOR is given and matches each other
    if (uspec1.get('ID_WWN') == uspec2.get('ID_WWN') is not None
            and uspec1.get('DEVTYPE') == uspec2.get('DEVTYPE') == 'partition'
            and uspec1.get('MINOR') == uspec2.get('MINOR') is not None):
        return True

    # True if ID_SERIAL_SHORT is given and matches each other
    # and DEVTYPE is given and is 'disk'
    if (uspec1.get('ID_SERIAL_SHORT') == uspec2.get('ID_SERIAL_SHORT')
            is not None
            and uspec1.get('DEVTYPE') == uspec2.get('DEVTYPE') == 'disk'):
        return True

    # True if DEVPATH is given and matches each other
    if uspec1.get('DEVPATH') == uspec2.get('DEVPATH') is not None:
        return True

    return False


a =[int(x) for x in input().split(' ')]

maxofgiven=[]
check = a[1]
for x in range(0,a[0]):
    maxo = -1
    for y in input().split(' '):
        y_int = int(y)
        mod = y_int%check
        if mod > maxo:
            maxo = mod
    maxofgiven.append(maxo)
maxtotal = 0
for x in maxofgiven:
    maxtotal += x**2

print(maxtotal)


# Exercício Python 31: Desenvolva um programa que pergunte a distância de uma viagem em Km. Calcule o preço da passagem, cobrando R$0,50 por Km para viagens de até 200Km e R$0,45 parta viagens mais longas.

distancia = int(input('Digite a distância em km: '))

if distancia <= 200:
    custo = distancia * 0.5
else:
    custo = distancia * 0.45

print(f'O custo da viagem com {distancia}km de distância é de R${custo:.2f}')


import re
import sublime, sublime_plugin
try:
	import indent
except:
	from . import indent

def indent_line(edit, view, line, options):
	line_str = view.substr(line)
	current_indent = indent.current_indent(line_str)
	new_indent = indent.indent(view, line.begin(), options)
	if not current_indent == new_indent:
		view.replace(edit,
		   sublime.Region(line.begin(),
		                  line.begin() + current_indent),
		   " " * new_indent)

def indent_selection(edit, view, idx, options):
	total = len(view.lines(view.sel()[idx]))

	for i in range(total):
		line = view.lines(view.sel()[idx])[i]
		indent_line(edit, view, line, options)

def indent_selections(edit, view, options):
	total = len(view.sel())

	for i in range(total):
		indent_selection(edit, view, i, options)

def insert_newline_and_indent(edit, view, options):
	total = len(view.sel())
	out = []

	for i in range(total):
		region = view.sel()[i]
		view.erase(edit, region)
		idx = region.begin()
		out += [sublime.Region(idx, idx)]

	sel = view.sel()
	sel.clear()
	for region in out: sel.add(region)

	for i in range(total):
		idx = view.sel()[i].begin()
		view.insert(edit, idx,
			"\n" + indent.get_indent_str(view, idx, options))

	view.show(sel)

###############################
## View file type + regexps

views = {}
filetypes = []
options = {}

def get_lisp_file_type(view):
	for (language, regex, syntax) in filetypes:
		view_syntax = view.settings().get('syntax')
		syntax_matches = syntax and view_syntax.endswith(syntax)
		file_name = view.file_name()
		filename_matches = file_name and regex.match(file_name)
		if filename_matches or syntax_matches:
			return language

def get_view_file_type(view):
	vwid = view.id()
	if vwid in views: return views[vwid]

def get_view_options(view):
	ft = get_view_file_type(view)
	if ft and (ft in options):
		return options[ft]

def should_use_lisp_indent(view):
	# Fix for SublimeREPL
	# Necessary because lispindent activates on syntax.
	if view.settings().get("repl"):
		return False
	return view.id() in views

def test_view(view):
	vwid = view.id()
	if not vwid in views:
		file_type = get_lisp_file_type(view)
		if file_type: views[vwid] = file_type

def test_current_view():
	win = sublime.active_window()
	if win:
		view = win.active_view()
		test_view(view)

def join_regex(regex):
	if isinstance(regex, str):
		return regex
	else:
		out = ""
		for part in regex: out += part
		return out

settings = None
def reload_languages():
	l = settings.get("languages")
	for language, opts in l.items():
		regex = join_regex(opts["regex"])
		compiled = {
			"detect": re.compile(opts["detect"]),
			"default_indent": opts["default_indent"],
			"regex": re.compile(regex)
		}
		filetypes.append((language, compiled["detect"], opts.get("syntax", None)))
		options[language] = compiled

reload_has_init = False
def init_env():
	global reload_has_init
	global settings
	if not reload_has_init:
		settings = sublime.load_settings("lispindent.sublime-settings")
		settings.add_on_change("languages", reload_languages)
		reload_languages()
		reload_has_init = True
		test_current_view()

###############################
## Commands

class LispindentCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		init_env()
		view = self.view
		test_view(view)
		if should_use_lisp_indent(view):
			indent_selections(edit, view, get_view_options(view))
		else:
			view.run_command("reindent")

class LispindentinsertnewlineCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		init_env()
		view = self.view
		test_view(view)
		if should_use_lisp_indent(view):
			insert_newline_and_indent(edit, view, get_view_options(view))
		else:
			view.run_command("insert", {"characters": "\n"})

class LispIndentListenerCommand(sublime_plugin.EventListener):
	def on_query_context(self, view, key, operator, operand, match_all):
		if key == "shoulduselispindent":
			init_env()
			test_view(view)
			return should_use_lisp_indent(view)

####
#### Override
def listen_to_syntax_change(view):
	def on_syntax_change():
		# this should "reload the view"
		pass

	view.settings().add_on_change("syntax", on_syntax_change)

class ViewOverrideRunNNNNNNNNNNNNNNNNNNNNNNCommand(sublime_plugin.TextCommand):
	def __init__(this, view):
		old_run_command = getattr(view, "run_command")

		def new_run_command(name, args={}):
			if name == "reindent":
				init_env()
				test_view(view)
				if should_use_lisp_indent(view):
					old_run_command("lispindent")
				else:
					old_run_command("reindent")
			else:
				old_run_command(name, args)

		setattr(view, "run_command", new_run_command)

		listen_to_syntax_change(view)

	def run(this, edit):
		pass


from typing import List, Optional

from hikaru.model.rel_1_26 import Job, PodList

from robusta.integrations.kubernetes.custom_models import RobustaPod, build_selector_query

CONTROLLER_UID = "controller-uid"

def get_job_selector(job: Job) -> Optional[str]:
    """
    First try to get by controller_uid, then by selectors, and if both not available by name
    """
    job_labels = job.metadata.labels
    controller_uid = job_labels.get(CONTROLLER_UID, None)
    if controller_uid:
        return f"{CONTROLLER_UID}={controller_uid}"

    if job.spec.selector:
        selector = build_selector_query(job.spec.selector)
        if selector:
            return selector

    job_name = job_labels.get("job-name", None)
    if not job_name:
        return None

    return f"job-name={job_name}"

def get_job_all_pods(job: Job) -> Optional[List[RobustaPod]]:
    if not job:
        return None

    job_selector = get_job_selector(job)
    if not job_selector:
        return None

    return PodList.listNamespacedPod(namespace=job.metadata.namespace, label_selector=job_selector).obj.items

def get_job_latest_pod(job: Job) -> Optional[RobustaPod]:
    pod_list: List[RobustaPod] = get_job_all_pods(job)
    if not pod_list:
        return None
    pod_list.sort(key=lambda pod: pod.status.startTime, reverse=True)
    return pod_list[0] if pod_list else None


#! /usr/bin/env python3

import numpy as np

c = 3 * 10**10  # cm/s
mu_lambda = -0.613
mu_nucl = 5.050783699*10**-24 # Erg * G^-1
hbar = 1.0545716 * 10**-27   # Erg * s

def stripFieldX(sigmaX, h, I):
    """ """
    return 4. * I / sigmaX * np.arctan(0.5 * sigmaX / h)

def freq(B, mu):
    """ """
    return -2 * B * mu * mu_nucl / hbar

def angle(B, dt):
    """ """
    return freq(B, mu_lambda) * dt

if __name__ == '__main__':
    import matplotlib.pyplot as plt
    I = 4.2 * 10**-3 # Amper
    sigmaX = 17.8 * 10**-4 # cm
    sigmaY = 0.178 * 10**-4 # cm
    B = stripFieldX(sigmaX, sigmaY, I)
    print('Filed: {} Gauss'.format(B))
    omega = freq(B, mu_lambda)
    print('Freq:  {} s^-1'.format(omega))
    print('Angle: {} mrad'.format(angle(B, 10**-10)*10**3))
    print('Angle: {} mrad'.format(angle(1.5*10**4, 1.4*2.6*10**-10)*10**3))

    h = np.linspace(sigmaY, 10*sigmaY, 1000)
    plt.figure(num=1)
    plt.plot(h * 10**4, stripFieldX(sigmaX, h, I))
    plt.plot(h * 10**4, stripFieldX(10*sigmaX, h, 10*I))
    plt.plot(h * 10**4, stripFieldX(100*sigmaX, h, 100*I))
    plt.plot(h * 10**4, stripFieldX(1000*sigmaX, h, 1000*I))
    plt.grid()
    # plt.semilogy()
    plt.tight_layout()
    plt.show()