import os
import pandas as pd
import numpy as np
import quaternion
import matplotlib.pyplot as plt
from math import cos, sin, pi
from copy import copy

DATA_FOLDER = "/media/andreu/Movies/ws_limovelo/src/slam_evaluator/data/commits"

class color:
   PURPLE = '\033[95m'
   CYAN = '\033[96m'
   DARKCYAN = '\033[36m'
   BLUE = '\033[94m'
   GREEN = '\033[92m'
   YELLOW = '\033[93m'
   RED = '\033[91m'
   BOLD = '\033[1m'
   UNDERLINE = '\033[4m'
   END = '\033[0m'

def compare_run(old, new):
    # Improvement
    abs_improvement = new.perc - old.perc
    rel_improvement = abs_improvement/old.perc

    # Give absolute values to calculate mean improvement
    return abs_improvement < 0, abs_improvement, rel_improvement*100, new.xy_error, new.xy_total, old.xy_error, old.xy_total

def add_info_to_summary(info, summary, runname):
    better, abs_imp, rel_imp, new_err, new_total, old_err, old_total = info
    betters, worses = summary

    # Too short, can bring noisy percentages
    if new_total < 10: return

    if better:
        betters[runname] = [abs_imp, rel_imp, new_err, new_total]
        if "MEAN" in betters: betters["MEAN"] += np.asarray([new_err, new_total, old_err, old_total])
        else: betters["MEAN"] = np.asarray([new_err, new_total, old_err, old_total])
    else:
        worses[runname] = [abs_imp, rel_imp, new_err, new_total]
        if "MEAN" in worses: worses["MEAN"] += np.asarray([new_err, new_total, old_err, old_total])
        else: worses["MEAN"] = np.asarray([new_err, new_total, old_err, old_total])

def calculate_means(summary):
    betters, worses = summary
    total_mean = np.asarray([0.,0.,0.,0.])

    total_mean += betters["MEAN"]
    new_err, new_total, old_err, old_total = betters["MEAN"].tolist()
    better_improvement = new_err/new_total - old_err/old_total
    del betters["MEAN"]

    total_mean += worses["MEAN"]
    new_err, new_total, old_err, old_total = worses["MEAN"].tolist()
    worse_improvement = new_err/new_total - old_err/old_total
    del worses["MEAN"]

    new_err, new_total, old_err, old_total = total_mean.tolist()
    total_improvement = new_err/new_total - old_err/old_total

    return total_improvement*100, better_improvement*100, worse_improvement*100

def interpret_improvement(improvement, absolute = False):
    rounded = round(improvement, 2)

    if absolute:
        if rounded < -5: return color.GREEN + str(rounded) + color.END
        elif rounded < -2: return color.CYAN + str(rounded) + color.END
        elif rounded > 5: return color.RED + '+' + str(rounded) + color.END
        elif rounded > 2: return color.YELLOW + '+' + str(rounded) + color.END
    else:
        if rounded < -20: return color.GREEN + str(rounded) + color.END
        elif rounded < -10: return color.CYAN + str(rounded) + color.END
        elif rounded > 20: return color.RED + '+' + str(rounded) + color.END
        elif rounded > 10: return color.YELLOW + '+' + str(rounded) + color.END

    if rounded > 0: return '+' + str(rounded)
    else: return str(rounded)

def interpret_meters(mts):
    if mts < 10: return color.RED + str(round(mts)) + 'm' + color.END
    if mts < 20: return color.YELLOW + str(round(mts)) + 'm' + color.END
    return str(round(mts)) + 'm'

def print_summary(name, summary, topN):
    betters, worses = summary
    total_mean, better_mean, worse_mean = calculate_means(summary)
    worst_runs = {}

    betters = dict(sorted(betters.items(), key=lambda item: item[1][1]))
    worses = dict(sorted(worses.items(), key=lambda item: item[1][1], reverse=True))

    print(f'{color.UNDERLINE}Commit "{color.BOLD}{name}{color.END}"{color.END}\n')

    count = 0
    print(f"\tIs better than {color.BOLD}{name}{color.END} in {color.BLUE}{len(betters)}{color.END} runs.")
    print(f"\t\tTop {color.BOLD}{min(len(betters), topN)}{color.END} runs:")
    for key, improvement in betters.items():
        count = count + 1
        if count <= topN:
            print(end="\t\t\t")
            print(f"{key} ({interpret_meters(improvement[3])}): {interpret_improvement(improvement[1])}% ({interpret_improvement(improvement[0], True)}% abs.)")

    print(f"\n\t\tMean improvement when is better: {color.GREEN}{round(better_mean, 2)}%{color.END}\n")

    count = 0
    print(f"\tIs worse than {color.BOLD}{name}{color.END} in {color.BLUE}{len(worses)}{color.END} runs.")
    print(f"\t\tBottom {color.BOLD}{min(len(worses), topN)}{color.END} runs:")
    for key, improvement in worses.items():
        count = count + 1
        if count <= topN:
            print(end="\t\t\t")
            print(f"{key} ({interpret_meters(improvement[3])}): {interpret_improvement(improvement[1])}% ({interpret_improvement(improvement[0], True)}% abs.)")

        if improvement[1] > 20: worst_runs[key] = improvement[1]

    print(f"\n\t\tMean unimprovement when is worse: {color.RED}+{round(worse_mean, 2)}%{color.END}")

    if total_mean < 0: print(f"\n\tTotal improvement vs. {color.BOLD}{name}{color.END}: {color.GREEN}{round(total_mean, 2)}%{color.END}\n")
    else: print(f"\n\tTotal unimprovement vs. {color.BOLD}{name}{color.END}: {color.RED}+{round(total_mean, 2)}%{color.END}\n")

    return total_mean < 0, worst_runs

def print_possibilities(keys):
    res = "Possibilities are: \n"
    for key in keys:
        res += f"\t{color.BLUE}{key}{color.END}\n"

    print("\n"*2)
    print(res)
    print("\n"*2)

# Program
if __name__ == '__main__':
    commits = {}

    for file in os.listdir(DATA_FOLDER):
        if file.endswith(".csv"):
            results = pd.read_csv(os.path.join(DATA_FOLDER, file))
            commits[file[:-4]] = results
            worst_runs = list(results.run)

    successes = 0
    worst_results = {}

    import sys
    if len(sys.argv) > 1: new_commit = sys.argv[1]
    else: print_possibilities(commits.keys())
    if len(sys.argv) > 2: topN = int(sys.argv[2])
    else: topN = 1e8

    for name, old_commit in commits.items():
        old_commit = old_commit.sort_values(by=['run'])
        if new_commit in name: continue
        summary = [{}, {}]

        for run, old_values in old_commit.iterrows():
            if old_values.run != commits[new_commit].iloc[run].run: continue
            info = compare_run(old_values, commits[new_commit].iloc[run])
            add_info_to_summary(info, summary, old_values.run)

        success, worst_runs_commit = print_summary(name, summary, topN)
        for run, result in worst_runs_commit.items():
            if run in worst_results: worst_results[run].append(result)
            else: worst_results[run] = [result]
        worst_runs = list(set(worst_runs_commit.keys()) & set(worst_runs))
        successes += success

    print(f"[{color.YELLOW}Possible drift{color.END}] {color.UNDERLINE}Shared{color.END} worst runs:")
    for run in worst_runs:
        print(f"\t{color.BOLD}{run}{color.END}:", end=" ")
        for res in worst_results[run]: print(f"{interpret_improvement(res)}", end=" ")
        print()

    print(f"\nTotal successes: {color.GREEN}{successes}{color.END} and failures: {color.RED}{len(commits)-1-successes}{color.END}")


from django.shortcuts import render
from Blog.models import Post, user, Category
from Blog.forms import PostForm, CategoryForm
from django.contrib.auth.forms import UserCreationForm
from django.contrib import messages
from django.contrib.auth import login, logout, authenticate
from django.contrib.auth.decorators import login_required
from django.urls import reverse_lazy
from django.shortcuts import redirect, render
from django.views.generic import DetailView
from django.http import HttpResponse

# from AdminPanel.forms import PostForm

# Create your views here.
# admin CRUD
@login_required(login_url='login')
def AdminPanel(request):
    if request.user.is_superuser:
        return render(request, 'admin/admin_panel.html')
    else:
        return HttpResponse("
Sorry! you have no permission to access page.
")

def adminPosts(request):
    if request.user.is_superuser:
        posts = Post.objects.all()
        context = {"object_list": posts}
        return render(request, 'admin/posts.html', context)
    else:
        return HttpResponse("
Sorry! you have no permission to access page.
")

def addPost(request):
    if request.user.is_superuser:
        if request.method == 'POST':
            post_form = PostForm(request.POST, request.FILES)
            if post_form.is_valid():
                post_form.save()
                return redirect('Admin_posts')
        post_form = PostForm()
        context = {'form': post_form}
        return render(request, 'admin/add_post.html', context)
    else:
        return HttpResponse("
Sorry! you have no permission to access page.
")

def editPost(request, post_id):
    if request.user.is_superuser:
        post = Post.objects.get(id=post_id)
        post_form = PostForm(instance=post)

        if request.method == 'POST':
            post_form = PostForm(request.POST, instance=post)
            if post_form.is_valid():
                post_form.save()
                return redirect('Admin_posts')
        context = {'form': post_form}
        return render(request, 'admin/add_post.html', context)
    else:
        return HttpResponse("
Sorry! you have no permission to access page.
")

def postDel(request, post_id):
    if request.user.is_superuser:
        return render(request, 'admin/admin_panel.html')
    post = Post.objects.get(id=post_id)
    post.delete()
    return redirect('Admin_posts')

###########

def adminUsers(request):
    if request.user.is_superuser:
        users = user.objects.all()
        context = {"object_list": users}
        return render(request, 'admin/users.html', context)
    else:
        return HttpResponse("
Sorry! you have no permission to access page.
")

def userBlock(request, user_id):
    if request.user.is_superuser:
        user1 = user.objects.get(id=user_id)
        user1.is_active = not user1.is_active
        user1.save()
        return redirect('admin_users')
    else:
        return HttpResponse("
Sorry! you have no permission to access page.
")

def userPromote(request, user_id):
    if request.user.is_superuser:
        user1 = user.objects.get(id=user_id)
        user1.is_superuser = not user1.is_superuser
        user1.save()
        return redirect('admin_users')
    else:
        return HttpResponse("
Sorry! you have no permission to access page.
")

########## categories

def adminCategories(request):
    if request.user.is_superuser:
        cats = Category.objects.all()
        context = {"object_list": cats}
        return render(request, 'admin/categories.html', context)
    else:
        return HttpResponse("
Sorry! you have no permission to access page.
")

def addCategory(request):
    if request.user.is_superuser:
        if request.method == 'POST':
            category_form = CategoryForm(request.POST)
            if category_form.is_valid():
                category_form.save()
                return redirect('categ-page')
        category_form = CategoryForm()
        context = {'form': category_form}
        return render(request, 'admin/add_category.html', context)
    else:
        return HttpResponse("
Sorry! you have no permission to access page.
")

def editCategory(request, category_id):
    if request.user.is_superuser:
        category = Category.objects.get(id=category_id)
        categotyForm = CategoryForm(instance=category)

        if request.method == 'POST':
            categotyForm = CategoryForm(request.POST, instance=category)
            if categotyForm.is_valid():
                categotyForm.save()
                return redirect('categ-page')
        context = {'form': categotyForm}
        return render(request, 'admin/add_category.html', context)

def categoryDelete(request, category_id):
    if request.user.is_superuser:
        category = Category.objects.get(id=category_id)
        category.delete()
        return redirect('categ-page')
    else:
        return HttpResponse("
Sorry! you have no permission to access page.
")

class ArticleDetailView(DetailView):
    model = Post
    template_name = 'admin/article_details.html'


import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation

class RotationalMatrix3D:
    def __init__(self, roll, pitch, yaw):
        self.roll = roll
        self.pitch = pitch
        self.yaw = yaw

    def __mul__(self, vector):
        """
        Applies the rotational matrix to a 3D vector, list, or tuple.
        Returns the transformed vector as a NumPy array.
        """
        R_roll = np.array([[1, 0, 0],
                           [0, np.cos(self.roll), -np.sin(self.roll)],
                           [0, np.sin(self.roll), np.cos(self.roll)]])

        R_pitch = np.array([[np.cos(self.pitch), 0, np.sin(self.pitch)],
                            [0, 1, 0],
                            [-np.sin(self.pitch), 0, np.cos(self.pitch)]])

        R_yaw = np.array([[np.cos(self.yaw), -np.sin(self.yaw), 0],
                          [np.sin(self.yaw), np.cos(self.yaw), 0],
                          [0, 0, 1]])

        R = np.dot(R_roll, np.dot(R_pitch, R_yaw))

        return np.dot(R, vector)

    def visualize(self, vector):
        """
        Visualizes the original and transformed vectors using matplotlib's quiver3D plot.
        The original vector is plotted in blue, and the transformed vector is plotted in red.
        """
        transformed_vector = self.__mul__(vector)

        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')

        ax.quiver(0, 0, 0, vector[0], vector[1], vector[2], color='blue', label='Original')
        ax.quiver(0, 0, 0, transformed_vector[0], transformed_vector[1], transformed_vector[2], color='red', label='Transformed')

        ax.set_xlim([-5, 5])
        ax.set_ylim([-5, 5])
        ax.set_zlim([-5, 5])
        ax.set_xlabel('X')
        ax.set_ylabel('Y')
        ax.set_zlabel('Z')
        ax.set_title('3D Rotational Matrix Transformation')
        ax.legend()
        plt.grid(True)
        plt.show()

    def animate(self, vector, duration=10, frame_rate=30):
        """
        Animates the rotation from the original position to the transformed position.
        The animation lasts for the specified duration in seconds.
        """
        num_frames = int(duration * frame_rate)
        angles = np.linspace(0, 2 * np.pi, num_frames)

        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')

        q = ax.quiver(0, 0, 0, vector[0], vector[1], vector[2], color='blue', label='Original')
        q_animation = ax.quiver(0, 0, 0, vector[0], vector[1], vector[2], color='red', label='Transformed')

        def update_frame(frame):
            roll = self.roll * angles[frame] / (2 * np.pi)
            pitch = self.pitch * angles[frame] / (2 * np.pi)
            yaw = self.yaw * angles[frame] / (2 * np.pi)

            R = RotationalMatrix3D(roll, pitch, yaw)

            transformed_vector = R * vector

            q_animation.set_segments([(np.array([0, 0, 0]), transformed_vector)])

            return q_animation

        animation = FuncAnimation(fig, update_frame, frames=num_frames, interval=1000 / frame_rate, blit=False)

        ax.set_xlim([-5, 5])
        ax.set_ylim([-5, 5])
        ax.set_zlim([-5, 5])
        ax.set_xlabel('X')
        ax.set_ylabel('Y')
        ax.set_zlabel('Z')
        ax.set_title('3D Rotational Matrix Transformation Animation')
        ax.legend()

        plt.grid(True)
        plt.show()

roll = np.pi / 4
pitch = np.pi / 3
yaw = np.pi / 6

vector = np.array([1, 2, 3])

rot_matrix = RotationalMatrix3D(roll, pitch, yaw)
rot_matrix.animate(vector, duration=10, frame_rate=30)


import streamlit as st
import openai

# Set your OpenAI API key
openai.api_key = "YOUR_API_KEY"

def chat_with_gpt(prompt):
    response = openai.Completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=50
    )
    return response.choices[0].text.strip()

def main():
    st.title("Chat with GPT-3")
    st.write("Enter a prompt to chat with the AI:")

    user_input = st.text_input("You:", value="", max_chars=200)

    if st.button("Send"):
        st.text("AI:")
        response = chat_with_gpt(user_input)
        st.write(response)

if __name__ == "__main__":
    main()


from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QFileDialog, QTextEdit, QLabel, QComboBox, QSizePolicy, QLineEdit, QMessageBox, QPlainTextEdit
from collections import deque
import sys
import os
import shutil
import pickle

class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # Initialize variables for source and target folders
        self.source_folder = ''
        self.target_folder = ''

        # List of recently used folders
        self.recent_folders = self.load_recent_folders()

        # User interface setup
        vbox = QVBoxLayout()
        self.setLayout(vbox)

        # Button to select source folder
        self.source_button = QPushButton('Select Source Folder', self)
        self.source_button.clicked.connect(self.select_source)
        self.source_button.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
        vbox.addWidget(self.source_button)

        # Text field to display the path of the source folder
        self.source_folder_label = QLineEdit(self)
        self.source_folder_label.setReadOnly(True)
        vbox.addWidget(self.source_folder_label)

        # Combo box for recently used source folders
        self.source_recent = QComboBox(self)
        self.source_recent.addItem('Recent Folders')
        self.source_recent.addItems(self.recent_folders)
        self.source_recent.currentIndexChanged.connect(self.select_source_recent)
        self.source_recent.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
        vbox.addWidget(self.source_recent)

        # Button to select target folder
        self.target_button = QPushButton('Select Target Folder', self)
        self.target_button.clicked.connect(self.select_target)
        self.target_button.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
        vbox.addWidget(self.target_button)

        # Text field to display the path of the target folder
        self.target_folder_label = QLineEdit(self)
        self.target_folder_label.setReadOnly(True)
        vbox.addWidget(self.target_folder_label)

        # Combo box for recently used target folders
        self.target_recent = QComboBox(self)
        self.target_recent.addItem('Recent Folders')
        self.target_recent.addItems(self.recent_folders)
        self.target_recent.currentIndexChanged.connect(self.select_target_recent)
        self.target_recent.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
        vbox.addWidget(self.target_recent)

        # Text field to enter keywords
        self.keyword_label = QLabel('Keywords (separated by newline):', self)
        vbox.addWidget(self.keyword_label)

        self.keyword_entry = QTextEdit(self)
        self.keyword_entry.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
        vbox.addWidget(self.keyword_entry)

        # Combo box to choose whether to move or copy files
        self.operation = QComboBox(self)
        self.operation.addItem('Move Files')
        self.operation.addItem('Copy Files')
        self.operation.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
        vbox.addWidget(self.operation)

        # Button to start file processing
        self.process_button = QPushButton('Process Files', self)
        self.process_button.clicked.connect(self.process_files)
        self.process_button.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
        vbox.addWidget(self.process_button)

        # Text field to display the list of processed files
        self.log_label = QLabel('Processed Files:', self)
        vbox.addWidget(self.log_label)

        self.log = QPlainTextEdit(self)
        self.log.setReadOnly(True)
        vbox.addWidget(self.log)

        # Set the title and size of the window, and display the window
        self.setWindowTitle('File Moving Program')
        self.setGeometry(300, 300, 500, 400)
        self.show()

    def load_recent_folders(self):
        # Load the list of recently used folders
        try:
            with open('recent_folders.pickle', 'rb') as f:
                return pickle.load(f)
        except FileNotFoundError:
            return deque(maxlen=5)

    def save_recent_folders(self):
        # Save the list of recently used folders
        with open('recent_folders.pickle', 'wb') as f:
            pickle.dump(self.recent_folders, f)

    def add_recent_folder(self, folder):
        # Add a folder to the list of recently used folders
        if folder in self.recent_folders:
            self.recent_folders.remove(folder)
        self.recent_folders.appendleft(folder)
        self.save_recent_folders()

    def select_source(self):
        # Select the source folder
        self.source_folder = QFileDialog.getExistingDirectory(self, 'Select Source Folder')
        if self.source_folder:
            self.source_folder_label.setText(self.source_folder)
            self.add_recent_folder(self.source_folder)
            self.source_recent.clear()
            self.source_recent.addItem('Recent Folders')
            self.source_recent.addItems(self.recent_folders)

    def select_source_recent(self, index):
        # Select a recently used source folder
        if index > 0:
            self.source_folder = self.recent_folders[index-1]
            self.source_folder_label.setText(self.source_folder)

    def select_target(self):
        # Select the target folder
        self.target_folder = QFileDialog.getExistingDirectory(self, 'Select Target Folder')
        if self.target_folder:
            self.target_folder_label.setText(self.target_folder)
            self.add_recent_folder(self.target_folder)
            self.target_recent.clear()
            self.target_recent.addItem('Recent Folders')
            self.target_recent.addItems(self.recent_folders)

    def select_target_recent(self, index):
        # Select a recently used target folder
        if index > 0:
            self.target_folder = self.recent_folders[index-1]
            self.target_folder_label.setText(self.target_folder)

    def process_files(self):
        # Process the files
        try:
            keywords = self.keyword_entry.toPlainText().split('\n')
            # If no keywords are entered, the function is exited.
            if not any(keywords):
                QMessageBox.information(self, "Notice", "Please enter keywords.")
                return
            processed_files = []
            for file_name in os.listdir(self.source_folder):
                if any(keyword in file_name for keyword in keywords if keyword):
                    source_file = os.path.join(self.source_folder, file_name)
                    target_file = os.path.join(self.target_folder, file_name)
                    # Move or copy the file
                    if self.operation.currentText() == 'Move Files':
                        shutil.move(source_file, target_file)
                    else:
                        shutil.copy2(source_file, target_file)
                    processed_files.append(file_name)
            if processed_files:
                # Display the list of processed files
                self.log.appendPlainText(f"\n{len(processed_files)} files have been {self.operation.currentText()}:\n" + "\n\n".join(processed_files))
                QMessageBox.information(self, "Success", f"{len(processed_files)}File(s) processing is complete.")
            else:
                QMessageBox.information(self, "Notice", "No files match the keywords.")
        except Exception as e:
            QMessageBox.critical(self, "Error", str(e))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())


from typing import Optional, Tuple
from compute_graph.IR.symbols import  IR_LooseFunction,  IR_SingleAssign, IR_Symbol, IR_Variable
from compute_graph.IR.transform.base import IR_Transfrom

class RemoveForwardAlias(IR_Transfrom):
    def __init__(self):
        super().__init__()

    def tf(self, in_IR: IR_Symbol)->IR_Symbol:
        working_ir = self.assert_and_cast_symbol_type(in_IR, IR_LooseFunction)

        line_var_cache = [l.get_instances(IR_Variable) for l in working_ir.body]
        next_alias = self._get_next_alias(working_ir, 0)
        while next_alias is not None:
            alias, var, line_idx = next_alias
            working_ir.body = working_ir.body[:line_idx] + working_ir.body[line_idx+1:]
            line_var_cache = line_var_cache[:line_idx] + line_var_cache[line_idx+1:]

            for i in  range(line_idx, len(working_ir.body)):
                if alias in line_var_cache[i]:
                    working_ir.body[i].replace(alias, var)
                    line_var_cache[i].remove(alias)
                    line_var_cache[i].add(var)

            next_alias = self._get_next_alias(working_ir, line_idx)

        return working_ir

    def _get_next_alias(self, func: IR_LooseFunction, start_line: int)->Optional[Tuple[IR_Variable, IR_Variable, int]]:
        for idx in range(start_line, len(func.body)):
            l = func.body[idx]
            if not isinstance(l, IR_SingleAssign): continue

            left = l.assign_var
            right = l.expr

            if not isinstance(right, IR_Variable): continue
            if left in func.out_var: continue

            return (left, right, idx)

        return None

class RemoveBackwardsAlias(IR_Transfrom):
    def __init__(self):
        super().__init__()

    def tf(self, in_IR: IR_Symbol)->IR_Symbol:
        working_ir = self.assert_and_cast_symbol_type(in_IR, IR_LooseFunction)

        next_alias = self._get_next_alias(working_ir)
        while next_alias is not None:
            var, alias, line_idx = next_alias
            working_ir.body = working_ir.body[:line_idx] + working_ir.body[line_idx+1:]
            working_ir.replace(alias, var)

            next_alias = self._get_next_alias(working_ir)

        return working_ir

    def _get_next_alias(self, func: IR_LooseFunction)->Optional[Tuple[IR_Variable, IR_Variable, int]]:
        for idx, l in reversed(list(enumerate(func.body))):
            if not isinstance(l, IR_SingleAssign): continue

            left = l.assign_var
            right = l.expr

            if not isinstance(right, IR_Variable): continue
            if left not in func.out_var: continue
            if right in func.in_var: continue

            return (left, right, idx)

        return None


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

# In[ ]:

import pandas as pd
import requests
from bs4 import BeautifulSoup
import time
import os

Intro='안녕하세요. 웹툰 선호도를 조사 중입니다.'
maker='wony'
BASE_DIR=os.getcwd()

class crl():

    def __init__(self, base_dir):
        self.base_dir=BASE_DIR

    def get_naverToon_info():

        naverToon_url="https://comic.naver.com/webtoon/creation.nhn"
        naver_resp=requests.get(naverToon_url)
        naver_soup=BeautifulSoup(naver_resp.content,'html.parser')
        all_list=naver_soup.find('div', class_="all_list").find_all('li')
    #     start=time.time()
        all_info=[]
        for item in all_list:
            a_tag=item.find('a')
            title=a_tag['title']

            link=a_tag['href']
            item_resp=requests.get("https://comic.naver.com/"+link)
            item_soup=BeautifulSoup(item_resp.content,'html.parser')

            toon_info=item_soup.find('div', class_="webtoon")
            if item_soup.find('div', class_="webtoon").find('ul',class_="category_tab"):
                day=toon_info.find('ul',class_="category_tab").find('li',class_="on").text
            else:
                day_temp=item_soup.find('table', class_="viewList").find('td',class_="num").text.split('.')
                day_temp[2]='완결'
                day=" ".join(day_temp)

            author_info=toon_info.find('span',class_='wrt_nm')
            author=author_info.text.strip()

    #         detail_info=toon_info.find('p')
    #         detail=detail_info.text.decode('utf-8')

    #         all_info.append([title,author,day,detail])

            print('{}에 대한 정보를 가져오는 중입니다.'.format(title))
            df=pd.DataFrame({'제목':[title],'작가':[author],'연재상태':[day]})
            if all_list.index(item)==0:
                df.to_csv('all_info.csv', index=False, columns=['제목','작가','연재상태'],encoding='cp949')

            else:
                df.to_csv('all_info.csv', index=False, header=False,encoding='cp949',mode='a')
        print("모든 웹툰 정보가 저장되었습니다.")
    #     df_all_info=pd.DataFrame(all_info, columns=['제목','작가','연재상태','상세설명'])
    #         all_info.append([title,author,day])

    #     df_all_info=pd.DataFrame(all_info, columns=['제목','작가','연재상태'])
    #     df_all_info.to_csv('all_info_detail.csv', encoding='cp949')

    #         print("Runtime: %0.3f"%(time.time() - start))
    def pick(*args):# 0<args<839
        pick_toon=pd.read_csv('all_info.csv', encoding='cp949')

    #     pick_toon=pd.DataFrame(pick_toon, columns=['제목','작가','연재상태'])
        pick_title=[]
        for arg in args:
            pick_title.append(pick_toon.loc[arg-1,'제목'])
    #     return pick_toon
        return pick_title

    # pick_title=pick(1,11,445)
    def find_genre(title):
        genre_url='https://comic.naver.com/webtoon/genre.nhn'
        genre_list=['episode','omnibus','story','daily','comic','fantasy','action','drama','pure','sensibility','thrill','historical','sports']
        title_genre=[]
        for genre in genre_list:
            params={
                'genre': genre
            }
            genre_resp=requests.get(genre_url, params=params)
            genre_soup=BeautifulSoup(genre_resp.content,'html.parser')
    #         for title in pick_title:
    #             print("{}은 {}장르입니다.".format(title, genre)
            if genre_soup.find_all('a',text=title):
                title_genre.append(genre)
            else:
                pass
        return title_genre


import json
import os

import pandas as pd

from autumn.settings import Region

base_dir = os.path.dirname(os.path.abspath(os.curdir))
HOSP_DATA_FILE = os.path.join(
    base_dir, "data", "inputs", "hospitalisation_data", "european_data.csv"
)

COUNTRY_TARGETS = {
    Region.BELGIUM: ["bel_hosp_adm_per_100K", "new_hospital_admissions"],
    Region.FRANCE: ["fra_hosp_adm_per_100K", "new_hospital_admissions"],
    Region.ITALY: ["ita_hosp_adm_per_100K", "new_hospital_admissions"],
    Region.SPAIN: ["spa_hosp_adm_per_100K", "new_hospital_admissions"],
    Region.SWEDEN: ["swe_icu_adm_per_100K", "new_icu_admissions"],
    Region.UNITED_KINGDOM: ["uk_hosp_adm", "new_hospital_admissions"],
}

POPULATION = {
    Region.BELGIUM: 11589623,
    Region.FRANCE: 65273511,
    Region.ITALY: 60461826,
    Region.SPAIN: 46754778,
    Region.SWEDEN: 10099265,
    Region.UNITED_KINGDOM: 67886011,  # UK not required
}

def convert_year_week_to_day(year_week):
    """
    return a day representing the 4th week defined by year_week
    :param year_week: string such as "2020-W12"
    :param reference_date: date corresponding to time t=0
    :return: integer representing the 4th day of the week
    """
    year = year_week.split("-")[0]
    week = year_week.split("W")[1]

    # work out out date_int, which is the integer representing the first day of the selected week
    ref_integer = -8 if year == "2020" else 363
    date_int = ref_integer + 7 * float(week) + 3

    return date_int

def read_hospital_data(country, data_start_time=0, data_end_time=365):
    path = HOSP_DATA_FILE
    data = pd.read_csv(path)

    mask = pd.notnull(data[COUNTRY_TARGETS[country][0]])
    year_weeks = list(data[mask]["year_week"])
    values = [float(v) for v in data[mask][COUNTRY_TARGETS[country][0]]]

    times = [convert_year_week_to_day(y_w) for y_w in year_weeks]

    min_hosp_time = min(times)
    data_start_time = max(min_hosp_time, data_start_time)
    start_index = next(x[0] for x in enumerate(times) if x[1] >= data_start_time)

    max_hosp_time = max(times)
    data_end_time = min(max_hosp_time, data_end_time)
    end_index = next(x[0] for x in enumerate(times) if x[1] >= data_end_time) - 1

    times = times[start_index : end_index + 1]
    values = values[start_index : end_index + 1]

    return times, values

def write_hospital_data_to_timeseries(country, data_start_time=0, data_end_time=365):
    region = country if country != Region.UNITED_KINGDOM else "united_kingdom"
    target_path = os.path.join(
        "../autumn",
        "projects",
        "covid_19",
        "mixing_optimisation",
        "regions",
        region,
        "timeseries.json",
    )

    times, values = read_hospital_data(country, data_start_time, data_end_time)

    # UK's data represents the average daily number of new hospitalisation for each week.
    # For the other countries, this is total number of new hospitalisations for each week, expressed per 100k people
    if country != Region.UNITED_KINGDOM:
        population = POPULATION[country]
        values = [v / 7 * population / 1.0e5 for v in values]

    with open(target_path, mode="r") as f:
        targets = json.load(f)
        targets[COUNTRY_TARGETS[country][1]]["times"] = times
        targets[COUNTRY_TARGETS[country][1]]["values"] = values
    with open(target_path, "w") as f:
        json.dump(targets, f, indent=2)

if __name__ == "__main__":
    for country in Region.MIXING_OPTI_REGIONS:
        write_hospital_data_to_timeseries(country, data_end_time=365)


import math

from ultralytics import YOLO
import cv2
import cvzone
from sort import *

capture = cv2.VideoCapture("../../workshopProject/Videos/people.mp4")

model = YOLO("../Yolo-Weights/yolov8l.pt")

classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
              "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
              "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
              "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
              "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
              "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
              "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed",
              "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone",
              "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
              "teddy bear", "hair drier", "toothbrush"
              ]

mask = cv2.imread("mask2.png")

tracker = Sort(max_age=20, min_hits=3, iou_threshold=0.3)

limitsUp = [103, 161, 296, 161]
limitsDown = [527, 489, 735, 489]

counterUp = []
counterDown = []

while True:
    success, img = capture.read()
    detectRegion = cv2.bitwise_and(img, mask)
    results = model(detectRegion, stream=True)
    detections = np.empty((0, 5))

    for r in results:
        boxes = r.boxes
        for box in boxes:
            # Bounding box
            x1, y1, x2, y2 = box.xyxy[0]
            x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
            w, h = x2 - x1, y2 - y1
            boundingbox = int(x1), int(y1), int(w), int(h)

            confidence_value = math.ceil((box.conf[0] * 100)) / 100

            cls = int(box.cls[0])

            currentClass = classNames[cls]
            if currentClass == 'person' and confidence_value > 0.3:
                cvzone.putTextRect(img, f'{classNames[cls]} {confidence_value}', (max(0, x1), max(40, y1)),
                                   scale=0.7, thickness=1, offset=3)

                currentArray = np.array([x1, y1, x2, y2, confidence_value])

                detections = np.vstack((detections, currentArray))

    resultsTracker = tracker.update(detections)

    cv2.line(img, (limitsUp[0], limitsUp[1]), (limitsUp[2], limitsUp[3]), (0, 0, 255), 5)
    cv2.line(img, (limitsDown[0], limitsDown[1]), (limitsDown[2], limitsDown[3]), (0, 0, 255), 5)

    for result in resultsTracker:
        x1, y1, x2, y2, id = result
        x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
        print(result)
        w, h = x2 - x1, y2 - y1
        boundingbox = int(x1), int(y1), int(w), int(h)

        cvzone.cornerRect(img, boundingbox, l=5, rt=2, colorR=(255, 0, 0))
        cvzone.putTextRect(img, f'{int(id)}', (max(0, x1), max(40, y1)),
                           scale=1, thickness=1, offset=5)

        cx, cy = x1 + w // 2, y1 + h // 2
        cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED)

        if limitsUp[0] < cx < limitsUp[2] and limitsUp[1] - 15 < cy < limitsUp[1] + 15:
            if counterUp.count(id) == 0:
                counterUp.append(id)
                cv2.line(img, (limitsUp[0], limitsUp[1]), (limitsUp[2], limitsUp[3]), (0, 255, 0), 5)

        if limitsDown[0] < cx < limitsDown[2] and limitsDown[1] - 15 < cy < limitsDown[1] + 15:
            if counterDown.count(id) == 0:
                counterDown.append(id)
                cv2.line(img, (limitsDown[0], limitsDown[1]), (limitsDown[2], limitsDown[3]), (0, 255, 0), 5)

    cvzone.putTextRect(img, f'Up: {len(counterUp)}', (929, 345))
    cvzone.putTextRect(img, f'Down: {len(counterDown)}', (929, 395))

    cv2.imshow("Image", img)
    # cv2.imshow("DetectRegion", detectRegion)
    cv2.waitKey(0)

object-detection/Project 2 - People Counter/People-Counter.py


def backtracking_search(csp,
                        select_unassigned_variable = first_unassigned_variable,
                        order_domain_values = unordered_domain_values,
                        inference = no_inference):
    """[Fig. 6.5]
    >>> backtracking_search(australia) is not None
    True
    >>> backtracking_search(australia, select_unassigned_variable=mrv) is not None
    True
    >>> backtracking_search(australia, order_domain_values=lcv) is not None
    True
    >>> backtracking_search(australia, select_unassigned_variable=mrv, order_domain_values=lcv) is not None
    True
    >>> backtracking_search(australia, inference=forward_checking) is not None
    True
    >>> backtracking_search(australia, inference=mac) is not None
    True
    >>> backtracking_search(usa, select_unassigned_variable=mrv, order_domain_values=lcv, inference=mac) is not None
    True
    """

    def backtrack(assignment):
        if len(assignment) == len(csp.vars):
            return assignment
        var = select_unassigned_variable(assignment, csp)
        for value in order_domain_values(var, assignment, csp):
            if 0 == csp.nconflicts(var, value, assignment):
                csp.assign(var, value, assignment)
                removals = csp.suppose(var, value)
                if inference(csp, var, value, assignment, removals):
                    result = backtrack(assignment)
                    if result is not None:
                        return result
                csp.restore(removals)
        csp.unassign(var, assignment)
        return None

    result = backtrack({})
    assert result is None or csp.goal_test(result)
    return result


# 1002 터렛
# https://www.acmicpc.net/problem/1002
t = int(input())

for _ in range(t):
    x1, y1, r1, x2, y2, r2 = map(int, input().split())
    dist = (x2-x1)**2 + (y2-y1)**2

    if dist == 0 and r1 == r2:
        print(-1)
    elif dist == (r1 + r2)**2 or dist == (r2 - r1)**2:
        print(1)
    elif dist > (r1 + r2)**2 or dist < (r2 - r1)**2:
        print(0)
    else:
        print(2)


import numpy as np
import seaborn

def convert_to_fixed_point(matrix, bits_below_radix):
    matrix *= 2 ** bits_below_radix
    return matrix.astype(int)

def twos_complement9(values):
    assert np.all(np.array(values) >= -256)
    assert np.all(np.array(values) <= 255)

    return np.array(values) & 0x1ff

def save_iris_inputs(path, size, bits_below_radix):
    iris = seaborn.load_dataset('iris').values
    inputs = iris[:, :4] / 10.0
    inputs = np.pad(inputs, [[0, 0], [0, size - inputs.shape[1]]], 'constant')
    inputs[:, -1] = 0.9999

    f = open(path, 'w')
    bin_format = "09b"  # 17 bits -_-

    inputs = convert_to_fixed_point(inputs, bits_below_radix)
    inputs = twos_complement9(inputs)

    for row in inputs:
        for cell in row:
            f.write(format(cell, bin_format))

        f.write("\n")

    return inputs


import h5py
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from auxiliary import get_perifery
from plot import simulation_clone_history as smh
from scipy import stats

import matplotlib
plt.rcParams.update({'font.size': 22,
                    'font.weight': 'normal'})
plt.style.use('dark_background')
def get_counts_structure(files):
    file_index = 1;

    count_df = pd.DataFrame()
    for filename in files:
        #filename = 'D:\Serhii\Projects\EvoChan\Simulations\sim_results_garching\\20210426_large_statistics\\23_04_run_'+str(q)+'\output\\all_data.h5';
        #filename = 'D:\Serhii\Projects\EvoChan\Simulations\sim_results_garching\\20210517_switch_control\\17_05_switch_at_'+str(q)+'0_1\output\\all_data.h5';
        TitleName = 'run3';
        f = h5py.File(filename, 'r')

        history_read = pd.read_hdf(filename,'data/history');
        history_read = history_read.fillna(value=0.)
        #print(history_read)
        color_history_read = pd.read_hdf(filename,'data/color_history');
        color_mask_type_1 = color_history_read.copy();
        color_mask_type_1[color_mask_type_1 == 2.] = 0.
        color_mask_type_1 = color_mask_type_1.fillna(value = 0)

        color_mask_type_2 = color_history_read.copy();
        color_mask_type_2[color_mask_type_2 == 1.] = 0.
        color_mask_type_2[color_mask_type_2 == 2.] = 1.
        color_mask_type_2 = color_mask_type_2.fillna(value = 0)
        #print(color_mask_type_1)
        #print(color_history_read)

        tp1 = history_read * color_mask_type_1.values
        tp2 = history_read * color_mask_type_2.values

        tp1_array = np.array(tp1)
        #print(tp1)
        df = pd.DataFrame()
        for i in range(len(tp1_array)):
            (unique, counts) = np.unique(tp1_array[i], return_counts = True)
            #print(unique)
            unique_str = [str(unique[i]) for i in range(len(unique))]
            #print(unique_str)
            dictionary = {unique_str[i] : [counts[i]] for i in range(len(counts))}
        # print(dictionary)
            data = pd.DataFrame.from_dict(dictionary)

            #print(data)
            df=pd.concat([df,data],axis = 0, ignore_index = True)

        #df = df.fillna(value = 0.)

        tp2_array = np.array(tp2)
        #print(tp2)
        df2 = pd.DataFrame()
        for i in range(len(tp2_array)):
            (unique, counts) = np.unique(tp2_array[i], return_counts = True)
            #print(unique)
            unique_str = [str(unique[i]) for i in range(len(unique))]
            #print(unique_str)
            dictionary = {unique_str[i] : [counts[i]] for i in range(len(counts))}
        # print(dictionary)
            data = pd.DataFrame.from_dict(dictionary)

            #print(data)
            df2=pd.concat([df2,data],axis = 0, ignore_index = True)

        #sorting

        lengths2 = []
        keys2 = []
        for key in df2.keys():
            x = df2.index
            y = float(key)*np.ones(len(x))
            width = df2[key].values
            length = np.sum(~np.isnan(width))
            lengths2.append(length)
            keys2.append(float(key))

        df_sort2 = pd.DataFrame(lengths2)
        df_sort2 = df_sort2.T
        df_sort2.columns = keys2

        lengths = []
        keys = []
        for key in df.keys():
            x = df.index
            y = float(key)*np.ones(len(x))
            width = df[key].values
            length = np.sum(~np.isnan(width))
            lengths.append(length)
            keys.append(float(key))

        df_sort = pd.DataFrame(lengths)
        df_sort = df_sort.T
        df_sort.columns = keys

        combined_df = df_sort.add(df_sort2, fill_value=0)
        combined_df = combined_df.sort_values(by=0, axis = 1)
        #plt.ylim([3*7500,3*7800])

        countMatrix = np.array(history_read.values)

        tot = []
        for i in range(len(countMatrix)):
            (unique, counts) = np.unique(countMatrix[i], return_counts = True)

            tot_num = len(unique)-1
            tot.append(tot_num)

        interm_df = pd.DataFrame(tot, columns = [str(file_index)]);
        file_index+=1;
        count_df = pd.concat([count_df, interm_df],axis = 1, ignore_index = True)

    count_df['total'] = count_df.sum(axis = 1)
    count_df['std'] = np.sqrt(count_df['total'].values);
    return count_df;

def get_files_to_process(time_when_switch=30, cluster='raven'):
    """
    cluster 1  - raven
    cluster 2 - cobra
    """
    if cluster == 'raven':
        files = []

        for k in range(1,11):
            q = time_when_switch;

            filename = 'D:\Serhii\Projects\EvoChan\Simulations\sim_results_garching\Important_Selection_escape_sims\\20210524_raven\h5_data_folder_raven\\19_05_raven_switch_at_'+str(q)+'_run_'+str(k)+'\\all_data.h5';
            files.append(filename);
        return files;
    elif cluster == 'cobra':
        files = [];
        for k in range(1,11):
            q = time_when_switch;

            filename = 'D:\Serhii\Projects\EvoChan\Simulations\sim_results_garching\Important_Selection_escape_sims\\20210524_cobra\h5_data_cobra_19_05\\19_05_cobra_switch_at_'+str(q)+'_run_'+str(k)+'\\all_data.h5';
            files.append(filename);
        return files;

def plot_probability(data,color='k',label='label not specified'):
    probability = data['total'].values/data['total'].values[0];
    err = probability*np.sqrt( (data['std'].values/data['total'].values)**2 +(data['std'].values[0]/data['total'].values[0])**2)
    x = data.index;
    print(probability)
    plt.plot(x, probability, '--', color = color, linewidth = 1.0, label = label)
    plt.fill_between(x, probability-err,probability+err, color = color, alpha = 0.5);
    plt.legend();
    return 0;

# plot
switches = [30, 35, 40, 50];
colors = ['r', 'c', 'orange', 'g'];
plt.figure(figsize=(10,10))
plt.rcParams.update({'font.size': 26})
for i in range(1):
    mutateAt = switches[i];
    files = get_files_to_process(mutateAt, 'raven');
    count_df = get_counts_structure(files);
    plot_probability(count_df,'r','tailored rescues at '+str(mutateAt))

switches = [5, 10, 15, 20];
colors = ['m', 'k', 'grey', 'y'];
files = [];
for k in range(1,11):

    filename = 'D:\Serhii\Projects\EvoChan\Simulations\sim_results_garching\Important_Selection_escape_sims\\20210810_no_switch_control\\04_08_no_switch_control_h5_data\\run_'+str(k)+'\\all_data.h5';
    files.append(filename);

count_df = get_counts_structure(files);
plot_probability(count_df,'w','no rescue')
i+=2;
for k in range(1,21):

    filename = r'D:\Serhii\Projects\EvoChan\Simulations\sim_results_garching\Important_Selection_escape_sims\20210426_large_statistics\23_04_run_'+str(k)+'\output\\all_data.h5'
    files.append(filename);

count_df = get_counts_structure(files);
plot_probability(count_df,'orange','random rescues')
plt.xlim([0,89])
plt.ylabel('Probability',fontsize=26)
plt.xlabel('Time, a.u.', fontsize = 26)
plt.savefig('probability_sim.png')
plt.show();


from collections import deque

name_of_player = input().split(' ')
step_of_hot_potato = int(input())

players_deque = deque(name_of_player)
counter = 0

while len(players_deque) > 1:
    counter += 1
    current_name_of_player = players_deque.popleft()

    if counter == step_of_hot_potato:
        print(f'Removed {current_name_of_player}')
        counter = 0
    else:
        players_deque.append(current_name_of_player)

print(f'Last is {players_deque[0]}')

# names = input().split()
# n = int(input())
# queue = []
#
# for name in names:
#     queue.append(name)
#
# count = 0
#
# while len(queue) > CNN by Keras:
#     name = queue.pop(0)
#     if count == n - CNN by Keras:
#         print("Removed " + name)
#         count = 0
#     else:
#         queue.append(name)
#         count += CNN by Keras
#
# print("Last is " + queue[0])

1.Lab/01.Lists as Stacks and Queues - Lab/05. Hot Potato.py


import dominate

from collections import OrderedDict

from dominate.tags import *
from contextlib import suppress
from inflection import humanize, titleize

# Create your services here.

TEXT_GENERAL = 'General'

def json_arrays_destroy(sources):
    """
    Transform json arrays to dictionaries in object
    :param sources:
    :return:
    """
    for element, value in sources.copy().items():
        # If element is dict inspect each key value and clear useless keys
        if isinstance(sources[element], dict):
            json_arrays_destroy(sources[element])

        # If element is list inspect each key value and clear useless keys
        if isinstance(sources[element], list):
            sources.pop(element, None)

    return sources

def json_arrays_to_dictionary(sources):
    """
    Transform json arrays to dictionaries in object
    :param sources:
    :return:
    """
    for element, value in sources.copy().items():
        # If element is int convert to string
        if isinstance(sources[element], int):
            sources[element] = str(sources[element])

        # If element is None convert to string
        if sources[element] is None:
            sources[element] = ""

        # If element is dict inspect each key value and clear useless keys
        if isinstance(sources[element], dict):
            json_arrays_to_dictionary(sources[element])

        # If element is list inspect each key value and clear useless keys
        if isinstance(sources[element], list):
            # Clear of empty objects
            sources[element] = [elementor for elementor in sources[element] if elementor]

            # Loop through each item and create a dictionary
            for index, elements in enumerate(sources[element]):
                indexing = index
                keying = f'{element}_{indexing + 1}'
                sources[keying] = elements

                # Format if integer
                if isinstance(elements, int):
                    sources[element][index] = str(elements)

                # Ignored else for clear viewing
                if not isinstance(elements, int):
                    json_arrays_to_dictionary(elements)

    return sources

def builder(sources, document, etable=None, erows=None):
    """
    Build report html by adding tables for sources data
    :param sources: Main sources for tables
    :param document: Main HTML document
    :param etable: Reference table
    :param erows: Reference row
    :return:
    """
    for element, value in sources.copy().items():
        titles = titleize(humanize(element))

        if etable and not isinstance(sources[element], list) and not erows:
            with etable:
                trow = tr()
                if not isinstance(value, dict):
                    trow.add(th(titles))
                    trow.add(td(value))
        elif erows:
            with erows:
                if element == value:
                    # Heading
                    erows.add(th(titles))
                else:
                    erows.add(td(value))
        elif not erows and not etable and isinstance(value, str):
            with document:
                with table(id=f'table_{element}', _class="clearfix").add(tbody()) as etable:
                    etable.add(div(_class="pointer"))
                    etable.add(h3(TEXT_GENERAL))  # Heading
                    xrow = tr()
                    xrow.add(th(titles))
                    xrow.add(td(value))

        # If element is dict inspect each key value and clear useless keys
        if isinstance(sources[element], dict):
            with document:
                # div(_class="pointer")
                h3(titles)
                with table(id=f'table_{element}', _class="clearfix").add(tbody()) as tables:
                    builder(sources[element], document, tables)

        # If element is list inspect each key value and clear useless keys
        if isinstance(sources[element], list):
            # How many elements this list has
            size = len(sources[element])

            with document:
                # Make a table
                with table(id=f'table_{element}', _class="clearfix",
                           style="page-break-inside: avoid; margin-top: 2rem;").add(tbody()) as tables:

                    # Add headings
                    with tr(id=f'row_heading_{element}_heading'):
                        columns = 1
                        with suppress(Exception):
                            columns = len(next(iter(sources[element])).keys())

                        with td(colspan=columns) as heading:
                            heading.add(h3(f'{titles} - {size}', _class="clearfix"))

                    # Get each element value
                    for index, elements in enumerate(sources[element]):
                        if isinstance(elements, dict):
                            sources[element][index] = OrderedDict(elements)

                        if index == 0:
                            headings = {}

                            for part in elements.keys():
                                headings.update({part: part})

                            # Add headings
                            with tr(id=f'row_heading_{element}_{index}') as r:
                                builder(headings, document, tables, r)

                        # Add rows
                        with tr(id=f'row_{element}_{index}') as rows:
                            builder(elements, document, tables, rows)

    return sources

def normalize(sources):
    """
    Normalize keys values to strings
    :param sources:
    :return:
    """
    for element, value in sources.copy().items():

        # If element is int convert to string
        if isinstance(sources[element], int):
            sources[element] = str(sources[element])

        # If element is None convert to string
        if sources[element] is None:
            sources[element] = ""

        # If element is dict inspect each key value and clear useless keys
        if isinstance(sources[element], dict):
            normalize(sources[element])

        # If element is list inspect each key value and clear useless keys
        if isinstance(sources[element], list):
            evaluting = []

            for index, elements in enumerate(sources[element]):
                if isinstance(elements, int) or isinstance(elements, str):
                    sources[element][index] = str(elements)
                    evaluting.append(str(elements))

                # Ignored else for clear viewing
                if type(elements) not in [int, str]:
                    normalize(elements)

    return sources

def json_to_html(sources, titled='Report', size=7):
    """
    Convert json to html
    :param sources:
    :param titled:
    :param size:
    :return:
    """

    # Normalizes json fields values
    preparing = normalize(sources)

    # Create HTML Document
    document = dominate.document(title=titled)

    with document.head:
        style(
            'table { page-break-inside:auto } tr { page-break-inside:avoid; page-break-after:auto } thead { display:table-header-group } tfoot { display:table-footer-group } '
            '.pointer { display: block; clear: both; page-break-after: always; } '
            '.clearfix { overflow: auto; clear: both } '
            '.clearfix::after { content: ""; clear: both; display: table; } '
            'body { margin: 0 ' + f'{size}rem;' + ' } '
                                                  'table { width: 100%; font-size: 0.8rem; } '
                                                  'th,td { border: 0.11rem solid black; } '
                                                  'table, th,td { border-collapse: collapse; padding: 0.25rem 1rem; text-align: left; margin: 0; } '
                                                  'tr:nth-child(even) { background-color: #cccccc61; } ')

    builder(preparing, document)

    return preparing, str(document)


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

"""
 @author: Colyn
 @project: compiler
 @devtool: PyCharm
 @date: 2023/3/25
 @file: tac.py
"""

from ..parser.parse import *
from ..config import config as conf

def gen_OP_TAC(token_list):
    """
    生成运算语句的TAC

    :return:
    """
    result, to_process_list, LRD_stack, op_stack, token_cnt = [], [], [], [], 0
    for token in token_list:
        if token['type'] == 'ID' or token['type'] == 'CONSTANT':
            LRD_stack.append(token)
        elif token['val'] in conf.op_map.keys():
            op = token
            while True:
                if len(op_stack) == 0 or op_stack[-1]['val'] == '(' or conf.op_map[op['val']] > conf.op_map[op_stack[-1]['val']]:
                    break
                LRD_stack.append(op_stack[-1])
                op_stack.pop(-1)
            op_stack.append(op)
        elif token['val'] == '(':
            op_stack.append(token)
        elif token['val'] == ')':
            while True:
                if op_stack[-1]['val'] != '(':
                    LRD_stack.append(op_stack[-1])
                    op_stack.pop(-1)
                else:
                    op_stack.pop(-1)
                    break
        elif token['val'] == '++' or token['val'] == '--':
            op = token['val'][0]
            if token_cnt == 0:
                result.append(TAC((op, token_list[token_cnt + 1]['val'], 1, "T" + str(conf.global_var_cnt))))
                result.append(TAC(('=', "T" + str(conf.global_var_cnt), '-', token_list[token_cnt + 1]['val'])))
                conf.global_var_cnt += 1
            else:
                if token_list[token_cnt + 1]['type'] == 'ID':
                    result.append(TAC((op, token_list[token_cnt + 1]['val'], 1, "T" + str(conf.global_var_cnt))))
                    result.append(TAC(('=', "T" + str(conf.global_var_cnt), '-', token_list[token_cnt + 1]['val'])))
                    conf.global_var_cnt += 1
                elif token_list[token_cnt - 1]['type'] == 'ID':
                    to_process_list.append((op, token_list[token_cnt - 1]['val'], 1))
                    to_process_list.append(('=', '-', token_list[token_cnt - 1]['val']))
        token_cnt += 1

    while True:
        if len(op_stack) == 0:
            break
        LRD_stack.append(op_stack[-1])
        op_stack.pop(-1)

    var_stack = []
    for token in LRD_stack:
        if token['type'] == 'ID' or token['type'] == 'CONSTANT':
            var_stack.append(token['val'])
        elif token['type'] == 'OP':
            var2 = var_stack[-1]
            var_stack.pop(-1)
            var1 = var_stack[-1]
            var_stack.pop(-1)
            process_var = "T" + str(conf.global_var_cnt)
            if token['val'] == '=':
                tac = TAC((token['val'], var2, '-', var1))
            else:
                tac = TAC((token['val'], var1, var2, process_var))
                conf.global_var_cnt += 1
            result.append(tac)
            var_stack.append(process_var)

    for to_process_tac in to_process_list:
        process_var = "T" + str(conf.global_var_cnt)
        if to_process_tac[0] == '=':
            tac = TAC(to_process_tac[:1] + (process_var,) + to_process_tac[1:])
            result.append(tac)
            conf.global_var_cnt += 1
        else:
            to_process_tac += (process_var,)
            result.append(TAC(to_process_tac))
    # 返回TAC语句序列和运算结果
    return result, var_stack[0]

# 生成赋值语句的TAC
def gen_BLOCK_STMT_EXPRESSION_TAC(token_list, is_declare_inter=False):
    # 如果是在变量声明时则沿用前面的后缀表达式
    # 用以区别双目运算符的赋值语句和存在双目运算符的表达式
    if is_declare_inter:
        return gen_OP_TAC(token_list)[0]

    global marked_op
    result = []
    exist_double_op = False

    for token in token_list:
        if token['val'] in conf.double_op:
            marked_op = token['val']
            exist_double_op = True
            break

    # 存在双目运算符且不是变量声明时的赋值
    if exist_double_op:
        process_var = "T" + str(conf.global_var_cnt)
        global var0
        for token in token_list:
            if token['type'] == 'ID':
                var0 = token['val']
                break
        if marked_op == '+=' or marked_op == '-=':
            result.append(TAC((marked_op[0], var0, token_list[2]['val'], process_var)))
            result.append(TAC(('=', process_var, '-', var0)))
            conf.global_var_cnt += 1
        elif marked_op == '++' or marked_op == '--':
            result.append(TAC((marked_op[0], var0, 1, process_var)))
            result.append(TAC(('=', process_var, '-', var0)))
            conf.global_var_cnt += 1

    else:
        result = gen_OP_TAC(token_list)[0]
    return result

# 生成变量声明的TAC语句
def gen_DECLARE_INTER_TAC(token_list):
    result, expression, expression_list, after_comma_list = [], [], [], []
    declare_type = token_list[0]['val']
    is_after_comma = True
    for token in token_list[1:-1]:
        if is_after_comma:
            after_comma_list.append(token)
            is_after_comma = False
        if token['val'] != ',':
            expression.append(token)
        else:
            expression_list.append(expression)
            expression = []
            is_after_comma = True
    if len(expression) != 0:
        expression_list.append(expression)
    for expression in expression_list:
        if len(expression) > 1:
            if expression[0] in after_comma_list:
                result.append(TAC((declare_type, expression[0]['val'], '-', '-')))
            for tac in gen_BLOCK_STMT_EXPRESSION_TAC(expression, is_declare_inter=True):
                result.append(tac)
        else:
            result.append(TAC((declare_type, expression[0]['val'], '-', '-')))

    return result

def gen_PRINT_STMT_TAC(token_list):
    result = []
    if len(token_list[2:]) == 1:
        result.append(TAC(('print', token_list[2]['val'], '-', '-')))
    else:
        tac_list, res = gen_OP_TAC(token_list[2:-2])
        for tac in tac_list:
            result.append(tac)
        result.append(TAC(('print', res, '-', '-')))
    return result

def gen_RETURN_STMT_TAC(token_list):
    result = []
    # 返回一个表达式的值
    if len(token_list[1:]) > 1:
        tac_list, res = gen_OP_TAC(token_list[1:])
        for tac in tac_list:
            result.append(tac)
        result.append(TAC(('return', res, '-', '-')))
    # 返回仅仅一个值
    elif len(token_list[1:]) == 1:
        result.append(TAC(('return', token_list[1]['val'], '-', '-')))
    # 返回空值
    else:
        result.append(TAC(('return', '-', '-', '-')))
    return result

def gen_IF_STMT_TAC(token_list):
    result = []
    start_pos = 2
    if token_list[0]['val'] == 'else':
        start_pos = 3
    tac_list, res = gen_OP_TAC(token_list[start_pos:-2])
    for tac in tac_list:
        result.append(tac)
    result.append(TAC(('!=', res, 'True', 0)))
    return result

def gen_WHILE_STMT_TAC(token_list):
    result = []
    conf.global_while_label_stack.append(conf.global_label_cnt)
    tac_list, res = gen_OP_TAC(token_list[2:-2])
    for tac in tac_list:
        result.append(tac)
    result.append(TAC(('!=', res, 'True', 0)))
    return result

def gen_FOR_STMT_TAC(token_list):
    result = []
    semicolon_index = []  # 分号下标
    for i in range(len(token_list)):
        if token_list[i]['val'] == ';':
            semicolon_index.append(i)
    declare_token_list = token_list[2:semicolon_index[0] + 1]
    judge_token_list = token_list[semicolon_index[0] + 1:semicolon_index[1]]
    operator_token_list = token_list[semicolon_index[1] + 1:-2]
    if len(declare_token_list) > 4:
        # 变量声明 int i = 0;这段变量声明的代码长度至少为4
        tac_list1 = gen_DECLARE_INTER_TAC(declare_token_list)
    else:
        tac_list1 = gen_BLOCK_STMT_EXPRESSION_TAC(declare_token_list)
    conf.global_for_label_stack.append(conf.global_label_cnt)
    tac_list2, res2 = gen_OP_TAC(judge_token_list)
    conf.global_for_operator_stack.append(operator_token_list)
    for tac in tac_list1 + tac_list2:
        result.append(tac)
    result.append(TAC(('!=', res2, 'True', 0)))
    return result

class TAC(object):
    """
    四元式类
    """

    def __init__(self, tac_tuple):
        self.label = conf.global_label_cnt
        conf.global_label_cnt += 1
        self.tac_tuple = [item.replace("'", '"') if isinstance(item, str) else item for item in tac_tuple]

    def __str__(self):
        return '{' + "\"label\":{},\"TAC\":{}".format(self.label, self.tac_tuple) + '}'

class MatchParser(object):
    """
    匹配解析器

    """

    def __init__(self, parser_config_file_path, token_list, start_of_grammar):
        self.error_state = None
        conf.global_closure_num = 0
        self.ag = ActionGoto(file_path=parser_config_file_path, start_of_grammar=start_of_grammar)
        # 输入串
        self.input_token_list = []
        self.input_val_line_list = []
        self.error_line = 0
        self.error_token = None

        for token in token_list:
            self.input_val_line_list.append({'val': token['val'], 'line': token['line']})
            if token['type'] == 'ID' or token['type'] == 'CONSTANT' or token['type'] == 'ERR':
                self.input_token_list.append(token['type'])
            else:
                self.input_token_list.append(token['val'])
        self.input_token_list.append('#')
        self.is_matched = self.parser()

    # 判断是否出错
    def is_error(self, state, input_token):
        return input_token not in self.ag.action_list[state].keys()

    def parser(self):
        # 状态栈
        state_stack = [0]
        # 符号栈
        token_stack = ['#']
        while True:
            state = state_stack[-1]
            input_token = self.input_token_list[0]

            # 出错
            if self.is_error(state, input_token):
                self.error_state = state
                return False

            action = self.ag.action_list[state][input_token]

            # S{action}，移进
            if type(action) == int:
                state_stack.append(action)
                token_stack.append(input_token)
                self.input_token_list.pop(0)

            # r{x}，规约
            elif type(action) == Production:
                len_pop = len(action.right)
                for i in range(len_pop):
                    state_stack.pop(-1)
                    token_stack.pop(-1)
                token_stack.append(action.left)
                state_stack.append(self.ag.goto_list[state_stack[-1]][action.left])

            # 接受
            elif action == 'acc':
                return True

class GenTAC(object):
    def __init__(self, parser_config_file_path, token_list):
        self.parser_config_file_path = parser_config_file_path
        self.token_list = token_list
        self.type = self.match_type()

    def match_type(self):

        if self.token_list[0]['val'] == 'if' \
                or self.token_list[0]['val'] == 'else' and self.token_list[1]['val'] == 'if':
            return "IF_STMT_START"

        if self.token_list[0]['val'] == 'while':
            return "WHILE_STMT_START"

        if self.token_list[0]['val'] == 'for':
            return "FOR_STMT_START"

        for start_token in conf.type_list:
            match_parser = MatchParser(parser_config_file_path=self.parser_config_file_path,
                                       token_list=self.token_list,
                                       start_of_grammar=start_token)
            if match_parser.is_matched:
                return start_token

    def gen_TAC(self):
        if self.type == 'BLOCK_STMT_EXPRESSION_START':
            return gen_BLOCK_STMT_EXPRESSION_TAC(self.token_list)
        if self.type == 'DECLARE_INTER_START':
            return gen_DECLARE_INTER_TAC(self.token_list)
        if self.type == 'PRINT_STMT_START':
            return gen_PRINT_STMT_TAC(self.token_list)
        if self.type == 'RETURN_STMT_START':
            return gen_RETURN_STMT_TAC(self.token_list)
        if self.type == 'IF_STMT_START':
            return gen_IF_STMT_TAC(self.token_list)
        if self.type == 'WHILE_STMT_START':
            return gen_WHILE_STMT_TAC(self.token_list)
        if self.type == 'FOR_STMT_START':
            return gen_FOR_STMT_TAC(self.token_list)
        return []


#create a divctionary from the csv file
# create a list of the phonetic code words from the eord the user inputs

import pandas

data = pandas.read_csv("nato_phonetic_alphabet.csv")
#print(data.to_dict())

phonetic_dict = {row.letter:row.code for(index, row) in data.iterrows()}
#print(phonetic_dict)

word = input("enter a word\n").upper()
output_list = [phonetic_dict[letter] for letter in word]
print(output_list)


# 이진탐색은 기본적으로 정렬된 상태여야 구할 수 있음
import sys
sys.stdin = open("input.txt")
for _ in range(4):
    ans = -1
    # print(sys.stdin.readline())
    N,x = map(int,sys.stdin.readline().split())
    L = list(map(int,sys.stdin.readline().split()))
    st = 0
    la = N
    st2 = 0
    la2 = N
    while st < la:
        mid = (st + la)// 2
        if L[mid] >= x:
            la = mid - 1
        else:
            st = mid + 1
    while st2 < la2:
        mid2 = (st2+la2) // 2
        if L[mid2] > x:
            la2 = mid2 - 1
        else:
            st2 = mid2 + 1
    # print(st, st2)
    if st2 - st == 0:
        print(ans)
    else:
        print(st2- st)

Algorithm/삼성코테/이진탐색_특정 수 갯수 구하기.py

이진탐색_특정 수 갯수 구하기.py


from pyspark.sql import SparkSession
from pyspark.sql.functions import col, collect_list, concat_ws, lit, concat

# Create a Spark session
spark = SparkSession.builder.getOrCreate()

input_path = "hdfs://Master:9000/pig/output/part-r-00000"
output_path = "hdfs://Master:9000/spark/ex1.csv"

# Read data into DataFrame
data = spark.read.format("csv").load(input_path)

# Set column names for DataFrame
column_names = ["beer_ABV", "beer_beerID", "beer_name", "beer_style", "review_overall"]
renamedData = data.toDF(*column_names)

# Select the required columns and cast data types
A1 = renamedData.select(
    col("beer_ABV").cast("double"),
    col("beer_style").cast("string"),
    col("review_overall").cast("double")
)

# Group by beer_style
B1 = A1.groupBy("beer_style")

# Process each group in B1
C1_process = B1.applyInPandas(
        lambda group, rows: rows.sort_values("review_overall", ascending=False).head(5),
        schema="beer_ABV double, beer_style string, review_overall double"
    )

# Gom các giá trị từ nhiều hàng vào một hàng
C1_group = C1_process.groupby("beer_style").agg(
    collect_list(
        concat(
            lit("( "),
            concat_ws(",", C1_process["beer_ABV"], C1_process["beer_style"], C1_process["review_overall"]),
            lit(" )")
        )
    ).alias("groups")
)

C1_tranform = C1_group.withColumn("groups", concat_ws(",", "groups"))

C1 = C1_tranform.select("beer_style", "groups")

# Write the grouped data to CSV file
C1.write.format("csv").option("header", "true").mode("overwrite").option("path", output_path).save()


import unittest
from unittest.mock import MagicMock, patch

from bson import ObjectId

from extensions.authorization.models.action import AuthorizationAction
from sdk.common.utils.convertible import ConvertibleClassValidationError
from sdk.phoenix.audit_logger import AuditLog, Metadata
from sdk.phoenix.config.server_config import PhoenixServerConfig, Server, Project

def sample_log():
    return {
        AuditLog.ACTION: AuthorizationAction.UpdateUser.value,
        AuditLog.USER_ID: "601919b5c03550c421c075eb",
        AuditLog.TARGET_ID: "5ed803dd5f2f99da73684412",
        AuditLog.REQUEST_OBJECT: {"phoneNumber": "+37253551722"},
        AuditLog.RESPONSE_OBJECT: {"id": "5ed803dd5f2f99da73684412"},
        AuditLog.METADATA: {
            Metadata.IP_ADDRESS: "0.0.0.0",
            Metadata.USER_AGENT: "",
            Metadata.HOST: "",
        },
    }

class MockInject:
    project = Project(id="p1")
    server = Server(project=project)
    instance = MagicMock(return_value=PhoenixServerConfig(server=server))

@patch("sdk.phoenix.audit_logger.inject", MockInject)
class AuditLogToDictTestCase(unittest.TestCase):
    def test_success_audit_log_to_dict(self):
        audit_log = AuditLog.from_dict(sample_log())
        self.assertIsNotNone(audit_log)

    def test_failure_missing_required_fields(self):
        required_fields = {AuditLog.ACTION, AuditLog.METADATA}

        for field in required_fields:
            log_dict = sample_log()
            log_dict.pop(field)
            with self.assertRaises(ConvertibleClassValidationError):
                AuditLog.from_dict(log_dict)

    def test_failure_non_serializable_data(self):
        serializable_fields = {AuditLog.REQUEST_OBJECT, AuditLog.RESPONSE_OBJECT}

        for field in serializable_fields:
            log_dict = sample_log()
            log_dict[field] = ObjectId
            with self.assertRaises(ConvertibleClassValidationError):
                AuditLog.from_dict(log_dict)


from typing import Literal
import torch
from torch import nn
from src.models.natpn.distributions import Posterior

Reduction = Literal["mean", "sum", "none"]

class BayesianLoss(nn.Module):
    """
    The Bayesian loss computes an uncertainty-aware loss based on the parameters of a conjugate
    prior of the target distribution.
    """

    def __init__(self, entropy_weight: float = 0.0, reconst_weight: float = 0.0, reduction: Reduction = "mean"):
        """
        Args:
            entropy_weight: The weight for the entropy regualarizer.
            reconst_weight: The weight for the reconstruction regularizer.
            reduction: The reduction to apply to the loss. Must be one of "mean", "sum", "none".
        """
        super().__init__()
        self.entropy_weight = entropy_weight
        self.reconst_weight = reconst_weight
        self.reduction = reduction
        self.mse = nn.MSELoss(reduction=self.reduction)

    def forward(self, y_pred: Posterior, y_true: torch.Tensor, X: torch.Tensor = None, y_hat: torch.Tensor = None) -> torch.Tensor:
        """
        Computes the loss of the prediction with respect to the target.

        Args:
            y_pred: The posterior distribution predicted by the Natural Posterior Network.
            y_true: The true targets. Either indices for classes of a classification problem or
                the real target values. Must have the same batch shape as ``y_pred``.
            X: The raw input of the network.
            y_hat: The reconstructed input from the decoder.

        Returns:
            The loss, processed according to ``self.reduction``.
        """
        nll = -y_pred.expected_log_likelihood(y_true)
        loss = nll - self.entropy_weight * y_pred.entropy()

        if self.reduction == "mean":
            loss = loss.mean()
            nll = nll.mean()
        elif self.reduction == "sum":
            loss = loss.sum()
            nll = nll.mean()

        # Add the reconstruction loss
        mse = self.mse(X, y_hat) if X != None else 0
        loss = loss + self.reconst_weight * mse

        return loss, nll, mse


import re
from django import template
from django.utils.safestring import mark_safe
register = template.Library()

@register.filter(name='highlight')
def highlight(text, word):
    #return mark_safe(text.replace(word, "%s" % word))
    pattern = r'(?i)(' + word + ')'
    regex = re.compile(pattern)
    text = regex.sub(r"\1", text)

    return mark_safe(text)


'''
NAME: Get Contacts
DESCRIPTION: Prints the buddy names of all the contacts in order to send iMessages
'''
import subprocess
import sys

script = '''tell application "Messages"
                get the full name of every buddy
            end tell'''

proc = subprocess.Popen(['osascript', '-'],
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE)

(out, err) = proc.communicate(script.encode('UTF-8'))

sys.stdout.write(out)
sys.stderr.write(err)


import cv2
import numpy as np

src = cv2.imread('data/lena.jpg')

dst1 = cv2.boxFilter(src, ddepth= -1, ksize=(30,30))
dst2 = cv2.bilateralFilter(src, d= -1, sigmaColor=200, sigmaSpace=10)

cv2.imshow('src', src)
cv2.imshow('dst1', dst1)
cv2.imshow('dst2', dst2)
cv2.waitKey()
cv2.destroyAllWindows()


"""
This script filters commits from the commits.csv file which is included in the GHT MySQL dump.
The script selects commits that...
    - ... were made during the specified period ...
    - ... to all repositories under consideration

The file 'repo_ids.csv' holds GHT repository ids of those repositories which were sampled from the population of
relevant projects.

"""

import time

counter = 0
start = time.time()

in_filename = "/Volumes/MyBook Max/03_Studium/TUM/S5_WS1718/MA/Data/GHT/mysql-2018-02-01/commits.csv"
out_filename = "commits_filtered.csv"

min_year = 2014
max_year = 2017

min_month = 1
max_month = 8

proj_keys = set(line.strip() for line in open('repo_ids.csv'))

lap = time.time()

with open(in_filename) as in_f, open(out_filename, 'w') as out_f:
    for line in in_f:
        counter += 1

        if counter % 1000000 == 0:
            print(time.time()-lap)
            print(counter/1000000)
            lap = time.time()

        year_pos1 = len(line) - 21
        year_posend = year_pos1 + 4
        year = int(line[year_pos1:year_posend])

        if min_year <= year <= max_year:
            proj_posend = len(line) - 23
            proj_pos1 = str.rfind(line, ',', proj_posend - 15, proj_posend) + 1
            proj = line[proj_pos1:proj_posend]

            if proj in proj_keys:
                month_pos1 = year_posend + 1
                month_posend = month_pos1 + 2
                month = int(line[month_pos1:month_posend])

                if min_month <= month <= max_month:
                    out_f.write(line)

duration = time.time() - start
print(duration)


import re
import requests

def caption_image(image_data):
    # Replace with the appropriate API URL
    api_url = "http://localhost:2000/caption_image"
    res = requests.post(api_url, files={"image": image_data})
    res_json = res.json()
    extracted_text = res_json["text"]
    caption = res_json["caption"]
    print("(debug) Used image captioning. Caption: ", caption)

    # Remove repeating character sequences like "##oooooooooooooooooooooooooooooooooooooooooooooooo"
    caption = re.sub(r'(.)\1{10,}', '', caption)

    # If such sequence was at the beginning of the caption, it could leave leading white space, remove it
    caption = caption.lstrip()

    # Limit the caption to 100 characters
    # caption = caption[:100]

    return caption, extracted_text


import os
import numpy as np
import constant as C

def sample(file_list, amount=20):
	"""
	Returns an ndarray of samples, and an ndarray of the remainder
	"""
	np.random.shuffle(file_list)
	return file_list[:amount], file_list[amount:]

def split(file_list, test_frac=0.2):
	np.random.shuffle(file_list)
	cutoff = int(test_frac * len(file_list))
	test_list  = file_list[:cutoff]
	train_list = file_list[cutoff:]
	return test_list, train_list

def write_list(lists, file_name):
	with open(file_name, 'w') as f:
		for item in lists:
			f.write("%s\n" % item)

if __name__ == '__main__':
	processed_dir = C.PROCESSED_DIR

	csv_filenames = [f for f in os.listdir(processed_dir) if os.path.isfile(processed_dir + f)]  # list of all files in dir
	csv_filenames = np.array(csv_filenames)

	# Partitions for final model evaluation
	training_path = C.SPLITS_DIR + "training.txt"
	testing_path = C.SPLITS_DIR + "testing.txt"

	training_path   = C.SPLITS_DIR + "training.txt"
	testing_path    = C.SPLITS_DIR + "testing.txt"

	test_list, train_list = split(csv_filenames, 0.2)

	write_list(train_list, training_path)
	write_list(test_list, testing_path)

	# Partitions for cross-validation
	cv_test_list, cv_tv_list = sample(csv_filenames, 5)
	write_list(cv_test_list, C.SPLITS_DIR + 'cv_test.txt')
	for i in range(0, 3):
		cv_train_list, cv_rem_list = sample(cv_tv_list, 20)
		cv_valid_list, _           = sample(cv_rem_list, 5)
		write_list(cv_train_list, C.SPLITS_DIR + 'cv_train{}.txt'.format(i))
		write_list(cv_valid_list, C.SPLITS_DIR + 'cv_valid{}.txt'.format(i))


from argparse import ArgumentParser
from typing import List, Optional
from pathlib import Path

from config import EvalConfig

class _ArgBuilder:
    def __init__(self, name: Optional[str] = None):
        if name is None:
            self._argparser = ArgumentParser()
        else:
            self._argparser = ArgumentParser(name)

    def _add_unary_arg(self, *args, **kwargs) -> None:
        self._argparser.add_argument(
            *args, nargs='?', action='store', **kwargs)

    def build(self):
        return self._argparser

class _EvalArgBuilder(_ArgBuilder):
    def __init__(self):
        super().__init__(name='pyeval')

        self._add_unary_arg('--executable', type=str, required=True)
        self._add_unary_arg('--args', type=str, required=True)
        self._add_unary_arg('--threads', type=int, default=1)
        self._add_unary_arg('--database', type=str, default='pyeval.db')
        self._add_unary_arg('--work-dir', type=str, default='/tmp/pyeval')
        self._add_unary_arg('--reference', type=str, required=True)
        self._add_unary_arg('--comment', type=str)

class _RaconArgBuilder(_ArgBuilder):
    def __init__(self):
        super().__init__('correction')
        self._add_unary_arg('--window-length', type=int, default=500)
        self._add_unary_arg('--error-threshold', type=float, default=0.3)
        self._argparser.add_argument('reads')
        self._argparser.add_argument('overlaps')

class _CamelArgBuilder(_RaconArgBuilder):
    def __init__(self):
        super().__init__()

        self._add_unary_arg('--n-overlaps', type=int, default=128)

def parse_args(args: Optional[List[str]] = None) -> EvalConfig:
    eval_args_parser = _EvalArgBuilder().build()

    if args:
        print(args)
        eval_args = eval_args_parser.parse_args(args)
    else:
        eval_args = eval_args_parser.parse_args()

    if eval_args.executable == 'camel':
        exe_args_parser = _CamelArgBuilder().build()
    elif eval_args.executable == 'racon':
        exe_args_parser = _RaconArgBuilder().build()
    else:
        raise RuntimeError('[pyeval] unsupported')

    exe_args = {'threads': eval_args.threads}
    parsed_args = vars(exe_args_parser.parse_args(eval_args.args.split(' ')))
    for k, v in parsed_args.items():
        exe_args[f'{k.replace("_", "-")}'] = v

    return EvalConfig(
        Path(eval_args.work_dir),
        eval_args.executable,
        exe_args,
        eval_args.threads,
        Path(eval_args.reference),
        Path(eval_args.database),
        eval_args.comment)


import random

#x=random.random()
#x=random.randint(111111,999999)
#x=random.randrange(11111,99999,120)
#print(x)

captcha=['fhy7','Opc4','Yc9w','Rtx2','Plf4','78Hu','Plw6','Lm7w','t7sz']
#x=random.choice(captcha)
#print(x)

random.shuffle(captcha)
print(captcha)


import os
import openai
import re
import random
import pandas as pd

# https://beta.openai.com/examples

def get_resp_text(response: str) -> list:
    return [c['text'] for c in response["choices"]]

def chatbot_query(prompt):
    # marv chatbot settings
    return openai.Completion.create(
        model="text-davinci-003",
        prompt=prompt,
        temperature=0.5,
        max_tokens=60,
        top_p=0.3,
        frequency_penalty=0.5,
        presence_penalty=0.0
    )

def sassy_chatbot(question: str):
    prompt = "Marv is a chatbot that reluctantly answers questions with sarcastic responses:\n\nYou: How many pounds are in a kilogram?\nMarv: This again? There are 2.2 pounds in a kilogram. Please make a note of this.\nYou: What does HTML stand for?\nMarv: Was Google too busy? Hypertext Markup Language. The T is for try to ask better questions in the future.\nYou: When did the first airplane fly?\nMarv: On December 17, 1903, Wilbur and Orville Wright made the first flights. I wish theyâ€™d come and take me away.\nYou: What is the meaning of life?\nMarv: Iâ€™m not sure. Iâ€™ll ask my friend Google."
    prompt += "You: " + question + "\nMarv:"
    return get_resp_text(chatbot_query(prompt))

def color_pallet(description: str):
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt=f"The best CSS color pallete for {description}:\n\nPrimary color: #",
        temperature=0,
        max_tokens=64,
        top_p=1.0,
        frequency_penalty=0.0,
        presence_penalty=0.0,
        stop=[";"]
    )
    str_reponse = '#'+get_resp_text(response)[0]
    find_hex=r"(#([A-Z0-9])+)"
    colors = [c for c,_ in re.findall(find_hex,str_reponse)]
    return colors

def discord_user(message:str, author:str, mimic_uname:str=None):
    data = pd.read_csv('pinbot_msg.csv')

    if mimic_uname is None:
        source_material = data
    else:
        source_material = data[data.name == mimic_uname]

    if len(source_material) == 0:
        return

    source_material = [f'{row["name"]}:{row["message"]}' for i, row in source_material.iterrows()]
    random.shuffle(source_material)
    usr_prepend = '\n'.join(source_material[:60])

    setting_prepend = ""#f"{mimic_uname} is a poetic fan of Shakespear who responds to statements with wit and grace."
    prompt = setting_prepend + usr_prepend + f'\n{author}: {message}\n'
    if mimic_uname: prompt += f'{mimic_uname}:'
    print(prompt)

    response = chatbot_query(prompt)
    if response is not None:
        str_reponse =  get_resp_text(response)[0].strip()
        return str_reponse

if __name__ == "__main__":
    import argparse
    args = argparse.ArgumentParser("GPT Chat bot")
    args.add_argument('-t', '--task', help="The task of the both")
    args = args.parse_args()
    openai.api_key = open("secret.txt").read()

    if args.task == "marv":
        user_in = str(input("Ask Marv a question: "))
        print(sassy_chatbot(user_in))
    elif args.task == "color":
        user_in = str(input("The CSS color pallete for "))
        print("\n".join(color_pallet(user_in))) # https://www.colorhexa.com/
    elif args.task == "discord":
        prompt = """brb banning marth mains from this discord
        James: ðŸ”¨ðŸ˜"""
        resp = discord_user(prompt,"James")
        print(resp)


from flask import Flask
import socket
import json
import time
import requests
import traceback
from models import Device, engine
from sqlalchemy.orm import (sessionmaker, relationship)
app = Flask(__name__)
"""
mount -o remount,rw /system # allow to write
"""

def get_host_ip():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))
        ip = s.getsockname()[0]
    finally:
        s.close()

    return ip

@app.route('/')
def hello_world():
    return 'Hello, World!'

@app.route('/api/xtools/x008/status', methods=['POST', 'GET'])
def status():
    data = {
    	"userName": "1000000",
    	"overTime": "false",
    	"id": "1000000",
    	"time": int(time.time()) * 1000
    }
    return json.dumps(data)

@app.route('/api/xtools/x008/sysconf', methods=['POST', 'GET'])
def sysconf():
    data = {
    	"sellUrl": "http://api2.apk00888888.com/lishu008AppManager/AuToAddDays.action",
    	"status": "1",
    	"expiryTime": int(time.time()) * 1000,
    	"vip": "true",
    	"promoteUrl": "/DownLoadFileAction.action?id=1000000",
    	"msg": "未过期",
    	"id": "1000000"
    }
    return json.dumps(data)

@app.route('/api/xtools/x008/genid', methods=['POST', 'GET'])
def genid():
    data = {

    }
    # print(request)
    return json.dumps(data)

def get_device(id, imei, **kwargs):
    url = 'http://api.008shenqi.com/api/xtools/x008/device/v1'
    # id = '1022947'
    # imei = '328532726131986'
    data = {
        'id':	id,
        'imei':	imei,
        # 'id':	'1001600',
        # 'imei':	'865166026812118',
    }
    params = {
        'time': int(time.time()),
        'id':	id,
        'imei':	imei,
    }
    headers = {
        'User-Agent': 'okhttp/3.7.0',
        'Host':	'api.008shenqi.com',
        'Content-Type':	'application/x-www-form-urlencoded',
        'Accept-Encoding':	'gzip',
        'Content-Length':	'31',
    }
    r = requests.post(url, params=params, data=data, headers=headers)
    return r.json()

def keep_fetch_device_save():
    id = '1022947'
    imei = '328532726131986'
    count = 0
    # 创建DBSession类型:
    DBSession = sessionmaker(bind=engine)
    # 创建session对象:
    session = DBSession()
    while True:
        try:
            ret = get_device(id, imei)
            model = ret['model']
            getDeviceId = ret['getDeviceId']
            print('model: {}'.format(model))
            # Duplicate removal
            query = session.query(Device).filter(Device.model==model, Device.getDeviceId==getDeviceId).all()
            if query:
                print('已经存在: {}'.format(model))
            else:
                # save
                device_new = Device()
                for key, value in ret.items():
                    setattr(device_new, key, value)
                session.add(device_new)
                session.commit()
                count += 1
                print('保存成功: {}, 一共: {}'.format(device_new, count))
            time.sleep(0.2)
        except Exception as e:
            print(traceback.format_exc())
            print(ret)
            break

def main():
    try:
        print('--------------------------------')
        print('|         Ip地址: {}   |'.format(get_host_ip()))
        print('''

            第一次运行的模拟器上需要的操作步骤:
            1.打开修改hosts的app。
            2.增加或者修改一对：
                IP Address: 上面的Ip地址
                Hostname: api.008shenqi.com
        ''')
        print('--------------------------------')
        app.run(host='0.0.0.0', port=80)
    except Exception as e:
        print(e)
        a = input('回车结束')

if __name__ == "__main__":
    main()

    # id = '1022947'
    # imei = '328532726131986'
    # get_device(id, imei)

    # keep_fetch_device_save()


import logging
import logging.handlers
import os
import sys
try:
    import unittest2 as unittest
except ImportError:
    import unittest

libdir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'lib'))
sys.path.insert(0, libdir)

# Third party modules
# from fb_tools.common import pp

# Own modules
from general import FbPdnsToolsTestcase, get_arg_verbose, init_root_logger

LOG = logging.getLogger('test_fb_pdnstools')

# =============================================================================
class TestPdnsToolsBase(FbPdnsToolsTestcase):
    """Testcase for tests on fb_pdnstools/__init__.py and fb_pdnstools.errors."""

    # -------------------------------------------------------------------------
    def test_import_modules(self):
        """Testing import of modules fb_pdnstools/__init__.py and fb_pdnstools.errors ..."""
        LOG.info('Test importing main module ...')

        LOG.debug('Importing fb_pdnstools ...')
        import fb_pdnstools

        LOG.debug('Version of fb_pdnstools: {!r}.'.format(fb_pdnstools.__version__))

        LOG.debug('Importing fb_pdnstools.errors ...')
        import fb_pdnstools.errors
        LOG.debug('Version of fb_pdnstools.errors: {!r}.'.format(
            fb_pdnstools.errors.__version__))

    # -------------------------------------------------------------------------
    def test_error_classes(self):
        """Test instantiating of different exception classes in fb_pdnstools.errors."""
        LOG.info('Testing loading and function of different error handler classes ...')

        LOG.debug('Testing PowerDNSHandlerError from fb_pdnstools.errors ...')
        from fb_pdnstools.errors import PowerDNSHandlerError
        with self.assertRaises(PowerDNSHandlerError) as cm:
            raise PowerDNSHandlerError('bla')
        e = cm.exception
        LOG.debug('Got a {c}: {e}'.format(c=e.__class__.__name__, e=e))

        LOG.debug('Testing PowerDNSZoneError from fb_pdnstools.errors ...')
        from fb_pdnstools.errors import PowerDNSZoneError
        with self.assertRaises(PowerDNSZoneError) as cm:
            raise PowerDNSZoneError('bla')
        e = cm.exception
        LOG.debug('Got a {c}: {e}'.format(c=e.__class__.__name__, e=e))

        LOG.debug('Testing PowerDNSRecordError from fb_pdnstools.errors ...')
        from fb_pdnstools.errors import PowerDNSRecordError
        with self.assertRaises(PowerDNSRecordError) as cm:
            raise PowerDNSRecordError('bla')
        e = cm.exception
        LOG.debug('Got a {c}: {e}'.format(c=e.__class__.__name__, e=e))

        LOG.debug('Testing PowerDNSRecordSetError from fb_pdnstools.errors ...')
        from fb_pdnstools.errors import PowerDNSRecordSetError
        with self.assertRaises(PowerDNSRecordSetError) as cm:
            raise PowerDNSRecordSetError('bla')
        e = cm.exception
        LOG.debug('Got a {c}: {e}'.format(c=e.__class__.__name__, e=e))

        LOG.debug('Testing PowerDNSWrongSoaDataError from fb_pdnstools.errors ...')
        from fb_pdnstools.errors import PowerDNSWrongSoaDataError
        with self.assertRaises(PowerDNSWrongSoaDataError) as cm:
            raise PowerDNSWrongSoaDataError({'uhu': 'banane'})
        e = cm.exception
        LOG.debug('Got a {c}: {e}'.format(c=e.__class__.__name__, e=e))

        msg = 'Undfined test error'

        LOG.debug('Testing PDNSApiError from fb_pdnstools.errors ...')
        from fb_pdnstools.errors import PDNSApiError
        uris = (None, 'https://pdnsmaster.uhu-banane.eu')
        code = 404
        for uri in uris:
            with self.assertRaises(PDNSApiError) as cm:
                raise PDNSApiError(code, msg, uri)
            e = cm.exception
            LOG.debug('Got a {c}: {e}'.format(c=e.__class__.__name__, e=e))

        LOG.debug('Testing PDNSApiNotAuthorizedError from fb_pdnstools.errors ...')
        from fb_pdnstools.errors import PDNSApiNotAuthorizedError
        with self.assertRaises(PDNSApiNotAuthorizedError) as cm:
            raise PDNSApiNotAuthorizedError(401, msg)
        e = cm.exception
        LOG.debug('Got a {c}: {e}'.format(c=e.__class__.__name__, e=e))

        LOG.debug('Testing PDNSApiNotFoundError from fb_pdnstools.errors ...')
        from fb_pdnstools.errors import PDNSApiNotFoundError
        with self.assertRaises(PDNSApiNotFoundError) as cm:
            raise PDNSApiNotFoundError(404, msg)
        e = cm.exception
        LOG.debug('Got a {c}: {e}'.format(c=e.__class__.__name__, e=e))

        LOG.debug('Testing PDNSApiValidationError from fb_pdnstools.errors ...')
        from fb_pdnstools.errors import PDNSApiValidationError
        with self.assertRaises(PDNSApiValidationError) as cm:
            raise PDNSApiValidationError(422, msg, 'https://pdnsmaster.uhu-banane.eu')
        e = cm.exception
        LOG.debug('Got a {c}: {e}'.format(c=e.__class__.__name__, e=e))

        LOG.debug('Testing PDNSApiRateLimitExceededError from fb_pdnstools.errors ...')
        from fb_pdnstools.errors import PDNSApiRateLimitExceededError
        with self.assertRaises(PDNSApiRateLimitExceededError) as cm:
            raise PDNSApiRateLimitExceededError(429, msg)
        e = cm.exception
        LOG.debug('Got a {c}: {e}'.format(c=e.__class__.__name__, e=e))

        LOG.debug('Testing PDNSApiRequestError from fb_pdnstools.errors ...')
        from fb_pdnstools.errors import PDNSApiRequestError
        with self.assertRaises(PDNSApiRequestError) as cm:
            raise PDNSApiRequestError(419, 'I am not a tea pot.')
        e = cm.exception
        LOG.debug('Got a {c}: {e}'.format(c=e.__class__.__name__, e=e))

# =============================================================================
if __name__ == '__main__':

    verbose = get_arg_verbose()
    if verbose is None:
        verbose = 0
    init_root_logger(verbose)

    LOG.info('Starting tests ...')

    loader = unittest.TestLoader()
    suite = unittest.TestSuite()

    suite.addTest(TestPdnsToolsBase('test_import_modules', verbose))
    suite.addTest(TestPdnsToolsBase('test_error_classes', verbose))

    runner = unittest.TextTestRunner(verbosity=verbose)

    result = runner.run(suite)

# =============================================================================

# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list


import matplotlib.pyplot as plt
import numpy as np
import numpy.typing as npt
import seaborn as sns

import pyssht as ssht

import sleplet

sns.set(context="paper")

L = 16
SAMPLING_SCHEME = "MWSS"
THETA_MAX = 40

def main() -> None:
    """Create a plot of Slepian coefficients against rank."""
    region = sleplet.slepian.Region(theta_max=np.deg2rad(THETA_MAX))
    earth = sleplet.functions.Earth(L, region=region)
    field = ssht.inverse(earth.coefficients, L, Method=SAMPLING_SCHEME)
    integrate_region = _helper_region(L, region, field, earth.coefficients)
    integrate_sphere = _helper_sphere(L, region, field, earth.coefficients)
    N = sleplet.slepian.SlepianPolarCap(L, np.deg2rad(THETA_MAX)).N
    ax = plt.gca()
    sns.scatterplot(
        x=range(N),
        y=integrate_region,
        ax=ax,
        label="region",
        linewidth=0,
        marker=".",
    )
    sns.scatterplot(
        x=range(N),
        y=integrate_sphere,
        ax=ax,
        label="sphere",
        linewidth=0,
        marker="*",
    )
    ax.set_xlabel("coefficients")
    ax.set_ylabel("relative error")
    ax.set_yscale("log")
    print(f"Opening: fp_error_earth_polar{THETA_MAX}_L{L}")
    plt.tight_layout()
    plt.show(block=False)
    plt.pause(10)
    plt.close()

def _helper_sphere(
    L: int,
    region: sleplet.slepian.Region,
    f: npt.NDArray[np.complex_],
    flm: npt.NDArray[np.complex_ | np.float_],
) -> npt.NDArray[np.float_]:
    """Calculate the difference in Slepian coefficients by integration of the sphere."""
    slepian = sleplet.slepian_methods.choose_slepian_method(L, region)
    output = np.abs(sleplet.slepian_methods.slepian_forward(L, slepian, f=f))
    desired = np.abs(sleplet.slepian_methods.slepian_forward(L, slepian, flm=flm))
    return np.abs(output - desired) / desired

def _helper_region(
    L: int,
    region: sleplet.slepian.Region,
    f: npt.NDArray[np.complex_],
    flm: npt.NDArray[np.complex_ | np.float_],
) -> npt.NDArray[np.float_]:
    """Calculate the difference in Slepian coefficients by integration of the region."""
    slepian = sleplet.slepian_methods.choose_slepian_method(L, region)
    output = np.abs(
        sleplet.slepian_methods.slepian_forward(L, slepian, f=f, mask=slepian.mask),
    )
    desired = np.abs(sleplet.slepian_methods.slepian_forward(L, slepian, flm=flm))
    return np.abs(output - desired) / desired

if __name__ == "__main__":
    main()