import asyncio
import aiohttp
from aiohttp import web
import json

TOKEN = '1383386139:AAGWeMlF9BW26ZwUwnVuk2pQm6nOvUADxyw'
API_URL = 'https://api.telegram.org/bot%s/sendMessage' % TOKEN

async def handler(request):
    data = await request.json()
    headers = {
        'Content-Type': 'application/json'
    }
    message = {
        'chat_id': data['message']['chat']['id'],
        'text': data['message']['text']
    }
    async with aiohttp.ClientSession(loop=loop) as session:
        async with session.post(API_URL,
                                data=json.dumps(message),
                                headers=headers) as resp:
            try:
                assert resp.status == 200
            except:
                return web.Response(status=500)
    return web.Response(status=200)

async def init_app(loop):
    app = web.Application(loop=loop, middlewares=[])
    app.router.add_post('/api/v1', handler)
    return app

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    try:
        app = loop.run_until_complete(init_app(loop))
        web.run_app(app, host='127.0.0.1', port=23456)
    except Exception as e:
        print('Error create server: %r' % e)
    finally:
        pass
    loop.close()


from __future__ import absolute_import

#__all__ = []

import numpy as np
import matplotlib.pyplot as plt
#import sys
#import platform
#import datetime
from scipy import constants as con
#from scipy.special import kv
import urllib2 as url
#from bs4 import BeautifulSoup
import os
import tempfile
import shutil
from sunpy.time import parse_time

from scipy.integrate import quad
from scipy import integrate
from scipy import interpolate
from scipy.misc import derivative
from scipy import optimize

try:
    from .. fit_data import Fit_data
except ValueError:
    print("Do not import util directly.  Import heroes instead.")
    print("(This hassle is due to the relative location of fit_data.py)")

try:
    data_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)),"data","")
except AttributeError:
    data_dir = '/Users/schriste/Dropbox/python/heroes/util/data/'

_msis_atmosphere_file = None

# densities
# source; wolframalpha
density = {"air stp": 0.001204, "si": 2.33, "be": 1.848, "water": 1, "cadmium telluride": 6.2,
"cesium iodide": 4.51, "gallium arsenide": 5.31, "mercuric iodide": 6.36, "lead glass": 6.22}

'''The X-ray transmission data comes from NIST
	(http://www.nist.gov/pml/data/xraycoef/index.cfm)'''

def xray_transmission(path_length_m, energy_kev, material='air stp'):
	"""Provide the X-ray transmission (0 to 1) in given a path length in meters at
	a particular energy given in keV through a material with a constant density."""

	coefficients = mass_attenuation_coefficicent(energy_kev, material=material)
	transmission = np.exp(-coefficients * density_cgs.get(material) * path_length_m * 100.0)

	return transmission

def load_mass_attenuation_coefficients(material='air_dry_near_sea_level'):
	'''Load the mass attenuation coefficients (cm2/g) and mass energy-absorption coefficients (cm2/g)
	from the data files as a function of energy (MeV). The allowed materials are listed in density.'''

	filename = data_dir + 'XrayMassCoef_' + material.replace(' ', '_').capitalize() + '.txt'
	data = np.genfromtxt(filename, comments = ';', missing_values = ' ', skip_header = 8)

	return data

def mass_attenuation_coefficicent(energy_kev, material):
    """Returns the mass attenuation coefficient at an energy given in keV"""

    data = load_mass_attenuation_coefficients(material)

    # data is better behaved in log space
    data_energy_kev = np.log10(data[:,0]*1000)
    data_attenuation_coeff = np.log10(data[:,1])
    f = interpolate.interp1d(data_energy_kev, data_attenuation_coeff)

    return 10 ** f(np.log10(energy_kev))

def plot_mass_attenuation_coefficient(material='air_dry_near_sea_level'):
	'''Plot the mass the mass attenuation coefficients and mass energy-absorption
	coefficients for a named material. See load_mass_attenuation_coefficients definition
	for list of allowed materials.'''

	data = load_mass_attenuation_coefficients(material=material)

	energy_kev = data[:,0]
	mass_atten_coeff = data[:,1]
	mass_energy_atten_coeff = data[:,2]

	ax = plt.subplot(111)
	ax.set_xscale('log')
	ax.set_yscale('log')
	ax.set_xlabel('Energy [keV]')
	ax.set_title(material.replace('_', ' ').capitalize())
	ax.set_ylabel(r'Mass Attenuation Coefficient [cm$^2$/g]')

	ax.plot(energy_kev, mass_atten_coeff)
	ax.plot(energy_kev, mass_energy_atten_coeff)

	ax.legend((r'$\mu/\rho$', r'$\mu_{en}/\rho$'))

	plt.show()

def xray_absorption(energy_kev, thickness_um, material='si'):
	'''Calculate the xray absorption in a material with a thickess (given in microns).'''

	return 1-xray_transmission(energy_kev, thickness_um/1e6, material=material)

def detector_efficiency(energy_kev, thickness_um, material='si'):
	'''Calculate the detector quantum efficiency (in percent) at a given energy'''

	return xray_absorption(energy_kev, thickness_um, material=material)*100.0

def load_attenuation_length(material='si'):
	filename = data_dir + material + '_xray_atten_length.txt'
	data = np.genfromtxt(filename, comments = ';', missing_values = ' ', skip_header = 3)

	return data

def xyplot(x, y, ytitle = None, xtitle = None, title = None, log = None):

	fig = plt.figure()
	ax = fig.add_subplot(111)

	if log is not None:
		if log[0] == 1:
			ax.set_xscale('log')
		if log[1] == 1:
			ax.set_yscale('log')

	if ytitle is not None:
		ax.set_ylabel(ytitle)
	if xtitle is not None:
		ax.set_xlabel(xtitle)
	if title is not None:
		ax.set_title(title)

	ax.plot(x, y)
	# plt.show()
	return fig

def oplot(x, y, plt):

    ax = plt.gca()
    ax.plot(x, y)
    plt.show()

def thermal_bremsstrahlung_thin(energy_kev, kt):
    """This function calculates the optically thin continuum thermal bremsstrahlung
    photon flux incident on the Earth from an isothermal plasma on the Sun.
    Normalization is for an emission measure on the Sun of 1.e49 cm-3

    function brem_49,e,kt , verbose=verbose

if keyword_set(verbose) then print, 'Differential Bremsstrahlung '+$
  'spectrum at Earth for emission measure of 1.e49.'
;
kt0 =( kt(0) > 0.1) ;protect against vectors for kt
result = (1.e8/9.26) * float(acgaunt(12.3985/E, KT0/.08617)) *exp(-(E/KT0 < 50)) /E / KT0^.5

return, result(*)

    """

    # kt0 =( kt(0) > 0.1) ; protect against vectors for kt
    #result = (1.e8/9.26) * float(acgaunt(12.3985/E, KT0/.08617)) *exp(-(E/KT0 < 50)) /E / KT0^.5

    result = (1.e8/9.26) * gaunt_factor(energy_kev, kt) * 1 / (energy_kev * np.sqrt(kt)) * np.exp(- (energy_kev / kt))
    return result

def rgaunt_factor(energy_kev, kt, Z=1):
    """Analytic fitting formula for the non-relativistivic gaunt factor

    Source
    ======
    Itoh et al. 2000, ApJSS, 128, 125
    """

    k = con.physical_constants.get('Boltzmann constant')[0]
    electron_volt = con.physical_constants.get('electron volt')[0]

    # units
    temp_K_to_kev_conversion = k / electron_volt / 1000

    data_gaunt = np.genfromtxt(data_dir + 'itoh.txt')
    coefficients = data_gaunt[Z-1].reshape(11,11)

    u = energy_kev / kt
    temperature_K = kt / temp_K_to_kev_conversion

    gaunt_factor = 0
    U = (np.log10(u) + 1.5) / 2.5
    t = (np.log10(temperature_K) - 7.25) / 1.25

    for j in range(11):
        for i in range(11):

            gaunt_factor += coefficients[i,j] * (t ** i) * (U ** j)

    return gaunt_factor

def nrgaunt_factor(energy_kev, kt, Z=1):
    """Analytic fitting formula for the non-relativistivic gaunt factor

    Source
    ======
    Itoh et al. 2000, ApJSS, 128, 125
    """

    k = con.physical_constants.get('Boltzmann constant')[0]
    electron_volt = con.physical_constants.get('electron volt')[0]

    # units
    temp_K_to_kev_conversion = k / electron_volt / 1000

    coefficients = np.genfromtxt(data_dir + 'itohnr.txt', delimiter = ",")

    u = energy_kev / kt

    temperature_K = kt / temp_K_to_kev_conversion
    print(temperature_K)

    U = (np.log10(u) + 1.5) / 2.5
    g2 = Z ** 2 * 1.579e5 / temperature_K
    G = (np.log10(g2) + 0.5) / 2.5

    gaunt_factor = 0

    for j in range(11):
        for i in range(11):
            gaunt_factor += coefficients[i,j] * (G ** i) * (U ** j)

    return gaunt_factor

def effective_area(energy_kev):
    """Returns the HEROES effective area in cm^2 at a particular energy given in keV."""

    data_energy_kev = np.arange(20,80,10)
    data_effective_area = np.array([80,75,60,40,15,5])
    f = interpolate.interp1d(data_energy_kev, data_effective_area)

    return f(energy_kev)

def effective_area2_fitdata():

    number_of_modules = 8
    data = np.genfromtxt(data_dir+'heroes_effective_area_0am5am.txt', comments=';', names=['x','y1','y2'])
    result = Fit_data(data['x'], number_of_modules * data['y1'], 'Energy', 'Effective Area', 'HEROES', 'keV', 'cm$^{2}$', log = [0,0])

    return result

def effective_area2(energy_kev):

    fitdata = effective_area2_fitdata()
    return fitdata.func(energy_kev)

def detector_background(energy_kev):

    data_energy_kev = np.arange(20,80,10)
    data_det_background = np.array([2,2,2.5,3,3,3]) * 0.001
    f = interpolate.interp1d(data_energy_kev, data_det_background)

    return f(energy_kev)

def atmo_transmission(energy_kev):

    data_energy_kev = np.arange(20,80,10)
    data_atmo_transmission = np.array([0.26, 2.0, 3.2, 3.7, 4.2, 4.5]) * 0.1
    f = interpolate.interp1d(data_energy_kev, data_atmo_transmission)

    return f(energy_kev)

def detector_efficiency(energy_kev):

    data_energy_kev = np.arange(20,80,10)
    data_detector_efficiency = np.array([9.8, 9.2, 9.9, 9.7, 8.9, 7.7]) * 0.1

    f = interpolate.interp1d(data_energy_kev, data_detector_efficiency)

    return f(energy_kev)

def sensitivity(background_counts, flux_to_counts_conversion, statistical_significance=5):
    """Calculates the sensitivity of an instrument using the following formula

        K = signal / sqrt(signal + background)

    where K is the significance (in sigma). This equation solves to

        Sensitivity Flux limit = (K^2 + sqrt(K^4 - 4*background)) / 2 * flux_to_counts_conversion

    """

    result = 1/(2 * flux_to_counts_conversion) * statistical_significance ** 2 + np.sqrt( statistical_significance ** 4 - 4 * background_counts )

    return result

def sensitivity(integration_time, de = 5, statistical_sig = 5):
    """Returns the HEROES sensitivity at a particular energy given in keV.
    de is the width of the energy interval in keV"""

    energy_kev = np.arange(20,80,10)
    det_eff = detector_background(energy_kev)
    det_background = detector_background(energy_kev)
    eff_area = effective_area(energy_kev)

    det_efficiency = detector_efficiency(energy_kev)
    transmission = atmo_transmission(energy_kev)
    background_area = 8 * 0.04
    fraction_flux = 0.8

    a = statistical_sig ** 2 + np.sqrt(statistical_sig ** 4 + 4*statistical_sig ** 2 *
        det_background * de * background_area * integration_time)

    b = 2 * eff_area * de * integration_time * transmission * det_eff * fraction_flux

    return  a/b

def get_msis_atmosphere_density(latitude=55, longitude=45, reload=False, date = '2000/01/01 01:00:00'):
    '''Downloads the MSIS atmospheric model from the web at a given longitude, latitude
    and returns the density (g/cm^3) as a function of height (km). The data is saved
    in a temporary file and further calls use this to save time'''

    global _msis_atmosphere_file
    t = parse_time(date)

    vars = [5,11] # 5 is height, 11 is density g/cm^3

    if (_msis_atmosphere_file == None) or (reload is True):
        temp = tempfile.NamedTemporaryFile(delete=False)
        _msis_atmosphere_file = temp.name

        addr = 'http://omniweb.gsfc.nasa.gov/cgi/vitmo/vitmo_model.cgi'
        data = u'model=msis&year=' + str(t.year) + '&month=' + str(t.month).zfill(2)
        data += '&day=' + str(t.day).zfill(2) + '&time_flag=0&hour='
        data += str(t.hour).zfill(2) + '&geo_flag=0.&latitude'
        data += str(latitude) + '&longitude=' + str(longitude)
        data += u'&height=100.&profile=1&start=0.&stop=1000.&step=20.&f10_7=&f10_7_3=&ap=&format=0&'
        data += 'vars=0' + str(vars[0]) + '&vars=0' + str(vars[1])
        a = url.Request(addr, data)
        req = url.urlopen(a)
        with open(temp.name, 'wb') as fp:
            shutil.copyfileobj(req, fp)

    data = np.genfromtxt(_msis_atmosphere_file, skip_header = 18, skip_footer = 16, dtype='f8,f8', names=['x','y'])

    return data

def atmosphere_density_fitdata(date = '2000/01/01 01:00:00', latitude=55, longitude=45):

    data = get_msis_atmosphere_density(date=date, latitude=latitude, longitude=longitude)
    f = Fit_data(1e5 * data['x'], data['y'], 'Height', 'density', 'MSIS', 'cm', 'g cm$^{-3}$', log = [0,1])

    return f

def atmosphere_density(height_km, date = '2000/01/01 01:00:00', latitude=55, longitude=45):
    '''
    Returns the atmospheric density (in g/cm^-3) at a specific height (given in cm)

    Source
    ------
    http://omniweb.gsfc.nasa.gov/vitmo/msis_vitmo.html
    '''
    fitdata = atmosphere_density_fitdata(date = date, latitude = latitude, longitude = longitude)
    return fitdata.func(height_km)

def atmosphere_mass(height_km):
    '''Returns the amount of mass in a 1 sq cm column of air above a height given in km'''

    mass_flux = quad(atmosphere_density_fitdata().func, height_km * 1e5, 1e8)[0]
    return mass_flux

def xray_transmission_in_atmosphere(energy_kev, height_km, view_angle=90, data = None):
    """Find the total mass of atmosphere above a height given in km"""

    co = mass_attenuation_coefficicent(energy_kev, material='air stp')
    mass_flux = atmosphere_mass(height_km)
    return np.exp(-co * mass_flux / np.sin(np.deg2rad(view_angle)) )

def foxsi_effective_area_fitdata():

    data = np.genfromtxt(data_dir + 'foxsi_effective_area.txt', skip_header = 1, delimiter = ',', dtype='f8,f8,f8', names=['x','y1','y2'])
    f = Fit_data(data['x'], data['y1'], 'Energy', 'Effective Area', 'FOXSI', 'keV', 'cm$^{2}$', log = [0,0])

    return f

def plot_foxsi_effarea_compare():

    data = np.genfromtxt(data_dir + 'foxsi_effective_area.txt', skip_header = 1, delimiter = ',')

    energy_kev = data[:,0]
    foxsi1_cm2 = data[:,1]
    foxsi2_cm2 = data[:,2]

    ax = plt.subplot(111)
    #ax.set_xscale('log')
    #ax.set_yscale('log')
    ax.set_xlabel('Energy [keV]')
    #ax.set_title(material.replace('_', ' ').capitalize())
    ax.set_ylabel(r'Effective Area [cm$^2$]')

    ax.plot(energy_kev, foxsi1_cm2, color = 'blue')
    ax.plot(energy_kev, foxsi2_cm2, color = 'red')

    ax.legend((r'FOXSI-1', r'FOXSI-2'))

    plt.show()

def heroes_effective_area_fit():

    data13 = np.genfromtxt(os.path.join(data_dir, 'aeff_13shells_sky.dat'),
                           skip_header = 2)
    data14 = np.genfromtxt(os.path.join(data_dir, 'aeff_14shells_sky.dat'),
                                        skip_header = 2)

    energy = data13[:,0] # keV, axis assumed to be the same in the two files
    #energy = np.arange(17.50,80.25,0.25)
    theta = np.arange(0,13) # arcmin

    #Total effective area is 3 13-shell modules and 5 14-shell modules
    #Count verified on 2012 Oct 22
    area = 3*data13[:,1:]+5*data14[:,1:]

    #y, x = np.meshgrid(theta, energy)
    #f2d = interpolate.interp2d(x, y, area)
    f2d = interpolate.RectBivariateSpline(energy, theta, area)

    return f2d

def heroes_effective_area_tophat(energy_range=(20,30), radius=9.5):
    """
    Calculates the average effective area for a tophat exposure

    energy_range is in keV
    radius of tophat is in arcmin
    """
    f2d = heroes_effective_area_fit()
    area = integrate.dblquad(lambda e,r: f2d(e,r)*2*np.pi*r,
                             0, radius,
                             lambda r:energy_range[0], lambda r: energy_range[1])[0]
    norm_area = np.pi*radius**2
    area /= norm_area*(energy_range[1]-energy_range[0])
    return area

def heroes_effective_area_gaussian(energy_range=(20,30), fwhm=3, radius=9.5,
                                   offaxis=0):
    """
    Calculates the average effective area for a Gaussian exposure.  Off-axis
    exposures take *much* longer to calculate, by >~2 orders of magnitude in
    time!

    energy_range is in keV
    fwhm of source is in arcmin
    radius of integration area is in arcmin
    offaxis is in arcmin
    """
    f2d = heroes_effective_area_fit()
    sigma = fwhm/2.355
    if offaxis == 0:
        area = integrate.dblquad(lambda e,r: f2d(e,r)*r*np.exp(-(r/sigma)**2/2)/sigma**2,
                                 0, radius,
                                 lambda r:energy_range[0], lambda r: energy_range[1])[0]
    else:
        r2 = lambda x,y: x**2+y**2
#        area = integrate.dblquad(lambda y,x: np.exp(-(r(x-offaxis,y)/sigma)**2/2)/(2*np.pi*sigma**2),
#                                 -radius, radius,
#                                 lambda x: -np.sqrt(radius**2-x**2), lambda x: np.sqrt(radius**2-x**2))[0]
        area = integrate.tplquad(lambda e,y,x: f2d(e,np.sqrt(r2(x,y)))*np.exp(-r2(x-offaxis,y)/sigma**2/2)/(2*np.pi*sigma**2),
                                 -radius, radius,
                                 lambda x: -np.sqrt(radius**2-x**2), lambda x: np.sqrt(radius**2-x**2),
                                 lambda x,y: energy_range[0], lambda x,y: energy_range[1])[0]
    norm_area = 1.
    area /= norm_area*(energy_range[1]-energy_range[0])
    return area

def heroes_effective_area_actual(energy_range=(20,30), actual='grs1915',
                                 radius=9.5):
    """
    Calculates the average effective area using actual HERO pointing data

    Currently fits 'grs1915' and 'all' with a quartic polynomial

    Currently performs a linear spline interpolation for 'cena'

    energy_range is in keV

    actual can be 'grs1915', 'cena', or 'all'

    radius of integration area is in arcmin
    """
    f2d = heroes_effective_area_fit()

    if actual in ['grs1915','all']:
        data = np.genfromtxt(os.path.join(data_dir,'hero2011_pointing.txt'),
                             skip_header = 1, names=['arcmin','grs1915','all'])

    # Incredibly slow, either due to Fit_data or due to derivative
    #    f = Fit_data(data['arcmin'], data[actual], 'Offset', 'Cumulative fraction',
    #                 'HERO 2011, ' + actual, 'arcmin', '', log = [0,1])
    #
    #    area = dblquad(lambda e,r: f2d(e,r)*derivative(f.func,r,dx=0.25),
    #                   0, radius,
    #                   lambda e:energy_range[0], lambda e: energy_range[1])[0]

        # Instead, fit with a fourth-degree polynomial and analytically differentiate
        # GRS1915 fits with a negative intercept, so this process (which effectively
        #   zeroes out the intercept) will overestimate the exposure in the range
        #   of [0,0.25] arcmin
        # For 'all', the reverse is true (the intercept is positive)
        p = np.polyfit(data['arcmin'],data[actual],4)
        dp = p[0:4]*[4,3,2,1] # Hopefully this is never negative...

        func = np.poly1d(dp)

    elif actual in ['cena']:
        data = np.genfromtxt(os.path.join(data_dir,'hero2011_pointing_cena.txt'),
                             skip_header = 0, names=['arcmin','cena','cena_diff'])
        data['cena_diff'] /= 0.25

#        f = Fit_data(data['arcmin'],data['cena_diff'],
#                            'Offset','Differential exposure',
#                            'Title','arcmin','',log=[0,0])
#
#        func = f.func

        func = interpolate.interp1d(data['arcmin'], data['cena_diff'],
                                    kind='linear',
                                    bounds_error=False, fill_value=0)

    else:
        raise ValueError("Invalid target for actual pointing data")

    area = integrate.dblquad(lambda e,r: f2d(e,r)*func(r),
                             0, radius,
                             lambda r:energy_range[0], lambda r: energy_range[1])[0]

    norm_area = 1.
    area /= norm_area*(energy_range[1]-energy_range[0])
    return area

def str2func(function):
    if isinstance(function, str):
        if function.lower() == 'gaussian':
            # p[0] is normalization
            # p[1] is mean (first raw moment)
            # p[2] is sigma (square root of second central moment)
            f = lambda p, x: p[0]/np.sqrt(2.*np.pi)/p[2]*np.exp(-((x-p[1])/p[2])**2/2.)
        else:
            raise ValueError
    return f

def fitfunc(x, y, function, initial, free=None, yerr=None, **kwargs):
    """Wrapper to scipy.optimize.leastsq to fit data to an arbitrary function."""
    f = str2func(function) if isinstance(function, str) else function
    if free is None: free = np.ones(np.shape(initial))
    if yerr is None: yerr = np.ones(np.shape(y))

    errfunc = lambda p, xp, yp, yerrp: (yp-f(p*free+initial*np.logical_not(free), xp))/yerrp

    return optimize.leastsq(errfunc, initial, args=(x, y, yerr), **kwargs)

def heroes_atmospheric_attenuation(energy_range = (20, 30),
                                   altitude = (40, 40.1),
                                   elevation = (45, 45.1)):
    """First attempt, not fully tested and *very* slow"""
    f = lambda en, al, el: xray_transmission_in_atmosphere(en, al, view_angle=el)
    attenuation = integrate.tplquad(f, elevation[0], elevation[1],
                                    lambda el: altitude[0], lambda el: altitude[1],
                                    lambda el, al: energy_range[0], lambda el, al: energy_range[1])[0]
    attenuation /= energy_range[1]-energy_range[0]
    attenuation /= altitude[1]-altitude[0]
    attenuation /= elevation[1]-elevation[0]

    return attenuation


# coding: utf-8
# Copyright (c) 2018-2019, Taku MURAKAMI. All rights reserved.
# Distributed under the terms of the BSD 3-clause License.

import logging
import numpy
import pymatgen
from pymatgen.io.vasp.inputs import Poscar

"""
Model analyser.
"""

logger = logging.getLogger(__name__)

class ModelAnalyser(object):
    """
    Model analyser.

    Arguments
    ---------
    structs: dict
        dict of analysed models, which format will be specified by "fmt".
    struct_stable: (fmt)
        Stable structure to analyse model, which format will be specified by "fmt".
    fmt: str
        structure format, default is pymatgen.Structure.

    Parameters
    ----------
    structs: dict
        dict of analysed models, format with pymatgen.Structure.
    struct_stable: pymatgen.Structure (or None)
        Stable structure to analyse model.
    """

    def __init__(self, structs, struct_stable=None, fmt="Structure"):
        if fmt == "Structure":
            self.structs = structs
            self.struct_stable = struct_stable
        else:
            self.structs = {}
            for struct_name, struct in structs.items():
                self.structs[struct_name] = pymatgen.Structure.from_str(
                    open(struct).read(), fmt=fmt
                )
            if struct_stable is not None:
                self.struct_stable = pymatgen.Structure.from_str(
                    open(struct_stable).read, fmt=fmt
                )
            else:
                self.struct_stable = struct_stable

    def calc_euclid_metric(self):
        """
        Analyse the metrics of models by calculationg euclidian distance.
        """
        metrics = {}
        for struct_name, struct in self.structs.items():
            metric = 0
            for i in range(3):
                metric += numpy.linalg.norm(
                    struct.lattice.matrix[i] - self.struct_stable.lattice.matrix[i]
                )
            for j in range(struct.num_sites):
                metric += numpy.linalg.norm(
                    numpy.array([struct.sites[j].a,
                                 struct.sites[j].b,
                                 struct.sites[j].c])
                    - numpy.array([self.struct_stable.sites[j].a,
                                   self.struct_stable.sites[j].b,
                                   self.struct_stable.sites[j].c])
                    )
            metrics[struct_name] = metric
        return metrics


####################################################################
# Skeleton for Multi Testing Selenium tests on Sauce Labs
####################################################################

###################################################################
# Imports that are good to use
###################################################################
from selenium import webdriver
from time import sleep
import multiprocessing
import os
import urllib3
import json
import random

###################################################################
# Selenium with Python doesn't like using HTTPS correctly
# and displays a warning that it uses Unverified HTTPS request
# The following disables that warning to clear the clutter
# But I should find a way to do the proper requests
###################################################################
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

###################################################################
# Select Data Center
# Set region to 'US' or 'EU'
# Test will default to 'US' if left blank or set to any other than 'US' or 'EU'
###################################################################
region = 'US'

###################################################################
# This makes the functions below execute 'run' amount of times
###################################################################
run = 2

###################################################################
# Declare as a function in order to do multiple runs
###################################################################
def run_sauce_test():
    ###################################################################
    # Pull a random Pokemon name to use as the test name
    ###################################################################
    pokemon_names_url = urllib3.PoolManager().request('GET', 'https://raw.githubusercontent.com/sindresorhus/pokemon/master/data/en.json')
    pokemon_names = json.loads(pokemon_names_url.data.decode('utf-8'))
    random_pokemon = random.choice(pokemon_names)

    ###################################################################
    # Common parameters (desired capabilities)
    ###################################################################
    sauceParameters = {
        'tags':['Case', 'NUM',],
        'platform': 'Windows 10',
        'browserName': 'chrome',
        # The following are not required
        'name': random_pokemon,
        'screenResolution':'1920x1080',
        # 'version': 'latest',
        # 'seleniumVersion': '3.141.59',

        # Sauce Specific Options
        # 'extendedDebugging': 'true',
        # 'capturePerformance': 'true',
        # 'idleTimeout': 180,
        # 'commandTimeout': 600,
        # 'prerun':{
        #     'executable': 'https://raw.githubusercontent.com/phillsauce/saucelabs-import-files/master/WinDownloadFiles.bat',
        #     'args': ['--silent'],
        #     'timeout': 500,
        #     'background': 'false',
        # },

        # Browser Specific Options
        # 'chromeOptions':{
        #     'mobileEmulation':{'deviceName':'iPhone X'},
        #     'prefs': {
        #         'profile': {
        #             'password_manager_enabled': 'false',
        #             },
        #             'credentials_enable_service': 'false',
        #         },
        #     'args': ['test-type', 'disable-infobars'],
        # },

        # 'moz:firefoxOptions':{
        #     'log': {'level': 'trace'},
        # },
    }
    # This concatenates the tags key above to add the build parameter
    sauceParameters.update({'build': '-'.join(sauceParameters.get('tags'))})

    ###################################################################
    # Connect to Sauce Labs
    ###################################################################
    try:
        region
    except NameError:
        region = 'US'

    if region != 'EU':
        print('You are using the US data center')
        driver = webdriver.Remote(
            command_executor='https://'+os.environ['SAUCE_USERNAME']+':'+os.environ['SAUCE_ACCESS_KEY']+'@ondemand.saucelabs.com:443/wd/hub',
            desired_capabilities=sauceParameters)
    elif region == 'EU':
        print ('You are using the EU data center')
        driver = webdriver.Remote(
            command_executor='https://'+os.environ['SAUCE_USERNAME']+':'+os.environ['SAUCE_ACCESS_KEY']+'@ondemand.eu-central-1.saucelabs.com:443/wd/hub',
            desired_capabilities=sauceParameters)

    ###################################################################
    # Test logic goes here
    ###################################################################
    # Navigating to a website
    driver.get('https://www.google.com')

    # Finding an element
    interact = driver.find_element_by_name('q')

    # Using the selected element
    interact.send_keys('chupacabra')
    interact.submit()
    # interact.click()

    # Saving an extra screenshot
    # driver.save_screenshot('screenshot.png')

    # Using Action chains
    # ActionChains(driver).move_to_element(interact).perform()

    # Sauce Labs specific executors
    # driver.execute_script('sauce: break')
    # driver.execute_script('sauce:context=Notes here')

    # Setting the job status to passed
    driver.execute_script('sauce:job-result=passed')

    # Ending the test session
    driver.quit()

###################################################################
# This is the command to use multiprocessing to run the desired
# amount of times
###################################################################
if __name__ == '__main__':
    jobs = [] # Array for the jobs
    for i in range(run): # Run the amount of times set above
        jobRun = multiprocessing.Process(target=run_sauce_test) # Define what function to run multiple times.
        jobs.append(jobRun) # Add to the array.
        jobRun.start() # Start the functions.
        # print('this is the run for: '+ str(i))


import logging
from datetime import datetime

from gym.common.tool import Tool

logger = logging.getLogger(__name__)

class Prober(Tool):
    """A prober is a Tool that must implement the probe
    function.

    Arguments:
        Tool {class} -- Establishes the core behavior of a Prober that
        is defined by the Tool.main function
    """

    def __init__(self, id, name, parameters, metrics):
        Tool.__init__(self, id, name, parameters, metrics)
        self._type = "prober"

    def probe(self, settings):
        opts = settings.get("opts")
        timeout = settings.get("timeout", None)

        cmd = [self._command, *opts]
        self._call = " ".join(cmd)

        results = self._processor.start_process(self._call, timeout)
        return results


from flask import redirect, render_template, request, Flask
from werkzeug.exceptions import BadRequest, NotFound
from flask.ext.sqlalchemy import SQLAlchemy
from utils import shorten_url, valid_url

# Initialize Flask application
app = Flask(__name__, template_folder='views')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///urls.db'

# Create the sqlalchemy object
db = SQLAlchemy(app)

from models import *

@app.route("/")
def home():
    """Render the home page."""
    return render_template('home.html')

@app.route("/shorten")
def shorten():
    """ Return a shortened version of the original url """
    original_url = request.args.get('url')

    if original_url.startswith('www'):
        original_url = '{}{}'.format('https://', original_url)

    if not original_url:
        raise BadRequest()
    elif not valid_url(original_url):
        return render_template('error.html')

    existing_url = db.session.query(Url).filter(Url.original == original_url).first()

    if existing_url:
        # get shortened from db
        shortened_url = existing_url.shortened
        hits = existing_url.hits

    else:
        shortened_url = shorten_url(original_url)
        hits = 0
        # add to db
        db.session.add(Url(original_url, shortened_url))
        db.session.commit()

    # Render the result page
    short_url = "http://{}/{}".format(request.host, shortened_url)
    return render_template('shortened.html', short_url=short_url, hits=hits)

@app.route('/<path:path>')
def retrieve_original_url(path=''):
    """ Find the original URL and redirect the user to it """
    existing_url = db.session.query(Url).filter(Url.shortened == path).first()

    if not existing_url:
        raise NotFound()

    existing_url.hits += 1
    db.session.commit()
    return redirect(existing_url.original)

@app.route('/report')
def report():
    """ Generate top 10 most popular URLs report """
    all_rows = db.session.query(Url).order_by(Url.hits.desc()).limit(10).all()
    if not all_rows:
        results = False
    else:
        results = True
    return render_template('report.html', rows=all_rows, results=results)

if __name__ == "__main__":
    app.run(debug=True)


from flask import Flask,render_template
app = Flask(__name__)

@app.route('/')
def index():
    return "Hello World"
@app.route("/bmi/<int:weight>/<int:height>")
def bmi(weight,height):
    bmi = weight/((height/100)**2)
    if bmi < 16:
        return "You are severely underweight" + " and your BMI index is: " + str(round(bmi,1))
    elif bmi >= 16 and bmi <18.5:
        return "You are underweight" + " and your BMI index is: " + str(round(bmi,1))
    elif bmi >= 18.5 and bmi < 25:
        return "You are normal" + " and your BMI index is: " + str(round(bmi,1))
    elif bmi >= 25 and bmi <30:
        return "You are overweight" + " and your BMI index is: " + str(round(bmi,1))
    else:
        return "You are obese" + " and your BMI index is: " + str(round(bmi,1))

if __name__ == '__main__':
  app.run(debug=True)


from django.db import models

from common.models import Location
from customer.models import Customer
from driver.models import Driver
from common.models import AuditLog
from vehicle.models import Vehicle

class Ride(AuditLog):
    class Status(models.IntegerChoices):
        PENDING = 0
        APPROVED = 1
        REJECTED = 2
        START = 3
        PARTIAL = 4
        EXPIRED = 6

    driver = models.ForeignKey(Driver, null=True, on_delete=models.CASCADE)
    vehicle = models.ForeignKey(Vehicle, null=True, on_delete=models.CASCADE)
    sharable = models.BooleanField()
    status = models.IntegerField(choices=Status.choices, default=Status.PENDING)

class Booking(AuditLog):
    class Status(models.IntegerChoices):
        PENDING = 0
        APPROVED = 1
        REJECTED = 2
        START = 3
        PARTIAL = 4
        COMPLETED = 5
        EXPIRED = 6

    start_location = models.ForeignKey(
        Location,
        related_name='booking_start_locations',
        null=True, on_delete=models.SET_NULL
    )
    end_location = models.ForeignKey(
        Location,
        related_name='booking_end_locations',
        null=True,
        on_delete=models.SET_NULL
    )
    vehicle_arrival_time = models.DateTimeField(null=True)
    start_time = models.DateTimeField(null=True)
    end_time = models.DateTimeField(null=True)
    expiry_time = models.DateTimeField(null=True)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    status = models.IntegerField(choices=Status.choices, default=Status.PENDING)


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('quotetron', '0007_auto_20141229_1425'),
    ]

    operations = [
        migrations.AlterField(
            model_name='quote',
            name='updater_id',
            field=models.PositiveIntegerField(editable=False),
            preserve_default=True,
        ),
        migrations.AlterField(
            model_name='quote',
            name='updater_type',
            field=models.ForeignKey(to='contenttypes.ContentType', editable=False),
            preserve_default=True,
        ),
    ]


from typing import List, Union

from django.apps import apps as APPS
from django.apps.config import AppConfig
from django.conf import settings

from introspection.model import ModelRepresentation

class AppInspector:
    """
    Inspect an app
    """

    app_config: AppConfig
    models: List[ModelRepresentation] = []

    def __init__(self, name: str) -> None:
        """
        Create an instance from an app name
        """
        app_config: Union[AppConfig, None] = None
        for appname in settings.INSTALLED_APPS:
            if appname == name:
                app_config = APPS.get_app_config(appname)
                break
        if app_config is not None:
            self.app_config = app_config
            return
        raise ModuleNotFoundError(f"App {name} was not found in settings")

    @property
    def name(self) -> str:
        """
        Get the app name
        """
        return self.app_config.name

    def get_models(self) -> None:
        """
        Get the app models
        """
        models_type = self.app_config.get_models()
        for model in models_type:
            self.models.append(ModelRepresentation.from_model_type(model))

    """def _convert_appname(self, appname: str) -> str:
        ""
        Remove the dots from an app name
        ""
        name = appname
        if "." in appname:
            name = appname.split(".")[-1]
        return name"""


import os

from random import randint
from PIL import Image, ImageDraw, ImageFont, ImageFilter
from io import BytesIO

current_path = os.path.dirname(os.path.abspath(__file__))

class RandomMan:
    def __init__(self, upper=True, color1=(64, 255), color2=(32, 127)):
        self.rndCharRange = (65, 90) if upper else (97, 122)
        self.rndColor1Range = color1
        self.rndColor2Range = color2

    def _get_randint(self, num=0, *args): return [randint(*args) for _ in range(num)]

    def rndChar(self, num=1): return [chr(*self._get_randint(1, *self.rndCharRange)) for _ in range(num)]

    def rndInt(self, num=1): return [randint(0, 9) for _ in range(num)]

    def rndColor1(self, num=3): return tuple(self._get_randint(num, *self.rndColor1Range))

    def rndColor2(self, num=3): return tuple(self._get_randint(num, *self.rndColor2Range))

class IMG:
    def __init__(self, mode='RGB', size=(240, 60), font_size=36, font_num=4,
                 fontSyle_path=os.path.join(current_path, 'Arial.ttf')):
        self.img = Image.new(mode, size, (255, 255, 255))
        self.width, self.height = size
        self.mode = mode
        try:
            self.font = ImageFont.truetype('Arial.ttf', font_size)
        except:
            self.font = ImageFont.truetype(fontSyle_path, font_size)
        self.draw = ImageDraw.Draw(self.img)
        self.font_num = font_num

class IMGFactory:
    def __init__(self, IMG, RandomMan, blur):
        self.img = IMG
        self.randomMan = RandomMan
        self.blur = blur
        self.res_img = None

    @classmethod
    def from_ir(cls, image, randomMan, blur=True):
        return cls(image, randomMan, blur)

    def _fill_pixel(self):
        for x in range(self.img.width):
            for y in range(self.img.height):
                self.img.draw.point((x, y), fill=self.randomMan.rndColor1())

    def _fill_font(self):
        lis = []
        _width = self.img.width // self.img.font_num
        for t in range(self.img.font_num):
            font = self.randomMan.rndChar()[0]
            lis.append(font)
            self.img.draw.text((_width * t + _width // 4, self.img.height // 4), font,
                               font=self.img.font, fill=self.randomMan.rndColor2())
        self.res_img = ''.join(lis)

    def _blur(self):
        if self.blur:
            self.img.img = self.img.img.filter(ImageFilter.BLUR)

    def save(self, filename='test.jpg', mode='jpeg', byte=False):
        if byte:
            filename = BytesIO()
        else:
            filename = os.path.join(current_path, filename)
        self.img.img.save(filename, mode)
        if byte:
            filename = filename.getvalue()
        return filename

    def create_picture(self, filename='test.jpg', mode='jpeg', byte=False):
        self._fill_pixel()
        self._fill_font()
        # self._blur()
        return self.res_img, self.save(filename, mode, byte)

def get_random_pic(font_num=4, byte=True):
    rm = RandomMan()
    img = IMG(font_num=font_num)
    handle = IMGFactory.from_ir(img, rm)
    return handle.create_picture(byte=byte)

if __name__ == '__main__':
    rm = RandomMan()
    img = IMG(font_num=4)
    handle = IMGFactory.from_ir(img, rm)
    pic = handle.create_picture(byte=False)


import py_midicsv, csv
import glob
import numpy as np
from tqdm import trange

mididir = 'midis/*/*/Chord Progressions/*/*.mid'
midis = glob.glob(mididir)

def makesequences(file):
	csvarr = [(s.split(",")) for s in py_midicsv.midi_to_csv(file)]
	final = []
	last_tick = 0
	note = np.zeros(101)

	for i in range(len(csvarr)):
		line = csvarr[i]
		time = int(line[1])

		if line[2] == " Note_on_c":
			if time > last_tick + 400:
				last_tick = time
				final.append(note)
				note = np.zeros(101)
			note[int(line[4].strip())] = 1
	final.append(note)
	eof = np.zeros(101)
	eof[100] = 1
	final.append(eof)
	return final

def getsequences():
	return [makesequences(midi) for midi in midis]

def make_train(sequence, X_train, y_train):
	TRAINING_LENGTH = 2
	chords_length = len(sequence)
	sequence = sequence * 2

	for i in range(1, chords_length):
		x = sequence[0:i]
		y = sequence[i]
		X_train.append(x)
		y_train.append(y)

def get_train():
	X_train = []
	y_train = []
	sequences = getsequences()
	print("Making training samples:")
	for i in trange(len(sequences)):
		make_train(sequences[i], X_train, y_train)

	return np.array(X_train), np.array(y_train)


from django import forms
from django.utils import timezone
from django.forms import ValidationError

from .models import Resume, Career, Education, Award, Link

class ResumeForm(forms.ModelForm):
    class Meta:
        model = Resume
        fields = '__all__'
        exclude = ('user',)
        widgets = {
            'phone_number': forms.TextInput(attrs={
                'placeholder': 'Phone number ex) 010-0000-0000'
            }),
            'about_me': forms.Textarea(attrs={
                'rows': 5,
                'placeholder': 'Make your resume stand out with a simple self introduction. (3-5 lines recommended)',
            }),
        }

class CareerForm(forms.ModelForm):
    since = forms.DateField(input_formats=['%Y.%m'], widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': '0000.0',
    }))
    until = forms.DateField(input_formats=['%Y.%m'], required=False, widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': '0000.0',
    }))

    class Meta:
        model = Career
        fields = '__all__'
        exclude = ('resume',)
        widgets = {
            'company': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Company Name',
            }),
            'position': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Task/Position',
            }),
            'achievement': forms.Textarea(attrs={
                'class': 'form-control',
                'placeholder': 'Major Achievement',
                'rows': 3,
            }),
            'currently_employed': forms.CheckboxInput(attrs={
                'class': 'custom-control-input form-control float-left',
            }),
        }

    def clean(self):
        cleaned_data = super().clean()
        if cleaned_data['currently_employed'] == True:
            cleaned_data['until'] = timezone.now().date()
        # Check since < until
        if cleaned_data['since'] >= cleaned_data['until']:
            raise ValidationError('"Until" should be later than "Since"')

class EducationForm(forms.ModelForm):
    since = forms.DateField(input_formats=['%Y.%m'], widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': '0000.0',
    }))
    until = forms.DateField(input_formats=['%Y.%m'], required=False, widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': '0000.0',
    }))
    class Meta:
        model = Education
        fields = '__all__'
        exclude = ('resume',)
        widgets = {
            'school_name': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'School Name',
            }),
            'major': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Major',
            }),
            'research_content': forms.Textarea(attrs={
                'class': 'form-control',
                'placeholder': 'Compeletion Courses or Research Content',
                'rows': 3,
            }),
            'currently_attending': forms.CheckboxInput(attrs={
                'class': 'custom-control-input form-control',
            }),
        }

    def clean(self):
        cleaned_data = super().clean()
        if cleaned_data['currently_attending'] == True:
            cleaned_data['until'] = timezone.now().date()
        # Check since < until
        if cleaned_data['since'] >= cleaned_data['until']:
            raise ValidationError('"Until" should be later than "Since"')

class AwardForm(forms.ModelForm):
    at = forms.DateField(input_formats=['%Y.%m'], widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': '0000.0',
    }))
    class Meta:
        model = Award
        fields = '__all__'
        exclude = ('resume',)
        widgets = {
            'activity_name': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Activity Name',
            }),
            'detail': forms.Textarea(attrs={
                'class': 'form-control',
                'placeholder': 'Detail',
                'rows': 3,
            }),
        }

class LinkForm(forms.ModelForm):
    class Meta:
        model = Link
        fields = '__all__'
        exclude = ('resume',)
        widgets = {
            'github': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Github ID',
            }),
            'stackoverflow': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Stackoverflow URL',
            }),
            'linkedin': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Linked URL',
            }),
            'facebook': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Facebook ID',
            }),
            'twitter': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Twitter ID(except @)',
            }),
            'google_plus': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Google+ URL',
            }),
            'blog': forms.URLInput(attrs={
                'class': 'form-control',
                'placeholder': 'Blog URL',
            })
        }


from .core import ReferenceGenerator
from .core import PhysicalSystem
from .core import RewardFunction
from .core import ElectricMotorVisualization
from .utils import make, register_superclass

register_superclass(RewardFunction)
register_superclass(ElectricMotorVisualization)
register_superclass(ReferenceGenerator)
register_superclass(PhysicalSystem)

import gym_electric_motor.reference_generators
import gym_electric_motor.reward_functions
import gym_electric_motor.visualization
import gym_electric_motor.physical_systems
import gym_electric_motor.envs
from gym.envs.registration import register

# Add all superclasses of the modules to the registry.

envs_path = 'gym_electric_motor.envs:'

"""register(id='emotor-asm-cont-v0',
         entry_point=envs_path+'ASMContinuousControlEnv')
register(id='emotor-asm-disc-v0',
         entry_point=envs_path+'ASMContinuousControlEnv')"""

register(id='emotor-pmsm-cont-v0',
         entry_point=envs_path+'PmsmCont')
register(id='emotor-pmsm-disc-v0',
         entry_point=envs_path+'PmsmDisc')

# Version 1
register(id='emotor-dc-permex-cont-v1',
         entry_point=envs_path+'ContDcPermanentlyExcitedMotorEnvironment')
register(id='emotor-dc-permex-disc-v1',
         entry_point=envs_path+'DiscDcPermanentlyExcitedMotorEnvironment')
register(id='emotor-dc-extex-cont-v1',
         entry_point=envs_path+'ContDcExternallyExcitedMotorEnvironment')
register(id='emotor-dc-extex-disc-v1',
         entry_point=envs_path+'DiscDcExternallyExcitedMotorEnvironment')
register(id='emotor-dc-series-cont-v1',
         entry_point=envs_path+'ContDcSeriesMotorEnvironment')
register(id='emotor-dc-series-disc-v1',
         entry_point=envs_path+'DiscDcSeriesMotorEnvironment')
register(id='emotor-dc-shunt-cont-v1',
         entry_point=envs_path+'ContDcShuntMotorEnvironment')
register(id='emotor-dc-shunt-disc-v1',
         entry_point=envs_path+'DiscDcShuntMotorEnvironment')
register(id='emotor-pmsm-cont-v1',
         entry_point=envs_path+'ContPermanentMagnetSynchronousMotorEnvironment')
register(id='emotor-pmsm-disc-v1',
         entry_point=envs_path+'DiscPermanentMagnetSynchronousMotorEnvironment')
register(id='emotor-synrm-cont-v1',
         entry_point=envs_path+'ContSynchronousReluctanceMotorEnvironment')
register(id='emotor-synrm-disc-v1',
         entry_point=envs_path+'DiscSynchronousReluctanceMotorEnvironment')


import requests  #import library

def check_internet():  #create function
    url = "https://www.youtube.com"       # what ever url we want to check
    timeout = 5 #after 5 seconds it will run
    try:
        _ = requests.get(url, timeout=timeout)  # request send
        print("all good")
        return True
    except requests.ConnectionError: # except block
        print("please check internet connection")
    return False

check_internet() # function call

#loop_value = 1
#while loop_value:
 #   try:
  #      urlopen("https://google.com")
   # except urllib2.URLError:
    #    f.write("hey network is down!!")
    #else:
    #    print("hey you are on...")
     #   loop_value = 0

#print ("test 1")
#loop_value = 1
#while loop_value == 1:
#    print ("test 2")
#    try: urllib.request.urlopen("http://google.com")
#    except urllib.error.URLError as e:
#        print(e.reason)
#        f.write( "Network currently down." )
#        time.sleep(5)
#    else:
#         print( "Up and running." )


# Copyright 2018 Argo AI, LLC

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#    http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from caffe.proto import caffe_pb2
from google.protobuf import text_format
from collections import OrderedDict

class LayerRecord(object):
    """A record which describes layer parameters.
    """

    def __init__(self, layer_def):
        """Reads layer defintion and sets up
        default parameters if none provided and
        coerces convolution parameters to lists instead of ints.
        Sets up layer tops and bottom.

        Args:
            layer_def (caffe layer): Caffe layer definition
        """
        self.layer_def = layer_def
        self.name = layer_def.name
        self.type = layer_def.type
        if layer_def.type in ('Pooling', 'Convolution'):
            if not isinstance(layer_def.convolution_param.kernel_size, list):
                self.filter = [layer_def.convolution_param.kernel_size]
            if len(self.filter) == 1:
                self.filter *= 2

            if not isinstance(layer_def.convolution_param.pad, list):
                self.pad = [layer_def.convolution_param.pad]
            if len(self.pad) == 0:
                self.pad = [0, 0]
            elif len(self.pad) == 1:
                self.pad *= 2

            if not isinstance(layer_def.convolution_param.stride, list):
                self.stride = [layer_def.convolution_param.stride]
            if len(self.stride) == 0:
                self.stride = [1, 1]
            elif len(self.stride) == 1:
                self.stride *= 2
        else:
            self.filter = [0, 0]
            self.pad = [0, 0]
            self.stride = [1, 1]

        self.tops = list(layer_def.top)
        self.bottoms = list(layer_def.bottom)
        self.parents = []
        self.children = []

def read_network_dag(deploy_prototxt):
    """ Reads network structure from caffe_prototxt.

    Args:
        deploy_prototxt (str): name of prototxt to load,

    Returns:
        layer_name_to_record: maps layer name to structure which
          describes in a simple form the layer parameters
        top_to_layers: maps blob name to an ordered list of layers which output that blob
    """

    # load prototxt file
    network_def = caffe_pb2.NetParameter()
    with open(deploy_prototxt, 'r') as proto_file:
        text_format.Merge(str(proto_file.read()), network_def)

    # map layer name to layer record
    layer_name_to_record = OrderedDict()
    for layer_def in network_def.layer:
        if (len(layer_def.include) == 0) or \
           (caffe_pb2.TEST in [item.phase for item in layer_def.include]):
            layer_name_to_record[layer_def.name] = LayerRecord(layer_def)

    top_to_layers = dict()
    for layer in network_def.layer:
        # no specific phase, or TEST phase is specifically asked for
        if (len(layer.include) == 0) or \
                (caffe_pb2.TEST in [item.phase for item in layer.include]):
            for top in layer.top:
                if top not in top_to_layers:
                    top_to_layers[top] = []
                top_to_layers[top].append(layer.name)

    # find parents and children of all layers
    for child_layer_name in layer_name_to_record.keys():
        child_layer_def = layer_name_to_record[child_layer_name]
        for bottom in child_layer_def.bottoms:
            if bottom in top_to_layers:
                for parent_layer_name in top_to_layers[bottom]:
                    if parent_layer_name in layer_name_to_record:
                        parent_layer_def = \
                            layer_name_to_record[parent_layer_name]
                        if parent_layer_def not in child_layer_def.parents:
                            child_layer_def.parents.append(parent_layer_def)
                        if child_layer_def not in parent_layer_def.children:
                            parent_layer_def.children.append(child_layer_def)

    return layer_name_to_record, top_to_layers


import pandas as pd
from scipy.optimize import curve_fit
import numpy as np
from collections import Counter
import scipy as sp
import datetime
import requests
import time

def telegram_bot_sendtext(bot_message):
    bot_token = '618426064:AAEPwV7G8fmf88r53RcFQ_aJm53AfvOj86Y'
    bot_chatID = '140278675'
    send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message

    response = requests.get(send_text)

    return response.json()

today = datetime.date.today()-datetime.timedelta(days=1)
country_data=pd.read_csv(f'./agged-record-data/country-day-summary-{today}.csv')
data=pd.read_csv(f'./agged-record-data/city-day-summary-{today}.csv')

data=data.sort_values(by=['provinceName', 'cityName','updateTime'])
data['updateTime']=pd.to_datetime(data['updateTime'])

city_list=list(Counter(data['cityName']))
city=list()
k_list=list()
b_list=list()

color=['r','y','g','b','m','c','lawngreen','sandybrown','darkviolet','hotpink']
def rmse(y_test, y):
    return sp.sqrt(sp.mean((y_test - y) ** 2))

for x in city_list:
    # i=result['city'][x]
    value = np.log10(data[data['cityName'] == x]['city_confirmedCount'])
    value.index = [(k - datetime.datetime(2020, 1, 23)).days for k in data[data['cityName'] == x]['updateTime']]
    value=value.rolling(3).mean()
    if len(value[15:])<6:
        continue
    # name = p.get_pinyin(f"{i}", "").capitalize()
    try:
        # popt, pcov = curve_fit(lambda t,k, b: k * t+b, list(range(len(value)))[-4:],value[-4:])
        popt, pcov = curve_fit(lambda t, k, b: k * t + b, value.index[-4:], value[-4:])
        city.append(x)
        k_list.append(popt[0])
        b_list.append(popt[1])

    except:
        continue

result=pd.DataFrame()
result['city']=city
result['k']=k_list
result['b']=b_list
result.sort_values(by='k',inplace=True,ascending=False)
result.to_csv(f'./result/slope-log-mean-{today}.csv',index=0)
test = telegram_bot_sendtext(f'calculate finish {time.strftime("%Y-%m-%d %H:%M:%S")} ')


from fuze import util, log
from sqlalchemy.orm import scoped_session, Session as _Session
from sqlalchemy.orm import Session, sessionmaker
from fuze.events import switchboard

class Session(_Session):
    def __init__(self, *args, **kwds):
        _Session.__init__(self, *args, **kwds)
        self._state = {
            "receipts": None,
            "transaction": None
        }

    def commit(self):
        try:
            _Session.commit(self)
        except Exception as ex:
            msg = ex.message if hasattr(ex, "message") else str(ex)
            msg = "Error committing session: " + msg
            print("<ERROR> " + msg)
            log.exception(msg)

            try:
                _Session.rollback(self)
            except:
                pass
            raise

    def rollback(self):
        try:
            _Session.rollback(self)
        except Exception as ex:
            msg = ex.message if hasattr(ex, "message") else str(ex)
            msg = "Error rolling back session: " + msg
            print("<ERROR> " + msg)
            log.exception(msg)
            raise

    def append(self, *models, **params):
        models = util.unroll(models)
        if params.get("bulk"):
            self.bulk_save_objects(models)
        else:
            for model in models:
                self.add(model)
        if params.get("commit"):
            self.commit()
        return self

    def save(self, *models):
        events = []
        for model in models:
            event = "model.create" if not model.id else "model.update"
            events.append([event, model])
            self.add(model)

        # if models and isinstance(models[0], list) and len(models[0]) > 10:
        #     self.bulk_save_objects(models[0])
        # else:
        #     for model in models:
        #         self.add(model)
        #         # with model as sanitized:
        #         #     self.add(sanitized)
        # # if not self.is_transaction:
        # #     self.commit()

        self.commit()

        receipts = None
        for event, model in events:
            receipt = switchboard.events.bubble(event, model)
            if receipt:
                if receipts is None:
                    receipts = []
                receipts.append(receipt)

        if receipts:
            self.bulk_save_objects(receipts)

        return self

    # def close(self):
    #     print("SESSION CLOSE...")
    #     if self._state:
    #         receipts = self.__dict__["_state"].pop("receipts", None)
    #         print(receipts)
    #         if receipts:
    #
    #             self.bulk_save_objects(receipts)
    #
    #     return self

    def dispose(self):
        print("SESSION DISPOSE...")
        # self.close()
        # if self._state:
        #     receipts = self._state.pop("receipts", None)
        #     if receipts:
        #         self.bulk_save_objects(receipts)

        for k, v in self.__dict__.items():
            # print("DISPOSE: " + str(k))
            self.__dict__[k] = None

    # @property
    # def inside_context(self):
    #     return self._state["inside_context"]
    #
    # @inside_context.setter
    # def inside_context(self, flag):
    #     self._state["inside_context"] = True if flag else False

    def __del__(self):
        self.dispose()

    # def __enter__(self):
    #     # self.inside_context = True
    #     self._state["transaction"] = True
    #     return self
    #
    # def __exit__(self, exc_type, exc_val, exc_tb):
    #     # self.inside_context = False
    #
    #     self._state["transaction"] = False
    #     if isinstance(exc_val, Exception):
    #         self.rollback()
    #         raise exc_val
    #     self.commit()
    #
    # @property
    # def is_transaction(self):
    #     return self._state.get("transaction")

    @classmethod
    def create(cls, engine):
        session_factory = sessionmaker(bind=engine, expire_on_commit=False, class_=Session)
        session_class = scoped_session(session_factory)
        instance = session_class()
        setattr(instance, "clone", session_class)
        return instance


# chat/consumers.py
import json
from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
from polls.models import *

#MyModel.objects.filter(pk=some_value).update(field1='some value')

class ChatConsumer(WebsocketConsumer):
    def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name

        print(self.scope["user"])
        # Join room group
        async_to_sync(self.channel_layer.group_add)(
            self.room_group_name,
            self.channel_name
        )

        self.accept()

    def disconnect(self, close_code):
        # Leave room group
        async_to_sync(self.channel_layer.group_discard)(
            self.room_group_name,
            self.channel_name
        )

    # Receive message from WebSocket
    def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']

        # Send message to room group
        async_to_sync(self.channel_layer.group_send)(
            self.room_group_name,
            {
                'type': 'chat_message',
                'message': message
            }
        )

    # Receive message from room group
    def chat_message(self, event):
        message = event['message']

        # Send message to WebSocket
        self.send(text_data=json.dumps({
            'message': message
        }))

class CommentsConsumer(WebsocketConsumer):
    def connect(self):
        self.accept()

    def receive(self, text_data):
        text_data_json = json.loads(text_data)

        if text_data_json['purpose'] == 'save':

            theid = text_data_json['theid']
            thenewcomment = text_data_json['thenewcomment']
            #print('######## ',theid,thenewcomment)
            Comments.objects.filter(commentId=theid).update(comment=thenewcomment)

            self.send(text_data=json.dumps({
                'from': 'save',
                'message': 'success'
            }))

        if text_data_json['purpose'] == 'delete':

            theid = text_data_json['theid']
            #print('######## ',theid,thenewcomment)
            Comments.objects.filter(commentId=theid).delete()

            self.send(text_data=json.dumps({
                'from': 'delete',
                'message': 'success'
            }))

        if text_data_json['purpose'] == 'deleteIssue':

            theid = text_data_json['theid']
            #print('######## ',theid,thenewcomment)
            Issues.objects.filter(theid=theid).delete()

            self.send(text_data=json.dumps({
                'from': 'deleteIssue',
                'message': 'success'
            }))

        if text_data_json['purpose'] == 'solvedIssue':

            theid = text_data_json['theid']
            #print('######## ',theid)
            Issues.objects.filter(theid=theid).update(isIssueSolved=True)

            self.send(text_data=json.dumps({
                'from': 'solvedIssue',
                'message': 'success'
            }))

        if text_data_json['purpose'] == 'solvedIssueComment':

            theid = text_data_json['theid']
            #print('######## ',theid)
            Comments.objects.filter(commentId=theid).update(isThisCommentSolution=True)

            self.send(text_data=json.dumps({
                'from': 'solvedIssue',
                'message': 'success'
            }))

class NotificationConsumer(WebsocketConsumer):

    def connect(self):
        self.accept()

    def receive(self, text_data):
        text_data_json = json.loads(text_data)
        print('yo')
        if text_data_json['purpose'] == 'update_notification':

            theid = text_data_json['theid']
            Notifications.objects.filter(notificationID=theid).update(isSeen=True)


# Project By : Akshay Jinde ( SE. Comp)

import json
import urllib.request
from tkinter import *
import tkinter.messagebox

def status1():
    api_key = "te6o4xl5"

    en_text = en_1.get()
    date = en_2.get()

    len1 = len(str(en_text))

    if len1 > 5:
        tkinter.messagebox.showerror('Error !!', 'Train Number Must Contain 5 Numbers')

    url = ('http://api.railwayapi.com/live/train/' + en_text + '/doj/' + date + '/apikey/' + api_key + '/')
    a = urllib.request.urlopen(url)

    b = a.read()
    obj = json.loads(b.decode())

    str_date = (obj['start_date'])
    # curr_day = (obj['current_station']['day'])
    curr_sta = (obj['current_station']['station_']['name'])
    shd_dep = (obj['current_station']['schdep'])
    acc_arr = (obj['current_station']['actarr'])
    sta_code = (obj['current_station']['station'])
    act_dep = (obj['current_station']['actdep'])
    sch_arr = (obj['current_station']['scharr'])
    late_m = (obj['current_station']['status'])
    status = (obj['position'])

    last = Tk()

    re_strdte = Label(last, text="Starting Date Of Train : ")
    re_strdte.grid(row=0, column=0, sticky=E)

    strdte = Label(last, text=str_date)
    strdte.grid(row=0, column=1)

    re_cursta = Label(last, text="Current Station : ")
    re_cursta.grid(row=1, column=0, sticky=E)

    cursta = Label(last, text=curr_sta)
    cursta.grid(row=1, column=1)

    re_stacod = Label(last, text="Station Code : ")
    re_stacod.grid(row=2, column=0, sticky=E)

    stacod = Label(last, text=sta_code)
    stacod.grid(row=2, column=1)

    re_shdarr = Label(last, text="Scheduled Arrival : ")
    re_shdarr.grid(row=3, column=0, sticky=E)

    shdarr = Label(last, text=sch_arr)
    shdarr.grid(row=3, column=1)

    re_accarr = Label(last, text="Actual Arrival : ")
    re_accarr.grid(row=4, column=0, sticky=E)

    accarr = Label(last, text=acc_arr)
    accarr.grid(row=4, column=1)

    re_shddep = Label(last, text="Scheduled Departure : ")
    re_shddep.grid(row=5, column=0, sticky=E)

    shddep = Label(last, text=shd_dep)
    shddep.grid(row=5, column=1)

    re_accdep = Label(last, text="Actual Departure : ")
    re_accdep.grid(row=6, column=0, sticky=E)

    accdep = Label(last, text=act_dep)
    accdep.grid(row=6, column=1)

    re_late = Label(last, text="Late By : ")
    re_late.grid(row=7, column=0, sticky=E)

    late = Label(last, text=late_m)
    late.grid(row=7, column=1)

    re_sta = Label(last, text="Current Train Status : ")
    re_sta.grid(row=8, column=0, sticky=E)

    sta = Label(last, text=status)
    sta.grid(row=8, column=1)

    b1 = Button(last, text="Exit", command=last.destroy)
    b1.grid(row=9, column=1)
    last.mainloop()

root = Tk()

photo = PhotoImage(file="icon.png")
head = Label(root, image=photo)
head.grid(row=0, column=0, sticky=E)

tr_label = Label(root, text="Enter Train Number : ")
tr_label.grid(row=1, column=0, sticky=E)
en_1 = Entry(root, bg="grey", fg="yellow")
en_1.grid(row=1, column=1)

da_label = Label(root, text="Enter Date Of Journey (YYYYMMDD): ")
da_label.grid(row=2, column=0, sticky=E)
en_2 = Entry(root, bg="grey", fg="yellow")
en_2.grid(row=2, column=1)

button2 = Button(root, text="Exit", fg="red", command=root.destroy)
button2.grid(row=3, column=0)
button1 = Button(root, text="GET LIVE RUNNING STATUS", fg="blue", command=status1)
button1.grid(row=3, column=1)

root.mainloop()


import pronouncing
import pyphen
from num2words import num2words as n2w
from syllables import estimate

from lib.constants import (
    BANNED_WORDS,
    BANNED_PHRASES,
    CHARS_ONLY,
    PRONUNCIATION_OVERRIDES,
    LICK_STRESSES,
    LICK_NOTES
)

dic = pyphen.Pyphen(lang="en_UK")

def isLick(title: str):
    """
    Returns whether or not the argument is pronounced in a way that matches THE LICC.

    :param title: the string to be tested
    :return: True, if it matches, False if not.
    """
    if containsBanned(title):
        return False

    clean = cleanStr(title)
    stresses = getTitleStresses(clean)
    return LICK_STRESSES.match(stresses) is not None

def getHyphenation(title: str):
    """Splits the title into words and its words into possible hyphenations.

    :param title: The string to split and hyphenate
    :return: A list (representing the whole title) containing lists (representing words)
            containing strings (representing hyphenated parts of the word)
    """
    return [dic.inserted(word).split("-") for word in title.split()]

def adjustHyphenation(hyphenation: list):
    """
    Adjusts a list of possible hyphenations in the format of getHyphenation(str),
    so that the amount of (deep) elements is equal to the amount of notes used by THE LICC.
    Note that this modifies the argument list.

    :param hyphenation: A list in the format of what getHyphenation(str) returns
    :return: the argument or None if it couldn't be adjusted.
    """
    for wordIndex in range(len(hyphenation)):
        word = hyphenation[wordIndex]
        for syllableIndex in range(len(word)):
            syllable = word[syllableIndex]
            if estimate(syllable) > 1:
                half = int(len(syllable) / 2) + 1
                word.insert(syllableIndex, syllable[:half])
                word.insert(syllableIndex + 1, syllable[half:])
                word.remove(syllable)
            if sum(map(lambda l: len(l), hyphenation)) == LICK_NOTES:
                return hyphenation

def containsBanned(title: str):
    """
    Return True if banned words or phrases in string.

    This implementation is slow, but is was fast to write and I don't care about
    speed for this script.
    """

    def _containsBannedWord(title: str):
        for word in title.split():
            word = CHARS_ONLY.sub("", word.lower())
            if word in BANNED_WORDS:
                return True
        return False

    def _containsBannedPhrase(title: str):
        for phrase in BANNED_PHRASES:
            if phrase in title.lower():
                return True
        return False

    return _containsBannedWord(title) or _containsBannedPhrase(title)

def getTitleStresses(title: str):
    """Takes a wikipedia title and gets the combined stresses of all words.

    >>> getTitleStresses('Teenage Mutant Ninja Turtles')
    '12101010'

    Args:
        title: String, title of a wikipedia page.
    Returns:
        String, stresses of each syllable as 0, 1, and 2s.
    """
    title_words = title.split()
    title_stresses = ""
    while title_words:
        word = title_words.pop(0)
        word_stresses = getWordStresses(word)
        # If word was a long number, it may have been parsed into several words.
        if isinstance(word_stresses, list):
            title_words = word_stresses + title_words
        elif isinstance(word_stresses, str):
            title_stresses += word_stresses

    return title_stresses

def getWordStresses(word: str):
    word = numbersToWords(word)
    if " " in word:
        return word.split()

    for override, stresses in PRONUNCIATION_OVERRIDES:
        if word.lower() == override.lower():
            return stresses

    try:
        phones = pronouncing.phones_for_word(word)
        stresses = pronouncing.stresses(phones[0])
    except IndexError:
        # Hacky way of discarding candidate title
        return "?"
    return stresses

def numbersToWords(word):
    ordinal_number_endings = ("nd", "rd", "st", "th")
    if word.isdigit():
        if len(word) == 4:
            try:
                word = n2w(word, to="year")
            except Exception:
                # Hacky way of discarding candidate title
                return "9"
        else:
            try:
                word = n2w(word)
            except Exception:
                # Hacky way of discarding candidate title
                return "9"
    if word[:-2].isdigit() and word[-2:] in ordinal_number_endings:
        word = word[-2:]
        try:
            word = n2w(word, to="ordinal")
        except Exception:
            # Hacky way of discarding candidate title
            return "9"

    return word

def cleanStr(s: str):
    """Remove characters that the pronouncing dictionary doesn't like.

    This isn't very efficient, but it's readable at least. :-)

    >>> cleanStr('fooBar123')
    'fooBar123'

    >>> cleanStr('Hello ([world])')
    'Hello world'

    >>> cleanStr('{hello-world}')
    'hello world'

    Args:
        s: String to be stripped of offending characters
    Returns:
        String without offending characters
    """
    DEL_CHARS = ["(", ")", "[", "]", "{", "}", ",", ":", ";", "."]
    SWAP_CHARS = [("-", " ")]

    for char in DEL_CHARS:
        s = s.replace(char, "")

    for char, replacement in SWAP_CHARS:
        s = s.replace(char, replacement)

    return s


# -*- coding: utf-8 -*-
"""
Created on Mon May  3 17:00:00 2021

@author: Harriet Fiagbor

Title : pygame drawing
"""

import pygame, sys
from pygame.locals import *

pygame.init()

# set up the window
DISPLAYSURF = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("Drawing")

# set up the colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# draw on the surface
DISPLAYSURF.fill(WHITE)
pygame.draw.polygon(DISPLAYSURF, RED,  ((146, 0), (291, 106), (236, 277),(56, 277), (0, 106)))
pygame.draw.line(DISPLAYSURF, GREEN, (60,60), (120, 60), 4)
pygame.draw.line(DISPLAYSURF, GREEN, (120, 60), (60, 120))
pygame.draw.line(DISPLAYSURF, GREEN, (60, 120), (120,120 ),4)
pygame.draw.circle(DISPLAYSURF, BLUE, (300, 60), 20 )
pygame.draw.ellipse(DISPLAYSURF, BLACK, (300, 250, 50, 60))
pygame.draw.rect(DISPLAYSURF, GREEN,(200, 150, 150, 100))

#this is assigning a pixel to a color
pixObj = pygame.PixelArray(DISPLAYSURF)
pixObj[480][380] = BLACK
pixObj[482][382] = BLACK
pixObj[484][384] = BLACK
pixObj[486][386] = BLACK
pixObj[488][388] = BLACK
del pixObj

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.update()


"""
================================================================
Logistic regression comparison: ``scikit-learn`` versus ``tick``
================================================================

In this example we give a naive comparison of ``tick`` and ``scikit-learn`` for
binary classification using logistic regression with :math:`\ell_1`
penalization.
This comparison is done using the well-known ``adult`` dataset, a standard
benchmark dataset for binary clasification.
Some remarks are the following:

* Both classifiers have the same performance in terms of AUC (area under the
  ROC curve)
* Learned model-weights are slightly different. This is explained by the fact
  that ``scikit-learn`` uses ``liblinear`` for optimization of the
  :math:`\ell_1`-penalized likelihood. When using this solver, the
  ``intercept`` is penalized like the model weights (``coeff_``), while this is
  not the case in `tick`. Note that this difference can be reduced by tuning the
  ``intercept_scaling`` parameter from ``scikit-learn``'s
  ``LogisticRegression``
* In this example, the computational time of ``tick`` is better than ``scikit``'s
"""
import numpy as np
from time import time
import matplotlib.pyplot as plt

from sklearn.metrics import roc_curve, auc
from sklearn.linear_model import LogisticRegression as LogRegScikit

from tick.dataset import fetch_tick_dataset
from tick.linear_model import LogisticRegression as LogRegTick

train_set = fetch_tick_dataset('binary/adult/adult.trn.bz2')
test_set = fetch_tick_dataset('binary/adult/adult.tst.bz2')

# scklearn 0.22 default solver for LogisticRegression is lbfgs
clf_tick = LogRegTick(C=1e5, penalty='l1', tol=1e-8)
clf_scikit = LogRegScikit(penalty='l1', solver='liblinear', tol=1e-8)

t1 = time()
clf_tick.fit(train_set[0], train_set[1])
t_tick = time() - t1

t1 = time()
clf_scikit.fit(train_set[0], train_set[1])
t_scikit = time() - t1

pred_tick = clf_tick.predict_proba(test_set[0])
pred_scikit = clf_scikit.predict_proba(test_set[0])

fpr_tick, tpr_tick, _ = roc_curve(test_set[1], pred_tick[:, 1])
fpr_scikit, tpr_scikit, _ = roc_curve(test_set[1], pred_scikit[:, 1])

plt.figure(figsize=(10, 8))

ax1 = plt.subplot2grid((2, 2), (0, 0))
plt.stem(clf_tick.weights)
plt.title(r'Model-weights in $\mathtt{tick}$', fontsize=16)
plt.ylim((-2, 2.5))

ax2 = plt.subplot2grid((2, 2), (0, 1))
plt.stem(np.ravel(clf_scikit.coef_))
# plt.legend()
plt.ylim((-2, 2.5))
plt.title(r'Model-weights in $\mathtt{scikit-learn}$', fontsize=16)

plt.subplot2grid((2, 2), (1, 0))
plt.plot(fpr_tick, tpr_tick, lw=2)
plt.plot(fpr_scikit, tpr_scikit, lw=2)
plt.legend([
    "tick (AUC = {:.2f})".format(auc(fpr_tick, tpr_tick)),
    "scikit-learn (AUC = {:.2f})".format(auc(fpr_tick, tpr_tick))
], loc='center right', fontsize=12)
plt.ylabel("True Positive Rate", fontsize=14)
plt.xlabel("False Positive Rate", fontsize=14)
plt.title('ROC curves comparison', fontsize=16)

ax4 = plt.subplot2grid((2, 2), (1, 1))
plt.bar([1, 2], [t_tick, t_scikit])
ax4.set_xticks([1, 2])
ax4.set_xticklabels(['tick', 'scikit-learn'], fontsize=14)
plt.title('Computational time (seconds)', fontsize=16)

plt.tight_layout()
plt.show()


#Assignment

#Question -1

#Method -1
from file_read_backwards import FileReadBackwards

with FileReadBackwards("passage.txt", encoding="utf-8") as frb:
    # getting lines by lines starting from the last line up
    for l in frb:
        print(l)

#Method -2
import pathlib

class FileNameNullError(NameError):
    pass

def inputHandlinig():
    """
    This funtion takes no parameters and returns nothing.
    It is used to parse the input and validate it, by verdict of few small things.
    First it is checked, file name is given or not, if not given then prompt to end use for entering again
    after 2 un-successfull atempts, it gets autoclosed.
    After checking enterd file name is correct, then two smallers checks are also done, by pathlib module.
    By Pathlib Module, the file exsistence and checked it is actually a file.
    after all successfull validations, file desired or mented operation is done and result is onto EndUser screen.
    :return:
    """

    fileName      = str()
    blFileCheck   = False
    askToContinue = 'n'

    while True:
         try:
             fileName = input("What is Your File Name  [Enter With Extension is Mandatary from Now.] :: ")
             if fileName == None:
                 raise FileNameNullError(fileName)
             else :
                 blFileCheck = fileExsists(fileName)
                 if blFileCheck :
                     break
                 else:
                     askToContinue = input("Want to Read Again [Y -> yes , N -> no]")
                     if askToContinue == 'Y':
                         inputHandlinig()
                     else:
                         break
         except Exception as exe:
             print("Not Accepted file name")

def fileExsists(fileName):
    """
    This function is used to check whether file exsists or not plus it is actually a file.
    :param fileName:
    :return: Boolean True if file is according to specified constarints or False if one of the limits comes True.
    """

    path = pathlib.Path(fileName)
    #blFileCheck = True
    RNR = int()

    if path.is_file() == True and path.exists() == True:
        RNR = int(input("Enter 1 -> Reverse else  -> Noraml _______"))
        rdFileContents(fileName,RNR)
        return True
    else:
        print("Misleaded by {}".format(fileName))
        return False

    """blFileCheck = True  if path.is_file() == True and path.exists() == True else blFileCheck = False
    rdFileContents(fileName)  if blFileCheck == True else print("Misleaded by {}".format(fileName))
    return blFileCheck """

def rdFileContents(fileName,revCheck):
    """
    Takes 2 parameters, one is fileName and another is revCheck.
    Its obiviouys first one is FileName and second is used for knowing, HowTo doso further.
    if it is 1 then traverse from end else do traditional.
    This function is used for printing or reading the file either in reversed or traditionl manner.
    :param fileName: Name of File
    :param revCheck: if it is 1, then do Reverse print else Normal Print
    :return: Nothing
    """
    if revCheck != 1:
        with open(fileName) as fileByDefault:
            for line in fileByDefault:
               print(line)
    else:
        with open(fileName) as fileByDefault:
            for line in reversed(fileByDefault):
                print(line)

inputHandlinig()

#Question -2

#Method -1
from collections import Counter

fname = input("Enter file name: ")

num_words = 0

with open(fname, 'r') as f:
    for line in f:
        words = line.split()
        num_words += len(words)
print("Number of words:")
print(num_words)

def word_count(fname):
        with open(fname) as f:
                return Counter(f.read().split())

print(" words in the file :",word_count(fname))

#Method -2

"""
Methods work simillar desptie of one, which is prescribed below.
"""
def inputHandlinig():

    fileName      = str()
    blFileCheck   = False
    askToContinue = 'n'

    while True:
         try:
             fileName = input("What is Your File Name  [Enter With Extension is Mandatary from Now.] :: ")
             if fileName == None:
                 raise FileNameNullError(fileName)
             else :
                 blFileCheck = fileExsists(fileName)
                 if blFileCheck :
                     break
                 else:
                     askToContinue = input("Want to Read Again [Y -> yes , N -> no]")
                     if askToContinue == 'Y':
                         inputHandlinig()
                     else:
                         break
         except Exception as exe:
             print("Not Accepted file name")

def fileExsists(fileName):

    path = pathlib.Path(fileName)
    #blFileCheck = True
    RNR = int()

    if path.is_file() == True and path.exists() == True:

        countAppearances(fileName)
        return True
    else:
        print("Misleaded by {}".format(fileName))
        return False

    """blFileCheck = True  if path.is_file() == True and path.exists() == True else blFileCheck = False
    rdFileContents(fileName)  if blFileCheck == True else print("Misleaded by {}".format(fileName))
    return blFileCheck """

def countAppearances(fileName):
    """
    This function is used to count Characters, Words and lines inside a file.
    By appliance with with--open--as.
    At first lines are counted from file, lenght of list is number of lines. Thus, each line is stored as
    distinct ele in list and then passed to var lines and by len() number of lines are gained.
    Thereafter number of words in each line is counted by use of for loop and split() function.
    BY split each line on spaces, sum up the lengths of the lists of words
    Then number of characters are counted not unique, redudancy is included not excluded.
    To calculate each line is summed up and += is done for characters with charac var.
    Atlast prints the Number of charac/words/lenght.
    :param fileName: Name of File
    :return:    Nothing

    """
    with open(fileName) as fileBYDefault:

        text   = str()
        words  = str()
        lines  = str()
        charac = str()

        lines = len(text)
        words = sum(len(line.split()) for line in text)
        charac = sun(len(line) for line in text)

        print("Number of Lines      -----------------------------    {}".format(lines))
        print("Number of Words      -----------------------------    {}".format(words))
        print("Number of Characters -----------------------------    {}".format(charac))

#Question -3

#Method -1
"""

In this a file is passed, followed by above inputHandling (). By use of with and open file is read and nested done for
another file in 'w' mode i.e. WriteMode.
As each line is parsed or iterated, each line is write onto another file.
Thus, File1 is copied onto File2.
Atlast readed it for confirmation for end user, by displaying to endUser on o/p screen.

"""
with open("passage.txt") as f:
    with open("passage_2.txt","w") as f1:
        for line in f:
            f1.write(line)
with open("passage_2.txt") as frb:
     for l in frb:
         print(l)

#Question -4
"""

This is used to traverse and do so on two files together.
Two differential files are operated under 'r' mode together and by for loop, zip inside and zip() function
Atlast a line is returned as concurrent line from both files.
This also follows or approached the same way as done earlier in inputHandling() function.

"""

with open('passage.txt') as fh1, open('passage_2.txt') as fh2:
    for line1, line2 in zip(fh1, fh2):
        # line1 from abc.txt, line2 from test.txtg
        print(line1+line2)

#Question -5

import random

def Rand(start, end, num):
    """
    This Method takes 3 parameters and returns a list of number integreal values.
    The three parameters decide the starting number and ending number for generating random number, last one decids for how long to do this
    simply, it decides how many random numbers are to be generated.
    The three parameters are static.
     Random module is imported and by use of its memeber function randInt -> Random Integers numbers are generated within limits.
     For loop with var 'j' is used to measure the lenght of lists along wiht parameter number
    :param start:
    says the range from which the random numbers are to be generated. for e.g start = 1, then it may result from or so.
    Cannot go <1
    :param end:
    Says the end range point, the numbers are to be generated it is like -end . for e,g,end = 2 then b = 1 of range(a,b,c)
    :param num:
    Number of elements of list
    :return:
    list of integer numbers 'd'
    """
    res = []

    for j in range(num):
        res.append(random.randint(start, end))

    return res

lstR = list()
with open("fileR1.txt","w") as f1:
    """
    This and below three work on same principle, despite of differential implementation.
    Main work is to open a file, if mode is not given then by default is 'r', else work in specified mode .
    with open as are the reserve-words here as is used to put a sticker on file object.
    Function of it, is to call Rand(start,end,number/leng) pass 3 arguments init and store the result onto a list.
    Thereafter wrote the list onto given file
    """
    lstR = Rand(12,57,10)
    f1.write(str(lstR))

lstRR = lstR

with open("fileR2.txt","w") as f2:
    """
    Similar to above, but differ in how being used and same applied for 2 down or below.
    """
    lstRR.sort()
    f2.write(str(lstRR))

with open("fileR1.txt") as f1:
    for ls in f1:
        print(ls)

with open("fileR2.txt") as f1:
    for ls in f1:
        print(ls)


import inspect
import json
import os
import sys
from typing import Callable, Any, Mapping, Optional

import domovoi

pkg_root = os.path.abspath(os.path.join(os.path.dirname(__file__), 'domovoilib'))  # noqa
sys.path.insert(0, pkg_root)  # noqa

from dss import BucketConfig, Config, Replica
from dss.logging import configure_lambda_logging
from dss.stepfunctions.visitation.index import IndexVisitation
from dss.stepfunctions.visitation.storage import StorageVisitation
from dss.util.types import JSON

Config.set_config(BucketConfig.NORMAL)

class Target:
    """
    The target of an admin operation. Targets are nouns (like index, storage) and actions are verbs (like verify,
    repair). After adding a subclass here, a corresponding subcommand (aka target) should be added to the admin CLI (
    admin-cli.py). Every method in the target subclass should correspond to an subsubcommand (aka action) of the
    target subcommand.
    """
    pass

class IndexTarget(Target):
    """
    Admin operations on the Elasticsearch index.
    """
    def __init__(self, replica: str, bucket: str = None, prefix: str = None) -> None:
        super().__init__()
        self.replica = Replica[replica]
        self.bucket = bucket or self.replica.bucket
        self.prefix = prefix or ''

    def repair(self, workers: int) -> JSON:
        return self._start(workers, dryrun=False, notify=False)

    def verify(self, workers: int) -> JSON:
        return self._start(workers, dryrun=True, notify=False)

    def _start(self, workers: int, dryrun: bool, notify: Optional[bool]) -> Mapping[str, Any]:
        assert 1 < workers
        return IndexVisitation.start(workers,
                                     replica=self.replica.name,
                                     bucket=self.bucket,
                                     prefix=self.prefix,
                                     dryrun=dryrun,
                                     notify=notify)

class StorageTarget(Target):
    """
    Admin operations on the storage buckets and the replication between them.
    """
    def __init__(self, replicas: Mapping[str, str], prefix: str = None) -> None:
        super().__init__()
        replicas = ((Replica[replica], bucket) for replica, bucket in replicas.items())
        self.replicas = {replica.name: bucket or replica.bucket for replica, bucket in replicas}
        self.prefix = prefix or ''

    def verify(self, workers: int, folder: str, quick: bool = False) -> JSON:
        assert 1 < workers
        replicas, buckets = zip(*self.replicas.items())
        return StorageVisitation.start(workers,
                                       replicas=replicas,
                                       buckets=buckets,
                                       prefix=self.prefix,
                                       folder=folder,
                                       quick=quick)

def _invoke(f: Callable, kwargs: Mapping[str, Any]) -> Any:
    """
    Invoke the given function with a given dictionary of keyword arguments, silently ignoring any extra arguments.
    """
    signature = inspect.signature(f)
    kwargs = {name: kwargs.get(name, param.default) for name, param in signature.parameters.items()}
    return f(**kwargs)

class DSSAdmin(domovoi.Domovoi):
    def __call__(self, options, context):
        target_class_name = options['target'].capitalize() + Target.__name__
        target_class = globals()[target_class_name]
        assert issubclass(target_class, Target)
        target = _invoke(target_class, options)
        action_name = options['action']
        action = getattr(target, action_name)
        result = _invoke(action, options)
        return json.dumps(result)

configure_lambda_logging()
app = DSSAdmin(configure_logs=False)


import sys
import json

class Person(object):
    """Future Support for Class"""
    def __init__(self, attrs):
        self.attrs = attrs

class ObfuscationWorker(object):
    """Main worker that Obfuscation personal information. To"""
    def __init__(self):
        self.Obfuscation = dict()
        self.Obfuscation["Id"] = False
        self.Obfuscation["Name"] = True
        self.Obfuscation["Address"] = True
        self.Obfuscation["City"] = False
        self.Obfuscation["State"] = False
        self.Obfuscation["Zip"] = False
        self.Obfuscation["SSN"] = True
        self.Obfuscation["Location"] = True
        self.Obfuscation["Phone"] = True
        self.Obfuscation["Countrycode"] = False
        self.Obfuscation["DOB"] = True
        self.Obfuscation["Website"] = True
        self.Obfuscation["Company"] = True
        self.Obfuscation["Occupation"] = False
        self.Obfuscation["HeightCM"] = False
        self.Obfuscation["WeightKG"] = False
        self.Obfuscation["BloodType"] = False
        self.Obfuscation["Vehicle"] = False
        self.Obfuscation["Color"] = False

    def process(self, person):
        """Process all attributes defined in class. Should not be modify by inherit."""
        # for attr, value in person.attrs.items():
        for attr, value in person.items():
            # check method and value exist
            if self.Obfuscation.get(attr, False) and value:
                encoder = getattr(self, "do_"+attr)
                # person.attrs[attr] = encoder(value)
                person[attr] = encoder(value)

    def star_encoder(self, string, start, stop):
        """Encode string[start:stop] to ***"""
        if stop > start:
            ret = string[:start] + '*' * (stop - start) + string[stop:]
            return ret
        else:
            return string

    def do_Name(self, string):
        sentiment = 2
        return self.star_encoder(string, sentiment+1, len(string)-sentiment)

    def do_Address(self, string):
        sentiment = 2
        return self.star_encoder(string, sentiment+1, len(string)-sentiment)

    def do_SSN(self, string):
        sentiment = 2
        return self.star_encoder(string, sentiment+1, len(string)-sentiment)

    def do_Location(self, string):
        splits = string.split(',')
        return len(splits[0]) * '*' + splits[1]

    def do_Phone(self, string):
        area_code_length = 3
        return self.star_encoder(string, area_code_length+1, len(string))

    def do_DOB(self, string):
        splits = string.split(',')
        return len(splits[0]) * '*' + splits[1]

    def do_Email(self, string):
        splits = string.split('@')
        return len(splits[0]) * '*' + splits[1]

    def do_Website(self, string):
        splits = string.split('.')
        return len(splits[0]) * '*' + splits[1]

    def do_Company(self, string):
        sentiment = 2
        return self.star_encoder(string, sentiment+1, len(string)-sentiment)

def load_data(path):
    # persons = []
    with open(path, encoding="utf-8") as f:
        datas = json.load(f)
    # for data in datas:
    #     persons.append(Person(data))
    # return persons
    return datas

def save_data(path, persons):
    with open(path, "w", encoding="utf-8") as f:
        json.dump(persons, f)

if __name__ == "__main__":
    # Parse command line and detect errors
    try:
        _, source_path, dest_path = sys.argv
    except Exception as e:
        print("Error args", sys.argv)
        raise
    persons = load_data(source_path)
    worker = ObfuscationWorker()
    for person in persons:
        worker.process(person)
    save_data(dest_path, persons)


import urllib.request
import urllib.parse
from bs4 import BeautifulSoup

headers = {}
headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"

MAX_PAGES = 3

def search_page(search):
    search = search.split(' ')
    search = '%20'.join(search)

    try:
        url_search = 'https://www.amazon.in/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords={0}&sprefix=thi%2Caps%2C505&crid=6O845RNLNMIR&rh=i%3Aaps%2Ck%3A{0}'.format(search)
        req = urllib.request.Request(url_search, headers=headers)
        resp = urllib.request.urlopen(req)

        respData = resp.read()
        soup = BeautifulSoup(respData, 'html.parser')
        product_link = soup.find('div', class_="a-row a-spacing-small")
        product_link = product_link.a['href']

        return product_link

    except Exception as e:
        print(str(e))

def product_page(search):

    try:
        url_product = search_page(search)
        req = urllib.request.Request(url_product, headers=headers)
        resp = urllib.request.urlopen(req)

        respData = resp.read()
        soup = BeautifulSoup(respData, 'html.parser')
        review_link = soup.find('div', class_="a-row a-spacing-large")
        review_link = "https://www.amazon.in{}".format(review_link.a['href'])

        return review_link

    except Exception as e:
        print(str(e))

def get_response(search, pageNumber):

    try:
        url = product_page(search)
        url = '{0}&pageNumber={1}'.format(url, pageNumber)
        req = urllib.request.Request(url, headers=headers)
        resp = urllib.request.urlopen(req)
        return resp.read()

    except Exception as e:
        print(str(e))

def get_reviews(search):

    # Writing it to a file
    with open('reviews.txt', 'w') as f:

        try:
            for i in range(1, MAX_PAGES + 1):
                respData = get_response(search, i)
                soup = BeautifulSoup(respData, 'html.parser')
                reviews = soup.find_all('div', attrs={'class': "a-section review"})

                for review in reviews:

                    # Getting the review title and star ratings
                    rev_title = str(review.div.div)
                    soup = BeautifulSoup(rev_title, 'html.parser')
                    rev_title = soup.find_all('a', class_="a-link-normal")
                    print("\nStar Ratings: {}".format(rev_title[0].text))
                    print("Review Title: {}".format(rev_title[1].text))

                    # Getting the actual review
                    rev_text = str(review.div)
                    soup = BeautifulSoup(rev_text, 'html.parser')
                    rev_text = soup.find('div', class_="a-row review-data")
                    print("Review Text:-\n{}".format(rev_text.text))
                    print('\n\n')

                    f.write("Star Ratings: {}".format(rev_title[0].text))
                    f.write('\n')
                    f.write("Review Title: {}".format(rev_title[1].text))
                    f.write('\n')
                    f.write("Review Text:-\n{}".format(rev_text.text))
                    f.write('\n\n\n')

        except Exception as e:
            print(str(e))

def main():

    search = input("\nEnter a Book Name to get customer reviews: ")

    get_reviews(search)

if __name__ == '__main__':
    main()