from rest_framework import permissions
from rest_framework import viewsets, mixins

from staff_profile_app.models import StaffProfile
from staff_profile_app.serializers import StaffProfileSerializer
from student_profile_app.serializers import StudentProfessionSerializer, StudentProfileSerializer, FieldReportSerializer
from alumni_profile_app.serializers import AlumniProfileSerializer, AlumniProfessionSerializer, PublishedAlumniSerializer
from student_profile_app.models import StudentProfession, StudentProfile, FieldReport
from alumni_profile_app.models import AlumniProfile, AlumniProfession
from program_app.serializers import ProgramSerializer
from program_app.models import Program
from field_post_app.models import FieldApplication, FieldPost, FieldPostProfession, FieldPostProgram
from field_post_app.serializers import FieldPostProfessionSerializer, FieldApplicationSerializer, FieldPostSerializer, FieldPostProgramSerializer
from internship_post_app.models import InternshipApplication, InternshipPost, InterviewSchedule
from internship_post_app.serializers import InternshipPostSerializer, InternshipApplicationSerializer, InterviewScheduleSerializer
from .models import UserProfile
from .serializers import UserProfileSerializer
from announcement_app.models import Announcement
from announcement_app.serializers import AnnouncementSerializer
from project_app.models import ProjectMember
from project_app.serializers import ProjectMemberSerializer
from questions_app.models import (MultipleChoice, ApplicantScore,
    ApplicantAnswer, Question)
from questions_app.serializers import (MultipleChoiceSerializer,
    ApplicantScoreSerializer, ApplicantAnswerSerializer,
    QuestionSerializer)
from organization_address_app.serializers import (ContractSerializer,
    OrganizationProfileSerializer, InvitationsSerializer)
from organization_address_app.models import Contract, OrganizationProfile, Invitations
from cv_app.serializers import (PersonalInformationSerializer,
    EducationInformationSerializer, ExperienceInformationSerializer)
from cv_app.models import (PersonalInformation, EducationInformation,
    ExperienceInformation)
from certificate_app.models import Certificate
from certificate_app.serializers import CertificateSerializer

class InterviewScheduleByPostViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = InterviewScheduleSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        post_id = self.kwargs.get('postId')
        return InterviewSchedule.objects.filter(post=post_id)

class QuestionChoicesViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = MultipleChoiceSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        question_id = self.kwargs.get('questionId')
        return MultipleChoice.objects.filter(question=question_id)

class QuestionsByProfessionsViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = QuestionSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        profession_id = self.kwargs.get('professionId')
        return Question.objects.filter(profession=profession_id)

class ApplicantMarksViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = ApplicantScoreSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        alumni_id = self.kwargs.get('alumniId')
        post_id = self.kwargs.get('postId')
        return ApplicantScore.objects.filter(alumni=alumni_id, post=post_id)

class ApplicantAnswersViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = ApplicantAnswerSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        alumni_id = self.kwargs.get('alumniId')
        post_id = self.kwargs.get('postId')
        return ApplicantAnswer.objects.filter(alumni=alumni_id, post=post_id)

class ProjectsByMemberViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = ProjectMemberSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        memberId = self.kwargs.get('memberId')
        return ProjectMember.objects.filter(member=memberId)

class ProjectMembersViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = ProjectMemberSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        projectId = self.kwargs.get('projectId')
        return ProjectMember.objects.filter(project=projectId)

class AnnouncementsByDesignationsViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = AnnouncementSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        designation_id = self.kwargs.get('designationId')
        return Announcement.objects.filter(destination=designation_id)

class UsersProfilesByDesignationIdViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = UserProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        designation_id = self.kwargs.get('designationId')
        return UserProfile.objects.filter(designation=designation_id)

class SingleUserProfileViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = UserProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        user_id = self.kwargs.get('userId')
        return UserProfile.objects.filter(user=user_id)

class StudentProfessionViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = StudentProfessionSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        student_id = self.kwargs.get('studentId')
        return StudentProfession.objects.filter(student=student_id)

class StudentReportViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = FieldReportSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        student_id = self.kwargs.get('studentId')
        return FieldReport.objects.filter(student=student_id)

class ProgramsByDepartmentViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = ProgramSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        department_id = self.kwargs.get('departmentId')
        return Program.objects.filter(department=department_id)

class StudentsByProgramViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = StudentProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        program_id = self.kwargs.get('programId')
        return StudentProfile.objects.filter(program=program_id)

class StudentsByOrganizationViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = StudentProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        organization_id = self.kwargs.get('organizationId')
        return StudentProfile.objects.filter(organization=organization_id)

class SingleOrganizationProfileViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = OrganizationProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        organization_id = self.kwargs.get('organizationId')
        return OrganizationProfile.objects.filter(organization_id=organization_id)

class StudentsByDepartmentViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = StudentProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        department_id = self.kwargs.get('departmentId')
        return StudentProfile.objects.filter(department=department_id)

class StudentsByAcademicSpervisorViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = StudentProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        supervisor_id = self.kwargs.get('supervisorId')
        return StudentProfile.objects.filter(academic_supervisor=supervisor_id)

class StudentsByFieldSpervisorViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = StudentProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        supervisor_id = self.kwargs.get('supervisorId')
        return StudentProfile.objects.filter(field_supervisor=supervisor_id)

class StudentsByStatusViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = StudentProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        status = self.kwargs.get('status')
        return StudentProfile.objects.filter(student_status=status)

class StudentsByYearOfStudyViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = StudentProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        year = self.kwargs.get('year')
        return StudentProfile.objects.filter(year_of_study=year)

class ReportedStudentsViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = FieldApplicationSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        return FieldApplication.objects.filter(has_reported=True)

class ReportedStudentsProfilesViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = StudentProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        return StudentProfile.objects.filter(has_reported=True)

class SingleStudentProfileViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = StudentProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        student_id = self.kwargs.get('studentId')
        return StudentProfile.objects.filter(student=student_id)

class AlumniProfessionViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = AlumniProfessionSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        alumni_id = self.kwargs.get('alumniId')
        return AlumniProfession.objects.filter(alumni=alumni_id)

class PubllishedAlumniViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = PublishedAlumniSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        return AlumniProfile.objects.filter(is_public=True)

class CvPersonalInformationViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = PersonalInformationSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        alumni_id = self.kwargs.get('alumniId')
        return PersonalInformation.objects.filter(alumni=alumni_id)

class CvEducationInformationViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = EducationInformationSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        alumni_id = self.kwargs.get('alumniId')
        return EducationInformation.objects.filter(alumni=alumni_id)

class CvExperienceInformationViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = ExperienceInformationSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        alumni_id = self.kwargs.get('alumniId')
        return ExperienceInformation.objects.filter(alumni=alumni_id)

class AlumniCertificatesViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = CertificateSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        alumni_id = self.kwargs.get('alumniId')
        return Certificate.objects.filter(alumni=alumni_id)

class SingleAlumniProfileViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = AlumniProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        alumni_id = self.kwargs.get('alumniId')
        return AlumniProfile.objects.filter(alumni=alumni_id)

class SingleStaffProfileViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = StaffProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        staff_id = self.kwargs.get('staffId')
        return StaffProfile.objects.filter(staff=staff_id)

class OrganizationContractsViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = ContractSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        organization_id = self.kwargs.get('organizationId')
        return Contract.objects.filter(organization=organization_id)

class AlumniByOrganizationViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = AlumniProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        organization_id = self.kwargs.get('organizationId')
        return AlumniProfile.objects.filter(organization=organization_id)

class AlumniByCompletionYearViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = AlumniProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        year = self.kwargs.get('year')
        return AlumniProfile.objects.filter(completion_year=year)

class AlumniByDegreeProgramViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = AlumniProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        program_id = self.kwargs.get('programId')
        return AlumniProfile.objects.filter(program=program_id)

class AlumniByDepartmentViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = AlumniProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        department_id = self.kwargs.get('departmentId')
        return AlumniProfile.objects.filter(department=department_id)

class FieldPostByOrganizationViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = FieldPostSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        organization_id = self.kwargs.get('organizationId')
        return FieldPost.objects.filter(organization=organization_id)

class FieldProfessionByPostViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = FieldPostProfessionSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        post_id = self.kwargs.get('postId')
        return FieldPostProfession.objects.filter(post=post_id)

class FieldProgramByPostViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = FieldPostProgramSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        post_id = self.kwargs.get('postId')
        return FieldPostProgram.objects.filter(post=post_id)

class FieldApplicationByPostViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = FieldApplicationSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        post_id = self.kwargs.get('postId')
        return FieldApplication.objects.filter(post=post_id)

class FieldApplicationByStudentViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = FieldApplicationSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        student_id = self.kwargs.get('studentId')
        return FieldApplication.objects.filter(student=student_id)

class InternshipPostByOrganizationViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = InternshipPostSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        organization_id = self.kwargs.get('organizationId')
        return InternshipPost.objects.filter(organization=organization_id)

class InternshipApplicationByOrganizationViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = InternshipApplicationSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        organization_id = self.kwargs.get('organizationId')
        return InternshipApplication.objects.filter(organization=organization_id, status="accepted")

class InternshipApplicationByPostViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = InternshipApplicationSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        post_id = self.kwargs.get('postId')
        return InternshipApplication.objects.filter(post=post_id)

class InternshipApplicationByAlumniViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = InternshipApplicationSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        alumni_id = self.kwargs.get('alumniId')
        return InternshipApplication.objects.filter(alumni=alumni_id)

class AlumniByStatusViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = AlumniProfileSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        status = self.kwargs.get('status')
        return AlumniProfile.objects.filter(is_active=status)

class AlumniInvitationsViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = InvitationsSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        alumni_id = self.kwargs.get('alumniId')
        return Invitations.objects.filter(alumni=alumni_id)

class OrganizationInvitationsViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    serializer_class = InvitationsSerializer
    permission_classes = [
        permissions.IsAuthenticated ]

    def get_queryset(self):
        organization_id = self.kwargs.get('organizationId')
        return Invitations.objects.filter(organization=organization_id)


import krogon.either as E
import json
from krogon import run_in_cluster,  gke_conn
from krogon.k8s import cron_job
from tests.helpers.mock_file_system import find_write_template_calls, MockFileSystem
from tests.helpers.mock_os_system import MockOsSystem
from base64 import b64encode
from tests.helpers.entry_mocks import mock_krogon_dsl

def test_can_generate_cron_job_template():
    def _run_dsl(args):
        os: MockOsSystem = args["os_system"]
        fs: MockFileSystem = args['file_system']
        project_id = "project1"
        job_name = 'test-service'
        cluster_name = 'prod-us-east1'
        kubectl_version = "v1.15.3"
        image_url = "test-service:1.0.0"
        service_account_b64 = b64encode(json.dumps({'key': 'someKey'}).encode('utf-8'))

        os.mock_clusters_list([cluster_name])
        os.mock_kubernetes_release(E.success(kubectl_version))
        os.mock_download_install_kubectl(kubectl_version, E.success())
        os.mock_create_kube_config(cluster_name, E.success())
        os.mock_kubectl_apply_temp_file(cluster_name, E.success())

        _, result = run_in_cluster(
            conn=gke_conn(cluster_name, project_id, service_account_b64),
            templates=[
                cron_job(job_name, image_url)
            ]
        )

        yamls = find_write_template_calls(fs)
        assert yamls[0]['kind'] == 'CronJob'
        assert yamls[0]['metadata']['name'] == job_name
        container = yamls[0]['spec']['jobTemplate']['spec']['template']['spec']['containers'][0]
        assert container['name'] == job_name
        assert container['image'] == image_url

    mock_krogon_dsl(_run_dsl)

def test_can_set_secret_for_job():
    def _run_dsl(args):
        fs: MockFileSystem = args['file_system']
        os: MockOsSystem = args["os_system"]
        project_id = "project1"
        job_name = 'test-service'
        cluster_name = 'prod-us-east1'
        kubectl_version = "v1.15.3"
        image_url = "test-service:1.0.0"
        service_account_b64 = b64encode(json.dumps({'key': 'someKey'}).encode('utf-8'))

        os.mock_clusters_list([cluster_name])
        os.mock_kubernetes_release(E.success(kubectl_version))
        os.mock_download_install_kubectl(kubectl_version, E.success())
        os.mock_create_kube_config(cluster_name, E.success())
        os.mock_kubectl_apply_temp_file(cluster_name, E.success())

        _, result = run_in_cluster(
            conn=gke_conn(cluster_name, project_id, service_account_b64),
            templates=[
                cron_job(job_name, image_url)
                    .with_environment_secret('coolSecret', {'ENV_NAME': 'secretkey'})
            ]
        )

        yamls = find_write_template_calls(fs)
        container = yamls[0]['spec']['jobTemplate']['spec']['template']['spec']['containers'][0]
        assert container['env'][0]['name'] == 'ENV_NAME'
        assert container['env'][0]['valueFrom']['secretKeyRef']['name'] == 'coolSecret'
        assert container['env'][0]['valueFrom']['secretKeyRef']['key'] == 'secretkey'

    mock_krogon_dsl(_run_dsl)


import math
from fractions import Fraction

def Pt2(a,b,c):
    while True:
        if a == 0 and b == 0:
            print("Mot trong so a, b phai khac khong.")
            a = float(input("Nhap so a: "))
            b = float(input("Nhap so b: "))
        else:
            delta = b**2 - 4 * a * c

            if delta < 0:
                print("Phuong trinh vo nghiem.")
                return ""
            elif delta == 0:
                print(f"x1 va x2 co nghiem kep la x1 = x2 = {-b / (2 * a)}.")
                return ""
            else:
                print("x1 va x2 co hai nghiem rieng biet lan luot la:")
                print(f"Nghiem cua a la {Fraction((-b + math.sqrt(delta))/(2*a))}")
                print(f"Nghiem cua b la {Fraction((-b - math.sqrt(delta))/(2*a))}")
                return ""


import os
from pdf2image import convert_from_path
from pathlib import Path

def convert_file(before_dir, after_dir, file_name):
    file_name = file_name.replace("/", "")
    input_pdf = Path(f"{before_dir}/{file_name}")
    images = convert_from_path(input_pdf)

    output_path = Path(f"{after_dir}/{file_name}")
    output_pdf = images[0].convert("RGB")
    output_pdf.save(output_path, save_all=True, append_images=images[1:])

def main():
    current_dir = os.getcwd()

    try:
        before_dir = os.mkdir(current_dir+"/PDF_ANTES")
    except:
        before_dir = current_dir+"/PDF_ANTES"

    try:
        after_dir = os.mkdir(current_dir+"/PDF_DEPOIS")
    except:
        after_dir = current_dir+"/PDF_DEPOIS"

    dir_list = os.listdir(current_dir+"/PDF_ANTES")

    for file_name in dir_list:
        print("Convertendo... "+file_name)
        convert_file(before_dir, after_dir, file_name)

main()


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

# In[1]:
## Ranking proteins by stepwise forward selection, bootrapped forward and bootstrapped L1.

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, KFold
import statsmodels.api as sm
from sklearn.metrics import accuracy_score, roc_auc_score, recall_score, precision_score, confusion_matrix, log_loss
from sklearn.metrics import precision_recall_curve, roc_curve
from sklearn.linear_model import LogisticRegression, LogisticRegressionCV
from sklearn.preprocessing import QuantileTransformer, PowerTransformer
from sklearn.feature_selection import SelectKBest, SelectFromModel, RFE, RFECV, SelectPercentile, SelectFpr, SelectFdr, SelectFwe
from sklearn.feature_selection import chi2, f_classif, mutual_info_classif, SelectFdr
from sklearn.ensemble import RandomForestClassifier
from boruta import BorutaPy
import matplotlib.pyplot as plt
import pickle
from sklearn.utils import resample
# import lifelines as ll
# from lifelines.utils.sklearn_adapter import sklearn_adapter
# CoxRegression = sklearn_adapter(ll.CoxPHFitter, event_col = 'event')
import sys
sys.path.append('/odinn/users/thjodbjorge/Python_functions/')
import Predict_functions as pf
from Calculate_score import calculate_metrics

raw_data = pd.read_csv('/odinn/users/thjodbjorge/Proteomics/Data/raw_with_info.csv',index_col = 'Barcode2d' )
probe_info = pd.read_csv('/odinn/users/thjodbjorge/Proteomics/Data/probe_info.csv', index_col = 'SeqId')

pn_info = pd.read_csv('/odinn/users/thjodbjorge/Proteomics/Data/pn_info_Mor/pn_info_Mor_event.csv',index_col = 'Barcode2d' )
probes_to_skip = pd.read_csv('/odinn/users/thjodbjorge/Proteomics/Data/probes_to_skip.txt')['probe']
nopro = pd.read_csv('/odinn/users/thjodbjorge/Proteomics/Data/no_protein_probes.txt', header = None)[0] # non-proten probes that were included
probes_to_skip = set(probes_to_skip).union(set(nopro))

folder = '/odinn/users/thjodbjorge/Proteomics/Mortality2/'
feat_folder = 'Features2/'
pred_folder = 'Predictions3/'

endpoints = ['death']
# endpoints = ['death','Cdeath','Gdeath','Ideath','Jdeath','Otherdeath']
# event_date = event_date_death
time_to_event = pn_info.time_to_death
no_event_before = pn_info.no_death_before
for endpoint in endpoints:
    if endpoint == 'death':
        use_event = pn_info.event_death
        print(use_event.sum())
    elif endpoint == 'Cdeath':
        use_event = pn_info.event_death & (pn_info.ICD_group == 'C')
        print(use_event.sum())
    elif endpoint == 'Gdeath':
        use_event = pn_info.event_death & (pn_info.ICD_group == 'G')
        print(use_event.sum())
    elif endpoint == 'Ideath':
        use_event = pn_info.event_death & (pn_info.ICD_group == 'I')
        print(use_event.sum())
    elif endpoint == 'Jdeath':
        use_event = pn_info.event_death & (pn_info.ICD_group == 'J')
        print(use_event.sum())
    elif endpoint == 'Otherdeath':
        use_event = pn_info.event_death & (~(pn_info.ICD_group == 'C')&~(pn_info.ICD_group == 'G')&~(pn_info.ICD_group == 'I')&~(pn_info.ICD_group == 'J'))
        print(use_event.sum())

y = []
for i in range(1,19):
    y.append(use_event & (time_to_event <= i))

kf = KFold(n_splits=10, random_state=10, shuffle=False)
I_train_main, I_test_main = train_test_split(pn_info.index, train_size=0.7, random_state = 10)
I_val_main, I_test_main = train_test_split(I_test_main, train_size=0.5, random_state = 10)

file = open(folder+"{}_keep_samples.pkl".format('Mor'),'rb')
keep_samples_dict = pickle.load(file)

print(keep_samples_dict.keys())
# keep_samples_keys = ['Old_18105', 'Old_60105', 'Old_6080','Old_18105_C', 'Old_18105_I', 'Old_18105_J', 'Old_18105_G','Old_18105_Other']
keep_samples_keys = ['Old_60105','Old_18105']#,'Old_6080']
# keep_samples_keys = ['Old_18105_Neoplasms','Old_18105_I','Old_18105_J','Old_18105_G','Old_18105_Other']
# keep_samples_keys = ['Old_18105_J','Old_18105_G','Old_18105_Other']
# keep_samples_keys = ['Old_60105']
# keep_samples_keys = ['Old_men_18105','Old_women_18105']

# K = [4, 9]
# K =[14]
# K = [4,9,0,1,2,3,5,6,7,8,10,11,12,13,14]
# K = [9,10,11,12,13,14]
K = [4,9]
for dataset in keep_samples_keys:
    print(dataset)
    keep_samples = keep_samples_dict[dataset]

    I_train = I_train_main.intersection(keep_samples)#.intersection(have_prs)
    I_test = I_val_main.intersection(keep_samples)#.intersection(have_prs)

    print('Training set: {}, MI within 15: {}, 10: {}, 5: {}, 2: {}'.format(len(I_train),y[14][I_train].sum(),y[9][I_train].sum(),y[4][I_train].sum(),y[1][I_train].sum()))
    print('Test set: {}, MI within 15: {}, 10: {}, 5: {}, 2: {}'.format(len(I_test),y[14][I_test].sum(),y[9][I_test].sum(),y[4][I_test].sum(),y[1][I_test].sum()))

        # ### Select data and normalize
    X = np.log(raw_data.iloc[:,16:].drop(probes_to_skip,axis=1))

    all_protein = X.columns
    X['sex'] = pn_info[['sex']].values-1
    X['age'] = pn_info[['Age_at_sample_collection_2']].values

    X['age2'] = X['age']**2
#     X['age3'] = X['age']**3
    X['agesex'] = X['age']*X['sex']
    X['age2sex'] = X['age2']*X['sex']

    agesex = ['age','sex','agesex','age2','age2sex']

    X['lnage'] = np.log(X['age'])
    X['lnage2'] = X['lnage']**2

    X['PAD'] = pn_info['PAD']
    no_bmi = (X['PAD'].isna())
    no_bmi_ind = X[no_bmi].index
    X.loc[I_train.intersection(no_bmi_ind),'PAD'] = X.loc[I_train].PAD.mean()
    X.loc[I_test.intersection(no_bmi_ind),'PAD'] = X.loc[I_test].PAD.mean()

    X['CAD'] = ~pn_info.no_CAD_before
    X['MI'] = ~pn_info.no_MI_before
    X['cancer'] = pn_info.cancer_margin
    X['ApoB'] = X['SeqId.2797-56']
    X['Smoker'] = pn_info['Smoker'].astype(int).values
    X['diabetes'] = pn_info['T2D'].astype(int).values
    X['HTN_treated'] = pn_info[['HTN_treated']].astype(int).values
#     X['statin'] = pn_info['statin'].astype(int).values
    X['statin'] = pn_info['statin_estimate_unsure'].astype(int).values
    X['ApoBstatin']  = X['ApoB']*X['statin']

    X['cancer1y']  = pn_info['cancer1y']
    X['cancer5y']  = pn_info['cancer5y']

    X['GDF15'] = X['SeqId.4374-45'].copy()
    X['GDF152'] = X['GDF15']**2
    X['GDF15age']  = X['GDF15']*X['age']
    X['GDF15sex']  = X['GDF15']*X['sex']

    X['bmi'] = pn_info['bmi']

    no_bmi = (X['bmi'].isna())
    no_bmi_ind = X[no_bmi].index
#     X.loc[no_bmi_ind,'bmi'] = X.loc[I_train].bmi.mean()
    X.loc[I_train.intersection(no_bmi_ind),'bmi'] = X.loc[I_train].bmi.mean()
    X.loc[I_test.intersection(no_bmi_ind),'bmi'] = X.loc[I_test].bmi.mean()

    X['bmi2'] = X['bmi']*X['bmi']

    X['Platelets'] = pn_info['Platelets']
    no_p = (X['Platelets'].isna()); print(no_p.sum())
    no_p_ind = X[no_p].index
    X.loc[I_train.intersection(no_p_ind),'Platelets'] = X.loc[I_train].Platelets.mean()
    X.loc[I_test.intersection(no_p_ind),'Platelets'] = X.loc[I_test].Platelets.mean()
    X['Platelets2'] = X['Platelets']*X['Platelets']

    X['Creatinine'] = pn_info['Creatinine']
    no_p = (X['Creatinine'].isna()); print(no_p.sum())
    no_p_ind = X[no_p].index
    X.loc[I_train.intersection(no_p_ind),'Creatinine'] = X.loc[I_train].Creatinine.mean()
    X.loc[I_test.intersection(no_p_ind),'Creatinine'] = X.loc[I_test].Creatinine.mean()

    X['Triglycerides'] = pn_info['Triglycerides']
    no_p = (X['Triglycerides'].isna()); print(no_p.sum())
    no_p_ind = X[no_p].index
    X.loc[I_train.intersection(no_p_ind),'Triglycerides'] = X.loc[I_train].Triglycerides.mean()
    X.loc[I_test.intersection(no_p_ind),'Triglycerides'] = X.loc[I_test].Triglycerides.mean()

    X['bmiage'] = X['bmi']*X['age']
    X['bmisex'] = X['bmi']*X['sex']
    X['bmi2age'] = X['bmi2']*X['age']
    X['bmi2sex'] = X['bmi2']*X['sex']
    X['PADage'] = X['PAD']*X['age']
    X['PADsex'] = X['PAD']*X['sex']

    X['ApoBage']  = X['ApoB']*X['age']
    X['Smokerage'] = X['Smoker']*X['age']
    X['diabetesage'] = X['diabetes']*X['age']
    X['statinage'] = X['statin']*X['age']
    X['CADage'] = X['CAD']*X['age']
    X['MIage'] = X['MI'] * X['age']
    X['HTN_treatedage'] =  X['age']*X['HTN_treated']
    X['cancerage'] = X['age']*X['cancer']

    X['Plateletsage'] = X['Platelets']*X['age']
    X['Creatinineage'] = X['Creatinine']*X['age']
    X['Triglyceridesage'] = X['Triglycerides']*X['age']
    X['Platelets2age'] = X['Platelets2']*X['age']

    X['cancer1yage'] = X['cancer1y']*X['age']
    X['cancer5yage'] = X['cancer5y']*X['age']

    X['ApoBsex']  = X['ApoB']*X['sex']
    X['Smokersex'] = X['Smoker']*X['sex']
    X['diabetessex'] = X['diabetes']*X['sex']
    X['statinsex'] = X['statin']*X['sex']
    X['CADsex'] = X['CAD']*X['sex']
    X['MIsex'] = X['MI'] * X['sex']
    X['HTN_treatedsex'] =  X['sex']*X['HTN_treated']
    X['cancersex'] = X['sex']*X['cancer']

    X['Plateletssex'] = X['Platelets']*X['sex']
    X['Creatininesex'] = X['Creatinine']*X['sex']
    X['Triglyceridessex'] = X['Triglycerides']*X['sex']

    X = X.join(pd.get_dummies(pn_info['agebin'],drop_first = True,prefix='age'))
    X['ageage2'] = X['age']*X['age_2.0']
    X['ageage3'] = X['age']*X['age_3.0']
    X['ageage4'] = X['age']*X['age_4.0']

    agebins = ['age_2.0','age_3.0','age_4.0', 'ageage2','ageage3','ageage4']
    agebinssex = [s+'sex' for s in agebins]
    X[agebinssex] = (X[agebins].transpose()*X['sex']).transpose()

    PRS = ['nonHDL_prs', 'HT_prs', 'CAD_prs', 'Cancer_prs', 'Stroke2_prs', 'alz_Jansen',
       'pgc_adhd_2017', 'PD_Nalls_2018', 'edu_160125', 'dep_2018', 'bpd_2018',
       'giant_bmi', 'schizo_clozuk', 'iq_2018', 'ipsych_pgc_aut_2017',
       'pgc_Anorexia_2019']
    X[PRS] = pn_info[PRS]

    trad = ['ApoB','Smoker','diabetes','HTN_treated','statin','CAD','MI','bmi','bmi2']
    tradage = ['ApoBage','Smokerage','diabetesage','CADage','MIage','HTN_treatedage','bmiage']
    tradsex = ['ApoBsex','Smokersex','diabetessex','CADsex','MIsex','HTN_treatedsex','bmisex']

    tradcoxR = ['Smoker','Smokersex','diabetes','diabetesage','HTN_treated','HTN_treatedage','MI','MIage','CAD','bmi','bmiage','statin','statinage']
    tradextralog = ['Smokersex','diabetessex','CADsex','CADage','MIage','HTN_treatedage','bmiage','statinage','bmi2age','ApoBstatin']
    tradextralognosex = ['CADage','MIage','HTN_treatedage','bmiage']
    tradblood = ['Creatinine','Triglycerides','Platelets','Platelets2','Plateletsage','Creatinineage','Platelets2age']
    tradcancer = ['cancer','cancerage']
    extra_cancer = ['cancer1y','cancer5y','cancer1yage','cancer5yage']

    X_train = X.loc[I_train]
    X_test = X.loc[I_test]

    train_mean = X_train.mean()
    train_std = X_train.std()

    X_train = (X_train-train_mean)/train_std
    X_test = (X_test-train_mean)/train_std

        ## For survival analysis
    X_train['event'] = use_event[I_train]
    X_test['event'] = use_event[I_test]

    tte_train = time_to_event[I_train]
    tte_test = time_to_event[I_test]

    ysurv_train = pd.DataFrame()
    ysurv_train['event'] = use_event[I_train]
    ysurv_train['time_to_event'] = time_to_event[I_train]

    for k in K:
        y_train = y[k][I_train]
        y_test = y[k][I_test]

        ycsurv_train = pd.DataFrame()
        ycsurv_train['event'] = y_train
        ycsurv_train['time_to_event'] = np.where(tte_train<= (k+1),tte_train,k+1)

        X_train['y'] = y_train
        X_test['y'] = y_test

        try:

            file = open(folder+feat_folder+"{}_{}_features.pkl".format(endpoint,dataset),'rb')
            features_dict = pickle.load(file)

            boruta = sorted(features_dict['{}_boruta_y{}'.format(dataset,k)])
            boruta.remove('SeqId.4374-45')
            boruta.remove('SeqId.11388-75')
            print('features_loaded')

        except Exception as e:
            print(e)
            print('No features')

        try:
            file = open(folder+pred_folder+"{}_{}_forward_noGDF15HE4.pkl".format(endpoint,dataset),'rb')
            pred_dict = pickle.load(file)
        except:
            pred_dict = {}

        ## Forward feature selection
        if 1:
            np.random.seed(10)
            boot_protein_order = [boruta]
            for b in range(1):
    #             ind = resample(I_train,replace=True)
                ind = I_train
                y_use = y_train[ind]
                use_const = []
                use_const.extend(agesex )
#                 use_const = ['age','age2']

                feat = []
                feat.extend(boruta)
    #             num_pro = len(feat)
                num_pro = 100
                protein_order = []
                for i in range(num_pro):#len(protein)+1):
                    mmp_pred_dict = {}
                    candidates = feat
                    print(len(candidates))
                    for c in candidates:
                        use = use_const + [c]
                        X_use = X_train.loc[ind,use]
                        model = sm.Logit(y_use,sm.add_constant(X_use))
                        res = model.fit(disp=0)

    #                     mmp_pred_dict[c] = [res.pvalues[-1]]
                        mmp_pred_dict[c] = [res.llf]

                    mmp_pred = pd.DataFrame.from_dict(mmp_pred_dict).transpose()
                    mmp_pred.columns = ['log_likelihood']

                    best_candidate = [mmp_pred.sort_values('log_likelihood', ascending = False).iloc[0,:].name]
                    feat.remove(best_candidate[0])
                    protein_order.append([best_candidate[0],mmp_pred.sort_values('log_likelihood', ascending = False).iloc[0,0]])
                    use_const = use_const + best_candidate

    #             boot_protein_order.append(pd.DataFrame(protein_order).sort_values(0).index)
                boot_protein_order.append(protein_order)
            pred_dict['{}_y{}_asprotein_lr'.format(dataset,k)] = boot_protein_order

            f = open(folder+pred_folder+"{}_{}_forward_noGDF15HE4.pkl".format(endpoint,dataset),"wb")
            pickle.dump(pred_dict,f)
            f.close()

        ## Bootstrap forward order
        if 0:
            np.random.seed(10)
            boot_protein_order = pd.DataFrame(index = boruta)

            for b in range(10):
                ind = resample(I_train,replace=True)
                y_use = y_train[ind]
                use_const = []
                use_const.extend(agesex)

                feat = []
                feat.extend(boruta)
    #             num_pro = len(feat)
                num_pro = 100
                protein_order = []
                for i in range(num_pro):#len(protein)+1):
                    mmp_pred_dict = {}
                    candidates = feat
                    print(len(candidates))
                    for c in candidates:
                        use = use_const + [c]
                        X_use = X_train.loc[ind,use]
                        model = sm.Logit(y_use,sm.add_constant(X_use))
                        res = model.fit(disp=0)
    #                     mmp_pred_dict[c] = [res.pvalues[-1]]
                        mmp_pred_dict[c] = [res.llf]

                    mmp_pred = pd.DataFrame.from_dict(mmp_pred_dict).transpose()
                    mmp_pred.columns = ['log_likelihood']

                    best_candidate = [mmp_pred.sort_values('log_likelihood', ascending = False).iloc[0,:].name]
                    feat.remove(best_candidate[0])
                    protein_order.append(best_candidate[0])
                    use_const = use_const + best_candidate

                boot_protein_order.loc[protein_order,b] = pd.DataFrame(protein_order).index
            pred_dict['{}_y{}_asprotein_lr_boot'.format(dataset,k)] = boot_protein_order

            f = open(folder+pred_folder+"{}_{}_forward.pkl".format(endpoint,dataset),"wb")
            pickle.dump(pred_dict,f)
            f.close()

        ## Recursive backward elimination feature ranking.
        if 0:
            use = []
            use.extend(agesex)
            use.extend(boruta)

            model = LogisticRegression(penalty='none',n_jobs=-1, solver ='saga', max_iter=2000, random_state = 10)
            feat_selector = RFE(model, step=1, n_features_to_select = 1)
            feat_selector.fit(X_train[use], y_train)
            pred_dict['{}_y{}_asprotein_RFElr'.format(dataset,k)] = pd.DataFrame(feat_selector.ranking_,index=use)

            f = open(folder+pred_folder+"{}_{}_forward.pkl".format(endpoint,dataset),"wb")
            pickle.dump(pred_dict,f)
            f.close()

        ## Forward feature selection baseline
        if 0:
            np.random.seed(10)
            boot_protein_order = [boruta]
            for b in range(1):
    #             ind = resample(I_train,replace=True)
                ind = I_train
                y_use = y_train[ind]
                use_const = []
                use_const.extend(agesex )
                use_const.extend(trad)
                use_const.extend(tradextralog)
                use_const.extend(tradcancer)

                feat = []
                feat.extend(boruta)
    #             num_pro = len(feat)
                num_pro = 100
                protein_order = []
                for i in range(num_pro):#len(protein)+1):
                    mmp_pred_dict = {}
                    candidates = feat
                    print(len(candidates))
                    for c in candidates:
                        use = use_const + [c]
                        X_use = X_train.loc[ind,use]
                        model = sm.Logit(y_use,sm.add_constant(X_use))
                        res = model.fit(disp=0)

    #                     mmp_pred_dict[c] = [res.pvalues[-1]]
                        mmp_pred_dict[c] = [res.llf]

                    mmp_pred = pd.DataFrame.from_dict(mmp_pred_dict).transpose()
                    mmp_pred.columns = ['log_likelihood']

                    best_candidate = [mmp_pred.sort_values('log_likelihood', ascending = False).iloc[0,:].name]
                    feat.remove(best_candidate[0])
                    protein_order.append([best_candidate[0],mmp_pred.sort_values('log_likelihood', ascending = False).iloc[0,0]])
                    use_const = use_const + best_candidate

    #             boot_protein_order.append(pd.DataFrame(protein_order).sort_values(0).index)
                boot_protein_order.append(protein_order)
            pred_dict['{}_y{}_baselineprotein_lr'.format(dataset,k)] = boot_protein_order

            f = open(folder+pred_folder+"{}_{}_forward.pkl".format(endpoint,dataset),"wb")
            pickle.dump(pred_dict,f)
            f.close()

        ## Forward feature selection baseline only men or only women
        if 0:
            np.random.seed(10)
            boot_protein_order = [boruta]
            for b in range(1):
    #             ind = resample(I_train,replace=True)
                ind = I_train
                y_use = y_train[ind]
                use_const = []
                use_const.extend(['age','age2'])
                use_const.extend(trad)
                use_const.extend(tradextralognosex)
                use_const.extend(tradcancer)

                feat = []
                feat.extend(boruta)
    #             num_pro = len(feat)
                num_pro = 100
                protein_order = []
                for i in range(num_pro):#len(protein)+1):
                    mmp_pred_dict = {}
                    candidates = feat
                    print(len(candidates))
                    for c in candidates:
                        use = use_const + [c]
                        X_use = X_train.loc[ind,use]
                        model = sm.Logit(y_use,sm.add_constant(X_use))
                        res = model.fit(disp=0)

    #                     mmp_pred_dict[c] = [res.pvalues[-1]]
                        mmp_pred_dict[c] = [res.llf]

                    mmp_pred = pd.DataFrame.from_dict(mmp_pred_dict).transpose()
                    mmp_pred.columns = ['log_likelihood']

                    best_candidate = [mmp_pred.sort_values('log_likelihood', ascending = False).iloc[0,:].name]
                    feat.remove(best_candidate[0])
                    protein_order.append([best_candidate[0],mmp_pred.sort_values('log_likelihood', ascending = False).iloc[0,0]])
                    use_const = use_const + best_candidate

    #             boot_protein_order.append(pd.DataFrame(protein_order).sort_values(0).index)
                boot_protein_order.append(protein_order)
            pred_dict['{}_y{}_baselineprotein_lr'.format(dataset,k)] = boot_protein_order

            f = open(folder+pred_folder+"{}_{}_forward.pkl".format(endpoint,dataset),"wb")
            pickle.dump(pred_dict,f)
            f.close()


from django.shortcuts import render
from .models import Collection
from product.models import Product
from user.models import Customer
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from django.db.models import Count
import json

# Create your views here.

def returnJson(data=None, pageCount=0, errorCode=0):
	if data is None:
		data = []
	return JsonResponse({'errorCode': errorCode, 'data': data, 'pageCount' : int(pageCount)})

# collection
@login_required
def customer_collection_list(request):
	try:
		customer = Customer.objects.get(id=request.user.id)
	except Customer.DoesNotExist:
		return returnJson([],0, 403)

	collections = Collection.objects.filter(customer=customer)

	return returnJson([dict(collection.body()) for collection in collections])

@login_required
def latest_customer_collection_list(request):
	try:
		customer = Customer.objects.get(id=request.user.id)
	except Customer.DoesNotExist:
		return returnJson([],0, 403)

	collections = Collection.objects.filter(customer=customer).order_by('-id')

	return returnJson([dict(collection.body()) for collection in collections])

def latest_customer_collection_list_by_page(request, pageNum):
	try:
		customer = Customer.objects.get(id=request.user.id)
	except Customer.DoesNotExist:
		return returnJson([],0, 403)

	collections = Collection.objects.filter(customer=customer)
	pages = (collections.count()+9)/10
	collections = collections.order_by('-id')[((pageNum-1)*10):(pageNum*10)]

	return returnJson([dict(collection.body()) for collection in collections],pages)

@login_required
def create_customer_collection(request):
	try:
		customer = Customer.objects.get(id=request.user.id)
	except Customer.DoesNotExist:
		return returnJson([],0, 403)

	data = json.loads(request.body)

	try:
		product = Product.objects.get(id = data["productId"])
	except Product.DoesNotExist:
		return returnJson([],0, 404)

	try:
		collection = Collection.objects.get(customer=customer, product=product)
	except Collection.DoesNotExist:
		collection = Collection.objects.create(product=product, customer=customer)
		return returnJson([dict(collection.body())])

	return returnJson([],0, 400)

@login_required
def get_customer_collection(request, pk_product):
	try:
		customer = Customer.objects.get(id=request.user.id)
	except Customer.DoesNotExist:
		return returnJson([],0, 403)

	try:
		product = Product.objects.get(id = pk_product)
	except Product.DoesNotExist:
		return returnJson([],0, 404)

	try:
		collection = Collection.objects.get(customer=customer, product=product)
	except Collection.DoesNotExist:
		return returnJson([],0, 404)

	return returnJson([dict(collection.body())])

@login_required
def edit_customer_collection(request, pk_product):
	try:
		customer = Customer.objects.get(id=request.user.id)
	except Customer.DoesNotExist:
		return returnJson([],0, 403)

	try:
		product = Product.objects.get(id = pk_product)
	except Product.DoesNotExist:
		return returnJson([],0, 404)

	try:
		collection = Collection.objects.get(customer=customer, product=product)
	except Collection.DoesNotExist:
		return returnJson([],0, 404)

	if request.method == 'DELETE':
		collection.delete()
		return returnJson()

def most_popular_product_list_by_page(request,pageNum):
	products = Product.objects.annotate(count = Count('collection'))
	pages = (products.count()+9)/10
	products = products.order_by('-count')[((pageNum-1)*10):(pageNum*10)]
	return returnJson([dict(product.body()) for product in products], pages)

def most_popular_product_list_by_category_and_page(request,pageNum):
	data = json.loads(request.body)
	products = Product.objects.filter(category=data["category"]).annotate(count = Count('collection'))
	pages = (products.count()+9)/10
	products = products.order_by('-count')[((pageNum-1)*10):(pageNum*10)]

	return returnJson([dict(product.body()) for product in products], pages)


import re

import pymongo

from asyncmongoengine.base.document import BaseDocument
from asyncmongoengine.base.errors import NotUniqueError, OperationError
from asyncmongoengine.base.metaclasses import (DocumentMetaclass,
                                               TopLevelDocumentMetaclass)
from asyncmongoengine.connection import DEFAULT_CONNECTION_NAME, get_db
from asyncmongoengine.context_managers import set_write_concern

class EmbeddedDocument(BaseDocument, metaclass=DocumentMetaclass):

    __slots__ = ("_instance",)

    my_metaclass = DocumentMetaclass

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._instance = None
        self._changed_fields = []

    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self._data == other._data
        return False

    def __ne__(self, other):
        return not self.__eq__(other)

class Document(BaseDocument, metaclass=TopLevelDocumentMetaclass):

    my_metaclass = TopLevelDocumentMetaclass

    __slots__ = ("__objects",)

    @property
    def pk(self):
        return None

    async def save_async(
        self,
        force_insert=False,
        validate=True,
        clean=True,
        write_concern=None,
        _refs=None,
        **kwargs
    ):
        if validate:
            self.validate(clean=clean)

        doc = self.to_mongo()

        if write_concern is None:
            write_concern = {}

        try:
            # Save a new document or update an existing one
            object_id = await self._save_create_async(doc, force_insert, write_concern)

        except pymongo.errors.DuplicateKeyError as err:
            message = "Tried to save duplicate unique keys (%s)"
            raise NotUniqueError(message % err)
        except pymongo.errors.OperationFailure as err:
            message = "Could not save document (%s)"
            if re.match("^E1100[01] duplicate key", str(err)):
                # E11000 - duplicate key error index
                # E11001 - duplicate key on update
                message = "Tried to save duplicate unique keys (%s)"
                raise NotUniqueError(message % err)
            raise OperationError(message % err)

        return self

    async def _save_create_async(self, doc, force_insert, write_concern):
        """Save a new document.

        Helper method, should only be used inside save().
        """
        collection = self._get_collection()

        with set_write_concern(collection, write_concern) as wc_collection:
            if "_id" in doc:
                raw_object = await wc_collection.find_one_and_replace(
                    filter={"_id": doc["_id"]},
                    replacement=doc
                )
                if raw_object:
                    return doc["_id"]

            object_id = (await wc_collection.insert_one(doc)).inserted_id

        return object_id

    def to_mongo(self, *args, **kwargs):
        data = super().to_mongo(*args, **kwargs)

        # If '_id' is None, try and set it from self._data. If that
        # doesn't exist either, remove '_id' from the SON completely.
        if data["_id"] is None:
            if self._data.get("id") is None:
                del data["_id"]
            else:
                data["_id"] = self._data["id"]

        return data

    @classmethod
    def _get_db(cls):
        """Some Model using other db_alias"""
        return get_db(cls._meta.get("db_alias", DEFAULT_CONNECTION_NAME))

    @classmethod
    def _get_collection(cls):
        """Return the PyMongo collection corresponding to this document.

        Upon first call, this method:
        1. Initializes a :class:`~pymongo.collection.Collection` corresponding
           to this document.
        2. Creates indexes defined in this document's :attr:`meta` dictionary.
           This happens only if `auto_create_index` is True.
        """
        if not hasattr(cls, "_collection") or cls._collection is None:
            db = cls._get_db()
            collection_name = cls._get_collection_name()
            cls._collection = db[collection_name]

        return cls._collection


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

def classify_kmeans(x, k, epsilon, seed=None, init='k++'):
    '''
    Classify x in R^(n,d) into k groups according to Lloyd's algorithm.
    Exit condition is that new centers are maximally displaced by epsilon.
    @init: allowed options are 'k++', 'box'
    @k: must be >=1

    '''
    n,d = x.shape
    if seed:
        np.random.seed(seed)

    if init == 'box':
        bbox = np.array([np.min(x, axis=0), np.max(x, axis=0)]) # Initialize within data range.
        centers = np.random.rand(k,d)*(bbox[1]-bbox[0])+bbox[0]
    elif init == 'k++':
        indices = np.arange(n)
        centers = x[np.random.choice(indices),:].reshape(1,d)
        for i in range(k-1):
            distances = np.array([np.sum(np.square(xi-centers), axis=1) for xi in x])
            weights = np.min(distances, axis=1)
            weights = weights/np.sum(weights)
            centers = np.append(centers, x[np.random.choice(indices, p=weights),:].reshape(1,d), axis=0)
    else:
        print('Unknown initialization scheme {}'.format(init))
        return None, None, None

    objective = []
    condition = True
    while condition:
        distances = np.array([np.sum(np.square(xi-centers), axis=1)  for xi in x])
        classify = np.argmin(distances, axis=1)
        objective.append(np.sum([np.sum(distances[classify==c, c])/np.sum(classify==c)
                                 if np.any(classify==c) else 0 for c in range(k)]))
        # If no points in class, reuse old center
        new_centers = np.array([np.mean(x[classify==c], axis=0) if np.any(classify==c)
                                else centers[c] for c in range(k)])
        if np.max(np.sum(np.square(new_centers - centers), axis=1)) < epsilon:
            condition = False
        centers = np.copy(new_centers)
    return classify, centers, objective

def clean_ax(ax):
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        ax.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off')

# =================================================================================================
if __name__ == '__main__':
    from mnist import MNIST
    mndata = MNIST(r'../CSE-546-HW-1/data/')
    X_train, labels_train = map(np.array, mndata.load_training())
    X_test, labels_test = map(np.array, mndata.load_testing())
    X_train = X_train/255.0
    X_test = X_test/255.0

    # 1. Plot centers for uniform (bounding box) initialization
    all_classes = []
    all_centers = []
    all_objectives = []
    for k in [5,10,20]:
        kclasses, kcenters, kobj = classify_kmeans(X_train, k, epsilon=1e-1, init='box')
        # Save data
        all_classes.append(kclasses)
        all_centers.append(kcenters)
        all_objectives.append(kobj)
        # Plot center images
        fig, ax = plt.subplots(1, k, figsize=[k,10])
        for c, anAx in zip(kcenters, ax):
            anAx.imshow(c.reshape([28,28]))
            clean_ax(anAx)
        fig.subplots_adjust(wspace=0, hspace=0)
        fig.savefig(os.path.join(r'./figs','imshow_box_{}.pdf'.format(k)))

    # 2. Plot centers for k-means++ initialization
    all_classes_2 = []
    all_centers_2 = []
    all_objectives_2 = []
    for k in [5,10,20]:
        kclasses, kcenters, kobj = classify_kmeans(X_train, k, epsilon=1e-1, init='k++')
        # Save data
        all_classes_2.append(kclasses)
        all_centers_2.append(kcenters)
        all_objectives_2.append(kobj)
        # Plot center images
        fig, ax = plt.subplots(1, k, figsize=[k,10])
        for c, anAx in zip(kcenters, ax):
            anAx.imshow(c.reshape([28,28]))
            clean_ax(anAx)
        fig.subplots_adjust(wspace=0, hspace=0)
        fig.savefig(os.path.join(r'./figs','imshow_kpp_{}.pdf'.format(k)))

    # 3. Plot k-means objective as a function of iteration
    fig, ax = plt.subplots(1,2, figsize=[10,4])

    for obj in all_objectives:
        ax[0].plot(np.arange(len(obj)), obj, lw=5, alpha=0.75)
        ax[0].set_xlabel('Iteration (box)', fontsize = 12)
        ax[0].set_ylabel('k-means Objective', fontsize=12)
    ax[0].legend(['k=5','k=10','k=20'],fontsize=12)
    ax[0].grid(True)

    for obj in all_objectives_2:
        ax[1].plot(np.arange(len(obj)), obj, lw=5, alpha=0.75)
        ax[1].set_xlabel('Iteration (k++)', fontsize = 12)
        ax[1].set_yticklabels([])
    ax[1].legend(['k=5','k=10','k=20'],fontsize=12)
    ax[1].grid(True)

    # Fix shared axis size using (experimental) limits
    for iax in ax:
        iax.set_ylim([0,3000])
        iax.set_xlim([-1,15])

    fig.subplots_adjust(wspace=0)
    fig.savefig(os.path.join(r'./figs','kmeans_combo_objs.pdf'))


import socket
import sys
import os
import logging
import random
import _thread
from checksum import Checksum
from packet import Packet

logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")

UDP_IP_ADDRESS = "127.0.0.1"
UDP_PORT_NO = 6789

class GBN:
    def __init__(self, windowSize, sequenceBit):
        self.sequence_bits = sequenceBit
        self.window_size = windowSize
        self.expected_seq_number = 1
        self.once = True

    def receive_packet(self, packet):
        if packet.sequenceNumber == 4 and self.once:
            self.once = False
            return self.expected_seq_number, True
        if packet.sequenceNumber == self.expected_seq_number:
            self.expected_seq_number += 1
            return self.expected_seq_number, False
        else:
            return self.expected_seq_number, True  # to discard because the packet is either out of order or duplicate

class SR:
    def __init__(self, windowSize, sequenceBit):
        self.window_size = windowSize
        self.sequence_bits = sequenceBit
        self.r_base = 1
        self.sequence_max = 2 ** self.sequence_bits
        self.queue = {}
        self.next_seq_num = 1
        self.mutex = _thread.allocate_lock()  # slide window mutex

    def is_packet_in_order(self, sequenceNumber):
        if sequenceNumber in self.queue:
            return True
        elif sequenceNumber < self.next_seq_num:
            return True
        else:
            return False

    def add_one_to_queue(self):
        self.queue[self.next_seq_num] = 'waiting'
        self.next_seq_num += 1

    def slide_window(self):
        for key in self.queue:
            if self.queue[key] == 'rcvd':
                del self.queue[key]
                self.add_one_to_queue()
            else:
                return

    def init_queue(self):
        for i in range(self.r_base, self.window_size + 1):
            self.add_one_to_queue()

    def receive_packet(self, packet):
        self.mutex.acquire()
        if self.is_packet_in_order(packet.sequenceNumber):
            self.queue[packet.sequenceNumber] = 'rcvd'
            self.slide_window()
            self.mutex.release()
            return packet.sequenceNumber, False
        else:
            self.mutex.release()
            return packet.sequenceNumber, True  # to discard because the packet is out of order

class UDPHelper:
    def __init__(self, protocol_name, windowSize, sequenceBit):
        self.ip_address = '127.0.0.1'
        self.port_number = 6789
        self.serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.serverSock.bind((UDP_IP_ADDRESS, UDP_PORT_NO))
        self.receiver_running = False
        self.receiver = None
        self.protocol = None
        self.packets = 0
        self.selectedPacket = 5
        if protocol_name == "GBN":
            self.protocol = GBN(windowSize, sequenceBit)
        elif protocol_name == "SR":
            self.protocol = SR(windowSize, sequenceBit)
            self.protocol.init_queue()
        else:
            raise Exception('***** Invalid protocol name *****')

    def simulatePacketLoss(self):
        self.packets += 1
        if self.packets == 10:
            self.packets = 0
            self.selectedPacket = random.randint(0, 10)
        if self.selectedPacket == self.packets:
            self.selectedPacket = None
            return True
        return False

    def startListening(self):
        while True:
            try:
                data, addr = self.serverSock.recvfrom(1024)
                packet = Packet()
                packet.deserializePacket(data)

                if self.simulatePacketLoss():
                    print("!!!!! Packet: ", packet.sequenceNumber, " lost :(   !!!!!")
                    continue

                if Checksum.verify(packet.data, packet.checksum):
                    ack_num, discard = self.protocol.receive_packet(packet)
                    if discard:
                        print("!!!!! Discarding packet: " + str(packet.sequenceNumber), " !!!!!")
                    else:
                        print("!!!!! Received Segment: ", str(packet.sequenceNumber), " !!!!!")
                    _ = self.serverSock.sendto(str(ack_num).encode(), addr)  # sending ack with ack_num
                    print("!!!!! ACK Sent: ", str(ack_num) ,"!!!!!")
                else:
                    print("!!!!! Checksum Error!!!!! Packet: ", packet.sequenceNumber, " discarded !!!!!")

            except KeyboardInterrupt:
                print('*****Interrupted*****')
                os._exit(0)
            except ConnectionResetError:
                pass  # Do nothing.
            except Exception:
                raise Exception
        return

    def waitToReceive(self):
        self.receiver.join()
        return

if __name__ == "__main__":
    if len(sys.argv) is not 3:
        print("***** Please insert 4 arguments! ***** ")
        print("***** Syntax *****")
        print("***** Receiver.py inputfile(GBN/SR) portNumber *****")
    else:
        try:
            UDP_PORT_NO = int(sys.argv[2])
            file = open(sys.argv[1]).readlines()
            protocol = file[0].strip()
            sequence_bits = int(file[1].strip().split(' ')[0])
            window_size = int(file[1].strip().split(' ')[1])
            timeout_period = float(file[2].strip())
            segment_size = int(file[3].strip())

            if protocol == "GBN":
                udp_helper = UDPHelper('GBN', window_size, sequence_bits)
            elif protocol == "SR":
                udp_helper = UDPHelper('SR', window_size, sequence_bits)

            udp_helper.startListening()

        except Exception:
            raise Exception

Go-back-n and Selective Repeat/receiver.py


import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

Apparatus_volume = float(input("Введите объем аппарата м.куб: "))
Fluid_speed = float(input("Введите скорость потока м.куб/cек: "))
Start_concentration = float(input("Введите начальную концентрацию на входе в реактор кмоль/м.куб:  "))
Reactor_concentration = float(input("Введите начальную концентрацию внутри реактора кмоль/м.куб:  "))

dt = 0.1
E = 0.0001
Reactor_time = Apparatus_volume/Fluid_speed
c = [0]
t = [0]

c[0] = Reactor_concentration
t[0] = 0
i = 0

while True:
    i = i+1
    t.append(t[i-1] + dt)
    print(t[i])
    c.append(c[i-1] + dt * (Start_concentration - c[i - 1]) / Reactor_time)
    print(c[i])

    if abs(c[i]-c[i-1]) < E:
        break
plt.title("Зависимость концентрации от времени в реакторе")
plt.xlabel("Время, секунды")
plt.ylabel("концентрацияб кмоль/м.куб")
plt.plot(t,c)
plt.show()


from datetime import datetime, timedelta

from bs4 import BeautifulSoup
import pytest

from app.database.models import Event
from app.routers.dayview import (
    DivAttributes,
    is_all_day_event_in_day,
    is_specific_time_event_in_day,
)

from app.routers.event import create_event

def create_dayview_event(events, session, user):
    for event in events:
        create_event(
            db=session,
            title="test",
            start=event.start,
            end=event.end,
            owner_id=user.id,
            color=event.color,
        )

def test_minutes_position_calculation(event_with_no_minutes_modified):
    div_attr = DivAttributes(event_with_no_minutes_modified)
    assert div_attr._minutes_position(div_attr.start_time.minute) is None
    assert div_attr._minutes_position(div_attr.end_time.minute) is None
    assert div_attr._minutes_position(0) is None
    assert div_attr._minutes_position(60) == 4

def test_div_attributes(event1):
    div_attr = DivAttributes(event1)
    assert div_attr.total_time == "07:05 - 09:15"
    assert div_attr.grid_position == "32 / 40"
    assert div_attr.length == 130
    assert div_attr.color == "grey"

@pytest.mark.parametrize(
    "minutes,css_class,visiblity",
    [
        (90, "title-size-small", True),
        (45, "title-size-xsmall", False),
        (30, "title-size-tiny", False),
    ],
)
def test_font_size_attribute(minutes, css_class, visiblity):
    start = datetime(year=2021, month=2, day=3, hour=7)
    end = start + timedelta(minutes=minutes)
    event = Event(
        title="test",
        content="test",
        start=start,
        end=end,
        owner_id=1,
    )
    div_attr = DivAttributes(event)
    assert div_attr.title_size_class == css_class
    assert div_attr.total_time_visible == visiblity

def test_div_attr_multiday(multiday_event):
    day = datetime(year=2021, month=2, day=1)
    assert DivAttributes(multiday_event, day).grid_position == "55 / 101"
    day += timedelta(hours=24)
    assert DivAttributes(multiday_event, day).grid_position == "1 / 101"
    day += timedelta(hours=24)
    assert DivAttributes(multiday_event, day).grid_position == "1 / 55"

def test_is_specific_time_event_in_day(all_day_event1, event3):
    day = datetime(year=2021, month=2, day=3, hour=0, minute=0)
    day_end = day + timedelta(hours=24)
    function_returns_true = is_specific_time_event_in_day(
        event=event3,
        day=day,
        day_end=day_end,
    )
    function_returns_false = is_specific_time_event_in_day(
        event=all_day_event1,
        day=day,
        day_end=day_end,
    )
    assert function_returns_true
    assert not function_returns_false

def test_is_all_day_event_in_day(all_day_event1, event3):
    day = datetime(year=2021, month=2, day=3, hour=0, minute=0)
    day_end = day + timedelta(hours=24)
    function_returns_true = is_all_day_event_in_day(
        event=all_day_event1,
        day=day,
        day_end=day_end,
    )
    function_returns_false = is_all_day_event_in_day(
        event=event3,
        day=day,
        day_end=day_end,
    )
    assert function_returns_true
    assert not function_returns_false

def test_div_attributes_with_costume_color(event2):
    div_attr = DivAttributes(event2)
    assert div_attr.color == "blue"

def test_wrong_timeformat(session, user, client, event1, event2, event3):
    create_dayview_event([event1, event2, event3], session=session, user=user)
    response = client.get("/day/1-2-2021")
    assert response.status_code == 404

def test_dayview_html(event1, event2, event3, session, user, client):
    create_dayview_event([event1, event2, event3], session=session, user=user)
    response = client.get("/day/2021-2-1")
    soup = BeautifulSoup(response.content, "html.parser")
    assert "FEBRUARY" in str(soup.find("div", {"id": "top-tab"}))
    assert "event1" in str(soup.find("div", {"id": "event1"}))
    assert "event2" in str(soup.find("div", {"id": "event2"}))
    assert soup.find("div", {"id": "event3"}) is None

@pytest.mark.parametrize(
    "day,grid_position",
    [
        ("2021-2-1", "55 / 101"),
        ("2021-2-2", "1 / 101"),
        ("2021-2-3", "1 / 55"),
    ],
)
def test_dayview_html_with_multiday_event(
    multiday_event,
    session,
    user,
    client,
    day,
    grid_position,
):
    create_dayview_event([multiday_event], session=session, user=user)
    session.commit()
    response = client.get(f"/day/{day}")
    soup = BeautifulSoup(response.content, "html.parser")
    grid_pos = f"grid-row: {grid_position};"
    assert grid_pos in str(soup.find("div", {"id": "event1"}))


from .core import TransformConverterBase, parameter_to_value

class Rotate3DConverter(TransformConverterBase):
    """
    ASDF support for serializing rotation models that
    use the rotate3d tag.
    """

    tags = ("tag:stsci.edu:asdf/transform/rotate3d-*",)
    types = (
        "astropy.modeling.rotations.RotateNative2Celestial",
        "astropy.modeling.rotations.RotateCelestial2Native",
        "astropy.modeling.rotations.EulerAngleRotation",
    )

    def to_yaml_tree_transform(self, model, tag, ctx):
        from astropy.modeling import rotations

        if isinstance(model, rotations.RotateNative2Celestial):
            node = {
                "phi": parameter_to_value(model.lon),
                "theta": parameter_to_value(model.lat),
                "psi": parameter_to_value(model.lon_pole),
                "direction": "native2celestial",
            }
        elif isinstance(model, rotations.RotateCelestial2Native):
            node = {
                "phi": parameter_to_value(model.lon),
                "theta": parameter_to_value(model.lat),
                "psi": parameter_to_value(model.lon_pole),
                "direction": "celestial2native",
            }
        else:
            node = {
                "phi": parameter_to_value(model.phi),
                "theta": parameter_to_value(model.theta),
                "psi": parameter_to_value(model.psi),
                "direction": model.axes_order,
            }

        return node

    def from_yaml_tree_transform(self, node, tag, ctx):
        from astropy.modeling import rotations

        if node["direction"] == "native2celestial":
            return rotations.RotateNative2Celestial(node["phi"], node["theta"], node["psi"])

        if node["direction"] == "celestial2native":
            return rotations.RotateCelestial2Native(node["phi"], node["theta"], node["psi"])

        return rotations.EulerAngleRotation(node["phi"], node["theta"], node["psi"], axes_order=node["direction"])

class RotationSequenceConverter(TransformConverterBase):
    """
    ASDF support for serializing rotation sequence models.
    """

    tags = ("tag:stsci.edu:asdf/transform/rotate_sequence_3d-*",)
    types = (
        "astropy.modeling.rotations.RotationSequence3D",
        "astropy.modeling.rotations.SphericalRotationSequence",
    )

    def to_yaml_tree_transform(self, model, tag, ctx):
        from astropy.modeling import rotations

        node = {"angles": list(model.angles.value)}
        node["axes_order"] = model.axes_order
        if isinstance(model, rotations.SphericalRotationSequence):
            node["rotation_type"] = "spherical"
        elif isinstance(model, rotations.RotationSequence3D):
            node["rotation_type"] = "cartesian"
        else:
            msg = f"Cannot serialize model of type {type(model)}"
            raise TypeError(msg)
        return node

    def from_yaml_tree_transform(self, node, tag, ctx):
        from astropy.modeling import rotations

        angles = node["angles"]
        axes_order = node["axes_order"]
        rotation_type = node["rotation_type"]
        if rotation_type == "cartesian":
            return rotations.RotationSequence3D(angles, axes_order=axes_order)

        if rotation_type == "spherical":
            return rotations.SphericalRotationSequence(angles, axes_order=axes_order)

        msg = f"Unrecognized rotation_type: {rotation_type}"
        raise ValueError(msg)


import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from time import sleep

def get_source(driver_path, website):
    driver = webdriver.Chrome(driver_path)
    driver.get(website)
    sleep(3)
    source = driver.page_source
    return source

def get_table_bs(driver_path, urls):
    source_best = get_source(driver_path, urls['Best'])
    source_good = get_source(driver_path, urls['Good'])
    soup_best = BeautifulSoup(source_best, 'html.parser')
    soup_good = BeautifulSoup(source_good, 'html.parser')
    table_best = soup_best.find('table', class_="CryptoTable_table__P2T2N")
    table_good = soup_good.find('table', class_="CryptoTable_table__P2T2N")
    return table_best, table_good

def selected_data_dic(table):
    retrieved_data = {}
    for c in table.find_all('tbody'):
        rows = c.find_all('tr', {'class': lambda value: value and value.startswith('CryptoTable')})
        for row in rows:
            symbol = row.find('a').text
            # print(symbol)
            rank = row.find('strong').text
            price = row.find_all('div', {'class': 'right'})[0].text
            price_ath = row.find_all('div', {'class': 'right'})[1].text
            change_24 = row.find_all('div', {'class': lambda value: value and value.startswith('center Percent')})[
                0].text
            change_30 = row.find_all('div', {'class': lambda value: value and value.startswith('center Percent')})[
                1].text
            change_ath = row.find_all('div', {'class': lambda value: value and value.startswith('center Percent')})[
                2].text
            retrieved_data[symbol] = {'Price': price,
                                      'Price ATH': price_ath,
                                      'Rank': rank,
                                      'Change(24H)': change_24,
                                      'Change(30D)': change_30,
                                      'Change(ATH)': change_ath
                                       }
    return retrieved_data

def main():
    base_url = 'https://4kings.xyz/crypto'
    urls = {'Best': base_url + '/best',
            'Good': base_url + '/good'}
    driver_path = '/Users/thomasgrandguillot/Desktop/chromedriver'
    table_best, table_good = get_table_bs(driver_path, urls)
    data_best_dic = selected_data_dic(table_best)
    data_good_dic = selected_data_dic(table_good)
    df_best = pd.DataFrame.from_dict(data_best_dic, orient='index')
    df_good = pd.DataFrame.from_dict(data_good_dic, orient='index')
    return df_best, df_good

if __name__ == "__main__":
    df_best, df_good = main()


from pathlib import Path
hppfiles = list(Path("../poprithms").rglob("*.[h][p][p]"))
bad_files = []

for fl in hppfiles:
    with open(fl) as f:
        lines = []
        line = f.readline()
        while (line and "#include" not in line):
            lines.append(line)
            line = f.readline()

        if ("#include" in line and lines and lines[-1].strip() != ""):
            bad_files.append(str(fl))

#1 : fail, and 0 : succeed
if (not bad_files):
    print("0")

else:
    print("1")
    print("header file(s) with incorrect header guard spacing are:")
    for x in bad_files:
        print(x)


import argparse
import csv
import datetime
import pathlib
import typing

def get_arguments():
	parser = argparse.ArgumentParser()
	parser.add_argument("--folder", default=None, help="Folder that contains log files you want to update")
	parser.add_argument("--file", default=None, help="Log file you want to update")

	return parser.parse_args()

class GpsRollover:
	FILE_FORMAT = "WG%Y%m%d%H%M%S.log"
	GPS_SENTENCE_CODE = "$GPRMC" # the code we need to look for to modify the date
	OFFSET_DAYS = 7168

	@classmethod
	def date_to_str(cls, date: datetime.date) -> str:
		return date.strftime("%0d%0m") + date.strftime("%Y")[2:]

	@classmethod
	def str_to_date(cls, date_string: str) -> datetime.date:
		assert len(date_string) == 6, "Incorrect length for date_string"
		day, month, year = date_string[0:2], date_string[2:4], date_string[4:6]
		return datetime.date(int("20"+year), int(month), int(day))

	@classmethod
	def add_offset(cls, date_obj: datetime.date) -> datetime.date:
		return date_obj + datetime.timedelta(days=cls.OFFSET_DAYS)

	@classmethod
	def fix_files_in_folder(cls, folder: str) -> None:
		path = pathlib.Path(folder).expanduser()
		for entry in path.iterdir():
			if not entry.is_file() or entry.suffix != ".log":
				continue
			if entry.name[:6] > "WG2021":  # We shouldn't have to touch these files
				continue

			cls.fix_file(entry)

	@classmethod
	def fix_file(cls, file: typing.Union[pathlib.Path, str]) -> None:
		if isinstance(file, str):
			filepath = pathlib.Path(file).expanduser()
		elif isinstance(file, pathlib.Path):
			filepath = file
		else:
			raise ValueError("File must be a Path or str")

		file_datetime = datetime.datetime.strptime(filepath.name, cls.FILE_FORMAT)

		target_path = pathlib.Path(filepath.with_name((cls.add_offset(file_datetime).strftime(cls.FILE_FORMAT))))

		writer = csv.writer(target_path.open("w"), delimiter=",", quoting=csv.QUOTE_NONE)
		reader = csv.reader(filepath.open("r"), delimiter=",")

		for row in reader:
			if row[0] == cls.GPS_SENTENCE_CODE:
				row[-4] = cls.date_to_str(cls.add_offset(cls.str_to_date(row[-4])))
			writer.writerow(row)

if __name__ == "__main__":
	args = get_arguments()
	if args.file:
		GpsRollover.fix_file(args.file)
	elif args.folder:
		GpsRollover.fix_files_in_folder(args.folder)


import sqlite3
import json
from models.entry import JournalEntry

def get_all_JournalEntries():
    # Open a connection to the database
    with sqlite3.connect("./dailyjournal.sqlite3") as conn:

        # Just use these. It's a Black Box.
        conn.row_factory = sqlite3.Row
        db_cursor = conn.cursor()

        # Write the SQL query to get the information you want
        db_cursor.execute("""
        SELECT
            e.id,
            e.concept,
            e.entry,
            e.date,
            e.mood_id
        FROM JournalEntries e
        """)

        # Initialize an empty list to hold all entry representations
        journalEntries = []

        # Convert rows of data into a Python list
        dataset = db_cursor.fetchall()

        # Iterate list of data returned from database
        for row in dataset:

            # Create an entry instance from the current row.
            # Note that the database fields are specified in
            # exact order of the parameters defined in the
            # entry class above.
            entry = JournalEntry(row['id'], row['concept'], row['entry'],
                                 row['date'], row['mood_id'])

            journalEntries.append(entry.__dict__)

    # Use `json` package to properly serialize list as JSON
    return json.dumps(journalEntries)

# Function with a single parameter
def get_single_entry(id):
    with sqlite3.connect("./dailyjournal.sqlite3") as conn:
        conn.row_factory = sqlite3.Row
        db_cursor = conn.cursor()

        # Use a ? parameter to inject a variable's value
        # into the SQL statement.
        db_cursor.execute("""
        SELECT
            e.id,
            e.concept,
            e.entry,
            e.date,
            e.mood_id
        FROM JournalEntries e
        WHERE e.id = ?
        """, (id, ))

        # Load the single result into memory
        data = db_cursor.fetchone()

        # Create an entry instance from the current row
        entry = JournalEntry(data['id'], data['concept'], data['entry'],
                             data['date'], data['mood_id'])

        return json.dumps(entry.__dict__)

def delete_entry(id):
    with sqlite3.connect("./dailyjournal.sqlite3") as conn:
        db_cursor = conn.cursor()

        db_cursor.execute("""
        DELETE FROM JournalEntries
        WHERE id = ?
        """, (id, ))

def get_entries_by_search(searchTerm):

    with sqlite3.connect("./dailyjournal.sqlite3") as conn:
        conn.row_factory = sqlite3.Row
        db_cursor = conn.cursor()
        # Write the SQL query to get the information you want
        db_cursor.execute("""
          SELECT
            e.id,
            e.concept,
            e.entry,
            e.date,
            e.mood_id
        FROM JournalEntries e
        WHERE e.entry LIKE ?
        """, ( f'%{searchTerm}%', ))

        entries = []
        dataset = db_cursor.fetchall()

        for row in dataset:
            entry = JournalEntry(
                row['id'], row['concept'], row['entry'], row['date'], row['mood_id'])

            entries.append(entry.__dict__)

    return json.dumps(entries)


"""
  Harness for conducting black box optimisation experiments.
  -- kandasamy@cs.cmu.edu
"""

# pylint: disable=invalid-name
# pylint: disable=no-member
# pylint: disable=abstract-class-not-used
# pylint: disable=star-args
# pylint: disable=maybe-no-member

from argparse import Namespace
from datetime import datetime
import numpy as np
import os
# Local imports
from opt.blackbox_optimiser import Initialiser
from opt.ga_optimiser import ga_optimise_from_args
from opt.nn_gp_bandit import nngpb_from_func_caller, nnrandbandit_from_func_caller
from opt.nn_opt_utils import get_initial_pool
from utils.experimenters import BasicExperimenter

class NNOptExperimenter(BasicExperimenter):
  """ Base class for running experiments. """
  # pylint: disable=attribute-defined-outside-init

  def __init__(self, experiment_name, func_caller, worker_manager, max_capital, methods,
               num_experiments, save_dir, experiment_options, save_file_prefix='',
               method_options=None, reporter=None, **kwargs):
    """ Constructor. Also see BasicExperimenter for more args. """
    # pylint: disable=too-many-arguments
    save_file_name = self._get_save_file_name(save_dir, experiment_name,
      worker_manager.num_workers, save_file_prefix, worker_manager.get_time_distro_info(),
      max_capital)
    super(NNOptExperimenter, self).__init__(experiment_name, num_experiments,
                                            save_file_name, reporter=reporter, **kwargs)
    self.func_caller = func_caller
    self.worker_manager = worker_manager
    self.max_capital = float(max_capital)
    # Methods
    self.methods = methods
    self.num_methods = len(methods)
    self.domain = func_caller.domain
    self.method_options = (method_options if method_options else
                           {key: None for key in methods})
    # Experiment options will have things such as if the evaluations are noisy,
    # the time distributions etc.
    self.experiment_options = experiment_options
    self._set_up_saving()

  @classmethod
  def _get_save_file_name(cls, save_dir, experiment_name, num_workers, save_file_prefix,
                          time_distro_str, max_capital):
    """ Gets the save file name. """
    save_file_prefix = save_file_prefix if save_file_prefix else experiment_name
    save_file_name = '%s-M%d-%s-c%d-%s.mat'%(save_file_prefix, num_workers,
      time_distro_str, int(max_capital), datetime.now().strftime('%m%d-%H%M%S'))
    save_file_name = os.path.join(save_dir, save_file_name)
    return save_file_name

  def _set_up_saving(self):
    """ Runs some routines to set up saving. """
    # Store methods and the options in to_be_saved
    self.to_be_saved.max_capital = self.max_capital
    self.to_be_saved.num_workers = self.worker_manager.num_workers
    self.to_be_saved.methods = self.methods
    self.to_be_saved.method_options = self.method_options # Some error created here.
    self.to_be_saved.time_distro_str = self.worker_manager.get_time_distro_info()
    # Data about the problem
    self.to_be_saved.true_opt_val = (self.func_caller.opt_val
      if self.func_caller.opt_val is not None else -np.inf)
    self.to_be_saved.true_opt_pt = (self.func_caller.opt_pt
      if self.func_caller.opt_pt is not None else 'not-known')
    self.to_be_saved.domain_type = self.domain.get_type()
#     # For the results
    self.data_to_be_saved = ['query_step_idxs',
                             'query_points',
                             'query_vals',
                             'query_true_vals',
                             'query_send_times',
                             'query_receive_times',
                             'query_eval_times',
                             'curr_opt_vals',
                             'curr_true_opt_vals',
                             'num_jobs_per_worker',
                            ]
    self.data_not_to_be_mat_saved.extend(['method_options', 'query_points'])
    self.data_not_to_be_pickled.extend(['method_options'])
    for data_type in self.data_to_be_saved:
      setattr(self.to_be_saved, data_type, self._get_new_empty_results_array())

  def _get_new_empty_results_array(self):
    """ Returns a new empty arrray to be used for saving results. """
#     return np.empty((self.num_methods, 0), dtype=np.object)
    return np.array([[] for _ in range(self.num_methods)], dtype=np.object)

  def _get_new_iter_results_array(self):
    """ Returns an empty array to be used for saving results of current iteration. """
#     return np.empty((self.num_methods, 1), dtype=np.object)
    return np.array([['-'] for _ in range(self.num_methods)], dtype=np.object)

  def _print_method_header(self, full_method_name):
    """ Prints a header for the current method. """
    experiment_header = '-- Exp %d/%d on %s:: %s with cap %0.4f. ----------------------'%(
      self.experiment_iter, self.num_experiments, self.experiment_name, full_method_name,
      self.max_capital)
    self.reporter.writeln(experiment_header)

  def get_iteration_header(self):
    """ Header for iteration. """
    noisy_str = ('no-noise' if self.func_caller.noise_type == 'none' else
                 'noisy(%0.2f)'%(self.func_caller.noise_scale))
    opt_val_str = ('?' if self.func_caller.opt_val is None
                       else '%0.5f'%(self.func_caller.opt_val))
    ret = '%s (M=%d), td: %s, max=%s, max-capital %0.2f, %s'%(self.experiment_name,
      self.worker_manager.num_workers, self.to_be_saved.time_distro_str, opt_val_str,
      self.max_capital, noisy_str)
    return ret

  def _print_method_result(self, method, comp_opt_val, num_evals):
    """ Prints the result for this method. """
    result_str = 'Method: %s achieved max-val %0.5f in %d evaluations.\n'%(method,
                  comp_opt_val, num_evals)
    self.reporter.writeln(result_str)

  def _get_pre_eval_points_and_vals(self):
    """ Gets Initial points for all methods. """
    if self.experiment_options.pre_eval_points == 'generate':
      init_pool = get_initial_pool(self.domain.get_type())
    else:
      # Load from the file.
      raise NotImplementedError('Not written reading results from file yet.')
    # Create an initialiser
    initialiser = Initialiser(self.func_caller, self.worker_manager)
    initialiser.options.get_initial_points = lambda _: init_pool
    initialiser.options.max_num_steps = 0
    _, _, init_hist = initialiser.initialise()
    pre_eval_points = init_hist.query_points
    pre_eval_vals = init_hist.query_vals
    pre_eval_true_vals = init_hist.query_true_vals
    return pre_eval_points, pre_eval_vals, pre_eval_true_vals

  def run_experiment_iteration(self):
    """ Runs each method in self.methods once and stores the results to be saved. """
    curr_iter_results = Namespace()
    for data_type in self.data_to_be_saved:
      setattr(curr_iter_results, data_type, self._get_new_iter_results_array())

    # Fetch pre-evaluation points.
    self.worker_manager.reset()
    (pre_eval_points_for_all_meths, pre_eval_vals_for_all_meths,
     pre_eval_true_vals_for_all_meths) = self._get_pre_eval_points_and_vals()
    self.reporter.writeln('Using %d pre-eval points with values: eval: %s, true: %s'%(
      len(pre_eval_vals_for_all_meths), pre_eval_vals_for_all_meths,
      pre_eval_true_vals_for_all_meths))

    # Will go through each method in this loop.
    for meth_iter in range(self.num_methods):
      curr_method = self.methods[meth_iter]
      curr_options = self.method_options[curr_method]
      # Set pre_eval points and vals
      curr_options.pre_eval_points = pre_eval_points_for_all_meths
      curr_options.pre_eval_vals = pre_eval_vals_for_all_meths
      curr_options.pre_eval_true_vals = pre_eval_true_vals_for_all_meths
      # Reset worker manager
      self.worker_manager.reset()
      self.reporter.writeln('\nResetting worker manager: worker_manager.optimiser:%s'%(
                            str(self.worker_manager.optimiser)))

      # Call the method here.
      self._print_method_header(curr_method)
      _, _, history = optimise_with_method_on_func_caller(curr_method, self.func_caller,
                        self.worker_manager, self.max_capital, meth_options=curr_options,
                        reporter=self.reporter)

      # Now save results for current method
      for data_type in self.data_to_be_saved:
        data = getattr(history, data_type)
        data_pointer = getattr(curr_iter_results, data_type)
        data_pointer[meth_iter, 0] = data
      # Print out results
      comp_opt_val = history.curr_true_opt_vals[-1]
      num_evals = len(history.curr_true_opt_vals)
      self._print_method_result(curr_method, comp_opt_val, num_evals)
      # Save results of current iteration
      self.update_to_be_saved(curr_iter_results)
      self.save_pickle()
      self.save_results()
      # for meth_iter ends here
    # Save here
    self.update_to_be_saved(curr_iter_results)
    self.save_pickle()
    # No need to explicitly save_results() here - it is done by the parent class.

  def update_to_be_saved(self, curr_iter_results):
    """ Updates the results of the data to be saved with curr_iter_results."""
    for data_type in self.data_to_be_saved:
      data = getattr(curr_iter_results, data_type)
      curr_data_to_be_saved = getattr(self.to_be_saved, data_type)
      if curr_data_to_be_saved.shape[1] == self.experiment_iter:
        updated_data_to_be_saved = curr_data_to_be_saved
        updated_data_to_be_saved[:, -1] = data.ravel()
      elif curr_data_to_be_saved.shape[1] < self.experiment_iter:
        updated_data_to_be_saved = np.concatenate((curr_data_to_be_saved, data), axis=1)
      else:
        raise ValueError('Something wrong with data saving.')
      setattr(self.to_be_saved, data_type, updated_data_to_be_saved)

# --------------------------------------------------------------------------------------
def optimise_with_method_on_func_caller(method, func_caller, worker_manager, max_capital,
                                        meth_options, reporter):
  """ This function does the optimisation. """
  # Mode should be specified in the first three letters
  meth_options.mode = method[0:3]
  specific_method = method[3:]
  if specific_method == 'GA':
    return ga_optimise_from_args(func_caller, worker_manager, max_capital,
                                 meth_options.mode, meth_options.mutation_op,
                                 options=meth_options, reporter=reporter)
  elif specific_method in ['HEI', 'EI', 'HTS', 'TS', 'HUCB', 'UCB']:
    meth_options.acq = specific_method
    return nngpb_from_func_caller(func_caller, worker_manager, meth_options.tp_comp,
                                  max_capital, acq=specific_method,
                                  options=meth_options, reporter=reporter)
  elif specific_method == 'RAND':
    meth_options.acq = specific_method
    return nnrandbandit_from_func_caller(func_caller, worker_manager, max_capital,
                                         options=meth_options, reporter=reporter)
  else:
    raise ValueError('Unknown method: %s'%(method))


import configparser
import gettext
import os

_LOCALE_DIR = 'locales'
_LANGUAGE_INI_FILEPATH = 'shiftscheduler/i18n/language.ini'
_code = None

def GetTextFn(domain_name):
    code = GetLanguageCode()
    language = gettext.translation(domain_name, localedir=_LOCALE_DIR, languages=[code])
    return language.gettext

def GetLanguageCode():
    global _code
    if _code is None:
        config = configparser.ConfigParser()
        config.read_file(open(_LANGUAGE_INI_FILEPATH))
        _code = config.get('DEFAULT', 'LanguageCode')

    return _code