from collections import namedtuple
from typing import Dict, List

import numpy as np

def get_any_value_len(parameters: Dict[str, np.ndarray]) -> int:
    """Convenience function that returns the len of any value. All lengths
    should be the same (so that we can iterate).

    Args:
      parameters (Dict[str, np.ndarray]): Parameters of the encryption (keys,
        plaintexts, masks...). A dictionary with name and the corresponding
        parameters represented as an np.array. All values must have the same
        non-zero length, their length must be a multiple of shard_length.

    Returns: Length of a single value.
    """
    return len(next(iter(parameters.values())))

def check_lengths(parameters: Dict[str, np.ndarray],
                  shard_length: np.uint64) -> None:
    """Check that all parameters have the same non-zero length.

    Args:
      parameters (Dict[str, np.ndarray]): Parameters of the encryption (keys,
        plaintexts, masks...). A dictionary with name and the corresponding
        parameters represented as an np.array. All values must have the same
        non-zero length, their length must be a multiple of shard_length.
      shard_length (np.uint64): How many tuples are in a shard. ResumeKTI
        autosaves progress after finishing a shard.

    Raises: ValueError if some length is not right.
    """
    if not parameters:
        raise ValueError("No parameters.")

    # Get length of a single values array.
    length = get_any_value_len(parameters)
    for parameter, values in parameters.items():
        if len(values) == 0:
            raise ValueError(f"There are no parameters for {parameter}")
        if len(values) % shard_length != 0:
            raise ValueError(f"The number of values of {parameter} is not "
                             f"divisible by shard_length.")
        if len(values) != length:
            raise ValueError("There are different number of parameter values.")

def _create_resume_kti(parameters: Dict[str, np.ndarray],
                       shard_length: np.uint64,
                       kt_filename: str = "parameters_tuples.txt",
                       progress_filename: str = "progress_tuples.txt",
                       allow_pickle: bool = False) -> None:
    """Saves the parameter tuples for later use. See create_resume_kti
    convenience function.

    Args:
        parameters (Dict[str, np.ndarray]): Parameters of the encryption (keys,
          plaintexts, masks...). A dictionary with name and the corresponding
          parameters represented as an np.array. All values must have the same
          non-zero length, their length must be a multiple of shard_length.
        shard_length (np.uint64): How many tuples are in a shard. ResumeKTI
          autosaves progress after finishing a shard.
        kt_filename: File where to save shard_length and parameters. If the
          file exists prints a message, but does not overwrite.
        progress_filename: The file with the number of traces captured so far.
        allow_pickle: Parameter for numpy.save.
    """
    # Check parameter lengths.
    check_lengths(parameters=parameters, shard_length=shard_length)

    # Save into file.
    try:
        with open(kt_filename, "xb") as kt_file:
            # Save the shard length.
            np.save(kt_file, shard_length, allow_pickle=allow_pickle)

            # Saving a dictionary directly would require allow_pickle=True. Save
            # dictionary.keys as an array of strings and then save the values in
            # the same order as the dictionary.keys array.
            parameter_names: List[str] = list(parameters.keys())
            np.save(kt_file,
                    np.array(parameter_names),
                    allow_pickle=allow_pickle)

            # Save their values.
            for parameter_name in parameter_names:
                np.save(kt_file,
                        parameters[parameter_name],
                        allow_pickle=allow_pickle)
    except FileExistsError:
        print(f"File {kt_filename} already exists, if you want to generate new"
              " keys, remove it first.")
        return
    with open(progress_filename, "w", encoding="utf-8") as progress_file:
        progress_file.write("0")

class ResumeKTI:
    """Iterable object, auto-saves progress after iterating shard length times.

    Does not support multiple iterations. If this is needed, write zero to
    progress_filename and create a new ResumeKTI object.
    """

    def __init__(self,
                 kt_filename: str = "parameters_tuples.txt",
                 progress_filename: str = "progress_tuples.txt",
                 allow_pickle: bool = False) -> None:
        """Create a resumable key-text iterable.

        Args:
            kt_filename: File to load shard_length and parameters.
            progress_filename: The file with the number of traces captured so
              far.  Gets updated by iterating shard length times or manually
              calling _save_progress.
            allow_pickle: Parameter for numpy.load.
        """
        with open(kt_filename, "rb") as kt_tuples:
            # Load shard length.
            self._shard_length = np.load(kt_tuples, allow_pickle=allow_pickle)

            # Load list of parameter names.
            parameter_names = np.load(kt_tuples, allow_pickle=allow_pickle)
            # Create the namedtuple class. Ignore mypy warning that named
            # tuples should not be constructed dynamically and that a list or
            # tuple literal should be used for the names.
            self._element_class = namedtuple(  # type: ignore
                "EncryptionParameters", parameter_names)

            # Load all parameters.
            self._parameters = {}
            for parameter_name in parameter_names:
                self._parameters[parameter_name] = np.load(
                    kt_tuples, allow_pickle=allow_pickle)

        # Check length of parameters.
        check_lengths(parameters=self._parameters,
                      shard_length=self._shard_length)

        # Load previous progress.
        self._progress_filename = progress_filename
        with open(self._progress_filename, encoding="utf-8") as progress_file:
            # Ensure self._index is a multiple of self._shard_length.
            i: int = int(progress_file.read())
            self._index = int(i - (i % self._shard_length))
        # Check validity of the progress.
        assert 0 <= self._index <= get_any_value_len(self._parameters)
        # How many traces have been captured before creating this object.
        self._initial_index = self._index

    def _save_progress(self) -> None:
        """Save the current iteration index.

        Save the number of traces in all finished shards (how many finished
        traces have been captured inside finished shards).  Updates content of
        the file self._progress_filename.

        Not atomic. Calling this 1000 times takes ~1s.
        """
        with open(self._progress_filename, "w",
                  encoding="utf-8") as progress_file:
            # Ensure the saved index is the largest multiple of
            # self._shard_length which is at most self._index.
            i = self._index - (self._index % self._shard_length
                              )  # End of last shard.
            i = int(i)  # Modulo by np.uint64 produces np.float64
            progress_file.write(str(i))

    def __iter__(self):
        return self

    def __next__(self):
        """Next with auto-save.

        Auto-save when a new shard is starting (after each shard length + 1
        calls without a _save_progress call), thus updates content of
        self._progress_filename file.

        Returns: Encryption parameters as a namedtuple.
        """
        if self._index % self._shard_length == 0:
            self._save_progress()
        if self._index >= get_any_value_len(self._parameters):
            raise StopIteration
        self._index += 1

        # Construct the namedtuple to return.
        element_class_parameters = {
            parameter_name: parameter_values[self._index - 1]
            for parameter_name, parameter_values in self._parameters.items()
        }
        return self._element_class(**element_class_parameters)

    def __len__(self) -> int:
        """How many tuples are there in total (including the initial_index
        skipped tuples)."""
        return get_any_value_len(self._parameters)

    @property
    def initial_index(self) -> int:
        """Returns where the current capture has started. The result is
        divisible by shard_length (number of examples in a shard) unless the
        progress file has been corrupted.

        Returns: The number of examples captured before starting this
        iteration.
        """
        return self._initial_index

def create_resume_kti(parameters: Dict[str, np.ndarray],
                      shard_length: np.uint64,
                      kt_filename: str = "parameters_tuples.txt",
                      progress_filename: str = "progress_tuples.txt",
                      allow_pickle: bool = False) -> ResumeKTI:
    """Saves the parameter tuples and returns a ResumeKTI object for immediate
    use.

    Args:
        parameters (Dict[str, np.ndarray]): Parameters of the encryption (keys,
          plaintexts, masks...). A dictionary with name and the corresponding
          parameters represented as an np.array. All values must have the same
          non-zero length, their length must be a multiple of shard_length.
        shard_length (np.uint64): How many tuples are in a shard. ResumeKTI
          autosaves progress after finishing a shard.
        kt_filename: File where to save shard_length and parameters. If the
          file exists prints a message, but does not overwrite.
        progress_filename: The file with the number of traces captured so far.
        allow_pickle: Parameter for numpy.save.

    Returns: ResumeKTI object just created.
    """
    _create_resume_kti(parameters=parameters,
                       shard_length=shard_length,
                       kt_filename=kt_filename,
                       progress_filename=progress_filename,
                       allow_pickle=allow_pickle)
    return ResumeKTI(kt_filename=kt_filename,
                     progress_filename=progress_filename,
                     allow_pickle=allow_pickle)


# -*- coding: utf-8 -*-
# @Time   : 2021/7/5 22:36
# @Author : beyoung
# @Email  : linbeyoung@stu.pku.edu.cn
# @File   : inverse.py

import cv2

src_path = '/Users/Beyoung/Desktop/Projects/AC_OCR/pkuocrtest-20210705/00049.tif'
src = cv2.imread(src_path, 1)
print(src.shape)
src = cv2.resize(src, (round(1/4 * src.shape[1]), round(1/4 * src.shape[0])))
# src = cv2.resize(src, (176, 704))
Img = 255 - src
print(Img.shape)
# cv2.imshow("Img",Img)
# cv2.imshow("src",src)
# cv2.waitKey(0)
cv2.imwrite(src_path[:-4] + '_inverse.tif', Img)


with open("/Users/dmarkov/Projects/zeRepo/AdventOfCode22/day6_input.txt", "r") as f:
    line = list(f.readline())

print(line)

def test (chars_list):
  test_set = set(chars_list)
  if len(test_set) == 14:
    return True
#get first 4 chars of line
chars_list = line[0:14]
test(chars_list)
#create set from start_chars and check it's length
index = 0
for char in line:
  chars_list.pop(0)
  chars_list.append(char)
  index += 1
  if test(chars_list):
    print(char)
    print(index)
    break


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

"""
Created on Thu Sep 20 16:14:30 2018
image scrambler prototype to focus on low level visual features
@author: Logan
"""
import os, random
from PIL import Image

files = []

#script to fetch  from a stimulus directory
def getImages(directory):
    #walk top down from current directory and return paths of images to pass on
    for (path, dirnames, filenames) in os.walk(directory):
        files.extend(os.path.join(path, name) for name in filenames)

    return(files)

def imageResize(img, width, height):
    print("resizing: " +img)
    pic = Image.open(img)
    pic = pic.resize((width, height), Image.ANTIALIAS)
    pic.save(img)

def scrambleImage(img, boxSize):
    print("scrambling: " +img)
    #open the image to work on it
    pic = Image.open(img)
    #width and height are saved to vars
    width, height = pic.size

    #find out how many boxes fit into the width and height (best to select % 0s)
    numX = int(width/boxSize)
    numY = int(height/boxSize)

    if boxSize % numX != 0 or boxSize % numY != 0:
        print("images are not evenly divided by scramble boxes, please resize")
        return False

    boxPos = []
    #create an array of all possible box 0,0 positions
    for i in range(0, width, boxSize):
        for j in range(0, height, boxSize):
            boxPos.append((i, j, i + boxSize, j + boxSize))

    posPos = len(boxPos)

    i = 0

    #iterate through the entirety of the image
    for x in range(numX):
        #checking to see which box to write to if it is a multiple move to next box
        for y in range(numY):
            thisBox = boxPos[i]
            randBox = boxPos[random.randint(0,posPos-1)]

            #get pixel values from first box
            box1 = pic.crop(thisBox)
            #get pixcel values from random box
            box2 = pic.crop(randBox)
            #print("this is randBox: " + randBox)

            randCords = (randBox[0], randBox[1])
            thisCords = (thisBox[0], thisBox[1])

            print(randCords)
            print(thisCords)

            pic.paste(box1, randCords)
            pic.paste(box2, thisCords)

            i += 1

            #swap the pixel values  of each

    newName = img.replace(".jpg", "scrambled.png")
    pic.save(newName)
    return

def main(imgSize, scrambleSize):
    pipeline = getImages('stimuli')

    for i in pipeline:
        imageResize(i, imgSize, imgSize)
        scrambleImage(i, scrambleSize)

if __name__ == '__main__':
        main(500, 50)


import math
import six

import tensorflow as tf

from object_detection.core import box_list
from object_detection.core import box_list_ops
from object_detection.core import standard_fields as fields
from object_detection.utils import static_shape

def expanded_shape(orig_shape, start_dim, num_dims):
  """Inserts multiple ones into a shape vector.

  Inserts an all-1 vector of length num_dims at position start_dim into a shape.
  Can be combined with tf.reshape to generalize tf.expand_dims.

  Args:
    orig_shape: the shape into which the all-1 vector is added (int32 vector)
    start_dim: insertion position (int scalar)
    num_dims: length of the inserted all-1 vector (int scalar)
  Returns:
    An int32 vector of length tf.size(orig_shape) + num_dims.
  """
  with tf.name_scope('ExpandedShape'):
    start_dim = tf.expand_dims(start_dim, 0)  # scalar to rank-1
    before = tf.slice(orig_shape, [0], start_dim)
    add_shape = tf.ones(tf.reshape(num_dims, [1]), dtype=tf.int32)
    after = tf.slice(orig_shape, start_dim, [-1])
    new_shape = tf.concat([before, add_shape, after], 0)
    return new_shape

def normalized_to_image_coordinates(normalized_boxes, image_shape,
                                    parallel_iterations=32):
  """Converts a batch of boxes from normal to image coordinates.

  Args:
    normalized_boxes: a float32 tensor of shape [None, num_boxes, 4] in
      normalized coordinates.
    image_shape: a float32 tensor of shape [4] containing the image shape.
    parallel_iterations: parallelism for the map_fn op.

  Returns:
    absolute_boxes: a float32 tensor of shape [None, num_boxes, 4] containg the
      boxes in image coordinates.
  """
  def _to_absolute_coordinates(normalized_boxes):
    return box_list_ops.to_absolute_coordinates(
        box_list.BoxList(normalized_boxes),
        image_shape[1], image_shape[2], check_range=False).get()

  absolute_boxes = tf.map_fn(
      _to_absolute_coordinates,
      elems=(normalized_boxes),
      dtype=tf.float32,
      parallel_iterations=parallel_iterations,
      back_prop=True)
  return absolute_boxes

def meshgrid(x, y):
  """Tiles the contents of x and y into a pair of grids.

  Multidimensional analog of numpy.meshgrid, giving the same behavior if x and y
  are vectors. Generally, this will give:

  xgrid(i1, ..., i_m, j_1, ..., j_n) = x(j_1, ..., j_n)
  ygrid(i1, ..., i_m, j_1, ..., j_n) = y(i_1, ..., i_m)

  Keep in mind that the order of the arguments and outputs is reverse relative
  to the order of the indices they go into, done for compatibility with numpy.
  The output tensors have the same shapes.  Specifically:

  xgrid.get_shape() = y.get_shape().concatenate(x.get_shape())
  ygrid.get_shape() = y.get_shape().concatenate(x.get_shape())

  Args:
    x: A tensor of arbitrary shape and rank. xgrid will contain these values
       varying in its last dimensions.
    y: A tensor of arbitrary shape and rank. ygrid will contain these values
       varying in its first dimensions.
  Returns:
    A tuple of tensors (xgrid, ygrid).
  """
  with tf.name_scope('Meshgrid'):
    x = tf.convert_to_tensor(x)
    y = tf.convert_to_tensor(y)
    x_exp_shape = expanded_shape(tf.shape(x), 0, tf.rank(y))
    y_exp_shape = expanded_shape(tf.shape(y), tf.rank(y), tf.rank(x))

    xgrid = tf.tile(tf.reshape(x, x_exp_shape), y_exp_shape)
    ygrid = tf.tile(tf.reshape(y, y_exp_shape), x_exp_shape)
    new_shape = y.get_shape().concatenate(x.get_shape())
    xgrid.set_shape(new_shape)
    ygrid.set_shape(new_shape)

    return xgrid, ygrid

def pad_to_multiple(tensor, multiple):
  """Returns the tensor zero padded to the specified multiple.

  Appends 0s to the end of the first and second dimension (height and width) of
  the tensor until both dimensions are a multiple of the input argument
  'multiple'. E.g. given an input tensor of shape [1, 3, 5, 1] and an input
  multiple of 4, PadToMultiple will append 0s so that the resulting tensor will
  be of shape [1, 4, 8, 1].

  Args:
    tensor: rank 4 float32 tensor, where
            tensor -> [batch_size, height, width, channels].
    multiple: the multiple to pad to.

  Returns:
    padded_tensor: the tensor zero padded to the specified multiple.
  """
  tensor_shape = tensor.get_shape()
  batch_size = static_shape.get_batch_size(tensor_shape)
  tensor_height = static_shape.get_height(tensor_shape)
  tensor_width = static_shape.get_width(tensor_shape)
  tensor_depth = static_shape.get_depth(tensor_shape)

  if batch_size is None:
    batch_size = tf.shape(tensor)[0]

  if tensor_height is None:
    tensor_height = tf.shape(tensor)[1]
    padded_tensor_height = tf.to_int32(
        tf.ceil(tf.to_float(tensor_height) / tf.to_float(multiple))) * multiple
  else:
    padded_tensor_height = int(
        math.ceil(float(tensor_height) / multiple) * multiple)

  if tensor_width is None:
    tensor_width = tf.shape(tensor)[2]
    padded_tensor_width = tf.to_int32(
        tf.ceil(tf.to_float(tensor_width) / tf.to_float(multiple))) * multiple
  else:
    padded_tensor_width = int(
        math.ceil(float(tensor_width) / multiple) * multiple)

  if tensor_depth is None:
    tensor_depth = tf.shape(tensor)[3]

  # Use tf.concat instead of tf.pad to preserve static shape
  height_pad = tf.zeros([
      batch_size, padded_tensor_height - tensor_height, tensor_width,
      tensor_depth
  ])
  padded_tensor = tf.concat([tensor, height_pad], 1)
  width_pad = tf.zeros([
      batch_size, padded_tensor_height, padded_tensor_width - tensor_width,
      tensor_depth
  ])
  padded_tensor = tf.concat([padded_tensor, width_pad], 2)

  return padded_tensor

def padded_one_hot_encoding(indices, depth, left_pad):
  """Returns a zero padded one-hot tensor.

  This function converts a sparse representation of indices (e.g., [4]) to a
  zero padded one-hot representation (e.g., [0, 0, 0, 0, 1] with depth = 4 and
  left_pad = 1). If `indices` is empty, the result will simply be a tensor of
  shape (0, depth + left_pad). If depth = 0, then this function just returns
  `None`.

  Args:
    indices: an integer tensor of shape [num_indices].
    depth: depth for the one-hot tensor (integer).
    left_pad: number of zeros to left pad the one-hot tensor with (integer).

  Returns:
    padded_onehot: a tensor with shape (num_indices, depth + left_pad). Returns
      `None` if the depth is zero.

  Raises:
    ValueError: if `indices` does not have rank 1 or if `left_pad` or `depth are
      either negative or non-integers.

  TODO: add runtime checks for depth and indices.
  """
  if depth < 0 or not isinstance(depth, (int, long) if six.PY2 else int):
    raise ValueError('`depth` must be a non-negative integer.')
  if left_pad < 0 or not isinstance(left_pad, (int, long) if six.PY2 else int):
    raise ValueError('`left_pad` must be a non-negative integer.')
  if depth == 0:
    return None
  if len(indices.get_shape().as_list()) != 1:
    raise ValueError('`indices` must have rank 1')

  def one_hot_and_pad():
    one_hot = tf.cast(tf.one_hot(tf.cast(indices, tf.int64), depth,
                                 on_value=1, off_value=0), tf.float32)
    return tf.pad(one_hot, [[0, 0], [left_pad, 0]], mode='CONSTANT')
  result = tf.cond(tf.greater(tf.size(indices), 0), one_hot_and_pad,
                   lambda: tf.zeros((depth + left_pad, 0)))
  return tf.reshape(result, [-1, depth + left_pad])

def dense_to_sparse_boxes(dense_locations, dense_num_boxes, num_classes):
  """Converts bounding boxes from dense to sparse form.

  Args:
    dense_locations:  a [max_num_boxes, 4] tensor in which only the first k rows
      are valid bounding box location coordinates, where k is the sum of
      elements in dense_num_boxes.
    dense_num_boxes: a [max_num_classes] tensor indicating the counts of
       various bounding box classes e.g. [1, 0, 0, 2] means that the first
       bounding box is of class 0 and the second and third bounding boxes are
       of class 3. The sum of elements in this tensor is the number of valid
       bounding boxes.
    num_classes: number of classes

  Returns:
    box_locations: a [num_boxes, 4] tensor containing only valid bounding
       boxes (i.e. the first num_boxes rows of dense_locations)
    box_classes: a [num_boxes] tensor containing the classes of each bounding
       box (e.g. dense_num_boxes = [1, 0, 0, 2] => box_classes = [0, 3, 3]
  """

  num_valid_boxes = tf.reduce_sum(dense_num_boxes)
  box_locations = tf.slice(dense_locations,
                           tf.constant([0, 0]), tf.stack([num_valid_boxes, 4]))
  tiled_classes = [tf.tile([i], tf.expand_dims(dense_num_boxes[i], 0))
                   for i in range(num_classes)]
  box_classes = tf.concat(tiled_classes, 0)
  box_locations.set_shape([None, 4])
  return box_locations, box_classes

def indices_to_dense_vector(indices,
                            size,
                            indices_value=1.,
                            default_value=0,
                            dtype=tf.float32):
  """Creates dense vector with indices set to specific value and rest to zeros.

  This function exists because it is unclear if it is safe to use
    tf.sparse_to_dense(indices, [size], 1, validate_indices=False)
  with indices which are not ordered.
  This function accepts a dynamic size (e.g. tf.shape(tensor)[0])

  Args:
    indices: 1d Tensor with integer indices which are to be set to
        indices_values.
    size: scalar with size (integer) of output Tensor.
    indices_value: values of elements specified by indices in the output vector
    default_value: values of other elements in the output vector.
    dtype: data type.

  Returns:
    dense 1D Tensor of shape [size] with indices set to indices_values and the
        rest set to default_value.
  """
  size = tf.to_int32(size)
  zeros = tf.ones([size], dtype=dtype) * default_value
  values = tf.ones_like(indices, dtype=dtype) * indices_value

  return tf.dynamic_stitch([tf.range(size), tf.to_int32(indices)],
                           [zeros, values])

def retain_groundtruth(tensor_dict, valid_indices):
  """Retains groundtruth by valid indices.

  Args:
    tensor_dict: a dictionary of following groundtruth tensors -
      fields.InputDataFields.groundtruth_boxes
      fields.InputDataFields.groundtruth_classes
      fields.InputDataFields.groundtruth_is_crowd
      fields.InputDataFields.groundtruth_area
      fields.InputDataFields.groundtruth_label_types
      fields.InputDataFields.groundtruth_difficult
    valid_indices: a tensor with valid indices for the box-level groundtruth.

  Returns:
    a dictionary of tensors containing only the groundtruth for valid_indices.

  Raises:
    ValueError: If the shape of valid_indices is invalid.
    ValueError: field fields.InputDataFields.groundtruth_boxes is
      not present in tensor_dict.
  """
  input_shape = valid_indices.get_shape().as_list()
  if not (len(input_shape) == 1 or
          (len(input_shape) == 2 and input_shape[1] == 1)):
    raise ValueError('The shape of valid_indices is invalid.')
  valid_indices = tf.reshape(valid_indices, [-1])
  valid_dict = {}
  if fields.InputDataFields.groundtruth_boxes in tensor_dict:
    # Prevents reshape failure when num_boxes is 0.
    num_boxes = tf.maximum(tf.shape(
        tensor_dict[fields.InputDataFields.groundtruth_boxes])[0], 1)
    for key in tensor_dict:
      if key in [fields.InputDataFields.groundtruth_boxes,
                 fields.InputDataFields.groundtruth_classes]:
        valid_dict[key] = tf.gather(tensor_dict[key], valid_indices)
      # Input decoder returns empty tensor when these fields are not provided.
      # Needs to reshape into [num_boxes, -1] for tf.gather() to work.
      elif key in [fields.InputDataFields.groundtruth_is_crowd,
                   fields.InputDataFields.groundtruth_area,
                   fields.InputDataFields.groundtruth_difficult,
                   fields.InputDataFields.groundtruth_label_types]:
        valid_dict[key] = tf.reshape(
            tf.gather(tf.reshape(tensor_dict[key], [num_boxes, -1]),
                      valid_indices), [-1])
      # Fields that are not associated with boxes.
      else:
        valid_dict[key] = tensor_dict[key]
  else:
    raise ValueError('%s not present in input tensor dict.' % (
        fields.InputDataFields.groundtruth_boxes))
  return valid_dict

def retain_groundtruth_with_positive_classes(tensor_dict):
  """Retains only groundtruth with positive class ids.

  Args:
    tensor_dict: a dictionary of following groundtruth tensors -
      fields.InputDataFields.groundtruth_boxes
      fields.InputDataFields.groundtruth_classes
      fields.InputDataFields.groundtruth_is_crowd
      fields.InputDataFields.groundtruth_area
      fields.InputDataFields.groundtruth_label_types
      fields.InputDataFields.groundtruth_difficult

  Returns:
    a dictionary of tensors containing only the groundtruth with positive
    classes.

  Raises:
    ValueError: If groundtruth_classes tensor is not in tensor_dict.
  """
  if fields.InputDataFields.groundtruth_classes not in tensor_dict:
    raise ValueError('`groundtruth classes` not in tensor_dict.')
  keep_indices = tf.where(tf.greater(
      tensor_dict[fields.InputDataFields.groundtruth_classes], 0))
  return retain_groundtruth(tensor_dict, keep_indices)

def filter_groundtruth_with_nan_box_coordinates(tensor_dict):
  """Filters out groundtruth with no bounding boxes.

  Args:
    tensor_dict: a dictionary of following groundtruth tensors -
      fields.InputDataFields.groundtruth_boxes
      fields.InputDataFields.groundtruth_classes
      fields.InputDataFields.groundtruth_is_crowd
      fields.InputDataFields.groundtruth_area
      fields.InputDataFields.groundtruth_label_types

  Returns:
    a dictionary of tensors containing only the groundtruth that have bounding
    boxes.
  """
  groundtruth_boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes]
  nan_indicator_vector = tf.greater(tf.reduce_sum(tf.to_int32(
      tf.is_nan(groundtruth_boxes)), reduction_indices=[1]), 0)
  valid_indicator_vector = tf.logical_not(nan_indicator_vector)
  valid_indices = tf.where(valid_indicator_vector)

  return retain_groundtruth(tensor_dict, valid_indices)

def normalize_to_target(inputs,
                        target_norm_value,
                        dim,
                        epsilon=1e-7,
                        trainable=True,
                        scope='NormalizeToTarget',
                        summarize=True):
  """L2 normalizes the inputs across the specified dimension to a target norm.

  This op implements the L2 Normalization layer introduced in
  Liu, Wei, et al. "SSD: Single Shot MultiBox Detector."
  and Liu, Wei, Andrew Rabinovich, and Alexander C. Berg.
  "Parsenet: Looking wider to see better." and is useful for bringing
  activations from multiple layers in a convnet to a standard scale.

  Note that the rank of `inputs` must be known and the dimension to which
  normalization is to be applied should be statically defined.

  TODO: Add option to scale by L2 norm of the entire input.

  Args:
    inputs: A `Tensor` of arbitrary size.
    target_norm_value: A float value that specifies an initial target norm or
      a list of floats (whose length must be equal to the depth along the
      dimension to be normalized) specifying a per-dimension multiplier
      after normalization.
    dim: The dimension along which the input is normalized.
    epsilon: A small value to add to the inputs to avoid dividing by zero.
    trainable: Whether the norm is trainable or not
    scope: Optional scope for variable_scope.
    summarize: Whether or not to add a tensorflow summary for the op.

  Returns:
    The input tensor normalized to the specified target norm.

  Raises:
    ValueError: If dim is smaller than the number of dimensions in 'inputs'.
    ValueError: If target_norm_value is not a float or a list of floats with
      length equal to the depth along the dimension to be normalized.
  """
  with tf.variable_scope(scope, 'NormalizeToTarget', [inputs]):
    if not inputs.get_shape():
      raise ValueError('The input rank must be known.')
    input_shape = inputs.get_shape().as_list()
    input_rank = len(input_shape)
    if dim < 0 or dim >= input_rank:
      raise ValueError(
          'dim must be non-negative but smaller than the input rank.')
    if not input_shape[dim]:
      raise ValueError('input shape should be statically defined along '
                       'the specified dimension.')
    depth = input_shape[dim]
    if not (isinstance(target_norm_value, float) or
            (isinstance(target_norm_value, list) and
             len(target_norm_value) == depth) and
            all([isinstance(val, float) for val in target_norm_value])):
      raise ValueError('target_norm_value must be a float or a list of floats '
                       'with length equal to the depth along the dimension to '
                       'be normalized.')
    if isinstance(target_norm_value, float):
      initial_norm = depth * [target_norm_value]
    else:
      initial_norm = target_norm_value
    target_norm = tf.contrib.framework.model_variable(
        name='weights', dtype=tf.float32,
        initializer=tf.constant(initial_norm, dtype=tf.float32),
        trainable=trainable)
    if summarize:
      mean = tf.reduce_mean(target_norm)
      mean = tf.Print(mean, ['NormalizeToTarget:', mean])
      tf.summary.scalar(tf.get_variable_scope().name, mean)
    lengths = epsilon + tf.sqrt(tf.reduce_sum(tf.square(inputs), dim, True))
    mult_shape = input_rank*[1]
    mult_shape[dim] = depth
    return tf.reshape(target_norm, mult_shape) * tf.truediv(inputs, lengths)

def position_sensitive_crop_regions(image,
                                    boxes,
                                    box_ind,
                                    crop_size,
                                    num_spatial_bins,
                                    global_pool,
                                    extrapolation_value=None):
  """Position-sensitive crop and pool rectangular regions from a feature grid.

  The output crops are split into `spatial_bins_y` vertical bins
  and `spatial_bins_x` horizontal bins. For each intersection of a vertical
  and a horizontal bin the output values are gathered by performing
  `tf.image.crop_and_resize` (bilinear resampling) on a a separate subset of
  channels of the image. This reduces `depth` by a factor of
  `(spatial_bins_y * spatial_bins_x)`.

  When global_pool is True, this function implements a differentiable version
  of position-sensitive RoI pooling used in
  [R-FCN detection system](https://arxiv.org/abs/1605.06409).

  When global_pool is False, this function implements a differentiable version
  of position-sensitive assembling operation used in
  [instance FCN](https://arxiv.org/abs/1603.08678).

  Args:
    image: A `Tensor`. Must be one of the following types: `uint8`, `int8`,
      `int16`, `int32`, `int64`, `half`, `float32`, `float64`.
      A 4-D tensor of shape `[batch, image_height, image_width, depth]`.
      Both `image_height` and `image_width` need to be positive.
    boxes: A `Tensor` of type `float32`.
      A 2-D tensor of shape `[num_boxes, 4]`. The `i`-th row of the tensor
      specifies the coordinates of a box in the `box_ind[i]` image and is
      specified in normalized coordinates `[y1, x1, y2, x2]`. A normalized
      coordinate value of `y` is mapped to the image coordinate at
      `y * (image_height - 1)`, so as the `[0, 1]` interval of normalized image
      height is mapped to `[0, image_height - 1] in image height coordinates.
      We do allow y1 > y2, in which case the sampled crop is an up-down flipped
      version of the original image. The width dimension is treated similarly.
      Normalized coordinates outside the `[0, 1]` range are allowed, in which
      case we use `extrapolation_value` to extrapolate the input image values.
    box_ind:  A `Tensor` of type `int32`.
      A 1-D tensor of shape `[num_boxes]` with int32 values in `[0, batch)`.
      The value of `box_ind[i]` specifies the image that the `i`-th box refers
      to.
    crop_size: A list of two integers `[crop_height, crop_width]`. All
      cropped image patches are resized to this size. The aspect ratio of the
      image content is not preserved. Both `crop_height` and `crop_width` need
      to be positive.
    num_spatial_bins: A list of two integers `[spatial_bins_y, spatial_bins_x]`.
      Represents the number of position-sensitive bins in y and x directions.
      Both values should be >= 1. `crop_height` should be divisible by
      `spatial_bins_y`, and similarly for width.
      The number of image channels should be divisible by
      (spatial_bins_y * spatial_bins_x).
      Suggested value from R-FCN paper: [3, 3].
    global_pool: A boolean variable.
      If True, we perform average global pooling on the features assembled from
        the position-sensitive score maps.
      If False, we keep the position-pooled features without global pooling
        over the spatial coordinates.
      Note that using global_pool=True is equivalent to but more efficient than
        running the function with global_pool=False and then performing global
        average pooling.
    extrapolation_value: An optional `float`. Defaults to `0`.
      Value used for extrapolation, when applicable.
  Returns:
    position_sensitive_features: A 4-D tensor of shape
      `[num_boxes, K, K, crop_channels]`,
      where `crop_channels = depth / (spatial_bins_y * spatial_bins_x)`,
      where K = 1 when global_pool is True (Average-pooled cropped regions),
      and K = crop_size when global_pool is False.
  Raises:
    ValueError: Raised in four situations:
      `num_spatial_bins` is not >= 1;
      `num_spatial_bins` does not divide `crop_size`;
      `(spatial_bins_y*spatial_bins_x)` does not divide `depth`;
      `bin_crop_size` is not square when global_pool=False due to the
        constraint in function space_to_depth.
  """
  total_bins = 1
  bin_crop_size = []

  for (num_bins, crop_dim) in zip(num_spatial_bins, crop_size):
    if num_bins < 1:
      raise ValueError('num_spatial_bins should be >= 1')

    if crop_dim % num_bins != 0:
      raise ValueError('crop_size should be divisible by num_spatial_bins')

    total_bins *= num_bins
    bin_crop_size.append(crop_dim // num_bins)

  if not global_pool and bin_crop_size[0] != bin_crop_size[1]:
    raise ValueError('Only support square bin crop size for now.')

  ymin, xmin, ymax, xmax = tf.unstack(boxes, axis=1)
  spatial_bins_y, spatial_bins_x = num_spatial_bins

  # Split each box into spatial_bins_y * spatial_bins_x bins.
  position_sensitive_boxes = []
  for bin_y in range(spatial_bins_y):
    step_y = (ymax - ymin) / spatial_bins_y
    for bin_x in range(spatial_bins_x):
      step_x = (xmax - xmin) / spatial_bins_x
      box_coordinates = [ymin + bin_y * step_y,
                         xmin + bin_x * step_x,
                         ymin + (bin_y + 1) * step_y,
                         xmin + (bin_x + 1) * step_x,
                        ]
      position_sensitive_boxes.append(tf.stack(box_coordinates, axis=1))

  image_splits = tf.split(value=image, num_or_size_splits=total_bins, axis=3)

  image_crops = []
  for (split, box) in zip(image_splits, position_sensitive_boxes):
    crop = tf.image.crop_and_resize(split, box, box_ind, bin_crop_size,
                                    extrapolation_value=extrapolation_value)
    image_crops.append(crop)

  if global_pool:
    # Average over all bins.
    position_sensitive_features = tf.add_n(image_crops) / len(image_crops)
    # Then average over spatial positions within the bins.
    position_sensitive_features = tf.reduce_mean(
        position_sensitive_features, [1, 2], keep_dims=True)
  else:
    # Reorder height/width to depth channel.
    block_size = bin_crop_size[0]
    if block_size >= 2:
      image_crops = [tf.space_to_depth(
          crop, block_size=block_size) for crop in image_crops]

    # Pack image_crops so that first dimension is for position-senstive boxes.
    position_sensitive_features = tf.stack(image_crops, axis=0)

    # Unroll the position-sensitive boxes to spatial positions.
    position_sensitive_features = tf.squeeze(
        tf.batch_to_space_nd(position_sensitive_features,
                             block_shape=[1] + num_spatial_bins,
                             crops=tf.zeros((3, 2), dtype=tf.int32)),
        squeeze_dims=[0])

    # Reorder back the depth channel.
    if block_size >= 2:
      position_sensitive_features = tf.depth_to_space(
          position_sensitive_features, block_size=block_size)

  return position_sensitive_features

def reframe_box_masks_to_image_masks(box_masks, boxes, image_height,
                                     image_width):
  """Transforms the box masks back to full image masks.

  Embeds masks in bounding boxes of larger masks whose shapes correspond to
  image shape.

  Args:
    box_masks: A tf.float32 tensor of size [num_masks, mask_height, mask_width].
    boxes: A tf.float32 tensor of size [num_masks, 4] containing the box
           corners. Row i contains [ymin, xmin, ymax, xmax] of the box
           corresponding to mask i. Note that the box corners are in
           normalized coordinates.
    image_height: Image height. The output mask will have the same height as
                  the image height.
    image_width: Image width. The output mask will have the same width as the
                 image width.

  Returns:
    A tf.float32 tensor of size [num_masks, image_height, image_width].
  """
  # TODO: Make this a public function.
  def transform_boxes_relative_to_boxes(boxes, reference_boxes):
    boxes = tf.reshape(boxes, [-1, 2, 2])
    min_corner = tf.expand_dims(reference_boxes[:, 0:2], 1)
    max_corner = tf.expand_dims(reference_boxes[:, 2:4], 1)
    transformed_boxes = (boxes - min_corner) / (max_corner - min_corner)
    return tf.reshape(transformed_boxes, [-1, 4])

  box_masks = tf.expand_dims(box_masks, axis=3)
  num_boxes = tf.shape(box_masks)[0]
  unit_boxes = tf.concat(
      [tf.zeros([num_boxes, 2]), tf.ones([num_boxes, 2])], axis=1)
  reverse_boxes = transform_boxes_relative_to_boxes(unit_boxes, boxes)
  image_masks = tf.image.crop_and_resize(image=box_masks,
                                         boxes=reverse_boxes,
                                         box_ind=tf.range(num_boxes),
                                         crop_size=[image_height, image_width],
                                         extrapolation_value=0.0)
  return tf.squeeze(image_masks, axis=3)


#!/usr/bin/env python3.4

import os,sys
import cgi, cgitb
import form_functions

cgitb.enable()

print('''\
Content-type:text/html\r\n
<html>
<head>
<title>Форма, строка запроса, запись и считывание</title>
</head>
<body>
<pre>
''')

form = cgi.FieldStorage()

print ("ключи(form.keys):",form.keys())
form_keys=list()
form_values=list()
print  ("\nНазвания ключей   и их значения (как есть)")
i=0
for key in form.keys():
    print  (i,  ": ", key," = ", form.getvalue(key))
    form_keys.append(key)
    i+=1

if "000_file_name" in form:
    print ("\n000_file_name: ", form["000_file_name"].value)
    print ("010_mode: ", form["010_mode"].value)
    print ("variable1: ", form["variable1"].value)
    print ("variable2: ", form["variable2"].value)

    try:
        variable1 = int(form["variable1"].value)
        variable2 = float(form["variable2"].value)
    except Exception as mistake:
        print("\n\nОшибка ввода")
        print(mistake)
    else:
      print('\n\nОшибки нет, вызываем функцию (при необходимости)')
      print(form_functions.function_1(variable1, variable2))

print ('''
<form  action="./form.py"   target='_self' method='get'>
Название файла: <input type="Техт" name="000_file_name" value="qs_file.txt" >
Тип записи в файл:<select name="010_mode">
    <OPTION value="a">a - дозаписать в файл</OPTION>
    <OPTION value="w">w - очисить файл и записать в файл</OPTION>
    </select>
Первая переменная: <input type="Техт" name="variable1" value="100 or Иван" >
Вторая переменная: <input type="Техт" name="variable2" value="1990. or Иванов" >
<input type="reset"  name="reset" value="Обновить">
<input type="submit" name="submit" value="Отправить">
</form>
''')

print('''
</pre>
</body>
<html>
''')


import pandas as pd
import numpy as np
import sklearn as sk

def RMSE(A, F):
    if type(A) == list:
        A = np.array(A)
    elif type(A) == pd.core.series.Series:
        A = np.array([A.iloc[i] for i in range(len(A))])
    if type(A) != np.ndarray:
        raise Exception("Unsupported types.")

    if type(F) == list:
        F = np.array(F)
    elif type(F) == pd.core.series.Series:
        F = np.array([F.iloc[i] for i in range(len(F))])
    if type(F) != np.ndarray:
        raise Exception("Unsupported types.")

    if A.shape != F.shape:
        raise Exception("Expected and actual have different dimensions!")

    if len(A.shape) <= 1:
        return np.sqrt(sk.metrics.mean_squared_error(np.nan_to_num(A), np.nan_to_num(F)))
    else:
        return [np.sqrt(sk.metrics.mean_squared_error(np.nan_to_num(A[i]), np.nan_to_num(F[i]))) for i in range(A.shape[0])]

def MAPE(A, F):
    if type(A) == list:
        A = np.array(A)
    elif type(A) == pd.core.series.Series:
        A = np.array([A.iloc[i] for i in range(len(A))])
    if type(A) != np.ndarray:
        raise Exception("Unsupported types.")

    if type(F) == list:
        F = np.array(F)
    elif type(F) == pd.core.series.Series:
        F = np.array([F.iloc[i] for i in range(len(F))])
    if type(F) != np.ndarray:
        raise Exception("Unsupported types.")

    if A.shape != F.shape:
        raise Exception("Expected and actual have different dimensions!")

    if len(A.shape) <= 1:
        n = 0
        s = 0
        for i in range(len(A)):
            s += np.abs((A[i] - F[i]) / A[i])
            n+=1
        return s / n if n > 0 else 0
    return [MAPE(A[i], F[i]) for i in range(len(A))]

def RMSEOverX(A, F, x):
    indices = np.where(np.maximum(A, F) > x)[0]
    return RMSE(A[indices], F[indices]) if len(indices) > 0 else np.nan

def RMSEOverOne(A, F):
    return RMSEOverX(A, F, 1)

def RMSEOverTwo(A, F):
    return RMSEOverX(A, F, 2)

def RMSEOverTen(A, F):
    return RMSEOverX(A, F, 10)

def overMAPE(A, F):
    if type(A) == list:
        A = np.array(A)
    elif type(A) == pd.core.series.Series:
        A = np.array([A.iloc[i] for i in range(len(A))])
    if type(A) != np.ndarray:
        raise Exception("Unsupported types.")

    if type(F) == list:
        F = np.array(F)
    elif type(F) == pd.core.series.Series:
        F = np.array([F.iloc[i] for i in range(len(F))])
    if type(F) != np.ndarray:
        raise Exception("Unsupported types.")

    if A.shape != F.shape:
        raise Exception("Expected and actual have different dimensions!")

    if len(A.shape) <= 1:
        n = 0
        s = 0
        for i in range(len(A)):
            if A[i] <= F[i] and (F[i] > 10):
                s += (F[i] - A[i]) / np.maximum(1, A[i])
                n+=1
        return s / n if n > 0 else 0
    return [overMAPE(A[i], F[i]) for i in range(len(A))]

def underMAPE(A, F):
    if type(A) == list:
        A = np.array(A)
    elif type(A) == pd.core.series.Series:
        A = np.array([A.iloc[i] for i in range(len(A))])
    if type(A) != np.ndarray:
        raise Exception("Unsupported types.")

    if type(F) == list:
        F = np.array(F)
    elif type(F) == pd.core.series.Series:
        F = np.array([F.iloc[i] for i in range(len(F))])
    if type(F) != np.ndarray:
        raise Exception("Unsupported types.")

    if A.shape != F.shape:
        raise Exception("Expected and actual have different dimensions!")

    if len(A.shape) <= 1:
        n = 0
        s = 0
        for i in range(len(A)):
            if A[i] >= F[i] and (A[i] > 10):
                s += (A[i] - F[i]) / np.maximum(1, A[i])
                n+=1
        return s / n if n > 0 else 0
    raise Exception("Uncovered case")

def getAllMetrics(datasets, classifiers, data_train, data_pred, predicate):
    metrics = {}
    for measure in [RMSE, SMAPE, RDIST, overMAPE, underMAPE]:
        measureName = str(measure.__name__)
        metrics[measureName] = {}
        metrics[measureName]['train'] = getMeasureForDatasetClassifierPairs(datasets, classifiers, data_train, measure, predicate)
        metrics[measureName]['test'] = getMeasureForDatasetClassifierPairs(datasets, classifiers, data_pred, measure, predicate)
    return metrics

def createPredictionErrorFile(suffix = "", query = None):
    inFile = "data/workdata/runtimepredictions" + suffix + ".csv"
    outFile = "data/workdata/runtimepredictions" + suffix + "_withmetrics.csv"
    serializedDF = pd.read_csv(inFile)
    if not query is None:
        serializedDF = serializedDF.query(query)
    dfOut = pd.read_csv(outFile)
    attributes = ["openmlid", "algorithm", "learner", "trainpoints_algorithm", "trainpoints_learner", "basefeatures", "expansions"]
    groups = serializedDF.groupby(attributes)
    measures = [RMSE, RMSEOverOne, RMSEOverTwo, RMSEOverTen, SMAPE, overMAPE, underMAPE]
    resultCols = []
    for measure in measures:
        name = str(measure.__name__)
        resultCols.append("fittime_" + name)
        resultCols.append("predicttime_" + name)

    cols = attributes.copy()
    cols.extend(resultCols)
    rows = []
    i = 0
    for gIndex, group in tqdm(groups):
        print(gIndex)
        row = list(gIndex)
        matches = dfOut[np.count_nonzero((dfOut[attributes] == row).values, axis=1) == len(attributes)]
        if len(matches) == 0:

            # sub-group by seed
            subresults = {}
            for rc in resultCols:
                subresults[rc] = []
            for sgIndex, subgroup in group.groupby("seed"):

                if len(subgroup) >= 4:
                    print("seed: ", sgIndex)

                    # compute basis to create metrics
                    trainGT = np.array(str(subgroup.query("recordtype == 'traintime_gt'")["entries"].values[0]).split(";")).astype('float64')
                    trainPR = np.array(str(subgroup.query("recordtype == 'traintime_pr'")["entries"].values[0]).split(";")).astype('float64')
                    testGT = np.array(str(subgroup.query("recordtype == 'predictiontime_gt'")["entries"].values[0]).split(";")).astype('float64')
                    testPR = np.array(str(subgroup.query("recordtype == 'predictiontime_pr'")["entries"].values[0]).split(";")).astype('float64')
                    if len(trainGT) != len(trainPR):
                        raise Exception("GT and PR of traintimes have different length!")
                    if len(testGT) != len(testPR):
                        raise Exception("GT and PR of prediction times have different length!")

                    # compute metrics
                    for measure in measures:
                        measureName = str(measure.__name__)
                        print("Measure " + measureName)
                        subresults["fittime_" + measureName].append(measure(trainGT, trainPR))
                        subresults["predicttime_" + measureName].append(measure(testGT, testPR))
                        #print(measureName + ": " + str(subresults["fittime_" + measureName][-1]) + "/" + str(subresults["predicttime_" + measureName][-1]))

                    #if np.abs(np.mean(testPR)-np.mean(testGT) > 30):
                    #    print(gIndex)
                    #    print(testPR)
                    #    print(testGT)

            for sr in subresults:
                row.append(np.mean(subresults[sr]))
            i += 1

            rows.append(row)
            if i % 1000 == 0:
                df = pd.concat([dfOut, pd.DataFrame(rows, columns = cols)])
                df.to_csv(outFile, index=False)
                i = 0

    if i > 0:
        df = pd.concat([dfOut, pd.DataFrame(rows, columns = cols)])
        df.to_csv(outFile, index=False)
    else:
        df = dfOut
    return df

def getMeasureForDatasetClassifierPairs(datasets, classifiers, data, measure, predicate):
    output = np.zeros((len(datasets), len(classifiers)))
    for i, d in enumerate(datasets):
        for j, c in enumerate(classifiers):
            if type(data[i][j]) == list and len(data[i][j]) > 0:
                A = np.nan_to_num(data[i][j][0][0])
                F = np.nan_to_num(data[i][j][0][1])

                if predicate is None:
                    indices = np.arange(len(A))
                else:
                    indices = np.where(predicate(A, F))[0]
                output[i][j] = measure(A[indices], F[indices]) if len(indices) > 0 else np.nan
    return output


from flask import request
from flask_restful import Resource, marshal, reqparse, marshal_with, fields

from app.system.models.follow import Follow
from app.system.models.company import Company
from app.system.services.lib_jwt import auth_jwt_required

parser_warning = reqparse.RequestParser()
parser_warning.add_argument('company_id', type=int)

parser_search_company = reqparse.RequestParser()
parser_search_company.add_argument('search', type=str, location="args")

resource_fields_follow_company = {
    "id": fields.Integer(),
    "company_name": fields.String(),
    "email": fields.String(),
    "city": fields.String(),
    "state": fields.String(),
    "photo_profile": fields.String()
}

resource_fields = {
    "id": fields.Integer(),
    "company": fields.Nested(resource_fields_follow_company)
}

class FollowResource(Resource):
    @auth_jwt_required
    @marshal_with(resource_fields)
    def post(self, current_user):
        args = parser_warning.parse_args(request)
        client_id = int(current_user.id)
        company_id = int(args.get("company_id"))
        # print(f"Client: {client_id}\nCompany: {company_id}")
        company = Follow.query.filter_by(
            company_id=company_id, client_id=client_id).first()
        if (company == None):
            new_follow = Follow(
                client_id=client_id,
                company_id=company_id
            )

            new_follow.save()

            return new_follow
        else:
            return None

    @auth_jwt_required
    @marshal_with(
        resource_fields)
    def get(self, current_user):
        follows = Follow.query.filter_by(client_id=int(current_user.id)).all()
        return [{"id": follow.id, "company": marshal(follow.company, resource_fields_follow_company)} for follow in follows]

class CompanyResource(Resource):
    @auth_jwt_required
    @marshal_with(
        resource_fields_follow_company)
    def get(self, current_user):
        args = parser_search_company.parse_args(request)
        searchText = args.get('search', '')
        search = f"%{searchText}%"
        print(search)
        return Company.query.filter(Company.company_name.like(
            search)).filter_by(state=current_user.state).all()


import asyncio
import json
from typing import Dict, List, Optional, Union

import discord
from discord.ext import commands
from discord.ext.commands import (
    BadArgument,
    CheckFailure,
    Converter,
    MessageConverter,
    TextChannelConverter,
)

class StringToEmbed(Converter):
    def __init__(self, content: bool = False):
        self.conversion_type = "json"  # currently only supports JSON
        self.allow_content = content

    async def convert(self, ctx: commands.Context, argument: str) -> discord.Embed:
        data = argument.strip("`")
        data = await self.load_from_json(ctx, data)
        content = self.get_content(data)

        if data.get("embed"):
            data = data["embed"]
        elif data.get("embeds"):
            data = data.get("embeds")[0]
        self.check_data_type(ctx, data)

        fields = await self.create_embed(ctx, data, content=content)
        embed = fields["embed"]
        return embed

    def check_data_type(self, ctx: commands.Context, data, *, data_type=(dict, list)):
        if not isinstance(data, data_type):
            raise BadArgument(
                f"This doesn't seem to be properly formatted embed {self.conversion_type.upper()}. "
                f"Use command `{ctx.bot.prefix}embed example` to see a JSON example."
            )

    async def load_from_json(self, ctx: commands.Context, data: str, **kwargs) -> dict:
        try:
            data = json.loads(data)
        except json.decoder.JSONDecodeError as error:
            return await self.embed_convert_error(ctx, "JSON Parse Error", error)
        self.check_data_type(ctx, data, **kwargs)
        return data

    def get_content(self, data: dict, *, content: str = None) -> Optional[str]:
        content = data.pop("content", content)
        if content is not None and not self.allow_content:
            raise BadArgument("The `content` field is not supported for this command.")
        return content

    async def create_embed(
        self, ctx: commands.Context, data: dict, *, content: str = None
    ) -> Dict[str, Union[discord.Embed, str]]:
        content = self.get_content(data, content=content)

        timestamp = data.get("timestamp")
        if timestamp:
            data["timestamp"] = timestamp.strip("Z")
        try:
            e = discord.Embed.from_dict(data)
            length = len(e)
            if length > 6000:
                raise BadArgument(f"Embed size exceeds Discord limit of 6000 characters ({length}).")
        except BadArgument:
            raise
        except Exception as error:
            return await self.embed_convert_error(ctx, "Embed Parse Error", error)
        return {"embed": e, "content": content}

    @staticmethod
    async def embed_convert_error(ctx: commands.Context, error_type: str, error: Exception):
        embed = discord.Embed(
            color=ctx.bot.main_color,
            title=f"{error_type}: `{type(error).__name__}`",
            description=f"```py\n{error}\n```",
        )
        embed.set_footer(text=f'Use "{ctx.prefix}embed example" to see an example')
        asyncio.create_task(ctx.send(embed=embed))
        raise CheckFailure

class ListStringToEmbed(StringToEmbed):
    async def convert(self, ctx: commands.Context, argument: str) -> List[discord.Embed]:
        data = argument.strip("`")
        data = await self.load_from_json(ctx, data, data_type=(dict, list))

        if isinstance(data, list):
            pass
        elif data.get("embed"):
            data = [data["embed"]]
        elif data.get("embeds"):
            data = data.get("embeds")
            if isinstance(data, dict):
                data = list(data.values())
        else:
            data = [data]
        self.check_data_type(ctx, data, data_type=list)

        embeds = []
        for embed_data in data:
            fields = await self.create_embed(ctx, embed_data)
            embed = fields["embed"]
            embeds.append(embed)
        if embeds:
            return embeds
        raise BadArgument

class StoredEmbedConverter(Converter):
    async def convert(self, ctx: commands.Context, name: str) -> dict:
        cog = ctx.cog
        data = await cog.db_config()  # can only be used within this cog
        embeds = data.get("embeds", {})
        embed = embeds.get(name)
        if not embed:
            raise BadArgument(f'Embed "{name}" not found.')

        return embed

    def __getitem__(self, item):
        return self[item]

class BotMessage(discord.Message):
    @classmethod
    async def convert(cls, ctx: commands.Context, argument: str) -> discord.Message:
        converter = MessageConverter()
        message = await converter.convert(ctx, argument)
        if message.author.id != ctx.me.id:
            raise BadArgument("That is not a message sent by me.")
        if not message.channel.permissions_for(ctx.me).send_messages:
            raise BadArgument(
                f"I do not have permissions to send/edit messages in {message.channel.mention}."
            )
        return message

class MessageableChannel(discord.TextChannel):
    @classmethod
    async def convert(cls, ctx: commands.Context, argument: str) -> discord.TextChannel:
        converter = TextChannelConverter()
        channel = await converter.convert(ctx, argument)
        my_perms = channel.permissions_for(ctx.me)
        if not (my_perms.send_messages and my_perms.embed_links):
            raise BadArgument(f"I do not have permissions to send embeds in {channel.mention}.")
        author_perms = channel.permissions_for(ctx.author)
        if not (author_perms.send_messages and author_perms.embed_links):
            raise BadArgument(f"You do not have permissions to send embeds in {channel.mention}.")
        return channel


#!/usr/local/bin/python

def GetLayerCodes(layer):
	if (layer == 0):
		textureCode = fltPolyTexture
		uCode = fltVU
		vCode = fltVV
	elif (layer == 1):
		textureCode = fltLayerTexture1
		uCode = fltLayerU1
		vCode = fltLayerV1
	elif (layer == 2):
		textureCode = fltLayerTexture2
		uCode = fltLayerU2
		vCode = fltLayerV2
	elif (layer == 3):
		textureCode = fltLayerTexture3
		uCode = fltLayerU3
		vCode = fltLayerV3
	elif (layer == 4):
		textureCode = fltLayerTexture4
		uCode = fltLayerU4
		vCode = fltLayerV4
	elif (layer == 5):
		textureCode = fltLayerTexture5
		uCode = fltLayerU5
		vCode = fltLayerV5
	elif (layer == 6):
		textureCode = fltLayerTexture6
		uCode = fltLayerU6
		vCode = fltLayerV6
	elif (layer == 7):
		textureCode = fltLayerTexture7
		uCode = fltLayerU7
		vCode = fltLayerV7
	return textureCode,uCode,vCode

def GetMeshUVMaskBit(layer):
	if (layer == 0):
		bit = MMESH_VTXUV0
	elif (layer == 1):
		bit = MMESH_VTXUV1
	elif (layer == 2):
		bit = MMESH_VTXUV2
	elif (layer == 3):
		bit = MMESH_VTXUV3
	elif (layer == 4):
		bit = MMESH_VTXUV4
	elif (layer == 5):
		bit = MMESH_VTXUV5
	elif (layer == 6):
		bit = MMESH_VTXUV6
	elif (layer == 7):
		bit = MMESH_VTXUV7
	return bit

class undoData():
	def __init__(self, layer):
		self.undoVtxList = []
		self.undoFaceList = []
		self.undoMeshVtxList = []
		self.undoMeshVtxMaskList = []
		self.layer = layer
		self.textureCode,self.uCode,self.vCode = GetLayerCodes(layer)

class undoVtxUV():
	def __init__(self, vtx, uCode, vCode):
		self.vtx = vtx
		numAttr,code,self.u = mgGetAttList (vtx, uCode)
		numAttr,code,self.v = mgGetAttList (vtx, vCode)
	def swap(self, uCode, vCode):
		numAttr,code,u = mgGetAttList (self.vtx, uCode)
		numAttr,code,v = mgGetAttList (self.vtx, vCode)
		mgSetAttList (self.vtx, uCode, self.u, vCode, self.v)
		self.u = u
		self.v = v

class undoMeshVtxUV():
	def __init__(self, mesh, vtxIndex, layer):
		self.mesh = mesh
		self.vtxIndex = vtxIndex
		b,self.u,self.v = mgMeshGetVtxUV (mesh, vtxIndex, layer)
		if (not b):
			self.u = 0.0
			self.v = 0.0
	def swap(self, layer):
		b,u,v = mgMeshGetVtxUV (self.mesh, self.vtxIndex, layer)
		mgMeshSetVtxUV (self.mesh, self.vtxIndex, layer, self.u, self.v)
		if (b):
			self.u = u
			self.v = v
		else:
			self.u = 0.0
			self.v = 0.0

class undoMeshVtxMask():
	def __init__(self, mesh):
		self.mesh = mesh
		self.mask = mgMeshGetVtxMask (mesh)
	def swap(self, layer):
		mask = mgMeshGetVtxMask (self.mesh)
		mgMeshSetVtxMask (self.mesh, self.mask)
		self.mask = mask

class undoFaceTextureIndex():
	def __init__(self, face, textureCode):
		self.face = face
		numAttr,code,self.textureIndex = mgGetAttList (face, textureCode)
	def swap(self, textureCode):
		numAttr,code,index = mgGetAttList (self.face, textureCode)
		mgSetAttList (self.face, textureCode, self.textureIndex)
		self.textureIndex = index

def RedoFunc (db, undoData):
	for i in undoData.undoVtxList:
		i.swap(undoData.uCode, undoData.vCode)
	for j in undoData.undoFaceList:
		j.swap(undoData.textureCode)
	for k in undoData.undoMeshVtxList:
		k.swap(undoData.layer)
	# for redo, have to set the mask after the UVs
	for l in undoData.undoMeshVtxMaskList:
		l.swap(undoData.layer)
	mgEditorAddUndoForRedo (UndoFunc, UndoCleanupFunc, undoData)

def UndoFunc (db, undoData):
	for i in undoData.undoVtxList:
		i.swap(undoData.uCode, undoData.vCode)
	for j in undoData.undoFaceList:
		j.swap(undoData.textureCode)
	# for undo, have to set the mask before the UVs
	for l in undoData.undoMeshVtxMaskList:
		l.swap(undoData.layer)
	for k in undoData.undoMeshVtxList:
		k.swap(undoData.layer)
	mgEditorAddRedo (RedoFunc, UndoCleanupFunc, undoData)

def UndoCleanupFunc (db, reason, undoList):
	pass

class deleteLayerData():
	def __init__(self, db, layer):
		self.layer = layer
		self.undoData = undoData (layer)
		self.textureCode = self.undoData.textureCode
		self.uCode = self.undoData.uCode
		self.vCode = self.undoData.vCode
	def appendUndo(self, faceOrMesh):
		undoFace = undoFaceTextureIndex (faceOrMesh, self.textureCode)
		self.undoData.undoFaceList.append (undoFace)
		if (mgGetCode(faceOrMesh) == fltPolygon):
			vtx = mgGetChild(face)
			while (vtx):
				undoVtx = undoVtxUV (vtx, self.uCode, self.vCode)
				self.undoData.undoVtxList.append (undoVtx)
				vtx = mgGetNext (vtx)
		else:
			undoMeshMask = undoMeshVtxMask(faceOrMesh)
			self.undoData.undoMeshVtxMaskList.append(undoMeshMask)
			numAttr,code,numVtx = mgGetAttList(faceOrMesh, fltMeshNumVtx)
			for i in range(0,numVtx):
				undoMeshVtx = undoMeshVtxUV(faceOrMesh, i, self.layer)
				self.undoData.undoMeshVtxList.append(undoMeshVtx)

	def applyToFace(self, face):
		self.appendUndo(face)
		mgSetAttList (face, self.textureCode, -1)
		vtx = mgGetChild (face)
		while (vtx):
			mgSetAttList (vtx, self.uCode, 0.0, self.vCode, 0.0)
			vtx = mgGetNext (vtx)
	def applyToMesh(self, mesh):
		self.appendUndo(mesh)
		mgSetAttList (mesh, self.textureCode, -1)
		maskBit = GetMeshUVMaskBit (self.layer)
		currentMask = mgMeshGetVtxMask (mesh)
		newMask = currentMask & ~maskBit
		mgMeshSetVtxMask (mesh, newMask)
		numAttr,code,numVtx = mgGetAttList (mesh, fltMeshNumVtx)
		for i in range(0,numVtx):
			mgMeshSetVtxUV (mesh, i, self.layer, 0.0, 0.0)

def DeleteTextureLayerOnFace(face, data):
	data.applyToFace(face)

def DeleteTextureLayerOnMesh(mesh, data):
	data.applyToMesh(mesh)

def CheckPolygonOrMesh (db, parent, rec, data):
	code = mgGetCode (rec)
	if (code == fltPolygon):
		DeleteTextureLayerOnFace (rec, data)
	elif (code == fltMesh):
		DeleteTextureLayerOnMesh (rec, data)
	return MG_TRUE

def DeleteTextureLayer(layer):
	toolName = "Delete Texture Layer"

	editorContext = mgNewEditorContext (toolName)

	if (not editorContext):
		mgSendMessage (MMSG_ERROR, "Failed creating Editor Context")
		return

	db = mgEditorGetDbRec (editorContext)

	geometry = mgGetSelectList (db)
	num = mgGetRecListCount (geometry)

	if (num == 0):
		mgSendMessage (MMSG_ERROR, "Nothing Selected")
		return

	if (layer == -1):
		# layer is "blend", cannot delete this layer
		mgSendMessage (MMSG_ERROR, "Cannot Delete Texture Layer, invalid layer specified")
		return

	data = deleteLayerData (db, layer)

	for i in range (0, num):
		rec,matrix = mgGetNextRecInList (geometry)
		code = mgGetCode(rec)
		if (code == fltPolygon):
			# this is a polygon, apply directly
			DeleteTextureLayerOnFace (rec, data)
		elif (code == fltMesh):
			# this is a mesh, apply directly
			DeleteTextureLayerOnMesh (rec, data)
		else:
			# visit all polygons/meshes below this node and apply to all found
			mgWalk (rec, CheckPolygonOrMesh, None, data, MWALK_NOREADONLY)

	mgEditorAddUndo (editorContext, toolName,
						UndoFunc, UndoCleanupFunc, data.undoData)

# change this to control which texture layer is deleted:
db = mgGetCurrentDb()
b, layer = mgGetCurrentTextureLayer(db)
DeleteTextureLayer (layer)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import random
import statistics

def valores():
    n = int(input("Informe o tamanho do conjunto númerico: "))
    i = 0
    lista = []

    for i in range(n):
        lista.append(float(input("Número: ")))

    lista = sorted(lista)
    return lista

def media_aritmetrica():
    n = valores()
    x = sum(n) / len(n)
    x = round(x, 2)
    print("A media aritmetrica desse cunjunto eh: ", x)

def media_ponderada():
    n = int(input("Informe o tamanho do conjunto númerico: "))
    i = 0
    y = 0
    z = 0
    a = 0

    for i in range(n):
        dados = float(input("Número: "))
        pesos = float(input("Peso: "))
        a += dados
        y += (dados*pesos)
        z += pesos
        i += 1

    try:
        x = float(y/z)
        x = round(x, 2)
        print("A media ponderada desse conjunto eh: ", x)
    except ZeroDivisionError:
        x = a/n
        print("A media desse conjunto eh: ", x)

def moda():
    n = valores()

    try:
        x = statistics.mode(n)
        print("A moda desse conjunto eh: ", x)
    except statistics.StatisticsError:
        print("\n <|||> ERRO: conjunto Amodal (sem moda) <|||> ")

def mediana():
    n = valores()
    x = statistics.median(n)
    print("A mediana desse conjunto eh: ", x)

def main():			# Função principal, montado com as funções anteriores...
    i = True

    print("""
		=======================================================
		||           <<<===>>> Medidas <<<===>>>             ||
		||~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~||
		||            <<<===>>> de <<<===>>>                 ||
		||~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~||
		||      <<<===>>> Tendencia Central <<<===>>>        ||
		=======================================================
	 """)

    while(i == True):
        print("\n	Informe o número correspondente para a opção desejada: ")
        print("	(1) Calcular media aritmetrica;")
        print("	(2) Calcular media ponderada;")
        print("	(3) Calcular moda;")
        print("	(4) Calcular mediana;")
        print("	(0) Para Encerrar;")
        op = int(input(">>> "))

        if(op < 0) or (op > 5):			# Caso a opção informada seja invalida
            print("\n <|||> ERRO: opção invalida... <|||> \n  **Por favor, reinicie o programa e imforme uma opção valida...")
            exit()

        elif(op == 1):
            media_aritmetrica()

        elif(op == 2):
            media_ponderada()

        elif(op == 3):
            moda()

        elif(op == 4):
            mediana()

        elif(op == 0) or (i == False):	# Encerra o programa
            print("	Obg por usar nossa Calculadora! ")
            print("				< by Alunos do 3º'I' 2019 > ")
            break

main()


import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

import cv2
from imutils import rotate
from imutils.video import WebcamVideoStream

from tensorflow.config.experimental import list_physical_devices
from tensorflow.config.experimental import set_memory_growth

from utils import *

def main():
    print('[INFO] Loading model...')
    model, time_taken = load_hand_model()
    print(f'[INFO] Loaded model in {time_taken:.2f}s')

    wheel = cv2.resize(cv2.imread('data/wheel.png', -1), (265, 265))

    forward_gear = cv2.resize(cv2.imread('data/gear-rod/forward.png', -1), (60, 300))
    normal_gear = cv2.resize(cv2.imread('data/gear-rod/normal.png', -1), (60, 300))
    reverse_gear = cv2.resize(cv2.imread('data/gear-rod/reverse.png', -1), (60, 300))

    current_gear = forward_gear

    src = WebcamVideoStream().start()
    trackers = None
    frame_count = 1

    while True:
        start = time()

        frame = cv2.flip(src.read(), 1)
        centroids = []

        if frame_count == 7:
            trackers = None
            frame_count = 1

        if trackers:
            ret, boxes = trackers.update(frame)
            for box in boxes:
                x, y, w, h = [int(coord) for coord in box]
                cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 1)

                cx, cy = (x + w//2), (y + h//2)
                cv2.circle(frame, (cx, cy), 3, (0, 255, 0), -1)
                centroids.append((cx, cy))

            frame_count += 1

        else:
            img_vector = cv2.resize(frame, (320, 320))[..., ::-1]
            img_vector = img_vector[newaxis, ...] / 255

            predictions = model(img_vector, training=False).numpy()

            num_det, boxes = locate_hands(predictions, frame.shape[:2])

            if num_det > 0:
                trackers = cv2.MultiTracker_create()
                for i in range(num_det):
                    y1, x1, y2, x2 = boxes[i]
                    cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)

                    cx, cy = (x1 + x2) // 2, (y1 + y2) // 2
                    cv2.circle(frame, (cx, cy), 3, (0, 255, 0), -1)
                    centroids.append((cx, cy))

                    trackers.add(cv2.TrackerKCF_create(), frame, (x1, y1, x2-x1, y2-y1))

        if len(centroids) > 1:
            right, left = (0, 1) if centroids[0][0] > centroids[1][0] else (1, 0)
            angle = int((centroids[left][1] - centroids[right][1]) / 4)

            left = centroids[left]
            right = centroids[right]

            if 20 <= left[0] <= 100:
                if left[1] <= 130:
                    current_gear = forward_gear
                    print('FORWARD', end='\r')

                elif 140 <= left[1] <= 340:
                    current_gear = normal_gear
                    print('NORMAL', end='\r')

                else:
                    current_gear = reverse_gear
                    print('REVERSE', end='\r')

        else:
            angle = 0

        overlay(frame, rotate(wheel, angle), current_gear)

        cv2.imshow('Frame', frame)
        if cv2.waitKey(1) == 27: break

        # print(f'[INFO] FPS : {1 / (time() - start):.2f}', end='\r')

    src.stop()
    cv2.destroyAllWindows()

devices = list_physical_devices('GPU')
if len(devices) > 0: set_memory_growth(devices[0], True)

main()


import sys
inp = lambda : sys.stdin.readline().rstrip()
N, M = map(int, inp().split())
# 새로운 키워드 작성
## 키워드 중 메모장에 있었던 키워드 삭제
## 없었으면 걍 넘기기

## 키워드 딕셔너리에 값들 넣기
keyword_set = {inp() for _ in range(N)}
for _ in range(M):
    keyword_list = list(inp().split(','))
    for k in keyword_list:
        if k not in keyword_set: continue
        keyword_set.remove(k)
    print(len(keyword_set))


from collections import deque

robot_name = input().split(";")
time = [int(ch) for ch in input().split(":")]
free_robot = {}
working_robot = {}
el_for_work = deque()

for ch in robot_name:
    new_ch = ch.split("-")
    robot_name = new_ch[0]
    seconds = int(new_ch[1])
    free_robot[robot_name] = seconds

while True:
    elements = input()

    if elements == "End":
        break

    el_for_work.append(elements)

while el_for_work:
    current_element = el_for_work.popleft()
    time[2] += 1

    if time[2] == 60:
        time[1] += 1
        time[2] = 0
        if time[1] == 60:
            time[1] = 0
            time[0] += 1
            if time[0] == 24:
                time[0] = 0

    for i in working_robot:
        working_robot[i] += 1

    for key in free_robot:
        if key in working_robot:
            if working_robot[key] == free_robot[key]:
                del working_robot[key]

    if len(free_robot) != len(working_robot):
        for chart in free_robot:
            if chart not in working_robot:
                print(f"{chart} - {current_element} [{time[0]:02d}:{time[1]:02d}:{time[2]:02d}]")
                working_robot[chart] = 0
                break
    else:
        el_for_work.append(current_element)


from matplotlib import pyplot as plt
import copy

class Bad_epoch:
	'''Object to store information about streches of EEG data that have artifacts.
	Contains boundaries (object) of type start and end with start sample with respect to start block
	Contains info about participant id experiment type block number and start sample of block
	'''

	def __init__(self,start_boundary = None,end_boundary = None,annotation = 'garbage',coder = 'martijn',color = 'blue', pp_id = None, exp_type = None, block_st_sample = None, bid = None, epoch_id = None, visible = True, correct = 'correct',perc_clean = -9,epoch_ids = '',block_et_sample = None):
		'''Information about stretch of eeg data containing artefacts.
		start_boundary 		boundary object with start sample number of type start
		end_boundary 		boundary object with end sample number of type end
		annotation 			label for the type of artefact
		color 				color of epoch in plot
		'''
		self.color_dict = {'heog':'green','jump':'pink','drift':'yellow','alpha':'green','garbage':'blue','movement':'cyan','unk':'red','blink':'magenta','ch-jump':'orange','artifact':'blue','clean':'white'}
		self.coder = coder
		self.visible = visible
		self.start = start_boundary
		self.end = end_boundary
		self.ok = False
		self.plotted = False
		self.color = color
		self.set_info()
		self.correct = correct
		self.perc_clean = perc_clean
		self.set_annotation( annotation )
		self.epoch_id = epoch_id
		self.check_boundaries()
		self.epoch_ids = epoch_ids
		self.block_et_sample = block_et_sample
		self.note = ''
		self.corrector = ''
		# print( (pp_id), exp_type, (bid), (block_st_sample), self.epoch_id)
		try: self.pp_id, self.exp_type, self.bid, self.block_st_sample = int(pp_id), exp_type, int(bid), int(block_st_sample)
		except:
			self.pp_id, self.exp_type, self.bid, self.block_st_sample = pp_id, exp_type, bid, block_st_sample
			print('missing id information to identify this bad epoch. If you are annotating, this is bad!')
		self.redraw = False

	def __str__(self):
		m = 'Epoch id:\t\t'+str(self.epoch_id) + '\n'
		if self.ok:
			m += 'pp id:\t\t\t'+str(self.pp_id) +'\n'
			m += 'exp type:\t\t'+str(self.exp_type)+'\n'
			m += 'block number:\t\t'+str(self.bid)+'\n'
			m += 'block_st_sample:\t'+str(self.block_st_sample)+'\n'
			m += 'st_sample:\t\t'+str(int(self.st_sample))+ '\n'
			m += 'et_sample:\t\t'+str(int(self.et_sample))+ '\n'
			m += 'duration:\t\t'+str(int(self.duration)) + '\n'
			m += 'annotation:\t\t'+str(self.annotation) + '\n'
		m += 'visible:\t\t'+str(self.visible)+ '\n'
		m += 'ok:\t\t\t'+str(self.ok)+ '\n'
		return m

	def __repr__(self):
		if self.ok: st, et = self.st_sample, self.et_sample
		else: st, et = 'NA','NA'
		return 'Bad_epoch-object:\t' + self.annotation.ljust(9)+ '\tstart: '+str(st) + '\tend: '+str(et) + '\tcoder: ' +self.coder + '\tok: ' + str(self.ok)

	def __eq__(self,other):
		if type(other) != Bad_epoch: return False
		return self.start == other.start and self.end == other.end

	def __lt__(self,other):
		if hasattr(self,'start') and hasattr(other, 'start'):
			return self.start < other.start

	def __gt__(self,other):
		if hasattr(self,'start') and hasattr(other, 'start'):
			return self.start > other.start

	def __contains__(self,boundary):
		if self.start == boundary:
			return True
		if self.end == boundary:
			return True
		return False

	def set_info(self):
		'''Set start time and end time of the bad_epoch.'''
		if self.ok:
			self.st_sample = int(round(self.start.x))
			self.et_sample = int(round(self.end.x))
			self.duration = self.et_sample - self.st_sample

	def plot(self,plot_annotation = True,plot_correct =True,selected = False):
		'''plot start / end boundary object add transparent color over time window and plot annotation epoch id.'''
		if not self.visible: return 0
		if self.start_boundary_ok and not self.start.plotted:
			self.start.plot()
		if self.end_boundary_ok and not self.end.plotted:
			self.end.plot()
		if self.ok and not self.plotted:
				if self.correct == 'incorrect': color = 'grey'
				else: color = self.color
				plt.axvspan(self.start.x,self.end.x, facecolor = color, alpha = 0.1)
				if selected:
					plt.axvspan(self.start.x,self.end.x,ymin = 0.05,ymax=0.95, facecolor = 'yellow', alpha = 0.4)
					plt.axvspan(self.start.x,self.end.x,ymin=0,ymax=0.05, facecolor = color, alpha = 0.1)
					plt.axvspan(self.start.x,self.end.x,ymin=0.95,ymax=1, facecolor = color, alpha = 0.1)
				else:
					plt.axvspan(self.start.x,self.end.x, facecolor = color, alpha = 0.1)
				if plot_annotation:
					self.plot_annotation(plot_correct)

	def set_complete_replot(self):
		'''Set flag to replot epoch (if something changed, e.g. boundary is deleted).'''
		self.plotted = False
		if self.start_boundary_ok: self.start.plotted = False
		if self.end_boundary_ok: self.end.plotted = False

	def in_plot_epoch(self,start,end):
		'''Check whether bad epoch and its boundaries are in the plot window defined by start and end.'''
		self.visible = False
		if self.ok:
			if self.end.x > start and self.end.x < end:
				self.visible = True
			if self.start.x < end and self.start.x >= start:
				self.visible = True
			if self.start.x < start and self.end.x >= end:
				self.visible = True
			self.start.in_plot_epoch(start,end)
			self.end.in_plot_epoch(start,end)

	def in_bad_epoch(self,x):
		'''Check whether x is within the bad epoch (to check whether mouse position is in this epoch).'''
		if self.ok and x >= self.start.x and x <= self.end.x: return True
		else: return False

	def check_boundaries(self):
		'''Check the state start and end boundaries, whether they are both there.'''
		self.start_boundary_ok = True
		self.end_boundary_ok = True
		self.last_ok = copy.copy(self.ok)
		self.ok = True
		self.empty = False
		if self.start == None:
			self.start_boundary_ok = False
		if self.end == None:
			self.end_boundary_ok = False

		if not self.start_boundary_ok or not self.end_boundary_ok:
			self.ok = False
		if not self.start_boundary_ok and not self.end_boundary_ok:
			print('epoch is empty')
			self.empty = True
		self.set_info()
		if self.ok: self.plot()

	def set_start(self,boundary):
		'''Set start boundary.'''
		if boundary.boundary_type == 'start':
			self.start = boundary
			self.check_boundaries()

	def set_end(self,boundary):
		'''Set end boundary.'''
		if boundary.boundary_type == 'end':
			self.end = boundary
			self.check_boundaries()

	def set_annotation(self,annotation):
		'''Set the annotation of the bad epoch.'''
		if type(annotation) == str:
			self.annotation = annotation
		if annotation in self.color_dict.keys(): self.color = self.color_dict[annotation]
		if self.visible: self.plot_annotation()
		self.redraw = True

	def set_correct(self,correct = 'correct'):
		if  self.correct != correct:
			self.redraw = True
		if correct == 'correct':
			self.correct = 'correct'
			# if self.annotation in self.color_dict.keys(): self.color = self.color_dict[self.annotation]
			# else: print('annotation:',self.annotation,'is not in color dict')
		elif correct == 'incorrect':
			self.correct = 'incorrect'
		else: print('unknown input:',correct,'please specify correct or incorrect')

	def add_note(self,note):
		if not hasattr(self,'note'):self.note = ''
		if self.note  == '':
			self.note = note
		else:
			n = self.note.split(',')
			n.append(note)
			self.note = ','.join(n)

	def set_corrector(self,corrector):
		self.corrector = corrector

	def plot_annotation(self,plot_correct = True):
		'''Plot the annotation to the plot window.'''
		ylow,yhigh = plt.ylim()
		if self.ok:
			center = (self.st_sample + self.et_sample) / 2
			if self.start.visible:
				plt.annotate(self.annotation,xy=(self.st_sample + 10,yhigh-50),color = 'black',fontsize=20)
				plt.annotate(str(self.epoch_id),xy=(self.st_sample + 50, ylow+50),color = 'black')
				plt.annotate(str(self.coder),xy=(self.st_sample + 50, ylow+30),color = 'black')
				if plot_correct:
					if self.correct == 'correct':
						plt.annotate('V', xy=(self.st_sample, ylow+30), color = 'green',fontsize=20)
					if self.correct == 'incorrect':
						plt.annotate('X', xy=(self.st_sample, ylow+30), color = 'red',fontsize=20)
			elif self.end.visible:
				plt.annotate(self.annotation,xy=(self.et_sample - 100,yhigh-50),color = 'black',fontsize =20)
				plt.annotate(str(self.epoch_id),xy=(self.et_sample - 100,ylow+50),color = 'black')
				plt.annotate(str(self.coder),xy=(self.et_sample - 100, ylow+30),color = 'black')
				if self.correct == 'correct':
					plt.annotate('V', xy=(self.et_sample - 50, ylow+30), color = 'green',fontsize=20)
				if self.correct == 'incorrect':
					plt.annotate('X', xy=(self.et_sample - 50, ylow+30), color = 'red',fontsize=20)

	def del_boundary(self,boundary):
		'''Delete the boundary that equals boundary or delete the boundary specified by the string 'start' or 'end'.'''
		self.redraw = True
		if boundary == 'start' or self.start == boundary:
			self.start = None
		elif boundary == 'end' or self.end == boundary:
			self.end = None
		else:
			self.redraw = False
			print('boundary not present in epoch')
		self.check_boundaries()

	def get_sample_info(self,multiplier =1):
		self.check_boundaries()
		if self.ok:
			return self.start.x * multiplier, self.end.x * multiplier, self.end.x * multiplier - self.start.x * multiplier
		else: return None

class Boundary:
	'''Object that contains information about the start or end of and bad epoch and provides methods to plot and
	check whether it should be plotted.'''

	def __init__(self,x,boundary_type,visible = True):
		''' Create boudnary object with time point and type to specify whether it is an end or start.
		x 				time point
		boundary_type 	str start or end to indicate type of boundary
		'''
		self.x = int(round(x))
		self.boundary_type = boundary_type
		self.visible = visible
		self.plotted = False
		self.plot()

	def __str__(self):
		m = 'Boundary type:\t\t'+ self.boundary_type + '\n'
		m += 'onset in ms: \t\t' + str(self.x) + '\n'
		m += 'visible:\t\t'+str(self.visible) + '\n'
		return m

	def __eq__(self,other):
		if other == None or type(other) != Boundary: return False
		return self.x == other.x and self.boundary_type == other.boundary_type

	def __repr__(self):
		return self.boundary_type + ' ' + str(self.x)

	def __lt__(self,other):
		if hasattr(other, 'x'):
			return self.x < other.x

	def __gt__(self,other):
		if hasattr(other, 'x'):
			return self.x > other.x

	def in_plot_epoch(self,start,end):
		'''checks whether boundary is in current plot window defined by start and end.'''
		if self.x > start and self.x < end: self.visible = True
		else: self.visible = False

	def plot(self):
		'''Plot a boundary line if it is visible in the current epoch.'''
		if self.visible and not self.plotted:

			if self.boundary_type == 'start':
				plt.axvline(self.x,color='tomato',linestyle='-',linewidth=1,alpha=0.5)
			if self.boundary_type == 'end':
				plt.axvline(self.x,color='tomato',linestyle='--',linewidth=1,alpha=0.5)
			self.plotted = True


from pythonds.graphs import Graph
from knightGraph import knightGraph

def knightTour(n,path,u,limit):
        print("Current Vertex:{}".format(u))
        u.setColor('gray')
        path.append(u)
        if n < limit:
            nbrList = list(u.getConnections())
            i = 0
            done = False
            while i < len(nbrList) and not done:
                if nbrList[i].getColor() == 'white':
                    done = knightTour(n+1, path, nbrList[i], limit)
                i = i + 1
            if not done:  # prepare to backtrack
                bt_vertex = path.pop()
                u.setColor('white')
                print("Backtracking vertex {}".format(bt_vertex))
        else:
            done = True
            print("Final path")
            for vertex in path:
                print(vertex)

        return done

path = []
kG = knightGraph(5)
knight_start_position = 15
v=kG.getVertex(knight_start_position)
number_of_squares_to_visit = 18
knightTour(0, path, v, number_of_squares_to_visit)


#
# (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
# AUTHOR   : A. Huck
# DATE     : June 2019
#
# Deep learning apply server
#
#

import sys
import argparse
import dgbpy.keystr as dgbkeys

parser = argparse.ArgumentParser(
          description='Server application of a trained machine learning model')
parser.add_argument( '-v', '--version',
            action='version',version='%(prog)s 2.0')
parser.add_argument( 'modelfile', type=argparse.FileType('r'),
                     help='The input trained model file' )
parser.add_argument( '--ppid',
                     dest='parentpid', action='store',
                     type=int, default=-1,
                     help='PID of the parent process' )
netgrp = parser.add_argument_group( 'Network' )
netgrp.add_argument( '--address',
            dest='addr', metavar='ADDRESS', action='store',
            type=str, default='localhost',
            help='Address to listen on' )
netgrp.add_argument( '--port',
            dest='port', action='store',
            type=int, default=65432,
            help='Port to listen on')
datagrp = parser.add_argument_group( 'Data' )
datagrp.add_argument( '--mldir',
            dest='mldir', nargs=1,
            help='Machine Learning Directory' )
datagrp.add_argument( '--Order',
            dest='applydir', type=str, default=dgbkeys.inlinestr,
            help='Apply direction for 2D models')
loggrp = parser.add_argument_group( 'Logging' )
loggrp.add_argument( '--log',
            dest='logfile', metavar='file', nargs='?',
            type=argparse.FileType('w'), default=sys.stdout,
            help='Progress report output' )
loggrp.add_argument( '--syslog',
            dest='sysout', metavar='stdout', nargs='?',
            type=argparse.FileType('w'), default=sys.stdout,
            help='System log' )
# optional
parser.add_argument( '--fakeapply', dest='fakeapply', action='store_true',
                     default=False,
                     help="applies a numpy average instead of the model" )
parser.add_argument( '--local', dest='localserv', action='store_true',
                     default=False,
                     help="use a local network socket connection" )

args = vars(parser.parse_args())
from odpy.common import *
initLogging( args )
redirect_stdout()

# Start listening as quickly as possible
import selectors
import socket
sel = selectors.DefaultSelector()
local = args['localserv']
host,port = args['addr'], args['port']
if local:
  addr = str(port)
  try:
    os.unlink(addr)
  except OSError:
    if os.path.exists(addr):
      raise
  host = 'LOCAL'
  sockfam = socket.AF_UNIX
else:
  addr = (host, port)
  sockfam = socket.AF_INET
lsock = socket.socket(sockfam, socket.SOCK_STREAM)
# Avoid bind() exception: OSError: [Errno 48] Address already in use
#lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
  lsock.bind(addr)
  lsock.listen()
except Exception as e:
  log_msg( 'Connection error for port', port, 'on host', host )
  log_msg( e )
  raise e
std_msg("listening on", addr)
lsock.setblocking(True)
sel.register(lsock, selectors.EVENT_READ, data=None)

# Keep all lengthy operations below
import numpy as np
import os
import psutil
import signal
import threading
import traceback

from odpy.common import Timer
import dgbpy.deeplearning_apply_serverlib as applylib

def signal_handler(signal, frame):
  raise applylib.ExitCommand()
signal.signal(signal.SIGINT,signal_handler)

def timerCB():
  if not parentproc.is_running():
    os.kill( psutil.Process().pid, signal.SIGINT )

def accept_wrapper(sock,applier):
  conn, addr = sock.accept()  # Should be ready to read
  conn.setblocking(True)
  message = applylib.Message(sel, conn, addr, applier)
  sel.register(conn, selectors.EVENT_READ, data=message)

timer = Timer(15, timerCB)
parentproc = None
if 'parentpid' in args:
  ppid = args['parentpid']
  if ppid > 0:
    parentproc = psutil.Process( ppid )
    timer.start()

applier = None
try:
  if applier == None:
    applier = applylib.ModelApplier( args['modelfile'].name, args['applydir'], args['fakeapply'] )
  lastmessage = False
  cont = True
  while cont:
    events = sel.select(timeout=300)
    for key, mask in events:
      if key.data is None:
        accept_wrapper(key.fileobj,applier)
      else:
        message = key.data
        try:
          message.process_events(mask)
        except Exception:
          log_msg( "main: error: exception for",
                  f"{message.addr}:\n{traceback.format_exc()}",
          )
          message.close()
        lastmessage = lastmessage or message.lastmessage
        cont = not lastmessage or len(events) > 1
        cont = cont
        if parentproc != None and not parentproc.is_running():
          cont = False
        if cont:
          applier = message.applier
except KeyboardInterrupt:
  std_msg('caught keyboard interrupt, exiting')
except applylib.ExitCommand:
  std_msg('Found dead parent, exiting')
finally:
  timer.cancel()
  sel.close()


import gi, sys

gi.require_version("Gtk", "3.0")
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk, Gdk, GLib

preview = Gtk.Builder()
preview.add_from_file("ui/preview.ui")

def updateCSS():
    provider.load_from_path(sys.argv[1] + "/gtk-3.0/gtk.css")
    return GLib.SOURCE_CONTINUE

class Handler:
    def destroy(self, *args):
        Gtk.main_quit()

preview.connect_signals(Handler())

Gtk.Settings.get_default().set_property("gtk-theme-name", "Empty")
provider = Gtk.CssProvider()
provider.load_from_path(sys.argv[1] + "/gtk-3.0/gtk.css")
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

window = preview.get_object("previewWindow")
window.show_all()

GLib.timeout_add(100,updateCSS)

Gtk.main()


import os

import json
import lxml.html as html

import schedule

from datetime import datetime
from datetime import date
from requests import Session
from requests.exceptions import ConnectionError
from lxml.html import Element
from time import sleep

from typing import Dict
from typing import Union

from lib import create_postgres_session
from lib import create_logger
from lib import News
from lib.typing_alias import Str
from lib.typing_alias import Bytes
from lib.typing_alias import Int
from lib.typing_alias import Date

# logging
log = create_logger("scraper")

def parse_news_block(element: Element, basic_url: Str) -> Union[Dict, None]:
    payload = dict()
    try:
        payload["news_title"] = element.xpath(".//span[@class='newslist__text-title']/text()")[0]
        payload["news_url"] = "".join([basic_url, element.xpath("./@href")[0]])
        payload["image_url"] = "".join([basic_url, element.xpath(".//img/@src")[0]])
    except IndexError:
        return None
    return payload

def parse_news_public_date(raw_date: Str) -> Union[Date, None]:
    months = {
        "января": 1,
        "февраля": 2,
        "марта": 3,
        "апреля": 4,
        "мая": 5,
        "июня": 6,
        "июля": 7,
        "августа": 8,
        "сентября": 9,
        "октября": 10,
        "ноября": 11,
        "декабря": 12
    }
    day, month, year = raw_date.strip().lower().split(" ")
    try:
        return date(int(year), months[month], int(day))
    except (ValueError, TypeError, KeyError):
        return None

def web_session_get(session: Session, url: Str) -> Union[Bytes, None]:
    try:
        log.info("Web session: {0}".format(url))
        response = session.get(url)
    except ConnectionError:
        log.error("connection error")
        return None
    if response.status_code == 200:
        return response.content
    else:
        log.error("status code")
        return None

def scrape_news_list(delay: Int = 2) -> None:

    basic_url = "https://www.mosmetro.ru"

    log.info("create Web session")
    with open("lib/user_agent.json") as f:
        user_agent = json.load(f)
    web_session = Session()
    web_session.headers.update(user_agent)

    log.info("create PostgreSQL session")
    postgres_engine, postgres_connection, postgres_session = create_postgres_session(os.environ["POSTGRES_URL"])

    content = web_session_get(web_session, "".join([basic_url, "/press/news/"]))
    if content:
        dom_news_list = html.fromstring(content)
        for item in dom_news_list.xpath("//a[@class='newslist__link']"):
            payload = parse_news_block(item, basic_url)
            payload["parse_date"] = datetime.today().date()

            log.info("News <{0}>".format(payload["news_url"]))

            where = News.news_url == payload["news_url"]
            news_exists = postgres_session.query(News).filter(where).first()
            if not news_exists:
                # extra public date
                dom_news = html.fromstring(web_session_get(web_session, payload["news_url"]))
                raw_public_date = dom_news.xpath("//div[@class='pagetitle__content-date']/text()")[0]
                payload["public_date"] = parse_news_public_date(raw_public_date)
                log.info("insert")
                postgres_session.add(News(**payload))
                postgres_session.commit()
                log.info("sleep")
                sleep(delay)
            else:
                log.info("exists")

    log.info("close Web session")
    web_session.close()
    log.info("close PostgreSQL session")
    postgres_connection.close()

def init_database(delay: Int = 15):
    log.info("create PostgreSQL session")
    postgres_engine, postgres_connection, postgres_session = create_postgres_session(os.environ["POSTGRES_URL"], delay)
    log.info("create table if required")
    News.__table__.create(postgres_engine, checkfirst=True)
    postgres_connection.close()
    log.info("close PostgreSQL session")
    postgres_connection.close()

if __name__ == "__main__":

    init_database(15)

    log.info("first round")
    scrape_news_list(1)  # step delay 1s

    minutes = 10
    log.info("schedule every {0} minutes".format(minutes))
    schedule.every(10).minutes.do(scrape_news_list, 5)  # step delay 5s

    while True:
        schedule.run_pending()
        sleep(1)