from datetime import datetime
from .models import TimeRecord
from .forms import EmployeeProfileForm
from .forms import LeaveRequestForm
from .models import LeaveRequest
from django.shortcuts import render, redirect, get_object_or_404
from django.shortcuts import render, redirect
from Admin.models import Employee, LeaveRequest, TimeRecord
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from datetime import timezone, datetime
from .forms import *
from django.http import HttpResponse
from reportlab.pdfgen import canvas
from .models import Employee
from django.shortcuts import get_object_or_404
from django.contrib.auth.views import PasswordChangeView
from django.urls import reverse_lazy, reverse
from django.db.models import Q

# employee portal
# View to show the employee dashboard
@login_required(login_url='login')
def employee_dashboard(request):
    user_id = request.user.id

    leave_requests = LeaveRequest.objects.all().filter(user=user_id)
    context = {
        'user_id': user_id,
        'leave_requests': leave_requests,
    }

    return render(request, 'employee/dashboard.html', context)

@login_required(login_url='login')
def leave_request_dashboard(request, user_id):
    user_id = request.user.id
    leave_requests = LeaveRequest.objects.filter(
        user=user_id).order_by('-id')[:5]
    context = {
        'leave_requests': leave_requests,
        'user_id': user_id,
    }
    return render(request, 'employee/leave_request_dashboard.html', context)

@login_required(login_url='login')
def attendance_dashboard(request, user_id):
    user_id = request.user.id
    attendances = TimeRecord.objects.filter(
        user=user_id).order_by('-record_date')
    context = {
        'attendances': attendances,
        'user_id': user_id,
    }
    return render(request, 'employee/attendance_dashboard.html', context)

@login_required(login_url='login')
def leave_request_list(request, user_id):
    user_id = request.user.id
    leave_requests = LeaveRequest.objects.all().filter(user_id=user_id)
    context = {
        'leave_requests': leave_requests,
        'user_id': user_id,
    }
    return render(request, 'employee/leave_request_list.html', context)

@login_required(login_url='login')
def create_leave_request(request):
    if request.method == 'POST':
        form = LeaveRequestForm(request.POST, request.FILES)
        if form.is_valid():
            leave_request = form.save(commit=False)
            leave_request.user = request.user  # Associate the user
            leave_request.save()
            return redirect('leave_request_list',user_id=request.user.id)
    else:
        form = LeaveRequestForm()

    context = {
        'form': form,
    }
    return render(request, 'employee/create_leave_request.html', context)

@login_required(login_url='login')
def edit_leave_request(request, request_id):
    leave_request = get_object_or_404(LeaveRequest, id=request_id, user=request.user)

    if request.method == 'POST':
        form = LeaveRequestForm(request.POST, request.FILES, instance=leave_request)
        if form.is_valid():
            leave_request = form.save()
            return redirect('leave_request_list', user_id=request.user.id)

    else:
        form = LeaveRequestForm(instance=leave_request)

    context = {
        'form': form,
        'leave_request': leave_request,
    }
    return render(request, 'employee/edit_leave_request.html', context)

@login_required(login_url='login')
def delete_leave_request(request, request_id):
    leave_request = get_object_or_404(LeaveRequest, id=request_id, user=request.user)

    if request.method == 'POST':
        leave_request.delete()

    return redirect('leave_request_list', user_id=request.user.id)
@login_required(login_url='login')
def leave_request_detail(request, user_id):
    leave_request = get_object_or_404(LeaveRequest, user=user_id)
    return render(request, 'leave_request_detail.html', {'leave_request': leave_request})

@login_required(login_url='login')
def view_employee_profile(request):
    user_id = request.user.id
    employee = get_object_or_404(Employee, user=user_id)
    return render(request, 'employee/view_profile.html', {'employee': employee})

@login_required(login_url='login')
def edit_employee_profile(request,user_id):
    employee = get_object_or_404(Employee, user=user_id)
    if request.method == 'POST':
        form = EmployeeProfileForm(request.POST, instance=employee)
        if form.is_valid():
            form.save()
            messages.success(request, 'Profile updated successfully.')
            return redirect('view_employee_profile')
    else:
        form = EmployeeProfileForm(instance=employee)
    return render(request, 'employee/edit_profile.html', {'employee': employee, 'form': form})

class ChangePasswordView(PasswordChangeView):
    template_name = 'employee/change_password.html'
    success_url = reverse_lazy('view_employee_profile')

@login_required(login_url='login')
def report_time(request):
    user = request.user
    record_date = datetime.now().date()

    # Check if there is an existing record for today
    existing_record = TimeRecord.objects.filter(
        Q(user=user, record_date=record_date),
        ~Q(clock_in=None),
    ).exists()

    if existing_record:
        messages.warning(request, 'You are already clocked in for today wait for approval !')
    else:
            # User is not clocked in, so this is a clock-in event
            TimeRecord.objects.create(
                user=user,
                record_date=record_date,
                clock_in=datetime.now().time(),
                clock_out=None,
                is_clock_in=True,
            )
            messages.success(request, 'You clock in successfully.')

    return redirect(reverse('time_record_list', args=[request.user.id]))

@login_required(login_url='login')
def clock_out(request, pk):
    user=request.user
    record_date = datetime.now().date()

    record = TimeRecord.objects.filter(pk=pk,
    user=user,
    record_date=record_date
    ).first()

    if record and  record.is_clock_out:
        # Check if the record is approved before allowing clock-out
        if record.is_approved:
            record.clock_out = datetime.now().time()
            record.is_clock_in = False
            record.is_clock_out = False
            record.save()
            messages.success(request, 'You clock out successfully.')
        else:
            messages.warning(request, 'You already clock out for the day!!.')
    else:
        messages.warning(request, 'Wait for the Approval.')
    return redirect(reverse('time_record_list', args=[request.user.id]))

@login_required(login_url='login')
def time_record_list(request, user_id):
    time_records = TimeRecord.objects.filter(
        user=user_id).order_by('-record_date')
    context = {
        'time_records': time_records,
        'user_id': user_id
    }
    return render(request, 'employee/time_record_list.html', context)

# generating reports using reportlab

@login_required(login_url='login')
def generate_pdf_report(request, user_id):
    # Retrieve the employee using the provided ID
    employee = get_object_or_404(Employee, user=user_id)

    # Create a PDF response
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = f'attachment; filename="employee_report_{employee.employee_id}.pdf"'

    # Create the PDF document
    p = canvas.Canvas(response)

    # Add content to the PDF (employee details)
    p.drawString(100, 760, f'Employee Name: {employee.user.username}')
    p.drawString(100, 740, f'Email: {employee.email}')
    p.drawString(100, 720, f'Contact: {employee.contact}')
    p.drawString(100, 700, f'Department: {employee.department}')
    p.drawString(100, 700, f'Role ID: {employee.role}')
    # Add more details as needed

    # Close the PDF document
    p.showPage()
    p.save()
    return response

# payroll information
# view payment history
def view_payroll_info(request):
    employee = Employee.objects.get(user_id=request.user)
    try:
        payroll_info = PayrollInformation.objects.get(employee=employee)
    except PayrollInformation.DoesNotExist:
        payroll_info = None
    return render(request, 'employee/view_payroll_info.html', {'employee': employee, 'payroll_info': payroll_info})

def view_payment_history(request):
    employee = Employee.objects.get(user=request.user)
    payment_history = PaymentRecord.objects.filter(employee=employee)
    return render(request, 'employee/view_payment_history.html', {'employee': employee, 'payment_history': payment_history})


import argparse
import glob
import os
import pandas as pd

import clip
import h5py
import numpy as np
import torch
from PIL import Image
from tqdm import tqdm

import src.util.log_util as log
from src.util.time_util import timing

# Load the open CLIP model
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)

@timing
def encode_search_query(search_query: str):
    with torch.no_grad():
        # Encode and normalize the search query using CLIP
        text_encoded = model.encode_text(clip.tokenize(search_query).to(device))
        text_encoded /= text_encoded.norm(dim=-1, keepdim=True)
    return text_encoded.cpu().numpy()

def encode_image_batch(photos_batch: list):
    # Load all the photos from the files
    photos = [Image.open(photo_file) for photo_file in photos_batch]
    # Preprocess all photos
    photos_preprocessed = torch.stack([preprocess(photo) for photo in photos]).to(device)
    with torch.no_grad():
        # Encode the photos batch to compute the feature vectors and normalize them
        photos_features = model.encode_image(photos_preprocessed)
    photos_features /= photos_features.norm(dim=-1, keepdim=True)
    return photos_features.cpu().numpy()

@timing
def extract_new_images(images_path: str, photo_ids: str):
    with open(photo_ids) as f:
        photos = f.readlines()
    photos = [photo.rstrip() for photo in photos]
    list_of_images = get_images(images_path)
    new_list = list(set(list_of_images) - set(photos))
    if not new_list:
        log.info('No new images were found')
        return
    log.info(f'New {len(new_list)} images found. Starting to extract')
    encode_all_images(new_list, 8, photo_ids)

def get_images(images_path: str):
    list_of_images = glob.glob(f'{images_path}/*.jpg') +\
                     glob.glob(f'{images_path}/*.jpeg') + \
                     glob.glob(f'{images_path}/*.png') + glob.glob(f'{images_path}/*.JPG')

    return list_of_images

@timing
def encode_all_images(list_of_images: list, batch_size: int, photo_ids: str):
    length = len(list_of_images)
    assert length > 0
    with tqdm(total=length) as progress_bar:
        for i in range(0, len(list_of_images), batch_size):
            batch = list_of_images[i:i+batch_size]
            batch_features = encode_image_batch(batch)
            progress_bar.update(batch_size)
            np.save(f'features/batch_{i}.npy', batch_features)
            photo_ids_data = pd.DataFrame(batch)
            photo_ids_data.to_csv(f'features/batch_{i}.csv', index=False)

    log.info('Features extracted')
    csv_files = sorted(glob.glob("features/batch_*.csv"))
    photos_df = pd.concat([pd.read_csv(ids_file) for ids_file in csv_files])

    if os.path.exists(photo_ids):
        log.info('Merging previous photo_ids with the latest update')
        previous_photo_ids = pd.read_csv(photo_ids)
        photos_df = pd.concat([previous_photo_ids, photos_df])
    photos_df.to_csv(photo_ids, index=False, header=False)

    log.info('Merging batch features')
    batch_files = sorted(glob.glob('features/*.npy'))
    features = np.concatenate([np.load(features_file) for features_file in batch_files])

    log.info(f'Features computed of shape {features.shape}')
    if os.path.exists('features/features.h5'):
        log.info('Merging previous features with the latest update')
        with h5py.File('features/features.h5', 'r') as hf:
            previous_photo_features = hf['features'][:]
        features = np.concatenate([previous_photo_features, features])

    with h5py.File(f'features/features.h5', 'w') as hf:
        hf.create_dataset('features', data=features)

    # removing useless batch feature file
    for batch_file in batch_files:
        os.remove(batch_file)
    for csv_file in csv_files:
        os.remove(csv_file)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--batch_size', type=int, default=8)
    parser.add_argument('--images', type=str, required=True)
    parser.add_argument('--photo_ids', type=str, default='photo_ids.csv')
    args = parser.parse_args()

    assert os.path.exists(args.images)
    assert args.batch_size > 0

    os.makedirs('features', exist_ok=True)
    log.info(f'Starting to process all the images from {args.images}')
    list_of_images = get_images(args.images)
    encode_all_images(list_of_images, args.batch_size, args.photo_ids)


import os
from flask import Flask, request, abort, jsonify
from flask_cors import CORS
import json

from sqlalchemy.sql.expression import null

from models import db_drop_and_create_all, setup_db, Actor, Movie, CastList, db
from .auth.auth import AuthError, requires_auth

def create_app(test_config=None):
  # create and configure the app
  app = Flask(__name__)
  setup_db(app)
  #app.config.from_object('config')
  CORS(app)

  @app.route('/actors')
  def get_all_actors():
    selection = Actor.query.all()
    actors = [actor.short() for actor in selection]
    return jsonify({
      'success': True,
      'actors': actors
    })

  @app.route('/actors', methods=['POST'])
  @requires_auth('post:actors')
  def create_actor(payload):
    request_data = request.get_json()
    if request_data is None:
      abort(400)
    try:
      new_name = request_data.get('name')
      new_age = request_data.get('age')
      new_image_link = request_data.get('image_link')
      new_imdb_link = request_data.get('imdb_link')
      new_gender = request_data.get('gender')
      new_seeking_roles = request_data.get('seeking_roles')
      new_seeking_description = request_data.get('seeking_description')
      actor = Actor(name=new_name, age=new_age, image_link=new_image_link, imdb_link=new_imdb_link, gender=new_gender, seeking_roles=new_seeking_roles, seeking_description=new_seeking_description)
      actor.insert()
      return jsonify({'success': True, 'id': actor.id})
    except:
      abort(422)

  @app.route('/actors/<int:actor_id>', methods=['PATCH'])
  @requires_auth('patch:actors')
  def update_actor(payload, actor_id):
    request_data = request.get_json()
    actor = Actor.query.filter(Actor.id == actor_id).one_or_none()
    if actor is None:
        abort(404)
    try:
      if 'name' in request_data:
        new_name = request_data.get('name')
        actor.name = new_name
      if 'age' in request_data:
        new_age = request_data.get('age')
        actor.age = new_age
      if 'image_link' in request_data:
        new_image_link = request_data.get('image_link')
        actor.image_link = new_image_link
      if 'imdb_link' in request_data:
        new_imdb_link = request_data.get('imdb_link')
        actor.imdb_link = new_imdb_link
      if 'gender' in request_data:
        new_gender = request_data.get('gender')
        actor.gender = new_gender
      if 'seeking_roles' in request_data:
        new_seeking_roles = request_data.get('seeking_roles')
        actor.seeking_roles = new_seeking_roles
      if 'seeking_description' in request_data:
        new_seeking_description = request_data.get('seeking_description')
        actor.seeking_description = new_seeking_description
      actor.update()
      return jsonify({'success': True, 'actor': actor.short()})
    except:
      abort(422)

  @app.route('/actors/<int:actor_id>')
  def get_actor_details(actor_id):
    selection = Actor.query.filter(Actor.id == actor_id).one_or_none()
    if selection is None:
      abort(404)
    actor = selection.format()

    return jsonify({'success': True, 'actor': actor})

  @app.route('/actors/<int:actor_id>', methods=['DELETE'])
  @requires_auth('delete:actors')
  def delete_actor(payload, actor_id):
    actor = Actor.query.filter(Actor.id == actor_id).one_or_none()
    if actor is None:
      abort(404)
    deleted_id = actor.id
    actor.delete()
    return jsonify({'success': True, 'id': deleted_id})

  @app.route('/actors/search', methods=['POST'])
  def search_actors():
    request_data = request.get_json()
    try:
      data = []
      query_result = Actor.query.filter(Actor.name.ilike('%' + request_data.get('search_term') + '%')).all()
      for actor in query_result:
        data.append(actor.short())
      response = {
        "success": True,
        "count": len(data),
        "actors": data
      }
      return response;
    except:
      abort(422)

  @app.route('/movies')
  def get_all_movies():
    selection = Movie.query.all()
    movies = [movie.short() for movie in selection]
    return jsonify({
      'success': True,
      'movies': movies
    })

  @app.route('/movies/<int:movie_id>')
  def get_movie_details(movie_id):
    selection = Movie.query.filter(Movie.id == movie_id).one_or_none()
    if selection is None:
      abort(404)
    return jsonify({'success': True, 'movie': selection.format()})

  @app.route('/movies/<int:movie_id>', methods=['DELETE'])
  @requires_auth('delete:movies')
  def delete_movie(payload, movie_id):
    movie = Movie.query.filter(Movie.id == movie_id).one_or_none()
    if movie is None:
      abort(404)
    deleted_id = movie.id
    movie.delete()
    return jsonify({'success': True, 'id': deleted_id})

  @app.route('/movies', methods=['POST'])
  @requires_auth('post:movies')
  def create_movie(payload):
    request_data = request.get_json()
    if request_data is None:
      abort(400)
    try:
      new_title = request_data.get('title')
      if 'image_link' in request_data:
        new_image_link = request_data.get('image_link')
      else:
        new_image_link = None
      if 'imdb_link' in request_data:
        new_imdb_link = request_data.get('imdb_link')
      else:
        new_imdb_link = None
      if 'release_date' in request_data:
        new_release_date = request_data.get('release_date')
      else:
        new_release_date = None
      if 'seeking_description' in request_data:
        new_seeking_description = request_data.get('seeking_description')
      else:
        new_seeking_description = None
      new_seeking_talent = request_data.get('seeking_talent')
      movie = Movie(title=new_title, image_link=new_image_link, imdb_link=new_imdb_link, \
        release_date=new_release_date, seeking_talent=new_seeking_talent, seeking_description=new_seeking_description)
      movie.insert()
      return jsonify({'success': True, 'id': movie.id})
    except:
      abort(422)

  @app.route('/movies/<int:movie_id>', methods=['PATCH'])
  @requires_auth('patch:movies')
  def update_movie(payload, movie_id):
    request_data = request.get_json()
    movie = Movie.query.filter(Movie.id == movie_id).one_or_none()
    if movie is None:
      abort(404)
    try:
      if 'title' in request_data:
        new_title = request_data.get('title')
        movie.title = new_title
      if 'image_link' in request_data:
        new_image_link = request_data.get('image_link')
        movie.image_link = new_image_link
      if 'imdb_link' in request_data:
        new_imdb_link = request_data.get('imdb_link')
        movie.imdb_link = new_imdb_link
      if 'release_date' in request_data:
        new_release_date = request_data.get('release_date')
        movie.release_date = new_release_date
      if 'seeking_talent' in request_data:
        new_seeking_talent = request_data.get('seeking_talent')
        movie.seeking_talent = new_seeking_talent
      if 'seeking_description' in request_data:
        new_seeking_description = request_data.get('seeking_description')
        movie.seeking_description = new_seeking_description
      movie.update()
      return jsonify({'success': True, 'movie': movie.short()})
    except:
      abort(422)

  @app.route('/movies/search', methods=['POST'])
  def search_movies():
    request_data = request.get_json()
    try:
      data = []
      query_result = Movie.query.filter(Movie.title.ilike('%' + request_data.get('search_term') + '%')).all()
      for movie in query_result:
        data.append(movie.format())
      response = {
        "success": True,
        "count": len(query_result),
        "movies": data
      }
      return response
    except:
      abort(422)

  @app.route('/castlist/actors/<int:actor_id>')
  def get_movies_for_actor(actor_id):
    data = []
    query_result = db.session.query(CastList.id, CastList.actor_id, CastList.movie_id, Movie.title, Movie.image_link)\
      .join(Movie, CastList.movie_id == Movie.id)\
      .filter(CastList.actor_id == actor_id)\
      .all()
    for item in query_result:
      obj = {
        "title": item.title,
        "image_link": item.image_link,
        "id": item.movie_id
      }
      data.append(obj)
    return jsonify({
      'success': True,
      'count': len(data),
      'movies': data
    })

  @app.route('/castlist/movies/<int:movie_id>')
  def get_cast_for_movie(movie_id):
    data = []
    query_result = db.session.query(CastList.id, CastList.actor_id, CastList.movie_id, Actor.name, Actor.image_link)\
      .join(Actor, CastList.actor_id == Actor.id)\
      .filter(CastList.movie_id == movie_id)\
      .all()
    for item in query_result:
      print(item)
      obj = {
        "name": item.name,
        "image_link": item.image_link,
        "id": item.actor_id
      }
      data.append(obj)
    return jsonify({
      'success': True,
      'count': len(data),
      'actors': data
    })

  @app.route('/castlist', methods=['POST'])
  @requires_auth('post:castlists')
  def create_castlist(payload):
    request_data = request.get_json()
    if request_data is None:
      abort(400)
    try:
      new_actor_id = request_data.get('actor_id')
      new_movie_id = request_data.get('movie_id')
      # No duplicates
      castlist_query = CastList.query.filter(CastList.movie_id == new_movie_id, CastList.actor_id == new_actor_id).one_or_none()
      if castlist_query is not None:
        abort(422)
      castlist = CastList(actor_id=new_actor_id, movie_id=new_movie_id)
      castlist.insert()
      return jsonify({'success': True})
    except:
      abort(422)

  @app.route('/castlist', methods=['DELETE'])
  @requires_auth('delete:castlists')
  def delete_castlist(payload):
    request_data = request.get_json()
    try:
      new_actor_id = request_data.get('actor_id')
      new_movie_id = request_data.get('movie_id')
    except:
      abort(400)
    # No duplicates
    castlist = CastList.query.filter(CastList.movie_id == new_movie_id, CastList.actor_id == new_actor_id).one_or_none()
    if castlist is None:
      abort(404)
    try:
      deleted_id = castlist.id
      castlist.delete()
      return jsonify({'success': True, 'id': deleted_id})
    except:
      abort(422)

  """ Error Handlers """
  @app.errorhandler(404)
  def not_found(error):
    return jsonify({
      'success': False,
      'error': 404,
      'message': 'resource not found'
    }), 404

  @app.errorhandler(422)
  def unprocessable(error):
    return jsonify({
      'success': False,
      'error': 422,
      'message': 'unprocessable'
    }), 422

  @app.errorhandler(405)
  def method_not_allowed(error):
    return jsonify({
      'success': False,
      'error': 405,
      'message': 'method not allowed'
    }), 405

  @app.errorhandler(400)
  def bad_request(error):
    return jsonify({
      "success": False,
      "error": 400,
      "message": "bad request"
      }), 400

  @app.errorhandler(500)
  def interal_server_error(error):
    return jsonify({
      "success": False,
      "error": 500,
      "message": "internal server error"
      }), 500

  @app.errorhandler(AuthError)
  def auth_error(error):
    return jsonify({
      'success': False,
      'error': error.status_code,
      'message': error.error.get('description')
    }), error.status_code

  return app


# -*- coding: UTF-8 -*-

from functools import reduce

'''
求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字。
例如2+22+222+2222+22222(此时共有5个数相加)，几个数相加由键盘控制。
'''

m= 0
n = []
a = int(input("qingshuru:"))
b = int(input("qingshuru:"))

for i in range(a):
    m += b
    b = b * 10
    n.append(m)
    print(n[i])

print(reduce(lambda x,y:x+y,n))

a = input("数字为：")
b = int(input("个数为："))
s=''
c= []
sum = 0
for j in range(b):
    s += a
    c.append(s)
print(c)

for k in c:
    sum+=int(k)
print(sum)


from twin import twin
import time
import numpy as np
import matplotlib.pyplot as plt
import scipy.sparse as sp
from scipy.sparse.linalg import eigsh
import pycuda.autoinit
import pycuda.gpuarray as gpuarray
from skcuda import linalg
plt.style.use('ilya_plot')

class twin_cuda(twin):
    """
    Adds GPU functionality to the twin class
    """

    def __init__(self, alpha, assymetry, states_per_island, flux_points, plot_or_not, message_or_not):
        """
        __ Parameters __
        EC:     charging energy
        EJ:     josephson energy
        alpha:  central island
        assymetry: assymetry between two loops
        states_per_island: accuracy of simulation
        plot_or_not: show plot output or not

        __ Description __
        Perform the general setup
        - measured in nm
        - working with frequencies (normalised by hbar)
        - working in unit of Phi0
        """

        twin.__init__(self, alpha, assymetry, states_per_island,
                      flux_points, plot_or_not, message_or_not)
        linalg.init()

    def simulate(self):
        """
        __ Parameters __

        __ Description __
        Method performs the eigenvalue simulations:
        1 - finish building the Hamiltonian, with the flux-dependent elements
        2 - evaluate the lowest eigenstate and eigenenergies of the Hamiltonian
        3 - plot out the spectrum

        """

        # 0 - prepare hamiltonian for this simulation
        self.prepare_hamiltonian_stage2()

        if (self.message_or_not):
            print("==> 'simulate' running")

        self.spectrum_eigvals = []
        self.spectrum_eigvecs = []
        self.spectrum_simulation_12 = []
        self.spectrum_simulation_23 = []

        op_H_row = self.op_H_row.copy()
        op_H_col = self.op_H_col.copy()
        op_H_row.extend(self.op_H_phi_row)
        op_H_row.extend(self.op_H_phiAss_row)
        op_H_col.extend(self.op_H_phi_col)
        op_H_col.extend(self.op_H_phiAss_col)

        # 1 - simulation performed for each value of the external flux
        for ext_flux_number in range(0, len(self.flux_list)):

            # 2 - extraction of the onset phase, due to external flux
            phi_external = (self.flux_list[ext_flux_number]) * 2 * np.pi
            phi_externalAss = phi_external * self.assymetry

            ####################
            # 3 - FINISH MAIN HAMILTONIAN for this particular bias
            ####################
            # a - copy base elements
            op_H_elm = self.op_H_elm.copy()

            # b - add on the phase dependent elements
            op_H_elm.extend(self.prepare_hamiltonian_stage3(
                phi_external, phi_externalAss))

            if((len(op_H_row) != len(op_H_elm)) or (len(op_H_col) != len(op_H_elm))):
                self.raise_error("Hamiltonin lists have %i rows, %i columns  and only %i elements" % (
                    len(op_H_row), len(op_H_col), len(op_H_elm)))

            # 2 - construct numpy matrix (using intermediate sparse matrix)
            self.op_H = sp.coo_matrix((op_H_elm,
                                       (op_H_row, op_H_col))).tocsr()
            gpu_array = self.op_H.toarray(order="F")
            gpu_array = gpuarray.to_gpu(gpu_array)

            # 3 - evaluate 3 lowest eigenenergies and eigenvectors, |1> |2> |3>
            eigvecs, eigvals = linalg.eig(gpu_array, jobvl='N', jobvr='V')
            self.spectrum_eigvals.append(eigvals)
            self.spectrum_simulation_12.append([eigvals[1] - eigvals[0]])
            self.spectrum_simulation_23.append([eigvals[2] - eigvals[1]])

            # self.track_progress(ext_flux_number, len(
            #     self.flux_list), 20, False)

        # 4 - finalise arrays
        self.spectrum_eigvals = np.array(self.spectrum_eigvals)
        self.spectrum_simulation_12 = np.array(self.spectrum_simulation_12)
        self.spectrum_simulation_23 = np.array(self.spectrum_simulation_23)

        if (self.message_or_not):
            print("==> 'simulate' finished")

if __name__ == "__main__":
    print("\nRunning 'twin_cuda.py'\n")
    start = time.time()
    EC = 31
    EJ = 22
    alpha = 1.023
    assymetry = 1.011
    test = twin_cuda(alpha, assymetry, 5, 300, False)
    test.override_parameters(EC, EJ, test.alpha, test.assymetry)
    test.experimental_data_load(test.ax, True)
    test.simulate()
    print(test.experimental_data_error())
    end = time.time()
    print(end - start)


import Chemins
from Utils import UTILS_Adaptations
from Utils.UTILS_Traduction import _
import wx
from Ctrl import CTRL_Bouton_image
import sys
import sqlite3

class SaisiePays(wx.Panel):
    def __init__(self, parent, mode="pays"):
        """ Selection d'un pays ou d'une nationalite """
        wx.Panel.__init__(self, parent, id=-1, style=wx.TAB_TRAVERSAL)
        self.parent = parent
        self.mode = mode # Mode = "pays" ou "nationalite"
        self.IDpays = None
        self.image_pays = wx.StaticBitmap(self, -1, wx.Bitmap(Chemins.GetStaticPath("Images/Drapeaux/france.png"), wx.BITMAP_TYPE_PNG), size=(22, 20))
        self.bouton_pays = wx.Button(self, -1, "...", size=(20, 20))

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.OnBoutonPays, self.bouton_pays)

        self.SetValue(100)

    def __set_properties(self):
        pass

    def __do_layout(self):
        grid_sizer_base = wx.FlexGridSizer(rows=1, cols=2, vgap=5, hgap=5)
        grid_sizer_base.Add(self.image_pays, 0, 0, 0)
        grid_sizer_base.Add(self.bouton_pays, 0, 0, 0)
        self.SetSizer(grid_sizer_base)
        grid_sizer_base.Fit(self)
        self.Layout()

    def SetValue(self, IDpays=None, nomPays=None):
        """ Recherche par l'IDpays ou le nom du pays """
        if IDpays == None and nomPays == None : return
        if IDpays != None : pays = self.Recherche_Pays(IDpays=IDpays)
        if nomPays != None : pays = self.Recherche_Pays(nomPays=nomPays)
        self.IDpays = pays[0]
        self.image_pays.SetBitmap(wx.Bitmap(Chemins.GetStaticPath("Images/Drapeaux/" + pays[1] + ".png"), wx.BITMAP_TYPE_PNG))
        self.image_pays.SetToolTip(wx.ToolTip(_(u"Pays de naissance : ") + pays[2]))
        if self.mode == "pays" :
            # Mode pays
            self.bouton_pays.SetToolTip(wx.ToolTip(_(u"Cliquez ici pour sélectionner un autre pays de naissance")))
            self.image_pays.SetToolTip(wx.ToolTip(_(u"Pays de naissance : %s") % pays[2]))
        else:
            # Mode nationalité
            self.image_pays.SetToolTip(wx.ToolTip(_(u"Nationalité : %s") % pays[3]))
            self.bouton_pays.SetToolTip(wx.ToolTip(_(u"Cliquez ici pour sélectionner une autre nationalité")))

    def GetValue(self):
        return self.IDpays

    def Recherche_Pays(self, IDpays=0, nomPays=""):
        """ Récupération de la liste des pays dans la base """
        con = sqlite3.connect(Chemins.GetStaticPath("Databases/Geographie.dat"))
        cur = con.cursor()
        if nomPays == "" :
            req = "SELECT IDpays, code_drapeau, nom, nationalite FROM pays WHERE IDpays=%d" % IDpays
        else:
            req = "SELECT IDpays, code_drapeau, nom, nationalite FROM pays WHERE nom='%s'" % nomPays
        cur.execute(req)
        listePays = cur.fetchall()
        con.close()
        if len(listePays) == 0 : return
        return listePays[0]

    def OnBoutonPays(self, event):
        from Dlg import DLG_Saisie_pays
        dlg = DLG_Saisie_pays.Dialog_pays(None, typeSelection=self.mode)
        if dlg.ShowModal() == wx.ID_OK:
            IDpays = dlg.GetIDpays()
            self.SetValue(IDpays=IDpays)
        dlg.Destroy()

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        panel = wx.Panel(self, -1)
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_1.Add(panel, 1, wx.ALL|wx.EXPAND)
        self.SetSizer(sizer_1)
        self.ctrl= SaisiePays(panel)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_2.Add(self.ctrl, 1, wx.ALL|wx.EXPAND, 4)
        panel.SetSizer(sizer_2)
        self.Layout()
        self.CentreOnScreen()

if __name__ == '__main__':
    app = wx.App(0)
    #wx.InitAllImageHandlers()
    frame_1 = MyFrame(None, -1, "TEST", size=(800, 400))
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()


from set_seed import random_ctl
seed = random_ctl()

from fastai.text import *
from fastai.callbacks import SaveModelCallback
from fastai.layers import LabelSmoothingCrossEntropy

import sentencepiece as spm #https://github.com/google/sentencepiece
import fire

from sp_tok import *

def split_data(all_texts_df:DataFrame, split_seed:int=None):
    if split_seed: np.random.seed(split_seed)
    idx = np.random.permutation(len(all_texts_df))
    test_cut = int(0.15 * len(idx))
    valid_cut = int(0.15 * len(idx-test_cut))

    df_train_all = all_texts_df.iloc[idx[:-(valid_cut+test_cut)],:]
    df_valid     = all_texts_df.iloc[idx[ -(valid_cut+test_cut):-test_cut],:]
    df_test      = all_texts_df.iloc[idx[-test_cut:],:]

    return df_train_all, df_valid, df_test

def fit_clas(model_path:str, sp_model:str,
             qrnn:bool=True, n_hid:int=2304, load_enc:str=None, split_seed:int=None):
    PATH = Path(model_path)
    torch.backends.cudnn.enabled=False

    defaults.text_spec_tok.append(NL) #add a New Line special char
    sp_vocab = Vocab( get_itos(sp_model) )
    mycust_tok = CustomTokenizer(SPTokenizer,sp_model,pre_rules=default_rules)

    all_texts_df = pd.read_csv('../data/haha_2019_train.csv')
    all_texts_df.funniness_average.fillna(0,inplace=True)
    raw_text = all_texts_df.loc[:,'text']

    print("Default Rules:\n",[x.__name__ for x in default_rules],"\n\n")
    for rule in default_rules: raw_text = raw_text.apply(lambda x: rule(str(x)))
    all_texts_df['new_text'] = raw_text #databunch adds `xxbos` so don't add here

    df_train,df_valid,df_test = split_data(all_texts_df, split_seed=split_seed)

    data = TextClasDataBunch.from_df(PATH,df_train,df_valid,df_test,
                                   tokenizer=mycust_tok, vocab=sp_vocab,
                                   text_cols='new_text', label_cols='funniness_average')
    config = awd_lstm_clas_config.copy()
    config['qrnn'] = qrnn
    config['n_hid'] = n_hid
    print(config)
    learn = text_classifier_learner(data, AWD_LSTM, drop_mult=0.7,pretrained=False,config=config)
    if load_enc : learn.load_encoder(load_enc)
    learn.unfreeze()

    learn.fit_one_cycle(10, slice(1e-2/(2.6**4),1e-2), moms=(0.7,0.4), pct_start=0.25, div_factor=8.,
                        callbacks=[SaveModelCallback(learn,every='improvement',mode='min',
                                                    name='best_vloss_model_Q')])
    learn.save(f'haha_regr_bd_{seed}')
    print(f"Reg RndSeed: {seed},{min(learn.recorder.val_losses)}")

if __name__ == "__main__":
    fire.Fire(fit_clas)


"""
This script is to check the health of the rchain service.

You have to config a file with server you want to check health, like below

servers:
  - http://node0.testnet.rchain-dev.tk:40403
  - http://node1.testnet.rchain-dev.tk:40403
  - http://node2.testnet.rchain-dev.tk:40403
  - http://node3.testnet.rchain-dev.tk:40403
  - http://observer.testnet.rchain.coop:40403
"""
import logging
import sys
import yaml
from argparse import ArgumentParser
import json
import time
import requests

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
root = logging.getLogger()
formatter = logging.Formatter("%(asctime)s-%(levelname)s-%(message)s")
handler.setFormatter(formatter)
root.addHandler(handler)
root.setLevel(logging.INFO)

parser = ArgumentParser(description="reporting performance test")
parser.add_argument("--config", action="store", type=str, required=True, dest="config")
args = parser.parse_args()

with open(args.config) as f:
    config = yaml.load(f)

def main():
    result = []
    for server in config['servers']:
        resp = requests.get(server+"/api/blocks/1")
        body = json.loads(resp.text)
        block = body[0]
        number = block['blockNumber']
        result.append((server, number))
    result.sort(key=lambda x:x[1], reverse=True)
    for re in result:
        logging.info("server {} latest block number is {}".format(re[0], re[1]))

if __name__ == '__main__':
    main()


#!/usr/bin/env python

from PIL import Image
import sys
import os

if len(sys.argv) < 2:
    print("usage: {} <image>".format(sys.argv[0]))

image = Image.open(sys.argv[1], 'r')
image = image.convert('RGBA')

def get_unique_colors(image):
    unique_colors = set()

    for i in range(image.size[0]):
        for j in range(image.size[1]):
            color = image.getpixel((i, j))
            if color == (0, 0, 0, 0):
                continue

            unique_colors.add(color)

    return unique_colors

def color_average(color):
    return sum(list(color)[:3])//3

def color_difference(brighter, darker):
    return (brighter[0] - darker[0], brighter[1] - darker[1], brighter[2] - darker[2])

unique_colors = list(get_unique_colors(image))
unique_colors.sort(key=color_average)
print("unique colors: ", unique_colors)

def filter_image(image, target_color, replacement_color):
    for i in range(image.size[0]):
        for j in range(image.size[1]):
            color = image.getpixel((i, j))
            if color == target_color:
                image.putpixel((i, j), replacement_color)
            else:
                image.putpixel((i, j), (0, 0, 0, 0))

    return image

def append_before_file_name(filename, data):
    name, ext = os.path.splitext(filename)
    return name + data + ext

unique_colors_len = len(unique_colors)
if unique_colors_len == 2:
    lightest_color = unique_colors[1]
    lightest_base_color = unique_colors[0]
    darkest_color = unique_colors[0]
    darkest_base_color = unique_colors[1]
elif unique_colors_len == 3:
    lightest_color = unique_colors[2]
    lightest_base_color = unique_colors[1]
    darkest_color = unique_colors[0]
    darkest_base_color = unique_colors[1]

lightest_color_difference = color_difference(lightest_color, lightest_base_color)
darkest_color_difference = color_difference(darkest_base_color, darkest_color)

print("lightest_color:", lightest_color)
print("lightest_base_color:", lightest_base_color)
print("lightest_color_difference:", lightest_color_difference)
print("darkest_color:", darkest_color)
print("darkest_base_color:", darkest_base_color)
print("darkest_color_difference:", darkest_color_difference)

highlights_image = filter_image(image.copy(), lightest_color, lightest_color_difference)
highlights_image_name = append_before_file_name(sys.argv[1], '_highlights')
highlights_image.save(highlights_image_name)

shadows_image = filter_image(image.copy(), darkest_color, darkest_color_difference)
shadows_image_name = append_before_file_name(sys.argv[1], '_shadows')
shadows_image.save(shadows_image_name)


from plugins.utils import *
import requests, random

def execute(updates,msg):
    if 'action' in updates:
        if updates['action']['type'] == 'chat_invite_user':
            if updates['action']['member_id'] == -158856938:
                out = '''Спасибо за приглашение!
👉🏻 Пиши мне команды через / и пробел, например: / инфа Я тебя люблю
Все команды можно узнать с помощью / помощь
💬 Если ты дашь мне доступ к переписке, я стану откликаться на одно из своих имён в помощи: vk.cc/adfqEi'''
                apisay(out,msg['toho'])
            else:
                params = {'access_token':msg['config']['group_token'],'v':'5.103','user_ids':updates['action']['member_id']}
                name = requests.post('https://api.vk.com/method/users.get',data=params).json()['response'][0]
                name = '[id{0}|{1} {2}]'.format(name['id'],name['first_name'],name['last_name'])
                apisay('Добро пожаловать в беседу, '+name+'! Чтобы узнать команды бота пиши: \n\
                '+msg['config']['names'][0]+' помощь',msg['toho'])
        if updates['action']['type'] == 'chat_kick_user':
            params = {'access_token':msg['config']['group_token'],'v':'5.103','user_ids':updates['action']['member_id']}
            name = requests.post('https://api.vk.com/method/users.get',data=params).json()['response'][0]
            name = '[id{0}|{1} {2}]'.format(name['id'],name['first_name'],name['last_name'])
            apisay(name+' покинул беседу (ну и похуй)',msg['toho'])
    if msg['text'].split(' ')[0].lower() in ['/f','f']:
        apisay('F',msg['toho'],attachment='photo-158856938_457255856')

    if ('@all' in msg['text']) and (msg['userid'] not in [46488248,354255965,143568047]):
        apisay(' ',msg['toho'],
               photo=requests.get('https://sun9-10.userapi.com/c858432/v858432947/217f15/iWFQjlY1aJE.jpg').content
        )

    if 'nospeak' not in msg['dialogdata']['params']:
        if msg['cmdinfo']['iscommand'] == False and msg['cmdinfo']['isbotname'] == True:
            apisay('Команда не найдена :(', msg['toho'])
    for word in ['typescript','javascript']:
        if word in msg['text'].lower():
            apisay('жс гавно',msg['toho'],attachment='photo-158856938_457279263')
            break
    if msg['text'] == '/':
        apisay('Да-да?\n\
Чтобы узнать команды бота пропиши - / помощь',msg['toho'])
    if msg['text'] in ['выход','/выход','/exit','exit']:
        setcontext('main',msg['userid'],db)
        apisay('Выполнен принудительный выход из контекста юзером '+str(msg['userid']),msg['toho'])


#!/usr/bin/python
import os
from gi.repository import Gtk as gtk, AppIndicator3 as appindicator

def main():
    # localdevtray is the ID of the tray indicator. It must be unique for each tray icon.
    # applications-development-symbolic is the tray icon and can be changed by any icon of your system.
    indicator = appindicator.Indicator.new("localdevtray", "applications-development-symbolic", appindicator.IndicatorCategory.APPLICATION_STATUS)
    indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
    indicator.set_menu(menu())
    gtk.main()

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

    # OpenVPN option
    handle_vpn = gtk.CheckMenuItem(label="VPN connection")
    handle_vpn.set_active(is_vpn_active())
    handle_vpn.connect("activate", update_vpn)
    menu.append(handle_vpn)

    menu.append(gtk.SeparatorMenuItem())

    # MySQL option
    handle_mysql = gtk.CheckMenuItem(label="MySQL service")
    handle_mysql.set_active(is_mysql_active())
    handle_mysql.connect("activate", update_mysql)
    menu.append(handle_mysql)

    # Apache2 option
    handle_apache2 = gtk.CheckMenuItem(label="Apache2 service")
    handle_apache2.set_active(is_apache_active())
    handle_apache2.connect("activate", update_apache2)
    menu.append(handle_apache2)

    # PHP 7.2-fpm option
    handle_php72fpm = gtk.CheckMenuItem(label="PHP 7.2-fpm service")
    handle_php72fpm.set_active(is_php72fpm_active())
    handle_php72fpm.connect("activate", update_php72fpm)
    menu.append(handle_php72fpm)

    # PHP 7.4-fpm option
    handle_php74fpm = gtk.CheckMenuItem(label="PHP 7.4-fpm service")
    handle_php74fpm.set_active(is_php74fpm_active())
    handle_php74fpm.connect("activate", update_php74fpm)
    menu.append(handle_php74fpm)

    # PHP 8.1-fpm option
    handle_php81fpm = gtk.CheckMenuItem(label="PHP 8.1-fpm service")
    handle_php81fpm.set_active(is_php81fpm_active())
    handle_php81fpm.connect("activate", update_php81fpm)
    menu.append(handle_php81fpm)

    menu.append(gtk.SeparatorMenuItem())

    # Exit option
    exit_item = gtk.MenuItem(label="Exit")
    exit_item.connect("activate", quit)
    menu.append(exit_item)

    menu.show_all()
    return menu

def update_vpn(_):
    if _.get_active():
        os.system("systemctl start openvpn")
    else:
        os.system("systemctl stop openvpn")

def update_mysql(_):
    if _.get_active():
        os.system("systemctl start mysql")
    else:
        os.system("systemctl stop mysql")

def update_apache2(_):
    if _.get_active():
        os.system("systemctl start apache2")
    else:
        os.system("systemctl stop apache2")

def update_php72fpm(_):
    if _.get_active():
        os.system("systemctl start php7.2-fpm")
    else:
        os.system("systemctl stop php7.2-fpm")

def update_php74fpm(_):
    if _.get_active():
        os.system("systemctl start php7.4-fpm")
    else:
        os.system("systemctl stop php7.4-fpm")

def update_php81fpm(_):
    if _.get_active():
        os.system("systemctl start php8.1-fpm")
    else:
        os.system("systemctl stop php8.1-fpm")

def quit(_):
    gtk.main_quit()

def is_vpn_active():
    return os.system("systemctl status openvpn --no-pager") == 0

def is_mysql_active():
    return os.system("systemctl status mysql --no-pager") == 0

def is_apache_active():
    return os.system("systemctl status apache2 --no-pager") == 0

def is_php72fpm_active():
    return os.system("systemctl status php7.2-fpm --no-pager") == 0

def is_php74fpm_active():
    return os.system("systemctl status php7.4-fpm --no-pager") == 0

def is_php81fpm_active():
    return os.system("systemctl status php8.1-fpm --no-pager") == 0

if __name__ == "__main__":
    main()


from ROOT import TFile, TTree, TH1D, TH2D, gROOT, gStyle, TCanvas, kGreen, kRed, TCut, TPaveText, TLegend, kBlue, gPad, TLine, TF1, TGraph2D, TF2, TEllipse
gROOT.SetBatch(True)
gStyle.SetOptFit(1)
import numpy as np
import sys, os
import pandas as pd
import argparse
fitdir=os.path.dirname(os.path.abspath(__file__))+'/../'
import matplotlib.pyplot as plt
import seaborn as sns
from root_pandas import to_root
from scipy.interpolate import interp1d

def interpolated_intercepts(x, y1, y2):
    """Find the intercepts of two curves, given by the same x data"""

    def intercept(point1, point2, point3, point4):
        """find the intersection between two lines
        the first line is defined by the line between point1 and point2
        the first line is defined by the line between point3 and point4
        each point is an (x,y) tuple.

        So, for example, you can find the intersection between
        intercept((0,0), (1,1), (0,1), (1,0)) = (0.5, 0.5)

        Returns: the intercept, in (x,y) format
        """

        def line(p1, p2):
            A = (p1[1] - p2[1])
            B = (p2[0] - p1[0])
            C = (p1[0]*p2[1] - p2[0]*p1[1])
            return A, B, -C

        def intersection(L1, L2):
            D  = L1[0] * L2[1] - L1[1] * L2[0]
            Dx = L1[2] * L2[1] - L1[1] * L2[2]
            Dy = L1[0] * L2[2] - L1[2] * L2[0]

            x = Dx / D
            y = Dy / D
            return x,y

        L1 = line([point1[0],point1[1]], [point2[0],point2[1]])
        L2 = line([point3[0],point3[1]], [point4[0],point4[1]])

        R = intersection(L1, L2)

        return R

    idxs = np.argwhere(np.diff(np.sign(y1 - y2)) != 0)

    xcs = []
    ycs = []

    for idx in idxs:
        xc, yc = intercept((x[idx], y1[idx]),((x[idx+1], y1[idx+1])), ((x[idx], y2[idx])), ((x[idx+1], y2[idx+1])))
        xcs.append(xc)
        ycs.append(yc)
    return np.array(xcs), np.array(ycs)

def main():
    ######### - just to get the headers by reading one of the toy i.e. when seed = 1
    #non floated params stored in fit results
    nonparams  = []
    nonparams += ['loglh']
    nonparams += ['is_valid']
    nonparams += ['has_parameters_at_limit']
    nonparams += ['has_accurate_covar']
    nonparams += ['has_posdef_covar']
    nonparams += ['has_made_posdef_covar']
    #floated params in fit results
    floatedvars= []

    #make fres dictionary and fill floatedvars. here just get the keys for the fres dictionary. no content stored
    fres       = {}
    resfname   = direc+'results_'+floatWC+'_'+str(seed)+'_'+bscheme+'_'+str(nevnts)+'_'+'_'.join(floated_FF)+'_'+suffix+'_0.txt'
    if not os.path.isfile(resfname): print('File does not exist', resfname)
    fres['indx']  = []
    fres['wcval'] = []
    f = open(resfname, "r")
    for l in f.readlines():
        varname = l.split()[0]
        if varname not in nonparams:
            fres[varname+'_val']  = []
            fres[varname+'_err']  = []
            fres[varname+'_pull'] = []
            floatedvars += [varname]
        else:
            fres[varname]   = []

    f.close()
    ###########

    ######### - loop through the toys and fill fres dictionary for each toy
    wcvals = np.linspace(wcval_min, wcval_max, wcnvals)
    for i in range(len(wcvals)):
        #fit result file
        resfname = direc+'results_'+floatWC+'_'+str(seed)+'_'+bscheme+'_'+str(nevnts)+'_'+'_'.join(floated_FF)+'_'+suffix+'_'+str(i)+'.txt'
        if not os.path.isfile(resfname):
            print('File does not exist', resfname)
            continue

        #open the file and store the results into fres dictionary
        fres['indx']  += [i]
        fres['wcval'] += [wcvals[i]]
        f = open(resfname, "r")
        ls= f.readlines()
        for l in ls:
            varname = l.split()[0]
            if varname not in nonparams:
                tval    = true_val[varname]
                fval    = float(l.split()[1])
                ferr    = float(l.split()[2])
                pull    = -100.
                if ferr != 0.0: pull = (fval - tval)/ferr
                fres[varname+'_val'] += [fval]
                fres[varname+'_err'] += [ferr]
                fres[varname+'_pull']+= [pull]
            else:
                if varname == 'loglh':
                    fres[varname]  += [float(l.split()[1])]
                else:
                    fres[varname]  += [int(l.split()[1])]

    #convert the fres dictionary into pandas data frame and make some requirements
    df_fres       = pd.DataFrame.from_dict(fres)
    shape_before  = df_fres.shape
    print('Before requirements of valid minima and params in limit', shape_before)
    condvalid     = df_fres['is_valid'] == 1
    condparmlmt   = df_fres['has_parameters_at_limit'] == 0
    condacccov    = df_fres['has_accurate_covar'] == 1
    condposdefcov = df_fres['has_posdef_covar'] == 1
    condmadeposdef= df_fres['has_made_posdef_covar'] == 0
    cond          = np.logical_and(condvalid, condparmlmt)
    cond          = np.logical_and(cond, condacccov)
    cond          = np.logical_and(cond, condposdefcov)
    cond          = np.logical_and(cond, condmadeposdef)
    df_fres       = df_fres[cond]
    shape_after   = df_fres.shape
    print('After requirements of valid minima and params in limit', shape_after)
    #if shape_before[0] != shape_after[0]:
    #    raise Exception("Shapes before and after the requirement do not agree")

    x = df_fres[cond]['wcval'].to_numpy()
    y = df_fres[cond]['loglh'].to_numpy()
    #for xi, yi in zip(x,y): print(xi,yi)
    f2   = interp1d(x, y, kind='cubic')

    xnew    = np.linspace(np.min(x), np.max(x), 10*wcnvals)
    ynew    = f2(xnew) #curve
    y_minpt_sigma = np.min(ynew) + 0.5
    y_line  = y_minpt_sigma * np.ones_like(ynew) #line
    xcs, ycs= interpolated_intercepts(xnew,ynew,y_line)

    #idx = np.argwhere(np.diff(np.sign(ynew - y_line)) != 0)
    #plt.plot(x[idx], y1[idx], 'ms', ms=7, label='Nearest data-point method')
    y_minpt = np.min(ynew)
    x_minpt = xnew[ynew == np.min(ynew)][0]

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.set_ylabel('$-\log(L)$', fontsize=10)
    ax.set_xlabel(scan_wcname, fontsize=10)
    ax.scatter(x, y, color='b')
    ax.axvline(x_minpt, color='red', linestyle='--', alpha = 0.8, label = 'Min Val'.format(x_minpt))
    ax.axvline(0., color='black', linestyle='-', alpha = 0.8, label = 'True Val')
    ax.axvspan(xcs[0][0], xcs[1][0], alpha=0.3, color='red', label = '$68\%$ CI: ${0:.3f}^{{+{1:.3f}}}_{{-{2:.3f}}}$'.format(x_minpt, xcs[1][0] - x_minpt, x_minpt - xcs[0][0]))
    #for xc, yc in zip(xcs, ycs):
    #    print('intercept', xc, yc)
    #    ax.plot(xc, yc, 'o')
    #ax.plot(xnew, ynew, color='r')
    #ax.axhline(y_minpt_sigma, color='black', linestyle='--', alpha = 0.8)
    ax.legend(loc='best')
    print('Lower Bound: ', xcs[1][0] - x_minpt)
    print('Upper Bound: ', x_minpt - xcs[0][0])
    fig.tight_layout()
    pfname   = nlldir+'plot_scan_'+scan_wcname+'_'+floatWC+'_'+str(seed)+'_'+bscheme+'_'+str(nevnts)+'_'+'_'.join(floated_FF)+'_'+suffix+'_'+str(wcval_min).replace('.', 'pt')+'_'+str(wcval_max).replace('.', 'pt')+'_'+str(wcnvals)+'.pdf'
    print(pfname)
    fig.savefig(pfname, bbox_inches = 'tight')

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Arguments for plot_toy_results.py')
    parser.add_argument('-f'      , '--floatWC'     , dest='floatWC'      ,type=str,  default='None'    , help='(string) Name of the Wilson coefficient (WC) floated when fitting. Available options are [CVR,CSR,CSL,CT,None]. Default is None.')
    parser.add_argument('-s'      , '--seed'        , dest='seed'         ,type=int,  default=int(0)    , help='(int) Value of seed (an index for the toy ) used in the generation of fake/toy data. Default is zero.')
    parser.add_argument('-b'      , '--bscheme'     , dest='bscheme'      ,type=str,  default='Scheme2' , help='(string) Binning scheme to be used when fitting. Default is Scheme2. See BinningSchemes/Binning_Scheme.py.')
    parser.add_argument('-n'      , '--nevnts'      , dest='nevnts'       ,type=int,  default=int(7.5e6), help='(int) Size of the toy sample generated. Default is 7.5M events.')
    parser.add_argument('-d'      , '--direc'       , dest='direc'        ,type=str,  default='./plots/', help='(string) Directory in which the fit result (.txt) is saved. Default is current directory.')
    parser.add_argument('-e'      , '--floated_FF'  , dest='floated_FF'   ,nargs='+', default = ['None'], help="(list) List of form factor (FF) parameters floated in fit. See LbToLclnu_fit_binscheme.py. Default is ['None'].")
    parser.add_argument('-sf'     , '--suffix'      , dest='suffix'       ,type=str,  default='toy'     , help="(int) A unique suffix used to get the fit result file (*_suffix_*.txt). Default is 'toy'.")
    parser.add_argument('-swc'    , '--scan_wcname' , dest='scan_wcname'  ,type=str,  default='CVR'     , help='(string) Name of the Wilson coefficient (WC) that has been fixed to a certain value. Available options are [CVR,CSR,CSL,CT,None]. Default is CVR.')
    parser.add_argument('-wcmin'  , '--wcval_min'   , dest='wcval_min'    ,type=float,default=-1.       , help='(int) Min value of wc to be scanned. Default is -1.')
    parser.add_argument('-wcmax'  , '--wcval_max'   , dest='wcval_max'    ,type=float,default=-1.       , help='(int) Max value of wc to be scanned. Default is  1.')
    parser.add_argument('-wcnvals', '--wcnvals'     , dest='wcnvals'      ,type=int,  default=int(100)  , help='(int) Number of wc values used to do the scan. Default is 100.')

    args       = parser.parse_args()
    floatWC    = args.floatWC
    seed       = args.seed
    bscheme    = args.bscheme
    nevnts     = args.nevnts
    suffix     = args.suffix
    direc      = args.direc
    floated_FF = args.floated_FF
    scan_wcname= args.scan_wcname
    wcval_min  = args.wcval_min
    wcval_max  = args.wcval_max
    wcnvals    = args.wcnvals
    print(args)
    if not direc.endswith('/'): direc += '/'

    ######### set the true values used in the generation
    true_val = {}
    #WC values
    true_val["CVL"] = 0.0
    true_val["CVR"] = 0.0
    true_val["CSR"] = 0.0
    true_val["CSL"] = 0.0
    true_val[ "CT"] = 0.0
    #FF mean values
    f = open(fitdir+'/FF_cov/LambdabLambdac_results.dat', 'r')
    for l in f.readlines(): true_val[l.split()[0]] = float(l.split()[1])
    f.close()
    #true_val['a0gperp']      = true_val['a0gplus']
    #true_val['a0htildeperp'] = true_val['a0htildeplus']
    #print(true_val)
    #FF covariance matrix
    true_covariance = {}
    for n in list(true_val.keys()):
        if ('a0' in n) or ('a1' in n): true_covariance[n] = {}

    for l in open(fitdir+"/FF_cov/LambdabLambdac_covariance.dat", "r").readlines():
        true_covariance[l.split()[0]][l.split()[1]] = float(l.split()[2])

    #print(true_covariance)
    #########

    #make directories to store the plots
    basedir = direc+'plots/'
    nlldir  = basedir+'Nll/'
    if not os.path.exists(basedir):  os.system('mkdir '+basedir)
    if not os.path.exists(nlldir):   os.system('mkdir '+nlldir)
    main()


import json
import os
import time

import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
from dotenv import load_dotenv
from paho.mqtt import publish

load_dotenv()

host = os.getenv('HOST')
port = os.getenv('MQTT_BROKER_PORT')

dataset = {
    "name": "LED-Test-Device",
    "cmd": "",
}

GPIO.setmode(GPIO.BCM)

green = 27
red = 22
blue = 17

GPIO.setup(red, GPIO.OUT)
GPIO.setup(green, GPIO.OUT)
GPIO.setup(blue, GPIO.OUT)

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    # client.subscribe("$SYS/#")
    client.subscribe("#")

def on_message(client, userdata, msg):
    payload = msg.payload.decode("utf-8")
    if msg.topic in ["DataTopic", "CommandTopic", "ResponseTopic"]:
        data = json.loads(payload)
        print(f"[ {msg.topic} ] - {data}")
    if msg.topic == "CommandTopic":
        if data["method"] == "set":
            if data["cmd"] == "light-blue":
                GPIO.output(blue, data["light-blue"])
            elif data["cmd"] == "light-green":
                GPIO.output(green, data["light-green"])
            elif data["cmd"] == "light-red":
                GPIO.output(red, data["light-red"])

        publish.single("ResponseTopic", json.dumps(data), hostname=host, port=port)
    dataset = {
        "name": "LED-Test-Device",
        "cmd": "",
    }

try:
    GPIO.output(red, 0)
    GPIO.output(green, 0)
    GPIO.output(blue, 0)
    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message

    client.connect(host, port, 60)
    client.loop_forever()

except Exception as e:
    print(e)
except KeyboardInterrupt:
    print("keyboardInterrupt")
finally:
	GPIO.cleanup()


from django.db import models
from django.contrib.auth import get_user_model
# Create your models here.

User = get_user_model()

class Student(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    age = models.DecimalField(max_digits=100, decimal_places=2)
    grade = models.DecimalField(max_digits=5, decimal_places=1)


#
# @lc app=leetcode id=1557 lang=python3
#
# [1557] Minimum Number of Vertices to Reach All Nodes
#

# @lc code=start
# TAGS: Graph
import collections
import itertools
class Solution:
    # 1288 ms, 81.78 %. Time: O(V+E). Space: O(E)
    def findSmallestSetOfVertices(self, n: int, edges: List[List[int]]) -> List[int]:
        graph = collections.defaultdict(list)
        for f, t in edges:
            graph[f].append(t)

        tos = set(item for val in graph.values() for item in val)
        froms = set(graph.keys())
        return froms - tos

# @lc code=end


from typing import Optional

class TreeNode:
    def __init__(self, key, val, left_child=None, right_child=None, parent=None):
        self.key = key
        self.val = val
        self.parent = parent
        self.left_child = left_child
        self.right_child = right_child

class BinarySearchTree:

    def __init__(self):
        self.root = None
        self.size = 0

    def length(self):
        return self.size

    def __len__(self):
        return self.size

    def __iter__(self):
        return self.root.__iter__()

    def insert(self, key, val):
        if not self.root:
            self.root = TreeNode(key, val)
        else:
            # self._insert(self.root, key, val)
            self._insert_v2(self.root, None, key, val)

    def _insert_v2(self, current_node: TreeNode, parent, key, val):
        if current_node is None:
            return TreeNode(key, val, parent=parent)

        if key < current_node.key:
            current_node.left_child = self._insert_v2(current_node.left_child, current_node, key, val)
        else:
            current_node.right_child = self._insert_v2(current_node.right_child, current_node, key, val)
        return current_node

    def get(self, key) -> Optional[TreeNode]:
        node = self._get(self.root, key)
        return node if node is not None else None

    def _get(self, current_node, key) -> Optional[TreeNode]:
        if not current_node:
            return None
        if current_node.key == key:
            return current_node

        if key < current_node.key:
            return self._get(current_node.left_child, key)
        else:
            return self._get(current_node.right_child, key)

    def delete(self, key):
        # Need to handle root case here, when size is only one
        self._delete(self.root, key=key)

    def _delete(self, current_node, key):
        # When deleting you can choose either the successor of the node (leftmost in the right tree) or predesessor (rightmost in the left tree)
        if current_node is None:
            return None

        if current_node.key == key:
            # Find successor to parent
            if current_node.right_child is None:
                return current_node.left_child
            if current_node.left_child is None:
                return current_node.right_child

            next_lowest = current_node.right_child
            while next_lowest.left_child is not None:
                next_lowest = next_lowest.left_child

            current_node.key = next_lowest.key
            current_node.right_child = self._delete(current_node.right_child, next_lowest.key)
            return current_node

        if key < current_node.key:
            current_node.left_child = self._delete(current_node.left_child, key)
        else:
            current_node.right_child = self._delete(current_node.right_child, key)

        return current_node

def main() -> None:
    bst = BinarySearchTree()
    tests = [5, 30, 2, 40, 25, 4]
    for number in tests:
        bst.insert(number, 5)

    bst.delete(30)
    assert bst.root.right_child.key == 40

    bst = BinarySearchTree()
    tests = [17, 5, 35, 2, 11, 29, 38, 9, 16, 7, 8]
    for number in tests:
        bst.insert(number, 5)

    bst.delete(5)
    assert bst.root.left_child.key == 7
    assert bst.root.left_child.right_child.left_child.left_child.key == 8

    bst = BinarySearchTree()
    tests = [3, 6, 7, 5, 1]
    for number in tests:
        bst.insert(number, 5)

    x = bst
    bst.delete(6)
    assert bst.root.right_child.key == 7
    bst.delete(7)
    assert bst.root.right_child.key == 5
    bst.delete(5)
    assert bst.root.right_child is None

if __name__ == "__main__":
    main()


import logging
import random
from contextlib import contextmanager
from itertools import groupby
from typing import Dict, List, Optional, Tuple, Union

import torch
from packaging.version import parse as V
from torch.nn.utils.rnn import pad_sequence
from typeguard import check_argument_types

from espnet2.asr.ctc import CTC
from espnet2.asr.decoder.abs_decoder import AbsDecoder
from espnet2.asr.encoder.abs_encoder import AbsEncoder
from espnet2.asr.frontend.abs_frontend import AbsFrontend
from espnet2.asr.postencoder.abs_postencoder import AbsPostEncoder
from espnet2.asr.preencoder.abs_preencoder import AbsPreEncoder
from espnet2.asr.specaug.abs_specaug import AbsSpecAug
from espnet2.asr_transducer.utils import get_transducer_task_io
from espnet2.layers.abs_normalize import AbsNormalize
from espnet2.torch_utils.device_funcs import force_gatherable
from espnet2.train.abs_espnet_model import AbsESPnetModel
from espnet.nets.e2e_asr_common import ErrorCalculator as ASRErrorCalculator
from espnet.nets.e2e_mt_common import ErrorCalculator as MTErrorCalculator
from espnet.nets.pytorch_backend.nets_utils import th_accuracy
from espnet.nets.pytorch_backend.transformer.add_sos_eos import add_sos_eos
from espnet.nets.pytorch_backend.transformer.label_smoothing_loss import (  # noqa: H301
    LabelSmoothingLoss,
)

if V(torch.__version__) >= V("1.6.0"):
    from torch.cuda.amp import autocast
else:
    # Nothing to do if torch<1.6.0
    @contextmanager
    def autocast(enabled=True):
        yield

class ESPnetSTModel(AbsESPnetModel):
    """CTC-attention hybrid Encoder-Decoder model"""

    def __init__(
        self,
        vocab_size: int,
        token_list: Union[Tuple[str, ...], List[str]],
        frontend: Optional[AbsFrontend],
        specaug: Optional[AbsSpecAug],
        normalize: Optional[AbsNormalize],
        preencoder: Optional[AbsPreEncoder],
        encoder: AbsEncoder,
        hier_encoder: Optional[AbsEncoder],
        md_encoder: Optional[AbsEncoder],
        extra_mt_encoder: Optional[AbsEncoder],
        postencoder: Optional[AbsPostEncoder],
        decoder: AbsDecoder,
        extra_asr_decoder: Optional[AbsDecoder],
        extra_mt_decoder: Optional[AbsDecoder],
        ctc: Optional[CTC],
        st_ctc: Optional[CTC],
        st_joint_network: Optional[torch.nn.Module],
        src_vocab_size: Optional[int],
        src_token_list: Optional[Union[Tuple[str, ...], List[str]]],
        asr_weight: float = 0.0,
        mt_weight: float = 0.0,
        mtlalpha: float = 0.0,
        st_mtlalpha: float = 0.0,
        ignore_id: int = -1,
        tgt_ignore_id: int = -1,
        lsm_weight: float = 0.0,
        length_normalized_loss: bool = False,
        report_cer: bool = True,
        report_wer: bool = True,
        report_bleu: bool = True,
        sym_space: str = "<space>",
        sym_blank: str = "<blank>",
        tgt_sym_space: str = "<space>",
        tgt_sym_blank: str = "<blank>",
        extract_feats_in_collect_stats: bool = True,
        ctc_sample_rate: float = 0.0,
        tgt_sym_sos: str = "<sos/eos>",
        tgt_sym_eos: str = "<sos/eos>",
        lang_token_id: int = -1,
    ):
        assert check_argument_types()
        assert 0.0 <= asr_weight < 1.0, "asr_weight should be [0.0, 1.0)"
        assert 0.0 <= mt_weight < 1.0, "mt_weight should be [0.0, 1.0)"
        assert 0.0 <= mtlalpha <= 1.0, "mtlalpha should be [0.0, 1.0]"

        super().__init__()
        # note that eos is the same as sos (equivalent ID)
        if tgt_sym_sos in token_list:
            self.sos = token_list.index(tgt_sym_sos)
        else:
            self.sos = vocab_size - 1
        if tgt_sym_eos in token_list:
            self.eos = token_list.index(tgt_sym_eos)
        else:
            self.eos = vocab_size - 1
        self.src_sos = src_vocab_size - 1 if src_vocab_size else None
        self.src_eos = src_vocab_size - 1 if src_vocab_size else None
        self.vocab_size = vocab_size
        self.src_vocab_size = src_vocab_size
        self.ignore_id = ignore_id
        self.tgt_ignore_id = tgt_ignore_id
        self.asr_weight = asr_weight
        self.mt_weight = mt_weight
        self.mtlalpha = mtlalpha
        self.st_mtlalpha = st_mtlalpha
        self.token_list = token_list.copy()
        self.src_token_list = src_token_list.copy()
        self.ctc_sample_rate = ctc_sample_rate

        self.frontend = frontend
        self.specaug = specaug
        self.normalize = normalize
        self.preencoder = preencoder
        self.postencoder = postencoder
        self.hier_encoder = hier_encoder
        self.encoder = encoder
        if self.st_mtlalpha < 1.0:
            self.decoder = decoder
        elif decoder is not None:
            logging.warning(
                "Not using decoder because "
                "st_mtlalpha is set as {} (== 1.00)".format(st_mtlalpha),
            )
        self.md_encoder = md_encoder

        self.st_use_transducer_decoder = st_joint_network is not None

        if self.st_use_transducer_decoder:
            from warprnnt_pytorch import RNNTLoss

            self.st_joint_network = st_joint_network
            if tgt_sym_blank in token_list:
                self.blank_id = token_list.index(tgt_sym_blank)
            else:
                # OpenAI Whisper model doesn't <blank> token
                self.blank_id = 0
            self.st_criterion_transducer = RNNTLoss(
                blank=self.blank_id,
                fastemit_lambda=0.0,
            )
        else:
            self.criterion_st = LabelSmoothingLoss(
                size=vocab_size,
                padding_idx=tgt_ignore_id,
                smoothing=lsm_weight,
                normalize_length=length_normalized_loss,
            )

        if self.st_mtlalpha > 0.0:
            self.st_ctc = st_ctc

        self.criterion_asr = LabelSmoothingLoss(
            size=src_vocab_size,
            padding_idx=ignore_id,
            smoothing=lsm_weight,
            normalize_length=length_normalized_loss,
        )

        # submodule for ASR task
        if self.asr_weight > 0:
            assert (
                src_token_list is not None
            ), "Missing src_token_list, cannot add asr module to st model"
            if self.mtlalpha > 0.0:
                self.ctc = ctc
            if self.mtlalpha < 1.0:
                self.extra_asr_decoder = extra_asr_decoder
            elif extra_asr_decoder is not None:
                logging.warning(
                    "Not using extra_asr_decoder because "
                    "mtlalpha is set as {} (== 1.0)".format(mtlalpha),
                )

        # submodule for MT task
        # TODO(brian): this should be deprecated
        if self.mt_weight > 0:
            self.extra_mt_decoder = extra_mt_decoder
            self.extra_mt_encoder = extra_mt_encoder
        elif extra_mt_decoder is not None:
            logging.warning(
                "Not using extra_mt_decoder because "
                "mt_weight is set as {} (== 0)".format(mt_weight),
            )

        # MT error calculator
        if report_bleu:
            self.mt_error_calculator = MTErrorCalculator(
                token_list, tgt_sym_space, tgt_sym_blank, report_bleu
            )
        else:
            self.mt_error_calculator = None

        # ASR error calculator
        if self.asr_weight > 0 and (report_cer or report_wer):
            assert (
                src_token_list is not None
            ), "Missing src_token_list, cannot add asr module to st model"
            self.asr_error_calculator = ASRErrorCalculator(
                src_token_list, sym_space, sym_blank, report_cer, report_wer
            )
        else:
            self.asr_error_calculator = None

        self.extract_feats_in_collect_stats = extract_feats_in_collect_stats

        self.use_multidecoder = self.md_encoder is not None
        if hasattr(self, "decoder"):
            self.use_speech_attn = getattr(self.decoder, "use_speech_attn", False)
        else:
            self.use_speech_attn = None

        # TODO(jiatong): add multilingual related functions

        if lang_token_id != -1:
            self.lang_token_id = torch.tensor([[lang_token_id]])
        else:
            self.lang_token_id = None

    def forward(
        self,
        speech: torch.Tensor,
        speech_lengths: torch.Tensor,
        text: torch.Tensor,
        text_lengths: torch.Tensor,
        src_text: Optional[torch.Tensor] = None,
        src_text_lengths: Optional[torch.Tensor] = None,
        **kwargs,
    ) -> Tuple[torch.Tensor, Dict[str, torch.Tensor], torch.Tensor]:
        """Frontend + Encoder + Decoder + Calc loss

        Args:
            speech: (Batch, Length, ...)
            speech_lengths: (Batch,)
            text: (Batch, Length)
            text_lengths: (Batch,)
            src_text: (Batch, length)
            src_text_lengths: (Batch,)
            kwargs: "utt_id" is among the input.
        """
        assert text_lengths.dim() == 1, text_lengths.shape
        # Check that batch_size is unified
        assert (
            speech.shape[0]
            == speech_lengths.shape[0]
            == text.shape[0]
            == text_lengths.shape[0]
        ), (speech.shape, speech_lengths.shape, text.shape, text_lengths.shape)

        # additional checks with valid src_text
        if src_text is not None:
            assert src_text_lengths.dim() == 1, src_text_lengths.shape
            assert text.shape[0] == src_text.shape[0] == src_text_lengths.shape[0], (
                text.shape,
                src_text.shape,
                src_text_lengths.shape,
            )

        batch_size = speech.shape[0]

        text[text == -1] = self.tgt_ignore_id

        # for data-parallel
        text = text[:, : text_lengths.max()]
        if src_text is not None:
            src_text = src_text[:, : src_text_lengths.max()]

        # lang id for mbart
        if hasattr(self, "lang_token_id") and self.lang_token_id is not None:
            text = torch.cat(
                [
                    self.lang_token_id.repeat(text.size(0), 1).to(text.device),
                    text,
                ],
                dim=1,
            )
            text_lengths += 1

        # 1. Encoder
        if self.hier_encoder is not None or (
            self.postencoder is not None and self.postencoder.return_int_enc
        ):
            (
                st_encoder_out,
                st_encoder_out_lens,
                asr_encoder_out,
                asr_encoder_out_lens,
            ) = self.encode(speech, speech_lengths, return_int_enc=True)
        else:
            st_encoder_out, st_encoder_out_lens = self.encode(speech, speech_lengths)
            asr_encoder_out, asr_encoder_out_lens = st_encoder_out, st_encoder_out_lens

        # 2a. CTC branch
        if self.asr_weight > 0:
            assert src_text is not None, "missing source text for asr sub-task of ST"

        if self.asr_weight > 0 and self.mtlalpha > 0:
            loss_asr_ctc, cer_asr_ctc = self._calc_asr_ctc_loss(
                asr_encoder_out, asr_encoder_out_lens, src_text, src_text_lengths
            )
        else:
            loss_asr_ctc, cer_asr_ctc = 0.0, None

        if self.st_mtlalpha > 0:
            if self.postencoder is not None and self.postencoder.return_int_enc:
                # run ST CTC without post-encoder downsampling (no hier enc)
                loss_st_ctc, bleu_st_ctc = self._calc_mt_ctc_loss(
                    asr_encoder_out, asr_encoder_out_lens, text, text_lengths
                )
            else:
                loss_st_ctc, bleu_st_ctc = self._calc_mt_ctc_loss(
                    st_encoder_out, st_encoder_out_lens, text, text_lengths
                )
        else:
            loss_st_ctc, bleu_st_ctc = 0.0, None

        # 2b. Attention-decoder branch (extra ASR)
        if self.asr_weight > 0 and self.mtlalpha < 1.0:
            (
                loss_asr_att,
                acc_asr_att,
                cer_asr_att,
                wer_asr_att,
                hs_dec_asr,
            ) = self._calc_asr_att_loss(
                asr_encoder_out,
                asr_encoder_out_lens,
                src_text,
                src_text_lengths,
                self.use_multidecoder,
            )
        else:
            loss_asr_att, acc_asr_att, cer_asr_att, wer_asr_att = 0.0, None, None, None

        # 2c. Attention-decoder branch (extra MT)
        if self.mt_weight > 0:
            mt_encoder_out, mt_encoder_out_lens = self.extra_mt_encoder(
                src_text, src_text_lengths
            )
            loss_mt_att, acc_mt_att, bleu_mt_att = self._calc_mt_att_loss(
                mt_encoder_out,
                mt_encoder_out_lens,
                text,
                text_lengths,
                None,
                None,
                st=False,  # uses same decoder as ST
            )
            # loss_mt_att, acc_mt_att = self._calc_mt_att_loss(
            #     st_encoder_out, st_encoder_out_lens, text, text_lengths, st=False
            # )
        else:
            loss_mt_att, acc_mt_att = 0.0, None

        # 2d. Multi-Decoder encoder
        if self.use_speech_attn:
            speech_out = st_encoder_out
            speech_lens = st_encoder_out_lens
        else:
            speech_out = None
            speech_lens = None

        if self.use_multidecoder:
            dec_asr_lengths = src_text_lengths + 1
            st_encoder_out, st_encoder_out_lens, _ = self.md_encoder(
                hs_dec_asr, dec_asr_lengths
            )

        st_ctc_weight = self.st_mtlalpha
        if st_ctc_weight < 1.0:
            if self.st_use_transducer_decoder:
                # 2e. Transducer decoder branch
                (
                    loss_st_trans,
                    _,
                    _,
                ) = self._calc_st_transducer_loss(
                    st_encoder_out,
                    st_encoder_out_lens,
                    text,
                )

                if st_ctc_weight == 1.0:
                    loss_st = loss_st_ctc
                elif st_ctc_weight == 0.0:
                    loss_st = loss_st_trans
                else:
                    loss_st = (
                        st_ctc_weight * loss_st_ctc
                        + (1 - st_ctc_weight) * loss_st_trans
                    )
                loss_st_att = 0.0
                acc_st_att = None
                bleu_st_att = None
            else:
                # 2e. Attention-decoder branch (ST)
                loss_st_att, acc_st_att, bleu_st_att = self._calc_mt_att_loss(
                    st_encoder_out,
                    st_encoder_out_lens,
                    text,
                    text_lengths,
                    speech_out,
                    speech_lens,
                    st=True,
                )

                if st_ctc_weight == 1.0:
                    loss_st = loss_st_ctc
                elif st_ctc_weight == 0.0:
                    loss_st = loss_st_att
                else:
                    loss_st = (
                        st_ctc_weight * loss_st_ctc + (1 - st_ctc_weight) * loss_st_att
                    )
                loss_st_trans = 0.0
        else:
            loss_st = loss_st_ctc
            loss_st_att = 0.0
            acc_st_att = None
            bleu_st_att = None
            loss_st_trans = 0.0

        # 3. Loss computation
        asr_ctc_weight = self.mtlalpha

        if asr_ctc_weight == 1.0:
            loss_asr = loss_asr_ctc
        elif asr_ctc_weight == 0.0:
            loss_asr = loss_asr_att
        else:
            loss_asr = (
                asr_ctc_weight * loss_asr_ctc + (1 - asr_ctc_weight) * loss_asr_att
            )
        loss_mt = self.mt_weight * loss_mt_att
        loss = (
            (1 - self.asr_weight - self.mt_weight) * loss_st
            + self.asr_weight * loss_asr
            + self.mt_weight * loss_mt
        )

        stats = dict(
            loss=loss.detach(),
            loss_asr=loss_asr.detach() if type(loss_asr) is not float else loss_asr,
            loss_mt=loss_mt.detach() if type(loss_mt) is not float else loss_mt,
            loss_st_ctc=loss_st_ctc.detach()
            if type(loss_st_ctc) is not float
            else loss_st_ctc,
            loss_st_trans=loss_st_trans.detach()
            if type(loss_st_trans) is not float
            else loss_st_trans,
            loss_st_att=loss_st_att.detach()
            if type(loss_st_att) is not float
            else loss_st_att,
            loss_st=loss_st.detach(),
            acc_asr=acc_asr_att,
            acc_mt=acc_mt_att,
            acc=acc_st_att,
            cer_ctc=cer_asr_ctc,
            cer=cer_asr_att,
            wer=wer_asr_att,
            bleu=bleu_st_att,
            bleu_ctc=bleu_st_ctc,
        )

        # force_gatherable: to-device and to-tensor if scalar for DataParallel
        loss, stats, weight = force_gatherable((loss, stats, batch_size), loss.device)
        return loss, stats, weight

    def collect_feats(
        self,
        speech: torch.Tensor,
        speech_lengths: torch.Tensor,
        text: torch.Tensor,
        text_lengths: torch.Tensor,
        src_text: Optional[torch.Tensor] = None,
        src_text_lengths: Optional[torch.Tensor] = None,
        **kwargs,
    ) -> Dict[str, torch.Tensor]:
        feats, feats_lengths = self._extract_feats(speech, speech_lengths)
        return {"feats": feats, "feats_lengths": feats_lengths}

    def encode(
        self,
        speech: torch.Tensor,
        speech_lengths: torch.Tensor,
        return_int_enc: bool = False,
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        """Frontend + Encoder. Note that this method is used by st_inference.py

        Args:
            speech: (Batch, Length, ...)
            speech_lengths: (Batch, )
        """
        with autocast(False):
            # 1. Extract feats
            feats, feats_lengths = self._extract_feats(speech, speech_lengths)

            # 2. Data augmentation
            if self.specaug is not None and self.training:
                feats, feats_lengths = self.specaug(feats, feats_lengths)

            # 3. Normalization for feature: e.g. Global-CMVN, Utterance-CMVN
            if self.normalize is not None:
                feats, feats_lengths = self.normalize(feats, feats_lengths)

        # Pre-encoder, e.g. used for raw input data
        if self.preencoder is not None:
            feats, feats_lengths = self.preencoder(feats, feats_lengths)

        # 4. Forward encoder
        # feats: (Batch, Length, Dim)
        # -> encoder_out: (Batch, Length2, Dim2)
        encoder_out, encoder_out_lens, _ = self.encoder(feats, feats_lengths)

        if return_int_enc:
            int_encoder_out, int_encoder_out_lens = encoder_out, encoder_out_lens

        if self.hier_encoder is not None:
            encoder_out, encoder_out_lens, _ = self.hier_encoder(
                encoder_out, encoder_out_lens
            )

        # Post-encoder, e.g. NLU
        if self.postencoder is not None:
            encoder_out, encoder_out_lens = self.postencoder(
                encoder_out, encoder_out_lens
            )

        assert encoder_out.size(0) == speech.size(0), (
            encoder_out.size(),
            speech.size(0),
        )
        assert encoder_out.size(1) <= encoder_out_lens.max(), (
            encoder_out.size(),
            encoder_out_lens.max(),
        )

        if return_int_enc:
            return encoder_out, encoder_out_lens, int_encoder_out, int_encoder_out_lens
        return encoder_out, encoder_out_lens

    def _extract_feats(
        self, speech: torch.Tensor, speech_lengths: torch.Tensor
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        assert speech_lengths.dim() == 1, speech_lengths.shape

        # for data-parallel
        speech = speech[:, : speech_lengths.max()]

        if self.frontend is not None:
            # Frontend
            #  e.g. STFT and Feature extract
            #       data_loader may send time-domain signal in this case
            # speech (Batch, NSamples) -> feats: (Batch, NFrames, Dim)
            feats, feats_lengths = self.frontend(speech, speech_lengths)
        else:
            # No frontend and no feature extract
            feats, feats_lengths = speech, speech_lengths
        return feats, feats_lengths

    def _calc_mt_att_loss(
        self,
        encoder_out: torch.Tensor,
        encoder_out_lens: torch.Tensor,
        ys_pad: torch.Tensor,
        ys_pad_lens: torch.Tensor,
        speech: Optional[torch.Tensor],
        speech_lens: Optional[torch.Tensor],
        st: bool = True,
    ):
        ys_in_pad, ys_out_pad = add_sos_eos(
            ys_pad, self.sos, self.eos, self.tgt_ignore_id
        )
        ys_in_lens = ys_pad_lens + 1
        # 1. Forward decoder
        if st:
            if self.use_speech_attn:
                decoder_out, _ = self.decoder(
                    encoder_out,
                    encoder_out_lens,
                    ys_in_pad,
                    ys_in_lens,
                    speech,
                    speech_lens,
                )
            else:
                decoder_out, _ = self.decoder(
                    encoder_out, encoder_out_lens, ys_in_pad, ys_in_lens
                )
        else:
            decoder_out, _ = self.decoder(
                encoder_out, encoder_out_lens, ys_in_pad, ys_in_lens
            )

        # 2. Compute attention loss
        loss_att = self.criterion_st(decoder_out, ys_out_pad)
        acc_att = th_accuracy(
            decoder_out.view(-1, self.vocab_size),
            ys_out_pad,
            ignore_label=self.tgt_ignore_id,
        )

        # Compute cer/wer using attention-decoder
        if self.training or self.mt_error_calculator is None:
            bleu_att = None
        else:
            ys_hat = decoder_out.argmax(dim=-1)
            bleu_att = self.mt_error_calculator(ys_hat.cpu(), ys_pad.cpu())

        return loss_att, acc_att, bleu_att

    def _calc_asr_att_loss(
        self,
        encoder_out: torch.Tensor,
        encoder_out_lens: torch.Tensor,
        ys_pad: torch.Tensor,
        ys_pad_lens: torch.Tensor,
        return_hs: bool = False,
    ):
        # Use CTC output as AR decoder target; useful for multi-decoder training
        skip_loss = False
        if self.training and self.ctc_sample_rate > 0:
            if random.uniform(0, 1) < self.ctc_sample_rate:
                ys_hat = self.ctc.argmax(encoder_out).data
                ys_hat = [[x[0] for x in groupby(ys)] for ys in ys_hat]
                ys_hat = [[x for x in filter(lambda x: x != 0, ys)] for ys in ys_hat]
                for i, ys in enumerate(ys_hat):
                    if len(ys) == 0:
                        ys_hat[i] = [x for x in ys_pad[i] if x != -1]
                ys_pad_lens = torch.tensor(
                    [len(x) for x in ys_hat], device=encoder_out.device
                )
                ys_pad = [torch.tensor(ys, device=encoder_out.device) for ys in ys_pad]
                ys_pad = pad_sequence(ys_pad, batch_first=True, padding_value=-1)

                # skip the loss
                skip_loss = True

        ys_in_pad, ys_out_pad = add_sos_eos(
            ys_pad, self.src_sos, self.src_eos, self.ignore_id
        )
        ys_in_lens = ys_pad_lens + 1

        # 1. Forward decoder
        if return_hs:
            decoder_out, _, hs_dec_asr = self.extra_asr_decoder(
                encoder_out, encoder_out_lens, ys_in_pad, ys_in_lens, return_hs=True
            )
        else:
            hs_dec_asr = None
            decoder_out, _ = self.extra_asr_decoder(
                encoder_out, encoder_out_lens, ys_in_pad, ys_in_lens
            )

        if skip_loss:
            return 0.0, None, None, None, hs_dec_asr

        # 2. Compute attention loss
        loss_att = self.criterion_asr(decoder_out, ys_out_pad)
        acc_att = th_accuracy(
            decoder_out.view(-1, self.src_vocab_size),
            ys_out_pad,
            ignore_label=self.ignore_id,
        )

        # Compute cer/wer using attention-decoder
        if self.training or self.asr_error_calculator is None:
            cer_att, wer_att = None, None
        else:
            ys_hat = decoder_out.argmax(dim=-1)
            cer_att, wer_att = self.asr_error_calculator(ys_hat.cpu(), ys_pad.cpu())

        return loss_att, acc_att, cer_att, wer_att, hs_dec_asr

    def _calc_asr_ctc_loss(
        self,
        encoder_out: torch.Tensor,
        encoder_out_lens: torch.Tensor,
        ys_pad: torch.Tensor,
        ys_pad_lens: torch.Tensor,
    ):
        # Calc CTC loss
        loss_ctc = self.ctc(encoder_out, encoder_out_lens, ys_pad, ys_pad_lens)

        # Calc CER using CTC
        cer_ctc = None
        if not self.training and self.asr_error_calculator is not None:
            ys_hat = self.ctc.argmax(encoder_out).data
            cer_ctc = self.asr_error_calculator(ys_hat.cpu(), ys_pad.cpu(), is_ctc=True)
        return loss_ctc, cer_ctc

    def _calc_mt_ctc_loss(
        self,
        encoder_out: torch.Tensor,
        encoder_out_lens: torch.Tensor,
        ys_pad: torch.Tensor,
        ys_pad_lens: torch.Tensor,
    ):
        # Calc CTC loss
        loss_ctc = self.st_ctc(encoder_out, encoder_out_lens, ys_pad, ys_pad_lens)

        # Calc CER using CTC
        bleu_ctc = None
        if not self.training and self.mt_error_calculator is not None:
            ys_hat = self.st_ctc.argmax(encoder_out).data
            bleu_ctc = self.mt_error_calculator(ys_hat.cpu(), ys_pad.cpu(), is_ctc=True)
        return loss_ctc, bleu_ctc

    def _calc_st_transducer_loss(
        self,
        encoder_out: torch.Tensor,
        encoder_out_lens: torch.Tensor,
        labels: torch.Tensor,
    ):
        """Compute Transducer loss.

        Args:
            encoder_out: Encoder output sequences. (B, T, D_enc)
            encoder_out_lens: Encoder output sequences lengths. (B,)
            labels: Label ID sequences. (B, L)

        Return:
            loss_transducer: Transducer loss value.
            cer_transducer: Character error rate for Transducer.
            wer_transducer: Word Error Rate for Transducer.

        """
        decoder_in, target, t_len, u_len = get_transducer_task_io(
            labels,
            encoder_out_lens,
            ignore_id=self.tgt_ignore_id,
            blank_id=self.blank_id,
        )

        self.decoder.set_device(encoder_out.device)
        decoder_out = self.decoder(decoder_in)

        joint_out = self.st_joint_network(
            encoder_out.unsqueeze(2), decoder_out.unsqueeze(1)
        )

        loss_transducer = self.st_criterion_transducer(
            joint_out,
            target,
            t_len,
            u_len,
        )

        cer_transducer, wer_transducer = None, None
        # TODO(brian): add error_calculator_trans

        return loss_transducer, cer_transducer, wer_transducer


# import requests
# from fake_useragent import UserAgent
# from lxml import etree
#
#
#
#
#
# url='https://www.ip138.com/iplookup.asp?ip=101.39.225.103&action=2'
# headers = {'User-Agent': UserAgent().random}
# html=requests.get(url=url,headers=headers)
# html=html.unicode("gb2312").encode("utf8")
#
# print(i)

import requests

def checkip(ip):
    URL = 'https://www.ip138.com/iplookup.asp?ip=101.39.225.103&action=2'
    try:
        # r = requests.get(URL, params=ip, timeout=3)
        r = requests.get(URL,timeout=3)
        data = r.text.encode("latin1").decode("gbk")
        print(data)
    except requests.RequestException as e:
        print(e)
    else:
        json_data = r.json()
        if json_data[u'code'] == 0:
            print ('所在国家： ' + json_data[u'data'][u'country'].encode('utf-8'))
            print  ('所在地区： ' + json_data[u'data'][u'area'].encode('utf-8'))
            print('所在省份： ' + json_data[u'data'][u'region'].encode('utf-8'))
            print('所在城市： ' + json_data[u'data'][u'city'].encode('utf-8'))
            print('所属运营商：' + json_data[u'data'][u'isp'].encode('utf-8'))
        else:
            print('查询失败,请稍后再试！')

ip = {'ip': '202.102.193.68'}
checkip(ip)


from concurrent import futures
import time
import grpc
import rpc_pb2
import rpc_pb2_grpc

class ctf(rpc_pb2_grpc.ctfServicer):
    def test_api(self, request, context):
        return rpc_pb2.TestApiReply(s="hello world")
    def get_flag(self, request, context):
        return rpc_pb2.GetFlagReply(flag="flag{acc551fdeeb94cae44ab9a8565b0398c}")

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    rpc_pb2_grpc.add_ctfServicer_to_server(ctf(), server)
    server.add_insecure_port('0.0.0.0:50051')
    server.start()
    try:
        while True:
            time.sleep(60 * 60 * 24)
    except KeyboardInterrupt:
        server.stop(0)

if __name__ == '__main__':
    serve()


from adcsim.simulations.sim import sim_attitude
import numpy as np
from adcsim.hysteresis_rod import HysteresisRod
from adcsim.CubeSat_model_examples import CubeSatModel
from multiprocessing import Pool
import os
import copy

# create initial simulation parameters dict
sim_params = {
    'time_step': 0.01,
    'save_every': 10,
    'end_time_index': 5000,
    'start_time': '2019/03/24 18:35:01',
    'final_time': '2019/03/24 18:35:51',
    'omega0_body': np.array([-2, 3, 3.5]),
    'sigma0': [0.6440095705520482, 0.39840861883760637, 0.18585931442943798]
}

# create inital cubesat parameters dict (the raw data is way to large to do manually like above)
rod1 = HysteresisRod(br=0.35, bs=0.73, hc=1.59, volume=0.075/(100**3), axes_alignment=np.array([1.0, 0, 0]))
rod2 = HysteresisRod(br=0.35, bs=0.73, hc=1.59, volume=0.075/(100**3), axes_alignment=np.array([0, 1.0, 0]))
cubesat = CubeSatModel(inertia=np.diag([8*(10**-3), 8*(10**-3), 2*(10**-3)]), magnetic_moment=np.array([0, 0, 1.5]),
                       hyst_rods=[rod1, rod2])
cubesat_params = cubesat.asdict()

# USING MULTIPLE CORES
if __name__ == "__main__":
    pool = Pool(os.cpu_count())  # the number of cores differ on different computers

    cp = []

    # First simulation (parameters what is shown above)
    cp.append([copy.deepcopy(sim_params), copy.deepcopy(cubesat_params), 'first'])

    # Second simulation (alter the parameters as desired before appending it to cp)
    cubesat_params['hyst_rods'][0]['axes_alignment'] = np.array([0, 0, 1.0])
    cubesat_params['magnetic_moment'] = np.array([1.5, 0, 0])
    cp.append([copy.deepcopy(sim_params), copy.deepcopy(cubesat_params), 'second'])

    # Third simulation (alter the parameters as desired before appending it to cp)
    sim_params['omega0_body'] = [0, -0.06666, 0]
    cp.append([copy.deepcopy(sim_params), copy.deepcopy(cubesat_params), 'third'])

    # Second simulation (alter the parameters as desired before appending it to cp)
    cubesat_params['hyst_rods'][0]['axes_alignment'] = np.array([1.0, 0, 0])
    cubesat_params['magnetic_moment'] = np.array([0, 0, 1.5])
    cp.append([copy.deepcopy(sim_params), copy.deepcopy(cubesat_params), 'fourth'])

    # ...
    # ...
    # ...
    # Infinity simulation

    pool.starmap(sim_attitude, cp)