# 코드업 100제
# 6079번-[기초-종합]언제까지더해야할까?(py).py

'''

1, 2, 3 ... 을 계속 더해 나갈 때,
그 합이 입력한 정수(0 ~ 1000)보다 같거나 작을 때까지만
계속 더하는 프로그램을 작성해보자.

즉, 1부터 n까지 정수를 계속 더해 나간다고 할 때,
어디까지 더해야 입력한 수보다 같거나 커지는 지를 알아보고자하는 문제이다.

*****예시
-

*****입력
정수 1개가 입력된다.

*****출력
1, 2, 3, 4, 5 ... 를 순서대로 계속 더해 합을 만들어가다가,
입력된 정수와 같거나 커졌을 때, 마지막에 더한 정수를 출력한다.

*****입력 예시
55

*****출력 예시
10

'''
# 풀이법

value = int(input())

sum = 0

for i in range(1, value):
    sum += i

    if sum >= value:
        print(i)
        break

'''

***** 참고

-

'''

codeup(코드업)/기초100제/파이썬/012_기초-종합/6079번-[기초-종합]언제까지더해야할까(py).py


import matplotlib.pylab as plt
import numpy as np
import scipy
from scipy import interpolate
import sys
import os

from linetools.isgm.abscomponent import AbsComponent
import json
from pyigm.guis import igmguesses
from linetools.spectra.io import readspec

import spectrum_analysis_tools as spa

# inwave = REST wavelength for rest-frame EW; observed wavelength for observed EW
# inspec = flux
# inerror = error
# vrange = [vmin, vmax] velocity range for integration
# w0 = rest wavelength of transition
# f0 = fval of transition
# limit = how many sigma limit do we want, for limits?
# norm = the flux value to normalise the spectrum by
# nstapix -- the number of pixels that need to be below the saturation limit or error before we trigger the saturation flag.
#
# Keywords
# ctnorm -- normalise the spectra by the median flux in +/-600km/s (excluding the line region)
#
# OUTPUTS
# EW -- 2-element array of eqw and eqwerror.
# colm -- 2-element array of linear column density and error
#
# Note -- Return 2-element arrays with error included, since 2 keywords eqw and eqwerr are
#         ambiguous to IDL and so not allowed.
#inspec
# REVISION HISTORY
#
def deriv(x):
#	return scipy.misc.derivative(x)
	return np.diff(np.append(x, x[-1]))

def ct_ew2n(ew, wrest, fval):
	# assume optically thin case, and compute column density from EW. See, e.g. Savage & Sembach (1996) eqn 3
	# EW in mA
	# wrest in A
	# fval is from morton
#return 1.13d17*ew/(fval * wrest**2)
	return 1.13e17*ew/(fval * wrest**2)

def eqwrange(ion, wave, spec, error, vrange, w0, f0, \
              limit = 1., norm = 1.0, ctnorm = 1, nsatpix = 3, \
              silent = 0, plots = 0, sig_limit = 3, sat_limit = 0.1, \
	      plot_dir = '.'):

	### DEFAULT SETTING FOR FLAG. 1 = ok.
	flag = 1

	if norm:
  		spec = spec / norm
  		error = error / norm

	if ctnorm:
		#grab the median flux value in the +/-600km/s range, outside the line area
		vel = (wave-w0)/w0*2.9979e5
		qq = ((vel > -1200) & (vel < vrange[0])) | ((vel > vrange[1]) & (vel < 1200))
		med = np.median(spec[qq])
		spec  /= med
		error /= med
	else:
		print('warning - assumes data and errors are already continuum normalized')

	# convert wavelengths to velocty
	vv = (wave-w0) / w0 * 2.9979e5
	# find points in range

	# get rid of infinity values down the line
	# note: results sensitive to the value we set spec to
	# sort of converges but not really
	spec[spec <= 0] = 0.01
	iv = (vv > vrange[0]) & (vv < vrange[1])
	if len(vv[iv]) == 0:
		# Something is wrong. We were given a spectrum with no points in the velocity range. Pull out!!
		print("Spectrum has no points in the velocity range")
		ew = [-999.9, -999.9]
		colm = [-999.9, -999.9]
		return

	sat = (spec[iv] < sat_limit) | (spec[iv] < error[iv])
	if len(spec[iv][sat]) > 0:
		spec[iv][sat] = error[iv][sat]
		if len(spec[iv][sat]) > nsatpix:
			flag += 8  #If there are more pixels than we set, then trip the saturated flag

	# now that we've calculated the saturation flag, we can filter spec[iv]
        # down even more to get more accurate column density estimates
#	iv =  iv & (spec > sat_limit)

	# plotting the flux vs velocities for all pixels and those used in fit
	if plots:
		fig, ax = plt.subplots(nrows = 1, ncols = 2, figsize=(10, 5), sharex = False, sharey = False)
		ax[0].errorbar(vv, spec, yerr = error)
		ax[0].errorbar(vv[iv], spec[iv], yerr = error[iv])
		ax[0].axhline(y = 0, color = 'red', linestyle = 'dashed')
		ax[0].set_xlim(vrange[0], vrange[1])
		ax[0].set_ylim(-0.1, 1.5)
		ax[0].set_xlabel('Relative Velocity (km/s)')
		ax[0].set_ylabel('Normalized Flux')

	dwave = deriv(wave[iv])
	d_eqw = dwave * (1. - spec[iv])  # * 1000.  # why 1000?

	eqw = np.sum(d_eqw)

 	# dtermine centroid of equivalent width
	f_eqw = np.cumsum(d_eqw) / np.max(np.cumsum(d_eqw)) #cumulative eqw fraction
	# interpolate for velocity at which fraction of eqw = 0.5
	# we'll take this as the velocity centroid
	# trying something new:
	vcent = np.interp(0.5, f_eqw, vv[iv])
	#print(vcent)
#	plt.plot(vv[iv], f_eqw)
#	plt.show()
	# now ready to calculate vwidth
	# Got rid of * 1000
#        eqwerr = np.sqrt(np.sum((deriv(wave[iv])*1000)**2 \
 #                                               * (error[iv])**2 ))

#	eqwerr = np.sqrt(np.sum((deriv(wave[iv])**2  * error[iv])**2 )
	eqwerr = np.sqrt(np.sum((deriv(wave[iv])**2  * error[iv])**2) )

	EW = [eqw, eqwerr]

  	# Upper limit flag
	if eqw < sig_limit*eqwerr:
  		flag += 4

  	# Convert to strings
  	#  eqw = string(eqw, format='(F8.1)')
  	#eqwerr = string(eqwerr, format='(F8.1)')

  	#now convert data points to tau0 -> column density using input line data
	tau = -1. * np.log(spec)
	f_tau = np.cumsum(tau[iv]) / max(np.cumsum(tau[iv])) # cumulative opticaldepth fraction
	tau_cent = np.interp(0.5, f_tau, vv[iv])

	# calculate velocity width as moment of tau dist
	top = np.sum((vv[iv] - tau_cent)**2 * tau[iv] * deriv(vv[iv])) # integral of (v-vbar)^2 * tau_a(v) * dv
	bottom = np.sum(tau[iv] * deriv(vv[iv])) # integral of tau_a(v) * dv
	sigma_v = (top / bottom)**0.5 # this is sigma as defined on pg 692 of heckman et al. 2002
	velcent = tau_cent
	velwidth = np.sqrt(2.) * sigma_v

	nv = tau / 2.654e-15 / f0 / w0 # in units cm^-2 / (km s^-1), SS91
                                        # column density per unit velocity
	n = nv * deriv(vv) # column density per bin obtained by multiplying differential Nv by bin width
#	when spec[iv] == 0, tau and therefore n and col = inf
# fixed this by specifying the [iv] mask to have spec > 0
	for ii in range(len(n[iv])):
		if np.isinf(n[iv][ii]):
			print(nv[iv][ii], spec[iv][ii])

	tauerr = error / spec
	nerr = tauerr / 2.654e-15 / f0 / w0 * deriv(vv)
	if plots:
		ax[1].errorbar(vv, n,  yerr = nerr)#, drawstyle = 'steps-mid-')
		ax[1].errorbar(vv[iv], n[iv], yerr = nerr[iv])#, drawstyle = 'steps-mid-')
		ax[1].set_yscale('log')
		ax[1].set_ylim(1e10, 1e15)
		ax[1].set_xlim(-250, 250)
		ax[1].set_xlabel('Relative Velocity (km/s)')
		ax[1].set_ylabel('Column Density (cm$^{-2}$)')
		fig.tight_layout()
		plt.savefig('%s/%s_%.1f_flag_%i.png'%(plot_dir, ion, w0, flag))

	col = np.sum(n[iv])
	colerr = np.sqrt(np.sum(nerr[iv]**2))
	colm = [col, colerr]

	if not silent:
		if not limit:
			print('Direct N = %e +/- %e'%(col, colerr))
			print('Direct N = %e, +/- %e'%(np.log10(col), np.alog10(col+colerr) - alog10(col)))
			print('W_lambda = %e +/- %e mA over %s km/s'%(eqw, eqwerr, str(vrange)))
		else:
			print('Limit N = (%e) +/- %e'%(col, colerr*limit))
			print('Limit N = (%e) +/- %e'%(np.log10(np.abs(col)), np.log10(colerr * limit)))
			print('W_lambda = (%e) +/- %e mA over %s km/s'%(eqw, eqwerr*limit, str(vrange)))
	return eqw, eqwerr, np.log10(np.abs(col)), np.log10(col+colerr)-np.log10(col), flag, velcent, velwidth

def find_ion_limits(ion, fn, restwave = 0, redshift = 0, \
			    vrange = (-200, 200), silent = 0, plots = 0, plot_dir = '.', sat_limit = 0.1):
    ion_names = np.loadtxt('../../dla.lst', unpack=True, skiprows = 1, usecols=(1), dtype = 'str')
    ion_wls, ion_fs = np.loadtxt('../../dla.lst', unpack=True, skiprows = 1,usecols=(0, 3))
    ion_wls_int = ion_wls.astype(int)

    wl, flux, ferr = spa.load_spec_from_fits(fn)

    eqw_list = []
    eqwerr_list = []
    col_list = []
    colerr_list = []
    flag_list = []
    velcent_list = []
    velwidth_list = []

    ion = ion.replace(" ", "")
    if restwave == 0:
	    w0 = spa.restwave(ion, z = 0) # REST wavelength for rest-frame EW; observed wavelength for observed EW
	    w_obs = spa.restwave(ion, z = redshift)
    else:
	    w0 = restwave
	    w_obs = restwave * (redshift + 1)
    ion_mask  = (ion_names == ion)
    wl_mask =  (ion_wls_int[ion_mask] == int(w0))
    f0 = ion_fs[ion_mask][wl_mask][0]

    eqw, eqwerr, col, colerr, flag, velcent, velwidth = eqwrange(ion, wl, flux, ferr, vrange, w_obs, f0, silent=silent, \
									   plots = plots, plot_dir = plot_dir)
    eqw_list.append(eqw)
    eqwerr_list.append(eqwerr)
    col_list.append(col)
    colerr_list.append(colerr)
    flag_list.append(flag)
    velcent_list.append(velcent)
    velwidth_list.append(velwidth)

    return eqw_list, eqwerr_list, col_list, colerr_list, flag_list, velcent_list, velwidth_list

def json_eqw(json_file, fits_file, outfile, overwrite = False):
	ion_name = []; restwave = []; col = []; col_err = []; flag = []; zcomp = [];
	if not os.path.isfile(json_file):
		return np.array(ion_name), np.array(restwave), np.array(col), \
		    np.array(col_err), np.array(flag), np.array(zcomp)

	if not os.path.isfile(outfile) or overwrite == True:
		out =  open(outfile, 'w')
		out.write('#ion_name, restwave, logN, logN_err, flag_N, zcomp\n')

		with open(json_file) as data_file:
			igmg_dict = json.load(data_file)

		comp_list = []
		for ii, key in enumerate(igmg_dict['cmps'].keys()):
			comp = AbsComponent.from_dict(igmg_dict['cmps'][key], chk_sep=False, chk_data=False, chk_vel=False)
			comp_list += [comp]

		spec = readspec(fits_file)
		for i, cc in enumerate(comp_list):
			for j, al in enumerate(cc._abslines):
				al.analy['spec']=spec
				al.measure_ew()
				al.measure_aodm()

				ion_name.append(al.ion_name)
				restwave.append(al.wrest.value)
				col.append(al.attrib['logN'])
				sig_logN = al.attrib['sig_logN']
				if np.isnan(sig_logN):
					sig_logN = 0.0
				col_err.append(sig_logN)
				flag.append(al.attrib['flag_N'])
				zcomp.append(al.z)

				out.write('%s\t%f\t%e\t%e\t%d\t%0.6f\n'%(al.ion_name, al.wrest.value, \
						   al.attrib['logN'], sig_logN, al.attrib['flag_N'], al.z))
		out.close()
	if os.path.isfile(outfile) and len(open(outfile).readlines()) > 1:
		ion_name = np.loadtxt(outfile, unpack = True, skiprows = 1, usecols = 0, dtype = 'str')
		restwave, col, col_err, flag, zcomp = \
		    np.loadtxt(outfile, unpack = True, skiprows = 1, usecols =  (1, 2, 3, 4, 5))

	return np.array(ion_name), np.array(restwave), np.array(col), np.array(col_err), np.array(flag), np.array(zcomp)

def load_veeper_fit(veeper_fn):
	if os.path.isfile(veeper_fn):
		restwaves, cols, sigcols, bvals, sigbvals, vels, sigvels, zcomps = \
		    np.loadtxt(veeper_fn, unpack=True, skiprows = 1, usecols = (1,3,4,5,6,7,8, 18), delimiter = '|')
		veeper_ions, labels = np.loadtxt(veeper_fn, unpack=True, skiprows = 1, \
						 usecols = (19, 21), dtype = 'str', delimiter = '|')

		if type(veeper_ions) == np.str_:
			veeper_ions = np.array([veeper_ions]); restwaves =  np.array([restwaves]);
			cols =  np.array([cols]); sigcols =  np.array([sigcols]);
			bvals =  np.array([bvals]); sigbvals =  np.array([sigbvals]);
			vels =  np.array([vels]); sigvels =  np.array([sigvels]);
			labels =  np.array([labels]); zcomps = np.array([zcomps]);

		for i in range(len(veeper_ions)):
			temp    = veeper_ions[i]
			veeper_ions[i] = temp.replace(" ", "")

		if sigcols.size > 1:
			mask = sigcols > 0
			veeper_ions = veeper_ions[mask]
			restwaves = restwaves[mask]
			cols = cols[mask]
			sigcols = sigcols[mask]
			bvals = bvals[mask]
			sigbvals = sigbvals[mask]
			vels = vels[mask]
			sigvels = sigvels[mask]
			labels = labels[mask]
			zcomps = zcomps[mask]
	else:
                restwaves = []; cols = []; sigcols = []; bvals = []; sigbvals = [];
                vels      = []; sigvels = []; flags = []; veeper_ions = []; labels = []; zcomps = [];
	return veeper_ions, restwaves, cols, sigcols, bvals, sigbvals, vels, sigvels, labels, zcomps


# 第六章 字典
# 字典用放在方括号中的一系列键值对表示
alien_0 = {'color': 'green', 'point': 5}
print(alien_0['color'])  # green
print(alien_0['point'])  # 5

alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)  # {'color': 'green', 'point': 5, 'x_position': 0, 'y_position': 25}

# 删除键-值对
alien_0 = {'color': 'green', 'point': 5}
del alien_0['point']
print(alien_0)  # {'color': 'green'}
# 遍历字典--dic.items()
favorite_language = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
}
for name, language in favorite_language.items():
    print(name.title() + " -> " + language)
# Jen -> python
# Sarah -> c
# Edward -> ruby
# Phil -> python

# items()方法返回一个键-值对列表(list)
print(favorite_language.items())  # dict_items([('jen', 'python'), ('sarah', 'c'), ('edward', 'ruby'), ('phil', 'python')])
# 遍历字典中所有的键--dic.keys()
for i in favorite_language:  # 遍历字典时，会默认遍历所有的键
    print(i, end=' ')  # jen sarah edward phil
print()
for name in favorite_language.keys():
    print(name, end=' ')  # jen sarah edward phil
print()

if 'phil' not in favorite_language.keys():
    print('phil no')
else:
    print('phil yes')  # phil yes
# 按顺序遍历字典中的所有键
for name in sorted(favorite_language.keys()):
    print(name.title(), end=' ')  # Edward Jen Phil Sarah
print()
# 遍历字典中的所有值--dic.values()
favorite_language = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
}
for language in favorite_language.values():
    print(language, end=' ')  # python c ruby python
print()
# 对列表使用set()，可剔除重复项
for language in set(favorite_language.values()):
    print(language.title(), end=' ')  # Ruby C Python (与原list的相对位置可能会发生改变，因为set是无序的)
print()

language = list(language for language in favorite_language.values())  # python c ruby python
language2 = list(set(language))  # C Ruby Python
language2.sort(key=language.index)  # 按原序重新排列
for i in language2:
    print(i, end=' ')  # python c ruby
print()
# 字典列表
alien_0 = {'color': 'green', 'point': 5}
alien_1 = {'color': 'yellow', 'point': 10}
alien_2 = {'color': 'red', 'point': 15}
aliens = [alien_0, alien_1, alien_2]
for alien in aliens:
    print(alien)
# {'color': 'green', 'point': 5}
# {'color': 'yellow', 'point': 10}
# {'color': 'red', 'point': 15}

# 在字典中存储列表
favorite_language = {
    'jen': ['python', 'ruby'],
    'sarah': 'c',
    'phil': ['python', 'haskell'],
}
for name, languages in favorite_language.items():
    if len(languages) == 1:
        print(name.title() + "'s favorite language is:\n\t" + languages)
    else:
        print(name.title() + "'s favorite languages are:")
        for i in languages:
            print("\t" + i)
# Jen's favorite languages are:
# 	python
# 	ruby
# Sarah's favorite language is:
# 	c
# Phil's favorite languages are:
# 	python
# 	haskell

# 在字典中存储字典
users = {
    'aeinstein': {
        'first': 'albert',
        'last': 'einstein',
        'location': 'princeton',
        },
    'mcurie': {
        'first': 'marie',
        'last': 'curie',
        'location': 'paris',
        },
}
for username, user_info in users.items():
    print("Username: " + username)
    print("\tFull Name: " + user_info['first'] + " " + user_info['last'])
    print("\tLocation: " + user_info['location'] + "\n")
# Username: aeinstein
# 	Full Name: albert einstein
# 	Location: princeton
#
# Username: mcurie
# 	Full Name: marie curie
# 	Location: paris
#


#!/usr/bin/python3

def Tri(n):
    p = int(n*(n+1)/2.);
    return p

def main():
    Starter = 20;
    Value  = 0;
    NOD = 200; #//Number of divisiors required
    while(True):
        Current = Tri(Starter)
        #print('Current tri is ',Current)
        Count = 0
        for p in range(1,Current+1):
            if Current % p == 0:
                Count += 1
                if(Count>NOD):
                    print(Starter, "th triangle number ",Current)
                    return 0;

        Starter += 1;

y = main()


from functools import partial
import os
from argparse import ArgumentParser
import numpy as np
import open3d as o3d

from utils.dyn_feasibility_check import check_dyn_feasible, check_dyn_feasible_parallel, simple_check_dyn_feasible
from utils.kin_feasibility_check import check_kin_feasible, check_kin_feasible_parallel
import model.manipulation_obj_creator as creator

DIFFUSION_RADIUS = 0.03
NUM_STEPS = 10
NUM_NEIGHBOR_SUPPORT=1

def load_seeds(seed_folder, use_scale=False):
    """
    seeds: grasp seed have only finger tip pose, no normal vector
    """
    files = os.listdir(f"data/{seed_folder}/pointclouds")
    files.sort(key=lambda x: int(x[5:7]))
    pcds = []
    for file in files:
        pcd = o3d.io.read_point_cloud(f"data/{seed_folder}/pointclouds/{file}")
        assert(pcd.has_normals())
        pcds.append(pcd)

    files = os.listdir(f"data/{seed_folder}/grasps")
    files.sort(key=lambda x: int(x[5:7]))
    all_grasps = []
    for file in files:
        grasps = np.load(f"data/{seed_folder}/grasps/{file}")
        if len(grasps.shape) == 2:
            grasps = grasps.reshape(1,grasps.shape[0],grasps.shape[1])
        all_grasps.append(grasps[:,:,:3])
    if use_scale:
        files = os.listdir(f"data/{seed_folder}/scales")
        files.sort(key=lambda x: int(x[5:7]))
        all_scales = []
        for file in files:
            scale = np.load(f"data/{seed_folder}/scales/{file}")
            all_scales.append(scale)
        return pcds, all_grasps, all_scales
    else:
        return pcds, all_grasps

def randomize_sample_trajectory(points, normals, kd_tree, seed, depth=8):
    """
    seed: only finger tip pose no normal vector
    """
    init_grasp = np.zeros((seed.shape[0], seed.shape[1] + 3))
    for i, point in enumerate(seed):
        init_idx = kd_tree.search_knn_vector_3d(point, 1)[1]
        init_grasp[i,:3] = points[init_idx]
        init_grasp[i,3:] = normals[init_idx]

    grasps_traj = [init_grasp]
    for i in range(depth):
        new_grasp = np.zeros_like(grasps_traj[-1])

        for i, point in enumerate(grasps_traj[-1]):
            indices = np.array(list(kd_tree.search_radius_vector_3d(point[:3], DIFFUSION_RADIUS)[1]))
            choice = np.random.choice(indices,1)
            new_grasp[i,:3] = points[choice]
            new_grasp[i,3:] = normals[choice]
        grasps_traj.append(new_grasp)
    return grasps_traj

def project_to_pcd(points, normals, grasp, kd_tree, num_neighbors):
    """
    grasp: finger tip pose and normal vector
    """
    new_poses=[]
    for i in range(len(grasp)):
        idx = kd_tree.search_knn_vector_3d(grasp[i,:3],num_neighbors)[1]
        new_poses.append(np.hstack([points[idx], normals[idx]]))
    if len(grasp) < 4:
        new_poses.append(np.array([[100, 100, 100, 0, 0, 0]] * num_neighbors)) # Ring finger
    return np.vstack(new_poses)

def check_traj_feasibility(grasps_traj, points, normals, kd_tree, num_neighbors=3, hand_model="allegro", env="laptop",scale=np.array([1,1,1])):
    """
    Assume grasps_traj have finger tip pose and normal vector
    For any small box object, use env == "laptop"
    """
    feasible_grasps = []
    infeasible_grasps = []
    for grasp in grasps_traj:
        tip_poses = project_to_pcd(points, normals, grasp, kd_tree, num_neighbors)
        flag = simple_check_dyn_feasible(tip_poses[:,:3], tip_poses[:,3:])
        print("Dyn:",flag)
        obj_creator = partial(creator.object_creators[env], scale=scale)
        if flag == False:
            infeasible_grasps.append(grasp)
        elif not check_kin_feasible_parallel(np.vstack([grasp[:,:3], np.array([100,100,100])]),
                                             np.vstack([grasp[:,3:], np.array([0,0,0])]), hand_model=hand_model,object_creator=obj_creator)[0]:
            infeasible_grasps.append(grasp)
        else:
            feasible_grasps.append(grasp)
    return feasible_grasps, infeasible_grasps

def main(args):
    if args.has_scale:
        pcds, all_grasps, all_scales = load_seeds(args.seed_folder, args.has_scale)
    else:
        pcds, all_grasps = load_seeds(args.seed_folder, args.has_scale)
    print(f"Seed Loaded: {len(pcds)} point clouds")
    for i, (pcd, grasps) in enumerate(zip(pcds, all_grasps)):
        if i < args.start_idx  or (args.use_idx is not None and str(i) not in args.use_idx):
            continue
        print(f"Seed {i} Loaded: {len(grasps)} grasps")
        kd_tree = o3d.geometry.KDTreeFlann(pcd)
        points = np.asarray(pcd.points)
        normals = np.asarray(pcd.normals)
        feasible_grasps_this_pose = []
        infeasible_grasps_this_pose = []
        for seed_grasp in grasps:
            grasps_traj = randomize_sample_trajectory(points, normals, kd_tree, seed_grasp, depth=NUM_STEPS)
            feasible_grasps, infeasible_grasps = check_traj_feasibility(grasps_traj, points, normals, kd_tree, num_neighbors=NUM_NEIGHBOR_SUPPORT, hand_model=args.hand_model,
                                                                        env=args.env, scale=all_scales[i] if args.has_scale else np.array([1,1,1]))
            feasible_grasps_this_pose += feasible_grasps
            infeasible_grasps_this_pose += infeasible_grasps
        print(f"Finish {i} pcd, total feasible grasps: {len(feasible_grasps_this_pose)} infeasible grasps {len(infeasible_grasps_this_pose)}")
        if len(feasible_grasps_this_pose) != 0:
            np.save(f"data/{args.seed_folder}/processed_data/pose_{i:02}.npy", np.asarray(feasible_grasps_this_pose))
        np.save(f"data/{args.seed_folder}/processed_data/infeasible_pose_{i:02}.npy", np.asarray(infeasible_grasps_this_pose))
    print("Processed data has been saved")

if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument("--exp_name", type=str, default="default")
    parser.add_argument("--hand_model", type=str, default="shadow")
    parser.add_argument("--seed_folder", type=str, default="seeds")
    parser.add_argument("--env",type=str,default="laptop")
    parser.add_argument("--has_scale",action="store_true", default=False)
    parser.add_argument("--start_idx", type=int, default=0)
    parser.add_argument('--use_idx','--list', action='append')
    args = parser.parse_args()
    main(args)


# !apt-get install -y xvfb python-opengl > /dev/null 2>&1
# !pip install gym pyvirtualdisplay > /dev/null 2>&1

import gym
import numpy as np
import random
import math
from tqdm import tqdm

# We can also use CartPole-v1 but it's slower
env_name = "CartPole-v0"

# Information from OpenAi Gym's repository, velocity and tip_velocity should be
# -inf to inf but for simplification I just used the values below
position_limit = [-2.4, 2.4]
velocity_limit = [-2.5, 2.5]
angle_limit = [-0.209, 0.209]
tip_velocity_limit = [-1.0, 1.0]

# Paramters representing how many intervals we discretize the state
n_p, n_v, n_a, n_t = z,40,40,40

p = np.linspace(position_limit[0], position_limit[1], n_p)
v = np.linspace(velocity_limit[0], velocity_limit[1], n_v)
a = np.linspace(angle_limit[0], angle_limit[1], n_a)
t = np.linspace(tip_velocity_limit[0], tip_velocity_limit[1], n_t)

def state_to_string(s, p = p, v = v, a = a, t = t):
  position = np.searchsorted(p, s[0], "right")
  velocity = np.searchsorted(v, s[1], "right")
  angle = np.searchsorted(a, s[2], "right")
  tip_velocity = np.searchsorted(t, s[3], "right")
  return f'{position}_{velocity}_{angle}_{tip_velocity}'

def generate_target_policy(Q, states):
  # Target policy based on state action values
  return {s: max(Q[s], key=Q[s].get) for s in states}

def generate_behavior_policy(pi, actions, states, epsilon = 0.5):
  # e-soft policy that makes sure each action is taken at least occasionally
  # This is the behavior policy, it's generated from existing target policy
  b = {s: {a: 1 - epsilon + epsilon/len(actions) if pi[s] == a else
           epsilon/len(actions) for a in actions} for s in states}
  return b

# Generate episodes
def generate_episode(b, actions):
  # Reset environment
  s = env.reset()
  s = state_to_string(s)
  E = []
  for i in range(200):
    # Choose action based on behavior policy
    action = np.random.RandomState(0).choice(actions, p=list(b[s].values()))
    next_s, next_r, done, info = env.step(action)
    # Add state, action and reward to episodes
    E.append({"state": s, "action": action, "next_reward": next_r})
    if done:
      break
    s = state_to_string(next_s)
  return E

# Test target policy
def test(target_policy):
  env = gym.make(env_name)
  s = env.reset()
  s = state_to_string(s)
  for i in range(201):
    action = target_policy[s]
    obs, reward, done, info = env.step(action)
    s = state_to_string(obs)
    if done:
      break
  env.close()
  return i

env = gym.make(env_name)

# Initialize all states
states = [f'{p}_{v}_{a}_{t}' for p in range(n_p+1) for v in range(n_v+1) for a in
          range(n_a+1) for t in range(n_t+1)]

# Possible actions
actions = list(range(env.action_space.n))

# Initialize Q and C, for all s in S, a in A(s)
Q = {s:{a: random.uniform(0,1) for a in actions} for s in states}
C = {s:{a: 0 for a in actions} for s in states}

target_policy = generate_target_policy(Q, states)

# Some hyper parameters
n_iterations = 100000
decay = 0.95

for k in tqdm(range(n_iterations), position=0, leave=True):
  # We can also use dynamic epsilon, but in this case I'm using a constant
  epsilon = 0.2
  b = generate_behavior_policy(target_policy, actions, states, epsilon = epsilon)
  E = generate_episode(b, actions)
  G = 0.0
  W = 1.0
  # Reverse time order
  for t in range(len(E) - 1, 0, -1):
    s, a, r = E[t]['state'], E[t]['action'], E[t]['next_reward']
    G = decay * G + r
    C[s][a] = C[s][a] + W
    Q[s][a] = Q[s][a] + (W/C[s][a]) * (G - Q[s][a])
    target_policy[s] = max(Q[s], key=Q[s].get)
    # print(f'{s}-----{target_policy[s]}')
    if a != target_policy[s]:
      break
    W = W /b[s][a]
env.close()

print(f'test ran {test(target_policy)} iterations')


from graphviz import Digraph as DotGraph
from wordcloud import WordCloud
import matplotlib.pyplot as plt

def showGraph(dot, show=True, file_name='textgraph.gv'):
  dot.render(file_name, view=show)

'''
def gshow0(g, file_name='textgraph.gv', show=True):
  dot = DotGraph()
  for e in g.edges():
    f, t = e
    # w = g[f][t]['weight']
    w = ''
    dot.edge(str(f), str(t), label=str(w))
  dot.render(file_name, view=show)
'''

def show_ranks(rank_dict,file_name="cloud.pdf",show=True) :
  if not show : return
  cloud=WordCloud(width=800,height=400)
  cloud.fit_words(rank_dict)
  f=plt.figure()
  plt.imshow(cloud, interpolation='bilinear')
  plt.axis("off")
  # plt.show()
  f.savefig(file_name,bbox_inches='tight')
  plt.close('all')

if __name__=="__main__":
  d = {'a': 0.1, 'b': 0.2, 'c': 0.33, 'd': 0.2}
  show_ranks(d)


import environ,os, boto3
from pathlib import Path
import base64
import hashlib
import datetime

BASE_DIR = Path(__file__).resolve().parent.parent

environ.Env.read_env(os.path.join(BASE_DIR, '.env'))

env = environ.Env(
    DEBUG=(bool, True)
)

def upload(user,image,*args):
    s3 = boto3.client('s3',
        aws_access_key_id=env('AWSAccessKeyId'),
        aws_secret_access_key=env('AWSSecretKey'),
        region_name='ap-northeast-2',
    )

    Bucket = "pracs3"

    if args:
        key = f'{user}/{args}/profile.jpg'
    else:
        key = f'{user}/profile.jpg'

    s3.put_object(
        ACL="public-read",
        Bucket=Bucket,
        Body=image,
        Key=key,
        ContentType=image.content_type,
        )

    url = f'https://{Bucket}.s3.ap-northeast-2.amazonaws.com/{key}'

    return url


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import datetime

# Reading the data
path = '/Users/iuliano/Documents/Proj/'
df = pd.read_excel(path + 'Dataset - Case Study - BI Analyst Wunderflats.xlsx')

# Rename columns
df = df.rename(columns={'dteday': 'date',
                        'yr': 'year_sk',
                        'mnth': 'month_int',
                        'hr': 'hour',
                        'weathersit': 'weather_sk',
                        'hum': 'humidity',
                        'season': 'season_sk',
                        'cnt': 'totals'})

# Dataset Exploration
print('Rows: ', df.shape[0])
print('Columns: ', df.shape[1])
print(df.info())
print('Total NULL Values per Columns\n', df.isna().sum())

# Plotting a quick correlation heatmap to have an overall view of relationships among features
corr = df.corr()
sns.heatmap(corr, annot=True)
plt.title('Correlation Heatmap among Features')

# Casual drivers are positively correlated with temperatures, and negativily correlated with humidity.
# This shows that casual drivers are in relation with weather conditions
# For registered drivers, the correlation is weaker showing that they might be more daily commuters

# Time Range #
min_d = df.date.min()
max_d = df.date.max()
print('Min date: ', min_d)
print('Max date: ', max_d)
# 2 Years data

# Totals across time
df['year'] = df['date'].dt.year
totals = df.groupby('year', as_index=False)['totals'].sum()
_ = totals.plot.bar(x='year')
plt.title('Total Rides by Year')

# Total registered and casual rides by year
reg_cas = df.groupby('year', as_index=False)[['registered', 'casual', 'totals']].sum()
_ = reg_cas[['year', 'registered', 'casual']].plot.bar(x='year')
plt.title('Bike usage for Registered and Casual by Year')
plt.ylabel('Count Rides')

reg_cas['registered'] = round((reg_cas['registered'] / reg_cas['totals']) * 100, 0)
reg_cas['casual'] = round((reg_cas['casual'] / reg_cas['totals']) * 100, 0)
print('Yearly Share of Registered and Casual\n', reg_cas)

# The percentage of registered increased by 2 points in 2012 and casual lost 2 points.
# Showing that something has been done to promote registrations over casual and
# that potentially casual have been converted into registered. As we don't have the granularity
# on a single user we cannot estimate how many converted.

# Month trend
df['year_month'] = df['date'].dt.to_period('M')
monthly = df.groupby('year_month', as_index=False)[['registered', 'casual']].sum().plot(x='year_month')
plt.title('Monthly Service Usage by Registared and Casual')
plt.show()

# Seasonal trend: can we observe different trends across seasons?
df['year_season'] = df['year'].astype(str) + '_' + df['season_sk'].astype(str)
seasonal = df.groupby('season_sk', as_index=False)[['registered', 'casual']].sum().plot.bar(x='season_sk')
plt.title('Seasonal Trend by Registered and Casual')

# Srping, Summer and Fall are the seasons with most users
# Distribution of rides across seasons
sns.boxplot(x=df['season_sk'], y='totals', data=df)
plt.title('Distribution of Rides across Seasons')

# Let's have a look at the weekdays and group by reg and cas
df['day_name'] = df.date.dt.day_name()
df['weekday'] = df.weekday.astype(int)
df['day_name'] = df.weekday.astype(str) + df.day_name
week_day = df.groupby('day_name', as_index=False)[['registered', 'casual']].sum().plot.bar(x='day_name')
week_day.plot.bar(x='day_name', y=['registered', 'casual'])

dist_day = df[['day_name', 'registered', 'casual']]
fig, ax = plt.subplots(2, 1)
sns.boxplot(dist_day['day_name'], dist_day['registered'], ax=ax[0])
ax[0].set_title('Weekday Distribution - Registered')
ax[0].set_xlabel("")
sns.boxplot(dist_day['day_name'], dist_day['casual'], ax=ax[1])
ax[1].set_title('Weekday Distribution - Casual')
ax[1].set_xlabel("")

# Hourly rides by reg and cas
df.groupby('hour', as_index=False)[['registered', 'casual']].sum().plot(x='hour')
plt.title('Hourly Trend')
plt.show()

# Hourly trend over the weekend and workingdays
fig, ax = plt.subplots(2, 1)
working_day = df[df.weekday == 1].groupby('hour', as_index=False)[['registered', 'casual']].sum()
weekends = df[df.weekday == 0].groupby('hour', as_index=False)[['registered', 'casual']].sum()
ax[0].plot(working_day['hour'], working_day[['registered', 'casual']], marker='v', linestyle='--')
ax[0].set_title('Hourly Working Days Rides')
ax[1].plot(weekends['hour'], weekends[['registered', 'casual']], marker='v', linestyle='--')
ax[1].set_title('Hourly Weekends Rides')
# Weekdays and weekends have opposite trends. But the increase of casual is remarkable during the weekend

# What's the relationship with the weather?
sns.heatmap(df[['temp', 'windspeed', 'humidity', 'registered', 'casual']].corr(), annot=True)
plt.title('Heatmap of Rides in Correlation with Weather')
# The casual riders seem to be more sensitive to temperature. As it increases, also casual riders are more around.

# Creating 2 dimensional table and joining to use labels instead of sk.

# Create Dim Tables to merge #
dim_season = pd.DataFrame({'season_sk': [1, 2, 3, 4],
                           'season': ['winter', 'spring', 'summer', 'fall']})

dim_weather = pd.DataFrame({'weather_sk': [1, 2, 3, 4],
                            'weather': ['Few clouds, Partly cloudy, Partly cloudy',
                                        'Mist + Cloudy, Mist + Broken clouds, Mist + Few clouds, Mist',
                                        'Light Snow, Light Rain + Thunderstorm + Scattered clouds, Light Rain + Scattered clouds',
                                        'Heavy Rain + Ice Pallets + Thunderstorm + Mist, Snow + Fog']})

# Join the 2 tables on sk
data = df.merge(dim_season, how='inner', on='season_sk').merge(dim_weather, how='inner', on='weather_sk')

bike_share = data[
    ['instant', 'date', 'year', 'year_season', 'month_int', 'hour', 'holiday', 'weekday', 'workingday', 'season',
     'weather', 'temp', 'atemp', 'humidity', 'windspeed', 'casual', 'registered', 'totals']]

# Export cleaned data set as csv file and plot it into Tableau
wf = bike_share.to_csv(path + 'bike_sharing_wf.csv',
                       header=True,
                       index=False)


"""
Written by: Jonas Vander Vennet
on: 2019/12/30
Answer: 430
"""

def get_orbitmap(orbits):
    orbitmap = {}
    for center, orbiter in orbits:
        if center not in orbitmap.keys():
            orbitmap[center] = []
        if orbiter not in orbitmap.keys():
            orbitmap[orbiter] = []
        orbitmap[center].append(orbiter)
    return orbitmap

def get_center(node, orbitmap):
    for key, value in orbitmap.items():
        if node in value:
            return key

def path_to(node, orbitmap, destination='COM'):
    current = get_center(node, orbitmap)
    path = []
    while current != destination:
        path.append(current)
        current = get_center(current, orbitmap)
    return path

def get_intersection(path1, path2):
    # optimize speed of intersection searching
    # by looping over largest path first
    if len(path2) > len(path1):
        temp = path1
        path1 = path2
        path2 = temp

    for node1 in path1:
        for node2 in path2:
            if node1 == node2:
                return node1

def num_orbital_transfers(orbits):
    orbitmap = get_orbitmap(orbits)

    path_you, path_santa = path_to('YOU', orbitmap), path_to('SAN', orbitmap)
    intersection = get_intersection(path_you, path_santa)

    return path_you.index(intersection) + path_santa.index(intersection)

def main():
    expected_output = 4
    with open('test.txt') as ifp:
        orbit_declarations = ifp.readlines()
    orbit_declarations = [tuple(decl.replace('\n', '').split(')')) for decl in orbit_declarations]

    assert(num_orbital_transfers(orbit_declarations) == expected_output)

    with open('../input.txt') as ifp:
        orbit_declarations = ifp.readlines()
    orbit_declarations = [tuple(decl.replace('\n', '').split(')')) for decl in orbit_declarations]
    print(num_orbital_transfers(orbit_declarations))

if __name__ == '__main__':
    main()


# udemy
# import Account as Acc
from operation import Operation
from os import system
accounts = []
op = Operation()
#print(Op)
if __name__ == "__main__":
	while(True):
		op.printOptions()
		options='0'
		options = input("Selection : ")
		if options=="q":
			break
		elif (options!="clear"):
			system("clear")
			op.doOp(options,accounts)
		else:
			system("clear")


from typing import Tuple
from neural import *

def parse_line(line: str) -> Tuple[List[float], List[float]]:
    """Splits line of CSV into inputs and output (transormfing output as appropriate)

    Args:
        line - one line of the CSV as a string

    Returns:
        tuple of input list and output list
    """
    tokens = line.split(",")

    print()
    print(tokens)
    if "?" not in tokens:
        out = int(tokens[13])
        output = [0 if out == 0 else 0.25 if out == 1 else
            0.5 if out == 2 else 0.75 if out == 3 else 1
                ]

        inpt = [float(x) for x in tokens[1:13]]
        print((inpt, [out]))

        return (inpt, [out])
    # else:
    #     out = int(tokens[13])
    #     output = [1 if out == 1 else 0.5 if out == 2 else 1]
    #     print("found")

    #     inpt = [float(x) for x in tokens[:13]]
    #     inpt.append(1)
    #     print((inpt, [out]))

    #     return (inpt, [out])

def normalize(data: List[Tuple[List[float], List[float]]]):
    """Makes the data range for each input feature from 0 to 1

    Args:
        data - list of (input, output) tuples

    Returns:
        normalized data where input features are mapped to 0-1 range (output already
        mapped in parse_line)
    """
    leasts = len(data[0][0]) * [100.0]
    mosts = len(data[0][0]) * [0.0]

    for i in range(len(data)):
        for j in range(len(data[i][0])):
            if data[i][0][j] < leasts[j]:
                leasts[j] = data[i][0][j]
            if data[i][0][j] > mosts[j]:
                mosts[j] = data[i][0][j]

    for i in range(len(data)):
        for j in range(len(data[i][0])):
            data[i][0][j] = (data[i][0][j] - leasts[j]) / (mosts[j] - leasts[j])
    return data

with open("processed.cleveland.data", "r") as f:

    # # Read in the file contents as a string
    # contents = f.read()

    # # Split the contents of the file into individual words
    # words = contents.split()

    # # Loop through each word and replace it with a random integer
    # for i in range(len(words)):
    #     # Check if the current word is composed entirely of letters
    #     if words[i].isalpha():
    #         # Generate a random integer between 1 and 4
    #         random_int = random.randint(1, 4)
    #         # Replace the current word with the random integer
    #         words[i] = str(random_int)

    # # Join the words back together into a string with spaces between them
    # new_contents = ' '.join(words)

    training_data = [parse_line(line) for line in f.readlines() if len(line) > 4]

for line in training_data:
    print (line)
    print("Here")

# # Open the input file for reading
# with open("processed.cleveland.data", "r") as f:

#     # Read in the file contents as a string
#     contents = f.read()

#     # Split the contents of the file into individual words
#     words = contents.split()

#     # Loop through each word and replace it with a random integer
#     for i in range(len(words)):
#         # Check if the current word is composed entirely of letters
#         if words[i].isalpha():
#             # Generate a random integer between 1 and 50
#             random_int = random.randint(1, 50)
#             # Replace the current word with the random integer
#             words[i] = str(random_int)

#     # Join the words back together into a string with spaces between them
#     new_contents = ' '.join(words)

# Open the output file for writing and write the new contents to it
# with open("processed.cleveland.data", "r") as f:
#     f.write(new_contents)

td = normalize(training_data)
test_data = (td)

nn = NeuralNet(13, 7, 1)
nn.train(td, iters=1000, print_interval=1000, learning_rate=0.1)

for i in nn.test_with_expected(test_data):
    print(i)
    print(f"desired: {i[1]}, actual: {i[2]}")
    print(i)


"""
1. Go to copart.com
2. Look at the Makes/Models section of the page
3. Create a two-dimensional list that stores the names of the Make/Model as well as their URLs
4. Check that each element in this list navigates to the correct page
"""
from pprint import pprint

MAKES_AND_MODELS = "//a[contains(@href, 'popular/make') or contains(@href, 'popular/model')]"

def test_ch7(py):
    py.visit('https://copart.com')
    cars = []
    for make_model in py.find_xpath(MAKES_AND_MODELS):
        name = make_model.text()
        url = make_model.get_attribute('href')
        cars.append([name, url])
    # pprint(cars)
    for car in cars:
        name, url = car
        py.visit(url)
        assert py.contains(name.lower())


from flask import render_template, Blueprint, flash
from flask import request, redirect, url_for, session
from suchwow.models import Post, Comment, Profile
from suchwow.utils.decorators import login_required

bp = Blueprint("comment", "comment")

@bp.route("/comment/create/post/<post_id>", methods=["GET", "POST"])
@login_required
def create(post_id):
    if not Post.filter(id=post_id):
        flash("WTF, that post doesn't exist. Stop it, hackerman.")
        return redirect(url_for("index"))

    if request.method == "POST":
        comment_text = request.form.get("comment")
        if len(comment_text) > 300:
            flash("WTF, too many characters to post, asshole.")
            return redirect(request.url)
        commenter = Profile.get(username=session["auth"]["preferred_username"])
        post = Post.get(id=post_id)
        c = Comment(
            comment=comment_text,
            commenter=commenter,
            post=post,
        )
        c.save()
        return redirect(url_for("post.read", id=post_id))
    return render_template("comment/create.html")


from sys import argv

import jetson_inference as ji
import jetson_utils as ju
import time
from jetson_utils import cudaFont
import argparse

def draw_boxes(img, detections):
    for detection in detections:
        if detection.ClassID == 1:
            left = int(detection.Left)
            top = int(detection.Top)
            right = int(detection.Right)
            bottom = int(detection.Bottom)
            # ju.cudaDrawRect(img, (left, top, right, bottom), (0, 255, 0, 255))
            ju.cudaDrawRect(img, (left, top, right, bottom), font.Gray40)

# Initialize the detection network, camera and display
net = ji.detectNet("/home/striker/.local/lib/python3.8/site-packages/yolov5/yolov5s.engine", threshold=0.5)
# net = ji.detectNet("/home/striker/.local/lib/python3.8/site-packages/yolov5/yolov5s.onnx",argv=[ '--input-blob=input_0 --output-cvg=scores --output-bbox=boxes'])
# net = ji.detectNet("/home/striker/.local/lib/python3.8/site-packages/yolov5/yolov5s.onnx",argv=[ '--output-bbox=boxes'])
# net = ji.detectNet("/home/striker/.local/lib/python3.8/site-packages/yolov5/yolov5s.onnx",argv=['-- output-bbox=boxes'])
# net = ji.detectNet(argv['--model=/home/striker/.local/lib/python3.8/site-packages/yolov5/yolov5s.onnx', ' --input-blob=input_0', ' --output-cvg=scores', ' --output-bbox=boxes'])

camera = ju.videoSource("v4l2:///dev/video4")
display = ju.videoOutput("display://0")

# Initialize CUDA Font object for FPS display
font = cudaFont()

while display.IsStreaming():
    # Capture a frame from the camera
    img = camera.Capture()

    # Perform object detection on the frame
    detections = net.Detect(img)

    # Draw boxes around the detected objects
    draw_boxes(img, detections)

    # Calculate and display FPS on the screen
    display.SetStatus("{:s} | Network {:.0f} FPS".format('PedNet', net.GetNetworkFPS()))
    # print out performance info
    net.PrintProfilerTimes()

    # Display the frame on the screen

    display.Render(img)

# Release resources
camera.Close()
display.Close()


import atexit
import sqlite3
import sys
import traceback

sys.path.append('./server')
from content import *
from helper import *

# Example value: 8_to_9
migration_numbers = sys.argv[1]
version = int(sys.argv[2])

# There must be two SQL files for each migration. One checks
# whether the migration has already occurred. The other modifies the
# database. Using the example value, these files would be called
# 8_to_9_check.sql and 8_to_9_migrate.sql, respectively.
# The check query must have a column called "count" in the select
# statement.
check_file_path = f"migration_scripts/{migration_numbers}_check.sql"
migrate_file_path = f"migration_scripts/{migration_numbers}_migrate.sql"

content = Content()

check_sql = read_file(check_file_path)

if content.fetchone(check_sql)["count"] > 0:
    print("***NotNeeded***")
else:
    sql_statements = read_file(migrate_file_path).split(";")
    params_list = [() for x in sql_statements]

    try:
        content.execute_multiple(sql_statements, params_list)

        if version >= 39:
            content.update_all_when_content_updated()

        print("***Success***")
    except:
        print(traceback.format_exc())


# -*- coding: utf-8 -*-
"""
Created on Fri May 19 07:24:06 2017

@author: manuj
"""
import datetime
from django.utils import timezone
from dateutil.parser import parse
import datefinder
import difflib

import time
import numpy as np
import pandas as pd

from MovieScraper import MovieScraper
from MovieListCleaner import MovieListCleaner
from .models import Movie
from django.db.models import Max
from django.db.models import Min
from django.utils import timezone
import imdb
import re
from imdb._exceptions import IMDbDataAccessError, IMDbParserError

class Utils:

    MAX_SCRAP_PAGES = 100
    INIT_SCRAP_TIME = 30 #days

    @classmethod
    def convert_timezone_aware(self,x):
        if isinstance(x,datetime.datetime):
            return timezone.make_aware(x)
        else:
            return x

    # @transaction.atomic
    @classmethod
    def scrape_and_add_movies(self):
        cut_off_year = 2015
        #Actions to be done here
        if Movie.objects.all().count()>0:
            post_max_date = timezone.make_naive(Movie.objects.latest('post_date').post_date)
        else:
            post_max_date = datetime.datetime.now() - datetime.timedelta(self.INIT_SCRAP_TIME)
        movie_scraped = MovieScraper()
        movie_scraped.scrape_site(self.MAX_SCRAP_PAGES, post_max_date)
        if len(movie_scraped.movieScraped.index) >0 :
            movie_clean = MovieListCleaner(movie_scraped.movieScraped)
            movie_clean.clean_movie()
            movie_df = movie_clean.cleanMovieList
            print("scrape complete")
            #rename dataframe to match model
            cols = [ 'name','year', 'genre', 'imdb_rating', 'imdb_votes','rt_critics','plot', 'starring','director',
                'imdb_link','rt_link', 'post_link','release_name', 'release_type', 'release_date','thumbnail_link',
                'date_time','trailer_link', 'tomatometer','rt_rating','post_date']
            movie_df.columns = cols
            #print(movie_df.name)
            movie_df.replace(r'^\s+$', np.nan, regex=True, inplace=True)
            #Making Date-time Timezone aware
            movie_df['release_date'] = movie_df['release_date'].map(self.convert_timezone_aware, na_action = 'ignore')
            movie_df['date_time'] = movie_df['date_time'].map(self.convert_timezone_aware, na_action = 'ignore')
            movie_df['post_date'] = movie_df['post_date'].map(self.convert_timezone_aware, na_action = 'ignore')
            #Make Key
            movie_df['key'] = movie_df[['name','year', 'genre', 'imdb_rating', 'imdb_votes','rt_critics','plot', 'starring','director',
                'imdb_link','rt_link', 'post_link','release_name', 'release_type', 'release_date','thumbnail_link',
                'trailer_link', 'tomatometer','rt_rating','post_date']].apply(lambda row: ','.join(map(str, row)), axis=1)
            movie_df['year'] = pd.to_numeric(movie_df['year'], errors='coerce')
            movie_df.dropna(subset='year',inplace=True)
            movie_df = movie_df.loc[movie_df.year>=cut_off_year]
            #print(movie_df.name)
            # print(movie_df.key.count())
            # print(len(movie_df.key.unique()))
        else:
            movie_df = pd.Dataframe()
        movie_dict = movie_df.to_dict('records')
        #replacing empty strings with None
        counter = 0
        for movie in movie_dict:
            # print(timezone.make_naive(movie['post_date']))
            # print(timezone.make_naive(movie['post_date']) <= post_max_date)
            if isinstance(movie['post_date'], datetime.datetime):
                if timezone.make_naive(movie['post_date']) <= post_max_date:
                    # print("Max post date")
                    if not(Movie.objects.filter(key=movie['key']).exists()):
                        # print("new-saving")
                        m = Movie(**movie)
                        m.save()
                        counter = counter+1
                else:
                    m = Movie(**movie)
                    m.save()
                    counter = counter+1

        return counter

    # def update_all_ratings():
    #     #for debug only
    #     movies = Movie.objects.filter(movie_read = False)
    #     # Create the object that will be used to access the IMDb's database.
    #     ia = imdb.IMDb() # by default access the web.
    #     counter = 0
    #     for movie in movies:
    #         print("movie name: {}".format(movie.name))
    #         print("movie link: {}".format(movie.imdb_link))
    #         movie_info, imdb_link_present = get_imdb_info(movie.imdb_link,movie.name,ia)
    #         if movie_info:
    #             if 'rating' in movie_info.keys():
    #                 print ("new rating: {} <--- old rating: {}".format(movie_info['rating'],movie.imdb_rating))
    #             else:
    #                 print("no ratings")
    #             if 'votes' in movie_info.keys():
    #                 print ("new votes: {} <--- old votes: {}".format(movie_info['votes'],movie.imdb_votes))
    #             else:
    #                 print ("no votes")
    #             counter = counter +1
    #             #For Debug only
    #             # movie.movie_read = True
    #             # movie.save()
    #     return counter

    # @transaction.atomic
    @classmethod
    def update_all_ratings(self):
        movies = Movie.objects.filter(movie_read = False)
        print("in update: count of movie {}".format(movies.count()))
        # Create the object that will be used to access the IMDb's database.
        ia = imdb.IMDb() # by default access the web.
        counter = 0
        for movie in movies:
            print("movie name: {}".format(u''.join(movie.name).encode('utf-8').strip()))
            print("movie link: {}".format(u''.join(movie.imdb_link).encode('utf-8').strip()))
            movie_info, imdb_link_present = self.get_imdb_info(movie.imdb_link,movie.name,ia)
            if movie_info:
                if 'rating' in movie_info.keys():
                    movie.imdb_rating = movie_info['rating']
                if 'votes' in movie_info.keys():
                    movie.imdb_votes = movie_info['votes']
                if 'plot' in movie_info.keys():
                    movie.plot = movie_info['plot']
                if not imdb_link_present:
                    movie.imdb_link = ia.get_imdbURL(movie_info)
                movie.datetime = timezone.make_aware(datetime.datetime.now())
                movie.save()
                print ("movie rating: {}".format(movie.imdb_rating))
                print ("movie votes: {}".format(movie.imdb_votes))
                counter = counter +1
        return counter

    @classmethod
    def get_imdb_info(self,imdb_link,imdb_name,ia):
        match = re.search(r'^https?://[^\s]+/[^\s]+/tt([^\s]+)$', imdb_link.strip('/'))
        if match is not None:
            id = match.group(1)
            try:
                movie = ia.get_movie(id)
                return movie, True
            except IMDbParserError as e:
                result_set = ia.search_movie(imdb_name)
                if len(result_set)>0:
                    return ia.search_movie(imdb_name)[0], False
                else:
                    return False, False
        else:
            result_set = ia.search_movie(imdb_name)
            if len(result_set)>0:
                return ia.search_movie(imdb_name)[0], False
            else:
                return False, False

    @classmethod
    def remove_duplicates_in_db(self):
        df = pd.DataFrame(list(Movie.objects.all().values()))
        print ("Total Number of Movies: {}".format(len(df)))
        movie_cleaner = MovieListCleaner(df)
        df = movie_cleaner.remove_duplicates_model(df)
        id_list = df.id.tolist()
        duplicates = Movie.objects.exclude(id__in = id_list).delete()
        #Movie query to delete all ids which are not in this list
        # print(df.id)
        return duplicates[0]

    # @commit_on_success
    @classmethod
    def update_release_type(self):
        movies = Movie.objects.filter(release_type="")
        movie_scraper = MovieScraper()
        for movie in movies:
            movie.release_type = movie_scraper.find_release_type_from_name(movie.release_name)
            movie.save()
        return movies.count()


import tensorflow.compat.v2 as tf
from scipy.optimize import minimize
import numpy as np
import tensorflow_probability as tfp

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter

np.sum(np.array([a for a in range(1, 10,1)]))
x_ = np.arange(start = -1, stop = 0, step = 0.1)
np.sum(x_[x_.__len__() // 4 :x_.__len__()//2])
x_range_ = np.arange(4)
x_range_[:-1] # по последнему не смотрят
#x_range_[1:] # по первому игнор

@tf.function
def input_tens(tens):
    tf.print(tens)

def rosen(x):
    """The Rosenbrock function"""
    return np.sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0, axis=0)

x_another = [x_range_,-x_range_]
print(x_another)
np.sum(x_another)

x = tf.convert_to_tensor(np.full((2,1), 1))

@tf.function
def input_(x_tens):
    tf.print(x_tens)

input_(x)

x_ = np.full((3,), 2)
rosen(x_)

tens_ = tf.convert_to_tensor(rosen(x_))
input_(tens_)
tens_.__repr__()

def rosen_tf(x_tens):
    return tf.reduce_sum(100.0 * (x_tens[1:] - x_tens[:-1] ** 2) ** 2 + (1 - x_tens[:-1]) ** 2)

def diff_(x_tens):
    return tf.reduce_sum(x_tens) * 3.0

class function(tf.Module):

    def compute_gradient_optional(self, functional_obj):
        with tf.GradientTape() as g:
            g.watch(self._tensor)
            res = functional_obj(self._tensor)
            grad_ = g.gradient(res, self._tensor)

        return grad_

    def __init__(self, x_numpy):

        assert( (x_numpy.shape.__len__() == 1), \
               ('We tae a x_numpy shape not (ndims, ) ! \n shape tensor is : {!s}'.format(x_numpy.shape.__len__())))

        if(x_numpy.shape.__len__() != 1):
            assert (1 != 1) ('shape is not signature : (n, )!')
        else:
            self._tensor = tf.Variable(x_numpy)
            self._dims = x_numpy.shape[0]

    def _call_f(self, function = rosen):
       return tf.converct_to_tensor(function(self._tensor)) ## При условии что ее вообще можно преобразовать в тензор

    def __call__(self, some_parameter = 0): # define a default parameter rosen function
        self._another = tf.convert_to_tensor(rosen(self._tensor.numpy()))
        return self._another  ## we can define call for another functional pbject

    def __repr__(self):
        return (self._tensor.__repr__(), self._another.__repr__())

    def __str__(self):

        return "Dimensional is {!s} \n Vector x is : {!s} \n Value of functional is: {!s} \n".format(
                                      self._dims, self._tensor.numpy(), self.__call__(some_parameter = 0).numpy())

    def _change(self, new_vector):
        @tf.function
        def assign_(x_new):
            x_new_tensor = tf.convert_to_tensor(x_new)
            if(x_new_tensor.shape != self._tensor.shape):
                assert (1 != 1) ("shape of tensor is not a equal!")
            else:
                self._tensor.assign(x_new_tensor)

        assign_(new_vector)

    def _wrapper(self):
        def _inner(vars_x) -> np.float32:
            if(vars_x.shape[0] != self._tensor.shape[0]):
                assert (1 != 1) ('Error with different size!: ')
                return None
            else:
                self._change(vars_x) # we change aor vector of x
                return (self.__call__(some_parameter = 0)).numpy()

        return _inner

    def compute_gradient(self):
                with tf.GradientTape() as g:
                    g.watch((self._tensor))
                    f_a = tf.reduce_sum(100.0 * (self._tensor[1:] - self._tensor[:-1] ** 2) ** 2 + (1 - self._tensor[:-1]) ** 2)
                    grad_ = g.gradient(f_a, self._tensor)

                return grad_

    def wrapper_grad(self):
        def inner_grad(vars_x, *args) -> float: # should be a vector of shape np.float
            if(vars_x.shape[0] != self._tensor.shape[0]):
                assert (1 != 1) ('Error with different size!: ')
                return None
            else:
                self._change(vars_x)
                return self.compute_gradient().numpy()

        return inner_grad

    def compute_hessian(self):
        with tf.GradientTape(persistent = True) as g:
            g.watch(self._tensor)
            f_a = tf.reduce_sum(100.0 * (self._tensor[1:] - self._tensor[:-1] ** 2) ** 2 + (1 - self._tensor[:-1]) ** 2)
            grad_ = g.gradient(f_a, self._tensor)
            list_ = []
            for iter_ in range(self._tensor.shape[0]):
                grad_grad = g.gradient(grad_[iter_], self._tensor)
                list_.append(grad_grad)

            matrix = tf.convert_to_tensor(list_)
        return matrix

    def wrapper_hessian(self):
        def inner_hessian(vars_x, *args) -> float:
            if(vars_x.shape[0] != self._tensor.shape[0]):
                assert (1 != 1) ('Error with different size!: ')
                return None
            else:
                self._change(vars_x)
                return self.compute_hessian().numpy()

        return inner_hessian

x_tens_ = tf.constant(np.full((5,), 2.))
x_another = tf.constant(np.full((5,), 1.5))

foo_ = function(x_tens_)
print("proba: ", foo_.compute_gradient_optional(diff_))
param_ = 3
input_(foo_.__call__(param_))

foo_._change(np.full((5, ), 2.))
function_ = foo_._wrapper()
funct_grad = foo_.wrapper_grad()

input_tens(foo_.compute_gradient())

funct_hessian = foo_.wrapper_hessian()

#print(funct_grad(np.full((5,), 0.0)))

from scipy.optimize import minimize
x_0 = np.array([1.3 , 0.7 , 0.8, 3.0, 2.0])
res_ = minimize(function_, x_0, method = 'nelder-mead', options = {'xtol': 1e-8, 'disp': True})
print(res_.x)
res_an_ = minimize(function_, x_0, method = 'BFGS', jac = funct_grad, options = {'disp': False})

print(res_an_)

res_another = minimize(function_, x_0, method = 'Newton-CG', jac = funct_grad,hess = funct_hessian,options = {'xtol' : 1e-8, 'disp': True})
print(res_another)


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

from __future__ import print_function

try:
    bytes
except NameError:
    bytes = str
    str = unicode

def reverse(x):
    '''We assume it is a helper function for something else.

    It returns True to let other stuff work.
    '''

    if not isinstance(x, (bytes, str)):
        x = str(x)

    if isinstance(x, bytes):
        x = x.decode('utf-8')

    print(x[::-1])

    return x

if __name__ == '__main__':
    import clime
    clime.start(ignore_return=True, debug=True)


import os
import random
from collections import defaultdict
import nltk
from nltk.util import ngrams

# Step 1: Import necessary libraries
nltk.download('punkt')

# Step 2: Read and preprocess the text from the files
def read_text_files(folder_path):
    text_corpus = []
    for filename in os.listdir(folder_path):
        if filename.endswith('.txt'):
            with open(os.path.join(folder_path, filename), 'r', encoding='utf-8') as file:
                text = file.read()
                text = text.lower()  # Convert to lowercase
                text_corpus.extend(nltk.sent_tokenize(text))
    return text_corpus

# Step 3: Create bigram and trigram models
def create_ngram_models(text_corpus, n):
    ngram_model = defaultdict(list)

    for sentence in text_corpus:
        words = nltk.word_tokenize(sentence)
        ngrams_list = list(ngrams(words, n, pad_left=True, pad_right=True))

        for ngram in ngrams_list:
            ngram_model[ngram[:-1]].append(ngram[-1])

    return ngram_model

folder_path = 'C:/Users/Lenovo/OneDrive/Desktop/IR/dhruv'  # Update with your folder path
text_corpus = read_text_files(folder_path)
bigram_model = create_ngram_models(text_corpus, 2)
trigram_model = create_ngram_models(text_corpus, 3)

# Step 4: Generate random text using the models
def generate_random_text(model, n, num_sentences=5, max_length=100):
    generated_text = []

    for _ in range(num_sentences):
        sentence = []
        current_ngram = [None] * (n - 1)

        while True:
            next_word = random.choice(model[tuple(current_ngram)])
            if next_word is None:
                break
            sentence.append(next_word)
            current_ngram = current_ngram[1:] + [next_word]
            if len(sentence) >= max_length:
                break

        generated_text.append(' '.join(sentence))

    return '\n'.join(generated_text)

# Generate random bigram text
random_bigram_text = generate_random_text(bigram_model, 2)

# Generate random trigram text
random_trigram_text = generate_random_text(trigram_model, 3)

print("Random Bigram Text:")
print(random_bigram_text)

print("\nRandom Trigram Text:")
print(random_trigram_text)


import xml.etree.ElementTree as ET
import gpxpy
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from datetime import datetime,timedelta
import osmnx as ox
import networkx as nx
import xml.sax
import fiona
import csv
from rtree import index
import math
from math import exp, sqrt
from datetime import datetime
import itertools
from collections import Counter, defaultdict
from pyproj import Proj, transform
from sklearn.neighbors import NearestNeighbors
import scipy
from scipy import spatial
import xml.sax
import statistics
import ast
from os import listdir,path
import time
from numpy import ma
from pykalman import KalmanFilter
import pywt
from sklearn.metrics import confusion_matrix
from scipy.stats import entropy

from sklearn.cluster import KMeans
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.neural_network import MLPClassifier
from sklearn import svm, metrics
import hdbscan

plt.rcParams.update({'font.size': 20})

FMT = '%H:%M:%S'
def DTtoFloat(dstring):
    x = datetime.strptime(dstring, FMT)
    return x.hour * 3600 + x.minute * 60 + x.second

def lowpassfilter(signal, thresh, wavelet="db3"):
    thresh = thresh*np.nanmax(signal)
    coeff = pywt.wavedec(signal, wavelet, mode="per" )
    coeff[1:] = (pywt.threshold(i, value=thresh, mode="soft" ) for i in coeff[1:])
    reconstructed_signal = pywt.waverec(coeff, wavelet, mode="per" )
    return reconstructed_signal

def calculate_entropy(list_values):
    counter_values = Counter(list_values).most_common()
    probabilities = [elem[1]/len(list_values) for elem in counter_values]
    entropy=scipy.stats.entropy(probabilities)
    return entropy

def Kmeans(data):
    kmeans = KMeans(n_clusters=2, random_state=0).fit(data)
    eL = kmeans.labels_

    return eL

def RF(X,y):

    X, y = make_classification(n_samples=1000, n_features=4,
                           n_informative=2, n_redundant=0, random_state=0, shuffle=False)
    clf = RandomForestClassifier(max_depth=2, random_state=0)
    clf.fit(X, y)

    #RandomForestClassifier(...)
    #print(clf.predict([[0, 0, 0, 0]]))

def SWF(testdf,predDWT,delTs): ## SLIDING WINDOW FILTERING
    ## sequential sliding window filtering > Check for consecutive 0 and 1

    predictDWT = []
    lastGroupValue = -1 # create a flag to keep track of the window:
    #Grouped_item = [list(group) for idx, (key, group) in enumerate(itertools.groupby(predDWT))]
    end_index = 0
    for idx, (key, group) in enumerate(itertools.groupby(predDWT)):
        GrpList = list(group)
        #print(key, GrpList)
        start_index = end_index
        end_index = start_index + len(GrpList) # end index is the number of elements in the grp
        tempTimeOfStay = sum(delTs[start_index:end_index])
        #print(tempTimeOfStay)
        if tempTimeOfStay < 60:
            if lastGroupValue != -1:
                newTempList = [lastGroupValue] * len(GrpList)
                predictDWT = predictDWT + newTempList
            else:
                 predictDWT = predictDWT + GrpList
        else:
            predictDWT = predictDWT + GrpList
        lastGroupValue = predictDWT[-1]

    return predictDWT

def DWT_noise_filter(signal):
    ## DWT noise filtering of probabilities

    signalf = signal[:,1]
##    signal = predSVMProb[:,1]
##
    rec = lowpassfilter(signalf, 0.6)
    lenDiff = abs(len(rec) - len(testY))

    recDWT = rec[:(len(rec) - lenDiff)]
    mean = 0.5 #recDWT.mean()

    predDWT = np.array([val > mean for val in recDWT]) * 1 ## enable/disable DWT based filtering
    return predDWT

def PlotTime(x,y,xlabel,title,ylabel,lg1,lg2,xticks):

    poix = list(range(1,len(x)+1))
    fig, ax = plt.subplots()
    ax.plot(poix,x, c='red',marker='*', linestyle='-',label=lg1)
    #for an in x1:
        #ax.annotate(an,poi[an])
    ax.plot(poix,y,c='blue',marker='o', linestyle='-',label=lg2)
    plt.xlabel(xlabel)
    plt.title(title)
    plt.ylabel(ylabel)
    plt.xticks(np.arange(1, len(x)+1))
    ax.legend(loc='lower right', frameon=False)
    plt.xticks(poix, xticks, rotation=90)
    plt.grid(True)
    #plt.ylim(0,1)
    plt.show()

def CM(CMSVM):
    sen1 = CMSVM[0][0]/(CMSVM[0][0] + CMSVM[0][1])
    sen2 = CMSVM[1][1]/(CMSVM[1][1] + CMSVM[1][0])
    pr1 = CMSVM[0][0]/(CMSVM[0][0] + CMSVM[1][0])
    pr2 = CMSVM[1][1]/(CMSVM[1][1] + CMSVM[0][1])
    return np.round_([sen1,sen2,pr1,pr2],2)

dfTrainTest = pd.read_csv("TrainTestFile_filtered_CWSelected.csv",index_col=0) # + str(file)
#dfTrainTest.drop(['Unnamed: 0.1', 'Unnamed: 0.1.1'], axis=1,inplace=True)
dfTrainTest.replace([np.inf, -np.inf], 0,inplace=True)
dfTrainTest.fillna(0,inplace=True)

dfTrainTest.replace([np.inf, -np.inf], 0)
dfTrainTest.fillna(0)
df = dfTrainTest.reset_index(drop=True)

Gdf = df.groupby('filename').head(n=1)
start_list = Gdf.index.values.tolist()

TimeofStay = df.TimeOfStay.values
minTime = min(TimeofStay)

acSVM = []
acSVMf = []
acSVMdwt = []

acRF = []
acRFf = []
acRFdwt = []

acGB = []
acGBf = []
acGBdwt = []

acKM = []
acKMf = []

acHD = []
acHDf = []
acHDdwt = []

fn = []

trainMean = []
trainStd = []
dci = []
DS = []
te = []
qMeanStdTest = []
Entropy = []

CMSVMf = np.zeros((2,2))
CMSVM = np.zeros((2,2))

CMRFf = np.zeros((2,2))
CMRF = np.zeros((2,2))

CMGBf = np.zeros((2,2))
CMGB = np.zeros((2,2))

clsSVM = svm.SVC(probability=True,kernel='linear')#'rbf',gamma='auto')
clsSVMf = svm.SVC(probability=True,kernel='linear')#'rbf',gamma='auto')
clsRF = RandomForestClassifier(random_state=0)
clsRFf = RandomForestClassifier(random_state=0)
clsGB = GradientBoostingClassifier(random_state=0)
clsGBf = GradientBoostingClassifier(random_state=0)

TRfiles = list(dfTrainTest.filename.unique())
for i_start,cfile in enumerate(TRfiles): #enumerate(start_list):#

    testdf = df[df.filename == cfile]

    TIMELIST = [DTtoFloat(i) for i in testdf.time.values]
    delTs = np.ediff1d(TIMELIST).tolist()
    #minTime = min(delTs)
    delTs.insert(0,0)

    testY = testdf.observed.values #
    testX = testdf[['PointVelocityPrj','PointAccPrj','PointAnglePrj']].values

    testX =  testX/testX.max(axis=0)
    testXf = testdf[['velocityFiltered','accelFiltered','AngleFiltered']].values

    testXf =  testXf/testXf.max(axis=0)

    ### define train dataset
    traindf = df.drop(testdf.index.values, axis=0)
    traindf = traindf[traindf.PointVelocityPrj != 0]

    trainY = traindf.observed.values

    trainX = traindf[['PointVelocityPrj','PointAccPrj','PointAnglePrj']].values
    trainX =  trainX/trainX.max(axis=0)

    trainXf = traindf[['velocityFiltered','accelFiltered','AngleFiltered']].values
    trainXf =  trainXf/trainXf.max(axis=0)

       ####
    clsSVM.fit(trainX, trainY)
    clsSVMf.fit(trainXf, trainY)

    clsRF.fit(trainX, trainY)
    clsRFf.fit(trainXf, trainY)

    clsGB.fit(trainX, trainY)
    clsGBf.fit(trainXf, trainY)
    ##################
     ### prediction using SVM

    predSVM = clsSVM.predict(testX)
    #predSVMProb = clsSVM.predict_proba(testX)

    predSVMf = clsSVMf.predict(testXf)
    predSVMfProb = clsSVMf.predict_proba(testXf)

    ### prediction using RF

    predRF = clsRF.predict(testX)
    #predRFProb = clsRF.predict_proba(testX)

    predRFf = clsRFf.predict(testXf)
    predRFfProb = clsRFf.predict_proba(testXf)

    ### prediction using GB
    predGB = clsGB.predict(testX)
    #predGBProb = clsGB.predict_proba(testX)
    predGBf = clsGBf.predict(testXf)
    predGBfProb = clsGBf.predict_proba(testXf)

    ### append scores SVM
    acSVM.append(metrics.accuracy_score(testY,y_pred=predSVM))
    y_pred_SVMf = SWF(testdf,predSVMf,delTs)
    acSVMf.append(metrics.accuracy_score(testY,y_pred=y_pred_SVMf))

    SVMdwt = DWT_noise_filter(predSVMfProb)
    y_pred_SVMdwt = SWF(testdf,SVMdwt,delTs)
    acSVMdwt.append(metrics.accuracy_score(testY,y_pred=y_pred_SVMdwt))

    ### append scores RF
    acRF.append(metrics.accuracy_score(testY,y_pred=predRF))
    y_pred_RFf = SWF(testdf,predRFf,delTs)
    acRFf.append(metrics.accuracy_score(testY,y_pred=y_pred_RFf))

    RFdwt = DWT_noise_filter(predRFfProb)
    y_pred_RFdwt = SWF(testdf,RFdwt,delTs)
    acRFdwt.append(metrics.accuracy_score(testY,y_pred=y_pred_RFdwt))

    ### append scores GB
    acGB.append(metrics.accuracy_score(testY,y_pred=predGB))
    y_pred_GBf = SWF(testdf,predGBf,delTs)
    acGBf.append(metrics.accuracy_score(testY,y_pred=y_pred_GBf))

    GBdwt = DWT_noise_filter(predGBfProb)
    y_pred_GBdwt = SWF(testdf,GBdwt,delTs)
    acGBdwt.append(metrics.accuracy_score(testY,y_pred=y_pred_GBdwt))
    ###############

    te.append(len(testdf)/sum(delTs))
    ModeCounts = int(scipy.stats.mode(delTs)[1])
    dci.append(ModeCounts / len(testdf))
    DS.append(len(testdf))
    qMeanStdTest.append([np.mean(delTs),np.std(delTs)])
    Entropy.append(calculate_entropy(delTs))
    print(acSVM[-1])
    print(acSVMf[-1])
    print(acRF[-1])
    print(acRFf[-1])
    print(cfile)

    CMSVMf = np.add(CMSVMf,confusion_matrix(testY,y_pred_SVMf))
    CMSVM = np.add(CMSVM,confusion_matrix(testY,predSVM))

    CMRFf = np.add(CMRFf,confusion_matrix(testY,y_pred_RFf))
    CMRF = np.add(CMRF,confusion_matrix(testY,predRF))

    CMGBf = np.add(CMGBf,confusion_matrix(testY,y_pred_GBf))
    CMGB = np.add(CMGB,confusion_matrix(testY,predGB))

errordf = pd.DataFrame({'Dataset':TRfiles,'acRF':acRF,'acRFf':acRFf,'acRFdwt':acRFdwt,'acSVM':acSVM,'acSVMf':acSVMf,'acSVMdwt':acSVMdwt,'acGB':acGB,
                        'acGBf':acGBf,'acGBdwt':acGBdwt,
                      'Entropy':Entropy,'dci':dci,'MeanStd':qMeanStdTest,'TE':te})
errordf.to_csv('errordf_Subset_only.csv')

if 1 == 1:
    print(CMSVM)
    print(CMSVMf)
    print(CMRF)
    print(CMRFf)
    print(CMGB)
    print(CMGBf)

    print(CM(CMSVM))
    print(CM(CMSVMf))
    print(CM(CMRF))
    print(CM(CMRFf))
    print(CM(CMGB))
    print(CM(CMGBf))

    print(errordf.acRFf.mean())
    print(errordf.acRF.mean())
    print(errordf.acSVMf.mean())
    print(errordf.acSVM.mean())
    print(errordf.acGBf.mean())
    print(errordf.acGB.mean())


# A Simple Calculator using class and oops - Multiple inheritance
class Addition:
    def __init__(self, number01, number02):
        self.number01 = number01
        self.number02 = number02

    def add(self):
        """
        This function takes two numbers as input and returns their sum
        :return: the sum of first and second number
        """
        return self.number01 + self.number02

class Subtraction:
    def __init__(self, number01, number02):
        self.number01 = number01
        self.number02 = number02

    def diff(self):
        """
        This function takes two numbers as input and returns their difference
        :return: the difference of first and second number
        """
        return self.number01 - self.number02

class Division:
    def __init__(self, number01, number02):
        self.number01 = number01
        self.number02 = number02

    def division(self):
        """
        This function takes two numbers as input and returns the answer of first number divided by the second
        :return: the value of first number divided by the second number
        """
        return self.number01 / self.number02

class FloorDivision:
    def __init__(self, number01, number02):
        self.number01 = number01
        self.number02 = number02

    def floor_division(self):
        """
        This function takes two numbers as input and returns the whole number of first number divided by the second
        :return: the value of first number divided by the second number
        """
        return self.number01 // self.number02

class Multiplication:
    def __init__(self, number01, number02):
        self.number01 = number01
        self.number02 = number02

    def multiply(self):
        """
        This function takes two numbers as input and returns the answer of first number multiplied by the second
        :return: the value of first number multiplied by the second number
        """
        return self.number01 * self.number02

class Modulus:
    def __init__(self, number01, number02):
        self.number01 = number01
        self.number02 = number02

    def mod(self):
        """
        This function takes two numbers as input and returns the balance after perfectly dividing first
        number by the second number
        :return: the balance after perfectly dividing first number by the second
        """
        return self.number01 % self.number02

def print_menu():
    print(
        '''
        ---------------- CALCULATOR MENU ----------------

          1 - Add
          2 - Subtract
          3 - Multiply
          4 - Divide
          5 - Floor Division
          6 - Modulo
          7 - All

        -------------------------------------------------

        '''
    )

class Calculator(Addition, Subtraction, Division, Multiplication, FloorDivision, Modulus):
    pass

print_menu()
choice = int(input('Enter Your menu Choice - '))
num01 = int(input('Enter the First Number - '))
num02 = int(input('Enter the Second Number - '))
value = 0

if choice == 1:
    my_object = Addition(num01, num02)
    value = my_object.add()
elif choice == 2:
    my_object = Subtraction(num01, num02)
    value = my_object.diff()
elif choice == 3:
    my_object = Multiplication(num01, num02)
    value = my_object.multiply()
elif choice == 4:
    my_object = Division(num01, num02)
    value = my_object.division()
elif choice == 5:
    my_object = FloorDivision(num01, num02)
    value = my_object.floor_division()
elif choice == 6:
    my_object = Modulus(num01, num02)
    value = my_object.mod()
elif choice == 7:
    my_object = Calculator(num01, num02)
    value = {
        "Addition = ": my_object.add(),
        "Subtraction = ": my_object.diff(),
        "Division = ": my_object.division(),
        "Floor Division = ": my_object.floor_division(),
        "Modulus = ": my_object.mod(),
        "Multiplication = ": my_object.multiply()
    }
else:
    print('Invalid menu choice')

print('Result = ', value)


#from torchvision.datasets.vision import VisionDataset
from core.VisionDataset import VisionDataset

from PIL import Image

import os
import os.path

import imageio
import numpy as np
import random

from torchvision import transforms
import torch

def has_file_allowed_extension(filename, extensions):
    """Checks if a file is an allowed extension.

    Args:
        filename (string): path to a file
        extensions (tuple of strings): extensions to consider (lowercase)

    Returns:
        bool: True if the filename ends with one of given extensions
    """
    return filename.lower().endswith(extensions)

def is_image_file(filename):
    """Checks if a file is an allowed image extension.

    Args:
        filename (string): path to a file

    Returns:
        bool: True if the filename ends with a known image extension
    """
    return has_file_allowed_extension(filename, IMG_EXTENSIONS)

def make_dataset(directory, class_to_idx, extensions=None, is_valid_file=None):
    instances = []
    directory = os.path.expanduser(directory)
    both_none = extensions is None and is_valid_file is None
    both_something = extensions is not None and is_valid_file is not None
    if both_none or both_something:
        raise ValueError("Both extensions and is_valid_file cannot be None or not None at the same time")
    if extensions is not None:
        def is_valid_file(x):
            return has_file_allowed_extension(x, extensions)
    for target_class in sorted(class_to_idx.keys()):
        class_index = class_to_idx[target_class]
        target_dir = os.path.join(directory, target_class)
        if not os.path.isdir(target_dir):
            continue
        for root, _, fnames in sorted(os.walk(target_dir, followlinks=True)):
            for fname in sorted(fnames):
                path = os.path.join(root, fname)
                if is_valid_file(path):
                    item = path, class_index
                    instances.append(item)
    return instances

class DatasetFolder(VisionDataset):
    """A generic data loader where the samples are arranged in this way: ::

        root/class_x/xxx.ext
        root/class_x/xxy.ext
        root/class_x/xxz.ext

        root/class_y/123.ext
        root/class_y/nsdf3.ext
        root/class_y/asd932_.ext

    Args:
        root (string): Root directory path.
        loader (callable): A function to load a sample given its path.
        extensions (tuple[string]): A list of allowed extensions.
            both extensions and is_valid_file should not be passed.
        transform (callable, optional): A function/transform that takes in
            a sample and returns a transformed version.
            E.g, ``transforms.RandomCrop`` for images.
        target_transform (callable, optional): A function/transform that takes
            in the target and transforms it.
        is_valid_file (callable, optional): A function that takes path of a file
            and check if the file is a valid file (used to check of corrupt files)
            both extensions and is_valid_file should not be passed.

     Attributes:
        classes (list): List of the class names sorted alphabetically.
        class_to_idx (dict): Dict with items (class_name, class_index).
        samples (list): List of (sample path, class_index) tuples
        targets (list): The class_index value for each image in the dataset
    """

    def __init__(self, root, root_masks, loader, attributes, training, extensions=None, transform=None,
                 target_transform=None, is_valid_file=None):
        super(DatasetFolder, self).__init__(root, root_masks, attributes, training, transform=transform,
                                            target_transform=target_transform)
        classes, class_to_idx = self._find_classes(self.root)
        samples = make_dataset(self.root, class_to_idx, extensions, is_valid_file)
        if len(samples) == 0:
            raise (RuntimeError("Found 0 files in subfolders of: " + self.root + "\n"
                                "Supported extensions are: " + ",".join(extensions)))

        self.root_masks = root_masks
        self.loader = loader
        self.training = training
        self.extensions = extensions
        self.attributes = attributes

        self.classes = classes
        self.class_to_idx = class_to_idx
        self.samples = samples
        self.targets = [s[1] for s in samples]

    def _find_classes(self, dir):
        """
        Finds the class folders in a dataset.

        Args:
            dir (string): Root directory path.

        Returns:
            tuple: (classes, class_to_idx) where classes are relative to (dir), and class_to_idx is a dictionary.

        Ensures:
            No class is a subdirectory of another.
        """
        classes = [d.name for d in os.scandir(dir) if d.is_dir()]
        classes.sort()
        class_to_idx = {classes[i]: i for i in range(len(classes))}
        return classes, class_to_idx

    def random_attribute(self, fname, size, attributes):

        width, height = size
        sample_mask = np.zeros([width//2,height//2,3])
        sel_attributes = []

        for i in range (len(attributes)):

            if attributes[i] == "eyes":
                sel_attributes.append("l_eye")
                sel_attributes.append("r_eye")
                sel_attributes.append("l_brow")
                sel_attributes.append("r_brow")
            elif attributes[i] == "lip":
                sel_attributes.append("l_lip")
                sel_attributes.append("u_lip")
                sel_attributes.append("mouth")
            else:
                sel_attributes.append(attributes[i])

        for i in range (len(sel_attributes)):

            mask_file = fname+"_"+sel_attributes[i]+".png"
            mask_path = os.path.join(self.root_masks, mask_file)

            if os.path.isfile(mask_path):
                img = imageio.imread(mask_path)
                sample_mask = sample_mask + img

        return sample_mask

    def _find_masks(self, path, size):
        """
        Args:
            path (strg): Index
            size(tuple): Image size

        Returns:
            mask
        """
        fname = path[-9:-4]
        try:
            attributes = self.attributes.split(", ")
            mask_eyes = self.random_attribute(fname, size, [attributes[0]])
            mask_lip = self.random_attribute(fname, size, [attributes[1]])
            mask_nose = self.random_attribute(fname, size, [attributes[2]])
            mask_skin = self.random_attribute(fname, size, ["skin"])
        except:
            pass

        mask_eyes = Image.fromarray(np.uint8(mask_eyes))
        mask_lip = Image.fromarray(np.uint8(mask_lip))
        mask_nose = Image.fromarray(np.uint8(mask_nose))
        mask_skin = Image.fromarray(np.uint8(mask_skin))

        return mask_eyes, mask_lip, mask_nose, mask_skin

    def __getitem__(self, index):
        """
        Args:
            index (int): Index

        Returns:
            tuple: (sample, target) where target is class_index of the target class.
        """
        path, target = self.samples[index]
        sample = self.loader(path)

        mask_eyes, mask_lip, mask_nose, mask_skin = self._find_masks(path, sample.size)

        if self.training == True:
            if random.random() > 0.5:
                sample = transforms.functional.hflip(sample)
                mask_eyes = transforms.functional.hflip(mask_eyes)
                mask_lip = transforms.functional.hflip(mask_lip)
                mask_nose = transforms.functional.hflip(mask_nose)
                mask_skin = transforms.functional.hflip(mask_skin)

        if self.transform is not None:
            sample = self.transform(sample)
            mask_eyes = self.transform(mask_eyes)
            mask_lip = self.transform(mask_lip)
            mask_nose = self.transform(mask_nose)
            mask_skin = self.transform(mask_skin)

        if self.target_transform is not None:
            target = self.target_transform(target)

        sample_mask = torch.ones([4, mask_eyes.shape[1], mask_eyes.shape[2]])
        sample_mask[0] = mask_eyes[0]
        sample_mask[1] = mask_lip[0]
        sample_mask[2] = mask_nose[0]
        sample_mask[3] = mask_skin[0]

        return sample, target, sample_mask

    def __len__(self):
        return len(self.samples)

IMG_EXTENSIONS = ('.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm', '.tif', '.tiff', '.webp')

def pil_loader(path):
    # open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
    with open(path, 'rb') as f:
        img = Image.open(f)
        return img.convert('RGB')

def accimage_loader(path):
    import accimage
    try:
        return accimage.Image(path)
    except IOError:
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path)

def default_loader(path):
    from torchvision import get_image_backend
    if get_image_backend() == 'accimage':
        return accimage_loader(path)
    else:
        return pil_loader(path)

class CustomImageFolder(DatasetFolder):
    """A generic data loader where the images are arranged in this way: ::

        root/dog/xxx.png
        root/dog/xxy.png
        root/dog/xxz.png

        root/cat/123.png
        root/cat/nsdf3.png
        root/cat/asd932_.png

    Args:
        root (string): Root directory path.
        transform (callable, optional): A function/transform that  takes in an PIL image
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
        loader (callable, optional): A function to load an image given its path.
        is_valid_file (callable, optional): A function that takes path of an Image file
            and check if the file is a valid file (used to check of corrupt files)

     Attributes:
        classes (list): List of the class names sorted alphabetically.
        class_to_idx (dict): Dict with items (class_name, class_index).
        imgs (list): List of (image path, class_index) tuples
    """

    def __init__(self, root, root_masks, attributes, training, transform=None, target_transform=None,
                 loader=default_loader, is_valid_file=None):
        super(CustomImageFolder, self).__init__(root, root_masks, loader, attributes, training,
                                                IMG_EXTENSIONS if is_valid_file is None else None,
                                                transform=transform, target_transform=target_transform,
                                                is_valid_file=is_valid_file)
        self.imgs = self.samples


cards = input().split()
shuffles_count = int(input())

middle_len = len(cards)//2

for _ in range(shuffles_count):

    res = []
    for index in range(middle_len):
        first_card = cards[index]
        second_card = cards[index + middle_len]

        res.append(first_card)
        res.append(second_card)
    cards = res
print(res)


import matplotlib.pyplot as plt
import visuals
import switch_functions as switch
import looper
import malus_calc
import numpy as np

"""
These are the used functions. For instructions on how to use them navigate to the README.md

solution = switch.single_loop():

switch.activity_switch_emptyslot(seconds,timeout_amount,change_amount, solution)

switch.activity_switch(seconds,timeout_amount,change_amount, solution)

switch.student_switch_emptyslot(seconds,timeout_amount,change_amount, solution)

switch.student_switch(seconds,timeout_amount,change_amount, solution)

looper.loop_tuner(ac_ac,ac_emp,stud_stud,stud_emp, run_time)
"""

# This is the looper function, comment out if using a single switch
# change variables to your liking
seconds = 60
ac_ac = 25
ac_emp = 25
stud_stud = 75
stud_emp = 75
week_data,student_data,activity_list,total_changes,total_attempts, malus_list_changes, malus_list_tries = looper.loop_tuner(ac_ac,ac_emp,stud_stud,stud_emp, seconds)

# this is the single switch, comment out if using the looper
# Change the function with any of the single switch functions found at the top of this file
# change variables to your liking
"""
seconds = 30
timeout_amount = -1
change_amount = -1
solution = switch.single_loop()
malus_list_2,change_tries,succesfull_changes,malus_list = switch.student_switch(seconds,timeout_amount,change_amount, solution)
"""

# This will print the final maluscount of our solution aqquired from the looper of switch function.
print("final malus count: {}".format(malus_calc.malus_calc(student_data,activity_list)))

# this will print our schedule
visuals.plotter(week_data)

# this will plot the total changes and the total change attempts on the x axis and the malus points on the y
# this is a result of the looper function, comment this out if you are not using the looper function.
plt.plot(np.arange(total_changes), malus_list_changes)
plt.plot(np.arange(total_attempts), malus_list_tries)
plt.title(f"{seconds} seconds of the combined switches")
plt.xlabel("Amount of total switch attempts")
plt.ylabel("Maluspoints")
plt.show()

# this will plot the total changes and the total change attempts on the x axis and the malus points on the y
# this is a result of the single switich functions, comment this out if you are using the looper function.
"""
plt.plot(np.arange(succesfull_changes), malus_list)
plt.plot(np.arange(change_tries), malus_list_2)
plt.title(f"{seconds} seconds of the single switches")
plt.xlabel("Amount of total switch attempts")
plt.ylabel("Maluspoints")
plt.show()
"""


import pandas as pd

def simulate_retirement(df, savings, initial_withdrawal_rate,
                        adjustment_withdrawal_rate, retirement_length,
                        initial_equity_weight, equity_glide_adjustment,
                        success_rule=1):
    """A function to simulate the outcome of retirement portfolios and spending plans.

    The function uses the provided parameters to simulate how a simple retirement
    portfolio would perform starting from each month of available data.

    The portfolio is invested in the the S&P 500 and the 10-year treasury bond
    according to the initial_equity_weight parameter. The value of withdrawals
    is set according to initial_withdrawal_rate. Over time, withdrawals are adjusted
    for inflation and the equity weight is increased gradually toward 1 according to
    the equity_glide_adjustment parameter.

    adjustment_withdrawal_rate is the rate at which withdrawals can safely be
    adjusted upward (in addition to inflation adjustments) after retirement begins.
    i.e. If your spending falls below this rate relative to the portfolio value,
    you can safely adjust upward and reset the equity glide path.
    """

    portfolio_preservation = []
    portfolio_survival = []

    nominalyield = df.SPNominalYield.tolist()
    dividendyield = df.SPDividendYield.tolist()
    bondyield = df.TenYearBondReturn.tolist()
    inflation = df.Inflation.tolist()

    for retirement_year in range(0, len(df) - retirement_length + 1):
        balance = savings
        income = (initial_withdrawal_rate/12)*savings
        inflation_factor = 1.0
        equity_weight = initial_equity_weight
        for w, x, y, z in zip(nominalyield[retirement_year:(retirement_year + retirement_length - 1)], \
                              dividendyield[retirement_year:(retirement_year + retirement_length - 1)], \
                              bondyield[retirement_year:(retirement_year + retirement_length - 1)], \
                              inflation[retirement_year:(retirement_year + retirement_length - 1)]):
            income *= (1 + z)
            balance = balance*(equity_weight*(1 + w)*(1 + x) + (1 - equity_weight)*(1 + y)) - income
            inflation_factor *= (1 + z)
            if equity_weight < 1:
                equity_weight += min(equity_glide_adjustment, 1 - equity_weight)
            if income < (adjustment_withdrawal_rate)*balance/12:
                income = (adjustment_withdrawal_rate)*balance/12
                equity_weight = initial_equity_weight
        portfolio_preservation.append(balance > inflation_factor*savings)
        portfolio_survival.append(balance > 0)

    if success_rule == 0:
        return all(portfolio_survival)
    elif success_rule == 1:
        return all(portfolio_preservation)
    else:
        raise ValueError('Invalid success rule.')

def find_solutions(df, savings, retirement_length, initial_equity_weight,
                   equity_glide_adjustment, success_rule=1):
    """A function to find the maximum safe withdrawal & safe adjustment rates.

    The function finds the withdrawal rates (between 0% and 100%) at which the
    portfolio maintains a positive balance (success_rule = 0) or maintains the
    inflation adjusted original balance (success_rule = 1) for all given retirement dates.
    """

    rate_a = 0.0
    rate_b = 1.0
    while rate_b - rate_a > 0.00005:
        rate_c = (rate_a + rate_b)/2
        if simulate_retirement(df, savings, rate_c, 0, retirement_length,
                               initial_equity_weight, equity_glide_adjustment,
                               success_rule):
            rate_a = rate_c
        else:
            rate_b = rate_c
    safe_withdrawal_rate = int(rate_c*10000)/10000.0 # Round down to 4 dec places

    adjustment_rate_a = 0.0
    adjustment_rate_b = 1.0
    while adjustment_rate_b - adjustment_rate_a > 0.00005:
        adjustment_rate_c = (adjustment_rate_a + adjustment_rate_b)/2
        if simulate_retirement(df, savings, safe_withdrawal_rate, adjustment_rate_c,
                               retirement_length, initial_equity_weight,
                               equity_glide_adjustment, success_rule):
            adjustment_rate_a = adjustment_rate_c
        else:
            adjustment_rate_b = adjustment_rate_c
    safe_adjustment_rate = int(adjustment_rate_c*10000)/10000.0

    return safe_withdrawal_rate, safe_adjustment_rate

if __name__ == '__main__':
    df = pd.read_csv('historical_market_data.csv')
    solution_rate, solution_adjustment_rate = find_solutions(df, 100000, 720, 0.6, 0.005, 1)
    print(solution_rate)
    print(solution_adjustment_rate)


#Giải mã base64 --> ảnh
from base64 import b64decode

#print(b64decode('AQID'))
f = open('imagebase64.txt')
b64data = f.read()
f.close()

data = b64decode(b64data)
print(len(data))

f = open('out.png', 'wb')
f.write(data)
f.close()


import sys
import socket
import re
# you may use urllib to encode data appropriately
import urllib.parse

def help():
    print("httpclient.py [GET/POST] [URL]\n")

class HTTPResponse(object):
    def __init__(self, code=200, body=""):
        self.code = code
        self.body = body

class HTTPClient(object):
    #def get_host_port(self,url):

    def connect(self, host, port):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.connect((host, port))
        return None

    def get_code(self, data):
        lines = data.split('\r\n')
        line = lines[0].split(' ')
        code = 500
        if (len(line)>1):
            try:
                code = int(line[1])
            except:
                pass
        return code

    def get_headers(self,data):
        return None

    def get_body(self, data):
        lines = data.split('\r\n')
        write=False
        body=''
        for line in lines:
            if (line==''):
                write=True
                continue
            if (write):
                body += line

        return body

    def sendall(self, data):
        self.socket.sendall(data.encode('utf-8'))

    def close(self):
        self.socket.close()

    # read everything from the socket
    def recvall(self, sock):
        buffer = bytearray()
        done = False
        while not done:
            part = sock.recv(1024)
            if (part):
                buffer.extend(part)
            else:
                done = not part
        return buffer.decode('utf-8')

    def GET(self, url, args=None):
        code = 500

        parse = urllib.parse.urlparse(url)

        path = parse.path
        host = parse.hostname
        port = 80
        try:
            if (parse.port != None):
                port = parse.port
        except:
            pass
        if (path==''):
            path='/'

        self.connect(host,port)

        requestLine = "GET " + path + " HTTP/1.1\r\n"
        headers = "Host: {}\r\n\r\n".format(host)

        request = requestLine + headers

        self.sendall(request)
        response = self.recvall(self.socket)

        self.close()

        body = self.get_body(response)
        code = self.get_code(response)

        print(body)

        return HTTPResponse(code, body)

    def POST(self, url, args=None):
        parse = urllib.parse.urlparse(url)

        path = parse.path
        host = parse.hostname
        port = 80
        try:
            if (parse.port != None):
                port = parse.port
        except:
            pass
        if (path==''):
            path='/'

        self.connect(host,port)

        length = 0
        payload = ''
        if (args != None):
            for key, value in args.items():
                payload += key+'='+value+'&'
            payload = payload[:-1]
            length = len(payload.encode('utf-8'))

        requestLine = "POST " + path + " HTTP/1.1\r\n"
        headers = "Host: {}\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: {}\r\n\r\n".format(host,length)

        request = requestLine + headers + payload

        self.sendall(request)
        response = self.recvall(self.socket)

        self.close()

        body = self.get_body(response)
        code = self.get_code(response)

        print(body)

        return HTTPResponse(code, body)

    def command(self, url, command="GET", args=None):
        if (command == "POST"):
            return self.POST( url, args )
        else:
            return self.GET( url, args )

if __name__ == "__main__":
    client = HTTPClient()
    command = "GET"
    if (len(sys.argv) <= 1):
        help()
        sys.exit(1)
    elif (len(sys.argv) == 3):
        print(client.command( sys.argv[2], sys.argv[1] ))
    else:
        print(client.command( sys.argv[1] ))