import os
import sys
import csv
import codecs
import timing

from hashlib import sha512
from collections import defaultdict

count = 0
hashcount = 0
dup_count = 0
c_arg = 0
file_ext = ('.pdf', '.dwg', '.dxf', '.sldprt',
            '.slddrw', '.sldasm', '.iam', '.ipt', '.ipn', '.idw', '.xml')
dup_dir = sys.argv[1]

def hashfile(path, blocksize=65536):
    with open(path, 'rb') as afile:
        hasher = sha512()
        while True:
            buf = afile.read(blocksize)
            if not buf:
                break
            hasher.update(buf)
    return hasher.hexdigest()

def assure_path_exists(path):
    try:
        dir = os.path.dirname(path)
        if not os.path.exists(dir):
                os.makedirs(dir)
    except Exception as e:
        print(e)

file_sizes = defaultdict(list)
for arg in sys.argv[2:]:
    c_arg += 1
    for root, dirs, filenames in os.walk(arg):
        for filename in filenames:
            if filename.lower().endswith(file_ext):
                file_path = os.path.join(root, filename)
                file_sizes[os.path.getsize(file_path)].append(file_path)
                count += 1
file_sizes = [x for x in file_sizes.values() if len(x) > 1]

files = defaultdict(list)
for paths in file_sizes:
    for path in paths:
        key = hashfile(path)
        files[key].append(path)
        hashcount += 1
        print(hashcount, end='\r')
files = [x for x in files.values() if len(x) > 1]
dup_count += len(files)

dupc = 0
dup_count2 = 0
with codecs.open('duplicates.csv', 'w', "utf-8") as csvfile:
    for duplicates in files:
        dupc += 1
        for dup in duplicates[1:]:
            try:
                opath = dup.split(os.path.sep)[-1]  # filename
                opath1 = dup.split(os.path.sep)[1:-1]  # dir no drive letter
                path2 = os.path.dirname(dup)
                opath2 = os.path.join(dup_dir, *opath1) + os.sep
                opath3 = os.path.join(opath2, opath)
                assure_path_exists(opath2)
                os.rename(dup, opath3)
                csvwriter = csv.writer(csvfile, delimiter=' ', quotechar='|')
                row = ['{}|{}|{}'.format(dupc,
                                         dup + '.duplicate',
                                         len(duplicates))]
                csvwriter.writerow(row)
                dup_count2 += 1
            except Exception as e:
                print(e)
                pass

print(dup_count, 'files with atleast one duplicate\n')
print('Total of ', dup_count2)


import sys, os, pygame, threading
from pydoc               import locate
from mpd                 import CommandError
from select              import select
from modules.MPControl   import MPControl

class UI:
    def __init__(self, mpdHost, mpdPort, mpdMusicDir, themeName, fps):

        Theme = locate('themes.'+themeName+'.'+themeName+'.Theme')

        self.__last_played_artist__    = ""
        self.__last_played_album__     = ""
        self.__last_cover_image__      = 0
        self.mpdHost                   = mpdHost
        self.mpdPort                   = mpdPort
        self.themeName                 = themeName
        self.framerate                 = fps
        self.over                      = False
        self.clock                     = pygame.time.Clock()
        self.mpcontrol                 = MPControl(mpdHost,mpdPort)
        self.mpcontrol.update()
        pygame.display.init()
        pygame.mouse.set_visible(False)
        self.screen                    = pygame.display.set_mode((0,0), pygame.FULLSCREEN, 0)
        self.theme                     = Theme(self.screen,mpdMusicDir)
        self.timeProgress              = 0

    def globalUpdate(self):
        self.mpcontrol.update()
        self.theme.handleInfos(self.mpcontrol.infos)
        self.theme.updateUI(self.timeProgress)
        self.mpcontrol.client.send_idle()

    def loop(self):
        canRead = select([self.mpcontrol.client], [], [], 0)[0]
        if canRead:
            result = self.mpcontrol.client.fetch_idle()
            self.timeProgress = 0
            self.globalUpdate()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.over = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    self.over = True
                else:
                    try:
                        self.mpcontrol.client.noidle()
                    except CommandError:
                        pass
                    self.mpcontrol.send(event.key)
                    self.mpcontrol.client.send_idle()

        if self.mpcontrol.infos["status"]["state"] == "play":
            self.timeProgress = self.timeProgress + (1/self.framerate)
            self.theme.updateUI(self.timeProgress)
        pygame.display.update()
        self.clock.tick(self.framerate)

    def load (self):
        self.globalUpdate()
        while not self.over:
            self.loop()
        try:
            self.mpcontrol.client.noidle()
        except CommandError:
            pass
        self.mpcontrol.client.close()
        self.mpcontrol.client.disconnect()
        pygame.quit()
        sys.exit(0)


import sys
import os

sys.path.append(os.path.abspath("."))
sys.dont_write_bytecode = True

__author__ = "bigfatnoob"

import numpy as np
import pandas as pd
import random
import inflect

from utils import cache, logger, lib
from misconceptions.common import datatypes

LOGGER = logger.get_logger(os.path.basename(__file__.split(".")[0]))
STORE_PATH = "/Users/panzer/Raise/ProgramRepair/CodeSeer/code/src/main/python/expt/store.pkl"
INFLECTOR = inflect.engine()

class VectorGenerator(lib.O):
  def __init__(self, **kwargs):
    self.n_values = 100
    self.types = [int, bool, float, str]
    self.int_range = [-100, 100]
    self.float_range = [-100.0, 100.0]
    self.max_levels = 10
    lib.O.__init__(self, **kwargs)

  def generate(self, vector_template):
    if vector_template is None or vector_template.type is None:
      fn = random.choice([self.generate_str, self.generate_bool, self.generate_int, self.generate_float])
      return fn(vector_template)
    elif vector_template.type == int:
      return self.generate_int(vector_template)
    elif vector_template.type == float:
      return self.generate_float(vector_template)
    elif vector_template.type == bool:
      return self.generate_bool(vector_template)
    elif vector_template.type == str:
      return self.generate_str(vector_template)

  def generate_int(self, vector_template):
    return np.random.randint(self.int_range[0], self.int_range[1]+1, self.n_values, int)

  def generate_float(self, vector_template):
    return np.random.uniform(low=self.float_range[0], high=self.float_range[1], size=(self.n_values,))

  def generate_bool(self, vector_template):
    return np.random.randint(0, 2, self.n_values, bool)

  def generate_str(self, vector_template):
    if vector_template is None or vector_template.levels is None:
      levels = [INFLECTOR.number_to_words(i) for i in xrange(self.max_levels)]
    else:
      levels = vector_template.levels
    return [random.choice(levels) for _ in xrange(self.n_values)]

class DataFrameGenerator(lib.O):
  def __init__(self, **kwargs):
    self.max_rand_rows = 100
    self.max_rand_cols = 20
    lib.O.__init__(self, **kwargs)

  def generate(self, df_template):
    if df_template is None:
      return generate_random_dataframe()
    else:
      n_cols = len(df_template.columns) + np.random.randint(0, self.max_rand_cols, dtype=int)
      n_rows = np.random.randint(1, self.max_rand_rows + 1)
      data = {}
      vectorGenerator = VectorGenerator(n_values=n_rows)
      for col_name, column in df_template.columns.items():
        data[col_name] = vectorGenerator.generate(column)
      for i in range(len(data), n_cols):
        col_name = "gen_col_%d" % i
        data[col_name] = vectorGenerator.generate(None)
      return pd.DataFrame(data=data)

def generate_dataframe(n_rows, n_cols, make_col_names):
  data = np.random.randint(-100, 100, (n_rows, n_cols))
  col_names = None
  if make_col_names:
    col_names = ["SLAAC_col%d" % (i + 1) for i in xrange(n_cols)]
  df = pd.DataFrame(data, columns=col_names)
  return df

def generate_named_dataframe(n_rows, n_cols, columns):
  col_names = list(columns.keys())
  data = np.random.randint(-100, 100, (n_rows, n_cols))
  pd_col_names = col_names + ["SLAAC_col%d" % (i + 1) for i in xrange(n_cols - len(col_names))]
  df = pd.DataFrame(data, columns=pd_col_names)
  col_headers = df.columns.values
  random.shuffle(col_headers)
  df = df[col_headers]
  return df

def generate_args_from_sample(sample_variables, n_args):
  generators = []
  for name, sample_variable in sample_variables.items():
    if type(sample_variable) == datatypes.DataFrame:
      generators.append(DataFrameGenerator())
    else:
      raise RuntimeError("For now we do not support '%s'" % type(datatypes.DataFrame))
  generated_args = []
  for _ in xrange(n_args):
    args = []
    for i, (name, sample_variable) in enumerate(sample_variables.items()):
      args.append(generators[i].generate(sample_variable))
    generated_args.append(args)
  return generated_args

# def generate_R_dataframe(n_rows, n_cols, make_col_names):
#   df = generate_dataframe(n_rows, n_cols, make_col_names)
#   return pandas2ri.py2ri(df)

def generate_random_dataframe(columns=None):
  delta = len(columns) if columns else 0
  n_rows = np.random.randint(1, 101)
  n_cols = np.random.randint(1 + delta, 20 + delta)
  if delta:
    return generate_named_dataframe(n_rows, n_cols, columns)
  else:
    make_col_names = np.random.choice([True, False])
    return generate_dataframe(n_rows, n_cols, make_col_names)

def make_key(data_types):
  comps = []
  sep = "$|$"
  for name, props in data_types.items():
    if datatypes.is_dataframe_type(props["type"]):
      if "col_names" in props and props["col_names"]:
        col_names = []
        for col_name, series in props["col_names"].items():
          col_names.append(col_name)
        comps.append("DataFrame[%s]" % "@|@".join(col_names))
      else:
        comps.append("DataFrame")
  return sep.join(comps)

def generate_args(data_types, n_args):
  args = []
  for _ in xrange(n_args):
    arg = []
    for arg_name, props in data_types.items():
      if datatypes.is_dataframe_type(props["type"]):
        if "col_names" in props:
          arg.append(generate_random_dataframe(props["col_names"]))
        else:
          arg.append(generate_random_dataframe())
      else:
        raise RuntimeError("Supports only dataframes")
    args.append(arg)
  return args

def store_args(key, args):
  data_store = cache.load_pickle(STORE_PATH)
  if not data_store:
    data_store = {}
  data_store[key] = args
  cache.save_pickle(STORE_PATH, data_store)

def load_args(data_types):
  key = make_key(data_types)
  data_store = cache.load_pickle(STORE_PATH)
  if data_store and key in data_store:
    arg_sets = data_store[key]
  else:
    LOGGER.info("Generating random arguments for the key '%s'" % key)
    arg_sets = generate_args(data_types, 100)
    store_args(key, arg_sets)
  return arg_sets

if __name__ == "__main__":
  # print(len(load_args([type(generate_R_dataframe(4, 3, True))])))
  # print(type(generate_R_dataframe(4, 3, True)).__name__)
  # print(convert_py_object_to_r(generate_dataframe(4, 3, True)))
  # print(generate_named_dataframe(3,5,["Hello", "World"]))
  print(VectorGenerator().generate_bool())


class Students():

    def __init__(self,name,contact_info):
        self.name = name
        self.contact_info = contact_info

    def getdata(self):
        print ("Accepting Data.")
        self.name = input ("Enter name:")
        self.contact_info = input ("Enter contact:")

    def putdata(self):
        print ("The Data I have is:")
        print ("Name: "+self.name)
        print ("Contact: "+self.contact_info)

obj = Students("Gaurang", 0)

obj.getdata()
obj.putdata()


# -*- coding:utf-8 -*-
# ！/usr/bin/env python
# @Time : 2023/7/26 11:44
# @Author : waxberry
# @File : handle_config.py
# @Software : PyCharm

import configparser

# 配置文件类
class HandleConfig:
    def operation_config(self, conf_file, section, option):
        cf = configparser.ConfigParser() #实例化
        cf.read(conf_file)
        value = cf.get(section, option) #定位
        return value

handle_config = HandleConfig()

if __name__ == '__main__':
    from base.base_path import *
    base_url = handle_config.operation_config(conf_path, 'BASEURL', 'base_url')
    print(base_url)


import os, sys
from os.path import dirname, abspath
import time

likely_lib_path = os.path.join(dirname(dirname(dirname(abspath(__file__)))), 'lib', 'p27_site_packages')
if os.path.exists(likely_lib_path):
    sys.path.insert(0, likely_lib_path)

import roslib
import rospy
from pacmod_msgs.msg import PacmodCmd, PositionWithSpeed
from openpose_ros_msgs.msg import OpenPoseHumanList
from std_msgs.msg import Bool, Float64
import time
input_topic = '/openpose_ros/human_list'
topic_prefix = '/pacmod/as_rx/'
pub_cmd_topic = {'brake': (topic_prefix+'brake_cmd', PacmodCmd),
                 'accel': (topic_prefix+'accel_cmd', PacmodCmd),
                 'turn': (topic_prefix+'turn_cmd', PacmodCmd),
                 'gear': (topic_prefix+'shift_cmd', PacmodCmd),
                 'steer': (topic_prefix+'steer_cmd', PositionWithSpeed),
                 'enable': (topic_prefix+'enable', Bool)}

pub_enable = rospy.Publisher(*pub_cmd_topic['enable'], queue_size=1)
pub_brake = rospy.Publisher(*pub_cmd_topic['brake'], queue_size=1) # queue_size not handled
pub_accel = rospy.Publisher(*pub_cmd_topic['accel'], queue_size=1)
pub_turn = rospy.Publisher(*pub_cmd_topic['turn'], queue_size=1)
pub_steer = rospy.Publisher(*pub_cmd_topic['steer'], queue_size=1)
pub_gear = rospy.Publisher(*pub_cmd_topic['gear'], queue_size = 1)
des_gear = PacmodCmd.SHIFT_FORWARD

accel_cmd = PacmodCmd()
prev_time = time.time()
desired_speed = 0.0
def speed_control(data):
    current_time = time.time()
    global accel_cmd
    global prev_time
    global dersired_speed
    current_time = time.time()
    delta_time = current_time - prev_time

    current_error = desired_speed - data.data
    #print ("desired speed: " + str(desired_speed))
    if not (desired_speed == 0 and data.data == 0):
        if current_error > -0.01 and delta_time > 3.0:
            output = 0.36
            accel_cmd.f64_cmd = output
            pub_accel.publish(accel_cmd)
        else:
            if delta_time > 3.0:
                prev_time = current_time  # update previous time after the receive the desired speed
            output = 0.31
            accel_cmd.f64_cmd = output
            pub_accel.publish(accel_cmd)

key = ''
def data_callback(data):
    if len(data.human_list) == 0:
        return
    global key
    center = data.human_list[0].body_key_points_with_prob[1]
    left_hand = data.human_list[0].body_key_points_with_prob[7]
    right_hand =  data.human_list[0].body_key_points_with_prob[4]
    right_elbow = data.human_list[0].body_key_points_with_prob[3]
    left_elbow = data.human_list[0].body_key_points_with_prob[6]

    center_exist = (center.x != 0 and center.y !=0)
    left_hand_exist = left_hand.x !=0 and left_hand.y != 0
    right_hand_exist = right_hand.x !=0 and right_hand.y != 0
    right_elbow_exist = right_elbow.x !=0 and right_elbow.y != 0
    left_elbow_exist = left_elbow.x !=0 and left_elbow.y != 0
    #if center.x == 0 and center.y == 0:
    #    print("drop frame because no center")
    #    key = ''
    #    return
    #if left_hand.x == 0 and left_hand.y == 0:
    #    print("drop frame because no left hand")
    #    key = ''
    #    return
    #if right_hand.x == 0 and right_hand.y == 0:
    #    print("drop frame because no right hand")
    #    key = ''
    #    return
    #if right_elbow.x == 0 and right_elbow.y == 0:
    #    print("drop frame because no right elbow")
    #    key = ''
    #    return
    #if left_elbow.x == 0 and left_elbow.y == 0:
    #    print("drop frame because no left elbow")
    #    key = ''
    #    return

    right_elbow_out = right_elbow_exist and right_hand_exist and (right_elbow.x - right_hand.x) < -5
    left_elbow_out = left_elbow_exist and left_hand_exist and (left_elbow.x - left_hand.x) > 5
    left_hand_raise = left_hand_exist and center_exist and (left_hand.y - center.y) < -10
    right_hand_raise = right_hand_exist and center_exist and (right_hand.y - center.y) < -10
    crossing = left_hand_exist and right_hand_exist and (right_hand.x - left_hand.x) > 15
    close_hand = left_hand_exist and right_hand_exist and (left_hand.x - right_hand.x) > 0 and  (left_hand.x - right_hand.x) <= 20

    #if crossing and left_hand_raise and right_hand_raise:
        #print('high cross')
        #print("setting speed to 1")
        #print(right_hand.x - left_hand.x)
        #key = 'sp1'
        #pub_enable.publish(True)
        #pub_gear.publish(ui16_cmd=des_gear, enable = True, ignore = False, clear = False)
    if crossing:
        print('low cross')
        print("setting speed to 0.5")
        print(right_hand.x - left_hand.x)
        key = 'sp0.5'
        #pub_enable.publish(True)
        #pub_gear.publish(ui16_cmd=des_gear, enable = True, ignore = False, clear = False)
    elif left_hand_raise and not right_hand_raise:
        print('only left hand raised')
        key = 'left_raise_only'
    elif right_hand_raise and not left_hand_raise:
        key = 'right_raise_only'
        print('only right hand raised')
    elif left_hand_raise and right_hand_raise and left_elbow_out and right_elbow_out: # crossing is already eliminated
	    #print (right)
        print('both left and right hand raised, stop')
        key = 'stop'
    elif close_hand:
        print("close hand")
        print("setting speed to 1")
        key = "sp1"
    else:
        #print("x cord")
        #print(right_hand.x - left_hand.x)
        #print("y cord")
        #print(right_hand.y - left_hand.y)
        key = ''
        print('no command detected')

rospy.init_node('gesture_ctrl', anonymous=True)
pose_sub = rospy.Subscriber(input_topic, OpenPoseHumanList, data_callback)
speed_sub = rospy.Subscriber("/pacmod/as_tx/vehicle_speed", Float64, speed_control)

brake_time = 0.5
signal_time = 2
steer_time = .5
accel_time = 2.0
turn_mag = 0
turn_increase = 2.0
pub_enable.publish(True)
pub_gear.publish(ui16_cmd=PacmodCmd.SHIFT_FORWARD, enable=True,ignore=False,clear = False)
while not rospy.is_shutdown():
    try:
        if key == 'stop':
            pub_enable.publish(True)
            pub_brake.publish(f64_cmd=0.5, enable=True)
            rospy.loginfo('sent brake command')
            t0 = time.time()
            time.sleep(brake_time)
            pub_brake.publish(f64_cmd=0.0, enable=True)
            desired_speed = 0.0
            rospy.loginfo('cancelled brake command, %.3f sec' % (time.time() - t0))

        # elif key in ('j', 'k'):
        #     pub_enable.publish(True)
        #     sig_dir, ui16_cmd = {'j': ('left', PacmodCmd.TURN_LEFT),
        #                          'k': ('right', PacmodCmd.TURN_RIGHT)}[key]
        #     pub_turn.publish(ui16_cmd=ui16_cmd, enable=True)
        #     rospy.loginfo('sent turn '+sig_dir+' command')
        #     t0 = time.time()
        #     time.sleep(signal_time)
        #     pub_turn.publish(ui16_cmd=PacmodCmd.TURN_NONE, enable=True)
        #     rospy.loginfo('cancelled turn %s command, %.3f sec' % (sig_dir, time.time() - t0))

        elif key in ('left_raise_only', 'right_raise_only'):
            pub_enable.publish(True)
            pub_brake.publish(f64_cmd=0.0, enable=True)
            steer_dir, th, w = {'right_raise_only': ('left', -turn_increase, +1.8),
                                'left_raise_only': ('right', turn_increase, +1.8)}[key]
            #steer_dir, th, w = {'right_raise_only': ('left',  -1.0, +0.2),
            #                    'left_raise_only': ('right', +1.0, +0.2)}[key]
            turn_mag += th
            if turn_mag > 0:
                turn_mag = min(8.0, turn_mag)
            elif turn_mag < 0:
                turn_mag = max(-8.0, turn_mag)
            steer_cmd = PositionWithSpeed()
            steer_cmd.angular_position = turn_mag
            steer_cmd.angular_velocity_limit = w
            #pub_steer.publish(angular_position=th, angular_velocity_limit=w, enable=True)
            pub_steer.publish(steer_cmd)
            rospy.loginfo('sent steer '+steer_dir+' command')
            t0 = time.time()
            time.sleep(steer_time)
            rospy.loginfo('ended steer %s command, %.3f sec' % (steer_dir, time.time() - t0))

        elif key == 'sp1':
            desired_speed = 1.0
            pub_enable.publish(True)
            pub_brake.publish(f64_cmd=0.0, enable=True)
            #pub_accel.publish(f64_cmd=0.4, enable=True)
            #rospy.loginfo('sent accelerate command')
            #t0 = time.time()
            #time.sleep(accel_time)
            #pub_accel.publish(f64_cmd=0.0, enable=True)
            #rospy.loginfo('cancelled accelerate command, %.3f sec' % (time.time() - t0))
        elif key == 'sp0.5':
            pub_enable.publish(True)
            pub_brake.publish(f64_cmd=0.0, enable=True)
            desired_speed = 0.5
        else:
            key = ''
    except KeyboardInterrupt:
        pub_enable.publish(False)
        exit(1)


def extractHobbycuresboredomBlogspotCom(item):
	'''
	Parser for 'hobbycuresboredom.blogspot.com'
	'''

	vol, chp, frag, postfix = extractVolChapterFragmentPostfix(item['title'])
	if not (chp or vol) or "preview" in item['title'].lower():
		return None

	tagmap = [
		('crc',       'Cultivator Returns to the City',                      'translated'),
		('lawmm',      'Life in Another World as a Maid Mage',                      'translated'),
		('eue',       'Era of Universal Evolution',                      'translated'),
		('IHASH',      'I Have A \'System\' Halo',                      'translated'),
		('PRC',       'PRC',                      'translated'),
		('Loiterous', 'Loiterous',                'oel'),
	]

	for tagname, name, tl_type in tagmap:
		if tagname in item['tags']:
			return buildReleaseMessageWithType(item, name, vol, chp, frag=frag, postfix=postfix, tl_type=tl_type)

	return False


import sys
import numpy
import scipy.optimize
from functools import partial

# Package Modules
from _utilities.plot_utilities import *                      # noqa: F401, F403
from _utilities.plot_utilities import load_plot_settings, save_plot, plt, \
        matplotlib, FormatStrFormatter
from _utilities.processing_time_utilities import TimeCounter, process_time, \
        restrict_computation_to_single_processor
from _utilities.data_utilities import generate_matrix, generate_noisy_data, \
        generate_basis_functions
from imate import InterpolateSchatten
from imate import traceinv

# ============================
# generalized cross validation
# ============================

def generalized_cross_validation(X, K, z, TI, shift, time_counter,
                                 use_log_theta, theta_):
    """
    Computes the CGV function :math:`V(\\theta)`.

    * ``X`` shape is ``(n, m)``.
    * :math:`K = X^{\\intercal} X`, which is ``(m, m)`` shape.

    **Reference:**

        Golub, G., & Von Matt, U. (1997).
        Generalized Cross-Validation for Large-Scale Problems.
        Journal of Computational and Graphical Statistics, 6(1), 1-34.
        doi: `10.2307-1390722 <https://www.jstor.org/stable/pdf/1390722.pdf?ref
        reqid=excelsior%3Adf48321fdd477aab0ea5dbf2542df01d>`_

        :param X: Matrix if basis functions of the shape ``(n, m)`` with ``m``
            basis functions over ``n`` spatial points.
        :type X: numpy.ndarray

        :param K: Correlation matrix of the shape ``(n, n)``.
        :type: numpy.ndarray or scipy.sparse.csc_matrix

        :param z: Column vector of data at ``n`` points.
        :type: numpy.array

        :param TI: imate interpolating object
        :type TI: imate.interpolateTraceInv

        :param shift: shift for the signular matrix ``K0`` to
            ``K = K0 + shift * I``.
        :type shift: float

        :param time_counter: A counter object to store the elapsed time and to
            be read outside of this function.
        :type TimeCounteR: examples._utilities.TimeCounter

        :param UseLog: A flag, if ``True``, it assumes ``theta_`` is in
            logarithmic scale. If ``False``, then ``theta_`` is not assumed to
            be in logarithmic scale.
        :type UseLog: bool

        :param theta_: Parameter of generalized cross validation.
        :type: float

        The generalized cross-validation (GCV) function is:

        .. math::

            V(\\theta) = \\frac{\\frac{1}{n} \\| \\mathbf{I} -
            \\mathbf{X} (\\mathbf{X}^{\\intercal} \\mathbf{X} +
            n \\theta \\mathbf{I})^{-1} \\mathbf{X}^{\\intercal}
            \\boldsymbol{z} \\|_2^2}{\\left( \\frac{1}{n} \\mathrm{trace}
            \\left( (\\mathbf{I} - \\mathbf{X}(\\mathbf{X}^{\\intercal}
            \\mathbf{X} + n \\theta \\mathbf{I})^{-1})\\mathbf{X}^{\\intercal}
            \\right) \\right)^2}

        In the above equation, the term involving trace is implemented
        differently depending on wtether :math:`n > m` or :math:`n < m` (see
        details in [GOLUB-1979]_).

        .. note::

            In this function, we use the variable ``theta_`` for
            :math:`\\theta`.

        **References:**

        .. [GOLUB-1979] Golub, G., Heath, M., & Wahba, G. (1979). Generalized
        Cross-Validation as a Method for Choosing a Good Ridge Parameter.
        Technometrics, 21(2), 215-223. doi: `10.2307/1268518
        <https://www.jstor.org/stable/1268518?seq=1>`_

    """

    # If theta is in the logarithm scale, convert it to normal scale
    if use_log_theta is True:
        theta_ = 10**theta_

    n, m = X.shape
    mu = n*theta_

    y1 = X.T.dot(z)
    Im = numpy.eye(m)
    A = X.T.dot(X) + mu * Im

    # Compute numerator
    y2 = numpy.linalg.solve(A, y1)
    y3 = X.dot(y2)
    y = z - y3
    numerator = numpy.linalg.norm(y)**2 / n

    # Compute denominator
    if n > m:
        if TI is not None:
            # Interpolate trace of inverse
            schatten_mu = TI.interpolate(mu-shift)
            trace_mu = m / schatten_mu
            trace = n - m + mu * trace_mu
        else:
            # Compute the exact value of the trace of inverse
            time0 = process_time()
            # Ainv = numpy.linalg.inv(A)
            # trace = n - m + mu * numpy.trace(Ainv)
            trace = n - m + mu * traceinv(A, method='cholesky')
            time1 = process_time()
            if time_counter is not None:
                time_counter.add(time1 - time0)
    else:
        if TI is not None:
            # Interpolate trace of inverse
            schatten_mu = TI.interpolate(mu-shift)
            trace_mu = n / schatten_mu
            trace = mu * trace_mu
        else:
            time0 = process_time()
            In = numpy.eye(n)
            B = X.dot(X.T) + mu * In
            # Binv = numpy.linalg.inv(B)
            # trace = mu * numpy.trace(Binv)
            trace = mu * traceinv(B, method='cholesky')
            time1 = process_time()
            if time_counter is not None:
                time_counter.add(time1 - time0)

    denominator = (trace / n)**2

    GCV = numerator / denominator

    return GCV

# ============
# minimize gcv
# ============

def minimize_gcv(X, K, z, TI, shift, theta_bounds, initial_elapsed_time,
                 time_counter):
    """
    Finds the parameter ``theta`` such that GCV is minimized.

    In this function, ``theta`` in logarithmic scale.
    """

    print('\nMinimize GCV ...')

    # Use theta in log scale
    use_log_theta = True
    bounds = [(numpy.log10(theta_bounds[0]), numpy.log10(theta_bounds[1]))]
    tolerance = 1e-4
    # guess_log_theta_ = -4

    # Partial function to minimize
    gcv_partial_function = partial(generalized_cross_validation,
                                   X, K, z, TI, shift, time_counter,
                                   use_log_theta)

    # Optimization methods
    time0 = process_time()

    # Local optimization method (use for both direct and presented method)
    # Method = 'Nelder-Mead'
    # results = scipy.optimize.minimize(gcv_partial_function,
    #       guess_log_theta_, method=Method, tol=tolerance,
    #       options={'maxiter':1000, 'xatol':tolerance, 'fatol':tolerance,
    #                'disp':True})
    #       callback=MinimizeTerminatorObj.__call__,

    # Global optimization methods (use for direct method)
    numpy.random.seed(31)   # for repeatability of results
    results = scipy.optimize.differential_evolution(
            gcv_partial_function, bounds, workers=1,
            tol=tolerance, atol=tolerance, updating='deferred', polish=True,
            strategy='best1bin', popsize=40, maxiter=200)  # Works well
    # results = scipy.optimize.dual_annealing(gcv_partial_function, bounds,
    #                                     maxiter=500)
    # results = scipy.optimize.shgo(gcv_partial_function, bounds,
    #             options={'minimize_every_iter': True, 'local_iter': True,
    #                      'minimizer_kwargs':{'method': 'Nelder-Mead'}})
    # results = scipy.optimize.basinhopping(gcv_partial_function,
    #                                   x0=guess_log_theta_)

    print(results)

    # Brute Force optimization method (use for direct method)
    # rranges = ((0.1, 0.3), (0.5, 25))
    # results = scipy.optimize.brute(gcv_partial_function, ranges=rranges,
    #                            full_output=True, finish=scipy.optimize.fmin,
    #                            workers=-1, Ns=30)
    # Optimal_DecorrelationScale = results[0][0]
    # Optimal_nu = results[0][1]
    # max_lp = -results[1]
    # Iterations = None
    # Message = "Using brute force"
    # Success = True

    time1 = process_time()
    elapsed_time = initial_elapsed_time + time1 - time0
    print('Elapsed time: %f\n' % elapsed_time)

    results = {
        'min_gcv': results.fun,
        'min_log_theta_': results.x[0],
        'min_theta_': 10.0**results.x[0],
        'fun_evaluations': results.nfev,
        'elapsed_time': elapsed_time
    }

    return results

# =================================
# plot generalized cross validation
# =================================

def plot_generalized_cross_validation(data, test):
    """
    Plots GCV for a range of theta_.

    data is a list of dictionaries, ``data[0], data[1], ...``.
    Each dictionary ``data[i]`` has the fields:

        * ``'theta_'``: x axis, this is the same for all dictionaries in the
          list.
        * ``'GCV'``: y axis data.
        * ``'label'``: the label of the data GCV in the plot.
    """

    # Load plot settings
    load_plot_settings()

    # Create a list of one item if data is not a list.
    if not isinstance(data, list):
        data = [data]

    theta_ = data[0]['theta_']

    textwidth = 5.25  # in inches
    fig, ax = plt.subplots(figsize=(textwidth, textwidth/1.458))
    colors_list = ["#000000",
                   "#1f77b4",
                   "#2ca02c",
                   "#d62728"]

    h_list = []
    for i in range(len(data)):
        h, = ax.semilogx(theta_, data[i]['GCV'], label=data[i]['label'],
                         color=colors_list[i])
        h_list.append(h)
        ax.semilogx(data[i]['minimization_result']['min_theta_'],
                    data[i]['minimization_result']['min_gcv'], 'o',
                    color=colors_list[i], markersize=3)

    ax.set_xlabel(r'$\theta$')
    ax.set_ylabel(r'$V(\theta)$')
    ax.set_title('Generalized Cross Validation')
    ax.set_xlim([theta_[0], theta_[-1]])

    GCV = data[0]['GCV']
    ax.set_yticks([numpy.min(GCV), numpy.min(GCV[:GCV.size//5])])
    ax.set_ylim([0.1634, numpy.max(GCV)+0.0001])
    ax.yaxis.set_major_formatter(FormatStrFormatter('%.4f'))

    ax.grid(True, axis='y')
    ax.legend(frameon=False, fontsize='small', bbox_to_anchor=(0.56, 0.24),
              loc='lower left')

    plt.tight_layout()

    # Save Plot
    filename = 'generalized_cross_validation'
    if test:
        filename = "test_" + filename
    save_plot(plt, filename)

    # If no display backend is enabled, do not plot in the interactive mode
    if (not test) and (matplotlib.get_backend() != 'agg'):
        plt.show()

# ====
# main
# ====

def main(test=False):
    """
    Run the script by

    ::

        python examples/Plot_generalized_cross_validation.py

    The script generates the figure below and prints the processing times of
    the computations. See more details in Figure 3 and results of Table 2 of
    [Ameli-2020]_.

    .. image:: https://raw.githubusercontent.com/ameli/imate/main/docs/images/g
               eneralized_cross_validation.svg
       :width: 550
       :align: center

    .. note::
        To *plot* GCV and trace estimations, compute trace with ``cholesky``,
        and *with* matrix inverse. That is, set ``invert_cholesky=True``.

    .. note::
        To properly *measure the elapsed-time* of minimizing GCV, do the
        followings:

        1. in the :func:`minimize_gcv`, use the *Differential Evolution*
           method, and  set ``worker=1`` (**NOT** ``-1``).
        2. Definitely call the function
            :func:`restrict_computation_to_single_processor()`
            to disable any multi-core processing. By this, all computations are
            forced to execute on a single thread. Otherwise, all measured
            elapsed times will be wrong due to the parallel processing.
            The only way that seems to measure elapsed time of multicore
            process properly is to prevent python to use multi-cores.
        3. Set the bound of search for ``theta`` to ``10e-16`` to ``10e+16``.
        4. Trace should be computed by either:
            * Hutchinson method
            * Cholesky factorization and without computing Inverse (set
            ``invert_cholesky=False``).

    .. warning::
        To compute the elapsed-time, do not compute trace with *stochastic
        Lanczos Quadrature* method, since for very small ``theta``, the
        tri-diagonalization fails.

    .. note::
        In the *rational polynomial functions * method for interpolation (using
        ``method='RPF'``), the variable ``q`` in the code represents the number
        of interpolant points. However, in the paper [Ameli-2020], the variable
        ``q`` represents the degree of the rational polynomial, As such, in the
        code we have ``q = 2``, and ``q = 4``, (num points) but in the plots in
        this script, they are labeled as ``q = 1`` and ``q = 2`` (degree of the
        rational polynomial).

    **References**

    .. [Ameli-2020] Ameli, S., and Shadden. S. C. (2020). Interpolating the
       Trace of the Inverse of Matrix **A** + t **B**. `arXiv:2009.07385
       <https://arxiv.org/abs/2009.07385>`__ [math.NA]
    """

    # When measuring elapsed time, restrict number of processors to a single
    # core only to measure time properly
    restrict_computation_to_single_processor()

    # shift to make singular matrix non-singular
    # shift = 2e-4
    # shift = 4e-4
    shift = 1e-3

    # Generate a nearly singular matrix
    if test:
        n = 100
        m = 50
    else:
        n = 1000
        m = 500
    noise_level = 4e-1
    X = generate_basis_functions(n, m)
    z = generate_noisy_data(X, noise_level)
    K = generate_matrix(X, n, m, shift)

    # Interpolating points
    scale = 5.0
    interpolant_points = [
            None,
            [1e-3, 1e-1],
            scale * numpy.logspace(-3, 0, 4),
            scale * numpy.logspace(-3, 0, 6)
    ]

    # Interpolating method
    # For plotting GCV and traces, use Cholesky with setting
    # invert_cholesky=True. However, to measure elapsed time, set
    # invert_cholesky=False.
    options = {'method': 'cholesky', 'invert_cholesky': True}
    kind = 'RPF'
    p = -1

    # Iterate over different set of interpolation points
    TI = []
    initial_elapsed_time = []

    for i in range(len(interpolant_points)):

        if interpolant_points[i] is not None:
            time0 = process_time()
            TI_ = InterpolateSchatten(K, p=p, ti=interpolant_points[i],
                                      kind=kind, options=options)
            time1 = process_time()
            initial_elapsed_time_ = time1 - time0
        else:
            TI_ = None
            initial_elapsed_time_ = 0.0

        TI.append(TI_)
        initial_elapsed_time.append(initial_elapsed_time_)

    # Minimize GCV
    # theta_bounds = (1e-4, 1e1)
    theta_bounds = (1e-16, 1e16)

    res = []
    for i in range(len(interpolant_points)):
        if TI[i] is None:
            time_counter = TimeCounter()
            res_ = minimize_gcv(X, K, z, None, shift, theta_bounds,
                                initial_elapsed_time[i], time_counter)
        else:
            time_counter = None
            res_ = minimize_gcv(X, K, z, TI[i], shift, theta_bounds,
                                initial_elapsed_time[i], time_counter)
        res.append(res_)

    # Compute GCV for a range of theta_
    if test:
        theta_Resolution = 50
    else:
        theta_Resolution = 500
    theta_ = numpy.logspace(-7, 1, theta_Resolution)

    plot_data = []
    for i in range(len(interpolant_points)):
        GCV = numpy.empty(theta_.size)
        use_log_theta = False
        for j in range(theta_.size):
            GCV[j] = generalized_cross_validation(X, K, z, TI[i], shift, None,
                                                  use_log_theta, theta_[j])

        if interpolant_points[i] is None:
            label = 'No interpolation (Exact)'
        else:
            label = 'Interpolation, $q = %d$' \
                    % (len(interpolant_points[i]) // 2)

        # Make a dictionary list of data for plots
        plot_data_ = {
            'theta_': theta_,
            'GCV': GCV,
            'label': label,
            'minimization_result': res[i]
        }

        # plot_data = [plot_data0, plot_data1, plot_data2]
        plot_data.append(plot_data_)

    # Plots
    plot_generalized_cross_validation(plot_data, test)

# ===========
# script main
# ===========

if __name__ == "__main__":
    sys.exit(main())


import unittest

from kafkajobs.jobqueue.queue import JobQueueProducer, JobQueueWorker

class TestQueues(unittest.TestCase):
    def test_pub_sub(self):
        consumer = JobQueueWorker('test_consumer',kafkaBootstrapUrl='localhost:9092',topicName='test_topic',appName='test_consumer', replication_factor=1)
        producer = JobQueueProducer('localhost:9092', 'test_topic', 'test_producer', replication_factor=1)
        producer.Enqueue('test_job', {'test': 'test'})
        print("Enqueued job")
        for i in range(0,30):
            job = consumer.TryGetNextJob(1000)
            if not(job is None):
                break
            else:
                print("No job found")
        consumer.Commit()
        self.assertDictEqual(job, {'test': 'test'}, "Should be {'test': 'test'}")

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


from mr_ckr import MRCKR

from rdflib.namespace import RDF, OWL
from rdflib import Graph

class Context(object):
    def __init__(self, name, namespace_manager):
        self.def_axiom_ctr = 0
        self.name = name
        self.axioms = Graph()
        self.axioms.namespace_manager = namespace_manager
        self.axioms.add((MRCKR.hasAxiomType_relation, RDF.type, OWL.AnnotationProperty))

    def add_axiom(self, axiom):
        self.axioms.add(axiom)

    def add_defeasible(self, relation_type, axiom):
        def_axiom = MRCKR.base.term(f"{self.name}_axiom_{self.def_axiom_ctr}")
        self.def_axiom_ctr += 1
        self.axioms.add((def_axiom, RDF.type, OWL.Axiom))
        self.axioms.add((def_axiom, MRCKR.hasAxiomType_relation, MRCKR.name_to_axiomtype[relation_type]))
        self.axioms.add((def_axiom, OWL.annotatedSource, axiom[0]))
        self.axioms.add((def_axiom, OWL.annotatedProperty, axiom[1]))
        self.axioms.add((def_axiom, OWL.annotatedTarget, axiom[2]))

    def to_file(self, path):
        self.axioms.serialize(f"{path}/m_{self.name}.n3", format="n3")
        if self.name == "global":
            with open(f"{path}/m_{self.name}.n3", 'a') as outfile:
                outfile.write("""<http://www.semanticweb.org/rkiesel/ontologies/2023/0/untitled-ontology-10#> a owl:Ontology ;
    owl:imports <http://dkm.fbk.eu/ckr/meta#> .""")


import numpy as np

import os
import torch
import torch.nn as nn

import torch.optim as optim

from torch.distributions import Normal, Categorical, Bernoulli
import torch.nn.functional as F

import sys
sys.path.insert(0, '../libraries/')

from  common_utils import get_bernoulli_loglik

import timeit

from copy import deepcopy

import itertools

softmax = nn.Softmax(dim = 0)

class NonLinearEncoder(nn.Module):
    def __init__(self, latent_dim = 200,
                    slen = 28):
        # the encoder returns the mean and variance of the latent parameters
        # given the image and its class (one hot encoded)

        super(NonLinearEncoder, self).__init__()

        # image / model parameters
        self.n_pixels = slen ** 2
        self.latent_dim = latent_dim
        self.slen = slen

        # define the linear layers
        self.fc1 = nn.Linear(self.n_pixels, 200)
        self.fc2 = nn.Linear(200, 200)
        self.fc3 = nn.Linear(200, self.latent_dim)

        self.sigmoid = nn.Sigmoid()

    def forward(self, image):

        # feed through neural network
        h = image.view(-1, self.n_pixels)

        h = F.leaky_relu(self.fc1(h))
        h = F.leaky_relu(self.fc2(h))
        latent_mean = self.sigmoid(self.fc3(h))

        return latent_mean

class NonLinearDecoder(nn.Module):
    def __init__(self, latent_dim = 200,
                    slen = 28):
        # the encoder returns the mean and variance of the latent parameters
        # given the image and its class (one hot encoded)

        super(NonLinearDecoder, self).__init__()

        # image / model parameters
        self.n_pixels = slen ** 2
        self.latent_dim = latent_dim
        self.slen = slen

        # define the linear layers
        self.fc1 = nn.Linear(self.latent_dim, 200)
        self.fc2 = nn.Linear(200, 200)
        self.fc3 = nn.Linear(200, self.n_pixels)

        self.sigmoid = nn.Sigmoid()

    def forward(self, latent_vars):

        # feed through neural network
        assert latent_vars.shape[1] == self.latent_dim

        h = F.leaky_relu(self.fc1(latent_vars))
        h = F.leaky_relu(self.fc2(h))
        image_mean = self.sigmoid(self.fc3(h)).view(-1, self.slen, self.slen)

        return image_mean

class VAE(nn.Module):
    def __init__(self, encoder, decoder):
        # the encoder returns the mean and variance of the latent parameters
        # given the image and its class (one hot encoded)

        super(VAE, self).__init__()

        assert encoder.latent_dim == decoder.latent_dim

        self.encoder = encoder
        self.decoder = decoder

    def forward(self, image):
        latent_means = self.encoder.forward(image)

        bernoulli_rv = Bernoulli(latent_means)
        bernoulli_samples = bernoulli_rv.sample().detach()

        image_mean = self.decoder.forward(bernoulli_samples)

        return image_mean, latent_means, bernoulli_samples

    def loss(self, image):
        # note this loss is not differentiable because of the
        # Bernoulli samples

        image_mean, latent_means, bernoulli_samples = self.forward(image)

        neg_log_lik = -get_bernoulli_loglik(image_mean, image)

        kl_latent_array = bernoulli_samples * \
                            np.log(2 * bernoulli_samples + 1e-8) + \
                    (1 - bernoulli_samples) * \
                            np.log(2 * (1 - bernoulli_samples) + 1e-8)
        kl_latent = kl_latent_array.sum(1)

        return neg_log_lik + kl_latent

#


import hashlib
import os
import subprocess
import sys
import tempfile
import unittest

from io import StringIO
from unittest import mock

import atest_error
import atest_utils
import constants
import unittest_utils
import unittest_constants

from test_finders import test_info

TEST_MODULE_NAME_A = 'ModuleNameA'
TEST_RUNNER_A = 'FakeTestRunnerA'
TEST_BUILD_TARGET_A = set(['bt1', 'bt2'])
TEST_DATA_A = {'test_data_a_1': 'a1',
               'test_data_a_2': 'a2'}
TEST_SUITE_A = 'FakeSuiteA'
TEST_MODULE_CLASS_A = 'FAKE_MODULE_CLASS_A'
TEST_INSTALL_LOC_A = set(['host', 'device'])
TEST_FINDER_A = 'MODULE'
TEST_INFO_A = test_info.TestInfo(TEST_MODULE_NAME_A, TEST_RUNNER_A,
                                 TEST_BUILD_TARGET_A, TEST_DATA_A,
                                 TEST_SUITE_A, TEST_MODULE_CLASS_A,
                                 TEST_INSTALL_LOC_A)
TEST_INFO_A.test_finder = TEST_FINDER_A
TEST_ZIP_DATA_DIR = 'zip_files'
TEST_SINGLE_ZIP_NAME = 'single_file.zip'
TEST_MULTI_ZIP_NAME = 'multi_file.zip'

REPO_INFO_OUTPUT = '''Manifest branch: test_branch
Manifest merge branch: refs/heads/test_branch
Manifest groups: all,-notdefault
----------------------------
'''

#pylint: disable=protected-access
class AtestUtilsUnittests(unittest.TestCase):
    """Unit tests for atest_utils.py"""

    def test_capture_fail_section_has_fail_section(self):
        """Test capture_fail_section when has fail section."""
        test_list = ['AAAAAA', 'FAILED: Error1', '^\n', 'Error2\n',
                     '[  6% 191/2997] BBBBBB\n', 'CCCCC',
                     '[  20% 322/2997] DDDDDD\n', 'EEEEE']
        want_list = ['FAILED: Error1', '^\n', 'Error2\n']
        self.assertEqual(want_list,
                         atest_utils._capture_fail_section(test_list))

    def test_capture_fail_section_no_fail_section(self):
        """Test capture_fail_section when no fail section."""
        test_list = ['[ 6% 191/2997] XXXXX', 'YYYYY: ZZZZZ']
        want_list = []
        self.assertEqual(want_list,
                         atest_utils._capture_fail_section(test_list))

    def test_is_test_mapping(self):
        """Test method is_test_mapping."""
        tm_option_attributes = [
            'test_mapping',
            'include_subdirs'
        ]
        for attr_to_test in tm_option_attributes:
            args = mock.Mock()
            for attr in tm_option_attributes:
                setattr(args, attr, attr == attr_to_test)
            args.tests = []
            args.host_unit_test_only = False
            self.assertTrue(
                atest_utils.is_test_mapping(args),
                'Failed to validate option %s' % attr_to_test)

        args = mock.Mock()
        for attr in tm_option_attributes:
            setattr(args, attr, False)
        args.tests = []
        args.host_unit_test_only = True
        self.assertFalse(atest_utils.is_test_mapping(args))

        args = mock.Mock()
        for attr in tm_option_attributes:
            setattr(args, attr, False)
        args.tests = [':group_name']
        args.host_unit_test_only = False
        self.assertTrue(atest_utils.is_test_mapping(args))

        args = mock.Mock()
        for attr in tm_option_attributes:
            setattr(args, attr, False)
        args.tests = [':test1', 'test2']
        args.host_unit_test_only = False
        self.assertFalse(atest_utils.is_test_mapping(args))

        args = mock.Mock()
        for attr in tm_option_attributes:
            setattr(args, attr, False)
        args.tests = ['test2']
        args.host_unit_test_only = False
        self.assertFalse(atest_utils.is_test_mapping(args))

    @mock.patch('curses.tigetnum')
    def test_has_colors(self, mock_curses_tigetnum):
        """Test method _has_colors."""
        # stream is file I/O
        stream = open('/tmp/test_has_colors.txt', 'wb')
        self.assertFalse(atest_utils._has_colors(stream))
        stream.close()

        # stream is not a tty(terminal).
        stream = mock.Mock()
        stream.isatty.return_value = False
        self.assertFalse(atest_utils._has_colors(stream))

        # stream is a tty(terminal) and colors < 2.
        stream = mock.Mock()
        stream.isatty.return_value = True
        mock_curses_tigetnum.return_value = 1
        self.assertFalse(atest_utils._has_colors(stream))

        # stream is a tty(terminal) and colors > 2.
        stream = mock.Mock()
        stream.isatty.return_value = True
        mock_curses_tigetnum.return_value = 256
        self.assertTrue(atest_utils._has_colors(stream))

    @mock.patch('atest_utils._has_colors')
    def test_colorize(self, mock_has_colors):
        """Test method colorize."""
        original_str = "test string"
        green_no = 2

        # _has_colors() return False.
        mock_has_colors.return_value = False
        converted_str = atest_utils.colorize(original_str, green_no,
                                             highlight=True)
        self.assertEqual(original_str, converted_str)

        # Green with highlight.
        mock_has_colors.return_value = True
        converted_str = atest_utils.colorize(original_str, green_no,
                                             highlight=True)
        green_highlight_string = '\x1b[1;42m%s\x1b[0m' % original_str
        self.assertEqual(green_highlight_string, converted_str)

        # Green, no highlight.
        mock_has_colors.return_value = True
        converted_str = atest_utils.colorize(original_str, green_no,
                                             highlight=False)
        green_no_highlight_string = '\x1b[1;32m%s\x1b[0m' % original_str
        self.assertEqual(green_no_highlight_string, converted_str)

    @mock.patch('atest_utils._has_colors')
    def test_colorful_print(self, mock_has_colors):
        """Test method colorful_print."""
        testing_str = "color_print_test"
        green_no = 2

        # _has_colors() return False.
        mock_has_colors.return_value = False
        capture_output = StringIO()
        sys.stdout = capture_output
        atest_utils.colorful_print(testing_str, green_no, highlight=True,
                                   auto_wrap=False)
        sys.stdout = sys.__stdout__
        uncolored_string = testing_str
        self.assertEqual(capture_output.getvalue(), uncolored_string)

        # Green with highlight, but no wrap.
        mock_has_colors.return_value = True
        capture_output = StringIO()
        sys.stdout = capture_output
        atest_utils.colorful_print(testing_str, green_no, highlight=True,
                                   auto_wrap=False)
        sys.stdout = sys.__stdout__
        green_highlight_no_wrap_string = '\x1b[1;42m%s\x1b[0m' % testing_str
        self.assertEqual(capture_output.getvalue(),
                         green_highlight_no_wrap_string)

        # Green, no highlight, no wrap.
        mock_has_colors.return_value = True
        capture_output = StringIO()
        sys.stdout = capture_output
        atest_utils.colorful_print(testing_str, green_no, highlight=False,
                                   auto_wrap=False)
        sys.stdout = sys.__stdout__
        green_no_high_no_wrap_string = '\x1b[1;32m%s\x1b[0m' % testing_str
        self.assertEqual(capture_output.getvalue(),
                         green_no_high_no_wrap_string)

        # Green with highlight and wrap.
        mock_has_colors.return_value = True
        capture_output = StringIO()
        sys.stdout = capture_output
        atest_utils.colorful_print(testing_str, green_no, highlight=True,
                                   auto_wrap=True)
        sys.stdout = sys.__stdout__
        green_highlight_wrap_string = '\x1b[1;42m%s\x1b[0m\n' % testing_str
        self.assertEqual(capture_output.getvalue(), green_highlight_wrap_string)

        # Green with wrap, but no highlight.
        mock_has_colors.return_value = True
        capture_output = StringIO()
        sys.stdout = capture_output
        atest_utils.colorful_print(testing_str, green_no, highlight=False,
                                   auto_wrap=True)
        sys.stdout = sys.__stdout__
        green_wrap_no_highlight_string = '\x1b[1;32m%s\x1b[0m\n' % testing_str
        self.assertEqual(capture_output.getvalue(),
                         green_wrap_no_highlight_string)

    @mock.patch('socket.gethostname')
    @mock.patch('subprocess.check_output')
    def test_is_external_run(self, mock_output, mock_hostname):
        """Test method is_external_run."""
        mock_output.return_value = ''
        mock_hostname.return_value = ''
        self.assertTrue(atest_utils.is_external_run())

        mock_output.return_value = 'test@other.com'
        mock_hostname.return_value = 'abc.com'
        self.assertTrue(atest_utils.is_external_run())

        mock_output.return_value = 'test@other.com'
        mock_hostname.return_value = 'abc.google.com'
        self.assertFalse(atest_utils.is_external_run())

        mock_output.return_value = 'test@other.com'
        mock_hostname.return_value = 'abc.google.def.com'
        self.assertTrue(atest_utils.is_external_run())

        mock_output.return_value = 'test@google.com'
        self.assertFalse(atest_utils.is_external_run())

        mock_output.return_value = 'test@other.com'
        mock_hostname.return_value = 'c.googlers.com'
        self.assertFalse(atest_utils.is_external_run())

        mock_output.return_value = 'test@other.com'
        mock_hostname.return_value = 'a.googlers.com'
        self.assertTrue(atest_utils.is_external_run())

        mock_output.side_effect = OSError()
        self.assertTrue(atest_utils.is_external_run())

        mock_output.side_effect = subprocess.CalledProcessError(1, 'cmd')
        self.assertTrue(atest_utils.is_external_run())

    @mock.patch('metrics.metrics_base.get_user_type')
    def test_print_data_collection_notice(self, mock_get_user_type):
        """Test method print_data_collection_notice."""

        # get_user_type return 1(external).
        mock_get_user_type.return_value = 1
        notice_str = ('\n==================\nNotice:\n'
                      '  We collect anonymous usage statistics'
                      ' in accordance with our'
                      ' Content Licenses (https://source.android.com/setup/start/licenses),'
                      ' Contributor License Agreement (https://opensource.google.com/docs/cla/),'
                      ' Privacy Policy (https://policies.google.com/privacy) and'
                      ' Terms of Service (https://policies.google.com/terms).'
                      '\n==================\n\n')
        capture_output = StringIO()
        sys.stdout = capture_output
        atest_utils.print_data_collection_notice()
        sys.stdout = sys.__stdout__
        uncolored_string = notice_str
        self.assertEqual(capture_output.getvalue(), uncolored_string)

        # get_user_type return 0(internal).
        mock_get_user_type.return_value = 0
        notice_str = ('\n==================\nNotice:\n'
                      '  We collect usage statistics'
                      ' in accordance with our'
                      ' Content Licenses (https://source.android.com/setup/start/licenses),'
                      ' Contributor License Agreement (https://cla.developers.google.com/),'
                      ' Privacy Policy (https://policies.google.com/privacy) and'
                      ' Terms of Service (https://policies.google.com/terms).'
                      '\n==================\n\n')
        capture_output = StringIO()
        sys.stdout = capture_output
        atest_utils.print_data_collection_notice()
        sys.stdout = sys.__stdout__
        uncolored_string = notice_str
        self.assertEqual(capture_output.getvalue(), uncolored_string)

    @mock.patch('builtins.input')
    @mock.patch('json.load')
    def test_update_test_runner_cmd(self, mock_json_load_data, mock_input):
        """Test method handle_test_runner_cmd without enable do_verification."""
        former_cmd_str = 'Former cmds ='
        write_result_str = 'Save result mapping to test_result'
        tmp_file = tempfile.NamedTemporaryFile()
        input_cmd = 'atest_args'
        runner_cmds = ['cmd1', 'cmd2']
        capture_output = StringIO()
        sys.stdout = capture_output
        # Previous data is empty. Should not enter strtobool.
        # If entered, exception will be raised cause test fail.
        mock_json_load_data.return_value = {}
        atest_utils.handle_test_runner_cmd(input_cmd,
                                           runner_cmds,
                                           do_verification=False,
                                           result_path=tmp_file.name)
        sys.stdout = sys.__stdout__
        self.assertEqual(capture_output.getvalue().find(former_cmd_str), -1)
        # Previous data is the same as the new input. Should not enter strtobool.
        # If entered, exception will be raised cause test fail
        capture_output = StringIO()
        sys.stdout = capture_output
        mock_json_load_data.return_value = {input_cmd:runner_cmds}
        atest_utils.handle_test_runner_cmd(input_cmd,
                                           runner_cmds,
                                           do_verification=False,
                                           result_path=tmp_file.name)
        sys.stdout = sys.__stdout__
        self.assertEqual(capture_output.getvalue().find(former_cmd_str), -1)
        self.assertEqual(capture_output.getvalue().find(write_result_str), -1)
        # Previous data has different cmds. Should enter strtobool not update,
        # should not find write_result_str.
        prev_cmds = ['cmd1']
        mock_input.return_value = 'n'
        capture_output = StringIO()
        sys.stdout = capture_output
        mock_json_load_data.return_value = {input_cmd:prev_cmds}
        atest_utils.handle_test_runner_cmd(input_cmd,
                                           runner_cmds,
                                           do_verification=False,
                                           result_path=tmp_file.name)
        sys.stdout = sys.__stdout__
        self.assertEqual(capture_output.getvalue().find(write_result_str), -1)

    @mock.patch('json.load')
    def test_verify_test_runner_cmd(self, mock_json_load_data):
        """Test method handle_test_runner_cmd without enable update_result."""
        tmp_file = tempfile.NamedTemporaryFile()
        input_cmd = 'atest_args'
        runner_cmds = ['cmd1', 'cmd2']
        # Previous data is the same as the new input. Should not raise exception.
        mock_json_load_data.return_value = {input_cmd:runner_cmds}
        atest_utils.handle_test_runner_cmd(input_cmd,
                                           runner_cmds,
                                           do_verification=True,
                                           result_path=tmp_file.name)
        # Previous data has different cmds. Should enter strtobool and hit
        # exception.
        prev_cmds = ['cmd1']
        mock_json_load_data.return_value = {input_cmd:prev_cmds}
        self.assertRaises(atest_error.DryRunVerificationError,
                          atest_utils.handle_test_runner_cmd,
                          input_cmd,
                          runner_cmds,
                          do_verification=True,
                          result_path=tmp_file.name)

    def test_get_test_info_cache_path(self):
        """Test method get_test_info_cache_path."""
        input_file_name = 'mytest_name'
        cache_root = '/a/b/c'
        expect_hashed_name = ('%s.cache' % hashlib.md5(str(input_file_name).
                                                       encode()).hexdigest())
        self.assertEqual(os.path.join(cache_root, expect_hashed_name),
                         atest_utils.get_test_info_cache_path(input_file_name,
                                                              cache_root))

    def test_get_and_load_cache(self):
        """Test method update_test_info_cache and load_test_info_cache."""
        test_reference = 'myTestRefA'
        test_cache_dir = tempfile.mkdtemp()
        atest_utils.update_test_info_cache(test_reference, [TEST_INFO_A],
                                           test_cache_dir)
        unittest_utils.assert_equal_testinfo_sets(
            self, set([TEST_INFO_A]),
            atest_utils.load_test_info_cache(test_reference, test_cache_dir))

    @mock.patch('os.getcwd')
    def test_get_build_cmd(self, mock_cwd):
        """Test method get_build_cmd."""
        build_top = '/home/a/b/c'
        rel_path = 'd/e'
        mock_cwd.return_value = os.path.join(build_top, rel_path)
        os_environ_mock = {constants.ANDROID_BUILD_TOP: build_top}
        with mock.patch.dict('os.environ', os_environ_mock, clear=True):
            expected_cmd = ['../../build/soong/soong_ui.bash', '--make-mode']
            self.assertEqual(expected_cmd, atest_utils.get_build_cmd())

    @mock.patch('subprocess.check_output')
    def test_get_modified_files(self, mock_co):
        """Test method get_modified_files"""
        mock_co.side_effect = [
            x.encode('utf-8') for x in ['/a/b/',
                                        '\n',
                                        'test_fp1.java\nc/test_fp2.java']]
        self.assertEqual({'/a/b/test_fp1.java', '/a/b/c/test_fp2.java'},
                         atest_utils.get_modified_files(''))
        mock_co.side_effect = [
            x.encode('utf-8') for x in ['/a/b/',
                                        'test_fp4',
                                        '/test_fp3.java']]
        self.assertEqual({'/a/b/test_fp4', '/a/b/test_fp3.java'},
                         atest_utils.get_modified_files(''))

    def test_delimiter(self):
        """Test method delimiter"""
        self.assertEqual('\n===\n\n', atest_utils.delimiter('=', 3, 1, 2))

    def test_has_python_module(self):
        """Test method has_python_module"""
        self.assertFalse(atest_utils.has_python_module('M_M'))
        self.assertTrue(atest_utils.has_python_module('os'))

    @mock.patch.object(atest_utils, 'matched_tf_error_log', return_value=True)
    def test_read_zip_single_text(self, _matched):
        """Test method extract_zip_text include only one text file."""
        zip_path = os.path.join(unittest_constants.TEST_DATA_DIR,
                                TEST_ZIP_DATA_DIR, TEST_SINGLE_ZIP_NAME)
        expect_content = '\nfile1_line1\nfile1_line2\n'
        self.assertEqual(expect_content, atest_utils.extract_zip_text(zip_path))

    @mock.patch.object(atest_utils, 'matched_tf_error_log', return_value=True)
    def test_read_zip_multi_text(self, _matched):
        """Test method extract_zip_text include multiple text files."""
        zip_path = os.path.join(unittest_constants.TEST_DATA_DIR,
                                TEST_ZIP_DATA_DIR, TEST_MULTI_ZIP_NAME)
        expect_content = ('\nfile1_line1\nfile1_line2\n\nfile2_line1\n'
                          'file2_line2\n')
        self.assertEqual(expect_content, atest_utils.extract_zip_text(zip_path))

    def test_matched_tf_error_log(self):
        """Test method extract_zip_text include multiple text files."""
        matched_content = '05-25 17:37:04 E/XXXXX YYYYY'
        not_matched_content = '05-25 17:37:04 I/XXXXX YYYYY'
        # Test matched content
        self.assertEqual(True,
                         atest_utils.matched_tf_error_log(matched_content))
        # Test not matched content
        self.assertEqual(False,
                         atest_utils.matched_tf_error_log(not_matched_content))

    @mock.patch('os.chmod')
    @mock.patch('shutil.copy2')
    @mock.patch('atest_utils.has_valid_cert')
    @mock.patch('subprocess.check_output')
    @mock.patch('os.path.exists')
    def test_get_flakes(self, mock_path_exists, mock_output, mock_valid_cert,
                        _cpc, _cm):
        """Test method get_flakes."""
        # Test par file does not exist.
        mock_path_exists.return_value = False
        self.assertEqual(None, atest_utils.get_flakes())
        # Test par file exists.
        mock_path_exists.return_value = True
        mock_output.return_value = (b'flake_percent:0.10001\n'
                                    b'postsubmit_flakes_per_week:12.0')
        mock_valid_cert.return_value = True
        expected_flake_info = {'flake_percent':'0.10001',
                               'postsubmit_flakes_per_week':'12.0'}
        self.assertEqual(expected_flake_info,
                         atest_utils.get_flakes())
        # Test no valid cert
        mock_valid_cert.return_value = False
        self.assertEqual(None,
                         atest_utils.get_flakes())

    @mock.patch('subprocess.check_call')
    def test_has_valid_cert(self, mock_call):
        """Test method has_valid_cert."""
        # raise subprocess.CalledProcessError
        mock_call.raiseError.side_effect = subprocess.CalledProcessError
        self.assertFalse(atest_utils.has_valid_cert())
        with mock.patch("constants.CERT_STATUS_CMD", ''):
            self.assertFalse(atest_utils.has_valid_cert())
        with mock.patch("constants.CERT_STATUS_CMD", 'CMD'):
            # has valid cert
            mock_call.return_value = 0
            self.assertTrue(atest_utils.has_valid_cert())
            # no valid cert
            mock_call.return_value = 4
            self.assertFalse(atest_utils.has_valid_cert())

    # pylint: disable=no-member
    def test_read_test_record_proto(self):
        """Test method read_test_record."""
        test_record_file_path = os.path.join(unittest_constants.TEST_DATA_DIR,
                                             "test_record.proto.testonly")
        test_record = atest_utils.read_test_record(test_record_file_path)
        self.assertEqual(test_record.children[0].inline_test_record.test_record_id,
                         'x86 hello_world_test')

    def test_is_valid_json_file_file_not_exist(self):
        """Test method is_valid_json_file if file not exist."""
        json_file_path = os.path.join(unittest_constants.TEST_DATA_DIR,
                                      "not_exist.json")
        self.assertFalse(atest_utils.is_valid_json_file(json_file_path))

    def test_is_valid_json_file_content_valid(self):
        """Test method is_valid_json_file if file exist and content is valid."""
        json_file_path = os.path.join(unittest_constants.TEST_DATA_DIR,
                                      "module-info.json")
        self.assertTrue(atest_utils.is_valid_json_file(json_file_path))

    def test_is_valid_json_file_content_not_valid(self):
        """Test method is_valid_json_file if file exist but content is valid."""
        json_file_path = os.path.join(unittest_constants.TEST_DATA_DIR,
                                      "not-valid-module-info.json")
        self.assertFalse(atest_utils.is_valid_json_file(json_file_path))

    @mock.patch('subprocess.check_output')
    @mock.patch('os.getenv')
    def test_get_manifest_branch(self, mock_env, mock_check_output):
        """Test method get_manifest_branch"""
        mock_env.return_value = 'any_path'
        mock_check_output.return_value = REPO_INFO_OUTPUT
        self.assertEqual('test_branch', atest_utils.get_manifest_branch())

        mock_env.return_value = 'any_path'
        mock_check_output.return_value = 'not_matched_branch_pattern.'
        self.assertEqual(None, atest_utils.get_manifest_branch())

        mock_env.return_value = 'any_path'
        mock_check_output.side_effect = subprocess.CalledProcessError(
            1,
            'repo info')
        self.assertEqual(None, atest_utils.get_manifest_branch())

        mock_env.return_value = None
        mock_check_output.return_value = REPO_INFO_OUTPUT
        self.assertEqual(None, atest_utils.get_manifest_branch())

    def test_has_wildcard(self):
        """Test method of has_wildcard"""
        self.assertFalse(atest_utils.has_wildcard('test1'))
        self.assertFalse(atest_utils.has_wildcard(['test1']))
        self.assertTrue(atest_utils.has_wildcard('test1?'))
        self.assertTrue(atest_utils.has_wildcard(['test1', 'b*', 'a?b*']))

    # pylint: disable=anomalous-backslash-in-string
    def test_quote(self):
        """Test method of quote()"""
        target_str = r'TEST_(F|P)[0-9].*\w$'
        expected_str = '\'TEST_(F|P)[0-9].*\w$\''
        self.assertEqual(atest_utils.quote(target_str), expected_str)
        self.assertEqual(atest_utils.quote('TEST_P224'), 'TEST_P224')

    @mock.patch('builtins.input', return_value='')
    def test_prompt_with_yn_result(self, mock_input):
        """Test method of prompt_with_yn_result"""
        msg = 'Do you want to continue?'
        mock_input.return_value = ''
        self.assertTrue(atest_utils.prompt_with_yn_result(msg, True))
        self.assertFalse(atest_utils.prompt_with_yn_result(msg, False))
        mock_input.return_value = 'y'
        self.assertTrue(atest_utils.prompt_with_yn_result(msg, True))
        mock_input.return_value = 'nO'
        self.assertFalse(atest_utils.prompt_with_yn_result(msg, True))

    def test_get_android_junit_config_filters(self):
        """Test method of get_android_junit_config_filters"""
        no_filter_test_config = os.path.join(
            unittest_constants.TEST_DATA_DIR,
            "filter_configs", "no_filter.cfg")
        self.assertEqual({},
                         atest_utils.get_android_junit_config_filters(
                             no_filter_test_config))

        filtered_test_config = os.path.join(
            unittest_constants.TEST_DATA_DIR,
            'filter_configs', 'filter.cfg')
        filter_dict = atest_utils.get_android_junit_config_filters(
            filtered_test_config)
        include_annotations = filter_dict.get(constants.INCLUDE_ANNOTATION)
        include_annotations.sort()
        self.assertEqual(
            ['include1', 'include2'],
            include_annotations)
        exclude_annotation = filter_dict.get(constants.EXCLUDE_ANNOTATION)
        exclude_annotation.sort()
        self.assertEqual(
            ['exclude1', 'exclude2'],
            exclude_annotation)

    def test_md5sum(self):
        """Test method of md5sum"""
        exist_string = os.path.join(unittest_constants.TEST_DATA_DIR,
                                    unittest_constants.JSON_FILE)
        inexist_string = os.path.join(unittest_constants.TEST_DATA_DIR,
                                      unittest_constants.CLASS_NAME)
        self.assertEqual(
            atest_utils.md5sum(exist_string), 'c26aab9baae99bcfb97633b69e9ceefd')
        self.assertEqual(
            atest_utils.md5sum(inexist_string), '')

    def test_check_md5(self):
        """Test method of check_md5"""
        file1 = os.path.join(unittest_constants.TEST_DATA_DIR,
                            unittest_constants.JSON_FILE)
        checksum_file = '/tmp/_tmp_module-info.json'
        atest_utils.save_md5([file1], '/tmp/_tmp_module-info.json')
        self.assertTrue(atest_utils.check_md5(checksum_file))
        os.remove(checksum_file)
        self.assertFalse(atest_utils.check_md5(checksum_file))
        self.assertTrue(atest_utils.check_md5(checksum_file, missing_ok=True))

    def test_get_config_parameter(self):
        """Test method of get_config_parameter"""
        parameter_config = os.path.join(
            unittest_constants.TEST_DATA_DIR,
            "parameter_config", "parameter.cfg")
        no_parameter_config = os.path.join(
            unittest_constants.TEST_DATA_DIR,
            "parameter_config", "no_parameter.cfg")

        # Test parameter empty value
        self.assertEqual(set(),
                         atest_utils.get_config_parameter(
                             no_parameter_config))

        # Test parameter empty value
        self.assertEqual({'value_1', 'value_2', 'value_3', 'value_4'},
                         atest_utils.get_config_parameter(
                             parameter_config))

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


import asyncio

import websockets

uri = "ws://localhost:8765"

async def omg_we_are_sinking():
    nickname = 'burn_in_hell'
    async with websockets.connect(uri) as websocket:
        await websocket.send(nickname)
        await websocket.send('?')

        wait_peoples = True
        while wait_peoples:
            peoples = await websocket.recv()
            peoples = peoples.split(', ')
            if nickname in peoples:
                wait_peoples = False

        peoples.remove(nickname)
        while True:
            for name in peoples:
                for _ in range(10):
                    await websocket.send(f'{name}: Памагите')
            await asyncio.sleep(1)

loop = asyncio.get_event_loop()
loop.run_until_complete(omg_we_are_sinking())


""" 36. Valid Sudoku
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

Each row must contain the digits 1-9 without repetition.
Each column must contain the digits 1-9 without repetition.
Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

A partially filled sudoku which is valid.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

Example 1:

Input:
[
  ["5","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
Output: true
Example 2:

Input:
[
  ["8","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being
    modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Note:

A Sudoku board (partially filled) could be valid but is not necessarily solvable.
Only the filled cells need to be validated according to the mentioned rules.
The given board contain only digits 1-9 and the character '.'.
The given board size is always 9x9.
"""
class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        # check rows
        #numberFreq = {}
        for row in range(len(board)):
            numberFreq = {}
            for col in range(len(board[row])):
                if board[row][col] == ".": continue
                if board[row][col] not in numberFreq: numberFreq[board[row][col]] = 1
                else: return False
            #numberFreq.clear()

        # check columns
        for col in range(len(board[0])):
            numberFreq = {}
            for row in range(len(board)):
                if board[row][col] == ".": continue
                if board[row][col] not in numberFreq: numberFreq[board[row][col]] = 1
                else: return False
            #numberFreq.clear()

        # check sub-boxes
        for i in range(9):
            # i/3 -> 0,1,2 (0, 3, 6) signify different sub-box rows
            # i%3 -> 0,1,2 signify different sub-box columns
            numberFreq = {}
            subBoxRow, subBoxCol = int(i/3), i%3
            for row in range(subBoxRow * 3, subBoxRow * 3 + 3):
                for col in range(subBoxCol * 3, subBoxCol * 3 + 3):

                    if board[row][col] == ".": continue
                    if board[row][col] not in numberFreq: numberFreq[board[row][col]] = 1
                    else: return False
            #numberFreq.clear()
        return True


import numpy as np
import pandas as pd

# path to xlsx file
path = "permissions.xlsx"

users = pd.read_excel(path, sheet_name="Users")
stores = pd.read_excel(path, sheet_name="Stores")
users.rename(columns={"Unnamed: 0": "user_id"}, inplace=True)
stores.rename(columns={"Unnamed: 0": "store_id"}, inplace=True)
users.set_index("user_id", inplace=True)
stores.set_index("store_id", inplace=True)

empty_dict = {"user_id": [], "store_id": [], "flag": []}

def create_new_table(new_dict: dict, users: pd.DataFrame, stores: pd.DataFrame) -> dict:
    for user in users.index:
        temp_user = users[users.index == user].dropna(axis=1)
        if "Region" in temp_user.columns:
            temp_user["Region"] = temp_user["Region"].astype(np.int64)
        for store in stores.index:
            temp_store = stores[stores.index == store].filter(items=temp_user.columns)
            new_dict["user_id"].append(user)
            new_dict["store_id"].append(store)
            if temp_user.loc[user].equals(temp_store.loc[store]):
                new_dict["flag"].append(True)
            else:
                new_dict["flag"].append(False)
    return new_dict

def df_to_csv(df: pd.DataFrame) -> None:
    df.to_csv("results_users/permissions_user.csv", sep=",", header=False, index=False)

if __name__ == "__main__":
    dict_result = create_new_table(empty_dict, users, stores)
    final_df = pd.DataFrame(dict_result)
    df_to_csv(final_df)


from System import System

def run():
    system = System()
    while True:
        try:
            system.recoverDataUsers()
            print("\t Authentication System")
            print("1) Register")
            print("2) Login")
            print("3) Problems with your account")
            print("4) Show users")
            print("5) Exit")
            option = int(input("Enter an option: "))
            if option == 1:
                system.createUser()
            elif option == 2:
                system.loginUser()
            elif option == 3:
                system.problemsUser()
            elif option == 4:
                system.showUsersInfo()
            elif option == 5:
                break
        except:
            print("Please, enter a validate option")
if __name__ == "__main__":
    run()


# Calculate the probability of an elderly person dying from a fall

# Consider the case where an elderly person (over 80 years of age) falls
# What is the probability that they will die from the fall?

# Assumptions
# P(A) - The base rate of someone elderly dying is 10%
# P(B) - The base rate for elderly people falling is 5%
# P(B|A) - 7% of all elderly people that die had a fall

# Calculate P(A|B) given P(B|A), P(A) and P(B)
# P(A|B) = P(B|A) * P(A) / P(B)
def bayes_theorem(p_a, p_b, p_b_given_a):
    p_a_given_b = p_b_given_a * p_a / p_b
    return p_a_given_b

# P(A)
p_a = 0.10

# P(B)
p_b = 0.05

# P(B|A)
p_b_given_a = 0.07

# Calculate P(A|B)
result = bayes_theorem(p_a, p_b, p_b_given_a)

# Summarize
print('P(A|B) = %.3f%%' % (result * 100))


"""m3ugen URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

from m3uservers.views import *

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home_view, name='home'),
    path('upload/', uploadM3U, name='uploadM3U'),
    path('list/', listM3U, name='listM3U'),
    #path('update/<str:id>/', updateM3U, name='updateM3U'),
    path('updateList/<str:id>/', updateList, name='updateList'),
    path('update_canal/<str:idm3u>/<str:idcanal>/', updateCanal, name='update_canal'),
    path('delete/<str:id>/', deleteM3U, name = 'delete'),
    path('reload/<str:id>/', reloadList, name = 'reloadList'),
    #path("canal/<str:id>/", updList, name = 'canal_list'),
    path("playlink/<str:idm3u>/<str:idCanal>", playLink, name = 'playLink'),

]


#!/usr/bin/env python

##############################################################################
#
# Name: lexf.py
# Date: 2019-03-23
# Author: kimoton
# Description:
# Problem
# Assume that an alphabet A has a predetermined order; that is, we write the
# alphabet as a permutation A=(a1,a2,…,ak), where a1<a2<⋯<ak. For instance, the
# English alphabet is organized as (A,B,…,Z).
#
# Given two strings s and t having the same length n, we say that s precedes
# t in the lexicographic order (and write s<Lext) if the first symbol s[j]
# that doesn't match t[j] satisfies sj<tj in A.
#
# Given: A collection of at most 10 symbols defining an ordered alphabet, and a
# positive integer n (n≤10).
#
# Return: All strings of length n that can be formed from the alphabet, ordered
# lexicographically (use the standard order of symbols in the English
# alphabet).
#
# Sample Dataset
# A C G T
# 2
# Sample Output
# AA
# AC
# AG
# AT
# CA
# CC
# CG
# CT
# GA
# GC
# GG
# GT
# TA
# TC
# TG
# TT
##############################################################################

import sys
import itertools
import operator

def get_lines(_file):
    with open(_file, "r") as rf:
        lines = rf.readlines()
    return lines

def get_all_permutations(components_list, length):
    """
    get all possible permutations by components_list
    """
    all_perm = list(itertools.product(components_list, repeat=length))
    sorted_all_perm = sorted(all_perm)
    return sorted_all_perm

def main():
    if len(sys.argv) != 2:
        print('usage {0:s} input_file'.format(sys.argv[0]))
        sys.exit(1)
    input_file = sys.argv[1]
    line1, line2 = get_lines(input_file)
    components_list = line1.split()
    length = int(line2)
    permutations = get_all_permutations(components_list, length)
    for i in permutations:
        print("".join(i))
    sys.exit(0)

if __name__ == '__main__':
    main()


import numpy as np
P = 50
B = 4
M = 3
N = 100000

nb = 0

for i in range(N):
    s = np.random.randint(0,P,M)
    fail = False
    for t in range(M):
        if (s[t] < B):
            fail = True
    if (not fail):
        nb += 1

print()
print("Prob no Boston in the fall = %0.4f" % (nb/N,))
print()


#!/usr/bin/python3

import sys

def fact(num):
    '''
    Function to find the factorial of a given number.

    args: a number
    returns: factorial of a number

    '''

    if num == 0:
        return 1
    else:
        return num * fact(num - 1)

def div(num):
    '''
    Function to divide 10 with the given number.

    args: a number
    returns: division of 10 by a number.

    '''
    res = 10/num
    return res

def main(num):
    res = fact(num)
    print(res)
    res = div(num)
    print(res)

if __name__ == '__main__':
    if len(sys.argv) == 2:
        if int(sys.argv[1]) < 0:
            print("Sorry, can't find the factorial of negative numbers")
        else:
            main(int(sys.argv[1]))
    else:
        print("SyntaxError: Invalid Syntax\n")
        print("Use \"./factorial.py <number>\"\n")


from __future__ import division, print_function, absolute_import
import os
import sys

import numpy as np
import tensorflow as tf
import argparse
from collections import OrderedDict
import copy
import pdb

from tfutils import base, optimizer
from tfutils.utils import online_agg
from tfutils.defaults import mean_and_reg_loss

CONVRNN_REPO_PATH = os.path.expanduser('~/ntcnet/convrnn_batch_norm_training')
sys.path.append(CONVRNN_REPO_PATH)
sys.path.append(os.path.join(CONVRNN_REPO_PATH, '../decoder_search/'))
from imagenet_dataprovider_gpu import ImageNet
from tnn_base_edges import tnn_base as tnn_base_fb

from gpu_train_script import get_parser, get_params_from_arg
slim = tf.contrib.slim

edges_2 = [(('conv8', 'conv5'), 0.0), (('conv9', 'conv6'), 0.0)]
edges_3 = edges_2 + [(('conv10', 'conv7'), 0.0)]
edges_5 = edges_3 + [(('conv7', 'conv6'), 0.0), (('conv10', 'conv9'), 0.0)]

def train_median_wfb(load_nm='medfb_configmodel438e5t0.npz',
                     cell_layers=['conv' + str(i) for i in range(4, 11)],
                     edges_arr=[],
                     input_args=[]):

    # Parse arguments
    parser = get_parser()
    args = parser.parse_args(input_args)

    # Get params needed, start training
    print(args)
    default_train_params = get_params_from_arg(args)

    params_copy = copy.copy(default_train_params)

    train_config = np.load(os.path.join(CONVRNN_REPO_PATH, 'model_config_npzs/', load_nm))['arr_0'][()]

    print('Loading config: ', train_config)

    model_params = train_config['model_params']

    cell_params = model_params.pop('cell_params')
    # add fb specific params
    cell_params['feedback_activation'] = tf.identity
    cell_params['feedback_entry'] = 'out'
    cell_params['feedback_depth_separable'] = False
    cell_params['feedback_filter_size'] = 1

    if args.layer_norm is not None:
        print('Setting layer norm to ', args.layer_norm)
        cell_params['layer_norm'] = args.layer_norm

    layer_params = {
            'conv1':{'cell_params': None},
            'conv2':{'cell_params':None},
            'conv3':{'cell_params':None},
            'conv4':{'cell_params':None},
            'conv5':{'cell_params':None},
            'conv6':{'cell_params':None},
            'conv7':{'cell_params':None},
            'conv8':{'cell_params':None},
            'conv9':{'cell_params':None},
            'conv10':{'cell_params':None},
            'imnetds':{'cell_params':None}}
    for k in cell_layers:
        print(k, 'layer norm', cell_params['layer_norm'])
        layer_params[k]['cell_params'] = cell_params

    model_params['layer_params'] = layer_params
    if args.times is not None:
        model_params['times'] = args.times
        print('Times now set to ', model_params['times'])

    if args.ff_bn:
        print('Setting ff batch norm to be true')
        bn_nodes_val = ['conv' + str(i) for i in range(1, 11)]
        model_params['bn_nodes'] = bn_nodes_val

    model_params['batch_norm_decay'] = args.batch_norm_decay
    model_params['batch_norm_epsilon'] = args.batch_norm_epsilon

    model_params['unroll_tf'] = args.unroll_tf
    if args.const_pres is not None:
        model_params['const_pres'] = args.const_pres

    if args.rnn_bn is not None:
        print('ARGS RNN bn', args.rnn_bn)
        model_params['rnn_bn'] = args.rnn_bn

    if args.rnn_bn_inp is not None:
        print('ARGS RNN bn inp', args.rnn_bn_inp)
        model_params['rnn_bn_inp'] = args.rnn_bn_inp

    if args.rnn_bn_all_convs is not None:
        print('ARGS RNN bn all convs', args.rnn_bn_all_convs)
        model_params['rnn_bn_all_convs'] = args.rnn_bn_all_convs
        model_params['edges_init_zero'] = args.edges_init_zero

    if args.batch_norm_cell_out is not None:
        print('ARGS RNN batch norm cell out', args.batch_norm_cell_out)
        model_params['batch_norm_cell_out'] = args.batch_norm_cell_out

    if args.gate_tau_bn_gamma_init is not None:
        print('ARGS RNN gate_tau_bn_gamma_init', args.gate_tau_bn_gamma_init)
        model_params['gate_tau_bn_gamma_init'] = args.gate_tau_bn_gamma_init

    lr_params = train_config['learning_rate_params']
    optimizer_params = train_config.get('optimizer_params')

    model_params['ff_weight_decay'] = None # use the values specified in json
    model_params['ff_kernel_initializer_kwargs'] = None # use the values specified in json
    model_params['final_max_pool'] = True
    if args.decoder_type == 'last':
        print('Using default last timestep decoder')
        model_params['decoder_end'] = model_params['times']
        model_params['decoder_start'] = model_params['decoder_end'] - 1
        model_params['decoder_type'] = 'last'
    else:
        print('Using ', args.decoder_type, ' decoder')
        model_params['decoder_end'] = model_params['times']
        model_params['decoder_start'] = 11 # first timestep of feedforward output
        model_params['decoder_type'] = args.decoder_type
        model_params['decoder_trainable'] = args.decoder_trainable

    # apply model params
    for k in model_params.keys():
        params_copy['model_params'][k] = model_params[k]

    params_copy['model_params']['func'] = tnn_base_fb
    params_copy['model_params']['base_name'] = args.tnn_json
    params_copy['model_params']['convrnn_type'] = 'recipcell'

    if args.use_edges:
        print('Passing in edges', edges_arr)
        params_copy['model_params']['edges_arr'] = edges_arr
    else:
        print('IGNORING any edges passed in')
        params_copy['model_params']['edges_arr'] = []

    if args.use_resnet:
        print('Using Resnet')
        params_copy['model_params'] = {'func': resnet_func, 'base_name': args.tnn_json, 'unroll_tf': args.unroll_tf}
        if args.times is not None:
           params_copy['model_params']['ntimes'] = args.times

    if args.use_google_resnet:
        print('Using Google Resnet')
        params_copy['model_params'] = {'func': google_resnet_func, 'resnet_size': args.resnet_size}

    init_lr_val = lr_params['learning_rate']
    if args.rescale_lr:
        print('Rescaling init lr')
        init_lr_val = (init_lr_val) * (args.train_batch_size / 128.0)
    if args.init_lr_fac10:
        print('Multiplying init lr by factor of 10')
        init_lr_val = init_lr_val * 10

    if args.use_resnet or args.use_google_resnet:
        init_lr_val = 0.1 * (args.train_batch_size / 256.0)

    if args.do_restore is not None:
        params_copy['load_params']['do_restore'] = True
        if args.load_step is not None:
            params_copy['load_params']['query'] = {'step': args.load_step}
        else:
            params_copy['load_params']['query'] = None

    if args.drop == 0:
        if (not args.use_resnet) and (not args.use_google_resnet):
            for k in lr_params.keys():
                params_copy['learning_rate_params'][k] = lr_params[k]
        params_copy['learning_rate_params']['learning_rate'] = init_lr_val
    elif args.drop > 0:
        prev_lr = ((0.1)**(args.drop-1))*(init_lr_val)
        next_lr = ((0.1)**args.drop)*(init_lr_val)
        print('Prev_lr:', prev_lr, 'next_lr:', next_lr, 'boundary_step:', args.boundary_step)
        params_copy['learning_rate_params'] = {'func': drop_learning_rate, 'boundary_step': args.boundary_step, 'next_lr': next_lr, 'prev_lr': prev_lr}

        params_copy['load_params']['do_restore'] = True
        if args.load_step is not None:
            params_copy['load_params']['query'] = {'step': args.load_step}
        else:
            params_copy['load_params']['query'] = None

    if args.load_exp is not None:
        params_copy['load_params']['exp_id'] = args.load_exp

    if (optimizer_params is not None) and (not args.use_resnet) and (not args.use_google_resnet):
        for k in optimizer_params.keys():
            params_copy['optimizer_params'][k] = optimizer_params[k]

    if 'base_name' in params_copy['model_params'].keys():
        print('BASE NAME', params_copy['model_params']['base_name'])
    if args.drop == 0:
        print('LR to be used: ', params_copy['learning_rate_params']['learning_rate'])
    else:
        print('LR to be used: ', params_copy['learning_rate_params']['next_lr'])
    return params_copy

def convrnn_model(inputs, input_args=[], units=128, *args, **kwargs):
    input_dict = {'images': inputs}

    default_args = "--tnn_json=10Lv9p2_imnet224_rrg128ctx_batchnorm_allones --const_pres=True --ff_bn=True --rnn_bn_all_convs=True --gate_tau_bn_gamma_init=0.1 --unroll_tf=True --times=7"
    input_args.extend(default_args.split())
    all_params = train_median_wfb(edges_arr=edges_5, input_args=input_args)

    model_params = all_params['model_params']
    model_params['out_layers'] = 'conv10'
    model_params['base_name'] = os.path.join(
            CONVRNN_REPO_PATH,
            model_params['base_name'])
    func = model_params.pop('func')
    model_params.update(kwargs)
    output, _ = func(input_dict, **model_params)
    output = tf.reduce_mean(output, [1, 2], keep_dims=False, name='global_pool')
    output = tf.layers.dense(
            inputs=output, units=units,
            kernel_initializer=tf.random_normal_initializer(stddev=0.01),
            )
    return output

def convrnn_imagenet_test():
    os.environ['CUDA_VISIBLE_DEVICES'] = '1,2,3,4,5,7,8,9'
    input_args = ['--gpu', '1,2,3,4,5,7,8,9']
    all_params = train_median_wfb(
            edges_arr=edges_5,
            input_args=input_args)

    old_model_params = all_params.pop('model_params')
    def _temp_model_func(inputs, *args, **kwargs):
        output = convrnn_model(
                inputs['images'], input_args=input_args,
                units=1000,
                *args, **kwargs)
        return output, {}
    new_model_params = {
            'func': _temp_model_func,
            'devices': old_model_params['devices'],
            'num_gpus': old_model_params['num_gpus'],
            }
    all_params['model_params'] = new_model_params
    all_params['save_params'] = {
            'host': 'localhost',
            'port': 27009,
            'dbname': 'convrnn',
            'collname': 'control',
            'exp_id': 'cate',
            'do_save': True,
            'save_initial_filters': True,
            'save_metrics_freq': 1000,
            'save_valid_freq': 10009,
            'save_filters_freq': 100090,
            'cache_filters_freq': 100090,
            }
    all_params['load_params'] = {
            'host': 'localhost',
            'port': 27009,
            'dbname': 'convrnn',
            'collname': 'control',
            'exp_id': 'cate',
            'do_restore': True,
            }
    all_params['validation_params'] = {}
    print(all_params.keys())
    base.train_from_params(**all_params)

if __name__ == '__main__':
    #inputs = tf.zeros(shape=[128, 224, 224, 3], dtype=tf.float32)
    #print(convrnn_model(inputs))
    convrnn_imagenet_test()