"""
# adc16.py

Provides KATCP wrapper around ADC16-based CASPER design.  Includes many
convenience functions for writing to the registers of the ADC chips,
calibrating the SERDES blocks, and accessing status info about the ADC16
design and clock status.  While most access will be done via the methods of
this class, there may be occasion to access the ADC16 controller directly
(via the #adc16_controller method, which returns a KATCP::Bram object)

## ADC16 controller memory map

   # ======================================= #             # ======================================= #
   # ADC16 3-Wire Register (word 0)          #             # ADC16 Control Register (word 1)         #
   # ======================================= #             # ======================================= #
   # LL = Clock locked bits                  #             # W  = Deux write-enable                  #
   # NNNN = Number of ADC chips supported    #             # MM = Demux mode                         #
   # RR = ROACH2 revision expected/required  #             # R = ADC16 Reset                         #
   # C = SCLK                                #             # S = Snap Request                        #
   # D = SDATA                               #             # H = ISERDES Bit Slip Chip H             #
   # 7 = CSNH (chip select H, active high)   #             # G = ISERDES Bit Slip Chip G             #
   # 6 = CSNG (chip select G, active high)   #             # F = ISERDES Bit Slip Chip F             #
   # 5 = CSNF (chip select F, active high)   #             # E = ISERDES Bit Slip Chip E             #
   # 4 = CSNE (chip select E, active high)   #             # D = ISERDES Bit Slip Chip D             #
   # 3 = CSND (chip select D, active high)   #             # C = ISERDES Bit Slip Chip C             #
   # 2 = CSNC (chip select C, active high)   #             # B = ISERDES Bit Slip Chip B             #
   # 1 = CSNB (chip select B, active high)   #             # A = ISERDES Bit Slip Chip A             #
   # 0 = CSNA (chip select A, active high)   #             # T = Delay Tap
   # ======================================= #             # i = Bitslip specific channel(out of 8)  #
   # |<-- MSb                       LSb -->| #             # ======================================= #
   # 0000_0000_0011_1111_1111_2222_2222_2233 #             # |<-- MSb                       LSb -->| #
   # 0123_4567_8901_2345_6789_0123_4567_8901 #             # 0000 0000 0011 1111 1111 2222 2222 2233 #
   # ---- --LL ---- ---- ---- ---- ---- ---- #             # 0123 4567 8901 2345 6789 0123 4567 8901 #
   # ---- ---- NNNN ---- ---- ---- ---- ---- #             # ---- -WMM ---- ---- ---- ---- ---- ---- #
   # ---- ---- ---- --RR ---- ---- ---- ---- #             # ---- ---- ---R ---- ---- ---- ---- ---- #
   # ---- ---- ---- ---- ---- --C- ---- ---- #             # ---- ---- ---- ---S ---- ---- ---- ---- #
   # ---- ---- ---- ---- ---- ---D ---- ---- #             # ---- ---- ---- ---- HGFE DCBA iii- ---- #
   # ---- ---- ---- ---- ---- ---- 7654 3210 #             # ---- ---- ---- ---- ---- ---- ---T TTTT #
   # |<--- Status ---->| |<--- 3-Wire ---->| #             # ======================================= #
   # ======================================= #             # NOTE: W enables writing the MM bits.    #
   # NOTE: LL reflects the runtime lock      #             #       Some of the other bits in this    #
   #       status of a line clock from each  #             #       register are one-hot.  Using      #
   #       ADC board.  A '1' bit means       #             #       W ensures that the MM bits will   #
   #       locked (good!).  Bit 5 is always  #             #       only be written to when desired.  #
   #       used, but bit 6 is only used when #             #       00: demux by 1 (single channel)   #
   #       NNNN is 4 (or less).              #             # ======================================= #
   # ======================================= #             # NOTE: MM selects the demux mode.        #
   # NOTE: NNNN and RR are read-only values  #             #       00: demux by 1 (quad channel)     #
   #       that are set at compile time.     #             #       01: demux by 2 (dual channel)     #
   #       They do not indicate the state    #             #       10: demux by 4 (single channel)   #
   #       of the actual hardware in use     #             #       11: undefined                     #
   #       at runtime.                       #             #       ADC board.  A '1' bit means       #
   # ======================================= #             #       locked (good!).  Bit 5 is always  #
                                                           #       used, but bit 6 is only used when #
                                                           #       NNNN is 4 (or less).              #
                                                           # ======================================= #

   # =============================================== #     # =============================================== #
   # ADC16 Delay A Strobe Register (word 2)          #     # ADC0 Delay B Strobe Register (word 3)           #
   # =============================================== #     # =============================================== #
   # D = Delay Strobe (rising edge active)           #     # D = Delay Strobe (rising edge active)           #
   # =============================================== #     # =============================================== #
   # |<-- MSb                              LSb -->|  #     # |<-- MSb                              LSb -->|  #
   # 0000  0000  0011  1111  1111  2222  2222  2233  #     # 0000  0000  0011  1111  1111  2222  2222  2233  #
   # 0123  4567  8901  2345  6789  0123  4567  8901  #     # 0123  4567  8901  2345  6789  0123  4567  8901  #
   # DDDD  DDDD  DDDD  DDDD  DDDD  DDDD  DDDD  DDDD  #     # DDDD  DDDD  DDDD  DDDD  DDDD  DDDD  DDDD  DDDD  #
   # |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  #     # |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  #
   # H4 H1 G4 G1 F4 F1 E4 E1 D4 D1 C4 C1 B4 B1 A4 A1 #     # H4 H1 G4 G1 F4 F1 E4 E1 D4 D1 C4 C1 B4 B1 A4 A1 #
   # =============================================== #     # =============================================== #

"""

import time
from . import katcp_wrapper
import numpy as np
import struct
import logging

katcp_port = 7147

class SnapAdc(object):
    """" Controller for HMCAD1511 ADC chip, as used in CASPER SNAP and ROACH2 boards

    Provides control and calibration routines for the HMCAD1511.

    Args:
        host (SnapBoard or FpgaClient object): Instance which has the adc16_controller

    """

    def __repr__(self):
        return "<SNAP HMCAD1511 8-bit ADC controller on %s>" % self.host.host

    def __init__(self, host):
        self.host = host

        # create a chip dictionary to facilitate writing to adc16_controller
        self.demux_mode    = 1              # Default to non-interleaved
        self.gain          = 1              # Default to gain of 1
        self.chip_select   = 7              # Default to select all chips
        self.chips = {'a': 0, 'b': 1, 'c': 2}

        self.logger = logging.getLogger('SnapAdc')

    def set_chip_select(self, chips):
        """ Setup which chips will be used in the programmed design

        Notes:
            update chip dictionary to facilitate writing to adc16_controller. This creates
            a self.chips dictionary and a self.chip_select which are used to tell if a
            given chip is enabled.

        Args:
            chips (list or str): list of chips, ['a', 'b', 'c'] or 'all', 'a', 'b' or 'c'
        """
        if isinstance(chips, str):
            if chips == 'all':
                self.chip_select = 0b111
                self.chips = {'a': 0, 'b': 1, 'c': 2}
            elif chips == 'a':
                self.chip_select = 0b001
                self.chips = {'a': 0}
            elif chips == 'b':
                self.chip_select = 0b010
                self.chips = {'b': 1}
            elif chips == 'c':
                self.chip_select = 0b100
                self.chips = {'c': 2}
        else:
            if 'all' in chips:
                self.chip_select = 0b111
                self.chips = {'a': 0, 'b': 1, 'c': 2}
            else:
                self.chips = {}
                chip_a, chip_b, chip_c = 0, 0, 0
                if 'a' in chips:
                    chip_a = 1
                    self.chips['a'] = 0
                if 'b' in chips:
                    chip_b = 1
                    self.chips['b'] = 1
                if 'c' in chips:
                    chip_c = 1
                    self.chips['c'] = 2
                self.chip_select = int('0b%i%i%i' % (chip_a, chip_b, chip_c), 2)

    def write(self, addr, data):
        """
        # write_adc is used for writing specific ADC registers.
        # ADC controller can only write to adc one bit at a time at rising clock edge
        """
        SCLK = 0x200
        CS = self.chip_select
        IDLE = SCLK
        SDA_SHIFT = 8
        self.host.write_int('adc16_controller', IDLE, offset=0, blindwrite=True)
        for i in range(8):
            addr_bit = (addr >> (8 - i - 1)) & 1
            state = (addr_bit << SDA_SHIFT) | CS
            self.host.write_int('adc16_controller', state, offset=0, blindwrite=True)
            self.logger.debug("Printing address state written to adc16_controller, offset=0, clock low")
            self.logger.debug(np.binary_repr(state, width=32))
            state = (addr_bit << SDA_SHIFT) | CS | SCLK
            self.host.write_int('adc16_controller', state, offset=0, blindwrite=True)
            self.logger.debug("Printing address state written to adc16_controller, offset=0, clock high")
            self.logger.debug(np.binary_repr(state, width=32))

        for j in range(16):
            data_bit = (data >> (16 - j - 1)) & 1
            state = (data_bit << SDA_SHIFT) | CS
            self.host.write_int('adc16_controller', state, offset=0, blindwrite=True)
            self.logger.debug("Printing data state written to adc16_controller, offset=0, clock low")
            self.logger.debug(np.binary_repr(state, width=32))
            state = (data_bit << SDA_SHIFT) | CS | SCLK
            self.host.write_int('adc16_controller', state, offset=0, blindwrite=True)
            self.logger.debug("Printing data address state written to adc16_controller, offset=0, clock high")
            self.logger.debug(np.binary_repr(state, width=32))

        self.host.write_int('adc16_controller', IDLE, offset=0, blindwrite=True)

    def power_cycle(self):
        """ Power cycle the ADC """
        logging.info('Power cycling the ADC')
        self.power_off()
        self.power_on()

    def power_off(self):
        """ Turn power to ADC off """
        self.logger.info('Power ADC down')
        self.write(0x0f, 0x0200)

    def power_on(self):
        """ Turn power to ADC on """
        self.logger.info('Power ADC up')
        self.write(0x0f, 0x0000)

    def reset(self):
        """ Reset the ADC """
        self.logger.info('Initializing ADC')
        # reset adc
        self.write(0x00, 0x0001)

    def initialize(self, chips='all', demux_mode=1, gain=1):
        """ Initialize the ADC

        Args:
            chip_select (str or list): 'all' or list of chips (a,b and/or c ) to enable. Defaults to 'all'
            demux_mode (int): Set demulitplexing to 1 (no interleave), 2 or 4 (interleave all). Defaults to 1.
            gain (int): Digital gain to apply, 1-32, default 1, below 8 recommended.
        """
        self.set_chip_select(chips)
        self.reset()
        self.power_off()
        self.set_demux(demux_mode)
        self.power_on()
        self.set_gain(gain)

    def set_demux(self, demux_mode):
        """ Set demux factor for ADC mode

        Interleave all inputs: demux=4
        No interleaving: demux=1
        Interleaving ADC0 and ADC2: demux=2

        Args:
            demux_mode (int): Set demulitplexing to 1 (no interleave), 2 or 4 (interleave all)
        """
        self.demux_mode = demux_mode
        if self.demux_mode == 1:
            # Setting number of channes to 4
            self.write(0x31, 0x04)
            # Route inputs to respective ADC's for demux 1
            self.logger.info('Routing all four inputs to corresponding ADC channels')
            self.write(0x3a, 0x0402)
            self.write(0x3b, 0x1008)
        elif self.demux_mode == 2:
            # Setting number of channels to 2
            self.write(0x31, 0x02)
            # Routing input 1 and input 3 to ADC for interleaving
            self.logger.info('Setting ADC to interleave inputs 1 (ADC0) and 3 (ADC2)')
            # Selecting input 1
            self.write(0x3a, 0x0202)
            # Selecting input 3
            self.write(0x3b, 0x0808)
        elif self.demux_mode == 4:
            # Setting the number of channels to 1
            self.write(0x31, 0x01)
            self.logger.info('Setting ADC to interleave input (ADC0)')
            # Selecting input 1
            self.write(0x3a, 0x0202)
            self.write(0x3b, 0x0202)
        else:
            self.logger.error('demux_mode variable not assigned. Weird.')
            exit(1)

    def enable_pattern(self, pattern):
        """

        Args:
            pattern (str): select a test pattern (ramp, deskew, sync, none, ...).
                           see list in notes for more details

        Notes
             Selects a test pattern or sampled data for all ADCs selected by
             +chip_select+.  +ptn+ can be any of:

               :ramp            Ramp pattern 0-255
               :deskew (:eye)   Deskew pattern (10101010)
               :sync (:frame)   Sync pattern (11110000)
               :custom1         Custom1 pattern
               :custom2         Custom2 pattern
               :dual            Dual custom pattern
               :none            No pattern (sampled data)

             Default is :ramp.  Any value other than shown above is the same as :none
             (i.e. pass through sampled data)
        """
        self.write(0x25, 0x00)
        self.write(0x45, 0x00)
        if pattern == 'ramp':
            self.write(0x25, 0x0040)
        elif pattern == 'deskew':
            self.write(0x45, 0x0001)
        elif pattern == 'sync':
            self.write(0x45, 0x0002)
        else:
            self.logger.error('Invalid test pattern selected')
            exit(1)
        # else:
        #			self.write_adc(0x25,0x10)
        #			self.write_adc(0x26,(self.expected)<<8)
        time.sleep(1)

    def read_ram(self, device):
        SNAP_REQ = 0x00010000
        self.host.write_int('adc16_controller', 0, offset=1, blindwrite=True)
        self.host.write_int('adc16_controller', SNAP_REQ, offset=1, blindwrite=True)
        # Read the device that is passed to the read_ram method,1024 elements at a time,
        # snapshot is a binary string that needs to get unpacked
        # Part of the read request is the size parameter,1024, which specifies the
        # amount of bytes to read form the device
        snapshot = self.host.read(device, 1024, offset=0)

        # struct unpack returns a tuple of signed int values.
        # Since we're requesting to read adc16_wb_ram at a size of 1024 bytes, we are unpacking
        # 1024 bytes each of which is a signed char(in C, python only knows ints). Unpacking as
        # a signed char is for mapping purposes:

        # ADC returns values from 0 to 255 (since it's an 8 bit ADC), the voltage going into ADC
        # varies from -1V to 1V, we want 0 to mean 0, not -1 volts so we need to remap the output
        # of the ADC to something more sensible, like -128 to 127. That way 0 volts corresponds to
        # a 0 value in the unpacked data.
        string_data = struct.unpack('>1024b', snapshot)
        # Converting the tuple into a vector of 1024 elements
        array_data = np.array(string_data)
        #		for i in range(array_data.shape[0]):
        #			print('{:08b}'.format(array_data[i]))
        # print(array_data)
        return array_data

    def bitslip(self, chip_num, channel):
        """
        The ADC16 controller word (the offset in write_int method) 2 and 3 are for delaying taps of
        A and B lanes, respectively.
        Refer to the memory map word 2 and word 3 for clarification. The memory map was made for a
        ROACH design so it has chips A-H. SNAP 1 design has three chips
        """
        chan_shift = 5
        chan_select_bs = channel << chan_shift
        state = 0
        chip_shift = 8
        chip_select_bs = 1 << chip_shift + chip_num
        state = (chip_select_bs | chan_select_bs)
        #		print('Bitslip state written to offset=1:',bin(state))
        self.host.write_int('adc16_controller', 0, offset=1, blindwrite=True)
        self.host.write_int('adc16_controller', state, offset=1, blindwrite=True)
        #	regvalue=self.read('adc16_controller', 32, offset=1)
        #	print('Bitslip Reg Value\n')
        #	print(struct.unpack('>32b', regvalue))
        self.host.write_int('adc16_controller', 0, offset=1, blindwrite=True)

    def delay_tap(self, tap, channel, chip_num):

        delay_tap_mask = 0x1f

        if channel == 'all':
            chan_select = (0xf << (chip_num * 4))
            self.host.write_int('adc16_controller', 0, offset=2, blindwrite=True)
            self.host.write_int('adc16_controller', 0, offset=3, blindwrite=True)
            # Set tap bits
            self.host.write_int('adc16_controller', delay_tap_mask & tap, offset=1, blindwrite=True)
            # Set strobe bits
            self.host.write_int('adc16_controller', chan_select, offset=2, blindwrite=True)
            self.host.write_int('adc16_controller', chan_select, offset=3, blindwrite=True)
            # Clear all bits
            self.host.write_int('adc16_controller', 0, offset=1, blindwrite=True)
            self.host.write_int('adc16_controller', 0, offset=2, blindwrite=True)
            self.host.write_int('adc16_controller', 0, offset=3, blindwrite=True)
            # Note this return statement, after all channels have been bitslip it'll exit out of the function.
            # the function is called again after figuring out the best tap with a single channel argument.
            return
        elif channel == '1a':
            chan_select = 0x1 << (chip_num * 4)
            lane_offset = 2
        elif channel == '1b':
            chan_select = 0x1 << (chip_num * 4)
            lane_offset = 3
        elif channel == '2a':
            chan_select = 0x2 << (chip_num * 4)
            lane_offset = 2
        elif channel == '2b':
            chan_select = 0x2 << (chip_num * 4)
            lane_offset = 3
        elif channel == '3a':
            chan_select = 0x4 << (chip_num * 4)
            lane_offset = 2
        elif channel == '3b':
            chan_select = 0x4 << (chip_num * 4)
            lane_offset = 3
        elif channel == '4a':
            chan_select = 0x8 << (chip_num * 4)
            lane_offset = 2
        elif channel == '4b':
            chan_select = 0x8 << (chip_num * 4)
            lane_offset = 3

        self.host.write_int('adc16_controller', 0, offset=lane_offset, blindwrite=True)
        # Set tap bits
        self.host.write_int('adc16_controller', delay_tap_mask & tap, offset=1, blindwrite=True)
        # Set strobe bits
        self.host.write_int('adc16_controller', chan_select, offset=lane_offset, blindwrite=True)
        # Clear all bits
        self.host.write_int('adc16_controller', 0, offset=1, blindwrite=True)
        self.host.write_int('adc16_controller', 0, offset=2, blindwrite=True)
        self.host.write_int('adc16_controller', 0, offset=3, blindwrite=True)

    def test_tap(self, chip_num, taps):
        """
        returns an array of error counts for a given tap(assume structure chan 1a, chan 1b, chan 2a, chan 2b etc.. until chan 4b
        taps argument can have a value of an int or a string. If it's a string then it will iterate through all 32 taps
        if it's an int it will only delay all channels by that particular tap value.
        """
        # Form dicts for counting channel erros and offsets
        chan_ids = ['1a', '1b', '2a', '2b', '3a', '3b', '4a', '4b']
        chan_offsets = dict(zip(chan_ids, range(8)))
        zeros    = [0, 0, 0, 0, 0, 0, 0, 0,]
        error_count = []

        if taps == 'all':
            # read_ram reuturns an array of data form a sanpshot from ADC output

            for tap in range(32):
                chan_errs = dict(zip(chan_ids, zeros))
                self.delay_tap(tap, 'all', chip_num)
                data = self.read_ram('adc16_wb_ram{0}'.format(chip_num))
                self.logger.debug("HERE TAPS %s | %s" % (taps, data))
                # each tap will return an error count for each channel and lane, so an array of 8 elements with an error count for each

                for i in range(0, 1024, 8):
                    for chan_id, chan_offset in chan_offsets.items():
                        if data[i + chan_offset] != 0x2a:
                            chan_errs[chan_id] += 1

                error_count.append(
                    [chan_errs['1a'], chan_errs['1b'], chan_errs['2a'], chan_errs['2b'],
                     chan_errs['3a'], chan_errs['3b'], chan_errs['4a'], chan_errs['4b']]
                    )
            return (error_count)
        else:
            chan_errs = dict(zip(chan_ids, zeros))

            # read_ram reuturns an array of data form a sanpshot from ADC output
            self.delay_tap(taps, 'all', chip_num)
            data = self.read_ram('adc16_wb_ram{0}'.format(chip_num))
            # each tap will return an error count for each channel and lane, so an array of 8 elements with an error count for each
            self.logger.debug("HERE TAPS %s | %s" % (taps, data))
            for i in range(0, 1024, 8):
                for chan_id, chan_offset in chan_offsets.items():
                    if data[i + chan_offset] != 0x2a:
                        chan_errs[chan_id] += 1

            error_count.append(
                [chan_errs['1a'], chan_errs['1b'], chan_errs['2a'], chan_errs['2b'],
                 chan_errs['3a'], chan_errs['3b'], chan_errs['4a'], chan_errs['4b']]
                )
            self.logger.debug('Chip {0} Error count for {1} tap: {2}'.format(chip_num, taps, error_count))
            return (error_count)

    def walk_taps(self):
        # Set FPGA to demux 4 because it makes snap blocks easier to interpret
        self.host.fpga_set_demux(4)

        for chip, chip_num in self.chips.iteritems():
            # Set demux 4 on the FPGA side (just rearranging outputs as opposed to dividing clock and assigning channels)

            self.logger.info('Calibrating chip %s...' % chip)
            self.logger.debug('Setting deskew pattern...')
            #self.logger.debug('Stuff in chip %s before enabling pattern' % chip)
            #self.logger.debug(self.read_ram('adc16_wb_ram{0}'.format(chip_num)))
            self.enable_pattern('deskew')
            self.logger.debug('Stuff in chip after enabling test mode\n')
            self.logger.debug(self.read_ram('adc16_wb_ram{0}'.format(chip_num)))

            self.logger.debug('Taps before bitslipping anything\n')
            self.logger.debug(self.test_tap(chip_num, 'all'))
            # check if either of the extreme tap setting returns zero errors in any one of the channels. Bitslip if True.
            # This is to make sure that the eye of the pattern is swept completely
            error_counts_0 = self.test_tap(chip_num, 0)
            error_counts_31 = self.test_tap(chip_num, 31)
            for i in range(8):
                if not (error_counts_0[0][i]) or not (error_counts_31[0][i]):
                    self.logger.debug('Bitslipping chan %i' % i)
                    self.bitslip(chip_num, i)
                    error_counts_0 = self.test_tap(chip_num, 0)
                    error_counts_31 = self.test_tap(chip_num, 31)

            # error_list is a list of 32 'rows'(corresponding to the 32 taps) , each row containing 8 elements,each element is the number of errors
            # of that lane  when compared to the expected value. read_ram method unpacks 1024 bytes. There are 8
            # lanes so each lane gets 1024/8=128 read outs from a single call to read_ram method, like this, channel_1a etc. represent the errors in that channel
            # tap 0: [ channel_1a channel_1b channel_2a channel_2b channel_3a channel_3b channel_4a channel_4b]
            # tap 1: [ channel_1a channel_1b channel_2a channel_2b channel_3a channel_3b channel_4a channel_4b]
            # .....: [ channel_1a channel_1b channel_2a channel_2b channel_3a channel_3b channel_4a channel_4b]
            # tap 31:[ channel_1a channel_1b channel_2a channel_2b channel_3a channel_3b channel_4a channel_4b]
            error_list = self.test_tap(chip_num, 'all')
            good_tap_range = []
            best_tap_range = []
            self.logger.debug('Printing the list of errors, each row is a tap\n')
            self.logger.debug(['chan1a', 'chan1b', 'chan2a', 'chan2b', 'chan3a', 'chan3b', 'chan4a', 'chan4b'])
            self.logger.debug(np.array(error_list))
            min_tap = []
            max_tap = []
            # This loop goes through error_list, finds the elements with a value of 0 and appends them to the good tap range list
            # It also picks out the elements corresponding to different channels and groups them together. The error_list is a list where each 'row' is a different tap
            # I wanted to find the elements in each channel that have zero errors, group the individual channels, and get the value of the tap in which they're in - which is the index of the row
            for i in range(8):
                good_tap_range.append([])
                # j represents the tap value
                for j in range(32):
                    # i represents the channel/lane value
                    if error_list[j][i] == 0:
                        good_tap_range[i].append(j)
                        #	find the min and max of each element of good tap range and call delay tap
            self.logger.debug('Printing good tap values for each channel...each row corresponds to different channel')

            for i in range(len(good_tap_range)):
                logging.debug('Channel {0}: {1}'.format(i + 1, good_tap_range[i]))

            channels = ['1a', '1b', '2a', '2b', '3a', '3b', '4a', '4b']
            for k in range(8):
                min_tap = min(good_tap_range[k])
                max_tap = max(good_tap_range[k])

                best_tap = (min_tap + max_tap) // 2
                self.delay_tap(best_tap, channels[k], chip_num)
            self.logger.debug('Printing the calibrated data from ram{0}.....'.format(self.chips[chip]))
            self.logger.debug(self.read_ram('adc16_wb_ram{0}'.format(self.chips[chip])))

            # Bitslip channels until the sync pattern is captured
            self.sync_chips(chip_num)

        # Set FPGA back to acutal demux mode
        self.host.fpga_set_demux(self.demux_mode)

    def sync_chips(self, chip_num):
        """ Synchronize chips with bitslip """
        # channels = {0:'1a',1:'1b',2:'2a',3:'2b',4:'3a',5:'3b',6:'4a',7:'4b'}
        self.enable_pattern('sync')

        snap = self.read_ram('adc16_wb_ram{0}'.format(chip_num))
        self.logger.debug('Snapshot before bitslipping:\n')
        self.logger.debug(snap[0:8])

        for i in range(8):
            loop_ctl = 0
            while snap[i] != 0x70:
                self.logger.debug('Bitsliping channel %i\n' % i)
                self.bitslip(chip_num, i)
                snap = self.read_ram('adc16_wb_ram{0}'.format(chip_num))
                self.logger.debug('Snapshot after bitslipping:\n')
                self.logger.debug(snap[0:8])
                loop_ctl += 1
                if loop_ctl > 10:
                    self.logger.error(
                        "It appears that bitslipping is not working, make sure you're using the version of Jasper library")
                    exit(1)

    def clock_locked(self):
        """ Check if CLK is locked """
        locked_bit = self.host.read_int('adc16_controller', offset=0) >> 24
        if locked_bit & 3:
            self.logger.info('ADC clock is locked!!!')
            self.logger.info('Board clock: %2.4f MHz' % self.host.est_brd_clk())
        else:
            err = 'ADC clock not locked, check your clock source/correctly set demux mode'
            self.logger.error('ADC clock not locked, check your clock source/correctly set demux mode')
            raise RuntimeError('ADC clock not locked, check your clock source/correctly set demux mode')

    def clear_pattern(self):
        """Clears test pattern from ADCs"""
        self.write(0x25, 0x00)
        self.write(0x45, 0x00)

    def set_gain(self, gain):
        """ Set gain value on ADCs"""
        self.gain = gain
        if self.demux_mode == 1:
            self.write(0x2a, self.gain * 0x1111)
        elif self.demux_mode == 2:
            self.write(0x2b, self.gain * 0x0011)
        elif self.demux_mode == 4:
            self.write(0x2b, self.gain * 0x0100)
        else:
            self.logger.error('demux mode is not set')
            exit(1)

    def calibrate(self):
        """" Run calibration routines """
        #self.initialize()
        # check if clock is locked
        self.clock_locked()
        # Calibrate ADC by going through various tap values
        self.walk_taps()
        # Clear pattern setting registers so real data could be taken
        self.clear_pattern()
        self.logger.info('Setting fpga demux to %i' % self.demux_mode)
        self.host.fpga_set_demux(self.demux_mode)

class SnapBoard(katcp_wrapper.FpgaClient):
    """ Controller for a CASPER SNAP board.

    Provides monitor and control of a CASPER SNAP board
    """
    def __init__(self, hostname, katcp_port=7147, timeout=10,
                 verbose=False, **kwargs):
        super(SnapBoard, self).__init__(hostname, katcp_port, timeout)
        self.katcp_port = katcp_port

        if verbose == True:
            logging.basicConfig(level=logging.DEBUG)
        else:
            logging.basicConfig(level=logging.INFO)

        # Wait up to timeout to see if ADC is connected
        t0 = time.time()
        while not self.is_connected():
            time.sleep(1e-3)
            if time.time() - t0 > timeout:
                break

        # Check if design has an ADC controller; if so, attach controller as self.adc
        self.adc = None
        if self.is_connected():
            try:
                if self.is_adc16_based():
                    self.adc = SnapAdc(self)
            except RuntimeError:
                pass

        self.logger = logging.getLogger('SnapBoard')

    def __repr__(self):

        return "<SnapBoard host: %s port: %s>" % (self.host, self.katcp_port)

    def program(self, boffile, gain=1, demux_mode=1, chips=('a', 'b', 'c')):
        """ Reprogram the FPGA with a given boffile AND calibrates

        Adds gain, demux_mode and chips params to katcp_wrapper's progdev

        Args:
            boffile (str): Name of boffile to program
            gain (int): ADC gain, from 1-32 (1, 2, 4, 8 recommended)

        """
        # Make a dictionary out of chips specified on command line.
        # mapping chip letters to numbers to facilitate writing to adc16_controller
        self.logger.info("Programming with %s" % boffile)
        self.progdev(boffile)

        if self.is_adc16_based():
            self.logger.info("Design is ADC16 based. Calibration routines will run.")
            self.adc = SnapAdc(self)
            self.adc.set_chip_select(chips)
            self.adc.set_demux(demux_mode)
            self.adc.set_gain(gain)
            self.adc.calibrate()

    def is_adc16_based(self):
        """ Check if design uses ADC16 chip """
        try:
            if 'adc16_controller' in self.listdev():
                return True
            else:
                return False
        except RuntimeError:
            return False

    def fpga_set_demux(self, fpga_demux):
        """ Set demux on FPGA

        Notes:
            Setting fpga demux rearranges the bits before they're output from the adc block depending on
            the adc demux mode used.
            State is assigned according to the adc16_controller memory map. (4+n) shifted by the amount of bits
            that precede the WMM field.
            4 always activates the W bit to allow writing to the MM bits and n is determined by the adc demux mode used
        """

        demux_shift = 24

        if fpga_demux == 1:
            state = (4 + 0) << demux_shift
            self.write_int('adc16_controller', state, offset=1, blindwrite=True)
        elif fpga_demux == 2:
            # writing the WW enable bit(4) as well as the demux setting bit(1 for demux mode 2
            # as seen in the adc16_controller memory map)
            state = (4 + 1) << demux_shift
            self.write_int('adc16_controller', state, offset=1, blindwrite=True)

        elif fpga_demux == 4:
            state = (4 + 2) << demux_shift
            self.write_int('adc16_controller', state, offset=1, blindwrite=True)
        else:
            self.logger.error('Invalid or no demux mode specified')
            exit(1)


# -*- coding: utf-8 -*-
import logging
import logging.handlers
import os
import time
import threading

#logging.basicConfig()

logger = 0
is_debug = False
lock = threading.Lock()

def show(log, flag=False):
	if flag or False:
		lock.acquire()
		print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),log)
		lock.release()

def init(name, dirpath="log"):
	global logger
	try:
		name = "%s.log"%name
		if os.path.exists(dirpath) == False:
			os.makedirs(dirpath)
		log_name = dirpath + "/" + name
		logger = logging.getLogger('[%s]'%name)
		handler = logging.handlers.RotatingFileHandler(log_name,maxBytes=20*1024*1024,backupCount=10)
		# handler = logging.handlers.TimedRotatingFileHandler(log_name,'D',1,30)
		# handler.suffix = "%Y-%m-%d.log"
		# handler.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}.log$")
		#formatter = logging.Formatter('%(asctime)s %(name)s[%(levelname)-7s]:%(message)s')
		formatter = logging.Formatter('%(asctime)s [%(levelname)-7s]:%(message)s')
		handler.setFormatter(formatter)
		logger.addHandler(handler)
		# logger.setLevel(logging.INFO)
		logger.setLevel(logging.DEBUG)
	except Exception as ex:
		print('log init error %s:%s'%(Exception,ex))

def log(level, log):
	#lock.acquire()
	try:
		show(log)
		logger.log(level,log)
	except Exception as ex:
		print('log log %s:%s'%(Exception,ex))
	#lock.release()

def info(log):
	#lock.acquire()
	try:
		show(log)
		logger.info(log)
	except Exception as ex:
		print('log info %s:%s'%(Exception,ex))
	#lock.release()

def warning(log):
	#lock.acquire()
	try:
		show(log)
		logger.warning(log)
	except Exception as ex:
		print('log warning %s:%s'%(Exception,ex))
	#lock.release()

def error(log):
	#lock.acquire()
	try:
		show(log)
		logger.error(log)
	except Exception as ex:
		print('log error %s:%s'%(Exception,ex))
	#lock.release()

def debug(log):
	global is_debug
	if False == is_debug:
		return
	#lock.acquire()
	try:
		show(log)
		logger.debug(log)
	except Exception as ex:
		print('log error %s:%s'%(Exception,ex))
	#lock.release()

class logDS:
	def POST(self):
		data = web.input()
		global is_debug
		response = 'no debug...'
		if data['debug'] == "1":
			is_debug = True
			response = 'debug...'
		else:
			is_debug = False
		return response

	def GET(self):
		response = 'no debug...'
		global is_debug
		if is_debug:
			response = 'debug...'
		return response

if __name__ == "__main__":
	print('__main__ begin....')
	init('logDS')
	log(logging.DEBUG, 'test log')
	log(logging.INFO, 'test log')
	log(logging.WARNING, 'test log')
	log(logging.ERROR, 'test log')
	info('test info')
	warning('test warning')
	error('test error')
	debug('test debug 00')
	#global is_debug
	is_debug = True
	debug('test debug 11')
	is_debug = False
	debug('test debug 22')
	print('__main__ end....')


"""Author: Brandon Trabucco, Copyright 2019"""

import tensorflow as tf

from mineral.core.monitors.local_monitor import LocalMonitor

from mineral.networks.dense import Dense
from mineral.distributions.gaussians.tanh_gaussian import TanhGaussian

from mineral.core.envs.normalized_env import NormalizedEnv
from mineral.core.envs.debug.pointmass_env import PointmassEnv

from mineral.core.buffers.path_buffer import PathBuffer
from mineral.core.buffers.off_policy_buffer import OffPolicyBuffer
from mineral.core.samplers.parallel_sampler import ParallelSampler

if __name__ == "__main__":

    variant = dict(
        experiment_id=0,
        max_path_length=3,
        max_size=2,
        num_warm_up_paths=2,
        num_exploration_paths=0,
        num_evaluation_paths=0,
        num_trains_per_step=1,
        update_tuner_every=1,
        update_actor_every=1,
        batch_size=3,
        num_threads=2,
        num_steps=1)

    for gpu in tf.config.experimental.list_physical_devices('GPU'):
        tf.config.experimental.set_memory_growth(gpu, True)

    experiment_id = variant["experiment_id"]
    logging_dir = "./test/sac/{}".format(
        experiment_id)

    max_path_length = variant["max_path_length"]
    max_size = variant["max_size"]

    num_warm_up_paths = variant["num_warm_up_paths"]
    num_exploration_paths = variant["num_exploration_paths"]
    num_evaluation_paths = variant["num_evaluation_paths"]
    num_trains_per_step = variant["num_trains_per_step"]

    update_tuner_every = variant["update_tuner_every"]
    update_actor_every = variant["update_actor_every"]

    batch_size = variant["batch_size"]
    num_steps = variant["num_steps"]

    monitor = LocalMonitor(logging_dir)

    env = NormalizedEnv(
        PointmassEnv, size=2, ord=2)

    policy = Dense(
        [256, 256, 4],
        tau=1e-1,
        optimizer_class=tf.keras.optimizers.Adam,
        optimizer_kwargs=dict(lr=0.0001),
        distribution_class=TanhGaussian,
        distribution_kwargs=dict(std=None))

    buffer = PathBuffer(
        max_size=variant["max_size"],
        max_path_length=variant["max_path_length"],
        selector=(lambda x: x["proprio_observation"]),
        monitor=monitor)

    step_buffer = OffPolicyBuffer(buffer)

    sampler = ParallelSampler(
        env, policy, buffer,
        max_path_length=variant["max_path_length"],
        num_warm_up_paths=variant["num_warm_up_paths"],
        num_exploration_paths=variant["num_exploration_paths"],
        num_evaluation_paths=variant["num_evaluation_paths"],
        num_threads=variant["num_threads"],
        selector=(lambda i, x: x["proprio_observation"]),
        monitor=monitor)

    sampler.warm_up()
    sampler.explore()
    sampler.evaluate()
    print("DONE: {}".format(sampler.num_steps_collected))

    step_buffer = OffPolicyBuffer(buffer)
    batch = step_buffer.sample(32)
    print(batch)

    import ipdb; ipdb.set_trace()


from unittest import TestCase
from boris.forecast import WeatherForecaster, Peak, Weather
from boris.planets import SolarSystem
from mock import Mock
from hamcrest import assert_that, is_, has_length, has_properties, calling, \
    raises
from dateutil.relativedelta import relativedelta

class WeatherForecasterTest(TestCase):

    def setUp(self):
        self.solar_system = SolarSystem()
        self.forecaster = WeatherForecaster(self.solar_system)

    def test_it_forecasts_drought_if_planets_are_aligned_with_sun(self):
        self._mock_solar_system_state(aligned_with_sun=True)
        assert_that(self.forecaster.current_weather(), is_(Weather.DROUGHT))

    def test_it_forecasts_optimal_conditions_if_planets_are_aligned(self):
        self._mock_solar_system_state(aligned=True)
        assert_that(self.forecaster.current_weather(), is_(Weather.OPTIMAL))

    def test_it_forecasts_rain_if_planets_polygon_contains_sun(self):
        self._mock_solar_system_state(contains_sun=True)
        assert_that(self.forecaster.current_weather(), is_(Weather.RAINY))

    def test_it_forecasts_regular_wetaher_otherwise(self):
        self._mock_solar_system_state(
            aligned_with_sun=False, aligned=False,  contains_sun=False)
        assert_that(self.forecaster.current_weather(), is_(Weather.REGULAR))

    def test_it_generates_a_forecast_for_a_given_time_span(self):
        self.forecaster.current_weather = Mock()
        self.forecaster.current_weather.side_effect = [
            Weather.DROUGHT, Weather.RAINY, Weather.RAINY]
        self.solar_system.planets_perimeter = Mock()
        self.solar_system.planets_perimeter.side_effect = [100, 200]
        forecast = self.forecaster.forecast_for(relativedelta(days=3))
        assert_that(forecast.periods, has_length(2))
        assert_that(forecast.periods[0], has_properties({
            'weather': Weather.DROUGHT, 'start_day': 0,
            'end_day': 0, 'peak': None
        }))
        assert_that(forecast.periods[1], has_properties({
            'weather': Weather.RAINY, 'start_day': 1,
            'end_day': 2, 'peak': Peak(2, 200)
        }))

    def test_it_fails_if_period_is_less_than_a_day(self):
        assert_that(calling(self.forecaster.forecast_for)
                    .with_args(relativedelta(hours=23)),
                    raises(ValueError, 'Invalid period.*'))

    def _mock_solar_system_state(self, aligned_with_sun=False, aligned=False,
                                 contains_sun=False):
        self.solar_system.are_planets_aligned_with_sun = \
            Mock(return_value=aligned_with_sun)
        self.solar_system.are_planets_aligned = \
            Mock(return_value=aligned)
        self.solar_system.does_planets_polygon_contain_sun = \
            Mock(return_value=contains_sun)


import asyncio
import requests
import json
from functools import partial

class LastfmPlugin:
    def __init__(self, key, timeout=10, response_format='json', loop=None, notify=None):
        self._loop = loop or asyncio.get_event_loop()
        self.key = key
        self.url = r'http://ws.audioscrobbler.com/2.0/'
        self.format = response_format
        self.default_timeout = timeout
        self.session = requests.Session()
        self.session.headers['Accept'] = 'application/json'

    def __getattr__(self, item):
        return AsyncRequest(self, [item])

    @asyncio.coroutine
    def send(self, method, **kwargs):
        params = {
            'method': method,
            'api_key': self.key,
            'format': self.format
        }
        params.update(kwargs)
        response = yield from self._loop.run_in_executor(
            None,
            partial(self.session.post, timeout=self.default_timeout),
            self.url,
            params
        )
        return json.loads(response.text)

class AsyncRequest:
    def __init__(self, sender, chain):
        self._sender = sender
        self.chain = chain

    def __getattr__(self, item):
        return AsyncRequest(self._sender, self.chain+[item])

    @asyncio.coroutine
    def __call__(self, *args, **kwargs):
        return (yield from self._sender.send('.'.join(self.chain), **kwargs))


#!/bin/env python
"""Add prefix to entities of a specified file.blend """
#import asyncio
import bpy
#set var BASENAME = to the 'basename' of the 'filepath'
BASENAME = bpy.path.basename(bpy.data.filepath)
#set var NAME = BASENAME cuted the last six characters
NAME = (BASENAME[:-6])
# async def rename_from_file():
# """An attempt to multiprocess/parallelism/idk"""
for objects in bpy.data.objects:
    objects.name = NAME + '_' + objects.name
for meshes in bpy.data.meshes:
    meshes.name = NAME + '_' + meshes.name
for brushes in bpy.data.brushes:
    brushes.name = NAME + '_' + brushes.name
for armatures in bpy.data.armatures:
    armatures.name = NAME + '_' + armatures.name
for materials in bpy.data.materials:
    materials.name = NAME + '_' + materials.name
for collections in bpy.data.collections:
    collections.name = NAME + '_' + collections.name
for cameras in bpy.data.cameras:
    cameras.name = NAME + '_' + cameras.name
for scenes in bpy.data.scenes:
    scenes.name = NAME + '_' + scenes.name
for node_groups in bpy.data.node_groups:
    node_groups.name = NAME + '_' + node_groups.name
for linestyles in bpy.data.linestyles:
    linestyles.name = NAME + '_' + linestyles.name
for worlds in bpy.data.worlds:
    worlds.name = NAME + '_' + worlds.name
for textures in bpy.data.textures:
    textures.name = NAME + '_' + textures.name
for actions in bpy.data.actions:
    actions.name = NAME + '_' + actions.name
for images in bpy.data.images:
    images.name = NAME + '_' + images.name
for palettes in bpy.data.palettes:
    palettes.name = NAME + '_' + palettes.name
for texts in bpy.data.texts:
    texts.name = NAME + '_' + texts.name
#asyncio.run(rename_from_file())
bpy.ops.wm.save_mainfile()


from xai.brain.wordbase.adjectives._solitary import _SOLITARY

#calss header
class _SOLITARIES(_SOLITARY, ):
	def __init__(self,):
		_SOLITARY.__init__(self)
		self.name = "SOLITARIES"
		self.specie = 'adjectives'
		self.basic = "solitary"
		self.jsondata = {}


# -*- encoding: utf-8 -*-
"""
Copyright (c) 2019 - present AppSeed.us
"""

from django.urls import include, path, re_path
from . import views
from rest_framework import routers

urlpatterns = [

    # The home page
    path('', views.index, name='home'),

    # Matches any html file
    # re_path(r'^.*\.*', views.pages, name='pages'),
    path('loadinvoice/', views.loadInvoice),
    path('loadvendor/', views.loadVendor),
    path('remittance/', views.remittance),
    path('vendor/', views.vendors),
    path('invoice/', views.invoices ),
    path('email/', views.sendEmailBatchJob ),
]


# -*- coding: utf-8 -*-
"""
__title__="train"
__author__="ngc7293"
__mtime__="2020/9/20"
"""
from data_load import get_dataloader
from model import TextCnn, TextRnn
import numpy as np
import pandas as pd

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import optim

if __name__ == '__main__':
    lr = 0.001
    epoches = 50
    class_num = 5
    batch_size = 32
    sentence_len = 40
    embedding_dim = 100

    device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')

    train_loader, test_loader, embed_pretrain_glove, vocab_size = get_dataloader(sentence_len=sentence_len, pre_model="glove")

    _, _, embed_pretrain_word2vec, _ = get_dataloader(sentence_len=sentence_len, pre_model="word2vec")

    rnn_model_with_glove = TextRnn(vocab_size=vocab_size, embedding_dim=embedding_dim, class_num=5, hidden_size=128,
                                   embedding_pretrained=embed_pretrain_glove, device=device)

    cnn_model_with_glove = TextCnn(vocab_size=vocab_size, embedding_dim=embedding_dim, hidden_size=128, class_num=class_num,
                                   embedding_pretrained=embed_pretrain_glove)

    rnn_model_without = TextRnn(vocab_size=vocab_size, embedding_dim=embedding_dim, class_num=5, hidden_size=128,
                                embedding_pretrained=None, device=device)

    cnn_model_without = TextCnn(vocab_size=vocab_size, embedding_dim=embedding_dim, hidden_size=128, class_num=class_num,
                                embedding_pretrained=None)

    rnn_model_with_word2vec = TextRnn(vocab_size=vocab_size, embedding_dim=embedding_dim, class_num=5, hidden_size=128,
                                embedding_pretrained=embed_pretrain_word2vec, device=device)

    cnn_model_with_word2vec =  TextCnn(vocab_size=vocab_size, embedding_dim=embedding_dim, hidden_size=128, class_num=class_num,
                                embedding_pretrained=embed_pretrain_word2vec)

    model_list = [rnn_model_with_glove,
                  rnn_model_without,
                  rnn_model_with_word2vec,
                  cnn_model_with_glove,
                  cnn_model_without,
                  cnn_model_with_word2vec]

    model_type_list = [
        "rnn with glove",
        "rnn with random wmbedding",
        "rnn with word2vec",
        "cnn with glove",
        "cnn with random embedding",
        "cnn with word2vec"
    ]
    # model_list = [rnn_model_with_word2vec]

    loss_history_list = []
    train_acc_list = []
    test_acc_list = []

    for model,model_type in zip(model_list,model_type_list):

        # if model_type == "cnn": model = TextCnn(vocab_size=vocab_size, embedding_dim=50, hidden_size=128,
        # class_num=class_num,  embedding_pretrained=embed_pretrain)

        print("traing %s model ..."%model_type)
        optimizer = optim.Adam(model.parameters(), lr=lr)
        loss_function = nn.CrossEntropyLoss()

        loss_history = []
        train_acc_history = []
        test_acc_history = []

        model.to(device)
        for epoch in range(epoches):
            model.train()
            losses = []
            count = 0
            for idx, (x, y) in enumerate(train_loader):
                x, y = x.to(device), y.to(device)
                # if len(x) < batch_size:
                #     break
                pred = model(x)
                optimizer.zero_grad()
                loss = loss_function(pred, y)
                loss.backward()
                losses.append(loss.item())
                optimizer.step()

            train_loss = np.mean(losses)
            loss_history.append(train_loss)

            # model.eval() will notify all your layers that you are in eval mode, that way, batchnorm or dropout
            # layers will work in eval mode instead of training mode. torch.no_grad() impacts the autograd engine and
            # deactivate it. It will reduce memory usage and speed up computations but you won’t be able to backprop
            # (which you don’t want in an eval script).

            model.eval()

            train_acces = []

            for idx, (x, y) in enumerate(train_loader):
                x = x.to(device)
                y = y.numpy()
                y_pred = np.argmax(model(x).cpu().detach().numpy(), 1)

                acc = np.mean(y_pred == y, dtype=np.float)
                train_acces.append(acc)
            train_acc = np.mean(train_acces)
            train_acc_history.append(train_acces)

            test_acces = []
            for idx, (x, y) in enumerate(test_loader):
                x = x.to(device)
                y = y.numpy()
                y_pred = np.argmax(model(x).cpu().detach().numpy(), 1)

                acc = np.mean(y_pred == y, dtype=np.float)
                test_acces.append(acc)

            test_acc = np.mean(test_acces)
            test_acc_history.append(test_acces)

            print("epoch %d train loss:%.6f,train acc:%.6f test acc:%.7f" % (
            epoch, float(train_loss), float(train_acc), float(test_acc)))

        loss_history_list.append(loss_history)
        train_acc_list.append(train_acc_history)
        test_acc_list.append(test_acc_history)

    pd.DataFrame(loss_history_list).to_csv("./result/loss_history_100d.csv")
    pd.DataFrame(train_acc_list).to_csv("./result/acc_history_100d.csv")
    pd.DataFrame(test_acc_list).to_csv("./result/test_acc_history_100d.csv")


from sklearn.metrics import accuracy_score, precision_score, recall_score, roc_auc_score, f1_score, log_loss
from sklearn.metrics import mean_squared_error,r2_score
from sklearn.metrics import confusion_matrix, classification_report
import numpy as np

# https://scikit-learn.org/stable/modules/model_evaluation.html

def clf_metrics(y, y_pred):
    """
    caculate classification machines performance

    Args:
    ----------
        y : y actual
        y_pred :  y predict
    Return:
    ----------
        d : Train varable
    """
    d={}
    d["accuracy"] = round(accuracy_score(y, y_pred),4)
    d["precision"] = round(precision_score(y, y_pred),4)
    d["recall"] = round(recall_score(y, y_pred),4)
    d["f1"] = round(f1_score(y, y_pred),4)
    d["AUC"] = round(roc_auc_score(y, y_pred),4)
    d["log_loss"] = round(log_loss(y, y_pred),4)
    return d

def reg_metrics(y, y_pred):
    """
    caculate regression machines performance

    Args:
    ----------
        y : y actual
        y_pred :  y predict
    Return:
    ----------
        d : Train varable
    """
    d={}
    d["MSE"] = round(mean_squared_error(y,y_pred),4)
    d["RMSE"] = round(np.sqrt(mean_squared_error(y, y_pred)),4)
    d["R2"] = round(r2_score(y,y_pred),4)
    return d

def clf_report(y, y_pred):
    """
    caculate classification machines report

    Args:
    ----------
        y : y actual
        y_pred :  y predict
    Return:
    ----------
        confusionmatrix : confusion matrix
        report : some metrics report
    """
    confusionmatrix = confusion_matrix(y,y_pred)
    report = classification_report(y, y_pred)
    print("Confusion Matrix")
    print(confusionmatrix)
    print()
    print("Classification Report")
    print(report)
    #return confusionmatrix ,report


import numpy as np
import logging
import sys
import heapq
import queue

from enum import IntEnum

import request_generator

requests_queue = queue.Queue()
number_of_empty_instances = 0
number_of_created_instances = 0
must_be_terminated = 0
sla_penalty_cost = []
instances = []
last_number_of_created_instances = 0
total_inst_cost = 0
alpha = 0.3

INSTANTIATION_TIME = 0

class EventTypes(IntEnum):
    arrival     = 0 # new demand arrival, it will be enqueued
    dequeue     = 1 # a server dequeues a request
    finish      = 2 # processing the service is finished
    instantiate = 3 # processing the service is finished
    terminate   = 4 # processing the service is finished
    monitor     = 5
    report      = 6

class Event:
    def __init__(self, time, event_type, processor, data):
        self.time = time
        self.event_type = event_type
        self.processor = processor
        self.data = data

    def __lt__(self, other):
        return self.time < other.time

def add_event(events, event):
    if len(events) > 0:
        heapq.heappush(events, event)
    else:
        events.append(event)
        heapq.heapify(events)

def print_queue(requests_queue):
    while not requests_queue.empty():
        req = requests_queue.get()
        print("req = ", req)

def reporter(prefix, time):
    global last_number_of_created_instances
    global total_inst_cost
    instantation_cost = 0
    if number_of_created_instances > last_number_of_created_instances:
        instantation_cost = (number_of_created_instances - last_number_of_created_instances) * INSTANTIATION_COST

    usage_cost = 0
    for slt in instances:
        if slt.terminatation_time == None:
            usage_cost += MONITORING_INTERVAL * INSTANTCE_USAGE_PER_TIMER

    total_inst_cost += instantation_cost + usage_cost
    sla_cost = sum(sla_penalty_cost)
    logging.debug("%s: t = %s, inst = %d empt = %d delay = %f inst_cost = %f sla_cost = %f", prefix, time, number_of_created_instances, number_of_empty_instances, last_processing_time, total_inst_cost, sla_cost)
    last_number_of_created_instances = number_of_created_instances

def arrival_event_processor(current_time, request, events):
    logging.debug("arrival_event_processor: time = %f, req = %s", current_time, request)
    requests_queue.put(request)

    if number_of_created_instances == 0:
        for _ in range(SMALL_IL):
            instantiate_event_processor(current_time, None, events)
    elif number_of_empty_instances > 0:
        dequeue_event_creator(current_time, events)

def arrival_event_creator(request, events):
    event = Event(request.arrival_time, EventTypes.arrival, arrival_event_processor, request)
    add_event(events, event)

def dequeue_event_processor(current_time, dummy, events):
    global number_of_empty_instances
    if requests_queue.empty():
        logging.debug("Queue is empty")
    elif number_of_empty_instances > 0:
        number_of_empty_instances -= 1

        request = requests_queue.get()
        logging.debug("dequeue_event_processor: time = %f, req = %s", current_time, request)

        finish_event_creator(current_time, request, events)

def dequeue_event_creator(time, events):
    logging.debug("dequeue_event_creator, time = %s", time)
    event = Event(time, EventTypes.dequeue, dequeue_event_processor, None)
    add_event(events, event)

def update_processing_time(time):
    global last_processing_time
    last_processing_time = time
    global processing_time
    processing_time = alpha * time + (1 - alpha) * processing_time

def sla_penalty(current_time, request):
    time = current_time - request.arrival_time
    update_processing_time(time)
    return request.service_type.sla_penalty(time)

def finish_event_processor(current_time, request, events):
    logging.debug("finish_event_processor: time = %f, request = %s", current_time, request)
    penalty = sla_penalty(current_time, request)
    logging.debug("finish_event_processor: SLA penalty = %f", penalty)
    sla_penalty_cost.append(penalty)

    global last_sla_violation_cost
    last_sla_violation_cost += penalty

    global number_of_empty_instances
    number_of_empty_instances += 1

    global must_be_terminated
    if must_be_terminated > 0:
        termination_event_processor(current_time, None, events)
    else:
        dequeue_event_creator(current_time, events)

def finish_event_creator(current_time, request, events):
    logging.debug("finish_event_creator: time = %s", current_time)
    time = current_time + request.holding_time
    event = Event(time, EventTypes.finish, finish_event_processor, request)
    add_event(events, event)

class InstanceLifeTime:
    def __init__(self):
        self.instantiation_time = None
        self.terminatation_time = None

def get_instantiation_time():
    return INSTANTIATION_TIME

def instantiate_event_processor(current_time, dummy, events):
    logging.debug("instantiate_event_processor: time = %s", current_time)
    slt = InstanceLifeTime()
    slt.instantiation_time = max(current_time, 0)
    instances.append(slt)

    global number_of_created_instances
    number_of_created_instances += 1
    global number_of_empty_instances
    number_of_empty_instances += 1

    dequeue_event_creator(current_time, events)

def instantiate_event_creator(current_time, events):
    logging.debug("instantiate_event_creator: time = %s", current_time)
    time = current_time + get_instantiation_time()
    event = Event(time, EventTypes.instantiate, instantiate_event_processor, None)
    add_event(events, event)

def termination_event_processor(current_time, dummy, events):
    logging.debug("termination_event_processor: time = %s", current_time)

    for slt in instances:
        if slt.terminatation_time == None:
            slt.terminatation_time = current_time

            global number_of_empty_instances
            number_of_empty_instances -= 1
            global number_of_created_instances
            number_of_created_instances -= 1
            global must_be_terminated
            must_be_terminated -= 1

            return

    logging.error("termination_event_processor: cannot find an instance to terminate")
    sys.exit(-1)

def termination_event_creator(current_time, events):
    logging.debug("termination_event_creator: time = %s", current_time)
    event = Event(current_time, EventTypes.terminate, termination_event_processor, None)
    add_event(events, event)

class MonitoringDataQLen:
    def __init__(self,  queue_len):
        self.queue_len = queue_len

'''
QLEN_HIGH_THRESHOLD = 5
QLEN_LOW_THRESHOLD = 3
def monitor_event_processor(current_time, interval, events):
    monitoring_data = MonitoringDataQLen(requests_queue.qsize())
    logging.debug("monitor_event_processor: time = %s, len = %d", current_time, monitoring_data.queue_len)
    if monitoring_data.queue_len > QLEN_HIGH_THRESHOLD:
        instantiate_event_creator(current_time, events)
    elif monitoring_data.queue_len < QLEN_LOW_THRESHOLD:
        if number_of_created_instances > 1: # FIXME or monitoring_data.queue_len == 0:
            global must_be_terminated
            must_be_terminated += 1

    if len(events) > 0:
        monitor_event_creator(current_time, interval, events)
'''

################## AIML ##########################

#Automative
#LOW_THRESHOLD = 3.710098751
#HIGH_THRESHOLD = 4.753636043

#Bird eye
LOW_THRESHOLD = 14.75434705
HIGH_THRESHOLD = 26.15610977

SMALL_IL = 2
BIG_IL = 5
NO_CHANGE_IL = 0

def infer_aiml(time):
    if time <= LOW_THRESHOLD:
        return SMALL_IL
    elif time <= HIGH_THRESHOLD:
        return NO_CHANGE_IL
    else:
        return BIG_IL

processing_time = 0
last_processing_time = 0
start_scaling_interval_small = 0
start_scaling_interval_big = 0
last_sla_violation_cost = 0
interval_sla_violation_cost = 0
interval_usage_cost = 0
SCALE_UP_THRESHOLD = 0.0015
SCALE_DOWN_THRESHOLD = 0.10
MONITORING_INTERVAL = 0

def monitor_event_processor(current_time, data, events):
    reporter(data["prefix"], current_time)
    target_il = infer_aiml(processing_time)

    logging.debug("monitor_event_processor: time = %s, target_il = %d", current_time, target_il)

    global start_scaling_interval_small
    global start_scaling_interval_big
    global interval_sla_violation_cost
    global interval_usage_cost
    global last_sla_violation_cost

    if target_il == SMALL_IL:
        logging.debug("target_il == SMALL_IL")
        if start_scaling_interval_small == 0:
            logging.debug("restart start_scaling_interval_small")
            start_scaling_interval_small = 1
            interval_usage_cost = (BIG_IL - SMALL_IL) * INSTANTCE_USAGE_PER_TIMER * MONITORING_INTERVAL
        else:
            print("continue start_scaling_interval_small")
            interval_usage_cost += (BIG_IL - SMALL_IL) * INSTANTCE_USAGE_PER_TIMER * MONITORING_INTERVAL

            if interval_usage_cost > SCALE_DOWN_THRESHOLD * INSTANTIATION_COST:
                print("try to terminate...")
                interval_usage_cost = 0
                start_scaling_interval_small = 0
                for _ in range(number_of_created_instances - SMALL_IL):
                    termination_event_creator(current_time, events)
    elif target_il == BIG_IL:
        print("target_il == BIG_IL")
        if start_scaling_interval_big == 0:
            print("restart start_scaling_interval_big")
            start_scaling_interval_big = 1
            interval_sla_violation_cost = last_sla_violation_cost
        else:
            print("continue start_scaling_interval_big")
            interval_sla_violation_cost += last_sla_violation_cost

            if interval_sla_violation_cost > SCALE_UP_THRESHOLD * INSTANTIATION_COST:
                interval_sla_violation_cost = 0
                start_scaling_interval_big = 0
                for _ in range(BIG_IL - number_of_created_instances):
                    instantiate_event_creator(current_time, events)
    else:
        print("target_il = NO_CHANGE_IL")
        start_scaling_interval_small = 0
        start_scaling_interval_big = 0

    last_sla_violation_cost = 0
    if len(events) > 0:
        monitor_event_creator(current_time, data, events)

def monitor_event_creator(current_time, data, events):
    logging.debug("monitor_event_creator: time = %s", current_time)
    monitoring_time = current_time + data["interval"]
    event = Event(monitoring_time, EventTypes.monitor, monitor_event_processor, data)
    add_event(events, event)

def report_event_creator(current_time, data, events):
    logging.debug("reporter_event_creator: time = %s", current_time)
    monitoring_time = current_time + data["interval"]
    event = Event(monitoring_time, EventTypes.report, report_event_processor, {"prefix":data["prefix"], "interval":data["interval"]})
    add_event(events, event)

def report_event_processor(current_time, data, events):
    reporter(data["prefix"], current_time)
    if len(events) > 0:
        report_event_creator(current_time, data, events)

def fill_arrival_events(requests, events):
    for req in requests:
        arrival_event_creator(req, events)

def fill_monitoring_events(prefix, interval, simulation_time, events):
    monitor_event_creator(0, {"prefix":prefix, "interval":interval}, events)

def fill_report_events(prefix, interval, simulation_time, events):
    report_event_creator(0, {"prefix":prefix, "interval":interval}, events)

INSTANTIATION_COST = 1000
def instantiation_cost():
    return INSTANTIATION_COST

INSTANTCE_USAGE_PER_TIMER = 10
def instance_usage_cost(ht):
    print("ht = ", ht)
    return ht * INSTANTCE_USAGE_PER_TIMER

def instances_costs():
    total_cost = 0
    for slt in instances:
        ht = slt.terminatation_time - slt.instantiation_time
        logging.debug("instances_costs: st = %s, et = %s", slt.instantiation_time, slt.terminatation_time)
        total_cost += instantiation_cost() + instance_usage_cost(ht)

    return total_cost

def sla_cost():
    return sum(sla_penalty_cost)

def print_events(events):
    for event in events:
        print("time = ", event.time, ", type = ", event.event_type, ", proc = ", event.processor, ", data = ", event.data)

def start():
    events = []
    return events

def run(events):
    last_time = 0
    while len(events) > 0:
        event = heapq.heappop(events)
        last_time = event.time
        event.processor(event.time, event.data, events)

        logging.debug("number_of_created_instances = %s number_of_empty_instances = %s Q len = %s", number_of_created_instances, number_of_empty_instances, requests_queue.qsize())

    return last_time

if __name__ == "__main__":
    rate_interval = 1.0 / 12.0
    load_scale = 0.7

    arrival_rates = [
            request_generator.ArrivalRateDynamics(rate_interval, 1 * load_scale),
            request_generator.ArrivalRateDynamics(rate_interval, 4 * load_scale),
            request_generator.ArrivalRateDynamics(rate_interval, 6 * load_scale),
            request_generator.ArrivalRateDynamics(rate_interval, 4 * load_scale),
            request_generator.ArrivalRateDynamics(rate_interval, 3 * load_scale),
            request_generator.ArrivalRateDynamics(rate_interval, 4 * load_scale),
            request_generator.ArrivalRateDynamics(rate_interval, 4 * load_scale),
            request_generator.ArrivalRateDynamics(rate_interval, 5 * load_scale),
            request_generator.ArrivalRateDynamics(rate_interval, 4 * load_scale),
            request_generator.ArrivalRateDynamics(rate_interval, 3 * load_scale),
            request_generator.ArrivalRateDynamics(rate_interval, 2 * load_scale),
            request_generator.ArrivalRateDynamics(rate_interval, 1 * load_scale)
        ]
    #Automative
    #service_type = request_generator.ServiceType1(1.0, 3.7, 4.7)
    #Bird Eye
    service_type = request_generator.ServiceType2(0.8, 14.7, 26.2)
    simulation_time = 1000
    iterations = 1

    MONITORING_INTERVAL = simulation_time / 100.0

    fix_inst_costs_arr_small = []
    fix_sla_costs_arr_small  = []
    fix_inst_costs_arr_big = []
    fix_sla_costs_arr_big  = []
    thre_inst_costs_arr = []
    thre_sla_costs_arr  = []

    for _ in range(iterations):
        logging.debug("******************************************")
        requests = request_generator.generate_requests_per_type(arrival_rates, service_type, simulation_time)

        def init():
            global requests_queue
            requests_queue = queue.Queue()
            global number_of_empty_instances
            number_of_empty_instances = 0
            global number_of_created_instances
            number_of_created_instances = 0
            global sla_penalty_cost
            sla_penalty_cost = []
            global instances
            instances = []
            global start_scaling_interval_small
            start_scaling_interval_small = 0
            global start_scaling_interval_big
            start_scaling_interval_big = 0
            global last_number_of_created_instances
            last_number_of_created_instances = 0
            global total_inst_cost
            total_inst_cost = 0

            events = start()
            fill_arrival_events(requests, events)

            return events

        def shutdown(inst_num, last_time):
            for _ in range(inst_num):
                termination_event_creator(last_time, events)
            run(events)

        def res(ins, sla):
            inst_costs = instances_costs()
            ins.append(inst_costs)
            sla_costs = sla_cost()
            sla.append(sla_costs)
            logging.debug("instances_costs = %s, sla_cost = %s", inst_costs, sla_costs)

        ############ FIX Policy ##################
        instance_num_small = SMALL_IL

        events = init()
        total_inst_cost = instance_num_small * INSTANTIATION_COST

        for _ in range(instance_num_small):
            instantiate_event_creator(-100, events)

        fill_report_events("small", MONITORING_INTERVAL, simulation_time, events)

        last_time = run(events)

        shutdown(instance_num_small, last_time)
        res(fix_inst_costs_arr_small, fix_sla_costs_arr_small)

        #----------------------------------------------

        instance_num_big = BIG_IL

        events = init()
        total_inst_cost = instance_num_big * INSTANTIATION_COST

        for _ in range(instance_num_big):
            instantiate_event_creator(-100, events)

        fill_report_events("big", MONITORING_INTERVAL, simulation_time, events)
        last_time = run(events)

        shutdown(instance_num_big, last_time)
        res(fix_inst_costs_arr_big, fix_sla_costs_arr_big)

        ############## Threshold scaling ###########
        logging.debug("------------------------------")

        events = init()

        fill_monitoring_events("aiml", MONITORING_INTERVAL, simulation_time, events)
        last_time = run(events)

        shutdown(number_of_created_instances, last_time)
        res(thre_inst_costs_arr, thre_sla_costs_arr)

    print("Fix small: instance_cost = ", sum(fix_inst_costs_arr_small) / iterations," sla cost = ", sum(fix_sla_costs_arr_small) / iterations)
    print("Fix big: instance_cost   = ", sum(fix_inst_costs_arr_big) / iterations," sla cost = ", sum(fix_sla_costs_arr_big) / iterations)
    print("Thr: instance_cost       = ", sum(thre_inst_costs_arr) / iterations," sla cost = ", sum(thre_sla_costs_arr) / iterations)


from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from DBConnection import DBConnection

class Ui_Voting(object):

    def __init__(self, dialog, vid):
        self.dialog = dialog;
        self.vid = vid

    def nominelist(self):
        try:
            database = DBConnection.getConnection()
            cursor = database.cursor()
            cursor.execute("select nominee,partyname from nominee")
            records = cursor.fetchall()
            for row in records:
                self.comboBox.addItem(str(row[0]) + "  -->  " + str(row[1]))

        except Exception as e:
            print("Error=" + e.args[0])
            tb = sys.exc_info()[2]
            print(tb.tb_lineno)

    def votingnm(self):
        database = DBConnection.getConnection()
        cursor = database.cursor()
        nmlist = self.comboBox.currentText()
        nm = nmlist.split("  -->  ")
        nominee = nm[0]
        symbol = nm[1]
        sql = "select count(*) from voting where vid='" + self.vid + "'"
        cursor.execute(sql)
        res = cursor.fetchone()[0]
        if res > 0:
            self.showMessageBox("Information", "Already Voted..!")
        else:
            try:
                vcnt = 0;
                sql = "select count(*) from voting where name='" + nominee + "'"
                cursor.execute(sql)
                res = cursor.fetchone()[0]
                if res > 0:
                    sql = "select vcnt from voting where name='" + nominee + "'"
                    cursor.execute(sql)
                    vcnt = cursor.fetchone()[0]
                    vcnt = vcnt + 1
                    sqll = "update voting set vcnt=" + str(vcnt) + " where name='" + nominee + "'"
                    cursor.execute(sqll)
                    database.commit();
                    self.showMessageBox("Information", "Voted Successfully..!")
                    self.dialog.hide()
                else:
                    vcnt = 1;
                    sql = "insert into voting values(%s,%s,%s,%s)"
                    values = (nominee, symbol, int(vcnt), self.vid)
                    cursor.execute(sql, values)
                    database.commit()
                    self.showMessageBox("Information", "Voted Successfully..!")
                    self.dialog.hide()

            except Exception as e:
                print("Error=" + e.args[0])
                tb = sys.exc_info()[2]
                print(tb.tb_lineno)

    def showMessageBox(self, title, message):
        msgBox = QtWidgets.QMessageBox()
        msgBox.setIcon(QtWidgets.QMessageBox.Information)
        msgBox.setWindowTitle(title)
        msgBox.setText(message)
        msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
        msgBox.exec_()

    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(691, 424)
        Dialog.setStyleSheet("background-color: rgb(85, 85, 255);")
        self.comboBox = QtWidgets.QComboBox(Dialog)
        self.comboBox.setGeometry(QtCore.QRect(170, 170, 181, 41))
        self.comboBox.setStyleSheet("color: rgb(0, 0, 0);\n"
"font: 75 12pt \"Verdana\";")
        self.comboBox.setObjectName("comboBox")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(170, 140, 151, 31))
        self.label.setStyleSheet("color: rgb(0, 0, 0);\n"
"font: 12pt \"Georgia\";")
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(170, 260, 181, 41))
        self.pushButton.setStyleSheet("color: rgb(255, 255, 255);\n"
"background-color: rgb(170, 85, 0);\n"
"font: 14pt \"Franklin Gothic Heavy\";")
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.votingnm)
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(310, 60, 201, 31))
        self.label_2.setStyleSheet("font: 18pt \"Franklin Gothic Heavy\";")
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(Dialog)
        self.label_3.setGeometry(QtCore.QRect(430, 90, 221, 271))
        self.label_3.setStyleSheet("image: url(../IRIS/images/Vote.png);")
        self.label_3.setText("")
        self.label_3.setObjectName("label_3")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Voting"))
        self.label.setText(_translate("Dialog", "Select Nominee"))
        self.pushButton.setText(_translate("Dialog", "Vote"))
        self.label_2.setText(_translate("Dialog", "Voting"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Voting(Dialog,'1839')
    ui.setupUi(Dialog)
    Dialog.show()
    ui.nominelist()
    sys.exit(app.exec_())


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

import os
from urllib.request import urlretrieve

"""
@File: grab_imgs.py
@Time: 4/28/17 7:31 PM
"""

def grab_img2file(url, home, subpath, filename):

    directory = "{}/{}/{}".format(home, subpath, filename)
    # The folder to save pics
    if not os.path.exists(home):
        os.mkdir(home)
    if not os.path.exists("{}/{}".format(home, subpath)):
        os.mkdir("{}/{}".format(home, subpath))
    if not os.path.isfile(directory):
        urlretrieve(url, directory)
    else:
        print("{} is already exists".format(directory))


import csv
import cv2
import numpy as np
import os
import math
from random import shuffle

samples = []
with open('data/driving_log.csv') as csvfile:
	reader = csv.reader(csvfile)
	for line in reader:
		samples.append(line)

recover_samples = []
with open('data_recover/driving_log.csv') as csvfile:
	reader = csv.reader(csvfile)
	for line in reader:
		recover_samples.append(line)

shuffle(samples)
shuffle(recover_samples)

image_paths = []
angles = []
for sample in samples:
    name = 'data/IMG/'+ sample[0].split('/')[-1]
    image_paths.append(name)
    center_angle = float(sample[3])
    angles.append(center_angle)

for sample in recover_samples:
    name = 'data_recover/IMG/'+ sample[0].split('/')[-1]
    image_paths.append(name)
    center_angle = float(sample[3])
    angles.append(center_angle)

print("size of angles:", len(angles))
print("size of image_paths", len(image_paths))
print("angles[14]", angles[14])
print("image_paths[14]", image_paths[14])
image = cv2.imread(image_paths[14])
cv2.imshow("im1", image)
cv2.waitKey(0)


import threading

"""
Remember threading from Java,
Threading is like running multiple processes concurrently.
"""

# Inherits from the Thread class
class Messenger(threading.Thread):
    # All threads need run function
    def run(self):
        for _ in range(0, 10):
            print(threading.current_thread().getName())

# Making objects of Messenger class that can be threaded
x = Messenger(name="Sender thread")
y = Messenger(name="Receiver thread")
z = Messenger(name="Mediator thread")

# Starting thread
x.start()
y.start()
z.start()


from django.shortcuts import render, redirect

import datetime
from django.utils import timezone

from .. import calgen
from .. import taskops

def cal_monthly(request, dest_year='', dest_month=''):
	# form handling
	if request.method == 'POST':
		print("wrong access")

	# choose month
	if dest_year == '':
		base_day = timezone.now()
	else:
		base_day = datetime.datetime(int(dest_year), int(dest_month), 1)

	context = calgen.populate_cal_monthly(base_day)

	# add form
	context.update(taskops.populate_task_form())

	return render(request, 'tdcs/calmonthly.html', context)

def cal_weekly(request, dest_year='', dest_month=''):
	# form handling
	if request.method == 'POST':
		print("wrong access")

	if dest_year == '':
		base_day = timezone.now()
	else:
		base_day = datetime.datetime(int(dest_year), int(dest_month), 1)

	context = calgen.populate_cal_weekly(base_day)

	# add form
	context.update(taskops.populate_task_form())

	return render(request, 'tdcs/calweekly.html', context)

def cal_daily(request):
	return render(request, 'tdcs/caldaily.html')

def create_task(request):
	if request.method == 'POST':
		dest = taskops.create_task_from_form(request)

	return redirect('/tdcs/cal/'+str(dest.year)+'/'+str(dest.month))

def edit_task(request):
	if request.method == 'POST':
		if 'edittaskbtn' in request.POST:
			dest = taskops.edit_task_from_form(request)
		elif 'completetaskbtn' in request.POST:
			dest = taskops.complete_task_from_form(request)
		elif 'deletetaskbtn' in request.POST:
			dest = taskops.delete_task_from_form(request)

	return redirect('/tdcs/cal/'+str(dest.year)+'/'+str(dest.month))


import AccountManager
import json
import xbmc
import os
import urllib

from cache import Cache, conditional_headers

''' Parameters for different F1TV API calls'''
__TV_API_PARAMS__ = {"event-occurrence": {"fields_to_expand": "image_urls,sessionoccurrence_urls,sessionoccurrence_urls__image_urls",
                               "fields": "name,self,image_urls,sessionoccurrence_urls,official_name,start_date,end_date,"
                                         "sessionoccurrence_urls__session_name,sessionoccurrence_urls__self,"
                                         "sessionoccurrence_urls__image_urls,sessionoccurrence_urls__start_time,sessionoccurrence_urls__available_for_user,sessionoccurrence_urls__content_urls"},
                     "race-season": {"fields": "year,name,self,eventoccurrence_urls,eventoccurrence_urls__name,eventoccurrence_urls__start_date,"
                                          "eventoccurrence_urls__self,eventoccurrence_urls__image_urls,image_urls",
                                "fields_to_expand": "eventoccurrence_urls,eventoccurrence_urls__image_urls,image_urls"},
                     "session-occurrence": {"fields": "name,self,image_urls,status,channel_urls,start_time,content_urls,session_name",
                                 "fields_to_expand": "channel_urls,image_urls,content_urls,channel_urls__image_urls,content_urls__image_urls,channel_urls__driver_urls,channel_urls__driver_urls__image_urls"},
                     "circuit": {"fields": "name,self,eventoccurrence_urls,eventoccurrence_urls__name,eventoccurrence_urls__start_date,"
                                          "eventoccurrence_urls__self,eventoccurrence_urls__image_urls,eventoccurrence_urls__official_name",
                                "fields_to_expand": "eventoccurrence_urls,eventoccurrence_urls__image_urls"}
                     }

class F1TV_API:
    """ Main API Object - is used to retrieve API information """

    def callAPI(self, endpoint, method="GET", api_ver=2, params=None, data=None):
        if int(api_ver) == 1:
            complete_url = 'https://f1tv.formula1.com' + endpoint
        elif int(api_ver) == 2:
            complete_url = 'https://f1tv-api.formula1.com/agl/1.0/gbr/en/all_devices/global/' + endpoint
        else:
            xbmc.log("Unable to make an API with invalid API version: {}".format(api_ver), xbmc.LOGERROR)
            return

        if method.upper() == 'GET':
            # Check to see if we've cached the response
            with Cache() as c:
                if params:
                    url_with_parameters = "{complete_url}?{parameters}".format(complete_url=complete_url,
                                                                               parameters=urllib.urlencode(params))
                else:
                    url_with_parameters = complete_url
                cached = c.get(url_with_parameters)
                if cached:
                    # If we have a fresh cached version, return it.
                    if cached["fresh"]:
                        return json.loads(cached["blob"])
                    # otherwise append applicable "If-None-Match"/"If-Modified-Since" headers
                    self.account_manager.getSession().headers.update(conditional_headers(cached))
                # request a new version of the data
                r = self.account_manager.getSession().get(complete_url, params=params, data=data)
                if 200 == r.status_code:
                    # add the new data and headers to the cache
                    c.set(url_with_parameters, r.content, r.headers)
                    return r.json()
                if 304 == r.status_code:
                    # the data hasn't been modified so just touch the cache with the new headers
                    # and return the existing data
                    c.touch(url_with_parameters, r.headers)
                    return json.loads(cached["blob"])

        elif method.upper() == 'POST':
            r = self.account_manager.getSession().post(complete_url, params=params, data=data)
            if r.ok:
                return r.json()
            else:
                return
        else:
            return

    def getFields(self, url):
        for key in __TV_API_PARAMS__:
            if key in url:
                return __TV_API_PARAMS__[key]

    def __init__(self):
        """ Initialize by creating AccountManager object"""
        self.account_manager = AccountManager.AccountManager()

    def login(self, username, password):
        """ Log in with supplied credentials."""
        return self.account_manager.login(username, password)

    def getStream(self, url):
        """ Get stream for supplied viewings item
            This will get the m3u8 url for Content and Channel."""
        item_dict = {"asset_url" if 'ass' in url else "channel_url": url}

        viewing_json = self.callAPI("/api/viewings/", api_ver=1, method='POST', data=json.dumps(item_dict))

        if 'chan' in url:
            return viewing_json["tokenised_url"]
        else:
            return viewing_json["objects"][0]["tata"]["tokenised_url"]

    def getSession(self, url):
        """ Get Session Object from API by supplying an url"""
        session = self.callAPI(url, params=__TV_API_PARAMS__["session-occurrence"])
        return session

    def getEvent(self, url, season = None):
        """ Get Event object from API by supplying an url"""
        event = self.callAPI(url, params=__TV_API_PARAMS__["event-occurrence"])
        return event

    def getSeason(self, url):
        """ Get Season object from API by supplying an url"""
        season = self.callAPI(url, api_ver=1, params=self.getFields(url))
        return season

    def getSeasons(self):
        """ Get all season urls that are available at API"""
        seasons = self.callAPI("/api/race-season/", api_ver=1, params={'order': '-year'})
        return seasons

    def getCircuits(self):
        """ Get all Circuit urls that are available at API"""
        circuits = self.callAPI("/api/circuit/", api_ver=1, params={"fields": "name,eventoccurrence_urls,self"})
        return circuits

    def getCircuit(self, url):
        """ Get Circuit object from API by supplying an url"""
        circuit = self.callAPI(url, api_ver=1, params=__TV_API_PARAMS__["circuit"])
        return circuit

    def getF2(self):
        f2 = self.callAPI("/api/sets/coll_4440e712d31d42fb95c9a2145ab4dac7")
        return f2

    def getSets(self):
        sets = self.callAPI("/api/sets/?slug=home", api_ver=1)
        content = {}
        for item in sets['objects'][0]['items']:
            item_details = self.callAPI(item['content_url'], api_ver=1)
            if 'title' in list(item_details):
                content[item_details['title']] = item['content_url']
            elif 'name' in list(item_details):
                content[item_details['name']] = item['content_url']
            else:
                content[item_details['UNKNOWN SET: ' + 'uid']] = item['content_url']
        return content

    def setLanguage(self, language):
        self.account_manager.session.headers['Accept-Language'] = "{}, en".format(language.upper())


# -*- coding: utf-8 -*- #
# Copyright 2022 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Command to describe a Cloud Asset Inventory saved query."""

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

from googlecloudsdk.api_lib.asset import client_util
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.asset import flags
from googlecloudsdk.command_lib.asset import utils as asset_utils

class Describe(base.Command):
  """Describe a Cloud Asset Inventory saved query."""

  detailed_help = {
      'DESCRIPTION':
          """\
      Describe a Cloud Asset Inventory saved query.
      """,
      'EXAMPLES':
          """\
        To describe a saved query with query id 'query1' in project 'p1', run:

          $ {command} query1 --project=p1
      """
  }

  @staticmethod
  def Args(parser):
    flags.AddParentArgs(parser, 'Project of the saved query.',
                        'Organization of the saved query.',
                        'Folder of the saved query.')
    query_id_helper_text = (
        'Asset Saved Query identifier being described. '
        'It must be unique under the'
        ' specified parent resource: project/folder/organization.')

    flags.AddSavedQueriesQueryId(parser, query_id_helper_text)

  def Run(self, args):
    parent = asset_utils.GetSavedQueriesParent(args.organization, args.project,
                                               args.folder)
    client = client_util.AssetSavedQueriesClient(parent)
    return client.Describe(args)


from zebpedersen_com_site.models import InstagramItem
from instagram.client import InstagramAPI
from secret_constants import SecretConstants

def update():
	#	Easiest to destory current stash and recreate with incoming 20

	#	1. get all new
	api = InstagramAPI(access_token = SecretConstants.INSTAGRAM['kACCESS_TOKEN'])
	recent_media, next = api.user_recent_media(user_id = SecretConstants.INSTAGRAM['kUSER_ID'], count = 20)

	#	2. delete all currently stored media
	InstagramItem.objects.all().delete()

	#	3. squeeze into internal db schema
	for item in recent_media:
		new_item = InstagramItem(
			image = item.images['standard_resolution'].url,
			link = item.link,
			alt_text = item.caption.text
		)
		new_item.save()


# -*- coding: utf-8 -*-
import sqlite3
import yaml
import os
import re
from datetime import timedelta, datetime

def update_db(db_name, sw_src_file=None, dhcp_src_file=None):
    if not os.path.exists(db_name):
        print('База данных не существует. Перед добавлением данных, ее надо создать')
        return None
    else:
        connection = sqlite3.connect(db_name)
        del_old_lines(connection)
        if sw_src_file:
            update_switches_table(sw_src_file, connection)
        if dhcp_src_file:
            update_dhcp_table(dhcp_src_file, connection)

def del_old_lines(connection):
    now = datetime.today().replace(microsecond=0)
    week_ago = now - timedelta(days=7)
    connection.execute(f"DELETE FROM dhcp WHERE last_active < '{week_ago}'")

def update_switches_table(src_file, connection):
    with open(src_file) as f:
        data = yaml.safe_load(f)
    query = 'INSERT into switches values (?, ?)'
    for line in data['switches'].items():
        try:
            connection.execute(query, line)
        except sqlite3.IntegrityError as error:
            print(f'При добавлении данных: {line} Возникла ошибка: {error}')
    connection.commit()

def update_dhcp_table(src_file, connection):
    switch_name = re.match(r'\S*(?P<host>sw\d+)_dhcp_snooping.txt', src_file)
    src_data = open(src_file, 'r').read()
    connection.execute(f"UPDATE dhcp SET active = 0 WHERE switch = '{switch_name[1]}'")
    matches = re.findall(r'(?P<mac>\S+) +(?P<ip>\S+) +\d+ +\S+ +(?P<vlan>\d+) +(?P<intf>\S+)', src_data)

    query = "INSERT INTO dhcp VALUES (?, ?, ?, ?, ?, ?, datetime('now'))"
    for match in matches:
        list_to_append = list(match)
        list_to_append.extend([switch_name[1], 1])
        try:
            connection.execute(query, list_to_append)
        except sqlite3.IntegrityError:
            connection.execute(f"UPDATE dhcp SET ip = '{list_to_append[1]}',"
                               f" vlan = '{list_to_append[2]}',"
                               f" interface = '{list_to_append[3]}',"
                               f" active = '{list_to_append[5]}',"
                               f" last_active = datetime('now')"
                               f" WHERE mac = '{list_to_append[0]}' and switch = '{list_to_append[4]}'")

    connection.commit()

if __name__ == '__main__':
    update_db('dhcp_snooping.db', dhcp_src_file='new_data/sw2_dhcp_snooping.txt')


"""Write a function named printTable() that takes a list of lists of strings and displays it in a well-organized table with each column right-justified. Assume that all the inner lists will contain the same number of strings. For example, the value could look like this:

tableData = [['apples', 'oranges', 'cherries', 'banana'],
             ['Alice', 'Bob', 'Carol', 'David'],
             ['dogs', 'cats', 'moose', 'goose']]
Your printTable() function would print the following:

  apples Alice  dogs
 oranges   Bob  cats
cherries Carol moose
  banana David goose"""

def printTable(table_data):
    col_widths = []
    for row in table_data:
      col_widths.append(len(max(row, key=len)))
    # print(col_widths)
    tx =[[row[i] for row in table_data] for i in range (len(table_data[0]))]
    # print (tx)

    for row in tx:
      res = ""
      for i,item in enumerate(row):
        res += item.rjust(col_widths[i]) + " "
      print(res)
    print()

table_data_01 = [['apples', 'oranges', 'cherries', 'banana'],
             ['Alice', 'Bob', 'Carol', 'David'],
             ['dogs', 'cats', 'moose', 'goose']]

printTable(table_data_01)


import time
from concurrent.futures import ThreadPoolExecutor

from ..file_watch import BaseWatchHandler
from ..utils.time_utility import tsnow
from ..utils.mail_utility import send_email

executor = ThreadPoolExecutor(max_workers=3)

class EmailHandler(BaseWatchHandler):

    def __init__(self):
        self.buffer = []
        self.last_send_time = tsnow()
        self.send_interval = 60

    def should_flush(self):
        now = tsnow()
        should_flush = (now - self.last_send_time) > self.send_interval
        return should_flush and self.buffer

    def flush(self):
        buffers = self.buffer
        self.buffer = []
        self.last_send_time = tsnow()
        executor.emit(send_email, host, port, username, password, subject, ''.join(buffers))

    def process(self, content):
        self.buffer.extend(content)
        if self.should_flush():
            self.flush()


# -*- coding: utf-8 -*-
"""
Created on Tue Nov 17 17:19:01 2020

@author: Mr Bcan
"""

#import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

hello=tf.constant('HEllo , TEnsor')
sess=tf.Session()
print(sess.run(hello))

"""

def dels(first,second):
    a=len(first)
    b=len(second)

    if a >b :
        del first[:b]
        print(first)

    if a<b:
        c=[]
        c.append(second[0])
        c=c+first
        c=c+second[1:]
        print(c)

    if a==b:
        c=[]
        print(c)

dels([3,4,2,5,6,7],[1,3,4])
dels([2,5],[3,4,7,8,9])
dels([2,3,4],[8,7,9])

def dels(a,b):

    if(len(a)>len(b)):
        return a[len(b):]

    elif(len(b)>len(a)):
        return (b[0:1]+a[:]+b[1:])

    else:
        return []

print(dels([3,4,2,5,6,7],[1,3,4]))
print(dels([2,5],[3,4,7,8,9]))
print(dels([2,3,4],[8,7,9]))

def digits(num):

    if(num<10):
        return [num]

    else:
        last = digits(int(num%10))
        return (digits(int(num/10)))+last

print(digits(532))

def multiply(num,zeros):

    return digits(num)+digits(zeros)[1:]

print(multiply(5,1000))

print(digits(3))
    """


from rest_framework import serializers
from student import models

class StudentSerializer(serializers.ModelSerializer):
    class Meta:
        fields = (
            'id',
            'name',
            'age',
            'is_adult',
            'school'
        )
        model = models.Student
    school = serializers.ReadOnlyField(source='school.name')


import d2lzh as d2l
from mxnet import autograd, gluon, init, nd
from mxnet.gluon import nn

# a = nd.array([[[1,2],[2,3]],[[4,5],[7,9]]])
# print(a.mean(axis=0),a.mean(axis=0,keepdims=True))

def batch_norm(X, gamma, beta, moving_mean, moving_var, eps, momentum):
    # 通过autograd判断是训练还是预测
    if not autograd.is_training():
        X_hat = (X - moving_mean) / nd.sqrt(moving_var + eps)
    else:
        assert len(X.shape) in (2, 4)
        if len(X.shape) == 2:
            # 使用全连接层，计算特征维上的均值和方差
            mean = X.mean(axis=0)
            var = ((X - mean)**2).mean(axis=0)
        else:
            # 使用二维卷积时，计算通道维度（axis=1)上的均值和方差
            # 保持住X的形状以便后面可以做广播运算
            mean = X.mean(axis=(0, 2, 3), keepdims=True)
            var = ((X - mean)**2).mean(axis=(0, 2, 3), keepdims=True)
        # 训练时使用当前均值和方差做标准化
        X_hat = (X - mean) / nd.sqrt(var + eps)
        # 更新移动平均的均值和方差
        moving_mean = momentum*moving_mean + (1 - momentum)*mean
        moving_var = momentum*moving_var + (1 - momentum)*var

    Y = gamma*X_hat + beta #拉伸和平移
    return Y, moving_mean,moving_var

class BatchNorm(nn.Block):
    def __init__(self, num_features, num_dims, **kwargs):
        super(BatchNorm, self).__init__(**kwargs)
        if num_dims == 2:
            shape = (1, num_features)
        else:
            shape = (1, num_features, 1, 1)
        # 参与求梯度的拉伸和偏移参数，分别初始化为1，0
        self.gamma = self.params.get('gamma', shape=shape, init=init.One())
        self.beta = self.params.get('beta', shape=shape, init=init.Zero())
        # 不参求梯度和迭代的变量，在内存上初始化为0
        self.moving_mean = nd.zeros(shape)
        self.moving_var = nd.zeros(shape)

    def forward(self, X):
         # 如果X不在内存上，将moving_mean和moving_var复制到X所在显存上
        if self.moving_mean.context != X.context:
            self.moving_mean = self.moving_mean.copyto(X.context)
            self.moving_var = self.moving_var.copyto(X.context)
        Y, self.moving_mean, self.moving_var = batch_norm(
                    X, self.gamma.data(), self.beta.data(),self.moving_mean,
                    self.moving_var, eps=1e-5, momentum=0.9)
        return Y

net = nn.Sequential()
net.add(nn.Conv2D(6, kernel_size=5),
        BatchNorm(6, num_dims=4),
        nn.Activation('sigmoid'),
        nn.MaxPool2D(pool_size=2, strides=2),
        nn.Conv2D(16, kernel_size=5),
        BatchNorm(16, num_dims=4),
        nn.Activation('sigmoid'),
        nn.MaxPool2D(pool_size=2, strides=2),
        nn.Dense(120),
        BatchNorm(120, num_dims=2),
        nn.Activation('sigmoid'),
        nn.Dense(84),
        BatchNorm(84, num_dims=2),
        nn.Activation('sigmoid'),
        nn.Dense(10))

lr, num_epochs, batch_size, ctx = 1.0, 5, 256, d2l.try_gpu()
net.initialize(ctx=ctx, init=init.Xavier())
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': lr})
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
d2l.train_ch5(net, train_iter, test_iter, batch_size, trainer, ctx,
              num_epochs)

# gluon简洁实现
net = nn.Sequential()
net.add(nn.Conv2D(6, kernel_size=5),
        nn.BatchNorm(),
        nn.Activation('sigmoid'),
        nn.MaxPool2D(pool_size=2, strides=2),
        nn.Conv2D(16, kernel_size=5),
        nn.BatchNorm(),
        nn.Activation('sigmoid'),
        nn.MaxPool2D(pool_size=2, strides=2),
        nn.Dense(120),
        nn.BatchNorm(),
        nn.Activation('sigmoid'),
        nn.Dense(84),
        nn.BatchNorm(),
        nn.Activation('sigmoid'),
        nn.Dense(10))

net.initialize(ctx=ctx, init=init.Xavier())
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': lr})
d2l.train_ch5(net, train_iter, test_iter, batch_size, trainer, ctx,
              num_epochs)


#!/usr/bin/python3

from scapy.all import *

interface = "lo" #localhost
count=1 # Capturing two packets

def get_payload() :
    while True :
        print("Sniffing.....")
        packet = sniff(iface=interface, count=count)
        print(packet)
        if "load" in packet :
            return packet.load
        print("No payload")