#! /usr/bin/env python3

import json
import time

import rclpy
from rclpy.node import Node
from rclpy.executors import MultiThreadedExecutor
from robot_navigator import BasicNavigator, TaskResult
from geometry_msgs.msg import PoseStamped

from std_msgs.msg import Float64MultiArray, Int32, String, Bool, Int32MultiArray

class ProtMsg:
    def __init__(self) -> None:
        self.data = 0

# Holds the current pose of the robot
current_x = 0.0
current_y = 0.0
current_yaw_angle = 0.0

# Holds the pose of the current tag
current_tag_x = 0.0
current_tag_y = 0.0
current_tag_yaw_angle = 0.0

# Holds next machine on the queue
next_machine = None

# Holds the current state of the battery
this_battery_state = 0.0
prev_battery_state = 0.0

# Flag for detecting the change in the battery state
low_battery = False
low_battery_min_threshold = 0.15

# File with the current machines positions
_file = open("Data/machine_positions.json")
machines = json.load(_file)

#PoC Variables
sim_list = [0,675,674,673,672,671,670,669,668]
idx = 0
first_run = True

nav_start = 0
pick_start = 0

class PickingNavigator(Node):
    """
    Navigates and request docking routines
    """

    def __init__(self):
        super().__init__("navigate_to_machines")

        self.current_mc_tag_pub = self.create_publisher(Int32, "/current_tag", 10)

        self.pick_feedback_sub = self.create_publisher(
            Int32MultiArray, "/picked_machine", 10
        )

        self.next_mc_request_sub = self.create_publisher(
            Bool, "/machine_number_request", 10
        )

        self.start_docking_pub = self.create_publisher(Bool, "/start_docking", 10)

        self.next_machine = self.create_subscription(
            Int32, "/next_machine_number", self.set_next_machine, 10
        )

        self.picking_feedback = self.create_subscription(
            Int32MultiArray, "/pick_feedback", self.update_feedback, 10
        )

        self.request_machine_sim()

        # Execution timer
        timer_period = 0.02
        self.timer = self.create_timer(timer_period, self.navigate_to_station)

        # Declare distance metrics in meters
        self.distance_goal_tolerance = 0.01
        self.reached_distance_goal = False

        # Declare angle metrics in radians
        self.heading_tolerance = 0.10
        self.yaw_goal_tolerance = 0.05

        # Picking related variables
        self.picking_feedback = [0, 0]
        self.picking = False

        self.nav_start = 0
        self.pick_start = 0

        # Navigation setup and feedback
        self.nav = BasicNavigator()
        self.nav.waitUntilNav2Active()
        self.done_navigating = False

        self.curr_record = [0]*4

    def set_next_machine(self, msg):
        """
        Sets the next machine in queue
        """
        global next_machine
        for mc_name, mc in machines.items():
            try:
                if mc["Number"] == msg.data:
                    next_machine = mc
            except:
                print("Machine number not found in 'machine_positions.json'")

    def update_feedback(self, msg):
        """
        Gets picking feedback from the docking node
        """
        self.picking_feedback = msg.data

    def request_machine_sim(self):
        global idx
        mc = sim_list[idx]
        idx = idx+1 if idx <= len(sim_list)-2 else 0
        msg = ProtMsg()
        msg.data = mc
        print("NEXT MACHINE: ")
        print(mc)
        self.set_next_machine(msg)

    def navigate_to_station(self):
        """
        Main routine: Goes to the selected machine and requests a docking sequence.
        Will end with "SUCCESS" flag if the docking sequence returns a "Done" value.
        Will stop with "FAIL" flag if the machine is unreachable/unconnected or if the
        docking sequence fails.
        """
        global next_machine, first_run
        if self.done_navigating:
            result = self.nav.getResult()
            if result == TaskResult.SUCCEEDED:
                if not self.picking:
                    self.curr_record[0] = 1
                    self.curr_record[1] = time.time()-self.nav_start
                    start_picking_msg = Bool()
                    start_picking_msg.data = False if (next_machine!=None and next_machine["Number"]==0) else True
                    self.start_docking_pub.publish(start_picking_msg)
                    self.picking_feedback = [0, 0]
                    self.picking = True
                    self.pick_start = time.time()
                else:
                    if self.picking_feedback == [0, 0]:
                        print("Waiting for picking feedback")
                    else:
                        if self.picking_feedback[1] == 0: fb = "Failed"
                        elif self.picking_feedback[1] == 1: fb = "Picked"
                        elif self.picking_feedback[1] == 2: fb = "At home"
                        else: fb = "Unknown feedback code"
                        self.curr_record[2] = time.time()-self.pick_start
                        self.curr_record[3] = self.picking_feedback[1]
                        with open("records.txt", "a") as file_object:
                            file_object.write(f"\n{round(self.curr_record[0],2)},{round(self.curr_record[1],2)},{round(self.curr_record[2],2)},{round(self.curr_record[3],2)}")
                        print(fb)
                        self.picking = False
                        self.done_navigating = False
                        next_machine = None
                        self.request_machine_sim()
            elif result == TaskResult.CANCELED:
                print("Goal was canceled!")
                self.done_navigating = False
                next_machine = None
                self.request_machine_sim()
            elif result == TaskResult.FAILED:
                print("Goal failed!")
                self.done_navigating = False
                next_machine = None
                self.request_machine_sim()
            else:
                print("Goal has an invalid return status!")
                self.done_navigating = False
                next_machine = None
                self.request_machine_sim()
            return None

        msg_req = Bool()
        msg_req.data = True
        self.next_mc_request_sub.publish(msg_req)

        if next_machine == None:
            return None

        current_machine = next_machine
        tag_msg = Int32()
        tag_msg.data = int(current_machine["Tag"])
        self.current_mc_tag_pub.publish(tag_msg)

        self.get_logger().info("Navigate to station")

        # You may use the navigator to clear or obtain costmaps
        self.nav.clearAllCostmaps()  # also have clearLocalCostmap() and clearGlobalCostmap()
        # global_costmap = self.nav.getGlobalCostmap()
        # local_costmap = self.nav.getLocalCostmap()

        # Set the robot's goal pose
        goal_pose = PoseStamped()
        goal_pose.header.frame_id = "map"
        goal_pose.header.stamp = self.nav.get_clock().now().to_msg()

        goal_pose = PoseStamped()
        goal_pose.header.frame_id = "map"
        goal_pose.header.stamp = self.nav.get_clock().now().to_msg()
        goal_pose.pose.position.x = current_machine["Position"][0]
        goal_pose.pose.position.y = current_machine["Position"][1]
        goal_pose.pose.position.z = 0.0
        goal_pose.pose.orientation.x = 0.0
        goal_pose.pose.orientation.y = 0.0
        if current_machine["Position"][2] == "D":
            z_pos = -0.7071068
        elif current_machine["Position"][2] == "E":
            z_pos = 0.7071068
        else:
            z_pos = 0.0
        goal_pose.pose.orientation.z = z_pos  # current_machine["Position"][2]
        goal_pose.pose.orientation.w = 0.7071068

        # Go to the goal pose
        self.nav.goToPose(goal_pose)
        self.nav_start = time.time()

        i = 0

        # Keep doing stuff as long as the robot is moving towards the goal
        while not self.nav.isTaskComplete():
            # Do something with the feedback
            i = i + 1
            feedback = self.nav.getFeedback()
            if feedback and i % 5 == 0:
                print(
                    "Distance remaining: "
                    + "{:.2f}".format(feedback.distance_remaining)
                    + " meters."
                )

                # Some navigation timeout to demo cancellation
                # if Duration.from_msg(feedback.navigation_time) > Duration(seconds=1800.0):
                # self.nav.cancelNav()
        self.done_navigating = True

class BatteryStateSubscriber(Node):
    """
    Subscriber node to the current battery state
    """

    def __init__(self):

        # Initialize the class using the constructor
        super().__init__("battery_state_subscriber")

        # Create a subscriber
        # This node subscribes to messages of type
        # sensor_msgs/BatteryState
        self.subscription_battery_state = self.create_subscription(
            Int32, "/battery_soc", self.get_battery_state, 10
        )

    def get_battery_state(self, msg):
        """
        Update the current battery state.
        """
        global this_battery_state
        global prev_battery_state
        global low_battery
        prev_battery_state = this_battery_state
        this_battery_state = float(msg.data) / 100

        # Check for low battery
        if (
            prev_battery_state >= low_battery_min_threshold
            and this_battery_state < low_battery_min_threshold
        ):
            low_battery = True

class PoseSubscriber(Node):
    """
    Subscriber node to the current 2D pose of the robot
    """

    def __init__(self):

        # Initialize the class using the constructor
        super().__init__("pose_subscriber")

        # Create a subscriber
        # This node subscribes to messages of type
        # std_msgs/Float64MultiArray
        self.subscription_pose = self.create_subscription(
            Float64MultiArray, "/bot_2d_pose", self.get_pose, 1
        )

        self.tag_pose_sub = self.create_subscription(
            Float64MultiArray, "/tag_2d_pose", self.get_tag_pose, 1
        )

    def get_pose(self, msg):
        global current_x
        global current_y
        global current_yaw_angle
        current_2d_pose = msg.data
        current_x = current_2d_pose[0]
        current_y = current_2d_pose[1]
        current_yaw_angle = current_2d_pose[2]

    def get_tag_pose(self, msg):
        global current_tag_x
        global current_tag_y
        global current_tag_yaw_angle
        current_2d_pose = msg.data
        current_tag_x = current_2d_pose[0]
        current_tag_y = current_2d_pose[1]
        current_tag_yaw_angle = current_2d_pose[2]

def main(args=None):
    rclpy.init(args=args)

    try:
        picking_navigator = PickingNavigator()
        battery_state_subscriber = BatteryStateSubscriber()
        pose_subscriber = PoseSubscriber()

        executor = MultiThreadedExecutor(num_threads=4)
        executor.add_node(picking_navigator)
        executor.add_node(battery_state_subscriber)
        executor.add_node(pose_subscriber)

        try:
            executor.spin()
        finally:
            executor.shutdown()
            picking_navigator.destroy_node()
            battery_state_subscriber.destroy_node()
            pose_subscriber.destroy_node()

    finally:
        # Shutdown
        rclpy.shutdown()

if __name__ == "__main__":
    main()


from flask import Flask, render_template, redirect
import pymongo
from flask_pymongo import PyMongo
import scrape_mars

#Spin up the Flask App
app = Flask(__name__, template_folder='templates')

app.config["MONGO_URI"] = "mongodb://localhost:27017/mars_app"
mongo = PyMongo(app)

#Or set inline
#mongo = PyMongo(app, uri="mongodb://localhost:27017/mars_app")

#Creation of connection variable. Connection to local host for local Mongo.
#conn = "mongodb://localhost:27017"

#Pass connection to the pymongo instance"
#client = pymongo.MongoClient(conn)

#Connect to a database. Will create one if not already available
#db = client.redplanet_db
#mars = db.mars_dict

#Drops collection if available to remove dublicates
#db.mars_dict.drop()

#Mongo Creates Collection automatically
#db.mars_dict.inset_many()#insert list of dict

#App Route
@app.route("/")
def index():
    mars = mongo.db.mars.find_one()
    return render_template("index.html", data=mars)

@app.route("/scrape")
def scraper():
    mars = mongo.db.mars
    mars_data = scrape_mars.scrape()
    mars.update({}, mars_data, upsert=True)
    #Redirect to the homepage
    return redirect("/", code=302)

if __name__ == "__main__":
    app.run(debug=True)


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

import numpy as np
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
import os
from os import path
import re

class WeatherMax():
    '''
    pass file directory path with forward slashes
    '''
    def __init__(self,directorypath):
        self.directorypath = directorypath
        self.df = None
        #self.weather_result
        #self.date
        #self.time
        #self.region

    def csvtodataset(self):
        os.chdir(self.directorypath)

        #read csv files in directory

    def patable(self):
        for file in os.listdir():
            if file.endswith(".csv"):
                file1 = file
                df = pd.read_csv(os.path.abspath(file1))

                #select required columns and enrich data with partition attributes

                df = df[['ForecastSiteCode','ObservationTime','ObservationDate','ScreenTemperature','SiteName','Region']]
                df = df.sort_values(by=['ForecastSiteCode','ObservationDate','ObservationTime'])
                df = df.reset_index(drop=True)
                df['ObsYear'] = pd.DatetimeIndex(df['ObservationDate']).year
                df['ObsMonth'] = pd.DatetimeIndex(df['ObservationDate']).month
                df['ObsDay'] = pd.DatetimeIndex(df['ObservationDate']).day

                table = pa.Table.from_pandas(df)

                #create additional files for testing

                file1 = file1.replace(".csv",".")
                file2 = file1 + 'parquet.snappy'
                pq.write_table(table, file2,compression='snappy')

                pq.write_to_dataset(table,root_path='weather_results',partition_cols=['ObsYear','ObsMonth','Region'])

    #create folder schema
    def builddataset(self):
        self.patable()

    def weathertable(self):
        weather_data = pq.ParquetDataset('weather_results/')
        table = weather_data.read()
        weather_table_df = table.to_pandas()
        return weather_table_df

    def weathermax_row(self):
        weather_table_df = self.weathertable()
        weather_result = weather_table_df.loc[weather_table_df['ScreenTemperature'].idxmax()]
        return weather_result

     #return max temp date
    def weather_date(self):
        weather_result = self.weathermax_row()
        date = weather_result['ObservationDate'][:9]
        return date

    #return max temp
    def weather_temp(self):
        weather_result = self.weathermax_row()
        temp = weather_result['ScreenTemperature']
        return temp

    #return max temp region
    def weather_region(self):
        weather_result = self.weathermax_row()
        region = weather_result['Region']
        return region

    #print questions and answers
    def prntresult(self):
        a = "Which date was the hottest day? " + str(self.weather_date()) +'\n'
        b = "What was the (max) temperature on that day? " + str(self.weather_temp()) +'\n'
        c = "In which region was the hottest day? " + str(self.weather_region())

        result = print(a,b,c)
        return result


#!/usr/bin/env python3

"""
    1 Aug 2022 Taeho Choi created for longhorn API python client
    KUBECONFIG needs to be imported properly before running this script
"""

import time
import longhorn
import os
import json

#Warning msg to user
print("#"*40)
print("!! WARNING: This script can cause catastrophic consequences to your infrastructure !! ")
print("!! Please make sure you understand what you are trying to achieve clearly !! ")
print("#"*40)

print("Please make sure you have imported k8s config file correctly")
time.sleep(1)

print("Running kube proxy to redirect svc port to local")
os.system("kubectl port-forward services/longhorn-frontend 8080:http -n longhorn-system & ")
time.sleep(3)

#not using proxy since it will be internal network
os.system("export http_proxy= ; export https_proxy= ")

# If automation/scripting tool is inside the same cluster in which Longhorn is installed
longhorn_url = 'http://longhorn-frontend.longhorn-system/v1'
# If forwarding `longhorn-frontend` service to localhost
longhorn_url = 'http://localhost:8080/v1'

#Check to see if port is listening
os.system("curl http://localhost:8080/v1")

#Create longhorn client object with given URL
client = longhorn.Client(url=longhorn_url)

def print_menu():
    print("\n")
    for key in menu_options.keys():
        print (key, '--', menu_options[key] )
    print("\n")

#Volume Ops
def option1():
     print('Handle option \'Option 1\'')
     volumes = client.list_volume()
     vol_json = json.loads(json.dumps(volumes,default=vars))
     print("#"*200)
     print("These are the longhorn volumes")
     print("#"*200+"\n")
     for _ijson in vol_json["data"]:
        try:
            _pod_name = _ijson["kubernetesStatus"]['workloadsStatus'][0].get('podName')
        except TypeError:
            _pod_name = "N/A"
        print("ID:{} STATE:{} CONTROLLER:{:<15} SIZE:{:<4}GB NS:{:<15} POD_NAME:{:<15}".format(_ijson["id"], _ijson["state"], _ijson["replicas"][0].get("hostId"),int( _ijson["size"])/1024/1024/1024,_ijson["kubernetesStatus"].get("namespace"), _pod_name))

def option2():
     print('Handle option \'Option 2\'')
     vol_id = input("what is the volume name or id to check: ")
     output = client.by_id_volume(id=vol_id)
     print(json.dumps(output, indent=4, default=vars))

def option3():
     print('Handle option \'Option 3\'')
     vol_id = input("what is the volume name or id to attach: ")
     node_id = input("what is the node id to attach: ")
     testvol1 = client.by_id_volume(id=vol_id)
     output = testvol1.attach(hostId=node_id)
     print(json.dumps(output, indent=4, default=vars))

def option4():
     print('Handle option \'Option 4\'')
     vol_id = input("what is the volume name or id to detach: ")
     testvol1 = client.by_id_volume(id=vol_id)
     print(type(testvol1))
     print(testvol1)
     output = testvol1.detach()
     print(json.dumps(output, indent=4, default=vars))

def option5():
     print('Handle option \'Option 5\'')
     vol_id = input("what is the volume name or id for snapshot: ")
     snapshot = input("what is the snapshot name to create: ")
     vol_client = client.by_id_volume(id=vol_id)
     output = vol_client.snapshotCreate(name=snapshot)
     print(json.dumps(output, indent=4, default=vars))

def option6():
     print('Handle option \'Option 6\'')
     vol_id = input("what is the volume name or id for backup: ")
     snapshot_name = input("what is the snampshot name to backup: ")
     vol_client = client.by_id_volume(id=vol_id)
     output = vol_client.snapshotBackup(name=snapshot_name)
     print(json.dumps(output, indent=4, default=vars))

def option7():
     print('Handle option \'Option 7\'')
     vol_id = input("what is the volume name or id to update replica count: ")
     no_rep = int(input("what is the new no of replica count?: "))
     vol_client = client.by_id_volume(id=vol_id)
     output = vol_client.updateReplicaCount(replicaCount=no_rep)
     print(json.dumps(output, indent=4, default=vars))

#Node Ops
def option8():
     print('Handle option \'Option 8\'')
     volumes = client.list_node()
     vol_json = json.loads(json.dumps(volumes,default=vars))
     print("#"*40)
     print("These are the longhorn nodes")
     print("#"*40)
     for i in vol_json["data"]:
        print(i["id"])
     print("#"*40)

def option9():
     print('Handle option \'Option 9\'')
     node_id = input("what is the node id or name to check: ")
     output = client.by_id_node(id=node_id)
     print(json.dumps(output, indent=4, default=vars))

def option10():
     print('Handle option \'Option 10\'')
     node_id = input("what is the node id or name to disable: ")
     node1_obj = client.by_id_node(id=node_id)
     output = client.update(node1_obj, allowScheduling=False)
     print(json.dumps(output, indent=4, default=vars))

def option11():
     print('Handle option \'Option 11\'')
     node_id = input("what is the node id or name to enable: ")
     node1_obj = client.by_id_node(id=node_id)
     output = client.update(node1_obj, allowScheduling=True)
     print(json.dumps(output, indent=4, default=vars))

#Setting Ops
def option12():
     print('Handle option \'Option 12\'')
     settings = client.list_setting()
     settings_json = json.loads(json.dumps(settings,default=vars))
     print("#"*40)
     print("These are the longhorn cluster settings")
     print("#"*40)
     print(json.dumps(settings,indent=4,default=vars))

def option13():
     print('Handle option \'Option 13\'')
     setting_id = input("what is setting id or name to check: ")
     output = client.by_id_setting(id=setting_id)
     print(json.dumps(output, indent=4, default=vars))

def option14():
     print('Handle option \'Option 14\'')
     setting_id = input("what is the setting id or name to update: ")
     new_val = input("what is the new value for the setting: ")
     setting_obj = client.by_id_setting(id=setting_id)
     output = client.update(setting_obj, value=new_val)
     print(json.dumps(output, indent=4, default=vars))

def option15():
     print('Handle option \'Option 15\'')
     print('Thanks message before exiting')
     exit()

#Set dict for menu option and func mapping
menu_options = {
    #Volume operation
    1: 'List all volumes',
    2: 'Get volume by NAME/ID',
    3: 'Attach volume to node',
    4: 'Detach TESTVOL1',
    5: 'Create a snapshot of TESTVOL1 with NAME',
    6: 'Create a backup from a snapshot NAME',
    7: 'Update the number of replicas of TESTVOL1',
    #node operation
    8: 'List all nodes',
    9: 'Get node by NAME/ID',
    10: 'Disable scheduling for NODE1',
    11: 'Enable scheduling for NODE1',
    #Setting operation
    12: 'List all settings',
    13: 'Get setting by NAME/ID',
    14: 'Update a setting',
    15: 'Exit',
}

options = {
                1: option1,
                2: option2,
                3: option3,
                4: option4,
                5: option5,
                6: option6,
                7: option7,
                8: option8,
                9: option9,
                10: option10,
                11: option11,
                12: option12,
                13: option13,
                14: option14,
                15: option15,
        }

if __name__=='__main__':
    while(True):
        print_menu()
        option = ''
        try:
            option = int(input('Enter your choice: '))
        except:
            print('Wrong input. Please enter a number ...')
        #Check what choice was entered and act accordingly
        if option in options:
            options[option]()
        else:
            print('Invalid option. Please enter a number between 1 and 15.')


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

#program to demonstrate  polynomial interpolation

import numpy as np
import math

#ask user for x
x0= float(input("Enter xo: \t"))
x1= float(input("Enter x1: \t"))
x2= float(input("Enter x1: \t"))
x3= float(input("Enter x2: \t"))

#function to construct each row of the matrix
def matrix_constr(x):
    arr=[]
    for i in range (3,-1,-1):
        arr.append(x**i)
    return arr

#trace in value of x
r_0=matrix_constr(x0)
r_1=matrix_constr(x1)
r_2=matrix_constr(x2)
r_3=matrix_constr(x3)

#matrix appending as 1d array list
big_arr=[]
big_arr=np.append(big_arr,r_0)
big_arr=np.append(big_arr,r_1)
big_arr=np.append(big_arr,r_2)
big_arr=np.append(big_arr,r_3)
print(big_arr)

# In[12]:

#reshape to matrix 4x4
matrix=np.reshape(big_arr,(4,4))
print(matrix)
print(len(matrix))
#obtain number of row and column
row=matrix.shape[0]
col=matrix.shape[1]
print('row={}'.format(row))
print('colums={}'.format(col))

# In[13]:

#construct y[]
y=[]
y0= float(input("Enter yo: \t"))
y1= float(input("Enter y1: \t"))
y2= float(input("Enter y1: \t"))
y3= float(input("Enter y2: \t"))
y=np.append(y,[y0,y1,y2,y3])
print(y)

# In[14]:

#append a column of 0 at the end of matrix
a=np.append(matrix,np.zeros([4,1]),axis=1)
print(a)

# In[15]:

for i in range(row):
    a[i][-1]=y[i]
print(a)

# In[16]:

#Gauss elimination procedure
#after this step we will get row echelon form

for i in range (0,col-1):
    for j in range(i+1,row):
        a[j,:]= -(a[j][i]/a[i][i]*a[i,:])+a[j,:]
print(a)

# In[28]:

#push the result into a matrix
#call matrix r to store result
r=np.zeros(4)

for i in range (row-1,-1,-1):
    r[i]= a[i,-1]- a[i,0:col]@r

print (r)


import torch
from torchvision import datasets, transforms

# Define paths for the data
data_dir = 'flowers'
train_dir = data_dir + '/train'
valid_dir = data_dir + '/valid'
test_dir = data_dir + '/test'

dirs = {
    'train': train_dir,
    'val': valid_dir,
    'test': test_dir
}

# Define transforms for the training, validation, and testing sets
data_transforms = {
    'train': transforms.Compose([
        transforms.RandomRotation(30),
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
    'val': transforms.Compose([
        transforms.Resize(255),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
    'test': transforms.Compose([
        transforms.Resize(255),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])
}

# Load the datasets with ImageFolder
image_datasets = {
    x: datasets.ImageFolder(dirs[x], transform=data_transforms[x])
    for x in ['train', 'val', 'test']
}

# Using the image datasets and the transforms, define the dataloaders
dataloaders = {
    x: torch.utils.data.DataLoader(image_datasets[x], batch_size=64, shuffle=True)
    for x in ['train', 'val', 'test']
}


"""
(39) Combination Sum
https://leetcode.com/problems/combination-sum/description/

Given an array of distinct integers candidates and a target integer target,
return a list of all unique combinations of candidates where the chosen numbers sum to target.
You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the
frequency of at least one of the chosen numbers is different.

The test cases are generated such that the number of unique combinations that sum up to
target is less than 150 combinations for the given input.
"""

class Solution:
    def combinationSum(self, candidates, target):
        # Use depth-first search with 'path' memoization to not explore paths you've already explored.
        # a 'path' in this case is a candidate representation of a target.
        visited = set()
        combinations = []

        stack = [(target, tuple([0]*len(candidates)))]
        while len(stack) > 0:
            nextTarget, frequency = stack.pop()
            if frequency in visited:
                continue

            visited.add(frequency)
            if nextTarget == 0:
                combination = []
                for i in range(len(candidates)):
                    f = frequency[i]
                    candidate = candidates[i]
                    combination.extend([candidate]*f)

                combinations.append(combination)
                continue

            for i in range(len(candidates)):
                candidate = candidates[i]
                child = nextTarget - candidate
                if child >= 0:
                    childFreq = list(frequency)
                    childFreq[i] += 1
                    stack.append((child, tuple(childFreq)))

        return combinations

if __name__ == '__main__':
    sol = Solution()
    target = 8
    candidates = [2,3,5]
    print(sol.combinationSum(candidates, target))


# -*- coding: utf-8 -*-
from plone.app.contenttypes.testing import PLONE_APP_CONTENTTYPES_FIXTURE
from plone.app.robotframework.testing import REMOTE_LIBRARY_BUNDLE_FIXTURE
from plone.app.testing import applyProfile
from plone.app.testing import FunctionalTesting
from plone.app.testing import IntegrationTesting
from plone.app.testing import PloneSandboxLayer
from plone.testing import z2

import gwopa.theme

class GwopaThemeLayer(PloneSandboxLayer):

    defaultBases = (PLONE_APP_CONTENTTYPES_FIXTURE,)

    def setUpZope(self, app, configurationContext):
        # Load any other ZCML that is required for your tests.
        # The z3c.autoinclude feature is disabled in the Plone fixture base
        # layer.
        self.loadZCML(package=gwopa.theme)

    def setUpPloneSite(self, portal):
        applyProfile(portal, 'gwopa.theme:default')

GWOPA_THEME_FIXTURE = GwopaThemeLayer()

GWOPA_THEME_INTEGRATION_TESTING = IntegrationTesting(
    bases=(GWOPA_THEME_FIXTURE,),
    name='GwopaThemeLayer:IntegrationTesting'
)

GWOPA_THEME_FUNCTIONAL_TESTING = FunctionalTesting(
    bases=(GWOPA_THEME_FIXTURE,),
    name='GwopaThemeLayer:FunctionalTesting'
)

GWOPA_THEME_ACCEPTANCE_TESTING = FunctionalTesting(
    bases=(
        GWOPA_THEME_FIXTURE,
        REMOTE_LIBRARY_BUNDLE_FIXTURE,
        z2.ZSERVER_FIXTURE
    ),
    name='GwopaThemeLayer:AcceptanceTesting'
)


import time
#import img2txt
import os
import json
from PIL import Image

def load_file(fpath):  # fpath是具体的文件 ，作用：#str to list
    assert os.path.exists(fpath)  # assert() raise-if-not
    with open(fpath, 'r') as fid:
        lines = fid.readlines()
    records = [json.loads(line.strip('\n')) for line in lines]  # str to list
    return records

def img2txt(odgtpath, respath):
    records = load_file(odgtpath)  # 提取odgt文件数据
    record_list = len(records)  # 获得record的长度，循环遍历所有数据。
    print(os.getcwd())
    # os.mkdir(os.getcwd() + respath)
    with open(respath, 'w') as txt:
        for i in range(record_list):
            file_name = records[i]['ID'] + '.jpg'
            file_name = str("/Image/" + file_name)
            txt.write(file_name + '\n')

def tonormlabel(odgtpath, storepath):
    records = load_file(odgtpath)           #records = img2txt.load_file(odgtpath)
    record_list = len(records)
    print(record_list)
    categories = {}
    # txt = open(respath, 'w')
    for i in range(record_list):
        txt_name = storepath + records[i]['ID'] + '.txt'
        file_name = records[i]['ID'] + '.jpg'
        #print(i)
        im = Image.open("./Image/" + file_name)
        height = im.size[1]
        width = im.size[0]
        file = open(txt_name, 'w')
        gt_box = records[i]['gtboxes']
        gt_box_len = len(gt_box)  # 每一个字典gtboxes里，也有好几个记录，分别提取记录。
        for j in range(gt_box_len):
            category = gt_box[j]['tag']
            if category not in categories:  # 该类型不在categories，就添加上去
                new_id = len(categories) + 1  # ID递增
                categories[category] = new_id
            category_id = categories[category]  # 重新获取它的类别ID
            fbox = gt_box[j]['fbox']  # 获得全身框
#            norm_x = fbox[0] / width
#            norm_y = fbox[1] / height
            norm_w = fbox[2] / width
            norm_h = fbox[3] / height
            norm_x = fbox[0] / width + 0.5 * norm_w
            norm_y = fbox[1] / height + 0.5 * norm_h

            '''
            norm_x = 0 if norm_x <= 0 else norm_x
            norm_x = 1 if norm_x >= 1 else norm_x
            norm_y = 0 if norm_y <= 0 else norm_y
            norm_y = 1 if norm_y >= 1 else norm_y
            norm_w = 0 if norm_w <= 0 else norm_w
            norm_w = 1 if norm_w >= 1 else norm_w
            norm_h = 0 if norm_h <= 0 else norm_h
            norm_h = 1 if norm_h >= 1 else norm_h
            '''
            blank = ' '
            if j == gt_box_len-1:
                file.write(str(category_id - 1) + blank + '{:.6f}'.format(norm_x) + blank + '{:.6f}'.format(norm_y) + blank
                           + '{:.6f}'.format(norm_w) + blank + '{:.6f}'.format(norm_h))
            else:
                file.write(str(category_id - 1) + blank + '{:.6f}'.format(norm_x) + blank + '{:.6f}'.format(norm_y) + blank
                           + '{:.6f}'.format(norm_w) + blank + '{:.6f}'.format(norm_h) + '\n')

if __name__ == '__main__':
    odgtpath = "./annotation_val.odgt"           #"/datasets/crowdhuman/annotation_train.odgt"
    respath = "./annotation_name.txt"  #"/datasets/crowdhuman/train_name.txt"
    storepath = "./annotation/"                    #"/datasets/crowdhuman/labels/train_all/Image/"
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))  # 格式化输出时间
    start = time.time()
    tonormlabel(odgtpath, storepath)
    end = time.time()
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
    print('已完成轉換，共耗時{:.5f}s'.format(end - start))


#!/usr/bin/env python

"""
A thermometer display stimulus.

This module contains a class implementing a thermometer display stimulus.

"""
from __future__ import absolute_import, print_function, division
from builtins import *

__author__ = 'Florian Krause <florian@expyriment.org>'
__version__ = ''
__revision__ = ''
__date__ = ''

import pygame

from expyriment.stimuli import Rectangle
from expyriment.stimuli._visual import Visual

class ThermometerDisplay(Visual):
    """A class implementing a thermometer display."""

    def __init__(self, state, goal=None, size=(128,340), nr_segments=10,
                 gap=3, frame_line_width=20, active_colour=(150,150,150),
                 inactive_colour=(0,0,0), frame_colour=(100,100,100),
                 goal_colour=(0,255,0), gap_colour=(255,255,255),
                 position=None):
        """Initializing a thermometer display.

        Parameters:
        -----------
        state : int
            The state of the thermometer in percent.
        goal : int, optional
            The goal state indication in percent.
        size : (int, int), optional
            The size of the thermometer display (default=(128,340)).
        nr_segments : int, optional
            The number of segments to use (default=10).
        gap : int, optional
            The visual gap between the individual segments (default=3).
        frame_line_width : int, optional
            The line width of the frame around the thermometer display
            (default=20).
        active_colour : (int, int, int), optional
            The colour of the active segments (default=(150,150,150)).
        inactive_colour : (int, int, int), optional
            The colour of the inactive segments (default=(0,0,0)).
        frame_colour : (int, int, int), optional
            The colour of the frame around the thermometer display
            (default=(100,100,100)).
        goal_colour : (int, int, int), optional
            The colour of the goal indicator (default=(0,255,0)).
        gap_colour : (int, int, int), optional
            The gap colour of the thermometer stimulus
            (default=(255,255,255)).
        position : (int, int), optional
            The position of the thermometer display).
        """

        self._state = state
        self._goal = goal
        self._size = size
        self._nr_segments = nr_segments
        self._gap = gap
        self._frame_line_width = frame_line_width
        self._active_colour = active_colour
        self._inactive_colour = inactive_colour
        self._frame_colour = frame_colour
        self._goal_colour = goal_colour
        self._gap_colour = gap_colour
        self._position = position

        Visual.__init__(self, position)

    _getter_exception_message = "Cannot set {0} if surface exists!"

    @property
    def state(self):
        """Getter for state."""
        return self._state

    @state.setter
    def state(self, value):
        """Setter for state."""

        if self.has_surface:
            raise AttributeError(
                ThermometerDisplay._getter_exception_message.format("state"))
        else:
            self._state = value

    @property
    def goal(self):
        """Getter for goal."""
        return self._goal

    @goal.setter
    def goal(self, value):
        """Setter for goal."""

        if self.has_surface:
            raise AttributeError(
                ThermometerDisplay._getter_exception_message.format("goal"))
        else:
            self._goal = value

    @property
    def size(self):
        """Getter for size."""
        return self._size

    @size.setter
    def size(self, value):
        """Setter for size."""

        if self.has_surface:
            raise AttributeError(
                ThermometerDisplay._getter_exception_message.format("size"))
        else:
            self._size = value

    @property
    def nr_segments(self):
        """Getter for nr_segments."""
        return self._nr_segments

    @nr_segments.setter
    def nr_segments(self, value):
        """Setter for nr_segments."""

        if self.has_surface:
            raise AttributeError(
                ThermometerDisplay._getter_exception_message.format(
                    "nr_segments"))
        else:
            self._nr_segments = value

    @property
    def gap(self):
        """Getter for gap."""
        return self._gap

    @gap.setter
    def gap(self, value):
        """Setter for gap."""

        if self.has_surface:
            raise AttributeError(
                ThermometerDisplay._getter_exception_message.format("gap"))
        else:
            self._gap = value

    @property
    def frame_line_width(self):
        """Getter for frame_line_width."""
        return self._frame_line_width

    @frame_line_width.setter
    def frame_line_width(self, value):
        """Setter for frame_line_width."""

        if self.has_surface:
            raise AttributeError(
                ThermometerDisplay._getter_exception_message.format(
                    "frame_line_width"))
        else:
            self._frame_line_width = value

    @property
    def active_colour(self):
        """Getter for active_colour."""
        return self._active_colour

    @active_colour.setter
    def active_colour(self, value):
        """Setter for active_colour."""

        if self.has_surface:
            raise AttributeError(
                ThermometerDisplay._getter_exception_message.format(
                    "active_colour"))
        else:
            self._active_colour = value

    @property
    def inactive_colour(self):
        """Getter for inactive_colour."""
        return self._inactive_colour

    @inactive_colour.setter
    def inactive_colour(self, value):
        """Setter for inactive_colour."""

        if self.has_surface:
            raise AttributeError(
                ThermometerDisplay._getter_exception_message.format(
                    "inactive_colour"))
        else:
            self._inactive_colour = value

    @property
    def frame_colour(self):
        """Getter for frame_colour."""
        return self._frame_colour

    @frame_colour.setter
    def frame_colour(self, value):
        """Setter for frame_colour."""

        if self.has_surface:
            raise AttributeError(
                ThermometerDisplay._getter_exception_message.format(
                    "frame_colour"))
        else:
            self._frame_colour = value

    @property
    def goal_colour(self):
        """Getter for goal_colour."""
        return self._goal_colour

    @goal_colour.setter
    def goal_colour(self, value):
        """Setter for goal_colour."""

        if self.has_surface:
            raise AttributeError(
                ThermometerDisplay._getter_exception_message.format(
                    "goal_colour"))
        else:
            self._goal_colour = value

    @property
    def gap_colour(self):
        """Getter for gap_colour."""
        return self._gap_colour

    @gap_colour.setter
    def gap_colour(self, value):
        """Setter for gap_colour."""

        if self.has_surface:
            raise AttributeError(
                ThermometerDisplay._getter_exception_message.format(
                    "gap_colour"))
        else:
            self._gap_colour = value

    def _create_surface(self):
        """Create the surface of the stimulus."""

        surface = pygame.surface.Surface((self._size[0] +
                                          self._frame_line_width,
                                          self._size[1] +
                                          self._frame_line_width),
                                         pygame.SRCALPHA).convert_alpha()

        if self._gap_colour is not None:
            surface.fill(self._gap_colour)

        parts = []
        width = self._size[0] - self._frame_line_width - \
                2 * self._frame_line_width % 2
        height = self._size[1] - self._frame_line_width - \
                 2 * self._frame_line_width % 2 + 1
        s_height = int(height - (self._nr_segments + 1) *
                       self._gap) // self._nr_segments
        for x in range(self._nr_segments):
            if x < self._state / 100.0 * self._nr_segments:
                colour = self._active_colour
            else:
                colour = self._inactive_colour
            s = Rectangle((width - self._gap * 2,
                           s_height), colour=colour,
                          position=(0, -height // 2 + s_height // 2 + x *
                                    height // self.nr_segments + self._gap))
            parts.append(s)
        parts.append(Rectangle(self._size, colour=self._frame_colour,
                               line_width=self._frame_line_width,
                               position=self._position))
        parts.append(Rectangle((width - self._gap, height - self._gap * 2),
                               colour=self._gap_colour,
                               line_width=self._gap,
                               position=self._position))

        if self._goal is not None:
            x = int(round(self._goal / 100.0 * self._nr_segments)) - 1
            current_y_pos = -height // 2 + s_height // 2 + \
                            x * height // self._nr_segments + self._gap
            above_y_pos = -height // 2 + s_height // 2 + \
                          (x + 1) * height // self._nr_segments + self._gap
            g1 = Rectangle((self._frame_line_width * 1.25,
                            self._frame_line_width * 1.25),
                           colour=self._goal_colour,
                           position=(
                               -self._size[0] // 2 - self._frame_line_width // 2,
                               (current_y_pos + above_y_pos) // 2))
            g2 = Rectangle((self._frame_line_width * 1.25,
                            self._frame_line_width * 1.25),
                           colour=self._goal_colour,
                           position=(
                               self._size[0] // 2 + self._frame_line_width // 2,
                               (current_y_pos + above_y_pos) // 2))
            g1.rotate(45)
            g2.rotate(45)
            parts.append(g1)
            parts.append(g2)

        for stim in parts:
            stim.rect = pygame.Rect((0, 0), stim.surface_size)
            surface_size = surface.get_size()
            stim.rect.center = [stim.position[0] + surface_size[0] // 2,
                                - stim.position[1] + surface_size[1] // 2]
            surface.blit(stim._get_surface(), stim.rect)
        return surface

if __name__ == "__main__":
    from ... import control
    control.set_develop_mode(True)
    control.defaults.event_logging = 0
    exp = control.initialize()
    thermometer_display = ThermometerDisplay(50, 50)
    thermometer_display.present()
    #exp.clock.wait(1000)
    exp.keyboard.wait()


import cv2
import numpy as np
from PIL import Image

def main():
    capture = cv2.VideoCapture(0)

    while True:
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        ret, frame = capture.read()
        cv2.imshow('Current', frame)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        image = Image.fromarray(gray)

        qrDetector=cv2.QRCodeDetector()
        retval , points ,straight_qrcode = qrDetector.detectAndDecode(frame)
        if len(retval)>0:
            print(retval)
            straight_qrcode = np.uint8(straight_qrcode)
            cv2.imshow('rectified qr code',straight_qrcode)

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


from fastapi import APIRouter
from BusinessLayer.PersonaNatural import *
from EntityLayer.PersonaNaturalEntity import *
from fastapi.encoders import jsonable_encoder
from Utilidades.Entidades.ResponseAPI import ResponseAPI, ResponseAPIError

PersonaNaturalRouter = APIRouter()
ApiName = "PersonaNatural"

@PersonaNaturalRouter.post(f"/api/{ApiName}/Save", tags=[ApiName])
def Save(Ent: PersonaNaturalSaveModel):
    try:
        Ent = PersonaNatural.Save(Ent)
        return jsonable_encoder(ResponseAPI.Response(Ent))
    except Exception as e:
        print(e)
        return jsonable_encoder(ResponseAPIError.Error())

@PersonaNaturalRouter.get(f"/api/{ApiName}/GetItems/", tags=[ApiName])
def GetItems():
    try:
        jsonData = PersonaNatural.GetItems()
        return jsonable_encoder(ResponseAPI.Response(jsonData))
    except Exception as e:
        print(e)
        return jsonable_encoder(ResponseAPIError.Error())

@PersonaNaturalRouter.get(f"/api/{ApiName}/GetItem/{{Id}}/", tags=[ApiName])
def GetItem(Id: int):
    try:
        jsonData = PersonaNatural.GetItem(Id)
        return jsonable_encoder(ResponseAPI.Response(jsonData))
    except Exception as e:
        print(e)
        return jsonable_encoder(ResponseAPIError.Error())

@PersonaNaturalRouter.delete(f"/api/{ApiName}/Delete/{{Id}}", tags=[ApiName])
def Delete(Id: int):
    try:
        jsonData = PersonaNatural.Delete(Id)
        return jsonable_encoder(ResponseAPI.Response(jsonData))
    except Exception as e:
        print(e)
        return jsonable_encoder(ResponseAPIError.Error())

@PersonaNaturalRouter.get(f"/api/{ApiName}/GetMainItems", tags=[ApiName])
def GetMainItems():
    try:
        jsonData = PersonaNatural.GetMainItems()
        return jsonable_encoder(ResponseAPI.Response(jsonData))
    except Exception as e:
        print(e)
        return jsonable_encoder(ResponseAPIError.Error())

@PersonaNaturalRouter.get(f"/api/{ApiName}/GetCabeceraItem/{{Id}}/", tags=[ApiName])
def GetCabeceraItem(Id: int):
    try:
        jsonData = PersonaNatural.GetCabeceraItem(Id)
        return jsonable_encoder(ResponseAPI.Response(jsonData))
    except Exception as e:
        print(e)
        return jsonable_encoder(ResponseAPIError.Error())


import torch.nn as nn
import torch.nn.functional as F

from   ...utils import CONFIG
from   ..encoders.resnet_enc import ResNet_D
from   ..ops import GuidedCxtAtten, SpectralNorm

class ResGuidedCxtAtten(ResNet_D):

    def __init__(self, block, layers, norm_layer=None, late_downsample=False):
        super(ResGuidedCxtAtten, self).__init__(block, layers, norm_layer, late_downsample=late_downsample)
        first_inplane = 3 + CONFIG.model.trimap_channel
        self.shortcut_inplane = [first_inplane, self.midplanes, 64, 128, 256]
        self.shortcut_plane = [32, self.midplanes, 64, 128, 256]

        self.shortcut = nn.ModuleList()
        for stage, inplane in enumerate(self.shortcut_inplane):
            self.shortcut.append(self._make_shortcut(inplane, self.shortcut_plane[stage]))

        self.guidance_head = nn.Sequential(
            nn.ReflectionPad2d(1),
            SpectralNorm(nn.Conv2d(3, 16, kernel_size=3, padding=0, stride=2, bias=False)),
            nn.ReLU(inplace=True),
            self._norm_layer(16),
            nn.ReflectionPad2d(1),
            SpectralNorm(nn.Conv2d(16, 32, kernel_size=3, padding=0, stride=2, bias=False)),
            nn.ReLU(inplace=True),
            self._norm_layer(32),
            nn.ReflectionPad2d(1),
            SpectralNorm(nn.Conv2d(32, 128, kernel_size=3, padding=0, stride=2, bias=False)),
            nn.ReLU(inplace=True),
            self._norm_layer(128)
        )

        self.gca = GuidedCxtAtten(128, 128)

        # initialize guidance head
        for layers in range(len(self.guidance_head)):
            m = self.guidance_head[layers]
            if isinstance(m, nn.Conv2d):
                if hasattr(m, "weight_bar"):
                    nn.init.xavier_uniform_(m.weight_bar)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)

    def _make_shortcut(self, inplane, planes):
        return nn.Sequential(
            SpectralNorm(nn.Conv2d(inplane, planes, kernel_size=3, padding=1, bias=False)),
            nn.ReLU(inplace=True),
            self._norm_layer(planes),
            SpectralNorm(nn.Conv2d(planes, planes, kernel_size=3, padding=1, bias=False)),
            nn.ReLU(inplace=True),
            self._norm_layer(planes)
        )

    def forward(self, x):

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.activation(out)
        out = self.conv2(out)
        out = self.bn2(out)
        x1 = self.activation(out) # N x 32 x 256 x 256
        out = self.conv3(x1)
        out = self.bn3(out)
        out = self.activation(out)

        im_fea = self.guidance_head(x[:,:3,...]) # downsample origin image and extract features
        if CONFIG.model.trimap_channel == 3:
            unknown = F.interpolate(x[:,4:5,...], scale_factor=1/8, mode='nearest')
        else:
            unknown = F.interpolate(x[:,3:,...].eq(1.).float(), scale_factor=1/8, mode='nearest')

        x2 = self.layer1(out) # N x 64 x 128 x 128
        x3= self.layer2(x2) # N x 128 x 64 x 64
        x3, offset = self.gca(im_fea, x3, unknown) # contextual attention
        x4 = self.layer3(x3) # N x 256 x 32 x 32
        out = self.layer_bottleneck(x4) # N x 512 x 16 x 16

        fea1 = self.shortcut[0](x) # input image and trimap
        fea2 = self.shortcut[1](x1)
        fea3 = self.shortcut[2](x2)
        fea4 = self.shortcut[3](x3)
        fea5 = self.shortcut[4](x4)

        return out, {'shortcut':(fea1, fea2, fea3, fea4, fea5),
                     'image_fea':im_fea,
                     'unknown':unknown,
                     'offset_1':offset}

if __name__ == "__main__":
    from networks.encoders.resnet_enc import BasicBlock
    m = ResGuidedCxtAtten(BasicBlock, [3, 4, 4, 2])
    for m in m.modules():
        print(m)


import os
import pandas as pd

def tag_line_items(df, configs):
    """
    Labels rows in the dataframe based on their association with anchor items.

    This function processes the input dataframe to identify line items based on
    the provided configurations. The function checks the presence of the minimum
    number of items on the same line as specified in the configuration.

    Parameters:
    - df (pandas.DataFrame): The input dataframe containing prediction results.
      Expected columns in the dataframe include 'label', 'page_id', 'ymin', and 'ymax',
      among others.

    - configs (list of dict): A list of configurations where each configuration is a dictionary
      containing the following keys:
      - 'item_classes' (list of str): Labels that should be present on the same line.
      - 'anchor_class' (str): The label used to identify and anchor a line.
      - 'table_id' (str): Identifier for the type of table or section.
      - 'min_items' (int): Minimum number of 'item_classes' labels that should be present on the
        same line to be considered valid.
      (Optional configurations can be added as needed.)

    Returns:
    - pandas.DataFrame: A modified dataframe with additional columns 'line' and 'table_id'
      indicating the line number and table type for each row, respectively.

    Example:
    df = pd.DataFrame({
        'label': ['payment', 'deduction', 'payment_type', 'deduction_type'],
        'page_id': [1, 1, 1, 1],
        'ymin': [10, 20, 10, 20],
        'ymax': [15, 25, 15, 25]
     })
    configs = [
        {
            'item_classes': ['payment_type', 'payment'],
            'anchor_class': 'payment',
            'table_id': 'payable',
            'min_items': 2,
        }
     ]
    tagged_df = tag_line_items(df, configs)
    """

    if 'line' not in df.columns:
        df['line'] = None
    if 'table_id' not in df.columns:
        df['table_id'] = None

    for config in configs:
        line_id = 1
        anchors = df[df['label'].isin([config['anchor_class']])]

        # Iterate through each unique page_id
        for page in df['page_id'].unique():
            page_anchors = anchors[anchors['page_id'] == page]

            for _, anchor_row in page_anchors.iterrows():
                # Identify the row based on ymin of the anchor
                same_row_indices = df[(df['page_id'] == page) & (df['ymin'] == anchor_row['ymin'])].index

                # Check if at least min_line_items are found
                line_items_found = sum(df.loc[same_row_indices, 'label'].isin(config['item_classes']))

                if line_items_found >= config['min_items']:
                    df.loc[same_row_indices, 'line'] = line_id
                    df.loc[same_row_indices, 'table_id'] = config['table_id']
                    line_id += 1

    return df

def process_csv_files(directory_path):
    csv_files = [f for f in os.listdir(directory_path) if f.endswith('.csv')]

    for csv_file in csv_files:
        df = pd.read_csv(os.path.join(directory_path, csv_file))

        configurations = [
            {
                'item_classes': ['payment_type', 'payment'],
                'anchor_class': 'payment',
                'table_id': 'payment',
                'min_items': 2,
            },
            {
                'item_classes': ['deduction_type', 'deduction'],
                'anchor_class': 'deduction',
                'table_id': 'deduction',
                'min_items': 2,
            },
        ]

        tagged_df = tag_line_items(df, configurations)
        print(f"Processed contents of {csv_file}:")
        print(tagged_df)

process_csv_files('data/preds_csv')


import gtk
import pango
import cairo
import gobject
import os
import math
from appenv import env
from prefs import prefs
from valcabuary import valcabuary
from prefixdb import prefixdb
from corrector import corrector
from tooltip import cls_tooltip
from simpledb import simpledb
from contentmgr import cls_contentmgr
from iconmgr import iconmgr
from messages import *

class cls_appmain(object):
    def __init__(self):
        self.builder = gtk.Builder()
        self.builder.add_from_file(os.path.join(env.get_ui_dir(),
                                   'mainwindow.glade'))
        self.builder.connect_signals(self)
        # window
        self.window = self.builder.get_object('window')
        # treeview
        self.treeview = self.builder.get_object('treeview')
        # notebook & content manager
        self.notebook = self.builder.get_object('notebook')
        self.contentmgr = cls_contentmgr(self.notebook)
        self.contentmgr.go_for_welcome()
        # init 3 buttons
        self.btnprefs = self.builder.get_object('btnprefs')
        self.btnprefs.connect_object('event',
                                    self._btnprefs_pressed,
                                    self.btnprefs)
        self.btncheck = self.builder.get_object('btncheck')
        self.btnhit = self.builder.get_object('btnhit')
        # status bar
        self.statusbar = self.builder.get_object('statusbar')
        self.statusbar.push(MSGID_COPYRIGHT,
                            messages[MSGID_COPYRIGHT])
        # input entry
        self.entry = self.builder.get_object('entry')
        self.id_entrychanged = self.entry.connect('changed',
                                               self._entry_changed)
        # hpaned
        self.hpaned = self.builder.get_object('hpaned')

        # init other stuffs
        self.__init_window()
        self.__init_acclgrps()
        self.__init_treeview()
        self.__init_menu()
        self.__init_tooltip()
        self.__init_monitor()
        self.__init_trayicon()

    def __init_window(self):
        # init app icon
        self.window.set_icon_from_file(iconmgr.get('ppdict'))

        # configure window according to prefs
        w, h = self.window.get_default_size()
        if h > 0 and w > 0:
            self.window.set_geometry_hints(min_height=h, min_width=w)

        sw = prefs.getint('savedwidth')
        sh = prefs.getint('savedheight')

        if sw < w:
            sw = w

        if sh < h:
            sh = h

        self.window.set_default_size(sw, sh)
        self.set_transparency(prefs.getint('transparency'))

    def __init_monitor(self):
        if not prefs.getbool('monitorsel'):
            return

        # pop up the window when clipboard updtes
        if prefs.getint('popupopt') == 0:
            self.start_monitor_clipboard()
        elif prefs.getint('popupopt') == 1:
            pass

    def __init_tooltip(self):
        # tooltip
        self.tooltip = cls_tooltip()
        self.tooltip.connect('clicked', self._tooltip_clicked)

    def __init_acclgrps(self):
        # init global accel group
        self.agr = gtk.AccelGroup()
        self.window.add_accel_group(self.agr)

        key, mod = gtk.accelerator_parse('<Alt>H')
        self.agr.connect_group(key, mod, gtk.ACCEL_VISIBLE, self._agr_tray_icon)

    def __init_menu(self):
        self.menu = gtk.Menu()

        dicts = gtk.ImageMenuItem(gtk.STOCK_EDIT, self.agr)
        dicts.set_always_show_image(True)
        dicts.set_label('%s...' % messages[MSGID_DICTIONARIES])
        key, mod = gtk.accelerator_parse('<Control>D')
        dicts.add_accelerator('activate', self.agr,
                               key, mod, gtk.ACCEL_VISIBLE)
        dicts.connect('activate', self._menuitem_edit_dicts_activated)
        self.menu.append(dicts)

        props = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES, self.agr)
        props.set_always_show_image(True)
        props.set_label('%s...' % messages[MSGID_PREFS])
        key, mod = gtk.accelerator_parse('<Control>P')
        props.add_accelerator('activate', self.agr,
                               key, mod, gtk.ACCEL_VISIBLE)
        props.connect('activate', self._menuitem_edit_props_activated)
        self.menu.append(props)

        sep = gtk.SeparatorMenuItem()
        self.menu.append(sep)

        about = gtk.ImageMenuItem(gtk.STOCK_ABOUT, self.agr)
        about.set_always_show_image(True)
        about.set_label(messages[MSGID_ABOUT])
        key, mod = gtk.accelerator_parse('<Control>B')
        about.add_accelerator('activate', self.agr,
                               key, mod, gtk.ACCEL_VISIBLE)
        about.connect('activate', self._menuitem_about_activated)
        self.menu.append(about)

        sep = gtk.SeparatorMenuItem()
        self.menu.append(sep)

        quit = gtk.ImageMenuItem(gtk.STOCK_QUIT, self.agr)
        quit.set_always_show_image(True)
        quit.set_label(messages[MSGID_QUIT])
        key, mod = gtk.accelerator_parse('<Control>Q')
        quit.add_accelerator('activate', self.agr,
                              key, mod, gtk.ACCEL_VISIBLE)
        quit.connect('activate', self._menuitem_quit_activated)
        self.menu.append(quit)
        self.menu.show_all()

    def __init_treeview(self):
        self.liststore = gtk.ListStore(str)
        for word in valcabuary:
            self.liststore.append([word])

        self.treeview.append_column(
                        gtk.TreeViewColumn(
                            None,
                            gtk.CellRendererText(),
                            text=0))

        self.treeview.set_model(self.liststore)
        self.treeview.set_cursor_on_cell(0)
        self.treeview.connect('row-activated',
                                self._treeview_row_activated)

        self.id_cursorchanged = self.treeview.connect(
                                'cursor-changed',
                                self._treeview_cursor_changed)

    def __init_trayicon(self):
        pixbuf = iconmgr.get_pixbuf('ppdict', gtk.ICON_SIZE_SMALL_TOOLBAR)
        self.trayicon = gtk.status_icon_new_from_pixbuf(pixbuf)
        self.trayicon.connect('popup-menu', self._tryicon_menu_popup)
        self.trayicon.connect('activate', self._menuitem_resume_activated)
        self.set_tray(prefs.getbool('tray'))

    def _agr_tray_icon(self, agr, accel, key, mod):
        self.window.emit('delete-event', None)

    def _tryicon_menu_popup(self, widget, button, time, data=None):
        menu = gtk.Menu()

        resume = gtk.ImageMenuItem(gtk.STOCK_REFRESH)
        resume.set_always_show_image(True)
        resume.set_label(messages[MSGID_RESUME])
        resume.connect('activate', self._menuitem_resume_activated)
        menu.append(resume)

        menu.append(gtk.SeparatorMenuItem())

        quit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
        quit.set_always_show_image(True)
        quit.set_label(messages[MSGID_QUIT])
        quit.connect('activate', self._menuitem_quit_activated)
        menu.append(quit)

        menu.show_all()
        menu.popup(None, None, None, button, time, None)

    def _entry_focus_out_event(self, widget, data=None):
        self.hide_tooltip()

    #(double clicked or press enter key) within the word list
    def _treeview_row_activated(self, widget, path, column, data=None):
        model = widget.get_model()
        wordsel = model[path[0]][0]
        self.entry.disconnect(self.id_entrychanged)
        self.entry.set_text(wordsel)
        self.id_entrychanged = self.entry.connect('changed', self._entry_changed)
        self.contentmgr.go_for_details(wordsel)

    def _treeview_cursor_changed(self, widget, data=None):
        sel = self.treeview.get_selection()
        model, iter = sel.get_selected()

        if not iter:
            return

        word = model.get(iter, 0)[0]
        self.entry.disconnect(self.id_entrychanged)
        self.entry.set_text(word)
        self.contentmgr.go_for_brief(word)
        self.id_entrychanged = self.entry.connect('changed', self._entry_changed)

    def _reposition_menu(self, widget, data=None):
        # wmenu refers to the width of the menu;
        # widget.allocation will not work at the first call,
        # so widget.requisition is required
        wmenu = widget.requisition.width

        # coordinates of right-bottom corner of prefs button,
        # the menu should not go across the right of that line.
        (x, y) = self.btnprefs.translate_coordinates(self.window,
                                self.btnprefs.requisition.width,
                                self.btnprefs.requisition.height)

        # self.window.window refers to gtk.gdk.window
        (xwin, ywin) = self.window.window.get_position()

        return (xwin+x-wmenu-2, ywin+y+2, True)

    def _btnprefs_pressed(self, widget, event):
        if event.type == gtk.gdk.BUTTON_PRESS:
            self.menu.popup(None, None, self._reposition_menu,
                            event.button, event.time)
            return True

        return False

    def _btncheck_released(self, widget, data=None):
        word = self.entry.get_text()
        word = word.strip()

        if not word:
            return

        pos = valcabuary.hit(word)
        if pos >= 0:
            return

        corrected = corrector.correct(word)
        if corrected != word:
            self.entry.set_text(corrected)

    def _btnhit_released(self, widget, data=None):
        sel = self.treeview.get_selection()
        model, iter = sel.get_selected()

        if not iter:
            return

        word = model.get(iter, 0)[0]
        self.contentmgr.go_for_details(word)

    def _btnhit_activated(self, widget, data=None):
        self._btnhit_released(widget, data)

    def _tooltip_clicked(self, widget, data):
        self.entry.set_text(data)

    def _entry_changed(self, widget, data=None):
        pos = -1
        word = self.entry.get_text()
        if word:
            pos = valcabuary.hit(word)
            if pos < 0:
                pos = valcabuary.auto_complete(word)

        if pos < 0:
            self.contentmgr.go_for_welcome()
            pos = 0
            self.hide_tooltip()
        else:
            word = valcabuary[pos]
            self.contentmgr.go_for_brief(word)
            self.present_tooltip(word)

        # update treeview if it is visible
        if prefs.getbool('viewvalcabuary'):
            self.treeview.disconnect(self.id_cursorchanged)
            self.treeview.set_cursor_on_cell(pos)
            self.treeview.scroll_to_cell(pos, None, True, 0, 0.0)
            self.id_cursorchanged = self.treeview.connect(
                                'cursor-changed',
                                self._treeview_cursor_changed)

    def _delete_event(self, widget, data=None):
        if prefs.getbool('tray'):
            self.set_tray(True)
            self.window.hide()
            return True

        return False

    def _window_destroy(self, widget, data=None):
        w, h = self.window.get_size()
        prefs.set('savedwidth', w)
        prefs.set('savedheight', h)
        prefs.save()
        gtk.main_quit()

    def _menuitem_resume_activated(self, widget, data=None):
        self.show()

    def _menuitem_quit_activated(self, widget, data=None):
        self.window.emit('destroy')

    def _menuitem_about_activated(self, widget, data=None):
        from aboutdlg import cls_aboutdlg
        dlg = cls_aboutdlg()
        dlg.run()
        dlg.destroy()

    def _menuitem_edit_dicts_activated(self, widget, data=None):
        from dbmgrdlg import cls_dbmgrdlg
        dlg = cls_dbmgrdlg()
        dlg.show()

    def _menuitem_edit_props_activated(self, widget, data=None):
        from prefsdlg import cls_prefsdlg
        dlg = cls_prefsdlg()
        dlg.show()

    def _clipboard_changed(self, clipboard, event):
        text = clipboard.wait_for_text()
        if not text:
            return

        text = text.lower()
        pos = valcabuary.hit(text)
        if pos < 0:
            return

        # retrieve mouse postion
        screen = self.window.get_screen()
        rootwin = screen.get_root_window()
        mousex, mousey, mods = rootwin.get_pointer()

        from floatwin import cls_floatwin
        flwin = cls_floatwin()
        flwin.update(text)

        # present the float window
        flwin.move(mousex, mousey)
        flwin.composited_show()

    def show(self):
        self.window.show_all()
        if not prefs.getbool('viewvalcabuary'):
            self.hide_valcabuary()

    # reposition and show tooltip if it is enabled
    def present_tooltip(self, word):
        if not prefs.getbool('viewtooltip'):
            return

        x, y = self.entry.translate_coordinates(self.window,
                                    0,
                                    self.entry.requisition.height)
        xwin, ywin = self.window.window.get_position()
        self.tooltip.move(xwin + x + 16, ywin + y)
        self.tooltip.set_text(word)
        self.tooltip.show_all()

    def hide_tooltip(self):
        self.tooltip.hide()

    def set_transparency(self, trans):
        if trans > 100 or trans < 0:
            return

        opacity = float(100-trans)/100.0
        self.window.set_opacity(opacity)

    def run(self):
        gtk.main()

    def set_tray(self, enabled):
        self.trayicon.set_visible(enabled)

    def get_root_window(self):
        return self.window

    def show_valcabuary(self):
        self.hpaned.set_position(100)

    def hide_valcabuary(self):
        self.hpaned.set_position(0)

    def start_monitor_clipboard(self):
        clipboard = gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY)
        self.id_monitorcb = clipboard.connect('owner-change', self._clipboard_changed)

    def stop_monitor_clipboard(self):
        self.disconnect(self.id_monitorcb)

    def set_autostart(self, enabled):
        from platform import platform
        platform.set_autostart(enabled)

appmain = cls_appmain()

if __name__ == '__main__':
    appmain.show()
    appmain.run()


from tkinter import Frame, DoubleVar
from tkinter.ttk import Scale

from typing import Dict, Tuple, List

from ui.misc import NoneTypeCheck
from ui.Components import Component

class Range(Component):
    """
    Tkinter range value
    """
    def __init__(self, parent: Frame, style: Dict, geometry: Tuple[int] | List[int]):
        super().__init__(parent, style, geometry)
        self.start = NoneTypeCheck(style.get("start"), 0)
        self.end = NoneTypeCheck(style.get("end"), 10)
        self.defaultValue = NoneTypeCheck(style.get("default"), 1)
        self.value = DoubleVar()
        self.fetchData = self.get
        self.setComponent()

    def setComponent(self):
        self.component = Scale(self.componentFrame,
                               from_=self.start,
                               to=self.end,
                               variable=self.value,
                               value=self.defaultValue)

    def get(self):
        return self.value.get()


"""
1957 - Converter para Hexadecimal
Os dados armazenados no computador estão em binário. Uma forma econômica de ver estes números é usar a base 16
(hexadecimal).
Sua tarefa consiste em escrever um programa que, dado um número natural na base 10, mostre sua representação em
hexadecimal.

Entrada
A entrada é um número inteiro positivo V na base 10 (1 ≤ V ≤ 2 x 10^9).

Saída
A saída é o mesmo número V na base 16 em uma única linha (não esqueça do caractere de fim-de-linha). Use letras
maiúsculas, conforme os exemplos.
"""

V = int(input())

r = V % 16
q = V//16
H = [r]
while q != 0:
    r = q % 16
    q = q // 16
    H.append(r)

for i in range(len(H)-1, -1, -1):
    if H[i] == 10:
        print("A", end="")
    elif H[i] == 11:
        print("B", end="")
    elif H[i] == 12:
        print("C", end="")
    elif H[i] == 13:
        print("D", end="")
    elif H[i] == 14:
        print("E", end="")
    elif H[i] == 15:
        print("F", end="")
    else:
        print("%d" % H[i], end="")

print("")


from __future__ import absolute_import, print_function, unicode_literals

import argparse
import os
import sys
import yaml

# Get relative imports to work when the package is not installed on the PYTHONPATH.
if __name__ == "__main__" and __package__ is None:
    sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# pylint: disable=wrong-import-position
from buildscripts.ciconfig.evergreen import parse_evergreen_file
# pylint: enable=wrong-import-position

# Name of map to search for in the variables map in evergreen.yml
MONGOCRYPTD_VARIANTS = "mongocryptd_variants"
PUSH_TASK_NAME = "push"

def can_validation_be_skipped(evg_config, variant):
    """
    Determine if the given build variant needs to be validated.

    A build variant does not need to be validated if it does not run the 'push' task or
    if it does not exist in the configuration (it is dynamically created).

    :param evg_config: Evergreen configuration.
    :param variant: Build variant to check.
    :return: True if validation can be skipped.
    """
    variant_config = evg_config.get_variant(variant)
    if not variant_config:
        return True

    if PUSH_TASK_NAME not in variant_config.task_names:
        return True

    return False

def read_variable_from_yml(filename, variable_name):
    """
    Read the given variable from the given yaml file.

    :param filename: Yaml file to read from.
    :param variable_name: Variable to read from file.
    :return: Value of variable or None.
    """
    with open(filename, 'r') as fh:
        nodes = yaml.safe_load(fh)

    variables = nodes["variables"]

    for var in variables:
        if variable_name in var:
            return var[variable_name]
    return None

def main():
    # type: () -> None
    """Execute Main Entry point."""

    parser = argparse.ArgumentParser(description='MongoDB CryptD Check Tool.')

    parser.add_argument('file', type=str, help="etc/evergreen.yml file")
    parser.add_argument('--variant', type=str, help="Build variant to check for")

    args = parser.parse_args()

    expected_variants = read_variable_from_yml(args.file, MONGOCRYPTD_VARIANTS)
    if not expected_variants:
        print("ERROR: Could not find node %s in file '%s'" % (MONGOCRYPTD_VARIANTS, args.file),
              file=sys.stderr)
        sys.exit(1)

    evg_config = parse_evergreen_file(args.file)
    if can_validation_be_skipped(evg_config, args.variant):
        print(f"Skipping validation on buildvariant {args.variant}")
        sys.exit(0)

    if args.variant not in expected_variants:
        print("ERROR: Expected to find variant %s in list %s" % (args.variant, expected_variants),
              file=sys.stderr)
        print(
            "ERROR:  Please add the build variant %s to the %s list in '%s'" %
            (args.variant, MONGOCRYPTD_VARIANTS, args.file), file=sys.stderr)
        sys.exit(1)

    sys.exit(0)

if __name__ == '__main__':
    main()


import subprocess
import optparse

def help():
    print("COMMAND                     USE")
    print("help:               Shows a list of options")
    print("list modules (-m)   Shows a list of modules (-m gives more info)")
    print("exit:               Exits the program")
    print("ip:                 Ifconfig simplified")
    print("use module:         Uses a preinstalled module")
    print("clear:              Clears the screen")
    print("run:                Runs the chosen program")

def start():

    shell_input = input("> ")

    if shell_input == "help":
        help()
        start()

    if shell_input == "list modules":
        subprocess.call("ls", shell=True)
        start()

    elif shell_input == "list modules -m":
        subprocess.call("ls -l", shell=True)
        start()

    elif shell_input == "exit":
        subprocess.call("clear", shell=True)
        exit()

    elif shell_input == "ip":
        subprocess.call("ifconfig", shell=True)
        start()

    elif shell_input == "use module":
        subprocess.call("ls", shell=True)
        module = input("what module would you like you use? > ")
        subprocess.call("python3 " + module, shell=True)

    elif shell_input == "clear":
        subprocess.call("clear", shell=True)
        start()

    elif shell_input == "run":
        program = input("What program would you like to run? > ")
        subprocess.call(program, shell=True)

    else:
        print("[!] Incorrect command used!")
        help()
        start()

parser = optparse.OptionParser()

parser.add_option("-q", "--quiet", dest="arg")

(options, arguments) = parser.parse_args()

arg = options.arg

if arg == "true":
    start()
else:
    subprocess.call("toilet -f bigascii12  PY-SHELL", shell=True)
    help()
    start()


import heapq
import sys
input = sys.stdin.readline
# 무한을 의미하는 값으로 10억을 설정
INF = int(1e9)

def dijkstra(start) :
    # 최단 거리 테이블을 모두 무한으로 초기화
    distance = [INF] * (n + 1)
    q = []

    # 시작 노드로 가기 위한 최단 거리는 0으로 설정하여 큐에 삽입
    heapq.heappush(q, (0, start))
    distance[start] = 0

    # 큐가 비어있지 않다면
    while q:
        # 가장 최단 거리가 짧은 노드에 대한 정보를 꺼내기
        dist, now = heapq.heappop(q)

        if distance[now] < dist:
            continue

        # 현재 노드와 연결된 다른 인접한 노드들을 확인
        for i in graph[now]:
            cost = dist + i[1]

            # 현재 노드를 거쳐, 다른 노드로 이동하는 거리가 더 짧은 경우
            if cost < distance[i[0]]:
                distance[i[0]] = cost
                heapq.heappush(q, (cost, i[0]))
    return distance

T = int(input()) # T:테스트케이스
for _ in range(T) :
    n, m, t = map(int, input().split()) # n:교차로개수, m:도로개수, t는 목적지후보개수
    s, g, h = map(int, input().split()) # s:출발지, g와 h 사이에 있는 도로를 지난다

    # 각 노드에 연결되어 있는 노드에 대한 정보를 담는 리스트 만들기
    graph = [[] for _ in range(n + 1)]

    for i in range(m) :
        a, b, d = map(int, input().split())  # a에서 b 양방향 길이 d
        graph[a].append([b,d])
        graph[b].append([a,d])

    t_list = []
    for j in range(t) :
        t_list.append(int(input()))

    start_s = dijkstra(s) #각 start기준에 따른 최단거리
    start_g = dijkstra(g)
    start_h = dijkstra(h)

    answer = []
    for k in t_list :
        if (start_s[g] + start_g[h] +start_h[k] == start_s[k]) or (start_s[h] + start_h[g] + start_g[k] == start_s[k]) :
            answer.append(k)
    answer.sort()
    for h in answer :
        print(h, end=' ')

ALGORITHM/BAEKJOON/SOURCE/10. Dijkstra(다익스트라)/4. 미확인 도착지.py

4. 미확인 도착지.py


#
# @lc app=leetcode id=1200 lang=python3
#
# [1200] Minimum Absolute Difference
#
# https://leetcode.com/problems/minimum-absolute-difference/description/
#
# algorithms
# Easy (67.26%)
# Likes:    896
# Dislikes: 42
# Total Accepted:    82K
# Total Submissions: 120.5K
# Testcase Example:  '[4,2,1,3]'
#
# Given an array of distinct integers arr, find all pairs of elements with the
# minimum absolute difference of any two elements.
#
# Return a list of pairs in ascending order(with respect to pairs), each pair
# [a, b] follows
#
#
# a, b are from arr
# a < b
# b - a equals to the minimum absolute difference of any two elements in
# arr
#
#
#
# Example 1:
#
#
# Input: arr = [4,2,1,3]
# Output: [[1,2],[2,3],[3,4]]
# Explanation: The minimum absolute difference is 1. List all pairs with
# difference equal to 1 in ascending order.
#
# Example 2:
#
#
# Input: arr = [1,3,6,10,15]
# Output: [[1,3]]
#
#
# Example 3:
#
#
# Input: arr = [3,8,-10,23,19,-4,-14,27]
# Output: [[-14,-10],[19,23],[23,27]]
#
#
#
# Constraints:
#
#
# 2 <= arr.length <= 10^5
# -10^6 <= arr[i] <= 10^6
#
#
#

# @lc code=start
class Solution:
    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
        if not arr or len(arr) == 0:
            return arr

        arr.sort()
        diff = defaultdict(list)

        for i in range(1, len(arr)):
            diff[arr[i] - arr[i - 1]].append([arr[i - 1], arr[i]])

        for i in range(1, arr[-1]):
            if i in diff:
                return diff[i]

        return []
# @lc code=end


from pico2d import *

import server

class Background:
    image = None

    def __init__(self, x=0, y=0, code=0):
        if Background.image is None:
            self.image = [load_image('map/mayreel_block_grass_1_400.png'),
                          load_image('map/mayreel_block_grass_3_400.png'),
                          load_image('map/Mayreel_Block_cloud_400.png'),
                          load_image('map/Mayreel_mountain.png')]

        self.x = x  # 블럭 좌표로 나타냄
        self.y = y
        self.code = code
        for i in range(server.map_area_x):
            for j in range(server.map_area_y):
                if server.map_area_size_x * i <= x < server.map_area_size_x * (i + 1):
                    if server.map_area_size_y * j <= y < server.map_area_size_x * (j + 1):
                        self.area_x = i
                        self.area_y = j
        self.state = 'awake'

    def background_update(self):
        if self.state == 'sleep':
            if abs(server.player_area_x - self.area_x) <= 1 and abs(server.player_area_y - self.area_y) <= 1:
                if self in server.block_sleep:
                    self.state = 'awake'
                    server.block.append(self)
                    server.block_sleep.remove(self)

        elif self.state == 'awake':
            if abs(server.player_area_x - self.area_x) > 1 or abs(server.player_area_y - self.area_y) > 1:
                if self in server.block:
                    self.state = 'sleep'
                    server.block_sleep.append(self)
                    server.block.remove(self)

    def update(self):
        pass

    def draw(self):
        if self.state == 'awake':
            self.image[self.code].clip_draw(0, 0, self.image[self.code].w, self.image[self.code].h, self.x + server.cx,
                                            self.y + server.cy + self.image[self.code].h / 2)

    # 저장할 정보를 선택하는 함수
    def __getstate__(self):
        state = {'x': self.x, 'y': self.y, 'code':self.code, 'area_x': self.area_x, 'area_y': self.area_y}
        return state

    # 정보를 복구하는 함수
    def __setstate__(self, state):
        self.__init__()
        self.__dict__.update(state)


from __future__ import absolute_import, unicode_literals, division, print_function
"""Scrape contacts from a Google email account.

Usage:
- First, you need an OAuth2 access token.  Run e.g.
  $ python2 oauth.py --client_id="..." --client_secret="..." --user=jdoe@example.com --generate_oauth2_token
- Open the URL in a web browser, log in as normal, and then copy and paste the code you're given in the script window.
  You will get, among other things, an access token.
- Run e.g.
  $ imap-get-correspondents --username=jdoe@example.com --access-token="..."

Features:
- Caches message information in a local SQLite database so that the program can be restarted if it exits and so that
  subsequent runs of the program do not have to start from the beginning; only new messages will need to be processed.
- Detection of automated messages (by headers).
- Detection of mailing list messages (by headers).
- Heuristic detection of addresses that "look automated" based on the local or domain parts of the email address.
- Heuristic for selecting a name when multiple names have been seen associated with a single email address.

Implementation notes:
- We can't directly connect to Google's IMAP servers with a username and password unless we enable "access from
  less-secure applications".  Instead,  we need to use the "OAuth2 for Installed Applications" flow:
  https://developers.google.com/identity/protocols/OAuth2InstalledApp
"""

import sys
import imaplib
import argparse
import email
import email.header

from .util import build_xoauth2_auth_string, is_automated, merge_addresses
from .models import open_db, populate_message, Message

PY2 = sys.version_info < (3,)
if PY2:
    text_type = unicode
    binary_type = str
else:
    text_type = str
    binary_type = bytes

GMAIL_ALL_MAIL = '"[Gmail]/All Mail"'

if PY2:
    raise Exception('Has not been tested with Python 2!')

class ImapConnection(object):
    def __init__(self, auth_string, debug=False):
        self.auth_string = auth_string
        self.debug = debug
        self.imap_conn = None

    def connect(self):
        imap_conn = imaplib.IMAP4_SSL('imap.gmail.com')
        if self.debug:
            imap_conn.debug = 4
        imap_conn.authenticate('XOAUTH2', lambda x: self.auth_string)
        self.imap_conn = imap_conn

    # Returns strings like
    #   (\All \HasNoChildren) "/" "[Gmail]/All Mail"
    # (We aren't fully parsing the result.)
    def iter_directories(self):
        rt, rval = self.imap_conn.list()
        assert rt == 'OK'  # "response type"; usually "OK" or "NO"
        for s in rval:
            yield s.decode('utf-8')

    # Yields (uid, msg) pairs.
    def iter_messages(self, dir_name=None, limit=None):
        for i, uid in enumerate(self.iter_uids(dir_name=dir_name)):
            if limit is not None and i >= limit:
                break
            yield (uid, self.fetch_message(uid=uid))

    def iter_uids(self, dir_name=None):
        self.imap_conn.select(dir_name)

        rt, rval = self.imap_conn.uid('SEARCH', None, '(ALL)')
        assert rt == 'OK'
        assert len(rval) == 1
        for s in rval[0].decode('utf-8').split():
            yield int(s)

    def fetch_message(self, uid):
        assert isinstance(uid, int)

        rt, rval = self.imap_conn.uid('FETCH', text_type(uid), '(BODY.PEEK[])')
        assert rt == 'OK'

        if rval[0] is None:
            raise KeyError('No message with UID {}'.format(uid))

        return email.message_from_bytes(rval[0][1])  # `policy` arg

def build_parser():
    p = argparse.ArgumentParser()
    p.add_argument('--username', help='e.g. jdoe@gmail.com')
    p.add_argument('--access-token')
    return p

def imap_connect(args):
    c = ImapConnection(
        auth_string=build_xoauth2_auth_string(args.username, args.access_token),
        debug=True,
    )
    c.connect()
    return c

def main_get_correspondents():
    args = build_parser().parse_args()
    c = imap_connect(args)

    db = open_db('./correspondents.db')

    # Pull all of the data that we will need into our local cache.  We deliberately defer filtering or cleaning up data
    # so that we have the original data; that way we can tweak filters/etc. locally.
    #
    # N.B.: This use of UIDs is probably fine for my purposes, but there are a lot of tricky, sharp edges around IMAP
    # UIDs!  Ref.: https://www.limilabs.com/blog/unique-id-in-imap-protocol
    for uid in c.iter_uids(GMAIL_ALL_MAIL):
        try:
            # Have we already inspected this message?
            db_msg = db.query(Message).get(uid)
            if db_msg is None:
                print('Need to fetch message UID=={}'.format(uid))
                imap_msg = c.fetch_message(uid)
                db_msg = populate_message(db, uid, imap_msg)
        except:
            db.rollback()
            raise
        else:
            db.commit()

    # Merge all of the data in the local cache, apply our filters/data-scrubbing/heuristics/etc., and produce our
    # result.
    correspondents = set()
    for db_msg in db.query(Message).all():

        # https://www.iana.org/assignments/auto-submitted-keywords/auto-submitted-keywords.xhtml
        if db_msg.header_auto_submitted is not None and db_msg.header_auto_submitted.lower().startswith('auto-'):
            print('** ignoring message with Auto-Submitted header')
            continue

        # https://www.ietf.org/rfc/rfc2369.txt
        if db_msg.header_list_unsubscribe is not None and db_msg.header_list_unsubscribe != '':
            print('** ignoring message with List-Unsubscribe header')
            continue

        # Discard individual automated senders.
        for msg_addr in db_msg.message_addresses:
            addr = msg_addr.address.as_tuple()
            if is_automated(addr):
                print('** ignoring address that looks automated: {}'.format(addr))
                continue
            correspondents.add(addr)

    # Display our list!
    for name, email_addr in sorted(merge_addresses(correspondents)):
        print((name, email_addr))


"""
This program finds the smallest divisor of a given natural number n greater than 1.
"""

def lowest_divider(num):
    for symbol in range(num + 1, 1, -1):
        if num % symbol == 0:
            divider = symbol
    return divider

number = int(input("Введите число: "))
lcd = lowest_divider(number)
print(f"Наименьший делитель, отличный от единицы: {lcd}")


###read training peptide input
import sys, os, re
from collections import OrderedDict

def readtrainfile(length, trainfile_peptide):
    if os.path.exists(trainfile_peptide) == False:
        print('Error: "' + trainfile_peptide + '" does not exist.')
        sys.exit(1)

    upscaleAA = ['A', 'R', 'N', 'D', 'V', 'Q', 'E', 'G', 'H', 'I', 'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W', 'Y', 'C']
    unusualAA = ['B', 'J', 'O', 'U', 'X', 'Z', 'b', 'j', 'o', 'u', 'x', 'z']
    peptides, labels = [], []

    with open(trainfile_peptide, 'rt') as f:
        content = f.readlines()
        f.seek(0, 0)
        content_txt = f.read()

    ###check unusual amino acid
    for eachAA in unusualAA:
        if eachAA in content_txt:
            print('Peptides contain unnatural amino acid, eg: B, J, O, U, X, Z')
            sys.exit(1)

    rownum, posstart, numpos, negstart = 1, 0, 0, 0
    for eachline in content:
        ###check pos label at first row
        if rownum == 1:
            if '1' in eachline:
                posstart += 1
            else:
                print("Please put the label '1' at the first row.")
                sys.exit(1)
            rownum -= 1
        elif '-1' in eachline:
            negstart += 1
        elif posstart == 1 and negstart == 0:
            peptide = (eachline.split('\n')[0]).upper()
            ###check unrecognised character
            for eachAA in peptide:
                if eachAA not in upscaleAA:
                    print('Unrecognized character exists in your training file')###check characters
                    sys.exit(1)
            ###check peptide length
            if len(peptide) == int(length):
                peptides.append(peptide)
                labels.append('1')
                numpos += 1
            elif len(peptide) == 0:
                continue
            else:
                print('The length of training peptide is not same with the length you choose')
                sys.exit(1)
        else:
            peptide = (eachline.split('\n')[0]).upper()
            ###check unrecognised character
            for eachAA in peptide:
                if eachAA not in upscaleAA:
                    print('Unrecognised character exists in your training file')###check characters
                    sys.exit(1)
            ###check peptide length
            if len(peptide) == int(length):
                peptides.append(peptide)
                labels.append('-1')
            elif len(peptide) == 0:
                continue
            else:
                print('The length of training peptide is not same with the length you choose')
                sys.exit(1)

    ###check number of training data
    if len(set(peptides)) < 5:
        print('The number of peptide in training set should be >= 5 unique instances')
        sys.exit(1)

    dict_labelpep = OrderedDict()
    for items in enumerate(peptides):
        dict_labelpep[items[1]] = labels[items[0]]

    if '-1' not in labels:
        return(dict_labelpep, 'NO')
    else:
        dict_possetneg = OrderedDict()
        dict_possetneg['neg'] = OrderedDict()
        for labelitems in enumerate(labels):
            if labelitems[1] == '1':
                dict_possetneg.setdefault('pos', []).append(peptides[labelitems[0]])
            elif labelitems[1] == '-1':
                dict_possetneg['neg'].setdefault('0', []).append(peptides[labelitems[0]])
        return(dict_possetneg, 'YES')


import ctypes

from libarsdkctrl_bindings import (
    arsdk_ctrl_destroy,
    arsdk_ctrl_new,
    arsdk_ctrl_set_device_cbs,
    struct_arsdk_ctrl,
    struct_arsdk_ctrl_device_cbs,
    struct_pomp_loop,
)

class ArsdkCtrl:
    def __init__(self, loop, device_handler):
        self._loop = ctypes.cast(loop, ctypes.POINTER(struct_pomp_loop))
        self._device_handler = device_handler
        self._arsdk_ctrl = None

        # Create arsdk_ctrl
        self._arsdk_ctrl = ctypes.POINTER(struct_arsdk_ctrl)()
        res = arsdk_ctrl_new(self._loop, ctypes.byref(self._arsdk_ctrl))
        if res < 0:
            raise RuntimeError(f"arsdk_ctrl_new: {res}")

        # Register callbacks
        self._device_cbs = struct_arsdk_ctrl_device_cbs.bind({
            'added': self._device_added_cb,
            'removed': self._device_removed_cb
        })
        res = arsdk_ctrl_set_device_cbs(self._arsdk_ctrl, self._device_cbs)
        if res < 0:
            raise RuntimeError(f"arsdk_ctrl_set_device_cbs: {res}")

    def __del__(self):
        self.cleanup()

    def cleanup(self):
        if self._arsdk_ctrl:
            res = arsdk_ctrl_destroy(self._arsdk_ctrl)
            if res < 0:
                raise RuntimeError(f"arsdk_ctrl_destroy: {res}")
            self._arsdk_ctrl = None

        self._loop = None
        self._device_handler = None

    def _device_added_cb(self, arsdk_device, user_data):
        self._device_handler.add_device(arsdk_device)

    def _device_removed_cb(self, arsdk_device, user_data):
        self._device_handler.remove_device(arsdk_device)

    def get_ptr(self):
        return self._arsdk_ctrl


#!/usr/bin/env python3
"""
 Experiment with different parameters the CNN training and collect accuracy

 Quick and dirty adaptation from train.py.
 Search `TUNING` in this code for hard coded parameters to try.

  [Code left as is, not cleaned, to explicit what I've tried...]
"""

import os
import pickle
import argparse
import datetime
import numpy as np
from tqdm import tqdm

import torch
import torch.nn as nn
import torch.optim as optim

from train import test, train, get_data, hybrid_model

def main():
     # Pre-trained model
    VALID_ARCH_CHOICES = ("vgg16", "vgg13", "densenet121")

    # Parse command line arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("data_dir",
                    help="Directory containing the dataset (default: data)",
                    default="data",
                    nargs="?")
    ap.add_argument("--arch",
                    help="Model architecture from 'torchvision.models' (default: vgg16)",
                    choices=VALID_ARCH_CHOICES, default=VALID_ARCH_CHOICES[0])
#    ap.add_argument("--hidden-units",
#                    help="Number of units the hidden layer should consist of (default: 512)",
#                    default=512,
#                    type=int)
    ap.add_argument("--cpu",
                    help="Use CPU (else GPU) for training (default if not set: GPU)",
                    action="store_true")
    args = ap.parse_args()

    device = "cpu" if args.cpu else "cuda"
    args.device = device
    args.noise = 0.25
    args.clip = 1.0
    args.batch_size = 64
    args.hidden_units = 256
    args.delta = 1e-4

    # Build model: chose loss function, optimizer, processor support
#    # Done later to reset the model
#    model = hybrid_model(arch=args.arch, hidden_units=args.hidden_units)
    criterion = nn.NLLLoss()
    device = "cpu" if args.cpu else "cuda"

    # ===== TUNING ===========================================================
    # Hyperparameters to test
    lr_range = [1e-4]                    #####  <== choice (enumeration)
    batch_size_range = [32, 16, 8, 2] #, 32, 128, 8, 4,  1] #####  <== choice (enumeration)
    epochs = 30                                 #####  <== choice (1 value=max)
    # Number of iteration for each parameter
    iter = 1                                   #####  <== choice (single value)

    # DP or not DP, that is the question
    args.disable_dp = False                     #####  <== choice (boolean)
    # ========================================================================

    # File to export results
    dp_or_not = "noDP_" if args.disable_dp else "DP_"
    file = "experiment_stats/accuracy_data_" + dp_or_not
    file += str(datetime.datetime.today()).replace(' ','_') + ".csv"

    steps = len(lr_range) * len(batch_size_range) * iter
    step = 0

    # Write column titles
    with open(file, 'w') as f:
        f.write('learning_rate, batch_size, n_epochs, accuracy, n_times_for_avg\n')

    # Experiment loops
    for lr in lr_range:
        args.learning_rate = lr

        for bs in batch_size_range:
            args.batch_size = bs
            # Load the dataset into a dataloader  ### default test batch size ###
            trainloader, testloader, mapping = get_data(data_folder=args.data_dir,
                                                        batch_size=bs)
            args.sample_size = len(trainloader.dataset)

            #for epochs in epochs_range:
            accuracy_sum = []

            for _ in range(iter):
                    # Reset the model
                    model, optimizer = hybrid_model(arch=args.arch,
                                                    hidden_units=args.hidden_units,
                                                    args=args)
                    step += 1
                    _, acc = train(model=model,
                                   trainloader=trainloader,
                                   testloader=testloader,
                                   epochs=epochs,
                                   print_every=None,
                                   criterion=criterion,
                                   optimizer=optimizer,
                                   device=device,
                                   arch=args.arch,
                                   model_dir='',
                                   serialize=False,
                                   detail=False,
                                   args=args,
                                  )
                    acc = np.multiply(acc, 100)
                    accuracy_sum.append(acc)
                    print(f' {step}/{steps}\tlr={lr}, bs={bs},')
                    for  n_epoch, accur in enumerate(acc, start=1):
                        line = f'{lr}, {bs}, {n_epoch}, {accur:.2f}, 1\n'
                        with open(file, 'a') as f:
                            f.write(line)
                        print(f'\t. ×{n_epoch} epoch{"s" if n_epoch > 1 else " "}'
                              f' => accuracy = {accur:.2f}%')

            # Sum up for identical settings, repeted `iter` times
            if iter > 1:
                acc_avg = np.average(accuracy_sum, axis=0)
                for n_epoch, accur in enumerate(acc_avg, start=1):
                        line = f'{lr}, {bs}, {n_epoch}, {accur:.2f}, {iter}\n'
                        with open(file, 'a') as f:
                            f.write(line)
                        print(f'\t\t>>> Average on {iter} iterations >>>\tlr={lr}, bs={bs},'
                              f' ×{n_epoch} epoch{"s" if n_epoch > 1 else " "}'
                              f' => accuracy = {accur:.2f}%')

if __name__ == "__main__":
    main()