from tornado.web import RequestHandler
from tornado import gen
from server import ChangePwdServer
from sql import mysql
class ChangePwdHandler(RequestHandler):
    @gen.coroutine
    def post(self):
        stuid=self.get_argument('stuid','none')
        localcookie,oldpwd=self.get_argument('cookie','none'),self.get_argument('oldpwd','none')
        newpwd,newpwd1=self.get_argument('newpwd','none'),self.get_argument('newpwd1','none')
        if localcookie=='none' or oldpwd=='none' or stuid=='none' or newpwd=='none' or newpwd1=='none':
            self.finish("failure")
        res=ChangePwdServer.change_pwd_server(localcookie,oldpwd,newpwd,newpwd1)
        result=yield res
        if result==None:
            self.finish("reqfailure")
        if result.find('success:true')!=-1:
            # change db data
            try:
                mysql.Sqlutils().updateSelective('userlist',stuid,newpwd)
            except Exception:
                self.finish('success')
            self.finish('success')
        else:
            self.finish('failure')


#!/usr/bin/env python3

import rospy
import cv2

import time
import numpy as np

from ros_aibot_core.msg import MsgCtl
from ros_object_detect.msg import DetInfo

speedVal = 0.1
angleVal = 0.0

stopCmd = 0

def driveCtl(speedval, angleval, stopCmd):
    global speedVal
    global angleVal
    global ctl_pub

    if (speedval >= 1.0):
      speedval=1.0

    msg = MsgCtl()
    msg.speedCmd = speedval
    msg.headingCmd = angleval
    msg.dirCmd  = 1
    msg.stopCmd  = stopCmd
    #print("speedval:{} angleval:{} stopCmd:{}".format(speedval,angleval, msg.stopCmd))
    ctl_pub.publish(msg)

def detresCB(data):
    global speedVal
    global stopCmd
    #print("label:{} x1:{} x2:{}".format(data.label, data.x1, data.x2))
    if data.label == "person":
        stopCmd = 1
        print("label:{}".format(data.label))

rospy.init_node('aibot_aeb', disable_signals=True)

det_sub = rospy.Subscriber('detdata', DetInfo, detresCB, queue_size=10)
ctl_pub = rospy.Publisher('aibot_ctl_msg', MsgCtl, queue_size=10)

loop_rate = rospy.Rate(1)

while not rospy.is_shutdown():
    try:
        driveCtl(speedVal, angleVal, stopCmd);
        stopCmd = 0

        loop_rate.sleep()
    except KeyboardInterrupt:
        print("key int")
        break

print("end")
speedVal = 0.0
stopCmd = 1
driveCtl(speedVal, angleVal, stopCmd);
time.sleep(1.0)


import random

yes = "y"
no = "n"
random_num_min = 1
roll_counter = 1
keep_playing = True

play = input("Welcome to diceroll!\nWould you like to roll a dice? y/n: ")

while play != yes and play != no:
    play = input("Please enter y or n!: ")

if play == yes:

    while keep_playing:
        sides = input("How many sides does your dice have? ")

        while not(sides.isdigit()):
            sides = input("Please enter a number: ")

        roll_count = input("How many times would you like to roll the dice? ")

        while not(roll_count.isdigit()):
            roll_count = input("Please enter a number: ")

        while roll_counter <= int(roll_count):
            print(random.randint(random_num_min, int(sides)))
            roll_counter += 1

        roll_counter = 1

        play_again = input("Would you like to roll another dice? y/n: ")

        while play_again != yes and play_again != no:
            play_again = input("Please enter y or n!: ")

        if play_again == no:
            keep_playing = False
            print("See you another day!")

elif play == no:
    print("See you another day!")


#!/usr/bin/env python3
import itertools
from abc import ABC

import utils
from year_2017.day_18.part_a import RegisterRValueInstruction, \
    RvalueRValueInstruction, Mul
from year_2017.day_18.part_b import ProgramExtended, InstructionExtended

class Challenge(utils.BaseChallenge):
    def solve(self, _input, debug=False):
        """
        >>> Challenge().default_solve()
        3969
        """
        return ProgramExtendedTwice.from_program_text(_input)\
            .step_and_count_instructions().get(Mul, 0)

class ProgramExtendedTwice(ProgramExtended):
    def step_and_count_instructions(self, registers=None, count=None):
        if count is None:
            steps = itertools.count()
        else:
            steps = range(count)
        if registers is None:
            registers = self.registers_class()
        instruction_counts = {}
        for _ in steps:
            if self.has_finished(registers):
                break
            instruction_type = \
                type(self.instructions[registers.instruction_pointer])
            instruction_counts.setdefault(instruction_type, 0)
            instruction_counts[instruction_type] += 1
            finished = self.apply(registers)
            if finished:
                break

        return instruction_counts

class InstructionExtendedTwice(InstructionExtended, ABC):
    instruction_classes = dict(InstructionExtended.instruction_classes)

ProgramExtendedTwice.instruction_class = InstructionExtendedTwice

@InstructionExtendedTwice.register
class Sub(InstructionExtendedTwice, RegisterRValueInstruction):
    name = 'sub'

    regex = RegisterRValueInstruction.make_regex(name)

    def apply(self, registers):
        registers[self.register] -= registers.resolve(self.rvalue)

@InstructionExtendedTwice.register
class Jgz(RvalueRValueInstruction):
    name = 'jnz'

    regex = RvalueRValueInstruction.make_regex(name)

    def apply(self, registers):
        if registers.resolve(self.rvalue_a) != 0:
            next_instruction_pointer = \
                registers.instruction_pointer + registers.resolve(self.rvalue_b)
        else:
            next_instruction_pointer = None

        return next_instruction_pointer

Challenge.main()
challenge = Challenge()


import glob
import os

from numpy import genfromtxt
from sqlalchemy import Column, Integer, String
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()

class counters(Base):
    __tablename__ = 'counters'

    id = Column(Integer, primary_key=True)
    hero = Column(String(250), nullable=False)
    counter = Column(String(250), nullable=False)

# Create the database
engine = create_engine('sqlite:///database.db')
Base.metadata.create_all(engine)

# create the session
session = sessionmaker()
session.configure(bind=engine)
s = session()

def Load_Data(file_name):
    data = genfromtxt(file_name, delimiter=',', skip_header=1, dtype=None, encoding=None)
    return data.tolist()

try:
    current_path = os.getcwd()
    # Get list of files in folder
    files = glob.glob(os.path.join(current_path, "*.csv"))

    # Loop through all files in list
    for file_path in files:
        file_name = file_path.split('/')[-1]
        data = Load_Data(file_name)

        for i in data:
            record = counters(**{
                'hero': i[0],
                'counter': i[1]
            })
            s.add(record)
    s.commit()

except:

    s.rollback()  # Rollback the changes on error
finally:
    s.close()  # Close the connection


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

import os
import re
import codecs
import zipfile
import gzip
try:
  import lzma
except ImportError:
  try:
    from backports import lzma
  except ImportError:
    import warnings
    warnings.warn('Install backports.lzma for xz support')
from collections import Counter
import  tensorflow as tf
import numpy as np
#------------------------------------------------------
class Flag():

    # =============================================================
    def __init__(self, placeholder_shape=[None], config=None, isTrain=True, isDev=False):
        """"""

        self.placeholder = tf.placeholder(tf.int64, placeholder_shape, name=self.classname)

        self._config = config
        self.id2flag = {}
        self.index2flag = {}
        if isTrain:
            if self.fix:
                self.n_sent = self.set_flag_train_fixlabeldata()
            else:
                self.n_sent = self.set_flag_train()
                if self.portion > 1:
                    self.split()
        # else:
        #     self.n_sent = self.set_flag_dev()
        elif isDev:
            self.n_sent = self.set_flag_dev()
        else:
            self.n_sent = self.set_flag_test()
        return

    # =============================================================

    # =============================================================
    def set_placeholders(self, indices, feed_dict={}):
        """"""
        flag = self.indices2flag(indices)
        feed_dict[self.placeholder] = flag
        return feed_dict

    # =============================================================

    # =============================================================
    def indices2flag(self, indices):
        """"""

        return [self.index2flag[index] for index in indices]

    # =============================================================

    # =============================================================
    def set_flag_train_fixlabeldata(self):
        """"""
        num = 0

        if self.train_conllu_file[0].endswith('.zip'):
            open_func = zipfile.Zipfile
            kwargs = {}
        elif self.train_conllu_file[0].endswith('.gz'):
            open_func = gzip.open
            kwargs = {}
        elif self.train_conllu_file[0].endswith('.xz'):
            open_func = lzma.open
            kwargs = {'errors': 'ignore'}
        else:
            open_func = codecs.open
            kwargs = {'errors': 'ignore'}

        with open_func(self.train_conllu_file[0], 'rb') as f:
            reader = codecs.getreader('utf-8')(f, **kwargs)
            for line in reader:
                line = line.strip()
                if line and line.startswith('#'):
                    # if re.match('[0-9]+[-.][0-9]+', line):
                    if num < self.labeled_num:
                        self.id2flag[line] = 1
                        self.index2flag[num] = 1
                    else:
                        self.id2flag[line] = 0
                        self.index2flag[num] = 0
                    num += 1
        self.dump()
        return num
    # =============================================================

    # =============================================================
    def set_flag_train(self):
        """"""
        num = 0

        if self.train_conllu_file[0].endswith('.zip'):
            open_func = zipfile.Zipfile
            kwargs = {}
        elif self.train_conllu_file[0].endswith('.gz'):
            open_func = gzip.open
            kwargs = {}
        elif self.train_conllu_file[0].endswith('.xz'):
            open_func = lzma.open
            kwargs = {'errors': 'ignore'}
        else:
            open_func = codecs.open
            kwargs = {'errors': 'ignore'}

        with open_func(self.train_conllu_file[0], 'rb') as f:
            reader = codecs.getreader('utf-8')(f, **kwargs)
            for line in reader:
                line = line.strip()
                if line and line.startswith('#'):
                    # if re.match('[0-9]+[-.][0-9]+', line):
                    if num%self.portion == 0:
                        self.id2flag[line] = 1
                        self.index2flag[num] = 1
                    else:
                        self.id2flag[line] = 0
                        self.index2flag[num] = 0
                    num += 1
        self.dump()
        return num
    # =============================================================

    # =============================================================
    def set_flag_dev(self):
        """"""
        num = 0

        if self.dev_conllu_file[0].endswith('.zip'):
            open_func = zipfile.Zipfile
            kwargs = {}
        elif self.dev_conllu_file[0].endswith('.gz'):
            open_func = gzip.open
            kwargs = {}
        elif self.dev_conllu_file[0].endswith('.xz'):
            open_func = lzma.open
            kwargs = {'errors': 'ignore'}
        else:
            open_func = codecs.open
            kwargs = {'errors': 'ignore'}

        with open_func(self.dev_conllu_file[0], 'rb') as f:
            reader = codecs.getreader('utf-8')(f, **kwargs)
            for line in reader:
                line = line.strip()
                if line and line.startswith('#'):
                    # if re.match('[0-9]+[-.][0-9]+', line):
                    self.id2flag[line] = 0
                    self.index2flag[num] = 0
                    num += 1
        return num
    # =============================================================

    #------------------test---------------------------------------
    # =============================================================
    def set_flag_test(self):
        """"""
        num = 0

        if self.dev_conllu_file[0].endswith('.zip'):
            open_func = zipfile.Zipfile
            kwargs = {}
        elif self.dev_conllu_file[0].endswith('.gz'):
            open_func = gzip.open
            kwargs = {}
        elif self.dev_conllu_file[0].endswith('.xz'):
            open_func = lzma.open
            kwargs = {'errors': 'ignore'}
        else:
            open_func = codecs.open
            kwargs = {'errors': 'ignore'}

        with open_func(self.test_conllu_file[0], 'rb') as f:
            reader = codecs.getreader('utf-8')(f, **kwargs)
            for line in reader:
                line = line.strip()
                if line and line.startswith('#'):
                    # if re.match('[0-9]+[-.][0-9]+', line):
                    self.id2flag[line] = 0
                    self.index2flag[num] = 0
                    num += 1
        return num
    # =============================================================
    #--------------------test---------------------------------------

    # =============================================================
    def dump(self):
        """"""
        with codecs.open(self.flag_savename, 'w', encoding='utf-8', errors='ignore') as f:
            for id in self.id2flag:
                f.write(u'{}\t{}\n'.format(id, self.id2flag[id]))
        return
    # =============================================================

    # =============================================================
    def split(self):

        if self.train_conllu_file[0].endswith('.zip'):
            open_func = zipfile.Zipfile
            kwargs = {}
        elif self.train_conllu_file[0].endswith('.gz'):
            open_func = gzip.open
            kwargs = {}
        elif self.train_conllu_file[0].endswith('.xz'):
            open_func = lzma.open
            kwargs = {'errors': 'ignore'}
        else:
            open_func = codecs.open
            kwargs = {'errors': 'ignore'}

        su_ids = [ids for ids, flag in self.id2flag.items() if flag == 1]

        su_file = self.train_conllu_file[0] + '.portion%d.su'%(self.portion)
        w = codecs.open(su_file, 'w', encoding='utf-8', errors='ignore')

        buff = []

        with open_func(self.train_conllu_file[0], 'rb') as f:
            reader = codecs.getreader('utf-8')(f, **kwargs)
            for line in reader:
                buff.append(line)
                if line.startswith('#'):
                    current = line
                if line == '\n':
                    if current.strip() in su_ids:
                        for contents in buff:
                            w.write(contents)
                    buff = []
        w.close()

        return
    # =============================================================

    @property
    def save_dir(self):
        return self._config.getstr(self, 'save_dir')
    @property
    def train_conllu_file(self):
        return self._config.getfiles(self, 'train_conllus')

    @property
    def dev_conllu_file(self):
        return self._config.getfiles(self, 'dev_conllus')

    @property
    def test_conllu_file(self):
        return self._config.getfiles(self, 'test_conllus')

    @property
    def flag_savename(self):
        return os.path.join(self.save_dir, 'flag' + '.lst')

    @property
    def classname(self):
        return self.__class__.__name__

    @property
    def portion(self):
        return self._config.getint(self, 'portion')

    @property
    def fix(self):
        return self._config.getboolean(self, 'fix_label_data')

    @property
    def labeled_num(self):
        return self._config.getint(self, 'labeled_num')


# Ref: ATmega16U4/ATmega32U4 8-bit Microcontroller with 16/32K bytes of ISP Flash and USB Controller datasheet
# Accession: G00058

import math
import logging
from amaranth import *

from ....interface.spi_controller import SPIControllerSubtarget, SPIControllerInterface
from .... import *
from .. import *

class ProgramAVRSPISubtarget(Elaboratable):
    def __init__(self, controller, reset_t, dut_reset):
        self.controller = controller
        self.reset_t = reset_t
        self.dut_reset = dut_reset

    def elaborate(self, platform):
        m = Module()

        m.submodules.controller = self.controller

        m.d.comb += [
            self.controller.bus.oe.eq(self.dut_reset),
            self.reset_t.oe.eq(1),
            self.reset_t.o.eq(~self.dut_reset)
        ]

        return m

class ProgramAVRSPIInterface(ProgramAVRInterface):
    def __init__(self, interface, logger, addr_dut_reset):
        self.lower   = interface
        self._logger = logger
        self._level  = logging.DEBUG if self._logger.name == __name__ else logging.TRACE
        self._addr_dut_reset = addr_dut_reset
        self._extended_addr  = None
        self.erase_time      = None

    def _log(self, message, *args):
        self._logger.log(self._level, "AVR SPI: " + message, *args)

    async def _command(self, byte1, byte2, byte3, byte4):
        command = [byte1, byte2, byte3, byte4]
        self._log("command %s", "{:08b} {:08b} {:08b} {:08b}".format(*command))
        result = await self.lower.transfer(command)
        self._log("result  %s", "{:08b} {:08b} {:08b} {:08b}".format(*result))
        return result

    async def programming_enable(self):
        self._log("programming enable")

        await self.lower.lower.device.write_register(self._addr_dut_reset, 1)
        await self.lower.delay_ms(20)

        _, _, echo, _ = await self._command(0b1010_1100, 0b0101_0011, 0, 0)
        if echo == 0b0101_0011:
            self._log("synchronization ok")
        else:
            raise ProgramAVRError("device not present or not synchronized")

    async def programming_disable(self):
        self._log("programming disable")
        await self.lower.synchronize()
        await self.lower.lower.device.write_register(self._addr_dut_reset, 0)
        await self.lower.delay_ms(20)

    async def _is_busy(self):
        if self.erase_time is not None:
            self._log("wait for completion")
            await self.lower.delay_ms(self.erase_time)
            return False
        self._log("poll ready/busy flag")
        _, _, _, busy = await self._command(0b1111_0000, 0b0000_0000, 0, 0)
        return bool(busy & 1)

    async def read_signature(self):
        self._log("read signature")
        signature = []
        for address in range(3):
            _, _, _, sig_byte = await self._command(0b0011_0000, 0b0000_0000, address & 0b11, 0)
            signature.append(sig_byte)
        return tuple(signature)

    async def read_fuse(self, address):
        self._log("read fuse address %#04x", address)
        a0, a1 = {
            0: (0b0000, 0b0000),
            1: (0b1000, 0b1000),
            2: (0b0000, 0b1000),
        }[address]
        _, _, _, data = await self._command(
            0b0101_0000 | a0,
            0b0000_0000 | a1,
            0,  0)
        return data

    async def write_fuse(self, address, data):
        self._log("write fuse address %#04x data %02x", address, data)
        a = {
            0: 0b0000,
            1: 0b1000,
            2: 0b0100,
        }[address]
        await self._command(
            0b1010_1100,
            0b1010_0000 | a,
            0,
            data)
        while await self._is_busy(): pass

    async def read_lock_bits(self):
        self._log("read lock bits")
        _, _, _, data = await self._command(0b0101_1000, 0b0000_0000, 0,  0)
        return data

    async def write_lock_bits(self, data):
        self._log("write lock bits data %02x", data)
        await self._command(
            0b1010_1100,
            0b1110_0000,
            0,
            0b1100_0000 | data)
        while await self._is_busy(): pass

    async def read_calibration(self, address):
        self._log("read calibration address %#04x", address)
        _, _, _, data = await self._command(0b0011_1000, 0b0000_0000, address, 0)
        return data

    async def load_extended_address_byte(self, address):
        extended_addr = (address >> 17) & 0xff
        if self._extended_addr != extended_addr:
            self._log("load extended address %#02x", extended_addr)
            await self._command(0b0100_1101, 0, extended_addr, 0)
            self._extended_addr = extended_addr

    async def read_program_memory(self, address):
        await self.load_extended_address_byte(address)
        self._log("read program memory address %#06x", address)
        _, _, _, data = await self._command(
            0b0010_0000 | (address & 1) << 3,
            (address >> 9) & 0xff,
            (address >> 1) & 0xff,
            0)
        return data

    async def load_program_memory_page(self, address, data):
        self._log("load program memory address %#06x data %02x", address, data)
        await self._command(
            0b0100_0000 | (address & 1) << 3,
            (address >> 9) & 0xff,
            (address >> 1) & 0xff,
            data)

    async def write_program_memory_page(self, address):
        await self.load_extended_address_byte(address)
        self._log("write program memory page at %#06x", address)
        await self._command(
            0b0100_1100,
            (address >> 9) & 0xff,
            (address >> 1) & 0xff,
            0)
        while await self._is_busy(): pass

    async def read_eeprom(self, address):
        self._log("read EEPROM address %#06x", address)
        _, _, _, data = await self._command(
            0b1010_0000,
            (address >> 8) & 0xff,
            (address >> 0) & 0xff,
            0)
        return data

    async def load_eeprom_page(self, address, data):
        self._log("load EEPROM address %#06x data %02x", address, data)
        await self._command(
            0b1100_0001,
            (address >> 8) & 0xff,
            (address >> 0) & 0xff,
            data)

    async def write_eeprom_page(self, address):
        self._log("write EEPROM page at %#06x", address)
        await self._command(
            0b1100_0010,
            (address >> 8) & 0xff,
            (address >> 0) & 0xff,
            0)
        while await self._is_busy(): pass

    async def chip_erase(self):
        self._log("chip erase")
        await self._command(0b1010_1100, 0b1000_0000, 0, 0)
        while await self._is_busy(): pass

class ProgramAVRSPIApplet(ProgramAVRApplet):
    logger = logging.getLogger(__name__)
    help = f"{ProgramAVRApplet.help} via SPI"
    description = """
    Identify, program, and verify Microchip AVR microcontrollers using low-voltage serial (SPI)
    programming.

    While programming is disabled, the programming interface is tristated, so the applet can be
    used for in-circuit programming even if the device uses SPI itself.

    The standard AVR ICSP connector layout is as follows:

    ::
        CIPO @ * VCC
         SCK * * COPI
        RST# * * GND
    """

    __pins = ("reset", "sck", "cipo", "copi")

    @classmethod
    def add_build_arguments(cls, parser, access):
        super().add_build_arguments(parser, access)

        access.add_pin_argument(parser, "reset", default=True)
        access.add_pin_argument(parser, "sck",   default=True)
        access.add_pin_argument(parser, "cipo",  default=True)
        access.add_pin_argument(parser, "copi",  default=True)

        parser.add_argument(
            "-f", "--frequency", metavar="FREQ", type=int, default=100,
            help="set SPI frequency to FREQ kHz (default: %(default)s)")

    def build(self, target, args):
        self.mux_interface = iface = target.multiplexer.claim_interface(self, args)
        controller = SPIControllerSubtarget(
            pads=iface.get_pads(args, pins=self.__pins),
            out_fifo=iface.get_out_fifo(),
            in_fifo=iface.get_in_fifo(auto_flush=False),
            period_cyc=math.ceil(target.sys_clk_freq / (args.frequency * 1000)),
            delay_cyc=math.ceil(target.sys_clk_freq / 1e6),
            sck_idle=0,
            sck_edge="rising",
            cs_active=0,
        )

        dut_reset, self.__addr_dut_reset = target.registers.add_rw(1)
        subtarget = ProgramAVRSPISubtarget(controller, iface.pads.reset_t, dut_reset)

        iface.add_subtarget(subtarget)

    async def run_lower(self, cls, device, args):
        iface = await device.demultiplexer.claim_interface(self, self.mux_interface, args)
        return SPIControllerInterface(iface, self.logger)

    async def run(self, device, args):
        spi_iface = await self.run_lower(ProgramAVRSPIApplet, device, args)
        avr_iface = ProgramAVRSPIInterface(spi_iface, self.logger, self.__addr_dut_reset)
        return avr_iface

    @classmethod
    def tests(cls):
        from . import test
        return test.ProgramAVRSPIAppletTestCase


# -*- coding: utf-8 -*-
import numpy
from numpy import loadtxt
import scipy
import pylab
import time
import copy
import os
import re
import sys
import matplotlib.pyplot as plt
from matplotlib import rc
from scipy import stats
from itertools import combinations
from ast import literal_eval
from matplotlib.colors import ListedColormap, NoNorm
from pylab import rcParams

# the axes attributes need to be set before the call to subplot
plt.rc('text', usetex=True)
plt.rc('font', family='serif', weight='bold', size=18)
plt.rc('xtick.major', size=5, pad=7)
plt.rc('ytick.major', size=5, pad=7)
rcParams['text.latex.preamble'] = [r'\usepackage{sfmath} \boldmath']
plt.rc('xtick', labelsize=18)
plt.rc('ytick', labelsize=18)

#filename = "_feats_shuffled_across_all_nouns.dat"
#filename = "_feats_shuffled_within_clusters.dat"
#filename = "_feats_shuffled_within_clusters_offblock_mean.dat"
filename = "_feats_shuffled_within_clusters_offblock_mean_inblock_mean.dat"
#filename = ".dat"

D = loadtxt("D%s"%filename)
N_words = 60
power = 1
#triplets

delta1 = []
delta2 = []

i_else = numpy.arange(5,11+1)

D=numpy.delete(D, numpy.s_[5:11+1], 0)
D=numpy.delete(D, numpy.s_[5:11+1], 1)

N_words = numpy.shape(D)[0]
for item in list(combinations(range(N_words),3)):
	#print(item)
	d1 = D[item[0],item[1]]#numpy.fabs()
	d2 = D[item[1],item[2]]#numpy.fabs()
	d3 = D[item[2],item[0]]#numpy.fabs()

	#print(d1)
	#print(d2)
	#print(d3)
	dmin = (numpy.sort([d1,d2,d3]))[0]
	dint = (numpy.sort([d1,d2,d3]))[1]
	dmax = (numpy.sort([d1,d2,d3]))[2]
	#print("dmin=",dmin)
	#print("dint=",dint)
	#print("dmax=",dmax)
	#print(10*"--")

	if (dmin > 0 and dint > 0 and dmax > 0):
		delta1 = numpy.append(delta1, dmin/dmax)
		delta2 = numpy.append(delta2, dint/dmax)

#print("delta1=",delta1)
#print("delta2=",delta2)

# ------------------------------------- compute UM content
lamda = []

for i in range(numpy.size(delta1)):
	if( (numpy.log(delta1[i]) + numpy.log(delta2[i])) != 0 ):
		x = (numpy.log(delta1[i]) - numpy.log(delta2[i]))/(numpy.log(delta1[i]) + numpy.log(delta2[i]))
		lamda = numpy.append(lamda,x)
print(lamda)
print("umcontent=", numpy.mean(lamda))

# ------------------------------------- PLOT

fig = plt.figure(figsize=(3.5, 5))
plt.plot(numpy.linspace(0.0,1.1,11),numpy.linspace(0.0,1.1,11), alpha=1, color='white', lw=3, zorder=2)
plt.plot(numpy.linspace(0.0,1.1,11),1-numpy.linspace(0.0,1.1,11), alpha=1, color='white', lw=2, zorder=2)
#plt.axvline(1.0, alpha=1, color='black', zorder=3)
plt.scatter(delta2, delta1, marker='o',s=0.1, c='black', zorder=3)
#####
#plt.hexbin(numpy.append(numpy.linspace(0.0,0.8),delta2), numpy.append(numpy.linspace(0.0,0.8), delta1), gridsize=50, bins='log', cmap='jet', vmin=0, vmax=2)
#plt.colorbar()
#####
plt.xlabel('$\delta_2 (d_{med}/d_{max})$')
plt.ylabel('$\delta_1 (d_{min}/d_{max})$')
plt.xlim(0.5,1.1)
plt.ylim(0.,1.1)

print(list(combinations(range(8),2)))

#plt.savefig('um_content%s_dpow%.1f.pdf'%(filename,power), bbox_inches='tight')
#plt.savefig('um_content%s_dpow%.1f.png'%(filename,power), bbox_inches='tight')


import cv2
from sklearn.model_selection import train_test_split

#get path
img_file_path =   '/Users/yash/playground/AI_clever_programer/2.car_human_detection/3.jpg'
classifier_file_path = '/Users/yash/playground/AI_clever_programer/2.car_human_detection/car_detection.xml'

# create classifier  -- use CascadeClassifier
trained_car_data = cv2.CascadeClassifier(classifier_file_path)

# create openc image1
img = cv2.imread(img_file_path)

# Must convert to grayscale
grayscaled_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect car -- it retruns cordinates of rectengle suranding the face
car_coordinates = trained_car_data.detectMultiScale(grayscaled_img)
print(car_coordinates)

for (x, y, w, h) in car_coordinates:
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)#(x, y) --uper left ,(x+w, y+h) : lowerright  -- #bgr color #green # last 2 is theekness of the rectangle
    # cv2.rectangle(img, (x, y), (x+w, y+h), (randrange(256), randrange(256), randrange(256)), 2)#(x

# Display the image with the faces spotted
cv2.imshow('yash Car Detector', img)
# Dont autoclose (Wait here in the code and listen for a key press)
cv2.waitKey ()

print ("Code Completed")


#Celcius to farenheit converter

#Obtain temp in celcius and save to variable
celcius = float(input('Please enter a temperature in Celsius. '))

#Convert celcius celcius variable to fareheit
temp = (celcius * 9 / 5) + 32

#Return converted temperature
print(temp)


# ===== LIBRARIES ======
# Basic libraries to handle data
import numpy as np
import pandas as pd

# To load data to SQL database
from sqlalchemy import create_engine

# To access configuration variables
import sys; sys.path.append('.')
from pathlib import Path
from config.core import DATASET_DIR, config

# ===== FUNCTIONS ======
def load_data(messages_file_name:str, categories_file_name:str):
    """Loads and merge the messages & categories datasets

    Arguments:
        messages_file_name: name file of message data
        categories_file_name: name file of categories data
    Returns:
        data: pandas dataframe
    """
    # Loads datasets
    messages = pd.read_csv(Path(f'{DATASET_DIR/messages_file_name}'))
    categories = pd.read_csv(Path(f'{DATASET_DIR/categories_file_name}'))

    # Merges the two datasets
    data = messages.merge(categories, on='id')

    return data

def clean_data(data):
    """Loads data, splits categories into multiple columns,
    ensures targets are binary, and then removes duplicates

    Arguments:
        data: pandas dataframe
    Returns:
        data: pandas dataframe
    """
    # ===== Transforming "Categories" ======
    # Splitting categories (one col) into n columns
    cats = data.categories.str.split(';', expand=True)
    cols = cats.iloc[0,:].apply(lambda x:x[:-2])
    cats.columns = cols
    for column in cats:
        cats[column] = pd.to_numeric(cats[column].str[-1])

    # Concat categories with the rest of the data
    data = pd.concat([data.drop(['categories'], axis=1), cats], axis=1)

    # The "related" category includes 3 unique values. To binarize
    # this class, I'll assume "2" as "1" to not lose those observations
    data['related'] = data['related'].replace({2:1})

    # ===== Removing duplicates ======
    # Removing unnecesary feature
    data = data.drop_duplicates()

    return data

def save_data(data):
    """Saves cleaned data into an SQLite database following
    configuration parameters for path file

    Arguments:
        data: pandas dataframe
    Returns:
        None
    """
    save_file_name = config.app_config.database_file_name
    table_name = config.app_config.table_name

    engine = create_engine(f'sqlite:///{DATASET_DIR}/{save_file_name}')
    data.to_sql(f'{table_name}', engine, index=False, if_exists='replace')

def etl_process():
    """Contains the 3 steps of the ETL pipeline

    Arguments:
        None
    Returns:
        None
    """
    data = load_data(messages_file_name = config.app_config.messages_file_name,
                   categories_file_name = config.app_config.categories_file_name)
    print('Data extraction complete')

    data = clean_data(data)
    print('Data transformed correctly')

    save_data(data)
    print('Loaded clean data into SQLite database')

if __name__ == '__main__':
    # run data pipeline
    etl_process()


import tensorflow as tf
import  numpy as np

#定义训练轮数
training_steps = 30000

#定义输入的数据和对应的标签并在for循环内进行填充
data = []
label = []
for i in range(200):
    x1 = np.random.uniform(-1, 1)
    x2 = np.random.uniform(0, 2)
    #这里对产生的x1和x2进行判断，如果产生的点落在半径为1的圆内，则label值为0
    #否则label值为1
    if x1**2 + x2**2 <= 1:
        data.append([np.random.normal(x1, 0.1),np.random.normal(x2,0.1)])
        label.append(0)
    else:
        data.append([np.random.normal(x1, 0.1), np.random.normal(x2, 0.1)])
        label.append(1)

#numpy的hstack()函数用于在水平方向将元素堆起来
#函数原型numpy.hstack(tup)，参数tup可以是元组、列表、或者numpy数组，
#返回结果为numpy的数组。reshape()函数的参数-1表示行列进行翻转。
#这样处理的结果为data变成了200x2大小的数组，而label是100x1
data = np.hstack(data).reshape(-1,2)
label = np.hstack(label).reshape(-1, 1)

#定义完成前向传播的隐层
def hidden_layer(input_tensor,weight1,bias1,weight2,bias2,weight3,bias3):
    layer1=tf.nn.relu(tf.matmul(input_tensor,weight1)+bias1)
    layer2 = tf.nn.relu(tf.matmul(layer1, weight2) + bias2)
    return tf.matmul(layer2,weight3)+bias3

x = tf.placeholder(tf.float32, shape=(None, 2),name="x-input")
y_ = tf.placeholder(tf.float32, shape=(None, 1),name="y-output")

#定义权重参数和偏置参数
weight1 = tf.Variable(tf.truncated_normal([2,10],stddev=0.1))
bias1 = tf.Variable(tf.constant(0.1, shape=[10]))
weight2 = tf.Variable(tf.truncated_normal([10,10],stddev=0.1))
bias2 = tf.Variable(tf.constant(0.1, shape=[10]))
weight3 = tf.Variable(tf.truncated_normal([10,1],stddev=0.1))
bias3 = tf.Variable(tf.constant(0.1, shape=[1]))

#用len()函数计算data数组的长度
sample_size = len(data)

#得到隐层前向传播结果
y= hidden_layer(x,weight1,bias1,weight2,bias2,weight3,bias3)

#自定义的损失函数。pow()函数用于计算幂函数，原型为pow(x,y,name=None)
#返回结果为x的y次幂，这里返回结果为(y_-y)^2用于衡量计算值与实际值的差距
error_loss = tf.reduce_sum(tf.pow(y_ - y, 2)) / sample_size
tf.add_to_collection("losses", error_loss)      #加入集合的操作

#在权重参数上实现L2正则化
regularizer = tf.contrib.layers.l2_regularizer(0.01)
regularization = regularizer(weight1)+regularizer(weight2)+regularizer(weight3)
tf.add_to_collection("losses",regularization)     #加入集合的操作

#get_collection()函数获取指定集合中的所有个体，这里是获取所有损失值
#并在add_n()函数中进行加和运算
loss = tf.add_n(tf.get_collection("losses"))

#定义一个优化器，学习率为固定为0.01，注意在实际应用中这个学习率数值应该大于0.01
train_op = tf.train.AdamOptimizer(0.01).minimize(loss)

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    #在for循环内进行30000训练
    for i in range(training_steps):
        sess.run(train_op, feed_dict={x: data, y_: label})

        #训练30000轮，但每隔2000轮就输出一次loss的值
        if i % 2000 == 0:
            loss_value = sess.run(loss, feed_dict={x: data, y_: label})
            print("After %d steps, loss_value is: %f" % (i,loss_value))


class Solution:
    def map1d_to_2d(self, idx, n):
        return (idx // n, idx % n)

    def searchMatrix(self, matrix: list[list[int]], target: int) -> bool:
        m = len(matrix)
        n = len(matrix[0])
        l = 0
        r = m * n
        while l < r:
            midpoint = (r - l) // 2 + l
            row, col = self.map1d_to_2d(midpoint, n)
            if matrix[row][col] == target:
                return True
            elif matrix[row][col] > target:
                r = midpoint
            else:
                l = midpoint + 1
        return False

print(Solution().searchMatrix([[1]], 2))


from django.urls import path
from . import views

app_name = 'blog'

urlpatterns = [
    path('', views.Home, name='home'),
    path('post/', views.BlogFormView, name='post'),
    path('blog-details/<slug:slug>', views.BlogDetails, name='blog_details'),
    path('blog-category/<int:pk>', views.CategoryBlogs, name='blog-category'),
]


# --------------------------------------------------------------------------
# Name:        Flatten a Multilevel Doubly Linked List
# Author(s):   Phu Tran
# --------------------------------------------------------------------------
"""
    You are given a doubly linked list which in addition to the next and
    previous pointers, it could have a child pointer, which may or may not
    point to a separate doubly linked list. These child lists may have one
    or more children of their own, and so on, to produce a multilevel data
    structure, as shown in the example below.

    Flatten the list so that all the nodes appear in a single-level, doubly
    linked list. You are given the head of the first level of the list.

    Example 1:
      Input: head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
      Output: [1,2,3,7,8,11,12,9,10,4,5,6]
    Explanation:
      **SEE IMAGE 7_10_example1_pic.PNG

    Example 2:
      Input: head = [1,2,null,3]
      Output: [1,3,2]
      Explanation:
        The input multilevel linked list is as follows:

          1---2---NULL
          |
          3---NULL

    Example 3:
      Input: head = []
      Output: []

    How multilevel linked list is represented in test case:
      We use the multilevel linked list from Example 1 above:
         1---2---3---4---5---6--NULL
                 |
                 7---8---9---10--NULL
                     |
                     11--12--NULL
    The serialization of each level is as follows:
      [1,2,3,4,5,6,null]
      [7,8,9,10,null]
      [11,12,null]

    To serialize all levels together we will add nulls in each level to signify no node connects to the upper node of the previous level. The serialization becomes:
      [1,2,3,4,5,6,null]
      [null,null,7,8,9,10,null]
      [null,11,12,null]

    Merging the serialization of each level and removing trailing nulls we obtain:
      [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]

    Constraints:
      1. Number of Nodes will not exceed 1000.
      2. 1 <= Node.val <= 10^5
"""

"""
# Definition for a Node.
class Node(object):
    def __init__(self, val, prev, next, child):
        self.val = val
        self.prev = prev
        self.next = next
        self.child = child
"""

class Solution(object):
    def flatten(self, head):
        """
        :type head: Node
        :rtype: Node
        """
        def helper(head):
            """
            use recursion when meet nodes with child and explore it first
            """
            curPtr = None
            flag = True

            while head and flag:
                if head.child is not None:
                    # save the node that the last child in the list that will connect to
                    curPtr = head.next

                    helper(head.child)

                    # after finish with the child, update the pointers of the parent
                    head.next = head.child
                    head.child.prev = head
                    head.child = None

                if head.next:
                    head = head.next
                else:
                    flag = False

            if curPtr is not None:
                curPtr.prev = head

            # curPtr can be None or a node element
            head.next = curPtr

        if head is None or head.next is None and head.child is None:
            return head

        temp = head
        helper(temp)

        return head

'''other faster methods (from other submissions)
##################################################
def flatten(self, head):
        """
        :type head: Node
        :rtype: Node
        """
        stack=[]
        if head is None:return head
        dummy=Node(0,None,head,None)
        dummy.next=head
        prev=dummy
        curr=head
        stack.append(curr)

        while(len(stack)!=0):

            curr=stack.pop()
            curr.prev=prev
            prev.next=curr
            prev=prev.next

            if curr.next:
                stack.append(curr.next)
            if curr.child:
                stack.append(curr.child)

                curr.child=None

        dummy.next.prev=None
        return dummy.next

        # if not head:return None
        # dummy=Node(0,None,head,None)
        # prev=dummy
        # curr=head
#         self.dfs(prev,curr)
#         dummy.next.prev=None
#         return dummy.next
#     def dfs(self,prev,curr):
#         if curr is None:
#             return prev
#         curr.prev=prev
#         prev.next=curr
#         tempright=curr.next
#         tail=self.dfs(curr,curr.child)
#         curr.child=None
#         return self.dfs(tail,tempright)

        #newNode.prev=node.prev
        # print(newNode)
        # return newNode
##################################################
 def flatten(self, head):
        if not head:
            return
        dummy=Node(0,None,head,None)
        prev=dummy
        stack=[head]
        while len(stack)!=0:
            root=stack.pop()
            root.prev=prev
            prev.next=root
            if root.next:
                stack.append(root.next)
                root.next=None
            if root.child:
                stack.append(root.child)
                root.child=None
            prev=root
        dummy.next.prev=None
        return dummy.next
##################################################
def flatten(self, head):
        """
        :type head: Node
        :rtype: Node
        """
        if not head:
            return head
        stack = []
        dummy = Node(0,None,None,None)
        cur = dummy
        stack.append(head)
        while len(stack)>0:
            new = stack.pop()
            if new.next:
                stack.append(new.next)
            if new.child:
                stack.append(new.child)
            new.child = None
            cur.next = new
            new.prev = cur
            cur = cur.next

        # cur.next = None
        new_head = dummy.next
        new_head.prev = None
        return new_head
'''


__author__ = 'mpetyx'

from collections import OrderedDict

from Parser import Parser
from pyapi.entities import APIRoot, APIResource, APIMethod
from pyapi.libraries.pyhydra import Hypermedia

class HydraParser(Parser):
    def parse(self, location):

        hm = Hypermedia()
        hm.open(location)
        # if hm.open(location):
        # print hm.doc.apidoc
        # for klassi in hm.doc.classes:
        # print "######################"
        #         print hm.doc.classes[klassi].dump()
        # for operation in  hm.doc.classes[klassi].operations:
        #     print

        # first_line, c = c.split('\n', 1)
        # raml_version = _validate_raml_header(first_line)

        # context = ParseContext(yaml.load(c), location)

        root = APIRoot(raml_version=str(0.8))
        # root.title = context.get_string_property('title', True)
        #
        # root.baseUri = context.get_string_property('baseUri')
        # root.version = context.get('version')
        # root.mediaType = context.get_string_property('mediaType')

        # root.documentation = context.get_property_with_schema('documentation', APIRoot.documentation)
        # root.traits = parse_traits(context, APIRoot.traits.field_name, root.mediaType)
        # root.resourceTypes = parse_resource_type(context)

        resources = OrderedDict()
        for klass in hm.doc.classes:
            resource = APIResource()
            resource.displayName = "yolo"
            resource.description = "example of the api"
            # Parse methods

            methods = OrderedDict()

            for operation in hm.doc.classes[klass].operations:
                method = APIMethod(notNull=True)
                method.description = str(operation.subj)
                methods[str(operation.method)] = method

            if len(methods):
                resource.methods = methods

            resources[str(klass)] = resource  #parse_resource(context, property_name, root, root.mediaType)

        if resources > 0:
            root.resources = resources

        #
        # api = APIRoot()
        # g = Graph().parse(location, format='json-ld')

        return root


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    left, right = 0, 0
    def btreeGameWinningMove(self, root: TreeNode, n: int, x: int) -> bool:

        def count(node):
            if not node: return 0

            l, r = count(node.left), count(node.right)
            if node.val == x:
                self.left, self.right = l, r

            return l + r + 1

        count(root)
        left, right = self.left, self.right

        first = left > (n - left)
        second = right > (n - right)
        third = (n - left - right - 1) > (1 + left + right)

        return first or second or third


import uuid as uuid_pkg
from typing import TYPE_CHECKING

from sqlmodel import Field, Relationship

from app.internals.model import BaseModelDb
from app.internals.user.model import UserBudgetLink, UserDb

if TYPE_CHECKING:
    from app.internals.user.model import UserDb

class BudgetDb(BaseModelDb, table=True):
    __tablename__ = "budget"

    id: uuid_pkg.UUID = Field(
        default_factory=uuid_pkg.uuid4,
        primary_key=True,
        index=True,
        nullable=False,
    )
    name: str
    description: str
    users: list["UserDb"] = Relationship(
        back_populates="budgets", link_model=UserBudgetLink
    )


from datetime import datetime

from telegram.ext import Updater, CommandHandler
from CameraModules.PiCam import PiCam
from Config import config

class TelegramBot:
    def startTelegramBot(self):
        updater = Updater(token=config["telegramApiKey"])
        dispatcher = updater.dispatcher

        try:
            start_CommandHandler = CommandHandler('start', self.__startHandler)
            capture_CommandHandler = CommandHandler('capture', self.__captureHandler)
            stop_CommandHandler = CommandHandler('stop', self.__stopHandler)

            dispatcher.add_handler(start_CommandHandler)
            dispatcher.add_handler(capture_CommandHandler)
            dispatcher.add_handler(stop_CommandHandler)

            updater.start_polling(0.5)
            print("Telegram bot started!")

        except Exception as e:
            print(e)

    def __startHandler(self, bot, update):
        if (update.message.chat_id == int(config['userId'])):
            try:
                time = datetime.now()
                print("Bot online: "+ str(time))
                bot.send_message(chat_id=update.message.chat_id, text="Bot online: "+ str(time))
            except Exception as e:
                print(e)
        else:
            bot.send_message(chat_id=update.message.chat_id, text="Unauthorised")
            print("unauthorised message from " + update.message.chat_id)

    def __captureHandler(self, bot, update):
        if update.message.chat_id == int(config['userId']):
            try:
                bot.send_message(chat_id=update.message.chat_id, text="Capturing...")
                camera = PiCam()
                camera.Snap()
                #subprocess.call("raspistill -o image.jpg")
                bot.sendPhoto(chat_id=update.message.chat_id, photo=open("/home/pi/image.jpg", 'rb'))

            except Exception as e:
                bot.send_message(chat_id=update.message.chat_id, text="Failed to capture")
                print(e)

    def __stopHandler(self, bot, update):
        if update.message.chat_id == int(config['userId']):
            bot.send_message(chat_id=update.message.chat_id, text="Exit terminal")


otroci = {
    "Adam": ["Matjaž", "Cilka", "Daniel"],
    "Aleksander": [],
    "Alenka": [],
    "Barbara": [],
    "Cilka": [],
    "Daniel": ["Elizabeta", "Hans"],
    "Erik": [],
    "Elizabeta": ["Ludvik", "Jurij", "Barbara"],
    "Franc": [],
    "Herman": ["Margareta"],
    "Hans": ["Herman", "Erik"],
    "Jožef": ["Alenka", "Aleksander", "Petra"],
    "Jurij": ["Franc", "Jožef"],
    "Ludvik": [],
    "Margareta": [],
    "Matjaž": ["Viljem"],
    "Petra": [],
    "Tadeja": [],
    "Viljem": ["Tadeja"],
}

def premozenje(oseba, denar):
    vsota = denar.get(oseba)
    for otrok in otroci[oseba]:
        vsota += premozenje(otrok, denar)
    return vsota

def najbogatejsi(oseba, denar):
    ime = oseba
    vred = denar.get(oseba)
    for otrok in otroci[oseba]:
        podatek = najbogatejsi(otrok, denar)
        if podatek[1] > vred:
            vred = podatek[1]
            ime = otrok

    return (ime, vred)


# project 21

from functools import lru_cache

@lru_cache(maxsize=1000)
def amicable_numbers(x):
     sum1 = sum([i for i in range(1,x)if x % i == 0 ])
     sum2 = sum([j for j in range(1,sum1)if sum1 % j == 0])
     return x  == sum2 and sum1 != sum2

l =0
for i in range(1,10000):
     if(amicable_numbers(i)):
          l+=(i)

print(l)#31626


import numpy as np
import os
from time import time
import colossus.cosmology.cosmology as cosmo
from colossus.halo import concentration, mass_so
from colossus.lss.mass_function import massFunction
from scipy.interpolate import interp1d
from scipy.integrate import cumtrapz
from scipy.stats import binned_statistic
import pandas as pd
from functools import lru_cache
from HaloUtils import Volume

cosmol=cosmo.setCosmology('planck15')
cosmo.setCurrent(cosmol)

class SizeModel(Volume):

    def __init__(self, rhalo, Vol=None):
        if Vol is not None:
            self.Vol = Vol
        else:
            super().__init__()
        self.rhalo = rhalo   #common to all size models

    def _print_error(self):
        print('Could not compute size function')
        return

    def get_distrib(self,Re,bins, Type='Cassata', stars = None, Re0=None):

        compactDef = globals()[Type]
        Re = Re-Re0
        #bins = bins-Re0
        cdef = compactDef(bins,Re,logstars = stars)
        H = cdef.hist()
        bwidth = bins[1]-bins[0]
        Bins = bins[1:]-0.5*bwidth
        try:
            return np.array([Bins, H])
        except:
            self._print_error

    def get_number_density_and_compacts(self,Re, bins, Type='Cassata', stars = None, Re0 = None):

        if Type=='Cassata':  #ugly, need to find a better solution
            if Re0 is None:
                raise ValueError("Re0 must be provided if Type=='Cassata'")
            else:
                Re = Re - Re0
                #  bins = bins -Re0

        compactDef = globals()[Type]
        cdef = compactDef(bins,Re,logstars = stars)

        N = cdef.number_density()
        Ncompact = cdef.compacts()
        try:
            return N, Ncompact
        except:
            self._print_error

    def get_environmental_dependence(self, Re, logstars, halos):

        M0 = np.median(logstars)
        M0 = 11.3
        gamma = 10**(Re+0.83*(11-logstars)) # above 10^11 0.83 is always valid
        mask = np.ma.masked_inside(halos,12.5,13).mask
        gamma_norm = np.mean(gamma[mask])
        gamma = gamma/gamma_norm

        halobins = np.arange(12.5,15,0.1)
        mean,_,_ = binned_statistic(halos, gamma,bins=halobins,statistic='mean')
        scatter,_,_ = binned_statistic(halos, gamma,bins=halobins,statistic='std')

        return halobins[1:]-0.05, mean, scatter

class K13Model(SizeModel):
    def __init__(self,rhalo, A_K, sigma_K,Vol=None):
        super().__init__(rhalo=rhalo, Vol=Vol)
        self.logA_K = np.log10(A_K)
        self.sigma_K = sigma_K

    def to_galaxy_size(self):
        return np.random.normal(self.logA_K+self.rhalo, self.sigma_K)

class Concentrationmodel(SizeModel):
    def __init__(self,rhalo, halos,z, A_c, sigma_c, gamma, Vol=None):
        super().__init__(rhalo=rhalo, Vol=Vol)
        self.logA_c = np.log10(A_c)
        self.sigma_c = sigma_c
        self.gamma = gamma
        self.halos = halos
        self.z = z

    def concentrationDuttonMaccio14(self):

        x = 10**(self.halos+np.log10(0.7))
        b =-0.097 +0.024*self.z
        a=0.537 + (1.025-0.537)*np.e**(-0.718*self.z**1.08)
        logc = a+b*(np.log10(x)-12)
        sc = 0.11
        logc  = np.random.normal(logc,scale=sc)
        return logc

    def to_galaxy_size(self): # A_c(c/10)**(-gamma)*R
        logc = self.concentrationDuttonMaccio14()
        x = self.logA_c - self.gamma*(logc-np.log10(10))  #log10(10)=1, but keep it like this for clarity
        return np.random.normal(x, self.sigma_c,size=len(self.rhalo)) + self.rhalo

class LambdaModel(SizeModel):
    def __init__(self,rhalo, A_lambda, Vol=None):
        super().__init__(rhalo=rhalo, Vol=Vol)
        self.logA_lambda = np.log10(A_lambda)

    def to_galaxy_size(self, skewed=False):

        if not skewed:
            ## Bullock+01
            lambda0 = -1.459
            sigma = 0.268
            return np.random.normal(lambda0+self.logA_lambda,sigma,size=len(self.rhalo)) + self.rhalo
        else:
            return self._sizes_from_lambda_skewed  #rodriguez-puebla+16

    def _sizes_from_lambda_skewed(self):

        lambdas = self._extract_lambdas()
        return self.rhalo+np.log10(lambdas)

    def _extract_lambdas(self):

        x=np.linspace(1.e-4,0.5,1000000)
        width =x[1]-x[0]
        cumul = self._Schechter_cumul(x,width)
        ff = interp1d(cumul,x)
        xx = np.random.uniform(0,1,size= len(R))
        lambdas = ff(xx)
        return lambdas

    def _Schechter_cumul(self,x,width):

        y = self._Schechter_like(x,width)
        cumul = np.cumsum(y)

        return cumul/np.max(cumul)

    def _Schechter_like(self,x,width):

        ###peebles spin
        alpha =-4.126
        beta=0.610
        lambda0 = -2.919

        first = (x/10**lambda0)**(-alpha)
        second = -(x/10**lambda0)**(beta)
        final = first*np.exp(second)

        area = np.sum(final*width)
        return final/area

class CompactDefinition:
    def __init__(self):
        super().__init__()
        self.Vol = (700)**3   #Fix
    def hist(self):
        bwidth = self.bins[1]-self.bins[0]
        return np.histogram(self.quantity, bins=self.bins)[0]/self.Vol/bwidth

    def number_density(self):
        h = self.hist()
        bwidth = self.bins[1]-self.bins[0]
        return  np.sum(h)*bwidth #cumtrapz(h,self.bins[1:])[-1]

    def compacts(self):
        h = self.hist()
        bwidth = self.bins[1]-self.bins[0]
        if self.threshold < 1:  # bit dirty, but encompasses Cassata and VanDerWel
            mask = np.ma.masked_less(self.bins[1:]-bwidth, self.threshold).mask
        else:
            mask = np.ma.masked_greater(self.bins[1:]-bwidth, self.threshold).mask
        return np.sum(h[mask])*bwidth # cumtrapz(,self.bins[1:][mask])[-1]

class Cassata(CompactDefinition):
    def __init__(self, bins, Re, logstars=None):
        super().__init__()
        self.bins = bins
        self.quantity = Re
        self.threshold = -0.4   #lower than

class VanDerWel(CompactDefinition):
    def __init__(self, bins, Re, logstars):
        super().__init__()
        self.bins = bins
        self.quantity = Re-0.75*(logstars-11)
        self.threshold = np.log10(2.5) #lower than

class Gargiulo(CompactDefinition):
    def __init__(self, bins, Re, logstars,):
        super().__init__()
        self.bins = bins
        self.quantity = logstars-2*Re-np.log10(2*np.pi) - 6 # -6 to convert from pc^-2 to kpc^-2
        self.threshold = 3.3 #higher than

class Barro(CompactDefinition):
    def __init__(self, bins, Re, logstars):
        super().__init__()
        self.bins = bins
        self.quantity = logstars-1.5*Re
        self.threshold = 10.3


from torchvision.transforms import transforms
from gaussian_blur import GaussianBlur
from torchvision import transforms, datasets
from get_dataset_util import ViewGen

class GetTransformedDataset:
    @staticmethod
    def get_simclr_transform(size, s=1):
        color_jitter = transforms.ColorJitter(
            0.8 * s, 0.8 * s, 0.8 * s, 0.2 * s)
        data_transforms = transforms.Compose([transforms.RandomResizedCrop(size=size),
                                              transforms.RandomHorizontalFlip(),
                                              transforms.RandomApply(
                                                  [color_jitter], p=0.8),
                                              transforms.RandomGrayscale(
                                                  p=0.2),
                                              GaussianBlur(
                                                  kernel_size=int(0.1 * size)),
                                              transforms.ToTensor()])
        return data_transforms

    def get_cifar10_train(self, n_views):
        return datasets.CIFAR10('.', train=True,
                                transform=ViewGen(
                                    self.get_simclr_transform(
                                        32),
                                    n_views),
                                download=True)

    def get_cifar10_test(self, n_views):
        return datasets.CIFAR10('.', train=False,
                                transform=transforms.ToTensor(),
                                download=True)

    def get_cifar100_test(self, n_views):
        return datasets.CIFAR100('.', train=False,
                                 transform=transforms.ToTensor(),
                                 download=True)


from fastapi import APIRouter, Depends, HTTPException,Query
from typing import List,Optional
from fastapi.responses import RedirectResponse,JSONResponse
from app.Core.Schemas.Book import BookBase,BookTitleQuery
from sqlalchemy.orm import Session
from app.database.db_connect import get_db
from app.Core.Models.Book import Book
from app.database.CRUD.Books import create_book,get_book_by_book_id,update_book,get_all_books,get_book_by_date,get_books_by_authors
from app.Routes import BookRoute
from app.Services.LoadData import GetBooks
from app.Services.CompareBooks import CompareBooks,CompareArraysBooks
from app.Services.GenerateBookSchema import GenerateBookSchema

router = APIRouter(
    prefix="/books",
    tags=["Books"],
    responses={404: {"description": "Not found"}},
)

@router.get("/",response_model=List[BookBase])
def getAll(db:Session = Depends(get_db),
            published_date:Optional[List[int]] = Query(None),
            sort:Optional[str] = "ASC",
            authors:Optional[List[str]] = Query(None),idBook:Optional[str] = None):
    books = []
    if published_date:
        books.extend(get_book_by_date(db,published_date))
    if authors:
        arr_temp = []
        for i in authors:
            arr_temp.extend(get_books_by_authors(db,i))
        if books:
            new_arr = CompareArraysBooks(arr_temp,books)
            books = new_arr
        else:
            books = arr_temp

    if idBook and books == None:
        books.append(get_book_by_book_id(db,idBook))

    if not books:
        books = get_all_books(db)

    if sort == "ASC":
        books.sort(key=lambda x: x.published_date)
    else:
        books.sort(key=lambda x: x.published_date,reverse=True)

    return books

@router.post("/")
def root(query:BookTitleQuery,db:Session = Depends(get_db)):
    books = GetBooks(query.q)
    BookSchemasList = GenerateBookSchema(books["items"])
    for book_compare in BookSchemasList:
        book = get_book_by_book_id(db,book_compare.book_id)
        if book == None:
            create_book(db,book_compare)
            continue
        if CompareBooks(book_compare,book):
            update_book(db,book_compare)
    url = router.url_path_for("getAll")
    return JSONResponse(content={"books":url})


#!/usr/bin/env python

# XMLRPC based Fault Tolerant Distributed File System - Meta Server: metaserver.py

from __future__ import print_function
import sys, SimpleXMLRPCServer, getopt, pickle, threading, xmlrpclib
from xmlrpclib import Binary

# Presents a dictionary with 'file pathname' as the key and a dictionary of the
# metadata of the given file as the value
class MetaServer:
	def __init__(self):
		self.data = {}

	# Retrieve metadata of file 'key' from the Meta Server
	def get(self, key, getPaths):
		print ("\n Retrieving meta data ... ")
		rv = {}                     # Default return value
		getPaths = pickle.loads(getPaths.data)

		# Retrieve required metadata.
		if (getPaths != 1):
			key = pickle.loads(key.data)
			if key in self.data:
				rv = self.data[key]

		# Retrieve all file pathnames
		else:
			rv = self.data.keys()
		self.print_content()
		return Binary(pickle.dumps(rv))

	# Insert metadata 'value' into the Meta Server for file 'key'
	def put(self, key, value):
		print ("\n Inserting data into Meta Server ... ")
		key = pickle.loads(key.data)
		value = pickle.loads(value.data)
		self.data[key] = value
		self.print_content()
		return True

	# Remove file 'key' and its contents from the Meta Server
	def rem(self, key):
		print ("\n Removind data from Meta Server ... ")
		key = pickle.loads(key.data)
		try:
			self.data.pop(key)
		except KeyError:
			pass
		self.print_content()
		return True

	# Print the contents of the Meta Server
	def print_content(self):
		print("\n **** Meta Server Contents **** ")
		print(self.data)
		return True

def main():
	#Check for correct arguments
	if len(sys.argv) != 2:
		print("usage: %s <metaserver port>" % sys.argv[0])
		exit(1)
	msPort = int(sys.argv[1])
	serve(msPort)

# Start the xmlrpc server and register the interfaces.
def serve(port):
	print ("\n Starting Meta Server ... ")
	file_server = SimpleXMLRPCServer.SimpleXMLRPCServer(('', port))
	file_server.register_introspection_functions()
	ms = MetaServer()
	file_server.register_function(ms.get)
	file_server.register_function(ms.put)
	file_server.register_function(ms.rem)
	file_server.register_function(ms.print_content)
	file_server.serve_forever()

if __name__ == "__main__":
	main()


""" Implementations of the SurrogateOptimizer class.

This module contains implementations of the SurrogateOptimizer ABC, which
are based on randomized search strategies.

Note that these strategies are all gradient-free, and therefore does not
require objective, constraint, or surrogate gradients methods to be defined.

The classes include:
 * ``RandomSearch`` -- search globally by generating random samples

"""

import numpy as np
import inspect
from parmoo.structs import SurrogateOptimizer, AcquisitionFunction
from parmoo.util import xerror

class RandomSearch(SurrogateOptimizer):
    """ Use randomized search to identify potentially efficient designs.

    Randomly search the design space and use the surrogate models to predict
    whether each search point is potentially Pareto optimal.

    """

    # Slots for the RandomSearch class
    __slots__ = ['n', 'lb', 'ub', 'acquisitions', 'constraints', 'objectives',
                 'budget']

    def __init__(self, o, lb, ub, hyperparams):
        """ Constructor for the RandomSearch class.

        Args:
            o (int): The number of objectives.

            lb (numpy.ndarray): A 1d array of lower bounds for the design
                region. The number of design variables is inferred from the
                dimension of lb.

            ub (numpy.ndarray): A 1d array of upper bounds for the design
                region. The dimension must match ub.

            hyperparams (dict): A dictionary of hyperparameters for the
                optimization procedure. It may contain the following:
                 * opt_budget (int): The sample size (default 10,000)

        Returns:
            SurrogateOptimizer: A new SurrogateOptimizer object.

        """

        # Check inputs
        xerror(o=o, lb=lb, ub=ub, hyperparams=hyperparams)
        self.n = lb.size
        self.lb = lb
        self.ub = ub
        # Check that the contents of hyperparams is legal
        if 'opt_budget' in hyperparams:
            if isinstance(hyperparams['opt_budget'], int):
                if hyperparams['opt_budget'] < 1:
                    raise ValueError("hyperparams['opt_budget'] "
                                     "must be positive")
                else:
                    self.budget = hyperparams['opt_budget']
            else:
                raise TypeError("hyperparams['opt_budget'] "
                                 "must be an integer")
        else:
            self.budget = 10000
        # Initialize the list of acquisition functions
        self.acquisitions = []
        return

    def setObjective(self, obj_func):
        """ Add a vector-valued objective function that will be solved.

        Args:
            obj_func (function): A vector-valued function that can be evaluated
                to solve the surrogate optimization problem.

        """

        # Check whether obj_func() has an appropriate signature
        if callable(obj_func):
            if len(inspect.signature(obj_func).parameters) != 1:
                raise ValueError("obj_func() must accept exactly one input")
            else:
                # Add obj_func to the problem
                self.objectives = obj_func
        else:
            raise TypeError("obj_func() must be callable")
        return

    def setReset(self, reset):
        """ Add a reset function for resetting surrogate updates.

        This method is not used by this class.

        """

        return

    def setPenalty(self, penalty_func, grad_func):
        """ Add a matrix-valued gradient function for obj_func.

        Args:
            penalty_func (function): A vector-valued penalized objective
                that incorporates a penalty for violating constraints.

            grad_func (function): A matrix-valued function that can be
                evaluated to obtain the Jacobian matrix for obj_func.

        """

        # Do nothing, RandomSearch is gradient free
        return

    def setConstraints(self, constraint_func):
        """ Add a constraint function that will be satisfied.

        Args:
            constraint_func (function): A vector-valued function from the
                design space whose components correspond to constraint
                violations. If the problem is unconstrained, a function
                that returns zeros could be provided.

        """

        # Check whether constraint_func() has an appropriate signature
        if callable(constraint_func):
            if len(inspect.signature(constraint_func).parameters) != 1:
                raise ValueError("constraint_func() must accept exactly one"
                                 + " input")
            else:
                # Add constraint_func to the problem
                self.constraints = constraint_func
        else:
            raise TypeError("constraint_func() must be callable")
        return

    def addAcquisition(self, *args):
        """ Add an acquisition function for the surrogate optimizer.

        Args:
            *args (AcquisitionFunction): Acquisition functions that are used
                to scalarize the list of objectives in order to solve the
                surrogate optimization problem.

        """

        # Check for illegal inputs
        if not all([isinstance(arg, AcquisitionFunction) for arg in args]):
            raise TypeError("Args must be instances of AcquisitionFunction")
        # Append all arguments to the acquisitions list
        for arg in args:
            self.acquisitions.append(arg)
        return

    def solve(self, x):
        """ Solve the surrogate problem using random search.

        Args:
            x (np.ndarray): A 2d array containing a list of feasible
                design points used to warm start the search.

        Returns:
            np.ndarray: A 2d numpy.ndarray containing a list of potentially
            efficient design points that were found by the random search.

        """

        from parmoo.util import updatePF

        # Check that x is legal
        if isinstance(x, np.ndarray):
            if self.n != x.shape[1]:
                raise ValueError("The columns of x must match n")
            elif len(self.acquisitions) != x.shape[0]:
                raise ValueError("The rows of x must match the number " +
                                 "of acquisition functions")
        else:
            raise TypeError("x must be a numpy array")
        # Check that x is feasible.
        for xj in x:
            if any(self.constraints(xj) > 0.00000001) or \
               np.any(xj[:] < self.lb[:]) or \
               np.any(xj[:] > self.ub[:]):
                raise ValueError("some of starting points (x) are infeasible")
        # Set the batch size
        batch_size = 1000
        # Initialize the database
        o = self.objectives(x[0, :]).size
        p = self.constraints(x[0, :]).size
        data = {'x_vals': np.zeros((batch_size, self.n)),
                'f_vals': np.zeros((batch_size, o)),
                'c_vals': np.zeros((batch_size, p))}
        # Loop over batch size until k == budget
        k = 0
        nondom = {}
        while (k < self.budget):
            # Check how many new points to generate
            k_new = min(self.budget, k + batch_size) - k
            if k_new < batch_size:
                data['x_vals'] = np.zeros((k_new, self.n))
                data['f_vals'] = np.zeros((k_new, o))
                data['c_vals'] = np.zeros((k_new, p))
            # Randomly generate k_new new points
            for i in range(k_new):
                data['x_vals'][i, :] = np.random.random_sample(self.n) \
                                       * (self.ub[:] - self.lb[:]) + self.lb[:]
                data['f_vals'][i, :] = self.objectives(data['x_vals'][i, :])
                data['c_vals'][i, :] = self.constraints(data['x_vals'][i, :])
            # Update the PF
            nondom = updatePF(data, nondom)
            k += k_new
        # Use acquisition functions to extract array of results
        results = []
        for acq in self.acquisitions:
            imin = np.argmin(np.asarray([acq.scalarize(fi)
                                         for fi in nondom['f_vals']]))
            results.append(nondom['x_vals'][imin, :])
        return np.asarray(results)


import numpy as np
from numpy.linalg import norm
from numpy import zeros, dot, cross
from math import sqrt, pi, cos, sin, tan
from cmath import exp

from Gauss_1D import Gauss_1D

def DIRECT_WS_ST_RWG(r1, r2, r3, Np_1D, ko):
    '''
    Main body of the DIRECT EVALUATION method for the
     evaluation of the coincident 4-D weakly singular integrals over planar
     triangular elements.

      Licensing: This code is distributed under the GNU LGPL license.

      Modified:  19 October 2011

      Author:    Athanasios Polimeridis

      Ported to python: Evgeny Frolov

     References

     A. G. Polimeridis and T. V. Yioultsis, ?On the direct evaluation of weakly singular
     integrals in Galerkin mixed potential integral equation formulations,? IEEE Trans.
     Antennas Propag., vol. 56, no. 9, pp. 3011-3019, Sep. 2008.

     A. G. Polimeridis and J. R. Mosig, ?Complete semi-analytical treatment of weakly
     singular integrals on planar triangles via the direct evaluation method,? Int. J.
     Numerical Methods Eng., vol. 83, pp. 1625-1650, 2010.

     A. G. Polimeridis, J. M. Tamayo, J. M. Rius and J. R. Mosig, ?Fast and accurate
     computation of hyper-singular integrals in Galerkin surface integral equation
     formulations via the direct evaluation method,? IEEE Trans.
     Antennas Propag., vol. 59, no. 6, pp. 2329-2340, Jun. 2011.

     A. G. Polimeridis and J. R. Mosig, ?On the direct evaluation of surface integral
     equation impedance matrix elements involving point singularities,? IEEE Antennas
     Wireless Propag. Lett., vol. 10, pp. 599-602, 2011.

     INPUT DATA
     r1,r2,r3 = point vectors of the triangular element's vertices
     Outer triangle P:(rp1,rp2,rp3)=(r1,r2,r3)
     Inner triangle Q:(rq1,rq2,rq3)=(r1,r2,r3)
     Np_1D = order of the Gauss-Legendre quadrature rule

     OUTPUT DATA
                 I_DE[0]  = I_f1_f1
                 I_DE[1]  = I_f1_f2
                 I_DE[2]  = I_f1_f3
                 I_DE[3]  = I_f2_f1
                 I_DE[4]  = I_f2_f2
                 I_DE[5]  = I_f2_f3
                 I_DE[6]  = I_f3_f1
                 I_DE[7]  = I_f3_f2
                 I_DE[8]  = I_f3_f3

    '''
    SQRT3 = sqrt(3)

    ##################  Triangle Area   #######################################
    Ap = 0.5 * norm(cross(r2-r1,r3-r1))
    ##################  Jacobian   ############################################
    Jp = Ap/SQRT3
    ###########################################################################
    Asing0 = norm(r1-r2)**2 / 4
    Asing1 = dot(r1-r2,r1+r2-2*r3) / (2*SQRT3)
    Asing2 = norm(r1+r2-2*r3)**2 / 12
    #
    Asing3 =      0.25 * Asing0 - (SQRT3/4) * Asing1 +      0.75 * Asing2
    Asing4 = (SQRT3/2) * Asing0 -       0.5 * Asing1 - (SQRT3/2) * Asing2
    Asing5 =      0.75 * Asing0 + (SQRT3/4) * Asing1 +      0.25 * Asing2
    #
    Asing6 =      0.25 * Asing0 + (SQRT3/4) * Asing1 +      0.75 * Asing2
    Asing7 =-(SQRT3/2) * Asing0 -       0.5 * Asing1 + (SQRT3/2) * Asing2
    Asing8 =      0.75 * Asing0 - (SQRT3/4) * Asing1 +      0.25 * Asing2
    #
    Asing = np.asarray([[Asing0, Asing3, Asing6],
                        [Asing1, Asing4, Asing7],
                        [Asing2, Asing5, Asing8]])
    ###########################################################################
    #                  1D  Gauss-Legendre Quadrature Rule                     #
    ###########################################################################
    w, z = Gauss_1D(Np_1D)

    Isub = zeros((3, 3, 3), dtype = complex)
    #---------------------------------------------
    for m in range(3):
        ######### Define the coefs of the appropriate subtriangle
        acc = Asing[0, m]
        acs = Asing[1, m]
        ass = Asing[2, m]
        ##################### Gauss quadrature ################################
        Int1_1_1 = Int2_1_1 = Int3_1_1 = 0.0

        for kk in range(Np_1D):
            ############### Int1,  0 =< PSI <= pi/3 ###########################
            PSI_1a = 0.0
            PSI_1b = pi/3
            PSI_1k = ((PSI_1b-PSI_1a)*z[kk]+(PSI_1b+PSI_1a))/2
            a_PSI_1k = sqrt(acc*cos(PSI_1k)**2-acs*cos(PSI_1k)*sin(PSI_1k)+ass*sin(PSI_1k)**2)
            # Int1_1_1
            PHI_a_1_1 = Phi_1_1(1,PSI_1k,a_PSI_1k,ko)
            PHI_e_1_1 = Phi_1_1(5,PSI_1k,a_PSI_1k,ko)
            PHI_f_1_1 = Phi_1_1(6,PSI_1k,a_PSI_1k,ko)

            F1_1_1 = PHI_a_1_1+PHI_e_1_1+PHI_f_1_1
            Int1_1_1  = Int1_1_1+w[kk]*F1_1_1
            ############### Int2,  pi/3 =< PSI <= 2pi/3 #######################
            PSI_2a = pi/3
            PSI_2b = 2*pi/3
            PSI_2k = ((PSI_2b-PSI_2a)*z[kk]+(PSI_2b+PSI_2a))/2
            a_PSI_2k = sqrt(acc*cos(PSI_2k)**2-acs*cos(PSI_2k)*sin(PSI_2k)+ass*sin(PSI_2k)**2)
            # Int2_1_1
            PHI_b_1_1 = Phi_1_1(2,PSI_2k,a_PSI_2k,ko)
            PHI_g_1_1 = Phi_1_1(7,PSI_2k,a_PSI_2k,ko)

            F2_1_1 = PHI_b_1_1+PHI_g_1_1
            Int2_1_1  = Int2_1_1+w[kk]*F2_1_1
            ############### Int3,  2pi/3 =< PSI <= pi #########################
            PSI_3a = 2*pi/3
            PSI_3b = pi
            PSI_3k = ((PSI_3b-PSI_3a)*z[kk]+(PSI_3b+PSI_3a))/2
            a_PSI_3k = sqrt(acc*cos(PSI_3k)**2-acs*cos(PSI_3k)*sin(PSI_3k)+ass*sin(PSI_3k)**2)
            # Int3_1_1
            PHI_c_1_1 = Phi_1_1(3,PSI_3k,a_PSI_3k,ko)
            PHI_d_1_1 = Phi_1_1(4,PSI_3k,a_PSI_3k,ko)
            PHI_h_1_1 = Phi_1_1(8,PSI_3k,a_PSI_3k,ko)

            F3_1_1 = PHI_c_1_1+PHI_d_1_1+PHI_h_1_1
            Int3_1_1  = Int3_1_1+w[kk]*F3_1_1

        # Isub_1_1
        Int1_1_1 = ((PSI_1b-PSI_1a)/2)*Int1_1_1
        Int2_1_1 = ((PSI_2b-PSI_2a)/2)*Int2_1_1
        Int3_1_1 = ((PSI_3b-PSI_3a)/2)*Int3_1_1

        Isub[0,0,m] = Int1_1_1+Int2_1_1+Int3_1_1

    ###########################################################################
    #               Output
    ###########################################################################
    I_DE = Jp**2 * (Isub[0, 0, 0]+Isub[0, 0, 1]+Isub[0, 0, 2])
    return I_DE

def Phi_1_1(argument,psi,Apsi, ko):
    ## Phi_1_1 function

    #  Licensing: This code is distributed under the GNU LGPL license.

    #  Modified:  20 September 2011

    #  Author:    Athanasios Polimeridis

    # References

    # A. G. Polimeridis and T. V. Yioultsis, ?On the direct evaluation of weakly singular
    # integrals in Galerkin mixed potential integral equation formulations,? IEEE Trans.
    # Antennas Propag., vol. 56, no. 9, pp. 3011-3019, Sep. 2008.

    # A. G. Polimeridis and J. R. Mosig, ?Complete semi-analytical treatment of weakly
    # singular integrals on planar triangles via the direct evaluation method,? Int. J.
    # Numerical Methods Eng., vol. 83, pp. 1625-1650, 2010.

    # A. G. Polimeridis, J. M. Tamayo, J. M. Rius and J. R. Mosig, ?Fast and accurate
    # computation of hyper-singular integrals in Galerkin surface integral equation
    # formulations via the direct evaluation method,? IEEE Trans.
    # Antennas Propag., vol. 59, no. 6, pp. 2329-2340, Jun. 2011.

    # A. G. Polimeridis and J. R. Mosig, ?On the direct evaluation of surface integral
    # equation impedance matrix elements involving point singularities,? IEEE Antennas
    # Wireless Propag. Lett., vol. 10, pp. 599-602, 2011.

    # INPUT DATA
    # argument  = 1-8 -> a-h
    # psi       = Variable Psi
    # Apsi      = alpha(Psi)

    # OUTPUT DATA
    # Phi_1_1
    #

#    global ko
    SQRT3 = sqrt(3)

    A = 1j*ko*Apsi
    B = cos(psi)
    C = sin(psi)/SQRT3

    ###########################################################################
    #                           Int_1_1                                       #
    ###########################################################################
    D = sin(psi)/((1j*ko)**2*Apsi**3)

    if argument == 1:
        ######################    PHI_a   #########################################
        PSI_a1 = A/(2*B)
        PSI_a2 = -1.
        PSI_a3 = (B/A)*(1-exp(-A/B))
        PSI_a  = PSI_a1+PSI_a2+PSI_a3
        PHI    = D*PSI_a

    elif argument == 2:
        ######################    PHI_b   #########################################
        PSI_b1 = A/(2*C)
        PSI_b2 = -1.
        PSI_b3 = (C/A)*(1-exp(-A/C))
        PSI_b  = PSI_b1+PSI_b2+PSI_b3
        PHI    = D*PSI_b

    elif argument == 3:
        ######################    PHI_c   #########################################
        beta    = tan(pi-psi)/SQRT3
        gamma   = (1.-beta)/(1.+beta)

        PSI_c1 = (A/C)*(0.5-(gamma-gamma**2/2))
        PSI_c2 = -(1.-gamma)
        PSI_c3 = (C/A)*(1.-exp(-A*(1.-gamma)/C))
        PSI_c  = PSI_c1+PSI_c2+PSI_c3
        PHI    = D*PSI_c

    elif argument == 4:
        ######################    PHI_d   #########################################
        beta    = tan(pi-psi)/SQRT3
        gamma   = (1.-beta)/(1.+beta)

        PSI_d1 = -A*(gamma+0.5*gamma**2)/B
        PSI_d2 = -gamma
        PSI_d3 = (B/A)*(exp(A*(1.+gamma)/B)-exp(A/B))
        PSI_d  = PSI_d1+PSI_d2+PSI_d3
        PHI    = D*PSI_d

    elif argument == 5:
        ######################    PHI_e   #########################################
        epsilon = tan(psi)/SQRT3
        delta   = -(1.-epsilon)/(1.+epsilon)

        PSI_e1 = A*(0.5*delta**2-delta)/B
        PSI_e2 = delta
        PSI_e3 = (B/A)*(exp(-A/B)-exp(-A*(1.-delta)/B))
        PSI_e  = PSI_e1+PSI_e2+PSI_e3
        PHI    = D*PSI_e

    elif argument == 6:
        ######################    PHI_f   #########################################
        epsilon = tan(psi)/SQRT3
        delta   = -(1.-epsilon)/(1.+epsilon)

        PSI_f1 = (A/C)*(delta+0.5*delta**2+0.5)
        PSI_f2 = -(1.+delta)
        PSI_f3 = (C/A)*(1.-exp(-A*(1.+delta)/C))
        PSI_f  = PSI_f1+PSI_f2+PSI_f3
        PHI    = D*PSI_f

    elif argument == 7:
        ######################    PHI_g   #########################################
        PSI_g1 = A/(2.*C)
        PSI_g2 = -1.
        PSI_g3 = (C/A)*(1.-exp(-A/C))
        PSI_g  = PSI_g1+PSI_g2+PSI_g3
        PHI    = D*PSI_g

    elif argument == 8:
        ######################    PHI_h   #########################################
        PSI_h1 = -A/(2.*B)
        PSI_h2 = -1.
        PSI_h3 = (B/A)*(exp(A/B)-1.)
        PSI_h  = PSI_h1+PSI_h2+PSI_h3
        PHI    = D*PSI_h

    else:
        raise Exception('Wrong argument:\n{0}'.format(argument))

    return PHI


from bs4 import BeautifulSoup
import urllib.request
import json
from crawler.pidgeon import Pidgeon
import re,logging

logger = logging.getLogger(__name__)

class Owl:
   def __init__(self, url, everything=True, flavor="all"):
      #self.user_agent = 'Mozilla/5.0'
      self.user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.114 Safari/537.36'
      self.url = url
      self.pidgeon = Pidgeon()
      self.host = self.findHost(url)
      self.flavor = flavor

      logger.info('owl initializing:: url: %s:: host: %s' % (url, self.host))
      if everything:
         self.process()

#processes everything
   def process(self):
      self.getSoup()
      self.parseSoup(self.flavor)

#  finds which part of the url is the host
   def findHost(self, url):
      logger.debug('finding host: %s' % url)
      bits = url[7:].split('.')
      logger.debug('bits of url: %s' % bits)
      for i in range(2):
         if self.pidgeon.checkHost(bits[i]):
            return bits[i]
         for check in bits:
            if self.pidgeon.checkHost(check):
               return check
      raise HostError(url)

#  connects to the website and creates a soup object out of it.
# can take parameters defining if cookies are needed and
# any extra headers
   def getSoup(self, url=False, headers=True, cookies=False):
      if url is False:
         url = self.url
         host = self.host
      else:
         host = self.findHost(url)

      conn = self.pidgeon.connType(host)

      html=''
      try:
         if conn is not None:
            if cookies or 'cookies' in conn:
               cookies = True
               headers = False
               import http.cookiejar
               cj = http.cookiejar.CookieJar()
               opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
               html = opener.open(url).read().decode('utf-8')
            if 'headers' in conn:
               if type(conn) is dict:
                  headers = conn['headers']
               else:
                  headers = False
         if headers:
            req = urllib.request.Request(url)
            if headers == True:
               req.add_header('User-agent', self.user_agent)
            else:
               req.add_header('User-agent', headers)
            html = urllib.request.urlopen(req).read()
         elif not cookies:
            html = urllib.request.urlopen(url).read()
      except urllib.error.URLError as e:
         logger.error(url)
         raise e
      html = str(html)
      self.soup = BeautifulSoup(html)
      headers = True

#parses the soup getting title, text, tags, etc.
   def parseSoup(self, flavor):
      if flavor == "all":
         self.text = self.getText()
         self.title = self.getTitle()
         self.tags = self.getTags()
         self.date = self.getDate()
         self.desc = self.getDesc()
         self.author = self.getAuthor()
         self.data = self.getData()

         logger.debug('length of text: %i' % len(str(self.text.split('\n'))))
      elif flavor == "chicken":
         pass

#  returns the body text of the article so
#  long as the host is in the source ref file
   def getText(self):
#     uses a search term dictionary to generate a soup search
      def getItems(query):
         if 'attrs' in query.keys():
            items = self.soup.find_all(query['tag'], attrs=query['attrs'])
         else:
            try:
               items = self.soup.find_all(query['tag'])
            except RuntimeError as e:
               raise RuntimeError('error using soup.find_all::url:%s::query:%s' % (self.url, query))
         return items

#     processes a paragraph by removing certain characters
      def processPara(s):
         s=''.join(s.split('\r'))
         s=''.join(s.split('\n'))
         s=' '.join(s.split('\t'))
         s=s.replace('"',"'").strip()
         s=s.encode('iso8859-1','ignore').decode('unicode_escape','ignore')
         return s

#     suddounds the text in all <a> tags with 'link' so it
#     appears in the processed text
      def processLinks(tag):
         for link in tag.find_all("a"):
            if not link.string: continue
            link.string = "'link'" + link.string + "'link'"

#     soup objects which are to be removed before text is extracted
      excepts = self.pidgeon.exceptions(self.host)
      logger.debug('excepts: %s' % excepts)
#     iterable list of tags/items searched for in the soup
#     if no text was previously found, listed sources are overriden and all
#     p tags are used instead
      queries = iter(list(self.pidgeon.query(self.host)))

#     chooses the right query by comparing the number of items returned
      query = next(queries)
      items = getItems(query)
      while len(items) < 3:
         try:
            query = next(queries)
            items2 = getItems(query)
         except StopIteration:
            break
         if len(items2) > len(items): items = items2
      if len(items) == 0:
         logger.warning('couldnt find article text:: url: %s' % self.url)
         raise TextError(self.url)

#     removes all excepted tags
      if excepts:
#        creates an appendable list if there are multiple
#        except clauses
         if type(excepts) is list:
            excludes = []
            for clause in excepts:
               rules = getItems(clause)
               for i in rules:
                  excludes.append(i)
         else:
            excludes = getItems(excepts)
#        removes excepted tags
         for fluff in excludes:
            fluff.decompose()

#     the output text in list form
      text = []
      for item in items:
         #processLinks(item)
#        for all queries that do not directly search for <p> tags
         if query['tag'] != 'p':
            paras = []
#           searches paragraphs and processes them
            for para in item.find_all('p'):
               s=para.get_text()
               s=processPara(s)
               paras.append(s)
#           appends found paragraphs to text
            text.append('\n'.join(paras))
         else:
            s=item.get_text()
            s=processPara(s)
            text.append(s)
      return '\n'.join(text)

#  gets the articles tags from metadata
   def getTags(self):
      tags = self.soup.find('meta', attrs={'name':re.compile(r'(?i)keywords$')})
      if tags is not None and tags.attrs['content'] != '':
         tags = tags.attrs['content']
         if tags[-1] == ',': tags = tags[:-1]
         return tags
      return ''

#  gets the articles published date from the metadata
#  YYYY-MM-DD
   def getDate(self):
      r=re.compile(r'(?i)date')
      for i in self.soup.find_all('meta',attrs={'name':r}):
         date = i.attrs['content']
         if len(date) == 8:
            return '-'.join([date[:4],date[4:6],date[6:]])
         if '-' in date:
            return '-'.join(date.split('-')[:3])[:10]
         if '/' in date:
            return '-'.join(date.split('/')[:3])[:10]
      return ''

#  gets the articles title from the metadata
   def getTitle(self):
      titles = [self.soup.find('meta', attrs={'property':re.compile(r'title$')})]
      titles.append(self.soup.find('meta', attrs={'name':re.compile(r'title$')}))

      out = ''
      for title in titles:
          if title is not None:
             title = title.attrs['content']
             title = title.replace('"',"'")
             if len(title) > len(out): out = title
      out = out.strip()
      out = out.encode('iso8859-1','ignore').decode('unicode_escape','ignore')
      return out

   def getDesc(self):
      desc = self.soup.find('meta', attrs={'property':re.compile(r'(?i)description')})
      if desc is not None:
         desc = desc.attrs['content']
         desc = desc.replace('"',"'")
         desc = desc.strip()
         desc = desc.encode('iso8859-1','ignore').decode('unicode_escape','ignore')
         return desc
      return ''

   def getAuthor(self):
      json = self.pidgeon.author(self.host)
      if json is None:
         return None
      author = None
      for method in json:
         logger.debug('method: %s' % method)
         if method['method'] == 'search':
            author=self.getAuthor_search(method['string'])
         elif method['method'] == 'meta':
            author = self.getAuthor_meta()
         elif method['method'] == 'soup_query':
            author = self.getAuthor_soup(method)
         if author:
            break
      logger.debug('author: %s' % author)
      if author is None:
         author = ''
      else:
         author = author.title().replace('\\','')
      return author

   def getAuthor_meta(self):
      x = self.soup.find_all('meta',attrs={'name':'author'})
      if x:
         return x[0].get('content')
      return None

   def getAuthor_search(self, string):
      string = string.replace('\\','')
      for script in self.soup.find_all('script'):
         text = str(script.string)
         text = text.replace('\\n','\n')
         text = text.replace('\\t','\t')
         text = text.replace('\\\'','\'')
         text = text.replace('\\"','\"')

         x=re.search(string, text)
         if x:
            return x.group(1)
      return None

#  uses the style in spider to find the text version of the
#  author's name, found usually by the article's title
   def getAuthor_soup(self, query):
      search = self.pidgeon.search(self.soup,query)
      if len(search) > 0:
         author = search[0].text
         logger.debug('found author: %s' % author)
         return author
      return None

#  gets all the metadata from the soup
   def getMeta(self):
      return {'title':str(self.title),'tags':str(self.tags),'date':str(self.date),'summary':str(self.desc)}

#  gets all the data that is not none and puts
#  it in a dictionary; takes a list argument to only return those items
#  if dic is a list, getData does not remove data values that are None
   def getData(self, dic=None):
      data = {'host':self.host,'url':self.url}
      if self.text != '': data['text'] = self.text
      if self.title != '': data['title'] = self.title
      if self.tags != '': data['ATags'] = self.tags
      if self.desc != '': data['summary'] = self.desc
      if self.date != '': data['date'] = self.date
      if self.author != '': data['author'] = self.author
      if type(dic) is list:
         output = {}
         for prop in list:
            output['list'] = data[prop]
         return output
      return data

class OwlError(ValueError):
    def __init__(self, url):
        self.url = url

class HostError(OwlError):
   def __str__(self):
      return 'no known host found in url: ' + str(self.url)

class TextError(OwlError):
   def __str__(self):
      return 'unable to find text: ' + str(self.url)