from calendar import c
from re import S
import sys
import time

import smbus
import spidev
import RPi.GPIO as GPIO

import logging
from ctypes import *

logger = logging.getLogger()
# logger.setLevel(logging.INFO)   # Display all print information
logger.setLevel(logging.FATAL)   # If you don’t want to display too many prints, only print errors, please use this option
ph = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s - [%(filename)s %(funcName)s]:%(lineno)d - %(levelname)s: %(message)s")
ph.setFormatter(formatter)
logger.addHandler(ph)

## I2C communication address when SDO is grounded
DFROBOT_BME280_I2C_ADDR_SDO_GND = 0x76
## I2C communication address when SDO is connected to power
DFROBOT_BME280_I2C_ADDR_SDO_VDD = 0x77
## BME280 chip version
BME280_REG_CHIP_ID_DEFAULT = 0x60

# BME280 register address
## 0x88-0xA1 is calibration data, calib00..calib25
BME280_CALIB_DATA_00_25 = 0x88
## The “CHIP_ID” register contains the chip identification code.
BME280_CHIP_ID = 0xD0
## Triggers a reset, all user configuration settings are overwritten with their default state.
BME280_CMD_RESET = 0xE0
## 0xE1-0xF0 is calibration data, calib26..calib41
BME280_CALIB_DATA_26_41 = 0xE1
## The “CTRL_HUM” register
BME280_CTRL_HUM = 0xF2
## The Sensor Status Flags are stored in the “STATUS” register.
BME280_STATUS = 0xF3
## The “CTRL_MEAS” register
BME280_CTRL_MEAS = 0xF4
## The “CONFIG” register
BME280_CONFIG = 0xF5
## The 24Bit pressure data is split and stored in three consecutive registers.
BME280_PRESS_DATA_MSB = 0xF7
## The 24Bit temperature data is split and stored in three consecutive registered.
BME280_TEMP_DATA_MSB = 0xFA
## The 16Bit temperature data is split and stored in two consecutive registered.
BME280_HUM_DATA_MSB = 0xFD

# Sensor configuration
## Sleep mode: It will be in sleep mode by default after power-on reset. In this mode, no measurement is performed and power consumption is minimal.
##             All registers are accessible for reading the chip ID and compensation coefficient.
SLEEP_MODE  = 0x00
## Forced mode: In this mode, the sensor will take a single measurement according to the selected measurement and filtering options. After the
##              measurement is completed, the sensor will return to sleep mode, and the measurement result can be obtained in the register.
FORCED_MODE = 0x01
## Normal mode: Continuously loop between the measurement period and the standby period. The output data rates are related to the ODR mode setting.
NORMAL_MODE = 0x03

## temperature oversampling settings
BME280_TEMP_OSR_SETTINGS = (0x00, 0x20, 0x40, 0x60, 0x80, 0xA0)
## pressure oversampling settings
BME280_PRESS_OSR_SETTINGS = (0x00, 0x04, 0x08, 0x0C, 0x10, 0x14)
## humidity oversampling settings
BME280_HUMI_OSR_SETTINGS = (0x00, 0x01, 0x02, 0x03, 0x04, 0x05)

## IIR filter settings
BME280_IIR_FILTER_SETTINGS = (0x00, 0x04, 0x08, 0x0C, 0x10)

# Controls inactive duration tstandby in normal mode.
## Controls inactive duration tstandby; ODR 2000Hz; Sampling period:0.5 ms
BME280_CONFIG_STANDBY_TIME_0P5  = 0x00
## Sampling period:62.5 ms
BME280_CONFIG_STANDBY_TIME_62P5 = 0x20
## Sampling period:125 ms
BME280_CONFIG_STANDBY_TIME_125  = 0x40
## Sampling period:250 ms
BME280_CONFIG_STANDBY_TIME_250  = 0x60
## Sampling period:500 ms
BME280_CONFIG_STANDBY_TIME_500  = 0x80
## Sampling period:1000 ms
BME280_CONFIG_STANDBY_TIME_1000 = 0xA0
## Sampling period:10 ms
BME280_CONFIG_STANDBY_TIME_10   = 0xC0
## Sampling period:20 ms
BME280_CONFIG_STANDBY_TIME_20   = 0xE0

## Triggers a reset, all user configuration settings are overwritten with their default state.
BME280_CMD_RESET_VALUE = 0xB6

## Standard sea level pressure, unit: pa
STANDARD_SEA_LEVEL_PRESSURE_PA = 101325.00

class DFRobot_BME280(object):
    '''!
      @brief define DFRobot_BME280 base class
      @details for driving the pressure sensor
    '''

    def __init__(self):
        '''!
          @brief Module init
        '''
        # Sea level pressure in Pa.
        self.sea_level_pressure = STANDARD_SEA_LEVEL_PRESSURE_PA
        self._t_fine = 0

    def begin(self):
        '''!
          @brief Initialize sensor
          @return  return initialization status
          @retval True indicate initialization succeed
          @retval False indicate initialization failed
        '''
        ret = False
        chip_id = self._read_reg(BME280_CHIP_ID, 1)
        logger.info(chip_id[0])
        if chip_id[0] == BME280_REG_CHIP_ID_DEFAULT:
            self.reset()
            time.sleep(0.3)
            self._get_coefficients()
            self.set_config_filter(BME280_IIR_FILTER_SETTINGS[0])
            self.set_config_T_standby(BME280_CONFIG_STANDBY_TIME_125)
            self.set_ctrl_meas_sampling_temp(BME280_TEMP_OSR_SETTINGS[3])
            self.set_ctrl_meas_sampling_press(BME280_PRESS_OSR_SETTINGS[3])
            self.set_ctrl_sampling_humi(BME280_HUMI_OSR_SETTINGS[3])
            self.set_ctrl_meas_mode(NORMAL_MODE)
            time.sleep(2)   # warm-up
            ret = True
        return ret

    @property
    def get_temperature(self):
        '''!
          @brief Get pressure measurement value from register, working range (-40 ~ +85 °C)
          @return Return temperature measurements, unit: °C
        '''
        data = self._read_reg(BME280_TEMP_DATA_MSB, 3)
        raw = data[0] << 12 | data[1] << 4 | (data[2] & 0x0F)

        # datasheet, Trimming Coefficient listing in register map with size and sign attributes
        t1, t2, t3, p1, p2, p3, p4, p5, p6, p7, p8, p9, h1, h2, h3, h4, h5, h6 = self._data_calib

        v1 = ((((raw >> 3) - (t1 << 1))) * t2) >> 11
        v2 = (((((raw >> 4) - t1) * ((raw >> 4) - t1)) >> 12) * t3) >> 14
        self._t_fine = v1 + v2
        rslt = (self._t_fine * 5 + 128) >> 8
        return (float(rslt) / 100)   # round((rslt / 100), 2)

    @property
    def get_pressure(self):
        '''!
          @brief Get pressure measurement value from register, working range (300 ~ 1100 hPa)
          @return Return pressure measurements, unit: Pa
          @attention If the reference value is provided before, the absolute value of the current
          @n         position pressure is calculated according to the calibrated sea level atmospheric pressure
        '''
        data = self._read_reg(BME280_PRESS_DATA_MSB, 3)
        raw = data[0] << 12 | data[1] << 4 | (data[2] & 0x0F)

        # datasheet, Trimming Coefficient listing in register map with size and sign attributes
        t1, t2, t3, p1, p2, p3, p4, p5, p6, p7, p8, p9, h1, h2, h3, h4, h5, h6 = self._data_calib

        self.get_temperature   # update _t_fine

        v1 = self._t_fine - 128000
        v2 = v1 * v1 * p6
        v2 = v2 + ((v1 * p5) << 17)
        v2 = v2 + (p4 << 35)
        v1 = ((v1 * v1 * p3) >> 8) + ((v1 * p2) << 12)
        v1 = (((1 << 47) + v1)) * p1 >> 33
        if(v1 == 0):
          return 0
        rslt = 1048576 - raw
        rslt = (((rslt << 31) - v2) * 3125) / v1
        v1 = (p9 * (int(rslt) >> 13) * (int(rslt) >> 13)) >> 25
        v2 = (p8 * int(rslt)) >> 19
        rslt = ((int(rslt) + v1 + v2) >> 8) + (p7 << 4)
        return (float(rslt) / 256)

    @property
    def get_humidity(self):
        '''!
          @brief Get humidity measurement value from register, working range (0 ~ 100 %RH)
          @return Return humidity measurements, unit: %RH
        '''
        data = self._read_reg(BME280_HUM_DATA_MSB, 2)
        raw = data[0] << 8 | data[1]

        # datasheet, Trimming Coefficient listing in register map with size and sign attributes
        t1, t2, t3, p1, p2, p3, p4, p5, p6, p7, p8, p9, h1, h2, h3, h4, h5, h6 = self._data_calib

        self.get_temperature   # update _t_fine

        v1 = self._t_fine - 76800
        v1 = (((((raw <<14) - (h4 << 20) - (h5 * v1)) + 16384) >> 15) * (((((((v1 * h6) >> 10) *
             (((v1 * h3) >> 11) + 32768)) >> 10) + 2097152) * h2 + 8192) >> 14))
        v1 = (v1 - (((((v1 >> 15) * (v1 >> 15)) >> 7) * h1) >> 4))
        if v1 < 0:
            v1 = 0
        elif v1 > 419430400:
            v1 = 419430400
        return (v1 >> 12) / 1024.0

    @property
    def get_altitude(self):
        '''!
          @brief Calculate the altitude based on the atmospheric pressure measured by the sensor
          @return Return altitude, unit: m
          @attention If the reference value is provided before, the absolute value of the current
          @n         position pressure is calculated according to the calibrated sea level atmospheric pressure
        '''
        # see https://www.weather.gov/media/epz/wxcalc/pressureAltitude.pdf
        return 44307.7 * (1 - (self.get_pressure / self.sea_level_pressure) ** 0.190284)

    @property
    def get_data_ready_status(self):
        '''!
          @brief get data ready status
          @return True is data ready
        '''
        temp = self._read_reg(BME280_STATUS, 1)[0]
        if temp & 0b00001000:   # measuring[3]
            return False
        else:
            return True

    def reset(self):
        '''!
          @brief Reset and restart the sensor, restoring the sensor configuration to the default configuration
        '''
        self._write_reg(BME280_CMD_RESET, BME280_CMD_RESET_VALUE)
        time.sleep(0.1)

    def calibrated_absolute_difference(self, altitude):
        '''!
          @brief Take the given current location altitude as the reference value
          @n     to eliminate the absolute difference for subsequent pressure and altitude data
          @param altitude Altitude in current position
          @return Pass the benchmark value successfully will return ture, if failed it will return false
        '''
        # The altitude in meters based on the currently set sea level pressure.
        ret = False
        if STANDARD_SEA_LEVEL_PRESSURE_PA == self.sea_level_pressure:
            self.sea_level_pressure = (self.get_pressure / pow(1.0 - (altitude / 44307.7), 5.255302))
            ret = True
        return ret

    def set_ctrl_meas_mode(self, mode):
        '''!
          @brief Configure measurement mode and power mode
          @param mode The measurement mode and power mode that need to be set:
          @n  SLEEP_MODE(Sleep mode): It will be in sleep mode by default after power-on reset. In this mode,no
          @n                          measurement is performed and power consumption is minimal. All registers
          @n                          are accessible for reading the chip ID and compensation coefficient.
          @n  FORCED_MODE(Forced mode): In this mode, the sensor will take a single measurement according to the selected
          @n                            measurement and filtering options. After the measurement is completed, the sensor
          @n                            will return to sleep mode, and the measurement result can be obtained in the register.
          @n  NORMAL_MODE(Normal mode): Continuously loop between the measurement period and the standby period.
          @n                            The output data rates are related to the ODR mode setting.
        '''
        mask = 0b00000011   # mode[1:0]
        self._write_reg_bits(BME280_CTRL_MEAS, mask, mode)

    def set_ctrl_meas_sampling_temp(self, osrs_t):
        '''!
          @brief Configure the oversampling when measuring temperature (OSR:over-sampling register)
          @details When the IIR filter is enabled, the temperature resolution is 20 bit.
          @n       When the IIR filter is disabled, the temperature resolution is 16 + (osrs_t – 1) bit,
          @n       e.g. 18 bit when osrs_t is set to ‘3’.
          @param - osrs_t 6 temp oversampling mode:
          @n         BME280_TEMP_OSR_SETTINGS[0], temperature sampling×0, Skipped (output set to 0x80000)
          @n         BME280_TEMP_OSR_SETTINGS[1], temperature sampling×2, 16 bit
          @n         BME280_TEMP_OSR_SETTINGS[2], temperature sampling×4, 17 bit
          @n         BME280_TEMP_OSR_SETTINGS[3], temperature sampling×8, 18 bit
          @n         BME280_TEMP_OSR_SETTINGS[4], temperature sampling×16, 19 bit
          @n         BME280_TEMP_OSR_SETTINGS[5], temperature sampling×32, 20 bit
        '''
        mask = 0b11100000   # osrs_t[7:5]
        self._write_reg_bits(BME280_CTRL_MEAS, mask, osrs_t)

    def set_ctrl_meas_sampling_press(self, osrs_p):
        '''!
          @brief Configure the oversampling when measuring press (OSR:over-sampling register)
          @details When the IIR filter is enabled, the pressure resolution is 20 bit.
          @n       When the IIR filter is disabled, the pressure resolution is 16 + (osrs_p – 1) bit,
          @n       e.g. 18 bit when osrs_p is set to ‘3’.
          @param - osrs_t 6 temp oversampling mode:
          @n         BME280_PRESS_OSR_SETTINGS[0], pressure sampling×0, Skipped (output set to 0x80000)
          @n         BME280_PRESS_OSR_SETTINGS[1], pressure sampling×2, 16 bit
          @n         BME280_PRESS_OSR_SETTINGS[2], pressure sampling×4, 17 bit
          @n         BME280_PRESS_OSR_SETTINGS[3], pressure sampling×8, 18 bit
          @n         BME280_PRESS_OSR_SETTINGS[4], pressure sampling×16, 19 bit
          @n         BME280_PRESS_OSR_SETTINGS[5], pressure sampling×32, 20 bit
        '''
        mask = 0b00011100   # osrs_t[4:2]
        self._write_reg_bits(BME280_CTRL_MEAS, mask, osrs_p)

    def set_ctrl_sampling_humi(self, osrs_h):
        '''!
          @brief Configure the oversampling when measuring humidity (OSR:over-sampling register)
          @details For the humidity measurement, oversampling is possible to reduce the noise.
          @n       The resolution of the humidity measurement is fixed at 16 bit ADC output.
          @param - osrs_t 6 temp oversampling mode:
          @n         BME280_HUMI_OSR_SETTINGS[0], humidity sampling×0, Skipped (output set to 0x80000)
          @n         BME280_HUMI_OSR_SETTINGS[1], humidity sampling×2, 16 bit
          @n         BME280_HUMI_OSR_SETTINGS[2], humidity sampling×4, 17 bit
          @n         BME280_HUMI_OSR_SETTINGS[3], humidity sampling×8, 18 bit
          @n         BME280_HUMI_OSR_SETTINGS[4], humidity sampling×16, 19 bit
          @n         BME280_HUMI_OSR_SETTINGS[5], humidity sampling×32, 20 bit
        '''
        mask = 0b00000111   # osrs_t[2:0]
        self._write_reg_bits(BME280_CTRL_HUM, mask, osrs_h)

    def set_config_filter(self, iir_config_coef):
        '''!
          @brief IIR filter coefficient setting(IIR filtering)
          @param - iir_config_coef Set IIR filter coefficient, configurable mode:
          @n         BME280_IIR_FILTER_SETTINGS[0], filter off
          @n         BME280_IIR_FILTER_SETTINGS[1], filter coefficient 2
          @n         BME280_IIR_FILTER_SETTINGS[2], filter coefficient 4
          @n         BME280_IIR_FILTER_SETTINGS[3], filter coefficient 8
          @n         BME280_IIR_FILTER_SETTINGS[4], filter coefficient 16
        '''
        mask = 0b00011100   # osrs_t[4:2]
        self._write_reg_bits(BME280_CONFIG, mask, iir_config_coef)

    def set_config_T_standby(self, odr_set):
        '''!
          @brief Set output data rate in subdivision/sub-sampling mode (ODR:output data rates)
          @param odr_set The output data rate needs to be set, configurable mode:
          @n BME280_CONFIG_STANDBY_TIME_0P5, BME280_CONFIG_STANDBY_TIME_62P5, BME280_CONFIG_STANDBY_TIME_125,
          @n BME280_CONFIG_STANDBY_TIME_250, BME280_CONFIG_STANDBY_TIME_500, BME280_CONFIG_STANDBY_TIME_1000,
          @n BME280_CONFIG_STANDBY_TIME_10, BME280_CONFIG_STANDBY_TIME_20
          @return  return configuration results
          @retval True indicate configuration succeed
          @retval False indicate configuration failed and remains its original state
        '''
        # The IIR filter coefficient.
        ret = True
        mask = 0b11100000   # osrs_t[7:5]
        self._write_reg_bits(BME280_CONFIG, mask, odr_set)
        if (self._read_reg(BME280_CONFIG, 1)[0] & 0xE0):
            logger.warning("Sensor configuration error detected!")
            ret = False
        return ret

    def _uint8_to_int(self,num):
        '''!
          @brief Convert the incoming uint8 type data to int type
          @param num Incoming uint8 type data
          @return data converted to int type
        '''
        if(num>127):
            num = num - 256
        return num

    def _uint16_to_int(self,num):
        '''!
          @brief Convert the incoming uint16 type data to int type
          @param num Incoming uint16 type data
          @return data converted to int type
        '''
        if(num>32767):
            num = num - 65536
        return num

    def _get_coefficients(self):
        '''!
          @brief Get the calibration data in the NVM register of the sensor
        '''
        calib = self._read_reg(BME280_CALIB_DATA_00_25, 26)
        calib1 = self._read_reg(BME280_CALIB_DATA_26_41, 7)
        self._data_calib = (
            (calib[1] << 8) | calib[0],  # T1
            self._uint16_to_int((calib[3] << 8) | calib[2]),  # T2
            self._uint16_to_int((calib[5] << 8) | (calib[4])),  # T3
            (calib[7] << 8) | calib[6],  # P1
            self._uint16_to_int((calib[9] << 8) | calib[8]),  # P2
            self._uint16_to_int((calib[11] << 8) | calib[10]),  # P3
            self._uint16_to_int((calib[13] << 8) | calib[12]),  # P4
            self._uint16_to_int((calib[15] << 8) | calib[14]),  # P5
            self._uint16_to_int((calib[17] << 8) | calib[16]),  # P6
            self._uint16_to_int((calib[19] << 8) | calib[18]),  # P7
            self._uint16_to_int((calib[21] << 8) | calib[20]),  # P8
            self._uint16_to_int((calib[23] << 8) | calib[22]),  # P9
            calib[25],  # H1
            self._uint16_to_int((calib1[1] << 8) | calib1[0]),  # H2
            calib1[2],  # H3
            self._uint16_to_int((calib1[3] << 4) | (calib1[4] & 0x0F)),  # H4
            self._uint16_to_int((calib1[5] << 4) | ((calib1[4] >> 4) & 0x0F)),  # H5
            self._uint8_to_int(calib1[6]),  # H6
        )

    def _write_reg_bits(self, reg, field, val):
        '''!
          @brief writes data to a register
          @param reg register address
          @param data written data
        '''
        temp = self._read_reg(reg, 1)[0]
        temp &= ~field
        temp |= val
        self._write_reg(reg, temp)

    def _write_reg(self, reg, data):
        '''!
          @brief writes data to a register
          @param reg register address
          @param data written data
        '''
        # Low level register writing, not implemented in base class
        raise NotImplementedError()

    def _read_reg(self, reg, length):
        '''!
          @brief read the data from the register
          @param reg register address
          @param length read data length
          @return read data list
        '''
        # Low level register writing, not implemented in base class
        raise NotImplementedError()

class DFRobot_BME280_I2C(DFRobot_BME280):
    '''!
      @brief define DFRobot_BME280_I2C base class
      @details for using I2C protocol to drive the pressure sensor
    '''

    def __init__(self, i2c_addr=0x77, bus=1):
        '''!
          @brief Module I2C communication init
          @param i2c_addr I2C communication address
          @param bus I2C bus
        '''
        self._addr = i2c_addr
        self._i2c = smbus.SMBus(bus)
        super(DFRobot_BME280_I2C, self).__init__()

    def _write_reg(self, reg, data):
        '''!
          @brief writes data to a register
          @param reg register address
          @param data written data
        '''
        if isinstance(data, int):
            data = [data]
            #logger.info(data)
        self._i2c.write_i2c_block_data(self._addr, reg, data)

    def _read_reg(self, reg, length):
        '''!
          @brief read the data from the register
          @param reg register address
          @param length read data length
          @return read data list
        '''
        return self._i2c.read_i2c_block_data(self._addr, reg, length)

class DFRobot_BME280_SPI(DFRobot_BME280):
    '''!
      @brief define DFRobot_BME280_SPI base class
      @details for using SPI protocol to drive the pressure sensor
    '''

    def __init__(self, cs=8, bus=0, dev=0, speed=500000):
        '''!
          @brief Module SPI communication init
          @param cs cs chip select pin
          @param bus SPI bus
          @param dev SPI device number
          @param speed SPI communication frequency
        '''
        self._cs = cs
        GPIO.setmode(GPIO.BCM)
        GPIO.setwarnings(False)
        GPIO.setup(self._cs, GPIO.OUT, initial=1)
        GPIO.output(self._cs, GPIO.LOW)
        self._spi = spidev.SpiDev()
        self._spi.open(bus, dev)
        self._spi.no_cs = True
        self._spi.max_speed_hz = speed
        super(DFRobot_BME280_SPI, self).__init__()

    def _write_reg(self, reg, data):
        '''!
          @brief writes data to a register
          @param reg register address
          @param data written data
        '''
        if isinstance(data, int):
            data = [data]
            #logger.info(data)
        reg_addr = [reg & 0x7f]
        GPIO.output(self._cs, GPIO.LOW)
        self._spi.xfer(reg_addr)
        self._spi.xfer(data)
        GPIO.output(self._cs, GPIO.HIGH)

    def _read_reg(self, reg, length):
        '''!
          @brief read the data from the register
          @param reg register address
          @param length read data length
          @return read data list
        '''
        reg_addr = [reg | 0x80]
        GPIO.output(self._cs, GPIO.LOW)
        #logger.info(reg_addr)
        self._spi.xfer(reg_addr)
        time.sleep(0.01)
        # self._spi.readbytes(1)
        rslt = self._spi.readbytes(length)
        GPIO.output(self._cs, GPIO.HIGH)
        return rslt


# Binary trees are already defined with this interface:
# class Tree(object):
#   def __init__(self, x):
#     self.value = x
#     self.left = None
#     self.right = None
# input: root of a binary tree
# output: sum of all integers in the tree
def tree_paths_sum(root):
    # initiate a count
    count = 0
    # initiate a queue for node traversal
    queue = []
    # traverse the tree, adding each value to count
    queue.append(root)
    while len(queue) != 0:
        # remove the first node from the queue and add it's value to the count
        current = queue.pop(0)
        count += current.value

        # add all children into the queue
        if current.left is not None:
            queue.append(current.left)
        if current.right is not None:
            queue.append(current.right)

    return count

def tree_paths_sum_dft(root):
    # initiate a count
    count = 0
    if root.left:
        root.left.tree_paths_sum_dft(root.left)
    count += root.value
    if root.right:
        root.right.tree_paths_sum_dft(root.right)

    return count


class MedianFinder:

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.n = 0
        self.nums = []

    def addNum(self, num: int) -> None:
        if not self.n:
            self.nums.append(num)
            self.n += 1
            return

        l = 0
        r = self.n-1
        insert_idx = self.n
        while l<=r:
            m = (l+r)//2
            if self.nums[m]>=num:
                insert_idx = m
                r = m-1
            else:
                l = m+1
        # print(insert_idx)

        self.nums.insert(insert_idx, num)
        self.n += 1
        # print(self.nums)

    def findMedian(self) -> float:
        # print(self.nums)
        m = self.n//2
        if self.n&1:
            return self.nums[m]
        else:
            return (self.nums[m]+self.nums[m-1])/2

s = MedianFinder()
s.addNum(1)
s.addNum(0)
print(s.findMedian())
s.addNum(3)
print(s.findMedian())
s.addNum(2)
s.addNum(100)
print(s.findMedian())

295. 数据流的中位数.py


with open('27-A.txt') as f:
    conts = [int(x) for x in f]
    conts.pop(0)
    trash = []

mid = len(conts)//2
leng = len(conts)
conts = conts+conts
prices = []
for i in range(leng):
    d = conts[i:i+leng]
    print(d)
    price = 0
    pricef = 1000000
    P = mid+i+1
    index = 0
    for k in range(len(d)):
        if k<mid:
        	index = k
        else:
        	index = leng-k
        price += d[k]*index
    prices.append(price)
print(prices)
print(prices.index(min(prices))+1)

2 полугодие/27-A.py


import os
import torch
import torchmetrics
from pathlib import Path

# Huggingface datasets and tokenizers
from tokenizers import Tokenizer
from tokenizers.models import WordLevel
from tokenizers.trainers import WordLevelTrainer
from tokenizers.pre_tokenizers import Whitespace

os.environ["TOKENIZERS_PARALLELISM"] = "true"

def get_device():
    device_count = 1
    if torch.cuda.is_available():
        device = "cuda"
        device_count = torch.cuda.device_count()
    elif torch.backends.mps.is_available():
        device = "mps"
    else:
        device = "cpu"
    print(f"Devices Found: {device_count} x {device}")
    return device, device_count

def get_or_build_tokenizer(file_path, ds, lang):
    tokenizer_path = Path(file_path)
    if not Path.exists(tokenizer_path):
        # Most code taken from: https://huggingface.co/docs/tokenizers/quicktour
        tokenizer = Tokenizer(WordLevel(unk_token='[UNK]'))
        tokenizer.pre_tokenizer = Whitespace()
        trainer = WordLevelTrainer(special_tokens=['[UNK]', '[PAD]', '[SOS]', '[EOS]'], min_frequency=4)
        tokenizer.train_from_iterator((item['translation'][lang] for item in ds), trainer=trainer)
        tokenizer.save(str(tokenizer_path))
    else:
        tokenizer = Tokenizer.from_file(str(tokenizer_path))
    return tokenizer

def causal_mask(seq_len):
    # (seq_len, seq_len)
    return torch.tril(torch.ones(seq_len, seq_len, dtype=torch.int))

def greedy_decode(model, source, source_mask, device='cpu'):
    tokenizer_tgt = model.tgt_tokenizer
    sos_idx = tokenizer_tgt.token_to_id('[SOS]')
    eos_idx = tokenizer_tgt.token_to_id('[EOS]')

    # Precompute the encoder output and reuse it for every step
    encoder_output = model.encode(source, source_mask)
    # Initialize the decoder input with the sos token
    decoder_input = torch.empty(1, 1).fill_(sos_idx).type_as(source).to(device)
    while True:
        if decoder_input.shape[1] == 161:
            break

        # build mask for target
        decoder_mask = causal_mask(decoder_input.shape[1]).type_as(source_mask).to(device)

        # calculate output
        out = model.decode(encoder_output, source_mask, decoder_input, decoder_mask)

        # get next token for input from last token of output
        prob = model.project(out[:, -1, :])
        _, next_word = torch.max(prob, dim=1)
        decoder_input = torch.cat(
            [decoder_input, torch.empty(1, 1).type_as(source).fill_(next_word.item()).to(device)], dim=1
        )

        if next_word == eos_idx:
            break

    return decoder_input.squeeze(0)

def run_validation(model, val_dataloader, print_msg, writer=None, global_step=0, num_examples=2, device='cpu'):
    model.eval()
    count = 0

    source_texts = []
    expected = []
    predicted = []

    try:
        # get the console window width
        with os.popen('stty size', 'r') as console:
            _, console_width = console.read().split()
            console_width = int(console_width)
    except:
        # If we can't get the console width, use 80 as default
        console_width = 80

    with torch.no_grad():
        for batch in val_dataloader:
            count += 1
            encoder_input = batch["encoder_input"].to(device)  # (B, seq_len)
            encoder_mask = batch["encoder_mask"].to(device)  # (B, 1, 1, seq_len)

            # check that the batch size is 1
            assert encoder_input.shape[0] == 1, "Batch size must be 1 for validation"

            model_out = greedy_decode(model, encoder_input, encoder_mask)

            source_text = batch["src_text"][0]
            target_text = batch["tgt_text"][0]
            model_out_text = model.tgt_tokenizer.decode(model_out.detach().cpu().numpy())

            source_texts.append(source_text)
            expected.append(target_text)
            predicted.append(model_out_text)

            # Print the source, target and model output
            print_msg('-' * console_width)
            print_msg(f"{f'SOURCE: ':>12}{source_text}")
            print_msg(f"{f'TARGET: ':>12}{target_text}")
            print_msg(f"{f'PREDICTED: ':>12}{model_out_text}")

            if count == num_examples:
                print_msg('-' * console_width)
                break

    # Evaluate the character error rate
    # Compute the char error rate
    metric = torchmetrics.CharErrorRate()
    cer = metric(predicted, expected)
    print('validation cer', cer)

    # Compute the word error rate
    metric = torchmetrics.WordErrorRate()
    wer = metric(predicted, expected)
    print('validation wer', wer)

    # Compute the BLEU metric
    metric = torchmetrics.BLEUScore()
    bleu = metric(predicted, expected)
    print('validation BLEU', bleu)

    if writer:
        writer.add_scalar('validation cer', cer, global_step)
        writer.flush()

        writer.add_scalar('validation wer', wer, global_step)
        writer.flush()

        writer.add_scalar('validation BLEU', bleu, global_step)
        writer.flush()


import spacy
from spacy.lang.en.stop_words import STOP_WORDS
from heapq import nlargest

class SpacyStrategy:
    def summarize_from_text(self, text):
        raw_text = text
        stopwords = list(STOP_WORDS)
        nlp = spacy.load('en')
        docx = nlp(raw_text)

        # Build Word Frequency
        # word.text is tokenization in spacy
        word_frequencies = {}
        for word in docx:
            if word.text not in stopwords:
                if word.text not in word_frequencies.keys():
                    word_frequencies[word.text] = 1
                else:
                    word_frequencies[word.text] += 1

        maximum_frequency = max(word_frequencies.values())

        for word in word_frequencies.keys():
            word_frequencies[word] = (word_frequencies[word] / maximum_frequency)

        # Sentence Tokens
        sentence_list = [sentence for sentence in docx.sents]

        # Calculate Sentence Score and Ranking
        sentence_scores = {}
        for sent in sentence_list:
            for word in sent:
                if word.text.lower() in word_frequencies.keys():
                    if len(sent.text.split(' ')) < 30:
                        if sent not in sentence_scores.keys():
                            sentence_scores[sent] = word_frequencies[word.text.lower()]
                        else:
                            sentence_scores[sent] += word_frequencies[word.text.lower()]

        # Find N Largest
        summary_sentences = nlargest(7, sentence_scores, key=sentence_scores.get)
        final_sentences = [w.text for w in summary_sentences]
        summary = ' '.join(final_sentences)
        return summary.strip()


def get_tag(text):
    tag = ''
    flag = False
    for char in text:
        if char == "<":
            flag = True
        if flag:
            tag += char
        if char == ">":
            flag = False
            yield tag
            tag = ''

#txt = "<head>nottag</head><title><dog></dog></title><marquee></marquee>edge"
#for tag in get_tag(txt):
 #   print(tag)

def is_matched_html(expr):
    tag_list = []
    for tag in get_tag(expr):
        if not tag[1] == "/":
            tag_list.append(tag)
        else:
            prev_tag = tag_list[len(tag_list) - 1]
            prev_name = prev_tag[1:len(prev_tag) - 1]
            curr_name = tag[2:len(tag) - 1]
            if(prev_name == curr_name):
                tag_list.pop()
            else:
                print(tag_list)
                return False
    return len(tag_list) == 0

#print(is_matched_html(txt))

with open("sampleHTML.txt","r") as text:
    content = text.read()
    #for tag in get_tag(content):
     #   print(tag)
    print(is_matched_html(content))


def func():

	a=int(input("first number is"))
	b=int(input("second number is"))
	c=int(input("third number is"))

	if a==b and b==c:
		print("all are same")
	elif a>b and b>c:
		print("a is greater")
	elif a<b and b<c:
		print("c is greater")
	elif a<b and b>c:
		print("b is greater")
func()


from django.core.exceptions import PermissionDenied

class UserIsAuthorMixin(object):
    """
    Checks that the user is the author of the object. If they are not, raise a
    403 error
    """
    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated and request.user.profile.pk is not self.get_object().author.pk:
            raise PermissionDenied

        return super(UserIsAuthorMixin, self).dispatch(request, *args, **kwargs)


def analyze():
    # test_file = open("../result/test.txt", 'r')
    dict_file = open("../data/dict.txt", 'r')
    correct_file = open("../data/correct.txt", 'r')
    misspell_file = open("../data/misspell.txt", 'r')
    misspell_not_in_dict = open("../data/misspell_not_in_dict.txt","w")

    dict = []
    for line in dict_file.readlines():
        line = line.strip()
        dict.append(line)
    dict_file.close()

    misspell = []
    for line in misspell_file.readlines():
        line = line.strip()
        misspell.append(line)
    misspell_file.close()

    correct = []
    for line in correct_file.readlines():
        line = line.strip()
        correct.append(line)
    correct_file.close()

    correct_exact_match = 0
    wrong_exact_match = 0
    misspell_not_in_dictionary = 0
    misspell_OOV = []
    for i in range(len(misspell)):
        if misspell[i] in dict:
            if misspell[i] == correct[i]:
                correct_exact_match = correct_exact_match + 1
            else:
                wrong_exact_match = wrong_exact_match + 1
        else:
            misspell_not_in_dictionary = misspell_not_in_dictionary + 1
            misspell_OOV.append(misspell[i])

    print("correct exact match ",correct_exact_match)
    print("wrong exact match ",wrong_exact_match)
    print("misspell not in dict: ",misspell_not_in_dictionary)

    for line in misspell_OOV:
        misspell_not_in_dict.write(line)
        misspell_not_in_dict.write("\n")
    misspell_not_in_dict.close()

def main():
    analyze()

if __name__ == "__main__":
    main()

COMP90049 Introduction to Machine Learning/Project/project1/Project1_MuTong_28_04_2019/code/analyze_dataset.py


# SWEA 5105 미로의 거리
from collections import deque

def bfs(x, y):
    q = deque([(x, y)])
    arr[x][y] = 0
    dr = [-1, 1, 0, 0]
    dc = [0, 0, -1, 1]
    while q:
        r, c = q.popleft()
        for i in range(4):
            nr = r + dr[i]
            nc = c + dc[i]
            if 0 <= nr < N and 0 <= nc < N and arr[nr][nc] == -1:
                if maze[nr][nc] == 0:
                    arr[nr][nc] = arr[r][c] + 1
                    q.append((nr, nc))
                elif maze[nr][nc] == 3:
                    arr[nr][nc] = arr[r][c]
                    return

T = int(input())
for tc in range(1, T + 1):
    N = int(input())
    maze = [list(map(int, list(input()))) for _ in range(N)]

    arr = [[-1 for _ in range(N)] for _ in range(N)]

    for i in range(N):
        for j in range(N):
            if maze[i][j] == 2:
                bfs(i, j)

    for i in range(N):
        for j in range(N):
            if maze[i][j] == 3:
                if arr[i][j] == -1:
                    print(f"#{tc} 0")
                else:
                    print(f"#{tc} {arr[i][j]}")


import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np

from functions import noso
from functions import dtdu_backward
from functions import dodt

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dtype = torch.float

class fcn(nn.Module):
    def __init__(self, task, thresh, tau_m, tau_s, num_steps, frate):
        super(fcn, self).__init__()
        self.task = task
        self.thresh = thresh
        self.tau_m = tau_m
        self.tau_s = tau_s
        self.num_steps = num_steps
        self.frate = frate

        if self.task == 'mnist':
            self.cfg_fc = [784, 400, 10]

        elif self.task == 'nmnist':
            self.cfg_fc = [34*34*2, 800, 10]
            self.num_steps = 300

        self.fc1 = nn.Linear(self.cfg_fc[0], self.cfg_fc[1], bias=False).float()
        self.fc2 = nn.Linear(self.cfg_fc[1], self.cfg_fc[2], bias=False).float()
        nn.init.normal_(self.fc1.weight, mean=0, std=np.sqrt(2/self.cfg_fc[0]))
        nn.init.normal_(self.fc2.weight, mean=0, std=np.sqrt(2/self.cfg_fc[1]))

    def forward(self, input, batch_size):
        h1_vm = h1_vs = torch.zeros(batch_size, self.cfg_fc[0], dtype=dtype, device=device)
        h1_um = h1_us = h1_spike = torch.zeros(batch_size, self.cfg_fc[1], dtype=dtype, device=device)
        h1_sav = torch.ones(batch_size, self.cfg_fc[1], dtype=dtype, device=device)

        h2_vm = h2_vs = torch.zeros(batch_size, self.cfg_fc[1], dtype=dtype, device=device)
        h2_um = h2_us = h2_spike = h2_u = torch.zeros(batch_size, self.cfg_fc[2], dtype=dtype, device=device)
        h2_sav = torch.ones(batch_size, self.cfg_fc[2], dtype=dtype, device=device)

        # output
        out_t = self.num_steps * torch.ones(batch_size, self.cfg_fc[2], dtype=dtype, device=device)
        out_u = torch.zeros(batch_size, self.cfg_fc[2], dtype=dtype, device=device)
        sum_sp = torch.zeros(len(self.cfg_fc))

        # for backprop
        h1_v = torch.zeros(batch_size, self.cfg_fc[1], self.cfg_fc[0], dtype=dtype, device=device)
        h2_v = torch.zeros(batch_size, self.cfg_fc[2], self.cfg_fc[1], dtype=dtype, device=device)

        h1_dvdt = torch.zeros(batch_size, self.cfg_fc[1], self.cfg_fc[0], dtype=dtype, device=device)
        h2_dvdt = torch.zeros(batch_size, self.cfg_fc[2], self.cfg_fc[1], dtype=dtype, device=device)

        h1_dtdu = torch.zeros(batch_size, self.cfg_fc[1], dtype=dtype, device=device)
        h2_dtdu = torch.zeros(batch_size, self.cfg_fc[2], dtype=dtype, device=device)

        for step in range(self.num_steps):
            # MNIST input encoding : Poisson spike generation
            if self.task == 'mnist':
                in_spike = (input * self.frate > torch.rand(input.size(), device=device)).view(batch_size, -1).float()

            # N-MNIST input encoding
            elif self.task == 'nmnist':
                in_spike = input[:,:,:,:, step].view(batch_size, -1)

            # Calculation of first hidden layer
            h1_sav, h1_vm, h1_vs, h1_um, h1_us, h1_spike = noso(self.thresh,
                                                                self.tau_m,
                                                                self.tau_s,
                                                                self.fc1,
                                                                in_spike,
                                                                h1_sav,
                                                                h1_vm,
                                                                h1_vs,
                                                                h1_spike,
                                                                outneuron=False)
            # Calculation of output layer
            h2_sav, h2_vm, h2_vs, h2_um, h2_us, h2_u, h2_spike = noso(self.thresh,
                                                                      self.tau_m,
                                                                      self.tau_s,
                                                                      self.fc2,
                                                                      h1_spike,
                                                                      h2_sav,
                                                                      h2_vm,
                                                                      h2_vs,
                                                                      h2_spike,
                                                                      outneuron=True)

            # Recoding of output spike timings and output membrane potential
            out_t += dodt.apply(h2_spike, step, self.num_steps)
            out_u += h2_spike * h2_u

            sum_sp[0] += in_spike.sum().item()
            sum_sp[1] += h1_spike.sum().item()
            sum_sp[2] += h2_spike.sum().item()

            # for backprop
            h1_v += torch.unsqueeze((h1_vm - h1_vs), 1) * torch.unsqueeze(h1_spike, 2)
            h2_v += torch.unsqueeze((h2_vm - h2_vs), 1) * torch.unsqueeze(h2_spike, 2)

            h1_dvdt += torch.unsqueeze((h1_vm / self.tau_m - h1_vs / self.tau_s), 1) * torch.unsqueeze(h1_spike, 2)
            h2_dvdt += torch.unsqueeze((h2_vm / self.tau_m - h2_vs / self.tau_s), 1) * torch.unsqueeze(h2_spike, 2)

            h1_dtdu += dtdu_backward(h1_um, h1_us, h1_spike, self.thresh, self.tau_m, self.tau_s)
            h2_dtdu += dtdu_backward(h2_um, h2_us, h2_spike, self.thresh, self.tau_m, self.tau_s)

            if out_t.max() < self.num_steps:
                return out_t, out_u, sum_sp, [h1_v, h2_v], [h1_dvdt, h2_dvdt], [h1_dtdu, h2_dtdu]
        return out_t, out_u, sum_sp, [h1_v, h2_v], [h1_dvdt, h2_dvdt], [h1_dtdu, h2_dtdu]


import sys
import os
import click
import json
import re
import pandas as pd
import numpy as np

from PIL import ImageTk, Image
from tkinter import Tk

from .modules.interface import AnnotationInterface

@click.version_option("0.0.1")
@click.command()
@click.argument('image_dir', required=True)
@click.argument('bindings_json', required=True)
@click.option('--csv', required=False, default=None,
              help='Location of annotation CSV file')
@click.option('--o', default="output.csv", required=False,
              help='File to save annotations to')
@click.option('--start_i', default=0, required=False)
@click.option('--window_w', default=764, required=False)
@click.option('--window_h', default=1200, required=False)
@click.option('--resize_w', default=382, required=False)
@click.option('--resize_h', default=470, required=False)
@click.option('--filetype', default='jpg', required=False)
def main(image_dir, bindings_json, csv,
         o, start_i,
         window_w, window_h,
         resize_w, resize_h,
         filetype):
    r"""
    EXPERIMENTAL
    Picture annotation tool.
    CSV should contain 'image' column as first column specifying image names
      without the directory.
    """
    print("csv =", csv)
    print("output file=", o)
    print("image_dir=", image_dir)
    print("bindings_json =", bindings_json)
    print("start_i =", start_i)
    print("window_w =", window_w)
    print("window_h =", window_h)
    print("resize_w =", resize_w)
    print("resize_h =", resize_h)
    print("filetype =", filetype)

    with open(bindings_json) as json_file:
        json_dict = json.load(json_file)
    bindings = json_dict['bindings']

    print("bindings =", bindings)
    classes = list(bindings.values())
    print("classes = ", classes)
    filetype_regex = re.compile(f".+\\.{filetype}$")

    # Default behaviour: if CSV is not provided, create empty dataframe with the
    #   categories set to the classes from the keybindings and the number of
    #   rows corresponding to the pictures .
    # If CSV is provided, use that as the annotation df (make sure the
    # keybinding classes are a subset of the annotation classes)
    if csv is None:
        assert not os.path.isfile(o), "csv not specified but output.csv file exists"

        files_in_image_dir = os.listdir(image_dir)
        image_names = list(filter(filetype_regex.match, files_in_image_dir))
        num_pictures = len(image_names)
        annotation_classes = classes
        annotation_values = np.zeros((num_pictures, len(annotation_classes)))
        annotations = pd.concat((pd.DataFrame(image_names), pd.DataFrame(annotation_values)), axis=1)
        annotations.columns = ['image'] + annotation_classes
    else:
        annotations = pd.read_csv(csv)
        annotation_classes = [x for i,x in
                              enumerate(annotations.columns) if i>0]

    root = Tk()

    ai = AnnotationInterface(master=root,
                             bindings=bindings,
                             classes=classes,
                             image_dir=image_dir,
                             data_df=annotations,
                             save_csv=o,
                             window_w = window_w, window_h = window_h,
                             start_i = start_i)

    # Key bindings
    for curr_key, curr_class in zip(bindings.keys(), classes):
        print(f"bound {curr_key} to {curr_class}")
        root.bind(curr_key, ai.pressEvent)
    root.bind("<Return>", ai.prev_picture)
    root.bind("<space>", ai.next_picture)
    root.mainloop()

if __name__ == '__main__':
    args = sys.argv
    if "--help" in args or len(args) == 1:
        print("Picture annotation tool")
    main()


from hmmlearn import hmm
import numpy as np

class CustomHMM:
  def __init__(self):
      def build_hmm():
          model = hmm.GMMHMM(n_components=3, n_mix=3, covariance_type="diag", init_params="t")
          model.transmat_ = np.array([[0.5, 0.5, 0.0],
                                      [0.0, 0.5, 0.5],
                                      [0.0, 0.0, 1.0]])
          return model

      self.hmm_0 = build_hmm()
      self.hmm_1 = build_hmm()

  def fit(self, X_train, y_train):
      # X_train shape(n_instances, n_samples)
      labels = set(y_train)
      if len(labels) != 2:
          raise Exception("y_train doesn't contain 2 classes")
      X_0 = X_train[y_train == 0, :]
      X_1 = X_train[y_train == 1, :]

      self.hmm_0.fit(X_0)
      self.hmm_1.fit(X_1)

  def predict(self, X_test):
      res = []
      for x in X_test:
        x = np.reshape(x,[1,len(x)])
        res.append(0 if self.hmm_0.score(x) > self.hmm_1.score(x) else 1)
      return np.array(res)


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

import time
import os
import h5py
import random
import numpy as np
from PIL import Image

import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import Dataset
from torchvision import models, transforms

from superpixel.slic import SLIC
from lib.make_index import make_data_index, default_loader
from lib.image_process import RandomCropPatches, NonOverlappingCropPatches
from lib.utils import mos_rescale

class IQADataset(Dataset):
    """
    IQA Dataset
    """
    def __init__(self, args, status='train', loader=default_loader):
        """
        :param args: arguments of the model
        :param status: train/val/test
        :param loader: image loader
        """
        self.args = args
        self.status = status
        self.loader = loader

        self.train_database = args.train_database
        self.test_database = args.test_database

        self.image_n_nodes = args.image_n_nodes
        self.patch_n_nodes = args.patch_n_nodes
        self.region_size = args.region_size
        self.ruler = args.ruler
        self.iterate = args.iterate

        self.patch_size = args.patch_size
        self.n_patches_train = args.n_patches_train

        # Train
        train_Info = h5py.File(args.train_info, 'r')
        train_index = train_Info['index']
        train_index = train_index[:, 0 % train_index.shape[1]]
        train_ref_ids = train_Info['ref_ids'][0, :]

        # Test
        test_Info = h5py.File(args.test_info, 'r')
        test_index = test_Info['index']
        test_index = test_index[:, 0 % test_index.shape[1]]
        test_ref_ids = test_Info['ref_ids'][0, :]

        # Get dataset index
        train_index_, test_index_ = [], []
        if 'train' in status:
            print('The Training Set Index is ', train_index, ' and num of Training index is ', len(train_index))
            for i in range(len(train_ref_ids)):
                train_index_.append(i)

            self.index = train_index_
            print("Number of Training Images: {}\n".format(len(self.index)))
            self.mos = train_Info['subjective_scores'][0, self.index]
            # self.mos_std = train_Info['subjective_scoresSTD'][0, self.index]
            im_names = [train_Info[train_Info['im_names'][0, :][i]][()].tobytes()[::2].decode() for i in self.index]

            self.label = []
            self.im_names = []
            self.dis_type = []

            for idx in range(len(self.index)):
                self.im_names.append(os.path.join(args.train_im_dir, im_names[idx]))
                self.label.append(self.mos[idx])

                if self.train_database == 'TID2008' or self.train_database == 'TID2013':
                    self.dis_type.append(int(im_names[idx][4:6]) - 1)

                elif self.train_database == 'KADID':
                    self.dis_type.append(int(im_names[idx][4:6]) - 1)

                elif self.train_database == 'CSIQ':
                    # Distortion Type
                    if 'AWGN' in im_names[idx]:
                        self.dis_type.append(0)
                    elif 'BLUR' in im_names[idx]:
                        self.dis_type.append(1)
                    elif 'contrast' in im_names[idx]:
                        self.dis_type.append(2)
                    elif 'fnoise' in im_names[idx]:
                        self.dis_type.append(3)
                    elif 'JPEG' in im_names[idx]:
                        self.dis_type.append(4)
                    elif 'jpeg2000' in im_names[idx]:
                        self.dis_type.append(5)

                elif self.train_database == 'LIVE':
                    # Distortion Type
                    if 'jp2k' in im_names[idx]:
                        self.dis_type.append(0)
                    elif 'jpeg' in im_names[idx]:
                        self.dis_type.append(1)
                    elif 'wn' in im_names[idx]:
                        self.dis_type.append(2)
                    elif 'gblur' in im_names[idx]:
                        self.dis_type.append(3)
                    elif 'fastfading' in im_names[idx]:
                        self.dis_type.append(4)

                elif self.train_database == 'SIQAD':
                    loc = im_names[idx].find('_')
                    self.dis_type.append(int(im_names[idx][loc + 1]) - 1)

                elif self.train_database == 'SCID':
                    self.dis_type.append(int(im_names[idx][6]) - 1)

        else:
            print('The Testing Set Index is ', test_index, ' and num of test index is ', len(test_index))
            for i in range(len(test_ref_ids)):
                test_index_.append(i)

            self.index = test_index_
            print("Number of Testing Images: {}".format(len(self.index)), '\n')
            self.mos = test_Info['subjective_scores'][0, self.index]
            im_names = [test_Info[test_Info['im_names'][0, :][i]][()].tobytes()[::2].decode() for i in self.index]

            self.label = []
            self.im_names = []
            self.dis_type = []

            for idx in range(len(self.index)):
                self.im_names.append(os.path.join(args.test_im_dir, im_names[idx]))
                self.label.append(self.mos[idx])
                self.dis_type.append(0)

    def __len__(self):
        return len(self.index)

    def __getitem__(self, idx):
        im = self.loader(self.im_names[idx])

        # Get CNN input
        if self.status == 'train':
            cnn_input, patch, patch_graph = RandomCropPatches(im, args=self.args, transforms=True)
        else:
            cnn_input, patch, patch_graph = NonOverlappingCropPatches(im, args=self.args, transforms=True)

        # Get labels
        label = torch.as_tensor([self.label[idx], ])

        # Choose whether to use distortion type or distortion level
        dis_type = torch.as_tensor([self.dis_type[idx], ])

        return patch, patch_graph, cnn_input, label, dis_type


import requests, re, os, json
import urllib.request
import urllib.parse
from bs4 import BeautifulSoup

class Check_Influences(object):
	def __init__(self):
		self.folder_data_name = '../../files/data'
		self.json_name_link = '../../files/name_links.json'
		self.good_json_file_name = '../../files/good_name_links.json'
		self.wiki_base_url = 'https://en.wikipedia.org'

	def bio_table(self, page):
	    # open url with bs
	    name = page.rsplit('/')[-1]
	    file = open(page)
	    page = file.read()
	    file.close()
	    soup = BeautifulSoup(page, "html.parser")
	    # get biography table
	    table = soup.find('table', class_='infobox biography vcard')
	    #print(len(table.find_all('ul', class_='NavContent')))
	    try:
	        # get influencers unordered list
	        influencers = table.find_all('ul', class_='NavContent')[0]
	    except:
	        influencers = []
	    try:
	        # get influenced unordered list
	        influenced = table.find_all('ul', class_='NavContent')[1]
	    except:
	        influenced = []
	    #print(influenced)
	    final_influencers = []
	    final_influenced = []
	    # We want a list of titles of wikipedia pages
	    if influencers != []:
	        for a in influencers.find_all('a'):
	            try:
	                # extract the title
	                final_influencers.append(a.get('title'))
	            except:
	                pass
	    # We want a list of titles of wikipedia pages
	    if influenced != []:
	        for a in influenced.find_all('a'):
	            try:
	                # extract the title
	                final_influenced.append(a.get('title'))
	            except:
	                pass
	    return name, final_influencers,final_influenced

	def get_all_files(self):
		return os.listdir(self.folder_data_name)

	def get_content_of_file(self):
		file = open(self.good_json_file_name)
		content = json.load(file)
		file.close()
		return content

	def has_infobox_table(self, file_name):
		file = open(file_name)
		file_content = file.read()
		file.close()
		if len(re.findall(r'infobox biography vcard', file_content)) != 0:	return True
		return False

	def write_json_file(self, lista):
		content = {"name_links":lista}
		file = open(self.good_json_file_name, 'w')
		json.dump(content, file, indent = 4)
		file.close()

	def get_good_cs(self):
		all_files = self.get_all_files()
		good_cs = []
		for file_name in all_files:
			file_name = self.folder_data_name + '/' + file_name
			if self.has_infobox_table(file_name):	good_cs.append(file_name)
		return good_cs

	def make_good_cs(self, good_cs):
		to_write = []
		for item in good_cs:
			item_name = item.rsplit('/', 1)[-1]
			item_link = self.wiki_base_url + '/' + item_name
			to_write.append({item_name:item_link})
		self.write_json_file(to_write)

	def get_good_cs_files(self):
		json_content = self.get_content_of_file()
		name_file_list = []
		for item in json_content['name_links']:
			name = list(item.keys())[0]
			name_file_list.append(self.folder_data_name + '/' + name)
		return name_file_list

	def check_influences(self, good_cs_filenames):
		count = 0

		for file_name in good_cs_filenames:
			print(file_name)
			#req = urllib.request.Request(, data)
			file = open(file_name)
			respData = file.read()
			file.close()
			paragraphs = re.findall(r'<table class="infobox(.*?)
', str(respData))

			for eachP in paragraphs:
				if "Influences" or "Influenced" in eachP:
					r = re.findall(r'Influence(.*?)Influenced', str(eachP))
					for e in r:
						influenze = re.findall(r'title="(.*?)"', str(e))
						for i in influenze:
							print(file_name + "," + i)

					p = re.findall(r'Influenced(.*?)colspan="2"', str(eachP))
					for el in p:
						influenzati = re.findall(r'title="(.*?)"', str(el))
						for inf in influenzati:
							print(inf + "," + file_name)
		#print(count)

	def main(self):
		all_files = self.get_all_files()
		good_cs = self.get_good_cs()

		print("Good Computer Scientists: %d" % len(good_cs))
		self.make_good_cs(good_cs)
		good_cs_filenames = self.get_good_cs_files()

		#self.check_influences(good_cs_filenames)
		for i in good_cs_filenames:
			print(self.bio_table(i))

if __name__ == '__main__':
	Check_Influences_Obj = Check_Influences()
	Check_Influences_Obj.main()


from rest_framework.test import APITestCase
from ...services.notification_service import NotificationService
from ...api_services.notification import NotificationAPIService
from django.urls import reverse

class TestNotificationAPIServices(APITestCase):

    def setUp(self):
        self.payload = {
            "title": "Winter discount sale started",
            "description": "Enter coupon-code to get flat 10% discount"
        }
        self.notification_id = NotificationService().create(self.payload).id
        self.url = reverse("notification")

    def test_create_notification(self):
        data = {
            "title": "Summer discount sale started",
            "description": "Enter coupon-code to get flat 10% discount"
        }
        notification = NotificationAPIService().create_notification(data)
        self.assertEqual(notification.get('title'), data.get('title'))

    def test_get_all_notification(self):
        notifications = NotificationAPIService().get_all_notification()
        self.assertNotEqual(len(notifications), 0)

    def test_get_notification_by_id(self):
        notification = NotificationAPIService().get_notification_by_id(self.notification_id)
        self.assertEqual(notification.get('id'), self.notification_id)

    def test_delete_notification_by_id(self):
        response = NotificationAPIService().delete_notification(self.notification_id)
        self.assertEqual(response, True)


"""
Author : Animesh Bhasin
Version : 1
Version Date : 16th Nov 2022
Description : This script is to read the data from City of New York API and load into postgres db
"""

import requests
import pandas as pd
from datetime import date, timedelta
from sqlalchemy import create_engine
import os

def main():
    """
    Main function
    """
    crashes_url = 'https://data.cityofnewyork.us/resource/h9gi-nx95.json'
    column_list = ['crash_date', 'crash_time', 'borough', 'zip_code', 'location_latitude', 'location_longitude',
                   'location_location', 'on_street_name', 'off_street_name', 'cross_street_name',
                   'number_of_persons_injured', 'number_of_persons_killed', 'number_of_pedestrians_injured',
                   'number_of_pedestrians_killed', 'number_of_cyclist_injured', 'number_of_cyclist_killed',
                   'number_of_motorist_injured', 'number_of_motorist_killed', 'contributing_factor_vehicle_1',
                   'contributing_factor_vehicle_2', 'contributing_factor_vehicle_3', 'contributing_factor_vehicle_4',
                   'contributing_factor_vehicle_5', 'collision_id', 'vehicle_type_code1', 'vehicle_type_code2',
                   'vehicle_type_code_3', 'vehicle_type_code_4', 'vehicle_type_code_5']

    #Connect to database
    database_url = os.getenv('database_url')
    schema_name = 'crash'
    crashes_table_name = 'crashes'
    engine = create_engine(database_url, echo=False)

    #Find maximum date for which data is present in db( to intelligently incrementally process data)
    crash_start_date = get_date_max_date_in_db(engine, schema_name, crashes_table_name)

    #Find dates to process
    date_list = get_list_of_dates_to_process(crash_start_date)
    print ('Processing dates : {}'.format(date_list))

    #Process data for each date
    for crash_date in date_list:
        print('Processing date : {}'.format(crash_date))
        #Read data from API
        data = read_data_for_crash_date(crash_date, crashes_url)
        if data:
            #convert dictionary data to dataframe
            df, collision_id_list_str = get_data_df(data, column_list)
            #Write data to database
            write_to_db(engine, df, collision_id_list_str, schema_name, crashes_table_name)

def get_date_max_date_in_db(engine, schema_name, table_name):
    """
    Function to get max crash date from database

    Args:
        engine (SQLAlchemyEngine): SQL Alchemy engine created from database
        schema_name(str): Schema Name
        table_name(str): Table Name
    Returns:
        (date): Max date from database
    """
    sql_statement = '''select max(crash_date + interval '1' day) from {}.{};'''.format(schema_name, table_name)

    with engine.connect() as connection:
        result = connection.execute(sql_statement)
        for row in result:
            max_date = row[0]

        if max_date:
            return max_date
        else:
            return (date.today() - timedelta(days=5)).strftime('%Y-%m-%d')

def get_list_of_dates_to_process(start_date):
    """
    Function to list of dates to process

    Args:
        start_date(date): Starting date
    Returns:
        (date): List of dates from start date to yesterday's date
    """
    end_date = date.today() - timedelta(days=2)
    date_df = pd.date_range(start_date, end_date, freq='d')
    return date_df.strftime('%Y-%m-%d').to_list()

def read_data_for_crash_date(crash_date, api_url):
    """
    Function to read json data from API using requests library

    Args:
        crash_date(date): Date for which crashes data is fetched
        api_url(string): API endpoint for crashes data
    Returns:
        data(dict): Return the json response of API containing crashes data for that date
    """
    params = {'crash_date': crash_date}
    r = requests.get(url=api_url, params=params)

    # extracting data in json format
    data = r.json()
    return data

def get_data_df(data, column_list):
    """
    Function to convert json data to dataframe

    Args:
        data(dict): json response of API containing crashes data
        column_list(list): List containing key names to be processed from data dict
    Returns:
        df(Dataframe) : Dataframe containing collisions data
        collision_id_list_str(str) : A String of list of collision ids being processed needed so that
                        existing collision id records can be deleted from database before inserting them
                        to avoid duplicates in database
    """
    df = pd.json_normalize(data, sep='_')

    df = df.drop(columns=[col for col in df if col not in column_list])

    collision_id_list = df['collision_id'].tolist()

    collision_id_list_str = str(collision_id_list)[1:-1]

    return df, collision_id_list_str

def write_to_db(engine, df, collision_id_list_str, schema_name, table_name):
    """
    Function to write the dataframe to database

    Args:
        engine (SQLAlchemyEngine): SQL Alchemy engine created from database
        df(Dataframe) : Dataframe containing collisions data
        collision_id_list_str(str) : A String of list of collision ids being processed needed so that
                existing collision id records can be deleted from database before inserting them
                to avoid duplicates in database
        schema_name(str): Schema Name
        table_name(str): Table Name

    """
    #Delete existing collision id records to ensure deduplication
    sql_statement = 'delete from {}.{} where collision_id in ({})'.format(schema_name, table_name,
                                                                          collision_id_list_str)
    engine.execute(sql_statement)
    df.to_sql(name=table_name, schema=schema_name, con=engine, if_exists='append', index=False)

if __name__ == '__main__':
    main()


# topics = ['动态规划']

from typing import List

class Solution:
    def maxUncrossedLines(self, nums1: List[int], nums2: List[int]) -> int:
        """
        Dynamic Programming
        time O(mn), space O(mn), m 和 n 分别为 nums1 和 nums2 的长度
        """
        m, n = len(nums1), len(nums2)
        dp = [[0] * (n + 1) for _ in range(m + 1)]

        for i, num1 in enumerate(nums1):
            for j, num2 in enumerate(nums2):
                if num1 == num2:
                    dp[i + 1][j + 1] = dp[i][j] + 1
                else:
                    dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j])

        return dp[m][n]


import discord, datetime, time
from discord.ext import commands, tasks
from itertools import cycle

start_time = time.time()

class Events(commands.Cog):
  def __init__(self, client):
        self.client = client
        self.status = cycle(['Bind', 'Haven', 'Ascent','Split','Fracture'])

  @tasks.loop(seconds=10.0)
  async def change_status(self):
        await self.client.change_presence(activity=discord.Game(next(self.status)))

  @commands.Cog.listener()
  async def on_ready(self):
        await self.client.wait_until_ready()
        self.change_status.start()
        print("Event Commands Are Ready!")

  @commands.command(pass_context=True)
  async def uptime(self, ctx):
        current_time = time.time()
        difference = int(round(current_time - start_time))
        text = str(datetime.timedelta(seconds=difference))

def setup(bot):
  bot.add_cog(Events(bot))


# `shutil` module provides many functions of high-level operations
# on files and collections of files.
# https://www.geeksforgeeks.org/python-shutil-copyfile-method/
from shutil import copyfile
from subprocess import Popen
import os, time, stat

bteq_glue_tester_path = r'C:\Users\oseledko.a\Desktop\AWS Database Tester\BTEQToAWSGlue'
tester_batch_fpath = r'Settings\generate-batch.xml'
bteq_batch_fpath = r'Scripts\bteq_import_signature_18_skip_repeat_star_pack_x\generate-batch.xml'
test_gen_bat_file = 'generate-test-json.bat'
clear_test_pyfile = 'clear_test.py'

# Source file path
src_file = bteq_glue_tester_path + '\\' + bteq_batch_fpath

# Destination file path
dst_file = bteq_glue_tester_path + '\\' + tester_batch_fpath

# Checking source file exists and is not a directory
try:
    if not os.path.isfile(src_file):
        print('Path is not a file:', src_file)
        time.sleep(1.5)
        quit()
except FileNotFoundError:
    print('File not found:', src_file)
    time.sleep(1.5)
    quit()

# Checking destination file permissions
if os.path.exists(dst_file) and \
   not os.access(dst_file, os.W_OK):
    os.chmod(dst_file, stat.S_IWRITE)

# Copy the content of `src_file` to `dst_file`
copyfile(src_file, dst_file)

# Clear previous test files
Popen(['py', clear_test_pyfile], cwd=bteq_glue_tester_path)

# Launch test generation *.bat file
Popen(bteq_glue_tester_path + '\\' + test_gen_bat_file, cwd=bteq_glue_tester_path)


#!/usr/bin/env python3

# File: code/remove_email.py

import csv
import club

source = club.EMAIL_JSON

if __name__ == '__main__':
    with open(source, newline='') as inf:
        reader = csv.DictReader(inf)
    print("so far so good")


import os

import pytest
import pandas as pd
from typing import List
from faker import Faker
from dataclasses import dataclass
from prepare_dataset import read_data, train_target

@dataclass()
class TrainingPipelineParams:
    input_data_path: str

@pytest.fixture()
def dataset_path(tmpdir):
    fake = Faker()
    fake.set_arguments('age', {'min_value': 29, 'max_value': 100})
    fake.set_arguments('sex', {'min_value': 0, 'max_value': 1})
    fake.set_arguments('resteg', {'min_value': 0, 'max_value': 3})
    fake.set_arguments('target', {'min_value': 0, 'max_value': 1})

    sample_csv = fake.csv(header=('age', 'sex', 'resteg', 'target'),
                          data_columns=('{{pyint: age}}',
                                        '{{pyint: sex}}',
                                        '{{pyint: resteg}}',
                                        '{{pyint: target}}'),
                          num_rows=10)

    dataset_fio = tmpdir.join('sample.csv')
    dataset_fio.write(sample_csv)

    return dataset_fio

@pytest.fixture()
def train_pipeline_params(dataset_path):
    params = TrainingPipelineParams
    params.input_data_path = dataset_path
    return params

@pytest.fixture()
def target_col():
    return 'target'

def test_load_dataset_using_path(train_pipeline_params, target_col):
    df = read_data(train_pipeline_params)
    assert isinstance(df, pd.DataFrame)
    assert 10 == len(df)
    assert target_col in df.keys()

def test_train_target(tmpdir, train_pipeline_params):
    df = read_data(train_pipeline_params)
    columns = ['age', 'sex', 'resteg']
    train, target = train_target(df, columns, 'target')
    assert (10, 3) == train.shape
    assert pd.Series == type(target)
    assert (10,) == target.shape


"""
EECS 445 - Winter 2017 - Project 2
FER2013 - Skeleton
This file reads the dataset and provides a function `preprocessed_data`
that returns preprocessed images, labels
Usage: python -m data_scripts.fer2013
"""

import numpy as np
from scipy.misc import imresize
from sklearn.utils import resample
import pandas

from utils.config import get, print_if_verbose

class FER2013:
    filename = ''
    data_stored = False
    train_images = np.zeros(0)
    train_labels = np.zeros(0)
    val_images = np.zeros(0)
    val_labels = np.zeros(0)
    test_images = np.zeros(0)
    test_labels = np.zeros(0)

    def __init__(self):
        self.filename = get('DATA.FER_PATH')
        self.data_stored = False

    def get_images_labels(self, matrix):
        image_row_len = len(np.fromstring(matrix[0, 1], dtype=int, sep=' '))
        image_dim = int(np.sqrt(image_row_len))
        # images = np.zeros((matrix.shape[0], image_dim, image_dim))
        labels = matrix[:, 0]
        images = []
        for i in range(matrix.shape[0]):
            image_row = np.fromstring(matrix[i, 1], dtype=int, sep=' ')
            images.append(np.reshape(image_row, (image_dim, image_dim)))
        images = np.array(images)
        return images, labels

    def read_csv(self):
        df = pandas.read_csv(self.filename)
        mat = df.as_matrix()
        train_mat = mat[mat[:, 2] == 'Training', :]
        val_mat = mat[mat[:, 2] == 'PublicTest', :]
        test_mat = mat[mat[:, 2] == 'PrivateTest', :]
        self.train_images, self.train_labels = self.get_images_labels(
            train_mat)
        self.val_images, self.val_labels = self.get_images_labels(val_mat)
        self.test_images, self.test_labels = self.get_images_labels(test_mat)
        self.data_stored = True

    def balance_classes(self, images, labels, count=5000):
        balanced_images, balanced_labels = [], []
        unique_labels = set(labels)
        for l in unique_labels:
            l_idx = np.where(labels == l)[0]
            l_images, l_labels = images[l_idx], labels[l_idx]
            # Consistent resampling to facilitate debugging
            resampled_images, resampled_labels = resample(l_images,
                                                          l_labels,
                                                          n_samples=count,
                                                          random_state=0)
            balanced_images.extend(resampled_images)
            balanced_labels.extend(resampled_labels)
        balanced_images = np.array(balanced_images)
        balanced_labels = np.array(balanced_labels)
        print('---Shuffled images shape: {}'.format(balanced_images.shape))
        print('---Shuffled labels shape: {}'.format(balanced_labels.shape))
        assert(len(balanced_images) == len(balanced_labels))
        shuffle_idx = np.random.permutation(len(balanced_images))
        return balanced_images[shuffle_idx], balanced_labels[shuffle_idx]

    def resize(self, images, new_size=32):
        resized = []
        for i in range(images.shape[0]):
            resized_image = imresize(images[i],
                                     size=(new_size, new_size),
                                     interp='bicubic')
            resized.append(resized_image)
        return np.array(resized)

    def preprocessed_data(self, split, dim=32, one_hot=False, balance_classes=True):
        if not self.data_stored:
            self.read_csv()
        if split == 'train':
            print_if_verbose('Loading train data...')
            images, labels = self.train_images, self.train_labels
            # TODO: Remove blank images
            temp_im = [];
            temp_lb = [];
            for i in range(len(images)):
                if(np.linalg.norm(images[i]) >= 1):
                    temp_im += [images[i]];
                    temp_lb += [labels[i]];
            images = np.array(temp_im);
            labels = np.array(temp_lb);

            if balance_classes:
                images, labels = self.balance_classes(images, labels, 5000)
        elif split == 'val':
            print_if_verbose('Loading validation data...')
            images, labels = self.val_images, self.val_labels
            # TODO: Remove blan                                                                                k images
            temp_im = [];
            temp_lb = [];
            for i in range(len(images)):
                if(np.linalg.norm(images[i]) >= 1):
                    temp_im += [images[i]];
                    temp_lb += [labels[i]];
            images = np.array(temp_im);
            labels = np.array(temp_lb);

            if balance_classes:
                images, labels = self.balance_classes(images, labels, 500)
        elif split == 'test':
            print_if_verbose('Loading test data...')
            images, labels = self.test_images, self.test_labels
            # TODO: Remove blank images
            temp_im = [];
            temp_lb = [];
            for i in range(len(images)):
                if(np.linalg.norm(images[i]) >= 1):
                    temp_im += [images[i]];
                    temp_lb += [labels[i]];
            images = np.array(temp_im);
            labels = np.array(temp_lb);

            if balance_classes:
                images, labels = self.balance_classes(images, labels, 500)
        else:
            print_if_verbose('Invalid input!')
            return
        images = self.resize(images, dim)
        # TODO: Normalize, add dimension, one-hot encoding of labels
        temp_im = [];
        temp_lb = [];
        for i in range(len(images)):
            t_im = images[i];
            mu = np.mean(t_im);
            t_im = np.add(t_im, -mu);
            std = np.std(t_im);
            t_im = np.divide(t_im, std);
            temp_im.append(np.expand_dims(t_im,axis = 2))
            if(one_hot):
                temp_vec = [0, 0, 0, 0, 0, 0, 0];
                temp_vec[labels[i]] = 1
                temp_lb += [temp_vec];
        images = np.array(temp_im);
        if(one_hot):
            labels = np.array(temp_lb);
        print_if_verbose('---Images shape: {}'.format(images.shape))
        print_if_verbose('---Labels shape: {}'.format(labels.shape))
        return images, labels

if __name__ == '__main__':
    data = FER2013()
    images, labels = data.preprocessed_data('train')  # 'train' or 'val' or 'test'


#!/usr/bin/env python3

import os
import sys

def auto_tune_area():
    command = "xdpyinfo | grep dimensions | awk '{print $2;}'"
    w, h = os.popen(command).read().split('x')
    tune_area(w, h)

def tune_area(screen_w, screen_h):
    screen_w = float(screen_w)
    screen_h = float(screen_h)
    with os.popen('xsetwacom --set 8 ResetArea; xsetwacom --get 8 area') as f:
        output = f.read().split()
        print(output)
        x0, y0, x1, y1 = [int(s) for s in output]
    if float(x1) / y1 > screen_w / screen_h:
        new_w = y1 / screen_h * screen_w
        new_x0 = (x1 - new_w) / 2.0
        new_x1 = new_x0 + new_w
        new_y0, new_y1 = y0, y1
    else:
        new_h = x1 / screen_w * screen_h
        new_y0 = (y1 - new_h) / 2.0
        new_y1 = new_y0 + new_h
        new_x0, new_x1 = x0, x1
    os.system('xsetwacom --set 8 area %d %d %d %d' %
            (new_x0, new_y0, new_x1, new_y1))

if __name__ == "__main__":
    if len(sys.argv) == 1:
        auto_tune_area()
    else:
        if sys.argv[1] == '-i':
            w = int(raw_input("screen width: "))
            h = int(raw_input("screen height: "))
            tune_area(w, h)
        else:
            print("Usage:\n  Auto mode:\t%s\nInteractive:\t%s -i")


import copy
import datetime as dt
import decimal
import re

import pytest
import pytz

from google.cloud import bigquery
from google.cloud import bigquery_storage_v1beta1
from google.protobuf import timestamp_pb2

def _to_bq_table_ref(proto_table_ref, partition_suffix=""):
    """Converts protobuf table reference to bigquery table reference.

    Args:
        proto_table_ref (bigquery_storage_v1beta1.types.TableReference):
            A protobuf reference to a table.
        partition_suffix (str):
            An optional suffix to append to the table_id, useful for selecting
            partitions of ingestion-time partitioned tables.

    Returns:
        google.cloud.bigquery.table.TableReference
    """
    return bigquery.table.TableReference.from_api_repr(
        {
            "projectId": proto_table_ref.project_id,
            "datasetId": proto_table_ref.dataset_id,
            "tableId": proto_table_ref.table_id + partition_suffix,
        }
    )

@pytest.mark.parametrize(
    "data_format,expected_schema_type",
    (
        (None, "avro_schema"),  # Default format (Avro).
        (bigquery_storage_v1beta1.enums.DataFormat.AVRO, "avro_schema"),
        (bigquery_storage_v1beta1.enums.DataFormat.ARROW, "arrow_schema"),
    ),
)
def test_read_rows_as_blocks_full_table(
    client, project_id, small_table_reference, data_format, expected_schema_type
):
    session = client.create_read_session(
        small_table_reference,
        "projects/{}".format(project_id),
        format_=data_format,
        requested_streams=1,
    )
    stream_pos = bigquery_storage_v1beta1.types.StreamPosition(
        stream=session.streams[0]
    )
    schema_type = session.WhichOneof("schema")
    assert schema_type == expected_schema_type

    blocks = list(client.read_rows(stream_pos))

    assert len(blocks) > 0
    block = blocks[0]
    assert block.status.estimated_row_count > 0

@pytest.mark.parametrize(
    "data_format,expected_schema_type",
    (
        (bigquery_storage_v1beta1.enums.DataFormat.AVRO, "avro_schema"),
        (bigquery_storage_v1beta1.enums.DataFormat.ARROW, "arrow_schema"),
    ),
)
def test_read_rows_as_rows_full_table(
    client, project_id, small_table_reference, data_format, expected_schema_type
):
    session = client.create_read_session(
        small_table_reference,
        "projects/{}".format(project_id),
        format_=data_format,
        requested_streams=1,
    )
    stream_pos = bigquery_storage_v1beta1.types.StreamPosition(
        stream=session.streams[0]
    )

    rows = list(client.read_rows(stream_pos).rows(session))

    assert len(rows) > 0

@pytest.mark.parametrize(
    "data_format",
    (
        (bigquery_storage_v1beta1.enums.DataFormat.AVRO),
        (bigquery_storage_v1beta1.enums.DataFormat.ARROW),
    ),
)
def test_basic_nonfiltered_read(client, project_id, table_with_data_ref, data_format):
    session = client.create_read_session(
        table_with_data_ref,
        "projects/{}".format(project_id),
        format_=data_format,
        requested_streams=1,
    )
    stream_pos = bigquery_storage_v1beta1.types.StreamPosition(
        stream=session.streams[0]
    )

    rows = list(client.read_rows(stream_pos).rows(session))

    assert len(rows) == 5  # all table rows

def test_filtered_rows_read(client, project_id, table_with_data_ref):
    read_options = bigquery_storage_v1beta1.types.TableReadOptions()
    read_options.row_restriction = "age >= 50"

    session = client.create_read_session(
        table_with_data_ref,
        "projects/{}".format(project_id),
        format_=bigquery_storage_v1beta1.enums.DataFormat.AVRO,
        requested_streams=1,
        read_options=read_options,
    )
    stream_pos = bigquery_storage_v1beta1.types.StreamPosition(
        stream=session.streams[0]
    )

    rows = list(client.read_rows(stream_pos).rows(session))

    assert len(rows) == 2

@pytest.mark.parametrize(
    "data_format",
    (
        (bigquery_storage_v1beta1.enums.DataFormat.AVRO),
        (bigquery_storage_v1beta1.enums.DataFormat.ARROW),
    ),
)
def test_column_selection_read(client, project_id, table_with_data_ref, data_format):
    read_options = bigquery_storage_v1beta1.types.TableReadOptions()
    read_options.selected_fields.append("first_name")
    read_options.selected_fields.append("age")

    session = client.create_read_session(
        table_with_data_ref,
        "projects/{}".format(project_id),
        format_=data_format,
        requested_streams=1,
        read_options=read_options,
    )
    stream_pos = bigquery_storage_v1beta1.types.StreamPosition(
        stream=session.streams[0]
    )

    rows = list(client.read_rows(stream_pos).rows(session))

    for row in rows:
        assert sorted(row.keys()) == ["age", "first_name"]

def test_snapshot(client, project_id, table_with_data_ref, bq_client):
    before_new_data = timestamp_pb2.Timestamp()
    before_new_data.GetCurrentTime()

    # load additional data into the table
    new_data = [
        {u"first_name": u"NewGuyFoo", u"last_name": u"Smith", u"age": 46},
        {u"first_name": u"NewGuyBar", u"last_name": u"Jones", u"age": 30},
    ]

    destination = _to_bq_table_ref(table_with_data_ref)
    bq_client.load_table_from_json(new_data, destination).result()

    # read data using the timestamp before the additional data load
    session = client.create_read_session(
        table_with_data_ref,
        "projects/{}".format(project_id),
        format_=bigquery_storage_v1beta1.enums.DataFormat.AVRO,
        requested_streams=1,
        table_modifiers={"snapshot_time": before_new_data},
    )
    stream_pos = bigquery_storage_v1beta1.types.StreamPosition(
        stream=session.streams[0]
    )

    rows = list(client.read_rows(stream_pos).rows(session))

    # verify that only the data before the timestamp was returned
    assert len(rows) == 5  # all initial records

    for row in rows:
        assert "NewGuy" not in row["first_name"]  # no new records

def test_column_partitioned_table(
    client, project_id, col_partition_table_ref, bq_client
):
    data = [
        {"description": "Tracking established.", "occurred": "2017-02-15"},
        {"description": "Look, a solar eclipse!", "occurred": "2018-02-15"},
        {"description": "Fake solar eclipse reported.", "occurred": "2018-02-15"},
        {"description": "1 day after false eclipse report.", "occurred": "2018-02-16"},
        {"description": "1 year after false eclipse report.", "occurred": "2019-02-15"},
    ]

    destination = _to_bq_table_ref(col_partition_table_ref)
    bq_client.load_table_from_json(data, destination).result()

    # Read from the table with a partition filter specified, and verify that
    # only the expected data is returned.
    read_options = bigquery_storage_v1beta1.types.TableReadOptions()
    read_options.row_restriction = "occurred = '2018-02-15'"

    session = client.create_read_session(
        col_partition_table_ref,
        "projects/{}".format(project_id),
        format_=bigquery_storage_v1beta1.enums.DataFormat.AVRO,
        requested_streams=1,
        read_options=read_options,
    )

    assert session.streams  # there should be some data to fetch

    stream_pos = bigquery_storage_v1beta1.types.StreamPosition(
        stream=session.streams[0]
    )
    rows = list(client.read_rows(stream_pos).rows(session))

    assert len(rows) == 2

    expected_descriptions = ("Look, a solar eclipse!", "Fake solar eclipse reported.")
    for row in rows:
        assert row["occurred"] == dt.date(2018, 2, 15)
        assert row["description"] in expected_descriptions

@pytest.mark.parametrize(
    "data_format",
    (
        (bigquery_storage_v1beta1.enums.DataFormat.AVRO),
        (bigquery_storage_v1beta1.enums.DataFormat.ARROW),
    ),
)
def test_ingestion_time_partitioned_table(
    client, project_id, ingest_partition_table_ref, bq_client, data_format
):
    data = [{"shape": "cigar", "altitude": 1200}, {"shape": "disc", "altitude": 750}]
    destination = _to_bq_table_ref(
        ingest_partition_table_ref, partition_suffix="$20190809"
    )
    bq_client.load_table_from_json(data, destination).result()

    data = [
        {"shape": "sphere", "altitude": 3500},
        {"shape": "doughnut", "altitude": 100},
    ]
    destination = _to_bq_table_ref(
        ingest_partition_table_ref, partition_suffix="$20190810"
    )
    bq_client.load_table_from_json(data, destination).result()

    data = [
        {"shape": "elephant", "altitude": 1},
        {"shape": "rocket", "altitude": 12700},
    ]
    destination = _to_bq_table_ref(
        ingest_partition_table_ref, partition_suffix="$20190811"
    )
    bq_client.load_table_from_json(data, destination).result()

    read_options = bigquery_storage_v1beta1.types.TableReadOptions()
    read_options.row_restriction = "DATE(_PARTITIONTIME) = '2019-08-10'"

    session = client.create_read_session(
        ingest_partition_table_ref,
        "projects/{}".format(project_id),
        format_=data_format,
        requested_streams=1,
        read_options=read_options,
    )

    assert session.streams  # there should be some data to fetch

    stream_pos = bigquery_storage_v1beta1.types.StreamPosition(
        stream=session.streams[0]
    )
    rows = list(client.read_rows(stream_pos).rows(session))
    assert len(rows) == 2

    actual_items = {(row["shape"], row["altitude"]) for row in rows}
    expected_items = {("sphere", 3500), ("doughnut", 100)}
    assert actual_items == expected_items

@pytest.mark.parametrize(
    "data_format",
    (
        (bigquery_storage_v1beta1.enums.DataFormat.AVRO),
        (bigquery_storage_v1beta1.enums.DataFormat.ARROW),
    ),
)
def test_decoding_data_types(
    client, project_id, all_types_table_ref, bq_client, data_format
):
    data = [
        {
            u"string_field": u"Price: € 9.95.",
            u"bytes_field": bigquery._helpers._bytes_to_json(b"byteees"),
            u"int64_field": -1085,
            u"float64_field": -42.195,
            u"numeric_field": "1.4142",
            u"bool_field": True,
            u"geography_field": '{"type": "Point", "coordinates": [-49.3028, 69.0622]}',
            u"person_struct_field": {u"name": u"John", u"age": 42},
            u"timestamp_field": 1565357902.017896,  # 2019-08-09T13:38:22.017896
            u"date_field": u"1995-03-17",
            u"time_field": u"16:24:51",
            u"datetime_field": u"2005-10-26T19:49:41",
            u"string_array_field": [u"foo", u"bar", u"baz"],
        }
    ]

    # Explicit schema is needed to recognize bytes_field as BYTES, and not STRING.
    # Since partial schemas are not supported in load_table_from_json(), a full
    # schema needs to be specified.
    schema = [
        bigquery.SchemaField("string_field", "STRING"),
        bigquery.SchemaField("bytes_field", "BYTES"),
        bigquery.SchemaField("int64_field", "INT64"),
        bigquery.SchemaField("float64_field", "FLOAT64"),
        bigquery.SchemaField("numeric_field", "NUMERIC"),
        bigquery.SchemaField("bool_field", "BOOL"),
        bigquery.SchemaField("geography_field", "GEOGRAPHY"),
        bigquery.SchemaField(
            "person_struct_field",
            "STRUCT",
            fields=(
                bigquery.SchemaField("name", "STRING"),
                bigquery.SchemaField("age", "INT64"),
            ),
        ),
        bigquery.SchemaField("timestamp_field", "TIMESTAMP"),
        bigquery.SchemaField("date_field", "DATE"),
        bigquery.SchemaField("time_field", "TIME"),
        bigquery.SchemaField("datetime_field", "DATETIME"),
        bigquery.SchemaField("string_array_field", "STRING", mode="REPEATED"),
    ]

    job_config = bigquery.LoadJobConfig(schema=schema)
    destination = _to_bq_table_ref(all_types_table_ref)
    bq_client.load_table_from_json(data, destination, job_config=job_config).result()

    session = client.create_read_session(
        all_types_table_ref,
        "projects/{}".format(project_id),
        format_=data_format,
        requested_streams=1,
    )

    assert session.streams  # there should be data available

    stream_pos = bigquery_storage_v1beta1.types.StreamPosition(
        stream=session.streams[0]
    )

    rows = list(client.read_rows(stream_pos).rows(session))

    expected_result = {
        u"string_field": u"Price: € 9.95.",
        u"bytes_field": b"byteees",
        u"int64_field": -1085,
        u"float64_field": -42.195,
        u"numeric_field": decimal.Decimal("1.4142"),
        u"bool_field": True,
        u"geography_field": "POINT(-49.3028 69.0622)",
        u"person_struct_field": {u"name": u"John", u"age": 42},
        u"timestamp_field": dt.datetime(2019, 8, 9, 13, 38, 22, 17896, tzinfo=pytz.UTC),
        u"date_field": dt.date(1995, 3, 17),
        u"time_field": dt.time(16, 24, 51),
        u"string_array_field": [u"foo", u"bar", u"baz"],
    }

    result_copy = copy.copy(rows[0])
    del result_copy["datetime_field"]
    assert result_copy == expected_result

    # Compare datetime separately, AVRO and PYARROW return different object types,
    # although they should both represent the same value.
    # TODO: when fixed, change assertion to assert a datetime instance!
    expected_pattern = re.compile(r"2005-10-26( |T)19:49:41")
    assert expected_pattern.match(str(rows[0]["datetime_field"]))

@pytest.mark.parametrize(
    "data_format",
    (
        (bigquery_storage_v1beta1.enums.DataFormat.AVRO),
        (bigquery_storage_v1beta1.enums.DataFormat.ARROW),
    ),
)
def test_resuming_read_from_offset(client, project_id, data_format):
    shakespeare_ref = bigquery_storage_v1beta1.types.TableReference()
    shakespeare_ref.project_id = project_id
    shakespeare_ref.dataset_id = "public_samples_copy"
    shakespeare_ref.table_id = "shakespeare"

    read_session = client.create_read_session(
        shakespeare_ref,
        "projects/{}".format(project_id),
        format_=data_format,
        requested_streams=1,
    )

    assert read_session.streams  # there should be data available

    stream_pos = bigquery_storage_v1beta1.types.StreamPosition(
        stream=read_session.streams[0], offset=0
    )
    read_rows_stream = client.read_rows(stream_pos)

    # fetch the first two batches of rows
    rows_iter = iter(read_rows_stream)
    some_rows = next(rows_iter)
    more_rows = next(rows_iter)

    # fetch the rest of the rows using the stream offset
    new_stream_pos = bigquery_storage_v1beta1.types.StreamPosition(
        stream=read_session.streams[0], offset=some_rows.row_count + more_rows.row_count
    )
    remaining_rows_count = sum(
        1 for _ in client.read_rows(new_stream_pos).rows(read_session)
    )

    # verify that the counts match
    expected_len = 164656  # total rows in shakespeare table
    actual_len = remaining_rows_count + some_rows.row_count + more_rows.row_count
    assert actual_len == expected_len


from flask import Flask,request,jsonify
import numpy as np
from scipy.signal import find_peaks
import pandas as pd

app = Flask(__name__)

@app.route('/data_process',methods=['GET'])
def data_process():
    dict1={}
    if '[' and ']' and "," in request.args['C1 raw']:
        input_control=request.args['C1 raw'].strip('[').strip(']').split(',')
        #print(input_control)
        input_test=request.args['T1 raw'].strip('[').strip(']').split(',')
    elif ',' in request.args['C1 raw']:
        input_control=request.args['C1 raw'].split(',')
        print(input_control)
        input_test=request.args['T1 raw'].split(',')

    elif '\n' in  request.args['C1 raw']:
        input_control=request.args['C1 raw'].split('\n')
        input_control=input_control[0:-2]
        #print(input_control)
        input_test=request.args['T1 raw'].split('\n')
        input_test = input_test[0:-2]

    else:
        input_control=request.args['C1 raw'].split(' ')
        #print(input_control)
        input_test=request.args['T1 raw'].split(' ')

    peaks1, _ = find_peaks(input_control, prominence=1,width=15)
    peaks2, _ = find_peaks(input_test, prominence=1,width=15)
    if len(peaks1)!=0 or len(peaks2)!=0:
        print('Peak1: {} Peak2: {}'.format(peaks1, peaks2))
        peak_diff = float(input_control[peaks1[0]]) - float(input_test[peaks2[0]])
        percentage_change = (peak_diff) / float(input_control[peaks1[0]]) * 100
        if percentage_change < 19:
            result = 'Negative'
        elif percentage_change >= 19:
            result = 'Positive'

        dict1['Result']=result
        dict1['Peak C1 raw index']=str(peaks1[0])
        dict1['Peak T1 raw index']=str(peaks2[0])
        dict1['Peak C1 raw']=str(input_control[peaks1[0]])
        dict1['Peak T1 raw']=str(input_test[peaks2[0]])
        dict1['Peak Difference']=str(peak_diff)
        dict1['Percentage Change']=str(percentage_change)
        return jsonify(dict1)

    else:

        dict1['Result']='Inconclusive'
        dict1['Peak C1 raw index']='0'
        dict1['Peak T1 raw index']='0'
        dict1['Peak C1 raw']='0'
        dict1['Peak T1 raw']='0'
        dict1['Peak Difference']='0'
        dict1['Percentage Change']='0'
        return jsonify(dict1)

if __name__ == '__main__':
    app.run(debug=True)
#http://127.0.0.1:5000/data_process?control=[123456]&test=[546789]


import random

def married_random():
    boys = ["ali", "reza", "yasin", "benyamin", "mehrdad", "sjjad", "aidin", "shahin"]
    girls = ["sara", "zari", "neda", "homa", "eli", "goli", "mari", "mina"]

    size1 = len(boys)
    size2 = len(girls)
    random_boys = list()
    random_girls = list()

    while len(random_girls) != size1 or len(random_girls) != size2:
        choose_girl = random.randint(0, size1 - 1)
        if girls[choose_girl] not in random_girls:
            random_girls.append(girls[choose_girl])

        choose_boy = random.randint(0, size1 - 1)
        if boys[choose_boy] not in random_boys:
            random_boys.append(boys[choose_boy])

    for i in range(0, size1 - 1):
        print(f"{random_boys[i]} with {random_girls[i]}")

if __name__ == '__main__':
    married_random()


import ply.lex as lex

# List of token names.   This is always required
tokens = (
   'PROP',
   'TRUE',
   'FALSE',
   'NOT',
   'AND',
   'OR',
   'IMPLY',
   'X',
   'U',
   'F',
   'G',
   'R',
   'LPAREN',
   'RPAREN',
)

# Regular expression rules for simple tokens
t_PROP    = r'[a-zA-Z_][a-zA-Z0-9_]*'
t_TRUE    = r'True'
t_FALSE   = r'False'
t_NOT     = r'Not'
t_AND     = r'And'
t_OR      = r'Or'
t_IMPLY   = r'Imply'
t_X       = r'X'
t_U       = r'U'
t_F       = r'F'
t_G       = r'G'
t_R       = r'R'
t_LPAREN  = r'\('
t_RPAREN  = r'\)'

# A regular expression rule with some action code
def t_NUMBER(t):
    r'\d+'
    t.value = int(t.value)
    return t

# Define a rule so we can track line numbers
def t_newline(t):
    r'\n+'
    t.lexer.lineno += len(t.value)

# A string containing ignored characters (spaces and tabs)
t_ignore  = ' \t'

# Error handling rule
def t_error(t):
    print("Illegal character '%s'" % t.value[0])
    t.lexer.skip(1)

# Build the lexer
lexer = lex.lex()


#SIEMPRE SIEMPRE SIEMPRE que abrimos un fichero lo tenemos que CERRAR después
#Si abrimos un fichero con with, python hace el close() por nosotros :))

fichero = 'nombres2.txt' #qué tengo que poner como fichero?
            #qué pasa si no está en la misma carpeta?

import pickle
import csv

with open(fichero) as f:
    print(f) #qué imprime?

with open(fichero) as f: #qué ha pasado?
    print(f.read())

with open(fichero,encoding='utf8') as f: #qué ha pasado?
    print(f.read())

with open(fichero,encoding='utf8') as f: #por qué sale así? cómo podemos solucionarlo?
    for line in f:
        print(line.strip())

with open(fichero,encoding='utf8') as f:
    lines=f.readlines() #lines=[Rocío,Ana...]
    for l in lines:
        print(l)

with open(fichero,mode='a',encoding='utf8') as f:
    f.write('\nIrene') #probar primero sin \n

lista=[1,2,3,4]
with open('lista.pickle',mode='wb') as f:
    pickle.dump(lista, f)

with open('lista.pickle',mode='rb') as f:
    print(pickle.load(f))

with open('lista.pickle',mode='rb') as f:
    x = pickle.load(f)
    x[0]=9
with open('lista.pickle',mode='wb') as f:
    pickle.dump(x,f)
with open('lista.pickle',mode='rb') as f:
    print(pickle.load(f))

import csv

with open('personas.csv', mode='r') as fichero:
    csv_reader = csv.reader(fichero, delimiter=',')#guardamos las líneas del fichero en csv_reader
    contador = 0
    for linea in csv_reader:#¿qué es una línea en un csv?

        if contador == 0:
            cabeceras = linea
            contador += 1

        else:
            print('Línea ' + str(contador) + ':')

        for i in range(len(cabeceras)):
            print(cabeceras[i], '->', linea[i])
            contador += 1

with open('personas.csv', mode='a', encoding='utf8', newline='') as fichero:
    csv_writer = csv.writer(fichero, delimiter=',')
    linea = ['Pablo','Buendía','24']
    csv_writer.writerow(linea)
    linea = ['Enrique', None,'15']
    csv_writer.writerow(linea)

import json
diccionario = {
    'nombre':'Marta',
    'edad':25
}

with open('diccionario.json',mode='w',encoding='utf8') as f:
    json.dump(diccionario,f)

with open('diccionario.json',mode='w',encoding='utf8') as f:
    json.dump(diccionario,f)


"""
Ex 43.

Given the arrays, arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) and arr1 = np.array([2, 3, 4]), find the common items present in both arrays.

Program Description: The objective of this program is to find the common items between two arrays using numpy. The common items get added to a new array.
Note: Use the format np.intersec1d(arr1, arr2) to find the common items.

"""

import numpy as np

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr1 = np.array([2, 3, 4])

arr2 = np.intersect1d(arr,arr1)

print(arr2)


from epyk.core.Page import Report

from epyk.mocks import urls as data_urls

page = Report()
page.headers.dev()

# https://codepen.io/sgratzl/pen/qBWwxKP

records = [
  {"name": "France", "value": 23}
]
page.ui.geo.chartJs.choropleths.world(records, y_columns=["value"], x_axis="name")
page.ui.geo.chartJs.choropleths.us(records, y_columns=["value"], x_axis="name")