import os
import glob
import h5py
import json
import copy
import torch
import librosa
import numpy as np
import soundfile as sf
import speech_recognition as sr
from jiwer import wer
from tqdm import tqdm
from scipy import signal
from trainer import Trainer
from hps.hps import hp, Hps
from torch.autograd import Variable
from preprocess import get_spectrograms
from model.tacotron.text.symbols import symbols

############
# CONSTANT #
############
MIN_LEN = 9

def griffin_lim(spectrogram): # Applies Griffin-Lim's raw.

	def _invert_spectrogram(spectrogram): # spectrogram: [f, t]
		return librosa.istft(spectrogram, hp.hop_length, win_length=hp.win_length, window="hann")

	X_best = copy.deepcopy(spectrogram)
	for i in range(hp.n_iter):
		X_t = _invert_spectrogram(X_best)
		est = librosa.stft(X_t, hp.n_fft, hp.hop_length, win_length=hp.win_length)
		phase = est / np.maximum(1e-8, np.abs(est))
		X_best = spectrogram * phase
	X_t = _invert_spectrogram(X_best)
	y = np.real(X_t)
	return y

def spectrogram2wav(mag): # Generate wave file from spectrogram
	mag = mag.T # transpose
	mag = (np.clip(mag, 0, 1) * hp.max_db) - hp.max_db + hp.ref_db # de-noramlize
	mag = np.power(10.0, mag * 0.05) # to amplitude
	wav = griffin_lim(mag) # wav reconstruction
	wav = signal.lfilter([1], [1, -hp.preemphasis], wav) # de-preemphasis
	wav, _ = librosa.effects.trim(wav) # trim
	return wav.astype(np.float32)

def synthesis(f0, sp, ap, sr=16000):
	y = pw.synthesize(f0.astype(np.float64), sp.astype(np.float64), ap.astype(np.float64), sr, pw.default_frame_period)
	return y

def convert_x(x, c, trainer, enc_only, verbose=False):
	c_var = Variable(torch.from_numpy(np.array([c]))).cuda()
	tensor = torch.from_numpy(np.expand_dims(x, axis=0)).type(torch.FloatTensor)
	converted, enc = trainer.test_step(tensor, c_var, enc_only=enc_only, verbose=verbose)
	converted = converted.squeeze(axis=0).transpose((1, 0))
	enc = enc.squeeze(axis=0).transpose((1, 0))
	return converted, enc

def encode_x(x, trainer):
	tensor = torch.from_numpy(np.expand_dims(x, axis=0)).type(torch.FloatTensor)
	enc = trainer.encoder_test_step(tensor)
	enc = enc.squeeze(axis=0).transpose((1, 0))
	return enc

def get_trainer(hps_path, model_path, g_mode, enc_mode, clf_path):
	HPS = Hps(hps_path)
	hps = HPS.get_tuple()
	global MIN_LEN
	MIN_LEN = MIN_LEN if hps.enc_mode != 'gumbel_t' else hps.seg_len
	trainer = Trainer(hps, None, g_mode, enc_mode)
	trainer.load_model(model_path, load_model_list=hps.load_model_list, clf_path = clf_path)
	return trainer

def asr(fname):
	r = sr.Recognizer()
	with sr.WavFile(fname) as source:
		audio = r.listen(source)
	text = r.recognize_google(audio, language='en')
	return text

def compare_asr(s_wav, t_wav):
	try:
		gt = asr(s_wav)
		recog = asr(t_wav)
		err_result = wer(gt, recog), wer(' '.join([c for c in gt if c != ' ']), ' '.join([c for c in recog if c != ' ']))
	except sr.UnknownValueError:
		err_result = [1., 1.]
	except:
		err_result = [-1., -1.]
	return err_result

def parse_encodings(encodings):
	return [' '.join([str(int(e)) for i, e in enumerate(enc)]) for enc in encodings]

def write_encodings(path, encodings):
	with open(path, 'w') as file:
		for enc in encodings:
			for i, e in enumerate(enc):
				file.write(str(int(e)) + (' ' if i < len(enc)-1 else ''))
			file.write('\n')

def convert(trainer,
			seg_len,
			src_speaker_spec,
			src_speaker,
			tar_speaker,
			utt_id,
			speaker2id,
			result_dir,
			enc_only=True,
			save=['wav', 'enc']):

	# pad spec to minimum len
	PADDED = False
	if len(src_speaker_spec) < MIN_LEN:
		padding = np.zeros((MIN_LEN - src_speaker_spec.shape[0], src_speaker_spec.shape[1]))
		src_speaker_spec = np.concatenate((src_speaker_spec, padding), axis=0)
		PADDED = True

	if len(src_speaker_spec) <= seg_len:
		converted_results, encodings = convert_x(src_speaker_spec, speaker2id[tar_speaker], trainer, enc_only=enc_only)
		if PADDED:
			encodings = encodings[:MIN_LEN//8] # truncate the encoding of zero paddings

	else:
		converted_results = []
		encodings = []
		for idx in range(0, len(src_speaker_spec), seg_len):
			if idx + (seg_len*2) > len(src_speaker_spec):
				spec_frag = src_speaker_spec[idx:-1]
			else:
				spec_frag = src_speaker_spec[idx:idx+seg_len]

			if len(spec_frag) >= seg_len:
				converted_x, enc = convert_x(spec_frag, speaker2id[tar_speaker], trainer, enc_only=enc_only)
				converted_results.append(converted_x)
				encodings.append(enc)
			elif idx == 0:
				raise RuntimeError('Please check if input is too short!')

		converted_results = np.concatenate(converted_results, axis=0)
		encodings = np.concatenate(encodings, axis=0)

	wav_data = spectrogram2wav(converted_results)
	if len(save) != 0:
		if 'wav' in save:
			wav_path = os.path.join(result_dir, f'{tar_speaker}_{utt_id}.wav')
			sf.write(wav_path, wav_data, hp.sr, 'PCM_16')
		if 'enc' in save:
			enc_path = os.path.join(result_dir, f'{src_speaker}_{utt_id}.txt')
			write_encodings(enc_path, encodings)
		return wav_path, len(converted_results)
	else:
		return wav_data, encodings

def encode(src_speaker_spec, trainer, seg_len, s_speaker=None, utt_id=None, result_dir=None, save=True):
	if save:
		assert result_dir != None
		assert s_speaker != None
		assert utt_id != None

	# pad spec to minimum len
	PADDED = False
	if len(src_speaker_spec) < MIN_LEN:
		padding = np.zeros((MIN_LEN - src_speaker_spec.shape[0], src_speaker_spec.shape[1]))
		src_speaker_spec = np.concatenate((src_speaker_spec, padding), axis=0)
		PADDED = True

	if len(src_speaker_spec) <= seg_len:
		encodings = encode_x(src_speaker_spec, trainer)
		if PADDED:
			encodings = encodings[:MIN_LEN//8] # truncate the encoding of zero paddings

	else:
		encodings = []
		for idx in range(0, len(src_speaker_spec), seg_len):
			if idx + (seg_len*2) > len(src_speaker_spec):
				spec_frag = src_speaker_spec[idx:-1]
			else:
				spec_frag = src_speaker_spec[idx:idx+seg_len]

			if len(spec_frag) >= seg_len:
				enc = encode_x(spec_frag, trainer)
				encodings.append(enc)
			elif idx == 0:
				raise RuntimeError('Please check if input is too short!')

		encodings = np.concatenate(encodings, axis=0)

	if save:
		enc_path = os.path.join(result_dir, f"{s_speaker}_{utt_id}.txt")
		write_encodings(enc_path, encodings)
	else:
		return encodings

def test_from_list(trainer, seg_len, synthesis_list, data_path, speaker2id_path, result_dir, enc_only, flag='test', run_asr=False):

	with open(speaker2id_path, 'r') as f_json:
		speaker2id = json.load(f_json)

	feeds = []
	with open(synthesis_list, 'r') as f:
		file = f.readlines()
		for line in file:
			line = line.split('\n')[0].split(' ')
			feeds.append({'s_id' : line[0].split('/')[1].split('_')[0],
						  'utt_id' : line[0].split('/')[1].split('_')[1],
						  't_id' : line[1], })

	print('[Tester] - Number of files to be resynthesize: ', len(feeds))
	dir_path = os.path.join(result_dir, f'{flag}/')
	os.makedirs(dir_path, exist_ok=True)

	err_results = []
	with h5py.File(data_path, 'r') as f_h5:
		for feed in tqdm(feeds):
			conv_audio, n_frames = convert(trainer,
								 		   seg_len,
								 		   src_speaker_spec=f_h5[f"test/{feed['s_id']}/{feed['utt_id']}/lin"][()],
								 		   src_speaker=feed['s_id'],
								 		   tar_speaker=feed['t_id'],
								 		   utt_id=feed['utt_id'],
								 		   speaker2id=speaker2id,
								 		   result_dir=dir_path,
								 		   enc_only=enc_only,
								 		   save=['wav'])
			n_frames = len(f_h5[f"test/{feed['s_id']}/{feed['utt_id']}/lin"][()])
			if run_asr:
				if hp.frame_shift * (n_frames - 1) + hp.frame_length >= 3.0:
					orig_audio = spectrogram2wav(f_h5[f"test/{feed['s_id']}/{feed['utt_id']}/lin"][()])
					sf.write('orig_audio.wav', orig_audio, hp.sr, 'PCM_16')
					err_results.append(compare_asr(s_wav='orig_audio.wav', t_wav=conv_audio))
					os.remove(path='orig_audio.wav')

	if run_asr:
		err_mean = np.mean(err_results, axis=0)
		print('WERR: {:.3f}  CERR: {:.3f}, computed over {} samples'.format(err_mean[0], err_mean[1], len(err_results)))

def cross_test(trainer, seg_len, data_path, speaker2id_path, result_dir, enc_only, flag):

	with h5py.File(data_path, 'r') as f_h5:

		with open(speaker2id_path, 'r') as f_json:
			speaker2id = json.load(f_json)

		if flag == 'test':
			source_speakers = sorted(list(f_h5['test'].keys()))
		elif flag == 'train':
			source_speakers = [s for s in sorted(list(f_h5['train'].keys())) if s[0] == 'S']
		target_speakers = [s for s in sorted(list(f_h5['train'].keys())) if s[0] == 'V']

		print('[Tester] - Testing on the {}ing set...'.format(flag))
		print('[Tester] - Source speakers: %i, Target speakers: %i' % (len(source_speakers), len(target_speakers)))
		print('[Tester] - Converting all testing utterances from source speakers to target speakers, this may take a while...')

		for src_speaker in tqdm(source_speakers):
			for tar_speaker in target_speakers:
				assert src_speaker != tar_speaker
				dir_path = os.path.join(result_dir, f'{src_speaker}_to_{tar_speaker}')
				os.makedirs(dir_path, exist_ok=True)

				for utt_id in f_h5[f'test/{src_speaker}']:
					src_speaker_spec = f_h5[f'test/{src_speaker}/{utt_id}/lin'][()]
					convert(trainer,
							seg_len,
							src_speaker_spec,
							tar_speaker,
							utt_id=utt_id,
							speaker2id=speaker2id,
							result_dir=dir_path,
							enc_only=enc_only)

def test_single(trainer, seg_len, speaker2id_path, result_dir, enc_only, s_speaker, t_speaker):

	with open(speaker2id_path, 'r') as f_json:
		speaker2id = json.load(f_json)

	if s_speaker == 'S015':
		filename = './data/english/train/unit/S015_0361841101.wav'
	elif s_speaker == 'S119':
		filename = './data/english/train/unit/S119_1561145062.wav'
	elif s_speaker == 'S130':
		filename = './data/english/test/S130_3516588097.wav'
	elif s_speaker == 'S089':
		filename = './data/english/test/S089_1810826781.wav'
	elif s_speaker == 'S378':
		filename = './data/surprise/test/S378_117437.wav'
	else:
		raise NotImplementedError('Please modify path manually!')

	_, spec = get_spectrograms(filename)
	wav_data, encodings = convert(trainer,
								  seg_len,
								  src_speaker_spec=spec,
								  src_speaker=s_speaker,
								  tar_speaker=t_speaker,
								  utt_id='',
								  speaker2id=speaker2id,
								  result_dir=result_dir,
								  enc_only=enc_only,
								  save=[])

	sf.write(os.path.join(result_dir, 'result.wav'), wav_data, hp.sr, 'PCM_16')
	write_encodings(os.path.join(result_dir, 'result.txt'), encodings)

	err_result = compare_asr(filename, os.path.join(result_dir, 'result.wav'))

	print('Testing on source speaker {} and target speaker {}, output shape: {}'.format(s_speaker, t_speaker, wav_data.shape))
	print('Comparing ASR result - WERR: {:.3f}  CERR: {:.3f}'.format(err_result[0], err_result[1]))

def test_encode(trainer, seg_len, test_path, data_path, result_dir, flag='test'):

	files = sorted(glob.glob(os.path.join(test_path, '*.wav')))
	feeds = []

	for line in files:
		line = line.split('/')[-1]
		feeds.append({'s_id' : line.split('_')[0],
					  'utt_id' : line.split('_')[1].split('.')[0]})

	print('[Tester] - Number of files to encoded: ', len(feeds))
	dir_path = os.path.join(result_dir, f'{flag}/')
	os.makedirs(dir_path, exist_ok=True)

	with h5py.File(data_path, 'r') as f_h5:
		for feed in tqdm(feeds):

			src_speaker_spec = f_h5[f"test/{feed['s_id']}/{feed['utt_id']}/lin"][()]
			encode(src_speaker_spec, trainer, seg_len, s_speaker=feed['s_id'], utt_id=feed['utt_id'], result_dir=dir_path)

def target_classify(trainer, seg_len, synthesis_list, result_dir, flag='test'):
	dir_path = os.path.join(result_dir, f'{flag}/')
	with open(synthesis_list, 'r') as f:
		file = f.readlines()
	acc = []
	for line in file:
		# get wav path
		line = line.split('\n')[0].split(' ')
		utt_id = line[0].split('/')[1].split('_')[1]
		tar_speaker = line[1]
		wav_path = os.path.join(dir_path, f'{tar_speaker}_{utt_id}.wav')

		# get spectrogram
		_, spec = get_spectrograms(wav_path)

		# padding spec
		if len(spec) < seg_len:
			padding = np.zeros((seg_len - spec.shape[0], spec.shape[1]))
			spec = np.concatenate((spec, padding), axis=0)

		# classification
		logits = []
		for idx in range(0, len(spec), seg_len):
			if idx + (seg_len*2) > len(spec):
				spec_frag = spec[idx:-1]
			else:
				spec_frag = spec[idx:idx+seg_len]

			if len(spec_frag) >= seg_len:
				x = torch.from_numpy(np.expand_dims(spec_frag[:seg_len, :], axis=0)).type(torch.FloatTensor)
				logit = trainer.classify(x)
				logits.append(logit)
			elif idx == 0:
				raise RuntimeError('Please check if input is too short!')
		logits = np.concatenate(logits, axis=0)
		#logits = np.sum(logits, axis = 0)
		for logit in logits:
			am = logit.argmax()
			if am == 0:
				clf_speaker = 'V001'
			elif am ==1:
				clf_speaker = 'V002'
			else:
				clf_speaker = 'None'
			if clf_speaker == tar_speaker:
				acc.append(1)
				#print('[info]: {} is classified to {}'.format(wav_path, clf_speaker))
			else:
				acc.append(0)
				#print('[Error]: {} is classified to {}'.format(wav_path, clf_speaker))
	print('Classification Acc: {:.3f}'.format(np.sum(acc)/float(len(acc))))

def encode_for_tacotron(target, trainer, seg_len, multi2idx_path, wav_path, result_path):
	wavs = sorted(glob.glob(os.path.join(wav_path, '*.wav')))
	print('[Converter] - Number of wav files to encoded: ', len(wavs))

	names = []
	enc_outputs = []

	for wav_path in tqdm(wavs):
		name = wav_path.split('/')[-1].split('.')[0]
		s_id = name.split('_')[0]
		u_id = name.split('_')[1]
		if s_id != target:
			continue

		y, sr = librosa.load(wav_path)
		d = librosa.get_duration(y=y, sr=sr)
		if d > 25:
			continue # --> this filter out too long utts, 3523/3533 for V001 and V002 together in the english dataset

		_, spec = get_spectrograms(wav_path)
		encodings = encode(spec, trainer, seg_len, save=False)
		encodings = parse_encodings(encodings)
		enc_outputs.append(encodings)
		names.append((s_id, u_id))

	# build encodings to character mapping
	idx = 0
	multi2idx = {}
	print('[Converter] - Building encoding to symbol mapping...')
	for encodings in tqdm(enc_outputs):
		for encoding in encodings:
			if str(encoding) not in multi2idx:
				multi2idx[str(encoding)] = symbols[idx]
				idx += 1

	print('[Converter] - Number of unique discret units: ', len(multi2idx))
	with open(multi2idx_path, 'w') as file:
		file.write(json.dumps(multi2idx))

	result_path = result_path.replace('target', target)
	print('[Converter] - Writing to meta file...')
	with open(result_path, 'w') as file:
		for i, encodings in enumerate(enc_outputs):
			file.write(str(names[i][0]) + '_' + str(names[i][1] + '|'))
			for encoding in encodings:
				file.write(multi2idx[str(encoding)])
			file.write('\n')


import telebot
import requests
import re
import os
from twilio.rest import Client
import pyrebase
bot = telebot.TeleBot("Replace this with telegram bot father key", parse_mode=None)
config = {
  "apiKey": "",
  "authDomain": "",
  "databaseURL": "",
  "storageBucket": ""
}
x = 0
y = 0
z = 0
q = 0
firebase = pyrebase.initialize_app(config)
db = firebase.database()

@bot.message_handler(func=lambda m: True)
def echo_all(message):
    a = message.text
    b = a.split('@')
    print(b)
    global x
    global y
    global z
    global q
    chatt = message.chat.id
    if(b[0] == "/start"):
        bot.reply_to(message, "Welcome to tele2WA bot")
    elif(b[0] == "/setsid"):
        x = b[1]
        print(x)
        bot.reply_to(message, "SID added")
    elif(b[0] == "/settoken"):
        y = b[1]
        print(y)
        bot.reply_to(message, "token added")
    elif(b[0] == "/setfromphone"):
        z = b[1]
        print(z)
        bot.reply_to(message, "fromphone added")
    elif(b[0] == "/settophone"):
        q = b[1]
        print(q)
        data = {
          "sid":x,
          "token":y,
          "fromphone":z,
          "tophone":q
         }
        db.child("users").child(chatt).set(data)
        bot.reply_to(message, "details added")
    elif(b[0] == "/updatetophone"):
        data4 = {
              "tophone": b[1]
             }
        db.child("users").child(chatt).update(data4)
        bot.reply_to(message, "tophone updated")
    elif(b[0] == "/updatefromphone"):
        data5 = {
              "fromphone": b[1]
             }
        db.child("users").child(chatt).update(data5)
        bot.reply_to(message, "fromphone updated")

    elif(b[0] =="/send"):
        test = db.child("users").child(chatt).get()
        p = test.val()['sid']
        d = test.val()['token']
        t = test.val()['fromphone']
        r = test.val()['tophone']
        client = Client(p,d)
        client.messages.create(body=b[1],from_="whatsapp:"+ str(t),to="whatsapp:"+str(r))
    else:
        pass
bot.polling()


import sys
from collections import deque

def div_area(q):
    while q:
        r, c = q.popleft()

        for x, y in [[r + 1, c], [r - 1, c], [r, c + 1], [r, c - 1]]:
            if 0<= x < n and 0<= y < m:
                if area[x][y] == 0:
                    area[x][y] = -1
                    q.append([x, y])

def next_hour():
    global cheese
    new_cheese = []
    new_air = deque()
    air = []

    for i, j in cheese:
        cnt = 0
        cnt2 = 0

        for x, y in [[i - 1, j], [i + 1, j], [i, j - 1], [i, j + 1]]:
            if area[x][y] == -1:
                cnt += 1
            elif area[x][y] == 0:
                cnt2 += 1
        if cnt >= 2:
            air.append([i, j])
            if cnt2 > 0:
                new_air.append([i, j])

        else:
            new_cheese.append([i, j])

    for i, j in air:
        area[i][j] = -1

    div_area(new_air)
    cheese = new_cheese

n, m = map(int, sys.stdin.readline().strip().split())
area = [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]
cheese = []
for i in range(n):
    for j in range(m):
        if area[i][j] == 1:
            cheese.append([i, j])

area[0][0] = -1
div_area(deque([[0, 0]]))

cnt = 0

while cheese:
    next_hour()
    cnt += 1

print(cnt)


import urllib.request
import datetime
import json

# 返回一个月内的cf场数和上/掉分情况
def get_CF_ContestCount(name):
    apiUrl = "https://codeforces.com/api/user.rating?handle=" + name

    try:
        page = urllib.request.urlopen(apiUrl, timeout=2000)
        s = page.read().decode('utf-8')
        contestsData = json.loads(s)['result']

        # 改为直接用rating变化的时间当做比赛时间
        # 由于rating变化时间一般延迟一天，放宽到32天
        lastTime=(datetime.timedelta(days=-32) +
           datetime.datetime.now()).timestamp()

        sum=0
        cnt=0
        for contest in contestsData:
            if contest['ratingUpdateTimeSeconds'] < lastTime:
                continue
            cnt += 1
            sum += contest['newRating'] - contest['oldRating']
        return [cnt, sum]

    except Exception as e:
        print(str(e))
        return [-1, -1]

if __name__ == "__main__":
    while(True):
        name=input("请输入要爬的ID:")
        print(get_CF_ContestCount(name))


from math import sqrt
from itertools import product
import numpy as np
from scipy.special import factorial as fact
from functools import lru_cache

# The interaction matrix in desired basis
# U^{spherical}_{m1 m2 m3 m4} = \sum_{k=0}^{2l} F_k angular_matrix_element(l, k, m1, m2, m3, m4)
# H = \frac{1}{2} \sum_{ijkl,\sigma \sigma'} U_{ijkl} a_{i \sigma}^\dagger a_{j \sigma'}^\dagger a_{l \sigma'} a_{k \sigma}.
@lru_cache(maxsize=8)
def U_matrix(l,
             radial_integrals=None,
             U_int=None,
             J_hund=None,
             basis='spherical',
             T=None):
    r"""
    Calculate the full four-index U matrix being given either radial_integrals or U_int and J_hund.
    The convetion for the U matrix is that used to construct the Hamiltonians, namely:

    .. math:: H = \frac{1}{2} \sum_{ijkl,\sigma \sigma'} U_{ijkl} a_{i \sigma}^\dagger a_{j \sigma'}^\dagger a_{l \sigma'} a_{k \sigma}.

    Parameters
    ----------
    l : integer
        Angular momentum of shell being treated (l=2 for d shell, l=3 for f shell).
    radial_integrals : list, optional
                       Slater integrals [F0,F2,F4,..].
                       Must be provided if U_int and J_hund are not given.
                       Preferentially used to compute the U_matrix if provided alongside U_int and J_hund.
    U_int : scalar, optional
            Value of the screened Hubbard interaction.
            Must be provided if radial_integrals are not given.
    J_hund : scalar, optional
             Value of the Hund's coupling.
             Must be provided if radial_integrals are not given.
    basis : string, optional
            The basis in which the interaction matrix should be computed.
            Takes the values

            - 'spherical': spherical harmonics,
            - 'cubic': cubic harmonics,
            - 'other': other basis type as given by the transformation matrix T.

    T : real/complex numpy array, optional
        Transformation matrix for basis change.
        Must be provided if basis='other'.
        The transformation matrix is defined such that new creation operators :math:`b^\dagger` are related to
        the old ones :math:`a^\dagger` as

        .. math:: b_{i \sigma}^\dagger = \sum_j T_{ij} a^\dagger_{j \sigma}.

    Returns
    -------
    U_matrix : float numpy array
               The four-index interaction matrix in the chosen basis.

    """

    # Check all necessary information is present and consistent
    if radial_integrals is None and (U_int is None and J_hund is None):
        raise ValueError(
            "U_matrix: provide either the radial_integrals or U_int and J_hund."
        )
    if radial_integrals is None and (U_int is not None and J_hund is not None):
        radial_integrals = U_J_to_radial_integrals(l, U_int, J_hund)
    if radial_integrals is not None and (U_int is not None
                                         and J_hund is not None):
        if len(radial_integrals) - 1 != l:
            raise ValueError(
                "U_matrix: inconsistency in l and number of radial_integrals provided."
            )
        if (radial_integrals - U_J_to_radial_integrals(l, U_int,
                                                       J_hund)).any() != 0.0:
            print(
                "Warning: U_matrix: radial_integrals provided do not match U_int and J_hund. Using radial_integrals to calculate U_matrix."
            )

    # Full interaction matrix
    # Basis of spherical harmonics Y_{-2}, Y_{-1}, Y_{0}, Y_{1}, Y_{2}
    # U^{spherical}_{m1 m2 m3 m4} = \sum_{k=0}^{2l} F_k angular_matrix_element(l, k, m1, m2, m3, m4)
    U_mat = np.zeros(
        (2 * l + 1, 2 * l + 1, 2 * l + 1, 2 * l + 1), dtype=float)

    m_range = list(range(-l, l + 1))
    for n, F in enumerate(radial_integrals):
        k = 2 * n
        for m1, m2, m3, m4 in product(m_range, m_range, m_range, m_range):
            U_mat[m1 + l, m2 + l, m3 + l, m4 +
                     l] += F * angular_matrix_element(l, k, m1, m2, m3, m4)

    # Transform from spherical basis if needed
    if basis == "cubic": T = spherical_to_cubic(l, convention='wien2k')
    if basis == "other" and T is None:
        raise ValueError("U_matrix: provide T for other bases.")
    if T is not None: U_mat = transform_U_matrix(U_mat, T)

    return U_mat

# Convert full 4-index U matrix to 2-index density-density form
def reduce_4index_to_2index(U_4index):
    r"""
    Reduces the four-index matrix to two-index matrices for parallel and anti-parallel spins.

    Parameters
    ----------
    U_4index : float numpy array
               The four-index interaction matrix.

    Returns
    -------
    U : float numpy array
        The two-index interaction matrix for parallel spins.
    Uprime : float numpy array
             The two-index interaction matrix for anti-parallel spins.

    """
    size = U_4index.shape[0] # 2l+1
    U = np.zeros((size, size), dtype=float)  # matrix for same spin
    Uprime = np.zeros((size, size), dtype=float)  # matrix for opposite spin

    m_range = list(range(size))
    for m, mp in product(m_range, m_range):
        U[m, mp] = U_4index[m, mp, m, mp].real - U_4index[m, mp, mp, m].real
        Uprime[m, mp] = U_4index[m, mp, m, mp].real

    return U, Uprime

# Construct the 2-index matrices for the density-density form
@lru_cache(maxsize=8)
def U_matrix_kanamori(n_orb, U_int, J_hund):
    r"""
    Calculate the Kanamori U and Uprime matrices.

    Parameters
    ----------
    n_orb : integer
            Number of orbitals in basis.
    U_int : scalar
            Value of the screened Hubbard interaction.
    J_hund : scalar
             Value of the Hund's coupling.

    Returns
    -------
    U : float numpy array
        The two-index interaction matrix for parallel spins.
    Uprime : float numpy array
             The two-index interaction matrix for anti-parallel spins.

    """

    U = np.zeros((n_orb, n_orb), dtype=float)  # matrix for same spin
    Uprime = np.zeros((n_orb, n_orb), dtype=float)  # matrix for opposite spin

    m_range = list(range(n_orb))
    for m, mp in product(m_range, m_range):
        if m == mp:
            Uprime[m, mp] = U_int
        else:
            U[m, mp] = U_int - 3.0 * J_hund
            Uprime[m, mp] = U_int - 2.0 * J_hund

    return U, Uprime

@lru_cache(maxsize=8)
def U_matrix_dudarev(n_orb, U_int, J_hund):
    r"""
    Calculate the Dudarev U and Uprime matrices.

    Parameters
    ----------
    n_orb : integer
            Number of orbitals in basis.
    U_int : scalar
            Value of the screened Hubbard interaction.
    J_hund : scalar
             Value of the Hund's coupling.

    Returns
    -------
    U : float numpy array
        The two-index interaction matrix for parallel spins.
    Uprime : float numpy array
             The two-index interaction matrix for anti-parallel spins.

    """

    U = np.zeros((n_orb, n_orb), dtype=float)  # matrix for same spin
    Uprime = np.zeros((n_orb, n_orb), dtype=float)  # matrix for opposite spin

    m_range = list(range(n_orb))
    for m, mp in product(m_range, m_range):
        if m == mp:
            Uprime[m, mp] = U_int
        else:
            U[m, mp] = U_int - 1.0 * J_hund
            Uprime[m, mp] = U_int
    return U, Uprime

# Get t2g or eg components
def t2g_submatrix(U, convention=''):
    r"""
    Extract the t2g submatrix of the full d-manifold two- or four-index U matrix.

    Parameters
    ----------
    U : float numpy array
        Two- or four-index interaction matrix.
    convention : string, optional
                 The basis convention.
                 Takes the values

                 - '': basis ordered as ("xy","yz","z^2","xz","x^2-y^2"),
                 - 'wien2k': basis ordered as ("z^2","x^2-y^2","xy","yz","xz").

    Returns
    -------
    U_t2g : float numpy array
            The t2g component of the interaction matrix.

    """
    if convention == 'wien2k':
        return subarray(U, len(U.shape) * [(2, 3, 4)])
    elif convention == '':
        return subarray(U, len(U.shape) * [(0, 1, 3)])
    else:
        raise ValueError("Unknown convention: " + str(convention))

def eg_submatrix(U, convention=''):
    r"""
    Extract the eg submatrix of the full d-manifold two- or four-index U matrix.

    Parameters
    ----------
    U : float numpy array
        Two- or four-index interaction matrix.
    convention : string, optional
                 The basis convention.
                 Takes the values

                 - '': basis ordered as ("xy","yz","z^2","xz","x^2-y^2"),
                 - 'wien2k': basis ordered as ("z^2","x^2-y^2","xy","yz","xz").

    Returns
    -------
    U_eg : float numpy array
           The eg component of the interaction matrix.

    """
    if convention == 'wien2k':
        return subarray(U, len(U.shape) * [(0, 1)])
    elif convention == '':
        return subarray(U, len(U.shape) * [(2, 4)])
    else:
        raise ValueError("Unknown convention: " + str(convention))

# Transform the interaction matrix into another basis
def transform_U_matrix(U_mat, T):
    r"""
    Transform a four-index interaction matrix into another basis.
    The transformation matrix is defined such that new creation operators :math:`b^\dagger` are related to
    the old ones :math:`a^\dagger` as

    .. math:: b_{i \sigma}^\dagger = \sum_j T_{ij} a^\dagger_{j \sigma}.

    Parameters
    ----------
    U_mat : float numpy array
               The four-index interaction matrix in the original basis.
    T : real/complex numpy array, optional
        Transformation matrix for basis change.
        Must be provided if basis='other'.

    Returns
    -------
    U_mat : float numpy array
               The four-index interaction matrix in the new basis.

    """
    return np.einsum("ij,kl,jlmo,mn,op", np.conj(T), np.conj(T), U_mat,
                     np.transpose(T), np.transpose(T))

# Rotation matrices: complex harmonics to cubic harmonics
# Complex harmonics basis: ..., Y_{-2}, Y_{-1}, Y_{0}, Y_{1}, Y_{2}, ...
def spherical_to_cubic(l, convention=''):
    r"""
    Get the spherical harmonics to cubic harmonics transformation matrix.

    Parameters
    ----------
    l : integer
        Angular momentum of shell being treated (l=2 for d shell, l=3 for f shell).
    convention : string, optional
                 The basis convention.
                 Takes the values

                 - '': basis ordered as ("xy","yz","z^2","xz","x^2-y^2"),
                 - 'wien2k': basis ordered as ("z^2","x^2-y^2","xy","yz","xz").

    Returns
    -------
    T : real/complex numpy array
        Transformation matrix for basis change.

    """

    if not convention in ('wien2k', ''):
        raise ValueError("Unknown convention: " + str(convention))

    size = 2 * l + 1
    T = np.zeros((size, size), dtype=complex)
    if convention == 'wien2k' and l != 2:
        raise ValueError(
            "spherical_to_cubic: wien2k convention implemented only for l=2")
    if l == 0:
        cubic_names = ("s")
    elif l == 1:
        cubic_names = ("x", "y", "z")
        T[0, 0] = 1.0 / sqrt(2)
        T[0, 2] = -1.0 / sqrt(2)
        T[1, 0] = 1j / sqrt(2)
        T[1, 2] = 1j / sqrt(2)
        T[2, 1] = 1.0
    elif l == 2:
        if convention == 'wien2k':
            cubic_names = ("z^2", "x^2-y^2", "xy", "yz", "xz")
            T[0, 2] = 1.0
            T[1, 0] = 1.0 / sqrt(2)
            T[1, 4] = 1.0 / sqrt(2)
            T[2, 0] = -1.0 / sqrt(2)
            T[2, 4] = 1.0 / sqrt(2)
            T[3, 1] = 1.0 / sqrt(2)
            T[3, 3] = -1.0 / sqrt(2)
            T[4, 1] = 1.0 / sqrt(2)
            T[4, 3] = 1.0 / sqrt(2)
        else:
            cubic_names = ("xy", "yz", "z^2", "xz", "x^2-y^2")
            T[0, 0] = 1j / sqrt(2)
            T[0, 4] = -1j / sqrt(2)
            T[1, 1] = 1j / sqrt(2)
            T[1, 3] = 1j / sqrt(2)
            T[2, 2] = 1.0
            T[3, 1] = 1.0 / sqrt(2)
            T[3, 3] = -1.0 / sqrt(2)
            T[4, 0] = 1.0 / sqrt(2)
            T[4, 4] = 1.0 / sqrt(2)
    elif l == 3:
        cubic_names = ("x(x^2-3y^2)", "z(x^2-y^2)", "xz^2", "z^3", "yz^2",
                       "xyz", "y(3x^2-y^2)")
        T[0, 0] = 1.0 / sqrt(2)
        T[0, 6] = -1.0 / sqrt(2)
        T[1, 1] = 1.0 / sqrt(2)
        T[1, 5] = 1.0 / sqrt(2)
        T[2, 2] = 1.0 / sqrt(2)
        T[2, 4] = -1.0 / sqrt(2)
        T[3, 3] = 1.0
        T[4, 2] = 1j / sqrt(2)
        T[4, 4] = 1j / sqrt(2)
        T[5, 1] = 1j / sqrt(2)
        T[5, 5] = -1j / sqrt(2)
        T[6, 0] = 1j / sqrt(2)
        T[6, 6] = 1j / sqrt(2)
    else:
        raise ValueError("spherical_to_cubic: implemented only for l=0,1,2,3")

    return np.matrix(T)

# Names of cubic harmonics
def cubic_names(l):
    r"""
    Get the names of the cubic harmonics.

    Parameters
    ----------
    l : integer or string
        Angular momentum of shell being treated.
        Also takes 't2g' and 'eg' as arguments.

    Returns
    -------
    cubic_names : tuple of strings
                  Names of the orbitals.

    """
    if l == 0 or l == 's':
        return ("s")
    elif l == 1 or l == 'p':
        return ("x", "y", "z")
    elif l == 2 or l == 'd':
        return ("xy", "yz", "z^2", "xz", "x^2-y^2")
    elif l == 't2g':
        return ("xy", "yz", "xz")
    elif l == 'eg':
        return ("z^2", "x^2-y^2")
    elif l == 3 or l == 'f':
        return ("x(x^2-3y^2)", "z(x^2-y^2)", "xz^2", "z^3", "yz^2", "xyz",
                "y(3x^2-y^2)")
    else:
        raise ValueError("cubic_names: implemented only for l=0,1,2,3")

# Convert U,J -> radial integrals F_k
def U_J_to_radial_integrals(l, U_int, J_hund):
    r"""
    Determine the radial integrals F_k from U_int and J_hund.

    Parameters
    ----------
    l : integer
        Angular momentum of shell being treated (l=2 for d shell, l=3 for f shell).
    U_int : scalar
            Value of the screened Hubbard interaction.
    J_hund : scalar
             Value of the Hund's coupling.

    Returns
    -------
    radial_integrals : list
                       Slater integrals [F0,F2,F4,..].

    """

    F = np.zeros((l + 1), dtype=float)
    if l == 1:
        F[0] = U_int
        F[1] = 5.0 * J_hund
    elif l == 2:
        F[0] = U_int
        F[1] = J_hund * 14.0 / (1.0 + 0.63)
        F[2] = 0.630 * F[1]
    elif l == 3:
        F[0] = U_int
        F[1] = 6435.0 * J_hund / (
            286.0 + 195.0 * 451.0 / 675.0 + 250.0 * 1001.0 / 2025.0)
        F[2] = 451.0 * F[1] / 675.0
        F[3] = 1001.0 * F[1] / 2025.0
    else:
        raise ValueError(
            "U_J_to_radial_integrals: implemented only for l=1,2,3")

    return F

# Convert radial integrals F_k -> U,J
def radial_integrals_to_U_J(l, F):
    r"""
    Determine U_int and J_hund from the radial integrals.

    Parameters
    ----------
    l : integer
        Angular momentum of shell being treated (l=2 for d shell, l=3 for f shell).
    F : list
        Slater integrals [F0,F2,F4,..].

    Returns
    -------
    U_int : scalar
            Value of the screened Hubbard interaction.
    J_hund : scalar
             Value of the Hund's coupling.

    """
    if l == 1:
        U_int = F[0]
        J_hund = F[1] / 5.0
    elif l == 2:
        U_int = F[0]
        J_hund = F[1] * (1.0 + 0.63) / 14.0
    elif l == 3:
        U_int = F[0]
        J_hund = F[1] * (
            286.0 + 195.0 * 451.0 / 675.0 + 250.0 * 1001.0 / 2025.0) / 6435.0
    else:
        raise ValueError(
            "radial_integrals_to_U_J: implemented only for l=1,2,3")

    return U_int, J_hund

# Angular matrix elements of particle-particle interaction
# (2l+1)^2 ((l 0) (k 0) (l 0))^2 \sum_{q=-k}^{k} (-1)^{m1+m2+q} ((l -m1) (k q) (l m3)) ((l -m2) (k -q) (l m4))
def angular_matrix_element(l, k, m1, m2, m3, m4):
    r"""
    Calculate the angular matrix element

    .. math::
       (2l+1)^2
       \begin{pmatrix}
            l & k & l \\
            0 & 0 & 0
       \end{pmatrix}^2
       \sum_{q=-k}^k (-1)^{m_1+m_2+q}
       \begin{pmatrix}
            l & k & l \\
         -m_1 & q & m_3
       \end{pmatrix}
       \begin{pmatrix}
            l & k  & l \\
         -m_2 & -q & m_4
       \end{pmatrix}.

    Parameters
    ----------
    l : integer
    k : integer
    m1 : integer
    m2 : integer
    m3 : integer
    m4 : integer

    Returns
    -------
    ang_mat_ele : scalar
                  Angular matrix element.

    """
    ang_mat_ele = 0
    for q in range(-k, k + 1):
        ang_mat_ele += three_j_symbol(
            (l, -m1), (k, q), (l, m3)) * three_j_symbol(
                (l, -m2), (k, -q), (l, m4)) * (-1.0
                                               if (m1 + q + m2) % 2 else 1.0)
    ang_mat_ele *= (2 * l + 1)**2 * (three_j_symbol((l, 0), (k, 0), (l, 0))**2)
    return ang_mat_ele

# Wigner 3-j symbols
# ((j1 m1) (j2 m2) (j3 m3))
def three_j_symbol(jm1, jm2, jm3):
    r"""
    Calculate the three-j symbol

    .. math::
       \begin{pmatrix}
        l_1 & l_2 & l_3\\
        m_1 & m_2 & m_3
       \end{pmatrix}.

    Parameters
    ----------
    jm1 : tuple of integers
          (j_1 m_1)
    jm2 : tuple of integers
          (j_2 m_2)
    jm3 : tuple of integers
          (j_3 m_3)

    Returns
    -------
    three_j_sym : scalar
                  Three-j symbol.

    """
    j1, m1 = jm1
    j2, m2 = jm2
    j3, m3 = jm3

    if (m1 + m2 + m3 != 0 or m1 < -j1 or m1 > j1 or m2 < -j2 or m2 > j2
            or m3 < -j3 or m3 > j3 or j3 > j1 + j2 or j3 < abs(j1 - j2)):
        return .0

    three_j_sym = -1.0 if (j1 - j2 - m3) % 2 else 1.0
    three_j_sym *= sqrt(
        fact(j1 + j2 - j3) * fact(j1 - j2 + j3) * fact(-j1 + j2 + j3) /
        fact(j1 + j2 + j3 + 1))
    three_j_sym *= sqrt(
        fact(j1 - m1) * fact(j1 + m1) * fact(j2 - m2) * fact(j2 + m2) *
        fact(j3 - m3) * fact(j3 + m3))

    t_min = max(j2 - j3 - m1, j1 - j3 + m2, 0)
    t_max = min(j1 - m1, j2 + m2, j1 + j2 - j3)

    t_sum = 0
    for t in range(t_min, t_max + 1):
        t_sum += (-1.0 if t % 2 else 1.0) / (
            fact(t) * fact(j3 - j2 + m1 + t) * fact(j3 - j1 - m2 + t) *
            fact(j1 + j2 - j3 - t) * fact(j1 - m1 - t) * fact(j2 + m2 - t))

    three_j_sym *= t_sum
    return three_j_sym

# Clebsch-Gordan coefficients
# < j1 m1 j2 m2 | j3 m3 > = (-1)^{j1-j2+m3} \sqrt{2j3+1} ((j1 m1) (j2 m2) (j3 -m3))
def clebsch_gordan(jm1, jm2, jm3):
    r"""
    Calculate the Clebsh-Gordan coefficient

    .. math::
       \langle j_1 m_1 j_2 m_2 | j_3 m_3 \rangle = (-1)^{j_1-j_2+m_3} \sqrt{2 j_3 + 1}
       \begin{pmatrix}
         j_1 & j_2 & j_3\\
         m_1 & m_2 & -m_3
       \end{pmatrix}.

    Parameters
    ----------
    jm1 : tuple of integers
          (j_1 m_1)
    jm2 : tuple of integers
          (j_2 m_2)
    jm3 : tuple of integers
          (j_3 m_3)

    Returns
    -------
    cgcoeff : scalar
              Clebsh-Gordan coefficient.

    """
    norm = sqrt(2 * jm3[0] + 1) * (-1 if jm1[0] - jm2[0] + jm3[1] % 2 else 1)
    return norm * three_j_symbol(jm1, jm2, (jm3[0], -jm3[1]))

# Create subarray containing columns in idxlist
# e.g. idxlist = [(0),(2,3),(0,1,2,3)] gives
#  column 0 for 1st dim,
#  columns 2 and 3 for 2nd dim,
#  columns 0,1,2 and 3 for 3rd dim.
def subarray(a, idxlist, n=None):
    r"""
    Extract a subarray from a matrix-like object.

    Parameters
    ----------
    a : matrix or array
    idxlist : list of tuples
              Columns that need to be extracted for each dimension.

    Returns
    -------
    subarray : matrix or array

    Examples
    --------
    idxlist = [(0),(2,3),(0,1,2,3)] gives

    - column 0 for 1st dim,
    - columns 2 and 3 for 2nd dim,
    - columns 0, 1, 2 and 3 for 3rd dim.

    """
    if n is None: n = len(a.shape) - 1
    sa = a[tuple(slice(x) for x in a.shape[:n]) + (idxlist[n], )]
    return subarray(sa, idxlist, n - 1) if n > 0 else sa


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from rocketmq.client import Producer,Message
from utils.my_logger import logger
import time
import re

def push(num):
    tt = re.findall('^\d{13}', str(time.time()).replace('.', ''))[0]
    print(type(tt))
    producer = Producer('PID-001')
    producer.set_namesrv_addr('192.168.90.131:9876')
    producer.start()
    msg = Message('linkcld_tdmp_gantry')
    msg.set_keys('key')
    msg.set_tags('bayonet')
    body = ("[{'enn':'河南惠济站'," \
            "'gid':'G003041003005620010'," \
            "'ipnc':'%s'," \
            "'ln':'201'," \
            "'marked':true," \
            "'mid':'020000410101620060354820210315072344'," \
            "'mt':3," \
            "'pnc':'%s'," \
            "'pt':%s," \
            "'rt':1615767977000," \
            "'vt':'2'}]"% (num,num,tt))
    msg.set_body(body)
    ret = producer.send_sync(msg)
    print(ret.status, ret.msg_id, ret.offset)
    producer.shutdown()

def main():
    '''MQ推送预警 '''
    try:
        push('豫A00001_1')
        raise
    except Exception:
        logger.exception("RocketMQ推送预警失败")
        raise
    else:
        logger('MQ推送成功')
if __name__ == '__main__':
    logger.info('mqpush.py开始运行')
    main()


import tkinter as tk
import json
from api import functions

class window(tk.Tk):
	def __init__(self):
		super().__init__()
		self.geometry('767x445')
		self.title('Get daily news')
		self.funcs = functions()

	def main_window(self):
		# Creating required labels and frame
		title = tk.Label(self, text="Get news", font="monospace 20")
		frame = tk.Frame(self)

		# Creating required variables
		query = tk.StringVar()
		query.set('')

		# Creating entry boxes
		query_box = tk.Entry(frame, textvariable=query)

		# Creating label for entry box
		query_label = tk.Label(frame, text="Query: ")

		# Creating buttons
		submit = tk.Button(frame, text="Submit", command=lambda: (self.window_result(query.get())))

		# Packing everything
		query_label.grid(column=1, row=1)
		query_box.grid(column=2, row=1)
		submit.grid(column=1, row=2)

		title.pack()
		frame.pack()

	def show_json(self, json_data: str):
		# Creating required frame
		frame = tk.Frame(self.slave_window)

		# Creating required variables
		data = json.loads(json_data)
		articles = data.get('articles')
		titles = []
		selected_title = tk.StringVar()
		selected_title.set('Options')

		# Functions which will be used
		def find_selected_article(title: str, articles: list):
			for article in articles:
				if article.get('title') == title:
					return article
			return False

		def see_news(selected_article: dict):
			#self.slave_window.geometry('')
			frame.pack_forget()
			frame2 = tk.Frame(self.slave_window)

			title = tk.Label(frame2, text='Title: '+selected_article.get('title'))
			author = tk.Label(frame2, text='Author: '+selected_article.get('author'))
			source = tk.Label(frame2, text="Source: "+selected_article.get('source').get('name'))
			link = tk.Label(frame2, text='Link: '+selected_article.get('url'))
			date = tk.Label(frame2, text='Published At: '+selected_article.get('publishedAt'))
			content = tk.Label(frame2, text='Content: '+selected_article.get('content'))

			title.grid(column=1, row=1)
			author.grid(column=1, row=2)
			source.grid(column=1, row=3)
			link.grid(column=1, row=4)
			date.grid(column=1, row=5)
			content.grid(column=1, row=6)

			frame2.pack()

		for article in articles:
			titles.append(article.get('title'))

		# Creating label
		label_choose = tk.Label(frame, text="Choose News: ")

		# Creating option menu
		entry_box = tk.OptionMenu(frame, selected_title, *titles)

		# Creating buttons
		submit = tk.Button(frame, text="Submit", command=lambda: (frame.pack_forget(), see_news(find_selected_article(selected_title.get(), articles))))

		# Packing Everything
		label_choose.grid(column=1, row=1)
		entry_box.grid(column=2, row=1)
		submit.grid(column=1, row=2)
		frame.pack()

	def window_result(self, query: str):
		self.slave_window = tk.Toplevel(self)

		self.slave_window.title(f'GET {query} NEWS')
		self.slave_window.geometry('700x300')

		json_text = self.funcs.get(query)

		self.show_json(json_text)

if __name__=='__main__':
	windo = window()
	windo.main_window()
	windo.mainloop()


#!/usr/bin/env python3
import rospy
import sounddevice as sd
import numpy as np
import queue
import sys
import sounddevice as sd
from audio_universal.msg import AudioData

'''
~output_device: use `python3 -m sounddevice` to get device list, numerical device ID or case-insensitive substrings is ok.
~channels: 1
~refresh_rate: 30
~latency: 'high'
~blocksize: 512
~dtype: 'float32'
~samplerate: 44100 48000 88200 96000 192000
'''

class audio_play:
    def __init__(self):
        self.initROS()
        self.q = queue.Queue()
        self.q_connects = queue.Queue()
        self.q_channels = queue.Queue()
        self.stream = sd.OutputStream(device=self.output_device,
                                      samplerate=self.samplerate,
                                      blocksize=self.blocksize,
                                      dtype=self.dtype,
                                      latency=self.latency,
                                      channels=self.channels,
                                      callback=self.audio_callback)
        with self.stream:
            rospy.spin()

    def initROS(self):
        rospy.init_node('audio_record', anonymous=True)
        self.output_device = rospy.get_param("~output_device", default=None)
        self.channels = rospy.get_param("~channels", default=1)
        self.refresh_rate = rospy.get_param("~refresh_rate", default=30)
        self.latency = rospy.get_param("~latency", default='high')
        self.blocksize = rospy.get_param("~blocksize", default=512)
        self.dtype = rospy.get_param("~dtype", default='float32')
        self.samplerate = rospy.get_param("~samplerate", default=48000)
        rospy.Subscriber('/audio_record_data', AudioData, self.AudioData_callback)

    def audio_callback(self, outdata, frames, time, status):
        if status:
            rospy.logwarn(status)
        try:
            data = self.q.get_nowait()
            connects = self.q_connects.get_nowait()
            in_channels = self.q_channels.get_nowait()
            data = data.reshape(self.blocksize, in_channels)
            if len(connects) / 2 != len(connects) // 2:
                raise Exception
            for idx in range(0, len(connects) // 2):
                if (connects[idx * 2] in range(0, self.channels)) and (connects[idx * 2 + 1] in range(0, self.channels)):
                    outdata[:, connects[idx * 2 + 1]] = data[:, connects[idx * 2]]
        except queue.Empty as e:
            # rospy.logwarn('Buffer is empty: increase buffersize?')
            outdata[:] = np.zeros_like(outdata)

    def AudioData_callback(self, AudioData):
        self.q.put(np.frombuffer(AudioData.data, dtype=self.dtype))
        self.q_connects.put(np.frombuffer(AudioData.connects, dtype='uint32'))
        self.q_channels.put(AudioData.channels)

if __name__ == '__main__':
    audio_play()


import json
import os
import boto3
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)
dynamodb = boto3.resource("dynamodb")

def lambda_handler(event, context):
    logger.info(f"EVENT: {event}")

    statistics_table_name = os.environ["STATISTICS_TABLE_NAME"]
    statistics_table = dynamodb.Table(statistics_table_name)
    try:
        # Get the new item from the DynamoDB event
        for item in event["Records"]:
            new_item = item["dynamodb"]
            if "NewImage" not in new_item.keys():
                continue
            new_item = new_item["NewImage"]

            # Extract required attributes for data processing
            event_type = new_item.get("EventType", {}).get("S")
            event_details = new_item.get("EventDetails", {}).get("S")
            team_name = new_item.get("Team", {}).get("S")
            opponent_team_name = new_item.get("Opponent", {}).get("S")
            match_id = new_item.get("MatchID", {}).get("S")

            if not event_type or not team_name or not match_id or not event_details:
                return

            event_details = json.loads(event_details)
            # Calculate statistics based on the event_type
            if event_type == "goal":
                update_statistics(statistics_table, match_id, team_name, opponent_team_name, "total_goals_scored", 1)
                update_statistics(statistics_table, match_id, opponent_team_name, team_name,"total_goals_conceded", 1)
            elif event_type == "foul":
                update_statistics(statistics_table, match_id, team_name, opponent_team_name, "total_fouls", 1)
                update_statistics(statistics_table, match_id, opponent_team_name, team_name, "total_fouls", 0)

            update_match_result(statistics_table, team_name, opponent_team_name, match_id)

            response = statistics_table.get_item(Key={"TeamName": team_name, "MatchID": match_id}, AttributesToGet=["Date"])
            if "Item" not in response or not response.get("Item", {}):
                date = new_item.get("Timestamp", {}).get("S")
                update_date(statistics_table, team_name, match_id, date)
                update_date(statistics_table, opponent_team_name, match_id, date)

    except Exception as e:
        print(f"Error processing data: {e}")
        raise e

def update_statistics(statistics_table, match_id, team_name, opponent_name, statistic_type, value):
    # Get the existing statistics for the team from the statistics table
    response = statistics_table.get_item(Key={"TeamName": team_name, "MatchID": match_id})

    if "Item" not in response or not response.get("Item", {}):
        # If the team and match are not present in the statistics table, create a new entry
        item = {
            "TeamName": team_name,
            "MatchID": match_id,
            "Opponent": opponent_name,
            statistic_type: str(value)
        }
        statistics_table.put_item(Item=item)
    else:
        # If the team and match are already present in the statistics table, update the existing entry
        existing_item = response["Item"]
        existing_item[statistic_type] = str(int(existing_item.get(statistic_type, "0")) + value)
        statistics_table.put_item(Item=existing_item)

def update_match_result(statistics_table, team_name, opponent_name, match_id):
    response = statistics_table.get_item(Key={"TeamName": team_name, "MatchID": match_id})
    response = response.get("Item", {})
    goals_scored = response.get("total_goals_scored", "0")
    goals_conceded = response.get("total_goals_conceded", "0")

    # Calculate match result based on goals scored and conceded
    results = {
        1: "win",
        -1: "loss",
        0: "draw",
    }
    sign = lambda x: -1 if x < 0 else (1 if x > 0 else 0)
    result = sign(int(goals_scored) - int(goals_conceded))

    # Update match result for the team
    response = statistics_table.update_item(
        Key={"TeamName": team_name, "MatchID": match_id},
        UpdateExpression="SET #result = :result",
        ExpressionAttributeNames={"#result": "Result"},
        ExpressionAttributeValues={":result": results[result]}
    )

    # Update match result for the opponent team
    response = statistics_table.update_item(
        Key={"TeamName": opponent_name, "MatchID": match_id},
        UpdateExpression="SET #result = :result",
        ExpressionAttributeNames={"#result": "Result"},
        ExpressionAttributeValues={":result": results[-result]}
    )

def update_date(table, team_name, match_id, date):
    # Update the "Date" attribute in the StatisticsTable for the team and match_id
    table.update_item(
        Key={"TeamName": team_name, "MatchID": match_id},
        UpdateExpression="SET #dateAttr = :dateValue",
        ExpressionAttributeNames={"#dateAttr": "Date"},
        ExpressionAttributeValues={":dateValue": str(date)}
    )


import sys
import cv2
import numpy as np
from os import listdir

PY3 = sys.version_info[0] == 3

#Define the parameters
SIZE = 32
CLASS_NUMBER = 6

#Read the traffic sign dataset and store the dataset and labels into a list
def load_traffic_dataset():
    dataset = []
    labels = []
    for sign_type in range(CLASS_NUMBER):
        sign_list = listdir("./dataset/{}".format(sign_type))
        for sign_file in sign_list:
            if '.png' in sign_file:
                path = "./dataset/{}/{}".format(sign_type,sign_file)
                print(path)
                img = cv2.imread(path,0)
                img = cv2.resize(img, (SIZE, SIZE))
                img = np.reshape(img, [SIZE, SIZE])
                dataset.append(img)
                labels.append(sign_type)
    return np.array(dataset), np.array(labels)

#Deskew the images
def deskew(img):
    m = cv2.moments(img)
    if abs(m['mu02']) < 1e-2:
        return img.copy()
    skew = m['mu11']/m['mu02']
    M = np.float32([[1, skew, -0.5*SIZE*skew], [0, 1, 0]])
    img = cv2.warpAffine(img, M, (SIZE, SIZE), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_LINEAR)
    return img

#Define a class for SVM model object
class StatModel(object):
    def load(self, fn):
        self.model.load(fn)  # Known bug: https://github.com/opencv/opencv/issues/4969
    def save(self, fn):
        self.model.save(fn)

class SVM(StatModel):
    def __init__(self, C = 12.5, gamma = 0.50625):
        self.model = cv2.ml.SVM_create()
        self.model.setGamma(gamma)
        self.model.setC(C)
        self.model.setKernel(cv2.ml.SVM_RBF)
        self.model.setType(cv2.ml.SVM_C_SVC)

    def train(self, samples, responses):
        self.model.train(samples, cv2.ml.ROW_SAMPLE, responses)

    def predict(self, samples):
        return self.model.predict(samples)[1].ravel()

def preprocess_simple(data):
    return np.float32(data).reshape(-1, SIZE*SIZE) / 255.0

def get_hog() :
    winSize = (20,20)
    blockSize = (10,10)
    blockStride = (5,5)
    cellSize = (10,10)
    nbins = 9
    deriveAperture = 1
    winSigma = -1.0
    histogramNormType = 0
    L2HysThreshold = 0.2
    gammaCorrection = 1
    nlevels = 64
    signedGradient = True

    hog = cv2.HOGDescriptor(winSize,blockSize,blockStride,cellSize,nbins,deriveAperture,winSigma,histogramNormType,L2HysThreshold,gammaCorrection,nlevels, signedGradient)
    return hog

#Train the model
def training():
    print('Loading data...')
    data, labels = load_traffic_dataset()
    print(data.shape)

    print('Shuffling data...')
    rand = np.random.RandomState(10)
    shuffle = rand.permutation(len(data))
    data, labels = data[shuffle], labels[shuffle]

    print('Deskewing images...')
    data_deskewed = list(map(deskew, data))

    print('Defining HoG parameters...')
    hog = get_hog()

    print('Calculating HoG descriptor for every image...')
    hog_descriptors = []
    for img in data_deskewed:
        hog_descriptors.append(hog.compute(img))
    hog_descriptors = np.squeeze(hog_descriptors)

    print('Training SVM model...')
    model = SVM()
    model.train(hog_descriptors, labels)

    print('Saving SVM model...')
    model.save('data_svm.dat')
    return model

#Get the label of detected traffic sign using the SVM model
def getLabel(model, data):
    gray = cv2.cvtColor(data, cv2.COLOR_BGR2GRAY)
    img = [cv2.resize(gray,(SIZE,SIZE))]
    img_deskewed = list(map(deskew, img))
    hog = get_hog()
    hog_descriptors = np.array([hog.compute(img_deskewed[0])])
    hog_descriptors = np.reshape(hog_descriptors, [-1, hog_descriptors.shape[1]])
    return int(model.predict(hog_descriptors)[0])


import imdb
import json
from tqdm import tqdm

ia = imdb.IMDb()

DATA_NEEDED = False

with open("./imdb/movie_title-id.json", "r") as data:
    current_data = json.load(data)

with open('./movie_titles_list.json', "r") as movie_list:
    movies = json.load(movie_list)

if DATA_NEEDED:
    for i in tqdm(range(0, len(movies))):
        # searching the name
        name = movies[i]
        try:
            search = ia.search_movie(name)
            # getting the id
            id = search[0].movieID
            current_data[name]=[id]
        except Exception:
            continue

for movie_name in current_data:
    if len(current_data[movie_name]) > 0:
        current_data[movie_name] = current_data[movie_name][0]

with open("./imdB/movie_title-id.json", "w") as data_list:
    json.dump(current_data, data_list)


#!/usr/bin/python

# Imports
### System
import os
import argparse

### Python
import numpy as np
import cv2 as cv
from tqdm.auto import tqdm
import matplotlib.pyplot as plt

### Pytorch
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F

### TODO: Use WandB, Pytorch Lightning & torchsummary
import wandb
import lightning.pytorch as pl
from lightning.pytorch import LightningDataModule, LightningModule, Trainer, seed_everything
from lightning.pytorch import Callback
from lightning.pytorch.callbacks import DeviceStatsMonitor, TQDMProgressBar, ModelCheckpoint, EarlyStopping, LearningRateMonitor
from lightning.pytorch.loggers import WandbLogger
from torchsummary import summary

### Timm
import timm
import timm.optim

### Custom
from custom_dataloader import get_dataloaders

# DONE_TODO 1: Import pytorch lightning, wandb(logging), timm and use it to load a pretrained model
# Done_TODO 2: Modify the model's classifier to output 3 classes instead of X (defined by the model)
# Done_TODO 3: Train the model + Logging & Saving best ckpt for 10 epochs and report test accuracy

def get_args():
    args = argparse.ArgumentParser(description='Transfer Learning')
    args.add_argument('--model', '-m', type=str, default='vgg16', required=True, help='Model to use [vgg16, vgg19, resnet50, resnet101, effb4, effb5]')
    args.add_argument('--epochs', '-e', type=int, default=10, help='Number of epochs to train for')
    args.add_argument('--batch_size', type=int, default=32, help='Batch size')
    args.add_argument('--device', '-d', type=str, default='cuda', required=True, help='Device to use [cpu, cuda:0, cuda:1, cuda]')
    args.add_argument('--mode', '-md', type=str, default='train', help='Mode to run: [train, trainX, test]. train = finetune only classifier layer. trainX = finetune last few layers including the classifier. test = test the model')
    args.add_argument('--ckpt_path', '-cp', type=str, default="", help='Path to checkpoint to load')
    args.add_argument('--lr', '-lr', type=float, default=1e-3, help='Learning rate')
    args.add_argument('--num_workers', '-nw', type=int, default=8, help='Number of workers for dataloader')
    args.add_argument('--exp_name', '-en', type=str, default='generic_exp', help='Experiment name for wandb')
    args.add_argument('--use_cam', action='store_true', help='Use Class Activation Maps Loss')
    # args.print_help()
    return args.parse_args()

def get_model(modelName):
    # VGG Fam
    if modelName == 'vgg16':
        model = timm.create_model('vgg16', pretrained=True)
    elif modelName == 'vgg19':
        model = timm.create_model('vgg19', pretrained=True)

    # Res Fam: Using catavgmax pooling to increase number of features
    elif modelName == 'resnet50':
        model = timm.create_model('resnet50', pretrained=True)
    elif modelName == 'resnet101':
        model = timm.create_model('resnet101', pretrained=True)

    # EfficientNet Fam: Using catavgmax pooling here as well
    elif modelName == 'effb4':
        model = timm.create_model('efficientnet_b4', pretrained=True)
    elif modelName == 'effb5':
        model = timm.create_model('efficientnet_b5', pretrained=True)

    return model

def check_args(args):
    if args.model not in ['vgg16', 'vgg19', 'resnet50', 'resnet101', 'effb4', 'effb5']:
        raise ValueError('[!] Invalid model')

    if 'cuda' in args.device and not torch.cuda.is_available():
        raise ValueError('[!] Cuda not available')

    if 'cuda:' in args.device:
        if int(args.device[-1]) >= torch.cuda.device_count():
            raise ValueError('[!] Invalid cuda device. You have lesser cuda devices than the one you specified')

class LIT_TL(pl.LightningModule):
    def __init__(self, model, modelName = "brrr", config: dict = None):
        super().__init__()
        self.save_hyperparameters(ignore=['model'])
        self.modelName = modelName
        self.config = config

        if "vgg" in modelName:
            self.num_filters = model.head.fc.in_features
        elif "eff" in modelName:
            self.num_filters = model.classifier.in_features
        else:
            self.num_filters = model.fc.in_features

        layers = list(model.children())[:-1]
        self.feature_extractor = nn.Sequential(*layers)
        num_target_classes = config['classes']

        if "vgg" in modelName: # custom classifier head for vggs ;)
            self.classifier = nn.Sequential(*[
                model.head.global_pool,
                model.head.drop,
                nn.Linear(in_features=4096, out_features=3, bias=True),
                model.head.flatten,
            ])
        else:
            self.classifier = nn.Linear(self.num_filters, num_target_classes)

        self.classifier.apply(self.init_xavier)
        self.ce_loss = nn.CrossEntropyLoss()

        # TODO: Apply Class-Activation-Maps Loss and visualize it
        self.use_cam = config['use_cam']
        if self.use_cam:
            self.gap_fc = nn.utils.spectral_norm(nn.Linear(self.num_filters, 1, bias=False))
            self.gmp_fc = nn.utils.spectral_norm(nn.Linear(self.num_filters, 1, bias=False))
            self.conv1x1 = nn.Conv2d(self.num_filters * 2, self.num_filters, kernel_size=1, stride=1, bias=True)
            self.conv_classifier = nn.utils.spectral_norm(
                nn.Conv2d(self.num_filters, 1, kernel_size=4, stride=1, padding=0, bias=False))
            self.cam_loss = nn.CrossEntropyLoss()

    def init_xavier(self, m):
        if type(m) == nn.Linear:
            torch.nn.init.xavier_uniform_(m.weight)
            m.bias.data.fill_(0.01)

    def calculate_accuracy(self, yhat, y):
        return 100. * torch.sum(yhat == y) / len(y)

    def forward(self, x):
        self.feature_extractor.eval()
        with torch.no_grad():
            rep = self.feature_extractor(x)

        if self.use_cam:
            gap = torch.nn.functional.adaptive_avg_pool2d(rep, 1)
            gap_logit = self.gap_fc(gap.view(rep.shape[0], -1))
            gap_weight = list(self.gap_fc.parameters())[0]
            gap = rep * gap_weight.unsqueeze(2).unsqueeze(3)

            gmp = torch.nn.functional.adaptive_max_pool2d(rep, 1)
            gmp_logit = self.gmp_fc(gmp.view(rep.shape[0], -1))
            gmp_weight = list(self.gmp_fc.parameters())[0]
            gmp = rep * gmp_weight.unsqueeze(2).unsqueeze(3)

            c_logit = torch.cat([gap_logit, gmp_logit], 1)
            rep = torch.cat([gap, gmp], 1)
            rep = self.leaky_relu(self.conv1x1(rep))

            heatmap = torch.sum(rep, dim=1, keepdim=True)

            rep = self.pad(rep)
            out = self.conv_classifier(rep)

            return out, c_logit, heatmap

        else:
            if not "vgg" in self.modelName:
                rep = rep.flatten(1)
            out = self.classifier(rep)
            return out

    def configure_optimizers(self):
        opt = timm.optim.AdamW((param for param in self.classifier.parameters() if param.requires_grad),
                               lr=self.config['lr'],
                               weight_decay=self.config['decay'])
        scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(opt,
                                                                         T_0=self.config['T_0'],
                                                                         eta_min=self.config['eta_min'])
        return [opt], [scheduler]

    def training_step(self, batch, batch_idx):
        img = batch['image'].to(self.device)
        # mask = batch['mask'] # Not Needed in classification setting
        y = batch['class_label'].to(self.device)

        if self.use_cam:
            y_hat, logits, heatmap = self.forward(img)

            if 1 + batch_idx % 100 == 0:
                plt.savefig(f"{self.config['model']}_{batch_idx}.png", heatmap.squeeze(0).cpu().numpy())

            cam_loss = self.cam_loss(logits, y)
            loss = self.ce_loss(y_hat, y) + cam_loss
        else:
            y_hat = self.forward(img)
            loss = self.ce_loss(y_hat, y)
            preds = torch.argmax(y_hat, dim=1)
        # print(f"y: {y}, y_hat: {y_hat}")

        # DONE_TODO: Log train metrics here
        train_step_acc = self.calculate_accuracy(preds, y) # DONE_TODO: Calculate train accuracy here
        self.log("train_acc", train_step_acc, on_step=True, on_epoch=True, prog_bar=True, logger=True)

        return loss

    def validation_step(self, batch, batch_idx):
        img = batch['image'].to(self.device)
        # mask = batch['mask'] # Not Needed in classification setting
        y = batch['class_label'].to(self.device)
        # print(f"{i+1} | {img.shape} | {y}")

        y_hat = self.forward(img)
        preds = torch.argmax(y_hat, dim=1)
        test_acc = self.calculate_accuracy(preds, y)

        self.log("test_acc", test_acc, on_step=True, on_epoch=True, prog_bar=True, logger=True)

        # DONE_TODO: Add images to the logger for one batch
        if (batch_idx + 1) % 10 == 0:
            id2cat = {'0': 'authentic', '1': 'copy-moved', '2': 'spliced'}
            caption_strs = []
            for i in range(len(img)):
                correct = "Misclassified" if preds[i] != y[i] else "Correct"
                caption_strs.append(f"Pred: {id2cat[str(preds[i].item())]}, Label: {id2cat[str(y[i].item())]} | {correct}")

            self.logger.log_image(
                key=f"Validation Batch: {batch_idx + 1}",
                images=[img[i] for i in range(len(img))],
                caption=caption_strs,
            )

    def test_step(self, batch, batch_idx):
        # NOTE: Same as validation loop minus the image logging
        # No image logging so that export can be done easily

        img = batch['image'].to(self.device)
        # mask = batch['mask'] # Not Needed in classification setting
        y = batch['class_label'].to(self.device)
        # print(f"{i+1} | {img.shape} | {y}")

        y_hat = self.forward(img)
        preds = torch.argmax(y_hat, dim=1)
        test_acc = self.calculate_accuracy(preds, y)

        self.log("test_acc", test_acc, on_step=True, on_epoch=True, prog_bar=True, logger=True)

def get_config(args):
    config = {
        'seed': 42,
        'model': args.model,
        'mode': args.mode,
        'lr': args.lr,
        'batch_size': args.batch_size,
        'epochs': args.epochs,
        'device': args.device,
        'num_workers': args.num_workers,
        'T_0': 50,
        'eta_min': 6e-4,
        'classes': 3,
        'decay': 1e-3,
        'exp_name': args.exp_name,
        'use_cam': args.use_cam
    }
    return config

if __name__ == '__main__':
    args = get_args()
    # print("Total devices:", torch.cuda.device_count())

    check_args(args) # will also set args.device properly
    config = get_config(args)
    seed_everything(42)
    if torch.cuda.is_available():
        device_name = torch.cuda.get_device_name(args.device)
    else:
        device_name = 'cpu'
    print("--------------------------------------------\nSelected device:", device_name,"\n--------------------------------------------")
    print(f"[+] Model Selected: {config['model']}")

    model = get_model(config['model'])

    lit_model = LIT_TL(model, config['model'], config)

    train_dataloader, test_dataloader = get_dataloaders(batch_size=config['batch_size'],
                                                        num_workers=config['num_workers'])

    # Callbacks
    checkpoint_callback = ModelCheckpoint(monitor="test_acc",
                                          mode="max",
                                          save_top_k=1,
                                          dirpath="checkpoints/",
                                          filename=f"{config['model']}" + "_{test_acc:.3f}")

    lr_monitor = LearningRateMonitor(logging_interval='step', log_momentum=True)
    # early_stop_callback = EarlyStopping(monitor="loss", patience=99)

    wandb.login()

    if config['exp_name'] == 'generic_exp':
        fnam = f"{config['model']}_GE"
    else:
        fnam = config['exp_name']

    wandb_logger = WandbLogger(project='forgery_detection',
                           name=f"TL_{fnam}",
                           config=config,
                           job_type='finetuning',
                           log_model="all")
    # call trainer
    trainer = Trainer(fast_dev_run=False,
                      inference_mode=False,  # to enable grad enabling during inference
                      max_epochs=config['epochs'],
                      accelerator="gpu" if "cuda" in config['device'] else "cpu",
                      devices=[int(config['device'].split(":")[-1])], # GPU ID that you selected
                      precision="16", # automatic mixed precision training
                      deterministic=True,
                      enable_checkpointing=True,
                      callbacks=[checkpoint_callback, lr_monitor],
                      gradient_clip_val=None,
                      log_every_n_steps=50,
                      logger=wandb_logger, # The absolute best: wandb <3
                      enable_progress_bar=True)

    # fit model
    if config['mode'] == 'train' or config['mode'] == 'trainX': # TODO: Implement trainX mode
        trainer.fit(lit_model, train_dataloader, test_dataloader)
    else:
        # DONE_TODO: Load last checkpoint and test
        if not os.exists(args.ckpt_path):
            args.ckpt_path = checkpoint_callback.best_model_path
        lit_model = LIT_TL.load_from_checkpoint(args.ckpt_path)
        lit_model.freeze()
        trainer.test(lit_model, test_dataloader)

    wandb.finish()


import google
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import base64

keyPath = '../firestoreKEY.json'

def initializeDb(path):
    cred = credentials.Certificate(path)
    firebase_admin.initialize_app(cred)
    db = firestore.client()
    return db

def stringToImage(string, imagePath):
    fh = open(imagePath, "wb")
    fh.write(string.decode('base64'))
    fh.close()

def getFieldFromDB(database, collectionName, docName, fieldName):
    dbRef = database.collection(collectionName).document(docName)
    try:
        doc = dbRef.get()
        str = doc.to_dict()[fieldName]
    except google.cloud.exception.NotFound:
        str = 'NO SUCH DOCUMENT'

    return str

def getDocsFromCol(database, collectionName):
    return database.collection(collectionName).get()

def main():
    database = initializeDb(keyPath)
    # str = getFieldFromDB(database, u'photos', u'04, 03:59AM on December 15, 2018', u'im')
    strIngs = getDocsFromCol(database, u'photos')
    for ims in strIngs:
        docInfo = ims.to_dict()
        stringToImage(docInfo[u'im'], "images/" + ims.id + ".jpg")

if __name__ == '__main__':
    main()


import jwt

JWT_SECRET = "this_is_just_for_testing"

def create_jwt(payload):
    token = jwt.encode(
        payload,
        JWT_SECRET,
        algorithm="HS256"
    )
    return token

def validate_jwt(token):
    try:
        payload = jwt.decode(token, JWT_SECRET, "HS256")
    except:
        raise

    return payload


import copy
import logging
import os.path as osp

import numpy as np
import torch
from fvcore.common.file_io import PathManager
from PIL import Image
from pycocotools import mask as maskUtils

from detectron2.data import detection_utils as utils
from detectron2.data import transforms as T
from detectron2.data.dataset_mapper import DatasetMapper
from detectron2.data.detection_utils import SizeMismatchError
from detectron2.structures import BoxMode

from .augmentation import RandomCropWithInstance
from .detection_utils import (annotations_to_instances, build_augmentation,
                              transform_instance_annotations)

"""
This file contains the default mapping that's applied to "dataset dicts".
"""

__all__ = ["DatasetMapperWithBasis"]

logger = logging.getLogger(__name__)

def segmToRLE(segm, img_size):
    h, w = img_size
    if type(segm) == list:
        # polygon -- a single object might consist of multiple parts
        # we merge all parts into one mask rle code
        rles = maskUtils.frPyObjects(segm, h, w)
        rle = maskUtils.merge(rles)
    elif type(segm["counts"]) == list:
        # uncompressed RLE
        rle = maskUtils.frPyObjects(segm, h, w)
    else:
        # rle
        rle = segm
    return rle

def segmToMask(segm, img_size):
    rle = segmToRLE(segm, img_size)
    m = maskUtils.decode(rle)
    return m

def filter_empty_instances(instances):
    """
    Filter out empty instances in an `Instances` object.

    Args:
        instances (Instances):
        by_box (bool): whether to filter out instances with empty boxes
        by_mask (bool): whether to filter out instances with empty masks
        box_threshold (float): minimum width and height to be considered non-empty
        return_mask (bool): whether to return boolean mask of filtered instances

    Returns:
        Instances: the filtered instances.
        tensor[bool], optional: boolean mask of filtered instances
    """
    pass
    r = []
    r.append(instances.gt_boxes.nonempty())

    if not r:
        return instances
    m = r[0]
    for x in r[1:]:
        m = m & x

    return instances[m]

class DatasetMapperWithBasis(DatasetMapper):
    """
    This caller enables the default Detectron2 mapper to read an additional basis semantic label
    """

    def __init__(self, cfg, is_train=True):
        super().__init__(cfg, is_train)

        # Rebuild augmentations
        logger.info(
            "Rebuilding the augmentations. The previous augmentations will be overridden."
        )
        self.augmentation = build_augmentation(cfg, is_train)

        if cfg.INPUT.CROP.ENABLED and is_train and cfg.MODEL.TRANSFORMER.BOUNDARY_HEAD:
            self.augmentation.insert(
                0,
                RandomCropWithInstance(
                    cfg.INPUT.CROP.TYPE,
                    cfg.INPUT.CROP.SIZE,
                    cfg.INPUT.CROP.CROP_INSTANCE,
                ),
            )
            logging.getLogger(__name__).info(
                "Cropping used in training: " + str(self.augmentation[0])
            )
        if cfg.INPUT.ROTATE and is_train:
            if cfg.MODEL.TRANSFORMER.BOUNDARY_HEAD:
                self.augmentation.insert(0, T.RandomRotation(angle=[-45, 45]))
            else:
                self.augmentation.insert(0, T.RandomRotation(angle=[-90, 90]))

    def __call__(self, dataset_dict):
        """
        Args:
            dataset_dict (dict): Metadata of one image, in Detectron2 Dataset format.

        Returns:
            dict: a format that builtin models in detectron2 accept
        """
        dataset_dict = copy.deepcopy(dataset_dict)  # it will be modified by code below
        # USER: Write your own image loading if it's not from a file
        try:
            image = utils.read_image(
                dataset_dict["file_name"], format=self.image_format
            )
        except Exception as e:
            print(dataset_dict["file_name"])
            print(e)
            raise e
        try:
            utils.check_image_size(dataset_dict, image)
        except SizeMismatchError as e:
            expected_wh = (dataset_dict["width"], dataset_dict["height"])
            image_wh = (image.shape[1], image.shape[0])
            if (image_wh[1], image_wh[0]) == expected_wh:
                print("transposing image {}".format(dataset_dict["file_name"]))
                image = image.transpose(1, 0, 2)
            else:
                raise e

        ######################################################################
        boxes = np.asarray(
            [
                BoxMode.convert(
                    instance["bbox"], instance["bbox_mode"], BoxMode.XYXY_ABS
                )
                for instance in dataset_dict["annotations"]
            ]
        )
        ######################################################################

        # aug_input = T.StandardAugInput(image)
        aug_input = T.StandardAugInput(image, boxes=boxes)

        transforms = aug_input.apply_augmentations(self.augmentation)
        image = aug_input.image

        image_shape = image.shape[:2]  # h, w
        # Pytorch's dataloader is efficient on torch.Tensor due to shared-memory,
        # but not efficient on large generic data structures due to the use of pickle & mp.Queue.
        # Therefore it's important to use torch.Tensor.
        dataset_dict["image"] = torch.as_tensor(
            np.ascontiguousarray(image.transpose(2, 0, 1))
        )

        if not self.is_train:
            dataset_dict.pop("annotations", None)
            dataset_dict.pop("sem_seg_file_name", None)
            dataset_dict.pop("pano_seg_file_name", None)
            return dataset_dict

        if "annotations" in dataset_dict:
            # USER: Modify this if you want to keep them for some reason.
            for anno in dataset_dict["annotations"]:
                if not self.use_instance_mask:
                    anno.pop("segmentation", None)
                if not self.use_keypoint:
                    anno.pop("keypoints", None)

            # USER: Implement additional transformations if you have other types of data
            annos = [
                transform_instance_annotations(
                    obj,
                    transforms,
                    image_shape,
                    keypoint_hflip_indices=self.keypoint_hflip_indices,
                )
                for obj in dataset_dict.pop("annotations")
                if obj.get("iscrowd", 0) == 0
            ]
            instances = annotations_to_instances(
                annos, image_shape, mask_format=self.instance_mask_format
            )

            # dataset_dict["instances"] = instances
            dataset_dict["instances"] = utils.filter_empty_instances(instances)

        return dataset_dict


"""

Steps to run:
python insured_info_scraper.py <account number>
Eg:
python insured_info_scraper.py 20011

Program written in Python 3

Program Output:
1 file:
Insured_Info_<account_num>.json - json file that contains the insured info details

Program Description:
Progam first fetches the ASP login page paramters - __VIEWSTATE, __VIEWSTATEGENERATOR,
etc and then inputs these paramters and the login credentials to login
Then the program stores cookie info and uses it along with the new page parameters
to access the quotes page.
The code then access the account details page by sending the account number
(retrieved as a command line argument) along with other parameter to get the
account details.
The insured information is then scraped and stored into a dictionary var which is
written into a json file

"""
import sys
import requests
from bs4 import BeautifulSoup
import json

# Main Function
def main():
    # Variable to store the json putput
    insured_information = {}
    # Getting the account number from the command line
    account_num = str(sys.argv[1])
    print("Account number entered:")
    print(account_num)
    # Login credentials
    credentials = dict()
    credentials['username'] = 'samplecsrtest'
    credentials['password'] = 'Ik vaari aa---123'
    print("Getting login session parameters to login")
    # Home page URL
    home_page_url = 'https://secure.financepro.net/financepro/default.aspx?company=deltafinance'
    # Storing the session info
    session = requests.Session()
    response = session.get(home_page_url)
    # Parsing the response using Beautiful Soup
    soup = BeautifulSoup(response.content, 'html.parser')
    # Storing 3 ASP web-page specific form parameters to use to login
    viewstate = soup.select('input[name=__VIEWSTATE]')[0]['value']
    viewstate_generator = soup.select('input[name=__VIEWSTATEGENERATOR]')[0]['value']
    event_validation = soup.select('input[name=__EVENTVALIDATION]')[0]['value']
    login_form_parameters = dict()
    login_form_parameters['__VIEWSTATE'] = viewstate
    login_form_parameters['__VIEWSTATEGENERATOR'] = viewstate_generator
    login_form_parameters['__EVENTVALIDATION'] = event_validation
    login_form_parameters['tblForm$txtUserName'] = credentials['username']
    login_form_parameters['tblForm$txtPassword'] = credentials['password']
    login_form_parameters['tblForm$btnLogin'] = 'Log In'
    login_form_parameters['tblForm$txtCompanyCode'] = 'deltafinance'
    # Storing the cookies post login
    response = session.post(home_page_url, login_form_parameters)
    cookies = session.cookies.get_dict()
    # Logging in
    response = requests.post(home_page_url, login_form_parameters)
    print("Logged in")
    print("Accessing the accounts page session paramaters to navigate to accounts page")
    accounts_url = 'https://secure.financepro.net/financepro/account/account.aspx'
    # Sending the same session cookies
    response = session.get(accounts_url, cookies=cookies)
    soup = BeautifulSoup(response.content, 'html.parser')
    # Getting the ASP accounts web page session paramters
    viewstate = soup.select('input[name=__VIEWSTATE]')[0]['value']
    viewstate_generator = soup.select('input[name=__VIEWSTATEGENERATOR]')[0]['value']
    event_validation = soup.select('input[name=__EVENTVALIDATION]')[0]['value']
    print("Parameters retrieved")
    # Storing paramters to get account details page
    account_info_params = dict()
    account_info_params['__VIEWSTATE'] = viewstate
    account_info_params['__VIEWSTATEGENERATOR'] = viewstate_generator
    account_info_params['__EVENTVALIDATION'] = event_validation
    account_info_params['fndAccountSearch$hdnName'] = 'hvalue'
    # Account number sent as input here
    account_info_params['fndAccountSearch$txtAccountNumber'] = account_num
    account_info_params['fndAccountSearch$txtAccountStatus'] = '0'
    account_info_params['fndAccountSearch$txtCompanyCheckType'] = 'paid'
    account_info_params['fndAccountSearch$btnFind'] = 'Find Account'
    # POST request to get account and insured details
    print("\nAccessing Account Details Page")
    response = requests.post(accounts_url, account_info_params, cookies=cookies)
    soup = BeautifulSoup(response.content, 'html.parser')
    # All insured information is stored in span tags
    insured_name = soup.find("span", {"id": "lblInsuredName"}).text
    print("\nInsured Details are:\n")
    print("Insured name:")
    print(insured_name)
    insured_information['Name'] = insured_name
    insured_address = soup.find("span", {"id": "lblInsuredAddress"})
    # Converting the
 tag into a new line char and then splitting the
    # address into its components
    insured_address = str(insured_address).replace("
", "\n")
    insured_address = insured_address.replace('', '')
    insured_address = insured_address.replace('', '')
    address_dict = dict()
    insured_address = insured_address.split("\n")
    address_dict['Line 1'] = insured_address[0]
    line2 = insured_address[1].split(" ")
    address_dict['City'] = line2[0][:-1]
    address_dict['State'] = line2[1]
    address_dict['Zip Code'] = line2[2]
    print("Insured address:")
    print("Address Line 1:")
    print(address_dict['Line 1'])
    print("City:")
    print(address_dict['City'])
    print("State:")
    print(address_dict['State'])
    print("Zip Code:")
    print(address_dict['Zip Code'])
    insured_information['Address'] = address_dict
    insured_telephone = soup.find("span", {"id": "lblInsuredPhone"}).text
    print("Insured telephone:")
    print(insured_telephone)
    insured_information['Telephone'] = insured_telephone
    # Writing the insured information into a json file
    file_name = 'Insured_Info_' + account_num + '.json'
    with open(file_name, 'w') as f:
        json.dump(insured_information, f)
    print("\nOutput File Created:")
    print(file_name)
    print("\nLogging off")
    # Log off page called with cookie info
    log_off_url = 'https://secure.financepro.net/financepro/logoff.aspx'
    response = requests.get(log_off_url, cookies=cookies)
    final_url = 'https://www.deltafinanceoftexas.com/'
    response = requests.get(final_url)

# Entry point of code
if __name__ == "__main__":
    main()


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

from django.db import models, migrations
import apps.users.models

class Migration(migrations.Migration):

    dependencies = [
        ('users', '0005_auto_20180802_1614'),
    ]

    operations = [
        migrations.AlterModelManagers(
            name='user',
            managers=[
                ('objects', apps.users.models.MyUserManager()),
            ],
        ),
    ]


from unittest.mock import MagicMock
from uuid import uuid4

import pytest
from pytest import raises

from pydantic import ValidationError

from api.exceptions import InvalidParameterError
from api.schemas.output import (
    ConsultaProcessoOutput,
    ExtractDataOutput,
    ExtractDataSecondInstanceOutput,
    StatusSolicitacaoOutput
)

def test_valida_numero_solicitacao_valid():
    """Testa se a validação do UUID é bem-sucedida com um UUID válido."""
    valid_uuid = str(uuid4())
    ConsultaProcessoOutput(numero_solicitacao=valid_uuid)

@pytest.mark.parametrize("invalid_uuid", ["invalid_uuid_string"])
def test_valida_numero_solicitacao_invalid(invalid_uuid):
    """Testa se a validação do UUID falha com uma string inválida."""
    with MagicMock(side_effect=InvalidParameterError("Mocked error")):
        with raises(InvalidParameterError):
            ConsultaProcessoOutput(numero_solicitacao=invalid_uuid)

def test_valid_extract_data():
    """Testa se o modelo é válido com dados corretos."""
    valid_data = {
        'classe': 'Penal',
        'area': 'Criminal',
        'assunto': 'Roubo',
        'data_distribuicao': 'Sorteio',
        'juiz': 'Dr. João Silva',
        'valor_acao': '5000,00',
        'partes_processo': [],
        'lista_movimentacoes': []
    }
    ExtractDataOutput(**valid_data)

def test_valid_output():
    """Testa um output válido para ExtractDataSecondInstanceOutput."""
    valid_data = {
        "classe": "Recurso Penal",
        "area": "Criminal",
        "assunto": "Furto",
        "partes_processo": [],
        "lista_movimentacoes": []
    }
    output = ExtractDataSecondInstanceOutput(**valid_data)
    assert output.classe == "Recurso Penal"
    assert output.area == "Criminal"

def test_missing_required_field():
    """Testa a ausência de campos obrigatórios para ExtractDataSecondInstanceOutput."""
    invalid_data = {
        "classe": "Recurso Penal",
        "assunto": "Furto",
        "partes_processo": [],
        "lista_movimentacoes": []
    }
    with raises(ValidationError):
        ExtractDataSecondInstanceOutput(**invalid_data)

def test_valid_output_first_instance_only():
    """Testa um output válido para StatusSolicitacaoOutput com apenas dados da primeira instância."""
    valid_data = {
        "numero_processo": "0113546-72.2018.8.02.0001",
        "sigla_tribunal": "TJAL",
        "status": "Na Fila",
        "first_instance": {
            "classe": "Penal",
            "area": "Criminal",
            "assunto": "Roubo",
            "data_distribuicao": "Sorteio",
            "juiz": "Dr. João Silva",
            "partes_processo": [],
            "lista_movimentacoes": []
        }
    }
    output = StatusSolicitacaoOutput(**valid_data)
    assert output.numero_processo == "0113546-72.2018.8.02.0001"
    assert output.first_instance.classe == "Penal"

def test_valid_output_both_instances():
    """Testa um output válido para StatusSolicitacaoOutput com dados de ambas as instâncias."""
    valid_data = {
        "numero_processo": "0113546-72.2018.8.02.0001",
        "sigla_tribunal": "TJAL",
        "status": "Na Fila",
        "first_instance": ExtractDataOutput(
            classe="Penal",
            area="Criminal",
            assunto="Roubo",
            data_distribuicao="Sorteio",
            juiz="Dr. João Silva",
            valor_acao="5000,00",
            partes_processo=[],
            lista_movimentacoes=[]
        ),
        "second_instance": ExtractDataSecondInstanceOutput(
            classe="Recurso Penal",
            area="Criminal",
            assunto="Furto",
            valor_acao="2500,55",
            partes_processo=[],
            lista_movimentacoes=[]
        )
    }

    output = StatusSolicitacaoOutput(**valid_data)

    assert output.numero_processo == "0113546-72.2018.8.02.0001"
    assert output.sigla_tribunal == "TJAL"
    assert output.status == "Na Fila"
    assert isinstance(output.first_instance, ExtractDataOutput)
    assert isinstance(output.second_instance, ExtractDataSecondInstanceOutput)

def test_invalid_output():
    """Testa um output inválido para StatusSolicitacaoOutput."""
    invalid_data = {
        "numero_processo": 123456  # Um número em vez de uma string
    }
    with raises(ValidationError):
        StatusSolicitacaoOutput(**invalid_data)


#coding=utf-8

"""
这是一个关于QQ模拟（QListView的使用）的例子--模型定义！
文章链接：http://www.xdbcb8.com/archives/701.html
"""

import random
import Random_Name
from PyQt5.QtCore import QAbstractListModel, Qt, QModelIndex, QVariant, QSize
from PyQt5.QtGui import QIcon, QFont

class ListModel(QAbstractListModel):
    '''
    自定义模型
    '''
    def __init__(self):
        '''
        一些初始设置
        '''
        super().__init__()
        self.ListItemData = []
        # 存储每个QQ用户的列表

        self.Data_init()

    def data(self, index, role):
        '''
        子类化QAbstractListModel必须要实现的函数，主要作用就是返回index所引用项目的给定role下存储的数据。
        '''
        if index.isValid() or (0 <= index.row() < len(self.ListItemData)):
            if role == Qt.DisplayRole:
                return QVariant(self.ListItemData[index.row()]['name'])
                # 文本形式呈现数据
            elif role == Qt.DecorationRole:
                return QVariant(QIcon(self.ListItemData[index.row()]['iconPath']))
                # 以图标形式呈现装饰数据
            elif role == Qt.SizeHintRole:
                return QVariant(QSize(70, 80))
                # 视图项目大小
            elif role == Qt.TextAlignmentRole:
                return QVariant(int(Qt.AlignHCenter|Qt.AlignVCenter))
                # 文本对齐方式
            elif role == Qt.FontRole:
                font = QFont()
                font.setPixelSize(20)
                return QVariant(font)
                # 字体设置
        return QVariant()
        # 非上述情况，返回为空，记住这里是QVariant()

    def rowCount(self, parent = QModelIndex()):
        '''
        返回行数，在这里就是数据列表的大小。
        '''
        return len(self.ListItemData)

    def Data_init(self):
        '''
        数据初始化
        '''
        randomnum = random.sample(range(26), 10)
        # 从0-25个数字中随机的抽取10个不重复的数字组成一个列表
        for i in randomnum:
            randname = Random_Name.getname()
            ItemData = {'name':'', 'iconPath':''}
            ItemData['name'] = randname
            ItemData['iconPath'] = "./res/"+ str(i) + ".jpg"
            # 遍历这个列表randomnum，其中联系人的姓名我是随机生成的，随机的生成图标的路径；把姓名和图标路径添加到字典当中。

            self.ListItemData.append(ItemData)
            # append到数据列表里面。

    def addItem(self, itemData):
        '''
        新增的操作实现
        '''
        if itemData:
            self.beginInsertRows(QModelIndex(), len(self.ListItemData), len(self.ListItemData) + 1)
            self.ListItemData.append(itemData)
            self.endInsertRows()
            # 结束行插入操作

    def deleteItem(self, index):
        '''
        指定索引的数据从数据列表中删除
        '''
        del self.ListItemData[index]

    def getItem(self, index):
        '''
        获得相应的项目数据
        '''
        if index > -1 and index < len(self.ListItemData):
            return self.ListItemData[index]


#!/usr/bin/python3

import multiprocessing
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
import math
import time

def points():
    import sys
    for line in open("input.txt"):
        line = line.replace("position=", "").replace("velocity=", "").replace(' ', '').replace('<', '').strip()
        a = line.split('>')
        yield [tuple([int(y) for y in x.split(',')]) for x in a[0:2]]

def produce_state(PP, PV, T):
    ret = []
    for p in zip(PP, PV):
        v = p[1]
        p = p[0]
        ret.append([p[0] + v[0] * T, p[1] + v[1] * T])

    return ret

def render(PP, T):
    plt.scatter([p[0] for p in PP], [p[1] for p in PP])
    plt.show(block=True)

def BB(PP):
    sx = 0
    sy = 0
    ex = 0
    ey = 0

    for p in PP:
        if p[0] < sx: sx = p[0]
        if p[0] > ex: ex = p[0]
        if p[1] < sy: sy = p[1]
        if p[1] > ey: ey = p[1]

    return sx, sy, ex, ey

P = [point for point in points()]

# calc BB
PP = [point[0] for point in P]
PV = [point[1] for point in P]

T = 0

Emin = 0
Tmin = -1

step = 1
def update(T):
    S = produce_state(PP, PV, T)
    sx, sy, ex, ey = BB(S)
    #render(S, T)
    return (math.sqrt((sx - ex) ** 2 + (sy - ey) ** 2))

fuck = 256000
scale = 100
start = int(10518.9 * scale)
end = int((1 + 10519) * scale)
#for T in range(start, end, 1):
for i in range(1):
    T = 10519
    #T = T / scale
    S = produce_state(PP, PV, T)
    sx, sy, ex, ey = BB(S)
    cunt = (math.sqrt((sx - ex) ** 2 + (sy - ey) ** 2))
    print(T)
    #if fuck< cunt:
    #    print(T)
    #    render(S, T)
    #    break
    render(S, T)
    fuck = cunt


import os
from datetime import datetime

dir = os.path.dirname(os.getcwd()+'/users/')
print(dir)
try:
    os.stat(dir)
except:
    os.mkdir(dir)
    print('make directory')

all_users = {}
for f in os.listdir(os.getcwd()):
    if f.endswith(".csv"):
        split = str(f).split('_')
        code = split[1]
        print('[{}] Processing {} - {}'.format(datetime.now(), code, f))
        users = {}
        with open(f, 'r', encoding='utf8') as fr:
            for line in fr:
                split = line.split(',')
                if len(split) < 2:
                    continue
                try:
                    uid = int(split[1])
                except:
                    continue
                # This file users
                f_uid = users.get(uid)
                if f_uid is None:
                    users[uid] = 1
                else:
                    users[uid] = f_uid + 1
                # All users
                f_all = all_users .get(uid)
                if f_all is None:
                    all_users[uid] = 1
                else:
                    all_users[uid] = f_all + 1
            with open(dir + '/' + str(code) + '.csv', 'w') as fw:
                for uid, f_uid in users.items():
                    fw.write('{},{}\n'.format(uid,f_uid))
        users.clear()

print('[{}] Processing all files'.format(datetime.now()))
with open(dir + '/#ALL.csv', 'w') as fw_all:
    for uid, f_all in all_users.items():
        fw_all.write('{},{}\n'.format(uid,f_all))

print('[{}] Program finished'.format(datetime.now()))


from selenium import webdriver
from selenium.webdriver.common.by import By
import time

class SwitchToWindow():

    def test(self):
        baseUrl = "https://letskodeit.teachable.com/pages/practice"
        driver = webdriver.Firefox()
        driver.maximize_window()
        driver.get(baseUrl)

        # Find parent handle -> Main Window
        parentHandle = driver.current_window_handle
        print("Parent Handle: " + parentHandle)

        # Find open window button and click it
        driver.find_element(By.ID, "openwindow").click()
        time.sleep(2)

        # Find all handles, there should two handles after clicking open window button
        handles = driver.window_handles

        # Switch to window and search course
        for handle in handles:
            print("Handle: " + handle)

ff = SwitchToWindow()
ff.test()

CODES/S23 - Selenium WebDriver -_ Switch Window And IFrames/1_switch-to-window.py


from django.shortcuts import render, HttpResponse
from django.conf import settings

from rest_framework.decorators import api_view
from rest_framework.response import Response

import random

from .models import Planet
from .serializers import PlanetSerializer

from api.serializers import GenericSerializer
from api.views import BaseRandomView, BaseIdView
from api.utils import validate_request, set_options_response

MODEL = Planet

@api_view(['GET', 'OPTIONS'])
def index(request):
    if request.method == 'OPTIONS':
        return set_options_response()

    result = validate_request(request)
    if 'error' in result:
        return Response({"error": result['error']}, status=result['status'], headers=settings.CORS_HEADERS)

    name = request.GET.get('name', None)
    affiliation = request.GET.get('affiliation', None)
    region = request.GET.get('region', None)
    page = request.GET.get('page', 0)

    planets_set = Planet.objects.all().order_by('id')

    if name:
        planets_set = planets_set.filter(name__icontains=name)

    if affiliation:
        planets_set = planets_set.filter(info__affiliation__icontains=affiliation)

    if region:
        planets_set = planets_set.filter(info__region__icontains=region)

    if page:
        try:
            page = int(page)
        except:
            page = 1;
        start = settings.RESOURCE_LIMIT*(page-1)
        end = settings.RESOURCE_LIMIT*(page-1)+settings.RESOURCE_LIMIT
        planets_set = planets_set[start:end]
    else:
        planets_set = planets_set[0:settings.RESOURCE_LIMIT]

    serializer = PlanetSerializer(planets_set, many=True)

    # If nothing matches queries
    if not serializer.data:
        return Response({"error": settings.MSG_404}, status=404, headers=settings.CORS_HEADERS)

    return Response(serializer.data, headers=settings.CORS_HEADERS)

class RandomPlanetView(BaseRandomView):
    model = MODEL

class PlanetIdView(BaseIdView):
    model = MODEL


from django.test import TestCase, RequestFactory

from djlotrek.request_utils import get_host_url

class RequestUtilsTestCase(TestCase):
    def test_get_host_url(self):
        """
        get_host_url function retrieve request object and
        return host url when request object is not None
        """
        request_factory = RequestFactory()
        request = request_factory.get("/path")
        request.META["HTTP_HOST"] = "localhost"
        host_url = get_host_url(request)
        self.assertEqual(host_url, "http://localhost")

    def test_get_host_url_no_request(self):
        """
        get_host_url function retrieve request object and
        return None when request object is None
        """
        host_url = get_host_url(None)
        self.assertEqual(host_url, None)