"""
读取csv文件中的数据
"""
import csv

def read_coeff(coeff_file):
    """读取线性回归参数, 返回以图片序号为键值的字典"""
    coeff_dict = {}
    with open('data/coeff/coeff-'+coeff_file+'.csv', 'r', newline='') as csvfile:
        reader = csv.reader(csvfile)
        for line in reader:
            coeff_dict[int(line[0])] = [float(x) for x in line[1:]]
    return coeff_dict

def read_param(param_file):
    """读取按线性回归调整过的标定参数, 返回焦距与主点坐标"""
    param_dict = {}
    with open('data/param/param-'+param_file+'.csv', 'r', newline='') as csvfile:
        reader = csv.reader(csvfile)
        for line in reader:
            param_dict[int(line[0])] = [float(line[2]), float(line[10]), float(line[11])]
    return param_dict

def read_tem(tem_file):
    """读取温度变化"""
    tem_dict = {}
    with open('data/tem/tem-'+tem_file+'.csv', 'r', newline='') as csvfile:
        reader = csv.reader(csvfile)
        for line in reader:
            tem_dict[int(line[0])] = float(line[1])
    return tem_dict

def read_coor(coor_file):
    """读取标志点平均位置与参考位置的差值"""
    coor_dict = {}
    with open('data/coor/coor-'+coor_file+'.csv', 'r', newline='') as csvfile:
        reader = csv.reader(csvfile)
        for line in reader:
            coor_dict[int(line[0])] = [float(x) for x in line[1:]]
    return coor_dict

def read_multi(multi_file, tem=False):
    """读取所有标志点位置与参考位置的差值, 前三行为标志点与中心点的直线距离、行列坐标距离, tem为True时读取温度修正后的数据"""
    Row_dict = {}
    with open(('data/multi_tem/Row-' if tem else 'data/multi/Row-')+multi_file+'.csv', 'r', newline='') as csvfile:
        reader = csv.reader(csvfile)
        for line in reader:
            Row_dict[int(line[0])] = [float(x) for x in line[1:]]
    Column_dict = {}
    with open(('data/multi_tem/Column-' if tem else 'data/multi/Column-')+multi_file+'.csv', 'r', newline='') as csvfile:
        reader = csv.reader(csvfile)
        for line in reader:
            Column_dict[int(line[0])] = [float(x) for x in line[1:]]
    return Row_dict, Column_dict

def read_focus(focus_file):
    """读取焦距变化"""
    focus_dict = {}
    with open('data/focus/focus-'+focus_file+'.csv', 'r', newline='') as csvfile:
        reader = csv.reader(csvfile)
        for line in reader:
            focus_dict[int(line[0])] = [float(x) for x in line[1:]]
    return focus_dict

if __name__ == '__main__':
    a = read_tem('N3-6')


from typing import Any
from django.db import models
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect, Http404, JsonResponse, HttpResponse
from django.urls import reverse
from django.views import generic
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import JSONParser
from .models import Question, Choice, Vote, Category
from .serializers import CategorySerializer
from .forms import CreateQuestionForm, CreateChoiceForm
# Create your views here.

class IndexView(generic.ListView):
    template_name = "polls/index.html"
    context_object_name = "latest_question_list"

    def get_queryset(self):
        """Return last five published questions"""
        return Question.objects.filter(pub_date__lte=timezone.now()).order_by("-pub_date")

def detail_view(request, pk):
    question = get_object_or_404(Question, pk=pk)
    if question.pub_date > timezone.now():
        raise Http404()
    if request.user.is_authenticated:
        if question.created_by == request.user:
            return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
        try:
            Vote.objects.get(voted_by=request.user, question=question)
            return render(request, "polls/results.html", {"question": question})
        except Vote.DoesNotExist:
            pass
    return render(request, "polls/details.html", {"question": question})

class Result(generic.DetailView):
    template_name = "polls/results.html"
    context_object_name = "question"

    def get_queryset(self):
        return Question.objects.filter(pub_date__lte=timezone.now())

def vote(request, question_id):
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse("login:index"))

    question = get_object_or_404(Question, pk=question_id)

    try:
        selected_choice = question.choice_set.get(pk=request.POST["choice"])
    except (KeyError, Choice.DoesNotExist):
        return render(request, "polls/details.html",
                      {
                            "question": question,
                            "error_message": "You didn't select a choice"
                       })
    else:
        Vote.objects.create(question=question, voted_on=selected_choice, voted_by=request.user)
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse("polls:results", args=(question.id, )))

def new_poll(request):
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse("login:index"))

    if request.method == "POST":
        created_by = request.user
        question_text = request.POST["question_text"]
        pub_date = timezone.now()
        new_question = Question(question_text=question_text, created_by=created_by, pub_date=pub_date, category_id=6)
        new_question.save()

        index = 1
        while True:
            value = request.POST.get(f"choice_text_{index}", None)
            print(value)
            if value:
                new_question.choice_set.create(choice_text=value)
            else:
                break
            index += 1

        return HttpResponseRedirect(reverse("polls:index"))

    elif request.method == "GET":
        q_form = CreateQuestionForm()

        return render(request, "polls/new_poll.html",
                      {
                        "question_form": q_form,
                      })

def delete_poll(request, pk):
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse("login:index"))

    poll = get_object_or_404(Question, pk=pk)
    if request.method == "GET":
        return render(request, "polls/delete_poll.html", {"question": poll})

    elif request.method == "POST":
        if poll.created_by != request.user:
            return render(request, "polls/delete_poll.html", {
                "error_messages": ["Permission denied"],
                "question": poll
            })
        poll.delete()
    return HttpResponseRedirect(reverse("polls:index"))

def edit_poll(request, pk):
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse("login:index"))

    poll = get_object_or_404(Question, pk=pk)

    if poll.created_by != request.user:
        return render(request, "polls/results.html", {"error_messages": ["You can edit this poll"], "question": poll})

    if request.method == "GET":
        q_form = CreateQuestionForm()
        q_form.data["question_text"] = poll.question_text
        return render(request, "polls/edit_poll.html", {
            "question_form": q_form,
            "question": poll
        })

    elif request.method == "POST":
        index = 1
        while True:
            value = request.POST.get(f"choice_text_{index}", None)
            if value:
                pass
            else:
                break

        poll.question_text = request.POST["question_text"]
        poll.save()
        return HttpResponseRedirect(reverse("polls:results", args=(poll.id,)))

class Categories(generic.ListView):
    template_name = "polls/all_categories.html"
    context_object_name = "categories"

    def get_queryset(self):
        return Category.objects.all().order_by("-pub_date")

def category(request, cat):
    polls = Question.objects.filter(category__category_name=cat, pub_date__lte=timezone.now()).order_by("-pub_date")
    return render(request, "polls/category.html", {"polls": polls, "category": cat})

def about(request):
    return render(request, 'polls/about.html', {})

@csrf_exempt
def category_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        categories = Category.objects.all()
        serializer = CategorySerializer(categories, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = CategorySerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)


"""
ITF: Python code to take images from source/Dataset and use them to train an image recognizer
that can later be used to recognize faces of users and mark their attendance

Using:
OpenCV2 for creating and training recognizer over dataset
Python Image Library (PIL) for converting images to openCV readable format
Numpy to create and manage image arrays

Training data is stored as TrainingData.yml file in source folder to be used later
"""

from PIL import Image
import numpy as np
import cv2
import os

recognizer = cv2.face.LBPHFaceRecognizer_create()	#create recognizer as LBPHFaceRecognizer object from openCV library

def getImagesWithID(path):
	imagePaths = [os.path.join(path, f) for f in os.listdir(path)]	#store path of every image in dataset in imagePaths list
	faces = []	#create faces list
	ids = []	#create ids list

	for imagePath in imagePaths:	#loop thorough every image in Dataset
		facePil = Image.open(imagePath).convert('L')	#convert image to Python Image Library
		faceNp = np.array(facePil, 'uint8')		#convert PIL image to NumPy array using uint8 decoding

		currentID = int(os.path.split(imagePath)[-1].split(".")[1])	#get current id of user from file name
		print(currentID)
		faces.append(faceNp)	#append numpy array to faces list
		ids.append(currentID)	#append relative id to id list

		# cv2.imshow("train", faceNp)
		# cv2.waitKey(0)

	return np.array(ids), faces	#function returns faces and id list, id list is also converted to numpy array

dataPath = "Dataset"
ids, faces = getImagesWithID(dataPath)
print(faces, "\n", ids)
recognizer.train(faces, ids)	#recognizer is trained with faces and id list from dataset
recognizer.save("trainingData.yml")	#save recognizer configuration as yml file
cv2.destroyAllWindows()


import networkx as nx
import matplotlib.pyplot as plt
import json
import pandas as pd

filenames = ["xqf131", "xqg237", "pma343", "pka379", "pbn423", "pbm436", "xql662"] #["bcl380", "pbk411", "pbl395"]

for name in filenames:
    with open(f"../output/graph-{name}.json", 'r') as file:
        data = json.load(file)

    coords = {}
    G = nx.Graph()

    for item in data:
        coords[item["id"]] = (item["x"], item["y"])

    mst = pd.read_csv(f"../output/mst-{name}.txt", sep=";")
    id1 = mst.id1
    id2 = mst.id2

    for i in range (0, len(id1)):
        G.add_edge(id1[i], id2[i])

    nx.draw(G, coords, with_labels=False, node_size=15, node_color='blue')
    plt.savefig(f"mst-{name}.png")
    plt.clf()

    G2 = nx.Graph()
    dfs = pd.read_csv(f"../output/dfs-{name}.txt", sep=";")
    v = dfs.id

    for i in range (1, len(v)):
        G2.add_edge(v[i-1], v[i])
    G2.add_edge(v[len(v)-1], 1)

    nx.draw(G2, coords, with_labels=False, node_size=15, node_color='red')
    plt.savefig(f"dfs-{name}.png")
    plt.clf()


# -*- coding: utf-8 -*-

from odoo import models, fields, api
from datetime import datetime, date, timedelta
import calendar
import time

class AttendanceSheets(models.Model):
    _name = 'berdikari.attendance.sheets'
    _description = 'Berdikari Attendance Sheets'

    name = fields.Many2one('hr.employee', string='Employee')
    period_id = fields.Many2one('berdikari.hr.attendance.period', string='Period Name')

    @api.depends('period_id')
    @api.onchange('period_id')
    def onchange_domain_seq(self):
        domain = {}
        period_id = self.period_id.id
        if period_id:
            domain_seq = [('attendance_period_id', '=', period_id)]
            domain['period_seq_id'] = domain_seq
        hasil = {'domain': domain}
        return hasil

    period_seq_id = fields.Many2one('berdikari.hr.attendance.period.line', string='Period Sequence')

    @api.depends('period_seq_id')
    @api.onchange('period_seq_id')
    def onchange_period_seq(self):
        for rec in self:
            period_seq_id = rec.period_seq_id.id
            model_period_line = self.env['berdikari.hr.attendance.period.line']
            if period_seq_id:
                period_line = model_period_line.search([('id', '=', period_seq_id)], limit=1)
                rec.period_start = period_line.date_from
                rec.period_end = period_line.date_to

    period_start = fields.Date()
    period_end = fields.Date()
    remarks = fields.Char()
    attendance_policy = fields.Char()
    no_of_overtimes = fields.Integer()
    total_overtime = fields.Float()
    ot_meal_provided = fields.Integer()
    no_of_absence_days = fields.Integer()
    total_absence_hour = fields.Float()
    no_of_lates = fields.Integer()
    total_late_in = fields.Float()
    no_of_diff_times = fields.Integer()
    total_diff_time_hours = fields.Float()
    no_of_work_days = fields.Integer()
    total_work_hours = fields.Float()
    no_of_leave_days = fields.Integer()
    calendar_id = fields.Many2one('resource.calendar')
    attendance_line_ids = fields.One2many('berdikari.attendance.sheets.line', 'attendance_sheet_id')

    @api.multi
    def action_get_attendance(self):
        for rec in self:
            model_attendance = self.env['hr.attendance']
            model_contract = self.env['hr.contract']
            model_calendar = self.env['resource.calendar']
            model_calendar_detail = self.env['resource.calendar.attendance']
            model_overtime = self.env['berdikari.overtime.request']
            model_leave = self.env['hr.leave']
            domain = []
            if rec.name:
                domain.append(('employee_id', '=', rec.name.id))
            else:
                domain = domain
            if rec.period_start and rec.period_end:
                domain.append(('check_in', '>=', rec.period_start))
                domain.append(('check_in', '<=', rec.period_end))
                domain.append(('check_out', '>=', rec.period_start))
                domain.append(('check_out', '<=', rec.period_end))
            else:
                domain = domain
            search_attendance_ids = model_attendance.search(domain, order="id asc")
            domain_name = []
            if rec.name:
                domain_name.append(('employee_id', '=', rec.name.id))
            else:
                domain_name = domain_name
            domain_name.append(('active', '=', True))
            contract_id = model_contract.search(domain_name, limit=1)
            calendar_id = model_calendar.search([('id', '=', contract_id.resource_calendar_id.id)], limit=1)
            rec.calendar_id = calendar_id
            # calendar_detail_ids = model_calendar_detail.search([('calendar_id', '=', calendar_id.id)])
            domain_overtime = []
            if rec.name:
                domain_overtime.append(('employee_id', '=', rec.name.id))
            else:
                domain_overtime = domain_overtime
            if rec.period_start and rec.period_end:
                domain_overtime.append(('start_date', '>=', rec.period_start))
                domain_overtime.append(('start_date', '<=', rec.period_end))
                domain_overtime.append(('state', '=', 'done'))
            else:
                domain_overtime = domain_overtime
            overtime_ids = model_overtime.search(domain_overtime)
            # cek leave
            domain_leave = []
            if rec.name:
                domain_leave.append(('employee_id', '=', rec.name.id))
            else:
                domain_leave = domain_leave
            if rec.period_start and rec.period_end:
                domain_leave.append(('date_from', '>=', rec.period_start))
                domain_leave.append(('date_from', '<=', rec.period_end))
                domain_leave.append(('date_to', '>=', rec.period_start))
                domain_leave.append(('date_to', '<=', rec.period_end))
            else:
                domain_leave = domain_leave
            domain_leave.append(('state', '=', 'validate'))
            leave_ids = model_leave.search(domain_leave)
            # hitung berapa banyak overtime
            no_of_overtimes = len(overtime_ids)
            total_overtime = 0
            ot_meal_provided = 0
            for one in overtime_ids:
                total_overtime = total_overtime + one.numbers_of_hour
                if one.meal_provided:
                    ot_meal_provided = ot_meal_provided + 1
            rec.no_of_overtimes = no_of_overtimes
            rec.total_overtime = total_overtime
            rec.ot_meal_provided = ot_meal_provided
            hasil = []
            no_of_absence_days = 0
            total_absence_hour = 0
            no_of_lates = 0
            total_late_in = 0
            no_of_diff_times = 0
            total_diff_time_hours = 0
            no_of_work_days = 0
            total_work_hours = 0
            no_of_leave_days = 0
            numbers_of_day = ((rec.period_end - rec.period_start).days) + 1
            for a in list(range(numbers_of_day)):
                if a == 0:
                    new_date = rec.period_start.day
                else:
                    new_date = (rec.period_start.day) + a
                schedule_date = date(rec.period_start.year, rec.period_start.month, new_date)
                dayNumber = calendar.weekday(rec.period_start.year, rec.period_start.month, new_date)
                day = calendar.day_name[dayNumber]
                day_to_sign_in = day + ' Morning'
                day_to_sign_out = day + ' Evening'
                sign_in = model_calendar_detail.search([('calendar_id', '=', calendar_id.id), ('name', '=', day_to_sign_in)])
                sign_in_time = sign_in.hour_from
                sign_out = model_calendar_detail.search([('calendar_id', '=', calendar_id.id), ('name', '=', day_to_sign_out)])
                sign_out_time = sign_out.hour_to
                data = False
                actual_sign_in_time = 0
                actual_sign_out_time = 0
                actual_sign_in = 0
                actual_sign_out = 0
                total_working_hour = 0
                nett_working_hour = 0
                overtime_hour = 0
                attendance_code = False
                diff_time = 0
                transport_allowance = 0
                meal_allowance = 0
                ot_meal_provided = False

                t1 = 0
                t2 = 0
                if search_attendance_ids:
                    for data in search_attendance_ids:
                        sign_in_date = date(data.check_in.year, data.check_in.month, data.check_in.day)
                        if sign_in_date == schedule_date:
                            # actual time to show
                            t1 = t1 + data.check_in.hour + 7
                            actual_sign_in_time = data.check_in.strftime("%M")
                            actual_sign_in_time = str(t1) + '.' + actual_sign_in_time
                            t2 = t2 + data.check_out.hour + 7
                            actual_sign_out_time = data.check_out.strftime("%M")
                            actual_sign_out_time = str(t2) + '.' + actual_sign_out_time
                            # actual time to calculate
                            actual_sign_in = data.check_in.hour + (data.check_in.minute / 60) + (
                                        data.check_in.second / 3600) + 7
                            actual_sign_out = data.check_out.hour + (data.check_out.minute / 60) + (
                                        data.check_out.second / 3600) + 7
                            total_working_hour = actual_sign_out - actual_sign_in
                            if day == 'Friday':
                                nett_working_hour = total_working_hour - 1.5
                            else:
                                nett_working_hour = total_working_hour - 1
                            diff_time = (sign_out_time - sign_in_time) - total_working_hour
                            attendance_code_id = self.env['berdikari.hr.attendance.code'].search(
                                [('name', '=', 'work')], limit=1)
                            attendance_code = attendance_code_id.code
                            overtime_hour = 0

                            # hitung berapa banyak late
                            if (actual_sign_in - sign_in_time) > 0:
                                no_of_lates = no_of_lates + 1
                                total_late_in = total_late_in + (actual_sign_in - sign_in_time)
                                rec.no_of_lates = no_of_lates
                                rec.total_late_in = total_late_in

                            # hitung berapa banyak diff
                            setup = self.env['jekdoo.setup'].get_setup()
                            min_diff = setup.diff_limit
                            if diff_time > 0 and diff_time >= min_diff:
                                no_of_diff_times = no_of_diff_times + 1
                                total_diff_time_hours = total_diff_time_hours + diff_time
                                rec.no_of_diff_times = no_of_diff_times
                                rec.total_diff_time_hours = total_diff_time_hours

                            # hitung meal dan transport bila employee check in
                            if (actual_sign_in <= sign_in_time) and nett_working_hour > 5:
                                transport_allowance = transport_allowance + 1
                                meal_allowance = meal_allowance + 1

                            #hitung working days and working hour
                            no_of_work_days = no_of_work_days + 1
                            total_work_hours = total_work_hours + nett_working_hour
                            rec.no_of_work_days = no_of_work_days
                            rec.total_work_hours = total_work_hours

                            if overtime_ids:
                                for overtime in overtime_ids:
                                    overtime_date = date(overtime.start_date.year, overtime.start_date.month,
                                                         overtime.start_date.day)
                                    if overtime_date == sign_in_date:
                                        overtime_hour = overtime.numbers_of_hour
                                        ot_meal_provided = overtime.meal_provided
                                        break
                            break
                        else:
                            if overtime_ids:
                                for overtime in overtime_ids:
                                    overtime_date = date(overtime.start_date.year, overtime.start_date.month,
                                                         overtime.start_date.day)
                                    if overtime_date == schedule_date:
                                        overtime_hour = overtime.numbers_of_hour
                                        ot_meal_provided = overtime.meal_provided
                                        break
                # hitung leave
                if leave_ids:
                    for leave in leave_ids:
                        leave_date_from = date(leave.date_from.year, leave.date_from.month, leave.date_from.day)
                        leave_date_to = date(leave.date_to.year, leave.date_to.month, leave.date_to.day)
                        if leave_date_from == schedule_date or leave_date_to == schedule_date:
                            holiday_status_id = leave.holiday_status_id
                            attendance_code_id = holiday_status_id.attendance_code_id
                            attendance_code = attendance_code_id.code
                            no_of_leave_days = no_of_leave_days + 1
                            rec.no_of_leave_days = no_of_leave_days
                            break
                # hitung berapa banyak absen
                if dayNumber in [0, 1, 2, 3, 4]:
                    if t1 <= 0 or t2 <=0:
                        if attendance_code == False:
                            attendance_code_id = self.env['berdikari.hr.attendance.code'].search([('name', '=', 'absence')],limit=1)
                            attendance_code = attendance_code_id.code
                            if dayNumber == 4:
                                total_absence_hour = total_absence_hour + (sign_out_time - sign_in_time - 1.5)
                            else:
                                total_absence_hour = total_absence_hour + (sign_out_time - sign_in_time - 1)
                            no_of_absence_days = no_of_absence_days + 1
                            rec.no_of_absence_days = no_of_absence_days
                            rec.total_absence_hour = total_absence_hour

                baru = [0, 0, {
                    'attendance_sheet_id': data.id if data else False,
                    'date': schedule_date,
                    'day': day,
                    'planned_sign_in': sign_in_time,
                    'planned_sign_out': sign_out_time,
                    'actual_sign_in_time': actual_sign_in_time,
                    'actual_sign_out_time': actual_sign_out_time,
                    'actual_sign_in': actual_sign_in,
                    'actual_sign_out': actual_sign_out,
                    'late_in': (actual_sign_in - sign_in_time) if (actual_sign_in - sign_in_time) > 0 else 0,
                    'total_working_hour': total_working_hour,
                    'nett_working_hour': nett_working_hour,
                    'overtime': overtime_hour,
                    'ot_meal_provided': ot_meal_provided,
                    'diff_time': diff_time,
                    'transport_allowance': transport_allowance,
                    'meal_allowance': meal_allowance,
                    'attendance_code': attendance_code,
                    'note': '',
                }]
                hasil.append(baru)
            if hasil:
                for line in rec.attendance_line_ids:
                    line.unlink()
                rec.attendance_line_ids = hasil

class AttendanceSheetsLine(models.Model):
    _name = 'berdikari.attendance.sheets.line'
    _description = 'Berdikari Attendance Sheets Line'

    attendance_sheet_id = fields.Many2one('berdikari.attendance.sheets')
    date = fields.Date()
    day = fields.Char()
    planned_sign_in = fields.Float()
    planned_sign_out = fields.Float()
    actual_sign_in_time = fields.Char()
    actual_sign_out_time = fields.Char()
    actual_sign_in = fields.Float()
    actual_sign_out = fields.Float()
    late_in = fields.Float()
    overtime = fields.Float()
    ot_meal_provided = fields.Boolean()
    total_working_hour = fields.Float()
    nett_working_hour = fields.Float()
    diff_time = fields.Float()
    transport_allowance = fields.Integer()
    meal_allowance = fields.Integer()
    # attendance_code = fields.Many2one('berdikari.hr.attendance.code')
    attendance_code = fields.Char()
    note = fields.Char()


# 示例输入：
# segments = [[u'我', 'v'], [u'想听', 'v'],[u'周', 'v'], [u'杰伦', 'v'], [u'的', 'v'],
#                 [u'青花', 'v'], [u'瓷', 'v'], [u'好吗', 'v']]
# slots = [(u'singer','周杰伦',3,6),(u'song','青花瓷',7,10)]
# 示例输出：
# O O B-singer I-singer O B-song I-song O

def func(segments, slots):
    res = []
    pre = 0
    count = 0
    for i in range(len(segments)):
        segments[i].append(count)
        count += len(segments[i][0])
    i, j = 0, 0
    while j < len(slots):
        begin = True
        while i < len(segments) and segments[i][2] < slots[j][3]:
            if segments[i][2] < slots[j][2]:
                res += ["O"]
            else:
                if begin:
                    res += ["B-" + slots[j][0]]
                    begin = False
                else:
                    res += ["I-" + slots[j][0]]
            i += 1
        j += 1
    print(res)

func([['我', 'v'], ['想听', 'v'], ['周', 'v'], ['杰伦', 'v'], ['的', 'v'],
      ['青花', 'v'], ['瓷', 'v'], ['好吗', 'v']],
     [('singer', '周杰伦', 3, 6), ('song', '青花瓷', 7, 10)])


import boto3
import json

# Set up the S3 client
s3 = boto3.client('s3')

# Load the existing JSON file from S3
bucket_name = 'jsonofthattree'
json_key = 'data.json'
response = s3.get_object(Bucket=bucket_name, Key=json_key)
json_data = response['Body'].read().decode('utf-8')
existing_data = json.loads(json_data)

# Update the JSON data
new_data = {'name': 'John', 'age': 30, 'city': 'New York'}
existing_data.update(new_data)

# Save the updated JSON data to S3
updated_json_data = json.dumps(existing_data)
s3.put_object(Bucket=bucket_name, Key=json_key, Body=updated_json_data)


# WAP to Find Greatest of 3 in a number

greatestOf3 = lambda x, y, z : x if x > y and x > z else y if y > z and y > x else z

if __name__ == '__main__':
    x = int(input("Enter num1 "))
    y = int(input("Enter num2 "))
    z = int(input("Enter num3 "))
    print(f"Greatest of {x}, {y}, {z} is {greatestOf3(x, y, z)}")

# Output
# Enter num1 1
# Enter num2 12
# Enter num3 10
# Greatest of 1, 12, 10 is 12


print('='*8,'Extraindo Dados de Uma Lista','='*8)
l = []
while True:
    l.append(int(input('Digite um valor: ')))
    r = ' '
    while r not in 'SN':
        r = str(input('Quer continuar a digitar valores? [S/N] ')).strip().upper()[0]
    if r in 'N':
        break
print(f'Sua lista ficou: {l}')
print(f'Você digitou {len(l)} números.')
if 5 in l:
    print(f'O valor 5 se encontra na lista!')
else:
    print('O valor 5 não se encontra na lista.')
l.sort(reverse = True)
print(f'Sua lista em ordem descrescente fica: {l}')


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('Finance', '0004_remove_paymentorder_payable'),
    ]

    operations = [
        migrations.AlterField(
            model_name='paymentorder',
            name='shipment_order',
            field=models.ForeignKey(verbose_name='订单', on_delete=models.SET('订单被删除'), to='ShipmentOrder.ShipmentOrder'),
        ),
    ]


import argparse
import torch.nn.functional as F
from torch.utils.data import DataLoader
import torch.nn as nn
from Models.dataloader.samplers import CategoriesSampler
from Models.utils import *
from Models.dataloader.data_utils import *
from Models.models.Network import DeepEMD
from torch.utils.tensorboard import SummaryWriter
import tqdm
import time
from datetime import datetime
import matplotlib.pyplot as plt

PRETRAIN_DIR='pretrained_model/'

DATA_DIR='/home/Improved_FSIC_Method/datasets'

parser = argparse.ArgumentParser()
#about dataset and training
parser.add_argument('-dataset', type=str, default='miniimagenet', choices=['miniimagenet','tieredimagenet'])
parser.add_argument('-data_dir', type=str, default=DATA_DIR,help='dir of datasets')

#about training
parser.add_argument('-batchsize', type=int, default=512,help='batch size of generate box')
parser.add_argument('-bs', type=int, default=1,help='batch size of tasks')
parser.add_argument('-max_epoch', type=int, default=30)
parser.add_argument('-lr', type=float, default=0.0005)
parser.add_argument('-temperature', type=float, default=12.5)
parser.add_argument('-step_size', type=int, default=10)
parser.add_argument('-gamma', type=float, default=0.5)
parser.add_argument('-val_frequency',type=int,default=50)
parser.add_argument('-random_val_task', type=int, default=1, help='random samples tasks for validation at each epoch')
#about task
parser.add_argument('-way', type=int, default=5)
parser.add_argument('-shot', type=int, default=1)
parser.add_argument('-query', type=int, default=1,help='number of query image per class')
parser.add_argument('-val_episode', type=int, default=2000, help='number of validation episode')
parser.add_argument('-test_episode', type=int, default=5000, help='number of testing episodes after training')

# about model
parser.add_argument('-pretrain_dir', type=str, default=PRETRAIN_DIR)
parser.add_argument('-metric', type=str, default='ADM', choices=['Cosine', 'L2', 'Dot', 'ADM'])
parser.add_argument('-norm', type=str, default='center', choices=['center'], help='feature normalization')
parser.add_argument('-deepemd', type=str, default='ic', choices=['fcn', 'ic'])
parser.add_argument('-num_patch',type=int,default=9)

# emd_cc setting
parser.add_argument('-outer_num', type=int, default=2)
parser.add_argument('-all_in_num', type=int, default=1)
parser.add_argument('-alpha', type=float, default=0.4)

# ADM setting
parser.add_argument('-cosine_rate', type=float, default=0.8)

# SFC
parser.add_argument('-sfc_lr', type=float, default=0.1, help='learning rate of SFC')
parser.add_argument('-sfc_wd', type=float, default=0, help='weight decay for SFC weight')
parser.add_argument('-sfc_update_step', type=float, default=100, help='number of updating step of SFC')
parser.add_argument('-sfc_bs', type=int, default=4, help='batch size for finetune sfc')

# OTHERS
parser.add_argument('-gpu', default='0')
parser.add_argument('-extra_dir', type=str,default=None,help='extra information that is added to checkpoint dir, e.g. hyperparameters')
parser.add_argument('-seed', type=int, default=1)
parser.add_argument('-train_time', default=datetime.now().strftime("%Y-%m-%d-%H-%M-%S"), type=str, metavar='PATH', help='path to cache (default: none)')

args = parser.parse_args()
pprint(vars(args))

set_seed(args.seed)
num_gpu = set_gpu(args)
Dataset=set_up_datasets(args)

# model
args.pretrain_dir = osp.join(args.pretrain_dir, args.dataset, 'resnet12/max_acc.pth')
print(args.pretrain_dir)
model = DeepEMD(args)
model = load_model(model, args.pretrain_dir)
model = nn.DataParallel(model, list(range(num_gpu)))
model = model.cuda()
model.eval()

args.save_path = '%s/%s/%dquery-%dshot-%dway/'%(args.dataset,args.deepemd,args.query,args.shot,args.way)

args.save_path = osp.join('checkpoint', args.save_path, args.train_time)

ensure_path(args.save_path)

############################################
# generate box
############################################

# Dataset prepare
trainset = Dataset('train', args)
valset = Dataset('val', args)

# Upadate box of dataset
def update_box(cc_loader, model, t=0.5):
    # print('==> Start updating boxes...')
    model.eval()
    boxes = []
    for _, batch in enumerate(cc_loader):
        data, _ = [_.cuda() for _ in batch]
        with torch.no_grad():
            feat_map = model(data)  # (N, C, H, W)
        N, Cf, Hf, Wf = feat_map.shape
        eval_train_map = feat_map.sum(1).view(N, -1)  # (N, Hf*Wf)
        eval_train_map = eval_train_map - eval_train_map.min(1, keepdim=True)[0]
        eval_train_map = eval_train_map / eval_train_map.max(1, keepdim=True)[0]
        eval_train_map = eval_train_map.view(N, 1, Hf, Wf)
        eval_train_map = F.interpolate(eval_train_map, size=data.shape[-2:], mode='bilinear')  # (N, 1, Hi, Wi)

        Hi, Wi = data.shape[-2:]

        for hmap in eval_train_map:
            hmap = hmap.squeeze(0)  # (Hi, Wi)

            h_filter = (hmap.max(1)[0] > t).int()
            w_filter = (hmap.max(0)[0] > t).int()

            h_min, h_max = torch.nonzero(h_filter).view(-1)[[0, -1]] / Hi  # [h_min, h_max]; 0 <= h <= 1
            w_min, w_max = torch.nonzero(w_filter).view(-1)[[0, -1]] / Wi  # [w_min, w_max]; 0 <= w <= 1
            boxes.append(torch.tensor([h_min, w_min, h_max, w_max]))

    all_boxes = torch.stack(boxes, dim=0).cuda()  # (num_iters, 4)
    return all_boxes

# Box generate
train_cc_loader = DataLoader(dataset=trainset, batch_size=args.batchsize, shuffle=False, num_workers=64, pin_memory=True, drop_last=False)
val_cc_loader = DataLoader(dataset=valset, batch_size=args.batchsize, shuffle=False, num_workers=64, pin_memory=True, drop_last=False)

print('==> Start generating train boxes...')
trainset.generate_box = True

len_ds = len(trainset)
all_boxes_outer = update_box(train_cc_loader, model.module.encoder, 0.5)
assert len(all_boxes_outer) == len_ds
trainset.boxes_outer = all_boxes_outer.cpu()

trainset.generate_box = False
print('Train box generated!!!')

print('==> Start generating val boxes...')
valset.generate_box = True

len_ds = len(valset)
all_boxes_outer = update_box(val_cc_loader, model.module.encoder, 0.5)
assert len(all_boxes_outer) == len_ds
valset.boxes_outer = all_boxes_outer.cpu()

valset.generate_box = False
print('Val box generated!!!')
############################################

# meta-train loader
train_sampler = CategoriesSampler(trainset.label, args.val_frequency*args.bs, args.way, args.shot + args.query)
train_loader = DataLoader(dataset=trainset, batch_sampler=train_sampler, num_workers=64, pin_memory=True)

val_sampler = CategoriesSampler(valset.label, args.val_episode, args.way, args.shot + args.query)
val_loader = DataLoader(dataset=valset, batch_sampler=val_sampler, num_workers=64, pin_memory=True)

if args.random_val_task == 0:
    print ('fix val set for all epochs')
    val_loader=[x for x in val_loader]

#label for query set, always in the same pattern
label = torch.arange(args.way, dtype=torch.int8).repeat(args.query)#012340123401234...
label = label.type(torch.LongTensor)
label = label.cuda()

optimizer = torch.optim.SGD([{'params': model.parameters(),'lr':args.lr}], momentum=0.9, nesterov=True, weight_decay=0.0005)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=args.step_size, gamma=args.gamma)

def save_model(name):
    torch.save(dict(params=model.state_dict()), osp.join(args.save_path, name + '.pth'))

trlog = {}
trlog['args'] = vars(args)
trlog['train_loss'] = []
trlog['val_loss'] = []
trlog['train_acc'] = []
trlog['val_acc'] = []
trlog['max_acc'] = 0.0
trlog['max_acc_epoch'] = 0

global_count = 0
writer = SummaryWriter(osp.join(args.save_path,'tf'))

result_list=[args.save_path]
for epoch in range(1, args.max_epoch + 1):
    print (args.save_path)
    start_time=time.time()

    tl = Averager()
    ta = Averager()

    tqdm_gen = tqdm.tqdm(train_loader)
    model.train()
    optimizer.zero_grad()
    for i, batch in enumerate(tqdm_gen, 1):

        global_count = global_count + 1
        data, _ = [_.cuda() for _ in batch]

        k = args.way * args.shot
        model.module.mode = 'encoder'
        data = model(data)
        data_shot, data_query = data[:k], data[k:]
        model.module.mode = 'meta'
        if args.shot > 1:
            data_shot = model.module.get_sfc(data_shot)
        logits = model((data_shot.unsqueeze(0).repeat(num_gpu, 1, 1, 1, 1), data_query))
        loss = F.cross_entropy(logits, label)

        acc = count_acc(logits, label)
        writer.add_scalar('data/loss', float(loss), global_count)
        writer.add_scalar('data/acc', float(acc), global_count)

        total_loss = loss/args.bs#batch of tasks, done by accumulate gradients
        writer.add_scalar('data/total_loss', float(total_loss), global_count)
        tqdm_gen.set_description('epo {}, total loss={:.4f} acc={:.4f}'
              .format(epoch, total_loss.item(), acc))
        tl.add(total_loss.item())
        ta.add(acc)
        total_loss.backward()

        detect_grad_nan(model)
        if i%args.bs==0: #batch of tasks, done by accumulate gradients
            optimizer.step()
            optimizer.zero_grad()

    tl = tl.item()
    ta = ta.item()
    vl = Averager()
    va = Averager()

    #validation
    model.eval()
    with torch.no_grad():
        tqdm_gen = tqdm.tqdm(val_loader)
        for i, batch in enumerate(tqdm_gen, 1):
            data, _ = [_.cuda() for _ in batch]
            k = args.way * args.shot
            model.module.mode = 'encoder'
            data = model(data)
            data_shot, data_query = data[:k], data[k:]
            model.module.mode = 'meta'
            if args.shot > 1:
                data_shot = model.module.get_sfc(data_shot)
            logits = model((data_shot.unsqueeze(0).repeat(num_gpu, 1, 1, 1, 1), data_query))

            loss = F.cross_entropy(logits, label)
            acc = count_acc(logits, label)
            vl.add(loss.item())
            va.add(acc)

    vl = vl.item()
    va = va.item()
    writer.add_scalar('data/val_loss', float(vl), epoch)
    writer.add_scalar('data/val_acc', float(va), epoch)
    tqdm_gen.set_description('epo {}, val, loss={:.4f} acc={:.4f}'.format(epoch, vl, va))
    print('epoch: {}, val_loss={:.4f} acc={:.4f}'.format(epoch, vl, va))

    print ('val acc:%.4f'%va)
    if va >= trlog['max_acc']:
        print ('*********A better model is found*********')
        trlog['max_acc'] = va
        trlog['max_acc_epoch'] = epoch
        save_model('max_acc')

    trlog['train_loss'].append(tl)
    trlog['train_acc'].append(ta)
    trlog['val_loss'].append(vl)
    trlog['val_acc'].append(va)

    result_list.append('epoch:%03d,training_loss:%.5f,training_acc:%.5f,val_loss:%.5f,val_acc:%.5f'%(epoch,tl,ta,vl,va))

    torch.save(trlog, osp.join(args.save_path, 'trlog'))

    print('best epoch {}, best val acc={:.4f}'.format(trlog['max_acc_epoch'], trlog['max_acc']))
    print ('This epoch takes %d seconds'%(time.time()-start_time),'\nstill need %.2f hour to finish'%((time.time()-start_time)*(args.max_epoch-epoch)/3600))
    lr_scheduler.step()

writer.close()

# Test Phase
trlog = torch.load(osp.join(args.save_path, 'trlog'))
print('Test episodes: ', args.test_episode)
test_acc_record = np.zeros((args.test_episode,))
model.load_state_dict(torch.load(osp.join(args.save_path, 'max_acc' + '.pth'))['params'])
model.eval()

# data prepare
testset = Dataset('test', args)
test_cc_loader = DataLoader(dataset=testset, batch_size=args.batchsize, shuffle=False, num_workers=64, pin_memory=True, drop_last=False)
########################################
print('==> Start generating test boxes...')
testset.generate_box = True

len_ds = len(testset)
all_boxes_outer = update_box(test_cc_loader, model.module.encoder, 0.5)
assert len(all_boxes_outer) == len_ds
testset.boxes_outer = all_boxes_outer.cpu()

testset.generate_box = False
print('Test box generated!!!')
########################################

sampler = CategoriesSampler(testset.label, args.test_episode, args.way, args.shot + args.query)
test_loader = DataLoader(testset, batch_sampler=sampler, num_workers=64, pin_memory=True)

ave_acc = Averager()
label = torch.arange(args.way).repeat(args.query)
if torch.cuda.is_available():
    label = label.type(torch.cuda.LongTensor)
else:
    label = label.type(torch.LongTensor)

tqdm_gen = tqdm.tqdm(test_loader)
with torch.no_grad():
    for i, batch in enumerate(tqdm_gen, 1):
        data, _ = [_.cuda() for _ in batch]
        k = args.way * args.shot
        model.module.mode = 'encoder'
        data = model(data)
        data_shot, data_query = data[:k], data[k:]
        model.module.mode = 'meta'
        if args.shot > 1:
            data_shot = model.module.get_sfc(data_shot)
        logits = model((data_shot.unsqueeze(0).repeat(num_gpu, 1, 1, 1, 1), data_query))
        acc = count_acc(logits, label)* 100
        ave_acc.add(acc)
        test_acc_record[i-1] = acc
        tqdm_gen.set_description('batch {}: {:.2f}({:.2f})'.format(i, ave_acc.item(), acc))

m, pm = compute_confidence_interval(test_acc_record)

result_list.append('Test Acc {:.4f} + {:.4f}'.format(m, pm))

print(result_list[-2])
print(result_list[-1])

save_list_to_txt(os.path.join(args.save_path, 'results.txt'), result_list)


#
# equivalent python v2 code, for those that completely reversed the challenge
#
# [at its heart, the binary had simple math routines (add, sub, cmp, div 2, mod 2, etc), implemented on a 1D Turing machine]
#

import sys
from pwn import *

# connect to challenge, read freq/mem from line 4 of output

r = remote("localhost", 13370)

in1 = r.recvuntil("kZB")
print(in1)

freq = float(in1.split()[-6])
mem = float(in1.split()[-2])

print("freq,mem=", freq, mem)

# compute integer freqi, memi

freqi = (14. - freq) / 5. * 65535
memi = (64. - mem) / 50. * 65535

freqi = int(freqi + 0.5)  # round
memi = int(memi + 0.5)

print("freqi,memi=", freqi, memi)

# compute coordinates

def fsum(a, b):
   vals = []
   for i in range(16):
      if (b & 1) == 0:
         vals.append(a)
      else:
         vals.append(b)
      b //= 2
   return sum(vals)

def gmod(a, b):
   ra = a % 2020
   rb = b % 2020
   if (ra + rb > 1000):
      return a // 2 + b
   else:
      return b // 2 + a

x = fsum(freqi, memi)
y = gmod(freqi, memi)

print("x,y=", x,y)
sys.stdout.flush()

# send answer

print(r.recvuntil("coordinates:"))

r.send(str(x) + "," + str(y) + "\n")

print(r.recvall())


#!/usr/bin/env python

import json
import math
import rospy
from time import sleep
from sensor_msgs.msg import Range
from sensor_msgs.msg import Imu
from sensor_msgs.msg import MagneticField
from psi.msg import CardinalDirection
from psi.msg import DirectionError
from psi.msg import MissionStatus

class Navigation:
    def drive_ms_cb(self, data):
        self.drive_mission_data = data.mission_status
        if self.drive_mission_data != self.prev_drive_mission_data:
            temp_mission_data = json.loads(self.drive_mission_data)
            self.direction = temp_mission_data['Direction']
            self.follow_up_direction = temp_mission_data['Follow-up-Direction']
            self.wait = temp_mission_data['Wait']
            self.delay = temp_mission_data['Delay']
            self.prev_drive_mission_data = self.drive_mission_data

    def drive_turn_control_cb(self, data):
        self.direction_error = -float(data.direction_error) / 100

        if self.wait:
            prev_direction = ""
            recorded_time = rospy.Time.now()
            prev_direction = self.direction
            self.direction = "Stop"
            while (rospy.Time.now() - recorded_time < rospy.Duration(self.delay)):
                self.navigate()
                rospy.sleep(1)
            self.wait = False
            self.direction = prev_direction

        self.navigate()

    def imu_cb(self, data):
         # imu data ranges from -180 to 180
         # To return it to positive, we add 360 to the negative values
        if data.orientation.z < 0:
            self.orientation_z = 360 + data.orientation.z
        else:
            self.orientation_z = data.orientation.z

    def navigate(self):
        self.cd_msg.header.stamp = rospy.Time.now()
        # This shoud change in the future when the wheel base link is
        # created and implemented in the urdf
        self.cd_msg.header.frame_id = "base_link"

        # Stop is always the first priority. If a stop is detected, execute the motion immediately.
        if "Stop" in self.direction:
            self.cardinal_direction = "T"
        # If there isn't a stop, determine the decision tree path to execute.
        else:
            # Determine if there is a junction.
            if self.junction:
                # Check if we have already reached the end of the block aisle
                # If the end_block flag has been triggered, we reverse the turn direction.
                if self.end_block:
                    # Execute the direction, which is a turn action, however, we reverse the turn direction.
                    if "Left" in self.direction:
                        # Determine the heading, and check if we have turned 90 degrees
                        self.prev_orientation_z = self.orientation_z
                        self.cardinal_direction = "E"
                        # Publish the message to turn the robot
                        self.cd_msg.cardinal_direction = self.cardinal_direction
                        self.cardinal_direction_pub.publish(self.cd_msg)
                        # Experimentation shows that 60 degree is a right angle turn
                        if self.prev_orientation_z > 270:
                            self.prev_orientation_z = 360 - self.prev_orientation_z
                            self.orientation_z = 360 - self.orientation_z
                        delta_orientation = abs(
                            self.orientation_z - self.prev_orientation_z)
                        while (delta_orientation < 90):
                            self.cardinal_direction = "E"
                            # Recalculate orientation in the while loop
                            delta_orientation = abs(
                                self.orientation_z - self.prev_orientation_z)
                            # Publish as long as the vehicle hasn't reach the 90 degrees
                            self.cd_msg.cardinal_direction = self.cardinal_direction
                            self.cardinal_direction_pub.publish(self.cd_msg)
                            self.rate.sleep()
                        if self.follow_up:
                            if "Forward" in self.follow_up_direction:
                                self.direction = self.follow_up_direction
                                # Publish message to move robot
                                self.cardinal_direction = "N"
                                self.cd_msg.cardinal_direction = self.cardinal_direction
                                self.cardinal_direction_pub.publish(
                                    self.cd_msg)
                                self.end_block = False
                    elif "Right" in self.direction:
                        # Determine the heading, and check if we have turned 90 degrees
                        self.prev_orientation_z = self.orientation_z
                        self.cardinal_direction = "W"
                        # Publish the message to turn the robot
                        self.cd_msg.cardinal_direction = self.cardinal_direction
                        self.cardinal_direction_pub.publish(self.cd_msg)
                        # Experimentation shows that 90 degree is a right angle turn
                        # Attempts to solve the 0/360 heading position error
                        if self.prev_orientation_z < 90:
                            self.prev_orientation_z = 360 + self.prev_orientation_z
                            self.orientation_z = self.orientation_z
                        delta_orientation = abs(
                            self.orientation_z - self.prev_orientation_z)
                        while (delta_orientation < 90):
                            self.cardinal_direction = "W"
                            # Recalculate orientation in while loop
                            delta_orientation = abs(
                                self.orientation_z - self.prev_orientation_z)
                            # Publish the message to turn the vehicle as long as the heading hasn't reach 90 degrees
                            self.cd_msg.cardinal_direction = self.cardinal_direction
                            self.cardinal_direction_pub.publish(self.cd_msg)
                            self.rate.sleep()
                        if self.follow_up:
                            if "Forward" in self.follow_up_direction:
                                self.direction = self.follow_up_direction
                                # Publish the message to move robot
                                self.cardinal_direction = "N"
                                self.cd_msg.cardinal_direction = self.cardinal_direction
                                self.cardinal_direction_pub.publish(
                                    self.cd_msg)
                                self.end_block = False
                else:  # If the end_block flag hasn't been triggered, we follow the motion in the qr code.
                    # Execute the direction, which is a turn action
                    if "Left" in self.direction:
                        # Determine the heading, and check if we have turned 90 degrees
                        self.prev_orientation_z = self.orientation_z
                        self.cardinal_direction = "W"
                        # Publish the message to turn the robot
                        self.cd_msg.cardinal_direction = self.cardinal_direction
                        self.cardinal_direction_pub.publish(self.cd_msg)
                        # Experimentation shows that 90 degree is a right angle turn
                        if self.prev_orientation_z < 90:
                            self.prev_orientation_z = 360 + self.prev_orientation_z
                            self.orientation_z = 360 + self.orientation_z
                        delta_orientation = abs(
                            self.orientation_z - self.prev_orientation_z)
                        while (delta_orientation < 90):
                            self.cardinal_direction = "W"
                            # Recalculate orientation in while loop
                            delta_orientation = abs(
                                self.orientation_z - self.prev_orientation_z)
                            # Publish the message to turn the robot as long as the desired heading hasn't reach
                            self.cd_msg.cardinal_direction = self.cardinal_direction
                            self.cardinal_direction_pub.publish(self.cd_msg)
                            self.rate.sleep()
                        if self.follow_up:
                            if "Forward" in self.follow_up_direction:
                                self.direction = self.follow_up_direction
                                # Publish message to move the robot
                                self.cardinal_direction = "N"
                                self.cd_msg.cardinal_direction = self.cardinal_direction
                                self.cardinal_direction_pub.publish(
                                    self.cd_msg)
                    elif "Right" in self.direction:
                        # Determine the heading, and check if we have turned 90 degrees
                        self.prev_orientation_z = self.orientation_z
                        self.cardinal_direction = "E"
                        # Publish message to turn the robot
                        self.cd_msg.cardinal_direction = self.cardinal_direction
                        self.cardinal_direction_pub.publish(self.cd_msg)
                        # Experimentation shows that 90 degree is a right angle turn
                        delta_orientation = abs(
                            self.orientation_z - self.prev_orientation_z)
                        while (delta_orientation < 90):
                            self.cardinal_direction = "E"
                            # Recalculates orientation in the while loop
                            delta_orientation = abs(
                                self.orientation_z - self.prev_orientation_z)
                            # Publish message as long as the desired heading hasn't been reached
                            self.cd_msg.cardinal_direction = self.cardinal_direction
                            self.cardinal_direction_pub.publish(self.cd_msg)
                            self.rate.sleep()
                        if self.follow_up:
                            if "Forward" in self.follow_up_direction:
                                self.direction = self.follow_up_direction
                                self.cardinal_direction = "N"
                                # Publish message to move the robot
                                self.cd_msg.cardinal_direction = self.cardinal_direction
                                self.cardinal_direction_pub.publish(
                                    self.cd_msg)

            if "Forward" in self.direction:
                if self.direction_error < -0.1:
                    self.cardinal_direction = "NW"
                elif self.direction_error > 0.1:
                    self.cardinal_direction = "NE"
                else:
                    self.cardinal_direction = "N"
            elif "Backward" in self.direction:
                if self.direction_error < -0.1:
                    self.cardinal_direction = "SW"
                elif self.direction_error > 0.1:
                    self.cardinal_direction = "SE"
                else:
                    self.cardinal_direction = "S"
            else:
                self.cardinal_direction = "T"

        self.cd_msg.cardinal_direction = self.cardinal_direction
        self.cardinal_direction_pub.publish(self.cd_msg)

    def status_ms_cb(self, data):
        self.status_mission_data = data.mission_status
        if self.status_mission_data != self.prev_status_mission_data:
            temp_mission_data = json.loads(self.status_mission_data)
            self.junction = temp_mission_data['Junction']
            self.follow_up = temp_mission_data['Follow-up']
            try:
                self.end_block = temp_mission_data['EndBlock']
            except KeyError:
                rospy.loginfo("No End Block value given.")
                pass
            self.end_mission = temp_mission_data['EndMission']

    def start(self):
        rospy.init_node('navigation', anonymous=False)
        self.rate = rospy.Rate(10)

        self.status_mission_data = ""
        self.prev_status_mission_data = ""
        self.junction = False
        self.follow_up = False
        self.end_block = False
        self.end_mission = False
        self.water_mission = False

        self.drive_mission_data = ""
        self.prev_drive_mission_data = ""
        self.direction = ""
        self.follow_up_direction = ""
        self.wait = False
        self.delay = 0.0
        self.direction_error = 0.0
        self.prev_direction_error = 0.0
        self.cardinal_direction = ""

        self.orientation_z = 0.0
        self.prev_orientation_z = 0.0

        self.range = 0

        self.cd_msg = CardinalDirection()
        self.cardinal_direction_pub = rospy.Publisher(
            'drive/cardinal_direction', CardinalDirection, queue_size=1)

        rospy.Subscriber('status/mission_status', MissionStatus,
                         self.status_ms_cb)

        rospy.Subscriber('drive/mission_status', MissionStatus,
                         self.drive_ms_cb)

        rospy.Subscriber('drive/direction_error', DirectionError,
                         self.drive_turn_control_cb)

        rospy.Subscriber('sensors/imu', Imu, self.imu_cb)

        rospy.spin()

if __name__ == "__main__":
    try:
        nav = Navigation()
        nav.start()
    except rospy.ROSInterruptException:
        pass


"""
The client sends a domain name taken from the command line (Ex: www.google.com) to the server.
The server opens a TCP connection to the IP address corresponding to the received domain name on port 80 (called HTTP-Srv).
It sends on the TCP connection the string: “GET / HTTP/1.0\n\n” and relays the answer back to the client.
When HTTP-Srv closes connection to the server, the server closes the connection to the client at its turn.
"""

import socket
import sys

try:
    clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    clientSocket.connect((sys.argv[1], int(sys.argv[2])))
except IndexError as msg:
    print(msg)
    exit(1)
except socket.error as msg:
    print(msg.strerror)
    exit(1)

try:
    clientSocket.sendall(bytes(sys.argv[3], 'ascii'), 0)

    answer = clientSocket.recv(1024)

    print(answer.decode('ascii'))
except socket.error as msg:
    print(msg.strerror)

clientSocket.close()


from unittest import TestCase, mock

from gosa.backend.components.jsonrpc_service import JsonRpcHandler
from gosa.backend.plugins.upload.main import *
from tests.RemoteTestCase import RemoteTestCase
from tornado.web import Application, decode_signed_value
import os
from requests_toolbelt.multipart.encoder import MultipartEncoder

class UploadManagerTestCase(TestCase):

    def test_registerUploadPath(self):
        manager = PluginRegistry.getInstance("UploadManager")
        uuid, path = manager.registerUploadPath("admin", "SESSION_ID", "workflow")

        res = manager.get_path_settings(uuid)
        assert res['type'] == "workflow"
        assert res['user'] == "admin"
        assert res['session_id'] == "SESSION_ID"

        manager.unregisterUploadPath(uuid)
        assert manager.get_path_settings(uuid) is None

    def test_garbage_collection(self):
        manager = PluginRegistry.getInstance("UploadManager")
        uuid, path = manager.registerUploadPath("admin", "SESSION_ID", "workflow")

        with mock.patch("gosa.backend.plugins.upload.main.datetime") as m:
            m.datetime.now.return_value = datetime.datetime.now() + datetime.timedelta(minutes=11)
            assert manager.get_path_settings(uuid) is not None
            manager._UploadManager__gc()
            assert manager.get_path_settings(uuid) is None

class UploadHandlerTestCase(RemoteTestCase):

    def get_app(self):
        return Application([('/rpc', JsonRpcHandler), ('/uploads/(?P<uuid>.*)?', UploadHandler)], cookie_secret='TecloigJink4',
                           xsrf_cookies=True)

    def test_upload(self):
        self.login()
        manager = PluginRegistry.getInstance("UploadManager")
        fpath = os.path.join(os.path.dirname(__file__), 'create_user.zip')

        with open(fpath, "rb") as f:
            m = MultipartEncoder(
                fields={'file': ('create_user.zip', f, 'text/plain')}
            )
            data = m.to_string()

            # try to use unregistered path
            uuid, path = manager.registerUploadPath("admin", self.session_id, "workflow")
            response = self.fetch("/uploads/unknown_path", method="POST", body=data, headers={
                'Content-Type': m.content_type
            })
            assert response.code == 404
            assert manager.unregisterUploadPath(uuid) is True

            # try to use path from another user
            uuid, path = manager.registerUploadPath("other_user", self.session_id, "workflow")
            response = self.fetch(path, method="POST", body=data, headers={
                'Content-Type': m.content_type
            })
            assert response.code == 403
            assert manager.unregisterUploadPath(uuid) is True

            # try to use path from another session
            uuid, path = manager.registerUploadPath("admin", "other session id", "workflow")
            response = self.fetch(path, method="POST", body=data, headers={
                'Content-Type': m.content_type
            })
            assert response.code == 403
            assert manager.unregisterUploadPath(uuid) is True

            # try to use path for unhandled type
            uuid, path = manager.registerUploadPath("admin", self.session_id, "unknown-type")
            response = self.fetch(path, method="POST", body=data, headers={
                'Content-Type': m.content_type
            })
            assert response.code == 501
            assert manager.unregisterUploadPath(uuid) is True

            # finally a working example
            uuid, path = manager.registerUploadPath("admin", self.session_id, "workflow")
            response = self.fetch(path, method="POST", body=data, headers={
                'Content-Type': m.content_type,
                'X-File-Name': 'create_user.zip'
            })
            assert response.code == 200
            # path should have been removed by successfully unsigning it
            assert manager.unregisterUploadPath(uuid) is False


import pickle
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM
# from tensorflow.keras.utils import np_utils
from tensorflow.keras.utils import to_categorical
from data_preprocessing import prepare_sequences, get_notes

def train_rnn(network_input, network_output, model_path):
    # Define the LSTM model
    model = Sequential()
    model.add(LSTM(512, input_shape=(network_input.shape[1], network_input.shape[2]), return_sequences=True))
    model.add(Dropout(0.3))
    model.add(LSTM(512))
    model.add(Dense(256))
    model.add(Dropout(0.3))
    model.add(Dense(network_output.shape[1], activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam')

    # Train the model
    model.fit(network_input, network_output, epochs=200, batch_size=128)

    # Save the model to disk
    model.save(model_path)

if __name__ == '__main__':
    notes = get_notes()
    network_input, network_output = prepare_sequences(notes, len(set(notes)))

    # with open('notes.pkl', 'rb') as filepath:
    #     notes = pickle.load(filepath)

    # n_vocab = len(set(notes))

    # network_input, network_output = prepare_sequences(notes, n_vocab)

    train_rnn(network_input, network_output, 'model.h5')


import cv2

img = cv2.imread("contour.png") # Resmi çağırır

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Resmi grileştirir

ret, thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) # Görüntüyü ikilik görüntüye (binary) çevirir

# (thresh değişkeni, hata azaltmak için varsayılan argümanlar)
contours,ret = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # kontur koordinatlarını bulur

cnt = contours[0]
print(cnt)

# Alan Hesaplama
M = cv2.moments(cnt) # Sözlük içerisinde bazı parametreler tutar
print("Alan:",M["m00"])

area = cv2.contourArea(cnt)
print("Alan:",area)

# Çevre Hesaplama
perimeter = cv2.arcLength(cnt,True)
print("Çevre:",perimeter)

cv2.imshow("img",img) # Resmi ekranda gösterme

cv2.waitKey(0) # Pencere açıldığında kapanma süresi ms cinsinden (0 olması sonuz açık pykalman).
cv2.destroyAllWindows() # Kapatma tuşuna basıldığında OpenCV'ye bağlı tüm pencereleri kapatır.


import string
from abc import ABC, abstractmethod
from math import log
from typing import List, Tuple, Generator

import numpy as np
from nltk.corpus import stopwords
from sklearn.metrics.pairwise import cosine_similarity
from util import tqdm

from util import Entity, Sample, get_question
from nltk import word_tokenize, SnowballStemmer, defaultdict
import ailog
import logging

ailog.setup_logging('logging.conf')
logger = logging.getLogger("This")

stemmer = SnowballStemmer('english')
stop_words = stopwords.words('english')

class Scorer(ABC):
    def __init__(self, remove_punctuation=True, remove_stopwords=False,
                 do_stem=False):
        self.remove_punctuation = remove_punctuation
        self.remove_stopwords = remove_stopwords
        self.do_stem = do_stem
        self.c = Curry()
        if self.remove_punctuation:
            self.c += unpunct
        if self.remove_stopwords:
            self.c += unstop
        if self.remove_punctuation:
            self.c += unpunct

    @abstractmethod
    def __call__(self, sentence, other_sentence, rest_sentences,
                 **kwargs) -> int:
        ...

class Curry:
    def __init__(self, function=None):
        if function:
            self.functions = [function]
        else:
            self.functions = []

    def __add__(self, other):
        c = Curry()
        c.functions = self.functions
        c.functions += [other]
        return c

    def __call__(self, arg):
        for f in self.functions:
            arg = f(arg)
        return arg

def fuzzy_substr(s1: str, s2: str):
    return s1 == s2

def remove_punctuation(s: str, trailing_only=False):
    if trailing_only:
        i = len(s) - 1
        while i > 0 and s[i] in string.punctuation:
            i -= 1
        return s[:i]
    return "".join(c for c in s if c not in string.punctuation)

def fuzzy_subsentence(sentence: List[str], other_sentence: List[str]):
    tries = len(other_sentence) - len(sentence) + 1
    if tries < 0:
        logger.debug(f'{sentence} in {other_sentence}: False')
        return False
    for i in range(tries):
        if all(fuzzy_substr(s1, s2) for s1, s2 in
               zip(sentence, other_sentence[i:])):
            logger.debug(f'{sentence} in {other_sentence}: True')
            return True
        logger.debug(f'{sentence} in {other_sentence}: False')
    return False

def stem(sentence: List[str]) -> Generator[str, None, None]:
    return (stemmer.stem(w) for w in sentence)

def unstop(sentence: List[str]) -> Generator[str, None, None]:
    return (w for w in sentence if w not in stop_words)

def unpunct(sentence: List[str]) -> Generator[str, None, None]:
    return (w for w in sentence if w not in string.punctuation)

def normalize(sentence: List[str]) -> List[str]:
    return [t.lower().strip() for t in sentence]

class MaxNgramScorer(Scorer):

    def __call__(self, sentence: List[str], other_sentence: List[str], *args,
                 **kwargs):

        if self.c.functions:
            sentence = list(self.c(sentence))
            other_sentence = list(self.c(other_sentence))

        max_ngram = 0
        sentence_idx = 0
        for i, _ in enumerate(sentence):
            subsentence = sentence[i:]
            for j, _ in enumerate(subsentence, 1):
                if fuzzy_subsentence(subsentence[:j],
                                     other_sentence) and j > max_ngram:
                    max_ngram = len(subsentence[:j])
                    sentence_idx = i
        return max_ngram

class GloveDistanceScorer(Scorer):
    def __init__(self, glove_file: str, m):
        super().__init__(do_stem=False)
        self.m = m
        self.words = dict()
        with open(glove_file, 'r') as f:
            content = f.readlines()
        for line in tqdm(content):
            word, embedding = line.split(" ", 1)
            embedding = np.array([float(val) for val in embedding.split(" ")])
            self.words[word] = embedding

    def w2v(self, word):
        try:
            return self.words[word].reshape(1, -1)
        except KeyError:
            return np.array([0] * 300, dtype=float).reshape(1, -1)

    def __call__(self, sentence: List[str], other_sentence: List[str], *args,
                 **kwargs):
        if self.c.functions:
            sentence = list(self.c(sentence))
            other_sentence = list(self.c(other_sentence))
        sims = np.array(
            [cosine_similarity(self.w2v(w), self.w2v(ow))
             for w in sentence
             for ow in other_sentence]
        )
        result = sims[sims.argsort()[-self.m:]].mean()
        return 0.0 if np.isnan(result) else float(result)

class MaxContainsScorer(Scorer):
    def __init__(self, remove_punctuation=True, remove_stopwords=True,
                 do_stem=True):
        super().__init__(remove_punctuation, remove_stopwords, do_stem)

    def __call__(self, sentence: List[str], other_sentence: List[str], *args,
                 **kwargs):
        if self.c.functions:
            sentence = list(self.c(sentence))
            other_sentence = list(self.c(other_sentence))
        return sum(1 for w in sentence if w in other_sentence)

ANY = 'all'
FULL_ONLY = 'full'

def yield_ngrams(sentence, n=1, skip_punctuation=True,
                 skip_stopwords=FULL_ONLY):
    i = 0
    if skip_punctuation:
        sentence = [s for s in sentence if s not in string.punctuation]
    if skip_stopwords == ANY:
        sentence = [s for s in sentence if s not in stopwords.words('english')]
    while i + n <= len(sentence):
        ngram = sentence[i:i + n]
        if all(w in stopwords.words('english') for w in ngram):
            pass
        else:
            yield ngram
        i += 1

class ContainsUniqueNgramScorer(Scorer):
    def __init__(self, remove_punctuation=True, remove_stopwords=False,
                 do_stem=True, n=2):

        super().__init__(remove_punctuation, remove_stopwords, do_stem)
        self.n = n

    def __call__(self, sentence, other_sentence, rest_sentences, *args,
                 **kwargs):
        if self.c.functions:
            sentence = list(self.c(sentence))
            other_sentence = list(self.c(other_sentence))

        for n_gram in yield_ngrams(sentence, self.n):
            if fuzzy_subsentence(n_gram, other_sentence) and not any(
                    fuzzy_subsentence(n_gram, s) for s in rest_sentences):
                return True
        return False

def avg_tf_idf(question, supporting_facts, rest):
    tf_idf_scores = tf_idf(question, supporting_facts, rest)
    sf_scores = defaultdict(int)
    rest_scores = defaultdict(int)
    for w, scores in tf_idf_scores.items():
        for i, score in enumerate(scores[0]):
            sf_scores[i] += score
        for i, score in enumerate(scores[1]):
            rest_scores[i] += score
    num_query_terms = len(tf_idf_scores.keys())
    for i, s in sf_scores.items():
        sf_scores[i] = s / num_query_terms

    for i, s in rest_scores.items():
        rest_scores[i] = s / num_query_terms
    return sf_scores, rest_scores

def tf_idf(question, supporting_facts, rest):
    c = Curry(stem) + Curry(unpunct) + Curry(unstop)
    question = list(c(question))
    for i, sf in enumerate(supporting_facts):
        supporting_facts[i] = list(c(sf))

    for i, r in enumerate(rest):
        rest[i] = list(c(r))
    tf_idf_scores = dict()
    for w in question:
        tf_supporting_facts = []
        for sf in supporting_facts:
            # sf = list(c(sf))

            tf_supporting_facts.append(1 / len(sf) if w in sf else 0)
            logger.debug(tf_supporting_facts)
        tf_rest = []
        for r in rest:
            # r = list(c(r))

            tf_rest.append(1 / len(sf) if w in r else 0)
        n = len(supporting_facts + rest)
        d = (1 + sum(tf_supporting_facts) + sum(tf_rest))

        idf = log(n / d)
        tf_idf_scores[w] = (
            [tf * idf for tf in tf_supporting_facts],
            [tf * idf for tf in tf_rest]
        )

    return tf_idf_scores

def ratio(question, supporting_fact, rest, operation: Scorer, aggregate=max):
    sf_score = operation(question, supporting_fact, rest)
    results = []
    for i, r in enumerate(rest):
        new_rest = rest[:] + supporting_fact
        new_rest.remove(r)
        result = operation(question, r, new_rest)
        results.append(result)
    rest_score = aggregate(results)
    epsilon = 0.5
    return (epsilon + sf_score) / (rest_score + epsilon)

def filter(question, supporting_facts, rest, operation: Scorer, threshold):
    everything = supporting_facts + rest
    result = []
    for s in everything:
        new_rest = rest[:]
        new_rest.remove(s)
        score = operation(question, s, rest)
        if score > threshold:
            result.append((score, s))
    return result

def average_precision(solution: List[List[str]], gold: List[List[str]]):
    subset_result = solution[:len(gold)]
    logger.debug(subset_result)
    # return sum(1 for s in subset_result if s in gold) / len(gold)
    return sum(sum(1 for s in subset_result[:i] if s in gold) / i for i, _ in
               enumerate(subset_result, 1)) / len(gold)

def last_correct_rank(solution: List, gold: List):
    for i, candidate in enumerate(solution, 1):
        if candidate in gold:
            gold.remove(candidate)
            if not gold:
                return i
    return len(solution)

def rank(question, supporting_facts, rest, operation: Scorer,
         eval_function) -> float:
    candidates = rest + supporting_facts

    def key(candidate):
        logger.debug(candidate)
        rest = candidates[:]
        rest.remove(candidate)
        return operation(question, candidate, rest)

    result = sorted(candidates, key=key, reverse=True)
    logger.debug(result)
    return eval_function(result, supporting_facts)

def fuzzy_map_supporting_fact(supporting_fact: str,
                              sentences: List[str]) -> int:
    for i, sentence in enumerate(sentences):
        if remove_punctuation(supporting_fact).strip() == remove_punctuation(
                sentence).strip():
            return i
    raise ValueError(
        "No supporting fact mapped! {}\n{}".format(supporting_fact,
                                                   '\n'.join(sentences)))

def get_supporting_fact(annotations: List[Entity]) -> List[str]:
    return [e.surface_form for e in annotations if e.type == "SupportingFact"]

def prepare(sample: Sample, annotations: List[Entity], paragraph_extract,
            just_supp_fact_ids=False):
    supp_facts_annotations = get_supporting_fact(annotations)
    sentences = paragraph_extract(sample.raw_text)
    question = get_question(sample.raw_text)
    try:
        supp_fact_ids = [fuzzy_map_supporting_fact(ann, sentences) for ann in
                         supp_facts_annotations]
    except ValueError as e:
        if len(supp_facts_annotations) == 1:
            sf = supp_facts_annotations[0]
            try:
                fuzzy_map_supporting_fact(sf, [question])
                supp_fact_ids = []
            except ValueError:
                raise e
        else:
            raise e

    if not just_supp_fact_ids:
        supp_fact_ids = sorted(supp_fact_ids, reverse=True)

        supp_facts = [sentences.pop(i) for i in supp_fact_ids]

        return (word_tokenize(question),
                [word_tokenize(s) for s in supp_facts],
                [word_tokenize(s) for s in sentences])
    else:
        return (word_tokenize(question),
                supp_fact_ids,
                [word_tokenize(s) for s in sentences])

def run_ratio_on_sample(samples: List[Tuple[Sample, List[Entity]]],
                        paragraph_extract, operation, aggregate=max):
    results = []
    for i, s in enumerate(tqdm(samples)):
        try:
            q, sf, sents = prepare(*s, paragraph_extract)
        except ValueError as e:
            raise ValueError(f"{i}: {str(e)}")
        if not sf:
            continue
        micro_results = [ratio(q, ssf, sents, operation, aggregate) for ssf in
                         sf]
        # if not micro_results:
        results.append(sum(micro_results) / len(micro_results))
    return results

def run_rank_on_sample(samples: List[Tuple[Sample, List[Entity]]],
                       paragraph_extract, operation,
                       eval_function):
    results = []
    for i, s in enumerate(samples):
        try:
            q, sf, sents = prepare(*s, paragraph_extract)
        except ValueError as e:
            raise ValueError(f"{i}: {str(e)}")
        if sf:
            result = rank(q, sf, sents, operation, eval_function)
            results.append(result)
    return results


from datetime import datetime
from ib_insync import *
import pandas as pd
import math
import sys
from typing import List, Any, Dict, Tuple

from datetime import datetime, time

def x_round(x):
    return round(x*4)/4

# TWs 7497, IBGW 4001
ib: IB = IB().connect('winhost',  7496, clientId=123, timeout=15)

es = Future('ES', '202303', 'CME')
print(ib.qualifyContracts(es))

ib.reqMarketDataType(1)

[ticker] = ib.reqTickers(es)
# print(ticker)
#ib.sleep(5)

# spx current price
spxValue =  ticker.marketPrice()
print('es futue value: ', spxValue)

expiration = '20230222'
trading_cls = 'E4C'

# put_contract = FuturesOption(es.symbol, expiration, 4010, 'P', 'CME', '50', 'USD', tradingClass=trading_cls)
# [put_contract] = ib.qualifyContracts(put_contract)
# [put_ticker] = ib.reqTickers(put_contract)
# print('put ticker:', put_ticker)
# print('put ticker martet price:', put_ticker.marketPrice())

# create combo
contracts = [
    FuturesOption(es.symbol, expiration, 4025, 'C', 'CME', '50', 'USD', tradingClass=trading_cls),
    FuturesOption(es.symbol, expiration, 4075, 'C', 'CME', '50', 'USD', tradingClass=trading_cls),
    FuturesOption(es.symbol, expiration, 3980, 'P', 'CME', '50', 'USD', tradingClass=trading_cls),
    FuturesOption(es.symbol, expiration, 3970, 'P', 'CME', '50', 'USD', tradingClass=trading_cls),]

contracts = ib.qualifyContracts(*contracts)

combo_contract = Contract(symbol=contracts[0].symbol, secType='BAG', exchange='SMART', currency='USD')
leg1 = ComboLeg(conId=contracts[0].conId, ratio=1, action='SELL', exchange=contracts[0].exchange)
leg2 = ComboLeg(conId=contracts[1].conId, ratio=1, action='BUY', exchange=contracts[1].exchange)
leg3 = ComboLeg(conId=contracts[2].conId, ratio=1, action='SELL', exchange=contracts[2].exchange)
leg4 = ComboLeg(conId=contracts[3].conId, ratio=1, action='BUY', exchange=contracts[3].exchange)
combo_contract.comboLegs = [leg1, leg2, leg3, leg4]

### IMPORTANT: use reqMktData insted of reqTickers and sleep so it has time to fill up, and then CANCEL
### https://groups.io/g/insync/topic/7793700
### BUT!! STILL DOES NOT WORK SOMETIMES. IT IS NOT RELIABLE
combo_ticker = ib.reqMktData(combo_contract)
ib.cancelMktData(combo_contract)

ib.sleep(5)

print('')
print('combo ticker:', combo_ticker)
print('combo ticker price:', combo_ticker.marketPrice())

# # Buy at the given price
# order = LimitOrder('BUY', 1, -55)
# trade = ib.placeOrder(combo_contract, order)

# ib.sleep(1)

ib.disconnect()


import urllib.request
from bs4 import BeautifulSoup

url = 'Enter a link to the site'
response = urllib.request.urlopen(url)
html = response.read()
soup = BeautifulSoup(html, 'html.parser')

links = soup.find_all('a')

for link in links:
    href = link.get('href')
    if href and (href.startswith('http://') or href.startswith('https://')):
        print(href)


# Polynomial Regression
# not a linear regression model

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Position_Salaries.csv')
# find the correlations between the level and salary
# to find out if the employee is bluffing about the salary
# position is already encoded in level column
# so it doesn't need to be included in the matrix
# of features X
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values

# Splitting the dataset into the Training set and Test set
# doesn't make sense to split the data into
# training and tests sets because there are only
# 10 observations
'''from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)'''

# Feature Scaling
"""from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)
sc_y = StandardScaler()
y_train = sc_y.fit_transform(y_train)"""

# Fitting linear regression to the dataset
# this will be a reference to then be able to
# compare the results of polynomial regression
# to the results of the linear regression reference
# space
from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X,y)

# Fitting Polynomial regression to the dataset
from sklearn.preprocessing import PolynomialFeatures
# used to make a new matrix of features X_poly
# poly_reg = PolynomialFeatures(degree = 2)
# poly_reg = PolynomialFeatures(degree = 3)
poly_reg = PolynomialFeatures(degree = 4)
X_poly = poly_reg.fit_transform(X)
# include this fit into a multiple linear regression model
lin_reg_2 = LinearRegression()
lin_reg_2.fit(X_poly, y)

# Visualising the linear regression results
# actual data
plt.scatter(X, y, color = 'red')
# now plot prediction data
plt.plot(X, lin_reg.predict(X), color = 'blue')
plt.title('Truth or Bluff (Linear Regression)')
plt.xlabel('Position Level')
plt.ylabel('Salary')
plt.savefig('Linear_py.png')

# Visualising the polynomial regression degree 2 results
# actual data
plt.scatter(X, y, color = 'red')
# now plot prediction data
plt.plot(X, lin_reg_2.predict(poly_reg.fit_transform(X)), color = 'blue')
plt.title('Truth or Bluff (Polynomial Degree 2 Regression)')
plt.xlabel('Position Level')
plt.ylabel('Salary')
plt.savefig('Polynomial_2_py.png')

# Visualising the polynomial regression degree 3 results
# actual data
plt.scatter(X, y, color = 'red')
# now plot prediction data
plt.plot(X, lin_reg_2.predict(poly_reg.fit_transform(X)), color = 'blue')
plt.title('Truth or Bluff (Polynomial Degree 3 Regression)')
plt.xlabel('Position Level')
plt.ylabel('Salary')
plt.savefig('Polynomial_3_py.png')

# Visualising the polynomial regression degree 4 results
# perfect model!
# actual data
plt.scatter(X, y, color = 'red')
# now plot prediction data
plt.plot(X, lin_reg_2.predict(poly_reg.fit_transform(X)), color = 'blue')
plt.title('Truth or Bluff (Polynomial Degree 4 Regression)')
plt.xlabel('Position Level')
plt.ylabel('Salary')
plt.savefig('Polynomial_4_py.png')

# Predicting a new result with Linear Regression
# predict one salary of level 6.5
lin_reg.predict(6.5)

# Predicting a new result with Polynomial Regression
lin_reg_2.predict(poly_reg.fit_transform(6.5))

Part 2 - Regression/Section 6 - Polynomial Regression/Polynomial_Regression/polynomial_regression.py


import re

from pmlib.filter.types import Rule

__all__ = [
    "Always"
]

# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Always

# Always MarkRead ""

class Always(Rule):

    def __repr__(self):
        text = "{0:s}: Always".format(str(self.action))
        return text

    def __init__(self):
        Rule.__init__(self, "Always")

        self._pattern = re.compile(
            "Always (?P<Action>.+)")
        return

    def parse(self, data: str) -> bool:
        m = self._pattern.search(data)
        if m is None:
            return False

        _action = m.group('Action')
        self.set_action(_action)
        return True


class TreeNode:
    def __init__(self, data):
        self.data = data
        self.children = []
        self.parent = None

    def add_child(self, child):
        child.parent = self
        self.children.append(child)

    def get_level(self):
        level = 0
        p = self.parent
        while p:
            level += 1
            p = p.parent
        return level

    def print_tree(self, level):
        if self.get_level() > level:
            return
        # print(self.get_level())
        spaces = " " * self.get_level() * 3
        prefix = spaces + "|__" if self.get_level else ""
        print(prefix + self.data)
        if self.children:
            for child in self.children:
                child.print_tree(level)

def add_node():
    global_ = TreeNode('Earth')

    country1 = TreeNode('India')
    country2 = TreeNode('USA')

    state1_1 = TreeNode('Himachal')
    state2_1 = TreeNode('Uttarakhand')

    state1_1.add_child(TreeNode('Kasol'))
    state1_1.add_child(TreeNode('Manali'))
    state2_1.add_child(TreeNode('Dehradun'))
    state2_1.add_child(TreeNode('Haridwar'))

    state1_2 = TreeNode('New Jersey')
    state2_2 = TreeNode('California')

    state1_2.add_child(TreeNode('Princeton'))
    state1_2.add_child(TreeNode('Trenton'))
    state2_2.add_child(TreeNode('San Fransisco'))
    state2_2.add_child(TreeNode('Mountain View'))
    state2_2.add_child(TreeNode('Palo Alto'))

    country1.add_child(state1_1)
    country1.add_child(state2_1)
    country2.add_child(state1_2)
    country2.add_child(state2_2)

    global_.add_child(country1)
    global_.add_child(country2)

    return global_

if __name__ == '__main__':
    root = add_node()
    root.print_tree(3)
    pass