from datetime import datetime

value_dict = {
"0": [0,0,0,0],
"1": [0,0,0,1],
"2": [0,0,1,0],
"3": [0,0,1,1],
"4": [0,1,0,0],
"5": [0,1,0,1],
"6": [0,1,1,0],
"7": [0,1,1,1],
"8": [1,0,0,0],
"9": [1,0,0,1],
"A": [1,0,1,0],
"B": [1,0,1,1],
"C": [1,1,0,0],
"D": [1,1,0,1],
"E": [1,1,1,0],
"F": [1,1,1,1]
}

arr_int_values = {}
packet_count = 0

global_bit_arr = []
header = 0

global_versions = []
global_types = []
global_values = []
global_values_v2 = []

global_temp_values = []

def get_bit_arr(unformatted_string):
    global value_dict
    result = []
    for i in range(unformatted_string.__len__()):
        mini_arr = value_dict[unformatted_string[i]]
        result.extend(mini_arr)
    return result

def bit_arr_to_int(arr):
    global arr_int_values
    print("arr len: ", arr.__len__())
    arr_key = "".join(str(arr))
    if arr_key in arr_int_values.keys():
        return arr_int_values[arr_key]
    result = 0
    reverse_arr = arr[::-1]
    for i in range(reverse_arr.__len__()):
        if reverse_arr[i] == 1:
            result+= 2**(i)
    arr_int_values[arr_key] = result
    return result

def unpack_bits(bit_arr):
    #bit_arr = global_bit_arr
    versions = []
    types = []
    values = []
    values_read = 0
    version = bit_arr_to_int(bit_arr[:3])
    type = bit_arr_to_int(bit_arr[3:6])
    print("header: ", header)

    values_read = 6
    value_arr = bit_arr.copy()[6:]
    versions.append(version)
    types.append(type)

    if type == 4:
        more_exists = True
        counter = 0
        literal_value = []
        while more_exists:
            try:
                more_exists = value_arr[counter] == 1
            except:
                more_exists=False
            payload = value_arr[counter +1:counter +5]
            literal_value.extend(payload)
            counter +=5
        values.append(bit_arr_to_int(literal_value))
        values_read +=counter
    else:
        len_id = value_arr[0]
        values_read+=1
        if len_id == 0:
            len_packets = bit_arr_to_int(value_arr[1:16])
            values_read += 15

            counter = 0
            value_arr = value_arr.copy()[16:]
            tot_values_read = 0
            while counter < len_packets:
                new_versions, new_types, new_values, new_values_read = unpack_bits(value_arr[counter:len_packets])
                counter += new_values_read
                versions.extend(new_versions)
                types.extend(types)
                values.extend(new_values)
                values_read += new_values_read

        elif len_id == 1:
            nbr_of_sub_packets = bit_arr_to_int(value_arr[1:12])
            values_read += 11

            counter = 0
            packets = 0
            value_arr = value_arr.copy()[12:]
            while packets < nbr_of_sub_packets:
                new_versions, new_types, new_values, new_values_read = unpack_bits(value_arr.copy()[counter:])
                counter += new_values_read
                packets+=1
                versions.extend(new_versions)
                types.extend(types)
                values.extend(new_values)
                values_read += new_values_read

        else:
            print("something gone terrible wrong")

    return versions, types, values, values_read

def unpack_arrays(new_packets):
    result = []
    print(type(new_packets))
    for n in new_packets:
        print(type(n))

        if type(n) != int:
            result.extend(unpack_arrays(n))
        else:
            result.append(n)
    return result

def unpack_bits2():
    global global_bit_arr
    global header
    global global_versions
    global global_types
    global global_values
    global global_temp_values
    global global_values_v2

    bit_arr = global_bit_arr
    h_check = header

    values = []
    version = bit_arr_to_int(bit_arr[header:header+3])
    header+=3
    type = bit_arr_to_int(bit_arr[header:header+3])
    header+=3
    print("header: ", header)
    print("bit_arr: ", len(bit_arr))

    global_versions.append(version)
    global_types.append(type)
    values_read = 6

    if type == 4:
        more_exists = True
        counter = 0
        literal_value = []
        while more_exists:
            try:
                more_exists = bit_arr[header] == 1
                header +=1
                counter +=1
            except:
                more_exists=False
            payload = bit_arr[header:header +4]
            literal_value.extend(payload)
            header += 4
            counter += 4
        end_val = bit_arr_to_int(literal_value)
        values.append(end_val)
        values_read += counter
    else:
        len_id = bit_arr[header]
        header+=1
        values_read +=1
        if len_id == 0:
            len_packets = bit_arr_to_int(bit_arr[header:header+15])
            header += 15
            values_read += 15
            header_value_before = header
            delta_header = 0
            packets = 0

            while delta_header < len_packets:
                #new_versions, new_types, new_values, new_values_read = unpack_bits(bit_arr[header:header+len_packets-counter])
                unpack_bits2()
                delta_header = header - header_value_before
                packets+=1
            new_packets = global_temp_values[-packets:]
            global_temp_values = global_temp_values[:-packets]
            new_arr = unpack_arrays(new_packets)
            global_temp_values.append(new_arr)

        elif len_id == 1:
            nbr_of_sub_packets = bit_arr_to_int(bit_arr[header:header+11])
            header+=11
            values_read += 11
            counter = 0
            packets = 0
            while packets < nbr_of_sub_packets:
                unpack_bits2()
                packets+=1
            new_packets = global_temp_values[-packets:]
            global_temp_values = global_temp_values[:-packets]
            new_arr = unpack_arrays(new_packets)
            global_temp_values.append(new_arr)
        else:
            print("something gone terrible wrong")

        ##Id checking
        #Sum
        if type!=4:
            g=global_temp_values
            #temp_values = global_temp_values[-1]
            #global_temp_values = global_temp_values[:global_temp_values.__len__()-1]
            g=global_temp_values
            if type == 0:
                arr = global_temp_values[-1]
                packet_sum = sum(global_temp_values[-1])
                del global_temp_values[-1]
                g = global_temp_values

            #product
            elif type == 1:
                packet_sum = 1
                for n in global_temp_values[-1]:
                    packet_sum*=n
                del global_temp_values[-1]

            #minimum
            elif type == 2:
                packet_sum = min(global_temp_values[-1])
                del global_temp_values[-1]

            #max
            elif type == 3:
                packet_sum = max(global_temp_values[-1])
                del global_temp_values[-1]

            #Greater than
            elif type == 5:
                packet_sum = 0
                if global_temp_values[-1][0]>global_temp_values[-1][1]:
                    packet_sum = 1
                del global_temp_values[-1]

            #less than
            elif type == 6:
                packet_sum = 0
                if global_temp_values[-1][0] < global_temp_values[-1][1]:
                    packet_sum = 1
                del global_temp_values[-1]
            #Equal
            elif type == 7:
                packet_sum = 0
                g=global_temp_values
                if global_temp_values[-1][0]==global_temp_values[-1][1]:
                    packet_sum = 1
                del global_temp_values[-1]

            global_values_v2.append(packet_sum)
            values.append(packet_sum)

    global_temp_values.append(values)
    global_values.append(values)
    #return versions, types, values

class Package():
    def __init__(self, version, type, payload):
        self.version = version
        self.type = type
        self.payload = payload

    def get_size(self):
        return self.payload.__len__()

def run():
    global global_bit_arr
    start_time = datetime.now().time()  # time object

    file = open("day16")
    unformatted_string = file.read()
    print(unformatted_string)
    char = unformatted_string[0]

    bit_arr = get_bit_arr(unformatted_string)
    global_bit_arr = bit_arr
    print(bit_arr.__len__())
    #versions, types, values, values_read = unpack_bits(bit_arr)
    unpack_bits2()
    print(global_versions)
    print(global_types)
    print(global_values)
    print(sum(global_versions))
    print("sum_v2_temp: ", global_temp_values)
    print("sum_v2: ", sum(global_values_v2))

    end_time = datetime.now().time()  # time object
    delta_seconds = end_time.microsecond - start_time.microsecond
    print(delta_seconds)
    #Should do class packet with payload... Then take the class and payload to generate new package and continue... No ram eating

run()


import numpy as np
import matplotlib.pyplot as plt
import os

np.random.seed(0)

# Draw 1000 samples from a very long
# period sinusoid
t = np.linspace(0, 1, 300)
z = 1 + 0.1 * np.sin(
    2 * np.pi / 10 * t.reshape(1, -1)
    + 2 * np.pi * np.random.random(size=(1000, 1))
)

# Normalize each sample to the mean
znorm = z / np.mean(z, axis=1).reshape(-1, 1)

# Figure setup
fig, ax = plt.subplots(2, figsize=(12, 8), sharex=True)
ax[1].set_xlabel("time", fontsize=18)
ax[0].set_ylabel("true flux", fontsize=18)
ax[1].set_ylabel("normalized flux", fontsize=18)
ax[0].margins(0, 0.1)
ax[1].margins(0, 0.1)
ax[0].set_rasterization_zorder(2)
ax[1].set_rasterization_zorder(2)

# Plot
for k in range(len(z)):
    ax[0].plot(t, z[k], color="C0", lw=1, alpha=0.1)
    ax[1].plot(t, znorm[k], color="C0", lw=1, alpha=0.1)

# We're done
fig.savefig(
    os.path.abspath(__file__).replace(".py", ".pdf"),
    bbox_inches="tight",
    dpi=300,
)


import cv2
import numpy as np
import x

def getContours(image):  # 寻找二维码边框轮廓
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # 重载为灰度图，单通道比较省事而且比较方便操作，更有利于分辨边框
    gray = cv2.blur(gray, (5, 5), 0)  # 将图片进行滤波处理，目的是排除一些椒盐噪音（除去雪花）。参数为（源图片，（分块像素规格），滤波阈值参数）
    ret, gray = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)  # 进行黑白二值化处理，以255的一半（127）为界限，进行二值化
    contours, hierachy = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # 第二个参数表示轮廓检索模式，第三个表示轮廓的存储方法（本次实验只需要确定一个矩形，因此SIMPLE参数就够用了）
    # contours返回轮廓本身的四个顶点信息，hierachy在本次项目中由于只存在一个边框，所以返回值用处不大。
    return contours, hierachy

def detectContours(vec):#判断定位点位置（三个大的定位码需要确定）
    distance1 = np.sqrt((vec[0] - vec[2]) ** 2 + (vec[1] - vec[3]) ** 2)
    distance2 = np.sqrt((vec[0] - vec[4]) ** 2 + (vec[1] - vec[5]) ** 2)
    distance3 = np.sqrt((vec[2] - vec[4]) ** 2 + (vec[3] - vec[5]) ** 2)
    if sum((distance1, distance2, distance3)) / 3 < 3:
        # print(sum((distance1,distance2,distance3))/3)
        return True
    return False

def getCenter(contours, i):  # 获取中心点
    M = cv2.moments(contours[i])  # 计算中心矩
    x = (M['m10'] / M['m00'])
    y = (M['m01'] / M['m00'])
    # x，y为中心点像素的坐标
    return x, y

def mask(mat, row, col, count):  # 掩膜  000型掩膜  (i+j)%2==0
    if (mat[row][col][count] == 255):
        mat[row][col][count] = 0
    else:
        mat[row][col][count] = 255
    return

def demask(mat, row, col, count, thre):
    if (mat[row][col][count] > thre):
        mat[row][col][count] = 0
    else:
        mat[row][col][count] = 255

def drawLocPoint(mat):
    # 创建定位点
    for i in range(x.border, x.border + x.blackWhite):
        # 左上角
        mat[i][x.border] = 0  # 竖
        mat[i][x.border + x.blackWhite - 1] = 0
        mat[i][x.border + 1] = 0
        mat[i][x.border + x.blackWhite - 1 - 1] = 0
        mat[x.border][i] = 0  # 横
        mat[x.border + x.blackWhite - 1][i] = 0
        mat[x.border + 1][i] = 0  # 横
        mat[x.border + x.blackWhite - 1 - 1][i] = 0
        # 左下角
        mat[x.width - x.border - x.blackWhite][i] = 0
        mat[x.width - x.border - 1][i] = 0
        mat[x.width - x.border - x.blackWhite + 1][i] = 0
        mat[x.width - x.border - 1 - 1][i] = 0
        mat[x.width - i - 1][x.border] = 0
        mat[x.width - i - 1][x.border + x.blackWhite - 1] = 0
        mat[x.width - i - 1][x.border + 1] = 0
        mat[x.width - i - 1][x.border + x.blackWhite - 1 - 1] = 0
        # 右上角和左上角对称
        mat[i][x.width - x.border - x.blackWhite] = 0
        mat[i][x.width - x.border - 1] = 0
        mat[i][x.width - x.border - x.blackWhite + 1] = 0
        mat[i][x.width - x.border - 1 - 1] = 0
        mat[x.border][x.width - i - 1] = 0
        mat[x.border + x.blackWhite - 1][x.width - i - 1] = 0
        mat[x.border + 1][x.width - i - 1] = 0
        mat[x.border + x.blackWhite - 1 - 1][x.width - i - 1] = 0
    for i in range(x.border, x.border + x.sblackWhite+1):
        # 右下角
        mat[x.width - i - 1][x.width - x.border - 1] = 0  # 竖
        mat[x.width - i - 1][x.width - (x.border + x.sblackWhite)] = 0
        mat[x.width - x.border - 1][x.width - i - 1] = 0  # 横
        mat[x.width - (x.border + x.sblackWhite)][x.width - i - 1] = 0
        mat[x.width - i - 1][x.width - x.border - 1-1] = 0  # 竖
        mat[x.width - i - 1][x.width - (x.border + x.sblackWhite)-1] = 0
        mat[x.width - x.border - 1-1][x.width - i - 1] = 0  # 横
        mat[x.width - (x.border + x.sblackWhite)-1][x.width - i - 1] = 0

    for i in range(x.border + 5, x.border + 5 + 13):
        for j in range(x.border + 5, x.border + 5 + 13):
            mat[i][j] = 0
            mat[i][x.width - j - 1] = 0
            mat[x.width - j - 1][i] = 0

    for i in range(x.border + 5, x.border + 5 + 7):
        for j in range(x.border + 5, x.border + 5 + 7):
            mat[x.width - i - 1][x.width - j - 1] = 0
    return

def encode(mat, binstring):
    row = x.border  # 记录绘制到第几行
    col = x.border + x.locWidth  # 记录绘制到第几列
    count = 0  # rgb通道变换
    while binstring and row < x.width - x.border:
        if row < x.border + x.locWidth:  # 上端两个定位码之间
            if int(binstring[0]) == 1:
                mat[row][col][count] = 0
            else:
                mat[row][col][count] = 255
            if (col + row) % 2 == 0:
                mask(mat, row, col, count)
            count += 1
            if count >= 3:
                count -= 3
                col += 1
                if col > x.width - x.border - x.locWidth - 1:
                    if row != x.border + x.locWidth - 1:
                        col = x.border + x.locWidth
                    else:
                        col = x.border
                    row += 1
            binstring = binstring[1:]
        elif row < x.width - x.border - x.locWidth:  # 二维码的肚子部份
            if int(binstring[0]) == 1:
                mat[row][col][count] = 0
            else:
                mat[row][col][count] = 255
            if (col + row) % 2 == 0:
                mask(mat, row, col, count)
            count += 1
            if count >= 3:
                count -= 3
                col += 1
                if col > x.width - x.border - 1:
                    if row != x.width - x.border - x.locWidth - 1:
                        col = x.border
                    else:
                        col = x.border + x.locWidth
                    row += 1
            binstring = binstring[1:]
        elif row < x.width - x.border - x.sLocWidth:  # 下端定位码部分
            if int(binstring[0]) == 1:
                mat[row][col][count] = 0
            else:
                mat[row][col][count] = 255
            if (col + row) % 2 == 0:
                mask(mat, row, col, count)
            count += 1
            if count >= 3:
                count -= 3
                col += 1
                if col > x.width - x.border - 1:
                    col = x.border + x.locWidth
                    row += 1
            binstring = binstring[1:]
        else:
            if int(binstring[0]) == 1:
                mat[row][col][count] = 0
            else:
                mat[row][col][count] = 255
            if (col + row) % 2 == 0:
                mask(mat, row, col, count)
            count += 1
            if count >= 3:
                count -= 3
                col += 1
                if col > x.width - x.sLocWidth - x.border - 1:
                    col = x.border + x.locWidth
                    row += 1
            binstring = binstring[1:]
    return binstring

def computeRate1(contours, i, j): #判断是否存在大回型
    area1 = cv2.contourArea(contours[i])
    area2 = cv2.contourArea(contours[j])
    if area2 == 0:
        return False
    ratio = area1 * 1.0 / area2
    # if abs(ratio - 49.0 / 25) < 1:
    if abs(ratio - 529.0 / 441) < 1:
        # print(abs(ratio-49.0/25))
        return True
    return False

def computeRate2(contours, i, j):#判断是否存在小回型
    area1 = cv2.contourArea(contours[i])
    area2 = cv2.contourArea(contours[j])
    if area2 == 0:
        return False
    ratio = area1 * 1.0 / area2
    # if abs(ratio - 25.0 / 9) < 1:
    if abs(ratio - 441.0/169) < 1:
        # print(abs(ratio-25.0/9))
        return True
    return False

def computeRate3(contours, i, j):#判断是否存在小回型
    area1 = cv2.contourArea(contours[i])
    area2 = cv2.contourArea(contours[j])
    if area2 == 0:
        return False
    ratio = area1 * 1.0 / area2
    # if abs(ratio - 25.0 / 9) < 1:
    if abs(ratio-324.0/196) < 1:
        # print(abs(ratio-25.0/9))
        return True
    return False

def computeRate4(contours, i, j):  # 判断是否存在小回型
    area1 = cv2.contourArea(contours[i])
    area2 = cv2.contourArea(contours[j])
    if area2 == 0:
        return False
    ratio = area1 * 1.0 / area2
    # if abs(ratio - 25.0 / 9) < 1:
    if abs(ratio - 196.0 / 49) < 1:
        # print(abs(ratio-25.0/9))
        return True
    return False

def genImage(mat, width, filename):  # 放大图片，由于cv2.resize放大图片一定会出现模糊情况，使用直接对nparray放大。
    img = np.zeros((width, width, 3), dtype=np.uint8)
    pwidth = 3
    #确定每一个像素的rgb三色值，放大倍数为10倍
    for i in range(width):
        normali = i // pwidth
        for j in range(width):
            normalj = j // pwidth
            if (normali < len(mat) and normalj < len(mat)):
                img[i][j][0] = (mat[normali][normalj][0])
                img[i][j][1] = (mat[normali][normalj][1])
                img[i][j][2] = (mat[normali][normalj][2])
            # img[i][j][0]=int(mat[int(normali)][int(normalj)][0])
            # img[i][j][1]=int(mat[int(normali)][int(normalj)][1])
            # img[i][j][2]=int(mat[int(normali)][int(normalj)][2])
    # img = cv2.resize(mat, (800,800), interpolation=cv2.INTER_LINEAR)
    cv2.imwrite(filename, img)
    return

def genBlankFrame(): #绘制起始标志图，避免采集无用图的情况发生
    mat = np.full((x.width, x.width, 3), 255, dtype=np.uint8)
    drawLocPoint(mat)
    genImage(mat, x.width * 3, "./video/" + str(0) + ".png")

def judgeOrder(rec): #由于视频可能翻转，按照顺时针确定四个定位点的位置，然后将图片校正
    if len(rec) < 4:
        return -1, -1, -1, -1
    max = 0
    index = 0
    for i in range(len(rec)):#取出四个定位点中最右端的点index
        if (rec[i][0] > max):
            max = rec[i][0]
            index = i
    for i in range(len(rec)):
        if i == index:
            continue
        for j in range(i + 1, len(rec)):
            if j == index:
                continue
            for k in range(j + 1, len(rec)):
                if k == index:
                    continue
                #取三个点两两的距离
                distance1 = np.sqrt((rec[i][0] - rec[j][0]) ** 2 + (rec[i][1] - rec[j][1]) ** 2)
                distance2 = np.sqrt((rec[i][0] - rec[k][0]) ** 2 + (rec[i][1] - rec[k][1]) ** 2)
                distance3 = np.sqrt((rec[j][0] - rec[k][0]) ** 2 + (rec[j][1] - rec[k][1]) ** 2)
                #利用余弦定理计算 余弦值为0的∠为直角，从而确定四个点的方位顺序index为最小点的位置。
                if abs(np.square(distance1) + np.square(distance2) - np.square(distance3)) / (
                        2 * distance1 * distance2) < 0.1:
                    if rec[j][0] < rec[k][0]:
                        return i, j, index, k
                    else:
                        return i, k, index, j
                elif abs(np.square(distance1) + np.square(distance3) - np.square(distance2)) / (
                        2 * distance1 * distance3) < 0.1:
                    if rec[i][0] < rec[k][0]:
                        return j, i, index, k
                    else:
                        return j, k, index, i
                elif abs(np.square(distance2) + np.square(distance3) - np.square(distance1)) / (
                        2 * distance2 * distance3) < 0.1:
                    if rec[i][0] < rec[j][0]:
                        return k, i, index, j
                    else:
                        return k, j, index, i
    return -1, -1, -1, -1

def find(image, contours, hierachy, root=0):
    width = x.width - 8
    rec = []
    #确定四个定位点，定位点为回字型，因此轮廓应该是有嵌套的孩子和孙子的
    for i in range(len(hierachy)):
        child = hierachy[i][2]
        grandchild = hierachy[child][2]
        if child != -1 and grandchild != -1:
            # x1, y1 = getCenter(contours, i)
            # print(x1)
            # print(y1)
            if (computeRate1(contours, i, child) and computeRate2(contours, child, grandchild)) or (computeRate3(contours, i, child) and computeRate4(contours, child, grandchild)): #查找回字形是否存在
                x1, y1 = getCenter(contours, i)
                x2, y2 = getCenter(contours, child)
                x3, y3 = getCenter(contours, grandchild)
                if detectContours([x1, y1, x2, y2, x3, y3, i, child, grandchild]):  #根据三个大的定位点查找轮廓
                    rec.append([x1, y1, x2, y2, x3, y3, i, child, grandchild])

    # print((rec))
    if len(rec) < 4:
        cv2.imwrite("wrong.png", image)
        print("二维码定位点数量不足！")
    i, j, k, t = judgeOrder(rec)
    if i == -1 or j == -1 or k == -1 or t == -1:
        print("定位点有问题，不符合二维码格式！")
        return

    vertexSrc = np.array(
        [[rec[i][0], rec[i][1]], [rec[j][0], rec[j][1]], [rec[k][0], rec[k][1]], [rec[t][0], rec[t][1]]],
        dtype="float32")
    # print(vertexSrc)
    # vertexWarp = np.array([[69.5, 69.5], [69.5, 659.5], [694.5, 694.5], [659.5, 69.5]], dtype="float32")
    vertexWarp = np.array([[ 34, 34.], [ 35,750.], [759,759.], [750,35.]], dtype="float32")
    M = cv2.getPerspectiveTransform(vertexSrc, vertexWarp)
    out = cv2.warpPerspective(image, M, (width * 3, width * 3))
    cv2.imwrite("tem.png", out)
    return out


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

# In[1]:

import pandas as pd, matplotlib as mpl
import os, ast # This is to read in a list of valid columns

# In[2]:

# Dependency checks
import sys
print("Developed using: ")
print("Python version: 3.6.0")
print("Pandas version: 0.24.2")
print("Matplotlib version: 2.1.1")
print("\n")
print("Current versions:")
print("Python version: "+sys.version[:5]) # 3.6.0
print("Pandas version: "+pd.__version__) # 0.23.4
print("Matplotlib version: "+mpl.__version__) # 2.1.1

# # Create sample.csv files for each Batch folder and aggregate master file

# In[3]:

# I manuallly trimmed some columns
# cols = str(df.columns.values.tolist())
# f = open("filtered_columns.txt", "w")
# f.write(cols)
# f.close()

# Here is a list of the bad columns
# bad_cols = str(df.loc[:,~df.columns.isin(cols)].columns.values.tolist())
# f = open("bad_columns.txt","w")
# f.write(bad_cols)
# f.close()

# Here is a list of columns to keep everything in order for edited_survey_data.csv
# final_cols = str(pd.read_csv("transformed_data/edited_survey_data.csv").columns.values.tolist())
# f = open("col_order.txt","w")
# f.write(final_cols)
# f.close()

# In[4]:

latest_batch = 4

# Our survey has an extra loop function. I created a list of valid columns
# file = open("filtered_columns.txt","r")
# good_cols = ast.literal_eval(file.read())
# file.close()

file = open("bad_columns.txt","r")
bad_cols = ast.literal_eval(file.read())
file.close()

# Create a master dataframe to hold all the batches
df_master = pd.DataFrame([])

for i in range(1,latest_batch+1):
# Set batch number for file naming
    batch_n = str(i)
    # Read in batch data file
    fname = "raw_data/Batch"+batch_n+"/qual.csv"
    df = pd.read_csv(fname,sep=",")[2:] # This index trims the first two rows which are metadata

    # In the fourth batch, we changed some column names (e.g. care_1 -> care)
    if int(batch_n) < 4:
        df.rename(columns = lambda x: x[:-2] if x.find("care_1")>0 else x,inplace=True)
        df.rename(columns = lambda x: x[:-2] if x.find("surprise_1")>0 else x,inplace=True)
    # Save a list of the renamed columns
    good_cols = df.loc[:,~(df.columns.isin(bad_cols))].columns
    # Save sample.csv which has trimmed columns
    df.loc[:,good_cols].to_csv("raw_data/Batch"+batch_n+"/sample.csv",index=False)
    # Smash this table against the existing table
    df_master = pd.concat([df_master,df[good_cols]],sort=False)

# Grab the preset column order
file = open("col_order.txt","r")
col_order = ast.literal_eval(file.read())
file.close()

# Drop the qualtrics identifying data
df_master["duration"] = df_master["Duration (in seconds)"] # Rename b/c it is easier
df_master.loc[:,col_order].to_csv("transformed_data/edited_survey_data.csv",sep=",",index=False)

# Save an unedited data file
df_master.to_csv("transformed_data/un-edited_survey_data.csv",sep=",",index=False)

# # Create data for analysis

# ## Recode variables to binary/numeric

# In[5]:

# Make a new df for the table
# Table columns will be [subject, fb_use,fb_fight,ig_use,ig_fight,pol,edu]
df_final = pd.DataFrame([])
df_final["subject"] = [x for x in df_master.reward_code] # Set subject number to reward code for now

# In[6]:

# Set column name - platform mapping
pref = [["fb_", "Facebook"],
        ["ig_", "Instagram"],
        ["fbm_", "Facebook Messenger"],
        ["wa_", "WhatsApp"],
        ["wc_","WeChat"],
        ["tblr_", "Tumblr"],
        ["red_", "Reddit"],
        ["sc_", "Snapchat"],
        ["yt_", "Youtube"],
        ["twt_", "Twitter"],]

# In[7]:

# Generate a list of all the platforms
platforms = [x[1] for x in pref]

# ## Mapping functions
# These reshape the values in the master data sheet into a table we can run analysis on

# In[8]:

# Find education level
def evaluate_eduation_level(_df):
    d = {
        "Some high school":1,
        "High school degree or equivalent (e.g. GED)":2,
        "Some college":3,
        "Associate degree (e.g. AA, AS)":4,
        "Bachelor's degree (e.g. BA, BS)":5,
        "Master's degree (e.g. MA, MS, MEd)":6,
        "Professional degree (e.g. MD, DDS, DVM)":7,
        "Doctorate (e.g. PhD, EdD)":8,
    }
    return _df.education.replace(d).values

# In[9]:

# Find if a string paramater (platform) was used by a participant
def evaluate_used_platforms(_df, platform):
    # Create a list of lists of individual platforms
    platforms = _df.Platform_Use.str.split(",")

    # Return a list of binaries (True if platform found, else False)
    return [1 if platform in x else 0 for x in platforms ]

# In[10]:

# Find political leaning
def evaluate_political_leaning(_df):
    # Create a dictionary to hold the scale values
    d = {
        "Extremely liberal":1,
        "Very liberal":2,
        "Somewhat liberal":3,
        "Moderate":4,
        "Somewhat conservative":5,
        "Very conservative":6,
        "Extremely conservative":7,
    }
    return _df.political_leaning.replace(d).values # Use values property to ignore indexing problems

# In[11]:

# Find if a string paramater (platform) was used to fight by a participant
def evaluate_fighting_platforms(_df, platform):
    # Create a list of lists of individual platforms
    platforms = _df.Platform_Fights.str.split(",")
    # Return a list of binaries (True if platform found, else False)
    return [1 if platform in x else 0 for x in platforms ]

# ## Populate the table with values

# In[12]:

suf = "_use"
for item in pref:
    df_final[item[0]+suf] = evaluate_used_platforms(df_master,item[1])

# In[13]:

suf = "_fight"

for item in pref:
    df_final[item[0]+suf] = evaluate_fighting_platforms(df_master,item[1])

# In[14]:

df_final["edu"] = evaluate_eduation_level(df_master)
df_final["pol"] = evaluate_political_leaning(df_master)

# In[15]:

df_final = df_final.astype("int64",errors="ignore") # Change binary to numeric, ignore values that can't be changed to int (NA, eduation)

# In[16]:

df_final.set_index("subject",inplace=True)

# In[17]:

df_final.head()

# In[18]:

df_final.to_csv("transformed_data/wide_table.csv",sep=",")

# In[ ]:

# ## Make long data format

# In[19]:

df_fmt = pd.DataFrame([])

# In[20]:

for user in df_master.reward_code:
    temp_df = pd.DataFrame([])
    temp_df["platform"] = [x for x in platforms]
    temp_df["subject"] = user
    df_fmt = pd.concat([df_fmt,temp_df])

# In[21]:

df_fmt.head()

# In[23]:

def find_platform_use(plat_user):
    # Find the platforms used by each user
    plats = df_master.loc[(df_master.reward_code==plat_user[1]),"Platform_Use"].str.split(",").values
    # If the platform is in the platform_use column, return 1
    if (plat_user[0] in plats[0]):
        return 1
    else:
        return 0

# In[24]:

df_fmt["use"] = df_fmt.apply(find_platform_use,axis=1)

# In[25]:

def find_platform_fights(plat_user):
    # plat_user is a list with Platform and Subject
    # Find the platforms used by each user
    plats = df_master.loc[(df_master.reward_code==plat_user[1]),"Platform_Fights"].str.split(",").values
    # If the platform is in the platform_use column, return 1
    if (plat_user[0] in plats[0]):
        return 1
    else:
        return 0

# In[26]:

df_fmt["fight"] = df_fmt.apply(find_platform_fights,axis=1)

# In[27]:

# df_master.columns

# In[28]:

df_fmt["pol"] = df_fmt.subject.apply(lambda x: df_final.loc[df_final.index==x,"pol"].values[0])

# In[29]:

df_fmt["edu"] = df_fmt.subject.apply(lambda x: df_final.loc[df_final.index==x,"edu"].values[0])

# In[30]:

df_fmt.to_csv("transformed_data/long_table.csv",sep=",",index=False)

# ## Visualizations

# In[31]:

import matplotlib.pyplot as plt
import numpy as np
get_ipython().run_line_magic('matplotlib', 'inline')

# In[32]:

# What are fight / use rates like

# In[33]:

use_cols = df_final.columns[df_final.columns.str.contains("use")]
fight_cols = df_final.columns[df_final.columns.str.contains("fight")]

# In[34]:

df_final[use_cols].sum()

# In[38]:

fig = plt.figure(num=None, figsize=(16,7))

use = df_final[use_cols].sum()
fights = df_final[fight_cols].sum()

N = 10 #number of platforms
ind = np.arange(N)

p1 = plt.bar(ind, use.values, color="c")
p2 = plt.bar(ind, fights.values, color="red")

plt.ylabel("Count")
plt.title("Platform Usage and Fights")
plt.xlabel("Platforms")
plt.xticks(ind, platforms)
y_max = use.max()+2
plt.yticks(np.arange(0,y_max,step=4))
plt.legend(["Use","Fights"])

plt.show()

fig.savefig("platform_use.png")

# In[ ]:

# In[ ]:


from __future__ import annotations

import json
import re
from subprocess import DEVNULL, PIPE
import sys
import tempfile
from dataclasses import dataclass
from datetime import date
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union

from anitracker import logger, ffprobe_cmd, ffmpeg_cmd
from anitracker.media.media import BaseAnime, BaseCollection
from anitracker.utilities import UserStatus
from anitracker.utilities import subprocess

if TYPE_CHECKING:
    from anitracker.sync import AniList

__all__ = (
    "Anime",
    "AnimeCollection",
    "AnimeFile",
    "SubtitleTrack",
)

def ffprobe_data(file: str) -> Dict:
    args = [ffprobe_cmd, "-show_format", "-show_streams", "-of", "json", file]
    logger.info(f"Running ffprobe command {args}")
    # I hate windows
    stdin = None
    shell = False
    if sys.platform.startswith("win32"):
        stdin = DEVNULL
        shell = True

    out, _ = subprocess.run(
        args,
        stdout=PIPE,
        stdin=stdin,
        shell=shell,
    )

    if out:
        return json.loads(out)
    else:
        return {}

@dataclass
class NyaaResult:
    title: str
    link: str
    magnet: str
    size: str
    upload_date: str
    seeders: int
    leechers: int
    downloads: int

    def __repr__(self) -> str:
        return (
            f"<NyaaResult "
            "title={self.title} "
            "size={self.size} "
            "upload_date={self.upload_date} "
            "seeders={self.seeders} "
            "leechers={self.leechers} "
            "downloads={self.downloads}>"
        )

    __str__ = __repr__

    @classmethod
    def from_data(cls, result) -> NyaaResult:
        children = result.findAll("td")
        link = children[2].find("a", href=re.compile(r"^\/download\/.*$")).get("href")
        magnet = children[2].find("a", href=re.compile(r"^magnet:.*$")).get("href")

        return cls(
            children[1].find("a", href=re.compile(r"^/view/\d+$")).text,
            link,
            magnet,
            children[3].text,
            str(date.fromtimestamp(int(children[4]["data-timestamp"]))),
            int(children[5].text),
            int(children[6].text),
            int(children[7].text),
        )

@dataclass
class Anime(BaseAnime):
    def __repr__(self) -> str:
        return f"<Anime id={self.id} title={self.english_title}>"

    __str__ = __repr__

    def __hash__(self) -> int:
        return self.id

    def __eq__(self, o: object) -> bool:
        return isinstance(o, Anime) and self.id == o.id

    def edit(self, sync: AniList, *, status: UserStatus):
        sync.gql("update_entry", {"mediaId": self.id, "status": status.name})

@dataclass
class AnimeCollection(BaseCollection, Anime):
    def __repr__(self) -> str:
        return f"<AnimeCollection(id={self.id} user_status={self.user_status} title={self.english_title})>"

    __str__ = __repr__

    def edit(
        self,
        sync: AniList,
        *,
        status: Optional[UserStatus] = None,
        score: Optional[float] = None,
        progress: Optional[int] = None,
        repeat: Optional[int] = None,
        notes: Optional[str] = None,
        started_at: Optional[date] = None,
        completed_at: Optional[date] = None,
    ):
        payload: Dict[str, Any] = {"id": self._list_id}

        if status is not None:
            payload["status"] = status.name
        if score is not None:
            payload["score"] = score
        if progress is not None:
            payload["progress"] = progress
        if repeat is not None:
            payload["repeat"] = repeat
        if notes is not None:
            payload["notes"] = notes
        if completed_at is not None:
            payload["completedAt"] = {
                "year": completed_at.year,
                "month": completed_at.month,
                "day": completed_at.day,
            }
        if started_at is not None:
            payload["startedAt"] = {
                "year": started_at.year,
                "month": started_at.month,
                "day": started_at.day,
            }

        ret = sync.gql("update_entry", payload)
        self.update_user_data(ret["data"]["SaveMediaListEntry"])

    def update_user_data(self, data: Dict):
        self.user_status = UserStatus[data["status"]]
        self.score = data["score"]
        self.notes = data["notes"]
        self.progress = data["progress"]
        self.repeat = data["repeat"]
        self.updated_at = (
            date.fromtimestamp(data["updatedAt"]) if data["updatedAt"] else None
        )

        user_start = (
            date(**data["startedAt"])
            if all(value for value in data["startedAt"].values())
            else None
        )
        user_end = (
            date(**data["completedAt"])
            if all(value for value in data["completedAt"].values())
            else None
        )

        self.user_start_date = user_start
        self.user_end_date = user_end

    def delete(self, sync: AniList):
        sync.gql("delete_entry", {"id": self._list_id})

class AnimeFile:
    title: str
    season: int
    episode_title: str
    file: str
    episode_number: int
    subtitles: List[SubtitleTrack]
    alternate_title: Optional[str]
    _thumbnail: Optional[bytes]

    def __repr__(self) -> str:
        return f"<AnimeFile title={self.title} season={self.season} episode_number={self.episode_number}>"

    __str__ = __repr__

    @classmethod
    def from_data(cls, data: Dict) -> Union[List[AnimeFile], AnimeFile]:
        # TODO: Probably create my own parser, this fails on e.g.
        # [Samir755] Violet Evergarden - 05- You Write Letters That Bring People Together.mkv
        # (No space after the episode number)
        episode = data["episode"]

        def ret_file(_episode: str) -> AnimeFile:
            inst = cls()
            inst.title = data["anime_title"]
            inst.season = int(data.get("season", 1))
            inst.episode_title = data.get("episode_title", "Unknown")
            inst.alternate_title = data.get("alternate_title")
            inst.file = data["file_name"]
            inst.episode_number = int(_episode)
            inst.subtitles = []
            inst._thumbnail = None

            return inst

        # We want to return a list of files, one for each episode
        if isinstance(episode, list):
            results = []

            for ep in episode:
                results.append(ret_file(ep))

            return results
        elif isinstance(episode, str):
            return ret_file(episode)
        else:
            raise TypeError(
                f"Could not parse data, expected list or string as episode, got {type(episode)}"
            )

    @property
    def thumbnail(self):
        if self._thumbnail is None:
            with tempfile.NamedTemporaryFile(suffix=".jpg") as f:
                cmd = [
                    ffmpeg_cmd,
                    "-ss",
                    "00:03:30.00",
                    "-i",
                    self.file,
                    "-vframes",
                    "1",
                    "-y",
                    f.name,
                ]
                subprocess.run(cmd)
                f.seek(0)
                image = f.read()

            self._thumbnail = image

        return self._thumbnail

    def load_subtitles(self, standalone_subs: Dict[Tuple[str, int], str]):
        self.subtitles = []

        sub_id = 1

        for stream in ffprobe_data(self.file).get("streams", []):
            if stream["codec_type"] == "subtitle":
                # Insert the sub_id into the data
                stream["tags"]["id"] = sub_id
                self.subtitles.append(SubtitleTrack.from_data(stream))
                sub_id += 1

        # Now find all the matching standalone ones
        for (title, episode_number), track in standalone_subs.items():
            if title == self.title and episode_number == self.episode_number:
                self.subtitles.append(SubtitleTrack.from_file(track))

class SubtitleTrack:
    language: str
    title: str
    id: int
    file: Optional[str]

    def __repr__(self) -> str:
        return f"<SubtitleTrack lang='{self.language}' title='{self.title}'>"

    __str__ = __repr__

    @classmethod
    def from_data(cls, data: Dict):
        inst = cls()
        inst.language = data.get("tags", {}).get("language", "Unknown")
        inst.title = data.get("tags", {}).get("title", "Unknown")
        inst.id = data.get("tags", {}).get("id", 0)

        if "file_name" in data:
            inst.file = data["file_name"]
        else:
            inst.file = None

        return inst

    @classmethod
    def from_file(cls, file: str):
        data = ffprobe_data(file)["streams"][0]
        data["file_name"] = file
        return cls.from_data(data)

    @property
    def is_songs_signs(self) -> bool:
        """A convenience property that tries to guess if this subtitle track is
        just for songs and signs, based on the name"""
        return "songs" in self.title.lower() or "signs" in self.title.lower()


# 1924. 2007년
# 나의 풀이
x, y = map(int, input().split())

for i in range(1, x):
    if i in [1, 3, 5, 7, 8, 10, 12]:
        y += 31
    elif i in [4, 6, 9, 11]:
        y += 30
    elif i in [2]:
        y += 28

if y % 7 == 1:
    print('MON')
elif y % 7 == 2:
    print('TUE')
elif y % 7 == 3:
    print('WED')
elif y % 7 == 4:
    print('THU')
elif y % 7 == 5:
    print('FRI')
elif y % 7 == 6:
    print('SAT')
elif y % 7 == 0:
    print('SUN')

# 다른 풀이
week = ['SUN', 'MON', 'TUE', 'WED', 'TUR', 'FRI', 'SAT']
month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
x, y = map(int, input().split())
day = 0

for i in range(x-1):  # 입력한 달은 제외
    day += month[i]

day = (day+y) % 7
print(week[day])


from media import CD, DVD, MP3

class MediaFactory():

    def create(self, format, title):
        if (format == 'DVD'):
            media = DVD(title)
        elif (format == 'CD'):
            media = CD(title)
        else:
            media = MP3(title)
        return media


class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:

        for r in ransomNote:
            if r in magazine:
                ransomNote.remove(r)
                magazine.remove(r)
            else:
                return False
        return len(ransomNote) == 0


from datetime import datetime
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
es = Elasticsearch([{'host': '130.245.168.89', 'port': 9200}])

def filter_with_query(query, timestamp, limit, sort_by, tags, has_media, accepted):
   must_not ={ "should":[] ,"must": [  { "range": { "timestamp": { "lte": timestamp }}}], 'must_not': [] }

   if len(query) != 0:
      must_not['should'].append(   {"match" : {"title": query }})
      must_not['should'].append(   {"match" : {"body": query }})

   if accepted == True:
      must_not['must'].append({ "exists": {"field": "accepted_answer_id" }}   ) # field must exist

   if has_media == True:
      must_not['must_not'].append( {"terms" : { "media" : [] }} )

   if tags != []:
      for item in tags:
         term = {"terms" : {'tags': [item]}}
         must_not['must'].append(term)

   body={
         "sort" : [{ sort_by : {"order" : "desc"}}],
         "from" : 0, "size" : limit,
         "query" : {
            "constant_score" : {
               "filter" : {
                  "bool" : must_not
               }
            }
         }
      }

   questFilter =[]
   res = es.search(index="test-index", body=body)
   for hit in res['hits']['hits']:
      # print( hit["_source"])
      temp = {
               'id': str(q['_id']),
               'title':q["_source"]['title'],
               'body': q["_source"]['body'],
               'tags': q["_source"]['tags'],
               'answer_count': 0,
               'media': q["_source"]['media'],
               'accepted_answer_id': q["_source"]['accepted_answer_id'] ,
               'user':q["_source"]['user'],
               'timestamp': q["_source"]['timestamp'],
               'score': q["_source"]['score'],
               "view_count": q["_source"]['view_count']
            }
      questFilter.append(temp)


"""
A demo for training Sequential Variational Autoencoders.

python -m dedo.run_svae --logdir=~/local/dedo --num_envs 12 --unsup_algo VAE

tensorboard --logdir=/tmp/dedo --bind_all --port 6006

--unsup_algo choices are: VAE, SVAE, PRED

Note: this code is for research i.e. quick experimentation; it has minimal
comments for now, but if we see further interest from the community -- we will
add further comments, unify the style, improve efficiency and add unittests.

@contactrika

"""
from copy import deepcopy

import gym
import numpy as np
from stable_baselines3.common.env_util import (
    make_vec_env, DummyVecEnv, SubprocVecEnv)
from tensorboardX import SummaryWriter
import torch

from dedo.utils.args import get_args
from dedo.utils.train_utils import init_train, object_to_str
from dedo.vaes.nets import ConvStack
from dedo.vaes.svae import SVAE  # used dynamically
from dedo.vaes.svae_utils import do_logging, fill_seq_bufs_from_rollouts
from dedo.vaes.svae_viz import viz_samples

def get_batch(env, rollout_len):
    x_1toT = []
    act_1toT = []
    mask_1toT = []
    for _ in range(rollout_len):
        act01 = np.random.rand(env.num_envs, env.action_space.shape[-1])
        act = act01*2 - 1.0  # [0,1] -> [-1,1]
        obs, rwd, done, next = env.step(act)
        masks = np.array([[0.0] if done_ else [1.0] for done_ in done])
        x_1toT.append(obs)
        act_1toT.append(act)
        mask_1toT.append(masks)
        if np.random.rand() > 0.9:  # reset envs randomly for data variety
            env_id = np.random.randint(env.num_envs)
            env.env_method('reset', indices=[env_id])
    x_1toT = torch.from_numpy(np.stack(x_1toT)).float()
    act_1toT = torch.from_numpy(np.stack(act_1toT)).float()
    mask_1toT = torch.from_numpy(np.stack(mask_1toT)).float()
    x_1toT = x_1toT.transpose(0, 1).transpose(2, -1).transpose(-2, -1)
    act_1toT = act_1toT.transpose(0, 1)   # put bsz 0th, time 1st
    mask_1toT = mask_1toT.transpose(0, 1)  # put bsz 0th, time 1st
    return x_1toT, act_1toT, mask_1toT

def main(args):
    assert(args.unsup_algo is not None), 'Please provide --unsup_algo'
    if args.cam_resolution not in ConvStack.IMAGE_SIZES:
        print(f'Setting cam_resolution to 512 (was {args.cam_resolution:d})')
        args.cam_resolution = 512  # set default image resolution if needed
    args.logdir, args.device = init_train(args.unsup_algo, args)
    tb_writer = SummaryWriter(args.logdir)
    tb_writer.add_text('args', object_to_str(args))
    print('svae_demo with args:\n', args)
    train_args = deepcopy(args)
    train_args.debug = False  # no debug during training
    train_args.viz = False  # no viz during training
    vec_env = make_vec_env(
        args.env, n_envs=args.num_envs,
        vec_env_cls=SubprocVecEnv if args.num_envs > 1 else DummyVecEnv,
        env_kwargs={'args': train_args})
    vec_env.seed(args.seed)
    print('Created', args.task, 'with observation_space',
          vec_env.observation_space.shape, 'action_space',
          vec_env.action_space.shape)
    #
    # Train unsupervised learner.
    #
    unsup_algo_params = 'PARAMS_'+args.unsup_algo
    unsup_algo_class = 'SVAE'
    svae = eval(unsup_algo_class)(
        im_sz=args.cam_resolution, act_sz=vec_env.action_space.shape[-1],
        params_class=unsup_algo_params, device=args.device)
    optim = torch.optim.Adam(svae.parameters(), lr=args.lr)
    seq_len = svae.pr.past+svae.pr.pred
    rlt_len = 50
    num_inner_epochs = 100 if args.unsup_algo == 'VAE' else 50
    mini_batch_size = 96 if args.unsup_algo == 'VAE' else 24
    if args.unsup_algo == 'PRED':
        mini_batch_size = 16
    vec_env.reset()
    steps_done = 0
    epoch = 0
    print('Getting test env data... ')
    test_x_1toT, test_act_1toT, test_mask_1toT = get_batch(vec_env, 300)
    print('got', test_x_1toT.shape)
    while steps_done < args.total_env_steps:
        print(f'Epoch {epoch:d}: getting train env data... ')
        all_x_1toT, all_act_1toT, all_mask_1toT = get_batch(vec_env, rlt_len)
        all_x_1toT.to(args.device)
        all_act_1toT.to(args.device)
        all_mask_1toT.to(args.device)
        print('got', all_x_1toT.shape)
        steps_done += rlt_len*args.num_envs
        for inner_epoch in range(num_inner_epochs):
            do_log_viz = inner_epoch+1 == num_inner_epochs
            x_1toT, act_1toT = fill_seq_bufs_from_rollouts(
                all_x_1toT, all_act_1toT, all_mask_1toT,
                mini_batch_size, seq_len, args.device)
            optim.zero_grad()
            loss, debug_dict = svae.loss(x_1toT, act_1toT, debug=do_log_viz)
            loss.backward()
            optim.step()
            if do_log_viz:
                do_logging(epoch, debug_dict, {}, tb_writer, 'train')
                viz_samples(svae, x_1toT, act_1toT, epoch, tb_writer, 'train')
                tmp_x_1toT, tmp_act_1toT = fill_seq_bufs_from_rollouts(
                    test_x_1toT, test_act_1toT, test_mask_1toT,
                    mini_batch_size, seq_len, args.device)
                steps_done += seq_len*args.num_envs
                loss, debug_dict = svae.loss(
                    tmp_x_1toT, tmp_act_1toT, debug=do_log_viz)
                do_logging(epoch, debug_dict, {}, tb_writer, 'test')
                viz_samples(svae, tmp_x_1toT, tmp_act_1toT, epoch, tb_writer,
                            'test')
        epoch += 1
    #
    # Clean up.
    vec_env.close()

if __name__ == "__main__":
    main(get_args())


#Gabriel Custodio Boelter 2190

h = float(input("Digite a altura da parede: "))#Entrada das variaveis n1,n2,n3,4 pedindo para lhe dar um valor
c = float(input("Digite o comprimento da parede: "))

m = (h*c) #equação de metros quadrados

consumolitros = (m/3) #equação do consumo de litros de tinta

latas = consumolitros/ 3.6 #equação da quatidade de latas

vt = (latas * 107) #valor total

print("A quantidade de latas é de {}" .format(latas))#Print na tela mostrando a quantidade de latas
print("E o valor total é de {}" .format(vt))#Print na tela com o valor total


import sys
from itertools import permutations

input = sys.stdin.readline

N = int(input())

"""
0 -> 1
1 -> 10
배열 2개
  0 1 2 3 4 5 6 7
A 1 0 1 1 2 3 5 8 13 21 34
B 0 1 1 2 3 5 8 13
"""
string_N = str(N)
l = sorted(string_N, reverse=True)
temp = sorted(set(p for p in permutations(string_N, len(string_N))))
answer = temp.index(tuple(string_N))
if answer < len(temp) -1:
    print(''.join(temp[answer + 1]))
else:
    print(0)


import pathlib
import pandas as pd
from cldfbench import Dataset as BaseDataset
from segments import Profile, Tokenizer
import re
from slugify import slugify
import lingpy
from clldutils.misc import slug
import cariban_helpers as crh
import cldf_helpers as cldfh

class Dataset(BaseDataset):
    dir = pathlib.Path(__file__).parent
    id = "cariban_pronouns"

    def cldf_specs(self):  # A dataset must declare all CLDF sets it creates.
        from cldfbench import CLDFSpec

        return CLDFSpec(dir=self.cldf_dir, module="Wordlist")

    def cmd_download(self, args):
        """
        Download files to the raw/ directory. You can use helpers methods of `self.raw_dir`, e.g.

        >>> self.raw_dir.download(url, fname)
        """
        pass

    def doc_sources(self):
        out = []
        with open("../docs/pld-slides/content/main.md", "r", encoding="utf-8") as f:
            content = f.read()
            psrc = re.findall(r"\[psrc\]\((.*?)\)", content)
            src = re.findall(r"\[src\]\((.*?)\)", content)
            for cits in psrc + src:
                for cit in cits.split(","):
                    c = cit.split("[")[0]
                    if c not in out:
                        out.append(c)
        return out

    def cmd_makecldf(self, args):

        args.writer.cldf.add_component("CognateTable")
        args.writer.cldf.add_component("CognatesetTable")
        args.writer.cldf.add_component("LanguageTable")
        args.writer.cldf.add_component("ParameterTable")
        args.writer.cldf.add_columns("CognatesetTable", "Form")
        args.writer.cldf.remove_columns("FormTable", "Parameter_ID")
        args.writer.cldf.add_columns(
            "FormTable",
            {
                "name": "Parameter_ID",
                "required": True,
                "propertyUrl": "http://cldf.clld.org/v1.0/terms.rdf#parameterReference",
                "dc:description": "A reference to the meaning denoted by the form",
                "datatype": "string",
                "separator": "; ",
            },
        )

        segments = open("etc/segments.txt").read()
        segment_list = [{"Grapheme": x, "mapping": x} for x in segments.split("\n")]
        t = Tokenizer(Profile(*segment_list))

        def segmentify(form):
            form = re.sub("[?()\[\]]", "", form)
            form = form.strip("-")
            return t(
                form.replace("-", "+")
            )  # morpheme boundaries are replaced with cognate boundaries

        lg_ids = []
        contained_sources = self.doc_sources()
        forms = pd.read_csv("raw/forms.csv", keep_default_na=False)

        # set morpheme IDs to lg + index where not present
        forms["ID"] = forms.apply(
            lambda x: f"""{x["Language_ID"]}-f-{x.name}""" if x["ID"] == "" else x["ID"],
            axis=1,
        )
        forms.set_index("ID", inplace=True)
        # assign cognacy information to morphologically complex forms

        cogsets = pd.read_csv("raw/cognatesets.csv", keep_default_na=False)
        cogsets.index = cogsets.index + 1
        cognum = dict(zip(cogsets["ID"], cogsets.index.astype(str)))

        def str2numcog(cogsets):
            return " ".join([cognum[x] for x in cogsets.split("+")])

        def check_cognates(row):
            if row["Cognates"].count("+") != row["Form"].count("+"):
                print(row)
            for cogset in row["Cognates"].split("+"):
                if cogset != "?" and cogset not in list(cogsets["ID"]):
                    print(f"Missing cognate set {cogset}:", row, sep="\n")

        forms.apply(check_cognates, axis=1)
        forms["Segments"] = forms["Form"].apply(segmentify)

        def proto(row):
            if "P" in row["Language_ID"]:
                row["Form"] = "*"+row["Form"]
            return row
        forms = forms.apply(proto, axis=1)

        alignments = {}

        for abs_cog, abs_id in [("1", "1abs"), ("2", "2abs"), ("3ANA.ANIM", "3aabs"), ("3ANA.INAN", "3iabs")]:
            cog_df = forms[((forms["Cognateset_ID"] == abs_cog) & ~(forms["Language_ID"].str.contains("P")))]
            cog_df.reset_index(inplace=True)
            seglist = lingpy.align.multiple.Multiple(list(cog_df["Segments"]))
            seglist.align(method="progressive")
            cog_df["Alignment"] = seglist.alm_matrix
            # cog_df["Alignment"] = cog_df["Alignment"].apply(lambda x: " ".join(x))
            for rec in cog_df.to_dict("records"):
                args.writer.objects["CognateTable"].append(
                    {
                        "ID": f"""{rec["ID"]}-abs""",
                        "Form_ID": rec["ID"],
                        "Cognateset_ID": abs_id,
                        "Alignment": rec["Alignment"],
                    }
                )

        modern_forms = forms[~(forms["Language_ID"].str.contains("P"))]

        for i, row in cogsets.iterrows():
            if row["ID"] == "?":
                continue
            args.writer.objects["CognatesetTable"].append(
                {
                    "ID": slug(row["ID"]),
                    "Form": "*" + row["Form"],
                    "Description": row["Description"],
                }
            )
            if "abs" in row["ID"]:
                continue
            cog_df = cldfh.get_cognates(modern_forms, row["ID"], form_col="Segments")
            cog_df["Segments"] = cog_df["Form"].str.strip(" ")
            cog_df = cog_df[cog_df["Segments"] != ""]
            seglist = lingpy.align.multiple.Multiple(list(cog_df["Segments"]))
            seglist.align(method="progressive")
            cog_df["Alignment"] = seglist.alm_matrix
            cog_df["Alignment"] = cog_df["Alignment"].apply(lambda x: " ".join(x))
            for j, row_i in cog_df.iterrows():
                alignments[f"""{j}-{row_i["Slice"]}"""] = row_i["Alignment"]
        edictor_output = pd.DataFrame()

        for i, row in forms.iterrows():
            row["Parameter_ID"] = [slug(par) for par in row["Meaning"].split("; ")]
            args.writer.objects["FormTable"].append(
                {
                    "ID": i,
                    "Language_ID": row["Language_ID"],
                    "Form": row["Form"].replace("+", ""),
                    "Segments": row["Segments"].split(" "),
                    "Source": row["Source"].split("; "),
                    "Comment": row["Comments"],
                    "Parameter_ID": row["Parameter_ID"],
                }
            )
            lg_ids.append(row["Language_ID"])
            for source in row["Source"].split("; "):
                contained_sources.append(source.split("[")[0])
            if "P" in row["Language_ID"]:
                continue
            if row["Cognates"] == "":
                continue
            edictor_alignment = []
            for segment_slice, cog_id in enumerate(row["Cognates"].split("+")):
                if cog_id == "?":
                    edictor_alignment.append("-")
                    continue
                align_key = f"{i}-{segment_slice+1}"
                if align_key not in alignments:
                    alignment = ""
                else:
                    alignment = (alignments[align_key],)
                args.writer.objects["CognateTable"].append(
                    {
                        "ID": f"{i}_{segment_slice}",
                        "Form_ID": i,
                        "Cognateset_ID": slug(cog_id),
                        "Segment_Slice": str(segment_slice + 1),
                        "Alignment": alignment,
                    }
                )
                edictor_alignment.append(" ".join(alignment))
            for par in row["Parameter_ID"]:
                edictor_output = edictor_output.append(
                    {
                        "DOCULECT": row["Language_ID"],
                        "CONCEPT": slug(par),
                        "CONCEPTID": slug(par),
                        "IPA": row["Form"].replace("+", ""),
                        "SEGMENTS": segmentify(row["Form"]),
                        "COGIDS": str2numcog(row["Cognates"]),
                        "ALIGNMENT": " + ".join(edictor_alignment),
                    },
                    ignore_index=True,
                )

        lg_ids = list(set(lg_ids))
        for lg in crh.lg_order().keys():
            if lg in lg_ids:
                args.writer.objects["LanguageTable"].append(
                    {
                        "ID": lg,
                        "Name": crh.get_name(lg),
                        "Glottocode": crh.get_glottocode(lg),
                    }
                )

        meanings = pd.read_csv("raw/meanings.csv", keep_default_na=False)
        meanings["ID"] = meanings["ID"].apply(slug)
        for i, row in meanings.iterrows():
            args.writer.objects["ParameterTable"].append(row.to_dict())

        sources = self.etc_dir.read_bib("etc/cariban_references_out.bib")
        sources = [x for x in sources if x.id in contained_sources]
        args.writer.cldf.add_sources(*sources)
        edictor_output.index.name = "ID"
        edictor_output.to_csv("etc/pronouns_edictor.tsv", sep="\t")


class Solution:
    def isPalindrome(self, x: int) -> bool:
        # convert number to string
        # check if the string is a palindrome

        x_str = str(x)

        first = 0
        last = len(x_str) - 1

        while first < last:
            if x_str[first] != x_str[last]:
                return False
            else:
                first += 1
                last -= 1

        return True

# this solution has time complexity O(n), where n is the number of digits in x


import yaml
from dqlib import lib_bigquery
from datetime import datetime, timedelta
import os

def main(request):

    data = request.get_json()

    DataExecucaoPortfolio = None

    if data and 'DataExecucaoPortfolio' in data:
        DataExecucaoPortfolio = data["DataExecucaoPortfolio"]
    else:
        DataExecucaoPortfolio = datetime.now() - timedelta(1)
        DataExecucaoPortfolio = DataExecucaoPortfolio.strftime('%Y-%m-%d')

    configFile = "config/dq.yaml"
    my_path = os.path.abspath(os.path.dirname(__file__))
    path = os.path.join(my_path, configFile)
    configList = yaml.safe_load(open(path))

    for tableConf in configList:
        project = tableConf['project']
        datasetName = tableConf['datasetName']
        datasetDataQualityName = tableConf['datasetDataQualityName']
        dataTable = tableConf['dataTable']
        reportTable = tableConf['reportTable']
        columnList = tableConf['includeColumns']

        # Get Row Count
        rowCount = lib_bigquery.getRowCount(project, datasetName, dataTable, DataExecucaoPortfolio)

        # Prepare Dynamic SQL Statement
        sqlQuery = lib_bigquery.getSql(project, datasetName, dataTable, columnList, rowCount, DataExecucaoPortfolio)

        # Run SQL & Display Output
        lib_bigquery.runSql(project, datasetDataQualityName, datasetName, dataTable, reportTable, sqlQuery)

    return {"status": "success"}


"""
This module provides an interface to the Berkeley EMD file format.

See https://emdatasets.com/ for more details.

* It fully supports v0.2 for reading and writing

Note
----
General users:
    Use the simplified emd.emdReader() function to load the data and meta
    data as a python dictionary.

Advanced users and developers:
    Access the file internals through the emd.fileDEMD() class.

"""

from pathlib import Path
import datetime

import numpy as np
import h5py

class fileEMD:
    """Class to represent Berkeley EMD files.

    Implemented for spec 0.2 using the recommended layout for metadata.

    Attributes
    ----------
    file_name : str
        The name of the file
    file_path : pathlib.Path
        A pathlib.Path object for the open file
    file_hdl : h5py.File
        The h5py file handle which provides direct access to the underlying h5py file structure.
    version : tuple
        A 2-tuple of the major and minor EMD version as ints.
    data : h5py.Group
        A link to the data group in the EMD file.
    microscope : h5py.Group
        A link to the microscope EMD group with metadata.
    sample : h5py.Group
        A link to the sample EMD group with metadata.
    user : h5py.Group
        A link to the user EMD group with metadata.
    comments : h5py.Group
        A link to the comments in this EMD file. A time stamp is included with the comment.
    list_emds : list
        A list of the EMD groups in the file.

    Examples
    --------
    Open an Berkeley EMD file using the *advanced* interface. See the emdReader function for a more convenient
    way to load the data. We show how to list the available data sets, load a 3D data set and plot the first image.
    >> from ncempy.io import emd
    >> with emd.fileEMD('filename.emd') as emd1:
    >>     [print(dataGroup.name) for dataGroup in emd1.list_emds] # print all available EMD datasets
    >>     data1, dims1 = emd1.get_emdgroup(0) # load the first full data array and dimension information
    """

    def __init__(self, filename, readonly=True):
        """Init opening/creating the file.

        Parameters
        ----------
        filename : str or pathlib.Path or file object
            The EMD file to open.
        readonly : bool, default True
            Set to False to allow writing to the file.

        """

        # necessary declarations in case something goes bad
        self.file_hdl = None

        # convenience handles to access the data in the emd file, everything can as well be accessed using the file_hdl
        self.version = None
        self.data = None
        self.microscope = None
        self.sample = None
        self.user = None
        self.comments = None
        self.list_emds = []  # list of HDF5 groups with emd_data_type type 1

        if hasattr(filename, 'read'):
            try:
                self.file_path = Path(filename.name)
                self.file_name = self.file_path.name
            except AttributeError:
                self.file_path = None
                self.file_name = None
        else:
            # check filename type, change to pathlib.Path
            if isinstance(filename, str):
                filename = Path(filename)
            elif isinstance(filename, Path):
                pass
            else:
                raise TypeError('Filename is supposed to be a string or pathlib.Path or file object')
            self.file_path = filename
            self.file_name = self.file_path.name

        # try opening the file
        if readonly:
            try:
                self.file_hdl = h5py.File(filename, 'r')
            except:
                print('Error opening file for readonly: "{}"'.format(filename))
                raise
        else:
            try:
                self.file_hdl = h5py.File(filename, 'a')
            except:
                print('Error opening file for read/write: "{}"'.format(filename))
                raise

        # if we got a working file
        if self.file_hdl:
            # check version information
            if 'version_major' in self.file_hdl.attrs and 'version_minor' in self.file_hdl.attrs:
                # read version information
                self.version = (self.file_hdl.attrs['version_major'], self.file_hdl.attrs['version_minor'])
                # compare to implementation
                if not self.version == (0, 2):
                    print('WARNING: You are reading a version {}.{} EMD file, '
                          'this implementation assumes version 0.2!'.format(self.version[0], self.version[1]))
            else:
                # set version information
                if not readonly:
                    self.file_hdl.attrs['version_major'] = 0
                    self.file_hdl.attrs['version_minor'] = 2

            # check for data group
            if 'data' not in self.file_hdl:
                if not readonly:
                    self.data = self.file_hdl.create_group('data')
            else:
                self.data = self.file_hdl['data']

            # check for data group
            if 'microscope' not in self.file_hdl:
                if not readonly:
                    self.microscope = self.file_hdl.create_group('microscope')
            else:
                self.microscope = self.file_hdl['microscope']

            # check for data group
            if 'sample' not in self.file_hdl:
                if not readonly:
                    self.sample = self.file_hdl.create_group('sample')
            else:
                self.sample = self.file_hdl['sample']

            # check for data group
            if 'user' not in self.file_hdl:
                if not readonly:
                    self.user = self.file_hdl.create_group('user')
            else:
                self.user = self.file_hdl['user']

            # check for data group
            if 'comments' not in self.file_hdl:
                if not readonly:
                    self.comments = self.file_hdl.create_group('comments')
            else:
                self.comments = self.file_hdl['comments']

            # find emd_data_type groups in the file
            self.list_emds = self.find_emdgroups(self.file_hdl)

    def __del__(self):
        """Destructor for EMD file object.

        """
        # close the file
        # if(not self.file_hdl.closed):
        self.file_hdl.close()

    def __enter__(self):
        """Implement python's with statement

        """
        return self

    def __exit__(self, exception_type, exception_value, traceback):
        """Implement python's with statement
        and close the file via __del__()
        """
        self.__del__()
        return None

    def find_emdgroups(self, parent):
        """Find all emd_data_type groups within the group parent and return a list of references to their HDF5 groups.

        Parameters
        ----------
            parent: h5py.Group
                Handle to the parent group.

        Returns
        -------
            : list
                A list of h5py.Group handles to children groups
                being emd_data_type groups.

        """

        emds0 = []

        # recursive function to run and retrieve groups with emd_group_type set to 1
        def proc_group(group, emds):
            # take a look at each item in the group
            for item in group:
                # check if group
                if group.get(item, getclass=True) == h5py.Group:
                    item = group.get(item)
                    # check if emd_group_type
                    if 'emd_group_type' in item.attrs and 'data' in item.keys():
                        if item.attrs['emd_group_type'] == 1:
                            emds.append(item)
                    # process subgroups
                    proc_group(item, emds)

        # run
        proc_group(parent, emds0)

        return emds0

    def get_emddims(self, group):
        """Get the emdtype dimensions saved in in group.

        Parameters
        ----------
            group: h5py.Group
                Reference to the emdtype HDF5 group.

        Returns
        -------
            : tuple
                List of dimension vectors plus labels and units.

        """
        # get the dims
        dims = []
        for ii in range(group['data'].ndim):
            dim = group['dim{}'.format(ii + 1)]
            # save them as (vector, name, units)

            if 'name' in dim.attrs:
                if isinstance(dim.attrs['name'], np.ndarray):
                    name = dim.attrs['name'][0]
                else:
                    name = dim.attrs['name']
            else:
                name = 'dim{}'.format(ii + 1)

            if 'units' in dim.attrs:
                if isinstance(dim.attrs['units'], np.ndarray):
                    units = dim.attrs['units'][0]
                else:
                    units = dim.attrs['units']
            else:
                units = 'pixels'

            # Handle bytes objects by decoding them to strings
            # If something goes wrong, the original attribute is left as-is
            try:
                if isinstance(name, bytes):
                    name = name.decode('utf-8')
                if isinstance(units, bytes):
                    units = units.decode('utf-8')
            except:
                pass

            dims.append((dim[:], name, units))

        dims = tuple(dims)
        return dims

    def get_emdgroup(self, group):
        """Get the emd data saved in the requested group.

        Note
        ____
        The memmap keyword has been removed. Please use get_memmap().

        Parameters
        ----------
            group: h5py._hl.group.Group or int
                Reference to the HDF5 group to load. If int is used then the item corresponding to self.list_emds
                is loaded

        Returns
        -------
            : tuple or None
                None or tuple containing:
                    : np.ndarray
                        The data of the emdtype group.
                    : list
                        List of [0] dimension vectors, [1] labels and [2] units.
        """

        # check input
        if not isinstance(group, h5py.Group):
            if isinstance(group, int):
                try:
                    group = self.list_emds[group]
                except IndexError:
                    print('group does not exist')
                    return
            else:
                raise TypeError('group needs to refer to a valid HDF5 group!')

        if 'emd_group_type' not in group.attrs:
            raise TypeError('group is not a emd_group_type group!')
        if not group.attrs['emd_group_type'] == 1:
            raise TypeError('group is not a emd_group_type group!')

        # retrieve data and dims
        try:
            data = group['data'][:]
            dims = self.get_emddims(group)

            return data, dims
        except:
            # if something goes wrong, return None
            print('Content of "{}" does not seem to be in emd specified shape'.format(group.name))

            return None

    def get_memmap(self, group):
        """ Opens a new fileEMD object and returns the requested EMD group. This prevents the file from being closed.
        The original HDF5 file is left open. The file pointer is lost, but it will be closed once the EMD group
        is deleted or removed. See also get_emdgroup().

        Parameters
        ----------
        group: h5py._hl.group.Group or int
                Reference to the HDF5 group to load. If int is used then the item corresponding to self.list_emds
                is loaded

        """
        f = h5py.File(self.file_hdl.filename, 'r')

        # check input
        if not isinstance(group, h5py.Group):
            if isinstance(group, int):
                try:
                    group = self.list_emds[group]
                except IndexError:
                    print('group does not exist')
                    return
            else:
                raise TypeError('group needs to refer to a valid HDF5 group!')
        data = f[group.name + '/data']
        return data, self.get_emddims(group)

    def write_dim(self, label, dim, parent):
        """Auxiliary function to write a dim dataset to parent.

        Input is not checked for sanity, so handle exceptions in call.

        Parameters
        ----------
            label: str
                Label for dataset, usually dim1, dim2, dimN.
            dim: tuple
                Tuple containing (data, name, units).
            parent: h5py.Group
                HDF5 handle to parent group.

        Returns
        -------
            : h5py.Group
                HDF5 dataset handle referencing this dim.

        """

        try:
            dset = parent.create_dataset(label, data=dim[0])
            dset.attrs['name'] = np.string_(dim[1])
            dset.attrs['units'] = np.string_(dim[2])
        except:
            raise RuntimeError('Error during writing dim dataset')

        return dset

    def put_emdgroup(self, label, data, dims, parent=None, overwrite=False, **kwargs):
        """Put an emdtype dataset into the EMD file.

        Parameters
        ----------
            label: str
                Label for the emdtype group containing the dataset.
            data: np.ndarray
                Numpy array containing the data.
            dims: tuple
                Tuple containing the necessary dims as ((vec, name, units), (vec, name, units), ...)
            parent: h5py.Group or None
                Parent for the emdtype group, if None it will be written to /data.
            overwrite: bool
                Set to force overwriting entry in EMD file.
            **kwargs: various
                Keyword arguments to be passed to h5py.create_dataset(), e.g. for compression.

        Returns
        -------
            : h5py.Group or None
                Group referencing this emdtype dataset or None if failed.
        """

        # check input
        if not isinstance(label, str):
            raise TypeError('label needs to be string!')

        if not isinstance(data, np.ndarray):
            raise TypeError('data needs to be a numpy.ndarray!')

        try:
            assert len(dims) == len(data.shape)
            for i in range(len(dims)):
                assert len(dims[i]) == 3
                assert dims[i][0].shape[0] == data.shape[i]
        except:
            raise TypeError('Something wrong with the provided dims')

        # write stuff to HDF5

        # create group
        try:
            if parent:
                if label in parent:
                    if overwrite:
                        print('overwriting "{}" in "{}"'.format(label, parent.name))
                        del parent[label]
                    else:
                        print('"{}" already exists in "{}"'.format(label, parent.name))
                        raise RuntimeError('"{}" already exists in "{}"'.format(label, parent.name))
                grp = parent.create_group(label)

            else:
                if label in self.data:
                    if overwrite:
                        print('overwriting "{}" in "{}"'.format(label, self.data.name))
                        del self.data[label]
                    else:
                        print('"{}" already exists in "{}"'.format(label, self.data.name))
                        raise RuntimeError('"{}" already exists in "{}"'.format(label, self.data.name))

                grp = self.data.create_group(label)

            # add attribute
            grp.attrs['emd_group_type'] = 1

            # create dataset
            _ = grp.create_dataset('data', data=data, **kwargs)

            # create dim datasets
            for i in range(len(dims)):
                self.write_dim('dim{}'.format(i + 1), dims[i], grp)

            # update emds list
            self.list_emds = self.find_emdgroups(self.file_hdl)

            return grp

        except:
            print('Something went wrong trying to write the dataset.')

            return None

    def put_comment(self, msg, timestamp=None):
        """Create a comment in the EMD file.

        If timestamp already exists, the msg is appended to existing comment.

        Parameters
        ----------
            msg: str
                String of the message to save.
            timestamp: str/None
                Timestamp used as the key, defaults to the current UTC time.

        """

        # check input
        if not isinstance(msg, str):
            raise TypeError('msg needs to be a string!')

        # create timestamp if missing
        if not timestamp:
            timestamp = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S (UTC)')
        else:
            # try to convert given timestamp to string
            try:
                timestamp = str(timestamp)
            except:
                raise

        # write comment
        if timestamp in self.comments.attrs:
            # append to existing
            self.comments.attrs[timestamp] += np.string_('\n' + msg)

        else:
            # create new entry
            self.comments.attrs[timestamp] = np.string_(msg)

def defaultDims(data, pixel_size=None, pixel_unit=None):
    """ A helper function that can generate a properly setup dim tuple
    with default values to allow quick writing of EMD files without
    the need to create these dim vectors manually.

    Parameters
    ----------
        data : ndarray
            The data that will be written to the EMD file. This is used to get the number of dims and their shape
        pixel_size : tuple, optional
            A tuple of pixel sizes. Must have same length as the number of dimensions of data.
        pixel_unit : tuple, optional
            A tuple of pixel units (i.e. nm), Must have the same length as the number of dimensions of data.
    Returns
    -------
        dims: list
            A properly formatted tuple of dim vectors used as input
            to emd.emdFile.put_emdgroup()
    """
    if isinstance(pixel_size, np.ndarray):
        raise Exception('pixel_size must be a tuple')

    num = data.ndim

    if not pixel_size:
        pixel_size = (1,) * num

    if not pixel_unit:
        pixel_unit = [f'unit{ii+1}' for ii in range(num)]

    if len(pixel_size) != data.ndim or len(pixel_unit) != data.ndim:
        raise ValueError('pixel_size, pixel_unit, and data dimensions must match')

    dims = []
    for ii in range(num):
        curDim = [np.linspace(0, data.shape[ii] - 1, data.shape[ii]) * pixel_size[ii],
                  'dim{}'.format(ii+1),
                  pixel_unit[ii]]
        dims.append(curDim)

    return dims

def emdReader(filename, dsetNum=0):
    """ A simple helper function to read in the data and metadata
    in a structured format similar to the other ncempy readers.

    Note
    ----
        Note fully implemented yet. Work in progress.

    Parameters
    ----------
        filename : str or pathlib.Path
            The path to the file as a string.
        dsetNum : int
            The index of the data set to load.

    Returns
    -------
        : dict
            Data and metadata as a dictionary similar to other ncempy readers.

    Example
    -------
        Simply load all data and metadata from a data set in an EMD Velox file
            >> import ncempy.io as nio
            >> emd0 = nio.emd.emdReader('filename.emd', dsetNum = 0)

    """
    with fileEMD(filename, readonly=True) as emd0:
        d, dims = emd0.get_emdgroup(dsetNum)  # memmap must be false. File is closed
        out = {'data': d, 'filename': filename, 'pixelSize': []}

        # Get the group name
        name = emd0.list_emds[dsetNum].name.split('/')[-1]
        out['name'] = name

        for dim in dims:
            try:
                d = dim[0][1] - dim[0][0]
            except:
                d = 0
            out['pixelSize'].append(d)
        out['pixelUnit'] = [aa[2] for aa in dims]
        out['pixelName'] = [aa[1] for aa in dims]
        return out

def emdWriter(filename, data, pixel_size=None, pixel_unit=None, overwrite=False):
    """ Simple method to write data to a file formatted as an EMD v0.2. The only possible metadata to write is the pixel
    size for each dimension. Use the emd.fileEMD() class for more complex operations. The file must not already exist.

    Parameters
    ----------
    filename : str or pathlib.Path
        The path and file name to write the data to.
    data : ndarray
        The data as an ndarray to write to the file.
    pixel_size : tuple
        A tuple with the same length as the number of dims in data. len(pixel_size) == data.ndim
    pixel_unit : tuple, optional
            A tuple of pixel units (i.e. nm), Must have the same length as the number of dimensions of data.
    overwrite : boolean
        If file exists, overwrite it.
    """
    if isinstance(filename, str):
        filename = Path(filename)

    if pixel_size:
        try:
            assert len(pixel_size) == data.ndim
        except ValueError:
            raise ValueError('pixel_size length must match the number of dimensions of data.')

    if pixel_unit:
        try:
            assert len(pixel_unit) == data.ndim
        except ValueError:
            raise ValueError('pixel_unit length must match the number of dimensions of data.')

    # Delete the file if it exists and overwrite is true
    if filename.exists() and overwrite:
        filename.unlink()

    if not filename.exists():
        with fileEMD(filename, readonly=False) as emd0:
            # Setup the dims for the EMD file. Pixel size is set to 1 by default for each dimension
            dims0 = defaultDims(data, pixel_size=pixel_size, pixel_unit=pixel_unit)
            # Write the data to he emd file.
            emd0.put_emdgroup('converted', data, dims0)
    else:
        raise FileExistsError


import os.path

from enum import Enum
import numbers
import json
import random

class ParameterType(Enum):
	'''An enumeration of all available parameter types'''
	CONSTANT = 1,
	RANGE = 2,
	ENUM = 3

class Parameter:
	name = None
	display_name = None
	type = None
	constraints = None
	options = None

	_value = None
	_configuration_file = None

	def __init__(self, content, configuration_file):
		self.name = content['name']
		self.display_name = content['display_name']
		self.type = ParameterType[content['type'].upper()]

		if self.type is ParameterType.ENUM:
			self.options = content['options']
		else:
			self.constraints = content['constraints']

		self._configuration_file = configuration_file

		if self._configuration_file is not None:
			# Fetch the current value from the configuration file or use the default
			# one if none exists
			configuration = self._get_configuration()

			if self.name in configuration.keys():
				self._value = configuration[self.name]
			else:
				self.set_value(content['default'])

	def get_value(self):
		return self._value

	def set_value(self, value):
		# Check whether the new value is valid given the parameter type
		if not _is_valid_value(value, self.type, self.options):
			raise ValueError('The value {} is invalid for the parameter {} of type {}'
				.format(value, self.name, self.type))

		# Clamp the value according to the constraints
		if self.type is not ParameterType.ENUM:
			min_value = self.constraints['min']
			max_value = self.constraints['max']

		if self.type is ParameterType.CONSTANT:
			value = max(min(value, max_value), min_value)
		elif self.type is ParameterType.RANGE:
			value['lower'] = max(min(value['lower'], max_value), min_value)
			value['upper'] = max(min(value['upper'], max_value), min_value)
		elif self.type is ParameterType.ENUM:
			for option in self.options:
				if option['name'] == value:
					value = option

		# Update the non-persistent representation of the value
		self._value = value

		if self._configuration_file is not None:
			# Write the new value to the configuration file for persistent storage
			configuration = self._get_configuration()

			with open(self._configuration_file, 'w+') as f:
				configuration[self.name] = value
				f.write(json.dumps(configuration))

	def _get_configuration(self):
		configuration = {}

		if os.path.isfile(self._configuration_file):
			with open(self._configuration_file, 'r') as f:
				file_content = f.read()

				if len(file_content) > 0:
					configuration = json.loads(file_content)

		with open(self._configuration_file, 'w+') as f:
			f.write(json.dumps(configuration))

		return configuration

def _is_valid_value(value, type, options=None):
	"""Given a value and a parameter type, check whether the value is a valid one.

	Arguments:
		value: An arbitrary value to be checked.
		type: A `ParameterType` enum signifying the type of the parameter.
	Returns:
		True if the value is valid, false otherwise.
	"""

	if type is ParameterType.RANGE:
		return 'lower' in value and 'upper' in value \
		 	and isinstance(value['lower'], numbers.Number) \
			and isinstance(value['upper'], numbers.Number) \
			and value['lower'] <= value['upper']
	elif type is ParameterType.CONSTANT:
		return isinstance(value, numbers.Number)
	elif type is ParameterType.ENUM:
		if options is not None:
			for option in options:
				if option['name'] == value:
					return True
			return False
		else:
			return False
	else:
		return False

def reset_random_seed():
	random.seed(42)

def generate_configuration(parameters, percentage=None, randomize=True):
	"""Given a set of parameters, select a valid value for each of them
	respecting given parameter types and constraints. By default, this will
	randomize parameters.

	Arguments:
		parameters: A valid set of configuration parameters.
		percentage: A floating point number between 0 and 1 according to which
			values will be chosen from available ranges. Can be None if randomized
			values should be generated.
		randomize: True if random values should be chosen instead of sequentially
		 	choosing them from the available range.
	Returns:
		A dictionary mapping each parameter name to its chosen value.
	"""

	configuration = {}

	for parameter_name in parameters.keys():
		parameter = parameters[parameter_name]

		if parameter.type is ParameterType.CONSTANT:
			configuration[parameter_name] = parameter.get_value()
		elif parameter.type is ParameterType.RANGE:
			lower = parameter.get_value()['lower']
			upper = parameter.get_value()['upper']

			if randomize:
				configuration[parameter_name] = random.uniform(lower, upper)
			elif percentage != None:
				configuration[parameter_name] = lower + percentage * (upper - lower)
		elif parameter.type is ParameterType.ENUM:
			configuration[parameter_name] = parameter.get_value()

	return configuration

def from_json(data):
	"""Initialize a dictionary of parameters from JSON data.

	Arguments:
		parameters: A valid set of configuration parameters as JSON data. Each
			parameter has to contain its name, type, and value.
	Returns:
		A dictionary containing all initialized parameters.
	"""

	data = json.loads(data)
	parameters = {}

	for _parameter in data:
		parameter = Parameter(
			{
				'name': _parameter['name'],
				'display_name': _parameter['displayName'],
				'type': _parameter['type'],
				'constraints': _parameter['constraints'],
				'options': _parameter['options']
			},
			None
		)
		parameter._value = _parameter['value']

		parameters[_parameter['name']] = parameter

	return parameters


#!/usr/bin/python3
""" list 10 commits of the repository “rails” by the user “rails”"""
import sys
import requests

if __name__ == "__main__":
    url = "https://api.github.com/repos/{}/{}/commits".format(
        sys.argv[2], sys.argv[1])
    resp = requests.get(url)
    count = 0
    try:
        json_data = resp.json()
        for item in json_data:
            if count < 10:
                print("{}: {}".format(
                    item.get('sha'),
                    item['commit']['author'].get('name')))
                count += 1
    except valueError:
        print("Not a valid JSON")


from tkinter import ttk

def style():
    style = ttk.Style()
    style.theme_use('clam')
    style.configure("Treeview",
    background="white",
    fg="white",
    rowheight="25",
    fieldbackground="white"
    )
    style.configure("Treeview.Heading",
    background="#E8DFF1",
    foreground="#5F1965",
    height="10",
    font=("Baloo Tamma 2 Medium",10),
    fieldbackground="white"
    )
    style.configure("TCombobox",
    background="white",
    foreground="back",
    height="10",
    font=("Baloo Tamma 2 Medium",11)
    )

    # Thay đổi màu đã chọn
    style.map ("Treeview",
    background=[('selected','#5F1965')]
    )

    style.map('TCombobox', fieldbackground=[('readonly','white')])
    style.map('TCombobox', selectbackground=[('readonly', 'white')])
    style.map('TCombobox', selectforeground=[('readonly', '#5F1965')])

def update(tv,row):
    tv.delete(*tv.get_children())
    global dem

    dem=0
    for i in row:
        i.insert(0,dem+1)
        if dem%2==0:
            tv.insert("",index="end",iid=dem,values=i,text='',tags=('evenrow'))
        else:
            tv.insert("",index="end",iid=dem,values=i,text='',tags=('ollrow'))
        dem += 1


import os
import openai
from functions.functions import load_data_from_file

load_data_from_file(r"data/key.json")
openai.api_key = os.environ["AI_API_KEY"]

response = openai.Completion.create(
  model="text-davinci-003",
  prompt="Correct this to standard English:\n\nShe no went to the market.",
  temperature=0,
  max_tokens=60,
  top_p=1.0,
  frequency_penalty=0.0,
  presence_penalty=0.0
)

print(response)